main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
int v4; // ebx
const char *v5; // r12
int v6; // er14
char **v7; // rbp
unsigned int v8; // er13
const char **v9; // r12
unsigned int v10; // er14
int v11; // ebx
int v12; // er15
const char *v13; // rax
char *v14; // rbx
__int64 v15; // r15
const char *v16; // rax
int v17; // [rsp+Ch] [rbp-21Ch]
char s[64]; // [rsp+10h] [rbp-218h] BYREF
__int64 v19[26]; // [rsp+50h] [rbp-1D8h] BYREF
__int64 v20[33]; // [rsp+120h] [rbp-108h] BYREF

memset(v20, 0, 0xD0uLL);
memset(v19, 0, sizeof(v19));
v3 = (unsigned int)read_file((__int64)v20, (__int64)v19);
printf("%i words left\n", v3);
while ( 1 )
{
while ( 1 )
{
printf("Next word: ");
fgets(s, 64, stdin);
chomp(s);
if ( s[0] )
break;
print_array(v20);
putchar(10);
}
v4 = index_first(s);
v5 = find_word_in_list(v20[v4], s);
if ( v5 )
break;
puts("Word not in index");
}
v6 = index_last(s);
v7 = (char **)create_word((__int64)v5);
v20[v4] = delete_word(v20[v4], v5);
v19[v6] = delete_word(v19[v6], v5);
v8 = v3 - 1;
v9 = (const char **)v7;
v10 = 1;
while ( 1 )
{
while ( 1 )
{
v11 = index_first(*v7);
v12 = index_last(*v9);
printf("%i words left\n", v8);
printf("Current chain of %i words: %s ... %s\n", v10, *v7, *v9);
printf("Next word: ");
fgets(s, 64, stdin);
chomp(s);
if ( s[0] )
break;
print_list(v19[v11]);
print_list(v20[v12]);
putchar(10);
}
v13 = find_word_in_list(v19[v11], s);
v14 = (char *)v13;
if ( v13 )
break;
v16 = find_word_in_list(v20[v12], s);
v14 = (char *)v16;
if ( v16 )
{
v9 = (const char **)append_word((__int64)v9, (__int64)v16);
LABEL_11:
v17 = index_first(v14);
v15 = (int)index_last(v14);
v19[v15] = delete_word(v19[v15], v14);
v20[v17] = delete_word(v20[v17], v14);
--v8;
++v10;
}
else
{
puts("Word not in index or not matching or already used");
}
}
v7 = (char **)prefix_word((__int64)v7, (__int64)v13);
goto LABEL_11;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
int v4; // ebx
const char *v5; // r12
int v6; // er14
char **v7; // rbp
unsigned int v8; // er13
const char **v9; // r12
unsigned int v10; // er14
int v11; // ebx
int v12; // er15
const char *v13; // rax
char *v14; // rbx
__int64 v15; // r15
const char *v16; // rax
int v17; // [rsp+Ch] [rbp-21Ch]
char s[64]; // [rsp+10h] [rbp-218h] BYREF
__int64 v19[26]; // [rsp+50h] [rbp-1D8h] BYREF
__int64 v20[33]; // [rsp+120h] [rbp-108h] BYREF

memset(v20, 0, 0xD0uLL);
memset(v19, 0, sizeof(v19));
v3 = (unsigned int)func12((__int64)v20, (__int64)v19);
printf("%i words left\n", v3);
while ( 1 )
{
while ( 1 )
{
printf("Next word: ");
fgets(s, 64, stdin);
func7(s);
if ( s[0] )
break;
func15(v20);
putchar(10);
}
v4 = func8(s);
v5 = func13(v20[v4], s);
if ( v5 )
break;
puts("Word not in index");
}
v6 = func9(s);
v7 = (char **)create_word((__int64)v5);
v20[v4] = func10(v20[v4], v5);
v19[v6] = func10(v19[v6], v5);
v8 = v3 - 1;
v9 = (const char **)v7;
v10 = 1;
while ( 1 )
{
while ( 1 )
{
v11 = func8(*v7);
v12 = func9(*v9);
printf("%i words left\n", v8);
printf("Current chain of %i words: %s ... %s\n", v10, *v7, *v9);
printf("Next word: ");
fgets(s, 64, stdin);
func7(s);
if ( s[0] )
break;
func14(v19[v11]);
func14(v20[v12]);
putchar(10);
}
v13 = func13(v19[v11], s);
v14 = (char *)v13;
if ( v13 )
break;
v16 = func13(v20[v12], s);
v14 = (char *)v16;
if ( v16 )
{
v9 = (const char **)append_word((__int64)v9, (__int64)v16);
LABEL_11:
v17 = func8(v14);
v15 = (int)func9(v14);
v19[v15] = func10(v19[v15], v14);
v20[v17] = func10(v20[v17], v14);
--v8;
++v10;
}
else
{
puts("Word not in index or not matching or already used");
}
}
v7 = (char **)prefix_word((__int64)v7, (__int64)v13);
goto LABEL_11;
}

original_code:
int main() {
   word* chainbegin = ((void *)0);
   word* chainend = ((void *)0);
   word* first[26] = {0};
   word* last[26] = {0};
   int words_left = read_file(first, last);
   printf("%i words left\n", words_left);
   char input[64];
   while (1) {
      int ifi, ila, len_chain = 0;
      char* ps;
      while (1) {
         printf("Next word: ");
         fgets(input, 64, stdin);
         chomp(input);
         if (strlen(input) == 0) {
            print_array(first);
            printf("\n");
            continue;
         } else {
            ifi = index_first(input);
            ila = index_last(input);
            ps = find_word_in_list(first[ifi], input);
            if (ps != ((void *)0)) {
               chainbegin = create_word(ps);
               len_chain++;
               chainend = chainbegin;
               first[ifi] = delete_word(first[ifi], ps);
               last[ila] = delete_word(last[ila], ps);
               words_left--;
               break;
            } else {
               printf("Word not in index\n");
               continue;
            }
         }
      }
      while (1) {
         ifi = index_first(chainbegin->val);
         ila = index_last(chainend->val);
         printf("%i words left\n", words_left);
         printf("Current chain of %i words: %s ... %s\n",len_chain ,chainbegin->val, chainend->val);
         printf("Next word: ");
         fgets(input, 64, stdin);
         chomp(input);
         if (strlen(input) == 0) {
            print_list(last[ifi]);
            print_list(first[ila]);
            printf("\n");
            continue;
         } else {
            ps = find_word_in_list(last[ifi], input);
            if (ps != ((void *)0)) {
               chainbegin = prefix_word(chainbegin, ps);
            } else {
               ps = find_word_in_list(first[ila], input);
               if (ps != ((void *)0)) {
                  chainend = append_word(chainend, ps);
               } else {
                  printf("Word not in index or not matching or already used\n");
                  continue;
               }
            }
            ifi = index_first(ps);
            ila = index_last(ps);
            last[ila] = delete_word(last[ila], ps);
            first[ifi] = delete_word(first[ifi], ps);
            words_left--;
            len_chain++;
         }
      }
   }
   return 0;
}

canonical_original_code:
int main() {
   struct word *chainbegin = ((void *)0);
   struct word *chainend = ((void *)0);
   struct word *first[26] = {0};
   struct word *last[26] = {0};
   int words_left = read_file(first, last);
   printf("%i words left\n", words_left);
   char input[64];
   while (1) {
      int ifi; int ila; int len_chain = 0;
      char *ps;
      while (1) {
         printf("Next word: ");
         fgets(input, 64, stdin);
         chomp(input);
         if (strlen(input) == 0) {
            print_array(first);
            printf("\n");
            continue;
         } else {
            ifi = index_first(input);
            ila = index_last(input);
            ps = find_word_in_list(first[ifi], input);
            if (ps != ((void *)0)) {
               chainbegin = create_word(ps);
               len_chain++;
               chainend = chainbegin;
               first[ifi] = delete_word(first[ifi], ps);
               last[ila] = delete_word(last[ila], ps);
               words_left--;
               break;
            } else {
               printf("Word not in index\n");
               continue;
            }
         }
      }
      while (1) {
         ifi = index_first(chainbegin->val);
         ila = index_last(chainend->val);
         printf("%i words left\n", words_left);
         printf("Current chain of %i words: %s ... %s\n",len_chain ,chainbegin->val, chainend->val);
         printf("Next word: ");
         fgets(input, 64, stdin);
         chomp(input);
         if (strlen(input) == 0) {
            print_list(last[ifi]);
            print_list(first[ila]);
            printf("\n");
            continue;
         } else {
            ps = find_word_in_list(last[ifi], input);
            if (ps != ((void *)0)) {
               chainbegin = prefix_word(chainbegin, ps);
            } else {
               ps = find_word_in_list(first[ila], input);
               if (ps != ((void *)0)) {
                  chainend = append_word(chainend, ps);
               } else {
                  printf("Word not in index or not matching or already used\n");
                  continue;
               }
            }
            ifi = index_first(ps);
            ila = index_last(ps);
            last[ila] = delete_word(last[ila], ps);
            first[ifi] = delete_word(first[ifi], ps);
            words_left--;
            len_chain++;
         }
      }
   }
   return 0;
}

variable_types: struct word { char *val; struct word *next; } *[26]

variable_types:
input
char [64]

variable_types: ifi int

variable_types: ila int

variable_types:
ps
char *


repo: grtkachenko/OS

