decompiled_code:
void __fastcall update_display_char(__int64 a1, __int64 a2, const unsigned __int16 *a3)
{
int v3; // eax
int v4; // ebp
__int64 v5; // rcx
__int64 v6; // rsi
__int64 v7; // rcx
int v8; // eax
int v9; // edx
int v10; // eax
int v11; // [rsp+Ch] [rbp-3Ch] BYREF
int v12[4]; // [rsp+10h] [rbp-38h]
int v13[10]; // [rsp+20h] [rbp-28h]

if ( !sr_end )
sr_end = screen_height - 1;
if ( (_BYTE)a1 == 27 )
{
if ( esc_size_5 )
{
fwrite("warning: Escape sequence [\\33", 1uLL, 0x1DuLL, logff);
display_esc_seq_0(logff);
fwrite("] aborted by an ESC\n", 1uLL, 0x14uLL, logff);
}
esc_4[0] = 27;
esc_size_5 = 1;
}
else
{
v3 = esc_size_5;
if ( esc_size_5 )
{
v4 = ++esc_size_5;
esc_4[v3] = a1;
v5 = (unsigned int)(unsigned __int8)byte_951 - 40;
if ( v4 == 3 && (unsigned __int8)(byte_951 - 40) <= 1u )
{
esc_size_5 = 0;
return;
}
LOBYTE(a3) = v4 == 2;
v6 = (unsigned int)(unsigned __int8)byte_951 - 61;
if ( (unsigned __int8)(byte_951 - 61) <= 1u && v4 == 2 )
{
esc_size_5 = 0;
return;
}
if ( byte_951 == 68 && v4 == 2 )
{
if ( cur_row_3 == sr_end )
{
scroll_up();
}
else if ( cur_row_3 < screen_height - 1 )
{
++cur_row_3;
}
esc_size_5 = 0;
}
else if ( byte_951 == 77 && v4 == 2 )
{
if ( cur_row_3 == sr_start )
{
scroll_down();
}
else if ( cur_row_3 > 0 )
{
--cur_row_3;
}
esc_size_5 = 0;
}
else
{
if ( byte_951 == 55 && v4 == 2 )
{
saved_row = cur_row_3;
saved_col = cur_col_2;
esc_size_5 = 0;
return;
}
if ( byte_951 == 56 && v4 == 2 )
{
cur_row_3 = saved_row;
cur_col_2 = saved_col;
esc_size_5 = 0;
return;
}
if ( byte_951 == 72 && v4 == 2 )
{
fwrite("Warning: Ignoring request to set tab-stop.\n", 1uLL, 0x2BuLL, logff);
esc_size_5 = 0;
return;
}
if ( byte_951 != 91 )
{
if ( v4 != 20 )
{
if ( (unsigned __int8)(byte_951 - 40) > 1u && v4 == 2 )
{
fprintf(logff, "Warning: Ignoring unknown escape sequence [\\33 \\%o]\n", (unsigned int)byte_951);
esc_size_5 = 0;
}
return;
}
LABEL_90:
cleanup(a1, v6, a3, v5);
fwrite("Unknown escape sequence \\33", 1uLL, 0x1BuLL, stderr);
display_esc_seq_0(stderr);
fputc(10, stderr);
exit(27);
}
a3 = *_ctype_b_loc();
if ( (a3[(char)a1] & 0x400) == 0 )
{
if ( v4 != 20 )
return;
goto LABEL_90;
}
switch ( (char)a1 )
{
case 'A':
case 'B':
case 'C':
case 'D':
v12[0] = -1;
v12[1] = 1;
v12[2] = 0;
v12[3] = 0;
v13[0] = 0;
v13[1] = 0;
v13[2] = 1;
v13[3] = -1;
v11 = 1;
_isoc99_sscanf(&unk_952, "%d", &v11);
v7 = (char)a1 - 65;
v8 = cur_col_2 + v13[v7] * v11;
v9 = screen_height - 1;
if ( cur_row_3 + v12[v7] * v11 < screen_height )
v9 = cur_row_3 + v12[v7] * v11;
if ( v9 < 0 )
v9 = 0;
cur_row_3 = v9;
if ( v8 >= screen_width )
v8 = screen_width - 1;
cur_col_2 = v8;
if ( v8 < 0 )
cur_col_2 = 0;
at_sr_bottom_1 = 0;
break;
case 'H':
if ( v4 == 3 )
{
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
else
{
if ( (unsigned int)_isoc99_sscanf(&unk_952, "%d;%d", &cur_row_3, &cur_col_2) != 2 )
{
((void (*)(void))cleanup)();
fwrite("Invalid cursor position sequence [\\33", 1uLL, 0x25uLL, stderr);
display_esc_seq_0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(72);
}
--cur_row_3;
--cur_col_2;
at_sr_bottom_1 = 0;
}
break;
case 'J':
case 'K':
case 'h':
case 'i':
case 'l':
case 'm':
break;
case 'g':
fwrite("Warning: Ignoring request to clear tab-stop.\n", 1uLL, 0x2DuLL, logff);
break;
case 'r':
if ( v4 == 3 )
{
sr_start = 0;
sr_end = screen_height - 1;
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
else
{
if ( (unsigned int)_isoc99_sscanf(&unk_952, "%d;%d", &sr_start, &sr_end) != 2 )
{
((void (*)(void))cleanup)();
fwrite("Invalid scroll region sequence [\\33", 1uLL, 0x23uLL, stderr);
display_esc_seq_0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(114);
}
--sr_start;
--sr_end;
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
break;
default:
fwrite("warning: Unknown escape sequence [\\33", 1uLL, 0x25uLL, logff);
display_esc_seq_0(logff);
fwrite("], ignoring.\n", 1uLL, 0xDuLL, logff);
break;
}
esc_size_5 = 0;
}
}
else
{
switch ( (char)a1 )
{
case 7:
case 14:
case 15:
return;
case 8:
v10 = cur_col_2 - 1;
if ( cur_col_2 - 1 < 0 )
v10 = 0;
cur_col_2 = v10;
at_sr_bottom_1 = 0;
break;
case 9:
cur_col_2 = (cur_col_2 + 8) & 0xFFFFFFF8;
if ( cur_col_2 >= screen_width )
{
cur_col_2 = 0;
goto LABEL_71;
}
break;
case 10:
LABEL_71:
if ( cur_row_3 == sr_end )
{
scroll_up();
}
else if ( cur_row_3 + 1 < screen_height )
{
++cur_row_3;
}
at_sr_bottom_1 = 0;
break;
case 13:
cur_col_2 = 0;
at_sr_bottom_1 = 0;
break;
default:
if ( ((*_ctype_b_loc())[(char)a1] & 0x4000) == 0 )
fprintf(logff, "Warning: Got unhandled non-printable character \\%o\n", (unsigned int)(char)a1);
if ( at_sr_bottom_1 )
{
scroll_up();
at_sr_bottom_1 = 0;
}
*(_BYTE *)(display + cur_col_2 + screen_width * cur_row_3) = a1;
if ( cur_col_2 + 1 < screen_width )
{
++cur_col_2;
}
else
{
cur_col_2 = 0;
if ( cur_row_3 == sr_end )
{
at_sr_bottom_1 = 1;
}
else if ( cur_row_3 + 1 < screen_height )
{
++cur_row_3;
}
}
break;
}
}
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, __int64 a2, const unsigned __int16 *a3)
{
int v3; // eax
int v4; // ebp
__int64 v5; // rcx
__int64 v6; // rsi
__int64 v7; // rcx
int v8; // eax
int v9; // edx
int v10; // eax
int v11; // [rsp+Ch] [rbp-3Ch] BYREF
int v12[4]; // [rsp+10h] [rbp-38h]
int v13[10]; // [rsp+20h] [rbp-28h]

if ( !sr_end )
sr_end = screen_height - 1;
if ( (_BYTE)a1 == 27 )
{
if ( esc_size_5 )
{
fwrite("warning: Escape sequence [\\33", 1uLL, 0x1DuLL, logff);
func0(logff);
fwrite("] aborted by an ESC\n", 1uLL, 0x14uLL, logff);
}
esc_4[0] = 27;
esc_size_5 = 1;
}
else
{
v3 = esc_size_5;
if ( esc_size_5 )
{
v4 = ++esc_size_5;
esc_4[v3] = a1;
v5 = (unsigned int)(unsigned __int8)byte_951 - 40;
if ( v4 == 3 && (unsigned __int8)(byte_951 - 40) <= 1u )
{
esc_size_5 = 0;
return;
}
LOBYTE(a3) = v4 == 2;
v6 = (unsigned int)(unsigned __int8)byte_951 - 61;
if ( (unsigned __int8)(byte_951 - 61) <= 1u && v4 == 2 )
{
esc_size_5 = 0;
return;
}
if ( byte_951 == 68 && v4 == 2 )
{
if ( cur_row_3 == sr_end )
{
func2();
}
else if ( cur_row_3 < screen_height - 1 )
{
++cur_row_3;
}
esc_size_5 = 0;
}
else if ( byte_951 == 77 && v4 == 2 )
{
if ( cur_row_3 == sr_start )
{
func1();
}
else if ( cur_row_3 > 0 )
{
--cur_row_3;
}
esc_size_5 = 0;
}
else
{
if ( byte_951 == 55 && v4 == 2 )
{
saved_row = cur_row_3;
saved_col = cur_col_2;
esc_size_5 = 0;
return;
}
if ( byte_951 == 56 && v4 == 2 )
{
cur_row_3 = saved_row;
cur_col_2 = saved_col;
esc_size_5 = 0;
return;
}
if ( byte_951 == 72 && v4 == 2 )
{
fwrite("Warning: Ignoring request to set tab-stop.\n", 1uLL, 0x2BuLL, logff);
esc_size_5 = 0;
return;
}
if ( byte_951 != 91 )
{
if ( v4 != 20 )
{
if ( (unsigned __int8)(byte_951 - 40) > 1u && v4 == 2 )
{
fprintf(logff, "Warning: Ignoring unknown escape sequence [\\33 \\%o]\n", (unsigned int)byte_951);
esc_size_5 = 0;
}
return;
}
LABEL_90:
cleanup(a1, v6, a3, v5);
fwrite("Unknown escape sequence \\33", 1uLL, 0x1BuLL, stderr);
func0(stderr);
fputc(10, stderr);
exit(27);
}
a3 = *_ctype_b_loc();
if ( (a3[(char)a1] & 0x400) == 0 )
{
if ( v4 != 20 )
return;
goto LABEL_90;
}
switch ( (char)a1 )
{
case 'A':
case 'B':
case 'C':
case 'D':
v12[0] = -1;
v12[1] = 1;
v12[2] = 0;
v12[3] = 0;
v13[0] = 0;
v13[1] = 0;
v13[2] = 1;
v13[3] = -1;
v11 = 1;
_isoc99_sscanf(&unk_952, "%d", &v11);
v7 = (char)a1 - 65;
v8 = cur_col_2 + v13[v7] * v11;
v9 = screen_height - 1;
if ( cur_row_3 + v12[v7] * v11 < screen_height )
v9 = cur_row_3 + v12[v7] * v11;
if ( v9 < 0 )
v9 = 0;
cur_row_3 = v9;
if ( v8 >= screen_width )
v8 = screen_width - 1;
cur_col_2 = v8;
if ( v8 < 0 )
cur_col_2 = 0;
at_sr_bottom_1 = 0;
break;
case 'H':
if ( v4 == 3 )
{
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
else
{
if ( (unsigned int)_isoc99_sscanf(&unk_952, "%d;%d", &cur_row_3, &cur_col_2) != 2 )
{
((void (*)(void))cleanup)();
fwrite("Invalid cursor position sequence [\\33", 1uLL, 0x25uLL, stderr);
func0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(72);
}
--cur_row_3;
--cur_col_2;
at_sr_bottom_1 = 0;
}
break;
case 'J':
case 'K':
case 'h':
case 'i':
case 'l':
case 'm':
break;
case 'g':
fwrite("Warning: Ignoring request to clear tab-stop.\n", 1uLL, 0x2DuLL, logff);
break;
case 'r':
if ( v4 == 3 )
{
sr_start = 0;
sr_end = screen_height - 1;
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
else
{
if ( (unsigned int)_isoc99_sscanf(&unk_952, "%d;%d", &sr_start, &sr_end) != 2 )
{
((void (*)(void))cleanup)();
fwrite("Invalid scroll region sequence [\\33", 1uLL, 0x23uLL, stderr);
func0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(114);
}
--sr_start;
--sr_end;
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
break;
default:
fwrite("warning: Unknown escape sequence [\\33", 1uLL, 0x25uLL, logff);
func0(logff);
fwrite("], ignoring.\n", 1uLL, 0xDuLL, logff);
break;
}
esc_size_5 = 0;
}
}
else
{
switch ( (char)a1 )
{
case 7:
case 14:
case 15:
return;
case 8:
v10 = cur_col_2 - 1;
if ( cur_col_2 - 1 < 0 )
v10 = 0;
cur_col_2 = v10;
at_sr_bottom_1 = 0;
break;
case 9:
cur_col_2 = (cur_col_2 + 8) & 0xFFFFFFF8;
if ( cur_col_2 >= screen_width )
{
cur_col_2 = 0;
goto LABEL_71;
}
break;
case 10:
LABEL_71:
if ( cur_row_3 == sr_end )
{
func2();
}
else if ( cur_row_3 + 1 < screen_height )
{
++cur_row_3;
}
at_sr_bottom_1 = 0;
break;
case 13:
cur_col_2 = 0;
at_sr_bottom_1 = 0;
break;
default:
if ( ((*_ctype_b_loc())[(char)a1] & 0x4000) == 0 )
fprintf(logff, "Warning: Got unhandled non-printable character \\%o\n", (unsigned int)(char)a1);
if ( at_sr_bottom_1 )
{
func2();
at_sr_bottom_1 = 0;
}
*(_BYTE *)(display + cur_col_2 + screen_width * cur_row_3) = a1;
if ( cur_col_2 + 1 < screen_width )
{
++cur_col_2;
}
else
{
cur_col_2 = 0;
if ( cur_row_3 == sr_end )
{
at_sr_bottom_1 = 1;
}
else if ( cur_row_3 + 1 < screen_height )
{
++cur_row_3;
}
}
break;
}
}
}
}

original_code:
static void update_display_char(char c) {
    static int cur_row, cur_col;
    static _Bool at_sr_bottom;
    static int esc_size;
    static char esc[20];
    void display_esc_seq(FILE *out) {
        for (int i = 1; i < esc_size; i++) {
            if (((*__ctype_b_loc ())[(int) ((esc[i]))] & (unsigned short int) _ISgraph))
                fprintf(out, " %c", esc[i]);
            else
                fprintf(out, " \\%o", esc[i]);
        }
    }
    if (sr_end == 0)
        sr_end = screen_height-1;
    if (c == '\33') {
        if (esc_size) {
            fprintf(logff, "warning: Escape sequence [\\33");
            display_esc_seq(logff);
            fprintf(logff, "] aborted by an ESC\n");
        }
        esc[0] = c;
        esc_size = 1;
        return;
    }
    if (esc_size) {
        esc[esc_size++] = c;
        if ((esc[1] == '(' || esc[1] == ')') && esc_size == 3) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if ((esc[1] == '>' || esc[1] == '=') && esc_size == 2) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if (esc[1] == 'D' && esc_size == 2) {
            trace("index: %.*s\n", esc_size, esc);
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row < screen_height-1)
                cur_row++;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'M' && esc_size == 2) {
            trace("reverse index: %.*s\n", esc_size, esc);
            if (cur_row == sr_start)
                scroll_down();
            else if (cur_row > 0)
                cur_row--;
            esc_size = 0;
            return;
        }
        if (esc[1] == '7' && esc_size == 2) {
            saved_row = cur_row;
            saved_col = cur_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == '8' && esc_size == 2) {
            cur_row = saved_row;
            cur_col = saved_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'H' && esc_size == 2) {
            fprintf(logff, "Warning: Ignoring request to set tab-stop.\n");
            esc_size = 0;
            return;
        }
        if (esc[1] == '[' && ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha)) {
            int rv;
            switch (c) {
                default:
                    fprintf(logff, "warning: Unknown escape sequence [\\33");
                    display_esc_seq(logff);
                    fprintf(logff, "], ignoring.\n");
                    break;
                case 'm':
                case 'h':
                case 'l':
                case 'i':
                case 'J': case 'K':
                    trace("ignoring: %.*s\n", esc_size, esc);
                    break;
                case 'g':
                    fprintf(logff, "Warning: Ignoring request to clear "
                                   "tab-stop.\n");
                    break;
                case 'r':
                    if (esc_size == 3) {
                        trace("setting default scroll region "
                              "(entire display)\n");
                        sr_start = 0;
                        sr_end = screen_height-1;
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &sr_start, &sr_end);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr, "Invalid scroll region sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('r');
                    }
                    sr_start--; sr_end--;
                    cur_row = cur_col = 0;
                    at_sr_bottom = 0;
                    trace("setting scroll region to %d-%d: %.*s\n",
                          sr_start, sr_end, esc_size, esc);
                    break;
                case 'H':
                    if (esc_size == 3) {
                        trace("going to (0, 0): %.*s\n", esc_size, esc);
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &cur_row, &cur_col);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr,
                                "Invalid cursor position sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('H');
                    }
                    cur_row--; cur_col--;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
                case 'A':
                case 'B':
                case 'C':
                case 'D':;
                    int drow[4] = { -1, 1, 0, 0 };
                    int dcol[4] = { 0, 0, 1, -1 };
                    rv = 1;
                    sscanf(esc+2, "%d", &rv);
                    cur_row += rv * drow[c-'A'];
                    cur_col += rv * dcol[c-'A'];
                    if (cur_row >= screen_height)
                        cur_row = screen_height-1;
                    if (cur_row < 0)
                        cur_row = 0;
                    if (cur_col >= screen_width)
                        cur_col = screen_width-1;
                    if (cur_col < 0)
                        cur_col = 0;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
            }
            esc_size = 0;
            return;
        }
        if (esc_size == 20) {
            cleanup();
            fprintf(stderr, "Unknown escape sequence \\33");
            display_esc_seq(stderr);
            fprintf(stderr, "\n");
            exit('\33');
        }
        if (esc_size == 2 && esc[1] != '[' && esc[1] != '(' && esc[1] != ')') {
            fprintf(logff, "Warning: Ignoring unknown escape sequence "
                           "[\\33 \\%o]\n", esc[1]);
            esc_size = 0;
        }
        return;
    }
    switch (c) {
        case '\a': case 016: case 017:
            trace("ignoring: \\%o\n", c);
            return;
        case '\r':
            cur_col = 0;
            trace("carriage return: going to (%d, %d)\n", cur_row, cur_col);
            at_sr_bottom = 0;
            return;
        case '\t':
            cur_col = (cur_col+8) & ~7;
            if (cur_col < screen_width)
                return;
            cur_col = 0;
        case '\n':
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row+1 < screen_height)
                cur_row++;
            at_sr_bottom = 0;
            trace("line feed: going to (%d, %d)\n", cur_row, cur_col);
            return;
        case '\b':
            cur_col--;
            if (cur_col < 0)
                cur_col = 0;
            trace("going to (%d, %d): %c\n", cur_row, cur_col, c);
            at_sr_bottom = 0;
            return;
        default:
            if (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint)) {
                fprintf(logff, "Warning: Got unhandled non-printable "
                               "character \\%o\n", c);
            }
            if (at_sr_bottom) {
                scroll_up();
                at_sr_bottom = 0;
            }
            (display[(cur_row)*screen_width + (cur_col)]) = c;
            trace("setting (%d, %d) to '%c' and ", cur_row, cur_col, c);
            if (cur_col+1 < screen_width) {
                cur_col++;
                trace("going to (%d, %d)\n", cur_row, cur_col);
                return;
            }
            cur_col = 0;
            if (cur_row == sr_end)
                at_sr_bottom = 1;
            else if (cur_row+1 < screen_height)
                cur_row++;
            trace("going to (%d, %d)\n", cur_row, cur_col);
            return;
    }
}

canonical_original_code:
static void update_display_char(char c) {
    static int cur_row; static int cur_col;
    static _Bool at_sr_bottom;
    static int esc_size;
    static char esc[20];
    void display_esc_seq(FILE *out) {
        for (int i = 1; i < esc_size; i++) {
            if (((*__ctype_b_loc ())[(int) ((esc[i]))] & (unsigned short) _ISgraph))
                fprintf(out, " %c", esc[i]);
            else
                fprintf(out, " \\%o", esc[i]);
        }
    }
    if (sr_end == 0)
        sr_end = screen_height-1;
    if (c == '\33') {
        if (esc_size) {
            fprintf(logff, "warning: Escape sequence [\\33");
            display_esc_seq(logff);
            fprintf(logff, "] aborted by an ESC\n");
        }
        esc[0] = c;
        esc_size = 1;
        return;
    }
    if (esc_size) {
        esc[esc_size++] = c;
        if ((esc[1] == '(' || esc[1] == ')') && esc_size == 3) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if ((esc[1] == '>' || esc[1] == '=') && esc_size == 2) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if (esc[1] == 'D' && esc_size == 2) {
            trace("index: %.*s\n", esc_size, esc);
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row < screen_height-1)
                cur_row++;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'M' && esc_size == 2) {
            trace("reverse index: %.*s\n", esc_size, esc);
            if (cur_row == sr_start)
                scroll_down();
            else if (cur_row > 0)
                cur_row--;
            esc_size = 0;
            return;
        }
        if (esc[1] == '7' && esc_size == 2) {
            saved_row = cur_row;
            saved_col = cur_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == '8' && esc_size == 2) {
            cur_row = saved_row;
            cur_col = saved_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'H' && esc_size == 2) {
            fprintf(logff, "Warning: Ignoring request to set tab-stop.\n");
            esc_size = 0;
            return;
        }
        if (esc[1] == '[' && ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha)) {
            int rv;
            switch (c) {
                default:
                    fprintf(logff, "warning: Unknown escape sequence [\\33");
                    display_esc_seq(logff);
                    fprintf(logff, "], ignoring.\n");
                    break;
                case 'm':
                case 'h':
                case 'l':
                case 'i':
                case 'J': case 'K':
                    trace("ignoring: %.*s\n", esc_size, esc);
                    break;
                case 'g':
                    fprintf(logff, "Warning: Ignoring request to clear "
                                   "tab-stop.\n");
                    break;
                case 'r':
                    if (esc_size == 3) {
                        trace("setting default scroll region "
                              "(entire display)\n");
                        sr_start = 0;
                        sr_end = screen_height-1;
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &sr_start, &sr_end);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr, "Invalid scroll region sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('r');
                    }
                    sr_start--; sr_end--;
                    cur_row = cur_col = 0;
                    at_sr_bottom = 0;
                    trace("setting scroll region to %d-%d: %.*s\n",
                          sr_start, sr_end, esc_size, esc);
                    break;
                case 'H':
                    if (esc_size == 3) {
                        trace("going to (0, 0): %.*s\n", esc_size, esc);
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &cur_row, &cur_col);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr,
                                "Invalid cursor position sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('H');
                    }
                    cur_row--; cur_col--;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
                case 'A':
                case 'B':
                case 'C':
                case 'D':;
                    int drow[4] = { -1, 1, 0, 0 };
                    int dcol[4] = { 0, 0, 1, -1 };
                    rv = 1;
                    sscanf(esc+2, "%d", &rv);
                    cur_row += rv * drow[c-'A'];
                    cur_col += rv * dcol[c-'A'];
                    if (cur_row >= screen_height)
                        cur_row = screen_height-1;
                    if (cur_row < 0)
                        cur_row = 0;
                    if (cur_col >= screen_width)
                        cur_col = screen_width-1;
                    if (cur_col < 0)
                        cur_col = 0;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
            }
            esc_size = 0;
            return;
        }
        if (esc_size == 20) {
            cleanup();
            fprintf(stderr, "Unknown escape sequence \\33");
            display_esc_seq(stderr);
            fprintf(stderr, "\n");
            exit('\33');
        }
        if (esc_size == 2 && esc[1] != '[' && esc[1] != '(' && esc[1] != ')') {
            fprintf(logff, "Warning: Ignoring unknown escape sequence "
                           "[\\33 \\%o]\n", esc[1]);
            esc_size = 0;
        }
        return;
    }
    switch (c) {
        case '\a': case 016: case 017:
            trace("ignoring: \\%o\n", c);
            return;
        case '\r':
            cur_col = 0;
            trace("carriage return: going to (%d, %d)\n", cur_row, cur_col);
            at_sr_bottom = 0;
            return;
        case '\t':
            cur_col = (cur_col+8) & ~7;
            if (cur_col < screen_width)
                return;
            cur_col = 0;
        case '\n':
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row+1 < screen_height)
                cur_row++;
            at_sr_bottom = 0;
            trace("line feed: going to (%d, %d)\n", cur_row, cur_col);
            return;
        case '\b':
            cur_col--;
            if (cur_col < 0)
                cur_col = 0;
            trace("going to (%d, %d): %c\n", cur_row, cur_col, c);
            at_sr_bottom = 0;
            return;
        default:
            if (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint)) {
                fprintf(logff, "Warning: Got unhandled non-printable "
                               "character \\%o\n", c);
            }
            if (at_sr_bottom) {
                scroll_up();
                at_sr_bottom = 0;
            }
            (display[(cur_row)*screen_width + (cur_col)]) = c;
            trace("setting (%d, %d) to '%c' and ", cur_row, cur_col, c);
            if (cur_col+1 < screen_width) {
                cur_col++;
                trace("going to (%d, %d)\n", cur_row, cur_col);
                return;
            }
            cur_col = 0;
            if (cur_row == sr_end)
                at_sr_bottom = 1;
            else if (cur_row+1 < screen_height)
                cur_row++;
            trace("going to (%d, %d)\n", cur_row, cur_col);
            return;
    }
}

