main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v4; // rdx
const char *v5; // rdi
FILE *v6; // rcx
unsigned int v7; // [rsp+0h] [rbp-28h] BYREF
const char **v8; // [rsp+8h] [rbp-20h]
int v9; // [rsp+10h] [rbp-18h]

v7 = argc;
v8 = argv;
v9 = 0;
if ( (unsigned int)fuse_opt_parse(&v7, &rw_path, &rwfs_opts, rwfs_parse_opt) )
{
v4 = 18LL;
v5 = "Invalid arguments\n";
v6 = stderr;
goto LABEL_5;
}
if ( !rw_path )
{
v4 = 25LL;
v5 = "Missing source for mount\n";
v6 = stderr;
LABEL_5:
fwrite(v5, 1uLL, v4, v6);
fprintf(stderr, "`%s -h' for help\n", *argv);
exit(1);
}
fuse_main_real(v7, v8, &rw_oper, 360LL, 0LL);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    struct fuse_args args = { argc, argv, 0 };
    int res;
    res = fuse_opt_parse(&args, &rw_path, rwfs_opts, rwfs_parse_opt);
    if (res != 0)
    {
        fprintf(stderr, "Invalid arguments\n");
        fprintf(stderr, "`%s -h' for help\n", argv[0]);
        exit(1);
    }
    if (rw_path == 0)
    {
        fprintf(stderr, "Missing source for mount\n");
        fprintf(stderr, "`%s -h' for help\n", argv[0]);
        exit(1);
    }
    fuse_main_real(args.argc, args.argv, &rw_oper, sizeof(*(&rw_oper)), ((void *)0));
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct fuse_args { int argc; char **argv; int allocated; }

variable_types: res int


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

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

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

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

original_code:
static int compare_uploads(const void* first, const void* second) {
  const upload_entry_t* first_entry = first;
  const upload_entry_t* second_entry = second;
  if (first_entry->last_modified < second_entry->last_modified) {
    return 1;
  } else if (first_entry->last_modified > second_entry->last_modified) {
    return -1;
  } else {
    return 0;
  }
}

canonical_original_code:
static int compare_uploads(void *first, void *second) {
  struct upload_entry_t *first_entry = first;
  struct upload_entry_t *second_entry = second;
  if (first_entry->last_modified < second_entry->last_modified) {
    return 1;
  } else if (first_entry->last_modified > second_entry->last_modified) {
    return -1;
  } else {
    return 0;
  }
}

variable_types:
first
void *

variable_types:
second
void *

variable_types: struct upload_entry_t { char filename[-1]; long last_modified; unsigned long size; int index; } *

return_type: int


decompiled_code:
__int64 __fastcall upload_list_init(_QWORD *a1)
{
void *v1; // rax
int *v3; // rax
char *v4; // rax

*a1 = 16LL;
v1 = malloc(0x10200uLL);
a1[1] = v1;
if ( v1 )
return 0LL;
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "uploads_list_init:malloc: %s", v4);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
void *v1; // rax
int *v3; // rax
char *v4; // rax

*a1 = 16LL;
v1 = malloc(0x10200uLL);
a1[1] = v1;
if ( v1 )
return 0LL;
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "uploads_list_init:malloc: %s", v4);
return 0xFFFFFFFFLL;
}

original_code:
int upload_list_init(upload_list_t* list) {
  list->capacity = 16;
  list->length = 0;
  list->entries = malloc(list->capacity * sizeof(list->entries[0]));
  if (list->entries == ((void *)0)) {
    syslog(3, "uploads_list_init:malloc: %s", strerror((*__errno_location ())));
    return -1;
  }
  return 0;
}

canonical_original_code:
int upload_list_init(struct upload_list_t *list) {
  list->capacity = 16;
  list->length = 0;
  list->entries = malloc(list->capacity * sizeof(list->entries[0]));
  if (list->entries == ((void *)0)) {
    syslog(3, "uploads_list_init:malloc: %s", strerror((*__errno_location ())));
    return -1;
  }
  return 0;
}

variable_types: struct upload_list_t { int capacity; int length; struct upload_entry_t { char filename[-1]; long last_modified; unsigned long size; int index; } *entries; } *

user_defined_types: struct upload_list_t { int capacity; int length; struct upload_entry_t *entries; }


decompiled_code:
void __fastcall upload_list_destroy(__int64 a1)
{
free(*(void **)(a1 + 8));
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
free(*(void **)(a1 + 8));
}

original_code:
void upload_list_destroy(upload_list_t* list) {
  free(list->entries);
}

canonical_original_code:
void upload_list_destroy(struct upload_list_t *list) {
  free(list->entries);
}

return_type: void

user_defined_types: struct upload_entry_t { char filename[-1]; long last_modified; unsigned long size; int index; }


decompiled_code:
__int64 __fastcall upload_list_append(__int64 a1, const char *a2, __int64 a3, __int64 a4, int a5)
{
char *v6; // rdi
int v8; // eax
int v11; // edx
char *v12; // rax
int v13; // eax
char *v14; // rax
int *v16; // rax
char *v17; // rax

v6 = *(char **)(a1 + 8);
if ( !v6 )
return 0xFFFFFFFFLL;
v8 = *(_DWORD *)(a1 + 4);
if ( v8 < *(_DWORD *)a1 )
{
LABEL_5:
v13 = v8 + 1;
*(_DWORD *)(a1 + 4) = v13;
v14 = strncpy(&v6[4128 * v13 - 4128], a2, 0x1000uLL);
*((_QWORD *)v14 + 513) = a3;
*((_QWORD *)v14 + 514) = a4;
*((_DWORD *)v14 + 1030) = a5;
return 0LL;
}
v11 = 2 * *(_DWORD *)a1;
*(_DWORD *)a1 = v11;
v12 = (char *)realloc(v6, 4128LL * v11);
v6 = v12;
if ( v12 )
{
*(_QWORD *)(a1 + 8) = v12;
v8 = *(_DWORD *)(a1 + 4);
goto LABEL_5;
}
v16 = _errno_location();
v17 = strerror(*v16);
syslog(3, "uploads_list_append:realloc: %s", v17);
return 0xFFFFFFFFLL;
}

original_code:
int upload_list_append(upload_list_t* list,
                       const char* filename,
                       const time_t last_modified,
                       const size_t size,
                       const int index) {
  if (list->entries == ((void *)0)) {
    return -1;
  }
  if (list->length >= list->capacity) {
    list->capacity *= 2;
    void* new_entries = realloc(list->entries,
                                list->capacity * sizeof(list->entries[0]));
    if (new_entries == ((void *)0)) {
      syslog(3, "uploads_list_append:realloc: %s", strerror((*__errno_location ())));
      return -1;
    } else {
      list->entries = new_entries;
    }
  }
  ++list->length;
  upload_entry_t* entry = &list->entries[list->length - 1];
  strncpy(entry->filename, filename, 4096);
  entry->last_modified = last_modified;
  entry->size = size;
  entry->index = index;
  return 0;
}

canonical_original_code:
int upload_list_append(struct upload_list_t *list, char *filename, long last_modified, unsigned long size, int index) {
  if (list->entries == ((void *)0)) {
    return -1;
  }
  if (list->length >= list->capacity) {
    list->capacity *= 2;
    void *new_entries = realloc(list->entries,
                                list->capacity * sizeof(list->entries[0]));
    if (new_entries == ((void *)0)) {
      syslog(3, "uploads_list_append:realloc: %s", strerror((*__errno_location ())));
      return -1;
    } else {
      list->entries = new_entries;
    }
  }
  ++list->length;
  struct upload_entry_t *entry = &list->entries[list->length - 1];
  strncpy(entry->filename, filename, 4096);
  entry->last_modified = last_modified;
  entry->size = size;
  entry->index = index;
  return 0;
}

variable_types:
filename
char *

variable_types:
size
unsigned long

variable_types: index int


decompiled_code:
void __fastcall upload_list_sort(__int64 a1)
{
qsort(*(void **)(a1 + 8), *(int *)(a1 + 4), 0x1020uLL, (__compar_fn_t)compare_uploads);
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
qsort(*(void **)(a1 + 8), *(int *)(a1 + 4), 0x1020uLL, (__compar_fn_t)compare_uploads);
}

original_code:
void upload_list_sort(upload_list_t* list) {
  qsort(list->entries, list->length, sizeof(list->entries[0]), compare_uploads);
}

canonical_original_code:
void upload_list_sort(struct upload_list_t *list) {
  qsort(list->entries, list->length, sizeof(list->entries[0]), compare_uploads);
}


unsymlink

repo: mark-raymond/nbd-bind

