main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
char *v5; // rax
const char *v6; // r12
int v7; // eax
char *v8; // rax
char *v9; // r15
struct hostent *v10; // rax
int **v11; // rax
int *v12; // rax
char *v13; // rbp
int v14; // ebx
int v15; // ebp
int v16; // edi
char *v17; // rax
char *v18; // rbp
int v19; // er8
char *v20; // rax
char *v21; // r15
struct stat *v22; // rdi
struct termios *v23; // rsi
__int64 i; // rcx
struct passwd *v25; // rax
const char *v26; // r12
__int64 v27; // rax
__uid_t v28; // edx
int v29; // ecx
char *v30; // r13
char *v31; // r12
size_t v32; // rax
int v33; // er12
__int64 v34; // r12
char *v35; // rdi
char *v36; // rbp
const char **v37; // rbx
const char *v38; // r12
const char *v39; // r12
char *v40; // rdi
__int64 v41; // r12
const char *v42; // rbp
size_t v43; // rax
char *v44; // rax
char *v45; // rbx
__int64 v46; // rax
int v47; // edi
char *v48; // rax
const char *v49; // rcx
char *v50; // rax
char *v51; // rax
char v52[32]; // [rsp+0h] [rbp-8A8h] BYREF
struct termios termios_p; // [rsp+20h] [rbp-888h] BYREF
struct stat v54[7]; // [rsp+60h] [rbp-848h] BYREF
char name; // [rsp+460h] [rbp-448h] BYREF
char v56[1095]; // [rsp+461h] [rbp-447h] BYREF

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

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
char *v5; // rax
const char *v6; // r12
int v7; // eax
char *v8; // rax
char *v9; // r15
struct hostent *v10; // rax
int **v11; // rax
int *v12; // rax
char *v13; // rbp
int v14; // ebx
int v15; // ebp
int v16; // edi
char *v17; // rax
char *v18; // rbp
int v19; // er8
char *v20; // rax
char *v21; // r15
struct stat *v22; // rdi
struct termios *v23; // rsi
__int64 i; // rcx
struct passwd *v25; // rax
const char *v26; // r12
__int64 v27; // rax
__uid_t v28; // edx
int v29; // ecx
char *v30; // r13
char *v31; // r12
size_t v32; // rax
int v33; // er12
__int64 v34; // r12
char *v35; // rdi
char *v36; // rbp
const char **v37; // rbx
const char *v38; // r12
const char *v39; // r12
char *v40; // rdi
__int64 v41; // r12
const char *v42; // rbp
size_t v43; // rax
char *v44; // rax
char *v45; // rbx
__int64 v46; // rax
int v47; // edi
char *v48; // rax
const char *v49; // rcx
char *v50; // rax
char *v51; // rax
char v52[32]; // [rsp+0h] [rbp-8A8h] BYREF
struct termios termios_p; // [rsp+20h] [rbp-888h] BYREF
struct stat v54[7]; // [rsp+60h] [rbp-848h] BYREF
char name; // [rsp+460h] [rbp-448h] BYREF
char v56[1095]; // [rsp+461h] [rbp-447h] BYREF

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

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


repo: lisa-project/lisa-user

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

