repo: Fydot/easyproxy

decompiled_code:
__int16 __fastcall read_args(__int64 a1, const char **a2)
{
size_t v2; // r12
__int64 v3; // rbx
const unsigned __int16 **v4; // rax
__int64 v5; // rdx
int v6; // eax
__int16 v7; // ax
in_addr_t v8; // eax
struct servent *v9; // rax
struct hostent *v10; // rax
int v11; // eax
char dest[32]; // [rsp+0h] [rbp-90h] BYREF
char cp[64]; // [rsp+20h] [rbp-70h] BYREF
char name[48]; // [rsp+60h] [rbp-30h] BYREF

strcpy(dest, a2[1]);
strcpy(cp, a2[2]);
strcpy(name, a2[3]);
v2 = strlen(dest);
v3 = 0LL;
while ( v2 != v3 )
{
v4 = __ctype_b_loc();
v5 = dest[v3++];
if ( ((*v4)[v5] & 0x800) == 0 )
{
LOWORD(v6) = puts("Invalid Args!");
return v6;
}
}
v7 = atoi(dest);
proxy_port = (unsigned __int16)_bswap_16(v7);
*(_QWORD *)&server_host.sa_family = 2LL;
*(_QWORD *)&server_host.sa_data[6] = 0LL;
v8 = inet_addr(cp);
if ( v8 == -1 )
{
v10 = gethostbyname(cp);
if ( !v10 )
{
LOWORD(v6) = printf("%s: Unknown Host\n", cp);
return v6;
}
memmove(&server_host.sa_data[2], *(const void **)v10->h_addr_list, v10->h_length);
}
else
{
*(_QWORD *)&server_host.sa_data[2] = v8;
}
v9 = getservbyname(name, "tcp");
if ( v9 )
{
v6 = v9->s_port;
*(_WORD *)server_host.sa_data = v6;
}
else
{
v11 = atoi(name);
if ( v11 <= 0 )
{
LOWORD(v6) = printf("%s: Invalid Service Name or Port\n", name);
}
else
{
LOWORD(v6) = _bswap_16(v11);
*(_WORD *)server_host.sa_data = v6;
}
}
return v6;
}

canonical_decompiled_code:
__int16 __fastcall func10(__int64 a1, const char **a2)
{
size_t v2; // r12
__int64 v3; // rbx
const unsigned __int16 **v4; // rax
__int64 v5; // rdx
int v6; // eax
__int16 v7; // ax
in_addr_t v8; // eax
struct servent *v9; // rax
struct hostent *v10; // rax
int v11; // eax
char dest[32]; // [rsp+0h] [rbp-90h] BYREF
char cp[64]; // [rsp+20h] [rbp-70h] BYREF
char name[48]; // [rsp+60h] [rbp-30h] BYREF

strcpy(dest, a2[1]);
strcpy(cp, a2[2]);
strcpy(name, a2[3]);
v2 = strlen(dest);
v3 = 0LL;
while ( v2 != v3 )
{
v4 = __ctype_b_loc();
v5 = dest[v3++];
if ( ((*v4)[v5] & 0x800) == 0 )
{
LOWORD(v6) = puts("Invalid Args!");
return v6;
}
}
v7 = atoi(dest);
proxy_port = (unsigned __int16)func8(v7);
*(_QWORD *)&server_host.sa_family = 2LL;
*(_QWORD *)&server_host.sa_data[6] = 0LL;
v8 = inet_addr(cp);
if ( v8 == -1 )
{
v10 = gethostbyname(cp);
if ( !v10 )
{
LOWORD(v6) = printf("%s: Unknown Host\n", cp);
return v6;
}
memmove(&server_host.sa_data[2], *(const void **)v10->h_addr_list, v10->h_length);
}
else
{
*(_QWORD *)&server_host.sa_data[2] = v8;
}
v9 = getservbyname(name, "tcp");
if ( v9 )
{
v6 = v9->s_port;
*(_WORD *)server_host.sa_data = v6;
}
else
{
v11 = atoi(name);
if ( v11 <= 0 )
{
LOWORD(v6) = printf("%s: Invalid Service Name or Port\n", name);
}
else
{
LOWORD(v6) = func8(v11);
*(_WORD *)server_host.sa_data = v6;
}
}
return v6;
}

original_code:
void read_args(int argc, char* args[]) {
  struct hostent *hostp;
  struct servent *servp;
  unsigned long inaddr;
  struct {
    char proxy_port[32];
    char service_host[64];
    char service_port[32];
  } pargs;
  strcpy(pargs.proxy_port, args[1]);
  strcpy(pargs.service_host, args[2]);
  strcpy(pargs.service_port, args[3]);
  int i;
  for (i = 0; i < strlen(pargs.proxy_port); ++i) {
    if (!((*__ctype_b_loc ())[(int) ((pargs.proxy_port[i]))] & (unsigned short int) _ISdigit)) {
      break;
    }
  }
  if (strlen(pargs.proxy_port) == i) {
    proxy_port = htons(atoi(pargs.proxy_port));
  }
  else {
    printf("Invalid Args!\n");
    return;
  }
  bzero(&server_host, sizeof(server_host));
  server_host.sin_family = 2;
  if ((inaddr = inet_addr(pargs.service_host)) != ((in_addr_t) 0xffffffff))
    bcopy(&inaddr,&server_host.sin_addr,sizeof(inaddr));
  else if ((hostp = gethostbyname(pargs.service_host)) != ((void *)0))
    bcopy(hostp->h_addr_list[0],&server_host.sin_addr,hostp->h_length);
  else {
    printf("%s: Unknown Host\n",pargs.service_host);
    return;
  }
  if ((servp = getservbyname(pargs.service_port, "tcp")) != ((void *)0))
    server_host.sin_port = servp->s_port;
  else if (atoi(pargs.service_port) > 0)
    server_host.sin_port = htons(atoi(pargs.service_port));
  else {
    printf("%s: Invalid Service Name or Port\n", pargs.service_port);
    return;
  }
}

canonical_original_code:
void read_args(int argc, char *args[]) {
  struct hostent *hostp;
  struct servent *servp;
  unsigned long inaddr;
  struct { char proxy_port[32]; char service_host[64]; char service_port[32]; } pargs;
  strcpy(pargs.proxy_port, args[1]);
  strcpy(pargs.service_host, args[2]);
  strcpy(pargs.service_port, args[3]);
  int i;
  for (i = 0; i < strlen(pargs.proxy_port); ++i) {
    if (!((*__ctype_b_loc ())[(int) ((pargs.proxy_port[i]))] & (unsigned short) _ISdigit)) {
      break;
    }
  }
  if (strlen(pargs.proxy_port) == i) {
    proxy_port = htons(atoi(pargs.proxy_port));
  }
  else {
    printf("Invalid Args!\n");
    return;
  }
  bzero(&server_host, sizeof(server_host));
  server_host.sin_family = 2;
  if ((inaddr = inet_addr(pargs.service_host)) != ((unsigned int) 0xffffffff))
    bcopy(&inaddr,&server_host.sin_addr,sizeof(inaddr));
  else if ((hostp = gethostbyname(pargs.service_host)) != ((void *)0))
    bcopy(hostp->h_addr_list[0],&server_host.sin_addr,hostp->h_length);
  else {
    printf("%s: Unknown Host\n",pargs.service_host);
    return;
  }
  if ((servp = getservbyname(pargs.service_port, "tcp")) != ((void *)0))
    server_host.sin_port = servp->s_port;
  else if (atoi(pargs.service_port) > 0)
    server_host.sin_port = htons(atoi(pargs.service_port));
  else {
    printf("%s: Invalid Service Name or Port\n", pargs.service_port);
    return;
  }
}

variable_types: argc int

variable_types:
args
char *[]

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

variable_types: struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; } *

variable_types:
inaddr
unsigned long


onproxy

decompiled_code:
int __fastcall onproxy(int a1)
{
int v1; // er15
int v2; // eax
int v3; // ebx
__int64 v4; // r14
int v5; // er12
__int64 v6; // r13
size_t v7; // rax
int v9; // eax
int v10; // eax
char s[4096]; // [rsp+10h] [rbp-10B0h] BYREF
fd_set readfds; // [rsp+1010h] [rbp-B0h] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
puts("failed to create socket to host");
v2 = connect(v1, &server_host, 0x10u);
if ( v2 == 101 )
goto LABEL_8;
if ( v2 <= 101 )
{
if ( !v2 )
goto LABEL_6;
goto LABEL_9;
}
if ( (unsigned int)(v2 - 110) <= 1 )
{
LABEL_8:
strcpy(&s[strlen(s)], "\r\n");
v7 = strlen(s);
write(a1, s, v7);
return close(a1);
}
LABEL_9:
puts("failed to connect to host");
LABEL_6:
v3 = a1 / 64;
v4 = 1LL << (a1 % 64);
v5 = v1 / 64;
v6 = 1LL << (v1 % 64);
while ( 1 )
{
do
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v3] |= v4;
readfds.fds_bits[v5] |= v6;
if ( select(1024, &readfds, 0LL, 0LL, 0LL) < 0 )
puts("select failed");
memset(s, 0, sizeof(s));
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
v9 = read(a1, s, 0x1000uLL);
if ( v9 <= 0 )
return close(v1);
write(v1, s, v9);
}
}
while ( (readfds.fds_bits[v5] & v6) == 0 );
v10 = read(v1, s, 0x1000uLL);
if ( v10 <= 0 )
break;
write(a1, s, v10);
}
return close(v1);
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int v1; // er15
int v2; // eax
int v3; // ebx
__int64 v4; // r14
int v5; // er12
__int64 v6; // r13
size_t v7; // rax
int v9; // eax
int v10; // eax
char s[4096]; // [rsp+10h] [rbp-10B0h] BYREF
fd_set readfds; // [rsp+1010h] [rbp-B0h] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
puts("failed to create socket to host");
v2 = connect(v1, &server_host, 0x10u);
if ( v2 == 101 )
goto LABEL_8;
if ( v2 <= 101 )
{
if ( !v2 )
goto LABEL_6;
goto LABEL_9;
}
if ( (unsigned int)(v2 - 110) <= 1 )
{
LABEL_8:
strcpy(&s[strlen(s)], "\r\n");
v7 = strlen(s);
write(a1, s, v7);
return close(a1);
}
LABEL_9:
puts("failed to connect to host");
LABEL_6:
v3 = a1 / 64;
v4 = 1LL << (a1 % 64);
v5 = v1 / 64;
v6 = 1LL << (v1 % 64);
while ( 1 )
{
do
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v3] |= v4;
readfds.fds_bits[v5] |= v6;
if ( select(1024, &readfds, 0LL, 0LL, 0LL) < 0 )
puts("select failed");
memset(s, 0, sizeof(s));
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
v9 = read(a1, s, 0x1000uLL);
if ( v9 <= 0 )
return close(v1);
write(v1, s, v9);
}
}
while ( (readfds.fds_bits[v5] & v6) == 0 );
v10 = read(v1, s, 0x1000uLL);
if ( v10 <= 0 )
break;
write(a1, s, v10);
}
return close(v1);
}

