decompiled_code:
unsigned __int64 __fastcall file_write(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v5; // r13
unsigned __int64 v6; // rsi
ssize_t v7; // rax

if ( !a3 )
return 0LL;
v5 = 0LL;
v6 = 0LL;
do
{
v7 = write(fd, (const void *)(a2 + v6), a3 - v6);
if ( v7 < 0 )
exit(-1);
v5 += v7;
v6 = v5;
}
while ( v5 < a3 );
return v5;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v5; // r13
unsigned __int64 v6; // rsi
ssize_t v7; // rax

if ( !a3 )
return 0LL;
v5 = 0LL;
v6 = 0LL;
do
{
v7 = write(fd, (const void *)(a2 + v6), a3 - v6);
if ( v7 < 0 )
exit(-1);
v5 += v7;
v6 = v5;
}
while ( v5 < a3 );
return v5;
}

original_code:
ssize_t file_write(int fd, const void *buf, size_t count) {
  ssize_t wcount = 0;
  while (wcount < count) {
    ssize_t result = write(fd, buf + wcount, count - wcount);
    if (result < 0) { exit(-1); }
    wcount += result;
  }
  return wcount;
}

canonical_original_code:
long file_write(int fd, void *buf, unsigned long count) {
  long wcount = 0;
  while (wcount < count) {
    long result = write(fd, buf + wcount, count - wcount);
    if (result < 0) { exit(-1); }
    wcount += result;
  }
  return wcount;
}

variable_types: wcount long

variable_types: result long


decompiled_code:
int __fastcall file_open_read(const char *a1)
{
int result; // eax

result = open(a1, 0);
if ( result <= 0 )
{
perror("error opening input file for reading");
exit(2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(const char *a1)
{
int result; // eax

result = open(a1, 0);
if ( result <= 0 )
{
perror("error opening input file for reading");
exit(2);
}
return result;
}

original_code:
int file_open_read(char* path) {
  int fd = open(path, 00);
  if (fd <= 0) {
    perror("error opening input file for reading");
    exit(2);
  }
  return fd;
}

canonical_original_code:
int file_open_read(char *path) {
  int fd = open(path, 00);
  if (fd <= 0) {
    perror("error opening input file for reading");
    exit(2);
  }
  return fd;
}

variable_types:
path
char *

variable_types: fd int

return_type: int


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

result = close(a1);
if ( result < 0 )
{
perror("error closing file");
exit(2);
}
return result;
}

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

result = close(a1);
if ( result < 0 )
{
perror("error closing file");
exit(2);
}
return result;
}

original_code:
void file_close(int fd) {
  int result = close(fd);
  if (result < 0) {
    perror("error closing file");
    exit(2);
  }
}

variable_types: result int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

if ( argc != 3 )
{
printf("USAGE: %s <source> <dest>\n", *argv);
exit(1);
}
sem_init(&buf_empty, 0, 0);
sem_init(&buf_full, 0, 0x2DFu);
pthread_create(&th, 0LL, reader, (void *)argv[1]);
pthread_create(newthread, 0LL, writer, (void *)argv[2]);
pthread_join(th, 0LL);
pthread_join(newthread[0], 0LL);
return 0;
}

original_code:
int main(int argc, char* argv[]) {
  if (argc != 3) {
    printf("USAGE: %s <source> <dest>\n", argv[0]);
    exit(1);
  }
  sem_init(&buf_empty, 0, 0);
  sem_init(&buf_full, 0, (735));
  pthread_t reader_thread;
  pthread_t writer_thread;
  pthread_create(&reader_thread, 0, (void*)reader, (void*)argv[1]);
  pthread_create(&writer_thread, 0, (void*)writer, (void*)argv[2]);
  pthread_join(reader_thread, ((void *)0));
  pthread_join(writer_thread, ((void *)0));
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("USAGE: %s <source> <dest>\n", argv[0]);
    exit(1);
  }
  sem_init(&buf_empty, 0, 0);
  sem_init(&buf_full, 0, (735));
  unsigned long reader_thread;
  unsigned long writer_thread;
  pthread_create(&reader_thread, 0, (void *)reader, (void *)argv[1]);
  pthread_create(&writer_thread, 0, (void *)writer, (void *)argv[2]);
  pthread_join(reader_thread, ((void *)0));
  pthread_join(writer_thread, ((void *)0));
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


repo: lexruee/Unifr-System-Programming

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // ebx
unsigned int v5; // ebp
__int64 v6; // rsi
char i; // al
__int64 v8; // rdx
unsigned int v10[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4 = 3;
v5 = 2;
print_usage(argc, argv, envp);
v6 = 1LL;
printf("%i%s", 1LL, "> ");
for ( i = getchar(); i != 81; i = getchar() )
{
if ( i == 103 )
{
v10[0] = fifo_get();
printf("%i\n", v10[0]);
}
else if ( i == 112 )
{
if ( (unsigned int)__isoc99_scanf() != 1 )
{
while ( getchar() != 10 )
;
printf("%s: ERROR: wrong scanf argument\n", *argv);
goto LABEL_7;
}
fifo_put(v10[0], v10);
printf("%i\n", v10[0]);
}
else
{
print_usage("%i%s", v6, v8);
}
while ( getchar() != 10 )
;
LABEL_7:
v6 = v5;
v5 = v4++;
printf("%i%s", v6, "> ");
}
puts("Quit");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // ebx
unsigned int v5; // ebp
__int64 v6; // rsi
char i; // al
__int64 v8; // rdx
unsigned int v10[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4 = 3;
v5 = 2;
func7(argc, argv, envp);
v6 = 1LL;
printf("%i%s", 1LL, "> ");
for ( i = getchar(); i != 81; i = getchar() )
{
if ( i == 103 )
{
v10[0] = func10();
printf("%i\n", v10[0]);
}
else if ( i == 112 )
{
if ( (unsigned int)__isoc99_scanf() != 1 )
{
while ( getchar() != 10 )
;
printf("%s: ERROR: wrong scanf argument\n", *argv);
goto LABEL_7;
}
func9(v10[0], v10);
printf("%i\n", v10[0]);
}
else
{
func7("%i%s", v6, v8);
}
while ( getchar() != 10 )
;
LABEL_7:
v6 = v5;
v5 = v4++;
printf("%i%s", v6, "> ");
}
puts("Quit");
return 0;
}

original_code:
int main(int argc, char *argv[])
{
  int elt;
  char c;
  const char *prompt = "> ";
  int prompt_count = 1;
  print_usage();
  printf("%i%s", prompt_count++, prompt);
  while ( (c = getchar()) != 'Q')
  {
    switch(c)
    {
      case 'p':
        if (scanf("%i", &elt) != 1) goto error_scanf;
        fifo_put(elt);
        printf("%i\n", elt);
        break;
      case 'g':
        elt = fifo_get();
        printf("%i\n", elt);
        break;
      default:
        print_usage();
        break;
    }
    while (getchar() != '\n') {}
    printf("%i%s", prompt_count++, prompt);
    continue;
error_scanf:
    while (getchar() != '\n') {}
    printf("%s: ERROR: wrong scanf argument\n", argv[0]);
    printf("%i%s", prompt_count++, prompt);
    continue;
  }
  printf("Quit\n");
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int elt;
  char c;
  char *prompt = "> ";
  int prompt_count = 1;
  print_usage();
  printf("%i%s", prompt_count++, prompt);
  while ( (c = getchar()) != 'Q')
  {
    switch(c)
    {
      case 'p':
        if (scanf("%i", &elt) != 1) goto error_scanf;
        fifo_put(elt);
        printf("%i\n", elt);
        break;
      case 'g':
        elt = fifo_get();
        printf("%i\n", elt);
        break;
      default:
        print_usage();
        break;
    }
    while (getchar() != '\n') {}
    printf("%i%s", prompt_count++, prompt);
    continue;
error_scanf:
    while (getchar() != '\n') {}
    printf("%s: ERROR: wrong scanf argument\n", argv[0]);
    printf("%i%s", prompt_count++, prompt);
    continue;
  }
  printf("Quit\n");
  return 0;
}

variable_types: elt int

variable_types: c char

variable_types:
prompt
char *


decompiled_code:
int print_usage()
{
return puts("p)put(i) g)get Q)Quit");
}

canonical_decompiled_code:
int func7()
{
return puts("p)put(i) g)get Q)Quit");
}

original_code:
void print_usage()
{
  printf("p)put(i)   g)get   Q)Quit\n");
}


decompiled_code:
void shift_low_end()
{
int v0; // ecx
__int64 v1; // rdx
__int64 v2; // rax

v0 = tail - head;
if ( tail - head > 0 )
{
v1 = head;
v2 = 0LL;
do
{
base[v2] = base[v2 + v1];
++v2;
}
while ( v0 > (int)v2 );
}
tail = v0;
head = 0;
}

canonical_decompiled_code:
void func8()
{
int v0; // ecx
__int64 v1; // rdx
__int64 v2; // rax

v0 = tail - head;
if ( tail - head > 0 )
{
v1 = head;
v2 = 0LL;
do
{
base[v2] = base[v2 + v1];
++v2;
}
while ( v0 > (int)v2 );
}
tail = v0;
head = 0;
}

original_code:
static void shift_low_end()
{
  int i;
  for (i=0; i<tail-head; i++) base[i] = base[i+head];
  tail = tail - head;
  head=0;
}

variable_types: i int


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

result = tail;
if ( tail - head == 4 )
exit(1);
if ( tail == 4 )
{
shift_low_end();
result = tail;
}
base[result] = a1;
tail = result + 1;
return result;
}

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

result = tail;
if ( tail - head == 4 )
exit(1);
if ( tail == 4 )
{
func8();
result = tail;
}
base[result] = a1;
tail = result + 1;
return result;
}

original_code:
void fifo_put(int e)
{
  if (tail - head == 4) exit(1);
  if (tail == 4) shift_low_end();
  base[tail++] = e;
}

variable_types: e int


decompiled_code:
__int64 fifo_get()
{
if ( head == tail )
exit(1);
return (unsigned int)base[head++];
}

canonical_decompiled_code:
__int64 func10()
{
if ( head == tail )
exit(1);
return (unsigned int)base[head++];
}

original_code:
int fifo_get()
{
  if (head == tail) exit(1);
  return base[head++];
}


repo: lexs/kjell

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // [rsp+0h] [rbp-E8h] BYREF
char *argva[10]; // [rsp+80h] [rbp-68h] BYREF
int v7; // [rsp+D0h] [rbp-18h]

v3 = 0;
__sysv_signal(2, (__sighandler_t)1);
while ( 1 )
{
print_prompt();
if ( (unsigned int)read_command(&v5) != -1 && !(unsigned int)execute_builtin(argva[0], argva) )
{
if ( v7 )
{
v4 = run_background(argva);
++v3;
printf("[?] %d\n", v4);
}
else
{
run_foreground(argva);
}
}
if ( v3 > 0 )
v3 = check_bg_processes((unsigned int)v3);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // [rsp+0h] [rbp-E8h] BYREF
char *argva[10]; // [rsp+80h] [rbp-68h] BYREF
int v7; // [rsp+D0h] [rbp-18h]

v3 = 0;
__sysv_signal(2, (__sighandler_t)1);
while ( 1 )
{
func16();
if ( (unsigned int)func11(&v5) != -1 && !(unsigned int)func14(argva[0], argva) )
{
if ( v7 )
{
v4 = func10(argva);
++v3;
printf("[?] %d\n", v4);
}
else
{
func9(argva);
}
}
if ( v3 > 0 )
v3 = func12((unsigned int)v3);
}
}

