decompiled_code:
unsigned __int64 move_cursor_to_window()
{
__int64 v0; // rbx
_QWORD *v1; // rdx
__int64 v2; // rsi
unsigned __int64 v3; // rcx
unsigned __int64 result; // rax
_QWORD *v5; // rdi
unsigned __int64 v6; // rsi

v0 = curwin;
v1 = (_QWORD *)*curwin;
v2 = *(_QWORD *)(curwin + 16LL);
v3 = *(_QWORD *)(v2 + 32);
result = *(_QWORD *)(*(_QWORD *)*curwin + 32LL);
if ( result < v3 )
{
*v1 = v2;
LABEL_6:
info_update();
return xvMoveToColumn(*(_DWORD **)v0, *(_DWORD *)(v0 + 92));
}
v5 = *(_QWORD **)(curwin + 24LL);
v6 = v5[4];
if ( result >= v6 && v3 < v6 )
{
*v1 = *v5;
goto LABEL_6;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func17()
{
__int64 v0; // rbx
_QWORD *v1; // rdx
__int64 v2; // rsi
unsigned __int64 v3; // rcx
unsigned __int64 result; // rax
_QWORD *v5; // rdi
unsigned __int64 v6; // rsi

v0 = curwin;
v1 = (_QWORD *)*curwin;
v2 = *(_QWORD *)(curwin + 16LL);
v3 = *(_QWORD *)(v2 + 32);
result = *(_QWORD *)(*(_QWORD *)*curwin + 32LL);
if ( result < v3 )
{
*v1 = v2;
LABEL_6:
info_update();
return func13(*(_DWORD **)v0, *(_DWORD *)(v0 + 92));
}
v5 = *(_QWORD **)(curwin + 24LL);
v6 = v5[4];
if ( result >= v6 && v3 < v6 )
{
*v1 = *v5;
goto LABEL_6;
}
return result;
}

original_code:
void
move_cursor_to_window()
{
    Xviwin *win = curwin;
    Posn *cp;
    cp = win->w_cursor;
    if (((cp->p_line)->l_number < (win->w_topline)->l_number)) {
 cp->p_line = win->w_topline;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    } else if (!((cp->p_line)->l_number < (win->w_botline)->l_number)
        && ((win->w_topline)->l_number < (win->w_botline)->l_number)) {
 cp->p_line = win->w_botline->l_prev;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    }
}

canonical_original_code:
void move_cursor_to_window()
{
    struct xviwin *win = curwin;
    struct position *cp;
    cp = win->w_cursor;
    if (((cp->p_line)->l_number < (win->w_topline)->l_number)) {
 cp->p_line = win->w_topline;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    } else if (!((cp->p_line)->l_number < (win->w_botline)->l_number)
        && ((win->w_topline)->l_number < (win->w_botline)->l_number)) {
 cp->p_line = win->w_botline->l_prev;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    }
}

variable_types: struct xviwin { struct position { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *p_line; int p_index; } *w_cursor; struct buffer { struct line *b_line0; struct line *b_file; struct line *b_lastline; struct line *b_prevline; char *b_Undotext; char *b_filename; char *b_tempfname; unsigned int b_flags; int b_nwindows; struct mark { struct position m_posn; int m_deleted; } b_mlist[26]; struct mark b_pcmark; struct changedata { unsigned int cd_nlevels; struct cmd { struct position cmd_startpos; struct position cmd_target; unsigned char cmd_flags; int cmd_two_char; int cmd_operator; int cmd_ch1; int cmd_ch2; long cmd_prenum; long cmd_opnum; int cmd_got_yp_name; int cmd_yp_name; int cmd_buffer_name; } *cd_vi_cmd; long cd_total_lines; struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } *cd_undo; struct change *cd_redo; } *b_change; } *w_buffer; struct line *w_topline; struct line *w_botline; struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct { char *s_line; unsigned char *s_colour; int s_used; unsigned int s_flags; } *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); } *w_vs; unsigned int w_nrows; unsigned int w_ncols; unsigned int w_winpos; unsigned int w_cmdline; struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; } w_statusline; int w_row; int w_virtcol; int w_curswant; int w_set_want_col; int w_c_line_size; struct xviwin *w_last; struct xviwin *w_next; struct cmd *w_cmd; } *


repo: martinwguy/xvi

decompiled_code:
__int64 show_map()
{
__int64 v0; // rdx

if ( !curmap )
return 0LL;
v0 = *(_QWORD *)(curmap + 8);
qword_BAC = 0LL;
lformat(&buf_0, "%-18.18s %-s", v0);
curmap = *(_QWORD *)curmap;
return flexgetstr(&buf_0);
}

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

if ( !curmap )
return 0LL;
v0 = *(_QWORD *)(curmap + 8);
qword_BAC = 0LL;
lformat(&buf_0, "%-18.18s %-s", v0);
curmap = *(_QWORD *)curmap;
return flexgetstr(&buf_0);
}

original_code:
static char *
show_map()
{
    static Flexbuf buf;
    if (curmap == ((void *)0)) {
 return(((void *)0));
    }
    ((&buf)->fxb_rcnt = (&buf)->fxb_wcnt = 0);
    (void) lformat(&buf, "%-18.18s %-s", curmap->m_lhs, curmap->m_rhs);
    curmap = curmap->m_next;
    return flexgetstr(&buf);
}

canonical_original_code:
static char *show_map()
{
    static struct Flexbuf buf;
    if (curmap == ((void *)0)) {
 return(((void *)0));
    }
    ((&buf)->fxb_rcnt = (&buf)->fxb_wcnt = 0);
    (void) lformat(&buf, "%-18.18s %-s", curmap->m_lhs, curmap->m_rhs);
    curmap = curmap->m_next;
    return flexgetstr(&buf);
}

variable_types: struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; }

return_type: char *


stuff

decompiled_code:
__int64 stuff(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return vformat(off_B98, a1, va);
}

canonical_decompiled_code:
__int64 func8(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return vformat(off_B98, a1, va);
}

original_code:
void
stuff
    (char *format, ...)
{
    va_list argp;
    __builtin_va_start(argp,format);
    (void) vformat(npos.mp_dest, format, argp);
    __builtin_va_end(argp);
}

canonical_original_code:
void stuff(char *format, ...)
{
    __builtin_va_list argp;
    __builtin_va_start(argp,format);
    (void) vformat(npos.mp_dest, format, argp);
    __builtin_va_end(argp);
}

variable_types:
format
char *

return_type: void


unstuff

decompiled_code:
void *unstuff()
{
void *result; // rax

*(_QWORD *)((char *)off_B98 + 12) = 0LL;
result = off_B90;
*(_QWORD *)((char *)off_B90 + 12) = 0LL;
return result;
}

canonical_decompiled_code:
void *func10()
{
void *result; // rax

*(_QWORD *)((char *)off_B98 + 12) = 0LL;
result = off_B90;
*(_QWORD *)((char *)off_B90 + 12) = 0LL;
return result;
}

original_code:
void
unstuff()
{
 ((npos.mp_dest)->fxb_rcnt = (npos.mp_dest)->fxb_wcnt = 0);
 ((npos.mp_src)->fxb_rcnt = (npos.mp_src)->fxb_wcnt = 0);
}


decompiled_code:
__int64 map_getc()
{
void *v0; // rdi
__int64 v1; // rsi

v0 = off_B98;
if ( *((_DWORD *)off_B98 + 4) > *((_DWORD *)off_B98 + 3) )
return flexpopch(v0);
while ( !kbdintr )
{
v0 = off_B90;
if ( *((_DWORD *)off_B90 + 3) < *((_DWORD *)off_B90 + 4) )
{
if ( literal_next )
return flexpopch(v0);
v1 = cmd_map;
if ( State )
{
v1 = 0LL;
if ( (unsigned int)(State - 2) < 3 )
v1 = ins_map;
}
mapthrough((__int64)&npos, v1);
v0 = off_B98;
if ( *((_DWORD *)off_B98 + 3) < *((_DWORD *)off_B98 + 4) )
return flexpopch(v0);
}
else
{
if ( *((_DWORD *)off_B70 + 3) >= *((_DWORD *)off_B70 + 4) )
return 0xFFFFFFFFLL;
mapthrough((__int64)&kpos, key_map);
v0 = off_B98;
if ( *((_DWORD *)off_B98 + 3) < *((_DWORD *)off_B98 + 4) )
return flexpopch(v0);
}
}
if ( !literal_next )
{
qword_C0C = 0LL;
imessage = 1;
}
return (unsigned int)kbdintr_ch;
}

canonical_decompiled_code:
__int64 func12()
{
void *v0; // rdi
__int64 v1; // rsi

v0 = off_B98;
if ( *((_DWORD *)off_B98 + 4) > *((_DWORD *)off_B98 + 3) )
return flexpopch(v0);
while ( !kbdintr )
{
v0 = off_B90;
if ( *((_DWORD *)off_B90 + 3) < *((_DWORD *)off_B90 + 4) )
{
if ( literal_next )
return flexpopch(v0);
v1 = cmd_map;
if ( State )
{
v1 = 0LL;
if ( (unsigned int)(State - 2) < 3 )
v1 = ins_map;
}
func3((__int64)&npos, v1);
v0 = off_B98;
if ( *((_DWORD *)off_B98 + 3) < *((_DWORD *)off_B98 + 4) )
return flexpopch(v0);
}
else
{
if ( *((_DWORD *)off_B70 + 3) >= *((_DWORD *)off_B70 + 4) )
return 0xFFFFFFFFLL;
func3((__int64)&kpos, key_map);
v0 = off_B98;
if ( *((_DWORD *)off_B98 + 3) < *((_DWORD *)off_B98 + 4) )
return flexpopch(v0);
}
}
if ( !literal_next )
{
qword_C0C = 0LL;
imessage = 1;
}
return (unsigned int)kbdintr_ch;
}

original_code:
int
map_getc()
{
    while (((npos.mp_dest)->fxb_rcnt >= (npos.mp_dest)->fxb_wcnt)) {
 if (kbdintr) {
     if (!literal_next) {
  ((&canon_queue)->fxb_rcnt = (&canon_queue)->fxb_wcnt = 0);
  imessage = 1;
     } else {
     }
     return(kbdintr_ch);
 }
 if (!((npos.mp_src)->fxb_rcnt >= (npos.mp_src)->fxb_wcnt)) {
     if (literal_next) {
  return(flexpopch(npos.mp_src));
     }
     mapthrough(&npos,
   (State == NORMAL) ? cmd_map :
   (State == INSERT ||
    State == REPLACE ||
    State == CMDLINE) ? ins_map :
   ((void *)0));
 } else if (!((kpos.mp_src)->fxb_rcnt >= (kpos.mp_src)->fxb_wcnt)) {
     mapthrough(&kpos, key_map);
 } else {
     break;
 }
    }
    return(((npos.mp_dest)->fxb_rcnt >= (npos.mp_dest)->fxb_wcnt) ? (-1) : flexpopch(npos.mp_dest));
}

return_type: int


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

if ( kpos )
result = map_failed((__int64)&kpos);
else
result = map_failed((__int64)&npos);
return result;
}

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

if ( kpos )
result = func1((__int64)&kpos);
else
result = func1((__int64)&npos);
return result;
}

original_code:
void
map_timeout()
{
    if (kpos.mp_map != ((void *)0)) {
 map_failed(&kpos);
    } else {
 map_failed(&npos);
    }
}


decompiled_code:
_BOOL8 map_waiting()
{
_BOOL8 result; // rax

result = 1LL;
if ( !kpos )
result = npos != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 func14()
{
_BOOL8 result; // rax

result = 1LL;
if ( !kpos )
result = npos != 0;
return result;
}

original_code:
bool_t
map_waiting()
{
    return(kpos.mp_map != ((void *)0) || npos.mp_map != ((void *)0));
}

canonical_original_code:
int map_waiting()
{
    return(kpos.mp_map != ((void *)0) || npos.mp_map != ((void *)0));
}


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

result = curwin;
*(_QWORD *)(curwin + 68LL) = 0LL;
return result;
}

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

result = curwin;
*(_QWORD *)(curwin + 68LL) = 0LL;
return result;
}

original_code:
void
init_sline()
{
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
}


decompiled_code:
__int64 show_message(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v2 = curwin;
*(_QWORD *)(curwin + 68LL) = 0LL;
vformat(v2 + 56, a1, va);
return update_sline();
}

canonical_decompiled_code:
__int64 func1(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v2 = curwin;
*(_QWORD *)(curwin + 68LL) = 0LL;
vformat(v2 + 56, a1, va);
return update_sline();
}

original_code:
void
show_message
    (char *format, ...)
{
    va_list argp;
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}

canonical_original_code:
void show_message(char *format, ...)
{
    __builtin_va_list argp;
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}


decompiled_code:
__int64 show_error(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( params[125] )
beep();
v2 = curwin;
va_start(va, a1);
*(_QWORD *)(curwin + 68LL) = 0LL;
vformat(v2 + 56, a1, va);
return update_sline();
}

canonical_decompiled_code:
__int64 func2(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( params[125] )
beep();
v2 = curwin;
va_start(va, a1);
*(_QWORD *)(curwin + 68LL) = 0LL;
vformat(v2 + 56, a1, va);
return update_sline();
}

original_code:
void
show_error
    (char *format, ...)
{
    va_list argp;
    if ((params[P_errorbells].p_value)) {
 beep();
    }
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}

canonical_original_code:
void show_error(char *format, ...)
{
    __builtin_va_list argp;
    if ((params[P_errorbells].p_value)) {
 beep();
    }
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}


decompiled_code:
__int64 info_update()
{
return show_file_info(params[195] == 1);
}

canonical_decompiled_code:
__int64 func4()
{
return func3(params[195] == 1);
}

original_code:
void
info_update()
{
    show_file_info((params[P_infoupdate].p_value) == 1);
}


WarnUnSaved

