RTreeRectVolume

decompiled_code:
double __fastcall RTreeRectVolume(double *a1)
{
double result; // xmm0_8

if ( !a1 )
_assert_fail("r", "src/rectangle.c", 0x9Au, "RTreeRectVolume");
if ( *a1 > a1[2] )
return 0.0;
result = (a1[2] - *a1) * (a1[3] - a1[1]);
if ( result < 0.0 )
_assert_fail("volume >= 0.0", "src/rectangle.c", 0xA0u, "RTreeRectVolume");
return result;
}

canonical_decompiled_code:
double __fastcall func4(double *a1)
{
double result; // xmm0_8

if ( !a1 )
_assert_fail("r", "src/rectangle.c", 0x9Au, "RTreeRectVolume");
if ( *a1 > a1[2] )
return 0.0;
result = (a1[2] - *a1) * (a1[3] - a1[1]);
if ( result < 0.0 )
_assert_fail("volume >= 0.0", "src/rectangle.c", 0xA0u, "RTreeRectVolume");
return result;
}

original_code:
RectReal RTreeRectVolume(struct Rect *R)
{
    register struct Rect *r = R;
    register int i;
    register RectReal volume = (RectReal)1;
    ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "src/rectangle.c", 154, __extension__ __PRETTY_FUNCTION__); }));
    if (((r)->boundary[0] > (r)->boundary[2]))
        return (RectReal)0;
    for(i=0; i<2; i++)
        volume *= r->boundary[i+2] - r->boundary[i];
    ((void) sizeof ((volume >= 0.0) ? 1 : 0), __extension__ ({ if (volume >= 0.0) ; else __assert_fail ("volume >= 0.0", "src/rectangle.c", 160, __extension__ __PRETTY_FUNCTION__); }));
    return volume;
}

canonical_original_code:
double RTreeRectVolume(struct Rect *R)
{
    register struct Rect *r = R;
    register int i;
    register double volume = (RectReal)1;
    ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "src/rectangle.c", 154, __extension__ __PRETTY_FUNCTION__); }));
    if (((r)->boundary[0] > (r)->boundary[2]))
        return (double)0;
    for(i=0; i<2; i++)
        volume *= r->boundary[i+2] - r->boundary[i];
    ((void) sizeof ((volume >= 0.0) ? 1 : 0), __extension__ ({ if (volume >= 0.0) ; else __assert_fail ("volume >= 0.0", "src/rectangle.c", 160, __extension__ __PRETTY_FUNCTION__); }));
    return volume;
}

variable_types: volume double


RTreeRectSurfaceArea

repo: davidreynolds/rtree

decompiled_code:
double __fastcall RTreeRectSurfaceArea(double *a1)
{
double v1; // xmm2_8
double result; // xmm0_8
double v3; // xmm1_8

if ( !a1 )
_assert_fail("r", "src/rectangle.c", 0x120u, "RTreeRectSurfaceArea");
v1 = a1[2];
result = 0.0;
if ( *a1 <= v1 )
{
v3 = a1[3] - a1[1] + 0.0;
result = v1 - *a1 + v3 + v1 - *a1 + v3;
}
return result;
}

canonical_decompiled_code:
double __fastcall func6(double *a1)
{
double v1; // xmm2_8
double result; // xmm0_8
double v3; // xmm1_8

if ( !a1 )
_assert_fail("r", "src/rectangle.c", 0x120u, "RTreeRectSurfaceArea");
v1 = a1[2];
result = 0.0;
if ( *a1 <= v1 )
{
v3 = a1[3] - a1[1] + 0.0;
result = v1 - *a1 + v3 + v1 - *a1 + v3;
}
return result;
}

original_code:
RectReal RTreeRectSurfaceArea(struct Rect *R)
{
    register struct Rect *r = R;
    register int i, j;
    register RectReal sum = (RectReal)0;
    ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "src/rectangle.c", 288, __extension__ __PRETTY_FUNCTION__); }));
    if (((r)->boundary[0] > (r)->boundary[2]))
        return (RectReal)0;
    for (i=0; i<2; i++) {
        RectReal face_area = (RectReal)1;
        for (j=0; j<2; j++)
            if(i != j) {
                RectReal j_extent =
                    r->boundary[j+2] - r->boundary[j];
                face_area *= j_extent;
            }
        sum += face_area;
    }
    return 2 * sum;
}

canonical_original_code:
double RTreeRectSurfaceArea(struct Rect *R)
{
    register struct Rect *r = R;
    register int i; register int j;
    register double sum = (RectReal)0;
    ((void) sizeof ((r) ? 1 : 0), __extension__ ({ if (r) ; else __assert_fail ("r", "src/rectangle.c", 288, __extension__ __PRETTY_FUNCTION__); }));
    if (((r)->boundary[0] > (r)->boundary[2]))
        return (double)0;
    for (i=0; i<2; i++) {
        double face_area = (RectReal)1;
        for (j=0; j<2; j++)
            if(i != j) {
                double j_extent = r->boundary[j+2] - r->boundary[j];
                face_area *= j_extent;
            }
        sum += face_area;
    }
    return 2 * sum;
}

variable_types:
R
struct Rect { double boundary[-1]; } *

variable_types:
r
struct Rect { double boundary[-1]; } *

variable_types: i int

variable_types: j int

variable_types: sum double

return_type: double


RTreeOverlap

