decompiled_code:
__int64 __fastcall usb_write_ram(unsigned int a1, int a2, unsigned int a3, char *a4)
{
char v4; // r12
__int64 result; // rax
__int16 v8; // r14
unsigned int v9; // ebx
int v10; // er14
char *v11; // r12
unsigned int v12; // er13

v4 = a3;
if ( a1 == 4096 )
{
v8 = a3;
usb_write_reg(18, a2);
v9 = a3 >> 9;
v10 = v8 & 0x1FF;
if ( (v4 & 7) != 0 )
{
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "usb_write_ram: length is not multiple of line size\n");
return 0xFFFFFFFFLL;
}
LABEL_6:
v11 = (char *)malloc(0x200uLL);
if ( v11 )
{
setup_xfer(a1, 512);
if ( v9 )
{
v12 = 0;
while ( 1 )
{
qmemcpy(v11, a4, 0x200uLL);
if ( (int)os_usb_write((unsigned int)cur_dev, 2LL, v11, 512LL) < 0 )
break;
++v12;
a4 += 512;
if ( v12 == v9 )
goto LABEL_13;
}
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "write not succesfful (xfer %d)\n", v12);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_13:
if ( v10 )
{
memcpy(v11, a4, v10);
if ( (int)os_usb_write((unsigned int)cur_dev, 2LL, v11, (unsigned int)v10) < 0 )
{
if ( do_debug )
debug("usb_write_ram", "write not succesfful (excess xfer)\n");
return 0xFFFFFFFFLL;
}
}
free(v11);
result = 0LL;
}
}
else
{
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "usb_write_ram: couldnt allocate scratchpad memory\n");
result = 0xFFFFFFFFLL;
}
return result;
}
if ( a1 == 0x2000 )
{
v9 = a3 >> 9;
v10 = a3 & 0x1FF;
goto LABEL_6;
}
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "usb_write_ram: invalid bank (0x%x)\n", a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, int a2, unsigned int a3, char *a4)
{
char v4; // r12
__int64 result; // rax
__int16 v8; // r14
unsigned int v9; // ebx
int v10; // er14
char *v11; // r12
unsigned int v12; // er13

v4 = a3;
if ( a1 == 4096 )
{
v8 = a3;
func5(18, a2);
v9 = a3 >> 9;
v10 = v8 & 0x1FF;
if ( (v4 & 7) != 0 )
{
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "usb_write_ram: length is not multiple of line size\n");
return 0xFFFFFFFFLL;
}
LABEL_6:
v11 = (char *)malloc(0x200uLL);
if ( v11 )
{
func4(a1, 512);
if ( v9 )
{
v12 = 0;
while ( 1 )
{
qmemcpy(v11, a4, 0x200uLL);
if ( (int)os_usb_write((unsigned int)cur_dev, 2LL, v11, 512LL) < 0 )
break;
++v12;
a4 += 512;
if ( v12 == v9 )
goto LABEL_13;
}
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "write not succesfful (xfer %d)\n", v12);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_13:
if ( v10 )
{
memcpy(v11, a4, v10);
if ( (int)os_usb_write((unsigned int)cur_dev, 2LL, v11, (unsigned int)v10) < 0 )
{
if ( do_debug )
debug("usb_write_ram", "write not succesfful (excess xfer)\n");
return 0xFFFFFFFFLL;
}
}
free(v11);
result = 0LL;
}
}
else
{
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "usb_write_ram: couldnt allocate scratchpad memory\n");
result = 0xFFFFFFFFLL;
}
return result;
}
if ( a1 == 0x2000 )
{
v9 = a3 >> 9;
v10 = a3 & 0x1FF;
goto LABEL_6;
}
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_write_ram", "usb_write_ram: invalid bank (0x%x)\n", a1);
return 0xFFFFFFFFLL;
}

original_code:
int
usb_write_ram (unsigned int bank, unsigned int start_addr, unsigned int len,
        char *data)
{
  char *outbuf;
  int i;
  char *ptr;
  int num_xfers;
  int xfer_size;
  int line_size;
  int excess_xfer;
  switch (bank)
    {
    case 0x1000:
      line_size = 8;
      xfer_size = 512;
      usb_write_reg (0x0012, start_addr);
      break;
    case 0x2000:
      line_size = 1;
      xfer_size = 512;
      break;
    default:
      if(do_debug) _debug(__FUNCTION__, "usb_write_ram: invalid bank (0x%x)\n", bank);
      return -1;
    }
  num_xfers = len / xfer_size;
  excess_xfer = len - num_xfers * xfer_size;
  if (len % line_size != 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_write_ram: length is not multiple of line size\n");
      return -1;
    }
  outbuf = (char *) malloc (xfer_size);
  if (!outbuf)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_write_ram: couldnt allocate scratchpad memory\n");
      return -1;
    }
  ptr = data;
  setup_xfer (bank, xfer_size);
  for (i = 0; i < num_xfers; i++)
    {
      memcpy (outbuf, ptr, xfer_size);
      if (os_usb_write (cur_dev, 0x02, outbuf, xfer_size) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "write not succesfful (xfer %d)\n", i);
   return -1;
 }
      ptr += xfer_size;
    }
  if (excess_xfer != 0)
    {
      memcpy (outbuf, ptr, excess_xfer);
      if (os_usb_write (cur_dev, 0x02, outbuf, excess_xfer) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "write not succesfful (excess xfer)\n");
   return -1;
 }
    }
  free (outbuf);
  return 0;
}

canonical_original_code:
int usb_write_ram(unsigned int bank, unsigned int start_addr, unsigned int len, char *data)
{
  char *outbuf;
  int i;
  char *ptr;
  int num_xfers;
  int xfer_size;
  int line_size;
  int excess_xfer;
  switch (bank)
    {
    case 0x1000:
      line_size = 8;
      xfer_size = 512;
      usb_write_reg (0x0012, start_addr);
      break;
    case 0x2000:
      line_size = 1;
      xfer_size = 512;
      break;
    default:
      if(do_debug) _debug(__FUNCTION__, "usb_write_ram: invalid bank (0x%x)\n", bank);
      return -1;
    }
  num_xfers = len / xfer_size;
  excess_xfer = len - num_xfers * xfer_size;
  if (len % line_size != 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_write_ram: length is not multiple of line size\n");
      return -1;
    }
  outbuf = (char *) malloc (xfer_size);
  if (!outbuf)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_write_ram: couldnt allocate scratchpad memory\n");
      return -1;
    }
  ptr = data;
  setup_xfer (bank, xfer_size);
  for (i = 0; i < num_xfers; i++)
    {
      memcpy (outbuf, ptr, xfer_size);
      if (os_usb_write (cur_dev, 0x02, outbuf, xfer_size) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "write not succesfful (xfer %d)\n", i);
   return -1;
 }
      ptr += xfer_size;
    }
  if (excess_xfer != 0)
    {
      memcpy (outbuf, ptr, excess_xfer);
      if (os_usb_write (cur_dev, 0x02, outbuf, excess_xfer) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "write not succesfful (excess xfer)\n");
   return -1;
 }
    }
  free (outbuf);
  return 0;
}

variable_types:
outbuf
char *

variable_types: i int


repo: imrehg/SpinAPI

decompiled_code:
__int64 __fastcall usb_do_outp(__int16 a1, unsigned __int8 a2)
{
int v2; // ebx

v2 = a2 | (a1 << 8) & 0x700;
if ( (int)usb_write_reg(20, v2) < 0 )
{
if ( do_debug )
debug("usb_do_outp", "pb_outp_usb: error 1\n");
return 0xFFFFFFFFLL;
}
BYTE1(v2) |= 8u;
if ( (int)usb_write_reg(20, v2) >= 0 )
return 0LL;
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_do_outp", "pb_outp_usb: error 2\n");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1, unsigned __int8 a2)
{
int v2; // ebx

v2 = a2 | (a1 << 8) & 0x700;
if ( (int)func5(20, v2) < 0 )
{
if ( do_debug )
debug("usb_do_outp", "pb_outp_usb: error 1\n");
return 0xFFFFFFFFLL;
}
BYTE1(v2) |= 8u;
if ( (int)func5(20, v2) >= 0 )
return 0LL;
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_do_outp", "pb_outp_usb: error 2\n");
return 0xFFFFFFFFLL;
}

original_code:
int
usb_do_outp (unsigned int address, char data)
{
  unsigned int word;
  word = 0x0FF & data;
  word = word | ((0x07 & address) << 8);
  unsigned int load_flag = 0x01 << 11;
  if (usb_write_reg (0x0014, word) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "pb_outp_usb: error 1\n");
      return -1;
    }
  if (usb_write_reg (0x0014, word | load_flag) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "pb_outp_usb: error 2\n");
      return -1;
    }
  return 0;
}

canonical_original_code:
int usb_do_outp(unsigned int address, char data)
{
  unsigned int word;
  word = 0x0FF & data;
  word = word | ((0x07 & address) << 8);
  unsigned int load_flag = 0x01 << 11;
  if (usb_write_reg (0x0014, word) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "pb_outp_usb: error 1\n");
      return -1;
    }
  if (usb_write_reg (0x0014, word | load_flag) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "pb_outp_usb: error 2\n");
      return -1;
    }
  return 0;
}

variable_types:
address
unsigned int

variable_types: data char

variable_types:
word
unsigned int

return_type: int


decompiled_code:
unsigned __int8 __fastcall inb_p(unsigned __int16 a1)
{
unsigned __int8 result; // al

result = __inbyte(a1);
__outbyte(0x80u, result);
return result;
}

canonical_decompiled_code:
unsigned __int8 __fastcall func0(unsigned __int16 a1)
{
unsigned __int8 result; // al

result = __inbyte(a1);
__outbyte(0x80u, result);
return result;
}

