stats

decompiled_code:
unsigned int *__fastcall stats(int *a1, int a2, __int64 a3)
{
unsigned int *result; // rax
int v5; // edi

result = (unsigned int *)(unsigned int)*a1;
*(_DWORD *)(a3 + 8) = (_DWORD)result;
*(_DWORD *)(a3 + 4) = (_DWORD)result;
v5 = *a1;
if ( a2 > 1 )
{
result = (unsigned int *)(a1 + 1);
do
{
if ( (signed int)*result > *(_DWORD *)(a3 + 4) )
*(_DWORD *)(a3 + 4) = *result;
if ( (signed int)*result < *(_DWORD *)(a3 + 8) )
*(_DWORD *)(a3 + 8) = *result;
v5 += *result++;
}
while ( result != (unsigned int *)&a1[a2 - 2 + 2] );
}
*(float *)a3 = (float)v5 / (float)a2;
return result;
}

canonical_decompiled_code:
unsigned int *__fastcall func2(int *a1, int a2, __int64 a3)
{
unsigned int *result; // rax
int v5; // edi

result = (unsigned int *)(unsigned int)*a1;
*(_DWORD *)(a3 + 8) = (_DWORD)result;
*(_DWORD *)(a3 + 4) = (_DWORD)result;
v5 = *a1;
if ( a2 > 1 )
{
result = (unsigned int *)(a1 + 1);
do
{
if ( (signed int)*result > *(_DWORD *)(a3 + 4) )
*(_DWORD *)(a3 + 4) = *result;
if ( (signed int)*result < *(_DWORD *)(a3 + 8) )
*(_DWORD *)(a3 + 8) = *result;
v5 += *result++;
}
while ( result != (unsigned int *)&a1[a2 - 2 + 2] );
}
*(float *)a3 = (float)v5 / (float)a2;
return result;
}

original_code:
void stats(int * numbers, int nb_numbers, struct stat_t *res)
{
  int i;
  long somme;
  res->min = numbers[0];
  res->max = numbers[0];
  somme = numbers[0];
  for (i=1; i < nb_numbers; ++i)
    {
      if (numbers[i] > res -> max) res -> max = numbers[i];
      if (numbers[i] < res -> min) res -> min = numbers[i];
      somme += numbers[i];
    }
  res -> moy = ((float)somme) / nb_numbers;
}

canonical_original_code:
void stats(int *numbers, int nb_numbers, struct stat_t *res)
{
  int i;
  long somme;
  res->min = numbers[0];
  res->max = numbers[0];
  somme = numbers[0];
  for (i=1; i < nb_numbers; ++i)
    {
      if (numbers[i] > res -> max) res -> max = numbers[i];
      if (numbers[i] < res -> min) res -> min = numbers[i];
      somme += numbers[i];
    }
  res -> moy = ((float)somme) / nb_numbers;
}

variable_types:
numbers
int *

variable_types: struct stat_t { float moy; int max; int min; } *

variable_types: somme long


repo: shinvwang/five-chess

decompiled_code:
__int64 is_draw()
{
int (__fastcall **v0)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *); // rdx
int (__fastcall **v1)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *); // rax

v0 = (int (__fastcall **)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *))&unk_404188;
while ( 2 )
{
v1 = v0 - 5;
do
{
if ( !*(_DWORD *)v1 )
return 0LL;
v1 = (int (__fastcall **)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *))((char *)v1 + 4);
}
while ( v1 != v0 );
v0 += 5;
if ( v0 != &_libc_start_main )
continue;
break;
}
return 1LL;
}

canonical_decompiled_code:
__int64 func7()
{
int (__fastcall **v0)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *); // rdx
int (__fastcall **v1)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *); // rax

v0 = (int (__fastcall **)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *))&unk_404188;
while ( 2 )
{
v1 = v0 - 5;
do
{
if ( !*(_DWORD *)v1 )
return 0LL;
v1 = (int (__fastcall **)(int (__fastcall *)(int, char **, char **), int, char **, void (*)(void), void (*)(void), void (*)(void), void *))((char *)v1 + 4);
}
while ( v1 != v0 );
v0 += 5;
if ( v0 != &_libc_start_main )
continue;
break;
}
return 1LL;
}

original_code:
_Bool is_draw(void)
{
    int i, j;
    for (i = 0; i < 10; i ++) {
        for (j = 0; j < 10; j ++) {
            if(chess_board[i][j] == 0) {
                return 0;
            }
        }
    }
    return 1;
}

canonical_original_code:
_Bool is_draw()
{
    int i; int j;
    for (i = 0; i < 10; i ++) {
        for (j = 0; j < 10; j ++) {
            if(chess_board[i][j] == 0) {
                return 0;
            }
        }
    }
    return 1;
}

variable_types: i int

variable_types: j int


decompiled_code:
int clear_screen()
{
return system("clear");
}

canonical_decompiled_code:
int func8()
{
return system("clear");
}

original_code:
void clear_screen(void)
{
    system("clear");
}

canonical_original_code:
void clear_screen()
{
    system("clear");
}

return_type: void


decompiled_code:
int chess_board_display()
{
unsigned int i; // ebx
int v1; // ebx
unsigned int *v2; // rbp
unsigned int j; // er12
unsigned int *v4; // rbx

printf(" ");
for ( i = 0; i != 10; ++i )
printf(" %d", i);
putchar(10);
v1 = 11;
do
{
printf(" %c", 45LL);
--v1;
}
while ( v1 );
putchar(10);
v2 = (unsigned int *)&unk_404188;
for ( j = 0; j != 10; ++j )
{
printf("%d|", j);
v4 = v2 - 10;
do
printf(" %d", *v4++);
while ( v4 != v2 );
putchar(10);
v2 += 10;
}
return putchar(10);
}

canonical_decompiled_code:
int func9()
{
unsigned int i; // ebx
int v1; // ebx
unsigned int *v2; // rbp
unsigned int j; // er12
unsigned int *v4; // rbx

printf(" ");
for ( i = 0; i != 10; ++i )
printf(" %d", i);
putchar(10);
v1 = 11;
do
{
printf(" %c", 45LL);
--v1;
}
while ( v1 );
putchar(10);
v2 = (unsigned int *)&unk_404188;
for ( j = 0; j != 10; ++j )
{
printf("%d|", j);
v4 = v2 - 10;
do
printf(" %d", *v4++);
while ( v4 != v2 );
putchar(10);
v2 += 10;
}
return putchar(10);
}