decompiled_code:
_BOOL8 __fastcall RTreeOverlap(double *a1, double *a2)
{
if ( !a1 || !a2 )
_assert_fail("r && s", "src/rectangle.c", 0x155u, "RTreeOverlap");
if ( *a1 > a2[2] )
return 0LL;
if ( *a2 > a1[2] )
return 0LL;
if ( a1[1] > a2[3] )
return 0LL;
return a2[1] <= a1[3];
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(double *a1, double *a2)
{
if ( !a1 || !a2 )
_assert_fail("r && s", "src/rectangle.c", 0x155u, "RTreeOverlap");
if ( *a1 > a2[2] )
return 0LL;
if ( *a2 > a1[2] )
return 0LL;
if ( a1[1] > a2[3] )
return 0LL;
return a2[1] <= a1[3];
}

original_code:
int RTreeOverlap(struct Rect *R, struct Rect *S)
{
    register struct Rect *r = R, *s = S;
    register int i, j;
    ((void) sizeof ((r && s) ? 1 : 0), __extension__ ({ if (r && s) ; else __assert_fail ("r && s", "src/rectangle.c", 341, __extension__ __PRETTY_FUNCTION__); }));
    for (i=0; i<2; i++)
    {
        j = i + 2;
        if (r->boundary[i] > s->boundary[j] ||
            s->boundary[i] > r->boundary[j])
        {
            return 0;
        }
    }
    return 1;
}

canonical_original_code:
int RTreeOverlap(struct Rect *R, struct Rect *S)
{
    register struct Rect *r = R; register struct Rect *s = S;
    register int i; register int j;
    ((void) sizeof ((r && s) ? 1 : 0), __extension__ ({ if (r && s) ; else __assert_fail ("r && s", "src/rectangle.c", 341, __extension__ __PRETTY_FUNCTION__); }));
    for (i=0; i<2; i++)
    {
        j = i + 2;
        if (r->boundary[i] > s->boundary[j] ||
            s->boundary[i] > r->boundary[j])
        {
            return 0;
        }
    }
    return 1;
}

variable_types:
S
struct Rect *

variable_types:
s
struct Rect { double boundary[-1]; } *

return_type: int


RTreeContained

decompiled_code:
_BOOL8 __fastcall RTreeContained(double *a1, double *a2)
{
double v2; // xmm1_8
_BOOL8 result; // rax
double v4; // xmm3_8

if ( !a1 || !a2 )
_assert_fail("(long)r && (long)s", "src/rectangle.c", 0x16Bu, "RTreeContained");
v2 = a1[2];
result = 1LL;
if ( *a1 <= v2 )
{
v4 = a2[2];
result = 0LL;
if ( *a2 <= v4 )
{
if ( *a1 < *a2 )
{
result = 0LL;
}
else if ( v4 >= v2 )
{
if ( a1[1] >= a2[1] )
result = a2[3] >= a1[3];
else
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(double *a1, double *a2)
{
double v2; // xmm1_8
_BOOL8 result; // rax
double v4; // xmm3_8

if ( !a1 || !a2 )
_assert_fail("(long)r && (long)s", "src/rectangle.c", 0x16Bu, "RTreeContained");
v2 = a1[2];
result = 1LL;
if ( *a1 <= v2 )
{
v4 = a2[2];
result = 0LL;
if ( *a2 <= v4 )
{
if ( *a1 < *a2 )
{
result = 0LL;
}
else if ( v4 >= v2 )
{
if ( a1[1] >= a2[1] )
result = a2[3] >= a1[3];
else
result = 0LL;
}
}
}
return result;
}

original_code:
int RTreeContained(struct Rect *R, struct Rect *S)
{
    register struct Rect *r = R, *s = S;
    register int i, j, result;
    ((void) sizeof (((long)r && (long)s) ? 1 : 0), __extension__ ({ if ((long)r && (long)s) ; else __assert_fail ("(long)r && (long)s", "src/rectangle.c", 363, __extension__ __PRETTY_FUNCTION__); }));
    if (((r)->boundary[0] > (r)->boundary[2]))
        return 1;
    if (((s)->boundary[0] > (s)->boundary[2]))
        return 0;
    result = 1;
    for (i = 0; i < 2; i++)
    {
        j = i + 2;
        result = result
            && r->boundary[i] >= s->boundary[i]
            && r->boundary[j] <= s->boundary[j];
    }
    return result;
}

canonical_original_code:
int RTreeContained(struct Rect *R, struct Rect *S)
{
    register struct Rect *r = R; register struct Rect *s = S;
    register int i; register int j; register int result;
    ((void) sizeof (((long)r && (long)s) ? 1 : 0), __extension__ ({ if ((long)r && (long)s) ; else __assert_fail ("(long)r && (long)s", "src/rectangle.c", 363, __extension__ __PRETTY_FUNCTION__); }));
    if (((r)->boundary[0] > (r)->boundary[2]))
        return 1;
    if (((s)->boundary[0] > (s)->boundary[2]))
        return 0;
    result = 1;
    for (i = 0; i < 2; i++)
    {
        j = i + 2;
        result = result
            && r->boundary[i] >= s->boundary[i]
            && r->boundary[j] <= s->boundary[j];
    }
    return result;
}

variable_types: result int


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

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

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

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

original_code:
static int set_max(int *which, int new_max)
{
    if(2 > new_max || new_max > (int)((4096 -(2*sizeof(int))) / sizeof(struct Branch)))
        return 0;
    *which = new_max;
    return 1;
}

variable_types:
which
int *

user_defined_types: struct Branch { struct Rect rect; struct Node *child; }

user_defined_types: struct Node { int count; int level; struct Branch branch[-1]; }


RTreeSetNodeMax

decompiled_code:
__int64 __fastcall RTreeSetNodeMax(int a1)
{
return set_max(&NODECARD, a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return func0(&NODECARD, a1);
}

original_code:
int RTreeSetNodeMax(int new_max)
{
    return set_max(&NODECARD, new_max);
}


RTreeSetLeafMax

decompiled_code:
__int64 __fastcall RTreeSetLeafMax(int a1)
{
return set_max(&LEAFCARD, a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return func0(&LEAFCARD, a1);
}

original_code:
int RTreeSetLeafMax(int new_max)
{
    return set_max(&LEAFCARD, new_max);
}


RTreeGetNodeMax

decompiled_code:
__int64 RTreeGetNodeMax()
{
return (unsigned int)NODECARD;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)NODECARD;
}

original_code:
int RTreeGetNodeMax()
{
    return NODECARD;
}


RTreeGetLeafMax

decompiled_code:
__int64 RTreeGetLeafMax()
{
return (unsigned int)LEAFCARD;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)LEAFCARD;
}

original_code:
int RTreeGetLeafMax()
{
    return LEAFCARD;
}


repo: dayne/zunkfs

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

canonical_decompiled_code:
void __fastcall func2(_QWORD *a1, __int64 *a2)
{
func1(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);
}

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

variable_types:
head
struct list_head *

return_type: void


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

result = list_del(a1);
*a1 = (__int64)a1;
a1[1] = (__int64)a1;
return result;
}

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

result = func3(a1);
*a1 = (__int64)a1;
a1[1] = (__int64)a1;
return result;
}

original_code:
static inline void list_del_init(struct list_head *item)
{
 list_del(item);
 do { (item)->next = (item)->prev = (item); } while(0);
}

user_defined_types: struct list_head { struct list_head *prev; }


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

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == a1;
}

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

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


decompiled_code:
void __fastcall list_move_tail(__int64 *a1, __int64 *a2)
{
list_del(a1);
list_add_tail(a1, a2);
}

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

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


decompiled_code:
_BOOL8 __fastcall is_cnode_dirty(__int64 a1)
{
return !list_empty(a1 + 65560);
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(__int64 a1)
{
return !func5(a1 + 65560);
}

original_code:
static inline int is_cnode_dirty(const struct chunk_node *cnode)
{
 return !list_empty(&cnode->dirty_entry);
}

canonical_original_code:
static inline int is_cnode_dirty(struct chunk_node *cnode)
{
 return !list_empty(&cnode->dirty_entry);
}

variable_types: 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; } *

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 chunk_tree { struct chunk_node *root; unsigned int nr_leafs; unsigned int height; struct chunk_tree_operations *ops; struct list_head dirty_list; }


original_code:
static inline void mark_cnode_dirty(struct chunk_node *cnode)
{
 list_move_tail(&cnode->dirty_entry, &cnode->ctree->dirty_list);
}

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:
_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 func9(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 flush_chunk_node(_QWORD *a1)
{
__int64 result; // rax
__int64 v3; // rdi

result = is_cnode_dirty((__int64)a1);
if ( (_DWORD)result )
{
result = (*(__int64 (__fastcall **)(_QWORD *, _QWORD))(*(_QWORD *)(a1[8194] + 16LL) + 16LL))(a1, a1[0x2000]);
if ( (int)result >= 0 )
{
v3 = a1[8193];
if ( v3 )
mark_cnode_dirty(v3);
list_del_init(a1 + 8195);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
__int64 result; // rax
__int64 v3; // rdi

result = func7((__int64)a1);
if ( (_DWORD)result )
{
result = (*(__int64 (__fastcall **)(_QWORD *, _QWORD))(*(_QWORD *)(a1[8194] + 16LL) + 16LL))(a1, a1[0x2000]);
if ( (int)result >= 0 )
{
v3 = a1[8193];
if ( v3 )
func8(v3);
func4(a1 + 8195);
result = 0LL;
}
}
return result;
}

original_code:
static int flush_chunk_node(struct chunk_node *cnode)
{
 int err;
 if (is_cnode_dirty(cnode)) {
  err = cnode->ctree->ops->write_chunk(cnode->chunk_data,
    cnode->chunk_digest);
  if (err < 0)
   return err;
  if (cnode->parent)
   mark_cnode_dirty(cnode->parent);
  list_del_init(&cnode->dirty_entry);
 }
 return 0;
}

variable_types: err int


original_code:
static struct chunk_node *new_chunk_node(struct chunk_tree *ctree,
  unsigned char *chunk_digest, int leaf)
{
 struct chunk_node *cnode;
 int err;
 ((void) sizeof ((chunk_digest != ((void *)0)) ? 1 : 0), __extension__ ({ if (chunk_digest != ((void *)0)) ; else __assert_fail ("chunk_digest != NULL", "chunk-tree.c", 25, __extension__ __PRETTY_FUNCTION__); }));
 cnode = malloc(sizeof(struct chunk_node));
 if (!cnode)
  return __ERR_PTR(12, __FUNCTION__, 29);
 err = -12;
 cnode->_private = ((void *)0);
 if (!leaf) {
  cnode->_private = calloc(((1UL << 16) / 20), sizeof(void *));
  if (!cnode->_private)
   goto error;
 }
 cnode->ctree = ctree;
 cnode->chunk_digest = chunk_digest;
 cnode->parent = ((void *)0);
 list_head_init(&cnode->dirty_entry);
 cnode->ref_count = 0;
 return cnode;
error:
 free(cnode);
 return __ERR_PTR(-err, __FUNCTION__, 48);
}

variable_types:
ctree
struct chunk_tree *

variable_types: leaf int

return_type: 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; } *


original_code:
static int grow_chunk_tree(struct chunk_tree *ctree)
{
 struct chunk_node *new_root;
 struct chunk_node *old_root;
 old_root = ctree->root;
 new_root = new_chunk_node(ctree, old_root->chunk_digest, 0);
 if (IS_ERR(new_root))
  return -__PTR_ERR(new_root, __FUNCTION__, 62);
 memset(new_root->chunk_data, 0, (1UL << 16));
 old_root->parent = new_root;
 old_root->chunk_digest = new_root->chunk_data;
 memcpy(old_root->chunk_digest, new_root->chunk_digest,
   20);
 mark_cnode_dirty(new_root);
 new_root->ref_count = 2;
 ((struct chunk_node **)(new_root)->_private)[0] = old_root;
 ctree->height ++;
 ctree->root = new_root;
 __put_chunk_node(old_root, ctree->height == 1);
 return 0;
}

variable_types: struct chunk_tree { struct chunk_node { unsigned char chunk_data[-1]; unsigned char *chunk_digest; struct chunk_node *parent; struct chunk_tree *ctree; struct list_head { struct list_head *prev; } dirty_entry; unsigned int ref_count; void *_private; } *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 dirty_list; } *


decompiled_code:
__int64 __fastcall chunk_nr(__int64 a1)
{
__int64 result; // rax
int v2; // ebx

result = 0LL;
if ( **(_QWORD **)(a1 + 65552) != a1 )
{
v2 = chunk_nr(*(_QWORD *)(a1 + 65544));
result = (unsigned int)_chunk_nr(a1) + 3276 * v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
__int64 result; // rax
int v2; // ebx

result = 0LL;
if ( **(_QWORD **)(a1 + 65552) != a1 )
{
v2 = func17(*(_QWORD *)(a1 + 65544));
result = (unsigned int)func11(a1) + 3276 * v2;
}
return result;
}

original_code:
unsigned chunk_nr(const struct chunk_node *cnode)
{
 if (cnode == cnode->ctree->root)
  return 0;
 return ((1UL << 16) / 20) * chunk_nr(cnode->parent) +
  __chunk_nr(cnode);
}

canonical_original_code:
unsigned int chunk_nr(struct chunk_node *cnode)
{
 if (cnode == cnode->ctree->root)
  return 0;
 return ((1UL << 16) / 20) * chunk_nr(cnode->parent) +
  __chunk_nr(cnode);
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall put_chunk_node(void *a1)
{
return _put_chunk_node(a1, 1);
}

canonical_decompiled_code:
__int64 __fastcall func18(void *a1)
{
return func13(a1, 1);
}

original_code:
void put_chunk_node(struct chunk_node *cnode)
{
 __put_chunk_node(cnode, 1);
}


original_code:
int init_chunk_tree(struct chunk_tree *ctree, unsigned nr_leafs,
  unsigned char *root_digest, struct chunk_tree_operations *ops)
{
 struct chunk_node *root;
 int err;
 if (!root_digest)
  return -22;
 list_head_init(&ctree->dirty_list);
 ctree->ops = ops;
 ctree->nr_leafs = nr_leafs;
 ctree->height = 0;
 nr_leafs -= !!nr_leafs;
 ctree->height = (fls(nr_leafs) - 1) / (fls(((1UL << 16) / 20)) - 1);
 ctree->height += !!nr_leafs;
 root = new_chunk_node(ctree, root_digest, !ctree->height);
 if (IS_ERR(root))
  return -__PTR_ERR(root, __FUNCTION__, 259);
 err = ctree->ops->read_chunk(root->chunk_data, root_digest);
 if (err < 0) {
  free(root);
  return err;
 }
 root->ref_count ++;
 ctree->root = root;
 return 0;
}

canonical_original_code:
int init_chunk_tree(struct chunk_tree *ctree, unsigned int nr_leafs, unsigned char *root_digest, struct chunk_tree_operations *ops)
{
 struct chunk_node *root;
 int err;
 if (!root_digest)
  return -22;
 list_head_init(&ctree->dirty_list);
 ctree->ops = ops;
 ctree->nr_leafs = nr_leafs;
 ctree->height = 0;
 nr_leafs -= !!nr_leafs;
 ctree->height = (fls(nr_leafs) - 1) / (fls(((1UL << 16) / 20)) - 1);
 ctree->height += !!nr_leafs;
 root = new_chunk_node(ctree, root_digest, !ctree->height);
 if (IS_ERR(root))
  return -__PTR_ERR(root, __FUNCTION__, 259);
 err = ctree->ops->read_chunk(root->chunk_data, root_digest);
 if (err < 0) {
  free(root);
  return err;
 }
 root->ref_count ++;
 ctree->root = root;
 return 0;
}

variable_types:
ops
struct chunk_tree_operations *


decompiled_code:
void __fastcall free_chunk_tree(__int64 *a1)
{
_QWORD *v1; // rbx
void *v2; // rdi

v1 = (_QWORD *)*a1;
if ( *(_DWORD *)(*a1 + 65576) != 1 )
_assert_fail("croot->ref_count == 1", "chunk-tree.c", 0x115u, "free_chunk_tree");
if ( is_cnode_dirty(*a1) )
flush_chunk_node(v1);
v2 = (void *)v1[8198];
if ( v2 )
free(v2);
free(v1);
}

canonical_decompiled_code:
void __fastcall func20(__int64 *a1)
{
_QWORD *v1; // rbx
void *v2; // rdi

v1 = (_QWORD *)*a1;
if ( *(_DWORD *)(*a1 + 65576) != 1 )
_assert_fail("croot->ref_count == 1", "chunk-tree.c", 0x115u, "free_chunk_tree");
if ( func7(*a1) )
func10(v1);
v2 = (void *)v1[8198];
if ( v2 )
free(v2);
free(v1);
}

original_code:
void free_chunk_tree(struct chunk_tree *ctree)
{
 struct chunk_node *croot = ctree->root;
 ((void) sizeof ((croot->ref_count == 1) ? 1 : 0), __extension__ ({ if (croot->ref_count == 1) ; else __assert_fail ("croot->ref_count == 1", "chunk-tree.c", 277, __extension__ __PRETTY_FUNCTION__); }));
 if (is_cnode_dirty(croot))
  flush_chunk_node(croot);
 if (croot->_private)
  free(croot->_private);
 free(croot);
}


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

while ( !list_empty(a1 + 24) )
{
result = flush_chunk_node((_QWORD *)(*(_QWORD *)(a1 + 32) - 65560LL));
if ( (_DWORD)result )
return result;
}
return 0LL;
}

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

while ( !func5(a1 + 24) )
{
result = func10((_QWORD *)(*(_QWORD *)(a1 + 32) - 65560LL));
if ( (_DWORD)result )
return result;
}
return 0LL;
}

original_code:
int flush_chunk_tree(struct chunk_tree *ctree)
{
 struct chunk_node *cnode;
 int error;
 while (!list_empty(&ctree->dirty_list)) {
  cnode = ((struct chunk_node *)((unsigned long)(ctree->dirty_list.next) - (unsigned long)&((struct chunk_node *)0)->dirty_entry));
  error = flush_chunk_node(cnode);
  if (error)
   return error;
 }
 return 0;
}

variable_types: error int


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

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


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

canonical_original_code:
static inline void list_move_tail(struct list_head *item, struct list_head *head)
{
 list_del(item);
 list_add_tail(item, head);
}


canonical_original_code:
static inline void mark_cnode_dirty(struct chunk_node *cnode)
{
 list_move_tail(&cnode->dirty_entry, &cnode->ctree->dirty_list);
}


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

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


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

canonical_decompiled_code:
__int64 __fastcall func6(__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; }

return_type: unsigned long


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

result = 4042322160LL;
if ( *(_QWORD *)(a1 + 65584) != 4042322160LL )
_assert_fail("cnode->_private == CACHED_CHUNK_MAGIC", "file.c", 0x39u, "clear_cached");
*(_QWORD *)(a1 + 65584) = 0LL;
return result;
}

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

result = 4042322160LL;
if ( *(_QWORD *)(a1 + 65584) != 4042322160LL )
_assert_fail("cnode->_private == CACHED_CHUNK_MAGIC", "file.c", 0x39u, "clear_cached");
*(_QWORD *)(a1 + 65584) = 0LL;
return result;
}

original_code:
static inline void clear_cached(struct chunk_node *cnode)
{
 ((void) sizeof ((cnode->_private == ((void *)0xf0f0f0f0)) ? 1 : 0), __extension__ ({ if (cnode->_private == ((void *)0xf0f0f0f0)) ; else __assert_fail ("cnode->_private == CACHED_CHUNK_MAGIC", "file.c", 57, __extension__ __PRETTY_FUNCTION__); }));
 cnode->_private = ((void *)0);
}


decompiled_code:
__int64 __fastcall is_cached(__int64 a1)
{
__int64 v1; // rdx
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 65584);
LOBYTE(result) = v1 == 4042322160LL;
if ( v1 && v1 != 4042322160LL )
_assert_fail("cnode->_private == CACHED_CHUNK_MAGIC || cnode->_private == NULL", "file.c", 0x3Fu, "is_cached");
return (unsigned __int8)result;
}

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

v1 = *(_QWORD *)(a1 + 65584);
LOBYTE(result) = v1 == 4042322160LL;
if ( v1 && v1 != 4042322160LL )
_assert_fail("cnode->_private == CACHED_CHUNK_MAGIC || cnode->_private == NULL", "file.c", 0x3Fu, "is_cached");
return (unsigned __int8)result;
}

