decompiled_code:
void log_warn(const char *a1, ...)
{
int *v1; // rbp
char *v2; // rax
int *v3; // rax
char *v4; // rax
char *v5; // rax
gcc_va_list va; // [rsp+0h] [rbp-E8h] BYREF
char *ptr; // [rsp+18h] [rbp-D0h] BYREF

if ( a1 )
{
va_start(va, a1);
v1 = _errno_location();
v2 = strerror(*v1);
if ( asprintf(&ptr, "%s: %s", a1, v2) == -1 )
{
vlog(2, a1, va);
v5 = strerror(*v1);
logit(2, "%s", v5);
}
else
{
vlog(2, ptr, va);
free(ptr);
}
}
else
{
v3 = _errno_location();
v4 = strerror(*v3);
logit(2, "%s", v4);
}
}

canonical_decompiled_code:
void func3(const char *a1, ...)
{
int *v1; // rbp
char *v2; // rax
int *v3; // rax
char *v4; // rax
char *v5; // rax
gcc_va_list va; // [rsp+0h] [rbp-E8h] BYREF
char *ptr; // [rsp+18h] [rbp-D0h] BYREF

if ( a1 )
{
va_start(va, a1);
v1 = _errno_location();
v2 = strerror(*v1);
if ( asprintf(&ptr, "%s: %s", a1, v2) == -1 )
{
func1(2, a1, va);
v5 = strerror(*v1);
func2(2, "%s", v5);
}
else
{
func1(2, ptr, va);
free(ptr);
}
}
else
{
v3 = _errno_location();
v4 = strerror(*v3);
func2(2, "%s", v4);
}
}

original_code:
void
log_warn(const char *emsg, ...)
{
 char *nfmt;
 va_list ap;
 if (emsg == ((void *)0))
  logit(2, "%s", strerror((*__errno_location ())));
 else {
  __builtin_va_start(ap,emsg);
  if (asprintf(&nfmt, "%s: %s", emsg, strerror((*__errno_location ()))) == -1) {
   vlog(2, emsg, ap);
   logit(2, "%s", strerror((*__errno_location ())));
  } else {
   vlog(2, nfmt, ap);
   free(nfmt);
  }
  __builtin_va_end(ap);
 }
}

canonical_original_code:
void log_warn(char *emsg, ...)
{
 char *nfmt;
 __builtin_va_list ap;
 if (emsg == ((void *)0))
  logit(2, "%s", strerror((*__errno_location ())));
 else {
  __builtin_va_start(ap,emsg);
  if (asprintf(&nfmt, "%s: %s", emsg, strerror((*__errno_location ()))) == -1) {
   vlog(2, emsg, ap);
   logit(2, "%s", strerror((*__errno_location ())));
  } else {
   vlog(2, nfmt, ap);
   free(nfmt);
  }
  __builtin_va_end(ap);
 }
}

variable_types:
emsg
char *


repo: conformal/adsuck

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

va_start(va, a1);
vlog(2, a1, va);
}

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

va_start(va, a1);
func1(2, a1, va);
}

original_code:
void
log_warnx(const char *emsg, ...)
{
 va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(2, emsg, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void log_warnx(char *emsg, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(2, emsg, ap);
 __builtin_va_end(ap);
}

return_type: void


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

va_start(va, a1);
vlog(6, a1, va);
}

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

va_start(va, a1);
func1(6, a1, va);
}

original_code:
void
log_info(const char *emsg, ...)
{
 va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(6, emsg, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void log_info(char *emsg, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,emsg);
 vlog(6, emsg, ap);
 __builtin_va_end(ap);
}


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

if ( debugsyslog | logdebug )
{
va_start(va, a1);
vlog(7, a1, va);
}
}

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

if ( debugsyslog | logdebug )
{
va_start(va, a1);
func1(7, a1, va);
}
}

original_code:
void
log_debug(const char *emsg, ...)
{
 va_list ap;
 if (logdebug || debugsyslog) {
  __builtin_va_start(ap,emsg);
  vlog(7, emsg, ap);
  __builtin_va_end(ap);
 }
}

canonical_original_code:
void log_debug(char *emsg, ...)
{
 __builtin_va_list ap;
 if (logdebug || debugsyslog) {
  __builtin_va_start(ap,emsg);
  vlog(7, emsg, ap);
  __builtin_va_end(ap);
 }
}


fatal

decompiled_code:
void __fastcall __noreturn fatal(const char *a1)
{
int v2; // edi
char *v3; // rax
int *v4; // rax
char *v5; // rax

if ( a1 )
{
v2 = *_errno_location();
if ( v2 )
{
v3 = strerror(v2);
logit(2, "fatal: %s: %s", a1, v3);
}
else
{
logit(2, "fatal: %s", a1);
}
}
else
{
v4 = _errno_location();
v5 = strerror(*v4);
logit(2, "fatal: %s", v5);
}
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
int v2; // edi
char *v3; // rax
int *v4; // rax
char *v5; // rax

if ( a1 )
{
v2 = *_errno_location();
if ( v2 )
{
v3 = strerror(v2);
func2(2, "fatal: %s: %s", a1, v3);
}
else
{
func2(2, "fatal: %s", a1);
}
}
else
{
v4 = _errno_location();
v5 = strerror(*v4);
func2(2, "fatal: %s", v5);
}
exit(1);
}

original_code:
void
fatal(const char *emsg)
{
 if (emsg == ((void *)0))
  logit(2, "fatal: %s", strerror((*__errno_location ())));
 else
  if ((*__errno_location ()))
   logit(2, "fatal: %s: %s",
       emsg, strerror((*__errno_location ())));
  else
   logit(2, "fatal: %s", emsg);
 exit(1);
}

canonical_original_code:
void fatal(char *emsg)
{
 if (emsg == ((void *)0))
  logit(2, "fatal: %s", strerror((*__errno_location ())));
 else
  if ((*__errno_location ()))
   logit(2, "fatal: %s: %s",
       emsg, strerror((*__errno_location ())));
  else
   logit(2, "fatal: %s", emsg);
 exit(1);
}


fatalx

decompiled_code:
void __fastcall __noreturn fatalx(const char *a1)
{
*_errno_location() = 0;
fatal(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
*_errno_location() = 0;
func7(a1);
}

original_code:
void
fatalx(const char *emsg)
{
 (*__errno_location ()) = 0;
 fatal(emsg);
}

canonical_original_code:
void fatalx(char *emsg)
{
 (*__errno_location ()) = 0;
 fatal(emsg);
}


decompiled_code:
const char *__fastcall log_sockaddr(const struct sockaddr *a1)
{
bool v1; // zf
const char *result; // rax

v1 = getnameinfo(a1, 0, buf_0, 0x401u, 0LL, 0, 1) == 0;
result = "(unknown)";
if ( v1 )
result = buf_0;
return result;
}

canonical_decompiled_code:
const char *__fastcall func9(const struct sockaddr *a1)
{
bool v1; // zf
const char *result; // rax

v1 = getnameinfo(a1, 0, buf_0, 0x401u, 0LL, 0, 1) == 0;
result = "(unknown)";
if ( v1 )
result = buf_0;
return result;
}

original_code:
const char *
log_sockaddr(struct sockaddr *sa)
{
 static char buf[1025];
 if (getnameinfo(sa, (0), buf, sizeof(buf), ((void *)0), 0,
     1))
  return ("(unknown)");
 else
  return (buf);
}

canonical_original_code:
char *log_sockaddr(struct sockaddr *sa)
{
 static char buf[1025];
 if (getnameinfo(sa, (0), buf, sizeof(buf), ((void *)0), 0,
     1))
  return ("(unknown)");
 else
  return (buf);
}

variable_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; } *

variable_types:
buf
char [1025]

return_type: char *


decompiled_code:
__int64 monitor_fork()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0xFFFFFFFFLL;
}

original_code:
pid_t
monitor_fork(char *filename)
{
 return (-1);
}

canonical_original_code:
int monitor_fork(char *filename)
{
 return (-1);
}

variable_types:
filename
char *

return_type: int


setresuid

