decompiled_code:
char *__fastcall get_unimplemented_moves(_QWORD **a1, __int64 a2, _DWORD *a3, int a4, unsigned int (__fastcall *a5)(__int64, unsigned __int64 *, _QWORD))
{
int v7; // ebx
unsigned int v8; // er15
signed int v9; // er12
signed int i; // ebx
char *v11; // rax
int v13; // [rsp+Ch] [rbp-5Ch]
char *ptr; // [rsp+10h] [rbp-58h]
unsigned __int64 v16[8]; // [rsp+28h] [rbp-40h] BYREF

v16[0] = 0LL;
v7 = *(_DWORD *)(a2 + 8);
ptr = (char *)calloc(*(_DWORD *)(a2 + 12) * v7, 8uLL);
if ( !ptr )
{
puts("ERROR: unable to init moves list in get_unimplemented_moves.");
return ptr;
}
if ( v7 <= 0 )
{
*a3 = 0;
LABEL_18:
free(ptr);
return 0LL;
}
v8 = a4 == 0 ? -1 : 1;
v13 = 0;
v9 = 0;
do
{
for ( i = 0; *(_DWORD *)(a2 + 12) > i; ++i )
{
v16[0] = __PAIR64__(i, v9);
if ( !(unsigned int)move_in_linked_list(__PAIR64__(i, v9), a1) && a5(a2, v16, v8) && v16[0] == __PAIR64__(i, v9) )
{
v11 = &ptr[8 * v13];
*(_DWORD *)v11 = v9;
*((_DWORD *)v11 + 1) = i;
++v13;
}
}
++v9;
}
while ( *(_DWORD *)(a2 + 8) > v9 );
*a3 = v13;
if ( !v13 )
goto LABEL_18;
ptr = (char *)realloc(ptr, 8LL * v13);
if ( !ptr )
{
puts("ERROR: can't allocate result in get_unimplemented_moves.");
exit(1);
}
return ptr;
}

canonical_decompiled_code:
char *__fastcall func1(_QWORD **a1, __int64 a2, _DWORD *a3, int a4, unsigned int (__fastcall *a5)(__int64, unsigned __int64 *, _QWORD))
{
int v7; // ebx
unsigned int v8; // er15
signed int v9; // er12
signed int i; // ebx
char *v11; // rax
int v13; // [rsp+Ch] [rbp-5Ch]
char *ptr; // [rsp+10h] [rbp-58h]
unsigned __int64 v16[8]; // [rsp+28h] [rbp-40h] BYREF

v16[0] = 0LL;
v7 = *(_DWORD *)(a2 + 8);
ptr = (char *)calloc(*(_DWORD *)(a2 + 12) * v7, 8uLL);
if ( !ptr )
{
puts("ERROR: unable to init moves list in get_unimplemented_moves.");
return ptr;
}
if ( v7 <= 0 )
{
*a3 = 0;
LABEL_18:
free(ptr);
return 0LL;
}
v8 = a4 == 0 ? -1 : 1;
v13 = 0;
v9 = 0;
do
{
for ( i = 0; *(_DWORD *)(a2 + 12) > i; ++i )
{
v16[0] = __PAIR64__(i, v9);
if ( !(unsigned int)move_in_linked_list(__PAIR64__(i, v9), a1) && a5(a2, v16, v8) && v16[0] == __PAIR64__(i, v9) )
{
v11 = &ptr[8 * v13];
*(_DWORD *)v11 = v9;
*((_DWORD *)v11 + 1) = i;
++v13;
}
}
++v9;
}
while ( *(_DWORD *)(a2 + 8) > v9 );
*a3 = v13;
if ( !v13 )
goto LABEL_18;
ptr = (char *)realloc(ptr, 8LL * v13);
if ( !ptr )
{
puts("ERROR: can't allocate result in get_unimplemented_moves.");
exit(1);
}
return ptr;
}

original_code:
Move *get_unimplemented_moves(linked_list *nodes_list, Board *board, int *new_length, int max,
                              int (*is_valid_move)(Board* board, Move* new_move, int value)) {
    int i, j;
    Move *result;
    Move move = {.i = 0, .j = 0};
    int result_index = 0;
    if ((result = (Move*)calloc(board->n*board->m, sizeof(Move))) == ((void *)0)) {
        printf("ERROR: unable to init moves list in get_unimplemented_moves.\n");
        return ((void *)0);
    }
    for (i=0; i < (board->n); i++) {
        for (j=0; j < board->m; j++) {
            move.i = i;
            move.j = j;
            if (!move_in_linked_list(move, nodes_list)) {
                if (is_valid_move(board, &move, max ? 1:-1) &&
                    ((move.i == i) && (move.j == j)) ){
                    result[result_index] = (Move){.i=move.i, .j=move.j};
                    result_index++;
                }
            }
        }
    }
    *new_length = result_index;
    if (result_index == 0) {
        free(result);
        return ((void *)0);
    }
    if ((result = (Move*)(realloc(result, sizeof(Move)*result_index))) == ((void *)0)) {
        printf("ERROR: can't allocate result in get_unimplemented_moves.\n");
        exit(1);
    }
    return result;
}

canonical_original_code:
struct move_s *get_unimplemented_moves(struct linked_list_s *nodes_list, struct board_s *board, int *new_length, int max, int (*is_valid_move)(struct board_s *board, struct move_s *new_move, int value)) {
    int i; int j;
    struct move_s *result;
    struct move_s move = {.i = 0, .j = 0};
    int result_index = 0;
    if ((result = (struct move_s *)calloc(board->n*board->m, sizeof(Move))) == ((void *)0)) {
        printf("ERROR: unable to init moves list in get_unimplemented_moves.\n");
        return ((void *)0);
    }
    for (i=0; i < (board->n); i++) {
        for (j=0; j < board->m; j++) {
            move.i = i;
            move.j = j;
            if (!move_in_linked_list(move, nodes_list)) {
                if (is_valid_move(board, &move, max ? 1:-1) &&
                    ((move.i == i) && (move.j == j)) ){
                    result[result_index] = (struct move_s){.i=move.i, .j=move.j};
                    result_index++;
                }
            }
        }
    }
    *new_length = result_index;
    if (result_index == 0) {
        free(result);
        return ((void *)0);
    }
    if ((result = (struct move_s *)(realloc(result, sizeof(Move)*result_index))) == ((void *)0)) {
        printf("ERROR: can't allocate result in get_unimplemented_moves.\n");
        exit(1);
    }
    return result;
}

variable_types: struct linked_list_s { struct element_s { struct vertex_s { int score; struct move_s *current_move; int value; struct linked_list_s *children; } *node; struct element_s *next; struct element_s *prev; } *head; struct element_s *tail; } *

variable_types: int (*)(struct board_s *board, struct move_s *new_move, int value)

variable_types:
result
struct move_s { int i; int j; } *

variable_types:
move
struct move_s { int i; int j; }

return_type: struct move_s { int i; int j; } *


repo: slallum/TAU-exercises

original_code:
int minmax_with_extend(vertex *node, int depth, int alpha, int beta, int max,
                       Board *board, Move *best_move,
                       int (*is_valid_move)(Board *board, Move *move, int value),
                       int (*make_move)(Board* board, Move* new_move, int value),
                       int (*get_score)(Board* board)) {
    int i;
    int unimplemented_moves_length;
    int current_score;
    Move *next_best_move;
    Board *copied_board = new_board(board->n, board->m);
    if (copied_board == ((void *)0)) {
        printf("ERROR: can't initiazlie copied board.\n");
        exit(1);
    }
    if ((depth == 0) || (node->score == 100000) || (node->score == -100000)) {
        free_board(copied_board);
        return node->score;
    }
    if (node->children == ((void *)0)) {
        if ((node->children = (linked_list*)malloc(sizeof(linked_list))) == ((void *)0)) {
            printf("ERROR: standard function malloc has failed\n");
            exit(1);
        }
        node->children->head = ((void *)0);
        node->children->tail = ((void *)0);
    }
    if ((next_best_move = (Move*)malloc(sizeof(Move))) == ((void *)0)) {
        printf("ERROR: malloc has failed in minmaxtree\n");
        exit(1);
    }
    element *iterator = node->children->head;
    Move *unimplemented_moves = get_unimplemented_moves(node->children, board, &unimplemented_moves_length, max,
                                                        is_valid_move);
    if (iterator != ((void *)0)) {
        best_move->i = iterator->node->current_move->i;
        best_move->j = iterator->node->current_move->j;
    } else {
        if (unimplemented_moves_length > 0) {
            best_move->i = unimplemented_moves[0].i;
            best_move->j = unimplemented_moves[0].j;
        } else {
            free_board(copied_board);
            free(next_best_move);
            free(unimplemented_moves);
            return node->score;
        }
    }
    if (max) {
        while (iterator != ((void *)0)) {
            copy_board(board, copied_board);
            make_move(copied_board, iterator->node->current_move, max ? 1:-1);
            current_score = minmax_with_extend(iterator->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score > alpha) {
                alpha = current_score;
                best_move->i = iterator->node->current_move->i;
                best_move->j = iterator->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return alpha;
                }
            }
            iterator = iterator->next;
        }
        for (i=0; i<unimplemented_moves_length; i++) {
            copy_board(board, copied_board);
            add_node_to_end(node->children, unimplemented_moves[i], copied_board, max ? 1:-1);
            make_move(copied_board, node->children->tail->node->current_move, node->children->tail->node->value);
            node->children->tail->node->score = get_score(copied_board);
            current_score = minmax_with_extend(node->children->tail->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score > alpha) {
                alpha = current_score;
                best_move->i = node->children->tail->node->current_move->i;
                best_move->j = node->children->tail->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return alpha;
                }
            }
        }
        free_board(copied_board);
        free(next_best_move);
        free(unimplemented_moves);
        return alpha;
    } else {
        while (iterator != ((void *)0)) {
            copy_board(board, copied_board);
            make_move(copied_board, iterator->node->current_move, max ? 1:-1);
            current_score = minmax_with_extend(iterator->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score < beta) {
                beta = current_score;
                best_move->i = iterator->node->current_move->i;
                best_move->j = iterator->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return beta;
                }
            }
            iterator = iterator->next;
        }
        for (i=0; i<unimplemented_moves_length; i++) {
            copy_board(board, copied_board);
            add_node_to_end(node->children, unimplemented_moves[i], copied_board, max ? 1:-1);
            make_move(copied_board, node->children->tail->node->current_move, node->children->tail->node->value);
            node->children->tail->node->score = get_score(copied_board);
            current_score = minmax_with_extend(node->children->tail->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score < beta) {
                beta = current_score;
                best_move->i = node->children->tail->node->current_move->i;
                best_move->j = node->children->tail->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return beta;
                }
            }
        }
        free_board(copied_board);
        free(next_best_move);
        free(unimplemented_moves);
        return beta;
    }
}

canonical_original_code:
int minmax_with_extend(struct vertex_s *node, int depth, int alpha, int beta, int max, struct board_s *board, struct move_s *best_move, int (*is_valid_move)(struct board_s *board, struct move_s *move, int value), int (*make_move)(struct board_s *board, struct move_s *new_move, int value), int (*get_score)(struct board_s *board)) {
    int i;
    int unimplemented_moves_length;
    int current_score;
    struct move_s *next_best_move;
    struct board_s *copied_board = new_board(board->n, board->m);
    if (copied_board == ((void *)0)) {
        printf("ERROR: can't initiazlie copied board.\n");
        exit(1);
    }
    if ((depth == 0) || (node->score == 100000) || (node->score == -100000)) {
        free_board(copied_board);
        return node->score;
    }
    if (node->children == ((void *)0)) {
        if ((node->children = (struct linked_list_s *)malloc(sizeof(linked_list))) == ((void *)0)) {
            printf("ERROR: standard function malloc has failed\n");
            exit(1);
        }
        node->children->head = ((void *)0);
        node->children->tail = ((void *)0);
    }
    if ((next_best_move = (struct move_s *)malloc(sizeof(Move))) == ((void *)0)) {
        printf("ERROR: malloc has failed in minmaxtree\n");
        exit(1);
    }
    struct element_s *iterator = node->children->head;
    struct move_s *unimplemented_moves = get_unimplemented_moves(node->children, board, &unimplemented_moves_length, max,
                                                        is_valid_move);
    if (iterator != ((void *)0)) {
        best_move->i = iterator->node->current_move->i;
        best_move->j = iterator->node->current_move->j;
    } else {
        if (unimplemented_moves_length > 0) {
            best_move->i = unimplemented_moves[0].i;
            best_move->j = unimplemented_moves[0].j;
        } else {
            free_board(copied_board);
            free(next_best_move);
            free(unimplemented_moves);
            return node->score;
        }
    }
    if (max) {
        while (iterator != ((void *)0)) {
            copy_board(board, copied_board);
            make_move(copied_board, iterator->node->current_move, max ? 1:-1);
            current_score = minmax_with_extend(iterator->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score > alpha) {
                alpha = current_score;
                best_move->i = iterator->node->current_move->i;
                best_move->j = iterator->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return alpha;
                }
            }
            iterator = iterator->next;
        }
        for (i=0; i<unimplemented_moves_length; i++) {
            copy_board(board, copied_board);
            add_node_to_end(node->children, unimplemented_moves[i], copied_board, max ? 1:-1);
            make_move(copied_board, node->children->tail->node->current_move, node->children->tail->node->value);
            node->children->tail->node->score = get_score(copied_board);
            current_score = minmax_with_extend(node->children->tail->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score > alpha) {
                alpha = current_score;
                best_move->i = node->children->tail->node->current_move->i;
                best_move->j = node->children->tail->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return alpha;
                }
            }
        }
        free_board(copied_board);
        free(next_best_move);
        free(unimplemented_moves);
        return alpha;
    } else {
        while (iterator != ((void *)0)) {
            copy_board(board, copied_board);
            make_move(copied_board, iterator->node->current_move, max ? 1:-1);
            current_score = minmax_with_extend(iterator->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score < beta) {
                beta = current_score;
                best_move->i = iterator->node->current_move->i;
                best_move->j = iterator->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return beta;
                }
            }
            iterator = iterator->next;
        }
        for (i=0; i<unimplemented_moves_length; i++) {
            copy_board(board, copied_board);
            add_node_to_end(node->children, unimplemented_moves[i], copied_board, max ? 1:-1);
            make_move(copied_board, node->children->tail->node->current_move, node->children->tail->node->value);
            node->children->tail->node->score = get_score(copied_board);
            current_score = minmax_with_extend(node->children->tail->node, depth-1, alpha, beta, !max, copied_board, next_best_move,
                                               is_valid_move, make_move, get_score);
            if (current_score < beta) {
                beta = current_score;
                best_move->i = node->children->tail->node->current_move->i;
                best_move->j = node->children->tail->node->current_move->j;
                if (beta <= alpha) {
                    free_board(copied_board);
                    free(next_best_move);
                    free(unimplemented_moves);
                    return beta;
                }
            }
        }
        free_board(copied_board);
        free(next_best_move);
        free(unimplemented_moves);
        return beta;
    }
}