original_code:
int main(int argc, char** argv) {
    signal(2, ((__sighandler_t) 1));
    int bg_processes = 0;
    while (1) {
        command_t command;
        if (bg_processes > 0) {
            bg_processes = check_bg_processes(bg_processes);
        }
        print_prompt();
        if (read_command(&command) == -1) {
            continue;
        }
        if (!execute_builtin(command.argv[0], command.argv)) {
            if (command.background) {
                pid_t pid = run_background(command.argv);
                printf("[?] %d\n", pid);
                bg_processes++;
            } else {
                run_foreground(command.argv);
            }
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    signal(2, ((void (*)(int)) 1));
    int bg_processes = 0;
    while (1) {
        struct command_t command;
        if (bg_processes > 0) {
            bg_processes = check_bg_processes(bg_processes);
        }
        print_prompt();
        if (read_command(&command) == -1) {
            continue;
        }
        if (!execute_builtin(command.argv[0], command.argv)) {
            if (command.background) {
                int pid = run_background(command.argv);
                printf("[?] %d\n", pid);
                bg_processes++;
            } else {
                run_foreground(command.argv);
            }
        }
    }
    return 0;
}

variable_types:
argv
char **

variable_types: struct command_t { char line[128]; char *argv[10]; int background; }

variable_types: pid int


checkerr

decompiled_code:
void __fastcall checkerr(int a1, const char *a2)
{
if ( a1 == -1 )
{
perror(a2);
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func7(int a1, const char *a2)
{
if ( a1 == -1 )
{
perror(a2);
exit(1);
}
}

original_code:
void checkerr(int err, char *message) {
    if (err == -1) {
        perror(message);
        exit(1);
    }
}

variable_types: err int

variable_types:
message
char *


execute

decompiled_code:
void __fastcall __noreturn execute(char **argv)
{
int *v1; // rax
char *v2; // rsi

execvp(*argv, argv);
v1 = __errno_location();
v2 = *argv;
if ( *v1 == 2 )
printf("kjell: command not found: %s\n", v2);
else
printf("kjell: failed to start command: %s\n", v2);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(char **argv)
{
int *v1; // rax
char *v2; // rsi

execvp(*argv, argv);
v1 = __errno_location();
v2 = *argv;
if ( *v1 == 2 )
printf("kjell: command not found: %s\n", v2);
else
printf("kjell: failed to start command: %s\n", v2);
exit(1);
}

original_code:
void execute(char **program) {
    execvp(program[0], program);
    if ((*__errno_location ()) == 2) {
        printf("kjell: command not found: %s\n", program[0]);
    } else {
        printf("kjell: failed to start command: %s\n", program[0]);
    }
    exit(1);
}

variable_types:
program
char **


decompiled_code:
void __fastcall run_foreground(char **argv)
{
int v1; // ebp
int v2; // eax
int stat_loc[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = fork();
checkerr(v1, "Cannot fork");
if ( !v1 )
{
__sysv_signal(2, 0LL);
execute(argv);
}
v2 = waitpid(v1, stat_loc, 0);
checkerr(v2, "waitpid() failed unexpectedly");
}

canonical_decompiled_code:
void __fastcall func9(char **argv)
{
int v1; // ebp
int v2; // eax
int stat_loc[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = fork();
func7(v1, "Cannot fork");
if ( !v1 )
{
__sysv_signal(2, 0LL);
func8(argv);
}
v2 = waitpid(v1, stat_loc, 0);
func7(v2, "waitpid() failed unexpectedly");
}

original_code:
void run_foreground(char** program) {
    pid_t pid = fork();
    checkerr(pid, "Cannot fork");
    if (pid == 0) {
        signal(2, ((__sighandler_t) 0));
        execute(program);
    } else {
        int status;
        int r = waitpid(pid, &status, 0);
        checkerr(r, "waitpid() failed unexpectedly");
    }
}

canonical_original_code:
void run_foreground(char **program) {
    int pid = fork();
    checkerr(pid, "Cannot fork");
    if (pid == 0) {
        signal(2, ((void (*)(int)) 0));
        execute(program);
    } else {
        int status;
        int r = waitpid(pid, &status, 0);
        checkerr(r, "waitpid() failed unexpectedly");
    }
}

variable_types: status int

variable_types: r int


decompiled_code:
__int64 __fastcall run_background(char **argv)
{
unsigned int v1; // er12

v1 = fork();
checkerr(v1, "Cannot fork");
if ( !v1 )
execute(argv);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func10(char **argv)
{
unsigned int v1; // er12

v1 = fork();
func7(v1, "Cannot fork");
if ( !v1 )
func8(argv);
return v1;
}

original_code:
pid_t run_background(char **program) {
    pid_t pid = fork();
    checkerr(pid, "Cannot fork");
    if (pid == 0) {
        execute(program);
    }
    return pid;
}

canonical_original_code:
int run_background(char **program) {
    int pid = fork();
    checkerr(pid, "Cannot fork");
    if (pid == 0) {
        execute(program);
    }
    return pid;
}


decompiled_code:
__int64 __fastcall read_command(__int64 a1)
{
unsigned int v1; // er12
_QWORD *v2; // r14
signed int v3; // eax
__int64 v4; // rax
__int64 result; // rax
char *stringp; // [rsp+8h] [rbp-30h] BYREF

if ( !fgets((char *)a1, 128, stdin) )
return 0xFFFFFFFFLL;
stringp = (char *)a1;
v1 = 0;
v2 = (_QWORD *)(a1 + 128);
while ( 1 )
{
v3 = (unsigned int)strsep(&stringp, " \t\n");
*v2 = v3;
if ( !v3 )
break;
if ( *(_BYTE *)v3 )
{
++v2;
++v1;
if ( (unsigned __int64)v2 >= a1 + 768 )
goto LABEL_6;
}
}
if ( !v1 )
return 0xFFFFFFFFLL;
LABEL_6:
v4 = a1 + 128 + 8LL * (int)v1 - 8;
if ( **(_BYTE **)v4 != 38 || *(_BYTE *)(*(_QWORD *)v4 + 1LL) )
{
*(_DWORD *)(a1 + 208) = 0;
result = v1;
}
else
{
*(_DWORD *)(a1 + 208) = 1;
*(_QWORD *)v4 = 0LL;
result = v1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
unsigned int v1; // er12
_QWORD *v2; // r14
signed int v3; // eax
__int64 v4; // rax
__int64 result; // rax
char *stringp; // [rsp+8h] [rbp-30h] BYREF

if ( !fgets((char *)a1, 128, stdin) )
return 0xFFFFFFFFLL;
stringp = (char *)a1;
v1 = 0;
v2 = (_QWORD *)(a1 + 128);
while ( 1 )
{
v3 = (unsigned int)strsep(&stringp, " \t\n");
*v2 = v3;
if ( !v3 )
break;
if ( *(_BYTE *)v3 )
{
++v2;
++v1;
if ( (unsigned __int64)v2 >= a1 + 768 )
goto LABEL_6;
}
}
if ( !v1 )
return 0xFFFFFFFFLL;
LABEL_6:
v4 = a1 + 128 + 8LL * (int)v1 - 8;
if ( **(_BYTE **)v4 != 38 || *(_BYTE *)(*(_QWORD *)v4 + 1LL) )
{
*(_DWORD *)(a1 + 208) = 0;
result = v1;
}
else
{
*(_DWORD *)(a1 + 208) = 1;
*(_QWORD *)v4 = 0LL;
result = v1;
}
return result;
}

original_code:
int read_command(command_t *command) {
    if (fgets(command->line, sizeof command->line, stdin)) {
        int argc = 0;
        char **ap, **argv = command->argv, *input = command->line;
        for (ap = argv; (*ap = strsep(&input, " \t\n")) != ((void *)0);)
            if (**ap != '\0') {
                argc++;
                if (++ap >= &argv[sizeof command->argv]) break;
            }
        if (argc == 0) {
            return -1;
        }
        if (strcmp(argv[argc - 1], "&") == 0) {
            command->background = 1;
            argv[argc - 1] = '\0';
        } else {
            command->background = 0;
        }
        return argc;
    }
    return -1;
}

canonical_original_code:
int read_command(struct command_t *command) {
    if (fgets(command->line, sizeof command->line, stdin)) {
        int argc = 0;
        char **ap; char **argv = command->argv; char *input = command->line;
        for (ap = argv; (*ap = strsep(&input, " \t\n")) != ((void *)0);)
            if (**ap != '\0') {
                argc++;
                if (++ap >= &argv[sizeof command->argv]) break;
            }
        if (argc == 0) {
            return -1;
        }
        if (strcmp(argv[argc - 1], "&") == 0) {
            command->background = 1;
            argv[argc - 1] = '\0';
        } else {
            command->background = 0;
        }
        return argc;
    }
    return -1;
}

variable_types: struct command_t { char line[128]; char *argv[10]; int background; } *

variable_types:
ap
char **

variable_types:
input
char *


decompiled_code:
__int64 __fastcall check_bg_processes(unsigned int a1)
{
int v1; // ebp
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = waitpid(-1, &stat_loc, 1);
checkerr(v1, "waitpid() failed unexpectedly");
result = a1;
if ( v1 > 0 )
{
if ( BYTE1(stat_loc) )
printf("[?] %d exit %d\n", (unsigned int)v1, BYTE1(stat_loc));
else
printf("[?] %d done\n", (unsigned int)v1);
result = a1 - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1)
{
int v1; // ebp
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = waitpid(-1, &stat_loc, 1);
func7(v1, "waitpid() failed unexpectedly");
result = a1;
if ( v1 > 0 )
{
if ( BYTE1(stat_loc) )
printf("[?] %d exit %d\n", (unsigned int)v1, BYTE1(stat_loc));
else
printf("[?] %d done\n", (unsigned int)v1);
result = a1 - 1;
}
return result;
}

original_code:
int check_bg_processes(int num_processes) {
    int status;
    pid_t pid = waitpid(-1, &status, 1);
    checkerr(pid, "waitpid() failed unexpectedly");
    if (pid > 0) {
        if ((((status) & 0xff00) >> 8) == 0) {
            printf("[?] %d done\n", pid);
        } else {
            printf("[?] %d exit %d\n", pid, (((status) & 0xff00) >> 8));
        }
        num_processes--;
    }
    return num_processes;
}

canonical_original_code:
int check_bg_processes(int num_processes) {
    int status;
    int pid = waitpid(-1, &status, 1);
    checkerr(pid, "waitpid() failed unexpectedly");
    if (pid > 0) {
        if ((((status) & 0xff00) >> 8) == 0) {
            printf("[?] %d done\n", pid);
        } else {
            printf("[?] %d exit %d\n", pid, (((status) & 0xff00) >> 8));
        }
        num_processes--;
    }
    return num_processes;
}


cd

decompiled_code:
int __fastcall cd(const char *a1)
{
int result; // eax

result = chdir(a1);
if ( result == -1 )
{
if ( *__errno_location() == 2 )
result = printf("kjell: no such file or directory: %s\n", a1);
else
result = puts("kjell: failed to change directory");
}
return result;
}

canonical_decompiled_code:
int __fastcall func13(const char *a1)
{
int result; // eax

result = chdir(a1);
if ( result == -1 )
{
if ( *__errno_location() == 2 )
result = printf("kjell: no such file or directory: %s\n", a1);
else
result = puts("kjell: failed to change directory");
}
return result;
}

original_code:
void cd(char *dir) {
    if (chdir(dir) == -1) {
        if ((*__errno_location ()) == 2) {
            printf("kjell: no such file or directory: %s\n", dir);
        } else {
            printf("kjell: failed to change directory\n");
        }
    }
}

variable_types:
dir
char *


decompiled_code:
__int64 __fastcall execute_builtin(__int64 a1, __int64 a2)
{
__int64 result; // rax

if ( *(_BYTE *)a1 == 99 && *(_BYTE *)(a1 + 1) == 100 && !*(_BYTE *)(a1 + 2) )
{
cd(*(const char **)(a2 + 8));
result = 1LL;
}
else
{
if ( !strcmp((const char *)a1, "exit") )
exit(1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2)
{
__int64 result; // rax

if ( *(_BYTE *)a1 == 99 && *(_BYTE *)(a1 + 1) == 100 && !*(_BYTE *)(a1 + 2) )
{
func13(*(const char **)(a2 + 8));
result = 1LL;
}
else
{
if ( !strcmp((const char *)a1, "exit") )
exit(1);
result = 0LL;
}
return result;
}

original_code:
int execute_builtin(char *name, char **argv) {
    if (strcmp(name, "cd") == 0) {
        cd(argv[1]);
    } else if (strcmp(name, "exit") == 0) {
        exit(1);
    } else {
        return 0;
    }
    return 1;
}

variable_types:
name
char *


decompiled_code:
char *__fastcall resolve_home(char *s1)
{
char *v1; // r12
char *v2; // rax
const char *v3; // rbp
size_t v4; // rbx

v1 = s1;
v2 = getenv("HOME");
if ( v2 )
{
v3 = v2;
v4 = strlen(v2);
if ( !strncmp(s1, v3, v4) )
{
v1 = &s1[v4 - 1];
*v1 = 126;
}
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func15(char *s1)
{
char *v1; // r12
char *v2; // rax
const char *v3; // rbp
size_t v4; // rbx

v1 = s1;
v2 = getenv("HOME");
if ( v2 )
{
v3 = v2;
v4 = strlen(v2);
if ( !strncmp(s1, v3, v4) )
{
v1 = &s1[v4 - 1];
*v1 = 126;
}
}
return v1;
}

original_code:
char* resolve_home(char* path) {
    char* home = getenv("HOME");
    if (home == ((void *)0)) {
        return path;
    }
    size_t home_len = strlen(home);
    if (strncmp(path, home, home_len) == 0) {
        path[home_len - 1] = '~';
        return &path[home_len - 1];
    } else {
        return path;
    }
}

canonical_original_code:
char *resolve_home(char *path) {
    char *home = getenv("HOME");
    if (home == ((void *)0)) {
        return path;
    }
    unsigned long home_len = strlen(home);
    if (strncmp(path, home, home_len) == 0) {
        path[home_len - 1] = '~';
        return &path[home_len - 1];
    } else {
        return path;
    }
}

variable_types:
path
char *

variable_types:
home
char *

return_type: char *


decompiled_code:
int print_prompt()
{
char *v0; // rax
char v2[136]; // [rsp+0h] [rbp-88h] BYREF

if ( !getcwd(v2, 0x80uLL) )
return printf("$ ");
v0 = resolve_home(v2);
return printf("%s$ ", v0);
}

canonical_decompiled_code:
int func16()
{
char *v0; // rax
char v2[136]; // [rsp+0h] [rbp-88h] BYREF

if ( !getcwd(v2, 0x80uLL) )
return printf("$ ");
v0 = func15(v2);
return printf("%s$ ", v0);
}

original_code:
void print_prompt() {
    char path[128];
    if (getcwd(path, 128) != ((void *)0)) {
        printf("%s$ ", resolve_home(path));
    } else {
        printf("$ ");
    }
}

variable_types:
path
char [128]


repo: leyafo/practice-algorithm

decompiled_code:
__int64 __fastcall cmp_int(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = 1LL;
if ( *a1 <= *a2 )
result = (unsigned int)-(*a1 < *a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = 1LL;
if ( *a1 <= *a2 )
result = (unsigned int)-(*a1 < *a2);
return result;
}

original_code:
static int
cmp_int(void *value1, void *value2)
{
    int A = *(int *) value1;
    int B = *(int *) value2;
    if(A > B)
        return 1;
    if(A < B)
        return -1;
    return 0;
}

variable_types: A int

variable_types: B int


decompiled_code:
int __fastcall print_int(unsigned int *a1)
{
return printf("node data is: %d\n", *a1);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int *a1)
{
return printf("node data is: %d\n", *a1);
}

original_code:
static void
print_int(void *value)
{
    printf("node data is: %d\n", *(int *) value);
}

variable_types:
value
void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
rb_tree_test();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func5();
return 0;
}

original_code:
int
main(int argc, char* argv[])
{
    rb_tree_test();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    rb_tree_test();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


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

if ( a1 )
result = *(_BYTE *)(a1 + 16) != 0;
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *(_BYTE *)(a1 + 16) != 0;
else
result = 0LL;
return result;
}

original_code:
int
is_red(struct tree_node *n)
{
    if(n == ((void *)0) || n->color == 0)
        return 0;
    return 1;
}

variable_types: struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; } *


original_code:
void *
rb_tree_remove(struct rb_tree *t, void *data)
{
    void *delete_data = ((void *)0);
    t->root = _node_remove(t->root, data, &delete_data, t->cmp_func);
    t->root->color = 0;
    return delete_data;
}

canonical_original_code:
void *rb_tree_remove(struct rb_tree *t, void *data)
{
    void *delete_data = ((void *)0);
    t->root = _node_remove(t->root, data, &delete_data, t->cmp_func);
    t->root->color = 0;
    return delete_data;
}

variable_types: struct rb_tree { struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; } *root; int (*cmp_func)(void *, void *); } *

variable_types:
data
void *

return_type: void *

user_defined_types: struct rb_tree { struct tree_node *root; int (*cmp_func)(void *, void *); }


decompiled_code:
_BOOL8 __fastcall rb_tree_empty(_QWORD *a1)
{
_BOOL8 result; // rax

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

canonical_decompiled_code:
_BOOL8 __fastcall func6(_QWORD *a1)
{
_BOOL8 result; // rax

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

original_code:
int
rb_tree_empty(struct rb_tree *t){
    return t == ((void *)0) || t->root == ((void *)0);
}

user_defined_types: struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; }


decompiled_code:
void __fastcall rb_tree_ascend_iterate(void **a1, void (__fastcall *a2)(void *, __int64))
{
tree_iterate(*a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func7(void **a1, void (__fastcall *a2)(void *, __int64))
{
func0(*a1, 1, a2);
}

original_code:
void
rb_tree_ascend_iterate(struct rb_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root,1, iterate_func);
}


decompiled_code:
void __fastcall rb_tree_descend_iterate(void **a1, void (__fastcall *a2)(void *, __int64))
{
tree_iterate(*a1, 0, a2);
}

canonical_decompiled_code:
void __fastcall func8(void **a1, void (__fastcall *a2)(void *, __int64))
{
func0(*a1, 0, a2);
}

original_code:
void
rb_tree_descend_iterate(struct rb_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root,0, iterate_func);
}


decompiled_code:
void __fastcall rb_tree_release(void ***a1)
{
tree_iterate(**a1, 2, 0LL);
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func9(void ***a1)
{
func0(**a1, 2, 0LL);
free(*a1);
*a1 = 0LL;
}

original_code:
void rb_tree_release(struct rb_tree **t){
    _tree_iterate((*t)->root, 2, ((void *)0));
    (*t)->root = ((void *)0);
    free(*t);
    *t = ((void *)0);
}

variable_types: struct rb_tree { struct tree_node { struct tree_node *left; struct tree_node *right; char color; void *data; } *root; int (*cmp_func)(void *, void *); } **


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

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 24);
}
return result;
}

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

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 24);
}
return result;
}

original_code:
void *
rb_tree_find_max(struct rb_tree *t)
{
    if (t->root == ((void *)0))
        return ((void *)0);
    struct tree_node *n = t->root;
    while(n->right != ((void *)0)){
        n = n->right;
    }
    return n->data;
}

canonical_original_code:
void *rb_tree_find_max(struct rb_tree *t)
{
    if (t->root == ((void *)0))
        return ((void *)0);
    struct tree_node *n = t->root;
    while(n->right != ((void *)0)){
        n = n->right;
    }
    return n->data;
}


decompiled_code:
_BOOL8 __fastcall binary_tree_empty(_QWORD *a1)
{
_BOOL8 result; // rax

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

canonical_decompiled_code:
_BOOL8 __fastcall func2(_QWORD *a1)
{
_BOOL8 result; // rax

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

original_code:
int
binary_tree_empty(struct binary_tree *t) {
    if(t)
        return t->root == ((void *)0);
    return 1;
}

variable_types: struct binary_tree { struct tree_node { void *data; struct tree_node *left; struct tree_node *right; struct tree_node *parent; struct list { struct list *next; void *data; } *equal_list; } *root; int (*cmp_func)(void *, void *); int count; } *

user_defined_types: struct binary_tree { struct tree_node *root; int (*cmp_func)(void *, void *); int count; }

user_defined_types: struct tree_node { void *data; struct tree_node *left; struct tree_node *right; struct tree_node *parent; struct list *equal_list; }


decompiled_code:
void __fastcall binary_tree_descend_iterate(void **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 0, a2);
}

canonical_decompiled_code:
void __fastcall func3(void **a1, void (__fastcall *a2)(_QWORD))
{
func1(*a1, 0, a2);
}

original_code:
void
binary_tree_descend_iterate(struct binary_tree *t, void (*iterate_func)(void *)) {
    _tree_iterate(t->root, 0, iterate_func);
}

user_defined_types: struct list { struct list *next; void *data; }


decompiled_code:
void __fastcall binary_tree_ascend_iterate(void **a1, void (__fastcall *a2)(_QWORD))
{
tree_iterate(*a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func4(void **a1, void (__fastcall *a2)(_QWORD))
{
func1(*a1, 1, a2);
}

original_code:
void
binary_tree_ascend_iterate(struct binary_tree *t, void (*iterate_func)(void *)) {
    _tree_iterate(t->root, 1, iterate_func);
}


decompiled_code:
_BOOL8 __fastcall binary_tree_find(__int64 a1, __int64 a2)
{
return tree_find(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD, __int64))(a1 + 8)) != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1, __int64 a2)
{
return tree_find(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD, __int64))(a1 + 8)) != 0LL;
}

original_code:
int
binary_tree_find(struct binary_tree *t, void *data) {
    if(_tree_find(t->root, data, t->cmp_func))
        return 1;
    return 0;
}


decompiled_code:
void __fastcall binary_tree_release(void ***a1)
{
tree_iterate(**a1, 2, 0LL);
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func6(void ***a1)
{
func1(**a1, 2, 0LL);
free(*a1);
*a1 = 0LL;
}

original_code:
void
binary_tree_release(struct binary_tree **t) {
    _tree_iterate((*t)->root, 2, ((void *)0));
    (*t)->count = 0;
    (*t)->root = ((void *)0);
    free(*t);
    *t = ((void *)0);
}

variable_types: struct binary_tree { struct tree_node { void *data; struct tree_node *left; struct tree_node *right; struct tree_node *parent; struct list { struct list *next; void *data; } *equal_list; } *root; int (*cmp_func)(void *, void *); int count; } **


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

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

original_code:
int
binary_tree_count(struct binary_tree *t) {
    return t->count;
}


original_code:
void
avl_tree_insert(struct avl_tree *t, void *data)
{
    struct tree_node **n = &t->root, *parent = ((void *)0);
    while(1) {
        if(*n == ((void *)0)) {
            *n = malloc(sizeof(struct tree_node));
            (*n)->parent = parent;
            (*n)->left = (*n)->right = ((void *)0);
            (*n)->height = 1;
            (*n)->data = data;
            break;
        }
        parent = *n;
        int cmp_result = (*t->cmp_func)((*n)->data,data);
        if( cmp_result > 0)
            n = &(*n)->left;
        else if(cmp_result < 0)
            n = &(*n)->right;
        else
            return;
    }
    ++t->count;
    _keep_balance(t, *n);
}

canonical_original_code:
void avl_tree_insert(struct avl_tree *t, void *data)
{
    struct tree_node **n = &t->root; struct tree_node *parent = ((void *)0);
    while(1) {
        if(*n == ((void *)0)) {
            *n = malloc(sizeof(struct tree_node));
            (*n)->parent = parent;
            (*n)->left = (*n)->right = ((void *)0);
            (*n)->height = 1;
            (*n)->data = data;
            break;
        }
        parent = *n;
        int cmp_result = (*t->cmp_func)((*n)->data,data);
        if( cmp_result > 0)
            n = &(*n)->left;
        else if(cmp_result < 0)
            n = &(*n)->right;
        else
            return;
    }
    ++t->count;
    _keep_balance(t, *n);
}

variable_types: struct avl_tree { struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } *root; int count; int (*cmp_func)(void *, void *); } *

variable_types: struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } **

user_defined_types: struct avl_tree { struct tree_node *root; int count; int (*cmp_func)(void *, void *); }


original_code:
void *
avl_tree_remove(struct avl_tree *t, void *data)
{
    struct tree_node *erase_node = _tree_find(t->root, data, t->cmp_func);
    void * remove_data = ((void *)0);
    if(erase_node)
    {
        struct tree_node *free_node;
        if(erase_node->left == ((void *)0) && erase_node->right == ((void *)0)){
            _set_parent(erase_node->parent, erase_node, ((void *)0));
            if(t->root == erase_node)
                t->root = ((void *)0);
            free_node = erase_node;
            goto keep_balance;
        }
        struct tree_node *it = erase_node;
        if(it->right != ((void *)0)) {
            it = it->right;
            while(it->left)
                it = it->left;
            void * tmp = erase_node->data;
            erase_node->data = it->data;
            it->data = tmp;
            if(it->right){
                _set_parent(it->parent, it, it->right);
            } else {
                _set_parent(it->parent, it, ((void *)0));
            }
        } else {
            _set_parent(it->parent, it, it->left);
            if(t->root == it) {
                t->root = it->left;
            }
        }
        free_node = it;
    keep_balance:
        {
            _keep_balance(t, free_node);
            remove_data = free_node->data;
            free(free_node);
            --t->count;
        }
    }
    return remove_data;
}

canonical_original_code:
void *avl_tree_remove(struct avl_tree *t, void *data)
{
    struct tree_node *erase_node = _tree_find(t->root, data, t->cmp_func);
    void *remove_data = ((void *)0);
    if(erase_node)
    {
        struct tree_node *free_node;
        if(erase_node->left == ((void *)0) && erase_node->right == ((void *)0)){
            _set_parent(erase_node->parent, erase_node, ((void *)0));
            if(t->root == erase_node)
                t->root = ((void *)0);
            free_node = erase_node;
            goto keep_balance;
        }
        struct tree_node *it = erase_node;
        if(it->right != ((void *)0)) {
            it = it->right;
            while(it->left)
                it = it->left;
            void *tmp = erase_node->data;
            erase_node->data = it->data;
            it->data = tmp;
            if(it->right){
                _set_parent(it->parent, it, it->right);
            } else {
                _set_parent(it->parent, it, ((void *)0));
            }
        } else {
            _set_parent(it->parent, it, it->left);
            if(t->root == it) {
                t->root = it->left;
            }
        }
        free_node = it;
    keep_balance:
        {
            _keep_balance(t, free_node);
            remove_data = free_node->data;
            free(free_node);
            --t->count;
        }
    }
    return remove_data;
}

variable_types: struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } *

variable_types:
tmp
void *


decompiled_code:
_BOOL8 __fastcall avl_tree_empty(_QWORD *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *a1 == 0LL;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(_QWORD *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *a1 == 0LL;
else
result = 0LL;
return result;
}

original_code:
int
avl_tree_empty(struct avl_tree *t)
{
    if(!t)
        return 0;
    return t->root == ((void *)0);
}

user_defined_types: struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; }


decompiled_code:
__int64 __fastcall avl_tree_count(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int
avl_tree_count(struct avl_tree *t)
{
    return t->count;
}


decompiled_code:
void __fastcall avl_tree_ascend_iterate(void **a1, void (__fastcall *a2)(_QWORD, __int64))
{
tree_iterate(*a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func11(void **a1, void (__fastcall *a2)(_QWORD, __int64))
{
func6(*a1, 1, a2);
}

original_code:
void
avl_tree_ascend_iterate(struct avl_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root, 1, iterate_func);
}


decompiled_code:
void __fastcall avl_tree_descend_iterate(void **a1, void (__fastcall *a2)(_QWORD, __int64))
{
tree_iterate(*a1, 0, a2);
}

canonical_decompiled_code:
void __fastcall func12(void **a1, void (__fastcall *a2)(_QWORD, __int64))
{
func6(*a1, 0, a2);
}

original_code:
void
avl_tree_descend_iterate(struct avl_tree *t, void (*iterate_func)(void *))
{
    _tree_iterate(t->root, 0, iterate_func);
}


decompiled_code:
_BOOL8 __fastcall avl_tree_find(__int64 a1, __int64 a2)
{
return tree_find(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD, __int64))(a1 + 16)) != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(__int64 a1, __int64 a2)
{
return tree_find(*(_QWORD **)a1, a2, *(__int64 (__fastcall **)(_QWORD, __int64))(a1 + 16)) != 0LL;
}

original_code:
int
avl_tree_find(struct avl_tree *t, void *data)
{
    if(_tree_find(t->root, data, t->cmp_func))
        return 1;
    return 0;
}


decompiled_code:
void __fastcall avl_tree_release(void ***a1)
{
tree_iterate(**a1, 2, 0LL);
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func14(void ***a1)
{
func6(**a1, 2, 0LL);
free(*a1);
*a1 = 0LL;
}

original_code:
void
avl_tree_release(struct avl_tree **t)
{
    _tree_iterate((*t)->root, 2, ((void *)0));
    (*t)->count = 0;
    (*t)->root = ((void *)0);
    free(*t);
    *t = ((void *)0);
}

variable_types: struct avl_tree { struct tree_node { struct tree_node *left; struct tree_node *right; struct tree_node *parent; unsigned int height; void *data; } *root; int count; int (*cmp_func)(void *, void *); } **


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

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 32);
}
return result;
}

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

result = *a1;
if ( *a1 )
{
do
{
v2 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *(_QWORD *)(v2 + 32);
}
return result;
}

original_code:
void *
avl_tree_find_max(struct avl_tree *t)
{
    struct tree_node *n = t->root;
    if(!n)
        return ((void *)0);
    while(n->right)
        n = n->right;
    return n->data;
}

canonical_original_code:
void *avl_tree_find_max(struct avl_tree *t)
{
    struct tree_node *n = t->root;
    if(!n)
        return ((void *)0);
    while(n->right)
        n = n->right;
    return n->data;
}


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

result = malloc(0x10uLL);
*(_DWORD *)result = 0;
result[1] = 0LL;
return result;
}

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

result = malloc(0x10uLL);
*(_DWORD *)result = 0;
result[1] = 0LL;
return result;
}

original_code:
struct stack*
stack_init(void) {
    struct stack *stk;
    stk = malloc(sizeof(struct stack));
    stk->count = 0;
    stk->head = ((void *)0);
    return stk;
}

canonical_original_code:
struct stack *stack_init() {
    struct stack *stk;
    stk = malloc(sizeof(struct stack));
    stk->count = 0;
    stk->head = ((void *)0);
    return stk;
}

variable_types: struct stack { int count; struct element { void *data; struct element *link; } *head; } *

user_defined_types: struct stack { int count; struct element *head; }


decompiled_code:
_BOOL8 __fastcall stack_empty(_DWORD *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_DWORD *a1)
{
return *a1 == 0;
}

original_code:
int
stack_empty(struct stack *stk) {
    return stk->count == 0;
}

user_defined_types: struct element { void *data; struct element *link; }


original_code:
void *
stack_pop(struct stack *stk) {
    void *data;
    struct element *e;
    e = stk->head;
    stk->head = e->link;
    stk->count--;
    data = e->data;
    free(e);
    return data;
}

canonical_original_code:
void *stack_pop(struct stack *stk) {
    void *data;
    struct element *e;
    e = stk->head;
    stk->head = e->link;
    stk->count--;
    data = e->data;
    free(e);
    return data;
}

variable_types: struct element { void *data; struct element *link; } *


decompiled_code:
void __fastcall stack_release(_QWORD **a1)
{
_QWORD *v2; // rdi
_QWORD *v3; // rbx
void *v4; // rdi

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

canonical_decompiled_code:
void __fastcall func3(_QWORD **a1)
{
_QWORD *v2; // rdi
_QWORD *v3; // rbx
void *v4; // rdi

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

original_code:
void
stack_release(struct stack **stk) {
    struct element *e, *u;
    e = (*stk)->head;
    while(e) {
        u = e;
        e = e->link;
        free(u);
    }
    free(*stk);
    *stk = ((void *)0);
}

canonical_original_code:
void stack_release(struct stack **stk) {
    struct element *e; struct element *u;
    e = (*stk)->head;
    while(e) {
        u = e;
        e = e->link;
        free(u);
    }
    free(*stk);
    *stk = ((void *)0);
}

variable_types: struct stack { int count; struct element { void *data; struct element *link; } *head; } **


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
char *v4; // rax
const char *v5; // r12
__int64 v6; // rbp
_QWORD *v7; // rax
_QWORD *v8; // r13
__int64 i; // rbx
_QWORD *v10; // rax
_QWORD *v11; // r13
__int64 j; // rbx
__int64 v13; // r13
int result; // eax

v3 = getenv("AWS_ACCESS_KEY_ID");
v4 = getenv("AWS_SECRET_KEY");
if ( argc <= 1 )
{
fwrite("Usage: s3test <bucket>\n", 1uLL, 0x17uLL, stderr);
result = 1;
}
else if ( v3 && v4 )
{
v5 = argv[1];
v6 = s3_init(v3, v4, "s3.amazonaws.com");
puts("Listing bucket root");
v7 = (_QWORD *)s3_list_bucket(v6, v5, 0LL);
v8 = v7;
if ( v7 )
{
for ( i = *v7; i; i = *(_QWORD *)(i + 32) )
printf("\tKey %s\n", *(const char **)i);
s3_bucket_entries_free(v8);
}
puts("Listing /foo/bar/");
v10 = (_QWORD *)s3_list_bucket(v6, v5, "foo/bar/");
v11 = v10;
if ( v10 )
{
for ( j = *v10; j; j = *(_QWORD *)(j + 32) )
printf("\tKey %s\n", *(const char **)j);
s3_bucket_entries_free(v11);
}
puts("Uploading foo.txt");
s3_put(v6, v5, "foo.txt", "text/plain", "foo bar gazonk", 14LL);
puts("Downloading foo.txt");
v13 = s3_string_init();
s3_get(v6, v5, "foo.txt", v13);
printf("Contents:\n%.*s\n", *(_DWORD *)(v13 + 8), *(const char **)v13);
s3_string_free(v13);
puts("Deleting foo.txt");
s3_delete(v6, v5, "foo.txt");
s3_free(v6);
result = 0;
}
else
{
fwrite("Error: Environment variable AWS_ACCESS_KEY_ID or AWS_SECRET_KEY not set\n", 1uLL, 0x48uLL, stderr);
result = 1;
}
return result;
}

original_code:
int main (int argc, char **argv) {
 struct S3 *s3;
 struct s3_string *out;
 struct s3_bucket_entry_head *bkt_entries;
 struct s3_bucket_entry *e;
 char *file_contents = "foo bar gazonk";
 char *s3_key_id = getenv("AWS_ACCESS_KEY_ID");
 char *s3_secret = getenv("AWS_SECRET_KEY");
 if (argc < 2) {
  fprintf(stderr, "Usage: s3test <bucket>\n");
  return 1;
 }
 if (s3_key_id == ((void *)0) || s3_secret == ((void *)0)) {
  fprintf(stderr, "Error: Environment variable AWS_ACCESS_KEY_ID or AWS_SECRET_KEY not set\n");
  return 1;
 }
 char *bucket = argv[1];
 s3 = s3_init(s3_key_id, s3_secret, "s3.amazonaws.com");
 printf("Listing bucket root\n");
 bkt_entries = s3_list_bucket(s3, bucket, ((void *)0));
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Listing /foo/bar/\n");
 bkt_entries = s3_list_bucket(s3, bucket, "foo/bar/");
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Uploading foo.txt\n");
 s3_put(s3, bucket, "foo.txt", "text/plain", file_contents, strlen(file_contents));
 printf("Downloading foo.txt\n");
 out = s3_string_init();
 s3_get(s3, bucket, "foo.txt", out);
 printf("Contents:\n%.*s\n", (int)out->len, out->ptr);
 s3_string_free(out);
 printf("Deleting foo.txt\n");
 s3_delete(s3, bucket, "foo.txt");
 s3_free(s3);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 struct S3 *s3;
 struct s3_string *out;
 struct s3_bucket_entry_head *bkt_entries;
 struct s3_bucket_entry *e;
 char *file_contents = "foo bar gazonk";
 char *s3_key_id = getenv("AWS_ACCESS_KEY_ID");
 char *s3_secret = getenv("AWS_SECRET_KEY");
 if (argc < 2) {
  fprintf(stderr, "Usage: s3test <bucket>\n");
  return 1;
 }
 if (s3_key_id == ((void *)0) || s3_secret == ((void *)0)) {
  fprintf(stderr, "Error: Environment variable AWS_ACCESS_KEY_ID or AWS_SECRET_KEY not set\n");
  return 1;
 }
 char *bucket = argv[1];
 s3 = s3_init(s3_key_id, s3_secret, "s3.amazonaws.com");
 printf("Listing bucket root\n");
 bkt_entries = s3_list_bucket(s3, bucket, ((void *)0));
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Listing /foo/bar/\n");
 bkt_entries = s3_list_bucket(s3, bucket, "foo/bar/");
 if (bkt_entries != ((void *)0)) {
  for ((e) = ((bkt_entries)->tqh_first); (e); (e) = ((e)->list.tqe_next)) {
   printf("\tKey %s\n", e->key);
  }
  s3_bucket_entries_free(bkt_entries);
 }
 printf("Uploading foo.txt\n");
 s3_put(s3, bucket, "foo.txt", "text/plain", file_contents, strlen(file_contents));
 printf("Downloading foo.txt\n");
 out = s3_string_init();
 s3_get(s3, bucket, "foo.txt", out);
 printf("Contents:\n%.*s\n", (int)out->len, out->ptr);
 s3_string_free(out);
 printf("Deleting foo.txt\n");
 s3_delete(s3, bucket, "foo.txt");
 s3_free(s3);
 return 0;
}

variable_types:
argv
char **

variable_types: struct S3 { char *secret; char *id; char *base_url; char *proxy; } *

variable_types: struct s3_string { char *ptr; unsigned long len; unsigned long uploaded; } *

variable_types: struct s3_bucket_entry_head { struct s3_bucket_entry { char *key; char *lastmod; unsigned long size; char *etag; struct { struct s3_bucket_entry *tqe_next; struct s3_bucket_entry **tqe_prev; } list; } *tqh_first; struct s3_bucket_entry **tqh_last; } *

variable_types:
bucket
char *

user_defined_types: struct s3_bucket_entry_head { struct s3_bucket_entry *tqh_first; struct s3_bucket_entry **tqh_last; }


decompiled_code:
size_t __fastcall s3_string_curl_writefunc(void *src, __int64 a2, __int64 a3, __int64 a4)
{
size_t v4; // r12
__int64 v6; // r13
char *v7; // rax
size_t result; // rax

v4 = a3 * a2;
v6 = a3 * a2 + *(_QWORD *)(a4 + 8);
v7 = (char *)realloc(*(void **)a4, v6 + 1);
*(_QWORD *)a4 = v7;
if ( !v7 )
{
fwrite("realloc() failed\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
memcpy(&v7[*(_QWORD *)(a4 + 8)], src, v4);
*(_BYTE *)(*(_QWORD *)a4 + v6) = 0;
result = v4;
*(_QWORD *)(a4 + 8) = v6;
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(void *src, __int64 a2, __int64 a3, __int64 a4)
{
size_t v4; // r12
__int64 v6; // r13
char *v7; // rax
size_t result; // rax

v4 = a3 * a2;
v6 = a3 * a2 + *(_QWORD *)(a4 + 8);
v7 = (char *)realloc(*(void **)a4, v6 + 1);
*(_QWORD *)a4 = v7;
if ( !v7 )
{
fwrite("realloc() failed\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
memcpy(&v7[*(_QWORD *)(a4 + 8)], src, v4);
*(_BYTE *)(*(_QWORD *)a4 + v6) = 0;
result = v4;
*(_QWORD *)(a4 + 8) = v6;
return result;
}

original_code:
size_t
s3_string_curl_writefunc(void *ptr, size_t len, size_t nmemb, struct s3_string *s) {
 size_t new_len = s->len + len *nmemb;
 s->ptr = realloc(s->ptr, new_len + 1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "realloc() failed\n");
  exit(1);
 }
 memcpy(s->ptr+s->len, ptr, len*nmemb);
 s->ptr[new_len] = '\0';
 s->len = new_len;
 return len*nmemb;
}

canonical_original_code:
unsigned long s3_string_curl_writefunc(void *ptr, unsigned long len, unsigned long nmemb, struct s3_string *s) {
 unsigned long new_len = s->len + len *nmemb;
 s->ptr = realloc(s->ptr, new_len + 1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "realloc() failed\n");
  exit(1);
 }
 memcpy(s->ptr+s->len, ptr, len*nmemb);
 s->ptr[new_len] = '\0';
 s->len = new_len;
 return len*nmemb;
}

variable_types:
ptr
void *

variable_types:
len
unsigned long

variable_types:
nmemb
unsigned long

return_type: unsigned long

user_defined_types: struct s3_string { char *ptr; unsigned long len; unsigned long uploaded; }


decompiled_code:
size_t __fastcall s3_string_curl_readfunc(void *a1, __int64 a2, __int64 a3, _QWORD *a4)
{
size_t v4; // r12
__int64 v6; // rsi

v4 = a3 * a2;
v6 = a4[2];
if ( a4[1] - v6 <= v4 )
v4 = a4[1] - v6;
memcpy(a1, (const void *)(*a4 + v6), v4);
a4[2] += v4;
return v4;
}

original_code:
size_t
s3_string_curl_readfunc(void *ptr, size_t len, size_t nmemb, struct s3_string *s) {
 size_t left = s->len - s->uploaded;
 size_t max_chunk = len * nmemb;
 size_t retcode = left < max_chunk ? left : max_chunk;
 memcpy(ptr, s->ptr + s->uploaded, retcode);
 s->uploaded += retcode;
 return retcode;
}

canonical_original_code:
unsigned long s3_string_curl_readfunc(void *ptr, unsigned long len, unsigned long nmemb, struct s3_string *s) {
 unsigned long left = s->len - s->uploaded;
 unsigned long max_chunk = len * nmemb;
 unsigned long retcode = left < max_chunk ? left : max_chunk;
 memcpy(ptr, s->ptr + s->uploaded, retcode);
 s->uploaded += retcode;
 return retcode;
}

variable_types:
left
unsigned long

variable_types:
retcode
unsigned long


decompiled_code:
_QWORD *s3_string_init()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
_BYTE *v2; // rax

v0 = malloc(0x18uLL);
v0[1] = 0LL;
v1 = v0;
v0[2] = 0LL;
v2 = malloc(1uLL);
*v1 = v2;
if ( !v2 )
{
fwrite("malloc() failed\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
*v2 = 0;
return v1;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
_BYTE *v2; // rax

v0 = malloc(0x18uLL);
v0[1] = 0LL;
v1 = v0;
v0[2] = 0LL;
v2 = malloc(1uLL);
*v1 = v2;
if ( !v2 )
{
fwrite("malloc() failed\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
*v2 = 0;
return v1;
}

original_code:
struct s3_string *
s3_string_init(void) {
 struct s3_string *s;
 s = malloc(sizeof (struct s3_string));
 s->len = 0;
 s->uploaded = 0;
 s->ptr = malloc(s->len+1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "malloc() failed\n");
  exit(1);
 }
 s->ptr[0] = '\0';
 return s;
}

canonical_original_code:
struct s3_string *s3_string_init() {
 struct s3_string *s;
 s = malloc(sizeof (struct s3_string));
 s->len = 0;
 s->uploaded = 0;
 s->ptr = malloc(s->len+1);
 if (s->ptr == ((void *)0)) {
  fprintf(stderr, "malloc() failed\n");
  exit(1);
 }
 s->ptr[0] = '\0';
 return s;
}

return_type: struct s3_string { char *ptr; unsigned long len; unsigned long uploaded; } *


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

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

original_code:
void
s3_string_free(struct s3_string *str) {
 free(str->ptr);
 free(str);
}


decompiled_code:
void __fastcall s3_free(void *ptr)
{
free(*((void **)ptr + 1));
free(*(void **)ptr);
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*((void **)ptr + 1));
free(*(void **)ptr);
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void
s3_free(struct S3 *s3) {
 free(s3->id);
 free(s3->secret);
 free(s3->base_url);
 free(s3);
}

user_defined_types: struct S3 { char *secret; char *id; char *base_url; char *proxy; }


decompiled_code:
char *s3_make_date()
{
char *v0; // r12
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
struct tm tp; // [rsp+10h] [rbp-48h] BYREF

v0 = (char *)malloc(0x80uLL);
timer = time(0LL);
v1 = gmtime(&timer);
*(__m128i *)&tp.tm_sec = _mm_loadu_si128((const __m128i *)v1);
*(__m128i *)&tp.tm_mon = _mm_loadu_si128((const __m128i *)&v1->tm_mon);
*(__m128i *)&tp.tm_isdst = _mm_loadu_si128((const __m128i *)&v1->tm_isdst);
tp.tm_zone = v1->tm_zone;
strftime(v0, 0x80uLL, "%a, %d %b %Y %H:%M:%S %Z", &tp);
return v0;
}

canonical_decompiled_code:
char *func3()
{
char *v0; // r12
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-50h] BYREF
struct tm tp; // [rsp+10h] [rbp-48h] BYREF

v0 = (char *)malloc(0x80uLL);
timer = time(0LL);
v1 = gmtime(&timer);
*(__m128i *)&tp.tm_sec = _mm_loadu_si128((const __m128i *)v1);
*(__m128i *)&tp.tm_mon = _mm_loadu_si128((const __m128i *)&v1->tm_mon);
*(__m128i *)&tp.tm_isdst = _mm_loadu_si128((const __m128i *)&v1->tm_isdst);
tp.tm_zone = v1->tm_zone;
strftime(v0, 0x80uLL, "%a, %d %b %Y %H:%M:%S %Z", &tp);
return v0;
}

original_code:
char *
s3_make_date(void) {
 char *date;
 time_t now;
 struct tm tm;
 date = malloc(128);
 now = time(0);
 tm = *gmtime(&now);
 strftime(date, 128, "%a, %d %b %Y %H:%M:%S %Z", &tm);
 return date;
}

canonical_original_code:
char *s3_make_date() {
 char *date;
 long now;
 struct tm tm;
 date = malloc(128);
 now = time(0);
 tm = *gmtime(&now);
 strftime(date, 128, "%a, %d %b %Y %H:%M:%S %Z", &tm);
 return date;
}

variable_types:
date
char *

variable_types: now long

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


decompiled_code:
void __fastcall s3_get(__int64 a1, const char *a2, const char *a3, __int64 a4)
{
char *v6; // rbp
void *v7; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v6 = s3_make_date();
asprintf((char **)&v7, "%s\n\n\n%s\n/%s/%s", "GET", v6, a2, a3);
asprintf(&ptr, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
s3_perform_op(a1, "GET", (__int64)ptr, (const char *)v7, v6, a4, 0LL, 0LL, 0LL);
free(v7);
free(v6);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, const char *a2, const char *a3, __int64 a4)
{
char *v6; // rbp
void *v7; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v6 = func3();
asprintf((char **)&v7, "%s\n\n\n%s\n/%s/%s", "GET", v6, a2, a3);
asprintf(&ptr, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
func4(a1, "GET", (__int64)ptr, (const char *)v7, v6, a4, 0LL, 0LL, 0LL);
free(v7);
free(v6);
free(ptr);
}

original_code:
void
s3_get(struct S3 *s3, const char *bucket, const char *key, struct s3_string *out) {
 const char *method = "GET";
 char *sign_data;
 char *date;
 char *url;
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 free(sign_data);
 free(date);
 free(url);
}

canonical_original_code:
void s3_get(struct S3 *s3, char *bucket, char *key, struct s3_string *out) {
 char *method = "GET";
 char *sign_data;
 char *date;
 char *url;
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 free(sign_data);
 free(date);
 free(url);
}

variable_types:
key
char *

variable_types:
method
char *

variable_types:
url
char *


decompiled_code:
void __fastcall s3_delete(__int64 a1, const char *a2, const char *a3)
{
__int64 v4; // r12
char *v5; // rbp
void *v6; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v4 = s3_string_init();
v5 = s3_make_date();
asprintf((char **)&v6, "%s\n\n\n%s\n/%s/%s", "DELETE", v5, a2, a3);
asprintf(&ptr, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
s3_perform_op(a1, "DELETE", (__int64)ptr, (const char *)v6, v5, v4, 0LL, 0LL, 0LL);
s3_string_free(v4);
free(v5);
free(ptr);
free(v6);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, const char *a2, const char *a3)
{
__int64 v4; // r12
char *v5; // rbp
void *v6; // [rsp+0h] [rbp-38h] BYREF
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v4 = s3_string_init();
v5 = func3();
asprintf((char **)&v6, "%s\n\n\n%s\n/%s/%s", "DELETE", v5, a2, a3);
asprintf(&ptr, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
func4(a1, "DELETE", (__int64)ptr, (const char *)v6, v5, v4, 0LL, 0LL, 0LL);
s3_string_free(v4);
free(v5);
free(ptr);
free(v6);
}

original_code:
void
s3_delete(struct S3 *s3, const char *bucket, const char *key) {
 char *sign_data;
 char *url;
 char *date;
 const char *method = "DELETE";
 struct s3_string *out;
 out = s3_string_init();
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 s3_string_free(out);
 free(date);
 free(url);
 free(sign_data);
}

canonical_original_code:
void s3_delete(struct S3 *s3, char *bucket, char *key) {
 char *sign_data;
 char *url;
 char *date;
 char *method = "DELETE";
 struct s3_string *out;
 out = s3_string_init();
 date = s3_make_date();
 asprintf(&sign_data, "%s\n\n\n%s\n/%s/%s", method, date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, ((void *)0), ((void *)0), ((void *)0));
 s3_string_free(out);
 free(date);
 free(url);
 free(sign_data);
}


decompiled_code:
void __fastcall s3_put(__int64 a1, const char *a2, const char *a3, const char *a4, const void *a5, size_t a6)
{
__int64 v9; // rbp
__int64 v10; // r15
void *v11; // rax
char *v12; // r12
char *v13; // rax
const char *v14; // r8
char *v15; // r13
char *ptr; // [rsp+20h] [rbp-48h] BYREF
char *v18; // [rsp+28h] [rbp-40h] BYREF

v9 = s3_string_init();
v10 = s3_string_init();
v11 = realloc(*(void **)v9, a6 + 1);
*(_QWORD *)v9 = v11;
memcpy(v11, a5, a6);
*(_QWORD *)(v9 + 8) = a6;
v12 = (char *)s3_md5_sum(a5, a6);
v13 = s3_make_date();
v14 = (const char *)&unk_714;
v15 = v13;
if ( a4 )
v14 = a4;
asprintf(&ptr, "%s\n%s\n%s\n%s\n/%s/%s", "PUT", v12, v14, v13, a2, a3);
asprintf(&v18, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
s3_perform_op(a1, "PUT", (__int64)v18, ptr, v15, v10, v9, v12, a4);
s3_string_free(v9);
s3_string_free(v10);
free(v18);
free(v12);
free(v15);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, const char *a2, const char *a3, const char *a4, const void *a5, size_t a6)
{
__int64 v9; // rbp
__int64 v10; // r15
void *v11; // rax
char *v12; // r12
char *v13; // rax
const char *v14; // r8
char *v15; // r13
char *ptr; // [rsp+20h] [rbp-48h] BYREF
char *v18; // [rsp+28h] [rbp-40h] BYREF

v9 = s3_string_init();
v10 = s3_string_init();
v11 = realloc(*(void **)v9, a6 + 1);
*(_QWORD *)v9 = v11;
memcpy(v11, a5, a6);
*(_QWORD *)(v9 + 8) = a6;
v12 = (char *)s3_md5_sum(a5, a6);
v13 = func3();
v14 = (const char *)&unk_714;
v15 = v13;
if ( a4 )
v14 = a4;
asprintf(&ptr, "%s\n%s\n%s\n%s\n/%s/%s", "PUT", v12, v14, v13, a2, a3);
asprintf(&v18, "http://%s.%s/%s", a2, *(const char **)(a1 + 16), a3);
func4(a1, "PUT", (__int64)v18, ptr, v15, v10, v9, v12, a4);
s3_string_free(v9);
s3_string_free(v10);
free(v18);
free(v12);
free(v15);
free(ptr);
}

original_code:
void
s3_put(struct S3 *s3, const char *bucket, const char *key, const char *content_type, const char *data, size_t len) {
 const char *method = "PUT";
 char *sign_data;
 char *date;
 char *url;
 char *md5;
 struct s3_string *in, *out;
 in = s3_string_init();
 out = s3_string_init();
 in->ptr = realloc(in->ptr, len + 1);
 memcpy(in->ptr, data, len);
 in->len = len;
 md5 = s3_md5_sum(data, len);
 date = s3_make_date();
 asprintf(&sign_data, "%s\n%s\n%s\n%s\n/%s/%s", method, md5, content_type ? content_type : "", date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, in, md5, content_type);
 s3_string_free(in);
 s3_string_free(out);
 free(url);
 free(md5);
 free(date);
 free(sign_data);
}

canonical_original_code:
void s3_put(struct S3 *s3, char *bucket, char *key, char *content_type, char *data, unsigned long len) {
 char *method = "PUT";
 char *sign_data;
 char *date;
 char *url;
 char *md5;
 struct s3_string *in; struct s3_string *out;
 in = s3_string_init();
 out = s3_string_init();
 in->ptr = realloc(in->ptr, len + 1);
 memcpy(in->ptr, data, len);
 in->len = len;
 md5 = s3_md5_sum(data, len);
 date = s3_make_date();
 asprintf(&sign_data, "%s\n%s\n%s\n%s\n/%s/%s", method, md5, content_type ? content_type : "", date, bucket, key);
 asprintf(&url, "http://%s.%s/%s", bucket, s3->base_url, key);
 s3_perform_op(s3, method, url, sign_data, date, out, in, md5, content_type);
 s3_string_free(in);
 s3_string_free(out);
 free(url);
 free(md5);
 free(date);
 free(sign_data);
}

variable_types:
data
char *


decompiled_code:
void __fastcall s3_bucket_entry_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
free(ptr);
}

original_code:
void
s3_bucket_entry_free(struct s3_bucket_entry *entry) {
 if (entry->key)
  free(entry->key);
 if (entry->lastmod)
  free(entry->lastmod);
 if (entry->etag)
  free(entry->etag);
 free(entry);
}

variable_types: struct s3_bucket_entry { char *key; char *lastmod; unsigned long size; char *etag; struct { struct s3_bucket_entry *tqe_next; struct s3_bucket_entry **tqe_prev; } list; } *


decompiled_code:
void __fastcall s3_bucket_entries_free(void *ptr)
{
_QWORD *v2; // rdi
__int64 v3; // rax
_QWORD *v4; // rdx

v2 = *(_QWORD **)ptr;
if ( v2 )
{
do
{
v3 = v2[4];
v4 = (_QWORD *)v2[5];
if ( v3 )
*(_QWORD *)(v3 + 40) = v4;
else
*((_QWORD *)ptr + 1) = v4;
*v4 = v3;
s3_bucket_entry_free(v2);
v2 = *(_QWORD **)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
_QWORD *v2; // rdi
__int64 v3; // rax
_QWORD *v4; // rdx

v2 = *(_QWORD **)ptr;
if ( v2 )
{
do
{
v3 = v2[4];
v4 = (_QWORD *)v2[5];
if ( v3 )
*(_QWORD *)(v3 + 40) = v4;
else
*((_QWORD *)ptr + 1) = v4;
*v4 = v3;
func1(v2);
v2 = *(_QWORD **)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}

original_code:
void
s3_bucket_entries_free(struct s3_bucket_entry_head *entries) {
 struct s3_bucket_entry *e;
 while ((e = ((entries)->tqh_first)) != ((void *)0)) {
  do { if (((e)->list.tqe_next) != ((void *)0)) (e)->list.tqe_next->list.tqe_prev = (e)->list.tqe_prev; else (entries)->tqh_last = (e)->list.tqe_prev; *(e)->list.tqe_prev = (e)->list.tqe_next; } while ( 0);
  s3_bucket_entry_free(e);
 }
 free(entries);
}

user_defined_types: struct s3_bucket_entry { char *key; char *lastmod; unsigned long size; char *etag; struct { struct s3_bucket_entry *tqe_next; struct s3_bucket_entry **tqe_prev; } list; }


repo: lgpasquale/qchextractor

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // [rsp+Ch] [rbp-Ch]

if ( argc == 1 )
{
result = def(stdin, stdout);
if ( result )
goto LABEL_7;
}
else
{
if ( argc != 2 || strcmp(argv[1], "-d") )
{
fwrite("zpipe usage: zpipe [-d] < source > dest\n", 1uLL, 0x28uLL, stderr);
return 1;
}
result = inf(stdin, stdout);
if ( result )
{
LABEL_7:
v4 = result;
zerr((unsigned int)result);
return v4;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // [rsp+Ch] [rbp-Ch]

if ( argc == 1 )
{
result = func7(stdin, stdout);
if ( result )
goto LABEL_7;
}
else
{
if ( argc != 2 || strcmp(argv[1], "-d") )
{
fwrite("zpipe usage: zpipe [-d] < source > dest\n", 1uLL, 0x28uLL, stderr);
return 1;
}
result = func8(stdin, stdout);
if ( result )
{
LABEL_7:
v4 = result;
func9((unsigned int)result);
return v4;
}
}
return result;
}

original_code:
int main(int argc, char **argv)
{
    int ret;
    ;
    ;
    if (argc == 1) {
        ret = def(stdin, stdout, (-1));
        if (ret != 0)
            zerr(ret);
        return ret;
    }
    else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
        ret = inf(stdin, stdout);
        if (ret != 0)
            zerr(ret);
        return ret;
    }
    else {
        fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
        return 1;
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ret int


zerr

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

result = fwrite("zpipe: ", 1uLL, 7uLL, stderr);
if ( a1 >= 0xFFFFFFFA )
{
result = a1 + 6;
switch ( a1 )
{
case 0xFFFFFFFA:
result = fwrite("zlib version mismatch!\n", 1uLL, 0x17uLL, stderr);
break;
case 0xFFFFFFFB:
return result;
case 0xFFFFFFFC:
result = fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
break;
case 0xFFFFFFFD:
result = fwrite("invalid or incomplete deflate data\n", 1uLL, 0x23uLL, stderr);
break;
case 0xFFFFFFFE:
result = fwrite("invalid compression level\n", 1uLL, 0x1AuLL, stderr);
break;
case 0xFFFFFFFF:
if ( ferror(stdin) )
fwrite("error reading stdin\n", 1uLL, 0x14uLL, stderr);
result = ferror(stdout);
if ( result )
result = fwrite("error writing stdout\n", 1uLL, 0x15uLL, stderr);
break;
}
}
return result;
}

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

result = fwrite("zpipe: ", 1uLL, 7uLL, stderr);
if ( a1 >= 0xFFFFFFFA )
{
result = a1 + 6;
switch ( a1 )
{
case 0xFFFFFFFA:
result = fwrite("zlib version mismatch!\n", 1uLL, 0x17uLL, stderr);
break;
case 0xFFFFFFFB:
return result;
case 0xFFFFFFFC:
result = fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
break;
case 0xFFFFFFFD:
result = fwrite("invalid or incomplete deflate data\n", 1uLL, 0x23uLL, stderr);
break;
case 0xFFFFFFFE:
result = fwrite("invalid compression level\n", 1uLL, 0x1AuLL, stderr);
break;
case 0xFFFFFFFF:
if ( ferror(stdin) )
fwrite("error reading stdin\n", 1uLL, 0x14uLL, stderr);
result = ferror(stdout);
if ( result )
result = fwrite("error writing stdout\n", 1uLL, 0x15uLL, stderr);
break;
}
}
return result;
}

original_code:
void zerr(int ret)
{
    fputs("zpipe: ", stderr);
    switch (ret) {
    case (-1):
        if (ferror(stdin))
            fputs("error reading stdin\n", stderr);
        if (ferror(stdout))
            fputs("error writing stdout\n", stderr);
        break;
    case (-2):
        fputs("invalid compression level\n", stderr);
        break;
    case (-3):
        fputs("invalid or incomplete deflate data\n", stderr);
        break;
    case (-4):
        fputs("out of memory\n", stderr);
        break;
    case (-6):
        fputs("zlib version mismatch!\n", stderr);
    }
}


repo: lhh/cpglock

decompiled_code:
void *__fastcall do_alloc(size_t nmemb)
{
void *result; // rax

while ( 1 )
{
result = calloc(nmemb, 1uLL);
if ( result )
break;
usleep(0x2710u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t nmemb)
{
void *result; // rax

while ( 1 )
{
result = calloc(nmemb, 1uLL);
if ( result )
break;
usleep(0x2710u);
}
return result;
}

original_code:
void *
do_alloc(size_t n)
{
 void *p;
 do {
  p = malloc(n);
  if (!p) {
   usleep(10000);
  }
 } while(!p);
 memset(p, 0, n);
 return p;
}

canonical_original_code:
void *do_alloc(unsigned long n)
{
 void *p;
 do {
  p = malloc(n);
  if (!p) {
   usleep(10000);
  }
 } while(!p);
 memset(p, 0, n);
 return p;
}

variable_types:
n
unsigned long

variable_types:
p
void *


decompiled_code:
__int64 __fastcall select_retry(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
unsigned int v8; // er12

do
v8 = select(nfds, readfds, writefds, exceptfds, timeout);
while ( v8 == -1 && *_errno_location() == 4 );
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
unsigned int v8; // er12

do
v8 = select(nfds, readfds, writefds, exceptfds, timeout);
while ( v8 == -1 && *_errno_location() == 4 );
return v8;
}

original_code:
int
select_retry(int fdmax, fd_set * rfds, fd_set * wfds, fd_set * xfds,
        struct timeval *timeout)
{
 int rv;
 while (1) {
  rv = select(fdmax, rfds, wfds, xfds, timeout);
  if ((rv == -1) && ((*__errno_location ()) == 4))
   continue;
  return rv;
 }
}

canonical_original_code:
int select_retry(int fdmax, struct fd_set *rfds, struct fd_set *wfds, struct fd_set *xfds, struct timeval *timeout)
{
 int rv;
 while (1) {
  rv = select(fdmax, rfds, wfds, xfds, timeout);
  if ((rv == -1) && ((*__errno_location ()) == 4))
   continue;
  return rv;
 }
}

variable_types: fdmax int

variable_types: struct fd_set { long __fds_bits[-1]; } *

variable_types:
wfds
struct fd_set *

variable_types:
xfds
struct fd_set *

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

variable_types: rv int


decompiled_code:
__int64 __fastcall write_retry(int a1, __int64 a2, size_t a3, struct timeval *a4)
{
size_t v4; // r15
unsigned __int64 v5; // r12
__int64 v6; // rbx
__int64 v7; // rbp
int v8; // eax
int v9; // eax
int v10; // eax
unsigned __int64 v12; // [rsp+0h] [rbp-168h]
int v13; // [rsp+Ch] [rbp-15Ch]
unsigned __int64 v15; // [rsp+28h] [rbp-140h]
fd_set writefds; // [rsp+30h] [rbp-138h] BYREF
fd_set exceptfds; // [rsp+B0h] [rbp-B8h] BYREF

v15 = a3;
if ( !a3 )
return 0LL;
v4 = a3;
v5 = 0LL;
v12 = 0LL;
v6 = a1 / 64;
v7 = 1LL << (a1 % 64);
while ( 1 )
{
memset(&writefds, 0, sizeof(writefds));
writefds.fds_bits[v6] |= v7;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v6] |= v7;
v8 = select_retry(a1 + 1, 0LL, &writefds, &exceptfds, a4);
if ( v8 == -1 )
return -1LL;
if ( !v8 )
break;
if ( (exceptfds.fds_bits[v6] & v7) != 0
|| (v13 = v8, (v9 = write(a1, (const void *)(a2 + v12), v4)) == 0) && v13 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v9 == -1 )
{
v10 = *_errno_location();
if ( v10 != 11 && v10 != 4 )
return -1LL;
}
else
{
v5 += v9;
v4 -= v9;
}
v12 = v5;
if ( v5 >= v15 )
return v5;
}
v5 = -1LL;
*_errno_location() = 110;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2, size_t a3, struct timeval *a4)
{
size_t v4; // r15
unsigned __int64 v5; // r12
__int64 v6; // rbx
__int64 v7; // rbp
int v8; // eax
int v9; // eax
int v10; // eax
unsigned __int64 v12; // [rsp+0h] [rbp-168h]
int v13; // [rsp+Ch] [rbp-15Ch]
unsigned __int64 v15; // [rsp+28h] [rbp-140h]
fd_set writefds; // [rsp+30h] [rbp-138h] BYREF
fd_set exceptfds; // [rsp+B0h] [rbp-B8h] BYREF

v15 = a3;
if ( !a3 )
return 0LL;
v4 = a3;
v5 = 0LL;
v12 = 0LL;
v6 = a1 / 64;
v7 = 1LL << (a1 % 64);
while ( 1 )
{
memset(&writefds, 0, sizeof(writefds));
writefds.fds_bits[v6] |= v7;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v6] |= v7;
v8 = func1(a1 + 1, 0LL, &writefds, &exceptfds, a4);
if ( v8 == -1 )
return -1LL;
if ( !v8 )
break;
if ( (exceptfds.fds_bits[v6] & v7) != 0
|| (v13 = v8, (v9 = write(a1, (const void *)(a2 + v12), v4)) == 0) && v13 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v9 == -1 )
{
v10 = *_errno_location();
if ( v10 != 11 && v10 != 4 )
return -1LL;
}
else
{
v5 += v9;
v4 -= v9;
}
v12 = v5;
if ( v5 >= v15 )
return v5;
}
v5 = -1LL;
*_errno_location() = 110;
return v5;
}

original_code:
ssize_t
write_retry(int fd, void *buf, size_t count, struct timeval * timeout)
{
 int n, rv = 0;
 ssize_t total = 0, remain = count;
 fd_set wfds, xfds;
 while (total < count) {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&wfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(fd + 1, ((void *)0), &wfds, &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = write(fd, (char *)buf + (off_t) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

canonical_original_code:
long write_retry(int fd, void *buf, unsigned long count, struct timeval *timeout)
{
 int n; int rv = 0;
 long total = 0; long remain = count;
 struct fd_set wfds; struct fd_set xfds;
 while (total < count) {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&wfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(fd + 1, ((void *)0), &wfds, &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = write(fd, (char *)buf + (long) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: n int

variable_types: total long

variable_types: remain long

variable_types:
wfds
struct fd_set { long __fds_bits[-1]; }

variable_types:
xfds
struct fd_set { long __fds_bits[-1]; }

return_type: long

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


decompiled_code:
__int64 __fastcall read_retry(int a1, char *a2, size_t a3, struct timeval *a4)
{
size_t v4; // r15
size_t v5; // r14
unsigned __int64 v6; // r12
__int64 v7; // rbx
__int64 v8; // rbp
int v9; // eax
int v10; // eax
int v11; // eax
unsigned __int64 v13; // [rsp+0h] [rbp-168h]
int v14; // [rsp+Ch] [rbp-15Ch]
fd_set readfds; // [rsp+30h] [rbp-138h] BYREF
fd_set exceptfds; // [rsp+B0h] [rbp-B8h] BYREF

v4 = a3;
v5 = a3;
memset(a2, 0, a3);
if ( !v5 )
return 0LL;
v13 = 0LL;
v6 = 0LL;
v7 = a1 / 64;
v8 = 1LL << (a1 % 64);
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v8;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v7] |= v8;
v9 = select_retry(a1 + 1, &readfds, 0LL, &exceptfds, a4);
if ( v9 == -1 )
return -1LL;
if ( !v9 )
break;
if ( (exceptfds.fds_bits[v7] & v8) != 0 || (v14 = v9, (v10 = read(a1, &a2[v13], v4)) == 0) && v14 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v10 == -1 )
{
v11 = *_errno_location();
if ( v11 != 11 && v11 != 4 )
return -1LL;
}
else
{
v6 += v10;
v4 -= v10;
}
v13 = v6;
if ( v6 >= a3 )
return v6;
}
v6 = -1LL;
*_errno_location() = 110;
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, char *a2, size_t a3, struct timeval *a4)
{
size_t v4; // r15
size_t v5; // r14
unsigned __int64 v6; // r12
__int64 v7; // rbx
__int64 v8; // rbp
int v9; // eax
int v10; // eax
int v11; // eax
unsigned __int64 v13; // [rsp+0h] [rbp-168h]
int v14; // [rsp+Ch] [rbp-15Ch]
fd_set readfds; // [rsp+30h] [rbp-138h] BYREF
fd_set exceptfds; // [rsp+B0h] [rbp-B8h] BYREF

v4 = a3;
v5 = a3;
memset(a2, 0, a3);
if ( !v5 )
return 0LL;
v13 = 0LL;
v6 = 0LL;
v7 = a1 / 64;
v8 = 1LL << (a1 % 64);
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v8;
memset(&exceptfds, 0, sizeof(exceptfds));
exceptfds.fds_bits[v7] |= v8;
v9 = func1(a1 + 1, &readfds, 0LL, &exceptfds, a4);
if ( v9 == -1 )
return -1LL;
if ( !v9 )
break;
if ( (exceptfds.fds_bits[v7] & v8) != 0 || (v14 = v9, (v10 = read(a1, &a2[v13], v4)) == 0) && v14 == 1 )
{
*_errno_location() = 32;
return -1LL;
}
if ( v10 == -1 )
{
v11 = *_errno_location();
if ( v11 != 11 && v11 != 4 )
return -1LL;
}
else
{
v6 += v10;
v4 -= v10;
}
v13 = v6;
if ( v6 >= a3 )
return v6;
}
v6 = -1LL;
*_errno_location() = 110;
return v6;
}

original_code:
ssize_t
read_retry(int sockfd, void *buf, size_t count, struct timeval * timeout)
{
 int n, rv = 0;
 ssize_t total = 0, remain = count;
 fd_set rfds, xfds;
 memset(buf, 0, count);
 while (total < count) {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(sockfd + 1, &rfds, ((void *)0), &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = read(sockfd, (char *)buf + (off_t) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

canonical_original_code:
long read_retry(int sockfd, void *buf, unsigned long count, struct timeval *timeout)
{
 int n; int rv = 0;
 long total = 0; long remain = count;
 struct fd_set rfds; struct fd_set xfds;
 memset(buf, 0, count);
 while (total < count) {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&xfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))));
  rv = select_retry(sockfd + 1, &rfds, ((void *)0), &xfds, timeout);
  if (rv == -1)
   return -1;
  else if (rv == 0) {
   (*__errno_location ()) = 110;
   return -1;
  }
  if (((((&xfds)->__fds_bits)[((sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  n = read(sockfd, (char *)buf + (long) total, remain);
  if ((n == 0) && (rv == 1)) {
   (*__errno_location ()) = 32;
   return -1;
  }
  if (n == -1) {
   if (((*__errno_location ()) == 11) || ((*__errno_location ()) == 4)) {
    continue;
   }
   return -1;
  }
  total += n;
  remain -= n;
 }
 return total;
}

variable_types: sockfd int

variable_types:
rfds
struct fd_set { long __fds_bits[-1]; }


decompiled_code:
__int64 __fastcall sock_listen(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
__mode_t v3; // edi
struct sockaddr v5[9]; // [rsp+0h] [rbp-98h] BYREF

v1 = socket(1, 1, 0);
if ( v1 < 0 )
goto LABEL_5;
v2 = v1;
v5[0].sa_family = 1;
snprintf(v5[0].sa_data, 0x6CuLL, "%s", a1);
unlink(v5[0].sa_data);
v3 = umask(0x3Fu);
if ( bind(v2, v5, 0x6Eu) < 0 )
{
umask(v3);
close(v2);
goto LABEL_5;
}
umask(v3);
if ( listen(v2, 128) < 0 )
{
close(v2);
LABEL_5:
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
__mode_t v3; // edi
struct sockaddr v5[9]; // [rsp+0h] [rbp-98h] BYREF

v1 = socket(1, 1, 0);
if ( v1 < 0 )
goto LABEL_5;
v2 = v1;
v5[0].sa_family = 1;
snprintf(v5[0].sa_data, 0x6CuLL, "%s", a1);
unlink(v5[0].sa_data);
v3 = umask(0x3Fu);
if ( bind(v2, v5, 0x6Eu) < 0 )
{
umask(v3);
close(v2);
goto LABEL_5;
}
umask(v3);
if ( listen(v2, 128) < 0 )
{
close(v2);
LABEL_5:
v2 = -1;
}
return v2;
}

original_code:
int
sock_listen(const char *sockpath)
{
 int sock = -1;
       struct sockaddr_un su;
 mode_t om;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  goto fail;
 su.sun_family = 1;
 snprintf(su.sun_path, (sizeof(su.sun_path)), "%s", sockpath);
 unlink(su.sun_path);
 om = umask(077);
 if (bind(sock, (struct sockaddr *)&su, sizeof(su)) < 0) {
  umask(om);
  goto fail;
 }
 umask(om);
 if (listen(sock, 128) < 0)
  goto fail;
 return sock;
fail:
       if (sock >= 0)
  close(sock);
 return -1;
}

canonical_original_code:
int sock_listen(char *sockpath)
{
 int sock = -1;
       struct sockaddr_un su;
 unsigned int om;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  goto fail;
 su.sun_family = 1;
 snprintf(su.sun_path, (sizeof(su.sun_path)), "%s", sockpath);
 unlink(su.sun_path);
 om = umask(077);
 if (bind(sock, (struct sockaddr *)&su, sizeof(su)) < 0) {
  umask(om);
  goto fail;
 }
 umask(om);
 if (listen(sock, 128) < 0)
  goto fail;
 return sock;
fail:
       if (sock >= 0)
  close(sock);
 return -1;
}

variable_types:
sockpath
char *

variable_types: sock int

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types:
om
unsigned int

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


decompiled_code:
__int64 __fastcall sock_connect(const char *a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
unsigned int v4; // eax
int v5; // eax
__int64 v7; // rbx
__int64 v8; // r13
__m128i v9; // xmm1
__m128i v10; // xmm2
__m128i v11; // xmm3
__m128i v12; // xmm4
__m128i v13; // xmm5
__m128i v14; // xmm6
__m128i v15; // xmm7
int v16; // edi
int v17; // edi
char optval[4]; // [rsp+8h] [rbp-1B0h] BYREF
socklen_t optlen; // [rsp+Ch] [rbp-1ACh] BYREF
struct timeval timeout; // [rsp+10h] [rbp-1A8h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-198h] BYREF
fd_set readfds; // [rsp+90h] [rbp-128h] BYREF
fd_set writefds; // [rsp+110h] [rbp-A8h] BYREF

timeout.tv_sec = a2;
timeout.tv_usec = 0LL;
v2 = socket(1, 1, 0);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
addr.sa_family = 1;
snprintf(addr.sa_data, 0x6CuLL, "%s", a1);
v4 = fcntl(v3, 3, 0LL);
BYTE1(v4) |= 8u;
fcntl(v3, 4, v4);
v5 = connect(v3, &addr, 0x6Eu);
if ( v5 < 0 )
{
if ( *_errno_location() != 115 )
{
LABEL_10:
v16 = v3;
v3 = -1;
close(v16);
return v3;
}
}
else if ( !v5 )
{
return v3;
}
memset(&readfds, 0, sizeof(readfds));
v7 = (int)v3 >> 6;
v8 = 1LL << v3;
readfds.fds_bits[v7] |= 1LL << v3;
v9 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
v10 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
v11 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
v12 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)writefds.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
v13 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
*(__m128i *)&writefds.fds_bits[2] = v9;
v14 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
v15 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
*(__m128i *)&writefds.fds_bits[4] = v10;
*(__m128i *)&writefds.fds_bits[6] = v11;
*(__m128i *)&writefds.fds_bits[8] = v12;
*(__m128i *)&writefds.fds_bits[10] = v13;
*(__m128i *)&writefds.fds_bits[12] = v14;
*(__m128i *)&writefds.fds_bits[14] = v15;
if ( !select(v3 + 1, &readfds, &writefds, 0LL, &timeout) )
{
v17 = v3;
v3 = -1;
close(v17);
*_errno_location() = 110;
return v3;
}
if ( (readfds.fds_bits[v7] & v8) == 0 && (writefds.fds_bits[v7] & v8) == 0 )
goto LABEL_10;
optlen = 4;
if ( getsockopt(v3, 1, 4, optval, &optlen) < 0 )
goto LABEL_10;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
unsigned int v4; // eax
int v5; // eax
__int64 v7; // rbx
__int64 v8; // r13
__m128i v9; // xmm1
__m128i v10; // xmm2
__m128i v11; // xmm3
__m128i v12; // xmm4
__m128i v13; // xmm5
__m128i v14; // xmm6
__m128i v15; // xmm7
int v16; // edi
int v17; // edi
char optval[4]; // [rsp+8h] [rbp-1B0h] BYREF
socklen_t optlen; // [rsp+Ch] [rbp-1ACh] BYREF
struct timeval timeout; // [rsp+10h] [rbp-1A8h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-198h] BYREF
fd_set readfds; // [rsp+90h] [rbp-128h] BYREF
fd_set writefds; // [rsp+110h] [rbp-A8h] BYREF

timeout.tv_sec = a2;
timeout.tv_usec = 0LL;
v2 = socket(1, 1, 0);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
addr.sa_family = 1;
snprintf(addr.sa_data, 0x6CuLL, "%s", a1);
v4 = fcntl(v3, 3, 0LL);
BYTE1(v4) |= 8u;
fcntl(v3, 4, v4);
v5 = connect(v3, &addr, 0x6Eu);
if ( v5 < 0 )
{
if ( *_errno_location() != 115 )
{
LABEL_10:
v16 = v3;
v3 = -1;
close(v16);
return v3;
}
}
else if ( !v5 )
{
return v3;
}
memset(&readfds, 0, sizeof(readfds));
v7 = (int)v3 >> 6;
v8 = 1LL << v3;
readfds.fds_bits[v7] |= 1LL << v3;
v9 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[2]);
v10 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[4]);
v11 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[6]);
v12 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[8]);
*(__m128i *)writefds.fds_bits = _mm_loadu_si128((const __m128i *)&readfds);
v13 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[10]);
*(__m128i *)&writefds.fds_bits[2] = v9;
v14 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[12]);
v15 = _mm_loadu_si128((const __m128i *)&readfds.fds_bits[14]);
*(__m128i *)&writefds.fds_bits[4] = v10;
*(__m128i *)&writefds.fds_bits[6] = v11;
*(__m128i *)&writefds.fds_bits[8] = v12;
*(__m128i *)&writefds.fds_bits[10] = v13;
*(__m128i *)&writefds.fds_bits[12] = v14;
*(__m128i *)&writefds.fds_bits[14] = v15;
if ( !select(v3 + 1, &readfds, &writefds, 0LL, &timeout) )
{
v17 = v3;
v3 = -1;
close(v17);
*_errno_location() = 110;
return v3;
}
if ( (readfds.fds_bits[v7] & v8) == 0 && (writefds.fds_bits[v7] & v8) == 0 )
goto LABEL_10;
optlen = 4;
if ( getsockopt(v3, 1, 4, optval, &optlen) < 0 )
goto LABEL_10;
return v3;
}

original_code:
int
sock_connect(const char *sockpath, int tout)
{
 struct timeval timeout = {tout, 0};
 int sock, flags, error, ret;
 socklen_t len;
 struct sockaddr_un sun;
 fd_set rfds, wfds;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  return -1;
 sun.sun_family = 1;
 snprintf(sun.sun_path, (sizeof(sun.sun_path)), "%s", sockpath);
 flags = fcntl(sock, 3, 0);
 fcntl(sock, 4, flags | 04000);
        ret = connect(sock, (struct sockaddr *) &sun, sizeof(sun));
        if (ret < 0 && ((*__errno_location ()) != 115)) {
  close(sock);
  return -1;
 }
 if (ret == 0)
  goto done;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
 wfds = rfds;
 ret = select(sock + 1, &rfds, &wfds, ((void *)0), &timeout);
 if (ret == 0) {
  close(sock);
  (*__errno_location ()) = 110;
  return -1;
 }
 if (((((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0) || ((((&wfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  len = sizeof (error);
  if (getsockopt(sock, 1, 4, &error, &len) < 0) {
   close(sock);
   return -1;
  }
 } else {
  close(sock);
  return -1;
 }
done:
 return sock;
}

canonical_original_code:
int sock_connect(char *sockpath, int tout)
{
 struct timeval timeout = {tout, 0};
 int sock; int flags; int error; int ret;
 unsigned int len;
 struct sockaddr_un sun;
 struct fd_set rfds; struct fd_set wfds;
 sock = socket(1, SOCK_STREAM, 0);
 if (sock < 0)
  return -1;
 sun.sun_family = 1;
 snprintf(sun.sun_path, (sizeof(sun.sun_path)), "%s", sockpath);
 flags = fcntl(sock, 3, 0);
 fcntl(sock, 4, flags | 04000);
        ret = connect(sock, (struct sockaddr *) &sun, sizeof(sun));
        if (ret < 0 && ((*__errno_location ()) != 115)) {
  close(sock);
  return -1;
 }
 if (ret == 0)
  goto done;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
 wfds = rfds;
 ret = select(sock + 1, &rfds, &wfds, ((void *)0), &timeout);
 if (ret == 0) {
  close(sock);
  (*__errno_location ()) = 110;
  return -1;
 }
 if (((((&rfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0) || ((((&wfds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  len = sizeof (error);
  if (getsockopt(sock, 1, 4, &error, &len) < 0) {
   close(sock);
   return -1;
  }
 } else {
  close(sock);
  return -1;
 }
done:
 return sock;
}

variable_types: tout int

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

variable_types: flags int

variable_types: error int

variable_types: ret int

variable_types:
len
unsigned int

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
int __fastcall sock_accept(int fd)
{
int result; // eax

if ( fd < 0 )
{
*_errno_location() = 9;
result = -1;
}
else
{
while ( 1 )
{
result = accept(fd, 0LL, 0LL);
if ( result >= 0 )
break;
if ( *_errno_location() != 4 )
return -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd)
{
int result; // eax

if ( fd < 0 )
{
*_errno_location() = 9;
result = -1;
}
else
{
while ( 1 )
{
result = accept(fd, 0LL, 0LL);
if ( result >= 0 )
break;
if ( *_errno_location() != 4 )
return -1;
}
}
return result;
}

original_code:
int
sock_accept(int sockfd)
{
 int acceptfd;
 if (sockfd < 0) {
  (*__errno_location ()) = 9;
  return -1;
 }
 while ((acceptfd =
  accept(sockfd, (struct sockaddr *) ((void *)0), ((void *)0))) < 0) {
  if ((*__errno_location ()) == 4) {
   continue;
  }
  return -1;
 }
 return acceptfd;
}

variable_types: acceptfd int


hexdump

decompiled_code:
int __fastcall hexdump(unsigned __int8 *a1, __int64 a2)
{
__int64 v3; // rbx
__int64 v4; // rsi

printf("%d bytes @ %p \n", a2, a1);
if ( a2 )
{
v3 = 1LL;
printf(" %02x", *a1);
while ( a2 != v3 )
{
while ( 1 )
{
v4 = a1[v3++];
printf(" %02x", v4);
if ( (v3 & 0xF) != 0 )
break;
putchar(10);
if ( a2 == v3 )
return putchar(10);
}
}
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func7(unsigned __int8 *a1, __int64 a2)
{
__int64 v3; // rbx
__int64 v4; // rsi

printf("%d bytes @ %p \n", a2, a1);
if ( a2 )
{
v3 = 1LL;
printf(" %02x", *a1);
while ( a2 != v3 )
{
while ( 1 )
{
v4 = a1[v3++];
printf(" %02x", v4);
if ( (v3 & 0xF) != 0 )
break;
putchar(10);
if ( a2 == v3 )
return putchar(10);
}
}
}
return putchar(10);
}

original_code:
void
hexdump(const void *buf, size_t len)
{
 size_t x;
 printf("%d bytes @ %p \n", (int)len, buf);
 for (x = 0; x < len; x++) {
  printf(" %02x", (((char *)buf)[x])&0xff);
  if (((x+1) % 16) == 0)
   printf("\n");
 }
 printf("\n");
}

canonical_original_code:
void hexdump(void *buf, unsigned long len)
{
 unsigned long x;
 printf("%d bytes @ %p \n", (int)len, buf);
 for (x = 0; x < len; x++) {
  printf(" %02x", (((char *)buf)[x])&0xff);
  if (((x+1) % 16) == 0)
   printf("\n");
 }
 printf("\n");
}

variable_types:
buf
void *

variable_types:
len
unsigned long

variable_types:
x
unsigned long


inter

decompiled_code:
void inter()
{
running = 0;
}

canonical_decompiled_code:
void func0()
{
running = 0;
}

original_code:
static void
inter(int sig)
{
 running = 0;
}

variable_types: sig int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int result; // eax
__int64 v5; // [rsp+8h] [rbp-B0h] BYREF
char v6[168]; // [rsp+10h] [rbp-A8h] BYREF

if ( argc <= 1 )
{
printf("usage: %s lockname\n", *argv);
result = 1;
}
else
{
signal(2, (__sighandler_t)inter);
if ( (int)cpg_lock_init(&v5) < 0 )
{
perror("cpg_lock_init");
result = 1;
}
else
{
v3 = running;
if ( running )
{
v3 = 0;
while ( 1 )
{
printf("lock ... ");
fflush(stdout);
if ( (int)cpg_lock(v5, argv[1], 0LL, v6) < 0 )
{
perror("cpg_lock");
return 1;
}
printf("unlock");
if ( (int)cpg_unlock(v5, v6) < 0 )
break;
++v3;
putchar(10);
if ( !running )
goto LABEL_10;
}
perror("cpg_unlock");
result = 1;
}
else
{
LABEL_10:
cpg_lock_fin(v5);
printf("%s taken/released %d times\n", *argv, v3);
result = 0;
}
}
}
return result;
}

original_code:
int
main(int argc, char **argv)
{
 cpg_lock_handle_t h;
 struct cpg_lock l;
 int n = 0;
 if (argc < 2) {
  printf("usage: %s lockname\n", argv[0]);
  return 1;
 }
 signal(2, inter);
 if (cpg_lock_init(&h) < 0) {
  perror("cpg_lock_init");
  return 1;
 }
 while(running) {
  printf("lock ... ");
  fflush(stdout);
  if (cpg_lock(h, argv[1], 0, &l) < 0) {
   perror("cpg_lock");
   return 1;
  }
  printf("unlock");
  if (cpg_unlock(h, &l) < 0) {
   perror("cpg_unlock");
   return 1;
  }
  ++n;
  printf("\n");
 }
 cpg_lock_fin(h);
 printf("%s taken/released %d times\n", argv[0], n);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 void *h;
 struct cpg_lock l;
 int n = 0;
 if (argc < 2) {
  printf("usage: %s lockname\n", argv[0]);
  return 1;
 }
 signal(2, inter);
 if (cpg_lock_init(&h) < 0) {
  perror("cpg_lock_init");
  return 1;
 }
 while(running) {
  printf("lock ... ");
  fflush(stdout);
  if (cpg_lock(h, argv[1], 0, &l) < 0) {
   perror("cpg_lock");
   return 1;
  }
  printf("unlock");
  if (cpg_unlock(h, &l) < 0) {
   perror("cpg_unlock");
   return 1;
  }
  ++n;
  printf("\n");
 }
 cpg_lock_fin(h);
 printf("%s taken/released %d times\n", argv[0], n);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
h
void *

variable_types: struct cpg_lock { char resource[96]; int local_fd; int local_id; enum lock_state_t { LOCK_FREE=0, LOCK_PENDING=1, LOCK_HELD=2 } state; int owner_nodeid; int owner_pid; int owner_tid; }

variable_types: n int

user_defined_types: struct cpg_lock { char resource[96]; int local_fd; int local_id; enum lock_state_t state; int owner_nodeid; int owner_pid; int owner_tid; }