original_code:
static __inline unsigned char
inb_p (unsigned short int __port)
{
  unsigned char _v;
  __asm__ __volatile__ ("inb %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  return _v;
}

canonical_original_code:
static __inline unsigned char inb_p(unsigned short __port)
{
  unsigned char _v;
  __asm__ __volatile__ ("inb %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  return _v;
}

return_type: unsigned char


decompiled_code:
unsigned __int32 __fastcall inl_p(unsigned __int16 a1)
{
unsigned __int32 result; // eax

result = __indword(a1);
__outbyte(0x80u, result);
return result;
}

canonical_decompiled_code:
unsigned __int32 __fastcall func1(unsigned __int16 a1)
{
unsigned __int32 result; // eax

result = __indword(a1);
__outbyte(0x80u, result);
return result;
}

original_code:
static __inline unsigned int
inl_p (unsigned short int __port)
{
  unsigned int _v;
  __asm__ __volatile__ ("inl %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  return _v;
}

canonical_original_code:
static __inline unsigned int inl_p(unsigned short __port)
{
  unsigned int _v;
  __asm__ __volatile__ ("inl %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  return _v;
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall outb_p(unsigned int a1, unsigned __int16 a2)
{
__int64 result; // rax

result = a1;
__outbyte(a2, a1);
__outbyte(0x80u, a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned __int16 a2)
{
__int64 result; // rax

result = a1;
__outbyte(a2, a1);
__outbyte(0x80u, a1);
return result;
}

original_code:
static __inline void
outb_p (unsigned char __value, unsigned short int __port)
{
  __asm__ __volatile__ ("outb %b0,%w1\noutb %%al,$0x80": :"a" (__value),
   "Nd" (__port));
}

canonical_original_code:
static __inline void outb_p(unsigned char __value, unsigned short __port)
{
  __asm__ __volatile__ ("outb %b0,%w1\noutb %%al,$0x80": :"a" (__value),
   "Nd" (__port));
}

return_type: void


decompiled_code:
__int64 __fastcall outl_p(unsigned int a1, unsigned __int16 a2)
{
__int64 result; // rax

result = a1;
__outdword(a2, a1);
__outbyte(0x80u, a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned __int16 a2)
{
__int64 result; // rax

result = a1;
__outdword(a2, a1);
__outbyte(0x80u, a1);
return result;
}

original_code:
static __inline void
outl_p (unsigned int __value, unsigned short int __port)
{
  __asm__ __volatile__ ("outl %0,%w1\noutb %%al,$0x80": :"a" (__value),
   "Nd" (__port));
}

canonical_original_code:
static __inline void outl_p(unsigned int __value, unsigned short __port)
{
  __asm__ __volatile__ ("outl %0,%w1\noutb %%al,$0x80": :"a" (__value),
   "Nd" (__port));
}


decompiled_code:
__int64 os_close()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 0LL;
}

original_code:
int
os_close (int card_num)
{
  return 0;
}

canonical_original_code:
int os_close(int card_num)
{
  return 0;
}


decompiled_code:
__int64 __fastcall os_outp(int a1, __int16 a2, const char *a3)
{
unsigned int v3; // er12

if ( num_cards <= a1 || a1 < 0 )
{
v3 = -1;
spinerr = "Card number out of range";
if ( do_debug )
debug("os_outp", "os_outp: %s\n", a3);
}
else
{
v3 = 0;
outb_p((unsigned __int8)a3, LOWORD(base_addr_array[a1]) + a2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, __int16 a2, const char *a3)
{
unsigned int v3; // er12

if ( num_cards <= a1 || a1 < 0 )
{
v3 = -1;
spinerr = "Card number out of range";
if ( do_debug )
debug("os_outp", "os_outp: %s\n", a3);
}
else
{
v3 = 0;
func2((unsigned __int8)a3, LOWORD(base_addr_array[a1]) + a2);
}
return v3;
}

original_code:
int
os_outp (int card_num, unsigned int address, char data)
{
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_outp: %s\n");
      return -1;
    }
  outb_p (data, base_addr_array[card_num] + address);
  return 0;
}

canonical_original_code:
int os_outp(int card_num, unsigned int address, char data)
{
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_outp: %s\n");
      return -1;
    }
  outb_p (data, base_addr_array[card_num] + address);
  return 0;
}


decompiled_code:
unsigned __int8 __fastcall os_inp(int a1, __int16 a2, const char *a3)
{
if ( num_cards > a1 && a1 >= 0 )
return inb_p(LOWORD(base_addr_array[a1]) + a2);
spinerr = "Card number out of range";
if ( do_debug )
debug("os_inp", "os_inp: %s\n", a3);
return -1;
}

canonical_decompiled_code:
unsigned __int8 __fastcall func9(int a1, __int16 a2, const char *a3)
{
if ( num_cards > a1 && a1 >= 0 )
return func0(LOWORD(base_addr_array[a1]) + a2);
spinerr = "Card number out of range";
if ( do_debug )
debug("os_inp", "os_inp: %s\n", a3);
return -1;
}

original_code:
char
os_inp (int card_num, unsigned int address)
{
  char data;
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_inp: %s\n");
      return -1;
    }
  data = inb_p (base_addr_array[card_num] + address);
  return data;
}

canonical_original_code:
char os_inp(int card_num, unsigned int address)
{
  char data;
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_inp: %s\n");
      return -1;
    }
  data = inb_p (base_addr_array[card_num] + address);
  return data;
}

return_type: char


decompiled_code:
__int64 __fastcall os_outw(int a1, __int16 a2, const char *a3)
{
unsigned int v3; // er12

if ( num_cards <= a1 || a1 < 0 )
{
v3 = -1;
spinerr = "Card number out of range";
if ( do_debug )
debug("os_outw", "os_outw: %s\n", a3);
}
else
{
v3 = 0;
outl_p((unsigned int)a3, LOWORD(base_addr_array[a1]) + a2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int16 a2, const char *a3)
{
unsigned int v3; // er12

if ( num_cards <= a1 || a1 < 0 )
{
v3 = -1;
spinerr = "Card number out of range";
if ( do_debug )
debug("os_outw", "os_outw: %s\n", a3);
}
else
{
v3 = 0;
func3((unsigned int)a3, LOWORD(base_addr_array[a1]) + a2);
}
return v3;
}

original_code:
int
os_outw (int card_num, unsigned int addresss, unsigned int data)
{
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_outw: %s\n");
      return -1;
    }
  outl_p (data, base_addr_array[card_num] + addresss);
  return 0;
}

canonical_original_code:
int os_outw(int card_num, unsigned int addresss, unsigned int data)
{
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_outw: %s\n");
      return -1;
    }
  outl_p (data, base_addr_array[card_num] + addresss);
  return 0;
}

variable_types:
addresss
unsigned int

variable_types:
data
unsigned int


decompiled_code:
unsigned __int32 __fastcall os_inw(int a1, __int16 a2, const char *a3)
{
if ( num_cards > a1 && a1 >= 0 )
return inl_p(LOWORD(base_addr_array[a1]) + a2);
spinerr = "Card number out of range";
if ( do_debug )
debug("os_inw", "os_inw: %s\n", a3);
return -1;
}

canonical_decompiled_code:
unsigned __int32 __fastcall func11(int a1, __int16 a2, const char *a3)
{
if ( num_cards > a1 && a1 >= 0 )
return func1(LOWORD(base_addr_array[a1]) + a2);
spinerr = "Card number out of range";
if ( do_debug )
debug("os_inw", "os_inw: %s\n", a3);
return -1;
}

original_code:
unsigned int
os_inw (int card_num, unsigned int address)
{
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_inw: %s\n");
      return -1;
    }
  return inl_p (base_addr_array[card_num] + address);
}

canonical_original_code:
unsigned int os_inw(int card_num, unsigned int address)
{
  if (card_num >= num_cards || card_num < 0)
    {
      spinerr = "Card number out of range";
      if(do_debug) _debug(__FUNCTION__, "os_inw: %s\n");
      return -1;
    }
  return inl_p (base_addr_array[card_num] + address);
}


decompiled_code:
void __fastcall pb_set_debug(int a1)
{
do_debug = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
do_debug = a1;
}

original_code:
void
pb_set_debug (int debug)
{
  do_debug = debug;
}

canonical_original_code:
void pb_set_debug(int debug)
{
  do_debug = debug;
}

variable_types: debug int


decompiled_code:
char *__fastcall my_strcat(void *src, void *a2)
{
size_t v2; // rbp
size_t v3; // rbx
char *v4; // r12

v2 = strlen((const char *)src);
v3 = strlen((const char *)a2);
v4 = (char *)malloc(v2 + v3 + 5);
memcpy(v4, src, v2);
*(_WORD *)&v4[v2] = 8250;
memcpy(&v4[v2 + 2], a2, v3 + 1);
return v4;
}

canonical_decompiled_code:
char *__fastcall func1(void *src, void *a2)
{
size_t v2; // rbp
size_t v3; // rbx
char *v4; // r12

v2 = strlen((const char *)src);
v3 = strlen((const char *)a2);
v4 = (char *)malloc(v2 + v3 + 5);
memcpy(v4, src, v2);
*(_WORD *)&v4[v2] = 8250;
memcpy(&v4[v2 + 2], a2, v3 + 1);
return v4;
}

original_code:
char *
my_strcat (char *a, char *b)
{
  char *string;
  string = malloc (sizeof (char) * (strlen (a) + strlen (b) + 5));
  strcpy (string, a);
  strcat (string, ": ");
  strcat (string, b);
  return string;
}

canonical_original_code:
char *my_strcat(char *a, char *b)
{
  char *string;
  string = malloc (sizeof (char) * (strlen (a) + strlen (b) + 5));
  strcpy (string, a);
  strcat (string, ": ");
  strcat (string, b);
  return string;
}

variable_types:
a
char *

variable_types:
b
char *

variable_types:
string
char *

return_type: char *


decompiled_code:
char *my_sprintf(char *format, ...)
{
char *v1; // r12
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = (char *)malloc(0x400uLL);
vsprintf(v1, format, arg);
return v1;
}

canonical_decompiled_code:
char *func2(char *format, ...)
{
char *v1; // r12
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = (char *)malloc(0x400uLL);
vsprintf(v1, format, arg);
return v1;
}

original_code:
char *
my_sprintf (char *format, ...)
{
  va_list ap;
  char *string;
  string = (char *) malloc (1024 * sizeof (char));
  __builtin_va_start(ap,format);
  vsprintf (string, format, ap);
  __builtin_va_end(ap);
  return string;
}

canonical_original_code:
char *my_sprintf(char *format, ...)
{
  __builtin_va_list ap;
  char *string;
  string = (char *) malloc (1024 * sizeof (char));
  __builtin_va_start(ap,format);
  vsprintf (string, format, ap);
  __builtin_va_end(ap);
  return string;
}

variable_types:
format
char *


decompiled_code:
__int64 __fastcall do_amcc_outp(__int64 a1, char a2, unsigned __int8 a3)
{
unsigned int v3; // er14
int v4; // ebx
__int64 v5; // r12
unsigned int v6; // er12
int v7; // ebx
__int64 v8; // r13

v3 = a3 | 0x1000000;
os_outp(a1, 15LL, 0LL);
v4 = 1000;
v5 = os_inp((unsigned int)a1, 31LL) & 1;
os_outw((unsigned int)a1, 12LL, a2 & 0xF | 0x1000000u);
while ( 1 )
{
if ( (_BYTE)v5 != (os_inp((unsigned int)a1, 31LL) & 1) )
{
v6 = 0;
goto LABEL_4;
}
if ( v4 == 1 )
break;
--v4;
}
v6 = -2;
spinerr = "timeout reached while sending address";
if ( do_debug )
debug("do_amcc_outp", "do_amcc_outp: %s\n", "timeout reached while sending address");
LABEL_4:
v7 = 1000;
os_outp((unsigned int)a1, 15LL, 0LL);
v8 = os_inp((unsigned int)a1, 31LL) & 1;
os_outw((unsigned int)a1, 12LL, v3);
while ( (_BYTE)v8 == (os_inp((unsigned int)a1, 31LL) & 1) )
{
if ( v7 == 1 )
{
v6 = -2;
spinerr = "timeout reached while sending data";
if ( do_debug )
debug("do_amcc_outp", "do_amcc_outp: %s\n", "timeout reached while sending data");
break;
}
--v7;
}
os_outp((unsigned int)a1, 15LL, 0LL);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, char a2, unsigned __int8 a3)
{
unsigned int v3; // er14
int v4; // ebx
__int64 v5; // r12
unsigned int v6; // er12
int v7; // ebx
__int64 v8; // r13

v3 = a3 | 0x1000000;
os_outp(a1, 15LL, 0LL);
v4 = 1000;
v5 = os_inp((unsigned int)a1, 31LL) & 1;
os_outw((unsigned int)a1, 12LL, a2 & 0xF | 0x1000000u);
while ( 1 )
{
if ( (_BYTE)v5 != (os_inp((unsigned int)a1, 31LL) & 1) )
{
v6 = 0;
goto LABEL_4;
}
if ( v4 == 1 )
break;
--v4;
}
v6 = -2;
spinerr = "timeout reached while sending address";
if ( do_debug )
func3("do_amcc_outp", "do_amcc_outp: %s\n", "timeout reached while sending address");
LABEL_4:
v7 = 1000;
os_outp((unsigned int)a1, 15LL, 0LL);
v8 = os_inp((unsigned int)a1, 31LL) & 1;
os_outw((unsigned int)a1, 12LL, v3);
while ( (_BYTE)v8 == (os_inp((unsigned int)a1, 31LL) & 1) )
{
if ( v7 == 1 )
{
v6 = -2;
spinerr = "timeout reached while sending data";
if ( do_debug )
func3("do_amcc_outp", "do_amcc_outp: %s\n", "timeout reached while sending data");
break;
}
--v7;
}
os_outp((unsigned int)a1, 15LL, 0LL);
return v6;
}

original_code:
int
do_amcc_outp (int card_num, unsigned int address, char data)
{
  unsigned int MAX_RECV_TIMEOUT = 1000;
  unsigned int RECV_START, RECV_TOGGLE, RECV_TIMEOUT = 0;
  int XFER_ERROR = 0;
  int RECV_POLLING = 0;
  unsigned int OGMB = 0x0C;
  unsigned int CHK_RECV = 0x1F;
  unsigned int SIG_TRNS = 0x0F;
  unsigned int CLEAR31 = 0x00000001;
  unsigned int CLEAR24 = 0x000000FF;
  unsigned int CLEAR28 = 0x0000000F;
  unsigned int SET_XFER = 0x01000000;
  unsigned int Temp_Address = address;
  unsigned int Temp_Data = data;
  Temp_Address &= CLEAR28;
  Temp_Address |= SET_XFER;
  Temp_Data &= CLEAR24;
  Temp_Data |= SET_XFER;
  os_outp (card_num, SIG_TRNS, 0);
  RECV_START = os_inp (card_num, CHK_RECV);
  RECV_START &= CLEAR31;
  os_outw (card_num, OGMB, Temp_Address);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= CLEAR31;
      if (RECV_TOGGLE != RECV_START)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -2;
   spinerr = "timeout reached while sending address";
   if(do_debug) _debug(__FUNCTION__, "do_amcc_outp: %s\n", spinerr);
 }
    }
  os_outp (card_num, SIG_TRNS, 0);
  RECV_START = os_inp (card_num, CHK_RECV);
  RECV_START &= CLEAR31;
  os_outw (card_num, OGMB, Temp_Data);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= CLEAR31;
      if (RECV_TOGGLE != RECV_START)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -2;
   spinerr = "timeout reached while sending data";
   if(do_debug) _debug(__FUNCTION__, "do_amcc_outp: %s\n", spinerr);
 }
    }
  os_outp (card_num, SIG_TRNS, 0);
  return XFER_ERROR;
}

canonical_original_code:
int do_amcc_outp(int card_num, unsigned int address, char data)
{
  unsigned int MAX_RECV_TIMEOUT = 1000;
  unsigned int RECV_START; unsigned int RECV_TOGGLE; unsigned int RECV_TIMEOUT = 0;
  int XFER_ERROR = 0;
  int RECV_POLLING = 0;
  unsigned int OGMB = 0x0C;
  unsigned int CHK_RECV = 0x1F;
  unsigned int SIG_TRNS = 0x0F;
  unsigned int CLEAR31 = 0x00000001;
  unsigned int CLEAR24 = 0x000000FF;
  unsigned int CLEAR28 = 0x0000000F;
  unsigned int SET_XFER = 0x01000000;
  unsigned int Temp_Address = address;
  unsigned int Temp_Data = data;
  Temp_Address &= CLEAR28;
  Temp_Address |= SET_XFER;
  Temp_Data &= CLEAR24;
  Temp_Data |= SET_XFER;
  os_outp (card_num, SIG_TRNS, 0);
  RECV_START = os_inp (card_num, CHK_RECV);
  RECV_START &= CLEAR31;
  os_outw (card_num, OGMB, Temp_Address);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= CLEAR31;
      if (RECV_TOGGLE != RECV_START)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -2;
   spinerr = "timeout reached while sending address";
   if(do_debug) _debug(__FUNCTION__, "do_amcc_outp: %s\n", spinerr);
 }
    }
  os_outp (card_num, SIG_TRNS, 0);
  RECV_START = os_inp (card_num, CHK_RECV);
  RECV_START &= CLEAR31;
  os_outw (card_num, OGMB, Temp_Data);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= CLEAR31;
      if (RECV_TOGGLE != RECV_START)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -2;
   spinerr = "timeout reached while sending data";
   if(do_debug) _debug(__FUNCTION__, "do_amcc_outp: %s\n", spinerr);
 }
    }
  os_outp (card_num, SIG_TRNS, 0);
  return XFER_ERROR;
}

variable_types:
OGMB
unsigned int


original_code:
int
do_amcc_outp_old (int card_num, unsigned int address, int data)
{
  unsigned int OGMB = 0x0C;
  unsigned int ICMB = 0x1C;
  int byte[4];
  int error_counter = 0;
  int expected[5];
  expected[0] = 7;
  expected[1] = 0;
  expected[2] = 1;
  expected[3] = 2;
  expected[4] = 7;
  unsigned char NotReady = -1;
  unsigned char NotFinished = -1;
  int nibble_counter = 0;
  int temp_data;
  byte[0] = 0x30;
  byte[1] = 0x00 | (address & 0x0F);
  byte[2] = 0x10 | (data >> 4 & 0x0F);
  byte[3] = 0x20 | (data & 0x0F);
  if(do_debug) _debug(__FUNCTION__, "Writing %x to addr %x\n", data, address);
  while (NotReady)
    {
      temp_data = os_inp (card_num, ICMB);
      temp_data &= 0x07;
      if (temp_data != expected[nibble_counter])
 {
   error_counter++;
   os_outp (card_num, OGMB, temp_data << 4);
 }
      else
 {
   NotReady = 0;
 }
      if (error_counter > 10)
 {
   return -3;
 }
    }
  NotReady = -1;
  error_counter = 0;
  while (NotFinished)
    {
      os_outp (card_num, OGMB, byte[nibble_counter]);
      nibble_counter++;
      while (NotReady)
 {
   temp_data = os_inp (card_num, ICMB);
   temp_data &= 0x07;
   if (temp_data != expected[nibble_counter])
     {
       error_counter++;
     }
   else
     {
       error_counter = 0;
       NotReady = 0;
     }
   if (error_counter > 100)
     {
       return -6;
     }
 }
      NotReady = -1;
      if (nibble_counter == 4)
 {
   NotFinished = 0;
 }
    }
  return 0;
}

