opentty

decompiled_code:
int __fastcall opentty(const char *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // eax
int result; // eax
int i; // ebx
int j; // ebx
int *v7; // rax
char *v8; // rax

v1 = open(a1, 2050);
if ( v1 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "FATAL: can't reopen tty: %s", v8);
sleep(1u);
exit(1);
}
v2 = v1;
v3 = fcntl(v1, 3);
BYTE1(v3) &= 0xF7u;
result = fcntl(v2, 4, v3);
if ( v2 > 0 )
{
for ( i = 0; i != v2; ++i )
result = close(i);
}
for ( j = 0; j != 3; ++j )
{
if ( v2 != j )
result = dup2(v2, j);
}
if ( v2 > 2 )
result = close(v2);
return result;
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // eax
int result; // eax
int i; // ebx
int j; // ebx
int *v7; // rax
char *v8; // rax

v1 = open(a1, 2050);
if ( v1 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "FATAL: can't reopen tty: %s", v8);
sleep(1u);
exit(1);
}
v2 = v1;
v3 = fcntl(v1, 3);
BYTE1(v3) &= 0xF7u;
result = fcntl(v2, 4, v3);
if ( v2 > 0 )
{
for ( i = 0; i != v2; ++i )
result = close(i);
}
for ( j = 0; j != 3; ++j )
{
if ( v2 != j )
result = dup2(v2, j);
}
if ( v2 > 2 )
result = close(v2);
return result;
}

original_code:
static void
opentty(const char * tty) {
 int i, fd, flags;
 fd = open(tty, 02 | 04000);
 if (fd == -1) {
  syslog(3, "FATAL: can't reopen tty: %s",
         strerror((*__errno_location ())));
  sleep(1);
  exit(1);
 }
 flags = fcntl(fd, 3);
 flags &= ~04000;
 fcntl(fd, 4, flags);
 for (i = 0; i < fd; i++)
  close(i);
 for (i = 0; i < 3; i++)
  if (fd != i)
   dup2(fd, i);
 if (fd >= 3)
  close(fd);
}

canonical_original_code:
static void opentty(char *tty) {
 int i; int fd; int flags;
 fd = open(tty, 02 | 04000);
 if (fd == -1) {
  syslog(3, "FATAL: can't reopen tty: %s",
         strerror((*__errno_location ())));
  sleep(1);
  exit(1);
 }
 flags = fcntl(fd, 3);
 flags &= ~04000;
 fcntl(fd, 4, flags);
 for (i = 0; i < fd; i++)
  close(i);
 for (i = 0; i < 3; i++)
  if (fd != i)
   dup2(fd, i);
 if (fd >= 3)
  close(fd);
}

variable_types:
tty
char *

variable_types: i int

variable_types: fd int

variable_types: flags int


xstrncpy

repo: lisa-project/lisa-user