decompiled_code:
__int64 __fastcall unsymlink(char *s2, char *filename, int a3)
{
size_t v4; // rdx
void *v5; // rsp
ssize_t v6; // rax
__int64 result; // rax
struct stat v8; // [rsp+0h] [rbp-B0h] BYREF

if ( (unsigned int)lstat(filename, &v8) )
{
fprintf(_bss_start, "lstat failed on file %s\n", filename);
return 1LL;
}
if ( (v8.st_mode & 0xF000) != 40960 )
{
fprintf(_bss_start, "%s is not a symbolic link\n", filename);
return 1LL;
}
v4 = a3 + 11;
v5 = alloca(v4);
v6 = readlink(filename, (char *)&v8, v4);
if ( v6 < 0 )
{
fprintf(_bss_start, "Could not read link %s\n", filename);
return 1LL;
}
if ( a3 + 10 >= v6 && (*((_BYTE *)&v8.st_dev + v6) = 0, !strcmp((const char *)&v8, s2)) )
{
unlink(filename);
result = 1LL;
}
else
{
fprintf(_bss_start, "%s does not point to %s\n", filename, s2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s2, char *filename, int a3)
{
size_t v4; // rdx
void *v5; // rsp
ssize_t v6; // rax
__int64 result; // rax
struct stat v8; // [rsp+0h] [rbp-B0h] BYREF

if ( (unsigned int)func16(filename, &v8) )
{
fprintf(_bss_start, "lstat failed on file %s\n", filename);
return 1LL;
}
if ( (v8.st_mode & 0xF000) != 40960 )
{
fprintf(_bss_start, "%s is not a symbolic link\n", filename);
return 1LL;
}
v4 = a3 + 11;
v5 = alloca(v4);
v6 = readlink(filename, (char *)&v8, v4);
if ( v6 < 0 )
{
fprintf(_bss_start, "Could not read link %s\n", filename);
return 1LL;
}
if ( a3 + 10 >= v6 && (*((_BYTE *)&v8.st_dev + v6) = 0, !strcmp((const char *)&v8, s2)) )
{
unlink(filename);
result = 1LL;
}
else
{
fprintf(_bss_start, "%s does not point to %s\n", filename, s2);
result = 1LL;
}
return result;
}

original_code:
_Bool unsymlink(const char *nbd_path, const char *tgt_path, int extlen) {
  struct stat s;
  if (lstat(tgt_path, &s)) {
    fprintf(stderr, "lstat failed on file %s\n", tgt_path);
    return 1;
  }
  if (!((((s.st_mode)) & 0170000) == (0120000))) {
    fprintf(stderr, "%s is not a symbolic link\n", tgt_path);
    return 1;
  }
  char link_path[11 + extlen];
  ssize_t chars = readlink(tgt_path, link_path, 11 + extlen);
  if (chars < 0) {
    fprintf(stderr, "Could not read link %s\n", tgt_path);
    return 1;
  }
  if (chars > 10 + extlen || (link_path[chars] = '\0', strcmp(link_path, nbd_path))) {
    fprintf(stderr, "%s does not point to %s\n", tgt_path, nbd_path);
    return 1;
  }
  unlink(tgt_path);
  return 1;
}

canonical_original_code:
_Bool unsymlink(char *nbd_path, char *tgt_path, int extlen) {
  struct stat s;
  if (lstat(tgt_path, &s)) {
    fprintf(stderr, "lstat failed on file %s\n", tgt_path);
    return 1;
  }
  if (!((((s.st_mode)) & 0170000) == (0120000))) {
    fprintf(stderr, "%s is not a symbolic link\n", tgt_path);
    return 1;
  }
  char link_path[-1];
  long chars = readlink(tgt_path, link_path, 11 + extlen);
  if (chars < 0) {
    fprintf(stderr, "Could not read link %s\n", tgt_path);
    return 1;
  }
  if (chars > 10 + extlen || (link_path[chars] = '\0', strcmp(link_path, nbd_path))) {
    fprintf(stderr, "%s does not point to %s\n", tgt_path, nbd_path);
    return 1;
  }
  unlink(tgt_path);
  return 1;
}

variable_types: extlen int

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

variable_types: chars long

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


decompiled_code:
bool __fastcall parent_dir_is_writeable(void *src)
{
size_t v1; // rdx
void *v2; // rsp
char *v3; // rax
char v5[8]; // [rsp+0h] [rbp-10h] BYREF

v1 = strlen((const char *)src) + 1;
v2 = alloca(v1);
memcpy(v5, src, v1);
v3 = dirname(v5);
return access(v3, 2) == 0;
}

canonical_decompiled_code:
bool __fastcall func8(void *src)
{
size_t v1; // rdx
void *v2; // rsp
char *v3; // rax
char v5[8]; // [rsp+0h] [rbp-10h] BYREF

v1 = strlen((const char *)src) + 1;
v2 = alloca(v1);
memcpy(v5, src, v1);
v3 = dirname(v5);
return access(v3, 2) == 0;
}

original_code:
_Bool parent_dir_is_writeable(const char * const path) {
  char copy[strlen(path) + 1];
  strcpy(copy, path);
  return access(dirname(copy), 2) == 0;
}

canonical_original_code:
_Bool parent_dir_is_writeable(char *path) {
  char copy[-1];
  strcpy(copy, path);
  return access(dirname(copy), 2) == 0;
}

variable_types:
path
char *

variable_types:
copy
char [-1]


decompiled_code:
bool __fastcall does_not_exist(char *a1)
{
int v1; // er8
bool result; // al
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0;
if ( v1 )
result = *__errno_location() == 2;
return result;
}

canonical_decompiled_code:
bool __fastcall func9(char *a1)
{
int v1; // er8
bool result; // al
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = func15(a1, &v3);
result = 0;
if ( v1 )
result = *__errno_location() == 2;
return result;
}

original_code:
_Bool does_not_exist(char *file) {
  struct stat s;
  return (stat(file, &s) && (*__errno_location ()) == 2);
}

variable_types:
file
char *

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


decompiled_code:
bool __fastcall is_nbd(_BYTE *a1)
{
bool result; // al
char v2; // dl

result = 0;
if ( *a1 == 110 && a1[1] == 98 && a1[2] == 100 && (unsigned __int8)(a1[3] - 48) <= 9u )
{
v2 = a1[4];
result = 1;
if ( v2 )
{
result = 0;
if ( (unsigned __int8)(v2 - 48) <= 9u )
result = a1[5] == 0;
}
}
return result;
}


decompiled_code:
__int64 __fastcall starts_with(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // cl
char v4; // dl

v2 = 0LL;
do
{
v4 = *(_BYTE *)(a2 + v2);
if ( !v4 )
return 1LL;
v3 = *(_BYTE *)(a1 + v2++);
}
while ( v3 == v4 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // cl
char v4; // dl

v2 = 0LL;
do
{
v4 = *(_BYTE *)(a2 + v2);
if ( !v4 )
return 1LL;
v3 = *(_BYTE *)(a1 + v2++);
}
while ( v3 == v4 );
return 0LL;
}

original_code:
_Bool starts_with(const char *str, const char *pre) {
  while (*pre) {
    if (*str++ != *pre++) return 0;
  }
  return 1;
}

canonical_original_code:
_Bool starts_with(char *str, char *pre) {
  while (*pre) {
    if (*str++ != *pre++) return 0;
  }
  return 1;
}

variable_types:
str
char *

variable_types:
pre
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4; // r12
ssize_t v5; // rax
__uid_t v6; // ebx
int v7; // er12
char buf[5]; // [rsp+5h] [rbp-B3h] BYREF
char s[6]; // [rsp+Ah] [rbp-AEh] BYREF
struct stat v10; // [rsp+10h] [rbp-A8h] BYREF

if ( argc == 2 )
{
v4 = (char *)argv[1];
if ( !access(v4, 6) && (unsigned __int8)parent_dir_is_writeable(v4) )
{
if ( (unsigned int)lstat(v4, &v10) )
{
fprintf(_bss_start, "lstat failed on %s\n", v4);
}
else if ( (v10.st_mode & 0xF000) == 40960 )
{
v5 = readlink(v4, buf, 0xBuLL);
if ( v5 < 0 )
{
fprintf(_bss_start, "Could not read link %s\n", v4);
}
else if ( v5 <= 10 && (buf[v5] = 0, (unsigned __int8)starts_with(buf, dev)) && (unsigned __int8)is_nbd(s) )
{
if ( (unsigned int)stat(buf, &v10) )
{
fprintf(_bss_start, "stat failed on file %s\n", buf);
}
else if ( getuid() && (v6 = v10.st_uid, v6 != getuid()) )
{
fprintf(_bss_start, "%s not owned by you\n", buf);
}
else
{
each_nbd(s, v4);
v7 = open("/dev/null", 2);
dup2(v7, 0);
dup2(v7, 1);
dup2(v7, 2);
close(v7);
execl(qemu_nbd_path, qemu_nbd, "-d", buf, 0LL);
}
}
else
{
fprintf(_bss_start, "%s does not point to an nbd device\n", v4);
}
}
else
{
fprintf(_bss_start, "%s is not a symbolic link\n", v4);
}
}
else
{
fprintf(_bss_start, "Insufficcient permissions for %s\n", v4);
}
}
else
{
fwrite("Usage: nbd-unbind target\n", 1uLL, 0x19uLL, _bss_start);
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4; // r12
ssize_t v5; // rax
__uid_t v6; // ebx
int v7; // er12
char buf[5]; // [rsp+5h] [rbp-B3h] BYREF
char s[6]; // [rsp+Ah] [rbp-AEh] BYREF
struct stat v10; // [rsp+10h] [rbp-A8h] BYREF

if ( argc == 2 )
{
v4 = (char *)argv[1];
if ( !access(v4, 6) && (unsigned __int8)func8(v4) )
{
if ( (unsigned int)func16(v4, &v10) )
{
fprintf(_bss_start, "lstat failed on %s\n", v4);
}
else if ( (v10.st_mode & 0xF000) == 40960 )
{
v5 = readlink(v4, buf, 0xBuLL);
if ( v5 < 0 )
{
fprintf(_bss_start, "Could not read link %s\n", v4);
}
else if ( v5 <= 10 && (buf[v5] = 0, (unsigned __int8)func11(buf, dev)) && (unsigned __int8)func10(s) )
{
if ( (unsigned int)func15(buf, &v10) )
{
fprintf(_bss_start, "stat failed on file %s\n", buf);
}
else if ( getuid() && (v6 = v10.st_uid, v6 != getuid()) )
{
fprintf(_bss_start, "%s not owned by you\n", buf);
}
else
{
func12(s, v4);
v7 = open("/dev/null", 2);
dup2(v7, 0);
dup2(v7, 1);
dup2(v7, 2);
close(v7);
execl(qemu_nbd_path, qemu_nbd, "-d", buf, 0LL);
}
}
else
{
fprintf(_bss_start, "%s does not point to an nbd device\n", v4);
}
}
else
{
fprintf(_bss_start, "%s is not a symbolic link\n", v4);
}
}
else
{
fprintf(_bss_start, "Insufficcient permissions for %s\n", v4);
}
}
else
{
fwrite("Usage: nbd-unbind target\n", 1uLL, 0x19uLL, _bss_start);
}
return 1;
}

original_code:
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: nbd-unbind target\n");
    return 1;
  }
  char *tgt = argv[1];
  if (access(tgt, 4 | 2) || !parent_dir_is_writeable(tgt)) {
    fprintf(stderr, "Insufficcient permissions for %s\n", tgt);
    return 1;
  }
  struct stat s;
  if (lstat(tgt, &s)) {
    fprintf(stderr, "lstat failed on %s\n", tgt);
    return 1;
  }
  if (!((((s.st_mode)) & 0170000) == (0120000))) {
    fprintf(stderr, "%s is not a symbolic link\n", tgt);
    return 1;
  }
  char nbd_path[11];
  ssize_t chars = readlink(tgt, nbd_path, 11);
  if (chars < 0) {
    fprintf(stderr, "Could not read link %s\n", tgt);
    return 1;
  }
  if (chars > 10 || (nbd_path[chars] = '\0', !(starts_with(nbd_path, dev) && is_nbd(nbd_path + 5)))) {
    fprintf(stderr, "%s does not point to an nbd device\n", tgt);
    return 1;
  }
  if (stat(nbd_path, &s)) {
    fprintf(stderr, "stat failed on file %s\n", nbd_path);
    return 1;
  }
  if (getuid() != 0 && s.st_uid != getuid()) {
    fprintf(stderr, "%s not owned by you\n", nbd_path);
    return 1;
  }
  char *nbd = nbd_path + 5;
  each_nbd(nbd, tgt, &unsymlink);
  int devnull = open("/dev/null", 02);
  dup2(devnull, 0);
  dup2(devnull, 1);
  dup2(devnull, 2);
  close(devnull);
  execl(qemu_nbd_path, qemu_nbd, "-d", nbd_path, (char*)((void *)0));
  return 1;
}

canonical_original_code:
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: nbd-unbind target\n");
    return 1;
  }
  char *tgt = argv[1];
  if (access(tgt, 4 | 2) || !parent_dir_is_writeable(tgt)) {
    fprintf(stderr, "Insufficcient permissions for %s\n", tgt);
    return 1;
  }
  struct stat s;
  if (lstat(tgt, &s)) {
    fprintf(stderr, "lstat failed on %s\n", tgt);
    return 1;
  }
  if (!((((s.st_mode)) & 0170000) == (0120000))) {
    fprintf(stderr, "%s is not a symbolic link\n", tgt);
    return 1;
  }
  char nbd_path[11];
  long chars = readlink(tgt, nbd_path, 11);
  if (chars < 0) {
    fprintf(stderr, "Could not read link %s\n", tgt);
    return 1;
  }
  if (chars > 10 || (nbd_path[chars] = '\0', !(starts_with(nbd_path, dev) && is_nbd(nbd_path + 5)))) {
    fprintf(stderr, "%s does not point to an nbd device\n", tgt);
    return 1;
  }
  if (stat(nbd_path, &s)) {
    fprintf(stderr, "stat failed on file %s\n", nbd_path);
    return 1;
  }
  if (getuid() != 0 && s.st_uid != getuid()) {
    fprintf(stderr, "%s not owned by you\n", nbd_path);
    return 1;
  }
  char *nbd = nbd_path + 5;
  each_nbd(nbd, tgt, &unsymlink);
  int devnull = open("/dev/null", 02);
  dup2(devnull, 0);
  dup2(devnull, 1);
  dup2(devnull, 2);
  close(devnull);
  execl(qemu_nbd_path, qemu_nbd, "-d", nbd_path, (char *)((void *)0));
  return 1;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
tgt
char *

variable_types:
nbd
char *

variable_types: devnull int