decompiled_code:
__int64 __fastcall setresuid(__uid_t a1, __uid_t a2)
{
__int64 result; // rax

if ( setuid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(seteuid(a2) != 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__uid_t a1, __uid_t a2)
{
__int64 result; // rax

if ( setuid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(seteuid(a2) != 0);
return result;
}

original_code:
int setresuid(uid_t ruid, uid_t euid, uid_t suid)
{
 if (setuid(ruid))
  return -1;
 if (seteuid(euid))
  return -1;
 return 0;
}

canonical_original_code:
int setresuid(unsigned int ruid, unsigned int euid, unsigned int suid)
{
 if (setuid(ruid))
  return -1;
 if (seteuid(euid))
  return -1;
 return 0;
}

variable_types:
ruid
unsigned int

variable_types:
euid
unsigned int

variable_types:
suid
unsigned int


setresgid

decompiled_code:
__int64 __fastcall setresgid(__gid_t a1, __gid_t a2)
{
__int64 result; // rax

if ( setgid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(setegid(a2) != 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__gid_t a1, __gid_t a2)
{
__int64 result; // rax

if ( setgid(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(setegid(a2) != 0);
return result;
}

original_code:
int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
{
 if (setgid(rgid))
  return -1;
 if (setegid(egid))
  return -1;
 return 0;
}

canonical_original_code:
int setresgid(unsigned int rgid, unsigned int egid, unsigned int sgid)
{
 if (setgid(rgid))
  return -1;
 if (setegid(egid))
  return -1;
 return 0;
}

variable_types:
rgid
unsigned int

variable_types:
egid
unsigned int

variable_types:
sgid
unsigned int


strlcpy

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
 char *d = dst;
 const char *s = src;
 size_t n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
 char *d = dst;
 char *s = src;
 unsigned long n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long

return_type: unsigned long


repo: conformal/clens

decompiled_code:
__int64 __fastcall arc4random_uniform(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
unsigned int v3; // ebp
unsigned int v4; // eax

v2 = 0LL;
if ( (unsigned int)a1 <= 1 )
return 0LL;
if ( (unsigned int)a1 <= 0x80000000 )
{
v2 = -2 * (int)a1 % (unsigned int)a1;
v3 = v2;
}
else
{
v3 = -(int)a1;
}
do
v4 = arc4random(a1, a2, v2);
while ( v4 < v3 );
return v4 % (unsigned int)a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
unsigned int v3; // ebp
unsigned int v4; // eax

v2 = 0LL;
if ( (unsigned int)a1 <= 1 )
return 0LL;
if ( (unsigned int)a1 <= 0x80000000 )
{
v2 = -2 * (int)a1 % (unsigned int)a1;
v3 = v2;
}
else
{
v3 = -(int)a1;
}
do
v4 = arc4random(a1, a2, v2);
while ( v4 < v3 );
return v4 % (unsigned int)a1;
}

original_code:
uint32_t
arc4random_uniform(u_int32_t upper_bound)
{
 uint32_t r, min;
 if (upper_bound < 2)
  return 0;
 if (upper_bound > 0x80000000)
  min = 1 + ~upper_bound;
 else {
  min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
 }
 for (;;) {
  r = arc4random();
  if (r >= min)
   break;
 }
 return r % upper_bound;
}

canonical_original_code:
unsigned int arc4random_uniform(unsigned int upper_bound)
{
 unsigned int r; unsigned int min;
 if (upper_bound < 2)
  return 0;
 if (upper_bound > 0x80000000)
  min = 1 + ~upper_bound;
 else {
  min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
 }
 for (;;) {
  r = arc4random();
  if (r >= min)
   break;
 }
 return r % upper_bound;
}

variable_types:
r
unsigned int

variable_types:
min
unsigned int

return_type: unsigned int


original_code:
int
scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i, ndigits = 0, fract_digits = 0;
 long long scale_fact = 1, whole = 0, fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short int) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

canonical_original_code:
int scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i; unsigned int ndigits = 0; unsigned int fract_digits = 0;
 long long scale_fact = 1; long long whole = 0; long long fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

variable_types:
scaled
char *

variable_types:
result
long long *

variable_types:
p
char *

variable_types: sign int

variable_types:
i
unsigned int

variable_types:
ndigits
unsigned int

variable_types:
whole
long long

variable_types:
fpart
long long


original_code:
int
fmt_scaled(long long number, char *result)
{
 long long abval, fract = 0;
 unsigned int i;
 unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

canonical_original_code:
int fmt_scaled(long long number, char *result)
{
 long long abval; long long fract = 0;
 unsigned int i;
 enum unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

variable_types:
number
long long

variable_types:
result
char *

variable_types:
abval
long long

variable_types:
fract
long long

variable_types: enum unit_type { NONE=0, KILO=1, MEGA=2, GIGA=3, TERA=4, PETA=5, EXA=6 }


strtonum

original_code:
long long
strtonum(const char *numstr, long long minval, long long maxval,
    const char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval {
  const char *errstr;
  int err;
 } ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

canonical_original_code:
long long strtonum(char *numstr, long long minval, long long maxval, char **errstrp)
{
 long long ll = 0;
 char *ep;
 int error = 0;
 struct errval ev[4] = {
  { ((void *)0), 0 },
  { "invalid", 22 },
  { "too small", 34 },
  { "too large", 34 },
 };
 ev[0].err = (*__errno_location ());
 (*__errno_location ()) = 0;
 if (minval > maxval)
  error = 1;
 else {
  ll = strtoll(numstr, &ep, 10);
  if (numstr == ep || *ep != '\0')
   error = 1;
  else if ((ll == (-0x7fffffffffffffffLL - 1LL) && (*__errno_location ()) == 34) || ll < minval)
   error = 2;
  else if ((ll == 0x7fffffffffffffffLL && (*__errno_location ()) == 34) || ll > maxval)
   error = 3;
 }
 if (errstrp != ((void *)0))
  *errstrp = ev[error].errstr;
 (*__errno_location ()) = ev[error].err;
 if (error)
  ll = 0;
 return (ll);
}

variable_types:
numstr
char *

variable_types:
minval
long long

variable_types:
maxval
long long

variable_types:
errstrp
char **

variable_types:
ll
long long

variable_types:
ep
char *

variable_types: error int

variable_types: struct errval { char *errstr; int err; } [4]

return_type: long long


decompiled_code:
const char *clens_verstring()
{
return "0.7.0";
}

canonical_decompiled_code:
const char *func0()
{
return "0.7.0";
}

original_code:
const char *
clens_verstring(void)
{
 return vertag;
}

canonical_original_code:
char *clens_verstring()
{
 return vertag;
}


isescaped

decompiled_code:
__int64 __fastcall isescaped(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi
__int64 v6; // rax

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
if ( a1 > a2 - 1 )
{
LOBYTE(v6) = 0;
}
else
{
v5 = a2 - a1;
v6 = 0LL;
while ( *v4 == a3 )
{
++v6;
--v4;
if ( v6 == v5 )
{
LOBYTE(v6) = v5;
break;
}
}
}
result = v6 & 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
char *v4; // rcx
__int64 v5; // rsi
__int64 v6; // rax

result = 1LL;
if ( a3 )
{
v4 = (char *)(a2 - 1);
if ( a1 > a2 - 1 )
{
LOBYTE(v6) = 0;
}
else
{
v5 = a2 - a1;
v6 = 0LL;
while ( *v4 == a3 )
{
++v6;
--v4;
if ( v6 == v5 )
{
LOBYTE(v6) = v5;
break;
}
}
}
result = v6 & 1;
}
return result;
}

original_code:
static int
isescaped(const char *sp, const char *p, int esc)
{
 const char *cp;
 size_t ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

canonical_original_code:
static int isescaped(char *sp, char *p, int esc)
{
 char *cp;
 unsigned long ne;
 if (esc == '\0')
  return 1;
 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  continue;
 return (ne & 1) != 0;
}

variable_types:
sp
char *

variable_types:
p
char *

variable_types: esc int

variable_types:
cp
char *

variable_types:
ne
unsigned long


strlcpy

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
 char *d = dst;
 const char *s = src;
 size_t n = siz;
 if (n != 0) {
  while (--n != 0) {
   if ((*d++ = *s++) == '\0')
    break;
  }
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
 char *d = dst;
 char *s = src;
 unsigned long n = siz;
 if (n != 0) {
  while (--n != 0) {
   if ((*d++ = *s++) == '\0')
    break;
  }
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long


repo: conformal/cyphertite

decompiled_code:
int __fastcall ct_glob_match(const char **a1, const char *a2)
{
const char *v3; // rdi
const char **v4; // rbx
int result; // eax

if ( !a1 )
return 1;
v3 = *a1;
if ( !v3 )
return 1;
v4 = a1 + 1;
while ( 1 )
{
result = fnmatch(v3, a2, 0);
if ( !result )
break;
v3 = *v4++;
if ( !v3 )
return 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char **a1, const char *a2)
{
const char *v3; // rdi
const char **v4; // rbx
int result; // eax

if ( !a1 )
return 1;
v3 = *a1;
if ( !v3 )
return 1;
v4 = a1 + 1;
while ( 1 )
{
result = fnmatch(v3, a2, 0);
if ( !result )
break;
v3 = *v4++;
if ( !v3 )
return 1;
}
return result;
}

original_code:
int
ct_glob_match(char **g, char *file)
{
 int i;
 if (g == ((void *)0))
  return (1);
 for (i = 0; g[i] != ((void *)0); i++) {
  if (g[i] == ((void *)0))
   break;
  if (fnmatch(g[i], file, 0) == 0)
   return (0);
 }
 return (1);
}

variable_types:
g
char **

variable_types:
file
char *

variable_types: i int


repo: contra/PiFM

decompiled_code:
__int64 __fastcall mem_virt_to_phys(int a1)
{
return *(_DWORD *)(page_map + 16LL * ((unsigned int)(a1 - virtbase) >> 12) + 8) + ((a1 - (_DWORD)virtbase) & 0xFFFu);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
return *(_DWORD *)(page_map + 16LL * ((unsigned int)(a1 - virtbase) >> 12) + 8) + ((a1 - (_DWORD)virtbase) & 0xFFFu);
}

original_code:
static uint32_t
mem_virt_to_phys(void *virt)
{
  uint32_t offset = (uint8_t *)virt - virtbase;
  return page_map[offset >> 12].physaddr + (offset % 4096);
}

canonical_original_code:
static unsigned int mem_virt_to_phys(void *virt)
{
  unsigned int offset = (uint8_t *)virt - virtbase;
  return page_map[offset >> 12].physaddr + (offset % 4096);
}

variable_types:
virt
void *

variable_types:
offset
unsigned int


udelay

decompiled_code:
int __fastcall udelay(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = 1000 * a1;
return nanosleep(&v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
struct timespec v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = 1000 * a1;
return nanosleep(&v2, 0LL);
}

original_code:
static void
udelay(int us)
{
  struct timespec ts = { 0, us * 1000 };
  nanosleep(&ts, ((void *)0));
}

variable_types: us int

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


terminate

decompiled_code:
void __noreturn terminate()
{
if ( dma_reg )
{
*(_DWORD *)dma_reg = 0x80000000;
udelay(10);
}
exit(1);
}

canonical_decompiled_code:
void __noreturn func9()
{
if ( dma_reg )
{
*(_DWORD *)dma_reg = 0x80000000;
func8(10);
}
exit(1);
}

original_code:
static void
terminate(int dummy)
{
  if (dma_reg) {
    dma_reg[(0x00/4)] = (1<<31);
    udelay(10);
  }
  exit(1);
}

variable_types: dummy int


fatal

decompiled_code:
void __noreturn fatal(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(_bss_start, format, arg);
terminate(0LL);
}

canonical_decompiled_code:
void __noreturn func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(_bss_start, format, arg);
func9(0LL);
}

original_code:
static void
fatal(char *fmt, ...)
{
  va_list ap;
  __builtin_va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  __builtin_va_end(ap);
  terminate(0);
}

canonical_original_code:
static void fatal(char *fmt, ...)
{
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  __builtin_va_end(ap);
  terminate(0);
}

variable_types:
fmt
char *


decompiled_code:
void *__fastcall map_peripheral(unsigned int a1, size_t len)
{
int v2; // eax
int v3; // ebx
void *v4; // rbp

v2 = open("/dev/mem", 2);
if ( v2 < 0 )
fatal("Failed to open /dev/mem: %m\n");
v3 = v2;
v4 = mmap(0LL, (unsigned int)len, 3, 1, v2, a1);
if ( v4 == (void *)-1LL )
fatal("Failed to map peripheral at 0x%08x: %m\n", a1);
close(v3);
return v4;
}

canonical_decompiled_code:
void *__fastcall func11(unsigned int a1, size_t len)
{
int v2; // eax
int v3; // ebx
void *v4; // rbp

v2 = open("/dev/mem", 2);
if ( v2 < 0 )
func10("Failed to open /dev/mem: %m\n");
v3 = v2;
v4 = mmap(0LL, (unsigned int)len, 3, 1, v2, a1);
if ( v4 == (void *)-1LL )
func10("Failed to map peripheral at 0x%08x: %m\n", a1);
close(v3);
return v4;
}

original_code:
static void *
map_peripheral(uint32_t base, uint32_t len)
{
  int fd = open("/dev/mem", 02);
  void * vaddr;
  if (fd < 0)
    fatal("Failed to open /dev/mem: %m\n");
  vaddr = mmap(((void *)0), len, 0x1|0x2, 0x01, fd, base);
  if (vaddr == ((void *) -1))
    fatal("Failed to map peripheral at 0x%08x: %m\n", base);
  close(fd);
  return vaddr;
}

canonical_original_code:
static void *map_peripheral(unsigned int base, unsigned int len)
{
  int fd = open("/dev/mem", 02);
  void *vaddr;
  if (fd < 0)
    fatal("Failed to open /dev/mem: %m\n");
  vaddr = mmap(((void *)0), len, 0x1|0x2, 0x01, fd, base);
  if (vaddr == ((void *) -1))
    fatal("Failed to map peripheral at 0x%08x: %m\n", base);
  close(fd);
  return vaddr;
}

variable_types:
base
unsigned int

variable_types:
len
unsigned int

variable_types: fd int

variable_types:
vaddr
void *

return_type: void *


decompiled_code:
__int64 __fastcall mem_phys_to_virt(unsigned int a1)
{
__int64 v1; // rsi
int v2; // edi
_DWORD *v3; // rax
int v4; // edx

v1 = a1;
v2 = a1 & 0xFFF;
v3 = (_DWORD *)(page_map + 8);
v4 = 0;
while ( *v3 != (v1 & 0xFFFFF000) )
{
++v4;
v3 += 4;
if ( v4 == 67 )
fatal("Failed to reverse map phys addr %08x\n", v1);
}
return (unsigned int)(virtbase + v2 + (v4 << 12));
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1)
{
__int64 v1; // rsi
int v2; // edi
_DWORD *v3; // rax
int v4; // edx

v1 = a1;
v2 = a1 & 0xFFF;
v3 = (_DWORD *)(page_map + 8);
v4 = 0;
while ( *v3 != (v1 & 0xFFFFF000) )
{
++v4;
v3 += 4;
if ( v4 == 67 )
func10("Failed to reverse map phys addr %08x\n", v1);
}
return (unsigned int)(virtbase + v2 + (v4 << 12));
}

original_code:
static uint32_t
mem_phys_to_virt(uint32_t phys)
{
  uint32_t pg_offset = phys & (4096 - 1);
  uint32_t pg_addr = phys - pg_offset;
  int i;
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    if (page_map[i].physaddr == pg_addr) {
      return (uint32_t)virtbase + i * 4096 + pg_offset;
    }
  }
  fatal("Failed to reverse map phys addr %08x\n", phys);
  return 0;
}

canonical_original_code:
static unsigned int mem_phys_to_virt(unsigned int phys)
{
  unsigned int pg_offset = phys & (4096 - 1);
  unsigned int pg_addr = phys - pg_offset;
  int i;
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    if (page_map[i].physaddr == pg_addr) {
      return (unsigned int)virtbase + i * 4096 + pg_offset;
    }
  }
  fatal("Failed to reverse map phys addr %08x\n", phys);
  return 0;
}

variable_types:
phys
unsigned int

user_defined_types: struct control_data_s { struct dma_cb_t cb[-1]; unsigned int sample[4000]; }


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx
__int64 v4; // rax
__int64 v5; // rbx
unsigned int v6; // eax
int v7; // eax
int v8; // er12
unsigned __int64 v9; // rbp
int v10; // eax
__int64 v11; // r12
_DWORD *v12; // rbx
__int64 v13; // r14
_DWORD *v14; // rbp
_DWORD *v15; // rbx
int v16; // er13
int v17; // eax
int v18; // er12
int v19; // ebx
int v20; // ebp
signed int j; // er14
int v22; // eax
signed int v23; // ebx
__m128i v24; // xmm2
int v25; // edi
int v26; // er10
__int64 v27; // r9
int k; // eax
signed int v29; // edx
int v30; // eax
struct sigaction buf; // [rsp+10h] [rbp-918h] BYREF
__int16 v32[1024]; // [rsp+B0h] [rbp-878h] BYREF
char s[120]; // [rsp+8B0h] [rbp-78h] BYREF

for ( i = 0; i != 64; ++i )
{
memset(&buf.sa_mask, 0, 0x90uLL);
buf.sa_handler = (__sighandler_t)terminate;
sigaction(i, &buf, 0LL);
}
dma_reg = (__int64)map_peripheral(0x20007000u, 0x24uLL);
pwm_reg = (__int64)map_peripheral(0x2020C000u, 0x28uLL);
clk_reg = (__int64)map_peripheral(0x20101000u, 0xA8uLL);
gpio_reg = (__int64)map_peripheral(0x20200000u, 0xB4uLL);
v4 = (__int64)mmap(0LL, 0x43000uLL, 3, 24609, -1, 0LL);
virtbase = v4;
if ( v4 != -1 )
{
v5 = v4 & 0xFFF;
if ( (v4 & 0xFFF) != 0 )
fatal("Virtual address is not page aligned\n");
printf("Virtual memory mapped at %p\n", (const void *)v4);
page_map = (__int64)malloc(0x430uLL);
if ( page_map )
{
v6 = getpid();
sprintf(s, "/proc/%d/pagemap", v6);
v7 = open(s, 0);
v8 = v7;
if ( v7 >= 0 )
{
v9 = (unsigned __int64)virtbase >> 9;
if ( v9 == lseek(v7, (unsigned __int64)virtbase >> 9, 0) )
{
while ( 1 )
{
*(_QWORD *)(page_map + 16 * v5) = virtbase + (v5 << 12);
**(_BYTE **)(page_map + 16 * v5) = 0;
if ( read(v8, &buf, 8uLL) != 8 )
break;
if ( ((__int64)buf.sa_handler & 0x80000000000000LL) != 0 )
fatal("Page %d not present (pfn 0x%016llx)\n", (unsigned int)v5, buf.sa_handler);
*(_DWORD *)(page_map + 16 * v5++ + 8) = (LODWORD(buf.sa_handler) << 12) | 0x40000000;
if ( v5 == 67 )
{
v10 = *(_DWORD *)gpio_reg;
BYTE1(v10) = BYTE1(*(_DWORD *)gpio_reg) & 0x8F | 0x40;
*(_DWORD *)gpio_reg = v10;
*(_DWORD *)(clk_reg + 112) = 1509949446;
udelay(100);
*(_DWORD *)(clk_reg + 112) = 1509949974;
v11 = virtbase;
ctl = virtbase;
v12 = (_DWORD *)virtbase;
v13 = virtbase + 256000;
v14 = (_DWORD *)(virtbase + 256000);
do
{
*v14 = 1509969920;
*v12 = 67108872;
v12[1] = mem_virt_to_phys((int)v14);
v12[2] = 2114982004;
v12[3] = 4;
v12[4] = 0;
v12[5] = mem_virt_to_phys((int)v12 + 32);
v12[8] = 67436616;
v12[9] = mem_virt_to_phys(v11);
v12[10] = 2116075544;
v12[11] = 4;
v12[12] = 0;
v12 += 16;
*(v12 - 3) = mem_virt_to_phys((int)v12);
++v14;
}
while ( v12 != (_DWORD *)v13 );
*(_DWORD *)(v11 + 255988) = mem_virt_to_phys(v11);
*(_DWORD *)pwm_reg = 0;
udelay(10);
*(_DWORD *)(clk_reg + 160) = 1509949446;
udelay(100);
*(_DWORD *)(clk_reg + 164) = 1509969920;
udelay(100);
*(_DWORD *)(clk_reg + 160) = 1509949462;
udelay(100);
*(_DWORD *)(pwm_reg + 16) = 454;
udelay(10);
*(_DWORD *)(pwm_reg + 8) = -2147479793;
udelay(10);
*(_DWORD *)pwm_reg = 64;
udelay(10);
*(_DWORD *)pwm_reg = 33;
udelay(10);
*(_DWORD *)dma_reg = 0x80000000;
udelay(10);
v15 = (_DWORD *)dma_reg;
*(_DWORD *)dma_reg = 6;
v15[1] = mem_virt_to_phys(ctl);
v15[8] = 7;
*v15 = 277348353;
if ( argc <= 1 )
{
v16 = 0;
}
else
{
v16 = open(argv[1], 114);
if ( v16 < 0 )
fatal("Failed to open .wav file\n");
}
v17 = read(v16, v32, 0x800uLL);
if ( v17 >= 0 )
{
if ( v17 > 45 )
{
v18 = v17 / 2;
v19 = ctl;
v20 = 22;
while ( 1 )
{
usleep(0x2710u);
v22 = mem_phys_to_virt(*(_DWORD *)(dma_reg + 4));
v23 = (unsigned int)(v19 - virtbase) >> 6;
for ( j = ((unsigned int)(v22 - virtbase) >> 6)
- v23
+ ((int)(((unsigned int)(v22 - virtbase) >> 6) - v23) < 0 ? 0xFA0 : 0); j > 9; j -= 10 )
{
v24 = 0LL;
*(float *)v24.m128i_i32 = (double)v32[v20] * 0.0000152587890625 * 25.0;
v25 = (int)floorf(*(float *)v24.m128i_i32);
v26 = (int)((float)(COERCE_FLOAT(_mm_cvtsi128_si32(v24)) - (float)v25) * 10.0);
v27 = ctl;
for ( k = 0; k != 10; ++k )
{
v29 = v23 + 1;
*(_DWORD *)(v27 + 4LL * v23 + 256000) = v25 + (v26 > k) + 1509969920;
if ( v23 == 3999 )
v29 = 0;
v23 = v29;
}
if ( v18 <= ++v20 )
{
v30 = read(v16, v32, 0x800uLL);
if ( v30 < 0 )
fatal("Error reading data: %m\n");
v18 = v30 / 2;
if ( !(v30 / 2) )
terminate(0LL);
v20 = 0;
}
}
v19 = virtbase + (v23 << 6);
}
}
fatal("Initial read of .wav file too short\n");
}
fatal("Failed to read .wav file\n");
}
}
fatal("Failed to read %s: %m\n", s);
}
fatal("Failed to seek on %s: %m\n", s);
}
fatal("Failed to open %s: %m\n", s);
}
fatal("Failed to malloc page_map: %m\n");
}
fatal("Failed to mmap physical pages: %m\n");
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx
__int64 v4; // rax
__int64 v5; // rbx
unsigned int v6; // eax
int v7; // eax
int v8; // er12
unsigned __int64 v9; // rbp
int v10; // eax
__int64 v11; // r12
_DWORD *v12; // rbx
__int64 v13; // r14
_DWORD *v14; // rbp
_DWORD *v15; // rbx
int v16; // er13
int v17; // eax
int v18; // er12
int v19; // ebx
int v20; // ebp
signed int j; // er14
int v22; // eax
signed int v23; // ebx
__m128i v24; // xmm2
int v25; // edi
int v26; // er10
__int64 v27; // r9
int k; // eax
signed int v29; // edx
int v30; // eax
struct sigaction buf; // [rsp+10h] [rbp-918h] BYREF
__int16 v32[1024]; // [rsp+B0h] [rbp-878h] BYREF
char s[120]; // [rsp+8B0h] [rbp-78h] BYREF

for ( i = 0; i != 64; ++i )
{
memset(&buf.sa_mask, 0, 0x90uLL);
buf.sa_handler = (__sighandler_t)terminate;
sigaction(i, &buf, 0LL);
}
dma_reg = (__int64)func11(0x20007000u, 0x24uLL);
pwm_reg = (__int64)func11(0x2020C000u, 0x28uLL);
clk_reg = (__int64)func11(0x20101000u, 0xA8uLL);
gpio_reg = (__int64)func11(0x20200000u, 0xB4uLL);
v4 = (__int64)mmap(0LL, 0x43000uLL, 3, 24609, -1, 0LL);
virtbase = v4;
if ( v4 != -1 )
{
v5 = v4 & 0xFFF;
if ( (v4 & 0xFFF) != 0 )
func10("Virtual address is not page aligned\n");
printf("Virtual memory mapped at %p\n", (const void *)v4);
page_map = (__int64)malloc(0x430uLL);
if ( page_map )
{
v6 = getpid();
sprintf(s, "/proc/%d/pagemap", v6);
v7 = open(s, 0);
v8 = v7;
if ( v7 >= 0 )
{
v9 = (unsigned __int64)virtbase >> 9;
if ( v9 == lseek(v7, (unsigned __int64)virtbase >> 9, 0) )
{
while ( 1 )
{
*(_QWORD *)(page_map + 16 * v5) = virtbase + (v5 << 12);
**(_BYTE **)(page_map + 16 * v5) = 0;
if ( read(v8, &buf, 8uLL) != 8 )
break;
if ( ((__int64)buf.sa_handler & 0x80000000000000LL) != 0 )
func10("Page %d not present (pfn 0x%016llx)\n", (unsigned int)v5, buf.sa_handler);
*(_DWORD *)(page_map + 16 * v5++ + 8) = (LODWORD(buf.sa_handler) << 12) | 0x40000000;
if ( v5 == 67 )
{
v10 = *(_DWORD *)gpio_reg;
BYTE1(v10) = BYTE1(*(_DWORD *)gpio_reg) & 0x8F | 0x40;
*(_DWORD *)gpio_reg = v10;
*(_DWORD *)(clk_reg + 112) = 1509949446;
func8(100);
*(_DWORD *)(clk_reg + 112) = 1509949974;
v11 = virtbase;
ctl = virtbase;
v12 = (_DWORD *)virtbase;
v13 = virtbase + 256000;
v14 = (_DWORD *)(virtbase + 256000);
do
{
*v14 = 1509969920;
*v12 = 67108872;
v12[1] = func7((int)v14);
v12[2] = 2114982004;
v12[3] = 4;
v12[4] = 0;
v12[5] = func7((int)v12 + 32);
v12[8] = 67436616;
v12[9] = func7(v11);
v12[10] = 2116075544;
v12[11] = 4;
v12[12] = 0;
v12 += 16;
*(v12 - 3) = func7((int)v12);
++v14;
}
while ( v12 != (_DWORD *)v13 );
*(_DWORD *)(v11 + 255988) = func7(v11);
*(_DWORD *)pwm_reg = 0;
func8(10);
*(_DWORD *)(clk_reg + 160) = 1509949446;
func8(100);
*(_DWORD *)(clk_reg + 164) = 1509969920;
func8(100);
*(_DWORD *)(clk_reg + 160) = 1509949462;
func8(100);
*(_DWORD *)(pwm_reg + 16) = 454;
func8(10);
*(_DWORD *)(pwm_reg + 8) = -2147479793;
func8(10);
*(_DWORD *)pwm_reg = 64;
func8(10);
*(_DWORD *)pwm_reg = 33;
func8(10);
*(_DWORD *)dma_reg = 0x80000000;
func8(10);
v15 = (_DWORD *)dma_reg;
*(_DWORD *)dma_reg = 6;
v15[1] = func7(ctl);
v15[8] = 7;
*v15 = 277348353;
if ( argc <= 1 )
{
v16 = 0;
}
else
{
v16 = open(argv[1], 114);
if ( v16 < 0 )
func10("Failed to open .wav file\n");
}
v17 = read(v16, v32, 0x800uLL);
if ( v17 >= 0 )
{
if ( v17 > 45 )
{
v18 = v17 / 2;
v19 = ctl;
v20 = 22;
while ( 1 )
{
usleep(0x2710u);
v22 = func12(*(_DWORD *)(dma_reg + 4));
v23 = (unsigned int)(v19 - virtbase) >> 6;
for ( j = ((unsigned int)(v22 - virtbase) >> 6)
- v23
+ ((int)(((unsigned int)(v22 - virtbase) >> 6) - v23) < 0 ? 0xFA0 : 0); j > 9; j -= 10 )
{
v24 = 0LL;
*(float *)v24.m128i_i32 = (double)v32[v20] * 0.0000152587890625 * 25.0;
v25 = (int)floorf(*(float *)v24.m128i_i32);
v26 = (int)((float)(COERCE_FLOAT(_mm_cvtsi128_si32(v24)) - (float)v25) * 10.0);
v27 = ctl;
for ( k = 0; k != 10; ++k )
{
v29 = v23 + 1;
*(_DWORD *)(v27 + 4LL * v23 + 256000) = v25 + (v26 > k) + 1509969920;
if ( v23 == 3999 )
v29 = 0;
v23 = v29;
}
if ( v18 <= ++v20 )
{
v30 = read(v16, v32, 0x800uLL);
if ( v30 < 0 )
func10("Error reading data: %m\n");
v18 = v30 / 2;
if ( !(v30 / 2) )
func9(0LL);
v20 = 0;
}
}
v19 = virtbase + (v23 << 6);
}
}
func10("Initial read of .wav file too short\n");
}
func10("Failed to read .wav file\n");
}
}
func10("Failed to read %s: %m\n", s);
}
func10("Failed to seek on %s: %m\n", s);
}
func10("Failed to open %s: %m\n", s);
}
func10("Failed to malloc page_map: %m\n");
}
func10("Failed to mmap physical pages: %m\n");
}

original_code:
int
main(int argc, char **argv)
{
  int i, fd, pid, freq_ctl;
  char pagemap_fn[64];
  for (i = 0; i < 64; i++) {
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.__sigaction_handler.sa_handler = terminate;
    sigaction(i, &sa, ((void *)0));
  }
  freq_ctl = ((float)(500000000 / 100000000)) * ( 1 << 12 );
  dma_reg = map_peripheral(0x20007000, 0x24);
  pwm_reg = map_peripheral(0x2020C000, 0x28);
  clk_reg = map_peripheral(0x20101000, 0xA8);
  gpio_reg = map_peripheral(0x20200000, 0xB4);
  virtbase = mmap(((void *)0), ((sizeof(struct control_data_s) + 4096 - 1) >> 12) * 4096, 0x1|0x2,
      0x01|0x20|0x04000|0x02000,
      -1, 0);
  if (virtbase == ((void *) -1))
    fatal("Failed to mmap physical pages: %m\n");
  if ((unsigned long)virtbase & (4096 -1))
    fatal("Virtual address is not page aligned\n");
  printf("Virtual memory mapped at %p\n", virtbase);
  page_map = malloc(((sizeof(struct control_data_s) + 4096 - 1) >> 12) * sizeof(*page_map));
  if (page_map == 0)
    fatal("Failed to malloc page_map: %m\n");
  pid = getpid();
  sprintf(pagemap_fn, "/proc/%d/pagemap", pid);
  fd = open(pagemap_fn, 00);
  if (fd < 0)
    fatal("Failed to open %s: %m\n", pagemap_fn);
  if (lseek(fd, (unsigned long)virtbase >> 9, 0) != (unsigned long)virtbase >> 9)
    fatal("Failed to seek on %s: %m\n", pagemap_fn);
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    uint64_t pfn;
    page_map[i].virtaddr = virtbase + i * 4096;
    page_map[i].virtaddr[0] = 0;
    if (read(fd, &pfn, sizeof(pfn)) != sizeof(pfn))
      fatal("Failed to read %s: %m\n", pagemap_fn);
    if ((pfn >> 55)&0xfbf != 0x10c)
      fatal("Page %d not present (pfn 0x%016llx)\n", i, pfn);
    page_map[i].physaddr = (uint32_t)pfn << 12 | 0x40000000;
  }
  gpio_reg[(0x00/4)] = (gpio_reg[(0x00/4)] & ~(7 << 12)) | (4 << 12);
  clk_reg[(0x70/4)] = 0x5A << 24 | 6;
  udelay(100);
  clk_reg[(0x70/4)] = 0x5A << 24 | 1 << 9 | 1 << 4 | 6;
  ctl = (struct control_data_s *)virtbase;
  dma_cb_t *cbp = ctl->cb;
  uint32_t phys_sample_dst = 0x7e101074;
  uint32_t phys_pwm_fifo_addr = 0x7e20c000 + 0x18;
  for (i = 0; i < 4000; i++) {
    ctl->sample[i] = 0x5a << 24 | freq_ctl;
    cbp->info = (1<<26) | (1<<3);
    cbp->src = mem_virt_to_phys(ctl->sample + i);
    cbp->dst = phys_sample_dst;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
    cbp->info = (1<<26) | (1<<3) | (1<<6) | ((5)<<16);
    cbp->src = mem_virt_to_phys(virtbase);
    cbp->dst = phys_pwm_fifo_addr;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
  }
  cbp--;
  cbp->next = mem_virt_to_phys(virtbase);
  pwm_reg[(0x00/4)] = 0;
  udelay(10);
  clk_reg[40] = 0x5A000006;
  udelay(100);
  clk_reg[41] = 0x5A000000 | (5<<12);
  udelay(100);
  clk_reg[40] = 0x5A000016;
  udelay(100);
  pwm_reg[(0x10/4)] = 454;
  udelay(10);
  pwm_reg[(0x08/4)] = (1<<31) | ((15<<8)|(15<<0));
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<6);
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<5) | (1<<0);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<31);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<2) | (1<<1);
  dma_reg[(0x04/4)] = mem_virt_to_phys(ctl->cb);
  dma_reg[(0x20/4)] = 7;
  dma_reg[(0x00/4)] = 0x10880001;
  fd = 0;
  if (argc > 1) {
          fd = open(argv[1], 'r');
    if (fd < 0)
      fatal("Failed to open .wav file\n");
  }
        short data[1024];
        int data_len = read(fd, data, sizeof(data));
  if (data_len < 0)
    fatal("Failed to read .wav file\n");
  data_len /= 2;
  if (data_len < 23)
    fatal("Initial read of .wav file too short\n");
  uint32_t last_cb = (uint32_t)ctl->cb;
  int data_index = 22;
  for (;;) {
    usleep(10000);
    uint32_t cur_cb = mem_phys_to_virt(dma_reg[(0x04/4)]);
    int last_sample = (last_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int this_sample = (cur_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int free_slots = this_sample - last_sample;
    if (free_slots < 0)
      free_slots += 4000;
    while (free_slots >= 10) {
      float dval = (float)(data[data_index])/65536.0 * 25.0;
      int intval = (int)((floor)(dval));
      int frac = (int)((dval - (float)intval) * 10.0);
      int j;
      for (j = 0; j < 10; j++) {
        ctl->sample[last_sample++] = (0x5A << 24 | freq_ctl) + (frac > j ? intval + 1 : intval);
        if (last_sample == 4000)
          last_sample = 0;
      }
      free_slots -= 10;
      if (++data_index >= data_len) {
              data_len = read(fd, data, sizeof(data));
        data_index = 0;
        if (data_len < 0)
          fatal("Error reading data: %m\n");
        data_len /= 2;
        if (data_len == 0)
          terminate(0);
      }
    }
    last_cb = (uint32_t)virtbase + last_sample * sizeof(dma_cb_t) * 2;
  }
  terminate(0);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int i; int fd; int pid; int freq_ctl;
  char pagemap_fn[64];
  for (i = 0; i < 64; i++) {
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.__sigaction_handler.sa_handler = terminate;
    sigaction(i, &sa, ((void *)0));
  }
  freq_ctl = ((float)(500000000 / 100000000)) * ( 1 << 12 );
  dma_reg = map_peripheral(0x20007000, 0x24);
  pwm_reg = map_peripheral(0x2020C000, 0x28);
  clk_reg = map_peripheral(0x20101000, 0xA8);
  gpio_reg = map_peripheral(0x20200000, 0xB4);
  virtbase = mmap(((void *)0), ((sizeof(struct control_data_s) + 4096 - 1) >> 12) * 4096, 0x1|0x2,
      0x01|0x20|0x04000|0x02000,
      -1, 0);
  if (virtbase == ((void *) -1))
    fatal("Failed to mmap physical pages: %m\n");
  if ((unsigned long)virtbase & (4096 -1))
    fatal("Virtual address is not page aligned\n");
  printf("Virtual memory mapped at %p\n", virtbase);
  page_map = malloc(((sizeof(struct control_data_s) + 4096 - 1) >> 12) * sizeof(*page_map));
  if (page_map == 0)
    fatal("Failed to malloc page_map: %m\n");
  pid = getpid();
  sprintf(pagemap_fn, "/proc/%d/pagemap", pid);
  fd = open(pagemap_fn, 00);
  if (fd < 0)
    fatal("Failed to open %s: %m\n", pagemap_fn);
  if (lseek(fd, (unsigned long)virtbase >> 9, 0) != (unsigned long)virtbase >> 9)
    fatal("Failed to seek on %s: %m\n", pagemap_fn);
  for (i = 0; i < ((sizeof(struct control_data_s) + 4096 - 1) >> 12); i++) {
    unsigned long pfn;
    page_map[i].virtaddr = virtbase + i * 4096;
    page_map[i].virtaddr[0] = 0;
    if (read(fd, &pfn, sizeof(pfn)) != sizeof(pfn))
      fatal("Failed to read %s: %m\n", pagemap_fn);
    if ((pfn >> 55)&0xfbf != 0x10c)
      fatal("Page %d not present (pfn 0x%016llx)\n", i, pfn);
    page_map[i].physaddr = (unsigned int)pfn << 12 | 0x40000000;
  }
  gpio_reg[(0x00/4)] = (gpio_reg[(0x00/4)] & ~(7 << 12)) | (4 << 12);
  clk_reg[(0x70/4)] = 0x5A << 24 | 6;
  udelay(100);
  clk_reg[(0x70/4)] = 0x5A << 24 | 1 << 9 | 1 << 4 | 6;
  ctl = (struct control_data_s *)virtbase;
  struct dma_cb_t *cbp = ctl->cb;
  unsigned int phys_sample_dst = 0x7e101074;
  unsigned int phys_pwm_fifo_addr = 0x7e20c000 + 0x18;
  for (i = 0; i < 4000; i++) {
    ctl->sample[i] = 0x5a << 24 | freq_ctl;
    cbp->info = (1<<26) | (1<<3);
    cbp->src = mem_virt_to_phys(ctl->sample + i);
    cbp->dst = phys_sample_dst;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
    cbp->info = (1<<26) | (1<<3) | (1<<6) | ((5)<<16);
    cbp->src = mem_virt_to_phys(virtbase);
    cbp->dst = phys_pwm_fifo_addr;
    cbp->length = 4;
    cbp->stride = 0;
    cbp->next = mem_virt_to_phys(cbp + 1);
    cbp++;
  }
  cbp--;
  cbp->next = mem_virt_to_phys(virtbase);
  pwm_reg[(0x00/4)] = 0;
  udelay(10);
  clk_reg[40] = 0x5A000006;
  udelay(100);
  clk_reg[41] = 0x5A000000 | (5<<12);
  udelay(100);
  clk_reg[40] = 0x5A000016;
  udelay(100);
  pwm_reg[(0x10/4)] = 454;
  udelay(10);
  pwm_reg[(0x08/4)] = (1<<31) | ((15<<8)|(15<<0));
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<6);
  udelay(10);
  pwm_reg[(0x00/4)] = (1<<5) | (1<<0);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<31);
  udelay(10);
  dma_reg[(0x00/4)] = (1<<2) | (1<<1);
  dma_reg[(0x04/4)] = mem_virt_to_phys(ctl->cb);
  dma_reg[(0x20/4)] = 7;
  dma_reg[(0x00/4)] = 0x10880001;
  fd = 0;
  if (argc > 1) {
          fd = open(argv[1], 'r');
    if (fd < 0)
      fatal("Failed to open .wav file\n");
  }
        short data[1024];
        int data_len = read(fd, data, sizeof(data));
  if (data_len < 0)
    fatal("Failed to read .wav file\n");
  data_len /= 2;
  if (data_len < 23)
    fatal("Initial read of .wav file too short\n");
  unsigned int last_cb = (uint32_t)ctl->cb;
  int data_index = 22;
  for (;;) {
    usleep(10000);
    unsigned int cur_cb = mem_phys_to_virt(dma_reg[(0x04/4)]);
    int last_sample = (last_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int this_sample = (cur_cb - (uint32_t)virtbase) / (sizeof(dma_cb_t) * 2);
    int free_slots = this_sample - last_sample;
    if (free_slots < 0)
      free_slots += 4000;
    while (free_slots >= 10) {
      float dval = (float)(data[data_index])/65536.0 * 25.0;
      int intval = (int)((floor)(dval));
      int frac = (int)((dval - (float)intval) * 10.0);
      int j;
      for (j = 0; j < 10; j++) {
        ctl->sample[last_sample++] = (0x5A << 24 | freq_ctl) + (frac > j ? intval + 1 : intval);
        if (last_sample == 4000)
          last_sample = 0;
      }
      free_slots -= 10;
      if (++data_index >= data_len) {
              data_len = read(fd, data, sizeof(data));
        data_index = 0;
        if (data_len < 0)
          fatal("Error reading data: %m\n");
        data_len /= 2;
        if (data_len == 0)
          terminate(0);
      }
    }
    last_cb = (unsigned int)virtbase + last_sample * sizeof(dma_cb_t) * 2;
  }
  terminate(0);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: pid int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types:
pfn
unsigned long

variable_types: struct dma_cb_t { unsigned int info; } *

variable_types:
data
short [1024]

variable_types: dval float

variable_types: intval int

variable_types: frac int

variable_types: j int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: coobird/pigeon-vm

decompiled_code:
void __fastcall place_entry(__int64 a1, int a2, __int64 a3, int a4)
{
__int64 v4; // rcx

v4 = a3 + 16LL * a4;
*(_QWORD *)v4 = a1;
*(_DWORD *)(v4 + 8) = a2;
}

original_code:
void place_entry(char* varname, int address, ENTRY* entries, int entry_num) {
 entries[entry_num].varname = varname;
 entries[entry_num].value = address;
}

canonical_original_code:
void place_entry(char *varname, int address, struct ENTRY *entries, int entry_num) {
 entries[entry_num].varname = varname;
 entries[entry_num].value = address;
}

variable_types:
varname
char *

variable_types: address int

variable_types: struct ENTRY { char *varname; int value; } *


decompiled_code:
__int64 __fastcall get_entry(char *s1, __int64 a2, int a3)
{
__int64 v3; // rbx
__int64 v4; // r13

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v3 = a2;
v4 = a2 + 16LL * (unsigned int)(a3 - 1) + 16;
while ( strcmp(s1, *(const char **)v3) )
{
v3 += 16LL;
if ( v3 == v4 )
return 0xFFFFFFFFLL;
}
return *(unsigned int *)(v3 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1, __int64 a2, int a3)
{
__int64 v3; // rbx
__int64 v4; // r13

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v3 = a2;
v4 = a2 + 16LL * (unsigned int)(a3 - 1) + 16;
while ( strcmp(s1, *(const char **)v3) )
{
v3 += 16LL;
if ( v3 == v4 )
return 0xFFFFFFFFLL;
}
return *(unsigned int *)(v3 + 8);
}

original_code:
int get_entry(char* varname, ENTRY* entries, int entry_size) {
 int i = 0;
 for (i = 0; i < entry_size; i++) {
  if (strcmp(varname, entries[i].varname) == 0) {
   return entries[i].value;
  }
 }
 return -1;
}

canonical_original_code:
int get_entry(char *varname, struct ENTRY *entries, int entry_size) {
 int i = 0;
 for (i = 0; i < entry_size; i++) {
  if (strcmp(varname, entries[i].varname) == 0) {
   return entries[i].value;
  }
 }
 return -1;
}

variable_types: i int

user_defined_types: struct ENTRY { char *varname; int value; }


parse

decompiled_code:
int __fastcall parse(unsigned __int8 *a1, __int64 a2)
{
unsigned __int64 v3; // rax
__int64 v4; // rdx
int v5; // ebp
bool v6; // zf
int v7; // eax
int v8; // edx
char v9; // r12
_BYTE *v10; // rdi
__int64 v11; // rax
int v12; // edx
bool v13; // si
int v14; // ecx
char v16[16]; // [rsp+0h] [rbp-48h] BYREF
char s2[4]; // [rsp+10h] [rbp-38h] BYREF
char v18[24]; // [rsp+14h] [rbp-34h]
int v19; // [rsp+2Ch] [rbp-1Ch] BYREF

v3 = *a1;
if ( (unsigned __int8)v3 > 0x23u || (v4 = 0x800002400LL, !_bittest64(&v4, v3)) )
{
_isoc99_sscanf(a1, "%i\t%s\t%s", &v19, s2, v16);
if ( v16[0] == 36 )
v5 = get_entry(v16, (__int64)&entries, entry_size);
else
v5 = atoi(v16);
if ( memcmp("DATA", s2, 4uLL) )
{
v6 = strcmp("ADD", s2) == 0;
v7 = !v6;
if ( !v6 )
{
if ( !strcmp("SUB", s2) )
{
v7 = 1;
}
else if ( !strcmp("AND", s2) )
{
v7 = 2;
}
else if ( !strcmp("OR", s2) )
{
v7 = 3;
}
else if ( !strcmp("XOR", s2) )
{
v7 = 4;
}
else if ( !strcmp("NOT", s2) )
{
v7 = 5;
}
else if ( !strcmp("LDM", s2) )
{
v7 = 6;
}
else if ( !strcmp("LDI", s2) )
{
v7 = 7;
}
else if ( !strcmp("STR", s2) )
{
v7 = 8;
}
else if ( !strcmp("JMP", s2) )
{
v7 = 9;
}
else if ( !strcmp("JMZ", s2) )
{
v7 = 10;
}
else if ( !strcmp("JMN", s2) )
{
v7 = 11;
}
else if ( !strcmp("CALL", s2) )
{
v7 = 12;
}
else if ( !strcmp("RET", s2) )
{
v7 = 13;
}
else if ( !strcmp("PUSH", s2) )
{
v7 = 14;
}
else
{
v8 = strcmp("POP", s2);
v7 = 15;
if ( v8 )
{
LODWORD(v3) = fprintf(stderr, "Unknown opcode: %s\n", s2);
return v3;
}
}
}
LODWORD(v3) = v5 + (v7 << 12);
*(_WORD *)(a2 + 2LL * v19 + 40) = v3;
return v3;
}
v9 = v18[0];
if ( v18[0] == 36 )
{
v10 = malloc(0xCuLL);
v11 = 1LL;
do
{
v10[v11 - 1] = v9;
v12 = v11;
v9 = v18[v11];
v13 = (int)v11++ > 12;
}
while ( v9 != 0 || v13 );
v10[v12] = 0;
v14 = entry_size++;
place_entry((__int64)v10, v19, (__int64)&entries, v14);
}
LODWORD(v3) = v19;
*(_WORD *)(a2 + 2LL * v19 + 40) = v5;
}
return v3;
}

original_code:
void parse(char* line, STATE* state) {
 if (line[0] == '#' || line[0] == '\r' || line[0] == '\n') {
  return;
 }
 int address;
 char opcode[16];
 char value_buffer[16];
 int value;
 sscanf(line, "%i\t%s\t%s", &address, opcode, value_buffer);
 if (value_buffer[0] == '$') {
  value = get_entry(value_buffer, entries, entry_size);
 } else {
  value = atoi(value_buffer);
 }
 int opcode_value;
 if (strncmp("DATA", opcode, 4) == 0) {
  if (opcode[4] == '$') {
   char* varname = malloc(12);
   int i = 0;
   for (i = 0; opcode[i + 4] != '\0' || i > 12; i++) {
    varname[i] = opcode[i + 4];
   }
   varname[i] = '\0';
   place_entry(varname, address, entries, entry_size++);
  }
  state->mem[address] = value;
  return;
 }
 else if (strcmp("ADD", opcode) == 0)
  opcode_value = 0;
 else if (strcmp("SUB", opcode) == 0)
  opcode_value = 1;
 else if (strcmp("AND", opcode) == 0)
  opcode_value = 2;
 else if (strcmp("OR", opcode) == 0)
  opcode_value = 3;
 else if (strcmp("XOR", opcode) == 0)
  opcode_value = 4;
 else if (strcmp("NOT", opcode) == 0)
  opcode_value = 5;
 else if (strcmp("LDM", opcode) == 0)
  opcode_value = 6;
 else if (strcmp("LDI", opcode) == 0)
  opcode_value = 7;
 else if (strcmp("STR", opcode) == 0)
  opcode_value = 8;
 else if (strcmp("JMP", opcode) == 0)
  opcode_value = 9;
 else if (strcmp("JMZ", opcode) == 0)
  opcode_value = 10;
 else if (strcmp("JMN", opcode) == 0)
  opcode_value = 11;
 else if (strcmp("CALL", opcode) == 0)
  opcode_value = 12;
 else if (strcmp("RET", opcode) == 0)
  opcode_value = 13;
 else if (strcmp("PUSH", opcode) == 0)
  opcode_value = 14;
 else if (strcmp("POP", opcode) == 0)
  opcode_value = 15;
 else {
  fprintf(stderr, "Unknown opcode: %s\n", opcode);
  return;
 }
 state->mem[address] = (opcode_value << 12) + value;
}

canonical_original_code:
void parse(char *line, struct state *state) {
 if (line[0] == '#' || line[0] == '\r' || line[0] == '\n') {
  return;
 }
 int address;
 char opcode[16];
 char value_buffer[16];
 int value;
 sscanf(line, "%i\t%s\t%s", &address, opcode, value_buffer);
 if (value_buffer[0] == '$') {
  value = get_entry(value_buffer, entries, entry_size);
 } else {
  value = atoi(value_buffer);
 }
 int opcode_value;
 if (strncmp("DATA", opcode, 4) == 0) {
  if (opcode[4] == '$') {
   char *varname = malloc(12);
   int i = 0;
   for (i = 0; opcode[i + 4] != '\0' || i > 12; i++) {
    varname[i] = opcode[i + 4];
   }
   varname[i] = '\0';
   place_entry(varname, address, entries, entry_size++);
  }
  state->mem[address] = value;
  return;
 }
 else if (strcmp("ADD", opcode) == 0)
  opcode_value = 0;
 else if (strcmp("SUB", opcode) == 0)
  opcode_value = 1;
 else if (strcmp("AND", opcode) == 0)
  opcode_value = 2;
 else if (strcmp("OR", opcode) == 0)
  opcode_value = 3;
 else if (strcmp("XOR", opcode) == 0)
  opcode_value = 4;
 else if (strcmp("NOT", opcode) == 0)
  opcode_value = 5;
 else if (strcmp("LDM", opcode) == 0)
  opcode_value = 6;
 else if (strcmp("LDI", opcode) == 0)
  opcode_value = 7;
 else if (strcmp("STR", opcode) == 0)
  opcode_value = 8;
 else if (strcmp("JMP", opcode) == 0)
  opcode_value = 9;
 else if (strcmp("JMZ", opcode) == 0)
  opcode_value = 10;
 else if (strcmp("JMN", opcode) == 0)
  opcode_value = 11;
 else if (strcmp("CALL", opcode) == 0)
  opcode_value = 12;
 else if (strcmp("RET", opcode) == 0)
  opcode_value = 13;
 else if (strcmp("PUSH", opcode) == 0)
  opcode_value = 14;
 else if (strcmp("POP", opcode) == 0)
  opcode_value = 15;
 else {
  fprintf(stderr, "Unknown opcode: %s\n", opcode);
  return;
 }
 state->mem[address] = (opcode_value << 12) + value;
}

variable_types:
line
char *

variable_types: struct state { unsigned short opcode; unsigned short operand; short acc; unsigned short pc; unsigned short cstack[8]; unsigned short dstack[8]; unsigned char csp; unsigned char dsp; short mem[4096]; } *

variable_types:
opcode
char [16]

variable_types: value int


decompiled_code:
void output_state_quiet()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void output_state_quiet(STATE* state) {
}

canonical_original_code:
void output_state_quiet(struct state *state) {
}

user_defined_types: struct state { unsigned short opcode; unsigned short operand; short acc; unsigned short pc; unsigned short cstack[8]; unsigned short dstack[8]; unsigned char csp; unsigned char dsp; short mem[4096]; }


decompiled_code:
int __fastcall output_state_json(__int64 a1)
{
char v2[13]; // [rsp+Bh] [rbp-Dh] BYREF

get_mnemonic(*(_BYTE *)a1 & 0xF, v2);
return printf(
"{\"pc\":%i,\"opcode\":\"%s\",\"operand\":%i,\"acc\":%i,\"csp\":%i,\"dsp\":%i}\n",
*(unsigned __int16 *)(a1 + 4),
v2,
*(_WORD *)a1 >> 4,
(unsigned int)*(__int16 *)(a1 + 2),
*(unsigned __int8 *)(a1 + 38),
*(unsigned __int8 *)(a1 + 39));
}

original_code:
void output_state_json(STATE* state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("{\"pc\":%i,\"opcode\":\"%s\",\"operand\":%i,\"acc\":%i,\"csp\":%i,\"dsp\":%i}\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}

canonical_original_code:
void output_state_json(struct state *state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("{\"pc\":%i,\"opcode\":\"%s\",\"operand\":%i,\"acc\":%i,\"csp\":%i,\"dsp\":%i}\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}

variable_types:
opcode
char [5]


decompiled_code:
int __fastcall output_state_text(__int64 a1)
{
char v2[13]; // [rsp+Bh] [rbp-Dh] BYREF

get_mnemonic(*(_BYTE *)a1 & 0xF, v2);
return printf(
"pc: %4i opcode: %s operand: %4i acc: %5i csp: %2i dsp: %2i\n",
*(unsigned __int16 *)(a1 + 4),
v2,
*(_WORD *)a1 >> 4,
(unsigned int)*(__int16 *)(a1 + 2),
*(unsigned __int8 *)(a1 + 38),
*(unsigned __int8 *)(a1 + 39));
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
char v2[13]; // [rsp+Bh] [rbp-Dh] BYREF

get_mnemonic(*(_BYTE *)a1 & 0xF, v2);
return printf(
"pc: %4i opcode: %s operand: %4i acc: %5i csp: %2i dsp: %2i\n",
*(unsigned __int16 *)(a1 + 4),
v2,
*(_WORD *)a1 >> 4,
(unsigned int)*(__int16 *)(a1 + 2),
*(unsigned __int8 *)(a1 + 38),
*(unsigned __int8 *)(a1 + 39));
}

original_code:
void output_state_text(STATE* state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("pc: %4i  opcode: %s operand: %4i  acc: %5i  csp: %2i  dsp: %2i\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}

canonical_original_code:
void output_state_text(struct state *state) {
 char opcode[5];
 get_mnemonic(state->opcode, opcode);
 printf("pc: %4i  opcode: %s operand: %4i  acc: %5i  csp: %2i  dsp: %2i\n",
  state->pc, opcode, state->operand, state->acc, state->csp, state->dsp);
}


decompiled_code:
int __fastcall print_usage(const char *a1)
{
printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", a1);
puts("Options:");
puts(" -h Displays this help");
puts(" -t Outputs execution states in text format (default)");
puts(" -j Outputs execution states in JSON format");
puts(" -q Suppress execution state output");
puts(" -o Outputs the final accumulator state");
puts(" -v Verbose output");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", a1);
puts("Options:");
puts(" -h Displays this help");
puts(" -t Outputs execution states in text format (default)");
puts(" -j Outputs execution states in JSON format");
puts(" -q Suppress execution state output");
puts(" -o Outputs the final accumulator state");
puts(" -v Verbose output");
return putchar(10);
}

original_code:
void print_usage(char* command) {
 printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", command);
 printf("Options:\n");
 printf("    -h    Displays this help\n");
 printf("    -t    Outputs execution states in text format (default)\n");
 printf("    -j    Outputs execution states in JSON format\n");
 printf("    -q    Suppress execution state output\n");
 printf("    -o    Outputs the final accumulator state\n");
 printf("    -v    Verbose output\n");
 printf("\n");
}

canonical_original_code:
void print_usage(char *command) {
 printf("Usage: %s [-v] [-t|-j|-q] [-o] [-h]\n", command);
 printf("Options:\n");
 printf("    -h    Displays this help\n");
 printf("    -t    Outputs execution states in text format (default)\n");
 printf("    -j    Outputs execution states in JSON format\n");
 printf("    -q    Suppress execution state output\n");
 printf("    -o    Outputs the final accumulator state\n");
 printf("    -v    Verbose output\n");
 printf("\n");
}

variable_types:
command
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
int v5; // ebx
void *v6; // rbx
int v8; // [rsp+4h] [rbp-2074h]
int v9; // [rsp+8h] [rbp-2070h]
unsigned int v10; // [rsp+Ch] [rbp-206Ch]
char v11[2]; // [rsp+10h] [rbp-2068h] BYREF
__int16 v12; // [rsp+12h] [rbp-2066h]

v3 = 0;
v4 = 0;
v9 = 0;
v8 = 0;
v10 = 0;
while ( 2 )
{
v5 = getopt(argc, (char *const *)argv, "ovqjth");
if ( v5 == -1 )
{
if ( v3 > 1 )
{
fwrite("Can only speficy one of -j, -t, or -q.\n", 1uLL, 0x27uLL, stderr);
v4 = -1;
}
else
{
v6 = output_state_text;
if ( v8 )
v6 = output_state_json;
if ( v9 )
v6 = output_state_quiet;
if ( v10 )
{
puts("Initializing VM state.");
init_vm(v11);
puts("Loading instructions from stdin.");
load_memory(stdin, v11, v10);
puts("Beginning execution.");
run(v11, v6);
puts("Finished execution.");
}
else
{
init_vm(v11);
load_memory(stdin, v11, 0LL);
run(v11, v6);
}
if ( v4 )
{
printf("%i\n", (unsigned int)v12);
v4 = 0;
}
}
}
else
{
switch ( v5 )
{
case 'h':
print_usage(*argv);
v4 = 0;
break;
case 'j':
++v3;
v8 = 1;
continue;
case 'o':
v4 = 1;
continue;
case 'q':
++v3;
v9 = 1;
continue;
case 't':
++v3;
continue;
case 'v':
v10 = 1;
continue;
default:
v4 = -1;
break;
}
}
break;
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
int v5; // ebx
void *v6; // rbx
int v8; // [rsp+4h] [rbp-2074h]
int v9; // [rsp+8h] [rbp-2070h]
unsigned int v10; // [rsp+Ch] [rbp-206Ch]
char v11[2]; // [rsp+10h] [rbp-2068h] BYREF
__int16 v12; // [rsp+12h] [rbp-2066h]

v3 = 0;
v4 = 0;
v9 = 0;
v8 = 0;
v10 = 0;
while ( 2 )
{
v5 = getopt(argc, (char *const *)argv, "ovqjth");
if ( v5 == -1 )
{
if ( v3 > 1 )
{
fwrite("Can only speficy one of -j, -t, or -q.\n", 1uLL, 0x27uLL, stderr);
v4 = -1;
}
else
{
v6 = output_state_text;
if ( v8 )
v6 = output_state_json;
if ( v9 )
v6 = output_state_quiet;
if ( v10 )
{
puts("Initializing VM state.");
init_vm(v11);
puts("Loading instructions from stdin.");
load_memory(stdin, v11, v10);
puts("Beginning execution.");
run(v11, v6);
puts("Finished execution.");
}
else
{
init_vm(v11);
load_memory(stdin, v11, 0LL);
run(v11, v6);
}
if ( v4 )
{
printf("%i\n", (unsigned int)v12);
v4 = 0;
}
}
}
else
{
switch ( v5 )
{
case 'h':
func3(*argv);
v4 = 0;
break;
case 'j':
++v3;
v8 = 1;
continue;
case 'o':
v4 = 1;
continue;
case 'q':
++v3;
v9 = 1;
continue;
case 't':
++v3;
continue;
case 'v':
v10 = 1;
continue;
default:
v4 = -1;
break;
}
}
break;
}
return v4;
}

original_code:
int main(int argc, char** argv) {
 void* callback = &output_state_text;
 int f_verbose = 0, f_json = 0, f_text = 0, f_quiet = 0, f_output = 0;
 int cnt_outflags = 0;
 int opt;
 while ((opt = getopt(argc, argv, "ovqjth")) != -1) {
  switch (opt) {
   case 'v':
    f_verbose = 1;
    break;
   case 'o':
    f_output = 1;
    break;
   case 'q':
    f_quiet = 1;
    cnt_outflags++;
    break;
   case 'j':
    f_json = 1;
    cnt_outflags++;
    break;
   case 't':
    f_text = 1;
    cnt_outflags++;
    break;
   case 'h':
    print_usage(argv[0]);
    return 0;
   default:
    return -1;
    ;
  }
 }
 if (cnt_outflags > 1) {
  fprintf(stderr, "Can only speficy one of -j, -t, or -q.\n");
  return -1;
 }
 if (f_json) callback = &output_state_json;
 if (f_quiet) callback = &output_state_quiet;
 STATE state;
 if (f_verbose) printf("Initializing VM state.\n");
 init_vm(&state);
 if (f_verbose) printf("Loading instructions from stdin.\n");
 load_memory(stdin, &state, f_verbose);
 if (f_verbose) printf("Beginning execution.\n");
 run(&state, callback);
 if (f_verbose) printf("Finished execution.\n");
 if (f_output) printf("%i\n", state.acc);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 void *callback = &output_state_text;
 int f_verbose = 0; int f_json = 0; int f_text = 0; int f_quiet = 0; int f_output = 0;
 int cnt_outflags = 0;
 int opt;
 while ((opt = getopt(argc, argv, "ovqjth")) != -1) {
  switch (opt) {
   case 'v':
    f_verbose = 1;
    break;
   case 'o':
    f_output = 1;
    break;
   case 'q':
    f_quiet = 1;
    cnt_outflags++;
    break;
   case 'j':
    f_json = 1;
    cnt_outflags++;
    break;
   case 't':
    f_text = 1;
    cnt_outflags++;
    break;
   case 'h':
    print_usage(argv[0]);
    return 0;
   default:
    return -1;
    ;
  }
 }
 if (cnt_outflags > 1) {
  fprintf(stderr, "Can only speficy one of -j, -t, or -q.\n");
  return -1;
 }
 if (f_json) callback = &output_state_json;
 if (f_quiet) callback = &output_state_quiet;
 struct state state;
 if (f_verbose) printf("Initializing VM state.\n");
 init_vm(&state);
 if (f_verbose) printf("Loading instructions from stdin.\n");
 load_memory(stdin, &state, f_verbose);
 if (f_verbose) printf("Beginning execution.\n");
 run(&state, callback);
 if (f_verbose) printf("Finished execution.\n");
 if (f_output) printf("%i\n", state.acc);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
callback
void *

variable_types: opt int

variable_types: struct state { unsigned short opcode; unsigned short operand; short acc; unsigned short pc; unsigned short cstack[8]; unsigned short dstack[8]; unsigned char csp; unsigned char dsp; short mem[4096]; }


fetch

original_code:
void fetch(STATE* state) {
 unsigned short instr = state->mem[state->pc];
 state->opcode = (instr & 0xF000) >> 12;
 state->operand = instr & 0x0FFF;
}

canonical_original_code:
void fetch(struct state *state) {
 unsigned short instr = state->mem[state->pc];
 state->opcode = (instr & 0xF000) >> 12;
 state->operand = instr & 0x0FFF;
}

variable_types:
instr
unsigned short


execute

original_code:
void execute(STATE* state) {
 unsigned short opcode = state->opcode;
 unsigned short operand = state->operand;
 unsigned short value = state->mem[operand];
 switch (opcode) {
  case 0:
   state->acc += value;
   (state->pc)++;
   break;
  case 1:
   state->acc -= value;
   (state->pc)++;
   break;
  case 2:
   state->acc &= value;
   (state->pc)++;
   break;
  case 3:
   state->acc |= value;
   (state->pc)++;
   break;
  case 4:
   state->acc ^= value;
   (state->pc)++;
   break;
  case 5:
   state->acc = ~state->acc;
   (state->pc)++;
   break;
  case 6:
   state->acc = value;
   (state->pc)++;
   break;
  case 7:
   state->acc = operand;
   (state->pc)++;
   break;
  case 8:
   state->mem[operand] = state->acc;
   (state->pc)++;
   break;
  case 9:
   state->pc = operand;
   break;
  case 10:
   if (state->acc == 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 11:
   if (state->acc < 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 12:
   state->cstack[state->csp] = ++(state->pc);
   (state->csp)++;
   state->pc = operand;
   break;
  case 13:
   state->pc = state->cstack[--(state->csp)];
   break;
  case 14:
   state->dstack[(state->dsp)++] = state->acc;
   (state->pc)++;
   break;
  case 15:
   state->acc = state->dstack[--(state->dsp)];
   (state->pc)++;
   break;
 }
}

canonical_original_code:
void execute(struct state *state) {
 unsigned short opcode = state->opcode;
 unsigned short operand = state->operand;
 unsigned short value = state->mem[operand];
 switch (opcode) {
  case 0:
   state->acc += value;
   (state->pc)++;
   break;
  case 1:
   state->acc -= value;
   (state->pc)++;
   break;
  case 2:
   state->acc &= value;
   (state->pc)++;
   break;
  case 3:
   state->acc |= value;
   (state->pc)++;
   break;
  case 4:
   state->acc ^= value;
   (state->pc)++;
   break;
  case 5:
   state->acc = ~state->acc;
   (state->pc)++;
   break;
  case 6:
   state->acc = value;
   (state->pc)++;
   break;
  case 7:
   state->acc = operand;
   (state->pc)++;
   break;
  case 8:
   state->mem[operand] = state->acc;
   (state->pc)++;
   break;
  case 9:
   state->pc = operand;
   break;
  case 10:
   if (state->acc == 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 11:
   if (state->acc < 0)
    state->pc = operand;
   else
    (state->pc)++;
   break;
  case 12:
   state->cstack[state->csp] = ++(state->pc);
   (state->csp)++;
   state->pc = operand;
   break;
  case 13:
   state->pc = state->cstack[--(state->csp)];
   break;
  case 14:
   state->dstack[(state->dsp)++] = state->acc;
   (state->pc)++;
   break;
  case 15:
   state->acc = state->dstack[--(state->dsp)];
   (state->pc)++;
   break;
 }
}

variable_types:
opcode
unsigned short

variable_types:
operand
unsigned short

variable_types:
value
unsigned short


original_code:
void get_mnemonic(unsigned short opcode, char mnemonic[5]) {
 if (opcode == 0)
  strcpy(mnemonic, "ADD");
 else if (opcode == 1)
  strcpy(mnemonic, "SUB");
 else if (opcode == 2)
  strcpy(mnemonic, "AND");
 else if (opcode == 3)
  strcpy(mnemonic, "OR");
 else if (opcode == 4)
  strcpy(mnemonic, "XOR");
 else if (opcode == 5)
  strcpy(mnemonic, "NOT");
 else if (opcode == 6)
  strcpy(mnemonic, "LDM");
 else if (opcode == 7)
  strcpy(mnemonic, "LDI");
 else if (opcode == 8)
  strcpy(mnemonic, "STR");
 else if (opcode == 9)
  strcpy(mnemonic, "JMP");
 else if (opcode == 10)
  strcpy(mnemonic, "JMZ");
 else if (opcode == 11)
  strcpy(mnemonic, "JMN");
 else if (opcode == 12)
  strcpy(mnemonic, "CALL");
 else if (opcode == 13)
  strcpy(mnemonic, "RET");
 else if (opcode == 14)
  strcpy(mnemonic, "PUSH");
 else if (opcode == 15)
  strcpy(mnemonic, "POP");
}

variable_types:
mnemonic
char [5]


run

decompiled_code:
__int64 __fastcall run(unsigned __int16 *a1, void (__fastcall *a2)(unsigned __int16 *))
{
int v2; // ebp

if ( a1[2] <= 0xFFEu )
{
v2 = 0;
do
{
++v2;
fetch(a1);
a2(a1);
execute((__int64)a1);
}
while ( a1[2] <= 0xFFEu && v2 <= 499 );
}
return ((__int64 (__fastcall *)(unsigned __int16 *))a2)(a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 *a1, void (__fastcall *a2)(unsigned __int16 *))
{
int v2; // ebp

if ( a1[2] <= 0xFFEu )
{
v2 = 0;
do
{
++v2;
func0(a1);
a2(a1);
func1((__int64)a1);
}
while ( a1[2] <= 0xFFEu && v2 <= 499 );
}
return ((__int64 (__fastcall *)(unsigned __int16 *))a2)(a1);
}

original_code:
void run(STATE* state, void (*callback)(STATE*)) {
 int instruction_count = 0;
 const int MAX_INST_COUNT = 500;
 while (state->pc < 4096 - 1 && instruction_count < MAX_INST_COUNT) {
  instruction_count++;
  fetch(state);
  callback(state);
  execute(state);
 }
 callback(state);
}

canonical_original_code:
void run(struct state *state, void (*callback)(struct state *)) {
 int instruction_count = 0;
 int MAX_INST_COUNT = 500;
 while (state->pc < 4096 - 1 && instruction_count < MAX_INST_COUNT) {
  instruction_count++;
  fetch(state);
  callback(state);
  execute(state);
 }
 callback(state);
}

variable_types:
callback
void (*)(struct state *)


repo: cooljc/rpi-eve

decompiled_code:
__int64 __fastcall tmp10x_read_temp(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
unsigned __int16 v3; // bp
unsigned __int16 v5[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v1 = ioctl(a1, 0x80022301uLL, v5);
v2 = v1;
if ( v1 )
{
fprintf(stderr, "Error: ioctl(TMP10X_IOC_GETTEMP) failed (err=%d)\n", v1);
}
else
{
v3 = v5[0];
fprintf(stderr, "temp.m_temp = 0x%02x -> ", v5[0]);
fprintf(stderr, "Temperature: %d.%04d C\n", v3 >> 4, 625 * (v3 & 0xFu));
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
unsigned __int16 v3; // bp
unsigned __int16 v5[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v1 = ioctl(a1, 0x80022301uLL, v5);
v2 = v1;
if ( v1 )
{
fprintf(stderr, "Error: ioctl(TMP10X_IOC_GETTEMP) failed (err=%d)\n", v1);
}
else
{
v3 = v5[0];
fprintf(stderr, "temp.m_temp = 0x%02x -> ", v5[0]);
fprintf(stderr, "Temperature: %d.%04d C\n", v3 >> 4, 625 * (v3 & 0xFu));
}
return v2;
}

original_code:
int tmp10x_read_temp (int fd)
{
  int ret = -1;
  tmp10x_temp_t temp;
  ret = ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((0x23)) << (0 +8)) | (((1)) << 0) | ((((sizeof(tmp10x_temp_t)))) << ((0 +8)+8))), &temp);
  if (ret == 0) {
    int tmp1 = (temp.m_temp >> 4);
    int tmp2 = (temp.m_temp & 0x0f) * 625;
    fprintf (stderr, "temp.m_temp   = 0x%02x -> ", temp.m_temp);
    fprintf (stderr, "Temperature: %d.%04d C\n", tmp1, tmp2);
  }
  else {
    fprintf (stderr, "Error: ioctl(TMP10X_IOC_GETTEMP) failed (err=%d)\n", ret);
  }
  return ret;
}

canonical_original_code:
int tmp10x_read_temp(int fd)
{
  int ret = -1;
  struct _tmp10x_temp_s temp;
  ret = ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((0x23)) << (0 +8)) | (((1)) << 0) | ((((sizeof(tmp10x_temp_t)))) << ((0 +8)+8))), &temp);
  if (ret == 0) {
    int tmp1 = (temp.m_temp >> 4);
    int tmp2 = (temp.m_temp & 0x0f) * 625;
    fprintf (stderr, "temp.m_temp   = 0x%02x -> ", temp.m_temp);
    fprintf (stderr, "Temperature: %d.%04d C\n", tmp1, tmp2);
  }
  else {
    fprintf (stderr, "Error: ioctl(TMP10X_IOC_GETTEMP) failed (err=%d)\n", ret);
  }
  return ret;
}

variable_types: fd int

variable_types: ret int

variable_types: struct _tmp10x_temp_s { unsigned short m_temp; }


decompiled_code:
__int64 __fastcall tmp10x_read_config(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
char v4[14]; // [rsp+Ah] [rbp-Eh] BYREF

v1 = ioctl(a1, 0x80062302uLL, v4);
v2 = v1;
if ( v1 )
{
fprintf(stderr, "Error: ioctl(TMP10X_IOC_GETCONFIG) failed (err=%d)\n", v1);
}
else
{
fprintf(stderr, "c.m_sd = 0x%02x\n", (unsigned __int8)v4[0]);
fprintf(stderr, "c.m_tm = 0x%02x\n", (unsigned __int8)v4[1]);
fprintf(stderr, "c.m_pol = 0x%02x\n", (unsigned __int8)v4[2]);
fprintf(stderr, "c.m_fq = 0x%02x\n", (unsigned __int8)v4[3]);
fprintf(stderr, "c.m_cr = 0x%02x\n", (unsigned __int8)v4[4]);
fprintf(stderr, "c.m_os = 0x%02x\n", (unsigned __int8)v4[5]);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
char v4[14]; // [rsp+Ah] [rbp-Eh] BYREF

v1 = ioctl(a1, 0x80062302uLL, v4);
v2 = v1;
if ( v1 )
{
fprintf(stderr, "Error: ioctl(TMP10X_IOC_GETCONFIG) failed (err=%d)\n", v1);
}
else
{
fprintf(stderr, "c.m_sd = 0x%02x\n", (unsigned __int8)v4[0]);
fprintf(stderr, "c.m_tm = 0x%02x\n", (unsigned __int8)v4[1]);
fprintf(stderr, "c.m_pol = 0x%02x\n", (unsigned __int8)v4[2]);
fprintf(stderr, "c.m_fq = 0x%02x\n", (unsigned __int8)v4[3]);
fprintf(stderr, "c.m_cr = 0x%02x\n", (unsigned __int8)v4[4]);
fprintf(stderr, "c.m_os = 0x%02x\n", (unsigned __int8)v4[5]);
}
return v2;
}

original_code:
int tmp10x_read_config (int fd)
{
  int ret = -1;
  tmp10x_config_t c;
  ret = ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((0x23)) << (0 +8)) | (((2)) << 0) | ((((sizeof(tmp10x_config_t)))) << ((0 +8)+8))), &c);
  if (ret == 0) {
    fprintf (stderr, "c.m_sd   = 0x%02x\n", c.m_sd);
    fprintf (stderr, "c.m_tm   = 0x%02x\n", c.m_tm);
    fprintf (stderr, "c.m_pol  = 0x%02x\n", c.m_pol);
    fprintf (stderr, "c.m_fq   = 0x%02x\n", c.m_fq);
    fprintf (stderr, "c.m_cr   = 0x%02x\n", c.m_cr);
    fprintf (stderr, "c.m_os   = 0x%02x\n", c.m_os);
  }
  else {
    fprintf (stderr, "Error: ioctl(TMP10X_IOC_GETCONFIG) failed (err=%d)\n", ret);
  }
  return ret;
}

canonical_original_code:
int tmp10x_read_config(int fd)
{
  int ret = -1;
  struct _tmp10x_config_s c;
  ret = ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (((0x23)) << (0 +8)) | (((2)) << 0) | ((((sizeof(tmp10x_config_t)))) << ((0 +8)+8))), &c);
  if (ret == 0) {
    fprintf (stderr, "c.m_sd   = 0x%02x\n", c.m_sd);
    fprintf (stderr, "c.m_tm   = 0x%02x\n", c.m_tm);
    fprintf (stderr, "c.m_pol  = 0x%02x\n", c.m_pol);
    fprintf (stderr, "c.m_fq   = 0x%02x\n", c.m_fq);
    fprintf (stderr, "c.m_cr   = 0x%02x\n", c.m_cr);
    fprintf (stderr, "c.m_os   = 0x%02x\n", c.m_os);
  }
  else {
    fprintf (stderr, "Error: ioctl(TMP10X_IOC_GETCONFIG) failed (err=%d)\n", ret);
  }
  return ret;
}

variable_types: struct _tmp10x_config_s { unsigned char m_sd; unsigned char m_tm; unsigned char m_pol; unsigned char m_fq; unsigned char m_cr; unsigned char m_os; }


decompiled_code:
__int64 __fastcall tmp10x_write_config(int fd)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // ebx
char v5[30]; // [rsp+Ah] [rbp-1Eh] BYREF

v1 = ioctl(fd, 0x80062302uLL, v5);
if ( v1 )
{
v3 = v1;
fprintf(stderr, "Error: ioctl(TMP10X_IOC_GETCONFIG) failed (err=%d)\n", v1);
}
else
{
v5[4] = 3;
v2 = ioctl(fd, 0x80062303uLL, v5);
v3 = v2;
if ( v2 )
fprintf(stderr, "Error: ioctl(TMP10X_IOC_SETCONFIG) failed (err=%d)\n", v2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd)
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // ebx
char v5[30]; // [rsp+Ah] [rbp-1Eh] BYREF

v1 = ioctl(fd, 0x80062302uLL, v5);
if ( v1 )
{
v3 = v1;
fprintf(stderr, "Error: ioctl(TMP10X_IOC_GETCONFIG) failed (err=%d)\n", v1);
}
else
{
v5[4] = 3;
v2 = ioctl(fd, 0x80062303uLL, v5);
v3 = v2;
if ( v2 )
fprintf(stderr, "Error: ioctl(TMP10X_IOC_SETCONFIG) failed (err=%d)\n", v2);
}
return v3;
}

user_defined_types: struct _tmp10x_config_s { unsigned char m_sd; unsigned char m_tm; unsigned char m_pol; unsigned char m_fq; unsigned char m_cr; unsigned char m_os; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebx
int result; // eax

v3 = open("/dev/tmp10x", 2, envp);
if ( v3 < 0 )
{
fwrite("Error: Failed to open /dev/tmp10x!!\n", 1uLL, 0x24uLL, stderr);
result = 1;
}
else
{
v4 = v3;
tmp10x_read_config(v3);
tmp10x_write_config(v4);
tmp10x_read_config(v4);
tmp10x_read_temp(v4);
close(v4);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebx
int result; // eax

v3 = open("/dev/tmp10x", 2, envp);
if ( v3 < 0 )
{
fwrite("Error: Failed to open /dev/tmp10x!!\n", 1uLL, 0x24uLL, stderr);
result = 1;
}
else
{
v4 = v3;
func8(v3);
func9(v4);
func8(v4);
func7(v4);
close(v4);
result = 0;
}
return result;
}

original_code:
int main (int argc, char *argv[])
{
  int fd = -1;
  fd = open("/dev/tmp10x", 02);
  if (fd < 0) {
    fprintf (stderr, "Error: Failed to open /dev/tmp10x!!\n");
    return 1;
  }
  tmp10x_read_config (fd);
  tmp10x_write_config (fd);
  tmp10x_read_config (fd);
  tmp10x_read_temp (fd);
  close (fd);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int fd = -1;
  fd = open("/dev/tmp10x", 02);
  if (fd < 0) {
    fprintf (stderr, "Error: Failed to open /dev/tmp10x!!\n");
    return 1;
  }
  tmp10x_read_config (fd);
  tmp10x_write_config (fd);
  tmp10x_read_config (fd);
  tmp10x_read_temp (fd);
  close (fd);
  return 0;
}

variable_types:
argv
char *[]


reader

decompiled_code:
void *__fastcall reader(void *a1)
{
int v1; // ebx
void *result; // rax
size_t v3; // r12
char *v4; // rax
char *v5; // rbp
int v6; // eax
size_t v7; // r12
char *v8; // rax
char *v9; // rbp
int buf; // [rsp+8h] [rbp-20h] BYREF
int v11[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = *(_DWORD *)a1;
pthread_mutex_lock(&mutex);
if ( status )
{
while ( 1 )
{
pthread_mutex_unlock(&mutex);
v6 = read(v1, v11, 4uLL);
if ( !v6 )
break;
if ( v6 == -1 )
{
puts("Read failed.");
goto LABEL_4;
}
if ( v11[0] == 4 )
{
if ( !read(v1, &buf, 4uLL) )
break;
v3 = buf;
v4 = (char *)malloc(buf);
v5 = v4;
if ( status )
read(v1, v4, v3);
puts(v5);
free(v5);
}
else if ( v11[0] == 8 )
{
if ( !read(v1, &buf, 4uLL) )
break;
v7 = buf;
v8 = (char *)malloc(buf);
v9 = v8;
if ( status )
read(v1, v8, v7);
printf("User Name - %s\n", v9);
free(v9);
}
pthread_mutex_lock(&mutex);
if ( !status )
goto LABEL_4;
}
result = 0LL;
}
else
{
LABEL_4:
pthread_mutex_unlock(&mutex);
result = reader + 1;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
int v1; // ebx
void *result; // rax
size_t v3; // r12
char *v4; // rax
char *v5; // rbp
int v6; // eax
size_t v7; // r12
char *v8; // rax
char *v9; // rbp
int buf; // [rsp+8h] [rbp-20h] BYREF
int v11[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = *(_DWORD *)a1;
pthread_mutex_lock(&mutex);
if ( status )
{
while ( 1 )
{
pthread_mutex_unlock(&mutex);
v6 = read(v1, v11, 4uLL);
if ( !v6 )
break;
if ( v6 == -1 )
{
puts("Read failed.");
goto LABEL_4;
}
if ( v11[0] == 4 )
{
if ( !read(v1, &buf, 4uLL) )
break;
v3 = buf;
v4 = (char *)malloc(buf);
v5 = v4;
if ( status )
read(v1, v4, v3);
puts(v5);
free(v5);
}
else if ( v11[0] == 8 )
{
if ( !read(v1, &buf, 4uLL) )
break;
v7 = buf;
v8 = (char *)malloc(buf);
v9 = v8;
if ( status )
read(v1, v8, v7);
printf("User Name - %s\n", v9);
free(v9);
}
pthread_mutex_lock(&mutex);
if ( !status )
goto LABEL_4;
}
result = 0LL;
}
else
{
LABEL_4:
pthread_mutex_unlock(&mutex);
result = reader + 1;
}
return result;
}

original_code:
void * reader (void *arg)
{
 int client_socket = *(int *)arg;
 pthread_mutex_lock(&mutex);
 while (status) {
  pthread_mutex_unlock(&mutex);
  int length;
  char* text;
  int header;
  int ret;
  if (ret = read (client_socket, &header, sizeof(header)), ret == 0)
   return 0;
  else if(ret == -1)
  {
   printf("Read failed.\n");
   break;
  }
  else
  {
   switch(header)
   {
    case 0x04:
     if (read (client_socket, &length, sizeof(length)) == 0)
      return 0;
     text = (char*) malloc (length);
     if(status)
     {
      read (client_socket, text, length);
     }
     printf("%s\n",text);;
     free (text);
     break;
    case 0x08:
                  if (read (client_socket, &length, sizeof(length)) == 0)
                         return 0;
                     text = (char*) malloc (length);
                     if(status)
                     {
                        read (client_socket, text, length);
                     }
                     printf ("User Name - %s\n", text);
                     free (text);
                     break;
   }
  }
  pthread_mutex_lock(&mutex);
 }
 pthread_mutex_unlock(&mutex);
 return (void *)1;
}

canonical_original_code:
void *reader(void *arg)
{
 int client_socket = *(int *)arg;
 pthread_mutex_lock(&mutex);
 while (status) {
  pthread_mutex_unlock(&mutex);
  int length;
  char *text;
  int header;
  int ret;
  if (ret = read (client_socket, &header, sizeof(header)), ret == 0)
   return 0;
  else if(ret == -1)
  {
   printf("Read failed.\n");
   break;
  }
  else
  {
   switch(header)
   {
    case 0x04:
     if (read (client_socket, &length, sizeof(length)) == 0)
      return 0;
     text = (char *) malloc (length);
     if(status)
     {
      read (client_socket, text, length);
     }
     printf("%s\n",text);;
     free (text);
     break;
    case 0x08:
                  if (read (client_socket, &length, sizeof(length)) == 0)
                         return 0;
                     text = (char *) malloc (length);
                     if(status)
                     {
                        read (client_socket, text, length);
                     }
                     printf ("User Name - %s\n", text);
                     free (text);
                     break;
   }
  }
  pthread_mutex_lock(&mutex);
 }
 pthread_mutex_unlock(&mutex);
 return (void *)1;
}

variable_types:
arg
void *

variable_types: length int

variable_types:
text
char *

variable_types: header int


decompiled_code:
size_t __fastcall get_home_page(int fd)
{
size_t v1; // rax
size_t result; // rax
char v3[10008]; // [rsp+0h] [rbp-2718h] BYREF

strcpy(v3, "GET /\n");
v1 = strlen(v3);
write(fd, v3, v1);
while ( 1 )
{
result = read(fd, v3, 0x2710uLL);
if ( !result )
break;
fwrite(v3, 1uLL, result, stdout);
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func4(int fd)
{
size_t v1; // rax
size_t result; // rax
char v3[10008]; // [rsp+0h] [rbp-2718h] BYREF

strcpy(v3, "GET /\n");
v1 = strlen(v3);
write(fd, v3, v1);
while ( 1 )
{
result = read(fd, v3, 0x2710uLL);
if ( !result )
break;
fwrite(v3, 1uLL, result, stdout);
}
return result;
}

original_code:
void get_home_page (int socket_fd)
{
 char buffer[10000];
 ssize_t number_characters_read;
 sprintf (buffer, "GET /\n");
 write (socket_fd, buffer, strlen (buffer));
 while (1) {
  number_characters_read = read (socket_fd, buffer, 10000);
  if (number_characters_read == 0)
   return;
   fwrite (buffer, sizeof (char), number_characters_read, stdout);
 }
}

canonical_original_code:
void get_home_page(int socket_fd)
{
 char buffer[10000];
 long number_characters_read;
 sprintf (buffer, "GET /\n");
 write (socket_fd, buffer, strlen (buffer));
 while (1) {
  number_characters_read = read (socket_fd, buffer, 10000);
  if (number_characters_read == 0)
   return;
   fwrite (buffer, sizeof (char), number_characters_read, stdout);
 }
}

variable_types:
buffer
char [10000]


decompiled_code:
__int64 __fastcall send_text(__int64 a1, const char *a2, int a3)
{
int buf[4]; // [rsp+Ch] [rbp-1Ch] BYREF
int v5[3]; // [rsp+1Ch] [rbp-Ch] BYREF

buf[0] = a3;
v5[0] = strlen(a2) + 1;
write(1, buf, 4uLL);
write(1, v5, 4uLL);
write(1, a2, v5[0]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, const char *a2, int a3)
{
int buf[4]; // [rsp+Ch] [rbp-1Ch] BYREF
int v5[3]; // [rsp+1Ch] [rbp-Ch] BYREF

buf[0] = a3;
v5[0] = strlen(a2) + 1;
write(1, buf, 4uLL);
write(1, v5, 4uLL);
write(1, a2, v5[0]);
return 1LL;
}

original_code:
int send_text(int socket_fd,const char *text,int header)
{
 int length = strlen (text) + 1;
 write (1, &header, sizeof (header));
 write (1, &length, sizeof (length));
 write (1, text, length);
 return 1;
}

canonical_original_code:
int send_text(int socket_fd, char *text, int header)
{
 int length = strlen (text) + 1;
 write (1, &header, sizeof (header));
 write (1, &length, sizeof (length));
 write (1, text, length);
 return 1;
}


writer

decompiled_code:
__int64 __fastcall writer(void *a1)
{
unsigned int v1; // eax
unsigned int v3; // eax
__int64 *v4; // rbx
__int64 v5[2]; // [rsp+0h] [rbp-218h] BYREF
char v6[496]; // [rsp+10h] [rbp-208h] BYREF

v1 = fcntl(0, 3, 0LL);
BYTE1(v1) |= 8u;
fcntl(0, 4, v1);
v5[0] = 0x206873656E746152LL;
v5[1] = 58LL;
memset(v6, 0, sizeof(v6));
connect(*(_DWORD *)a1, (const struct sockaddr *)((char *)a1 + 4), 0x10u);
while ( 2 )
{
printf("Socket id : %d : msg : %s \n", *(unsigned int *)a1, (const char *)v5);
if ( (unsigned int)send_text(*(unsigned int *)a1, (const char *)v5, 4) == -1 )
{
perror("Write failed.\n");
return 1LL;
}
v4 = v5;
while ( 1 )
{
v3 = read(0, v4, 1uLL);
if ( v3 != -1 )
break;
LABEL_4:
if ( !status )
goto LABEL_8;
}
if ( *(_BYTE *)v4 != 10 )
{
v4 = (__int64 *)((char *)v4 + 1);
goto LABEL_4;
}
LABEL_8:
*(_BYTE *)v4 = 0;
printf("out of writer : %d\n", v3);
if ( status )
continue;
return 1LL;
}
}

canonical_decompiled_code:
__int64 __fastcall func6(void *a1)
{
unsigned int v1; // eax
unsigned int v3; // eax
__int64 *v4; // rbx
__int64 v5[2]; // [rsp+0h] [rbp-218h] BYREF
char v6[496]; // [rsp+10h] [rbp-208h] BYREF

v1 = fcntl(0, 3, 0LL);
BYTE1(v1) |= 8u;
fcntl(0, 4, v1);
v5[0] = 0x206873656E746152LL;
v5[1] = 58LL;
memset(v6, 0, sizeof(v6));
connect(*(_DWORD *)a1, (const struct sockaddr *)((char *)a1 + 4), 0x10u);
while ( 2 )
{
printf("Socket id : %d : msg : %s \n", *(unsigned int *)a1, (const char *)v5);
if ( (unsigned int)func5(*(unsigned int *)a1, (const char *)v5, 4) == -1 )
{
perror("Write failed.\n");
return 1LL;
}
v4 = v5;
while ( 1 )
{
v3 = read(0, v4, 1uLL);
if ( v3 != -1 )
break;
LABEL_4:
if ( !status )
goto LABEL_8;
}
if ( *(_BYTE *)v4 != 10 )
{
v4 = (__int64 *)((char *)v4 + 1);
goto LABEL_4;
}
LABEL_8:
*(_BYTE *)v4 = 0;
printf("out of writer : %d\n", v3);
if ( status )
continue;
return 1LL;
}
}

original_code:
void * writer (void *arg)
{
 packet *data = (packet *)arg;
 int ret;
 int flags = fcntl(0, 3, 0);
 fcntl(0, 4, flags | 04000);
    char message[512] = {"Ratnesh :"};
    char *pmsg;
 ret = connect (data->socket_fd,(struct sockaddr *) &data->client_name, sizeof(data->client_name));
    do{
  pmsg = message;
     printf("Socket id : %d : msg : %s \n",data->socket_fd,message);
     ret = send_text(data->socket_fd, message,0x04);
     if(ret == -1)
     {
      perror("Write failed.\n");
      break;
  }
        do{
         ret = read(0,pmsg,1);
            if(ret != -1)
            {
                if(*pmsg == '\n')
                    break;
                pmsg++;
            }
        }while( status );
  *pmsg = '\0';
  printf("out of writer : %d\n",ret);
 }while(status);
 return (void *)1;
}

canonical_original_code:
void *writer(void *arg)
{
 struct packet *data = (packet *)arg;
 int ret;
 int flags = fcntl(0, 3, 0);
 fcntl(0, 4, flags | 04000);
    char message[512] = {"Ratnesh :"};
    char *pmsg;
 ret = connect (data->socket_fd,(struct sockaddr *) &data->client_name, sizeof(data->client_name));
    do{
  pmsg = message;
     printf("Socket id : %d : msg : %s \n",data->socket_fd,message);
     ret = send_text(data->socket_fd, message,0x04);
     if(ret == -1)
     {
      perror("Write failed.\n");
      break;
  }
        do{
         ret = read(0,pmsg,1);
            if(ret != -1)
            {
                if(*pmsg == '\n')
                    break;
                pmsg++;
            }
        }while( status );
  *pmsg = '\0';
  printf("out of writer : %d\n",ret);
 }while(status);
 return (void *)1;
}

variable_types: struct packet { int socket_fd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } client_name; } *

variable_types: flags int

variable_types:
message
char [512]

variable_types:
pmsg
char *

user_defined_types: struct packet { int socket_fd; struct sockaddr client_name; }