decompiled_code:
__int64 ftdi_bb_open()
{
unsigned __int64 v0; // rax
unsigned int v1; // er12
_BYTE v3[9]; // [rsp+Dh] [rbp-Bh] BYREF

if ( (int)ftdi_init(&handle) < 0 )
return (unsigned int)-1;
v0 = strtoul((const char *)(unsigned int)((_DWORD)&p + 2576), 0LL, 0);
if ( (int)ftdi_set_interface(&handle, v0) < 0 )
return (unsigned int)-1;
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 (unsigned int)-1;
}
}
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 (unsigned int)-1;
}
if ( (int)ftdi_bb_bitmode(0) < 0 || (int)ftdi_set_baudrate(&handle, *((unsigned int *)&p + 781)) < 0 )
return (unsigned int)-1;
v1 = 0;
if ( *(&p + 3093) >= 0 )
return v1;
v3[2] = 0;
*(_WORD *)v3 = 0;
v3[1] = *((_WORD *)&p + 1564);
ftdi_bb_io(v3);
*(_WORD *)&v3[1] = 0;
ftdi_bb_io(v3);
io_usleep(55000LL);
*(_WORD *)&v3[1] = 256;
ftdi_bb_io(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int64 v0; // rax
unsigned int v1; // er12
_BYTE v3[9]; // [rsp+Dh] [rbp-Bh] BYREF

if ( (int)ftdi_init(&handle) < 0 )
return (unsigned int)-1;
v0 = strtoul((const char *)(unsigned int)((_DWORD)&p + 2576), 0LL, 0);
if ( (int)ftdi_set_interface(&handle, v0) < 0 )
return (unsigned int)-1;
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 (unsigned int)-1;
}
}
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 (unsigned int)-1;
}
if ( (int)func3(0) < 0 || (int)ftdi_set_baudrate(&handle, *((unsigned int *)&p + 781)) < 0 )
return (unsigned int)-1;
v1 = 0;
if ( *(&p + 3093) >= 0 )
return v1;
v3[2] = 0;
*(_WORD *)v3 = 0;
v3[1] = *((_WORD *)&p + 1564);
func4(v3);
*(_WORD *)&v3[1] = 0;
func4(v3);
io_usleep(55000LL);
*(_WORD *)&v3[1] = 256;
func4(v3);
return 0LL;
}

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


repo: GBert/openwrt-files

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

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

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

if ( *(&p + 3093) < 0 )
{
v1 = *((_WORD *)&p + 1564);
v2 = 0;
HIBYTE(v3) = 1;
LOBYTE(v3) = v1;
func4(&v2);
v3 = 0;
func4(&v2);
io_usleep(55000LL);
v3 = 256;
func4(&v2);
}
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);
}