variable_types: struct vertex_s { int score; struct move_s { int i; int j; } *current_move; int value; struct linked_list_s { struct element_s { struct vertex_s *node; struct element_s *next; struct element_s *prev; } *head; struct element_s *tail; } *children; } *

variable_types: depth int

variable_types: alpha int

variable_types: beta int

variable_types: max int

variable_types: struct board_s { int **cells; int n; int m; } *

variable_types: int (*)(struct board_s *board, struct move_s *move, int value)

variable_types: i int

variable_types: struct element_s { struct vertex_s { int score; struct move_s { int i; int j; } *current_move; int value; struct linked_list_s { struct element_s *head; struct element_s *tail; } *children; } *node; struct element_s *next; struct element_s *prev; } *

return_type: int

user_defined_types: struct vertex_s { int score; struct move_s *current_move; int value; struct linked_list_s *children; }


decompiled_code:
void __fastcall remove_tree(void **a1)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
void *v4; // rdi

if ( a1 )
{
v2 = a1[3];
if ( v2 )
{
v3 = (_QWORD *)*v2;
if ( *v2 )
{
do
{
remove_tree(*v3);
v4 = v3;
v3 = (_QWORD *)v3[1];
free(v4);
}
while ( v3 );
}
free(a1[3]);
}
}
free(a1[1]);
free(a1);
}

original_code:
void remove_tree(vertex* current_node) {
    element* previous;
    element* nextush;
    if ((current_node != ((void *)0)) && (current_node->children != ((void *)0))) {
        previous = current_node->children->head;
        while (previous != ((void *)0)) {
            remove_tree(previous->node);
            nextush = previous->next;
            free(previous);
            previous = nextush;
        }
        free(current_node->children);
    }
    free(current_node->current_move);
    free(current_node);
}

canonical_original_code:
void remove_tree(struct vertex_s *current_node) {
    struct element_s *previous;
    struct element_s *nextush;
    if ((current_node != ((void *)0)) && (current_node->children != ((void *)0))) {
        previous = current_node->children->head;
        while (previous != ((void *)0)) {
            remove_tree(previous->node);
            nextush = previous->next;
            free(previous);
            previous = nextush;
        }
        free(current_node->children);
    }
    free(current_node->current_move);
    free(current_node);
}

return_type: void

user_defined_types: struct element_s { struct vertex_s *node; struct element_s *next; struct element_s *prev; }

user_defined_types: struct linked_list_s { struct element_s *head; struct element_s *tail; }


original_code:
void update_tree(minmax_tree *tree, int col, int row, int depth) {
    element* iterator;
    vertex* current_root = tree->root;
    if (current_root->children != ((void *)0)) {
        iterator = current_root->children->head;
        while ((iterator != ((void *)0)) &&
               (!((iterator->node->current_move->j == col) &&
                  (iterator->node->current_move->i == row) ))) {
            iterator = iterator->next;
        }
        if (iterator != ((void *)0)) {
            tree->root = iterator->node;
            if (iterator->prev != ((void *)0)) {
                iterator->prev->next = iterator->next;
            } else {
                current_root->children->head = iterator->next;
            }
            if (iterator->next != ((void *)0)) {
                iterator->next->prev = iterator->prev;
            } else {
                current_root->children->tail = iterator->prev;
            }
            remove_tree(current_root);
            free(iterator);
        }
    } else {
        tree->root->current_move->i = row;
        tree->root->current_move->j = col;
        tree->root->value = (-1) * (tree->root->value);
    }
 }

canonical_original_code:
void update_tree(struct minmax_tree_s *tree, int col, int row, int depth) {
    struct element_s *iterator;
    struct vertex_s *current_root = tree->root;
    if (current_root->children != ((void *)0)) {
        iterator = current_root->children->head;
        while ((iterator != ((void *)0)) &&
               (!((iterator->node->current_move->j == col) &&
                  (iterator->node->current_move->i == row) ))) {
            iterator = iterator->next;
        }
        if (iterator != ((void *)0)) {
            tree->root = iterator->node;
            if (iterator->prev != ((void *)0)) {
                iterator->prev->next = iterator->next;
            } else {
                current_root->children->head = iterator->next;
            }
            if (iterator->next != ((void *)0)) {
                iterator->next->prev = iterator->prev;
            } else {
                current_root->children->tail = iterator->prev;
            }
            remove_tree(current_root);
            free(iterator);
        }
    } else {
        tree->root->current_move->i = row;
        tree->root->current_move->j = col;
        tree->root->value = (-1) * (tree->root->value);
    }
 }

variable_types: struct minmax_tree_s { struct vertex_s { int score; struct move_s { int i; int j; } *current_move; int value; struct linked_list_s { struct element_s { struct vertex_s *node; struct element_s *next; struct element_s *prev; } *head; struct element_s *tail; } *children; } *root; } *

variable_types: col int

variable_types: row int

user_defined_types: struct minmax_tree_s { struct vertex_s *root; }


decompiled_code:
__int64 __fastcall get_score(__int64 a1)
{
return (*(__int64 (__fastcall **)(_QWORD, _QWORD, _QWORD))(a1 + 16))(
*(_QWORD *)a1,
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 12));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return (*(__int64 (__fastcall **)(_QWORD, _QWORD, _QWORD))(a1 + 16))(
*(_QWORD *)a1,
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 12));
}

original_code:
int get_score(board_t *board) {
 return (*board->scoring_func)(board->cells, board->n, board->m);
}

canonical_original_code:
int get_score(struct Board *board) {
 return (*board->scoring_func)(board->cells, board->n, board->m);
}

variable_types: struct Board { int **cells; int n; int m; int (*scoring_func)(int **, int, int); int (*make_move)(int **, int, int, int); } *


decompiled_code:
__int64 __fastcall execute_move(__int64 a1)
{
return (*(__int64 (__fastcall **)(_QWORD))(a1 + 24))(*(_QWORD *)a1);
}

original_code:
int execute_move(board_t* board, int n, int i, int value) {
 return (*board->make_move)(board->cells, n, i, value);
}

canonical_original_code:
int execute_move(struct Board *board, int n, int i, int value) {
 return (*board->make_move)(board->cells, n, i, value);
}

variable_types: n int

variable_types: value int

user_defined_types: struct Board { int **cells; int n; int m; int (*scoring_func)(int **, int, int); int (*make_move)(int **, int, int, int); }


decompiled_code:
__int64 __fastcall board_is_full(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, unsigned int **a7)
{
unsigned int *v7; // rax
unsigned int v8; // edx

if ( a1 <= 0 )
return 1;
v7 = *a7;
while ( 1 )
{
v8 = *v7;
if ( !*v7 )
break;
if ( ++v7 == &(*a7)[a1 - 1 + 1] )
return 1;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, unsigned int **a7)
{
unsigned int *v7; // rax
unsigned int v8; // edx

if ( a1 <= 0 )
return 1;
v7 = *a7;
while ( 1 )
{
v8 = *v7;
if ( !*v7 )
break;
if ( ++v7 == &(*a7)[a1 - 1 + 1] )
return 1;
}
return v8;
}

original_code:
int board_is_full(board_t board, int m) {
 int i;
 for (i=0; i<m; i++) {
  if (board.cells[0][i] == 0) {
   return 0;
  }
 }
 return 1;
}

canonical_original_code:
int board_is_full(struct Board board, int m) {
 int i;
 for (i=0; i<m; i++) {
  if (board.cells[0][i] == 0) {
   return 0;
  }
 }
 return 1;
}

variable_types: struct Board { int **cells; int n; int m; int (*scoring_func)(int **, int, int); int (*make_move)(int **, int, int, int); }

variable_types: m int


original_code:
void print_board(board_t *board) {
 int i, j;
 for (i = 0; i < board->n; i++) {
  printf("|");
  for (j = 0; j < board->m; j++) {
   if (board->cells[i][j] == -1) {
    printf(" %c", 'X');
   } else if (board->cells[i][j] == 1) {
    printf(" %c", 'O');
   } else {
    printf("  ");
   }
  }
  printf(" |\n");
 }
 for (i = 0; i < 17; i++) {
  printf("-");
 }
 printf("\n  ");
 for (i = 1; i <= board->m; i++) {
  printf("%d ", i);
 }
 printf(" \n");
}

canonical_original_code:
void print_board(struct Board *board) {
 int i; int j;
 for (i = 0; i < board->n; i++) {
  printf("|");
  for (j = 0; j < board->m; j++) {
   if (board->cells[i][j] == -1) {
    printf(" %c", 'X');
   } else if (board->cells[i][j] == 1) {
    printf(" %c", 'O');
   } else {
    printf("  ");
   }
  }
  printf(" |\n");
 }
 for (i = 0; i < 17; i++) {
  printf("-");
 }
 printf("\n  ");
 for (i = 1; i <= board->m; i++) {
  printf("%d ", i);
 }
 printf(" \n");
}

variable_types: j int


decompiled_code:
__int64 __fastcall only_whitespaces(_BYTE *a1)
{
char v1; // bl
_BYTE *v2; // rbp
const unsigned __int16 *v3; // rax

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = a1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x2000) != 0 )
{
v1 = *++v2;
if ( !*v2 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
char v1; // bl
_BYTE *v2; // rbp
const unsigned __int16 *v3; // rax

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = a1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x2000) != 0 )
{
v1 = *++v2;
if ( !*v2 )
return 1LL;
}
return 0LL;
}

original_code:
static int only_whitespaces(char *str) {
    while (*str != '\0') {
        if (!((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short int) _ISspace)) {
            return 0;
        }
        str ++;
    }
    return 1;
}

canonical_original_code:
static int only_whitespaces(char *str) {
    while (*str != '\0') {
        if (!((*__ctype_b_loc ())[(int) ((*str))] & (unsigned short) _ISspace)) {
            return 0;
        }
        str ++;
    }
    return 1;
}

variable_types:
str
char *


decompiled_code:
char *get_command_line()
{
__int64 v0; // rbx
char v2[56]; // [rsp+0h] [rbp-38h] BYREF

while ( fgets(v2, 42, stdin) )
{
if ( v2[40] != 10 && strlen(v2) == 41 )
{
printf("Error: command length must be less than or equal to %d characters\n", 40LL);
while ( getchar() != 10 )
;
}
else
{
if ( !(unsigned int)only_whitespaces(v2) )
return v2;
puts("Error: command line must have at least one non-whitespace character");
}
}
v0 = 0LL;
perror("Error: standard function fgets has failed");
return (char *)v0;
}

canonical_decompiled_code:
char *func1()
{
__int64 v0; // rbx
char v2[56]; // [rsp+0h] [rbp-38h] BYREF

while ( fgets(v2, 42, stdin) )
{
if ( v2[40] != 10 && strlen(v2) == 41 )
{
printf("Error: command length must be less than or equal to %d characters\n", 40LL);
while ( getchar() != 10 )
;
}
else
{
if ( !(unsigned int)func0(v2) )
return v2;
puts("Error: command line must have at least one non-whitespace character");
}
}
v0 = 0LL;
perror("Error: standard function fgets has failed");
return (char *)v0;
}

original_code:
char *get_command_line() {
    char command_line[42];
    char *res;
    while(1)
    {
        if (fgets(command_line,sizeof(command_line),stdin)==((void *)0))
        {
            perror("Error: standard function fgets has failed");
            return ((void *)0);
        }
        if (command_line[sizeof(command_line)-2]!='\n'&&strlen(command_line)==40 +1)
        {
            printf("Error: command length must be less than or equal to %d characters\n",40);
            while(getchar()!='\n'){}
            continue;
        }
        if (only_whitespaces(command_line))
        {
            printf("Error: command line must have at least one non-whitespace character\n");
            continue;
        }
        res = &command_line[0];
        return res;
    }
}

variable_types:
res
char *

return_type: char *