original_code:
void chess_board_display(void)
{
    int i, j;
    printf("  ");
    for (i = 0; i < 10; i ++) {
        printf(" %d", i);
    }
    putchar('\n');
    for (i = 0; i < 10 + 1; i ++) {
        printf(" %c", '-');
    }
    putchar('\n');
    for (i = 0; i < 10; i ++) {
        printf("%d|", i);
        for (j = 0; j < 10; j ++) {
            printf(" %d", chess_board[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

canonical_original_code:
void chess_board_display()
{
    int i; int j;
    printf("  ");
    for (i = 0; i < 10; i ++) {
        printf(" %d", i);
    }
    putchar('\n');
    for (i = 0; i < 10 + 1; i ++) {
        printf(" %c", '-');
    }
    putchar('\n');
    for (i = 0; i < 10; i ++) {
        printf("%d|", i);
        for (j = 0; j < 10; j ++) {
            printf(" %d", chess_board[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}


decompiled_code:
int __fastcall get_user_input(__int64 a1, __int64 a2, unsigned int a3)
{
printf("Player%d Input: ", a3);
while ( (unsigned int)__isoc99_scanf() != 2 )
{
puts("Bad Input, Re-try.");
while ( getchar() != 10 )
;
}
return getchar();
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1, __int64 a2, unsigned int a3)
{
printf("Player%d Input: ", a3);
while ( (unsigned int)__isoc99_scanf() != 2 )
{
puts("Bad Input, Re-try.");
while ( getchar() != 10 )
;
}
return getchar();
}

original_code:
void get_user_input(int *px, int *py, int who)
{
    printf("Player%d Input: ", who);
    while(scanf("%d%d", px, py) != 2) {
        printf("Bad Input, Re-try.\n");
        while(getchar() != '\n');
    }
    getchar();
}

variable_types:
px
int *

variable_types:
py
int *

variable_types: who int


decompiled_code:
__int64 __fastcall is_on_board(unsigned int a1, unsigned int a2, int a3)
{
unsigned int v3; // eax

LOBYTE(v3) = a1 <= 9;
LOBYTE(a3) = a2 <= 9;
return a3 & v3;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1, unsigned int a2, int a3)
{
unsigned int v3; // eax

LOBYTE(v3) = a1 <= 9;
LOBYTE(a3) = a2 <= 9;
return a3 & v3;
}

original_code:
_Bool is_on_board(int x, int y)
{
    if ((x >= 0 && x < 10) &&
            (y >= 0 && y < 10)) {
        return 1;
    }
    return 0;
}

variable_types: x int

variable_types: y int


decompiled_code:
bool __fastcall is_empty(int a1, int a2)
{
return chess_board[10 * a1 + a2] == 0;
}

canonical_decompiled_code:
bool __fastcall func12(int a1, int a2)
{
return chess_board[10 * a1 + a2] == 0;
}

original_code:
_Bool is_empty(int x, int y)
{
    return chess_board[x][y] == 0;
}


decompiled_code:
__int64 __fastcall move_in_chess(int a1, int a2, int a3)
{
__int64 result; // rax

result = a2 + 10LL * a1;
chess_board[result] = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1, int a2, int a3)
{
__int64 result; // rax

result = a2 + 10LL * a1;
chess_board[result] = a3;
return result;
}

original_code:
void move_in_chess(int x, int y, int who)
{
    chess_board[x][y] = who;
}


decompiled_code:
__int64 __fastcall chess_counter(unsigned int a1, unsigned int a2, int *a3)
{
int v3; // er14
int v4; // edx
int *v5; // r12
unsigned int v6; // ebp
unsigned int v7; // ebx
unsigned int i; // er13
int v9; // eax
int v11; // [rsp+4h] [rbp-3Ch]
__int64 v12; // [rsp+8h] [rbp-38h]

v3 = *a3;
v11 = a3[1];
v12 = 4 * (v11 + 10LL * *a3);
v4 = 5 * (*a3 + a1);
v5 = &chess_board[10 * (v3 + a1) + v11 + a2];
v6 = a2;
v7 = a1;
for ( i = 0; ; ++i )
{
v7 += v3;
v6 += v11;
if ( !(unsigned __int8)is_on_board(v7, v6, v4) )
break;
v9 = *v5;
v5 = (int *)((char *)v5 + v12);
if ( v9 != chess_board[10 * a1 + a2] )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1, unsigned int a2, int *a3)
{
int v3; // er14
int v4; // edx
int *v5; // r12
unsigned int v6; // ebp
unsigned int v7; // ebx
unsigned int i; // er13
int v9; // eax
int v11; // [rsp+4h] [rbp-3Ch]
__int64 v12; // [rsp+8h] [rbp-38h]

v3 = *a3;
v11 = a3[1];
v12 = 4 * (v11 + 10LL * *a3);
v4 = 5 * (*a3 + a1);
v5 = &chess_board[10 * (v3 + a1) + v11 + a2];
v6 = a2;
v7 = a1;
for ( i = 0; ; ++i )
{
v7 += v3;
v6 += v11;
if ( !(unsigned __int8)func11(v7, v6, v4) )
break;
v9 = *v5;
v5 = (int *)((char *)v5 + v12);
if ( v9 != chess_board[10 * a1 + a2] )
break;
}
return i;
}

original_code:
int chess_counter(int x, int y, dir_t *dir)
{
    int nx, ny;
    int counter = 0;
    nx = x;
    ny = y;
    while(1) {
        nx += dir->dx;
        ny += dir->dy;
        if (! is_on_board(nx, ny)) {
            break;
        }
        if (chess_board[nx][ny] != chess_board[x][y]) {
            break;
        }
        counter ++;
    }
    return counter;
}

canonical_original_code:
int chess_counter(int x, int y, struct direction *dir)
{
    int nx; int ny;
    int counter = 0;
    nx = x;
    ny = y;
    while(1) {
        nx += dir->dx;
        ny += dir->dy;
        if (! is_on_board(nx, ny)) {
            break;
        }
        if (chess_board[nx][ny] != chess_board[x][y]) {
            break;
        }
        counter ++;
    }
    return counter;
}

variable_types: struct direction { int dx; int dy; char name[16]; } *

variable_types: nx int

variable_types: ny int

variable_types: counter int

return_type: int


decompiled_code:
__int64 __fastcall is_win(unsigned int a1, unsigned int a2)
{
int *v2; // rbp
int v3; // ebx

v2 = (int *)&dirs;
while ( 1 )
{
v3 = chess_counter(a1, a2, v2);
if ( (int)(chess_counter(a1, a2, v2 + 6) + v3) > 3 )
break;
v2 += 12;
if ( v2 == (int *)&_bss_start )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int a1, unsigned int a2)
{
int *v2; // rbp
int v3; // ebx

v2 = (int *)&dirs;
while ( 1 )
{
v3 = func14(a1, a2, v2);
if ( (int)(func14(a1, a2, v2 + 6) + v3) > 3 )
break;
v2 += 12;
if ( v2 == (int *)&_bss_start )
return 0LL;
}
return 1LL;
}

original_code:
_Bool is_win(int x, int y)
{
    int i;
    for (i = 0; i < 4; i ++) {
        int counter1 = chess_counter(x, y, &(dirs[i * 2]));
        int counter2 = chess_counter(x, y, &(dirs[i * 2 + 1]));
        if (counter1 + counter2 + 1 >= 5) {
            return 1;
        }
    }
    return 0;
}


decompiled_code:
int five_chess()
{
int i; // er12
unsigned int v1; // ebx
int v2; // er13
int v3; // ebp
int v4; // edx
unsigned int v6; // [rsp+8h] [rbp-30h] BYREF
unsigned int v7[11]; // [rsp+Ch] [rbp-2Ch] BYREF

chess_board_init();
for ( i = 0; ; ++i )
{
while ( 1 )
{
while ( 1 )
{
clear_screen();
chess_board_display();
v1 = i % 2 + 1;
get_user_input((__int64)v7, (__int64)&v6, v1);
v2 = v6;
v3 = v7[0];
if ( (unsigned __int8)is_on_board(v7[0], v6, v4) )
break;
puts("Bad Location, Re-try.");
getchar();
}
if ( is_empty(v3, v2) )
break;
puts("Not Empty, Re-try.");
getchar();
}
move_in_chess(v3, v2, v1);
if ( (unsigned __int8)is_win(v7[0], v6) )
{
clear_screen();
chess_board_display();
return printf("Player%d win.\n", v1);
}
if ( (unsigned __int8)is_draw() )
break;
}
return puts("Draw, Game Over.");
}

canonical_decompiled_code:
int func16()
{
int i; // er12
unsigned int v1; // ebx
int v2; // er13
int v3; // ebp
int v4; // edx
unsigned int v6; // [rsp+8h] [rbp-30h] BYREF
unsigned int v7[11]; // [rsp+Ch] [rbp-2Ch] BYREF

chess_board_init();
for ( i = 0; ; ++i )
{
while ( 1 )
{
while ( 1 )
{
func8();
func9();
v1 = i % 2 + 1;
func10((__int64)v7, (__int64)&v6, v1);
v2 = v6;
v3 = v7[0];
if ( (unsigned __int8)func11(v7[0], v6, v4) )
break;
puts("Bad Location, Re-try.");
getchar();
}
if ( func12(v3, v2) )
break;
puts("Not Empty, Re-try.");
getchar();
}
func13(v3, v2, v1);
if ( (unsigned __int8)func15(v7[0], v6) )
{
func8();
func9();
return printf("Player%d win.\n", v1);
}
if ( (unsigned __int8)func7() )
break;
}
return puts("Draw, Game Over.");
}

original_code:
void five_chess(void)
{
    int who;
    int x, y;
    int step = 0;
    chess_board_init();
    while(1) {
        clear_screen();
        chess_board_display();
        who = step % 2 + 1;
        get_user_input(&x, &y, who);
        if (! is_on_board(x, y)) {
            printf("Bad Location, Re-try.\n");
            getchar();
            continue;
        }
        if (! is_empty(x, y)) {
            printf("Not Empty, Re-try.\n");
            getchar();
            continue;
        }
        move_in_chess(x, y, who);
        if (is_win(x, y)) {
            clear_screen();
            chess_board_display();
            printf("Player%d win.\n", who);
            break;
        }
        if (is_draw()) {
            printf("Draw, Game Over.\n");
            break;
        }
        step ++;
    }
}

canonical_original_code:
void five_chess()
{
    int who;
    int x; int y;
    int step = 0;
    chess_board_init();
    while(1) {
        clear_screen();
        chess_board_display();
        who = step % 2 + 1;
        get_user_input(&x, &y, who);
        if (! is_on_board(x, y)) {
            printf("Bad Location, Re-try.\n");
            getchar();
            continue;
        }
        if (! is_empty(x, y)) {
            printf("Not Empty, Re-try.\n");
            getchar();
            continue;
        }
        move_in_chess(x, y, who);
        if (is_win(x, y)) {
            clear_screen();
            chess_board_display();
            printf("Player%d win.\n", who);
            break;
        }
        if (is_draw()) {
            printf("Draw, Game Over.\n");
            break;
        }
        step ++;
    }
}

variable_types: step int


main

canonical_name: main

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

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

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

variable_types: argc int

variable_types:
argv
char *[]


repo: shishirng/ndmp

decompiled_code:
_QWORD *init_queue()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x20uLL);
v0[2] = get_lock();
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rbx

v0 = calloc(1uLL, 0x20uLL);
v0[2] = get_lock();
return v0;
}

original_code:
struct queue_hdr* init_queue()
{
        struct queue_hdr* retval = (struct queue_hdr *) calloc(1,
                        sizeof(struct queue_hdr));
        retval->lock = get_lock();
        return retval;
}

canonical_original_code:
struct queue_hdr *init_queue()
{
        struct queue_hdr *retval = (struct queue_hdr *) calloc(1,
                        sizeof(struct queue_hdr));
        retval->lock = get_lock();
        return retval;
}

variable_types: struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *

user_defined_types: struct queue_hdr { struct queue_node *first; struct queue_node *last; struct lock *lock; int num_elems; }

user_defined_types: struct lock { union pthread_mutex_t mutex; }

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

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


enqueue

decompiled_code:
__int64 __fastcall enqueue(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp

v2 = malloc(0x10uLL);
*v2 = a2;
v2[1] = 0LL;
enter_critical_section(*(_QWORD *)(a1 + 16));
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v2;
else
*(_QWORD *)a1 = v2;
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)(a1 + 24);
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp

v2 = malloc(0x10uLL);
*v2 = a2;
v2[1] = 0LL;
enter_critical_section(*(_QWORD *)(a1 + 16));
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v2;
else
*(_QWORD *)a1 = v2;
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)(a1 + 24);
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

original_code:
void enqueue(struct queue_hdr* hdr, void *elem)
{
        struct queue_node *node = (struct queue_node *) malloc(
                        sizeof(struct queue_node));
        node->elem = elem;
        node->next = ((void *)0);
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                hdr->first = node;
                hdr->last = node;
        } else {
                hdr->last->next = node;
                hdr->last = node;
        }
        hdr->num_elems++;
        exit_critical_section(hdr->lock);
}

canonical_original_code:
void enqueue(struct queue_hdr *hdr, void *elem)
{
        struct queue_node *node = (struct queue_node *) malloc(
                        sizeof(struct queue_node));
        node->elem = elem;
        node->next = ((void *)0);
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                hdr->first = node;
                hdr->last = node;
        } else {
                hdr->last->next = node;
                hdr->last = node;
        }
        hdr->num_elems++;
        exit_critical_section(hdr->lock);
}

variable_types:
elem
void *

variable_types: struct queue_node { void *elem; struct queue_node *next; } *

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


dequeue

decompiled_code:
__int64 __fastcall dequeue(__int64 a1)
{
__int64 *v1; // rbp
__int64 v2; // r12

enter_critical_section(*(_QWORD *)(a1 + 16));
v1 = *(__int64 **)a1;
if ( *(_QWORD *)a1 )
{
*(_QWORD *)a1 = v1[1];
v2 = *v1;
free(v1);
--*(_DWORD *)(a1 + 24);
exit_critical_section(*(_QWORD *)(a1 + 16));
}
else
{
exit_critical_section(*(_QWORD *)(a1 + 16));
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 *v1; // rbp
__int64 v2; // r12

enter_critical_section(*(_QWORD *)(a1 + 16));
v1 = *(__int64 **)a1;
if ( *(_QWORD *)a1 )
{
*(_QWORD *)a1 = v1[1];
v2 = *v1;
free(v1);
--*(_DWORD *)(a1 + 24);
exit_critical_section(*(_QWORD *)(a1 + 16));
}
else
{
exit_critical_section(*(_QWORD *)(a1 + 16));
v2 = 0LL;
}
return v2;
}

original_code:
void *dequeue(struct queue_hdr *hdr)
{
        void* retval;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                exit_critical_section(hdr->lock);
                return ((void *)0) ;
        }
        node = hdr->first;
        hdr->first = node->next;
        retval = node->elem;
        free(node);
        hdr->num_elems--;
        exit_critical_section(hdr->lock);
        return retval;
}

canonical_original_code:
void *dequeue(struct queue_hdr *hdr)
{
        void *retval;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                exit_critical_section(hdr->lock);
                return ((void *)0) ;
        }
        node = hdr->first;
        hdr->first = node->next;
        retval = node->elem;
        free(node);
        hdr->num_elems--;
        exit_critical_section(hdr->lock);
        return retval;
}

variable_types:
retval
void *

return_type: void *

user_defined_types: struct queue_node { void *elem; struct queue_node *next; }


decompiled_code:
__int64 __fastcall get_elem(_QWORD *a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // rbx
__int64 result; // rax

enter_critical_section(a1[2]);
v4 = (_QWORD *)*a1;
if ( *a1 )
{
while ( !a3(a2, *v4) )
{
v4 = (_QWORD *)v4[1];
if ( !v4 )
goto LABEL_4;
}
exit_critical_section(a1[2]);
result = *v4;
}
else
{
LABEL_4:
exit_critical_section(a1[2]);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // rbx
__int64 result; // rax

enter_critical_section(a1[2]);
v4 = (_QWORD *)*a1;
if ( *a1 )
{
while ( !a3(a2, *v4) )
{
v4 = (_QWORD *)v4[1];
if ( !v4 )
goto LABEL_4;
}
exit_critical_section(a1[2]);
result = *v4;
}
else
{
LABEL_4:
exit_critical_section(a1[2]);
result = 0LL;
}
return result;
}

original_code:
void* get_elem(struct queue_hdr *hdr, void* target, comparator cmp)
{
        int result;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        node = hdr->first;
        while (node != ((void *)0) ) {
                result = cmp(target, node->elem);
                if (result) {
                        exit_critical_section(hdr->lock);
                        return node->elem;
                } else
                        node = node->next;
        }
        exit_critical_section(hdr->lock);
        return ((void *)0) ;
}

canonical_original_code:
void *get_elem(struct queue_hdr *hdr, void *target, int (*cmp)(void *target, void *elem))
{
        int result;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        node = hdr->first;
        while (node != ((void *)0) ) {
                result = cmp(target, node->elem);
                if (result) {
                        exit_critical_section(hdr->lock);
                        return node->elem;
                } else
                        node = node->next;
        }
        exit_critical_section(hdr->lock);
        return ((void *)0) ;
}

variable_types:
target
void *

variable_types:
cmp
int (*)(void *target, void *elem)

variable_types: result int


decompiled_code:
__int64 __fastcall remove_elem(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // rbx
_QWORD *v5; // r13

enter_critical_section(*(_QWORD *)(a1 + 16));
v4 = *(_QWORD **)a1;
if ( *(_QWORD *)a1 )
{
v5 = *(_QWORD **)a1;
while ( !a3(a2, *v4) )
{
v5 = v4;
if ( !v4[1] )
return exit_critical_section(*(_QWORD *)(a1 + 16));
v4 = (_QWORD *)v4[1];
}
if ( *(_QWORD **)a1 == v4 )
{
*(_QWORD *)a1 = v4[1];
}
else if ( *(_QWORD **)(a1 + 8) == v4 )
{
*(_QWORD *)(a1 + 8) = v5;
v5[1] = 0LL;
}
else
{
v5[1] = v4[1];
}
--*(_DWORD *)(a1 + 24);
free(v4);
}
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

original_code:
void remove_elem(struct queue_hdr *hdr, void* target, comparator cmp)
{
        struct queue_node *node, *nodeprev;
        enter_critical_section(hdr->lock);
        nodeprev = hdr->first;
        node = nodeprev;
        while (node != ((void *)0) ) {
                if (cmp(target, node->elem)) {
                        if (node == hdr->first) {
                                hdr->first = node->next;
                        }
                        else if (node == hdr->last) {
                                hdr->last = nodeprev;
                                nodeprev->next = ((void *)0);
                        }
                        else {
                                nodeprev->next = node->next;
                        }
                        hdr->num_elems--;
                        free(node);
                        break;
                }
                nodeprev = node;
                node = node->next;
        }
        exit_critical_section(hdr->lock);
}

canonical_original_code:
void remove_elem(struct queue_hdr *hdr, void *target, int (*cmp)(void *target, void *elem))
{
        struct queue_node *node; struct queue_node *nodeprev;
        enter_critical_section(hdr->lock);
        nodeprev = hdr->first;
        node = nodeprev;
        while (node != ((void *)0) ) {
                if (cmp(target, node->elem)) {
                        if (node == hdr->first) {
                                hdr->first = node->next;
                        }
                        else if (node == hdr->last) {
                                hdr->last = nodeprev;
                                nodeprev->next = ((void *)0);
                        }
                        else {
                                nodeprev->next = node->next;
                        }
                        hdr->num_elems--;
                        free(node);
                        break;
                }
                nodeprev = node;
                node = node->next;
        }
        exit_critical_section(hdr->lock);
}


decompiled_code:
__int64 __fastcall num_elems(__int64 a1)
{
unsigned int v1; // ebp

enter_critical_section(*(_QWORD *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 24);
exit_critical_section(*(_QWORD *)(a1 + 16));
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int v1; // ebp

enter_critical_section(*(_QWORD *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 24);
exit_critical_section(*(_QWORD *)(a1 + 16));
return v1;
}

original_code:
int num_elems(struct queue_hdr *hdr)
{
        int retval = 0;
        enter_critical_section(hdr->lock);
        retval = hdr->num_elems;
        exit_critical_section(hdr->lock);
        return retval;
}

variable_types: retval int


decompiled_code:
void __fastcall __noreturn run_job(void *a1)
{
__int64 v2; // rdi

while ( 1 )
{
v2 = dequeue(*((_QWORD *)a1 + 4));
if ( v2 )
{
(*(void (__fastcall **)(__int64))a1)(v2);
puts("Processed a job ");
}
else
{
pthread_yield();
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
__int64 v2; // rdi

while ( 1 )
{
v2 = dequeue(*((_QWORD *)a1 + 4));
if ( v2 )
{
(*(void (__fastcall **)(__int64))a1)(v2);
puts("Processed a job ");
}
else
{
pthread_yield();
}
}
}

original_code:
void* run_job(void *context)
{
        struct comm_context *ctx = (struct comm_context *)context;
        struct client_txn *job;
        while (1) {
                job = (struct client_txn *)dequeue(ctx->request_jobs);
                if (job != ((void *)0)) {
                        ctx->marshal_unmarshal(job);
                        printf("Processed a job \n");
                }
                else {
                        pthread_yield();
                }
        }
        return ctx;
}

canonical_original_code:
void *run_job(void *context)
{
        struct comm_context *ctx = (struct comm_context *)context;
        struct client_txn *job;
        while (1) {
                job = (struct client_txn *)dequeue(ctx->request_jobs);
                if (job != ((void *)0)) {
                        ctx->marshal_unmarshal(job);
                        printf("Processed a job \n");
                }
                else {
                        pthread_yield();
                }
        }
        return ctx;
}

variable_types:
context
void *

variable_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn { struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; } *

user_defined_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; }

user_defined_types: struct client_txn { struct session client_session; struct comm_message request; struct comm_message reply; }

user_defined_types: struct session { int session_id; struct client_endpoint client_info; }

user_defined_types: struct client_endpoint { struct sockaddr_in client; int fd; }

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


decompiled_code:
int __fastcall create_worker_threads(int a1)
{
__int64 v2; // rbx
void *v3; // rax
void *v4; // rax

if ( !threads )
{
threads = (__int64)malloc(8LL * (a1 + 1));
if ( a1 > 0 )
{
v2 = 0LL;
do
{
v3 = (void *)comm_context();
pthread_create((pthread_t *)(threads + v2), 0LL, (void *(*)(void *))run_job, v3);
v2 += 8LL;
}
while ( v2 != 8LL * (unsigned int)a1 );
}
v4 = (void *)comm_context();
pthread_create((pthread_t *)(threads + 8LL * (a1 + 1) - 8), 0LL, process_response, v4);
}
return printf("Created %d worker threads \n", (unsigned int)a1);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
__int64 v2; // rbx
void *v3; // rax
void *v4; // rax

if ( !threads )
{
threads = (__int64)malloc(8LL * (a1 + 1));
if ( a1 > 0 )
{
v2 = 0LL;
do
{
v3 = (void *)comm_context();
pthread_create((pthread_t *)(threads + v2), 0LL, (void *(*)(void *))run_job, v3);
v2 += 8LL;
}
while ( v2 != 8LL * (unsigned int)a1 );
}
v4 = (void *)comm_context();
pthread_create((pthread_t *)(threads + 8LL * (a1 + 1) - 8), 0LL, process_response, v4);
}
return printf("Created %d worker threads \n", (unsigned int)a1);
}

original_code:
void create_worker_threads(int thread_pool_size)
{
        int i;
        if (threads == ((void *)0)) {
                threads = (pthread_t *)malloc((thread_pool_size+1)*
                                              sizeof(pthread_t));
                for (i=0; i<thread_pool_size; i++) {
                        pthread_create(&threads[i], ((void *)0), run_job,
                                       (void *)comm_context());
                }
                pthread_create(&threads[thread_pool_size], ((void *)0),
                               process_response, (void *)comm_context());
                num_threads = thread_pool_size;
        }
        printf("Created %d worker threads \n", thread_pool_size);
}

canonical_original_code:
void create_worker_threads(int thread_pool_size)
{
        int i;
        if (threads == ((void *)0)) {
                threads = (unsigned long *)malloc((thread_pool_size+1)*
                                              sizeof(pthread_t));
                for (i=0; i<thread_pool_size; i++) {
                        pthread_create(&threads[i], ((void *)0), run_job,
                                       (void *)comm_context());
                }
                pthread_create(&threads[thread_pool_size], ((void *)0),
                               process_response, (void *)comm_context());
                num_threads = thread_pool_size;
        }
        printf("Created %d worker threads \n", thread_pool_size);
}

variable_types: i int


decompiled_code:
void shutdown_worker_threads()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void shutdown_worker_threads()
{
}

canonical_original_code:
void shutdown_worker_threads()
{
}


decompiled_code:
__int64 __fastcall write_message(const __m128i *a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
__int16 v3; // bx
unsigned __int16 v5; // bx
char *v6; // rax
int v7[516]; // [rsp+0h] [rbp-858h] BYREF
struct in_addr in[4]; // [rsp+810h] [rbp-48h]

*(__m128i *)&in[0].s_addr = _mm_loadu_si128(a1);
v2 = a1->m128i_i32[0];
v3 = a1->m128i_i16[3];
qmemcpy(v7, &a1[130], 0x808uLL);
if ( v7[513] > 0 )
{
v1 = (unsigned int)send(v2, &v7[1], v7[513], 0);
v5 = _bswap_16(v3);
v6 = inet_ntoa(in[2]);
printf("Sent a %d byte response to (%s,%d) on sock %d\n", v1, v6, v5, v2);
}
return (unsigned int)-(v7[513] != (_DWORD)v1);
}

canonical_decompiled_code:
__int64 __fastcall func4(const __m128i *a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
__int16 v3; // bx
unsigned __int16 v5; // bx
char *v6; // rax
int v7[516]; // [rsp+0h] [rbp-858h] BYREF
struct in_addr in[4]; // [rsp+810h] [rbp-48h]

*(__m128i *)&in[0].s_addr = _mm_loadu_si128(a1);
v2 = a1->m128i_i32[0];
v3 = a1->m128i_i16[3];
qmemcpy(v7, &a1[130], 0x808uLL);
if ( v7[513] > 0 )
{
v1 = (unsigned int)send(v2, &v7[1], v7[513], 0);
v5 = func1(v3);
v6 = inet_ntoa(in[2]);
printf("Sent a %d byte response to (%s,%d) on sock %d\n", v1, v6, v5, v2);
}
return (unsigned int)-(v7[513] != (_DWORD)v1);
}

original_code:
int write_message(struct client_txn *job)
{
        struct session tmp;
        struct comm_message reply;
        int fd, sent_bytes;
        int len;
        tmp = job->client_session;
        reply = job->reply;
        fd = tmp.session_id;
        if (reply.length > 0) {
                sent_bytes = send(fd,reply.message,reply.length,0);
                printf("Sent a %d byte response to (%s,%d) on sock %d\n",
                       sent_bytes, inet_ntoa(tmp.client_info.client.sin_addr),
                       ntohs(tmp.client_info.client.sin_port), fd);
        }
        return -1 * (sent_bytes != reply.length);
}

canonical_original_code:
int write_message(struct client_txn *job)
{
        struct session tmp;
        struct comm_message reply;
        int fd; int sent_bytes;
        int len;
        tmp = job->client_session;
        reply = job->reply;
        fd = tmp.session_id;
        if (reply.length > 0) {
                sent_bytes = send(fd,reply.message,reply.length,0);
                printf("Sent a %d byte response to (%s,%d) on sock %d\n",
                       sent_bytes, inet_ntoa(tmp.client_info.client.sin_addr),
                       ntohs(tmp.client_info.client.sin_port), fd);
        }
        return -1 * (sent_bytes != reply.length);
}

variable_types: struct client_txn { struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *

variable_types: fd int

variable_types: len int


decompiled_code:
void __fastcall __noreturn process_response(void *a1)
{
const __m128i *v1; // rax
__m128i *v2; // rbx
__m128i v3; // [rsp+0h] [rbp-38h] BYREF
__int64 v4; // [rsp+10h] [rbp-28h]

while ( 1 )
{
while ( 1 )
{
v1 = (const __m128i *)dequeue(*((_QWORD *)a1 + 5));
v2 = (__m128i *)v1;
if ( v1 )
break;
pthread_yield();
}
v3 = _mm_loadu_si128(v1);
v4 = v1[1].m128i_i64[0];
if ( get_elem(*((_QWORD *)a1 + 3), &v3, &session_comparator) )
{
if ( (unsigned int)write_message(v2) == -1 )
puts("Write message error");
}
free(v2);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func5(void *a1)
{
const __m128i *v1; // rax
__m128i *v2; // rbx
__m128i v3; // [rsp+0h] [rbp-38h] BYREF
__int64 v4; // [rsp+10h] [rbp-28h]

while ( 1 )
{
while ( 1 )
{
v1 = (const __m128i *)dequeue(*((_QWORD *)a1 + 5));
v2 = (__m128i *)v1;
if ( v1 )
break;
pthread_yield();
}
v3 = _mm_loadu_si128(v1);
v4 = v1[1].m128i_i64[0];
if ( get_elem(*((_QWORD *)a1 + 3), &v3, &session_comparator) )
{
if ( (unsigned int)func4(v2) == -1 )
puts("Write message error");
}
free(v2);
}
}

original_code:
void *process_response(void *context)
{
        int write_result;
        struct comm_context *ctx = (struct comm_context *)context;
        struct client_txn *job;
        struct session client_session;
        while (1) {
                job = (struct client_txn *)dequeue(ctx->reply_jobs);
                if (job != ((void *)0)) {
                        client_session = job->client_session;
                        if (get_elem(ctx->sessions, &client_session,
                                     session_comparator) != ((void *)0)) {
                                write_result = write_message(job);
                                if (write_result == -1)
                                        printf("Write message error\n");
                        }
                        free(job);
                }
                else
                        pthread_yield();
        }
        return ctx;
}

variable_types: struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; }

user_defined_types: struct comm_message { int is_tcp_connect; char message[2048]; int length; }


decompiled_code:
_BOOL8 __fastcall session_info_cmp(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

original_code:
int session_info_cmp (void *id, void *session)
{
        int session_id = *(int *) id;
        int queue_elem_session_id =
                ((struct ndmp_session_info *)session)->session_id;
        return session_id == queue_elem_session_id;
}

canonical_original_code:
int session_info_cmp(void *id, void *session)
{
        int session_id = *(int *) id;
        int queue_elem_session_id = ((struct ndmp_session_info *)session)->session_id;
        return session_id == queue_elem_session_id;
}

variable_types:
id
void *

variable_types:
session
void *


decompiled_code:
_BOOL8 __fastcall job_cmp(__int64 a1, __int64 a2)
{
return a2 == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, __int64 a2)
{
return a2 == a1;
}

original_code:
int job_cmp(void *target, void *elem)
{
        return target == elem;
}


decompiled_code:
__int64 get_next_seq_number()
{
return (unsigned int)sequence_number_1++;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)sequence_number_1++;
}

original_code:
int get_next_seq_number() {
        static int sequence_number = 1;
        return sequence_number++;
}


decompiled_code:
__int64 __fastcall ndmp_accept_notify(__int64 a1)
{
u_int v1; // edx
XDR v3; // [rsp+0h] [rbp-78h] BYREF
__int64 v4[2]; // [rsp+30h] [rbp-48h] BYREF
int v5; // [rsp+40h] [rbp-38h]
int v6; // [rsp+44h] [rbp-34h]
__int64 v7; // [rsp+48h] [rbp-30h]
int v8; // [rsp+50h] [rbp-28h]
int v9; // [rsp+60h] [rbp-18h] BYREF
__int16 v10; // [rsp+64h] [rbp-14h]
void *v11; // [rsp+68h] [rbp-10h]

v9 = 0;
v10 = 3;
v11 = &unk_688;
v4[0] = (int)get_next_seq_number();
v4[1] = time(0LL);
v5 = 0;
v6 = 1282;
v7 = 0LL;
v8 = 0;
*(_DWORD *)(a1 + 4132) = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v4);
v1 = *(_DWORD *)(a1 + 4132) + xdr_sizeof((xdrproc_t)&xdr_ndmp_notify_connected_request, &v9);
*(_DWORD *)(a1 + 4132) = v1;
xdrmem_create(&v3, (const caddr_t)(a1 + 2084), v1, XDR_ENCODE);
xdr_ndmp_header(&v3, v4);
xdr_ndmp_notify_connected_request(&v3, &v9);
return write_client_log(*(_QWORD *)(a1 + 4), *(_QWORD *)(a1 + 12), "Sent CONNECT_NOTIFY to client");
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
u_int v1; // edx
XDR v3; // [rsp+0h] [rbp-78h] BYREF
__int64 v4[2]; // [rsp+30h] [rbp-48h] BYREF
int v5; // [rsp+40h] [rbp-38h]
int v6; // [rsp+44h] [rbp-34h]
__int64 v7; // [rsp+48h] [rbp-30h]
int v8; // [rsp+50h] [rbp-28h]
int v9; // [rsp+60h] [rbp-18h] BYREF
__int16 v10; // [rsp+64h] [rbp-14h]
void *v11; // [rsp+68h] [rbp-10h]

v9 = 0;
v10 = 3;
v11 = &unk_688;
v4[0] = (int)func2();
v4[1] = time(0LL);
v5 = 0;
v6 = 1282;
v7 = 0LL;
v8 = 0;
*(_DWORD *)(a1 + 4132) = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v4);
v1 = *(_DWORD *)(a1 + 4132) + xdr_sizeof((xdrproc_t)&xdr_ndmp_notify_connected_request, &v9);
*(_DWORD *)(a1 + 4132) = v1;
xdrmem_create(&v3, (const caddr_t)(a1 + 2084), v1, XDR_ENCODE);
xdr_ndmp_header(&v3, v4);
xdr_ndmp_notify_connected_request(&v3, &v9);
return write_client_log(*(_QWORD *)(a1 + 4), *(_QWORD *)(a1 + 12), "Sent CONNECT_NOTIFY to client");
}

original_code:
void ndmp_accept_notify(struct client_txn* txn, struct ndmp_header header, XDR* request_stream)
{
        struct ndmp_notify_connected_request reply;
        struct ndmp_header reply_header;
        XDR reply_stream;
        reply.reason = NDMP_CONNECTED;
        reply.protocol_version = 3;
        reply.text_reason = "";
        reply_header.sequence = get_next_seq_number();
        reply_header.time_stamp = (u_long) time(((void *)0));
        reply_header.message_type = NDMP_MESSAGE_REQUEST;
        reply_header.message = NDMP_NOTIFY_CONNECTED;
        reply_header.reply_sequence = 0;
        reply_header.error = NDMP_NO_ERR;
        txn->reply.length = xdr_sizeof((xdrproc_t)
                                       xdr_ndmp_header,
                                       &reply_header);
        txn->reply.length += xdr_sizeof((xdrproc_t)
                                        xdr_ndmp_notify_connected_request,
                                        &reply);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
        xdr_ndmp_notify_connected_request(&reply_stream, &reply);
        write_client_log(txn->client_session.client_info.client, "Sent CONNECT_NOTIFY to client");
}

canonical_original_code:
void ndmp_accept_notify(struct client_txn *txn, struct ndmp_header header, struct XDR *request_stream)
{
        struct ndmp_notify_connected_request reply;
        struct ndmp_header reply_header;
        struct XDR reply_stream;
        reply.reason = NDMP_CONNECTED;
        reply.protocol_version = 3;
        reply.text_reason = "";
        reply_header.sequence = get_next_seq_number();
        reply_header.time_stamp = (unsigned long) time(((void *)0));
        reply_header.message_type = NDMP_MESSAGE_REQUEST;
        reply_header.message = NDMP_NOTIFY_CONNECTED;
        reply_header.reply_sequence = 0;
        reply_header.error = NDMP_NO_ERR;
        txn->reply.length = xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                       xdr_ndmp_header,
                                       &reply_header);
        txn->reply.length += xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                        xdr_ndmp_notify_connected_request,
                                        &reply);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
        xdr_ndmp_notify_connected_request(&reply_stream, &reply);
        write_client_log(txn->client_session.client_info.client, "Sent CONNECT_NOTIFY to client");
}

variable_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type { NDMP_MESSAGE_REQUEST=0, NDMP_MESSAGE_REPLY=1 } message_type; enum ndmp_message { NDMP_CONNECT_OPEN=2304, NDMP_CONNECT_CLIENT_AUTH=2305, NDMP_CONNECT_CLOSE=2306, NDMP_CONNECT_SERVER_AUTH=2307, NDMP_CONFIG_GET_HOST_INFO=256, NDMP_CONFIG_GET_CONNECTION_TYPE=258, NDMP_CONFIG_GET_AUTH_ATTR=259, NDMP_CONFIG_GET_BUTYPE_INFO=260, NDMP_CONFIG_GET_FS_INFO=261, NDMP_CONFIG_GET_TAPE_INFO=262, NDMP_CONFIG_GET_SCSI_INFO=263, NDMP_CONFIG_GET_SERVER_INFO=264, NDMP_SCSI_OPEN=512, NDMP_SCSI_CLOSE=513, NDMP_SCSI_GET_STATE=514, NDMP_SCSI_SET_TARGET=515, NDMP_SCSI_RESET_DEVICE=516, NDMP_SCSI_RESET_BUS=517, NDMP_SCSI_EXECUTE_CDB=518, NDMP_TAPE_OPEN=768, NDMP_TAPE_CLOSE=769, NDMP_TAPE_GET_STATE=770, NDMP_TAPE_MTIO=771, NDMP_TAPE_WRITE=772, NDMP_TAPE_READ=773, NDMP_TAPE_EXECUTE_CDB=775, NDMP_DATA_GET_STATE=1024, NDMP_DATA_START_BACKUP=1025, NDMP_DATA_START_RECOVER=1026, NDMP_DATA_ABORT=1027, NDMP_DATA_GET_ENV=1028, NDMP_DATA_STOP=1031, NDMP_DATA_LISTEN=1033, NDMP_DATA_CONNECT=1034, NDMP_NOTIFY_DATA_HALTED=1281, NDMP_NOTIFY_CONNECTED=1282, NDMP_NOTIFY_MOVER_HALTED=1283, NDMP_NOTIFY_MOVER_PAUSED=1284, NDMP_NOTIFY_DATA_READ=1285, NDMP_LOG_FILE=1538, NDMP_LOG_MESSAGE=1539, NDMP_FH_ADD_FILE=1795, NDMP_FH_ADD_DIR=1796, NDMP_FH_ADD_NODE=1797, NDMP_MOVER_GET_STATE=2560, NDMP_MOVER_LISTEN=2561, NDMP_MOVER_CONTINUE=2562, NDMP_MOVER_ABORT=2563, NDMP_MOVER_STOP=2564, NDMP_MOVER_SET_WINDOW=2565, NDMP_MOVER_READ=2566, NDMP_MOVER_CLOSE=2567, NDMP_MOVER_SET_RECORD_SIZE=2568, NDMP_MOVER_CONNECT=2569, NDMP_VENDORS_BASE=61440, NDMP_RESERVED_BASE=65280 } message; unsigned long reply_sequence; enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 } error; }

variable_types: struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; } *

variable_types: struct ndmp_notify_connected_request { enum ndmp_connect_reason { NDMP_CONNECTED=0, NDMP_SHUTDOWN=1, NDMP_REFUSED=2 } reason; unsigned short protocol_version; char *text_reason; }

user_defined_types: struct ndmp_header { unsigned long sequence; unsigned long time_stamp; enum ndmp_header_message_type message_type; enum ndmp_message message; unsigned long reply_sequence; enum ndmp_error error; }

user_defined_types: struct ndmp_notify_connected_request { enum ndmp_connect_reason reason; unsigned short protocol_version; char *text_reason; }

user_defined_types: struct XDR { enum xdr_op x_op; struct xdr_ops *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }


decompiled_code:
__int64 __fastcall set_header(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, __int64 a9)
{
__int64 result; // rax

*(_QWORD *)a1 = (int)get_next_seq_number();
*(_QWORD *)(a1 + 8) = time(0LL);
*(_DWORD *)(a1 + 16) = 1;
*(_DWORD *)(a1 + 20) = HIDWORD(a9);
result = a7;
*(_QWORD *)(a1 + 24) = a7;
*(_DWORD *)(a1 + 32) = a2;
return result;
}

original_code:
void set_header(ndmp_header req, ndmp_header *reply, ndmp_error err)
{
        reply->sequence = get_next_seq_number();
        reply->time_stamp = (u_long) time(((void *)0));
        reply->message_type = NDMP_MESSAGE_REPLY;
        reply->message = req.message;
        reply->reply_sequence = req.sequence;
        reply->error = err;
}

canonical_original_code:
void set_header(struct ndmp_header req, struct ndmp_header *reply, enum ndmp_error err)
{
        reply->sequence = get_next_seq_number();
        reply->time_stamp = (unsigned long) time(((void *)0));
        reply->message_type = NDMP_MESSAGE_REPLY;
        reply->message = req.message;
        reply->reply_sequence = req.sequence;
        reply->error = err;
}

variable_types:
reply
struct ndmp_header *

variable_types: enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 }

user_defined_types: enum ndmp_header_message_type { NDMP_MESSAGE_REQUEST=0, NDMP_MESSAGE_REPLY=1 }

user_defined_types: enum ndmp_message { NDMP_CONNECT_OPEN=2304, NDMP_CONNECT_CLIENT_AUTH=2305, NDMP_CONNECT_CLOSE=2306, NDMP_CONNECT_SERVER_AUTH=2307, NDMP_CONFIG_GET_HOST_INFO=256, NDMP_CONFIG_GET_CONNECTION_TYPE=258, NDMP_CONFIG_GET_AUTH_ATTR=259, NDMP_CONFIG_GET_BUTYPE_INFO=260, NDMP_CONFIG_GET_FS_INFO=261, NDMP_CONFIG_GET_TAPE_INFO=262, NDMP_CONFIG_GET_SCSI_INFO=263, NDMP_CONFIG_GET_SERVER_INFO=264, NDMP_SCSI_OPEN=512, NDMP_SCSI_CLOSE=513, NDMP_SCSI_GET_STATE=514, NDMP_SCSI_SET_TARGET=515, NDMP_SCSI_RESET_DEVICE=516, NDMP_SCSI_RESET_BUS=517, NDMP_SCSI_EXECUTE_CDB=518, NDMP_TAPE_OPEN=768, NDMP_TAPE_CLOSE=769, NDMP_TAPE_GET_STATE=770, NDMP_TAPE_MTIO=771, NDMP_TAPE_WRITE=772, NDMP_TAPE_READ=773, NDMP_TAPE_EXECUTE_CDB=775, NDMP_DATA_GET_STATE=1024, NDMP_DATA_START_BACKUP=1025, NDMP_DATA_START_RECOVER=1026, NDMP_DATA_ABORT=1027, NDMP_DATA_GET_ENV=1028, NDMP_DATA_STOP=1031, NDMP_DATA_LISTEN=1033, NDMP_DATA_CONNECT=1034, NDMP_NOTIFY_DATA_HALTED=1281, NDMP_NOTIFY_CONNECTED=1282, NDMP_NOTIFY_MOVER_HALTED=1283, NDMP_NOTIFY_MOVER_PAUSED=1284, NDMP_NOTIFY_DATA_READ=1285, NDMP_LOG_FILE=1538, NDMP_LOG_MESSAGE=1539, NDMP_FH_ADD_FILE=1795, NDMP_FH_ADD_DIR=1796, NDMP_FH_ADD_NODE=1797, NDMP_MOVER_GET_STATE=2560, NDMP_MOVER_LISTEN=2561, NDMP_MOVER_CONTINUE=2562, NDMP_MOVER_ABORT=2563, NDMP_MOVER_STOP=2564, NDMP_MOVER_SET_WINDOW=2565, NDMP_MOVER_READ=2566, NDMP_MOVER_CLOSE=2567, NDMP_MOVER_SET_RECORD_SIZE=2568, NDMP_MOVER_CONNECT=2569, NDMP_VENDORS_BASE=61440, NDMP_RESERVED_BASE=65280 }


decompiled_code:
bool_t __fastcall ndmp_error_message(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
u_int v9; // eax
XDR v11; // [rsp+8h] [rbp-68h] BYREF
char v12[56]; // [rsp+38h] [rbp-38h] BYREF

set_header((__int64)v12, 1, a3, a4, a5, a6, a7, a8, a9);
v9 = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v12);
*(_DWORD *)(a1 + 4132) = v9;
xdrmem_create(&v11, (const caddr_t)(a1 + 2084), v9, XDR_ENCODE);
return xdr_ndmp_header(&v11, v12);
}

canonical_decompiled_code:
bool_t __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
u_int v9; // eax
XDR v11; // [rsp+8h] [rbp-68h] BYREF
char v12[56]; // [rsp+38h] [rbp-38h] BYREF

func4((__int64)v12, 1, a3, a4, a5, a6, a7, a8, a9);
v9 = xdr_sizeof((xdrproc_t)&xdr_ndmp_header, v12);
*(_DWORD *)(a1 + 4132) = v9;
xdrmem_create(&v11, (const caddr_t)(a1 + 2084), v9, XDR_ENCODE);
return xdr_ndmp_header(&v11, v12);
}

original_code:
void ndmp_error_message(struct client_txn *txn,
                        struct ndmp_header header,
                        XDR* requeststream)
{
        struct ndmp_header reply_header;
        XDR reply_stream;
        set_header(header, &reply_header, NDMP_NOT_SUPPORTED_ERR);
        txn->reply.length = xdr_sizeof((xdrproc_t)
                                       xdr_ndmp_header,
                                       &reply_header);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
}

canonical_original_code:
void ndmp_error_message(struct client_txn *txn, struct ndmp_header header, struct XDR *requeststream)
{
        struct ndmp_header reply_header;
        struct XDR reply_stream;
        set_header(header, &reply_header, NDMP_NOT_SUPPORTED_ERR);
        txn->reply.length = xdr_sizeof((int (*)(struct XDR *, void *, ...))
                                       xdr_ndmp_header,
                                       &reply_header);
        xdrmem_create(&reply_stream, txn->reply.message, txn->reply.length,
                      XDR_ENCODE);
        xdr_ndmp_header(&reply_stream, &reply_header);
}

variable_types: struct XDR { enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 } x_op; struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); } *x_ops; char *x_public; char *x_private; char *x_base; unsigned int x_handy; }