decompiled_code:
__int64 __fastcall set_permissions_and_link(char *from, char *file)
{
__gid_t v2; // er14
__uid_t v3; // eax
__gid_t v4; // er12
__uid_t v5; // eax
int v6; // er8
__int64 result; // rax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

if ( stat(from, &v8) )
{
fprintf(stderr, "stat failed on file %s\n", from);
result = 0LL;
}
else
{
v2 = v8.st_gid;
v3 = getuid();
if ( chown(from, v3, v2) )
{
fprintf(stderr, "chown failed on file %s\n", from);
result = 0LL;
}
else if ( symlink(from, file) )
{
fprintf(stderr, "symlink failed %s -> %s\n", file, from);
result = 0LL;
}
else
{
v4 = getgid();
v5 = getuid();
v6 = lchown(file, v5, v4);
result = 1LL;
if ( v6 )
{
fprintf(stderr, "lchown failed on file %s\n", file);
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *from, char *file)
{
__gid_t v2; // er14
__uid_t v3; // eax
__gid_t v4; // er12
__uid_t v5; // eax
int v6; // er8
__int64 result; // rax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

if ( stat(from, &v8) )
{
fprintf(stderr, "stat failed on file %s\n", from);
result = 0LL;
}
else
{
v2 = v8.st_gid;
v3 = getuid();
if ( chown(from, v3, v2) )
{
fprintf(stderr, "chown failed on file %s\n", from);
result = 0LL;
}
else if ( symlink(from, file) )
{
fprintf(stderr, "symlink failed %s -> %s\n", file, from);
result = 0LL;
}
else
{
v4 = getgid();
v5 = getuid();
v6 = lchown(file, v5, v4);
result = 1LL;
if ( v6 )
{
fprintf(stderr, "lchown failed on file %s\n", file);
result = 0LL;
}
}
}
return result;
}

original_code:
_Bool set_permissions_and_link(const char *nbd_path, const char *tgt_path, int extlen) {
  struct stat s;
  if (stat(nbd_path, &s)) {
    fprintf(stderr, "stat failed on file %s\n", nbd_path);
    return 0;
  }
  if (chown(nbd_path, getuid(), s.st_gid)) {
    fprintf(stderr, "chown failed on file %s\n", nbd_path);
    return 0;
  }
  if (symlink(nbd_path, tgt_path)) {
    fprintf(stderr, "symlink failed %s -> %s\n", tgt_path, nbd_path);
    return 0;
  }
  if (lchown(tgt_path, getuid(), getgid())) {
    fprintf(stderr, "lchown failed on file %s\n", tgt_path);
    return 0;
  }
  return 1;
}

canonical_original_code:
_Bool set_permissions_and_link(char *nbd_path, char *tgt_path, int extlen) {
  struct stat s;
  if (stat(nbd_path, &s)) {
    fprintf(stderr, "stat failed on file %s\n", nbd_path);
    return 0;
  }
  if (chown(nbd_path, getuid(), s.st_gid)) {
    fprintf(stderr, "chown failed on file %s\n", nbd_path);
    return 0;
  }
  if (symlink(nbd_path, tgt_path)) {
    fprintf(stderr, "symlink failed %s -> %s\n", tgt_path, nbd_path);
    return 0;
  }
  if (lchown(tgt_path, getuid(), getgid())) {
    fprintf(stderr, "lchown failed on file %s\n", tgt_path);
    return 0;
  }
  return 1;
}


decompiled_code:
bool __fastcall try_nbd(void *src, __int64 a2)
{
size_t v2; // rbx
void *v3; // rsp
char *v4; // rax
int v5; // eax
int v7; // er12
char v8[12]; // [rsp+0h] [rbp-30h] BYREF
int stat_loc; // [rsp+Ch] [rbp-24h] BYREF

v2 = strlen((const char *)src);
v3 = alloca(v2 + 6);
v4 = stpcpy(v8, dev);
memcpy(v4, src, v2 + 1);
v5 = fork();
if ( v5 < 0 )
return 0;
if ( !v5 )
{
v7 = open("/dev/null", 2);
dup2(v7, 0);
dup2(v7, 1);
dup2(v7, 2);
close(v7);
execl(qemu_nbd_path, qemu_nbd, "-c", v8, a2, 0LL);
exit(1);
}
waitpid(v5, &stat_loc, 0);
return (stat_loc & 0x7F | BYTE1(stat_loc)) == 0;
}

canonical_decompiled_code:
bool __fastcall func1(void *src, __int64 a2)
{
size_t v2; // rbx
void *v3; // rsp
char *v4; // rax
int v5; // eax
int v7; // er12
char v8[12]; // [rsp+0h] [rbp-30h] BYREF
int stat_loc; // [rsp+Ch] [rbp-24h] BYREF

v2 = strlen((const char *)src);
v3 = alloca(v2 + 6);
v4 = stpcpy(v8, dev);
memcpy(v4, src, v2 + 1);
v5 = fork();
if ( v5 < 0 )
return 0;
if ( !v5 )
{
v7 = open("/dev/null", 2);
dup2(v7, 0);
dup2(v7, 1);
dup2(v7, 2);
close(v7);
execl(qemu_nbd_path, qemu_nbd, "-c", v8, a2, 0LL);
exit(1);
}
waitpid(v5, &stat_loc, 0);
return (stat_loc & 0x7F | BYTE1(stat_loc)) == 0;
}

original_code:
_Bool try_nbd(const char *nbd, const char *img) {
  char nbd_path[strlen(nbd) + 6];
  pid_t pid;
  int status;
  strcpy(nbd_path, dev);
  strcat(nbd_path, nbd);
  pid = fork();
  if (pid < 0) {
    return 0;
  } else if (pid == 0) {
    int devnull = open("/dev/null", 02);
    dup2(devnull, 0);
    dup2(devnull, 1);
    dup2(devnull, 2);
    close(devnull);
    execl(qemu_nbd_path, qemu_nbd, "-c", nbd_path, img, (char*)((void *)0));
    _exit(1);
  } else {
    waitpid(pid, &status, 0);
    return (((status) & 0x7f) == 0) && ((((status) & 0xff00) >> 8) == 0);
  }
}

canonical_original_code:
_Bool try_nbd(char *nbd, char *img) {
  char nbd_path[-1];
  int pid;
  int status;
  strcpy(nbd_path, dev);
  strcat(nbd_path, nbd);
  pid = fork();
  if (pid < 0) {
    return 0;
  } else if (pid == 0) {
    int devnull = open("/dev/null", 02);
    dup2(devnull, 0);
    dup2(devnull, 1);
    dup2(devnull, 2);
    close(devnull);
    execl(qemu_nbd_path, qemu_nbd, "-c", nbd_path, img, (char *)((void *)0));
    _exit(1);
  } else {
    waitpid(pid, &status, 0);
    return (((status) & 0x7f) == 0) && ((((status) & 0xff00) >> 8) == 0);
  }
}

variable_types:
img
char *

variable_types: pid int

variable_types: status int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
const char *v5; // r14
const char *v6; // r13
char *v7; // r14
char v8; // r13

if ( argc == 3 )
{
v5 = argv[1];
v6 = argv[2];
v3 = access(v5, 6);
if ( v3 )
{
v3 = 1;
fprintf(stderr, "Insufficcient permissions for %s\n", v5);
}
else if ( (unsigned __int8)parent_dir_is_writeable(v6) )
{
if ( (unsigned __int8)does_not_exist(v6) )
{
v7 = bind_nbd((__int64)v5);
if ( v7 )
{
usleep(0xC350u);
v8 = each_nbd(v7, v6, set_permissions_and_link);
free(v7);
if ( !v8 )
{
v3 = 1;
fwrite("Unable to set permissions and link\n", 1uLL, 0x23uLL, stderr);
}
}
else
{
v3 = 1;
fwrite("Unable to bind nbd\n", 1uLL, 0x13uLL, stderr);
}
}
else
{
v3 = 1;
fprintf(stderr, "%s already exists\n", v6);
}
}
else
{
v3 = 1;
fprintf(stderr, "Insufficcient permissions to create %s\n", v6);
}
}
else
{
v3 = 1;
fwrite("Usage: nbd-bind image target\n", 1uLL, 0x1DuLL, stderr);
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
const char *v5; // r14
const char *v6; // r13
char *v7; // r14
char v8; // r13

if ( argc == 3 )
{
v5 = argv[1];
v6 = argv[2];
v3 = access(v5, 6);
if ( v3 )
{
v3 = 1;
fprintf(stderr, "Insufficcient permissions for %s\n", v5);
}
else if ( (unsigned __int8)parent_dir_is_writeable(v6) )
{
if ( (unsigned __int8)does_not_exist(v6) )
{
v7 = func2((__int64)v5);
if ( v7 )
{
usleep(0xC350u);
v8 = each_nbd(v7, v6, set_permissions_and_link);
free(v7);
if ( !v8 )
{
v3 = 1;
fwrite("Unable to set permissions and link\n", 1uLL, 0x23uLL, stderr);
}
}
else
{
v3 = 1;
fwrite("Unable to bind nbd\n", 1uLL, 0x13uLL, stderr);
}
}
else
{
v3 = 1;
fprintf(stderr, "%s already exists\n", v6);
}
}
else
{
v3 = 1;
fprintf(stderr, "Insufficcient permissions to create %s\n", v6);
}
}
else
{
v3 = 1;
fwrite("Usage: nbd-bind image target\n", 1uLL, 0x1DuLL, stderr);
}
return v3;
}

original_code:
int main(int argc, char **argv) {
  if (argc != 3) {
    fprintf(stderr, "Usage: nbd-bind image target\n");
    return 1;
  }
  char *img = argv[1];
  char *tgt = argv[2];
  if (access(img, 4 | 2)) {
    fprintf(stderr, "Insufficcient permissions for %s\n", img);
    return 1;
  }
  if (!parent_dir_is_writeable(tgt)) {
    fprintf(stderr, "Insufficcient permissions to create %s\n", tgt);
    return 1;
  }
  if (!does_not_exist(tgt)) {
    fprintf(stderr, "%s already exists\n", tgt);
    return 1;
  }
  char *nbd = bind_nbd(img);
  if (nbd == ((void *)0)) {
    fprintf(stderr, "Unable to bind nbd\n");
    return 1;
  }
  usleep(50000);
  _Bool r = each_nbd(nbd, tgt, &set_permissions_and_link);
  free(nbd);
  if (r) {
    return 0;
  } else {
    fprintf(stderr, "Unable to set permissions and link\n");
    return 1;
  }
}


decompiled_code:
void startup_constructor()
{
char *v0; // rsi
char *v1; // rsi
char *v2; // rsi

if ( !g_have_settings )
{
v0 = getenv("SDL_FS_TOGGLE");
if ( v0 && strcmp("0", v0) )
g_enable_fullscreen_toggle = 1;
v1 = getenv("SDL_FS_TOGGLE_NO_UNGRAB");
if ( v1 && strcmp("1", v1) )
g_enable_ungrab = 0;
v2 = getenv("SDL_HDB_COLORKEY_FIX");
if ( v2 )
{
if ( strcmp("0", v2) )
g_enable_hdb_colorkey_fix = 1;
}
g_have_settings = 1;
}
}

canonical_decompiled_code:
void func2()
{
char *v0; // rsi
char *v1; // rsi
char *v2; // rsi

if ( !g_have_settings )
{
v0 = getenv("SDL_FS_TOGGLE");
if ( v0 && strcmp("0", v0) )
g_enable_fullscreen_toggle = 1;
v1 = getenv("SDL_FS_TOGGLE_NO_UNGRAB");
if ( v1 && strcmp("1", v1) )
g_enable_ungrab = 0;
v2 = getenv("SDL_HDB_COLORKEY_FIX");
if ( v2 )
{
if ( strcmp("0", v2) )
g_enable_hdb_colorkey_fix = 1;
}
g_have_settings = 1;
}
}

original_code:
static void __attribute__ ((constructor)) startup_constructor()
{
    char *str;
    if (!g_have_settings) {
        str = getenv("SDL_FS_TOGGLE");
        if (str && strcmp("0", str)) {
            g_enable_fullscreen_toggle = 1;
        }
        str = getenv("SDL_FS_TOGGLE_NO_UNGRAB");
        if (str && strcmp("1", str)) {
            g_enable_ungrab = 0;
        }
        str = getenv("SDL_HDB_COLORKEY_FIX");
        if (str && strcmp("0", str)) {
            g_enable_hdb_colorkey_fix = 1;
        }
        g_have_settings = 1;
    }
}

canonical_original_code:
static void startup_constructor()
{
    char *str;
    if (!g_have_settings) {
        str = getenv("SDL_FS_TOGGLE");
        if (str && strcmp("0", str)) {
            g_enable_fullscreen_toggle = 1;
        }
        str = getenv("SDL_FS_TOGGLE_NO_UNGRAB");
        if (str && strcmp("1", str)) {
            g_enable_ungrab = 0;
        }
        str = getenv("SDL_HDB_COLORKEY_FIX");
        if (str && strcmp("0", str)) {
            g_enable_hdb_colorkey_fix = 1;
        }
        g_have_settings = 1;
    }
}


repo: markfasheh/duperemove

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

gettimeofday((struct timeval *)(a1 + 16), 0LL);
result = *(_QWORD *)(a1 + 16) - *(_QWORD *)a1;
*(double *)(a1 + 40) = (float)((float)((float)(*(_DWORD *)(a1 + 24) - *(_DWORD *)(a1 + 8)) / 1000000.0)
+ (float)(int)result);
return result;
}

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

gettimeofday((struct timeval *)(a1 + 16), 0LL);
result = *(_QWORD *)(a1 + 16) - *(_QWORD *)a1;
*(double *)(a1 + 40) = (float)((float)((float)(*(_DWORD *)(a1 + 24) - *(_DWORD *)(a1 + 8)) / 1000000.0)
+ (float)(int)result);
return result;
}

original_code:
static void record_end(struct elapsed_time *e)
{
 gettimeofday(&e->end, ((void *)0));
 e->elapsed = (e->end.tv_sec - e->start.tv_sec) +
  ((e->end.tv_usec - e->start.tv_usec) / 1000000.0F);
}

variable_types: struct elapsed_time { struct timeval { long tv_sec; long tv_usec; } start; struct timeval end; char *name; double elapsed; } *

user_defined_types: struct elapsed_time { struct timeval start; struct timeval end; char *name; double elapsed; }


decompiled_code:
unsigned __int64 __fastcall parse_size(char *nptr)
{
__int64 v1; // rbp
__int64 v2; // rbx
int v3; // er13
__int64 v4; // rbx
__int32_t v6; // edx
char v7; // al

if ( !nptr )
goto LABEL_20;
v1 = 0LL;
do
{
v2 = nptr[v1];
v3 = v1;
if ( !(_BYTE)v2 )
{
if ( (_DWORD)v1 )
{
v4 = 1LL;
return v4 * strtoull(nptr, 0LL, 10);
}
LABEL_20:
fwrite("ERROR: size value is empty\n", 1uLL, 0x1BuLL, stderr);
exit(50);
}
++v1;
}
while ( ((*_ctype_b_loc())[v2] & 0x800) != 0 );
if ( !v3 )
goto LABEL_20;
v6 = (*_ctype_tolower_loc())[v2];
switch ( (char)v6 )
{
case 'G':
case 'g':
v4 = 0x40000000LL;
break;
case 'K':
case 'k':
v4 = 1024LL;
break;
case 'M':
case 'm':
v4 = 0x100000LL;
break;
case 'b':
v4 = 1LL;
break;
case 'e':
v4 = 0x1000000000000000LL;
break;
case 'p':
v4 = 0x4000000000000LL;
break;
case 't':
v4 = 0x10000000000LL;
break;
default:
fprintf(stderr, "ERROR: Unknown size descriptor '%c'\n", (unsigned int)(char)v6);
exit(1);
}
v7 = nptr[v1];
if ( v7 )
{
fprintf(stderr, "ERROR: Illegal suffix contains character '%c' in wrong position\n", (unsigned int)v7);
exit(51);
}
return v4 * strtoull(nptr, 0LL, 10);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(char *nptr)
{
__int64 v1; // rbp
__int64 v2; // rbx
int v3; // er13
__int64 v4; // rbx
__int32_t v6; // edx
char v7; // al

if ( !nptr )
goto LABEL_20;
v1 = 0LL;
do
{
v2 = nptr[v1];
v3 = v1;
if ( !(_BYTE)v2 )
{
if ( (_DWORD)v1 )
{
v4 = 1LL;
return v4 * strtoull(nptr, 0LL, 10);
}
LABEL_20:
fwrite("ERROR: size value is empty\n", 1uLL, 0x1BuLL, stderr);
exit(50);
}
++v1;
}
while ( ((*_ctype_b_loc())[v2] & 0x800) != 0 );
if ( !v3 )
goto LABEL_20;
v6 = (*_ctype_tolower_loc())[v2];
switch ( (char)v6 )
{
case 'G':
case 'g':
v4 = 0x40000000LL;
break;
case 'K':
case 'k':
v4 = 1024LL;
break;
case 'M':
case 'm':
v4 = 0x100000LL;
break;
case 'b':
v4 = 1LL;
break;
case 'e':
v4 = 0x1000000000000000LL;
break;
case 'p':
v4 = 0x4000000000000LL;
break;
case 't':
v4 = 0x10000000000LL;
break;
default:
fprintf(stderr, "ERROR: Unknown size descriptor '%c'\n", (unsigned int)(char)v6);
exit(1);
}
v7 = nptr[v1];
if ( v7 )
{
fprintf(stderr, "ERROR: Illegal suffix contains character '%c' in wrong position\n", (unsigned int)v7);
exit(51);
}
return v4 * strtoull(nptr, 0LL, 10);
}

original_code:
uint64_t parse_size(char *s)
{
 int i;
 char c;
 uint64_t mult = 1;
 for (i = 0; s && s[i] && ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit); i++) ;
 if (!i) {
  fprintf(stderr, "ERROR: size value is empty\n");
  exit(50);
 }
 if (s[i]) {
  c = tolower(s[i]);
  switch (c) {
  case 'e':
   mult *= 1024;
  case 'p':
   mult *= 1024;
  case 't':
   mult *= 1024;
  case 'g':
  case 'G':
   mult *= 1024;
  case 'm':
  case 'M':
   mult *= 1024;
  case 'k':
  case 'K':
   mult *= 1024;
  case 'b':
   break;
  default:
   fprintf(stderr, "ERROR: Unknown size descriptor "
    "'%c'\n", c);
   exit(1);
  }
 }
 if (s[i] && s[i+1]) {
  fprintf(stderr, "ERROR: Illegal suffix contains "
   "character '%c' in wrong position\n",
   s[i+1]);
  exit(51);
 }
 return strtoull(s, ((void *)0), 10) * mult;
}

canonical_original_code:
unsigned long parse_size(char *s)
{
 int i;
 char c;
 unsigned long mult = 1;
 for (i = 0; s && s[i] && ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit); i++) ;
 if (!i) {
  fprintf(stderr, "ERROR: size value is empty\n");
  exit(50);
 }
 if (s[i]) {
  c = tolower(s[i]);
  switch (c) {
  case 'e':
   mult *= 1024;
  case 'p':
   mult *= 1024;
  case 't':
   mult *= 1024;
  case 'g':
  case 'G':
   mult *= 1024;
  case 'm':
  case 'M':
   mult *= 1024;
  case 'k':
  case 'K':
   mult *= 1024;
  case 'b':
   break;
  default:
   fprintf(stderr, "ERROR: Unknown size descriptor "
    "'%c'\n", c);
   exit(1);
  }
 }
 if (s[i] && s[i+1]) {
  fprintf(stderr, "ERROR: Illegal suffix contains "
   "character '%c' in wrong position\n",
   s[i+1]);
  exit(51);
 }
 return strtoull(s, ((void *)0), 10) * mult;
}

