decompiled_code:
__int64 __fastcall nduid_to_serialno(const char *a1, __int64 a2)
{
const char *v2; // rbp
unsigned int v3; // er12
int v4; // ebx
int v5; // er14
int v6; // eax
_BYTE *v7; // rax
char v9[104]; // [rsp+10h] [rbp-68h] BYREF

strcpy(v9, "0123456789ABCDEFGHIJKLMNOPQRSTUV");
if ( (unsigned int)strlen(a1) != 40 )
return 0xFFFFFFFFLL;
v2 = a1;
v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = hexval((unsigned int)*v2);
if ( v6 < 0 )
break;
v3 |= (v6 & 0xF) << (4 * v4++);
if ( v4 == 5 )
{
v7 = (_BYTE *)(a2 + v5);
do
{
*v7 = v9[v3 & 0x1F];
v3 >>= 5;
++v7;
}
while ( v7 != (_BYTE *)(a2 + 4 + v5) );
v5 += 4;
v3 = 0;
v4 = 0;
}
if ( ++v2 == a1 + 40 )
{
*(_BYTE *)(a2 + v5) = 0;
return 0LL;
}
}
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, __int64 a2)
{
const char *v2; // rbp
unsigned int v3; // er12
int v4; // ebx
int v5; // er14
int v6; // eax
_BYTE *v7; // rax
char v9[104]; // [rsp+10h] [rbp-68h] BYREF

strcpy(v9, "0123456789ABCDEFGHIJKLMNOPQRSTUV");
if ( (unsigned int)strlen(a1) != 40 )
return 0xFFFFFFFFLL;
v2 = a1;
v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
v6 = hexval((unsigned int)*v2);
if ( v6 < 0 )
break;
v3 |= (v6 & 0xF) << (4 * v4++);
if ( v4 == 5 )
{
v7 = (_BYTE *)(a2 + v5);
do
{
*v7 = v9[v3 & 0x1F];
v3 >>= 5;
++v7;
}
while ( v7 != (_BYTE *)(a2 + 4 + v5) );
v5 += 4;
v3 = 0;
v4 = 0;
}
if ( ++v2 == a1 + 40 )
{
*(_BYTE *)(a2 + v5) = 0;
return 0LL;
}
}
return 4294967294LL;
}

original_code:
int nduid_to_serialno(char *nduid, char *serialno) {
 int nduid_len, i, j, k;
 char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
 uint32_t val;
 int hval, l;
 nduid_len = strlen(nduid);
 if (nduid_len != 40) return -1;
 for (i = 0, k = 0, j = 0, val = 0; i < nduid_len; i++) {
  hval = hexval(nduid[i]);
  if (hval < 0) return -2;
  val |= ((unsigned)hval & 0xF) << (k * 4);
  k++;
  if (k == 5) {
   for (l = 0; l < 4; l++) {
    serialno[j++] = charset[(val & 0x1F)];
    val >>= 5;
   }
   k = 0;
   val = 0;
  }
 }
 serialno[j] = '\0';
 return 0;
}

canonical_original_code:
int nduid_to_serialno(char *nduid, char *serialno) {
 int nduid_len; int i; int j; int k;
 char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
 unsigned int val;
 int hval; int l;
 nduid_len = strlen(nduid);
 if (nduid_len != 40) return -1;
 for (i = 0, k = 0, j = 0, val = 0; i < nduid_len; i++) {
  hval = hexval(nduid[i]);
  if (hval < 0) return -2;
  val |= ((unsigned int)hval & 0xF) << (k * 4);
  k++;
  if (k == 5) {
   for (l = 0; l < 4; l++) {
    serialno[j++] = charset[(val & 0x1F)];
    val >>= 5;
   }
   k = 0;
   val = 0;
  }
 }
 serialno[j] = '\0';
 return 0;
}

variable_types:
nduid
char *

variable_types:
serialno
char *

variable_types: j int

variable_types: k int

variable_types:
charset
char []

variable_types:
val
unsigned int

variable_types: hval int

variable_types: l int


repo: willcast/nsboot

decompiled_code:
char __fastcall check_keep_arg(char *s1)
{
const char **v2; // rbx
const char *v3; // rbp
size_t v4; // rax
char *v5; // rbp
bool v6; // zf
size_t v7; // rax
char *v8; // rbx
char v10[120]; // [rsp+0h] [rbp-78h] BYREF

v2 = (const char **)&keepable_args;
do
{
v3 = *v2;
v4 = strlen(*v2);
if ( !strncmp(s1, v3, v4) )
{
strcat(kept_cmdline, s1);
v5 = kept_cmdline;
*(_WORD *)&v5[strlen(kept_cmdline)] = 32;
}
++v2;
}
while ( v2 != (const char **)(unsigned int)((_DWORD)&keepable_args + 48) );
v6 = memcmp(s1, "nduid=", 6uLL) == 0;
LOBYTE(v7) = !v6;
if ( v6 )
{
if ( (int)nduid_to_serialno(s1 + 6, (__int64)v10) < 0 )
{
LOBYTE(v7) = logprintf("1error generating android serialno");
}
else
{
strcat(kept_cmdline, v10);
v8 = kept_cmdline;
v7 = strlen(kept_cmdline);
*(_WORD *)&v8[v7] = 32;
}
}
return v7;
}

canonical_decompiled_code:
char __fastcall func6(char *s1)
{
const char **v2; // rbx
const char *v3; // rbp
size_t v4; // rax
char *v5; // rbp
bool v6; // zf
size_t v7; // rax
char *v8; // rbx
char v10[120]; // [rsp+0h] [rbp-78h] BYREF

v2 = (const char **)&keepable_args;
do
{
v3 = *v2;
v4 = strlen(*v2);
if ( !strncmp(s1, v3, v4) )
{
strcat(kept_cmdline, s1);
v5 = kept_cmdline;
*(_WORD *)&v5[strlen(kept_cmdline)] = 32;
}
++v2;
}
while ( v2 != (const char **)(unsigned int)((_DWORD)&keepable_args + 48) );
v6 = memcmp(s1, "nduid=", 6uLL) == 0;
LOBYTE(v7) = !v6;
if ( v6 )
{
if ( (int)func5(s1 + 6, (__int64)v10) < 0 )
{
LOBYTE(v7) = logprintf("1error generating android serialno");
}
else
{
strcat(kept_cmdline, v10);
v8 = kept_cmdline;
v7 = strlen(kept_cmdline);
*(_WORD *)&v8[v7] = 32;
}
}
return v7;
}

original_code:
void check_keep_arg(char *arg) {
 char *nduid, serialno[68];
 for (int i = 0; i < (sizeof(keepable_args) / sizeof(*(keepable_args))); ++i) {
  const char *chk_arg = keepable_args[i];
  if (!strncmp(arg, chk_arg, strlen(chk_arg))) {
   strcat(kept_cmdline, arg);
   strcat(kept_cmdline, " ");
  }
 }
 if (!strncmp(arg, "nduid=", 6)) {
  nduid = arg + 6;
  if (nduid_to_serialno(nduid, serialno) < 0) {
   logprintf("1error generating android serialno");
   return;
  }
  strcat(kept_cmdline, serialno);
  strcat(kept_cmdline, " ");
 }
}

canonical_original_code:
void check_keep_arg(char *arg) {
 char *nduid; char serialno[68];
 for (int i = 0; i < (sizeof(keepable_args) / sizeof(*(keepable_args))); ++i) {
  char *chk_arg = keepable_args[i];
  if (!strncmp(arg, chk_arg, strlen(chk_arg))) {
   strcat(kept_cmdline, arg);
   strcat(kept_cmdline, " ");
  }
 }
 if (!strncmp(arg, "nduid=", 6)) {
  nduid = arg + 6;
  if (nduid_to_serialno(nduid, serialno) < 0) {
   logprintf("1error generating android serialno");
   return;
  }
  strcat(kept_cmdline, serialno);
  strcat(kept_cmdline, " ");
 }
}

variable_types:
arg
char *

variable_types:
serialno
char [68]

variable_types: i int

return_type: void


decompiled_code:
char *parse_proc_cmdline()
{
int v0; // eax
int v1; // ebx
char *result; // rax
char *i; // rdi
int *v4; // rax
char *v5; // rax
char v6[520]; // [rsp+0h] [rbp-208h] BYREF

kept_cmdline = (char *)malloc(0x200uLL);
*kept_cmdline = 0;
v0 = open("/proc/cmdline", 0);
if ( v0 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
result = (char *)logprintf("2%s: %s", "1error opening /proc/cmdline", v5);
}
else
{
v1 = v0;
v6[(int)read(v0, v6, 0x1FFuLL)] = 0;
close(v1);
result = strtok(v6, " ");
for ( i = result; result; i = result )
{
check_keep_arg(i);
result = strtok(0LL, " ");
}
}
return result;
}

canonical_decompiled_code:
char *func7()
{
int v0; // eax
int v1; // ebx
char *result; // rax
char *i; // rdi
int *v4; // rax
char *v5; // rax
char v6[520]; // [rsp+0h] [rbp-208h] BYREF

kept_cmdline = (char *)malloc(0x200uLL);
*kept_cmdline = 0;
v0 = open("/proc/cmdline", 0);
if ( v0 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
result = (char *)logprintf("2%s: %s", "1error opening /proc/cmdline", v5);
}
else
{
v1 = v0;
v6[(int)read(v0, v6, 0x1FFuLL)] = 0;
close(v1);
result = strtok(v6, " ");
for ( i = result; result; i = result )
{
func6(i);
result = strtok(0LL, " ");
}
}
return result;
}

original_code:
void parse_proc_cmdline(void) {
 int cmdline_fd;
 int nchars;
 char cmdline[512];
 char *cur_arg;
 kept_cmdline = malloc(512);
 kept_cmdline[0] = '\0';
 cmdline_fd = open("/proc/cmdline", 00);
 if (cmdline_fd < 0) {
  logprintf("2%s: %s", "1error opening /proc/cmdline", strerror((*__errno_location ())));
  return;
 }
 nchars = read(cmdline_fd, cmdline, 512 - 1);
 cmdline[nchars] = '\0';
 close(cmdline_fd);
 cur_arg = strtok(cmdline, " ");
 while (cur_arg != ((void *)0)) {
  check_keep_arg(cur_arg);
  cur_arg = strtok(((void *)0), " ");
 }
}

canonical_original_code:
void parse_proc_cmdline() {
 int cmdline_fd;
 int nchars;
 char cmdline[512];
 char *cur_arg;
 kept_cmdline = malloc(512);
 kept_cmdline[0] = '\0';
 cmdline_fd = open("/proc/cmdline", 00);
 if (cmdline_fd < 0) {
  logprintf("2%s: %s", "1error opening /proc/cmdline", strerror((*__errno_location ())));
  return;
 }
 nchars = read(cmdline_fd, cmdline, 512 - 1);
 cmdline[nchars] = '\0';
 close(cmdline_fd);
 cur_arg = strtok(cmdline, " ");
 while (cur_arg != ((void *)0)) {
  check_keep_arg(cur_arg);
  cur_arg = strtok(((void *)0), " ");
 }
}

variable_types: nchars int

variable_types:
cmdline
char [512]


hexval

decompiled_code:
__int64 __fastcall hexval(char a1)
{
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 5u )
return (unsigned int)(a1 - 87);
result = (unsigned int)(a1 - 55);
if ( (unsigned __int8)(a1 - 65) >= 6u )
result = 0xFFFFFFFFLL;
return result;
}

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

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 97) <= 5u )
return (unsigned int)(a1 - 87);
result = (unsigned int)(a1 - 55);
if ( (unsigned __int8)(a1 - 65) >= 6u )
result = 0xFFFFFFFFLL;
return result;
}

variable_types: c char

return_type: int


