decompiled_code:
void __fastcall dump_dentry(const void **a1, const char *a2)
{
if ( a1 )
{
printf(
"%s%p:%p:%p:%p %s ref_count=%d\n",
a2,
a1,
*a1,
a1[1],
a1[3],
(const char *)*a1 + 60,
*((unsigned int *)a1 + 20));
dump_ctree((__int64 *)a1 + 11, a2, (void (__fastcall *)(__int64, const char *))dump_dentries);
}
}

canonical_decompiled_code:
void __fastcall func5(const void **a1, const char *a2)
{
if ( a1 )
{
printf(
"%s%p:%p:%p:%p %s ref_count=%d\n",
a2,
a1,
*a1,
a1[1],
a1[3],
(const char *)*a1 + 60,
*((unsigned int *)a1 + 20));
func4((__int64 *)a1 + 11, a2, (void (__fastcall *)(__int64, const char *))dump_dentries);
}
}

original_code:
void dump_dentry(struct dentry *dentry, const char *indent)
{
 if (!dentry)
  return;
 printf("%s%p:%p:%p:%p %s ref_count=%d\n", indent, dentry, dentry->ddent,
   dentry->ddent_cnode, dentry->parent,
   dentry->ddent->name, dentry->ref_count);
 dump_ctree(&dentry->chunk_tree, indent, dump_dentries);
}

canonical_original_code:
void dump_dentry(struct dentry *dentry, char *indent)
{
 if (!dentry)
  return;
 printf("%s%p:%p:%p:%p %s ref_count=%d\n", indent, dentry, dentry->ddent,
   dentry->ddent_cnode, dentry->parent,
   dentry->ddent->name, dentry->ref_count);
 dump_ctree(&dentry->chunk_tree, indent, dump_dentries);
}

variable_types: struct dentry { struct disk_dentry { unsigned char digest[20]; unsigned char secret_digest[20]; struct le16_t { unsigned short v; } mode; unsigned char flags; unsigned char mtime_csec; struct le64_t { unsigned long v; } size; struct le32_t { unsigned int v; } ctime; struct le32_t mtime; unsigned char name[-1]; } *ddent; struct chunk_node { unsigned char chunk_data[-1]; unsigned char *chunk_digest; struct chunk_node *parent; struct chunk_tree { struct chunk_node *root; unsigned int nr_leafs; unsigned int height; struct chunk_tree_operations { void (*free_private)(void *); int (*read_chunk)(unsigned char *chunk, unsigned char *digest); int (*write_chunk)(unsigned char *chunk, unsigned char *digest); } *ops; struct list_head { struct list_head *prev; } dirty_list; } *ctree; struct list_head dirty_entry; unsigned int ref_count; void *_private; } *ddent_cnode; struct mutex { 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; } mutex; unsigned long owner; } *ddent_mutex; struct dentry *parent; struct mutex mutex; unsigned int ref_count; struct chunk_tree chunk_tree; unsigned char *secret_chunk; unsigned int dirty; unsigned long size; struct timeval { long tv_sec; long tv_usec; } mtime; unsigned int mode; } *


repo: dayne/zunkfs

decompiled_code:
void __fastcall dump_dentries(const void ***a1, const char *a2)
{
const void ***v2; // r12
const void ***v3; // rbx
const void **v4; // rdi

v2 = a1 + 256;
v3 = a1;
do
{
v4 = *v3++;
dump_dentry(v4, a2);
}
while ( v3 != v2 );
}

canonical_decompiled_code:
void __fastcall func6(const void ***a1, const char *a2)
{
const void ***v2; // r12
const void ***v3; // rbx
const void **v4; // rdi

v2 = a1 + 256;
v3 = a1;
do
{
v4 = *v3++;
func5(v4, a2);
}
while ( v3 != v2 );
}

original_code:
void dump_dentries(void **list, const char *indent)
{
 int i;
 for (i = 0; i < ((1UL << 16) / sizeof(struct disk_dentry)); i ++)
  dump_dentry(list[i], indent);
}

canonical_original_code:
void dump_dentries(void **list, char *indent)
{
 int i;
 for (i = 0; i < ((1UL << 16) / sizeof(struct disk_dentry)); i ++)
  dump_dentry(list[i], indent);
}

variable_types:
list
void **

variable_types:
indent
char *

variable_types: i int

return_type: void

user_defined_types: struct disk_dentry { unsigned char digest[20]; unsigned char secret_digest[20]; struct le16_t mode; unsigned char flags; unsigned char mtime_csec; struct le64_t size; struct le32_t ctime; struct le32_t mtime; unsigned char name[-1]; }


decompiled_code:
void __fastcall dump_dentry_2(const void **a1, const char *a2)
{
const char *v2; // rdx
int v3; // eax
int v4; // eax
char *v5; // rax

if ( a1 )
{
v2 = "dir";
v3 = (_DWORD)a1[21] & 0xF000;
if ( v3 != 0x4000 )
{
v2 = "reg";
if ( v3 != 0x8000 )
v2 = "???";
}
printf(
"%s%p:%p:%p:%p %s ref_count=%u size=%ld type=%s\n",
a2,
a1,
*a1,
a1[1],
a1[3],
(const char *)*a1 + 60,
*((unsigned int *)a1 + 20),
a1[18],
v2);
if ( ((_DWORD)a1[21] & 0xF000) == 0x4000 )
{
v4 = scan_dir(a1, dump_child, a2 - 1);
if ( v4 )
{
v5 = strerror(-v4);
fprintf(stderr, "PANIC: scan_dir(%p): %s\n", a1, v5);
dump_ctree((__int64 *)a1 + 11, a2, (void (__fastcall *)(__int64, const char *))dump_dentries);
fflush(stdout);
abort();
}
}
}
}

canonical_decompiled_code:
void __fastcall func7(const void **a1, const char *a2)
{
const char *v2; // rdx
int v3; // eax
int v4; // eax
char *v5; // rax

if ( a1 )
{
v2 = "dir";
v3 = (_DWORD)a1[21] & 0xF000;
if ( v3 != 0x4000 )
{
v2 = "reg";
if ( v3 != 0x8000 )
v2 = "???";
}
printf(
"%s%p:%p:%p:%p %s ref_count=%u size=%ld type=%s\n",
a2,
a1,
*a1,
a1[1],
a1[3],
(const char *)*a1 + 60,
*((unsigned int *)a1 + 20),
a1[18],
v2);
if ( ((_DWORD)a1[21] & 0xF000) == 0x4000 )
{
v4 = scan_dir(a1, dump_child, a2 - 1);
if ( v4 )
{
v5 = strerror(-v4);
fprintf(stderr, "PANIC: scan_dir(%p): %s\n", a1, v5);
func4((__int64 *)a1 + 11, a2, (void (__fastcall *)(__int64, const char *))dump_dentries);
fflush(stdout);
abort();
}
}
}
}

original_code:
void dump_dentry_2(struct dentry *dentry, const char *indent)
{
 int err;
 if (!dentry)
  return;
 printf("%s%p:%p:%p:%p %s ref_count=%u size=%ld type=%s\n",
   indent, dentry, dentry->ddent,
   dentry->ddent_cnode, dentry->parent,
   dentry->ddent->name, dentry->ref_count,
   (long)dentry->size,
   ((((dentry->mode)) & 0170000) == (0040000)) ? "dir" :
   ((((dentry->mode)) & 0170000) == (0100000)) ? "reg" :
   "???");
 if (!((((dentry->mode)) & 0170000) == (0040000)))
  return;
 err = scan_dir(dentry, dump_child, (void *)(indent - 1));
 if (err) {
  fprintf(stderr, "PANIC: scan_dir(%p): %s\n",
    dentry, strerror(-err));
  dump_ctree(&dentry->chunk_tree, indent, dump_dentries);
  fflush(stdout);
  abort();
 }
}

canonical_original_code:
void dump_dentry_2(struct dentry *dentry, char *indent)
{
 int err;
 if (!dentry)
  return;
 printf("%s%p:%p:%p:%p %s ref_count=%u size=%ld type=%s\n",
   indent, dentry, dentry->ddent,
   dentry->ddent_cnode, dentry->parent,
   dentry->ddent->name, dentry->ref_count,
   (long)dentry->size,
   ((((dentry->mode)) & 0170000) == (0040000)) ? "dir" :
   ((((dentry->mode)) & 0170000) == (0100000)) ? "reg" :
   "???");
 if (!((((dentry->mode)) & 0170000) == (0040000)))
  return;
 err = scan_dir(dentry, dump_child, (void *)(indent - 1));
 if (err) {
  fprintf(stderr, "PANIC: scan_dir(%p): %s\n",
    dentry, strerror(-err));
  dump_ctree(&dentry->chunk_tree, indent, dump_dentries);
  fflush(stdout);
  abort();
 }
}

variable_types: err int

user_defined_types: struct dentry { struct disk_dentry *ddent; struct chunk_node *ddent_cnode; struct mutex *ddent_mutex; struct dentry *parent; struct mutex mutex; unsigned int ref_count; struct chunk_tree chunk_tree; unsigned char *secret_chunk; unsigned int dirty; unsigned long size; struct timeval mtime; unsigned int mode; }

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

user_defined_types: struct chunk_node { unsigned char chunk_data[-1]; unsigned char *chunk_digest; struct chunk_node *parent; struct chunk_tree *ctree; struct list_head dirty_entry; unsigned int ref_count; void *_private; }

user_defined_types: struct mutex { union pthread_mutex_t mutex; unsigned long owner; }

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

user_defined_types: struct chunk_tree { struct chunk_node *root; unsigned int nr_leafs; unsigned int height; struct chunk_tree_operations *ops; struct list_head dirty_list; }

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 list_head { struct list_head *prev; }

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

user_defined_types: struct chunk_tree_operations { void (*free_private)(void *); int (*read_chunk)(unsigned char *chunk, unsigned char *digest); int (*write_chunk)(unsigned char *chunk, unsigned char *digest); }


decompiled_code:
__int64 __fastcall dump_child(const void **a1, const char *a2)
{
dump_dentry_2(a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(const void **a1, const char *a2)
{
func7(a1, a2);
return 0LL;
}

original_code:
static int dump_child(struct dentry *child, void *data)
{
 const char *indent = data;
 dump_dentry_2(child, indent);
 return 0;
}

canonical_original_code:
static int dump_child(struct dentry *child, void *data)
{
 char *indent = data;
 dump_dentry_2(child, indent);
 return 0;
}

variable_types:
data
void *

return_type: int


decompiled_code:
bool __fastcall local_write_chunk(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // er8
bool result; // al
int v7; // er14
int v8; // er12
__int64 v9; // r15
int v10; // eax
int *v11; // rax
int v12; // er11
int *v13; // rax
int v14; // er13
int *v15; // r12
char *v16; // rax
int *v17; // rax
int v18; // er12
int *v19; // rbx
char *v20; // rax
__int64 v21; // [rsp+0h] [rbp-90h] BYREF
int v22; // [rsp+44h] [rbp-4Ch]
int *v23; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

v4 = (const char *)_digest_string(a2, &v21);
v5 = asprintf(&ptr, "%s/%s", a3, v4);
result = 0;
if ( v5 >= 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v13 = _errno_location();
v14 = *v13;
v15 = v13;
_zprintf(2LL, "local_write_chunk", 69LL, "path=%s\n", ptr);
*v15 = v14;
}
v7 = open64(ptr, 65, 384LL);
if ( v7 < 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v17 = _errno_location();
v18 = *v17;
v19 = v17;
v20 = strerror(*v17);
_zprintf(1LL, "local_write_chunk", 73LL, "%s: %s\n", ptr, v20);
*v19 = v18;
}
free(ptr);
result = 0;
}
else
{
v8 = 0;
v9 = 0LL;
free(ptr);
do
{
while ( 1 )
{
v10 = write(v7, (const void *)(a1 + v9), 0x10000 - v9);
if ( v10 < 0 )
break;
v8 += v10;
if ( v8 > 0xFFFF )
return close(v7) == 0;
v9 = v8;
}
v11 = _errno_location();
v12 = *v11;
v23 = v11;
}
while ( v12 == 4 );
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v22 = v12;
v16 = strerror(v12);
_zprintf(1LL, "local_write_chunk", 85LL, "%s: %s\n", ptr, v16);
*v23 = v22;
close(v7);
result = 0;
}
else
{
close(v7);
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
bool __fastcall func0(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // er8
bool result; // al
int v7; // er14
int v8; // er12
__int64 v9; // r15
int v10; // eax
int *v11; // rax
int v12; // er11
int *v13; // rax
int v14; // er13
int *v15; // r12
char *v16; // rax
int *v17; // rax
int v18; // er12
int *v19; // rbx
char *v20; // rax
__int64 v21; // [rsp+0h] [rbp-90h] BYREF
int v22; // [rsp+44h] [rbp-4Ch]
int *v23; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

v4 = (const char *)_digest_string(a2, &v21);
v5 = asprintf(&ptr, "%s/%s", a3, v4);
result = 0;
if ( v5 >= 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v13 = _errno_location();
v14 = *v13;
v15 = v13;
_zprintf(2LL, "local_write_chunk", 69LL, "path=%s\n", ptr);
*v15 = v14;
}
v7 = open64(ptr, 65, 384LL);
if ( v7 < 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v17 = _errno_location();
v18 = *v17;
v19 = v17;
v20 = strerror(*v17);
_zprintf(1LL, "local_write_chunk", 73LL, "%s: %s\n", ptr, v20);
*v19 = v18;
}
free(ptr);
result = 0;
}
else
{
v8 = 0;
v9 = 0LL;
free(ptr);
do
{
while ( 1 )
{
v10 = write(v7, (const void *)(a1 + v9), 0x10000 - v9);
if ( v10 < 0 )
break;
v8 += v10;
if ( v8 > 0xFFFF )
return close(v7) == 0;
v9 = v8;
}
v11 = _errno_location();
v12 = *v11;
v23 = v11;
}
while ( v12 == 4 );
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v22 = v12;
v16 = strerror(v12);
_zprintf(1LL, "local_write_chunk", 85LL, "%s: %s\n", ptr, v16);
*v23 = v22;
close(v7);
result = 0;
}
else
{
close(v7);
result = 0;
}
}
}
return result;
}