canonical_original_code:
int do_amcc_outp_old(int card_num, unsigned int address, int data)
{
  unsigned int OGMB = 0x0C;
  unsigned int ICMB = 0x1C;
  int byte[4];
  int error_counter = 0;
  int expected[5];
  expected[0] = 7;
  expected[1] = 0;
  expected[2] = 1;
  expected[3] = 2;
  expected[4] = 7;
  unsigned char NotReady = -1;
  unsigned char NotFinished = -1;
  int nibble_counter = 0;
  int temp_data;
  byte[0] = 0x30;
  byte[1] = 0x00 | (address & 0x0F);
  byte[2] = 0x10 | (data >> 4 & 0x0F);
  byte[3] = 0x20 | (data & 0x0F);
  if(do_debug) _debug(__FUNCTION__, "Writing %x to addr %x\n", data, address);
  while (NotReady)
    {
      temp_data = os_inp (card_num, ICMB);
      temp_data &= 0x07;
      if (temp_data != expected[nibble_counter])
 {
   error_counter++;
   os_outp (card_num, OGMB, temp_data << 4);
 }
      else
 {
   NotReady = 0;
 }
      if (error_counter > 10)
 {
   return -3;
 }
    }
  NotReady = -1;
  error_counter = 0;
  while (NotFinished)
    {
      os_outp (card_num, OGMB, byte[nibble_counter]);
      nibble_counter++;
      while (NotReady)
 {
   temp_data = os_inp (card_num, ICMB);
   temp_data &= 0x07;
   if (temp_data != expected[nibble_counter])
     {
       error_counter++;
     }
   else
     {
       error_counter = 0;
       NotReady = 0;
     }
   if (error_counter > 100)
     {
       return -6;
     }
 }
      NotReady = -1;
      if (nibble_counter == 4)
 {
   NotFinished = 0;
 }
    }
  return 0;
}

variable_types: data int

variable_types:
ICMB
unsigned int

variable_types:
byte
int [4]

variable_types:
expected
int [5]

variable_types:
NotReady
unsigned char

variable_types:
NotFinished
unsigned char


decompiled_code:
__int64 __fastcall do_amcc_inp(__int64 a1, unsigned __int8 a2)
{
int v2; // ebx
unsigned int v3; // er12
char v4; // al
__int64 v5; // rdx
int v6; // ebx

v2 = 10000;
do_amcc_outp(a1, 8, a2);
do_amcc_outp((unsigned int)a1, 9, 0);
while ( (os_inp((unsigned int)a1, 31LL) & 2) == 0 )
{
if ( v2 == 1 )
{
spinerr = "timeout reached while sending address";
if ( do_debug )
debug("do_amcc_inp", "%s\n", "timeout reached while sending address");
return (unsigned int)-2;
}
--v2;
}
v3 = os_inp((unsigned int)a1, 28LL);
v4 = os_inp((unsigned int)a1, 15LL);
if ( (v4 & 2) != 0 )
v5 = (unsigned int)(char)(v4 & 0xFD);
else
v5 = (unsigned int)(v4 | 2);
v6 = 10000;
os_outp((unsigned int)a1, 15LL, v5);
while ( (os_inp((unsigned int)a1, 31LL) & 2) != 0 )
{
if ( v6 == 1 )
{
spinerr = "timeout reached while getting data";
if ( do_debug )
debug("do_amcc_inp", "%s\n", "timeout reached while getting data");
return 4294967293LL;
}
--v6;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int8 a2)
{
int v2; // ebx
unsigned int v3; // er12
char v4; // al
__int64 v5; // rdx
int v6; // ebx

v2 = 10000;
func4(a1, 8, a2);
func4((unsigned int)a1, 9, 0);
while ( (os_inp((unsigned int)a1, 31LL) & 2) == 0 )
{
if ( v2 == 1 )
{
spinerr = "timeout reached while sending address";
if ( do_debug )
func3("do_amcc_inp", "%s\n", "timeout reached while sending address");
return (unsigned int)-2;
}
--v2;
}
v3 = os_inp((unsigned int)a1, 28LL);
v4 = os_inp((unsigned int)a1, 15LL);
if ( (v4 & 2) != 0 )
v5 = (unsigned int)(char)(v4 & 0xFD);
else
v5 = (unsigned int)(v4 | 2);
v6 = 10000;
os_outp((unsigned int)a1, 15LL, v5);
while ( (os_inp((unsigned int)a1, 31LL) & 2) != 0 )
{
if ( v6 == 1 )
{
spinerr = "timeout reached while getting data";
if ( do_debug )
func3("do_amcc_inp", "%s\n", "timeout reached while getting data");
return 4294967293LL;
}
--v6;
}
return v3;
}

original_code:
char
do_amcc_inp (int card_num, unsigned int address)
{
  unsigned int MAX_RECV_TIMEOUT = 10000;
  unsigned int RECV_START, RECV_STOP, RECV_TOGGLE, RECV_TIMEOUT = 0;
  int XFER_ERROR = 0;
  int RECV_POLLING = 0;
  unsigned int CHK_RECV = 0x1F;
  unsigned int SIG_TRNS = 0x0F;
  unsigned int ICMB = 0x1C;
  unsigned int CLEAR24 = 0x000000FF;
  unsigned int BIT1 = 0x00000002;
  unsigned int INV1 = 0x000000FD;
  unsigned short READ_ADDR = 0x09;
  int Toggle = 0;
  int Toggle_Temp = 0;
  unsigned int Temp_Data = 0;
  do_amcc_outp (card_num, 8, address);
  do_amcc_outp (card_num, READ_ADDR, 0);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  RECV_START = 0x02;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= BIT1;
      if (RECV_TOGGLE == RECV_START)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -2;
   spinerr = "timeout reached while sending address";
   if(do_debug) _debug(__FUNCTION__, "%s\n", spinerr);
 }
    }
  if (XFER_ERROR != 0)
    {
      return XFER_ERROR;
    }
  Temp_Data = os_inp (card_num, ICMB);
  Temp_Data &= CLEAR24;
  Toggle = os_inp (card_num, SIG_TRNS);
  Toggle_Temp = Toggle & BIT1;
  if (Toggle_Temp == 0x0)
    {
      Toggle |= BIT1;
    }
  else
    {
      Toggle &= INV1;
    }
  os_outp (card_num, SIG_TRNS, Toggle);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  RECV_STOP = 0x0;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= BIT1;
      if (RECV_TOGGLE == RECV_STOP)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -3;
   spinerr = "timeout reached while getting data";
   if(do_debug) _debug(__FUNCTION__, "%s\n", spinerr);
 }
    }
  if (XFER_ERROR != 0)
    {
      return XFER_ERROR;
    }
  if (XFER_ERROR == 0)
    {
      return Temp_Data;
    }
  else
    {
      return XFER_ERROR;
    }
}

canonical_original_code:
char do_amcc_inp(int card_num, unsigned int address)
{
  unsigned int MAX_RECV_TIMEOUT = 10000;
  unsigned int RECV_START; unsigned int RECV_STOP; unsigned int RECV_TOGGLE; unsigned int RECV_TIMEOUT = 0;
  int XFER_ERROR = 0;
  int RECV_POLLING = 0;
  unsigned int CHK_RECV = 0x1F;
  unsigned int SIG_TRNS = 0x0F;
  unsigned int ICMB = 0x1C;
  unsigned int CLEAR24 = 0x000000FF;
  unsigned int BIT1 = 0x00000002;
  unsigned int INV1 = 0x000000FD;
  unsigned short READ_ADDR = 0x09;
  int Toggle = 0;
  int Toggle_Temp = 0;
  unsigned int Temp_Data = 0;
  do_amcc_outp (card_num, 8, address);
  do_amcc_outp (card_num, READ_ADDR, 0);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  RECV_START = 0x02;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= BIT1;
      if (RECV_TOGGLE == RECV_START)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -2;
   spinerr = "timeout reached while sending address";
   if(do_debug) _debug(__FUNCTION__, "%s\n", spinerr);
 }
    }
  if (XFER_ERROR != 0)
    {
      return XFER_ERROR;
    }
  Temp_Data = os_inp (card_num, ICMB);
  Temp_Data &= CLEAR24;
  Toggle = os_inp (card_num, SIG_TRNS);
  Toggle_Temp = Toggle & BIT1;
  if (Toggle_Temp == 0x0)
    {
      Toggle |= BIT1;
    }
  else
    {
      Toggle &= INV1;
    }
  os_outp (card_num, SIG_TRNS, Toggle);
  RECV_POLLING = 1;
  RECV_TIMEOUT = 0;
  RECV_STOP = 0x0;
  while ((RECV_POLLING == 1) && (RECV_TIMEOUT < MAX_RECV_TIMEOUT))
    {
      RECV_TOGGLE = os_inp (card_num, CHK_RECV);
      RECV_TOGGLE &= BIT1;
      if (RECV_TOGGLE == RECV_STOP)
 RECV_POLLING = 0;
      else
 RECV_TIMEOUT++;
      if (RECV_TIMEOUT == MAX_RECV_TIMEOUT)
 {
   XFER_ERROR = -3;
   spinerr = "timeout reached while getting data";
   if(do_debug) _debug(__FUNCTION__, "%s\n", spinerr);
 }
    }
  if (XFER_ERROR != 0)
    {
      return XFER_ERROR;
    }
  if (XFER_ERROR == 0)
    {
      return Temp_Data;
    }
  else
    {
      return XFER_ERROR;
    }
}

variable_types:
address
unsigned int

variable_types: Toggle int


decompiled_code:
__int64 os_usb_count_devices()
{
__int64 v0; // rax
unsigned int v1; // er8
_QWORD *v2; // rcx
__int64 *i; // rax
bool v4; // zf

if ( do_debug )
debug("os_usb_count_devices", "os_usb_count_devices called\n");
usb_init();
usb_find_busses();
usb_find_devices();
v0 = usb_get_busses();
v1 = 0;
v2 = (_QWORD *)v0;
if ( v0 )
{
do
{
for ( i = (__int64 *)v2[515]; i; v1 += v4 )
{
v4 = *((_WORD *)i + 2068) == 1027;
i = (__int64 *)*i;
}
v2 = (_QWORD *)*v2;
}
while ( v2 );
}
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rax
unsigned int v1; // er8
_QWORD *v2; // rcx
__int64 *i; // rax
bool v4; // zf

if ( do_debug )
debug("os_usb_count_devices", "os_usb_count_devices called\n");
usb_init();
usb_find_busses();
usb_find_devices();
v0 = usb_get_busses();
v1 = 0;
v2 = (_QWORD *)v0;
if ( v0 )
{
do
{
for ( i = (__int64 *)v2[515]; i; v1 += v4 )
{
v4 = *((_WORD *)i + 2068) == 1027;
i = (__int64 *)*i;
}
v2 = (_QWORD *)*v2;
}
while ( v2 );
}
return v1;
}

original_code:
int os_usb_count_devices(int vendor_id)
{
    int d = 0;
    struct usb_bus* bus;
    struct usb_bus* busses;
    struct usb_device* device;
    if(do_debug) _debug(__FUNCTION__, "os_usb_count_devices called\n");
    usb_init();
    usb_find_busses();
    usb_find_devices();
    busses = usb_get_busses();
    for (bus = busses; bus; bus = bus->next)
    {
        for (device = bus->devices; device; device = device->next)
        {
            if (device->descriptor.idVendor == 0x0403)
                d++;
        }
    }
 return d;
}

canonical_original_code:
int os_usb_count_devices(int vendor_id)
{
    int d = 0;
    struct usb_bus *bus;
    struct usb_bus *busses;
    struct usb_device *device;
    if(do_debug) _debug(__FUNCTION__, "os_usb_count_devices called\n");
    usb_init();
    usb_find_busses();
    usb_find_devices();
    busses = usb_get_busses();
    for (bus = busses; bus; bus = bus->next)
    {
        for (device = bus->devices; device; device = device->next)
        {
            if (device->descriptor.idVendor == 0x0403)
                d++;
        }
    }
 return d;
}

variable_types: d int

variable_types: struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *devices; unsigned int location; struct usb_device *root_dev; } *

variable_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus { struct usb_bus *next; char dirname[-1]; struct usb_device *devices; unsigned int location; struct usb_device *root_dev; } *bus; struct usb_device_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } descriptor; struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface { struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; unsigned char bRefresh; unsigned char bSynchAddress; unsigned char *extra; int extralen; } *endpoint; unsigned char *extra; int extralen; } *altsetting; int num_altsetting; } *interface; unsigned char *extra; int extralen; } *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; } *

user_defined_types: struct usb_device { struct usb_device *next; char filename[-1]; struct usb_bus *bus; struct usb_device_descriptor descriptor; struct usb_config_descriptor *config; void *dev; unsigned char devnum; unsigned char num_children; struct usb_device **children; }

user_defined_types: struct usb_config_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned short wTotalLength; unsigned char bNumInterfaces; unsigned char bConfigurationValue; unsigned char iConfiguration; unsigned char bmAttributes; unsigned char MaxPower; struct usb_interface *interface; unsigned char *extra; int extralen; }

user_defined_types: struct usb_interface { struct usb_interface_descriptor *altsetting; int num_altsetting; }

user_defined_types: struct usb_interface_descriptor { unsigned char bLength; unsigned char bDescriptorType; unsigned char bInterfaceNumber; unsigned char bAlternateSetting; unsigned char bNumEndpoints; unsigned char bInterfaceClass; unsigned char bInterfaceSubClass; unsigned char bInterfaceProtocol; unsigned char iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; int extralen; }