variable_types:
s
char *

variable_types: i int

variable_types: c char

variable_types:
mult
unsigned long

return_type: unsigned long


decompiled_code:
int __fastcall pretty_size_snprintf(unsigned __int64 a1, char *a2, size_t a3)
{
__int64 v6; // rax
int v7; // edx
const char *v8; // rcx
float v9; // xmm0_4

if ( !a3 )
return 0;
if ( !human_readable )
return snprintf(a2, a3, "%lu", a1);
if ( a1 <= 0x3FF )
{
v9 = (float)(int)a1;
v8 = (const char *)&unk_778;
return snprintf(a2, a3, "%.1f%sB", v9, v8);
}
LODWORD(v6) = 0;
do
{
v7 = a1;
a1 >>= 10;
v6 = (unsigned int)(v6 + 1);
}
while ( a1 > 0x3FF );
if ( (unsigned int)v6 <= 6 )
{
v8 = (const char *)*(&size_strs + v6);
v9 = (float)v7 * 0.0009765625;
return snprintf(a2, a3, "%.1f%sB", v9, v8);
}
*a2 = 0;
return -1;
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int64 a1, char *a2, size_t a3)
{
__int64 v6; // rax
int v7; // edx
const char *v8; // rcx
float v9; // xmm0_4

if ( !a3 )
return 0;
if ( !human_readable )
return snprintf(a2, a3, "%lu", a1);
if ( a1 <= 0x3FF )
{
v9 = (float)(int)a1;
v8 = (const char *)&unk_778;
return snprintf(a2, a3, "%.1f%sB", v9, v8);
}
LODWORD(v6) = 0;
do
{
v7 = a1;
a1 >>= 10;
v6 = (unsigned int)(v6 + 1);
}
while ( a1 > 0x3FF );
if ( (unsigned int)v6 <= 6 )
{
v8 = (const char *)*(&size_strs + v6);
v9 = (float)v7 * 0.0009765625;
return snprintf(a2, a3, "%.1f%sB", v9, v8);
}
*a2 = 0;
return -1;
}

original_code:
int pretty_size_snprintf(uint64_t size, char *str, size_t str_bytes)
{
 uint32_t num_divs = 0;
 float fraction;
 if (str_bytes == 0)
  return 0;
 if (!human_readable)
  return snprintf(str, str_bytes, "%""l" "u", size);
 if (size < 1024){
  fraction = size;
  num_divs = 0;
 } else {
  uint64_t last_size = size;
  num_divs = 0;
  while(size >= 1024){
   last_size = size;
   size /= 1024;
   num_divs ++;
  }
  if (num_divs >= (sizeof(size_strs) / sizeof((size_strs)[0]))) {
   str[0] = '\0';
   return -1;
  }
  fraction = (float)last_size / 1024;
 }
 return snprintf(str, str_bytes, "%.1f%sB", fraction,
   size_strs[num_divs]);
}

canonical_original_code:
int pretty_size_snprintf(unsigned long size, char *str, unsigned long str_bytes)
{
 unsigned int num_divs = 0;
 float fraction;
 if (str_bytes == 0)
  return 0;
 if (!human_readable)
  return snprintf(str, str_bytes, "%""l" "u", size);
 if (size < 1024){
  fraction = size;
  num_divs = 0;
 } else {
  unsigned long last_size = size;
  num_divs = 0;
  while(size >= 1024){
   last_size = size;
   size /= 1024;
   num_divs ++;
  }
  if (num_divs >= (sizeof(size_strs) / sizeof((size_strs)[0]))) {
   str[0] = '\0';
   return -1;
  }
  fraction = (float)last_size / 1024;
 }
 return snprintf(str, str_bytes, "%.1f%sB", fraction,
   size_strs[num_divs]);
}

variable_types:
size
unsigned long

variable_types: fraction float


decompiled_code:
void print_stack_trace()
{
int v0; // ebp
char **v1; // r12
const char **v2; // rbx
__int64 v3; // rbp
const char *v4; // rdi
void *v5[19]; // [rsp+0h] [rbp-98h] BYREF

v0 = backtrace(v5, 16);
v1 = backtrace_symbols(v5, v0);
puts("[stack trace follows]");
if ( v0 > 0 )
{
v2 = (const char **)v1;
v3 = (__int64)&v1[(unsigned int)(v0 - 1) + 1];
do
{
v4 = *v2++;
puts(v4);
}
while ( v2 != (const char **)v3 );
}
free(v1);
}

canonical_decompiled_code:
void func4()
{
int v0; // ebp
char **v1; // r12
const char **v2; // rbx
__int64 v3; // rbp
const char *v4; // rdi
void *v5[19]; // [rsp+0h] [rbp-98h] BYREF

v0 = backtrace(v5, 16);
v1 = backtrace_symbols(v5, v0);
puts("[stack trace follows]");
if ( v0 > 0 )
{
v2 = (const char **)v1;
v3 = (__int64)&v1[(unsigned int)(v0 - 1) + 1];
do
{
v4 = *v2++;
puts(v4);
}
while ( v2 != (const char **)v3 );
}
free(v1);
}

original_code:
void print_stack_trace(void)
{
 void *trace[16];
 char **messages = (char **)((void *)0);
 int i, trace_size = 0;
 trace_size = backtrace(trace, 16);
 messages = backtrace_symbols(trace, trace_size);
 printf("[stack trace follows]\n");
 for (i=0; i < trace_size; i++)
  printf("%s\n", messages[i]);
 free(messages);
}

canonical_original_code:
void print_stack_trace()
{
 void *trace[16];
 char **messages = (char **)((void *)0);
 int i; int trace_size = 0;
 trace_size = backtrace(trace, 16);
 messages = backtrace_symbols(trace, trace_size);
 printf("[stack trace follows]\n");
 for (i=0; i < trace_size; i++)
  printf("%s\n", messages[i]);
 free(messages);
}

variable_types:
trace
void *[16]

variable_types:
messages
char **


decompiled_code:
int __fastcall record_start(struct timeval *a1, __time_t a2)
{
a1[2].tv_sec = a2;
return gettimeofday(a1, 0LL);
}

canonical_decompiled_code:
int __fastcall func5(struct timeval *a1, __time_t a2)
{
a1[2].tv_sec = a2;
return gettimeofday(a1, 0LL);
}

original_code:
void record_start(struct elapsed_time *e, const char *name)
{
 e->name = name;
 gettimeofday(&e->start, ((void *)0));
}

canonical_original_code:
void record_start(struct elapsed_time *e, char *name)
{
 e->name = name;
 gettimeofday(&e->start, ((void *)0));
}

variable_types:
name
char *

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


decompiled_code:
int __fastcall record_end_print(__int64 a1)
{
record_end(a1);
return printf("%s took %fs\n", *(const char **)(a1 + 32), *(double *)(a1 + 40));
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
func0(a1);
return printf("%s took %fs\n", *(const char **)(a1 + 32), *(double *)(a1 + 40));
}

original_code:
void record_end_print(struct elapsed_time *e)
{
 record_end(e);
 printf("%s took %fs\n", e->name, e->elapsed);
}