return_type: int


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

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

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

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

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
int v2; // edi
__pid_t v3; // eax
int v4; // ebp
struct rlimit v6; // [rsp+0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
int func0()
{
__pid_t v0; // eax
rlim_t v1; // rbx
int v2; // edi
__pid_t v3; // eax
int v4; // ebp
struct rlimit v6; // [rsp+0h] [rbp-28h] BYREF

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

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

return_type: void


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; // rbp
char *v2; // rax
char *v3; // rdx
__int64 v4; // rax
__int64 v5; // rcx

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

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

v1 = (char **)malloc(0x48uLL);
v2 = strdup("/bin/grep");
v3 = *a1;
*v1 = v2;
if ( v3 )
{
v4 = 1LL;
do
{
v1[v4] = v3;
v5 = v4++;
v3 = a1[v4 - 1];
}
while ( v3 );
}
else
{
v5 = 0LL;
}
v1[v5 + 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(_DWORD *a1, int a2)
{
if ( a1 )
{
while ( *a1 )
{
if ( (*a1 & a2) == 0 )
return 0LL;
++a1;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, int a2)
{
if ( a1 )
{
while ( *a1 )
{
if ( (*a1 & a2) == 0 )
return 0LL;
++a1;
}
}
return 1LL;
}

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


main

canonical_name: main

repo: listx/simtic

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

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

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

setvbuf(_bss_start, 0LL, 2, 0LL);
tcgetattr(0, &v6);
v3 = _mm_loadu_si128((const __m128i *)&v6);
v4 = _mm_loadu_si128((const __m128i *)&v6.c_cc[15]);
*(__m128i *)&termios_p.c_line = _mm_loadu_si128((const __m128i *)&v6.c_line);
*(_QWORD *)&termios_p.c_cc[31] = *(_QWORD *)&v6.c_cc[31];
*(__m128i *)&termios_p.c_iflag = v3;
termios_p.c_ospeed = v6.c_ospeed;
*(__m128i *)&termios_p.c_cc[15] = v4;
termios_p.c_lflag = v3.m128i_i32[3] & 0xFFFFFFFC;
*(_WORD *)&termios_p.c_cc[5] = 258;
tcsetattr(0, 0, &termios_p);
func19();
tcsetattr(0, 0, &v6);
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; }


checkmate

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; }


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)
{
int v2; // edx
_BOOL8 result; // rax

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

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

v2 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(a1 + 4LL * a2) = 2;
result = v2 == 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; // er12
__int64 result; // rax
int v4; // er12
int v5; // er12
__int64 v6; // rbp
int v7; // ecx
int v8[9]; // [rsp+0h] [rbp-58h] BYREF
int v9; // [rsp+24h] [rbp-34h]

++nodecount;
if ( (unsigned int)checkmate((__int64)a1) )
{
v4 = -(a1[9] == 0);
LOBYTE(v4) = v4 & 0x38;
result = (unsigned int)(v4 + 100);
}
else
{
v2 = 0;
if ( (unsigned __int8)board_empty(a1) )
{
if ( a2 )
{
move_generate((__int64)a1, (__int64)v8);
v5 = -(a1[9] == 0);
LOBYTE(v5) = v5 & 0x38;
v2 = v5 + 100;
if ( v9 > 0 )
{
v6 = 0LL;
do
{
move_do((__int64)a1, v8[v6]);
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;
}
}
result = (unsigned int)v2;
}
return result;
}

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");
}
++v1;
putchar(32);
}
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");
}
++v1;
putchar(32);
}
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; // er12
int v3; // er13
int v4; // er13
__int64 v5; // r11
__int64 v6; // r11
int v7; // ecx
__int64 v8; // r11
int v10[9]; // [rsp+0h] [rbp-58h] BYREF
int v11; // [rsp+24h] [rbp-34h]

nodecount = 0;
move_generate((__int64)a1, (__int64)v10);
v2 = v10[0];
printf("Possible moves: ");
display_moves((__int64)v10);
v3 = -(a1[9] == 0);
LOBYTE(v3) = v3 & 0x38;
v4 = v3 + 100;
if ( v11 > 0 )
{
v5 = 0LL;
do
{
while ( 1 )
{
move_do((__int64)a1, v10[v5]);
minimax(a1, a2);
move_undo((__int64)a1, v10[v6]);
if ( a1[9] )
break;
if ( v7 > v4 )
goto LABEL_4;
LABEL_5:
v5 = v8 + 1;
if ( v11 <= (int)v5 )
goto LABEL_9;
}
if ( v7 < v4 )
{
LABEL_4:
v2 = v10[v8];
v4 = v7;
goto LABEL_5;
}
v5 = v8 + 1;
}
while ( v11 > (int)v5 );
}
LABEL_9:
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; // er12
int v3; // er13
int v4; // er13
__int64 v5; // r11
__int64 v6; // r11
int v7; // ecx
__int64 v8; // r11
int v10[9]; // [rsp+0h] [rbp-58h] BYREF
int v11; // [rsp+24h] [rbp-34h]