return_type: void

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[2] = a1;
v2[0] = 0;
v2[1] = result;
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[2] = a1;
v2[0] = 0;
v2[1] = result;
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)
{
__int16 v1; // ax
char v3[11]; // [rsp+Dh] [rbp-Bh] BYREF

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

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)
{
__int16 v1; // ax
char v3[11]; // [rsp+Dh] [rbp-Bh] BYREF

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

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)
{
__int16 v1; // ax
char v3[11]; // [rsp+Dh] [rbp-Bh] BYREF

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

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()
{
__int16 v0; // ax
char v2[2]; // [rsp+Dh] [rbp-Bh] BYREF
unsigned __int8 v3; // [rsp+Fh] [rbp-9h]

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

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

return_type: unsigned char


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

variable_types:
i
unsigned int

return_type: int


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[1] = a1;
v2[0] = 1;
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[1] = a1;
v2[0] = 1;
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; // cl
int v3; // eax
int v4; // edx

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

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
__int64 result; // rax
char v2; // cl
int v3; // eax
int v4; // edx

result = 0LL;
if ( *a1 )
{
if ( a1[1] )
{
func0(*a1);
v3 = func0(v2);
result = v3 | (unsigned int)(16 * v4);
}
}
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)
{
__int64 result; // rax
unsigned int v3; // edx

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

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

result = 0xFFFFFFFFLL;
v3 = **(_DWORD **)a2;
if ( **(_DWORD **)a1 >= v3 )
result = **(_DWORD **)a1 != 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; // er12
const char *v3; // rcx

v2 = 0;
if ( a1 < 0 )
{
v3 = error_5;
v2 = *((_DWORD *)&p + 785) & 1;
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 func3(int a1, unsigned int a2)
{
unsigned int v2; // er12
const char *v3; // rcx

v2 = 0;
if ( a1 < 0 )
{
v3 = error_5;
v2 = *((_DWORD *)&p + 785) & 1;
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; // r12
void **v3; // rbp
void **v4; // rbx
void *v5; // rdi

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

canonical_decompiled_code:
void __fastcall func9(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbp
void **v4; // rbx
void *v5; // rdi

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

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


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

return_type: char *


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)
{
unsigned int v4; // er14
int v5; // ebx
int v6; // edx
__int64 v7; // rdx
__int64 v8; // 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 )
{
v8 = 0LL;
}
else
{
v5 = 0;
do
{
v6 = (int)a3 >> v5++;
v7 = v6 & 1;
v4 ^= v7;
io_clock_out_bits(a1, a1, v7, 1LL);
}
while ( !io_stop && v5 != 8 );
v8 = v4;
}
io_clock_out_bits(a1, a1, v8, 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)
{
unsigned int v4; // er14
int v5; // ebx
int v6; // edx
__int64 v7; // rdx
__int64 v8; // 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 )
{
v8 = 0LL;
}
else
{
v5 = 0;
do
{
v6 = (int)a3 >> v5++;
v7 = v6 & 1;
v4 ^= v7;
io_clock_out_bits(a1, a1, v7, 1LL);
}
while ( !io_stop && v5 != 8 );
v8 = v4;
}
io_clock_out_bits(a1, a1, v8, 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; // ebp
int v5; // er13
int v6; // eax
char v7; // cl
unsigned int v8; // er12

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

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2, unsigned __int8 *a3)
{
int v4; // ebp
int v5; // er13
int v6; // eax
char v7; // cl
unsigned int v8; // er12

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

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 v7; // er13
unsigned __int8 v10; // r15
__int64 v11; // rcx
int v12; // er14
unsigned int v13; // eax
unsigned int v15; // [rsp+4h] [rbp-54h]
__int64 v16; // [rsp+8h] [rbp-50h]
unsigned __int8 v17[57]; // [rsp+1Fh] [rbp-39h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2, unsigned __int8 *a3, int a4, int *a5, __int64 a6)
{
int v7; // er13
unsigned __int8 v10; // r15
__int64 v11; // rcx
int v12; // er14
unsigned int v13; // eax
unsigned int v15; // [rsp+4h] [rbp-54h]
__int64 v16; // [rsp+8h] [rbp-50h]
unsigned __int8 v17[57]; // [rsp+1Fh] [rbp-39h] BYREF

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

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:
const char *__fastcall icspio_err(int a1)
{
const char *result; // rax

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

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

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

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)
{
unsigned int v1; // er12
unsigned __int8 v3; // [rsp+Bh] [rbp-Dh] BYREF
unsigned int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
unsigned int v1; // er12
unsigned __int8 v3; // [rsp+Bh] [rbp-Dh] BYREF
unsigned int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

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

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)
{
unsigned int v1; // er12
const char *v3; // rax
__int64 v4; // rcx
unsigned __int8 v5; // [rsp+Bh] [rbp-Dh] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
unsigned int v1; // er12
const char *v3; // rax
__int64 v4; // rcx
unsigned __int8 v5; // [rsp+Bh] [rbp-Dh] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

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

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; // rbp
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 ( v0 == 0x4000 )
{
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 )
{
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 )
goto LABEL_11;
}
}
else if ( (v4.st_mode & 0xF000) == 0x8000 )
{
goto LABEL_11;
}
}
return -1;
}
LABEL_11:
if ( getuid() )
{
if ( getuid() != v4.st_uid )
{
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);
}

canonical_decompiled_code:
int func0()
{
int v0; // eax
char *v1; // rbp
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 ( v0 == 0x4000 )
{
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 )
{
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 )
goto LABEL_11;
}
}
else if ( (v4.st_mode & 0xF000) == 0x8000 )
{
goto LABEL_11;
}
}
return -1;
}
LABEL_11:
if ( getuid() )
{
if ( getuid() != v4.st_uid )
{
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);
}

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
__int128 v8; // [rsp+0h] [rbp-208h] BYREF
char v9[496]; // [rsp+10h] [rbp-1F8h] BYREF

v0 = getenv("PICKLE");
if ( v0 )
{
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);
LODWORD(v2) = getdotpath();
if ( !(_DWORD)v2 )
return (int)v2;
}
else if ( v1 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
LODWORD(v2) = getdotpath();
if ( !(_DWORD)v2 )
return (int)v2;
}
else
{
LODWORD(v2) = getdotpath();
if ( !(_DWORD)v2 )
return (int)v2;
}
printf("%s: warning: invalid PICKLE environment variable\n", "getdotfile");
}
memset(v9, 0, sizeof(v9));
v8 = 0LL;
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_18;
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_18:
v2 = getenv("USER");
if ( !v2 )
goto LABEL_23;
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_23:
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
__int128 v8; // [rsp+0h] [rbp-208h] BYREF
char v9[496]; // [rsp+10h] [rbp-1F8h] BYREF

v0 = getenv("PICKLE");
if ( v0 )
{
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);
LODWORD(v2) = func0();
if ( !(_DWORD)v2 )
return (int)v2;
}
else if ( v1 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "getdotfile");
io_exit(70LL);
LODWORD(v2) = func0();
if ( !(_DWORD)v2 )
return (int)v2;
}
else
{
LODWORD(v2) = func0();
if ( !(_DWORD)v2 )
return (int)v2;
}
printf("%s: warning: invalid PICKLE environment variable\n", "getdotfile");
}
memset(v9, 0, sizeof(v9));
v8 = 0LL;
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_18;
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_18:
v2 = getenv("USER");
if ( !v2 )
goto LABEL_23;
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_23:
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; // rbp
char v3; // al
char *v4; // rdx
int v5; // ecx
__int64 v6; // rsi
char *v7; // rcx

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 )
{
v7 = v2;
}
else
{
v4 = dest + 1;
v5 = 0;
do
{
if ( v3 > 32 )
{
v6 = v5++;
v2[v6] = v3;
}
v3 = *v4++;
}
while ( v3 && v3 != 35 );
v7 = &v2[v5];
}
*v7 = 0;
strcpy(dest, v2);
free(v2);
return dest;
}

canonical_decompiled_code:
char *__fastcall func2(char *dest, int a2)
{
char *v2; // rbp
char v3; // al
char *v4; // rdx
int v5; // ecx
__int64 v6; // rsi
char *v7; // rcx

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 )
{
v7 = v2;
}
else
{
v4 = dest + 1;
v5 = 0;
do
{
if ( v3 > 32 )
{
v6 = v5++;
v2[v6] = v3;
}
v3 = *v4++;
}
while ( v3 && v3 != 35 );
v7 = &v2[v5];
}
*v7 = 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)
{
unsigned int v2; // edi
__int64 result; // rax

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

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

v2 = 2 * a1;
result = a1 >> 1;
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()
{
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();
if ( !strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "FTDI") )
return ftdi_bb_backend();
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
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();
if ( !strcasecmp((const char *)(unsigned int)((_DWORD)p + 1040), "FTDI") )
return ftdi_bb_backend();
return 0LL;
}

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 *