decompiled_code:
void __fastcall free_arr_of_arr(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 > 0 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 > 0 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

original_code:
void free_arr_of_arr(char ** arr, int num_files) {
    int i;
    for (i = 0; i < num_files; i++) {
        free(arr[i]);
    }
    free(arr);
}

canonical_original_code:
void free_arr_of_arr(char **arr, int num_files) {
    int i;
    for (i = 0; i < num_files; i++) {
        free(arr[i]);
    }
    free(arr);
}

variable_types:
arr
char **

variable_types: i int

return_type: void


original_code:
char ** get_command(char * buffer, int len, int * num) {
    char ** ans = (char **) malloc(MAX_SIZE);
    if (ans == ((void *)0)) {
        exit(5);
    }
    int i = 0, left = 0;
    (*num) = 0;
    for (i = 0; i < len; i++) {
       if (buffer[i] == ' ') {
           char * cur = (char *) malloc(i - left + 1);
           if (cur == ((void *)0)) {
               free(ans);
               exit(6);
           }
           memcpy(cur, buffer + left, i - left);
           (*num)++;
           ans[*num] = cur;
           cur[i - left] = 0;
           left = i + 1;
        }
    }
    char * cur = (char *) malloc(i - left + 1);
    if (cur == ((void *)0)) {
        free_arr_of_arr(ans, *num);
        exit(7);
    }
    memcpy(cur, buffer + left, i - left);
    (*num)++;
    ans[*num] = cur;
    cur[i - left] = 0;
    return ans;
}

canonical_original_code:
char **get_command(char *buffer, int len, int *num) {
    char **ans = (char **) malloc(MAX_SIZE);
    if (ans == ((void *)0)) {
        exit(5);
    }
    int i = 0; int left = 0;
    (*num) = 0;
    for (i = 0; i < len; i++) {
       if (buffer[i] == ' ') {
           char *cur = (char *) malloc(i - left + 1);
           if (cur == ((void *)0)) {
               free(ans);
               exit(6);
           }
           memcpy(cur, buffer + left, i - left);
           (*num)++;
           ans[*num] = cur;
           cur[i - left] = 0;
           left = i + 1;
        }
    }
    char *cur = (char *) malloc(i - left + 1);
    if (cur == ((void *)0)) {
        free_arr_of_arr(ans, *num);
        exit(7);
    }
    memcpy(cur, buffer + left, i - left);
    (*num)++;
    ans[*num] = cur;
    cur[i - left] = 0;
    return ans;
}

variable_types:
buffer
char *

variable_types: len int

variable_types:
num
int *

variable_types:
ans
char **

variable_types: left int

variable_types:
cur
char *

return_type: char **


canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
char *v5; // r13
int v6; // er14
int v7; // ebx
int i; // eax
_BYTE *v9; // rax
_BYTE *v10; // r12
const char **v11; // r12
__pid_t v12; // ebx
_BYTE *v13; // rax
_BYTE *v14; // r14
int v15; // eax
int v16; // er12
int v17; // ebx
int v18; // [rsp+4h] [rbp-64h]
_QWORD *ptr; // [rsp+8h] [rbp-60h]
int fd; // [rsp+14h] [rbp-54h]
int pipedes[2]; // [rsp+24h] [rbp-44h] BYREF
int v22[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v3 = 1;
if ( argc > 2 )
{
v5 = (char *)malloc(0x1000uLL);
if ( v5 )
{
ptr = malloc(0xFFFuLL);
if ( ptr )
{
fd = open(argv[1], 0);
v6 = 0;
v7 = 0;
v18 = 0;
for ( i = read(fd, v5, 0x1000uLL); ; i = read(fd, &v5[v7], 4096 - v7) )
{
v3 = i;
if ( !i )
break;
v16 = v7 + i;
while ( v16 > v7 )
{
if ( v5[v7] == 10 )
{
v13 = malloc(v7 + 1);
v14 = v13;
if ( !v13 )
goto LABEL_15;
memcpy(v13, v5, v7);
v15 = v18++;
ptr[v15] = v14;
v14[v7] = 0;
v16 -= v7 + 1;
memmove(v5, &v5[v7 + 1], v16);
v6 = v7;
v7 = 0;
}
else
{
++v7;
}
}
if ( v7 == 4096 )
{
free(v5);
free_arr_of_arr(ptr, v18);
return 4;
}
}
if ( (unsigned int)(v7 - 1) <= 0xFFE )
{
v9 = malloc(v7 + 1);
v10 = v9;
if ( !v9 )
{
LABEL_15:
free(v5);
free_arr_of_arr(ptr, v18);
return 8;
}
memcpy(v9, v5, v7);
v10[v7] = 0;
ptr[v18] = v10;
v6 = v7;
++v18;
}
close(fd);
v11 = (const char **)get_command((__int64)v5, v6, v22);
pipe(pipedes);
v12 = fork();
if ( v12 )
{
wait(0LL);
dup2(pipedes[0], 0);
close(pipedes[0]);
close(pipedes[1]);
execvp(*v11, (char *const *)v11);
}
else
{
close(pipedes[0]);
if ( v18 > 1 )
{
do
{
if ( !fork() )
{
v17 = open((const char *)ptr[v12], 0);
dup2(v17, 0);
dup2(pipedes[1], 1);
close(v17);
close(pipedes[1]);
execvp(argv[2], (char *const *)argv + 2);
exit(0);
}
wait(0LL);
++v12;
}
while ( v12 != v18 - 1 );
}
close(pipedes[1]);
}
free(v5);
free_arr_of_arr(ptr, v18);
free_arr_of_arr(v11, v22[0]);
}
else
{
free(v5);
v3 = 3;
}
}
else
{
v3 = 2;
}
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
char *v5; // r13
int v6; // er14
int v7; // ebx
int i; // eax
_BYTE *v9; // rax
_BYTE *v10; // r12
const char **v11; // r12
__pid_t v12; // ebx
_BYTE *v13; // rax
_BYTE *v14; // r14
int v15; // eax
int v16; // er12
int v17; // ebx
int v18; // [rsp+4h] [rbp-64h]
_QWORD *ptr; // [rsp+8h] [rbp-60h]
int fd; // [rsp+14h] [rbp-54h]
int pipedes[2]; // [rsp+24h] [rbp-44h] BYREF
int v22[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v3 = 1;
if ( argc > 2 )
{
v5 = (char *)malloc(0x1000uLL);
if ( v5 )
{
ptr = malloc(0xFFFuLL);
if ( ptr )
{
fd = open(argv[1], 0);
v6 = 0;
v7 = 0;
v18 = 0;
for ( i = read(fd, v5, 0x1000uLL); ; i = read(fd, &v5[v7], 4096 - v7) )
{
v3 = i;
if ( !i )
break;
v16 = v7 + i;
while ( v16 > v7 )
{
if ( v5[v7] == 10 )
{
v13 = malloc(v7 + 1);
v14 = v13;
if ( !v13 )
goto LABEL_15;
memcpy(v13, v5, v7);
v15 = v18++;
ptr[v15] = v14;
v14[v7] = 0;
v16 -= v7 + 1;
memmove(v5, &v5[v7 + 1], v16);
v6 = v7;
v7 = 0;
}
else
{
++v7;
}
}
if ( v7 == 4096 )
{
free(v5);
func7(ptr, v18);
return 4;
}
}
if ( (unsigned int)(v7 - 1) <= 0xFFE )
{
v9 = malloc(v7 + 1);
v10 = v9;
if ( !v9 )
{
LABEL_15:
free(v5);
func7(ptr, v18);
return 8;
}
memcpy(v9, v5, v7);
v10[v7] = 0;
ptr[v18] = v10;
v6 = v7;
++v18;
}
close(fd);
v11 = (const char **)func8((__int64)v5, v6, v22);
pipe(pipedes);
v12 = fork();
if ( v12 )
{
wait(0LL);
dup2(pipedes[0], 0);
close(pipedes[0]);
close(pipedes[1]);
execvp(*v11, (char *const *)v11);
}
else
{
close(pipedes[0]);
if ( v18 > 1 )
{
do
{
if ( !fork() )
{
v17 = open((const char *)ptr[v12], 0);
dup2(v17, 0);
dup2(pipedes[1], 1);
close(v17);
close(pipedes[1]);
execvp(argv[2], (char *const *)argv + 2);
exit(0);
}
wait(0LL);
++v12;
}
while ( v12 != v18 - 1 );
}
close(pipedes[1]);
}
free(v5);
func7(ptr, v18);
func7(v11, v22[0]);
}
else
{
free(v5);
v3 = 3;
}
}
else
{
v3 = 2;
}
}
return v3;
}

original_code:
int main(int argc, char* argv[]) {
    if (argc < 3) {
        return 1;
    }
    char * buffer = (char *) malloc(MAX_SIZE + 1);
    if (buffer == ((void *)0)) {
        return 2;
    }
    char ** files = (char **) malloc(MAX_SIZE);
    if (files == ((void *)0)) {
        free(buffer);
        return 3;
    }
    int num_files = 0, len = 0, last_size = 0;
    int in_file = open(argv[1], 00);
    while (1) {
        int read_res = read(in_file, buffer + len, MAX_SIZE + 1 - len);
        if (read_res == 0) {
            if (len > 0 && len <= MAX_SIZE) {
                last_size = len;
                char * cur = (char *) malloc(len + 1);
                if (cur == ((void *)0)) {
                    free(buffer);
                    free_arr_of_arr(files, num_files);
                    return 8;
                }
                memcpy(cur, buffer, len);
                cur[len] = 0;
                files[num_files++] = cur;
            }
            break;
        }
        int left = len;
        int right = read_res + len;
        while (left < right) {
            if (buffer[left] == '\n') {
                last_size = left;
                char * cur = (char *) malloc(left + 1);
                if (cur == ((void *)0)) {
                    free(buffer);
                    free_arr_of_arr(files, num_files);
                    return 8;
                }
                memcpy(cur, buffer, left);
                files[num_files++] = cur;
                cur[left] = 0;
                left++;
                memmove(buffer, buffer + left, right - left);
                right = right - left;
                left = 0;
            } else {
                left++;
            }
        }
        if (left == MAX_SIZE + 1) {
            free(buffer);
            free_arr_of_arr(files, num_files);
            return 4;
        } else {
            len = left;
        }
    }
    close(in_file);
    int num_of_commands;
    char ** command = get_command(buffer, last_size, &num_of_commands);
    int fds[2];
    pipe(fds);
    if (!fork()) {
        close(fds[0]);
        int it;
        for (it = 0; it < num_files - 1; it++) {
            if (!fork()) {
                int fdin = open(files[it], 00);
                dup2(fdin, 0);
                dup2(fds[1], 1);
                close(fdin);
                close(fds[1]);
                execvp(argv[2], &argv[2]);
                exit(0);
            }
            wait(((void *)0));
        }
        close(fds[1]);
    } else {
        wait(((void *)0));
        dup2(fds[0], 0);
        close(fds[0]);
        close(fds[1]);
        execvp(command[0], command);
    }
    free(buffer);
    free_arr_of_arr(files, num_files);
    free_arr_of_arr(command, num_of_commands);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    if (argc < 3) {
        return 1;
    }
    char *buffer = (char *) malloc(MAX_SIZE + 1);
    if (buffer == ((void *)0)) {
        return 2;
    }
    char **files = (char **) malloc(MAX_SIZE);
    if (files == ((void *)0)) {
        free(buffer);
        return 3;
    }
    int num_files = 0; int len = 0; int last_size = 0;
    int in_file = open(argv[1], 00);
    while (1) {
        int read_res = read(in_file, buffer + len, MAX_SIZE + 1 - len);
        if (read_res == 0) {
            if (len > 0 && len <= MAX_SIZE) {
                last_size = len;
                char *cur = (char *) malloc(len + 1);
                if (cur == ((void *)0)) {
                    free(buffer);
                    free_arr_of_arr(files, num_files);
                    return 8;
                }
                memcpy(cur, buffer, len);
                cur[len] = 0;
                files[num_files++] = cur;
            }
            break;
        }
        int left = len;
        int right = read_res + len;
        while (left < right) {
            if (buffer[left] == '\n') {
                last_size = left;
                char *cur = (char *) malloc(left + 1);
                if (cur == ((void *)0)) {
                    free(buffer);
                    free_arr_of_arr(files, num_files);
                    return 8;
                }
                memcpy(cur, buffer, left);
                files[num_files++] = cur;
                cur[left] = 0;
                left++;
                memmove(buffer, buffer + left, right - left);
                right = right - left;
                left = 0;
            } else {
                left++;
            }
        }
        if (left == MAX_SIZE + 1) {
            free(buffer);
            free_arr_of_arr(files, num_files);
            return 4;
        } else {
            len = left;
        }
    }
    close(in_file);
    int num_of_commands;
    char **command = get_command(buffer, last_size, &num_of_commands);
    int fds[2];
    pipe(fds);
    if (!fork()) {
        close(fds[0]);
        int it;
        for (it = 0; it < num_files - 1; it++) {
            if (!fork()) {
                int fdin = open(files[it], 00);
                dup2(fdin, 0);
                dup2(fds[1], 1);
                close(fdin);
                close(fds[1]);
                execvp(argv[2], &argv[2]);
                exit(0);
            }
            wait(((void *)0));
        }
        close(fds[1]);
    } else {
        wait(((void *)0));
        dup2(fds[0], 0);
        close(fds[0]);
        close(fds[1]);
        execvp(command[0], command);
    }
    free(buffer);
    free_arr_of_arr(files, num_files);
    free_arr_of_arr(command, num_of_commands);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
files
char **

variable_types: right int

variable_types:
command
char **

variable_types:
fds
int [2]

variable_types: it int

variable_types: fdin int

return_type: int


decompiled_code:
void __fastcall print_ans(__int64 a1, int a2)
{
int v2; // ebx
int v3; // eax

if ( a2 > 0 )
{
v2 = 0;
do
{
v3 = write(1, (const void *)(a1 + v2), a2 - v2);
if ( v3 > 0 )
v2 += v3;
}
while ( v2 < a2 );
}
}

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

if ( a2 > 0 )
{
v2 = 0;
do
{
v3 = write(1, (const void *)(a1 + v2), a2 - v2);
if ( v3 > 0 )
v2 += v3;
}
while ( v2 < a2 );
}
}

original_code:
void print_ans(char * buffer, int count) {
    int num_ok = 0;
    while (num_ok < count) {
        int cur = write(OUT, buffer + num_ok, count - num_ok);
        if (cur > 0) {
            num_ok += cur;
        }
    }
}

canonical_original_code:
void print_ans(char *buffer, int count) {
    int num_ok = 0;
    while (num_ok < count) {
        int cur = write(OUT, buffer + num_ok, count - num_ok);
        if (cur > 0) {
            num_ok += cur;
        }
    }
}

variable_types: count int

variable_types: cur int


println

decompiled_code:
ssize_t println()
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = 10;
return write(1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t func8()
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = 10;
return write(1, buf, 1uLL);
}

original_code:
void println() {
    char endl = '\n';
    write(OUT, &endl, 1);
}

variable_types: endl char


decompiled_code:
_BOOL8 __fastcall is_ok_status(__int16 a1)
{
return (a1 & 0x7F | HIBYTE(a1)) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(__int16 a1)
{
return (a1 & 0x7F | HIBYTE(a1)) == 0;
}

original_code:
int is_ok_status(int status) {
    return (((status) & 0x7f) == 0) && (((status) & 0xff00) >> 8) == 0;
}

variable_types: status int


decompiled_code:
void __fastcall check_ans(char **argv, int a2, char *a3, int a4)
{
__WAIT_STATUS stat_loc; // [rsp+4h] [rbp-34h] BYREF
int v7; // [rsp+Ch] [rbp-2Ch]

pipe((int *)&stat_loc.__iptr + 1);
if ( !fork() )
{
argv[a2] = a3;
dup2(v7, 1);
execvp(*argv, argv);
exit(0);
}
LODWORD(stat_loc.__uptr) = 0;
wait((__WAIT_STATUS)&stat_loc);
if ( is_ok_status((__int16)stat_loc.__uptr) )
{
print_ans((__int64)a3, a4);
if ( a3[a4 - 1] != 10 )
println();
}
}

canonical_decompiled_code:
void __fastcall func10(char **argv, int a2, char *a3, int a4)
{
__WAIT_STATUS stat_loc; // [rsp+4h] [rbp-34h] BYREF
int v7; // [rsp+Ch] [rbp-2Ch]

pipe((int *)&stat_loc.__iptr + 1);
if ( !fork() )
{
argv[a2] = a3;
dup2(v7, 1);
execvp(*argv, argv);
exit(0);
}
LODWORD(stat_loc.__uptr) = 0;
wait((__WAIT_STATUS)&stat_loc);
if ( func9((__int16)stat_loc.__uptr) )
{
func7((__int64)a3, a4);
if ( a3[a4 - 1] != 10 )
func8();
}
}

original_code:
void check_ans(char ** arr_argv, int num_in_arr, char * buffer, int count) {
    int pipefd[2];
    pipe(pipefd);
    if (!fork()) {
        arr_argv[num_in_arr] = buffer;
        dup2(pipefd[1], 1);
        execvp(arr_argv[0], arr_argv);
        exit(0);
    }
    int status = 0;
    wait(&status);
    if (is_ok_status(status)) {
        print_ans(buffer, count);
        if (buffer[count - 1] != '\n') {
            println();
        }
    }
}

canonical_original_code:
void check_ans(char **arr_argv, int num_in_arr, char *buffer, int count) {
    int pipefd[2];
    pipe(pipefd);
    if (!fork()) {
        arr_argv[num_in_arr] = buffer;
        dup2(pipefd[1], 1);
        execvp(arr_argv[0], arr_argv);
        exit(0);
    }
    int status = 0;
    wait(&status);
    if (is_ok_status(status)) {
        print_ans(buffer, count);
        if (buffer[count - 1] != '\n') {
            println();
        }
    }
}

variable_types:
buffer
char *

variable_types:
pipefd
int [2]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char v4; // r13
int v5; // eax
char *v6; // r12
_QWORD *v7; // r15
int v8; // er10
__int64 v9; // r9
const char **v10; // r8
__int64 i; // rax
int v12; // ebp
int v13; // ebx
int v15; // ebx
int v16; // ebp
int v17; // eax
int v18; // [rsp+8h] [rbp-40h]
int v19; // [rsp+Ch] [rbp-3Ch]

v4 = 10;
v19 = 4095;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "nzb:");
if ( v5 == -1 )
break;
switch ( v5 )
{
case 'n':
v4 = 10;
break;
case 'z':
v4 = 0;
break;
case 'b':
v19 = atoi(optarg);
break;
}
}
v18 = v19 + 1;
v6 = (char *)malloc(v19 + 1);
if ( !v6 )
return 1;
v7 = malloc(argc - _bss_start + 1);
if ( v7 )
{
v8 = _bss_start;
if ( argc > _bss_start )
{
v9 = (unsigned int)(~_bss_start + argc);
v10 = &argv[_bss_start];
for ( i = 0LL; ; ++i )
{
if ( !strcmp(v10[i], "{}") )
v3 = i;
v7[i] = v10[i];
if ( i == v9 )
break;
}
}
v7[argc - v8] = 0LL;
v13 = 0;
while ( 1 )
{
v17 = read(0, &v6[v13], v18 - v13);
v12 = v17;
if ( v17 < 0 )
{
free(v6);
free(v7);
return 3;
}
if ( !v17 )
break;
v16 = v13 + v17;
while ( v16 > v13 )
{
if ( v6[v13] == v4 )
{
v15 = v13 + 1;
check_ans((char **)v7, v3, v6, v15);
v16 -= v15;
memmove(v6, &v6[v15], v16);
v13 = 0;
}
else
{
++v13;
}
}
if ( v18 == v13 )
{
free(v6);
free(v7);
return 5;
}
}
if ( v13 <= 0 || v19 == v13 )
{
free(v6);
free(v7);
v12 = 4;
}
else
{
if ( v19 > v13 )
check_ans((char **)v7, v3, v6, v13);
free(v6);
free(v7);
}
}
else
{
free(v6);
v12 = 2;
}
return v12;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char v4; // r13
int v5; // eax
char *v6; // r12
_QWORD *v7; // r15
int v8; // er10
__int64 v9; // r9
const char **v10; // r8
__int64 i; // rax
int v12; // ebp
int v13; // ebx
int v15; // ebx
int v16; // ebp
int v17; // eax
int v18; // [rsp+8h] [rbp-40h]
int v19; // [rsp+Ch] [rbp-3Ch]

v4 = 10;
v19 = 4095;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "nzb:");
if ( v5 == -1 )
break;
switch ( v5 )
{
case 'n':
v4 = 10;
break;
case 'z':
v4 = 0;
break;
case 'b':
v19 = atoi(optarg);
break;
}
}
v18 = v19 + 1;
v6 = (char *)malloc(v19 + 1);
if ( !v6 )
return 1;
v7 = malloc(argc - _bss_start + 1);
if ( v7 )
{
v8 = _bss_start;
if ( argc > _bss_start )
{
v9 = (unsigned int)(~_bss_start + argc);
v10 = &argv[_bss_start];
for ( i = 0LL; ; ++i )
{
if ( !strcmp(v10[i], "{}") )
v3 = i;
v7[i] = v10[i];
if ( i == v9 )
break;
}
}
v7[argc - v8] = 0LL;
v13 = 0;
while ( 1 )
{
v17 = read(0, &v6[v13], v18 - v13);
v12 = v17;
if ( v17 < 0 )
{
free(v6);
free(v7);
return 3;
}
if ( !v17 )
break;
v16 = v13 + v17;
while ( v16 > v13 )
{
if ( v6[v13] == v4 )
{
v15 = v13 + 1;
func10((char **)v7, v3, v6, v15);
v16 -= v15;
memmove(v6, &v6[v15], v16);
v13 = 0;
}
else
{
++v13;
}
}
if ( v18 == v13 )
{
free(v6);
free(v7);
return 5;
}
}
if ( v13 <= 0 || v19 == v13 )
{
free(v6);
free(v7);
v12 = 4;
}
else
{
if ( v19 > v13 )
func10((char **)v7, v3, v6, v13);
free(v6);
free(v7);
}
}
else
{
free(v6);
v12 = 2;
}
return v12;
}