original_code:
void onproxy (int usersockfd) {
  int isosockfd;
  fd_set rdfdset;
  int connstat;
  int iolen;
  char buf[4096];
  if ((isosockfd = socket(2,SOCK_STREAM,0)) < 0) puts("failed to create socket to host");
  connstat = connect(isosockfd,(struct sockaddr *) &server_host, sizeof(server_host));
  switch (connstat) {
    case 0:
      break;
    case 101:
    case 110:
    case 111:
      strcat(buf,"\r\n");
      write(usersockfd,buf,strlen(buf));
      close(usersockfd);
      return;
      break;
    default:
      puts("failed to connect to host");
  }
  while (1) {
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfdset)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))));
    if (select(1024,&rdfdset,((void *)0),((void *)0),((void *)0)) < 0) {
      puts("select failed");
    }
    bzero(&buf, sizeof(buf));
    if (((((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(usersockfd,buf,sizeof(buf))) <= 0) break;
      write(isosockfd,buf,iolen);
    }
    if (((((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(isosockfd,buf,sizeof(buf))) <= 0) break;
      write(usersockfd,buf,iolen);
    }
  }
  close(isosockfd);
}

canonical_original_code:
void onproxy(int usersockfd) {
  int isosockfd;
  struct fd_set rdfdset;
  int connstat;
  int iolen;
  char buf[4096];
  if ((isosockfd = socket(2,SOCK_STREAM,0)) < 0) puts("failed to create socket to host");
  connstat = connect(isosockfd,(struct sockaddr *) &server_host, sizeof(server_host));
  switch (connstat) {
    case 0:
      break;
    case 101:
    case 110:
    case 111:
      strcat(buf,"\r\n");
      write(usersockfd,buf,strlen(buf));
      close(usersockfd);
      return;
      break;
    default:
      puts("failed to connect to host");
  }
  while (1) {
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfdset)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))));
    if (select(1024,&rdfdset,((void *)0),((void *)0),((void *)0)) < 0) {
      puts("select failed");
    }
    bzero(&buf, sizeof(buf));
    if (((((&rdfdset)->__fds_bits)[((usersockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((usersockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(usersockfd,buf,sizeof(buf))) <= 0) break;
      write(isosockfd,buf,iolen);
    }
    if (((((&rdfdset)->__fds_bits)[((isosockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((isosockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      if ((iolen = read(isosockfd,buf,sizeof(buf))) <= 0) break;
      write(usersockfd,buf,iolen);
    }
  }
  close(isosockfd);
}

variable_types: usersockfd int

variable_types: isosockfd int

variable_types:
rdfdset
struct fd_set { long __fds_bits[-1]; }

variable_types: connstat int

variable_types: iolen int

variable_types:
buf
char [4096]

return_type: void

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


main

canonical_name: main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
int v5; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-34h] BYREF
struct sockaddr v8; // [rsp+10h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-20h] BYREF

read_args(*(__int64 *)&argc, argv);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = proxy_port;
v3 = socket(2, 1, 0);
v4 = v3;
if ( v3 < 0 )
{
fwrite("Failed to create server socket\r\n", 1uLL, 0x20uLL, stderr);
}
else
{
if ( bind(v3, &addr, 0x10u) >= 0 )
{
listen(v4, 25);
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v5 = accept(v4, &v8, &addr_len);
if ( v5 < 0 )
break;
LABEL_6:
if ( !fork() )
{
close(v4);
onproxy(v5);
return 1;
}
}
if ( *__errno_location() != 4 )
{
puts("failed to accept connection");
goto LABEL_6;
}
}
}
fwrite("faild to bind server socket to specified port\r\n", 1uLL, 0x2FuLL, stderr);
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
int v5; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-34h] BYREF
struct sockaddr v8; // [rsp+10h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-20h] BYREF

func10(*(__int64 *)&argc, argv);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func9(0);
*(_WORD *)addr.sa_data = proxy_port;
v3 = socket(2, 1, 0);
v4 = v3;
if ( v3 < 0 )
{
fwrite("Failed to create server socket\r\n", 1uLL, 0x20uLL, stderr);
}
else
{
if ( bind(v3, &addr, 0x10u) >= 0 )
{
listen(v4, 25);
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v5 = accept(v4, &v8, &addr_len);
if ( v5 < 0 )
break;
LABEL_6:
if ( !fork() )
{
close(v4);
func11(v5);
return 1;
}
}
if ( *__errno_location() != 4 )
{
puts("failed to accept connection");
goto LABEL_6;
}
}
}
fwrite("faild to bind server socket to specified port\r\n", 1uLL, 0x2FuLL, stderr);
}
return 1;
}

original_code:
int main(int argc, char* args[]) {
  struct sockaddr_in servaddr, cliaddr;
  int clilen;
  int childpid;
  int sockfd, newsockfd;
  read_args(argc, args);
  bzero((char *) &servaddr, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  servaddr.sin_port = proxy_port;
  if ((sockfd = socket(2,SOCK_STREAM,0)) < 0) {
    fputs("Failed to create server socket\r\n",stderr);
    return 1;
  }
  if (bind(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr)) < 0) {
    fputs("faild to bind server socket to specified port\r\n",stderr);
    return 1;
  }
  listen(sockfd,25);
  while (1) {
    clilen = sizeof(cliaddr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cliaddr, (socklen_t*)&(clilen));
    if (newsockfd < 0 && (*__errno_location ()) == 4) {
      continue;
    }
    else if (newsockfd < 0) {
      puts("failed to accept connection");
    }
    if ((childpid = fork()) == 0) {
      close(sockfd);
      onproxy(newsockfd);
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *args[]) {
  struct sockaddr_in servaddr; struct sockaddr_in cliaddr;
  int clilen;
  int childpid;
  int sockfd; int newsockfd;
  read_args(argc, args);
  bzero((char *) &servaddr, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  servaddr.sin_port = proxy_port;
  if ((sockfd = socket(2,SOCK_STREAM,0)) < 0) {
    fputs("Failed to create server socket\r\n",stderr);
    return 1;
  }
  if (bind(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr)) < 0) {
    fputs("faild to bind server socket to specified port\r\n",stderr);
    return 1;
  }
  listen(sockfd,25);
  while (1) {
    clilen = sizeof(cliaddr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cliaddr, (unsigned int *)&(clilen));
    if (newsockfd < 0 && (*__errno_location ()) == 4) {
      continue;
    }
    else if (newsockfd < 0) {
      puts("failed to accept connection");
    }
    if ((childpid = fork()) == 0) {
      close(sockfd);
      onproxy(newsockfd);
      return 1;
    }
  }
  return 0;
}

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

variable_types: clilen int

variable_types: childpid int

variable_types: sockfd int

variable_types: newsockfd int

return_type: int

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


repo: GBert/openwrt-files

decompiled_code:
const char *ftdi_bb_error()
{
return "Can't open FTDI I/O";
}

canonical_decompiled_code:
const char *func0()
{
return "Can't open FTDI I/O";
}

original_code:
char *
ftdi_bb_error(void)
{
 return "Can't open FTDI I/O";
}

return_type: char *


decompiled_code:
int __fastcall ftbi_bb_dumpbuffer(__int64 a1, unsigned int a2)
{
int result; // eax
unsigned int i; // er12
__int16 v4; // cx
unsigned int v5; // ebx

result = printf(
"%s VALUE=%08ld NBITS=%d MSB=%d IX=%d\n",
dir_2[*(_BYTE *)a1 & 1],
*(_QWORD *)(a1 + 2),
*(unsigned __int8 *)(a1 + 1),
(unsigned __int8)*(&p + 3145),
a2);
if ( a2 )
{
for ( i = 4; ; i += 4 )
{
v5 = i - 4;
printf("[%02X]", i - 4);
do
{
if ( *(_BYTE *)a1 )
v4 = *((_WORD *)&p + 1567);
else
v4 = *((_WORD *)&p + 1566);
printf(
" %c%c",
(unsigned __int8)((((int)(unsigned __int8)buffer[v5] >> *((_WORD *)&p + 1565)) & 1) == 0 ? 95 : 67),
(unsigned __int8)(((unsigned __int8)(1 << v4) & (unsigned __int8)buffer[v5]) == 0 ? 95 : 72));
++v5;
}
while ( v5 != i );
putchar(10);
result = i + 4;
if ( a2 <= i )
break;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned int a2)
{
int result; // eax
unsigned int i; // er12
__int16 v4; // cx
unsigned int v5; // ebx

result = printf(
"%s VALUE=%08ld NBITS=%d MSB=%d IX=%d\n",
dir_2[*(_BYTE *)a1 & 1],
*(_QWORD *)(a1 + 2),
*(unsigned __int8 *)(a1 + 1),
(unsigned __int8)*(&p + 3145),
a2);
if ( a2 )
{
for ( i = 4; ; i += 4 )
{
v5 = i - 4;
printf("[%02X]", i - 4);
do
{
if ( *(_BYTE *)a1 )
v4 = *((_WORD *)&p + 1567);
else
v4 = *((_WORD *)&p + 1566);
printf(
" %c%c",
(unsigned __int8)((((int)(unsigned __int8)buffer[v5] >> *((_WORD *)&p + 1565)) & 1) == 0 ? 95 : 67),
(unsigned __int8)(((unsigned __int8)(1 << v4) & (unsigned __int8)buffer[v5]) == 0 ? 95 : 72));
++v5;
}
while ( v5 != i );
putchar(10);
result = i + 4;
if ( a2 <= i )
break;
}
}
return result;
}

original_code:
static void
ftbi_bb_dumpbuffer(struct ftdi_bb_shift *shift, uint32_t ix)
{
 uint8_t c, d, bit;
 static char read[] = "READ", write[] = "WRITE", *dir[] = {write, read};
 printf("%s VALUE=%08" "l" "d" " NBITS=%d MSB=%d IX=%d\n",
  dir[shift->dir & 1], shift->bits, shift->nbits, p.msb_first, ix);
 for (uint32_t i = 0; i < ix; i += (4)) {
  printf("[%02X]", i);
  for (uint32_t j = 0; j < (4); ++j) {
   if (buffer[i + j] & (1 << p.pgc))
    c = 'C';
   else
    c = '_';
   if (shift->dir)
    bit = (1 << p.pgdi);
   else
    bit = (1 << p.pgdo);
   if (buffer[i + j] & bit)
    d = 'H';
   else
    d = '_';
   printf(" %c%c", c, d);
  }
  printf("\n");
 }
}

canonical_original_code:
static void ftbi_bb_dumpbuffer(struct ftdi_bb_shift *shift, unsigned int ix)
{
 unsigned char c; unsigned char d; unsigned char bit;
 static char read[] = "READ"; static char write[] = "WRITE"; static char *dir[] = {write, read};
 printf("%s VALUE=%08" "l" "d" " NBITS=%d MSB=%d IX=%d\n",
  dir[shift->dir & 1], shift->bits, shift->nbits, p.msb_first, ix);
 for (unsigned int i = 0; i < ix; i += (4)) {
  printf("[%02X]", i);
  for (unsigned int j = 0; j < (4); ++j) {
   if (buffer[i + j] & (1 << p.pgc))
    c = 'C';
   else
    c = '_';
   if (shift->dir)
    bit = (1 << p.pgdi);
   else
    bit = (1 << p.pgdo);
   if (buffer[i + j] & bit)
    d = 'H';
   else
    d = '_';
   printf(" %c%c", c, d);
  }
  printf("\n");
 }
}

variable_types: struct ftdi_bb_shift { unsigned char dir; unsigned char nbits; unsigned long bits; } *

variable_types:
ix
unsigned int

variable_types:
c
unsigned char

variable_types:
d
unsigned char

variable_types:
bit
unsigned char

variable_types:
read
char []

variable_types:
write
char []

variable_types:
dir
char *[]

variable_types:
i
unsigned int

variable_types:
j
unsigned int


decompiled_code:
__int64 ftdi_bb_backend()
{
*((_QWORD *)&p + 395) = &ftdi_bb_ops;
return (unsigned __int8)ftdi_bb_ops;
}

canonical_decompiled_code:
__int64 func2()
{
*((_QWORD *)&p + 395) = &ftdi_bb_ops;
return (unsigned __int8)ftdi_bb_ops;
}

original_code:
uint8_t
ftdi_bb_backend(void)
{
 p.io = &ftdi_bb_ops;
 return p.io->type;
}

canonical_original_code:
unsigned char ftdi_bb_backend()
{
 p.io = &ftdi_bb_ops;
 return p.io->type;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall ftdi_bb_bitmode(char a1)
{
__int64 result; // rax
int v2; // ebx
int v3; // eax

result = 0LL;
if ( ddr != a1 )
{
ddr = a1;
v2 = 11;
while ( 1 )
{
v3 = ftdi_set_bitmode(&handle, (unsigned __int8)ddr, 4LL);
if ( v3 >= 0 )
break;
if ( !--v2 )
break;
io_usleep(10000LL);
}
result = (unsigned int)(v3 >> 31);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
__int64 result; // rax
int v2; // ebx
int v3; // eax

result = 0LL;
if ( ddr != a1 )
{
ddr = a1;
v2 = 11;
while ( 1 )
{
v3 = ftdi_set_bitmode(&handle, (unsigned __int8)ddr, 4LL);
if ( v3 >= 0 )
break;
if ( !--v2 )
break;
io_usleep(10000LL);
}
result = (unsigned int)(v3 >> 31);
}
return result;
}

original_code:
int
ftdi_bb_bitmode(uint8_t new_ddr)
{
 if (ddr != new_ddr) {
  int rc, retry = 10;
  ddr = new_ddr;
  while ((rc = ftdi_set_bitmode(&handle, ddr, BITMODE_SYNCBB)) < 0 && retry--)
   io_usleep(10000);
  if (rc < 0) {
   ;
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int ftdi_bb_bitmode(unsigned char new_ddr)
{
 if (ddr != new_ddr) {
  int rc; int retry = 10;
  ddr = new_ddr;
  while ((rc = ftdi_set_bitmode(&handle, ddr, BITMODE_SYNCBB)) < 0 && retry--)
   io_usleep(10000);
  if (rc < 0) {
   ;
   return -1;
  }
 }
 return 0;
}

variable_types: rc int

variable_types: retry int


decompiled_code:
__int64 __fastcall ftdi_bb_io(_BYTE *a1)
{
_BYTE *v1; // rbx
__int64 result; // rax
int v3; // eax
char v4[9]; // [rsp+Fh] [rbp-9h] BYREF

v1 = a1;
if ( *a1 )
{
LODWORD(a1) = __ROL4__(-2, a1[1]) & (unsigned __int8)ddr;
}
else
{
v3 = 1 << a1[1];
LOBYTE(a1) = v3 | ddr;
if ( v1[2] )
latch |= v3;
else
latch &= ~(_BYTE)v3;
}
if ( (int)ftdi_bb_bitmode((char)a1) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_write_data(&handle, &latch, 1LL) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_read_data(&handle, v4, 1LL) < 0 )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *v1 )
{
v1[2] = ((int)(unsigned __int8)v4[0] >> v1[1]) & 1;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
_BYTE *v1; // rbx
__int64 result; // rax
int v3; // eax
char v4[9]; // [rsp+Fh] [rbp-9h] BYREF

v1 = a1;
if ( *a1 )
{
LODWORD(a1) = __ROL4__(-2, a1[1]) & (unsigned __int8)ddr;
}
else
{
v3 = 1 << a1[1];
LOBYTE(a1) = v3 | ddr;
if ( v1[2] )
latch |= v3;
else
latch &= ~(_BYTE)v3;
}
if ( (int)func3((char)a1) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_write_data(&handle, &latch, 1LL) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_read_data(&handle, v4, 1LL) < 0 )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *v1 )
{
v1[2] = ((int)(unsigned __int8)v4[0] >> v1[1]) & 1;
result = 0LL;
}
return result;
}

original_code:
int
ftdi_bb_io(struct ftdi_bb_io *io)
{
 uint8_t new_ddr = ddr, port;
 if (io->dir) {
  new_ddr &= ~(1 << io->pin);
 } else {
  new_ddr |= (1 << io->pin);
  if (io->bit)
   latch |= (1 << io->pin);
  else
   latch &= ~(1 << io->pin);
 }
 if (ftdi_bb_bitmode(new_ddr) < 0)
  return -1;
 if (ftdi_write_data(&handle, &latch, 1) < 0) {
  ;
  return -1;
 }
 if (ftdi_read_data(&handle, &port, 1) < 0) {
  ;
  return -1;
 }
 if (io->dir)
  io->bit = (port & (1 << io->pin)) != 0;
 return 0;
}

canonical_original_code:
int ftdi_bb_io(struct ftdi_bb_io *io)
{
 unsigned char new_ddr = ddr; unsigned char port;
 if (io->dir) {
  new_ddr &= ~(1 << io->pin);
 } else {
  new_ddr |= (1 << io->pin);
  if (io->bit)
   latch |= (1 << io->pin);
  else
   latch &= ~(1 << io->pin);
 }
 if (ftdi_bb_bitmode(new_ddr) < 0)
  return -1;
 if (ftdi_write_data(&handle, &latch, 1) < 0) {
  ;
  return -1;
 }
 if (ftdi_read_data(&handle, &port, 1) < 0) {
  ;
  return -1;
 }
 if (io->dir)
  io->bit = (port & (1 << io->pin)) != 0;
 return 0;
}

variable_types: struct ftdi_bb_io { unsigned char dir; unsigned char pin; unsigned char bit; } *

variable_types:
port
unsigned char


decompiled_code:
__int64 ftdi_bb_open()
{
unsigned __int64 v0; // rax
__int64 result; // rax
__int16 v2; // [rsp+Dh] [rbp-Bh] BYREF
char v3; // [rsp+Fh] [rbp-9h]

if ( (int)ftdi_init(&handle) < 0 )
return 0xFFFFFFFFLL;
v0 = strtoul((const char *)(unsigned int)((_DWORD)&p + 2576), 0LL, 0);
if ( (int)ftdi_set_interface(&handle, v0) < 0 )
return 0xFFFFFFFFLL;
if ( *(&p + 2064) )
{
if ( (int)ftdi_usb_open_desc(&handle, 1027LL, 24597LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0
&& (int)ftdi_usb_open_desc(&handle, 1027LL, 24593LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0
&& (int)ftdi_usb_open_desc(&handle, 1027LL, 24592LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0
&& (int)ftdi_usb_open_desc(&handle, 1027LL, 24577LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0 )
{
return 0xFFFFFFFFLL;
}
}
else if ( (int)ftdi_usb_open(&handle, 1027LL, 24597LL) < 0
&& (int)ftdi_usb_open(&handle, 1027LL, 24593LL) < 0
&& (int)ftdi_usb_open(&handle, 1027LL, 24592LL) < 0
&& (int)ftdi_usb_open(&handle, 1027LL, 24577LL) < 0 )
{
return 0xFFFFFFFFLL;
}
if ( (int)ftdi_bb_bitmode(0) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_set_baudrate(&handle, *((unsigned int *)&p + 781)) < 0 )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *(&p + 3093) < 0 )
{
v2 = 0;
v3 = 0;
HIBYTE(v2) = *((_WORD *)&p + 1564);
ftdi_bb_io(&v2);
HIBYTE(v2) = 0;
v3 = 0;
ftdi_bb_io(&v2);
io_usleep(55000LL);
HIBYTE(v2) = 0;
v3 = 1;
ftdi_bb_io(&v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int64 v0; // rax
__int64 result; // rax
__int16 v2; // [rsp+Dh] [rbp-Bh] BYREF
char v3; // [rsp+Fh] [rbp-9h]

if ( (int)ftdi_init(&handle) < 0 )
return 0xFFFFFFFFLL;
v0 = strtoul((const char *)(unsigned int)((_DWORD)&p + 2576), 0LL, 0);
if ( (int)ftdi_set_interface(&handle, v0) < 0 )
return 0xFFFFFFFFLL;
if ( *(&p + 2064) )
{
if ( (int)ftdi_usb_open_desc(&handle, 1027LL, 24597LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0
&& (int)ftdi_usb_open_desc(&handle, 1027LL, 24593LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0
&& (int)ftdi_usb_open_desc(&handle, 1027LL, 24592LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0
&& (int)ftdi_usb_open_desc(&handle, 1027LL, 24577LL, 0LL, (unsigned int)((_DWORD)&p + 2064)) < 0 )
{
return 0xFFFFFFFFLL;
}
}
else if ( (int)ftdi_usb_open(&handle, 1027LL, 24597LL) < 0
&& (int)ftdi_usb_open(&handle, 1027LL, 24593LL) < 0
&& (int)ftdi_usb_open(&handle, 1027LL, 24592LL) < 0
&& (int)ftdi_usb_open(&handle, 1027LL, 24577LL) < 0 )
{
return 0xFFFFFFFFLL;
}
if ( (int)func3(0) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_set_baudrate(&handle, *((unsigned int *)&p + 781)) < 0 )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *(&p + 3093) < 0 )
{
v2 = 0;
v3 = 0;
HIBYTE(v2) = *((_WORD *)&p + 1564);
func4(&v2);
HIBYTE(v2) = 0;
v3 = 0;
func4(&v2);
io_usleep(55000LL);
HIBYTE(v2) = 0;
v3 = 1;
func4(&v2);
result = 0LL;
}
return result;
}

original_code:
int
ftdi_bb_open(void)
{
 if (ftdi_init(&handle) < 0) {
  ;
  return -1;
 }
 if (ftdi_set_interface(&handle, strtoul(p.iface, ((void *)0), 0)) < 0) {
  ;
  return -1;
 }
 if (*p.serial) {
  if ((ftdi_usb_open_desc(&handle, 0x0403, 0x6015, ((void *)0), p.serial) < 0) &&
   (ftdi_usb_open_desc(&handle, 0x0403, 0x6011, ((void *)0), p.serial) < 0) &&
   (ftdi_usb_open_desc(&handle, 0x0403, 0x6010, ((void *)0), p.serial) < 0) &&
   (ftdi_usb_open_desc(&handle, 0x0403, 0x6001, ((void *)0), p.serial) < 0)) {
   ;
   return -1;
  }
 } else {
  if ((ftdi_usb_open(&handle, 0x0403, 0x6015) < 0) &&
   (ftdi_usb_open(&handle, 0x0403, 0x6011) < 0) &&
   (ftdi_usb_open(&handle, 0x0403, 0x6010) < 0) &&
   (ftdi_usb_open(&handle, 0x0403, 0x6001) < 0)) {
   ;
   return -1;
  }
 }
 if (ftdi_bb_bitmode(0x00) < 0)
  return -1;
 if (ftdi_set_baudrate(&handle, p.baudrate) < 0) {
  ;
  return -1;
 }
 if (p.bitrules & (0x00008000)) {
  struct ftdi_bb_io io = {.dir = 0};
  io.pin = p.vpp;
  io.bit = (0x00);
  ftdi_bb_io(&io);
  io.pin = 0;
  io.bit = (0x00);
  ftdi_bb_io(&io);
  io_usleep(55000);
  io.pin = 0;
  io.bit = (0x01);
  ftdi_bb_io(&io);
 }
 return 0;
}

canonical_original_code:
int ftdi_bb_open()
{
 if (ftdi_init(&handle) < 0) {
  ;
  return -1;
 }
 if (ftdi_set_interface(&handle, strtoul(p.iface, ((void *)0), 0)) < 0) {
  ;
  return -1;
 }
 if (*p.serial) {
  if ((ftdi_usb_open_desc(&handle, 0x0403, 0x6015, ((void *)0), p.serial) < 0) &&
   (ftdi_usb_open_desc(&handle, 0x0403, 0x6011, ((void *)0), p.serial) < 0) &&
   (ftdi_usb_open_desc(&handle, 0x0403, 0x6010, ((void *)0), p.serial) < 0) &&
   (ftdi_usb_open_desc(&handle, 0x0403, 0x6001, ((void *)0), p.serial) < 0)) {
   ;
   return -1;
  }
 } else {
  if ((ftdi_usb_open(&handle, 0x0403, 0x6015) < 0) &&
   (ftdi_usb_open(&handle, 0x0403, 0x6011) < 0) &&
   (ftdi_usb_open(&handle, 0x0403, 0x6010) < 0) &&
   (ftdi_usb_open(&handle, 0x0403, 0x6001) < 0)) {
   ;
   return -1;
  }
 }
 if (ftdi_bb_bitmode(0x00) < 0)
  return -1;
 if (ftdi_set_baudrate(&handle, p.baudrate) < 0) {
  ;
  return -1;
 }
 if (p.bitrules & (0x00008000)) {
  struct ftdi_bb_io io = {.dir = 0};
  io.pin = p.vpp;
  io.bit = (0x00);
  ftdi_bb_io(&io);
  io.pin = 0;
  io.bit = (0x00);
  ftdi_bb_io(&io);
  io_usleep(55000);
  io.pin = 0;
  io.bit = (0x01);
  ftdi_bb_io(&io);
 }
 return 0;
}

variable_types: struct ftdi_bb_io { unsigned char dir; unsigned char pin; unsigned char bit; }


decompiled_code:
__int64 ftdi_bb_close()
{
char v1; // [rsp+Dh] [rbp-Bh] BYREF
char v2; // [rsp+Eh] [rbp-Ah]
char v3; // [rsp+Fh] [rbp-9h]

if ( *(&p + 3093) < 0 )
{
v1 = 0;
v2 = *((_WORD *)&p + 1564);
v3 = 1;
ftdi_bb_io(&v1);
v2 = 0;
v3 = 0;
ftdi_bb_io(&v1);
io_usleep(55000LL);
v2 = 0;
v3 = 1;
ftdi_bb_io(&v1);
}
ftdi_disable_bitbang(&handle);
ftdi_usb_reset(&handle);
return ftdi_usb_close(&handle);
}

canonical_decompiled_code:
__int64 func6()
{
char v1; // [rsp+Dh] [rbp-Bh] BYREF
char v2; // [rsp+Eh] [rbp-Ah]
char v3; // [rsp+Fh] [rbp-9h]

if ( *(&p + 3093) < 0 )
{
v1 = 0;
v2 = *((_WORD *)&p + 1564);
v3 = 1;
func4(&v1);
v2 = 0;
v3 = 0;
func4(&v1);
io_usleep(55000LL);
v2 = 0;
v3 = 1;
func4(&v1);
}
ftdi_disable_bitbang(&handle);
ftdi_usb_reset(&handle);
return ftdi_usb_close(&handle);
}

original_code:
void
ftdi_bb_close(void)
{
 if (p.bitrules & (0x00008000)) {
         struct ftdi_bb_io io = {.dir = 0};
         io.pin = p.vpp;
         io.bit = (0x01);
         ftdi_bb_io(&io);
         io.pin = 0;
         io.bit = (0x00);
         ftdi_bb_io(&io);
         io_usleep(55000);
         io.pin = 0;
         io.bit = (0x01);
         ftdi_bb_io(&io);
 }
 ftdi_disable_bitbang(&handle);
 ftdi_usb_reset(&handle);
 ftdi_usb_close(&handle);
}

canonical_original_code:
void ftdi_bb_close()
{
 if (p.bitrules & (0x00008000)) {
         struct ftdi_bb_io io = {.dir = 0};
         io.pin = p.vpp;
         io.bit = (0x01);
         ftdi_bb_io(&io);
         io.pin = 0;
         io.bit = (0x00);
         ftdi_bb_io(&io);
         io_usleep(55000);
         io.pin = 0;
         io.bit = (0x01);
         ftdi_bb_io(&io);
 }
 ftdi_disable_bitbang(&handle);
 ftdi_usb_reset(&handle);
 ftdi_usb_close(&handle);
}

user_defined_types: struct ftdi_bb_io { unsigned char dir; unsigned char pin; unsigned char bit; }


decompiled_code:
__int64 __fastcall ftdi_bb_set_pgm(char a1)
{
__int64 result; // rax
char v2[11]; // [rsp+1h] [rbp-Bh] BYREF

result = *((unsigned __int16 *)&p + 1568);
if ( (_WORD)result != 0xFFFF )
{
v2[0] = 0;
v2[1] = result;
v2[2] = a1;
result = ftdi_bb_io(v2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
__int64 result; // rax
char v2[11]; // [rsp+1h] [rbp-Bh] BYREF

result = *((unsigned __int16 *)&p + 1568);
if ( (_WORD)result != 0xFFFF )
{
v2[0] = 0;
v2[1] = result;
v2[2] = a1;
result = func4(v2);
}
return result;
}

original_code:
void
ftdi_bb_set_pgm(uint8_t pgm)
{
 if (p.pgm != (uint16_t)(-1)) {
  struct ftdi_bb_io io = {.dir = 0, .pin = p.pgm, .bit = pgm};
  ftdi_bb_io(&io);
 }
}

canonical_original_code:
void ftdi_bb_set_pgm(unsigned char pgm)
{
 if (p.pgm != (unsigned short)(-1)) {
  struct ftdi_bb_io io = {.dir = 0, .pin = p.pgm, .bit = pgm};
  ftdi_bb_io(&io);
 }
}

variable_types:
pgm
unsigned char


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

v2[0] = 0;
v2[1] = *((_WORD *)&p + 1564);
v2[2] = a1;
return ftdi_bb_io(v2);
}

original_code:
void
ftdi_bb_set_vpp(uint8_t vpp)
{
 struct ftdi_bb_io io = {.dir = 0, .pin = p.vpp, .bit = vpp};
 ftdi_bb_io(&io);
}

canonical_original_code:
void ftdi_bb_set_vpp(unsigned char vpp)
{
 struct ftdi_bb_io io = {.dir = 0, .pin = p.vpp, .bit = vpp};
 ftdi_bb_io(&io);
}

variable_types:
vpp
unsigned char


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

v2[0] = 0;
v2[1] = *((_WORD *)&p + 1566);
v2[2] = a1;
return ftdi_bb_io(v2);
}

original_code:
void
ftdi_bb_set_pgd(uint8_t pgd)
{
 struct ftdi_bb_io io = {.dir = 0, .pin = p.pgdo, .bit = pgd};
 ftdi_bb_io(&io);
}

canonical_original_code:
void ftdi_bb_set_pgd(unsigned char pgd)
{
 struct ftdi_bb_io io = {.dir = 0, .pin = p.pgdo, .bit = pgd};
 ftdi_bb_io(&io);
}

variable_types:
pgd
unsigned char


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

v2[0] = 0;
v2[1] = *((_WORD *)&p + 1565);
v2[2] = a1;
return ftdi_bb_io(v2);
}

original_code:
void
ftdi_bb_set_pgc(uint8_t pgc)
{
 struct ftdi_bb_io io = {.dir = 0, .pin = p.pgc, .bit = pgc};
 ftdi_bb_io(&io);
}

canonical_original_code:
void ftdi_bb_set_pgc(unsigned char pgc)
{
 struct ftdi_bb_io io = {.dir = 0, .pin = p.pgc, .bit = pgc};
 ftdi_bb_io(&io);
}

variable_types:
pgc
unsigned char


decompiled_code:
__int64 ftdi_bb_get_pgd()
{
char v1[2]; // [rsp+Dh] [rbp-Bh] BYREF
unsigned __int8 v2; // [rsp+Fh] [rbp-9h]

v1[0] = 1;
v1[1] = *((_WORD *)&p + 1567);
v2 = 0;
ftdi_bb_io(v1);
return v2;
}

original_code:
uint8_t
ftdi_bb_get_pgd(void)
{
 struct ftdi_bb_io io = {.dir = 1, .pin = p.pgdi, .bit = 0};
 ftdi_bb_io(&io);
 return io.bit;
}

canonical_original_code:
unsigned char ftdi_bb_get_pgd()
{
 struct ftdi_bb_io io = {.dir = 1, .pin = p.pgdi, .bit = 0};
 ftdi_bb_io(&io);
 return io.bit;
}


decompiled_code:
__int64 __fastcall ftdi_bb_shift(__int64 a1)
{
__int16 v2; // cx
int v3; // edi
__int64 v4; // r11
char v5; // r9
unsigned __int64 v6; // rsi
int v7; // er12
int v8; // er13
char v9; // r8
_BYTE *v10; // rax
unsigned int v11; // ecx
char v12; // dl
unsigned int v13; // ebp
unsigned __int64 v14; // rdx
__int64 result; // rax
char v16; // r9
unsigned __int64 v17; // rax
__int64 v18; // rdx
__int64 v19; // rcx
unsigned __int64 v20; // rsi

if ( (unsigned __int8)(*(_BYTE *)(a1 + 1) - 1) > 0x3Fu )
_assert_fail("shift->nbits > 0 && shift->nbits < 65", "ftdi-bb.c", 0x149u, "ftdi_bb_shift");
v2 = *((_WORD *)&p + 1567);
if ( v2 == *((_WORD *)&p + 1566) )
{
v3 = *(_BYTE *)a1 ? __ROL4__(-2, v2) & (unsigned __int8)ddr : (1 << v2) | (unsigned __int8)ddr;
if ( (int)ftdi_bb_bitmode(v3) < 0 )
return 0xFFFFFFFFLL;
}
memset(buffer, 0, 0x100uLL);
v4 = *(_QWORD *)(a1 + 2);
v5 = *(&p + 3145);
v6 = 1LL;
if ( v5 )
v6 = (unsigned int)(1 << (*(_BYTE *)(a1 + 1) - 1));
if ( *(_BYTE *)(a1 + 1) )
{
v7 = 1 << *((_WORD *)&p + 1566);
v8 = 1 << *((_WORD *)&p + 1565);
v9 = ~(_BYTE)v8;
v10 = (_BYTE *)(ftdi_bb_error + 4);
v11 = 0;
do
{
if ( !*(_BYTE *)a1 )
{
if ( (v6 & v4) != 0 )
latch |= v7;
else
latch &= ~(_BYTE)v7;
}
v12 = latch;
v10[2364] = latch;
v10[2365] = v12 | v8;
v10[2366] = v12 | v8;
latch = v9 & v12;
v13 = (unsigned int)v10;
v10[2367] = v9 & v12;
v14 = v6 >> 1;
v6 *= 2LL;
if ( v5 )
v6 = v14;
++v11;
v10 += 4;
}
while ( *(unsigned __int8 *)(a1 + 1) > v11 );
if ( *((_DWORD *)&p + 779) <= 9u )
goto LABEL_20;
goto LABEL_43;
}
v13 = 0;
if ( *((_DWORD *)&p + 779) > 9u )
{
v13 = 0;
LABEL_43:
ftbi_bb_dumpbuffer(a1, v13);
LABEL_20:
if ( v13 > 0xFF )
_assert_fail("ix < FTDI_BB_BUFFER_SIZE", "ftdi-bb.c", 0x16Du, "ftdi_bb_shift");
}
if ( (int)ftdi_write_data(&handle, buffer, v13) < 0 )
return 0xFFFFFFFFLL;
if ( (int)ftdi_read_data(&handle, buffer, v13) < 0 )
return 0xFFFFFFFFLL;
result = 0LL;
if ( *(_BYTE *)a1 )
{
v16 = *(&p + 3145);
v17 = 1LL;
if ( v16 )
v17 = (unsigned int)(1 << (*(_BYTE *)(a1 + 1) - 1));
if ( v13 )
{
v18 = 0LL;
v19 = 0LL;
do
{
if ( ((1 << *((_WORD *)&p + 1567)) & (unsigned __int8)byte_943[v18]) != 0 )
v19 |= v17;
v20 = v17 >> 1;
v17 *= 2LL;
if ( v16 )
v17 = v20;
v18 += 4LL;
}
while ( v13 > (unsigned int)v18 );
}
else
{
v19 = 0LL;
}
*(_QWORD *)(a1 + 2) = v19;
result = 0LL;
if ( *((_DWORD *)&p + 779) > 9u )
{
ftbi_bb_dumpbuffer(a1, v13);
result = 0LL;
}
}
return result;
}

original_code:
int
ftdi_bb_shift(struct ftdi_bb_shift *shift)
{
 uint32_t ix = 0, offset;
 uint64_t value, value_mask;
 ((void) sizeof ((shift->nbits > 0 && shift->nbits < 65) ? 1 : 0), __extension__ ({ if (shift->nbits > 0 && shift->nbits < 65) ; else __assert_fail ("shift->nbits > 0 && shift->nbits < 65", "ftdi-bb.c", 329, __extension__ __PRETTY_FUNCTION__); }));
 if (p.pgdi == p.pgdo) {
  uint8_t new_ddr = ddr;
  if (shift->dir)
   new_ddr &= ~(1 << p.pgdi);
  else
   new_ddr |= (1 << p.pgdi);
  if (ftdi_bb_bitmode(new_ddr) < 0)
   return -1;
 }
 bzero(buffer, ((64) * (4)));
 value = shift->bits;
 value_mask = (p.msb_first) ? (1U << (shift->nbits - 1)) : (1 << 0);
 for (uint32_t i = 0; i < shift->nbits; ++i) {
  if (!shift->dir) {
   if (value & value_mask)
    latch |= (1 << p.pgdo);
   else
    latch &= ~(1 << p.pgdo);
  }
  buffer[ix++] = latch;
  latch |= (1 << p.pgc);
  buffer[ix++] = latch;
  buffer[ix++] = latch;
  latch &= ~(1 << p.pgc);
  buffer[ix++] = latch;
  value_mask = (p.msb_first) ? (value_mask >> 1) : (value_mask << 1);
 }
 if (p.debug >= 10)
  ftbi_bb_dumpbuffer(shift, ix);
 ((void) sizeof ((ix < ((64) * (4))) ? 1 : 0), __extension__ ({ if (ix < ((64) * (4))) ; else __assert_fail ("ix < FTDI_BB_BUFFER_SIZE", "ftdi-bb.c", 365, __extension__ __PRETTY_FUNCTION__); }));
 if ((ftdi_write_data(&handle, buffer, ix)) < 0) {
  ;
  return -1;
 }
 if ((ftdi_read_data(&handle, buffer, ix)) < 0) {
  ;
  return -1;
 }
 if (shift->dir) {
  value = 0;
  value_mask = (p.msb_first) ? (1U << (shift->nbits - 1)) : (1 << 0);
  offset = (p.clock_falling) ? (3) : (3);
  for (uint32_t i = 0; i < ix; i += (4)) {
   if (buffer[i + offset] & (1 << p.pgdi))
    value |= value_mask;
   value_mask = (p.msb_first) ? (value_mask >> 1) : (value_mask << 1);
  }
  shift->bits = value;
  if (p.debug >= 10)
   ftbi_bb_dumpbuffer(shift, ix);
 }
 return 0;
}

canonical_original_code:
int ftdi_bb_shift(struct ftdi_bb_shift *shift)
{
 unsigned int ix = 0; unsigned int offset;
 unsigned long value; unsigned long value_mask;
 ((void) sizeof ((shift->nbits > 0 && shift->nbits < 65) ? 1 : 0), __extension__ ({ if (shift->nbits > 0 && shift->nbits < 65) ; else __assert_fail ("shift->nbits > 0 && shift->nbits < 65", "ftdi-bb.c", 329, __extension__ __PRETTY_FUNCTION__); }));
 if (p.pgdi == p.pgdo) {
  unsigned char new_ddr = ddr;
  if (shift->dir)
   new_ddr &= ~(1 << p.pgdi);
  else
   new_ddr |= (1 << p.pgdi);
  if (ftdi_bb_bitmode(new_ddr) < 0)
   return -1;
 }
 bzero(buffer, ((64) * (4)));
 value = shift->bits;
 value_mask = (p.msb_first) ? (1U << (shift->nbits - 1)) : (1 << 0);
 for (unsigned int i = 0; i < shift->nbits; ++i) {
  if (!shift->dir) {
   if (value & value_mask)
    latch |= (1 << p.pgdo);
   else
    latch &= ~(1 << p.pgdo);
  }
  buffer[ix++] = latch;
  latch |= (1 << p.pgc);
  buffer[ix++] = latch;
  buffer[ix++] = latch;
  latch &= ~(1 << p.pgc);
  buffer[ix++] = latch;
  value_mask = (p.msb_first) ? (value_mask >> 1) : (value_mask << 1);
 }
 if (p.debug >= 10)
  ftbi_bb_dumpbuffer(shift, ix);
 ((void) sizeof ((ix < ((64) * (4))) ? 1 : 0), __extension__ ({ if (ix < ((64) * (4))) ; else __assert_fail ("ix < FTDI_BB_BUFFER_SIZE", "ftdi-bb.c", 365, __extension__ __PRETTY_FUNCTION__); }));
 if ((ftdi_write_data(&handle, buffer, ix)) < 0) {
  ;
  return -1;
 }
 if ((ftdi_read_data(&handle, buffer, ix)) < 0) {
  ;
  return -1;
 }
 if (shift->dir) {
  value = 0;
  value_mask = (p.msb_first) ? (1U << (shift->nbits - 1)) : (1 << 0);
  offset = (p.clock_falling) ? (3) : (3);
  for (unsigned int i = 0; i < ix; i += (4)) {
   if (buffer[i + offset] & (1 << p.pgdi))
    value |= value_mask;
   value_mask = (p.msb_first) ? (value_mask >> 1) : (value_mask << 1);
  }
  shift->bits = value;
  if (p.debug >= 10)
   ftbi_bb_dumpbuffer(shift, ix);
 }
 return 0;
}

variable_types: struct ftdi_bb_shift { unsigned char dir; unsigned char nbits; unsigned long bits; } *

variable_types:
ix
unsigned int

variable_types:
offset
unsigned int

variable_types:
value
unsigned long


decompiled_code:
__int64 __fastcall ftdi_bb_shift_in(char a1)
{
char v2[2]; // [rsp+6h] [rbp-12h] BYREF
__int64 v3; // [rsp+8h] [rbp-10h]

v2[0] = 1;
v2[1] = a1;
v3 = 0LL;
ftdi_bb_shift((__int64)v2);
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func13(char a1)
{
char v2[2]; // [rsp+6h] [rbp-12h] BYREF
__int64 v3; // [rsp+8h] [rbp-10h]

v2[0] = 1;
v2[1] = a1;
v3 = 0LL;
func12((__int64)v2);
return (unsigned int)v3;
}

original_code:
uint32_t
ftdi_bb_shift_in(uint8_t nbits)
{
 struct ftdi_bb_shift shift = {1, nbits, 0};
 ftdi_bb_shift(&shift);
 return (uint32_t)(shift.bits);
}

canonical_original_code:
unsigned int ftdi_bb_shift_in(unsigned char nbits)
{
 struct ftdi_bb_shift shift = {1, nbits, 0};
 ftdi_bb_shift(&shift);
 return (unsigned int)(shift.bits);
}

variable_types:
nbits
unsigned char

variable_types: struct ftdi_bb_shift { unsigned char dir; unsigned char nbits; unsigned long bits; }

return_type: unsigned int


decompiled_code:
__int64 __fastcall ftdi_bb_shift_out(unsigned int a1, char a2)
{
char v3[2]; // [rsp+6h] [rbp-12h] BYREF
__int64 v4; // [rsp+8h] [rbp-10h]

v3[0] = 0;
v3[1] = a2;
v4 = a1;
return ftdi_bb_shift((__int64)v3);
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1, char a2)
{
char v3[2]; // [rsp+6h] [rbp-12h] BYREF
__int64 v4; // [rsp+8h] [rbp-10h]

v3[0] = 0;
v3[1] = a2;
v4 = a1;
return func12((__int64)v3);
}

original_code:
void
ftdi_bb_shift_out(uint32_t bits, uint8_t nbits)
{
 struct ftdi_bb_shift shift = {0, nbits, (uint64_t)(bits)};
 ftdi_bb_shift(&shift);
}

canonical_original_code:
void ftdi_bb_shift_out(unsigned int bits, unsigned char nbits)
{
 struct ftdi_bb_shift shift = {0, nbits, (uint64_t)(bits)};
 ftdi_bb_shift(&shift);
}

variable_types:
bits
unsigned int

user_defined_types: struct ftdi_bb_shift { unsigned char dir; unsigned char nbits; unsigned long bits; }


decompiled_code:
__int64 __fastcall inhx32_hex2nibble(int a1)
{
__int64 result; // rax
unsigned __int8 v2; // al
unsigned int v3; // edi
bool v4; // cf

result = (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 48) > 9u )
{
if ( (unsigned __int8)(a1 - 97) <= 5u )
{
result = (unsigned int)(a1 - 87);
}
else
{
v2 = a1 - 65;
v3 = a1 - 55;
v4 = v2 < 6u;
result = 0LL;
if ( v4 )
result = v3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 result; // rax
unsigned __int8 v2; // al
unsigned int v3; // edi
bool v4; // cf

result = (unsigned int)(a1 - 48);
if ( (unsigned __int8)(a1 - 48) > 9u )
{
if ( (unsigned __int8)(a1 - 97) <= 5u )
{
result = (unsigned int)(a1 - 87);
}
else
{
v2 = a1 - 65;
v3 = a1 - 55;
v4 = v2 < 6u;
result = 0LL;
if ( v4 )
result = v3;
}
}
return result;
}

original_code:
static uint8_t
inhx32_hex2nibble(const char c)
{
 if (c >= '0' && c <= '9')
  return c - '0';
 if (c >= 'a' && c <= 'f')
  return c - 'a' + 10;
 if (c >= 'A' && c <= 'F')
  return c - 'A' + 10;
 return 0;
}

canonical_original_code:
static unsigned char inhx32_hex2nibble(char c)
{
 if (c >= '0' && c <= '9')
  return c - '0';
 if (c >= 'a' && c <= 'f')
  return c - 'a' + 10;
 if (c >= 'A' && c <= 'F')
  return c - 'A' + 10;
 return 0;
}

variable_types: c char


decompiled_code:
__int64 __fastcall inhx32_hex2byte(char *a1)
{
__int64 result; // rax
char v2; // bp
int v3; // ebx

result = 0LL;
if ( *a1 )
{
v2 = a1[1];
if ( v2 )
{
v3 = inhx32_hex2nibble(*a1);
result = (unsigned int)inhx32_hex2nibble(v2) | (16 * v3);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
__int64 result; // rax
char v2; // bp
int v3; // ebx

result = 0LL;
if ( *a1 )
{
v2 = a1[1];
if ( v2 )
{
v3 = func0(*a1);
result = (unsigned int)func0(v2) | (16 * v3);
}
}
return result;
}

original_code:
static uint8_t
inhx32_hex2byte(const char *s)
{
 if (s[0] && s[1])
  return inhx32_hex2nibble(s[0]) << 4 | inhx32_hex2nibble(s[1]);
 return 0;
}

canonical_original_code:
static unsigned char inhx32_hex2byte(char *s)
{
 if (s[0] && s[1])
  return inhx32_hex2nibble(s[0]) << 4 | inhx32_hex2nibble(s[1]);
 return 0;
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall inhx32_array_compare(const void *a1, const void *a2)
{
unsigned int v2; // ecx
unsigned int v3; // edx
__int64 result; // rax

v2 = **(_DWORD **)a1;
v3 = **(_DWORD **)a2;
result = 0xFFFFFFFFLL;
if ( v2 >= v3 )
result = v2 != v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const void *a1, const void *a2)
{
unsigned int v2; // ecx
unsigned int v3; // edx
__int64 result; // rax

v2 = **(_DWORD **)a1;
v3 = **(_DWORD **)a2;
result = 0xFFFFFFFFLL;
if ( v2 >= v3 )
result = v2 != v3;
return result;
}

original_code:
int
inhx32_array_compare(const void *d1, const void *d2)
{
 pic_data *data1 = *(pic_data **)d1;
 pic_data *data2 = *(pic_data **)d2;
 if (data1->address < data2->address)
  return -1;
 else if (data1->address == data2->address)
  return 0;
 return 1;
}

canonical_original_code:
int inhx32_array_compare(void *d1, void *d2)
{
 struct pic_data *data1 = *(pic_data **)d1;
 struct pic_data *data2 = *(pic_data **)d2;
 if (data1->address < data2->address)
  return -1;
 else if (data1->address == data2->address)
  return 0;
 return 1;
}

variable_types: struct pic_data { unsigned int address; unsigned short nbytes; unsigned char bytes[-1]; } *


decompiled_code:
__int64 __fastcall inhx32_error(int a1, unsigned int a2)
{
unsigned int v2; // ebx
const char *v3; // rcx

v2 = 0;
if ( a1 < 0 )
{
v2 = *((_DWORD *)&p + 785) & 1;
v3 = error_5;
if ( !v2 )
v3 = warning_4;
if ( p && (unsigned int)a1 >= 0xFFFFFFF8 )
{
switch ( a1 )
{
case -8:
fprintf(p, "%s: %s: invalid record type [0x%02X]\n", "inhx32_error", v3, a2);
break;
case -7:
fprintf(p, "%s: %s: invalid address record\n", "inhx32_error", v3);
break;
case -6:
fprintf(p, "%s: %s: invalid data record alignment\n", "inhx32_error", v3);
break;
case -5:
fprintf(p, "%s: %s: invalid data record length\n", "inhx32_error", v3);
break;
case -4:
fprintf(p, "%s: %s: invalid byte length\n", "inhx32_error", v3);
break;
case -3:
fprintf(p, "%s: %s: invalid checksum\n", "inhx32_error", v3);
break;
case -2:
fprintf(p, "%s: %s: invalid line length\n", "inhx32_error", v3);
break;
case -1:
fprintf(p, "%s: %s: invalid line prefix\n", "inhx32_error", v3);
break;
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, unsigned int a2)
{
unsigned int v2; // ebx
const char *v3; // rcx

v2 = 0;
if ( a1 < 0 )
{
v2 = *((_DWORD *)&p + 785) & 1;
v3 = error_5;
if ( !v2 )
v3 = warning_4;
if ( p && (unsigned int)a1 >= 0xFFFFFFF8 )
{
switch ( a1 )
{
case -8:
fprintf(p, "%s: %s: invalid record type [0x%02X]\n", "inhx32_error", v3, a2);
break;
case -7:
fprintf(p, "%s: %s: invalid address record\n", "inhx32_error", v3);
break;
case -6:
fprintf(p, "%s: %s: invalid data record alignment\n", "inhx32_error", v3);
break;
case -5:
fprintf(p, "%s: %s: invalid data record length\n", "inhx32_error", v3);
break;
case -4:
fprintf(p, "%s: %s: invalid byte length\n", "inhx32_error", v3);
break;
case -3:
fprintf(p, "%s: %s: invalid checksum\n", "inhx32_error", v3);
break;
case -2:
fprintf(p, "%s: %s: invalid line length\n", "inhx32_error", v3);
break;
case -1:
fprintf(p, "%s: %s: invalid line prefix\n", "inhx32_error", v3);
break;
}
}
}
return v2;
}

original_code:
static int
inhx32_error(int err, int val)
{
 static char warning[] = "warning", error[] = "error";
 char *msg;
 int rc = 0;
 if (err >= 0)
  return rc;
 if (p.error & (1)) {
  rc = 1;
  msg = error;
 } else {
  msg = warning;
 }
 if (p.f) switch (err) {
 case -1:fprintf(p.f, "%s: %s: invalid line prefix\n", __func__, msg);
  break;
 case -2:fprintf(p.f, "%s: %s: invalid line length\n", __func__, msg);
  break;
 case -3:fprintf(p.f, "%s: %s: invalid checksum\n", __func__, msg);
  break;
 case -4:fprintf(p.f, "%s: %s: invalid byte length\n", __func__, msg);
  break;
 case -5:fprintf(p.f, "%s: %s: invalid data record length\n", __func__, msg);
  break;
 case -6:fprintf(p.f, "%s: %s: invalid data record alignment\n", __func__, msg);
  break;
 case -7:fprintf(p.f, "%s: %s: invalid address record\n", __func__, msg);
  break;
 case -8:fprintf(p.f, "%s: %s: invalid record type [0x%02X]\n", __func__, msg, val);
  break;
 }
 return rc;
}

canonical_original_code:
static int inhx32_error(int err, int val)
{
 static char warning[] = "warning"; static char error[] = "error";
 char *msg;
 int rc = 0;
 if (err >= 0)
  return rc;
 if (p.error & (1)) {
  rc = 1;
  msg = error;
 } else {
  msg = warning;
 }
 if (p.f) switch (err) {
 case -1:fprintf(p.f, "%s: %s: invalid line prefix\n", __func__, msg);
  break;
 case -2:fprintf(p.f, "%s: %s: invalid line length\n", __func__, msg);
  break;
 case -3:fprintf(p.f, "%s: %s: invalid checksum\n", __func__, msg);
  break;
 case -4:fprintf(p.f, "%s: %s: invalid byte length\n", __func__, msg);
  break;
 case -5:fprintf(p.f, "%s: %s: invalid data record length\n", __func__, msg);
  break;
 case -6:fprintf(p.f, "%s: %s: invalid data record alignment\n", __func__, msg);
  break;
 case -7:fprintf(p.f, "%s: %s: invalid address record\n", __func__, msg);
  break;
 case -8:fprintf(p.f, "%s: %s: invalid record type [0x%02X]\n", __func__, msg, val);
  break;
 }
 return rc;
}

variable_types: err int

variable_types: val int

variable_types:
warning
char []

variable_types:
error
char []

variable_types:
msg
char *

variable_types: rc int


decompiled_code:
void __fastcall inhx32_array_free(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 )
{
v2 = (void **)ptr;
do
free(*v2++);
while ( v2 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
free(ptr);
}
}

original_code:
void
inhx32_array_free(pic_data **pdata, uint32_t count)
{
 for (uint32_t i = 0; i < count; ++i) {
  free(pdata[i]);
 }
 if (count) {
  free(pdata);
 }
}

canonical_original_code:
void inhx32_array_free(struct pic_data **pdata, unsigned int count)
{
 for (unsigned int i = 0; i < count; ++i) {
  free(pdata[i]);
 }
 if (count) {
  free(pdata);
 }
}

variable_types: struct pic_data { unsigned int address; unsigned short nbytes; unsigned char bytes[-1]; } **

variable_types:
count
unsigned int

variable_types:
i
unsigned int


decompiled_code:
const char *cp2104_bb_error()
{
return "Can't open CP2104 I/O";
}

canonical_decompiled_code:
const char *func0()
{
return "Can't open CP2104 I/O";
}

original_code:
char *
cp2104_bb_error(void)
{
 return "Can't open CP2104 I/O";
}


decompiled_code:
int cp2104_bb_close()
{
int result; // eax

result = close(fd);
fd = -1;
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax

result = close(fd);
fd = -1;
return result;
}

original_code:
void
cp2104_bb_close(void)
{
 close(fd);
 fd = -1;
}

canonical_original_code:
void cp2104_bb_close()
{
 close(fd);
 fd = -1;
}


decompiled_code:
__int64 cp2104_bb_backend()
{
*((_QWORD *)&p + 395) = &cp2104_bb_ops;
return (unsigned __int8)cp2104_bb_ops;
}

canonical_decompiled_code:
__int64 func2()
{
*((_QWORD *)&p + 395) = &cp2104_bb_ops;
return (unsigned __int8)cp2104_bb_ops;
}

original_code:
uint8_t
cp2104_bb_backend(void)
{
 p.io = &cp2104_bb_ops;
 return p.io->type;
}

canonical_original_code:
unsigned char cp2104_bb_backend()
{
 p.io = &cp2104_bb_ops;
 return p.io->type;
}


decompiled_code:
int __fastcall cp2104_bb_set(char a1, char a2)
{
int v2; // eax
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2 = 1 << (a1 & 3);
if ( a2 )
v2 |= 1 << ((a1 & 3) + 8);
v4[0] = v2;
return ioctl(fd, 0x8001uLL, v4);
}

canonical_decompiled_code:
int __fastcall func3(char a1, char a2)
{
int v2; // eax
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2 = 1 << (a1 & 3);
if ( a2 )
v2 |= 1 << ((a1 & 3) + 8);
v4[0] = v2;
return ioctl(fd, 0x8001uLL, v4);
}

original_code:
void
cp2104_bb_set(uint8_t pin, uint8_t val)
{
 uint32_t gpio;
 pin &= 3;
 gpio = 1 << pin;
 if (val)
  gpio |= 1 << (8 + pin);
 ioctl(fd, 0x8001, &gpio);
}

canonical_original_code:
void cp2104_bb_set(unsigned char pin, unsigned char val)
{
 unsigned int gpio;
 pin &= 3;
 gpio = 1 << pin;
 if (val)
  gpio |= 1 << (8 + pin);
 ioctl(fd, 0x8001, &gpio);
}

variable_types:
pin
unsigned char

variable_types:
val
unsigned char

variable_types:
gpio
unsigned int


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

fd = open((const char *)(unsigned int)((_DWORD)p + 2576), 2306);
if ( fd < 0 )
{
fd = -1;
result = 0xFFFFFFFFLL;
}
else
{
cp2104_bb_set(p[3134], 1);
result = 0LL;
}
return result;
}

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

fd = open((const char *)(unsigned int)((_DWORD)p + 2576), 2306);
if ( fd < 0 )
{
fd = -1;
result = 0xFFFFFFFFLL;
}
else
{
func3(p[3134], 1);
result = 0LL;
}
return result;
}


decompiled_code:
int __fastcall cp2104_bb_set_pgm(char a1)
{
int result; // eax

result = p[1568];
if ( (_WORD)result != 0xFFFF )
result = cp2104_bb_set(result, a1);
return result;
}

canonical_decompiled_code:
int __fastcall func5(char a1)
{
int result; // eax

result = p[1568];
if ( (_WORD)result != 0xFFFF )
result = func3(result, a1);
return result;
}

original_code:
void
cp2104_bb_set_pgm(uint8_t pgm)
{
 if (p.pgm != (uint16_t)(-1))
  cp2104_bb_set(p.pgm, pgm);
}

canonical_original_code:
void cp2104_bb_set_pgm(unsigned char pgm)
{
 if (p.pgm != (unsigned short)(-1))
  cp2104_bb_set(p.pgm, pgm);
}

variable_types:
pgm
unsigned char


decompiled_code:
int __fastcall cp2104_bb_set_vpp(char a1)
{
return cp2104_bb_set(p[1564], a1);
}

canonical_decompiled_code:
int __fastcall func6(char a1)
{
return func3(p[1564], a1);
}

original_code:
void
cp2104_bb_set_vpp(uint8_t vpp)
{
 cp2104_bb_set(p.vpp, vpp);
}

canonical_original_code:
void cp2104_bb_set_vpp(unsigned char vpp)
{
 cp2104_bb_set(p.vpp, vpp);
}

variable_types:
vpp
unsigned char


decompiled_code:
int __fastcall cp2104_bb_set_pgd(char a1)
{
return cp2104_bb_set(p[1566], a1);
}

canonical_decompiled_code:
int __fastcall func7(char a1)
{
return func3(p[1566], a1);
}

original_code:
void
cp2104_bb_set_pgd(uint8_t pgd)
{
 cp2104_bb_set(p.pgdo, pgd);
}

canonical_original_code:
void cp2104_bb_set_pgd(unsigned char pgd)
{
 cp2104_bb_set(p.pgdo, pgd);
}

variable_types:
pgd
unsigned char


decompiled_code:
int __fastcall cp2104_bb_set_pgc(char a1)
{
return cp2104_bb_set(p[1565], a1);
}

canonical_decompiled_code:
int __fastcall func8(char a1)
{
return func3(p[1565], a1);
}

original_code:
void
cp2104_bb_set_pgc(uint8_t pgc)
{
 cp2104_bb_set(p.pgc, pgc);
}

canonical_original_code:
void cp2104_bb_set_pgc(unsigned char pgc)
{
 cp2104_bb_set(p.pgc, pgc);
}

variable_types:
pgc
unsigned char


decompiled_code:
bool __fastcall cp2104_bb_get(char a1)
{
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

ioctl(fd, 0x8000uLL, v2);
return (v2[0] & (1 << (a1 & 3))) != 0;
}

canonical_decompiled_code:
bool __fastcall func9(char a1)
{
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

ioctl(fd, 0x8000uLL, v2);
return (v2[0] & (1 << (a1 & 3))) != 0;
}

original_code:
uint8_t
cp2104_bb_get(uint8_t pin)
{
 uint32_t gpio;
 ioctl(fd, 0x8000, &gpio);
 pin &= 3;
 if (gpio & (1 << pin))
  return (0x01);
 return (0x00);
}

canonical_original_code:
unsigned char cp2104_bb_get(unsigned char pin)
{
 unsigned int gpio;
 ioctl(fd, 0x8000, &gpio);
 pin &= 3;
 if (gpio & (1 << pin))
  return (0x01);
 return (0x00);
}


decompiled_code:
bool cp2104_bb_get_pgd()
{
return cp2104_bb_get(p[1567]);
}

canonical_decompiled_code:
bool func10()
{
return func9(p[1567]);
}

original_code:
uint8_t
cp2104_bb_get_pgd(void)
{
 return cp2104_bb_get(p.pgdi);
}

canonical_original_code:
unsigned char cp2104_bb_get_pgd()
{
 return cp2104_bb_get(p.pgdi);
}


decompiled_code:
__int64 __fastcall icspio_out(unsigned int a1, unsigned int a2, unsigned __int8 a3)
{
int v4; // ebx
unsigned int v5; // er12
__int64 v6; // rdx

if ( p[779] > 9u )
fprintf(stderr, "%s(t1=%d, t2=%d, byte=0x%02X)\n", "icspio_out", a1, a2, a3);
io_clock_out_bits(a1, a1, 0LL, 1LL);
v4 = io_stop;
if ( io_stop )
{
v5 = 0;
}
else
{
v5 = io_stop;
do
{
v6 = ((int)a3 >> v4) & 1;
v5 ^= v6;
io_clock_out_bits(a1, a1, v6, 1LL);
++v4;
}
while ( !io_stop && v4 <= 7 );
}
io_clock_out_bits(a1, a1, v5, 1LL);
io_clock_out_bits(a1, a1, 1LL, 1LL);
io_usleep(a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2, unsigned __int8 a3)
{
int v4; // ebx
unsigned int v5; // er12
__int64 v6; // rdx

if ( p[779] > 9u )
fprintf(stderr, "%s(t1=%d, t2=%d, byte=0x%02X)\n", "icspio_out", a1, a2, a3);
io_clock_out_bits(a1, a1, 0LL, 1LL);
v4 = io_stop;
if ( io_stop )
{
v5 = 0;
}
else
{
v5 = io_stop;
do
{
v6 = ((int)a3 >> v4) & 1;
v5 ^= v6;
io_clock_out_bits(a1, a1, v6, 1LL);
++v4;
}
while ( !io_stop && v4 <= 7 );
}
io_clock_out_bits(a1, a1, v5, 1LL);
io_clock_out_bits(a1, a1, 1LL, 1LL);
io_usleep(a2);
return 0LL;
}

original_code:
int
icspio_out(int t1, int t2, uint8_t byte)
{
 int i, bit;
 int parity = 0;
 if (p.debug >= 10) {
  fprintf(stderr, "%s(t1=%d, t2=%d, byte=0x%02X)\n",
   __func__, t1, t2, byte);
 }
 io_clock_out_bits(t1, t1, (0x00), 1);
 for (i = 0; !io_stop && i < 8; ++i) {
  bit = (byte >> i) & 1;
  parity ^= bit;
  io_clock_out_bits(t1, t1, bit, 1);
 }
 io_clock_out_bits(t1, t1, parity, 1);
 io_clock_out_bits(t1, t1, (0x01), 1);
 io_usleep(t2);
 return (0);
}

canonical_original_code:
int icspio_out(int t1, int t2, unsigned char byte)
{
 int i; int bit;
 int parity = 0;
 if (p.debug >= 10) {
  fprintf(stderr, "%s(t1=%d, t2=%d, byte=0x%02X)\n",
   __func__, t1, t2, byte);
 }
 io_clock_out_bits(t1, t1, (0x00), 1);
 for (i = 0; !io_stop && i < 8; ++i) {
  bit = (byte >> i) & 1;
  parity ^= bit;
  io_clock_out_bits(t1, t1, bit, 1);
 }
 io_clock_out_bits(t1, t1, parity, 1);
 io_clock_out_bits(t1, t1, (0x01), 1);
 io_usleep(t2);
 return (0);
}

variable_types:
byte
unsigned char

variable_types: i int

variable_types: bit int

variable_types: parity int


decompiled_code:
__int64 __fastcall icspio_in(unsigned int a1, unsigned int a2, unsigned __int8 *a3)
{
int v4; // ebx
int v5; // er13
int v6; // eax
__int64 result; // rax

*a3 = 0;
if ( p[779] > 9u )
fprintf(stderr, "%s(t1=%d, t2=%d, %p)\n", "icspio_in", a1, a2, a3);
if ( (unsigned int)io_clock_in_bits(a1, a1, 1LL) )
{
result = 2LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID START BIT (NOT LOW)\n", "icspio_in");
result = 2LL;
}
}
else
{
v4 = io_stop;
v5 = io_stop;
if ( io_stop )
{
v5 = 0;
}
else
{
do
{
v6 = io_clock_in_bits(a1, a1, 1LL);
v5 ^= v6;
*a3 |= v6 << v4++;
}
while ( !io_stop && v4 <= 7 );
}
if ( (unsigned int)io_clock_in_bits(a1, a1, 1LL) == v5 )
{
if ( (unsigned int)io_clock_in_bits(a1, a1, 1LL) == 1 )
{
io_usleep(a2);
result = 0LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: OKAY [0x%02X]\n", "icspio_in", *a3);
result = 0LL;
}
}
else
{
result = 2LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID STOP BIT (NOT HIGH)\n", "icspio_in");
result = 2LL;
}
}
}
else
{
result = 3LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID PARITY BIT\n", "icspio_in");
result = 3LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2, unsigned __int8 *a3)
{
int v4; // ebx
int v5; // er13
int v6; // eax
__int64 result; // rax

*a3 = 0;
if ( p[779] > 9u )
fprintf(stderr, "%s(t1=%d, t2=%d, %p)\n", "icspio_in", a1, a2, a3);
if ( (unsigned int)io_clock_in_bits(a1, a1, 1LL) )
{
result = 2LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID START BIT (NOT LOW)\n", "icspio_in");
result = 2LL;
}
}
else
{
v4 = io_stop;
v5 = io_stop;
if ( io_stop )
{
v5 = 0;
}
else
{
do
{
v6 = io_clock_in_bits(a1, a1, 1LL);
v5 ^= v6;
*a3 |= v6 << v4++;
}
while ( !io_stop && v4 <= 7 );
}
if ( (unsigned int)io_clock_in_bits(a1, a1, 1LL) == v5 )
{
if ( (unsigned int)io_clock_in_bits(a1, a1, 1LL) == 1 )
{
io_usleep(a2);
result = 0LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: OKAY [0x%02X]\n", "icspio_in", *a3);
result = 0LL;
}
}
else
{
result = 2LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID STOP BIT (NOT HIGH)\n", "icspio_in");
result = 2LL;
}
}
}
else
{
result = 3LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID PARITY BIT\n", "icspio_in");
result = 3LL;
}
}
}
return result;
}

original_code:
int
icspio_in(int t1, int t2, uint8_t *byte)
{
 int i, bit;
 int parity = 0;
 *byte = '\0';
 if (p.debug >= 10) {
  fprintf(stderr, "%s(t1=%d, t2=%d, %p)\n",
   __func__, t1, t2, (void *)(byte));
 }
 if (io_clock_in_bits(t1, t1, 1) != (0x00)) {
  if (p.debug >= 10)
   fprintf(stderr, "%s: INVALID START BIT (NOT LOW)\n", __func__);
  return (2);
 }
 for (i = 0; !io_stop && i < 8; i++) {
  bit = io_clock_in_bits(t1, t1, 1);
  parity ^= bit;
  *byte |= (bit << i);
 }
 bit = io_clock_in_bits(t1, t1, 1);
 if (bit != parity) {
  if (p.debug >= 10)
   fprintf(stderr, "%s: INVALID PARITY BIT\n", __func__);
  return (3);
 }
 if (io_clock_in_bits(t1, t1, 1) != (0x01)) {
  if (p.debug >= 10)
   fprintf(stderr, "%s: INVALID STOP BIT (NOT HIGH)\n", __func__);
  return (2);
 }
 io_usleep(t2);
 if (p.debug >= 10)
  fprintf(stderr, "%s: OKAY [0x%02X]\n", __func__, *byte);
 return (0);
}

canonical_original_code:
int icspio_in(int t1, int t2, unsigned char *byte)
{
 int i; int bit;
 int parity = 0;
 *byte = '\0';
 if (p.debug >= 10) {
  fprintf(stderr, "%s(t1=%d, t2=%d, %p)\n",
   __func__, t1, t2, (void *)(byte));
 }
 if (io_clock_in_bits(t1, t1, 1) != (0x00)) {
  if (p.debug >= 10)
   fprintf(stderr, "%s: INVALID START BIT (NOT LOW)\n", __func__);
  return (2);
 }
 for (i = 0; !io_stop && i < 8; i++) {
  bit = io_clock_in_bits(t1, t1, 1);
  parity ^= bit;
  *byte |= (bit << i);
 }
 bit = io_clock_in_bits(t1, t1, 1);
 if (bit != parity) {
  if (p.debug >= 10)
   fprintf(stderr, "%s: INVALID PARITY BIT\n", __func__);
  return (3);
 }
 if (io_clock_in_bits(t1, t1, 1) != (0x01)) {
  if (p.debug >= 10)
   fprintf(stderr, "%s: INVALID STOP BIT (NOT HIGH)\n", __func__);
  return (2);
 }
 io_usleep(t2);
 if (p.debug >= 10)
  fprintf(stderr, "%s: OKAY [0x%02X]\n", __func__, *byte);
 return (0);
}

variable_types:
byte
unsigned char *


decompiled_code:
__int64 __fastcall icspio_command(unsigned int a1, unsigned int a2, unsigned __int8 *a3, int a4, int *a5, __int64 a6)
{
int v8; // er13
__int64 result; // rax
__int64 v10; // r15
bool v11; // al
int v12; // er14
unsigned __int8 v13; // dl
unsigned __int8 v15; // [rsp+Fh] [rbp-49h]
unsigned __int8 v16[57]; // [rsp+1Fh] [rbp-39h] BYREF

v8 = a6;
if ( p[779] > 9u )
fprintf(
stderr,
"%s(t1=%d, t2=%d, cmdarg[0]=%02X, cmdargc=%d, %p, %d)\n",
"icspio_command",
a1,
a2,
*a3,
(unsigned int)a4,
a5,
a6);
if ( a4 < 0 )
{
printf("%s: fatal error: invalid length: %d\n", "icspio_command", (unsigned int)a4);
io_exit(70LL);
}
if ( (unsigned int)v8 > 4 )
{
printf("%s: fatal error: invalid width: %d\n", "icspio_command", (unsigned int)v8);
io_exit(70LL);
}
if ( !a5 && v8 > 0 )
{
printf("%s: fatal error: invalid result pointer: (NULL)\n", "icspio_command");
io_exit(70LL);
}
icspio_out(a1, a2, *a3);
result = icspio_in(a1, a2, v16);
if ( !(_DWORD)result )
{
if ( v16[0] == 6 )
{
if ( a4 > 0 || v8 > 0 )
{
v15 = *a3 + 6;
if ( a4 > 0 )
{
if ( !io_stop )
{
v10 = 2LL;
do
{
icspio_out(a1, a2, a3[v10 - 1]);
v15 += a3[v10 - 1];
v11 = a4 >= (int)v10++;
}
while ( v11 && io_stop == 0 );
}
if ( v8 <= 0 )
goto LABEL_23;
}
*a5 = 0;
v12 = io_stop;
if ( io_stop )
{
LABEL_23:
result = icspio_in(a1, a2, v16);
if ( !(_DWORD)result && v16[0] != v15 )
{
result = 5LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID CHECKSUM 0x%02X != 0x%02X\n", "icspio_command", v16[0], v15);
result = 5LL;
}
}
}
else
{
while ( 1 )
{
result = icspio_in(a1, a2, v16);
if ( (_DWORD)result )
break;
v13 = v16[0];
*a5 = v16[0] | (*a5 << 8);
v15 += v13;
++v12;
if ( io_stop || v8 <= v12 )
goto LABEL_23;
}
}
}
}
else
{
result = 4LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2, unsigned __int8 *a3, int a4, int *a5, __int64 a6)
{
int v8; // er13
__int64 result; // rax
__int64 v10; // r15
bool v11; // al
int v12; // er14
unsigned __int8 v13; // dl
unsigned __int8 v15; // [rsp+Fh] [rbp-49h]
unsigned __int8 v16[57]; // [rsp+1Fh] [rbp-39h] BYREF

v8 = a6;
if ( p[779] > 9u )
fprintf(
stderr,
"%s(t1=%d, t2=%d, cmdarg[0]=%02X, cmdargc=%d, %p, %d)\n",
"icspio_command",
a1,
a2,
*a3,
(unsigned int)a4,
a5,
a6);
if ( a4 < 0 )
{
printf("%s: fatal error: invalid length: %d\n", "icspio_command", (unsigned int)a4);
io_exit(70LL);
}
if ( (unsigned int)v8 > 4 )
{
printf("%s: fatal error: invalid width: %d\n", "icspio_command", (unsigned int)v8);
io_exit(70LL);
}
if ( !a5 && v8 > 0 )
{
printf("%s: fatal error: invalid result pointer: (NULL)\n", "icspio_command");
io_exit(70LL);
}
func0(a1, a2, *a3);
result = func1(a1, a2, v16);
if ( !(_DWORD)result )
{
if ( v16[0] == 6 )
{
if ( a4 > 0 || v8 > 0 )
{
v15 = *a3 + 6;
if ( a4 > 0 )
{
if ( !io_stop )
{
v10 = 2LL;
do
{
func0(a1, a2, a3[v10 - 1]);
v15 += a3[v10 - 1];
v11 = a4 >= (int)v10++;
}
while ( v11 && io_stop == 0 );
}
if ( v8 <= 0 )
goto LABEL_23;
}
*a5 = 0;
v12 = io_stop;
if ( io_stop )
{
LABEL_23:
result = func1(a1, a2, v16);
if ( !(_DWORD)result && v16[0] != v15 )
{
result = 5LL;
if ( p[779] > 9u )
{
fprintf(stderr, "%s: INVALID CHECKSUM 0x%02X != 0x%02X\n", "icspio_command", v16[0], v15);
result = 5LL;
}
}
}
else
{
while ( 1 )
{
result = func1(a1, a2, v16);
if ( (_DWORD)result )
break;
v13 = v16[0];
*a5 = v16[0] | (*a5 << 8);
v15 += v13;
++v12;
if ( io_stop || v8 <= v12 )
goto LABEL_23;
}
}
}
}
else
{
result = 4LL;
}
}
return result;
}

original_code:
int
icspio_command(int t1, int t2, uint8_t *cmdarg, int cmdargc, uint32_t *res, int resw)
{
 int i, err;
 uint8_t byte;
 if (p.debug >= 10)
  fprintf(stderr, "%s(t1=%d, t2=%d, cmdarg[0]=%02X, cmdargc=%d, %p, %d)\n", __func__,
   t1, t2, cmdarg[0], cmdargc, (void *)(res), resw);
 if (cmdargc < 0) {
  printf("%s: fatal error: invalid length: %d\n", __func__, cmdargc);
  io_exit(70);
 }
 if (resw < 0 || resw > 4) {
  printf("%s: fatal error: invalid width: %d\n", __func__, resw);
  io_exit(70);
 }
 if (resw > 0 && res == ((void *)0)) {
  printf("%s: fatal error: invalid result pointer: (NULL)\n", __func__);
  io_exit(70);
 }
 icspio_out(t1, t2, cmdarg[0]);
 err = icspio_in(t1, t2, &byte);
 if (err != (0)) {
  return err;
 }
 if (byte != (0x06)) {
  return (4);
 }
 if (cmdargc > 0 || resw > 0) {
  uint8_t checksum = cmdarg[0] + byte;
  if (cmdargc > 0) {
   for (i = 1; !io_stop && i <= cmdargc; ++i) {
    icspio_out(t1, t2, cmdarg[i]);
    checksum += cmdarg[i];
   }
  }
  if (resw > 0) {
   *res = 0;
   for (i = 0; !io_stop && i < resw; ++i) {
    err = icspio_in(t1, t2, &byte);
    if (err != (0)) {
     return err;
    }
    *res = (*res << 8) | byte;
    checksum += byte;
   }
  }
  err = icspio_in(t1, t2, &byte);
  if (err != (0)) {
   return err;
  }
  if (byte != checksum) {
   if (p.debug >= 10)
    fprintf(stderr, "%s: INVALID CHECKSUM 0x%02X != 0x%02X\n",
     __func__, byte, checksum);
   return (5);
  }
 }
 return (0);
}

canonical_original_code:
int icspio_command(int t1, int t2, unsigned char *cmdarg, int cmdargc, unsigned int *res, int resw)
{
 int i; int err;
 unsigned char byte;
 if (p.debug >= 10)
  fprintf(stderr, "%s(t1=%d, t2=%d, cmdarg[0]=%02X, cmdargc=%d, %p, %d)\n", __func__,
   t1, t2, cmdarg[0], cmdargc, (void *)(res), resw);
 if (cmdargc < 0) {
  printf("%s: fatal error: invalid length: %d\n", __func__, cmdargc);
  io_exit(70);
 }
 if (resw < 0 || resw > 4) {
  printf("%s: fatal error: invalid width: %d\n", __func__, resw);
  io_exit(70);
 }
 if (resw > 0 && res == ((void *)0)) {
  printf("%s: fatal error: invalid result pointer: (NULL)\n", __func__);
  io_exit(70);
 }
 icspio_out(t1, t2, cmdarg[0]);
 err = icspio_in(t1, t2, &byte);
 if (err != (0)) {
  return err;
 }
 if (byte != (0x06)) {
  return (4);
 }
 if (cmdargc > 0 || resw > 0) {
  unsigned char checksum = cmdarg[0] + byte;
  if (cmdargc > 0) {
   for (i = 1; !io_stop && i <= cmdargc; ++i) {
    icspio_out(t1, t2, cmdarg[i]);
    checksum += cmdarg[i];
   }
  }
  if (resw > 0) {
   *res = 0;
   for (i = 0; !io_stop && i < resw; ++i) {
    err = icspio_in(t1, t2, &byte);
    if (err != (0)) {
     return err;
    }
    *res = (*res << 8) | byte;
    checksum += byte;
   }
  }
  err = icspio_in(t1, t2, &byte);
  if (err != (0)) {
   return err;
  }
  if (byte != checksum) {
   if (p.debug >= 10)
    fprintf(stderr, "%s: INVALID CHECKSUM 0x%02X != 0x%02X\n",
     __func__, byte, checksum);
   return (5);
  }
 }
 return (0);
}

variable_types:
cmdarg
unsigned char *

variable_types: cmdargc int

variable_types:
res
unsigned int *

variable_types: resw int

variable_types: err int

variable_types:
checksum
unsigned char


decompiled_code:
__int64 __fastcall icspio_err(int a1)
{
switch ( a1 )
{
case 0:
errmsg_4 = (__int64)"ERRNONE";
break;
case 1:
errmsg_4 = (__int64)"ERRTIMEOUT";
break;
case 2:
errmsg_4 = (__int64)"ERRPROTOCOL";
break;
case 3:
errmsg_4 = (__int64)"ERRPARITY";
break;
case 4:
errmsg_4 = (__int64)"ERRNOTSUP";
break;
case 5:
errmsg_4 = (__int64)"ERRINVALID";
break;
default:
errmsg_4 = (__int64)"ERRUNKNOWN";
break;
}
return errmsg_4;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
switch ( a1 )
{
case 0:
errmsg_4 = (__int64)"ERRNONE";
break;
case 1:
errmsg_4 = (__int64)"ERRTIMEOUT";
break;
case 2:
errmsg_4 = (__int64)"ERRPROTOCOL";
break;
case 3:
errmsg_4 = (__int64)"ERRPARITY";
break;
case 4:
errmsg_4 = (__int64)"ERRNOTSUP";
break;
case 5:
errmsg_4 = (__int64)"ERRINVALID";
break;
default:
errmsg_4 = (__int64)"ERRUNKNOWN";
break;
}
return errmsg_4;
}

original_code:
char *
icspio_err(int err)
{
 static char *errmsg;
 switch (err) {
 case (0):
  errmsg = "ERRNONE";
  break;
 case (1):
  errmsg = "ERRTIMEOUT";
  break;
 case (2):
  errmsg = "ERRPROTOCOL";
  break;
 case (3):
  errmsg = "ERRPARITY";
  break;
 case (4):
  errmsg = "ERRNOTSUP";
  break;
 case (5):
  errmsg = "ERRINVALID";
  break;
 default:errmsg = "ERRUNKNOWN";
  break;
 }
 return errmsg;
}

canonical_original_code:
char *icspio_err(int err)
{
 static char *errmsg;
 switch (err) {
 case (0):
  errmsg = "ERRNONE";
  break;
 case (1):
  errmsg = "ERRTIMEOUT";
  break;
 case (2):
  errmsg = "ERRPROTOCOL";
  break;
 case (3):
  errmsg = "ERRPARITY";
  break;
 case (4):
  errmsg = "ERRNOTSUP";
  break;
 case (5):
  errmsg = "ERRINVALID";
  break;
 default:errmsg = "ERRUNKNOWN";
  break;
 }
 return errmsg;
}

variable_types:
errmsg
char *


decompiled_code:
__int64 __fastcall icspio_switch(unsigned int a1)
{
__int64 result; // rax
unsigned int v2; // ebx
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned __int8 v4[9]; // [rsp+Fh] [rbp-9h] BYREF

v4[0] = 2;
result = icspio_command(a1, 2 * a1, v4, 0, (int *)&v3, 1LL);
if ( !(_DWORD)result )
{
v2 = io_stop;
if ( !io_stop )
{
result = io_stop;
if ( v3 != lastsw_3 )
{
printf(
"%s: SW=0x%02X SW1=%d SW2=%d SW3=%d SW4=%d\n",
"icspio_switch",
v3,
v3 & 1,
(v3 >> 1) & 1,
(v3 >> 2) & 1,
(v3 >> 3) & 1);
lastsw_3 = v3;
result = v2;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
__int64 result; // rax
unsigned int v2; // ebx
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned __int8 v4[9]; // [rsp+Fh] [rbp-9h] BYREF

v4[0] = 2;
result = func2(a1, 2 * a1, v4, 0, (int *)&v3, 1LL);
if ( !(_DWORD)result )
{
v2 = io_stop;
if ( !io_stop )
{
result = io_stop;
if ( v3 != lastsw_3 )
{
printf(
"%s: SW=0x%02X SW1=%d SW2=%d SW3=%d SW4=%d\n",
"icspio_switch",
v3,
v3 & 1,
(v3 >> 1) & 1,
(v3 >> 2) & 1,
(v3 >> 3) & 1);
lastsw_3 = v3;
result = v2;
}
}
}
return result;
}

original_code:
int
icspio_switch(int t)
{
 int err;
 uint8_t cmd[1];
 uint32_t sw;
 static uint32_t lastsw = 0xFF;
 cmd[0] = (0x02);
 err = icspio_command(t, t << 1, cmd, 0, &sw, 1);
 if (err != (0)) {
  return err;
 }
 if (!io_stop && sw != lastsw) {
  printf("%s: SW=0x%02X SW1=%d SW2=%d SW3=%d SW4=%d\n",
   __func__, sw,
   (sw & 0x01) >> 0, (sw & 0x02) >> 1,
   (sw & 0x04) >> 2, (sw & 0x08) >> 3);
  lastsw = sw;
 }
 return (0);
}

canonical_original_code:
int icspio_switch(int t)
{
 int err;
 unsigned char cmd[1];
 unsigned int sw;
 static unsigned int lastsw = 0xFF;
 cmd[0] = (0x02);
 err = icspio_command(t, t << 1, cmd, 0, &sw, 1);
 if (err != (0)) {
  return err;
 }
 if (!io_stop && sw != lastsw) {
  printf("%s: SW=0x%02X SW1=%d SW2=%d SW3=%d SW4=%d\n",
   __func__, sw,
   (sw & 0x01) >> 0, (sw & 0x02) >> 1,
   (sw & 0x04) >> 2, (sw & 0x08) >> 3);
  lastsw = sw;
 }
 return (0);
}

variable_types: t int

variable_types:
cmd
unsigned char [1]

variable_types:
sw
unsigned int

variable_types:
lastsw
unsigned int


decompiled_code:
__int64 __fastcall icspio_lasterror(unsigned int a1)
{
__int64 result; // rax
unsigned int v2; // ebx
unsigned int v3; // ebp
const char *v4; // rax
int v5; // [rsp+8h] [rbp-20h] BYREF
unsigned __int8 v6[25]; // [rsp+Fh] [rbp-19h] BYREF

v6[0] = -1;
result = icspio_command(a1, 2 * a1, v6, 0, &v5, 1LL);
if ( !(_DWORD)result )
{
v2 = io_stop;
if ( !io_stop )
{
v3 = v5;
result = io_stop;
if ( v5 != lastle_1 )
{
v4 = (const char *)icspio_err(v5);
printf("%s: last error: %s [0x%02X]\n", "icspio_lasterror", v4, v3);
lastle_1 = v5;
result = v2;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
__int64 result; // rax
unsigned int v2; // ebx
unsigned int v3; // ebp
const char *v4; // rax
int v5; // [rsp+8h] [rbp-20h] BYREF
unsigned __int8 v6[25]; // [rsp+Fh] [rbp-19h] BYREF

v6[0] = -1;
result = func2(a1, 2 * a1, v6, 0, &v5, 1LL);
if ( !(_DWORD)result )
{
v2 = io_stop;
if ( !io_stop )
{
v3 = v5;
result = io_stop;
if ( v5 != lastle_1 )
{
v4 = (const char *)func3(v5);
printf("%s: last error: %s [0x%02X]\n", "icspio_lasterror", v4, v3);
lastle_1 = v5;
result = v2;
}
}
}
return result;
}

original_code:
int
icspio_lasterror(int t)
{
 int err;
 uint8_t cmd[1];
 uint32_t le;
 static uint32_t lastle = 0;
 cmd[0] = (0xFF);
 err = icspio_command(t, t << 1, cmd, 0, &le, 1);
 if (err != (0)) {
  return err;
 }
 if (!io_stop && le != lastle) {
  printf("%s: last error: %s [0x%02X]\n",
   __func__, icspio_err(le), le);
  lastle = le;
 }
 return (0);
}

canonical_original_code:
int icspio_lasterror(int t)
{
 int err;
 unsigned char cmd[1];
 unsigned int le;
 static unsigned int lastle = 0;
 cmd[0] = (0xFF);
 err = icspio_command(t, t << 1, cmd, 0, &le, 1);
 if (err != (0)) {
  return err;
 }
 if (!io_stop && le != lastle) {
  printf("%s: last error: %s [0x%02X]\n",
   __func__, icspio_err(le), le);
  lastle = le;
 }
 return (0);
}

variable_types:
le
unsigned int

variable_types:
lastle
unsigned int


getdotpath

decompiled_code:
int getdotpath()
{
int v0; // eax
char *v1; // rbx
int v2; // eax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

if ( stat((const char *)(unsigned int)((_DWORD)&p + 528), &v4) < 0 )
{
if ( *_errno_location() == 2 )
return -1;
printf("%s: fatal error: stat failed [%s]\n", "getdotpath", (const char *)(unsigned int)((_DWORD)&p + 528));
io_exit(70LL);
}
v0 = v4.st_mode & 0xF000;
if ( v0 == 0x8000 )
{
if ( !getuid() || getuid() == v4.st_uid )
return access((const char *)(unsigned int)((_DWORD)&p + 528), 4);
LABEL_23:
printf("%s: fatal error: invalid owner [%s]\n", "getdotpath", (const char *)(unsigned int)((_DWORD)&p + 528));
io_exit(70LL);
return access((const char *)(unsigned int)((_DWORD)&p + 528), 4);
}
if ( v0 != 0x4000 )
return -1;
v1 = strdup((const char *)(unsigned int)((_DWORD)&p + 528));
if ( !v1 )
{
printf("%s: fatal error: strdup failed\n", "getdotpath");
io_exit(71LL);
}
v2 = snprintf((char *)(unsigned int)((_DWORD)&p + 528), 0x200uLL, "%s/%s", v1, "config");
if ( v2 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotpath");
io_exit(71LL);
}
else if ( v2 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotpath");
io_exit(70LL);
}
free(v1);
if ( stat((const char *)(unsigned int)((_DWORD)&p + 528), &v4) < 0 )
{
if ( *_errno_location() == 2 )
return -1;
printf("%s: fatal error: stat failed [%s]\n", "getdotpath", (const char *)(unsigned int)((_DWORD)&p + 528));
io_exit(70LL);
}
if ( (v4.st_mode & 0xF000) == 0x8000 )
{
if ( !getuid() || getuid() == v4.st_uid )
return access((const char *)(unsigned int)((_DWORD)&p + 528), 4);
goto LABEL_23;
}
return -1;
}

canonical_decompiled_code:
int func0()
{
int v0; // eax
char *v1; // rbx
int v2; // eax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

if ( stat((const char *)(unsigned int)((_DWORD)&p + 528), &v4) < 0 )
{
if ( *_errno_location() == 2 )
return -1;
printf("%s: fatal error: stat failed [%s]\n", "getdotpath", (const char *)(unsigned int)((_DWORD)&p + 528));
io_exit(70LL);
}
v0 = v4.st_mode & 0xF000;
if ( v0 == 0x8000 )
{
if ( !getuid() || getuid() == v4.st_uid )
return access((const char *)(unsigned int)((_DWORD)&p + 528), 4);
LABEL_23:
printf("%s: fatal error: invalid owner [%s]\n", "getdotpath", (const char *)(unsigned int)((_DWORD)&p + 528));
io_exit(70LL);
return access((const char *)(unsigned int)((_DWORD)&p + 528), 4);
}
if ( v0 != 0x4000 )
return -1;
v1 = strdup((const char *)(unsigned int)((_DWORD)&p + 528));
if ( !v1 )
{
printf("%s: fatal error: strdup failed\n", "getdotpath");
io_exit(71LL);
}
v2 = snprintf((char *)(unsigned int)((_DWORD)&p + 528), 0x200uLL, "%s/%s", v1, "config");
if ( v2 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotpath");
io_exit(71LL);
}
else if ( v2 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotpath");
io_exit(70LL);
}
free(v1);
if ( stat((const char *)(unsigned int)((_DWORD)&p + 528), &v4) < 0 )
{
if ( *_errno_location() == 2 )
return -1;
printf("%s: fatal error: stat failed [%s]\n", "getdotpath", (const char *)(unsigned int)((_DWORD)&p + 528));
io_exit(70LL);
}
if ( (v4.st_mode & 0xF000) == 0x8000 )
{
if ( !getuid() || getuid() == v4.st_uid )
return access((const char *)(unsigned int)((_DWORD)&p + 528), 4);
goto LABEL_23;
}
return -1;
}

original_code:
int
getdotpath(void)
{
 struct stat st;
 int rc;
 if (stat(p.dotfile, &st) < 0) {
  if ((*__errno_location ()) == 2)
   return -1;
  printf("%s: fatal error: stat failed [%s]\n", __func__, p.dotfile);
  io_exit(70);
 }
 if (((((st.st_mode)) & 0170000) == (0100000))) {
  if (getuid() && getuid() != st.st_uid) {
   printf("%s: fatal error: invalid owner [%s]\n", __func__, p.dotfile);
   io_exit(70);
  }
  return access(p.dotfile, 4);
 }
 if (((((st.st_mode)) & 0170000) == (0040000))) {
  char *dotdup = (char *)strdup(p.dotfile);
  if (dotdup == ((void *)0)) {
   printf("%s: fatal error: strdup failed\n", __func__);
   io_exit(71);
  }
  rc = snprintf(p.dotfile, (512), "%s/%s", dotdup, "config");
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  free(dotdup);
  if (stat(p.dotfile, &st) < 0) {
   if ((*__errno_location ()) == 2)
    return -1;
   printf("%s: fatal error: stat failed [%s]\n", __func__, p.dotfile);
   io_exit(70);
  }
  if (((((st.st_mode)) & 0170000) == (0100000))) {
   if (getuid() && getuid() != st.st_uid) {
    printf("%s: fatal error: invalid owner [%s]\n", __func__, p.dotfile);
    io_exit(70);
   }
   return access(p.dotfile, 4);
  }
 }
 return -1;
}

canonical_original_code:
int getdotpath()
{
 struct stat st;
 int rc;
 if (stat(p.dotfile, &st) < 0) {
  if ((*__errno_location ()) == 2)
   return -1;
  printf("%s: fatal error: stat failed [%s]\n", __func__, p.dotfile);
  io_exit(70);
 }
 if (((((st.st_mode)) & 0170000) == (0100000))) {
  if (getuid() && getuid() != st.st_uid) {
   printf("%s: fatal error: invalid owner [%s]\n", __func__, p.dotfile);
   io_exit(70);
  }
  return access(p.dotfile, 4);
 }
 if (((((st.st_mode)) & 0170000) == (0040000))) {
  char *dotdup = (char *)strdup(p.dotfile);
  if (dotdup == ((void *)0)) {
   printf("%s: fatal error: strdup failed\n", __func__);
   io_exit(71);
  }
  rc = snprintf(p.dotfile, (512), "%s/%s", dotdup, "config");
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  free(dotdup);
  if (stat(p.dotfile, &st) < 0) {
   if ((*__errno_location ()) == 2)
    return -1;
   printf("%s: fatal error: stat failed [%s]\n", __func__, p.dotfile);
   io_exit(70);
  }
  if (((((st.st_mode)) & 0170000) == (0100000))) {
   if (getuid() && getuid() != st.st_uid) {
    printf("%s: fatal error: invalid owner [%s]\n", __func__, p.dotfile);
    io_exit(70);
   }
   return access(p.dotfile, 4);
  }
 }
 return -1;
}

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: rc int

variable_types:
dotdup
char *

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


getdotfile

decompiled_code:
int getdotfile()
{
char *v0; // rax
int v1; // eax
char *v2; // rax
int v3; // eax
char *v4; // rcx
int v5; // eax
int v6; // eax
__int64 v8[2]; // [rsp+0h] [rbp-208h] BYREF
char v9[496]; // [rsp+10h] [rbp-1F8h] BYREF

v0 = getenv("PICKLE");
if ( !v0 )
goto LABEL_9;
v1 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "%s", v0);
if ( v1 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v1 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = getdotpath();
if ( (_DWORD)v2 )
{
printf("%s: warning: invalid PICKLE environment variable\n", "getdotfile");
LABEL_9:
v8[0] = 0LL;
v8[1] = 0LL;
memset(v9, 0, sizeof(v9));
if ( !getcwd((char *)v8, 0x200uLL) )
{
printf("%s: fatal error: getcwd failed\n", "getdotfile");
io_exit(71LL);
}
v3 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "%s/%s", (const char *)v8, ".pickle");
if ( v3 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v3 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = getdotpath();
if ( (_DWORD)v2 )
{
v4 = getenv("HOME");
if ( !v4 )
goto LABEL_20;
v5 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "%s/%s", v4, ".pickle");
if ( v5 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v5 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = getdotpath();
if ( (_DWORD)v2 )
{
LABEL_20:
v2 = getenv("USER");
if ( !v2 )
goto LABEL_25;
v6 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "/home/%s/%s", v2, ".pickle");
if ( v6 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v6 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = getdotpath();
if ( (_DWORD)v2 )
LABEL_25:
p[528] = 0;
}
}
}
return (int)v2;
}

canonical_decompiled_code:
int func1()
{
char *v0; // rax
int v1; // eax
char *v2; // rax
int v3; // eax
char *v4; // rcx
int v5; // eax
int v6; // eax
__int64 v8[2]; // [rsp+0h] [rbp-208h] BYREF
char v9[496]; // [rsp+10h] [rbp-1F8h] BYREF

v0 = getenv("PICKLE");
if ( !v0 )
goto LABEL_9;
v1 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "%s", v0);
if ( v1 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v1 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = func0();
if ( (_DWORD)v2 )
{
printf("%s: warning: invalid PICKLE environment variable\n", "getdotfile");
LABEL_9:
v8[0] = 0LL;
v8[1] = 0LL;
memset(v9, 0, sizeof(v9));
if ( !getcwd((char *)v8, 0x200uLL) )
{
printf("%s: fatal error: getcwd failed\n", "getdotfile");
io_exit(71LL);
}
v3 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "%s/%s", (const char *)v8, ".pickle");
if ( v3 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v3 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = func0();
if ( (_DWORD)v2 )
{
v4 = getenv("HOME");
if ( !v4 )
goto LABEL_20;
v5 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "%s/%s", v4, ".pickle");
if ( v5 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v5 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = func0();
if ( (_DWORD)v2 )
{
LABEL_20:
v2 = getenv("USER");
if ( !v2 )
goto LABEL_25;
v6 = snprintf((char *)(unsigned int)((_DWORD)p + 528), 0x200uLL, "/home/%s/%s", v2, ".pickle");
if ( v6 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "getdotfile");
io_exit(71LL);
}
else if ( v6 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
}
LODWORD(v2) = func0();
if ( (_DWORD)v2 )
LABEL_25:
p[528] = 0;
}
}
}
return (int)v2;
}

original_code:
void
getdotfile(void)
{
 int rc;
 char *filename = getenv("PICKLE");
 if (filename != ((void *)0)) {
  rc = snprintf(p.dotfile, (512), "%s", filename);
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  if (getdotpath() == 0)
   return;
  printf("%s: warning: invalid PICKLE environment variable\n", __func__);
 }
 char dir[(512)] = {0};
 if (getcwd(dir, (512)) == ((void *)0)) {
  printf("%s: fatal error: getcwd failed\n", __func__);
  io_exit(71);
 }
 rc = snprintf(p.dotfile, (512), "%s/%s", dir, ".pickle");
 if (rc < 0) {
  printf("%s: fatal error: snprintf failed\n", __func__);
  io_exit(71);
 } else if (rc >= (512)) {
  printf("%s: fatal error: snprintf overrun\n", __func__);
  io_exit(70);
 }
 if (getdotpath() == 0)
  return;
 char *homedir = getenv("HOME");
 if (homedir != ((void *)0)) {
  rc = snprintf(p.dotfile, (512), "%s/%s", homedir, ".pickle");
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  if (getdotpath() == 0)
   return;
 }
 char *username = getenv("USER");
 if (username != ((void *)0)) {
  rc = snprintf(p.dotfile, (512), "/home/%s/%s", username, ".pickle");
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  if (getdotpath() == 0)
   return;
 }
 p.dotfile[0] = '\0';
}

canonical_original_code:
void getdotfile()
{
 int rc;
 char *filename = getenv("PICKLE");
 if (filename != ((void *)0)) {
  rc = snprintf(p.dotfile, (512), "%s", filename);
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  if (getdotpath() == 0)
   return;
  printf("%s: warning: invalid PICKLE environment variable\n", __func__);
 }
 char dir[-1] = {0};
 if (getcwd(dir, (512)) == ((void *)0)) {
  printf("%s: fatal error: getcwd failed\n", __func__);
  io_exit(71);
 }
 rc = snprintf(p.dotfile, (512), "%s/%s", dir, ".pickle");
 if (rc < 0) {
  printf("%s: fatal error: snprintf failed\n", __func__);
  io_exit(71);
 } else if (rc >= (512)) {
  printf("%s: fatal error: snprintf overrun\n", __func__);
  io_exit(70);
 }
 if (getdotpath() == 0)
  return;
 char *homedir = getenv("HOME");
 if (homedir != ((void *)0)) {
  rc = snprintf(p.dotfile, (512), "%s/%s", homedir, ".pickle");
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  if (getdotpath() == 0)
   return;
 }
 char *username = getenv("USER");
 if (username != ((void *)0)) {
  rc = snprintf(p.dotfile, (512), "/home/%s/%s", username, ".pickle");
  if (rc < 0) {
   printf("%s: fatal error: snprintf failed\n", __func__);
   io_exit(71);
  } else if (rc >= (512)) {
   printf("%s: fatal error: snprintf overrun\n", __func__);
   io_exit(70);
  }
  if (getdotpath() == 0)
   return;
 }
 p.dotfile[0] = '\0';
}

variable_types:
filename
char *

variable_types:
dir
char [-1]

variable_types:
homedir
char *

variable_types:
username
char *


getvar

decompiled_code:
char *__fastcall getvar(char *dest, int a2)
{
char *v2; // rbx
char v3; // al
char *v4; // rdx
int v5; // ecx

if ( !a2 )
{
printf("%s: fatal error: invalid length\n", "getvar");
io_exit(70LL);
}
v2 = (char *)malloc(a2);
if ( !v2 )
{
printf("%s: fatal error: malloc failed\n", "getvar");
io_exit(71LL);
}
dest[a2 - 1] = 0;
v3 = *dest;
if ( !*dest || v3 == 35 )
{
v5 = 0;
}
else
{
v4 = dest + 1;
v5 = 0;
do
{
if ( v3 > 32 )
v2[v5++] = v3;
v3 = *v4++;
}
while ( v3 && v3 != 35 );
}
v2[v5] = 0;
strcpy(dest, v2);
free(v2);
return dest;
}

canonical_decompiled_code:
char *__fastcall func2(char *dest, int a2)
{
char *v2; // rbx
char v3; // al
char *v4; // rdx
int v5; // ecx

if ( !a2 )
{
printf("%s: fatal error: invalid length\n", "getvar");
io_exit(70LL);
}
v2 = (char *)malloc(a2);
if ( !v2 )
{
printf("%s: fatal error: malloc failed\n", "getvar");
io_exit(71LL);
}
dest[a2 - 1] = 0;
v3 = *dest;
if ( !*dest || v3 == 35 )
{
v5 = 0;
}
else
{
v4 = dest + 1;
v5 = 0;
do
{
if ( v3 > 32 )
v2[v5++] = v3;
v3 = *v4++;
}
while ( v3 && v3 != 35 );
}
v2[v5] = 0;
strcpy(dest, v2);
free(v2);
return dest;
}

original_code:
char *
getvar(char *s, int slen)
{
 int i = 0, j = 0;
 char *t;
 if (!slen) {
  printf("%s: fatal error: invalid length\n", __func__);
  io_exit(70);
 }
 t = malloc(slen);
 if (t == ((void *)0)) {
  printf("%s: fatal error: malloc failed\n", __func__);
  io_exit(71);
 }
 s[slen - 1] = '\0';
 while (s[i] && s[i] != '#') {
  if (s[i] > ' ')
   t[j++] = s[i];
  ++i;
 }
 t[j] = '\0';
 strcpy(s, t);
 free(t);
 return s;
}

canonical_original_code:
char *getvar(char *s, int slen)
{
 int i = 0; int j = 0;
 char *t;
 if (!slen) {
  printf("%s: fatal error: invalid length\n", __func__);
  io_exit(70);
 }
 t = malloc(slen);
 if (t == ((void *)0)) {
  printf("%s: fatal error: malloc failed\n", __func__);
  io_exit(71);
 }
 s[slen - 1] = '\0';
 while (s[i] && s[i] != '#') {
  if (s[i] > ' ')
   t[j++] = s[i];
  ++i;
 }
 t[j] = '\0';
 strcpy(s, t);
 free(t);
 return s;
}

variable_types:
s
char *

variable_types: slen int

variable_types: i int

variable_types: j int

variable_types:
t
char *


decompiled_code:
void io_signal()
{
io_stop = 1;
}

canonical_decompiled_code:
void func0()
{
io_stop = 1;
}

original_code:
void
io_signal()
{
 io_stop = 1;
}

canonical_original_code:
void io_signal()
{
 io_stop = 1;
}


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

result = 1LL;
if ( p[3145] )
result = (unsigned int)(1 << (a1 - 1));
return result;
}

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

result = 1LL;
if ( p[3145] )
result = (unsigned int)(1 << (a1 - 1));
return result;
}

original_code:
static inline uint32_t
io_first_mask(uint8_t nbits)
{
 return (p.msb_first) ? (1U << (nbits - 1)) : (1 << 0);
}

canonical_original_code:
static inline unsigned int io_first_mask(unsigned char nbits)
{
 return (p.msb_first) ? (1U << (nbits - 1)) : (1 << 0);
}

variable_types:
nbits
unsigned char


decompiled_code:
__int64 __fastcall io_next_mask(unsigned int a1)
{
__int64 result; // rax
unsigned int v2; // edi

result = a1 >> 1;
v2 = 2 * a1;
if ( !p[3145] )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 result; // rax
unsigned int v2; // edi

result = a1 >> 1;
v2 = 2 * a1;
if ( !p[3145] )
result = v2;
return result;
}

original_code:
static inline uint32_t
io_next_mask(uint32_t mask)
{
 return (p.msb_first) ? (mask >> 1) : (mask << 1);
}

canonical_original_code:
static inline unsigned int io_next_mask(unsigned int mask)
{
 return (p.msb_first) ? (mask >> 1) : (mask << 1);
}

variable_types:
mask
unsigned int


decompiled_code:
__int64 io_backend()
{
int v0; // edx
__int64 result; // rax

if ( !strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "GPIO-BB") )
return gpio_bb_backend();
if ( !strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "CP2104") )
return cp2104_bb_backend();
v0 = strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "FTDI");
result = 0LL;
if ( !v0 )
result = ftdi_bb_backend();
return result;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // edx
__int64 result; // rax

if ( !strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "GPIO-BB") )
return gpio_bb_backend();
if ( !strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "CP2104") )
return cp2104_bb_backend();
v0 = strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "FTDI");
result = 0LL;
if ( !v0 )
result = ftdi_bb_backend();
return result;
}

original_code:
uint8_t
io_backend(void)
{
 if (strcasecmp(p.device, "GPIO-BB") == 0)
  return gpio_bb_backend();
 if (strcasecmp(p.device, "CP2104") == 0)
  return cp2104_bb_backend();
 if (strcasecmp(p.device, "FTDI") == 0)
  return ftdi_bb_backend();
 return 0;
}

canonical_original_code:
unsigned char io_backend()
{
 if (strcasecmp(p.device, "GPIO-BB") == 0)
  return gpio_bb_backend();
 if (strcasecmp(p.device, "CP2104") == 0)
  return cp2104_bb_backend();
 if (strcasecmp(p.device, "FTDI") == 0)
  return ftdi_bb_backend();
 return 0;
}


decompiled_code:
__sighandler_t io_signal_on()
{
io_stop = 0;
signal(2, (__sighandler_t)io_signal);
return signal(13, (__sighandler_t)((char *)io_signal + 1));
}

canonical_decompiled_code:
__sighandler_t func4()
{
io_stop = 0;
signal(2, (__sighandler_t)io_signal);
return signal(13, (__sighandler_t)((char *)io_signal + 1));
}

original_code:
void
io_signal_on()
{
 io_stop = 0;
 signal(2, io_signal);
 signal(13, ((__sighandler_t) 1));
}

canonical_original_code:
void io_signal_on()
{
 io_stop = 0;
 signal(2, io_signal);
 signal(13, ((void (*)(int)) 1));
}


decompiled_code:
__sighandler_t io_signal_off()
{
signal(2, 0LL);
return signal(13, 0LL);
}

canonical_decompiled_code:
__sighandler_t func5()
{
signal(2, 0LL);
return signal(13, 0LL);
}

original_code:
void
io_signal_off()
{
 signal(2, ((__sighandler_t) 0));
 signal(13, ((__sighandler_t) 0));
}

canonical_original_code:
void io_signal_off()
{
 signal(2, ((void (*)(int)) 0));
 signal(13, ((void (*)(int)) 0));
}


decompiled_code:
__int64 io_open()
{
__int64 v0; // rax
int (*v1)(void); // rax

if ( p[3168] )
return 0xFFFFFFFFLL;
v0 = *(_QWORD *)&p[3160];
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = *(int (**)(void))(v0 + 8);
if ( !v1 )
return 0xFFFFFFFFLL;
if ( v1() < 0 )
return 0xFFFFFFFFLL;
io_signal_on();
p[3168] = **(_BYTE **)&p[3160];
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rax
int (*v1)(void); // rax

if ( p[3168] )
return 0xFFFFFFFFLL;
v0 = *(_QWORD *)&p[3160];
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = *(int (**)(void))(v0 + 8);
if ( !v1 )
return 0xFFFFFFFFLL;
if ( v1() < 0 )
return 0xFFFFFFFFLL;
func4();
p[3168] = **(_BYTE **)&p[3160];
return 0LL;
}

original_code:
int
io_open(void)
{
 if (p.iot != (0))
  return -1;
 if (p.io && p.io->open) {
  if (p.io->open() < 0)
   return -1;
  io_signal_on();
  p.iot = p.io->type;
  return 0;
 }
 return -1;
}

canonical_original_code:
int io_open()
{
 if (p.iot != (0))
  return -1;
 if (p.io && p.io->open) {
  if (p.io->open() < 0)
   return -1;
  io_signal_on();
  p.iot = p.io->type;
  return 0;
 }
 return -1;
}


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

result = "device not detected";
if ( a1 )
result = "device not compatible";
msg_4 = (__int64)result;
return result;
}

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

result = "device not detected";
if ( a1 )
result = "device not compatible";
msg_4 = (__int64)result;
return result;
}

original_code:
char *
io_fault(int errorcode)
{
 static char *msg;
 if (errorcode == 0)
  msg = "device not detected";
 else
  msg = "device not compatible";
 return msg;
}

canonical_original_code:
char *io_fault(int errorcode)
{
 static char *msg;
 if (errorcode == 0)
  msg = "device not detected";
 else
  msg = "device not compatible";
 return msg;
}

variable_types: errorcode int

variable_types:
msg
char *