ttmove

decompiled_code:
void __fastcall ttmove(int a1, int a2)
{
char *v2; // rax

if ( ttrow != a1 || ttcol != a2 )
{
v2 = tgoto(*((const char **)cur_term->type.Strings + 10), a2, a1);
tputs(v2, 1, (int (*)(int))&ttputc);
ttrow = a1;
ttcol = a2;
}
}

canonical_decompiled_code:
void __fastcall func4(int a1, int a2)
{
char *v2; // rax

if ( ttrow != a1 || ttcol != a2 )
{
v2 = tgoto(*((const char **)cur_term->type.Strings + 10), a2, a1);
tputs(v2, 1, (int (*)(int))&ttputc);
ttrow = a1;
ttcol = a2;
}
}

original_code:
void
ttmove(int row, int col)
{
 if (ttrow != row || ttcol != col) {
  tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[10], col, row), 1, ttputc);
  ttrow = row;
  ttcol = col;
 }
}

canonical_original_code:
void ttmove(int row, int col)
{
 if (ttrow != row || ttcol != col) {
  tputs(tgoto(((struct termtype *)(cur_term))-> Strings[10], col, row), 1, ttputc);
  ttrow = row;
  ttcol = col;
 }
}

variable_types: row int

variable_types: col int


tteeol

repo: scott-parker/mg-openbsd

decompiled_code:
int tteeol()
{
char **v0; // rax
const char *v1; // rdi
int v2; // ebx

v0 = cur_term->type.Strings;
v1 = v0[6];
if ( v1 )
{
LODWORD(v0) = tputs(v1, 1, (int (*)(int))&ttputc);
}
else
{
v2 = ncol - ttcol;
if ( ncol != ttcol )
{
do
{
LODWORD(v0) = ttputc(32);
--v2;
}
while ( v2 );
}
ttcol = 1000;
ttrow = 1000;
}
return (int)v0;
}

canonical_decompiled_code:
int func5()
{
char **v0; // rax
const char *v1; // rdi
int v2; // ebx

v0 = cur_term->type.Strings;
v1 = v0[6];
if ( v1 )
{
LODWORD(v0) = tputs(v1, 1, (int (*)(int))&ttputc);
}
else
{
v2 = ncol - ttcol;
if ( ncol != ttcol )
{
do
{
LODWORD(v0) = ttputc(32);
--v2;
}
while ( v2 );
}
ttcol = 1000;
ttrow = 1000;
}
return (int)v0;
}

original_code:
void
tteeol(void)
{
 int i;
 if (((TERMTYPE *)(cur_term))-> Strings[6])
  tputs(((TERMTYPE *)(cur_term))-> Strings[6], 1, ttputc);
 else {
  i = ncol - ttcol;
  while (i--)
   ttputc(' ');
  ttrow = ttcol = 1000;
 }
}

canonical_original_code:
void tteeol()
{
 int i;
 if (((struct termtype *)(cur_term))-> Strings[6])
  tputs(((struct termtype *)(cur_term))-> Strings[6], 1, ttputc);
 else {
  i = ncol - ttcol;
  while (i--)
   ttputc(' ');
  ttrow = ttcol = 1000;
 }
}

variable_types: i int

return_type: void

user_defined_types: struct termtype { char *term_names; char *str_table; char *Booleans; short *Numbers; char **Strings; char *ext_str_table; char **ext_Names; unsigned short num_Booleans; unsigned short num_Numbers; unsigned short num_Strings; unsigned short ext_Booleans; unsigned short ext_Numbers; unsigned short ext_Strings; }


ttbeep

decompiled_code:
__int64 ttbeep()
{
tputs(*((const char **)cur_term->type.Strings + 1), 1, (int (*)(int))&ttputc);
return ttflush();
}

canonical_decompiled_code:
__int64 func6()
{
tputs(*((const char **)cur_term->type.Strings + 1), 1, (int (*)(int))&ttputc);
return ttflush();
}

original_code:
void
ttbeep(void)
{
 tputs(((TERMTYPE *)(cur_term))-> Strings[1], 1, ttputc);
 ttflush();
}

canonical_original_code:
void ttbeep()
{
 tputs(((struct termtype *)(cur_term))-> Strings[1], 1, ttputc);
 ttflush();
}


ttwindow

decompiled_code:
int __fastcall ttwindow(int a1, int a2)
{
char **v3; // rax
const char *v4; // rdi
int v5; // er12
char *v6; // rax

v3 = cur_term->type.Strings;
v4 = v3[3];
if ( v4 && (tttop != a1 || ttbot != a2) )
{
v5 = nrow - ttrow;
v6 = tgoto(v4, a2, a1);
LODWORD(v3) = tputs(v6, v5, (int (*)(int))&ttputc);
ttrow = 1000;
ttcol = 1000;
tttop = a1;
ttbot = a2;
}
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func7(int a1, int a2)
{
char **v3; // rax
const char *v4; // rdi
int v5; // er12
char *v6; // rax

v3 = cur_term->type.Strings;
v4 = v3[3];
if ( v4 && (tttop != a1 || ttbot != a2) )
{
v5 = nrow - ttrow;
v6 = tgoto(v4, a2, a1);
LODWORD(v3) = tputs(v6, v5, (int (*)(int))&ttputc);
ttrow = 1000;
ttcol = 1000;
tttop = a1;
ttbot = a2;
}
return (int)v3;
}

original_code:
void
ttwindow(int top, int bot)
{
 if (((TERMTYPE *)(cur_term))-> Strings[3] && (tttop != top || ttbot != bot)) {
  tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[3], bot, top), nrow - ttrow, ttputc);
  ttrow = 1000;
  ttcol = 1000;
  tttop = top;
  ttbot = bot;
 }
}

canonical_original_code:
void ttwindow(int top, int bot)
{
 if (((struct termtype *)(cur_term))-> Strings[3] && (tttop != top || ttbot != bot)) {
  tputs(tgoto(((struct termtype *)(cur_term))-> Strings[3], bot, top), nrow - ttrow, ttputc);
  ttrow = 1000;
  ttcol = 1000;
  tttop = top;
  ttbot = bot;
 }
}

variable_types: top int

variable_types: bot int


ttnowindow

decompiled_code:
int ttnowindow()
{
int result; // eax
const char *v1; // rdi
int v2; // ebx
int v3; // esi
char *v4; // rax

result = (int)cur_term;
v1 = (const char *)*((_QWORD *)cur_term->type.Strings + 3);
if ( v1 )
{
v2 = nrow - ttrow;
v3 = cur_term->type.Numbers[2];
if ( v3 < nrow )
v3 = nrow;
v4 = tgoto(v1, v3 - 1, 0);
result = tputs(v4, v2, (int (*)(int))&ttputc);
ttrow = 1000;
ttcol = 1000;
tttop = 1000;
ttbot = 1000;
}
return result;
}

canonical_decompiled_code:
int func8()
{
int result; // eax
const char *v1; // rdi
int v2; // ebx
int v3; // esi
char *v4; // rax

result = (int)cur_term;
v1 = (const char *)*((_QWORD *)cur_term->type.Strings + 3);
if ( v1 )
{
v2 = nrow - ttrow;
v3 = cur_term->type.Numbers[2];
if ( v3 < nrow )
v3 = nrow;
v4 = tgoto(v1, v3 - 1, 0);
result = tputs(v4, v2, (int (*)(int))&ttputc);
ttrow = 1000;
ttcol = 1000;
tttop = 1000;
ttbot = 1000;
}
return result;
}

original_code:
void
ttnowindow(void)
{
 if (((TERMTYPE *)(cur_term))-> Strings[3]) {
  tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[3], (nrow > ((TERMTYPE *)(cur_term))-> Numbers[2] ? nrow : ((TERMTYPE *)(cur_term))-> Numbers[2]) - 1, 0), nrow - ttrow, ttputc);
  ttrow = 1000;
  ttcol = 1000;
  tttop = 1000;
  ttbot = 1000;
 }
}

canonical_original_code:
void ttnowindow()
{
 if (((struct termtype *)(cur_term))-> Strings[3]) {
  tputs(tgoto(((struct termtype *)(cur_term))-> Strings[3], (nrow > ((struct termtype *)(cur_term))-> Numbers[2] ? nrow : ((struct termtype *)(cur_term))-> Numbers[2]) - 1, 0), nrow - ttrow, ttputc);
  ttrow = 1000;
  ttcol = 1000;
  tttop = 1000;
  ttbot = 1000;
 }
}


ttinsl

decompiled_code:
int __fastcall ttinsl(int a1, int a2, int a3)
{
int v4; // ebx
char **v5; // rax
int v6; // er13
const char *v7; // rdi
char *v8; // rax
int v9; // er12
char **v10; // rax
const char *v11; // rdi
char *v12; // rax
int v13; // er12
int i; // ebp

if ( a1 == a2 )
{
ttmove(a1, 0);
LODWORD(v10) = tteeol();
return (int)v10;
}
v4 = a3;
v5 = cur_term->type.Strings;
if ( !v5[3] || !v5[130] )
{
if ( !insdel )
{
LODWORD(v10) = panic("ttinsl: Can't insert/delete line");
return (int)v10;
}
ttmove(a2 + 1 - a3, 0);
v6 = nrow - ttrow;
v7 = (const char *)*((_QWORD *)cur_term->type.Strings + 106);
if ( v7 )
{
v8 = tgoto(v7, 0, v4);
tputs(v8, v6, (int (*)(int))&ttputc);
ttmove(a1, 0);
v9 = nrow - ttrow;
v10 = cur_term->type.Strings;
v11 = v10[110];
if ( v11 )
goto LABEL_7;
if ( v4 <= 0 )
goto LABEL_8;
}
else
{
v13 = 0;
if ( v4 <= 0 )
{
ttmove(a1, 0);
v10 = cur_term->type.Strings;
v11 = v10[110];
if ( !v11 )
goto LABEL_8;
v9 = nrow - ttrow;
goto LABEL_7;
}
do
{
tputs(*((const char **)cur_term->type.Strings + 22), v6, (int (*)(int))&ttputc);
++v13;
}
while ( v4 != v13 );
ttmove(a1, 0);
v9 = nrow - ttrow;
v11 = (const char *)*((_QWORD *)cur_term->type.Strings + 110);
if ( v11 )
{
LABEL_7:
v12 = tgoto(v11, 0, v4);
LODWORD(v10) = tputs(v12, v9, (int (*)(int))&ttputc);
LABEL_8:
ttrow = 1000;
ttcol = 1000;
return (int)v10;
}
}
for ( i = 0; i < v4; ++i )
LODWORD(v10) = tputs(*((const char **)cur_term->type.Strings + 53), v9, (int (*)(int))&ttputc);
goto LABEL_8;
}
ttwindow(a1, a2);
ttmove(a1, 0);
for ( ; v4; --v4 )
tputs(*((const char **)cur_term->type.Strings + 130), a2 - a1, (int (*)(int))&ttputc);
LODWORD(v10) = ttnowindow();
return (int)v10;
}

canonical_decompiled_code:
int __fastcall func9(int a1, int a2, int a3)
{
int v4; // ebx
char **v5; // rax
int v6; // er13
const char *v7; // rdi
char *v8; // rax
int v9; // er12
char **v10; // rax
const char *v11; // rdi
char *v12; // rax
int v13; // er12
int i; // ebp

if ( a1 == a2 )
{
func4(a1, 0);
LODWORD(v10) = func5();
return (int)v10;
}
v4 = a3;
v5 = cur_term->type.Strings;
if ( !v5[3] || !v5[130] )
{
if ( !insdel )
{
LODWORD(v10) = panic("ttinsl: Can't insert/delete line");
return (int)v10;
}
func4(a2 + 1 - a3, 0);
v6 = nrow - ttrow;
v7 = (const char *)*((_QWORD *)cur_term->type.Strings + 106);
if ( v7 )
{
v8 = tgoto(v7, 0, v4);
tputs(v8, v6, (int (*)(int))&ttputc);
func4(a1, 0);
v9 = nrow - ttrow;
v10 = cur_term->type.Strings;
v11 = v10[110];
if ( v11 )
goto LABEL_7;
if ( v4 <= 0 )
goto LABEL_8;
}
else
{
v13 = 0;
if ( v4 <= 0 )
{
func4(a1, 0);
v10 = cur_term->type.Strings;
v11 = v10[110];
if ( !v11 )
goto LABEL_8;
v9 = nrow - ttrow;
goto LABEL_7;
}
do
{
tputs(*((const char **)cur_term->type.Strings + 22), v6, (int (*)(int))&ttputc);
++v13;
}
while ( v4 != v13 );
func4(a1, 0);
v9 = nrow - ttrow;
v11 = (const char *)*((_QWORD *)cur_term->type.Strings + 110);
if ( v11 )
{
LABEL_7:
v12 = tgoto(v11, 0, v4);
LODWORD(v10) = tputs(v12, v9, (int (*)(int))&ttputc);
LABEL_8:
ttrow = 1000;
ttcol = 1000;
return (int)v10;
}
}
for ( i = 0; i < v4; ++i )
LODWORD(v10) = tputs(*((const char **)cur_term->type.Strings + 53), v9, (int (*)(int))&ttputc);
goto LABEL_8;
}
func7(a1, a2);
func4(a1, 0);
for ( ; v4; --v4 )
tputs(*((const char **)cur_term->type.Strings + 130), a2 - a1, (int (*)(int))&ttputc);
LODWORD(v10) = func8();
return (int)v10;
}

original_code:
void
ttinsl(int row, int bot, int nchunk)
{
 int i, nl;
 if (row == bot) {
  ttmove(row, 0);
  tteeol();
  return;
 }
 if (((TERMTYPE *)(cur_term))-> Strings[3] && ((TERMTYPE *)(cur_term))-> Strings[130]) {
  nl = bot - row;
  ttwindow(row, bot);
  ttmove(row, 0);
  while (nchunk--)
   tputs(((TERMTYPE *)(cur_term))-> Strings[130], nl, ttputc);
  ttnowindow();
  return;
 } else if (insdel) {
  ttmove(1 + bot - nchunk, 0);
  nl = nrow - ttrow;
  if (((TERMTYPE *)(cur_term))-> Strings[106])
   tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[106], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((TERMTYPE *)(cur_term))-> Strings[22], nl, ttputc);
  ttmove(row, 0);
  nl = nrow - ttrow;
  if (((TERMTYPE *)(cur_term))-> Strings[110])
   tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[110], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((TERMTYPE *)(cur_term))-> Strings[53], nl, ttputc);
  ttrow = 1000;
  ttcol = 1000;
 } else
  panic("ttinsl: Can't insert/delete line");
}

