decompiled_code:
_BOOL8 from_cu()
{
_BOOL8 result; // rax
struct stat v1; // [rsp+0h] [rbp-128h] BYREF
struct stat buf; // [rsp+90h] [rbp-98h] BYREF

buf.st_dev = 0LL;
v1.st_dev = 0LL;
buf.st_rdev = 0LL;
v1.st_rdev = 0LL;
fstat(1, &v1);
fstat(2, &buf);
result = 1LL;
if ( v1.st_rdev == buf.st_rdev )
result = v1.st_dev != buf.st_dev;
Fromcu = result;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
_BOOL8 result; // rax
struct stat v1; // [rsp+0h] [rbp-128h] BYREF
struct stat buf; // [rsp+90h] [rbp-98h] BYREF

buf.st_dev = 0LL;
v1.st_dev = 0LL;
buf.st_rdev = 0LL;
v1.st_rdev = 0LL;
fstat(1, &v1);
fstat(2, &buf);
result = 1LL;
if ( v1.st_rdev == buf.st_rdev )
result = v1.st_dev != buf.st_dev;
Fromcu = result;
return result;
}

original_code:
int
from_cu(void)
{
 struct stat a, b;
 int help=0;
 a.st_rdev=b.st_rdev=a.st_dev=b.st_dev=help;
 fstat(1, &a); fstat(2, &b);
 Fromcu = (a.st_rdev != b.st_rdev) || (a.st_dev != b.st_dev);
 return Fromcu;
}

canonical_original_code:
int from_cu()
{
 struct stat a; struct stat b;
 int help = 0;
 a.st_rdev=b.st_rdev=a.st_dev=b.st_dev=help;
 fstat(1, &a); fstat(2, &b);
 Fromcu = (a.st_rdev != b.st_rdev) || (a.st_dev != b.st_dev);
 return Fromcu;
}

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

variable_types: help int

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


rdchk

repo: tigerking/zmodem-android

decompiled_code:
__int64 __fastcall rdchk(int a1)
{
ioctl(a1, 0x541BuLL, &lf_1);
return (unsigned int)lf_1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
ioctl(a1, 0x541BuLL, &lf_1);
return (unsigned int)lf_1;
}

original_code:
int
rdchk(int fd)
{
 static long lf;
 ioctl(fd, 0x541B, &lf);
 return ((int) lf);
}

variable_types: fd int

variable_types: lf long

return_type: int


decompiled_code:
__int64 __fastcall io_mode(int fd, __int64 a2)
{
struct termios *v4; // rdi
struct termios *v5; // rsi
__int64 i; // rcx
__m128i v7; // xmm3
__m128i v8; // xmm4
__m128i v9; // xmm5
speed_t v10; // eax
tcflag_t v11; // eax
int v12; // esi
unsigned int v13; // eax
int v14; // eax
__m128i v15; // xmm0
__m128i v16; // xmm1
__m128i v17; // xmm2
speed_t v18; // eax
int v19; // er8
tcflag_t v20; // eax
bool v21; // zf
_BOOL4 v22; // eax
cc_t v23; // al
__m128i v24; // xmm6
__m128i v25; // xmm7
speed_t v26; // eax
__m128i v27; // xmm6

vfile("mode:%d", a2);
if ( (_DWORD)a2 != 2 )
{
if ( (int)a2 > 2 )
{
if ( (_DWORD)a2 == 3 )
{
if ( did0_0 )
{
v24 = _mm_load_si128((const __m128i *)&oldtty);
v25 = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
v26 = oldtty.c_ospeed;
*(__m128i *)&tty.c_iflag = v24;
v27 = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
tty.c_ospeed = v26;
*(__m128i *)&tty.c_line = v25;
*(__m128i *)&tty.c_cc[15] = v27;
goto LABEL_28;
}
LABEL_15:
did0_0 = 1;
tcgetattr(fd, &oldtty);
v7 = _mm_load_si128((const __m128i *)&oldtty);
v8 = _mm_load_si128((const __m128i *)&oldtty.c_line);
v9 = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
v10 = oldtty.c_ospeed;
*(__m128i *)&tty.c_iflag = v7;
tty.c_ospeed = v10;
*(__m128i *)&tty.c_line = v8;
*(__m128i *)&tty.c_cc[15] = v9;
if ( (_DWORD)a2 != 3 )
{
LABEL_16:
tty.c_iflag = 1;
LABEL_17:
v11 = tty.c_cflag;
v12 = Twostop;
tty.c_oflag = 0;
tty.c_lflag &= 0xFFFFFFF4;
v13 = v11 & 0xFFFFFECF;
tty.c_cflag = v13 | 0x30;
if ( v12 )
tty.c_cflag = v13 | 0x70;
*(_WORD *)&tty.c_cc[5] = 257;
tcsetattr(fd, 1, &tty);
v14 = cfgetospeed(&tty);
Baudrate = getspeed(v14);
return 0LL;
}
LABEL_28:
tty.c_iflag = 4097;
goto LABEL_17;
}
}
else
{
if ( !(_DWORD)a2 )
{
if ( did0_0 )
{
tcflush(fd, 2);
tcsetattr(fd, 1, &oldtty);
tcflow(fd, 1);
return 0LL;
}
return 0xFFFFFFFFLL;
}
if ( (_DWORD)a2 == 1 )
{
if ( did0_0 )
{
v4 = &tty;
v5 = &oldtty;
for ( i = 15LL; i; --i )
{
v4->c_iflag = v5->c_iflag;
v5 = (struct termios *)((char *)v5 + 4);
v4 = (struct termios *)((char *)v4 + 4);
}
goto LABEL_16;
}
goto LABEL_15;
}
}
return 0xFFFFFFFFLL;
}
if ( !did0_0 )
{
did0_0 = 1;
tcgetattr(fd, &oldtty);
}
v15 = _mm_load_si128((const __m128i *)&oldtty);
v16 = _mm_load_si128((const __m128i *)&oldtty.c_line);
v17 = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
v18 = oldtty.c_ospeed;
*(__m128i *)&tty.c_iflag = v15;
v19 = Twostop;
tty.c_ospeed = v18;
v20 = tty.c_cflag;
*(_QWORD *)&tty.c_iflag = 1026LL;
BYTE1(v20) &= 0xFEu;
*(__m128i *)&tty.c_line = v16;
*(__m128i *)&tty.c_cc[15] = v17;
tty.c_cflag = v20 | 0x30;
if ( v19 )
tty.c_cflag = v20 | 0x70;
v21 = protocol == 2;
v22 = protocol != 2;
*(_WORD *)&tty.c_cc[5] = 257;
tty.c_lflag = v22;
v23 = -1;
if ( !v21 )
v23 = 24;
tty.c_cc[1] = 0;
tty.c_cc[0] = v23;
tcsetattr(fd, 1, &tty);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2)
{
struct termios *v4; // rdi
struct termios *v5; // rsi
__int64 i; // rcx
__m128i v7; // xmm3
__m128i v8; // xmm4
__m128i v9; // xmm5
speed_t v10; // eax
tcflag_t v11; // eax
int v12; // esi
unsigned int v13; // eax
int v14; // eax
__m128i v15; // xmm0
__m128i v16; // xmm1
__m128i v17; // xmm2
speed_t v18; // eax
int v19; // er8
tcflag_t v20; // eax
bool v21; // zf
_BOOL4 v22; // eax
cc_t v23; // al
__m128i v24; // xmm6
__m128i v25; // xmm7
speed_t v26; // eax
__m128i v27; // xmm6

vfile("mode:%d", a2);
if ( (_DWORD)a2 != 2 )
{
if ( (int)a2 > 2 )
{
if ( (_DWORD)a2 == 3 )
{
if ( did0_0 )
{
v24 = _mm_load_si128((const __m128i *)&oldtty);
v25 = _mm_load_si128((const __m128i *)&oldtty.c_line);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
v26 = oldtty.c_ospeed;
*(__m128i *)&tty.c_iflag = v24;
v27 = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
tty.c_ospeed = v26;
*(__m128i *)&tty.c_line = v25;
*(__m128i *)&tty.c_cc[15] = v27;
goto LABEL_28;
}
LABEL_15:
did0_0 = 1;
tcgetattr(fd, &oldtty);
v7 = _mm_load_si128((const __m128i *)&oldtty);
v8 = _mm_load_si128((const __m128i *)&oldtty.c_line);
v9 = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
v10 = oldtty.c_ospeed;
*(__m128i *)&tty.c_iflag = v7;
tty.c_ospeed = v10;
*(__m128i *)&tty.c_line = v8;
*(__m128i *)&tty.c_cc[15] = v9;
if ( (_DWORD)a2 != 3 )
{
LABEL_16:
tty.c_iflag = 1;
LABEL_17:
v11 = tty.c_cflag;
v12 = Twostop;
tty.c_oflag = 0;
tty.c_lflag &= 0xFFFFFFF4;
v13 = v11 & 0xFFFFFECF;
tty.c_cflag = v13 | 0x30;
if ( v12 )
tty.c_cflag = v13 | 0x70;
*(_WORD *)&tty.c_cc[5] = 257;
tcsetattr(fd, 1, &tty);
v14 = cfgetospeed(&tty);
Baudrate = func0(v14);
return 0LL;
}
LABEL_28:
tty.c_iflag = 4097;
goto LABEL_17;
}
}
else
{
if ( !(_DWORD)a2 )
{
if ( did0_0 )
{
tcflush(fd, 2);
tcsetattr(fd, 1, &oldtty);
tcflow(fd, 1);
return 0LL;
}
return 0xFFFFFFFFLL;
}
if ( (_DWORD)a2 == 1 )
{
if ( did0_0 )
{
v4 = &tty;
v5 = &oldtty;
for ( i = 15LL; i; --i )
{
v4->c_iflag = v5->c_iflag;
v5 = (struct termios *)((char *)v5 + 4);
v4 = (struct termios *)((char *)v4 + 4);
}
goto LABEL_16;
}
goto LABEL_15;
}
}
return 0xFFFFFFFFLL;
}
if ( !did0_0 )
{
did0_0 = 1;
tcgetattr(fd, &oldtty);
}
v15 = _mm_load_si128((const __m128i *)&oldtty);
v16 = _mm_load_si128((const __m128i *)&oldtty.c_line);
v17 = _mm_load_si128((const __m128i *)&oldtty.c_cc[15]);
*(_QWORD *)&tty.c_cc[31] = *(_QWORD *)&oldtty.c_cc[31];
v18 = oldtty.c_ospeed;
*(__m128i *)&tty.c_iflag = v15;
v19 = Twostop;
tty.c_ospeed = v18;
v20 = tty.c_cflag;
*(_QWORD *)&tty.c_iflag = 1026LL;
BYTE1(v20) &= 0xFEu;
*(__m128i *)&tty.c_line = v16;
*(__m128i *)&tty.c_cc[15] = v17;
tty.c_cflag = v20 | 0x30;
if ( v19 )
tty.c_cflag = v20 | 0x70;
v21 = protocol == 2;
v22 = protocol != 2;
*(_WORD *)&tty.c_cc[5] = 257;
tty.c_lflag = v22;
v23 = -1;
if ( !v21 )
v23 = 24;
tty.c_cc[1] = 0;
tty.c_cc[0] = v23;
tcsetattr(fd, 1, &tty);
return 0LL;
}

original_code:
int
io_mode(int fd, int n)
{
 static int did0 = 0;
 vfile("mode:%d", n);
 switch(n) {
 case 2:
  if(!did0) {
   did0 = 1;
   tcgetattr(fd,&oldtty);
  }
  tty = oldtty;
  tty.c_iflag = 0000002|0002000;
  tty.c_oflag = 0;
  tty.c_cflag &= ~0000400;
  tty.c_cflag |= 0000060;
  if (Twostop)
   tty.c_cflag |= 0000100;
  tty.c_lflag = protocol==ZM_ZMODEM ? 0 : 0000001;
  tty.c_cc[0] = protocol==ZM_ZMODEM ? -1 : 030;
  if (((int) '\0')!=(-1)) {
   tty.c_cc[1] = '\0';
  } else {
   tty.c_cc[1] = -1;
  }
  tty.c_cc[6] = 1;
  tty.c_cc[5] = 1;
  tcsetattr(fd,1,&tty);
  return 0;
 case 1:
 case 3:
  if(!did0) {
   did0 = 1;
   tcgetattr(fd,&oldtty);
  }
  tty = oldtty;
  tty.c_iflag = 0000001;
  if (n==3)
   tty.c_iflag |= 0010000;
  tty.c_lflag &= ~(0000010 | 0000002 | 0000001);
  tty.c_oflag = 0;
  tty.c_cflag &= ~(0000400);
  tty.c_cflag &= ~(0000060);
  tty.c_cflag |= 0000060;
  if (Twostop)
   tty.c_cflag |= 0000100;
  tty.c_cc[6] = 1;
  tty.c_cc[5] = 1;
  tcsetattr(fd,1,&tty);
  Baudrate = getspeed(cfgetospeed(&tty));
  return 0;
 case 0:
  if(!did0)
   return (-1);
  tcflush (fd,2);
  tcsetattr (fd,1,&oldtty);
  tcflow (fd,1);
  return 0;
  return 0;
 default:
  return (-1);
 }
}

variable_types: n int


sendbrk

decompiled_code:
int __fastcall sendbrk(int a1)
{
return tcsendbreak(a1, 0);
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
return tcsendbreak(a1, 0);
}

original_code:
void
sendbrk(int fd)
{
 tcsendbreak(fd,0);
}

return_type: void


purgeline

decompiled_code:
int __fastcall purgeline(int fd)
{
readline_purge();
return ioctl(fd, 0x540BuLL, 0LL);
}

canonical_decompiled_code:
int __fastcall func5(int fd)
{
readline_purge();
return ioctl(fd, 0x540BuLL, 0LL);
}

original_code:
void
purgeline(int fd)
{
 readline_purge();
 ioctl(fd, 0x540B, 0);
}


zperr