decompiled_code:
__int64 __fastcall num_digits(unsigned __int64 a1)
{
unsigned __int64 v1; // rdx
unsigned int v2; // esi
unsigned __int64 v3; // rcx

v1 = a1;
if ( !a1 )
return 0LL;
v2 = 0;
do
{
v3 = v1;
++v2;
v1 /= 0xAuLL;
}
while ( v3 > 9 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int64 a1)
{
unsigned __int64 v1; // rdx
unsigned int v2; // esi
unsigned __int64 v3; // rcx

v1 = a1;
if ( !a1 )
return 0LL;
v2 = 0;
do
{
v3 = v1;
++v2;
v1 /= 0xAuLL;
}
while ( v3 > 9 );
return v2;
}

original_code:
int num_digits(unsigned long long num)
{
 unsigned int digits = 0;
 while (num) {
  num /= 10;
  digits++;
 }
 return digits;
}

variable_types:
num
unsigned long long

variable_types:
digits
unsigned int


decompiled_code:
int __fastcall get_num_cpus(int *a1, int *a2)
{
__int64 v2; // rax
__int64 v3; // rsi
__int64 v4; // rdx
const char *v5; // rcx

if ( (int)get_core_count(a1, a2) < 0 )
{
LODWORD(v2) = get_core_count_fallback(a1, a2);
if ( (int)v2 >= 0 )
{
if ( !debug )
return v2;
v4 = (unsigned int)*a1;
v3 = (unsigned int)*a2;
v5 = "is off";
}
else
{
v2 = sysconf(84);
*a2 = v2;
v3 = v2;
v4 = (unsigned int)v2;
*a1 = v2;
if ( !debug )
return v2;
v5 = "detection broken";
}
}
else
{
LODWORD(v2) = debug;
if ( !debug )
return v2;
v3 = (unsigned int)*a2;
v4 = (unsigned int)*a1;
v5 = "is off";
if ( (unsigned int)v3 > (unsigned int)v4 )
v5 = "is on";
}
LODWORD(v2) = printf("Detected %u logical and %u physical cpus (ht %s).\n", v3, v4, v5);
return v2;
}

canonical_decompiled_code:
int __fastcall func9(int *a1, int *a2)
{
__int64 v2; // rax
__int64 v3; // rsi
__int64 v4; // rdx
const char *v5; // rcx

if ( (int)func8(a1, a2) < 0 )
{
LODWORD(v2) = func1(a1, a2);
if ( (int)v2 >= 0 )
{
if ( !debug )
return v2;
v4 = (unsigned int)*a1;
v3 = (unsigned int)*a2;
v5 = "is off";
}
else
{
v2 = sysconf(84);
*a2 = v2;
v3 = v2;
v4 = (unsigned int)v2;
*a1 = v2;
if ( !debug )
return v2;
v5 = "detection broken";
}
}
else
{
LODWORD(v2) = debug;
if ( !debug )
return v2;
v3 = (unsigned int)*a2;
v4 = (unsigned int)*a1;
v5 = "is off";
if ( (unsigned int)v3 > (unsigned int)v4 )
v5 = "is on";
}
LODWORD(v2) = printf("Detected %u logical and %u physical cpus (ht %s).\n", v3, v4, v5);
return v2;
}

original_code:
void get_num_cpus(unsigned int *nr_phys, unsigned int *nr_log)
{
 int ht = 0;
 int ret = get_core_count(nr_phys, nr_log);
 if (ret < 0) {
  ret = get_core_count_fallback(nr_phys, nr_log);
  if (ret < 0)
   *nr_phys = *nr_log = sysconf(_SC_NPROCESSORS_ONLN);
 } else
  ht = *nr_log > *nr_phys;
 if (debug) printf("Detected %u logical and %u physical cpus (ht %s).\n", *nr_log, *nr_phys, ht ? "is on" : ret < 0 ? "detection broken" : "is off");
}

variable_types: ht int

variable_types: ret int


decompiled_code:
__int64 increase_limits()
{
__int64 result; // rax
struct rlimit64 v1; // [rsp+0h] [rbp-28h] BYREF
struct rlimit64 rlimits; // [rsp+10h] [rbp-18h] BYREF

if ( getrlimit64(RLIMIT_NOFILE, &v1) < 0 )
return (unsigned int)-*_errno_location();
rlimits.rlim_cur = v1.rlim_max;
rlimits.rlim_max = v1.rlim_max;
if ( setrlimit64(RLIMIT_NOFILE, &rlimits) < 0 )
return (unsigned int)-*_errno_location();
result = verbose;
if ( verbose )
{
printf("Increased open file limit from %llu to %llu.\n", v1.rlim_cur, rlimits.rlim_cur);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 result; // rax
struct rlimit64 v1; // [rsp+0h] [rbp-28h] BYREF
struct rlimit64 rlimits; // [rsp+10h] [rbp-18h] BYREF

if ( getrlimit64(RLIMIT_NOFILE, &v1) < 0 )
return (unsigned int)-*_errno_location();
rlimits.rlim_cur = v1.rlim_max;
rlimits.rlim_max = v1.rlim_max;
if ( setrlimit64(RLIMIT_NOFILE, &rlimits) < 0 )
return (unsigned int)-*_errno_location();
result = verbose;
if ( verbose )
{
printf("Increased open file limit from %llu to %llu.\n", v1.rlim_cur, rlimits.rlim_cur);
result = 0LL;
}
return result;
}

original_code:
int increase_limits(void) {
 struct rlimit cur_r;
 struct rlimit new_r;
 int ret;
 ret = getrlimit(RLIMIT_NOFILE, &cur_r);
 if (ret < 0)
  return -(*__errno_location ());
 new_r.rlim_cur = cur_r.rlim_max;
 new_r.rlim_max = cur_r.rlim_max;
 ret = setrlimit(RLIMIT_NOFILE, &new_r);
 if (ret < 0)
  return -(*__errno_location ());
 if (verbose) printf("Increased open file limit from %llu to %llu.\n", (unsigned long long)cur_r.rlim_cur, (unsigned long long)new_r.rlim_cur);
 return 0;
}

canonical_original_code:
int increase_limits() {
 struct rlimit cur_r;
 struct rlimit new_r;
 int ret;
 ret = getrlimit(RLIMIT_NOFILE, &cur_r);
 if (ret < 0)
  return -(*__errno_location ());
 new_r.rlim_cur = cur_r.rlim_max;
 new_r.rlim_max = cur_r.rlim_max;
 ret = setrlimit(RLIMIT_NOFILE, &new_r);
 if (ret < 0)
  return -(*__errno_location ());
 if (verbose) printf("Increased open file limit from %llu to %llu.\n", (unsigned long long)cur_r.rlim_cur, (unsigned long long)new_r.rlim_cur);
 return 0;
}

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }


decompiled_code:
int print_mem_stats()
{
__int64 v0; // r12
__int64 v1; // rax

puts("Duperemove memory usage statistics:");
show_allocs_file_block();
show_allocs_dupe_blocks_list();
show_allocs_dupe_extents();
show_allocs_extent();
show_allocs_filerec();
show_allocs_filerec_token();
show_allocs_file_hash_head();
show_allocs_find_dupes_cmp();
v0 = sqlite3_memory_highwater(0LL);
v1 = sqlite3_memory_used();
return printf("Sqlite3 used: %lu highwater: %lu\n", v1, v0);
}

canonical_decompiled_code:
int func0()
{
__int64 v0; // r12
__int64 v1; // rax

puts("Duperemove memory usage statistics:");
show_allocs_file_block();
show_allocs_dupe_blocks_list();
show_allocs_dupe_extents();
show_allocs_extent();
show_allocs_filerec();
show_allocs_filerec_token();
show_allocs_file_hash_head();
show_allocs_find_dupes_cmp();
v0 = sqlite3_memory_highwater(0LL);
v1 = sqlite3_memory_used();
return printf("Sqlite3 used: %lu highwater: %lu\n", v1, v0);
}

original_code:
void print_mem_stats(void)
{
 uint64_t sqlite3_highwater, sqlite3_memused;
 printf("Duperemove memory usage statistics:\n");
 show_allocs_file_block();
 show_allocs_dupe_blocks_list();
 show_allocs_dupe_extents();
 show_allocs_extent();
 show_allocs_filerec();
 show_allocs_filerec_token();
 show_allocs_file_hash_head();
 show_allocs_find_dupes_cmp();
 sqlite3_highwater = sqlite3_memory_highwater(0);
 sqlite3_memused = sqlite3_memory_used();
 printf("Sqlite3 used: %""l" "u""  highwater: %""l" "u""\n",
        sqlite3_memused, sqlite3_highwater);
}

canonical_original_code:
void print_mem_stats()
{
 unsigned long sqlite3_highwater; unsigned long sqlite3_memused;
 printf("Duperemove memory usage statistics:\n");
 show_allocs_file_block();
 show_allocs_dupe_blocks_list();
 show_allocs_dupe_extents();
 show_allocs_extent();
 show_allocs_filerec();
 show_allocs_filerec_token();
 show_allocs_file_hash_head();
 show_allocs_find_dupes_cmp();
 sqlite3_highwater = sqlite3_memory_highwater(0);
 sqlite3_memused = sqlite3_memory_used();
 printf("Sqlite3 used: %""l" "u""  highwater: %""l" "u""\n",
        sqlite3_memused, sqlite3_highwater);
}


decompiled_code:
__int64 checksum_block()
{
return (**(__int64 (***)(void))csum_mod)();
}

original_code:
void checksum_block(char *buf, int len, unsigned char *digest)
{
 csum_mod->ops->checksum_block(buf, len, digest);
}

variable_types:
buf
char *

variable_types: len int

variable_types:
digest
unsigned char *


decompiled_code:
int __fastcall print_all_blocks(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp

printf(" blocks: ");
v1 = rb_first(a1 + 104);
if ( v1 )
{
v2 = v1;
do
{
printf("%lu ", *(_QWORD *)(v2 - 32));
v2 = rb_next(v2);
}
while ( v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp

printf(" blocks: ");
v1 = rb_first(a1 + 104);
if ( v1 )
{
v2 = v1;
do
{
printf("%lu ", *(_QWORD *)(v2 - 32));
v2 = rb_next(v2);
}
while ( v2 );
}
return putchar(10);
}

original_code:
static int print_all_blocks(unsigned char *digest)
{
 int ret;
 uint64_t loff;
 unsigned int flags;
 const unsigned char *filename;
 ret = sqlite3_bind_blob(find_blocks_stmt, 1, digest, 16,
    ((sqlite3_destructor_type)0));
 if (ret) {
  fprintf(stderr, "Error %d binding digest for blocks: %s\n", ret,
   sqlite3_errstr(ret));
  return ret;
 }
 while ((ret = sqlite3_step(find_blocks_stmt)) == 100) {
  filename = sqlite3_column_text(find_blocks_stmt, 0);
  loff = sqlite3_column_int64(find_blocks_stmt, 1);
  flags = sqlite3_column_int(find_blocks_stmt, 2);
  printf("  %s\tloff: %llu lblock: %llu "
         "flags: 0x%x ", filename,
         (unsigned long long)loff,
         (unsigned long long)loff / blocksize,
         flags);
  printf_file_block_flags(flags);
  printf("\n");
 }
 if (ret != 101) {
  fprintf(stderr,
   "error %d running block stmt: %s\n",
   ret, sqlite3_errstr(ret));
  return ret;
 }
 sqlite3_reset(find_blocks_stmt);
 return 0;
}

canonical_original_code:
static int print_all_blocks(unsigned char *digest)
{
 int ret;
 unsigned long loff;
 unsigned int flags;
 unsigned char *filename;
 ret = sqlite3_bind_blob(find_blocks_stmt, 1, digest, 16,
    ((void (*)(void *))0));
 if (ret) {
  fprintf(stderr, "Error %d binding digest for blocks: %s\n", ret,
   sqlite3_errstr(ret));
  return ret;
 }
 while ((ret = sqlite3_step(find_blocks_stmt)) == 100) {
  filename = sqlite3_column_text(find_blocks_stmt, 0);
  loff = sqlite3_column_int64(find_blocks_stmt, 1);
  flags = sqlite3_column_int(find_blocks_stmt, 2);
  printf("  %s\tloff: %llu lblock: %llu "
         "flags: 0x%x ", filename,
         (unsigned long long)loff,
         (unsigned long long)loff / blocksize,
         flags);
  printf_file_block_flags(flags);
  printf("\n");
 }
 if (ret != 101) {
  fprintf(stderr,
   "error %d running block stmt: %s\n",
   ret, sqlite3_errstr(ret));
  return ret;
 }
 sqlite3_reset(find_blocks_stmt);
 return 0;
}

variable_types:
loff
unsigned long

variable_types:
flags
unsigned int

variable_types:
filename
unsigned char *


decompiled_code:
int __fastcall lookup_btrfs_subvolid(int fd, _QWORD *a2)
{
int result; // eax
__int64 v3[513]; // [rsp+0h] [rbp-1008h] BYREF

memset(v3, 0, 0x1000uLL);
v3[1] = 256LL;
result = ioctl(fd, 0xD0009412uLL, v3);
if ( result )
return *_errno_location();
*a2 = v3[0];
return result;
}

canonical_decompiled_code:
int __fastcall func1(int fd, _QWORD *a2)
{
int result; // eax
__int64 v3[513]; // [rsp+0h] [rbp-1008h] BYREF

memset(v3, 0, 0x1000uLL);
v3[1] = 256LL;
result = ioctl(fd, 0xD0009412uLL, v3);
if ( result )
return *_errno_location();
*a2 = v3[0];
return result;
}

original_code:
int lookup_btrfs_subvolid(int fd, uint64_t *subvolid)
{
 int ret;
 struct btrfs_ioctl_ino_lookup_args args;
 memset(&args, 0, sizeof(args));
 args.objectid = 256ULL;
 ret = ioctl(fd, (((2U|1U) << (((0 +8)+8)+14)) | (((0x94)) << (0 +8)) | (((18)) << 0) | ((((sizeof(struct btrfs_ioctl_ino_lookup_args)))) << ((0 +8)+8))), &args);
 if (ret)
  return (*__errno_location ());
 *subvolid = args.treeid;
 return 0;
}

canonical_original_code:
int lookup_btrfs_subvolid(int fd, unsigned long *subvolid)
{
 int ret;
 struct btrfs_ioctl_ino_lookup_args args;
 memset(&args, 0, sizeof(args));
 args.objectid = 256ULL;
 ret = ioctl(fd, (((2U|1U) << (((0 +8)+8)+14)) | (((0x94)) << (0 +8)) | (((18)) << 0) | ((((sizeof(struct btrfs_ioctl_ino_lookup_args)))) << ((0 +8)+8))), &args);
 if (ret)
  return (*__errno_location ());
 *subvolid = args.treeid;
 return 0;
}

variable_types: fd int

variable_types:
subvolid
unsigned long *

variable_types: struct btrfs_ioctl_ino_lookup_args { unsigned long long treeid; unsigned long long objectid; char name[4080]; }


decompiled_code:
__int64 __fastcall check_btrfs_get_fsid(char *file, unsigned __int64 *a2)
{
int v3; // eax
int v4; // ebp
unsigned int v5; // er12
__int64 v6; // [rsp+8h] [rbp-A0h] BYREF
struct statfs64 v7; // [rsp+10h] [rbp-98h] BYREF

*a2 = 0LL;
if ( statfs64(file, &v7) )
return (unsigned int)*_errno_location();
if ( v7.f_type != 2435016766LL )
return 0LL;
v3 = open64(file, 0);
v4 = v3;
if ( v3 == -1 )
return (unsigned int)*_errno_location();
v5 = lookup_btrfs_subvolid(v3, &v6);
if ( v5 )
v5 = *_errno_location();
*a2 = get_btrfs_fsid_constprop_0(*(_QWORD *)&v7.f_fsid, v6);
close(v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *file, unsigned __int64 *a2)
{
int v3; // eax
int v4; // ebp
unsigned int v5; // er12
__int64 v6; // [rsp+8h] [rbp-A0h] BYREF
struct statfs64 v7; // [rsp+10h] [rbp-98h] BYREF

*a2 = 0LL;
if ( statfs64(file, &v7) )
return (unsigned int)*_errno_location();
if ( v7.f_type != 2435016766LL )
return 0LL;
v3 = open64(file, 0);
v4 = v3;
if ( v3 == -1 )
return (unsigned int)*_errno_location();
v5 = func1(v3, &v6);
if ( v5 )
v5 = *_errno_location();
*a2 = func0(*(_QWORD *)&v7.f_fsid, v6);
close(v4);
return v5;
}

original_code:
int check_btrfs_get_fsid(char *name, uint64_t *ret_fsid)
{
 struct statfs fs;
 int ret, fd;
 uint64_t subvol;
 *ret_fsid = 0;
 ret = statfs(name, &fs);
 if (ret) {
  ret = (*__errno_location ());
  goto out;
 }
 if (fs.f_type != 0x9123683E)
  return 0;
 fd = open(name, 00);
 if (fd == -1) {
  ret = (*__errno_location ());
  goto out;
 }
 ret = lookup_btrfs_subvolid(fd, &subvol);
 if (ret)
  ret = (*__errno_location ());
 *ret_fsid = get_btrfs_fsid(fs.f_fsid, subvol, ((void *)0));
 close(fd);
out:
 return ret;
}

canonical_original_code:
int check_btrfs_get_fsid(char *name, unsigned long *ret_fsid)
{
 struct statfs fs;
 int ret; int fd;
 unsigned long subvol;
 *ret_fsid = 0;
 ret = statfs(name, &fs);
 if (ret) {
  ret = (*__errno_location ());
  goto out;
 }
 if (fs.f_type != 0x9123683E)
  return 0;
 fd = open(name, 00);
 if (fd == -1) {
  ret = (*__errno_location ());
  goto out;
 }
 ret = lookup_btrfs_subvolid(fd, &subvol);
 if (ret)
  ret = (*__errno_location ());
 *ret_fsid = get_btrfs_fsid(fs.f_fsid, subvol, ((void *)0));
 close(fd);
out:
 return ret;
}

variable_types: struct statfs { long f_type; long f_bsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; struct __fsid_t { int __val[2]; } f_fsid; long f_namelen; long f_frsize; long f_flags; long f_spare[4]; }

variable_types:
subvol
unsigned long

user_defined_types: struct statfs { long f_type; long f_bsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; struct __fsid_t f_fsid; long f_namelen; long f_frsize; long f_flags; long f_spare[4]; }


decompiled_code:
_BOOL8 __fastcall list_empty(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

original_code:
static inline int list_empty(const struct list_head *head)
{
 return (head->next) == head;
}

canonical_original_code:
static inline int list_empty(struct list_head *head)
{
 return (head->next) == head;
}

variable_types: struct list_head { struct list_head *next; } *


merge

original_code:
static struct list_head *merge(void *priv,
    int (*cmp)(void *priv, struct list_head *a,
     struct list_head *b),
    struct list_head *a, struct list_head *b)
{
 struct list_head head, *tail = &head;
 while (a && b) {
  if ((*cmp)(priv, a, b) <= 0) {
   tail->next = a;
   a = a->next;
  } else {
   tail->next = b;
   b = b->next;
  }
  tail = tail->next;
 }
 tail->next = a?:b;
 return head.next;
}

canonical_original_code:
static struct list_head *merge(void *priv, int (*cmp)(void *priv, struct list_head *a, struct list_head *b), struct list_head *a, struct list_head *b)
{
 struct list_head head; struct list_head *tail = &head;
 while (a && b) {
  if ((*cmp)(priv, a, b) <= 0) {
   tail->next = a;
   a = a->next;
  } else {
   tail->next = b;
   b = b->next;
  }
  tail = tail->next;
 }
 tail->next = a?:b;
 return head.next;
}

variable_types:
priv
void *

variable_types: int (*)(void *priv, struct list_head *a, struct list_head *b)

variable_types:
a
struct list_head *

variable_types:
b
struct list_head *

return_type: struct list_head { struct list_head *next; } *


repo: markokr/mdrotor

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

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

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

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

original_code:
usec_t get_real_time(void)
{
 struct timeval tv;
 gettimeofday(&tv, ((void *)0));
 return ((usec_t)tv.tv_sec) * 1000000 + tv.tv_usec;
}

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

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


die

decompiled_code:
void __fastcall __noreturn die(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(1);
}

original_code:
void die(const char *reason)
{
 fprintf(stderr, "%s\n", reason);
 exit(1);
}

canonical_original_code:
void die(char *reason)
{
 fprintf(stderr, "%s\n", reason);
 exit(1);
}

variable_types:
reason
char *


hexval

decompiled_code:
__int64 __fastcall hexval(char a1)
{
if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(char)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 5u )
return (unsigned int)(char)(a1 - 87);
if ( (unsigned __int8)(a1 - 65) > 5u )
die("bad hex val");
return (unsigned int)(char)(a1 - 55);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(char)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 5u )
return (unsigned int)(char)(a1 - 87);
if ( (unsigned __int8)(a1 - 65) > 5u )
func1("bad hex val");
return (unsigned int)(char)(a1 - 55);
}

original_code:
static int hexval(char c) {
 if (c >= '0' && c <= '9') return c - '0';
 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
 die("bad hex val");
 return -1;
}

variable_types: c char


zmalloc

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

result = calloc((unsigned int)nmemb, 1uLL);
if ( !result )
die("no mem");
return result;
}

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

result = calloc((unsigned int)nmemb, 1uLL);
if ( !result )
func1("no mem");
return result;
}

original_code:
void *zmalloc(unsigned len)
{
 void *p = malloc(len);
 if (!p)
  die("no mem");
 memset(p, 0, len);
 return p;
}

canonical_original_code:
void *zmalloc(unsigned int len)
{
 void *p = malloc(len);
 if (!p)
  die("no mem");
 memset(p, 0, len);
 return p;
}

variable_types:
len
unsigned int

variable_types:
p
void *

return_type: void *


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

result = (unsigned int)n_1;
if ( n_1 <= 0 )
{
result = sysconf(84);
if ( (int)result > 0 )
{
n_1 = result;
}
else
{
n_1 = 1;
result = 1LL;
}
}
return result;
}

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