decompiled_code:
void __fastcall io_usleep(__useconds_t useconds)
{
unsigned int v1; // eax
__int64 v2; // rax
void (__fastcall *v3)(_QWORD); // rax
__int64 v4; // rax
struct timeval v5; // [rsp-8h] [rbp-38h] BYREF
struct timeval tv; // [rsp+8h] [rbp-28h] BYREF

if ( useconds )
{
v1 = *(_DWORD *)&p[3108];
if ( v1 != 2 )
{
if ( v1 > 2 )
{
if ( v1 != 3 )
return;
}
else
{
if ( v1 )
{
v2 = *(_QWORD *)&p[3160];
if ( v2 )
{
v3 = *(void (__fastcall **)(_QWORD))(v2 + 40);
if ( v3 )
{
LABEL_7:
v3(useconds);
return;
}
}
goto LABEL_9;
}
if ( useconds <= 9 )
{
v4 = *(_QWORD *)&p[3160];
if ( v4 )
{
v3 = *(void (__fastcall **)(_QWORD))(v4 + 40);
if ( v3 )
goto LABEL_7;
}
goto LABEL_17;
}
if ( useconds <= 0x63 )
{
LABEL_17:
gettimeofday(&v5, 0LL);
do
gettimeofday(&tv, 0LL);
while ( useconds > LODWORD(tv.tv_usec)
- LODWORD(v5.tv_usec)
+ (LODWORD(tv.tv_usec) - LODWORD(v5.tv_usec) < 0 ? 0xF4240 : 0) );
return;
}
}
LABEL_9:
usleep(useconds);
return;
}
gettimeofday(&v5, 0LL);
do
gettimeofday(&tv, 0LL);
while ( useconds > LODWORD(tv.tv_usec)
- LODWORD(v5.tv_usec)
+ (LODWORD(tv.tv_usec) - LODWORD(v5.tv_usec) < 0 ? 0xF4240 : 0) );
}
}

canonical_decompiled_code:
void __fastcall func9(__useconds_t useconds)
{
unsigned int v1; // eax
__int64 v2; // rax
void (__fastcall *v3)(_QWORD); // rax
__int64 v4; // rax
struct timeval v5; // [rsp-8h] [rbp-38h] BYREF
struct timeval tv; // [rsp+8h] [rbp-28h] BYREF

if ( useconds )
{
v1 = *(_DWORD *)&p[3108];
if ( v1 != 2 )
{
if ( v1 > 2 )
{
if ( v1 != 3 )
return;
}
else
{
if ( v1 )
{
v2 = *(_QWORD *)&p[3160];
if ( v2 )
{
v3 = *(void (__fastcall **)(_QWORD))(v2 + 40);
if ( v3 )
{
LABEL_7:
v3(useconds);
return;
}
}
goto LABEL_9;
}
if ( useconds <= 9 )
{
v4 = *(_QWORD *)&p[3160];
if ( v4 )
{
v3 = *(void (__fastcall **)(_QWORD))(v4 + 40);
if ( v3 )
goto LABEL_7;
}
goto LABEL_17;
}
if ( useconds <= 0x63 )
{
LABEL_17:
gettimeofday(&v5, 0LL);
do
gettimeofday(&tv, 0LL);
while ( useconds > LODWORD(tv.tv_usec)
- LODWORD(v5.tv_usec)
+ (LODWORD(tv.tv_usec) - LODWORD(v5.tv_usec) < 0 ? 0xF4240 : 0) );
return;
}
}
LABEL_9:
usleep(useconds);
return;
}
gettimeofday(&v5, 0LL);
do
gettimeofday(&tv, 0LL);
while ( useconds > LODWORD(tv.tv_usec)
- LODWORD(v5.tv_usec)
+ (LODWORD(tv.tv_usec) - LODWORD(v5.tv_usec) < 0 ? 0xF4240 : 0) );
}
}

original_code:
void
io_usleep(uint32_t n)
{
 struct timeval tv1, tv2;
 int diff;
 if (n == 0)
  return;
 switch (p.sleep_algo) {
 case (0):
  if (n < 10 && p.io && p.io->usleep) {
   p.io->usleep(n);
   return;
  }
  if (n < 100) {
   gettimeofday(&tv1, ((void *)0));
   do {
    gettimeofday(&tv2, ((void *)0));
    diff = tv2.tv_usec - tv1.tv_usec;
    if (diff < 0)
     diff += 1000000;
   }
   while (diff < n);
   return;
  }
  usleep(n);
  break;
 case (1):
  if (p.io && p.io->usleep) {
   p.io->usleep(n);
   return;
  }
 case (3):
  usleep(n);
  break;
 case (2):
  gettimeofday(&tv1, ((void *)0));
  do {
   gettimeofday(&tv2, ((void *)0));
   diff = tv2.tv_usec - tv1.tv_usec;
   if (diff < 0)
    diff += 1000000;
  }
  while (diff < n);
  break;
 }
}

canonical_original_code:
void io_usleep(unsigned int n)
{
 struct timeval tv1; struct timeval tv2;
 int diff;
 if (n == 0)
  return;
 switch (p.sleep_algo) {
 case (0):
  if (n < 10 && p.io && p.io->usleep) {
   p.io->usleep(n);
   return;
  }
  if (n < 100) {
   gettimeofday(&tv1, ((void *)0));
   do {
    gettimeofday(&tv2, ((void *)0));
    diff = tv2.tv_usec - tv1.tv_usec;
    if (diff < 0)
     diff += 1000000;
   }
   while (diff < n);
   return;
  }
  usleep(n);
  break;
 case (1):
  if (p.io && p.io->usleep) {
   p.io->usleep(n);
   return;
  }
 case (3):
  usleep(n);
  break;
 case (2):
  gettimeofday(&tv1, ((void *)0));
  do {
   gettimeofday(&tv2, ((void *)0));
   diff = tv2.tv_usec - tv1.tv_usec;
   if (diff < 0)
    diff += 1000000;
  }
  while (diff < n);
  break;
 }
}