original_code:
int main(int argc, char ** argv) {
    int num = 4095, len = 0, c, num_arr_argv;
    char delim = '\n';
    while ((c = getopt(argc, argv, "nzb:")) != -1) {
        if (c == 'n') {
            delim = '\n';
        }
        if (c == 'z') {
            delim = '\0';
        }
        if (c == 'b') {
            num = atoi(optarg);
        }
    }
    char * buffer = (char *) malloc(num + 1);
    if (buffer == ((void *)0)) {
        return 1;
    }
    char ** arr_argv = (char **) malloc(argc - optind + 1);
    if (arr_argv == ((void *)0)) {
        free(buffer);
        return 2;
    }
    int i;
    for (i = optind; i < argc; i++) {
        if (strcmp(argv[i], "{}") == 0) {
            num_arr_argv = i - optind;
        }
        arr_argv[i - optind] = argv[i];
    }
    arr_argv[argc - optind] = 0;
    while (1) {
        int read_res = read(IN, buffer + len, num + 1 - len);
        if (read_res < 0) {
            free(buffer);
            free(arr_argv);
            return 3;
        }
        if (read_res == 0) {
            if (len <= 0 || len == num) {
                free(buffer);
                free(arr_argv);
                return 4;
            }
            if (len > 0 && len < num) {
                check_ans(arr_argv, num_arr_argv, buffer, len);
            }
            break;
        }
        int left = len;
        int right = read_res + len;
        while (left < right) {
            if (buffer[left] == delim) {
                left++;
                check_ans(arr_argv, num_arr_argv, buffer, left);
                memmove(buffer, buffer + left, right - left);
                right = right - left;
                left = 0;
            } else {
                left++;
            }
        }
        if (left == num + 1) {
            free(buffer);
            free(arr_argv);
            return 5;
        } else {
            len = left;
        }
    }
    free(buffer);
    free(arr_argv);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    int num = 4095; int len = 0; int c; int num_arr_argv;
    char delim = '\n';
    while ((c = getopt(argc, argv, "nzb:")) != -1) {
        if (c == 'n') {
            delim = '\n';
        }
        if (c == 'z') {
            delim = '\0';
        }
        if (c == 'b') {
            num = atoi(optarg);
        }
    }
    char *buffer = (char *) malloc(num + 1);
    if (buffer == ((void *)0)) {
        return 1;
    }
    char **arr_argv = (char **) malloc(argc - optind + 1);
    if (arr_argv == ((void *)0)) {
        free(buffer);
        return 2;
    }
    int i;
    for (i = optind; i < argc; i++) {
        if (strcmp(argv[i], "{}") == 0) {
            num_arr_argv = i - optind;
        }
        arr_argv[i - optind] = argv[i];
    }
    arr_argv[argc - optind] = 0;
    while (1) {
        int read_res = read(IN, buffer + len, num + 1 - len);
        if (read_res < 0) {
            free(buffer);
            free(arr_argv);
            return 3;
        }
        if (read_res == 0) {
            if (len <= 0 || len == num) {
                free(buffer);
                free(arr_argv);
                return 4;
            }
            if (len > 0 && len < num) {
                check_ans(arr_argv, num_arr_argv, buffer, len);
            }
            break;
        }
        int left = len;
        int right = read_res + len;
        while (left < right) {
            if (buffer[left] == delim) {
                left++;
                check_ans(arr_argv, num_arr_argv, buffer, left);
                memmove(buffer, buffer + left, right - left);
                right = right - left;
                left = 0;
            } else {
                left++;
            }
        }
        if (left == num + 1) {
            free(buffer);
            free(arr_argv);
            return 5;
        } else {
            len = left;
        }
    }
    free(buffer);
    free(arr_argv);
    return 0;
}

variable_types:
argv
char **

variable_types: num int

variable_types: len int

variable_types: c int

variable_types: delim char

variable_types: i int


decompiled_code:
void __fastcall write_to_fd(int fd, __int64 a2, int a3)
{
int i; // ebx

if ( a3 > 0 )
{
for ( i = 0; i < a3; i += write(fd, (const void *)(a2 + i), a3 - i) )
;
}
}

canonical_decompiled_code:
void __fastcall func7(int fd, __int64 a2, int a3)
{
int i; // ebx

if ( a3 > 0 )
{
for ( i = 0; i < a3; i += write(fd, (const void *)(a2 + i), a3 - i) )
;
}
}

original_code:
void write_to_fd(int fd, char * buffer, int size) {
    int written = 0;
    while (written < size) {
        written += write(fd, buffer + written, size - written);
    }
}

canonical_original_code:
void write_to_fd(int fd, char *buffer, int size) {
    int written = 0;
    while (written < size) {
        written += write(fd, buffer + written, size - written);
    }
}

variable_types: fd int

variable_types: size int

variable_types: written int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // er14
int v5; // er12
int i; // ebx
int v7; // eax
char *v8; // rbp
int v9; // er13
void *ptr; // [rsp+0h] [rbp-58h]
int fd; // [rsp+Ch] [rbp-4Ch]
int pipedes[2]; // [rsp+18h] [rbp-40h] BYREF