nodecount = 0;
func9((__int64)a1, (__int64)v10);
v2 = v10[0];
printf("Possible moves: ");
func14((__int64)v10);
v3 = -(a1[9] == 0);
LOBYTE(v3) = v3 & 0x38;
v4 = v3 + 100;
if ( v11 > 0 )
{
v5 = 0LL;
do
{
while ( 1 )
{
func10((__int64)a1, v10[v5]);
func13(a1, a2);
func11((__int64)a1, v10[v6]);
if ( a1[9] )
break;
if ( v7 > v4 )
goto LABEL_4;
LABEL_5:
v5 = v8 + 1;
if ( v11 <= (int)v5 )
goto LABEL_9;
}
if ( v7 < v4 )
{
LABEL_4:
v2 = v10[v8];
v4 = v7;
goto LABEL_5;
}
v5 = v8 + 1;
}
while ( v11 > (int)v5 );
}
LABEL_9:
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; // r13
__int64 v2; // rbx
int v3; // eax

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

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

v1 = 292LL;
v2 = 0LL;
puts("\n+---+---+---+");
do
{
printf("| ");
v3 = *(_DWORD *)(a1 + 4 * v2);
switch ( v3 )
{
case 1:
putchar(79);
break;
case 2:
putchar(32);
break;
case 0:
putchar(88);
break;
}
putchar(32);
if ( _bittest64(&v1, (unsigned int)v2) )
{
puts("|");
puts("+---+---+---+");
}
++v2;
}
while ( v2 != 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)
{
int v3; // ebx
int v4; // esi
unsigned int v7; // [rsp+Ch] [rbp-1Ch]

if ( a2 )
{
display_board((__int64)a1);
while ( 1 )
{
do
{
printf("Enter square 0 - 8: ");
v3 = getchar();
putchar(10);
v4 = v3 - 48;
}
while ( (unsigned int)(v3 - 48) > 8 );
if ( a1[v4] == 2 )
break;
puts("That square is taken.");
}
}
else
{
printf("Deciding best move... ");
v7 = move_pick(a1, a3);
printf("AI chose square %d\n", v7);
v4 = v7;
}
return move_do((__int64)a1, v4);
}