original_code:
static inline int is_cached(struct chunk_node *cnode)
{
 ((void) sizeof ((cnode->_private == ((void *)0xf0f0f0f0) || cnode->_private == ((void *)0)) ? 1 : 0), __extension__ ({ if (cnode->_private == ((void *)0xf0f0f0f0) || cnode->_private == ((void *)0)) ; else __assert_fail ("cnode->_private == CACHED_CHUNK_MAGIC || cnode->_private == NULL", "file.c", 63, __extension__ __PRETTY_FUNCTION__); }));
 return cnode->_private == ((void *)0xf0f0f0f0);
}


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

if ( *(_QWORD *)(a1 + 65584) )
_assert_fail("cnode->_private == NULL", "file.c", 0x33u, "set_cached");
result = 4042322160LL;
*(_QWORD *)(a1 + 65584) = 4042322160LL;
return result;
}

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

if ( *(_QWORD *)(a1 + 65584) )
_assert_fail("cnode->_private == NULL", "file.c", 0x33u, "set_cached");
result = 4042322160LL;
*(_QWORD *)(a1 + 65584) = 4042322160LL;
return result;
}

original_code:
static inline void set_cached(struct chunk_node *cnode)
{
 ((void) sizeof ((cnode->_private == ((void *)0)) ? 1 : 0), __extension__ ({ if (cnode->_private == ((void *)0)) ; else __assert_fail ("cnode->_private == NULL", "file.c", 51, __extension__ __PRETTY_FUNCTION__); }));
 cnode->_private = ((void *)0xf0f0f0f0);
}