decompiled_code:
int WarnUnSaved()
{
int result; // eax

result = params[635];
if ( result )
{
result = xvChangesNotSaved();
if ( result )
{
fwrite("[No write since last change]\r\n", 1uLL, 0x1EuLL, stdout);
result = fflush(stdout);
}
}
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

result = params[635];
if ( result )
{
result = xvChangesNotSaved();
if ( result )
{
fwrite("[No write since last change]\r\n", 1uLL, 0x1EuLL, stdout);
result = fflush(stdout);
}
}
return result;
}

original_code:
static void
WarnUnSaved()
{
    if ((params[P_warn].p_value) && xvChangesNotSaved()) {
 fputs("[No write since last change]\r\n", stdout);
 fflush(stdout);
    }
}


exHelp

decompiled_code:
__int64 exHelp()
{
int v0; // ebx
__int64 v1; // rdi
__int64 result; // rax
__int64 v3; // rax

v0 = (int)echo;
v1 = params[89];
result = (unsigned int)echo & 0xFFFFFFF1;
LODWORD(echo) = (unsigned int)echo & 0xFFFFFFF1;
if ( v1 && (v3 = fexpand(v1, 0LL), result = exNewBuffer(v3, 0LL), (_DWORD)result) )
{
*(_DWORD *)(curbuf + 56LL) |= 2u;
move_window_to_cursor();
show_file_info(1LL);
result = redraw_window(0LL);
LODWORD(echo) = v0;
}
else
{
LODWORD(echo) = v0;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // ebx
__int64 v1; // rdi
__int64 result; // rax
__int64 v3; // rax

v0 = (int)echo;
v1 = params[89];
result = (unsigned int)echo & 0xFFFFFFF1;
LODWORD(echo) = (unsigned int)echo & 0xFFFFFFF1;
if ( v1 && (v3 = fexpand(v1, 0LL), result = exNewBuffer(v3, 0LL), (_DWORD)result) )
{
*(_DWORD *)(curbuf + 56LL) |= 2u;
move_window_to_cursor();
show_file_info(1LL);
result = redraw_window(0LL);
LODWORD(echo) = v0;
}
else
{
LODWORD(echo) = v0;
}
return result;
}

original_code:
void
exHelp()
{
    unsigned savecho;
    savecho = echo;
    echo &= ~(2 | 4 | 010);
    if ((params[P_helpfile].pu.pu_str) != ((void *)0) &&
      exNewBuffer(fexpand((params[P_helpfile].pu.pu_str), 0), 0)) {
 curbuf->b_flags |= 0x2;
 move_window_to_cursor();
 show_file_info(1);
 redraw_window(0);
    }
    echo = savecho;
}

canonical_original_code:
void exHelp()
{
    unsigned int savecho;
    savecho = echo;
    echo &= ~(2 | 4 | 010);
    if ((params[P_helpfile].pu.pu_str) != ((void *)0) &&
      exNewBuffer(fexpand((params[P_helpfile].pu.pu_str), 0), 0)) {
 curbuf->b_flags |= 0x2;
 move_window_to_cursor();
 show_file_info(1);
 redraw_window(0);
    }
    echo = savecho;
}

variable_types:
savecho
unsigned int


decompiled_code:
__int64 alt_file_name()
{
__int64 result; // rax
__int64 v1; // rax

result = 0LL;
if ( n_elements )
{
v1 = top - 1;
if ( top <= 0 )
v1 = 31LL;
result = stack[2 * v1];
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
__int64 v1; // rax

result = 0LL;
if ( n_elements )
{
v1 = top - 1;
if ( top <= 0 )
v1 = 31LL;
result = stack[2 * v1];
}
return result;
}

original_code:
char *
alt_file_name()
{
    if ((!n_elements)) {
 return(((void *)0));
    } else {
 return(stack[(top > 0 ? top - 1 : (sizeof(stack) / sizeof(stack[0])) - 1)].filename);
    }
}

canonical_original_code:
char *alt_file_name()
{
    if ((!n_elements)) {
 return(((void *)0));
    } else {
 return(stack[(top > 0 ? top - 1 : (sizeof(stack) / sizeof(stack[0])) - 1)].filename);
    }
}


xvClear

decompiled_code:
unsigned int xvClear()
{
__int64 v0; // rbx
int v1; // ebp
unsigned int result; // eax
int i; // ebx
int v4; // edi

v0 = *(_QWORD *)(curwin + 32LL);
v1 = *(_DWORD *)(v0 + 8);
(*(void (__fastcall **)(__int64, _QWORD))(v0 + 120))(v0, *(unsigned int *)(v0 + 40));
result = (*(__int64 (__fastcall **)(__int64))(v0 + 80))(v0);
if ( v1 )
{
for ( i = 0; i != v1; ++i )
{
v4 = i;
result = xvClearLine(v4);
}
}
return result;
}

canonical_decompiled_code:
unsigned int func5()
{
__int64 v0; // rbx
int v1; // ebp
unsigned int result; // eax
int i; // ebx
int v4; // edi

v0 = *(_QWORD *)(curwin + 32LL);
v1 = *(_DWORD *)(v0 + 8);
(*(void (__fastcall **)(__int64, _QWORD))(v0 + 120))(v0, *(unsigned int *)(v0 + 40));
result = (*(__int64 (__fastcall **)(__int64))(v0 + 80))(v0);
if ( v1 )
{
for ( i = 0; i != v1; ++i )
{
v4 = i;
result = func4(v4);
}
}
return result;
}

original_code:
void
xvClear()
{
    VirtScr *vs = curwin->w_vs;
    register unsigned row;
    register unsigned nrows;
    nrows = (vs->pv_rows);
    ((*((vs)->v_set_colour))(vs, (vs)->pv_colours[0]));
    ((*(vs->v_clear_all))(vs));
    for (row = 0; row < nrows; row++) {
 xvClearLine(row);
    }
}

canonical_original_code:
void xvClear()
{
    struct virtscr *vs = curwin->w_vs;
    register unsigned int row;
    register unsigned int nrows;
    nrows = (vs->pv_rows);
    ((*((vs)->v_set_colour))(vs, (vs)->pv_colours[0]));
    ((*(vs->v_clear_all))(vs));
    for (row = 0; row < nrows; row++) {
 xvClearLine(row);
    }
}

variable_types: struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct { char *s_line; unsigned char *s_colour; int s_used; unsigned int s_flags; } *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); } *

variable_types:
row
unsigned int

variable_types:
nrows
unsigned int

user_defined_types: struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); }


initline

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

result = newline(81LL);
if ( !result )
result = newline(1LL);
return result;
}

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

result = newline(81LL);
if ( !result )
result = newline(1LL);
return result;
}

original_code:
static Line *
initline()
{
    Line *lp;
    if ((lp = newline(81)) == ((void *)0)) {
 lp = newline(1);
    }
    return lp;
}

canonical_original_code:
static struct line *initline()
{
    struct line *lp;
    if ((lp = newline(81)) == ((void *)0)) {
 lp = newline(1);
    }
    return lp;
}

variable_types: struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *


decompiled_code:
__sighandler_t catch_signals()
{
setsigcatcher(2, (__sighandler_t)int_handler);
setsigcatcher(20, (__sighandler_t)tstp_handler);
setsigcatcher(1, (__sighandler_t)hup_handler);
return setsigcatcher(15, (__sighandler_t)term_handler);
}

canonical_decompiled_code:
__sighandler_t func5()
{
func3(2, (__sighandler_t)int_handler);
func3(20, (__sighandler_t)tstp_handler);
func3(1, (__sighandler_t)hup_handler);
return func3(15, (__sighandler_t)term_handler);
}

original_code:
void
catch_signals()
{
    setsigcatcher(2, int_handler);
    setsigcatcher(20, tstp_handler);
    setsigcatcher(1, hup_handler);
    setsigcatcher(15, term_handler);
}


ralloc

decompiled_code:
void *ralloc()
{
void *result; // rax
int v1; // ebx
int v2; // eax
void *v3; // rdi

result = (void *)reuselist;
if ( reuselist )
{
reuselist = *(_QWORD *)reuselist;
}
else
{
v1 = (int)echo;
v2 = (int)echo;
LOBYTE(v2) = (unsigned __int8)echo & 0x7F;
LODWORD(echo) = v2;
v3 = alloc(0x280uLL);
if ( v3 )
usemem_constprop_0((__int64)v3);
result = (void *)reuselist;
LODWORD(echo) = v1;
if ( reuselist )
reuselist = *(_QWORD *)reuselist;
else
result = alloc(0x28uLL);
}
return result;
}

canonical_decompiled_code:
void *func3()
{
void *result; // rax
int v1; // ebx
int v2; // eax
void *v3; // rdi

result = (void *)reuselist;
if ( reuselist )
{
reuselist = *(_QWORD *)reuselist;
}
else
{
v1 = (int)echo;
v2 = (int)echo;
LOBYTE(v2) = (unsigned __int8)echo & 0x7F;
LODWORD(echo) = v2;
v3 = func2(0x280uLL);
if ( v3 )
func0((__int64)v3);
result = (void *)reuselist;
LODWORD(echo) = v1;
if ( reuselist )
reuselist = *(_QWORD *)reuselist;
else
result = func2(0x28uLL);
}
return result;
}

original_code:
static Reusable *
ralloc()
{
    Reusable *p;
    if (reuselist == ((void *)0)) {
 unsigned savecho = echo;
 echo &= ~0200;
 if ((p = alloc((16 * sizeof (Reusable)))) != ((void *)0)) {
     usemem((char *) p, (16 * sizeof (Reusable)));
 }
 echo = savecho;
    }
    if (reuselist) {
 p = reuselist;
 reuselist = p->ru_next;
 return p;
    }
    return alloc(sizeof(Reusable));
}

canonical_original_code:
static union reusable *ralloc()
{
    union reusable *p;
    if (reuselist == ((void *)0)) {
 unsigned int savecho = echo;
 echo &= ~0200;
 if ((p = alloc((16 * sizeof (Reusable)))) != ((void *)0)) {
     usemem((char *) p, (16 * sizeof (Reusable)));
 }
 echo = savecho;
    }
    if (reuselist) {
 p = reuselist;
 reuselist = p->ru_next;
 return p;
    }
    return alloc(sizeof(Reusable));
}

variable_types: union reusable { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } ru_l; struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } ru_c; union reusable *ru_next; } *

user_defined_types: union reusable { struct line ru_l; struct change ru_c; union reusable *ru_next; }

user_defined_types: struct change { struct change *c_next; enum c_type_t c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; }


challoc

decompiled_code:
void *challoc()
{
return ralloc();
}

canonical_decompiled_code:
void *func4()
{
return func3();
}

original_code:
Change *
challoc()
{
    return (Change *) ralloc();
}

canonical_original_code:
struct change *challoc()
{
    return (struct change *) ralloc();
}

return_type: struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } *


bufempty

decompiled_code:
_BOOL8 bufempty()
{
__int64 v0; // rdx
_BOOL8 result; // rax

v0 = *(_QWORD *)(curbuf + 8LL);
if ( *(_QWORD *)(v0 + 8) == *(_QWORD *)(curbuf + 16LL) )
result = **(_BYTE **)(v0 + 16) == 0;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 func8()
{
__int64 v0; // rdx
_BOOL8 result; // rax

v0 = *(_QWORD *)(curbuf + 8LL);
if ( *(_QWORD *)(v0 + 8) == *(_QWORD *)(curbuf + 16LL) )
result = **(_BYTE **)(v0 + 16) == 0;
else
result = 0LL;
return result;
}

original_code:
bool_t
bufempty()
{
    return(curbuf->b_file->l_next == curbuf->b_lastline &&
    curbuf->b_file->l_text[0] == '\0');
}

canonical_original_code:
int bufempty()
{
    return(curbuf->b_file->l_next == curbuf->b_lastline &&
    curbuf->b_file->l_text[0] == '\0');
}


usage

decompiled_code:
__int64 usage()
{
startup_error(&Version);
startup_error("\nUsage: xvi { options } [ file ... ]");
startup_error(" xvi { options } -t tag");
startup_error(" xvi { options } +[num] file");
startup_error(" xvi { options } +/pat file");
startup_error("\nOptions are:");
startup_error(" -c ex-command");
startup_error(" -R (set readonly mode)");
startup_error(" -s [no]boolean-parameter");
startup_error(" -s parameter=value");
return startup_error(" -w nlines (ignored)");
}

canonical_decompiled_code:
__int64 func0()
{
startup_error(&Version);
startup_error("\nUsage: xvi { options } [ file ... ]");
startup_error(" xvi { options } -t tag");
startup_error(" xvi { options } +[num] file");
startup_error(" xvi { options } +/pat file");
startup_error("\nOptions are:");
startup_error(" -c ex-command");
startup_error(" -R (set readonly mode)");
startup_error(" -s [no]boolean-parameter");
startup_error(" -s parameter=value");
return startup_error(" -w nlines (ignored)");
}

original_code:
static void
usage()
{
    startup_error(Version);
    startup_error("\nUsage: xvi { options } [ file ... ]");
    startup_error("       xvi { options } -t tag");
    startup_error("       xvi { options } +[num] file");
    startup_error("       xvi { options } +/pat  file");
    startup_error("\nOptions are:");
    startup_error("       -c ex-command");
    startup_error("       -R (set readonly mode)");
    startup_error("       -s [no]boolean-parameter");
    startup_error("       -s parameter=value");
    startup_error("       -w nlines (ignored)");
}


original_code:
Buffer *
new_buffer()
{
    Buffer *newbuf;
    newbuf = alloc(sizeof(Buffer));
    if (newbuf == ((void *)0)) {
 return(((void *)0));
    }
    if (setup_buffer(newbuf) == 0) {
 free(newbuf);
 return(((void *)0));
    }
    newbuf->b_filename = ((void *)0);
    newbuf->b_tempfname = ((void *)0);
    newbuf->b_nwindows = 0;
    return(newbuf);
}

canonical_original_code:
struct buffer *new_buffer()
{
    struct buffer *newbuf;
    newbuf = alloc(sizeof(Buffer));
    if (newbuf == ((void *)0)) {
 return(((void *)0));
    }
    if (setup_buffer(newbuf) == 0) {
 free(newbuf);
 return(((void *)0));
    }
    newbuf->b_filename = ((void *)0);
    newbuf->b_tempfname = ((void *)0);
    newbuf->b_nwindows = 0;
    return(newbuf);
}

variable_types: struct buffer { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *b_line0; struct line *b_file; struct line *b_lastline; struct line *b_prevline; char *b_Undotext; char *b_filename; char *b_tempfname; unsigned int b_flags; int b_nwindows; struct mark { struct position { struct line *p_line; int p_index; } m_posn; int m_deleted; } b_mlist[26]; struct mark b_pcmark; struct changedata { unsigned int cd_nlevels; struct cmd { struct position cmd_startpos; struct position cmd_target; unsigned char cmd_flags; int cmd_two_char; int cmd_operator; int cmd_ch1; int cmd_ch2; long cmd_prenum; long cmd_opnum; int cmd_got_yp_name; int cmd_yp_name; int cmd_buffer_name; } *cd_vi_cmd; long cd_total_lines; struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } *cd_undo; struct change *cd_redo; } *b_change; } *