canonical_original_code:
void ttinsl(int row, int bot, int nchunk)
{
 int i; int nl;
 if (row == bot) {
  ttmove(row, 0);
  tteeol();
  return;
 }
 if (((struct termtype *)(cur_term))-> Strings[3] && ((struct termtype *)(cur_term))-> Strings[130]) {
  nl = bot - row;
  ttwindow(row, bot);
  ttmove(row, 0);
  while (nchunk--)
   tputs(((struct termtype *)(cur_term))-> Strings[130], nl, ttputc);
  ttnowindow();
  return;
 } else if (insdel) {
  ttmove(1 + bot - nchunk, 0);
  nl = nrow - ttrow;
  if (((struct termtype *)(cur_term))-> Strings[106])
   tputs(tgoto(((struct termtype *)(cur_term))-> Strings[106], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((struct termtype *)(cur_term))-> Strings[22], nl, ttputc);
  ttmove(row, 0);
  nl = nrow - ttrow;
  if (((struct termtype *)(cur_term))-> Strings[110])
   tputs(tgoto(((struct termtype *)(cur_term))-> Strings[110], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((struct termtype *)(cur_term))-> Strings[53], nl, ttputc);
  ttrow = 1000;
  ttcol = 1000;
 } else
  panic("ttinsl: Can't insert/delete line");
}

variable_types: nchunk int

variable_types: nl int


ttdell

decompiled_code:
int __fastcall ttdell(int a1, int a2, int a3)
{
char **v4; // rax
int v5; // er13
const char *v6; // rdi
char *v7; // rax
int v8; // er12
const char *v9; // rdi
char *v10; // rax
int v11; // er12
int v12; // eax
int v13; // ebp

if ( a1 == a2 )
{
ttmove(a1, 0);
LODWORD(v4) = tteeol();
return (int)v4;
}
if ( !*((_QWORD *)cur_term->type.Strings + 3) )
{
if ( !insdel )
{
LODWORD(v4) = panic("ttdell: Can't insert/delete line");
return (int)v4;
}
ttmove(a1, 0);
v5 = nrow - ttrow;
v6 = (const char *)*((_QWORD *)cur_term->type.Strings + 106);
if ( v6 )
{
v7 = tgoto(v6, 0, a3);
tputs(v7, v5, (int (*)(int))&ttputc);
ttmove(a2 + 1 - a3, 0);
v8 = nrow - ttrow;
v4 = cur_term->type.Strings;
v9 = v4[110];
if ( v9 )
goto LABEL_10;
if ( a3 <= 0 )
goto LABEL_11;
}
else
{
v11 = 0;
if ( a3 <= 0 )
{
ttmove(a2 + 1 - a3, 0);
v4 = cur_term->type.Strings;
v9 = v4[110];
if ( !v9 )
goto LABEL_11;
v8 = nrow - ttrow;
goto LABEL_10;
}
do
{
tputs(*((const char **)cur_term->type.Strings + 22), v5, (int (*)(int))&ttputc);
v12 = v11++;
}
while ( a3 != v11 );
ttmove(a2 - v12, 0);
v8 = nrow - ttrow;
v9 = (const char *)*((_QWORD *)cur_term->type.Strings + 110);
if ( v9 )
{
LABEL_10:
v10 = tgoto(v9, 0, a3);
LODWORD(v4) = tputs(v10, v8, (int (*)(int))&ttputc);
LABEL_11:
ttrow = 1000;
ttcol = 1000;
return (int)v4;
}
}
v13 = 0;
do
{
LODWORD(v4) = tputs(*((const char **)cur_term->type.Strings + 53), v8, (int (*)(int))&ttputc);
++v13;
}
while ( a3 > v13 );
goto LABEL_11;
}
ttwindow(a1, a2);
ttmove(a2, 0);
for ( ; a3; --a3 )
tputs(scroll_fwd, a2 - a1, (int (*)(int))&ttputc);
LODWORD(v4) = ttnowindow();
return (int)v4;
}

canonical_decompiled_code:
int __fastcall func10(int a1, int a2, int a3)
{
char **v4; // rax
int v5; // er13
const char *v6; // rdi
char *v7; // rax
int v8; // er12
const char *v9; // rdi
char *v10; // rax
int v11; // er12
int v12; // eax
int v13; // ebp

if ( a1 == a2 )
{
func4(a1, 0);
LODWORD(v4) = func5();
return (int)v4;
}
if ( !*((_QWORD *)cur_term->type.Strings + 3) )
{
if ( !insdel )
{
LODWORD(v4) = panic("ttdell: Can't insert/delete line");
return (int)v4;
}
func4(a1, 0);
v5 = nrow - ttrow;
v6 = (const char *)*((_QWORD *)cur_term->type.Strings + 106);
if ( v6 )
{
v7 = tgoto(v6, 0, a3);
tputs(v7, v5, (int (*)(int))&ttputc);
func4(a2 + 1 - a3, 0);
v8 = nrow - ttrow;
v4 = cur_term->type.Strings;
v9 = v4[110];
if ( v9 )
goto LABEL_10;
if ( a3 <= 0 )
goto LABEL_11;
}
else
{
v11 = 0;
if ( a3 <= 0 )
{
func4(a2 + 1 - a3, 0);
v4 = cur_term->type.Strings;
v9 = v4[110];
if ( !v9 )
goto LABEL_11;
v8 = nrow - ttrow;
goto LABEL_10;
}
do
{
tputs(*((const char **)cur_term->type.Strings + 22), v5, (int (*)(int))&ttputc);
v12 = v11++;
}
while ( a3 != v11 );
func4(a2 - v12, 0);
v8 = nrow - ttrow;
v9 = (const char *)*((_QWORD *)cur_term->type.Strings + 110);
if ( v9 )
{
LABEL_10:
v10 = tgoto(v9, 0, a3);
LODWORD(v4) = tputs(v10, v8, (int (*)(int))&ttputc);
LABEL_11:
ttrow = 1000;
ttcol = 1000;
return (int)v4;
}
}
v13 = 0;
do
{
LODWORD(v4) = tputs(*((const char **)cur_term->type.Strings + 53), v8, (int (*)(int))&ttputc);
++v13;
}
while ( a3 > v13 );
goto LABEL_11;
}
func7(a1, a2);
func4(a2, 0);
for ( ; a3; --a3 )
tputs(scroll_fwd, a2 - a1, (int (*)(int))&ttputc);
LODWORD(v4) = func8();
return (int)v4;
}

original_code:
void
ttdell(int row, int bot, int nchunk)
{
 int i, nl;
 if (row == bot) {
  ttmove(row, 0);
  tteeol();
  return;
 }
 if (((TERMTYPE *)(cur_term))-> Strings[3]) {
  nl = bot - row;
  ttwindow(row, bot);
  ttmove(bot, 0);
  while (nchunk--)
   tputs(scroll_fwd, nl, ttputc);
  ttnowindow();
 } else if (insdel) {
  ttmove(row, 0);
  nl = nrow - ttrow;
  if (((TERMTYPE *)(cur_term))-> Strings[106])
   tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[106], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((TERMTYPE *)(cur_term))-> Strings[22], nl, ttputc);
  ttmove(1 + bot - nchunk, 0);
  nl = nrow - ttrow;
  if (((TERMTYPE *)(cur_term))-> Strings[110])
   tputs(tgoto(((TERMTYPE *)(cur_term))-> Strings[110], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((TERMTYPE *)(cur_term))-> Strings[53], nl, ttputc);
  ttrow = 1000;
  ttcol = 1000;
 } else
  panic("ttdell: Can't insert/delete line");
}

canonical_original_code:
void ttdell(int row, int bot, int nchunk)
{
 int i; int nl;
 if (row == bot) {
  ttmove(row, 0);
  tteeol();
  return;
 }
 if (((struct termtype *)(cur_term))-> Strings[3]) {
  nl = bot - row;
  ttwindow(row, bot);
  ttmove(bot, 0);
  while (nchunk--)
   tputs(scroll_fwd, nl, ttputc);
  ttnowindow();
 } else if (insdel) {
  ttmove(row, 0);
  nl = nrow - ttrow;
  if (((struct termtype *)(cur_term))-> Strings[106])
   tputs(tgoto(((struct termtype *)(cur_term))-> Strings[106], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((struct termtype *)(cur_term))-> Strings[22], nl, ttputc);
  ttmove(1 + bot - nchunk, 0);
  nl = nrow - ttrow;
  if (((struct termtype *)(cur_term))-> Strings[110])
   tputs(tgoto(((struct termtype *)(cur_term))-> Strings[110], 0, nchunk), nl, ttputc);
  else
   for (i = 0; i < nchunk; i++)
    tputs(((struct termtype *)(cur_term))-> Strings[53], nl, ttputc);
  ttrow = 1000;
  ttcol = 1000;
 } else
  panic("ttdell: Can't insert/delete line");
}


tteeop

decompiled_code:
int tteeop()
{
char **v0; // rax
const char *v1; // rdi
int result; // eax
int v3; // ebx

v0 = cur_term->type.Strings;
v1 = v0[7];
if ( v1 )
return tputs(v1, nrow - ttrow, (int (*)(int))&ttputc);
tputs(v0[6], 1, (int (*)(int))&ttputc);
if ( insdel )
{
result = ttdell(ttrow + 1, cur_term->type.Numbers[2], cur_term->type.Numbers[2] - ttrow - 1);
}
else
{
v3 = ttrow + 1;
result = cur_term->type.Numbers[2];
if ( ttrow + 1 <= result )
{
do
{
ttmove(v3, 0);
tteeol();
++v3;
result = cur_term->type.Numbers[2];
}
while ( result >= v3 );
}
}
ttcol = 1000;
ttrow = 1000;
return result;
}

canonical_decompiled_code:
int func11()
{
char **v0; // rax
const char *v1; // rdi
int result; // eax
int v3; // ebx

v0 = cur_term->type.Strings;
v1 = v0[7];
if ( v1 )
return tputs(v1, nrow - ttrow, (int (*)(int))&ttputc);
tputs(v0[6], 1, (int (*)(int))&ttputc);
if ( insdel )
{
result = func10(ttrow + 1, cur_term->type.Numbers[2], cur_term->type.Numbers[2] - ttrow - 1);
}
else
{
v3 = ttrow + 1;
result = cur_term->type.Numbers[2];
if ( ttrow + 1 <= result )
{
do
{
func4(v3, 0);
func5();
++v3;
result = cur_term->type.Numbers[2];
}
while ( result >= v3 );
}
}
ttcol = 1000;
ttrow = 1000;
return result;
}

original_code:
void
tteeop(void)
{
 int line;
 if (((TERMTYPE *)(cur_term))-> Strings[7])
  tputs(((TERMTYPE *)(cur_term))-> Strings[7], nrow - ttrow, ttputc);
 else {
  tputs(((TERMTYPE *)(cur_term))-> Strings[6], 1, ttputc);
  if (insdel)
   ttdell(ttrow + 1, ((TERMTYPE *)(cur_term))-> Numbers[2], ((TERMTYPE *)(cur_term))-> Numbers[2] - ttrow - 1);
  else {
   for (line = ttrow + 1; line <= ((TERMTYPE *)(cur_term))-> Numbers[2]; ++line) {
    ttmove(line, 0);
    tteeol();
   }
  }
  ttrow = ttcol = 1000;
 }
}

canonical_original_code:
void tteeop()
{
 int line;
 if (((struct termtype *)(cur_term))-> Strings[7])
  tputs(((struct termtype *)(cur_term))-> Strings[7], nrow - ttrow, ttputc);
 else {
  tputs(((struct termtype *)(cur_term))-> Strings[6], 1, ttputc);
  if (insdel)
   ttdell(ttrow + 1, ((struct termtype *)(cur_term))-> Numbers[2], ((struct termtype *)(cur_term))-> Numbers[2] - ttrow - 1);
  else {
   for (line = ttrow + 1; line <= ((struct termtype *)(cur_term))-> Numbers[2]; ++line) {
    ttmove(line, 0);
    tteeol();
   }
  }
  ttrow = ttcol = 1000;
 }
}

variable_types: line int


ttcolor

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

if ( tthue != a1 )
{
if ( a1 == 1 )
{
result = tputs(*((const char **)cur_term->type.Strings + 43), 1, (int (*)(int))&ttputc);
}
else if ( a1 == 2 )
{
result = tputs(*((const char **)cur_term->type.Strings + 35), 1, (int (*)(int))&ttputc);
}
tthue = a1;
}
return result;
}

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

if ( tthue != a1 )
{
if ( a1 == 1 )
{
result = tputs(*((const char **)cur_term->type.Strings + 43), 1, (int (*)(int))&ttputc);
}
else if ( a1 == 2 )
{
result = tputs(*((const char **)cur_term->type.Strings + 35), 1, (int (*)(int))&ttputc);
}
tthue = a1;
}
return result;
}

original_code:
void
ttcolor(int color)
{
 if (color != tthue) {
  if (color == 1)
   tputs(((TERMTYPE *)(cur_term))-> Strings[43], 1, ttputc);
  else if (color == 2)
   tputs(((TERMTYPE *)(cur_term))-> Strings[35], 1, ttputc);
  tthue = color;
 }
}

canonical_original_code:
void ttcolor(int color)
{
 if (color != tthue) {
  if (color == 1)
   tputs(((struct termtype *)(cur_term))-> Strings[43], 1, ttputc);
  else if (color == 2)
   tputs(((struct termtype *)(cur_term))-> Strings[35], 1, ttputc);
  tthue = color;
 }
}

variable_types: color int


ttresize

decompiled_code:
__int64 ttresize()
{
__int64 result; // rax
__int16 v1; // [rsp+8h] [rbp-10h] BYREF

ioctl(0, 0x5413uLL, &v1);
result = vtresize(1LL);
if ( (_DWORD)result != 1 )
result = panic("vtresize failed");
return result;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 result; // rax
__int16 v1; // [rsp+8h] [rbp-10h] BYREF

ioctl(0, 0x5413uLL, &v1);
result = vtresize(1LL);
if ( (_DWORD)result != 1 )
result = panic("vtresize failed");
return result;
}

original_code:
void
ttresize(void)
{
 int newrow = 0, newcol = 0;
 struct winsize winsize;
 if (ioctl(0, 0x5413, &winsize) == 0) {
  newrow = winsize.ws_row;
  newcol = winsize.ws_col;
 }
 if ((newrow <= 0 || newcol <= 0) &&
     ((newrow = ((TERMTYPE *)(cur_term))-> Numbers[2]) <= 0 || (newcol = ((TERMTYPE *)(cur_term))-> Numbers[0]) <= 0)) {
  newrow = 24;
  newcol = 80;
 }
 if (vtresize(1, newrow, newcol) != 1)
  panic("vtresize failed");
}

canonical_original_code:
void ttresize()
{
 int newrow = 0; int newcol = 0;
 struct winsize winsize;
 if (ioctl(0, 0x5413, &winsize) == 0) {
  newrow = winsize.ws_row;
  newcol = winsize.ws_col;
 }
 if ((newrow <= 0 || newcol <= 0) &&
     ((newrow = ((struct termtype *)(cur_term))-> Numbers[2]) <= 0 || (newcol = ((struct termtype *)(cur_term))-> Numbers[0]) <= 0)) {
  newrow = 24;
  newcol = 80;
 }
 if (vtresize(1, newrow, newcol) != 1)
  panic("vtresize failed");
}

variable_types: newrow int

variable_types: newcol int

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


ttinit

decompiled_code:
__int64 ttinit()
{
char **v0; // rax
char *v1; // rdx
char *v2; // rdx
const char *v3; // rdi
char **v4; // rax
const char *v5; // rdi
int v6; // ebx
char **v7; // rax
const char *v8; // rdi
int v9; // ebx
int v10; // edx
const char *v11; // rdi
const char *v13; // rdi
const char *v14; // rdi
int v15[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( setupterm(0LL, 1, v15) )
panic("Terminal setup failed");
signal(28, (__sighandler_t)winchhandler);
signal(18, (__sighandler_t)winchhandler);
siginterrupt(28, 1);
v0 = cur_term->type.Strings;
v1 = v0[129];
scroll_fwd = v1;
if ( !v1 || !*v1 )
{
v2 = v0[107];
scroll_fwd = v2;
if ( !v2 || !*v2 )
scroll_fwd = "\n";
}
if ( !v0[10] || !v0[19] )
panic("This terminal is too stupid to run mg");
ttresize();
v3 = (const char *)*((_QWORD *)cur_term->type.Strings + 6);
if ( v3 )
tceeol = charcost(v3);
else
tceeol = ncol;
v4 = cur_term->type.Strings;
v5 = v4[3];
if ( v5 && v4[130] )
{
v6 = charcost(v5);
tcinsl = charcost(*((const char **)cur_term->type.Strings + 130)) + 2 * v6;
}
else
{
v13 = v4[110];
if ( v13 || (v13 = v4[53]) != 0LL )
tcinsl = charcost(v13);
else
tcinsl = ncol * nrow;
}
v7 = cur_term->type.Strings;
v8 = v7[3];
if ( v8 )
{
v9 = charcost(v8);
tcdell = charcost(scroll_fwd) + 2 * v9;
}
else
{
v14 = v7[106];
if ( !v14 )
{
v14 = v7[22];
if ( !v14 )
{
tcdell = ncol * nrow;
if ( v7[53] || !v7[110] )
goto LABEL_36;
LABEL_33:
v10 = 1;
if ( v7[106] )
goto LABEL_19;
goto LABEL_36;
}
}
tcdell = charcost(v14);
}
v7 = cur_term->type.Strings;
if ( v7[53] || v7[110] )
{
v10 = 1;
if ( v7[22] )
goto LABEL_19;
goto LABEL_33;
}
LABEL_36:
v10 = 0;
LABEL_19:
insdel = v10;
v11 = v7[28];
if ( v11 )
tputs(v11, 1, (int (*)(int))&ttputc);
return ttresize();
}

canonical_decompiled_code:
__int64 func14()
{
char **v0; // rax
char *v1; // rdx
char *v2; // rdx
const char *v3; // rdi
char **v4; // rax
const char *v5; // rdi
int v6; // ebx
char **v7; // rax
const char *v8; // rdi
int v9; // ebx
int v10; // edx
const char *v11; // rdi
const char *v13; // rdi
const char *v14; // rdi
int v15[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( setupterm(0LL, 1, v15) )
panic("Terminal setup failed");
signal(28, (__sighandler_t)winchhandler);
signal(18, (__sighandler_t)winchhandler);
siginterrupt(28, 1);
v0 = cur_term->type.Strings;
v1 = v0[129];
scroll_fwd = v1;
if ( !v1 || !*v1 )
{
v2 = v0[107];
scroll_fwd = v2;
if ( !v2 || !*v2 )
scroll_fwd = "\n";
}
if ( !v0[10] || !v0[19] )
panic("This terminal is too stupid to run mg");
func13();
v3 = (const char *)*((_QWORD *)cur_term->type.Strings + 6);
if ( v3 )
tceeol = func2(v3);
else
tceeol = ncol;
v4 = cur_term->type.Strings;
v5 = v4[3];
if ( v5 && v4[130] )
{
v6 = func2(v5);
tcinsl = func2(*((const char **)cur_term->type.Strings + 130)) + 2 * v6;
}
else
{
v13 = v4[110];
if ( v13 || (v13 = v4[53]) != 0LL )
tcinsl = func2(v13);
else
tcinsl = ncol * nrow;
}
v7 = cur_term->type.Strings;
v8 = v7[3];
if ( v8 )
{
v9 = func2(v8);
tcdell = func2(scroll_fwd) + 2 * v9;
}
else
{
v14 = v7[106];
if ( !v14 )
{
v14 = v7[22];
if ( !v14 )
{
tcdell = ncol * nrow;
if ( v7[53] || !v7[110] )
goto LABEL_36;
LABEL_33:
v10 = 1;
if ( v7[106] )
goto LABEL_19;
goto LABEL_36;
}
}
tcdell = func2(v14);
}
v7 = cur_term->type.Strings;
if ( v7[53] || v7[110] )
{
v10 = 1;
if ( v7[22] )
goto LABEL_19;
goto LABEL_33;
}
LABEL_36:
v10 = 0;
LABEL_19:
insdel = v10;
v11 = v7[28];
if ( v11 )
tputs(v11, 1, (int (*)(int))&ttputc);
return func13();
}

original_code:
void
ttinit(void)
{
 int errret;
 if (setupterm(((void *)0), 1, &errret))
  panic("Terminal setup failed");
 signal(28, winchhandler);
 signal(18, winchhandler);
 siginterrupt(28, 1);
 scroll_fwd = ((TERMTYPE *)(cur_term))-> Strings[129];
 if (scroll_fwd == ((void *)0) || *scroll_fwd == '\0') {
  scroll_fwd = ((TERMTYPE *)(cur_term))-> Strings[107];
  if (scroll_fwd == ((void *)0) || *scroll_fwd == '\0')
   scroll_fwd = "\n";
 }
 if (((TERMTYPE *)(cur_term))-> Strings[10] == ((void *)0) || ((TERMTYPE *)(cur_term))-> Strings[19] == ((void *)0))
  panic("This terminal is too stupid to run mg");
 ttresize();
 if (!((TERMTYPE *)(cur_term))-> Strings[6])
  tceeol = ncol;
 else
  tceeol = charcost(((TERMTYPE *)(cur_term))-> Strings[6]);
 if (((TERMTYPE *)(cur_term))-> Strings[3] && ((TERMTYPE *)(cur_term))-> Strings[130])
  tcinsl = charcost(((TERMTYPE *)(cur_term))-> Strings[3]) * 2 +
      charcost(((TERMTYPE *)(cur_term))-> Strings[130]);
 else if (((TERMTYPE *)(cur_term))-> Strings[110])
  tcinsl = charcost(((TERMTYPE *)(cur_term))-> Strings[110]);
 else if (((TERMTYPE *)(cur_term))-> Strings[53])
  tcinsl = charcost(((TERMTYPE *)(cur_term))-> Strings[53]);
 else
  tcinsl = nrow * ncol;
 if (((TERMTYPE *)(cur_term))-> Strings[3])
  tcdell = charcost(((TERMTYPE *)(cur_term))-> Strings[3]) * 2 +
      charcost(scroll_fwd);
 else if (((TERMTYPE *)(cur_term))-> Strings[106])
  tcdell = charcost(((TERMTYPE *)(cur_term))-> Strings[106]);
 else if (((TERMTYPE *)(cur_term))-> Strings[22])
  tcdell = charcost(((TERMTYPE *)(cur_term))-> Strings[22]);
 else
  tcdell = nrow * ncol;
 insdel = (((TERMTYPE *)(cur_term))-> Strings[53] || ((TERMTYPE *)(cur_term))-> Strings[110]) &&
     (((TERMTYPE *)(cur_term))-> Strings[22] || ((TERMTYPE *)(cur_term))-> Strings[106]);
 if (((TERMTYPE *)(cur_term))-> Strings[28])
  tputs(((TERMTYPE *)(cur_term))-> Strings[28], 1, ttputc);
 ttresize();
}

canonical_original_code:
void ttinit()
{
 int errret;
 if (setupterm(((void *)0), 1, &errret))
  panic("Terminal setup failed");
 signal(28, winchhandler);
 signal(18, winchhandler);
 siginterrupt(28, 1);
 scroll_fwd = ((struct termtype *)(cur_term))-> Strings[129];
 if (scroll_fwd == ((void *)0) || *scroll_fwd == '\0') {
  scroll_fwd = ((struct termtype *)(cur_term))-> Strings[107];
  if (scroll_fwd == ((void *)0) || *scroll_fwd == '\0')
   scroll_fwd = "\n";
 }
 if (((struct termtype *)(cur_term))-> Strings[10] == ((void *)0) || ((struct termtype *)(cur_term))-> Strings[19] == ((void *)0))
  panic("This terminal is too stupid to run mg");
 ttresize();
 if (!((struct termtype *)(cur_term))-> Strings[6])
  tceeol = ncol;
 else
  tceeol = charcost(((struct termtype *)(cur_term))-> Strings[6]);
 if (((struct termtype *)(cur_term))-> Strings[3] && ((struct termtype *)(cur_term))-> Strings[130])
  tcinsl = charcost(((struct termtype *)(cur_term))-> Strings[3]) * 2 +
      charcost(((struct termtype *)(cur_term))-> Strings[130]);
 else if (((struct termtype *)(cur_term))-> Strings[110])
  tcinsl = charcost(((struct termtype *)(cur_term))-> Strings[110]);
 else if (((struct termtype *)(cur_term))-> Strings[53])
  tcinsl = charcost(((struct termtype *)(cur_term))-> Strings[53]);
 else
  tcinsl = nrow * ncol;
 if (((struct termtype *)(cur_term))-> Strings[3])
  tcdell = charcost(((struct termtype *)(cur_term))-> Strings[3]) * 2 +
      charcost(scroll_fwd);
 else if (((struct termtype *)(cur_term))-> Strings[106])
  tcdell = charcost(((struct termtype *)(cur_term))-> Strings[106]);
 else if (((struct termtype *)(cur_term))-> Strings[22])
  tcdell = charcost(((struct termtype *)(cur_term))-> Strings[22]);
 else
  tcdell = nrow * ncol;
 insdel = (((struct termtype *)(cur_term))-> Strings[53] || ((struct termtype *)(cur_term))-> Strings[110]) &&
     (((struct termtype *)(cur_term))-> Strings[22] || ((struct termtype *)(cur_term))-> Strings[106]);
 if (((struct termtype *)(cur_term))-> Strings[28])
  tputs(((struct termtype *)(cur_term))-> Strings[28], 1, ttputc);
 ttresize();
}

variable_types: errret int


ttreinit

decompiled_code:
__int64 ttreinit()
{
const char *v0; // rdi
const char *v1; // rdi

if ( (unsigned int)fchecktime(curbp) != 1 )
*(_BYTE *)(curbp + 91LL) |= 0x20u;
v0 = (const char *)*((_QWORD *)cur_term->type.Strings + 28);
if ( v0 )
tputs(v0, 1, (int (*)(int))&ttputc);
v1 = (const char *)*((_QWORD *)cur_term->type.Strings + 89);
if ( v1 )
tputs(v1, 1, (int (*)(int))&ttputc);
return ttresize();
}

canonical_decompiled_code:
__int64 func15()
{
const char *v0; // rdi
const char *v1; // rdi

if ( (unsigned int)fchecktime(curbp) != 1 )
*(_BYTE *)(curbp + 91LL) |= 0x20u;
v0 = (const char *)*((_QWORD *)cur_term->type.Strings + 28);
if ( v0 )
tputs(v0, 1, (int (*)(int))&ttputc);
v1 = (const char *)*((_QWORD *)cur_term->type.Strings + 89);
if ( v1 )
tputs(v1, 1, (int (*)(int))&ttputc);
return func13();
}

original_code:
void
ttreinit(void)
{
 if (fchecktime(curbp) != 1) {
  curbp->b_flag |= 0x20;
 }
 if (((TERMTYPE *)(cur_term))-> Strings[28])
  tputs(((TERMTYPE *)(cur_term))-> Strings[28], 1, ttputc);
 if (((TERMTYPE *)(cur_term))-> Strings[89])
  tputs(((TERMTYPE *)(cur_term))-> Strings[89], 1, ttputc);
 ttresize();
}

canonical_original_code:
void ttreinit()
{
 if (fchecktime(curbp) != 1) {
  curbp->b_flag |= 0x20;
 }
 if (((struct termtype *)(cur_term))-> Strings[28])
  tputs(((struct termtype *)(cur_term))-> Strings[28], 1, ttputc);
 if (((struct termtype *)(cur_term))-> Strings[89])
  tputs(((struct termtype *)(cur_term))-> Strings[89], 1, ttputc);
 ttresize();
}


decompiled_code:
__int64 __fastcall find_autoexec(char *name)
{
__int64 v1; // rbx
int v2; // ebp
int v3; // er14
__int64 v4; // r12

if ( !ready )
return 0LL;
v1 = autos;
if ( !autos )
return autos;
v2 = 0;
v3 = 0;
v4 = 0LL;
do
{
if ( !fnmatch(*(const char **)(v1 + 8), name, 0) )
{
if ( v3 <= v2 )
{
v4 = reallocarray(v4, v3 + 9, 8LL);
if ( !v4 )
panic("out of memory");
v3 += 8;
}
*(_QWORD *)(v4 + 8LL * v2++) = *(_QWORD *)(v1 + 16);
}
v1 = *(_QWORD *)v1;
}
while ( v1 );
if ( v2 )
*(_QWORD *)(v4 + 8LL * v2) = 0LL;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *name)
{
__int64 v1; // rbx
int v2; // ebp
int v3; // er14
__int64 v4; // r12

if ( !ready )
return 0LL;
v1 = autos;
if ( !autos )
return autos;
v2 = 0;
v3 = 0;
v4 = 0LL;
do
{
if ( !fnmatch(*(const char **)(v1 + 8), name, 0) )
{
if ( v3 <= v2 )
{
v4 = reallocarray(v4, v3 + 9, 8LL);
if ( !v4 )
panic("out of memory");
v3 += 8;
}
*(_QWORD *)(v4 + 8LL * v2++) = *(_QWORD *)(v1 + 16);
}
v1 = *(_QWORD *)v1;
}
while ( v1 );
if ( v2 )
*(_QWORD *)(v4 + 8LL * v2) = 0LL;
return v4;
}

original_code:
PF *
find_autoexec(const char *fname)
{
 PF *pfl, *npfl;
 int have, used;
 struct autoexec *ae;
 if (!ready)
  return (((void *)0));
 pfl = ((void *)0);
 have = 0;
 used = 0;
 for((ae) = ((&autos)->slh_first); (ae) != ((void *)0); (ae) = ((ae)->next.sle_next)) {
  if (fnmatch(ae->pattern, fname, 0) == 0) {
   if (used >= have) {
    npfl = reallocarray(pfl, have + 8 + 1,
        sizeof(PF));
    if (npfl == ((void *)0))
     panic("out of memory");
    pfl = npfl;
    have += 8;
   }
   pfl[used++] = ae->fp;
  }
 }
 if (used)
  pfl[used] = ((void *)0);
 return (pfl);
}

canonical_original_code:
int (**find_autoexec(char *fname))(int, int)
{
 int (**pfl)(int, int); int (**npfl)(int, int);
 int have; int used;
 struct autoexec *ae;
 if (!ready)
  return (((void *)0));
 pfl = ((void *)0);
 have = 0;
 used = 0;
 for((ae) = ((&autos)->slh_first); (ae) != ((void *)0); (ae) = ((ae)->next.sle_next)) {
  if (fnmatch(ae->pattern, fname, 0) == 0) {
   if (used >= have) {
    npfl = reallocarray(pfl, have + 8 + 1,
        sizeof(PF));
    if (npfl == ((void *)0))
     panic("out of memory");
    pfl = npfl;
    have += 8;
   }
   pfl[used++] = ae->fp;
  }
 }
 if (used)
  pfl[used] = ((void *)0);
 return (pfl);
}

variable_types:
fname
char *

variable_types:
pfl
int (**)(int, int)

variable_types:
npfl
int (**)(int, int)

variable_types: have int

variable_types: used int

variable_types: struct autoexec { struct { struct autoexec *sle_next; } next; char *pattern; int (*fp)(int, int); } *

return_type: int (**)(int, int)


decompiled_code:
__int64 __fastcall add_autoexec(char *s, __int64 a2)
{
__int64 v2; // rbp
__int64 result; // rax
_QWORD *v4; // rax
_QWORD *v5; // rbx
char *v6; // rax

if ( !ready )
{
autos = 0LL;
ready = 1;
}
v2 = name_function(a2);
result = 0LL;
if ( v2 )
{
v4 = malloc(0x18uLL);
v5 = v4;
if ( v4 )
{
v4[2] = v2;
v6 = strdup(s);
v5[1] = v6;
if ( v6 )
{
*v5 = autos;
autos = (__int64)v5;
result = 1LL;
}
else
{
free(v5);
result = 0LL;
}
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s, __int64 a2)
{
__int64 v2; // rbp
__int64 result; // rax
_QWORD *v4; // rax
_QWORD *v5; // rbx
char *v6; // rax

if ( !ready )
{
autos = 0LL;
ready = 1;
}
v2 = name_function(a2);
result = 0LL;
if ( v2 )
{
v4 = malloc(0x18uLL);
v5 = v4;
if ( v4 )
{
v4[2] = v2;
v6 = strdup(s);
v5[1] = v6;
if ( v6 )
{
*v5 = autos;
autos = (__int64)v5;
result = 1LL;
}
else
{
free(v5);
result = 0LL;
}
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int
add_autoexec(const char *pattern, const char *func)
{
 PF fp;
 struct autoexec *ae;
 if (!ready) {
  { ((&autos)->slh_first) = ((void *)0); };
  ready = 1;
 }
 fp = name_function(func);
 if (fp == ((void *)0))
  return (0);
 ae = malloc(sizeof(*ae));
 if (ae == ((void *)0))
  return (0);
 ae->fp = fp;
 ae->pattern = strdup(pattern);
 if (ae->pattern == ((void *)0)) {
  free(ae);
  return (0);
 }
 do { (ae)->next.sle_next = (&autos)->slh_first; (&autos)->slh_first = (ae); } while (0);
 return (1);
}

canonical_original_code:
int add_autoexec(char *pattern, char *func)
{
 int (*fp)(int, int);
 struct autoexec *ae;
 if (!ready) {
  { ((&autos)->slh_first) = ((void *)0); };
  ready = 1;
 }
 fp = name_function(func);
 if (fp == ((void *)0))
  return (0);
 ae = malloc(sizeof(*ae));
 if (ae == ((void *)0))
  return (0);
 ae->fp = fp;
 ae->pattern = strdup(pattern);
 if (ae->pattern == ((void *)0)) {
  free(ae);
  return (0);
 }
 do { (ae)->next.sle_next = (&autos)->slh_first; (&autos)->slh_first = (ae); } while (0);
 return (1);
}

variable_types:
pattern
char *

variable_types:
func
char *

variable_types:
fp
int (*)(int, int)

return_type: int

user_defined_types: struct autoexec { struct { struct autoexec *sle_next; } next; char *pattern; int (*fp)(int, int); }


decompiled_code:
__int64 auto_execute()
{
__int64 v0; // rax
char *v1; // rbx
__int64 result; // rax
_BYTE *v3; // rsi
__int64 v4; // [rsp+0h] [rbp-108h] BYREF
char v5[136]; // [rsp+80h] [rbp-88h] BYREF

v0 = eread("Filename pattern: ", v5, 128LL, 24LL);
if ( !v0 )
return 2LL;
v1 = (char *)v0;
result = 0LL;
if ( *v1 )
{
v3 = (_BYTE *)eread("Execute: ", &v4, 128LL, 25LL);
if ( v3 )
{
result = 0LL;
if ( *v3 )
result = add_autoexec(v1, (__int64)v3);
}
else
{
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
char *v1; // rbx
__int64 result; // rax
_BYTE *v3; // rsi
__int64 v4; // [rsp+0h] [rbp-108h] BYREF
char v5[136]; // [rsp+80h] [rbp-88h] BYREF

v0 = eread("Filename pattern: ", v5, 128LL, 24LL);
if ( !v0 )
return 2LL;
v1 = (char *)v0;
result = 0LL;
if ( *v1 )
{
v3 = (_BYTE *)eread("Execute: ", &v4, 128LL, 25LL);
if ( v3 )
{
result = 0LL;
if ( *v3 )
result = func1(v1, (__int64)v3);
}
else
{
result = 2LL;
}
}
return result;
}

original_code:
int
auto_execute(int f, int n)
{
 char patbuf[128], funcbuf[128], *patp, *funcp;
 int s;
 if ((patp = eread("Filename pattern: ", patbuf, sizeof(patbuf),
     0x0008 | 0x0010)) == ((void *)0))
  return (2);
 else if (patp[0] == '\0')
  return (0);
 if ((funcp = eread("Execute: ", funcbuf, sizeof(funcbuf),
     0x0008 | 0x0010 | 0x0001)) == ((void *)0))
  return (2);
 else if (funcp[0] == '\0')
  return (0);
 if ((s = add_autoexec(patp, funcp)) != 1)
  return (s);
 return (1);
}

canonical_original_code:
int auto_execute(int f, int n)
{
 char patbuf[128]; char funcbuf[128]; char *patp; char *funcp;
 int s;
 if ((patp = eread("Filename pattern: ", patbuf, sizeof(patbuf),
     0x0008 | 0x0010)) == ((void *)0))
  return (2);
 else if (patp[0] == '\0')
  return (0);
 if ((funcp = eread("Execute: ", funcbuf, sizeof(funcbuf),
     0x0008 | 0x0010 | 0x0001)) == ((void *)0))
  return (2);
 else if (funcp[0] == '\0')
  return (0);
 if ((s = add_autoexec(patp, funcp)) != 1)
  return (s);
 return (1);
}

variable_types: f int

variable_types: n int

variable_types:
patbuf
char [128]

variable_types:
funcbuf
char [128]

variable_types:
patp
char *

variable_types:
funcp
char *

variable_types: s int


dirinit

decompiled_code:
int dirinit()
{
char *v0; // rax

mgcwd = 0;
v0 = getcwd(&mgcwd, 0x400uLL);
if ( !v0 )
{
ewprintf("Can't get current directory!");
LODWORD(v0) = chdir("/");
}
if ( mgcwd != 47 || byte_401 )
LODWORD(v0) = strlcat(&mgcwd, "/", 1024LL);
return (int)v0;
}

canonical_decompiled_code:
int func0()
{
char *v0; // rax

mgcwd = 0;
v0 = getcwd(&mgcwd, 0x400uLL);
if ( !v0 )
{
ewprintf("Can't get current directory!");
LODWORD(v0) = chdir("/");
}
if ( mgcwd != 47 || byte_401 )
LODWORD(v0) = strlcat(&mgcwd, "/", 1024LL);
return (int)v0;
}

original_code:
void
dirinit(void)
{
 mgcwd[0] = '\0';
 if (getcwd(mgcwd, sizeof(mgcwd)) == ((void *)0)) {
  ewprintf("Can't get current directory!");
  chdir("/");
 }
 if (!(mgcwd[0] == '/' && mgcwd [1] == '\0'))
  (void)strlcat(mgcwd, "/", sizeof(mgcwd));
}

canonical_original_code:
void dirinit()
{
 mgcwd[0] = '\0';
 if (getcwd(mgcwd, sizeof(mgcwd)) == ((void *)0)) {
  ewprintf("Can't get current directory!");
  chdir("/");
 }
 if (!(mgcwd[0] == '/' && mgcwd [1] == '\0'))
  (void)strlcat(mgcwd, "/", sizeof(mgcwd));
}


changedir

decompiled_code:
__int64 changedir()
{
_BYTE *v0; // rax
unsigned int v1; // edx
char *v3; // rbx
char v4[1032]; // [rsp+0h] [rbp-408h] BYREF

strlcpy(v4, &mgcwd, 1024LL);
v0 = (_BYTE *)eread("Change default directory: ", v4, 1024LL, 60LL);
if ( !v0 )
return 2;
v1 = 0;
if ( *v0 )
{
if ( chdir(v4) == -1 )
{
dobeep();
ewprintf("Can't change dir to %s", v4);
v1 = 0;
}
else
{
v3 = getcwd(&mgcwd, 0x400uLL);
if ( !v3 )
panic("Can't get current directory!");
if ( byte_3FF[strlen(&mgcwd)] != 47 )
strlcat(&mgcwd, "/", 1024LL);
ewprintf("Current directory is now %s", v3);
v1 = 1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 func1()
{
_BYTE *v0; // rax
unsigned int v1; // edx
char *v3; // rbx
char v4[1032]; // [rsp+0h] [rbp-408h] BYREF

strlcpy(v4, &mgcwd, 1024LL);
v0 = (_BYTE *)eread("Change default directory: ", v4, 1024LL, 60LL);
if ( !v0 )
return 2;
v1 = 0;
if ( *v0 )
{
if ( chdir(v4) == -1 )
{
dobeep();
ewprintf("Can't change dir to %s", v4);
v1 = 0;
}
else
{
v3 = getcwd(&mgcwd, 0x400uLL);
if ( !v3 )
panic("Can't get current directory!");
if ( byte_3FF[strlen(&mgcwd)] != 47 )
strlcat(&mgcwd, "/", 1024LL);
ewprintf("Current directory is now %s", v3);
v1 = 1;
}
}
return v1;
}

original_code:
int
changedir(int f, int n)
{
 char bufc[1024], *bufp;
 (void)strlcpy(bufc, mgcwd, sizeof(bufc));
 if ((bufp = eread("Change default directory: ", bufc, 1024,
     0x0020 | 0x0008 | 0x0010 | 0x0004)) == ((void *)0))
  return (2);
 else if (bufp[0] == '\0')
  return (0);
 if (chdir(bufc) == -1) {
  dobeep();
  ewprintf("Can't change dir to %s", bufc);
  return (0);
 }
 if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == ((void *)0))
  panic("Can't get current directory!");
 if (mgcwd[strlen(mgcwd) - 1] != '/')
  (void)strlcat(mgcwd, "/", sizeof(mgcwd));
 ewprintf("Current directory is now %s", bufp);
 return (1);
}

canonical_original_code:
int changedir(int f, int n)
{
 char bufc[1024]; char *bufp;
 (void)strlcpy(bufc, mgcwd, sizeof(bufc));
 if ((bufp = eread("Change default directory: ", bufc, 1024,
     0x0020 | 0x0008 | 0x0010 | 0x0004)) == ((void *)0))
  return (2);
 else if (bufp[0] == '\0')
  return (0);
 if (chdir(bufc) == -1) {
  dobeep();
  ewprintf("Can't change dir to %s", bufc);
  return (0);
 }
 if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == ((void *)0))
  panic("Can't get current directory!");
 if (mgcwd[strlen(mgcwd) - 1] != '/')
  (void)strlcat(mgcwd, "/", sizeof(mgcwd));
 ewprintf("Current directory is now %s", bufp);
 return (1);
}

variable_types:
bufc
char [1024]

variable_types:
bufp
char *


showcwdir

decompiled_code:
__int64 showcwdir()
{
ewprintf("Current directory: %s", &mgcwd);
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
ewprintf("Current directory: %s", &mgcwd);
return 1LL;
}

original_code:
int
showcwdir(int f, int n)
{
 ewprintf("Current directory: %s", mgcwd);
 return (1);
}


getcwdir

decompiled_code:
_BOOL8 __fastcall getcwdir(__int64 a1, unsigned __int64 a2)
{
return strlcpy(a1, &mgcwd, a2) < a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1, unsigned __int64 a2)
{
return strlcpy(a1, &mgcwd, a2) < a2;
}

original_code:
int
getcwdir(char *buf, size_t len)
{
 if (strlcpy(buf, mgcwd, len) >= len)
  return (0);
 return (1);
}

canonical_original_code:
int getcwdir(char *buf, unsigned long len)
{
 if (strlcpy(buf, mgcwd, len) >= len)
  return (0);
 return (1);
}

variable_types:
buf
char *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall do_makedir(__int64 a1)
{
const char *v1; // rax
const char *v2; // r13
const char *v3; // rax
int v4; // er15
char *i; // rbx
const char *v7; // rbx
char v8; // r12
int v9; // ebp
__mode_t mask; // [rsp+Ch] [rbp-CCh]
struct stat v11; // [rsp+10h] [rbp-C8h] BYREF

v1 = (const char *)adjustname(a1, 1LL);
if ( v1 )
{
v2 = v1;
v3 = &v1[strlen(v1) - 1];
if ( v2 < v3 )
{
do
{
if ( *v3 != 47 )
break;
*v3-- = 0;
}
while ( v2 != v3 );
}
mask = umask(0);
v4 = ~(_WORD)mask & 0x1FF;
LOBYTE(v4) = ~(_BYTE)mask | 0xC0;
for ( i = (char *)v2; ; *i = 47 )
{
while ( 1 )
{
v7 = &i[strspn(i, "/")];
i = (char *)&v7[strcspn(v7, "/")];
v8 = *i;
*i = 0;
v9 = stat(v2, &v11);
if ( !v8 && !v9 )
{
dobeep();
ewprintf("Cannot create directory %s: file exists", v2);
return 0LL;
}
if ( v8 && !v9 )
break;
if ( v8 )
{
if ( !mkdir(v2, v4) )
goto LABEL_13;
}
else if ( !mkdir(v2, ~(_WORD)mask & 0x1FF) )
{
goto LABEL_24;
}
if ( v9 )
{
dobeep();
ewprintf("Creating directory: permission denied, %s", v2);
LABEL_22:
umask(mask);
return 0LL;
}
LABEL_11:
if ( (v11.st_mode & 0xF000) != 0x4000 )
{
eerase();
goto LABEL_22;
}
if ( !v8 )
{
LABEL_24:
eerase();
umask(mask);
return 1LL;
}
LABEL_13:
*i = 47;
}
if ( (v11.st_mode & 0xF000) != 0x4000 )
{
if ( !mkdir(v2, v4) )
goto LABEL_13;
goto LABEL_11;
}
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
const char *v1; // rax
const char *v2; // r13
const char *v3; // rax
int v4; // er15
char *i; // rbx
const char *v7; // rbx
char v8; // r12
int v9; // ebp
__mode_t mask; // [rsp+Ch] [rbp-CCh]
struct stat v11; // [rsp+10h] [rbp-C8h] BYREF

v1 = (const char *)adjustname(a1, 1LL);
if ( v1 )
{
v2 = v1;
v3 = &v1[strlen(v1) - 1];
if ( v2 < v3 )
{
do
{
if ( *v3 != 47 )
break;
*v3-- = 0;
}
while ( v2 != v3 );
}
mask = umask(0);
v4 = ~(_WORD)mask & 0x1FF;
LOBYTE(v4) = ~(_BYTE)mask | 0xC0;
for ( i = (char *)v2; ; *i = 47 )
{
while ( 1 )
{
v7 = &i[strspn(i, "/")];
i = (char *)&v7[strcspn(v7, "/")];
v8 = *i;
*i = 0;
v9 = stat(v2, &v11);
if ( !v8 && !v9 )
{
dobeep();
ewprintf("Cannot create directory %s: file exists", v2);
return 0LL;
}
if ( v8 && !v9 )
break;
if ( v8 )
{
if ( !mkdir(v2, v4) )
goto LABEL_13;
}
else if ( !mkdir(v2, ~(_WORD)mask & 0x1FF) )
{
goto LABEL_24;
}
if ( v9 )
{
dobeep();
ewprintf("Creating directory: permission denied, %s", v2);
LABEL_22:
umask(mask);
return 0LL;
}
LABEL_11:
if ( (v11.st_mode & 0xF000) != 0x4000 )
{
eerase();
goto LABEL_22;
}
if ( !v8 )
{
LABEL_24:
eerase();
umask(mask);
return 1LL;
}
LABEL_13:
*i = 47;
}
if ( (v11.st_mode & 0xF000) != 0x4000 )
{
if ( !mkdir(v2, v4) )
goto LABEL_13;
goto LABEL_11;
}
}
}
return 0LL;
}

original_code:
int
do_makedir(char *path)
{
 struct stat sb;
 int finished, ishere;
 mode_t dir_mode, mode, oumask;
 char *slash;
 if ((path = adjustname(path, 1)) == ((void *)0))
  return (0);
 slash = strrchr(path, '\0');
 while (--slash > path && *slash == '/')
  *slash = '\0';
 slash = path;
 oumask = umask(0);
 mode = 0777 & ~oumask;
 dir_mode = mode | 0200 | 0100;
 for (;;) {
  slash += strspn(slash, "/");
  slash += strcspn(slash, "/");
  finished = (*slash == '\0');
  *slash = '\0';
  ishere = !stat(path, &sb);
  if (finished && ishere) {
   dobeep();
   ewprintf("Cannot create directory %s: file exists",
        path);
   return(0);
  } else if (!finished && ishere && ((((sb.st_mode)) & 0170000) == (0040000))) {
   *slash = '/';
   continue;
  }
  if (mkdir(path, finished ? mode : dir_mode) == 0) {
   if (mode > 0777 && chmod(path, mode) < 0) {
    umask(oumask);
    return (2);
   }
  } else {
   if (!ishere || !((((sb.st_mode)) & 0170000) == (0040000))) {
    if (!ishere) {
     dobeep();
     ewprintf("Creating directory: "
         "permission denied, %s", path);
    } else
     eerase();
    umask(oumask);
    return (0);
   }
  }
  if (finished)
   break;
  *slash = '/';
 }
 eerase();
 umask(oumask);
 return (1);
}

canonical_original_code:
int do_makedir(char *path)
{
 struct stat sb;
 int finished; int ishere;
 unsigned int dir_mode; unsigned int mode; unsigned int oumask;
 char *slash;
 if ((path = adjustname(path, 1)) == ((void *)0))
  return (0);
 slash = strrchr(path, '\0');
 while (--slash > path && *slash == '/')
  *slash = '\0';
 slash = path;
 oumask = umask(0);
 mode = 0777 & ~oumask;
 dir_mode = mode | 0200 | 0100;
 for (;;) {
  slash += strspn(slash, "/");
  slash += strcspn(slash, "/");
  finished = (*slash == '\0');
  *slash = '\0';
  ishere = !stat(path, &sb);
  if (finished && ishere) {
   dobeep();
   ewprintf("Cannot create directory %s: file exists",
        path);
   return(0);
  } else if (!finished && ishere && ((((sb.st_mode)) & 0170000) == (0040000))) {
   *slash = '/';
   continue;
  }
  if (mkdir(path, finished ? mode : dir_mode) == 0) {
   if (mode > 0777 && chmod(path, mode) < 0) {
    umask(oumask);
    return (2);
   }
  } else {
   if (!ishere || !((((sb.st_mode)) & 0170000) == (0040000))) {
    if (!ishere) {
     dobeep();
     ewprintf("Creating directory: "
         "permission denied, %s", path);
    } else
     eerase();
    umask(oumask);
    return (0);
   }
  }
  if (finished)
   break;
  *slash = '/';
 }
 eerase();
 umask(oumask);
 return (1);
}

variable_types:
path
char *

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

variable_types: finished int

variable_types: ishere int

variable_types:
mode
unsigned int

variable_types:
oumask
unsigned int

variable_types:
slash
char *

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


decompiled_code:
__int64 ask_makedir()
{
int v0; // edx
__int64 result; // rax
_BYTE *v2; // rdi
_BYTE v3[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = getbufcwd(v3, 1024LL);
result = 2LL;
if ( v0 == 1 )
{
v2 = (_BYTE *)eread("Make directory: ", v3, 1024LL, 60LL);
if ( v2 )
{
result = 0LL;
if ( *v2 )
result = do_makedir((__int64)v2);
}
else
{
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // edx
__int64 result; // rax
_BYTE *v2; // rdi
_BYTE v3[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = getbufcwd(v3, 1024LL);
result = 2LL;
if ( v0 == 1 )
{
v2 = (_BYTE *)eread("Make directory: ", v3, 1024LL, 60LL);
if ( v2 )
{
result = 0LL;
if ( *v2 )
result = func4((__int64)v2);
}
else
{
result = 2LL;
}
}
return result;
}

original_code:
int
ask_makedir(void)
{
 char bufc[1024];
 char *path;
 if (getbufcwd(bufc, sizeof(bufc)) != 1)
  return (2);
 if ((path = eread("Make directory: ", bufc, 1024,
     0x0020 | 0x0008 | 0x0010 | 0x0004)) == ((void *)0))
  return (2);
 else if (path[0] == '\0')
  return (0);
 return (do_makedir(path));
}

canonical_original_code:
int ask_makedir()
{
 char bufc[1024];
 char *path;
 if (getbufcwd(bufc, sizeof(bufc)) != 1)
  return (2);
 if ((path = eread("Make directory: ", bufc, 1024,
     0x0020 | 0x0008 | 0x0010 | 0x0004)) == ((void *)0))
  return (2);
 else if (path[0] == '\0')
  return (0);
 return (do_makedir(path));
}


makedir

decompiled_code:
__int64 makedir()
{
return ask_makedir();
}

canonical_decompiled_code:
__int64 func6()
{
return func5();
}

original_code:
int
makedir(int f, int n)
{
 return (ask_makedir());
}

variable_types: f int

variable_types: n int


changemode

original_code:
int
changemode(int f, int n, char *mode)
{
 int i;
 struct maps_s *m;
 if ((m = name_mode(mode)) == ((void *)0)) {
  dobeep();
  ewprintf("Can't find mode %s", mode);
  return (0);
 }
 if (!(f & 7)) {
  for (i = 0; i <= curbp->b_nmodes; i++)
   if (curbp->b_modes[i] == m) {
    n = 0;
    break;
   }
 }
 if (n > 0) {
  for (i = 0; i <= curbp->b_nmodes; i++)
   if (curbp->b_modes[i] == m)
    return (1);
  if (curbp->b_nmodes >= 4 - 1) {
   dobeep();
   ewprintf("Too many modes");
   return (0);
  }
  curbp->b_modes[++(curbp->b_nmodes)] = m;
 } else {
  for (i = 1; i <= curbp->b_nmodes && m != curbp->b_modes[i];
      i++);
  if (i > curbp->b_nmodes)
   return (1);
  for (; i < curbp->b_nmodes; i++)
   curbp->b_modes[i] = curbp->b_modes[i + 1];
  curbp->b_nmodes--;
 }
 upmodes(curbp);
 return (1);
}

variable_types:
mode
char *

variable_types: i int

variable_types: struct maps_s { struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; } map_element[1]; } *p_map; char *p_name; struct maps_s *p_next; } *

user_defined_types: struct maps_s { struct keymap_s *p_map; char *p_name; struct maps_s *p_next; }

user_defined_types: struct keymap_s { short map_num; short map_max; int (*map_default)(int, int); struct map_element map_element[1]; }


indentmode

decompiled_code:
__int64 __fastcall indentmode(char a1, int a2)
{
return changemode(a1, a2, "indent");
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, int a2)
{
return func0(a1, a2, "indent");
}

original_code:
int
indentmode(int f, int n)
{
 return (changemode(f, n, "indent"));
}


fillmode

decompiled_code:
__int64 __fastcall fillmode(char a1, int a2)
{
return changemode(a1, a2, "fill");
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, int a2)
{
return func0(a1, a2, "fill");
}

original_code:
int
fillmode(int f, int n)
{
 return (changemode(f, n, "fill"));
}


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

result = changemode(a1, a2, "overwrite");
if ( (_DWORD)result )
{
if ( (a1 & 7) != 0 )
{
if ( a2 <= 0 )
*(_BYTE *)(curbp + 91LL) &= 0xF7u;
else
*(_BYTE *)(curbp + 91LL) |= 8u;
result = 1LL;
}
else
{
*(_BYTE *)(curbp + 91LL) ^= 8u;
result = 1LL;
}
}
return result;
}

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

result = func0(a1, a2, "overwrite");
if ( (_DWORD)result )
{
if ( (a1 & 7) != 0 )
{
if ( a2 <= 0 )
*(_BYTE *)(curbp + 91LL) &= 0xF7u;
else
*(_BYTE *)(curbp + 91LL) |= 8u;
result = 1LL;
}
else
{
*(_BYTE *)(curbp + 91LL) ^= 8u;
result = 1LL;
}
}
return result;
}

original_code:
int
overwrite_mode(int f, int n)
{
 if (changemode(f, n, "overwrite") == 0)
  return (0);
 if (f & 7) {
  if (n <= 0)
   curbp->b_flag &= ~0x08;
  else
   curbp->b_flag |= 0x08;
 } else
  curbp->b_flag ^= 0x08;
 return (1);
}


decompiled_code:
__int64 __fastcall set_default_mode(char a1, int a2)
{
_BYTE *v2; // rax
unsigned int v3; // edx
void *v5; // rdx
int v6; // ecx
__int64 v7; // rax
int v8; // esi
__int64 v9; // rax
__int64 v10; // rcx
__int64 v11; // rax
char v12[56]; // [rsp+0h] [rbp-38h] BYREF

v2 = (_BYTE *)eread("Set Default Mode: ", v12, 32LL, 8LL);
if ( !v2 )
return 2;
v3 = 0;
if ( *v2 )
{
v5 = (void *)name_mode(v12);
if ( !v5 )
{
dobeep(v12, v12, 0LL);
ewprintf("can't find mode %s", v12);
return 0;
}
if ( (a1 & 7) != 0 )
{
if ( a2 <= 0 )
{
LABEL_11:
v8 = defb_nmodes;
v9 = 1LL;
LODWORD(v10) = 1;
if ( defb_nmodes > 0 )
{
while ( 1 )
{
LODWORD(v10) = v9;
if ( *(&defb_modes + v9) == v5 )
break;
if ( defb_nmodes < (int)++v9 )
return 1;
}
}
goto LABEL_30;
}
v6 = defb_nmodes;
if ( defb_nmodes >= 0 )
{
LABEL_18:
v11 = 0LL;
while ( *(&defb_modes + v11) != v5 )
{
if ( v6 < (int)++v11 )
goto LABEL_21;
}
return 1;
}
LABEL_21:
if ( v6 > 2 )
{
dobeep(v12, v12, v5);
ewprintf("Too many modes");
return 0;
}
}
else
{
v6 = defb_nmodes;
if ( defb_nmodes >= 0 )
{
v7 = 0LL;
while ( *(&defb_modes + v7) != v5 )
{
if ( defb_nmodes < (int)++v7 )
{
if ( a2 > 0 )
goto LABEL_18;
goto LABEL_11;
}
}
goto LABEL_11;
}
if ( a2 <= 0 )
{
v8 = defb_nmodes;
LODWORD(v10) = 1;
LABEL_30:
if ( (int)v10 > v8 )
return 1;
if ( (int)v10 < v8 )
{
v10 = (int)v10;
do
{
*(&defb_modes + v10) = *(&defb_modes + v10 + 1);
++v10;
}
while ( v8 > (int)v10 );
}
defb_nmodes = v8 - 1;
v3 = 1;
if ( !strcmp(v12, "overwrite") )
defb_flag &= 0xFFFFFFF7;
return v3;
}
}
defb_nmodes = v6 + 1;
*(&defb_modes + v6 + 1) = v5;
v3 = 1;
if ( !strcmp(v12, "overwrite") )
{
defb_flag |= 8u;
v3 = 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1, int a2)
{
_BYTE *v2; // rax
unsigned int v3; // edx
void *v5; // rdx
int v6; // ecx
__int64 v7; // rax
int v8; // esi
__int64 v9; // rax
__int64 v10; // rcx
__int64 v11; // rax
char v12[56]; // [rsp+0h] [rbp-38h] BYREF

v2 = (_BYTE *)eread("Set Default Mode: ", v12, 32LL, 8LL);
if ( !v2 )
return 2;
v3 = 0;
if ( *v2 )
{
v5 = (void *)name_mode(v12);
if ( !v5 )
{
dobeep(v12, v12, 0LL);
ewprintf("can't find mode %s", v12);
return 0;
}
if ( (a1 & 7) != 0 )
{
if ( a2 <= 0 )
{
LABEL_11:
v8 = defb_nmodes;
v9 = 1LL;
LODWORD(v10) = 1;
if ( defb_nmodes > 0 )
{
while ( 1 )
{
LODWORD(v10) = v9;
if ( *(&defb_modes + v9) == v5 )
break;
if ( defb_nmodes < (int)++v9 )
return 1;
}
}
goto LABEL_30;
}
v6 = defb_nmodes;
if ( defb_nmodes >= 0 )
{
LABEL_18:
v11 = 0LL;
while ( *(&defb_modes + v11) != v5 )
{
if ( v6 < (int)++v11 )
goto LABEL_21;
}
return 1;
}
LABEL_21:
if ( v6 > 2 )
{
dobeep(v12, v12, v5);
ewprintf("Too many modes");
return 0;
}
}
else
{
v6 = defb_nmodes;
if ( defb_nmodes >= 0 )
{
v7 = 0LL;
while ( *(&defb_modes + v7) != v5 )
{
if ( defb_nmodes < (int)++v7 )
{
if ( a2 > 0 )
goto LABEL_18;
goto LABEL_11;
}
}
goto LABEL_11;
}
if ( a2 <= 0 )
{
v8 = defb_nmodes;
LODWORD(v10) = 1;
LABEL_30:
if ( (int)v10 > v8 )
return 1;
if ( (int)v10 < v8 )
{
v10 = (int)v10;
do
{
*(&defb_modes + v10) = *(&defb_modes + v10 + 1);
++v10;
}
while ( v8 > (int)v10 );
}
defb_nmodes = v8 - 1;
v3 = 1;
if ( !strcmp(v12, "overwrite") )
defb_flag &= 0xFFFFFFF7;
return v3;
}
}
defb_nmodes = v6 + 1;
*(&defb_modes + v6 + 1) = v5;
v3 = 1;
if ( !strcmp(v12, "overwrite") )
{
defb_flag |= 8u;
v3 = 1;
}
}
return v3;
}

original_code:
int
set_default_mode(int f, int n)
{
 int i;
 struct maps_s *m;
 char modebuf[32], *bufp;
 if ((bufp = eread("Set Default Mode: ", modebuf, sizeof(modebuf),
     0x0008)) == ((void *)0))
  return (2);
 else if (bufp[0] == '\0')
  return (0);
 if ((m = name_mode(modebuf)) == ((void *)0)) {
  dobeep();
  ewprintf("can't find mode %s", modebuf);
  return (0);
 }
 if (!(f & 7)) {
  for (i = 0; i <= defb_nmodes; i++)
   if (defb_modes[i] == m) {
    n = 0;
    break;
   }
 }
 if (n > 0) {
  for (i = 0; i <= defb_nmodes; i++)
   if (defb_modes[i] == m)
    return (1);
  if (defb_nmodes >= 4 - 1) {
   dobeep();
   ewprintf("Too many modes");
   return (0);
  }
  defb_modes[++defb_nmodes] = m;
 } else {
  for (i = 1; i <= defb_nmodes && m != defb_modes[i]; i++);
  if (i > defb_nmodes)
   return (1);
  for (; i < defb_nmodes; i++)
   defb_modes[i] = defb_modes[i + 1];
  defb_nmodes--;
 }
 if (strcmp(modebuf, "overwrite") == 0) {
  if (n <= 0)
   defb_flag &= ~0x08;
  else
   defb_flag |= 0x08;
 }
 return (1);
}

canonical_original_code:
int set_default_mode(int f, int n)
{
 int i;
 struct maps_s *m;
 char modebuf[32]; char *bufp;
 if ((bufp = eread("Set Default Mode: ", modebuf, sizeof(modebuf),
     0x0008)) == ((void *)0))
  return (2);
 else if (bufp[0] == '\0')
  return (0);
 if ((m = name_mode(modebuf)) == ((void *)0)) {
  dobeep();
  ewprintf("can't find mode %s", modebuf);
  return (0);
 }
 if (!(f & 7)) {
  for (i = 0; i <= defb_nmodes; i++)
   if (defb_modes[i] == m) {
    n = 0;
    break;
   }
 }
 if (n > 0) {
  for (i = 0; i <= defb_nmodes; i++)
   if (defb_modes[i] == m)
    return (1);
  if (defb_nmodes >= 4 - 1) {
   dobeep();
   ewprintf("Too many modes");
   return (0);
  }
  defb_modes[++defb_nmodes] = m;
 } else {
  for (i = 1; i <= defb_nmodes && m != defb_modes[i]; i++);
  if (i > defb_nmodes)
   return (1);
  for (; i < defb_nmodes; i++)
   defb_modes[i] = defb_modes[i + 1];
  defb_nmodes--;
 }
 if (strcmp(modebuf, "overwrite") == 0) {
  if (n <= 0)
   defb_flag &= ~0x08;
  else
   defb_flag |= 0x08;
 }
 return (1);
}

variable_types:
modebuf
char [32]

user_defined_types: struct map_element { short k_base; short k_num; int (**k_funcp)(int, int); struct keymap_s *k_prefmap; }


showcpos

original_code:
int
showcpos(int f, int n)
{
 struct line *clp;
 long nchar, cchar;
 int nline, row;
 int cline, cbyte;
 int ratio;
 clp = ((((curbp)->b_headp)->l_fp));
 cchar = 0;
 cline = 0;
 cbyte = 0;
 nchar = 0;
 nline = 0;
 for (;;) {
  ++nline;
  if (clp == curwp->w_dotp) {
   cline = nline;
   cchar = nchar + curwp->w_doto;
   if (curwp->w_doto == ((clp)->l_used))
    cbyte = '\n';
   else
    cbyte = (((unsigned char) ((clp)->l_text[(curwp->w_doto)])));
  }
  nchar += ((clp)->l_used);
  clp = ((clp)->l_fp);
  if (clp == curbp->b_headp)
   break;
  nchar++;
 }
 row = curwp->w_toprow + 1;
 clp = curwp->w_linep;
 while (clp != curbp->b_headp && clp != curwp->w_dotp) {
  ++row;
  clp = ((clp)->l_fp);
 }
 ratio = nchar ? (100L * cchar) / nchar : 100;
 ewprintf("Char: %c (0%o)  point=%ld(%d%%)  line=%d  row=%d  col=%d",
     cbyte, cbyte, cchar, ratio, cline, row, getcolpos(curwp));
 return (1);
}

canonical_original_code:
int showcpos(int f, int n)
{
 struct line *clp;
 long nchar; long cchar;
 int nline; int row;
 int cline; int cbyte;
 int ratio;
 clp = ((((curbp)->b_headp)->l_fp));
 cchar = 0;
 cline = 0;
 cbyte = 0;
 nchar = 0;
 nline = 0;
 for (;;) {
  ++nline;
  if (clp == curwp->w_dotp) {
   cline = nline;
   cchar = nchar + curwp->w_doto;
   if (curwp->w_doto == ((clp)->l_used))
    cbyte = '\n';
   else
    cbyte = (((unsigned char) ((clp)->l_text[(curwp->w_doto)])));
  }
  nchar += ((clp)->l_used);
  clp = ((clp)->l_fp);
  if (clp == curbp->b_headp)
   break;
  nchar++;
 }
 row = curwp->w_toprow + 1;
 clp = curwp->w_linep;
 while (clp != curbp->b_headp && clp != curwp->w_dotp) {
  ++row;
  clp = ((clp)->l_fp);
 }
 ratio = nchar ? (100L * cchar) / nchar : 100;
 ewprintf("Char: %c (0%o)  point=%ld(%d%%)  line=%d  row=%d  col=%d",
     cbyte, cbyte, cchar, ratio, cline, row, getcolpos(curwp));
 return (1);
}

variable_types: struct line { struct line *l_fp; struct line *l_bp; int l_size; int l_used; char *l_text; } *

variable_types: nchar long

variable_types: cchar long

variable_types: nline int

variable_types: row int

variable_types: cline int

variable_types: cbyte int

variable_types: ratio int


openline

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

v2 = 0;
if ( a2 >= 0 )
{
v2 = 1;
if ( a2 )
{
undo_boundary_enable(8LL, 0LL);
v4 = a2;
while ( 1 )
{
v2 = lnewline();
if ( v2 != 1 )
break;
if ( !--v4 )
{
v2 = backchar(a1 | 8u, (unsigned int)a2);
break;
}
}
undo_boundary_enable(8LL, 1LL);
}
}
return v2;
}

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

v2 = 0;
if ( a2 >= 0 )
{
v2 = 1;
if ( a2 )
{
undo_boundary_enable(8LL, 0LL);
v4 = a2;
while ( 1 )
{
v2 = lnewline();
if ( v2 != 1 )
break;
if ( !--v4 )
{
v2 = backchar(a1 | 8u, (unsigned int)a2);
break;
}
}
undo_boundary_enable(8LL, 1LL);
}
}
return v2;
}

original_code:
int
openline(int f, int n)
{
 int i, s;
 if (n < 0)
  return (0);
 if (n == 0)
  return (1);
 undo_boundary_enable(8, 0);
 i = n;
 do {
  s = lnewline();
 } while (s == 1 && --i);
 if (s == 1)
  s = backchar(f | 8, n);
 undo_boundary_enable(8, 1);
 return (s);
}

canonical_original_code:
int openline(int f, int n)
{
 int i; int s;
 if (n < 0)
  return (0);
 if (n == 0)
  return (1);
 undo_boundary_enable(8, 0);
 i = n;
 do {
  s = lnewline();
 } while (s == 1 && --i);
 if (s == 1)
  s = backchar(f | 8, n);
 undo_boundary_enable(8, 1);
 return (s);
}

variable_types: s int


newline

decompiled_code:
__int64 __fastcall newline(__int64 a1, int a2)
{
int v2; // ebx
__int64 result; // rax

v2 = a2;
result = 0LL;
if ( a2 >= 0 )
{
while ( --v2 != -1 )
{
result = lnewline();
if ( (_DWORD)result != 1 )
return result;
}
result = 1LL;
}
return result;
}

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

v2 = a2;
result = 0LL;
if ( a2 >= 0 )
{
while ( --v2 != -1 )
{
result = lnewline();
if ( (_DWORD)result != 1 )
return result;
}
result = 1LL;
}
return result;
}

original_code:
int
newline(int f, int n)
{
 int s;
 if (n < 0)
  return (0);
 while (n--) {
  if ((s = lnewline()) != 1)
   return (s);
 }
 return (1);
}


deblank

original_code:
int
deblank(int f, int n)
{
 struct line *lp1, *lp2;
 RSIZE nld;
 lp1 = curwp->w_dotp;
 while (((lp1)->l_used) == 0 && (lp2 = ((lp1)->l_bp)) != curbp->b_headp)
  lp1 = lp2;
 lp2 = lp1;
 nld = (RSIZE)0;
 while ((lp2 = ((lp2)->l_fp)) != curbp->b_headp && ((lp2)->l_used) == 0)
  ++nld;
 if (nld == 0)
  return (1);
 curwp->w_dotp = ((lp1)->l_fp);
 curwp->w_doto = 0;
 return (ldelete((RSIZE)nld, 0x00));
}

canonical_original_code:
int deblank(int f, int n)
{
 struct line *lp1; struct line *lp2;
 int nld;
 lp1 = curwp->w_dotp;
 while (((lp1)->l_used) == 0 && (lp2 = ((lp1)->l_bp)) != curbp->b_headp)
  lp1 = lp2;
 lp2 = lp1;
 nld = (int)0;
 while ((lp2 = ((lp2)->l_fp)) != curbp->b_headp && ((lp2)->l_used) == 0)
  ++nld;
 if (nld == 0)
  return (1);
 curwp->w_dotp = ((lp1)->l_fp);
 curwp->w_doto = 0;
 return (ldelete((int)nld, 0x00));
}

variable_types: nld int

user_defined_types: struct line { struct line *l_fp; struct line *l_bp; int l_size; int l_used; char *l_text; }


delwhite

original_code:
int
delwhite(int f, int n)
{
 int col, s;
 col = curwp->w_doto;
 while (col < ((curwp->w_dotp)->l_used) &&
     (((*__ctype_b_loc ())[(int) (((((unsigned char) ((curwp->w_dotp)->l_text[(col)])))))] & (unsigned short int) _ISspace)))
  ++col;
 do {
  if (curwp->w_doto == 0) {
   s = 0;
   break;
  }
  if ((s = backchar(8, 1)) != 1)
   break;
 } while (((*__ctype_b_loc ())[(int) (((((unsigned char) ((curwp->w_dotp)->l_text[(curwp->w_doto)])))))] & (unsigned short int) _ISspace));
 if (s == 1)
  (void)forwchar(8, 1);
 (void)ldelete((RSIZE)(col - curwp->w_doto), 0x00);
 return (1);
}

canonical_original_code:
int delwhite(int f, int n)
{
 int col; int s;
 col = curwp->w_doto;
 while (col < ((curwp->w_dotp)->l_used) &&
     (((*__ctype_b_loc ())[(int) (((((unsigned char) ((curwp->w_dotp)->l_text[(col)])))))] & (unsigned short) _ISspace)))
  ++col;
 do {
  if (curwp->w_doto == 0) {
   s = 0;
   break;
  }
  if ((s = backchar(8, 1)) != 1)
   break;
 } while (((*__ctype_b_loc ())[(int) (((((unsigned char) ((curwp->w_dotp)->l_text[(curwp->w_doto)])))))] & (unsigned short) _ISspace));
 if (s == 1)
  (void)forwchar(8, 1);
 (void)ldelete((RSIZE)(col - curwp->w_doto), 0x00);
 return (1);
}

variable_types: col int


justone

decompiled_code:
__int64 __fastcall justone(unsigned int a1, unsigned int a2)
{
__int64 v2; // rdx
_QWORD *v3; // rcx

undo_boundary_enable(8LL, 0LL);
delwhite(a1, a2, v2, v3);
linsert(1LL, 32LL);
undo_boundary_enable(8LL, 1LL);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned int a2)
{
__int64 v2; // rdx
_QWORD *v3; // rcx

undo_boundary_enable(8LL, 0LL);
func5(a1, a2, v2, v3);
linsert(1LL, 32LL);
undo_boundary_enable(8LL, 1LL);
return 1LL;
}

original_code:
int
justone(int f, int n)
{
 undo_boundary_enable(8, 0);
 (void)delwhite(f, n);
 linsert(1, ' ');
 undo_boundary_enable(8, 1);
 return (1);
}

variable_types: f int


deltrailwhite

decompiled_code:
__int64 deltrailwhite()
{
int v0; // ebx
__int64 v1; // rdx
_QWORD *v2; // rcx

v0 = *(_DWORD *)(curwp + 48LL);
gotoeol(8LL, 1LL);
delwhite(8LL, 1LL, v1, v2);
if ( *(_DWORD *)(curwp + 48LL) > v0 )
*(_DWORD *)(curwp + 48LL) = v0;
return 1LL;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // ebx
__int64 v1; // rdx
_QWORD *v2; // rcx

v0 = *(_DWORD *)(curwp + 48LL);
gotoeol(8LL, 1LL);
func5(8LL, 1LL, v1, v2);
if ( *(_DWORD *)(curwp + 48LL) > v0 )
*(_DWORD *)(curwp + 48LL) = v0;
return 1LL;
}

original_code:
int
deltrailwhite(int f, int n)
{
 int soff;
 soff = curwp->w_doto;
 gotoeol(8, 1);
 delwhite(8, 1);
 if (soff < curwp->w_doto)
  curwp->w_doto = soff;
 return (1);
}

variable_types: n int

variable_types: soff int


lfindent

decompiled_code:
__int64 __fastcall lfindent(__int64 a1, int a2)
{
unsigned int v2; // er12
int v3; // ebp
int v4; // ebx
unsigned __int8 *v5; // rax
int v6; // edx
__int64 v7; // rdi
__int64 v8; // rax
int v9; // edx
__int64 v10; // rsi

v2 = 0;
if ( a2 >= 0 )
{
v3 = a2;
undo_boundary_enable(8LL, 0LL);
if ( a2 )
{
do
{
v8 = *(_QWORD *)(curwp + 32LL);
v9 = *(_DWORD *)(v8 + 20);
if ( v9 <= 0 )
{
v2 = lnewline();
if ( !v2 )
goto LABEL_20;
}
else
{
v5 = *(unsigned __int8 **)(v8 + 24);
v10 = (__int64)&v5[v9 - 1 + 1];
v4 = 0;
do
{
v6 = *v5;
if ( v6 != 32 && v6 != 9 )
break;
if ( v6 == 9 )
v4 |= 7u;
++v4;
++v5;
}
while ( v5 != (unsigned __int8 *)v10 );
v2 = lnewline();
if ( !v2 )
goto LABEL_20;
v7 = (unsigned int)(v4 / 8);
if ( (_DWORD)v7 )
{
v2 = linsert(v7, 9LL);
if ( !v2 )
goto LABEL_20;
}
if ( (v4 & 7) != 0 )
{
v2 = linsert((unsigned int)(v4 % 8), 32LL);
if ( !v2 )
goto LABEL_20;
}
}
--v3;
}
while ( v3 );
v2 = 1;
}
else
{
v2 = 1;
}
LABEL_20:
undo_boundary_enable(8LL, 1LL);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
unsigned int v2; // er12
int v3; // ebp
int v4; // ebx
unsigned __int8 *v5; // rax
int v6; // edx
__int64 v7; // rdi
__int64 v8; // rax
int v9; // edx
__int64 v10; // rsi

v2 = 0;
if ( a2 >= 0 )
{
v3 = a2;
undo_boundary_enable(8LL, 0LL);
if ( a2 )
{
do
{
v8 = *(_QWORD *)(curwp + 32LL);
v9 = *(_DWORD *)(v8 + 20);
if ( v9 <= 0 )
{
v2 = lnewline();
if ( !v2 )
goto LABEL_20;
}
else
{
v5 = *(unsigned __int8 **)(v8 + 24);
v10 = (__int64)&v5[v9 - 1 + 1];
v4 = 0;
do
{
v6 = *v5;
if ( v6 != 32 && v6 != 9 )
break;
if ( v6 == 9 )
v4 |= 7u;
++v4;
++v5;
}
while ( v5 != (unsigned __int8 *)v10 );
v2 = lnewline();
if ( !v2 )
goto LABEL_20;
v7 = (unsigned int)(v4 / 8);
if ( (_DWORD)v7 )
{
v2 = linsert(v7, 9LL);
if ( !v2 )
goto LABEL_20;
}
if ( (v4 & 7) != 0 )
{
v2 = linsert((unsigned int)(v4 % 8), 32LL);
if ( !v2 )
goto LABEL_20;
}
}
--v3;
}
while ( v3 );
v2 = 1;
}
else
{
v2 = 1;
}
LABEL_20:
undo_boundary_enable(8LL, 1LL);
}
return v2;
}

original_code:
int
lfindent(int f, int n)
{
 int c, i, nicol;
 int s = 1;
 if (n < 0)
  return (0);
 undo_boundary_enable(8, 0);
 while (n--) {
  nicol = 0;
  for (i = 0; i < ((curwp->w_dotp)->l_used); ++i) {
   c = (((unsigned char) ((curwp->w_dotp)->l_text[(i)])));
   if (c != ' ' && c != '\t')
    break;
   if (c == '\t')
    nicol |= 0x07;
   ++nicol;
  }
  if (lnewline() == 0 || ((
      ((i = nicol / 8) != 0 && linsert(i, '\t') == 0) ||
      ((i = nicol % 8) != 0 && linsert(i, ' ') == 0)))) {
   s = 0;
   break;
  }
 }
 undo_boundary_enable(8, 1);
 return (s);
}

canonical_original_code:
int lfindent(int f, int n)
{
 int c; int i; int nicol;
 int s = 1;
 if (n < 0)
  return (0);
 undo_boundary_enable(8, 0);
 while (n--) {
  nicol = 0;
  for (i = 0; i < ((curwp->w_dotp)->l_used); ++i) {
   c = (((unsigned char) ((curwp->w_dotp)->l_text[(i)])));
   if (c != ' ' && c != '\t')
    break;
   if (c == '\t')
    nicol |= 0x07;
   ++nicol;
  }
  if (lnewline() == 0 || ((
      ((i = nicol / 8) != 0 && linsert(i, '\t') == 0) ||
      ((i = nicol % 8) != 0 && linsert(i, ' ') == 0)))) {
   s = 0;
   break;
  }
 }
 undo_boundary_enable(8, 1);
 return (s);
}

variable_types: c int

variable_types: i int

variable_types: nicol int


backdel

decompiled_code:
__int64 __fastcall backdel(int a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rcx

if ( a2 < 0 )
return forwdel(a1 | 8u, (unsigned int)-a2);
if ( (a1 & 7) != 0 )
{
if ( (lastflag & 2) == 0 )
kdelete();
thisflag |= 2u;
}
result = backchar(a1 | 8u, (unsigned int)a2);
if ( (_DWORD)result == 1 )
result = ldelete((unsigned int)a2, (a1 & 7) != 0, v3, v4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdx
__int64 v4; // rcx

if ( a2 < 0 )
return func11(a1 | 8u, (unsigned int)-a2);
if ( (a1 & 7) != 0 )
{
if ( (lastflag & 2) == 0 )
kdelete();
thisflag |= 2u;
}
result = backchar(a1 | 8u, (unsigned int)a2);
if ( (_DWORD)result == 1 )
result = ldelete((unsigned int)a2, (a1 & 7) != 0, v3, v4);
return result;
}

original_code:
int
backdel(int f, int n)
{
 int s;
 if (n < 0)
  return (forwdel(f | 8, -n));
 if (f & 7) {
  if ((lastflag & 0x0002) == 0)
   kdelete();
  thisflag |= 0x0002;
 }
 if ((s = backchar(f | 8, n)) == 1)
  s = ldelete((RSIZE)n, (f & 7) ? 0x01 : 0x00);
 return (s);
}

canonical_original_code:
int backdel(int f, int n)
{
 int s;
 if (n < 0)
  return (forwdel(f | 8, -n));
 if (f & 7) {
  if ((lastflag & 0x0002) == 0)
   kdelete();
  thisflag |= 0x0002;
 }
 if ((s = backchar(f | 8, n)) == 1)
  s = ldelete((int)n, (f & 7) ? 0x01 : 0x00);
 return (s);
}


twiddle

decompiled_code:
__int64 twiddle()
{
__int64 v0; // rbp
unsigned int v1; // ebx
unsigned int v2; // ebx
unsigned int v3; // ebx

v0 = *(_QWORD *)(curwp + 32LL);
v1 = *(_DWORD *)(curwp + 48LL);
if ( v1 )
{
if ( v1 != *(_DWORD *)(v0 + 20) )
{
undo_boundary_enable(8LL, 0LL);
goto LABEL_9;
}
}
else
{
if ( *(_QWORD *)(v0 + 8) == *(_QWORD *)(curbp + 40LL) )
{
dobeep();
ewprintf("Beginning of buffer");
return v1;
}
if ( *(_DWORD *)(v0 + 20) )
{
undo_boundary_enable(8LL, 0LL);
goto LABEL_13;
}
}
if ( *(_QWORD *)v0 != *(_QWORD *)(curbp + 40LL) )
{
undo_boundary_enable(8LL, 0LL);
if ( !v1 )
{
LABEL_13:
if ( *(_DWORD *)(v0 + 20) )
{
v3 = **(unsigned __int8 **)(v0 + 24);
backdel(8, 1);
forwchar(8LL, 1LL);
lnewline();
linsert(1LL, v3);
backdel(8, 1);
}
else
{
forwline(8LL, 1LL);
*(_DWORD *)(curwp + 48LL) = 0;
}
goto LABEL_15;
}
LABEL_9:
v2 = *(unsigned __int8 *)(*(_QWORD *)(v0 + 24) + (int)v1 - 1LL);
backdel(8, 1);
forwchar(8LL, 1LL);
linsert(1LL, v2);
LABEL_15:
undo_boundary_enable(8LL, 1LL);
lchange(4LL);
return 1;
}
dobeep();
return 0;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 v0; // rbp
unsigned int v1; // ebx
unsigned int v2; // ebx
unsigned int v3; // ebx

v0 = *(_QWORD *)(curwp + 32LL);
v1 = *(_DWORD *)(curwp + 48LL);
if ( v1 )
{
if ( v1 != *(_DWORD *)(v0 + 20) )
{
undo_boundary_enable(8LL, 0LL);
goto LABEL_9;
}
}
else
{
if ( *(_QWORD *)(v0 + 8) == *(_QWORD *)(curbp + 40LL) )
{
dobeep();
ewprintf("Beginning of buffer");
return v1;
}
if ( *(_DWORD *)(v0 + 20) )
{
undo_boundary_enable(8LL, 0LL);
goto LABEL_13;
}
}
if ( *(_QWORD *)v0 != *(_QWORD *)(curbp + 40LL) )
{
undo_boundary_enable(8LL, 0LL);
if ( !v1 )
{
LABEL_13:
if ( *(_DWORD *)(v0 + 20) )
{
v3 = **(unsigned __int8 **)(v0 + 24);
func9(8, 1);
forwchar(8LL, 1LL);
lnewline();
linsert(1LL, v3);
func9(8, 1);
}
else
{
forwline(8LL, 1LL);
*(_DWORD *)(curwp + 48LL) = 0;
}
goto LABEL_15;
}
LABEL_9:
v2 = *(unsigned __int8 *)(*(_QWORD *)(v0 + 24) + (int)v1 - 1LL);
func9(8, 1);
forwchar(8LL, 1LL);
linsert(1LL, v2);
LABEL_15:
undo_boundary_enable(8LL, 1LL);
lchange(4LL);
return 1;
}
dobeep();
return 0;
}

original_code:
int
twiddle(int f, int n)
{
 struct line *dotp;
 int doto, cr;
 dotp = curwp->w_dotp;
 doto = curwp->w_doto;
 if (doto == 0 && ((dotp)->l_bp) == curbp->b_headp) {
  dobeep();
  ewprintf("Beginning of buffer");
  return(0);
 }
 if (doto == ((dotp)->l_used) && ((dotp)->l_fp) == curbp->b_headp) {
  dobeep();
  return(0);
 }
 undo_boundary_enable(8, 0);
 if (doto == 0 && doto == ((dotp)->l_used)) {
  (void)forwline(8, 1);
  curwp->w_doto = 0;
 } else {
  if (doto == 0) {
   cr = (((unsigned char) ((dotp)->l_text[(doto)])));
   (void)backdel(8, 1);
   (void)forwchar(8, 1);
   lnewline();
   linsert(1, cr);
   (void)backdel(8, 1);
  } else {
   cr = (((unsigned char) ((dotp)->l_text[(doto - 1)])));
   (void)backdel(8, 1);
   (void)forwchar(8, 1);
   linsert(1, cr);
  }
 }
 undo_boundary_enable(8, 1);
 lchange(0x04);
 return (1);
}

canonical_original_code:
int twiddle(int f, int n)
{
 struct line *dotp;
 int doto; int cr;
 dotp = curwp->w_dotp;
 doto = curwp->w_doto;
 if (doto == 0 && ((dotp)->l_bp) == curbp->b_headp) {
  dobeep();
  ewprintf("Beginning of buffer");
  return(0);
 }
 if (doto == ((dotp)->l_used) && ((dotp)->l_fp) == curbp->b_headp) {
  dobeep();
  return(0);
 }
 undo_boundary_enable(8, 0);
 if (doto == 0 && doto == ((dotp)->l_used)) {
  (void)forwline(8, 1);
  curwp->w_doto = 0;
 } else {
  if (doto == 0) {
   cr = (((unsigned char) ((dotp)->l_text[(doto)])));
   (void)backdel(8, 1);
   (void)forwchar(8, 1);
   lnewline();
   linsert(1, cr);
   (void)backdel(8, 1);
  } else {
   cr = (((unsigned char) ((dotp)->l_text[(doto - 1)])));
   (void)backdel(8, 1);
   (void)forwchar(8, 1);
   linsert(1, cr);
  }
 }
 undo_boundary_enable(8, 1);
 lchange(0x04);
 return (1);
}

variable_types: struct line { struct line *l_fp; struct line *l_bp; int l_size; int l_used; char *l_text; } *

variable_types: doto int

variable_types: cr int


forwdel

decompiled_code:
__int64 __fastcall forwdel(int a1, int a2, __int64 a3, __int64 a4)
{
if ( a2 < 0 )
return backdel(a1 | 8u, -a2);
if ( (a1 & 7) != 0 )
{
if ( (lastflag & 2) == 0 )
kdelete();
thisflag |= 2u;
}
return ldelete((unsigned int)a2, (a1 & 7) != 0, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, int a2, __int64 a3, __int64 a4)
{
if ( a2 < 0 )
return func9(a1 | 8u, -a2);
if ( (a1 & 7) != 0 )
{
if ( (lastflag & 2) == 0 )
kdelete();
thisflag |= 2u;
}
return ldelete((unsigned int)a2, (a1 & 7) != 0, a3, a4);
}

original_code:
int
forwdel(int f, int n)
{
 if (n < 0)
  return (backdel(f | 8, -n));
 if (f & 7) {
  if ((lastflag & 0x0002) == 0)
   kdelete();
  thisflag |= 0x0002;
 }
 return (ldelete((RSIZE) n, (f & 7) ? 0x01 : 0x00));
}

canonical_original_code:
int forwdel(int f, int n)
{
 if (n < 0)
  return (backdel(f | 8, -n));
 if (f & 7) {
  if ((lastflag & 0x0002) == 0)
   kdelete();
  thisflag |= 0x0002;
 }
 return (ldelete((int) n, (f & 7) ? 0x01 : 0x00));
}


delleadwhite

decompiled_code:
__int64 delleadwhite()
{
__int64 v0; // rbx
int v1; // er12
int v2; // ebp
const unsigned __int16 *v3; // rcx
unsigned __int8 *v4; // rax
int v5; // ebx
__int64 v6; // rdx
__int64 v7; // rcx
int v8; // er12
__int64 v9; // rsi

v0 = *(_QWORD *)(curwp + 32LL);
v1 = *(_DWORD *)(curwp + 48LL);
v2 = *(_DWORD *)(v0 + 20);
if ( v2 <= 0 )
{
v5 = 0;
}
else
{
v3 = *_ctype_b_loc();
v4 = *(unsigned __int8 **)(v0 + 24);
v5 = 0;
while ( (v3[*v4] & 0x2000) != 0 )
{
++v5;
++v4;
if ( v5 == v2 )
{
v5 = v2;
break;
}
}
}
gotobol(8LL, 1LL);
forwdel(8, v5, v6, v7);
v8 = v1 - v5;
v9 = 0LL;
if ( v8 >= 0 )
v9 = (unsigned int)v8;
forwchar(8LL, v9);
return 1LL;
}

original_code:
int
delleadwhite(int f, int n)
{
 int soff, ls;
 struct line *slp;
 slp = curwp->w_dotp;
 soff = curwp->w_doto;
 for (ls = 0; ls < ((slp)->l_used); ls++)
                 if (!((*__ctype_b_loc ())[(int) (((((unsigned char) ((slp)->l_text[(ls)])))))] & (unsigned short int) _ISspace))
                        break;
 gotobol(8, 1);
 forwdel(8, ls);
 soff -= ls;
 if (soff < 0)
  soff = 0;
 forwchar(8, soff);
 return (1);
}

canonical_original_code:
int delleadwhite(int f, int n)
{
 int soff; int ls;
 struct line *slp;
 slp = curwp->w_dotp;
 soff = curwp->w_doto;
 for (ls = 0; ls < ((slp)->l_used); ls++)
                 if (!((*__ctype_b_loc ())[(int) (((((unsigned char) ((slp)->l_text[(ls)])))))] & (unsigned short) _ISspace))
                        break;
 gotobol(8, 1);
 forwdel(8, ls);
 soff -= ls;
 if (soff < 0)
  soff = 0;
 forwchar(8, soff);
 return (1);
}

variable_types: ls int


indent

decompiled_code:
__int64 __fastcall indent(char a1, int a2)
{
__int64 result; // rax
unsigned int v3; // ebp
__int64 v4; // rdi

result = 0LL;
if ( a2 >= 0 )
{
delleadwhite(8LL, 1LL);
result = 1LL;
if ( (a1 & 7) != 0 )
{
v3 = *(_DWORD *)(curwp + 48LL);
gotobol(8LL, 1LL);
v4 = (unsigned int)(a2 >> 3);
if ( !(_DWORD)v4 || (result = linsert(v4, 9LL), (_DWORD)result) )
{
if ( (a2 & 7) == 0 || (result = linsert((unsigned int)(a2 % 8), 32LL), (_DWORD)result) )
{
forwchar(8LL, v3);
result = 1LL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char a1, int a2)
{
__int64 result; // rax
unsigned int v3; // ebp
__int64 v4; // rdi

result = 0LL;
if ( a2 >= 0 )
{
func12(8LL, 1LL);
result = 1LL;
if ( (a1 & 7) != 0 )
{
v3 = *(_DWORD *)(curwp + 48LL);
gotobol(8LL, 1LL);
v4 = (unsigned int)(a2 >> 3);
if ( !(_DWORD)v4 || (result = linsert(v4, 9LL), (_DWORD)result) )
{
if ( (a2 & 7) == 0 || (result = linsert((unsigned int)(a2 % 8), 32LL), (_DWORD)result) )
{
forwchar(8LL, v3);
result = 1LL;
}
}
}
}
return result;
}

original_code:
int
indent(int f, int n)
{
 int soff, i;
 if (n < 0)
  return (0);
 delleadwhite(8, 1);
 if (!(f & 7))
  return (1);
 soff = curwp->w_doto;
 (void)gotobol(8, 1);
 if (
     (((i = n / 8) != 0 && linsert(i, '\t') == 0) ||
     ((i = n % 8) != 0 && linsert(i, ' ') == 0)))
  return (0);
 forwchar(8, soff);
 return (1);
}

canonical_original_code:
int indent(int f, int n)
{
 int soff; int i;
 if (n < 0)
  return (0);
 delleadwhite(8, 1);
 if (!(f & 7))
  return (1);
 soff = curwp->w_doto;
 (void)gotobol(8, 1);
 if (
     (((i = n / 8) != 0 && linsert(i, '\t') == 0) ||
     ((i = n % 8) != 0 && linsert(i, ' ') == 0)))
  return (0);
 forwchar(8, soff);
 return (1);
}


backtoindent

original_code:
int
backtoindent(int f, int n)
{
 gotobol(8, 1);
 while (curwp->w_doto < ((curwp->w_dotp)->l_used) &&
     (((*__ctype_b_loc ())[(int) (((((unsigned char) ((curwp->w_dotp)->l_text[(curwp->w_doto)])))))] & (unsigned short int) _ISspace)))
  ++curwp->w_doto;
 return (1);
}

canonical_original_code:
int backtoindent(int f, int n)
{
 gotobol(8, 1);
 while (curwp->w_doto < ((curwp->w_dotp)->l_used) &&
     (((*__ctype_b_loc ())[(int) (((((unsigned char) ((curwp->w_dotp)->l_text[(curwp->w_doto)])))))] & (unsigned short) _ISspace)))
  ++curwp->w_doto;
 return (1);
}


joinline

decompiled_code:
__int64 __fastcall joinline(char a1)
{
__int64 v1; // rdx
__int64 v2; // rcx
__int64 v3; // rdx
_QWORD *v4; // rcx
int v5; // ebx

undo_boundary_enable(8LL, 0LL);
if ( (a1 & 7) != 0 )
{
gotoeol(8LL, 1LL);
forwdel(8, 1, v1, v2);
}
else
{
gotobol(8LL, 1LL);
backdel(8, 1);
}
delwhite(8LL, 1LL, v3, v4);
v5 = *(_DWORD *)(curwp + 48LL);
if ( v5 > 0 )
{
linsert(1LL, 32LL);
*(_DWORD *)(curwp + 48LL) = v5;
}
undo_boundary_enable(8LL, 1LL);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(char a1)
{
__int64 v1; // rdx
__int64 v2; // rcx
__int64 v3; // rdx
_QWORD *v4; // rcx
int v5; // ebx

undo_boundary_enable(8LL, 0LL);
if ( (a1 & 7) != 0 )
{
gotoeol(8LL, 1LL);
func11(8, 1, v1, v2);
}
else
{
gotobol(8LL, 1LL);
func9(8, 1);
}
func5(8LL, 1LL, v3, v4);
v5 = *(_DWORD *)(curwp + 48LL);
if ( v5 > 0 )
{
linsert(1LL, 32LL);
*(_DWORD *)(curwp + 48LL) = v5;
}
undo_boundary_enable(8LL, 1LL);
return 1LL;
}

original_code:
int
joinline(int f, int n)
{
 int doto;
 undo_boundary_enable(8, 0);
 if (f & 7) {
  gotoeol(8, 1);
  forwdel(8, 1);
 } else {
  gotobol(8, 1);
  backdel(8, 1);
 }
 delwhite(8, 1);
 if ((doto = curwp->w_doto) > 0) {
  linsert(1, ' ');
  curwp->w_doto = doto;
 }
 undo_boundary_enable(8, 1);
 return (1);
}


gotobol

decompiled_code:
__int64 gotobol()
{
*(_DWORD *)(curwp + 48LL) = 0;
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
*(_DWORD *)(curwp + 48LL) = 0;
return 1LL;
}

original_code:
int
gotobol(int f, int n)
{
 curwp->w_doto = 0;
 return (1);
}


gotoeol

decompiled_code:
__int64 gotoeol()
{
*(_DWORD *)(curwp + 48LL) = *(_DWORD *)(*(_QWORD *)(curwp + 32LL) + 20LL);
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
*(_DWORD *)(curwp + 48LL) = *(_DWORD *)(*(_QWORD *)(curwp + 32LL) + 20LL);
return 1LL;
}

original_code:
int
gotoeol(int f, int n)
{
 curwp->w_doto = ((curwp->w_dotp)->l_used);
 return (1);
}


forwchar

original_code:
int
forwchar(int f, int n)
{
 if (n < 0)
  return (backchar(f, -n));
 while (n--) {
  if (curwp->w_doto == ((curwp->w_dotp)->l_used)) {
   curwp->w_dotp = ((curwp->w_dotp)->l_fp);
   if (curwp->w_dotp == curbp->b_headp) {
    curwp->w_dotp = ((curwp->w_dotp)->l_bp);
    if (!(f & 8)) {
     dobeep();
     ewprintf("End of buffer");
    }
    return (0);
   }
   curwp->w_doto = 0;
   curwp->w_dotline++;
   curwp->w_rflag |= 0x02;
  } else
   curwp->w_doto++;
 }
 return (1);
}


backchar

decompiled_code:
__int64 __fastcall backchar(__int64 a1, int a2)
{
char v2; // bl
__int64 v3; // rax
__int64 v4; // rdi
__int64 v5; // rcx
__int64 v6; // rdx
__int64 v7; // rsi

v2 = a1;
if ( a2 < 0 )
{
LODWORD(v6) = forwchar(a1, -a2);
return (unsigned int)v6;
}
v3 = curwp;
v4 = curbp;
v5 = (unsigned int)a2;
LODWORD(v6) = 1;
if ( !a2 )
return (unsigned int)v6;
while ( 1 )
{
v6 = *(unsigned int *)(v3 + 48);
if ( !(_DWORD)v6 )
break;
*(_DWORD *)(v3 + 48) = v6 - 1;
LABEL_8:
v5 = (unsigned int)(v5 - 1);
if ( !(_DWORD)v5 )
{
LODWORD(v6) = 1;
return (unsigned int)v6;
}
}
v7 = *(_QWORD *)(*(_QWORD *)(v3 + 32) + 8LL);
if ( *(_QWORD *)(v4 + 40) != v7 )
{
*(_QWORD *)(v3 + 32) = v7;
*(_DWORD *)(v3 + 48) = *(_DWORD *)(v7 + 20);
*(_BYTE *)(v3 + 68) |= 2u;
--*(_DWORD *)(v3 + 80);
goto LABEL_8;
}
if ( (v2 & 8) == 0 )
{
dobeep(v4, v7, v6, v5);
ewprintf("Beginning of buffer");
LODWORD(v6) = 0;
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
char v2; // bl
__int64 v3; // rax
__int64 v4; // rdi
__int64 v5; // rcx
__int64 v6; // rdx
__int64 v7; // rsi

v2 = a1;
if ( a2 < 0 )
{
LODWORD(v6) = func2(a1, -a2);
return (unsigned int)v6;
}
v3 = curwp;
v4 = curbp;
v5 = (unsigned int)a2;
LODWORD(v6) = 1;
if ( !a2 )
return (unsigned int)v6;
while ( 1 )
{
v6 = *(unsigned int *)(v3 + 48);
if ( !(_DWORD)v6 )
break;
*(_DWORD *)(v3 + 48) = v6 - 1;
LABEL_8:
v5 = (unsigned int)(v5 - 1);
if ( !(_DWORD)v5 )
{
LODWORD(v6) = 1;
return (unsigned int)v6;
}
}
v7 = *(_QWORD *)(*(_QWORD *)(v3 + 32) + 8LL);
if ( *(_QWORD *)(v4 + 40) != v7 )
{
*(_QWORD *)(v3 + 32) = v7;
*(_DWORD *)(v3 + 48) = *(_DWORD *)(v7 + 20);
*(_BYTE *)(v3 + 68) |= 2u;
--*(_DWORD *)(v3 + 80);
goto LABEL_8;
}
if ( (v2 & 8) == 0 )
{
dobeep(v4, v7, v6, v5);
ewprintf("Beginning of buffer");
LODWORD(v6) = 0;
}
return (unsigned int)v6;
}

original_code:
int
backchar(int f, int n)
{
 struct line *lp;
 if (n < 0)
  return (forwchar(f, -n));
 while (n--) {
  if (curwp->w_doto == 0) {
   if ((lp = ((curwp->w_dotp)->l_bp)) == curbp->b_headp) {
    if (!(f & 8)) {
     dobeep();
     ewprintf("Beginning of buffer");
    }
    return (0);
   }
   curwp->w_dotp = lp;
   curwp->w_doto = ((lp)->l_used);
   curwp->w_rflag |= 0x02;
   curwp->w_dotline--;
  } else
   curwp->w_doto--;
 }
 return (1);
}


setgoal

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

result = getcolpos(curwp);
curgoal = result;
return result;
}

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

result = getcolpos(curwp);
curgoal = result;
return result;
}

original_code:
void
setgoal(void)
{
 curgoal = getcolpos(curwp);
}

canonical_original_code:
void setgoal()
{
 curgoal = getcolpos(curwp);
}


getgoal

original_code:
int
getgoal(struct line *dlp)
{
 int c, i, col = 0;
 char tmp[5];
 for (i = 0; i < ((dlp)->l_used); i++) {
  c = (((unsigned char) ((dlp)->l_text[(i)])));
  if (c == '\t'
   ) {
   col |= 0x07;
   col++;
  } else if (((cinfo[((unsigned char) (c))]&0x08)!=0) != 0) {
   col += 2;
  } else if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint))
   col++;
  else {
   col += snprintf(tmp, sizeof(tmp), "\\%o", c);
  }
  if (col > curgoal)
   break;
 }
 return (i);
}

canonical_original_code:
int getgoal(struct line *dlp)
{
 int c; int i; int col = 0;
 char tmp[5];
 for (i = 0; i < ((dlp)->l_used); i++) {
  c = (((unsigned char) ((dlp)->l_text[(i)])));
  if (c == '\t'
   ) {
   col |= 0x07;
   col++;
  } else if (((cinfo[((unsigned char) (c))]&0x08)!=0) != 0) {
   col += 2;
  } else if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint))
   col++;
  else {
   col += snprintf(tmp, sizeof(tmp), "\\%o", c);
  }
  if (col > curgoal)
   break;
 }
 return (i);
}

variable_types:
tmp
char [5]


backline

decompiled_code:
__int64 __fastcall backline(__int64 a1, int a2)
{
char v2; // bp
__int64 v4; // rcx
__int64 v5; // rdx
__int64 v6; // rbx
__int64 v7; // rsi
unsigned int v8; // eax
__int64 result; // rax
__int64 v10; // rdi
__int64 v11; // rdx
__int64 v12; // rbp

v2 = a1;
if ( a2 < 0 )
return forwline((unsigned int)a1 | 8, (unsigned int)-a2);
if ( (lastflag & 1) == 0 )
setgoal();
thisflag |= 1u;
v4 = curwp;
v5 = *(_QWORD *)(curwp + 32LL);
v6 = *(_QWORD *)(v5 + 8);
v7 = curbp;
if ( v6 == *(_QWORD *)(curbp + 40LL) )
{
result = 1LL;
if ( (a1 & 8) == 0 )
{
dobeep(a1, curbp, v5, curwp);
ewprintf("Beginning of buffer");
result = 1LL;
}
}
else
{
v8 = a2 - 2;
if ( a2 )
{
while ( 1 )
{
--*(_DWORD *)(v4 + 80);
v10 = v8;
if ( v8 == -1 )
break;
v11 = *(_QWORD *)(v6 + 8);
--v8;
if ( v11 == *(_QWORD *)(v7 + 40) )
{
if ( (int)v10 > 0 && (v2 & 8) == 0 )
{
dobeep(v10, v7, v11, v4);
ewprintf("Beginning of buffer");
}
break;
}
v6 = *(_QWORD *)(v6 + 8);
}
}
else
{
v6 = *(_QWORD *)(curwp + 32LL);
}
v12 = curwp;
*(_QWORD *)(curwp + 32LL) = v6;
*(_DWORD *)(v12 + 48) = getgoal(v6);
*(_BYTE *)(curwp + 68LL) |= 2u;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
char v2; // bp
__int64 v4; // rcx
__int64 v5; // rdx
__int64 v6; // rbx
__int64 v7; // rsi
unsigned int v8; // eax
__int64 result; // rax
__int64 v10; // rdi
__int64 v11; // rdx
__int64 v12; // rbp

v2 = a1;
if ( a2 < 0 )
return func7((unsigned int)a1 | 8, (unsigned int)-a2);
if ( (lastflag & 1) == 0 )
func4();
thisflag |= 1u;
v4 = curwp;
v5 = *(_QWORD *)(curwp + 32LL);
v6 = *(_QWORD *)(v5 + 8);
v7 = curbp;
if ( v6 == *(_QWORD *)(curbp + 40LL) )
{
result = 1LL;
if ( (a1 & 8) == 0 )
{
dobeep(a1, curbp, v5, curwp);
ewprintf("Beginning of buffer");
result = 1LL;
}
}
else
{
v8 = a2 - 2;
if ( a2 )
{
while ( 1 )
{
--*(_DWORD *)(v4 + 80);
v10 = v8;
if ( v8 == -1 )
break;
v11 = *(_QWORD *)(v6 + 8);
--v8;
if ( v11 == *(_QWORD *)(v7 + 40) )
{
if ( (int)v10 > 0 && (v2 & 8) == 0 )
{
dobeep(v10, v7, v11, v4);
ewprintf("Beginning of buffer");
}
break;
}
v6 = *(_QWORD *)(v6 + 8);
}
}
else
{
v6 = *(_QWORD *)(curwp + 32LL);
}
v12 = curwp;
*(_QWORD *)(curwp + 32LL) = v6;
*(_DWORD *)(v12 + 48) = func5(v6);
*(_BYTE *)(curwp + 68LL) |= 2u;
result = 1LL;
}
return result;
}

original_code:
int
backline(int f, int n)
{
 struct line *dlp;
 if (n < 0)
  return (forwline(f | 8, -n));
 if ((lastflag & 0x0001) == 0)
  setgoal();
 thisflag |= 0x0001;
 dlp = curwp->w_dotp;
 if (((dlp)->l_bp) == curbp->b_headp) {
  if (!(f & 8)) {
   dobeep();
   ewprintf("Beginning of buffer");
  }
  return(1);
 }
 while (n-- && ((dlp)->l_bp) != curbp->b_headp) {
  dlp = ((dlp)->l_bp);
  curwp->w_dotline--;
 }
 if (n > 0 && !(f & 8)) {
  dobeep();
  ewprintf("Beginning of buffer");
 }
 curwp->w_dotp = dlp;
 curwp->w_doto = getgoal(dlp);
 curwp->w_rflag |= 0x02;
 return (1);
}


forwline

decompiled_code:
__int64 __fastcall forwline(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
_QWORD *v4; // rbp
__int64 result; // rax
__int64 v6; // rax
__int64 v7; // r12
__int64 v8; // rax

if ( (int)a2 < 0 )
return backline((unsigned int)a1 | 8, -(int)a2);
v4 = *(_QWORD **)(curwp + 32LL);
if ( *(_QWORD **)(curbp + 40LL) == v4 )
{
result = 1LL;
if ( (a1 & 8) == 0 )
{
LABEL_15:
dobeep(a1, a2, a3, a4);
ewprintf("End of buffer");
return 1LL;
}
}
else
{
if ( (lastflag & 1) == 0 )
setgoal();
thisflag |= 1u;
result = 1LL;
if ( (_DWORD)a2 )
{
v6 = curbp;
v7 = curwp;
a2 = (unsigned int)a2;
while ( 1 )
{
v4 = (_QWORD *)*v4;
if ( *(_QWORD **)(v6 + 40) == v4 )
break;
++*(_DWORD *)(v7 + 80);
a2 = (unsigned int)(a2 - 1);
if ( !(_DWORD)a2 )
{
*(_BYTE *)(v7 + 68) |= 2u;
*(_QWORD *)(v7 + 32) = v4;
*(_DWORD *)(v7 + 48) = getgoal((__int64)v4);
return 1LL;
}
}
v8 = v4[1];
*(_QWORD *)(v7 + 32) = v8;
*(_DWORD *)(v7 + 48) = *(_DWORD *)(v8 + 20);
*(_BYTE *)(v7 + 68) |= 2u;
result = 1LL;
if ( (a1 & 8) == 0 )
goto LABEL_15;
}
}
return result;
}

original_code:
int
forwline(int f, int n)
{
 struct line *dlp;
 if (n < 0)
  return (backline(f | 8, -n));
 if ((dlp = curwp->w_dotp) == curbp->b_headp) {
  if (!(f & 8)) {
   dobeep();
   ewprintf("End of buffer");
  }
  return(1);
 }
 if ((lastflag & 0x0001) == 0)
  setgoal();
 thisflag |= 0x0001;
 if (n == 0)
  return (1);
 while (n--) {
  dlp = ((dlp)->l_fp);
  if (dlp == curbp->b_headp) {
   curwp->w_dotp = ((dlp)->l_bp);
   curwp->w_doto = ((curwp->w_dotp)->l_used);
   curwp->w_rflag |= 0x02;
   if (!(f & 8)) {
    dobeep();
    ewprintf("End of buffer");
   }
   return (1);
  }
  curwp->w_dotline++;
 }
 curwp->w_rflag |= 0x02;
 curwp->w_dotp = dlp;
 curwp->w_doto = getgoal(dlp);
 return (1);
}


backpage

decompiled_code:
__int64 __fastcall backpage(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
_QWORD *v4; // rbp
_QWORD *i; // rbx
__int64 result; // rax
__int64 v7; // rax
int v8; // esi
int v9; // edx
__int64 v10; // rdi
__int64 v11; // rcx
int v12; // edx

if ( (a1 & 7) != 0 )
{
if ( (int)a2 < 0 )
return forwpage((unsigned int)a1 | 8, (unsigned int)-(int)a2);
}
else
{
a2 = (unsigned int)(*(_DWORD *)(curwp + 60LL) - 2);
if ( *(_DWORD *)(curwp + 60LL) - 2 <= 0 )
return backline(a1, 1);
}
v4 = *(_QWORD **)(curwp + 24LL);
for ( i = v4; (_DWORD)a2; i = (_QWORD *)i[1] )
{
a2 = (unsigned int)(a2 - 1);
if ( i[1] == *(_QWORD *)(curbp + 40LL) )
break;
}
if ( i == v4 )
{
dobeep(a1, a2, curbp, a4);
ewprintf("Beginning of buffer");
}
v7 = curwp;
*(_QWORD *)(curwp + 24LL) = i;
*(_BYTE *)(v7 + 68) |= 8u;
v8 = *(_DWORD *)(v7 + 60);
if ( v8 )
{
v9 = *(_DWORD *)(v7 + 60);
while ( *(_QWORD **)(curbp + 40LL) != i )
{
if ( *(_QWORD **)(v7 + 32) == i )
return 1LL;
i = (_QWORD *)*i;
if ( !--v9 )
break;
}
}
v10 = *v4;
v11 = *(_QWORD *)(v7 + 32);
if ( *v4 == v11 )
{
LABEL_20:
*(_DWORD *)(v7 + 48) = 0;
result = 1LL;
}
else
{
while ( 1 )
{
v12 = *(_DWORD *)(v7 + 80);
if ( v12 <= v8 )
break;
v11 = *(_QWORD *)(v11 + 8);
*(_QWORD *)(v7 + 32) = v11;
*(_DWORD *)(v7 + 80) = v12 - 1;
if ( v11 == v10 )
goto LABEL_20;
}
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
_QWORD *v4; // rbp
_QWORD *i; // rbx
__int64 result; // rax
__int64 v7; // rax
int v8; // esi
int v9; // edx
__int64 v10; // rdi
__int64 v11; // rcx
int v12; // edx

if ( (a1 & 7) != 0 )
{
if ( (int)a2 < 0 )
return func9((unsigned int)a1 | 8, (unsigned int)-(int)a2);
}
else
{
a2 = (unsigned int)(*(_DWORD *)(curwp + 60LL) - 2);
if ( *(_DWORD *)(curwp + 60LL) - 2 <= 0 )
return func6(a1, 1);
}
v4 = *(_QWORD **)(curwp + 24LL);
for ( i = v4; (_DWORD)a2; i = (_QWORD *)i[1] )
{
a2 = (unsigned int)(a2 - 1);
if ( i[1] == *(_QWORD *)(curbp + 40LL) )
break;
}
if ( i == v4 )
{
dobeep(a1, a2, curbp, a4);
ewprintf("Beginning of buffer");
}
v7 = curwp;
*(_QWORD *)(curwp + 24LL) = i;
*(_BYTE *)(v7 + 68) |= 8u;
v8 = *(_DWORD *)(v7 + 60);
if ( v8 )
{
v9 = *(_DWORD *)(v7 + 60);
while ( *(_QWORD **)(curbp + 40LL) != i )
{
if ( *(_QWORD **)(v7 + 32) == i )
return 1LL;
i = (_QWORD *)*i;
if ( !--v9 )
break;
}
}
v10 = *v4;
v11 = *(_QWORD *)(v7 + 32);
if ( *v4 == v11 )
{
LABEL_20:
*(_DWORD *)(v7 + 48) = 0;
result = 1LL;
}
else
{
while ( 1 )
{
v12 = *(_DWORD *)(v7 + 80);
if ( v12 <= v8 )
break;
v11 = *(_QWORD *)(v11 + 8);
*(_QWORD *)(v7 + 32) = v11;
*(_DWORD *)(v7 + 80) = v12 - 1;
if ( v11 == v10 )
goto LABEL_20;
}
result = 1LL;
}
return result;
}

original_code:
int
backpage(int f, int n)
{
 struct line *lp, *lp2;
 if (!(f & 7)) {
  n = curwp->w_ntrows - 2;
  if (n <= 0)
   return (backline(f, 1));
 } else if (n < 0)
  return (forwpage(f | 8, -n));
 lp = lp2 = curwp->w_linep;
 while (n-- && ((lp)->l_bp) != curbp->b_headp) {
  lp = ((lp)->l_bp);
 }
 if (lp == curwp->w_linep) {
  dobeep();
  ewprintf("Beginning of buffer");
 }
 curwp->w_linep = lp;
 curwp->w_rflag |= 0x08;
 for (n = curwp->w_ntrows; n-- && lp != curbp->b_headp; lp = ((lp)->l_fp))
  if (lp == curwp->w_dotp)
   return (1);
        lp2 = ((lp2)->l_fp);
 while (curwp->w_dotp != lp2) {
                if (curwp->w_dotline <= curwp->w_ntrows)
                        return (1);
  curwp->w_dotp = ((curwp->w_dotp)->l_bp);
  curwp->w_dotline--;
 }
 curwp->w_doto = 0;
 return (1);
}

canonical_original_code:
int backpage(int f, int n)
{
 struct line *lp; struct line *lp2;
 if (!(f & 7)) {
  n = curwp->w_ntrows - 2;
  if (n <= 0)
   return (backline(f, 1));
 } else if (n < 0)
  return (forwpage(f | 8, -n));
 lp = lp2 = curwp->w_linep;
 while (n-- && ((lp)->l_bp) != curbp->b_headp) {
  lp = ((lp)->l_bp);
 }
 if (lp == curwp->w_linep) {
  dobeep();
  ewprintf("Beginning of buffer");
 }
 curwp->w_linep = lp;
 curwp->w_rflag |= 0x08;
 for (n = curwp->w_ntrows; n-- && lp != curbp->b_headp; lp = ((lp)->l_fp))
  if (lp == curwp->w_dotp)
   return (1);
        lp2 = ((lp2)->l_fp);
 while (curwp->w_dotp != lp2) {
                if (curwp->w_dotline <= curwp->w_ntrows)
                        return (1);
  curwp->w_dotp = ((curwp->w_dotp)->l_bp);
  curwp->w_dotline--;
 }
 curwp->w_doto = 0;
 return (1);
}


forwpage

decompiled_code:
__int64 __fastcall forwpage(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v4; // rcx
_QWORD *v5; // rax
__int64 v6; // rdx
int v8; // esi
_QWORD *v9; // rdi
_QWORD *v10; // rdx
_QWORD *i; // rdx

if ( (a1 & 7) != 0 )
{
if ( (int)a2 < 0 )
return backpage((unsigned int)a1 | 8, (unsigned int)-(int)a2, a3, a4);
}
else
{
LODWORD(a2) = *(_DWORD *)(curwp + 60LL) - 2;
if ( (int)a2 <= 0 )
LODWORD(a2) = 1;
}
v4 = curwp;
v5 = *(_QWORD **)(curwp + 24LL);
v6 = curbp;
while ( (_DWORD)a2 )
{
v5 = (_QWORD *)*v5;
a2 = (unsigned int)(a2 - 1);
if ( *(_QWORD **)(curbp + 40LL) == v5 )
{
dobeep(a1, a2, curbp, curwp);
ewprintf("End of buffer");
return 1LL;
}
}
*(_QWORD *)(curwp + 24LL) = v5;
*(_BYTE *)(v4 + 68) |= 8u;
v8 = *(_DWORD *)(v4 + 60);
if ( v8 )
{
v9 = *(_QWORD **)(v6 + 40);
v10 = v5;
while ( v9 != v10 )
{
if ( *(_QWORD **)(v4 + 32) == v10 )
return 1LL;
v10 = (_QWORD *)*v10;
if ( !--v8 )
break;
}
}
for ( i = *(_QWORD **)(v4 + 32); i != v5; ++*(_DWORD *)(v4 + 80) )
{
i = (_QWORD *)*i;
*(_QWORD *)(v4 + 32) = i;
}
*(_DWORD *)(v4 + 48) = 0;
return 1LL;
}

original_code:
int
forwpage(int f, int n)
{
 struct line *lp;
 if (!(f & 7)) {
  n = curwp->w_ntrows - 2;
  if (n <= 0)
   n = 1;
 } else if (n < 0)
  return (backpage(f | 8, -n));
 lp = curwp->w_linep;
 while (n--)
  if ((lp = ((lp)->l_fp)) == curbp->b_headp) {
   dobeep();
   ewprintf("End of buffer");
   return(1);
  }
 curwp->w_linep = lp;
 curwp->w_rflag |= 0x08;
 for (n = curwp->w_ntrows; n-- && lp != curbp->b_headp; lp = ((lp)->l_fp))
  if (lp == curwp->w_dotp)
   return (1);
 while (curwp->w_dotp != curwp->w_linep) {
  curwp->w_dotp = ((curwp->w_dotp)->l_fp);
  curwp->w_dotline++;
 }
 curwp->w_doto = 0;
 return (1);
}


original_code:
int
forw1page(int f, int n)
{
 if (!(f & 7)) {
  n = 1;
  f = 1;
 }
 forwpage(f | 8, n);
 return (1);
}


original_code:
int
back1page(int f, int n)
{
 if (!(f & 7)) {
  n = 1;
  f = 1;
 }
 backpage(f | 8, n);
 return (1);
}


isetmark

decompiled_code:
_DWORD *isetmark()
{
_DWORD *result; // rax

result = (_DWORD *)curwp;
*(_QWORD *)(curwp + 40LL) = *(_QWORD *)(curwp + 32LL);
result[13] = result[12];
result[21] = result[20];
return result;
}

canonical_decompiled_code:
_DWORD *func13()
{
_DWORD *result; // rax

result = (_DWORD *)curwp;
*(_QWORD *)(curwp + 40LL) = *(_QWORD *)(curwp + 32LL);
result[13] = result[12];
result[21] = result[20];
return result;
}

original_code:
void
isetmark(void)
{
 curwp->w_markp = curwp->w_dotp;
 curwp->w_marko = curwp->w_doto;
 curwp->w_markline = curwp->w_dotline;
}

canonical_original_code:
void isetmark()
{
 curwp->w_markp = curwp->w_dotp;
 curwp->w_marko = curwp->w_doto;
 curwp->w_markline = curwp->w_dotline;
}


setmark

decompiled_code:
__int64 setmark()
{
isetmark();
ewprintf("Mark set");
return 1LL;
}

canonical_decompiled_code:
__int64 func14()
{
func13();
ewprintf("Mark set");
return 1LL;
}

original_code:
int
setmark(int f, int n)
{
 isetmark();
 ewprintf("Mark set");
 return (1);
}


gotobob

original_code:
int
gotobob(int f, int n)
{
 (void) setmark(f, n);
 curwp->w_dotp = ((((curbp)->b_headp)->l_fp));
 curwp->w_doto = 0;
 curwp->w_rflag |= 0x08;
 curwp->w_dotline = 1;
 return (1);
}


gotoeob

original_code:
int
gotoeob(int f, int n)
{
 struct line *lp;
 (void) setmark(f, n);
 curwp->w_dotp = ((((curbp)->b_headp)->l_bp));
 curwp->w_doto = ((curwp->w_dotp)->l_used);
 curwp->w_dotline = curwp->w_bufp->b_lines;
 lp = curwp->w_dotp;
 n = curwp->w_ntrows - 3;
 if (n < curwp->w_bufp->b_lines && n >= 3) {
  while (n--)
   curwp->w_dotp = ((curwp->w_dotp)->l_bp);
  curwp->w_linep = curwp->w_dotp;
  curwp->w_dotp = lp;
 }
 curwp->w_rflag |= 0x08;
 return (1);
}


clearmark

decompiled_code:
__int64 clearmark()
{
__int64 v0; // rax

v0 = curwp;
if ( !*(_QWORD *)(curwp + 40LL) )
return 0LL;
*(_QWORD *)(curwp + 40LL) = 0LL;
*(_DWORD *)(v0 + 52) = 0;
*(_DWORD *)(v0 + 84) = 0;
return 1LL;
}

original_code:
int
clearmark(int f, int n)
{
 if (!curwp->w_markp)
  return (0);
 curwp->w_markp = ((void *)0);
 curwp->w_marko = 0;
 curwp->w_markline = 0;
 return (1);
}