variable_types: c char

variable_types:
esc
char [20]

variable_types: rv int

variable_types:
drow
int [4]

variable_types:
dcol
int [4]


repo: jlmjlm/atc-ai

decompiled_code:
void __fastcall update_display(__int64 a1, int a2)
{
const unsigned __int16 *v2; // rdx
char *v3; // rbx

vwrite(1LL, a1, (unsigned int)a2);
if ( a2 > 0 )
{
v3 = (char *)a1;
do
update_display_char((unsigned int)*v3++, a1, v2);
while ( v3 != (char *)(a1 + (unsigned int)(a2 - 1) + 1) );
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, int a2)
{
const unsigned __int16 *v2; // rdx
char *v3; // rbx

vwrite(1LL, a1, (unsigned int)a2);
if ( a2 > 0 )
{
v3 = (char *)a1;
do
func3((unsigned int)*v3++, a1, v2);
while ( v3 != (char *)(a1 + (unsigned int)(a2 - 1) + 1) );
}
}

original_code:
void update_display(const char *buf, int nchar) {
    vwrite(1, buf, nchar);
    for (int i = 0; i < nchar; i++)
        update_display_char(buf[i]);
}

canonical_original_code:
void update_display(char *buf, int nchar) {
    vwrite(1, buf, nchar);
    for (int i = 0; i < nchar; i++)
        update_display_char(buf[i]);
}

variable_types:
buf
char *

variable_types: nchar int

variable_types: i int

return_type: void


decompiled_code:
__int64 __fastcall queue_string(char *a1)
{
char i; // dl
__int16 v2; // cx
__int64 result; // rax

for ( i = *a1; *a1; i = *a1 )
{
v2 = tqtail;
tqueue[tqtail] = i;
result = (v2 + 1) & 0x3FF;
tqtail = (v2 + 1) & 0x3FF;
++a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char i; // dl
__int16 v2; // cx
__int64 result; // rax

for ( i = *a1; *a1; i = *a1 )
{
v2 = tqtail;
tqueue[tqtail] = i;
result = (v2 + 1) & 0x3FF;
tqtail = (v2 + 1) & 0x3FF;
++a1;
}
return result;
}

original_code:
static void queue_string(const char s[]) {
    while (*s) {
        tqueue[tqtail] = *s;
        tqtail = (tqtail+1)%1024u;
        s++;
    }
}

canonical_original_code:
static void queue_string(char s[]) {
    while (*s) {
        tqueue[tqtail] = *s;
        tqtail = (tqtail+1)%1024u;
        s++;
    }
}

variable_types:
s
char []


decompiled_code:
__int64 __fastcall order_new_bearing(char a1, int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ct%c\n", (unsigned int)a1, (unsigned int)*(char *)(bearings + 32LL * a2 + 12));
return queue_string(v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ct%c\n", (unsigned int)a1, (unsigned int)*(char *)(bearings + 32LL * a2 + 12));
return func0(v3);
}

original_code:
void order_new_bearing(char id, int bearing) {
    char order[20];
    sprintf(order, "%ct%c\n", id, bearings[bearing].key);
    queue_string(order);
}

variable_types: id char

variable_types: bearing int

variable_types:
order
char [20]


decompiled_code:
__int64 __fastcall order_new_altitude(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ca%d\n", (unsigned int)a1, a2);
return queue_string(v3);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ca%d\n", (unsigned int)a1, a2);
return func0(v3);
}

original_code:
void order_new_altitude(char id, int alt) {
    char order[20];
    sprintf(order, "%ca%d\n", id, alt);
    queue_string(order);
}

variable_types: alt int


decompiled_code:
__int64 __fastcall land_at_airport(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ctta%d\n%ca0\n", (unsigned int)a1, a2, (unsigned int)a1);
return queue_string(v3);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ctta%d\n%ca0\n", (unsigned int)a1, a2, (unsigned int)a1);
return func0(v3);
}

original_code:
void land_at_airport(char id, int airport_num) {
    char order[20];
    sprintf(order, "%ctta%d\n%ca0\n", id, airport_num, id);
    queue_string(order);
}


decompiled_code:
__int64 next_tick()
{
return queue_string("\n");
}

canonical_decompiled_code:
__int64 func4()
{
return func0("\n");
}

original_code:
void next_tick() {
    queue_string("\n");
}


decompiled_code:
__int64 mark_msg()
{
char v0; // al
char *v1; // rdi

v0 = mark_sense;
mark_sense ^= 1u;
mark_sent = 1;
v1 = "zm";
if ( v0 == 1 )
v1 = "zu";
return queue_string(v1);
}

canonical_decompiled_code:
__int64 func5()
{
char v0; // al
char *v1; // rdi

v0 = mark_sense;
mark_sense ^= 1u;
mark_sent = 1;
v1 = "zm";
if ( v0 == 1 )
v1 = "zu";
return func0(v1);
}

original_code:
void mark_msg() {
    mark_sense = !mark_sense;
    mark_sent = 1;
    queue_string(mark_sense ? "zm" : "zu");
}


decompiled_code:
__int64 de_mark_msg()
{
__int64 result; // rax
char v1[3]; // [rsp+Dh] [rbp-3h] BYREF

v1[0] = erase_char;
v1[1] = erase_char;
v1[2] = 0;
result = queue_string(v1);
mark_sent = 0;
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
char v1[3]; // [rsp+Dh] [rbp-3h] BYREF

v1[0] = erase_char;
v1[1] = erase_char;
v1[2] = 0;
result = func0(v1);
mark_sent = 0;
return result;
}

original_code:
void de_mark_msg() {
    char str[3] = { erase_char, erase_char, '\0' };
    queue_string(str);
    mark_sent = 0;
}

variable_types:
str
char [3]


decompiled_code:
__int64 get_ptm()
{
unsigned int v0; // ebx
unsigned __int16 v2; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v3; // [rsp+Ah] [rbp-Eh]

v0 = open("/dev/ptmx", 258);
grantpt(v0);
unlockpt(v0);
ioctl(0, 0x5413uLL, &v2);
ioctl(v0, 0x5414uLL, &v2);
screen_height = v2;
screen_width = v3;
display = count_malloc("get_ptm", v3 * v2 + 1);
memset(display, 32, screen_width * screen_height);
*(_BYTE *)(display + screen_width * screen_height) = 0;
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // ebx
unsigned __int16 v2; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v3; // [rsp+Ah] [rbp-Eh]

v0 = open("/dev/ptmx", 258);
grantpt(v0);
unlockpt(v0);
ioctl(0, 0x5413uLL, &v2);
ioctl(v0, 0x5414uLL, &v2);
screen_height = v2;
screen_width = v3;
display = count_malloc("get_ptm", v3 * v2 + 1);
memset(display, 32, screen_width * screen_height);
*(_BYTE *)(display + screen_width * screen_height) = 0;
return v0;
}

original_code:
int get_ptm() {
    struct winsize ws;
    int ptm = open("/dev/ptmx", 02|0400);
    grantpt(ptm);
    unlockpt(ptm);
    ioctl(0, 0x5413, &ws);
    ioctl(ptm, 0x5414, &ws);
    screen_height = ws.ws_row;
    screen_width = ws.ws_col;
    display = count_malloc(__func__, screen_height*screen_width+1);
    memset(display, ' ', screen_height*screen_width);
    display[screen_height*screen_width] = '\0';
    return ptm;
}

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

variable_types: ptm int

return_type: int


spawn

decompiled_code:
__pid_t __fastcall spawn(char *file, char **argv, int fd)
{
__pid_t result; // eax
char *v6; // r13
int v7; // edi
int v8; // ebp
int *v9; // rax
char *v10; // rax

fflush(stderr);
result = fork();
if ( !result )
{
setenv("TERM", "vt100", 1);
unsetenv("TERMCAP");
v6 = ptsname(fd);
close(fd);
close(0);
close(1);
close(2);
v7 = open("/dev/tty", 2);
if ( v7 != -1 )
ioctl(v7, 0x5422uLL);
v8 = open(v6, 2);
setsid();
ioctl(v8, 0x540EuLL);
dup2(v8, 0);
dup2(v8, 1);
dup2(v8, 2);
*argv = file;
execvp(file, argv);
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "exec of %s failed: %s\n", file, v10);
fflush(stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func1(char *file, char **argv, int fd)
{
__pid_t result; // eax
char *v6; // r13
int v7; // edi
int v8; // ebp
int *v9; // rax
char *v10; // rax

fflush(stderr);
result = fork();
if ( !result )
{
setenv("TERM", "vt100", 1);
unsetenv("TERMCAP");
v6 = ptsname(fd);
close(fd);
close(0);
close(1);
close(2);
v7 = open("/dev/tty", 2);
if ( v7 != -1 )
ioctl(v7, 0x5422uLL);
v8 = open(v6, 2);
setsid();
ioctl(v8, 0x540EuLL);
dup2(v8, 0);
dup2(v8, 1);
dup2(v8, 2);
*argv = file;
execvp(file, argv);
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "exec of %s failed: %s\n", file, v10);
fflush(stderr);
exit(-1);
}
return result;
}

original_code:
int spawn(const char *cmd, const char *args[], int ptm) {
    fflush(stderr);
    int pid = fork();
    if (pid == 0) {
        setenv("TERM", "vt100", 1);
        unsetenv("TERMCAP");
        const char *ptsfn = ptsname(ptm);
        close(ptm); close(0); close(1); close(2);
        int dt = open("/dev/tty", 02);
        if (dt != -1)
            ioctl(dt, 0x5422);
        int pts = open(ptsfn, 02);
        setsid();
        ioctl(pts, 0x540E);
        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);
        args[0] = cmd;
        execvp(cmd, (char *const *)args);
        fprintf(stderr, "exec of %s failed: %s\n", cmd, strerror((*__errno_location ())));
        fflush(stderr);
        _exit(-1);
    }
    return pid;
}

canonical_original_code:
int spawn(char *cmd, char *args[], int ptm) {
    fflush(stderr);
    int pid = fork();
    if (pid == 0) {
        setenv("TERM", "vt100", 1);
        unsetenv("TERMCAP");
        char *ptsfn = ptsname(ptm);
        close(ptm); close(0); close(1); close(2);
        int dt = open("/dev/tty", 02);
        if (dt != -1)
            ioctl(dt, 0x5422);
        int pts = open(ptsfn, 02);
        setsid();
        ioctl(pts, 0x540E);
        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);
        args[0] = cmd;
        execvp(cmd, (char **)args);
        fprintf(stderr, "exec of %s failed: %s\n", cmd, strerror((*__errno_location ())));
        fflush(stderr);
        _exit(-1);
    }
    return pid;
}

variable_types:
cmd
char *

variable_types:
args
char *[]

variable_types: pid int

variable_types:
ptsfn
char *

variable_types: dt int

variable_types: pts int


decompiled_code:
void __fastcall __noreturn count_packets(void *a1)
{
_WORD *v1; // rbx

v1 = malloc(0x5EEuLL);
in_buff = v1;
while ( (unsigned int)recvfrom(sock_fd, in_buff, 0x5EEuLL, 0, 0LL, 0LL) != -1 )
{
if ( v1[6] == 25600 && !memcmp(v1, &snd_mac, 6uLL) )
++total_recv_packets;
}
perror("recvfrom():");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
_WORD *v1; // rbx

v1 = malloc(0x5EEuLL);
in_buff = v1;
while ( (unsigned int)recvfrom(sock_fd, in_buff, 0x5EEuLL, 0, 0LL, 0LL) != -1 )
{
if ( v1[6] == 25600 && !memcmp(v1, &snd_mac, 6uLL) )
++total_recv_packets;
}
perror("recvfrom():");
exit(1);
}

original_code:
void * count_packets(void *ptr) {
 int recv;
 in_buff = (void*)malloc(1514 +4);
 struct ethhdr *in_hdr = (struct ethhdr *)in_buff;
 while (1) {
  recv = recvfrom(sock_fd, in_buff, 1518, 0, ((void *)0), ((void *)0));
  if (recv == -1) {
   perror("recvfrom():");
   exit(1);
  }
  if (in_hdr->h_proto == 0x6400 && memcmp( (const void*)in_hdr->h_dest, (const void*)snd_mac, 6) == 0 ) {
   total_recv_packets++;
  }
 }
}

canonical_original_code:
void *count_packets(void *ptr) {
 int recv;
 in_buff = (void *)malloc(1514 +4);
 struct ethhdr *in_hdr = (struct ethhdr *)in_buff;
 while (1) {
  recv = recvfrom(sock_fd, in_buff, 1518, 0, ((void *)0), ((void *)0));
  if (recv == -1) {
   perror("recvfrom():");
   exit(1);
  }
  if (in_hdr->h_proto == 0x6400 && memcmp( (void *)in_hdr->h_dest, (void *)snd_mac, 6) == 0 ) {
   total_recv_packets++;
  }
 }
}

variable_types:
ptr
void *

variable_types: recv int

variable_types: struct ethhdr { unsigned char h_dest[6]; unsigned char h_source[6]; unsigned short h_proto; } *

return_type: void *


sigint

decompiled_code:
void sigint()
{
int v0; // ebx
float v1; // xmm2_4
char dest[16]; // [rsp+10h] [rbp-38h] BYREF
__int16 v3; // [rsp+20h] [rbp-28h]

v0 = sock_fd;
if ( sock_fd != -1 )
{
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(v0, 0x8913uLL, dest);
v3 &= 0xFEFFu;
ioctl(sock_fd, 0x8914uLL, dest);
close(sock_fd);
free(out_buff);
free(in_buff);
v1 = 1.0 - (double)(int)total_recv_packets / (double)(int)total_sent_packets;
puts("Client terminating....");
printf("Totally sent: %ld packets\n", total_sent_packets);
printf("Totally recv: %ld packets\n", total_recv_packets);
printf("Ploss=%f\n", v1);
exit(0);
}
}

canonical_decompiled_code:
void func8()
{
int v0; // ebx
float v1; // xmm2_4
char dest[16]; // [rsp+10h] [rbp-38h] BYREF
__int16 v3; // [rsp+20h] [rbp-28h]

v0 = sock_fd;
if ( sock_fd != -1 )
{
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(v0, 0x8913uLL, dest);
v3 &= 0xFEFFu;
ioctl(sock_fd, 0x8914uLL, dest);
close(sock_fd);
free(out_buff);
free(in_buff);
v1 = 1.0 - (double)(int)total_recv_packets / (double)(int)total_sent_packets;
puts("Client terminating....");
printf("Totally sent: %ld packets\n", total_sent_packets);
printf("Totally recv: %ld packets\n", total_recv_packets);
printf("Ploss=%f\n", v1);
exit(0);
}
}

original_code:
void sigint(int signum) {
 struct ifreq ifr;
 if (sock_fd == -1)
  return;
 strncpy(ifr.ifr_ifrn.ifrn_name, sender_ifname, 16);
 ioctl(sock_fd, 0x8913, &ifr);
 ifr.ifr_ifru.ifru_flags &= ~IFF_PROMISC;
 ioctl(sock_fd, 0x8914, &ifr);
 close(sock_fd);
 free(out_buff);
 free(in_buff);
 float loss = 1-((double)total_recv_packets/(double)total_sent_packets);
 printf("Client terminating....\n");
 printf("Totally sent: %ld packets\n", total_sent_packets);
 printf("Totally recv: %ld packets\n", total_recv_packets);
 printf("Ploss=%f\n", loss);
 exit(0);
}

variable_types: signum int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; void *ifru_data; struct if_settings { unsigned int type; unsigned int size; union { struct raw_hdlc_proto { unsigned short encoding; unsigned short parity; } *raw_hdlc; struct cisco_proto { unsigned int interval; unsigned int timeout; } *cisco; struct fr_proto { unsigned int t391; unsigned int t392; unsigned int n391; unsigned int n392; unsigned int n393; unsigned short lmi; unsigned short dce; } *fr; struct fr_proto_pvc { unsigned int dlci; } *fr_pvc; struct fr_proto_pvc_info { unsigned int dlci; char master[16]; } *fr_pvc_info; struct sync_serial_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; } *sync; struct te1_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; unsigned int slot_map; } *te1; } ifs_ifsu; } ifru_settings; } ifr_ifru; }