original_code:
static _Bool local_write_chunk(const unsigned char *chunk,
  const unsigned char *digest, void *db_info)
{
 char *chunk_dir = db_info;
 int fd, len, n;
 char *path;
 int err;
 err = asprintf(&path, "%s/%s", chunk_dir, __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 if (err < 0)
  return 0;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 69, "path=%s\n", path); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 fd = open(path, 01|0100, 0400|0200);
 if (fd < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 73, "%s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  free(path);
  return 0;
 }
 free(path);
 len = 0;
 while (len < (1UL << 16)) {
  n = write(fd, chunk + len, (1UL << 16) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 85, "%s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
   close(fd);
   return 0;
  }
  len += n;
 }
 err = close(fd);
 if (err)
  return 0;
 return 1;
}

canonical_original_code:
static _Bool local_write_chunk(unsigned char *chunk, unsigned char *digest, void *db_info)
{
 char *chunk_dir = db_info;
 int fd; int len; int n;
 char *path;
 int err;
 err = asprintf(&path, "%s/%s", chunk_dir, __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 if (err < 0)
  return 0;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 69, "path=%s\n", path); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 fd = open(path, 01|0100, 0400|0200);
 if (fd < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 73, "%s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  free(path);
  return 0;
 }
 free(path);
 len = 0;
 while (len < (1UL << 16)) {
  n = write(fd, chunk + len, (1UL << 16) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 85, "%s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
   close(fd);
   return 0;
  }
  len += n;
 }
 err = close(fd);
 if (err)
  return 0;
 return 1;
}

variable_types:
chunk
unsigned char *

variable_types:
digest
unsigned char *

variable_types: fd int

variable_types: len int

variable_types: n int

variable_types:
path
char *


decompiled_code:
__int64 __fastcall local_read_chunk(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // er8
__int64 result; // rax
int v7; // er14
int v8; // er12
__int64 v9; // r15
int v10; // eax
int *v11; // rax
int v12; // er11
int *v13; // rax
int v14; // er13
int *v15; // r12
char *v16; // rax
int *v17; // rax
int v18; // er12
int *v19; // rbx
char *v20; // rax
__int64 v21; // [rsp+0h] [rbp-90h] BYREF
int v22; // [rsp+44h] [rbp-4Ch]
int *v23; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

v4 = (const char *)_digest_string(a2, &v21);
v5 = asprintf(&ptr, "%s/%s", a3, v4);
result = 0LL;
if ( v5 >= 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v13 = _errno_location();
v14 = *v13;
v15 = v13;
_zprintf(2LL, "local_read_chunk", 30LL, "path=%s\n", ptr);
*v15 = v14;
}
v7 = open64(ptr, 0);
if ( v7 < 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v17 = _errno_location();
v18 = *v17;
v19 = v17;
v20 = strerror(*v17);
_zprintf(1LL, "local_read_chunk", 34LL, "%s: %s\n", ptr, v20);
*v19 = v18;
}
free(ptr);
result = 0LL;
}
else
{
v8 = 0;
v9 = 0LL;
free(ptr);
do
{
while ( 1 )
{
v10 = read(v7, (void *)(a1 + v9), 0x10000 - v9);
if ( v10 < 0 )
break;
v8 += v10;
if ( v8 > 0xFFFF )
{
close(v7);
return 1LL;
}
v9 = v8;
}
v11 = _errno_location();
v12 = *v11;
v23 = v11;
}
while ( v12 == 4 );
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v22 = v12;
v16 = strerror(v12);
_zprintf(1LL, "local_read_chunk", 46LL, "read %s: %s\n", ptr, v16);
*v23 = v22;
close(v7);
result = 0LL;
}
else
{
close(v7);
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // er8
__int64 result; // rax
int v7; // er14
int v8; // er12
__int64 v9; // r15
int v10; // eax
int *v11; // rax
int v12; // er11
int *v13; // rax
int v14; // er13
int *v15; // r12
char *v16; // rax
int *v17; // rax
int v18; // er12
int *v19; // rbx
char *v20; // rax
__int64 v21; // [rsp+0h] [rbp-90h] BYREF
int v22; // [rsp+44h] [rbp-4Ch]
int *v23; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

v4 = (const char *)_digest_string(a2, &v21);
v5 = asprintf(&ptr, "%s/%s", a3, v4);
result = 0LL;
if ( v5 >= 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v13 = _errno_location();
v14 = *v13;
v15 = v13;
_zprintf(2LL, "local_read_chunk", 30LL, "path=%s\n", ptr);
*v15 = v14;
}
v7 = open64(ptr, 0);
if ( v7 < 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v17 = _errno_location();
v18 = *v17;
v19 = v17;
v20 = strerror(*v17);
_zprintf(1LL, "local_read_chunk", 34LL, "%s: %s\n", ptr, v20);
*v19 = v18;
}
free(ptr);
result = 0LL;
}
else
{
v8 = 0;
v9 = 0LL;
free(ptr);
do
{
while ( 1 )
{
v10 = read(v7, (void *)(a1 + v9), 0x10000 - v9);
if ( v10 < 0 )
break;
v8 += v10;
if ( v8 > 0xFFFF )
{
close(v7);
return 1LL;
}
v9 = v8;
}
v11 = _errno_location();
v12 = *v11;
v23 = v11;
}
while ( v12 == 4 );
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v22 = v12;
v16 = strerror(v12);
_zprintf(1LL, "local_read_chunk", 46LL, "read %s: %s\n", ptr, v16);
*v23 = v22;
close(v7);
result = 0LL;
}
else
{
close(v7);
result = 0LL;
}
}
}
return result;
}

original_code:
static _Bool local_read_chunk(unsigned char *chunk, const unsigned char *digest,
  void *db_info)
{
 char *chunk_dir = db_info;
 int fd, len, n;
 char *path;
 int err;
 err = asprintf(&path, "%s/%s", chunk_dir, __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 if (err < 0)
  return 0;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 30, "path=%s\n", path); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 fd = open(path, 00);
 if (fd < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 34, "%s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  free(path);
  return 0;
 }
 free(path);
 len = 0;
 while (len < (1UL << 16)) {
  n = read(fd, chunk + len, (1UL << 16) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 46, "read %s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
   close(fd);
   return 0;
  }
  len += n;
 }
 close(fd);
 return 1;
}

canonical_original_code:
static _Bool local_read_chunk(unsigned char *chunk, unsigned char *digest, void *db_info)
{
 char *chunk_dir = db_info;
 int fd; int len; int n;
 char *path;
 int err;
 err = asprintf(&path, "%s/%s", chunk_dir, __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 if (err < 0)
  return 0;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 30, "path=%s\n", path); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 fd = open(path, 00);
 if (fd < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 34, "%s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  free(path);
  return 0;
 }
 free(path);
 len = 0;
 while (len < (1UL << 16)) {
  n = read(fd, chunk + len, (1UL << 16) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 46, "read %s: %s\n", path, strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
   close(fd);
   return 0;
  }
  len += n;
 }
 close(fd);
 return 1;
}


decompiled_code:
__int64 __fastcall local_chunkdb_ctor(char *name, __int64 a2)
{
__int64 result; // rax
int *v3; // rax
int v4; // er13
int *v5; // r12
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
struct stat64 v10; // [rsp+0h] [rbp-B8h] BYREF

if ( !zunkfs_log_fp || zunkfs_log_level <= 1 )
{
if ( stat64(name, &v10) != -1 )
goto LABEL_4;
LABEL_9:
v6 = _errno_location();
v7 = strerror(*v6);
return sprintf_new("Can't stat %s: %s.", name, v7);
}
v3 = _errno_location();
v4 = *v3;
v5 = v3;
_zprintf(2LL, "local_chunkdb_ctor", 103LL, "mode=0x%x spec=%s\n", *(unsigned int *)(a2 + 8), name);
*v5 = v4;
if ( stat64(name, &v10) == -1 )
goto LABEL_9;
LABEL_4:
if ( (v10.st_mode & 0xF000) != 0x4000 )
return sprintf_new("%s is not a directory.", name);
if ( access(name, (2 * (unsigned __int8)*(_DWORD *)(a2 + 8)) & 2 | 4) )
{
v8 = _errno_location();
v9 = strerror(*v8);
result = sprintf_new("%s.", v9);
}
else
{
*(_QWORD *)(a2 + 16) = name;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *name, __int64 a2)
{
__int64 result; // rax
int *v3; // rax
int v4; // er13
int *v5; // r12
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
struct stat64 v10; // [rsp+0h] [rbp-B8h] BYREF

if ( !zunkfs_log_fp || zunkfs_log_level <= 1 )
{
if ( stat64(name, &v10) != -1 )
goto LABEL_4;
LABEL_9:
v6 = _errno_location();
v7 = strerror(*v6);
return sprintf_new("Can't stat %s: %s.", name, v7);
}
v3 = _errno_location();
v4 = *v3;
v5 = v3;
_zprintf(2LL, "local_chunkdb_ctor", 103LL, "mode=0x%x spec=%s\n", *(unsigned int *)(a2 + 8), name);
*v5 = v4;
if ( stat64(name, &v10) == -1 )
goto LABEL_9;
LABEL_4:
if ( (v10.st_mode & 0xF000) != 0x4000 )
return sprintf_new("%s is not a directory.", name);
if ( access(name, (2 * (unsigned __int8)*(_DWORD *)(a2 + 8)) & 2 | 4) )
{
v8 = _errno_location();
v9 = strerror(*v8);
result = sprintf_new("%s.", v9);
}
else
{
*(_QWORD *)(a2 + 16) = name;
result = 0LL;
}
return result;
}

original_code:
static char *local_chunkdb_ctor(const char *spec, struct chunk_db *cdb)
{
 struct stat stbuf;
 int err;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 103, "mode=0x%x spec=%s\n", cdb->mode, spec); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 err = stat(spec, &stbuf);
 if (err == -1)
  return sprintf_new("Can't stat %s: %s.", spec, strerror((*__errno_location ())));
 if (!((((stbuf.st_mode)) & 0170000) == (0040000)))
  return sprintf_new("%s is not a directory.", spec);
 if (access(spec, 4 | ((cdb->mode & 1) ? 2 : 0)))
  return sprintf_new("%s.", strerror((*__errno_location ())));
 cdb->db_info = (void *)spec;
 return ((void *)0);
}

canonical_original_code:
static char *local_chunkdb_ctor(char *spec, struct chunk_db *cdb)
{
 struct stat stbuf;
 int err;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 103, "mode=0x%x spec=%s\n", cdb->mode, spec); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 err = stat(spec, &stbuf);
 if (err == -1)
  return sprintf_new("Can't stat %s: %s.", spec, strerror((*__errno_location ())));
 if (!((((stbuf.st_mode)) & 0170000) == (0040000)))
  return sprintf_new("%s is not a directory.", spec);
 if (access(spec, 4 | ((cdb->mode & 1) ? 2 : 0)))
  return sprintf_new("%s.", strerror((*__errno_location ())));
 cdb->db_info = (void *)spec;
 return ((void *)0);
}

variable_types:
spec
char *

variable_types: struct chunk_db { struct chunk_db_type { char *spec_prefix; unsigned int info_size; struct list_head { struct list_head *prev; } type_entry; char *(*ctor)(char *spec, struct chunk_db *chunk_db); _Bool (*read_chunk)(unsigned char *chunk, unsigned char *digest, void *db_info); _Bool (*write_chunk)(unsigned char *chunk, unsigned char *digest, void *db_info); char *help; } *type; int mode; void *db_info; struct list_head db_entry; } *

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]; }

return_type: char *

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]; }

user_defined_types: struct chunk_db { struct chunk_db_type *type; int mode; void *db_info; struct list_head db_entry; }

user_defined_types: struct chunk_db_type { char *spec_prefix; unsigned int info_size; struct list_head type_entry; char *(*ctor)(char *spec, struct chunk_db *chunk_db); _Bool (*read_chunk)(unsigned char *chunk, unsigned char *digest, void *db_info); _Bool (*write_chunk)(unsigned char *chunk, unsigned char *digest, void *db_info); char *help; }


decompiled_code:
__int64 register_chunkdb_local_chunkdb_type()
{
return register_chunkdb(&local_chunkdb_type);
}

canonical_decompiled_code:
__int64 func3()
{
return register_chunkdb(&local_chunkdb_type);
}

original_code: static void __attribute__((constructor)) register_chunkdb_local_chunkdb_type(void) { register_chunkdb(&local_chunkdb_type); }

canonical_original_code: static void register_chunkdb_local_chunkdb_type() { register_chunkdb(&local_chunkdb_type); }