decompiled_code:
_BOOL8 __fastcall test_file(const char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

return stat(a1, &v2) != -1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(const char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

return stat(a1, &v2) != -1;
}

original_code:
int test_file(const char *path) {
 struct stat test;
 if (stat(path, &test) == -1) return 0;
 return 1;
}

canonical_original_code:
int test_file(char *path) {
 struct stat test;
 if (stat(path, &test) == -1) return 0;
 return 1;
}

variable_types:
path
char *

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

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


decompiled_code:
void __fastcall cp_file(char *file, char *a2)
{
void *v2; // rbp
int v3; // er12
int v4; // er13
size_t v5; // rax
size_t v6; // rbx
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax

if ( test_file(file) )
{
v2 = malloc(0x10000uLL);
if ( v2 )
{
v3 = open(file, 0);
if ( v3 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
logprintf("2%s: %s", "2can't open source file", v10);
}
else
{
v4 = open(a2, 577);
if ( v4 < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
logprintf("2%s: %s", "2can't open destination file", v12);
close(v3);
}
else
{
while ( 1 )
{
v5 = read(v3, v2, 0x10000uLL);
v6 = v5;
if ( v5 == -1LL )
break;
write(v4, v2, v5);
if ( v6 != 0x10000 )
goto LABEL_13;
}
v13 = _errno_location();
v14 = strerror(*v13);
logprintf("2%s: %s", "error reading from source file", v14);
LABEL_13:
close(v3);
close(v4);
free(v2);
}
}
}
else
{
v7 = _errno_location();
v8 = strerror(*v7);
logprintf("2%s: %s", "2can't allocate buffer", v8);
}
}
else
{
logprintf("1file %s doesn't exist, so can't copy", file);
}
}

canonical_decompiled_code:
void __fastcall func5(char *file, char *a2)
{
void *v2; // rbp
int v3; // er12
int v4; // er13
size_t v5; // rax
size_t v6; // rbx
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax

if ( func4(file) )
{
v2 = malloc(0x10000uLL);
if ( v2 )
{
v3 = open(file, 0);
if ( v3 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
logprintf("2%s: %s", "2can't open source file", v10);
}
else
{
v4 = open(a2, 577);
if ( v4 < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
logprintf("2%s: %s", "2can't open destination file", v12);
close(v3);
}
else
{
while ( 1 )
{
v5 = read(v3, v2, 0x10000uLL);
v6 = v5;
if ( v5 == -1LL )
break;
write(v4, v2, v5);
if ( v6 != 0x10000 )
goto LABEL_13;
}
v13 = _errno_location();
v14 = strerror(*v13);
logprintf("2%s: %s", "error reading from source file", v14);
LABEL_13:
close(v3);
close(v4);
free(v2);
}
}
}
else
{
v7 = _errno_location();
v8 = strerror(*v7);
logprintf("2%s: %s", "2can't allocate buffer", v8);
}
}
else
{
logprintf("1file %s doesn't exist, so can't copy", file);
}
}

original_code:
void cp_file(const char *src, const char *dest) {
 int src_fd, dest_fd;
 char *buf;
 size_t size;
 if (!test_file(src)) {
  logprintf("1file %s doesn't exist, so can't copy", src);
  return;
 }
 buf = malloc(65536);
 if (buf == ((void *)0)) {
  logprintf("2%s: %s", "2can't allocate buffer", strerror((*__errno_location ())));
  return;
 }
 src_fd = open(src, 00);
 if (src_fd < 0) {
  logprintf("2%s: %s", "2can't open source file", strerror((*__errno_location ())));
  return;
 }
 dest_fd = open(dest, 01|0100|01000);
 if (dest_fd < 0) {
  logprintf("2%s: %s", "2can't open destination file", strerror((*__errno_location ())));
  close(src_fd);
  return;
 }
 do {
  size = read(src_fd, buf, 65536);
  if (size == -1) {
   logprintf("2%s: %s", "error reading from source file", strerror((*__errno_location ())));
   break;
  }
  write(dest_fd, buf, size);
  if (size == -1) {
   logprintf("2%s: %s", "error writing to destination file", strerror((*__errno_location ())));
   break;
  }
 } while (size == 65536);
 close(src_fd);
 close(dest_fd);
 free(buf);
}

canonical_original_code:
void cp_file(char *src, char *dest) {
 int src_fd; int dest_fd;
 char *buf;
 unsigned long size;
 if (!test_file(src)) {
  logprintf("1file %s doesn't exist, so can't copy", src);
  return;
 }
 buf = malloc(65536);
 if (buf == ((void *)0)) {
  logprintf("2%s: %s", "2can't allocate buffer", strerror((*__errno_location ())));
  return;
 }
 src_fd = open(src, 00);
 if (src_fd < 0) {
  logprintf("2%s: %s", "2can't open source file", strerror((*__errno_location ())));
  return;
 }
 dest_fd = open(dest, 01|0100|01000);
 if (dest_fd < 0) {
  logprintf("2%s: %s", "2can't open destination file", strerror((*__errno_location ())));
  close(src_fd);
  return;
 }
 do {
  size = read(src_fd, buf, 65536);
  if (size == -1) {
   logprintf("2%s: %s", "error reading from source file", strerror((*__errno_location ())));
   break;
  }
  write(dest_fd, buf, size);
  if (size == -1) {
   logprintf("2%s: %s", "error writing to destination file", strerror((*__errno_location ())));
   break;
  }
 } while (size == 65536);
 close(src_fd);
 close(dest_fd);
 free(buf);
}

variable_types:
src
char *

variable_types:
dest
char *

variable_types:
buf
char *

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall system_logged(char *command)
{
char *v1; // rax
char *v2; // rbp
int v3; // eax
__int64 result; // rax
char v5[4096]; // [rsp+0h] [rbp-2018h] BYREF
char dest[4120]; // [rsp+1000h] [rbp-1018h] BYREF

if ( !command )
_assert_fail("cmd != NULL", "lib.c", 0xACu, "system_logged");
strncpy(dest, command, 0x1000uLL);
dest[4095] = 0;
v1 = strchr(dest, 32);
if ( v1 )
*v1 = 0;
v2 = basename(dest);
v3 = system(command);
if ( (v3 & 0x7F) != 0 )
{
if ( __OFSUB__((v3 & 0x7F) + 1, 1) || (v3 & 0x7F) == 0 )
{
result = logprintf("2something evil happened to command %s", v2);
}
else if ( (v3 & 0x80u) == 0 )
{
result = logprintf("2command %s died from signal %d (but no core was produced)", v2, v3 & 0x7F);
}
else
{
logprintf("2command %s died from signal %d and dumped core\n", v2, v3 & 0x7F);
snprintf(v5, 0x1000uLL, "/var/core-%s", v2);
cp_file(v5, "/mnt/media/nsboot/logs");
result = logprintf("0the core dump was copied to /mnt/media/nsboot/logs/core-%s", v2);
}
}
else
{
result = (unsigned int)(v3 >> 8);
if ( (_BYTE)result )
result = logprintf("2command %s died with code %d\n", v2, (unsigned __int8)result);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *command)
{
char *v1; // rax
char *v2; // rbp
int v3; // eax
__int64 result; // rax
char v5[4096]; // [rsp+0h] [rbp-2018h] BYREF
char dest[4120]; // [rsp+1000h] [rbp-1018h] BYREF

if ( !command )
_assert_fail("cmd != NULL", "lib.c", 0xACu, "system_logged");
strncpy(dest, command, 0x1000uLL);
dest[4095] = 0;
v1 = strchr(dest, 32);
if ( v1 )
*v1 = 0;
v2 = basename(dest);
v3 = system(command);
if ( (v3 & 0x7F) != 0 )
{
if ( __OFSUB__((v3 & 0x7F) + 1, 1) || (v3 & 0x7F) == 0 )
{
result = logprintf("2something evil happened to command %s", v2);
}
else if ( (v3 & 0x80u) == 0 )
{
result = logprintf("2command %s died from signal %d (but no core was produced)", v2, v3 & 0x7F);
}
else
{
logprintf("2command %s died from signal %d and dumped core\n", v2, v3 & 0x7F);
snprintf(v5, 0x1000uLL, "/var/core-%s", v2);
func5(v5, "/mnt/media/nsboot/logs");
result = logprintf("0the core dump was copied to /mnt/media/nsboot/logs/core-%s", v2);
}
}
else
{
result = (unsigned int)(v3 >> 8);
if ( (_BYTE)result )
result = logprintf("2command %s died with code %d\n", v2, (unsigned __int8)result);
}
return result;
}

original_code:
void system_logged(char *cmd) {
 pid_t code;
 uint8_t exit_status;
 int sig_num;
 char argv_zero[4096];
 char core_path[4096];
 char *exe_name;
 char *space;
 ((void) sizeof ((cmd != ((void *)0)) ? 1 : 0), __extension__ ({ if (cmd != ((void *)0)) ; else __assert_fail ("cmd != NULL", "lib.c", 172, __extension__ __PRETTY_FUNCTION__); }));
 strncpy(argv_zero, cmd, sizeof(argv_zero));
 argv_zero[4096 -1] = '\0';
 space = strchr(argv_zero, ' ');
 if (space != ((void *)0))
  *space = '\0';
 exe_name = basename(argv_zero);
 code = system(cmd);
 if ((((code) & 0x7f) == 0)) {
  exit_status = (((code) & 0xff00) >> 8);
  if (exit_status) {
   logprintf("2command %s died with code %d\n",
    exe_name, exit_status);
   return;
  }
 } else if ((((signed char) (((code) & 0x7f) + 1) >> 1) > 0)) {
  sig_num = ((code) & 0x7f);
  if (((code) & 0x80)) {
   logprintf("2command %s died from signal %d and dumped core\n",
    exe_name, sig_num);
   snprintf(core_path, sizeof(core_path), "/var/core-%s",
    exe_name);
   cp_file(core_path, "/mnt/media/nsboot/logs");
   logprintf("0the core dump was copied to /mnt/media/nsboot/logs/core-%s",
    exe_name);
   return;
  } else {
   logprintf("2command %s died from signal %d (but no core was produced)",
    exe_name, sig_num);
   return;
  }
 } else {
  logprintf("2something evil happened to command %s", exe_name);
 }
}

canonical_original_code:
void system_logged(char *cmd) {
 int code;
 unsigned char exit_status;
 int sig_num;
 char argv_zero[4096];
 char core_path[4096];
 char *exe_name;
 char *space;
 ((void) sizeof ((cmd != ((void *)0)) ? 1 : 0), __extension__ ({ if (cmd != ((void *)0)) ; else __assert_fail ("cmd != NULL", "lib.c", 172, __extension__ __PRETTY_FUNCTION__); }));
 strncpy(argv_zero, cmd, sizeof(argv_zero));
 argv_zero[4096 -1] = '\0';
 space = strchr(argv_zero, ' ');
 if (space != ((void *)0))
  *space = '\0';
 exe_name = basename(argv_zero);
 code = system(cmd);
 if ((((code) & 0x7f) == 0)) {
  exit_status = (((code) & 0xff00) >> 8);
  if (exit_status) {
   logprintf("2command %s died with code %d\n",
    exe_name, exit_status);
   return;
  }
 } else if ((((char) (((code) & 0x7f) + 1) >> 1) > 0)) {
  sig_num = ((code) & 0x7f);
  if (((code) & 0x80)) {
   logprintf("2command %s died from signal %d and dumped core\n",
    exe_name, sig_num);
   snprintf(core_path, sizeof(core_path), "/var/core-%s",
    exe_name);
   cp_file(core_path, "/mnt/media/nsboot/logs");
   logprintf("0the core dump was copied to /mnt/media/nsboot/logs/core-%s",
    exe_name);
   return;
  } else {
   logprintf("2command %s died from signal %d (but no core was produced)",
    exe_name, sig_num);
   return;
  }
 } else {
  logprintf("2something evil happened to command %s", exe_name);
 }
}

variable_types:
cmd
char *

variable_types: code int

variable_types:
space
char *


sysprintf

decompiled_code:
void sysprintf(char *format, ...)
{
size_t v1; // rbx
char *v2; // rbp
int *v3; // rax
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = vsnprintf(0LL, 0LL, format, arg) + 1LL;
v2 = (char *)malloc(v1);
if ( v2 )
{
va_end(arg);
va_start(arg, format);
vsnprintf(v2, v1, format, arg);
system_logged(v2);
free(v2);
}
else
{
v3 = _errno_location();
v4 = strerror(*v3);
logprintf("2%s: %s", "command buffer allocation failed", v4);
}
}

canonical_decompiled_code:
void func7(char *format, ...)
{
size_t v1; // rbx
char *v2; // rbp
int *v3; // rax
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = vsnprintf(0LL, 0LL, format, arg) + 1LL;
v2 = (char *)malloc(v1);
if ( v2 )
{
va_end(arg);
va_start(arg, format);
vsnprintf(v2, v1, format, arg);
func6(v2);
free(v2);
}
else
{
v3 = _errno_location();
v4 = strerror(*v3);
logprintf("2%s: %s", "command buffer allocation failed", v4);
}
}

original_code:
void sysprintf(const char *fmt, ...) {
 va_list args;
 char *buf;
 size_t size;
 __builtin_va_start(args,fmt);
 size = vsnprintf(((void *)0), 0, fmt, args);
 __builtin_va_end(args);
 buf = malloc(size + 1);
 if (buf == ((void *)0)) {
  logprintf("2%s: %s", "command buffer allocation failed", strerror((*__errno_location ())));
  return;
 }
 __builtin_va_start(args,fmt);
 vsnprintf(buf, size + 1, fmt, args);
 __builtin_va_end(args);
 system_logged(buf);
 free(buf);
}

canonical_original_code:
void sysprintf(char *fmt, ...) {
 __builtin_va_list args;
 char *buf;
 unsigned long size;
 __builtin_va_start(args,fmt);
 size = vsnprintf(((void *)0), 0, fmt, args);
 __builtin_va_end(args);
 buf = malloc(size + 1);
 if (buf == ((void *)0)) {
  logprintf("2%s: %s", "command buffer allocation failed", strerror((*__errno_location ())));
  return;
 }
 __builtin_va_start(args,fmt);
 vsnprintf(buf, size + 1, fmt, args);
 __builtin_va_end(args);
 system_logged(buf);
 free(buf);
}

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall wipe_lv_set(const char *a1)
{
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

logprintf("0wiping volumes starting with %s", a1);
sprintf(v2, "%s-system", a1);
wipe_lv(v2);
sprintf(v2, "%s-data", a1);
wipe_lv(v2);
sprintf(v2, "%s-cache", a1);
return wipe_lv(v2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

logprintf("0wiping volumes starting with %s", a1);
sprintf(v2, "%s-system", a1);
wipe_lv(v2);
sprintf(v2, "%s-data", a1);
wipe_lv(v2);
sprintf(v2, "%s-cache", a1);
return wipe_lv(v2);
}

original_code:
void wipe_lv_set(const char *lv_set) {
 char lv[4096];
 logprintf("0wiping volumes starting with %s", lv_set);
 sprintf(lv, "%s-system", lv_set);
 wipe_lv(lv);
 sprintf(lv, "%s-data", lv_set);
 wipe_lv(lv);
 sprintf(lv, "%s-cache", lv_set);
 wipe_lv(lv);
}

canonical_original_code:
void wipe_lv_set(char *lv_set) {
 char lv[4096];
 logprintf("0wiping volumes starting with %s", lv_set);
 sprintf(lv, "%s-system", lv_set);
 wipe_lv(lv);
 sprintf(lv, "%s-data", lv_set);
 wipe_lv(lv);
 sprintf(lv, "%s-cache", lv_set);
 wipe_lv(lv);
}

variable_types:
lv
char [4096]


decompiled_code:
__int64 __fastcall umount_lv_set(const char *a1)
{
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

logprintf("0unmounting volumes starting with %s", a1);
sprintf(v2, "%s-system", a1);
umount_lv(v2);
sprintf(v2, "%s-data", a1);
umount_lv(v2);
sprintf(v2, "%s-cache", a1);
return umount_lv(v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

logprintf("0unmounting volumes starting with %s", a1);
sprintf(v2, "%s-system", a1);
umount_lv(v2);
sprintf(v2, "%s-data", a1);
umount_lv(v2);
sprintf(v2, "%s-cache", a1);
return umount_lv(v2);
}

original_code:
void umount_lv_set(const char *lv_set) {
 char lv[4096];
 logprintf("0unmounting volumes starting with %s", lv_set);
 sprintf(lv, "%s-system", lv_set);
 umount_lv(lv);
 sprintf(lv, "%s-data", lv_set);
 umount_lv(lv);
 sprintf(lv, "%s-cache", lv_set);
 umount_lv(lv);
}

canonical_original_code:
void umount_lv_set(char *lv_set) {
 char lv[4096];
 logprintf("0unmounting volumes starting with %s", lv_set);
 sprintf(lv, "%s-system", lv_set);
 umount_lv(lv);
 sprintf(lv, "%s-data", lv_set);
 umount_lv(lv);
 sprintf(lv, "%s-cache", lv_set);
 umount_lv(lv);
}


decompiled_code:
int __fastcall mount_lv_set(const char *a1)
{
int result; // eax
int *v2; // rax
char *v3; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
char v8[4096]; // [rsp+0h] [rbp-2008h] BYREF
char s[4104]; // [rsp+1000h] [rbp-1008h] BYREF

logprintf("0mounting volumes starting with %s", a1);
sprintf(v8, "/mnt/%s", a1);
mkdir(v8, 0x1EDu);
sprintf(v8, "/mnt/%s/system", a1);
mkdir(v8, 0x1EDu);
sprintf(s, "/dev/store/%s-system", a1);
if ( mount(s, v8, "ext4", 0x8000uLL, 0LL) == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "can't mount system volume", v3);
}
sprintf(v8, "/mnt/%s/data", a1);
mkdir(v8, 0x1EDu);
sprintf(s, "/dev/store/%s-data", a1);
if ( mount(s, v8, "ext4", 0x8000uLL, 0LL) == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
logprintf("2%s: %s", "can't mount data volume", v5);
}
sprintf(v8, "/mnt/%s/cache", a1);
mkdir(v8, 0x1EDu);
sprintf(s, "/dev/store/%s-cache", a1);
result = mount(s, v8, "ext4", 0x8000uLL, 0LL);
if ( result == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
result = logprintf("2%s: %s", "can't mount cache volume", v7);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
int result; // eax
int *v2; // rax
char *v3; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
char v8[4096]; // [rsp+0h] [rbp-2008h] BYREF
char s[4104]; // [rsp+1000h] [rbp-1008h] BYREF

logprintf("0mounting volumes starting with %s", a1);
sprintf(v8, "/mnt/%s", a1);
mkdir(v8, 0x1EDu);
sprintf(v8, "/mnt/%s/system", a1);
mkdir(v8, 0x1EDu);
sprintf(s, "/dev/store/%s-system", a1);
if ( mount(s, v8, "ext4", 0x8000uLL, 0LL) == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
logprintf("2%s: %s", "can't mount system volume", v3);
}
sprintf(v8, "/mnt/%s/data", a1);
mkdir(v8, 0x1EDu);
sprintf(s, "/dev/store/%s-data", a1);
if ( mount(s, v8, "ext4", 0x8000uLL, 0LL) == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
logprintf("2%s: %s", "can't mount data volume", v5);
}
sprintf(v8, "/mnt/%s/cache", a1);
mkdir(v8, 0x1EDu);
sprintf(s, "/dev/store/%s-cache", a1);
result = mount(s, v8, "ext4", 0x8000uLL, 0LL);
if ( result == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
result = logprintf("2%s: %s", "can't mount cache volume", v7);
}
return result;
}

original_code:
void mount_lv_set(const char *base) {
 char src[4096], targ[4096];
 logprintf("0mounting volumes starting with %s", base);
 sprintf(targ, "/mnt/%s", base);
 mkdir(targ, 0755);
 sprintf(targ, "/mnt/%s/system", base);
 mkdir(targ, 0755);
 sprintf(src, "/dev/store/%s-system", base);
 if (mount(src, targ, "ext4", MS_SILENT, ((void *)0)) == -1)
  logprintf("2%s: %s", "can't mount system volume", strerror((*__errno_location ())));
 sprintf(targ, "/mnt/%s/data", base);
 mkdir(targ, 0755);
 sprintf(src, "/dev/store/%s-data", base);
 if (mount(src, targ, "ext4", MS_SILENT, ((void *)0)) == -1)
  logprintf("2%s: %s", "can't mount data volume", strerror((*__errno_location ())));
 sprintf(targ, "/mnt/%s/cache", base);
 mkdir(targ, 0755);
 sprintf(src, "/dev/store/%s-cache", base);
 if (mount(src, targ, "ext4", MS_SILENT, ((void *)0)) == -1)
  logprintf("2%s: %s", "can't mount cache volume", strerror((*__errno_location ())));
}

canonical_original_code:
void mount_lv_set(char *base) {
 char src[4096]; char targ[4096];
 logprintf("0mounting volumes starting with %s", base);
 sprintf(targ, "/mnt/%s", base);
 mkdir(targ, 0755);
 sprintf(targ, "/mnt/%s/system", base);
 mkdir(targ, 0755);
 sprintf(src, "/dev/store/%s-system", base);
 if (mount(src, targ, "ext4", MS_SILENT, ((void *)0)) == -1)
  logprintf("2%s: %s", "can't mount system volume", strerror((*__errno_location ())));
 sprintf(targ, "/mnt/%s/data", base);
 mkdir(targ, 0755);
 sprintf(src, "/dev/store/%s-data", base);
 if (mount(src, targ, "ext4", MS_SILENT, ((void *)0)) == -1)
  logprintf("2%s: %s", "can't mount data volume", strerror((*__errno_location ())));
 sprintf(targ, "/mnt/%s/cache", base);
 mkdir(targ, 0755);
 sprintf(src, "/dev/store/%s-cache", base);
 if (mount(src, targ, "ext4", MS_SILENT, ((void *)0)) == -1)
  logprintf("2%s: %s", "can't mount cache volume", strerror((*__errno_location ())));
}

variable_types:
base
char *

variable_types:
src
char [4096]

variable_types:
targ
char [4096]


decompiled_code:
char *__fastcall deduce_lv_set(const char *a1)
{
char *v1; // rbx

v1 = basename(a1);
if ( !strncasecmp("froyo-", v1, 6uLL) || !strncasecmp("gapps-mdpi", v1, 0xAuLL) )
return strdup("android22");
if ( !strncasecmp("update-cm7", v1, 0xAuLL)
|| !strncasecmp("update-cm-7", v1, 0xBuLL)
|| !strncasecmp("cm-7", v1, 4uLL)
|| !strncasecmp("gapps-gb", v1, 8uLL) )
{
return strdup("android23");
}
if ( !strncasecmp("update-cm9", v1, 0xAuLL)
|| !strncasecmp("update-cm-9", v1, 0xBuLL)
|| !strncasecmp("cm-9", v1, 4uLL)
|| !strncasecmp("gapps-ics", v1, 9uLL) )
{
return strdup("android40");
}
if ( !strncasecmp("update-cm10-", v1, 0xAuLL)
|| !strncasecmp("update-cm-10-", v1, 0xBuLL)
|| !strncasecmp("cm-10-", v1, 6uLL)
|| !strncasecmp("skz_tenderloin-1.20", v1, 0x13uLL)
|| !strncasecmp("gapps-jb-20120", v1, 0xEuLL)
|| !strncasecmp("gapps-jb-201210", v1, 0xFuLL) )
{
return strdup("android41");
}
if ( !strncasecmp("ev", v1, 2uLL)
|| !strncasecmp("cm-10.1", v1, 7uLL)
|| !strncasecmp("skz_tenderloin-2.00", v1, 0x13uLL)
|| !strncasecmp("gapps-jb", v1, 8uLL) )
{
return strdup("android42");
}
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func3(const char *a1)
{
char *v1; // rbx

v1 = basename(a1);
if ( !strncasecmp("froyo-", v1, 6uLL) || !strncasecmp("gapps-mdpi", v1, 0xAuLL) )
return strdup("android22");
if ( !strncasecmp("update-cm7", v1, 0xAuLL)
|| !strncasecmp("update-cm-7", v1, 0xBuLL)
|| !strncasecmp("cm-7", v1, 4uLL)
|| !strncasecmp("gapps-gb", v1, 8uLL) )
{
return strdup("android23");
}
if ( !strncasecmp("update-cm9", v1, 0xAuLL)
|| !strncasecmp("update-cm-9", v1, 0xBuLL)
|| !strncasecmp("cm-9", v1, 4uLL)
|| !strncasecmp("gapps-ics", v1, 9uLL) )
{
return strdup("android40");
}
if ( !strncasecmp("update-cm10-", v1, 0xAuLL)
|| !strncasecmp("update-cm-10-", v1, 0xBuLL)
|| !strncasecmp("cm-10-", v1, 6uLL)
|| !strncasecmp("skz_tenderloin-1.20", v1, 0x13uLL)
|| !strncasecmp("gapps-jb-20120", v1, 0xEuLL)
|| !strncasecmp("gapps-jb-201210", v1, 0xFuLL) )
{
return strdup("android41");
}
if ( !strncasecmp("ev", v1, 2uLL)
|| !strncasecmp("cm-10.1", v1, 7uLL)
|| !strncasecmp("skz_tenderloin-2.00", v1, 0x13uLL)
|| !strncasecmp("gapps-jb", v1, 8uLL) )
{
return strdup("android42");
}
return 0LL;
}

original_code:
char * deduce_lv_set(const char *name) {
 char *base = basename(name);
 char *result = ((void *)0);
 if ((!strncasecmp("froyo-", base, 6)) ||
    (!strncasecmp("gapps-mdpi", base, 10)))
  result = strdup("android22");
 else if ((!strncasecmp("update-cm7", base, 10)) ||
  (!strncasecmp("update-cm-7", base, 11)) ||
  (!strncasecmp("cm-7", base, 4)) ||
  (!strncasecmp("gapps-gb", base, 8)))
  result = strdup("android23");
 else if ((!strncasecmp("update-cm9", base, 10)) ||
  (!strncasecmp("update-cm-9", base, 11)) ||
  (!strncasecmp("cm-9", base, 4)) ||
  (!strncasecmp("gapps-ics", base, 9)))
  result = strdup("android40");
 else if ((!strncasecmp("update-cm10-", base, 10)) ||
  (!strncasecmp("update-cm-10-", base, 11)) ||
  (!strncasecmp("cm-10-", base, 6)) ||
  (!strncasecmp("skz_tenderloin-1.20", base, 19)) ||
  (!strncasecmp("gapps-jb-20120", base, 14)) ||
  (!strncasecmp("gapps-jb-201210", base, 15)))
  result = strdup("android41");
 else if ((!strncasecmp("ev", base, 2)) ||
  (!strncasecmp("cm-10.1", base, 7)) ||
  (!strncasecmp("skz_tenderloin-2.00", base, 19)) ||
  (!strncasecmp("gapps-jb", base, 8)))
  result = strdup("android42");
 return result;
}

canonical_original_code:
char *deduce_lv_set(char *name) {
 char *base = basename(name);
 char *result = ((void *)0);
 if ((!strncasecmp("froyo-", base, 6)) ||
    (!strncasecmp("gapps-mdpi", base, 10)))
  result = strdup("android22");
 else if ((!strncasecmp("update-cm7", base, 10)) ||
  (!strncasecmp("update-cm-7", base, 11)) ||
  (!strncasecmp("cm-7", base, 4)) ||
  (!strncasecmp("gapps-gb", base, 8)))
  result = strdup("android23");
 else if ((!strncasecmp("update-cm9", base, 10)) ||
  (!strncasecmp("update-cm-9", base, 11)) ||
  (!strncasecmp("cm-9", base, 4)) ||
  (!strncasecmp("gapps-ics", base, 9)))
  result = strdup("android40");
 else if ((!strncasecmp("update-cm10-", base, 10)) ||
  (!strncasecmp("update-cm-10-", base, 11)) ||
  (!strncasecmp("cm-10-", base, 6)) ||
  (!strncasecmp("skz_tenderloin-1.20", base, 19)) ||
  (!strncasecmp("gapps-jb-20120", base, 14)) ||
  (!strncasecmp("gapps-jb-201210", base, 15)))
  result = strdup("android41");
 else if ((!strncasecmp("ev", base, 2)) ||
  (!strncasecmp("cm-10.1", base, 7)) ||
  (!strncasecmp("skz_tenderloin-2.00", base, 19)) ||
  (!strncasecmp("gapps-jb", base, 8)))
  result = strdup("android42");
 return result;
}

variable_types:
name
char *

variable_types:
result
char *

return_type: char *


decompiled_code:
__int64 __fastcall new_lv_set(const char *a1, int a2, int a3, int a4)
{
__int64 result; // rax
char v7[72]; // [rsp+0h] [rbp-48h] BYREF

logprintf("0%s", "creating volume set with name %s");
result = get_free_vg_space();
if ( result != -1 )
{
resize_lv("media", 4LL, a4 + a2 + a3);
sprintf(v7, "%s-system", a1);
new_lv(v7, a2);
sprintf(v7, "%s-data", a1);
new_lv(v7, a3);
sprintf(v7, "%s-cache", a1);
result = new_lv(v7, a4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, int a2, int a3, int a4)
{
__int64 result; // rax
char v7[72]; // [rsp+0h] [rbp-48h] BYREF

logprintf("0%s", "creating volume set with name %s");
result = get_free_vg_space();
if ( result != -1 )
{
resize_lv("media", 4LL, a4 + a2 + a3);
sprintf(v7, "%s-system", a1);
new_lv(v7, a2);
sprintf(v7, "%s-data", a1);
new_lv(v7, a3);
sprintf(v7, "%s-cache", a1);
result = new_lv(v7, a4);
}
return result;
}

original_code:
void new_lv_set(const char *name, int sys, int data, int cache) {
 char lv[32];
 long space;
 logprintf("0%s", "creating volume set with name %s", name);
 space = get_free_vg_space();
 if (space == -1) return;
 resize_lv("media", RS_FREE, sys + data + cache);
 sprintf(lv, "%s-system", name);
 new_lv(lv, sys);
 sprintf(lv, "%s-data", name);
 new_lv(lv, data);
 sprintf(lv, "%s-cache", name);
 new_lv(lv, cache);
}

canonical_original_code:
void new_lv_set(char *name, int sys, int data, int cache) {
 char lv[32];
 long space;
 logprintf("0%s", "creating volume set with name %s", name);
 space = get_free_vg_space();
 if (space == -1) return;
 resize_lv("media", RS_FREE, sys + data + cache);
 sprintf(lv, "%s-system", name);
 new_lv(lv, sys);
 sprintf(lv, "%s-data", name);
 new_lv(lv, data);
 sprintf(lv, "%s-cache", name);
 new_lv(lv, cache);
}

variable_types: sys int

variable_types: data int

variable_types: cache int

variable_types:
lv
char [32]

variable_types: space long


original_code:
void deduce_lv_set_size(const char *name, int *sys, int *dat, int *cac) {
 if (!strcmp(name, "android22")) {
  *sys = 256; *dat = 448; *cac = 200;
 } else if (!strcmp(name, "android23")) {
  *sys = 304; *dat = 504; *cac = 200;
 } else if (!strcmp(name, "android40")) {
  *sys = 352; *dat = 754; *cac = 200;
 } else if (!strcmp(name, "android41")) {
  *sys = 448; *dat = 754; *cac = 200;
 } else if (!strcmp(name, "android42")) {
  *sys = 448; *dat = 754; *cac = 200;
 }
}

canonical_original_code:
void deduce_lv_set_size(char *name, int *sys, int *dat, int *cac) {
 if (!strcmp(name, "android22")) {
  *sys = 256; *dat = 448; *cac = 200;
 } else if (!strcmp(name, "android23")) {
  *sys = 304; *dat = 504; *cac = 200;
 } else if (!strcmp(name, "android40")) {
  *sys = 352; *dat = 754; *cac = 200;
 } else if (!strcmp(name, "android41")) {
  *sys = 448; *dat = 754; *cac = 200;
 } else if (!strcmp(name, "android42")) {
  *sys = 448; *dat = 754; *cac = 200;
 }
}

variable_types:
sys
int *

variable_types:
dat
int *

variable_types:
cac
int *


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

result = new_str_list(8LL);
loglist = result;
return result;
}

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

result = new_str_list(8LL);
loglist = result;
return result;
}

original_code:
void init_log(void) {
 loglist = new_str_list(8);
}

canonical_original_code:
void init_log() {
 loglist = new_str_list(8);
}


decompiled_code:
int display_logbar()
{
unsigned int v0; // ebp
int i; // ebx
int v2; // eax
int v3; // eax
__int64 v4; // r12
char *endptr; // [rsp+8h] [rbp-20h] BYREF

if ( !(unsigned int)is_fb_available() )
return fprintf(
stderr,
"%s\n",
(const char *)(*(_QWORD *)(*(_QWORD *)loglist + 8LL * *(int *)(loglist + 12) - 8) + 1LL));
v0 = 512;
for ( i = 0; i != 16; ++i )
{
v2 = i + *(_DWORD *)(loglist + 12) - 16;
if ( v2 >= 0 )
{
v3 = strtol(*(const char **)(*(_QWORD *)loglist + 8LL * v2), &endptr, 10);
if ( endptr )
{
if ( v3 > 3 )
v3 = 3;
if ( v3 < 0 )
v3 = 0;
v4 = (unsigned int)logpalette[v3];
fill_rect(0LL, v0, 1024LL, 16LL, v4);
text(endptr, 0LL, v0, 1LL, 1LL, 0xFFFFFFFFLL, v4);
}
}
v0 += 16;
}
return update_screen();
}

canonical_decompiled_code:
int func1()
{
unsigned int v0; // ebp
int i; // ebx
int v2; // eax
int v3; // eax
__int64 v4; // r12
char *endptr; // [rsp+8h] [rbp-20h] BYREF

if ( !(unsigned int)is_fb_available() )
return fprintf(
stderr,
"%s\n",
(const char *)(*(_QWORD *)(*(_QWORD *)loglist + 8LL * *(int *)(loglist + 12) - 8) + 1LL));
v0 = 512;
for ( i = 0; i != 16; ++i )
{
v2 = i + *(_DWORD *)(loglist + 12) - 16;
if ( v2 >= 0 )
{
v3 = strtol(*(const char **)(*(_QWORD *)loglist + 8LL * v2), &endptr, 10);
if ( endptr )
{
if ( v3 > 3 )
v3 = 3;
if ( v3 < 0 )
v3 = 0;
v4 = (unsigned int)logpalette[v3];
fill_rect(0LL, v0, 1024LL, 16LL, v4);
text(endptr, 0LL, v0, 1LL, 1LL, 0xFFFFFFFFLL, v4);
}
}
v0 += 16;
}
return update_screen();
}

original_code:
void display_logbar(void) {
 char *str;
 int loglevel = 0, logindex = 0;
 if (is_fb_available()) {
  for (int i = 0; i < 16; ++i) {
   logindex = loglist->used - 16 + i;
   if (logindex < 0) continue;
   loglevel = (int)strtol(loglist->data[logindex], &str, 10);
   if (str == ((void *)0)) continue;
   if (loglevel < 0) loglevel = 0;
   else if (loglevel > 3) loglevel = 3;
   fill_rect(0, 768 - 256 + i*16, 1024, 16, logpalette[loglevel]);
   text(str, 0, 768 - 256 + i*16, 1, 1, 0xFFFFFFFF, logpalette[loglevel]);
  }
  update_screen();
 } else
  fprintf(stderr, "%s\n", loglist->data[loglist->used-1]+1);
}

canonical_original_code:
void display_logbar() {
 char *str;
 int loglevel = 0; int logindex = 0;
 if (is_fb_available()) {
  for (int i = 0; i < 16; ++i) {
   logindex = loglist->used - 16 + i;
   if (logindex < 0) continue;
   loglevel = (int)strtol(loglist->data[logindex], &str, 10);
   if (str == ((void *)0)) continue;
   if (loglevel < 0) loglevel = 0;
   else if (loglevel > 3) loglevel = 3;
   fill_rect(0, 768 - 256 + i*16, 1024, 16, logpalette[loglevel]);
   text(str, 0, 768 - 256 + i*16, 1, 1, 0xFFFFFFFF, logpalette[loglevel]);
  }
  update_screen();
 } else
  fprintf(stderr, "%s\n", loglist->data[loglist->used-1]+1);
}

variable_types:
str
char *

variable_types: loglevel int

variable_types: logindex int

variable_types: i int


logprintf

decompiled_code:
int logprintf(char *format, ...)
{
char v2[136]; // [rsp+0h] [rbp-158h] BYREF
gcc_va_list arg; // [rsp+88h] [rbp-D0h] BYREF

va_start(arg, format);
vsnprintf(v2, 0x80uLL, format, arg);
append_to_str_list(loglist, v2);
return display_logbar();
}

canonical_decompiled_code:
int func2(char *format, ...)
{
char v2[136]; // [rsp+0h] [rbp-158h] BYREF
gcc_va_list arg; // [rsp+88h] [rbp-D0h] BYREF

va_start(arg, format);
vsnprintf(v2, 0x80uLL, format, arg);
append_to_str_list(loglist, v2);
return func1();
}

original_code:
void logprintf(const char *fmt, ...) {
 va_list args;
 char buf[128];
 __builtin_va_start(args,fmt);
 vsnprintf(buf, sizeof(buf), fmt, args);
 __builtin_va_end(args);
 append_to_str_list(loglist, buf);
 display_logbar();
}

canonical_original_code:
void logprintf(char *fmt, ...) {
 __builtin_va_list args;
 char buf[128];
 __builtin_va_start(args,fmt);
 vsnprintf(buf, sizeof(buf), fmt, args);
 __builtin_va_end(args);
 append_to_str_list(loglist, buf);
 display_logbar();
}

variable_types:
fmt
char *

variable_types:
buf
char [128]


decompiled_code:
__int64 display_wholelog()
{
__int64 result; // rax
__int64 j; // rbx
int v2; // er15
int v3; // er12
unsigned int i; // ebx
__int64 v5; // rbp
const char *v6; // rdi
int v7; // eax
__int64 v8; // r13
char *endptr; // [rsp+8h] [rbp-40h] BYREF

if ( (unsigned int)is_fb_available() )
{
sbstr = 0x7765697620676F6CLL;
strcpy((char *)&sbstr + 8, "er");
v2 = 0;
do
{
clear_screen();
v3 = v2;
v5 = 8LL * v2;
for ( i = 48; i != 768; i += 16 )
{
if ( *(_DWORD *)(loglist + 12) <= v3 )
break;
v6 = *(const char **)(*(_QWORD *)loglist + v5);
if ( v6 )
{
v7 = strtol(v6, &endptr, 10);
if ( endptr )
{
if ( v7 > 3 )
v7 = 3;
if ( v7 < 0 )
v7 = 0;
v8 = (unsigned int)logpalette[v7];
fill_rect(0LL, i, 1024LL, 16LL, v8);
text(endptr, 0LL, i, 1LL, 1LL, 0xFFFFFFFFLL, v8);
}
}
++v3;
v5 += 8LL;
}
update_screen();
input_read();
if ( diff_input[3] )
{
if ( v2 <= 15 )
vibrate(40LL);
else
v2 -= 16;
}
if ( diff_input[4] )
{
if ( *(_DWORD *)(loglist + 12) - 16 <= v2 )
vibrate(40LL);
else
v2 += 16;
}
if ( diff_input[2] )
{
result = (unsigned int)(*(_DWORD *)(loglist + 12) - 45);
if ( (int)result <= v2 )
return result;
v2 += 45;
}
result = (__int64)diff_input;
}
while ( !diff_input[5] );
}
else
{
result = loglist;
for ( j = 0LL; *(_DWORD *)(loglist + 12) > (int)j; ++j )
{
fprintf(stderr, "%s\n", (const char *)(*(_QWORD *)(*(_QWORD *)result + 8 * j) + 1LL));
result = loglist;
}
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
__int64 j; // rbx
int v2; // er15
int v3; // er12
unsigned int i; // ebx
__int64 v5; // rbp
const char *v6; // rdi
int v7; // eax
__int64 v8; // r13
char *endptr; // [rsp+8h] [rbp-40h] BYREF

if ( (unsigned int)is_fb_available() )
{
sbstr = 0x7765697620676F6CLL;
strcpy((char *)&sbstr + 8, "er");
v2 = 0;
do
{
clear_screen();
v3 = v2;
v5 = 8LL * v2;
for ( i = 48; i != 768; i += 16 )
{
if ( *(_DWORD *)(loglist + 12) <= v3 )
break;
v6 = *(const char **)(*(_QWORD *)loglist + v5);
if ( v6 )
{
v7 = strtol(v6, &endptr, 10);
if ( endptr )
{
if ( v7 > 3 )
v7 = 3;
if ( v7 < 0 )
v7 = 0;
v8 = (unsigned int)logpalette[v7];
fill_rect(0LL, i, 1024LL, 16LL, v8);
text(endptr, 0LL, i, 1LL, 1LL, 0xFFFFFFFFLL, v8);
}
}
++v3;
v5 += 8LL;
}
update_screen();
input_read();
if ( diff_input[3] )
{
if ( v2 <= 15 )
vibrate(40LL);
else
v2 -= 16;
}
if ( diff_input[4] )
{
if ( *(_DWORD *)(loglist + 12) - 16 <= v2 )
vibrate(40LL);
else
v2 += 16;
}
if ( diff_input[2] )
{
result = (unsigned int)(*(_DWORD *)(loglist + 12) - 45);
if ( (int)result <= v2 )
return result;
v2 += 45;
}
result = (__int64)diff_input;
}
while ( !diff_input[5] );
}
else
{
result = loglist;
for ( j = 0LL; *(_DWORD *)(loglist + 12) > (int)j; ++j )
{
fprintf(stderr, "%s\n", (const char *)(*(_QWORD *)(*(_QWORD *)result + 8 * j) + 1LL));
result = loglist;
}
}
return result;
}

original_code:
void display_wholelog(void) {
 char *str;
 int loglevel = 0, logindex = 0;
 if (is_fb_available()) {
  int start = 0;
  int done = 0;
  const int lines_per_page = 768 / 16 - 3;
  const int button_scroll = 16;
  strcpy(sbstr, "log viewer");
  while (!done) {
   clear_screen();
   for (int i = 0; i < lines_per_page; ++i) {
    logindex = start + i;
    if (logindex >= loglist->used) break;
    if (loglist->data[logindex] == ((void *)0)) continue;
    loglevel = (int)strtol(loglist->data[logindex], &str, 10);
    if (str == ((void *)0)) continue;
    if (loglevel < 0) loglevel = 0;
    else if (loglevel > 3) loglevel = 3;
    fill_rect(0, i*16+48, 1024, 16, logpalette[loglevel]);
    text(str, 0, i*16+48, 1, 1, 0xFFFFFFFF, logpalette[loglevel]);
   }
   update_screen();
   input_read();
   if (diff_input.vol_up) {
    if (start >= button_scroll) {
     start -= button_scroll;
    } else {
     vibrate(40);
    }
   }
   if (diff_input.vol_down) {
    if (start < loglist->used - button_scroll) {
     start += button_scroll;
    } else {
     vibrate(40);
    }
   }
   if (diff_input.touch) {
    if (start < loglist->used - lines_per_page) {
     start += lines_per_page;
    } else {
     done = 1;
    }
   }
   if (diff_input.center)
    done = 1;
  }
 } else {
  for (int i = 0; i < loglist->used; ++i)
   fprintf(stderr, "%s\n", loglist->data[i]+1);
 }
}

canonical_original_code:
void display_wholelog() {
 char *str;
 int loglevel = 0; int logindex = 0;
 if (is_fb_available()) {
  int start = 0;
  int done = 0;
  int lines_per_page = 768 / 16 - 3;
  int button_scroll = 16;
  strcpy(sbstr, "log viewer");
  while (!done) {
   clear_screen();
   for (int i = 0; i < lines_per_page; ++i) {
    logindex = start + i;
    if (logindex >= loglist->used) break;
    if (loglist->data[logindex] == ((void *)0)) continue;
    loglevel = (int)strtol(loglist->data[logindex], &str, 10);
    if (str == ((void *)0)) continue;
    if (loglevel < 0) loglevel = 0;
    else if (loglevel > 3) loglevel = 3;
    fill_rect(0, i*16+48, 1024, 16, logpalette[loglevel]);
    text(str, 0, i*16+48, 1, 1, 0xFFFFFFFF, logpalette[loglevel]);
   }
   update_screen();
   input_read();
   if (diff_input.vol_up) {
    if (start >= button_scroll) {
     start -= button_scroll;
    } else {
     vibrate(40);
    }
   }
   if (diff_input.vol_down) {
    if (start < loglist->used - button_scroll) {
     start += button_scroll;
    } else {
     vibrate(40);
    }
   }
   if (diff_input.touch) {
    if (start < loglist->used - lines_per_page) {
     start += lines_per_page;
    } else {
     done = 1;
    }
   }
   if (diff_input.center)
    done = 1;
  }
 } else {
  for (int i = 0; i < loglist->used; ++i)
   fprintf(stderr, "%s\n", loglist->data[i]+1);
 }
}

variable_types: start int

variable_types: done int


decompiled_code:
__int64 __fastcall install_native(const char *a1, const char *a2, int a3)
{
const char *v3; // rbx
int *v6; // rax
char *v7; // rax
char v8[4120]; // [rsp+0h] [rbp-1018h] BYREF

v3 = a2;
logprintf("0installing new native Linux distro to %s", a2);
if ( !a2 )
v3 = (const char *)deduce_lv(a1);
if ( (unsigned int)lv_exists(v3) )
{
umount_lv(v3);
if ( a3 == -1 )
goto LABEL_9;
}
else
{
if ( a3 <= 0 )
return logprintf("2volume does not exist");
new_lv(v3, a3);
umount_lv(v3);
}
wipe_lv(v3);
LABEL_9:
mount_lv(v3);
snprintf(v8, 0x1000uLL, "/mnt/%s", v3);
if ( chdir(v8) == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
logprintf("2%s: %s", "can't chdir into target root", v7);
}
logprintf("0extracting .tar.gz file, this may take 10+ minutes");
return sysprintf("tar -xzpf %s", a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, const char *a2, int a3)
{
const char *v3; // rbx
int *v6; // rax
char *v7; // rax
char v8[4120]; // [rsp+0h] [rbp-1018h] BYREF

v3 = a2;
logprintf("0installing new native Linux distro to %s", a2);
if ( !a2 )
v3 = (const char *)deduce_lv(a1);
if ( (unsigned int)lv_exists(v3) )
{
umount_lv(v3);
if ( a3 == -1 )
goto LABEL_9;
}
else
{
if ( a3 <= 0 )
return logprintf("2volume does not exist");
new_lv(v3, a3);
umount_lv(v3);
}
wipe_lv(v3);
LABEL_9:
mount_lv(v3);
snprintf(v8, 0x1000uLL, "/mnt/%s", v3);
if ( chdir(v8) == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
logprintf("2%s: %s", "can't chdir into target root", v7);
}
logprintf("0extracting .tar.gz file, this may take 10+ minutes");
return sysprintf("tar -xzpf %s", a1);
}

original_code:
void install_native(const char *tarname, const char *lv, int size) {
 char cmd[4096];
 int code;
 logprintf("0installing new native Linux distro to %s", lv);
 if (lv == ((void *)0)) lv = deduce_lv(tarname);
 if (!lv_exists(lv)) {
  if (size > 0) new_lv(lv, size);
  else {
   logprintf("2volume does not exist");
   return;
  }
 }
 umount_lv(lv);
 if (size != -1) wipe_lv(lv);
 mount_lv(lv);
 snprintf(cmd, sizeof(cmd), "/mnt/%s", lv);
 if (chdir(cmd) == -1)
  logprintf("2%s: %s", "can't chdir into target root", strerror((*__errno_location ())));
 logprintf("0extracting .tar.gz file, this may take 10+ minutes");
 sysprintf("tar -xzpf %s", tarname);
}

canonical_original_code:
void install_native(char *tarname, char *lv, int size) {
 char cmd[4096];
 int code;
 logprintf("0installing new native Linux distro to %s", lv);
 if (lv == ((void *)0)) lv = deduce_lv(tarname);
 if (!lv_exists(lv)) {
  if (size > 0) new_lv(lv, size);
  else {
   logprintf("2volume does not exist");
   return;
  }
 }
 umount_lv(lv);
 if (size != -1) wipe_lv(lv);
 mount_lv(lv);
 snprintf(cmd, sizeof(cmd), "/mnt/%s", lv);
 if (chdir(cmd) == -1)
  logprintf("2%s: %s", "can't chdir into target root", strerror((*__errno_location ())));
 logprintf("0extracting .tar.gz file, this may take 10+ minutes");
 sysprintf("tar -xzpf %s", tarname);
}

variable_types:
tarname
char *

variable_types:
lv
char *

variable_types: size int

variable_types:
cmd
char [4096]

variable_types: code int


decompiled_code:
int __fastcall install_android(const char *a1, const char *a2, char a3)
{
const char *v3; // rbx
int result; // eax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int *v14; // rax
char *v15; // rax
unsigned int v16; // [rsp+4h] [rbp-2024h] BYREF
unsigned int v17; // [rsp+8h] [rbp-2020h] BYREF
unsigned int v18; // [rsp+Ch] [rbp-201Ch] BYREF
char buf[4096]; // [rsp+10h] [rbp-2018h] BYREF
char s[4120]; // [rsp+1010h] [rbp-1018h] BYREF

v3 = a2;
logprintf("0installing to %s from file %s", a2, a1);
if ( !a2 )
v3 = (const char *)deduce_lv_set(a1);
umount_lv_set(v3);
snprintf(s, 0x1000uLL, "%s-data", v3);
if ( !(unsigned int)lv_exists(s) )
{
deduce_lv_set_size(v3, &v18, &v17, &v16);
new_lv_set(v3, v18, v17, v16);
a3 |= 7u;
}
if ( (a3 & 2) != 0 )
wipe_lv(s);
if ( (a3 & 1) != 0 )
{
snprintf(s, 0x1000uLL, "%s-system", v3);
wipe_lv(s);
}
if ( (a3 & 4) != 0 )
{
snprintf(s, 0x1000uLL, "%s-cache", v3);
wipe_lv(s);
}
mount_lv_set(v3);
if ( !getcwd(buf, 0x1000uLL) )
{
v6 = _errno_location();
v7 = strerror(*v6);
logprintf("2%s: %s", "getcwd", v7);
}
snprintf(s, 0x1000uLL, "/mnt/%s", v3);
if ( chdir(s) == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
logprintf("2%s: %s", "chdir into root of new install", v9);
}
logprintf("0extracting zip file");
sysprintf("unzip -qo %s", a1);
snprintf(s, 0x1000uLL, "/mnt/%s/system/etc/firmware/q6.mdt", v3);
if ( !(unsigned int)test_file(s) )
{
mount_lv("firmware");
logprintf("0copying firmware");
sysprintf("cp /mnt/firmware/IMAGE/* /mnt/%s/system/etc/firmware", v3);
}
logprintf("0converting updater script");
system_logged(update_str);
logprintf("0running updater script");
if ( !(unsigned int)is_lv_mounted("root") )
mount_lv("root");
chmod("updatescript", 0x1EDu);
system_logged("bash updatescript");
snprintf(s, 0x1000uLL, "/mnt/%s/boot.img", v3);
if ( (unsigned int)test_file(s) && (a3 & 8) != 0 )
{
snprintf(s, 0x1000uLL, "/mnt/%s/moboot.splash.CyanogenMod.tga", v3);
if ( (unsigned int)test_file(s) )
{
logprintf("0installing moboot splash");
sysprintf("cp moboot.splash.CyanogenMod.tga /mnt/boot/moboot.splash.%s.tga", v3);
}
logprintf("0patching uImage");
system_logged("uimage-extract boot.img");
system_logged("mv ramdisk.img ramdisk.gz");
system_logged("gunzip ramdisk.gz");
mkdir("extracted", 0x1EDu);
if ( chdir("extracted") == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
logprintf("2%s: %s", "error changing directory into extraction area", v11);
}
system_logged("cpio -i < ../ramdisk");
sysprintf(
"sed -i -e 's/cm-system/%s-system/' -e 's/cm-data/%s-data/' -e 's/cm-cache/%s-cache/' init.tenderloin.rc",
v3,
v3,
v3);
system_logged("find . | cpio -o --format=newc | lzma > ../ramdisk");
if ( chdir("..") == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
logprintf("2%s: %s", "changing into parent directory failed", v13);
}
system_logged("mkimage -A arm -O linux -T kernel -C none -a 0x40208000 -e 0x40208000 -n kernel -d kernel.img uKernel");
system_logged(
"mkimage -A arm -O linux -T ramdisk -C none -a 0x60000000 -e 0x60000000 -n nsboot_patched_ramdisk -d ramdisk uRamdisk");
system_logged(
"mkimage -A arm -O linux -T multi -C none -a 0x40208000 -e 0x40208000 -n nsboot_patched_multi -d uKernel:uRamdisk uImage");
sysprintf("mv uImage /mnt/boot/uImage.%s", v3);
unlink("kernel.img");
unlink("ramdisk");
unlink("uImage");
}
result = chdir(buf);
if ( result == -1 )
{
v14 = _errno_location();
v15 = strerror(*v14);
result = logprintf("2%s: %s", "restoration of working directory failed", v15);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, const char *a2, char a3)
{
const char *v3; // rbx
int result; // eax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int *v14; // rax
char *v15; // rax
unsigned int v16; // [rsp+4h] [rbp-2024h] BYREF
unsigned int v17; // [rsp+8h] [rbp-2020h] BYREF
unsigned int v18; // [rsp+Ch] [rbp-201Ch] BYREF
char buf[4096]; // [rsp+10h] [rbp-2018h] BYREF
char s[4120]; // [rsp+1010h] [rbp-1018h] BYREF

v3 = a2;
logprintf("0installing to %s from file %s", a2, a1);
if ( !a2 )
v3 = (const char *)deduce_lv_set(a1);
umount_lv_set(v3);
snprintf(s, 0x1000uLL, "%s-data", v3);
if ( !(unsigned int)lv_exists(s) )
{
deduce_lv_set_size(v3, &v18, &v17, &v16);
new_lv_set(v3, v18, v17, v16);
a3 |= 7u;
}
if ( (a3 & 2) != 0 )
wipe_lv(s);
if ( (a3 & 1) != 0 )
{
snprintf(s, 0x1000uLL, "%s-system", v3);
wipe_lv(s);
}
if ( (a3 & 4) != 0 )
{
snprintf(s, 0x1000uLL, "%s-cache", v3);
wipe_lv(s);
}
mount_lv_set(v3);
if ( !getcwd(buf, 0x1000uLL) )
{
v6 = _errno_location();
v7 = strerror(*v6);
logprintf("2%s: %s", "getcwd", v7);
}
snprintf(s, 0x1000uLL, "/mnt/%s", v3);
if ( chdir(s) == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
logprintf("2%s: %s", "chdir into root of new install", v9);
}
logprintf("0extracting zip file");
sysprintf("unzip -qo %s", a1);
snprintf(s, 0x1000uLL, "/mnt/%s/system/etc/firmware/q6.mdt", v3);
if ( !(unsigned int)test_file(s) )
{
mount_lv("firmware");
logprintf("0copying firmware");
sysprintf("cp /mnt/firmware/IMAGE/* /mnt/%s/system/etc/firmware", v3);
}
logprintf("0converting updater script");
system_logged(update_str);
logprintf("0running updater script");
if ( !(unsigned int)is_lv_mounted("root") )
mount_lv("root");
chmod("updatescript", 0x1EDu);
system_logged("bash updatescript");
snprintf(s, 0x1000uLL, "/mnt/%s/boot.img", v3);
if ( (unsigned int)test_file(s) && (a3 & 8) != 0 )
{
snprintf(s, 0x1000uLL, "/mnt/%s/moboot.splash.CyanogenMod.tga", v3);
if ( (unsigned int)test_file(s) )
{
logprintf("0installing moboot splash");
sysprintf("cp moboot.splash.CyanogenMod.tga /mnt/boot/moboot.splash.%s.tga", v3);
}
logprintf("0patching uImage");
system_logged("uimage-extract boot.img");
system_logged("mv ramdisk.img ramdisk.gz");
system_logged("gunzip ramdisk.gz");
mkdir("extracted", 0x1EDu);
if ( chdir("extracted") == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
logprintf("2%s: %s", "error changing directory into extraction area", v11);
}
system_logged("cpio -i < ../ramdisk");
sysprintf(
"sed -i -e 's/cm-system/%s-system/' -e 's/cm-data/%s-data/' -e 's/cm-cache/%s-cache/' init.tenderloin.rc",
v3,
v3,
v3);
system_logged("find . | cpio -o --format=newc | lzma > ../ramdisk");
if ( chdir("..") == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
logprintf("2%s: %s", "changing into parent directory failed", v13);
}
system_logged("mkimage -A arm -O linux -T kernel -C none -a 0x40208000 -e 0x40208000 -n kernel -d kernel.img uKernel");
system_logged(
"mkimage -A arm -O linux -T ramdisk -C none -a 0x60000000 -e 0x60000000 -n nsboot_patched_ramdisk -d ramdisk uRamdisk");
system_logged(
"mkimage -A arm -O linux -T multi -C none -a 0x40208000 -e 0x40208000 -n nsboot_patched_multi -d uKernel:uRamdisk uImage");
sysprintf("mv uImage /mnt/boot/uImage.%s", v3);
unlink("kernel.img");
unlink("ramdisk");
unlink("uImage");
}
result = chdir(buf);
if ( result == -1 )
{
v14 = _errno_location();
v15 = strerror(*v14);
result = logprintf("2%s: %s", "restoration of working directory failed", v15);
}
return result;
}

original_code:
void install_android(const char *zipname, const char *base, int flags) {
 char cmd[4096], pwd[4096];
 int sys, dat, cac;
 int code;
 logprintf("0installing to %s from file %s", base, zipname);
 if (base == ((void *)0)) base = deduce_lv_set(zipname);
 umount_lv_set(base);
 snprintf(cmd, sizeof(cmd), "%s-data", base);
 if (!lv_exists(cmd)) {
  deduce_lv_set_size(base, &sys, &dat, &cac);
  new_lv_set(base, sys, dat, cac);
  flags |= 0x01 | 0x02 | 0x04;
 }
 if (flags & 0x02)
  wipe_lv(cmd);
 if (flags & 0x01) {
  snprintf(cmd, sizeof(cmd), "%s-system", base);
  wipe_lv(cmd);
 }
 if (flags & 0x04) {
  snprintf(cmd, sizeof(cmd), "%s-cache", base);
  wipe_lv(cmd);
 }
 mount_lv_set(base);
 if (getcwd(pwd, 4096) == ((void *)0))
  logprintf("2%s: %s", "getcwd", strerror((*__errno_location ())));
 snprintf(cmd, sizeof(cmd), "/mnt/%s", base);
 if (chdir(cmd) == -1)
  logprintf("2%s: %s", "chdir into root of new install", strerror((*__errno_location ())));
 logprintf("0extracting zip file");
 sysprintf("unzip -qo %s", zipname);
 snprintf(cmd, sizeof(cmd), "/mnt/%s/system/etc/firmware/q6.mdt", base);
 if (!test_file(cmd)) {
  mount_lv("firmware");
  logprintf("0copying firmware");
  sysprintf("cp /mnt/firmware/IMAGE/* /mnt/%s/system/etc/firmware",
   base);
 }
 logprintf("0converting updater script");
 system_logged(update_str);
 logprintf("0running updater script");
 if (!is_lv_mounted("root")) mount_lv("root");
 chmod("updatescript", 0755);
 system_logged("bash updatescript");
 snprintf(cmd, sizeof(cmd), "/mnt/%s/boot.img", base);
 if (test_file(cmd) && (flags & 0x08)) {
  snprintf(cmd, sizeof(cmd),
   "/mnt/%s/moboot.splash.CyanogenMod.tga", base);
  if (test_file(cmd)) {
   logprintf("0installing moboot splash");
   sysprintf("cp moboot.splash.CyanogenMod.tga "
    "/mnt/boot/moboot.splash.%s.tga",
    base);
  }
  logprintf("0patching uImage");
  system_logged("uimage-extract boot.img");
  system_logged("mv ramdisk.img ramdisk.gz");
  system_logged("gunzip ramdisk.gz");
  mkdir("extracted", 0755);
  if (chdir("extracted") == -1)
   logprintf("2%s: %s", "error changing directory into extraction " " area", strerror((*__errno_location ())));
  system_logged("cpio -i < ../ramdisk");
  sysprintf("sed -i -e 's/cm-system/%s-system/'"
   " -e 's/cm-data/%s-data/' -e 's/cm-cache/%s-cache/' "
   "init.tenderloin.rc", base, base, base);
  system_logged("find . | cpio -o --format=newc | lzma > ../ramdisk");
  if (chdir("..") == -1)
   logprintf("2%s: %s", "changing into parent directory failed", strerror((*__errno_location ())));
  system_logged("mkimage -A arm -O linux -T kernel -C none "
   "-a 0x40208000 -e 0x40208000 -n kernel -d kernel.img "
   "uKernel");
  system_logged("mkimage -A arm -O linux -T ramdisk -C none "
   "-a 0x60000000 -e 0x60000000 -n nsboot_patched_ramdisk "
   "-d ramdisk uRamdisk");
  system_logged("mkimage -A arm -O linux -T multi -C none "
   "-a 0x40208000 -e 0x40208000 -n nsboot_patched_multi "
   "-d uKernel:uRamdisk uImage");
  sysprintf("mv uImage /mnt/boot/uImage.%s", base);
  unlink("kernel.img");
  unlink("ramdisk");
  unlink("uImage");
 }
 if (chdir(pwd) == -1) logprintf("2%s: %s", "restoration of working directory failed", strerror((*__errno_location ())));
}

canonical_original_code:
void install_android(char *zipname, char *base, int flags) {
 char cmd[4096]; char pwd[4096];
 int sys; int dat; int cac;
 int code;
 logprintf("0installing to %s from file %s", base, zipname);
 if (base == ((void *)0)) base = deduce_lv_set(zipname);
 umount_lv_set(base);
 snprintf(cmd, sizeof(cmd), "%s-data", base);
 if (!lv_exists(cmd)) {
  deduce_lv_set_size(base, &sys, &dat, &cac);
  new_lv_set(base, sys, dat, cac);
  flags |= 0x01 | 0x02 | 0x04;
 }
 if (flags & 0x02)
  wipe_lv(cmd);
 if (flags & 0x01) {
  snprintf(cmd, sizeof(cmd), "%s-system", base);
  wipe_lv(cmd);
 }
 if (flags & 0x04) {
  snprintf(cmd, sizeof(cmd), "%s-cache", base);
  wipe_lv(cmd);
 }
 mount_lv_set(base);
 if (getcwd(pwd, 4096) == ((void *)0))
  logprintf("2%s: %s", "getcwd", strerror((*__errno_location ())));
 snprintf(cmd, sizeof(cmd), "/mnt/%s", base);
 if (chdir(cmd) == -1)
  logprintf("2%s: %s", "chdir into root of new install", strerror((*__errno_location ())));
 logprintf("0extracting zip file");
 sysprintf("unzip -qo %s", zipname);
 snprintf(cmd, sizeof(cmd), "/mnt/%s/system/etc/firmware/q6.mdt", base);
 if (!test_file(cmd)) {
  mount_lv("firmware");
  logprintf("0copying firmware");
  sysprintf("cp /mnt/firmware/IMAGE/* /mnt/%s/system/etc/firmware",
   base);
 }
 logprintf("0converting updater script");
 system_logged(update_str);
 logprintf("0running updater script");
 if (!is_lv_mounted("root")) mount_lv("root");
 chmod("updatescript", 0755);
 system_logged("bash updatescript");
 snprintf(cmd, sizeof(cmd), "/mnt/%s/boot.img", base);
 if (test_file(cmd) && (flags & 0x08)) {
  snprintf(cmd, sizeof(cmd),
   "/mnt/%s/moboot.splash.CyanogenMod.tga", base);
  if (test_file(cmd)) {
   logprintf("0installing moboot splash");
   sysprintf("cp moboot.splash.CyanogenMod.tga "
    "/mnt/boot/moboot.splash.%s.tga",
    base);
  }
  logprintf("0patching uImage");
  system_logged("uimage-extract boot.img");
  system_logged("mv ramdisk.img ramdisk.gz");
  system_logged("gunzip ramdisk.gz");
  mkdir("extracted", 0755);
  if (chdir("extracted") == -1)
   logprintf("2%s: %s", "error changing directory into extraction " " area", strerror((*__errno_location ())));
  system_logged("cpio -i < ../ramdisk");
  sysprintf("sed -i -e 's/cm-system/%s-system/'"
   " -e 's/cm-data/%s-data/' -e 's/cm-cache/%s-cache/' "
   "init.tenderloin.rc", base, base, base);
  system_logged("find . | cpio -o --format=newc | lzma > ../ramdisk");
  if (chdir("..") == -1)
   logprintf("2%s: %s", "changing into parent directory failed", strerror((*__errno_location ())));
  system_logged("mkimage -A arm -O linux -T kernel -C none "
   "-a 0x40208000 -e 0x40208000 -n kernel -d kernel.img "
   "uKernel");
  system_logged("mkimage -A arm -O linux -T ramdisk -C none "
   "-a 0x60000000 -e 0x60000000 -n nsboot_patched_ramdisk "
   "-d ramdisk uRamdisk");
  system_logged("mkimage -A arm -O linux -T multi -C none "
   "-a 0x40208000 -e 0x40208000 -n nsboot_patched_multi "
   "-d uKernel:uRamdisk uImage");
  sysprintf("mv uImage /mnt/boot/uImage.%s", base);
  unlink("kernel.img");
  unlink("ramdisk");
  unlink("uImage");
 }
 if (chdir(pwd) == -1) logprintf("2%s: %s", "restoration of working directory failed", strerror((*__errno_location ())));
}

variable_types:
zipname
char *

variable_types:
base
char *

variable_types: flags int

variable_types:
pwd
char [4096]

variable_types: sys int

variable_types: dat int

variable_types: cac int


decompiled_code:
__int64 __fastcall delete_lv_set(const char *a1)
{
char v2[40]; // [rsp+0h] [rbp-28h] BYREF

logprintf("0deleting volume set %s", a1);
snprintf(v2, 0x20uLL, "%s-system", a1);
delete_lv(v2);
snprintf(v2, 0x20uLL, "%s-data", a1);
delete_lv(v2);
snprintf(v2, 0x20uLL, "%s-cache", a1);
return delete_lv(v2);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
char v2[40]; // [rsp+0h] [rbp-28h] BYREF

logprintf("0deleting volume set %s", a1);
snprintf(v2, 0x20uLL, "%s-system", a1);
delete_lv(v2);
snprintf(v2, 0x20uLL, "%s-data", a1);
delete_lv(v2);
snprintf(v2, 0x20uLL, "%s-cache", a1);
return delete_lv(v2);
}

original_code:
void delete_lv_set(const char *set) {
 char lv[32];
 logprintf("0deleting volume set %s", set);
 snprintf(lv, sizeof(lv), "%s-system", set);
 delete_lv(lv);
 snprintf(lv, sizeof(lv), "%s-data", set);
 delete_lv(lv);
 snprintf(lv, sizeof(lv), "%s-cache", set);
 delete_lv(lv);
}

canonical_original_code:
void delete_lv_set(char *set) {
 char lv[32];
 logprintf("0deleting volume set %s", set);
 snprintf(lv, sizeof(lv), "%s-system", set);
 delete_lv(lv);
 snprintf(lv, sizeof(lv), "%s-data", set);
 delete_lv(lv);
 snprintf(lv, sizeof(lv), "%s-cache", set);
 delete_lv(lv);
}

variable_types:
set
char *

variable_types:
lv
char [32]


decompiled_code:
char *__fastcall install_uimage(char *s)
{
char *result; // rax
__int64 v2; // rbx
char v3[4120]; // [rsp+0h] [rbp-1018h] BYREF

logprintf("0installing uImage file %s", s);
result = strchr(s, 46);
if ( result )
{
v2 = (__int64)result;
sysprintf("cp %s /mnt/boot", s);
sprintf(v3, "/mnt/boot/moboot.verbose.%s", (const char *)(v2 + 1));
result = (char *)qfprintf(v3, "yes");
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(char *s)
{
char *result; // rax
__int64 v2; // rbx
char v3[4120]; // [rsp+0h] [rbp-1018h] BYREF

logprintf("0installing uImage file %s", s);
result = strchr(s, 46);
if ( result )
{
v2 = (__int64)result;
sysprintf("cp %s /mnt/boot", s);
sprintf(v3, "/mnt/boot/moboot.verbose.%s", (const char *)(v2 + 1));
result = (char *)qfprintf(v3, "yes");
}
return result;
}

original_code:
void install_uimage(char *file) {
 char cmd[4096];
 int code;
 int verbose_fd;
 char *dot;
 logprintf("0installing uImage file %s", file);
 dot = strchr(file, '.');
 if (dot == ((void *)0)) return;
 ++dot;
 sysprintf("cp %s /mnt/boot", file);
 sprintf(cmd, "/mnt/boot/moboot.verbose.%s", dot);
 qfprintf(cmd, "yes");
 return;
}

variable_types:
file
char *

variable_types:
dot
char *


decompiled_code:
unsigned int replace_moboot()
{
unsigned int result; // eax
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax

logprintf("0replacing noboot with nsboot");
if ( !(unsigned int)test_file("/mnt/boot/uImage.nsboot") )
return logprintf("2nsboot is not installed in boot");
if ( chdir("/mnt/boot") == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
result = logprintf("2%s: %s", "error changing directory to boot", v2);
}
else
{
system_logged("mv uImage uImage.old");
if ( !(unsigned int)test_file("uImage") && (unsigned int)test_file("uImage.old") )
{
result = symlink("uImage.nsboot", "uImage");
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
logprintf("2%s: %s", "error symlinking nsboot as main bootloader!", v4);
logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
result = sleep(1u);
}
}
else
{
logprintf("2relocation of old bootloader failed!");
logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
result = sleep(1u);
}
}
return result;
}

canonical_decompiled_code:
unsigned int func5()
{
unsigned int result; // eax
int *v1; // rax
char *v2; // rax
int *v3; // rax
char *v4; // rax

logprintf("0replacing noboot with nsboot");
if ( !(unsigned int)test_file("/mnt/boot/uImage.nsboot") )
return logprintf("2nsboot is not installed in boot");
if ( chdir("/mnt/boot") == -1 )
{
v1 = _errno_location();
v2 = strerror(*v1);
result = logprintf("2%s: %s", "error changing directory to boot", v2);
}
else
{
system_logged("mv uImage uImage.old");
if ( !(unsigned int)test_file("uImage") && (unsigned int)test_file("uImage.old") )
{
result = symlink("uImage.nsboot", "uImage");
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
logprintf("2%s: %s", "error symlinking nsboot as main bootloader!", v4);
logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
result = sleep(1u);
}
}
else
{
logprintf("2relocation of old bootloader failed!");
logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
result = sleep(1u);
}
}
return result;
}

original_code:
void replace_moboot(void) {
 int code;
 logprintf("0replacing noboot with nsboot");
 if (!test_file("/mnt/boot/uImage.nsboot")) {
  logprintf("2nsboot is not installed in boot");
  return;
 }
 if (chdir("/mnt/boot") == -1) {
  logprintf("2%s: %s", "error changing directory to boot", strerror((*__errno_location ())));
  return;
 }
 system_logged("mv uImage uImage.old");
 if (test_file("uImage") || !test_file("uImage.old")) {
  logprintf("2relocation of old bootloader failed!");
  logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
  sleep(1);
  return;
 }
 if (symlink("uImage.nsboot", "uImage") == -1) {
  logprintf("2%s: %s", "error symlinking nsboot as main bootloader!", strerror((*__errno_location ())));
  logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
  sleep(1);
 }
}

canonical_original_code:
void replace_moboot() {
 int code;
 logprintf("0replacing noboot with nsboot");
 if (!test_file("/mnt/boot/uImage.nsboot")) {
  logprintf("2nsboot is not installed in boot");
  return;
 }
 if (chdir("/mnt/boot") == -1) {
  logprintf("2%s: %s", "error changing directory to boot", strerror((*__errno_location ())));
  return;
 }
 system_logged("mv uImage uImage.old");
 if (test_file("uImage") || !test_file("uImage.old")) {
  logprintf("2relocation of old bootloader failed!");
  logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
  sleep(1);
  return;
 }
 if (symlink("uImage.nsboot", "uImage") == -1) {
  logprintf("2%s: %s", "error symlinking nsboot as main bootloader!", strerror((*__errno_location ())));
  logprintf("3%s", "WARNING: YOU MAY NEED TO USE NOVACOM TO REINSTALL A BOOTLOADER");
  sleep(1);
 }
}


decompiled_code:
void __fastcall free_str_list(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 3) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2);
if ( v3 )
free(v3);
++v2;
}
while ( *((_DWORD *)ptr + 3) > (int)v2 );
}
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 3) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2);
if ( v3 )
free(v3);
++v2;
}
while ( *((_DWORD *)ptr + 3) > (int)v2 );
}
free(*(void **)ptr);
free(ptr);
}

