sort

original_code:
void sort(std_t *stdp, int len, int sign)
{
    int i, j, pos;
    std_t tmp;
    printf("in sort...\n");
    for(i = 0; i < len - 1; i++){
        pos = i;
        for(j = i + 1; j < len; j++){
            switch(sign){
            case 1:if(stdp[pos].average < stdp[j].average)
                    pos = j;
                  break;
            case 2:if(stdp[pos].id > stdp[j].id)
                        pos = j;
                    break;
            }
        }
        if(pos != i){
            tmp = stdp[pos];
            stdp[pos] = stdp[i];
            stdp[i] = tmp;
        }
    }
    if(sign == 1)
        display(stdp, len);
}

canonical_original_code:
void sort(struct std_t *stdp, int len, int sign)
{
    int i; int j; int pos;
    struct std_t tmp;
    printf("in sort...\n");
    for(i = 0; i < len - 1; i++){
        pos = i;
        for(j = i + 1; j < len; j++){
            switch(sign){
            case 1:if(stdp[pos].average < stdp[j].average)
                    pos = j;
                  break;
            case 2:if(stdp[pos].id > stdp[j].id)
                        pos = j;
                    break;
            }
        }
        if(pos != i){
            tmp = stdp[pos];
            stdp[pos] = stdp[i];
            stdp[i] = tmp;
        }
    }
    if(sign == 1)
        display(stdp, len);
}

variable_types: j int

variable_types: pos int

variable_types: struct std_t { unsigned int id; char name[20]; unsigned int math; double average; }


repo: akaehy/Project

decompiled_code:
void __fastcall __noreturn sys_err(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void sys_err(const char *msg)
{
    perror(msg);
    exit(1);
}

canonical_original_code:
void sys_err(char *msg)
{
    perror(msg);
    exit(1);
}

variable_types:
msg
char *

return_type: void


decompiled_code:
void __noreturn err_sys(char *format, ...)
{
int v1; // ebp
char *v2; // rbx
size_t v3; // rax
gcc_va_list arg; // [rsp+8h] [rbp-140h] BYREF
char s[2]; // [rsp+20h] [rbp-128h] BYREF

va_start(arg, format);
v1 = *__errno_location();
vsnprintf(s, 0x50uLL, format, arg);
v2 = strerror(v1);
v3 = strlen(s);
snprintf(&s[v3], 80 - v3, " : %s", v2);
*(_WORD *)&s[strlen(s)] = 10;
fputs(s, _bss_start);
exit(1);
}

canonical_decompiled_code:
void __noreturn func7(char *format, ...)
{
int v1; // ebp
char *v2; // rbx
size_t v3; // rax
gcc_va_list arg; // [rsp+8h] [rbp-140h] BYREF
char s[2]; // [rsp+20h] [rbp-128h] BYREF

va_start(arg, format);
v1 = *__errno_location();
vsnprintf(s, 0x50uLL, format, arg);
v2 = strerror(v1);
v3 = strlen(s);
snprintf(&s[v3], 80 - v3, " : %s", v2);
*(_WORD *)&s[strlen(s)] = 10;
fputs(s, _bss_start);
exit(1);
}

original_code:
void err_sys(const char *fmt,...)
{
  int err = (*__errno_location ());
  char buf[80 +1];
  va_list ap;
  __builtin_va_start(ap,fmt);
  vsnprintf(buf, 80, fmt, ap);
  snprintf(buf+strlen(buf), 80 -strlen(buf), " : %s", strerror(err));
  strcat(buf, "\n");
  fputs(buf, stderr);
  __builtin_va_end(ap);
  exit(1);
}

canonical_original_code:
void err_sys(char *fmt, ...)
{
  int err = (*__errno_location ());
  char buf[-1];
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  vsnprintf(buf, 80, fmt, ap);
  snprintf(buf+strlen(buf), 80 -strlen(buf), " : %s", strerror(err));
  strcat(buf, "\n");
  fputs(buf, stderr);
  __builtin_va_end(ap);
  exit(1);
}

variable_types:
fmt
char *

variable_types: err int

variable_types:
buf
char [-1]


decompiled_code:
__int64 __fastcall my_div(int a1, int a2)
{
return (unsigned int)(a1 / a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
return (unsigned int)(a1 / a2);
}

original_code:
int my_div(int a, int b)
{
 return a / b;
}

variable_types: a int

variable_types: b int

return_type: int


main

canonical_name: main

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

puts("hello!");
v3 = atoi(argv[2]);
v4 = atoi(argv[1]);
v5 = my_div(v4, v3);
printf("div:%d\n", v5);
return 0;
}

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

puts("hello!");
v3 = atoi(argv[2]);
v4 = atoi(argv[1]);
v5 = func7(v4, v3);
printf("div:%d\n", v5);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 printf("hello!\n");
 printf("div:%d\n", my_div(atoi(argv[1]), atoi(argv[2])));
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
char *__fastcall get_a_day(int a1)
{
strcpy(buf_0, (&msg)[a1]);
return buf_0;
}

canonical_decompiled_code:
char *__fastcall func7(int a1)
{
strcpy(buf_0, (&msg)[a1]);
return buf_0;
}

original_code:
char *get_a_day(int idx)
{
 static char buf[20];
 strcpy(buf, msg[idx]);
 return buf;
}

variable_types: idx int

variable_types:
buf
char [20]

return_type: char *


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

v3 = get_a_day(1);
v4 = get_a_day(0);
printf("%s %s\n", v4, v3);
return 0;
}

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

v3 = func7(1);
v4 = func7(0);
printf("%s %s\n", v4, v3);
return 0;
}

original_code:
int main(void)
{
 printf("%s %s\n", get_a_day(0), get_a_day(1));
 return 0;
}

canonical_original_code:
int main()
{
 printf("%s %s\n", get_a_day(0), get_a_day(1));
 return 0;
}


repo: akashgangil/LRVM

decompiled_code:
__int64 __fastcall rvm_begin_trans(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
int v4; // ebp
__int64 v6; // r9
_QWORD *v7; // rsi
__int64 v8; // r8
_QWORD *v9; // rdi
__int64 i; // rax
unsigned int v11; // ecx
__int64 j; // rax

v4 = a3;
fprintf(stdout, "Begin transaction on %d segments\n", a3);
v6 = segment_list;
if ( v4 <= 0 )
return (unsigned int)global_tid++;
v7 = a4;
v8 = (__int64)&a4[(unsigned int)(v4 - 1) + 1];
v9 = a4;
for ( i = segment_list; !i; i = 0LL )
{
LABEL_8:
if ( ++v9 == (_QWORD *)v8 )
{
v11 = global_tid++;
do
{
for ( j = v6; j; j = *(_QWORD *)(j + 24) )
{
if ( *(_QWORD *)(*(_QWORD *)j + 16LL) == *v7 )
*(_DWORD *)(j + 8) = v11;
}
++v7;
}
while ( v7 != (_QWORD *)v8 );
return v11;
}
}
while ( *(_QWORD *)(*(_QWORD *)i + 16LL) != *v9 || *(_DWORD *)(i + 8) == -1 )
{
i = *(_QWORD *)(i + 24);
if ( !i )
goto LABEL_8;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
int v4; // ebp
__int64 v6; // r9
_QWORD *v7; // rsi
__int64 v8; // r8
_QWORD *v9; // rdi
__int64 i; // rax
unsigned int v11; // ecx
__int64 j; // rax

v4 = a3;
fprintf(stdout, "Begin transaction on %d segments\n", a3);
v6 = segment_list;
if ( v4 <= 0 )
return (unsigned int)global_tid++;
v7 = a4;
v8 = (__int64)&a4[(unsigned int)(v4 - 1) + 1];
v9 = a4;
for ( i = segment_list; !i; i = 0LL )
{
LABEL_8:
if ( ++v9 == (_QWORD *)v8 )
{
v11 = global_tid++;
do
{
for ( j = v6; j; j = *(_QWORD *)(j + 24) )
{
if ( *(_QWORD *)(*(_QWORD *)j + 16LL) == *v7 )
*(_DWORD *)(j + 8) = v11;
}
++v7;
}
while ( v7 != (_QWORD *)v8 );
return v11;
}
}
while ( *(_QWORD *)(*(_QWORD *)i + 16LL) != *v9 || *(_DWORD *)(i + 8) == -1 )
{
i = *(_QWORD *)(i + 24);
if ( !i )
goto LABEL_8;
}
return (unsigned int)-1;
}

original_code:
trans_t rvm_begin_trans(rvm_t rvm, int num_segs, void** seg_bases){
    fprintf(stdout, "Begin transaction on %d segments\n", num_segs);
    segment_list_t* seg_node = segment_list;
    int seg_it = 0;
    for(seg_it=0; seg_it < num_segs; ++seg_it){
        while(seg_node!= ((void *)0)){
            if(seg_node->segment->data == seg_bases[seg_it])
                if(seg_node->txn != -1) return (trans_t)-1;
            seg_node = seg_node -> next_seg;
        }
    }
    trans_t tid = global_tid++;
    seg_it = 0;
    for(seg_it=0; seg_it<num_segs; ++seg_it){
        seg_node = segment_list;
        while(seg_node != ((void *)0)){
            if(seg_node->segment->data == seg_bases[seg_it]) {
                seg_node->txn = tid;
            }
            seg_node = seg_node -> next_seg;
        }
    }
    return tid;
}

canonical_original_code:
int rvm_begin_trans(struct rvm_t rvm, int num_segs, void **seg_bases){
    fprintf(stdout, "Begin transaction on %d segments\n", num_segs);
    struct seg_node *seg_node = segment_list;
    int seg_it = 0;
    for(seg_it=0; seg_it < num_segs; ++seg_it){
        while(seg_node!= ((void *)0)){
            if(seg_node->segment->data == seg_bases[seg_it])
                if(seg_node->txn != -1) return (trans_t)-1;
            seg_node = seg_node -> next_seg;
        }
    }
    int tid = global_tid++;
    seg_it = 0;
    for(seg_it=0; seg_it<num_segs; ++seg_it){
        seg_node = segment_list;
        while(seg_node != ((void *)0)){
            if(seg_node->segment->data == seg_bases[seg_it]) {
                seg_node->txn = tid;
            }
            seg_node = seg_node -> next_seg;
        }
    }
    return tid;
}

variable_types: struct rvm_t { char *directory; char *logfile; }

variable_types: struct seg_node { struct segment_t { char *name; int size; void *data; } *segment; int txn; struct offset_node { int offset_val; int size; struct offset_node *next_offset; } *offset; struct seg_node *next_seg; } *

variable_types: tid int

user_defined_types: struct seg_node { struct segment_t *segment; int txn; struct offset_node *offset; struct seg_node *next_seg; }


decompiled_code:
int check_segment_list()
{
__int64 i; // rbp
int result; // eax
unsigned int *j; // rbx

for ( i = segment_list; i; i = *(_QWORD *)(i + 24) )
{
puts("A NODE!!!!");
printf("TXN Id : %d\n", *(unsigned int *)(i + 8));
printf("Name : %s\n", **(const char ***)i);
result = printf("SIZE: %d\n", *(unsigned int *)(*(_QWORD *)i + 8LL));
for ( j = *(unsigned int **)(i + 16); j; j = (unsigned int *)*((_QWORD *)j + 1) )
{
printf("Offset: %d\n", *j);
printf("SIZE: %d\n", j[1]);
result = printf("Data: %s\n", (const char *)(*(_QWORD *)(*(_QWORD *)i + 16LL) + (int)*j));
}
}
return result;
}

canonical_decompiled_code:
int func1()
{
__int64 i; // rbp
int result; // eax
unsigned int *j; // rbx

for ( i = segment_list; i; i = *(_QWORD *)(i + 24) )
{
puts("A NODE!!!!");
printf("TXN Id : %d\n", *(unsigned int *)(i + 8));
printf("Name : %s\n", **(const char ***)i);
result = printf("SIZE: %d\n", *(unsigned int *)(*(_QWORD *)i + 8LL));
for ( j = *(unsigned int **)(i + 16); j; j = (unsigned int *)*((_QWORD *)j + 1) )
{
printf("Offset: %d\n", *j);
printf("SIZE: %d\n", j[1]);
result = printf("Data: %s\n", (const char *)(*(_QWORD *)(*(_QWORD *)i + 16LL) + (int)*j));
}
}
return result;
}

original_code:
void check_segment_list(){
    segment_list_t* segment_node = segment_list;
    while(segment_node != ((void *)0)){
        printf("A NODE!!!!\n");
        printf("TXN Id : %d\n",segment_node->txn);
        printf("Name   : %s\n", segment_node->segment->name);
        printf("SIZE:    %d\n", segment_node->segment->size);
        offset_t* base_offset = segment_node->offset;
        while(base_offset != ((void *)0)) {
            printf("Offset:  %d\n", base_offset->offset_val);
            printf("SIZE: %d\n", base_offset->size);
            printf("Data:    %s\n", (char*)segment_node->segment->data + base_offset->offset_val);
            base_offset = base_offset -> next_offset;
        }
        segment_node = segment_node -> next_seg;
    }
}

canonical_original_code:
void check_segment_list(){
    struct seg_node *segment_node = segment_list;
    while(segment_node != ((void *)0)){
        printf("A NODE!!!!\n");
        printf("TXN Id : %d\n",segment_node->txn);
        printf("Name   : %s\n", segment_node->segment->name);
        printf("SIZE:    %d\n", segment_node->segment->size);
        struct offset_node *base_offset = segment_node->offset;
        while(base_offset != ((void *)0)) {
            printf("Offset:  %d\n", base_offset->offset_val);
            printf("SIZE: %d\n", base_offset->size);
            printf("Data:    %s\n", (char *)segment_node->segment->data + base_offset->offset_val);
            base_offset = base_offset -> next_offset;
        }
        segment_node = segment_node -> next_seg;
    }
}

variable_types: struct offset_node { int offset_val; int size; struct offset_node *next_offset; } *

user_defined_types: struct segment_t { char *name; int size; void *data; }


decompiled_code:
int __fastcall rvm_about_to_modify(unsigned int a1, __int64 a2, unsigned int a3, unsigned int a4)
{
__int64 v6; // rax
__int64 i; // rbx
__int64 v8; // rcx
_DWORD *v9; // rdx

LODWORD(v6) = fprintf(stdout, "About to modify segment in transaction %d with offset %d and size %d\n", a1, a3, a4);
for ( i = segment_list; i; i = *(_QWORD *)(i + 24) )
{
if ( *(_DWORD *)(i + 8) == a1 )
{
v6 = *(_QWORD *)i;
if ( *(_QWORD *)(*(_QWORD *)i + 16LL) == a2 )
{
v9 = malloc(0x10uLL);
*v9 = a3;
v9[1] = a4;
*((_QWORD *)v9 + 1) = 0LL;
v6 = *(_QWORD *)(i + 16);
if ( v6 )
{
do
{
v8 = v6;
v6 = *(_QWORD *)(v6 + 8);
}
while ( v6 );
*(_QWORD *)(v8 + 8) = v9;
}
else
{
*(_QWORD *)(i + 16) = v9;
}
}
}
}
return v6;
}

canonical_decompiled_code:
int __fastcall func2(unsigned int a1, __int64 a2, unsigned int a3, unsigned int a4)
{
__int64 v6; // rax
__int64 i; // rbx
__int64 v8; // rcx
_DWORD *v9; // rdx

LODWORD(v6) = fprintf(stdout, "About to modify segment in transaction %d with offset %d and size %d\n", a1, a3, a4);
for ( i = segment_list; i; i = *(_QWORD *)(i + 24) )
{
if ( *(_DWORD *)(i + 8) == a1 )
{
v6 = *(_QWORD *)i;
if ( *(_QWORD *)(*(_QWORD *)i + 16LL) == a2 )
{
v9 = malloc(0x10uLL);
*v9 = a3;
v9[1] = a4;
*((_QWORD *)v9 + 1) = 0LL;
v6 = *(_QWORD *)(i + 16);
if ( v6 )
{
do
{
v8 = v6;
v6 = *(_QWORD *)(v6 + 8);
}
while ( v6 );
*(_QWORD *)(v8 + 8) = v9;
}
else
{
*(_QWORD *)(i + 16) = v9;
}
}
}
}
return v6;
}

original_code:
void rvm_about_to_modify(trans_t tid, void* seg_base, int offset, int size){
    fprintf(stdout, "About to modify segment in transaction %d with offset %d and size %d\n",
            tid, offset, size);
    segment_list_t* seg_node = segment_list;
    while(seg_node != ((void *)0)){
        if(seg_node->txn == tid && seg_node->segment->data == seg_base){
            offset_t* new_offset = (offset_t*)malloc(sizeof(offset_t));
            new_offset -> offset_val = offset;
            new_offset -> size = size;
            new_offset -> next_offset = ((void *)0);
            offset_t* seg_offset = seg_node->offset;
            if(seg_offset == ((void *)0)){
                seg_node->offset = new_offset;
            }
            else{
                while(seg_offset -> next_offset != ((void *)0))
                    seg_offset = seg_offset->next_offset;
                seg_offset->next_offset = new_offset;
            }
        }
        seg_node = seg_node -> next_seg;
    }
}

canonical_original_code:
void rvm_about_to_modify(int tid, void *seg_base, int offset, int size){
    fprintf(stdout, "About to modify segment in transaction %d with offset %d and size %d\n",
            tid, offset, size);
    struct seg_node *seg_node = segment_list;
    while(seg_node != ((void *)0)){
        if(seg_node->txn == tid && seg_node->segment->data == seg_base){
            struct offset_node *new_offset = (offset_t*)malloc(sizeof(offset_t));
            new_offset -> offset_val = offset;
            new_offset -> size = size;
            new_offset -> next_offset = ((void *)0);
            struct offset_node *seg_offset = seg_node->offset;
            if(seg_offset == ((void *)0)){
                seg_node->offset = new_offset;
            }
            else{
                while(seg_offset -> next_offset != ((void *)0))
                    seg_offset = seg_offset->next_offset;
                seg_offset->next_offset = new_offset;
            }
        }
        seg_node = seg_node -> next_seg;
    }
}

variable_types: offset int

variable_types: size int

user_defined_types: struct offset_node { int offset_val; int size; struct offset_node *next_offset; }


decompiled_code:
void __fastcall rvm_unmap(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rcx
_QWORD *v5; // rdi
_QWORD *v6; // rdx

fwrite("Unmap the segment\n", 1uLL, 0x12uLL, stdout);
v4 = segment_list;
if ( segment_list )
{
while ( 1 )
{
v5 = (_QWORD *)v4;
v6 = 0LL;
if ( v4 )
break;
LABEL_7:
v4 = *(_QWORD *)(v4 + 24);
if ( !v4 )
return;
}
while ( *(_QWORD *)(*v5 + 16LL) != a3 )
{
v6 = v5;
if ( !v5[3] )
goto LABEL_7;
v5 = (_QWORD *)v5[3];
}
if ( v6 )
v6[3] = v5[3];
else
segment_list = v5[3];
free(v5);
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rcx
_QWORD *v5; // rdi
_QWORD *v6; // rdx

fwrite("Unmap the segment\n", 1uLL, 0x12uLL, stdout);
v4 = segment_list;
if ( segment_list )
{
while ( 1 )
{
v5 = (_QWORD *)v4;
v6 = 0LL;
if ( v4 )
break;
LABEL_7:
v4 = *(_QWORD *)(v4 + 24);
if ( !v4 )
return;
}
while ( *(_QWORD *)(*v5 + 16LL) != a3 )
{
v6 = v5;
if ( !v5[3] )
goto LABEL_7;
v5 = (_QWORD *)v5[3];
}
if ( v6 )
v6[3] = v5[3];
else
segment_list = v5[3];
free(v5);
}
}

original_code:
void rvm_unmap(rvm_t rvm, void* seg_base){
    fprintf(stdout, "Unmap the segment\n");
    segment_list_t* seg_node = segment_list;
    while(seg_node != ((void *)0)){
        segment_list_t *currP, *prevP;
        prevP = ((void *)0);
        for (currP = seg_node; currP != ((void *)0); prevP = currP, currP = currP->next_seg) {
            if (currP->segment->data == seg_base) {
                if (prevP == ((void *)0)) {
                     segment_list = currP->next_seg;
                } else {
                     prevP->next_seg = currP->next_seg;
                }
                free(currP);
                return;
            }
        }
        seg_node = seg_node -> next_seg;
    }
}

canonical_original_code:
void rvm_unmap(struct rvm_t rvm, void *seg_base){
    fprintf(stdout, "Unmap the segment\n");
    struct seg_node *seg_node = segment_list;
    while(seg_node != ((void *)0)){
        struct seg_node *currP; struct seg_node *prevP;
        prevP = ((void *)0);
        for (currP = seg_node; currP != ((void *)0); prevP = currP, currP = currP->next_seg) {
            if (currP->segment->data == seg_base) {
                if (prevP == ((void *)0)) {
                     segment_list = currP->next_seg;
                } else {
                     prevP->next_seg = currP->next_seg;
                }
                free(currP);
                return;
            }
        }
        seg_node = seg_node -> next_seg;
    }
}

user_defined_types: struct rvm_t { char *directory; char *logfile; }


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

return stat(a1, &v2) == 0;
}

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

return stat(a1, &v2) == 0;
}