decompiled_code:
signed __int64 __fastcall base64_map(_BYTE **a1)
{
_BYTE *v1; // rbx
char *v2; // rax

v1 = *a1;
do
{
if ( *v1 == 61 || !*v1 )
return 0LL;
*a1 = ++v1;
v2 = strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", (char)*(v1 - 1));
}
while ( !v2 );
return v2 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}

canonical_decompiled_code:
signed __int64 __fastcall func0(_BYTE **a1)
{
_BYTE *v1; // rbx
char *v2; // rax

v1 = *a1;
do
{
if ( *v1 == 61 || !*v1 )
return 0LL;
*a1 = ++v1;
v2 = strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", (char)*(v1 - 1));
}
while ( !v2 );
return v2 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}

original_code:
static unsigned char base64_map(const char **strp)
{
 const char *str = *strp;
 const char *ptr;
 while (*(str = *strp) && *str != '=') {
  *strp = str + 1;
  ptr = strchr(base64_chars, *str);
  if (ptr)
   return ptr - base64_chars;
 }
 return 0;
}

canonical_original_code:
static unsigned char base64_map(char **strp)
{
 char *str = *strp;
 char *ptr;
 while (*(str = *strp) && *str != '=') {
  *strp = str + 1;
  ptr = strchr(base64_chars, *str);
  if (ptr)
   return ptr - base64_chars;
 }
 return 0;
}

variable_types:
strp
char **

variable_types:
str
char *

variable_types:
ptr
char *

return_type: unsigned char


original_code:
size_t base64_decode(const char *str, unsigned char *buf, size_t size)
{
 unsigned int n;
 size_t i, j, actual = 0;
 for (i = 0; *str && *str != '='; ) {
  n = base64_map(&str) << 18;
  n += base64_map(&str) << 12;
  n += base64_map(&str) << 6;
  n += base64_map(&str);
  if (i < size)
   buf[i++] = (n >> 16) & 255;
  if (i < size)
   buf[i++] = (n >> 8) & 255;
  if (i < size)
   buf[i++] = n & 255;
  actual += 3;
 }
 for (j = 0; j < 3 && str[j] == '='; j ++)
  actual --;
 return actual;
}

canonical_original_code:
unsigned long base64_decode(char *str, unsigned char *buf, unsigned long size)
{
 unsigned int n;
 unsigned long i; unsigned long j; unsigned long actual = 0;
 for (i = 0; *str && *str != '='; ) {
  n = base64_map(&str) << 18;
  n += base64_map(&str) << 12;
  n += base64_map(&str) << 6;
  n += base64_map(&str);
  if (i < size)
   buf[i++] = (n >> 16) & 255;
  if (i < size)
   buf[i++] = (n >> 8) & 255;
  if (i < size)
   buf[i++] = n & 255;
  actual += 3;
 }
 for (j = 0; j < 3 && str[j] == '='; j ++)
  actual --;
 return actual;
}

variable_types:
buf
unsigned char *

variable_types:
size
unsigned long

variable_types:
n
unsigned int

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
actual
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall set_logging(char *filename)
{
char *v1; // r8
struct _IO_FILE *v2; // rdx
__int64 result; // rax
char v4; // dl

if ( zunkfs_log_fp )
return 4294967182LL;
v1 = filename;
if ( filename[1] == 44 )
{
v4 = *filename;
if ( *filename == 84 )
{
zunkfs_log_level = 2;
}
else if ( v4 == 87 )
{
zunkfs_log_level = 1;
}
else
{
result = 4294967274LL;
if ( v4 != 69 )
return result;
zunkfs_log_level = 0;
}
v1 = filename + 2;
}
if ( !strcmp(v1, "stderr") )
{
result = 0LL;
zunkfs_log_fp = stderr;
if ( stderr )
return result;
return (unsigned int)-*_errno_location();
}
if ( !strcmp(v1, "stdout") )
{
v2 = stdout;
zunkfs_log_fp = stdout;
}
else
{
zunkfs_log_fp = fopen64(v1, "w");
v2 = zunkfs_log_fp;
}
result = 0LL;
if ( !v2 )
return (unsigned int)-*_errno_location();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *filename)
{
char *v1; // r8
struct _IO_FILE *v2; // rdx
__int64 result; // rax
char v4; // dl

if ( zunkfs_log_fp )
return 4294967182LL;
v1 = filename;
if ( filename[1] == 44 )
{
v4 = *filename;
if ( *filename == 84 )
{
zunkfs_log_level = 2;
}
else if ( v4 == 87 )
{
zunkfs_log_level = 1;
}
else
{
result = 4294967274LL;
if ( v4 != 69 )
return result;
zunkfs_log_level = 0;
}
v1 = filename + 2;
}
if ( !strcmp(v1, "stderr") )
{
result = 0LL;
zunkfs_log_fp = stderr;
if ( stderr )
return result;
return (unsigned int)-*_errno_location();
}
if ( !strcmp(v1, "stdout") )
{
v2 = stdout;
zunkfs_log_fp = stdout;
}
else
{
zunkfs_log_fp = fopen64(v1, "w");
v2 = zunkfs_log_fp;
}
result = 0LL;
if ( !v2 )
return (unsigned int)-*_errno_location();
return result;
}

original_code:
int set_logging(const char *params)
{
 if (zunkfs_log_fp)
  return -114;
 if (params[1] == ',') {
  switch(params[0]) {
  case 'E':
   zunkfs_log_level = 0;
   break;
  case 'W':
   zunkfs_log_level = 1;
   break;
  case 'T':
   zunkfs_log_level = 2;
   break;
  default:
   return -22;
  }
  params += 2;
 }
 if (!strcmp(params, "stderr"))
  zunkfs_log_fp = stderr;
 else if (!strcmp(params, "stdout"))
  zunkfs_log_fp = stdout;
 else
  zunkfs_log_fp = fopen(params, "w");
 return zunkfs_log_fp ? 0 : -(*__errno_location ());
}

canonical_original_code:
int set_logging(char *params)
{
 if (zunkfs_log_fp)
  return -114;
 if (params[1] == ',') {
  switch(params[0]) {
  case 'E':
   zunkfs_log_level = 0;
   break;
  case 'W':
   zunkfs_log_level = 1;
   break;
  case 'T':
   zunkfs_log_level = 2;
   break;
  default:
   return -22;
  }
  params += 2;
 }
 if (!strcmp(params, "stderr"))
  zunkfs_log_fp = stderr;
 else if (!strcmp(params, "stdout"))
  zunkfs_log_fp = stdout;
 else
  zunkfs_log_fp = fopen(params, "w");
 return zunkfs_log_fp ? 0 : -(*__errno_location ());
}

variable_types:
params
char *


decompiled_code:
__int64 __fastcall dup_log_fd(int fd2)
{
int v1; // eax
__int64 result; // rax

if ( zunkfs_log_fp && (v1 = fileno(zunkfs_log_fp), dup2(v1, fd2)) )
result = (unsigned int)-*_errno_location();
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd2)
{
int v1; // eax
__int64 result; // rax

if ( zunkfs_log_fp && (v1 = fileno(zunkfs_log_fp), dup2(v1, fd2)) )
result = (unsigned int)-*_errno_location();
else
result = 0LL;
return result;
}

original_code:
int dup_log_fd(int to)
{
 if (zunkfs_log_fp && dup2(fileno(zunkfs_log_fp), to))
  return -(*__errno_location ());
 return 0;
}

variable_types: to int


strnlen

original_code:
size_t __attribute__((weak)) strnlen(const char *str, size_t max)
{
 size_t len;
 for (len = 0; len < max && str[len]; len ++)
  ;
 return len;
}

canonical_original_code:
unsigned long strnlen(char *str, unsigned long max)
{
 unsigned long len;
 for (len = 0; len < max && str[len]; len ++)
  ;
 return len;
}

variable_types:
max
unsigned long

variable_types:
len
unsigned long


fls

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