original_code:
void free_str_list(str_list *list) {
 for (int i = 0; i < list->used; ++i)
  if (list->data[i] != ((void *)0))
   free(list->data[i]);
 free(list->data);
 free(list);
}

canonical_original_code:
void free_str_list(struct str_list *list) {
 for (int i = 0; i < list->used; ++i)
  if (list->data[i] != ((void *)0))
   free(list->data[i]);
 free(list->data);
 free(list);
}

variable_types: struct str_list { char **data; int size; int used; } *

variable_types: i int


decompiled_code:
char *__fastcall append_to_str_list(__int64 a1, const char *a2)
{
int v2; // ebp
int v3; // ebp
void *v4; // rax
int v5; // edx
__int64 v6; // rax
char **v7; // rbp
char *result; // rax

v2 = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) >= v2 )
{
v3 = 2 * v2;
v4 = realloc(*(void **)a1, 8LL * v3);
if ( !v4 )
{
perror("can't realloc string data");
exit(1);
}
*(_QWORD *)a1 = v4;
v5 = *(_DWORD *)(a1 + 8);
if ( v3 > v5 )
{
v6 = 8LL * v5;
do
{
*(_QWORD *)(*(_QWORD *)a1 + v6) = 0LL;
v6 += 8LL;
}
while ( v6 != 8 * (v5 + (unsigned __int64)(unsigned int)(v3 - v5 - 1) + 1) );
}
*(_DWORD *)(a1 + 8) = v3;
}
v7 = (char **)(*(_QWORD *)a1 + 8LL * *(int *)(a1 + 12));
result = strdup(a2);
*v7 = result;
++*(_DWORD *)(a1 + 12);
return result;
}