original_code:
int file_exist (char *filename)
{
    struct stat buffer;
    return (stat(filename, &buffer) == 0);
}

canonical_original_code:
int file_exist(char *filename)
{
    struct stat buffer;
    return (stat(filename, &buffer) == 0);
}

variable_types:
filename
char *

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

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


decompiled_code:
const char *__fastcall rvm_init(char *src)
{
const char **v1; // r12
size_t v2; // rax
char *v3; // r13
size_t v4; // rax
char *v5; // rbx

fprintf(stdout, "Initialize the rvm library with the directory %s\n", src);
segment_list = 0LL;
v1 = (const char **)malloc(0x10uLL);
rvm = (__int64)v1;
v2 = strlen(src);
v3 = (char *)malloc(v2 + 1);
*v1 = v3;
strcpy(v3, src);
v4 = strlen(src);
v5 = (char *)malloc(v4 + 5);
v1[1] = v5;
strcpy(v5, src);
strcat(v5, ".log");
if ( file_exist(v3) || !mkdir(*v1, 0x1FDu) )
return *v1;
fwrite("Failed to create RVM directory\n", 1uLL, 0x1FuLL, stderr);
return *(const char **)rvm;
}

canonical_decompiled_code:
const char *__fastcall func5(char *src)
{
const char **v1; // r12
size_t v2; // rax
char *v3; // r13
size_t v4; // rax
char *v5; // rbx

fprintf(stdout, "Initialize the rvm library with the directory %s\n", src);
segment_list = 0LL;
v1 = (const char **)malloc(0x10uLL);
rvm = (__int64)v1;
v2 = strlen(src);
v3 = (char *)malloc(v2 + 1);
*v1 = v3;
strcpy(v3, src);
v4 = strlen(src);
v5 = (char *)malloc(v4 + 5);
v1[1] = v5;
strcpy(v5, src);
strcat(v5, ".log");
if ( func4(v3) || !mkdir(*v1, 0x1FDu) )
return *v1;
fwrite("Failed to create RVM directory\n", 1uLL, 0x1FuLL, stderr);
return *(const char **)rvm;
}

original_code:
rvm_t rvm_init(const char* directory) {
    fprintf(stdout, "Initialize the rvm library with the directory %s\n", directory);
    segment_list = (segment_list_t*) malloc( sizeof(segment_list_t*) );
    segment_list = ((void *)0);
    rvm = (rvm_t*) malloc( sizeof(rvm_t) );
    rvm->directory = (char*) malloc( strlen(directory) + 1 );
    strcpy(rvm -> directory, directory);
    rvm->logfile = (char*) malloc(strlen(directory) + strlen(log_file_ext) + 1);
    strcpy(rvm->logfile, directory);
    strcat(rvm->logfile, log_file_ext);
    if(!file_exist(rvm->directory)){
        if(mkdir(rvm->directory, (0400|0200|0100) | ((0400|0200|0100) >> 3) | ((0400 >> 3) >> 3) | ((0100 >> 3) >> 3)) != 0){
            fprintf(stderr,"Failed to create RVM directory\n");
            return *rvm;
        }
    }
    return *rvm;
}

canonical_original_code:
struct rvm_t rvm_init(char *directory) {
    fprintf(stdout, "Initialize the rvm library with the directory %s\n", directory);
    segment_list = (struct seg_node *) malloc( sizeof(struct seg_node *) );
    segment_list = ((void *)0);
    rvm = (struct rvm_t *) malloc( sizeof(rvm_t) );
    rvm->directory = (char *) malloc( strlen(directory) + 1 );
    strcpy(rvm -> directory, directory);
    rvm->logfile = (char *) malloc(strlen(directory) + strlen(log_file_ext) + 1);
    strcpy(rvm->logfile, directory);
    strcat(rvm->logfile, log_file_ext);
    if(!file_exist(rvm->directory)){
        if(mkdir(rvm->directory, (0400|0200|0100) | ((0400|0200|0100) >> 3) | ((0400 >> 3) >> 3) | ((0100 >> 3) >> 3)) != 0){
            fprintf(stderr,"Failed to create RVM directory\n");
            return *rvm;
        }
    }
    return *rvm;
}

variable_types:
directory
char *

return_type: struct rvm_t { char *directory; char *logfile; }


decompiled_code:
char *__fastcall get_seg_file_path(char *src)
{
const char *v1; // r12
size_t v2; // rbx
size_t v3; // rax
char *v4; // rbx

v1 = *(const char **)rvm;
v2 = strlen(*(const char **)rvm);
v3 = strlen(src);
v4 = (char *)malloc(v2 + v3 + 5);
strcpy(v4, v1);
*(_WORD *)&v4[strlen(v4)] = 47;
strcat(v4, src);
strcat(v4, ".seg");
return v4;
}

canonical_decompiled_code:
char *__fastcall func6(char *src)
{
const char *v1; // r12
size_t v2; // rbx
size_t v3; // rax
char *v4; // rbx

v1 = *(const char **)rvm;
v2 = strlen(*(const char **)rvm);
v3 = strlen(src);
v4 = (char *)malloc(v2 + v3 + 5);
strcpy(v4, v1);
*(_WORD *)&v4[strlen(v4)] = 47;
strcat(v4, src);
strcat(v4, ".seg");
return v4;
}

original_code:
char* get_seg_file_path(const char* seg_name){
    char *seg_path = (char*) malloc( strlen(rvm->directory)
            + strlen(seg_name) + strlen(seg_file_ext)+1);
    strcpy(seg_path, rvm->directory);
    strcat(seg_path, "/");
    strcat(seg_path, seg_name);
    strcat(seg_path, seg_file_ext);
    return seg_path;
}

canonical_original_code:
char *get_seg_file_path(char *seg_name){
    char *seg_path = (char*) malloc( strlen(rvm->directory)
            + strlen(seg_name) + strlen(seg_file_ext)+1);
    strcpy(seg_path, rvm->directory);
    strcat(seg_path, "/");
    strcat(seg_path, seg_name);
    strcat(seg_path, seg_file_ext);
    return seg_path;
}