user_defined_types: enum ndmp_error { NDMP_NO_ERR=0, NDMP_NOT_SUPPORTED_ERR=1, NDMP_DEVICE_BUSY_ERR=2, NDMP_DEVICE_OPENED_ERR=3, NDMP_NOT_AUTHORIZED_ERR=4, NDMP_PERMISSION_ERR=5, NDMP_DEV_NOT_OPEN_ERR=6, NDMP_IO_ERR=7, NDMP_TIMEOUT_ERR=8, NDMP_ILLEGAL_ARGS_ERR=9, NDMP_NO_TAPE_LOADED_ERR=10, NDMP_WRITE_PROTECT_ERR=11, NDMP_EOF_ERR=12, NDMP_EOM_ERR=13, NDMP_FILE_NOT_FOUND_ERR=14, NDMP_BAD_FILE_ERR=15, NDMP_NO_DEVICE_ERR=16, NDMP_NO_BUS_ERR=17, NDMP_XDR_DECODE_ERR=18, NDMP_ILLEGAL_STATE_ERR=19, NDMP_UNDEFINED_ERR=20, NDMP_XDR_ENCODE_ERR=21, NDMP_NO_MEM_ERR=22, NDMP_CONNECT_ERR=23 }


decompiled_code:
__int64 get_session_queue()
{
__int64 v0; // rbx

v0 = get_lock();
enter_critical_section(v0);
if ( !session_info_queue_0 )
session_info_queue_0 = init_queue();
exit_critical_section(v0);
return session_info_queue_0;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rbx

v0 = get_lock();
enter_critical_section(v0);
if ( !session_info_queue_0 )
session_info_queue_0 = init_queue();
exit_critical_section(v0);
return session_info_queue_0;
}