for ( result = 0LL; a1; a1 >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

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

for ( result = 0LL; a1; a1 >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

original_code:
int __attribute__((weak)) fls(int i)
{
 int j = 0;
 while (i) {
  i >>= 1;
  j ++;
 }
 return j;
}

canonical_original_code:
int fls(int i)
{
 int j = 0;
 while (i) {
  i >>= 1;
  j ++;
 }
 return j;
}

variable_types: i int

variable_types: j int


sranddev

decompiled_code:
void sranddev()
{
struct timeval v0; // [rsp+0h] [rbp-18h] BYREF

if ( (unsigned int)_sranddev("/dev/urandom") )
{
if ( (unsigned int)_sranddev("/dev/random") )
{
gettimeofday(&v0, 0LL);
srand(v0.tv_usec);
}
}
}

canonical_decompiled_code:
void func8()
{
struct timeval v0; // [rsp+0h] [rbp-18h] BYREF

if ( (unsigned int)func0("/dev/urandom") )
{
if ( (unsigned int)func0("/dev/random") )
{
gettimeofday(&v0, 0LL);
srand(v0.tv_usec);
}
}
}

original_code:
void __attribute__((weak)) sranddev(void)
{
 struct timeval tv;
 if (!__sranddev("/dev/urandom"))
  return;
 if (!__sranddev("/dev/random"))
  return;
 gettimeofday(&tv, ((void *)0));
 srand(tv.tv_usec);
}

canonical_original_code:
void sranddev()
{
 struct timeval tv;
 if (!__sranddev("/dev/urandom"))
  return;
 if (!__sranddev("/dev/random"))
  return;
 gettimeofday(&tv, ((void *)0));
 srand(tv.tv_usec);
}

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


decompiled_code:
__int64 posix_madvise()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
return 0LL;
}

original_code:
int __attribute__((weak)) posix_madvise(void *ptr, size_t len, int advice)
{
 return 0;
}

canonical_original_code:
int posix_madvise(void *ptr, unsigned long len, int advice)
{
 return 0;
}

variable_types:
ptr
void *

variable_types: advice int


decompiled_code:
char *sprintf_new(char *format, ...)
{
int v1; // eax
char *v2; // rax
char *v3; // r12
char s; // [rsp+7h] [rbp-E1h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = vsnprintf(&s, 1uLL, format, arg);
v2 = (char *)malloc(v1 + 1);
v3 = v2;
if ( !v2 )
return (char *)(int)_PTR_ERR_constprop_0();
va_end(arg);
va_start(arg, format);
vsprintf(v2, format, arg);
return v3;
}

canonical_decompiled_code:
char *func12(char *format, ...)
{
int v1; // eax
char *v2; // rax
char *v3; // r12
char s; // [rsp+7h] [rbp-E1h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = vsnprintf(&s, 1uLL, format, arg);
v2 = (char *)malloc(v1 + 1);
v3 = v2;
if ( !v2 )
return (char *)(int)func5();
va_end(arg);
va_start(arg, format);
vsprintf(v2, format, arg);
return v3;
}

original_code:
char *sprintf_new(const char *fmt, ...)
{
 va_list ap;
 char *str, dummy[1];
 int len;
 __builtin_va_start(ap,fmt);
 len = vsnprintf(dummy, 1, fmt, ap);
 __builtin_va_end(ap);
 str = malloc(len + 1);
 if (!str)
  return __PTR_ERR(12, __FUNCTION__, 217);
 __builtin_va_start(ap,fmt);
 vsprintf(str, fmt, ap);
 __builtin_va_end(ap);
 return str;
}

canonical_original_code:
char *sprintf_new(char *fmt, ...)
{
 __builtin_va_list ap;
 char *str; char dummy[1];
 int len;
 __builtin_va_start(ap,fmt);
 len = vsnprintf(dummy, 1, fmt, ap);
 __builtin_va_end(ap);
 str = malloc(len + 1);
 if (!str)
  return __PTR_ERR(12, __FUNCTION__, 217);
 __builtin_va_start(ap,fmt);
 vsprintf(str, fmt, ap);
 __builtin_va_end(ap);
 return str;
}

variable_types:
fmt
char *

variable_types:
str
char *

variable_types:
dummy
char [1]

variable_types: len int


decompiled_code:
// write access to const memory has been detected, the output may be wrong!
void *util_init()
{
void *result; // rax
int *v1; // rax
char *v2; // rax

result = mmap64(0LL, 0x1000uLL, 0, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
v1 = _errno_location();
v2 = strerror(*v1);
fprintf(stderr, "errptr: %s\n", v2);
exit(-1);
}
_errptr = (__int64)result;
return result;
}

canonical_decompiled_code:
void *func14()
{
void *result; // rax
int *v1; // rax
char *v2; // rax

result = mmap64(0LL, 0x1000uLL, 0, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
v1 = _errno_location();
v2 = strerror(*v1);
fprintf(stderr, "errptr: %s\n", v2);
exit(-1);
}
_errptr = (__int64)result;
return result;
}

original_code:
static void __attribute__((constructor)) util_init(void)
{
 void *errptr = mmap(((void *)0), (256 + 4095) & ~4095, 0x0,
   0x02|0x20, -1, 0);
 if (errptr == ((void *) -1)) {
  fprintf(stderr, "errptr: %s\n", strerror((*__errno_location ())));
  exit(-1);
 }
 memcpy((void *)&__errptr, &errptr, sizeof(void *));
}

canonical_original_code:
static void util_init()
{
 void *errptr = mmap(((void *)0), (256 + 4095) & ~4095, 0x0,
   0x02|0x20, -1, 0);
 if (errptr == ((void *) -1)) {
  fprintf(stderr, "errptr: %s\n", strerror((*__errno_location ())));
  exit(-1);
 }
 memcpy((void *)&__errptr, &errptr, sizeof(void *));
}

variable_types:
errptr
void *


decompiled_code:
__int64 __fastcall xfer_chunk(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD, __int64, __int64), int a4, const char *a5)
{
int v8; // ebx
unsigned int v9; // er12
int *v11; // rax
int v12; // er15
int *v13; // rbx
__pid_t v14; // eax
__pid_t v15; // er15
__int64 v16; // r14
int v17; // eax
int v18; // er12
int *v19; // rax
int v20; // ebp
int *v21; // rbx
char *v22; // rax
int *v23; // rax
char *v24; // rax
int *v25; // rax
int v26; // ebp
int *v27; // rbx
char *v28; // rax
int *v29; // rax
int v30; // ebx
int *v31; // r12
char *v32; // rax
int *v33; // rax
int v34; // ebp
int *v35; // rbx
char *v36; // rax
int *v37; // rax
int v38; // er13
int *v39; // rbp
int *v40; // rax
int *v41; // rbx
int v42; // ebp
char *v43; // rax
int *v44; // [rsp+0h] [rbp-88h]
int v45; // [rsp+Ch] [rbp-7Ch]
int stat_loc; // [rsp+14h] [rbp-74h] BYREF
int pipedes[2]; // [rsp+18h] [rbp-70h] BYREF
char v48[104]; // [rsp+20h] [rbp-68h] BYREF

if ( !a5 )
_assert_fail("fetch_cmd != NULL", "chunk-db-cmd.c", 0x20u, "xfer_chunk");
_digest_string(a2, v48);
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v11 = _errno_location();
v12 = *v11;
v13 = v11;
_zprintf(2LL, "xfer_chunk", 36LL, "chunk=%s using %s\n", v48, a5);
*v13 = v12;
v8 = pipe(pipedes);
if ( v8 )
goto LABEL_5;
}
else
{
v8 = pipe(pipedes);
if ( v8 )
{
LABEL_5:
v9 = 0;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v19 = _errno_location();
v20 = *v19;
v21 = v19;
v22 = strerror(*v19);
_zprintf(0LL, "xfer_chunk", 40LL, "pipe: %s\n", v22);
*v21 = v20;
}
return v9;
}
}
v14 = fork();
v15 = v14;
if ( v14 < 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v25 = _errno_location();
v26 = *v25;
v27 = v25;
v28 = strerror(*v25);
_zprintf(0LL, "xfer_chunk", 46LL, "fork: %s\n", v28);
*v27 = v26;
}
v9 = 0;
close(pipedes[0]);
close(pipedes[1]);
}
else
{
if ( !v14 )
{
close(pipedes[0]);
if ( dup2(pipedes[1], a4) >= 0 )
{
v18 = dup_log_fd(2LL);
if ( v18 )
{
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v33 = _errno_location();
v34 = *v33;
v35 = v33;
v36 = strerror(-v18);
_zprintf(0LL, "xfer_chunk", 96LL, "stderr redirection failed: %s\n", v36);
*v35 = v34;
}
exit(v18);
}
execl(a5, a5, v48, 0LL);
v29 = _errno_location();
v30 = *v29;
v31 = v29;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v32 = strerror(0);
_zprintf(0LL, "xfer_chunk", 102LL, "\"%s %s\" failed: %s\n", a5, v48, v32);
*v31 = v30;
}
exit(-v30);
}
v40 = _errno_location();
v41 = v40;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v42 = *v40;
v43 = strerror(*v40);
_zprintf(0LL, "xfer_chunk", 90LL, "stdio redirection failed: %s\n", v43);
*v41 = v42;
}
exit(-*v41);
}
v16 = 0LL;
close(pipedes[1]);
do
{
while ( 1 )
{
v17 = a3((unsigned int)pipedes[0], a1 + v16, 0x10000 - v16);
if ( v17 < 0 )
break;
if ( !v17 || (v8 += v17, v8 > 0xFFFF) )
{
v9 = 1;
close(pipedes[0]);
waitpid(v15, &stat_loc, 0);
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v37 = _errno_location();
v38 = *v37;
v39 = v37;
_zprintf(2LL, "xfer_chunk", 81LL, "len=%d", (unsigned int)v8);
*v39 = v38;
}
return v9;
}
v16 = v8;
}
v23 = _errno_location();
v44 = v23;
}
while ( *v23 == 4 );
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v45 = *v23;
v24 = strerror(*v23);
_zprintf(1LL, "xfer_chunk", 62LL, "read: %s\n", v24);
*v44 = v45;
}
v9 = 0;
close(pipedes[1]);
kill(v15, 9);
waitpid(v15, 0LL, 0);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD, __int64, __int64), int a4, const char *a5)
{
int v8; // ebx
unsigned int v9; // er12
int *v11; // rax
int v12; // er15
int *v13; // rbx
__pid_t v14; // eax
__pid_t v15; // er15
__int64 v16; // r14
int v17; // eax
int v18; // er12
int *v19; // rax
int v20; // ebp
int *v21; // rbx
char *v22; // rax
int *v23; // rax
char *v24; // rax
int *v25; // rax
int v26; // ebp
int *v27; // rbx
char *v28; // rax
int *v29; // rax
int v30; // ebx
int *v31; // r12
char *v32; // rax
int *v33; // rax
int v34; // ebp
int *v35; // rbx
char *v36; // rax
int *v37; // rax
int v38; // er13
int *v39; // rbp
int *v40; // rax
int *v41; // rbx
int v42; // ebp
char *v43; // rax
int *v44; // [rsp+0h] [rbp-88h]
int v45; // [rsp+Ch] [rbp-7Ch]
int stat_loc; // [rsp+14h] [rbp-74h] BYREF
int pipedes[2]; // [rsp+18h] [rbp-70h] BYREF
char v48[104]; // [rsp+20h] [rbp-68h] BYREF

if ( !a5 )
_assert_fail("fetch_cmd != NULL", "chunk-db-cmd.c", 0x20u, "xfer_chunk");
_digest_string(a2, v48);
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v11 = _errno_location();
v12 = *v11;
v13 = v11;
_zprintf(2LL, "xfer_chunk", 36LL, "chunk=%s using %s\n", v48, a5);
*v13 = v12;
v8 = pipe(pipedes);
if ( v8 )
goto LABEL_5;
}
else
{
v8 = pipe(pipedes);
if ( v8 )
{
LABEL_5:
v9 = 0;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v19 = _errno_location();
v20 = *v19;
v21 = v19;
v22 = strerror(*v19);
_zprintf(0LL, "xfer_chunk", 40LL, "pipe: %s\n", v22);
*v21 = v20;
}
return v9;
}
}
v14 = fork();
v15 = v14;
if ( v14 < 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v25 = _errno_location();
v26 = *v25;
v27 = v25;
v28 = strerror(*v25);
_zprintf(0LL, "xfer_chunk", 46LL, "fork: %s\n", v28);
*v27 = v26;
}
v9 = 0;
close(pipedes[0]);
close(pipedes[1]);
}
else
{
if ( !v14 )
{
close(pipedes[0]);
if ( dup2(pipedes[1], a4) >= 0 )
{
v18 = dup_log_fd(2LL);
if ( v18 )
{
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v33 = _errno_location();
v34 = *v33;
v35 = v33;
v36 = strerror(-v18);
_zprintf(0LL, "xfer_chunk", 96LL, "stderr redirection failed: %s\n", v36);
*v35 = v34;
}
exit(v18);
}
execl(a5, a5, v48, 0LL);
v29 = _errno_location();
v30 = *v29;
v31 = v29;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v32 = strerror(0);
_zprintf(0LL, "xfer_chunk", 102LL, "\"%s %s\" failed: %s\n", a5, v48, v32);
*v31 = v30;
}
exit(-v30);
}
v40 = _errno_location();
v41 = v40;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v42 = *v40;
v43 = strerror(*v40);
_zprintf(0LL, "xfer_chunk", 90LL, "stdio redirection failed: %s\n", v43);
*v41 = v42;
}
exit(-*v41);
}
v16 = 0LL;
close(pipedes[1]);
do
{
while ( 1 )
{
v17 = a3((unsigned int)pipedes[0], a1 + v16, 0x10000 - v16);
if ( v17 < 0 )
break;
if ( !v17 || (v8 += v17, v8 > 0xFFFF) )
{
v9 = 1;
close(pipedes[0]);
waitpid(v15, &stat_loc, 0);
if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v37 = _errno_location();
v38 = *v37;
v39 = v37;
_zprintf(2LL, "xfer_chunk", 81LL, "len=%d", (unsigned int)v8);
*v39 = v38;
}
return v9;
}
v16 = v8;
}
v23 = _errno_location();
v44 = v23;
}
while ( *v23 == 4 );
if ( zunkfs_log_fp && zunkfs_log_level > 0 )
{
v45 = *v23;
v24 = strerror(*v23);
_zprintf(1LL, "xfer_chunk", 62LL, "read: %s\n", v24);
*v44 = v45;
}
v9 = 0;
close(pipedes[1]);
kill(v15, 9);
waitpid(v15, 0LL, 0);
}
return v9;
}

original_code:
static _Bool xfer_chunk(unsigned char *chunk, const unsigned char *digest,
  xfer_fn op, int orig_fd, void *db_info)
{
 const char *fetch_cmd = db_info;
 char chunk_name[(20 * 2)+1];
 int err;
 int fd[2];
 int pid;
 int len;
 int n;
 ((void) sizeof ((fetch_cmd != ((void *)0)) ? 1 : 0), __extension__ ({ if (fetch_cmd != ((void *)0)) ; else __assert_fail ("fetch_cmd != NULL", "chunk-db-cmd.c", 32, __extension__ __PRETTY_FUNCTION__); }));
 __digest_string(digest, chunk_name);
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 36, "chunk=%s using %s\n", chunk_name, fetch_cmd); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 err = pipe(fd);
 if (err) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 40, "pipe: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  return 0;
 }
 pid = fork();
 if (pid < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 46, "fork: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  close(fd[0]);
  close(fd[1]);
  return 0;
 }
 if (pid) {
  int status;
  close(fd[1]);
  for (len = 0; len < (1UL << 16); ) {
   n = op(fd[0], chunk + len, (1UL << 16) - len);
   if (n < 0) {
    if ((*__errno_location ()) == 4)
     continue;
    ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 62, "read: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
    close(fd[1]);
    kill(pid, 9);
    waitpid(pid, ((void *)0), 0);
    return 0;
   }
   if (!n)
    break;
   len += n;
  }
  close(fd[0]);
  waitpid(pid, &status, 0);
  ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 81, "len=%d", len); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  return 1;
 }
 close(fd[0]);
 err = dup2(fd[1], orig_fd);
 if (err < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 90, "stdio redirection failed: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  exit(-(*__errno_location ()));
 }
 err = dup_log_fd(2);
 if (err) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 96, "stderr redirection failed: %s\n", strerror(-err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  exit(err);
 }
 execl(fetch_cmd, fetch_cmd, chunk_name, ((void *)0));
 ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 102, "\"%s %s\" failed: %s\n", fetch_cmd, chunk_name, strerror(err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 exit(-(*__errno_location ()));
 return 0;
}