original_code:
int str_to_int(char *str, int start) {
    int i = start;
    int res = 0;
    while ((48 <= str[i] && str[i] <= 57) || ((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace)) {
        if (!((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short int) _ISspace)) {
            res = res * 10 + (str[i] - 48);
        }
        i++;
    }
    return res;
}

canonical_original_code:
int str_to_int(char *str, int start) {
    int i = start;
    int res = 0;
    while ((48 <= str[i] && str[i] <= 57) || ((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace)) {
        if (!((*__ctype_b_loc ())[(int) ((str[i]))] & (unsigned short) _ISspace)) {
            res = res * 10 + (str[i] - 48);
        }
        i++;
    }
    return res;
}

variable_types: start int

variable_types: res int


decompiled_code:
__int64 __fastcall make_connect4_move(_QWORD *a1, int a2, int a3, int a4)
{
__int64 v5; // r8
__int64 v6; // rdi
_QWORD *v7; // rdx
int v8; // eax

if ( a2 <= 0 )
{
v8 = 0;
}
else
{
v5 = a3;
v6 = 4LL * a3;
v7 = a1;
v8 = 0;
while ( !*(_DWORD *)(*v7 + v6) )
{
++v8;
++v7;
if ( a2 == v8 )
goto LABEL_5;
}
if ( v8 )
{
LABEL_5:
*(_DWORD *)(a1[v8 - 1] + 4 * v5) = a4;
return (unsigned int)(v8 - 1);
}
}
return (unsigned int)(v8 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, int a2, int a3, int a4)
{
__int64 v5; // r8
__int64 v6; // rdi
_QWORD *v7; // rdx
int v8; // eax

if ( a2 <= 0 )
{
v8 = 0;
}
else
{
v5 = a3;
v6 = 4LL * a3;
v7 = a1;
v8 = 0;
while ( !*(_DWORD *)(*v7 + v6) )
{
++v8;
++v7;
if ( a2 == v8 )
goto LABEL_5;
}
if ( v8 )
{
LABEL_5:
*(_DWORD *)(a1[v8 - 1] + 4 * v5) = a4;
return (unsigned int)(v8 - 1);
}
}
return (unsigned int)(v8 - 1);
}

original_code:
int make_connect4_move(int** cells, int n, int i, int value) {
 int j = 0;
 while ((j < n) && (cells[j][i] == 0)) {
  j++;
 }
 if (j != 0) {
  cells[j - 1][i] = value;
 }
 return j - 1;
}

canonical_original_code:
int make_connect4_move(int **cells, int n, int i, int value) {
 int j = 0;
 while ((j < n) && (cells[j][i] == 0)) {
  j++;
 }
 if (j != 0) {
  cells[j - 1][i] = value;
 }
 return j - 1;
}

variable_types:
cells
int **


decompiled_code:
__int64 __fastcall new_game(__int64 a1, unsigned int a2)
{
_QWORD *v2; // rax
_QWORD *v3; // r12
_QWORD *v4; // rbx
_QWORD *v5; // r13
void *v6; // rax
_QWORD *v7; // rax
__int64 v8; // rax
__int64 result; // rax
__int64 v10; // rax

v2 = calloc(6uLL, 8uLL);
if ( v2 )
{
v3 = v2;
v4 = v2;
v5 = v2 + 6;
do
{
v6 = calloc(7uLL, 4uLL);
*v4 = v6;
if ( !v6 )
goto LABEL_10;
++v4;
}
while ( v4 != v5 );
*(_QWORD *)a1 = v3;
*(_DWORD *)(a1 + 8) = 6;
*(_DWORD *)(a1 + 12) = 7;
*(_QWORD *)(a1 + 16) = &connect4_scoring;
*(_QWORD *)(a1 + 24) = make_connect4_move;
*(_DWORD *)(a1 + 32) = 0;
v7 = *(_QWORD **)(a1 + 40);
if ( v7 )
remove_tree(*v7);
if ( a2 == -1 )
{
v10 = create_tree(a1, 1LL);
*(_QWORD *)(a1 + 40) = v10;
if ( v10 )
goto LABEL_9;
result = 0LL;
}
else
{
v8 = create_tree(a1, a2);
*(_QWORD *)(a1 + 40) = v8;
if ( v8 )
{
LABEL_9:
*(_DWORD *)(a1 + 48) = a2;
*(_DWORD *)(a1 + 52) = 0;
return 1LL;
}
result = 0LL;
}
}
else
{
LABEL_10:
perror("Error: standard function malloc has failed");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2)
{
_QWORD *v2; // rax
_QWORD *v3; // r12
_QWORD *v4; // rbx
_QWORD *v5; // r13
void *v6; // rax
_QWORD *v7; // rax
__int64 v8; // rax
__int64 result; // rax
__int64 v10; // rax

v2 = calloc(6uLL, 8uLL);
if ( v2 )
{
v3 = v2;
v4 = v2;
v5 = v2 + 6;
do
{
v6 = calloc(7uLL, 4uLL);
*v4 = v6;
if ( !v6 )
goto LABEL_10;
++v4;
}
while ( v4 != v5 );
*(_QWORD *)a1 = v3;
*(_DWORD *)(a1 + 8) = 6;
*(_DWORD *)(a1 + 12) = 7;
*(_QWORD *)(a1 + 16) = &connect4_scoring;
*(_QWORD *)(a1 + 24) = make_connect4_move;
*(_DWORD *)(a1 + 32) = 0;
v7 = *(_QWORD **)(a1 + 40);
if ( v7 )
remove_tree(*v7);
if ( a2 == -1 )
{
v10 = create_tree(a1, 1LL);
*(_QWORD *)(a1 + 40) = v10;
if ( v10 )
goto LABEL_9;
result = 0LL;
}
else
{
v8 = create_tree(a1, a2);
*(_QWORD *)(a1 + 40) = v8;
if ( v8 )
{
LABEL_9:
*(_DWORD *)(a1 + 48) = a2;
*(_DWORD *)(a1 + 52) = 0;
return 1LL;
}
result = 0LL;
}
}
else
{
LABEL_10:
perror("Error: standard function malloc has failed");
result = 0LL;
}
return result;
}

original_code:
int new_game(game *current_game, int depth) {
    int **cells;
    int i;
    if ((cells = (int**)calloc(6, sizeof(int*))) == ((void *)0)) {
        perror("Error: standard function malloc has failed");
        return 0;
    }
    for (i=0; i<6; i++) {
        if ((cells[i] = (int*)calloc(7, sizeof(int))) == ((void *)0)) {
            perror("Error: standard function malloc has failed");
            return 0;
        }
    }
    board_t current_board = { cells, 6, 7, &connect4_scoring, &make_connect4_move };
    current_game->current_board = current_board;
    current_game->is_comp_turn = 0;
    if (current_game->tree != ((void *)0)) {
     remove_tree(current_game->tree->root);
    }
    if (depth == -1) {
     if ((current_game->tree = create_tree(&(current_game->current_board), 1)) == ((void *)0)) {
   return 0;
  }
    } else {
     if ((current_game->tree = create_tree(&(current_game->current_board), depth)) == ((void *)0)) {
   return 0;
  }
    }
    current_game->depth=depth;
    current_game->game_over=0;
    return 1;
}

canonical_original_code:
int new_game(struct game_s *current_game, int depth) {
    int **cells;
    int i;
    if ((cells = (int **)calloc(6, sizeof(int *))) == ((void *)0)) {
        perror("Error: standard function malloc has failed");
        return 0;
    }
    for (i=0; i<6; i++) {
        if ((cells[i] = (int *)calloc(7, sizeof(int))) == ((void *)0)) {
            perror("Error: standard function malloc has failed");
            return 0;
        }
    }
    struct Board current_board = { cells, 6, 7, &connect4_scoring, &make_connect4_move };
    current_game->current_board = current_board;
    current_game->is_comp_turn = 0;
    if (current_game->tree != ((void *)0)) {
     remove_tree(current_game->tree->root);
    }
    if (depth == -1) {
     if ((current_game->tree = create_tree(&(current_game->current_board), 1)) == ((void *)0)) {
   return 0;
  }
    } else {
     if ((current_game->tree = create_tree(&(current_game->current_board), depth)) == ((void *)0)) {
   return 0;
  }
    }
    current_game->depth=depth;
    current_game->game_over=0;
    return 1;
}

variable_types: struct game_s { struct Board { int **cells; int n; int m; int (*scoring_func)(int **, int, int); int (*make_move)(int **, int, int, int); } current_board; int is_comp_turn; struct minmax_tree_s { struct vertex_s { int score; int column_num; int value; struct linked_list_s { struct element_s { struct vertex_s *node; struct element_s *next; struct element_s *prev; } *head; struct element_s *tail; } *children; } *root; int (*make_move)(int **, int, int, int); int (*scoring_func)(int **, int, int); } *tree; int depth; int game_over; } *

variable_types: depth int

user_defined_types: struct game_s { struct Board current_board; int is_comp_turn; struct minmax_tree_s *tree; int depth; int game_over; }

user_defined_types: struct minmax_tree_s { struct vertex_s *root; int (*make_move)(int **, int, int, int); int (*scoring_func)(int **, int, int); }

user_defined_types: struct vertex_s { int score; int column_num; int value; struct linked_list_s *children; }


original_code:
int won_board(board_t board) {
    int score = connect4_scoring(board.cells, 6, 7);
    if (score == 100000 || score == -100000) {
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
int won_board(struct Board board) {
    int score = connect4_scoring(board.cells, 6, 7);
    if (score == 100000 || score == -100000) {
        return 1;
    } else {
        return 0;
    }
}

variable_types: score int


decompiled_code:
__int64 __fastcall get_computer_move(__int64 a1)
{
return get_best_coloumn(**(_QWORD **)(a1 + 40), *(_DWORD *)(a1 + 48), 0);
}

original_code:
int get_computer_move(game *current_game) {
    return get_best_coloumn(current_game->tree->root, current_game->depth, 0);
}

canonical_original_code:
int get_computer_move(struct game_s *current_game) {
    return get_best_coloumn(current_game->tree->root, current_game->depth, 0);
}


decompiled_code:
__int64 __fastcall get_best_move_for_player(__int64 a1)
{
return get_best_coloumn(**(_QWORD **)(a1 + 40), *(_DWORD *)(a1 + 48), 1);
}

original_code:
int get_best_move_for_player(game *current_game) {
 return get_best_coloumn(current_game->tree->root, current_game->depth, 1);
}

canonical_original_code:
int get_best_move_for_player(struct game_s *current_game) {
 return get_best_coloumn(current_game->tree->root, current_game->depth, 1);
}


decompiled_code:
__int64 __fastcall check_game(const char *a1)
{
bool v1; // cf
bool v2; // zf
const char *v4; // rdi
__int64 v5; // rcx
const char *v6; // rsi
char v7; // dl
bool v8; // cf
bool v9; // zf
const char *v10; // rdi
__int64 v11; // rcx
const char *v12; // rsi
char v13; // dl
bool v14; // cf
bool v15; // zf
const char *v16; // rdi
__int64 v17; // rcx
const char *v18; // rsi
__int64 result; // rax

v1 = 0;
v2 = 0;
v4 = "Connect4";
v5 = 9LL;
v6 = a1;
do
{
if ( !v5 )
break;
v1 = *v6 < (unsigned int)*v4;
v2 = *v6++ == *v4++;
--v5;
}
while ( v2 );
v7 = (!v1 && !v2) - v1;
v8 = 0;
v9 = v7 == 0;
if ( !v7 )
return connect4_new_game(v4, v6);
v10 = "Reversi";
v11 = 8LL;
v12 = a1;
do
{
if ( !v11 )
break;
v8 = *v12 < (unsigned int)*v10;
v9 = *v12++ == *v10++;
--v11;
}
while ( v9 );
v13 = (!v8 && !v9) - v8;
v14 = 0;
v15 = v13 == 0;
if ( !v13 )
return reversi_new_game(v10, v12);
v16 = "Tic-tac-toe";
v17 = 12LL;
v18 = a1;
do
{
if ( !v17 )
break;
v14 = *v18 < (unsigned int)*v16;
v15 = *v18++ == *v16++;
--v17;
}
while ( v15 );
result = 0LL;
if ( (!v14 && !v15) == v14 )
result = ttt_new_game(v16, v18);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
bool v1; // cf
bool v2; // zf
const char *v4; // rdi
__int64 v5; // rcx
const char *v6; // rsi
char v7; // dl
bool v8; // cf
bool v9; // zf
const char *v10; // rdi
__int64 v11; // rcx
const char *v12; // rsi
char v13; // dl
bool v14; // cf
bool v15; // zf
const char *v16; // rdi
__int64 v17; // rcx
const char *v18; // rsi
__int64 result; // rax

v1 = 0;
v2 = 0;
v4 = "Connect4";
v5 = 9LL;
v6 = a1;
do
{
if ( !v5 )
break;
v1 = *v6 < (unsigned int)*v4;
v2 = *v6++ == *v4++;
--v5;
}
while ( v2 );
v7 = (!v1 && !v2) - v1;
v8 = 0;
v9 = v7 == 0;
if ( !v7 )
return connect4_new_game(v4, v6);
v10 = "Reversi";
v11 = 8LL;
v12 = a1;
do
{
if ( !v11 )
break;
v8 = *v12 < (unsigned int)*v10;
v9 = *v12++ == *v10++;
--v11;
}
while ( v9 );
v13 = (!v8 && !v9) - v8;
v14 = 0;
v15 = v13 == 0;
if ( !v13 )
return reversi_new_game(v10, v12);
v16 = "Tic-tac-toe";
v17 = 12LL;
v18 = a1;
do
{
if ( !v17 )
break;
v14 = *v18 < (unsigned int)*v16;
v15 = *v18++ == *v16++;
--v17;
}
while ( v15 );
result = 0LL;
if ( (!v14 && !v15) == v14 )
result = ttt_new_game(v16, v18);
return result;
}

original_code:
Game* check_game(char game_name[80]) {
 if (strcmp(game_name, "Connect4") == 0) {
  return connect4_new_game();
 }
 if (strcmp(game_name, "Reversi") == 0) {
  return reversi_new_game();
 }
 if (strcmp(game_name, "Tic-tac-toe") == 0) {
  return ttt_new_game();
 }
 return ((void *)0);
}

canonical_original_code:
struct game_s *check_game(char game_name[80]) {
 if (strcmp(game_name, "Connect4") == 0) {
  return connect4_new_game();
 }
 if (strcmp(game_name, "Reversi") == 0) {
  return reversi_new_game();
 }
 if (strcmp(game_name, "Tic-tac-toe") == 0) {
  return ttt_new_game();
 }
 return ((void *)0);
}

return_type: struct game_s { struct board_s { int **cells; int n; int m; } *board; char *save_game_name; int current_player; int game_over; int first_player_ai; int second_player_ai; int first_player_depth; int second_player_depth; int depth_range[2]; void (*init_board)(struct board_s *board); int (*is_valid_move)(struct board_s *board, struct move_s { int i; int j; } *move, int value); int (*make_move)(struct board_s *board, struct move_s *new_move, int value); int (*won_game)(struct game_s *); int (*get_score)(struct board_s *board); struct minmax_tree_s { struct vertex_s { int score; struct move_s *current_move; int value; struct linked_list_s { struct element_s { struct vertex_s *node; struct element_s *next; struct element_s *prev; } *head; struct element_s *tail; } *children; } *root; } *tree; char *tiles[4]; } *

user_defined_types: struct game_s { struct board_s *board; char *save_game_name; int current_player; int game_over; int first_player_ai; int second_player_ai; int first_player_depth; int second_player_depth; int depth_range[2]; void (*init_board)(struct board_s *board); int (*is_valid_move)(struct board_s *board, struct move_s *move, int value); int (*make_move)(struct board_s *board, struct move_s *new_move, int value); int (*won_game)(struct game_s *); int (*get_score)(struct board_s *board); struct minmax_tree_s *tree; char *tiles[4]; }


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

result = 1LL;
if ( a1 <= 0 )
{
puts("ERROR: Game file not in expected format");
result = 0LL;
}
return result;
}

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

result = 1LL;
if ( a1 <= 0 )
{
puts("ERROR: Game file not in expected format");
result = 0LL;
}
return result;
}

original_code:
int check_validity(int rc) {
 if (rc <= 0) {
  printf("ERROR: Game file not in expected format\n");
  return 0;
 }
 return 1;
}

variable_types: rc int


decompiled_code:
__int64 init_fw()
{
__int64 result; // rax
const char *v1; // rax

if ( (int)SDL_Init(32LL) < 0 )
{
v1 = (const char *)SDL_GetError();
printf("ERROR: Unable to init SDL: %s\n", v1);
result = 0LL;
}
else
{
atexit((void (*)(void))&SDL_Quit);
TTF_Init();
atexit((void (*)(void))&TTF_Quit);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
const char *v1; // rax

if ( (int)SDL_Init(32LL) < 0 )
{
v1 = (const char *)SDL_GetError();
printf("ERROR: Unable to init SDL: %s\n", v1);
result = 0LL;
}
else
{
atexit((void (*)(void))&SDL_Quit);
TTF_Init();
atexit((void (*)(void))&TTF_Quit);
result = 1LL;
}
return result;
}

original_code:
int init_fw() {
 if (SDL_Init(0x00000020) < 0) {
  printf("ERROR: Unable to init SDL: %s\n", SDL_GetError());
  return 0;
 }
 atexit(SDL_Quit);
 TTF_Init();
 atexit(TTF_Quit);
 return 1;
}


original_code:
void count_horizontal(int *spans_count, int** board, int n, int m) {
 int i, j;
 int result = 0;
 for (i = 0; i < n; i++) {
  result = board[i][0] + board[i][1] + board[i][2] + board[i][3];
  spans_count[result + 4] += 1;
  for (j = 4; j < m; j++) {
   result -= board[i][j - 4];
   result += board[i][j];
   spans_count[result + 4] += 1;
  }
 }
}

canonical_original_code:
void count_horizontal(int *spans_count, int **board, int n, int m) {
 int i; int j;
 int result = 0;
 for (i = 0; i < n; i++) {
  result = board[i][0] + board[i][1] + board[i][2] + board[i][3];
  spans_count[result + 4] += 1;
  for (j = 4; j < m; j++) {
   result -= board[i][j - 4];
   result += board[i][j];
   spans_count[result + 4] += 1;
  }
 }
}

variable_types:
board
int **

variable_types: i int

variable_types: result int


original_code:
void count_vertical(int *spans_count, int** board, int n, int m) {
 int i, j;
 int result = 0;
 for (i = 0; i < m; i++) {
  result = board[0][i] + board[1][i] + board[2][i] + board[3][i];
  spans_count[result + 4] += 1;
  for (j = 4; j < n; j++) {
   result -= board[j - 4][i];
   result += board[j][i];
   spans_count[result + 4] += 1;
  }
 }
}

canonical_original_code:
void count_vertical(int *spans_count, int **board, int n, int m) {
 int i; int j;
 int result = 0;
 for (i = 0; i < m; i++) {
  result = board[0][i] + board[1][i] + board[2][i] + board[3][i];
  spans_count[result + 4] += 1;
  for (j = 4; j < n; j++) {
   result -= board[j - 4][i];
   result += board[j][i];
   spans_count[result + 4] += 1;
  }
 }
}

variable_types: n int


original_code:
void count_diagonal(int *spans_count, int** board, int n, int m) {
 int i, j;
 int result = 0;
 for (i = 0; i < n - 3; i++) {
  for (j = 0; j < m - 3; j++) {
   result = board[i][j] + board[i+1][j+1] + board[i+2][j+2] + board[i+3][j+3];
   spans_count[result + 4] += 1;
  }
 }
 result = 0;
 for (i = 0; i < n - 3; i++) {
  for (j = 3; j < m; j++) {
   result = board[i][j] + board[i+1][j-1] + board[i+2][j-2] + board[i+3][j-3];
   spans_count[result + 4] += 1;
  }
 }
}

canonical_original_code:
void count_diagonal(int *spans_count, int **board, int n, int m) {
 int i; int j;
 int result = 0;
 for (i = 0; i < n - 3; i++) {
  for (j = 0; j < m - 3; j++) {
   result = board[i][j] + board[i+1][j+1] + board[i+2][j+2] + board[i+3][j+3];
   spans_count[result + 4] += 1;
  }
 }
 result = 0;
 for (i = 0; i < n - 3; i++) {
  for (j = 3; j < m; j++) {
   result = board[i][j] + board[i+1][j-1] + board[i+2][j-2] + board[i+3][j-3];
   spans_count[result + 4] += 1;
  }
 }
}


repo: slashbeast/pax-utils

decompiled_code:
int *__fastcall ar_open_fd(__int64 a1, int a2)
{
ssize_t v2; // rdx
int *result; // rax
char buf[15]; // [rsp+9h] [rbp-Fh] BYREF

qword_15A8 = a1;
ret_2 = a2;
qword_15B0 = 0LL;
qword_15B8 = 0LL;
v2 = read(a2, buf, 7uLL);
result = 0LL;
if ( v2 == 7 )
{
result = &ret_2;
if ( memcmp(buf, "!<arch>", 7uLL) )
result = 0LL;
}
return result;
}

canonical_decompiled_code:
int *__fastcall func0(__int64 a1, int a2)
{
ssize_t v2; // rdx
int *result; // rax
char buf[15]; // [rsp+9h] [rbp-Fh] BYREF

qword_15A8 = a1;
ret_2 = a2;
qword_15B0 = 0LL;
qword_15B8 = 0LL;
v2 = read(a2, buf, 7uLL);
result = 0LL;
if ( v2 == 7 )
{
result = &ret_2;
if ( memcmp(buf, "!<arch>", 7uLL) )
result = 0LL;
}
return result;
}

original_code:
archive_handle *ar_open_fd(const char *filename, int fd)
{
 static archive_handle ret;
 char buf[(sizeof("!<arch>")-1)];
 ret.filename = filename;
 ret.fd = fd;
 ret.skip = 0;
 ret.extfn = ((void *)0);
 if (read(ret.fd, buf, (sizeof("!<arch>")-1)) != (sizeof("!<arch>")-1))
  return ((void *)0);
 if (strncmp(buf, "!<arch>", (sizeof("!<arch>")-1)))
  return ((void *)0);
 return &ret;
}

canonical_original_code:
struct archive_handle *ar_open_fd(char *filename, int fd)
{
 static struct archive_handle ret;
 char buf[-1];
 ret.filename = filename;
 ret.fd = fd;
 ret.skip = 0;
 ret.extfn = ((void *)0);
 if (read(ret.fd, buf, (sizeof("!<arch>")-1)) != (sizeof("!<arch>")-1))
  return ((void *)0);
 if (strncmp(buf, "!<arch>", (sizeof("!<arch>")-1)))
  return ((void *)0);
 return &ret;
}

variable_types:
filename
char *

variable_types: fd int

variable_types:
buf
char [-1]

return_type: struct archive_handle { int fd; char *filename; unsigned long skip; char *extfn; } *


decompiled_code:
int *__fastcall ar_open(const char *a1)
{
int v1; // eax
int v2; // ebx
int *v3; // rbp

v1 = open(a1, 0);
if ( v1 == -1 )
{
fprintf(stderr, "%s%s%s: Could not open '%s'\n", RED, (const char *)&argv0, NORM, a1);
exit(1);
}
v2 = v1;
v3 = ar_open_fd((__int64)a1, v1);
if ( !v3 )
close(v2);
return v3;
}

canonical_decompiled_code:
int *__fastcall func1(const char *a1)
{
int v1; // eax
int v2; // ebx
int *v3; // rbp

v1 = open(a1, 0);
if ( v1 == -1 )
{
fprintf(stderr, "%s%s%s: Could not open '%s'\n", RED, (const char *)&argv0, NORM, a1);
exit(1);
}
v2 = v1;
v3 = func0((__int64)a1, v1);
if ( !v3 )
close(v2);
return v3;
}

original_code:
archive_handle *ar_open(const char *filename)
{
 int fd;
 archive_handle *ret;
 if ((fd=open(filename, 00)) == -1)
  do { fprintf(stderr, "%s%s%s: " "Could not open '%s'" "\n", RED, argv0, NORM , filename); exit(1); } while (0);
 ret = ar_open_fd(filename, fd);
 if (ret == ((void *)0))
  close(fd);
 return ret;
}

canonical_original_code:
struct archive_handle *ar_open(char *filename)
{
 int fd;
 struct archive_handle *ret;
 if ((fd=open(filename, 00)) == -1)
  do { fprintf(stderr, "%s%s%s: " "Could not open '%s'" "\n", RED, argv0, NORM , filename); exit(1); } while (0);
 ret = ar_open_fd(filename, fd);
 if (ret == ((void *)0))
  close(fd);
 return ret;
}

variable_types: struct archive_handle { int fd; char *filename; unsigned long skip; char *extfn; } *


decompiled_code:
char *__fastcall ar_next(__int64 a1)
{
__off_t v1; // rsi
__int64 v2; // r12
const char *v3; // r13
size_t v4; // rax
char *v5; // rax
size_t v7; // r12
void *v8; // rsi
size_t v9; // rax
_BYTE *v10; // rdx
int v11; // eax
void *v12; // rsp
__int64 v13; // r13
_BYTE v14[15]; // [rsp+0h] [rbp-20h] BYREF

v1 = *(_QWORD *)(a1 + 16);
if ( v1 && lseek(*(_DWORD *)a1, v1, 1) == -1 )
goto LABEL_16;
if ( read(*(_DWORD *)a1, &unk_1560, 0x3CuLL) != 60 )
goto LABEL_16;
if ( unk_1560 == 10 )
{
memmove(&unk_1560, &word_1561, 0x3BuLL);
if ( read(*(_DWORD *)a1, (char *)&word_159A + 1, 1uLL) != 1 )
goto LABEL_16;
}
if ( word_159A != 2656 )
{
fprintf(stderr, "%s%s%s: Invalid ar entry\n", RED, (const char *)&argv0, NORM);
goto LABEL_16;
}
if ( unk_1560 != 12079 )
{
if ( unk_1560 == 35 )
{
v2 = 0LL;
v3 = (const char *)&unk_1560;
if ( *(_WORD *)&word_1561 != 12081 )
goto LABEL_10;
v11 = atoi(byte_1563);
v2 = v11;
if ( (unsigned __int64)v11 > 0x10 )
{
v12 = alloca(v11 + 8LL);
v3 = v14;
if ( read(*(_DWORD *)a1, v14, v11) == v11 )
goto LABEL_10;
}
else if ( read(*(_DWORD *)a1, &unk_1560, v11) == v11 )
{
goto LABEL_10;
}
}
else
{
if ( unk_1560 != 47 )
{
v2 = 0LL;
v3 = (const char *)&unk_1560;
LABEL_10:
snprintf(ret_1, 0x1000uLL, "%s:%s", *(const char **)(a1 + 8), v3);
v4 = strlen(*(const char **)(a1 + 8));
v5 = strchr(&ret_1[v4], 47);
if ( v5 )
*v5 = 0;
qword_1540 = atoi(nptr);
dword_1548 = atoi(byte_157C);
dword_154C = atoi(byte_1582);
dword_1550 = strtol(byte_1588, 0LL, 8);
qword_1558 = atoi(byte_1590);
*(_QWORD *)(a1 + 16) = qword_1558 - v2;
return ret_1;
}
v2 = 0LL;
v3 = (const char *)&unk_1560;
if ( (unsigned __int8)(word_1561 - 48) > 9u )
goto LABEL_10;
v13 = *(_QWORD *)(a1 + 24);
if ( v13 )
{
v3 = (const char *)(atoi(&word_1561) + v13);
v2 = 0LL;
goto LABEL_10;
}
fprintf(stderr, "%s%s%s: GNU extended filename without special data section\n", RED, (const char *)&argv0, NORM);
}
goto LABEL_16;
}
if ( *(_QWORD *)(a1 + 24) )
{
fprintf(stderr, "%s%s%s: Duplicate GNU extended filename section\n", RED, (const char *)&argv0, NORM);
goto LABEL_16;
}
v7 = atoi(byte_1590);
v8 = (void *)xmalloc(v7 + 1);
*(_QWORD *)(a1 + 24) = v8;
if ( read(*(_DWORD *)a1, v8, v7) != v7 )
{
LABEL_16:
close(*(_DWORD *)a1);
return 0LL;
}
*(_BYTE *)(*(_QWORD *)(a1 + 24) + v7) = 0;
v9 = v7 - 1;
if ( v7 != 1 )
{
do
{
v10 = (_BYTE *)(*(_QWORD *)(a1 + 24) + v9);
if ( *v10 == 10 )
*v10 = 0;
--v9;
}
while ( v9 );
}
*(_QWORD *)(a1 + 16) = 0LL;
return (char *)ar_next(a1);
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1)
{
__off_t v1; // rsi
__int64 v2; // r12
const char *v3; // r13
size_t v4; // rax
char *v5; // rax
size_t v7; // r12
void *v8; // rsi
size_t v9; // rax
_BYTE *v10; // rdx
int v11; // eax
void *v12; // rsp
__int64 v13; // r13
_BYTE v14[15]; // [rsp+0h] [rbp-20h] BYREF

v1 = *(_QWORD *)(a1 + 16);
if ( v1 && lseek(*(_DWORD *)a1, v1, 1) == -1 )
goto LABEL_16;
if ( read(*(_DWORD *)a1, &unk_1560, 0x3CuLL) != 60 )
goto LABEL_16;
if ( unk_1560 == 10 )
{
memmove(&unk_1560, &word_1561, 0x3BuLL);
if ( read(*(_DWORD *)a1, (char *)&word_159A + 1, 1uLL) != 1 )
goto LABEL_16;
}
if ( word_159A != 2656 )
{
fprintf(stderr, "%s%s%s: Invalid ar entry\n", RED, (const char *)&argv0, NORM);
goto LABEL_16;
}
if ( unk_1560 != 12079 )
{
if ( unk_1560 == 35 )
{
v2 = 0LL;
v3 = (const char *)&unk_1560;
if ( *(_WORD *)&word_1561 != 12081 )
goto LABEL_10;
v11 = atoi(byte_1563);
v2 = v11;
if ( (unsigned __int64)v11 > 0x10 )
{
v12 = alloca(v11 + 8LL);
v3 = v14;
if ( read(*(_DWORD *)a1, v14, v11) == v11 )
goto LABEL_10;
}
else if ( read(*(_DWORD *)a1, &unk_1560, v11) == v11 )
{
goto LABEL_10;
}
}
else
{
if ( unk_1560 != 47 )
{
v2 = 0LL;
v3 = (const char *)&unk_1560;
LABEL_10:
snprintf(ret_1, 0x1000uLL, "%s:%s", *(const char **)(a1 + 8), v3);
v4 = strlen(*(const char **)(a1 + 8));
v5 = strchr(&ret_1[v4], 47);
if ( v5 )
*v5 = 0;
qword_1540 = atoi(nptr);
dword_1548 = atoi(byte_157C);
dword_154C = atoi(byte_1582);
dword_1550 = strtol(byte_1588, 0LL, 8);
qword_1558 = atoi(byte_1590);
*(_QWORD *)(a1 + 16) = qword_1558 - v2;
return ret_1;
}
v2 = 0LL;
v3 = (const char *)&unk_1560;
if ( (unsigned __int8)(word_1561 - 48) > 9u )
goto LABEL_10;
v13 = *(_QWORD *)(a1 + 24);
if ( v13 )
{
v3 = (const char *)(atoi(&word_1561) + v13);
v2 = 0LL;
goto LABEL_10;
}
fprintf(stderr, "%s%s%s: GNU extended filename without special data section\n", RED, (const char *)&argv0, NORM);
}
goto LABEL_16;
}
if ( *(_QWORD *)(a1 + 24) )
{
fprintf(stderr, "%s%s%s: Duplicate GNU extended filename section\n", RED, (const char *)&argv0, NORM);
goto LABEL_16;
}
v7 = atoi(byte_1590);
v8 = (void *)xmalloc(v7 + 1);
*(_QWORD *)(a1 + 24) = v8;
if ( read(*(_DWORD *)a1, v8, v7) != v7 )
{
LABEL_16:
close(*(_DWORD *)a1);
return 0LL;
}
*(_BYTE *)(*(_QWORD *)(a1 + 24) + v7) = 0;
v9 = v7 - 1;
if ( v7 != 1 )
{
do
{
v10 = (_BYTE *)(*(_QWORD *)(a1 + 24) + v9);
if ( *v10 == 10 )
*v10 = 0;
--v9;
}
while ( v9 );
}
*(_QWORD *)(a1 + 16) = 0LL;
return (char *)func2(a1);
}

original_code:
archive_member *ar_next(archive_handle *ar)
{
 char *s;
 size_t len = 0;
 static archive_member ret;
 if (ar->skip && lseek(ar->fd, ar->skip, 1) == -1) {
close_and_ret:
  close(ar->fd);
  return ((void *)0);
 }
 if (read(ar->fd, ret.buf.raw, sizeof(ret.buf.raw)) != sizeof(ret.buf.raw))
  goto close_and_ret;
 if (ret.buf.raw[0] == '\n') {
  memmove(ret.buf.raw, ret.buf.raw+1, 59);
  if (read(ar->fd, ret.buf.raw+59, 1) != 1)
   goto close_and_ret;
 }
 if ((ret.buf.formatted.magic[0] != '`') || (ret.buf.formatted.magic[1] != '\n')) {
  fprintf(stderr, "%s%s%s: " "Invalid ar entry" "\n", RED, argv0, NORM);
  goto close_and_ret;
 }
 if (ret.buf.formatted.name[0] == '/' && ret.buf.formatted.name[1] == '/') {
  if (ar->extfn != ((void *)0)) {
   fprintf(stderr, "%s%s%s: " "Duplicate GNU extended filename section" "\n", RED, argv0, NORM);
   goto close_and_ret;
  }
  len = atoi(ret.buf.formatted.size);
  ar->extfn = xmalloc(sizeof(char) * (len + 1));
  if (read(ar->fd, ar->extfn, len) != len)
   goto close_and_ret;
  ar->extfn[len--] = '\0';
  for (; len > 0; len--)
   if (ar->extfn[len] == '\n')
    ar->extfn[len] = '\0';
  ar->skip = 0;
  return ar_next(ar);
 }
 s = ret.buf.formatted.name;
 if (s[0] == '#' && s[1] == '1' && s[2] == '/') {
  len = atoi(s + 3);
  if (len <= sizeof(ret.buf.formatted.name)) {
   if (read(ar->fd, ret.buf.formatted.name, len) != len)
    goto close_and_ret;
  } else {
   s = __builtin_alloca (sizeof(char) * len);
   if (read(ar->fd, s, len) != len)
    goto close_and_ret;
  }
 } else if (s[0] == '/' && s[1] >= '0' && s[1] <= '9') {
  if (ar->extfn == ((void *)0)) {
   fprintf(stderr, "%s%s%s: " "GNU extended filename without special data section" "\n", RED, argv0, NORM);
   goto close_and_ret;
  }
  s = ar->extfn + atoi(s + 1);
 }
 snprintf(ret.name, sizeof(ret.name), "%s:%s", ar->filename, s);
 if ((s=strchr(ret.name+strlen(ar->filename), '/')) != ((void *)0))
  *s = '\0';
 ret.date = atoi(ret.buf.formatted.date);
 ret.uid = atoi(ret.buf.formatted.uid);
 ret.gid = atoi(ret.buf.formatted.gid);
 ret.mode = strtol(ret.buf.formatted.mode, ((void *)0), 8);
 ret.size = atoi(ret.buf.formatted.size);
 ar->skip = ret.size - len;
 return &ret;
}

canonical_original_code:
struct archive_member *ar_next(struct archive_handle *ar)
{
 char *s;
 unsigned long len = 0;
 static struct archive_member ret;
 if (ar->skip && lseek(ar->fd, ar->skip, 1) == -1) {
close_and_ret:
  close(ar->fd);
  return ((void *)0);
 }
 if (read(ar->fd, ret.buf.raw, sizeof(ret.buf.raw)) != sizeof(ret.buf.raw))
  goto close_and_ret;
 if (ret.buf.raw[0] == '\n') {
  memmove(ret.buf.raw, ret.buf.raw+1, 59);
  if (read(ar->fd, ret.buf.raw+59, 1) != 1)
   goto close_and_ret;
 }
 if ((ret.buf.formatted.magic[0] != '`') || (ret.buf.formatted.magic[1] != '\n')) {
  fprintf(stderr, "%s%s%s: " "Invalid ar entry" "\n", RED, argv0, NORM);
  goto close_and_ret;
 }
 if (ret.buf.formatted.name[0] == '/' && ret.buf.formatted.name[1] == '/') {
  if (ar->extfn != ((void *)0)) {
   fprintf(stderr, "%s%s%s: " "Duplicate GNU extended filename section" "\n", RED, argv0, NORM);
   goto close_and_ret;
  }
  len = atoi(ret.buf.formatted.size);
  ar->extfn = xmalloc(sizeof(char) * (len + 1));
  if (read(ar->fd, ar->extfn, len) != len)
   goto close_and_ret;
  ar->extfn[len--] = '\0';
  for (; len > 0; len--)
   if (ar->extfn[len] == '\n')
    ar->extfn[len] = '\0';
  ar->skip = 0;
  return ar_next(ar);
 }
 s = ret.buf.formatted.name;
 if (s[0] == '#' && s[1] == '1' && s[2] == '/') {
  len = atoi(s + 3);
  if (len <= sizeof(ret.buf.formatted.name)) {
   if (read(ar->fd, ret.buf.formatted.name, len) != len)
    goto close_and_ret;
  } else {
   s = __builtin_alloca (sizeof(char) * len);
   if (read(ar->fd, s, len) != len)
    goto close_and_ret;
  }
 } else if (s[0] == '/' && s[1] >= '0' && s[1] <= '9') {
  if (ar->extfn == ((void *)0)) {
   fprintf(stderr, "%s%s%s: " "GNU extended filename without special data section" "\n", RED, argv0, NORM);
   goto close_and_ret;
  }
  s = ar->extfn + atoi(s + 1);
 }
 snprintf(ret.name, sizeof(ret.name), "%s:%s", ar->filename, s);
 if ((s=strchr(ret.name+strlen(ar->filename), '/')) != ((void *)0))
  *s = '\0';
 ret.date = atoi(ret.buf.formatted.date);
 ret.uid = atoi(ret.buf.formatted.uid);
 ret.gid = atoi(ret.buf.formatted.gid);
 ret.mode = strtol(ret.buf.formatted.mode, ((void *)0), 8);
 ret.size = atoi(ret.buf.formatted.size);
 ar->skip = ret.size - len;
 return &ret;
}

variable_types:
s
char *

variable_types:
len
unsigned long

return_type: struct archive_member { char name[4096]; long date; unsigned int uid; unsigned int gid; unsigned int mode; long size; union { char raw[60]; struct { char name[16]; char date[12]; char uid[6]; char gid[6]; char mode[8]; char size[10]; char magic[2]; } formatted; } buf; } *

user_defined_types: struct archive_handle { int fd; char *filename; unsigned long skip; char *extfn; }


strfileperms

decompiled_code:
void *__fastcall strfileperms(const char *a1)
{
int v1; // edx
void *result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = &unk_1605;
if ( v1 != -1 )
{
snprintf(buf_0, 8uLL, "%o", v3.st_mode);
result = &unk_52A;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(const char *a1)
{
int v1; // edx
void *result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = &unk_1605;
if ( v1 != -1 )
{
snprintf(buf_0, 8uLL, "%o", v3.st_mode);
result = &unk_52A;
}
return result;
}

original_code:
const char *strfileperms(const char *fname)
{
 struct stat st;
 static char buf[8];
 if (stat(fname, &st) == -1)
  return "";
 snprintf(buf, sizeof(buf), "%o", st.st_mode);
 return buf + 2;
}

canonical_original_code:
char *strfileperms(char *fname)
{
 struct stat st;
 static char buf[8];
 if (stat(fname, &st) == -1)
  return "";
 snprintf(buf, sizeof(buf), "%o", st.st_mode);
 return buf + 2;
}

variable_types:
fname
char *

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

variable_types:
buf
char [8]

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


decompiled_code:
void __fastcall color_init(char a1)
{
char *v1; // rax

if ( a1 || (v1 = getenv("NOCOLOR")) != 0LL && (!strcmp(v1, "yes") || !strcmp(v1, "true")) )
{
YELLOW = (char *)&unk_1605;
RED = (char *)&unk_1605;
NORM = (char *)&unk_1605;
}
}

canonical_decompiled_code:
void __fastcall func4(char a1)
{
char *v1; // rax

if ( a1 || (v1 = getenv("NOCOLOR")) != 0LL && (!strcmp(v1, "yes") || !strcmp(v1, "true")) )
{
YELLOW = (char *)&unk_1605;
RED = (char *)&unk_1605;
NORM = (char *)&unk_1605;
}
}

original_code:
void color_init(_Bool disable)
{
 if (!disable) {
  const char *nocolor = getenv("NOCOLOR");
  if (nocolor)
   disable = !strcmp(nocolor, "yes") || !strcmp(nocolor, "true");
 }
 if (disable)
  NORM = RED = YELLOW = "";
}

canonical_original_code:
void color_init(_Bool disable)
{
 if (!disable) {
  char *nocolor = getenv("NOCOLOR");
  if (nocolor)
   disable = !strcmp(nocolor, "yes") || !strcmp(nocolor, "true");
 }
 if (disable)
  NORM = RED = YELLOW = "";
}

variable_types:
nocolor
char *


xstrdup

decompiled_code:
char *__fastcall xstrdup(const char *a1)
{
char *result; // rax
int *v2; // rax
char *v3; // rax

result = strdup(a1);
if ( !result )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "%s%s%s: Could not strdup(): %s\n", RED, (const char *)&argv0, NORM, v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1)
{
char *result; // rax
int *v2; // rax
char *v3; // rax

result = strdup(a1);
if ( !result )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "%s%s%s: Could not strdup(): %s\n", RED, (const char *)&argv0, NORM, v3);
exit(1);
}
return result;
}

original_code:
char *xstrdup(const char *s)
{
 char *ret = strdup(s);
 if (!ret) do { fprintf(stderr, "%s%s%s: " "Could not strdup(): %s" "\n", RED, argv0, NORM , strerror((*__errno_location ()))); exit(1); } while (0);
 return ret;
}

canonical_original_code:
char *xstrdup(char *s)
{
 char *ret = strdup(s);
 if (!ret) do { fprintf(stderr, "%s%s%s: " "Could not strdup(): %s" "\n", RED, argv0, NORM , strerror((*__errno_location ()))); exit(1); } while (0);
 return ret;
}

variable_types:
ret
char *


xmalloc

decompiled_code:
void *__fastcall xmalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fprintf(stderr, "%s%s%s: Could not malloc() %zu bytes\n", RED, (const char *)&argv0, NORM, a1);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fprintf(stderr, "%s%s%s: Could not malloc() %zu bytes\n", RED, (const char *)&argv0, NORM, a1);
exit(1);
}
return result;
}

original_code:
void *xmalloc(size_t size)
{
 void *ret = malloc(size);
 if (!ret) do { fprintf(stderr, "%s%s%s: " "Could not malloc() %zu bytes" "\n", RED, argv0, NORM , size); exit(1); } while (0);
 return ret;
}

canonical_original_code:
void *xmalloc(unsigned long size)
{
 void *ret = malloc(size);
 if (!ret) do { fprintf(stderr, "%s%s%s: " "Could not malloc() %zu bytes" "\n", RED, argv0, NORM , size); exit(1); } while (0);
 return ret;
}

variable_types:
size
unsigned long

variable_types:
ret
void *

return_type: void *


xzalloc

decompiled_code:
void *__fastcall xzalloc(size_t n)
{
void *v1; // rax

v1 = xmalloc(n);
return memset(v1, 0, n);
}

canonical_decompiled_code:
void *__fastcall func2(size_t n)
{
void *v1; // rax

v1 = func1(n);
return memset(v1, 0, n);
}

original_code:
void *xzalloc(size_t size)
{
 return memset(xmalloc(size), 0, size);
}

canonical_original_code:
void *xzalloc(unsigned long size)
{
 return memset(xmalloc(size), 0, size);
}


xrealloc

decompiled_code:
void *__fastcall xrealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
fprintf(stderr, "%s%s%s: Could not realloc() %zu bytes\n", RED, (const char *)&argv0, NORM, a2);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
fprintf(stderr, "%s%s%s: Could not realloc() %zu bytes\n", RED, (const char *)&argv0, NORM, a2);
exit(1);
}
return result;
}

original_code:
void *xrealloc(void *ptr, size_t size)
{
 void *ret = realloc(ptr, size);
 if (!ret) do { fprintf(stderr, "%s%s%s: " "Could not realloc() %zu bytes" "\n", RED, argv0, NORM , size); exit(1); } while (0);
 return ret;
}

canonical_original_code:
void *xrealloc(void *ptr, unsigned long size)
{
 void *ret = realloc(ptr, size);
 if (!ret) do { fprintf(stderr, "%s%s%s: " "Could not realloc() %zu bytes" "\n", RED, argv0, NORM , size); exit(1); } while (0);
 return ret;
}

variable_types:
ptr
void *


xstrncat

decompiled_code:
char *__fastcall xstrncat(const char **a1, const char *a2, size_t *a3, size_t a4)
{
size_t v7; // r14
size_t v8; // r15
size_t v9; // rax
char *result; // rax
size_t v11; // rsi
const char *v12; // rax
size_t v13; // rsi
const char *v14; // rax

v7 = *a3;
if ( *a3 )
{
v8 = strlen(*a1);
v9 = v8 + strlen(a2);
if ( v7 <= v9 )
{
v13 = v9 + (v7 >> 1) + 1;
*a3 = v13;
v14 = (const char *)realloc((void *)*a1, v13);
*a1 = v14;
if ( !v14 )
{
LABEL_8:
fprintf(stderr, "%s%s%s: could not realloc() %zu bytes\n", RED, (const char *)&argv0, NORM, *a3);
exit(1);
}
}
}
else
{
v11 = strlen(a2) + 1;
*a3 = v11;
v12 = (const char *)realloc((void *)*a1, v11);
*a1 = v12;
if ( !v12 )
goto LABEL_8;
*v12 = 0;
}
if ( a4 )
result = strncat((char *)*a1, a2, a4);
else
result = strcat((char *)*a1, a2);
return result;
}

canonical_decompiled_code:
char *__fastcall func4(const char **a1, const char *a2, size_t *a3, size_t a4)
{
size_t v7; // r14
size_t v8; // r15
size_t v9; // rax
char *result; // rax
size_t v11; // rsi
const char *v12; // rax
size_t v13; // rsi
const char *v14; // rax

v7 = *a3;
if ( *a3 )
{
v8 = strlen(*a1);
v9 = v8 + strlen(a2);
if ( v7 <= v9 )
{
v13 = v9 + (v7 >> 1) + 1;
*a3 = v13;
v14 = (const char *)realloc((void *)*a1, v13);
*a1 = v14;
if ( !v14 )
{
LABEL_8:
fprintf(stderr, "%s%s%s: could not realloc() %zu bytes\n", RED, (const char *)&argv0, NORM, *a3);
exit(1);
}
}
}
else
{
v11 = strlen(a2) + 1;
*a3 = v11;
v12 = (const char *)realloc((void *)*a1, v11);
*a1 = v12;
if ( !v12 )
goto LABEL_8;
*v12 = 0;
}
if ( a4 )
result = strncat((char *)*a1, a2, a4);
else
result = strcat((char *)*a1, a2);
return result;
}

original_code:
void xstrncat(char **dst, const char *src, size_t *curr_len, size_t n)
{
 _Bool init;
 size_t new_len;
 init = *curr_len ? 0 : 1;
 new_len = (init ? 0 : strlen(*dst)) + strlen(src);
 if (*curr_len <= new_len) {
  *curr_len = new_len + (*curr_len / 2) + 1;
  *dst = realloc(*dst, *curr_len);
  if (!*dst)
   do { fprintf(stderr, "%s%s%s: " "could not realloc() %zu bytes" "\n", RED, argv0, NORM , *curr_len); exit(1); } while (0);
  if (init)
   *dst[0] = '\0';
 }
 if (n)
  strncat(*dst, src, n);
 else
  strcat(*dst, src);
}

canonical_original_code:
void xstrncat(char **dst, char *src, unsigned long *curr_len, unsigned long n)
{
 _Bool init;
 unsigned long new_len;
 init = *curr_len ? 0 : 1;
 new_len = (init ? 0 : strlen(*dst)) + strlen(src);
 if (*curr_len <= new_len) {
  *curr_len = new_len + (*curr_len / 2) + 1;
  *dst = realloc(*dst, *curr_len);
  if (!*dst)
   do { fprintf(stderr, "%s%s%s: " "could not realloc() %zu bytes" "\n", RED, argv0, NORM , *curr_len); exit(1); } while (0);
  if (init)
   *dst[0] = '\0';
 }
 if (n)
  strncat(*dst, src, n);
 else
  strcat(*dst, src);
}

variable_types:
dst
char **

variable_types:
src
char *

variable_types:
n
unsigned long


xchrcat

decompiled_code:
char *__fastcall xchrcat(const char **a1, char a2, size_t *a3)
{
my_app_0 = a2;
byte_20C = 0;
return xstrncat(a1, &my_app_0, a3, 0LL);
}

canonical_decompiled_code:
char *__fastcall func5(const char **a1, char a2, size_t *a3)
{
my_app_0 = a2;
byte_20C = 0;
return func4(a1, &my_app_0, a3, 0LL);
}

original_code:
void xchrcat(char **dst, const char append, size_t *curr_len)
{
 static char my_app[2];
 my_app[0] = append;
 my_app[1] = '\0';
 xstrncat(dst,my_app,curr_len,0);
}

canonical_original_code:
void xchrcat(char **dst, char append, unsigned long *curr_len)
{
 static char my_app[2];
 my_app[0] = append;
 my_app[1] = '\0';
 xstrncat(dst,my_app,curr_len,0);
}

variable_types: append char


main

canonical_name: main

repo: slcz/taroo

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
int v4; // eax
int v5; // ebp
int v6; // ebx
int v7; // ebx
char *v8; // rax
_BYTE v10[32768]; // [rsp+0h] [rbp-8018h] BYREF
char v11; // [rsp+8000h] [rbp-18h] BYREF

v3 = argv[1];
hdr = 306585771;
dword_40407C = 128;
v4 = open(v3, 2, envp);
if ( v4 >= 0 )
{
v5 = v4;
v6 = 0;
while ( (int)read(v5, v10, 0x8000uLL) > 0 )
v6 += 0x8000;
dword_404074 = v6;
dword_404078 = 0;
lseek(v5, 4LL, 0);
write(v5, &hdr, 0x10uLL);
lseek(v5, 0LL, 0);
memset(v10, 0, sizeof(v10));
v7 = 0;
while ( (int)read(v5, v10, 0x8000uLL) > 0 )
{
v8 = v10;
do
{
v7 += *(_DWORD *)v8;
v8 += 4;
}
while ( v8 != &v11 );
memset(v10, 0, sizeof(v10));
}
dword_404078 = -v7;
lseek(v5, 4LL, 0);
write(v5, &hdr, 0x10uLL);
close(v5);
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 char *name = argv[1];
 uint32_t buf[32 * 1024 / sizeof(uint32_t)];
 int fd, i;
 uint32_t sum = 0, size = 0;
 hdr.magic = 0x124620AB;
 hdr.drv = 0x80;
 fd = open(name, 02);
 if (fd < 0)
  return (0);
 while (read(fd, buf, sizeof(buf)) > 0)
  size += sizeof(buf);
 hdr.size = size;
 hdr.cksum = 0;
 lseek(fd, 4, 0);
 (void)write(fd, &hdr, sizeof(hdr));
 lseek(fd, 0, 0);
 memset(buf, 0, sizeof(buf));
 while (read(fd, buf, sizeof(buf)) > 0) {
  for (i = 0; i < sizeof(buf) / sizeof(uint32_t); i ++)
   sum += buf[i];
  memset(buf, 0, sizeof(buf));
 }
 hdr.cksum = ~sum + 1;
 lseek(fd, 4, 0);
 (void)write(fd, &hdr, sizeof(hdr));
 close(fd);
 return (0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 char *name = argv[1];
 unsigned int buf[-1];
 int fd; int i;
 unsigned int sum = 0; unsigned int size = 0;
 hdr.magic = 0x124620AB;
 hdr.drv = 0x80;
 fd = open(name, 02);
 if (fd < 0)
  return (0);
 while (read(fd, buf, sizeof(buf)) > 0)
  size += sizeof(buf);
 hdr.size = size;
 hdr.cksum = 0;
 lseek(fd, 4, 0);
 (void)write(fd, &hdr, sizeof(hdr));
 lseek(fd, 0, 0);
 memset(buf, 0, sizeof(buf));
 while (read(fd, buf, sizeof(buf)) > 0) {
  for (i = 0; i < sizeof(buf) / sizeof(unsigned int); i ++)
   sum += buf[i];
  memset(buf, 0, sizeof(buf));
 }
 hdr.cksum = ~sum + 1;
 lseek(fd, 4, 0);
 (void)write(fd, &hdr, sizeof(hdr));
 close(fd);
 return (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
name
char *

variable_types:
buf
unsigned int [-1]

variable_types: fd int

variable_types: i int

variable_types:
sum
unsigned int

variable_types:
size
unsigned int


myrandom

decompiled_code:
double myrandom()
{
__int64 v0; // rcx
double v1; // xmm1_8

v0 = __readfsqword(0xFFFFFFF8) * mult_n % 0x7FFFFFFF;
__writefsqword(0xFFFFFFF8, v0);
if ( v0 < 0 )
v1 = (double)(int)(v0 & 1 | ((unsigned __int64)v0 >> 1)) + (double)(int)(v0 & 1 | ((unsigned __int64)v0 >> 1));
else
v1 = (double)(int)v0;
return v1 / 2147483647.0 * (*(double *)&random_hi - *(double *)&random_low) + *(double *)&random_low;
}

canonical_decompiled_code:
double func8()
{
__int64 v0; // rcx
double v1; // xmm1_8

v0 = __readfsqword(0xFFFFFFF8) * mult_n % 0x7FFFFFFF;
__writefsqword(0xFFFFFFF8, v0);
if ( v0 < 0 )
v1 = (double)(int)(v0 & 1 | ((unsigned __int64)v0 >> 1)) + (double)(int)(v0 & 1 | ((unsigned __int64)v0 >> 1));
else
v1 = (double)(int)v0;
return v1 / 2147483647.0 * (*(double *)&random_hi - *(double *)&random_low) + *(double *)&random_low;
}

original_code:
double myrandom()
{
  unsigned long long random_next;
  double ret_val;
  random_next = (unsigned long long)((mult_n * random_last)% PMOD);
  random_last = random_next;
  ret_val = ((double)random_next/(double)PMOD)*(random_hi-random_low)+random_low;
  return ret_val;
}

return_type: double


seed

decompiled_code:
unsigned __int64 __fastcall seed(unsigned __int64 a1, double a2, double a3)
{
unsigned __int64 v3; // rbx
int v4; // ebp
int v5; // eax
unsigned __int64 *v6; // rsi
__int64 v7; // rcx
__int64 v8; // rdx
__int64 v9; // rdx
unsigned __int64 v10; // rdi
unsigned __int64 result; // rax

v3 = a1;
v4 = omp_get_thread_num();
if ( (unsigned __int8)GOMP_single_start() )
{
if ( a3 <= a2 )
{
random_low = *(_QWORD *)&a3;
random_hi = *(_QWORD *)&a2;
}
else
{
random_low = *(_QWORD *)&a2;
random_hi = *(_QWORD *)&a3;
}
v5 = omp_get_num_threads();
if ( !a1 )
v3 = 2LL;
pseed[0] = v3;
mult_n = 764261123LL;
if ( v5 > 1 )
{
v6 = (unsigned __int64 *)&unk_404100;
v7 = 764261123LL;
do
{
v8 = (0x200000005LL * (unsigned __int128)(764261123 * v3)) >> 64;
v3 = 764261123 * v3 - 0x7FFFFFFF * ((v8 + ((764261123 * v3 - v8) >> 1)) >> 30);
*v6 = v3;
v9 = (0x200000005LL * (unsigned __int128)(unsigned __int64)(764261123 * v7)) >> 64;
v10 = 764261123 * v7 - 0x7FFFFFFF * ((v9 + ((unsigned __int64)(764261123 * v7 - v9) >> 1)) >> 30);
v7 = v10;
v6 += 4;
}
while ( v6 != (unsigned __int64 *)(32LL * (unsigned int)(v5 - 2) + 4210976) );
mult_n = v10;
}
}
GOMP_barrier();
result = pseed[4 * v4];
__writefsqword(0xFFFFFFF8, result);
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(unsigned __int64 a1, double a2, double a3)
{
unsigned __int64 v3; // rbx
int v4; // ebp
int v5; // eax
unsigned __int64 *v6; // rsi
__int64 v7; // rcx
__int64 v8; // rdx
__int64 v9; // rdx
unsigned __int64 v10; // rdi
unsigned __int64 result; // rax

v3 = a1;
v4 = omp_get_thread_num();
if ( (unsigned __int8)GOMP_single_start() )
{
if ( a3 <= a2 )
{
random_low = *(_QWORD *)&a3;
random_hi = *(_QWORD *)&a2;
}
else
{
random_low = *(_QWORD *)&a2;
random_hi = *(_QWORD *)&a3;
}
v5 = omp_get_num_threads();
if ( !a1 )
v3 = 2LL;
pseed[0] = v3;
mult_n = 764261123LL;
if ( v5 > 1 )
{
v6 = (unsigned __int64 *)&unk_404100;
v7 = 764261123LL;
do
{
v8 = (0x200000005LL * (unsigned __int128)(764261123 * v3)) >> 64;
v3 = 764261123 * v3 - 0x7FFFFFFF * ((v8 + ((764261123 * v3 - v8) >> 1)) >> 30);
*v6 = v3;
v9 = (0x200000005LL * (unsigned __int128)(unsigned __int64)(764261123 * v7)) >> 64;
v10 = 764261123 * v7 - 0x7FFFFFFF * ((v9 + ((unsigned __int64)(764261123 * v7 - v9) >> 1)) >> 30);
v7 = v10;
v6 += 4;
}
while ( v6 != (unsigned __int64 *)(32LL * (unsigned int)(v5 - 2) + 4210976) );
mult_n = v10;
}
}
GOMP_barrier();
result = pseed[4 * v4];
__writefsqword(0xFFFFFFF8, result);
return result;
}

original_code:
void seed(unsigned long long iseed, double low_in, double hi_in)
{
  int i, id, nthreads;
  id = omp_get_thread_num();

#pragma omp single
  {
    if(low_in < hi_in)
      {
 random_low = low_in;
 random_hi = hi_in;
      }
    else
      {
 random_low = hi_in;
 random_hi = low_in;
      }
    nthreads = omp_get_num_threads();
    if ( iseed == 0 )
      iseed = PMOD/MULTIPLIER;
    pseed[0][0] = iseed;
    mult_n = MULTIPLIER;
    for (i = 1; i < nthreads; ++i)
      {
 iseed = (unsigned long long)((MULTIPLIER * iseed) % PMOD);
 pseed[i][0] = iseed;
 mult_n = (mult_n * MULTIPLIER) % PMOD;
      }
  }
  random_last = (unsigned long long) pseed[id][0];
}

canonical_original_code:
void seed(unsigned long long iseed, double low_in, double hi_in)
{
  int i; int id; int nthreads;
  id = omp_get_thread_num();

#pragma omp single
  {
    if(low_in < hi_in)
      {
 random_low = low_in;
 random_hi = hi_in;
      }
    else
      {
 random_low = hi_in;
 random_hi = low_in;
      }
    nthreads = omp_get_num_threads();
    if ( iseed == 0 )
      iseed = PMOD/MULTIPLIER;
    pseed[0][0] = iseed;
    mult_n = MULTIPLIER;
    for (i = 1; i < nthreads; ++i)
      {
 iseed = (unsigned long long)((MULTIPLIER * iseed) % PMOD);
 pseed[i][0] = iseed;
 mult_n = (mult_n * MULTIPLIER) % PMOD;
      }
  }
  random_last = (unsigned long long) pseed[id][0];
}

variable_types:
iseed
unsigned long long

variable_types: id int

variable_types: nthreads int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
unsigned int *v4; // rbx
unsigned int v5; // ebp
char v7; // [rsp+3F0h] [rbp-3E8h] BYREF
char v8; // [rsp+7B0h] [rbp-28h] BYREF

seed(0x75BCD15uLL, 0.0, 2147483647.0);
v3 = &v7;
do
{
*(double *)v3 = myrandom();
v3 += 8;
}
while ( v3 != &v8 );
v4 = (unsigned int *)&tests;
do
{
v5 = *v4;
omp_set_num_threads();
GOMP_parallel();
printf(" Diff for %i threads is %f.\n", v5, 0.0);
++v4;
}
while ( v4 != (unsigned int *)&_bss_start );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
unsigned int *v4; // rbx
unsigned int v5; // ebp
char v7; // [rsp+3F0h] [rbp-3E8h] BYREF
char v8; // [rsp+7B0h] [rbp-28h] BYREF

func9(0x75BCD15uLL, 0.0, 2147483647.0);
v3 = &v7;
do
{
*(double *)v3 = func8();
v3 += 8;
}
while ( v3 != &v8 );
v4 = (unsigned int *)&tests;
do
{
v5 = *v4;
omp_set_num_threads();
GOMP_parallel();
printf(" Diff for %i threads is %f.\n", v5, 0.0);
++v4;
}
while ( v4 != (unsigned int *)&_bss_start );
putchar(10);
return 0;
}

original_code:
int main(int argc, char **argv) {
  double baseref[120];
  double testref[120];
  unsigned long long myseed = 123456789, setseed;
  int i, j;
  setseed=myseed;
  seed(setseed, 0.0, PMOD);
  for (i=0; i<120; i++) {
    baseref[i] = myrandom();
  }
  for (j=0; j<6; j++) {
    int numthreads = tests[j];
    double sum;
    omp_set_num_threads(numthreads);
    sum = 0;

#pragma omp parallel reduction(+:sum)
    {
      setseed=myseed;
      seed(setseed,0.0, PMOD);

#pragma omp for
      for (i=0; i<120; i++) {
 testref[i] = (double)lfprng();
 sum += abs(testref[i] - baseref[i]);
 printf(" lfprng: %f, base: %f.", testref[i], baseref[i]);
      }
    }
    printf(" Diff for %i threads is %f.\n", numthreads, sum);
  }
  printf("\n");
}

canonical_original_code:
int main(int argc, char **argv) {
  double baseref[120];
  double testref[120];
  unsigned long long myseed = 123456789; unsigned long long setseed;
  int i; int j;
  setseed=myseed;
  seed(setseed, 0.0, PMOD);
  for (i=0; i<120; i++) {
    baseref[i] = myrandom();
  }
  for (j=0; j<6; j++) {
    int numthreads = tests[j];
    double sum;
    omp_set_num_threads(numthreads);
    sum = 0;

#pragma omp parallel reduction(+:sum)
    {
      setseed=myseed;
      seed(setseed,0.0, PMOD);

#pragma omp for
      for (i=0; i<120; i++) {
 testref[i] = (double)lfprng();
 sum += abs(testref[i] - baseref[i]);
 printf(" lfprng: %f, base: %f.", testref[i], baseref[i]);
      }
    }
    printf(" Diff for %i threads is %f.\n", numthreads, sum);
  }
  printf("\n");
}

variable_types:
baseref
double [120]

variable_types:
testref
double [120]

variable_types:
myseed
unsigned long long

variable_types:
setseed
unsigned long long

variable_types: j int

variable_types: numthreads int

variable_types: sum double


repo: slim-curve/slim-curve

decompiled_code:
__int64 __fastcall parse_ini_file(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
const char *v5; // rsi
unsigned int v6; // eax
const char *v7; // rsi
__int64 result; // rax

if ( iniparser_load() )
{
iniparser_dump();
puts("Pizza:");
v1 = iniparser_getboolean();
printf("Ham: [%d]\n", v1);
v2 = iniparser_getboolean();
printf("Mushrooms: [%d]\n", v2);
v3 = iniparser_getboolean();
printf("Capres: [%d]\n", v3);
v4 = iniparser_getboolean();
printf("Cheese: [%d]\n", v4);
puts("Wine:");
v5 = (const char *)iniparser_getstring();
if ( !v5 )
v5 = "UNDEF";
printf("Grape: [%s]\n", v5);
v6 = iniparser_getint();
printf("Year: [%d]\n", v6);
v7 = (const char *)iniparser_getstring();
if ( !v7 )
v7 = "UNDEF";
printf("Country: [%s]\n", v7);
iniparser_getdouble();
printf("Alcohol: [%g]\n", -1.0);
iniparser_freedict();
result = 0LL;
}
else
{
fprintf(stderr, "cannot parse file: %s\n", a1);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
const char *v5; // rsi
unsigned int v6; // eax
const char *v7; // rsi
__int64 result; // rax

if ( iniparser_load() )
{
iniparser_dump();
puts("Pizza:");
v1 = iniparser_getboolean();
printf("Ham: [%d]\n", v1);
v2 = iniparser_getboolean();
printf("Mushrooms: [%d]\n", v2);
v3 = iniparser_getboolean();
printf("Capres: [%d]\n", v3);
v4 = iniparser_getboolean();
printf("Cheese: [%d]\n", v4);
puts("Wine:");
v5 = (const char *)iniparser_getstring();
if ( !v5 )
v5 = "UNDEF";
printf("Grape: [%s]\n", v5);
v6 = iniparser_getint();
printf("Year: [%d]\n", v6);
v7 = (const char *)iniparser_getstring();
if ( !v7 )
v7 = "UNDEF";
printf("Country: [%s]\n", v7);
iniparser_getdouble();
printf("Alcohol: [%g]\n", -1.0);
iniparser_freedict();
result = 0LL;
}
else
{
fprintf(stderr, "cannot parse file: %s\n", a1);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int parse_ini_file(char * ini_name)
{
    dictionary * ini ;
    int b ;
    int i ;
    double d ;
    char * s ;
    ini = iniparser_load(ini_name);
    if (ini==((void *)0)) {
        fprintf(stderr, "cannot parse file: %s\n", ini_name);
        return -1 ;
    }
    iniparser_dump(ini, stderr);
    printf("Pizza:\n");
    b = iniparser_getboolean(ini, "pizza:ham", -1);
    printf("Ham:       [%d]\n", b);
    b = iniparser_getboolean(ini, "pizza:mushrooms", -1);
    printf("Mushrooms: [%d]\n", b);
    b = iniparser_getboolean(ini, "pizza:capres", -1);
    printf("Capres:    [%d]\n", b);
    b = iniparser_getboolean(ini, "pizza:cheese", -1);
    printf("Cheese:    [%d]\n", b);
    printf("Wine:\n");
    s = iniparser_getstring(ini, "wine:grape", ((void *)0));
    printf("Grape:     [%s]\n", s ? s : "UNDEF");
    i = iniparser_getint(ini, "wine:year", -1);
    printf("Year:      [%d]\n", i);
    s = iniparser_getstring(ini, "wine:country", ((void *)0));
    printf("Country:   [%s]\n", s ? s : "UNDEF");
    d = iniparser_getdouble(ini, "wine:alcohol", -1.0);
    printf("Alcohol:   [%g]\n", d);
    iniparser_freedict(ini);
    return 0 ;
}

canonical_original_code:
int parse_ini_file(char *ini_name)
{
    struct _dictionary_ *ini;
    int b;
    int i;
    double d;
    char *s;
    ini = iniparser_load(ini_name);
    if (ini==((void *)0)) {
        fprintf(stderr, "cannot parse file: %s\n", ini_name);
        return -1 ;
    }
    iniparser_dump(ini, stderr);
    printf("Pizza:\n");
    b = iniparser_getboolean(ini, "pizza:ham", -1);
    printf("Ham:       [%d]\n", b);
    b = iniparser_getboolean(ini, "pizza:mushrooms", -1);
    printf("Mushrooms: [%d]\n", b);
    b = iniparser_getboolean(ini, "pizza:capres", -1);
    printf("Capres:    [%d]\n", b);
    b = iniparser_getboolean(ini, "pizza:cheese", -1);
    printf("Cheese:    [%d]\n", b);
    printf("Wine:\n");
    s = iniparser_getstring(ini, "wine:grape", ((void *)0));
    printf("Grape:     [%s]\n", s ? s : "UNDEF");
    i = iniparser_getint(ini, "wine:year", -1);
    printf("Year:      [%d]\n", i);
    s = iniparser_getstring(ini, "wine:country", ((void *)0));
    printf("Country:   [%s]\n", s ? s : "UNDEF");
    d = iniparser_getdouble(ini, "wine:alcohol", -1.0);
    printf("Alcohol:   [%g]\n", d);
    iniparser_freedict(ini);
    return 0 ;
}

variable_types: struct _dictionary_ { int n; int size; char **val; char **key; unsigned int *hash; } *

variable_types: b int

variable_types: d double

variable_types:
s
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc > 1 )
return parse_ini_file(argv[1]);
create_example_ini_file();
return parse_ini_file("example.ini");
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc > 1 )
return func8(argv[1]);
func7();
return func8("example.ini");
}

original_code:
int main(int argc, char * argv[])
{
    int status ;
    if (argc<2) {
        create_example_ini_file();
        status = parse_ini_file("example.ini");
    } else {
        status = parse_ini_file(argv[1]);
    }
    return status ;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int status;
    if (argc<2) {
        create_example_ini_file();
        status = parse_ini_file("example.ini");
    } else {
        status = parse_ini_file(argv[1]);
    }
    return status ;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: status int


repo: slitvinov/cuda-examples

decompiled_code:
int __fastcall get_timer_resolution(struct timespec *res)
{
return clock_getres(0, res);
}

canonical_decompiled_code:
int __fastcall func0(struct timespec *res)
{
return clock_getres(0, res);
}

original_code:
void get_timer_resolution(struct time_t* val)
{
 if ((sizeof(int64_t) == sizeof(time_t)) &&
  (sizeof(int64_t) == sizeof(long)))
  clock_getres(0, (struct timespec *)val);
 else
 {
  struct timespec t;
  clock_getres(0, &t);
  val->seconds = t.tv_sec;
  val->nanoseconds = t.tv_nsec;
 }
}

canonical_original_code:
void get_timer_resolution(struct time_t *val)
{
 if ((sizeof(long) == sizeof(time_t)) &&
  (sizeof(long) == sizeof(long)))
  clock_getres(0, (struct timespec *)val);
 else
 {
  struct timespec t;
  clock_getres(0, &t);
  val->seconds = t.tv_sec;
  val->nanoseconds = t.tv_nsec;
 }
}

variable_types: struct time_t { long seconds; long nanoseconds; } *

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


decompiled_code:
int __fastcall get_time(struct timespec *tp)
{
return clock_gettime(0, tp);
}

canonical_decompiled_code:
int __fastcall func1(struct timespec *tp)
{
return clock_gettime(0, tp);
}

original_code:
void get_time(struct time_t* val)
{
 if ((sizeof(int64_t) == sizeof(time_t)) &&
  (sizeof(int64_t) == sizeof(long)))
  clock_gettime(0, (struct timespec *)val);
 else
 {
  struct timespec t;
  clock_gettime(0, &t);
  val->seconds = t.tv_sec;
  val->nanoseconds = t.tv_nsec;
 }
}

canonical_original_code:
void get_time(struct time_t *val)
{
 if ((sizeof(long) == sizeof(time_t)) &&
  (sizeof(long) == sizeof(long)))
  clock_gettime(0, (struct timespec *)val);
 else
 {
  struct timespec t;
  clock_gettime(0, &t);
  val->seconds = t.tv_sec;
  val->nanoseconds = t.tv_nsec;
 }
}

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

user_defined_types: struct time_t { long seconds; long nanoseconds; }


original_code:
double get_time_diff(
 struct time_t* val1, struct time_t* val2)
{
 int64_t seconds = val2->seconds - val1->seconds;
 int64_t nanoseconds = val2->nanoseconds - val1->nanoseconds;
 if (val2->nanoseconds < val1->nanoseconds)
 {
  seconds--;
  nanoseconds = (1000000000 - val1->nanoseconds) + val2->nanoseconds;
 }
 return (double)0.000000001 * nanoseconds + seconds;
}

canonical_original_code:
double get_time_diff(struct time_t *val1, struct time_t *val2)
{
 long seconds = val2->seconds - val1->seconds;
 long nanoseconds = val2->nanoseconds - val1->nanoseconds;
 if (val2->nanoseconds < val1->nanoseconds)
 {
  seconds--;
  nanoseconds = (1000000000 - val1->nanoseconds) + val2->nanoseconds;
 }
 return (double)0.000000001 * nanoseconds + seconds;
}

variable_types: seconds long

variable_types: nanoseconds long


original_code:
void print_time_diff(
 struct time_t* val1, struct time_t* val2)
{
 int64_t seconds = val2->seconds - val1->seconds;
 int64_t nanoseconds = val2->nanoseconds - val1->nanoseconds;
 if (val2->nanoseconds < val1->nanoseconds)
 {
  seconds--;
  nanoseconds = (1000000000 - val1->nanoseconds) + val2->nanoseconds;
 }
 if (sizeof(uint64_t) == sizeof(long))
  printf("%ld.%09ld", (long)seconds, (long)nanoseconds);
 else
  printf("%lld.%09lld", seconds, nanoseconds);
}

canonical_original_code:
void print_time_diff(struct time_t *val1, struct time_t *val2)
{
 long seconds = val2->seconds - val1->seconds;
 long nanoseconds = val2->nanoseconds - val1->nanoseconds;
 if (val2->nanoseconds < val1->nanoseconds)
 {
  seconds--;
  nanoseconds = (1000000000 - val1->nanoseconds) + val2->nanoseconds;
 }
 if (sizeof(unsigned long) == sizeof(long))
  printf("%ld.%09ld", (long)seconds, (long)nanoseconds);
 else
  printf("%lld.%09lld", seconds, nanoseconds);
}


decompiled_code:
__int64 tl_endstats()
{
printf("\ntotal memory used: %9ld\n", All_Mem);
return a_stats();
}

canonical_decompiled_code:
__int64 func0()
{
printf("\ntotal memory used: %9ld\n", All_Mem);
return a_stats();
}

original_code:
static void
tl_endstats(void)
{ extern int Stack_mx;
 printf("\ntotal memory used: %9ld\n", All_Mem);
 a_stats();
}

canonical_original_code:
static void tl_endstats()
{ extern int Stack_mx;
 printf("\ntotal memory used: %9ld\n", All_Mem);
 a_stats();
}


alldone

decompiled_code:
void __fastcall __noreturn alldone(int status)
{
if ( out1 )
unlink(&out1);
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(int status)
{
if ( out1 )
unlink(&out1);
exit(status);
}

original_code:
void
alldone(int estatus)
{
        if (strlen(out1) > 0)
                (void) unlink((const char *)out1);
        exit(estatus);
}

canonical_original_code:
void alldone(int estatus)
{
        if (strlen(out1) > 0)
                (void) unlink((char *)out1);
        exit(estatus);
}

variable_types: estatus int


decompiled_code:
__int64 tl_Getchar()
{
int v0; // eax
__int64 result; // rax

v0 = cnt;
if ( cnt >= hasuform )
{
++cnt;
result = 0xFFFFFFFFLL;
}
else
{
++cnt;
result = (unsigned int)uform[v0];
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
__int64 result; // rax

v0 = cnt;
if ( cnt >= hasuform )
{
++cnt;
result = 0xFFFFFFFFLL;
}
else
{
++cnt;
result = (unsigned int)uform[v0];
}
return result;
}

original_code:
int
tl_Getchar(void)
{
 if (cnt < hasuform)
  return uform[cnt++];
 cnt++;
 return -1;
}

canonical_original_code:
int tl_Getchar()
{
 if (cnt < hasuform)
  return uform[cnt++];
 cnt++;
 return -1;
}


decompiled_code:
int put_uform()
{
return fputs(uform, tl_out);
}

canonical_decompiled_code:
int func3()
{
return fputs(uform, tl_out);
}

original_code:
void
put_uform(void)
{
 fprintf(tl_out, "%s", uform);
}

canonical_original_code:
void put_uform()
{
 fprintf(tl_out, "%s", uform);
}


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

result = (unsigned int)cnt;
if ( cnt > 0 )
result = (unsigned int)--cnt;
return result;
}

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

result = (unsigned int)cnt;
if ( cnt > 0 )
result = (unsigned int)--cnt;
return result;
}

original_code:
void
tl_UnGetchar(void)
{
 if (cnt > 0) cnt--;
}

canonical_original_code:
void tl_UnGetchar()
{
 if (cnt > 0) cnt--;
}


usage

decompiled_code:
void __noreturn usage()
{
puts("usage: ltl2ba [-flag] -f formula");
puts(" or -F file");
puts(" -h\t\tthis help message");
puts(" -V\t\tprint version and exit");
puts(" -t type\toutput format; default is \"spin\"; can be one of the following:\n\t\tspin, dot, gexf");
printf(" -f \"formula\"\ttranslate LTL ");
puts("into never claim");
printf(" -F file\tlike -f, but with the LTL ");
puts("formula stored in a 1-line file");
puts(" -d\t\tdisplay automata (D)escription at each step");
puts(" -s\t\tcomputing time and automata sizes (S)tatistics");
puts(" -l\t\tdisable (L)ogic formula simplification");
puts(" -p\t\tdisable a-(P)osteriori simplification");
puts(" -o\t\tdisable (O)n-the-fly simplification");
puts(" -c\t\tdisable strongly (C)onnected components simplification");
puts(" -a\t\tdisable trick in (A)ccepting conditions");
alldone(1);
}

canonical_decompiled_code:
void __noreturn func5()
{
puts("usage: ltl2ba [-flag] -f formula");
puts(" or -F file");
puts(" -h\t\tthis help message");
puts(" -V\t\tprint version and exit");
puts(" -t type\toutput format; default is \"spin\"; can be one of the following:\n\t\tspin, dot, gexf");
printf(" -f \"formula\"\ttranslate LTL ");
puts("into never claim");
printf(" -F file\tlike -f, but with the LTL ");
puts("formula stored in a 1-line file");
puts(" -d\t\tdisplay automata (D)escription at each step");
puts(" -s\t\tcomputing time and automata sizes (S)tatistics");
puts(" -l\t\tdisable (L)ogic formula simplification");
puts(" -p\t\tdisable a-(P)osteriori simplification");
puts(" -o\t\tdisable (O)n-the-fly simplification");
puts(" -c\t\tdisable strongly (C)onnected components simplification");
puts(" -a\t\tdisable trick in (A)ccepting conditions");
func1(1);
}

original_code:
void
usage(void)
{
        printf("usage: ltl2ba [-flag] -f formula\n");
        printf("                   or -F file\n");
  printf(" -h\t\tthis help message\n");
  printf(" -V\t\tprint version and exit\n");
  printf(" -t type\toutput format; default is \"spin\"; can be one of the following:\n\t\tspin, dot, gexf\n");
        printf(" -f \"formula\"\ttranslate LTL ");
        printf("into never claim\n");
        printf(" -F file\tlike -f, but with the LTL ");
        printf("formula stored in a 1-line file\n");
        printf(" -d\t\tdisplay automata (D)escription at each step\n");
        printf(" -s\t\tcomputing time and automata sizes (S)tatistics\n");
        printf(" -l\t\tdisable (L)ogic formula simplification\n");
        printf(" -p\t\tdisable a-(P)osteriori simplification\n");
        printf(" -o\t\tdisable (O)n-the-fly simplification\n");
        printf(" -c\t\tdisable strongly (C)onnected components simplification\n");
        printf(" -a\t\tdisable trick in (A)ccepting conditions\n");
        alldone(1);
}

canonical_original_code:
void usage()
{
        printf("usage: ltl2ba [-flag] -f formula\n");
        printf("                   or -F file\n");
  printf(" -h\t\tthis help message\n");
  printf(" -V\t\tprint version and exit\n");
  printf(" -t type\toutput format; default is \"spin\"; can be one of the following:\n\t\tspin, dot, gexf\n");
        printf(" -f \"formula\"\ttranslate LTL ");
        printf("into never claim\n");
        printf(" -F file\tlike -f, but with the LTL ");
        printf("formula stored in a 1-line file\n");
        printf(" -d\t\tdisplay automata (D)escription at each step\n");
        printf(" -s\t\tcomputing time and automata sizes (S)tatistics\n");
        printf(" -l\t\tdisable (L)ogic formula simplification\n");
        printf(" -p\t\tdisable a-(P)osteriori simplification\n");
        printf(" -o\t\tdisable (O)n-the-fly simplification\n");
        printf(" -c\t\tdisable strongly (C)onnected components simplification\n");
        printf(" -a\t\tdisable trick in (A)ccepting conditions\n");
        alldone(1);
}


decompiled_code:
__int64 __fastcall tl_main(int a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
const char *v4; // rsi
__int64 v5; // rax
char *v6; // rcx
char v7; // dl
_BYTE *v8; // rax

if ( a1 > 1 )
{
v2 = a2;
v3 = a2 + 16LL * (((unsigned int)(a1 - 2) >> 1) + 1);
do
{
v8 = *(_BYTE **)(v2 + 8);
if ( *v8 != 45 )
break;
if ( v8[1] != 102 )
usage();
v6 = *(char **)(v2 + 16);
v7 = *v6;
if ( *v6 <= 0 )
{
v4 = *(const char **)(v2 + 16);
}
else
{
v5 = 0LL;
do
{
if ( (unsigned __int8)(v7 - 9) <= 1u || v7 == 34 )
*v6 = 32;
v4 = *(const char **)(v2 + 16);
v6 = (char *)&v4[++v5];
v7 = v4[v5];
}
while ( v7 > (int)v5 );
}
strcpy(uform, v4);
hasuform = strlen(uform);
v2 += 16LL;
}
while ( v3 != v2 );
}
if ( !hasuform )
usage();
tl_parse();
if ( tl_stats )
tl_endstats();
return (unsigned int)tl_errs;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
const char *v4; // rsi
__int64 v5; // rax
char *v6; // rcx
char v7; // dl
_BYTE *v8; // rax

if ( a1 > 1 )
{
v2 = a2;
v3 = a2 + 16LL * (((unsigned int)(a1 - 2) >> 1) + 1);
do
{
v8 = *(_BYTE **)(v2 + 8);
if ( *v8 != 45 )
break;
if ( v8[1] != 102 )
func5();
v6 = *(char **)(v2 + 16);
v7 = *v6;
if ( *v6 <= 0 )
{
v4 = *(const char **)(v2 + 16);
}
else
{
v5 = 0LL;
do
{
if ( (unsigned __int8)(v7 - 9) <= 1u || v7 == 34 )
*v6 = 32;
v4 = *(const char **)(v2 + 16);
v6 = (char *)&v4[++v5];
v7 = v4[v5];
}
while ( v7 > (int)v5 );
}
strcpy(uform, v4);
hasuform = strlen(uform);
v2 += 16LL;
}
while ( v3 != v2 );
}
if ( !hasuform )
func5();
tl_parse();
if ( tl_stats )
func0();
return (unsigned int)tl_errs;
}

original_code:
int
tl_main(int argc, char *argv[])
{ int i;
 while (argc > 1 && argv[1][0] == '-')
 { switch (argv[1][1]) {
  case 'f': argc--; argv++;
    for (i = 0; i < argv[1][i]; i++)
    { if (argv[1][i] == '\t'
     || argv[1][i] == '\"'
     || argv[1][i] == '\n')
      argv[1][i] = ' ';
    }
    strcpy(uform, argv[1]);
    hasuform = strlen(uform);
    break;
  default : usage();
  }
  argc--; argv++;
 }
 if (hasuform == 0) usage();
 tl_parse();
 if (tl_stats) tl_endstats();
 return tl_errs;
}

variable_types: i int