decompiled_code:
__int64 __fastcall release_cached_chunks(_QWORD *a1)
{
__int64 result; // rax
__int64 *v2; // rbx
__int64 v3; // rbp

result = have_mutex(*a1 + 32LL);
if ( !(_DWORD)result )
_assert_fail("have_mutex(&(ofile)->dentry->mutex)", "file.c", 0x79u, "release_cached_chunks");
v2 = a1 + 1;
do
{
v3 = *v2;
if ( *v2 )
{
clear_cached(*v2);
*v2 = 0LL;
result = put_chunk_node(v3);
}
++v2;
}
while ( v2 != a1 + 257 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(_QWORD *a1)
{
__int64 result; // rax
__int64 *v2; // rbx
__int64 v3; // rbp

result = func13(*a1 + 32LL);
if ( !(_DWORD)result )
_assert_fail("have_mutex(&(ofile)->dentry->mutex)", "file.c", 0x79u, "release_cached_chunks");
v2 = a1 + 1;
do
{
v3 = *v2;
if ( *v2 )
{
func10(*v2);
*v2 = 0LL;
result = put_chunk_node(v3);
}
++v2;
}
while ( v2 != a1 + 257 );
return result;
}

original_code:
static void release_cached_chunks(struct open_file *ofile)
{
 struct chunk_node *cnode;
 int i;
 ((void) sizeof ((have_mutex(&(ofile)->dentry->mutex)) ? 1 : 0), __extension__ ({ if (have_mutex(&(ofile)->dentry->mutex)) ; else __assert_fail ("have_mutex(&(ofile)->dentry->mutex)", "file.c", 121, __extension__ __PRETTY_FUNCTION__); }));
 for (i = 0; i < (16 * (1UL << 16) / 4096); i ++) {
  cnode = ofile->ccache[i];
  if (cnode) {
   clear_cached(cnode);
   ofile->ccache[i] = ((void *)0);
   put_chunk_node(cnode);
  }
 }
}

variable_types: struct open_file { 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; } *dentry; struct chunk_node *ccache[-1]; unsigned int ccache_index; } *

variable_types: i int

user_defined_types: struct open_file { struct dentry *dentry; struct chunk_node *ccache[-1]; unsigned int ccache_index; }

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

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 __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


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

if ( cachable(a1) && !(unsigned int)is_cached(a2) )
result = _cache_file_chunk(a1, a2);
else
result = put_chunk_node(a2);
return result;
}

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

if ( func7(a1) && !(unsigned int)func11(a2) )
result = func15(a1, a2);
else
result = put_chunk_node(a2);
return result;
}

original_code:
static inline void cache_file_chunk(struct open_file *ofile,
  struct chunk_node *cnode)
{
 if (cachable(ofile) && !is_cached(cnode))
  __cache_file_chunk(ofile, cnode);
 else
  put_chunk_node(cnode);
}

variable_types:
cnode
struct chunk_node *

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

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


decompiled_code:
__int64 __fastcall rw_file(_QWORD *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 a4, int a5)
{
unsigned __int64 v6; // rdx
unsigned int v7; // ebp
unsigned __int64 v9; // r14
__int64 v10; // r12
unsigned int v11; // ebx
unsigned __int64 v12; // rcx
__int64 v13; // [rsp+8h] [rbp-60h]
unsigned int v14; // [rsp+10h] [rbp-58h]
unsigned __int64 v16; // [rsp+20h] [rbp-48h]

v6 = *(_QWORD *)(*a1 + 144LL) << 8;
if ( (*(_DWORD *)(*a1 + 168LL) & 0xF000) != 0x4000 )
v6 = *(_QWORD *)(*a1 + 144LL);
v16 = v6;
if ( a4 > v6 )
return (unsigned int)-22;
if ( !a5 || (v7 = 0, v6 != a4) )
{
if ( a3 > 0x7FFFFFFF )
return (unsigned int)-22;
if ( a5 )
{
if ( a4 + a3 > v6 )
a3 = v6 - a4;
v9 = a4 >> 16;
v14 = (unsigned __int16)a4;
if ( !a3 )
return 0;
}
else
{
v9 = a4 >> 16;
v14 = (unsigned __int16)a4;
if ( !a3 )
{
if ( (*(_DWORD *)(*a1 + 168LL) & 0xF000) != 0x4000 )
{
v7 = 0;
goto LABEL_27;
}
goto LABEL_28;
}
}
v7 = 0;
v10 = 0LL;
do
{
v13 = get_dentry_chunk(*a1, (unsigned int)v9);
if ( IS_ERR(v13) )
return (unsigned int)_PTR_ERR(v13, (__int64)"rw_file", 0xDBu);
v11 = 0x10000 - v14;
if ( (int)(a3 - v7) <= 0x10000 - (unsigned __int64)v14 )
v11 = a3 - v7;
if ( a5 )
{
if ( (int)v11 > v16 - v10 )
v11 = v16 - v7;
memcpy((void *)(a2 + v10), (const void *)(v13 + v14), (int)v11);
}
else
{
memcpy((void *)(v13 + v14), (const void *)(a2 + v10), (int)v11);
mark_cnode_dirty(v13);
}
v7 += v11;
cache_file_chunk((__int64)a1, v13);
LODWORD(v9) = v9 + 1;
v10 = (int)v7;
v14 = 0;
}
while ( (int)v7 < a3 );
if ( a5 )
return v7;
if ( (*(_DWORD *)(*a1 + 168LL) & 0xF000) != 0x4000 )
{
v12 = a4 + (int)v7;
if ( v12 > v16 )
*(_QWORD *)(*a1 + 144LL) = v12;
LABEL_27:
gettimeofday((struct timeval *)(*a1 + 152LL), 0LL);
*(_BYTE *)(*a1 + 136LL) |= 1u;
return v7;
}
LABEL_28:
_assert_fail("!S_ISDIR(ofile->dentry->mode)", "file.c", 0xF1u, "rw_file");
}
return v7;
}