canonical_decompiled_code:
char *__fastcall func1(__int64 a1, const char *a2)
{
int v2; // ebp
int v3; // ebp
void *v4; // rax
int v5; // edx
__int64 v6; // rax
char **v7; // rbp
char *result; // rax

v2 = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) >= v2 )
{
v3 = 2 * v2;
v4 = realloc(*(void **)a1, 8LL * v3);
if ( !v4 )
{
perror("can't realloc string data");
exit(1);
}
*(_QWORD *)a1 = v4;
v5 = *(_DWORD *)(a1 + 8);
if ( v3 > v5 )
{
v6 = 8LL * v5;
do
{
*(_QWORD *)(*(_QWORD *)a1 + v6) = 0LL;
v6 += 8LL;
}
while ( v6 != 8 * (v5 + (unsigned __int64)(unsigned int)(v3 - v5 - 1) + 1) );
}
*(_DWORD *)(a1 + 8) = v3;
}
v7 = (char **)(*(_QWORD *)a1 + 8LL * *(int *)(a1 + 12));
result = strdup(a2);
*v7 = result;
++*(_DWORD *)(a1 + 12);
return result;
}

original_code:
void append_to_str_list(str_list *list, char *str) {
 if (list->used >= list->size) {
  int newsize = list->size * 2;
  char **newdata = realloc(list->data, newsize * sizeof(char *));
  if (newdata == ((void *)0)) {
   perror("can't realloc string data");
   exit(1);
  }
  list->data = newdata;
  for (int i = list->size; i < newsize; ++i)
   list->data[i] = ((void *)0);
  list->size = newsize;
 }
 list->data[list->used] = strdup(str);
 ++list->used;
}