decompiled_code:
int zperr(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = Verbose;
if ( Verbose > 0 )
{
fprintf(stderr, "Retry %d: ", errors);
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

canonical_decompiled_code:
int func0(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = Verbose;
if ( Verbose > 0 )
{
fprintf(stderr, "Retry %d: ", errors);
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

original_code:
void
zperr(const char *fmt, ...)
{
    va_list ap;
 if (Verbose<=0)
  return;
 fprintf(stderr,"Retry %d: ",errors);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

canonical_original_code:
void zperr(char *fmt, ...)
{
    __builtin_va_list ap;
 if (Verbose<=0)
  return;
 fprintf(stderr,"Retry %d: ",errors);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

variable_types:
fmt
char *


zpfatal

decompiled_code:
int zpfatal(char *format, ...)
{
int result; // eax
int v2; // er12
char *v3; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

result = Verbose;
if ( Verbose > 0 )
{
v2 = *_errno_location();
fprintf(stderr, "%s: ", program_name);
va_start(arg, format);
vfprintf(stderr, format, arg);
v3 = strerror(v2);
result = fprintf(stderr, ": %s\n", v3);
}
return result;
}

canonical_decompiled_code:
int func1(char *format, ...)
{
int result; // eax
int v2; // er12
char *v3; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

result = Verbose;
if ( Verbose > 0 )
{
v2 = *_errno_location();
fprintf(stderr, "%s: ", program_name);
va_start(arg, format);
vfprintf(stderr, format, arg);
v3 = strerror(v2);
result = fprintf(stderr, ": %s\n", v3);
}
return result;
}

original_code:
void
zpfatal(const char *fmt, ...)
{
    va_list ap;
    int err=(*__errno_location ());
 if (Verbose<=0)
  return;
 fprintf(stderr,"%s: ",program_name);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
 fprintf(stderr,": %s\n",strerror(err));
}

canonical_original_code:
void zpfatal(char *fmt, ...)
{
    __builtin_va_list ap;
    int err = (*__errno_location ());
 if (Verbose<=0)
  return;
 fprintf(stderr,"%s: ",program_name);
    __builtin_va_start(ap,fmt);
    vfprintf(stderr,fmt, ap);
    __builtin_va_end(ap);
 fprintf(stderr,": %s\n",strerror(err));
}

variable_types: err int


vfile

decompiled_code:
int vfile(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( Verbose > 2 )
{
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

canonical_decompiled_code:
int func2(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( Verbose > 2 )
{
va_start(arg, format);
vfprintf(stderr, format, arg);
result = putc(10, stderr);
}
return result;
}

original_code:
void
vfile(const char *format, ...)
{
    va_list ap;
 if (Verbose < 3)
  return;
    __builtin_va_start(ap,format);
    vfprintf(stderr,format, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

canonical_original_code:
void vfile(char *format, ...)
{
    __builtin_va_list ap;
 if (Verbose < 3)
  return;
    __builtin_va_start(ap,format);
    vfprintf(stderr,format, ap);
    __builtin_va_end(ap);
    putc('\n',stderr);
}

variable_types:
format
char *


lsyslog

decompiled_code:
void lsyslog(int pri, char *f, ...)
{
__uid_t v3; // ebx
struct passwd *v4; // rax
const char *v5; // rdi
char *v6; // rax
void *ptr; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

ptr = 0LL;
if ( enable_syslog )
{
if ( !init_done_2 )
{
init_done_2 = 1;
v3 = getuid();
v4 = getpwuid(v3);
if ( v4 && (v5 = v4->pw_name) != 0LL && *v5 )
{
v6 = strdup(v5);
username_1 = (__int64)v6;
}
else
{
v6 = (char *)username_1;
}
if ( !v6 )
{
username_1 = (__int64)uid_string_0;
sprintf(uid_string_0, "#%lu", v3);
}
}
va_start(arg, f);
vasprintf((char **)&ptr, f, arg);
syslog(pri, "[%s] %s", (const char *)username_1, (const char *)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void func0(int pri, char *f, ...)
{
__uid_t v3; // ebx
struct passwd *v4; // rax
const char *v5; // rdi
char *v6; // rax
void *ptr; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

ptr = 0LL;
if ( enable_syslog )
{
if ( !init_done_2 )
{
init_done_2 = 1;
v3 = getuid();
v4 = getpwuid(v3);
if ( v4 && (v5 = v4->pw_name) != 0LL && *v5 )
{
v6 = strdup(v5);
username_1 = (__int64)v6;
}
else
{
v6 = (char *)username_1;
}
if ( !v6 )
{
username_1 = (__int64)uid_string_0;
sprintf(uid_string_0, "#%lu", v3);
}
}
va_start(arg, f);
vasprintf((char **)&ptr, f, arg);
syslog(pri, "[%s] %s", (const char *)username_1, (const char *)ptr);
free(ptr);
}
}

original_code:
void
lsyslog(int prio, const char *format, ...)
{
 static char *username=((void *)0);
 static char uid_string[20]="";
 char *s=((void *)0);
 static int init_done=0;
    va_list ap;
 if (!enable_syslog)
  return;
 if (!init_done) {
  uid_t uid;
  struct passwd *pwd;
  init_done=1;
  uid=getuid();
  pwd=getpwuid(uid);
  if (pwd && pwd->pw_name && *pwd->pw_name) {
   username=strdup(pwd->pw_name);
  }
  if (!username) {
   username=uid_string;
   sprintf(uid_string,"#%lu",(unsigned long) uid);
  }
 }
    __builtin_va_start(ap,format);
    vasprintf(&s,format, ap);
    __builtin_va_end(ap);
    syslog(prio,"[%s] %s",username,s);
 free(s);
}

canonical_original_code:
void lsyslog(int prio, char *format, ...)
{
 static char *username = ((void *)0);
 static char uid_string[20] = "";
 char *s = ((void *)0);
 static int init_done = 0;
    __builtin_va_list ap;
 if (!enable_syslog)
  return;
 if (!init_done) {
  unsigned int uid;
  struct passwd *pwd;
  init_done=1;
  uid=getuid();
  pwd=getpwuid(uid);
  if (pwd && pwd->pw_name && *pwd->pw_name) {
   username=strdup(pwd->pw_name);
  }
  if (!username) {
   username=uid_string;
   sprintf(uid_string,"#%lu",(unsigned long) uid);
  }
 }
    __builtin_va_start(ap,format);
    vasprintf(&s,format, ap);
    __builtin_va_end(ap);
    syslog(prio,"[%s] %s",username,s);
 free(s);
}

variable_types: prio int

variable_types:
username
char *

variable_types:
s
char *

variable_types:
uid
unsigned int

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


error

decompiled_code:
int error(int status, int errnum, char *format, ...)
{
int result; // eax
char *v5; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s: ", program_name);
}
va_start(arg, format);
vfprintf(stderr, format, arg);
++error_message_count;
if ( errnum )
{
v5 = strerror(errnum);
fprintf(stderr, ": %s", v5);
}
putc(10, stderr);
result = fflush(stderr);
if ( status )
exit(status);
return result;
}

canonical_decompiled_code:
int func0(int status, int errnum, char *format, ...)
{
int result; // eax
char *v5; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s: ", program_name);
}
va_start(arg, format);
vfprintf(stderr, format, arg);
++error_message_count;
if ( errnum )
{
v5 = strerror(errnum);
fprintf(stderr, ": %s", v5);
}
putc(10, stderr);
result = fflush(stderr);
if ( status )
exit(status);
return result;
}

original_code:
void
error (int status, int errnum, const char *message, ...)
{
  va_list args;
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s: ", program_name);
    }
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

canonical_original_code:
void error(int status, int errnum, char *message, ...)
{
  __builtin_va_list args;
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s: ", program_name);
    }
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

variable_types: status int

variable_types: errnum int

variable_types:
message
char *


decompiled_code:
int error_at_line(int status, int errnum, char *s2, unsigned int a4, const char *a5, ...)
{
int result; // eax
char *v9; // rax
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

result = error_one_per_line;
if ( error_one_per_line )
{
if ( old_line_number_1 == a4 )
{
if ( old_file_name_0 == s2 )
return result;
result = strcmp(old_file_name_0, s2);
if ( !result )
return result;
}
old_file_name_0 = s2;
old_line_number_1 = a4;
}
if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s:", program_name);
}
if ( s2 )
fprintf(stderr, "%s:%d: ", s2, a4);
va_start(arg, a5);
vfprintf(stderr, a5, arg);
++error_message_count;
if ( errnum )
{
v9 = strerror(errnum);
fprintf(stderr, ": %s", v9);
}
putc(10, stderr);
result = fflush(stderr);
if ( status )
exit(status);
return result;
}

canonical_decompiled_code:
int func1(int status, int errnum, char *s2, unsigned int a4, const char *a5, ...)
{
int result; // eax
char *v9; // rax
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

result = error_one_per_line;
if ( error_one_per_line )
{
if ( old_line_number_1 == a4 )
{
if ( old_file_name_0 == s2 )
return result;
result = strcmp(old_file_name_0, s2);
if ( !result )
return result;
}
old_file_name_0 = s2;
old_line_number_1 = a4;
}
if ( error_print_progname )
{
error_print_progname();
}
else
{
fflush(stdout);
fprintf(stderr, "%s:", program_name);
}
if ( s2 )
fprintf(stderr, "%s:%d: ", s2, a4);
va_start(arg, a5);
vfprintf(stderr, a5, arg);
++error_message_count;
if ( errnum )
{
v9 = strerror(errnum);
fprintf(stderr, ": %s", v9);
}
putc(10, stderr);
result = fflush(stderr);
if ( status )
exit(status);
return result;
}

original_code:
void
error_at_line (int status, int errnum, const char *file_name,
        unsigned int line_number, const char *message, ...)
{
  va_list args;
  if (error_one_per_line)
    {
      static const char *old_file_name;
      static unsigned int old_line_number;
      if (old_line_number == line_number &&
   (file_name == old_file_name || !strcmp (old_file_name, file_name)))
 return;
      old_file_name = file_name;
      old_line_number = line_number;
    }
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s:", program_name);
    }
  if (file_name != ((void *)0))
    fprintf (stderr, "%s:%d: ", file_name, line_number);
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}

canonical_original_code:
void error_at_line(int status, int errnum, char *file_name, unsigned int line_number, char *message, ...)
{
  __builtin_va_list args;
  if (error_one_per_line)
    {
      static char *old_file_name;
      static unsigned int old_line_number;
      if (old_line_number == line_number &&
   (file_name == old_file_name || !strcmp (old_file_name, file_name)))
 return;
      old_file_name = file_name;
      old_line_number = line_number;
    }
  if (error_print_progname)
    (*error_print_progname) ();
  else
    {
      fflush (stdout);
      fprintf (stderr, "%s:", program_name);
    }
  if (file_name != ((void *)0))
    fprintf (stderr, "%s:%d: ", file_name, line_number);
  __builtin_va_start(args,message);
  vfprintf (stderr, message, args);
  __builtin_va_end(args);
  ++error_message_count;
  if (errnum)
    fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  if (status)
    exit (status);
}


canit

decompiled_code:
ssize_t __fastcall canit(int fd)
{
size_t v1; // rax
ssize_t result; // rax
size_t v3; // rax

purgeline();
v1 = strlen(&canistr_0);
result = write(fd, &canistr_0, v1);
if ( !fd )
{
v3 = strlen(&canistr_0);
result = write(1, &canistr_0, v3);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd)
{
size_t v1; // rax
ssize_t result; // rax
size_t v3; // rax

purgeline();
v1 = strlen(&canistr_0);
result = write(fd, &canistr_0, v1);
if ( !fd )
{
v3 = strlen(&canistr_0);
result = write(1, &canistr_0, v3);
}
return result;
}

original_code:
void
canit (int fd)
{
 static char canistr[] =
 {
  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0
 };
 purgeline(fd);
 write(fd,canistr,strlen(canistr));
 if (fd==0)
  write(1,canistr,strlen(canistr));
}

canonical_original_code:
void canit(int fd)
{
 static char canistr[] = {
  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0
 };
 purgeline(fd);
 write(fd,canistr,strlen(canistr));
 if (fd==0)
  write(1,canistr,strlen(canistr));
}

variable_types:
canistr
char []


decompiled_code:
void zreadline_alarm_handler()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void
zreadline_alarm_handler(int dummy __attribute__((__unused__)))
{
}

canonical_original_code:
static void zreadline_alarm_handler(int dummy)
{
}

variable_types: dummy int


decompiled_code:
__int64 __fastcall readline_internal(unsigned int a1)
{
unsigned int v1; // er12
int v2; // er12
__int64 v3; // rsi
unsigned __int8 *v4; // rax
__int64 v6; // rcx
int v7; // eax
char v8; // dl
int v9; // edi
__int64 v10; // rbx
__int64 i; // r12
int *v12; // rbx
char *v13; // rax

if ( !no_timeout )
{
if ( (a1 > 0x13 || (v1 = 3, a1 == 1)) && (v1 = 1, a1 > 9) )
{
v1 = a1 / 0xA;
if ( Verbose > 5 )
goto LABEL_25;
}
else if ( Verbose > 5 )
{
LABEL_25:
fprintf(stderr, "Calling read: alarm=%d Readnum=%d ", v1, readline_readnum);
goto LABEL_5;
}
LABEL_5:
signal(14, (__sighandler_t)zreadline_alarm_handler);
alarm(v1);
goto LABEL_6;
}
if ( Verbose > 5 )
fprintf(stderr, "Calling read: Readnum=%d ", readline_readnum);
LABEL_6:
readline_ptr = (__int64)readline_buffer;
readline_left = read(readline_fd, readline_buffer, readline_readnum);
v2 = readline_left;
if ( !no_timeout )
{
alarm(0);
v2 = readline_left;
}
if ( v2 <= 0 )
{
if ( Verbose <= 5 )
return 4294967294LL;
goto LABEL_27;
}
v3 = bytes_per_error;
if ( !bytes_per_error )
{
if ( Verbose <= 5 )
{
LABEL_11:
v4 = (unsigned __int8 *)readline_ptr;
readline_left = v2 - 1;
++readline_ptr;
return *v4;
}
LABEL_27:
fprintf(stderr, "Read returned %d bytes\n", (unsigned int)v2);
v2 = readline_left;
if ( readline_left == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "errno=%d:%s\n", (unsigned int)*v12, v13);
v2 = readline_left;
}
if ( Verbose > 9 )
{
if ( v2 <= 0 )
return 4294967294LL;
fwrite(" ", 1uLL, 4uLL, stderr);
if ( v2 > 48 )
v2 = 48;
v10 = (unsigned int)(v2 - 1);
for ( i = 0LL; ; ++i )
{
fprintf(stderr, "%02x ", *(unsigned __int8 *)(readline_ptr + i));
if ( v10 == i )
break;
if ( __ROR4__(-1431655765 * (i + 1), 3) <= 0xAAAAAAAu )
fwrite("\n ", 1uLL, 5uLL, stderr);
}
fputc(10, stderr);
v2 = readline_left;
}
goto LABEL_21;
}
v6 = ct_1 + v2;
v7 = mod_0;
v8 = 0;
ct_1 = v6;
LABEL_15:
v9 = v7;
while ( v3 < v6 )
{
v7 = 1;
*(_BYTE *)(readline_ptr + v6 % v3) ^= v9;
v3 = bytes_per_error;
++v9;
v8 = 1;
v6 -= bytes_per_error;
if ( v9 == 256 )
goto LABEL_15;
}
if ( v8 )
{
mod_0 = v9;
ct_1 = v6;
}
v2 = readline_left;
if ( Verbose > 5 )
goto LABEL_27;
LABEL_21:
if ( v2 > 0 )
goto LABEL_11;
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // er12
int v2; // er12
__int64 v3; // rsi
unsigned __int8 *v4; // rax
__int64 v6; // rcx
int v7; // eax
char v8; // dl
int v9; // edi
__int64 v10; // rbx
__int64 i; // r12
int *v12; // rbx
char *v13; // rax

if ( !no_timeout )
{
if ( (a1 > 0x13 || (v1 = 3, a1 == 1)) && (v1 = 1, a1 > 9) )
{
v1 = a1 / 0xA;
if ( Verbose > 5 )
goto LABEL_25;
}
else if ( Verbose > 5 )
{
LABEL_25:
fprintf(stderr, "Calling read: alarm=%d Readnum=%d ", v1, readline_readnum);
goto LABEL_5;
}
LABEL_5:
signal(14, (__sighandler_t)zreadline_alarm_handler);
alarm(v1);
goto LABEL_6;
}
if ( Verbose > 5 )
fprintf(stderr, "Calling read: Readnum=%d ", readline_readnum);
LABEL_6:
readline_ptr = (__int64)readline_buffer;
readline_left = read(readline_fd, readline_buffer, readline_readnum);
v2 = readline_left;
if ( !no_timeout )
{
alarm(0);
v2 = readline_left;
}
if ( v2 <= 0 )
{
if ( Verbose <= 5 )
return 4294967294LL;
goto LABEL_27;
}
v3 = bytes_per_error;
if ( !bytes_per_error )
{
if ( Verbose <= 5 )
{
LABEL_11:
v4 = (unsigned __int8 *)readline_ptr;
readline_left = v2 - 1;
++readline_ptr;
return *v4;
}
LABEL_27:
fprintf(stderr, "Read returned %d bytes\n", (unsigned int)v2);
v2 = readline_left;
if ( readline_left == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "errno=%d:%s\n", (unsigned int)*v12, v13);
v2 = readline_left;
}
if ( Verbose > 9 )
{
if ( v2 <= 0 )
return 4294967294LL;
fwrite(" ", 1uLL, 4uLL, stderr);
if ( v2 > 48 )
v2 = 48;
v10 = (unsigned int)(v2 - 1);
for ( i = 0LL; ; ++i )
{
fprintf(stderr, "%02x ", *(unsigned __int8 *)(readline_ptr + i));
if ( v10 == i )
break;
if ( __ROR4__(-1431655765 * (i + 1), 3) <= 0xAAAAAAAu )
fwrite("\n ", 1uLL, 5uLL, stderr);
}
fputc(10, stderr);
v2 = readline_left;
}
goto LABEL_21;
}
v6 = ct_1 + v2;
v7 = mod_0;
v8 = 0;
ct_1 = v6;
LABEL_15:
v9 = v7;
while ( v3 < v6 )
{
v7 = 1;
*(_BYTE *)(readline_ptr + v6 % v3) ^= v9;
v3 = bytes_per_error;
++v9;
v8 = 1;
v6 -= bytes_per_error;
if ( v9 == 256 )
goto LABEL_15;
}
if ( v8 )
{
mod_0 = v9;
ct_1 = v6;
}
v2 = readline_left;
if ( Verbose > 5 )
goto LABEL_27;
LABEL_21:
if ( v2 > 0 )
goto LABEL_11;
return 4294967294LL;
}

original_code:
int
readline_internal(unsigned int timeout)
{
 if (!no_timeout)
 {
  unsigned int n;
  n = timeout/10;
  if (n < 2 && timeout!=1)
   n = 3;
  else if (n==0)
   n=1;
  if (Verbose > 5)
   fprintf(stderr,"Calling read: alarm=%d  Readnum=%d ", n, readline_readnum);
  signal(14, zreadline_alarm_handler);
  alarm(n);
 }
 else if (Verbose > 5)
  fprintf(stderr,"Calling read: Readnum=%d ", readline_readnum);
 readline_ptr=readline_buffer;
 readline_left=read(readline_fd, readline_ptr, readline_readnum);
 if (!no_timeout)
  alarm(0);
 if (readline_left>0 && bytes_per_error) {
  static long ct=0;
  static int mod=1;
  ct+=readline_left;
  while (ct>bytes_per_error) {
   readline_ptr[ct % bytes_per_error]^=mod;
   ct-=bytes_per_error;
   mod++;
   if (mod==256)
    mod=1;
  }
 }
 if (Verbose > 5) {
  fprintf(stderr,"Read returned %d bytes\n", readline_left);
  if (readline_left==-1)
   fprintf(stderr,"errno=%d:%s\n", (*__errno_location ()),strerror((*__errno_location ())));
  if (Verbose > 9 && readline_left>0) {
   int i,j;
   j=readline_left > 48 ? 48 : readline_left;
   fputs("    ",stderr);
   for (i=0;i<j;i++) {
    if (i%24==0 && i)
     fputs("\n    ",stderr);
    fprintf(stderr,"%02x ", readline_ptr[i] & 0377);
   }
   fprintf(stderr,"\n");
  }
 }
 if (readline_left < 1)
  return (-2);
 --readline_left;
 return (*readline_ptr++ & 0377);
}

canonical_original_code:
int readline_internal(unsigned int timeout)
{
 if (!no_timeout)
 {
  unsigned int n;
  n = timeout/10;
  if (n < 2 && timeout!=1)
   n = 3;
  else if (n==0)
   n=1;
  if (Verbose > 5)
   fprintf(stderr,"Calling read: alarm=%d  Readnum=%d ", n, readline_readnum);
  signal(14, zreadline_alarm_handler);
  alarm(n);
 }
 else if (Verbose > 5)
  fprintf(stderr,"Calling read: Readnum=%d ", readline_readnum);
 readline_ptr=readline_buffer;
 readline_left=read(readline_fd, readline_ptr, readline_readnum);
 if (!no_timeout)
  alarm(0);
 if (readline_left>0 && bytes_per_error) {
  static long ct = 0;
  static int mod = 1;
  ct+=readline_left;
  while (ct>bytes_per_error) {
   readline_ptr[ct % bytes_per_error]^=mod;
   ct-=bytes_per_error;
   mod++;
   if (mod==256)
    mod=1;
  }
 }
 if (Verbose > 5) {
  fprintf(stderr,"Read returned %d bytes\n", readline_left);
  if (readline_left==-1)
   fprintf(stderr,"errno=%d:%s\n", (*__errno_location ()),strerror((*__errno_location ())));
  if (Verbose > 9 && readline_left>0) {
   int i; int j;
   j=readline_left > 48 ? 48 : readline_left;
   fputs("    ",stderr);
   for (i=0;i<j;i++) {
    if (i%24==0 && i)
     fputs("\n    ",stderr);
    fprintf(stderr,"%02x ", readline_ptr[i] & 0377);
   }
   fprintf(stderr,"\n");
  }
 }
 if (readline_left < 1)
  return (-2);
 --readline_left;
 return (*readline_ptr++ & 0377);
}

variable_types:
timeout
unsigned int

variable_types:
n
unsigned int

variable_types: ct long

variable_types: mod int

variable_types: i int

variable_types: j int


decompiled_code:
void __fastcall readline_setup(int a1, size_t a2, size_t a3)
{
readline_fd = a1;
if ( a2 >= a3 )
a3 = a2;
readline_readnum = a2;
readline_buffer = malloc(a3);
if ( !readline_buffer )
error(1, 0, "out of memory");
}

canonical_decompiled_code:
void __fastcall func2(int a1, size_t a2, size_t a3)
{
readline_fd = a1;
if ( a2 >= a3 )
a3 = a2;
readline_readnum = a2;
readline_buffer = malloc(a3);
if ( !readline_buffer )
error(1, 0, "out of memory");
}

original_code:
void
readline_setup(int fd, size_t readnum, size_t bufsize)
{
 readline_fd=fd;
 readline_readnum=readnum;
 readline_buffer=malloc(bufsize > readnum ? bufsize : readnum);
 if (!readline_buffer)
  error(1,0,"out of memory");
}

canonical_original_code:
void readline_setup(int fd, unsigned long readnum, unsigned long bufsize)
{
 readline_fd=fd;
 readline_readnum=readnum;
 readline_buffer=malloc(bufsize > readnum ? bufsize : readnum);
 if (!readline_buffer)
  error(1,0,"out of memory");
}

variable_types: fd int

variable_types:
readnum
unsigned long

variable_types:
bufsize
unsigned long


decompiled_code:
void readline_purge()
{
readline_left = 0;
}

canonical_decompiled_code:
void func3()
{
readline_left = 0;
}

original_code:
void
readline_purge(void)
{
 readline_left=0;
 return;
}

canonical_original_code:
void readline_purge()
{
 readline_left=0;
 return;
}


protname

decompiled_code:
const char *protname()
{
const char *result; // rax

result = "XMODEM";
if ( protocol )
{
result = "ZMODEM";
if ( protocol == 1 )
result = "YMODEM";
}
return result;
}

canonical_decompiled_code:
const char *func0()
{
const char *result; // rax

result = "XMODEM";
if ( protocol )
{
result = "ZMODEM";
if ( protocol == 1 )
result = "YMODEM";
}
return result;
}

original_code:
const char *
protname(void)
{
 const char *prot_name;
 switch(protocol) {
 case ZM_XMODEM:
  prot_name="XMODEM";
  break;
 case ZM_YMODEM:
  prot_name="YMODEM";
  break;
 default:
  prot_name="ZMODEM";
  break;
 }
 return prot_name;
}

canonical_original_code:
char *protname()
{
 char *prot_name;
 switch(protocol) {
 case ZM_XMODEM:
  prot_name="XMODEM";
  break;
 case ZM_YMODEM:
  prot_name="YMODEM";
  break;
 default:
  prot_name="ZMODEM";
  break;
 }
 return prot_name;
}

return_type: char *


timing

decompiled_code:
double __fastcall timing(int a1, _QWORD *a2)
{
double result; // xmm0_8
struct timezone tz; // [rsp+8h] [rbp-30h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

tz.tz_dsttime = 0;
gettimeofday(&tv, &tz);
result = (double)SLODWORD(tv.tv_usec) / 1000000.0 + (double)SLODWORD(tv.tv_sec);
if ( a2 )
*a2 = (unsigned int)(int)result;
if ( !a1 )
return result - *(double *)&starttime_2;
starttime_2 = *(_QWORD *)&result;
return result;
}

canonical_decompiled_code:
double __fastcall func0(int a1, _QWORD *a2)
{
double result; // xmm0_8
struct timezone tz; // [rsp+8h] [rbp-30h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

tz.tz_dsttime = 0;
gettimeofday(&tv, &tz);
result = (double)SLODWORD(tv.tv_usec) / 1000000.0 + (double)SLODWORD(tv.tv_sec);
if ( a2 )
*a2 = (unsigned int)(int)result;
if ( !a1 )
return result - *(double *)&starttime_2;
starttime_2 = *(_QWORD *)&result;
return result;
}

original_code:
double
timing (int reset, time_t *nowp)
{
  static double elaptime, starttime, stoptime;
  double yet;
  struct timeval tv;
  struct timezone tz;
  tz.tz_dsttime = 0;
  gettimeofday (&tv, &tz);
  yet=tv.tv_sec + tv.tv_usec/1000000.0;
  if (nowp)
    *nowp=(time_t) yet;
  if (reset) {
    starttime = yet;
    return starttime;
  }
  else {
    stoptime = yet;
    elaptime = stoptime - starttime;
    return elaptime;
  }
}

canonical_original_code:
double timing(int reset, long *nowp)
{
  static double elaptime; static double starttime; static double stoptime;
  double yet;
  struct timeval tv;
  struct timezone tz;
  tz.tz_dsttime = 0;
  gettimeofday (&tv, &tz);
  yet=tv.tv_sec + tv.tv_usec/1000000.0;
  if (nowp)
    *nowp=(long) yet;
  if (reset) {
    starttime = yet;
    return starttime;
  }
  else {
    stoptime = yet;
    elaptime = stoptime - starttime;
    return elaptime;
  }
}

variable_types: reset int

variable_types:
nowp
long *

variable_types: elaptime double

variable_types: starttime double

variable_types: stoptime double

variable_types: yet double

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

return_type: double


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
unsigned int v4; // eax
int v5; // er12
unsigned int v6; // eax

puts(" exVasi0n, tihmstar will pwn you :O");
v3 = (__int64)" waiting for device";
puts(" waiting for device");
while ( (unsigned int)deviceConnect(v3, argv) )
{
v3 = 1LL;
sleep(1u);
}
puts(" device found!");
if ( (unsigned int)startLockdownd() || (unsigned int)startAFC() || (unsigned int)connectAFC() )
return -1;
afcerr = afc_make_directory();
if ( afcerr )
{
v5 = -1;
printf("%s Error creating dir %s\n\n", "\x1B[31m", "\x1B[0m");
afc_client_free();
idevice_free();
return v5;
}
afcerr = afc_send_file(gAfc, "mylittlesecret.tar", "evasi0n-install/Cydia.tar");
if ( afcerr )
{
v5 = -1;
printf("%s Error putting file.%s\n\n", "\x1B[31m", "\x1B[0m");
return v5;
}
puts(" lockdownd...");
lderr = lockdownd_client_new_with_handshake();
if ( lderr )
{
v5 = -1;
printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
return v5;
}
puts(" gonna reboot");
lderr = lockdownd_start_service();
if ( lderr )
{
v5 = -1;
printf("%s diag service error%s\n", "\x1B[31m", "\x1B[0m");
return v5;
}
v4 = diagnostics_relay_client_new();
diagerr = v4;
v5 = v4;
if ( v4 )
{
printf("%s diag client error %s %d\n", "\x1B[31m", "\x1B[0m", v4);
goto LABEL_16;
}
v6 = diagnostics_relay_restart();
diagerr = v6;
if ( ((v6 + 2) & 0xFFFFFFFD) != 0 )
{
printf("%s reboot error, reboot manually %d %s\n", "\x1B[0m", v6, "\x1B[0m");
LABEL_16:
lockdownd_client_free();
idevice_free();
return -1;
}
puts(" done :P ");
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
unsigned int v4; // eax
int v5; // er12
unsigned int v6; // eax

puts(" exVasi0n, tihmstar will pwn you :O");
v3 = (__int64)" waiting for device";
puts(" waiting for device");
while ( (unsigned int)func8(v3, argv) )
{
v3 = 1LL;
sleep(1u);
}
puts(" device found!");
if ( (unsigned int)func9() || (unsigned int)func10() || (unsigned int)func11() )
return -1;
afcerr = afc_make_directory();
if ( afcerr )
{
v5 = -1;
printf("%s Error creating dir %s\n\n", "\x1B[31m", "\x1B[0m");
afc_client_free();
idevice_free();
return v5;
}
afcerr = func7(gAfc, "mylittlesecret.tar", "evasi0n-install/Cydia.tar");
if ( afcerr )
{
v5 = -1;
printf("%s Error putting file.%s\n\n", "\x1B[31m", "\x1B[0m");
return v5;
}
puts(" lockdownd...");
lderr = lockdownd_client_new_with_handshake();
if ( lderr )
{
v5 = -1;
printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
return v5;
}
puts(" gonna reboot");
lderr = lockdownd_start_service();
if ( lderr )
{
v5 = -1;
printf("%s diag service error%s\n", "\x1B[31m", "\x1B[0m");
return v5;
}
v4 = diagnostics_relay_client_new();
diagerr = v4;
v5 = v4;
if ( v4 )
{
printf("%s diag client error %s %d\n", "\x1B[31m", "\x1B[0m", v4);
goto LABEL_16;
}
v6 = diagnostics_relay_restart();
diagerr = v6;
if ( ((v6 + 2) & 0xFFFFFFFD) != 0 )
{
printf("%s reboot error, reboot manually %d %s\n", "\x1B[0m", v6, "\x1B[0m");
LABEL_16:
lockdownd_client_free();
idevice_free();
return -1;
}
puts(" done :P ");
return v5;
}

original_code:
int main(int argc, char *argv[]) {
 printf(" exVasi0n, tihmstar will pwn you :O\n");
 printf(" waiting for device\n");
 while (deviceConnect() != 0) {
  sleep(1);
 }
 printf(" device found!\n");
 if (startLockdownd() != 0) {
  return -1;
 }
 if (startAFC() != 0) {
  return -1;
 }
 if (connectAFC() != 0) {
  return -1;
 }
 afcerr = afc_make_directory(gAfc, "/evasi0n-install");
 if (afcerr != AFC_E_SUCCESS) {
  printf("%s Error creating dir %s\n\n", "\x1B[31m", "\x1B[0m");
  afc_client_free(gAfc);
  idevice_free(gDevice);
  return -1;
 }
 afcerr = afc_send_file(gAfc, "mylittlesecret.tar", "evasi0n-install/Cydia.tar");
 if (afcerr != AFC_E_SUCCESS) {
  printf("%s Error putting file.%s\n\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 printf(" lockdownd...\n");
 lderr = lockdownd_client_new_with_handshake(gDevice, &gLockdown, "exVasi0n");
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 printf(" gonna reboot\n");
 lderr = lockdownd_start_service(gLockdown, "com.apple.mobile.diagnostics_relay", &port);
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s diag service error%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 diagerr = diagnostics_relay_client_new(gDevice, port, &gDiag);
 if (diagerr != DIAGNOSTICS_RELAY_E_SUCCESS) {
  printf("%s diag client error %s %d\n", "\x1B[31m", "\x1B[0m",diagerr);
  lockdownd_client_free(gLockdown);
  idevice_free(gDevice);
  return -1;
 }
 diagerr = diagnostics_relay_restart(gDiag, (1 << 2));
 if (diagerr != DIAGNOSTICS_RELAY_E_SUCCESS && diagerr != -2) {
        printf("%s reboot error, reboot manually %d %s\n", "\x1B[0m",diagerr, "\x1B[0m");
  lockdownd_client_free(gLockdown);
  idevice_free(gDevice);
  return -1;
 }
 printf(" done :P \n");
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


deviceConnect

decompiled_code:
__int64 deviceConnect()
{
unsigned int v0; // er12

if ( (unsigned int)idevice_new() )
return (unsigned int)-1;
v0 = 0;
if ( (unsigned int)idevice_get_udid() )
printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
printf(" UDID: %s!\n", (const char *)udid);
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // er12

if ( (unsigned int)idevice_new() )
return (unsigned int)-1;
v0 = 0;
if ( (unsigned int)idevice_get_udid() )
printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
printf(" UDID: %s!\n", (const char *)udid);
return v0;
}

original_code:
int deviceConnect() {
 idevice_error_t ideverr = 0;
 ideverr = idevice_new(&gDevice, ((void *)0));
 if (ideverr != IDEVICE_E_SUCCESS) {
  return -1;
 }
 ideverr = idevice_get_udid(gDevice, &udid);
 if (ideverr != IDEVICE_E_SUCCESS) {
  printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
 }
 printf(" UDID: %s!\n", udid);
 return 0;
}

canonical_original_code:
int deviceConnect() {
 enum idevice_error_t ideverr = 0;
 ideverr = idevice_new(&gDevice, ((void *)0));
 if (ideverr != IDEVICE_E_SUCCESS) {
  return -1;
 }
 ideverr = idevice_get_udid(gDevice, &udid);
 if (ideverr != IDEVICE_E_SUCCESS) {
  printf("%s Unable to communicate with device.%s\n", "\x1B[31m", "\x1B[0m");
 }
 printf(" UDID: %s!\n", udid);
 return 0;
}

variable_types: enum idevice_error_t { IDEVICE_E_SUCCESS=0, IDEVICE_E_INVALID_ARG=-1, IDEVICE_E_UNKNOWN_ERROR=-2, IDEVICE_E_NO_DEVICE=-3, IDEVICE_E_NOT_ENOUGH_DATA=-4, IDEVICE_E_BAD_HEADER=-5, IDEVICE_E_SSL_ERROR=-6 }


startLockdownd

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

puts(" lockdownd...");
result = lockdownd_client_new_with_handshake();
lderr = result;
if ( (_DWORD)result )
{
printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

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

puts(" lockdownd...");
result = lockdownd_client_new_with_handshake();
lderr = result;
if ( (_DWORD)result )
{
printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int startLockdownd() {
 printf(" lockdownd...\n");
 lderr = lockdownd_client_new_with_handshake(gDevice, &gLockdown, "exVasi0n");
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s [*] Unable to connect to lockdownd. Please reboot your device and try again.%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 return 0;
}


startAFC

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

puts(" afc service");
result = lockdownd_start_service();
lderr = result;
if ( (_DWORD)result )
{
printf("%s afc service error%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

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

puts(" afc service");
result = lockdownd_start_service();
lderr = result;
if ( (_DWORD)result )
{
printf("%s afc service error%s\n", "\x1B[31m", "\x1B[0m");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int startAFC() {
 printf(" afc service\n");
 lderr = lockdownd_start_service(gLockdown, "com.apple.afc", &port);
 if (lderr != LOCKDOWN_E_SUCCESS) {
  printf("%s afc service error%s\n", "\x1B[31m", "\x1B[0m");
  return -1;
 }
 return 0;
}


connectAFC

decompiled_code:
__int64 connectAFC()
{
unsigned int v0; // er12

puts(" afc client");
afcerr = afc_client_new();
if ( afcerr )
{
printf("%s afc client error%s\n", "\x1B[31m", "\x1B[0m");
v0 = -1;
lockdownd_client_free();
idevice_free();
}
else
{
v0 = 0;
lockdownd_client_free();
gLockdown = 0LL;
puts(" copying files");
}
return v0;
}

canonical_decompiled_code:
__int64 func11()
{
unsigned int v0; // er12

puts(" afc client");
afcerr = afc_client_new();
if ( afcerr )
{
printf("%s afc client error%s\n", "\x1B[31m", "\x1B[0m");
v0 = -1;
lockdownd_client_free();
idevice_free();
}
else
{
v0 = 0;
lockdownd_client_free();
gLockdown = 0LL;
puts(" copying files");
}
return v0;
}

original_code:
int connectAFC() {
 printf(" afc client\n");
 afcerr = afc_client_new(gDevice, port, &gAfc);
 if (afcerr != AFC_E_SUCCESS) {
  printf("%s afc client error%s\n", "\x1B[31m", "\x1B[0m");
  lockdownd_client_free(gLockdown);
  idevice_free(gDevice);
  return -1;
 }
 lockdownd_client_free(gLockdown);
 gLockdown = ((void *)0);
 printf(" copying files\n");
 return 0;
}


repo: tillmannw/prowler

decompiled_code:
time_t __fastcall session_init(__int64 a1, const __m128i *a2, __int64 a3)
{
__int64 v3; // rbp
_QWORD *v5; // rcx
__int64 v6; // r12
time_t result; // rax
__int64 v8; // rbp

v3 = 104 * a1;
v5 = (_QWORD *)(104 * a1 + session);
*v5 = 0LL;
v5[12] = 0LL;
memset(
(void *)((unsigned __int64)(v5 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v5 - (((_DWORD)v5 + 8) & 0xFFFFFFF8) + 104) >> 3));
*(__m128i *)(session + v3) = _mm_loadu_si128(a2);
v6 = 104 * a1 + session;
*(_WORD *)(v3 + session) = 2;
result = time(0LL);
v8 = session + 104 * a1;
*(_QWORD *)(v6 + 88) = result;
*(_QWORD *)(v8 + 80) = result;
if ( result == -1 )
{
perror("time()");
exit(1);
}
*(_DWORD *)(v8 + 64) = 1;
*(_QWORD *)(v8 + 96) = a3;
return result;
}

canonical_decompiled_code:
time_t __fastcall func0(__int64 a1, const __m128i *a2, __int64 a3)
{
__int64 v3; // rbp
_QWORD *v5; // rcx
__int64 v6; // r12
time_t result; // rax
__int64 v8; // rbp

v3 = 104 * a1;
v5 = (_QWORD *)(104 * a1 + session);
*v5 = 0LL;
v5[12] = 0LL;
memset(
(void *)((unsigned __int64)(v5 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v5 - (((_DWORD)v5 + 8) & 0xFFFFFFF8) + 104) >> 3));
*(__m128i *)(session + v3) = _mm_loadu_si128(a2);
v6 = 104 * a1 + session;
*(_WORD *)(v3 + session) = 2;
result = time(0LL);
v8 = session + 104 * a1;
*(_QWORD *)(v6 + 88) = result;
*(_QWORD *)(v8 + 80) = result;
if ( result == -1 )
{
perror("time()");
exit(1);
}
*(_DWORD *)(v8 + 64) = 1;
*(_QWORD *)(v8 + 96) = a3;
return result;
}

original_code:
void session_init(size_t i, struct sockaddr_in *saddr, size_t peerlist_index) {
 memset(&session[i], 0, sizeof(session_t));
 memcpy(&session[i].peer, saddr, sizeof(struct sockaddr_in));
 session[i].peer.sin_family = 2;
 if ((session[i].session_start = session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 session[i].used = 1;
 session[i].peerlist_index = peerlist_index;
 return;
}

canonical_original_code:
void session_init(unsigned long i, struct sockaddr_in *saddr, unsigned long peerlist_index) {
 memset(&session[i], 0, sizeof(session_t));
 memcpy(&session[i].peer, saddr, sizeof(struct sockaddr_in));
 session[i].peer.sin_family = 2;
 if ((session[i].session_start = session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 session[i].used = 1;
 session[i].peerlist_index = peerlist_index;
 return;
}

variable_types:
i
unsigned long

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }


original_code:
int session_connect(size_t i) {
 int flags;
 if ((fds[i].fd = socket(2, SOCK_STREAM, 0)) == -1) {
  perror("socket()");
  exit(1);
 }
 if (((flags = fcntl(fds[i].fd, 3, 0)) == -1) ||
     (fcntl(fds[i].fd, 4, flags | 04000) == -1)) {
  perror("fcntl()");
  exit(1);
 }
 fds[i].events = 0x004 | 0x001;
 if (connect(fds[i].fd, (struct sockaddr *) &session[i].peer, sizeof(struct sockaddr_in)) == -1) {
  switch ((*__errno_location ())) {
  case 115:
   break;
  case 101:
   return -1;
   break;
  default:
   perror("connect()");
   exit(1);
  }
  session[i].connecting = 1;
 }
 if (session[i].peerlist_index != -1) {
  peerlist[session[i].peerlist_index].last_connect = time(((void *)0));
  peerlist[session[i].peerlist_index].active = 1;
 }
 return fds[i].fd;
}

canonical_original_code:
int session_connect(unsigned long i) {
 int flags;
 if ((fds[i].fd = socket(2, SOCK_STREAM, 0)) == -1) {
  perror("socket()");
  exit(1);
 }
 if (((flags = fcntl(fds[i].fd, 3, 0)) == -1) ||
     (fcntl(fds[i].fd, 4, flags | 04000) == -1)) {
  perror("fcntl()");
  exit(1);
 }
 fds[i].events = 0x004 | 0x001;
 if (connect(fds[i].fd, (struct sockaddr *) &session[i].peer, sizeof(struct sockaddr_in)) == -1) {
  switch ((*__errno_location ())) {
  case 115:
   break;
  case 101:
   return -1;
   break;
  default:
   perror("connect()");
   exit(1);
  }
  session[i].connecting = 1;
 }
 if (session[i].peerlist_index != -1) {
  peerlist[session[i].peerlist_index].last_connect = time(((void *)0));
  peerlist[session[i].peerlist_index].active = 1;
 }
 return fds[i].fd;
}

variable_types: flags int

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


decompiled_code:
__int64 session_get_next_free()
{
_DWORD *v0; // rdx
__int64 result; // rax

if ( nfds == 1 )
{
LABEL_6:
puts("error: file descriptor limit exceeded. use -f.");
exit(1);
}
v0 = (_DWORD *)(session + 64);
result = 0LL;
while ( *v0 )
{
++result;
v0 += 26;
if ( nfds - 1 == result )
goto LABEL_6;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
_DWORD *v0; // rdx
__int64 result; // rax

if ( nfds == 1 )
{
LABEL_6:
puts("error: file descriptor limit exceeded. use -f.");
exit(1);
}
v0 = (_DWORD *)(session + 64);
result = 0LL;
while ( *v0 )
{
++result;
v0 += 26;
if ( nfds - 1 == result )
goto LABEL_6;
}
return result;
}

original_code:
int session_get_next_free(void) {
 int i;
 for (i = 0; i < nfds - 1; ++i)
  if (session[i].used == 0) return i;
 printf("error: file descriptor limit exceeded. use -f.\n");
 exit(1);
 return -1;
}

canonical_original_code:
int session_get_next_free() {
 int i;
 for (i = 0; i < nfds - 1; ++i)
  if (session[i].used == 0) return i;
 printf("error: file descriptor limit exceeded. use -f.\n");
 exit(1);
 return -1;
}

variable_types: i int


decompiled_code:
__int64 __fastcall session_delete(__int64 a1)
{
__int64 v1; // rbx
_QWORD *v2; // rcx
void *v3; // rdi
__int64 v4; // rax
__int64 result; // rax

v1 = 104 * a1;
v2 = (_QWORD *)(104 * a1 + session);
v3 = (void *)v2[3];
if ( v3 )
{
free(v3);
v2 = (_QWORD *)(session + v1);
}
v4 = v2[12];
if ( v4 != -1 )
*(_DWORD *)(peerlist + 48 * v4 + 12) = 0;
*v2 = 0LL;
result = 0LL;
v2[12] = 0LL;
memset(
(void *)((unsigned __int64)(v2 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v2 - (((_DWORD)v2 + 8) & 0xFFFFFFF8) + 104) >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rbx
_QWORD *v2; // rcx
void *v3; // rdi
__int64 v4; // rax
__int64 result; // rax

v1 = 104 * a1;
v2 = (_QWORD *)(104 * a1 + session);
v3 = (void *)v2[3];
if ( v3 )
{
free(v3);
v2 = (_QWORD *)(session + v1);
}
v4 = v2[12];
if ( v4 != -1 )
*(_DWORD *)(peerlist + 48 * v4 + 12) = 0;
*v2 = 0LL;
result = 0LL;
v2[12] = 0LL;
memset(
(void *)((unsigned __int64)(v2 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v2 - (((_DWORD)v2 + 8) & 0xFFFFFFF8) + 104) >> 3));
return result;
}

original_code:
void session_delete(size_t i) {
 if (session[i].rx != ((void *)0)) free(session[i].rx);
 if (session[i].peerlist_index != -1)
  peerlist[session[i].peerlist_index].active = 0;
 memset(&session[i], 0, sizeof(session_t));
 return;
}

canonical_original_code:
void session_delete(unsigned long i) {
 if (session[i].rx != ((void *)0)) free(session[i].rx);
 if (session[i].peerlist_index != -1)
  peerlist[session[i].peerlist_index].active = 0;
 memset(&session[i], 0, sizeof(session_t));
 return;
}


decompiled_code:
__int64 __fastcall session_done(__int64 a1)
{
__int64 v2; // rbx

if ( !*(_DWORD *)(session + 104 * a1 + 72) )
return 0LL;
shutdown(*(_DWORD *)(fds + 8 * a1), 0);
v2 = session + 104 * a1;
if ( *(_QWORD *)(v2 + 56) || *(_QWORD *)(v2 + 32) )
return 0LL;
session_delete(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v2; // rbx

if ( !*(_DWORD *)(session + 104 * a1 + 72) )
return 0LL;
shutdown(*(_DWORD *)(fds + 8 * a1), 0);
v2 = session + 104 * a1;
if ( *(_QWORD *)(v2 + 56) || *(_QWORD *)(v2 + 32) )
return 0LL;
func3(a1);
return 1LL;
}

original_code:
int session_done(size_t i) {
 if (session[i].closed_by_peer == 0) return 0;
 shutdown(fds[i].fd, SHUT_RD);
 if (session[i].txbytes || session[i].rxbytes) return 0;
 session_delete(i);
 return 1;
}

canonical_original_code:
int session_done(unsigned long i) {
 if (session[i].closed_by_peer == 0) return 0;
 shutdown(fds[i].fd, SHUT_RD);
 if (session[i].txbytes || session[i].rxbytes) return 0;
 session_delete(i);
 return 1;
}


decompiled_code:
__int64 __fastcall session_last_action(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 88);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 88);
}

original_code:
time_t session_last_action(size_t i) {
 return session[i].last_action;
}

canonical_original_code:
long session_last_action(unsigned long i) {
 return session[i].last_action;
}

return_type: long


decompiled_code:
__int64 __fastcall session_start_time(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 80);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 80);
}

original_code:
time_t session_start_time(size_t i) {
 return session[i].session_start;
}

canonical_original_code:
long session_start_time(unsigned long i) {
 return session[i].session_start;
}


decompiled_code:
ssize_t __fastcall session_receive(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r12
time_t v3; // rax
__int64 v4; // r12
void *v5; // rax
ssize_t result; // rax
ssize_t v7; // r12
__int64 v8; // r13

v1 = 104 * a1;
v2 = session;
v3 = time(0LL);
*(_QWORD *)(104 * a1 + v2 + 88) = v3;
if ( v3 == -1 )
{
perror("time()");
exit(1);
}
v4 = v1 + session;
v5 = realloc(*(void **)(v1 + session + 24), *(_QWORD *)(v1 + session + 32) + 0x2000LL);
*(_QWORD *)(v4 + 24) = v5;
if ( !v5 )
{
perror("realloc()");
exit(1);
}
result = recv(
*(_DWORD *)(fds + 8 * a1),
(void *)(*(_QWORD *)(v1 + session + 24) + *(_QWORD *)(v1 + session + 32)),
0x2000uLL,
0);
v7 = result;
if ( result == -1 )
{
if ( *_errno_location() != 11 )
{
perror("recv()");
return v7;
}
LABEL_5:
*(_QWORD *)(session + v1 + 32) += v7;
return v7;
}
if ( result )
goto LABEL_5;
v8 = fds + 8 * a1;
*(_DWORD *)(session + v1 + 72) = 1;
*(_WORD *)(v8 + 4) &= 0xFFFEu;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func7(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r12
time_t v3; // rax
__int64 v4; // r12
void *v5; // rax
ssize_t result; // rax
ssize_t v7; // r12
__int64 v8; // r13

v1 = 104 * a1;
v2 = session;
v3 = time(0LL);
*(_QWORD *)(104 * a1 + v2 + 88) = v3;
if ( v3 == -1 )
{
perror("time()");
exit(1);
}
v4 = v1 + session;
v5 = realloc(*(void **)(v1 + session + 24), *(_QWORD *)(v1 + session + 32) + 0x2000LL);
*(_QWORD *)(v4 + 24) = v5;
if ( !v5 )
{
perror("realloc()");
exit(1);
}
result = recv(
*(_DWORD *)(fds + 8 * a1),
(void *)(*(_QWORD *)(v1 + session + 24) + *(_QWORD *)(v1 + session + 32)),
0x2000uLL,
0);
v7 = result;
if ( result == -1 )
{
if ( *_errno_location() != 11 )
{
perror("recv()");
return v7;
}
LABEL_5:
*(_QWORD *)(session + v1 + 32) += v7;
return v7;
}
if ( result )
goto LABEL_5;
v8 = fds + 8 * a1;
*(_DWORD *)(session + v1 + 72) = 1;
*(_WORD *)(v8 + 4) &= 0xFFFEu;
return result;
}

original_code:
ssize_t session_receive(size_t i) {
 ssize_t bytes;
 if ((session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 if ((session[i].rx = realloc(session[i].rx, session[i].rxbytes + 8192)) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 switch ((bytes = recv(fds[i].fd, session[i].rx + session[i].rxbytes, 8192, 0))) {
 case -1:
  if ((*__errno_location ()) == 11) break;
  perror("recv()");
  return -1;
 case 0:
  session[i].closed_by_peer = 1;
  fds[i].events &= ~0x001;
  return 0;
 default:
  break;
 }
 session[i].rxbytes += bytes;
 return bytes;
}

canonical_original_code:
long session_receive(unsigned long i) {
 long bytes;
 if ((session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 if ((session[i].rx = realloc(session[i].rx, session[i].rxbytes + 8192)) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 switch ((bytes = recv(fds[i].fd, session[i].rx + session[i].rxbytes, 8192, 0))) {
 case -1:
  if ((*__errno_location ()) == 11) break;
  perror("recv()");
  return -1;
 case 0:
  session[i].closed_by_peer = 1;
  fds[i].events &= ~0x001;
  return 0;
 default:
  break;
 }
 session[i].rxbytes += bytes;
 return bytes;
}

variable_types: bytes long


decompiled_code:
__int64 __fastcall session_prepare_for_send(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v3; // rax
__int64 result; // rax

v3 = (_QWORD *)(session + 104 * a1);
v3[5] = a2;
v3[7] = a3;
v3[6] = 0LL;
result = fds;
*(_WORD *)(fds + 8 * a1 + 4) |= 4u;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v3; // rax
__int64 result; // rax

v3 = (_QWORD *)(session + 104 * a1);
v3[5] = a2;
v3[7] = a3;
v3[6] = 0LL;
result = fds;
*(_WORD *)(fds + 8 * a1 + 4) |= 4u;
return result;
}

original_code:
void session_prepare_for_send(size_t i, unsigned char *data, size_t len) {
 session[i].tx = data;
 session[i].txbytes = len;
 session[i].txoff = 0;
 fds[i].events |= 0x004;
 return;
}

canonical_original_code:
void session_prepare_for_send(unsigned long i, unsigned char *data, unsigned long len) {
 session[i].tx = data;
 session[i].txbytes = len;
 session[i].txoff = 0;
 fds[i].events |= 0x004;
 return;
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned long


decompiled_code:
ssize_t __fastcall session_send(__int64 a1)
{
__int64 v1; // rbx
_QWORD *v2; // rax
__int64 v3; // rsi
size_t v4; // rdx
ssize_t v5; // r12
ssize_t result; // rax
__int64 v7; // rbx
bool v8; // zf

v1 = 104 * a1;
v2 = (_QWORD *)(104 * a1 + session);
v3 = v2[5];
if ( !v3 )
return 0LL;
v4 = v2[7];
v5 = 0LL;
if ( !v4 )
return v5;
v5 = send(*(_DWORD *)(fds + 8 * a1), (const void *)(v2[6] + v3), v4, 0);
if ( v5 == -1 && *_errno_location() != 11 )
{
perror("send()");
return v5;
}
memmove(
*(void **)(v1 + session + 40),
(const void *)(*(_QWORD *)(v1 + session + 40) + v5),
*(_QWORD *)(v1 + session + 56) - v5);
v7 = session + v1;
*(_QWORD *)(v7 + 48) += v5;
v8 = *(_QWORD *)(v7 + 56) == v5;
*(_QWORD *)(v7 + 56) -= v5;
if ( !v8 )
return v5;
result = v5;
*(_WORD *)(fds + 8 * a1 + 4) &= 0xFFFBu;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(__int64 a1)
{
__int64 v1; // rbx
_QWORD *v2; // rax
__int64 v3; // rsi
size_t v4; // rdx
ssize_t v5; // r12
ssize_t result; // rax
__int64 v7; // rbx
bool v8; // zf

v1 = 104 * a1;
v2 = (_QWORD *)(104 * a1 + session);
v3 = v2[5];
if ( !v3 )
return 0LL;
v4 = v2[7];
v5 = 0LL;
if ( !v4 )
return v5;
v5 = send(*(_DWORD *)(fds + 8 * a1), (const void *)(v2[6] + v3), v4, 0);
if ( v5 == -1 && *_errno_location() != 11 )
{
perror("send()");
return v5;
}
memmove(
*(void **)(v1 + session + 40),
(const void *)(*(_QWORD *)(v1 + session + 40) + v5),
*(_QWORD *)(v1 + session + 56) - v5);
v7 = session + v1;
*(_QWORD *)(v7 + 48) += v5;
v8 = *(_QWORD *)(v7 + 56) == v5;
*(_QWORD *)(v7 + 56) -= v5;
if ( !v8 )
return v5;
result = v5;
*(_WORD *)(fds + 8 * a1 + 4) &= 0xFFFBu;
return result;
}

original_code:
ssize_t session_send(size_t i) {
 ssize_t bytes;
 if (session[i].tx == ((void *)0) || session[i].txbytes == 0) {
  return 0;
 }
 if ((bytes = send(fds[i].fd, session[i].tx + session[i].txoff, session[i].txbytes, 0)) == -1) {
  if ((*__errno_location ()) != 11) {
   perror("send()");
   return -1;
  }
 }
 memmove(session[i].tx, session[i].tx + bytes, session[i].txbytes - bytes);
 session[i].txoff += bytes;
 session[i].txbytes -= bytes;
 if (session[i].txbytes == 0) { fds[i].events &= ~0x004; }
 return bytes;
}

canonical_original_code:
long session_send(unsigned long i) {
 long bytes;
 if (session[i].tx == ((void *)0) || session[i].txbytes == 0) {
  return 0;
 }
 if ((bytes = send(fds[i].fd, session[i].tx + session[i].txoff, session[i].txbytes, 0)) == -1) {
  if ((*__errno_location ()) != 11) {
   perror("send()");
   return -1;
  }
 }
 memmove(session[i].tx, session[i].tx + bytes, session[i].txbytes - bytes);
 session[i].txoff += bytes;
 session[i].txbytes -= bytes;
 if (session[i].txbytes == 0) { fds[i].events &= ~0x004; }
 return bytes;
}


decompiled_code:
void __fastcall peer_add(int a1, __int16 a2, __int64 a3)
{
_DWORD *v5; // rdx
__int64 v6; // rax
size_t v7; // rsi
char *v8; // rax
__int64 v9; // rdx
char *v10; // rax

if ( a1 && a2 )
{
if ( peerlist_size )
{
v5 = peerlist;
v6 = 0LL;
while ( *v5 != a1 )
{
++v6;
v5 += 12;
if ( v6 == peerlist_size )
{
v7 = 16 * (3 * v6 + 3);
goto LABEL_9;
}
}
}
else
{
v7 = 48LL;
LABEL_9:
v8 = (char *)realloc(peerlist, v7);
peerlist = v8;
if ( !v8 )
{
perror("realloc()");
exit(1);
}
v9 = peerlist_size;
v10 = &v8[48 * peerlist_size];
*((_QWORD *)v10 + 2) = peerlist_size;
*(_DWORD *)v10 = a1;
*((_WORD *)v10 + 2) = a2;
*((_QWORD *)v10 + 5) = a3;
*((_QWORD *)v10 + 4) = 0LL;
*((_QWORD *)v10 + 1) = 0LL;
peerlist_size = v9 + 1;
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, __int16 a2, __int64 a3)
{
_DWORD *v5; // rdx
__int64 v6; // rax
size_t v7; // rsi
char *v8; // rax
__int64 v9; // rdx
char *v10; // rax

if ( a1 && a2 )
{
if ( peerlist_size )
{
v5 = peerlist;
v6 = 0LL;
while ( *v5 != a1 )
{
++v6;
v5 += 12;
if ( v6 == peerlist_size )
{
v7 = 16 * (3 * v6 + 3);
goto LABEL_9;
}
}
}
else
{
v7 = 48LL;
LABEL_9:
v8 = (char *)realloc(peerlist, v7);
peerlist = v8;
if ( !v8 )
{
perror("realloc()");
exit(1);
}
v9 = peerlist_size;
v10 = &v8[48 * peerlist_size];
*((_QWORD *)v10 + 2) = peerlist_size;
*(_DWORD *)v10 = a1;
*((_WORD *)v10 + 2) = a2;
*((_QWORD *)v10 + 5) = a3;
*((_QWORD *)v10 + 4) = 0LL;
*((_QWORD *)v10 + 1) = 0LL;
peerlist_size = v9 + 1;
}
}
}

original_code:
void peer_add(u_int32_t ipaddr, u_int16_t port, void *data) {
 size_t i;
 if (!ipaddr || !port) return;
 for (i = 0; i < peerlist_size; ++i) {
  if (ipaddr == peerlist[i].ipaddr) return;
 }
 if ((peerlist = realloc(peerlist, (peerlist_size + 1) * sizeof(peer_t))) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 peerlist[peerlist_size].ipaddr = ipaddr;
 peerlist[peerlist_size].port = port;
 peerlist[peerlist_size].data = data;
 peerlist[peerlist_size].last_contact = 0;
 peerlist[peerlist_size].active = 0;
 peerlist[peerlist_size].unresponsive = 0;
 peerlist[peerlist_size].index = peerlist_size;
 peerlist_size++;
 return;
}

canonical_original_code:
void peer_add(unsigned int ipaddr, unsigned int port, void *data) {
 unsigned long i;
 if (!ipaddr || !port) return;
 for (i = 0; i < peerlist_size; ++i) {
  if (ipaddr == peerlist[i].ipaddr) return;
 }
 if ((peerlist = realloc(peerlist, (peerlist_size + 1) * sizeof(peer_t))) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 peerlist[peerlist_size].ipaddr = ipaddr;
 peerlist[peerlist_size].port = port;
 peerlist[peerlist_size].data = data;
 peerlist[peerlist_size].last_contact = 0;
 peerlist[peerlist_size].active = 0;
 peerlist[peerlist_size].unresponsive = 0;
 peerlist[peerlist_size].index = peerlist_size;
 peerlist_size++;
 return;
}

variable_types:
ipaddr
unsigned int

variable_types:
port
unsigned int

variable_types:
data
void *


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

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

original_code:
int peerlist_process(size_t peerlist_index, unsigned char *msg, size_t size) {
 return -1;
}

canonical_original_code:
int peerlist_process(unsigned long peerlist_index, unsigned char *msg, unsigned long size) {
 return -1;
}

variable_types:
msg
unsigned char *

variable_types:
size
unsigned long


sigh

decompiled_code:
void __fastcall sigh(int a1)
{
unsigned __int64 i; // rbx
int v2; // edi
const char *v3; // rax
int v4; // edi
int v5; // edi

switch ( a1 )
{
case 10:
v5 = *((_DWORD *)&pfd + 1);
ctlevents |= 2u;
if ( write(v5, &unk_15C, 1uLL) == -1 )
perror("write()");
break;
case 14:
v4 = *((_DWORD *)&pfd + 1);
ctlevents |= 1u;
if ( write(v4, &unk_15C, 1uLL) == -1 )
perror("write()");
alarm(1u);
break;
case 2:
for ( i = 0LL; nfds > i; ++i )
{
v2 = *((_DWORD *)fds + 2 * i);
close(v2);
}
free(fds);
free(session);
v3 = (const char *)timestr();
fprintf(logstd, "\r[%s] -- terminated --\n", v3);
fclose(logstd);
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
unsigned __int64 i; // rbx
int v2; // edi
const char *v3; // rax
int v4; // edi
int v5; // edi

switch ( a1 )
{
case 10:
v5 = *((_DWORD *)&pfd + 1);
ctlevents |= 2u;
if ( write(v5, &unk_15C, 1uLL) == -1 )
perror("write()");
break;
case 14:
v4 = *((_DWORD *)&pfd + 1);
ctlevents |= 1u;
if ( write(v4, &unk_15C, 1uLL) == -1 )
perror("write()");
alarm(1u);
break;
case 2:
for ( i = 0LL; nfds > i; ++i )
{
v2 = *((_DWORD *)fds + 2 * i);
close(v2);
}
free(fds);
free(session);
v3 = (const char *)timestr();
fprintf(logstd, "\r[%s] -- terminated --\n", v3);
fclose(logstd);
exit(0);
}
}

original_code:
void sigh(int s) {
 nfds_t i;
 switch (s) {
 case 10:
  ctlevents |= 2;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  break;
 case 14:
  ctlevents |= 1;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  alarm(1);
  break;
 case 2:
  for (i = 0; i < nfds; ++i) close(fds[i].fd);
  free(fds);
  free(session);
  fprintf(logstd, "\r[%s] -- terminated --\n", timestr());
  fclose(logstd);
  exit(0);
 default:
  break;
 }
 return;
}

canonical_original_code:
void sigh(int s) {
 unsigned long i;
 switch (s) {
 case 10:
  ctlevents |= 2;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  break;
 case 14:
  ctlevents |= 1;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  alarm(1);
  break;
 case 2:
  for (i = 0; i < nfds; ++i) close(fds[i].fd);
  free(fds);
  free(session);
  fprintf(logstd, "\r[%s] -- terminated --\n", timestr());
  fclose(logstd);
  exit(0);
 default:
  break;
 }
 return;
}

variable_types: s int


decompiled_code:
_QWORD *img_alloc()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = malloc(0x18uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
}
else
{
puts("IMGALLOC: Couldn't allocate image structure");
}
return v1;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = malloc(0x18uLL);
v1 = v0;
if ( v0 )
{
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
}
else
{
puts("IMGALLOC: Couldn't allocate image structure");
}
return v1;
}

original_code:
IMAGE *img_alloc()
{
  IMAGE *new;
  new = (IMAGE *) malloc (sizeof (IMAGE));
  if (new == ((void *)0)) {
    printf("IMGALLOC: Couldn't allocate image structure\n");
    return (((void *)0));
  }
  new->rows = 0;
  new->cols = 0;
  new->name = ((void *)0);
  new->data = ((void *)0);
  return (new);
}

canonical_original_code:
struct IMAGE *img_alloc()
{
  struct IMAGE *new;
  new = (struct IMAGE *) malloc (sizeof (IMAGE));
  if (new == ((void *)0)) {
    printf("IMGALLOC: Couldn't allocate image structure\n");
    return (((void *)0));
  }
  new->rows = 0;
  new->cols = 0;
  new->name = ((void *)0);
  new->data = ((void *)0);
  return (new);
}

variable_types: struct IMAGE { char *name; int rows; int *data; } *


decompiled_code:
_QWORD *imgl_alloc()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = malloc(0x10uLL);
v1 = v0;
if ( v0 )
{
*(_DWORD *)v0 = 0;
v0[1] = 0LL;
}
else
{
puts("IMGL_ALLOC: Couldn't allocate image list");
}
return v1;
}

canonical_decompiled_code:
_QWORD *func6()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = malloc(0x10uLL);
v1 = v0;
if ( v0 )
{
*(_DWORD *)v0 = 0;
v0[1] = 0LL;
}
else
{
puts("IMGL_ALLOC: Couldn't allocate image list");
}
return v1;
}

original_code:
IMAGELIST *imgl_alloc()
{
  IMAGELIST *new;
  new = (IMAGELIST *) malloc (sizeof (IMAGELIST));
  if (new == ((void *)0)) {
    printf("IMGL_ALLOC: Couldn't allocate image list\n");
    return(((void *)0));
  }
  new->n = 0;
  new->list = ((void *)0);
  return (new);
}

canonical_original_code:
struct IMAGELIST *imgl_alloc()
{
  struct IMAGELIST *new;
  new = (struct IMAGELIST *) malloc (sizeof (IMAGELIST));
  if (new == ((void *)0)) {
    printf("IMGL_ALLOC: Couldn't allocate image list\n");
    return(((void *)0));
  }
  new->n = 0;
  new->list = ((void *)0);
  return (new);
}

variable_types: struct IMAGELIST { int n; struct IMAGE { char *name; int rows; int *data; } **list; } *

user_defined_types: struct IMAGELIST { int n; struct IMAGE **list; }


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

result = calloc(1uLL, 0x18uLL);
if ( result )
result[4] = -1;
return result;
}

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

result = calloc(1uLL, 0x18uLL);
if ( result )
result[4] = -1;
return result;
}

original_code:
static struct alps_serio_dev *alps_serio_alloc(void)
{
 struct alps_serio_dev *dev;
 dev = calloc(1, sizeof(*dev));
 if (!dev)
  return ((void *)0);
 dev->serio_fd = -1;
 return dev;
}

canonical_original_code:
static struct alps_serio_dev *alps_serio_alloc()
{
 struct alps_serio_dev *dev;
 dev = calloc(1, sizeof(*dev));
 if (!dev)
  return ((void *)0);
 dev->serio_fd = -1;
 return dev;
}

variable_types: struct alps_serio_dev { int proto_version; char *serio_drvctl_path; int serio_fd; } *


decompiled_code:
__int64 __fastcall do_write(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
ssize_t v5; // rax

v4 = 0LL;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return 0xFFFFFFFFLL;
}
else
{
if ( !v5 )
{
puts("Unexpected end of file");
return 0xFFFFFFFFLL;
}
v4 += v5;
}
}
while ( v4 < a3 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
ssize_t v5; // rax

v4 = 0LL;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return 0xFFFFFFFFLL;
}
else
{
if ( !v5 )
{
puts("Unexpected end of file");
return 0xFFFFFFFFLL;
}
v4 += v5;
}
}
while ( v4 < a3 );
return 0LL;
}

original_code:
static int do_write(int fd, const void *buf, size_t count)
{
 size_t bytes_written = 0;
 do {
  size_t ret = write(fd, (const char *)buf + bytes_written,
       count - bytes_written);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_written += ret;
 } while (bytes_written < count);
 return 0;
}

canonical_original_code:
static int do_write(int fd, void *buf, unsigned long count)
{
 unsigned long bytes_written = 0;
 do {
  unsigned long ret = write(fd, (const char *)buf + bytes_written,
       count - bytes_written);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_written += ret;
 } while (bytes_written < count);
 return 0;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types:
ret
unsigned long


decompiled_code:
int __fastcall serio_mouse_deinit(__int64 a1)
{
int v2; // edi
int v3; // eax
int v4; // ebp
int v6; // [rsp+0h] [rbp-18h]

v2 = *(_DWORD *)(a1 + 16);
if ( v2 != -1 )
{
close(v2);
*(_DWORD *)(a1 + 16) = -1;
}
v3 = open(*(const char **)(a1 + 8), 1);
v4 = v3;
if ( v3 == -1 )
return v6;
write(v3, "psmouse", 7uLL);
return close(v4);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
int v2; // edi
int v3; // eax
int v4; // ebp
int v6; // [rsp+0h] [rbp-18h]

v2 = *(_DWORD *)(a1 + 16);
if ( v2 != -1 )
{
close(v2);
*(_DWORD *)(a1 + 16) = -1;
}
v3 = open(*(const char **)(a1 + 8), 1);
v4 = v3;
if ( v3 == -1 )
return v6;
write(v3, "psmouse", 7uLL);
return close(v4);
}

original_code:
static void serio_mouse_deinit(struct alps_serio_dev *dev)
{
 int fd;
 if (dev->serio_fd != -1) {
  close(dev->serio_fd);
  dev->serio_fd = -1;
 }
 fd = open(dev->serio_drvctl_path, 01);
 if (fd != -1) {
  write(fd, "psmouse", 7);
  close(fd);
 }
}

user_defined_types: struct alps_serio_dev { int proto_version; char *serio_drvctl_path; int serio_fd; }


decompiled_code:
__int64 __fastcall serio_mouse_init(__int64 a1)
{
void *v2; // rax
void *v3; // r12
size_t v4; // rbx
int v5; // eax
int v6; // ebp
int *v7; // rax
char *v8; // rax
unsigned int v9; // er13
int v10; // eax
int v12; // eax
int v13; // ebx
int v14; // edi
char *v15; // rax
int *v16; // rax
char *v17; // rax
__int128 v18[4]; // [rsp+0h] [rbp-B8h] BYREF
glob_t pglob; // [rsp+40h] [rbp-78h] BYREF

v2 = malloc(0x1000uLL);
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
pglob.gl_offs = 0LL;
if ( glob("/sys/bus/serio/devices/serio*", 8199, 0LL, &pglob) )
{
free(v3);
return (unsigned int)-1;
}
if ( !pglob.gl_pathc )
{
LABEL_11:
v9 = -1;
goto LABEL_12;
}
v4 = 0LL;
while ( 1 )
{
while ( 1 )
{
v18[0] = 0LL;
v18[1] = 0LL;
v18[2] = 0LL;
v18[3] = 0LL;
snprintf((char *)v3, 0x1000uLL, "%sdescription", pglob.gl_pathv[v4]);
v5 = open((const char *)v3, 0);
v6 = v5;
if ( v5 != -1 )
break;
v7 = _errno_location();
++v4;
v8 = strerror(*v7);
printf("Could not open %s for reading: %s\n", (const char *)v3, v8);
if ( pglob.gl_pathc <= v4 )
goto LABEL_11;
}
if ( read(v5, v18, 0x40uLL) <= 0 )
{
printf("Could not read from %s\n", (const char *)v3);
goto LABEL_6;
}
if ( !strncmp((const char *)v18, "i8042 AUX port", 0xEuLL) )
break;
LABEL_6:
++v4;
close(v6);
if ( pglob.gl_pathc <= v4 )
goto LABEL_11;
}
printf("Found serio mouse at %s\n", pglob.gl_pathv[v4]);
close(v6);
snprintf((char *)v3, 0x1000uLL, "%sdrvctl", pglob.gl_pathv[v4]);
v12 = open((const char *)v3, 1);
v9 = v12;
if ( v12 == -1 )
{
v16 = _errno_location();
v17 = strerror(*v16);
printf("Could not open %s for writing: %s\n", (const char *)v3, v17);
return v9;
}
if ( write(v12, "serio_raw", 9uLL) == 9 )
{
v15 = strdup((const char *)v3);
*(_QWORD *)(a1 + 8) = v15;
v13 = -(v15 == 0LL);
}
else
{
v13 = -1;
printf("Error writing to %s\n", (const char *)v3);
}
v14 = v9;
v9 = v13;
close(v14);
LABEL_12:
if ( !*(_QWORD *)(a1 + 8) )
{
LABEL_18:
globfree(&pglob);
free(v3);
return (unsigned int)-(v9 != 0);
}
globfree(&pglob);
v9 = glob("/dev/serio_raw*", 1, 0LL, &pglob);
if ( v9 )
{
puts("Could not find serio_raw device node");
goto LABEL_18;
}
v10 = open(*(const char **)pglob.gl_pathv, 2);
if ( v10 == -1 )
{
v9 = -1;
printf("Failed to open device node %s\n", *(const char **)pglob.gl_pathv);
}
else
{
*(_DWORD *)(a1 + 16) = v10;
}
globfree(&pglob);
free(v3);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
void *v2; // rax
void *v3; // r12
size_t v4; // rbx
int v5; // eax
int v6; // ebp
int *v7; // rax
char *v8; // rax
unsigned int v9; // er13
int v10; // eax
int v12; // eax
int v13; // ebx
int v14; // edi
char *v15; // rax
int *v16; // rax
char *v17; // rax
__int128 v18[4]; // [rsp+0h] [rbp-B8h] BYREF
glob_t pglob; // [rsp+40h] [rbp-78h] BYREF

v2 = malloc(0x1000uLL);
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
pglob.gl_offs = 0LL;
if ( glob("/sys/bus/serio/devices/serio*", 8199, 0LL, &pglob) )
{
free(v3);
return (unsigned int)-1;
}
if ( !pglob.gl_pathc )
{
LABEL_11:
v9 = -1;
goto LABEL_12;
}
v4 = 0LL;
while ( 1 )
{
while ( 1 )
{
v18[0] = 0LL;
v18[1] = 0LL;
v18[2] = 0LL;
v18[3] = 0LL;
snprintf((char *)v3, 0x1000uLL, "%sdescription", pglob.gl_pathv[v4]);
v5 = open((const char *)v3, 0);
v6 = v5;
if ( v5 != -1 )
break;
v7 = _errno_location();
++v4;
v8 = strerror(*v7);
printf("Could not open %s for reading: %s\n", (const char *)v3, v8);
if ( pglob.gl_pathc <= v4 )
goto LABEL_11;
}
if ( read(v5, v18, 0x40uLL) <= 0 )
{
printf("Could not read from %s\n", (const char *)v3);
goto LABEL_6;
}
if ( !strncmp((const char *)v18, "i8042 AUX port", 0xEuLL) )
break;
LABEL_6:
++v4;
close(v6);
if ( pglob.gl_pathc <= v4 )
goto LABEL_11;
}
printf("Found serio mouse at %s\n", pglob.gl_pathv[v4]);
close(v6);
snprintf((char *)v3, 0x1000uLL, "%sdrvctl", pglob.gl_pathv[v4]);
v12 = open((const char *)v3, 1);
v9 = v12;
if ( v12 == -1 )
{
v16 = _errno_location();
v17 = strerror(*v16);
printf("Could not open %s for writing: %s\n", (const char *)v3, v17);
return v9;
}
if ( write(v12, "serio_raw", 9uLL) == 9 )
{
v15 = strdup((const char *)v3);
*(_QWORD *)(a1 + 8) = v15;
v13 = -(v15 == 0LL);
}
else
{
v13 = -1;
printf("Error writing to %s\n", (const char *)v3);
}
v14 = v9;
v9 = v13;
close(v14);
LABEL_12:
if ( !*(_QWORD *)(a1 + 8) )
{
LABEL_18:
globfree(&pglob);
free(v3);
return (unsigned int)-(v9 != 0);
}
globfree(&pglob);
v9 = glob("/dev/serio_raw*", 1, 0LL, &pglob);
if ( v9 )
{
puts("Could not find serio_raw device node");
goto LABEL_18;
}
v10 = open(*(const char **)pglob.gl_pathv, 2);
if ( v10 == -1 )
{
v9 = -1;
printf("Failed to open device node %s\n", *(const char **)pglob.gl_pathv);
}
else
{
*(_DWORD *)(a1 + 16) = v10;
}
globfree(&pglob);
free(v3);
return v9;
}

original_code:
static int serio_mouse_init(struct alps_serio_dev *dev)
{
 static const char serio_mouse_desc[] = "i8042 AUX port";
 char *path;
 glob_t globbuf;
 int ret;
 int i;
 int fd;
 path = malloc(4096);
 if (!path)
  return -1;
 globbuf.gl_offs = 0;
 ret = glob("/sys/bus/serio/devices/serio*",
     (1 << 0)|(1 << 1)|(1 << 2)|(1 << 13), ((void *)0), &globbuf);
 if (ret)
  goto free_path;
 ret = -1;
 for (i = 0; i < globbuf.gl_pathc; i++) {
  char read_buf[64] = {0,};
  do { if (verbose) printf("Checking path %s\n", globbuf.gl_pathv[i]); } while (0);
  snprintf(path, 4096, "%sdescription", globbuf.gl_pathv[i]);
  fd = open(path, 00);
  if (fd == -1) {
   printf("Could not open %s for reading: %s\n",
          path, strerror((*__errno_location ())));
   continue;
  }
  if (read(fd, read_buf, sizeof(read_buf)) <= 0) {
   printf("Could not read from %s\n", path);
  } else if (!strncmp(read_buf, serio_mouse_desc,
        sizeof(serio_mouse_desc) - 1)) {
   printf("Found serio mouse at %s\n", globbuf.gl_pathv[i]);
   close(fd);
   snprintf(path, 4096, "%sdrvctl", globbuf.gl_pathv[i]);
   fd = open(path, 01);
   if (fd == -1) {
    printf("Could not open %s for writing: %s\n",
           path, strerror((*__errno_location ())));
    return -1;
   }
   if (write(fd, "serio_raw", 9) != 9) {
    printf("Error writing to %s\n", path);
   } else {
    dev->serio_drvctl_path = strdup(path);
    if (dev->serio_drvctl_path)
     ret = 0;
   }
   close(fd);
   break;
  }
  close(fd);
 }
 if (dev->serio_drvctl_path) {
  globfree(&globbuf);
  ret = glob("/dev/serio_raw*", (1 << 0), ((void *)0), &globbuf);
  if (ret) {
   printf("Could not find serio_raw device node\n");
   goto free_glob;
  }
  ret = open(globbuf.gl_pathv[0], 02);
  if (ret != -1) {
   dev->serio_fd = ret;
   ret = 0;
  } else {
   printf("Failed to open device node %s\n",
    globbuf.gl_pathv[0]);
  }
 }
free_glob:
 globfree(&globbuf);
free_path:
 free(path);
 return ret ? -1 : 0;
}

canonical_original_code:
static int serio_mouse_init(struct alps_serio_dev *dev)
{
 static char serio_mouse_desc[] = "i8042 AUX port";
 char *path;
 struct glob_t globbuf;
 int ret;
 int i;
 int fd;
 path = malloc(4096);
 if (!path)
  return -1;
 globbuf.gl_offs = 0;
 ret = glob("/sys/bus/serio/devices/serio*",
     (1 << 0)|(1 << 1)|(1 << 2)|(1 << 13), ((void *)0), &globbuf);
 if (ret)
  goto free_path;
 ret = -1;
 for (i = 0; i < globbuf.gl_pathc; i++) {
  char read_buf[64] = {0,};
  do { if (verbose) printf("Checking path %s\n", globbuf.gl_pathv[i]); } while (0);
  snprintf(path, 4096, "%sdescription", globbuf.gl_pathv[i]);
  fd = open(path, 00);
  if (fd == -1) {
   printf("Could not open %s for reading: %s\n",
          path, strerror((*__errno_location ())));
   continue;
  }
  if (read(fd, read_buf, sizeof(read_buf)) <= 0) {
   printf("Could not read from %s\n", path);
  } else if (!strncmp(read_buf, serio_mouse_desc,
        sizeof(serio_mouse_desc) - 1)) {
   printf("Found serio mouse at %s\n", globbuf.gl_pathv[i]);
   close(fd);
   snprintf(path, 4096, "%sdrvctl", globbuf.gl_pathv[i]);
   fd = open(path, 01);
   if (fd == -1) {
    printf("Could not open %s for writing: %s\n",
           path, strerror((*__errno_location ())));
    return -1;
   }
   if (write(fd, "serio_raw", 9) != 9) {
    printf("Error writing to %s\n", path);
   } else {
    dev->serio_drvctl_path = strdup(path);
    if (dev->serio_drvctl_path)
     ret = 0;
   }
   close(fd);
   break;
  }
  close(fd);
 }
 if (dev->serio_drvctl_path) {
  globfree(&globbuf);
  ret = glob("/dev/serio_raw*", (1 << 0), ((void *)0), &globbuf);
  if (ret) {
   printf("Could not find serio_raw device node\n");
   goto free_glob;
  }
  ret = open(globbuf.gl_pathv[0], 02);
  if (ret != -1) {
   dev->serio_fd = ret;
   ret = 0;
  } else {
   printf("Failed to open device node %s\n",
    globbuf.gl_pathv[0]);
  }
 }
free_glob:
 globfree(&globbuf);
free_path:
 free(path);
 return ret ? -1 : 0;
}

variable_types:
path
char *

variable_types: struct glob_t { unsigned long gl_pathc; char **gl_pathv; unsigned long gl_offs; int gl_flags; void (*gl_closedir)(void *); void *(*gl_readdir)(void *); void *(*gl_opendir)(char *); int (*gl_lstat)(char *, void *); int (*gl_stat)(char *, void *); }

variable_types: ret int

variable_types: i int


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

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
free(ptr);
}

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

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
free(ptr);
}

original_code:
static void alps_serio_free(struct alps_serio_dev *dev)
{
 if (dev->serio_drvctl_path)
  free(dev->serio_drvctl_path);
 free(dev);
}


decompiled_code:
int __fastcall ps2_drain(__int64 a1)
{
int v1; // eax
__int64 v2; // r12
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = fcntl(*(_DWORD *)(a1 + 16), 3);
if ( v1 == -1 )
return puts("Could not get serio_fd flags, device will not be drained");
v2 = v1;
BYTE1(v1) |= 8u;
if ( fcntl(*(_DWORD *)(a1 + 16), 4, v1) == -1 )
return puts("Could not set serio_fd flags, device will not be drained");
while ( read(*(_DWORD *)(a1 + 16), buf, 1uLL) == 1 )
;
result = fcntl(*(_DWORD *)(a1 + 16), 4, v2);
if ( result )
result = puts("Error restoring serio_fd flags, left in nonblock mode");
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
int v1; // eax
__int64 v2; // r12
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = fcntl(*(_DWORD *)(a1 + 16), 3);
if ( v1 == -1 )
return puts("Could not get serio_fd flags, device will not be drained");
v2 = v1;
BYTE1(v1) |= 8u;
if ( fcntl(*(_DWORD *)(a1 + 16), 4, v1) == -1 )
return puts("Could not set serio_fd flags, device will not be drained");
while ( read(*(_DWORD *)(a1 + 16), buf, 1uLL) == 1 )
;
result = fcntl(*(_DWORD *)(a1 + 16), 4, v2);
if ( result )
result = puts("Error restoring serio_fd flags, left in nonblock mode");
return result;
}

original_code:
static void ps2_drain(struct alps_serio_dev *dev)
{
 unsigned char byte;
 long flags;
 flags = fcntl(dev->serio_fd, 3);
 if (flags == -1) {
  printf("Could not get serio_fd flags, device will not be drained\n");
  return;
 }
 if (fcntl(dev->serio_fd, 4, flags | 04000) == -1) {
  printf("Could not set serio_fd flags, device will not be drained\n");
  return;
 }
 while (read(dev->serio_fd, &byte, 1) == 1)
  ;
 if (fcntl(dev->serio_fd, 4, flags))
  printf("Error restoring serio_fd flags, left in nonblock mode\n");
}

variable_types:
byte
unsigned char

variable_types: flags long


decompiled_code:
__int64 __fastcall do_read(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
int v5; // eax
struct pollfd fds; // [rsp+8h] [rbp-30h] BYREF

v4 = 0LL;
fds.fd = fd;
fds.events = 1;
while ( 1 )
{
if ( (unsigned int)(poll(&fds, 1uLL, 500) + 1) <= 1 )
return 0xFFFFFFFFLL;
v5 = read(fd, (void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return 0xFFFFFFFFLL;
goto LABEL_4;
}
if ( !v5 )
break;
v4 += v5;
LABEL_4:
if ( v4 >= a3 )
return 0LL;
}
puts("Unexpected end of file");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
int v5; // eax
struct pollfd fds; // [rsp+8h] [rbp-30h] BYREF

v4 = 0LL;
fds.fd = fd;
fds.events = 1;
while ( 1 )
{
if ( (unsigned int)(poll(&fds, 1uLL, 500) + 1) <= 1 )
return 0xFFFFFFFFLL;
v5 = read(fd, (void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return 0xFFFFFFFFLL;
goto LABEL_4;
}
if ( !v5 )
break;
v4 += v5;
LABEL_4:
if ( v4 >= a3 )
return 0LL;
}
puts("Unexpected end of file");
return 0xFFFFFFFFLL;
}

original_code:
static int do_read(int fd, void *buf, size_t count)
{
 size_t bytes_read = 0;
 struct pollfd pfd;
 int ret;
 pfd.fd = fd;
 pfd.events = 0x001;
 do {
  ret = poll(&pfd, 1, 500);
  if (ret == 0 || ret == -1)
   return -1;
  ret = read(fd, (char *)buf + bytes_read, count - bytes_read);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_read += ret;
 } while (bytes_read < count);
 return 0;
}

canonical_original_code:
static int do_read(int fd, void *buf, unsigned long count)
{
 unsigned long bytes_read = 0;
 struct pollfd pfd;
 int ret;
 pfd.fd = fd;
 pfd.events = 0x001;
 do {
  ret = poll(&pfd, 1, 500);
  if (ret == 0 || ret == -1)
   return -1;
  ret = read(fd, (char *)buf + bytes_read, count - bytes_read);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_read += ret;
 } while (bytes_read < count);
 return 0;
}

variable_types: struct pollfd { int fd; short events; short revents; }


decompiled_code:
__int64 __fastcall ps2_command(__int64 a1, __int64 a2, int a3)
{
int v3; // er12
int v4; // er13
unsigned int v5; // er14
char v7; // [rsp+Eh] [rbp-2Ah] BYREF
char v8[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a3 >> 12;
v4 = BYTE1(a3) & 0xF;
v7 = a3;
if ( (((unsigned __int8)(a3 >> 12) | BYTE1(a3)) & 0xF) != 0 && !a2 )
return (unsigned int)-1;
if ( (unsigned int)do_write(*(_DWORD *)(a1 + 16), (__int64)&v7, 1uLL) )
{
v5 = -1;
puts("Error writing to serio device");
}
else
{
v5 = do_read(*(_DWORD *)(a1 + 16), (__int64)v8, 1uLL);
if ( v5 )
{
v5 = -1;
puts("Error reading ack byte");
}
else if ( v8[0] == -6 )
{
if ( !v3 )
goto LABEL_23;
if ( (unsigned int)do_write(*(_DWORD *)(a1 + 16), a2, 1uLL) )
{
v5 = -1;
puts("Error writing command data");
return v5;
}
if ( (unsigned int)do_read(*(_DWORD *)(a1 + 16), (__int64)v8, 1uLL) )
{
v5 = -1;
puts("Error reading ack byte after sending data");
return v5;
}
if ( v8[0] == -6 )
{
LABEL_23:
if ( v4 && (unsigned int)do_read(*(_DWORD *)(a1 + 16), a2, v4) )
{
v5 = -1;
puts("Error reading command response");
}
}
else
{
v5 = -1;
printf("Invalid ack byte 0x%02hhx after data\n", (unsigned __int8)v8[0]);
}
}
else
{
v5 = -1;
printf("Invalid ack byte 0x%02hhx after command\n", (unsigned __int8)v8[0]);
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3)
{
int v3; // er12
int v4; // er13
unsigned int v5; // er14
char v7; // [rsp+Eh] [rbp-2Ah] BYREF
char v8[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a3 >> 12;
v4 = BYTE1(a3) & 0xF;
v7 = a3;
if ( (((unsigned __int8)(a3 >> 12) | BYTE1(a3)) & 0xF) != 0 && !a2 )
return (unsigned int)-1;
if ( (unsigned int)func1(*(_DWORD *)(a1 + 16), (__int64)&v7, 1uLL) )
{
v5 = -1;
puts("Error writing to serio device");
}
else
{
v5 = func6(*(_DWORD *)(a1 + 16), (__int64)v8, 1uLL);
if ( v5 )
{
v5 = -1;
puts("Error reading ack byte");
}
else if ( v8[0] == -6 )
{
if ( !v3 )
goto LABEL_23;
if ( (unsigned int)func1(*(_DWORD *)(a1 + 16), a2, 1uLL) )
{
v5 = -1;
puts("Error writing command data");
return v5;
}
if ( (unsigned int)func6(*(_DWORD *)(a1 + 16), (__int64)v8, 1uLL) )
{
v5 = -1;
puts("Error reading ack byte after sending data");
return v5;
}
if ( v8[0] == -6 )
{
LABEL_23:
if ( v4 && (unsigned int)func6(*(_DWORD *)(a1 + 16), a2, v4) )
{
v5 = -1;
puts("Error reading command response");
}
}
else
{
v5 = -1;
printf("Invalid ack byte 0x%02hhx after data\n", (unsigned __int8)v8[0]);
}
}
else
{
v5 = -1;
printf("Invalid ack byte 0x%02hhx after command\n", (unsigned __int8)v8[0]);
}
}
return v5;
}

original_code:
static int ps2_command(struct alps_serio_dev *dev, unsigned char *data,
         int command)
{
 int send_bytes = (command & 0xf000) >> 12;
 int recv_bytes = (command & 0x0f00) >> 8;
 unsigned char cmd = command & 0x00ff;
 unsigned char ack_byte;
 if ((send_bytes || recv_bytes) && !data)
  return -1;
 if (do_write(dev->serio_fd, &cmd, 1)) {
  printf("Error writing to serio device\n");
  return -1;
 }
 if (do_read(dev->serio_fd, &ack_byte, 1)) {
  printf("Error reading ack byte\n");
  return -1;
 }
 if (ack_byte != 0xfa) {
  printf("Invalid ack byte 0x%02hhx after command\n", ack_byte);
  return -1;
 }
 if (send_bytes) {
  if (do_write(dev->serio_fd, data, send_bytes)) {
   printf("Error writing command data\n");
   return -1;
  } else if (do_read(dev->serio_fd, &ack_byte, 1)) {
   printf("Error reading ack byte after sending data\n");
   return -1;
  } else if (ack_byte != 0xfa) {
   printf("Invalid ack byte 0x%02hhx after data\n", ack_byte);
   return -1;
  }
 }
 if (recv_bytes && do_read(dev->serio_fd, data, recv_bytes)) {
  printf("Error reading command response\n");
  return -1;
 }
 return 0;
}

variable_types:
data
unsigned char *

variable_types: command int

variable_types:
cmd
unsigned char


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
void *v4; // rbp
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = alps_serio_alloc();
v4 = v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)serio_mouse_init((__int64)v3) )
{
puts("Error: Could not locate serio mouse");
serio_mouse_deinit((__int64)v4);
exit(1);
}
ps2_command((__int64)v4, (__int64)v5, 767);
ps2_drain((__int64)v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)ps2_command((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 230)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 230)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 230)
|| (unsigned int)ps2_command((__int64)v4, (__int64)v5, 1001) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_9;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( (unsigned int)ps2_command((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 231)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 231)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 231)
|| (unsigned int)ps2_command((__int64)v4, (__int64)v5, 1001) )
{
puts("E7 report failed, not an ALPS touchpad");
}
else
{
if ( LOBYTE(v5[0]) != 115 )
{
LABEL_24:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( HIBYTE(v5[0]) == 3 )
{
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_24;
puts("V5 ALPS touchpad found");
}
}
else
{
if ( *(_WORD *)((char *)v5 + 1) != 25602 )
goto LABEL_24;
puts("V3 ALPS touchpad found");
}
}
LABEL_9:
serio_mouse_deinit((__int64)v4);
alps_serio_free(v4);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
void *v4; // rbp
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = func0();
v4 = v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)func3((__int64)v3) )
{
puts("Error: Could not locate serio mouse");
func2((__int64)v4);
exit(1);
}
func7((__int64)v4, (__int64)v5, 767);
func5((__int64)v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)func7((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)func7((__int64)v4, 0LL, 230)
|| (unsigned int)func7((__int64)v4, 0LL, 230)
|| (unsigned int)func7((__int64)v4, 0LL, 230)
|| (unsigned int)func7((__int64)v4, (__int64)v5, 1001) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_9;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( (unsigned int)func7((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)func7((__int64)v4, 0LL, 231)
|| (unsigned int)func7((__int64)v4, 0LL, 231)
|| (unsigned int)func7((__int64)v4, 0LL, 231)
|| (unsigned int)func7((__int64)v4, (__int64)v5, 1001) )
{
puts("E7 report failed, not an ALPS touchpad");
}
else
{
if ( LOBYTE(v5[0]) != 115 )
{
LABEL_24:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_9;
}
if ( HIBYTE(v5[0]) == 3 )
{
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_24;
puts("V5 ALPS touchpad found");
}
}
else
{
if ( *(_WORD *)((char *)v5 + 1) != 25602 )
goto LABEL_24;
puts("V3 ALPS touchpad found");
}
}
LABEL_9:
func2((__int64)v4);
func4(v4);
exit(0);
}

original_code:
int main(void)
{
 struct alps_serio_dev *dev;
 unsigned char param[4];
 if (geteuid() != 0) {
  fprintf(stderr, "Error: Must be run as root\n");
  exit(1);
 }
 dev = alps_serio_alloc();
 if (!dev) {
  printf("Error: Could not allocate device\n");
  exit(1);
 }
 if (serio_mouse_init(dev)) {
  printf("Error: Could not locate serio mouse\n");
  serio_mouse_deinit(dev);
  exit(1);
 }
 ps2_command(dev, param, 0x02ff);
 ps2_drain(dev);
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E6 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] != 0 || param[1] != 0 ||
     (param[2] != 10 && param[2] != 100)) {
  printf("Invalid E6 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
  goto cleanup;
 }
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E7 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x50) {
     printf("V6 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x0a) {
     printf("V5 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x02 && param[2] == 0x64) {
     printf("V3 ALPS touchpad found\n");
 }
 else {
  printf("E7 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
 }
cleanup:
 serio_mouse_deinit(dev);
 alps_serio_free(dev);
 exit(0);
}

canonical_original_code:
int main()
{
 struct alps_serio_dev *dev;
 unsigned char param[4];
 if (geteuid() != 0) {
  fprintf(stderr, "Error: Must be run as root\n");
  exit(1);
 }
 dev = alps_serio_alloc();
 if (!dev) {
  printf("Error: Could not allocate device\n");
  exit(1);
 }
 if (serio_mouse_init(dev)) {
  printf("Error: Could not locate serio mouse\n");
  serio_mouse_deinit(dev);
  exit(1);
 }
 ps2_command(dev, param, 0x02ff);
 ps2_drain(dev);
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E6 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] != 0 || param[1] != 0 ||
     (param[2] != 10 && param[2] != 100)) {
  printf("Invalid E6 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
  goto cleanup;
 }
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E7 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x50) {
     printf("V6 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x0a) {
     printf("V5 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x02 && param[2] == 0x64) {
     printf("V3 ALPS touchpad found\n");
 }
 else {
  printf("E7 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
 }
cleanup:
 serio_mouse_deinit(dev);
 alps_serio_free(dev);
 exit(0);
}

variable_types:
param
unsigned char [4]