canonical_original_code:
static _Bool xfer_chunk(unsigned char *chunk, unsigned char *digest, long (*op)(int fd, void *buf, unsigned long len), int orig_fd, void *db_info)
{
 char *fetch_cmd = db_info;
 char chunk_name[-1];
 int err;
 int fd[2];
 int pid;
 int len;
 int n;
 ((void) sizeof ((fetch_cmd != ((void *)0)) ? 1 : 0), __extension__ ({ if (fetch_cmd != ((void *)0)) ; else __assert_fail ("fetch_cmd != NULL", "chunk-db-cmd.c", 32, __extension__ __PRETTY_FUNCTION__); }));
 __digest_string(digest, chunk_name);
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 36, "chunk=%s using %s\n", chunk_name, fetch_cmd); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 err = pipe(fd);
 if (err) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 40, "pipe: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  return 0;
 }
 pid = fork();
 if (pid < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 46, "fork: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  close(fd[0]);
  close(fd[1]);
  return 0;
 }
 if (pid) {
  int status;
  close(fd[1]);
  for (len = 0; len < (1UL << 16); ) {
   n = op(fd[0], chunk + len, (1UL << 16) - len);
   if (n < 0) {
    if ((*__errno_location ()) == 4)
     continue;
    ({ int ___ret = 0; if (zunkfs_log_fp && (1) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(1, __FUNCTION__, 62, "read: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
    close(fd[1]);
    kill(pid, 9);
    waitpid(pid, ((void *)0), 0);
    return 0;
   }
   if (!n)
    break;
   len += n;
  }
  close(fd[0]);
  waitpid(pid, &status, 0);
  ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 81, "len=%d", len); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  return 1;
 }
 close(fd[0]);
 err = dup2(fd[1], orig_fd);
 if (err < 0) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 90, "stdio redirection failed: %s\n", strerror((*__errno_location ()))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  exit(-(*__errno_location ()));
 }
 err = dup_log_fd(2);
 if (err) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 96, "stderr redirection failed: %s\n", strerror(-err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  exit(err);
 }
 execl(fetch_cmd, fetch_cmd, chunk_name, ((void *)0));
 ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 102, "\"%s %s\" failed: %s\n", fetch_cmd, chunk_name, strerror(err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 exit(-(*__errno_location ()));
 return 0;
}

variable_types:
chunk
unsigned char *

variable_types:
digest
unsigned char *

variable_types: long (*)(int fd, void *buf, unsigned long len)

variable_types: err int

variable_types:
fd
int [2]

variable_types: pid int

variable_types: n int

variable_types: status int


decompiled_code:
__int64 __fastcall cmd_write_chunk(__int64 a1, __int64 a2, const char *a3)
{
return xfer_chunk(a1, a2, (__int64 (__fastcall *)(_QWORD, __int64, __int64))&write, 0, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, const char *a3)
{
return func0(a1, a2, (__int64 (__fastcall *)(_QWORD, __int64, __int64))&write, 0, a3);
}

original_code:
static _Bool cmd_write_chunk(const unsigned char *chunk,
  const unsigned char *digest, void *db_info)
{
 return xfer_chunk((unsigned char *)chunk, digest,
   (xfer_fn)write, 0, db_info);
}

canonical_original_code:
static _Bool cmd_write_chunk(unsigned char *chunk, unsigned char *digest, void *db_info)
{
 return xfer_chunk((unsigned char *)chunk, digest,
   (long (*)(int fd, void *buf, unsigned long len))write, 0, db_info);
}


decompiled_code:
__int64 __fastcall cmd_read_chunk(__int64 a1, __int64 a2, const char *a3)
{
return xfer_chunk(a1, a2, (__int64 (__fastcall *)(_QWORD, __int64, __int64))&read, 1, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, const char *a3)
{
return func0(a1, a2, (__int64 (__fastcall *)(_QWORD, __int64, __int64))&read, 1, a3);
}

original_code:
static _Bool cmd_read_chunk(unsigned char *chunk, const unsigned char *digest,
  void *db_info)
{
 return xfer_chunk(chunk, digest, (xfer_fn)read, 1, db_info);
}

canonical_original_code:
static _Bool cmd_read_chunk(unsigned char *chunk, unsigned char *digest, void *db_info)
{
 return xfer_chunk(chunk, digest, (long (*)(int fd, void *buf, unsigned long len))read, 1, db_info);
}


decompiled_code:
__int64 __fastcall cmd_chunkdb_ctor(char *name, __int64 a2)
{
int *v3; // rax
int v4; // er13
int *v5; // r12

if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v3 = _errno_location();
v4 = *v3;
v5 = v3;
_zprintf(2LL, "cmd_chunkdb_ctor", 124LL, "mode=0x%x spec=%s\n", *(unsigned int *)(a2 + 8), name);
*v5 = v4;
}
if ( access(name, 1) )
return sprintf_new("%s is not executable.", name);
*(_QWORD *)(a2 + 16) = name;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *name, __int64 a2)
{
int *v3; // rax
int v4; // er13
int *v5; // r12

if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v3 = _errno_location();
v4 = *v3;
v5 = v3;
_zprintf(2LL, "cmd_chunkdb_ctor", 124LL, "mode=0x%x spec=%s\n", *(unsigned int *)(a2 + 8), name);
*v5 = v4;
}
if ( access(name, 1) )
return sprintf_new("%s is not executable.", name);
*(_QWORD *)(a2 + 16) = name;
return 0LL;
}

original_code:
static char *cmd_chunkdb_ctor(const char *spec, struct chunk_db *cdb)
{
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 124, "mode=0x%x spec=%s\n", cdb->mode, spec); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 if (access(spec, 1))
  return sprintf_new("%s is not executable.", spec);
 cdb->db_info = (void *)spec;
 return ((void *)0);
}

canonical_original_code:
static char *cmd_chunkdb_ctor(char *spec, struct chunk_db *cdb)
{
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 124, "mode=0x%x spec=%s\n", cdb->mode, spec); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 if (access(spec, 1))
  return sprintf_new("%s is not executable.", spec);
 cdb->db_info = (void *)spec;
 return ((void *)0);
}

variable_types:
spec
char *

variable_types: struct chunk_db { struct chunk_db_type { char *spec_prefix; unsigned int info_size; struct list_head { struct list_head *prev; } type_entry; char *(*ctor)(char *spec, struct chunk_db *chunk_db); _Bool (*read_chunk)(unsigned char *chunk, unsigned char *digest, void *db_info); _Bool (*write_chunk)(unsigned char *chunk, unsigned char *digest, void *db_info); char *help; } *type; int mode; void *db_info; struct list_head db_entry; } *


decompiled_code:
__int64 init_chunkdb_cmd()
{
return register_chunkdb(&cmd_chunkdb_type);
}

canonical_decompiled_code:
__int64 func4()
{
return register_chunkdb(&cmd_chunkdb_type);
}

original_code:
static void __attribute__((constructor)) init_chunkdb_cmd(void)
{
 register_chunkdb(&cmd_chunkdb_type);
}

canonical_original_code:
static void init_chunkdb_cmd()
{
 register_chunkdb(&cmd_chunkdb_type);
}


original_code:
void init_mutex(struct mutex *m)
{
 *m = (struct mutex){ { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, 0, { 0, 0 } } }, (pthread_t)-1 };
}

variable_types: struct mutex { 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; } mutex; unsigned long owner; } *


lock

decompiled_code:
pthread_t __fastcall lock(pthread_mutex_t *a1)
{
int v1; // eax
pthread_t result; // rax
int v3; // er12
char *v4; // rax
int *v5; // rax
int v6; // ebp
int *v7; // rbx
char *v8; // rax

v1 = pthread_mutex_lock(a1);
if ( v1 )
{
v3 = v1;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v5 = _errno_location();
v6 = *v5;
v7 = v5;
v8 = strerror(v3);
_zprintf(0LL, "lock", 17LL, "pthread_mutex_lock: %s\n", v8);
*v7 = v6;
}
else
{
v4 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock: %s\n", v4);
}
abort();
}
result = pthread_self();
a1[1].__align = result;
return result;
}

canonical_decompiled_code:
pthread_t __fastcall func1(pthread_mutex_t *a1)
{
int v1; // eax
pthread_t result; // rax
int v3; // er12
char *v4; // rax
int *v5; // rax
int v6; // ebp
int *v7; // rbx
char *v8; // rax

v1 = pthread_mutex_lock(a1);
if ( v1 )
{
v3 = v1;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v5 = _errno_location();
v6 = *v5;
v7 = v5;
v8 = strerror(v3);
_zprintf(0LL, "lock", 17LL, "pthread_mutex_lock: %s\n", v8);
*v7 = v6;
}
else
{
v4 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock: %s\n", v4);
}
abort();
}
result = pthread_self();
a1[1].__align = result;
return result;
}

original_code:
void lock(struct mutex *m)
{
 int err = pthread_mutex_lock(&m->mutex);
 if (err)
  do { if (!({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 17, "pthread_mutex_lock: %s\n", strerror(err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; })) fprintf(stderr, "pthread_mutex_lock: %s\n", strerror(err)); abort(); } while(0);
 m->owner = pthread_self();
}


unlock

decompiled_code:
int __fastcall unlock(pthread_mutex_t *a1)
{
int result; // eax
int v2; // er12
char *v3; // rax
int *v4; // rax
int v5; // ebp
int *v6; // rbx
char *v7; // rax

a1[1].__align = -1LL;
result = pthread_mutex_unlock(a1);
if ( result )
{
v2 = result;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v4 = _errno_location();
v5 = *v4;
v6 = v4;
v7 = strerror(v2);
_zprintf(0LL, "unlock", 27LL, "pthread_mutex_lock: %s\n", v7);
*v6 = v5;
}
else
{
v3 = strerror(result);
fprintf(stderr, "pthread_mutex_lock: %s\n", v3);
}
abort();
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(pthread_mutex_t *a1)
{
int result; // eax
int v2; // er12
char *v3; // rax
int *v4; // rax
int v5; // ebp
int *v6; // rbx
char *v7; // rax

a1[1].__align = -1LL;
result = pthread_mutex_unlock(a1);
if ( result )
{
v2 = result;
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v4 = _errno_location();
v5 = *v4;
v6 = v4;
v7 = strerror(v2);
_zprintf(0LL, "unlock", 27LL, "pthread_mutex_lock: %s\n", v7);
*v6 = v5;
}
else
{
v3 = strerror(result);
fprintf(stderr, "pthread_mutex_lock: %s\n", v3);
}
abort();
}
return result;
}

original_code:
void unlock(struct mutex *m)
{
 int err;
 m->owner = (pthread_t)-1;
 err = pthread_mutex_unlock(&m->mutex);
 if (err)
  do { if (!({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 27, "pthread_mutex_lock: %s\n", strerror(err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; })) fprintf(stderr, "pthread_mutex_lock: %s\n", strerror(err)); abort(); } while(0);
}


trylock

decompiled_code:
_BOOL8 __fastcall trylock(pthread_mutex_t *a1)
{
int v1; // ebp
char *v3; // rax
int *v4; // rax
int v5; // er12
int *v6; // rbx
char *v7; // rax

v1 = pthread_mutex_trylock(a1);
if ( (v1 & 0xFFFFFFEF) != 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v4 = _errno_location();
v5 = *v4;
v6 = v4;
v7 = strerror(v1);
_zprintf(0LL, "trylock", 34LL, "pthread_mutex_lock: %s\n", v7);
*v6 = v5;
}
else
{
v3 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock: %s\n", v3);
}
abort();
}
if ( !v1 )
a1[1].__align = pthread_self();
return v1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(pthread_mutex_t *a1)
{
int v1; // ebp
char *v3; // rax
int *v4; // rax
int v5; // er12
int *v6; // rbx
char *v7; // rax

v1 = pthread_mutex_trylock(a1);
if ( (v1 & 0xFFFFFFEF) != 0 )
{
if ( zunkfs_log_fp && zunkfs_log_level >= 0 )
{
v4 = _errno_location();
v5 = *v4;
v6 = v4;
v7 = strerror(v1);
_zprintf(0LL, "trylock", 34LL, "pthread_mutex_lock: %s\n", v7);
*v6 = v5;
}
else
{
v3 = strerror(v1);
fprintf(stderr, "pthread_mutex_lock: %s\n", v3);
}
abort();
}
if ( !v1 )
a1[1].__align = pthread_self();
return v1 == 0;
}

original_code:
int trylock(struct mutex *m)
{
 int err = pthread_mutex_trylock(&m->mutex);
 if (err && err != 16)
  do { if (!({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 34, "pthread_mutex_lock: %s\n", strerror(err)); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; })) fprintf(stderr, "pthread_mutex_lock: %s\n", strerror(err)); abort(); } while(0);
 if (!err)
  m->owner = pthread_self();
 return !err;
}


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

result = 0LL;
if ( _errptr <= a1 )
result = a1 < _errptr + 256LL;
return result;
}

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

result = 0LL;
if ( _errptr <= a1 )
result = a1 < _errptr + 256LL;
return result;
}

original_code:
static inline int IS_ERR(const void *ptr)
{
 return ptr >= __errptr && ptr < __errptr + 256;
}