canonical_original_code:
void append_to_str_list(struct str_list *list, char *str) {
 if (list->used >= list->size) {
  int newsize = list->size * 2;
  char **newdata = realloc(list->data, newsize * sizeof(char *));
  if (newdata == ((void *)0)) {
   perror("can't realloc string data");
   exit(1);
  }
  list->data = newdata;
  for (int i = list->size; i < newsize; ++i)
   list->data[i] = ((void *)0);
  list->size = newsize;
 }
 list->data[list->used] = strdup(str);
 ++list->used;
}

variable_types:
str
char *

variable_types: newsize int

variable_types:
newdata
char **

user_defined_types: struct str_list { char **data; int size; int used; }


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
sbstr = 0x2E676E6964616F6CLL;
strcpy((char *)&sbstr + 8, "..");
do_init(argc, argv, envp);
main_menu();
exit(0);
}

original_code:
int main(int argc, char **argv) {
 strcpy(sbstr, "loading...");
 do_init();
 main_menu();
 exit(0);
}

variable_types: argc int

variable_types:
argv
char **


variable_types:
prompt
char *

variable_types: done int

variable_types:
buf
char [12]

return_type: long


typeinit

repo: willghatch/cpslc

decompiled_code:
_QWORD *typeinit()
{
_QWORD *result; // rax

int_type = (__int64)typecreate(4, 0, 0LL, 0LL, (__int64)"integer");
bool_type = (__int64)typecreate(4, 2, 0LL, 0LL, (__int64)"boolean");
char_type = (__int64)typecreate(4, 1, 0LL, 0LL, (__int64)"char");
str_type = (__int64)typecreate(4, 3, 0LL, 0LL, (__int64)"string");
result = typecreate(0, 6, 0LL, 0LL, (__int64)"undefinded");
undef_type = (__int64)result;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

int_type = (__int64)typecreate(4, 0, 0LL, 0LL, (__int64)"integer");
bool_type = (__int64)typecreate(4, 2, 0LL, 0LL, (__int64)"boolean");
char_type = (__int64)typecreate(4, 1, 0LL, 0LL, (__int64)"char");
str_type = (__int64)typecreate(4, 3, 0LL, 0LL, (__int64)"string");
result = typecreate(0, 6, 0LL, 0LL, (__int64)"undefinded");
undef_type = (__int64)result;
return result;
}