user_defined_types: struct buffer { struct line *b_line0; struct line *b_file; struct line *b_lastline; struct line *b_prevline; char *b_Undotext; char *b_filename; char *b_tempfname; unsigned int b_flags; int b_nwindows; struct mark b_mlist[26]; struct mark b_pcmark; struct changedata *b_change; }

user_defined_types: struct mark { struct position m_posn; int m_deleted; }

user_defined_types: struct changedata { unsigned int cd_nlevels; struct cmd *cd_vi_cmd; long cd_total_lines; struct change *cd_undo; struct change *cd_redo; }


lformat

decompiled_code:
__int64 lformat(__int64 a1, char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return vformat(a1, a2, (__int64)va);
}

canonical_decompiled_code:
__int64 func11(__int64 a1, char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return func10(a1, a2, (__int64)va);
}

original_code:
bool_t
lformat
    (Flexbuf *f, char *format, ...)
{
    va_list argp;
    bool_t retval;
    __builtin_va_start(argp,format);
    retval = vformat(f, format, argp);
    __builtin_va_end(argp);
    return retval;
}

canonical_original_code:
int lformat(struct Flexbuf *f, char *format, ...)
{
    __builtin_va_list argp;
    int retval;
    __builtin_va_start(argp,format);
    retval = vformat(f, format, argp);
    __builtin_va_end(argp);
    return retval;
}

variable_types: struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; } *

variable_types: retval int


unpreserve

decompiled_code:
void unpreserve()
{
__int64 v0; // rbx
const char *v1; // rdi

if ( params[335] != 3 )
{
v0 = curbuf;
v1 = *(const char **)(curbuf + 48LL);
if ( v1 )
{
remove(v1);
free(*(void **)(v0 + 48));
*(_QWORD *)(v0 + 48) = 0LL;
}
}
}

canonical_decompiled_code:
void func3()
{
__int64 v0; // rbx
const char *v1; // rdi

if ( params[335] != 3 )
{
v0 = curbuf;
v1 = *(const char **)(curbuf + 48LL);
if ( v1 )
{
remove(v1);
free(*(void **)(v0 + 48));
*(_QWORD *)(v0 + 48) = 0LL;
}
}
}

original_code:
void
unpreserve()
{
    Buffer *buffer = curbuf;
    if ((params[P_preserve].p_value) != 3 && buffer->b_tempfname != ((void *)0)) {
 (void) remove(buffer->b_tempfname);
 free(buffer->b_tempfname);
 buffer->b_tempfname = ((void *)0);
    }
}

canonical_original_code:
void unpreserve()
{
    struct buffer *buffer = curbuf;
    if ((params[P_preserve].p_value) != 3 && buffer->b_tempfname != ((void *)0)) {
 (void) remove(buffer->b_tempfname);
 free(buffer->b_tempfname);
 buffer->b_tempfname = ((void *)0);
    }
}

user_defined_types: struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; }

user_defined_types: struct position { struct line *p_line; int p_index; }

user_defined_types: struct cmd { struct position cmd_startpos; struct position cmd_target; unsigned char cmd_flags; int cmd_two_char; int cmd_operator; int cmd_ch1; int cmd_ch2; long cmd_prenum; long cmd_opnum; int cmd_got_yp_name; int cmd_yp_name; int cmd_buffer_name; }

user_defined_types: enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 }


repo: martolini/heisprosjekt

decompiled_code:
_BOOL8 io_init()
{
__int64 v0; // rax
__int64 v1; // rdi
int v2; // er12
unsigned int v3; // ebx
int v4; // ebp
int v5; // ebp
int v6; // eax
__int64 v7; // rdx

v0 = comedi_open("/dev/comedi0");
it_g = v0;
if ( !v0 )
return 0LL;
v1 = v0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v4 = comedi_dio_config(v1, 2LL, v3, 0LL);
v5 = comedi_dio_config(it_g, 3LL, v3, 1LL) | v4;
v6 = comedi_dio_config(it_g, 3LL, v3 + 8, 1LL);
v7 = v3 + 16;
++v3;
v2 |= v6 | v5 | comedi_dio_config(it_g, 3LL, v7, 0LL);
if ( v3 == 8 )
break;
v1 = it_g;
}
return v2 == 0;
}

canonical_decompiled_code:
_BOOL8 func0()
{
__int64 v0; // rax
__int64 v1; // rdi
int v2; // er12
unsigned int v3; // ebx
int v4; // ebp
int v5; // ebp
int v6; // eax
__int64 v7; // rdx

v0 = comedi_open("/dev/comedi0");
it_g = v0;
if ( !v0 )
return 0LL;
v1 = v0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v4 = comedi_dio_config(v1, 2LL, v3, 0LL);
v5 = comedi_dio_config(it_g, 3LL, v3, 1LL) | v4;
v6 = comedi_dio_config(it_g, 3LL, v3 + 8, 1LL);
v7 = v3 + 16;
++v3;
v2 |= v6 | v5 | comedi_dio_config(it_g, 3LL, v7, 0LL);
if ( v3 == 8 )
break;
v1 = it_g;
}
return v2 == 0;
}

original_code:
int io_init()
{
 int i, status = 0;
 it_g = comedi_open("/dev/comedi0");
 if (it_g == ((void *)0))
  return 0;
 for (i = 0; i < 8; i++) {
  status |= comedi_dio_config(it_g, 2, i, COMEDI_INPUT);
  status |= comedi_dio_config(it_g, 3, i, COMEDI_OUTPUT);
  status |= comedi_dio_config(it_g, 3, i+8, COMEDI_OUTPUT);
  status |= comedi_dio_config(it_g, 3, i+16, COMEDI_INPUT);
 }
 return (status == 0);
}

canonical_original_code:
int io_init()
{
 int i; int status = 0;
 it_g = comedi_open("/dev/comedi0");
 if (it_g == ((void *)0))
  return 0;
 for (i = 0; i < 8; i++) {
  status |= comedi_dio_config(it_g, 2, i, COMEDI_INPUT);
  status |= comedi_dio_config(it_g, 3, i, COMEDI_OUTPUT);
  status |= comedi_dio_config(it_g, 3, i+8, COMEDI_OUTPUT);
  status |= comedi_dio_config(it_g, 3, i+16, COMEDI_INPUT);
 }
 return (status == 0);
}

variable_types: i int

variable_types: status int


decompiled_code:
__int64 __fastcall io_set_bit(int a1)
{
return comedi_dio_write(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return comedi_dio_write(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 1LL);
}

original_code:
void io_set_bit(int channel)
{
 comedi_dio_write(it_g, channel >> 8, channel & 0xff, 1);
}

variable_types: channel int


decompiled_code:
__int64 __fastcall io_clear_bit(int a1)
{
return comedi_dio_write(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return comedi_dio_write(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 0LL);
}

original_code:
void io_clear_bit(int channel)
{
 comedi_dio_write(it_g, channel >> 8, channel & 0xff, 0);
}


decompiled_code:
__int64 __fastcall io_write_analog(int a1, unsigned int a2)
{
return comedi_data_write(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 0LL, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, unsigned int a2)
{
return comedi_data_write(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 0LL, 0LL, a2);
}

original_code:
void io_write_analog(int channel, int value)
{
 comedi_data_write(it_g, channel>>8, channel&0xff, 0, 0x00, value);
}

variable_types: value int


decompiled_code:
__int64 __fastcall io_read_bit(int a1)
{
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
comedi_dio_read(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, v2);
return v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
comedi_dio_read(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, v2);
return v2[0];
}

original_code:
int io_read_bit(int channel)
{
 unsigned int data=0;
 comedi_dio_read(it_g, channel>>8, channel&0xff, &data);
 return (int)data;
}

canonical_original_code:
int io_read_bit(int channel)
{
 unsigned int data = 0;
 comedi_dio_read(it_g, channel>>8, channel&0xff, &data);
 return (int)data;
}

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall io_read_analog(int a1)
{
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
comedi_data_read(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 0LL, 0LL, v2);
return v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
comedi_data_read(it_g, (unsigned int)(a1 >> 8), (unsigned __int8)a1, 0LL, 0LL, v2);
return v2[0];
}

original_code:
int io_read_analog(int channel)
{
 lsampl_t data = 0;
 comedi_data_read(it_g, channel>>8, channel&0xff, 0, 0x00, &data);
 return (int)data;
}

canonical_original_code:
int io_read_analog(int channel)
{
 unsigned int data = 0;
 comedi_data_read(it_g, channel>>8, channel&0xff, 0, 0x00, &data);
 return (int)data;
}


main

canonical_name: main

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

original_code:
int main()
{
    elev_run();
    return 0;
}

canonical_original_code:
int main()
{
    elev_run();
    return 0;
}


decompiled_code:
int timer_start()
{
return gettimeofday(&startTime, 0LL);
}

canonical_decompiled_code:
int func0()
{
return gettimeofday(&startTime, 0LL);
}

original_code:
void timer_start(void) {
    gettimeofday(&startTime, ((void *)0));
}

canonical_original_code:
void timer_start() {
    gettimeofday(&startTime, ((void *)0));
}


decompiled_code:
_BOOL8 __fastcall timer_timerIsFinished(double a1)
{
gettimeofday(&endTime_0, 0LL);
return (double)(int)((endTime_0.tv_usec - startTime.tv_usec) / 1000
+ 1000 * (LODWORD(endTime_0.tv_sec) - LODWORD(startTime.tv_sec)))
/ 1000.0 >= a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(double a1)
{
gettimeofday(&endTime_0, 0LL);
return (double)(int)((endTime_0.tv_usec - startTime.tv_usec) / 1000
+ 1000 * (LODWORD(endTime_0.tv_sec) - LODWORD(startTime.tv_sec)))
/ 1000.0 >= a1;
}

original_code:
int timer_timerIsFinished(double sek) {
    static struct timeval endTime;
    gettimeofday(&endTime, ((void *)0));
    double secs = (endTime.tv_sec - startTime.tv_sec) * 1000
    + (endTime.tv_usec - startTime.tv_usec) / 1000;
    if (secs/1000 >= sek)
        return 1;
    return 0;
}

variable_types: sek double

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

variable_types: secs double


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

if ( a1 )
result = io_set_bit(771LL);
else
result = io_clear_bit(771LL);
return result;
}

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

if ( a1 )
result = io_set_bit(771LL);
else
result = io_clear_bit(771LL);
return result;
}

original_code:
void panel_setDoorOpenLamp(int value) {
    if (value)
        io_set_bit((0x300+3));
    else
        io_clear_bit((0x300+3));
}


decompiled_code:
__int64 panel_getObstructionSignal()
{
return io_read_bit(791LL);
}

canonical_decompiled_code:
__int64 func1()
{
return io_read_bit(791LL);
}

original_code:
int panel_getObstructionSignal(void) {
    return io_read_bit((0x300+23));
}

canonical_original_code:
int panel_getObstructionSignal() {
    return io_read_bit((0x300+23));
}


decompiled_code:
__int64 panel_getStopSignal()
{
return io_read_bit(790LL);
}

canonical_decompiled_code:
__int64 func2()
{
return io_read_bit(790LL);
}

original_code:
int panel_getStopSignal(void) {
    return io_read_bit((0x300+22));
}

canonical_original_code:
int panel_getStopSignal() {
    return io_read_bit((0x300+22));
}


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

if ( a1 )
result = io_set_bit(782LL);
else
result = io_clear_bit(782LL);
return result;
}

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

if ( a1 )
result = io_set_bit(782LL);
else
result = io_clear_bit(782LL);
return result;
}

original_code:
void panel_setStopLamp(int value) {
    if (value)
        io_set_bit((0x300+14));
    else
        io_clear_bit((0x300+14));
}