result = 1;
if ( argc > 2 )
{
v4 = atoi(argv[1]);
if ( v4 <= 0 )
{
result = 2;
}
else
{
ptr = malloc(0x1000uLL);
if ( ptr )
{
v5 = 0;
while ( 1 )
{
while ( 1 )
{
sleep(v4);
pipe(pipedes);
if ( fork() )
break;
dup2(pipedes[1], 1);
close(pipedes[0]);
close(pipedes[1]);
execvp(argv[2], (char *const *)argv + 2);
}
wait(0LL);
v8 = (char *)malloc(0x1000uLL);
if ( !v8 )
break;
close(pipedes[1]);
for ( i = 0; ; i += v7 )
{
v7 = read(pipedes[0], &v8[i], 4096 - i);
if ( !v7 )
break;
}
close(pipedes[0]);
if ( v5 > 0 )
{
v9 = open(PREV, 577);
fd = open(CURRENT, 577);
write_to_fd(v9, (__int64)ptr, v5);
write_to_fd(fd, (__int64)v8, i);
if ( !fork() )
{
close(v9);
close(fd);
free(ptr);
execlp("/usr/bin/diff", "diff", &unk_402004, PREV, CURRENT, 0LL);
}
close(v9);
close(fd);
ptr = v8;
v5 = i;
}
else
{
ptr = v8;
v5 = i;
}
}
free(ptr);
result = 4;
}
else
{
result = 3;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // er14
int v5; // er12
int i; // ebx
int v7; // eax
char *v8; // rbp
int v9; // er13
void *ptr; // [rsp+0h] [rbp-58h]
int fd; // [rsp+Ch] [rbp-4Ch]
int pipedes[2]; // [rsp+18h] [rbp-40h] BYREF

result = 1;
if ( argc > 2 )
{
v4 = atoi(argv[1]);
if ( v4 <= 0 )
{
result = 2;
}
else
{
ptr = malloc(0x1000uLL);
if ( ptr )
{
v5 = 0;
while ( 1 )
{
while ( 1 )
{
sleep(v4);
pipe(pipedes);
if ( fork() )
break;
dup2(pipedes[1], 1);
close(pipedes[0]);
close(pipedes[1]);
execvp(argv[2], (char *const *)argv + 2);
}
wait(0LL);
v8 = (char *)malloc(0x1000uLL);
if ( !v8 )
break;
close(pipedes[1]);
for ( i = 0; ; i += v7 )
{
v7 = read(pipedes[0], &v8[i], 4096 - i);
if ( !v7 )
break;
}
close(pipedes[0]);
if ( v5 > 0 )
{
v9 = open(PREV, 577);
fd = open(CURRENT, 577);
func7(v9, (__int64)ptr, v5);
func7(fd, (__int64)v8, i);
if ( !fork() )
{
close(v9);
close(fd);
free(ptr);
execlp("/usr/bin/diff", "diff", &unk_402004, PREV, CURRENT, 0LL);
}
close(v9);
close(fd);
ptr = v8;
v5 = i;
}
else
{
ptr = v8;
v5 = i;
}
}
free(ptr);
result = 4;
}
else
{
result = 3;
}
}
}
return result;
}

original_code:
int main(int argc, char ** argv) {
    if (argc < 3) {
        return 1;
    }
    int interval = atoi(argv[1]);
    if (interval <= 0) {
        return 2;
    }
    int old_size = 0, buffer_size = 4096;
    char * old_buffer = (char *) malloc(buffer_size);
    if (old_buffer == ((void *)0)) {
        return 3;
    }
    while (1) {
        sleep(interval);
        int fds[2];
        pipe(fds);
        if (!fork()) {
            dup2(fds[1], 1);
            close(fds[0]);
            close(fds[1]);
            execvp(argv[2], &argv[2]);
        } else {
            wait(((void *)0));
            char * buffer = (char *) malloc(buffer_size);
            if (buffer == ((void *)0)) {
                free(old_buffer);
                return 4;
            }
            close(fds[1]);
            int read_result = 0, used = 0;
            while ((read_result = read(fds[0], buffer + used, buffer_size - used)) != 0) {
                used += read_result;
            }
            close(fds[0]);
            if (old_size > 0) {
                int prev = open(PREV, 01 | 0100 | 01000);
                int current = open(CURRENT, 01 | 0100 | 01000);
                write_to_fd(prev, old_buffer, old_size);
                write_to_fd(current, buffer, used);
                if (!fork()) {
                    close(prev);
                    close(current);
                    free(old_buffer);
                    execlp("/usr/bin/diff", "diff", "-u", PREV, CURRENT, ((void *)0));
                }
                close(prev);
                close(current);
            }
            old_buffer = buffer;
            old_size = used;
        }
    }
    free(old_buffer);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    if (argc < 3) {
        return 1;
    }
    int interval = atoi(argv[1]);
    if (interval <= 0) {
        return 2;
    }
    int old_size = 0; int buffer_size = 4096;
    char *old_buffer = (char *) malloc(buffer_size);
    if (old_buffer == ((void *)0)) {
        return 3;
    }
    while (1) {
        sleep(interval);
        int fds[2];
        pipe(fds);
        if (!fork()) {
            dup2(fds[1], 1);
            close(fds[0]);
            close(fds[1]);
            execvp(argv[2], &argv[2]);
        } else {
            wait(((void *)0));
            char *buffer = (char *) malloc(buffer_size);
            if (buffer == ((void *)0)) {
                free(old_buffer);
                return 4;
            }
            close(fds[1]);
            int read_result = 0; int used = 0;
            while ((read_result = read(fds[0], buffer + used, buffer_size - used)) != 0) {
                used += read_result;
            }
            close(fds[0]);
            if (old_size > 0) {
                int prev = open(PREV, 01 | 0100 | 01000);
                int current = open(CURRENT, 01 | 0100 | 01000);
                write_to_fd(prev, old_buffer, old_size);
                write_to_fd(current, buffer, used);
                if (!fork()) {
                    close(prev);
                    close(current);
                    free(old_buffer);
                    execlp("/usr/bin/diff", "diff", "-u", PREV, CURRENT, ((void *)0));
                }
                close(prev);
                close(current);
            }
            old_buffer = buffer;
            old_size = used;
        }
    }
    free(old_buffer);
    return 0;
}

variable_types: argc int

variable_types: interval int

variable_types:
fds
int [2]

variable_types: used int

variable_types: prev int

variable_types: current int


decompiled_code:
void __fastcall print_string_prefix(__int64 a1, int a2, int a3)
{
int v3; // ebx
int v5; // eax

v3 = a2;
while ( v3 < a3 )
{
v5 = write(1, (const void *)(a1 + v3), a3 - v3);
if ( v5 > 0 )
v3 += v5;
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, int a2, int a3)
{
int v3; // ebx
int v5; // eax

v3 = a2;
while ( v3 < a3 )
{
v5 = write(1, (const void *)(a1 + v3), a3 - v3);
if ( v5 > 0 )
v3 += v5;
}
}

original_code:
void print_string_prefix(char* buffer, int left, int right) {
    int cur_left = left;
    while (cur_left < right) {
        int cur = write(OUT, buffer + cur_left, right - cur_left);
        if (cur > 0) {
            cur_left += cur;
        }
    }
}

canonical_original_code:
void print_string_prefix(char *buffer, int left, int right) {
    int cur_left = left;
    while (cur_left < right) {
        int cur = write(OUT, buffer + cur_left, right - cur_left);
        if (cur > 0) {
            cur_left += cur;
        }
    }
}

variable_types:
buffer
char *

variable_types: left int

variable_types: right int

variable_types: cur int


decompiled_code:
void __fastcall print_string_twice(__int64 a1, int a2, int a3)
{
print_string_prefix(a1, a2, a3);
print_string_prefix(a1, a2, a3);
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, int a2, int a3)
{
func7(a1, a2, a3);
func7(a1, a2, a3);
}

original_code:
void print_string_twice(char* buffer, int left, int right) {
    print_string_prefix(buffer, left, right);
    print_string_prefix(buffer, left, right);
}

canonical_original_code:
void print_string_twice(char *buffer, int left, int right) {
    print_string_prefix(buffer, left, right);
    print_string_prefix(buffer, left, right);
}


original_code:
int get_next_string(char * ans, char * buffer, int * len, int k) {
    state current_state = PRINT;
    while (1) {
        int read_res = read(IN, buffer + *len, k + 1 - *len);
        if (read_res == 0) {
            if (current_state == PRINT && *len > 0 && *len <= k) {
                buffer[(*len)++] = '\n';
                int tmp = *len;
                memcpy(ans, buffer, *len);
                *len = 0;
                return tmp;
            }
            break;
        }
        int i, was_moved = 0, right = read_res + *len;
        *len = right;
        for (i = 0; i < right; i++) {
            if (buffer[i] == '\n') {
                if (current_state == PRINT) {
                    memcpy(ans, buffer, i + 1);
                    *len -= i + 1;
                    memmove(buffer, buffer + i + 1, k + 1 - (i + 1));
                    return i + 1;
                } else {
                    current_state = PRINT;
                    *len -= i + 1;
                    memmove(buffer, buffer + i + 1, k + 1 - (i + 1));
                    was_moved = 1;
                    break;
                }
            }
        }
        if (was_moved) {
            continue;
        }
        if (right == k + 1) {
            *len = 0;
            current_state = IGNORE;
        }
    }
    return -1;
}

canonical_original_code:
int get_next_string(char *ans, char *buffer, int *len, int k) {
    enum state current_state = PRINT;
    while (1) {
        int read_res = read(IN, buffer + *len, k + 1 - *len);
        if (read_res == 0) {
            if (current_state == PRINT && *len > 0 && *len <= k) {
                buffer[(*len)++] = '\n';
                int tmp = *len;
                memcpy(ans, buffer, *len);
                *len = 0;
                return tmp;
            }
            break;
        }
        int i; int was_moved = 0; int right = read_res + *len;
        *len = right;
        for (i = 0; i < right; i++) {
            if (buffer[i] == '\n') {
                if (current_state == PRINT) {
                    memcpy(ans, buffer, i + 1);
                    *len -= i + 1;
                    memmove(buffer, buffer + i + 1, k + 1 - (i + 1));
                    return i + 1;
                } else {
                    current_state = PRINT;
                    *len -= i + 1;
                    memmove(buffer, buffer + i + 1, k + 1 - (i + 1));
                    was_moved = 1;
                    break;
                }
            }
        }
        if (was_moved) {
            continue;
        }
        if (right == k + 1) {
            *len = 0;
            current_state = IGNORE;
        }
    }
    return -1;
}

variable_types:
ans
char *

variable_types:
len
int *

variable_types: k int

variable_types: tmp int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // ebp
_BYTE *v5; // r12
void *i; // rbx
int v7; // edx
int v8[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = 1;
if ( argc > 1 )
{
v4 = atoi(argv[1]);
v8[0] = 0;
result = 2;
if ( v4 > 0 )
{
v5 = malloc(v4 + 1);
if ( v5 )
{
for ( i = malloc(v4 + 1); ; print_string_twice((__int64)i, 0, v7) )
{
v7 = get_next_string(i, v5, v8, v4);
if ( v7 == -1 )
break;
}
free(v5);
result = 0;
}
else
{
result = 3;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // ebp
_BYTE *v5; // r12
void *i; // rbx
int v7; // edx
int v8[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = 1;
if ( argc > 1 )
{
v4 = atoi(argv[1]);
v8[0] = 0;
result = 2;
if ( v4 > 0 )
{
v5 = malloc(v4 + 1);
if ( v5 )
{
for ( i = malloc(v4 + 1); ; func8((__int64)i, 0, v7) )
{
v7 = func9(i, v5, v8, v4);
if ( v7 == -1 )
break;
}
free(v5);
result = 0;
}
else
{
result = 3;
}
}
}
return result;
}

original_code:
int main(int argc, char* argv[]) {
    if (argc < 2) {
        return 1;
    }
    int k = atoi(argv[1]), len = 0, res = 0;
    if (k < 1) {
        return 2;
    }
    char * buffer = (char *) malloc(k + 1);
    if (buffer == ((void *)0)) {
        return 3;
    }
    char * ans = (char *) malloc(k + 1);
    while (1) {
        if ((res = get_next_string(ans, buffer, &len, k)) == -1) {
            break;
        }
        print_string_twice(ans, 0, res);
    }
    free(buffer);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    if (argc < 2) {
        return 1;
    }
    int k = atoi(argv[1]); int len = 0; int res = 0;
    if (k < 1) {
        return 2;
    }
    char *buffer = (char *) malloc(k + 1);
    if (buffer == ((void *)0)) {
        return 3;
    }
    char *ans = (char *) malloc(k + 1);
    while (1) {
        if ((res = get_next_string(ans, buffer, &len, k)) == -1) {
            break;
        }
        print_string_twice(ans, 0, res);
    }
    free(buffer);
    return 0;
}

variable_types:
argv
char *[]

variable_types: len int

variable_types: res int


freeMem

repo: gryftir/recursivereverse

decompiled_code:
void __fastcall __noreturn freeMem(_QWORD **a1)
{
_QWORD *i; // rdi
_QWORD *v2; // rbx

for ( i = *a1; ; i = v2 )
{
v2 = (_QWORD *)i[1];
free(i);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func7(_QWORD **a1)
{
_QWORD *i; // rdi
_QWORD *v2; // rbx

for ( i = *a1; ; i = v2 )
{
v2 = (_QWORD *)i[1];
free(i);
}
}

original_code:
void freeMem (NODE ** current) {
NODE * walker, * front = *current;
while (current) {
 walker = front;
 front = front->next;
 free (walker);
}
*current = ((void *)0);
 return;
}

canonical_original_code:
void freeMem(struct NODE **current) {
struct NODE *walker; struct NODE *front = *current;
while (current) {
 walker = front;
 front = front->next;
 free (walker);
}
*current = ((void *)0);
 return;
}

variable_types: struct NODE { int num; struct NODE *next; } **


tailrecursereverse

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

result = a2;
if ( a1 != a2 )
{
v3 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = a2;
if ( v3 )
result = tailrecursereverse(v3, a1);
else
result = a1;
}
return result;
}

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

result = a2;
if ( a1 != a2 )
{
v3 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = a2;
if ( v3 )
result = func8(v3, a1);
else
result = a1;
}
return result;
}

original_code:
NODE * tailrecursereverse(NODE * current, NODE * append){
 NODE * next;
 if (current == append)
  return current;
 if (current->next == ((void *)0)) {
  current->next = append;
  return current;
 }
 next = current->next;
 current->next = append;
 return tailrecursereverse(next, current);
}

canonical_original_code:
struct NODE *tailrecursereverse(struct NODE *current, struct NODE *append){
 struct NODE *next;
 if (current == append)
  return current;
 if (current->next == ((void *)0)) {
  current->next = append;
  return current;
 }
 next = current->next;
 current->next = append;
 return tailrecursereverse(next, current);
}

variable_types:
current
struct NODE *

variable_types:
append
struct NODE *

variable_types: struct NODE { int num; struct NODE *next; } *


interativetail

decompiled_code:
__int64 __fastcall interativetail(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( result )
{
v2 = 0LL;
while ( 1 )
{
*(_QWORD *)(a1 + 8) = v2;
v2 = a1;
if ( !*(_QWORD *)(result + 8) )
break;
a1 = result;
result = *(_QWORD *)(result + 8);
}
}
else
{
result = a1;
a1 = 0LL;
}
*(_QWORD *)(result + 8) = a1;
return result;
}

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( result )
{
v2 = 0LL;
while ( 1 )
{
*(_QWORD *)(a1 + 8) = v2;
v2 = a1;
if ( !*(_QWORD *)(result + 8) )
break;
a1 = result;
result = *(_QWORD *)(result + 8);
}
}
else
{
result = a1;
a1 = 0LL;
}
*(_QWORD *)(result + 8) = a1;
return result;
}

original_code:
NODE * interativetail(NODE * current){
 NODE * next;
 NODE * append = ((void *)0);
 if (current == append)
  return current;
 while (current->next != ((void *)0)) {
  next = current->next;
  current->next = append;
  append = current;
    current = next;
 }
 current->next = append;
  return current;
}

canonical_original_code:
struct NODE *interativetail(struct NODE *current){
 struct NODE *next;
 struct NODE *append = ((void *)0);
 if (current == append)
  return current;
 while (current->next != ((void *)0)) {
  next = current->next;
  current->next = append;
  append = current;
    current = next;
 }
 current->next = append;
  return current;
}

return_type: struct NODE { int num; struct NODE *next; } *


printList

decompiled_code:
int __fastcall printList(unsigned int *a1)
{
unsigned int *i; // rbx
int result; // eax

for ( i = a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
result = printf("%d\n", *i);
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int *a1)
{
unsigned int *i; // rbx
int result; // eax

for ( i = a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
result = printf("%d\n", *i);
return result;
}

original_code:
void printList (NODE * first){
 while (first)
 {
  printf ("%d\n", first->num);
  first = first->next;
 }
 return;
}

canonical_original_code:
void printList(struct NODE *first){
 while (first)
 {
  printf ("%d\n", first->num);
  first = first->next;
 }
 return;
}

user_defined_types: struct NODE { int num; struct NODE *next; }


recursiveReverse

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

if ( !a1 )
return 0LL;
result = a1;
if ( *(_QWORD *)(a1 + 8) )
{
result = recursiveReverse();
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = a1;
*(_QWORD *)(a1 + 8) = 0LL;
}
return result;
}

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

if ( !a1 )
return 0LL;
result = a1;
if ( *(_QWORD *)(a1 + 8) )
{
result = func11();
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = a1;
*(_QWORD *)(a1 + 8) = 0LL;
}
return result;
}

original_code:
NODE * recursiveReverse(NODE * current){
 NODE * next;
 if (!current)
  return current;
 if (current->next) {
  next = recursiveReverse(current->next);
 }
 else {
  return current;
 }
 current->next->next = current;
 current->next = ((void *)0);
 return next;
}

canonical_original_code:
struct NODE *recursiveReverse(struct NODE *current){
 struct NODE *next;
 if (!current)
  return current;
 if (current->next) {
  next = recursiveReverse(current->next);
 }
 else {
  return current;
 }
 current->next->next = current;
 current->next = ((void *)0);
 return next;
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx
unsigned int *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = (unsigned int *)makenode(0);
for ( i = 1; i != 11; ++i )
v4 = (unsigned int *)pushNode((__int64)v4, i);
puts("before reverse");
printList(v4);
v4 = (unsigned int *)recursiveReverse((__int64)v4);
puts("after recursive reverse");
printList(v4);
puts("now we use the tailrecursereverse");
v4 = (unsigned int *)tailrecursereverse((__int64)v4, 0LL);
printList(v4);
puts("now interative reversal based on tail reversal");
v4 = (unsigned int *)interativetail((__int64)v4);
printList(v4);
freeMem((_QWORD **)&v4);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx
unsigned int *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = (unsigned int *)makenode(0);
for ( i = 1; i != 11; ++i )
v4 = (unsigned int *)pushNode((__int64)v4, i);
puts("before reverse");
func10(v4);
v4 = (unsigned int *)func11((__int64)v4);
puts("after recursive reverse");
func10(v4);
puts("now we use the tailrecursereverse");
v4 = (unsigned int *)func8((__int64)v4, 0LL);
func10(v4);
puts("now interative reversal based on tail reversal");
v4 = (unsigned int *)func9((__int64)v4);
func10(v4);
func7((_QWORD **)&v4);
}

original_code:
int main (int argc, char * argv[]){
int count;
NODE * list = makenode(0);
for (count = 1; count <= 10; count++)
 list = pushNode(list,count);
printf("before reverse\n");
printList(list);
list = recursiveReverse(list);
printf ("after recursive reverse\n");
printList(list);
printf("now we use the tailrecursereverse\n");
list = tailrecursereverse(list, ((void *)0));
printList(list);
printf("now interative reversal based on tail reversal\n");
list = interativetail(list);
printList(list);
freeMem(&list);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
int count;
struct NODE *list = makenode(0);
for (count = 1; count <= 10; count++)
 list = pushNode(list,count);
printf("before reverse\n");
printList(list);
list = recursiveReverse(list);
printf ("after recursive reverse\n");
printList(list);
printf("now we use the tailrecursereverse\n");
list = tailrecursereverse(list, ((void *)0));
printList(list);
printf("now interative reversal based on tail reversal\n");
list = interativetail(list);
printList(list);
freeMem(&list);
 return 0;
}

variable_types: argc int

variable_types: count int


repo: gsauthof/traceproc

original_code:
int timespec_add(struct timespec *res,
    const struct timespec *a)
{
  struct timespec old = *res;
  res->tv_sec += a->tv_sec;
  long sum = res->tv_nsec + a->tv_nsec;
  if (sum < 1000000000)
    res->tv_nsec = sum;
  else {
    ++res->tv_sec;
    res->tv_nsec = sum - 1000000000;
  }
  if (old.tv_sec > res->tv_sec)
    return -1;
  if (old.tv_sec == res->tv_sec && old.tv_nsec > res->tv_nsec)
    return -1;
  return 0;
}

canonical_original_code:
int timespec_add(struct timespec *res, struct timespec *a)
{
  struct timespec old = *res;
  res->tv_sec += a->tv_sec;
  long sum = res->tv_nsec + a->tv_nsec;
  if (sum < 1000000000)
    res->tv_nsec = sum;
  else {
    ++res->tv_sec;
    res->tv_nsec = sum - 1000000000;
  }
  if (old.tv_sec > res->tv_sec)
    return -1;
  if (old.tv_sec == res->tv_sec && old.tv_nsec > res->tv_nsec)
    return -1;
  return 0;
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *

variable_types:
a
struct timespec *

variable_types: sum long


decompiled_code:
__int64 __fastcall timespec_minus(_QWORD *a1, _QWORD *a2)
{
__int64 v2; // rax
__int64 v3; // rdx
__int64 v4; // rcx
__int64 result; // rax

if ( *a1 < *a2 )
return 0xFFFFFFFFLL;
v2 = *a1 - *a2;
*a1 = v2;
v3 = a1[1];
v4 = a2[1];
if ( v3 < v4 )
{
if ( v2 )
{
*a1 = v2 - 1;
a1[1] = v3 - a2[1] + 1000000000;
result = 0LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
else
{
a1[1] = v3 - v4;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, _QWORD *a2)
{
__int64 v2; // rax
__int64 v3; // rdx
__int64 v4; // rcx
__int64 result; // rax

if ( *a1 < *a2 )
return 0xFFFFFFFFLL;
v2 = *a1 - *a2;
*a1 = v2;
v3 = a1[1];
v4 = a2[1];
if ( v3 < v4 )
{
if ( v2 )
{
*a1 = v2 - 1;
a1[1] = v3 - a2[1] + 1000000000;
result = 0LL;
}
else
{
result = 0xFFFFFFFFLL;
}
}
else
{
a1[1] = v3 - v4;
result = 0LL;
}
return result;
}

original_code:
int timespec_minus(struct timespec *res,
    const struct timespec *a)
{
  if (res->tv_sec < a->tv_sec)
    return -1;
  res->tv_sec -= a->tv_sec;
  if (res->tv_nsec >= a->tv_nsec)
    res->tv_nsec -= a->tv_nsec;
  else {
    if (!res->tv_sec)
      return -1;
    --res->tv_sec;
    long x = 1000000000 + (res->tv_nsec - a->tv_nsec);
    res->tv_nsec = x;
  }
  return 0;
}

canonical_original_code:
int timespec_minus(struct timespec *res, struct timespec *a)
{
  if (res->tv_sec < a->tv_sec)
    return -1;
  res->tv_sec -= a->tv_sec;
  if (res->tv_nsec >= a->tv_nsec)
    res->tv_nsec -= a->tv_nsec;
  else {
    if (!res->tv_sec)
      return -1;
    --res->tv_sec;
    long x = 1000000000 + (res->tv_nsec - a->tv_nsec);
    res->tv_nsec = x;
  }
  return 0;
}

variable_types: x long

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


decompiled_code:
__int64 __fastcall timespec_add_intv(__int64 *a1, _QWORD *a2, const __m128i *a3)
{
unsigned int v3; // ebx
__int128 v5[2]; // [rsp+0h] [rbp-28h] BYREF

if ( *a2 > a3->m128i_i64[0] )
return (unsigned int)-1;
if ( *a2 == a3->m128i_i64[0] && a2[1] > a3->m128i_i64[1] )
return (unsigned int)-1;
v5[0] = (__int128)_mm_loadu_si128(a3);
v3 = timespec_minus(v5, a2);
if ( v3 )
{
fprintf(stderr, "%s() %s:%d: Error %s\n", "timespec_add_intv", "timespec.c", 83LL, (const char *)&LC0);
}
else
{
v3 = timespec_add(a1, v5);
if ( v3 )
fprintf(stderr, "%s() %s:%d: Error %s\n", "timespec_add_intv", "timespec.c", 85LL, (const char *)&LC0);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1, _QWORD *a2, const __m128i *a3)
{
unsigned int v3; // ebx
__int128 v5[2]; // [rsp+0h] [rbp-28h] BYREF

if ( *a2 > a3->m128i_i64[0] )
return (unsigned int)-1;
if ( *a2 == a3->m128i_i64[0] && a2[1] > a3->m128i_i64[1] )
return (unsigned int)-1;
v5[0] = (__int128)_mm_loadu_si128(a3);
v3 = func1(v5, a2);
if ( v3 )
{
fprintf(stderr, "%s() %s:%d: Error %s\n", "timespec_add_intv", "timespec.c", 83LL, (const char *)&LC0);
}
else
{
v3 = func0(a1, v5);
if ( v3 )
fprintf(stderr, "%s() %s:%d: Error %s\n", "timespec_add_intv", "timespec.c", 85LL, (const char *)&LC0);
}
return v3;
}

original_code:
int timespec_add_intv(struct timespec *res,
    const struct timespec *begin, const struct timespec *end)
{
  if (begin->tv_sec > end->tv_sec)
    return -1;
  if (begin->tv_sec == end->tv_sec
      && begin->tv_nsec > end->tv_nsec)
    return -1;
  struct timespec t = {0};
  struct timespec a = *end;
  int ret = timespec_minus(&a, begin);
  do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "timespec.c", 83, (0) ? (0) : ""); return (ret); } } while (0);
  ret = timespec_add(res, &a);
  do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "timespec.c", 85, (0) ? (0) : ""); return (ret); } } while (0);
  return 0;
}

canonical_original_code:
int timespec_add_intv(struct timespec *res, struct timespec *begin, struct timespec *end)
{
  if (begin->tv_sec > end->tv_sec)
    return -1;
  if (begin->tv_sec == end->tv_sec
      && begin->tv_nsec > end->tv_nsec)
    return -1;
  struct timespec t = {0};
  struct timespec a = *end;
  int ret = timespec_minus(&a, begin);
  do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "timespec.c", 83, (0) ? (0) : ""); return (ret); } } while (0);
  ret = timespec_add(res, &a);
  do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "timespec.c", 85, (0) ? (0) : ""); return (ret); } } while (0);
  return 0;
}