original_code:
void typeinit (void)
{ int_type = typecreate(4, Integer, ((void *)0), ((void *)0), "integer");
 bool_type = typecreate(4, Boolean, ((void *)0), ((void *)0), "boolean");
 char_type = typecreate(4, Char, ((void *)0), ((void *)0), "char");
 str_type = typecreate(4, String, ((void *)0), ((void *)0), "string");
 undef_type = typecreate(0, UndefinedType, ((void *)0), ((void *)0), "undefinded");
}

canonical_original_code:
void typeinit()
{ int_type = typecreate(4, Integer, ((void *)0), ((void *)0), "integer");
 bool_type = typecreate(4, Boolean, ((void *)0), ((void *)0), "boolean");
 char_type = typecreate(4, Char, ((void *)0), ((void *)0), "char");
 str_type = typecreate(4, String, ((void *)0), ((void *)0), "string");
 undef_type = typecreate(0, UndefinedType, ((void *)0), ((void *)0), "undefinded");
}


getTypeName

decompiled_code:
const char *__fastcall getTypeName(__int64 a1)
{
const char *result; // rax

if ( !a1 )
return "void";
result = *(const char **)(a1 + 8);
if ( !result )
result = "unnamed-type";
return result;
}

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

if ( !a1 )
return "void";
result = *(const char **)(a1 + 8);
if ( !result )
result = "unnamed-type";
return result;
}