decompiled_code:
__int64 panel_getFloorSensorSignal()
{
int v0; // er8
__int64 result; // rax
int v2; // er8
int v3; // er8

v0 = io_read_bit(516LL);
result = 0LL;
if ( !v0 )
{
v2 = io_read_bit(517LL);
result = 1LL;
if ( !v2 )
{
v3 = io_read_bit(518LL);
result = 2LL;
if ( !v3 )
result = (unsigned int)io_read_bit(519LL) == 0 ? -1 : 3;
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // er8
__int64 result; // rax
int v2; // er8
int v3; // er8

v0 = io_read_bit(516LL);
result = 0LL;
if ( !v0 )
{
v2 = io_read_bit(517LL);
result = 1LL;
if ( !v2 )
{
v3 = io_read_bit(518LL);
result = 2LL;
if ( !v3 )
result = (unsigned int)io_read_bit(519LL) == 0 ? -1 : 3;
}
}
return result;
}

original_code:
int panel_getFloorSensorSignal(void) {
    if (io_read_bit((0x200+4)))
        return 0;
    else if (io_read_bit((0x200+5)))
        return 1;
    else if (io_read_bit((0x200+6)))
        return 2;
    else if (io_read_bit((0x200+7)))
        return 3;
    else
        return -1;
}

canonical_original_code:
int panel_getFloorSensorSignal() {
    if (io_read_bit((0x200+4)))
        return 0;
    else if (io_read_bit((0x200+5)))
        return 1;
    else if (io_read_bit((0x200+6)))
        return 2;
    else if (io_read_bit((0x200+7)))
        return 3;
    else
        return -1;
}


decompiled_code:
__int64 __fastcall panel_setFloorIndicator(int a1)
{
if ( a1 < 0 )
_assert_fail("floor >= 0", "panel.c", 0x89u, "panel_setFloorIndicator");
if ( a1 > 3 )
_assert_fail("floor < N_FLOORS", "panel.c", 0x8Au, "panel_setFloorIndicator");
if ( (a1 & 2) != 0 )
{
io_set_bit(768LL);
if ( (a1 & 1) != 0 )
return io_set_bit(769LL);
}
else
{
io_clear_bit(768LL);
if ( (a1 & 1) != 0 )
return io_set_bit(769LL);
}
return io_clear_bit(769LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
if ( a1 < 0 )
_assert_fail("floor >= 0", "panel.c", 0x89u, "panel_setFloorIndicator");
if ( a1 > 3 )
_assert_fail("floor < N_FLOORS", "panel.c", 0x8Au, "panel_setFloorIndicator");
if ( (a1 & 2) != 0 )
{
io_set_bit(768LL);
if ( (a1 & 1) != 0 )
return io_set_bit(769LL);
}
else
{
io_clear_bit(768LL);
if ( (a1 & 1) != 0 )
return io_set_bit(769LL);
}
return io_clear_bit(769LL);
}

original_code:
void panel_setFloorIndicator(int floor) {
    ((void) sizeof ((floor >= 0) ? 1 : 0), __extension__ ({ if (floor >= 0) ; else __assert_fail ("floor >= 0", "panel.c", 137, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((floor < 4) ? 1 : 0), __extension__ ({ if (floor < 4) ; else __assert_fail ("floor < N_FLOORS", "panel.c", 138, __extension__ __PRETTY_FUNCTION__); }));
    if (floor & 0x02)
        io_set_bit((0x300+0));
    else
        io_clear_bit((0x300+0));
    if (floor & 0x01)
        io_set_bit((0x300+1));
    else
        io_clear_bit((0x300+1));
}

variable_types: floor int


decompiled_code:
_BOOL8 __fastcall panel_getButtonSignal(unsigned int a1, int a2)
{
if ( a2 < 0 )
_assert_fail("floor >= 0", "panel.c", 0x9Cu, "panel_getButtonSignal");
if ( a2 > 3 )
_assert_fail("floor < N_FLOORS", "panel.c", 0x9Du, "panel_getButtonSignal");
if ( !a1 && a2 == 3 )
_assert_fail("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 0x9Eu, "panel_getButtonSignal");
if ( a1 == 1 && !a2 )
_assert_fail("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 0x9Fu, "panel_getButtonSignal");
if ( a1 > 2 )
_assert_fail(
"button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND",
"panel.c",
0xA0u,
"panel_getButtonSignal");
return (unsigned int)io_read_bit((unsigned int)button_channel_matrix[3 * a2 + a1]) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(unsigned int a1, int a2)
{
if ( a2 < 0 )
_assert_fail("floor >= 0", "panel.c", 0x9Cu, "panel_getButtonSignal");
if ( a2 > 3 )
_assert_fail("floor < N_FLOORS", "panel.c", 0x9Du, "panel_getButtonSignal");
if ( !a1 && a2 == 3 )
_assert_fail("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 0x9Eu, "panel_getButtonSignal");
if ( a1 == 1 && !a2 )
_assert_fail("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 0x9Fu, "panel_getButtonSignal");
if ( a1 > 2 )
_assert_fail(
"button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND",
"panel.c",
0xA0u,
"panel_getButtonSignal");
return (unsigned int)io_read_bit((unsigned int)button_channel_matrix[3 * a2 + a1]) != 0;
}

original_code:
int panel_getButtonSignal(elev_button_type_t button, int floor) {
    ((void) sizeof ((floor >= 0) ? 1 : 0), __extension__ ({ if (floor >= 0) ; else __assert_fail ("floor >= 0", "panel.c", 156, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((floor < 4) ? 1 : 0), __extension__ ({ if (floor < 4) ; else __assert_fail ("floor < N_FLOORS", "panel.c", 157, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_UP && floor == 4 -1)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_UP && floor == 4 -1)) ; else __assert_fail ("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 158, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_DOWN && floor == 0)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_DOWN && floor == 0)) ; else __assert_fail ("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 159, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ? 1 : 0), __extension__ ({ if (button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ; else __assert_fail ("button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND", "panel.c", 160, __extension__ __PRETTY_FUNCTION__); }));
    if (io_read_bit(button_channel_matrix[floor][button]))
        return 1;
    else
        return 0;
}

canonical_original_code:
int panel_getButtonSignal(enum tag_elev_lamp_type button, int floor) {
    ((void) sizeof ((floor >= 0) ? 1 : 0), __extension__ ({ if (floor >= 0) ; else __assert_fail ("floor >= 0", "panel.c", 156, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((floor < 4) ? 1 : 0), __extension__ ({ if (floor < 4) ; else __assert_fail ("floor < N_FLOORS", "panel.c", 157, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_UP && floor == 4 -1)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_UP && floor == 4 -1)) ; else __assert_fail ("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 158, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_DOWN && floor == 0)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_DOWN && floor == 0)) ; else __assert_fail ("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 159, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ? 1 : 0), __extension__ ({ if (button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ; else __assert_fail ("button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND", "panel.c", 160, __extension__ __PRETTY_FUNCTION__); }));
    if (io_read_bit(button_channel_matrix[floor][button]))
        return 1;
    else
        return 0;
}

variable_types: enum tag_elev_lamp_type { BUTTON_CALL_UP=0, BUTTON_CALL_DOWN=1, BUTTON_COMMAND=2 }


decompiled_code:
__int64 __fastcall panel_setButtonLamp(unsigned int a1, int a2, int a3)
{
__int64 v3; // rdi
__int64 result; // rax

if ( a2 < 0 )
_assert_fail("floor >= 0", "panel.c", 0xAEu, "panel_setButtonLamp");
if ( a2 > 3 )
_assert_fail("floor < N_FLOORS", "panel.c", 0xAFu, "panel_setButtonLamp");
if ( !a1 && a2 == 3 )
_assert_fail("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 0xB0u, "panel_setButtonLamp");
if ( a1 == 1 && !a2 )
_assert_fail("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 0xB1u, "panel_setButtonLamp");
if ( a1 > 2 )
_assert_fail(
"button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND",
"panel.c",
0xB2u,
"panel_setButtonLamp");
v3 = (unsigned int)lamp_channel_matrix[3 * a2 + a1];
if ( a3 == 1 )
result = io_set_bit(v3);
else
result = io_clear_bit(v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, int a2, int a3)
{
__int64 v3; // rdi
__int64 result; // rax

if ( a2 < 0 )
_assert_fail("floor >= 0", "panel.c", 0xAEu, "panel_setButtonLamp");
if ( a2 > 3 )
_assert_fail("floor < N_FLOORS", "panel.c", 0xAFu, "panel_setButtonLamp");
if ( !a1 && a2 == 3 )
_assert_fail("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 0xB0u, "panel_setButtonLamp");
if ( a1 == 1 && !a2 )
_assert_fail("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 0xB1u, "panel_setButtonLamp");
if ( a1 > 2 )
_assert_fail(
"button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND",
"panel.c",
0xB2u,
"panel_setButtonLamp");
v3 = (unsigned int)lamp_channel_matrix[3 * a2 + a1];
if ( a3 == 1 )
result = io_set_bit(v3);
else
result = io_clear_bit(v3);
return result;
}

original_code:
void panel_setButtonLamp(elev_button_type_t button, int floor, int value) {
    ((void) sizeof ((floor >= 0) ? 1 : 0), __extension__ ({ if (floor >= 0) ; else __assert_fail ("floor >= 0", "panel.c", 174, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((floor < 4) ? 1 : 0), __extension__ ({ if (floor < 4) ; else __assert_fail ("floor < N_FLOORS", "panel.c", 175, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_UP && floor == 4 -1)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_UP && floor == 4 -1)) ; else __assert_fail ("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 176, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_DOWN && floor == 0)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_DOWN && floor == 0)) ; else __assert_fail ("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 177, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ? 1 : 0), __extension__ ({ if (button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ; else __assert_fail ("button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND", "panel.c", 178, __extension__ __PRETTY_FUNCTION__); }));
    if (value == 1)
        io_set_bit(lamp_channel_matrix[floor][button]);
    else
        io_clear_bit(lamp_channel_matrix[floor][button]);
}

canonical_original_code:
void panel_setButtonLamp(enum tag_elev_lamp_type button, int floor, int value) {
    ((void) sizeof ((floor >= 0) ? 1 : 0), __extension__ ({ if (floor >= 0) ; else __assert_fail ("floor >= 0", "panel.c", 174, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((floor < 4) ? 1 : 0), __extension__ ({ if (floor < 4) ; else __assert_fail ("floor < N_FLOORS", "panel.c", 175, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_UP && floor == 4 -1)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_UP && floor == 4 -1)) ; else __assert_fail ("!(button == BUTTON_CALL_UP && floor == N_FLOORS-1)", "panel.c", 176, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((!(button == BUTTON_CALL_DOWN && floor == 0)) ? 1 : 0), __extension__ ({ if (!(button == BUTTON_CALL_DOWN && floor == 0)) ; else __assert_fail ("!(button == BUTTON_CALL_DOWN && floor == 0)", "panel.c", 177, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ? 1 : 0), __extension__ ({ if (button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND) ; else __assert_fail ("button == BUTTON_CALL_UP || button == BUTTON_CALL_DOWN || button == BUTTON_COMMAND", "panel.c", 178, __extension__ __PRETTY_FUNCTION__); }));
    if (value == 1)
        io_set_bit(lamp_channel_matrix[floor][button]);
    else
        io_clear_bit(lamp_channel_matrix[floor][button]);
}

user_defined_types: enum tag_elev_lamp_type { BUTTON_CALL_UP=0, BUTTON_CALL_DOWN=1, BUTTON_COMMAND=2 }


decompiled_code:
__int64 panel_init()
{
int i; // ebx

for ( i = 0; ; ++i )
{
if ( !i )
{
i = 1;
panel_setButtonLamp(0, 0, 0);
panel_setButtonLamp(2u, 0, 0);
}
panel_setButtonLamp(1u, i, 0);
if ( i == 3 )
break;
panel_setButtonLamp(0, i, 0);
panel_setButtonLamp(2u, i, 0);
}
panel_setButtonLamp(2u, 3, 0);
panel_setStopLamp(0);
panel_setDoorOpenLamp(0);
panel_setFloorIndicator(1);
return 1LL;
}

original_code:
int panel_init(void) {
    int i;
    for (i = 0; i < 4; ++i) {
        if (i != 0)
            panel_setButtonLamp(BUTTON_CALL_DOWN, i, LAMP_OFF);
        if (i != 4 -1)
            panel_setButtonLamp(BUTTON_CALL_UP, i, LAMP_OFF);
        panel_setButtonLamp(BUTTON_COMMAND, i, LAMP_OFF);
    }
    panel_setStopLamp(LAMP_OFF);
    panel_setDoorOpenLamp(LAMP_OFF);
    panel_setFloorIndicator(LAMP_ON);
    return 1;
}

canonical_original_code:
int panel_init() {
    int i;
    for (i = 0; i < 4; ++i) {
        if (i != 0)
            panel_setButtonLamp(BUTTON_CALL_DOWN, i, LAMP_OFF);
        if (i != 4 -1)
            panel_setButtonLamp(BUTTON_CALL_UP, i, LAMP_OFF);
        panel_setButtonLamp(BUTTON_COMMAND, i, LAMP_OFF);
    }
    panel_setStopLamp(LAMP_OFF);
    panel_setDoorOpenLamp(LAMP_OFF);
    panel_setFloorIndicator(LAMP_ON);
    return 1;
}


decompiled_code:
__int64 __fastcall panel_checkForOrders(unsigned int *a1)
{
unsigned int i; // ebp
unsigned int v2; // er12
unsigned int j; // ebx
__int64 result; // rax

for ( i = 0; i != 4; ++i )
{
v2 = 1;
for ( j = 0; ; ++j )
{
if ( j == 1 && !i || !j && i == 3 )
goto LABEL_16;
result = *a1;
if ( j == 2 || (_DWORD)result != 4 )
{
result = (unsigned int)(result - 2);
if ( (unsigned int)result > 1 || a1[3] != i )
{
result = panel_getButtonSignal(j, i);
if ( (_DWORD)result )
break;
}
}
LABEL_12:
if ( v2 > 2 )
goto LABEL_13;
LABEL_16:
++v2;
}
panel_setButtonLamp(j, i, 1);
if ( j == 1 )
{
oq_addOuterOrder(0LL, i);
goto LABEL_16;
}
if ( j != 2 )
{
result = oq_addOuterOrder(1LL, i);
goto LABEL_12;
}
result = oq_addInnerOrder(i, a1[3]);
LABEL_13:
;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int *a1)
{
unsigned int i; // ebp
unsigned int v2; // er12
unsigned int j; // ebx
__int64 result; // rax

for ( i = 0; i != 4; ++i )
{
v2 = 1;
for ( j = 0; ; ++j )
{
if ( j == 1 && !i || !j && i == 3 )
goto LABEL_16;
result = *a1;
if ( j == 2 || (_DWORD)result != 4 )
{
result = (unsigned int)(result - 2);
if ( (unsigned int)result > 1 || a1[3] != i )
{
result = func6(j, i);
if ( (_DWORD)result )
break;
}
}
LABEL_12:
if ( v2 > 2 )
goto LABEL_13;
LABEL_16:
++v2;
}
func7(j, i, 1);
if ( j == 1 )
{
oq_addOuterOrder(0LL, i);
goto LABEL_16;
}
if ( j != 2 )
{
result = oq_addOuterOrder(1LL, i);
goto LABEL_12;
}
result = oq_addInnerOrder(i, a1[3]);
LABEL_13:
;
}
return result;
}

original_code:
void panel_checkForOrders(const elevatorParameters_t *param) {
    int floor;
    elev_button_type_t buttonType;
    for (floor=0; floor<4; floor++) {
        for (buttonType = BUTTON_CALL_UP; buttonType <= BUTTON_COMMAND; buttonType++) {
            if ((buttonType == BUTTON_CALL_DOWN && floor == 0) || (buttonType == BUTTON_CALL_UP && floor == 4 -1))
                continue;
            else if (buttonType != BUTTON_COMMAND && param->currentState == STATE_EMERGENCYSTOP)
                continue;
            else if ((param->currentState == STATE_OPENDOOR || param->currentState == STATE_CLOSEDOOR) && floor == param->currentFloor)
                continue;
            int buttonPushed = panel_getButtonSignal(buttonType, floor);
            if (buttonPushed) {
                panel_setButtonLamp(buttonType, floor, LAMP_ON);
                switch(buttonType) {
                    case BUTTON_CALL_UP:
                        oq_addOuterOrder(DIR_UP, floor);
                        break;
                    case BUTTON_CALL_DOWN:
                        oq_addOuterOrder(DIR_DOWN, floor);
                        break;
                    case BUTTON_COMMAND:
                        oq_addInnerOrder(floor, param->currentFloor);
                        break;
                }
            }
        }
    }
}

canonical_original_code:
void panel_checkForOrders(struct tag_elevatorParameters_t *param) {
    int floor;
    enum tag_elev_lamp_type buttonType;
    for (floor=0; floor<4; floor++) {
        for (buttonType = BUTTON_CALL_UP; buttonType <= BUTTON_COMMAND; buttonType++) {
            if ((buttonType == BUTTON_CALL_DOWN && floor == 0) || (buttonType == BUTTON_CALL_UP && floor == 4 -1))
                continue;
            else if (buttonType != BUTTON_COMMAND && param->currentState == STATE_EMERGENCYSTOP)
                continue;
            else if ((param->currentState == STATE_OPENDOOR || param->currentState == STATE_CLOSEDOOR) && floor == param->currentFloor)
                continue;
            int buttonPushed = panel_getButtonSignal(buttonType, floor);
            if (buttonPushed) {
                panel_setButtonLamp(buttonType, floor, LAMP_ON);
                switch(buttonType) {
                    case BUTTON_CALL_UP:
                        oq_addOuterOrder(DIR_UP, floor);
                        break;
                    case BUTTON_CALL_DOWN:
                        oq_addOuterOrder(DIR_DOWN, floor);
                        break;
                    case BUTTON_COMMAND:
                        oq_addInnerOrder(floor, param->currentFloor);
                        break;
                }
            }
        }
    }
}

variable_types: struct tag_elevatorParameters_t { enum elevatorState_t { STATE_IDLE=0, STATE_DRIVE=1, STATE_OPENDOOR=2, STATE_CLOSEDOOR=3, STATE_EMERGENCYSTOP=4 } currentState; enum elevatorState_t { STATE_IDLE=0, STATE_DRIVE=1, STATE_OPENDOOR=2, STATE_CLOSEDOOR=3, STATE_EMERGENCYSTOP=4 } nextState; enum elevatorDirection_t { DIR_DOWN=0, DIR_UP=1 } directionUp; int currentFloor; int signals[-1]; } *

variable_types: buttonPushed int

user_defined_types: struct tag_elevatorParameters_t { enum elevatorState_t currentState; enum elevatorState_t nextState; enum elevatorDirection_t directionUp; int currentFloor; int signals[-1]; }


decompiled_code:
__int64 __fastcall panel_turnOffLightsInFloor(int a1)
{
bool v2; // r12
bool v3; // r13
unsigned int i; // edi
__int64 result; // rax

v2 = a1 == 0;
v3 = a1 == 3;
for ( i = 0; ; ++i )
{
if ( (i != 1 || !v2) && (i || !v3) )
{
result = panel_setButtonLamp(i, a1, 0);
if ( i + 1 > 2 )
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
bool v2; // r12
bool v3; // r13
unsigned int i; // edi
__int64 result; // rax

v2 = a1 == 0;
v3 = a1 == 3;
for ( i = 0; ; ++i )
{
if ( (i != 1 || !v2) && (i || !v3) )
{
result = func7(i, a1, 0);
if ( i + 1 > 2 )
break;
}
}
return result;
}

original_code:
void panel_turnOffLightsInFloor(int floor) {
    elev_button_type_t buttonType;
    for (buttonType = BUTTON_CALL_UP; buttonType <= BUTTON_COMMAND; buttonType++) {
        if ((buttonType == BUTTON_CALL_DOWN && floor == 0) || (buttonType == BUTTON_CALL_UP && floor == 4 -1))
            continue;
        panel_setButtonLamp(buttonType, floor, LAMP_OFF);
    }
}

canonical_original_code:
void panel_turnOffLightsInFloor(int floor) {
    enum tag_elev_lamp_type buttonType;
    for (buttonType = BUTTON_CALL_UP; buttonType <= BUTTON_COMMAND; buttonType++) {
        if ((buttonType == BUTTON_CALL_DOWN && floor == 0) || (buttonType == BUTTON_CALL_UP && floor == 4 -1))
            continue;
        panel_setButtonLamp(buttonType, floor, LAMP_OFF);
    }
}


decompiled_code:
__int64 panel_turnOffAllLights()
{
panel_turnOffLightsInFloor(0);
panel_turnOffLightsInFloor(1);
panel_turnOffLightsInFloor(2);
return panel_turnOffLightsInFloor(3);
}

original_code:
void panel_turnOffAllLights(void) {
    int floor;
    for (floor = 0; floor<4; floor++)
        panel_turnOffLightsInFloor(floor);
}

canonical_original_code:
void panel_turnOffAllLights() {
    int floor;
    for (floor = 0; floor<4; floor++)
        panel_turnOffLightsInFloor(floor);
}


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

result = (unsigned int)isInitialized_0;
if ( !isInitialized_0 )
{
orders = 0LL;
result = 1LL;
qword_168 = 0LL;
qword_170 = 0LL;
qword_178 = 0LL;
isInitialized_0 = 1;
}
return result;
}

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

result = (unsigned int)isInitialized_0;
if ( !isInitialized_0 )
{
orders = 0LL;
result = 1LL;
qword_168 = 0LL;
qword_170 = 0LL;
qword_178 = 0LL;
isInitialized_0 = 1;
}
return result;
}

original_code:
int oq_init(void) {
    static int isInitialized = 0;
    if (!isInitialized) {
        int dir, floor;
        for (dir=DIR_DOWN; dir<= DIR_UP; dir++) {
            for (floor=0; floor<4; floor++)
                orders[dir][floor] = 0;
        }
        isInitialized = 1;
    }
    return isInitialized;
}

canonical_original_code:
int oq_init() {
    static int isInitialized = 0;
    if (!isInitialized) {
        int dir; int floor;
        for (dir=DIR_DOWN; dir<= DIR_UP; dir++) {
            for (floor=0; floor<4; floor++)
                orders[dir][floor] = 0;
        }
        isInitialized = 1;
    }
    return isInitialized;
}

variable_types: isInitialized int

variable_types: dir int


decompiled_code:
__int64 oq_hasOrders()
{
__int64 *v0; // rax

v0 = &orders;
while ( !*(_DWORD *)v0 && !*((_DWORD *)v0 + 4) )
{
v0 = (__int64 *)((char *)v0 + 4);
if ( v0 == &qword_170 )
return 0LL;
}
return 1LL;
}

original_code:
int oq_hasOrders(void) {
    int floor;
    for (floor = 0; floor<4; floor++) {
        if (orders[DIR_DOWN][floor] || orders[DIR_UP][floor])
            return 1;
    }
    return 0;
}

canonical_original_code:
int oq_hasOrders() {
    int floor;
    for (floor = 0; floor<4; floor++) {
        if (orders[DIR_DOWN][floor] || orders[DIR_UP][floor])
            return 1;
    }
    return 0;
}


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

result = a2 + 4LL * a1;
*((_DWORD *)&orders + result) = 1;
return result;
}

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

result = a2 + 4LL * a1;
*((_DWORD *)&orders + result) = 1;
return result;
}

original_code:
void oq_addOuterOrder(elevatorDirection_t direction, int floor) {
    orders[direction][floor] = 1;
}

canonical_original_code:
void oq_addOuterOrder(enum elevatorDirection_t direction, int floor) {
    orders[direction][floor] = 1;
}

variable_types: enum elevatorDirection_t { DIR_DOWN=0, DIR_UP=1 }


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

result = a1 + 4LL * (a1 > a2);
*((_DWORD *)&orders + result) = 1;
return result;
}

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

result = a1 + 4LL * (a1 > a2);
*((_DWORD *)&orders + result) = 1;
return result;
}

original_code:
void oq_addInnerOrder(int floor, int currentFloor) {
    int direction = DIR_DOWN;
    if (floor > currentFloor)
        direction = DIR_UP;
    orders[direction][floor] = 1;
}

variable_types: currentFloor int

variable_types: direction int


decompiled_code:
void __fastcall oq_deleteOrderInFloor(int a1)
{
*((_DWORD *)&orders + a1) = 0;
*((_DWORD *)&qword_170 + a1) = 0;
}

canonical_decompiled_code:
void __fastcall func4(int a1)
{
*((_DWORD *)&orders + a1) = 0;
*((_DWORD *)&qword_170 + a1) = 0;
}

original_code:
void oq_deleteOrderInFloor(int floor) {
    int dir;
    for (dir=DIR_DOWN; dir<= DIR_UP; dir++)
        orders[dir][floor] = 0;
}


decompiled_code:
__int64 __fastcall oq_hasOrderInFloor(unsigned int a1, int a2)
{
return *((unsigned int *)&orders[2 * a1] + a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, int a2)
{
return *((unsigned int *)&orders[2 * a1] + a2);
}

original_code:
int oq_hasOrderInFloor(elevatorDirection_t direction, int floor) {
    return orders[direction][floor];
}

canonical_original_code:
int oq_hasOrderInFloor(enum elevatorDirection_t direction, int floor) {
    return orders[direction][floor];
}

user_defined_types: enum elevatorDirection_t { DIR_DOWN=0, DIR_UP=1 }


decompiled_code:
void oq_deleteAllOrders()
{
orders[0] = 0LL;
qword_168 = 0LL;
qword_170 = 0LL;
qword_178 = 0LL;
}

canonical_decompiled_code:
void func6()
{
orders[0] = 0LL;
qword_168 = 0LL;
qword_170 = 0LL;
qword_178 = 0LL;
}

original_code:
void oq_deleteAllOrders(void) {
    int dir, floor;
    for (dir=DIR_DOWN; dir<=DIR_UP; dir++) {
        for (floor=0; floor<4; floor++)
            orders[dir][floor] = 0;
    }
}

canonical_original_code:
void oq_deleteAllOrders() {
    int dir; int floor;
    for (dir=DIR_DOWN; dir<=DIR_UP; dir++) {
        for (floor=0; floor<4; floor++)
            orders[dir][floor] = 0;
    }
}


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

if ( a2 == 1 )
{
v5 = a1 + 1;
if ( a1 + 1 <= 3 )
{
while ( !(unsigned int)oq_hasOrderInFloor(1LL, v5) && !(unsigned int)oq_hasOrderInFloor(0LL, v5) )
{
if ( ++v5 == 4 )
return 0LL;
}
return 1LL;
}
return 0LL;
}
if ( !a2 )
{
v2 = a1 - 1;
if ( a1 - 1 >= 0 )
{
while ( !(unsigned int)oq_hasOrderInFloor(1LL, v2) && !(unsigned int)oq_hasOrderInFloor(0LL, v2) )
{
if ( v2-- == 0 )
return 1LL;
}
return 0LL;
}
}
return 1LL;
}

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

if ( a2 == 1 )
{
v5 = a1 + 1;
if ( a1 + 1 <= 3 )
{
while ( !(unsigned int)oq_hasOrderInFloor(1LL, v5) && !(unsigned int)oq_hasOrderInFloor(0LL, v5) )
{
if ( ++v5 == 4 )
return 0LL;
}
return 1LL;
}
return 0LL;
}
if ( !a2 )
{
v2 = a1 - 1;
if ( a1 - 1 >= 0 )
{
while ( !(unsigned int)oq_hasOrderInFloor(1LL, v2) && !(unsigned int)oq_hasOrderInFloor(0LL, v2) )
{
if ( v2-- == 0 )
return 1LL;
}
return 0LL;
}
}
return 1LL;
}

canonical_original_code:
enum elevatorDirection_t elev_findDirection(int currentFloor, int direction) {
    int floor = currentFloor;
    if (direction == DIR_UP) {
        for (floor = floor+1; floor<4; floor++) {
            if (oq_hasOrderInFloor(DIR_UP, floor) || oq_hasOrderInFloor(DIR_DOWN, floor))
                return DIR_UP;
        }
        return DIR_DOWN;
    }
    else if (direction == DIR_DOWN) {
        for (floor = floor-1; floor>=0; floor--) {
            if (oq_hasOrderInFloor(DIR_UP, floor) || oq_hasOrderInFloor(DIR_DOWN, floor))
                return DIR_DOWN;
        }
        return DIR_UP;
    }
    return DIR_UP;
}

return_type: enum elevatorDirection_t { DIR_DOWN=0, DIR_UP=1 }


decompiled_code:
__int64 __fastcall elev_updateSignals(unsigned __int64 a1)
{
_DWORD *v1; // rbx
unsigned int v2; // eax
__int64 result; // rax
int v4; // eax
__int64 v5; // rsi
__int64 v6; // rdi
int v7; // er8
_BOOL4 v8; // eax
unsigned int v9; // eax

v1 = (_DWORD *)a1;
switch ( *(_DWORD *)a1 )
{
case 0:
v4 = ((__int64 (*)(void))oq_hasOrders)();
v5 = *(unsigned int *)(a1 + 12);
v6 = 1LL;
v1[4] = v4;
v7 = oq_hasOrderInFloor(1LL, v5);
v8 = 1;
if ( !v7 )
{
v6 = 0LL;
v8 = oq_hasOrderInFloor(0LL, (unsigned int)v1[3]) != 0;
}
v1[5] = v8;
result = panel_getStopSignal(v6);
v1[6] = result;
break;
case 1:
v9 = panel_getFloorSensorSignal();
a1 = v9;
if ( v9 != -1 )
{
v1[3] = v9;
panel_setFloorIndicator(v9);
a1 = (unsigned int)v1[2];
if ( (unsigned int)oq_hasOrderInFloor(a1, (unsigned int)v1[3])
|| (a1 = (unsigned int)v1[3], (unsigned int)elev_findDirection(a1, v1[2]) == (v1[2] == 0)) )
{
v1[5] = 1;
goto LABEL_5;
}
}
v1[5] = 0;
result = panel_getStopSignal(a1);
v1[6] = result;
break;
case 2:
*(_DWORD *)(a1 + 28) = timer_timerIsFinished(3.0);
result = ((__int64 (*)(void))panel_getStopSignal)();
*(_DWORD *)(a1 + 24) = result;
break;
case 3:
*(_DWORD *)(a1 + 16) = ((__int64 (*)(void))oq_hasOrders)();
*(_DWORD *)(a1 + 32) = panel_getObstructionSignal();
result = panel_getStopSignal(a1);
*(_DWORD *)(a1 + 24) = result;
break;
case 4:
v2 = panel_getFloorSensorSignal();
a1 = v2;
if ( v2 != -1 )
{
v1[3] = v2;
panel_setFloorIndicator(v2);
}
v1[4] = oq_hasOrders(a1);
goto LABEL_5;
default:
LABEL_5:
result = panel_getStopSignal(a1);
v1[6] = result;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 a1)
{
_DWORD *v1; // rbx
unsigned int v2; // eax
__int64 result; // rax
int v4; // eax
__int64 v5; // rsi
__int64 v6; // rdi
int v7; // er8
_BOOL4 v8; // eax
unsigned int v9; // eax

v1 = (_DWORD *)a1;
switch ( *(_DWORD *)a1 )
{
case 0:
v4 = ((__int64 (*)(void))oq_hasOrders)();
v5 = *(unsigned int *)(a1 + 12);
v6 = 1LL;
v1[4] = v4;
v7 = oq_hasOrderInFloor(1LL, v5);
v8 = 1;
if ( !v7 )
{
v6 = 0LL;
v8 = oq_hasOrderInFloor(0LL, (unsigned int)v1[3]) != 0;
}
v1[5] = v8;
result = panel_getStopSignal(v6);
v1[6] = result;
break;
case 1:
v9 = panel_getFloorSensorSignal();
a1 = v9;
if ( v9 != -1 )
{
v1[3] = v9;
panel_setFloorIndicator(v9);
a1 = (unsigned int)v1[2];
if ( (unsigned int)oq_hasOrderInFloor(a1, (unsigned int)v1[3])
|| (a1 = (unsigned int)v1[3], (unsigned int)func0(a1, v1[2]) == (v1[2] == 0)) )
{
v1[5] = 1;
goto LABEL_5;
}
}
v1[5] = 0;
result = panel_getStopSignal(a1);
v1[6] = result;
break;
case 2:
*(_DWORD *)(a1 + 28) = timer_timerIsFinished(3.0);
result = ((__int64 (*)(void))panel_getStopSignal)();
*(_DWORD *)(a1 + 24) = result;
break;
case 3:
*(_DWORD *)(a1 + 16) = ((__int64 (*)(void))oq_hasOrders)();
*(_DWORD *)(a1 + 32) = panel_getObstructionSignal();
result = panel_getStopSignal(a1);
*(_DWORD *)(a1 + 24) = result;
break;
case 4:
v2 = panel_getFloorSensorSignal();
a1 = v2;
if ( v2 != -1 )
{
v1[3] = v2;
panel_setFloorIndicator(v2);
}
v1[4] = oq_hasOrders(a1);
goto LABEL_5;
default:
LABEL_5:
result = panel_getStopSignal(a1);
v1[6] = result;
break;
}
return result;
}

original_code:
void elev_updateSignals(elevatorParameters_t *param) {
    switch(param->currentState) {
        case STATE_IDLE:
            param->signals[SIG_HAS_ORDERS] = oq_hasOrders();
            param->signals[SIG_SHOULD_STOP] = (oq_hasOrderInFloor(DIR_UP, param->currentFloor) || oq_hasOrderInFloor(DIR_DOWN, param->currentFloor));
            break;
        case STATE_DRIVE:;
            int tempFloor = panel_getFloorSensorSignal();
            if (tempFloor != -1) {
                param->currentFloor = tempFloor;
                panel_setFloorIndicator(param->currentFloor);
                if (oq_hasOrderInFloor(param->directionUp, param->currentFloor) || (elev_findDirection(param->currentFloor, param->directionUp) == !param->directionUp))
                    param->signals[SIG_SHOULD_STOP] = 1;
                else param->signals[SIG_SHOULD_STOP] = 0;
            }
            else param->signals[SIG_SHOULD_STOP] = 0;
            break;
        case STATE_OPENDOOR:
            param->signals[SIG_TIMER_FINISHED] = timer_timerIsFinished(3);
            break;
        case STATE_CLOSEDOOR:
            param->signals[SIG_HAS_ORDERS] = oq_hasOrders();
            param->signals[SIG_OBSTRUCTION] = panel_getObstructionSignal();
            break;
        case STATE_EMERGENCYSTOP:;
            tempFloor = panel_getFloorSensorSignal();
            if (tempFloor != -1) {
                param->currentFloor = tempFloor;
                panel_setFloorIndicator(param->currentFloor);
            }
            param->signals[SIG_HAS_ORDERS] = oq_hasOrders();
            break;
    }
    param->signals[SIG_EMERGENCY_STOP] = panel_getStopSignal();
}

canonical_original_code:
void elev_updateSignals(struct tag_elevatorParameters_t *param) {
    switch(param->currentState) {
        case STATE_IDLE:
            param->signals[SIG_HAS_ORDERS] = oq_hasOrders();
            param->signals[SIG_SHOULD_STOP] = (oq_hasOrderInFloor(DIR_UP, param->currentFloor) || oq_hasOrderInFloor(DIR_DOWN, param->currentFloor));
            break;
        case STATE_DRIVE:;
            int tempFloor = panel_getFloorSensorSignal();
            if (tempFloor != -1) {
                param->currentFloor = tempFloor;
                panel_setFloorIndicator(param->currentFloor);
                if (oq_hasOrderInFloor(param->directionUp, param->currentFloor) || (elev_findDirection(param->currentFloor, param->directionUp) == !param->directionUp))
                    param->signals[SIG_SHOULD_STOP] = 1;
                else param->signals[SIG_SHOULD_STOP] = 0;
            }
            else param->signals[SIG_SHOULD_STOP] = 0;
            break;
        case STATE_OPENDOOR:
            param->signals[SIG_TIMER_FINISHED] = timer_timerIsFinished(3);
            break;
        case STATE_CLOSEDOOR:
            param->signals[SIG_HAS_ORDERS] = oq_hasOrders();
            param->signals[SIG_OBSTRUCTION] = panel_getObstructionSignal();
            break;
        case STATE_EMERGENCYSTOP:;
            tempFloor = panel_getFloorSensorSignal();
            if (tempFloor != -1) {
                param->currentFloor = tempFloor;
                panel_setFloorIndicator(param->currentFloor);
            }
            param->signals[SIG_HAS_ORDERS] = oq_hasOrders();
            break;
    }
    param->signals[SIG_EMERGENCY_STOP] = panel_getStopSignal();
}

variable_types: tempFloor int

user_defined_types: enum elevatorState_t { STATE_IDLE=0, STATE_DRIVE=1, STATE_OPENDOOR=2, STATE_CLOSEDOOR=3, STATE_EMERGENCYSTOP=4 }


printStatus

decompiled_code:
int __fastcall printStatus(unsigned int *a1)
{
unsigned int v1; // er13
unsigned int v2; // ebp
unsigned int v3; // er12
unsigned int v4; // eax

v1 = a1[1];
v2 = *a1;
v3 = a1[2];
v4 = oq_hasOrders(a1);
return printf(
"Current Floor: %i, HasOrders: %i, directionUP: %i, currentState = %i, elevParam->nextState = %i\n",
a1[3],
v4,
v3,
v2,
v1);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int *a1)
{
unsigned int v1; // er13
unsigned int v2; // ebp
unsigned int v3; // er12
unsigned int v4; // eax

v1 = a1[1];
v2 = *a1;
v3 = a1[2];
v4 = oq_hasOrders(a1);
return printf(
"Current Floor: %i, HasOrders: %i, directionUP: %i, currentState = %i, elevParam->nextState = %i\n",
a1[3],
v4,
v3,
v2,
v1);
}

original_code:
void printStatus (const elevatorParameters_t *param) {
    printf("Current Floor: %i, HasOrders: %i, directionUP: %i, currentState = %i, elevParam->nextState = %i\n", param->currentFloor, oq_hasOrders(), param->directionUp, param->currentState, param->nextState);
}

canonical_original_code:
void printStatus(struct tag_elevatorParameters_t *param) {
    printf("Current Floor: %i, HasOrders: %i, directionUP: %i, currentState = %i, elevParam->nextState = %i\n", param->currentFloor, oq_hasOrders(), param->directionUp, param->currentState, param->nextState);
}


decompiled_code:
__int64 __fastcall elev_setSpeed(int a1)
{
if ( a1 > 0 )
goto LABEL_6;
if ( a1 )
{
LABEL_5:
io_set_bit(783LL);
goto LABEL_7;
}
if ( last_speed_0 < 0 )
{
LABEL_6:
io_clear_bit(783LL);
goto LABEL_7;
}
if ( last_speed_0 )
goto LABEL_5;
LABEL_7:
last_speed_0 = a1;
return io_write_analog(256LL, 4 * abs32(a1) + 2048);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
if ( a1 > 0 )
goto LABEL_6;
if ( a1 )
{
LABEL_5:
io_set_bit(783LL);
goto LABEL_7;
}
if ( last_speed_0 < 0 )
{
LABEL_6:
io_clear_bit(783LL);
goto LABEL_7;
}
if ( last_speed_0 )
goto LABEL_5;
LABEL_7:
last_speed_0 = a1;
return io_write_analog(256LL, 4 * abs32(a1) + 2048);
}

original_code:
void elev_setSpeed(int speed)
{
    static int last_speed = 0;
    if (speed > 0)
        io_clear_bit((0x300+15));
    else if (speed < 0)
        io_set_bit((0x300+15));
    else if (last_speed < 0)
        io_clear_bit((0x300+15));
    else if (last_speed > 0)
        io_set_bit((0x300+15));
    last_speed = speed ;
    io_write_analog((0x100+0), 2048 + 4*abs(speed));
}

variable_types: speed int


decompiled_code:
__int64 __fastcall elev_stop(int a1)
{
if ( a1 == 1 )
elev_setSpeed(-100);
else
elev_setSpeed(100);
timer_start();
while ( !(unsigned int)timer_timerIsFinished(0.02) )
;
return elev_setSpeed(0);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
if ( a1 == 1 )
func3(-100);
else
func3(100);
timer_start();
while ( !(unsigned int)timer_timerIsFinished(0.02) )
;
return func3(0);
}

original_code:
void elev_stop(elevatorDirection_t direction){
    if (direction == DIR_UP) elev_setSpeed(-100);
    else elev_setSpeed(100);
    timer_start();
    while (!timer_timerIsFinished(0.02)) {;}
    elev_setSpeed(0);
}

canonical_original_code:
void elev_stop(enum elevatorDirection_t direction){
    if (direction == DIR_UP) elev_setSpeed(-100);
    else elev_setSpeed(100);
    timer_start();
    while (!timer_timerIsFinished(0.02)) {;}
    elev_setSpeed(0);
}


decompiled_code:
__int64 __fastcall elev_init(__int64 a1)
{
unsigned int v2; // eax

if ( !(unsigned int)io_init() || !(unsigned int)oq_init() || !(unsigned int)panel_init() )
return 0LL;
elev_setSpeed(100);
while ( (unsigned int)panel_getFloorSensorSignal() == -1 )
;
elev_stop(1);
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
*(_DWORD *)(a1 + 32) = 0;
*(_QWORD *)a1 = 0LL;
*(_DWORD *)(a1 + 8) = 1;
v2 = panel_getFloorSensorSignal();
*(_DWORD *)(a1 + 12) = v2;
panel_setFloorIndicator(v2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int v2; // eax

if ( !(unsigned int)io_init() || !(unsigned int)oq_init() || !(unsigned int)panel_init() )
return 0LL;
func3(100);
while ( (unsigned int)panel_getFloorSensorSignal() == -1 )
;
func4(1);
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
*(_DWORD *)(a1 + 32) = 0;
*(_QWORD *)a1 = 0LL;
*(_DWORD *)(a1 + 8) = 1;
v2 = panel_getFloorSensorSignal();
*(_DWORD *)(a1 + 12) = v2;
panel_setFloorIndicator(v2);
return 1LL;
}

original_code:
int elev_init(elevatorParameters_t *param) {
    if (!io_init())
        return 0;
    if (!oq_init())
        return 0;
    if (!panel_init())
        return 0;
    elev_setSpeed(100);
    while (panel_getFloorSensorSignal()== -1) {
        ;
    }
    elev_stop(DIR_UP);
    param->currentState = STATE_IDLE;
    param->nextState = STATE_IDLE;
    int i;
    for (i=0; i<SIG_NUMBER_OF_SIGNALS; i++)
        param->signals[i] = 0;
    param->directionUp = DIR_UP;
    param->currentFloor = panel_getFloorSensorSignal();
    panel_setFloorIndicator(param->currentFloor);
    return 1;
}

canonical_original_code:
int elev_init(struct tag_elevatorParameters_t *param) {
    if (!io_init())
        return 0;
    if (!oq_init())
        return 0;
    if (!panel_init())
        return 0;
    elev_setSpeed(100);
    while (panel_getFloorSensorSignal()== -1) {
        ;
    }
    elev_stop(DIR_UP);
    param->currentState = STATE_IDLE;
    param->nextState = STATE_IDLE;
    int i;
    for (i=0; i<SIG_NUMBER_OF_SIGNALS; i++)
        param->signals[i] = 0;
    param->directionUp = DIR_UP;
    param->currentFloor = panel_getFloorSensorSignal();
    panel_setFloorIndicator(param->currentFloor);
    return 1;
}

variable_types: i int


decompiled_code:
__int64 __fastcall elev_setDirectionAndSpeed(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = elev_findDirection(*(_DWORD *)(a1 + 12), *(_DWORD *)(a1 + 8));
*(_DWORD *)(a1 + 8) = v1;
if ( v1 == 1 )
result = elev_setSpeed(300);
else
result = elev_setSpeed(-300);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = func0(*(_DWORD *)(a1 + 12), *(_DWORD *)(a1 + 8));
*(_DWORD *)(a1 + 8) = v1;
if ( v1 == 1 )
result = func3(300);
else
result = func3(-300);
return result;
}

original_code:
void elev_setDirectionAndSpeed(elevatorParameters_t *param) {
    param->directionUp = elev_findDirection(param->currentFloor, param->directionUp);
    if (param->directionUp == DIR_UP)
        elev_setSpeed(300);
    else elev_setSpeed(-300);
}

canonical_original_code:
void elev_setDirectionAndSpeed(struct tag_elevatorParameters_t *param) {
    param->directionUp = elev_findDirection(param->currentFloor, param->directionUp);
    if (param->directionUp == DIR_UP)
        elev_setSpeed(300);
    else elev_setSpeed(-300);
}


decompiled_code:
void elev_run()
{
int v0; // edx
int v1; // [rsp+0h] [rbp-38h] BYREF
int v2; // [rsp+4h] [rbp-34h]
int v3; // [rsp+8h] [rbp-30h]
unsigned int v4; // [rsp+Ch] [rbp-2Ch]
int v5; // [rsp+10h] [rbp-28h]
int v6; // [rsp+14h] [rbp-24h]
int v7; // [rsp+18h] [rbp-20h]
int v8; // [rsp+1Ch] [rbp-1Ch]
int v9; // [rsp+20h] [rbp-18h]

elev_init((__int64)&v1);
while ( 1 )
{
switch ( v1 )
{
case 0:
if ( !v5 )
{
v2 = 0;
LABEL_9:
if ( v7 )
{
LABEL_10:
v2 = 4;
if ( v1 == 1 )
elev_stop(v3);
goto LABEL_12;
}
goto LABEL_13;
}
if ( !v6 )
goto LABEL_36;
v2 = 2;
if ( v7 )
goto LABEL_10;
goto LABEL_22;
case 1:
if ( v6 )
goto LABEL_31;
v2 = 1;
goto LABEL_9;
case 2:
if ( !v8 )
{
v2 = 2;
goto LABEL_9;
}
v2 = 3;
if ( v7 )
goto LABEL_42;
LABEL_35:
panel_setDoorOpenLamp(0LL);
v0 = v2;
goto LABEL_6;
case 3:
if ( v9 )
{
LABEL_31:
v2 = 2;
if ( v7 )
goto LABEL_10;
if ( v1 == 1 )
elev_stop(v3);
LABEL_22:
oq_deleteOrderInFloor(v4);
panel_turnOffLightsInFloor(v4);
panel_setDoorOpenLamp(1LL);
timer_start();
v0 = v2;
}
else
{
if ( !v5 )
{
v2 = 0;
v0 = 0;
if ( !v7 )
goto LABEL_6;
LABEL_42:
v2 = 4;
LABEL_12:
oq_deleteAllOrders();
panel_turnOffAllLights();
panel_setStopLamp(1LL);
LABEL_13:
v0 = v2;
goto LABEL_6;
}
LABEL_36:
v2 = 1;
if ( v7 )
goto LABEL_10;
LABEL_37:
elev_setDirectionAndSpeed((__int64)&v1);
v0 = v2;
}
LABEL_6:
v1 = v0;
elev_updateSignals((unsigned __int64)&v1);
panel_checkForOrders(&v1);
break;
case 4:
if ( v5 )
{
v2 = 3;
if ( v7 )
{
v2 = 4;
v0 = v1;
}
else
{
panel_setStopLamp(0LL);
panel_setDoorOpenLamp(0LL);
v0 = v2;
}
}
else
{
v2 = 4;
if ( !v7 )
goto LABEL_13;
v0 = v1;
}
goto LABEL_6;
default:
if ( v7 )
goto LABEL_10;
v0 = v2;
if ( v1 == v2 )
goto LABEL_6;
switch ( v2 )
{
case 1:
goto LABEL_37;
case 0:
goto LABEL_6;
case 3:
goto LABEL_35;
case 4:
goto LABEL_12;
case 2:
goto LABEL_22;
}
goto LABEL_6;
}
}
}

canonical_decompiled_code:
void func7()
{
int v0; // edx
int v1; // [rsp+0h] [rbp-38h] BYREF
int v2; // [rsp+4h] [rbp-34h]
int v3; // [rsp+8h] [rbp-30h]
unsigned int v4; // [rsp+Ch] [rbp-2Ch]
int v5; // [rsp+10h] [rbp-28h]
int v6; // [rsp+14h] [rbp-24h]
int v7; // [rsp+18h] [rbp-20h]
int v8; // [rsp+1Ch] [rbp-1Ch]
int v9; // [rsp+20h] [rbp-18h]

func5((__int64)&v1);
while ( 1 )
{
switch ( v1 )
{
case 0:
if ( !v5 )
{
v2 = 0;
LABEL_9:
if ( v7 )
{
LABEL_10:
v2 = 4;
if ( v1 == 1 )
func4(v3);
goto LABEL_12;
}
goto LABEL_13;
}
if ( !v6 )
goto LABEL_36;
v2 = 2;
if ( v7 )
goto LABEL_10;
goto LABEL_22;
case 1:
if ( v6 )
goto LABEL_31;
v2 = 1;
goto LABEL_9;
case 2:
if ( !v8 )
{
v2 = 2;
goto LABEL_9;
}
v2 = 3;
if ( v7 )
goto LABEL_42;
LABEL_35:
panel_setDoorOpenLamp(0LL);
v0 = v2;
goto LABEL_6;
case 3:
if ( v9 )
{
LABEL_31:
v2 = 2;
if ( v7 )
goto LABEL_10;
if ( v1 == 1 )
func4(v3);
LABEL_22:
oq_deleteOrderInFloor(v4);
panel_turnOffLightsInFloor(v4);
panel_setDoorOpenLamp(1LL);
timer_start();
v0 = v2;
}
else
{
if ( !v5 )
{
v2 = 0;
v0 = 0;
if ( !v7 )
goto LABEL_6;
LABEL_42:
v2 = 4;
LABEL_12:
oq_deleteAllOrders();
panel_turnOffAllLights();
panel_setStopLamp(1LL);
LABEL_13:
v0 = v2;
goto LABEL_6;
}
LABEL_36:
v2 = 1;
if ( v7 )
goto LABEL_10;
LABEL_37:
func6((__int64)&v1);
v0 = v2;
}
LABEL_6:
v1 = v0;
func1((unsigned __int64)&v1);
panel_checkForOrders(&v1);
break;
case 4:
if ( v5 )
{
v2 = 3;
if ( v7 )
{
v2 = 4;
v0 = v1;
}
else
{
panel_setStopLamp(0LL);
panel_setDoorOpenLamp(0LL);
v0 = v2;
}
}
else
{
v2 = 4;
if ( !v7 )
goto LABEL_13;
v0 = v1;
}
goto LABEL_6;
default:
if ( v7 )
goto LABEL_10;
v0 = v2;
if ( v1 == v2 )
goto LABEL_6;
switch ( v2 )
{
case 1:
goto LABEL_37;
case 0:
goto LABEL_6;
case 3:
goto LABEL_35;
case 4:
goto LABEL_12;
case 2:
goto LABEL_22;
}
goto LABEL_6;
}
}
}

original_code:
void elev_run(void) {
    elevatorParameters_t l_elevParam;
    elevatorParameters_t *elevParam = &l_elevParam;
    elev_init(elevParam);
    while (1) {
        switch (elevParam->currentState) {
            case STATE_IDLE:
                if (elevParam->signals[SIG_HAS_ORDERS]) {
                    if (elevParam->signals[SIG_SHOULD_STOP])
                        elevParam->nextState = STATE_OPENDOOR;
                    else elevParam->nextState = STATE_DRIVE;
                }
                else elevParam->nextState = STATE_IDLE;
                break;
            case STATE_DRIVE:
                if (elevParam->signals[SIG_SHOULD_STOP]) elevParam->nextState = STATE_OPENDOOR;
                else elevParam->nextState = STATE_DRIVE;
                break;
            case STATE_OPENDOOR:
                if (elevParam->signals[SIG_TIMER_FINISHED]) elevParam->nextState = STATE_CLOSEDOOR;
                else elevParam->nextState = STATE_OPENDOOR;
                break;
            case STATE_CLOSEDOOR:
                if (elevParam->signals[SIG_OBSTRUCTION]) elevParam->nextState = STATE_OPENDOOR;
                else if (elevParam->signals[SIG_HAS_ORDERS]) elevParam->nextState = STATE_DRIVE;
                else elevParam->nextState = STATE_IDLE;
                break;
            case STATE_EMERGENCYSTOP:
                if (elevParam->signals[SIG_HAS_ORDERS]) elevParam->nextState = STATE_CLOSEDOOR;
                else elevParam->nextState = STATE_EMERGENCYSTOP;
                break;
        }
        if (elevParam->signals[SIG_EMERGENCY_STOP]) elevParam->nextState = STATE_EMERGENCYSTOP;
        if (elevParam->nextState != elevParam->currentState) {
            switch (elevParam->nextState) {
                case STATE_IDLE:
                    break;
                case STATE_DRIVE:
                    elev_setDirectionAndSpeed(elevParam);
                    break;
                case STATE_OPENDOOR:
                    if (elevParam->currentState == STATE_DRIVE) elev_stop(elevParam->directionUp);
                    oq_deleteOrderInFloor(elevParam->currentFloor);
                    panel_turnOffLightsInFloor(elevParam->currentFloor);
                    panel_setDoorOpenLamp(LAMP_ON);
                    timer_start();
                    break;
                case STATE_CLOSEDOOR:
                    if (elevParam->currentState == STATE_EMERGENCYSTOP) panel_setStopLamp(LAMP_OFF);
                    panel_setDoorOpenLamp(LAMP_OFF);
                    break;
                case STATE_EMERGENCYSTOP:
                    if (elevParam->currentState == STATE_DRIVE)
                        elev_stop(elevParam->directionUp);
                    oq_deleteAllOrders();
                    panel_turnOffAllLights();
                    panel_setStopLamp(LAMP_ON);
                    break;
            }
        }
        elevParam->currentState = elevParam->nextState;
        elev_updateSignals(elevParam);
        panel_checkForOrders(elevParam);
    }
}

canonical_original_code:
void elev_run() {
    struct tag_elevatorParameters_t l_elevParam;
    struct tag_elevatorParameters_t *elevParam = &l_elevParam;
    elev_init(elevParam);
    while (1) {
        switch (elevParam->currentState) {
            case STATE_IDLE:
                if (elevParam->signals[SIG_HAS_ORDERS]) {
                    if (elevParam->signals[SIG_SHOULD_STOP])
                        elevParam->nextState = STATE_OPENDOOR;
                    else elevParam->nextState = STATE_DRIVE;
                }
                else elevParam->nextState = STATE_IDLE;
                break;
            case STATE_DRIVE:
                if (elevParam->signals[SIG_SHOULD_STOP]) elevParam->nextState = STATE_OPENDOOR;
                else elevParam->nextState = STATE_DRIVE;
                break;
            case STATE_OPENDOOR:
                if (elevParam->signals[SIG_TIMER_FINISHED]) elevParam->nextState = STATE_CLOSEDOOR;
                else elevParam->nextState = STATE_OPENDOOR;
                break;
            case STATE_CLOSEDOOR:
                if (elevParam->signals[SIG_OBSTRUCTION]) elevParam->nextState = STATE_OPENDOOR;
                else if (elevParam->signals[SIG_HAS_ORDERS]) elevParam->nextState = STATE_DRIVE;
                else elevParam->nextState = STATE_IDLE;
                break;
            case STATE_EMERGENCYSTOP:
                if (elevParam->signals[SIG_HAS_ORDERS]) elevParam->nextState = STATE_CLOSEDOOR;
                else elevParam->nextState = STATE_EMERGENCYSTOP;
                break;
        }
        if (elevParam->signals[SIG_EMERGENCY_STOP]) elevParam->nextState = STATE_EMERGENCYSTOP;
        if (elevParam->nextState != elevParam->currentState) {
            switch (elevParam->nextState) {
                case STATE_IDLE:
                    break;
                case STATE_DRIVE:
                    elev_setDirectionAndSpeed(elevParam);
                    break;
                case STATE_OPENDOOR:
                    if (elevParam->currentState == STATE_DRIVE) elev_stop(elevParam->directionUp);
                    oq_deleteOrderInFloor(elevParam->currentFloor);
                    panel_turnOffLightsInFloor(elevParam->currentFloor);
                    panel_setDoorOpenLamp(LAMP_ON);
                    timer_start();
                    break;
                case STATE_CLOSEDOOR:
                    if (elevParam->currentState == STATE_EMERGENCYSTOP) panel_setStopLamp(LAMP_OFF);
                    panel_setDoorOpenLamp(LAMP_OFF);
                    break;
                case STATE_EMERGENCYSTOP:
                    if (elevParam->currentState == STATE_DRIVE)
                        elev_stop(elevParam->directionUp);
                    oq_deleteAllOrders();
                    panel_turnOffAllLights();
                    panel_setStopLamp(LAMP_ON);
                    break;
            }
        }
        elevParam->currentState = elevParam->nextState;
        elev_updateSignals(elevParam);
        panel_checkForOrders(elevParam);
    }
}

variable_types: struct tag_elevatorParameters_t { enum elevatorState_t { STATE_IDLE=0, STATE_DRIVE=1, STATE_OPENDOOR=2, STATE_CLOSEDOOR=3, STATE_EMERGENCYSTOP=4 } currentState; enum elevatorState_t { STATE_IDLE=0, STATE_DRIVE=1, STATE_OPENDOOR=2, STATE_CLOSEDOOR=3, STATE_EMERGENCYSTOP=4 } nextState; enum elevatorDirection_t { DIR_DOWN=0, DIR_UP=1 } directionUp; int currentFloor; int signals[-1]; }


repo: maryheintz/vme

decompiled_code:
__int64 __fastcall test_data_bus(int fd, void *buf)
{
char v2; // al
char bufa[25]; // [rsp+Fh] [rbp-19h] BYREF

*(_BYTE *)buf = 1;
do
{
lseek(fd, 0LL, 0);
write(fd, buf, 1uLL);
lseek(fd, -1LL, 1);
read(fd, bufa, 1uLL);
if ( *(_BYTE *)buf != bufa[0] )
return 0xFFFFFFFFLL;
v2 = 2 * *(_BYTE *)buf;
*(_BYTE *)buf = v2;
}
while ( v2 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, void *buf)
{
char v2; // al
char bufa[25]; // [rsp+Fh] [rbp-19h] BYREF

*(_BYTE *)buf = 1;
do
{
lseek(fd, 0LL, 0);
write(fd, buf, 1uLL);
lseek(fd, -1LL, 1);
read(fd, bufa, 1uLL);
if ( *(_BYTE *)buf != bufa[0] )
return 0xFFFFFFFFLL;
v2 = 2 * *(_BYTE *)buf;
*(_BYTE *)buf = v2;
}
while ( v2 );
return 0LL;
}

original_code:
int test_data_bus(int fd, unsigned char *data)
{
 unsigned char rval;
 for (*data = 1; 0 != *data; *data <<= 1) {
  lseek(fd, 0, 0);
  write(fd, data, sizeof (*data));
  lseek(fd, -(sizeof (*data)), 1);
  read(fd, &rval, sizeof (rval));
  if (rval != *data)
   return -1;
 }
 return 0;
}

variable_types: fd int

variable_types:
data
unsigned char *

variable_types:
rval
unsigned char


decompiled_code:
__int64 __fastcall test_address_bus(int fd, __int64 a2, __off_t *a3)
{
__int64 v3; // r12
__off_t v5; // r13
__off_t v6; // rsi
__off_t v7; // rsi
__int64 result; // rax
__off_t v9; // r14
__off_t v10; // rsi
char buf; // [rsp+Dh] [rbp-2Bh] BYREF
char v12; // [rsp+Eh] [rbp-2Ah] BYREF
char v13[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a2 - 1;
*a3 = 1LL;
buf = -86;
v12 = 85;
v5 = ((_BYTE)a2 - 1) & 1;
if ( (((_BYTE)a2 - 1) & 1) != 0 )
{
v6 = ((_BYTE)a2 - 1) & 1;
do
{
lseek(fd, v6, 0);
write(fd, &buf, 1uLL);
v6 = 2 * *a3;
*a3 = v6;
}
while ( (v3 & v6) != 0 );
lseek(fd, 0LL, 0);
write(fd, &v12, 1uLL);
*a3 = 1LL;
v7 = v5;
while ( 1 )
{
lseek(fd, v7, 0);
read(fd, v13, 1uLL);
if ( v13[0] != buf )
break;
v7 = 2 * *a3;
*a3 = v7;
if ( (v3 & v7) == 0 )
{
v9 = v5;
LABEL_9:
lseek(fd, v9, 0);
write(fd, &v12, 1uLL);
*a3 = 1LL;
v10 = v5;
while ( 1 )
{
lseek(fd, v10, 0);
read(fd, v13, 1uLL);
if ( v13[0] != buf && *a3 != v9 )
goto LABEL_7;
lseek(fd, -1LL, 1);
write(fd, &buf, 1uLL);
v10 = 2 * *a3;
*a3 = v10;
if ( (v3 & v10) == 0 )
{
v9 *= 2LL;
if ( (v3 & v9) != 0 )
goto LABEL_9;
return 0LL;
}
}
}
}
LABEL_7:
result = 0xFFFFFFFFLL;
}
else
{
lseek(fd, 0LL, 0);
write(fd, &v12, 1uLL);
*a3 = 1LL;
result = 0LL;
}
return result;
}

original_code:
int test_address_bus(int fd, off_t size, off_t * offset)
{
 unsigned char pattern = 0xaa;
 unsigned char antipattern = 0x55;
 unsigned char data;
 off_t ii;
 for (*offset = 1; *offset & (size - 1); *offset <<= 1) {
  lseek(fd, *offset, 0);
  write(fd, &pattern, sizeof (pattern));
 }
 lseek(fd, 0, 0);
 write(fd, &antipattern, sizeof (antipattern));
 for (*offset = 1; *offset & (size - 1); *offset <<= 1) {
  lseek(fd, *offset, 0);
  read(fd, &data, sizeof (data));
  if (data != pattern)
   return -1;
 }
 for (ii = 1; ii & (size - 1); ii <<= 1) {
  lseek(fd, ii, 0);
  write(fd, &antipattern, sizeof (antipattern));
  for (*offset = 1; *offset & (size - 1); *offset <<= 1) {
   lseek(fd, *offset, 0);
   read(fd, &data, sizeof (data));
   if ((data != pattern) && (*offset != ii))
    return -1;
   lseek(fd, -(sizeof (data)), 1);
   write(fd, &pattern, sizeof (pattern));
  }
 }
 return 0;
}

canonical_original_code:
int test_address_bus(int fd, long size, long *offset)
{
 unsigned char pattern = 0xaa;
 unsigned char antipattern = 0x55;
 unsigned char data;
 long ii;
 for (*offset = 1; *offset & (size - 1); *offset <<= 1) {
  lseek(fd, *offset, 0);
  write(fd, &pattern, sizeof (pattern));
 }
 lseek(fd, 0, 0);
 write(fd, &antipattern, sizeof (antipattern));
 for (*offset = 1; *offset & (size - 1); *offset <<= 1) {
  lseek(fd, *offset, 0);
  read(fd, &data, sizeof (data));
  if (data != pattern)
   return -1;
 }
 for (ii = 1; ii & (size - 1); ii <<= 1) {
  lseek(fd, ii, 0);
  write(fd, &antipattern, sizeof (antipattern));
  for (*offset = 1; *offset & (size - 1); *offset <<= 1) {
   lseek(fd, *offset, 0);
   read(fd, &data, sizeof (data));
   if ((data != pattern) && (*offset != ii))
    return -1;
   lseek(fd, -(sizeof (data)), 1);
   write(fd, &pattern, sizeof (pattern));
  }
 }
 return 0;
}

variable_types: size long

variable_types:
offset
long *

variable_types:
pattern
unsigned char

variable_types:
antipattern
unsigned char

variable_types:
data
unsigned char

variable_types: ii long


decompiled_code:
__int64 __fastcall test_memory(int fd, __int64 a2, __int64 *a3)
{
__off_t v5; // rsi
__int64 v6; // rax
__off_t v7; // rsi
__int64 v8; // rax
__off_t v10; // rsi
__int64 v11; // rax
char buf; // [rsp+Dh] [rbp-1Bh] BYREF
char v13; // [rsp+Eh] [rbp-1Ah] BYREF
char v14[25]; // [rsp+Fh] [rbp-19h] BYREF

*a3 = 0LL;
buf = 1;
if ( a2 <= 0 )
return 0LL;
v5 = 0LL;
do
{
lseek(fd, v5, 0);
write(fd, &buf, 1uLL);
v6 = *a3;
++buf;
v5 = v6 + 1;
*a3 = v6 + 1;
}
while ( v6 + 1 < a2 );
buf = 1;
v7 = 0LL;
*a3 = 0LL;
while ( 1 )
{
lseek(fd, v7, 0);
read(fd, v14, 1uLL);
if ( v14[0] != buf )
break;
v13 = ~buf;
lseek(fd, -1LL, 1);
write(fd, &v13, 1uLL);
v8 = *a3;
++buf;
v7 = v8 + 1;
*a3 = v8 + 1;
if ( v8 + 1 >= a2 )
{
buf = 1;
v10 = 0LL;
*a3 = 0LL;
while ( 1 )
{
lseek(fd, v10, 0);
read(fd, v14, 1uLL);
if ( v14[0] != ~buf )
break;
v14[0] = 0;
lseek(fd, -1LL, 1);
write(fd, v14, 1uLL);
v11 = *a3;
++buf;
v10 = v11 + 1;
*a3 = v11 + 1;
if ( v11 + 1 >= a2 )
return 0LL;
}
return 0xFFFFFFFFLL;
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, __int64 a2, __int64 *a3)
{
__off_t v5; // rsi
__int64 v6; // rax
__off_t v7; // rsi
__int64 v8; // rax
__off_t v10; // rsi
__int64 v11; // rax
char buf; // [rsp+Dh] [rbp-1Bh] BYREF
char v13; // [rsp+Eh] [rbp-1Ah] BYREF
char v14[25]; // [rsp+Fh] [rbp-19h] BYREF

*a3 = 0LL;
buf = 1;
if ( a2 <= 0 )
return 0LL;
v5 = 0LL;
do
{
lseek(fd, v5, 0);
write(fd, &buf, 1uLL);
v6 = *a3;
++buf;
v5 = v6 + 1;
*a3 = v6 + 1;
}
while ( v6 + 1 < a2 );
buf = 1;
v7 = 0LL;
*a3 = 0LL;
while ( 1 )
{
lseek(fd, v7, 0);
read(fd, v14, 1uLL);
if ( v14[0] != buf )
break;
v13 = ~buf;
lseek(fd, -1LL, 1);
write(fd, &v13, 1uLL);
v8 = *a3;
++buf;
v7 = v8 + 1;
*a3 = v8 + 1;
if ( v8 + 1 >= a2 )
{
buf = 1;
v10 = 0LL;
*a3 = 0LL;
while ( 1 )
{
lseek(fd, v10, 0);
read(fd, v14, 1uLL);
if ( v14[0] != ~buf )
break;
v14[0] = 0;
lseek(fd, -1LL, 1);
write(fd, v14, 1uLL);
v11 = *a3;
++buf;
v10 = v11 + 1;
*a3 = v11 + 1;
if ( v11 + 1 >= a2 )
return 0LL;
}
return 0xFFFFFFFFLL;
}
}
return 0xFFFFFFFFLL;
}

original_code:
int test_memory(int fd, off_t size, off_t * offset)
{
 unsigned char pattern;
 unsigned char antipattern;
 unsigned char data;
 for (pattern = 1, *offset = 0; *offset < size; ++pattern, ++(*offset)) {
  lseek(fd, *offset, 0);
  write(fd, &pattern, sizeof (pattern));
 }
 for (pattern = 1, *offset = 0; *offset < size; ++pattern, ++(*offset)) {
  lseek(fd, *offset, 0);
  read(fd, &data, sizeof (data));
  if (data != pattern)
   return -1;
  antipattern = ~pattern;
  lseek(fd, -(sizeof (data)), 1);
  write(fd, &antipattern, sizeof (antipattern));
 }
 for (pattern = 1, *offset = 0; *offset < size; ++pattern, ++(*offset)) {
  lseek(fd, *offset, 0);
  read(fd, &data, sizeof (data));
  if ((data & 0xff) != (~pattern & 0xff))
   return -1;
  data = 0;
  lseek(fd, -(sizeof (data)), 1);
  write(fd, &data, sizeof (data));
 }
 return 0;
}

canonical_original_code:
int test_memory(int fd, long size, long *offset)
{
 unsigned char pattern;
 unsigned char antipattern;
 unsigned char data;
 for (pattern = 1, *offset = 0; *offset < size; ++pattern, ++(*offset)) {
  lseek(fd, *offset, 0);
  write(fd, &pattern, sizeof (pattern));
 }
 for (pattern = 1, *offset = 0; *offset < size; ++pattern, ++(*offset)) {
  lseek(fd, *offset, 0);
  read(fd, &data, sizeof (data));
  if (data != pattern)
   return -1;
  antipattern = ~pattern;
  lseek(fd, -(sizeof (data)), 1);
  write(fd, &antipattern, sizeof (antipattern));
 }
 for (pattern = 1, *offset = 0; *offset < size; ++pattern, ++(*offset)) {
  lseek(fd, *offset, 0);
  read(fd, &data, sizeof (data));
  if ((data & 0xff) != (~pattern & 0xff))
   return -1;
  data = 0;
  lseek(fd, -(sizeof (data)), 1);
  write(fd, &data, sizeof (data));
 }
 return 0;
}