decompiled_code:
char *__fastcall xstrncpy(char *a1, const char *a2, __int64 a3)
{
char *result; // rax

result = strncpy(a1, a2, a3 - 1);
a1[a3 - 1] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func4(char *a1, const char *a2, __int64 a3)
{
char *result; // rax

result = strncpy(a1, a2, a3 - 1);
a1[a3 - 1] = 0;
return result;
}

original_code:
void
xstrncpy(char *dest, const char *src, size_t n) {
 strncpy(dest, src, n-1);
 dest[n-1] = 0;
}

canonical_original_code:
void xstrncpy(char *dest, char *src, unsigned long n) {
 strncpy(dest, src, n-1);
 dest[n-1] = 0;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
n
unsigned long

return_type: void


badlogin

decompiled_code:
void __fastcall badlogin(const char *a1)
{
if ( failures == 1 )
{
if ( hostname )
syslog(5, "LOGIN FAILURE FROM %s, %s", (const char *)hostname, a1);
else
syslog(5, "LOGIN FAILURE ON %s, %s", tty_name, a1);
}
else if ( hostname )
{
syslog(5, "%d LOGIN FAILURES FROM %s, %s", (unsigned int)failures, (const char *)hostname, a1);
}
else
{
syslog(5, "%d LOGIN FAILURES ON %s, %s", (unsigned int)failures, tty_name, a1);
}
}

canonical_decompiled_code:
void __fastcall func6(const char *a1)
{
if ( failures == 1 )
{
if ( hostname )
syslog(5, "LOGIN FAILURE FROM %s, %s", (const char *)hostname, a1);
else
syslog(5, "LOGIN FAILURE ON %s, %s", tty_name, a1);
}
else if ( hostname )
{
syslog(5, "%d LOGIN FAILURES FROM %s, %s", (unsigned int)failures, (const char *)hostname, a1);
}
else
{
syslog(5, "%d LOGIN FAILURES ON %s, %s", (unsigned int)failures, tty_name, a1);
}
}

original_code:
void
badlogin(const char *name) {
    if (failures == 1) {
 if (hostname)
   syslog(5, "LOGIN FAILURE FROM %s, %s",
   hostname, name);
 else
   syslog(5, "LOGIN FAILURE ON %s, %s",
   tty_name, name);
    } else {
 if (hostname)
   syslog(5, "%d LOGIN FAILURES FROM %s, %s",
   failures, hostname, name);
 else
   syslog(5, "%d LOGIN FAILURES ON %s, %s",
   failures, tty_name, name);
    }
}

canonical_original_code:
void badlogin(char *name) {
    if (failures == 1) {
 if (hostname)
   syslog(5, "LOGIN FAILURE FROM %s, %s",
   hostname, name);
 else
   syslog(5, "LOGIN FAILURE ON %s, %s",
   tty_name, name);
    } else {
 if (hostname)
   syslog(5, "%d LOGIN FAILURES FROM %s, %s",
   failures, hostname, name);
 else
   syslog(5, "%d LOGIN FAILURES ON %s, %s",
   failures, tty_name, name);
    }
}

variable_types:
name
char *


getloginname

decompiled_code:
int getloginname()
{
int v0; // er12
int v1; // ebx
_BYTE *v2; // rbp
int result; // eax

v0 = 51;
while ( 1 )
{
printf("\n%s login: ", thishost);
fflush(stdout);
v1 = 53;
v2 = nbuf_0;
while ( 1 )
{
result = getchar();
if ( result == 10 )
break;
if ( result == -1 )
{
badlogin("EOF");
exit(0);
}
if ( v2 < byte_10C0 )
*v2++ = result;
if ( !--v1 )
{
fwrite("login name much too long.\n", 1uLL, 0x1AuLL, stderr);
badlogin("NAME too long");
exit(0);
}
}
if ( v2 <= nbuf_0 )
goto LABEL_13;
if ( nbuf_0[0] != 45 )
break;
fwrite("login names may not start with '-'.\n", 1uLL, 0x24uLL, stderr);
LABEL_13:
if ( !--v0 )
{
fwrite("too many bare linefeeds.\n", 1uLL, 0x19uLL, stderr);
badlogin("EXCESSIVE linefeeds");
exit(0);
}
}
*v2 = 0;
username = nbuf_0;
return result;
}

canonical_decompiled_code:
int func7()
{
int v0; // er12
int v1; // ebx
_BYTE *v2; // rbp
int result; // eax

v0 = 51;
while ( 1 )
{
printf("\n%s login: ", thishost);
fflush(stdout);
v1 = 53;
v2 = nbuf_0;
while ( 1 )
{
result = getchar();
if ( result == 10 )
break;
if ( result == -1 )
{
func6("EOF");
exit(0);
}
if ( v2 < byte_10C0 )
*v2++ = result;
if ( !--v1 )
{
fwrite("login name much too long.\n", 1uLL, 0x1AuLL, stderr);
func6("NAME too long");
exit(0);
}
}
if ( v2 <= nbuf_0 )
goto LABEL_13;
if ( nbuf_0[0] != 45 )
break;
fwrite("login names may not start with '-'.\n", 1uLL, 0x24uLL, stderr);
LABEL_13:
if ( !--v0 )
{
fwrite("too many bare linefeeds.\n", 1uLL, 0x19uLL, stderr);
func6("EXCESSIVE linefeeds");
exit(0);
}
}
*v2 = 0;
username = nbuf_0;
return result;
}

original_code:
static void
getloginname(void) {
    int ch, cnt, cnt2;
    char *p;
    static char nbuf[32 + 1];
    cnt2 = 0;
    for (;;) {
 cnt = 0;
 printf("\n%s login: ", thishost); fflush(stdout);
 for (p = nbuf; (ch = getchar()) != '\n'; ) {
     if (ch == (-1)) {
  badlogin("EOF");
  exit(0);
     }
     if (p < nbuf + 32)
       *p++ = ch;
     cnt++;
     if (cnt > 32 + 20) {
  fprintf(stderr, "login name much too long.\n");
  badlogin("NAME too long");
  exit(0);
     }
 }
 if (p > nbuf) {
   if (nbuf[0] == '-')
     fprintf(stderr,
      "login names may not start with '-'.\n");
   else {
       *p = '\0';
       username = nbuf;
       break;
   }
 }
 cnt2++;
 if (cnt2 > 50) {
     fprintf(stderr, "too many bare linefeeds.\n");
     badlogin("EXCESSIVE linefeeds");
     exit(0);
 }
    }
}

canonical_original_code:
static void getloginname() {
    int ch; int cnt; int cnt2;
    char *p;
    static char nbuf[-1];
    cnt2 = 0;
    for (;;) {
 cnt = 0;
 printf("\n%s login: ", thishost); fflush(stdout);
 for (p = nbuf; (ch = getchar()) != '\n'; ) {
     if (ch == (-1)) {
  badlogin("EOF");
  exit(0);
     }
     if (p < nbuf + 32)
       *p++ = ch;
     cnt++;
     if (cnt > 32 + 20) {
  fprintf(stderr, "login name much too long.\n");
  badlogin("NAME too long");
  exit(0);
     }
 }
 if (p > nbuf) {
   if (nbuf[0] == '-')
     fprintf(stderr,
      "login names may not start with '-'.\n");
   else {
       *p = '\0';
       username = nbuf;
       break;
   }
 }
 cnt2++;
 if (cnt2 > 50) {
     fprintf(stderr, "too many bare linefeeds.\n");
     badlogin("EXCESSIVE linefeeds");
     exit(0);
 }
    }
}

variable_types: ch int

variable_types: cnt int

variable_types:
p
char *

variable_types:
nbuf
char [-1]


sleepexit

decompiled_code:
void __fastcall __noreturn sleepexit(int status)
{
sleep(5u);
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(int status)
{
sleep(5u);
exit(status);
}

original_code:
void
sleepexit(int eval) {
    sleep(5);
    exit(eval);
}

variable_types: eval int


checknologin

decompiled_code:
int checknologin()
{
int result; // eax
int v1; // ebx
int v2; // eax
int v3; // ebp
_BYTE v4[8216]; // [rsp+0h] [rbp-2018h] BYREF

result = open("/etc/nologin", 0, 0LL);
if ( result >= 0 )
{
v1 = result;
while ( 1 )
{
v3 = read(v1, v4, 0x2000uLL);
if ( v3 <= 0 )
break;
v2 = fileno(stdout);
write(v2, v4, v3);
}
close(v1);
sleepexit(0);
}
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax
int v1; // ebx
int v2; // eax
int v3; // ebp
_BYTE v4[8216]; // [rsp+0h] [rbp-2018h] BYREF

result = open("/etc/nologin", 0, 0LL);
if ( result >= 0 )
{
v1 = result;
while ( 1 )
{
v3 = read(v1, v4, 0x2000uLL);
if ( v3 <= 0 )
break;
v2 = fileno(stdout);
write(v2, v4, v3);
}
close(v1);
func8(0);
}
return result;
}

original_code:
void
checknologin(void) {
    int fd, nchars;
    char tbuf[8192];
    if ((fd = open("/etc/nologin", 00, 0)) >= 0) {
 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
   write(fileno(stdout), tbuf, nchars);
 close(fd);
 sleepexit(0);
    }
}

canonical_original_code:
void checknologin() {
    int fd; int nchars;
    char tbuf[8192];
    if ((fd = open("/etc/nologin", 00, 0)) >= 0) {
 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
   write(fileno(stdout), tbuf, nchars);
 close(fd);
 sleepexit(0);
    }
}

variable_types: nchars int

variable_types:
tbuf
char [8192]


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
int v4; // er13
char *v5; // rax
char *v6; // r15
struct hostent *v7; // rax
int **v8; // rax
int *v9; // rax
int v10; // eax
char *v11; // rbx
int v12; // ebp
int v13; // ebx
char *v14; // rax
char *v15; // r15
int v16; // edx
char *v17; // rax
char *v18; // r12
struct stat *v19; // rdi
__int64 v20; // rcx
struct termios *v21; // rsi
int v22; // er12
char v23; // bl
const char *v24; // r14
__int64 v25; // rax
char *v26; // rbp
char *v27; // r14
size_t v28; // rax
struct passwd *v29; // rax
char *v30; // rax
const char *v31; // rbx
int v32; // eax
char *v33; // rax
__uid_t v34; // edx
int v35; // ecx
char *v36; // rax
char *v37; // rbx
const char *v38; // rbx
const char *v39; // rbx
char *v40; // rax
__int64 v41; // rbp
size_t v42; // rax
char *v43; // rax
char *v44; // rbx
int v45; // ebx
char *v46; // rax
int v47; // [rsp+Ch] [rbp-8FCh]
struct termios termios_p; // [rsp+10h] [rbp-8F8h] BYREF
struct stat v49[7]; // [rsp+50h] [rbp-8B8h] BYREF
char *file; // [rsp+450h] [rbp-4B8h]
char *argva[9]; // [rsp+458h] [rbp-4B0h] BYREF
char s[32]; // [rsp+4A0h] [rbp-468h] BYREF
char name; // [rsp+4C0h] [rbp-448h] BYREF
char v54[1095]; // [rsp+4C1h] [rbp-447h] BYREF

getpid();
signal(14, (__sighandler_t)timedout);
alarm(0x1770u);
signal(3, (__sighandler_t)((char *)timedout2 + 1));
signal(2, (__sighandler_t)((char *)timedout2 + 1));
setpriority(PRIO_PROCESS, 0, 0);
gethostname(&name, 0x402uLL);
xstrncpy(thishost, &name, 100LL);
v3 = strchr(&name, 46);
hostname = 0LL;
tty_name = 0LL;
username = 0LL;
v47 = 0;
v4 = 0;
while ( 1 )
{
v10 = getopt(argc, (char *const *)argv, "fh:p");
if ( v10 == -1 )
break;
switch ( v10 )
{
case 'h':
if ( getuid() )
{
fwrite("login: -h for super-user only.\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
if ( v3 )
{
v5 = strchr(optarg, 46);
v6 = v5;
if ( v5 )
{
if ( !strcasecmp(v5, v3) )
*v6 = 0;
}
}
hostname = (__int64)strdup(optarg);
v7 = gethostbyname((const char *)hostname);
LOBYTE(hostaddress) = 0;
if ( v7 )
{
v8 = (int **)v7->h_addr_list;
if ( v8 )
{
v9 = *v8;
if ( v9 )
hostaddress = *v9;
}
}
break;
case 'p':
v47 = 1;
break;
case 'f':
v4 = 1;
break;
default:
fwrite("usage: login [-fp] [username]\n", 1uLL, 0x1EuLL, stderr);
exit(1);
}
}
v11 = (char *)argv[optind];
v12 = 1;
if ( v11 )
{
username = strdup(v11);
while ( *v11 )
*v11++ = 32;
v12 = 0;
}
v13 = getdtablesize();
if ( v13 > 2 )
{
do
close(v13--);
while ( v13 != 2 );
}
v14 = ttyname(0);
v15 = v14;
if ( !v14 || !*v14 )
{
sprintf(s, "%s??", "/dev/tty");
v15 = s;
}
check_ttyname(v15);
v16 = strncmp(v15, "/dev/", 5uLL);
v17 = v15;
if ( !v16 )
v17 = v15 + 5;
tty_name = v17;
v18 = v15;
if ( strncmp(v15, "/dev/tty", 8uLL) )
{
while ( *v18 )
{
v23 = *v18;
if ( ((*_ctype_b_loc())[v23] & 0x800) != 0 )
break;
++v18;
}
}
setpgrp();
tcgetattr(0, &termios_p);
v19 = v49;
v20 = 15LL;
v21 = &termios_p;
while ( v20 )
{
LODWORD(v19->st_dev) = v21->c_iflag;
v21 = (struct termios *)((char *)v21 + 4);
v19 = (struct stat *)((char *)v19 + 4);
--v20;
}
LODWORD(v49[0].st_ino) &= 0xFFFFFBFF;
chown(v15, 0, 0);
chmod(v15, 0x180u);
tcsetattr(0, 2, (const struct termios *)v49);
signal(1, (__sighandler_t)((char *)timedout2 + 1));
signal(1, 0LL);
opentty(v15);
tcsetattr(0, 2, &termios_p);
openlog("login", 4, 80);
v22 = 0;
while ( 1 )
{
if ( v12 )
{
getloginname();
v4 = 0;
}
if ( *username == 43 )
{
puts("Illegal username");
badlogin(username);
sleepexit(1);
}
v29 = getpwnam(username);
pwd = (__int64)v29;
if ( !v29 )
break;
v24 = v29->pw_passwd;
initgroups(username, v29->pw_gid);
if ( !pwd || *(_DWORD *)(pwd + 16) )
goto LABEL_56;
v25 = pwd;
if ( v4 )
goto LABEL_76;
LABEL_43:
if ( !**(_BYTE **)(v25 + 8) )
goto LABEL_79;
LABEL_44:
setpriority(PRIO_PROCESS, 0, -4);
v26 = getpass("Password: ");
v27 = crypt(v26, v24);
setpriority(PRIO_PROCESS, 0, 0);
v28 = strlen(v26);
memset(v26, 0, v28);
if ( pwd && !strcmp(v27, *(const char **)(pwd + 8)) )
goto LABEL_79;
puts("Login incorrect");
badlogin(username);
++failures;
if ( ++v22 > 3 )
{
if ( v22 > 9 )
sleepexit(1);
sleep(5 * v22 - 15);
}
v12 = 1;
}
v24 = "xx";
LABEL_56:
checknologin();
if ( !v4 )
{
v25 = pwd;
if ( !pwd )
goto LABEL_44;
goto LABEL_43;
}
if ( !pwd )
goto LABEL_44;
LABEL_76:
v34 = getuid();
v25 = pwd;
v35 = *(_DWORD *)(pwd + 16);
if ( v35 && (v34 == v35 || !v34) )
{
LABEL_79:
alarm(0);
endpwent();
dolastlog(0);
chown(v15, *(_DWORD *)(pwd + 16), *(_DWORD *)(pwd + 20));
chmod(v15, 0x180u);
setgid(*(_DWORD *)(pwd + 20));
if ( !**(_BYTE **)(pwd + 40) )
*(_QWORD *)(pwd + 40) = "/bin/sh";
v36 = getenv("TERM");
v37 = "dumb";
if ( v36 )
{
v37 = strdup(v36);
if ( !v37 )
v37 = "dumb";
}
if ( !v47 )
{
environ = (char **)malloc(8uLL);
*environ = 0LL;
}
setenv("HOME", *(const char **)(pwd + 32), 0);
if ( *(_DWORD *)(pwd + 16) )
setenv("PATH", "/usr/bin:/bin", 1);
else
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
setenv("SHELL", *(const char **)(pwd + 40), 1);
setenv("TERM", v37, 1);
v38 = *(const char **)pwd;
if ( strlen(*(const char **)pwd) + 11 <= 0x3FF )
{
sprintf((char *)v49, "%s/%s", "/var/mail", v38);
setenv("MAIL", (const char *)v49, 0);
}
setenv("LOGNAME", *(const char **)pwd, 1);
v39 = tty_name;
if ( !strncmp(tty_name, "ttyS", 4uLL) )
syslog(6, "DIALUP AT %s BY %s", v39, *(const char **)pwd);
if ( *(_DWORD *)(pwd + 16) )
{
if ( hostname )
syslog(6, "LOGIN ON %s BY %s FROM %s", tty_name, *(const char **)pwd, (const char *)hostname);
else
syslog(6, "LOGIN ON %s BY %s", tty_name, *(const char **)pwd);
}
else if ( hostname )
{
syslog(5, "ROOT LOGIN ON %s FROM %s", tty_name, (const char *)hostname);
}
else
{
syslog(5, "ROOT LOGIN ON %s", tty_name);
}
v40 = getenv("MAIL");
if ( v40 && !stat(v40, v49) && v49[0].st_size )
{
if ( v49[0].st_mtim.tv_sec <= v49[0].st_atim.tv_sec )
puts("You have mail.");
else
puts("You have new mail.");
}
signal(14, 0LL);
signal(3, 0LL);
signal(20, (__sighandler_t)((char *)timedout2 + 1));
signal(2, 0LL);
if ( setuid(*(_DWORD *)(pwd + 16)) >= 0 || !*(_DWORD *)(pwd + 16) )
{
if ( chdir(*(const char **)(pwd + 32)) < 0 )
{
printf("No directory %s!\n", *(const char **)(pwd + 32));
if ( chdir("/") )
exit(0);
*(_QWORD *)(pwd + 32) = "/";
puts("Logging in with home = \"/\".");
}
v41 = pwd;
v31 = *(const char **)(pwd + 40);
if ( strchr(v31, 32) )
{
v42 = strlen(v31);
v43 = (char *)malloc(v42 + 6);
v44 = v43;
if ( !v43 )
{
fwrite("login: no memory for shell script.\n", 1uLL, 0x23uLL, stderr);
exit(0);
}
strcpy(v43, "exec ");
strcat(v43, *(const char **)(v41 + 40));
file = "/bin/sh";
argva[0] = "-sh";
argva[1] = "-c";
argva[2] = v44;
v32 = 4;
}
else
{
name = 45;
v30 = strrchr(v31, 47);
if ( v30 )
v31 = v30 + 1;
xstrncpy(v54, v31, 1025LL);
file = *(char **)(pwd + 40);
argva[0] = &name;
v32 = 2;
}
(&file)[v32] = 0LL;
execvp(file, argva);
v45 = *_errno_location();
if ( !strcmp(file, "/bin/sh") )
{
v46 = strerror(v45);
fprintf(stderr, "login: couldn't exec shell script: %s.\n", v46);
}
else
{
v33 = strerror(v45);
fprintf(stderr, "login: no shell: %s.\n", v33);
}
exit(0);
}
syslog(1, "setuid() failed");
exit(1);
}
goto LABEL_43;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
int v4; // er13
char *v5; // rax
char *v6; // r15
struct hostent *v7; // rax
int **v8; // rax
int *v9; // rax
int v10; // eax
char *v11; // rbx
int v12; // ebp
int v13; // ebx
char *v14; // rax
char *v15; // r15
int v16; // edx
char *v17; // rax
char *v18; // r12
struct stat *v19; // rdi
__int64 v20; // rcx
struct termios *v21; // rsi
int v22; // er12
char v23; // bl
const char *v24; // r14
__int64 v25; // rax
char *v26; // rbp
char *v27; // r14
size_t v28; // rax
struct passwd *v29; // rax
char *v30; // rax
const char *v31; // rbx
int v32; // eax
char *v33; // rax
__uid_t v34; // edx
int v35; // ecx
char *v36; // rax
char *v37; // rbx
const char *v38; // rbx
const char *v39; // rbx
char *v40; // rax
__int64 v41; // rbp
size_t v42; // rax
char *v43; // rax
char *v44; // rbx
int v45; // ebx
char *v46; // rax
int v47; // [rsp+Ch] [rbp-8FCh]
struct termios termios_p; // [rsp+10h] [rbp-8F8h] BYREF
struct stat v49[7]; // [rsp+50h] [rbp-8B8h] BYREF
char *file; // [rsp+450h] [rbp-4B8h]
char *argva[9]; // [rsp+458h] [rbp-4B0h] BYREF
char s[32]; // [rsp+4A0h] [rbp-468h] BYREF
char name; // [rsp+4C0h] [rbp-448h] BYREF
char v54[1095]; // [rsp+4C1h] [rbp-447h] BYREF

getpid();
signal(14, (__sighandler_t)timedout);
alarm(0x1770u);
signal(3, (__sighandler_t)((char *)timedout2 + 1));
signal(2, (__sighandler_t)((char *)timedout2 + 1));
setpriority(PRIO_PROCESS, 0, 0);
gethostname(&name, 0x402uLL);
func4(thishost, &name, 100LL);
v3 = strchr(&name, 46);
hostname = 0LL;
tty_name = 0LL;
username = 0LL;
v47 = 0;
v4 = 0;
while ( 1 )
{
v10 = getopt(argc, (char *const *)argv, "fh:p");
if ( v10 == -1 )
break;
switch ( v10 )
{
case 'h':
if ( getuid() )
{
fwrite("login: -h for super-user only.\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
if ( v3 )
{
v5 = strchr(optarg, 46);
v6 = v5;
if ( v5 )
{
if ( !strcasecmp(v5, v3) )
*v6 = 0;
}
}
hostname = (__int64)strdup(optarg);
v7 = gethostbyname((const char *)hostname);
LOBYTE(hostaddress) = 0;
if ( v7 )
{
v8 = (int **)v7->h_addr_list;
if ( v8 )
{
v9 = *v8;
if ( v9 )
hostaddress = *v9;
}
}
break;
case 'p':
v47 = 1;
break;
case 'f':
v4 = 1;
break;
default:
fwrite("usage: login [-fp] [username]\n", 1uLL, 0x1EuLL, stderr);
exit(1);
}
}
v11 = (char *)argv[optind];
v12 = 1;
if ( v11 )
{
username = strdup(v11);
while ( *v11 )
*v11++ = 32;
v12 = 0;
}
v13 = getdtablesize();
if ( v13 > 2 )
{
do
close(v13--);
while ( v13 != 2 );
}
v14 = ttyname(0);
v15 = v14;
if ( !v14 || !*v14 )
{
sprintf(s, "%s??", "/dev/tty");
v15 = s;
}
func2(v15);
v16 = strncmp(v15, "/dev/", 5uLL);
v17 = v15;
if ( !v16 )
v17 = v15 + 5;
tty_name = v17;
v18 = v15;
if ( strncmp(v15, "/dev/tty", 8uLL) )
{
while ( *v18 )
{
v23 = *v18;
if ( ((*_ctype_b_loc())[v23] & 0x800) != 0 )
break;
++v18;
}
}
setpgrp();
tcgetattr(0, &termios_p);
v19 = v49;
v20 = 15LL;
v21 = &termios_p;
while ( v20 )
{
LODWORD(v19->st_dev) = v21->c_iflag;
v21 = (struct termios *)((char *)v21 + 4);
v19 = (struct stat *)((char *)v19 + 4);
--v20;
}
LODWORD(v49[0].st_ino) &= 0xFFFFFBFF;
chown(v15, 0, 0);
chmod(v15, 0x180u);
tcsetattr(0, 2, (const struct termios *)v49);
signal(1, (__sighandler_t)((char *)timedout2 + 1));
signal(1, 0LL);
func3(v15);
tcsetattr(0, 2, &termios_p);
openlog("login", 4, 80);
v22 = 0;
while ( 1 )
{
if ( v12 )
{
func7();
v4 = 0;
}
if ( *username == 43 )
{
puts("Illegal username");
func6(username);
func8(1);
}
v29 = getpwnam(username);
pwd = (__int64)v29;
if ( !v29 )
break;
v24 = v29->pw_passwd;
initgroups(username, v29->pw_gid);
if ( !pwd || *(_DWORD *)(pwd + 16) )
goto LABEL_56;
v25 = pwd;
if ( v4 )
goto LABEL_76;
LABEL_43:
if ( !**(_BYTE **)(v25 + 8) )
goto LABEL_79;
LABEL_44:
setpriority(PRIO_PROCESS, 0, -4);
v26 = getpass("Password: ");
v27 = crypt(v26, v24);
setpriority(PRIO_PROCESS, 0, 0);
v28 = strlen(v26);
memset(v26, 0, v28);
if ( pwd && !strcmp(v27, *(const char **)(pwd + 8)) )
goto LABEL_79;
puts("Login incorrect");
func6(username);
++failures;
if ( ++v22 > 3 )
{
if ( v22 > 9 )
func8(1);
sleep(5 * v22 - 15);
}
v12 = 1;
}
v24 = "xx";
LABEL_56:
func9();
if ( !v4 )
{
v25 = pwd;
if ( !pwd )
goto LABEL_44;
goto LABEL_43;
}
if ( !pwd )
goto LABEL_44;
LABEL_76:
v34 = getuid();
v25 = pwd;
v35 = *(_DWORD *)(pwd + 16);
if ( v35 && (v34 == v35 || !v34) )
{
LABEL_79:
alarm(0);
endpwent();
func5(0);
chown(v15, *(_DWORD *)(pwd + 16), *(_DWORD *)(pwd + 20));
chmod(v15, 0x180u);
setgid(*(_DWORD *)(pwd + 20));
if ( !**(_BYTE **)(pwd + 40) )
*(_QWORD *)(pwd + 40) = "/bin/sh";
v36 = getenv("TERM");
v37 = "dumb";
if ( v36 )
{
v37 = strdup(v36);
if ( !v37 )
v37 = "dumb";
}
if ( !v47 )
{
environ = (char **)malloc(8uLL);
*environ = 0LL;
}
setenv("HOME", *(const char **)(pwd + 32), 0);
if ( *(_DWORD *)(pwd + 16) )
setenv("PATH", "/usr/bin:/bin", 1);
else
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
setenv("SHELL", *(const char **)(pwd + 40), 1);
setenv("TERM", v37, 1);
v38 = *(const char **)pwd;
if ( strlen(*(const char **)pwd) + 11 <= 0x3FF )
{
sprintf((char *)v49, "%s/%s", "/var/mail", v38);
setenv("MAIL", (const char *)v49, 0);
}
setenv("LOGNAME", *(const char **)pwd, 1);
v39 = tty_name;
if ( !strncmp(tty_name, "ttyS", 4uLL) )
syslog(6, "DIALUP AT %s BY %s", v39, *(const char **)pwd);
if ( *(_DWORD *)(pwd + 16) )
{
if ( hostname )
syslog(6, "LOGIN ON %s BY %s FROM %s", tty_name, *(const char **)pwd, (const char *)hostname);
else
syslog(6, "LOGIN ON %s BY %s", tty_name, *(const char **)pwd);
}
else if ( hostname )
{
syslog(5, "ROOT LOGIN ON %s FROM %s", tty_name, (const char *)hostname);
}
else
{
syslog(5, "ROOT LOGIN ON %s", tty_name);
}
v40 = getenv("MAIL");
if ( v40 && !stat(v40, v49) && v49[0].st_size )
{
if ( v49[0].st_mtim.tv_sec <= v49[0].st_atim.tv_sec )
puts("You have mail.");
else
puts("You have new mail.");
}
signal(14, 0LL);
signal(3, 0LL);
signal(20, (__sighandler_t)((char *)timedout2 + 1));
signal(2, 0LL);
if ( setuid(*(_DWORD *)(pwd + 16)) >= 0 || !*(_DWORD *)(pwd + 16) )
{
if ( chdir(*(const char **)(pwd + 32)) < 0 )
{
printf("No directory %s!\n", *(const char **)(pwd + 32));
if ( chdir("/") )
exit(0);
*(_QWORD *)(pwd + 32) = "/";
puts("Logging in with home = \"/\".");
}
v41 = pwd;
v31 = *(const char **)(pwd + 40);
if ( strchr(v31, 32) )
{
v42 = strlen(v31);
v43 = (char *)malloc(v42 + 6);
v44 = v43;
if ( !v43 )
{
fwrite("login: no memory for shell script.\n", 1uLL, 0x23uLL, stderr);
exit(0);
}
strcpy(v43, "exec ");
strcat(v43, *(const char **)(v41 + 40));
file = "/bin/sh";
argva[0] = "-sh";
argva[1] = "-c";
argva[2] = v44;
v32 = 4;
}
else
{
name = 45;
v30 = strrchr(v31, 47);
if ( v30 )
v31 = v30 + 1;
func4(v54, v31, 1025LL);
file = *(char **)(pwd + 40);
argva[0] = &name;
v32 = 2;
}
(&file)[v32] = 0LL;
execvp(file, argva);
v45 = *_errno_location();
if ( !strcmp(file, "/bin/sh") )
{
v46 = strerror(v45);
fprintf(stderr, "login: couldn't exec shell script: %s.\n", v46);
}
else
{
v33 = strerror(v45);
fprintf(stderr, "login: no shell: %s.\n", v33);
}
exit(0);
}
syslog(1, "setuid() failed");
exit(1);
}
goto LABEL_43;
}

original_code:
int
main(int argc, char **argv)
{
    extern int optind;
    extern char *optarg, **environ;
    register int ch;
    register char *p;
    int ask, fflag, hflag, pflag, cnt, errsv;
    int quietlog, passwd_req;
    char *domain, *ttyn;
    char tbuf[1024 + 2], tname[sizeof("/dev/tty") + 10];
    char *termenv;
    char *childArgv[10];
    char *buff;
    int childArgc = 0;
    char *salt, *pp;
    pid = getpid();
    signal(14, timedout);
    alarm((unsigned int)timeout);
    signal(3, ((__sighandler_t) 1));
    signal(2, ((__sighandler_t) 1));
    setpriority(PRIO_PROCESS, 0, 0);
    gethostname(tbuf, sizeof(tbuf));
    xstrncpy(thishost, tbuf, sizeof(thishost));
    domain = strchr(tbuf, '.');
    username = tty_name = hostname = ((void *)0);
    fflag = hflag = pflag = 0;
    passwd_req = 1;
    while ((ch = getopt(argc, argv, "fh:p")) != -1)
      switch (ch) {
 case 'f':
   fflag = 1;
   break;
 case 'h':
   if (getuid()) {
       fprintf(stderr,
        "login: -h for super-user only.\n");
       exit(1);
   }
   hflag = 1;
   if (domain && (p = strchr(optarg, '.')) &&
       strcasecmp(p, domain) == 0)
     *p = 0;
   hostname = strdup(optarg);
   {
    struct hostent *he = gethostbyname(hostname);
    hostaddress[0] = 0;
    if (he && he->h_addr_list && he->h_addr_list[0])
     memcpy(hostaddress, he->h_addr_list[0],
     sizeof(hostaddress));
   }
   break;
 case 'p':
   pflag = 1;
   break;
 case '?':
 default:
   fprintf(stderr,
    "usage: login [-fp] [username]\n");
   exit(1);
      }
    argc -= optind;
    argv += optind;
    if (*argv) {
 char *p = *argv;
 username = strdup(p);
 ask = 0;
 while(*p)
     *p++ = ' ';
    } else
        ask = 1;
    for (cnt = getdtablesize(); cnt > 2; cnt--)
      close(cnt);
    ttyn = ttyname(0);
    if (ttyn == ((void *)0) || *ttyn == '\0') {
 sprintf(tname, "%s??", "/dev/tty");
 ttyn = tname;
    }
    check_ttyname(ttyn);
    if (strncmp(ttyn, "/dev/", 5) == 0)
 tty_name = ttyn+5;
    else
 tty_name = ttyn;
    if (strncmp(ttyn, "/dev/tty", 8) == 0)
 tty_number = ttyn+8;
    else {
 char *p = ttyn;
 while (*p && !((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit)) p++;
 tty_number = p;
    }
    setpgrp();
    {
 struct termios tt, ttt;
 tcgetattr(0, &tt);
 ttt = tt;
 ttt.c_cflag &= ~0002000;
 chown(ttyn, 0, 0);
 chmod(ttyn, 0600);
 tcsetattr(0,2,&ttt);
 signal(1, ((__sighandler_t) 1));
 signal(1, ((__sighandler_t) 0));
 opentty(ttyn);
 tcsetattr(0,2,&tt);
    }
    openlog("login", 0x04, (10<<3));
    for (cnt = 0;; ask = 1) {
 if (ask) {
     fflag = 0;
     getloginname();
 }
 if (username[0] == '+') {
     puts("Illegal username");
     badlogin(username);
     sleepexit(1);
 }
 if ((pwd = getpwnam(username))) {
     salt = pwd->pw_passwd;
 } else
   salt = (char *) "xx";
 if (pwd) {
     initgroups(username, pwd->pw_gid);
 }
 if (pwd == ((void *)0) || pwd->pw_uid)
   checknologin();
 if (fflag && pwd) {
     int uid = getuid();
     passwd_req = pwd->pw_uid == 0 ||
       (uid && uid != pwd->pw_uid);
 }
 if (!passwd_req || (pwd && !*pwd->pw_passwd))
   break;
 setpriority(PRIO_PROCESS, 0, -4);
 pp = getpass("Password: ");
 p = crypt(pp, salt);
 setpriority(PRIO_PROCESS, 0, 0);
 memset(pp, 0, strlen(pp));
 if (pwd && !strcmp(p, pwd->pw_passwd))
   break;
 printf("Login incorrect\n");
 badlogin(username);
 failures++;
 if (++cnt > 3) {
     if (cnt >= 10) {
  sleepexit(1);
     }
     sleep((unsigned int)((cnt - 3) * 5));
 }
    }
    alarm((unsigned int)0);
    endpwent();
 quietlog = 0;
    dolastlog(quietlog);
    chown(ttyn, pwd->pw_uid, pwd->pw_gid);
    chmod(ttyn, 0600);
    setgid(pwd->pw_gid);
    if (*pwd->pw_shell == '\0')
      pwd->pw_shell = (char *)"/bin/sh";
    {
 char *ep;
 if(!((ep = getenv("TERM")) && (termenv = strdup(ep))))
   termenv = (char *) "dumb";
    }
    if (!pflag)
      {
          environ = (char**)malloc(sizeof(char*));
   memset(environ, 0, sizeof(char*));
      }
    setenv("HOME", pwd->pw_dir, 0);
    if(pwd->pw_uid)
      setenv("PATH", "/usr/bin:/bin", 1);
    else
      setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
    setenv("SHELL", pwd->pw_shell, 1);
    setenv("TERM", termenv, 1);
    {
      char tmp[1024];
      if (sizeof("/var/mail") + strlen(pwd->pw_name) + 1 < 1024) {
       sprintf(tmp, "%s/%s", "/var/mail", pwd->pw_name);
       setenv("MAIL",tmp,0);
      }
    }
    setenv("LOGNAME", pwd->pw_name, 1);
    if (!strncmp(tty_name, "ttyS", 4))
      syslog(6, "DIALUP AT %s BY %s", tty_name, pwd->pw_name);
    if (pwd->pw_uid == 0) {
 if (hostname)
   syslog(5, "ROOT LOGIN ON %s FROM %s",
   tty_name, hostname);
 else
   syslog(5, "ROOT LOGIN ON %s", tty_name);
    } else {
 if (hostname)
   syslog(6, "LOGIN ON %s BY %s FROM %s", tty_name,
   pwd->pw_name, hostname);
 else
   syslog(6, "LOGIN ON %s BY %s", tty_name,
   pwd->pw_name);
    }
    if (!quietlog) {
 struct stat st;
 char *mail;
 mail = getenv("MAIL");
 if (mail && stat(mail, &st) == 0 && st.st_size != 0) {
  if (st.st_mtim.tv_sec > st.st_atim.tv_sec)
   printf("You have new mail.\n");
  else
   printf("You have mail.\n");
 }
    }
    signal(14, ((__sighandler_t) 0));
    signal(3, ((__sighandler_t) 0));
    signal(20, ((__sighandler_t) 1));
    signal(2, ((__sighandler_t) 0));
    if(setuid(pwd->pw_uid) < 0 && pwd->pw_uid) {
 syslog(1, "setuid() failed");
 exit(1);
    }
    if (chdir(pwd->pw_dir) < 0) {
 printf("No directory %s!\n", pwd->pw_dir);
 if (chdir("/"))
   exit(0);
 pwd->pw_dir = (char *) "/";
 printf("Logging in with home = \"/\".\n");
    }
    if (strchr(pwd->pw_shell, ' ')) {
 buff = malloc(strlen(pwd->pw_shell) + 6);
 if (!buff) {
     fprintf(stderr, "login: no memory for shell script.\n");
     exit(0);
 }
 strcpy(buff, "exec ");
 strcat(buff, pwd->pw_shell);
 childArgv[childArgc++] = (char *) "/bin/sh";
 childArgv[childArgc++] = (char *) "-sh";
 childArgv[childArgc++] = (char *) "-c";
 childArgv[childArgc++] = buff;
    } else {
 tbuf[0] = '-';
 xstrncpy(tbuf + 1, ((p = strrchr(pwd->pw_shell, '/')) ?
      p + 1 : pwd->pw_shell),
  sizeof(tbuf)-1);
 childArgv[childArgc++] = pwd->pw_shell;
 childArgv[childArgc++] = tbuf;
    }
    childArgv[childArgc++] = ((void *)0);
    execvp(childArgv[0], childArgv + 1);
    errsv = (*__errno_location ());
    if (!strcmp(childArgv[0], "/bin/sh"))
 fprintf(stderr, "login: couldn't exec shell script: %s.\n",
  strerror(errsv));
    else
 fprintf(stderr, "login: no shell: %s.\n", strerror(errsv));
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    extern int optind;
    extern char *optarg; extern char **environ;
    register int ch;
    register char *p;
    int ask; int fflag; int hflag; int pflag; int cnt; int errsv;
    int quietlog; int passwd_req;
    char *domain; char *ttyn;
    char tbuf[-1]; char tname[-1];
    char *termenv;
    char *childArgv[10];
    char *buff;
    int childArgc = 0;
    char *salt; char *pp;
    pid = getpid();
    signal(14, timedout);
    alarm((unsigned int)timeout);
    signal(3, ((void (*)(int)) 1));
    signal(2, ((void (*)(int)) 1));
    setpriority(PRIO_PROCESS, 0, 0);
    gethostname(tbuf, sizeof(tbuf));
    xstrncpy(thishost, tbuf, sizeof(thishost));
    domain = strchr(tbuf, '.');
    username = tty_name = hostname = ((void *)0);
    fflag = hflag = pflag = 0;
    passwd_req = 1;
    while ((ch = getopt(argc, argv, "fh:p")) != -1)
      switch (ch) {
 case 'f':
   fflag = 1;
   break;
 case 'h':
   if (getuid()) {
       fprintf(stderr,
        "login: -h for super-user only.\n");
       exit(1);
   }
   hflag = 1;
   if (domain && (p = strchr(optarg, '.')) &&
       strcasecmp(p, domain) == 0)
     *p = 0;
   hostname = strdup(optarg);
   {
    struct hostent *he = gethostbyname(hostname);
    hostaddress[0] = 0;
    if (he && he->h_addr_list && he->h_addr_list[0])
     memcpy(hostaddress, he->h_addr_list[0],
     sizeof(hostaddress));
   }
   break;
 case 'p':
   pflag = 1;
   break;
 case '?':
 default:
   fprintf(stderr,
    "usage: login [-fp] [username]\n");
   exit(1);
      }
    argc -= optind;
    argv += optind;
    if (*argv) {
 char *p = *argv;
 username = strdup(p);
 ask = 0;
 while(*p)
     *p++ = ' ';
    } else
        ask = 1;
    for (cnt = getdtablesize(); cnt > 2; cnt--)
      close(cnt);
    ttyn = ttyname(0);
    if (ttyn == ((void *)0) || *ttyn == '\0') {
 sprintf(tname, "%s??", "/dev/tty");
 ttyn = tname;
    }
    check_ttyname(ttyn);
    if (strncmp(ttyn, "/dev/", 5) == 0)
 tty_name = ttyn+5;
    else
 tty_name = ttyn;
    if (strncmp(ttyn, "/dev/tty", 8) == 0)
 tty_number = ttyn+8;
    else {
 char *p = ttyn;
 while (*p && !((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit)) p++;
 tty_number = p;
    }
    setpgrp();
    {
 struct termios tt; struct termios ttt;
 tcgetattr(0, &tt);
 ttt = tt;
 ttt.c_cflag &= ~0002000;
 chown(ttyn, 0, 0);
 chmod(ttyn, 0600);
 tcsetattr(0,2,&ttt);
 signal(1, ((void (*)(int)) 1));
 signal(1, ((void (*)(int)) 0));
 opentty(ttyn);
 tcsetattr(0,2,&tt);
    }
    openlog("login", 0x04, (10<<3));
    for (cnt = 0;; ask = 1) {
 if (ask) {
     fflag = 0;
     getloginname();
 }
 if (username[0] == '+') {
     puts("Illegal username");
     badlogin(username);
     sleepexit(1);
 }
 if ((pwd = getpwnam(username))) {
     salt = pwd->pw_passwd;
 } else
   salt = (char *) "xx";
 if (pwd) {
     initgroups(username, pwd->pw_gid);
 }
 if (pwd == ((void *)0) || pwd->pw_uid)
   checknologin();
 if (fflag && pwd) {
     int uid = getuid();
     passwd_req = pwd->pw_uid == 0 ||
       (uid && uid != pwd->pw_uid);
 }
 if (!passwd_req || (pwd && !*pwd->pw_passwd))
   break;
 setpriority(PRIO_PROCESS, 0, -4);
 pp = getpass("Password: ");
 p = crypt(pp, salt);
 setpriority(PRIO_PROCESS, 0, 0);
 memset(pp, 0, strlen(pp));
 if (pwd && !strcmp(p, pwd->pw_passwd))
   break;
 printf("Login incorrect\n");
 badlogin(username);
 failures++;
 if (++cnt > 3) {
     if (cnt >= 10) {
  sleepexit(1);
     }
     sleep((unsigned int)((cnt - 3) * 5));
 }
    }
    alarm((unsigned int)0);
    endpwent();
 quietlog = 0;
    dolastlog(quietlog);
    chown(ttyn, pwd->pw_uid, pwd->pw_gid);
    chmod(ttyn, 0600);
    setgid(pwd->pw_gid);
    if (*pwd->pw_shell == '\0')
      pwd->pw_shell = (char *)"/bin/sh";
    {
 char *ep;
 if(!((ep = getenv("TERM")) && (termenv = strdup(ep))))
   termenv = (char *) "dumb";
    }
    if (!pflag)
      {
          environ = (char **)malloc(sizeof(char *));
   memset(environ, 0, sizeof(char *));
      }
    setenv("HOME", pwd->pw_dir, 0);
    if(pwd->pw_uid)
      setenv("PATH", "/usr/bin:/bin", 1);
    else
      setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
    setenv("SHELL", pwd->pw_shell, 1);
    setenv("TERM", termenv, 1);
    {
      char tmp[1024];
      if (sizeof("/var/mail") + strlen(pwd->pw_name) + 1 < 1024) {
       sprintf(tmp, "%s/%s", "/var/mail", pwd->pw_name);
       setenv("MAIL",tmp,0);
      }
    }
    setenv("LOGNAME", pwd->pw_name, 1);
    if (!strncmp(tty_name, "ttyS", 4))
      syslog(6, "DIALUP AT %s BY %s", tty_name, pwd->pw_name);
    if (pwd->pw_uid == 0) {
 if (hostname)
   syslog(5, "ROOT LOGIN ON %s FROM %s",
   tty_name, hostname);
 else
   syslog(5, "ROOT LOGIN ON %s", tty_name);
    } else {
 if (hostname)
   syslog(6, "LOGIN ON %s BY %s FROM %s", tty_name,
   pwd->pw_name, hostname);
 else
   syslog(6, "LOGIN ON %s BY %s", tty_name,
   pwd->pw_name);
    }
    if (!quietlog) {
 struct stat st;
 char *mail;
 mail = getenv("MAIL");
 if (mail && stat(mail, &st) == 0 && st.st_size != 0) {
  if (st.st_mtim.tv_sec > st.st_atim.tv_sec)
   printf("You have new mail.\n");
  else
   printf("You have mail.\n");
 }
    }
    signal(14, ((void (*)(int)) 0));
    signal(3, ((void (*)(int)) 0));
    signal(20, ((void (*)(int)) 1));
    signal(2, ((void (*)(int)) 0));
    if(setuid(pwd->pw_uid) < 0 && pwd->pw_uid) {
 syslog(1, "setuid() failed");
 exit(1);
    }
    if (chdir(pwd->pw_dir) < 0) {
 printf("No directory %s!\n", pwd->pw_dir);
 if (chdir("/"))
   exit(0);
 pwd->pw_dir = (char *) "/";
 printf("Logging in with home = \"/\".\n");
    }
    if (strchr(pwd->pw_shell, ' ')) {
 buff = malloc(strlen(pwd->pw_shell) + 6);
 if (!buff) {
     fprintf(stderr, "login: no memory for shell script.\n");
     exit(0);
 }
 strcpy(buff, "exec ");
 strcat(buff, pwd->pw_shell);
 childArgv[childArgc++] = (char *) "/bin/sh";
 childArgv[childArgc++] = (char *) "-sh";
 childArgv[childArgc++] = (char *) "-c";
 childArgv[childArgc++] = buff;
    } else {
 tbuf[0] = '-';
 xstrncpy(tbuf + 1, ((p = strrchr(pwd->pw_shell, '/')) ?
      p + 1 : pwd->pw_shell),
  sizeof(tbuf)-1);
 childArgv[childArgc++] = pwd->pw_shell;
 childArgv[childArgc++] = tbuf;
    }
    childArgv[childArgc++] = ((void *)0);
    execvp(childArgv[0], childArgv + 1);
    errsv = (*__errno_location ());
    if (!strcmp(childArgv[0], "/bin/sh"))
 fprintf(stderr, "login: couldn't exec shell script: %s.\n",
  strerror(errsv));
    else
 fprintf(stderr, "login: no shell: %s.\n", strerror(errsv));
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: optind int

variable_types:
optarg
char *

variable_types:
environ
char **

variable_types: ask int

variable_types: fflag int

variable_types: hflag int

variable_types: pflag int

variable_types: errsv int

variable_types: quietlog int

variable_types:
domain
char *

variable_types:
ttyn
char *

variable_types:
tbuf
char [-1]

variable_types:
tname
char [-1]

variable_types:
termenv
char *

variable_types:
childArgv
char *[10]

variable_types:
buff
char *

variable_types: childArgc int

variable_types:
salt
char *

variable_types:
pp
char *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: uid int

variable_types:
ep
char *

variable_types:
tmp
char [1024]

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:
mail
char *

return_type: int

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 __fastcall parse_mac(char *s, _BYTE *a2)
{
size_t v2; // rax
char v3; // al
char v4; // al
char v5; // al
int v7; // [rsp+0h] [rbp-28h] BYREF
int v8; // [rsp+4h] [rbp-24h] BYREF
int v9; // [rsp+8h] [rbp-20h] BYREF
int v10; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)_isoc99_sscanf(s, "%x.%x.%x%n", &v10, &v9, &v8, &v7) != 3 )
return 4294967274LL;
v2 = strlen(s);
if ( v2 != v7 )
return 4294967274LL;
v3 = v10;
*a2 = BYTE1(v10);
a2[1] = v3;
v4 = v9;
a2[2] = BYTE1(v9);
a2[3] = v4;
v5 = v8;
a2[4] = BYTE1(v8);
a2[5] = v5;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s, _BYTE *a2)
{
size_t v2; // rax
char v3; // al
char v4; // al
char v5; // al
int v7; // [rsp+0h] [rbp-28h] BYREF
int v8; // [rsp+4h] [rbp-24h] BYREF
int v9; // [rsp+8h] [rbp-20h] BYREF
int v10; // [rsp+Ch] [rbp-1Ch] BYREF

if ( (unsigned int)_isoc99_sscanf(s, "%x.%x.%x%n", &v10, &v9, &v8, &v7) != 3 )
return 4294967274LL;
v2 = strlen(s);
if ( v2 != v7 )
return 4294967274LL;
v3 = v10;
*a2 = BYTE1(v10);
a2[1] = v3;
v4 = v9;
a2[2] = BYTE1(v9);
a2[3] = v4;
v5 = v8;
a2[4] = BYTE1(v8);
a2[5] = v5;
return 0LL;
}

original_code:
int parse_mac(const char *str, unsigned char *mac)
{
 int a, b, c, n;
 if (sscanf(str, "%x.%x.%x%n", &a, &b, &c, &n) != 3)
  return -22;
 if (strlen(str) != n)
  return -22;
 mac[0] = (a & 0xff00) >> 8;
 mac[1] = (a & 0x00ff) >> 0;
 mac[2] = (b & 0xff00) >> 8;
 mac[3] = (b & 0x00ff) >> 0;
 mac[4] = (c & 0xff00) >> 8;
 mac[5] = (c & 0x00ff) >> 0;
 return 0;
}

canonical_original_code:
int parse_mac(char *str, unsigned char *mac)
{
 int a; int b; int c; int n;
 if (sscanf(str, "%x.%x.%x%n", &a, &b, &c, &n) != 3)
  return -22;
 if (strlen(str) != n)
  return -22;
 mac[0] = (a & 0xff00) >> 8;
 mac[1] = (a & 0x00ff) >> 0;
 mac[2] = (b & 0xff00) >> 8;
 mac[3] = (b & 0x00ff) >> 0;
 mac[4] = (c & 0xff00) >> 8;
 mac[5] = (c & 0x00ff) >> 0;
 return 0;
}

variable_types:
str
char *

variable_types:
mac
unsigned char *

variable_types: a int

variable_types: b int

variable_types: c int

variable_types: n int


decompiled_code:
__int64 read_key()
{
unsigned int v0; // ebx
struct termios v2; // [rsp+0h] [rbp-98h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-58h] BYREF

tcgetattr(0, &termios_p);
*(__m128i *)&v2.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v2.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v2.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v2.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v2.c_ospeed = termios_p.c_ospeed;
v2.c_lflag = -3;
*(_WORD *)&v2.c_cc[5] = 256;
tcsetattr(0, 0, &v2);
v0 = getchar();
tcsetattr(0, 0, &termios_p);
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // ebx
struct termios v2; // [rsp+0h] [rbp-98h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-58h] BYREF

tcgetattr(0, &termios_p);
*(__m128i *)&v2.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v2.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v2.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v2.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v2.c_ospeed = termios_p.c_ospeed;
v2.c_lflag = -3;
*(_WORD *)&v2.c_cc[5] = 256;
tcsetattr(0, 0, &v2);
v0 = getchar();
tcsetattr(0, 0, &termios_p);
return v0;
}

original_code:
int read_key(void) {
 int ret;
 struct termios t_old, t_new;
 tcgetattr(0, &t_old);
 t_new = t_old;
 t_new.c_lflag = ~0000002;
 t_new.c_cc[5] = 0;
 t_new.c_cc[6] = 1;
 tcsetattr(0, 0, &t_new);
 ret = getchar();
 tcsetattr(0, 0, &t_old);
 return ret;
}

canonical_original_code:
int read_key() {
 int ret;
 struct termios t_old; struct termios t_new;
 tcgetattr(0, &t_old);
 t_new = t_old;
 t_new.c_lflag = ~0000002;
 t_new.c_cc[5] = 0;
 t_new.c_cc[6] = 1;
 tcsetattr(0, 0, &t_new);
 ret = getchar();
 tcsetattr(0, 0, &t_old);
 return ret;
}

variable_types: ret int

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


daemonize

decompiled_code:
int daemonize()
{
__pid_t v0; // eax
rlim_t v1; // rbx
__pid_t v2; // eax
int v3; // ebx
struct rlimit v5; // [rsp+0h] [rbp-18h] BYREF

v5.rlim_cur = 0LL;
v5.rlim_max = 0LL;
v0 = fork();
if ( v0 == -1 )
{
syslog(3, "Prefork stage 1: %m");
exit(1);
}
if ( v0 )
exit(0);
v5.rlim_max = 0LL;
getrlimit(RLIMIT_NOFILE, &v5);
if ( !v5.rlim_max )
{
syslog(3, "Max number of open file descriptors is 0!");
exit(1);
}
if ( v5.rlim_max == -1LL )
{
syslog(3, "getrlimit");
exit(1);
}
v1 = 0LL;
do
close(v1++);
while ( v5.rlim_max > v1 );
if ( setsid() == -1 )
{
syslog(3, "setsid failed");
exit(1);
}
v2 = fork();
if ( v2 == -1 )
{
syslog(3, "Prefork stage 2: %m");
exit(1);
}
if ( v2 )
exit(0);
chdir("/");
umask(0);
v3 = open("/dev/null", 2);
dup(v3);
return dup(v3);
}

canonical_decompiled_code:
int func0()
{
__pid_t v0; // eax
rlim_t v1; // rbx
__pid_t v2; // eax
int v3; // ebx
struct rlimit v5; // [rsp+0h] [rbp-18h] BYREF

v5.rlim_cur = 0LL;
v5.rlim_max = 0LL;
v0 = fork();
if ( v0 == -1 )
{
syslog(3, "Prefork stage 1: %m");
exit(1);
}
if ( v0 )
exit(0);
v5.rlim_max = 0LL;
getrlimit(RLIMIT_NOFILE, &v5);
if ( !v5.rlim_max )
{
syslog(3, "Max number of open file descriptors is 0!");
exit(1);
}
if ( v5.rlim_max == -1LL )
{
syslog(3, "getrlimit");
exit(1);
}
v1 = 0LL;
do
close(v1++);
while ( v5.rlim_max > v1 );
if ( setsid() == -1 )
{
syslog(3, "setsid failed");
exit(1);
}
v2 = fork();
if ( v2 == -1 )
{
syslog(3, "Prefork stage 2: %m");
exit(1);
}
if ( v2 )
exit(0);
chdir("/");
umask(0);
v3 = open("/dev/null", 2);
dup(v3);
return dup(v3);
}

original_code:
void daemonize(void) {
 struct rlimit rl = {0};
 int fd = -1;
 int i;
 switch (fork()) {
 case -1:
  syslog(3, "Prefork stage 1: %m");
  exit(1);
 case 0:
  break;
 default:
  exit(0);
 }
 rl.rlim_max = 0;
 getrlimit(RLIMIT_NOFILE, &rl);
 switch (rl.rlim_max) {
 case -1:
  syslog(3, "getrlimit");
  exit(1);
 case 0:
  syslog(3, "Max number of open file descriptors is 0!");
  exit(1);
 }
 for (i = 0; i < rl.rlim_max; i++)
  close(i);
 if (setsid() == -1) {
  syslog(3, "setsid failed");
  exit(1);
 }
 switch (fork()) {
 case -1:
  syslog(3, "Prefork stage 2: %m");
  exit(1);
 case 0:
  break;
 default:
  exit(0);
 }
 chdir("/");
 umask(0);
 fd = open("/dev/null", 02);
 dup(fd);
 dup(fd);
}

canonical_original_code:
void daemonize() {
 struct rlimit rl = {0};
 int fd = -1;
 int i;
 switch (fork()) {
 case -1:
  syslog(3, "Prefork stage 1: %m");
  exit(1);
 case 0:
  break;
 default:
  exit(0);
 }
 rl.rlim_max = 0;
 getrlimit(RLIMIT_NOFILE, &rl);
 switch (rl.rlim_max) {
 case -1:
  syslog(3, "getrlimit");
  exit(1);
 case 0:
  syslog(3, "Max number of open file descriptors is 0!");
  exit(1);
 }
 for (i = 0; i < rl.rlim_max; i++)
  close(i);
 if (setsid() == -1) {
  syslog(3, "setsid failed");
  exit(1);
 }
 switch (fork()) {
 case -1:
  syslog(3, "Prefork stage 2: %m");
  exit(1);
 case 0:
  break;
 default:
  exit(0);
 }
 chdir("/");
 umask(0);
 fd = open("/dev/null", 02);
 dup(fd);
 dup(fd);
}

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }

variable_types: fd int

variable_types: i int


decompiled_code:
void redisplay_void()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code: static void redisplay_void(void) {}

canonical_original_code: static void redisplay_void() {}


grep

decompiled_code:
__int64 __fastcall grep(char **a1)
{
char **v1; // rbx
char *v2; // rdx
__int64 v3; // rax
__int64 v4; // rcx

v1 = (char **)malloc(0x48uLL);
*v1 = strdup("/bin/grep");
v2 = *a1;
if ( *a1 )
{
v3 = 1LL;
do
{
v1[v3] = v2;
v4 = v3++;
v2 = a1[v3 - 1];
}
while ( v2 );
}
else
{
v4 = 0LL;
}
v1[v4 + 1] = 0LL;
execv("/bin/grep", v1);
free(v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char **a1)
{
char **v1; // rbx
char *v2; // rdx
__int64 v3; // rax
__int64 v4; // rcx

v1 = (char **)malloc(0x48uLL);
*v1 = strdup("/bin/grep");
v2 = *a1;
if ( *a1 )
{
v3 = 1LL;
do
{
v1[v3] = v2;
v4 = v3++;
v2 = a1[v3 - 1];
}
while ( v2 );
}
else
{
v4 = 0LL;
}
v1[v4 + 1] = 0LL;
execv("/bin/grep", v1);
free(v1);
return 0LL;
}

original_code:
static int grep(char *grep_args[])
{
 int i;
 char **new_args;
 new_args = (char **)malloc(sizeof(char *) * (sizeof(grep_args)+1));
 new_args[0] = strdup("/bin/grep");
 for (i=0; grep_args[i]; i++)
  new_args[i+1] = grep_args[i];
 new_args[i+1] = ((void *)0);
 execv("/bin/grep", new_args);
 free(new_args);
 return 0;
}


usage

decompiled_code:
__int64 __fastcall usage(const char *a1)
{
printf(
"Usage: %s <type> <args>, where type can be one of: \n"
"\tinclude - include filter\n"
"\texclude - exclude filter\n"
"\tbegin - begin filter\n"
"\tgrep - grep filter\n",
a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
printf(
"Usage: %s <type> <args>, where type can be one of: \n"
"\tinclude - include filter\n"
"\texclude - exclude filter\n"
"\tbegin - begin filter\n"
"\tgrep - grep filter\n",
a1);
return 1LL;
}

original_code:
static int usage(const char *progname)
{
 printf( "Usage: %s <type> <args>, where type can be one of: \n"
   "\tinclude - include filter\n"
   "\texclude - exclude filter\n"
   "\tbegin - begin filter\n"
   "\tgrep - grep filter\n",
   progname);
 return 1;
}

canonical_original_code:
static int usage(char *progname)
{
 printf( "Usage: %s <type> <args>, where type can be one of: \n"
   "\tinclude - include filter\n"
   "\texclude - exclude filter\n"
   "\tbegin - begin filter\n"
   "\tgrep - grep filter\n",
   progname);
 return 1;
}

variable_types:
progname
char *


decompiled_code:
int __fastcall vec_compare(int a1, int a2, int a3, int a4, int a5, int a6, char s1, int a8, int a9, char s2)
{
return memcmp(&s1, &s2, 0x18uLL);
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, int a3, int a4, int a5, int a6, char s1, int a8, int a9, char s2)
{
return memcmp(&s1, &s2, 0x18uLL);
}

original_code:
int vec_compare(struct priority_vector v1, struct priority_vector v2) {
 return memcmp(&v1, &v2, sizeof(struct priority_vector));
}

variable_types: struct priority_vector { struct bridge_id { unsigned char bridge_priority[2]; unsigned char bridge_address[6]; } root_bridge_id; unsigned char root_path_cost[4]; struct bridge_id designated_bridge_id; unsigned char designated_port_id[2]; unsigned char bridge_port_id[2]; }

user_defined_types: struct priority_vector { struct bridge_id root_bridge_id; unsigned char root_path_cost[4]; struct bridge_id designated_bridge_id; unsigned char designated_port_id[2]; unsigned char bridge_port_id[2]; }


decompiled_code:
int __fastcall vec_compare4(int a1, int a2, int a3, int a4, int a5, int a6, char s1, int a8, int a9, char s2)
{
return memcmp(&s1, &s2, 0x16uLL);
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2, int a3, int a4, int a5, int a6, char s1, int a8, int a9, char s2)
{
return memcmp(&s1, &s2, 0x16uLL);
}

original_code:
int vec_compare4(struct priority_vector4 v1, struct priority_vector4 v2) {
 return memcmp(&v1, &v2, sizeof(struct priority_vector4));
}

variable_types: struct priority_vector4 { struct bridge_id { unsigned char bridge_priority[2]; unsigned char bridge_address[6]; } root_bridge_id; unsigned char root_path_cost[4]; struct bridge_id designated_bridge_id; unsigned char designated_port_id[2]; }

user_defined_types: struct priority_vector4 { struct bridge_id root_bridge_id; unsigned char root_path_cost[4]; struct bridge_id designated_bridge_id; unsigned char designated_port_id[2]; }


decompiled_code:
int __fastcall tim_compare(__int64 a1, __int64 a2)
{
__int64 v3; // [rsp+0h] [rbp-18h] BYREF
__int64 s1; // [rsp+8h] [rbp-10h] BYREF

s1 = a1;
v3 = a2;
return memcmp(&s1, &v3, 8uLL);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v3; // [rsp+0h] [rbp-18h] BYREF
__int64 s1; // [rsp+8h] [rbp-10h] BYREF

s1 = a1;
v3 = a2;
return memcmp(&s1, &v3, 8uLL);
}

original_code:
int tim_compare(struct rstp_times t1, struct rstp_times t2) {
 return memcmp(&t1, &t2, sizeof(struct rstp_times));
}

variable_types: struct rstp_times { unsigned short MessageAge; unsigned short MaxAge; unsigned short HelloTime; unsigned short ForwardDelay; }


decompiled_code:
ssize_t __fastcall send_packet(int a1, const void *a2, int a3)
{
return send(a1, a2, a3, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func2(int a1, const void *a2, int a3)
{
return send(a1, a2, a3, 0);
}

original_code:
int send_packet(int sock_fd, unsigned char *packet, int size)
{
 return send(sock_fd, packet, size, 0);
}

variable_types:
packet
unsigned char *

variable_types: size int


decompiled_code:
__int64 __fastcall cli_mask_apply(int *a1, int a2)
{
int v2; // eax

if ( !a1 )
return 1LL;
v2 = *a1;
if ( !*a1 )
return 1LL;
while ( (v2 & a2) != 0 )
{
v2 = *++a1;
if ( !*a1 )
return 1LL;
}
return 0LL;
}

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

if ( !a1 )
return 1LL;
v2 = *a1;
if ( !*a1 )
return 1LL;
while ( (v2 & a2) != 0 )
{
v2 = *++a1;
if ( !*a1 )
return 1LL;
}
return 0LL;
}

original_code:
static __inline__ int cli_mask_apply(uint32_t *mask, uint32_t filter) {
 if (!mask)
  return 1;
 while (*mask) {
  if (!(*mask & filter))
   return 0;
  mask++;
 }
 return 1;
}

canonical_original_code:
static __inline__ int cli_mask_apply(unsigned int *mask, unsigned int filter) {
 if (!mask)
  return 1;
 while (*mask) {
  if (!(*mask & filter))
   return 0;
  mask++;
 }
 return 1;
}

variable_types:
mask
unsigned int *

variable_types:
filter
unsigned int


checkmate

repo: listx/simtic

original_code:
int checkmate(struct board_pos *pos)
{
 int i, j, player_color, ours;
 player_color = (pos->color == WHITE) ? BLACK : WHITE;
 for (i = 0; i < 8; i++) {
  ours = 0;
  for (j = 0; j < 3; j++) {
   if (pos->sq[winning_squares[i][j]] == player_color)
    ours++;
   else
    break;
  }
  if (ours == 3)
   return 1;
 }
 return 0;
}

canonical_original_code:
int checkmate(struct board_pos *pos)
{
 int i; int j; int player_color; int ours;
 player_color = (pos->color == WHITE) ? BLACK : WHITE;
 for (i = 0; i < 8; i++) {
  ours = 0;
  for (j = 0; j < 3; j++) {
   if (pos->sq[winning_squares[i][j]] == player_color)
    ours++;
   else
    break;
  }
  if (ours == 3)
   return 1;
 }
 return 0;
}

variable_types: struct board_pos { int sq[9]; int color; } *

variable_types: j int

variable_types: ours int


eval

original_code:
int eval(struct board_pos *pos)
{
 int i, j, enemy_color, ours, points;
 points = 0;
 enemy_color = (pos->color == WHITE) ? BLACK : WHITE;
 for (i = 0; i < 8; i++) {
  ours = 0;
  for (j = 0; j < 3; j++) {
   if (pos->sq[winning_squares[i][j]] == pos->color)
    ours++;
  }
  if (ours > 1) {
   for (j = 0; j < 3; j++) {
    if (pos->sq[winning_squares[i][j]] == EMPTY)
     points++;
    if (pos->sq[winning_squares[i][j]] == enemy_color)
     points--;
   }
  }
 }
 return points;
}

canonical_original_code:
int eval(struct board_pos *pos)
{
 int i; int j; int enemy_color; int ours; int points;
 points = 0;
 enemy_color = (pos->color == WHITE) ? BLACK : WHITE;
 for (i = 0; i < 8; i++) {
  ours = 0;
  for (j = 0; j < 3; j++) {
   if (pos->sq[winning_squares[i][j]] == pos->color)
    ours++;
  }
  if (ours > 1) {
   for (j = 0; j < 3; j++) {
    if (pos->sq[winning_squares[i][j]] == EMPTY)
     points++;
    if (pos->sq[winning_squares[i][j]] == enemy_color)
     points--;
   }
  }
 }
 return points;
}

variable_types: points int

user_defined_types: struct board_pos { int sq[9]; int color; }


decompiled_code:
__int64 __fastcall move_generate(__int64 a1, _DWORD *a2)
{
_DWORD *v2; // rax
__int64 result; // rax
int v4; // edx

v2 = a2;
do
*v2++ = -1;
while ( v2 != a2 + 9 );
a2[9] = 0;
for ( result = 0LL; result != 9; ++result )
{
if ( *(_DWORD *)(a1 + 4 * result) == 2 )
{
v4 = a2[9];
a2[v4] = result;
a2[9] = v4 + 1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, _DWORD *a2)
{
_DWORD *v2; // rax
__int64 result; // rax
int v4; // edx

v2 = a2;
do
*v2++ = -1;
while ( v2 != a2 + 9 );
a2[9] = 0;
for ( result = 0LL; result != 9; ++result )
{
if ( *(_DWORD *)(a1 + 4 * result) == 2 )
{
v4 = a2[9];
a2[v4] = result;
a2[9] = v4 + 1;
}
}
return result;
}

original_code:
void move_generate(struct board_pos *pos, struct move_list *mp)
{
 int i;
 for (i = 0; i < 9; i++) {
  mp->move[i] = MOVE_NONE;
 }
 mp->moves = 0;
 for (i = 0; i < 9; i++) {
  if (pos->sq[i] == EMPTY) {
   mp->move[mp->moves] = i;
   mp->moves++;
  }
 }
}

variable_types: struct move_list { int move[9]; int moves; } *


decompiled_code:
_BOOL8 __fastcall move_do(__int64 a1, int a2)
{
int v2; // eax
_BOOL8 result; // rax

v2 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(a1 + 4LL * a2) = v2;
result = v2 == 0;
*(_DWORD *)(a1 + 36) = result;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(__int64 a1, int a2)
{
int v2; // eax
_BOOL8 result; // rax

v2 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(a1 + 4LL * a2) = v2;
result = v2 == 0;
*(_DWORD *)(a1 + 36) = result;
return result;
}

original_code:
void move_do(struct board_pos *pos, int move)
{
 pos->sq[move] = pos->color;
 pos->color = (pos->color == WHITE) ? BLACK : WHITE;
}

variable_types: move int


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

*(_DWORD *)(a1 + 4LL * a2) = 2;
result = *(_DWORD *)(a1 + 36) == 0;
*(_DWORD *)(a1 + 36) = result;
return result;
}

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

*(_DWORD *)(a1 + 4LL * a2) = 2;
result = *(_DWORD *)(a1 + 36) == 0;
*(_DWORD *)(a1 + 36) = result;
return result;
}

original_code:
void move_undo(struct board_pos *pos, int move)
{
 pos->sq[move] = EMPTY;
 pos->color = (pos->color == WHITE) ? BLACK : WHITE;
}


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

v1 = a1 + 9;
while ( *a1 != 2 )
{
if ( ++a1 == v1 )
return 0LL;
}
return 1LL;
}

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

v1 = a1 + 9;
while ( *a1 != 2 )
{
if ( ++a1 == v1 )
return 0LL;
}
return 1LL;
}

original_code:
_Bool board_empty(struct board_pos *pos)
{
 int i;
 for (i = 0; i < 9; i++) {
  if (pos->sq[i] == EMPTY)
   return 1;
 }
 return 0;
}


minimax

decompiled_code:
__int64 __fastcall minimax(_DWORD *a1, int a2)
{
int v2; // ebx
int v3; // ebx
int v5; // ebx
__int64 v6; // r13
int v7; // er12
int v8[9]; // [rsp+0h] [rbp-58h] BYREF
int v9; // [rsp+24h] [rbp-34h]

++nodecount;
if ( (unsigned int)checkmate((__int64)a1) )
{
v3 = -(a1[9] == 0);
LOBYTE(v3) = v3 & 0x38;
v2 = v3 + 100;
}
else
{
v2 = 0;
if ( (unsigned __int8)board_empty(a1) )
{
if ( a2 )
{
move_generate((__int64)a1, v8);
v5 = -(a1[9] == 0);
LOBYTE(v5) = v5 & 0x38;
v2 = v5 + 100;
if ( v9 > 0 )
{
v6 = 0LL;
do
{
move_do((__int64)a1, v8[v6]);
v7 = minimax(a1, (unsigned int)(a2 - 1));
move_undo((__int64)a1, v8[v6]);
if ( a1[9] )
{
if ( v2 > v7 )
v2 = v7;
}
else if ( v2 < v7 )
{
v2 = v7;
}
++v6;
}
while ( v9 > (int)v6 );
}
}
else
{
v2 = eval((__int64)a1);
if ( !a1[9] )
v2 = -v2;
}
}
}
return (unsigned int)v2;
}

original_code:
int minimax(struct board_pos *pos, int depth)
{
 struct move_list mlist;
 int score, score_best, won, i;
 nodecount++;
 won = checkmate(pos);
 if (won)
  return ((pos->color == WHITE) ? -INF : INF);
 if (!board_empty(pos))
  return 0;
 score = eval(pos);
 if (depth == 0) {
  return ((pos->color == WHITE) ? -score : score);
 }
 move_generate(pos, &mlist);
 score = (pos->color == WHITE) ? -INF : INF;
 for (i = 0; i < mlist.moves; i++) {
  move_do(pos, mlist.move[i]);
  score_best = minimax(pos, depth - 1);
  move_undo(pos, mlist.move[i]);
  if (pos->color == WHITE)
   score = (score > score_best ? score : score_best);
  else
   score = (score > score_best ? score_best : score);
 }
 return score;
}

canonical_original_code:
int minimax(struct board_pos *pos, int depth)
{
 struct move_list mlist;
 int score; int score_best; int won; int i;
 nodecount++;
 won = checkmate(pos);
 if (won)
  return ((pos->color == WHITE) ? -INF : INF);
 if (!board_empty(pos))
  return 0;
 score = eval(pos);
 if (depth == 0) {
  return ((pos->color == WHITE) ? -score : score);
 }
 move_generate(pos, &mlist);
 score = (pos->color == WHITE) ? -INF : INF;
 for (i = 0; i < mlist.moves; i++) {
  move_do(pos, mlist.move[i]);
  score_best = minimax(pos, depth - 1);
  move_undo(pos, mlist.move[i]);
  if (pos->color == WHITE)
   score = (score > score_best ? score : score_best);
  else
   score = (score > score_best ? score_best : score);
 }
 return score;
}

variable_types: depth int

variable_types: struct move_list { int move[9]; int moves; }

variable_types: score int

variable_types: won int


decompiled_code:
int __fastcall display_moves(__int64 a1)
{
__int64 v1; // rbx

if ( *(int *)(a1 + 36) > 0 )
{
v1 = 0LL;
do
{
switch ( *(_DWORD *)(a1 + 4 * v1) )
{
case 0:
putchar(48);
break;
case 1:
putchar(49);
break;
case 2:
putchar(50);
break;
case 3:
putchar(51);
break;
case 4:
putchar(52);
break;
case 5:
putchar(53);
break;
case 6:
putchar(54);
break;
case 7:
putchar(55);
break;
case 8:
putchar(56);
break;
default:
__assert_fail("0", "main.c", 0x20Au, "display_moves");
}
putchar(32);
++v1;
}
while ( *(_DWORD *)(a1 + 36) > (int)v1 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func14(__int64 a1)
{
__int64 v1; // rbx

if ( *(int *)(a1 + 36) > 0 )
{
v1 = 0LL;
do
{
switch ( *(_DWORD *)(a1 + 4 * v1) )
{
case 0:
putchar(48);
break;
case 1:
putchar(49);
break;
case 2:
putchar(50);
break;
case 3:
putchar(51);
break;
case 4:
putchar(52);
break;
case 5:
putchar(53);
break;
case 6:
putchar(54);
break;
case 7:
putchar(55);
break;
case 8:
putchar(56);
break;
default:
__assert_fail("0", "main.c", 0x20Au, "display_moves");
}
putchar(32);
++v1;
}
while ( *(_DWORD *)(a1 + 36) > (int)v1 );
}
return putchar(10);
}

original_code:
void display_moves(struct move_list *mp)
{
 int i;
 for (i = 0; i < mp->moves; i++) {
  switch (mp->move[i]) {
  case 0: printf("0"); break;
  case 1: printf("1"); break;
  case 2: printf("2"); break;
  case 3: printf("3"); break;
  case 4: printf("4"); break;
  case 5: printf("5"); break;
  case 6: printf("6"); break;
  case 7: printf("7"); break;
  case 8: printf("8"); break;
  default: ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "main.c", 522, __extension__ __PRETTY_FUNCTION__); }));
  }
  printf(" ");
 }
 printf("\n");
}

variable_types: i int

user_defined_types: struct move_list { int move[9]; int moves; }


decompiled_code:
__int64 __fastcall move_pick(_DWORD *a1, int a2)
{
int v2; // er15
int v3; // er13
int v4; // er13
__int64 v5; // rbx
int v6; // er12
int v8[9]; // [rsp+0h] [rbp-68h] BYREF
int v9; // [rsp+24h] [rbp-44h]

nodecount = 0;
move_generate((__int64)a1, v8);
v2 = v8[0];
printf("Possible moves: ");
display_moves((__int64)v8);
v3 = -(a1[9] == 0);
LOBYTE(v3) = v3 & 0x38;
v4 = v3 + 100;
if ( v9 > 0 )
{
v5 = 0LL;
do
{
move_do((__int64)a1, v8[v5]);
v6 = minimax(a1, a2);
move_undo((__int64)a1, v8[v5]);
if ( a1[9] )
{
if ( v6 < v4 )
{
v2 = v8[v5];
v4 = v6;
}
}
else if ( v6 > v4 )
{
v2 = v8[v5];
v4 = v6;
}
++v5;
}
while ( v9 > (int)v5 );
}
if ( v2 > 8 )
__assert_fail("move_picked < SQUARES_MAX", "main.c", 0x13Du, "move_pick");
if ( a1[v2] != 2 )
__assert_fail("pos->sq[move_picked] == EMPTY", "main.c", 0x13Eu, "move_pick");
printf("After examining %u nodes, best move is: %d\n", (unsigned int)nodecount, (unsigned int)(v2 + 1));
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func15(_DWORD *a1, int a2)
{
int v2; // er15
int v3; // er13
int v4; // er13
__int64 v5; // rbx
int v6; // er12
int v8[9]; // [rsp+0h] [rbp-68h] BYREF
int v9; // [rsp+24h] [rbp-44h]

nodecount = 0;
func9((__int64)a1, v8);
v2 = v8[0];
printf("Possible moves: ");
func14((__int64)v8);
v3 = -(a1[9] == 0);
LOBYTE(v3) = v3 & 0x38;
v4 = v3 + 100;
if ( v9 > 0 )
{
v5 = 0LL;
do
{
func10((__int64)a1, v8[v5]);
v6 = func13(a1, a2);
func11((__int64)a1, v8[v5]);
if ( a1[9] )
{
if ( v6 < v4 )
{
v2 = v8[v5];
v4 = v6;
}
}
else if ( v6 > v4 )
{
v2 = v8[v5];
v4 = v6;
}
++v5;
}
while ( v9 > (int)v5 );
}
if ( v2 > 8 )
__assert_fail("move_picked < SQUARES_MAX", "main.c", 0x13Du, "move_pick");
if ( a1[v2] != 2 )
__assert_fail("pos->sq[move_picked] == EMPTY", "main.c", 0x13Eu, "move_pick");
printf("After examining %u nodes, best move is: %d\n", (unsigned int)nodecount, (unsigned int)(v2 + 1));
return (unsigned int)v2;
}

original_code:
int move_pick(struct board_pos *pos, int depth)
{
 struct move_list mlist;
 int move_picked, score_current, score_of_candidate_move, i;
 nodecount = 0;
 move_generate(pos, &mlist);
 move_picked = mlist.move[0];
 printf("Possible moves: ");
 display_moves(&mlist);
 score_current = (pos->color == WHITE) ? -INF : INF;
 for (i = 0; i < mlist.moves; i++) {
  move_do(pos, mlist.move[i]);
  score_of_candidate_move = minimax(pos, depth);
  move_undo(pos, mlist.move[i]);
  if (pos->color == WHITE) {
   if (score_of_candidate_move > score_current) {
    move_picked = mlist.move[i];
    score_current = score_of_candidate_move;
   }
  } else {
   if (score_of_candidate_move < score_current) {
    move_picked = mlist.move[i];
    score_current = score_of_candidate_move;
   }
  }
 }
 ((void) sizeof ((move_picked < 9) ? 1 : 0), __extension__ ({ if (move_picked < 9) ; else __assert_fail ("move_picked < SQUARES_MAX", "main.c", 317, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((pos->sq[move_picked] == EMPTY) ? 1 : 0), __extension__ ({ if (pos->sq[move_picked] == EMPTY) ; else __assert_fail ("pos->sq[move_picked] == EMPTY", "main.c", 318, __extension__ __PRETTY_FUNCTION__); }));
 printf("After examining %u nodes, best move is: %d\n", nodecount, move_picked + 1);
 return move_picked;
}

canonical_original_code:
int move_pick(struct board_pos *pos, int depth)
{
 struct move_list mlist;
 int move_picked; int score_current; int score_of_candidate_move; int i;
 nodecount = 0;
 move_generate(pos, &mlist);
 move_picked = mlist.move[0];
 printf("Possible moves: ");
 display_moves(&mlist);
 score_current = (pos->color == WHITE) ? -INF : INF;
 for (i = 0; i < mlist.moves; i++) {
  move_do(pos, mlist.move[i]);
  score_of_candidate_move = minimax(pos, depth);
  move_undo(pos, mlist.move[i]);
  if (pos->color == WHITE) {
   if (score_of_candidate_move > score_current) {
    move_picked = mlist.move[i];
    score_current = score_of_candidate_move;
   }
  } else {
   if (score_of_candidate_move < score_current) {
    move_picked = mlist.move[i];
    score_current = score_of_candidate_move;
   }
  }
 }
 ((void) sizeof ((move_picked < 9) ? 1 : 0), __extension__ ({ if (move_picked < 9) ; else __assert_fail ("move_picked < SQUARES_MAX", "main.c", 317, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((pos->sq[move_picked] == EMPTY) ? 1 : 0), __extension__ ({ if (pos->sq[move_picked] == EMPTY) ; else __assert_fail ("pos->sq[move_picked] == EMPTY", "main.c", 318, __extension__ __PRETTY_FUNCTION__); }));
 printf("After examining %u nodes, best move is: %d\n", nodecount, move_picked + 1);
 return move_picked;
}


decompiled_code:
int __fastcall display_board(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r14
int v3; // eax

puts("\n+---+---+---+");
v1 = 0LL;
v2 = 292LL;
do
{
printf("| ");
v3 = *(_DWORD *)(a1 + 4 * v1);
switch ( v3 )
{
case 1:
putchar(79);
break;
case 2:
putchar(32);
break;
case 0:
putchar(88);
break;
}
putchar(32);
if ( (unsigned int)v1 <= 8 && _bittest64(&v2, (unsigned int)v1) )
{
puts("|");
puts("+---+---+---+");
}
++v1;
}
while ( v1 != 9 );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func16(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r14
int v3; // eax

puts("\n+---+---+---+");
v1 = 0LL;
v2 = 292LL;
do
{
printf("| ");
v3 = *(_DWORD *)(a1 + 4 * v1);
switch ( v3 )
{
case 1:
putchar(79);
break;
case 2:
putchar(32);
break;
case 0:
putchar(88);
break;
}
putchar(32);
if ( (unsigned int)v1 <= 8 && _bittest64(&v2, (unsigned int)v1) )
{
puts("|");
puts("+---+---+---+");
}
++v1;
}
while ( v1 != 9 );
return putchar(10);
}

original_code:
void display_board(struct board_pos *pos)
{
 int i;
 printf("\n+---+---+---+\n");
 for (i = 0; i < 9; i++) {
  printf("| ");
  switch (pos->sq[i]) {
  case WHITE: printf("X"); break;
  case BLACK: printf("O"); break;
  case EMPTY: printf(" "); break;
  default: break;
  }
  printf(" ");
  if (i == 2 || i == 5 || i == 8) {
   printf("|\n");
   printf("+---+---+---+\n");
  }
 }
 printf("\n");
}


decompiled_code:
_BOOL8 __fastcall make_move(_DWORD *a1, char a2, int a3)
{
__int64 v3; // rbx
int v4; // ebx

if ( a2 )
{
display_board((__int64)a1);
while ( 1 )
{
printf("Enter square 0 - 8: ");
v4 = getchar();
putchar(10);
switch ( v4 )
{
case '0':
LODWORD(v3) = 0;
break;
case '1':
LODWORD(v3) = 1;
break;
case '2':
LODWORD(v3) = 2;
break;
case '3':
LODWORD(v3) = 3;
break;
case '4':
LODWORD(v3) = 4;
break;
case '5':
LODWORD(v3) = 5;
break;
case '6':
LODWORD(v3) = 6;
break;
case '7':
LODWORD(v3) = 7;
break;
case '8':
LODWORD(v3) = 8;
break;
default:
continue;
}
if ( a1[(int)v3] == 2 )
break;
puts("That square is taken.");
}
}
else
{
printf("Deciding best move... ");
v3 = (unsigned int)move_pick(a1, a3);
printf("AI chose square %d\n", v3);
}
return move_do((__int64)a1, v3);
}

canonical_decompiled_code:
_BOOL8 __fastcall func17(_DWORD *a1, char a2, int a3)
{
__int64 v3; // rbx
int v4; // ebx

if ( a2 )
{
func16((__int64)a1);
while ( 1 )
{
printf("Enter square 0 - 8: ");
v4 = getchar();
putchar(10);
switch ( v4 )
{
case '0':
LODWORD(v3) = 0;
break;
case '1':
LODWORD(v3) = 1;
break;
case '2':
LODWORD(v3) = 2;
break;
case '3':
LODWORD(v3) = 3;
break;
case '4':
LODWORD(v3) = 4;
break;
case '5':
LODWORD(v3) = 5;
break;
case '6':
LODWORD(v3) = 6;
break;
case '7':
LODWORD(v3) = 7;
break;
case '8':
LODWORD(v3) = 8;
break;
default:
continue;
}
if ( a1[(int)v3] == 2 )
break;
puts("That square is taken.");
}
}
else
{
printf("Deciding best move... ");
v3 = (unsigned int)func15(a1, a3);
printf("AI chose square %d\n", v3);
}
return func10((__int64)a1, v3);
}

original_code:
void make_move(struct board_pos *pos, _Bool human, int depth)
{
 int move, sq, sq_tentative, best_sq;
 if (human) {
  display_board(pos);
choose_square:
  printf("Enter square 0 - 8: ");
  sq_tentative = getchar();
  printf("\n");
  switch (sq_tentative) {
  case '0': sq = 0; break;
  case '1': sq = 1; break;
  case '2': sq = 2; break;
  case '3': sq = 3; break;
  case '4': sq = 4; break;
  case '5': sq = 5; break;
  case '6': sq = 6; break;
  case '7': sq = 7; break;
  case '8': sq = 8; break;
  default: goto choose_square;
  }
  if (pos->sq[sq] == EMPTY) {
   move = sq;
  } else {
   printf("That square is taken.\n");
   goto choose_square;
  }
 } else {
  printf("Deciding best move... ");
  best_sq = move_pick(pos, depth);
  printf("AI chose square %d\n", best_sq);
  move = best_sq;
 }
 move_do(pos, move);
}

canonical_original_code:
void make_move(struct board_pos *pos, _Bool human, int depth)
{
 int move; int sq; int sq_tentative; int best_sq;
 if (human) {
  display_board(pos);
choose_square:
  printf("Enter square 0 - 8: ");
  sq_tentative = getchar();
  printf("\n");
  switch (sq_tentative) {
  case '0': sq = 0; break;
  case '1': sq = 1; break;
  case '2': sq = 2; break;
  case '3': sq = 3; break;
  case '4': sq = 4; break;
  case '5': sq = 5; break;
  case '6': sq = 6; break;
  case '7': sq = 7; break;
  case '8': sq = 8; break;
  default: goto choose_square;
  }
  if (pos->sq[sq] == EMPTY) {
   move = sq;
  } else {
   printf("That square is taken.\n");
   goto choose_square;
  }
 } else {
  printf("Deciding best move... ");
  best_sq = move_pick(pos, depth);
  printf("AI chose square %d\n", best_sq);
  move = best_sq;
 }
 move_do(pos, move);
}

variable_types: sq int


newgame

decompiled_code:
int __fastcall newgame(char a1, int a2)
{
const char *v3; // rsi
const char *v4; // rsi
int result; // eax
int v6[9]; // [rsp+0h] [rbp-48h] BYREF
int v7; // [rsp+24h] [rbp-24h]

v6[0] = 2;
v6[1] = 2;
v6[2] = 2;
v6[3] = 2;
v6[4] = 2;
v6[5] = 2;
v6[6] = 2;
v6[7] = 2;
v6[8] = 2;
v7 = 0;
while ( (unsigned __int8)board_empty(v6) )
{
make_move(v6, a1, a2);
a1 ^= 1u;
if ( (unsigned int)checkmate((__int64)v6) )
{
v3 = "Black (O)";
if ( v7 )
v3 = "White (X)";
printf("%s wins!\n", v3);
v4 = "AI";
if ( !a1 )
v4 = "You";
printf("%s won the game!", v4);
break;
}
}
display_board((__int64)v6);
result = checkmate((__int64)v6);
if ( !result )
result = puts("Draw!");
return result;
}

canonical_decompiled_code:
int __fastcall func18(char a1, int a2)
{
const char *v3; // rsi
const char *v4; // rsi
int result; // eax
int v6[9]; // [rsp+0h] [rbp-48h] BYREF
int v7; // [rsp+24h] [rbp-24h]

v6[0] = 2;
v6[1] = 2;
v6[2] = 2;
v6[3] = 2;
v6[4] = 2;
v6[5] = 2;
v6[6] = 2;
v6[7] = 2;
v6[8] = 2;
v7 = 0;
while ( (unsigned __int8)func12(v6) )
{
func17(v6, a1, a2);
a1 ^= 1u;
if ( (unsigned int)func7((__int64)v6) )
{
v3 = "Black (O)";
if ( v7 )
v3 = "White (X)";
printf("%s wins!\n", v3);
v4 = "AI";
if ( !a1 )
v4 = "You";
printf("%s won the game!", v4);
break;
}
}
func16((__int64)v6);
result = func7((__int64)v6);
if ( !result )
result = puts("Draw!");
return result;
}

original_code:
void newgame(_Bool human, int depth)
{
 struct board_pos pos = {
  {
   EMPTY, EMPTY, EMPTY,
   EMPTY, EMPTY, EMPTY,
   EMPTY, EMPTY, EMPTY
  },
  WHITE
 };
 while (board_empty(&pos)) {
  make_move(&pos, human, depth);
  human = human ? 0 : 1;
  if (checkmate(&pos)) {
   printf("%s wins!\n", (pos.color == WHITE) ? "Black (O)" : "White (X)");
   printf("%s won the game!", human ? "AI" : "You");
   break;
  }
 }
 display_board(&pos);
 if (!checkmate(&pos))
  printf("Draw!\n");
}

variable_types: struct board_pos { int sq[9]; int color; }


decompiled_code:
int game_loop()
{
char v0; // bp
int v1; // esi
int v2; // eax
int v3; // eax
int v4; // eax

LABEL_8:
printf("\nStarting new game...");
do
{
printf("\nWould you like to move first? (y/n) ");
v3 = getchar();
if ( v3 == 110 )
{
v0 = 0;
goto LABEL_12;
}
}
while ( v3 != 121 );
v0 = 1;
do
{
LABEL_12:
printf("\nChoose difficulty ([h]ard/[m]edium/[e]asy): ");
v4 = getchar();
if ( v4 == 104 )
{
v1 = 9;
goto LABEL_5;
}
if ( v4 == 109 )
{
v1 = 3;
goto LABEL_5;
}
}
while ( v4 != 101 );
v1 = 1;
LABEL_5:
newgame(v0, v1);
while ( 1 )
{
printf("\nPlay again? (y/n) ");
v2 = getchar();
if ( v2 == 110 )
return puts("\nGoodbye!");
if ( v2 == 121 )
goto LABEL_8;
}
}

canonical_decompiled_code:
int func19()
{
char v0; // bp
int v1; // esi
int v2; // eax
int v3; // eax
int v4; // eax

LABEL_8:
printf("\nStarting new game...");
do
{
printf("\nWould you like to move first? (y/n) ");
v3 = getchar();
if ( v3 == 110 )
{
v0 = 0;
goto LABEL_12;
}
}
while ( v3 != 121 );
v0 = 1;
do
{
LABEL_12:
printf("\nChoose difficulty ([h]ard/[m]edium/[e]asy): ");
v4 = getchar();
if ( v4 == 104 )
{
v1 = 9;
goto LABEL_5;
}
if ( v4 == 109 )
{
v1 = 3;
goto LABEL_5;
}
}
while ( v4 != 101 );
v1 = 1;
LABEL_5:
func18(v0, v1);
while ( 1 )
{
printf("\nPlay again? (y/n) ");
v2 = getchar();
if ( v2 == 110 )
return puts("\nGoodbye!");
if ( v2 == 121 )
goto LABEL_8;
}
}

original_code:
void game_loop()
{
 _Bool human = 0;
 int depth;
start_game_loop:
 printf("\nStarting new game...");
move_first_menu:
 printf("\nWould you like to move first? (y/n) ");
 switch (getchar()) {
 case 'y': human = 1; break;
 case 'n': human = 0; break;
 default: goto move_first_menu;
 }
difficulty_menu:
 printf("\nChoose difficulty ([h]ard/[m]edium/[e]asy): ");
 switch (getchar()) {
 case 'h': depth = 9; break;
 case 'm': depth = 3; break;
 case 'e': depth = 1; break;
 default: goto difficulty_menu;
 }
 newgame(human, depth);
newgame_menu:
 printf("\nPlay again? (y/n) ");
 switch (getchar()) {
 case 'y': goto start_game_loop;
 case 'n': goto exit_game_loop;
 default: goto newgame_menu;
 }
exit_game_loop:
 printf("\nGoodbye!\n");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct termios v4; // [rsp+0h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-48h] BYREF

setvbuf(_bss_start, 0LL, 2, 0LL);
tcgetattr(0, &termios_p);
*(__m128i *)&v4.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v4.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v4.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v4.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v4.c_ospeed = termios_p.c_ospeed;
v4.c_lflag &= 0xFFFFFFFC;
*(_WORD *)&v4.c_cc[5] = 258;
tcsetattr(0, 0, &v4);
game_loop();
tcsetattr(0, 0, &termios_p);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct termios v4; // [rsp+0h] [rbp-88h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-48h] BYREF

setvbuf(_bss_start, 0LL, 2, 0LL);
tcgetattr(0, &termios_p);
*(__m128i *)&v4.c_iflag = _mm_loadu_si128((const __m128i *)&termios_p);
*(__m128i *)&v4.c_line = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
*(__m128i *)&v4.c_cc[15] = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v4.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
v4.c_ospeed = termios_p.c_ospeed;
v4.c_lflag &= 0xFFFFFFFC;
*(_WORD *)&v4.c_cc[5] = 258;
tcsetattr(0, 0, &v4);
func19();
tcsetattr(0, 0, &termios_p);
return 0;
}

original_code:
int main()
{
 struct termios orig, rawmode;
        setvbuf(stdout, ((void *)0), 2 ,0);
        tcgetattr(0, &orig);
        rawmode=orig;
        rawmode.c_lflag &= ~(0000001|0000002);
        rawmode.c_cc[6]=1;
        rawmode.c_cc[5]=2;
        tcsetattr(0, 0, &rawmode);
 game_loop();
        tcsetattr(0, 0, &orig);
 return 0;
}

canonical_original_code:
int main()
{
 struct termios orig; struct termios rawmode;
        setvbuf(stdout, ((void *)0), 2 ,0);
        tcgetattr(0, &orig);
        rawmode=orig;
        rawmode.c_lflag &= ~(0000001|0000002);
        rawmode.c_cc[6]=1;
        rawmode.c_cc[5]=2;
        tcsetattr(0, 0, &rawmode);
 game_loop();
        tcsetattr(0, 0, &orig);
 return 0;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


bubbleSort

decompiled_code:
void bubbleSort()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void bubbleSort(Node* list)
{
}

canonical_original_code:
void bubbleSort(struct Node *list)
{
}

variable_types: struct Node { int data; struct Node *next; } *


insertSort

decompiled_code:
void insertSort()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void insertSort(Node* list)
{
}

canonical_original_code:
void insertSort(struct Node *list)
{
}

user_defined_types: struct Node { int data; struct Node *next; }


shellSort

decompiled_code:
void shellSort()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void shellSort(Node* list)
{
}

canonical_original_code:
void shellSort(struct Node *list)
{
}


quickSort

decompiled_code:
void quickSort()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void quickSort(Node* list)
{
}

canonical_original_code:
void quickSort(struct Node *list)
{
}


swap

decompiled_code:
__int64 __fastcall swap(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

original_code:
void swap(Node* node1, Node* node2)
{
 int temp;
 temp=node1->data;
 node1->data=node2->data;
 node2->data=temp;
}

canonical_original_code:
void swap(struct Node *node1, struct Node *node2)
{
 int temp;
 temp=node1->data;
 node1->data=node2->data;
 node2->data=temp;
}

variable_types: temp int


pickInsert

original_code:
void pickInsert(Node* list, Node* prev, int index)
{
 Node *tar, *temp;
 if(index==0)
 {
  tar=prev->next;
  prev->next=tar->next;
  tar->next=list->next;
  list->next=tar;
  swap(list, tar);
 }
 else
 {
  temp=find(list, index-1);
  if(temp&&temp->next)
  {
   tar=prev->next;
   prev->next=tar->next;
   tar->next=temp->next;
   temp->next=tar;
  }
  else
   printf("pickAndInsert(): error index not found");
 }
}

canonical_original_code:
void pickInsert(struct Node *list, struct Node *prev, int index)
{
 struct Node *tar; struct Node *temp;
 if(index==0)
 {
  tar=prev->next;
  prev->next=tar->next;
  tar->next=list->next;
  list->next=tar;
  swap(list, tar);
 }
 else
 {
  temp=find(list, index-1);
  if(temp&&temp->next)
  {
   tar=prev->next;
   prev->next=tar->next;
   tar->next=temp->next;
   temp->next=tar;
  }
  else
   printf("pickAndInsert(): error index not found");
 }
}

variable_types:
prev
struct Node *

variable_types: index int


insertToEnd

decompiled_code:
__int64 __fastcall insertToEnd(__int64 a1)
{
__int64 v1; // rbx

i = 0;
v1 = traversal(a1, 0xFFFFFFFF);
*(_QWORD *)(v1 + 8) = create();
return (unsigned int)(i + 1);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rbx

i = 0;
v1 = func9(a1, 0xFFFFFFFF);
*(_QWORD *)(v1 + 8) = func7();
return (unsigned int)(i + 1);
}

original_code:
int insertToEnd(Node* first)
{
 Node* lastNode;
 i=0;
 lastNode=traversal(first, -1);
 lastNode->next=create();
 return i+1;
}

canonical_original_code:
int insertToEnd(struct Node *first)
{
 struct Node *lastNode;
 i=0;
 lastNode=traversal(first, -1);
 lastNode->next=create();
 return i+1;
}

variable_types: struct Node { char *data; struct Node *next; } *


find

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

result = 0LL;
if ( a2 >= 0 )
{
i = -1;
result = traversal(a1, a2);
}
return result;
}

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

result = 0LL;
if ( a2 >= 0 )
{
i = -1;
result = func9(a1, a2);
}
return result;
}

original_code:
Node* find(Node* first, int index)
{
 if(index<0)
  return ((void *)0);
 i=-1;
 return traversal(first, index);
}

canonical_original_code:
struct Node *find(struct Node *first, int index)
{
 if(index<0)
  return ((void *)0);
 i=-1;
 return traversal(first, index);
}

variable_types:
first
struct Node *

return_type: struct Node { char *data; struct Node *next; } *


repo: litcave/fbpad

decompiled_code:
__int64 __fastcall find(int a1, _DWORD *a2, int a3)
{
__int64 result; // rax
int v5; // edx
int v6; // esi
int v7; // eax
_DWORD *v8; // rcx

result = 0LL;
if ( *a2 <= a1 )
{
v5 = a3 - 1;
if ( v5 < 0 )
return 0LL;
v6 = 0;
while ( 1 )
{
v7 = (v6 + v5) / 2;
v8 = &a2[2 * v7];
if ( *v8 > a1 )
{
v5 = v7 - 1;
}
else
{
if ( v8[1] >= a1 )
return 1LL;
v6 = v7 + 1;
}
if ( v6 > v5 )
return 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, _DWORD *a2, int a3)
{
__int64 result; // rax
int v5; // edx
int v6; // esi
int v7; // eax
_DWORD *v8; // rcx

result = 0LL;
if ( *a2 <= a1 )
{
v5 = a3 - 1;
if ( v5 < 0 )
return 0LL;
v6 = 0;
while ( 1 )
{
v7 = (v6 + v5) / 2;
v8 = &a2[2 * v7];
if ( *v8 > a1 )
{
v5 = v7 - 1;
}
else
{
if ( v8[1] >= a1 )
return 1LL;
v6 = v7 + 1;
}
if ( v6 > v5 )
return 0LL;
}
}
return result;
}

original_code:
static int find(int c, int tab[][2], int n)
{
 int l = 0;
 int h = n - 1;
 int m;
 if (c < tab[0][0])
  return 0;
 while (l <= h) {
  m = (h + l) / 2;
  if (c >= tab[m][0] && c <= tab[m][1])
   return 1;
  if (c < tab[m][0])
   h = m - 1;
  else
   l = m + 1;
 }
 return 0;
}

variable_types: c int

variable_types:
tab
int [][2]

variable_types: n int

variable_types: l int

variable_types: h int

variable_types: m int


isdw

decompiled_code:
_BOOL8 __fastcall isdw(int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 > 4351 )
result = (unsigned int)find(a1, dwchars, 37) != 0;
return result;
}

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

result = 0LL;
if ( a1 > 4351 )
result = (unsigned int)func0(a1, dwchars, 37) != 0;
return result;
}

original_code:
int isdw(int c)
{
 return c >= 0x1100 && find(c, dwchars, (sizeof(dwchars) / sizeof((dwchars)[0])));
}


iszw

decompiled_code:
_BOOL8 __fastcall iszw(int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 > 767 )
result = (unsigned int)find(a1, &zwchars, 189) != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 > 767 )
result = (unsigned int)func0(a1, &zwchars, 189) != 0;
return result;
}

original_code:
int iszw(int c)
{
 return c >= 0x0300 && find(c, zwchars, (sizeof(zwchars) / sizeof((zwchars)[0])));
}


decompiled_code:
char *__fastcall gc_get(int a1, int a2, int a3)
{
__int64 v3; // rax

v3 = 16 * (a1 & 0x7F);
do
{
if ( gc_glyph[v3] == a1 && gc_fg[v3] == a2 && gc_bg[v3] == a3 )
return (char *)gc_mem + 4 * fncols * fnrows * (int)v3;
++v3;
}
while ( v3 != 16 * ((a1 & 0x7F) + 1) );
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func0(int a1, int a2, int a3)
{
__int64 v3; // rax

v3 = 16 * (a1 & 0x7F);
do
{
if ( gc_glyph[v3] == a1 && gc_fg[v3] == a2 && gc_bg[v3] == a3 )
return (char *)gc_mem + 4 * fncols * fnrows * (int)v3;
++v3;
}
while ( v3 != 16 * ((a1 & 0x7F) + 1) );
return 0LL;
}

original_code:
static char *gc_get(int c, int fg, int bg)
{
 int idx = ((c) & ((1 << 7) - 1)) * (1 << 4);
 int i;
 for (i = idx; i < idx + (1 << 4); i++)
  if (gc_glyph[i] == c && gc_fg[i] == fg && gc_bg[i] == bg)
   return gc_mem + i * (fnrows * fncols * 4);
 return ((void *)0);
}

variable_types: fg int

variable_types: bg int

variable_types: idx int

return_type: char *


decompiled_code:
char *__fastcall gc_put(int a1, int a2, int a3)
{
int v5; // edx
int v6; // er9
int v7; // edx

v5 = gc_next[a1 & 0x7F];
v6 = v5 + 1;
v7 = 16 * (a1 & 0x7F) + v5;
if ( v6 > 15 )
gc_next[a1 & 0x7F] = 0;
else
gc_next[a1 & 0x7F] = v6;
gc_glyph[v7] = a1;
gc_fg[v7] = a2;
gc_bg[v7] = a3;
return (char *)gc_mem + 4 * v7 * fncols * fnrows;
}

canonical_decompiled_code:
char *__fastcall func1(int a1, int a2, int a3)
{
int v5; // edx
int v6; // er9
int v7; // edx

v5 = gc_next[a1 & 0x7F];
v6 = v5 + 1;
v7 = 16 * (a1 & 0x7F) + v5;
if ( v6 > 15 )
gc_next[a1 & 0x7F] = 0;
else
gc_next[a1 & 0x7F] = v6;
gc_glyph[v7] = a1;
gc_fg[v7] = a2;
gc_bg[v7] = a3;
return (char *)gc_mem + 4 * v7 * fncols * fnrows;
}

original_code:
static char *gc_put(int c, int fg, int bg)
{
 int lst = ((c) & ((1 << 7) - 1));
 int pos = gc_next[lst]++;
 int idx = lst * (1 << 4) + pos;
 if (gc_next[lst] >= (1 << 4))
  gc_next[lst] = 0;
 gc_glyph[idx] = c;
 gc_fg[idx] = fg;
 gc_bg[idx] = bg;
 return gc_mem + idx * (fnrows * fncols * 4);
}

variable_types: lst int

variable_types: pos int


rowbuf

decompiled_code:
void *__fastcall rowbuf(unsigned int a1, int a2)
{
int v4; // er11
int v5; // er9
_BYTE *v6; // rsi
__int64 v7; // rdi
__int64 v8; // rax

if ( a2 > 0 )
{
v4 = bpp;
v5 = 0;
v6 = &row_2;
v7 = (unsigned int)bpp;
do
{
v8 = 0LL;
if ( v4 > 0 )
{
do
{
v6[v8] = a1 >> (8 * v8);
++v8;
}
while ( v8 != v7 );
v6 += v7;
}
++v5;
}
while ( a2 != v5 );
}
return &row_2;
}

canonical_decompiled_code:
void *__fastcall func2(unsigned int a1, int a2)
{
int v4; // er11
int v5; // er9
_BYTE *v6; // rsi
__int64 v7; // rdi
__int64 v8; // rax

if ( a2 > 0 )
{
v4 = bpp;
v5 = 0;
v6 = &row_2;
v7 = (unsigned int)bpp;
do
{
v8 = 0LL;
if ( v4 > 0 )
{
do
{
v6[v8] = a1 >> (8 * v8);
++v8;
}
while ( v8 != v7 );
v6 += v7;
}
++v5;
}
while ( a2 != v5 );
}
return &row_2;
}

original_code:
static char *rowbuf(unsigned c, int len)
{
 static char row[32 * 1024];
 char *p = row;
 int i, k;
 for (i = 0; i < len; i++)
  for (k = 0; k < bpp; k++)
   *p++ = (c >> (k * 8)) & 0xff;
 return row;
}

canonical_original_code:
static char *rowbuf(unsigned int c, int len)
{
 static char row[-1];
 char *p = row;
 int i; int k;
 for (i = 0; i < len; i++)
  for (k = 0; k < bpp; k++)
   *p++ = (c >> (k * 8)) & 0xff;
 return row;
}

variable_types:
c
unsigned int

variable_types: len int

variable_types:
row
char [-1]

variable_types:
p
char *

variable_types: i int

variable_types: k int


fnsel

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

v2 = a1 | a2;
if ( (v2 & 0x2000000) != 0 )
return 2 * (unsigned int)(qword_FEC0 != 0);
result = v2 & 0x1000000;
if ( (v2 & 0x1000000) != 0 )
result = qword_FEB8 != 0;
return result;
}

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

v2 = a1 | a2;
if ( (v2 & 0x2000000) != 0 )
return 2 * (unsigned int)(qword_FEC0 != 0);
result = v2 & 0x1000000;
if ( (v2 & 0x1000000) != 0 )
result = qword_FEB8 != 0;
return result;
}

original_code:
static int fnsel(int fg, int bg)
{
 if ((fg | bg) & 0x02000000)
  return fonts[2] ? 2 : 0;
 if ((fg | bg) & 0x01000000)
  return fonts[1] ? 1 : 0;
 return 0;
}


decompiled_code:
_BOOL8 gc_init()
{
gc_mem = malloc((fncols * fnrows) << 13);
return gc_mem == 0LL;
}

canonical_decompiled_code:
_BOOL8 func4()
{
gc_mem = malloc((fncols * fnrows) << 13);
return gc_mem == 0LL;
}

original_code:
static int gc_init(void)
{
 gc_mem = malloc((1 << 7) * (1 << 4) * (fnrows * fncols * 4));
 return !gc_mem;
}

canonical_original_code:
static int gc_init()
{
 gc_mem = malloc((1 << 7) * (1 << 4) * (fnrows * fncols * 4));
 return !gc_mem;
}


decompiled_code:
void gc_free()
{
free(gc_mem);
}

canonical_decompiled_code:
void func5()
{
free(gc_mem);
}

original_code:
static void gc_free(void)
{
 free(gc_mem);
}

canonical_original_code:
static void gc_free()
{
 free(gc_mem);
}


decompiled_code:
__int64 __fastcall color2fb(int a1)
{
return fb_val(BYTE2(a1), BYTE1(a1), (unsigned __int8)a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
return fb_val(BYTE2(a1), BYTE1(a1), (unsigned __int8)a1);
}

variable_types: c int

return_type: unsigned int


decompiled_code:
__int64 __fastcall mixed_color(int a1, int a2, __int16 a3)
{
return fb_val(
(unsigned __int8)(BYTE2(a2) + ((unsigned __int16)(a3 * (BYTE2(a1) - BYTE2(a2))) >> 8)),
(unsigned __int8)(BYTE1(a2) + ((unsigned __int16)(a3 * (BYTE1(a1) - BYTE1(a2))) >> 8)),
(unsigned __int8)(a2 + ((unsigned __int16)(a3 * ((unsigned __int8)a1 - (unsigned __int8)a2)) >> 8)));
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2, __int16 a3)
{
return fb_val(
(unsigned __int8)(BYTE2(a2) + ((unsigned __int16)(a3 * (BYTE2(a1) - BYTE2(a2))) >> 8)),
(unsigned __int8)(BYTE1(a2) + ((unsigned __int16)(a3 * (BYTE1(a1) - BYTE1(a2))) >> 8)),
(unsigned __int8)(a2 + ((unsigned __int16)(a3 * ((unsigned __int8)a1 - (unsigned __int8)a2)) >> 8)));
}

variable_types:
val
unsigned int

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char


decompiled_code:
void *__fastcall fb_set(int a1, int a2, const void *a3, int a4)
{
size_t v5; // rbp
__int64 v6; // rax

v5 = bpp * a4;
v6 = fb_mem((unsigned int)(fbroff + a1));
return memcpy((void *)(v6 + bpp * (fbcoff + a2)), a3, v5);
}

canonical_decompiled_code:
void *__fastcall func9(int a1, int a2, const void *a3, int a4)
{
size_t v5; // rbp
__int64 v6; // rax

v5 = bpp * a4;
v6 = fb_mem((unsigned int)(fbroff + a1));
return memcpy((void *)(v6 + bpp * (fbcoff + a2)), a3, v5);
}

original_code:
static void fb_set(int r, int c, void *mem, int len)
{
 memcpy(fb_mem(fbroff + r) + (fbcoff + c) * bpp, mem, len * bpp);
}

variable_types: r int

variable_types:
mem
void *


decompiled_code:
void *__fastcall fb_box(int a1, int a2, int a3, int a4, unsigned int a5)
{
int v5; // ebx
int v7; // er12
void *result; // rax
const void *v9; // r14

v5 = a1;
v7 = a4 - a3;
result = rowbuf(a5, a4 - a3);
v9 = result;
if ( a1 < a2 )
{
do
result = fb_set(v5++, a3, v9, v7);
while ( a2 != v5 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func10(int a1, int a2, int a3, int a4, unsigned int a5)
{
int v5; // ebx
int v7; // er12
void *result; // rax
const void *v9; // r14

v5 = a1;
v7 = a4 - a3;
result = func2(a5, a4 - a3);
v9 = result;
if ( a1 < a2 )
{
do
result = func9(v5++, a3, v9, v7);
while ( a2 != v5 );
}
return result;
}

original_code:
static void fb_box(int sr, int er, int sc, int ec, unsigned val)
{
 char *row = rowbuf(val, ec - sc);
 int i;
 for (i = sr; i < er; i++)
  fb_set(i, sc, row, ec - sc);
}

canonical_original_code:
static void fb_box(int sr, int er, int sc, int ec, unsigned int val)
{
 char *row = rowbuf(val, ec - sc);
 int i;
 for (i = sr; i < er; i++)
  fb_set(i, sc, row, ec - sc);
}

variable_types: sr int

variable_types: er int

variable_types: sc int

variable_types: ec int

variable_types:
row
char *


decompiled_code:
char *__fastcall ch2fb(int a1, int a2, int a3, int a4)
{
char *v6; // rbp
int v8; // ebx
int v9; // eax

if ( a2 < 0 )
return 0LL;
if ( a2 > 127 || (v6 = 0LL, ((*_ctype_b_loc())[a2] & 0x6000) == 0x4000) )
{
v6 = gc_get(a2, a3, a4);
if ( !v6 && !(unsigned int)font_bitmap(fonts[a1], &bits_1, (unsigned int)a2) )
{
v6 = gc_put(a2, a3, a4);
v8 = font_cols(fonts[a1]);
v9 = font_rows(fonts[a1]);
bmp2fb((__int64)v6, (__int64)&bits_1, a3 & 0xFFFFFF, a4 & 0xFFFFFF, v9, v8);
}
}
return v6;
}

canonical_decompiled_code:
char *__fastcall func11(int a1, int a2, int a3, int a4)
{
char *v6; // rbp
int v8; // ebx
int v9; // eax

if ( a2 < 0 )
return 0LL;
if ( a2 > 127 || (v6 = 0LL, ((*_ctype_b_loc())[a2] & 0x6000) == 0x4000) )
{
v6 = func0(a2, a3, a4);
if ( !v6 && !(unsigned int)font_bitmap(fonts[a1], &bits_1, (unsigned int)a2) )
{
v6 = func1(a2, a3, a4);
v8 = font_cols(fonts[a1]);
v9 = font_rows(fonts[a1]);
func8((__int64)v6, (__int64)&bits_1, a3 & 0xFFFFFF, a4 & 0xFFFFFF, v9, v8);
}
}
return v6;
}

original_code:
static char *ch2fb(int fn, int c, int fg, int bg)
{
 static char bits[1024 * 4];
 char *fbbits;
 if (c < 0 || (c < 128 && (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint) || ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISspace))))
  return ((void *)0);
 if ((fbbits = gc_get(c, fg, bg)))
  return fbbits;
 if (font_bitmap(fonts[fn], bits, c))
  return ((void *)0);
 fbbits = gc_put(c, fg, bg);
 bmp2fb(fbbits, bits, fg & 0x00ffffff, bg & 0x00ffffff,
  font_rows(fonts[fn]), font_cols(fonts[fn]));
 return fbbits;
}

canonical_original_code:
static char *ch2fb(int fn, int c, int fg, int bg)
{
 static char bits[-1];
 char *fbbits;
 if (c < 0 || (c < 128 && (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint) || ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISspace))))
  return ((void *)0);
 if ((fbbits = gc_get(c, fg, bg)))
  return fbbits;
 if (font_bitmap(fonts[fn], bits, c))
  return ((void *)0);
 fbbits = gc_put(c, fg, bg);
 bmp2fb(fbbits, bits, fg & 0x00ffffff, bg & 0x00ffffff,
  font_rows(fonts[fn]), font_cols(fonts[fn]));
 return fbbits;
}

variable_types: fn int

variable_types: fg int

variable_types: bg int

variable_types:
bits
char [-1]

variable_types:
fbbits
char *


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

fbroff = a1;
fbcoff = a2;
fbrows = a3;
fbcols = a4;
rows = a3 / fnrows;
result = (unsigned int)(a4 / fncols);
cols = a4 / fncols;
return result;
}

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

fbroff = a1;
fbcoff = a2;
fbrows = a3;
fbcols = a4;
rows = a3 / fnrows;
result = (unsigned int)(a4 / fncols);
cols = a4 / fncols;
return result;
}

original_code:
void pad_conf(int roff, int coff, int _rows, int _cols)
{
 fbroff = roff;
 fbcoff = coff;
 fbrows = _rows;
 fbcols = _cols;
 rows = fbrows / fnrows;
 cols = fbcols / fncols;
}

variable_types: roff int

variable_types: coff int


decompiled_code:
void pad_free()
{
int *v0; // rbx

gc_free();
v0 = (int *)fonts;
do
{
if ( *(_QWORD *)v0 )
font_free();
v0 += 2;
}
while ( v0 != &bpp );
}

canonical_decompiled_code:
void func13()
{
int *v0; // rbx

func5();
v0 = (int *)fonts;
do
{
if ( *(_QWORD *)v0 )
font_free();
v0 += 2;
}
while ( v0 != &bpp );
}

original_code:
void pad_free(void)
{
 int i;
 gc_free();
 for (i = 0; i < 3; i++)
  if (fonts[i])
   font_free(fonts[i]);
}

canonical_original_code:
void pad_free()
{
 int i;
 gc_free();
 for (i = 0; i < 3; i++)
  if (fonts[i])
   font_free(fonts[i]);
}

variable_types: i int


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

result = color2fb(a1 & 0xFFFFFF);
if ( fbroff >= a2 )
{
v3 = result;
if ( fbcoff >= a2 )
{
fb_box(-a2, 0, -a2, fbcols + a2, result);
fb_box(fbrows, fbrows + a2, -a2, fbcols + a2, v3);
fb_box(-a2, fbrows + a2, -a2, 0, v3);
result = (__int64)fb_box(-a2, fbrows + a2, fbcols, fbcols + a2, v3);
}
}
return result;
}

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

result = func6(a1 & 0xFFFFFF);
if ( fbroff >= a2 )
{
v3 = result;
if ( fbcoff >= a2 )
{
func10(-a2, 0, -a2, fbcols + a2, result);
func10(fbrows, fbrows + a2, -a2, fbcols + a2, v3);
func10(-a2, fbrows + a2, -a2, 0, v3);
result = (__int64)func10(-a2, fbrows + a2, fbcols, fbcols + a2, v3);
}
}
return result;
}

original_code:
void pad_border(unsigned c, int wid)
{
 int v = color2fb(c & 0x00ffffff);
 if (fbroff < wid || fbcoff < wid)
  return;
 fb_box(-wid, 0, -wid, fbcols + wid, v);
 fb_box(fbrows, fbrows + wid, -wid, fbcols + wid, v);
 fb_box(-wid, fbrows + wid, -wid, 0, v);
 fb_box(-wid, fbrows + wid, fbcols, fbcols + wid, v);
}

canonical_original_code:
void pad_border(unsigned int c, int wid)
{
 int v = color2fb(c & 0x00ffffff);
 if (fbroff < wid || fbcoff < wid)
  return;
 fb_box(-wid, 0, -wid, fbcols + wid, v);
 fb_box(fbrows, fbrows + wid, -wid, fbcols + wid, v);
 fb_box(-wid, fbrows + wid, -wid, 0, v);
 fb_box(-wid, fbrows + wid, fbcols, fbcols + wid, v);
}

variable_types:
c
unsigned int

variable_types: wid int

variable_types: v int


decompiled_code:
char *__fastcall pad_put(int a1, int a2, int a3, int a4, int a5)
{
int v7; // er12
int v8; // er13
int v9; // eax
char *result; // rax
char *v11; // rbp
int i; // ebx
unsigned int v13; // eax

v7 = fnrows * a2;
v8 = fncols * a3;
v9 = fnsel(a4, a5);
result = ch2fb(v9, a1, a4, a5);
v11 = result;
if ( result || (result = ch2fb(0, a1, a4, a5), (v11 = result) != 0LL) )
{
for ( i = 0; fnrows > i; ++i )
result = (char *)fb_set(v7 + i, v8, &v11[bpp * i * fncols], fncols);
}
else
{
v13 = color2fb(a5 & 0xFFFFFF);
result = (char *)fb_box(v7, fnrows + v7, v8, fncols + v8, v13);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func15(int a1, int a2, int a3, int a4, int a5)
{
int v7; // er12
int v8; // er13
int v9; // eax
char *result; // rax
char *v11; // rbp
int i; // ebx
unsigned int v13; // eax

v7 = fnrows * a2;
v8 = fncols * a3;
v9 = func3(a4, a5);
result = func11(v9, a1, a4, a5);
v11 = result;
if ( result || (result = func11(0, a1, a4, a5), (v11 = result) != 0LL) )
{
for ( i = 0; fnrows > i; ++i )
result = (char *)func9(v7 + i, v8, &v11[bpp * i * fncols], fncols);
}
else
{
v13 = func6(a5 & 0xFFFFFF);
result = (char *)func10(v7, fnrows + v7, v8, fncols + v8, v13);
}
return result;
}

original_code:
void pad_put(int ch, int r, int c, int fg, int bg)
{
 int sr = fnrows * r;
 int sc = fncols * c;
 char *bits;
 int i;
 bits = ch2fb(fnsel(fg, bg), ch, fg, bg);
 if (!bits)
  bits = ch2fb(0, ch, fg, bg);
 if (!bits)
  fb_box(sr, sr + fnrows, sc, sc + fncols, color2fb(bg & 0x00ffffff));
 else
  for (i = 0; i < fnrows; i++)
   fb_set(sr + i, sc, bits + (i * fncols * bpp), fncols);
}

variable_types: ch int

variable_types: r int

variable_types: c int

variable_types:
bits
char *


decompiled_code:
void *__fastcall pad_fill(int a1, int a2, int a3, int a4, int a5)
{
int v6; // er12
int v7; // er13
unsigned int v8; // eax

v6 = fbrows;
if ( a2 >= 0 )
v6 = fnrows * a2;
v7 = fbcols;
if ( a4 >= 0 )
v7 = fncols * a4;
v8 = color2fb(a5 & 0xFFFFFF);
return fb_box(fnrows * a1, v6, fncols * a3, v7, v8);
}

canonical_decompiled_code:
void *__fastcall func16(int a1, int a2, int a3, int a4, int a5)
{
int v6; // er12
int v7; // er13
unsigned int v8; // eax

v6 = fbrows;
if ( a2 >= 0 )
v6 = fnrows * a2;
v7 = fbcols;
if ( a4 >= 0 )
v7 = fncols * a4;
v8 = func6(a5 & 0xFFFFFF);
return func10(fnrows * a1, v6, fncols * a3, v7, v8);
}

original_code:
void pad_fill(int sr, int er, int sc, int ec, int c)
{
 int fber = er >= 0 ? er * fnrows : fbrows;
 int fbec = ec >= 0 ? ec * fncols : fbcols;
 fb_box(sr * fnrows, fber, sc * fncols, fbec, color2fb(c & 0x00ffffff));
}

variable_types: sr int

variable_types: er int

variable_types: sc int

variable_types: ec int

variable_types: fber int

variable_types: fbec int


decompiled_code:
__int64 pad_rows()
{
return (unsigned int)rows;
}

canonical_decompiled_code:
__int64 func17()
{
return (unsigned int)rows;
}

original_code:
int pad_rows(void)
{
 return rows;
}

canonical_original_code:
int pad_rows()
{
 return rows;
}


decompiled_code:
__int64 pad_cols()
{
return (unsigned int)cols;
}

canonical_decompiled_code:
__int64 func18()
{
return (unsigned int)cols;
}

original_code:
int pad_cols(void)
{
 return cols;
}

canonical_original_code:
int pad_cols()
{
 return cols;
}