variable_types: loss float

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; void *ifru_data; struct if_settings ifru_settings; } ifr_ifru; }

user_defined_types: struct if_settings { unsigned int type; unsigned int size; union { struct raw_hdlc_proto *raw_hdlc; struct cisco_proto *cisco; struct fr_proto *fr; struct fr_proto_pvc *fr_pvc; struct fr_proto_pvc_info *fr_pvc_info; struct sync_serial_settings *sync; struct te1_settings *te1; } ifs_ifsu; }


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char *v4; // rbx
unsigned __int16 v5; // bp
unsigned int v6; // eax
int v7; // edx
__int64 v8; // rax
int v9; // er12
int v10; // edx
__int64 v11; // rax
char v12; // r13
char v13; // r14
char v14; // r15
_BYTE *v15; // rax
_BYTE *v16; // rbx
__int64 v17; // rsi
__int64 v18; // [rsp+0h] [rbp-B8h]
int v19; // [rsp+18h] [rbp-A0h]
char v20; // [rsp+1Dh] [rbp-9Bh]
char v21; // [rsp+1Eh] [rbp-9Ah]
char v22; // [rsp+1Fh] [rbp-99h]
struct sockaddr addr; // [rsp+20h] [rbp-98h] BYREF
char v24; // [rsp+30h] [rbp-88h]
char v25; // [rsp+31h] [rbp-87h]
char v26; // [rsp+32h] [rbp-86h]
char v27; // [rsp+33h] [rbp-85h]
char dest[16]; // [rsp+40h] [rbp-78h] BYREF
int v29[8]; // [rsp+50h] [rbp-68h]
int v30; // [rsp+72h] [rbp-46h]
__int16 v31; // [rsp+76h] [rbp-42h]
pthread_t newthread[8]; // [rsp+78h] [rbp-40h] BYREF