variable_types:
n
unsigned int

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

variable_types: diff int


decompiled_code:
__int64 __fastcall io_set_pgm(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 48);
if ( v3 )
{
if ( (p[3092] & 0x20) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 48);
if ( v3 )
{
if ( (p[3092] & 0x20) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

original_code:
void
io_set_pgm(uint8_t pgm)
{
 if (p.io && p.io->set_pgm) {
  if (p.bitrules & (0x00000020))
   pgm = (0x01) - pgm;
  p.io->set_pgm(pgm);
 }
}

canonical_original_code:
void io_set_pgm(unsigned char pgm)
{
 if (p.io && p.io->set_pgm) {
  if (p.bitrules & (0x00000020))
   pgm = (0x01) - pgm;
  p.io->set_pgm(pgm);
 }
}

variable_types:
pgm
unsigned char


decompiled_code:
__int64 __fastcall io_set_vpp(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 56);
if ( v3 )
{
if ( (p[3092] & 4) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 56);
if ( v3 )
{
if ( (p[3092] & 4) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

original_code:
void
io_set_vpp(uint8_t vpp)
{
 if (p.io && p.io->set_vpp) {
  if (p.bitrules & (0x00000004))
   vpp = (0x01) - vpp;
  p.io->set_vpp(vpp);
 }
}

canonical_original_code:
void io_set_vpp(unsigned char vpp)
{
 if (p.io && p.io->set_vpp) {
  if (p.bitrules & (0x00000004))
   vpp = (0x01) - vpp;
  p.io->set_vpp(vpp);
 }
}

variable_types:
vpp
unsigned char


original_code:
void
io_close(int run)
{
 if (p.iot == (0))
  return;
 if (p.io != ((void *)0)) {
  if (p.io->release) {
   p.io->release();
  }
  if (p.io->run) {
   if ((p.bitrules & (0x00000800)) == 0) {
    io_usleep(10);
    if (run)
     io_set_vpp((0x01));
    else
     io_set_vpp((0x00));
   }
  }
  if (p.io->close) {
   p.io->close();
  }
  p.io = ((void *)0);
 }
 p.iot = (0);
 io_signal_off();
}

variable_types: run int


decompiled_code:
void __fastcall __noreturn io_exit(int status)
{
fflush(stdout);
if ( p[3168] )
io_close(*(_DWORD *)&p[3092] & 0x1000);
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func13(int status)
{
fflush(stdout);
if ( p[3168] )
func12(*(_DWORD *)&p[3092] & 0x1000);
exit(status);
}

original_code:
void
io_exit(int err)
{
 fflush(stdout);
 if (p.iot != (0))
  io_close(p.bitrules & (0x00001000));
 exit(err);
}

variable_types: err int


decompiled_code:
char *io_error()
{
__int64 v0; // rax
__int64 (*v1)(void); // rax
const char *v2; // rax
int v3; // eax

v0 = *(_QWORD *)&p[3160];
if ( v0 && (v1 = *(__int64 (**)(void))(v0 + 32)) != 0LL )
{
v2 = (const char *)v1();
v3 = snprintf(
msg_6,
0x200uLL,
"%s [DEVICE=%s] [IFACE=%s]",
v2,
(const char *)(unsigned int)((_DWORD)p + 1040),
(const char *)(unsigned int)((_DWORD)p + 2576));
if ( v3 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "io_error");
io_exit(71);
}
}
else
{
v3 = snprintf(
msg_6,
0x200uLL,
"Can't open unsupported I/O [DEVICE=%s] [IFACE=%s].\n\n\tRun `pickle` to list supported backend I/O",
(const char *)(unsigned int)((_DWORD)p + 1040),
(const char *)(unsigned int)((_DWORD)p + 2576));
}
if ( v3 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "io_error");
io_exit(70);
}
return msg_6;
}

canonical_decompiled_code:
char *func14()
{
__int64 v0; // rax
__int64 (*v1)(void); // rax
const char *v2; // rax
int v3; // eax

v0 = *(_QWORD *)&p[3160];
if ( v0 && (v1 = *(__int64 (**)(void))(v0 + 32)) != 0LL )
{
v2 = (const char *)v1();
v3 = snprintf(
msg_6,
0x200uLL,
"%s [DEVICE=%s] [IFACE=%s]",
v2,
(const char *)(unsigned int)((_DWORD)p + 1040),
(const char *)(unsigned int)((_DWORD)p + 2576));
if ( v3 < 0 )
{
printf("%s: fatal error: snprintf failed\n", "io_error");
func13(71);
}
}
else
{
v3 = snprintf(
msg_6,
0x200uLL,
"Can't open unsupported I/O [DEVICE=%s] [IFACE=%s].\n\n\tRun `pickle` to list supported backend I/O",
(const char *)(unsigned int)((_DWORD)p + 1040),
(const char *)(unsigned int)((_DWORD)p + 2576));
}
if ( v3 > 511 )
{
printf("%s: fatal error: snprintf overrun\n", "io_error");
func13(70);
}
return msg_6;
}

original_code:
char *
io_error(void)
{
 static char msg[(512)] = {0};
 int rc;
 if (p.io && p.io->error)
  rc = snprintf(msg, (512), "%s [DEVICE=%s] [IFACE=%s]",
   p.io->error(), p.device, p.iface);
 else
  rc = snprintf(msg, (512), "Can't open unsupported I/O [DEVICE=%s] [IFACE=%s].\n\n"
   "	Run `pickle` to list supported backend I/O", p.device, p.iface);
 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);
 }
 return msg;
}

canonical_original_code:
char *io_error()
{
 static char msg[-1] = {0};
 int rc;
 if (p.io && p.io->error)
  rc = snprintf(msg, (512), "%s [DEVICE=%s] [IFACE=%s]",
   p.io->error(), p.device, p.iface);
 else
  rc = snprintf(msg, (512), "Can't open unsupported I/O [DEVICE=%s] [IFACE=%s].\n\n"
   "	Run `pickle` to list supported backend I/O", p.device, p.iface);
 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);
 }
 return msg;
}

variable_types:
msg
char [-1]

variable_types: rc int


decompiled_code:
__int64 __fastcall io_set_pgd(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 64);
if ( v3 )
{
if ( (p[3092] & 1) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 64);
if ( v3 )
{
if ( (p[3092] & 1) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

original_code:
void
io_set_pgd(uint8_t pgd)
{
 if (p.io && p.io->set_pgd) {
  if (p.bitrules & (0x00000001))
   pgd = (0x01) - pgd;
  p.io->set_pgd(pgd);
 }
}

canonical_original_code:
void io_set_pgd(unsigned char pgd)
{
 if (p.io && p.io->set_pgd) {
  if (p.bitrules & (0x00000001))
   pgd = (0x01) - pgd;
  p.io->set_pgd(pgd);
 }
}

variable_types:
pgd
unsigned char


decompiled_code:
__int64 __fastcall io_set_pgc(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 72);
if ( v3 )
{
if ( (p[3092] & 2) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned int a1)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 (__fastcall *v3)(_QWORD); // rcx

v1 = *(_QWORD *)&p[3160];
result = a1;
if ( v1 )
{
v3 = *(__int64 (__fastcall **)(_QWORD))(v1 + 72);
if ( v3 )
{
if ( (p[3092] & 2) != 0 )
LOBYTE(result) = 1 - a1;
result = v3((unsigned __int8)result);
}
}
return result;
}

original_code:
void
io_set_pgc(uint8_t pgc)
{
 if (p.io && p.io->set_pgc) {
  if (p.bitrules & (0x00000002))
   pgc = (0x01) - pgc;
  p.io->set_pgc(pgc);
 }
}

canonical_original_code:
void io_set_pgc(unsigned char pgc)
{
 if (p.io && p.io->set_pgc) {
  if (p.bitrules & (0x00000002))
   pgc = (0x01) - pgc;
  p.io->set_pgc(pgc);
 }
}

variable_types:
pgc
unsigned char


decompiled_code:
void __fastcall io_clock_bit(__useconds_t useconds, __useconds_t a2)
{
io_set_pgc(1u);
io_usleep(a2);
io_set_pgc(0);
io_usleep(useconds);
}

canonical_decompiled_code:
void __fastcall func17(__useconds_t useconds, __useconds_t a2)
{
func16(1u);
func9(a2);
func16(0);
func9(useconds);
}

original_code:
static inline void
io_clock_bit(uint32_t ldly, uint32_t hdly)
{
 io_set_pgc((0x01));
 io_usleep(hdly);
 io_set_pgc((0x00));
 io_usleep(ldly);
}

canonical_original_code:
static inline void io_clock_bit(unsigned int ldly, unsigned int hdly)
{
 io_set_pgc((0x01));
 io_usleep(hdly);
 io_set_pgc((0x00));
 io_usleep(ldly);
}

variable_types:
ldly
unsigned int

variable_types:
hdly
unsigned int


original_code:
uint8_t
io_get_pgd(void)
{
 uint8_t pgd = 0;
 if (p.io && p.io->get_pgd) {
  pgd = p.io->get_pgd();
  if (p.bitrules & (0x00000008))
   pgd = (0x01) - pgd;
 }
 return pgd;
}

canonical_original_code:
unsigned char io_get_pgd()
{
 unsigned char pgd = 0;
 if (p.io && p.io->get_pgd) {
  pgd = p.io->get_pgd();
  if (p.bitrules & (0x00000008))
   pgd = (0x01) - pgd;
 }
 return pgd;
}


decompiled_code:
__int64 __fastcall io_configure(char a1, char a2)
{
__int64 result; // rax

result = *(_QWORD *)&p[3160];
p[3144] = a1;
p[3145] = a2;
if ( result )
{
result = *(_QWORD *)(result + 88);
if ( result )
result = ((__int64 (*)(void))result)();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(char a1, char a2)
{
__int64 result; // rax

result = *(_QWORD *)&p[3160];
p[3144] = a1;
p[3145] = a2;
if ( result )
{
result = *(_QWORD *)(result + 88);
if ( result )
result = ((__int64 (*)(void))result)();
}
return result;
}

original_code:
void
io_configure(uint8_t clock_falling, uint8_t msb_first)
{
 p.clock_falling = clock_falling;
 p.msb_first = msb_first;
 if (p.io && p.io->configure)
  p.io->configure();
}

canonical_original_code:
void io_configure(unsigned char clock_falling, unsigned char msb_first)
{
 p.clock_falling = clock_falling;
 p.msb_first = msb_first;
 if (p.io && p.io->configure)
  p.io->configure();
}


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

result = p[773];
if ( (result & 0x10) != 0 )
return io_set_pgd(1u);
if ( (result & 0x80u) != 0LL )
result = io_set_pgd(0);
return result;
}

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

result = p[773];
if ( (result & 0x10) != 0 )
return func15(1u);
if ( (result & 0x80u) != 0LL )
result = func15(0);
return result;
}

original_code:
void
io_data_input(void)
{
 if (p.bitrules & (0x00000010)) {
  io_set_pgd((0x01));
 }
 else if (p.bitrules & (0x00000080)) {
  io_set_pgd((0x00));
 }
}

canonical_original_code:
void io_data_input()
{
 if (p.bitrules & (0x00000010)) {
  io_set_pgd((0x01));
 }
 else if (p.bitrules & (0x00000080)) {
  io_set_pgd((0x00));
 }
}


decompiled_code:
__int64 __fastcall io_clock_in_bits(__useconds_t useconds, __useconds_t a2, int a3)
{
unsigned int v4; // ebp
int v5; // er14
unsigned int v6; // er12

v4 = io_first_mask(a3);
io_data_input();
if ( !(_BYTE)a3 )
return 0;
v5 = a3 - 1;
v6 = 0;
do
{
if ( !LOBYTE(p[786]) )
io_clock_bit(useconds, a2);
if ( (unsigned __int8)io_get_pgd() )
v6 |= v4;
if ( LOBYTE(p[786]) )
io_clock_bit(useconds, a2);
--v5;
v4 = io_next_mask(v4);
}
while ( (_BYTE)v5 != 0xFF );
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func21(__useconds_t useconds, __useconds_t a2, int a3)
{
unsigned int v4; // ebp
int v5; // er14
unsigned int v6; // er12

v4 = func1(a3);
func20();
if ( !(_BYTE)a3 )
return 0;
v5 = a3 - 1;
v6 = 0;
do
{
if ( !LOBYTE(p[786]) )
func17(useconds, a2);
if ( (unsigned __int8)func18() )
v6 |= v4;
if ( LOBYTE(p[786]) )
func17(useconds, a2);
--v5;
v4 = func2(v4);
}
while ( (_BYTE)v5 != 0xFF );
return v6;
}

original_code:
uint32_t
io_clock_in_bits(uint32_t ldly, uint32_t hdly, uint8_t nbits)
{
 uint32_t bits = 0, mask = io_first_mask(nbits);
 io_data_input();
 while (nbits--) {
  if (!p.clock_falling)
   io_clock_bit(ldly, hdly);
  if (io_get_pgd())
   bits |= mask;
  if (p.clock_falling)
   io_clock_bit(ldly, hdly);
  mask = io_next_mask(mask);
 }
 return bits;
}

canonical_original_code:
unsigned int io_clock_in_bits(unsigned int ldly, unsigned int hdly, unsigned char nbits)
{
 unsigned int bits = 0; unsigned int mask = io_first_mask(nbits);
 io_data_input();
 while (nbits--) {
  if (!p.clock_falling)
   io_clock_bit(ldly, hdly);
  if (io_get_pgd())
   bits |= mask;
  if (p.clock_falling)
   io_clock_bit(ldly, hdly);
  mask = io_next_mask(mask);
 }
 return bits;
}

variable_types:
nbits
unsigned char

variable_types:
bits
unsigned int


decompiled_code:
__int64 __fastcall io_clock_out_bits(__useconds_t useconds, __useconds_t a2, int a3, char a4)
{
__int64 result; // rax
int v6; // edx
unsigned int v7; // ebp
int v8; // er14

result = io_first_mask(a4);
if ( (_BYTE)v6 )
{
v7 = result;
v8 = v6 - 1;
do
{
--v8;
io_set_pgd((v7 & a3) != 0);
io_clock_bit(useconds, a2);
result = io_next_mask(v7);
v7 = result;
}
while ( (_BYTE)v8 != 0xFF );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func22(__useconds_t useconds, __useconds_t a2, int a3, char a4)
{
__int64 result; // rax
int v6; // edx
unsigned int v7; // ebp
int v8; // er14

result = func1(a4);
if ( (_BYTE)v6 )
{
v7 = result;
v8 = v6 - 1;
do
{
--v8;
func15((v7 & a3) != 0);
func17(useconds, a2);
result = func2(v7);
v7 = result;
}
while ( (_BYTE)v8 != 0xFF );
}
return result;
}

original_code:
void
io_clock_out_bits(uint32_t ldly, uint32_t hdly, uint32_t bits, uint8_t nbits)
{
 uint32_t mask = io_first_mask(nbits);
 while (nbits--) {
  io_set_pgd((bits & mask) != 0);
  io_clock_bit(ldly, hdly);
  mask = io_next_mask(mask);
 }
}

canonical_original_code:
void io_clock_out_bits(unsigned int ldly, unsigned int hdly, unsigned int bits, unsigned char nbits)
{
 unsigned int mask = io_first_mask(nbits);
 while (nbits--) {
  io_set_pgd((bits & mask) != 0);
  io_clock_bit(ldly, hdly);
  mask = io_next_mask(mask);
 }
}

variable_types:
mask
unsigned int


decompiled_code:
__int64 __fastcall io_clock_bit_4phase(unsigned __int8 a1, unsigned __int8 a2)
{
unsigned int v2; // er12

io_set_pgd(a2);
io_clock_bit(p[775], p[776]);
io_set_pgd(a1);
io_clock_bit(p[775], p[776]);
io_data_input();
io_get_pgd();
io_clock_bit(p[775], p[776]);
v2 = io_get_pgd();
io_clock_bit(p[775], p[776]);
return v2;
}

original_code:
uint8_t
io_clock_bit_4phase(uint8_t tms, uint8_t tdi)
{
 uint8_t tdo;
 io_set_pgd(tdi);
 io_clock_bit(p.sleep_low, p.sleep_high);
 io_set_pgd(tms);
 io_clock_bit(p.sleep_low, p.sleep_high);
 io_data_input();
 io_get_pgd();
 io_clock_bit(p.sleep_low, p.sleep_high);
 tdo = io_get_pgd();
 io_clock_bit(p.sleep_low, p.sleep_high);
 return tdo;
}

canonical_original_code:
unsigned char io_clock_bit_4phase(unsigned char tms, unsigned char tdi)
{
 unsigned char tdo;
 io_set_pgd(tdi);
 io_clock_bit(p.sleep_low, p.sleep_high);
 io_set_pgd(tms);
 io_clock_bit(p.sleep_low, p.sleep_high);
 io_data_input();
 io_get_pgd();
 io_clock_bit(p.sleep_low, p.sleep_high);
 tdo = io_get_pgd();
 io_clock_bit(p.sleep_low, p.sleep_high);
 return tdo;
}

variable_types:
tms
unsigned char

variable_types:
tdi
unsigned char

variable_types:
tdo
unsigned char


decompiled_code:
int __fastcall io_program_feedback(char a1)
{
int result; // eax
bool v2; // cc
__int64 v3; // rax

if ( c_last_3 != a1 || (result = c_count_2, v2 = c_count_2 <= p[774], ++c_count_2, !v2) )
{
v3 = a_count_1++ & 3;
result = fprintf(stderr, "%c[%c]\r", (unsigned int)arrow_0[v3], (unsigned int)a1);
c_last_3 = a1;
c_count_2 = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func24(char a1)
{
int result; // eax
bool v2; // cc
__int64 v3; // rax

if ( c_last_3 != a1 || (result = c_count_2, v2 = c_count_2 <= p[774], ++c_count_2, !v2) )
{
v3 = a_count_1++ & 3;
result = fprintf(stderr, "%c[%c]\r", (unsigned int)arrow_0[v3], (unsigned int)a1);
c_last_3 = a1;
c_count_2 = 0;
}
return result;
}

original_code:
void
io_program_feedback(char c)
{
 static int c_count = 0, a_count = 0;
 static const char arrow[] = "|/-\\";
 static char c_last = 0;
 if ((c_last != c) || (c_count++ > p.busy)) {
  fprintf(stderr, "%c[%c]\r", arrow[a_count++ % strlen(arrow)], c);
  c_last = c;
  c_count = 0;
 }
}

canonical_original_code:
void io_program_feedback(char c)
{
 static int c_count = 0; static int a_count = 0;
 static char arrow[] = "|/-\\";
 static char c_last = 0;
 if ((c_last != c) || (c_count++ > p.busy)) {
  fprintf(stderr, "%c[%c]\r", arrow[a_count++ % strlen(arrow)], c);
  c_last = c;
  c_count = 0;
 }
}

variable_types: c char

variable_types:
arrow
char []


decompiled_code:
__int64 __fastcall io_clock_bits_4phase(unsigned __int8 a1, unsigned int a2, unsigned int a3)
{
int v4; // ebp
int v5; // ebx
unsigned int v6; // er12
unsigned __int8 v7; // al
char v8; // cl

if ( !p[774] )
{
v4 = a1;
if ( a1 )
goto LABEL_3;
return 0LL;
}
io_program_feedback(43);
v4 = a1;
if ( !a1 )
return 0LL;
LABEL_3:
v5 = 0;
v6 = 0;
do
{
v7 = io_clock_bit_4phase((a2 >> v5) & 1, (a3 >> v5) & 1);
v8 = v5++;
v6 |= v7 << v8;
}
while ( v5 != v4 );
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func25(unsigned __int8 a1, unsigned int a2, unsigned int a3)
{
int v4; // ebp
int v5; // ebx
unsigned int v6; // er12
unsigned __int8 v7; // al
char v8; // cl

if ( !p[774] )
{
v4 = a1;
if ( a1 )
goto LABEL_3;
return 0LL;
}
func24(43);
v4 = a1;
if ( !a1 )
return 0LL;
LABEL_3:
v5 = 0;
v6 = 0;
do
{
v7 = func23((a2 >> v5) & 1, (a3 >> v5) & 1);
v8 = v5++;
v6 |= v7 << v8;
}
while ( v5 != v4 );
return v6;
}

original_code:
uint32_t
io_clock_bits_4phase(uint8_t nbits, uint32_t tms, uint32_t tdi)
{
 uint32_t tdo = 0;
 if (p.busy)
  io_program_feedback('+');
 for (uint32_t i = 0; i < nbits; ++i)
  tdo |= io_clock_bit_4phase((tms >> i) & 1, (tdi >> i) & 1) << i;
 return tdo;
}

canonical_original_code:
unsigned int io_clock_bits_4phase(unsigned char nbits, unsigned int tms, unsigned int tdi)
{
 unsigned int tdo = 0;
 if (p.busy)
  io_program_feedback('+');
 for (unsigned int i = 0; i < nbits; ++i)
  tdo |= io_clock_bit_4phase((tms >> i) & 1, (tdi >> i) & 1) << i;
 return tdo;
}

variable_types:
tms
unsigned int

variable_types:
tdi
unsigned int

variable_types:
tdo
unsigned int

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall io_program_in(unsigned __int8 a1)
{
__int64 v1; // rax
__int64 (__fastcall *v2)(_QWORD); // rax
__int64 result; // rax

if ( p[774] )
io_program_feedback(105);
v1 = *(_QWORD *)&p[790];
if ( v1 && (v2 = *(__int64 (__fastcall **)(_QWORD))(v1 + 96)) != 0LL )
result = v2(a1);
else
result = io_clock_in_bits(p[775], p[776], a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func26(unsigned __int8 a1)
{
__int64 v1; // rax
__int64 (__fastcall *v2)(_QWORD); // rax
__int64 result; // rax

if ( p[774] )
func24(105);
v1 = *(_QWORD *)&p[790];
if ( v1 && (v2 = *(__int64 (__fastcall **)(_QWORD))(v1 + 96)) != 0LL )
result = v2(a1);
else
result = func21(p[775], p[776], a1);
return result;
}

original_code:
uint32_t
io_program_in(uint8_t nbits)
{
 if (p.busy)
  io_program_feedback('i');
 if (p.io && p.io->shift_in)
  return p.io->shift_in(nbits);
 return io_clock_in_bits(p.sleep_low, p.sleep_high, nbits);
}

canonical_original_code:
unsigned int io_program_in(unsigned char nbits)
{
 if (p.busy)
  io_program_feedback('i');
 if (p.io && p.io->shift_in)
  return p.io->shift_in(nbits);
 return io_clock_in_bits(p.sleep_low, p.sleep_high, nbits);
}


decompiled_code:
__int64 __fastcall io_program_out(unsigned int a1, unsigned __int8 a2)
{
__int64 v2; // rax
__int64 (__fastcall *v3)(_QWORD, _QWORD); // rax
__int64 result; // rax

if ( p[774] )
io_program_feedback(111);
v2 = *(_QWORD *)&p[790];
if ( v2 && (v3 = *(__int64 (__fastcall **)(_QWORD, _QWORD))(v2 + 104)) != 0LL )
result = v3(a1, a2);
else
result = io_clock_out_bits(p[775], p[776], a1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func27(unsigned int a1, unsigned __int8 a2)
{
__int64 v2; // rax
__int64 (__fastcall *v3)(_QWORD, _QWORD); // rax
__int64 result; // rax

if ( p[774] )
func24(111);
v2 = *(_QWORD *)&p[790];
if ( v2 && (v3 = *(__int64 (__fastcall **)(_QWORD, _QWORD))(v2 + 104)) != 0LL )
result = v3(a1, a2);
else
result = func22(p[775], p[776], a1, a2);
return result;
}

original_code:
void
io_program_out(uint32_t bits, uint8_t nbits)
{
 if (p.busy)
  io_program_feedback('o');
 if (p.io && p.io->shift_out)
  p.io->shift_out(bits, nbits);
 else
  io_clock_out_bits(p.sleep_low, p.sleep_high, bits, nbits);
}

canonical_original_code:
void io_program_out(unsigned int bits, unsigned char nbits)
{
 if (p.busy)
  io_program_feedback('o');
 if (p.io && p.io->shift_out)
  p.io->shift_out(bits, nbits);
 else
  io_clock_out_bits(p.sleep_low, p.sleep_high, bits, nbits);
}


decompiled_code:
const char *gpio_bb_error()
{
return "Can't open GPIO BIT-BANG I/O";
}

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

original_code:
char *
gpio_bb_error(void)
{
 return "Can't open GPIO BIT-BANG I/O";
}


decompiled_code:
__int64 gpio_bb_open()
{
fd = open((const char *)(unsigned int)((_DWORD)&p + 2576), 2);
if ( fd >= 0 )
return 0LL;
fd = -1;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func1()
{
fd = open((const char *)(unsigned int)((_DWORD)&p + 2576), 2);
if ( fd >= 0 )
return 0LL;
fd = -1;
return 0xFFFFFFFFLL;
}

original_code:
int
gpio_bb_open(void)
{
 fd = open(p.iface, 02);
 if (fd < 0) {
  fd = -1;
  return -1;
 }
 return 0;
}

canonical_original_code:
int gpio_bb_open()
{
 fd = open(p.iface, 02);
 if (fd < 0) {
  fd = -1;
  return -1;
 }
 return 0;
}


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

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

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

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

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

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


decompiled_code:
int __fastcall gpio_bb_set_pgm(char a1)
{
int result; // eax
__int16 v2; // [rsp+0h] [rbp-Ch] BYREF
char v3; // [rsp+2h] [rbp-Ah]
char v4; // [rsp+3h] [rbp-9h]

result = *((unsigned __int16 *)&p + 1568);
if ( (_WORD)result != 0xFFFF )
{
v4 = a1;
v2 = result;
v3 = 0;
result = ioctl(fd, 0xC008B4CDuLL, &v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(char a1)
{
int result; // eax
__int16 v2; // [rsp+0h] [rbp-Ch] BYREF
char v3; // [rsp+2h] [rbp-Ah]
char v4; // [rsp+3h] [rbp-9h]

result = *((unsigned __int16 *)&p + 1568);
if ( (_WORD)result != 0xFFFF )
{
v4 = a1;
v2 = result;
v3 = 0;
result = ioctl(fd, 0xC008B4CDuLL, &v2);
}
return result;
}

variable_types:
pgm
unsigned char

variable_types: struct gpio_bb_io { unsigned short pin; unsigned char dir; unsigned char bit; }


decompiled_code:
int __fastcall gpio_bb_set_vpp(char a1)
{
__int16 v1; // ax
__int16 v3; // [rsp+Ch] [rbp-Ch] BYREF
char v4; // [rsp+Eh] [rbp-Ah]
char v5; // [rsp+Fh] [rbp-9h]

v1 = *((_WORD *)&p + 1564);
v5 = a1;
v3 = v1;
v4 = 0;
return ioctl(fd, 0xC008B4CDuLL, &v3);
}

canonical_decompiled_code:
int __fastcall func4(char a1)
{
__int16 v1; // ax
__int16 v3; // [rsp+Ch] [rbp-Ch] BYREF
char v4; // [rsp+Eh] [rbp-Ah]
char v5; // [rsp+Fh] [rbp-9h]

v1 = *((_WORD *)&p + 1564);
v5 = a1;
v3 = v1;
v4 = 0;
return ioctl(fd, 0xC008B4CDuLL, &v3);
}

variable_types:
vpp
unsigned char

user_defined_types: struct gpio_bb_io { unsigned short pin; unsigned char dir; unsigned char bit; }


decompiled_code:
int __fastcall gpio_bb_set_pgd(char a1)
{
__int16 v1; // ax
__int16 v3; // [rsp+Ch] [rbp-Ch] BYREF
char v4; // [rsp+Eh] [rbp-Ah]
char v5; // [rsp+Fh] [rbp-9h]

v1 = *((_WORD *)&p + 1566);
v5 = a1;
v3 = v1;
v4 = 0;
return ioctl(fd, 0xC008B4CDuLL, &v3);
}

canonical_decompiled_code:
int __fastcall func5(char a1)
{
__int16 v1; // ax
__int16 v3; // [rsp+Ch] [rbp-Ch] BYREF
char v4; // [rsp+Eh] [rbp-Ah]
char v5; // [rsp+Fh] [rbp-9h]

v1 = *((_WORD *)&p + 1566);
v5 = a1;
v3 = v1;
v4 = 0;
return ioctl(fd, 0xC008B4CDuLL, &v3);
}

variable_types:
pgd
unsigned char