decompiled_code:
void __fastcall rvm_destroy(__int64 a1, __int64 a2, char *a3)
{
__int64 v4; // rbx
char *v5; // rbx

fprintf(stdout, "Destroy the segment %s\n", a3);
v4 = segment_list;
if ( segment_list )
{
while ( !strcmp(a3, **(const char ***)v4) )
{
v4 = *(_QWORD *)(v4 + 24);
if ( !v4 )
goto LABEL_4;
}
fwrite("Segment is currently mapped. Failed to delete", 1uLL, 0x2DuLL, stderr);
}
else
{
LABEL_4:
v5 = get_seg_file_path(a3);
if ( file_exist(v5) )
{
if ( rmdir(v5) )
fprintf(stderr, "Error deleting segment: %s\n", v5);
}
free(v5);
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, __int64 a2, char *a3)
{
__int64 v4; // rbx
char *v5; // rbx

fprintf(stdout, "Destroy the segment %s\n", a3);
v4 = segment_list;
if ( segment_list )
{
while ( !strcmp(a3, **(const char ***)v4) )
{
v4 = *(_QWORD *)(v4 + 24);
if ( !v4 )
goto LABEL_4;
}
fwrite("Segment is currently mapped. Failed to delete", 1uLL, 0x2DuLL, stderr);
}
else
{
LABEL_4:
v5 = func6(a3);
if ( func4(v5) )
{
if ( rmdir(v5) )
fprintf(stderr, "Error deleting segment: %s\n", v5);
}
free(v5);
}
}

original_code:
void rvm_destroy(rvm_t rvm, const char* seg_name){
    fprintf(stdout, "Destroy the segment %s\n", seg_name);
    segment_list_t* seg_node = segment_list;
    while(seg_node != ((void *)0)){
        if(strcmp(seg_name, seg_node->segment->name)){
            fprintf(stderr, "Segment is currently mapped. Failed to delete");
            return;
        }
        seg_node = seg_node->next_seg;
    }
    char* seg_path = get_seg_file_path(seg_name);
    if(file_exist(seg_path)){
        if(rmdir(seg_path)){
            fprintf(stderr, "Error deleting segment: %s\n", seg_path);
        }
    }
    free(seg_path);
}

canonical_original_code:
void rvm_destroy(struct rvm_t rvm, char *seg_name){
    fprintf(stdout, "Destroy the segment %s\n", seg_name);
    struct seg_node *seg_node = segment_list;
    while(seg_node != ((void *)0)){
        if(strcmp(seg_name, seg_node->segment->name)){
            fprintf(stderr, "Segment is currently mapped. Failed to delete");
            return;
        }
        seg_node = seg_node->next_seg;
    }
    char *seg_path = get_seg_file_path(seg_name);
    if(file_exist(seg_path)){
        if(rmdir(seg_path)){
            fprintf(stderr, "Error deleting segment: %s\n", seg_path);
        }
    }
    free(seg_path);
}


decompiled_code:
__int64 __fastcall rvm_map(__int64 a1, __int64 a2, char *a3, int a4)
{
_QWORD *v6; // rbx
char *v7; // rax
_QWORD *v8; // rdx
__int64 v9; // rax
__int64 v10; // rcx

fprintf(stdout, "Map the segment %s\n", a3);
v6 = calloc(1uLL, 0x18uLL);
v7 = (char *)malloc(8uLL);
*v6 = v7;
strcpy(v7, a3);
v6[2] = malloc(a4);
*((_DWORD *)v6 + 2) = a4;
v8 = malloc(0x20uLL);
*v8 = v6;
v8[3] = 0LL;
*((_DWORD *)v8 + 2) = -1;
v8[2] = 0LL;
v9 = segment_list;
if ( segment_list )
{
do
{
v10 = v9;
v9 = *(_QWORD *)(v9 + 24);
}
while ( v9 );
*(_QWORD *)(v10 + 24) = v8;
}
else
{
segment_list = (__int64)v8;
}
restore_seg_from_log(a3, (__int64)v6);
return v6[2];
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, char *a3, int a4)
{
_QWORD *v6; // rbx
char *v7; // rax
_QWORD *v8; // rdx
__int64 v9; // rax
__int64 v10; // rcx

fprintf(stdout, "Map the segment %s\n", a3);
v6 = calloc(1uLL, 0x18uLL);
v7 = (char *)malloc(8uLL);
*v6 = v7;
strcpy(v7, a3);
v6[2] = malloc(a4);
*((_DWORD *)v6 + 2) = a4;
v8 = malloc(0x20uLL);
*v8 = v6;
v8[3] = 0LL;
*((_DWORD *)v8 + 2) = -1;
v8[2] = 0LL;
v9 = segment_list;
if ( segment_list )
{
do
{
v10 = v9;
v9 = *(_QWORD *)(v9 + 24);
}
while ( v9 );
*(_QWORD *)(v10 + 24) = v8;
}
else
{
segment_list = (__int64)v8;
}
func10(a3, (__int64)v6);
return v6[2];
}

original_code:
void* rvm_map(rvm_t rvm, const char* seg_name, int size_to_create){
    fprintf(stdout, "Map the segment %s\n", seg_name);
    segment_t* seg = (segment_t*) calloc(1, sizeof(segment_t));
    seg->name = (char*) malloc(sizeof( strlen(seg_name) + 1));
    strcpy(seg->name, seg_name);
    seg->data = (void*)malloc(sizeof(char) * size_to_create);
    seg->size = size_to_create;
    segment_list_t* new_seg = (segment_list_t*)malloc(sizeof(segment_list_t));
    new_seg->segment = seg;
    new_seg->next_seg = ((void *)0);
    new_seg->txn = -1;
    new_seg->offset = ((void *)0);
    segment_list_t* seg_node = segment_list;
    if(seg_node == ((void *)0))
        segment_list = new_seg;
    else{
        while(seg_node -> next_seg != ((void *)0)){
            seg_node = seg_node->next_seg ;
        }
        seg_node -> next_seg = new_seg;
    }
    restore_seg_from_log(seg_name, seg);
    return seg->data;
}

canonical_original_code:
void *rvm_map(struct rvm_t rvm, char *seg_name, int size_to_create){
    fprintf(stdout, "Map the segment %s\n", seg_name);
    struct segment_t *seg = (segment_t*) calloc(1, sizeof(segment_t));
    seg->name = (char *) malloc(sizeof( strlen(seg_name) + 1));
    strcpy(seg->name, seg_name);
    seg->data = (void *)malloc(sizeof(char) * size_to_create);
    seg->size = size_to_create;
    struct seg_node *new_seg = (segment_list_t*)malloc(sizeof(segment_list_t));
    new_seg->segment = seg;
    new_seg->next_seg = ((void *)0);
    new_seg->txn = -1;
    new_seg->offset = ((void *)0);
    struct seg_node *seg_node = segment_list;
    if(seg_node == ((void *)0))
        segment_list = new_seg;
    else{
        while(seg_node -> next_seg != ((void *)0)){
            seg_node = seg_node->next_seg ;
        }
        seg_node -> next_seg = new_seg;
    }
    restore_seg_from_log(seg_name, seg);
    return seg->data;
}

variable_types: struct segment_t { char *name; int size; void *data; } *

return_type: void *


decompiled_code:
int __fastcall rvm_abort_trans(unsigned int a1)
{
int result; // eax
__int64 i; // rbx

result = fprintf(stdout, "Abort transaction %d\n", a1);
for ( i = segment_list; i; i = *(_QWORD *)(i + 24) )
{
if ( *(_DWORD *)(i + 8) == a1 )
result = restore_seg_from_log(**(char ***)i, *(_QWORD *)i);
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(unsigned int a1)
{
int result; // eax
__int64 i; // rbx

result = fprintf(stdout, "Abort transaction %d\n", a1);
for ( i = segment_list; i; i = *(_QWORD *)(i + 24) )
{
if ( *(_DWORD *)(i + 8) == a1 )
result = func10(**(char ***)i, *(_QWORD *)i);
}
return result;
}

original_code:
void rvm_abort_trans(trans_t tid) {
    fprintf(stdout, "Abort transaction %d\n", tid);
    segment_list_t* seg_node = segment_list;
    while(seg_node !=((void *)0)) {
        if (seg_node->txn == tid) {
            restore_seg_from_log(seg_node->segment->name, seg_node->segment);
        }
        seg_node = seg_node -> next_seg;
    }
}

canonical_original_code:
void rvm_abort_trans(int tid) {
    fprintf(stdout, "Abort transaction %d\n", tid);
    struct seg_node *seg_node = segment_list;
    while(seg_node !=((void *)0)) {
        if (seg_node->txn == tid) {
            restore_seg_from_log(seg_node->segment->name, seg_node->segment);
        }
        seg_node = seg_node -> next_seg;
    }
}


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

fprintf(stdout, "Removing segment from transaction %d\n", a1);
for ( result = segment_list; result; result = *(_QWORD *)(result + 24) )
{
if ( *(_DWORD *)(result + 8) == a1 )
*(_DWORD *)(result + 8) = -1;
}
return result;
}

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

fprintf(stdout, "Removing segment from transaction %d\n", a1);
for ( result = segment_list; result; result = *(_QWORD *)(result + 24) )
{
if ( *(_DWORD *)(result + 8) == a1 )
*(_DWORD *)(result + 8) = -1;
}
return result;
}

original_code:
void remove_seg_from_transaction(trans_t tid){
    fprintf(stdout, "Removing segment from transaction %d\n", tid);
    segment_list_t* seg_node = segment_list;
    while(seg_node != ((void *)0)){
        if(seg_node->txn == tid){
            seg_node->txn = -1;
        }
        seg_node = seg_node -> next_seg;
    }
}

canonical_original_code:
void remove_seg_from_transaction(int tid){
    fprintf(stdout, "Removing segment from transaction %d\n", tid);
    struct seg_node *seg_node = segment_list;
    while(seg_node != ((void *)0)){
        if(seg_node->txn == tid){
            seg_node->txn = -1;
        }
        seg_node = seg_node -> next_seg;
    }
}


elapsed

decompiled_code:
long double __fastcall elapsed(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
return (long double)(a4 - a2) * 0.000001 + (long double)(a3 - a1);
}

canonical_decompiled_code:
long double __fastcall func0(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
return (long double)(a4 - a2) * 0.000001 + (long double)(a3 - a1);
}

original_code:
static
long double
elapsed (struct timeval start, struct timeval stop)
{
  return (long double)(stop.tv_sec - start.tv_sec)
    + (long double)(stop.tv_usec - start.tv_usec)*1e-6;
}

canonical_original_code:
static long double elapsed(struct timeval start, struct timeval stop)
{
  return (long double)(stop.tv_sec - start.tv_sec)
    + (long double)(stop.tv_usec - start.tv_usec)*1e-6;
}

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

variable_types:
stop
struct timeval

return_type: long double


decompiled_code:
long double __fastcall stopwatch_elapsed(__int64 a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

if ( !a1 )
return 0.0;
if ( !*(_DWORD *)(a1 + 32) )
return elapsed(*(_QWORD *)a1, *(_QWORD *)(a1 + 8), *(_QWORD *)(a1 + 16), *(_QWORD *)(a1 + 24));
gettimeofday(&v2, 0LL);
return elapsed(*(_QWORD *)a1, *(_QWORD *)(a1 + 8), v2.tv_sec, v2.tv_usec);
}

canonical_decompiled_code:
long double __fastcall func1(__int64 a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

if ( !a1 )
return 0.0;
if ( !*(_DWORD *)(a1 + 32) )
return func0(*(_QWORD *)a1, *(_QWORD *)(a1 + 8), *(_QWORD *)(a1 + 16), *(_QWORD *)(a1 + 24));
gettimeofday(&v2, 0LL);
return func0(*(_QWORD *)a1, *(_QWORD *)(a1 + 8), v2.tv_sec, v2.tv_usec);
}

original_code:
long double
stopwatch_elapsed (struct stopwatch_t* T)
{
  long double dt = 0;
  if (T) {
    if (T->is_running_) {
      struct timeval stop;
      gettimeofday (&stop, 0);
      dt = elapsed (T->t_start_, stop);
    } else {
      dt = elapsed (T->t_start_, T->t_stop_);
    }
  }
  return dt;
}

canonical_original_code:
long double stopwatch_elapsed(struct stopwatch_t *T)
{
  long double dt = 0;
  if (T) {
    if (T->is_running_) {
      struct timeval stop;
      gettimeofday (&stop, 0);
      dt = elapsed (T->t_start_, stop);
    } else {
      dt = elapsed (T->t_start_, T->t_stop_);
    }
  }
  return dt;
}

variable_types: struct stopwatch_t { struct timeval { long tv_sec; long tv_usec; } t_start_; struct timeval t_stop_; int is_running_; } *

variable_types:
dt
long double

user_defined_types: struct stopwatch_t { struct timeval t_start_; struct timeval t_stop_; int is_running_; }


decompiled_code:
int stopwatch_init()
{
return fflush(stderr);
}

canonical_decompiled_code:
int func2()
{
return fflush(stderr);
}

original_code:
void
stopwatch_init (void)
{
  fflush (stderr);
}

canonical_original_code:
void stopwatch_init()
{
  fflush (stderr);
}


decompiled_code:
int __fastcall stopwatch_start(__int64 a1)
{
if ( !a1 )
_assert_fail("T", "timer.c", 0x3Eu, "stopwatch_start");
*(_DWORD *)(a1 + 32) = 1;
return gettimeofday((struct timeval *)a1, 0LL);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
if ( !a1 )
_assert_fail("T", "timer.c", 0x3Eu, "stopwatch_start");
*(_DWORD *)(a1 + 32) = 1;
return gettimeofday((struct timeval *)a1, 0LL);
}

original_code:
void
stopwatch_start (struct stopwatch_t* T)
{
  ((void) sizeof ((T) ? 1 : 0), __extension__ ({ if (T) ; else __assert_fail ("T", "timer.c", 62, __extension__ __PRETTY_FUNCTION__); }));
  T->is_running_ = 1;
  gettimeofday (&(T->t_start_), 0);
}

canonical_original_code:
void stopwatch_start(struct stopwatch_t *T)
{
  ((void) sizeof ((T) ? 1 : 0), __extension__ ({ if (T) ; else __assert_fail ("T", "timer.c", 62, __extension__ __PRETTY_FUNCTION__); }));
  T->is_running_ = 1;
  gettimeofday (&(T->t_start_), 0);
}

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


decompiled_code:
long double __fastcall stopwatch_stop(__int64 a1)
{
if ( !a1 )
return 0.0;
if ( *(_DWORD *)(a1 + 32) )
{
gettimeofday((struct timeval *)(a1 + 16), 0LL);
*(_DWORD *)(a1 + 32) = 0;
}
return stopwatch_elapsed(a1);
}

canonical_decompiled_code:
long double __fastcall func4(__int64 a1)
{
if ( !a1 )
return 0.0;
if ( *(_DWORD *)(a1 + 32) )
{
gettimeofday((struct timeval *)(a1 + 16), 0LL);
*(_DWORD *)(a1 + 32) = 0;
}
return func1(a1);
}

original_code:
long double
stopwatch_stop (struct stopwatch_t* T)
{
  long double dt = 0;
  if (T) {
    if (T->is_running_) {
      gettimeofday (&(T->t_stop_), 0);
      T->is_running_ = 0;
    }
    dt = stopwatch_elapsed (T);
  }
  return dt;
}

canonical_original_code:
long double stopwatch_stop(struct stopwatch_t *T)
{
  long double dt = 0;
  if (T) {
    if (T->is_running_) {
      gettimeofday (&(T->t_stop_), 0);
      T->is_running_ = 0;
    }
    dt = stopwatch_elapsed (T);
  }
  return dt;
}


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

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

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

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

original_code:
struct stopwatch_t *
stopwatch_create (void)
{
  struct stopwatch_t* new_timer =
    (struct stopwatch_t *)malloc (sizeof (struct stopwatch_t));
  if (new_timer)
    memset (new_timer, 0, sizeof (struct stopwatch_t));
  return new_timer;
}

canonical_original_code:
struct stopwatch_t *stopwatch_create()
{
  struct stopwatch_t *new_timer = (struct stopwatch_t *)malloc (sizeof (struct stopwatch_t));
  if (new_timer)
    memset (new_timer, 0, sizeof (struct stopwatch_t));
  return new_timer;
}

return_type: struct stopwatch_t { struct timeval { long tv_sec; long tv_usec; } t_start_; struct timeval t_stop_; int is_running_; } *


decompiled_code:
void __fastcall stopwatch_destroy(void *ptr)
{
if ( ptr )
{
stopwatch_stop((__int64)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
if ( ptr )
{
func4((__int64)ptr);
free(ptr);
}
}

original_code:
void
stopwatch_destroy (struct stopwatch_t* T)
{
  if (T) {
    stopwatch_stop (T);
    free (T);
  }
}

canonical_original_code:
void stopwatch_destroy(struct stopwatch_t *T)
{
  if (T) {
    stopwatch_stop (T);
    free (T);
  }
}


decompiled_code:
__int64 __fastcall thread1Start(void *arg)
{
void (*v1)(void); // rbx

v1 = *(void (**)(void))arg;
free(arg);
v1();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(void *arg)
{
void (*v1)(void); // rbx

v1 = *(void (**)(void))arg;
free(arg);
v1();
return 0LL;
}

original_code:
static int thread1Start( void* arg )
{
 struct thread1Arguments* arguments = (struct thread1Arguments*) arg;
 void (*function)() = arguments->function;
 free( arguments );
 arguments = ((void *)0);
 ;
 function();
 return 0;
}

canonical_original_code:
static int thread1Start(void *arg)
{
 struct thread1Arguments *arguments = (struct thread1Arguments*) arg;
 void (*function)() = arguments->function;
 free( arguments );
 arguments = ((void *)0);
 ;
 function();
 return 0;
}

variable_types:
arg
void *

variable_types: struct thread1Arguments { void (*function)(); } *

variable_types:
function
void (*)()


decompiled_code:
__pid_t init_threads()
{
__int128 *v0; // rax
__pid_t result; // eax

v0 = &thread1List;
do
{
*(_DWORD *)v0 = 0;
*((_QWORD *)v0++ + 1) = 0LL;
}
while ( v0 != (__int128 *)&end );
result = getpid();
parentPid = result;
return result;
}

canonical_decompiled_code:
__pid_t func8()
{
__int128 *v0; // rax
__pid_t result; // eax

v0 = &thread1List;
do
{
*(_DWORD *)v0 = 0;
*((_QWORD *)v0++ + 1) = 0LL;
}
while ( v0 != (__int128 *)&end );
result = getpid();
parentPid = result;
return result;
}

original_code:
void init_threads()
{
 int i;
 for ( i = 0; i < 10; i++ )
 {
  thread1List[i].pid = 0;
  thread1List[i].stack = 0;
 }
 parentPid = getpid();
}

variable_types: i int


decompiled_code:
int thread1_Yield()
{
return sched_yield();
}

canonical_decompiled_code:
int func9()
{
return sched_yield();
}

original_code:
void thread1_Yield()
{
 int status;
 status=sched_yield();
}

variable_types: status int


decompiled_code:
__int64 __fastcall spawn_thread1(__int64 a1)
{
int v1; // ebx
__int64 result; // rax
char *v3; // rbp
_QWORD *v4; // rax
void *v5; // r12

v1 = numthreads;
result = 1LL;
if ( numthreads != 10 )
{
v3 = (char *)malloc(0x100000uLL);
*((_QWORD *)&thread1List[v1] + 1) = v3;
if ( v3 )
{
v4 = malloc(8uLL);
v5 = v4;
if ( v4 )
{
*v4 = a1;
LODWORD(thread1List[v1]) = clone((int (*)(void *))thread1Start, v3 + 0x100000, 3857, v4);
if ( LODWORD(thread1List[numthreads]) == -1 )
{
free(*((void **)&thread1List[numthreads] + 1));
free(v5);
result = 3LL;
}
else
{
++numthreads;
result = 0LL;
}
}
else
{
free(v3);
result = 2LL;
}
}
else
{
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
int v1; // ebx
__int64 result; // rax
char *v3; // rbp
_QWORD *v4; // rax
void *v5; // r12

v1 = numthreads;
result = 1LL;
if ( numthreads != 10 )
{
v3 = (char *)malloc(0x100000uLL);
*((_QWORD *)&thread1List[v1] + 1) = v3;
if ( v3 )
{
v4 = malloc(8uLL);
v5 = v4;
if ( v4 )
{
*v4 = a1;
LODWORD(thread1List[v1]) = clone((int (*)(void *))thread1Start, v3 + 0x100000, 3857, v4);
if ( LODWORD(thread1List[numthreads]) == -1 )
{
free(*((void **)&thread1List[numthreads] + 1));
free(v5);
result = 3LL;
}
else
{
++numthreads;
result = 0LL;
}
}
else
{
free(v3);
result = 2LL;
}
}
else
{
result = 2LL;
}
}
return result;
}

original_code:
int spawn_thread1( void (*func)(void) )
{
 struct thread1Arguments* arguments = ((void *)0);
 if ( numthreads == 10 ) return 1;
 thread1List[numthreads].stack = malloc( (1024*1024) );
 if ( thread1List[numthreads].stack == 0 )
 {
  ;
  return 2;
 }
 arguments = (struct thread1Arguments*) malloc( sizeof(*arguments) );
 if ( arguments == 0 ) {
  free( thread1List[numthreads].stack );
  ;
  return 2;
 }
 arguments->function = func;
 thread1List[numthreads].pid = clone( &thread1Start, (char*) thread1List[numthreads].stack + (1024*1024),
  17 | 0x00000200 | 0x00000400 | 0x00000800 | 0x00000100, arguments );
 if ( thread1List[numthreads].pid == -1 )
 {
  free( thread1List[numthreads].stack );
  free( arguments );
  ;
  return 3;
 }
 numthreads ++;
 return 0;
}

canonical_original_code:
int spawn_thread1(void (*func)())
{
 struct thread1Arguments *arguments = ((void *)0);
 if ( numthreads == 10 ) return 1;
 thread1List[numthreads].stack = malloc( (1024*1024) );
 if ( thread1List[numthreads].stack == 0 )
 {
  ;
  return 2;
 }
 arguments = (struct thread1Arguments *) malloc( sizeof(*arguments) );
 if ( arguments == 0 ) {
  free( thread1List[numthreads].stack );
  ;
  return 2;
 }
 arguments->function = func;
 thread1List[numthreads].pid = clone( &thread1Start, (char *) thread1List[numthreads].stack + (1024*1024),
  17 | 0x00000200 | 0x00000400 | 0x00000800 | 0x00000100, arguments );
 if ( thread1List[numthreads].pid == -1 )
 {
  free( thread1List[numthreads].stack );
  free( arguments );
  ;
  return 3;
 }
 numthreads ++;
 return 0;
}

variable_types:
func
void (*)()

user_defined_types: struct thread1Arguments { void (*function)(); }


waitForAllthreads

decompiled_code:
__int64 waitForAllthreads()
{
__pid_t v1; // ecx
int v2; // edi
__int64 v3; // rax
int v4; // ebx

if ( parentPid != getpid() )
return 4LL;
while ( numthreads > 0 )
{
LABEL_8:
v1 = wait(0LL);
if ( v1 == -1 )
exit(1);
v2 = numthreads;
if ( numthreads <= 0 )
return 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( LODWORD(thread1List[v3]) == v1 )
break;
if ( ++v3 == numthreads )
goto LABEL_8;
}
--numthreads;
free(*((void **)&thread1List[(int)v3] + 1));
if ( v2 - 1 != v4 )
thread1List[v4] = (__int128)_mm_load_si128((const __m128i *)&thread1List[v2 - 1]);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func11()
{
__pid_t v1; // ecx
int v2; // edi
__int64 v3; // rax
int v4; // ebx

if ( parentPid != getpid() )
return 4LL;
while ( numthreads > 0 )
{
LABEL_8:
v1 = wait(0LL);
if ( v1 == -1 )
exit(1);
v2 = numthreads;
if ( numthreads <= 0 )
return 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( LODWORD(thread1List[v3]) == v1 )
break;
if ( ++v3 == numthreads )
goto LABEL_8;
}
--numthreads;
free(*((void **)&thread1List[(int)v3] + 1));
if ( v2 - 1 != v4 )
thread1List[v4] = (__int128)_mm_load_si128((const __m128i *)&thread1List[v2 - 1]);
}
return 0LL;
}

original_code:
int waitForAllthreads()
{
 pid_t pid;
 int i;
 int threadsRemaining = 0;
 pid = getpid();
 if ( pid != parentPid ) return 4;
 while ( numthreads > threadsRemaining )
 {
  pid = wait( 0 );
  if ( pid == -1 )
  {
   ;
   exit( 1 );
  }
  for ( i = 0; i < numthreads; ++ i )
  {
   if ( thread1List[i].pid == pid )
   {
    ;
    numthreads --;
    free( thread1List[i].stack );
    if ( i != numthreads )
    {
     thread1List[i] = thread1List[numthreads];
    }
    i = -1;
    break;
   }
  }
  if ( i == numthreads )
  {
   ;
  }
 }
 return 0;
}

canonical_original_code:
int waitForAllthreads()
{
 int pid;
 int i;
 int threadsRemaining = 0;
 pid = getpid();
 if ( pid != parentPid ) return 4;
 while ( numthreads > threadsRemaining )
 {
  pid = wait( 0 );
  if ( pid == -1 )
  {
   ;
   exit( 1 );
  }
  for ( i = 0; i < numthreads; ++ i )
  {
   if ( thread1List[i].pid == pid )
   {
    ;
    numthreads --;
    free( thread1List[i].stack );
    if ( i != numthreads )
    {
     thread1List[i] = thread1List[numthreads];
    }
    i = -1;
    break;
   }
  }
  if ( i == numthreads )
  {
   ;
  }
 }
 return 0;
}

variable_types: pid int

variable_types: threadsRemaining int


EvenOdd

decompiled_code:
int EvenOdd()
{
unsigned int i; // ebx
int result; // eax

puts("I'm thread A:");
for ( i = 1; i != 11; ++i )
{
if ( (i & 1) != 0 )
printf("%d is odd\n", i);
else
printf("%d is even\n", i);
result = thread1_Yield();
}
return result;
}

canonical_decompiled_code:
int func12()
{
unsigned int i; // ebx
int result; // eax

puts("I'm thread A:");
for ( i = 1; i != 11; ++i )
{
if ( (i & 1) != 0 )
printf("%d is odd\n", i);
else
printf("%d is even\n", i);
result = func9();
}
return result;
}

original_code:
void EvenOdd()
{
 int i;
 printf( "I'm thread A:\n");
 for ( i = 1; i <= 10; i++ )
 {
  if (i%2==0)
  printf("%d is even\n", i );
  else
  printf("%d is odd\n", i);
  thread1_Yield();
 }
 return;
}


squares

decompiled_code:
int squares()
{
unsigned int i; // ebx
int result; // eax

puts("I'm thread C: ");
for ( i = 0; i != 10; ++i )
{
printf("%d*%d = %d\n", i, i, i * i);
result = thread1_Yield();
}
return result;
}

canonical_decompiled_code:
int func13()
{
unsigned int i; // ebx
int result; // eax

puts("I'm thread C: ");
for ( i = 0; i != 10; ++i )
{
printf("%d*%d = %d\n", i, i, i * i);
result = func9();
}
return result;
}

original_code:
void squares()
{
 int i;
 printf("I'm thread C: \n");
 for ( i = 0; i < 10; ++ i )
 {
  printf( "%d*%d = %d\n", i, i, i*i );
  thread1_Yield();
 }
}


fib

decompiled_code:
__int64 fib()
{
int v0; // ebp
unsigned int v1; // ebx
__int64 result; // rax
unsigned int v3; // er12

puts("I'm thread B: ");
printf("fibonacci series:\n ");
puts("0\n1");
v0 = 9;
v1 = 1;
LODWORD(result) = 0;
do
{
v3 = v1;
v1 += result;
printf("%d\n", v1);
thread1_Yield();
result = v3;
--v0;
}
while ( v0 );
return result;
}

canonical_decompiled_code:
__int64 func14()
{
int v0; // ebp
unsigned int v1; // ebx
__int64 result; // rax
unsigned int v3; // er12

puts("I'm thread B: ");
printf("fibonacci series:\n ");
puts("0\n1");
v0 = 9;
v1 = 1;
LODWORD(result) = 0;
do
{
v3 = v1;
v1 += result;
printf("%d\n", v1);
func9();
result = v3;
--v0;
}
while ( v0 );
return result;
}

original_code:
void fib()
{
 int i,next;
 int first=0;
 int second=1;
 printf("I'm thread B: \n");
 printf("fibonacci series:\n ");
 printf( "0\n1\n" );
 for( i = 2; i<=10; i++ )
 {
  next = first + second;
  printf("%d\n",next);
  first = second;
  second = next;
  thread1_Yield();
 }
}

canonical_original_code:
void fib()
{
 int i; int next;
 int first = 0;
 int second = 1;
 printf("I'm thread B: \n");
 printf("fibonacci series:\n ");
 printf( "0\n1\n" );
 for( i = 2; i<=10; i++ )
 {
  next = first + second;
  printf("%d\n",next);
  first = second;
  second = next;
  thread1_Yield();
 }
}

variable_types: next int

variable_types: first int

variable_types: second int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init_threads();
spawn_thread1((__int64)EvenOdd);
spawn_thread1((__int64)fib);
spawn_thread1((__int64)squares);
waitForAllthreads(squares, argv);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func8();
func10((__int64)EvenOdd);
func10((__int64)fib);
func10((__int64)squares);
func11(squares, argv);
return 0;
}

original_code:
int main()
{
 init_threads();
 spawn_thread1(&EvenOdd);
 spawn_thread1(&fib);
 spawn_thread1(&squares);
 waitForAllthreads();
 return 0;
}


decompiled_code:
size_t __fastcall correct_dict(const char *a1)
{
size_t result; // rax

result = strlen(a1);
if ( a1[(int)result - 1] != 47 )
*(_WORD *)&a1[result] = 47;
return result;
}

canonical_decompiled_code:
size_t __fastcall func7(const char *a1)
{
size_t result; // rax

result = strlen(a1);
if ( a1[(int)result - 1] != 47 )
*(_WORD *)&a1[result] = 47;
return result;
}

original_code:
void correct_dict(char *p)
{
        int l;
        l = strlen(p);
        if (p[l-1]!='/')
                strcat(p,"/");
}

variable_types:
p
char *

variable_types: l int


callerror

decompiled_code:
void __fastcall __noreturn callerror(char *s, char *src)
{
__int64 v2[2]; // [rsp+0h] [rbp-218h] BYREF
char v3[496]; // [rsp+10h] [rbp-208h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
memset(v3, 0, sizeof(v3));
strcat((char *)v2, s);
strcat((char *)v2, src);
perror(s);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(char *s, char *src)
{
__int64 v2[2]; // [rsp+0h] [rbp-218h] BYREF
char v3[496]; // [rsp+10h] [rbp-208h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
memset(v3, 0, sizeof(v3));
strcat((char *)v2, s);
strcat((char *)v2, src);
perror(s);
exit(1);
}

original_code:
void callerror(char *string, char *file)
{
 char error[512] = "";
 strcat(error, string);
 strcat(error, file);
 perror(string);
 exit(1);
}

variable_types:
string
char *

variable_types:
file
char *

variable_types:
error
char [512]


decompiled_code:
ssize_t __fastcall write_file(char *src, char *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er12
ssize_t result; // rax
_BYTE v6[552]; // [rsp+0h] [rbp-228h] BYREF

v2 = open(src, 0);
if ( v2 <= 0 )
callerror("error in opening ", src);
v3 = v2;
v4 = open(a2, 577, 420LL);
if ( v4 <= 0 )
callerror("error in opening ", a2);
while ( 1 )
{
result = read(v3, v6, 0x200uLL);
if ( (int)result <= 0 )
break;
if ( write(v4, v6, (int)result) != (int)result )
callerror("error in writing", a2);
}
if ( (int)result < 0 )
callerror("error in reading ", src);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(char *src, char *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er12
ssize_t result; // rax
_BYTE v6[552]; // [rsp+0h] [rbp-228h] BYREF

v2 = open(src, 0);
if ( v2 <= 0 )
func8("error in opening ", src);
v3 = v2;
v4 = open(a2, 577, 420LL);
if ( v4 <= 0 )
func8("error in opening ", a2);
while ( 1 )
{
result = read(v3, v6, 0x200uLL);
if ( (int)result <= 0 )
break;
if ( write(v4, v6, (int)result) != (int)result )
func8("error in writing", a2);
}
if ( (int)result < 0 )
func8("error in reading ", src);
return result;
}

original_code:
void write_file(char *arg1,char *arg2)
{
        int fdr,fdw,n;
        char buf[512];
        fdr = open(arg1,00);
        if (fdr <= 0)
  callerror("error in opening ", arg1);
        fdw = open(arg2,01|0100|01000, 0644);
        if (fdw <= 0)
  callerror("error in opening ",arg2);
        while ((n = read(fdr,buf,sizeof(buf)))>0)
                if (write(fdw, buf, n) != n)
   callerror("error in writing", arg2);
        if (n < 0)
                callerror("error in reading ",arg1);
}

canonical_original_code:
void write_file(char *arg1, char *arg2)
{
        int fdr; int fdw; int n;
        char buf[512];
        fdr = open(arg1,00);
        if (fdr <= 0)
  callerror("error in opening ", arg1);
        fdw = open(arg2,01|0100|01000, 0644);
        if (fdw <= 0)
  callerror("error in opening ",arg2);
        while ((n = read(fdr,buf,sizeof(buf)))>0)
                if (write(fdw, buf, n) != n)
   callerror("error in writing", arg2);
        if (n < 0)
                callerror("error in reading ",arg1);
}

variable_types: fdr int

variable_types: fdw int

variable_types: n int

variable_types:
buf
char [512]


openfile

decompiled_code:
int __fastcall openfile(char *src)
{
int result; // eax

result = open(src, 0);
if ( result < 0 )
callerror("error in opening ", src);
return result;
}

canonical_decompiled_code:
int __fastcall func10(char *src)
{
int result; // eax

result = open(src, 0);
if ( result < 0 )
func8("error in opening ", src);
return result;
}

original_code:
int openfile(char *file)
{
 int fd;
 fd = open(file,00);
 if (fd < 0){
  callerror("error in opening ",file);
 }
 return fd;
}

variable_types: fd int


cmp

original_code:
int cmp(char a[],char b[])
{
        int i,l;
        l = 4;
        for (i = 0; i<l; i++)
                if (a[i]!=b[i])
                        return 0;
        return 1;
}

canonical_original_code:
int cmp(char a[], char b[])
{
        int i; int l;
        l = 4;
        for (i = 0; i<l; i++)
                if (a[i]!=b[i])
                        return 0;
        return 1;
}

variable_types:
a
char []

variable_types:
b
char []

variable_types: i int


decompiled_code:
__int64 __fastcall get_format(char *src, char *s)
{
int v2; // ebp
__int64 result; // rax
int v4; // [rsp+Eh] [rbp-3Ah] BYREF
__int16 v5; // [rsp+12h] [rbp-36h]
int v6; // [rsp+14h] [rbp-34h] BYREF
__int16 v7; // [rsp+18h] [rbp-30h]
int v8; // [rsp+1Ah] [rbp-2Eh] BYREF
__int16 v9; // [rsp+1Eh] [rbp-2Ah]
char v10[40]; // [rsp+20h] [rbp-28h] BYREF

strcpy(v10, "%PDF-");
v8 = -520103681;
v9 = 0;
v6 = 1196314761;
v7 = 13;
v4 = 0x20000000;
v5 = 102;
v2 = openfile(src);
if ( (int)read(v2, &v10[6], 0xAuLL) < 0 )
callerror("error in reading", src);
close(v2);
*s = 0;
if ( (unsigned int)cmp((__int64)v10, (__int64)&v10[6]) )
{
result = strlen(s);
strcpy(&s[result], "pdf/");
}
else if ( (unsigned int)cmp((__int64)&v8, (__int64)&v10[6]) )
{
result = strlen(s);
strcpy(&s[result], "jpg/");
}
else if ( (unsigned int)cmp((__int64)&v6, (__int64)&v10[6]) )
{
result = strlen(s);
strcpy(&s[result], "png/");
}
else
{
result = cmp((__int64)&v4, (__int64)&v10[6]);
if ( (_DWORD)result )
{
result = strlen(s);
strcpy(&s[result], "mp4/");
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *src, char *s)
{
int v2; // ebp
__int64 result; // rax
int v4; // [rsp+Eh] [rbp-3Ah] BYREF
__int16 v5; // [rsp+12h] [rbp-36h]
int v6; // [rsp+14h] [rbp-34h] BYREF
__int16 v7; // [rsp+18h] [rbp-30h]
int v8; // [rsp+1Ah] [rbp-2Eh] BYREF
__int16 v9; // [rsp+1Eh] [rbp-2Ah]
char v10[40]; // [rsp+20h] [rbp-28h] BYREF

strcpy(v10, "%PDF-");
v8 = -520103681;
v9 = 0;
v6 = 1196314761;
v7 = 13;
v4 = 0x20000000;
v5 = 102;
v2 = func10(src);
if ( (int)read(v2, &v10[6], 0xAuLL) < 0 )
func8("error in reading", src);
close(v2);
*s = 0;
if ( (unsigned int)func11((__int64)v10, (__int64)&v10[6]) )
{
result = strlen(s);
strcpy(&s[result], "pdf/");
}
else if ( (unsigned int)func11((__int64)&v8, (__int64)&v10[6]) )
{
result = strlen(s);
strcpy(&s[result], "jpg/");
}
else if ( (unsigned int)func11((__int64)&v6, (__int64)&v10[6]) )
{
result = strlen(s);
strcpy(&s[result], "png/");
}
else
{
result = func11((__int64)&v4, (__int64)&v10[6]);
if ( (_DWORD)result )
{
result = strlen(s);
strcpy(&s[result], "mp4/");
}
}
return result;
}

original_code:
void get_format(char *loc, char *format)
{
 int fd, l;
 char buff[10];
        char pdf[] = {0x25,0x50,0x44,0x46,0x2d,'\0'};
        char jpg[] = {0xff,0xd8,0xff,0xe0,0x00,'\0'};
        char png[] = {0x89,0x50,0x4e,0x47,0x0d,'\0'};
        char mp4[] = {0x00,0x00,0x00,0x20,0x66,'\0'};
 fd = openfile(loc);
 l = read(fd,buff,10);
 if (l < 0)
  callerror("error in reading",loc);
 close(fd);
 format[0] = '\0';
 if (cmp(pdf,buff))
  strcat(format,"pdf/");
 else if (cmp(jpg,buff))
  strcat(format,"jpg/");
        else if (cmp(png,buff))
  strcat(format,"png/");
 else if (cmp(mp4,buff))
  strcat(format,"mp4/");
 else
  format[0] = '\0';
}

canonical_original_code:
void get_format(char *loc, char *format)
{
 int fd; int l;
 char buff[10];
        char pdf[] = {0x25,0x50,0x44,0x46,0x2d,'\0'};
        char jpg[] = {0xff,0xd8,0xff,0xe0,0x00,'\0'};
        char png[] = {0x89,0x50,0x4e,0x47,0x0d,'\0'};
        char mp4[] = {0x00,0x00,0x00,0x20,0x66,'\0'};
 fd = openfile(loc);
 l = read(fd,buff,10);
 if (l < 0)
  callerror("error in reading",loc);
 close(fd);
 format[0] = '\0';
 if (cmp(pdf,buff))
  strcat(format,"pdf/");
 else if (cmp(jpg,buff))
  strcat(format,"jpg/");
        else if (cmp(png,buff))
  strcat(format,"png/");
 else if (cmp(mp4,buff))
  strcat(format,"mp4/");
 else
  format[0] = '\0';
}

variable_types:
loc
char *

variable_types:
format
char *

variable_types:
buff
char [10]

variable_types:
pdf
char []

variable_types:
jpg
char []

variable_types:
png
char []


decompiled_code:
char *__fastcall correct_ext(char *dest, char *src)
{
src[(int)strlen(src) - 1] = 0;
*(_WORD *)&dest[strlen(dest)] = 46;
return strcat(dest, src);
}

canonical_decompiled_code:
char *__fastcall func13(char *dest, char *src)
{
src[(int)strlen(src) - 1] = 0;
*(_WORD *)&dest[strlen(dest)] = 46;
return strcat(dest, src);
}

original_code:
void correct_ext(char *file, char *format)
{
 int i;
 i = strlen(format);
 format[i - 1] ='\0';
 strcat(file,".");
 strcat(file,format);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
int v4; // eax
int v5; // er14
int v6; // er13
int v7; // ebp
int v8; // er15
char *v9; // rbx
struct stat v10; // [rsp+0h] [rbp-4CD8h] BYREF
char src[8]; // [rsp+90h] [rbp-4C48h] BYREF
__int64 v12; // [rsp+98h] [rbp-4C40h]
char v13[496]; // [rsp+A0h] [rbp-4C38h] BYREF
char dest[8]; // [rsp+290h] [rbp-4A48h] BYREF
__int64 v15; // [rsp+298h] [rbp-4A40h]
char v16[496]; // [rsp+2A0h] [rbp-4A38h] BYREF
char s[15]; // [rsp+491h] [rbp-4847h] BYREF
char path[1024]; // [rsp+4A0h] [rbp-4838h] BYREF
char filename[1024]; // [rsp+8A0h] [rbp-4438h] BYREF
char buf[16440]; // [rsp+CA0h] [rbp-4038h] BYREF

*(_QWORD *)dest = 0LL;
v15 = 0LL;
memset(v16, 0, sizeof(v16));
*(_QWORD *)src = 0LL;
v12 = 0LL;
memset(v13, 0, sizeof(v13));
if ( argc == 3 )
{
strcat(dest, argv[1]);
strcat(src, argv[2]);
}
else
{
if ( argc != 2 )
{
printf("take one or two arguments,%d given\n", (unsigned int)(argc - 1));
exit(1);
}
v3 = argv[1];
strcat(dest, v3);
strcat(src, v3);
}
correct_dict(dest);
correct_dict(src);
printf("copy from %s to %s..................\n", dest, src);
v4 = inotify_init();
v5 = v4;
if ( v4 >= 0 )
{
if ( inotify_add_watch(v4, argv[1], 0x88u) >= 0 )
{
while ( 1 )
{
v6 = read(v5, buf, 0x4000uLL);
if ( v6 <= 0 )
break;
v7 = 0;
do
{
v9 = &buf[v7];
if ( *((_DWORD *)v9 + 3) )
{
filename[0] = 0;
strcat(filename, dest);
strcat(filename, v9 + 16);
printf("found a new %s \n", filename);
if ( (unsigned int)stat(filename, &v10) == -1 )
{
printf("can not get status of %s \n", filename);
v7 += *((_DWORD *)v9 + 3) + 16;
}
else if ( (v10.st_mode & 0x8000) == 0 )
{
printf("\t but not a file");
v7 += *((_DWORD *)v9 + 3) + 16;
}
else
{
get_format(filename, s);
if ( s[0] )
{
path[0] = 0;
strcat(path, src);
strcat(path, s);
v8 = mkdir(path, 0x1FFu);
if ( *__errno_location() != 17 && v8 < 0 )
callerror("error in creating directory", path);
strcat(path, v9 + 16);
correct_ext(path, s);
write_file(filename, path);
printf("just copyed from %s to %s \n", filename, path);
v7 += *((_DWORD *)v9 + 3) + 16;
}
else
{
puts("unknown file signature file skiped");
v7 += *((_DWORD *)v9 + 3) + 16;
}
}
}
else
{
v7 += 16;
}
}
while ( v7 < v6 );
}
callerror("Problem in reading watch instance", (char *)"");
}
callerror("can not add inotify watch", (char *)"");
}
callerror("cannot obtain an inotify instance", (char *)"");
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
int v4; // eax
int v5; // er14
int v6; // er13
int v7; // ebp
int v8; // er15
char *v9; // rbx
struct stat v10; // [rsp+0h] [rbp-4CD8h] BYREF
char src[8]; // [rsp+90h] [rbp-4C48h] BYREF
__int64 v12; // [rsp+98h] [rbp-4C40h]
char v13[496]; // [rsp+A0h] [rbp-4C38h] BYREF
char dest[8]; // [rsp+290h] [rbp-4A48h] BYREF
__int64 v15; // [rsp+298h] [rbp-4A40h]
char v16[496]; // [rsp+2A0h] [rbp-4A38h] BYREF
char s[15]; // [rsp+491h] [rbp-4847h] BYREF
char path[1024]; // [rsp+4A0h] [rbp-4838h] BYREF
char filename[1024]; // [rsp+8A0h] [rbp-4438h] BYREF
char buf[16440]; // [rsp+CA0h] [rbp-4038h] BYREF

*(_QWORD *)dest = 0LL;
v15 = 0LL;
memset(v16, 0, sizeof(v16));
*(_QWORD *)src = 0LL;
v12 = 0LL;
memset(v13, 0, sizeof(v13));
if ( argc == 3 )
{
strcat(dest, argv[1]);
strcat(src, argv[2]);
}
else
{
if ( argc != 2 )
{
printf("take one or two arguments,%d given\n", (unsigned int)(argc - 1));
exit(1);
}
v3 = argv[1];
strcat(dest, v3);
strcat(src, v3);
}
func7(dest);
func7(src);
printf("copy from %s to %s..................\n", dest, src);
v4 = inotify_init();
v5 = v4;
if ( v4 >= 0 )
{
if ( inotify_add_watch(v4, argv[1], 0x88u) >= 0 )
{
while ( 1 )
{
v6 = read(v5, buf, 0x4000uLL);
if ( v6 <= 0 )
break;
v7 = 0;
do
{
v9 = &buf[v7];
if ( *((_DWORD *)v9 + 3) )
{
filename[0] = 0;
strcat(filename, dest);
strcat(filename, v9 + 16);
printf("found a new %s \n", filename);
if ( (unsigned int)func16(filename, &v10) == -1 )
{
printf("can not get status of %s \n", filename);
v7 += *((_DWORD *)v9 + 3) + 16;
}
else if ( (v10.st_mode & 0x8000) == 0 )
{
printf("\t but not a file");
v7 += *((_DWORD *)v9 + 3) + 16;
}
else
{
func12(filename, s);
if ( s[0] )
{
path[0] = 0;
strcat(path, src);
strcat(path, s);
v8 = mkdir(path, 0x1FFu);
if ( *__errno_location() != 17 && v8 < 0 )
func8("error in creating directory", path);
strcat(path, v9 + 16);
func13(path, s);
func9(filename, path);
printf("just copyed from %s to %s \n", filename, path);
v7 += *((_DWORD *)v9 + 3) + 16;
}
else
{
puts("unknown file signature file skiped");
v7 += *((_DWORD *)v9 + 3) + 16;
}
}
}
else
{
v7 += 16;
}
}
while ( v7 < v6 );
}
func8("Problem in reading watch instance", (char *)"");
}
func8("can not add inotify watch", (char *)"");
}
func8("cannot obtain an inotify instance", (char *)"");
}

original_code:
int main(int arg, char *argv[])
{
 int ifd, wd, i, n, a;
 char buf[16384];
 char fileloc[1024];
 char fileloc1[1024];
 char format[15];
        char arg1[512] = "";
        char arg2[512] = "";
 struct inotify_event *ev;
 struct stat sb;
        if (arg == 3){
         strcat(arg1,argv[1]);
         strcat(arg2,argv[2]);
        }
        else if (arg == 2){
         strcat(arg1,argv[1]);
         strcat(arg2,argv[1]);
        }
        else {
                printf("take one or two arguments,%d given\n",arg -1);
                exit(1);
        }
        correct_dict(arg1);
        correct_dict(arg2);
 printf("copy from %s to  %s..................\n",arg1,arg2);
 ifd = inotify_init();
 if (ifd < 0)
  callerror("cannot obtain an inotify instance","");
 wd = inotify_add_watch(ifd, argv[1], 0x00000008|0x00000080);
 if (wd < 0)
  callerror("can not add inotify watch","");
 while (1){
  n = read(ifd, buf, sizeof(buf));
  if (n<= 0)
   callerror("Problem in reading watch instance","");
  i = 0;
  while (i < n){
   ev = (struct inotify_event *) &buf[i];
   if (!ev->len){
    i += sizeof(struct inotify_event) + ev->len;
    continue;
   }
   fileloc[0] = '\0';
                 strcat(fileloc,arg1);
                 strcat(fileloc,ev->name);
   printf("found a new %s \n", fileloc);
   if (stat(fileloc, &sb) == -1){
    printf("can not get status of %s \n",fileloc);
                                i += sizeof(struct inotify_event) + ev->len;
                                continue;
   }
   if (!(sb.st_mode & 0100000)){
    printf("\t but not a file");
    i += sizeof(struct inotify_event) + ev->len;
    continue;
   }
   get_format(fileloc,format);
   if (format[0] == '\0'){
    printf("unknown file signature file skiped\n");
    i += sizeof(struct inotify_event) + ev->len;
    continue;
   }
                        fileloc1[0] = '\0';
                        strcat(fileloc1,arg2);
   strcat(fileloc1,format);
                        a = mkdir(fileloc1,0777);
                        if ((a < 0)&((*__errno_location ()) != 17))
                                callerror("error in creating directory",fileloc1);
                        strcat(fileloc1,ev->name);
   correct_ext(fileloc1,format);
                        write_file(fileloc,fileloc1);
   printf("just copyed from %s to  %s \n",fileloc,fileloc1);
   i += sizeof(struct inotify_event) + ev->len;
  }
 }
}

canonical_original_code:
int main(int arg, char *argv[])
{
 int ifd; int wd; int i; int n; int a;
 char buf[16384];
 char fileloc[1024];
 char fileloc1[1024];
 char format[15];
        char arg1[512] = "";
        char arg2[512] = "";
 struct inotify_event *ev;
 struct stat sb;
        if (arg == 3){
         strcat(arg1,argv[1]);
         strcat(arg2,argv[2]);
        }
        else if (arg == 2){
         strcat(arg1,argv[1]);
         strcat(arg2,argv[1]);
        }
        else {
                printf("take one or two arguments,%d given\n",arg -1);
                exit(1);
        }
        correct_dict(arg1);
        correct_dict(arg2);
 printf("copy from %s to  %s..................\n",arg1,arg2);
 ifd = inotify_init();
 if (ifd < 0)
  callerror("cannot obtain an inotify instance","");
 wd = inotify_add_watch(ifd, argv[1], 0x00000008|0x00000080);
 if (wd < 0)
  callerror("can not add inotify watch","");
 while (1){
  n = read(ifd, buf, sizeof(buf));
  if (n<= 0)
   callerror("Problem in reading watch instance","");
  i = 0;
  while (i < n){
   ev = (struct inotify_event *) &buf[i];
   if (!ev->len){
    i += sizeof(struct inotify_event) + ev->len;
    continue;
   }
   fileloc[0] = '\0';
                 strcat(fileloc,arg1);
                 strcat(fileloc,ev->name);
   printf("found a new %s \n", fileloc);
   if (stat(fileloc, &sb) == -1){
    printf("can not get status of %s \n",fileloc);
                                i += sizeof(struct inotify_event) + ev->len;
                                continue;
   }
   if (!(sb.st_mode & 0100000)){
    printf("\t but not a file");
    i += sizeof(struct inotify_event) + ev->len;
    continue;
   }
   get_format(fileloc,format);
   if (format[0] == '\0'){
    printf("unknown file signature file skiped\n");
    i += sizeof(struct inotify_event) + ev->len;
    continue;
   }
                        fileloc1[0] = '\0';
                        strcat(fileloc1,arg2);
   strcat(fileloc1,format);
                        a = mkdir(fileloc1,0777);
                        if ((a < 0)&((*__errno_location ()) != 17))
                                callerror("error in creating directory",fileloc1);
                        strcat(fileloc1,ev->name);
   correct_ext(fileloc1,format);
                        write_file(fileloc,fileloc1);
   printf("just copyed from %s to  %s \n",fileloc,fileloc1);
   i += sizeof(struct inotify_event) + ev->len;
  }
 }
}

variable_types: arg int

variable_types:
argv
char *[]

variable_types: ifd int

variable_types: wd int

variable_types: a int

variable_types:
buf
char [16384]

variable_types:
fileloc
char [1024]

variable_types:
format
char [15]

variable_types: struct inotify_event { int wd; unsigned int mask; unsigned int cookie; unsigned int len; char name[]; } *

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


canonical_original_code:
void correct_dict(char *p)
{
        int l;
        l = strlen(p);
        if (p[l-1]!='/')
                strcat(p,"/");
}

variable_types:
p
char *

variable_types: l int


decompiled_code:
ssize_t __fastcall write_file(char *src, char *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er12
ssize_t result; // rax
size_t v6; // rax
_BYTE v7[552]; // [rsp+0h] [rbp-228h] BYREF

v2 = open(src, 0);
if ( v2 <= 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in opening ");
strcat(error, src);
perror(error);
exit(1);
}
v3 = v2;
v4 = open(a2, 577, 420LL);
if ( v4 <= 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in opening ");
strcat(error, a2);
perror(error);
exit(1);
}
while ( 1 )
{
result = read(v3, v7, 0x200uLL);
if ( (int)result <= 0 )
break;
if ( write(v4, v7, (int)result) != (int)result )
{
error[0] = 0;
v6 = strlen(error);
*(_QWORD *)&error[v6] = 0x6920726F7272650ALL;
*(__int64 *)((char *)&qword_4040C8 + v6) = 0x6E6974697277206ELL;
*(__int16 *)((char *)&word_4040D0 + v6) = 8295;
byte_4040D2[v6] = 0;
strcat(error, a2);
perror(error);
exit(1);
}
}
if ( (int)result < 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in reading ");
strcat(error, src);
perror(error);
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func8(char *src, char *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er12
ssize_t result; // rax
size_t v6; // rax
_BYTE v7[552]; // [rsp+0h] [rbp-228h] BYREF

v2 = open(src, 0);
if ( v2 <= 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in opening ");
strcat(error, src);
perror(error);
exit(1);
}
v3 = v2;
v4 = open(a2, 577, 420LL);
if ( v4 <= 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in opening ");
strcat(error, a2);
perror(error);
exit(1);
}
while ( 1 )
{
result = read(v3, v7, 0x200uLL);
if ( (int)result <= 0 )
break;
if ( write(v4, v7, (int)result) != (int)result )
{
error[0] = 0;
v6 = strlen(error);
*(_QWORD *)&error[v6] = 0x6920726F7272650ALL;
*(__int64 *)((char *)&qword_4040C8 + v6) = 0x6E6974697277206ELL;
*(__int16 *)((char *)&word_4040D0 + v6) = 8295;
byte_4040D2[v6] = 0;
strcat(error, a2);
perror(error);
exit(1);
}
}
if ( (int)result < 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in reading ");
strcat(error, src);
perror(error);
exit(1);
}
return result;
}

variable_types: fdr int

variable_types: fdw int

variable_types: n int

variable_types:
buf
char [512]


openfile

decompiled_code:
int __fastcall openfile(char *src)
{
int result; // eax

result = open(src, 0);
if ( result < 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in opening ");
strcat(error, src);
perror(error);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char *src)
{
int result; // eax

result = open(src, 0);
if ( result < 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in opening ");
strcat(error, src);
perror(error);
exit(1);
}
return result;
}

canonical_original_code:
int openfile(char *file)
{
 int fd;
 fd = open(file,00);
 if (fd < 0){
  callerror("error in opening ",file);
 }
 return fd;
}

variable_types:
file
char *

variable_types: fd int


cmp

decompiled_code:
__int64 __fastcall cmp(const char *a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rcx
__int64 i; // rax

v2 = strlen(a1);
if ( v2 <= 0 )
return 1LL;
v3 = (unsigned int)(v2 - 1);
for ( i = 0LL; a1[i] == *(_BYTE *)(a2 + i); ++i )
{
if ( i == v3 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rcx
__int64 i; // rax

v2 = strlen(a1);
if ( v2 <= 0 )
return 1LL;
v3 = (unsigned int)(v2 - 1);
for ( i = 0LL; a1[i] == *(_BYTE *)(a2 + i); ++i )
{
if ( i == v3 )
return 1LL;
}
return 0LL;
}

variable_types:
a
char []

variable_types:
b
char []

variable_types: i int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
DIR *v4; // r12
int v5; // ebp
struct dirent *v6; // rax
struct dirent *v7; // rbx
const char *v8; // rbx
int v9; // ebp
char buf[5]; // [rsp+Bh] [rbp-50Dh] BYREF
char v12[8]; // [rsp+10h] [rbp-508h] BYREF
__int64 v13; // [rsp+18h] [rbp-500h]
char v14[480]; // [rsp+20h] [rbp-4F8h] BYREF
int v15; // [rsp+200h] [rbp-318h]
char dest[8]; // [rsp+210h] [rbp-308h] BYREF
__int64 v17; // [rsp+218h] [rbp-300h]
char v18[480]; // [rsp+220h] [rbp-2F8h] BYREF
int v19; // [rsp+400h] [rbp-118h]
char s[112]; // [rsp+410h] [rbp-108h] BYREF
char src[104]; // [rsp+480h] [rbp-98h] BYREF
int v22; // [rsp+4E8h] [rbp-30h] BYREF
__int16 v23; // [rsp+4ECh] [rbp-2Ch]
int v24; // [rsp+4EEh] [rbp-2Ah] BYREF
__int16 v25; // [rsp+4F2h] [rbp-26h]
int v26; // [rsp+4F4h] [rbp-24h] BYREF
__int16 v27; // [rsp+4F8h] [rbp-20h]
char v28[30]; // [rsp+4FAh] [rbp-1Eh] BYREF

strcpy(v28, "%PDF-");
v26 = -520103681;
v27 = 0;
v24 = 1196314761;
v25 = 13;
v22 = 0x20000000;
v23 = 102;
*(_QWORD *)dest = 0LL;
v17 = 0LL;
memset(v18, 0, sizeof(v18));
v19 = 0;
*(_QWORD *)v12 = 0LL;
v13 = 0LL;
memset(v14, 0, sizeof(v14));
v15 = 0;
if ( argc == 3 )
{
strcat(dest, argv[1]);
strcat(v12, argv[2]);
}
else
{
if ( argc != 2 )
{
printf("take one or two arguments,%d given\n", (unsigned int)(argc - 1));
exit(1);
}
v3 = argv[1];
strcat(dest, v3);
strcat(v12, v3);
}
correct_dict(dest);
correct_dict(v12);
v4 = open_dir(dest);
while ( 1 )
{
v6 = readdir(v4);
v7 = v6;
if ( !v6 )
break;
if ( v6->d_type == 8 )
{
src[0] = 0;
strcat(src, dest);
v8 = v7->d_name;
strcat(src, v8);
v9 = openfile(src);
if ( read(v9, buf, 5uLL) < 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in reading ");
strcat(error, src);
perror(error);
exit(1);
}
close(v9);
s[0] = 0;
strcat(s, v12);
if ( (unsigned int)cmp((const char *)&v24, (__int64)buf) )
{
strcpy(&s[strlen(s)], "png/");
goto LABEL_9;
}
if ( (unsigned int)cmp((const char *)&v26, (__int64)buf) )
{
strcpy(&s[strlen(s)], "jpg/");
goto LABEL_9;
}
if ( (unsigned int)cmp(v28, (__int64)buf) )
{
strcpy(&s[strlen(s)], "pdf/");
LABEL_9:
v5 = mkdir(s, 0x1FFu);
if ( *__errno_location() != 17 && v5 < 0 )
{
error[0] = 47;
strcpy((char *)(strlen(error) + 4210880), "\nerror in creating directory ");
strcat(error, s);
perror(error);
exit(1);
}
strcat(s, v8);
write_file(src, s);
unlink(src);
}
else if ( (unsigned int)cmp((const char *)&v22, (__int64)buf) )
{
strcpy(&s[strlen(s)], "mp4/");
goto LABEL_9;
}
}
}
closedir(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
DIR *v4; // r12
int v5; // ebp
struct dirent *v6; // rax
struct dirent *v7; // rbx
const char *v8; // rbx
int v9; // ebp
char buf[5]; // [rsp+Bh] [rbp-50Dh] BYREF
char v12[8]; // [rsp+10h] [rbp-508h] BYREF
__int64 v13; // [rsp+18h] [rbp-500h]
char v14[480]; // [rsp+20h] [rbp-4F8h] BYREF
int v15; // [rsp+200h] [rbp-318h]
char dest[8]; // [rsp+210h] [rbp-308h] BYREF
__int64 v17; // [rsp+218h] [rbp-300h]
char v18[480]; // [rsp+220h] [rbp-2F8h] BYREF
int v19; // [rsp+400h] [rbp-118h]
char s[112]; // [rsp+410h] [rbp-108h] BYREF
char src[104]; // [rsp+480h] [rbp-98h] BYREF
int v22; // [rsp+4E8h] [rbp-30h] BYREF
__int16 v23; // [rsp+4ECh] [rbp-2Ch]
int v24; // [rsp+4EEh] [rbp-2Ah] BYREF
__int16 v25; // [rsp+4F2h] [rbp-26h]
int v26; // [rsp+4F4h] [rbp-24h] BYREF
__int16 v27; // [rsp+4F8h] [rbp-20h]
char v28[30]; // [rsp+4FAh] [rbp-1Eh] BYREF

strcpy(v28, "%PDF-");
v26 = -520103681;
v27 = 0;
v24 = 1196314761;
v25 = 13;
v22 = 0x20000000;
v23 = 102;
*(_QWORD *)dest = 0LL;
v17 = 0LL;
memset(v18, 0, sizeof(v18));
v19 = 0;
*(_QWORD *)v12 = 0LL;
v13 = 0LL;
memset(v14, 0, sizeof(v14));
v15 = 0;
if ( argc == 3 )
{
strcat(dest, argv[1]);
strcat(v12, argv[2]);
}
else
{
if ( argc != 2 )
{
printf("take one or two arguments,%d given\n", (unsigned int)(argc - 1));
exit(1);
}
v3 = argv[1];
strcat(dest, v3);
strcat(v12, v3);
}
func7(dest);
func7(v12);
v4 = open_dir(dest);
while ( 1 )
{
v6 = readdir(v4);
v7 = v6;
if ( !v6 )
break;
if ( v6->d_type == 8 )
{
src[0] = 0;
strcat(src, dest);
v8 = v7->d_name;
strcat(src, v8);
v9 = func9(src);
if ( read(v9, buf, 5uLL) < 0 )
{
error[0] = 0;
strcpy((char *)(strlen(error) + 4210880), "\nerror in reading ");
strcat(error, src);
perror(error);
exit(1);
}
close(v9);
s[0] = 0;
strcat(s, v12);
if ( (unsigned int)func10((const char *)&v24, (__int64)buf) )
{
strcpy(&s[strlen(s)], "png/");
goto LABEL_9;
}
if ( (unsigned int)func10((const char *)&v26, (__int64)buf) )
{
strcpy(&s[strlen(s)], "jpg/");
goto LABEL_9;
}
if ( (unsigned int)func10(v28, (__int64)buf) )
{
strcpy(&s[strlen(s)], "pdf/");
LABEL_9:
v5 = mkdir(s, 0x1FFu);
if ( *__errno_location() != 17 && v5 < 0 )
{
error[0] = 47;
strcpy((char *)(strlen(error) + 4210880), "\nerror in creating directory ");
strcat(error, s);
perror(error);
exit(1);
}
strcat(s, v8);
func8(src, s);
unlink(src);
}
else if ( (unsigned int)func10((const char *)&v22, (__int64)buf) )
{
strcpy(&s[strlen(s)], "mp4/");
goto LABEL_9;
}
}
}
closedir(v4);
return 0;
}

variable_types: arg int

variable_types:
argv
char *[]

variable_types: ifd int

variable_types: wd int

variable_types: a int

user_defined_types: struct inotify_event { int wd; unsigned int mask; unsigned int cookie; unsigned int len; char name[]; }

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


repo: aklomp/anagram

original_code:
static _Bool
input_from_args (const struct config *config, struct input *input)
{
 char *p;
 input->str = ((void *)0);
 input->len = 0;
 if (config->words.ac == 0) {
  return 0;
 }
 for (int i = 0; i < config->words.ac; i++) {
  for (char *c = config->words.av[i]; *c; c++) {
   if (!((*__ctype_b_loc ())[(int) ((*c))] & (unsigned short int) _ISspace)) {
    input->len++;
   }
  }
 }
 if (input->len == 0) {
  return 0;
 }
 if ((p = input->str = malloc(input->len)) == ((void *)0)) {
  return 0;
 }
 for (int i = 0; i < config->words.ac; i++) {
  for (char *c = config->words.av[i]; *c; c++) {
   if (!((*__ctype_b_loc ())[(int) ((*c))] & (unsigned short int) _ISspace)) {
    *p++ = *c;
   }
  }
 }
 return 1;
}

canonical_original_code:
static _Bool input_from_args(struct config *config, struct input *input)
{
 char *p;
 input->str = ((void *)0);
 input->len = 0;
 if (config->words.ac == 0) {
  return 0;
 }
 for (int i = 0; i < config->words.ac; i++) {
  for (char *c = config->words.av[i]; *c; c++) {
   if (!((*__ctype_b_loc ())[(int) ((*c))] & (unsigned short) _ISspace)) {
    input->len++;
   }
  }
 }
 if (input->len == 0) {
  return 0;
 }
 if ((p = input->str = malloc(input->len)) == ((void *)0)) {
  return 0;
 }
 for (int i = 0; i < config->words.ac; i++) {
  for (char *c = config->words.av[i]; *c; c++) {
   if (!((*__ctype_b_loc ())[(int) ((*c))] & (unsigned short) _ISspace)) {
    *p++ = *c;
   }
  }
 }
 return 1;
}

variable_types: struct config { char *name; char *dictfile; struct args { char **av; int ac; } words; unsigned char minlength; unsigned char haslength; _Bool print_help; } *

variable_types: struct input { char *str; unsigned long len; } *

variable_types:
c
char *

user_defined_types: struct config { char *name; char *dictfile; struct args words; unsigned char minlength; unsigned char haslength; _Bool print_help; }


original_code:
static _Bool
input_from_stdin (struct input *input)
{
 ssize_t nread;
 char *t, *p;
 input->len = 0;
 if ((p = input->str = malloc(100)) == ((void *)0)) {
  return 0;
 }
 while (input->len < 100) {
  if ((nread = read(0, p, 100 - input->len)) < 0) {
   if ((*__errno_location ()) == 4) {
    continue;
   }
   break;
  }
  if (nread == 0) {
   break;
  }
  input->len += (size_t) nread;
  p += (size_t) nread;
 }
 for (char *h = t = input->str; h < input->str + input->len; h++) {
  if (!((*__ctype_b_loc ())[(int) ((*h))] & (unsigned short int) _ISspace)) {
   *t++ = *h;
  }
 }
 if (input->len == 0 || t == input->str) {
  free(input->str);
  input->str = ((void *)0);
  return 0;
 }
 *t = '\0';
 input->len = t - input->str;
 return 1;
}

canonical_original_code:
static _Bool input_from_stdin(struct input *input)
{
 long nread;
 char *t; char *p;
 input->len = 0;
 if ((p = input->str = malloc(100)) == ((void *)0)) {
  return 0;
 }
 while (input->len < 100) {
  if ((nread = read(0, p, 100 - input->len)) < 0) {
   if ((*__errno_location ()) == 4) {
    continue;
   }
   break;
  }
  if (nread == 0) {
   break;
  }
  input->len += (unsigned long) nread;
  p += (unsigned long) nread;
 }
 for (char *h = t = input->str; h < input->str + input->len; h++) {
  if (!((*__ctype_b_loc ())[(int) ((*h))] & (unsigned short) _ISspace)) {
   *t++ = *h;
  }
 }
 if (input->len == 0 || t == input->str) {
  free(input->str);
  input->str = ((void *)0);
  return 0;
 }
 *t = '\0';
 input->len = t - input->str;
 return 1;
}

variable_types: nread long

variable_types:
t
char *

variable_types:
p
char *

variable_types:
h
char *

user_defined_types: struct input { char *str; unsigned long len; }


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

result = input_from_args(a1, a2);
if ( !(_BYTE)result )
result = input_from_stdin(a2);
return result;
}

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

result = func0(a1, a2);
if ( !(_BYTE)result )
result = func1(a2);
return result;
}

original_code:
_Bool
input_get (const struct config *config, struct input *input)
{
 if (input_from_args(config, input)) {
  return 1;
 }
 if (input_from_stdin(input)) {
  return 1;
 }
 return 0;
}

canonical_original_code:
_Bool input_get(struct config *config, struct input *input)
{
 if (input_from_args(config, input)) {
  return 1;
 }
 if (input_from_stdin(input)) {
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall get_uint8(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // edx
char *endptr; // [rsp+8h] [rbp-10h] BYREF

v1 = strtol(optarg, &endptr, 10);
v2 = 0;
if ( (unsigned __int64)(v1 - 1) <= 0xFE && !*endptr )
{
*a1 = v1;
v2 = 1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // edx
char *endptr; // [rsp+8h] [rbp-10h] BYREF

v1 = strtol(optarg, &endptr, 10);
v2 = 0;
if ( (unsigned __int64)(v1 - 1) <= 0xFE && !*endptr )
{
*a1 = v1;
v2 = 1;
}
return v2;
}

original_code:
static _Bool
get_uint8 (uint8_t *dst)
{
 char *eptr;
 const long l = strtol(optarg, &eptr, 10);
 if (l <= 0 || l > 255 || *eptr != '\0') {
  return 0;
 }
 *dst = (uint8_t) l;
 return 1;
}

canonical_original_code:
static _Bool get_uint8(unsigned char *dst)
{
 char *eptr;
 long l = strtol(optarg, &eptr, 10);
 if (l <= 0 || l > 255 || *eptr != '\0') {
  return 0;
 }
 *dst = (unsigned char) l;
 return 1;
}

variable_types:
dst
unsigned char *

variable_types:
eptr
char *

variable_types: l long


decompiled_code:
__int64 __fastcall args_parse(const char **a1, __int64 a2, unsigned int a3)
{
unsigned int v3; // er12
int v4; // eax
unsigned int v6; // er14
int v7; // eax

LOBYTE(a3) = a2 == 0 || a1 == 0LL;
v3 = 0;
if ( !(_BYTE)a3 )
{
v3 = a3;
*a1 = **(const char ***)a2;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v4 = getopt_long(*(_DWORD *)(a2 + 8), *(char *const **)a2, ":hf:m:l:", &opts_0, 0LL);
if ( v4 == -1 )
{
v7 = optind;
*((_DWORD *)a1 + 6) = *(_DWORD *)(a2 + 8) - optind;
a1[2] = (const char *)(*(_QWORD *)a2 + 8LL * v7);
return 1;
}
if ( v4 != 108 )
break;
v6 = get_uint8((_BYTE *)a1 + 33);
if ( !(_BYTE)v6 )
goto LABEL_12;
}
if ( v4 <= 108 )
break;
if ( v4 != 109 )
goto LABEL_16;
v6 = get_uint8((_BYTE *)a1 + 32);
if ( !(_BYTE)v6 )
{
LABEL_12:
fprintf(stderr, "%s: '%s': invalid value.\n", *a1, optarg);
return v6;
}
}
if ( v4 != 102 )
break;
a1[1] = optarg;
}
if ( v4 == 104 )
{
*((_BYTE *)a1 + 34) = 1;
return 1;
}
LABEL_16:
if ( optopt )
fprintf(stderr, "%s: '%c': unknown option.\n", *a1, (unsigned int)optopt);
else
fprintf(stderr, "%s: %s: unknown option.\n", *a1, *(const char **)(*(_QWORD *)a2 + 8LL * optind - 8));
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char **a1, __int64 a2, unsigned int a3)
{
unsigned int v3; // er12
int v4; // eax
unsigned int v6; // er14
int v7; // eax

LOBYTE(a3) = a2 == 0 || a1 == 0LL;
v3 = 0;
if ( !(_BYTE)a3 )
{
v3 = a3;
*a1 = **(const char ***)a2;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v4 = getopt_long(*(_DWORD *)(a2 + 8), *(char *const **)a2, ":hf:m:l:", &opts_0, 0LL);
if ( v4 == -1 )
{
v7 = optind;
*((_DWORD *)a1 + 6) = *(_DWORD *)(a2 + 8) - optind;
a1[2] = (const char *)(*(_QWORD *)a2 + 8LL * v7);
return 1;
}
if ( v4 != 108 )
break;
v6 = func0((_BYTE *)a1 + 33);
if ( !(_BYTE)v6 )
goto LABEL_12;
}
if ( v4 <= 108 )
break;
if ( v4 != 109 )
goto LABEL_16;
v6 = func0((_BYTE *)a1 + 32);
if ( !(_BYTE)v6 )
{
LABEL_12:
fprintf(stderr, "%s: '%s': invalid value.\n", *a1, optarg);
return v6;
}
}
if ( v4 != 102 )
break;
a1[1] = optarg;
}
if ( v4 == 104 )
{
*((_BYTE *)a1 + 34) = 1;
return 1;
}
LABEL_16:
if ( optopt )
fprintf(stderr, "%s: '%c': unknown option.\n", *a1, (unsigned int)optopt);
else
fprintf(stderr, "%s: %s: unknown option.\n", *a1, *(const char **)(*(_QWORD *)a2 + 8LL * optind - 8));
}
return v3;
}

original_code:
_Bool
args_parse (struct config *config, const struct args *args)
{
 int c;
 static const struct option opts[] = {
  { "help", 0, ((void *)0), 'h' },
  { "dictfile", 1, ((void *)0), 'f' },
  { "minlength", 1, ((void *)0), 'm' },
  { "haslength", 1, ((void *)0), 'l' },
  { ((void *)0) }
 };
 if (config == ((void *)0) || args == ((void *)0)) {
  return 0;
 }
 config->name = args->av[0];
 while ((c = getopt_long(args->ac, args->av, ":hf:m:l:", opts, ((void *)0))) != -1) {
  switch (c) {
  case 'h':
   config->print_help = 1;
   return 1;
  case 'f':
   config->dictfile = optarg;
   break;
  case 'm':
   if (!get_uint8(&config->minlength)) {
    fprintf(stderr, "%s: '%s': invalid value.\n",
            config->name, optarg);
    return 0;
   }
   break;
  case 'l':
   if (!get_uint8(&config->haslength)) {
    fprintf(stderr, "%s: '%s': invalid value.\n",
            config->name, optarg);
    return 0;
   }
   break;
  default:
   if (optopt != 0) {
    fprintf(stderr, "%s: '%c': unknown option.\n",
            config->name, optopt);
   } else {
    fprintf(stderr, "%s: %s: unknown option.\n",
            config->name, args->av[optind - 1]);
   }
   return 0;
  }
 }
 config->words.ac = args->ac - optind;
 config->words.av = args->av + optind;
 return 1;
}

canonical_original_code:
_Bool args_parse(struct config *config, struct args *args)
{
 int c;
 static struct option opts[] = {
  { "help", 0, ((void *)0), 'h' },
  { "dictfile", 1, ((void *)0), 'f' },
  { "minlength", 1, ((void *)0), 'm' },
  { "haslength", 1, ((void *)0), 'l' },
  { ((void *)0) }
 };
 if (config == ((void *)0) || args == ((void *)0)) {
  return 0;
 }
 config->name = args->av[0];
 while ((c = getopt_long(args->ac, args->av, ":hf:m:l:", opts, ((void *)0))) != -1) {
  switch (c) {
  case 'h':
   config->print_help = 1;
   return 1;
  case 'f':
   config->dictfile = optarg;
   break;
  case 'm':
   if (!get_uint8(&config->minlength)) {
    fprintf(stderr, "%s: '%s': invalid value.\n",
            config->name, optarg);
    return 0;
   }
   break;
  case 'l':
   if (!get_uint8(&config->haslength)) {
    fprintf(stderr, "%s: '%s': invalid value.\n",
            config->name, optarg);
    return 0;
   }
   break;
  default:
   if (optopt != 0) {
    fprintf(stderr, "%s: '%c': unknown option.\n",
            config->name, optopt);
   } else {
    fprintf(stderr, "%s: %s: unknown option.\n",
            config->name, args->av[optind - 1]);
   }
   return 0;
  }
 }
 config->words.ac = args->ac - optind;
 config->words.av = args->av + optind;
 return 1;
}

variable_types:
args
struct args *

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


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

v0 = (void **)word_head;
if ( word_head )
{
do
{
v1 = v0;
v0 = (void **)v0[3];
histogram_destroy(v1 + 2);
free(*v1);
free(v1);
}
while ( v0 );
}
}

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

v0 = (void **)word_head;
if ( word_head )
{
do
{
v1 = v0;
v0 = (void **)v0[3];
histogram_destroy(v1 + 2);
free(*v1);
free(v1);
}
while ( v0 );
}
}

original_code:
static void
words_destroy (void)
{
 struct word *w;
 struct word *t;
 for (w = word_head; w; w = t) {
  t = w->next;
  histogram_destroy(&w->hist);
  free(w->str);
  free(w);
 }
}

canonical_original_code:
static void words_destroy()
{
 struct word *w;
 struct word *t;
 for (w = word_head; w; w = t) {
  t = w->next;
  histogram_destroy(&w->hist);
  free(w->str);
  free(w);
 }
}

variable_types: struct word { char *str; unsigned long len; struct histogram { char *bins; int *freq; unsigned long len; int maxfreq; unsigned long ntotal; } *hist; struct word *next; } *

user_defined_types: struct word { char *str; unsigned long len; struct histogram *hist; struct word *next; }


usage

decompiled_code:
int __fastcall usage(const char **a1)
{
char *v1; // rbx
int result; // eax
__int64 v3[4]; // [rsp+0h] [rbp-38h] BYREF
char v4; // [rsp+20h] [rbp-18h] BYREF

v3[0] = (__int64)" -h|--help Show this help text";
v3[1] = (__int64)" -f|--dictfile <dictfile> Use this dictionary file (one word per line)";
v3[2] = (__int64)" -m|--minlength <length> All anagram words must be at least this long";
v3[3] = (__int64)" -l|--haslength <length> One anagram word must be at least this long\n";
fwrite("\nFind anagrams of the input phrases (as argument, else standard input)\n", 1uLL, 0x47uLL, stderr);
fprintf(stderr, "Usage: %s [-h] [-f dictfile] [-m minlength] [-l haslength] words...\n\n", *a1);
v1 = (char *)v3;
do
{
result = fprintf(stderr, "%s\n", *(const char **)v1);
v1 += 8;
}
while ( v1 != &v4 );
return result;
}

canonical_decompiled_code:
int __fastcall func2(const char **a1)
{
char *v1; // rbx
int result; // eax
__int64 v3[4]; // [rsp+0h] [rbp-38h] BYREF
char v4; // [rsp+20h] [rbp-18h] BYREF

v3[0] = (__int64)" -h|--help Show this help text";
v3[1] = (__int64)" -f|--dictfile <dictfile> Use this dictionary file (one word per line)";
v3[2] = (__int64)" -m|--minlength <length> All anagram words must be at least this long";
v3[3] = (__int64)" -l|--haslength <length> One anagram word must be at least this long\n";
fwrite("\nFind anagrams of the input phrases (as argument, else standard input)\n", 1uLL, 0x47uLL, stderr);
fprintf(stderr, "Usage: %s [-h] [-f dictfile] [-m minlength] [-l haslength] words...\n\n", *a1);
v1 = (char *)v3;
do
{
result = fprintf(stderr, "%s\n", *(const char **)v1);
v1 += 8;
}
while ( v1 != &v4 );
return result;
}

original_code:
static void
usage (const struct config *config)
{
 char *usage[] = {
  "  -h|--help                  Show this help text",
  "  -f|--dictfile <dictfile>   Use this dictionary file (one word per line)",
  "  -m|--minlength <length>    All anagram words must be at least this long",
  "  -l|--haslength <length>    One anagram word must be at least this long\n"
 };
 unsigned int i;
 fprintf(stderr, "\nFind anagrams of the input phrases (as argument, else standard input)\n");
 fprintf(stderr, "Usage: %s [-h] [-f dictfile] [-m minlength] [-l haslength] words...\n\n", config->name);
 for (i = 0; i < sizeof(usage) / sizeof(usage[0]); i++) {
  fprintf(stderr, "%s\n", usage[i]);
 }
}

canonical_original_code:
static void usage(struct config *config)
{
 char *usage[] = {
  "  -h|--help                  Show this help text",
  "  -f|--dictfile <dictfile>   Use this dictionary file (one word per line)",
  "  -m|--minlength <length>    All anagram words must be at least this long",
  "  -l|--haslength <length>    One anagram word must be at least this long\n"
 };
 unsigned int i;
 fprintf(stderr, "\nFind anagrams of the input phrases (as argument, else standard input)\n");
 fprintf(stderr, "Usage: %s [-h] [-f dictfile] [-m minlength] [-l haslength] words...\n\n", config->name);
 for (i = 0; i < sizeof(usage) / sizeof(usage[0]); i++) {
  fprintf(stderr, "%s\n", usage[i]);
 }
}

variable_types:
usage
char *[]

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall word_add(void *src, size_t n, __int64 a3)
{
__int64 result; // rax
__int64 v5; // rax
_QWORD *v6; // rbp
_BYTE *v7; // rax
_BYTE *v8; // r13
__int64 v9[6]; // [rsp+0h] [rbp-30h] BYREF

result = 0LL;
if ( !n || *(_QWORD *)(a3 + 32) < n )
return result;
v5 = histogram_create();
v9[0] = v5;
if ( !v5 )
return 0LL;
if ( *(_DWORD *)(v5 + 24) > *(_DWORD *)(a3 + 24)
|| !(unsigned __int8)histogram_fits(v5, a3)
|| (v6 = malloc(0x20uLL)) == 0LL )
{
LABEL_5:
histogram_destroy(v9);
return 0LL;
}
v7 = malloc(n + 1);
v8 = v7;
*v6 = v7;
if ( !v7 )
{
free(v6);
goto LABEL_5;
}
v6[1] = n;
v6[3] = 0LL;
memcpy(v7, src, n);
v8[n] = 0;
v6[2] = v9[0];
if ( word_head )
{
*(_QWORD *)(word_tail + 24) = v6;
word_tail = (__int64)v6;
}
else
{
word_tail = (__int64)v6;
word_head = v6;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *src, size_t n, __int64 a3)
{
__int64 result; // rax
__int64 v5; // rax
_QWORD *v6; // rbp
_BYTE *v7; // rax
_BYTE *v8; // r13
__int64 v9[6]; // [rsp+0h] [rbp-30h] BYREF

result = 0LL;
if ( !n || *(_QWORD *)(a3 + 32) < n )
return result;
v5 = histogram_create();
v9[0] = v5;
if ( !v5 )
return 0LL;
if ( *(_DWORD *)(v5 + 24) > *(_DWORD *)(a3 + 24)
|| !(unsigned __int8)histogram_fits(v5, a3)
|| (v6 = malloc(0x20uLL)) == 0LL )
{
LABEL_5:
histogram_destroy(v9);
return 0LL;
}
v7 = malloc(n + 1);
v8 = v7;
*v6 = v7;
if ( !v7 )
{
free(v6);
goto LABEL_5;
}
v6[1] = n;
v6[3] = 0LL;
memcpy(v7, src, n);
v8[n] = 0;
v6[2] = v9[0];
if ( word_head )
{
*(_QWORD *)(word_tail + 24) = v6;
word_tail = (__int64)v6;
}
else
{
word_tail = (__int64)v6;
word_head = v6;
}
return 1LL;
}

original_code:
static int
word_add (const char *const word, const size_t len, const struct histogram *const inhist)
{
 struct word *w;
 struct histogram *h;
 if (len == 0) {
  goto err_0;
 }
 if (len > inhist->ntotal) {
  goto err_0;
 }
 if ((h = histogram_create(word, len)) == ((void *)0)) {
  goto err_0;
 }
 if (h->maxfreq > inhist->maxfreq) {
  goto err_1;
 }
 if (!histogram_fits(h, inhist)) {
  goto err_1;
 }
 if ((w = malloc(sizeof(*w))) == ((void *)0)) {
  goto err_1;
 }
 if ((w->str = malloc(len + 1)) == ((void *)0)) {
  goto err_2;
 }
 w->len = len;
 w->next = ((void *)0);
 memcpy(w->str, word, len);
 w->str[len] = '\0';
 w->hist = h;
 if (word_head == ((void *)0)) {
  word_head = word_tail = w;
 }
 else {
  word_tail->next = w;
  word_tail = w;
 }
 return 1;
err_2: free(w);
err_1: histogram_destroy(&h);
err_0: return 0;
}

canonical_original_code:
static int word_add(char *word, unsigned long len, struct histogram *inhist)
{
 struct word *w;
 struct histogram *h;
 if (len == 0) {
  goto err_0;
 }
 if (len > inhist->ntotal) {
  goto err_0;
 }
 if ((h = histogram_create(word, len)) == ((void *)0)) {
  goto err_0;
 }
 if (h->maxfreq > inhist->maxfreq) {
  goto err_1;
 }
 if (!histogram_fits(h, inhist)) {
  goto err_1;
 }
 if ((w = malloc(sizeof(*w))) == ((void *)0)) {
  goto err_1;
 }
 if ((w->str = malloc(len + 1)) == ((void *)0)) {
  goto err_2;
 }
 w->len = len;
 w->next = ((void *)0);
 memcpy(w->str, word, len);
 w->str[len] = '\0';
 w->hist = h;
 if (word_head == ((void *)0)) {
  word_head = word_tail = w;
 }
 else {
  word_tail->next = w;
  word_tail = w;
 }
 return 1;
err_2: free(w);
err_1: histogram_destroy(&h);
err_0: return 0;
}

variable_types:
word
char *

variable_types:
len
unsigned long

variable_types: struct histogram { char *bins; int *freq; unsigned long len; int maxfreq; unsigned long ntotal; } *