v3 = (char *)malloc(0x5EEuLL);
out_buff = v3;
if ( argc != 4 )
{
printf("Missing arguments.\n%s [src_ifname] [dest_ifname] [time msec] \n", *argv);
exit(1);
}
v4 = v3;
sender_ifname = (char *)argv[1];
receiver_ifname = (char *)argv[2];
v19 = atoi(argv[3]);
v5 = _bswap_16(3);
v6 = socket(17, 3, v5);
sock_fd = v6;
if ( v6 != -1 )
{
printf("Successfully opened socket: %i\n", v6);
strncpy(dest, receiver_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
printf("Successfully got interface index: %i\n", (unsigned int)v29[0]);
v7 = ioctl(sock_fd, 0x8927uLL, dest);
v8 = 0LL;
if ( v7 != -1 )
{
do
{
*((_BYTE *)&v30 + v8) = *((_BYTE *)v29 + v8 + 2);
++v8;
}
while ( v8 != 6 );
strncpy(dest, sender_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
v9 = v29[0];
printf("Successfully got interface index: %i\n", (unsigned int)v29[0]);
v10 = ioctl(sock_fd, 0x8927uLL, dest);
v11 = 0LL;
if ( v10 != -1 )
{
do
{
*((_BYTE *)&snd_mac + v11) = *((_BYTE *)v29 + v11 + 2);
++v11;
}
while ( v11 != 6 );
printf(
"SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)snd_mac,
BYTE1(snd_mac),
BYTE2(snd_mac),
HIBYTE(snd_mac),
(unsigned __int8)word_4040E4,
HIBYTE(word_4040E4));
v12 = BYTE2(v30);
v13 = BYTE1(v30);
v14 = v30;
v20 = HIBYTE(v31);
LODWORD(v18) = HIBYTE(v31);
v21 = v31;
v22 = HIBYTE(v30);
printf(
"RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)v30,
BYTE1(v30),
BYTE2(v30),
HIBYTE(v30),
(unsigned __int8)v31,
v18);
addr.sa_family = 17;
*(_WORD *)addr.sa_data = v5;
*(_DWORD *)&addr.sa_data[2] = v9;
addr.sa_data[9] = 6;
addr.sa_data[10] = v14;
addr.sa_data[11] = v13;
addr.sa_data[12] = v12;
addr.sa_data[13] = v22;
v24 = v21;
v25 = v20;
v26 = 0;
v27 = 0;
if ( bind(sock_fd, &addr, 0x14u) != -1 )
{
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(sock_fd, 0x8913uLL, dest);
LOWORD(v29[0]) |= 0x100u;
ioctl(sock_fd, 0x8913uLL, dest);
signal(2, (__sighandler_t)sigint);
puts("Sending packets....");
*(_DWORD *)v4 = v30;
*((_WORD *)v4 + 2) = v31;
*(_DWORD *)(v4 + 6) = snd_mac;
*((_WORD *)v4 + 5) = word_4040E4;
v15 = v4 + 14;
v16 = v4 + 114;
do
*v15++ = 0;
while ( v15 != v16 );
pthread_create(newthread, 0LL, (void *(*)(void *))count_packets, 0LL);
while ( 1 )
{
sendto(sock_fd, out_buff, 0x72uLL, 0, &addr, 0x14u);
v17 = total_sent_packets + 1;
total_sent_packets = v17;
if ( v17 == 10 * (v17 / 10) )
printf("\rtotal_sent_packets: %ld total_recv_packets: %ld", v17, total_recv_packets);
usleep(1000 * v19);
}
}
perror("binding:");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("socket():");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char *v4; // rbx
unsigned __int16 v5; // bp
unsigned int v6; // eax
int v7; // edx
__int64 v8; // rax
int v9; // er12
int v10; // edx
__int64 v11; // rax
char v12; // r13
char v13; // r14
char v14; // r15
_BYTE *v15; // rax
_BYTE *v16; // rbx
__int64 v17; // rsi
__int64 v18; // [rsp+0h] [rbp-B8h]
int v19; // [rsp+18h] [rbp-A0h]
char v20; // [rsp+1Dh] [rbp-9Bh]
char v21; // [rsp+1Eh] [rbp-9Ah]
char v22; // [rsp+1Fh] [rbp-99h]
struct sockaddr addr; // [rsp+20h] [rbp-98h] BYREF
char v24; // [rsp+30h] [rbp-88h]
char v25; // [rsp+31h] [rbp-87h]
char v26; // [rsp+32h] [rbp-86h]
char v27; // [rsp+33h] [rbp-85h]
char dest[16]; // [rsp+40h] [rbp-78h] BYREF
int v29[8]; // [rsp+50h] [rbp-68h]
int v30; // [rsp+72h] [rbp-46h]
__int16 v31; // [rsp+76h] [rbp-42h]
pthread_t newthread[8]; // [rsp+78h] [rbp-40h] BYREF

v3 = (char *)malloc(0x5EEuLL);
out_buff = v3;
if ( argc != 4 )
{
printf("Missing arguments.\n%s [src_ifname] [dest_ifname] [time msec] \n", *argv);
exit(1);
}
v4 = v3;
sender_ifname = (char *)argv[1];
receiver_ifname = (char *)argv[2];
v19 = atoi(argv[3]);
v5 = func9(3);
v6 = socket(17, 3, v5);
sock_fd = v6;
if ( v6 != -1 )
{
printf("Successfully opened socket: %i\n", v6);
strncpy(dest, receiver_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
printf("Successfully got interface index: %i\n", (unsigned int)v29[0]);
v7 = ioctl(sock_fd, 0x8927uLL, dest);
v8 = 0LL;
if ( v7 != -1 )
{
do
{
*((_BYTE *)&v30 + v8) = *((_BYTE *)v29 + v8 + 2);
++v8;
}
while ( v8 != 6 );
strncpy(dest, sender_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
v9 = v29[0];
printf("Successfully got interface index: %i\n", (unsigned int)v29[0]);
v10 = ioctl(sock_fd, 0x8927uLL, dest);
v11 = 0LL;
if ( v10 != -1 )
{
do
{
*((_BYTE *)&snd_mac + v11) = *((_BYTE *)v29 + v11 + 2);
++v11;
}
while ( v11 != 6 );
printf(
"SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)snd_mac,
BYTE1(snd_mac),
BYTE2(snd_mac),
HIBYTE(snd_mac),
(unsigned __int8)word_4040E4,
HIBYTE(word_4040E4));
v12 = BYTE2(v30);
v13 = BYTE1(v30);
v14 = v30;
v20 = HIBYTE(v31);
LODWORD(v18) = HIBYTE(v31);
v21 = v31;
v22 = HIBYTE(v30);
printf(
"RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)v30,
BYTE1(v30),
BYTE2(v30),
HIBYTE(v30),
(unsigned __int8)v31,
v18);
addr.sa_family = 17;
*(_WORD *)addr.sa_data = v5;
*(_DWORD *)&addr.sa_data[2] = v9;
addr.sa_data[9] = 6;
addr.sa_data[10] = v14;
addr.sa_data[11] = v13;
addr.sa_data[12] = v12;
addr.sa_data[13] = v22;
v24 = v21;
v25 = v20;
v26 = 0;
v27 = 0;
if ( bind(sock_fd, &addr, 0x14u) != -1 )
{
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(sock_fd, 0x8913uLL, dest);
LOWORD(v29[0]) |= 0x100u;
ioctl(sock_fd, 0x8913uLL, dest);
signal(2, (__sighandler_t)sigint);
puts("Sending packets....");
*(_DWORD *)v4 = v30;
*((_WORD *)v4 + 2) = v31;
*(_DWORD *)(v4 + 6) = snd_mac;
*((_WORD *)v4 + 5) = word_4040E4;
v15 = v4 + 14;
v16 = v4 + 114;
do
*v15++ = 0;
while ( v15 != v16 );
pthread_create(newthread, 0LL, (void *(*)(void *))count_packets, 0LL);
while ( 1 )
{
sendto(sock_fd, out_buff, 0x72uLL, 0, &addr, 0x14u);
v17 = total_sent_packets + 1;
total_sent_packets = v17;
if ( v17 == 10 * (v17 / 10) )
printf("\rtotal_sent_packets: %ld total_recv_packets: %ld", v17, total_recv_packets);
usleep(1000 * v19);
}
}
perror("binding:");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("socket():");
exit(1);
}

original_code:
int main(int argc, char *argv[]) {
 pthread_t thread1;
 unsigned char rcv_mac[6];
 struct ifreq ifr;
 out_buff = (void*)malloc(1514 +4);
 unsigned char* data_ptr = out_buff + 14;
 struct ethhdr *out_hdr = (struct ethhdr *)out_buff;
 struct sockaddr_ll s_addr;
 int ifindex = 0;
 int i;
 int sent;
 int time;
 if (argc !=4)
     {
      printf("Missing arguments.\n"
       "%s [src_ifname] [dest_ifname] [time msec] \n",argv[0]);
      exit(1);
 }
 sender_ifname = argv[1];
 receiver_ifname = argv[2];
 time = atoi(argv[3]);
 sock_fd = socket(17, SOCK_RAW, htons(0x0003));
 if (sock_fd == -1) {
  perror("socket():");
  exit(1);
 }
 printf("Successfully opened socket: %i\n", sock_fd);
 strncpy(ifr.ifr_ifrn.ifrn_name, receiver_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  rcv_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 strncpy(ifr.ifr_ifrn.ifrn_name, sender_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  snd_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
   snd_mac[0],snd_mac[1],snd_mac[2],snd_mac[3],snd_mac[4],snd_mac[5]);
 printf("RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  rcv_mac[0],rcv_mac[1],rcv_mac[2],rcv_mac[3],rcv_mac[4],rcv_mac[5]);
 s_addr.sll_family = 17;
 s_addr.sll_protocol = htons(0x0003);
 s_addr.sll_ifindex = ifindex;
 s_addr.sll_halen = 6;
 s_addr.sll_addr[0] = rcv_mac[0];
 s_addr.sll_addr[1] = rcv_mac[1];
 s_addr.sll_addr[2] = rcv_mac[2];
 s_addr.sll_addr[3] = rcv_mac[3];
 s_addr.sll_addr[4] = rcv_mac[4];
 s_addr.sll_addr[5] = rcv_mac[5];
 s_addr.sll_addr[6] = 0x00;
 s_addr.sll_addr[7] = 0x00;
 if (bind(sock_fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
  perror("binding:");
  exit(1);
 }
 strncpy(ifr.ifr_ifrn.ifrn_name,sender_ifname,16);
 ioctl(sock_fd,0x8913,&ifr);
 ifr.ifr_ifru.ifru_flags |= IFF_PROMISC;
 ioctl(sock_fd,0x8913,&ifr);
 signal(2, sigint);
 printf("Sending packets....\n");
 memcpy(out_hdr->h_dest, (void*)rcv_mac, 6);
 memcpy(out_hdr->h_source, (void*)snd_mac, 6);
 for (i = 0; i < 100; i++) {
  data_ptr[i] = (unsigned char)(0);
 }
 pthread_create( &thread1, ((void *)0), count_packets, (void*) ((void *)0));
 while (1) {
  sent = sendto(sock_fd, out_buff, i+14, 0, (struct sockaddr*)&s_addr, sizeof(s_addr));
  total_sent_packets++;
  if (!(total_sent_packets%10)){
   printf("\rtotal_sent_packets: %ld "
    "total_recv_packets: %ld",total_sent_packets,total_recv_packets);
  }
  usleep(time*1000);
 }
 return -1;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 unsigned long thread1;
 unsigned char rcv_mac[6];
 struct ifreq ifr;
 out_buff = (void *)malloc(1514 +4);
 unsigned char *data_ptr = out_buff + 14;
 struct ethhdr *out_hdr = (struct ethhdr *)out_buff;
 struct sockaddr_ll s_addr;
 int ifindex = 0;
 int i;
 int sent;
 int time;
 if (argc !=4)
     {
      printf("Missing arguments.\n"
       "%s [src_ifname] [dest_ifname] [time msec] \n",argv[0]);
      exit(1);
 }
 sender_ifname = argv[1];
 receiver_ifname = argv[2];
 time = atoi(argv[3]);
 sock_fd = socket(17, SOCK_RAW, htons(0x0003));
 if (sock_fd == -1) {
  perror("socket():");
  exit(1);
 }
 printf("Successfully opened socket: %i\n", sock_fd);
 strncpy(ifr.ifr_ifrn.ifrn_name, receiver_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  rcv_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 strncpy(ifr.ifr_ifrn.ifrn_name, sender_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  snd_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
   snd_mac[0],snd_mac[1],snd_mac[2],snd_mac[3],snd_mac[4],snd_mac[5]);
 printf("RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  rcv_mac[0],rcv_mac[1],rcv_mac[2],rcv_mac[3],rcv_mac[4],rcv_mac[5]);
 s_addr.sll_family = 17;
 s_addr.sll_protocol = htons(0x0003);
 s_addr.sll_ifindex = ifindex;
 s_addr.sll_halen = 6;
 s_addr.sll_addr[0] = rcv_mac[0];
 s_addr.sll_addr[1] = rcv_mac[1];
 s_addr.sll_addr[2] = rcv_mac[2];
 s_addr.sll_addr[3] = rcv_mac[3];
 s_addr.sll_addr[4] = rcv_mac[4];
 s_addr.sll_addr[5] = rcv_mac[5];
 s_addr.sll_addr[6] = 0x00;
 s_addr.sll_addr[7] = 0x00;
 if (bind(sock_fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
  perror("binding:");
  exit(1);
 }
 strncpy(ifr.ifr_ifrn.ifrn_name,sender_ifname,16);
 ioctl(sock_fd,0x8913,&ifr);
 ifr.ifr_ifru.ifru_flags |= IFF_PROMISC;
 ioctl(sock_fd,0x8913,&ifr);
 signal(2, sigint);
 printf("Sending packets....\n");
 memcpy(out_hdr->h_dest, (void *)rcv_mac, 6);
 memcpy(out_hdr->h_source, (void *)snd_mac, 6);
 for (i = 0; i < 100; i++) {
  data_ptr[i] = (unsigned char)(0);
 }
 pthread_create( &thread1, ((void *)0), count_packets, (void *) ((void *)0));
 while (1) {
  sent = sendto(sock_fd, out_buff, i+14, 0, (struct sockaddr *)&s_addr, sizeof(s_addr));
  total_sent_packets++;
  if (!(total_sent_packets%10)){
   printf("\rtotal_sent_packets: %ld "
    "total_recv_packets: %ld",total_sent_packets,total_recv_packets);
  }
  usleep(time*1000);
 }
 return -1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct sockaddr_ll { unsigned short sll_family; unsigned short sll_protocol; int sll_ifindex; unsigned short sll_hatype; unsigned char sll_pkttype; unsigned char sll_halen; unsigned char sll_addr[8]; }

variable_types: ifindex int

variable_types: i int

variable_types: sent int

variable_types: time int

user_defined_types: struct ethhdr { unsigned char h_dest[6]; unsigned char h_source[6]; unsigned short h_proto; }

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

user_defined_types: struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; }

user_defined_types: struct raw_hdlc_proto { unsigned short encoding; unsigned short parity; }

user_defined_types: struct cisco_proto { unsigned int interval; unsigned int timeout; }

user_defined_types: struct fr_proto { unsigned int t391; unsigned int t392; unsigned int n391; unsigned int n392; unsigned int n393; unsigned short lmi; unsigned short dce; }

user_defined_types: struct fr_proto_pvc { unsigned int dlci; }

user_defined_types: struct fr_proto_pvc_info { unsigned int dlci; char master[16]; }

user_defined_types: struct sync_serial_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; }

user_defined_types: struct te1_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; unsigned int slot_map; }


decompiled_code:
__int64 __fastcall put_mac_address(__int64 a1, int a2)
{
char *v2; // rcx
unsigned int i; // eax
unsigned int v4; // edx
__int64 result; // rax

v2 = (char *)indexer;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
v4 = i;
v2 = (char *)indexer + 6 * v4 + 6;
}
*(_DWORD *)v2 = a1;
result = WORD2(a1);
*((_WORD *)v2 + 2) = WORD2(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
char *v2; // rcx
unsigned int i; // eax
unsigned int v4; // edx
__int64 result; // rax

v2 = (char *)indexer;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
v4 = i;
v2 = (char *)indexer + 6 * v4 + 6;
}
*(_DWORD *)v2 = a1;
result = WORD2(a1);
*((_WORD *)v2 + 2) = WORD2(a1);
return result;
}

original_code:
void put_mac_address(struct mac_address addr, int pos)
{
 int i;
 void *ptr = indexer;
  for (i=0; i < pos ; i++) {
   ptr = ptr + sizeof(struct mac_address);
  }
  memcpy(ptr, &addr, sizeof(struct mac_address));
}

variable_types: struct mac_address { unsigned char addr[6]; }

variable_types: pos int


decompiled_code:
char *__fastcall get_mac_address(int a1)
{
char *result; // rax

result = 0LL;
if ( array_size > a1 )
result = (char *)indexer + 6 * a1;
return result;
}

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

result = 0LL;
if ( array_size > a1 )
result = (char *)indexer + 6 * a1;
return result;
}

original_code:
struct mac_address * get_mac_address(int pos) {
 void * ptr = indexer;
 ptr = ptr + (sizeof(struct mac_address)*pos);
 return ((pos >= array_size) ? ((void *)0) : (struct mac_address*)ptr);
}

canonical_original_code:
struct mac_address *get_mac_address(int pos) {
 void *ptr = indexer;
 ptr = ptr + (sizeof(struct mac_address)*pos);
 return ((pos >= array_size) ? ((void *)0) : (struct mac_address *)ptr);
}

return_type: struct mac_address { unsigned char addr[6]; } *


decompiled_code:
__int64 __fastcall find_pos_by_mac_address(void *s2)
{
char *v1; // rbp
int v2; // er13
int v3; // ebx

v1 = (char *)indexer;
v2 = array_size;
v3 = 0;
while ( memcmp(v1, s2, 6uLL) )
{
if ( v2 <= v3 )
return (unsigned int)-1;
++v3;
v1 += 6;
}
if ( v2 <= v3 )
v3 = -1;
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *s2)
{
char *v1; // rbp
int v2; // er13
int v3; // ebx

v1 = (char *)indexer;
v2 = array_size;
v3 = 0;
while ( memcmp(v1, s2, 6uLL) )
{
if ( v2 <= v3 )
return (unsigned int)-1;
++v3;
v1 += 6;
}
if ( v2 <= v3 )
v3 = -1;
return (unsigned int)v3;
}

original_code:
int find_pos_by_mac_address(struct mac_address *addr) {
 int i=0;
 void * ptr = indexer;
 while(memcmp(ptr,addr,sizeof(struct mac_address)) && i < array_size)
 {
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
 return ((i >= array_size) ? -1 : i);
}

canonical_original_code:
int find_pos_by_mac_address(struct mac_address *addr) {
 int i = 0;
 void *ptr = indexer;
 while(memcmp(ptr,addr,sizeof(struct mac_address)) && i < array_size)
 {
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
 return ((i >= array_size) ? -1 : i);
}

variable_types: struct mac_address { unsigned char addr[6]; } *


decompiled_code:
void print_mac_address_array()
{
_WORD *v0; // rbx
int v1; // ebp
_WORD *v2; // rcx
unsigned int v3; // eax
int v4; // er8
__int16 v5; // dx

v0 = indexer;
if ( array_size > 0 )
{
v1 = 0;
do
{
v2 = malloc(6uLL);
v3 = *(_DWORD *)v0;
v4 = HIWORD(*(_DWORD *)v0);
*(_DWORD *)v2 = *(_DWORD *)v0;
v5 = v0[2];
v2[2] = v5;
printf(
"A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned int)v1++,
(unsigned __int8)v3,
BYTE1(v3),
(unsigned __int8)v4,
HIBYTE(v3),
(unsigned __int8)v5,
HIBYTE(v5));
v0 += 3;
}
while ( array_size > v1 );
}
}

canonical_decompiled_code:
void func3()
{
_WORD *v0; // rbx
int v1; // ebp
_WORD *v2; // rcx
unsigned int v3; // eax
int v4; // er8
__int16 v5; // dx

v0 = indexer;
if ( array_size > 0 )
{
v1 = 0;
do
{
v2 = malloc(6uLL);
v3 = *(_DWORD *)v0;
v4 = HIWORD(*(_DWORD *)v0);
*(_DWORD *)v2 = *(_DWORD *)v0;
v5 = v0[2];
v2[2] = v5;
printf(
"A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned int)v1++,
(unsigned __int8)v3,
BYTE1(v3),
(unsigned __int8)v4,
HIBYTE(v3),
(unsigned __int8)v5,
HIBYTE(v5));
v0 += 3;
}
while ( array_size > v1 );
}
}

original_code:
void print_mac_address_array() {
 int i=0;
 void * ptr = indexer;
 while (i < array_size) {
  struct mac_address *a = malloc(sizeof(struct mac_address));
  memcpy(a,ptr,sizeof(struct mac_address));
  printf("A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
         i,a->addr[0], a->addr[1], a->addr[2],
         a->addr[3], a->addr[4], a->addr[5]);
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
}

canonical_original_code:
void print_mac_address_array() {
 int i = 0;
 void *ptr = indexer;
 while (i < array_size) {
  struct mac_address *a = malloc(sizeof(struct mac_address));
  memcpy(a,ptr,sizeof(struct mac_address));
  printf("A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
         i,a->addr[0], a->addr[1], a->addr[2],
         a->addr[3], a->addr[4], a->addr[5]);
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
}

user_defined_types: struct mac_address { unsigned char addr[6]; }


decompiled_code:
void *__fastcall init_probability(int a1)
{
__int64 v1; // rbx
void *v2; // rax

array_size = a1;
indexer = malloc(6LL * a1);
if ( !indexer )
{
printf("Problem allocating vector");
exit(1);
}
v1 = a1 * a1;
v2 = malloc(96 * v1);
return memset(v2, 0, 96 * v1);
}

canonical_decompiled_code:
void *__fastcall func7(int a1)
{
__int64 v1; // rbx
void *v2; // rax

array_size = a1;
indexer = malloc(6LL * a1);
if ( !indexer )
{
printf("Problem allocating vector");
exit(1);
}
v1 = a1 * a1;
v2 = malloc(96 * v1);
return memset(v2, 0, 96 * v1);
}

original_code:
double * init_probability(int size) {
 array_size = size;
 indexer = malloc(sizeof(struct mac_address)*array_size);
 if (indexer==((void *)0)) {
  printf("Problem allocating vector");
  exit(1);
 }
  double * mat = malloc(sizeof(double)*(size*size)*12);
  memset(mat,0,sizeof(double)*(size*size)*12);
  return mat;
}

canonical_original_code:
double *init_probability(int size) {
 array_size = size;
 indexer = malloc(sizeof(struct mac_address)*array_size);
 if (indexer==((void *)0)) {
  printf("Problem allocating vector");
  exit(1);
 }
  double *mat = malloc(sizeof(double)*(size*size)*12);
  memset(mat,0,sizeof(double)*(size*size)*12);
  return mat;
}

variable_types: size int

variable_types:
mat
double *

return_type: double *


decompiled_code:
void kill_handler()
{
running = 0;
}

canonical_decompiled_code:
void func0()
{
running = 0;
}

original_code:
void kill_handler() {
 running = 0;
}

canonical_original_code:
void kill_handler() {
 running = 0;
}


decompiled_code:
double generate_random_double()
{
return (double)rand() * 4.656612873077393e-10;
}

original_code:
double generate_random_double()
{
 return rand()/((double)2147483647 +1);
}

return_type: double


decompiled_code:
__int64 __fastcall send_tx_info_frame_nl(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, __int64 a5)
{
unsigned int v8; // eax
unsigned int v9; // ebx

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
LABEL_7:
nlmsg_free(msg);
return (unsigned int)-1;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 3LL, 1LL);
nla_put(msg, 2LL, 6LL, a1);
nla_put_u32(msg, 4LL, a2);
nla_put_u32(msg, 6LL, a3);
nla_put(msg, 7LL, 10LL, a4);
v9 = nla_put_u64(msg, 8LL, a5);
if ( v9 )
{
puts("Error filling payload");
goto LABEL_7;
}
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, __int64 a5)
{
unsigned int v8; // eax
unsigned int v9; // ebx

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
LABEL_7:
nlmsg_free(msg);
return (unsigned int)-1;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 3LL, 1LL);
nla_put(msg, 2LL, 6LL, a1);
nla_put_u32(msg, 4LL, a2);
nla_put_u32(msg, 6LL, a3);
nla_put(msg, 7LL, 10LL, a4);
v9 = nla_put_u64(msg, 8LL, a5);
if ( v9 )
{
puts("Error filling payload");
goto LABEL_7;
}
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
return v9;
}

original_code:
int send_tx_info_frame_nl(struct mac_address *src,
     unsigned int flags, int signal,
     struct hwsim_tx_rate *tx_attempts,
     unsigned long cookie)
{
 msg = nlmsg_alloc();
 if (!msg) {
  printf("Error allocating new message MSG!\n");
  goto out;
 }
 genlmsg_put(msg, 0, 0, genl_family_get_id(family),
      0, 0x01, 3, 1);
 int rc;
 rc = nla_put(msg, 2,
       sizeof(struct mac_address), src);
 rc = nla_put_u32(msg, 4, flags);
 rc = nla_put_u32(msg, 6, signal);
 rc = nla_put(msg, 7,
       5 *
       sizeof(struct hwsim_tx_rate), tx_attempts);
 rc = nla_put_u64(msg, 8, cookie);
 if(rc!=0) {
  printf("Error filling payload\n");
  goto out;
 }
 nl_send_auto_complete(sock,msg);
 nlmsg_free(msg);
 return 0;
out:
 nlmsg_free(msg);
 return -1;
}

variable_types:
flags
unsigned int

variable_types: signal int

variable_types: struct hwsim_tx_rate { char idx; unsigned char count; } *

variable_types:
cookie
unsigned long

variable_types: rc int


decompiled_code:
__int64 __fastcall send_cloned_frame_msg(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
unsigned int v8; // eax
unsigned int v9; // ebx

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
LABEL_7:
nlmsg_free(msg);
return (unsigned int)-1;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 2LL, 1LL);
nla_put(msg, 1LL, 6LL, a1);
nla_put(msg, 3LL, a3, a2);
nla_put_u32(msg, 5LL, a4);
v9 = nla_put_u32(msg, 6LL, a5);
if ( v9 )
{
puts("Error filling payload");
goto LABEL_7;
}
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
unsigned int v8; // eax
unsigned int v9; // ebx

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
LABEL_7:
nlmsg_free(msg);
return (unsigned int)-1;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 2LL, 1LL);
nla_put(msg, 1LL, 6LL, a1);
nla_put(msg, 3LL, a3, a2);
nla_put_u32(msg, 5LL, a4);
v9 = nla_put_u32(msg, 6LL, a5);
if ( v9 )
{
puts("Error filling payload");
goto LABEL_7;
}
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
return v9;
}

original_code:
int send_cloned_frame_msg(struct mac_address *dst,
     char *data, int data_len, int rate_idx, int signal)
{
 msg = nlmsg_alloc();
 if (!msg) {
  printf("Error allocating new message MSG!\n");
  goto out;
 }
 genlmsg_put(msg, 0, 0, genl_family_get_id(family),
      0, 0x01, 2, 1);
 int rc;
 rc = nla_put(msg, 1,
       sizeof(struct mac_address), dst);
 rc = nla_put(msg, 3, data_len, data);
 rc = nla_put_u32(msg, 5, rate_idx);
 rc = nla_put_u32(msg, 6, signal);
 if(rc!=0) {
  printf("Error filling payload\n");
  goto out;
 }
 nl_send_auto_complete(sock,msg);
 nlmsg_free(msg);
 return 0;
out:
 nlmsg_free(msg);
 return -1;
}

variable_types:
data
char *


original_code:
int get_signal_by_rate(int rate_idx)
{
 const int rate2signal [] =
  { -80,-77,-74,-71,-69,-66,-64,-62,-59,-56,-53,-50 };
 if (rate_idx >= 0 || rate_idx < 12)
  return rate2signal[rate_idx];
 return 0;
}

canonical_original_code:
int get_signal_by_rate(int rate_idx)
{
 int rate2signal[] = { -80,-77,-74,-71,-69,-66,-64,-62,-59,-56,-53,-50 };
 if (rate_idx >= 0 || rate_idx < 12)
  return rate2signal[rate_idx];
 return 0;
}


decompiled_code:
__int64 __fastcall send_frame_msg_apply_prob_and_rate(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
double v8; // r12
__int64 result; // rax
unsigned int v10; // eax

v8 = find_prob_by_addrs_and_rate(prob_matrix, a1, a2, a5);
if ( v8 <= generate_random_double() )
{
puts("sent");
v10 = get_signal_by_rate(a5);
send_cloned_frame_msg(a2, a3, a4, a5, v10);
++sent;
result = 1LL;
}
else
{
puts("dropped");
++dropped;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
double v8; // r12
__int64 result; // rax
unsigned int v10; // eax

v8 = find_prob_by_addrs_and_rate(prob_matrix, a1, a2, a5);
if ( v8 <= func1() )
{
puts("sent");
v10 = func4(a5);
func3(a2, a3, a4, a5, v10);
++sent;
result = 1LL;
}
else
{
puts("dropped");
++dropped;
result = 0LL;
}
return result;
}

original_code:
int send_frame_msg_apply_prob_and_rate(struct mac_address *src,
           struct mac_address *dst,
           char *data, int data_len, int rate_idx)
{
 double prob_per_link = find_prob_by_addrs_and_rate(prob_matrix,
          src,dst, rate_idx);
 double random_double = generate_random_double();
 if (random_double < prob_per_link) {
  printf("dropped\n");
  dropped++;
  return 0;
 } else {
  printf("sent\n");
  int signal = get_signal_by_rate(rate_idx);
  send_cloned_frame_msg(dst,data,data_len,rate_idx,signal);
  sent++;
  return 1;
 }
}

variable_types:
dst
struct mac_address *


original_code:
int jam_mac(struct jammer_cfg *jcfg, struct mac_address *src)
{
 int jam = 0, i;
 for (i = 0; i < jcfg->nmacs; i++) {
  if (!memcmp(&jcfg->macs[i], src, sizeof(struct mac_address))) {
   jam = 1;
   break;
  }
 }
 return jam;
}

canonical_original_code:
int jam_mac(struct jammer_cfg *jcfg, struct mac_address *src)
{
 int jam = 0; int i;
 for (i = 0; i < jcfg->nmacs; i++) {
  if (!memcmp(&jcfg->macs[i], src, sizeof(struct mac_address))) {
   jam = 1;
   break;
  }
 }
 return jam;
}

variable_types: struct jammer_cfg { int jam_all; struct mac_address { unsigned char addr[6]; } *macs; int nmacs; } *

variable_types:
src
struct mac_address *

variable_types: jam int

user_defined_types: struct jammer_cfg { int jam_all; struct mac_address *macs; int nmacs; }


decompiled_code:
int __fastcall send_frames_to_radios_with_retries(const void *a1, __int64 a2, unsigned int a3, unsigned int a4, char *a5, __int64 a6)
{
char v7; // al
int v8; // ebp
const void *v9; // rbx
int v10; // er15
unsigned int v11; // eax
int v13; // [rsp+Ch] [rbp-7Ch]
__int64 v15; // [rsp+20h] [rbp-68h]
char *v16; // [rsp+28h] [rbp-60h]
int v17; // [rsp+30h] [rbp-58h]
char v20[66]; // [rsp+46h] [rbp-42h] BYREF

if ( jam_cfg )
return puts("medium busy!");
v13 = jam_mac((__int64)&jam_cfg, a1);
if ( v13 )
return puts("medium busy!");
set_all_rates_invalid(v20);
v7 = *a5;
if ( *a5 == -1 )
return send_tx_info_frame_nl((__int64)a1, a4, 0, (__int64)v20, a6);
v16 = v20;
v15 = 0LL;
do
{
v17 = v15;
*v16 = v7;
if ( a5[2 * v15 + 1] )
{
v10 = 1;
do
{
v8 = 1;
if ( size > 0 )
{
while ( 1 )
{
v9 = (const void *)get_mac_address((unsigned int)(v8 - 1));
if ( memcmp(a1, v9, 6uLL)
&& (unsigned int)send_frame_msg_apply_prob_and_rate((__int64)a1, (__int64)v9, a2, a3, *v16)
&& !memcmp(v9, (const void *)(a2 + 4), 6uLL) )
{
if ( size <= v8 )
{
v16[1] = v10;
v13 = 1;
goto LABEL_24;
}
v13 = 1;
}
else if ( size <= v8 )
{
break;
}
++v8;
}
}
v16[1] = v10++;
}
while ( (unsigned __int8)a5[2 * v15 + 1] >= v10 && v13 != 1 );
}
if ( v15 == 4 )
break;
v7 = a5[2 * v15++ + 2];
v16 += 2;
if ( v13 == 1 )
break;
}
while ( v7 != -1 );
LABEL_24:
if ( !v13 )
return send_tx_info_frame_nl((__int64)a1, a4, 0, (__int64)v20, a6);
++acked;
v11 = get_signal_by_rate(v20[2 * v17]);
return send_tx_info_frame_nl((__int64)a1, a4 | 4, v11, (__int64)v20, a6);
}

canonical_decompiled_code:
int __fastcall func7(const void *a1, __int64 a2, unsigned int a3, unsigned int a4, char *a5, __int64 a6)
{
char v7; // al
int v8; // ebp
const void *v9; // rbx
int v10; // er15
unsigned int v11; // eax
int v13; // [rsp+Ch] [rbp-7Ch]
__int64 v15; // [rsp+20h] [rbp-68h]
char *v16; // [rsp+28h] [rbp-60h]
int v17; // [rsp+30h] [rbp-58h]
char v20[66]; // [rsp+46h] [rbp-42h] BYREF

if ( jam_cfg )
return puts("medium busy!");
v13 = func6((__int64)&jam_cfg, a1);
if ( v13 )
return puts("medium busy!");
set_all_rates_invalid(v20);
v7 = *a5;
if ( *a5 == -1 )
return func2((__int64)a1, a4, 0, (__int64)v20, a6);
v16 = v20;
v15 = 0LL;
do
{
v17 = v15;
*v16 = v7;
if ( a5[2 * v15 + 1] )
{
v10 = 1;
do
{
v8 = 1;
if ( size > 0 )
{
while ( 1 )
{
v9 = (const void *)get_mac_address((unsigned int)(v8 - 1));
if ( memcmp(a1, v9, 6uLL)
&& (unsigned int)func5((__int64)a1, (__int64)v9, a2, a3, *v16)
&& !memcmp(v9, (const void *)(a2 + 4), 6uLL) )
{
if ( size <= v8 )
{
v16[1] = v10;
v13 = 1;
goto LABEL_24;
}
v13 = 1;
}
else if ( size <= v8 )
{
break;
}
++v8;
}
}
v16[1] = v10++;
}
while ( (unsigned __int8)a5[2 * v15 + 1] >= v10 && v13 != 1 );
}
if ( v15 == 4 )
break;
v7 = a5[2 * v15++ + 2];
v16 += 2;
if ( v13 == 1 )
break;
}
while ( v7 != -1 );
LABEL_24:
if ( !v13 )
return func2((__int64)a1, a4, 0, (__int64)v20, a6);
++acked;
v11 = func4(v20[2 * v17]);
return func2((__int64)a1, a4 | 4, v11, (__int64)v20, a6);
}

original_code:
void send_frames_to_radios_with_retries(struct mac_address *src, char*data,
     int data_len, unsigned int flags,
     struct hwsim_tx_rate *tx_rates,
     unsigned long cookie)
{
 struct mac_address *dst;
 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
 struct hwsim_tx_rate tx_attempts[5];
 int round = 0, tx_ok = 0, counter, i;
 if (jam_cfg.jam_all || jam_mac(&jam_cfg, src)) {
  printf("medium busy!\n");
  return;
 }
 set_all_rates_invalid(tx_attempts);
 while (round < 5 &&
        tx_rates[round].idx != -1 && tx_ok!=1) {
  counter = 1;
  tx_attempts[round].idx = tx_rates[round].idx;
  while(counter <= tx_rates[round].count && tx_ok !=1 ) {
   for (i=0;i<size;i++) {
    dst = get_mac_address(i);
    if(memcmp(src,dst,sizeof(struct mac_address))
       == 0 ){
     continue;
    }
    if(send_frame_msg_apply_prob_and_rate(
     src, dst, data, data_len,
     tx_attempts[round].idx) &&
     memcmp(dst, hdr->addr1,
     sizeof(struct mac_address))==0) {
      tx_ok = 1;
    }
   }
   tx_attempts[round].count = counter;
   counter++;
  }
  round++;
 }
 if (tx_ok) {
  acked++;
  int signal = get_signal_by_rate(tx_attempts[round-1].idx);
  flags |= (1 << 2);
  send_tx_info_frame_nl(src, flags, signal, tx_attempts, cookie);
 } else {
  send_tx_info_frame_nl(src, flags, 0, tx_attempts, cookie);
 }
}

canonical_original_code:
void send_frames_to_radios_with_retries(struct mac_address *src, char *data, int data_len, unsigned int flags, struct hwsim_tx_rate *tx_rates, unsigned long cookie)
{
 struct mac_address *dst;
 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
 struct hwsim_tx_rate tx_attempts[5];
 int round = 0; int tx_ok = 0; int counter; int i;
 if (jam_cfg.jam_all || jam_mac(&jam_cfg, src)) {
  printf("medium busy!\n");
  return;
 }
 set_all_rates_invalid(tx_attempts);
 while (round < 5 &&
        tx_rates[round].idx != -1 && tx_ok!=1) {
  counter = 1;
  tx_attempts[round].idx = tx_rates[round].idx;
  while(counter <= tx_rates[round].count && tx_ok !=1 ) {
   for (i=0;i<size;i++) {
    dst = get_mac_address(i);
    if(memcmp(src,dst,sizeof(struct mac_address))
       == 0 ){
     continue;
    }
    if(send_frame_msg_apply_prob_and_rate(
     src, dst, data, data_len,
     tx_attempts[round].idx) &&
     memcmp(dst, hdr->addr1,
     sizeof(struct mac_address))==0) {
      tx_ok = 1;
    }
   }
   tx_attempts[round].count = counter;
   counter++;
  }
  round++;
 }
 if (tx_ok) {
  acked++;
  int signal = get_signal_by_rate(tx_attempts[round-1].idx);
  flags |= (1 << 2);
  send_tx_info_frame_nl(src, flags, signal, tx_attempts, cookie);
 } else {
  send_tx_info_frame_nl(src, flags, 0, tx_attempts, cookie);
 }
}

variable_types: struct ieee80211_hdr { unsigned char frame_control[2]; unsigned char duration_id[2]; unsigned char addr1[6]; unsigned char addr2[6]; unsigned char addr3[6]; unsigned char seq_ctrl[2]; unsigned char addr4[6]; } *

variable_types: struct hwsim_tx_rate { char idx; unsigned char count; } [5]

variable_types: round int

variable_types: counter int

variable_types: i int


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

msg = (void *)nlmsg_alloc();
if ( msg )
{
v0 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v0, 0LL, 1LL, 1LL, 1LL);
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
result = 0LL;
}
else
{
puts("Error allocating new message MSG!");
result = 0xFFFFFFFFLL;
}
return result;
}

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

msg = (void *)nlmsg_alloc();
if ( msg )
{
v0 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v0, 0LL, 1LL, 1LL, 1LL);
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
result = 0LL;
}
else
{
puts("Error allocating new message MSG!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int send_register_msg()
{
 msg = nlmsg_alloc();
 if (!msg) {
  printf("Error allocating new message MSG!\n");
  return -1;
 }
 genlmsg_put(msg, 0, 0, genl_family_get_id(family),
      0, 0x01, 1, 1);
 nl_send_auto_complete(sock,msg);
 nlmsg_free(msg);
 return 0;
}


decompiled_code:
__int64 init_netlink()
{
void *v0; // rax
void *v1; // rdi

v0 = (void *)nl_cb_alloc(3LL);
cb = v0;
if ( !v0 )
{
puts("Error allocating netlink callbacks");
exit(1);
}
v1 = (void *)nl_socket_alloc_cb(v0);
sock = v1;
if ( !v1 )
{
puts("Error allocationg netlink socket");
exit(1);
}
genl_connect(v1);
genl_ctrl_alloc_cache(sock, &cache);
family = (void *)genl_ctrl_search_by_name(cache, "MAC80211_HWSIM");
if ( !family )
{
puts("Family MAC80211_HWSIM not registered");
exit(1);
}
return nl_cb_set(cb, 5LL, 3LL, process_messages_cb, 0LL);
}

canonical_decompiled_code:
__int64 func10()
{
void *v0; // rax
void *v1; // rdi

v0 = (void *)nl_cb_alloc(3LL);
cb = v0;
if ( !v0 )
{
puts("Error allocating netlink callbacks");
exit(1);
}
v1 = (void *)nl_socket_alloc_cb(v0);
sock = v1;
if ( !v1 )
{
puts("Error allocationg netlink socket");
exit(1);
}
genl_connect(v1);
genl_ctrl_alloc_cache(sock, &cache);
family = (void *)genl_ctrl_search_by_name(cache, "MAC80211_HWSIM");
if ( !family )
{
puts("Family MAC80211_HWSIM not registered");
exit(1);
}
return nl_cb_set(cb, 5LL, 3LL, process_messages_cb, 0LL);
}

original_code:
void init_netlink()
{
 cb = nl_cb_alloc(NL_CB_CUSTOM);
 if (!cb) {
  printf("Error allocating netlink callbacks\n");
  exit(1);
 }
 sock = nl_socket_alloc_cb(cb);
 if (!sock) {
  printf("Error allocationg netlink socket\n");
  exit(1);
 }
 genl_connect(sock);
 genl_ctrl_alloc_cache(sock, &cache);
 family = genl_ctrl_search_by_name(cache, "MAC80211_HWSIM");
 if (!family) {
  printf("Family MAC80211_HWSIM not registered\n");
  exit(1);
 }
 nl_cb_set(cb, NL_CB_MSG_IN, NL_CB_CUSTOM, process_messages_cb, ((void *)0));
}


decompiled_code:
void __fastcall __noreturn print_help(int status)
{
printf("wmediumd v%s - a wireless medium simulator\n", "0.1");
puts("wmediumd [-h] [-V] [-c FILE] [-o FILE]\n");
puts(" -h print this help and exit");
puts(" -V print version and exit\n");
puts(" -c FILE set intput config file");
puts(" -o FILE set output config file\n");
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func11(int status)
{
printf("wmediumd v%s - a wireless medium simulator\n", "0.1");
puts("wmediumd [-h] [-V] [-c FILE] [-o FILE]\n");
puts(" -h print this help and exit");
puts(" -V print version and exit\n");
puts(" -c FILE set intput config file");
puts(" -o FILE set output config file\n");
exit(status);
}

original_code:
void print_help(int exval)
{
 printf("wmediumd v%s - a wireless medium simulator\n", "0.1");
 printf("wmediumd [-h] [-V] [-c FILE] [-o FILE]\n\n");
 printf("  -h              print this help and exit\n");
 printf("  -V              print version and exit\n\n");
 printf("  -c FILE         set intput config file\n");
 printf("  -o FILE         set output config file\n\n");
 exit(exval);
}

variable_types: exval int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

setvbuf(stdout, 0LL, 1, 0x2000uLL);
if ( argc == 1 )
{
fwrite("This program needs arguments....\n\n", 1uLL, 0x22uLL, stderr);
print_help(1);
}
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hVc:o:");
if ( v3 == -1 )
break;
if ( v3 == 99 )
{
printf("Input configuration file: %s\n", optarg);
load_config(optarg);
}
else if ( v3 > 99 )
{
if ( v3 == 104 )
print_help(0);
if ( v3 == 111 )
{
printf("Output configuration file: %s\n", optarg);
puts("How many interfaces are active?");
_isoc99_scanf("%d", v5);
if ( v5[0] <= 1 )
{
puts("active interfaces must be at least 2");
exit(1);
}
write_config(optarg, 0.0);
}
}
else
{
if ( v3 == 86 )
{
printf("wmediumd v%s - a wireless medium simulator for mac80211_hwsim\n", "0.1");
exit(0);
}
if ( v3 <= 86 )
{
if ( v3 == 58 )
{
printf("wmediumd: Error - Option `%c' needs a value\n\n", (unsigned int)optopt);
print_help(1);
}
if ( v3 == 63 )
{
printf("wmediumd: Error - No such option: `%c'\n\n", (unsigned int)optopt);
print_help(1);
}
}
}
}
if ( optind < argc )
print_help(1);
print_prob_matrix(prob_matrix);
running = 1;
signal(10, (__sighandler_t)kill_handler);
init_netlink();
if ( !(unsigned int)send_register_msg() )
puts("REGISTER SENT!");
while ( running )
nl_recvmsgs_default(sock);
free(sock);
free(msg);
free(cb);
free(cache);
free(family);
free(prob_matrix);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

setvbuf(stdout, 0LL, 1, 0x2000uLL);
if ( argc == 1 )
{
fwrite("This program needs arguments....\n\n", 1uLL, 0x22uLL, stderr);
func11(1);
}
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hVc:o:");
if ( v3 == -1 )
break;
if ( v3 == 99 )
{
printf("Input configuration file: %s\n", optarg);
load_config(optarg);
}
else if ( v3 > 99 )
{
if ( v3 == 104 )
func11(0);
if ( v3 == 111 )
{
printf("Output configuration file: %s\n", optarg);
puts("How many interfaces are active?");
_isoc99_scanf("%d", v5);
if ( v5[0] <= 1 )
{
puts("active interfaces must be at least 2");
exit(1);
}
write_config(optarg, 0.0);
}
}
else
{
if ( v3 == 86 )
{
printf("wmediumd v%s - a wireless medium simulator for mac80211_hwsim\n", "0.1");
exit(0);
}
if ( v3 <= 86 )
{
if ( v3 == 58 )
{
printf("wmediumd: Error - Option `%c' needs a value\n\n", (unsigned int)optopt);
func11(1);
}
if ( v3 == 63 )
{
printf("wmediumd: Error - No such option: `%c'\n\n", (unsigned int)optopt);
func11(1);
}
}
}
}
if ( optind < argc )
func11(1);
print_prob_matrix(prob_matrix);
running = 1;
signal(10, (__sighandler_t)kill_handler);
func10();
if ( !(unsigned int)func9() )
puts("REGISTER SENT!");
while ( running )
nl_recvmsgs_default(sock);
free(sock);
free(msg);
free(cb);
free(cache);
free(family);
free(prob_matrix);
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 int opt, ifaces;
 setvbuf (stdout, ((void *)0), 1, 8192);
 if(argc == 1) {
  fprintf(stderr, "This program needs arguments....\n\n");
  print_help(1);
 }
 while((opt = getopt(argc, argv, "hVc:o:")) != -1) {
  switch(opt) {
  case 'h':
   print_help(0);
       break;
  case 'V':
   printf("wmediumd v%s - a wireless medium simulator "
          "for mac80211_hwsim\n", "0.1");
   exit(0);
   break;
  case 'c':
   printf("Input configuration file: %s\n", optarg);
   load_config(optarg);
   break;
  case 'o':
   printf("Output configuration file: %s\n", optarg);
   printf("How many interfaces are active?\n");
   scanf("%d",&ifaces);
   if (ifaces < 2) {
    printf("active interfaces must be at least 2\n");
    exit(1);
   }
    write_config(optarg, ifaces, 0.0);
   break;
  case ':':
   printf("wmediumd: Error - Option `%c' "
          "needs a value\n\n", optopt);
   print_help(1);
   break;
  case '?':
   printf("wmediumd: Error - No such option:"
          " `%c'\n\n", optopt);
   print_help(1);
   break;
  }
 }
 if (optind < argc)
  print_help(1);
 print_prob_matrix(prob_matrix);
 running = 1;
 signal(10, kill_handler);
 init_netlink();
 if (send_register_msg()==0)
  printf("REGISTER SENT!\n");
 while(running) {
  nl_recvmsgs_default(sock);
 }
 free(sock);
 free(msg);
 free(cb);
 free(cache);
 free(family);
 free(prob_matrix);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int opt; int ifaces;
 setvbuf (stdout, ((void *)0), 1, 8192);
 if(argc == 1) {
  fprintf(stderr, "This program needs arguments....\n\n");
  print_help(1);
 }
 while((opt = getopt(argc, argv, "hVc:o:")) != -1) {
  switch(opt) {
  case 'h':
   print_help(0);
       break;
  case 'V':
   printf("wmediumd v%s - a wireless medium simulator "
          "for mac80211_hwsim\n", "0.1");
   exit(0);
   break;
  case 'c':
   printf("Input configuration file: %s\n", optarg);
   load_config(optarg);
   break;
  case 'o':
   printf("Output configuration file: %s\n", optarg);
   printf("How many interfaces are active?\n");
   scanf("%d",&ifaces);
   if (ifaces < 2) {
    printf("active interfaces must be at least 2\n");
    exit(1);
   }
    write_config(optarg, ifaces, 0.0);
   break;
  case ':':
   printf("wmediumd: Error - Option `%c' "
          "needs a value\n\n", optopt);
   print_help(1);
   break;
  case '?':
   printf("wmediumd: Error - No such option:"
          " `%c'\n\n", optopt);
   print_help(1);
   break;
  }
 }
 if (optind < argc)
  print_help(1);
 print_prob_matrix(prob_matrix);
 running = 1;
 signal(10, kill_handler);
 init_netlink();
 if (send_register_msg()==0)
  printf("REGISTER SENT!\n");
 while(running) {
  nl_recvmsgs_default(sock);
 }
 free(sock);
 free(msg);
 free(cb);
 free(cache);
 free(family);
 free(prob_matrix);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

variable_types: ifaces int


decompiled_code:
unsigned __int64 __fastcall string_to_mac_address(__int64 a1)
{
int v2; // [rsp+0h] [rbp-28h] BYREF
int v3; // [rsp+4h] [rbp-24h] BYREF
int v4; // [rsp+8h] [rbp-20h] BYREF
int v5; // [rsp+Ch] [rbp-1Ch] BYREF
int v6; // [rsp+10h] [rbp-18h] BYREF
int v7; // [rsp+14h] [rbp-14h] BYREF
unsigned int v8; // [rsp+1Ah] [rbp-Eh]
unsigned __int16 v9; // [rsp+1Eh] [rbp-Ah]

_isoc99_sscanf(a1, "%x:%x:%x:%x:%x:%x", &v2, &v3, &v4, &v5, &v6, &v7);
LOBYTE(v8) = v2;
BYTE1(v8) = v3;
BYTE2(v8) = v4;
HIBYTE(v8) = v5;
LOBYTE(v9) = v6;
HIBYTE(v9) = v7;
return ((unsigned __int64)v9 << 32) | v8;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1)
{
int v2; // [rsp+0h] [rbp-28h] BYREF
int v3; // [rsp+4h] [rbp-24h] BYREF
int v4; // [rsp+8h] [rbp-20h] BYREF
int v5; // [rsp+Ch] [rbp-1Ch] BYREF
int v6; // [rsp+10h] [rbp-18h] BYREF
int v7; // [rsp+14h] [rbp-14h] BYREF
unsigned int v8; // [rsp+1Ah] [rbp-Eh]
unsigned __int16 v9; // [rsp+1Eh] [rbp-Ah]

_isoc99_sscanf(a1, "%x:%x:%x:%x:%x:%x", &v2, &v3, &v4, &v5, &v6, &v7);
LOBYTE(v8) = v2;
BYTE1(v8) = v3;
BYTE2(v8) = v4;
HIBYTE(v8) = v5;
LOBYTE(v9) = v6;
HIBYTE(v9) = v7;
return ((unsigned __int64)v9 << 32) | v8;
}

original_code:
struct mac_address string_to_mac_address(const char* str)
{
 struct mac_address mac;
 int a[6];
 sscanf(str, "%x:%x:%x:%x:%x:%x",
        &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
 mac.addr[0] = a[0];
 mac.addr[1] = a[1];
 mac.addr[2] = a[2];
 mac.addr[3] = a[3];
 mac.addr[4] = a[4];
 mac.addr[5] = a[5];
 return mac;
}

canonical_original_code:
struct mac_address string_to_mac_address(char *str)
{
 struct mac_address mac;
 int a[6];
 sscanf(str, "%x:%x:%x:%x:%x:%x",
        &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
 mac.addr[0] = a[0];
 mac.addr[1] = a[1];
 mac.addr[2] = a[2];
 mac.addr[3] = a[3];
 mac.addr[4] = a[4];
 mac.addr[5] = a[5];
 return mac;
}

variable_types:
str
char *

variable_types: struct mac_address { unsigned char addr[6]; }

variable_types:
a
int [6]


decompiled_code:
void *__fastcall str_replace(char *src, char *needle, char *a3)
{
char *v3; // r12
size_t v5; // rbp
__int64 i; // rbx
char *v7; // rax
void *v8; // rax
char *v9; // r13
signed __int64 v10; // rbp
char *v11; // r13
char *v12; // rax
char *v13; // rbx
size_t v15; // [rsp+8h] [rbp-50h]
size_t v16; // [rsp+10h] [rbp-48h]
void *v17; // [rsp+18h] [rbp-40h]

v3 = src;
v5 = strlen(src);
v15 = strlen(needle);
v16 = strlen(a3);
for ( i = 0LL; ; ++i )
{
v7 = strstr(src, needle);
if ( !v7 )
break;
src = &v7[v15];
}
v8 = malloc(v5 + (v16 - v15) * i + 1);
v17 = v8;
if ( v8 )
{
v9 = (char *)v8;
while ( 1 )
{
v12 = strstr(v3, needle);
v13 = v12;
if ( !v12 )
break;
v10 = v12 - v3;
memcpy(v9, v3, v12 - v3);
v11 = &v9[v10];
strcpy(v11, a3);
v9 = &v11[v16];
v3 = &v13[v15];
}
strcpy(v9, v3);
}
return v17;
}

canonical_decompiled_code:
void *__fastcall func0(char *src, char *needle, char *a3)
{
char *v3; // r12
size_t v5; // rbp
__int64 i; // rbx
char *v7; // rax
void *v8; // rax
char *v9; // r13
signed __int64 v10; // rbp
char *v11; // r13
char *v12; // rax
char *v13; // rbx
size_t v15; // [rsp+8h] [rbp-50h]
size_t v16; // [rsp+10h] [rbp-48h]
void *v17; // [rsp+18h] [rbp-40h]

v3 = src;
v5 = strlen(src);
v15 = strlen(needle);
v16 = strlen(a3);
for ( i = 0LL; ; ++i )
{
v7 = strstr(src, needle);
if ( !v7 )
break;
src = &v7[v15];
}
v8 = malloc(v5 + (v16 - v15) * i + 1);
v17 = v8;
if ( v8 )
{
v9 = (char *)v8;
while ( 1 )
{
v12 = strstr(v3, needle);
v13 = v12;
if ( !v12 )
break;
v10 = v12 - v3;
memcpy(v9, v3, v12 - v3);
v11 = &v9[v10];
strcpy(v11, a3);
v9 = &v11[v16];
v3 = &v13[v15];
}
strcpy(v9, v3);
}
return v17;
}

original_code:
char *str_replace(const char *str, const char *old, const char *new)
{
 char *ret, *r;
 const char *p, *q;
 size_t len_str = strlen(str);
 size_t len_old = strlen(old);
 size_t len_new = strlen(new);
 size_t count;
 for(count = 0, p = str; (p = strstr(p, old)); p += len_old)
  count++;
 ret = malloc(count * (len_new - len_old) + len_str + 1);
 if(!ret)
  return ((void *)0);
 for(r = ret, p = str; (q = strstr(p, old)); p = q + len_old) {
  count = q - p;
  memcpy(r, p, count);
  r += count;
  strcpy(r, new);
  r += len_new;
 }
 strcpy(r, p);
 return ret;
}

canonical_original_code:
char *str_replace(char *str, char *old, char *new)
{
 char *ret; char *r;
 char *p; char *q;
 unsigned long len_str = strlen(str);
 unsigned long len_old = strlen(old);
 unsigned long len_new = strlen(new);
 unsigned long count;
 for(count = 0, p = str; (p = strstr(p, old)); p += len_old)
  count++;
 ret = malloc(count * (len_new - len_old) + len_str + 1);
 if(!ret)
  return ((void *)0);
 for(r = ret, p = str; (q = strstr(p, old)); p = q + len_old) {
  count = q - p;
  memcpy(r, p, count);
  r += count;
  strcpy(r, new);
  r += len_new;
 }
 strcpy(r, p);
 return ret;
}

variable_types:
old
char *

variable_types:
new
char *

variable_types:
ret
char *

variable_types:
r
char *

variable_types:
p
char *

variable_types:
q
char *

variable_types:
count
unsigned long

return_type: char *


usage

repo: jlusticky/Bachelor-thesis

decompiled_code:
void __noreturn usage()
{
fprintf(
stderr,
"%s\n%s\n%s\n%s\n%s\n%s\n",
"Usage: sntp [-p] [-i n] [-b | -c | -s] address",
"\t-p\tuse privileged NTP port",
"\t-i n\tcontinue sending NTP messsage every i seconds",
"\t-b\tbroadcast NTP message to address",
"\t-c\tclient NTP message to address (default)",
"\t-s\tserver NTP message to address");
exit(1);
}

canonical_decompiled_code:
void __noreturn func9()
{
fprintf(
stderr,
"%s\n%s\n%s\n%s\n%s\n%s\n",
"Usage: sntp [-p] [-i n] [-b | -c | -s] address",
"\t-p\tuse privileged NTP port",
"\t-i n\tcontinue sending NTP messsage every i seconds",
"\t-b\tbroadcast NTP message to address",
"\t-c\tclient NTP message to address (default)",
"\t-s\tserver NTP message to address");
exit(1);
}

original_code:
void
usage(void)
{
 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
  "Usage: sntp [-p] [-i n] [-b | -c | -s] address",
  "\t-p\tuse privileged NTP port",
  "\t-i n\tcontinue sending NTP messsage every i seconds",
  "\t-b\tbroadcast NTP message to address",
  "\t-c\tclient NTP message to address (default)",
  "\t-s\tserver NTP message to address");
 exit(1);
}

canonical_original_code:
void usage()
{
 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
  "Usage: sntp [-p] [-i n] [-b | -c | -s] address",
  "\t-p\tuse privileged NTP port",
  "\t-i n\tcontinue sending NTP messsage every i seconds",
  "\t-b\tbroadcast NTP message to address",
  "\t-c\tclient NTP message to address (default)",
  "\t-s\tserver NTP message to address");
 exit(1);
}