original_code:
struct queue_hdr* get_session_queue()
{
        static struct queue_hdr *session_info_queue = ((void *)0);
        struct lock *s_lock = get_lock();
        enter_critical_section(s_lock);
        if (session_info_queue == ((void *)0)) {
                session_info_queue = init_queue();
        }
        exit_critical_section(s_lock);
        return session_info_queue;
}

canonical_original_code:
struct queue_hdr *get_session_queue()
{
        static struct queue_hdr *session_info_queue = ((void *)0);
        struct lock *s_lock = get_lock();
        enter_critical_section(s_lock);
        if (session_info_queue == ((void *)0)) {
                session_info_queue = init_queue();
        }
        exit_critical_section(s_lock);
        return session_info_queue;
}

variable_types: struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *

user_defined_types: struct queue_hdr { struct queue_node *first; struct queue_node *last; struct lock *lock; int num_elems; }

user_defined_types: struct lock { union pthread_mutex_t mutex; }


decompiled_code:
__int64 __fastcall add_job_to_session(int *a1)
{
_DWORD *v1; // rax

v1 = get_session_info(*a1);
return enqueue(*((_QWORD *)v1 + 3), a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(int *a1)
{
_DWORD *v1; // rax

v1 = get_session_info(*a1);
return enqueue(*((_QWORD *)v1 + 3), a1);
}

original_code:
void add_job_to_session(struct client_txn *txn)
{
        struct ndmp_session_info *session;
        session = get_session_info(txn->client_session.session_id);
        enqueue(session->jobs, txn);
}

variable_types: struct ndmp_session_info { int session_id; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } connection_state; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } data_state; enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 } mover_state; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *s_lock; struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock *lock; int num_elems; } *jobs; int is_terminated; } *