original_code:
char* getTypeName(TYPE* type) {
    if (type == ((void *)0)) {
        return "void";
    }
    if (type->ty_name == ((void *)0)) {
        return "unnamed-type";
    }
    return type->ty_name;
}

canonical_original_code:
char *getTypeName(struct type_info *type) {
    if (type == ((void *)0)) {
        return "void";
    }
    if (type->ty_name == ((void *)0)) {
        return "unnamed-type";
    }
    return type->ty_name;
}

variable_types: struct type_info { int ty_size; char *ty_name; enum type_kind { Integer=0, Char=1, Boolean=2, String=3, Array=4, Record=5, UndefinedType=6 } ty_kind; struct type_info *ty_next; union { struct { struct type_info *RangeType; int min; int max; } ty_subrange; struct { struct type_info *ElementType; struct type_info *IndexType; int min; int max; } ty_array; struct { struct id_info { char *id_name; int id_addr; int id_label; int id_level; struct type_info *id_type; enum identifier_kind { Constant_id=0, Type=1, Variable=2, FParameter=3, Field=4, Procedure=5, Function=6 } id_kind; struct id_info *id_left; struct id_info *id_right; struct id_info *id_next; struct expression_struct { struct type_info *type; enum expr_kind_enum { constant_expr=0, operation_bin=1, operation_un=2, registerVal=3, globalVar=4, localVar=5, functionCall=6, typecast=7 } kind; int int_val; char char_val; int bool_val; char *str_val; int str_const_index; struct expression_struct *offsetExpr; int pointer_p; int to_pointer_p; union { struct { struct expression_struct *operand1; struct expression_struct *operand2; enum operator_enum { op_and=0, op_or=1, op_not=2, op_negate=3, op_add=4, op_sub=5, op_mult=6, op_div=7, op_mod=8, op_equal=9, op_nequal=10, op_gt=11, op_lt=12, op_gte=13, op_lte=14 } op; } opdata; int reg_number; struct id_info *id; struct stmt_struct { enum stmt_type_enum { assign_stmt=0, if_stmt=1, while_stmt=2, repeat_stmt=3, for_stmt=4, stop_stmt=5, return_stmt=6, read_stmt=7, write_stmt=8, proc_stmt=9, null_stmt=10 } type; union { struct htslist_struct { struct slist_struct { struct slist_struct *next; void *data; } *head; struct slist_struct *tail; } *if_conditionals; struct slist_struct *exprList; struct expression_struct *expression; struct { struct stmt_struct *init; struct conditional_struct { struct expression_struct *condition; enum BranchType_enum { bt_equal0=0, bt_nequal0=1 } branchType; struct htslist_struct *statements; } *cBlock; } loopdata; struct { struct expression_struct *lvalue; struct expression_struct *rvalue; } assigndata; struct { char *name; struct slist_struct *paramExprs; } procdata; } data; } *funcCall; struct expression_struct *innerExpr; } edata; } *const_expr; struct slist_struct *typedIdentLists; int param_size; int pointer_p; } *FirstField; } ty_record; } ty_form; } *

user_defined_types: struct type_info { int ty_size; char *ty_name; enum type_kind ty_kind; struct type_info *ty_next; union { struct { struct type_info *RangeType; int min; int max; } ty_subrange; struct { struct type_info *ElementType; struct type_info *IndexType; int min; int max; } ty_array; struct { struct id_info *FirstField; } ty_record; } ty_form; }

user_defined_types: struct id_info { char *id_name; int id_addr; int id_label; int id_level; struct type_info *id_type; enum identifier_kind id_kind; struct id_info *id_left; struct id_info *id_right; struct id_info *id_next; struct expression_struct *const_expr; struct slist_struct *typedIdentLists; int param_size; int pointer_p; }

user_defined_types: struct expression_struct { struct type_info *type; enum expr_kind_enum kind; int int_val; char char_val; int bool_val; char *str_val; int str_const_index; struct expression_struct *offsetExpr; int pointer_p; int to_pointer_p; union { struct { struct expression_struct *operand1; struct expression_struct *operand2; enum operator_enum op; } opdata; int reg_number; struct id_info *id; struct stmt_struct *funcCall; struct expression_struct *innerExpr; } edata; }

user_defined_types: struct stmt_struct { enum stmt_type_enum type; union { struct htslist_struct *if_conditionals; struct slist_struct *exprList; struct expression_struct *expression; struct { struct stmt_struct *init; struct conditional_struct *cBlock; } loopdata; struct { struct expression_struct *lvalue; struct expression_struct *rvalue; } assigndata; struct { char *name; struct slist_struct *paramExprs; } procdata; } data; }

user_defined_types: struct htslist_struct { struct slist_struct *head; struct slist_struct *tail; }

user_defined_types: struct conditional_struct { struct expression_struct *condition; enum BranchType_enum branchType; struct htslist_struct *statements; }


getIdKindName

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