original_code:
static int rw_file(struct open_file *ofile, char *buf, size_t bufsz,
  off_t offset, int read)
{
 struct chunk_node *cnode;
 unsigned chunk_nr;
 unsigned chunk_off;
 uint64_t file_size;
 int len, cplen;
 file_size = ofile->dentry->size;
 if (((((ofile->dentry->mode)) & 0170000) == (0040000)))
  file_size *= sizeof(struct disk_dentry);
 if (offset > file_size)
  return -22;
 if (read && offset == file_size)
  return 0;
 if (bufsz > 0x7fffffff)
  return -22;
 if (read && (bufsz + offset) > file_size)
  bufsz = file_size - offset;
 chunk_nr = offset / (1UL << 16);
 chunk_off = offset % (1UL << 16);
 len = 0;
 while (len < bufsz) {
  cnode = get_dentry_chunk(ofile->dentry, chunk_nr);
  if (IS_ERR(cnode))
   return __PTR_ERR(cnode, __FUNCTION__, 219);
  cplen = bufsz - len;
  if (cplen > (1UL << 16) - chunk_off)
   cplen = (1UL << 16) - chunk_off;
  if (read) {
   if (cplen > file_size - len)
    cplen = file_size - len;
   memcpy(buf + len, cnode->chunk_data + chunk_off, cplen);
  } else {
   memcpy(cnode->chunk_data + chunk_off, buf + len, cplen);
   mark_cnode_dirty(cnode);
  }
  len += cplen;
  cache_file_chunk(ofile, cnode);
  chunk_nr ++;
  chunk_off = 0;
 }
 if (!read) {
  ((void) sizeof ((!((((ofile->dentry->mode)) & 0170000) == (0040000))) ? 1 : 0), __extension__ ({ if (!((((ofile->dentry->mode)) & 0170000) == (0040000))) ; else __assert_fail ("!S_ISDIR(ofile->dentry->mode)", "file.c", 241, __extension__ __PRETTY_FUNCTION__); }));
  if ((len + offset) > file_size)
   ofile->dentry->size = len + offset;
  gettimeofday(&ofile->dentry->mtime, ((void *)0));
  ofile->dentry->dirty = 1;
 }
 return len;
}

canonical_original_code:
static int rw_file(struct open_file *ofile, char *buf, unsigned long bufsz, long offset, int read)
{
 struct chunk_node *cnode;
 unsigned int chunk_nr;
 unsigned int chunk_off;
 unsigned long file_size;
 int len; int cplen;
 file_size = ofile->dentry->size;
 if (((((ofile->dentry->mode)) & 0170000) == (0040000)))
  file_size *= sizeof(struct disk_dentry);
 if (offset > file_size)
  return -22;
 if (read && offset == file_size)
  return 0;
 if (bufsz > 0x7fffffff)
  return -22;
 if (read && (bufsz + offset) > file_size)
  bufsz = file_size - offset;
 chunk_nr = offset / (1UL << 16);
 chunk_off = offset % (1UL << 16);
 len = 0;
 while (len < bufsz) {
  cnode = get_dentry_chunk(ofile->dentry, chunk_nr);
  if (IS_ERR(cnode))
   return __PTR_ERR(cnode, __FUNCTION__, 219);
  cplen = bufsz - len;
  if (cplen > (1UL << 16) - chunk_off)
   cplen = (1UL << 16) - chunk_off;
  if (read) {
   if (cplen > file_size - len)
    cplen = file_size - len;
   memcpy(buf + len, cnode->chunk_data + chunk_off, cplen);
  } else {
   memcpy(cnode->chunk_data + chunk_off, buf + len, cplen);
   mark_cnode_dirty(cnode);
  }
  len += cplen;
  cache_file_chunk(ofile, cnode);
  chunk_nr ++;
  chunk_off = 0;
 }
 if (!read) {
  ((void) sizeof ((!((((ofile->dentry->mode)) & 0170000) == (0040000))) ? 1 : 0), __extension__ ({ if (!((((ofile->dentry->mode)) & 0170000) == (0040000))) ; else __assert_fail ("!S_ISDIR(ofile->dentry->mode)", "file.c", 241, __extension__ __PRETTY_FUNCTION__); }));
  if ((len + offset) > file_size)
   ofile->dentry->size = len + offset;
  gettimeofday(&ofile->dentry->mtime, ((void *)0));
  ofile->dentry->dirty = 1;
 }
 return len;
}

variable_types:
buf
char *

variable_types:
bufsz
unsigned long

variable_types: offset long

variable_types: read int

variable_types: len int

variable_types: cplen int


decompiled_code:
__int64 __fastcall write_dir(_QWORD *a1, __int64 a2, unsigned __int64 a3, char a4)
{
unsigned __int64 v7; // r12
__int64 result; // rax
int *v9; // r14
int *v10; // r14
__int64 v11; // [rsp-Eh] [rbp-D0h] BYREF
__int64 v12; // [rsp+32h] [rbp-90h] BYREF
int v13; // [rsp+7Eh] [rbp-44h]
__int64 v14; // [rsp+82h] [rbp-40h]
__int64 v15; // [rsp+8Ah] [rbp-38h]

if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v9 = _errno_location();
LODWORD(v15) = *v9;
_zprintf(2LL, "write_dir", 261LL, "len=%zu off=%zu\n", a3);
*v9 = v15;
}
if ( a3 <= 0xFF )
return 4294967274LL;
v7 = (unsigned __int8)(a3 | a4);
if ( (_DWORD)v7 )
return 4294967274LL;
while ( 1 )
{
v15 = a2;
if ( !le64toh(*(_QWORD *)(a2 + 44)) )
break;
result = dup_disk_dentry(*a1, a2);
if ( (_DWORD)result )
return result;
if ( zunkfs_log_fp )
{
if ( zunkfs_log_level > 1 )
{
v10 = _errno_location();
v13 = *v10;
v14 = _digest_string(a2 + 20, &v12);
_digest_string(v15, &v11);
_zprintf(2LL, "write_dir", 279LL, "%s %s %s\n", a2 + 60);
*v10 = v13;
}
}
v7 += 256LL;
a2 += 256LL;
if ( a3 <= v7 )
return (unsigned int)v7;
}
return 4294967274LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(_QWORD *a1, __int64 a2, unsigned __int64 a3, char a4)
{
unsigned __int64 v7; // r12
__int64 result; // rax
int *v9; // r14
int *v10; // r14
__int64 v11; // [rsp-Eh] [rbp-D0h] BYREF
__int64 v12; // [rsp+32h] [rbp-90h] BYREF
int v13; // [rsp+7Eh] [rbp-44h]
__int64 v14; // [rsp+82h] [rbp-40h]
__int64 v15; // [rsp+8Ah] [rbp-38h]

if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v9 = _errno_location();
LODWORD(v15) = *v9;
_zprintf(2LL, "write_dir", 261LL, "len=%zu off=%zu\n", a3);
*v9 = v15;
}
if ( a3 <= 0xFF )
return 4294967274LL;
v7 = (unsigned __int8)(a3 | a4);
if ( (_DWORD)v7 )
return 4294967274LL;
while ( 1 )
{
v15 = a2;
if ( !func6(*(_QWORD *)(a2 + 44)) )
break;
result = dup_disk_dentry(*a1, a2);
if ( (_DWORD)result )
return result;
if ( zunkfs_log_fp )
{
if ( zunkfs_log_level > 1 )
{
v10 = _errno_location();
v13 = *v10;
v14 = _digest_string(a2 + 20, &v12);
_digest_string(v15, &v11);
_zprintf(2LL, "write_dir", 279LL, "%s %s %s\n", a2 + 60);
*v10 = v13;
}
}
v7 += 256LL;
a2 += 256LL;
if ( a3 <= v7 )
return (unsigned int)v7;
}
return 4294967274LL;
}