canonical_original_code:
static inline int IS_ERR(void *ptr)
{
 return ptr >= __errptr && ptr < __errptr + 256;
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall htole16(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return a1;
}

original_code: static inline le16_t htole16(uint16_t x) { return (le16_t){ x }; }

canonical_original_code: static inline struct le16_t htole16(unsigned short x) { return (struct le16_t){ x }; }

variable_types:
x
unsigned short

return_type: struct le16_t { unsigned short v; }


decompiled_code:
__int64 __fastcall htole64(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return a1;
}

original_code: static inline le64_t htole64(uint64_t x) { return (le64_t){ x }; }

canonical_original_code: static inline struct le64_t htole64(unsigned long x) { return (struct le64_t){ x }; }

variable_types:
x
unsigned long

return_type: struct le64_t { unsigned long v; }


decompiled_code:
__int64 __fastcall le64toh(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return a1;
}

original_code: static inline uint64_t le64toh(le64_t x) { return x.v; }

canonical_original_code: static inline unsigned long le64toh(struct le64_t x) { return x.v; }

variable_types:
x
struct le64_t { unsigned long v; }


decompiled_code:
__int64 str2digest()
{
unsigned __int64 v0; // rax
int v1; // edi
__int64 result; // rax

v0 = _string_digest();
v1 = v0;
if ( IS_ERR(v0) )
result = _PTR_ERR(v1, (__int64)"str2digest", 0x32u);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned __int64 v0; // rax
int v1; // edi
__int64 result; // rax

v0 = _string_digest();
v1 = v0;
if ( func0(v0) )
result = func5(v1, (__int64)"str2digest", 0x32u);
else
result = 0LL;
return result;
}

original_code:
static inline int str2digest(const char *str, unsigned char *digest)
{
 unsigned char *d;
 d = __string_digest(str, digest);
 if (IS_ERR(d))
  return __PTR_ERR(d, __FUNCTION__, 50);
 return 0;
}

canonical_original_code:
static inline int str2digest(char *str, unsigned char *digest)
{
 unsigned char *d;
 d = __string_digest(str, digest);
 if (IS_ERR(d))
  return __PTR_ERR(d, __FUNCTION__, 50);
 return 0;
}

variable_types:
str
char *

variable_types:
d
unsigned char *


decompiled_code:
__int64 __fastcall write_dentry(int fd, __int64 a2)
{
__int64 v2; // r14
int v3; // er12
int v4; // eax
int v5; // eax

v2 = 0LL;
v3 = 0;
do
{
while ( 1 )
{
v4 = write(fd, (const void *)(a2 + v2), 256 - v2);
if ( v4 < 0 )
break;
v3 += v4;
if ( v3 > 255 )
return (unsigned int)v3;
v2 = v3;
}
v5 = *_errno_location();
}
while ( v5 == 4 );
return (unsigned int)-v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, __int64 a2)
{
__int64 v2; // r14
int v3; // er12
int v4; // eax
int v5; // eax

v2 = 0LL;
v3 = 0;
do
{
while ( 1 )
{
v4 = write(fd, (const void *)(a2 + v2), 256 - v2);
if ( v4 < 0 )
break;
v3 += v4;
if ( v3 > 255 )
return (unsigned int)v3;
v2 = v3;
}
v5 = *_errno_location();
}
while ( v5 == 4 );
return (unsigned int)-v5;
}

original_code:
static int write_dentry(int fd, struct disk_dentry *de)
{
 void *ptr = de;
 int len;
 for (len = 0; len < sizeof(struct disk_dentry); ) {
  int n = write(fd, ptr + len, sizeof(struct disk_dentry) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   return -(*__errno_location ());
  }
  len += n;
 }
 return len;
}

variable_types: fd int

variable_types: struct disk_dentry { unsigned char digest[20]; unsigned char secret_digest[20]; struct le16_t { unsigned short v; } mode; unsigned char flags; unsigned char mtime_csec; struct le64_t { unsigned long v; } size; struct le32_t { unsigned int v; } ctime; struct le32_t mtime; unsigned char name[-1]; } *

variable_types: len int


decompiled_code:
__int64 __fastcall test_write_chunk(__int64 a1, __int64 a2)
{
void *v2; // rax
unsigned __int8 v3; // bl
const char *v4; // rax
__int64 v6; // [rsp-Eh] [rbp-10050h] BYREF
char v7[65552]; // [rsp+32h] [rbp-10010h] BYREF

v2 = 0LL;
do
{
v7[(_QWORD)v2] = rand_chunk[(_QWORD)v2] ^ *((_BYTE *)v2 + a1);
v2 = (char *)v2 + 1;
}
while ( v2 != &unk_10000 );
v3 = write_chunk(v7, a2);
v4 = (const char *)_digest_string(a2, &v6);
printf("test_write_chunk: %s\n", v4);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
void *v2; // rax
unsigned __int8 v3; // bl
const char *v4; // rax
__int64 v6; // [rsp-Eh] [rbp-10050h] BYREF
char v7[65552]; // [rsp+32h] [rbp-10010h] BYREF

v2 = 0LL;
do
{
v7[(_QWORD)v2] = rand_chunk[(_QWORD)v2] ^ *((_BYTE *)v2 + a1);
v2 = (char *)v2 + 1;
}
while ( v2 != &unk_10000 );
v3 = write_chunk(v7, a2);
v4 = (const char *)_digest_string(a2, &v6);
printf("test_write_chunk: %s\n", v4);
return v3;
}

original_code:
static int test_write_chunk(const unsigned char *chunk, unsigned char *digest)
{
 unsigned char real_chunk[(1UL << 16)];
 int i, err;
 for (i = 0; i < (1UL << 16); i ++)
  real_chunk[i] = chunk[i] ^ rand_chunk[i];
 err = write_chunk(real_chunk, digest);
 if (err < 0)
  return err;
 printf("test_write_chunk: %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 return err;
}

canonical_original_code:
static int test_write_chunk(unsigned char *chunk, unsigned char *digest)
{
 unsigned char real_chunk[-1];
 int i; int err;
 for (i = 0; i < (1UL << 16); i ++)
  real_chunk[i] = chunk[i] ^ rand_chunk[i];
 err = write_chunk(real_chunk, digest);
 if (err < 0)
  return err;
 printf("test_write_chunk: %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 return err;
}

variable_types:
chunk
unsigned char *

variable_types:
digest
unsigned char *

variable_types: i int

variable_types: err int


decompiled_code:
__int64 __fastcall test_read_chunk(__int64 a1, __int64 a2)
{
const char *v2; // rax
unsigned int v3; // er8
void *v4; // rax
__int64 v6; // [rsp+0h] [rbp-50h] BYREF

v2 = (const char *)_digest_string(a2, &v6);
printf("test_read_chunk: %s\n", v2);
v3 = (unsigned __int8)read_chunk(a1, a2);
v4 = 0LL;
do
{
*((_BYTE *)v4 + a1) ^= rand_chunk[(_QWORD)v4];
v4 = (char *)v4 + 1;
}
while ( v4 != &unk_10000 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
const char *v2; // rax
unsigned int v3; // er8
void *v4; // rax
__int64 v6; // [rsp+0h] [rbp-50h] BYREF

v2 = (const char *)_digest_string(a2, &v6);
printf("test_read_chunk: %s\n", v2);
v3 = (unsigned __int8)read_chunk(a1, a2);
v4 = 0LL;
do
{
*((_BYTE *)v4 + a1) ^= rand_chunk[(_QWORD)v4];
v4 = (char *)v4 + 1;
}
while ( v4 != &unk_10000 );
return v3;
}

original_code:
static int test_read_chunk(unsigned char *chunk, const unsigned char *digest)
{
 int i, err;
 printf("test_read_chunk: %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 err = read_chunk(chunk, digest);
 if (err < 0)
  return err;
 for (i = 0; i < (1UL << 16); i ++)
  chunk[i] ^= rand_chunk[i];
 return err;
}

canonical_original_code:
static int test_read_chunk(unsigned char *chunk, unsigned char *digest)
{
 int i; int err;
 printf("test_read_chunk: %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 err = read_chunk(chunk, digest);
 if (err < 0)
  return err;
 for (i = 0; i < (1UL << 16); i ++)
  chunk[i] ^= rand_chunk[i];
 return err;
}


decompiled_code:
__int64 __fastcall do_store(int fd)
{
int v2; // er12
__int64 v3; // rbx
int v4; // eax
int v5; // edi
char *v6; // rax
const char *v8; // rax
__int64 v9; // [rsp-Eh] [rbp-10088h] BYREF
_BYTE v10[32]; // [rsp+32h] [rbp-10048h] BYREF
__int128 v11; // [rsp+52h] [rbp-10028h] BYREF
_BYTE v12[65520]; // [rsp+62h] [rbp-10018h] BYREF

v2 = 0;
v3 = 0LL;
v11 = 0LL;
memset(v12, 0, sizeof(v12));
while ( 1 )
{
while ( 1 )
{
v4 = read(fd, (char *)&v11 + v3, 0x10000 - v3);
if ( v4 >= 0 )
break;
v5 = *_errno_location();
if ( v5 != 4 )
{
v6 = strerror(v5);
fprintf(stderr, "read: %s\n", v6);
exit(-1);
}
}
if ( !v4 )
break;
v2 += v4;
if ( v2 > 0xFFFF )
goto LABEL_10;
v3 = v2;
}
if ( !v2 )
return 0LL;
LABEL_10:
write_chunk(&v11, v10);
v8 = (const char *)_digest_string(v10, &v9);
printf("Stored chunk %s\n", v8);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd)
{
int v2; // er12
__int64 v3; // rbx
int v4; // eax
int v5; // edi
char *v6; // rax
const char *v8; // rax
__int64 v9; // [rsp-Eh] [rbp-10088h] BYREF
_BYTE v10[32]; // [rsp+32h] [rbp-10048h] BYREF
__int128 v11; // [rsp+52h] [rbp-10028h] BYREF
_BYTE v12[65520]; // [rsp+62h] [rbp-10018h] BYREF

v2 = 0;
v3 = 0LL;
v11 = 0LL;
memset(v12, 0, sizeof(v12));
while ( 1 )
{
while ( 1 )
{
v4 = read(fd, (char *)&v11 + v3, 0x10000 - v3);
if ( v4 >= 0 )
break;
v5 = *_errno_location();
if ( v5 != 4 )
{
v6 = strerror(v5);
fprintf(stderr, "read: %s\n", v6);
exit(-1);
}
}
if ( !v4 )
break;
v2 += v4;
if ( v2 > 0xFFFF )
goto LABEL_10;
v3 = v2;
}
if ( !v2 )
return 0LL;
LABEL_10:
write_chunk(&v11, v10);
v8 = (const char *)_digest_string(v10, &v9);
printf("Stored chunk %s\n", v8);
return 1LL;
}

original_code:
static int do_store(int fd)
{
 unsigned char chunk[(1UL << 16)] = {0};
 unsigned char digest[20];
 int err, n, size = 0;
 while (size < (1UL << 16)) {
  n = read(fd, chunk + size, (1UL << 16) - size);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   fprintf(stderr, "read: %s\n", strerror((*__errno_location ())));
   exit(-1);
  }
  if (!n)
   break;
  size += n;
 }
 if (!size)
  return 0;
 err = write_chunk(chunk, digest);
 if (err < 0) {
  fprintf(stderr, "write_chunk: %s\n", strerror(-err));
  exit(-1);
 }
 printf("Stored chunk %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 return 1;
}

canonical_original_code:
static int do_store(int fd)
{
 unsigned char chunk[-1] = {0};
 unsigned char digest[20];
 int err; int n; int size = 0;
 while (size < (1UL << 16)) {
  n = read(fd, chunk + size, (1UL << 16) - size);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   fprintf(stderr, "read: %s\n", strerror((*__errno_location ())));
   exit(-1);
  }
  if (!n)
   break;
  size += n;
 }
 if (!size)
  return 0;
 err = write_chunk(chunk, digest);
 if (err < 0) {
  fprintf(stderr, "write_chunk: %s\n", strerror(-err));
  exit(-1);
 }
 printf("Stored chunk %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
 return 1;
}

variable_types:
chunk
unsigned char [-1]

variable_types:
digest
unsigned char [20]

variable_types: n int

variable_types: size int


decompiled_code:
__int64 __fastcall verify_chunk(__int64 a1, __int64 a2)
{
return verify_digest(a2, a1, 0x10000LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
return verify_digest(a2, a1, 0x10000LL);
}

original_code:
static inline int verify_chunk(const unsigned char *chunk,
  const unsigned char *digest)
{
 return verify_digest(digest, chunk, (1UL << 16));
}

canonical_original_code:
static inline int verify_chunk(unsigned char *chunk, unsigned char *digest)
{
 return verify_digest(digest, chunk, (1UL << 16));
}


decompiled_code:
int __fastcall do_find(__int64 a1)
{
const char *v2; // rax
__int64 v3; // [rsp-Eh] [rbp-10058h] BYREF
__int128 v4; // [rsp+32h] [rbp-10018h] BYREF
_BYTE v5[65520]; // [rsp+42h] [rbp-10008h] BYREF

v4 = 0LL;
memset(v5, 0, sizeof(v5));
read_chunk(&v4, a1);
if ( !(unsigned int)verify_chunk((__int64)&v4, a1) )
return fwrite("Failed to verify received chunk.\n", 1uLL, 0x21uLL, stderr);
v2 = (const char *)_digest_string(a1, &v3);
return printf("Found chunk %s\n", v2);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
const char *v2; // rax
__int64 v3; // [rsp-Eh] [rbp-10058h] BYREF
__int128 v4; // [rsp+32h] [rbp-10018h] BYREF
_BYTE v5[65520]; // [rsp+42h] [rbp-10008h] BYREF

v4 = 0LL;
memset(v5, 0, sizeof(v5));
read_chunk(&v4, a1);
if ( !(unsigned int)func3((__int64)&v4, a1) )
return fwrite("Failed to verify received chunk.\n", 1uLL, 0x21uLL, stderr);
v2 = (const char *)_digest_string(a1, &v3);
return printf("Found chunk %s\n", v2);
}

original_code:
static void do_find(const unsigned char *digest)
{
 unsigned char chunk[(1UL << 16)] = {0};
 int err;
 err = read_chunk(chunk, digest);
 if (err < 0)
  fprintf(stderr, "read_chunk: %s\n", strerror(-err));
 else if (!verify_chunk(chunk, digest))
  fprintf(stderr, "Failed to verify received chunk.\n");
 else
  printf("Found chunk %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
}

canonical_original_code:
static void do_find(unsigned char *digest)
{
 unsigned char chunk[-1] = {0};
 int err;
 err = read_chunk(chunk, digest);
 if (err < 0)
  fprintf(stderr, "read_chunk: %s\n", strerror(-err));
 else if (!verify_chunk(chunk, digest))
  fprintf(stderr, "Failed to verify received chunk.\n");
 else
  printf("Found chunk %s\n", __digest_string(digest, __builtin_alloca ((20 * 2) + 1)));
}


decompiled_code:
__int64 __fastcall read_chunk_from_file(const char *a1, unsigned int a2, __int64 a3)
{
int v4; // eax
int v5; // ebp
int v6; // er12
__int64 i; // rbx
int v8; // eax

v4 = open64(a1, 0);
if ( v4 < 0 )
{
v6 = -5;
}
else
{
v5 = v4;
if ( lseek64(v4, (unsigned __int64)a2 << 16, 0) == -1 )
{
LABEL_9:
close(v5);
return 4294967291LL;
}
v6 = 0;
for ( i = 0LL; ; i = v6 )
{
while ( 1 )
{
v8 = read(v5, (void *)(a3 + i), 0x10000 - i);
if ( v8 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_9;
}
if ( !v8 )
break;
v6 += v8;
if ( v6 > 0xFFFF )
break;
}
close(v5);
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, unsigned int a2, __int64 a3)
{
int v4; // eax
int v5; // ebp
int v6; // er12
__int64 i; // rbx
int v8; // eax

v4 = open64(a1, 0);
if ( v4 < 0 )
{
v6 = -5;
}
else
{
v5 = v4;
if ( lseek64(v4, (unsigned __int64)a2 << 16, 0) == -1 )
{
LABEL_9:
close(v5);
return 4294967291LL;
}
v6 = 0;
for ( i = 0LL; ; i = v6 )
{
while ( 1 )
{
v8 = read(v5, (void *)(a3 + i), 0x10000 - i);
if ( v8 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_9;
}
if ( !v8 )
break;
v6 += v8;
if ( v6 > 0xFFFF )
break;
}
close(v5);
}
return (unsigned int)v6;
}

original_code:
static int read_chunk_from_file(const char *path, unsigned nr,
  unsigned char *chunk)
{
 int len, n, fd;
 off_t offset;
 fd = open(path, 00);
 if (fd < 0)
  return -5;
 offset = (off_t)nr * (1UL << 16);
 if (lseek(fd, offset, 0) == (off_t)-1) {
  close(fd);
  return -5;
 }
 for (len = 0; len < (1UL << 16); ) {
  n = read(fd, chunk + len, (1UL << 16) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   close(fd);
   return -5;
  }
  if (!n)
   break;
  len += n;
 }
 close(fd);
 return len;
}

canonical_original_code:
static int read_chunk_from_file(char *path, unsigned int nr, unsigned char *chunk)
{
 int len; int n; int fd;
 long offset;
 fd = open(path, 00);
 if (fd < 0)
  return -5;
 offset = (long)nr * (1UL << 16);
 if (lseek(fd, offset, 0) == (off_t)-1) {
  close(fd);
  return -5;
 }
 for (len = 0; len < (1UL << 16); ) {
  n = read(fd, chunk + len, (1UL << 16) - len);
  if (n < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   close(fd);
   return -5;
  }
  if (!n)
   break;
  len += n;
 }
 close(fd);
 return len;
}

variable_types:
path
char *

variable_types:
nr
unsigned int

variable_types: offset long


decompiled_code:
__int64 __fastcall sqlite3_query_callback(__int64 a1, unsigned int a2, const char **a3)
{
unsigned int v3; // er13
__int64 result; // rax
int *v6; // rax
int v7; // er12
int *v8; // rbp

v3 = 0;
if ( !a2 )
return v3;
if ( a2 == 2 )
{
snprintf((char *)a1, 0x1000uLL, "%s", *a3);
*(_DWORD *)(a1 + 4096) = atoi(a3[1]);
result = 0LL;
}
else
{
v3 = 1;
if ( !zunkfs_log_fp || zunkfs_log_level < 0 )
return v3;
v6 = _errno_location();
v7 = *v6;
v8 = v6;
_zprintf(0LL, "sqlite3_query_callback", 92LL, "Unexpected number of results: %d\n", a2);
*v8 = v7;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int a2, const char **a3)
{
unsigned int v3; // er13
__int64 result; // rax
int *v6; // rax
int v7; // er12
int *v8; // rbp

v3 = 0;
if ( !a2 )
return v3;
if ( a2 == 2 )
{
snprintf((char *)a1, 0x1000uLL, "%s", *a3);
*(_DWORD *)(a1 + 4096) = atoi(a3[1]);
result = 0LL;
}
else
{
v3 = 1;
if ( !zunkfs_log_fp || zunkfs_log_level < 0 )
return v3;
v6 = _errno_location();
v7 = *v6;
v8 = v6;
_zprintf(0LL, "sqlite3_query_callback", 92LL, "Unexpected number of results: %d\n", a2);
*v8 = v7;
result = 1LL;
}
return result;
}

original_code:
static int sqlite3_query_callback(void *user_data, int argc, char **argv,
  char **column_names)
{
 struct chunk_map *cmap = user_data;
 if (argc == 0)
  return 0;
 if (argc != 2) {
  ({ int ___ret = 0; if (zunkfs_log_fp && (0) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(0, __FUNCTION__, 92, "Unexpected number of results: %d\n", argc); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  return 1;
 }
 snprintf(cmap->path, 4096, "%s", argv[0]);
 cmap->chunk_nr = atoi(argv[1]);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct chunk_map { char path[4096]; int chunk_nr; } *


decompiled_code:
__int64 register_chunkdb_map_chunkdb_type()
{
return register_chunkdb(&map_chunkdb_type);
}

canonical_decompiled_code:
__int64 func5()
{
return register_chunkdb(&map_chunkdb_type);
}

original_code: static void __attribute__((constructor)) register_chunkdb_map_chunkdb_type(void) { register_chunkdb(&map_chunkdb_type); }

canonical_original_code: static void register_chunkdb_map_chunkdb_type() { register_chunkdb(&map_chunkdb_type); }


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

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

original_code:
static inline void list_add(struct list_head *new, struct list_head *head)
{
 __list_add(new, head, head->next);
}

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

variable_types:
head
struct list_head *


decompiled_code:
void __fastcall list_move(_QWORD *a1, __int64 a2)
{
list_del(a1);
list_add(a1, a2);
}

canonical_decompiled_code:
void __fastcall func4(_QWORD *a1, __int64 a2)
{
func3(a1);
func2(a1, a2);
}

original_code:
static inline void list_move(struct list_head *item, struct list_head *head)
{
 list_del(item);
 list_add(item, head);
}


decompiled_code:
__int64 register_chunkdb_sqlite_chunkdb_type()
{
return register_chunkdb(&sqlite_chunkdb_type);
}

canonical_decompiled_code:
__int64 func3()
{
return register_chunkdb(&sqlite_chunkdb_type);
}

original_code: static void __attribute__((constructor)) register_chunkdb_sqlite_chunkdb_type(void) { register_chunkdb(&sqlite_chunkdb_type); }

canonical_original_code: static void register_chunkdb_sqlite_chunkdb_type() { register_chunkdb(&sqlite_chunkdb_type); }


original_code:
int verify_digest(const unsigned char *digest, const unsigned char *data,
  size_t data_size)
{
 unsigned char tmp_digest[20];
 SHA1(data, data_size, tmp_digest);
 return !memcmp(tmp_digest, digest, 20);
}

canonical_original_code:
int verify_digest(unsigned char *digest, unsigned char *data, unsigned long data_size)
{
 unsigned char tmp_digest[20];
 SHA1(data, data_size, tmp_digest);
 return !memcmp(tmp_digest, digest, 20);
}

variable_types:
data
unsigned char *


decompiled_code:
__int64 __fastcall digest_distance(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
int v4; // eax
int v5; // eax

v2 = 0LL;
while ( 1 )
{
if ( !_BitScanForward((unsigned int *)&v4, *(_DWORD *)(a2 + 4 * v2) ^ *(_DWORD *)(a1 + 4 * v2)) )
v4 = -1;
v5 = v4 + 1;
if ( v5 )
break;
if ( ++v2 == 5 )
return 0xFFFFFFFFLL;
}
return (unsigned int)(160 - v5 - 32 * v2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
int v4; // eax
int v5; // eax

v2 = 0LL;
while ( 1 )
{
if ( !_BitScanForward((unsigned int *)&v4, *(_DWORD *)(a2 + 4 * v2) ^ *(_DWORD *)(a1 + 4 * v2)) )
v4 = -1;
v5 = v4 + 1;
if ( v5 )
break;
if ( ++v2 == 5 )
return 0xFFFFFFFFLL;
}
return (unsigned int)(160 - v5 - 32 * v2);
}

original_code:
int digest_distance(const unsigned char *a, const unsigned char *b)
{
 const unsigned *ia = (void *)a;
 const unsigned *ib = (void *)b;
 int i;
 for (i = 0; i < (20 / sizeof(int)); i ++) {
  unsigned bit = ffs(ia[i] ^ ib[i]);
  if (bit)
   return (20 * 8) - (bit + i * 32);
 }
 return -1;
}

canonical_original_code:
int digest_distance(unsigned char *a, unsigned char *b)
{
 unsigned int *ia = (void *)a;
 unsigned int *ib = (void *)b;
 int i;
 for (i = 0; i < (20 / sizeof(int)); i ++) {
  unsigned int bit = ffs(ia[i] ^ ib[i]);
  if (bit)
   return (20 * 8) - (bit + i * 32);
 }
 return -1;
}

variable_types:
a
unsigned char *

variable_types:
b
unsigned char *

variable_types:
ia
unsigned int *

variable_types:
ib
unsigned int *

variable_types:
bit
unsigned int


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

result = 0LL;
if ( _errptr <= a1 )
result = a1 < _errptr + 256LL;
return result;
}

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

result = 0LL;
if ( _errptr <= a1 )
result = a1 < _errptr + 256LL;
return result;
}

original_code:
static inline int IS_ERR(const void *ptr)
{
 return ptr >= __errptr && ptr < __errptr + 256;
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall htole32(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return a1;
}

original_code: static inline le32_t htole32(uint32_t x) { return (le32_t){ x }; }

canonical_original_code: static inline struct le32_t htole32(unsigned int x) { return (struct le32_t){ x }; }

variable_types:
x
unsigned int

return_type: struct le32_t { unsigned int v; }


decompiled_code:
void __fastcall list_add_tail(_QWORD *a1, __int64 *a2)
{
_list_add(a1, *a2, a2);
}

canonical_decompiled_code:
void __fastcall func1(_QWORD *a1, __int64 *a2)
{
func0(a1, *a2, a2);
}

original_code:
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
 __list_add(new, head->prev, head);
}


main

canonical_name: main

original_code:
int main( int argc, char **argv )
{
  srand(time(((void *)0)));
 for( int n = 101; n < 102; n = n+1+n/3 )
 {
  for( int m = 10001; m < 10002; m = m+1+m/3 )
  {
    float *A = (float*) malloc( m * n * sizeof(float) );
    float *B = (float*) malloc( n * m * sizeof(float) );
    float *C = (float*) malloc( m * m * sizeof(float) );
    for( int i = 0; i < m*n; i++ ) A[i] = 2 * drand48() - 1;
    for( int i = 0; i < n*m; i++ ) B[i] = 2 * drand48() - 1;
    for( int i = 0; i < m*m; i++ ) C[i] = 2 * drand48() - 1;
    double Gflop_s, seconds = -1.0;
    for( int n_iterations = 1; seconds < 0.1; n_iterations *= 2 )
    {
 sgemm( m, n, A, B, C );
      struct timeval start, end;
      gettimeofday( &start, ((void *)0) );
      for( int i = 0; i < n_iterations; i++ )
   sgemm( m,n, A, B, C );
      gettimeofday( &end, ((void *)0) );
      seconds = (end.tv_sec - start.tv_sec) + 1.0e-6 * (end.tv_usec - start.tv_usec);
      Gflop_s = 2e-9 * n_iterations * m * m * n / seconds;
    }
    printf( "%d by %d matrix \t %g Gflop/s\n", m, n, Gflop_s );
    memset( C, 0, sizeof( float ) * m * m );
    if(0){
    for (int x = 0; x<m; x++){
      printf("|");
      for (int y = 0; y<m; y++){
        printf("%.2f|", C[y*m+x]);
      }
      printf("\n");
    }
    }
    sgemm( m,n, A, B, C );
    if(0){
    for (int x = 0; x<m; x++){
      printf("|");
      for (int y = 0; y<m; y++){
        printf("%.2f|", C[y*m+x]);
      }
      printf("\n");
    }
    }
    cblas_sgemm( CblasColMajor,CblasNoTrans,CblasTrans, m,m,n, -1, A,m, B,m, 1, C,m );
    for( int i = 0; i < m*n; i++ ) A[i] = fabs( A[i] );
    for( int i = 0; i < m*n; i++ ) B[i] = fabs( B[i] );
    for( int i = 0; i < m*m; i++ ) C[i] = fabs( C[i] );
    cblas_sgemm( CblasColMajor,CblasNoTrans,CblasTrans, m,m,n, -3.0*1.19209289550781250000000000000000000e-7F*n, A,m, B,m, 1, C,m );
    for( int i = 0; i < m * m; i++ )
      if( C[i] > 0 ) {
 printf( "FAILURE: error in matrix multiply exceeds an acceptable margin\n" );
 return -1;
      }
    free( C );
    free( B );
    free( A );
  }
 }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  srand(time(((void *)0)));
 for( int n = 101; n < 102; n = n+1+n/3 )
 {
  for( int m = 10001; m < 10002; m = m+1+m/3 )
  {
    float *A = (float*) malloc( m * n * sizeof(float) );
    float *B = (float*) malloc( n * m * sizeof(float) );
    float *C = (float*) malloc( m * m * sizeof(float) );
    for( int i = 0; i < m*n; i++ ) A[i] = 2 * drand48() - 1;
    for( int i = 0; i < n*m; i++ ) B[i] = 2 * drand48() - 1;
    for( int i = 0; i < m*m; i++ ) C[i] = 2 * drand48() - 1;
    double Gflop_s; double seconds = -1.0;
    for( int n_iterations = 1; seconds < 0.1; n_iterations *= 2 )
    {
 sgemm( m, n, A, B, C );
      struct timeval start; struct timeval end;
      gettimeofday( &start, ((void *)0) );
      for( int i = 0; i < n_iterations; i++ )
   sgemm( m,n, A, B, C );
      gettimeofday( &end, ((void *)0) );
      seconds = (end.tv_sec - start.tv_sec) + 1.0e-6 * (end.tv_usec - start.tv_usec);
      Gflop_s = 2e-9 * n_iterations * m * m * n / seconds;
    }
    printf( "%d by %d matrix \t %g Gflop/s\n", m, n, Gflop_s );
    memset( C, 0, sizeof( float ) * m * m );
    if(0){
    for (int x = 0; x<m; x++){
      printf("|");
      for (int y = 0; y<m; y++){
        printf("%.2f|", C[y*m+x]);
      }
      printf("\n");
    }
    }
    sgemm( m,n, A, B, C );
    if(0){
    for (int x = 0; x<m; x++){
      printf("|");
      for (int y = 0; y<m; y++){
        printf("%.2f|", C[y*m+x]);
      }
      printf("\n");
    }
    }
    cblas_sgemm( CblasColMajor,CblasNoTrans,CblasTrans, m,m,n, -1, A,m, B,m, 1, C,m );
    for( int i = 0; i < m*n; i++ ) A[i] = fabs( A[i] );
    for( int i = 0; i < m*n; i++ ) B[i] = fabs( B[i] );
    for( int i = 0; i < m*m; i++ ) C[i] = fabs( C[i] );
    cblas_sgemm( CblasColMajor,CblasNoTrans,CblasTrans, m,m,n, -3.0*1.19209289550781250000000000000000000e-7F*n, A,m, B,m, 1, C,m );
    for( int i = 0; i < m * m; i++ )
      if( C[i] > 0 ) {
 printf( "FAILURE: error in matrix multiply exceeds an acceptable margin\n" );
 return -1;
      }
    free( C );
    free( B );
    free( A );
  }
 }
  return 0;
}

variable_types: n int

variable_types: m int

variable_types:
A
float *

variable_types:
B
float *

variable_types:
C
float *

variable_types: i int

variable_types: seconds double

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

variable_types: x int

variable_types: y int


repo: dbacarel/Dplan

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
size_t v4; // rax
__int64 v5; // rbx
_QWORD *v6; // rbp
int v7; // er12
void *v8; // rax
DIR *v9; // r13
int v10; // ebx
struct dirent *v11; // rax
__int64 v12; // rcx
char *v13; // rdi
const char *v14; // rbp
FILE *v15; // r15
__int64 v16; // r14
void *v17; // r14
__int64 v18; // rbx
void *v19; // rdi
void *v20; // rdi
__int64 v21; // rbx
void *v22; // rdi
void *ptr; // [rsp+8h] [rbp-1E0h]
pthread_t newthread; // [rsp+10h] [rbp-1D8h] BYREF
void *thread_return; // [rsp+18h] [rbp-1D0h] BYREF
char s[112]; // [rsp+20h] [rbp-1C8h] BYREF
sigset_t set; // [rsp+90h] [rbp-158h] BYREF
struct sigaction v28; // [rsp+110h] [rbp-D8h] BYREF

memset(&v28, 0, sizeof(v28));
if ( sigemptyset(&set) >= 0 && sigaddset(&set, 15) >= 0 && pthread_sigmask(0, &set, 0LL) >= 0 )
{
v28.sa_handler = (__sighandler_t)&algn_1;
if ( sigaction(13, &v28, 0LL) >= 0 )
{
if ( argc == 1 )
{
puts("Usage: dserver PATH_DIR_AGENDE");
}
else
{
v3 = argv[1];
v4 = strlen(v3);
if ( v4 <= 0x6C )
{
v5 = 0LL;
qmemcpy(path_dir_agende, v3, (unsigned int)(v4 + 1));
agende = 0LL;
thread_return = 0LL;
a_size = 10;
offset = 0;
tid_disp = -1LL;
nomi_agende = malloc(0x50uLL);
v6 = nomi_agende;
if ( !nomi_agende )
{
puts("Problemi durante il settaggio delle strutture dati");
exit(1);
}
while ( 1 )
{
v7 = v5;
v8 = malloc(0x15uLL);
v6[v5] = v8;
if ( !v8 )
break;
if ( ++v5 == 10 )
{
agende = calloc(0x50uLL, 1uLL);
if ( agende )
{
v9 = opendir(path_dir_agende);
if ( !v9 && *_errno_location() == 2 && mkdir(path_dir_agende, 0x1C0u) )
{
perror(&byte_192);
exit(1);
}
v10 = 0;
while ( 1 )
{
v11 = readdir(v9);
if ( !v11 )
break;
if ( v11->d_type == 8 )
{
v12 = 27LL;
v13 = s;
while ( v12 )
{
*(_DWORD *)v13 = 0;
v13 += 4;
--v12;
}
v14 = v11->d_name;
sprintf(s, "%s/%s", path_dir_agende, v11->d_name);
v15 = fopen(s, "r");
if ( v15 )
{
if ( a_size == v10 )
{
v17 = nomi_agende;
ptr = agende;
agende = (void *)enlarge_array(65LL);
if ( !agende )
{
puts("Problemi durante l'allocazione di mem");
cleanup();
exit(1);
}
nomi_agende = (void *)enlarge_array(78LL);
if ( !nomi_agende )
{
puts("Problemi durante l'allocazione di mem");
free(agende);
cleanup();
exit(1);
}
a_size += 10;
free(ptr);
free(v17);
}
v16 = v10;
if ( (int)loadAgenda(v15, (char *)agende + 8 * v10) >= 0 && strlen(v14) <= 0x14 )
{
++v10;
strncpy(*((char **)nomi_agende + v16), v14, 0x15uLL);
++offset;
}
fclose(v15);
}
}
}
closedir(v9);
thread_return = 0LL;
if ( pthread_create(&tid_disp, 0LL, (void *(*)(void *))&thread_dispatcher, 0LL) )
perror("Errore durante la creazione del thread dispatcher");
sleep(1u);
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))&thread_sigHandler, 0LL) )
perror("Errore durante la creazione del thread dispatcher");
pthread_join(newthread, &thread_return);
exit(0);
}
v21 = 0LL;
puts("Problemi durante il settaggio delle strutture dati");
free(nomi_agende);
while ( 1 )
{
v20 = nomi_agende;
if ( a_size <= (int)v21 )
break;
v22 = (void *)*((_QWORD *)nomi_agende + v21++);
free(v22);
}
LABEL_42:
free(v20);
exit(1);
}
}
v18 = 0LL;
puts("Problemi durante il settaggio delle strutture dati");
while ( 1 )
{
v20 = nomi_agende;
if ( v7 <= (int)v18 )
break;
v19 = (void *)*((_QWORD *)nomi_agende + v18++);
free(v19);
}
goto LABEL_42;
}
}
}
}
exit(1);
}

original_code:
int main(int argc, char **argv)
{
    int arg, tmp, tipo;
    short int flags[7];
    char *args[7];
    char *agenda;
    channel_t chan;
    message_t *mess;
    for (tmp = 0; tmp < 7; tmp++)
 flags[tmp] = 0;
    for (tmp = 0; tmp < 7; tmp++)
 args[tmp] = ((void *)0);
    if (argc == 1 || argc == 2) {
 printHelp();
 exit(0);
    }
    while ((arg = getopt(argc, argv, ":cqd:u:g:m:r:")) != -1) {
 switch (arg) {
 case 'c':
     if (checkOpts(flags, 2))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 2, &tipo, 'C', args, ((void *)0));
     break;
 case 'd':
     if (checkOpts(flags, 0))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 0, &tipo, 'D', args, optarg);
     break;
 case 'q':
     if (checkOpts(flags, 3))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 3, &tipo, 'Q', args, ((void *)0));
     break;
 case 'r':
     if (checkOpts(flags, 4))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 4, &tipo, 'R', args, optarg);
     break;
 case 'g':
     if (checkOpts(flags, 5))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 5, &tipo, 'G', args, optarg);
     break;
 case 'm':
     if (checkOpts(flags, 6))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 6, &tipo, 'M', args, optarg);
     break;
 case 'u':
     if (checkOpts(flags, 1))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 1, &tipo, 'D', args, optarg);
     break;
 default:
     printf("dplan: -%c: Opzione errata\n", optopt);
     exit(1);
     ;
 }
    }
    if (flags[0] || flags[1]) {
 if (!(flags[0] && flags[1])) {
     printf("dplan: Formato scorretto del comando\n");
     exit(1);
 }
    }
    if (checkArgs(flags, argc, argv, args, &agenda))
 exit(1);
    chan = -1;
    tmp = -1;
    while (chan < 0 && (++tmp < 5)) {
 chan = openConnection("./tmp/");
 sleep(1);
    }
    if (tmp == 5)
 exit(1);
    mess = (message_t *) calloc(1, sizeof(message_t));
    mess->type = tipo;
    mess->buffer = getBufferString(tipo, agenda, args, &(mess->length));
    sendMessage(chan, mess);
    free(mess->buffer);
    mess->buffer = ((void *)0);
    mess->length = 0;
    mess->type = '0';
    receiveMessage(chan, mess);
    if (tipo == 'G' || tipo == 'M') {
 if (mess->type == '0') {
     printf("dplan: %s: %s\n", agenda, mess->buffer);
     free(mess->buffer);
     while (receiveMessage(chan, mess) >= 0) {
  printf("dplan: %s: %s\n", agenda, mess->buffer);
  free(mess->buffer);
     }
 } else {
     printf("dplan: %s: %s\n", agenda, mess->buffer);
     free(mess->buffer);
 }
    } else {
 if (tipo == 'R') {
     if (mess->type == '0')
  printf("dplan: %s: %s\n", args[4], mess->buffer);
     else
  printf("dplan: %s: %s\n", agenda, mess->buffer);
 } else if (tipo == 'D')
     printf("dplan: %s: %s\n", args[0], mess->buffer);
 else
     printf("dplan: %s: %s\n", agenda, mess->buffer);
 free(mess->buffer);
    }
    free(mess);
    closeConnection(chan);
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    int arg; int tmp; int tipo;
    short flags[7];
    char *args[7];
    char *agenda;
    int chan;
    struct message_t *mess;
    for (tmp = 0; tmp < 7; tmp++)
 flags[tmp] = 0;
    for (tmp = 0; tmp < 7; tmp++)
 args[tmp] = ((void *)0);
    if (argc == 1 || argc == 2) {
 printHelp();
 exit(0);
    }
    while ((arg = getopt(argc, argv, ":cqd:u:g:m:r:")) != -1) {
 switch (arg) {
 case 'c':
     if (checkOpts(flags, 2))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 2, &tipo, 'C', args, ((void *)0));
     break;
 case 'd':
     if (checkOpts(flags, 0))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 0, &tipo, 'D', args, optarg);
     break;
 case 'q':
     if (checkOpts(flags, 3))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 3, &tipo, 'Q', args, ((void *)0));
     break;
 case 'r':
     if (checkOpts(flags, 4))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 4, &tipo, 'R', args, optarg);
     break;
 case 'g':
     if (checkOpts(flags, 5))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 5, &tipo, 'G', args, optarg);
     break;
 case 'm':
     if (checkOpts(flags, 6))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 6, &tipo, 'M', args, optarg);
     break;
 case 'u':
     if (checkOpts(flags, 1))
  { printf("dplan: Formato scorretto del comando\n"); exit(1);}
      else
  setInfo(flags, 1, &tipo, 'D', args, optarg);
     break;
 default:
     printf("dplan: -%c: Opzione errata\n", optopt);
     exit(1);
     ;
 }
    }
    if (flags[0] || flags[1]) {
 if (!(flags[0] && flags[1])) {
     printf("dplan: Formato scorretto del comando\n");
     exit(1);
 }
    }
    if (checkArgs(flags, argc, argv, args, &agenda))
 exit(1);
    chan = -1;
    tmp = -1;
    while (chan < 0 && (++tmp < 5)) {
 chan = openConnection("./tmp/");
 sleep(1);
    }
    if (tmp == 5)
 exit(1);
    mess = (struct message_t *) calloc(1, sizeof(message_t));
    mess->type = tipo;
    mess->buffer = getBufferString(tipo, agenda, args, &(mess->length));
    sendMessage(chan, mess);
    free(mess->buffer);
    mess->buffer = ((void *)0);
    mess->length = 0;
    mess->type = '0';
    receiveMessage(chan, mess);
    if (tipo == 'G' || tipo == 'M') {
 if (mess->type == '0') {
     printf("dplan: %s: %s\n", agenda, mess->buffer);
     free(mess->buffer);
     while (receiveMessage(chan, mess) >= 0) {
  printf("dplan: %s: %s\n", agenda, mess->buffer);
  free(mess->buffer);
     }
 } else {
     printf("dplan: %s: %s\n", agenda, mess->buffer);
     free(mess->buffer);
 }
    } else {
 if (tipo == 'R') {
     if (mess->type == '0')
  printf("dplan: %s: %s\n", args[4], mess->buffer);
     else
  printf("dplan: %s: %s\n", agenda, mess->buffer);
 } else if (tipo == 'D')
     printf("dplan: %s: %s\n", args[0], mess->buffer);
 else
     printf("dplan: %s: %s\n", agenda, mess->buffer);
 free(mess->buffer);
    }
    free(mess);
    closeConnection(chan);
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: arg int

variable_types: tmp int

variable_types: tipo int

variable_types:
flags
short [7]

variable_types:
args
char *[7]

variable_types:
agenda
char *

variable_types: chan int

variable_types: struct message_t { char type; unsigned int length; char *buffer; } *