variable_types:
begin
struct timespec *

variable_types:
end
struct timespec *

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

variable_types: ret int


decompiled_code:
double __fastcall timespec_cent(_QWORD *a1, _QWORD *a2)
{
return ((double)(int)*a1 * 1000000000.0 + (double)(int)a1[1]) / ((double)(int)*a2 * 1000000000.0 + (double)(int)a2[1]);
}

canonical_decompiled_code:
double __fastcall func3(_QWORD *a1, _QWORD *a2)
{
return ((double)(int)*a1 * 1000000000.0 + (double)(int)a1[1]) / ((double)(int)*a2 * 1000000000.0 + (double)(int)a2[1]);
}

original_code:
double timespec_cent(const struct timespec *part,
    const struct timespec *sum)
{
  return
    ((double)part->tv_sec * 1000000000.0 + (double)part->tv_nsec) /
    ((double)sum->tv_sec * 1000000000.0 + (double)sum->tv_nsec);
}

canonical_original_code:
double timespec_cent(struct timespec *part, struct timespec *sum)
{
  return
    ((double)part->tv_sec * 1000000000.0 + (double)part->tv_nsec) /
    ((double)sum->tv_sec * 1000000000.0 + (double)sum->tv_nsec);
}

variable_types:
sum
struct timespec *

return_type: double


decompiled_code:
__int64 __fastcall timespec_pp_intv(_QWORD *a1, char *a2, size_t a3)
{
__int64 v3; // rax
char s[4]; // [rsp+Ah] [rbp-1Eh] BYREF
__int16 v7; // [rsp+Eh] [rbp-1Ah]

*(_DWORD *)s = 0;
v7 = 0;
v3 = a1[1];
if ( v3 > 999999999 )
return 0xFFFFFFFFLL;
snprintf(s, 6uLL, "%.3f", (double)(int)v3 / 1000000000.0);
snprintf(a2, a3, "%zu.%s", *a1, &s[2]);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, char *a2, size_t a3)
{
__int64 v3; // rax
char s[4]; // [rsp+Ah] [rbp-1Eh] BYREF
__int16 v7; // [rsp+Eh] [rbp-1Ah]

*(_DWORD *)s = 0;
v7 = 0;
v3 = a1[1];
if ( v3 > 999999999 )
return 0xFFFFFFFFLL;
snprintf(s, 6uLL, "%.3f", (double)(int)v3 / 1000000000.0);
snprintf(a2, a3, "%zu.%s", *a1, &s[2]);
return 0LL;
}

original_code:
int timespec_pp_intv(const struct timespec *i,
    char *s, size_t n)
{
  char suffix[6] = {0};
  if (i->tv_nsec >= 1000000000)
    return -1;
  snprintf(suffix, 6, "%.3f", (double)i->tv_nsec/1000000000.0);
  snprintf(s, n, "%zu.%s", i->tv_sec, suffix+2);
  return 0;
}

canonical_original_code:
int timespec_pp_intv(struct timespec *i, char *s, unsigned long n)
{
  char suffix[6] = {0};
  if (i->tv_nsec >= 1000000000)
    return -1;
  snprintf(suffix, 6, "%.3f", (double)i->tv_nsec/1000000000.0);
  snprintf(s, n, "%zu.%s", i->tv_sec, suffix+2);
  return 0;
}

variable_types:
s
char *

variable_types:
n
unsigned long

variable_types:
suffix
char [6]


decompiled_code:
size_t __fastcall timespec_strftime(char *s, size_t maxsize, char *format, time_t *a4)
{
size_t v4; // rbx
void *v8; // rsp
size_t v9; // r14
size_t v10; // r14
char *v11; // rax
char *v12; // r13
size_t v13; // r14
char v14; // dl
char *dest; // [rsp+0h] [rbp-90h] BYREF
char *sa; // [rsp+8h] [rbp-88h]
char src[4]; // [rsp+1Ah] [rbp-76h] BYREF
__int16 v18; // [rsp+1Eh] [rbp-72h]
struct tm tp; // [rsp+20h] [rbp-70h] BYREF
time_t timer; // [rsp+58h] [rbp-38h] BYREF

dest = s;
v4 = maxsize;
if ( maxsize )
{
timer = *a4;
*(_QWORD *)&tp.tm_sec = 0LL;
*(_QWORD *)&tp.tm_hour = 0LL;
*(_QWORD *)&tp.tm_mon = 0LL;
*(_QWORD *)&tp.tm_wday = 0LL;
*(_QWORD *)&tp.tm_isdst = 0LL;
tp.tm_gmtoff = 0LL;
tp.tm_zone = 0LL;
if ( localtime_r(&timer, &tp) )
{
if ( strftime(s, maxsize, format, &tp) )
{
v8 = alloca(maxsize);
LOBYTE(dest) = 0;
sa = s;
v9 = 0LL;
while ( 1 )
{
v11 = strchr(sa, 35);
v12 = v11;
if ( !v11 )
break;
v13 = v11 - sa + v9;
if ( maxsize <= v13 )
return 0LL;
strncat((char *)&dest, sa, v11 - sa);
v14 = v12[1];
if ( v14 == 83 )
{
v9 = v13 + 3;
if ( maxsize <= v9 )
return 0LL;
*(_DWORD *)src = 0;
v18 = 0;
snprintf(src, 6uLL, "%.3f", (double)(int)a4[1] / 1000000000.0);
strcat((char *)&dest, &src[2]);
}
else if ( v14 > 83 )
{
if ( v14 != 95 )
goto LABEL_25;
v9 = v13 + 1;
if ( maxsize <= v9 )
return 0LL;
*(_WORD *)((char *)&dest + strlen((const char *)&dest)) = 32;
}
else
{
if ( !v14 )
{
fwrite("timespec_strftime: missing # code\n", 1uLL, 0x22uLL, stderr);
return 0LL;
}
if ( v14 != 35 )
{
LABEL_25:
fprintf(stderr, "timespec_strftime: unknown # code: %c\n", (unsigned int)v14);
return 0LL;
}
v9 = v13 + 1;
if ( maxsize <= v9 )
return 0LL;
*(_WORD *)((char *)&dest + strlen((const char *)&dest)) = 35;
}
sa = v12 + 2;
}
v10 = strlen(sa) + v9;
if ( maxsize > v10 )
{
strcat((char *)&dest, sa);
strcpy(dest, (const char *)&dest);
v4 = v10;
}
else
{
v4 = 0LL;
}
}
else
{
v4 = 0LL;
}
}
else
{
v4 = 0LL;
}
}
return v4;
}