user_defined_types: struct ndmp_session_info { int session_id; enum ndmp_state connection_state; enum ndmp_state data_state; enum ndmp_state mover_state; struct lock *s_lock; struct queue_hdr *jobs; int is_terminated; }

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

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


decompiled_code:
__int64 __fastcall cleanup_session(int *a1)
{
_DWORD *v1; // rbx
__int64 result; // rax
int v3; // edx
__int64 v4; // rax

v1 = get_session_info(*a1);
remove_elem(*((_QWORD *)v1 + 3), a1, job_cmp);
result = (unsigned int)v1[8];
if ( (_DWORD)result )
{
v3 = num_elems(*((_QWORD *)v1 + 3));
result = 1LL;
if ( !v3 )
{
v4 = get_session_queue();
remove_elem(v4, v1, session_info_cmp);
free(v1);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int *a1)
{
_DWORD *v1; // rbx
__int64 result; // rax
int v3; // edx
__int64 v4; // rax

v1 = get_session_info(*a1);
remove_elem(*((_QWORD *)v1 + 3), a1, job_cmp);
result = (unsigned int)v1[8];
if ( (_DWORD)result )
{
v3 = num_elems(*((_QWORD *)v1 + 3));
result = 1LL;
if ( !v3 )
{
v4 = func6();
remove_elem(v4, v1, session_info_cmp);
free(v1);
result = 1LL;
}
}
return result;
}

original_code:
int cleanup_session(struct client_txn *txn)
{
        struct ndmp_session_info *session;
        struct queue_hdr *session_queue;
        session = get_session_info(txn->client_session.session_id);
        remove_elem(session->jobs, txn, job_cmp);
        if (session->is_terminated) {
                if (num_elems(session->jobs) == 0) {
                        session_queue = get_session_queue();
                        remove_elem(session_queue,
                                    &session->session_id,
                                    session_info_cmp);
                        free(session);
                }
                return 1;
        }
        return 0;
}

user_defined_types: enum ndmp_state { IDLE=0, LISTEN=1, CONNECTED=2, ACTIVE=3, HALTED=4 }

user_defined_types: struct queue_node { void *elem; struct queue_node *next; }

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


decompiled_code:
void __fastcall xdr_decode_encode(void *ptr)
{
__int64 v1; // r12
__int64 (__fastcall *v2)(); // rax
int v3; // ebp
_DWORD *v4; // r13
int v5; // [rsp+8h] [rbp-C0h] BYREF
char addr[4]; // [rsp+Ch] [rbp-BCh] BYREF
__int64 v7; // [rsp+10h] [rbp-B8h] BYREF
int v8; // [rsp+24h] [rbp-A4h]
XDR v9; // [rsp+40h] [rbp-88h] BYREF
XDR xdrs; // [rsp+70h] [rbp-58h] BYREF

v1 = comm_context();
write_client_log(*(_QWORD *)((char *)ptr + 4), *(_QWORD *)((char *)ptr + 12), "Received request from client");
if ( *((_DWORD *)ptr + 6) == 1 )
{
v8 = 61441;
}
else
{
xdrmem_create(&xdrs, (const caddr_t)ptr + 32, *((_DWORD *)ptr + 519) - 4, XDR_DECODE);
xdr_ndmp_header(&xdrs, &v7);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Decoded NDMP header from client request");
}
add_job_to_session((int *)ptr);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Dispatched client request to appropriate message handler");
v2 = ndmp_dispatch(v8);
((void (__fastcall *)(void *, XDR *))v2)(ptr, &xdrs);
if ( (unsigned int)cleanup_session((int *)ptr) != 1 && (v3 = *((_DWORD *)ptr + 1033)) != 0 )
{
v4 = malloc(v3 + 4);
memcpy(v4 + 1, (char *)ptr + 2084, v3);
v5 = v3 | 0x80000000;
xdrmem_create(&v9, addr, 4u, XDR_ENCODE);
xdr_int(&v9, &v5);
*v4 = *(_DWORD *)addr;
memcpy((char *)ptr + 2084, v4, *((_DWORD *)ptr + 1033) + 4);
*((_DWORD *)ptr + 1033) += 4;
enqueue(*(_QWORD *)(v1 + 40), ptr);
free(v4);
}
else
{
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
__int64 v1; // r12
__int64 (__fastcall *v2)(); // rax
int v3; // ebp
_DWORD *v4; // r13
int v5; // [rsp+8h] [rbp-C0h] BYREF
char addr[4]; // [rsp+Ch] [rbp-BCh] BYREF
__int64 v7; // [rsp+10h] [rbp-B8h] BYREF
int v8; // [rsp+24h] [rbp-A4h]
XDR v9; // [rsp+40h] [rbp-88h] BYREF
XDR xdrs; // [rsp+70h] [rbp-58h] BYREF

v1 = comm_context();
write_client_log(*(_QWORD *)((char *)ptr + 4), *(_QWORD *)((char *)ptr + 12), "Received request from client");
if ( *((_DWORD *)ptr + 6) == 1 )
{
v8 = 61441;
}
else
{
xdrmem_create(&xdrs, (const caddr_t)ptr + 32, *((_DWORD *)ptr + 519) - 4, XDR_DECODE);
xdr_ndmp_header(&xdrs, &v7);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Decoded NDMP header from client request");
}
func7((int *)ptr);
write_client_log(
*(_QWORD *)((char *)ptr + 4),
*(_QWORD *)((char *)ptr + 12),
"Dispatched client request to appropriate message handler");
v2 = ndmp_dispatch(v8);
((void (__fastcall *)(void *, XDR *))v2)(ptr, &xdrs);
if ( (unsigned int)func8((int *)ptr) != 1 && (v3 = *((_DWORD *)ptr + 1033)) != 0 )
{
v4 = malloc(v3 + 4);
memcpy(v4 + 1, (char *)ptr + 2084, v3);
v5 = v3 | 0x80000000;
xdrmem_create(&v9, addr, 4u, XDR_ENCODE);
xdr_int(&v9, &v5);
*v4 = *(_DWORD *)addr;
memcpy((char *)ptr + 2084, v4, *((_DWORD *)ptr + 1033) + 4);
*((_DWORD *)ptr + 1033) += 4;
enqueue(*(_QWORD *)(v1 + 40), ptr);
free(v4);
}
else
{
free(ptr);
}
}

original_code:
void xdr_decode_encode(struct client_txn *txn)
{
        XDR request_stream, stream_len;
        int message_type;
        struct ndmp_header header;
        char *buf, mesg_len[4];
        int len;
        struct comm_context *ctx = comm_context();
        write_client_log(txn->client_session.client_info.client, "Received request from client");
        if (txn->request.is_tcp_connect == 1) {
                header.message = 0xf001;
        }
        else {
                xdrmem_create(&request_stream, txn->request.message+4, txn->request.length-4,
                        XDR_DECODE);
                xdr_ndmp_header(&request_stream, &header);
                write_client_log(txn->client_session.client_info.client, "Decoded NDMP header from client request");
        }
        add_job_to_session(txn);
        write_client_log(txn->client_session.client_info.client, "Dispatched client request to appropriate message handler");
        ndmp_dispatch(header.message)(txn, header, &request_stream);
        if (cleanup_session(txn) == 1 || txn->reply.length == 0) {
                free(txn);
        }
        else {
                buf = (char *)malloc(txn->reply.length+4);
                memcpy(buf+4,txn->reply.message,txn->reply.length);
                len = txn->reply.length | (1<<31);
                xdrmem_create(&stream_len,mesg_len,4, XDR_ENCODE);
                xdr_int(&stream_len, &len);
                memcpy(buf,mesg_len,4);
                memcpy(txn->reply.message, buf, txn->reply.length+4);
                txn->reply.length +=4;
                enqueue(ctx->reply_jobs, txn);
                free(buf);
        }
}

canonical_original_code:
void xdr_decode_encode(struct client_txn *txn)
{
        struct XDR request_stream; struct XDR stream_len;
        int message_type;
        struct ndmp_header header;
        char *buf; char mesg_len[4];
        int len;
        struct comm_context *ctx = comm_context();
        write_client_log(txn->client_session.client_info.client, "Received request from client");
        if (txn->request.is_tcp_connect == 1) {
                header.message = 0xf001;
        }
        else {
                xdrmem_create(&request_stream, txn->request.message+4, txn->request.length-4,
                        XDR_DECODE);
                xdr_ndmp_header(&request_stream, &header);
                write_client_log(txn->client_session.client_info.client, "Decoded NDMP header from client request");
        }
        add_job_to_session(txn);
        write_client_log(txn->client_session.client_info.client, "Dispatched client request to appropriate message handler");
        ndmp_dispatch(header.message)(txn, header, &request_stream);
        if (cleanup_session(txn) == 1 || txn->reply.length == 0) {
                free(txn);
        }
        else {
                buf = (char *)malloc(txn->reply.length+4);
                memcpy(buf+4,txn->reply.message,txn->reply.length);
                len = txn->reply.length | (1<<31);
                xdrmem_create(&stream_len,mesg_len,4, XDR_ENCODE);
                xdr_int(&stream_len, &len);
                memcpy(buf,mesg_len,4);
                memcpy(txn->reply.message, buf, txn->reply.length+4);
                txn->reply.length +=4;
                enqueue(ctx->reply_jobs, txn);
                free(buf);
        }
}

variable_types:
buf
char *

user_defined_types: struct client_txn { struct session client_session; struct comm_message request; struct comm_message reply; }

user_defined_types: enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 }

user_defined_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; }

user_defined_types: struct session { int session_id; struct client_endpoint client_info; }

user_defined_types: struct xdr_ops { int (*x_getlong)(struct XDR *__xdrs, long *__lp); int (*x_putlong)(struct XDR *__xdrs, long *__lp); int (*x_getbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); int (*x_putbytes)(struct XDR *__xdrs, char *__addr, unsigned int __len); unsigned int (*x_getpostn)(struct XDR *__xdrs); int (*x_setpostn)(struct XDR *__xdrs, unsigned int __pos); int *(*x_inline)(struct XDR *__xdrs, unsigned int __len); void (*x_destroy)(struct XDR *__xdrs); int (*x_getint32)(struct XDR *__xdrs, int *__ip); int (*x_putint32)(struct XDR *__xdrs, int *__ip); }

user_defined_types: struct client_endpoint { struct sockaddr_in client; int fd; }


decompiled_code:
int __fastcall dump_hex(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebx
unsigned int v5; // ebp

v4 = a2 + 8;
v5 = a2;
do
{
if ( v5 < a3 )
printf("%02x ", *(unsigned __int8 *)(a1 + (int)v5));
else
printf(" ");
++v5;
}
while ( v4 != v5 );
putchar(32);
do
{
if ( a3 <= v4 )
printf(" ");
else
printf("%02x ", *(unsigned __int8 *)(a1 + (int)v4));
++v4;
}
while ( a2 + 16 != v4 );
return putchar(32);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v4; // ebx
unsigned int v5; // ebp

v4 = a2 + 8;
v5 = a2;
do
{
if ( v5 < a3 )
printf("%02x ", *(unsigned __int8 *)(a1 + (int)v5));
else
printf(" ");
++v5;
}
while ( v4 != v5 );
putchar(32);
do
{
if ( a3 <= v4 )
printf(" ");
else
printf("%02x ", *(unsigned __int8 *)(a1 + (int)v4));
++v4;
}
while ( a2 + 16 != v4 );
return putchar(32);
}

original_code:
void dump_hex(unsigned char *buf, unsigned int whence, unsigned int size)
{
        int i, j, offset;
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 8; j++) {
                        offset = whence + i * 8 + j;
                        if (offset < size) {
                                printf("%02x ", buf[offset]);
                        } else
                                printf("   ");
                }
                putchar(' ');
        }
}