result = (unsigned int)n_1;
if ( n_1 <= 0 )
{
result = sysconf(84);
if ( (int)result > 0 )
{
n_1 = result;
}
else
{
n_1 = 1;
result = 1LL;
}
}
return result;
}

original_code:
int get_number_of_cpus(void)
{
 static int n = 0;
 if (n < 1) {
  n = sysconf(_SC_NPROCESSORS_ONLN);
 }
 if (n < 1) n = 1;
 return n;
}

canonical_original_code:
int get_number_of_cpus()
{
 static int n = 0;
 if (n < 1) {
  n = sysconf(_SC_NPROCESSORS_ONLN);
 }
 if (n < 1) n = 1;
 return n;
}

variable_types: n int


decompiled_code:
unsigned __int64 __fastcall get_user_time(int a1)
{
unsigned __int64 v1; // rbx
struct rusage v3; // [rsp+0h] [rbp-A8h] BYREF

v1 = (int)get_number_of_cpus();
if ( getrusage(RUSAGE_SELF, &v3) < 0 )
die("getrusage");
if ( (int)v1 > a1 )
v1 = a1;
return (v3.ru_utime.tv_usec + 1000000 * v3.ru_utime.tv_sec) / v1;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(int a1)
{
unsigned __int64 v1; // rbx
struct rusage v3; // [rsp+0h] [rbp-A8h] BYREF

v1 = (int)func6();
if ( getrusage(RUSAGE_SELF, &v3) < 0 )
func1("getrusage");
if ( (int)v1 > a1 )
v1 = a1;
return (v3.ru_utime.tv_usec + 1000000 * v3.ru_utime.tv_sec) / v1;
}

original_code:
usec_t get_user_time(int nthreads)
{
 usec_t cur;
 struct rusage u;
 int ncpu = get_number_of_cpus();
 if (getrusage(RUSAGE_SELF, &u) < 0)
  die("getrusage");
 cur = ((usec_t)u.ru_utime.tv_sec) * 1000000 + u.ru_utime.tv_usec;
 if (ncpu > nthreads) ncpu = nthreads;
 return cur / ncpu;
}

canonical_original_code:
unsigned long get_user_time(int nthreads)
{
 unsigned long cur;
 struct rusage u;
 int ncpu = get_number_of_cpus();
 if (getrusage(RUSAGE_SELF, &u) < 0)
  die("getrusage");
 cur = ((unsigned long)u.ru_utime.tv_sec) * 1000000 + u.ru_utime.tv_usec;
 if (ncpu > nthreads) ncpu = nthreads;
 return cur / ncpu;
}

variable_types: nthreads int

variable_types:
cur
unsigned long

variable_types: struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }

variable_types: ncpu int

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


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

result = (unsigned int)got_sse2_0;
if ( got_sse2_0 < 0 )
{
_RAX = 1LL;
__asm { cpuid }
result = ((int)_RDX >> 26) & 1;
got_sse2_0 = ((int)_RDX >> 26) & 1;
}
return result;
}

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

result = (unsigned int)got_sse2_0;
if ( got_sse2_0 < 0 )
{
_RAX = 1LL;
__asm { cpuid }
result = ((int)_RDX >> 26) & 1;
got_sse2_0 = ((int)_RDX >> 26) & 1;
}
return result;
}

original_code:
int cpu_has_sse2(void)
{
 static int got_sse2 = -1;
 int regs[4];
 if (got_sse2 < 0) {
  cpuid(regs, 1);
  got_sse2 = (regs[3] >> 26) & 1;
 }
 return got_sse2;
}

canonical_original_code:
int cpu_has_sse2()
{
 static int got_sse2 = -1;
 int regs[4];
 if (got_sse2 < 0) {
  cpuid(regs, 1);
  got_sse2 = (regs[3] >> 26) & 1;
 }
 return got_sse2;
}

variable_types:
regs
int [4]


decompiled_code:
void test_add_hash()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void test_add_hash(struct EngineThread *eng, const void *hash)
{
}

canonical_original_code:
static void test_add_hash(struct EngineThread *eng, void *hash)
{
}

variable_types: struct EngineThread { struct EngineInfo { char *eng_name; char *algo_name; void (*init)(struct EngineThread *); void (*release)(struct EngineThread *); void (*add_hash)(struct EngineThread *eng, void *); void (*run)(struct EngineThread *); void (*set_length)(struct RotorStack { int nrotors; _Bool stopped; struct EngineThread *eng; struct Rotor { int *wiring; int *step; int angle; int pos; } subzero; struct Rotor stack_[-1]; int id; } *stk, unsigned int len); void (*set_char)(struct RotorStack *stk, unsigned int char_pos, unsigned int c); int hash_len; char **sample_list; } *info; struct TopRotor { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; int nrotors; int angle; _Bool stopped; int wlen; int start_len; int end_len; int *step_list; int *wiring; } *top; void *priv; _Bool stopped; int active; unsigned int counter; unsigned int last_counter; unsigned long thread; struct EngineThread *next; } *

variable_types:
hash
void *

user_defined_types: struct EngineThread { struct EngineInfo *info; struct TopRotor *top; void *priv; _Bool stopped; int active; unsigned int counter; unsigned int last_counter; unsigned long thread; struct EngineThread *next; }

user_defined_types: struct EngineInfo { char *eng_name; char *algo_name; void (*init)(struct EngineThread *); void (*release)(struct EngineThread *); void (*add_hash)(struct EngineThread *eng, void *); void (*run)(struct EngineThread *); void (*set_length)(struct RotorStack *stk, unsigned int len); void (*set_char)(struct RotorStack *stk, unsigned int char_pos, unsigned int c); int hash_len; char **sample_list; }

user_defined_types: struct TopRotor { union pthread_mutex_t lock; int nrotors; int angle; _Bool stopped; int wlen; int start_len; int end_len; int *step_list; int *wiring; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }

user_defined_types: struct RotorStack { int nrotors; _Bool stopped; struct EngineThread *eng; struct Rotor subzero; struct Rotor stack_[-1]; int id; }


decompiled_code:
int __fastcall test_set_char(__int64 a1, unsigned int a2, unsigned int a3)
{
char v3; // bp
__int64 v4; // r12
int result; // eax

v3 = a3;
v4 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL);
result = printf("#%d: set_char(%d, '%c')\n", *(unsigned int *)(a1 + 832), a2, a3);
*(_BYTE *)(v4 + a2 + 840) = v3;
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned int a2, unsigned int a3)
{
char v3; // bp
__int64 v4; // r12
int result; // eax

v3 = a3;
v4 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 16LL);
result = printf("#%d: set_char(%d, '%c')\n", *(unsigned int *)(a1 + 832), a2, a3);
*(_BYTE *)(v4 + a2 + 840) = v3;
return result;
}

original_code:
static void test_set_char(struct RotorStack *stk, unsigned int char_pos, unsigned int c)
{
 struct TestEng *my = stk->eng->priv;
 printf("#%d: set_char(%d, '%c')\n", stk->id, char_pos, c);
 my->word[char_pos] = c;
}

variable_types: struct RotorStack { int nrotors; _Bool stopped; struct EngineThread { struct EngineInfo { char *eng_name; char *algo_name; void (*init)(struct EngineThread *); void (*release)(struct EngineThread *); void (*add_hash)(struct EngineThread *eng, void *); void (*run)(struct EngineThread *); void (*set_length)(struct RotorStack *stk, unsigned int len); void (*set_char)(struct RotorStack *stk, unsigned int char_pos, unsigned int c); int hash_len; char **sample_list; } *info; struct TopRotor { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; int nrotors; int angle; _Bool stopped; int wlen; int start_len; int end_len; int *step_list; int *wiring; } *top; void *priv; _Bool stopped; int active; unsigned int counter; unsigned int last_counter; unsigned long thread; struct EngineThread *next; } *eng; struct Rotor { int *wiring; int *step; int angle; int pos; } subzero; struct Rotor stack_[-1]; int id; } *

variable_types:
c
unsigned int

variable_types: struct TestEng { struct RotorStack { int nrotors; _Bool stopped; struct EngineThread { struct EngineInfo { char *eng_name; char *algo_name; void (*init)(struct EngineThread *); void (*release)(struct EngineThread *); void (*add_hash)(struct EngineThread *eng, void *); void (*run)(struct EngineThread *); void (*set_length)(struct RotorStack *stk, unsigned int len); void (*set_char)(struct RotorStack *stk, unsigned int char_pos, unsigned int c); int hash_len; char **sample_list; } *info; struct TopRotor { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; int nrotors; int angle; _Bool stopped; int wlen; int start_len; int end_len; int *step_list; int *wiring; } *top; void *priv; _Bool stopped; int active; unsigned int counter; unsigned int last_counter; unsigned long thread; struct EngineThread *next; } *eng; struct Rotor { int *wiring; int *step; int angle; int pos; } subzero; struct Rotor stack_[-1]; int id; } stack; char word[256]; } *

user_defined_types: struct TestEng { struct RotorStack stack; char word[256]; }


decompiled_code:
int __fastcall test_set_length(__int64 a1, unsigned int a2)
{
return printf("#%d: set_length(%d)\n", *(unsigned int *)(a1 + 832), a2);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, unsigned int a2)
{
return printf("#%d: set_length(%d)\n", *(unsigned int *)(a1 + 832), a2);
}

original_code:
static void test_set_length(struct RotorStack *stk, unsigned int len)
{
 printf("#%d: set_length(%d)\n", stk->id, len);
}