decompiled_code:
__int64 os_usb_close()
{
__int64 v0; // rbx
int v1; // eax
__int64 v2; // r12
__int64 v3; // rdi

if ( do_debug )
debug("os_usb_close", "os_usb_close called\n");
v0 = 0LL;
v1 = os_usb_count_devices(1027LL);
v2 = v1;
if ( v1 <= 0 )
return 0LL;
while ( 1 )
{
v3 = *(_QWORD *)(handles + 8 * v0);
if ( v3 )
{
if ( do_debug )
{
debug("os_usb_close", "os_usb_close: closing device %d\n", (unsigned int)v0);
if ( (unsigned int)usb_release_interface(*(_QWORD *)(handles + 8 * v0), 0LL) )
return 4294967294LL;
}
else if ( (unsigned int)usb_release_interface(v3, 0LL) )
{
return 4294967294LL;
}
if ( (int)usb_close(*(_QWORD *)(handles + 8 * v0)) < 0 )
break;
}
if ( v2 == ++v0 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rbx
int v1; // eax
__int64 v2; // r12
__int64 v3; // rdi

if ( do_debug )
debug("os_usb_close", "os_usb_close called\n");
v0 = 0LL;
v1 = func0(1027LL);
v2 = v1;
if ( v1 <= 0 )
return 0LL;
while ( 1 )
{
v3 = *(_QWORD *)(handles + 8 * v0);
if ( v3 )
{
if ( do_debug )
{
debug("os_usb_close", "os_usb_close: closing device %d\n", (unsigned int)v0);
if ( (unsigned int)usb_release_interface(*(_QWORD *)(handles + 8 * v0), 0LL) )
return 4294967294LL;
}
else if ( (unsigned int)usb_release_interface(v3, 0LL) )
{
return 4294967294LL;
}
if ( (int)usb_close(*(_QWORD *)(handles + 8 * v0)) < 0 )
break;
}
if ( v2 == ++v0 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int os_usb_close()
{
    if(do_debug) _debug(__FUNCTION__, "os_usb_close called\n");
    int number_of_devices = os_usb_count_devices(0x0403);
    int i;
    for (i = 0; i < number_of_devices; i++)
        if (handles[i]) {
            if(do_debug) _debug(__FUNCTION__, "os_usb_close: closing device %d\n", i);
            if (usb_release_interface(handles[i], 0))
             return -2;
            if (usb_close(handles[i]) < 0)
                return -1;
        }
    return 0;
}

variable_types: i int


decompiled_code:
__int64 __fastcall os_usb_reset_pipes(int a1)
{
__int64 result; // rax

if ( do_debug )
debug("os_usb_reset_pipes", "os_usb_reset_pipes called\n");
if ( (int)usb_clear_halt(*(_QWORD *)(handles + 8LL * a1), 1LL) < 0
|| (int)usb_clear_halt(*(_QWORD *)(handles + 8LL * a1), 2LL) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)((int)usb_clear_halt(*(_QWORD *)(handles + 8LL * a1), 134LL) >> 31);
}
return result;
}

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

if ( do_debug )
debug("os_usb_reset_pipes", "os_usb_reset_pipes called\n");
if ( (int)usb_clear_halt(*(_QWORD *)(handles + 8LL * a1), 1LL) < 0
|| (int)usb_clear_halt(*(_QWORD *)(handles + 8LL * a1), 2LL) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)((int)usb_clear_halt(*(_QWORD *)(handles + 8LL * a1), 134LL) >> 31);
}
return result;
}

original_code:
int os_usb_reset_pipes(int dev_num)
{
    if(do_debug) _debug(__FUNCTION__, "os_usb_reset_pipes called\n");
    if (usb_clear_halt(handles[dev_num], 0x01) < 0)
        return -1;
    if (usb_clear_halt(handles[dev_num], 0x02) < 0)
        return -1;
    if (usb_clear_halt(handles[dev_num], 0x86) < 0)
        return -1;
    return 0;
}


decompiled_code:
__int64 __fastcall os_usb_write(unsigned int a1, unsigned int a2, __int64 a3, unsigned int a4)
{
int v6; // eax
__int64 v7; // rdx
__int64 result; // rax

if ( do_debug )
debug("os_usb_write", "os_usb_write(dev_num = %d, pipe = 0x%X, data, size = %d)\n", a1, a2, a4);
v6 = usb_bulk_write(*(_QWORD *)(handles + 8LL * (int)a1), a2, a3, a4, 500LL);
v7 = (unsigned int)v6;
if ( v6 < 0 )
{
spinerr = "write error.";
result = 0xFFFFFFFFLL;
}
else
{
result = do_debug;
if ( do_debug )
{
debug("os_usb_write", "number of bytes written: %d\n", v7);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, unsigned int a2, __int64 a3, unsigned int a4)
{
int v6; // eax
__int64 v7; // rdx
__int64 result; // rax

if ( do_debug )
debug("os_usb_write", "os_usb_write(dev_num = %d, pipe = 0x%X, data, size = %d)\n", a1, a2, a4);
v6 = usb_bulk_write(*(_QWORD *)(handles + 8LL * (int)a1), a2, a3, a4, 500LL);
v7 = (unsigned int)v6;
if ( v6 < 0 )
{
spinerr = "write error.";
result = 0xFFFFFFFFLL;
}
else
{
result = do_debug;
if ( do_debug )
{
debug("os_usb_write", "number of bytes written: %d\n", v7);
result = 0LL;
}
}
return result;
}

original_code:
int os_usb_write(int dev_num, int pipe, void *data, int size)
{
    if(do_debug) _debug(__FUNCTION__, "os_usb_write(dev_num = %d, pipe = 0x%X, data, size = %d)\n", dev_num, pipe, size);
    int bytes_written = usb_bulk_write(handles[dev_num], pipe, data, size, 500);
    if (bytes_written < 0)
    {
        spinerr = "write error.";
        return -1;
    }
    if(do_debug) _debug(__FUNCTION__, "number of bytes written: %d\n", bytes_written);
    return 0;
}

variable_types: pipe int

variable_types:
data
void *

variable_types: size int


decompiled_code:
__int64 __fastcall os_usb_read(unsigned int a1, unsigned int a2, __int64 a3, unsigned int a4)
{
int v6; // eax
__int64 v7; // rdx
__int64 result; // rax

if ( do_debug )
debug("os_usb_read", "os_usb_read(dev_num = %d, pipe = 0x%X, data, size = %d)\n", a1, a2, a4);
v6 = usb_bulk_read(*(_QWORD *)(handles + 8LL * (int)a1), a2, a3, a4, 500LL);
v7 = (unsigned int)v6;
if ( v6 < 0 )
{
spinerr = "Read error.";
result = 0xFFFFFFFFLL;
}
else
{
result = do_debug;
if ( do_debug )
{
debug("os_usb_read", "number of bytes read: %d\n", v7);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, unsigned int a2, __int64 a3, unsigned int a4)
{
int v6; // eax
__int64 v7; // rdx
__int64 result; // rax

if ( do_debug )
debug("os_usb_read", "os_usb_read(dev_num = %d, pipe = 0x%X, data, size = %d)\n", a1, a2, a4);
v6 = usb_bulk_read(*(_QWORD *)(handles + 8LL * (int)a1), a2, a3, a4, 500LL);
v7 = (unsigned int)v6;
if ( v6 < 0 )
{
spinerr = "Read error.";
result = 0xFFFFFFFFLL;
}
else
{
result = do_debug;
if ( do_debug )
{
debug("os_usb_read", "number of bytes read: %d\n", v7);
result = 0LL;
}
}
return result;
}

original_code:
int os_usb_read(int dev_num, int pipe, void *data, int size)
{
    if(do_debug) _debug(__FUNCTION__, "os_usb_read(dev_num = %d, pipe = 0x%X, data, size = %d)\n", dev_num, pipe, size);
    int bytes_read = usb_bulk_read(handles[dev_num], pipe, data, size, 500);
    if (bytes_read < 0)
    {
        spinerr = "Read error.";
        return -1;
    }
    if(do_debug) _debug(__FUNCTION__, "number of bytes read: %d\n", bytes_read);
    return 0;
}


main

canonical_name: main

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

v3 = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d");
if ( v4 == -1 )
break;
v3 = 1;
if ( v4 != 100 )
goto LABEL_8;
}
if ( _bss_start + 1 >= argc )
{
LABEL_8:
usage();
return 1;
}
v5 = open(argv[_bss_start], 0);
v6 = creat(argv[_bss_start + 1], 0x1A4u);
if ( v3 )
decompress(v5, v6);
else
compress(v5, v6);
close(v5);
close(v6);
return 0;
}

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

v3 = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d");
if ( v4 == -1 )
break;
v3 = 1;
if ( v4 != 100 )
goto LABEL_8;
}
if ( _bss_start + 1 >= argc )
{
LABEL_8:
func23();
return 1;
}
v5 = open(argv[_bss_start], 0);
v6 = creat(argv[_bss_start + 1], 0x1A4u);
if ( v3 )
func22(v5, v6);
else
func21(v5, v6);
close(v5);
close(v6);
return 0;
}

original_code:
int main(int argc, char **argv)
{
  int mode_decompress = 0;
  int in, out;
  int opt;
  while ((opt = getopt(argc, argv, "d")) != -1) {
    switch (opt) {
    case 'd':
      mode_decompress = 1;
      break;
    default:
      usage();
      return 1;
    }
  }
  if (argc < optind+2) {
    usage();
    return 1;
  }
  in = open(argv[optind], 00);
  out = creat(argv[optind+1], 0644);
  if (mode_decompress) decompress(in, out);
  else compress(in, out);
  close(in);
  close(out);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int mode_decompress = 0;
  int in; int out;
  int opt;
  while ((opt = getopt(argc, argv, "d")) != -1) {
    switch (opt) {
    case 'd':
      mode_decompress = 1;
      break;
    default:
      usage();
      return 1;
    }
  }
  if (argc < optind+2) {
    usage();
    return 1;
  }
  in = open(argv[optind], 00);
  out = creat(argv[optind+1], 0644);
  if (mode_decompress) decompress(in, out);
  else compress(in, out);
  close(in);
  close(out);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: in int

variable_types: out int

variable_types: opt int


decompiled_code:
char *__fastcall window_init(__int64 a1, int a2)
{
char *result; // rax

result = (char *)malloc(3 * a2);
*(_DWORD *)(a1 + 24) = a2;
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 8) = &result[3 * a2];
*(_QWORD *)(a1 + 16) = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func8(__int64 a1, int a2)
{
char *result; // rax

result = (char *)malloc(3 * a2);
*(_DWORD *)(a1 + 24) = a2;
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 8) = &result[3 * a2];
*(_QWORD *)(a1 + 16) = result;
return result;
}

original_code:
void window_init(window_t *w, int block_size)
{
  w->start = malloc(3*block_size);
  w->end = w->start + 3*block_size;
  w->cursor = w->start;
  w->block_size = block_size;
}

canonical_original_code:
void window_init(struct window *w, int block_size)
{
  w->start = malloc(3*block_size);
  w->end = w->start + 3*block_size;
  w->cursor = w->start;
  w->block_size = block_size;
}

variable_types: struct window { char *start; char *end; char *cursor; int block_size; } *


decompiled_code:
void __fastcall window_free(void **a1)
{
free(*a1);
}

canonical_decompiled_code:
void __fastcall func9(void **a1)
{
free(*a1);
}

original_code:
void window_free(window_t *w)
{
  free(w->start);
}

canonical_original_code:
void window_free(struct window *w)
{
  free(w->start);
}

user_defined_types: struct window { char *start; char *end; char *cursor; int block_size; }


decompiled_code:
__int64 __fastcall window_match(_QWORD *a1, _BYTE *a2, int a3)
{
unsigned __int64 v4; // rdi
_BYTE *v5; // r15
unsigned int v7; // er12
unsigned int v8; // er13
__int64 v9; // rbx
unsigned int v10; // eax
unsigned int v11; // eax
__int64 v12; // rbx
_BYTE *v13; // rsi
_BYTE *v14; // rax
_BYTE *v15; // rbx

v4 = a1[2];
v5 = (_BYTE *)*a1;
if ( *a1 <= v4 - match_distance_max )
v5 = (_BYTE *)(v4 - match_distance_max);
if ( v4 <= (unsigned __int64)v5 )
return 0;
v7 = 0;
v8 = a3 - (_DWORD)a2;
do
{
v9 = 0LL;
v10 = min((int)v4 - (int)v5, v8);
v11 = min(0x3Fu, v10);
if ( v11 )
{
v12 = v11;
v13 = v5;
v14 = a2;
v15 = &v5[v12];
while ( *v13 == *v14 )
{
++v13;
++v14;
if ( v15 == v13 )
goto LABEL_10;
}
v15 = v13;
LABEL_10:
v9 = v15 - v5;
}
if ( (int)match_length((unsigned __int16)v7) < v9 )
v7 = match_pack((unsigned __int16)(a1[2] - (_WORD)v5), (unsigned __int16)v9);
v4 = a1[2];
++v5;
}
while ( v4 > (unsigned __int64)v5 );
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1, _BYTE *a2, int a3)
{
unsigned __int64 v4; // rdi
_BYTE *v5; // r15
unsigned int v7; // er12
unsigned int v8; // er13
__int64 v9; // rbx
unsigned int v10; // eax
unsigned int v11; // eax
__int64 v12; // rbx
_BYTE *v13; // rsi
_BYTE *v14; // rax
_BYTE *v15; // rbx

v4 = a1[2];
v5 = (_BYTE *)*a1;
if ( *a1 <= v4 - match_distance_max )
v5 = (_BYTE *)(v4 - match_distance_max);
if ( v4 <= (unsigned __int64)v5 )
return 0;
v7 = 0;
v8 = a3 - (_DWORD)a2;
do
{
v9 = 0LL;
v10 = func7((int)v4 - (int)v5, v8);
v11 = func7(0x3Fu, v10);
if ( v11 )
{
v12 = v11;
v13 = v5;
v14 = a2;
v15 = &v5[v12];
while ( *v13 == *v14 )
{
++v13;
++v14;
if ( v15 == v13 )
goto LABEL_10;
}
v15 = v13;
LABEL_10:
v9 = v15 - v5;
}
if ( (int)func18((unsigned __int16)v7) < v9 )
v7 = func16((unsigned __int16)(a1[2] - (_WORD)v5), (unsigned __int16)v9);
v4 = a1[2];
++v5;
}
while ( v4 > (unsigned __int64)v5 );
return v7;
}

original_code:
match_t window_match(window_t *w,
       const char *start,
       const char *end)
{
  match_t m = 0;
  const char *a = w->cursor-match_distance_max;
  const char *k = w->start < a? a: w->start;
  while (k < w->cursor) {
    int n = min(63, min(w->cursor-k, end-start));
    const char *p = k;
    const char *q = start;
    while (n && *p == *q) { p++; q++; n--; }
    if (match_length(m) < p-k)
      m = match_pack(w->cursor-k, p-k);
    k++;
  }
  return m;
}

canonical_original_code:
unsigned short window_match(struct window *w, char *start, char *end)
{
  unsigned short m = 0;
  char *a = w->cursor-match_distance_max;
  char *k = w->start < a? a: w->start;
  while (k < w->cursor) {
    int n = min(63, min(w->cursor-k, end-start));
    char *p = k;
    char *q = start;
    while (n && *p == *q) { p++; q++; n--; }
    if (match_length(m) < p-k)
      m = match_pack(w->cursor-k, p-k);
    k++;
  }
  return m;
}

variable_types:
start
char *

variable_types:
end
char *

variable_types:
m
unsigned short

variable_types:
a
char *

variable_types:
k
char *

variable_types: n int

variable_types:
p
char *

variable_types:
q
char *

return_type: unsigned short


decompiled_code:
unsigned __int64 __fastcall window_distance(unsigned __int64 *a1, int a2)
{
unsigned __int64 result; // rax

result = a1[2] - a2;
if ( *a1 > result )
result = 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func11(unsigned __int64 *a1, int a2)
{
unsigned __int64 result; // rax

result = a1[2] - a2;
if ( *a1 > result )
result = 0LL;
return result;
}

original_code:
const char *window_distance(window_t *w, int n)
{
  const char *p = w->cursor-n;
  return p < w->start? 0: p;
}

canonical_original_code:
char *window_distance(struct window *w, int n)
{
  char *p = w->cursor-n;
  return p < w->start? 0: p;
}


decompiled_code:
int __fastcall window_append_match(unsigned __int64 *a1, unsigned __int16 a2)
{
unsigned int v2; // er12
int v3; // eax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rsi
_BYTE *v6; // rdx

v2 = match_length(a2);
v3 = match_distance(a2);
v4 = window_distance(a1, v3);
if ( v4 )
{
v5 = v4 + v2;
if ( v2 )
{
do
{
v6 = (_BYTE *)a1[2];
++v4;
a1[2] = (unsigned __int64)(v6 + 1);
*v6 = *(_BYTE *)(v4 - 1);
}
while ( v5 != v4 );
}
}
else
{
LODWORD(v4) = puts("illegal state");
}
return v4;
}

canonical_decompiled_code:
int __fastcall func12(unsigned __int64 *a1, unsigned __int16 a2)
{
unsigned int v2; // er12
int v3; // eax
unsigned __int64 v4; // rax
unsigned __int64 v5; // rsi
_BYTE *v6; // rdx

v2 = func18(a2);
v3 = func17(a2);
v4 = func11(a1, v3);
if ( v4 )
{
v5 = v4 + v2;
if ( v2 )
{
do
{
v6 = (_BYTE *)a1[2];
++v4;
a1[2] = (unsigned __int64)(v6 + 1);
*v6 = *(_BYTE *)(v4 - 1);
}
while ( v5 != v4 );
}
}
else
{
LODWORD(v4) = puts("illegal state");
}
return v4;
}

original_code:
void window_append_match(window_t *w, match_t m)
{
  int n = match_length(m);
  const char *p = window_distance(w, match_distance(m));
  if (p == 0) printf("illegal state\n");
  else while (n--) *w->cursor++ = *p++;
}

canonical_original_code:
void window_append_match(struct window *w, unsigned short m)
{
  int n = match_length(m);
  char *p = window_distance(w, match_distance(m));
  if (p == 0) printf("illegal state\n");
  else while (n--) *w->cursor++ = *p++;
}


decompiled_code:
_BOOL8 __fastcall window_should_flush(__int64 a1)
{
return *(_QWORD *)(a1 + 8) - *(_QWORD *)(a1 + 16) < *(int *)(a1 + 24);
}

original_code:
int window_should_flush(window_t *w)
{
  return w->end-w->cursor < w->block_size;
}

canonical_original_code:
int window_should_flush(struct window *w)
{
  return w->end-w->cursor < w->block_size;
}


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

result = window_should_flush(a1);
if ( (_DWORD)result )
{
memmove(
*(void **)a1,
(const void *)(*(_QWORD *)a1 + *(int *)(a1 + 24)),
*(_QWORD *)(a1 + 16) - (*(_QWORD *)a1 + *(int *)(a1 + 24)));
result = *(int *)(a1 + 24);
*(_QWORD *)(a1 + 16) -= result;
}
return result;
}

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

result = func13(a1);
if ( (_DWORD)result )
{
memmove(
*(void **)a1,
(const void *)(*(_QWORD *)a1 + *(int *)(a1 + 24)),
*(_QWORD *)(a1 + 16) - (*(_QWORD *)a1 + *(int *)(a1 + 24)));
result = *(int *)(a1 + 24);
*(_QWORD *)(a1 + 16) -= result;
}
return result;
}

original_code:
void window_flush(window_t *w)
{
  if (!window_should_flush(w)) return;
  char *p = w->start+w->block_size;
  memmove(w->start, p, w->cursor-p);
  w->cursor -= w->block_size;
}

canonical_original_code:
void window_flush(struct window *w)
{
  if (!window_should_flush(w)) return;
  char *p = w->start+w->block_size;
  memmove(w->start, p, w->cursor-p);
  w->cursor -= w->block_size;
}


decompiled_code:
__int64 __fastcall match_pack(unsigned __int16 a1)
{
unsigned __int16 v1; // dx
int v2; // eax
int v3; // ecx

min_0(a1, 0x3FFu);
v2 = min_0(v1, 0x3Fu);
return v3 | (unsigned int)(v2 << 10);
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned __int16 a1)
{
unsigned __int16 v1; // dx
int v2; // eax
int v3; // ecx

func15(a1, 0x3FFu);
v2 = func15(v1, 0x3Fu);
return v3 | (unsigned int)(v2 << 10);
}

original_code:
match_t match_pack(uint16_t distance, uint16_t length)
{
  return min(distance, 1023) | (min(length, 63) << 10);
}

canonical_original_code:
unsigned short match_pack(unsigned short distance, unsigned short length)
{
  return min(distance, 1023) | (min(length, 63) << 10);
}

variable_types:
distance
unsigned short

variable_types:
length
unsigned short


decompiled_code:
__int64 __fastcall match_distance(__int16 a1)
{
return a1 & 0x3FF;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int16 a1)
{
return a1 & 0x3FF;
}

original_code:
int match_distance(match_t m)
{
  return m & 1023;
}

canonical_original_code:
int match_distance(unsigned short m)
{
  return m & 1023;
}


decompiled_code:
__int64 __fastcall match_length(unsigned __int16 a1)
{
return a1 >> 10;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned __int16 a1)
{
return a1 >> 10;
}

original_code:
int match_length(match_t m)
{
  return m >> 10;
}

canonical_original_code:
int match_length(unsigned short m)
{
  return m >> 10;
}


decompiled_code:
ssize_t __fastcall write_tab(int fd, void *buf, int a3)
{
__int16 bufa[13]; // [rsp+Eh] [rbp-1Ah] BYREF

bufa[0] = match_pack(0);
write(fd, bufa, 2uLL);
return write(fd, buf, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func19(int fd, void *buf, int a3)
{
__int16 bufa[13]; // [rsp+Eh] [rbp-1Ah] BYREF

bufa[0] = func16(0);
write(fd, bufa, 2uLL);
return write(fd, buf, a3);
}

original_code:
void write_tab(int f, char *tab, int n)
{
  match_t a = match_pack(0, n);
  write(f, &a, sizeof a);
  write(f, tab, n);
}

canonical_original_code:
void write_tab(int f, char *tab, int n)
{
  unsigned short a = match_pack(0, n);
  write(f, &a, sizeof a);
  write(f, tab, n);
}

variable_types: f int

variable_types:
tab
char *

variable_types:
a
unsigned short


decompiled_code:
_BOOL8 __fastcall read_tab(int a1, void *a2, int a3)
{
return a3 == read(a1, a2, a3);
}

canonical_decompiled_code:
_BOOL8 __fastcall func20(int a1, void *a2, int a3)
{
return a3 == read(a1, a2, a3);
}

original_code:
int read_tab(int f, char *tab, int n)
{
  return read(f, tab, n) == n;
}


compress

decompiled_code:
void __fastcall compress(int fd, int a2)
{
int v3; // er12
int v4; // eax
char *v5; // rbp
char *v6; // rbx
__int64 v7; // rax
char v8; // si
unsigned __int16 v9; // [rsp+Eh] [rbp-49Ah] BYREF
void *v10[4]; // [rsp+10h] [rbp-498h] BYREF
char v11[64]; // [rsp+30h] [rbp-478h] BYREF
char buf[1080]; // [rsp+70h] [rbp-438h] BYREF

v3 = 0;
window_init((__int64)v10, 1024);
while ( 1 )
{
v4 = read(fd, buf, 0x400uLL);
if ( v4 <= 0 )
break;
v5 = &buf[v4];
if ( v5 > buf )
{
v6 = buf;
do
{
v9 = window_match(v10, v6, (int)v5);
if ( (int)match_length(v9) > 1 )
{
if ( v3 )
write_tab(a2, v11, v3);
v3 = 0;
write(a2, &v9, 2uLL);
window_append_match((unsigned __int64 *)v10, v9);
v6 += (int)match_length(v9);
}
else
{
if ( match_length_max <= v3 )
{
v3 = 1;
write_tab(a2, v11, match_length_max);
v7 = 0LL;
}
else
{
v7 = v3++;
}
v8 = *v6++;
v11[v7] = v8;
window_append((__int64)v10, v8);
}
window_flush((__int64)v10);
}
while ( v6 < v5 );
}
}
if ( v3 )
write_tab(a2, v11, v3);
window_free(v10);
}

canonical_decompiled_code:
void __fastcall func21(int fd, int a2)
{
int v3; // er12
int v4; // eax
char *v5; // rbp
char *v6; // rbx
__int64 v7; // rax
char v8; // si
unsigned __int16 v9; // [rsp+Eh] [rbp-49Ah] BYREF
void *v10[4]; // [rsp+10h] [rbp-498h] BYREF
char v11[64]; // [rsp+30h] [rbp-478h] BYREF
char buf[1080]; // [rsp+70h] [rbp-438h] BYREF

v3 = 0;
func8((__int64)v10, 1024);
while ( 1 )
{
v4 = read(fd, buf, 0x400uLL);
if ( v4 <= 0 )
break;
v5 = &buf[v4];
if ( v5 > buf )
{
v6 = buf;
do
{
v9 = func10(v10, v6, (int)v5);
if ( (int)func18(v9) > 1 )
{
if ( v3 )
func19(a2, v11, v3);
v3 = 0;
write(a2, &v9, 2uLL);
func12((unsigned __int64 *)v10, v9);
v6 += (int)func18(v9);
}
else
{
if ( match_length_max <= v3 )
{
v3 = 1;
func19(a2, v11, match_length_max);
v7 = 0LL;
}
else
{
v7 = v3++;
}
v8 = *v6++;
v11[v7] = v8;
window_append((__int64)v10, v8);
}
func14((__int64)v10);
}
while ( v6 < v5 );
}
}
if ( v3 )
func19(a2, v11, v3);
func9(v10);
}

original_code:
void compress(int in, int out)
{
  match_t m;
  window_t w;
  char *p;
  char *end;
  char buf[1024];
  int length;
  char tab[64];
  int n = 0;
  window_init(&w, 1024);
  while ((length = read(in, buf, 1024)) > 0) {
    p = buf;
    end = buf+length;
    while (p < end) {
      m = window_match(&w, p, end);
      if (match_length(m) <= 1) {
 if (n >= match_length_max) {
   write_tab(out, tab, match_length_max);
   n = 0;
 }
 tab[n++] = *p;
 window_append(&w, *p);
 p++;
      }
      else {
 if (n) {
   write_tab(out, tab, n);
   n = 0;
 }
 write(out, &m, sizeof m);
 window_append_match(&w, m);
 p += match_length(m);
      }
      window_flush(&w);
    }
  }
  if (n) write_tab(out, tab, n);
  window_free(&w);
}

canonical_original_code:
void compress(int in, int out)
{
  unsigned short m;
  struct window w;
  char *p;
  char *end;
  char buf[1024];
  int length;
  char tab[64];
  int n = 0;
  window_init(&w, 1024);
  while ((length = read(in, buf, 1024)) > 0) {
    p = buf;
    end = buf+length;
    while (p < end) {
      m = window_match(&w, p, end);
      if (match_length(m) <= 1) {
 if (n >= match_length_max) {
   write_tab(out, tab, match_length_max);
   n = 0;
 }
 tab[n++] = *p;
 window_append(&w, *p);
 p++;
      }
      else {
 if (n) {
   write_tab(out, tab, n);
   n = 0;
 }
 write(out, &m, sizeof m);
 window_append_match(&w, m);
 p += match_length(m);
      }
      window_flush(&w);
    }
  }
  if (n) write_tab(out, tab, n);
  window_free(&w);
}

variable_types: in int

variable_types: struct window { char *start; char *end; char *cursor; int block_size; }

variable_types:
buf
char [1024]

variable_types: length int

variable_types:
tab
char [64]


decompress

decompiled_code:
void __fastcall decompress(int fd, int a2)
{
unsigned int v2; // er12
__int16 buf; // [rsp+Eh] [rbp-7Ah] BYREF
void *v4[2]; // [rsp+10h] [rbp-78h] BYREF
__int64 v5; // [rsp+20h] [rbp-68h]
int v6; // [rsp+28h] [rbp-60h]
char v7[88]; // [rsp+30h] [rbp-58h] BYREF

window_init((__int64)v4, 1024);
while ( read(fd, &buf, 2uLL) == 2 )
{
if ( (unsigned int)match_distance(buf) )
{
window_append_match((unsigned __int64 *)v4, buf);
if ( window_should_flush((__int64)v4) )
goto LABEL_8;
}
else
{
v2 = match_length(buf);
if ( !read_tab(fd, v7, v2) )
puts("illegal state");
window_append_tab((__int64)v4, (__int64)v7, v2);
if ( window_should_flush((__int64)v4) )
{
LABEL_8:
write(a2, v4[0], v6);
window_flush((__int64)v4);
}
}
}
if ( (signed __int64)(v5 - (unsigned __int64)v4[0]) > 0 )
write(a2, v4[0], v5 - (unsigned __int64)v4[0]);
window_free(v4);
}

canonical_decompiled_code:
void __fastcall func22(int fd, int a2)
{
unsigned int v2; // er12
__int16 buf; // [rsp+Eh] [rbp-7Ah] BYREF
void *v4[2]; // [rsp+10h] [rbp-78h] BYREF
__int64 v5; // [rsp+20h] [rbp-68h]
int v6; // [rsp+28h] [rbp-60h]
char v7[88]; // [rsp+30h] [rbp-58h] BYREF

func8((__int64)v4, 1024);
while ( read(fd, &buf, 2uLL) == 2 )
{
if ( (unsigned int)func17(buf) )
{
func12((unsigned __int64 *)v4, buf);
if ( func13((__int64)v4) )
goto LABEL_8;
}
else
{
v2 = func18(buf);
if ( !func20(fd, v7, v2) )
puts("illegal state");
window_append_tab((__int64)v4, (__int64)v7, v2);
if ( func13((__int64)v4) )
{
LABEL_8:
write(a2, v4[0], v6);
func14((__int64)v4);
}
}
}
if ( (signed __int64)(v5 - (unsigned __int64)v4[0]) > 0 )
write(a2, v4[0], v5 - (unsigned __int64)v4[0]);
func9(v4);
}

original_code:
void decompress(int in, int out)
{
  match_t m;
  window_t w;
  window_init(&w, 1024);
  while (read(in, &m, sizeof m) == sizeof m) {
    if (match_distance(m) == 0) {
      int n = match_length(m);
      char tab[64];
      if (!read_tab(in, tab, n)) {
 printf("illegal state\n");
      }
      window_append_tab(&w, tab, n);
    }
    else {
      window_append_match(&w, m);
    }
    if (window_should_flush(&w)) {
      write(out, w.start, w.block_size);
      window_flush(&w);
    }
  }
  if (0 < w.cursor-w.start) write(out, w.start, w.cursor-w.start);
  window_free(&w);
}

canonical_original_code:
void decompress(int in, int out)
{
  unsigned short m;
  struct window w;
  window_init(&w, 1024);
  while (read(in, &m, sizeof m) == sizeof m) {
    if (match_distance(m) == 0) {
      int n = match_length(m);
      char tab[64];
      if (!read_tab(in, tab, n)) {
 printf("illegal state\n");
      }
      window_append_tab(&w, tab, n);
    }
    else {
      window_append_match(&w, m);
    }
    if (window_should_flush(&w)) {
      write(out, w.start, w.block_size);
      window_flush(&w);
    }
  }
  if (0 < w.cursor-w.start) write(out, w.start, w.cursor-w.start);
  window_free(&w);
}

variable_types: out int


usage

decompiled_code:
int usage()
{
return puts("usage");
}

canonical_decompiled_code:
int func23()
{
return puts("usage");
}

original_code:
void usage(void)
{
  printf("usage\n");
}

canonical_original_code:
void usage()
{
  printf("usage\n");
}


mcprint

repo: inau/CShell

decompiled_code:
int __fastcall mcprint(const char *a1, const char *a2)
{
return printf("%s%s%s", a2, a1, "\x1B[0m");
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const char *a2)
{
return printf("%s%s%s", a2, a1, "\x1B[0m");
}

original_code:
void mcprint(char* src, char* color)
{
    printf("%s%s%s", color ,src , "\x1B[0m");
}

canonical_original_code:
void mcprint(char *src, char *color)
{
    printf("%s%s%s", color ,src , "\x1B[0m");
}

variable_types:
src
char *

variable_types:
color
char *


printfred

decompiled_code:
int __fastcall printfred(const char *a1)
{
return mcprint(a1, "\x1B[31m");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return func0(a1, "\x1B[31m");
}

original_code:
void printfred(char* input) {
    mcprint(input, "\x1B[31m");
}

canonical_original_code:
void printfred(char *input) {
    mcprint(input, "\x1B[31m");
}

variable_types:
input
char *


printfgreen

decompiled_code:
int __fastcall printfgreen(const char *a1)
{
return mcprint(a1, "\x1B[32m");
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
return func0(a1, "\x1B[32m");
}

original_code:
void printfgreen(char* input) {
    mcprint(input, "\x1B[32m");
}

canonical_original_code:
void printfgreen(char *input) {
    mcprint(input, "\x1B[32m");
}


printfyellow

decompiled_code:
int __fastcall printfyellow(const char *a1)
{
return mcprint(a1, "\x1B[33m");
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
return func0(a1, "\x1B[33m");
}

original_code:
void printfyellow(char* input) {
    mcprint(input, "\x1B[33m");
}

canonical_original_code:
void printfyellow(char *input) {
    mcprint(input, "\x1B[33m");
}


printfblue

decompiled_code:
int __fastcall printfblue(const char *a1)
{
return mcprint(a1, "\x1B[34m");
}

canonical_decompiled_code:
int __fastcall func4(const char *a1)
{
return func0(a1, "\x1B[34m");
}

original_code:
void printfblue(char* input) {
    mcprint(input, "\x1B[34m");
}

canonical_original_code:
void printfblue(char *input) {
    mcprint(input, "\x1B[34m");
}


printfmagenta

decompiled_code:
int __fastcall printfmagenta(const char *a1)
{
return mcprint(a1, "\x1B[35m");
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
return func0(a1, "\x1B[35m");
}

original_code:
void printfmagenta(char* input) {
    mcprint(input, "\x1B[35m");
}

canonical_original_code:
void printfmagenta(char *input) {
    mcprint(input, "\x1B[35m");
}


printfcyan

decompiled_code:
int __fastcall printfcyan(const char *a1)
{
return mcprint(a1, "\x1B[36m");
}

canonical_decompiled_code:
int __fastcall func6(const char *a1)
{
return func0(a1, "\x1B[36m");
}

original_code:
void printfcyan(char* input) {
    mcprint(input, "\x1B[36m");
}

canonical_original_code:
void printfcyan(char *input) {
    mcprint(input, "\x1B[36m");
}


acmd

original_code:
int acmd (char *s, Cmd **cmd)
{
  char *tok;
  int n, cnt = 0;
  Cmd *cmd0 = cmds;
  cmds = cmds->next;
  cmd0->next = ((void *)0);
  cmd0->cmd = pp;
  while (1) {
    n = nexttoken(s, &tok);
    if (n == 0 || (((*tok) == ('|')) || ((*tok) == ('&')) || ((*tok) == ('<')) || ((*tok) == ('>'))))
      {
 *cmd = cmd0;
 *pp++ = ((void *)0);
 return cnt;
      }
    else
      {
 *pp++ = tok;
 cnt += n;
 s += n;
      }
  }
}

canonical_original_code:
int acmd(char *s, struct _cmd **cmd)
{
  char *tok;
  int n; int cnt = 0;
  struct _cmd *cmd0 = cmds;
  cmds = cmds->next;
  cmd0->next = ((void *)0);
  cmd0->cmd = pp;
  while (1) {
    n = nexttoken(s, &tok);
    if (n == 0 || (((*tok) == ('|')) || ((*tok) == ('&')) || ((*tok) == ('<')) || ((*tok) == ('>'))))
      {
 *cmd = cmd0;
 *pp++ = ((void *)0);
 return cnt;
      }
    else
      {
 *pp++ = tok;
 cnt += n;
 s += n;
      }
  }
}

variable_types:
s
char *

variable_types: struct _cmd { char **cmd; struct _cmd *next; } **

variable_types:
tok
char *

variable_types: n int

variable_types: cnt int


isidentifier

decompiled_code:
__int64 __fastcall isidentifier(char *a1)
{
__int64 v2; // rbx
char *v3; // rbp

while ( 1 )
{
v2 = *a1;
if ( !(_BYTE)v2 )
break;
++a1;
v3 = strrchr("_-.,/~+", (char)v2);
if ( ((*_ctype_b_loc())[v2] & 8) == 0 && !v3 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
__int64 v2; // rbx
char *v3; // rbp

while ( 1 )
{
v2 = *a1;
if ( !(_BYTE)v2 )
break;
++a1;
v3 = strrchr("_-.,/~+", (char)v2);
if ( ((*_ctype_b_loc())[v2] & 8) == 0 && !v3 )
return 0LL;
}
return 1LL;
}

original_code:
int isidentifier (char *s)
{
  while (*s)
    {
      char *p = strrchr ("_-.,/~+", *s);
      if (! ((*__ctype_b_loc ())[(int) ((*s++))] & (unsigned short int) _ISalnum) && (p == ((void *)0)))
 return 0;
    }
  return 1;
}

canonical_original_code:
int isidentifier(char *s)
{
  while (*s)
    {
      char *p = strrchr ("_-.,/~+", *s);
      if (! ((*__ctype_b_loc ())[(int) ((*s++))] & (unsigned short) _ISalnum) && (p == ((void *)0)))
 return 0;
    }
  return 1;
}

variable_types:
p
char *


parsecommand

decompiled_code:
__int64 __fastcall parsecommand(char *a1, __int64 a2)
{
const char *v2; // rax
int v4; // eax
char *v5; // rbp
__int64 v6; // rax
int v7; // eax
char v8; // al
int v9; // eax
__int64 v10; // r13
char *v11; // r15
__int64 result; // rax
int v13; // eax
__int64 v14; // [rsp+0h] [rbp-48h] BYREF
__int64 v15[8]; // [rsp+8h] [rbp-40h] BYREF

v2 = (const char *)&unk_730;
do
{
*((_QWORD *)v2 - 1) = v2;
v2 += 16;
}
while ( "_-.,/~+" != v2 );
qword_858 = 0LL;
cmds = (__int64)&cmdbuf;
cp = (__int64)&cbuf;
pp = (__int64)&pbuf;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = 0LL;
*(_DWORD *)(a2 + 32) = 0;
*(_QWORD *)a2 = 0LL;
while ( 2 )
{
v4 = acmd(a1, &v14);
if ( v4 <= 0 )
return 0xFFFFFFFFLL;
v5 = &a1[v4];
v6 = v14;
*(_QWORD *)(v14 + 8) = *(_QWORD *)a2;
*(_QWORD *)a2 = v6;
while ( 1 )
{
v7 = nexttoken(v5, v15);
if ( !v7 )
return 1LL;
a1 = &v5[v7];
v8 = *(_BYTE *)v15[0];
if ( *(_BYTE *)v15[0] == 62 )
{
if ( *(_QWORD *)(a2 + 16) )
{
fwrite("duplicate redirection of stdout\n", 1uLL, 0x20uLL, stderr);
return 0xFFFFFFFFLL;
}
v13 = nexttoken(a1, (_QWORD *)(a2 + 16));
v10 = v13;
if ( v13 < 0 )
return 0xFFFFFFFFLL;
v11 = *(char **)(a2 + 16);
if ( !(unsigned int)isidentifier(v11) )
{
LABEL_20:
fprintf(stderr, "Illegal filename: \"%s\"\n", v11);
return 0xFFFFFFFFLL;
}
goto LABEL_14;
}
if ( v8 > 62 )
break;
if ( v8 == 38 )
{
if ( (unsigned int)nexttoken(a1, v15) )
{
fwrite("illegal bakgrounding\n", 1uLL, 0x15uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
*(_DWORD *)(a2 + 32) = 1;
result = 1LL;
}
return result;
}
if ( v8 != 60 )
return 0xFFFFFFFFLL;
if ( *(_QWORD *)(a2 + 8) )
{
fwrite("duplicate redirection of stdin\n", 1uLL, 0x1FuLL, stderr);
return 0xFFFFFFFFLL;
}
v9 = nexttoken(a1, (_QWORD *)(a2 + 8));
v10 = v9;
if ( v9 < 0 )
return 0xFFFFFFFFLL;
v11 = *(char **)(a2 + 8);
if ( !(unsigned int)isidentifier(v11) )
goto LABEL_20;
LABEL_14:
v5 = &a1[v10];
}
if ( v8 == 124 )
continue;
return 0xFFFFFFFFLL;
}
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, __int64 a2)
{
const char *v2; // rax
int v4; // eax
char *v5; // rbp
__int64 v6; // rax
int v7; // eax
char v8; // al
int v9; // eax
__int64 v10; // r13
char *v11; // r15
__int64 result; // rax
int v13; // eax
__int64 v14; // [rsp+0h] [rbp-48h] BYREF
__int64 v15[8]; // [rsp+8h] [rbp-40h] BYREF

v2 = (const char *)&unk_730;
do
{
*((_QWORD *)v2 - 1) = v2;
v2 += 16;
}
while ( "_-.,/~+" != v2 );
qword_858 = 0LL;
cmds = (__int64)&cmdbuf;
cp = (__int64)&cbuf;
pp = (__int64)&pbuf;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = 0LL;
*(_DWORD *)(a2 + 32) = 0;
*(_QWORD *)a2 = 0LL;
while ( 2 )
{
v4 = func1(a1, &v14);
if ( v4 <= 0 )
return 0xFFFFFFFFLL;
v5 = &a1[v4];
v6 = v14;
*(_QWORD *)(v14 + 8) = *(_QWORD *)a2;
*(_QWORD *)a2 = v6;
while ( 1 )
{
v7 = func0(v5, v15);
if ( !v7 )
return 1LL;
a1 = &v5[v7];
v8 = *(_BYTE *)v15[0];
if ( *(_BYTE *)v15[0] == 62 )
{
if ( *(_QWORD *)(a2 + 16) )
{
fwrite("duplicate redirection of stdout\n", 1uLL, 0x20uLL, stderr);
return 0xFFFFFFFFLL;
}
v13 = func0(a1, (_QWORD *)(a2 + 16));
v10 = v13;
if ( v13 < 0 )
return 0xFFFFFFFFLL;
v11 = *(char **)(a2 + 16);
if ( !(unsigned int)func2(v11) )
{
LABEL_20:
fprintf(stderr, "Illegal filename: \"%s\"\n", v11);
return 0xFFFFFFFFLL;
}
goto LABEL_14;
}
if ( v8 > 62 )
break;
if ( v8 == 38 )
{
if ( (unsigned int)func0(a1, v15) )
{
fwrite("illegal bakgrounding\n", 1uLL, 0x15uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
*(_DWORD *)(a2 + 32) = 1;
result = 1LL;
}
return result;
}
if ( v8 != 60 )
return 0xFFFFFFFFLL;
if ( *(_QWORD *)(a2 + 8) )
{
fwrite("duplicate redirection of stdin\n", 1uLL, 0x1FuLL, stderr);
return 0xFFFFFFFFLL;
}
v9 = func0(a1, (_QWORD *)(a2 + 8));
v10 = v9;
if ( v9 < 0 )
return 0xFFFFFFFFLL;
v11 = *(char **)(a2 + 8);
if ( !(unsigned int)func2(v11) )
goto LABEL_20;
LABEL_14:
v5 = &a1[v10];
}
if ( v8 == 124 )
continue;
return 0xFFFFFFFFLL;
}
}

original_code:
int parsecommand(char *cmdline, Shellcmd *shellcmd)
{
  int i, n;
  Cmd *cmd0;
  char *t = cmdline;
  char *tok;
  for (i = 0; i < 20 -1; i++) cmdbuf[i].next = &cmdbuf[i+1];
  cmdbuf[20 -1].next = ((void *)0);
  cmds = cmdbuf;
  cp = cbuf;
  pp = pbuf;
  shellcmd->rd_stdin = ((void *)0);
  shellcmd->rd_stdout = ((void *)0);
  shellcmd->rd_stderr = ((void *)0);
  shellcmd->background = 0;
  shellcmd->the_cmds = ((void *)0);
  do {
    if ((n = acmd(t, &cmd0)) <= 0)
      return -1;
    t += n;
    cmd0->next = shellcmd->the_cmds;
    shellcmd->the_cmds = cmd0;
    int newtoken = 1;
    while (newtoken) {
      n = nexttoken(t, &tok);
      if (n == 0)
  {
   return 1;
  }
      t += n;
    switch(*tok) {
      case ('|'):
  newtoken = 0;
  break;
   case ('&'):
  n = nexttoken(t, &tok);
  if (n == 0)
  {
   shellcmd->background = 1;
   return 1;
  }
  else
  {
   fprintf(stderr, "illegal bakgrounding\n");
   return -1;
  }
   newtoken = 0;
   break;
      case ('<'):
  if (shellcmd->rd_stdin != ((void *)0))
  {
   fprintf(stderr, "duplicate redirection of stdin\n");
   return -1;
  }
  if ((n = nexttoken(t, &(shellcmd->rd_stdin))) < 0) return -1;
  if (!isidentifier(shellcmd->rd_stdin))
  {
   fprintf(stderr, "Illegal filename: \"%s\"\n", shellcmd->rd_stdin);
   return -1;
  }
  t += n;
  break;
      case ('>'):
  if (shellcmd->rd_stdout != ((void *)0))
  {
   fprintf(stderr, "duplicate redirection of stdout\n");
   return -1;
  }
  if ((n = nexttoken(t, &(shellcmd->rd_stdout))) < 0)
  return -1;
  if (!isidentifier(shellcmd->rd_stdout))
  {
   fprintf(stderr, "Illegal filename: \"%s\"\n", shellcmd->rd_stdout);
   return -1;
  }
  t += n;
  break;
   default:
  return -1;
 }
}
}
while (1);
  return 0;
}

canonical_original_code:
int parsecommand(char *cmdline, struct _shellcmd *shellcmd)
{
  int i; int n;
  struct _cmd *cmd0;
  char *t = cmdline;
  char *tok;
  for (i = 0; i < 20 -1; i++) cmdbuf[i].next = &cmdbuf[i+1];
  cmdbuf[20 -1].next = ((void *)0);
  cmds = cmdbuf;
  cp = cbuf;
  pp = pbuf;
  shellcmd->rd_stdin = ((void *)0);
  shellcmd->rd_stdout = ((void *)0);
  shellcmd->rd_stderr = ((void *)0);
  shellcmd->background = 0;
  shellcmd->the_cmds = ((void *)0);
  do {
    if ((n = acmd(t, &cmd0)) <= 0)
      return -1;
    t += n;
    cmd0->next = shellcmd->the_cmds;
    shellcmd->the_cmds = cmd0;
    int newtoken = 1;
    while (newtoken) {
      n = nexttoken(t, &tok);
      if (n == 0)
  {
   return 1;
  }
      t += n;
    switch(*tok) {
      case ('|'):
  newtoken = 0;
  break;
   case ('&'):
  n = nexttoken(t, &tok);
  if (n == 0)
  {
   shellcmd->background = 1;
   return 1;
  }
  else
  {
   fprintf(stderr, "illegal bakgrounding\n");
   return -1;
  }
   newtoken = 0;
   break;
      case ('<'):
  if (shellcmd->rd_stdin != ((void *)0))
  {
   fprintf(stderr, "duplicate redirection of stdin\n");
   return -1;
  }
  if ((n = nexttoken(t, &(shellcmd->rd_stdin))) < 0) return -1;
  if (!isidentifier(shellcmd->rd_stdin))
  {
   fprintf(stderr, "Illegal filename: \"%s\"\n", shellcmd->rd_stdin);
   return -1;
  }
  t += n;
  break;
      case ('>'):
  if (shellcmd->rd_stdout != ((void *)0))
  {
   fprintf(stderr, "duplicate redirection of stdout\n");
   return -1;
  }
  if ((n = nexttoken(t, &(shellcmd->rd_stdout))) < 0)
  return -1;
  if (!isidentifier(shellcmd->rd_stdout))
  {
   fprintf(stderr, "Illegal filename: \"%s\"\n", shellcmd->rd_stdout);
   return -1;
  }
  t += n;
  break;
   default:
  return -1;
 }
}
}
while (1);
  return 0;
}

variable_types:
cmdline
char *

variable_types: struct _shellcmd { struct _cmd { char **cmd; struct _cmd *next; } *the_cmds; char *rd_stdin; char *rd_stdout; char *rd_stderr; int background; } *

variable_types: i int

variable_types:
t
char *

variable_types: newtoken int

user_defined_types: struct _shellcmd { struct _cmd *the_cmds; char *rd_stdin; char *rd_stdout; char *rd_stderr; int background; }


printshellcmd

decompiled_code:
int __fastcall printshellcmd(__int64 *a1)
{
const char *v1; // rsi
const char *v2; // rsi
const char *v3; // rsi
int result; // eax
__int64 i; // rbp
const char **v6; // rbx
const char *j; // rsi

puts("Shellcommand:");
v1 = (const char *)a1[1];
if ( !v1 )
v1 = "<none>";
printf(" stdin : %s\n", v1);
v2 = (const char *)a1[2];
if ( !v2 )
v2 = "<none>";
printf(" stdout: %s\n", v2);
v3 = "yes";
if ( !*((_DWORD *)a1 + 8) )
v3 = "no";
result = printf(" bg : %s\n", v3);
for ( i = *a1; i; result = puts("]") )
{
v6 = *(const char ***)i;
i = *(_QWORD *)(i + 8);
printf(" [");
for ( j = *v6; *v6; j = *v6 )
{
++v6;
printf("%s ", j);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 *a1)
{
const char *v1; // rsi
const char *v2; // rsi
const char *v3; // rsi
int result; // eax
__int64 i; // rbp
const char **v6; // rbx
const char *j; // rsi

puts("Shellcommand:");
v1 = (const char *)a1[1];
if ( !v1 )
v1 = "<none>";
printf(" stdin : %s\n", v1);
v2 = (const char *)a1[2];
if ( !v2 )
v2 = "<none>";
printf(" stdout: %s\n", v2);
v3 = "yes";
if ( !*((_DWORD *)a1 + 8) )
v3 = "no";
result = printf(" bg : %s\n", v3);
for ( i = *a1; i; result = puts("]") )
{
v6 = *(const char ***)i;
i = *(_QWORD *)(i + 8);
printf(" [");
for ( j = *v6; *v6; j = *v6 )
{
++v6;
printf("%s ", j);
}
}
return result;
}

original_code:
void printshellcmd(Shellcmd *shellcmd)
{
  printf("Shellcommand:\n");
  printf("   stdin : %s\n", shellcmd->rd_stdin ? shellcmd->rd_stdin : "<none>" );
  printf("   stdout: %s\n", shellcmd->rd_stdout ? shellcmd->rd_stdout : "<none>" );
  printf("   bg    : %s\n", shellcmd->background ? "yes" : "no");
  Cmd *cmdlist = shellcmd->the_cmds;
  while (cmdlist != ((void *)0)) {
    char **cmd = cmdlist->cmd;
    cmdlist = cmdlist->next;
    char **printcmd = cmd;
    printf("    [");
    while (*printcmd != ((void *)0)) {
      printf("%s ", *printcmd++);
    }
    printf("]\n");
  }
}

canonical_original_code:
void printshellcmd(struct _shellcmd *shellcmd)
{
  printf("Shellcommand:\n");
  printf("   stdin : %s\n", shellcmd->rd_stdin ? shellcmd->rd_stdin : "<none>" );
  printf("   stdout: %s\n", shellcmd->rd_stdout ? shellcmd->rd_stdout : "<none>" );
  printf("   bg    : %s\n", shellcmd->background ? "yes" : "no");
  struct _cmd *cmdlist = shellcmd->the_cmds;
  while (cmdlist != ((void *)0)) {
    char **cmd = cmdlist->cmd;
    cmdlist = cmdlist->next;
    char **printcmd = cmd;
    printf("    [");
    while (*printcmd != ((void *)0)) {
      printf("%s ", *printcmd++);
    }
    printf("]\n");
  }
}

variable_types: struct _cmd { char **cmd; struct _cmd *next; } *

variable_types:
cmd
char **

variable_types:
printcmd
char **


cmdexist

decompiled_code:
__int64 __fastcall cmdexist(char *src, char **a2)
{
char *v3; // rsi
__int64 result; // rax
char *v5; // rsi
char *v6; // rax
char v7[1024]; // [rsp+0h] [rbp-818h] BYREF
char dest[1048]; // [rsp+400h] [rbp-418h] BYREF

v3 = getenv("PATH");
result = 0LL;
if ( v3 )
{
strcpy(v7, v3);
v5 = strtok(v7, ":");
if ( v5 )
{
while ( 1 )
{
v6 = stpcpy(dest, v5);
*v6 = 47;
strcpy(v6 + 1, src);
if ( access(dest, 0) != -1 )
break;
v5 = strtok(0LL, ":");
if ( !v5 )
goto LABEL_8;
}
*a2 = dest;
result = 1LL;
}
else
{
LABEL_8:
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src, char **a2)
{
char *v3; // rsi
__int64 result; // rax
char *v5; // rsi
char *v6; // rax
char v7[1024]; // [rsp+0h] [rbp-818h] BYREF
char dest[1048]; // [rsp+400h] [rbp-418h] BYREF

v3 = getenv("PATH");
result = 0LL;
if ( v3 )
{
strcpy(v7, v3);
v5 = strtok(v7, ":");
if ( v5 )
{
while ( 1 )
{
v6 = stpcpy(dest, v5);
*v6 = 47;
strcpy(v6 + 1, src);
if ( access(dest, 0) != -1 )
break;
v5 = strtok(0LL, ":");
if ( !v5 )
goto LABEL_8;
}
*a2 = dest;
result = 1LL;
}
else
{
LABEL_8:
result = 0LL;
}
}
return result;
}

original_code:
int cmdexist(char *filename, char** givenprogpath) {
  char *path = getenv("PATH");
  if (path != ((void *)0)) {
    char pathlist[1024];
    strcpy(pathlist, path);
    char *progPath, *s = ":";
    progPath = strtok(pathlist, s);
    while(progPath != ((void *)0)) {
      char prog[1024];
      strcpy(prog, progPath);
      strcat(prog, "/");
      strcat(prog, filename);
      if (access(prog, 0) != -1) {
        *givenprogpath = prog;
    return 1;
   }
      progPath = strtok(((void *)0), s);
    }
  }
  return 0;
}

canonical_original_code:
int cmdexist(char *filename, char **givenprogpath) {
  char *path = getenv("PATH");
  if (path != ((void *)0)) {
    char pathlist[1024];
    strcpy(pathlist, path);
    char *progPath; char *s = ":";
    progPath = strtok(pathlist, s);
    while(progPath != ((void *)0)) {
      char prog[1024];
      strcpy(prog, progPath);
      strcat(prog, "/");
      strcat(prog, filename);
      if (access(prog, 0) != -1) {
        *givenprogpath = prog;
    return 1;
   }
      progPath = strtok(((void *)0), s);
    }
  }
  return 0;
}

variable_types:
filename
char *

variable_types:
givenprogpath
char **

variable_types:
path
char *

variable_types:
pathlist
char [1024]

variable_types:
progPath
char *

variable_types:
prog
char [1024]


printAvailable

repo: inau/Cloaked-computing-machine

decompiled_code:
int printAvailable()
{
int v0; // ebx
__int64 v1; // rbx
__int64 v2; // rsi

puts("Availability vector:");
if ( col > 0 )
{
v0 = 0;
do
printf("R%d ", (unsigned int)++v0);
while ( v0 < col );
}
putchar(10);
if ( col > 0 )
{
v1 = 0LL;
do
{
v2 = *(unsigned int *)(*(_QWORD *)(s + 8) + 4 * v1++);
printf("%d ", v2);
}
while ( col > (int)v1 );
}
return putchar(10);
}

canonical_decompiled_code:
int func0()
{
int v0; // ebx
__int64 v1; // rbx
__int64 v2; // rsi

puts("Availability vector:");
if ( col > 0 )
{
v0 = 0;
do
printf("R%d ", (unsigned int)++v0);
while ( v0 < col );
}
putchar(10);
if ( col > 0 )
{
v1 = 0LL;
do
{
v2 = *(unsigned int *)(*(_QWORD *)(s + 8) + 4 * v1++);
printf("%d ", v2);
}
while ( col > (int)v1 );
}
return putchar(10);
}

original_code:
void printAvailable() {
 int i,j;
 printf("Availability vector:\n");
  for(i = 0; i < col; i++)
    printf("R%d ", i+1);
  printf("\n");
  for(j = 0; j < col; j++)
    printf("%d  ",s->available[j]);
  printf("\n");
}

canonical_original_code:
void printAvailable() {
 int i; int j;
 printf("Availability vector:\n");
  for(i = 0; i < col; i++)
    printf("R%d ", i+1);
  printf("\n");
  for(j = 0; j < col; j++)
    printf("%d  ",s->available[j]);
  printf("\n");
}

variable_types: j int


printRequest

decompiled_code:
int __fastcall printRequest(__int64 a1)
{
int v1; // ebx
__int64 v2; // rbx
__int64 v3; // rsi

puts("Request vector:");
if ( col > 0 )
{
v1 = 0;
do
printf("R%d ", (unsigned int)++v1);
while ( v1 < col );
}
putchar(10);
if ( col > 0 )
{
v2 = 0LL;
do
{
v3 = *(unsigned int *)(a1 + 4 * v2++);
printf("%d ", v3);
}
while ( col > (int)v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
int v1; // ebx
__int64 v2; // rbx
__int64 v3; // rsi

puts("Request vector:");
if ( col > 0 )
{
v1 = 0;
do
printf("R%d ", (unsigned int)++v1);
while ( v1 < col );
}
putchar(10);
if ( col > 0 )
{
v2 = 0LL;
do
{
v3 = *(unsigned int *)(a1 + 4 * v2++);
printf("%d ", v3);
}
while ( col > (int)v2 );
}
return putchar(10);
}

original_code:
void printRequest(int* request) {
 int i,j;
 printf("Request vector:\n");
  for(i = 0; i < col; i++)
    printf("R%d ", i+1);
  printf("\n");
  for(j = 0; j < col; j++)
    printf("%d  ",request[j]);
  printf("\n");
}

canonical_original_code:
void printRequest(int *request) {
 int i; int j;
 printf("Request vector:\n");
  for(i = 0; i < col; i++)
    printf("R%d ", i+1);
  printf("\n");
  for(j = 0; j < col; j++)
    printf("%d  ",request[j]);
  printf("\n");
}

variable_types:
request
int *


stateSafeCheck

original_code:
int stateSafeCheck() {
 int *work = (int *) malloc(col*sizeof(int));
  int *finish = (int *) malloc(row*sizeof(int));
  int i, j, l;
  l = col > row ? col : row;
  for (i = 0; i < l; i++) {
   if (i < row)
   {
    finish[i] = 0;
   }
   if (i < col)
   {
    work[i] = s->available[i];
   }
  }
  for (i = 0; i < row; i++) {
  if (finish[i] == 0)
  {
   for (j = 0; j < col; j++)
   {
    if (s->need[i][j] > work[j])
    {
     break;
    }
   }
   if (j == col)
   {
    for (j = 0; j < col; j += 1)
    {
     work[j] = work[j] + s->allocation[i][j];
    }
    finish[i] = 1;
   }
  }
  }
  for (i = 0; i < row; i++)
  {
   if (finish[i] == 0)
   {
    printf("State is not safe!\n");
    return 0;
   }
  }
  printf("State is safe!\n");
  return 1;
}

canonical_original_code:
int stateSafeCheck() {
 int *work = (int *) malloc(col*sizeof(int));
  int *finish = (int *) malloc(row*sizeof(int));
  int i; int j; int l;
  l = col > row ? col : row;
  for (i = 0; i < l; i++) {
   if (i < row)
   {
    finish[i] = 0;
   }
   if (i < col)
   {
    work[i] = s->available[i];
   }
  }
  for (i = 0; i < row; i++) {
  if (finish[i] == 0)
  {
   for (j = 0; j < col; j++)
   {
    if (s->need[i][j] > work[j])
    {
     break;
    }
   }
   if (j == col)
   {
    for (j = 0; j < col; j += 1)
    {
     work[j] = work[j] + s->allocation[i][j];
    }
    finish[i] = 1;
   }
  }
  }
  for (i = 0; i < row; i++)
  {
   if (finish[i] == 0)
   {
    printf("State is not safe!\n");
    return 0;
   }
  }
  printf("State is safe!\n");
  return 1;
}

variable_types:
work
int *

variable_types:
finish
int *

variable_types: l int


decompiled_code:
__int64 __fastcall resource_request(unsigned int a1, __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rax
__int64 v4; // rdx
__int64 result; // rax
_DWORD *v6; // rbx
__int64 v7; // r13
__int64 v8; // rsi
__int64 v9; // rcx
__int64 v10; // rdx
__int64 v11; // rax
__int64 v12; // rsi
__int64 v13; // rcx
__int64 v14; // rdx
__int64 v15; // rax

v2 = a2;
printf("Process %d: ", a1);
printRequest(a2);
if ( col > 0 )
{
v3 = s;
v4 = 0LL;
do
{
if ( *(_DWORD *)(a2 + 4 * v4) > *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(s + 32) + 8LL * (int)a1) + 4 * v4) )
{
puts("Request can't be granted, because the process has exceeded its maximum claim");
return 0LL;
}
++v4;
}
while ( col != v4 );
v6 = (_DWORD *)a2;
v7 = 0LL;
do
{
if ( *v6 > *(_DWORD *)(*(_QWORD *)(v3 + 8) + 4 * v7) )
{
do
{
puts("No resources are available, therefore the process must wait!");
sleep(0x64u);
v3 = s;
}
while ( *v6 > *(_DWORD *)(*(_QWORD *)(s + 8) + 4 * v7) );
}
++v7;
++v6;
}
while ( col > (int)v7 );
}
pthread_mutex_lock(&lock);
if ( col > 0 )
{
v8 = *(_QWORD *)(s + 8);
v9 = *(_QWORD *)(*(_QWORD *)(s + 24) + 8LL * (int)a1);
v10 = *(_QWORD *)(*(_QWORD *)(s + 32) + 8LL * (int)a1);
v11 = 0LL;
do
{
*(_DWORD *)(v8 + 4 * v11) -= *(_DWORD *)(v2 + 4 * v11);
*(_DWORD *)(v9 + 4 * v11) += *(_DWORD *)(v2 + 4 * v11);
*(_DWORD *)(v10 + 4 * v11) -= *(_DWORD *)(v2 + 4 * v11);
++v11;
}
while ( col > (int)v11 );
}
if ( (unsigned int)stateSafeCheck() )
{
pthread_mutex_unlock(&lock);
printAvailable();
result = 1LL;
}
else
{
if ( col > 0 )
{
v12 = *(_QWORD *)(s + 8);
v13 = *(_QWORD *)(*(_QWORD *)(s + 24) + 8LL * (int)a1);
v14 = *(_QWORD *)(*(_QWORD *)(s + 32) + 8LL * (int)a1);
v15 = 0LL;
do
{
*(_DWORD *)(v12 + 4 * v15) += *(_DWORD *)(v2 + 4 * v15);
*(_DWORD *)(v13 + 4 * v15) -= *(_DWORD *)(v2 + 4 * v15);
*(_DWORD *)(v14 + 4 * v15) += *(_DWORD *)(v2 + 4 * v15);
++v15;
}
while ( col > (int)v15 );
}
pthread_mutex_unlock(&lock);
result = 0LL;
}
return result;
}

original_code:
int resource_request(int i, int *request)
{
 printf("Process %d: ",i);
 printRequest(request);
 int j;
 for (j = 0; j < col; j++) {
  if (request[j] > s->need[i][j]) {
   printf("Request can't be granted, because the process has exceeded its maximum claim\n");
   return 0;
  }
 }
 for (j = 0; j < col; j++) {
  while (request[j] > s->available[j]) {
   printf("No resources are available, therefore the process must wait!\n");
   sleep(100);
  }
 }
 pthread_mutex_lock(&lock);
 for (j = 0; j < col; j += 1)
 {
  s->available[j] -= request[j];
  s->allocation[i][j] += request[j];
  s->need[i][j] -= request[j];
 }
 if (stateSafeCheck() == 0)
 {
  for (j = 0; j < col; j += 1)
  {
   s->available[j] += request[j];
   s->allocation[i][j] -= request[j];
   s->need[i][j] += request[j];
  }
  pthread_mutex_unlock(&lock);
  return 0;
 }
 pthread_mutex_unlock(&lock);
 printAvailable();
  return 1;
}

variable_types: i int


decompiled_code:
int __fastcall resource_release(int a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rcx
__int64 v5; // rdx
__int64 v6; // rax

puts("RELEASING RESOURCES!");
printRequest(a2);
pthread_mutex_lock(&lock);
if ( col > 0 )
{
v3 = *(_QWORD *)(s + 8);
v4 = *(_QWORD *)(*(_QWORD *)(s + 24) + 8LL * a1);
v5 = *(_QWORD *)(*(_QWORD *)(s + 32) + 8LL * a1);
v6 = 0LL;
do
{
*(_DWORD *)(v3 + 4 * v6) += *(_DWORD *)(a2 + 4 * v6);
*(_DWORD *)(v4 + 4 * v6) -= *(_DWORD *)(a2 + 4 * v6);
*(_DWORD *)(v5 + 4 * v6) += *(_DWORD *)(a2 + 4 * v6);
++v6;
}
while ( col > (int)v6 );
}
pthread_mutex_unlock(&lock);
return printAvailable();
}

original_code:
void resource_release(int i, int *request)
{
 printf("RELEASING RESOURCES!\n");
 printRequest(request);
 int j;
 pthread_mutex_lock(&lock);
 for (j = 0; j < col; j++) {
  s->available[j] += request[j];
  s->allocation[i][j] -= request[j];
  s->need[i][j] += request[j];
 }
 pthread_mutex_unlock(&lock);
 printAvailable();
}


decompiled_code:
int __fastcall generate_request(unsigned int a1, _DWORD *a2)
{
int v2; // er15
int v3; // ebx
int i; // eax
__int64 v5; // rax
__int64 v6; // rax
_DWORD *v7; // rbp
double v8; // xmm0_8
double v10; // [rsp+8h] [rbp-40h]

v2 = 0;
v3 = 0;
for ( i = col; ; i = col )
{
if ( v2 < i )
{
v5 = v2++;
v6 = v5;
v7 = &a2[v6];
goto LABEL_3;
}
if ( v3 )
break;
while ( i <= 0 )
;
v7 = a2;
v2 = 1;
v6 = 0LL;
LABEL_3:
v10 = (double)*(int *)(*(_QWORD *)(*(_QWORD *)(s + 32) + 8LL * (int)a1) + v6 * 4);
v8 = (double)rand() * v10 / 2147483647.0;
*v7 = (int)v8;
v3 += (int)v8;
}
return printf("Process %d: Requesting resources.\n", a1);
}

canonical_decompiled_code:
int __fastcall func6(unsigned int a1, _DWORD *a2)
{
int v2; // er15
int v3; // ebx
int i; // eax
__int64 v5; // rax
__int64 v6; // rax
_DWORD *v7; // rbp
double v8; // xmm0_8
double v10; // [rsp+8h] [rbp-40h]

v2 = 0;
v3 = 0;
for ( i = col; ; i = col )
{
if ( v2 < i )
{
v5 = v2++;
v6 = v5;
v7 = &a2[v6];
goto LABEL_3;
}
if ( v3 )
break;
while ( i <= 0 )
;
v7 = a2;
v2 = 1;
v6 = 0LL;
LABEL_3:
v10 = (double)*(int *)(*(_QWORD *)(*(_QWORD *)(s + 32) + 8LL * (int)a1) + v6 * 4);
v8 = (double)rand() * v10 / 2147483647.0;
*v7 = (int)v8;
v3 += (int)v8;
}
return printf("Process %d: Requesting resources.\n", a1);
}

original_code:
void generate_request(int i, int *request)
{
  int j, sum = 0;
  while (!sum) {
    for (j = 0;j < col; j++) {
      request[j] = s->need[i][j] * ((double)rand())/ (double)2147483647;
      sum += request[j];
    }
  }
  printf("Process %d: Requesting resources.\n",i);
}

canonical_original_code:
void generate_request(int i, int *request)
{
  int j; int sum = 0;
  while (!sum) {
    for (j = 0;j < col; j++) {
      request[j] = s->need[i][j] * ((double)rand())/ (double)2147483647;
      sum += request[j];
    }
  }
  printf("Process %d: Requesting resources.\n",i);
}

variable_types: sum int


decompiled_code:
int __fastcall generate_release(unsigned int a1, _DWORD *a2)
{
int v2; // er15
int v3; // ebx
int i; // eax
__int64 v5; // rax
__int64 v6; // rax
_DWORD *v7; // rbp
double v8; // xmm0_8
double v10; // [rsp+8h] [rbp-40h]

v2 = 0;
v3 = 0;
for ( i = col; ; i = col )
{
if ( v2 < i )
{
v5 = v2++;
v6 = v5;
v7 = &a2[v6];
goto LABEL_3;
}
if ( v3 )
break;
while ( i <= 0 )
;
v7 = a2;
v2 = 1;
v6 = 0LL;
LABEL_3:
v10 = (double)*(int *)(*(_QWORD *)(*(_QWORD *)(s + 24) + 8LL * (int)a1) + v6 * 4);
v8 = (double)rand() * v10 / 2147483647.0;
*v7 = (int)v8;
v3 += (int)v8;
}
return printf("Process %d: Releasing resources.\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, _DWORD *a2)
{
int v2; // er15
int v3; // ebx
int i; // eax
__int64 v5; // rax
__int64 v6; // rax
_DWORD *v7; // rbp
double v8; // xmm0_8
double v10; // [rsp+8h] [rbp-40h]

v2 = 0;
v3 = 0;
for ( i = col; ; i = col )
{
if ( v2 < i )
{
v5 = v2++;
v6 = v5;
v7 = &a2[v6];
goto LABEL_3;
}
if ( v3 )
break;
while ( i <= 0 )
;
v7 = a2;
v2 = 1;
v6 = 0LL;
LABEL_3:
v10 = (double)*(int *)(*(_QWORD *)(*(_QWORD *)(s + 24) + 8LL * (int)a1) + v6 * 4);
v8 = (double)rand() * v10 / 2147483647.0;
*v7 = (int)v8;
v3 += (int)v8;
}
return printf("Process %d: Releasing resources.\n", a1);
}

original_code:
void generate_release(int i, int *request)
{
  int j, sum = 0;
  while (!sum) {
    for (j = 0;j < col; j++) {
      request[j] = s->allocation[i][j] * ((double)rand())/ (double)2147483647;
      sum += request[j];
    }
  }
  printf("Process %d: Releasing resources.\n",i);
}

canonical_original_code:
void generate_release(int i, int *request)
{
  int j; int sum = 0;
  while (!sum) {
    for (j = 0;j < col; j++) {
      request[j] = s->allocation[i][j] * ((double)rand())/ (double)2147483647;
      sum += request[j];
    }
  }
  printf("Process %d: Releasing resources.\n",i);
}


decompiled_code:
void __fastcall __noreturn process_thread(void *a1)
{
_DWORD *v1; // rbx

v1 = malloc(4LL * col);
while ( 1 )
{
generate_request((unsigned int)a1, v1);
while ( !(unsigned int)resource_request((unsigned int)a1, (__int64)v1) )
Sleep(100.0);
generate_release((unsigned int)a1, v1);
resource_release((int)a1, (__int64)v1);
Sleep(1000.0);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
_DWORD *v1; // rbx

v1 = malloc(4LL * col);
while ( 1 )
{
func6((unsigned int)a1, v1);
while ( !(unsigned int)func4((unsigned int)a1, (__int64)v1) )
func2(100.0);
func7((unsigned int)a1, v1);
func5((int)a1, (__int64)v1);
func2(1000.0);
}
}

original_code:
void *process_thread(void *param)
{
  int i = (int) (long) param, j;
  int *request = malloc(col*sizeof(int));
  while (1) {
    generate_request(i, request);
    while (!resource_request(i, request)) {
      Sleep(100);
    }
    generate_release(i, request);
    resource_release(i, request);
    Sleep(1000);
  }
  free(request);
}

canonical_original_code:
void *process_thread(void *param)
{
  int i = (int) (long) param; int j;
  int *request = malloc(col*sizeof(int));
  while (1) {
    generate_request(i, request);
    while (!resource_request(i, request)) {
      Sleep(100);
    }
    generate_release(i, request);
    resource_release(i, request);
    Sleep(1000);
  }
  free(request);
}

variable_types:
param
void *

return_type: void *


Sleep

decompiled_code:
int __fastcall Sleep(float a1)
{
int v1; // eax

v1 = rand();
return usleep((int)(float)((float)((float)((float)v1 * a1) * 4.6566129e-10) * 1000.0));
}

canonical_decompiled_code:
int __fastcall func2(float a1)
{
int v1; // eax

v1 = rand();
return usleep((int)(float)((float)((float)((float)v1 * a1) * 4.6566129e-10) * 1000.0));
}

original_code:
void Sleep(float wait_time_ms)
{
  wait_time_ms = ((float)rand())*wait_time_ms / (float)2147483647;
  usleep((int) (wait_time_ms * 1e3f));
}