canonical_original_code:
void dump_hex(unsigned char *buf, unsigned int whence, unsigned int size)
{
        int i; int j; int offset;
        for (i = 0; i < 2; i++) {
                for (j = 0; j < 8; j++) {
                        offset = whence + i * 8 + j;
                        if (offset < size) {
                                printf("%02x ", buf[offset]);
                        } else
                                printf("   ");
                }
                putchar(' ');
        }
}

variable_types:
buf
unsigned char *

variable_types:
whence
unsigned int

variable_types:
size
unsigned int

variable_types: j int

variable_types: offset int


decompiled_code:
int __fastcall dump_ascii(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // ebx
char v5; // bp

v3 = a2;
putchar(124);
do
{
if ( v3 < a3 )
{
v5 = *(_BYTE *)(a1 + v3);
if ( ((*_ctype_b_loc())[v5] & 0x4000) == 0 )
v5 = 46;
putchar(v5);
}
++v3;
}
while ( a2 + 16 != v3 );
return puts("|");
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // ebx
char v5; // bp

v3 = a2;
putchar(124);
do
{
if ( v3 < a3 )
{
v5 = *(_BYTE *)(a1 + v3);
if ( ((*_ctype_b_loc())[v5] & 0x4000) == 0 )
v5 = 46;
putchar(v5);
}
++v3;
}
while ( a2 + 16 != v3 );
return puts("|");
}

original_code:
void dump_ascii(unsigned char *buf, unsigned int whence, unsigned int size)
{
        char c;
        int i;
        printf("|");
        for (i = 0; i < 16; i++) {
                if (i + whence < size) {
                        c = buf[i + whence];
                        putchar(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint) ? c : '.');
                }
        }
        printf("|\n");
}

canonical_original_code:
void dump_ascii(unsigned char *buf, unsigned int whence, unsigned int size)
{
        char c;
        int i;
        printf("|");
        for (i = 0; i < 16; i++) {
                if (i + whence < size) {
                        c = buf[i + whence];
                        putchar(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint) ? c : '.');
                }
        }
        printf("|\n");
}

variable_types: c char

variable_types: i int


hexdump

decompiled_code:
int __fastcall hexdump(__int64 a1, unsigned int a2)
{
unsigned int i; // ebx
int result; // eax

if ( a2 )
{
for ( i = 0; i < a2; i += 16 )
{
printf("%04x ", i);
dump_hex(a1, i, a2);
result = dump_ascii(a1, i, a2);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, unsigned int a2)
{
unsigned int i; // ebx
int result; // eax

if ( a2 )
{
for ( i = 0; i < a2; i += 16 )
{
printf("%04x ", i);
func0(a1, i, a2);
result = func1(a1, i, a2);
}
}
return result;
}

original_code:
void hexdump(void *buffer, unsigned int size)
{
        unsigned char *buf = (unsigned char*) buffer;
        int i, j;
        for (i = 0; i < size; i += 16) {
                printf("%04x  ", i);
                dump_hex(buf, i, size);
                dump_ascii(buf, i, size);
        }
}

canonical_original_code:
void hexdump(void *buffer, unsigned int size)
{
        unsigned char *buf = (unsigned char*) buffer;
        int i; int j;
        for (i = 0; i < size; i += 16) {
                printf("%04x  ", i);
                dump_hex(buf, i, size);
                dump_ascii(buf, i, size);
        }
}

variable_types:
buffer
void *


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

v2 = malloc(4uLL);
*v2 = a2;
return enqueue(a1, v2);
}

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

v2 = malloc(4uLL);
*v2 = a2;
return enqueue(a1, v2);
}