user_defined_types: struct Rotor { int *wiring; int *step; int angle; int pos; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


calc

decompiled_code:
char *__fastcall calc(char *src)
{
int v1; // edx
int v2; // edi
char *result; // rax
int v4; // ecx
char v5[256]; // [rsp+0h] [rbp-108h] BYREF
char v6; // [rsp+100h] [rbp-8h] BYREF

strcpy(v5, src);
v1 = blah;
v2 = 16;
do
{
result = v5;
do
{
v4 = *result++;
v1 += v4;
}
while ( result != &v6 );
--v2;
}
while ( v2 );
blah = v1;
return result;
}

canonical_decompiled_code:
char *__fastcall func3(char *src)
{
int v1; // edx
int v2; // edi
char *result; // rax
int v4; // ecx
char v5[256]; // [rsp+0h] [rbp-108h] BYREF
char v6; // [rsp+100h] [rbp-8h] BYREF

strcpy(v5, src);
v1 = blah;
v2 = 16;
do
{
result = v5;
do
{
v4 = *result++;
v1 += v4;
}
while ( result != &v6 );
--v2;
}
while ( v2 );
blah = v1;
return result;
}

original_code:
static void calc(const char *word)
{
 char buf[256];
 int i, j;
 strcpy(buf, word);
 for (j = 0; j < 16; j++)
 for (i = 0; i < 256; i++)
  blah += buf[i];
}

canonical_original_code:
static void calc(char *word)
{
 char buf[256];
 int i; int j;
 strcpy(buf, word);
 for (j = 0; j < 16; j++)
 for (i = 0; i < 256; i++)
  blah += buf[i];
}

variable_types:
word
char *

variable_types:
buf
char [256]

variable_types: i int

variable_types: j int


decompiled_code:
void __fastcall test_release(__int64 a1)
{
free(*(void **)(a1 + 16));
*(_QWORD *)(a1 + 16) = 0LL;
}

original_code:
static void test_release(struct EngineThread *eng)
{
 struct TestEng *my = eng->priv;
 free(my);
 eng->priv = ((void *)0);
}


decompiled_code:
__int64 __fastcall test_init(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // r12

v1 = zmalloc(1096LL);
*(_QWORD *)(a1 + 16) = v1;
v2 = v1;
puts("test_init");
++eng_id;
return stack_init(a1, v2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // r12

v1 = zmalloc(1096LL);
*(_QWORD *)(a1 + 16) = v1;
v2 = v1;
puts("test_init");
++eng_id;
return stack_init(a1, v2);
}

original_code:
static void test_init(struct EngineThread *eng)
{
 struct TestEng *my = zmalloc(sizeof(*my));
 eng->priv = my;
 printf("test_init\n");
 stack_init(eng, &my->stack, eng_id++);
}


decompiled_code:
int __fastcall cmp_item32(const void *a1, const void *a2)
{
return memcmp(a1, a2, 4uLL);
}

canonical_decompiled_code:
int __fastcall func2(const void *a1, const void *a2)
{
return memcmp(a1, a2, 4uLL);
}

original_code: static int cmp_item32(const void *a, const void *b) { return memcmp(a, b, 4); }

canonical_original_code: static int cmp_item32(void *a, void *b) { return memcmp(a, b, 4); }

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
__int64 __fastcall bitarray_set(unsigned __int64 *a1, unsigned __int64 a2)
{
__int64 v3; // r8
unsigned __int64 v4; // [rsp+0h] [rbp-10h] BYREF
__int64 v5; // [rsp+8h] [rbp-8h] BYREF

if ( *a1 < a2 )
return 0LL;
bitarray_offset_remainder(a2, &v4, &v5);
*(_QWORD *)(v3 + 8 * v4 + 16) |= 1 << v5;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int64 *a1, unsigned __int64 a2)
{
__int64 v3; // r8
unsigned __int64 v4; // [rsp+0h] [rbp-10h] BYREF
__int64 v5; // [rsp+8h] [rbp-8h] BYREF

if ( *a1 < a2 )
return 0LL;
func7(a2, &v4, &v5);
*(_QWORD *)(v3 + 8 * v4 + 16) |= 1 << v5;
return 1LL;
}

original_code:
int8_t
bitarray_set(bitarray_t *array, uint64_t number)
{
  if (number > array->max)
    return 0;
  uint64_t offset;
  uint64_t remainder;
  bitarray_offset_remainder(number, &offset, &remainder);
  array->chunk[offset] |= 1 << remainder;
  return 1;
}

canonical_original_code:
char bitarray_set(struct bitarray_s *array, unsigned long number)
{
  if (number > array->max)
    return 0;
  unsigned long offset;
  unsigned long remainder;
  bitarray_offset_remainder(number, &offset, &remainder);
  array->chunk[offset] |= 1 << remainder;
  return 1;
}

variable_types: struct bitarray_s { unsigned long max; unsigned long chunk_count; unsigned long chunk[]; } *

variable_types:
number
unsigned long

variable_types:
offset
unsigned long

variable_types:
remainder
unsigned long

return_type: char


decompiled_code:
bool __fastcall bitarray_contains(unsigned __int64 *a1, unsigned __int64 a2)
{
__int64 v3; // r8
unsigned __int64 v4; // [rsp+0h] [rbp-10h] BYREF
__int64 v5; // [rsp+8h] [rbp-8h] BYREF

if ( *a1 < a2 )
return 0;
bitarray_offset_remainder(a2, &v4, &v5);
return ((1 << v5) & *(_QWORD *)(v3 + 8 * v4 + 16)) == 1 << v5;
}

canonical_decompiled_code:
bool __fastcall func9(unsigned __int64 *a1, unsigned __int64 a2)
{
__int64 v3; // r8
unsigned __int64 v4; // [rsp+0h] [rbp-10h] BYREF
__int64 v5; // [rsp+8h] [rbp-8h] BYREF

if ( *a1 < a2 )
return 0;
func7(a2, &v4, &v5);
return ((1 << v5) & *(_QWORD *)(v3 + 8 * v4 + 16)) == 1 << v5;
}

original_code:
int8_t
bitarray_contains(bitarray_t *array, uint64_t number)
{
  if (number > array->max)
    return 0;
  uint64_t offset;
  uint64_t remainder, expected;
  bitarray_offset_remainder(number, &offset, &remainder);
  expected = 1 << remainder;
  return (array->chunk[offset] & expected) == expected;
}

canonical_original_code:
char bitarray_contains(struct bitarray_s *array, unsigned long number)
{
  if (number > array->max)
    return 0;
  unsigned long offset;
  unsigned long remainder; unsigned long expected;
  bitarray_offset_remainder(number, &offset, &remainder);
  expected = 1 << remainder;
  return (array->chunk[offset] & expected) == expected;
}

variable_types:
expected
unsigned long

user_defined_types: struct bitarray_s { unsigned long max; unsigned long chunk_count; unsigned long chunk[]; }


original_code:
void
bitarray_print_all(bitarray_t *array)
{
  uint64_t expected;
  for (uint64_t offset = 0; offset < array->chunk_count; offset++) {
    for (uint64_t remainder = 0; remainder < (sizeof(uint64_t) * 8); remainder++) {
      expected = 1 << remainder;
      if ((array->chunk[offset] & expected) == expected)
        printf("%ld\n", (sizeof(uint64_t) * 8) * offset + remainder);
    }
  }
}

canonical_original_code:
void bitarray_print_all(struct bitarray_s *array)
{
  unsigned long expected;
  for (unsigned long offset = 0; offset < array->chunk_count; offset++) {
    for (unsigned long remainder = 0; remainder < (sizeof(unsigned long) * 8); remainder++) {
      expected = 1 << remainder;
      if ((array->chunk[offset] & expected) == expected)
        printf("%ld\n", (sizeof(unsigned long) * 8) * offset + remainder);
    }
  }
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
int v5; // ebx

v3 = bitarray_new(10000000LL, argv, envp);
if ( v3 )
{
v4 = (void *)v3;
v5 = 10000000;
do
{
if ( (unsigned int)__isoc99_scanf() != 1 )
break;
bitarray_set(v4, 0LL);
--v5;
}
while ( v5 );
if ( *__errno_location() )
perror("scanf");
else
bitarray_print_all(v4);
free(v4);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
int v5; // ebx

v3 = bitarray_new(10000000LL, argv, envp);
if ( v3 )
{
v4 = (void *)v3;
v5 = 10000000;
do
{
if ( (unsigned int)__isoc99_scanf() != 1 )
break;
func8(v4, 0LL);
--v5;
}
while ( v5 );
if ( *__errno_location() )
perror("scanf");
else
func10(v4);
free(v4);
}
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
  bitarray_t *array = ((void *)0);
  if ((array = bitarray_new(10000000)) == ((void *)0))
    goto finished;
  uint32_t i, parsed_int;
  for(i = 0, parsed_int = 0; (i < 10000000) && (scanf("%u", &parsed_int) == 1); i++)
    bitarray_set(array, parsed_int);
  if ((*__errno_location ())) {
    perror("scanf");
    goto finished;
  }
  bitarray_print_all(array);
 finished:
  if (array != ((void *)0))
    free(array);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  struct bitarray_s *array = ((void *)0);
  if ((array = bitarray_new(10000000)) == ((void *)0))
    goto finished;
  unsigned int i; unsigned int parsed_int;
  for(i = 0, parsed_int = 0; (i < 10000000) && (scanf("%u", &parsed_int) == 1); i++)
    bitarray_set(array, parsed_int);
  if ((*__errno_location ())) {
    perror("scanf");
    goto finished;
  }
  bitarray_print_all(array);
 finished:
  if (array != ((void *)0))
    free(array);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
i
unsigned int


comparer

decompiled_code:
__int64 __fastcall comparer(const void *a1, const void *a2)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)a1 != *(_DWORD *)a2 )
result = *(_DWORD *)a1 < *(_DWORD *)a2 ? -1 : 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const void *a1, const void *a2)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)a1 != *(_DWORD *)a2 )
result = *(_DWORD *)a1 < *(_DWORD *)a2 ? -1 : 1;
return result;
}

original_code:
int
comparer(const void *v_a, const void *v_b)
{
  const uint32_t *a = v_a, *b = v_b;
  return *a == *b ? 0 : (*a < *b ? -1 : 1);
}

canonical_original_code:
int comparer(void *v_a, void *v_b)
{
  unsigned int *a = v_a; unsigned int *b = v_b;
  return *a == *b ? 0 : (*a < *b ? -1 : 1);
}

variable_types:
a
unsigned int *

variable_types:
b
unsigned int *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int *v3; // rax
unsigned int *v4; // r13
char *v5; // rbx
size_t i; // rbp
unsigned int v7; // er12
__int64 v8; // rbx
__int64 v9; // rsi

v3 = (unsigned int *)calloc(0x989680uLL, 4uLL);
if ( !v3 )
return 0;
v4 = v3;
v5 = (char *)v3;
for ( i = 0LL; i != 10000000; ++i )
{
v7 = i;
if ( (unsigned int)__isoc99_scanf() != 1 )
{
if ( !*__errno_location() )
{
qsort(v4, i, 4uLL, comparer);
if ( !(_DWORD)i )
goto LABEL_9;
goto LABEL_7;
}
LABEL_11:
perror("scanf");
goto LABEL_9;
}
v5 += 4;
}
if ( *__errno_location() )
goto LABEL_11;
qsort(v4, 0x989680uLL, 4uLL, comparer);
v7 = 10000000;
LABEL_7:
v8 = 0LL;
do
{
v9 = v4[v8++];
printf("%d\n", v9);
}
while ( v7 > (unsigned int)v8 );
LABEL_9:
free(v4);
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
  uint32_t *incoming = calloc(10000000, sizeof(uint32_t)), i;
  if (!incoming)
    goto finished;
  for(i = 0; (i < 10000000) && (scanf("%u", &incoming[i]) == 1); i++);
  if ((*__errno_location ())) {
    perror("scanf");
    goto finished;
  }
  qsort(incoming, i, sizeof(uint32_t), comparer);
  for(int32_t j = 0; j < i; j++)
    printf("%d\n", incoming[j]);
 finished:
  if (incoming)
    free(incoming);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  unsigned int *incoming = calloc(10000000, sizeof(uint32_t)); unsigned int i;
  if (!incoming)
    goto finished;
  for(i = 0; (i < 10000000) && (scanf("%u", &incoming[i]) == 1); i++);
  if ((*__errno_location ())) {
    perror("scanf");
    goto finished;
  }
  qsort(incoming, i, sizeof(unsigned int), comparer);
  for(int j = 0; j < i; j++)
    printf("%d\n", incoming[j]);
 finished:
  if (incoming)
    free(incoming);
  return 0;
}

variable_types:
incoming
unsigned int *


fade

repo: markshroyer/sparklemotion

decompiled_code:
double __fastcall fade(unsigned __int8 a1, double a2)
{
double v2; // xmm0_8
double result; // xmm0_8

*(_QWORD *)&v2 = *(_QWORD *)&a2 & LC1;
if ( v2 > 70.0 )
result = 0.0;
else
result = (double)a1 * (1.0 - v2 / 70.0);
return result;
}

canonical_decompiled_code:
double __fastcall func0(unsigned __int8 a1, double a2)
{
double v2; // xmm0_8
double result; // xmm0_8

*(_QWORD *)&v2 = *(_QWORD *)&a2 & LC1;
if ( v2 > 70.0 )
result = 0.0;
else
result = (double)a1 * (1.0 - v2 / 70.0);
return result;
}

original_code:
double fade(uint8_t pointval, double distance)
{
    const double max_dist = 7 * 10;
    if (fabs(distance) > max_dist)
        return 0;
    return ((double)pointval) * (1 - fabs(distance) / max_dist);
}

canonical_original_code:
double fade(unsigned char pointval, double distance)
{
    double max_dist = 7 * 10;
    if (fabs(distance) > max_dist)
        return 0;
    return ((double)pointval) * (1 - fabs(distance) / max_dist);
}

variable_types:
pointval
unsigned char

variable_types: distance double

return_type: double


decompiled_code:
double __fastcall point_led_distance(__int64 a1, int a2)
{
int v2; // eax
int v3; // esi
int v4; // eax
unsigned int v5; // ecx
unsigned int v6; // edx
int v7; // eax

v2 = 5 * a2;
v3 = *(_DWORD *)(a1 + 4);
v4 = 2 * v2;
v5 = abs32(v4 - v3);
v6 = abs32(v4 - (v3 - 600));
if ( v6 > v5 )
v6 = v5;
v7 = abs32(v4 - (v3 + 600));
if ( v7 > v6 )
v7 = v6;
return (double)v7;
}

canonical_decompiled_code:
double __fastcall func1(__int64 a1, int a2)
{
int v2; // eax
int v3; // esi
int v4; // eax
unsigned int v5; // ecx
unsigned int v6; // edx
int v7; // eax

v2 = 5 * a2;
v3 = *(_DWORD *)(a1 + 4);
v4 = 2 * v2;
v5 = abs32(v4 - v3);
v6 = abs32(v4 - (v3 - 600));
if ( v6 > v5 )
v6 = v5;
v7 = abs32(v4 - (v3 + 600));
if ( v7 > v6 )
v7 = v6;
return (double)v7;
}

original_code:
double point_led_distance(point_t *point, int led)
{
    int led_pos = led * 10;
    int point_pos = (int)(point->pos);
    unsigned int test;
    unsigned int result = abs(led_pos - point_pos);
    test = abs(led_pos - (point_pos - (60 * 10)));
    if (test < result)
        result = test;
    test = abs(led_pos - (point_pos + (60 * 10)));
    if (test < result)
        result = test;
    return (double)result;
}

canonical_original_code:
double point_led_distance(struct point *point, int led)
{
    int led_pos = led * 10;
    int point_pos = (int)(point->pos);
    unsigned int test;
    unsigned int result = abs(led_pos - point_pos);
    test = abs(led_pos - (point_pos - (60 * 10)));
    if (test < result)
        result = test;
    test = abs(led_pos - (point_pos + (60 * 10)));
    if (test < result)
        result = test;
    return (double)result;
}

variable_types: struct point { struct color { unsigned char r; unsigned char g; unsigned char b; } color; int pos; int speed; } *

variable_types: led int

variable_types:
test
unsigned int

variable_types:
result
unsigned int

user_defined_types: struct point { struct color color; int pos; int speed; }


original_code:
color_t led_color(point_t *points, int npoint, int led)
{
    int i;
    double distance;
    color_t result;
    double r = 0xff, g = 0xff, b = 0xff;
    for (i = 0; i < 4; i++) {
        distance = point_led_distance(points+i, led);
        r -= fade(0xff - points[i].color.r, distance);
        g -= fade(0xff - points[i].color.g, distance);
        b -= fade(0xff - points[i].color.b, distance);
    }
    result.r = (uint8_t)(fabs(r));
    result.g = (uint8_t)(fabs(g));
    result.b = (uint8_t)(fabs(b));
    return result;
}

canonical_original_code:
struct color led_color(struct point *points, int npoint, int led)
{
    int i;
    double distance;
    struct color result;
    double r = 0xff; double g = 0xff; double b = 0xff;
    for (i = 0; i < 4; i++) {
        distance = point_led_distance(points+i, led);
        r -= fade(0xff - points[i].color.r, distance);
        g -= fade(0xff - points[i].color.g, distance);
        b -= fade(0xff - points[i].color.b, distance);
    }
    result.r = (unsigned char)(fabs(r));
    result.g = (unsigned char)(fabs(g));
    result.b = (unsigned char)(fabs(b));
    return result;
}

variable_types: struct point { struct color color; int pos; int speed; } *

variable_types: npoint int

variable_types: i int

variable_types: struct color { unsigned char r; unsigned char g; unsigned char b; }