original_code:
static int write_dir(struct open_file *ofile, const char *buf, size_t len,
  off_t offset)
{
 const struct disk_dentry *ddent;
 size_t total = 0;
 int err;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 261, "len=%zu off=%zu\n", len, (size_t)offset); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 if (len < sizeof(struct disk_dentry))
  return -22;
 if (len % sizeof(struct disk_dentry))
  return -22;
 if (offset % sizeof(struct disk_dentry))
  return -22;
 while (total < len) {
  ddent = (struct disk_dentry *)(buf + total);
  if (!le64toh(ddent->size))
   return -22;
  err = dup_disk_dentry(ofile->dentry, ddent);
  if (err)
   return err;
  ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 279, "%s %s %s\n", (char *)ddent->name, __digest_string(ddent->digest, __builtin_alloca ((20 * 2) + 1)), __digest_string(ddent->secret_digest, __builtin_alloca ((20 * 2) + 1))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  total += sizeof(struct disk_dentry);
 }
 return total;
}

canonical_original_code:
static int write_dir(struct open_file *ofile, char *buf, unsigned long len, long offset)
{
 struct disk_dentry *ddent;
 unsigned long total = 0;
 int err;
 ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 261, "len=%zu off=%zu\n", len, (unsigned long)offset); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
 if (len < sizeof(struct disk_dentry))
  return -22;
 if (len % sizeof(struct disk_dentry))
  return -22;
 if (offset % sizeof(struct disk_dentry))
  return -22;
 while (total < len) {
  ddent = (struct disk_dentry *)(buf + total);
  if (!le64toh(ddent->size))
   return -22;
  err = dup_disk_dentry(ofile->dentry, ddent);
  if (err)
   return err;
  ({ int ___ret = 0; if (zunkfs_log_fp && (2) <= zunkfs_log_level) { int ___saved_errno = (*__errno_location ()); __zprintf(2, __FUNCTION__, 279, "%s %s %s\n", (char *)ddent->name, __digest_string(ddent->digest, __builtin_alloca ((20 * 2) + 1)), __digest_string(ddent->secret_digest, __builtin_alloca ((20 * 2) + 1))); (*__errno_location ()) = ___saved_errno; ___ret = 1; } ___ret; });
  total += sizeof(struct disk_dentry);
 }
 return total;
}

variable_types:
len
unsigned long

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:
total
unsigned long


decompiled_code:
__int64 __fastcall close_file(void *ptr)
{
__int64 v2; // rdi
unsigned int v3; // ebp

lock(*(_QWORD *)ptr + 32LL);
release_cached_chunks(ptr);
v2 = *(_QWORD *)ptr;
v3 = 0;
if ( *(_QWORD *)(*(_QWORD *)ptr + 88LL) )
v3 = flush_chunk_tree(v2 + 88);
unlock(*(_QWORD *)ptr + 32LL);
put_dentry(*(_QWORD *)ptr);
free(ptr);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func21(void *ptr)
{
__int64 v2; // rdi
unsigned int v3; // ebp

lock(*(_QWORD *)ptr + 32LL);
func14(ptr);
v2 = *(_QWORD *)ptr;
v3 = 0;
if ( *(_QWORD *)(*(_QWORD *)ptr + 88LL) )
v3 = flush_chunk_tree(v2 + 88);
unlock(*(_QWORD *)ptr + 32LL);
put_dentry(*(_QWORD *)ptr);
free(ptr);
return v3;
}

original_code:
int close_file(struct open_file *ofile)
{
 unsigned retv = 0;
 lock(&(ofile)->dentry->mutex);
 release_cached_chunks(ofile);
 if (ofile->dentry->chunk_tree.root)
  retv = flush_chunk_tree(&ofile->dentry->chunk_tree);
 unlock(&(ofile)->dentry->mutex);
 put_dentry(ofile->dentry);
 memset(ofile, 0xcc, sizeof(struct open_file));
 free(ofile);
 return retv;
}

canonical_original_code:
int close_file(struct open_file *ofile)
{
 unsigned int retv = 0;
 lock(&(ofile)->dentry->mutex);
 release_cached_chunks(ofile);
 if (ofile->dentry->chunk_tree.root)
  retv = flush_chunk_tree(&ofile->dentry->chunk_tree);
 unlock(&(ofile)->dentry->mutex);
 put_dentry(ofile->dentry);
 memset(ofile, 0xcc, sizeof(struct open_file));
 free(ofile);
 return retv;
}

variable_types:
retv
unsigned int


decompiled_code:
__int64 __fastcall flush_file(_QWORD *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebp

lock(*a1 + 32LL);
release_cached_chunks(a1);
v2 = *a1;
v3 = 0;
if ( *(_QWORD *)(*a1 + 88LL) )
v3 = flush_chunk_tree(v2 + 88);
unlock(*a1 + 32LL);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func22(_QWORD *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebp

lock(*a1 + 32LL);
func14(a1);
v2 = *a1;
v3 = 0;
if ( *(_QWORD *)(*a1 + 88LL) )
v3 = flush_chunk_tree(v2 + 88);
unlock(*a1 + 32LL);
return v3;
}

original_code:
int flush_file(struct open_file *ofile)
{
 unsigned retv = 0;
 lock(&(ofile)->dentry->mutex);
 release_cached_chunks(ofile);
 if (ofile->dentry->chunk_tree.root)
  retv = flush_chunk_tree(&ofile->dentry->chunk_tree);
 unlock(&(ofile)->dentry->mutex);
 return retv;
}

canonical_original_code:
int flush_file(struct open_file *ofile)
{
 unsigned int retv = 0;
 lock(&(ofile)->dentry->mutex);
 release_cached_chunks(ofile);
 if (ofile->dentry->chunk_tree.root)
  retv = flush_chunk_tree(&ofile->dentry->chunk_tree);
 unlock(&(ofile)->dentry->mutex);
 return retv;
}


decompiled_code:
__int64 __fastcall read_file(_QWORD *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 a4)
{
unsigned int v6; // ebp

lock(*a1 + 32LL);
v6 = rw_file(a1, a2, a3, a4, 1);
unlock(*a1 + 32LL);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func23(_QWORD *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 a4)
{
unsigned int v6; // ebp

lock(*a1 + 32LL);
v6 = func17(a1, a2, a3, a4, 1);
unlock(*a1 + 32LL);
return v6;
}

original_code:
int read_file(struct open_file *ofile, char *buf, size_t bufsz, off_t offset)
{
 int len;
 lock(&(ofile)->dentry->mutex);
 len = rw_file(ofile, buf, bufsz, offset, 1);
 unlock(&(ofile)->dentry->mutex);
 return len;
}

canonical_original_code:
int read_file(struct open_file *ofile, char *buf, unsigned long bufsz, long offset)
{
 int len;
 lock(&(ofile)->dentry->mutex);
 len = rw_file(ofile, buf, bufsz, offset, 1);
 unlock(&(ofile)->dentry->mutex);
 return len;
}


decompiled_code:
__int64 __fastcall write_file(_QWORD *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 a4)
{
unsigned int v6; // ebp

lock(*a1 + 32LL);
if ( (*(_DWORD *)(*a1 + 168LL) & 0xF000) == 0x4000 )
v6 = write_dir(a1, a2, a3, a4);
else
v6 = rw_file(a1, a2, a3, a4, 0);
unlock(*a1 + 32LL);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func24(_QWORD *a1, __int64 a2, unsigned __int64 a3, unsigned __int64 a4)
{
unsigned int v6; // ebp

lock(*a1 + 32LL);
if ( (*(_DWORD *)(*a1 + 168LL) & 0xF000) == 0x4000 )
v6 = func18(a1, a2, a3, a4);
else
v6 = func17(a1, a2, a3, a4, 0);
unlock(*a1 + 32LL);
return v6;
}

original_code:
int write_file(struct open_file *ofile, const char *buf, size_t len, off_t off)
{
 int retv;
 lock(&(ofile)->dentry->mutex);
 if (((((ofile->dentry->mode)) & 0170000) == (0040000)))
  retv = write_dir(ofile, buf, len, off);
 else
  retv = rw_file(ofile, (char *)buf, len, off, 0);
 unlock(&(ofile)->dentry->mutex);
 return retv;
}

canonical_original_code:
int write_file(struct open_file *ofile, char *buf, unsigned long len, long off)
{
 int retv;
 lock(&(ofile)->dentry->mutex);
 if (((((ofile->dentry->mode)) & 0170000) == (0040000)))
  retv = write_dir(ofile, buf, len, off);
 else
  retv = rw_file(ofile, (char *)buf, len, off, 0);
 unlock(&(ofile)->dentry->mutex);
 return retv;
}

variable_types: off long

variable_types: retv int


decompiled_code:
__int64 __fastcall file_dentry(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func25(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
struct dentry *file_dentry(struct open_file *ofile)
{
 return ofile->dentry;
}

variable_types: struct open_file { struct dentry *dentry; struct chunk_node *ccache[-1]; unsigned int ccache_index; } *

return_type: 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; } *


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 le16toh(unsigned int a1)
{
return a1;
}

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

original_code: static inline uint16_t le16toh(le16_t x) { return x.v; }

canonical_original_code: static inline unsigned short le16toh(struct le16_t x) { return x.v; }

variable_types:
x
struct le16_t { unsigned short v; }

return_type: unsigned short


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

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

original_code: static inline uint32_t le32toh(le32_t x) { return x.v; }

canonical_original_code: static inline unsigned int le32toh(struct le32_t x) { return x.v; }

variable_types:
x
struct le32_t { unsigned int v; }


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

variable_types:
x
struct le64_t { unsigned long v; }


decompiled_code:
__int64 __fastcall read_dentry(int fd, __int64 a2)
{
unsigned int v2; // ebx
int v3; // eax
int v5; // eax

v2 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + (int)v2), 256LL - (int)v2);
if ( v5 < 0 )
break;
if ( !v5 )
return v2;
v2 += v5;
LABEL_5:
if ( v2 > 0xFF )
return v2;
}
v3 = *_errno_location();
if ( v3 == 4 )
goto LABEL_5;
return (unsigned int)-v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, __int64 a2)
{
unsigned int v2; // ebx
int v3; // eax
int v5; // eax

v2 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + (int)v2), 256LL - (int)v2);
if ( v5 < 0 )
break;
if ( !v5 )
return v2;
v2 += v5;
LABEL_5:
if ( v2 > 0xFF )
return v2;
}
v3 = *_errno_location();
if ( v3 == 4 )
goto LABEL_5;
return (unsigned int)-v3;
}

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