original_code:
void add_a_job(struct queue_hdr *job_queue, int i)
{
 int *jobid = (int *) malloc(sizeof(int));
 *jobid = i;
 enqueue(job_queue, jobid);
}

variable_types:
jobid
int *


decompiled_code:
__int64 get_job_from_queue()
{
return dequeue();
}

canonical_decompiled_code:
__int64 func2()
{
return dequeue();
}

original_code:
int *get_job_from_queue(struct queue_hdr *job_queue)
{
 return dequeue(job_queue);
}

return_type: int *


decompiled_code:
void __fastcall check_get_from_queue(__int64 a1, int a2)
{
_DWORD *v2; // rdx
int v3; // eax
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4[0] = 0;
if ( a2 > 0 )
{
do
{
v2 = (_DWORD *)get_elem(a1, v4, queue_cmp);
v3 = v4[0];
if ( *v2 != v4[0] )
_assert_fail("i == *(int *)get_elem(queue,&i,queue_cmp)", "testq.c", 0x50u, "check_get_from_queue");
++v4[0];
}
while ( v3 + 1 < a2 );
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
_DWORD *v2; // rdx
int v3; // eax
int v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v4[0] = 0;
if ( a2 > 0 )
{
do
{
v2 = (_DWORD *)get_elem(a1, v4, queue_cmp);
v3 = v4[0];
if ( *v2 != v4[0] )
_assert_fail("i == *(int *)get_elem(queue,&i,queue_cmp)", "testq.c", 0x50u, "check_get_from_queue");
++v4[0];
}
while ( v3 + 1 < a2 );
}
}

original_code:
void check_get_from_queue(struct queue_hdr *queue, int n)
{
 int i;
 for (i=0; i<n; i++)
  ((void) sizeof ((i == *(int *)get_elem(queue,&i,queue_cmp)) ? 1 : 0), __extension__ ({ if (i == *(int *)get_elem(queue,&i,queue_cmp)) ; else __assert_fail ("i == *(int *)get_elem(queue,&i,queue_cmp)", "testq.c", 80, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: n int


decompiled_code:
__int64 __fastcall check_remove_from_queue(__int64 a1, int a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

result = set_up_job_queue(a1, a2);
v3[0] = 0;
if ( a2 > 0 )
{
do
{
remove_elem(a1, v3, queue_cmp);
if ( get_elem(a1, v3, queue_cmp) )
_assert_fail("get_elem(queue,&i,queue_cmp) == NULL", "testq.c", 0x59u, "check_remove_from_queue");
result = (unsigned int)(v3[0] + 1);
v3[0] = result;
}
while ( (int)result < a2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
__int64 result; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

result = func3(a1, a2);
v3[0] = 0;
if ( a2 > 0 )
{
do
{
remove_elem(a1, v3, queue_cmp);
if ( get_elem(a1, v3, queue_cmp) )
_assert_fail("get_elem(queue,&i,queue_cmp) == NULL", "testq.c", 0x59u, "check_remove_from_queue");
result = (unsigned int)(v3[0] + 1);
v3[0] = result;
}
while ( (int)result < a2 );
}
return result;
}

original_code:
void check_remove_from_queue(struct queue_hdr *queue, int n)
{
 int i;
 set_up_job_queue(queue,n);
 for (i=0; i<n; i++) {
  remove_elem(queue,&i,queue_cmp);
  ((void) sizeof ((get_elem(queue,&i,queue_cmp) == ((void *)0)) ? 1 : 0), __extension__ ({ if (get_elem(queue,&i,queue_cmp) == ((void *)0)) ; else __assert_fail ("get_elem(queue,&i,queue_cmp) == NULL", "testq.c", 89, __extension__ __PRETTY_FUNCTION__); }));
 }
}


decompiled_code:
_BOOL8 __fastcall session_comparator(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

original_code:
int session_comparator(void *target, void *elem)
{
        int target_id;
        int elem_id;
        target_id = ((struct session *) target)->session_id;
        elem_id = ((struct session *) elem)->session_id;
        return elem_id == target_id;
}

variable_types:
target
void *

variable_types:
elem
void *

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


decompiled_code:
void __fastcall sig_handler(int a1)
{
if ( a1 == 2 )
{
puts("\nReceived SIGINT, closing server");
write_server_log("Closed NDMP server\n");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 == 2 )
{
puts("\nReceived SIGINT, closing server");
write_server_log("Closed NDMP server\n");
exit(0);
}
}

original_code:
void sig_handler(int signo)
{
       if (signo == 2){
                printf("\nReceived SIGINT, closing server\n");
                write_server_log("Closed NDMP server\n");
                exit(0);
       }
}

variable_types: signo int


decompiled_code:
__int64 comm_context()
{
__int64 v1; // rbx
__int64 v2; // rbx

if ( !retval_0 )
{
retval_0 = (__int64)calloc(1uLL, 0x38uLL);
*(_QWORD *)(retval_0 + 24) = init_queue();
v1 = retval_0;
*(_QWORD *)(v1 + 32) = init_queue();
v2 = retval_0;
*(_QWORD *)(v2 + 40) = init_queue();
}
return retval_0;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v1; // rbx
__int64 v2; // rbx

if ( !retval_0 )
{
retval_0 = (__int64)calloc(1uLL, 0x38uLL);
*(_QWORD *)(retval_0 + 24) = init_queue();
v1 = retval_0;
*(_QWORD *)(v1 + 32) = init_queue();
v2 = retval_0;
*(_QWORD *)(v2 + 40) = init_queue();
}
return retval_0;
}

original_code:
struct comm_context* comm_context()
{
        static struct comm_context *retval = ((void *)0);
        if (retval == ((void *)0) ) {
                retval = calloc(1, sizeof(struct comm_context));
                retval->sessions = init_queue();
                retval->request_jobs = init_queue();
                retval->reply_jobs = init_queue();
        }
        return retval;
}

canonical_original_code:
struct comm_context *comm_context()
{
        static struct comm_context *retval = ((void *)0);
        if (retval == ((void *)0) ) {
                retval = calloc(1, sizeof(struct comm_context));
                retval->sessions = init_queue();
                retval->request_jobs = init_queue();
                retval->reply_jobs = init_queue();
        }
        return retval;
}

variable_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn { struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *lock; int num_elems; } *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; } *


decompiled_code:
__int64 __fastcall set_fd_flags(void *a1, int a2, __int64 a3)
{
__int64 v4; // r12
__int64 result; // rax
int **v6; // rsi
__int64 v7; // r8
int v8; // ecx

memset(a1, 0, 0x80uLL);
*((_QWORD *)a1 + a2 / 64) |= 1LL << (a2 % 64);
v4 = get_all_elems(*(_QWORD *)(a3 + 24));
result = num_elems(*(_QWORD *)(a3 + 24));
if ( (int)result > 0 )
{
v6 = (int **)v4;
v7 = v4 + 8LL * (unsigned int)(result - 1) + 8;
do
{
v8 = **v6;
result = v8 / 64;
*((_QWORD *)a1 + result) |= 1LL << (v8 % 64);
++v6;
}
while ( v6 != (int **)v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *a1, int a2, __int64 a3)
{
__int64 v4; // r12
__int64 result; // rax
int **v6; // rsi
__int64 v7; // r8
int v8; // ecx

memset(a1, 0, 0x80uLL);
*((_QWORD *)a1 + a2 / 64) |= 1LL << (a2 % 64);
v4 = get_all_elems(*(_QWORD *)(a3 + 24));
result = num_elems(*(_QWORD *)(a3 + 24));
if ( (int)result > 0 )
{
v6 = (int **)v4;
v7 = v4 + 8LL * (unsigned int)(result - 1) + 8;
do
{
v8 = **v6;
result = v8 / 64;
*((_QWORD *)a1 + result) |= 1LL << (v8 % 64);
++v6;
}
while ( v6 != (int **)v7 );
}
return result;
}

original_code:
void set_fd_flags(fd_set *fds, int listener, struct comm_context *ctx)
{
        struct session **active_sessions;
        int n, i;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((fds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((fds)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
        active_sessions = (struct session **) get_all_elems(ctx->sessions);
        n = num_elems(ctx->sessions);
        for (i = 0; i < n; i++) {
                ((void) (((fds)->__fds_bits)[((active_sessions[i]->session_id) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((active_sessions[i]->session_id) % (8 * (int) sizeof (__fd_mask)))))));
        }
}

canonical_original_code:
void set_fd_flags(struct fd_set *fds, int listener, struct comm_context *ctx)
{
        struct session **active_sessions;
        int n; int i;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((fds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((fds)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
        active_sessions = (struct session **) get_all_elems(ctx->sessions);
        n = num_elems(ctx->sessions);
        for (i = 0; i < n; i++) {
                ((void) (((fds)->__fds_bits)[((active_sessions[i]->session_id) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((active_sessions[i]->session_id) % (8 * (int) sizeof (__fd_mask)))))));
        }
}

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

variable_types: listener int

variable_types: struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } **

user_defined_types: struct comm_message { int is_tcp_connect; char message[2048]; int length; }


decompiled_code:
__int64 __fastcall accept_all_connections(int a1, __int64 a2, __int64 a3)
{
int v4; // ebx
__int64 result; // rax
char *v6; // r12
unsigned __int16 v7; // bx
char *v8; // rax
__m128i *v9; // rax
struct in_addr in[4]; // [rsp+0h] [rbp-48h] BYREF
int v11; // [rsp+10h] [rbp-38h]

accept_connection((__m128i *)in, a1);
v4 = v11;
result = 0LL;
if ( v11 != -1 )
{
v6 = (char *)malloc(0x18uLL);
*(_DWORD *)v6 = v4;
*(__m128i *)(v6 + 4) = _mm_loadu_si128((const __m128i *)in);
*((_DWORD *)v6 + 5) = v11;
enqueue(*(_QWORD *)(a3 + 24), v6);
*(_QWORD *)(a2 + 8LL * (v4 / 64)) |= 1LL << (v4 % 64);
if ( *(_DWORD *)(a3 + 48) < v4 )
*(_DWORD *)(a3 + 48) = v4;
write_client_log(*(_QWORD *)&in[0].s_addr, *(_QWORD *)&in[2].s_addr, "Client connection accepted");
write_server_log("Client connection accepted");
v7 = _bswap_16(SHIWORD(in[0].s_addr));
v8 = inet_ntoa(in[1]);
printf("Connection request from %s at %d\n", v8, v7);
v9 = (__m128i *)malloc(0x1028uLL);
*v9 = _mm_loadu_si128((const __m128i *)v6);
v9[1].m128i_i64[0] = *((_QWORD *)v6 + 2);
v9[1].m128i_i32[2] = 1;
enqueue(*(_QWORD *)(a3 + 32), v9);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, __int64 a2, __int64 a3)
{
int v4; // ebx
__int64 result; // rax
char *v6; // r12
unsigned __int16 v7; // bx
char *v8; // rax
__m128i *v9; // rax
struct in_addr in[4]; // [rsp+0h] [rbp-48h] BYREF
int v11; // [rsp+10h] [rbp-38h]

accept_connection((__m128i *)in, a1);
v4 = v11;
result = 0LL;
if ( v11 != -1 )
{
v6 = (char *)malloc(0x18uLL);
*(_DWORD *)v6 = v4;
*(__m128i *)(v6 + 4) = _mm_loadu_si128((const __m128i *)in);
*((_DWORD *)v6 + 5) = v11;
enqueue(*(_QWORD *)(a3 + 24), v6);
*(_QWORD *)(a2 + 8LL * (v4 / 64)) |= 1LL << (v4 % 64);
if ( *(_DWORD *)(a3 + 48) < v4 )
*(_DWORD *)(a3 + 48) = v4;
write_client_log(*(_QWORD *)&in[0].s_addr, *(_QWORD *)&in[2].s_addr, "Client connection accepted");
write_server_log("Client connection accepted");
v7 = func2(SHIWORD(in[0].s_addr));
v8 = inet_ntoa(in[1]);
printf("Connection request from %s at %d\n", v8, v7);
v9 = (__m128i *)malloc(0x1028uLL);
*v9 = _mm_loadu_si128((const __m128i *)v6);
v9[1].m128i_i64[0] = *((_QWORD *)v6 + 2);
v9[1].m128i_i32[2] = 1;
enqueue(*(_QWORD *)(a3 + 32), v9);
result = 1LL;
}
return result;
}

original_code:
int accept_all_connections(int listener, fd_set *read_socks,
                struct comm_context *ctx)
{
        struct client_endpoint endpoint;
        struct session* new_session;
        struct client_txn* txn;
        int num_new_sessions = 0;
        endpoint = accept_connection(listener);
        if (endpoint.fd != -1) {
                new_session = (struct session*) malloc(sizeof(struct session));
                new_session->session_id = endpoint.fd;
                new_session->client_info = endpoint;
                enqueue(ctx->sessions, new_session);
                ((void) (((read_socks)->__fds_bits)[((endpoint.fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((endpoint.fd) % (8 * (int) sizeof (__fd_mask)))))));
                if (endpoint.fd > ctx->maxfds) {
                        ctx->maxfds = endpoint.fd;
                }
                write_client_log(endpoint.client, "Client connection accepted");
                write_server_log("Client connection accepted");
                printf("Connection request from %s at %d\n",
                                inet_ntoa(endpoint.client.sin_addr),
                                ntohs(endpoint.client.sin_port));
                txn = (struct client_txn *) malloc(sizeof(struct client_txn));
                txn->client_session = *new_session;
                txn->request.is_tcp_connect = 1;
                enqueue(ctx->request_jobs, txn);
                num_new_sessions++;
        }
        return num_new_sessions;
}

canonical_original_code:
int accept_all_connections(int listener, struct fd_set *read_socks, struct comm_context *ctx)
{
        struct client_endpoint endpoint;
        struct session *new_session;
        struct client_txn *txn;
        int num_new_sessions = 0;
        endpoint = accept_connection(listener);
        if (endpoint.fd != -1) {
                new_session = (struct session *) malloc(sizeof(struct session));
                new_session->session_id = endpoint.fd;
                new_session->client_info = endpoint;
                enqueue(ctx->sessions, new_session);
                ((void) (((read_socks)->__fds_bits)[((endpoint.fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((endpoint.fd) % (8 * (int) sizeof (__fd_mask)))))));
                if (endpoint.fd > ctx->maxfds) {
                        ctx->maxfds = endpoint.fd;
                }
                write_client_log(endpoint.client, "Client connection accepted");
                write_server_log("Client connection accepted");
                printf("Connection request from %s at %d\n",
                                inet_ntoa(endpoint.client.sin_addr),
                                ntohs(endpoint.client.sin_port));
                txn = (struct client_txn *) malloc(sizeof(struct client_txn));
                txn->client_session = *new_session;
                txn->request.is_tcp_connect = 1;
                enqueue(ctx->request_jobs, txn);
                num_new_sessions++;
        }
        return num_new_sessions;
}

variable_types: struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } *

variable_types: struct client_txn { struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *


decompiled_code:
int __fastcall handle_a_client_request(int fd, __int64 a2)
{
_QWORD *v3; // rbx
_QWORD *v4; // rax
__int64 v5; // rdi
__int64 v6; // rax
int v7; // eax
int result; // eax
_QWORD v9[7]; // [rsp+0h] [rbp-38h] BYREF

LODWORD(v9[0]) = fd;
v3 = malloc(0x1028uLL);
v4 = (_QWORD *)get_elem(*(_QWORD *)(a2 + 24), v9, session_comparator);
v5 = v4[1];
v9[0] = *v4;
v9[1] = v5;
v6 = v4[2];
v9[2] = v6;
*v3 = v9[0];
v3[1] = v5;
v3[2] = v6;
v7 = recv(fd, (char *)v3 + 28, 0x800uLL, 0);
*((_DWORD *)v3 + 519) = v7;
*((_DWORD *)v3 + 6) = 0;
if ( v7 )
{
enqueue(*(_QWORD *)(a2 + 32), v3);
write_server_log("Created a new job on socket at server");
result = printf("\tcreated a new job on %d [%d bytes]\n", (unsigned int)fd, *((unsigned int *)v3 + 519));
}
else
{
close(fd);
remove_elem(*(_QWORD *)(a2 + 24), v9, session_comparator);
(*(void (__fastcall **)(_QWORD))(a2 + 16))((unsigned int)fd);
free(v3);
write_client_log(*(_QWORD *)((char *)v9 + 4), *(_QWORD *)((char *)&v9[1] + 4), "Connection closed by client\n");
result = puts("Detected socket closure");
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd, __int64 a2)
{
_QWORD *v3; // rbx
_QWORD *v4; // rax
__int64 v5; // rdi
__int64 v6; // rax
int v7; // eax
int result; // eax
_QWORD v9[7]; // [rsp+0h] [rbp-38h] BYREF

LODWORD(v9[0]) = fd;
v3 = malloc(0x1028uLL);
v4 = (_QWORD *)get_elem(*(_QWORD *)(a2 + 24), v9, session_comparator);
v5 = v4[1];
v9[0] = *v4;
v9[1] = v5;
v6 = v4[2];
v9[2] = v6;
*v3 = v9[0];
v3[1] = v5;
v3[2] = v6;
v7 = recv(fd, (char *)v3 + 28, 0x800uLL, 0);
*((_DWORD *)v3 + 519) = v7;
*((_DWORD *)v3 + 6) = 0;
if ( v7 )
{
enqueue(*(_QWORD *)(a2 + 32), v3);
write_server_log("Created a new job on socket at server");
result = printf("\tcreated a new job on %d [%d bytes]\n", (unsigned int)fd, *((unsigned int *)v3 + 519));
}
else
{
close(fd);
remove_elem(*(_QWORD *)(a2 + 24), v9, session_comparator);
(*(void (__fastcall **)(_QWORD))(a2 + 16))((unsigned int)fd);
free(v3);
write_client_log(*(_QWORD *)((char *)v9 + 4), *(_QWORD *)((char *)&v9[1] + 4), "Connection closed by client\n");
result = puts("Detected socket closure");
}
return result;
}

original_code:
void handle_a_client_request(int fd, struct comm_context *ctx)
{
        struct session tmp;
        tmp.session_id = fd;
        struct client_txn* txn;
        int n;
        txn = (struct client_txn *) malloc(sizeof(struct client_txn));
        tmp = *(struct session *) get_elem(ctx->sessions, &tmp,
                session_comparator);
        txn->client_session = tmp;
        txn->request.length = recv(fd, txn->request.message, 2048,
                        0);
        txn->request.is_tcp_connect = 0;
        if (txn->request.length == 0) {
                close(fd);
                remove_elem(ctx->sessions, &tmp, session_comparator);
                ctx->terminate_session(fd);
                free(txn);
                write_client_log(tmp.client_info.client, "Connection closed by client\n");
                printf("Detected socket closure\n");
                return;
        }
        enqueue(ctx->request_jobs, txn);
        write_server_log("Created a new job on socket at server");
        printf("\tcreated a new job on %d [%d bytes]\n", fd,
                        txn->request.length);
}

canonical_original_code:
void handle_a_client_request(int fd, struct comm_context *ctx)
{
        struct session tmp;
        tmp.session_id = fd;
        struct client_txn *txn;
        int n;
        txn = (struct client_txn *) malloc(sizeof(struct client_txn));
        tmp = *(struct session *) get_elem(ctx->sessions, &tmp,
                session_comparator);
        txn->client_session = tmp;
        txn->request.length = recv(fd, txn->request.message, 2048,
                        0);
        txn->request.is_tcp_connect = 0;
        if (txn->request.length == 0) {
                close(fd);
                remove_elem(ctx->sessions, &tmp, session_comparator);
                ctx->terminate_session(fd);
                free(txn);
                write_client_log(tmp.client_info.client, "Connection closed by client\n");
                printf("Detected socket closure\n");
                return;
        }
        enqueue(ctx->request_jobs, txn);
        write_server_log("Created a new job on socket at server");
        printf("\tcreated a new job on %d [%d bytes]\n", fd,
                        txn->request.length);
}

variable_types: fd int

variable_types: struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; }


decompiled_code:
void __fastcall handle_client_requests(__int64 a1, __int64 a2)
{
int v2; // ebx

if ( *(int *)(a2 + 48) >= 0 )
{
v2 = 0;
do
{
printf("Checking for event on sock %d\n", (unsigned int)v2);
if ( (*(_QWORD *)(a1 + 8LL * (v2 / 64)) & (1LL << (v2 % 64))) != 0 )
handle_a_client_request(v2, a2);
++v2;
}
while ( *(_DWORD *)(a2 + 48) >= v2 );
}
}

original_code:
void handle_client_requests(fd_set *read_socks, struct comm_context *ctx)
{
        int i;
        for (i = 0; i <= ctx->maxfds; i++) {
                printf("Checking for event on sock %d\n", i);
                if (((((read_socks)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                        handle_a_client_request(i, ctx);
                }
        }
}

canonical_original_code:
void handle_client_requests(struct fd_set *read_socks, struct comm_context *ctx)
{
        int i;
        for (i = 0; i <= ctx->maxfds; i++) {
                printf("Checking for event on sock %d\n", i);
                if (((((read_socks)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                        handle_a_client_request(i, ctx);
                }
        }
}


errExit

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

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

original_code:
int errExit(char *s)
{
        fprintf(stderr, "%s\n", s);
        exit(-1);
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall create_listener(__int16 a1)
{
unsigned int v1; // ebx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

optval = 1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = _bswap_16(a1);
v1 = socket(2, 1, 0);
setsockopt(v1, 1, 2, &optval, 4u);
if ( bind(v1, &addr, 0x10u) == -1 )
errExit("Error:bind");
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1)
{
unsigned int v1; // ebx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

optval = 1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = func2(a1);
v1 = socket(2, 1, 0);
setsockopt(v1, 1, 2, &optval, 4u);
if ( bind(v1, &addr, 0x10u) == -1 )
func8("Error:bind");
return v1;
}

original_code:
int create_listener(int port) {
        int retval;
        struct sockaddr_in listener;
        int reuse = 1;
        listener.sin_family = 2;
        listener.sin_addr.s_addr = ((in_addr_t) 0x00000000);
        listener.sin_port = htons(port);
        retval = socket(2, SOCK_STREAM, 0);
        setsockopt(retval, 1, 2, &reuse, sizeof(int));
        if (bind(retval, (struct sockaddr *) &listener,
                        sizeof(struct sockaddr_in)) == -1)
                errExit("Error:bind");
        return retval;
}

canonical_original_code:
int create_listener(int port) {
        int retval;
        struct sockaddr_in listener;
        int reuse = 1;
        listener.sin_family = 2;
        listener.sin_addr.s_addr = ((unsigned int) 0x00000000);
        listener.sin_port = htons(port);
        retval = socket(2, SOCK_STREAM, 0);
        setsockopt(retval, 1, 2, &reuse, sizeof(int));
        if (bind(retval, (struct sockaddr *) &listener,
                        sizeof(struct sockaddr_in)) == -1)
                errExit("Error:bind");
        return retval;
}

variable_types: port int

variable_types: retval int

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

variable_types: reuse int

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