canonical_decompiled_code:
size_t __fastcall func5(char *s, size_t maxsize, char *format, time_t *a4)
{
size_t v4; // rbx
void *v8; // rsp
size_t v9; // r14
size_t v10; // r14
char *v11; // rax
char *v12; // r13
size_t v13; // r14
char v14; // dl
char *dest; // [rsp+0h] [rbp-90h] BYREF
char *sa; // [rsp+8h] [rbp-88h]
char src[4]; // [rsp+1Ah] [rbp-76h] BYREF
__int16 v18; // [rsp+1Eh] [rbp-72h]
struct tm tp; // [rsp+20h] [rbp-70h] BYREF
time_t timer; // [rsp+58h] [rbp-38h] BYREF

dest = s;
v4 = maxsize;
if ( maxsize )
{
timer = *a4;
*(_QWORD *)&tp.tm_sec = 0LL;
*(_QWORD *)&tp.tm_hour = 0LL;
*(_QWORD *)&tp.tm_mon = 0LL;
*(_QWORD *)&tp.tm_wday = 0LL;
*(_QWORD *)&tp.tm_isdst = 0LL;
tp.tm_gmtoff = 0LL;
tp.tm_zone = 0LL;
if ( localtime_r(&timer, &tp) )
{
if ( strftime(s, maxsize, format, &tp) )
{
v8 = alloca(maxsize);
LOBYTE(dest) = 0;
sa = s;
v9 = 0LL;
while ( 1 )
{
v11 = strchr(sa, 35);
v12 = v11;
if ( !v11 )
break;
v13 = v11 - sa + v9;
if ( maxsize <= v13 )
return 0LL;
strncat((char *)&dest, sa, v11 - sa);
v14 = v12[1];
if ( v14 == 83 )
{
v9 = v13 + 3;
if ( maxsize <= v9 )
return 0LL;
*(_DWORD *)src = 0;
v18 = 0;
snprintf(src, 6uLL, "%.3f", (double)(int)a4[1] / 1000000000.0);
strcat((char *)&dest, &src[2]);
}
else if ( v14 > 83 )
{
if ( v14 != 95 )
goto LABEL_25;
v9 = v13 + 1;
if ( maxsize <= v9 )
return 0LL;
*(_WORD *)((char *)&dest + strlen((const char *)&dest)) = 32;
}
else
{
if ( !v14 )
{
fwrite("timespec_strftime: missing # code\n", 1uLL, 0x22uLL, stderr);
return 0LL;
}
if ( v14 != 35 )
{
LABEL_25:
fprintf(stderr, "timespec_strftime: unknown # code: %c\n", (unsigned int)v14);
return 0LL;
}
v9 = v13 + 1;
if ( maxsize <= v9 )
return 0LL;
*(_WORD *)((char *)&dest + strlen((const char *)&dest)) = 35;
}
sa = v12 + 2;
}
v10 = strlen(sa) + v9;
if ( maxsize > v10 )
{
strcat((char *)&dest, sa);
strcpy(dest, (const char *)&dest);
v4 = v10;
}
else
{
v4 = 0LL;
}
}
else
{
v4 = 0LL;
}
}
else
{
v4 = 0LL;
}
}
return v4;
}

original_code:
size_t timespec_strftime(char *s, size_t max, const char *format,
    const struct timespec *t)
{
  if (!max)
    return 0;
  time_t secs = t->tv_sec;
  struct tm tm = {0};
  struct tm *ret_l = localtime_r(&secs, &tm);
  if (!ret_l)
    return 0;
  size_t ret_s = strftime(s, max, format, &tm);
  if (!ret_s)
    return 0;
  char out[max];
  *out = 0;
  const char *a = s;
  size_t written = 0;
  for (;;) {
    const char *x = strchr(a, '#');
    if (!x) {
      written += strlen(a);
      if (written >= max)
        return 0;
      strcat(out, a);
      strcpy(s, out);
      return written;
    }
    written += x-a;
    if (written >= max)
      return 0;
    strncat(out, a, x-a);
    switch (x[1]) {
      case '#':
        ++written;
        if (written >= max)
          return 0;
        strcat(out, "#");
        break;
      case '_':
        ++written;
        if (written >= max)
          return 0;
        strcat(out, " ");
        break;
      case 'S':
        {
          written += 3;
          if (written >= max)
            return 0;
          char sub[6] = {0};
          snprintf(sub, 6, "%.3f", (double)t->tv_nsec/1000000000.0);
          strcat(out, sub+2);
        }
        break;
      case 0:
        fprintf(stderr, "timespec_strftime: missing # code\n");
        return 0;
      default:
        fprintf(stderr, "timespec_strftime: unknown # code: %c\n",x[1]);
        return 0;
    }
    a = x+2;
  }
}

canonical_original_code:
unsigned long timespec_strftime(char *s, unsigned long max, char *format, struct timespec *t)
{
  if (!max)
    return 0;
  long secs = t->tv_sec;
  struct tm tm = {0};
  struct tm *ret_l = localtime_r(&secs, &tm);
  if (!ret_l)
    return 0;
  unsigned long ret_s = strftime(s, max, format, &tm);
  if (!ret_s)
    return 0;
  char out[-1];
  *out = 0;
  char *a = s;
  unsigned long written = 0;
  for (;;) {
    char *x = strchr(a, '#');
    if (!x) {
      written += strlen(a);
      if (written >= max)
        return 0;
      strcat(out, a);
      strcpy(s, out);
      return written;
    }
    written += x-a;
    if (written >= max)
      return 0;
    strncat(out, a, x-a);
    switch (x[1]) {
      case '#':
        ++written;
        if (written >= max)
          return 0;
        strcat(out, "#");
        break;
      case '_':
        ++written;
        if (written >= max)
          return 0;
        strcat(out, " ");
        break;
      case 'S':
        {
          written += 3;
          if (written >= max)
            return 0;
          char sub[6] = {0};
          snprintf(sub, 6, "%.3f", (double)t->tv_nsec/1000000000.0);
          strcat(out, sub+2);
        }
        break;
      case 0:
        fprintf(stderr, "timespec_strftime: missing # code\n");
        return 0;
      default:
        fprintf(stderr, "timespec_strftime: unknown # code: %c\n",x[1]);
        return 0;
    }
    a = x+2;
  }
}

variable_types:
max
unsigned long

variable_types:
format
char *

variable_types: secs long

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

variable_types:
out
char [-1]

variable_types:
a
char *

variable_types:
written
unsigned long

variable_types:
x
char *

variable_types:
sub
char [6]

return_type: unsigned long