variable_types: fd int

variable_types: n int


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

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

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

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

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


decompiled_code:
_BOOL8 __fastcall is_cnode_dirty(__int64 a1)
{
return !list_empty(a1 + 65560);
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1)
{
return !func0(a1 + 65560);
}

original_code:
static inline int is_cnode_dirty(const struct chunk_node *cnode)
{
 return !list_empty(&cnode->dirty_entry);
}

canonical_original_code:
static inline int is_cnode_dirty(struct chunk_node *cnode)
{
 return !list_empty(&cnode->dirty_entry);
}

variable_types: 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; } *


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

canonical_decompiled_code:
__int64 __fastcall func2(__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));
}

variable_types:
chunk
unsigned char *

variable_types:
digest
unsigned char *


decompiled_code:
void __fastcall dump_cnode(__int64 a1, const char *a2, int a3, void (__fastcall *a4)(__int64, const char *))
{
const char *v6; // rax
const void *v7; // rcx
__int64 v8; // rdx
__int64 v9; // rdi
__int64 v10; // rbx
unsigned int v11; // er13
__int64 v12; // [rsp-Eh] [rbp-78h] BYREF

if ( a1 )
{
v6 = (const char *)_digest_string(*(_QWORD *)(a1 + 0x10000), &v12);
printf("%s%p:%p: %s", a2, (const void *)a1, *(const void **)(a1 + 0x10000), v6);
if ( is_cnode_dirty(a1) )
{
printf(" [dirty]");
}
else if ( !(unsigned int)verify_chunk(a1, *(_QWORD *)(a1 + 0x10000)) )
{
printf(" [ERR]");
}
v7 = *(const void **)(a1 + 65584);
v8 = *(unsigned int *)(a1 + 65576);
if ( a3 )
{
printf(" [%s] refcount=%d %p\n", "internal", v8, v7);
v10 = 0LL;
v11 = a3 - 1;
do
{
dump_cnode(*(_QWORD *)(*(_QWORD *)(a1 + 65584) + v10), a2 - 1, v11, a4);
v10 += 8LL;
}
while ( v10 != 26208 );
}
else
{
printf(" [%s] refcount=%d %p\n", "leaf", v8, v7);
if ( a4 )
{
v9 = *(_QWORD *)(a1 + 65584);
if ( v9 )
a4(v9, a2);
}
}
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, const char *a2, int a3, void (__fastcall *a4)(__int64, const char *))
{
const char *v6; // rax
const void *v7; // rcx
__int64 v8; // rdx
__int64 v9; // rdi
__int64 v10; // rbx
unsigned int v11; // er13
__int64 v12; // [rsp-Eh] [rbp-78h] BYREF

if ( a1 )
{
v6 = (const char *)_digest_string(*(_QWORD *)(a1 + 0x10000), &v12);
printf("%s%p:%p: %s", a2, (const void *)a1, *(const void **)(a1 + 0x10000), v6);
if ( func1(a1) )
{
printf(" [dirty]");
}
else if ( !(unsigned int)func2(a1, *(_QWORD *)(a1 + 0x10000)) )
{
printf(" [ERR]");
}
v7 = *(const void **)(a1 + 65584);
v8 = *(unsigned int *)(a1 + 65576);
if ( a3 )
{
printf(" [%s] refcount=%d %p\n", "internal", v8, v7);
v10 = 0LL;
v11 = a3 - 1;
do
{
func3(*(_QWORD *)(*(_QWORD *)(a1 + 65584) + v10), a2 - 1, v11, a4);
v10 += 8LL;
}
while ( v10 != 26208 );
}
else
{
printf(" [%s] refcount=%d %p\n", "leaf", v8, v7);
if ( a4 )
{
v9 = *(_QWORD *)(a1 + 65584);
if ( v9 )
a4(v9, a2);
}
}
}
}

original_code:
void dump_cnode(struct chunk_node *cnode, const char *indent, int height,
  void (*dump_leaf)(void **, const char *))
{
 int i;
 if (!cnode)
  return;
 printf("%s%p:%p: %s", indent, cnode, cnode->chunk_digest,
   __digest_string(cnode->chunk_digest, __builtin_alloca ((20 * 2) + 1)));
 if (is_cnode_dirty(cnode))
  printf(" [dirty]");
 else if (!verify_chunk(cnode->chunk_data, cnode->chunk_digest))
  printf(" [ERR]");
 printf(" [%s] refcount=%d %p\n", height ? "internal" : "leaf",
   cnode->ref_count, cnode->_private);
 if (!height) {
  if (dump_leaf && cnode->_private)
   dump_leaf(cnode->_private, indent);
 } else {
  for (i = 0; i < ((1UL << 16) / 20); i ++) {
   dump_cnode(((struct chunk_node **)(cnode)->_private)[i], indent - 1,
     height - 1, dump_leaf);
  }
 }
}

canonical_original_code:
void dump_cnode(struct chunk_node *cnode, char *indent, int height, void (*dump_leaf)(void **, char *))
{
 int i;
 if (!cnode)
  return;
 printf("%s%p:%p: %s", indent, cnode, cnode->chunk_digest,
   __digest_string(cnode->chunk_digest, __builtin_alloca ((20 * 2) + 1)));
 if (is_cnode_dirty(cnode))
  printf(" [dirty]");
 else if (!verify_chunk(cnode->chunk_data, cnode->chunk_digest))
  printf(" [ERR]");
 printf(" [%s] refcount=%d %p\n", height ? "internal" : "leaf",
   cnode->ref_count, cnode->_private);
 if (!height) {
  if (dump_leaf && cnode->_private)
   dump_leaf(cnode->_private, indent);
 } else {
  for (i = 0; i < ((1UL << 16) / 20); i ++) {
   dump_cnode(((struct chunk_node **)(cnode)->_private)[i], indent - 1,
     height - 1, dump_leaf);
  }
 }
}

variable_types:
indent
char *

variable_types: height int

variable_types: i int


decompiled_code:
void __fastcall dump_ctree(__int64 *a1, const char *a2, void (__fastcall *a3)(__int64, const char *))
{
__int64 v5; // rdi

printf("%sCTREE %p nr_leafs=%d height=%d\n", a2, a1, *((unsigned int *)a1 + 2), *((unsigned int *)a1 + 3));
v5 = *a1;
if ( v5 )
dump_cnode(v5, a2 - 1, *((_DWORD *)a1 + 3), a3);
}

canonical_decompiled_code:
void __fastcall func4(__int64 *a1, const char *a2, void (__fastcall *a3)(__int64, const char *))
{
__int64 v5; // rdi

printf("%sCTREE %p nr_leafs=%d height=%d\n", a2, a1, *((unsigned int *)a1 + 2), *((unsigned int *)a1 + 3));
v5 = *a1;
if ( v5 )
func3(v5, a2 - 1, *((_DWORD *)a1 + 3), a3);
}

original_code:
void dump_ctree(struct chunk_tree *ctree, const char *indent,
  void (*dump_leaf)(void **child, const char *indent))
{
 printf("%sCTREE %p nr_leafs=%d height=%d\n", indent, ctree,
   ctree->nr_leafs, ctree->height);
 if (ctree->root)
  dump_cnode(ctree->root, indent - 1, ctree->height, dump_leaf);
}

canonical_original_code:
void dump_ctree(struct chunk_tree *ctree, char *indent, void (*dump_leaf)(void **child, char *indent))
{
 printf("%sCTREE %p nr_leafs=%d height=%d\n", indent, ctree,
   ctree->nr_leafs, ctree->height);
 if (ctree->root)
  dump_cnode(ctree->root, indent - 1, ctree->height, dump_leaf);
}

variable_types: struct chunk_tree { struct chunk_node { unsigned char chunk_data[-1]; unsigned char *chunk_digest; struct chunk_node *parent; struct chunk_tree *ctree; struct list_head { struct list_head *prev; } dirty_entry; unsigned int ref_count; void *_private; } *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 dirty_list; } *


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


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

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

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

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

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 **


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

if ( a1 )
{
v2 = (_DWORD)a1[21] & 0xF000;
v3 = "dir";
if ( v2 != 0x4000 )
{
v3 = "reg";
if ( v2 != 0x8000 )
v3 = "???";
}
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],
v3);
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)
{
int v2; // eax
const char *v3; // rdx
int v4; // eax
char *v5; // rax

if ( a1 )
{
v2 = (_DWORD)a1[21] & 0xF000;
v3 = "dir";
if ( v2 != 0x4000 )
{
v3 = "reg";
if ( v2 != 0x8000 )
v3 = "???";
}
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],
v3);
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


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 *