variable_types: r double

variable_types: g double

variable_types: b double


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__m128 v3; // xmm0
void *v4; // r13
char *v5; // rbp
char *v6; // rcx
int v7; // edx
int v8; // ebx
int v9; // edx
int v10; // eax
__int64 v11; // r11
struct timespec requested_time; // [rsp+10h] [rbp-38h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 10000000LL;
if ( (int)sparkle_init(argc, argv, envp) >= 0 )
{
sparkle_max_luminance = 80;
v4 = malloc(0xB4uLL);
v5 = (char *)calloc(4uLL, 0xCuLL);
*(_WORD *)v5 = 255;
v5[2] = 0;
*((_DWORD *)v5 + 2) = 1;
*((_WORD *)v5 + 6) = -256;
v5[14] = 0;
*((_WORD *)v5 + 12) = 255;
*((_QWORD *)v5 + 2) = 0x100000096LL;
*((_QWORD *)v5 + 5) = 0x1000001C2LL;
v5[26] = 0;
*(_QWORD *)(v5 + 28) = 0x10000012CLL;
*((_WORD *)v5 + 18) = -256;
v5[38] = 0;
while ( 1 )
{
v6 = v5 + 4;
do
{
v7 = *(_DWORD *)v6 + *((_DWORD *)v6 + 1);
v6 += 12;
*((_DWORD *)v6 - 3) = v7 % 600;
}
while ( v5 + 52 != v6 );
v8 = 0;
do
{
v9 = v8++;
v10 = led_color((__int64)v5, v3, 4LL, v9);
v11 += 3LL;
*(_BYTE *)(v11 - 2) = v10;
*(_BYTE *)(v11 - 3) = BYTE1(v10);
*(_BYTE *)(v11 - 1) = BYTE2(v10);
}
while ( v8 != 60 );
sparkle_write(v4, 180LL);
nanosleep(&requested_time, 0LL);
}
}
fwrite("Could not initialize sparkle\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__m128 v3; // xmm0
void *v4; // r13
char *v5; // rbp
char *v6; // rcx
int v7; // edx
int v8; // ebx
int v9; // edx
int v10; // eax
__int64 v11; // r11
struct timespec requested_time; // [rsp+10h] [rbp-38h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 10000000LL;
if ( (int)sparkle_init(argc, argv, envp) >= 0 )
{
sparkle_max_luminance = 80;
v4 = malloc(0xB4uLL);
v5 = (char *)calloc(4uLL, 0xCuLL);
*(_WORD *)v5 = 255;
v5[2] = 0;
*((_DWORD *)v5 + 2) = 1;
*((_WORD *)v5 + 6) = -256;
v5[14] = 0;
*((_WORD *)v5 + 12) = 255;
*((_QWORD *)v5 + 2) = 0x100000096LL;
*((_QWORD *)v5 + 5) = 0x1000001C2LL;
v5[26] = 0;
*(_QWORD *)(v5 + 28) = 0x10000012CLL;
*((_WORD *)v5 + 18) = -256;
v5[38] = 0;
while ( 1 )
{
v6 = v5 + 4;
do
{
v7 = *(_DWORD *)v6 + *((_DWORD *)v6 + 1);
v6 += 12;
*((_DWORD *)v6 - 3) = v7 % 600;
}
while ( v5 + 52 != v6 );
v8 = 0;
do
{
v9 = v8++;
v10 = func2((__int64)v5, v3, 4LL, v9);
v11 += 3LL;
*(_BYTE *)(v11 - 2) = v10;
*(_BYTE *)(v11 - 3) = BYTE1(v10);
*(_BYTE *)(v11 - 1) = BYTE2(v10);
}
while ( v8 != 60 );
sparkle_write(v4, 180LL);
nanosleep(&requested_time, 0LL);
}
}
fwrite("Could not initialize sparkle\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}

original_code:
int main(void)
{
    uint8_t *buf;
    point_t *points;
    color_t c;
    int i;
    struct timespec delay = { 0, 10000000 };
    if (sparkle_init() < 0) {
        fprintf(stderr, "Could not initialize sparkle\n");
        exit(1);
    }
    sparkle_max_luminance = 0x50;
    buf = malloc((3 * 60));
    points = calloc(4, sizeof(point_t));
    points[0].pos = 0 * 10;
    points[0].speed = 1;
    points[0].color.r = 0xff;
    points[0].color.g = 0x00;
    points[0].color.b = 0x00;
    points[1].pos = 15 * 10;
    points[1].speed = 1;
    points[1].color.r = 0x00;
    points[1].color.g = 0xff;
    points[1].color.b = 0x00;
    points[2].pos = 30 * 10;
    points[2].speed = 1;
    points[2].color.r = 0xff;
    points[2].color.g = 0x00;
    points[2].color.b = 0x00;
    points[3].pos = 45 * 10;
    points[3].speed = 1;
    points[3].color.r = 0x00;
    points[3].color.g = 0xff;
    points[3].color.b = 0x00;
    while ( 1 ) {
        for (i = 0; i < 4; i++) {
            points[i].pos = (points[i].pos + points[i].speed) % (60 * 10);
        }
        for (i = 0; i < 60; i++) {
            c = led_color(points, 4, i);
            buf[3*i+0] = c.g;
            buf[3*i+1] = c.r;
            buf[3*i+2] = c.b;
        }
        sparkle_write(buf, (3 * 60));
        nanosleep(&delay, ((void *)0));
    }
    sparkle_exit();
    return 0;
}

canonical_original_code:
int main()
{
    unsigned char *buf;
    struct point *points;
    struct color c;
    int i;
    struct timespec delay = { 0, 10000000 };
    if (sparkle_init() < 0) {
        fprintf(stderr, "Could not initialize sparkle\n");
        exit(1);
    }
    sparkle_max_luminance = 0x50;
    buf = malloc((3 * 60));
    points = calloc(4, sizeof(point_t));
    points[0].pos = 0 * 10;
    points[0].speed = 1;
    points[0].color.r = 0xff;
    points[0].color.g = 0x00;
    points[0].color.b = 0x00;
    points[1].pos = 15 * 10;
    points[1].speed = 1;
    points[1].color.r = 0x00;
    points[1].color.g = 0xff;
    points[1].color.b = 0x00;
    points[2].pos = 30 * 10;
    points[2].speed = 1;
    points[2].color.r = 0xff;
    points[2].color.g = 0x00;
    points[2].color.b = 0x00;
    points[3].pos = 45 * 10;
    points[3].speed = 1;
    points[3].color.r = 0x00;
    points[3].color.g = 0xff;
    points[3].color.b = 0x00;
    while ( 1 ) {
        for (i = 0; i < 4; i++) {
            points[i].pos = (points[i].pos + points[i].speed) % (60 * 10);
        }
        for (i = 0; i < 60; i++) {
            c = led_color(points, 4, i);
            buf[3*i+0] = c.g;
            buf[3*i+1] = c.r;
            buf[3*i+2] = c.b;
        }
        sparkle_write(buf, (3 * 60));
        nanosleep(&delay, ((void *)0));
    }
    sparkle_exit();
    return 0;
}

variable_types:
buf
unsigned char *

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

user_defined_types: struct color { unsigned char r; unsigned char g; unsigned char b; }


repo: markusfisch/libhttp

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r13
const char **v4; // r12
const char *v5; // rdi
int v6; // ebp
void *v8[527]; // [rsp+0h] [rbp-1078h] BYREF

if ( argc != 1 )
{
v3 = &argv[argc - 1];
v4 = argv;
while ( 1 )
{
while ( 1 )
{
v5 = v4[1];
++v4;
v6 = http_request(v5, argv, envp);
if ( v6 > 0 )
break;
perror("http_request");
LABEL_4:
if ( v4 == v3 )
return 0;
}
memset(v8, 0, 0x1048uLL);
while ( 1 )
{
argv = (const char **)v8;
if ( (int)http_response((unsigned int)v6, v8) <= 0 )
break;
if ( v8[1] )
write(1, v8[1], SLODWORD(v8[2]));
}
close(v6);
envp = (const char **)LODWORD(v8[0]);
if ( LODWORD(v8[0]) == 200 )
goto LABEL_4;
argv = (const char **)"error: returned HTTP code %d\n";
fprintf(stderr, "error: returned HTTP code %d\n", LODWORD(v8[0]));
if ( v4 == v3 )
return 0;
}
}
return 0;
}

original_code:
int main(int argc, char **argv) {
 while (--argc) {
  int sd;
  struct http_message msg;
  if ((sd = http_request(*++argv)) < 1) {
   perror("http_request");
   continue;
  }
  memset(&msg, 0, sizeof(msg));
  while (http_response(sd, &msg) > 0)
   if (msg.content) {
    write(1, msg.content, msg.length);
   }
  close(sd);
  if (msg.header.code != 200) {
   fprintf(
    stderr,
    "error: returned HTTP code %d\n",
    msg.header.code);
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: sd int

variable_types: struct http_message { struct { int code; int length; } header; char *content; int length; struct { int in_content; int chunk; char buf[4096]; int size; char *offset; char *last; int free; int left; int total; } state; }


decompiled_code:
const char *__fastcall http_parse_content(__int64 a1, const char *a2, const char *a3)
{
int v4; // edx
const char *v5; // rbx
__int64 v6; // rax
char *v7; // r13
const char *result; // rax
int v9; // edx
const char *v10; // [rsp+8h] [rbp-30h]

v4 = (_DWORD)a3 - (_DWORD)a2;
v5 = a2;
v6 = *(int *)(a1 + 28);
if ( (int)v6 < 0 )
goto LABEL_9;
if ( (int)v6 >= v4 )
{
*(_DWORD *)(a1 + 28) = v6 - v4;
LABEL_9:
*(_QWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 16) = v4;
http_cut_trailing_crlf(a1);
return a3;
}
if ( (_DWORD)v6 )
{
if ( (_DWORD)v6 != 1 )
{
*(_DWORD *)(a1 + 16) = v6;
*(_QWORD *)(a1 + 8) = a2;
goto LABEL_7;
}
*(_DWORD *)(a1 + 16) = 0;
result = a2 + 1;
*(_DWORD *)(a1 + 28) = 0;
}
else
{
v7 = strchr(a2, 10);
result = a2;
if ( v7 )
{
*v7 = 0;
v5 = v7 + 1;
_isoc99_sscanf(a2, &unk_8A1, a1 + 28);
LODWORD(v6) = *(_DWORD *)(a1 + 28);
*(_QWORD *)(a1 + 8) = v7 + 1;
v9 = (_DWORD)a3 - ((_DWORD)v7 + 1);
LODWORD(v6) = v6 + 2;
*(_DWORD *)(a1 + 28) = v6;
if ( (int)v6 < v9 )
{
*(_DWORD *)(a1 + 16) = v6;
v6 = (int)v6;
LABEL_7:
*(_DWORD *)(a1 + 28) = 0;
v10 = &v5[v6];
http_cut_trailing_crlf(a1);
return v10;
}
*(_DWORD *)(a1 + 16) = v9;
*(_DWORD *)(a1 + 28) = v6 - v9;
http_cut_trailing_crlf(a1);
result = a3;
}
}
return result;
}

original_code:
static char *http_parse_content(
  struct http_message *msg,
  char *bod,
  char *eod) {
 int len = eod - bod;
 if (msg->state.chunk > -1) {
  if (msg->state.chunk < len) {
   char *eoc;
   if (msg->state.chunk > 0) {
    if (msg->state.chunk == 1) {
     ++bod;
     msg->length = 0;
     msg->state.chunk = 0;
    } else {
     msg->length = msg->state.chunk;
     msg->content = bod;
     bod += msg->state.chunk;
     msg->state.chunk = 0;
     http_cut_trailing_crlf(msg);
    }
    return bod;
   }
   if (!(eoc = strchr(bod, '\n'))) {
    return bod;
   }
   *eoc = 0;
   sscanf(bod, "%x", &msg->state.chunk);
   msg->state.chunk += 2;
   msg->content = ++eoc;
   len = eod - msg->content;
   if (msg->state.chunk < len) {
    msg->length = msg->state.chunk;
    bod = msg->content + msg->state.chunk;
    msg->state.chunk = 0;
    http_cut_trailing_crlf(msg);
    return bod;
   } else {
    msg->length = len;
    msg->state.chunk -= len;
    http_cut_trailing_crlf(msg);
    return eod;
   }
  } else {
   msg->state.chunk -= len;
  }
 }
 msg->content = bod;
 msg->length = len;
 http_cut_trailing_crlf(msg);
 return eod;
}

canonical_original_code:
static char *http_parse_content(struct http_message *msg, char *bod, char *eod) {
 int len = eod - bod;
 if (msg->state.chunk > -1) {
  if (msg->state.chunk < len) {
   char *eoc;
   if (msg->state.chunk > 0) {
    if (msg->state.chunk == 1) {
     ++bod;
     msg->length = 0;
     msg->state.chunk = 0;
    } else {
     msg->length = msg->state.chunk;
     msg->content = bod;
     bod += msg->state.chunk;
     msg->state.chunk = 0;
     http_cut_trailing_crlf(msg);
    }
    return bod;
   }
   if (!(eoc = strchr(bod, '\n'))) {
    return bod;
   }
   *eoc = 0;
   sscanf(bod, "%x", &msg->state.chunk);
   msg->state.chunk += 2;
   msg->content = ++eoc;
   len = eod - msg->content;
   if (msg->state.chunk < len) {
    msg->length = msg->state.chunk;
    bod = msg->content + msg->state.chunk;
    msg->state.chunk = 0;
    http_cut_trailing_crlf(msg);
    return bod;
   } else {
    msg->length = len;
    msg->state.chunk -= len;
    http_cut_trailing_crlf(msg);
    return eod;
   }
  } else {
   msg->state.chunk -= len;
  }
 }
 msg->content = bod;
 msg->length = len;
 http_cut_trailing_crlf(msg);
 return eod;
}

variable_types: struct http_message { struct { int code; int length; } header; char *content; int length; struct { int in_content; int chunk; char buf[4096]; int size; char *offset; char *last; int free; int left; int total; } state; } *

variable_types:
bod
char *

variable_types:
eod
char *

variable_types: len int

variable_types:
eoc
char *

return_type: char *