switch ( a1 )
{
case 0:
result = "Constant";
break;
case 1:
result = "Type";
break;
case 2:
result = "Variable";
break;
case 5:
result = "Procedure";
break;
case 6:
result = "Function";
break;
default:
result = "Unknown Kind";
break;
}
return result;
}

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

switch ( a1 )
{
case 0:
result = "Constant";
break;
case 1:
result = "Type";
break;
case 2:
result = "Variable";
break;
case 5:
result = "Procedure";
break;
case 6:
result = "Function";
break;
default:
result = "Unknown Kind";
break;
}
return result;
}

original_code:
char* getIdKindName(ID_KIND kind) {
    switch(kind) {
        case Constant_id:
            return "Constant";
        case Type:
            return "Type";
        case Variable:
            return "Variable";
        case Procedure:
            return "Procedure";
        case Function:
            return "Function";
        default:
            return "Unknown Kind";
    }
}

canonical_original_code:
char *getIdKindName(enum identifier_kind kind) {
    switch(kind) {
        case Constant_id:
            return "Constant";
        case Type:
            return "Type";
        case Variable:
            return "Variable";
        case Procedure:
            return "Procedure";
        case Function:
            return "Function";
        default:
            return "Unknown Kind";
    }
}

variable_types: enum identifier_kind { Constant_id=0, Type=1, Variable=2, FParameter=3, Field=4, Procedure=5, Function=6 }


IDsearch

decompiled_code:
__int64 __fastcall IDsearch(const char *a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rdx

if ( !a2 )
return 0LL;
v2 = strcmp(a1, *(const char **)a2);
v3 = a2;
if ( v2 )
{
if ( v2 < 0 )
v3 = IDsearch(a1, *(_QWORD *)(a2 + 40), a2);
else
v3 = IDsearch(a1, *(_QWORD *)(a2 + 48), a2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, __int64 a2)
{
int v2; // eax
__int64 v3; // rdx

if ( !a2 )
return 0LL;
v2 = strcmp(a1, *(const char **)a2);
v3 = a2;
if ( v2 )
{
if ( v2 < 0 )
v3 = func3(a1, *(_QWORD *)(a2 + 40), a2);
else
v3 = func3(a1, *(_QWORD *)(a2 + 48), a2);
}
return v3;
}

original_code:
ID *IDsearch (char *name, ID *table)
{ int temp;
 if (table == ((void *)0))
  return(((void *)0));
 temp = strcmp(name, table->id_name);
 if (temp == 0)
  return(table);
 else if (temp < 0)
  return(IDsearch(name, table->id_left));
 else
  return(IDsearch(name, table->id_right));
}

canonical_original_code:
struct id_info *IDsearch(char *name, struct id_info *table)
{ int temp;
 if (table == ((void *)0))
  return(((void *)0));
 temp = strcmp(name, table->id_name);
 if (temp == 0)
  return(table);
 else if (temp < 0)
  return(IDsearch(name, table->id_left));
 else
  return(IDsearch(name, table->id_right));
}

variable_types:
name
char *

variable_types:
table
struct id_info *

variable_types: temp int

return_type: struct id_info { char *id_name; int id_addr; int id_label; int id_level; struct type_info { int ty_size; char *ty_name; enum type_kind { Integer=0, Char=1, Boolean=2, String=3, Array=4, Record=5, UndefinedType=6 } ty_kind; struct type_info *ty_next; union { struct { struct type_info *RangeType; int min; int max; } ty_subrange; struct { struct type_info *ElementType; struct type_info *IndexType; int min; int max; } ty_array; struct { struct id_info *FirstField; } ty_record; } ty_form; } *id_type; enum identifier_kind { Constant_id=0, Type=1, Variable=2, FParameter=3, Field=4, Procedure=5, Function=6 } id_kind; struct id_info *id_left; struct id_info *id_right; struct id_info *id_next; struct expression_struct { struct type_info *type; enum expr_kind_enum { constant_expr=0, operation_bin=1, operation_un=2, registerVal=3, globalVar=4, localVar=5, functionCall=6, typecast=7 } kind; int int_val; char char_val; int bool_val; char *str_val; int str_const_index; struct expression_struct *offsetExpr; int pointer_p; int to_pointer_p; union { struct { struct expression_struct *operand1; struct expression_struct *operand2; enum operator_enum { op_and=0, op_or=1, op_not=2, op_negate=3, op_add=4, op_sub=5, op_mult=6, op_div=7, op_mod=8, op_equal=9, op_nequal=10, op_gt=11, op_lt=12, op_gte=13, op_lte=14 } op; } opdata; int reg_number; struct id_info *id; struct stmt_struct { enum stmt_type_enum { assign_stmt=0, if_stmt=1, while_stmt=2, repeat_stmt=3, for_stmt=4, stop_stmt=5, return_stmt=6, read_stmt=7, write_stmt=8, proc_stmt=9, null_stmt=10 } type; union { struct htslist_struct { struct slist_struct { struct slist_struct *next; void *data; } *head; struct slist_struct *tail; } *if_conditionals; struct slist_struct *exprList; struct expression_struct *expression; struct { struct stmt_struct *init; struct conditional_struct { struct expression_struct *condition; enum BranchType_enum { bt_equal0=0, bt_nequal0=1 } branchType; struct htslist_struct *statements; } *cBlock; } loopdata; struct { struct expression_struct *lvalue; struct expression_struct *rvalue; } assigndata; struct { char *name; struct slist_struct *paramExprs; } procdata; } data; } *funcCall; struct expression_struct *innerExpr; } edata; } *const_expr; struct slist_struct *typedIdentLists; int param_size; int pointer_p; } *


scopeLookup

decompiled_code:
__int64 __fastcall scopeLookup(const char *a1)
{
__int64 v1; // rbx
__int64 result; // rax

if ( currscope < 0 )
return 0LL;
v1 = currscope;
do
{
result = IDsearch(a1, scope[v1]);
if ( result )
break;
--v1;
}
while ( (int)v1 >= 0 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
__int64 v1; // rbx
__int64 result; // rax

if ( currscope < 0 )
return 0LL;
v1 = currscope;
do
{
result = func3(a1, scope[v1]);
if ( result )
break;
--v1;
}
while ( (int)v1 >= 0 );
return result;
}

original_code:
ID* scopeLookup(char* name) {
    ID* id;
    for (int i = currscope; i >= 0; --i) {
        id = IDsearch(name, scope[i]);
        if(id) {
            return id;
        }
    }
    return ((void *)0);
}

canonical_original_code:
struct id_info *scopeLookup(char *name) {
    struct id_info *id;
    for (int i = currscope; i >= 0; --i) {
        id = IDsearch(name, scope[i]);
        if(id) {
            return id;
        }
    }
    return ((void *)0);
}

variable_types: struct id_info { char *id_name; int id_addr; int id_label; int id_level; struct type_info { int ty_size; char *ty_name; enum type_kind { Integer=0, Char=1, Boolean=2, String=3, Array=4, Record=5, UndefinedType=6 } ty_kind; struct type_info *ty_next; union { struct { struct type_info *RangeType; int min; int max; } ty_subrange; struct { struct type_info *ElementType; struct type_info *IndexType; int min; int max; } ty_array; struct { struct id_info *FirstField; } ty_record; } ty_form; } *id_type; enum identifier_kind { Constant_id=0, Type=1, Variable=2, FParameter=3, Field=4, Procedure=5, Function=6 } id_kind; struct id_info *id_left; struct id_info *id_right; struct id_info *id_next; struct expression_struct { struct type_info *type; enum expr_kind_enum { constant_expr=0, operation_bin=1, operation_un=2, registerVal=3, globalVar=4, localVar=5, functionCall=6, typecast=7 } kind; int int_val; char char_val; int bool_val; char *str_val; int str_const_index; struct expression_struct *offsetExpr; int pointer_p; int to_pointer_p; union { struct { struct expression_struct *operand1; struct expression_struct *operand2; enum operator_enum { op_and=0, op_or=1, op_not=2, op_negate=3, op_add=4, op_sub=5, op_mult=6, op_div=7, op_mod=8, op_equal=9, op_nequal=10, op_gt=11, op_lt=12, op_gte=13, op_lte=14 } op; } opdata; int reg_number; struct id_info *id; struct stmt_struct { enum stmt_type_enum { assign_stmt=0, if_stmt=1, while_stmt=2, repeat_stmt=3, for_stmt=4, stop_stmt=5, return_stmt=6, read_stmt=7, write_stmt=8, proc_stmt=9, null_stmt=10 } type; union { struct htslist_struct { struct slist_struct { struct slist_struct *next; void *data; } *head; struct slist_struct *tail; } *if_conditionals; struct slist_struct *exprList; struct expression_struct *expression; struct { struct stmt_struct *init; struct conditional_struct { struct expression_struct *condition; enum BranchType_enum { bt_equal0=0, bt_nequal0=1 } branchType; struct htslist_struct *statements; } *cBlock; } loopdata; struct { struct expression_struct *lvalue; struct expression_struct *rvalue; } assigndata; struct { char *name; struct slist_struct *paramExprs; } procdata; } data; } *funcCall; struct expression_struct *innerExpr; } edata; } *const_expr; struct slist_struct *typedIdentLists; int param_size; int pointer_p; } *


decompiled_code:
void __fastcall addIdToTable_noAddrMove(const char **a1, const char ***a2)
{
const char **v2; // rbp
const char *v3; // r12
int v4; // eax

v2 = *a2;
if ( *a2 )
{
v3 = *a1;
v4 = strcmp(*a1, *v2);
if ( v4 < 0 )
{
addIdToTable_noAddrMove(a1, v2 + 5);
}
else if ( v4 <= 0 )
{
printf("Error - symbol %s defined twice in the same scope\n", v3);
}
else
{
addIdToTable_noAddrMove(a1, v2 + 6);
}
}
else
{
*a2 = a1;
}
}

canonical_decompiled_code:
void __fastcall func5(const char **a1, const char ***a2)
{
const char **v2; // rbp
const char *v3; // r12
int v4; // eax

v2 = *a2;
if ( *a2 )
{
v3 = *a1;
v4 = strcmp(*a1, *v2);
if ( v4 < 0 )
{
func5(a1, v2 + 5);
}
else if ( v4 <= 0 )
{
printf("Error - symbol %s defined twice in the same scope\n", v3);
}
else
{
func5(a1, v2 + 6);
}
}
else
{
*a2 = a1;
}
}

original_code:
void addIdToTable_noAddrMove(ID* newId, ID** table) {
    if (*table == ((void *)0)) {
        *table = newId;
        return;
    }
    int cmp = strcmp(newId->id_name, (*table)->id_name);
    if (cmp < 0) {
        ID** p = &((*table)->id_left);
        addIdToTable_noAddrMove(newId, p);
    } else if (cmp > 0) {
        ID** p = &((*table)->id_right);
        addIdToTable_noAddrMove(newId, p);
    } else {
        printf("Error - symbol %s defined twice in the same scope\n", newId->id_name);
    }
}

canonical_original_code:
void addIdToTable_noAddrMove(struct id_info *newId, struct id_info **table) {
    if (*table == ((void *)0)) {
        *table = newId;
        return;
    }
    int cmp = strcmp(newId->id_name, (*table)->id_name);
    if (cmp < 0) {
        struct id_info **p = &((*table)->id_left);
        addIdToTable_noAddrMove(newId, p);
    } else if (cmp > 0) {
        struct id_info **p = &((*table)->id_right);
        addIdToTable_noAddrMove(newId, p);
    } else {
        printf("Error - symbol %s defined twice in the same scope\n", newId->id_name);
    }
}

variable_types:
table
struct id_info **

variable_types: cmp int

variable_types: struct id_info { char *id_name; int id_addr; int id_label; int id_level; struct type_info { int ty_size; char *ty_name; enum type_kind { Integer=0, Char=1, Boolean=2, String=3, Array=4, Record=5, UndefinedType=6 } ty_kind; struct type_info *ty_next; union { struct { struct type_info *RangeType; int min; int max; } ty_subrange; struct { struct type_info *ElementType; struct type_info *IndexType; int min; int max; } ty_array; struct { struct id_info *FirstField; } ty_record; } ty_form; } *id_type; enum identifier_kind { Constant_id=0, Type=1, Variable=2, FParameter=3, Field=4, Procedure=5, Function=6 } id_kind; struct id_info *id_left; struct id_info *id_right; struct id_info *id_next; struct expression_struct { struct type_info *type; enum expr_kind_enum { constant_expr=0, operation_bin=1, operation_un=2, registerVal=3, globalVar=4, localVar=5, functionCall=6, typecast=7 } kind; int int_val; char char_val; int bool_val; char *str_val; int str_const_index; struct expression_struct *offsetExpr; int pointer_p; int to_pointer_p; union { struct { struct expression_struct *operand1; struct expression_struct *operand2; enum operator_enum { op_and=0, op_or=1, op_not=2, op_negate=3, op_add=4, op_sub=5, op_mult=6, op_div=7, op_mod=8, op_equal=9, op_nequal=10, op_gt=11, op_lt=12, op_gte=13, op_lte=14 } op; } opdata; int reg_number; struct id_info *id; struct stmt_struct { enum stmt_type_enum { assign_stmt=0, if_stmt=1, while_stmt=2, repeat_stmt=3, for_stmt=4, stop_stmt=5, return_stmt=6, read_stmt=7, write_stmt=8, proc_stmt=9, null_stmt=10 } type; union { struct htslist_struct { struct slist_struct { struct slist_struct *next; void *data; } *head; struct slist_struct *tail; } *if_conditionals; struct slist_struct *exprList; struct expression_struct *expression; struct { struct stmt_struct *init; struct conditional_struct { struct expression_struct *condition; enum BranchType_enum { bt_equal0=0, bt_nequal0=1 } branchType; struct htslist_struct *statements; } *cBlock; } loopdata; struct { struct expression_struct *lvalue; struct expression_struct *rvalue; } assigndata; struct { char *name; struct slist_struct *paramExprs; } procdata; } data; } *funcCall; struct expression_struct *innerExpr; } edata; } *const_expr; struct slist_struct *typedIdentLists; int param_size; int pointer_p; } **