decompiled_code:
const char *test_rotate_clear()
{
char v0; // dl
const char *result; // rax
char *v2; // rax
_BYTE v3[36]; // [rsp+0h] [rbp-38h] BYREF
char v4; // [rsp+24h] [rbp-14h] BYREF

v0 = rotate(0xFFFFFFFFLL, v3);
result = "test_rotate_clear";
if ( v0 )
{
v2 = v3;
while ( !*(_DWORD *)v2 )
{
v2 += 4;
if ( v2 == &v4 )
return 0LL;
}
result = "test_rotate_clear";
}
return result;
}

canonical_decompiled_code:
const char *func21()
{
char v0; // dl
const char *result; // rax
char *v2; // rax
_BYTE v3[36]; // [rsp+0h] [rbp-38h] BYREF
char v4; // [rsp+24h] [rbp-14h] BYREF

v0 = rotate(0xFFFFFFFFLL, v3);
result = "test_rotate_clear";
if ( v0 )
{
v2 = v3;
while ( !*(_DWORD *)v2 )
{
v2 += 4;
if ( v2 == &v4 )
return 0LL;
}
result = "test_rotate_clear";
}
return result;
}

original_code:
static
const_char_p
test_rotate_clear(void)
{
    int buf[9];
    int i;
    _Bool ret;
    ret = rotate(-1, buf);
    if(!ret){
        return __func__;
    }
    for(i=0; i < 9; i++){
        if (buf[i] != 0){
            return __func__;
        }
    }
    return ((void *)0);
}

canonical_original_code:
static char *test_rotate_clear()
{
    int buf[9];
    int i;
    _Bool ret;
    ret = rotate(-1, buf);
    if(!ret){
        return __func__;
    }
    for(i=0; i < 9; i++){
        if (buf[i] != 0){
            return __func__;
        }
    }
    return ((void *)0);
}


main

canonical_name: main

repo: bgnori/solverForPuzzleA

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 (__fastcall **v3)(); // rbp
const char **v4; // rbx

v3 = &cases;
do
{
v4 = (const char **)((__int64 (__fastcall *)(_QWORD, const char **, const char **))*v3)(
*(_QWORD *)&argc,
argv,
envp);
if ( v4 )
{
puts("!");
argv = v4;
*(_QWORD *)&argc = "fail, %s\n";
printf("fail, %s\n", (const char *)v4);
}
else
{
*(_QWORD *)&argc = 46LL;
putchar(46);
}
++v3;
}
while ( v3 != (__int64 (__fastcall **)())(unsigned int)((_DWORD)&cases + 160) );
return 0;
}