canonical_decompiled_code:
_BOOL8 __fastcall func17(_DWORD *a1, char a2, int a3)
{
int v3; // ebx
int v4; // esi
unsigned int v7; // [rsp+Ch] [rbp-1Ch]

if ( a2 )
{
func16((__int64)a1);
while ( 1 )
{
do
{
printf("Enter square 0 - 8: ");
v3 = getchar();
putchar(10);
v4 = v3 - 48;
}
while ( (unsigned int)(v3 - 48) > 8 );
if ( a1[v4] == 2 )
break;
puts("That square is taken.");
}
}
else
{
printf("Deciding best move... ");
v7 = func15(a1, a3);
printf("AI chose square %d\n", v7);
v4 = v7;
}
return func10((__int64)a1, v4);
}

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)
{
char v4; // si
int result; // eax
const char *v6; // rsi
const char *v7; // rsi
__int64 v8[4]; // [rsp+0h] [rbp-48h] BYREF
__int64 v9; // [rsp+20h] [rbp-28h]

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

canonical_decompiled_code:
int __fastcall func18(char a1, int a2)
{
char v4; // si
int result; // eax
const char *v6; // rsi
const char *v7; // rsi
__int64 v8[4]; // [rsp+0h] [rbp-48h] BYREF
__int64 v9; // [rsp+20h] [rbp-28h]

v8[0] = 0x200000002LL;
v8[1] = 0x200000002LL;
v8[2] = 0x200000002LL;
v8[3] = 0x200000002LL;
v9 = 2LL;
while ( (unsigned __int8)func12(v8) )
{
v4 = a1;
a1 ^= 1u;
func17(v8, v4, a2);
if ( (unsigned int)func7((__int64)v8) )
{
v6 = "Black (O)";
if ( HIDWORD(v9) )
v6 = "White (X)";
printf("%s wins!\n", v6);
v7 = "AI";
if ( !a1 )
v7 = "You";
printf("%s won the game!", v7);
break;
}
}
func16((__int64)v8);
result = func7((__int64)v8);
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()
{
int v0; // eax
char v1; // bp
int v2; // eax
int v3; // esi
int v4; // eax

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

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

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

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");
}


bubbleSort

decompiled_code:
void bubbleSort()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

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 func2()
{
;
}

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 func3()
{
;
}

original_code:
void shellSort(Node* list)
{
}

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


quickSort

decompiled_code:
void quickSort()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

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 func5(_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, -1);
*(_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, -1);
*(_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, int a2)
{
if ( a2 < 0 )
return 0LL;
i = -1;
return traversal(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2)
{
if ( a2 < 0 )
return 0LL;
i = -1;
return func9(a1, a2);
}

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; // ecx
int v7; // eax
_DWORD *v8; // rsi

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

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

result = 0LL;
if ( *a2 <= a1 )
{
v5 = a3 - 1;
v6 = 0;
while ( 1 )
{
while ( 1 )
{
v7 = (v6 + v5) >> 1;
v8 = &a2[2 * v7];
if ( *v8 <= a1 )
break;
v5 = v7 - 1;
if ( v6 > v7 - 1 )
return 0LL;
}
if ( v8[1] >= a1 )
break;
v6 = v7 + 1;
if ( v7 + 1 > v5 )
return 0LL;
}
result = 1LL;
}
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)
{
int v4; // eax
int v5; // edx
__int64 v6; // rax

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

canonical_decompiled_code:
char *__fastcall func0(int a1, int a2, int a3)
{
int v4; // eax
int v5; // edx
__int64 v6; // rax

v4 = a1 & 0x7F;
v5 = 16 * (v4 + 1);
v6 = 16 * v4;
do
{
if ( gc_glyph[v6] == a1 && gc_fg[v6] == a2 && gc_bg[v6] == a3 )
return (char *)gc_mem + 4 * fncols * fnrows * (int)v6;
++v6;
}
while ( v6 != v5 );
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)
{
__int64 v5; // rsi
int v6; // edx
int v7; // ecx
int v8; // edx
int v9; // eax

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

canonical_decompiled_code:
char *__fastcall func1(int a1, int a2, int a3)
{
__int64 v5; // rsi
int v6; // edx
int v7; // ecx
int v8; // edx
int v9; // eax

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

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

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_FF80 != 0);
result = v2 & 0x1000000;
if ( (v2 & 0x1000000) != 0 )
result = qword_FF78 != 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_FF80 != 0);
result = v2 & 0x1000000;
if ( (v2 & 0x1000000) != 0 )
result = qword_FF78 != 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((unsigned int)(a1 >> 16), BYTE1(a1), (unsigned __int8)a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
return fb_val((unsigned int)(a1 >> 16), 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 * (HIWORD(a1) - HIWORD(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 * (HIWORD(a1) - HIWORD(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; // r12
__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 func8(int a1, int a2, const void *a3, int a4)
{
size_t v5; // r12
__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 v6; // er12
int v7; // ebx
void *result; // rax
const void *v9; // r14

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

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

v6 = a4 - a3;
v7 = a1;
result = func2(a5, a4 - a3);
if ( a1 < a2 )
{
v9 = result;
do
result = func8(v7++, a3, v9, v6);
while ( a2 != v7 );
}
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 *result; // rax
char *v7; // r12
int v8; // ebp
int v9; // eax

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

canonical_decompiled_code:
char *__fastcall func11(int a1, int a2, int a3, int a4)
{
char *result; // rax
char *v7; // r12
int v8; // ebp
int v9; // eax

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

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

fbrows = a3;
fbroff = a1;
fbcoff = a2;
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

fbrows = a3;
fbroff = a1;
fbcoff = a2;
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

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

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

v0 = (int *)fonts;
func5();
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 && fbcoff >= a2 )
{
v3 = result;
fb_box(-a2, 0, -a2, a2 + fbcols, result);
fb_box(fbrows, fbrows + a2, -a2, a2 + fbcols, v3);
fb_box(-a2, a2 + fbrows, -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 && fbcoff >= a2 )
{
v3 = result;
func9(-a2, 0, -a2, a2 + fbcols, result);
func9(fbrows, fbrows + a2, -a2, a2 + fbcols, v3);
func9(-a2, a2 + fbrows, -a2, 0, v3);
result = (__int64)func9(-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:
unsigned __int64 __fastcall pad_put(int a1, int a2, __int64 a3, int a4, int a5)
{
int v7; // ebp
int v8; // eax
int v9; // edx
int v10; // er12
int v11; // ecx
char *v12; // rbx
unsigned __int64 result; // rax
int i; // er13
int v15; // edi
int v16; // edx
unsigned int v17; // eax

v7 = fnrows * a2;
v8 = fnsel(a4, a5);
v10 = v9;
v12 = ch2fb(v8, a1, a4, v11);
if ( v12 || (v12 = ch2fb(0, a1, a4, a5)) != 0LL )
{
result = (unsigned int)fnrows;
for ( i = 0; fnrows > i; result = (unsigned __int64)fb_set(v15, v10, &v12[bpp * v16], fncols) )
{
v15 = i + v7;
v16 = i * fncols;
++i;
}
}
else
{
v17 = color2fb(a5 & 0xFFFFFF);
result = (unsigned __int64)fb_box(v7, v7 + fnrows, v10, v10 + fncols, v17);
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func15(int a1, int a2, __int64 a3, int a4, int a5)
{
int v7; // ebp
int v8; // eax
int v9; // edx
int v10; // er12
int v11; // ecx
char *v12; // rbx
unsigned __int64 result; // rax
int i; // er13
int v15; // edi
int v16; // edx
unsigned int v17; // eax

v7 = fnrows * a2;
v8 = func3(a4, a5);
v10 = v9;
v12 = func11(v8, a1, a4, v11);
if ( v12 || (v12 = func11(0, a1, a4, a5)) != 0LL )
{
result = (unsigned int)fnrows;
for ( i = 0; fnrows > i; result = (unsigned __int64)func8(v15, v10, &v12[bpp * v16], fncols) )
{
v15 = i + v7;
v16 = i * fncols;
++i;
}
}
else
{
v17 = func6(a5 & 0xFFFFFF);
result = (unsigned __int64)func9(v7, v7 + fnrows, v10, v10 + fncols, v17);
}
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, a3 * fncols, 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 func9(fnrows * a1, v6, a3 * fncols, 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;
}


decompiled_code:
__int64 pad_init()
{
int v1; // eax
__int64 v2; // rdx
int v3; // eax
__int64 v4; // rdx
__int64 v5; // rdx
int v6; // er13
int v7; // eax

if ( (unsigned int)pad_font((__int64)"/path/to/font.tf", 0LL, 0LL) || gc_init() )
return 1LL;
v1 = fb_rows();
v2 = (unsigned int)(v1 >> 31);
LODWORD(v2) = v1 % fnrows;
rows = v1 / fnrows;
v3 = fb_cols("/path/to/font.tf", 0LL, v2);
v4 = (unsigned int)(v3 >> 31);
LODWORD(v4) = v3 % fncols;
cols = v3 / fncols;
bpp = ((unsigned int)fb_mode("/path/to/font.tf", 0LL, v4) >> 16) & 0xF;
v6 = fb_cols("/path/to/font.tf", 0LL, v5);
v7 = fb_rows();
pad_conf(0, 0, v7, v6);
return 0LL;
}

canonical_decompiled_code:
__int64 func20()
{
int v1; // eax
__int64 v2; // rdx
int v3; // eax
__int64 v4; // rdx
__int64 v5; // rdx
int v6; // er13
int v7; // eax

if ( (unsigned int)func19((__int64)"/path/to/font.tf", 0LL, 0LL) || func4() )
return 1LL;
v1 = fb_rows();
v2 = (unsigned int)(v1 >> 31);
LODWORD(v2) = v1 % fnrows;
rows = v1 / fnrows;
v3 = fb_cols("/path/to/font.tf", 0LL, v2);
v4 = (unsigned int)(v3 >> 31);
LODWORD(v4) = v3 % fncols;
cols = v3 / fncols;
bpp = ((unsigned int)fb_mode("/path/to/font.tf", 0LL, v4) >> 16) & 0xF;
v6 = fb_cols("/path/to/font.tf", 0LL, v5);
v7 = fb_rows();
func12(0, 0, v7, v6);
return 0LL;
}

original_code:
int pad_init(void)
{
 if (pad_font("/path/to/font.tf", ((void *)0), ((void *)0)))
  return 1;
 if (gc_init())
  return 1;
 rows = fb_rows() / fnrows;
 cols = fb_cols() / fncols;
 bpp = (((fb_mode()) >> 16) & 0x0f);
 pad_conf(0, 0, fb_rows(), fb_cols());
 return 0;
}

canonical_original_code:
int pad_init()
{
 if (pad_font("/path/to/font.tf", ((void *)0), ((void *)0)))
  return 1;
 if (gc_init())
  return 1;
 rows = fb_rows() / fnrows;
 cols = fb_cols() / fncols;
 bpp = (((fb_mode()) >> 16) & 0x0f);
 pad_conf(0, 0, fb_rows(), fb_cols());
 return 0;
}


decompiled_code:
char *pad_fbdev()
{
unsigned int v0; // ebp
unsigned int v1; // ebx
__int64 v2; // r13
__int64 v3; // r12
const char *v4; // rax

v0 = fbrows;
v1 = fbcols;
v2 = (unsigned int)fbroff;
v3 = (unsigned int)fbcoff;
v4 = (const char *)fb_dev();
snprintf(fbdev_0, 0x400uLL, "FBDEV=%s:%dx%d%+d%+d", v4, v1, v0, v3, v2);
return fbdev_0;
}

canonical_decompiled_code:
char *func21()
{
unsigned int v0; // ebp
unsigned int v1; // ebx
__int64 v2; // r13
__int64 v3; // r12
const char *v4; // rax

v0 = fbrows;
v1 = fbcols;
v2 = (unsigned int)fbroff;
v3 = (unsigned int)fbcoff;
v4 = (const char *)fb_dev();
snprintf(fbdev_0, 0x400uLL, "FBDEV=%s:%dx%d%+d%+d", v4, v1, v0, v3, v2);
return fbdev_0;
}

original_code:
char *pad_fbdev(void)
{
 static char fbdev[1024];
 snprintf(fbdev, sizeof(fbdev), "FBDEV=%s:%dx%d%+d%+d",
  fb_dev(), fbcols, fbrows, fbcoff, fbroff);
 return fbdev;
}

canonical_original_code:
char *pad_fbdev()
{
 static char fbdev[1024];
 snprintf(fbdev, sizeof(fbdev), "FBDEV=%s:%dx%d%+d%+d",
  fb_dev(), fbcols, fbrows, fbcoff, fbroff);
 return fbdev;
}

variable_types:
fbdev
char [1024]


decompiled_code:
__int64 pad_crows()
{
return (unsigned int)fnrows;
}

canonical_decompiled_code:
__int64 func22()
{
return (unsigned int)fnrows;
}

original_code:
int pad_crows(void)
{
 return fnrows;
}

canonical_original_code:
int pad_crows()
{
 return fnrows;
}


decompiled_code:
__int64 pad_ccols()
{
return (unsigned int)fncols;
}

canonical_decompiled_code:
__int64 func23()
{
return (unsigned int)fncols;
}

original_code:
int pad_ccols(void)
{
 return fncols;
}

canonical_original_code:
int pad_ccols()
{
 return fncols;
}


decompiled_code:
__int64 __fastcall find_glyph(__int64 a1, int a2)
{
int v3; // edx
int v4; // esi
__int64 v5; // rcx
int v6; // eax
unsigned int v7; // er10

v3 = *(_DWORD *)(a1 + 8);
v4 = 0;
while ( v4 < v3 )
{
v5 = *(_QWORD *)(a1 + 16);
v6 = (v4 + v3) / 2;
v7 = v6;
if ( *(_DWORD *)(v5 + 4LL * v6) == a2 )
return v7;
if ( *(_DWORD *)(v5 + 4LL * v6) <= a2 )
v4 = v6 + 1;
else
v3 = (v4 + v3) / 2;
}
return (unsigned int)-1;
}

original_code:
static int find_glyph(struct font *font, int c)
{
 int l = 0;
 int h = font->n;
 while (l < h) {
  int m = (l + h) / 2;
  if (font->glyphs[m] == c)
   return m;
  if (c < font->glyphs[m])
   h = m;
  else
   l = m + 1;
 }
 return -1;
}

variable_types: struct font { int rows; int n; int *glyphs; char *data; } *

variable_types: l int

variable_types: h int

variable_types: m int


xread

decompiled_code:
void *__fastcall xread(int fd, int a2)
{
void *v2; // rax
void *v3; // r12
void *v4; // rdi

v2 = malloc(a2);
v3 = v2;
if ( !v2 || read(fd, v2, a2) != a2 )
{
v4 = v3;
v3 = 0LL;
free(v4);
}
return v3;
}

canonical_decompiled_code:
void *__fastcall func1(int fd, int a2)
{
void *v2; // rax
void *v3; // r12
void *v4; // rdi

v2 = malloc(a2);
v3 = v2;
if ( !v2 || read(fd, v2, a2) != a2 )
{
v4 = v3;
v3 = 0LL;
free(v4);
}
return v3;
}

original_code:
static void *xread(int fd, int len)
{
 void *buf = malloc(len);
 if (buf && read(fd, buf, len) == len)
  return buf;
 free(buf);
 return ((void *)0);
}

variable_types: fd int

variable_types: len int

variable_types:
buf
void *

return_type: void *


decompiled_code:
__int64 __fastcall font_bitmap(__int64 a1, void *a2, int a3)
{
int v3; // eax
__int64 v4; // r11
unsigned int v5; // er8

v3 = find_glyph(a1, a3);
v5 = 1;
if ( v3 >= 0 )
{
memcpy(
a2,
(const void *)(*(_QWORD *)(v4 + 24) + *(_DWORD *)(v4 + 4) * *(_DWORD *)v4 * v3),
*(_DWORD *)(v4 + 4) * *(_DWORD *)v4);
v5 = 0;
}
return v5;
}

original_code:
int font_bitmap(struct font *font, void *dst, int c)
{
 int i = find_glyph(font, c);
 int len = font->rows * font->cols;
 if (i < 0)
  return 1;
 memcpy(dst, font->data + i * len, len);
 return 0;
}

variable_types:
dst
void *

variable_types: i int

user_defined_types: struct font { int rows; int n; int *glyphs; char *data; }


decompiled_code:
void __fastcall font_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = (void *)*((_QWORD *)ptr + 3);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = (void *)*((_QWORD *)ptr + 3);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
free(ptr);
}

original_code:
void font_free(struct font *font)
{
 if (font->data)
  free(font->data);
 if (font->glyphs)
  free(font->glyphs);
 free(font);
}


decompiled_code:
__int64 __fastcall font_rows(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
return *a1;
}

original_code:
int font_rows(struct font *font)
{
 return font->rows;
}


decompiled_code:
__int64 __fastcall font_cols(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
int font_cols(struct font *font)
{
 return font->cols;
}


decompiled_code:
__int64 __fastcall scr_snap(int a1)
{
__int64 v1; // r13
unsigned int v2; // ebx
__int64 result; // rax
int v4; // ebx
__int64 v5; // r12
int v6; // ebp
__int64 v7; // rdi
const void *v8; // rsi
void *v9; // rdi
int v10; // eax

v1 = a1;
v2 = fb_mode();
result = fb_cols();
if ( a1 <= 127 )
{
v4 = result * (HIWORD(v2) & 0xF);
if ( scrs[a1] || (v10 = fb_rows(), result = (__int64)malloc(v4 * v10), (scrs[a1] = result) != 0) )
{
v5 = 0LL;
v6 = 0;
while ( 1 )
{
result = fb_rows();
if ( (int)result <= v6 )
break;
v7 = (unsigned int)v6++;
v8 = (const void *)fb_mem(v7);
v9 = (void *)(v5 + scrs[v1]);
v5 += v4;
memcpy(v9, v8, v4);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // r13
unsigned int v2; // ebx
__int64 result; // rax
int v4; // ebx
__int64 v5; // r12
int v6; // ebp
__int64 v7; // rdi
const void *v8; // rsi
void *v9; // rdi
int v10; // eax

v1 = a1;
v2 = fb_mode();
result = fb_cols();
if ( a1 <= 127 )
{
v4 = result * (HIWORD(v2) & 0xF);
if ( scrs[a1] || (v10 = fb_rows(), result = (__int64)malloc(v4 * v10), (scrs[a1] = result) != 0) )
{
v5 = 0LL;
v6 = 0;
while ( 1 )
{
result = fb_rows();
if ( (int)result <= v6 )
break;
v7 = (unsigned int)v6++;
v8 = (const void *)fb_mem(v7);
v9 = (void *)(v5 + scrs[v1]);
v5 += v4;
memcpy(v9, v8, v4);
}
}
}
return result;
}

original_code:
void scr_snap(int idx)
{
 int rowsz = (((fb_mode()) >> 16) & 0x0f) * fb_cols();
 int i;
 if (idx < 128 && !scrs[idx])
  scrs[idx] = malloc(fb_rows() * rowsz);
 if (idx < 128 && scrs[idx])
  for (i = 0; i < fb_rows(); i++)
   memcpy(scrs[idx] + i * rowsz, fb_mem(i), rowsz);
}

variable_types: idx int

variable_types: rowsz int


decompiled_code:
void __fastcall scr_free(int a1)
{
if ( a1 <= 127 )
{
free((void *)scrs[a1]);
scrs[a1] = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 <= 127 )
{
free((void *)scrs[a1]);
scrs[a1] = 0LL;
}
}

original_code:
void scr_free(int idx)
{
 if (idx < 128) {
  free(scrs[idx]);
  scrs[idx] = ((void *)0);
 }
}


decompiled_code:
__int64 __fastcall scr_load(int a1)
{
__int64 v1; // r13
unsigned int v2; // ebx
int v3; // eax
int v4; // ebp
unsigned int v5; // eax
__int64 v6; // r12
int v7; // ebx
size_t v8; // rbp
__int64 v9; // rdi
const void *v10; // r14
void *v11; // rdi

v1 = a1;
v2 = fb_mode();
v3 = fb_cols();
if ( a1 <= 127 && scrs[a1] )
{
v4 = v3;
v5 = v2;
v6 = 0LL;
v7 = 0;
v8 = (int)(v4 * (HIWORD(v5) & 0xF));
while ( (int)fb_rows() > v7 )
{
v9 = (unsigned int)v7++;
v10 = (const void *)(v6 + scrs[v1]);
v11 = (void *)fb_mem(v9);
v6 += v8;
memcpy(v11, v10, v8);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 v1; // r13
unsigned int v2; // ebx
int v3; // eax
int v4; // ebp
unsigned int v5; // eax
__int64 v6; // r12
int v7; // ebx
size_t v8; // rbp
__int64 v9; // rdi
const void *v10; // r14
void *v11; // rdi

v1 = a1;
v2 = fb_mode();
v3 = fb_cols();
if ( a1 <= 127 && scrs[a1] )
{
v4 = v3;
v5 = v2;
v6 = 0LL;
v7 = 0;
v8 = (int)(v4 * (HIWORD(v5) & 0xF));
while ( (int)fb_rows() > v7 )
{
v9 = (unsigned int)v7++;
v10 = (const void *)(v6 + scrs[v1]);
v11 = (void *)fb_mem(v9);
v6 += v8;
memcpy(v11, v10, v8);
}
}
return 0LL;
}

original_code:
int scr_load(int idx)
{
 int rowsz = (((fb_mode()) >> 16) & 0x0f) * fb_cols();
 int i;
 if (idx < 128 && scrs[idx])
  for (i = 0; i < fb_rows(); i++)
   memcpy(fb_mem(i), scrs[idx] + i * rowsz, rowsz);
 return 0;
}


decompiled_code:
void scr_done()
{
__int64 *v0; // rbx
void *v1; // rdi

v0 = scrs;
do
{
v1 = (void *)*v0++;
free(v1);
}
while ( v0 != (__int64 *)&unk_5A0 );
}

original_code:
void scr_done(void)
{
 int i;
 for (i = 0; i < 128; i++)
  free(scrs[i]);
}

canonical_original_code:
void scr_done()
{
 int i;
 for (i = 0; i < 128; i++)
  free(scrs[i]);
}


decompiled_code:
__int64 fb_len()
{
return (unsigned int)(dword_FCC * dword_F90);
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)(dword_FCC * dword_F90);
}

original_code:
static int fb_len(void)
{
 return finfo.line_length * vinfo.yres_virtual;
}

canonical_original_code:
static int fb_len()
{
 return finfo.line_length * vinfo.yres_virtual;
}