decompiled_code:
__int64 __fastcall sql_count_host_vars(char *s)
{
__int64 v2; // r12
char *i; // rbx
char v4; // bp
char *v5; // rbp
char *v6; // rax
char *v7; // rax

v2 = 0LL;
while ( 1 )
{
v5 = strchr(s, 39);
v6 = strchr(s, 58);
if ( !v6 )
break;
if ( v5 && v5 <= v6 )
{
v7 = strchr(v5 + 1, 39);
if ( !v7 )
return v2;
s = v7 + 1;
}
else
{
++v2;
for ( i = v6 + 1; ; ++i )
{
v4 = *i;
if ( !(unsigned int)is_hostvar_char(*i) )
break;
}
s = &i[v4 == 58];
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s)
{
__int64 v2; // r12
char *i; // rbx
char v4; // bp
char *v5; // rbp
char *v6; // rax
char *v7; // rax

v2 = 0LL;
while ( 1 )
{
v5 = strchr(s, 39);
v6 = strchr(s, 58);
if ( !v6 )
break;
if ( v5 && v5 <= v6 )
{
v7 = strchr(v5 + 1, 39);
if ( !v7 )
return v2;
s = v7 + 1;
}
else
{
++v2;
for ( i = v6 + 1; ; ++i )
{
v4 = *i;
if ( !(unsigned int)func0(*i) )
break;
}
s = &i[v4 == 58];
}
}
return v2;
}

original_code:
size_t sql_count_host_vars(const char *s)
{
  size_t ret = 0;
  const char *pos = s;
  for (;;) {
    const char *a = strchr(pos, '\'');
    const char *b = strchr(pos, ':');
    if (!b)
      break;
    if (!a || b<a) {
      ++ret;
      pos = b + 1;
      for (; is_hostvar_char(*pos); ++pos)
        ;
      if (*pos == ':')
        ++pos;
    } else {
      pos = a + 1;
      pos = strchr(pos, '\'');
      if (!pos)
        break;
      ++pos;
    }
  }
  return ret;
}

canonical_original_code:
unsigned long sql_count_host_vars(char *s)
{
  unsigned long ret = 0;
  char *pos = s;
  for (;;) {
    char *a = strchr(pos, '\'');
    char *b = strchr(pos, ':');
    if (!b)
      break;
    if (!a || b<a) {
      ++ret;
      pos = b + 1;
      for (; is_hostvar_char(*pos); ++pos)
        ;
      if (*pos == ':')
        ++pos;
    } else {
      pos = a + 1;
      pos = strchr(pos, '\'');
      if (!pos)
        break;
      ++pos;
    }
  }
  return ret;
}

variable_types:
ret
unsigned long

variable_types:
pos
char *

variable_types:
b
char *


original_code:
int sql_tokenize(const char *s,
    SQL_Token **tokens, size_t *size)
{
  size_t l = sql_count_host_vars(s);
  *size = l*2+2;
  *tokens = calloc(*size, sizeof(SQL_Token));
  if (!tokens)
    return -1;
  SQL_Token *out = *tokens;
  size_t n = strlen(s);
  const char *token_begin = s;
  const char *pos = s;
  for (;;) {
    const char *a = strchr(pos, '\'');
    const char *b = strchr(pos, ':');
    if (!b) {
      out->begin = token_begin;
      out->end = token_begin + strlen(token_begin);
      ++out;
      return 0;
    }
    if (!a || b<a) {
      if (token_begin < b) {
        out->begin = token_begin;
        out->end = b;
         ++out;
      }
      const char *str_pos = b+1;
      for (;;) {
        for (; is_hostvar_char(*str_pos); ++str_pos)
          ;
        if (*str_pos != ':')
          break;
        ++str_pos;
      }
      out->host_var = 1;
      out->begin = b;
      out->end = str_pos;
      ++out;
      token_begin = pos = str_pos;
    } else {
      const char *str_pos = a+1;
      for (;;) {
        const char *a = strchr(str_pos, '\'');
        if (!a) {
          out->begin = token_begin;
          out->end = token_begin + strlen(token_begin);
          ++out;
          return 0;
        }
        if (!a[1]) {
          out->begin = token_begin;
          out->end = token_begin + strlen(token_begin);
          ++out;
          return 0;
        }
        if (a[1] == '\'') {
          str_pos = a+2;
          continue;
        }
        pos = a+1;
        break;
      }
    }
  }
}

canonical_original_code:
int sql_tokenize(char *s, struct SQL_Token **tokens, unsigned long *size)
{
  unsigned long l = sql_count_host_vars(s);
  *size = l*2+2;
  *tokens = calloc(*size, sizeof(SQL_Token));
  if (!tokens)
    return -1;
  struct SQL_Token *out = *tokens;
  unsigned long n = strlen(s);
  char *token_begin = s;
  char *pos = s;
  for (;;) {
    char *a = strchr(pos, '\'');
    char *b = strchr(pos, ':');
    if (!b) {
      out->begin = token_begin;
      out->end = token_begin + strlen(token_begin);
      ++out;
      return 0;
    }
    if (!a || b<a) {
      if (token_begin < b) {
        out->begin = token_begin;
        out->end = b;
         ++out;
      }
      char *str_pos = b+1;
      for (;;) {
        for (; is_hostvar_char(*str_pos); ++str_pos)
          ;
        if (*str_pos != ':')
          break;
        ++str_pos;
      }
      out->host_var = 1;
      out->begin = b;
      out->end = str_pos;
      ++out;
      token_begin = pos = str_pos;
    } else {
      char *str_pos = a+1;
      for (;;) {
        char *a = strchr(str_pos, '\'');
        if (!a) {
          out->begin = token_begin;
          out->end = token_begin + strlen(token_begin);
          ++out;
          return 0;
        }
        if (!a[1]) {
          out->begin = token_begin;
          out->end = token_begin + strlen(token_begin);
          ++out;
          return 0;
        }
        if (a[1] == '\'') {
          str_pos = a+2;
          continue;
        }
        pos = a+1;
        break;
      }
    }
  }
}

variable_types: struct SQL_Token { _Bool host_var; char *begin; char *end; } **

variable_types:
size
unsigned long *

variable_types:
l
unsigned long


decompiled_code:
__int64 __fastcall sql_quote_str(char *src, unsigned __int64 a2, char *a3, unsigned __int64 a4)
{
char *v4; // rbp
unsigned __int64 v6; // r14
char *v7; // rax
char *v8; // rbx
size_t v9; // rdx
unsigned __int64 v10; // r13
__int64 v11; // r14
char *v12; // rax

v4 = src;
v6 = a4;
*a3 = 39;
v7 = strchr(src, 39);
if ( v7 )
{
v8 = v7;
v9 = v7 - src;
if ( a2 <= v7 - src )
{
v4 = src;
v11 = 0LL;
v10 = 0LL;
}
else
{
v10 = v9 + 2;
if ( v6 <= v9 + 2 )
return 0xFFFFFFFFLL;
v11 = 0LL;
while ( 1 )
{
v11 += v9 + 1;
strncat(a3, v4, v9);
strcat(a3, "''");
v4 = v8 + 1;
v12 = strchr(v8 + 1, 39);
v8 = v12;
if ( !v12 || v12 - src >= a2 )
break;
v9 = v12 - v4;
v10 += v12 - v4 + 2;
if ( v10 >= a4 )
return 0xFFFFFFFFLL;
}
}
}
else
{
v4 = src;
v11 = 0LL;
v10 = 0LL;
}
if ( a4 <= v10 + a2 - v11 + 1 )
return 0xFFFFFFFFLL;
strncat(a3, v4, a2 - v11);
*(_WORD *)&a3[strlen(a3)] = 39;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src, unsigned __int64 a2, char *a3, unsigned __int64 a4)
{
char *v4; // rbp
unsigned __int64 v6; // r14
char *v7; // rax
char *v8; // rbx
size_t v9; // rdx
unsigned __int64 v10; // r13
__int64 v11; // r14
char *v12; // rax

v4 = src;
v6 = a4;
*a3 = 39;
v7 = strchr(src, 39);
if ( v7 )
{
v8 = v7;
v9 = v7 - src;
if ( a2 <= v7 - src )
{
v4 = src;
v11 = 0LL;
v10 = 0LL;
}
else
{
v10 = v9 + 2;
if ( v6 <= v9 + 2 )
return 0xFFFFFFFFLL;
v11 = 0LL;
while ( 1 )
{
v11 += v9 + 1;
strncat(a3, v4, v9);
strcat(a3, "''");
v4 = v8 + 1;
v12 = strchr(v8 + 1, 39);
v8 = v12;
if ( !v12 || v12 - src >= a2 )
break;
v9 = v12 - v4;
v10 += v12 - v4 + 2;
if ( v10 >= a4 )
return 0xFFFFFFFFLL;
}
}
}
else
{
v4 = src;
v11 = 0LL;
v10 = 0LL;
}
if ( a4 <= v10 + a2 - v11 + 1 )
return 0xFFFFFFFFLL;
strncat(a3, v4, a2 - v11);
*(_WORD *)&a3[strlen(a3)] = 39;
return 0LL;
}

original_code:
int sql_quote_str(const char *input, size_t input_size, char *output,
    size_t output_size)
{
  const char *p = input;
  *output = '\'';
  char *o = output;
  size_t written = 0;
  size_t seen = 0;
  for (const char *x = strchr(p, '\'');
      x && x-input<input_size;
      x = strchr(p, '\'')) {
    written += x-p + 2;
    if (written>=output_size)
      return -1;
    seen += x-p + 1;
    strncat(o, p, x-p);
    strcat(o, "''");
    p = x+1;
  }
  size_t n = input_size - seen;
  written += n + 1;
  if (written>=output_size)
    return -1;
  strncat(o, p, n);
  strcat(o, "'");
  return 0;
}

canonical_original_code:
int sql_quote_str(char *input, unsigned long input_size, char *output, unsigned long output_size)
{
  char *p = input;
  *output = '\'';
  char *o = output;
  unsigned long written = 0;
  unsigned long seen = 0;
  for (char *x = strchr(p, '\'');
      x && x-input<input_size;
      x = strchr(p, '\'')) {
    written += x-p + 2;
    if (written>=output_size)
      return -1;
    seen += x-p + 1;
    strncat(o, p, x-p);
    strcat(o, "''");
    p = x+1;
  }
  unsigned long n = input_size - seen;
  written += n + 1;
  if (written>=output_size)
    return -1;
  strncat(o, p, n);
  strcat(o, "'");
  return 0;
}

variable_types:
input
char *

variable_types:
output
char *

variable_types:
p
char *

variable_types:
o
char *

variable_types:
seen
unsigned long

variable_types:
n
unsigned long


original_code:
int sql_pseudo_tokenize(size_t n,
    SQL_Token **tokens, size_t *size)
{
  *size = 2*n+2+1;
  *tokens = calloc(*size, sizeof(SQL_Token));
  if (!tokens)
    return -1;
  SQL_Token *out = *tokens;
  out->begin = pseudo_begin_token;
  out->end = pseudo_begin_token+2;
  ++out;
  if (0<n) {
    out->host_var = 1;
    out->begin = pseudo_host_token;
    out->end = pseudo_host_token+4;
    ++out;
  }
  for (size_t i = 1; i<n; ++i) {
    out->begin = pseudo_token;
    out->end = pseudo_token+2;
    ++out;
    out->host_var = 1;
    out->begin = pseudo_host_token;
    out->end = pseudo_host_token+4;
    ++out;
  }
  out->begin = pseudo_end_token;
  out->end = pseudo_end_token+2;
  ++out;
  return 0;
}

canonical_original_code:
int sql_pseudo_tokenize(unsigned long n, struct SQL_Token **tokens, unsigned long *size)
{
  *size = 2*n+2+1;
  *tokens = calloc(*size, sizeof(SQL_Token));
  if (!tokens)
    return -1;
  struct SQL_Token *out = *tokens;
  out->begin = pseudo_begin_token;
  out->end = pseudo_begin_token+2;
  ++out;
  if (0<n) {
    out->host_var = 1;
    out->begin = pseudo_host_token;
    out->end = pseudo_host_token+4;
    ++out;
  }
  for (unsigned long i = 1; i<n; ++i) {
    out->begin = pseudo_token;
    out->end = pseudo_token+2;
    ++out;
    out->host_var = 1;
    out->begin = pseudo_host_token;
    out->end = pseudo_host_token+4;
    ++out;
  }
  out->begin = pseudo_end_token;
  out->end = pseudo_end_token+2;
  ++out;
  return 0;
}

variable_types: struct SQL_Token { _Bool host_var; char *begin; char *end; } *

variable_types:
i
unsigned long


decompiled_code:
void *__fastcall sig_to_str(int a1)
{
int v1; // eax
int *v2; // rdx

v1 = sig_str;
if ( !sig_str )
return &unk_541;
v2 = &sig_str;
while ( a1 != v1 )
{
v2 += 4;
v1 = *v2;
if ( !*v2 )
return &unk_541;
}
return (void *)*((_QWORD *)v2 + 1);
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
int v1; // eax
int *v2; // rdx

v1 = sig_str;
if ( !sig_str )
return &unk_541;
v2 = &sig_str;
while ( a1 != v1 )
{
v2 += 4;
v1 = *v2;
if ( !*v2 )
return &unk_541;
}
return (void *)*((_QWORD *)v2 + 1);
}

original_code:
static const char *sig_to_str(int i)
{
  const Sig_Str *s = sig_str;
  for (; s->sig; ++s) {
    if (s->sig == i) {
      return s->str;
    }
  }
  return "UNK";
}

canonical_original_code:
static char *sig_to_str(int i)
{
  struct Sig_Str *s = sig_str;
  for (; s->sig; ++s) {
    if (s->sig == i) {
      return s->str;
    }
  }
  return "UNK";
}

variable_types: i int

variable_types: struct Sig_Str { int sig; char *str; } *

return_type: char *


decompiled_code:
__int64 __fastcall ora_init_vars2(char *dest, char *a2, char *a3, __int64 a4)
{
char *v7; // rsi
size_t v8; // rbx
char *v9; // rsi
char *v10; // rsi

if ( !a4 )
return 0xFFFFFFFFLL;
v7 = getenv("ORACLE_USER");
if ( !v7 )
return 4294967294LL;
v8 = a4 - 1;
strncpy(dest, v7, v8);
v9 = getenv("ORACLE_PASS");
if ( !v9 )
return 4294967294LL;
strncpy(a2, v9, v8);
v10 = getenv("ORACLE_SID");
if ( !v10 )
return 4294967294LL;
strncpy(a3, v10, v8);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *dest, char *a2, char *a3, __int64 a4)
{
char *v7; // rsi
size_t v8; // rbx
char *v9; // rsi
char *v10; // rsi

if ( !a4 )
return 0xFFFFFFFFLL;
v7 = getenv("ORACLE_USER");
if ( !v7 )
return 4294967294LL;
v8 = a4 - 1;
strncpy(dest, v7, v8);
v9 = getenv("ORACLE_PASS");
if ( !v9 )
return 4294967294LL;
strncpy(a2, v9, v8);
v10 = getenv("ORACLE_SID");
if ( !v10 )
return 4294967294LL;
strncpy(a3, v10, v8);
return 0LL;
}

original_code:
int ora_init_vars2(char *username, char *password, char *dbspec,
    size_t size)
{
  if (!size)
    return -1;
  const char *e = getenv("ORACLE_USER");
  if (e)
    strncpy(username, e, size-1);
  else
    return -2;
  e = getenv("ORACLE_PASS");
  if (e)
    strncpy(password, e, size-1);
  else
    return -2;
  e = getenv("ORACLE_SID");
  if (e)
    strncpy(dbspec, e, size-1);
  else
    return -2;
  return 0;
}

canonical_original_code:
int ora_init_vars2(char *username, char *password, char *dbspec, unsigned long size)
{
  if (!size)
    return -1;
  char *e = getenv("ORACLE_USER");
  if (e)
    strncpy(username, e, size-1);
  else
    return -2;
  e = getenv("ORACLE_PASS");
  if (e)
    strncpy(password, e, size-1);
  else
    return -2;
  e = getenv("ORACLE_SID");
  if (e)
    strncpy(dbspec, e, size-1);
  else
    return -2;
  return 0;
}

variable_types:
username
char *

variable_types:
password
char *

variable_types:
dbspec
char *

variable_types:
size
unsigned long

variable_types:
e
char *


decompiled_code:
__int64 __fastcall ora_init_vars(char *a1)
{
return ora_init_vars2(a1, a1 + 32, a1 + 64, 31LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
return func1(a1, a1 + 32, a1 + 64, 31LL);
}

original_code:
int ora_init_vars(Ora_Connect *pc)
{
  return ora_init_vars2(pc->username, pc->password, pc->dbspec, 31);
}

canonical_original_code:
int ora_init_vars(struct Ora_Connect *pc)
{
  return ora_init_vars2(pc->username, pc->password, pc->dbspec, 31);
}

variable_types: struct Ora_Connect { char username[32]; char password[32]; char dbspec[32]; } *


decompiled_code:
__int64 ora_reset_signals()
{
unsigned int *v0; // rbx
unsigned int v1; // edi
struct sigaction v3; // [rsp+0h] [rbp-A8h] BYREF

memset(&v3, 0, sizeof(v3));
v0 = (unsigned int *)&sigs_2;
v1 = 3;
while ( !sigaction(v1, &v3, 0LL) )
{
v1 = *++v0;
if ( !*v0 )
return v1;
}
fprintf(stderr, "%s() %s:%d: Error %s\n", "ora_reset_signals", "ora_util.c", 80LL, (const char *)&unk_568);
return 1;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int *v0; // rbx
unsigned int v1; // edi
struct sigaction v3; // [rsp+0h] [rbp-A8h] BYREF

memset(&v3, 0, sizeof(v3));
v0 = (unsigned int *)&sigs_2;
v1 = 3;
while ( !sigaction(v1, &v3, 0LL) )
{
v1 = *++v0;
if ( !*v0 )
return v1;
}
fprintf(stderr, "%s() %s:%d: Error %s\n", "ora_reset_signals", "ora_util.c", 80LL, (const char *)&unk_568);
return 1;
}

original_code:
int ora_reset_signals()
{
  struct sigaction act = {0};
  act.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
  int ret = 0;
  static const int sigs[] = {
    3,
    4,
    5,
    6,
    7,
    8,
    11,
    0
  };
  const int *s = sigs;
  for (; *s; ++s) {
    ret = sigaction(*s, &act, 0);
    do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "ora_util.c", 80, (0) ? (0) : ""); return (1); } } while (0);
  }
  return 0;
}

canonical_original_code:
int ora_reset_signals()
{
  struct sigaction act = {0};
  act.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
  int ret = 0;
  static int sigs[] = {
    3,
    4,
    5,
    6,
    7,
    8,
    11,
    0
  };
  int *s = sigs;
  for (; *s; ++s) {
    ret = sigaction(*s, &act, 0);
    do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "ora_util.c", 80, (0) ? (0) : ""); return (1); } } while (0);
  }
  return 0;
}

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

variable_types: ret int

variable_types:
sigs
int []

variable_types:
s
int *

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

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