original_code:
int
main(int argc, const char** argv)
{
    int i, len;
    const_char_p msg;
    unittest u;
    len = sizeof(cases)/sizeof(unittest);
    for(i = 0; i < len; i++){
        u = cases[i];
        msg = u();
        if(msg){
            printf("!\n");
            printf("fail, %s\n", msg);
        }else{
            printf(".");
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i; int len;
    char *msg;
    char *(*u)();
    len = sizeof(cases)/sizeof(unittest);
    for(i = 0; i < len; i++){
        u = cases[i];
        msg = u();
        if(msg){
            printf("!\n");
            printf("fail, %s\n", msg);
        }else{
            printf(".");
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: len int

variable_types:
msg
char *

return_type: int


decompiled_code:
__int64 __fastcall idx2h(int a1)
{
return (unsigned int)(a1 % 3);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
return (unsigned int)(a1 % 3);
}

original_code:
int
idx2h(int idx)
{
    return idx % 3;
}

canonical_original_code:
int idx2h(int idx)
{
    return idx % 3;
}

variable_types: idx int


decompiled_code:
__int64 __fastcall idx2v(int a1)
{
return (unsigned int)(a1 / 3);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
return (unsigned int)(a1 / 3);
}

original_code:
int
idx2v(int idx)
{
    return idx / 3;
}

canonical_original_code:
int idx2v(int idx)
{
    return idx / 3;
}


decompiled_code:
__int64 __fastcall hv2idx(int a1, int a2)
{
return (unsigned int)(a1 + 3 * a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2)
{
return (unsigned int)(a1 + 3 * a2);
}

original_code:
int
hv2idx(int h, int v)
{
    return h + v*3;
}

variable_types: h int

variable_types: v int


perfhash

original_code:
void
perfhash(int n, int* buf)
{
    int i;
    int nth;
    int availables[10] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int found;
    for(i = 0; i < 9; i++){
        nth = n / fact[8 - i];
        found = -1;
        while(nth > -1){
            found++;
            if(availables[found]){
                nth--;
            }
        }
        availables[found] = 0;
        buf[i] = found - 1;
        n = n % fact[8-i];
    }
}

canonical_original_code:
void perfhash(int n, int *buf)
{
    int i;
    int nth;
    int availables[10] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int found;
    for(i = 0; i < 9; i++){
        nth = n / fact[8 - i];
        found = -1;
        while(nth > -1){
            found++;
            if(availables[found]){
                nth--;
            }
        }
        availables[found] = 0;
        buf[i] = found - 1;
        n = n % fact[8-i];
    }
}

variable_types: n int

variable_types:
buf
int *

variable_types: nth int

variable_types:
availables
int [10]

variable_types: found int

return_type: void


rotate

original_code:
_Bool
rotate(int idx, int* buf)
{
    int i;
    if(idx == -1){
        for(i=0;i<9;i++){
            buf[i] = 0;
        }
        return 1;
    }
    if(buf[idx] >= 3){
        return 0;
    }
    buf[idx] ++;
    return 1;
}

canonical_original_code:
_Bool rotate(int idx, int *buf)
{
    int i;
    if(idx == -1){
        for(i=0;i<9;i++){
            buf[i] = 0;
        }
        return 1;
    }
    if(buf[idx] >= 3){
        return 0;
    }
    buf[idx] ++;
    return 1;
}


decompiled_code:
__int64 __fastcall get_pattern(int a1, __int64 a2, __int64 a3, int a4)
{
return *(unsigned int *)(16LL * *(int *)(a3 + 4LL * a1) + a2 + 4LL * a4);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, __int64 a2, __int64 a3, int a4)
{
return *(unsigned int *)(16LL * *(int *)(a3 + 4LL * a1) + a2 + 4LL * a4);
}

original_code:
int
get_pattern(int idx, TTile* tiles, TPlacement* p, int direction)
{
    return (tiles[p->fPermutation[idx]]).fPattern[direction];
}

canonical_original_code:
int get_pattern(int idx, struct TTile *tiles, struct TPlacement *p, int direction)
{
    return (tiles[p->fPermutation[idx]]).fPattern[direction];
}

variable_types:
tiles
struct TTile { int fPattern[4]; } *

variable_types: struct TPlacement { int fPermutation[9]; int fRotation[9]; } *

variable_types: direction int


decompiled_code:
bool __fastcall ok_up(int a1, __int64 a2, __int64 a3)
{
bool result; // al
int v5; // er13
unsigned int v6; // eax
unsigned int v7; // eax

result = 1;
if ( a1 > 2 )
{
v5 = idx2h();
v6 = idx2v((unsigned int)a1);
v7 = hv2idx((unsigned int)(v5 - 1), v6);
LOBYTE(v5) = get_pattern(v7, a2, a3, 2LL);
result = (~((unsigned __int8)get_pattern((unsigned int)a1, a2, a3, 0LL) ^ (unsigned __int8)v5) & 0xF) != 0;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func10(int a1, __int64 a2, __int64 a3)
{
bool result; // al
int v5; // er13
unsigned int v6; // eax
unsigned int v7; // eax

result = 1;
if ( a1 > 2 )
{
v5 = func4();
v6 = func5((unsigned int)a1);
v7 = func6((unsigned int)(v5 - 1), v6);
LOBYTE(v5) = func9(v7, a2, a3, 2LL);
result = (~((unsigned __int8)func9((unsigned int)a1, a2, a3, 0LL) ^ (unsigned __int8)v5) & 0xF) != 0;
}
return result;
}

original_code:
_Bool
ok_up(int idx, TTile* tiles, TPlacement* p)
{
    int h, v;
    int n;
    if(idx < 3)
        return 1;
    h = idx2h(idx);
    v = idx2v(idx);
    n = hv2idx(h - 1, v);
    return ((get_pattern(n, tiles, p, 2) ^ get_pattern(idx, tiles, p, 0) ^ (8+4+2+1)) & (8+4+2+1));
}

canonical_original_code:
_Bool ok_up(int idx, struct TTile *tiles, struct TPlacement *p)
{
    int h; int v;
    int n;
    if(idx < 3)
        return 1;
    h = idx2h(idx);
    v = idx2v(idx);
    n = hv2idx(h - 1, v);
    return ((get_pattern(n, tiles, p, 2) ^ get_pattern(idx, tiles, p, 0) ^ (8+4+2+1)) & (8+4+2+1));
}

user_defined_types: struct TPlacement { int fPermutation[9]; int fRotation[9]; }


decompiled_code:
bool __fastcall ok_left(unsigned int a1, __int64 a2, __int64 a3)
{
int v4; // ebx
bool result; // al
unsigned int v6; // eax
unsigned int v7; // eax
unsigned __int8 v8; // bl

v4 = idx2h();
result = 1;
if ( v4 )
{
v6 = idx2v(a1);
v7 = hv2idx((unsigned int)(v4 - 1), v6);
v8 = get_pattern(v7, a2, a3, 3LL);
result = (~((unsigned __int8)get_pattern(a1, a2, a3, 1LL) ^ v8) & 0xF) != 0;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func11(unsigned int a1, __int64 a2, __int64 a3)
{
int v4; // ebx
bool result; // al
unsigned int v6; // eax
unsigned int v7; // eax
unsigned __int8 v8; // bl

v4 = func4();
result = 1;
if ( v4 )
{
v6 = func5(a1);
v7 = func6((unsigned int)(v4 - 1), v6);
v8 = func9(v7, a2, a3, 3LL);
result = (~((unsigned __int8)func9(a1, a2, a3, 1LL) ^ v8) & 0xF) != 0;
}
return result;
}

original_code:
_Bool
ok_left(int idx, TTile* tiles, TPlacement* p)
{
    int h, v;
    int n;
    h = idx2h(idx);
    if (h == 0)
        return 1;
    v = idx2v(idx);
    n = hv2idx(h - 1, v);
    return ((get_pattern(n, tiles, p, 3) ^ get_pattern(idx, tiles, p, 1) ^ (8+4+2+1)) & (8+4+2+1));
}

canonical_original_code:
_Bool ok_left(int idx, struct TTile *tiles, struct TPlacement *p)
{
    int h; int v;
    int n;
    h = idx2h(idx);
    if (h == 0)
        return 1;
    v = idx2v(idx);
    n = hv2idx(h - 1, v);
    return ((get_pattern(n, tiles, p, 3) ^ get_pattern(idx, tiles, p, 1) ^ (8+4+2+1)) & (8+4+2+1));
}


decompiled_code:
__int64 __fastcall failed_at(__int64 a1, __int64 a2)
{
unsigned int v2; // ebx

v2 = 0;
while ( (unsigned __int8)ok_up(v2, a1, a2) && (unsigned __int8)ok_left(v2, a1, a2) )
{
if ( ++v2 == 9 )
return (unsigned int)-1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2)
{
unsigned int v2; // ebx

v2 = 0;
while ( (unsigned __int8)func10(v2, a1, a2) && (unsigned __int8)func11(v2, a1, a2) )
{
if ( ++v2 == 9 )
return (unsigned int)-1;
}
return v2;
}

original_code:
int
failed_at(TTile* tiles, TPlacement* p)
{
    int idx;
    for (idx=0; idx < 9; idx++){
        if(!ok_up(idx, tiles, p) ||
           !ok_left(idx, tiles, p)){
            return idx;
        }
    }
    return -1;
}

canonical_original_code:
int failed_at(struct TTile *tiles, struct TPlacement *p)
{
    int idx;
    for (idx=0; idx < 9; idx++){
        if(!ok_up(idx, tiles, p) ||
           !ok_left(idx, tiles, p)){
            return idx;
        }
    }
    return -1;
}


solve

decompiled_code:
__int64 __fastcall solve(__int64 a1, __int64 a2, unsigned int *a3, int a4)
{
unsigned int v6; // er14
__int64 v7; // rdi
unsigned int v8; // ebx
int v9; // eax

v6 = *a3;
if ( (int)*a3 < a4 )
{
do
{
perfhash(v6, a2);
v7 = 0xFFFFFFFFLL;
while ( 1 )
{
v8 = rotate(v7, a2 + 36);
if ( !(_BYTE)v8 )
break;
v9 = failed_at(a1, a2);
v7 = (unsigned int)v9;
if ( v9 < 0 )
{
*a3 = v6;
return v8;
}
}
++v6;
}
while ( a4 != v6 );
}
*a3 = v6;
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 a2, unsigned int *a3, int a4)
{
unsigned int v6; // er14
__int64 v7; // rdi
unsigned int v8; // ebx
int v9; // eax

v6 = *a3;
if ( (int)*a3 < a4 )
{
do
{
func7(v6, a2);
v7 = 0xFFFFFFFFLL;
while ( 1 )
{
v8 = func8(v7, a2 + 36);
if ( !(_BYTE)v8 )
break;
v9 = func12(a1, a2);
v7 = (unsigned int)v9;
if ( v9 < 0 )
{
*a3 = v6;
return v8;
}
}
++v6;
}
while ( a4 != v6 );
}
*a3 = v6;
return 0;
}

original_code:
_Bool
solve(TTile* tiles, TPlacement* p, int* start, int end)
{
    int i;
    int at;
    for(i = *start; i < end; i++){
        perfhash(i, p->fPermutation);
        at = -1;
        while(rotate(at, p->fRotation)){
            at = failed_at(tiles, p);
            if (at < 0){
                *start = i;
                return 1;
            }
        }
    }
    *start = i;
    return 0;
}

canonical_original_code:
_Bool solve(struct TTile *tiles, struct TPlacement *p, int *start, int end)
{
    int i;
    int at;
    for(i = *start; i < end; i++){
        perfhash(i, p->fPermutation);
        at = -1;
        while(rotate(at, p->fRotation)){
            at = failed_at(tiles, p);
            if (at < 0){
                *start = i;
                return 1;
            }
        }
    }
    *start = i;
    return 0;
}

variable_types:
start
int *

variable_types: end int

variable_types: at int


repo: bharanis/algorithms

original_code:
int min_delete (int *heap)
{
   int pos = 1;
   int selected;
   int retval = heap[1];
   int temp;
   ((heap)[0]) --;
   heap[1] = heap[((heap)[0])];
   while (((pos)*2) < ((heap)[0])) {
     selected = pos;
     if (heap[((pos)*2)] < heap[selected]) {
        selected = ((pos)*2);
     }
     if (heap[((pos)*2 + 1)] < heap[selected]) {
        selected = ((pos)*2 + 1);
     }
     if (selected != pos) {
        temp = heap[pos];
        heap[pos] = heap[selected];
        heap[selected] = temp;
        pos = selected;
     }
     else
        break;
   }
   return retval;
}

canonical_original_code:
int min_delete(int *heap)
{
   int pos = 1;
   int selected;
   int retval = heap[1];
   int temp;
   ((heap)[0]) --;
   heap[1] = heap[((heap)[0])];
   while (((pos)*2) < ((heap)[0])) {
     selected = pos;
     if (heap[((pos)*2)] < heap[selected]) {
        selected = ((pos)*2);
     }
     if (heap[((pos)*2 + 1)] < heap[selected]) {
        selected = ((pos)*2 + 1);
     }
     if (selected != pos) {
        temp = heap[pos];
        heap[pos] = heap[selected];
        heap[selected] = temp;
        pos = selected;
     }
     else
        break;
   }
   return retval;
}

variable_types:
heap
int *

variable_types: pos int

variable_types: selected int

variable_types: retval int

variable_types: temp int


original_code:
int max_delete (int *heap)
{
   int pos = 1;
   int selected;
   int retval = heap[1];
   int temp;
   ((heap)[0]) --;
   heap[1] = heap[((heap)[0])];
   while (((pos)*2) < ((heap)[0])) {
     selected = pos;
     if (heap[((pos)*2)] > heap[selected]) {
        selected = ((pos)*2);
     }
     if (heap[((pos)*2 + 1)] > heap[selected]) {
        selected = ((pos)*2 + 1);
     }
     if (selected != pos) {
        temp = heap[pos];
        heap[pos] = heap[selected];
        heap[selected] = temp;
        pos = selected;
     }
     else
        break;
   }
   return retval;
}

canonical_original_code:
int max_delete(int *heap)
{
   int pos = 1;
   int selected;
   int retval = heap[1];
   int temp;
   ((heap)[0]) --;
   heap[1] = heap[((heap)[0])];
   while (((pos)*2) < ((heap)[0])) {
     selected = pos;
     if (heap[((pos)*2)] > heap[selected]) {
        selected = ((pos)*2);
     }
     if (heap[((pos)*2 + 1)] > heap[selected]) {
        selected = ((pos)*2 + 1);
     }
     if (selected != pos) {
        temp = heap[pos];
        heap[pos] = heap[selected];
        heap[selected] = temp;
        pos = selected;
     }
     else
        break;
   }
   return retval;
}


decompiled_code:
__int64 __fastcall populate_array(_DWORD *a1, int a2)
{
int v2; // er12
_DWORD *v3; // rbx

v2 = random() % (a2 + a2 / -10) + a2 / 10;
if ( v2 > 0 )
{
v3 = a1;
do
*v3++ = random() % (10 * v2);
while ( v3 != &a1[v2 - 1 + 1] );
}
return (unsigned int)v2;
}

original_code:
int populate_array (int *arr, int size)
{
   int i;
   size = (random() % (size-size/10)) + size/10;
   for (i = 0; i < size; i ++) {
      arr[i] = random() % (size*10);
   }
   return size;
}

canonical_original_code:
int populate_array(int *arr, int size)
{
   int i;
   size = (random() % (size-size/10)) + size/10;
   for (i = 0; i < size; i ++) {
      arr[i] = random() % (size*10);
   }
   return size;
}

variable_types:
arr
int *

variable_types: size int

variable_types: i int


decompiled_code:
int __fastcall print_array(const char *a1, unsigned int *a2, int a3)
{
int result; // eax
unsigned int *v5; // rbx
__int64 v6; // rbp

result = printf("%s", a1);
if ( a3 > 0 )
{
v5 = a2;
v6 = (__int64)&a2[a3 - 1 + 1];
do
result = printf("%d ", *v5++);
while ( v5 != (unsigned int *)v6 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(const char *a1, unsigned int *a2, int a3)
{
int result; // eax
unsigned int *v5; // rbx
__int64 v6; // rbp

result = printf("%s", a1);
if ( a3 > 0 )
{
v5 = a2;
v6 = (__int64)&a2[a3 - 1 + 1];
do
result = printf("%d ", *v5++);
while ( v5 != (unsigned int *)v6 );
}
return result;
}

original_code:
void print_array(char *header, int *a, int s)
{
  int i;
  printf("%s", header);
  for (i = 0; i < s; i++)
     printf("%d ", a[i]);
}

variable_types:
header
char *

variable_types:
a
int *

variable_types: s int


insert

decompiled_code:
__int64 __fastcall insert(__int64 a1, __int64 a2)
{
int v2; // eax
int *v3; // rcx
int v4; // edx

v2 = 1;
do
{
v3 = (int *)(a1 + 4LL * v2);
v4 = *v3;
if ( *v3 == -1 )
{
*v3 = a2;
return 0LL;
}
if ( v4 <= (int)a2 )
{
if ( v4 >= (int)a2 )
{
printf("insert %d duplicate\n", a2);
return 0LL;
}
v2 = 2 * v2 + 1;
}
else
{
v2 *= 2;
}
}
while ( v2 <= 1024 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
int v2; // eax
int *v3; // rcx
int v4; // edx

v2 = 1;
do
{
v3 = (int *)(a1 + 4LL * v2);
v4 = *v3;
if ( *v3 == -1 )
{
*v3 = a2;
return 0LL;
}
if ( v4 <= (int)a2 )
{
if ( v4 >= (int)a2 )
{
printf("insert %d duplicate\n", a2);
return 0LL;
}
v2 = 2 * v2 + 1;
}
else
{
v2 *= 2;
}
}
while ( v2 <= 1024 );
return 0xFFFFFFFFLL;
}

original_code:
int insert (int *array, int key)
{
   int root = 1;
   while (array[root] != -1) {
     if (array[root] > key) {
        root = ((root)*2);
     } else if (array[root] < key) {
        root = ((root)*2 + 1);
     } else {
       printf ("insert %d duplicate\n", key);
       return 0;
     }
     if(root > (1<<10)) return -1;;
   }
   array[root] = key;
   return 0;
}

canonical_original_code:
int insert(int *array, int key)
{
   int root = 1;
   while (array[root] != -1) {
     if (array[root] > key) {
        root = ((root)*2);
     } else if (array[root] < key) {
        root = ((root)*2 + 1);
     } else {
       printf ("insert %d duplicate\n", key);
       return 0;
     }
     if(root > (1<<10)) return -1;;
   }
   array[root] = key;
   return 0;
}

variable_types:
array
int *

variable_types: key int

variable_types: root int


bfs

original_code:
void bfs (bnode *root, void (*fn) (bnode *, void *ctx), void *ctx)
{
   int c = count(root);
   bnode **q = malloc (sizeof (bnode *) * c );
   int qidx = 0, pidx = 0, i;
   q[qidx++] = root;
   while (pidx < c) {
     if (((void *)0) != q[pidx]->left) {
       q[qidx++] = q[pidx]->left;
     }
     if (((void *)0) != q[pidx]->right) {
       q[qidx++] = q[pidx]->right;
     }
     pidx ++;
   }
   for (i=0; i<c; i++)
      fn(q[i], ctx);
   free(q);
}

canonical_original_code:
void bfs(struct node *root, void (*fn)(struct node *, void *ctx), void *ctx)
{
   int c = count(root);
   struct node **q = malloc (sizeof (bnode *) * c );
   int qidx = 0; int pidx = 0; int i;
   q[qidx++] = root;
   while (pidx < c) {
     if (((void *)0) != q[pidx]->left) {
       q[qidx++] = q[pidx]->left;
     }
     if (((void *)0) != q[pidx]->right) {
       q[qidx++] = q[pidx]->right;
     }
     pidx ++;
   }
   for (i=0; i<c; i++)
      fn(q[i], ctx);
   free(q);
}

variable_types:
fn
void (*)(struct node *, void *ctx)

variable_types:
ctx
void *

variable_types: c int

variable_types: struct node { struct node *left; struct node *right; int key; int x; int y; int modifier; int height; } **

variable_types: qidx int

variable_types: pidx int


inorder

decompiled_code:
__int64 __fastcall inorder(__int64 a1, int a2, void (__fastcall *a3)(__int64, _QWORD))
{
if ( a2 > 1024 )
return 0LL;
if ( *(_DWORD *)(a1 + 4LL * a2) == -1 )
return 0LL;
inorder(a1, (unsigned int)(2 * a2));
a3(a1, (unsigned int)a2);
return inorder(a1, (unsigned int)(2 * a2 + 1));
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, int a2, void (__fastcall *a3)(__int64, _QWORD))
{
if ( a2 > 1024 )
return 0LL;
if ( *(_DWORD *)(a1 + 4LL * a2) == -1 )
return 0LL;
func18(a1, (unsigned int)(2 * a2));
a3(a1, (unsigned int)a2);
return func18(a1, (unsigned int)(2 * a2 + 1));
}

original_code:
void inorder(bnode *root, void (*fn) (bnode*, void*), void *ctx)
{
    if (!root) return;
    inorder(root->left, fn, ctx);
    fn(root, ctx);
    inorder(root->right, fn, ctx);
}

canonical_original_code:
void inorder(struct node *root, void (*fn)(struct node *, void *), void *ctx)
{
    if (!root) return;
    inorder(root->left, fn, ctx);
    fn(root, ctx);
    inorder(root->right, fn, ctx);
}

variable_types: struct node { struct node *left; struct node *right; int key; int x; int y; int modifier; int height; } *

variable_types:
fn
void (*)(struct node *, void *)


postorder

decompiled_code:
__int64 __fastcall postorder(__int64 a1, int a2, __int64 (__fastcall *a3)(__int64, _QWORD))
{
if ( a2 > 1024 )
return 0LL;
if ( *(_DWORD *)(a1 + 4LL * a2) == -1 )
return 0LL;
postorder(a1, (unsigned int)(2 * a2));
postorder(a1, (unsigned int)(2 * a2 + 1));
return a3(a1, (unsigned int)a2);
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, int a2, __int64 (__fastcall *a3)(__int64, _QWORD))
{
if ( a2 > 1024 )
return 0LL;
if ( *(_DWORD *)(a1 + 4LL * a2) == -1 )
return 0LL;
func19(a1, (unsigned int)(2 * a2));
func19(a1, (unsigned int)(2 * a2 + 1));
return a3(a1, (unsigned int)a2);
}

original_code:
void postorder(bnode *root, void (*fn) (bnode*, void*), void *ctx)
{
    if (!root) return;
    postorder(root->left, fn, ctx);
    postorder(root->right, fn, ctx);
    fn(root, ctx);
}

canonical_original_code:
void postorder(struct node *root, void (*fn)(struct node *, void *), void *ctx)
{
    if (!root) return;
    postorder(root->left, fn, ctx);
    postorder(root->right, fn, ctx);
    fn(root, ctx);
}

user_defined_types: struct node { struct node *left; struct node *right; int key; int x; int y; int modifier; int height; }


preorder

decompiled_code:
__int64 __fastcall preorder(__int64 a1, int a2, void (*a3)(void))
{
if ( a2 > 1024 )
return 0LL;
if ( *(_DWORD *)(a1 + 4LL * a2) == -1 )
return 0LL;
a3();
preorder(a1, (unsigned int)(2 * a2), a3);
return preorder(a1, (unsigned int)(2 * a2 + 1), a3);
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, int a2, void (*a3)(void))
{
if ( a2 > 1024 )
return 0LL;
if ( *(_DWORD *)(a1 + 4LL * a2) == -1 )
return 0LL;
a3();
func20(a1, (unsigned int)(2 * a2), a3);
return func20(a1, (unsigned int)(2 * a2 + 1), a3);
}

original_code:
void preorder(bnode *root, void (*fn) (bnode*, void*), void *ctx)
{
    if (!root) return;
    fn(root, ctx);
    preorder(root->left, fn, ctx);
    preorder(root->right, fn, ctx);
}

canonical_original_code:
void preorder(struct node *root, void (*fn)(struct node *, void *), void *ctx)
{
    if (!root) return;
    fn(root, ctx);
    preorder(root->left, fn, ctx);
    preorder(root->right, fn, ctx);
}


decompiled_code:
__int64 __fastcall print_tree(__int64 a1)
{
printf("inorder: ");
return inorder(a1, 1, (void (__fastcall *)(__int64, _QWORD))printnode);
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1)
{
printf("inorder: ");
return func18(a1, 1, (void (__fastcall *)(__int64, _QWORD))printnode);
}

original_code:
void print_tree(bnode *root)
{
   print_ws(root);
   printf ("\n");
}

canonical_original_code:
void print_tree(struct node *root)
{
   print_ws(root);
   printf ("\n");
}


largest

decompiled_code:
__int64 __fastcall largest(__int64 a1, unsigned int a2)
{
__int64 result; // rax
int v3; // eax

result = a2;
if ( (int)(2 * a2) <= 1023 )
{
while ( 1 )
{
v3 = 2 * a2;
if ( *(_DWORD *)(a1 + 4LL * (int)(2 * a2) + 4) == -1 )
break;
a2 = v3 + 1;
if ( v3 + 1 > 1024 )
return (unsigned int)((int)a2 / 2);
}
result = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, unsigned int a2)
{
__int64 result; // rax
int v3; // eax

result = a2;
if ( (int)(2 * a2) <= 1023 )
{
while ( 1 )
{
v3 = 2 * a2;
if ( *(_DWORD *)(a1 + 4LL * (int)(2 * a2) + 4) == -1 )
break;
a2 = v3 + 1;
if ( v3 + 1 > 1024 )
return (unsigned int)((int)a2 / 2);
}
result = a2;
}
return result;
}

original_code:
int largest(int *array, int root)
{
   if(((root)*2 + 1) > (1<<10)) return root;;
   while (array[((root)*2 + 1)] != -1) {
     root = ((root)*2 + 1);
     if(root > (1<<10)) return ((int)(root)/2);;
   }
   return (root);
}


swap

decompiled_code:
int *__fastcall swap(__int64 a1, int a2, int a3)
{
int *v3; // rcx
int v4; // esi
int *result; // rax

v3 = (int *)(a1 + 4LL * a2);
v4 = *v3;
result = (int *)(a1 + 4LL * a3);
*v3 = *result;
*result = v4;
return result;
}

canonical_decompiled_code:
int *__fastcall func7(__int64 a1, int a2, int a3)
{
int *v3; // rcx
int v4; // esi
int *result; // rax

v3 = (int *)(a1 + 4LL * a2);
v4 = *v3;
result = (int *)(a1 + 4LL * a3);
*v3 = *result;
*result = v4;
return result;
}

original_code:
void swap(int *arr, int i, int j)
{
  int t = arr[i];
  arr[i] = arr[j];
  arr[j] = t;
}

variable_types:
arr
int *

variable_types: j int

variable_types: t int


partition

decompiled_code:
__int64 __fastcall partition(__int64 a1, int a2, int a3, int a4)
{
unsigned int v4; // ebp
int v6; // er13
__int64 v7; // rbx

v4 = a2;
v6 = *(_DWORD *)(a1 + 4LL * a4);
swap(a1, a4, a3);
if ( a2 < a3 )
{
v7 = a2;
do
{
if ( *(_DWORD *)(a1 + 4 * v7) < v6 )
swap(a1, v7, v4++);
++v7;
}
while ( v7 != a2 + (unsigned __int64)(unsigned int)(a3 - 1 - a2) + 1 );
}
swap(a1, a3, v4);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2, int a3, int a4)
{
unsigned int v4; // ebp
int v6; // er13
__int64 v7; // rbx

v4 = a2;
v6 = *(_DWORD *)(a1 + 4LL * a4);
func7(a1, a4, a3);
if ( a2 < a3 )
{
v7 = a2;
do
{
if ( *(_DWORD *)(a1 + 4 * v7) < v6 )
func7(a1, v7, v4++);
++v7;
}
while ( v7 != a2 + (unsigned __int64)(unsigned int)(a3 - 1 - a2) + 1 );
}
func7(a1, a3, v4);
return v4;
}

original_code:
int partition (int *arr, int left, int right, int pivotidx)
{
   int pivot = arr[pivotidx];
   int storeidx = left;
   int i;
   swap (arr, pivotidx, right);
   for (i = left; i <= right-1; i++) {
      if (arr[i] < pivot) {
        swap(arr, i, storeidx);
        storeidx ++;
      }
   }
   swap (arr, right, storeidx);
   return storeidx;
}

canonical_original_code:
int partition(int *arr, int left, int right, int pivotidx)
{
   int pivot = arr[pivotidx];
   int storeidx = left;
   int i;
   swap (arr, pivotidx, right);
   for (i = left; i <= right-1; i++) {
      if (arr[i] < pivot) {
        swap(arr, i, storeidx);
        storeidx ++;
      }
   }
   swap (arr, right, storeidx);
   return storeidx;
}

variable_types: left int

variable_types: right int

variable_types: pivotidx int

variable_types: pivot int

variable_types: storeidx int

variable_types: i int


decompiled_code:
int __fastcall print_array(const char *a1, unsigned int *a2, int a3)
{
unsigned int *v4; // rbx
__int64 v5; // rbp

puts(a1);
if ( a3 > 0 )
{
v4 = a2;
v5 = (__int64)&a2[a3 - 1 + 1];
do
printf("%d ", *v4++);
while ( v4 != (unsigned int *)v5 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1, unsigned int *a2, int a3)
{
unsigned int *v4; // rbx
__int64 v5; // rbp

puts(a1);
if ( a3 > 0 )
{
v4 = a2;
v5 = (__int64)&a2[a3 - 1 + 1];
do
printf("%d ", *v4++);
while ( v4 != (unsigned int *)v5 );
}
return putchar(10);
}

original_code:
void print_array(char *header, int *a, int s)
{
  int i;
  printf("%s\n", header);
  for (i = 0; i < s; i++)
     printf("%d ", a[i]);
  printf("\n");
}

variable_types:
header
char *

variable_types:
a
int *

variable_types: s int


decompiled_code:
void *__fastcall copy_data_one_direction(void *a1)
{
char *v1; // rbp
int v2; // eax
int v3; // er12

v1 = (char *)a1 + 12;
while ( 1 )
{
v2 = read(*(_DWORD *)a1, v1, 0x2000uLL);
v3 = v2;
if ( v2 <= 0 )
break;
if ( write(*((_DWORD *)a1 + 1), v1, v2) < 0 )
{
*((_DWORD *)a1 + 2) = 1;
perror("write");
return 0LL;
}
}
shutdown(*((_DWORD *)a1 + 1), 1);
if ( v3 < 0 )
perror("read");
if ( *(_DWORD *)a1 )
fwrite("[received EOF]\n", 1uLL, 0xFuLL, stderr);
else
fwrite("[sent EOF]\n", 1uLL, 0xBuLL, stderr);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
char *v1; // rbp
int v2; // eax
int v3; // er12

v1 = (char *)a1 + 12;
while ( 1 )
{
v2 = read(*(_DWORD *)a1, v1, 0x2000uLL);
v3 = v2;
if ( v2 <= 0 )
break;
if ( write(*((_DWORD *)a1 + 1), v1, v2) < 0 )
{
*((_DWORD *)a1 + 2) = 1;
perror("write");
return 0LL;
}
}
shutdown(*((_DWORD *)a1 + 1), 1);
if ( v3 < 0 )
perror("read");
if ( *(_DWORD *)a1 )
fwrite("[received EOF]\n", 1uLL, 0xFuLL, stderr);
else
fwrite("[sent EOF]\n", 1uLL, 0xBuLL, stderr);
return 0LL;
}

original_code:
void *
copy_data_one_direction (void *_st)
{
  struct copy_state *st = _st;
  int n;
  while ((n = read (st->in, st->buf, sizeof (st->buf))) > 0)
    if (write (st->out, st->buf, n) < 0) {
      st->error = 1;
      perror ("write");
      return ((void *)0);
    }
  shutdown (st->out, SHUT_WR);
  if (n < 0)
    perror ("read");
  if (st->in)
    fprintf (stderr, "[received EOF]\n");
  else
    fprintf (stderr, "[sent EOF]\n");
  return ((void *)0);
}

canonical_original_code:
void *copy_data_one_direction(void *_st)
{
  struct copy_state *st = _st;
  int n;
  while ((n = read (st->in, st->buf, sizeof (st->buf))) > 0)
    if (write (st->out, st->buf, n) < 0) {
      st->error = 1;
      perror ("write");
      return ((void *)0);
    }
  shutdown (st->out, SHUT_WR);
  if (n < 0)
    perror ("read");
  if (st->in)
    fprintf (stderr, "[received EOF]\n");
  else
    fprintf (stderr, "[sent EOF]\n");
  return ((void *)0);
}

variable_types: struct copy_state { int in; int out; int error; char buf[8192]; } *

variable_types: n int

return_type: void *


sock

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

result = socket(a1, 1, 0);
if ( result < 0 )
{
perror("socket");
exit(1);
}
return result;
}

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

result = socket(a1, 1, 0);
if ( result < 0 )
{
perror("socket");
exit(1);
}
return result;
}

original_code:
static int
sock (int family)
{
  int s = socket (family, SOCK_STREAM, 0);
  if (s < 0) {
    perror ("socket");
    exit (1);
  }
  return s;
}

canonical_original_code:
static int sock(int family)
{
  int s = socket (family, SOCK_STREAM, 0);
  if (s < 0) {
    perror ("socket");
    exit (1);
  }
  return s;
}

variable_types: family int


decompiled_code:
int __fastcall copy_data(int a1)
{
int v2[2051]; // [rsp+0h] [rbp-4028h] BYREF
int arg[2051]; // [rsp+200Ch] [rbp-201Ch] BYREF
pthread_t newthread[2]; // [rsp+4018h] [rbp-10h] BYREF

v2[0] = a1;
v2[1] = 1;
v2[2] = 0;
arg[0] = 0;
arg[1] = a1;
arg[2] = 0;
pthread_create(newthread, 0LL, copy_data_one_direction, arg);
copy_data_one_direction(v2);
return pthread_join(newthread[0], 0LL);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
int v2[2051]; // [rsp+0h] [rbp-4028h] BYREF
int arg[2051]; // [rsp+200Ch] [rbp-201Ch] BYREF
pthread_t newthread[2]; // [rsp+4018h] [rbp-10h] BYREF

v2[0] = a1;
v2[1] = 1;
v2[2] = 0;
arg[0] = 0;
arg[1] = a1;
arg[2] = 0;
pthread_create(newthread, 0LL, copy_data_one_direction, arg);
func0(v2);
return pthread_join(newthread[0], 0LL);
}

original_code:
static void
copy_data (int s)
{
  pthread_t to_s;
  struct copy_state st[2];
  st[0].in = s;
  st[0].out = 1;
  st[0].error = 0;
  st[1].in = 0;
  st[1].out = s;
  st[1].error = 0;
  pthread_create (&to_s, ((void *)0), copy_data_one_direction, &st[1]);
  copy_data_one_direction (&st[0]);
  pthread_join (to_s, ((void *)0));
}

canonical_original_code:
static void copy_data(int s)
{
  unsigned long to_s;
  struct copy_state st[2];
  st[0].in = s;
  st[0].out = 1;
  st[0].error = 0;
  st[1].in = 0;
  st[1].out = s;
  st[1].error = 0;
  pthread_create (&to_s, ((void *)0), copy_data_one_direction, &st[1]);
  copy_data_one_direction (&st[0]);
  pthread_join (to_s, ((void *)0));
}

variable_types: struct copy_state { int in; int out; int error; char buf[8192]; } [2]


decompiled_code:
int __fastcall do_listen(struct sockaddr *addr, socklen_t a2)
{
int v2; // eax
int v3; // ebx
char *v4; // r12
int v5; // ebp
int v7; // eax
socklen_t len; // [rsp+Ch] [rbp-ACh] BYREF
struct sockaddr addra[8]; // [rsp+10h] [rbp-A8h] BYREF
int optval[7]; // [rsp+9Ch] [rbp-1Ch] BYREF

len = a2;
v2 = sock(addr->sa_family);
v3 = v2;
if ( addr->sa_family != 1 )
{
optval[0] = 1;
setsockopt(v2, 1, 2, optval, 4u);
if ( bind(v3, addr, len) >= 0 )
{
v4 = 0LL;
goto LABEL_4;
}
v4 = "bind";
goto LABEL_11;
}
v4 = addr->sa_data;
optval[0] = 1;
setsockopt(v2, 1, 2, optval, 4u);
if ( bind(v3, addr, len) < 0 )
{
if ( *_errno_location() == 98 )
{
v7 = strlen(addr->sa_data);
fprintf(
stderr,
"%s: already exists; you should delete this socket if you are\n%*s not running another instance of %s.\n",
addr->sa_data,
v7,
(const char *)&unk_73B,
(const char *)progname);
LABEL_12:
exit(1);
}
LABEL_11:
perror(v4);
goto LABEL_12;
}
LABEL_4:
if ( listen(v3, 1) < 0 )
{
perror("listen");
exit(1);
}
memset(addra, 0, sizeof(addra));
len = 128;
v5 = accept(v3, addra, &len);
if ( v5 < 0 )
{
perror("accept");
exit(1);
}
if ( v4 )
unlink(v4);
fwrite("[accepted connection]\n", 1uLL, 0x16uLL, stderr);
close(v3);
return copy_data(v5);
}

canonical_decompiled_code:
int __fastcall func4(struct sockaddr *addr, socklen_t a2)
{
int v2; // eax
int v3; // ebx
char *v4; // r12
int v5; // ebp
int v7; // eax
socklen_t len; // [rsp+Ch] [rbp-ACh] BYREF
struct sockaddr addra[8]; // [rsp+10h] [rbp-A8h] BYREF
int optval[7]; // [rsp+9Ch] [rbp-1Ch] BYREF

len = a2;
v2 = func2(addr->sa_family);
v3 = v2;
if ( addr->sa_family != 1 )
{
optval[0] = 1;
setsockopt(v2, 1, 2, optval, 4u);
if ( bind(v3, addr, len) >= 0 )
{
v4 = 0LL;
goto LABEL_4;
}
v4 = "bind";
goto LABEL_11;
}
v4 = addr->sa_data;
optval[0] = 1;
setsockopt(v2, 1, 2, optval, 4u);
if ( bind(v3, addr, len) < 0 )
{
if ( *_errno_location() == 98 )
{
v7 = strlen(addr->sa_data);
fprintf(
stderr,
"%s: already exists; you should delete this socket if you are\n%*s not running another instance of %s.\n",
addr->sa_data,
v7,
(const char *)&unk_73B,
(const char *)progname);
LABEL_12:
exit(1);
}
LABEL_11:
perror(v4);
goto LABEL_12;
}
LABEL_4:
if ( listen(v3, 1) < 0 )
{
perror("listen");
exit(1);
}
memset(addra, 0, sizeof(addra));
len = 128;
v5 = accept(v3, addra, &len);
if ( v5 < 0 )
{
perror("accept");
exit(1);
}
if ( v4 )
unlink(v4);
fwrite("[accepted connection]\n", 1uLL, 0x16uLL, stderr);
close(v3);
return func3(v5);
}

original_code:
static void
do_listen (const struct sockaddr *sap, socklen_t len)
{
  int sl = sock (sap->sa_family);
  int r, s, n;
  const char *path = sap->sa_family == 1
    ? ((struct sockaddr_un *) sap)->sun_path : ((void *)0);
  struct sockaddr_storage garbage;
  n = 1;
  setsockopt (sl, 1, 2, (char *) &n, sizeof (n));
  r = bind (sl, sap, len);
  if (r < 0) {
    if ((*__errno_location ()) == 98 && path)
      fprintf (stderr, "%s: already exists; you should delete this socket"
        " if you are\n%*s  not running another instance of %s.\n",
        path, (int) strlen (path), "", progname);
    else
      perror (path ? path : "bind");
    exit (1);
  }
  if (listen (sl, 1) < 0) {
    perror ("listen");
    exit (1);
  }
  memset (&garbage, 0, sizeof (garbage));
  len = sizeof (garbage);
  s = accept (sl, (struct sockaddr *) &garbage, &len);
  if (s < 0) {
    perror ("accept");
    exit (1);
  }
  if (path)
    unlink (path);
  fprintf (stderr, "[accepted connection]\n");
  close (sl);
  copy_data (s);
}

canonical_original_code:
static void do_listen(struct sockaddr *sap, unsigned int len)
{
  int sl = sock (sap->sa_family);
  int r; int s; int n;
  char *path = sap->sa_family == 1
    ? ((struct sockaddr_un *) sap)->sun_path : ((void *)0);
  struct sockaddr_storage garbage;
  n = 1;
  setsockopt (sl, 1, 2, (char *) &n, sizeof (n));
  r = bind (sl, sap, len);
  if (r < 0) {
    if ((*__errno_location ()) == 98 && path)
      fprintf (stderr, "%s: already exists; you should delete this socket"
        " if you are\n%*s  not running another instance of %s.\n",
        path, (int) strlen (path), "", progname);
    else
      perror (path ? path : "bind");
    exit (1);
  }
  if (listen (sl, 1) < 0) {
    perror ("listen");
    exit (1);
  }
  memset (&garbage, 0, sizeof (garbage));
  len = sizeof (garbage);
  s = accept (sl, (struct sockaddr *) &garbage, &len);
  if (s < 0) {
    perror ("accept");
    exit (1);
  }
  if (path)
    unlink (path);
  fprintf (stderr, "[accepted connection]\n");
  close (sl);
  copy_data (s);
}

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

variable_types:
len
unsigned int

variable_types: sl int

variable_types: r int

variable_types:
path
char *

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


decompiled_code:
int __fastcall do_connect(struct sockaddr *addr, socklen_t len)
{
int v2; // ebp

v2 = sock(addr->sa_family);
if ( connect(v2, addr, len) < 0 )
{
perror("connect");
exit(1);
}
fwrite("[established connection]\n", 1uLL, 0x19uLL, stderr);
return copy_data(v2);
}

canonical_decompiled_code:
int __fastcall func5(struct sockaddr *addr, socklen_t len)
{
int v2; // ebp

v2 = func2(addr->sa_family);
if ( connect(v2, addr, len) < 0 )
{
perror("connect");
exit(1);
}
fwrite("[established connection]\n", 1uLL, 0x19uLL, stderr);
return func3(v2);
}

original_code:
static void
do_connect (const struct sockaddr *sap, socklen_t len)
{
  int s = sock (sap->sa_family);
  int r = connect (s, sap, len);
  if (r < 0) {
    perror ("connect");
    exit (1);
  }
  fprintf (stderr, "[established connection]\n");
  copy_data (s);
}

canonical_original_code:
static void do_connect(struct sockaddr *sap, unsigned int len)
{
  int s = sock (sap->sa_family);
  int r = connect (s, sap, len);
  if (r < 0) {
    perror ("connect");
    exit (1);
  }
  fprintf (stderr, "[established connection]\n");
  copy_data (s);
}

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


decompiled_code:
__int64 __fastcall get_address(_QWORD *a1, const char *a2, const char *a3)
{
int v4; // eax
struct addrinfo *v5; // rdx
socklen_t v6; // ecx
sockaddr *v7; // rsi
const char *v8; // rax
int v10; // eax
const char *v11; // rax
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( a2 )
{
v4 = getaddrinfo(a2, a3, &req, &pai);
if ( v4 )
{
v8 = gai_strerror(v4);
fprintf(stderr, "%s:%s: %s\n", a2, a3, v8);
return 0xFFFFFFFFLL;
}
}
else
{
req.ai_flags = 1;
v10 = getaddrinfo(0LL, a3, &req, &pai);
if ( v10 )
{
v11 = gai_strerror(v10);
fprintf(stderr, "local port %s: %s\n", a3, v11);
return 0xFFFFFFFFLL;
}
}
v5 = pai;
v6 = pai->ai_addrlen;
if ( v6 > 0x80 )
_assert_fail("ai->ai_addrlen <= sizeof (*sa)", "uc.c", 0xA4u, "get_address");
v7 = pai->ai_addr;
if ( v6 >= 8 )
{
*a1 = *(_QWORD *)&v7->sa_family;
*(_QWORD *)((char *)a1 + v6 - 8) = *(_QWORD *)((char *)v7 + v6 - 8);
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v7 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((_DWORD)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + v6) >> 3));
}
else if ( (v6 & 4) != 0 )
{
*(_DWORD *)a1 = *(_DWORD *)&v7->sa_family;
*(_DWORD *)((char *)a1 + v6 - 4) = *(_DWORD *)((char *)v7 + v6 - 4);
}
else if ( v6 )
{
*(_BYTE *)a1 = v7->sa_family;
if ( (v6 & 2) != 0 )
*(_WORD *)((char *)a1 + v6 - 2) = *(_WORD *)((char *)v7 + v6 - 2);
}
freeaddrinfo(v5);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, const char *a2, const char *a3)
{
int v4; // eax
struct addrinfo *v5; // rdx
socklen_t v6; // ecx
sockaddr *v7; // rsi
const char *v8; // rax
int v10; // eax
const char *v11; // rax
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( a2 )
{
v4 = getaddrinfo(a2, a3, &req, &pai);
if ( v4 )
{
v8 = gai_strerror(v4);
fprintf(stderr, "%s:%s: %s\n", a2, a3, v8);
return 0xFFFFFFFFLL;
}
}
else
{
req.ai_flags = 1;
v10 = getaddrinfo(0LL, a3, &req, &pai);
if ( v10 )
{
v11 = gai_strerror(v10);
fprintf(stderr, "local port %s: %s\n", a3, v11);
return 0xFFFFFFFFLL;
}
}
v5 = pai;
v6 = pai->ai_addrlen;
if ( v6 > 0x80 )
_assert_fail("ai->ai_addrlen <= sizeof (*sa)", "uc.c", 0xA4u, "get_address");
v7 = pai->ai_addr;
if ( v6 >= 8 )
{
*a1 = *(_QWORD *)&v7->sa_family;
*(_QWORD *)((char *)a1 + v6 - 8) = *(_QWORD *)((char *)v7 + v6 - 8);
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v7 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((_DWORD)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + v6) >> 3));
}
else if ( (v6 & 4) != 0 )
{
*(_DWORD *)a1 = *(_DWORD *)&v7->sa_family;
*(_DWORD *)((char *)a1 + v6 - 4) = *(_DWORD *)((char *)v7 + v6 - 4);
}
else if ( v6 )
{
*(_BYTE *)a1 = v7->sa_family;
if ( (v6 & 2) != 0 )
*(_WORD *)((char *)a1 + v6 - 2) = *(_WORD *)((char *)v7 + v6 - 2);
}
freeaddrinfo(v5);
return 0LL;
}

original_code:
int
get_address (struct sockaddr_storage *sa, char *host, char *port)
{
  struct addrinfo hints;
  struct addrinfo *ai;
  int err;
  memset (&hints, 0, sizeof (hints));
  hints.ai_family = 0;
  hints.ai_socktype = SOCK_STREAM;
  if (host) {
    err = getaddrinfo (host, port, &hints, &ai);
    if (err) {
      fprintf (stderr, "%s:%s: %s\n", host, port,
        gai_strerror (err));
      return -1;
    }
  }
  else {
    hints.ai_flags = 0x0001;
    err = getaddrinfo (((void *)0), port, &hints, &ai);
    if (err) {
      fprintf (stderr, "local port %s: %s\n", port, gai_strerror (err));
      return -1;
    }
  }
  ((void) sizeof ((ai->ai_addrlen <= sizeof (*sa)) ? 1 : 0), __extension__ ({ if (ai->ai_addrlen <= sizeof (*sa)) ; else __assert_fail ("ai->ai_addrlen <= sizeof (*sa)", "uc.c", 164, __extension__ __PRETTY_FUNCTION__); }));
  memcpy (sa, ai->ai_addr, ai->ai_addrlen);
  freeaddrinfo (ai);
  return 0;
}

canonical_original_code:
int get_address(struct sockaddr_storage *sa, char *host, char *port)
{
  struct addrinfo hints;
  struct addrinfo *ai;
  int err;
  memset (&hints, 0, sizeof (hints));
  hints.ai_family = 0;
  hints.ai_socktype = SOCK_STREAM;
  if (host) {
    err = getaddrinfo (host, port, &hints, &ai);
    if (err) {
      fprintf (stderr, "%s:%s: %s\n", host, port,
        gai_strerror (err));
      return -1;
    }
  }
  else {
    hints.ai_flags = 0x0001;
    err = getaddrinfo (((void *)0), port, &hints, &ai);
    if (err) {
      fprintf (stderr, "local port %s: %s\n", port, gai_strerror (err));
      return -1;
    }
  }
  ((void) sizeof ((ai->ai_addrlen <= sizeof (*sa)) ? 1 : 0), __extension__ ({ if (ai->ai_addrlen <= sizeof (*sa)) ; else __assert_fail ("ai->ai_addrlen <= sizeof (*sa)", "uc.c", 164, __extension__ __PRETTY_FUNCTION__); }));
  memcpy (sa, ai->ai_addr, ai->ai_addrlen);
  freeaddrinfo (ai);
  return 0;
}

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *

variable_types:
host
char *

variable_types:
port
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: err int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
const char *v4; // rax
int v5; // er13
int v6; // er14
int v7; // eax
const char *v8; // rbp
struct sockaddr *v9; // rdi
socklen_t v10; // esi
int v12; // eax
_QWORD v13[16]; // [rsp+0h] [rbp-118h] BYREF
struct sockaddr addr; // [rsp+80h] [rbp-98h] BYREF

v3 = strrchr(*argv, 47);
progname = (__int64)v3;
if ( v3 )
v4 = v3 + 1;
else
v4 = *argv;
progname = (__int64)v4;
v5 = 0;
v6 = 0;
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "lu");
if ( v7 == -1 )
break;
if ( v7 == 108 )
{
v6 = 1;
}
else
{
if ( v7 != 117 )
usage();
v5 = 1;
}
}
if ( v5 )
{
if ( optind + 1 != argc )
usage();
v8 = argv[optind];
if ( strlen(v8) > 0x6B )
{
fprintf(stderr, "socket name exceeds maximum of %d characters\n", 107LL);
exit(1);
}
memset(&addr, 0, 0x6EuLL);
addr.sa_family = 1;
strcpy(addr.sa_data, v8);
v9 = &addr;
v10 = 110;
}
else
{
if ( optind + 1 == argc )
{
v12 = get_address(v13, 0LL, argv[optind]);
}
else
{
if ( optind + 2 != argc )
usage();
v12 = get_address(v13, argv[optind], argv[optind + 1]);
}
if ( v12 < 0 )
exit(1);
v9 = (struct sockaddr *)v13;
if ( LOWORD(v13[0]) == 2 )
v10 = 16;
else
v10 = 28;
}
if ( v6 )
do_listen(v9, v10);
else
do_connect(v9, v10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
const char *v4; // rax
int v5; // er13
int v6; // er14
int v7; // eax
const char *v8; // rbp
struct sockaddr *v9; // rdi
socklen_t v10; // esi
int v12; // eax
_QWORD v13[16]; // [rsp+0h] [rbp-118h] BYREF
struct sockaddr addr; // [rsp+80h] [rbp-98h] BYREF

v3 = strrchr(*argv, 47);
progname = (__int64)v3;
if ( v3 )
v4 = v3 + 1;
else
v4 = *argv;
progname = (__int64)v4;
v5 = 0;
v6 = 0;
while ( 1 )
{
v7 = getopt(argc, (char *const *)argv, "lu");
if ( v7 == -1 )
break;
if ( v7 == 108 )
{
v6 = 1;
}
else
{
if ( v7 != 117 )
func1();
v5 = 1;
}
}
if ( v5 )
{
if ( optind + 1 != argc )
func1();
v8 = argv[optind];
if ( strlen(v8) > 0x6B )
{
fprintf(stderr, "socket name exceeds maximum of %d characters\n", 107LL);
exit(1);
}
memset(&addr, 0, 0x6EuLL);
addr.sa_family = 1;
strcpy(addr.sa_data, v8);
v9 = &addr;
v10 = 110;
}
else
{
if ( optind + 1 == argc )
{
v12 = func6(v13, 0LL, argv[optind]);
}
else
{
if ( optind + 2 != argc )
func1();
v12 = func6(v13, argv[optind], argv[optind + 1]);
}
if ( v12 < 0 )
exit(1);
v9 = (struct sockaddr *)v13;
if ( LOWORD(v13[0]) == 2 )
v10 = 16;
else
v10 = 28;
}
if ( v6 )
func4(v9, v10);
else
func5(v9, v10);
return 0;
}

original_code:
int
main (int argc, char **argv)
{
  int opt_listen = 0;
  int opt_unixdomain = 0;
  int opt;
  socklen_t len;
  struct sockaddr_un sun;
  struct sockaddr_storage ss;
  struct sockaddr *sap;
  progname = strrchr (argv[0], '/');
  if (progname)
    progname++;
  else
    progname = argv[0];
  while ((opt = getopt (argc, argv, "lu")) != -1)
    switch (opt) {
    case 'l':
      opt_listen = 1;
      break;
    case 'u':
      opt_unixdomain = 1;
      break;
    default:
      usage ();
      break;
    }
  if (opt_unixdomain) {
    if (optind + 1 != argc)
      usage ();
    if (strlen (argv[optind]) >= sizeof (sun.sun_path)) {
      fprintf (stderr, "socket name exceeds maximum of %d characters\n",
        (int) sizeof (sun.sun_path) - 1);
      exit (1);
    }
    memset (&sun, 0, sizeof (sun));
    sun.sun_family = 1;
    strcpy (sun.sun_path, argv[optind]);
    len = sizeof (sun);
    sap = (struct sockaddr *) &sun;
  }
  else {
    int ret;
    if (optind + 1 == argc)
      ret = get_address (&ss, ((void *)0), argv[optind]);
    else if (optind + 2 == argc)
      ret = get_address (&ss, argv[optind], argv[optind+1]);
    else
      usage ();
    if (ret < 0)
      exit (1);
    sap = (struct sockaddr *) &ss;
    if (ss.ss_family == 2)
      len = sizeof (struct sockaddr_in);
    else
      len = sizeof (struct sockaddr_in6);
  }
  if (opt_listen)
    do_listen (sap, len);
  else
    do_connect (sap, len);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int opt_listen = 0;
  int opt_unixdomain = 0;
  int opt;
  unsigned int len;
  struct sockaddr_un sun;
  struct sockaddr_storage ss;
  struct sockaddr *sap;
  progname = strrchr (argv[0], '/');
  if (progname)
    progname++;
  else
    progname = argv[0];
  while ((opt = getopt (argc, argv, "lu")) != -1)
    switch (opt) {
    case 'l':
      opt_listen = 1;
      break;
    case 'u':
      opt_unixdomain = 1;
      break;
    default:
      usage ();
      break;
    }
  if (opt_unixdomain) {
    if (optind + 1 != argc)
      usage ();
    if (strlen (argv[optind]) >= sizeof (sun.sun_path)) {
      fprintf (stderr, "socket name exceeds maximum of %d characters\n",
        (int) sizeof (sun.sun_path) - 1);
      exit (1);
    }
    memset (&sun, 0, sizeof (sun));
    sun.sun_family = 1;
    strcpy (sun.sun_path, argv[optind]);
    len = sizeof (sun);
    sap = (struct sockaddr *) &sun;
  }
  else {
    int ret;
    if (optind + 1 == argc)
      ret = get_address (&ss, ((void *)0), argv[optind]);
    else if (optind + 2 == argc)
      ret = get_address (&ss, argv[optind], argv[optind+1]);
    else
      usage ();
    if (ret < 0)
      exit (1);
    sap = (struct sockaddr *) &ss;
    if (ss.ss_family == 2)
      len = sizeof (struct sockaddr_in);
    else
      len = sizeof (struct sockaddr_in6);
  }
  if (opt_listen)
    do_listen (sap, len);
  else
    do_connect (sap, len);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

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

variable_types: ret int

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }


usage

decompiled_code:
void __noreturn usage()
{
fprintf(
stderr,
"usage: %s { -u unix-socket | [host] tcp-port } (to connect)\n"
" %s -l { -u unix-socket | tcp-port } (to listen)\n",
(const char *)progname,
(const char *)progname);
exit(1);
}

canonical_decompiled_code:
void __noreturn func1()
{
fprintf(
stderr,
"usage: %s { -u unix-socket | [host] tcp-port } (to connect)\n"
" %s -l { -u unix-socket | tcp-port } (to listen)\n",
(const char *)progname,
(const char *)progname);
exit(1);
}

original_code:
static void
usage (void)
{
  fprintf (stderr, "usage: %s { -u unix-socket | [host] tcp-port }"
    "   (to connect)\n"
    "       %s -l { -u unix-socket | tcp-port }"
    "       (to listen)\n",
    progname, progname);
  exit (1);
}

canonical_original_code:
static void usage()
{
  fprintf (stderr, "usage: %s { -u unix-socket | [host] tcp-port }"
    "   (to connect)\n"
    "       %s -l { -u unix-socket | tcp-port }"
    "       (to listen)\n",
    progname, progname);
  exit (1);
}


repo: bhdn/utp-iia-gain

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

v2 = *(_QWORD *)(a1 + 8);
if ( v2 )
{
v3 = *(_QWORD *)(a1 + 16);
v4 = 0LL;
while ( !*(_QWORD *)(v3 + 8 * v4) )
{
if ( ++v4 == v2 )
return v2;
}
if ( v2 > v4 )
{
*a2 = *(_QWORD *)(*(_QWORD *)(v3 + 8 * v4) + 16LL);
v2 = v4;
}
}
return v2;
}

original_code:
hash_iter_t hash_iter_first(struct hash_table *table, void **dataptr)
{
 struct hash_entry *found;
 hash_iter_t state;
 size_t first;
 for (first = 0;
      first < table->size && !table->entries[first];
      first++)
  ;
 if (first >= table->size) {
  state.i = table->size;
  state.current = ((void *)0);
 }
 else {
  found = table->entries[first];
  *dataptr = found->data;
  state.i = first;
  state.current = found;
 }
 return state;
}

canonical_original_code:
struct hash_iter_t_ hash_iter_first(struct hash_table *table, void **dataptr)
{
 struct hash_entry *found;
 struct hash_iter_t_ state;
 unsigned long first;
 for (first = 0;
      first < table->size && !table->entries[first];
      first++)
  ;
 if (first >= table->size) {
  state.i = table->size;
  state.current = ((void *)0);
 }
 else {
  found = table->entries[first];
  *dataptr = found->data;
  state.i = first;
  state.current = found;
 }
 return state;
}

variable_types: struct hash_table { unsigned long count; unsigned long size; struct hash_entry **entries; } *

variable_types:
dataptr
void **

variable_types: struct hash_iter_t_ { unsigned long i; struct hash_entry { unsigned long key_len; unsigned int hash; void *data; struct hash_entry *next; } *current; }

variable_types:
first
unsigned long

user_defined_types: struct hash_iter_t_ { unsigned long i; struct hash_entry *current; }


decompiled_code:
unsigned __int64 __fastcall hash_iter_next(__int64 a1, unsigned __int64 a2, __int64 a3, _QWORD *a4)
{
unsigned __int64 result; // rax
__int64 v7; // rcx
unsigned __int64 v8; // rcx
__int64 v9; // rsi

result = a2;
v7 = *(_QWORD *)(a3 + 24);
if ( v7 )
goto LABEL_2;
result = a2 + 1;
v8 = *(_QWORD *)(a1 + 8);
if ( a2 + 1 < v8 )
{
v9 = *(_QWORD *)(a1 + 16);
while ( !*(_QWORD *)(v9 + 8 * result) )
{
if ( ++result >= v8 )
return result;
}
if ( v8 > result )
{
v7 = *(_QWORD *)(v9 + 8 * result);
if ( v7 )
LABEL_2:
*a4 = *(_QWORD *)(v7 + 16);
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(__int64 a1, unsigned __int64 a2, __int64 a3, _QWORD *a4)
{
unsigned __int64 result; // rax
__int64 v7; // rcx
unsigned __int64 v8; // rcx
__int64 v9; // rsi

result = a2;
v7 = *(_QWORD *)(a3 + 24);
if ( v7 )
goto LABEL_2;
result = a2 + 1;
v8 = *(_QWORD *)(a1 + 8);
if ( a2 + 1 < v8 )
{
v9 = *(_QWORD *)(a1 + 16);
while ( !*(_QWORD *)(v9 + 8 * result) )
{
if ( ++result >= v8 )
return result;
}
if ( v8 > result )
{
v7 = *(_QWORD *)(v9 + 8 * result);
if ( v7 )
LABEL_2:
*a4 = *(_QWORD *)(v7 + 16);
}
}
return result;
}

original_code:
hash_iter_t hash_iter_next(struct hash_table *table, hash_iter_t last,
  void **dataptr)
{
 struct hash_entry *found;
 found = last.current->next;
 if (!found) {
  for (last.i++;
       last.i < table->size && !table->entries[last.i] ;
       last.i++)
   ;
  if (last.i < table->size)
   found = table->entries[last.i];
 }
 if (found) {
  *dataptr = found->data;
  last.current = found;
 }
 return last;
}

canonical_original_code:
struct hash_iter_t_ hash_iter_next(struct hash_table *table, struct hash_iter_t_ last, void **dataptr)
{
 struct hash_entry *found;
 found = last.current->next;
 if (!found) {
  for (last.i++;
       last.i < table->size && !table->entries[last.i] ;
       last.i++)
   ;
  if (last.i < table->size)
   found = table->entries[last.i];
 }
 if (found) {
  *dataptr = found->data;
  last.current = found;
 }
 return last;
}

variable_types:
last
struct hash_iter_t_

return_type: struct hash_iter_t_ { unsigned long i; struct hash_entry { unsigned long key_len; unsigned int hash; void *data; struct hash_entry *next; } *current; }

user_defined_types: struct hash_table { unsigned long count; unsigned long size; struct hash_entry **entries; }


decompiled_code:
_BOOL8 __fastcall hash_iter_done(__int64 a1, unsigned __int64 a2)
{
return *(_QWORD *)(a1 + 8) > a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, unsigned __int64 a2)
{
return *(_QWORD *)(a1 + 8) > a2;
}

original_code:
int hash_iter_done(struct hash_table *table, hash_iter_t state)
{
 return state.i < table->size;
}

canonical_original_code:
int hash_iter_done(struct hash_table *table, struct hash_iter_t_ state)
{
 return state.i < table->size;
}

variable_types: struct hash_table { unsigned long count; unsigned long size; struct hash_entry { unsigned long key_len; unsigned int hash; void *data; struct hash_entry *next; } **entries; } *

variable_types: struct hash_iter_t_ { unsigned long i; struct hash_entry *current; }


decompiled_code:
void __fastcall hash_free(void *ptr)
{
unsigned __int64 v2; // rbx
void *v3; // rdi

if ( *((_QWORD *)ptr + 1) )
{
v2 = 0LL;
do
{
v3 = *(void **)(*((_QWORD *)ptr + 2) + 8 * v2);
if ( v3 )
hash_free_entry(v3);
++v2;
}
while ( *((_QWORD *)ptr + 1) > v2 );
}
free(*((void **)ptr + 2));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
unsigned __int64 v2; // rbx
void *v3; // rdi

if ( *((_QWORD *)ptr + 1) )
{
v2 = 0LL;
do
{
v3 = *(void **)(*((_QWORD *)ptr + 2) + 8 * v2);
if ( v3 )
func3(v3);
++v2;
}
while ( *((_QWORD *)ptr + 1) > v2 );
}
free(*((void **)ptr + 2));
free(ptr);
}

original_code:
void hash_free(struct hash_table *table)
{
 size_t i;
 for (i = 0; i < table->size; i++)
  if (table->entries[i])
   hash_free_entry(table->entries[i]);
 free(table->entries);
 free(table);
}

canonical_original_code:
void hash_free(struct hash_table *table)
{
 unsigned long i;
 for (i = 0; i < table->size; i++)
  if (table->entries[i])
   hash_free_entry(table->entries[i]);
 free(table->entries);
 free(table);
}

variable_types:
i
unsigned long

user_defined_types: struct hash_entry { unsigned long key_len; unsigned int hash; void *data; struct hash_entry *next; }


original_code:
unsigned int get_hash(unsigned char *key, size_t key_len)
{
    unsigned int hash = 0;
    size_t i;
    for (i = 0; i < key_len; i++){
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

canonical_original_code:
unsigned int get_hash(unsigned char *key, unsigned long key_len)
{
    unsigned int hash = 0;
    unsigned long i;
    for (i = 0; i < key_len; i++){
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

variable_types:
key
unsigned char *

variable_types:
hash
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall hash_get(__int64 a1, unsigned __int8 *a2, __int64 a3, unsigned int a4)
{
__int64 result; // rax

if ( !a4 )
a4 = get_hash(a2, a3);
result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * ((unsigned __int64)a4 % *(_QWORD *)(a1 + 8)));
if ( result )
{
if ( *(_QWORD *)(result + 24) )
{
while ( *(_DWORD *)(result + 8) != a4 || *(_QWORD *)result != a3 )
{
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 16);
}
else
{
result = *(_QWORD *)(result + 16);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int8 *a2, __int64 a3, unsigned int a4)
{
__int64 result; // rax

if ( !a4 )
a4 = func5(a2, a3);
result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * ((unsigned __int64)a4 % *(_QWORD *)(a1 + 8)));
if ( result )
{
if ( *(_QWORD *)(result + 24) )
{
while ( *(_DWORD *)(result + 8) != a4 || *(_QWORD *)result != a3 )
{
result = *(_QWORD *)(result + 24);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 16);
}
else
{
result = *(_QWORD *)(result + 16);
}
}
return result;
}

original_code:
void *hash_get(struct hash_table *table, unsigned char *key, size_t
  key_len, unsigned int force_hash)
{
 unsigned int hash;
 size_t pos;
 struct hash_entry *found;
 void *data = ((void *)0);
 hash = force_hash ? force_hash : get_hash(key, key_len);
 pos = hash % table->size;
 found = table->entries[pos];
 if (found) {
  if (found->next) {
   while (found) {
    if (found->hash == hash
     && found->key_len == key_len) {
     data = found->data;
     break;
    }
    found = found->next;
   }
  }
  else
   data = found->data;
 }
 return data;
}

canonical_original_code:
void *hash_get(struct hash_table *table, unsigned char *key, unsigned long key_len, unsigned int force_hash)
{
 unsigned int hash;
 unsigned long pos;
 struct hash_entry *found;
 void *data = ((void *)0);
 hash = force_hash ? force_hash : get_hash(key, key_len);
 pos = hash % table->size;
 found = table->entries[pos];
 if (found) {
  if (found->next) {
   while (found) {
    if (found->hash == hash
     && found->key_len == key_len) {
     data = found->data;
     break;
    }
    found = found->next;
   }
  }
  else
   data = found->data;
 }
 return data;
}

variable_types:
pos
unsigned long

variable_types: struct hash_entry { unsigned long key_len; unsigned int hash; void *data; struct hash_entry *next; } *

variable_types:
data
void *


decompiled_code:
struct passwd *__fastcall uid_for_username(const char *a1, _DWORD *a2)
{
struct passwd *result; // rax

result = getpwnam(a1);
if ( result )
return (struct passwd *)result->pw_uid;
*a2 = 1;
return result;
}

canonical_decompiled_code:
struct passwd *__fastcall func7(const char *a1, _DWORD *a2)
{
struct passwd *result; // rax

result = getpwnam(a1);
if ( result )
return (struct passwd *)result->pw_uid;
*a2 = 1;
return result;
}

original_code:
uid_t
uid_for_username(const char *name, int *err)
{
  struct passwd * passwd;
  passwd = getpwnam(name);
  if (passwd == ((void *)0)) {
    *err = 1;
    return 0;
  }
  return passwd->pw_uid;
}

canonical_original_code:
unsigned int uid_for_username(char *name, int *err)
{
  struct passwd *passwd;
  passwd = getpwnam(name);
  if (passwd == ((void *)0)) {
    *err = 1;
    return 0;
  }
  return passwd->pw_uid;
}

variable_types:
name
char *

variable_types:
err
int *

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__uid_t v4; // ebx
__pid_t v6; // eax
char *envpa; // [rsp+8h] [rbp-40h] BYREF
char *argva[3]; // [rsp+10h] [rbp-38h] BYREF
int v9[7]; // [rsp+2Ch] [rbp-1Ch] BYREF

v9[0] = -1;
if ( argc <= 1 )
{
fwrite("need username\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
v3 = argv[1];
v4 = (unsigned int)uid_for_username(v3, v9);
if ( v9[0] == 1 )
{
fwrite("username not found\n", 1uLL, 0x13uLL, stderr);
exit(1);
}
if ( !fork() )
{
v6 = read_pid_from_file(PID_FILE, v9);
if ( v9[0] > 1 )
{
fprintf(stderr, "could not read from file %s\n", PID_FILE);
exit(1);
}
if ( v9[0] == -1 )
{
kill(v6, 9);
if ( remove(PID_FILE) )
{
fprintf(stderr, "could not remove pid file %s\n", PID_FILE);
exit(1);
}
}
write_pid_to_file(PID_FILE, v9);
if ( v9[0] == 1 )
fprintf(stderr, "could not write pid to file %s\n", PID_FILE);
if ( setuid(v4) )
{
fprintf(stderr, "couldn't change uid to user %s\n", v3);
exit(1);
}
if ( access(KARAF, 1) == -1 )
{
fprintf(stderr, "can't find executable %s\n", KARAF);
exit(1);
}
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
argva[0] = "clean";
argva[1] = 0LL;
envpa = 0LL;
execve(KARAF, argva, &envpa);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__uid_t v4; // ebx
__pid_t v6; // eax
char *envpa; // [rsp+8h] [rbp-40h] BYREF
char *argva[3]; // [rsp+10h] [rbp-38h] BYREF
int v9[7]; // [rsp+2Ch] [rbp-1Ch] BYREF

v9[0] = -1;
if ( argc <= 1 )
{
fwrite("need username\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
v3 = argv[1];
v4 = (unsigned int)func7(v3, v9);
if ( v9[0] == 1 )
{
fwrite("username not found\n", 1uLL, 0x13uLL, stderr);
exit(1);
}
if ( !fork() )
{
v6 = func8(PID_FILE, v9);
if ( v9[0] > 1 )
{
fprintf(stderr, "could not read from file %s\n", PID_FILE);
exit(1);
}
if ( v9[0] == -1 )
{
kill(v6, 9);
if ( remove(PID_FILE) )
{
fprintf(stderr, "could not remove pid file %s\n", PID_FILE);
exit(1);
}
}
func9(PID_FILE, v9);
if ( v9[0] == 1 )
fprintf(stderr, "could not write pid to file %s\n", PID_FILE);
if ( setuid(v4) )
{
fprintf(stderr, "couldn't change uid to user %s\n", v3);
exit(1);
}
if ( access(KARAF, 1) == -1 )
{
fprintf(stderr, "can't find executable %s\n", KARAF);
exit(1);
}
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
argva[0] = "clean";
argva[1] = 0LL;
envpa = 0LL;
execve(KARAF, argva, &envpa);
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
  int err, retval;
  char * username;
  uid_t uid;
  pid_t running_process;
  err = -1;
  if (argc < 2) {
    fprintf(stderr, "need username\n");
    exit(1);
  }
  username = argv[1];
  uid = uid_for_username(username, &err);
  if (err == 1) {
    fprintf(stderr, "username not found\n");
    exit(1);
  }
  if (fork() == 0) {
    running_process = read_pid_from_file(PID_FILE, &err);
    if (err > 1) {
      fprintf(stderr, "could not read from file %s\n", PID_FILE);
      exit(1);
    } else if (err == -1) {
      kill(running_process, 9);
      if (remove(PID_FILE) != 0) {
        fprintf(stderr, "could not remove pid file %s\n", PID_FILE);
        exit(1);
      }
    }
    write_pid_to_file(PID_FILE, &err);
    if (err == 1) {
      fprintf(stderr, "could not write pid to file %s\n", PID_FILE);
    }
    retval = setuid(uid);
    if (retval != 0) {
      fprintf(stderr, "couldn't change uid to user %s\n", username);
      exit(1);
    }
    if (access(KARAF, 1) == -1) {
      fprintf(stderr, "can't find executable %s\n", KARAF);
      exit(1);
    }
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/dev/null", "w", stderr);
    char *args[] = {"clean", ((void *)0)};
    char *envp[] = {((void *)0)};
    execve(KARAF, args, envp);
  }
}

canonical_original_code:
int main(int argc, char **argv)
{
  int err; int retval;
  char *username;
  unsigned int uid;
  int running_process;
  err = -1;
  if (argc < 2) {
    fprintf(stderr, "need username\n");
    exit(1);
  }
  username = argv[1];
  uid = uid_for_username(username, &err);
  if (err == 1) {
    fprintf(stderr, "username not found\n");
    exit(1);
  }
  if (fork() == 0) {
    running_process = read_pid_from_file(PID_FILE, &err);
    if (err > 1) {
      fprintf(stderr, "could not read from file %s\n", PID_FILE);
      exit(1);
    } else if (err == -1) {
      kill(running_process, 9);
      if (remove(PID_FILE) != 0) {
        fprintf(stderr, "could not remove pid file %s\n", PID_FILE);
        exit(1);
      }
    }
    write_pid_to_file(PID_FILE, &err);
    if (err == 1) {
      fprintf(stderr, "could not write pid to file %s\n", PID_FILE);
    }
    retval = setuid(uid);
    if (retval != 0) {
      fprintf(stderr, "couldn't change uid to user %s\n", username);
      exit(1);
    }
    if (access(KARAF, 1) == -1) {
      fprintf(stderr, "can't find executable %s\n", KARAF);
      exit(1);
    }
    freopen( "/dev/null", "r", stdin);
    freopen( "/dev/null", "w", stdout);
    freopen( "/dev/null", "w", stderr);
    char *args[] = {"clean", ((void *)0)};
    char *envp[] = {((void *)0)};
    execve(KARAF, args, envp);
  }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: err int

variable_types: retval int

variable_types:
username
char *

variable_types:
uid
unsigned int

variable_types:
args
char *[]

variable_types:
envp
char *[]


repo: bietje/RBtree

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

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

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

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

original_code:
static inline struct rbtree *tree_parent(struct rbtree *node)
{
 if(node) {
  return node->parent;
 } else {
  return ((void *)0);
 }
}

variable_types:
node
struct rbtree *

return_type: struct rbtree { struct rbtree *left; struct rbtree *right; struct rbtree *parent; struct rbtree *root; int key; enum rbtree_color_t { RED=0, BLACK=1 } color; void *data; } *

user_defined_types: struct rbtree { struct rbtree *left; struct rbtree *right; struct rbtree *parent; struct rbtree *root; int key; enum rbtree_color_t color; void *data; }


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

result = tree_parent(a1);
if ( result )
result = tree_parent(result);
return result;
}

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

result = func0(a1);
if ( result )
result = func0(result);
return result;
}

original_code:
static inline struct rbtree *tree_grandparent(struct rbtree *node)
{
 struct rbtree *parent;
 if((parent = tree_parent(node)) != ((void *)0)) {
  return tree_parent(parent);
 } else {
  return ((void *)0);
 }
}

variable_types: struct rbtree { struct rbtree *left; struct rbtree *right; struct rbtree *parent; struct rbtree *root; int key; enum rbtree_color_t { RED=0, BLACK=1 } color; void *data; } *


decompiled_code:
__int64 __fastcall tree_node_has_sibling(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rdx

if ( !a1 )
return 0LL;
v1 = (__int64 *)tree_parent(a1);
if ( !v1 )
return 0LL;
v2 = *v1;
if ( *v1 == a1 )
v2 = v1[1];
return v2;
}

original_code:
static inline struct rbtree *tree_node_has_sibling(struct rbtree *node)
{
 struct rbtree *parent;
 if(node) {
  parent = tree_parent(node);
  if(parent) {
   if(node == parent->left) {
    return parent->right;
   } else {
    return parent->left;
   }
  }
 }
 return ((void *)0);
}


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

if ( !a1 )
return 0LL;
result = tree_node_has_sibling(a1);
if ( result )
{
v2 = tree_parent(a1);
if ( *(_QWORD *)v2 == a1 )
result = *(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL);
else
result = **(_QWORD **)v2;
}
return result;
}

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

if ( !a1 )
return 0LL;
result = func2(a1);
if ( result )
{
v2 = func0(a1);
if ( *(_QWORD *)v2 == a1 )
result = *(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL);
else
result = **(_QWORD **)v2;
}
return result;
}

original_code:
static inline struct rbtree *tree_node_far_nephew(struct rbtree *node)
{
 struct rbtree *parent;
 if(node && tree_node_has_sibling(node)) {
  parent = tree_parent(node);
  return (parent->left == node) ? parent->right->right : parent->left->left;
 }
 return ((void *)0);
}


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

v1 = *(_QWORD *)(a1 + 16);
result = 0LL;
if ( v1 )
result = *(_QWORD *)(v1 + 8) == a1;
return result;
}

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

v1 = *(_QWORD *)(a1 + 16);
result = 0LL;
if ( v1 )
result = *(_QWORD *)(v1 + 8) == a1;
return result;
}

original_code:
static inline int tree_parent_on_left(struct rbtree *current)
{
 if(current->parent != ((void *)0) && current->parent->right == current) {
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall tree_rotate_swap_parent(__int64 **a1, __int64 a2, __int64 a3)
{
int v4; // er12
__int64 result; // rax

v4 = *(_DWORD *)(a2 + 36);
if ( *(_QWORD *)(a2 + 8) == a3 )
tree_rotate_left(a1, a2);
else
tree_rotate_right(a1, (_QWORD *)a2);
result = *(unsigned int *)(a3 + 36);
*(_DWORD *)(a2 + 36) = result;
*(_DWORD *)(a3 + 36) = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 **a1, __int64 a2, __int64 a3)
{
int v4; // er12
__int64 result; // rax

v4 = *(_DWORD *)(a2 + 36);
if ( *(_QWORD *)(a2 + 8) == a3 )
tree_rotate_left(a1, a2);
else
func5(a1, (_QWORD *)a2);
result = *(unsigned int *)(a3 + 36);
*(_DWORD *)(a2 + 36) = result;
*(_DWORD *)(a3 + 36) = v4;
return result;
}

original_code:
static void tree_rotate_swap_parent(struct rbtree_root *root, struct rbtree *parent,
         struct rbtree *current)
{
 rbtree_color_t tmp = parent->color;
 if(parent->right == current) {
  tree_rotate_left(root, parent);
 } else {
  tree_rotate_right(root, parent);
 }
 parent->color = current->color;
 current->color = tmp;
}

canonical_original_code:
static void tree_rotate_swap_parent(struct rbtree_root *root, struct rbtree *parent, struct rbtree *current)
{
 enum rbtree_color_t tmp = parent->color;
 if(parent->right == current) {
  tree_rotate_left(root, parent);
 } else {
  tree_rotate_right(root, parent);
 }
 parent->color = current->color;
 current->color = tmp;
}

variable_types: struct rbtree_root { struct rbtree { struct rbtree *left; struct rbtree *right; struct rbtree *parent; struct rbtree *root; int key; enum rbtree_color_t { RED=0, BLACK=1 } color; void *data; } *tree; unsigned long size; } *

variable_types:
parent
struct rbtree *

variable_types:
current
struct rbtree *

variable_types:
tmp
enum rbtree_color_t { RED=0, BLACK=1 }

user_defined_types: struct rbtree_root { struct rbtree *tree; unsigned long size; }


decompiled_code:
__int64 __fastcall tree_sub_deletion(__int64 **a1, __int64 a2)
{
int v2; // ebp
__int64 v3; // r12
__int64 v4; // rbx
__int64 v5; // rdx
__int64 result; // rax
__int64 v7; // rcx
__int64 v8; // rax
int v9; // edx
_QWORD *v10; // rsi
__int64 v11; // r13
__int64 *v12; // rbp
__int64 v13; // rax
_QWORD *v14; // rsi

v3 = a2;
v4 = tree_node_has_sibling(a2);
v5 = tree_node_far_nephew(a2);
result = *(_QWORD *)(a2 + 16);
if ( *(_QWORD *)result == a2 )
*(_QWORD *)result = 0LL;
else
*(_QWORD *)(result + 8) = 0LL;
while ( 1 )
{
while ( 1 )
{
if ( !v4 )
goto LABEL_23;
result = *(unsigned int *)(v4 + 36);
if ( (_DWORD)result )
break;
LABEL_15:
v8 = *(_QWORD *)(v4 + 16);
v9 = *(_DWORD *)(v8 + 36);
*(_DWORD *)(v8 + 36) = *(_DWORD *)(v4 + 36);
*(_DWORD *)(v4 + 36) = v9;
v10 = *(_QWORD **)(v4 + 16);
if ( *v10 == v4 )
{
tree_rotate_right(a1, v10);
result = *(_QWORD *)(v3 + 16);
v4 = *(_QWORD *)result;
v5 = **(_QWORD **)result;
}
else
{
tree_rotate_left(a1, (__int64)v10);
result = *(_QWORD *)(v3 + 16);
v4 = *(_QWORD *)(result + 8);
v5 = *(_QWORD *)(v4 + 8);
}
v2 = 1;
}
v11 = *(_QWORD *)v4;
if ( *(_QWORD *)v4 && *(_DWORD *)(v11 + 36) != 1 )
{
if ( (_DWORD)result == 1 )
{
if ( !*(_DWORD *)(v11 + 36) )
goto LABEL_30;
result = *(_QWORD *)(v4 + 8);
if ( result )
{
LABEL_9:
if ( !*(_DWORD *)(result + 36) )
{
if ( v5 )
goto LABEL_31;
LABEL_32:
v11 = *(_QWORD *)v4;
if ( *(_QWORD *)v4 == v5 )
{
v12 = *(__int64 **)(v4 + 8);
tree_rotate_left(a1, v4);
v5 = *v12;
v4 = (__int64)v12;
goto LABEL_34;
}
goto LABEL_33;
}
}
}
LABEL_23:
switch ( v2 )
{
case 1:
goto LABEL_15;
case 2:
goto LABEL_25;
case 3:
goto LABEL_32;
case 4:
goto LABEL_34;
case 5:
goto LABEL_35;
default:
return result;
}
return result;
}
v7 = *(_QWORD *)(v4 + 8);
if ( v7 )
{
if ( *(_DWORD *)(v7 + 36) != 1 )
break;
}
LABEL_25:
*(_DWORD *)(v4 + 36) = 0;
v3 = *(_QWORD *)(v4 + 16);
if ( *(_DWORD *)(v3 + 36) != 1 )
{
*(_DWORD *)(v3 + 36) = 1;
return result;
}
v4 = tree_node_has_sibling(*(_QWORD *)(v4 + 16));
result = tree_node_far_nephew(v3);
v5 = result;
v2 = 2;
}
if ( (_DWORD)result != 1 )
goto LABEL_23;
if ( !v11 )
{
result = *(_QWORD *)(v4 + 8);
goto LABEL_9;
}
if ( *(_DWORD *)(v11 + 36) )
{
result = *(_QWORD *)(v4 + 8);
goto LABEL_9;
}
LABEL_30:
if ( !v5 )
{
LABEL_33:
tree_rotate_right(a1, (_QWORD *)v4);
v5 = *(_QWORD *)(v11 + 8);
v4 = v11;
goto LABEL_34;
}
LABEL_31:
if ( *(_DWORD *)(v5 + 36) == 1 )
goto LABEL_32;
LABEL_34:
*(_DWORD *)(v5 + 36) = 1;
v13 = *(_QWORD *)(v4 + 16);
*(_DWORD *)(v4 + 36) = *(_DWORD *)(v13 + 36);
*(_DWORD *)(v13 + 36) = 1;
LABEL_35:
v14 = *(_QWORD **)(v3 + 16);
if ( v14[1] == v4 )
result = (__int64)tree_rotate_left(a1, (__int64)v14);
else
result = tree_rotate_right(a1, v14);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 **a1, __int64 a2)
{
int v2; // ebp
__int64 v3; // r12
__int64 v4; // rbx
__int64 v5; // rdx
__int64 result; // rax
__int64 v7; // rcx
__int64 v8; // rax
int v9; // edx
_QWORD *v10; // rsi
__int64 v11; // r13
__int64 *v12; // rbp
__int64 v13; // rax
_QWORD *v14; // rsi

v3 = a2;
v4 = func2(a2);
v5 = func3(a2);
result = *(_QWORD *)(a2 + 16);
if ( *(_QWORD *)result == a2 )
*(_QWORD *)result = 0LL;
else
*(_QWORD *)(result + 8) = 0LL;
while ( 1 )
{
while ( 1 )
{
if ( !v4 )
goto LABEL_23;
result = *(unsigned int *)(v4 + 36);
if ( (_DWORD)result )
break;
LABEL_15:
v8 = *(_QWORD *)(v4 + 16);
v9 = *(_DWORD *)(v8 + 36);
*(_DWORD *)(v8 + 36) = *(_DWORD *)(v4 + 36);
*(_DWORD *)(v4 + 36) = v9;
v10 = *(_QWORD **)(v4 + 16);
if ( *v10 == v4 )
{
func5(a1, v10);
result = *(_QWORD *)(v3 + 16);
v4 = *(_QWORD *)result;
v5 = **(_QWORD **)result;
}
else
{
tree_rotate_left(a1, (__int64)v10);
result = *(_QWORD *)(v3 + 16);
v4 = *(_QWORD *)(result + 8);
v5 = *(_QWORD *)(v4 + 8);
}
v2 = 1;
}
v11 = *(_QWORD *)v4;
if ( *(_QWORD *)v4 && *(_DWORD *)(v11 + 36) != 1 )
{
if ( (_DWORD)result == 1 )
{
if ( !*(_DWORD *)(v11 + 36) )
goto LABEL_30;
result = *(_QWORD *)(v4 + 8);
if ( result )
{
LABEL_9:
if ( !*(_DWORD *)(result + 36) )
{
if ( v5 )
goto LABEL_31;
LABEL_32:
v11 = *(_QWORD *)v4;
if ( *(_QWORD *)v4 == v5 )
{
v12 = *(__int64 **)(v4 + 8);
tree_rotate_left(a1, v4);
v5 = *v12;
v4 = (__int64)v12;
goto LABEL_34;
}
goto LABEL_33;
}
}
}
LABEL_23:
switch ( v2 )
{
case 1:
goto LABEL_15;
case 2:
goto LABEL_25;
case 3:
goto LABEL_32;
case 4:
goto LABEL_34;
case 5:
goto LABEL_35;
default:
return result;
}
return result;
}
v7 = *(_QWORD *)(v4 + 8);
if ( v7 )
{
if ( *(_DWORD *)(v7 + 36) != 1 )
break;
}
LABEL_25:
*(_DWORD *)(v4 + 36) = 0;
v3 = *(_QWORD *)(v4 + 16);
if ( *(_DWORD *)(v3 + 36) != 1 )
{
*(_DWORD *)(v3 + 36) = 1;
return result;
}
v4 = func2(*(_QWORD *)(v4 + 16));
result = func3(v3);
v5 = result;
v2 = 2;
}
if ( (_DWORD)result != 1 )
goto LABEL_23;
if ( !v11 )
{
result = *(_QWORD *)(v4 + 8);
goto LABEL_9;
}
if ( *(_DWORD *)(v11 + 36) )
{
result = *(_QWORD *)(v4 + 8);
goto LABEL_9;
}
LABEL_30:
if ( !v5 )
{
LABEL_33:
func5(a1, (_QWORD *)v4);
v5 = *(_QWORD *)(v11 + 8);
v4 = v11;
goto LABEL_34;
}
LABEL_31:
if ( *(_DWORD *)(v5 + 36) == 1 )
goto LABEL_32;
LABEL_34:
*(_DWORD *)(v5 + 36) = 1;
v13 = *(_QWORD *)(v4 + 16);
*(_DWORD *)(v4 + 36) = *(_DWORD *)(v13 + 36);
*(_DWORD *)(v13 + 36) = 1;
LABEL_35:
v14 = *(_QWORD **)(v3 + 16);
if ( v14[1] == v4 )
result = (__int64)tree_rotate_left(a1, (__int64)v14);
else
result = func5(a1, v14);
return result;
}

original_code:
static void tree_sub_deletion(struct rbtree_root *root, struct rbtree *current)
{
 tree_deletion_subcase_t _case;
 struct rbtree *sibling, *fn;
 rbtree_color_t tmp;
 sibling = tree_node_has_sibling(current);
 fn = tree_node_far_nephew(current);
 if(current->parent->left == current) {
  current->parent->left = ((void *)0);
 } else {
  current->parent->right = ((void *)0);
 }
 do {
  if(sibling != ((void *)0)) {
   if(sibling->color == RED) {
    _case = TREE_SUB_DELETION_CASE0;
   } else if((!sibling->left || sibling->left->color == BLACK) &&
    (!sibling->right || sibling->right->color == BLACK)) {
    _case = TREE_SUB_DELETION_CASE1;
   } else if(sibling->color == BLACK) {
    if((sibling->left && sibling->left->color == RED) ||
     (sibling->right && sibling->right->color == RED)) {
     _case = (!fn || fn->color == BLACK) ? TREE_SUB_DELETION_CASE2 :
                  TREE_SUB_DELETION_CASE3;
    }
   }
  }
  switch(_case) {
   case TREE_SUB_DELETION_CASE0:
    tmp = sibling->parent->color;
    sibling->parent->color = sibling->color;
    sibling->color = tmp;
    if(sibling->parent->left == sibling) {
     tree_rotate_right(root, sibling->parent);
     sibling = current->parent->left;
     fn = sibling->left;
    } else {
     tree_rotate_left(root, sibling->parent);
     sibling = current->parent->right;
     fn = sibling->right;
    }
    break;
   case TREE_SUB_DELETION_CASE1:
    sibling->color = RED;
    current = sibling->parent;
    if(current->color == BLACK) {
     sibling = tree_node_has_sibling(current);
     fn = tree_node_far_nephew(current);
     break;
    } else {
     current->color = BLACK;
     _case = TREE_SUB_DELETION_TERMINATE;
     break;
    }
   case TREE_SUB_DELETION_CASE2:
    if(fn == sibling->left) {
     fn = sibling->right;
     tree_rotate_left(root, sibling);
     sibling = fn;
     fn = sibling->left;
    } else {
     fn = sibling->left;
     tree_rotate_right(root, sibling);
     sibling = fn;
     fn = sibling->right;
    }
   case TREE_SUB_DELETION_CASE3:
    fn->color = BLACK;
    sibling->color = sibling->parent->color;
    sibling->parent->color = BLACK;
   case TREE_SUB_DELETION_CASE4:
    if(current->parent->right == sibling) {
     tree_rotate_left(root, current->parent);
    } else {
     tree_rotate_right(root, current->parent);
    }
    _case = TREE_SUB_DELETION_TERMINATE;
    break;
   default:
    _case = TREE_SUB_DELETION_TERMINATE;
    break;
  }
 } while(_case);
}

canonical_original_code:
static void tree_sub_deletion(struct rbtree_root *root, struct rbtree *current)
{
 enum tree_deletion_subcase_t _case;
 struct rbtree *sibling; struct rbtree *fn;
 enum rbtree_color_t tmp;
 sibling = tree_node_has_sibling(current);
 fn = tree_node_far_nephew(current);
 if(current->parent->left == current) {
  current->parent->left = ((void *)0);
 } else {
  current->parent->right = ((void *)0);
 }
 do {
  if(sibling != ((void *)0)) {
   if(sibling->color == RED) {
    _case = TREE_SUB_DELETION_CASE0;
   } else if((!sibling->left || sibling->left->color == BLACK) &&
    (!sibling->right || sibling->right->color == BLACK)) {
    _case = TREE_SUB_DELETION_CASE1;
   } else if(sibling->color == BLACK) {
    if((sibling->left && sibling->left->color == RED) ||
     (sibling->right && sibling->right->color == RED)) {
     _case = (!fn || fn->color == BLACK) ? TREE_SUB_DELETION_CASE2 :
                  TREE_SUB_DELETION_CASE3;
    }
   }
  }
  switch(_case) {
   case TREE_SUB_DELETION_CASE0:
    tmp = sibling->parent->color;
    sibling->parent->color = sibling->color;
    sibling->color = tmp;
    if(sibling->parent->left == sibling) {
     tree_rotate_right(root, sibling->parent);
     sibling = current->parent->left;
     fn = sibling->left;
    } else {
     tree_rotate_left(root, sibling->parent);
     sibling = current->parent->right;
     fn = sibling->right;
    }
    break;
   case TREE_SUB_DELETION_CASE1:
    sibling->color = RED;
    current = sibling->parent;
    if(current->color == BLACK) {
     sibling = tree_node_has_sibling(current);
     fn = tree_node_far_nephew(current);
     break;
    } else {
     current->color = BLACK;
     _case = TREE_SUB_DELETION_TERMINATE;
     break;
    }
   case TREE_SUB_DELETION_CASE2:
    if(fn == sibling->left) {
     fn = sibling->right;
     tree_rotate_left(root, sibling);
     sibling = fn;
     fn = sibling->left;
    } else {
     fn = sibling->left;
     tree_rotate_right(root, sibling);
     sibling = fn;
     fn = sibling->right;
    }
   case TREE_SUB_DELETION_CASE3:
    fn->color = BLACK;
    sibling->color = sibling->parent->color;
    sibling->parent->color = BLACK;
   case TREE_SUB_DELETION_CASE4:
    if(current->parent->right == sibling) {
     tree_rotate_left(root, current->parent);
    } else {
     tree_rotate_right(root, current->parent);
    }
    _case = TREE_SUB_DELETION_TERMINATE;
    break;
   default:
    _case = TREE_SUB_DELETION_TERMINATE;
    break;
  }
 } while(_case);
}

variable_types: enum tree_deletion_subcase_t { TREE_SUB_DELETION_TERMINATE=0, TREE_SUB_DELETION_CASE0=1, TREE_SUB_DELETION_CASE1=2, TREE_SUB_DELETION_CASE2=3, TREE_SUB_DELETION_CASE3=4, TREE_SUB_DELETION_CASE4=5, TREE_SUB_DELETION_CASE5=6, TREE_SUB_DELETION_CASE6=7 }


decompiled_code:
__int64 __fastcall tree_insert(__int64 **a1, __int64 a2)
{
__int64 *v2; // rax

*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_DWORD *)(a2 + 36) = 0;
v2 = _tree_insert(a1, (__int64 *)a2);
tree_validate_insertion(a1, (__int64)v2);
return 0LL;
}

original_code:
int tree_insert(struct rbtree_root *root, struct rbtree *node)
{
 node->left = ((void *)0);
 node->right = ((void *)0);
 node->parent = ((void *)0);
 node->color = RED;
 tree_validate_insertion(root, __tree_insert(root, node));
 return 0;
}


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

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

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

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

original_code:
struct rbtree *tree_find_rightmost(struct rbtree *tree)
{
 if(!tree) {
  return ((void *)0);
 }
 for(;;) {
  if(tree->right == ((void *)0)) {
   return tree;
  }
  tree = tree->right;
 }
}

variable_types:
tree
struct rbtree *


decompiled_code:
__int64 __fastcall tree_find_predecessor(__int64 *a1)
{
__int64 v1; // rbx
__int64 v2; // rdi
__int64 result; // rax

v1 = (__int64)a1;
if ( !a1 )
return 0LL;
v2 = *a1;
if ( v2 )
return tree_find_rightmost(v2);
result = tree_parent(v1);
if ( result )
{
while ( *(_QWORD *)(result + 8) != v1 )
{
v1 = result;
if ( !*(_QWORD *)(result + 16) )
return 0LL;
result = *(_QWORD *)(result + 16);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 *a1)
{
__int64 v1; // rbx
__int64 v2; // rdi
__int64 result; // rax

v1 = (__int64)a1;
if ( !a1 )
return 0LL;
v2 = *a1;
if ( v2 )
return func11(v2);
result = func0(v1);
if ( result )
{
while ( *(_QWORD *)(result + 8) != v1 )
{
v1 = result;
if ( !*(_QWORD *)(result + 16) )
return 0LL;
result = *(_QWORD *)(result + 16);
}
}
return result;
}

original_code:
static struct rbtree *tree_find_predecessor(struct rbtree *tree)
{
 if(!tree) {
  return ((void *)0);
 }
 if(!tree->left) {
  struct rbtree *tmp = tree, *carriage = tree_parent(tree);
  while (carriage != ((void *)0) && carriage->right != tmp) {
   tmp = carriage;
   carriage = carriage->parent;
  }
  return carriage;
 }
 return tree_find_rightmost(tree->left);
}

canonical_original_code:
static struct rbtree *tree_find_predecessor(struct rbtree *tree)
{
 if(!tree) {
  return ((void *)0);
 }
 if(!tree->left) {
  struct rbtree *tmp = tree; struct rbtree *carriage = tree_parent(tree);
  while (carriage != ((void *)0) && carriage->right != tmp) {
   tmp = carriage;
   carriage = carriage->parent;
  }
  return carriage;
 }
 return tree_find_rightmost(tree->left);
}


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

result = (__int64)tree_find_successor((__int64)a1);
if ( result && *(_DWORD *)(result + 36) == 1 && !*(_QWORD *)result && !*(_QWORD *)(result + 8) )
result = tree_find_predecessor(a1);
return result;
}

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

result = (__int64)tree_find_successor((__int64)a1);
if ( result && *(_DWORD *)(result + 36) == 1 && !*(_QWORD *)result && !*(_QWORD *)(result + 8) )
result = func12(a1);
return result;
}

original_code:
static struct rbtree *tree_find_replacement(struct rbtree *tree)
{
 struct rbtree *successor = tree_find_successor(tree);
 if(!successor) {
  return ((void *)0);
 }
 if(successor->color == RED || !(successor->color == BLACK && successor->left == ((void *)0)
  && successor->right == ((void *)0))) {
  return successor;
 } else {
  return tree_find_predecessor(tree);
 }
}


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

result = _tree_delete_node(a1, a2);
if ( *a1 )
*((_DWORD *)*a1 + 9) = 1;
return result;
}

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

result = func14(a1, a2);
if ( *a1 )
*((_DWORD *)*a1 + 9) = 1;
return result;
}

original_code:
int tree_delete_node(struct rbtree_root *root, struct rbtree *node)
{
 int rc = __tree_delete_node(root, node);
 if(root->tree) {
  root->tree->color = BLACK;
 }
 return rc;
}

variable_types: rc int


cmpstr

decompiled_code:
int __fastcall cmpstr(const void *a1, const void *a2)
{
return strcmp((const char *)(*(_QWORD *)a1 + 19LL), (const char *)(*(_QWORD *)a2 + 19LL));
}

canonical_decompiled_code:
int __fastcall func9(const void *a1, const void *a2)
{
return strcmp((const char *)(*(_QWORD *)a1 + 19LL), (const char *)(*(_QWORD *)a2 + 19LL));
}

original_code:
int cmpstr(const void *p1, const void *p2) {
  struct dirent * const *name_one = p1;
  struct dirent * const *name_two = p2;
  return strcmp((*name_one)->d_name, (*name_two)->d_name);
}

canonical_original_code:
int cmpstr(void *p1, void *p2) {
  struct dirent **name_one = p1;
  struct dirent **name_two = p2;
  return strcmp((*name_one)->d_name, (*name_two)->d_name);
}

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } **


decompiled_code:
const char *__fastcall fix_path(const char *a1, const char *a2)
{
int v3; // er14
int v4; // er13
const char *v5; // rax
const char *v6; // rbx
__int64 v7; // rsi
__int64 v8; // rax
__int64 v9; // rdx
char *v10; // rcx
__int64 v11; // rax
__int64 v12; // rdx

v3 = strlen(a1);
v4 = strlen(a2);
v5 = (const char *)malloc(v4 + v3 + 1);
v6 = v5;
if ( v3 <= 0 )
{
v10 = (char *)v5;
}
else
{
v7 = (unsigned int)(v3 - 1);
v8 = 0LL;
do
{
v6[v8] = a1[v8];
v9 = v8++;
}
while ( v7 != v9 );
v10 = (char *)&v6[v7 + 1];
}
*v10 = 47;
if ( v4 > 0 )
{
v11 = 0LL;
do
{
v10[v11 + 1] = a2[v11];
v12 = v11++;
}
while ( v4 - 1 != v12 );
}
puts(v6);
return v6;
}

original_code:
char *fix_path(const char *folder_path, const char *file_name) {
  int i = 0;
  char *pointer;
  char *fixed_path;
  int folder_length = strlen(folder_path);
  int file_length = strlen(file_name);
  fixed_path = (char *)malloc(sizeof(char) * (folder_length + file_length + 1));
  pointer = fixed_path;
  for (i = 0; i< folder_length; i++) {
    *(pointer++) = *(folder_path++);
  }
  *(pointer++) = '/';
  for (i = 0; i< file_length; i++) {
    *(pointer++) = *(file_name++);
  }
  printf("%s\n", fixed_path);
  return fixed_path;
}

canonical_original_code:
char *fix_path(char *folder_path, char *file_name) {
  int i = 0;
  char *pointer;
  char *fixed_path;
  int folder_length = strlen(folder_path);
  int file_length = strlen(file_name);
  fixed_path = (char *)malloc(sizeof(char) * (folder_length + file_length + 1));
  pointer = fixed_path;
  for (i = 0; i< folder_length; i++) {
    *(pointer++) = *(folder_path++);
  }
  *(pointer++) = '/';
  for (i = 0; i< file_length; i++) {
    *(pointer++) = *(file_name++);
  }
  printf("%s\n", fixed_path);
  return fixed_path;
}

variable_types: i int

variable_types:
pointer
char *

return_type: char *


decompiled_code:
char *__fastcall fix_path2(char *s, char *src)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rbx

v2 = strlen(s);
v3 = strlen(src);
v4 = (char *)malloc(v2 + v3 + 2);
strcpy(v4, s);
if ( v4[strlen(s) - 1] != 47 )
*(_WORD *)&v4[strlen(v4)] = 47;
return strcat(v4, src);
}

canonical_decompiled_code:
char *__fastcall func11(char *s, char *src)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rbx

v2 = strlen(s);
v3 = strlen(src);
v4 = (char *)malloc(v2 + v3 + 2);
strcpy(v4, s);
if ( v4[strlen(s) - 1] != 47 )
*(_WORD *)&v4[strlen(v4)] = 47;
return strcat(v4, src);
}

original_code:
char *fix_path2(const char *folder_path, const char *file_name) {
  char *both = malloc(strlen(folder_path) + strlen(file_name) + 2);
  strcpy(both, folder_path);
  if (*(both + strlen(folder_path) - 1) != '/') {
    strcat(both, "/");
  }
  strcat(both, file_name);
  return both;
}

canonical_original_code:
char *fix_path2(char *folder_path, char *file_name) {
  char *both = malloc(strlen(folder_path) + strlen(file_name) + 2);
  strcpy(both, folder_path);
  if (*(both + strlen(folder_path) - 1) != '/') {
    strcat(both, "/");
  }
  strcat(both, file_name);
  return both;
}

variable_types:
both
char *


decompiled_code:
const char *__fastcall get_username(__uid_t a1)
{
struct passwd *v1; // rax
const char *result; // rax

v1 = getpwuid(a1);
if ( v1 )
result = v1->pw_name;
else
result = "Un Known";
return result;
}

canonical_decompiled_code:
const char *__fastcall func12(__uid_t a1)
{
struct passwd *v1; // rax
const char *result; // rax

v1 = getpwuid(a1);
if ( v1 )
result = v1->pw_name;
else
result = "Un Known";
return result;
}

original_code:
char *get_username(const uid_t st_uid) {
  struct passwd *entry;
  entry = getpwuid(st_uid);
  if (entry == ((void *)0)) {
    return "Un Known";
  }
  return entry->pw_name;
}

canonical_original_code:
char *get_username(unsigned int st_uid) {
  struct passwd *entry;
  entry = getpwuid(st_uid);
  if (entry == ((void *)0)) {
    return "Un Known";
  }
  return entry->pw_name;
}

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


decompiled_code:
const char *__fastcall get_groupname(__gid_t a1)
{
struct group *v1; // rax
const char *result; // rax

v1 = getgrgid(a1);
if ( v1 )
result = v1->gr_name;
else
result = "Un Known";
return result;
}

canonical_decompiled_code:
const char *__fastcall func13(__gid_t a1)
{
struct group *v1; // rax
const char *result; // rax

v1 = getgrgid(a1);
if ( v1 )
result = v1->gr_name;
else
result = "Un Known";
return result;
}

original_code:
char *get_groupname(const gid_t st_gid) {
  struct group *entry;
  entry = getgrgid(st_gid);
  if (entry == ((void *)0)) {
    return "Un Known";
  }
  return entry->gr_name;
}

canonical_original_code:
char *get_groupname(unsigned int st_gid) {
  struct group *entry;
  entry = getgrgid(st_gid);
  if (entry == ((void *)0)) {
    return "Un Known";
  }
  return entry->gr_name;
}

variable_types: struct group { char *gr_name; char *gr_passwd; unsigned int gr_gid; char **gr_mem; } *


decompiled_code:
char *__fastcall format_time(time_t a1)
{
char *v1; // rbx
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0x14uLL);
v2 = localtime(&timer);
strftime(v1, 0x14uLL, "%Y-%m-%d %H:%M:%S", v2);
return v1;
}

canonical_decompiled_code:
char *__fastcall func14(time_t a1)
{
char *v1; // rbx
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0x14uLL);
v2 = localtime(&timer);
strftime(v1, 0x14uLL, "%Y-%m-%d %H:%M:%S", v2);
return v1;
}

original_code:
char *format_time(const time_t st_time) {
  char *buf;
  int bufsize = 20;
  struct tm *ts;
  buf = malloc(sizeof(char) * bufsize);
  ts = localtime(&st_time);
  strftime(buf, bufsize, "%Y-%m-%d %H:%M:%S", ts);
  return buf;
}

canonical_original_code:
char *format_time(long st_time) {
  char *buf;
  int bufsize = 20;
  struct tm *ts;
  buf = malloc(sizeof(char) * bufsize);
  ts = localtime(&st_time);
  strftime(buf, bufsize, "%Y-%m-%d %H:%M:%S", ts);
  return buf;
}

variable_types:
buf
char *

variable_types: bufsize int

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


sort

decompiled_code:
void __fastcall sort(void *a1, int a2)
{
qsort(a1, a2, 8uLL, cmpstr);
}

canonical_decompiled_code:
void __fastcall func15(void *a1, int a2)
{
qsort(a1, a2, 8uLL, cmpstr);
}

original_code:
void sort(void **entries, int number) {
  qsort(entries, number, sizeof(*entries), cmpstr);
}

variable_types:
entries
void **

variable_types: number int


decompiled_code:
__int64 __fastcall add_new_arg(void **a1, int a2, int a3, const char *a4)
{
unsigned int v4; // er12
char **v7; // rbx
void *v9; // rax

v4 = a2;
if ( a3 + 1 >= a2 )
{
v4 = a2 + 4;
v9 = realloc(*a1, 8LL * (a2 + 4));
*a1 = v9;
if ( !v9 )
{
syslog(3, "Failed to realloc memory for more arguments!\n");
exit(1);
}
}
if ( a4 )
{
v7 = (char **)((char *)*a1 + 8 * a3);
*v7 = strdup(a4);
}
else
{
*((_QWORD *)*a1 + a3) = 0LL;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(void **a1, int a2, int a3, const char *a4)
{
unsigned int v4; // er12
char **v7; // rbx
void *v9; // rax

v4 = a2;
if ( a3 + 1 >= a2 )
{
v4 = a2 + 4;
v9 = realloc(*a1, 8LL * (a2 + 4));
*a1 = v9;
if ( !v9 )
{
syslog(3, "Failed to realloc memory for more arguments!\n");
exit(1);
}
}
if ( a4 )
{
v7 = (char **)((char *)*a1 + 8 * a3);
*v7 = strdup(a4);
}
else
{
*((_QWORD *)*a1 + a3) = 0LL;
}
return v4;
}

original_code:
int add_new_arg(char ***args_array, int array_size,
                int element_index, char *arg) {
  if (array_size <= element_index + 1) {
    int new_size = array_size + (4);
    *args_array = realloc(*args_array, new_size * sizeof(char *));
    if (*args_array == ((void *)0)) {
      syslog(3, "Failed to realloc memory for more arguments!\n");
      exit(1);
    }
    array_size = new_size;
  }
  if (arg != ((void *)0)) {
          (*args_array)[element_index] = strdup(arg);
  } else {
          (*args_array)[element_index] = ((void *)0);
  }
  return array_size;
}

variable_types:
arg
char *


decompiled_code:
__int64 __fastcall add_new_arg2(__int64 a1, int a2, int a3, const char *a4)
{
unsigned int v4; // er13
char **v7; // rbx
void *v9; // rax

v4 = a2;
if ( a3 + 1 >= a2 )
{
v4 = a2 + 4;
v9 = realloc(*(void **)(a1 + 24), 8LL * (a2 + 4));
*(_QWORD *)(a1 + 24) = v9;
if ( !v9 )
{
syslog(3, "Failed to realloc memory for more arguments!\n");
exit(1);
}
}
if ( a4 )
{
v7 = (char **)(*(_QWORD *)(a1 + 24) + 8LL * a3);
*v7 = strdup(a4);
}
else
{
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * a3) = 0LL;
}
++*(_DWORD *)(a1 + 32);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3, const char *a4)
{
unsigned int v4; // er13
char **v7; // rbx
void *v9; // rax

v4 = a2;
if ( a3 + 1 >= a2 )
{
v4 = a2 + 4;
v9 = realloc(*(void **)(a1 + 24), 8LL * (a2 + 4));
*(_QWORD *)(a1 + 24) = v9;
if ( !v9 )
{
syslog(3, "Failed to realloc memory for more arguments!\n");
exit(1);
}
}
if ( a4 )
{
v7 = (char **)(*(_QWORD *)(a1 + 24) + 8LL * a3);
*v7 = strdup(a4);
}
else
{
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * a3) = 0LL;
}
++*(_DWORD *)(a1 + 32);
return v4;
}

original_code:
int add_new_arg2(struct AppStruct *appstruct, int array_size,
                 int element_index, char *arg) {
  if (array_size <= element_index + 1) {
    int new_size = array_size + (4);
    appstruct->args = realloc(appstruct->args, new_size * sizeof(char *));
    if (appstruct->args == ((void *)0)) {
      syslog(3, "Failed to realloc memory for more arguments!\n");
      exit(1);
    }
    array_size = new_size;
  }
  if (arg != ((void *)0)) {
    appstruct->args[element_index] = strdup(arg);
  } else {
    appstruct->args[element_index] = ((void *)0);
  }
  appstruct->argnum += 1;
  return array_size;
}

variable_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; } *