decompiled_code:
bool __fastcall local_write_chunk(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // edx
bool result; // al
int v7; // ebx
signed int v8; // er12
int *v9; // rbx
int v10; // er12
int *v11; // rbx
int v12; // er12
char *v13; // rax
char *v14; // rax
int v15; // eax
int *v16; // r13
int v17; // er15
__int64 v18; // [rsp+0h] [rbp-90h] BYREF
__int64 v19; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

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

canonical_decompiled_code:
bool __fastcall func0(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // edx
bool result; // al
int v7; // ebx
signed int v8; // er12
int *v9; // rbx
int v10; // er12
int *v11; // rbx
int v12; // er12
char *v13; // rax
char *v14; // rax
int v15; // eax
int *v16; // r13
int v17; // er15
__int64 v18; // [rsp+0h] [rbp-90h] BYREF
__int64 v19; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

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

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: len 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; // edx
__int64 result; // rax
int v7; // ebx
signed int v8; // er12
int *v9; // rbx
int v10; // er12
int *v11; // rbx
int v12; // er12
char *v13; // rax
char *v14; // rax
int v15; // eax
int *v16; // r13
int v17; // er15
__int64 v18; // [rsp+0h] [rbp-90h] BYREF
__int64 v19; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, const char *a3)
{
const char *v4; // rax
int v5; // edx
__int64 result; // rax
int v7; // ebx
signed int v8; // er12
int *v9; // rbx
int v10; // er12
int *v11; // rbx
int v12; // er12
char *v13; // rax
char *v14; // rax
int v15; // eax
int *v16; // r13
int v17; // er15
__int64 v18; // [rsp+0h] [rbp-90h] BYREF
__int64 v19; // [rsp+48h] [rbp-48h]
char *ptr; // [rsp+58h] [rbp-38h] BYREF

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

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; // r12
int v4; // er13
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
struct stat64 v9; // [rsp+0h] [rbp-B8h] BYREF

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

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

if ( zunkfs_log_fp && zunkfs_log_level > 1 )
{
v3 = _errno_location();
v4 = *v3;
_zprintf(2LL, "local_chunkdb_ctor", 103LL, "mode=0x%x spec=%s\n", *(unsigned int *)(a2 + 8), name);
*v3 = v4;
}
if ( stat64(name, &v9) == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
result = sprintf_new("Can't stat %s: %s.", name, v6);
}
else if ( (v9.st_mode & 0xF000) == 0x4000 )
{
if ( access(name, (2 * (unsigned __int8)*(_DWORD *)(a2 + 8)) & 2 | 4) )
{
v7 = _errno_location();
v8 = strerror(*v7);
result = sprintf_new("%s.", v8);
}
else
{
*(_QWORD *)(a2 + 16) = name;
result = 0LL;
}
}
else
{
result = sprintf_new("%s is not a directory.", name);
}
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(char **a1)
{
char *v1; // rax
char v2; // dl
char *v3; // rax

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

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

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

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


decompiled_code:
__int64 __fastcall set_logging(char *filename)
{
char *v1; // rax
unsigned int v2; // edx
char v4; // cl
unsigned int v5; // edx

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

canonical_decompiled_code:
__int64 __fastcall func2(char *filename)
{
char *v1; // rax
unsigned int v2; // edx
char v4; // cl
unsigned int v5; // edx

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

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:
int __fastcall dup_log_fd(int fd2)
{
int result; // eax
int v2; // eax

result = 0;
if ( zunkfs_log_fp )
{
v2 = fileno(zunkfs_log_fp);
result = dup2(v2, fd2);
if ( result )
result = -*_errno_location();
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int fd2)
{
int result; // eax
int v2; // eax

result = 0;
if ( zunkfs_log_fp )
{
v2 = fileno(zunkfs_log_fp);
result = dup2(v2, fd2);
if ( result )
result = -*_errno_location();
}
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


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 *func6()
{
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 *


strnlen

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

result = 0LL;
if ( !a2 )
return 0LL;
do
{
if ( !*(_BYTE *)(a1 + result) )
break;
++result;
}
while ( a2 != result );
return result;
}

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

result = 0LL;
if ( !a2 )
return 0LL;
do
{
if ( !*(_BYTE *)(a1 + result) )
break;
++result;
}
while ( a2 != result );
return result;
}

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

if ( !a1 )
return 0LL;
LODWORD(result) = 0;
do
{
result = (unsigned int)(result + 1);
a1 >>= 1;
}
while ( a1 );
return result;
}

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

if ( !a1 )
return 0LL;
LODWORD(result) = 0;
do
{
result = (unsigned int)(result + 1);
a1 >>= 1;
}
while ( a1 );
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 func9()
{
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 func10()
{
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; // rbx
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);
if ( !v2 )
return (char *)(int)_PTR_ERR(12, "sprintf_new", 0xD9u);
va_end(arg);
va_start(arg, format);
vsprintf(v2, format, arg);
return v2;
}

canonical_decompiled_code:
char *func13(char *format, ...)
{
int v1; // eax
char *v2; // rbx
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);
if ( !v2 )
return (char *)(int)func5(12, "sprintf_new", 0xD9u);
va_end(arg);
va_start(arg, format);
vsprintf(v2, format, arg);
return v2;
}

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