decompiled_code:
__int64 __fastcall ora_print_signals(const char *a1)
{
unsigned int v1; // ebx
int *v2; // rbx
char *v3; // rax
char *v4; // r9
unsigned int v5; // ebp
const char *v7; // r14
const char *v8; // r12
const char *v9; // rax
__sighandler_t v10; // r13
const char *v11; // [rsp+8h] [rbp-E0h]
struct sigaction v12; // [rsp+10h] [rbp-D8h] BYREF

fprintf(stderr, "# Signals - %s\n", a1);
fprintf(
stderr,
"#%5s | %15s | %20s | %10s | %7s | %7s\n",
"No",
"NAME",
"Pointer",
"SA_SIGINFO",
"SIG_DFL",
"SIG_IGN");
v1 = 1;
while ( 1 )
{
memset(&v12, 0, sizeof(v12));
v5 = sigaction(v1, 0LL, &v12);
if ( v5 )
break;
v10 = v12.sa_handler;
if ( v12.sa_handler == (__sighandler_t)((char *)sig_to_str + 1) )
{
v11 = "true";
v7 = "false";
}
else
{
v11 = "false";
v7 = "true";
if ( v12.sa_handler )
v7 = "false";
}
v8 = "true";
if ( (v12.sa_flags & 4) == 0 )
v8 = "false";
v9 = (const char *)sig_to_str(v1);
fprintf(stderr, "%6d | %15s | %20p | %10s | %7s | %7s\n", v1++, v9, v10, v8, v7, v11);
if ( v1 == 32 )
{
fwrite("\n\n", 1uLL, 2uLL, stderr);
fwrite("\n\n", 1uLL, 2uLL, stderr);
return v5;
}
}
v2 = _errno_location();
v3 = strerror(*v2);
v4 = (char *)&unk_568;
if ( v3 )
v4 = strerror(*v2);
fprintf(stderr, "%s() %s:%d: Error %s\n", "ora_print_signals", "ora_util.c", 153LL, v4);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
unsigned int v1; // ebx
int *v2; // rbx
char *v3; // rax
char *v4; // r9
unsigned int v5; // ebp
const char *v7; // r14
const char *v8; // r12
const char *v9; // rax
__sighandler_t v10; // r13
const char *v11; // [rsp+8h] [rbp-E0h]
struct sigaction v12; // [rsp+10h] [rbp-D8h] BYREF

fprintf(stderr, "# Signals - %s\n", a1);
fprintf(
stderr,
"#%5s | %15s | %20s | %10s | %7s | %7s\n",
"No",
"NAME",
"Pointer",
"SA_SIGINFO",
"SIG_DFL",
"SIG_IGN");
v1 = 1;
while ( 1 )
{
memset(&v12, 0, sizeof(v12));
v5 = sigaction(v1, 0LL, &v12);
if ( v5 )
break;
v10 = v12.sa_handler;
if ( v12.sa_handler == (__sighandler_t)((char *)sig_to_str + 1) )
{
v11 = "true";
v7 = "false";
}
else
{
v11 = "false";
v7 = "true";
if ( v12.sa_handler )
v7 = "false";
}
v8 = "true";
if ( (v12.sa_flags & 4) == 0 )
v8 = "false";
v9 = (const char *)func0(v1);
fprintf(stderr, "%6d | %15s | %20p | %10s | %7s | %7s\n", v1++, v9, v10, v8, v7, v11);
if ( v1 == 32 )
{
fwrite("\n\n", 1uLL, 2uLL, stderr);
fwrite("\n\n", 1uLL, 2uLL, stderr);
return v5;
}
}
v2 = _errno_location();
v3 = strerror(*v2);
v4 = (char *)&unk_568;
if ( v3 )
v4 = strerror(*v2);
fprintf(stderr, "%s() %s:%d: Error %s\n", "ora_print_signals", "ora_util.c", 153LL, v4);
return (unsigned int)-1;
}

original_code:
int ora_print_signals(const char *label)
{
  void (*fp1)(int, siginfo_t *, void *);
  void (*fp2)(int, siginfo_t *, void *);
  fprintf(stderr, "# Signals - %s\n", label);
  fprintf(stderr, "#%5s | %15s | %20s | %10s | %7s | %7s\n", "No", "NAME", "Pointer", "SA_SIGINFO", "SIG_DFL", "SIG_IGN");
  int i = 0;
  for (i = 1; i<32; ++i) {
    struct sigaction act = {0};
    int ret = sigaction(i, 0, &act);
    do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "ora_util.c", 153, (strerror((*__errno_location ()))) ? (strerror((*__errno_location ()))) : ""); return (-1); } } while (0);
    fprintf(stderr, "%6d | %15s | %20p | %10s | %7s | %7s\n", i, sig_to_str(i), act.__sigaction_handler.sa_sigaction, act.sa_flags & 4 ? "true" : "false", act.__sigaction_handler.sa_handler == ((__sighandler_t) 0) ? "true" : "false", act.__sigaction_handler.sa_handler == ((__sighandler_t) 1) ? "true" : "false");
    if (i == 2)
      fp1 = act.__sigaction_handler.sa_sigaction;
    if (i == 3)
      fp2 = act.__sigaction_handler.sa_sigaction;
  }
  fprintf(stderr, "\n\n");
  breakme(fp1);
  breakme(fp2);
  fprintf(stderr, "\n\n");
  return 0;
}

canonical_original_code:
int ora_print_signals(char *label)
{
  void (*fp1)(int, struct siginfo_t *, void *);
  void (*fp2)(int, struct siginfo_t *, void *);
  fprintf(stderr, "# Signals - %s\n", label);
  fprintf(stderr, "#%5s | %15s | %20s | %10s | %7s | %7s\n", "No", "NAME", "Pointer", "SA_SIGINFO", "SIG_DFL", "SIG_IGN");
  int i = 0;
  for (i = 1; i<32; ++i) {
    struct sigaction act = {0};
    int ret = sigaction(i, 0, &act);
    do { if (ret) { fprintf(stderr, "%s() %s:%d: Error %s\n", __func__, "ora_util.c", 153, (strerror((*__errno_location ()))) ? (strerror((*__errno_location ()))) : ""); return (-1); } } while (0);
    fprintf(stderr, "%6d | %15s | %20p | %10s | %7s | %7s\n", i, sig_to_str(i), act.__sigaction_handler.sa_sigaction, act.sa_flags & 4 ? "true" : "false", act.__sigaction_handler.sa_handler == ((void (*)(int)) 0) ? "true" : "false", act.__sigaction_handler.sa_handler == ((void (*)(int)) 1) ? "true" : "false");
    if (i == 2)
      fp1 = act.__sigaction_handler.sa_sigaction;
    if (i == 3)
      fp2 = act.__sigaction_handler.sa_sigaction;
  }
  fprintf(stderr, "\n\n");
  breakme(fp1);
  breakme(fp2);
  fprintf(stderr, "\n\n");
  return 0;
}

variable_types:
label
char *

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

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
__int64 __fastcall pp_para_gory(__int64 a1, __int64 a2, char a3)
{
int v4; // edx
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 120);
if ( v4 == 1403 || v4 == 0 || (result = 0LL, v4 == 100) )
{
if ( !*(_QWORD *)(a2 + 24) )
{
if ( !*(_QWORD *)(a2 + 8) )
tprintf("Iterations: %zu (of %zu)\n", *(_QWORD *)(a2 + 16), *(_QWORD *)(a1 + 56));
tprintf(" Iteration: %zu\n", *(_QWORD *)(a2 + 8));
}
if ( *(_DWORD *)a2 )
{
if ( *(_DWORD *)a2 == 1 )
{
result = 0LL;
if ( a3 )
return result;
tprintf(" OUT paramter");
}
}
else
{
if ( *(_DWORD *)(a1 + 24) == 4 )
{
result = 0LL;
if ( *(_QWORD *)(a2 + 8) )
return result;
}
tprintf(" IN paramter");
}
tprintf(" (a=%zu)", *(_QWORD *)(a2 + 24));
tprintf("\n");
tprintf(" Type: %s (%d), ", *(const char **)(a2 + 72), *(unsigned int *)(a2 + 68));
if ( *(_DWORD *)(a2 + 68) != 10 )
tprintf(
"Length: %zu, Size: %zu, Ind %d\n",
*(_QWORD *)(a2 + 48),
*(_QWORD *)(a2 + 32),
(unsigned int)*(__int16 *)(a2 + 64));
result = pp_para_gory_value(a1, a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, char a3)
{
int v4; // edx
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 120);
if ( v4 == 1403 || v4 == 0 || (result = 0LL, v4 == 100) )
{
if ( !*(_QWORD *)(a2 + 24) )
{
if ( !*(_QWORD *)(a2 + 8) )
tprintf("Iterations: %zu (of %zu)\n", *(_QWORD *)(a2 + 16), *(_QWORD *)(a1 + 56));
tprintf(" Iteration: %zu\n", *(_QWORD *)(a2 + 8));
}
if ( *(_DWORD *)a2 )
{
if ( *(_DWORD *)a2 == 1 )
{
result = 0LL;
if ( a3 )
return result;
tprintf(" OUT paramter");
}
}
else
{
if ( *(_DWORD *)(a1 + 24) == 4 )
{
result = 0LL;
if ( *(_QWORD *)(a2 + 8) )
return result;
}
tprintf(" IN paramter");
}
tprintf(" (a=%zu)", *(_QWORD *)(a2 + 24));
tprintf("\n");
tprintf(" Type: %s (%d), ", *(const char **)(a2 + 72), *(unsigned int *)(a2 + 68));
if ( *(_DWORD *)(a2 + 68) != 10 )
tprintf(
"Length: %zu, Size: %zu, Ind %d\n",
*(_QWORD *)(a2 + 48),
*(_QWORD *)(a2 + 32),
(unsigned int)*(__int16 *)(a2 + 64));
result = func0(a1, a2);
}
return result;
}

original_code:
int pp_para_gory(const Statement *stmt,
    const Parameter *p,
    _Bool before,
    void *user_ptr)
{
  if (!(!stmt->errorcode || stmt->errorcode == 1403 || stmt->errorcode == 100))
    return 0;
  if (!p->pos) {
    if (!p->iteration)
      tprintf("Iterations: %zu (of %zu)\n",
          p->iterations, stmt->iterations);
    tprintf("  Iteration: %zu\n", p->iteration);
  }
  switch (p->direction) {
    case PARA_IN:
      if (stmt->type == SELECT && p->iteration > 0)
        return 0;
      tprintf("    IN paramter");
      break;
    case PARA_OUT:
      if (before)
        return 0;
      tprintf("    OUT paramter");
      break;
  }
  tprintf(" (a=%zu)", p->pos);
  tprintf("\n");
  tprintf("      Type: %s (%d), ",
      p->type_str,
      p->type);
  if (p->type != VOID) {
    tprintf("Length: %zu, Size: %zu, Ind %d\n",
        p->length,
        p->size,
        p->indicator);
  }
  return pp_para_gory_value(stmt, p, before, user_ptr);
}

canonical_original_code:
int pp_para_gory(struct Statement *stmt, struct Parameter *p, _Bool before, void *user_ptr)
{
  if (!(!stmt->errorcode || stmt->errorcode == 1403 || stmt->errorcode == 100))
    return 0;
  if (!p->pos) {
    if (!p->iteration)
      tprintf("Iterations: %zu (of %zu)\n",
          p->iterations, stmt->iterations);
    tprintf("  Iteration: %zu\n", p->iteration);
  }
  switch (p->direction) {
    case PARA_IN:
      if (stmt->type == SELECT && p->iteration > 0)
        return 0;
      tprintf("    IN paramter");
      break;
    case PARA_OUT:
      if (before)
        return 0;
      tprintf("    OUT paramter");
      break;
  }
  tprintf(" (a=%zu)", p->pos);
  tprintf("\n");
  tprintf("      Type: %s (%d), ",
      p->type_str,
      p->type);
  if (p->type != VOID) {
    tprintf("Length: %zu, Size: %zu, Ind %d\n",
        p->length,
        p->size,
        p->indicator);
  }
  return pp_para_gory_value(stmt, p, before, user_ptr);
}

variable_types: struct Statement { short number; short offset; short length; short max_row_insert; unsigned long prefetch; char *text; enum Statement_Type { DROP=1, DELETE=2, INSERT=3, SELECT=4, UPDATE=5, OPEN=9, FETCH=13, CLOSE=15, PREPARE=17, EXECUTE=21, EXECUTE_IMMEDIATE=24, CONNECT=27, COMMIT=29, COMMIT_RELEASE=30, ROLLBACK=31, ROLLBACK_RELEASE=32, CREATE=44, OPEN_DYNAMIC=45, CALL=122, CONNECT_AT=1051 } type; char *type_str; char *filename; unsigned long line_number; unsigned long iterations; short number_of_params; short number_of_params_in; _Bool scroll_cursor; enum Fetch_Type { FETCH_CURRENT=1, FETCH_NEXT=2, FETCH_FIRST=4, FETCH_LAST=8, FETCH_PRIOR=16, FETCH_ABSOLUTE=32, FETCH_RELATIVE=64 } fetch_type; char *fetch_type_str; int fetch_offset; unsigned long acc_fetched_rows; char *msg; char *extended_msg; int errorcode; char *last_prepared_stmt; char *immediate_stmt; } *

variable_types: struct Parameter { enum Para_Type { PARA_IN=0, PARA_OUT=1 } direction; unsigned long iteration; unsigned long iterations; unsigned long pos; unsigned long size; unsigned long ind_size; unsigned long length; void *value; short indicator; enum type_no { CHAR=1, INT=3, DOUBLE=4, STRING0=5, VARCHAR=9, VOID=10, UNSIGNED=68, STRING=97 } type; char *type_str; } *

user_defined_types: struct Statement { short number; short offset; short length; short max_row_insert; unsigned long prefetch; char *text; enum Statement_Type type; char *type_str; char *filename; unsigned long line_number; unsigned long iterations; short number_of_params; short number_of_params_in; _Bool scroll_cursor; enum Fetch_Type fetch_type; char *fetch_type_str; int fetch_offset; unsigned long acc_fetched_rows; char *msg; char *extended_msg; int errorcode; char *last_prepared_stmt; char *immediate_stmt; }

user_defined_types: struct Parameter { enum Para_Type direction; unsigned long iteration; unsigned long iterations; unsigned long pos; unsigned long size; unsigned long ind_size; unsigned long length; void *value; short indicator; enum type_no type; char *type_str; }