InitPrinter

decompiled_code:
__int64 __fastcall InitPrinter(__int64 fd)
{
unsigned __int8 v1; // al
unsigned __int8 v2; // al
unsigned __int8 v3; // al
unsigned __int8 v4; // al
char v5; // bl
unsigned __int8 v6; // al
unsigned __int8 v7; // al
unsigned __int8 v8; // al
unsigned __int8 v9; // al
unsigned __int8 v10; // al
unsigned __int8 v11; // al
unsigned __int8 v12; // al
unsigned __int8 v13; // al
unsigned __int8 v14; // al
unsigned __int8 v15; // al
unsigned __int8 v16; // al
unsigned __int8 v17; // al
unsigned __int8 v18; // al
unsigned __int8 v19; // al
unsigned __int8 v20; // al
unsigned __int8 v21; // al
unsigned int v22; // er12
int v24[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v24[0] = 0;
Parport_WriteControl(fd, 6LL);
v1 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 91 PortStatus %04X \n", v1);
Parport_WriteData((unsigned int)fd, 170LL);
v2 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter out 0xaa 95 PortStatus %04X\n", v2);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 8LL);
v3 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 97 Ctrl 0x08 PortStatus %04X\n", v3);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 12LL);
v4 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 100 Ctrl 0x0c PortStatus %04X\n", v4);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 88LL, 255LL, 10000000LL) == 1 )
return 5LL;
v5 = 2;
v6 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 140 PortStatus %04X\n", v6);
while ( 1 )
{
Parport_WriteControl((unsigned int)fd, 4LL);
v7 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 150 0x04 PortStatus %04X\n", v7);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v8 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 151 0x04 PortStatus %04X\n", v8);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 170LL);
v9 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 155 0x04 PortStatus %04X\n", v9);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 85LL);
v10 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 156 0x04 PortStatus %04X\n", v10);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 0LL);
v11 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 157 0x04 PortStatus %04X\n", v11);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 255LL);
v12 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 200 PortStatus %04X\n", v12);
v13 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 220 0x04 PortStatus %04X\n", v13);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 12LL);
if ( v5 == 1 )
break;
v5 = 1;
}
v14 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 250 PortStatus %04X\n", v14);
Parport_WriteData((unsigned int)fd, 40LL);
v15 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 400 0x04 PortStatus %04X\n", v15);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
v16 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 420 out 0x28 PortStatus %04X\n", v16);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
Parport_WriteControl((unsigned int)fd, 6LL);
v17 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 450 ctrl 0x06 PortStatus %04X\n", v17);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 600 PortMode %04X\n", (unsigned int)v24[0]);
v18 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 630 PortStatus %04X\n", v18);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 56LL, 255LL, 10000000LL) == 1 )
return 5LL;
Parport_WriteControl((unsigned int)fd, 7LL);
v19 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 650 ctrl 0x07 PortStatus %04X\n", v19);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v20 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 670 ctrl 0x04 PortStatus %04X\n", v20);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 216LL, 255LL, 10000000LL) == 1 )
return 5LL;
v21 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 700 PortStatus %04X\n", v21);
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 900 PortMode %04X\n", (unsigned int)v24[0]);
v22 = Send_PaperPacket((unsigned int)fd);
if ( (_BYTE)v22 )
return 5LL;
fwrite("hlcapt:InitPrinter Point 1500\n", 1uLL, 0x1EuLL, stderr);
PrintBuffer();
return v22;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 fd)
{
unsigned __int8 v1; // al
unsigned __int8 v2; // al
unsigned __int8 v3; // al
unsigned __int8 v4; // al
char v5; // bl
unsigned __int8 v6; // al
unsigned __int8 v7; // al
unsigned __int8 v8; // al
unsigned __int8 v9; // al
unsigned __int8 v10; // al
unsigned __int8 v11; // al
unsigned __int8 v12; // al
unsigned __int8 v13; // al
unsigned __int8 v14; // al
unsigned __int8 v15; // al
unsigned __int8 v16; // al
unsigned __int8 v17; // al
unsigned __int8 v18; // al
unsigned __int8 v19; // al
unsigned __int8 v20; // al
unsigned __int8 v21; // al
unsigned int v22; // er12
int v24[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v24[0] = 0;
Parport_WriteControl(fd, 6LL);
v1 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 91 PortStatus %04X \n", v1);
Parport_WriteData((unsigned int)fd, 170LL);
v2 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter out 0xaa 95 PortStatus %04X\n", v2);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 8LL);
v3 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 97 Ctrl 0x08 PortStatus %04X\n", v3);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 12LL);
v4 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 100 Ctrl 0x0c PortStatus %04X\n", v4);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 88LL, 255LL, 10000000LL) == 1 )
return 5LL;
v5 = 2;
v6 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 140 PortStatus %04X\n", v6);
while ( 1 )
{
Parport_WriteControl((unsigned int)fd, 4LL);
v7 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 150 0x04 PortStatus %04X\n", v7);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v8 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 151 0x04 PortStatus %04X\n", v8);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 170LL);
v9 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 155 0x04 PortStatus %04X\n", v9);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 85LL);
v10 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 156 0x04 PortStatus %04X\n", v10);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 0LL);
v11 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 157 0x04 PortStatus %04X\n", v11);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 255LL);
v12 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 200 PortStatus %04X\n", v12);
v13 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 220 0x04 PortStatus %04X\n", v13);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 12LL);
if ( v5 == 1 )
break;
v5 = 1;
}
v14 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 250 PortStatus %04X\n", v14);
Parport_WriteData((unsigned int)fd, 40LL);
v15 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 400 0x04 PortStatus %04X\n", v15);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
v16 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 420 out 0x28 PortStatus %04X\n", v16);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
Parport_WriteControl((unsigned int)fd, 6LL);
v17 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 450 ctrl 0x06 PortStatus %04X\n", v17);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 600 PortMode %04X\n", (unsigned int)v24[0]);
v18 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 630 PortStatus %04X\n", v18);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 56LL, 255LL, 10000000LL) == 1 )
return 5LL;
Parport_WriteControl((unsigned int)fd, 7LL);
v19 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 650 ctrl 0x07 PortStatus %04X\n", v19);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v20 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 670 ctrl 0x04 PortStatus %04X\n", v20);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 216LL, 255LL, 10000000LL) == 1 )
return 5LL;
v21 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 700 PortStatus %04X\n", v21);
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 900 PortMode %04X\n", (unsigned int)v24[0]);
v22 = Send_PaperPacket((unsigned int)fd);
if ( (_BYTE)v22 )
return 5LL;
fwrite("hlcapt:InitPrinter Point 1500\n", 1uLL, 0x1EuLL, stderr);
PrintBuffer();
return v22;
}

original_code:
BYTE InitPrinter(int Device)
{
  BYTE i=0;
  int dPortMode=0;
  Parport_WriteControl(Device, 0x2 |0x4 );
  fprintf(stderr, "hlcapt:InitPrinter  91 PortStatus %04X  \n",(int)Parport_ReadStatus(Device));
  Parport_WriteData(Device, 0xaa);
  fprintf(stderr, "hlcapt:InitPrinter out 0xaa 95 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  Parport_WriteControl(Device, 0x08);
  fprintf(stderr, "hlcapt:InitPrinter  97  Ctrl 0x08 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  Parport_WriteControl(Device, 0x0c);
    fprintf(stderr, "hlcapt:InitPrinter  100 Ctrl 0x0c  PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  if(WaitForStatus(Device, 0x58, 0xff, 10000000L) == 0x01){
    return 0x05;
  }
  fprintf(stderr, "hlcapt:InitPrinter  140 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  for(i = 0; i < 2 ; i++)
  {
    Parport_WriteControl(Device, 0x04);
    fprintf(stderr, "hlcapt:InitPrinter  150  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteControl(Device, 0x04);
    fprintf(stderr, "hlcapt:InitPrinter  151  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0xaa);
    fprintf(stderr, "hlcapt:InitPrinter  155  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0x55);
    fprintf(stderr, "hlcapt:InitPrinter  156  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0x00);
    fprintf(stderr, "hlcapt:InitPrinter  157  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0xff);
 fprintf(stderr, "hlcapt:InitPrinter  200 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    fprintf(stderr, "hlcapt:InitPrinter  220  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteControl(Device, 0x0c);
  }
  i=0x28;
   fprintf(stderr, "hlcapt:InitPrinter  250 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_WriteData(Device, 0x28);
     fprintf(stderr, "hlcapt:InitPrinter 400  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_MonitorStatus(Device,1000);
   fprintf(stderr, "hlcapt:InitPrinter 420  out 0x28 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_MonitorStatus(Device,1000);
   Port_Sleep();
   Parport_WriteControl(Device, 0x06);
   fprintf(stderr, "hlcapt:InitPrinter 450  ctrl 0x06 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_MonitorStatus(Device,1000);
   Port_Sleep();
   ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x98)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dPortMode);
   fprintf(stderr, "hlcapt:InitPrinter  600 PortMode %04X\n",(int)dPortMode);
   fprintf(stderr, "hlcapt:InitPrinter  630 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   if(WaitForStatus(Device, 0x38, 0xff, 10000000L) == 0x01){
  return 0x05;
   }
  Parport_WriteControl(Device, 0x07);
  fprintf(stderr, "hlcapt:InitPrinter 650  ctrl 0x07 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  Parport_WriteControl(Device, 0x04);
  fprintf(stderr, "hlcapt:InitPrinter 670  ctrl 0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  if(WaitForStatus(Device, 0xd8, 0xff, 10000000L) == 0x01){
      return 0x05;
  }
  fprintf(stderr, "hlcapt:InitPrinter  700 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x98)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dPortMode);
  fprintf(stderr, "hlcapt:InitPrinter  900 PortMode %04X\n",(int)dPortMode);
  if(Send_PaperPacket(Device) != 0x00){
    return 0x05;
  }
   fprintf(stderr, "hlcapt:InitPrinter Point 1500\n");
  PrintBuffer();
  return 0x00;
}

canonical_original_code:
unsigned char InitPrinter(int Device)
{
  unsigned char i = 0;
  int dPortMode = 0;
  Parport_WriteControl(Device, 0x2 |0x4 );
  fprintf(stderr, "hlcapt:InitPrinter  91 PortStatus %04X  \n",(int)Parport_ReadStatus(Device));
  Parport_WriteData(Device, 0xaa);
  fprintf(stderr, "hlcapt:InitPrinter out 0xaa 95 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  Parport_WriteControl(Device, 0x08);
  fprintf(stderr, "hlcapt:InitPrinter  97  Ctrl 0x08 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  Parport_WriteControl(Device, 0x0c);
    fprintf(stderr, "hlcapt:InitPrinter  100 Ctrl 0x0c  PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  if(WaitForStatus(Device, 0x58, 0xff, 10000000L) == 0x01){
    return 0x05;
  }
  fprintf(stderr, "hlcapt:InitPrinter  140 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  for(i = 0; i < 2 ; i++)
  {
    Parport_WriteControl(Device, 0x04);
    fprintf(stderr, "hlcapt:InitPrinter  150  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteControl(Device, 0x04);
    fprintf(stderr, "hlcapt:InitPrinter  151  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0xaa);
    fprintf(stderr, "hlcapt:InitPrinter  155  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0x55);
    fprintf(stderr, "hlcapt:InitPrinter  156  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0x00);
    fprintf(stderr, "hlcapt:InitPrinter  157  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteData(Device, 0xff);
 fprintf(stderr, "hlcapt:InitPrinter  200 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    fprintf(stderr, "hlcapt:InitPrinter  220  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    Parport_MonitorStatus(Device,1000);
    Parport_WriteControl(Device, 0x0c);
  }
  i=0x28;
   fprintf(stderr, "hlcapt:InitPrinter  250 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_WriteData(Device, 0x28);
     fprintf(stderr, "hlcapt:InitPrinter 400  0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_MonitorStatus(Device,1000);
   fprintf(stderr, "hlcapt:InitPrinter 420  out 0x28 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_MonitorStatus(Device,1000);
   Port_Sleep();
   Parport_WriteControl(Device, 0x06);
   fprintf(stderr, "hlcapt:InitPrinter 450  ctrl 0x06 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   Parport_MonitorStatus(Device,1000);
   Port_Sleep();
   ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x98)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dPortMode);
   fprintf(stderr, "hlcapt:InitPrinter  600 PortMode %04X\n",(int)dPortMode);
   fprintf(stderr, "hlcapt:InitPrinter  630 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
   if(WaitForStatus(Device, 0x38, 0xff, 10000000L) == 0x01){
  return 0x05;
   }
  Parport_WriteControl(Device, 0x07);
  fprintf(stderr, "hlcapt:InitPrinter 650  ctrl 0x07 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  Parport_WriteControl(Device, 0x04);
  fprintf(stderr, "hlcapt:InitPrinter 670  ctrl 0x04 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  Parport_MonitorStatus(Device,1000);
  if(WaitForStatus(Device, 0xd8, 0xff, 10000000L) == 0x01){
      return 0x05;
  }
  fprintf(stderr, "hlcapt:InitPrinter  700 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x98)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dPortMode);
  fprintf(stderr, "hlcapt:InitPrinter  900 PortMode %04X\n",(int)dPortMode);
  if(Send_PaperPacket(Device) != 0x00){
    return 0x05;
  }
   fprintf(stderr, "hlcapt:InitPrinter Point 1500\n");
  PrintBuffer();
  return 0x00;
}

variable_types: Device int

variable_types:
i
unsigned char

variable_types: dPortMode int

return_type: unsigned char


CheckPaper

decompiled_code:
bool __fastcall CheckPaper(__int64 a1)
{
char v1; // r8
bool result; // al

v1 = Send_PaperPacket(a1);
result = 0;
if ( !v1 )
result = RETBUF[16] == 0xFD;
return result;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1)
{
char v1; // r8
bool result; // al

v1 = Send_PaperPacket(a1);
result = 0;
if ( !v1 )
result = RETBUF[16] == 0xFD;
return result;
}

original_code:
BYTE CheckPaper(int Device)
{
  if(Send_PaperPacket(Device) != 0x00)
    return 0;
  return(RETBUF[16] == 0xfd);
}

canonical_original_code:
unsigned char CheckPaper(int Device)
{
  if(Send_PaperPacket(Device) != 0x00)
    return 0;
  return(RETBUF[16] == 0xfd);
}


StartPrint

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

if ( (unsigned __int8)Send_Packet_a1_a1()
|| (unsigned __int8)Send_Packet_a0_a2(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_PaperPacket(a1)
|| (unsigned __int8)Send_Packet_a4_e0(a1)
|| (unsigned __int8)Send_Packet_a5_e0(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_Packet_a0_a0(a1)
|| (unsigned __int8)Send_PaperPacket(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_Packet_a0_d0(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_Packet_a1_d0(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (result = Send_Packet_a0_a0(a1), (_BYTE)result) )
{
result = 5LL;
}
return result;
}

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

if ( (unsigned __int8)Send_Packet_a1_a1()
|| (unsigned __int8)Send_Packet_a0_a2(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_PaperPacket(a1)
|| (unsigned __int8)Send_Packet_a4_e0(a1)
|| (unsigned __int8)Send_Packet_a5_e0(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_Packet_a0_a0(a1)
|| (unsigned __int8)Send_PaperPacket(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_Packet_a0_d0(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (unsigned __int8)Send_Packet_a1_d0(a1)
|| (unsigned __int8)Send_InterBandPacket(a1)
|| (result = Send_Packet_a0_a0(a1), (_BYTE)result) )
{
result = 5LL;
}
return result;
}

original_code:
BYTE StartPrint(int Device)
{
  if(Send_Packet_a1_a1(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_a2(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_PaperPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a4_e0(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a5_e0(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_a0(Device) != 0x00)
    return 0x05;
  if(Send_PaperPacket(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_d0(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a1_d0(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_a0(Device) != 0x00)
    return 0x05;
  return 0x00;
}

canonical_original_code:
unsigned char StartPrint(int Device)
{
  if(Send_Packet_a1_a1(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_a2(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_PaperPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a4_e0(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a5_e0(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_a0(Device) != 0x00)
    return 0x05;
  if(Send_PaperPacket(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_d0(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a1_d0(Device) != 0x00)
    return 0x05;
  if(Send_InterBandPacket(Device) != 0x00)
    return 0x05;
  if(Send_Packet_a0_a0(Device) != 0x00)
    return 0x05;
  return 0x00;
}


ClosePrinter

decompiled_code:
__int64 __fastcall ClosePrinter(__int64 a1)
{
unsigned __int8 v1; // bl
unsigned __int8 v2; // al

Parport_WriteData(a1, 52LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 12LL);
usleep(5u);
v1 = Parport_ReadData((unsigned int)a1);
v2 = Parport_ReadStatus((unsigned int)a1);
fprintf(stderr, "hlcapt:ClosePrinter 650 ctrl PortStatus %02X Device data %02X \n", v2, v1);
return Parport_MonitorStatus((unsigned int)a1, 1000LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned __int8 v1; // bl
unsigned __int8 v2; // al

Parport_WriteData(a1, 52LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 8LL);
usleep(5u);
Parport_WriteControl((unsigned int)a1, 12LL);
usleep(5u);
v1 = Parport_ReadData((unsigned int)a1);
v2 = Parport_ReadStatus((unsigned int)a1);
fprintf(stderr, "hlcapt:ClosePrinter 650 ctrl PortStatus %02X Device data %02X \n", v2, v1);
return Parport_MonitorStatus((unsigned int)a1, 1000LL);
}

original_code:
void ClosePrinter(int a_Device){
  Parport_WriteData(a_Device, 0x34);
  usleep(5);
  Parport_WriteControl(a_Device, 0x08);
  usleep(5);
  Parport_WriteControl(a_Device, 0x08);
  usleep(5);
  Parport_WriteControl(a_Device, 0x08);
  usleep(5);
  Parport_WriteControl(a_Device, 0x08);
  usleep(5);
  Parport_WriteControl(a_Device, 0x0C);
  usleep(5);
  fprintf(stderr, "hlcapt:ClosePrinter 650  ctrl PortStatus %02X Device data %02X \n",(int)Parport_ReadStatus(a_Device),(int)Parport_ReadData(a_Device));
  Parport_MonitorStatus(a_Device,1000);
}

return_type: void


decompiled_code:
__int64 __fastcall Parport_Open(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
int v4; // edi
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = 0;
v1 = open(a1, 2);
v2 = v1;
if ( v1 == -1 )
{
fprintf(stderr, "ERROR: Parport_Open Can not open %s\n", a1);
}
else if ( ioctl(v1, 0x708BuLL) )
{
perror("ERROR: Parport_Open Can not claim");
v4 = v2;
v2 = -1;
close(v4);
}
else
{
ioctl(v2, 0x80047098uLL, v5);
fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", v5[0]);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
int v4; // edi
unsigned int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = 0;
v1 = open(a1, 2);
v2 = v1;
if ( v1 == -1 )
{
fprintf(stderr, "ERROR: Parport_Open Can not open %s\n", a1);
}
else if ( ioctl(v1, 0x708BuLL) )
{
perror("ERROR: Parport_Open Can not claim");
v4 = v2;
v2 = -1;
close(v4);
}
else
{
ioctl(v2, 0x80047098uLL, v5);
fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", v5[0]);
}
return v2;
}

original_code:
inline int Parport_Open(char *DeviceName)
{
  int Device=-1;
  int bCurrentPortMode=0;
  int bPortMode=(1<<4);
  bPortMode=((1<<4) | (1<<5)) ;
  Device = open (DeviceName, 02);
  if(Device == -1){
    fprintf(stderr, "ERROR: Parport_Open Can not open %s\n", DeviceName);
    return Device;
  }
  if (ioctl (Device, (((0U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x8b)) << 0) | ((0) << ((0 +8)+8)))))
  {
    perror("ERROR: Parport_Open Can not claim");
    close (Device);
    return -1;
  }
   ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x98)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &bCurrentPortMode);
    fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", (int) bCurrentPortMode);
  return Device;
}

canonical_original_code:
inline int Parport_Open(char *DeviceName)
{
  int Device = -1;
  int bCurrentPortMode = 0;
  int bPortMode = (1<<4);
  bPortMode=((1<<4) | (1<<5)) ;
  Device = open (DeviceName, 02);
  if(Device == -1){
    fprintf(stderr, "ERROR: Parport_Open Can not open %s\n", DeviceName);
    return Device;
  }
  if (ioctl (Device, (((0U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x8b)) << 0) | ((0) << ((0 +8)+8)))))
  {
    perror("ERROR: Parport_Open Can not claim");
    close (Device);
    return -1;
  }
   ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x98)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &bCurrentPortMode);
    fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", (int) bCurrentPortMode);
  return Device;
}

variable_types:
DeviceName
char *

variable_types: bCurrentPortMode int

variable_types: bPortMode int

return_type: int


decompiled_code:
int __fastcall Parport_Close(int fd)
{
ioctl(fd, 0x708CuLL);
return close(fd);
}

canonical_decompiled_code:
int __fastcall func1(int fd)
{
ioctl(fd, 0x708CuLL);
return close(fd);
}


decompiled_code:
__int64 __fastcall Parport_ReadData(int a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

ioctl(a1, 0x80017085uLL, v2);
return (unsigned __int8)v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

ioctl(a1, 0x80017085uLL, v2);
return (unsigned __int8)v2[0];
}

original_code:
inline BYTE Parport_ReadData(int Device)
{
  BYTE data;
  ioctl(Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x85)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &data);
  return data;
}

canonical_original_code:
inline unsigned char Parport_ReadData(int Device)
{
  unsigned char data;
  ioctl(Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x85)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &data);
  return data;
}

variable_types:
data
unsigned char


decompiled_code:
int __fastcall Parport_WriteData(int a1, char a2)
{
char v3[12]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
return ioctl(a1, 0x40017086uLL, v3);
}

canonical_decompiled_code:
int __fastcall func3(int a1, char a2)
{
char v3[12]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
return ioctl(a1, 0x40017086uLL, v3);
}

canonical_original_code:
inline void Parport_WriteData(int Device, unsigned char data)
{
  ioctl (Device, (((1U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x86)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &data);
}


decompiled_code:
__int64 __fastcall Parport_ReadStatus(int a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 0;
ioctl(a1, 0x80017081uLL, v2);
return (unsigned __int8)v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 0;
ioctl(a1, 0x80017081uLL, v2);
return (unsigned __int8)v2[0];
}

original_code:
inline BYTE Parport_ReadStatus(int Device)
{
  BYTE stat=0;
  ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x81)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &stat);
  return stat;
}

canonical_original_code:
inline unsigned char Parport_ReadStatus(int Device)
{
  unsigned char stat = 0;
  ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x81)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &stat);
  return stat;
}

variable_types:
stat
unsigned char


decompiled_code:
int __fastcall Parport_WriteControl(int fd, unsigned int a2)
{
char v3[16]; // [rsp+Ch] [rbp-1Ch] BYREF
int v4[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v3[0] = a2;
v4[0] = (a2 >> 5) & 1;
ioctl(fd, 0x40047090uLL, v4);
return ioctl(fd, 0x40017084uLL, v3);
}

canonical_decompiled_code:
int __fastcall func5(int fd, unsigned int a2)
{
char v3[16]; // [rsp+Ch] [rbp-1Ch] BYREF
int v4[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v3[0] = a2;
v4[0] = (a2 >> 5) & 1;
ioctl(fd, 0x40047090uLL, v4);
return ioctl(fd, 0x40017084uLL, v3);
}

variable_types:
control
unsigned char

variable_types: direction int


decompiled_code:
__int64 __fastcall Parport_ReadControl(int a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 0;
ioctl(a1, 0x80017083uLL, v2);
return (unsigned __int8)v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v2[0] = 0;
ioctl(a1, 0x80017083uLL, v2);
return (unsigned __int8)v2[0];
}

original_code:
BYTE Parport_ReadControl(int Device)
{
  BYTE bControl=0;
  ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x83)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &bControl);
  return bControl;
}

canonical_original_code:
unsigned char Parport_ReadControl(int Device)
{
  unsigned char bControl = 0;
  ioctl (Device, (((2U) << (((0 +8)+8)+14)) | ((('p')) << (0 +8)) | (((0x83)) << 0) | ((((sizeof(unsigned char)))) << ((0 +8)+8))), &bControl);
  return bControl;
}

variable_types:
bControl
unsigned char


decompiled_code:
size_t __fastcall Parport_MonitorStatus(int a1, int a2)
{
int v2; // ebp
unsigned int i; // ebx
unsigned __int8 v4; // r8
int v5; // eax

if ( a2 > 0 )
{
v2 = 0;
for ( i = 0; i != a2; ++i )
{
v4 = Parport_ReadStatus(a1);
v5 = v2;
v2 = v4;
if ( v4 != v5 )
fprintf(stderr, "pport:Parport_MonitorStatus 30 PortStatus %02X %d\n", v4, i);
}
}
return fwrite("pport:Parport_MonitorStatus 999 \n \n", 1uLL, 0x23uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func7(int a1, int a2)
{
int v2; // ebp
unsigned int i; // ebx
unsigned __int8 v4; // r8
int v5; // eax

if ( a2 > 0 )
{
v2 = 0;
for ( i = 0; i != a2; ++i )
{
v4 = func4(a1);
v5 = v2;
v2 = v4;
if ( v4 != v5 )
fprintf(stderr, "pport:Parport_MonitorStatus 30 PortStatus %02X %d\n", v4, i);
}
}
return fwrite("pport:Parport_MonitorStatus 999 \n \n", 1uLL, 0x23uLL, stderr);
}

original_code:
void Parport_MonitorStatus(int a_dDevice, int a_dCounter){
 int dIndex=0;
 int bCurrentStatus=0;
 int bPrevStatus=0;
    for (dIndex=0;dIndex<a_dCounter;dIndex++){
        bCurrentStatus=Parport_ReadStatus(a_dDevice);
        if (bPrevStatus!=bCurrentStatus){
         bPrevStatus=bCurrentStatus;
          fprintf(stderr, "pport:Parport_MonitorStatus 30 PortStatus %02X %d\n",(int)bPrevStatus,dIndex);
         }
        }
    fprintf(stderr, "pport:Parport_MonitorStatus 999 \n \n");
}

canonical_original_code:
void Parport_MonitorStatus(int a_dDevice, int a_dCounter){
 int dIndex = 0;
 int bCurrentStatus = 0;
 int bPrevStatus = 0;
    for (dIndex=0;dIndex<a_dCounter;dIndex++){
        bCurrentStatus=Parport_ReadStatus(a_dDevice);
        if (bPrevStatus!=bCurrentStatus){
         bPrevStatus=bCurrentStatus;
          fprintf(stderr, "pport:Parport_MonitorStatus 30 PortStatus %02X %d\n",(int)bPrevStatus,dIndex);
         }
        }
    fprintf(stderr, "pport:Parport_MonitorStatus 999 \n \n");
}

variable_types: dIndex int

variable_types: bCurrentStatus int

variable_types: bPrevStatus int


WaitForStatus

decompiled_code:
__int64 __fastcall WaitForStatus(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3, __int64 a4)
{
int v6; // eax
unsigned int v7; // er12
_BYTE v9[72]; // [rsp+0h] [rbp-48h] BYREF

fprintf(stderr, "DEBUG - llcapt:WaitForStatus(0x%02x, 0x%02x...) - ", a2, a3);
StartTimer(v9, a4);
do
{
v6 = Parport_ReadStatus(a1);
v7 = v6 & 0xFFFFFFF8;
if ( (v6 & 0xF8 & a3) == a2 )
{
fprintf(stderr, "OK %02x\n", (unsigned __int8)v7);
return v7;
}
usleep(0x32u);
}
while ( !(unsigned __int8)Timeout(v9) );
fprintf(stderr, "TIMEOUT - llcapt STATUS IS 0x%02x\n", (unsigned __int8)v7);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3, __int64 a4)
{
int v6; // eax
unsigned int v7; // er12
_BYTE v9[72]; // [rsp+0h] [rbp-48h] BYREF

fprintf(stderr, "DEBUG - llcapt:WaitForStatus(0x%02x, 0x%02x...) - ", a2, a3);
StartTimer(v9, a4);
do
{
v6 = Parport_ReadStatus(a1);
v7 = v6 & 0xFFFFFFF8;
if ( (v6 & 0xF8 & a3) == a2 )
{
fprintf(stderr, "OK %02x\n", (unsigned __int8)v7);
return v7;
}
usleep(0x32u);
}
while ( !(unsigned __int8)Timeout(v9) );
fprintf(stderr, "TIMEOUT - llcapt STATUS IS 0x%02x\n", (unsigned __int8)v7);
return 1LL;
}

original_code:
BYTE WaitForStatus(int Device, const BYTE s, const BYTE mask, const ULONG usec)
{
  BYTE stat=0;
  TTimeId Timer;
  fprintf(stderr, "DEBUG - llcapt:WaitForStatus(0x%02x, 0x%02x...) - ", s, mask);
  StartTimer(&Timer, usec);
  do
  {
   stat = Parport_ReadStatus(Device) & 0xf8;
   if( (stat & mask) == s)
   {
      fprintf(stderr, "OK %02x\n", stat);
     return stat;
   }
 usleep(50);
  }
  while(!Timeout(&Timer));
  fprintf(stderr, "TIMEOUT - llcapt STATUS IS 0x%02x\n", stat);
  return 0x01;
}

canonical_original_code:
unsigned char WaitForStatus(int Device, unsigned char s, unsigned char mask, unsigned long usec)
{
  unsigned char stat = 0;
  struct TTimeId Timer;
  fprintf(stderr, "DEBUG - llcapt:WaitForStatus(0x%02x, 0x%02x...) - ", s, mask);
  StartTimer(&Timer, usec);
  do
  {
   stat = Parport_ReadStatus(Device) & 0xf8;
   if( (stat & mask) == s)
   {
      fprintf(stderr, "OK %02x\n", stat);
     return stat;
   }
 usleep(50);
  }
  while(!Timeout(&Timer));
  fprintf(stderr, "TIMEOUT - llcapt STATUS IS 0x%02x\n", stat);
  return 0x01;
}

variable_types:
s
unsigned char

variable_types:
mask
unsigned char

variable_types:
usec
unsigned long

variable_types: struct TTimeId { struct timeval { long tv_sec; long tv_usec; } StartTime; long Delay; unsigned char Done; }

user_defined_types: struct TTimeId { struct timeval StartTime; long Delay; unsigned char Done; }


CheckStatus

decompiled_code:
__int64 __fastcall CheckStatus(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3, __int64 a4)
{
int v4; // eax

v4 = WaitForStatus(a1, a2, a3, a4);
LOBYTE(v4) = (_BYTE)v4 == 1;
return (unsigned int)(5 * v4);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned __int8 a2, unsigned __int8 a3, __int64 a4)
{
int v4; // eax

v4 = func0(a1, a2, a3, a4);
LOBYTE(v4) = (_BYTE)v4 == 1;
return (unsigned int)(5 * v4);
}

original_code:
BYTE CheckStatus(int Device, const BYTE s, const BYTE mask, const ULONG usec)
{
  if(WaitForStatus(Device, s, mask, usec) == 0x01){
    return 0x05;
 }
  return 0x00;
}

canonical_original_code:
unsigned char CheckStatus(int Device, unsigned char s, unsigned char mask, unsigned long usec)
{
  if(WaitForStatus(Device, s, mask, usec) == 0x01){
    return 0x05;
 }
  return 0x00;
}

variable_types: Device int


PrintBuffer

decompiled_code:
size_t PrintBuffer()
{
__int64 v0; // rbx

v0 = 0LL;
fwrite("RETBUF = [", 1uLL, 0xAuLL, stderr);
while ( 1 )
{
if ( !v0 )
{
fprintf(stderr, "%02x", (unsigned __int8)RETBUF[0]);
goto LABEL_3;
}
fwrite(", ", 1uLL, 2uLL, stderr);
fprintf(stderr, "%02x", (unsigned __int8)RETBUF[v0]);
if ( v0 == 19 )
return fwrite("]\n", 1uLL, 2uLL, stderr);
LABEL_3:
++v0;
}
}

canonical_decompiled_code:
size_t func2()
{
__int64 v0; // rbx

v0 = 0LL;
fwrite("RETBUF = [", 1uLL, 0xAuLL, stderr);
while ( 1 )
{
if ( !v0 )
{
fprintf(stderr, "%02x", (unsigned __int8)RETBUF[0]);
goto LABEL_3;
}
fwrite(", ", 1uLL, 2uLL, stderr);
fprintf(stderr, "%02x", (unsigned __int8)RETBUF[v0]);
if ( v0 == 19 )
return fwrite("]\n", 1uLL, 2uLL, stderr);
LABEL_3:
++v0;
}
}

original_code:
void PrintBuffer(void)
{
  int i;
  fprintf(stderr, "RETBUF = [");
  for(i = 0 ; i < 20 ; i++)
  {
    if(i > 0)
      fprintf(stderr, ", ");
    fprintf(stderr, "%02x", RETBUF[i]);
  }
  fprintf(stderr, "]\n");
}

canonical_original_code:
void PrintBuffer()
{
  int i;
  fprintf(stderr, "RETBUF = [");
  for(i = 0 ; i < 20 ; i++)
  {
    if(i > 0)
      fprintf(stderr, ", ");
    fprintf(stderr, "%02x", RETBUF[i]);
  }
  fprintf(stderr, "]\n");
}

variable_types: i int


SendPacket

decompiled_code:
__int64 __fastcall SendPacket(__int64 a1, unsigned __int8 *a2, char a3, unsigned __int8 a4)
{
unsigned __int8 v5; // r14
unsigned __int8 *v7; // rbx
__int64 v8; // r12
int v9; // er12
__int64 v10; // rbx
char v11; // al
bool v12; // zf
char v13; // al

RETCNT = 0;
if ( !a3 )
return 0LL;
v5 = a4;
v7 = a2;
v8 = (__int64)&a2[(unsigned __int8)(a3 - 1) + 1];
Parport_WriteControl(a1, 6LL);
while ( 1 )
{
Parport_WriteData((unsigned int)a1, *v7);
usleep(0x2710u);
Parport_WriteControl((unsigned int)a1, 4LL);
usleep(0x2710u);
Parport_WriteControl((unsigned int)a1, 5LL);
if ( (unsigned __int8)WaitForStatus(a1, 0x78u, 0xFFu, 10000000LL) == 1 )
break;
Parport_WriteControl((unsigned int)a1, 4LL);
if ( (unsigned __int8)WaitForStatus(a1, 0xF0u, 0xFFu, 10000000LL) == 1 )
break;
if ( ++v7 == (unsigned __int8 *)v8 )
{
if ( !a4 )
return 0LL;
v9 = 0;
Parport_WriteControl((unsigned int)a1, 38LL);
usleep(0x2710u);
while ( 1 )
{
Parport_WriteControl((unsigned int)a1, 34LL);
if ( (unsigned __int8)WaitForStatus(a1, 0x10u, 0xFFu, 10000000LL) == 1 )
break;
v10 = (unsigned __int8)RETCNT++;
v11 = Parport_ReadData((unsigned int)a1);
v12 = RETCNT == 4;
RETBUF[v10] = v11;
if ( v12 )
v5 = RETBUF[2];
Parport_WriteControl((unsigned int)a1, 32LL);
v13 = WaitForStatus(a1, 0x50u, 0xF7u, 10000000LL);
if ( v13 == 1 )
break;
if ( v13 != 88 && v5 > (unsigned __int8)++v9 )
continue;
Parport_WriteControl((unsigned int)a1, 34LL);
if ( (unsigned __int8)WaitForStatus(a1, 0x58u, 0xFFu, 10000000LL) != 1 )
{
Parport_WriteControl((unsigned int)a1, 38LL);
if ( (unsigned __int8)WaitForStatus(a1, 0xF8u, 0xFFu, 10000000LL) != 1 )
{
Parport_WriteControl((unsigned int)a1, 6LL);
return 0LL;
}
}
return 5LL;
}
return 5LL;
}
}
return 5LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int8 *a2, char a3, unsigned __int8 a4)
{
unsigned __int8 v5; // r14
unsigned __int8 *v7; // rbx
__int64 v8; // r12
int v9; // er12
__int64 v10; // rbx
char v11; // al
bool v12; // zf
char v13; // al

RETCNT = 0;
if ( !a3 )
return 0LL;
v5 = a4;
v7 = a2;
v8 = (__int64)&a2[(unsigned __int8)(a3 - 1) + 1];
Parport_WriteControl(a1, 6LL);
while ( 1 )
{
Parport_WriteData((unsigned int)a1, *v7);
usleep(0x2710u);
Parport_WriteControl((unsigned int)a1, 4LL);
usleep(0x2710u);
Parport_WriteControl((unsigned int)a1, 5LL);
if ( (unsigned __int8)func0(a1, 0x78u, 0xFFu, 10000000LL) == 1 )
break;
Parport_WriteControl((unsigned int)a1, 4LL);
if ( (unsigned __int8)func0(a1, 0xF0u, 0xFFu, 10000000LL) == 1 )
break;
if ( ++v7 == (unsigned __int8 *)v8 )
{
if ( !a4 )
return 0LL;
v9 = 0;
Parport_WriteControl((unsigned int)a1, 38LL);
usleep(0x2710u);
while ( 1 )
{
Parport_WriteControl((unsigned int)a1, 34LL);
if ( (unsigned __int8)func0(a1, 0x10u, 0xFFu, 10000000LL) == 1 )
break;
v10 = (unsigned __int8)RETCNT++;
v11 = Parport_ReadData((unsigned int)a1);
v12 = RETCNT == 4;
RETBUF[v10] = v11;
if ( v12 )
v5 = RETBUF[2];
Parport_WriteControl((unsigned int)a1, 32LL);
v13 = func0(a1, 0x50u, 0xF7u, 10000000LL);
if ( v13 == 1 )
break;
if ( v13 != 88 && v5 > (unsigned __int8)++v9 )
continue;
Parport_WriteControl((unsigned int)a1, 34LL);
if ( (unsigned __int8)func0(a1, 0x58u, 0xFFu, 10000000LL) != 1 )
{
Parport_WriteControl((unsigned int)a1, 38LL);
if ( (unsigned __int8)func0(a1, 0xF8u, 0xFFu, 10000000LL) != 1 )
{
Parport_WriteControl((unsigned int)a1, 6LL);
return 0LL;
}
}
return 5LL;
}
return 5LL;
}
}
return 5LL;
}

original_code:
BYTE SendPacket(int Device, BYTE const *buf, const BYTE cnt, BYTE MaxRead)
{
  BYTE i=0, stat=0;
  RETCNT = 0;
  if(cnt == 0)
    return 0;
  Parport_WriteControl(Device, 0x06);
  for(i = 0 ; i < cnt ; i++)
  {
    Parport_WriteData(Device, buf[i]);
    usleep(10000);
    Parport_WriteControl(Device, 0x04);
    usleep(10000);
    Parport_WriteControl(Device, 0x05);
    if(WaitForStatus(Device, 0x78, 0xff, 10000000L) == 0x01)
      return 0x05;
    Parport_WriteControl(Device, 0x04);
    if(WaitForStatus(Device, 0xf0, 0xff, 10000000L) == 0x01)
      return 0x05;
  }
  if(MaxRead == 0)
    return 0x00;
  Parport_WriteControl(Device, 0x26);
  usleep(10000);
  for(i = 0 ; i < MaxRead ; i++)
  {
    Parport_WriteControl(Device, 0x22);
    if((stat = WaitForStatus(Device, 0x10, 0xff, 10000000L) == 0x01))
      return 0x05;
    RETBUF[RETCNT++] = Parport_ReadData(Device);
    if(RETCNT == 4)
      MaxRead = RETBUF[2];
    Parport_WriteControl(Device, 0x20);
    stat = WaitForStatus(Device, 0x50, 0xf7, 10000000L);
    if(stat == 0x01)
     return 0x05;
    if(stat == 0x58)
      break;
  }
  Parport_WriteControl(Device, 0x22);
  if(WaitForStatus(Device, 0x58, 0xff, 10000000L) == 0x01)
    return 0x05;
  Parport_WriteControl(Device, 0x26);
  if(WaitForStatus(Device, 0xf8, 0xff, 10000000L) == 0x01)
    return 0x05;
  Parport_WriteControl(Device, 0x06);
  return 0x00;
}

canonical_original_code:
unsigned char SendPacket(int Device, unsigned char *buf, unsigned char cnt, unsigned char MaxRead)
{
  unsigned char i = 0; unsigned char stat = 0;
  RETCNT = 0;
  if(cnt == 0)
    return 0;
  Parport_WriteControl(Device, 0x06);
  for(i = 0 ; i < cnt ; i++)
  {
    Parport_WriteData(Device, buf[i]);
    usleep(10000);
    Parport_WriteControl(Device, 0x04);
    usleep(10000);
    Parport_WriteControl(Device, 0x05);
    if(WaitForStatus(Device, 0x78, 0xff, 10000000L) == 0x01)
      return 0x05;
    Parport_WriteControl(Device, 0x04);
    if(WaitForStatus(Device, 0xf0, 0xff, 10000000L) == 0x01)
      return 0x05;
  }
  if(MaxRead == 0)
    return 0x00;
  Parport_WriteControl(Device, 0x26);
  usleep(10000);
  for(i = 0 ; i < MaxRead ; i++)
  {
    Parport_WriteControl(Device, 0x22);
    if((stat = WaitForStatus(Device, 0x10, 0xff, 10000000L) == 0x01))
      return 0x05;
    RETBUF[RETCNT++] = Parport_ReadData(Device);
    if(RETCNT == 4)
      MaxRead = RETBUF[2];
    Parport_WriteControl(Device, 0x20);
    stat = WaitForStatus(Device, 0x50, 0xf7, 10000000L);
    if(stat == 0x01)
     return 0x05;
    if(stat == 0x58)
      break;
  }
  Parport_WriteControl(Device, 0x22);
  if(WaitForStatus(Device, 0x58, 0xff, 10000000L) == 0x01)
    return 0x05;
  Parport_WriteControl(Device, 0x26);
  if(WaitForStatus(Device, 0xf8, 0xff, 10000000L) == 0x01)
    return 0x05;
  Parport_WriteControl(Device, 0x06);
  return 0x00;
}

variable_types:
buf
unsigned char *

variable_types:
cnt
unsigned char

variable_types:
MaxRead
unsigned char

variable_types:
i
unsigned char


CheckForPaperBeforeSendNewPageToPrinter

decompiled_code:
__int64 __fastcall CheckForPaperBeforeSendNewPageToPrinter(unsigned int a1)
{
int v1; // ebp
char v2; // bl
unsigned int v3; // er12

v1 = 120;
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter Can we print next page?\n", 1uLL, 0x47uLL, stderr);
PrintBuffer();
while ( 1 )
{
if ( RETBUF[0] == -95 && RETBUF[1] == -96 && RETBUF[16] == -3 )
{
v3 = 0;
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter First page is ready. p50 \n", 1uLL, 0x49uLL, stderr);
goto LABEL_9;
}
v2 = Send_PaperPacket(a1);
sleep(1u);
if ( !--v1 )
break;
PrintBuffer();
if ( v2 )
{
v3 = 5;
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter Check printer on/off \n", 1uLL, 0x45uLL, stderr);
goto LABEL_9;
}
}
v3 = 5;
LABEL_9:
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter page printed. p 90 \n", 1uLL, 0x43uLL, stderr);
PrintBuffer();
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
int v1; // ebp
char v2; // bl
unsigned int v3; // er12

v1 = 120;
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter Can we print next page?\n", 1uLL, 0x47uLL, stderr);
func2();
while ( 1 )
{
if ( RETBUF[0] == -95 && RETBUF[1] == -96 && RETBUF[16] == -3 )
{
v3 = 0;
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter First page is ready. p50 \n", 1uLL, 0x49uLL, stderr);
goto LABEL_9;
}
v2 = Send_PaperPacket(a1);
sleep(1u);
if ( !--v1 )
break;
func2();
if ( v2 )
{
v3 = 5;
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter Check printer on/off \n", 1uLL, 0x45uLL, stderr);
goto LABEL_9;
}
}
v3 = 5;
LABEL_9:
fwrite("llcapt:CheckForPaperBeforeSendNewPageToPrinter page printed. p 90 \n", 1uLL, 0x43uLL, stderr);
func2();
return v3;
}

original_code:
BYTE CheckForPaperBeforeSendNewPageToPrinter(int a_dDevice){
  BYTE dPaperStatus=0x05;
  BYTE dPortStatus=0x00;
  int dTimeIndex=0;
  fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter Can we print next page?\n");
  for (dTimeIndex=0; dTimeIndex< 120 ;dTimeIndex++){
      PrintBuffer();
      if (dPortStatus!= 0x00){
 dPaperStatus=0x05;
        fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter Check printer on/off \n");
 break;
      }
      if ((RETBUF[0]==0xA1)&&(RETBUF[1]==0xA0)&& (RETBUF[16]==0xFD)) {
  dPaperStatus=0x00;
         fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter First page is ready. p50 \n");
  break;
      }
      dPortStatus=Send_PaperPacket(a_dDevice);
      sleep(1);
  }
  fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter page printed. p 90 \n");
   PrintBuffer();
  return dPaperStatus;
}

canonical_original_code:
unsigned char CheckForPaperBeforeSendNewPageToPrinter(int a_dDevice){
  unsigned char dPaperStatus = 0x05;
  unsigned char dPortStatus = 0x00;
  int dTimeIndex = 0;
  fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter Can we print next page?\n");
  for (dTimeIndex=0; dTimeIndex< 120 ;dTimeIndex++){
      PrintBuffer();
      if (dPortStatus!= 0x00){
 dPaperStatus=0x05;
        fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter Check printer on/off \n");
 break;
      }
      if ((RETBUF[0]==0xA1)&&(RETBUF[1]==0xA0)&& (RETBUF[16]==0xFD)) {
  dPaperStatus=0x00;
         fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter First page is ready. p50 \n");
  break;
      }
      dPortStatus=Send_PaperPacket(a_dDevice);
      sleep(1);
  }
  fprintf(stderr,"llcapt:CheckForPaperBeforeSendNewPageToPrinter page printed. p 90 \n");
   PrintBuffer();
  return dPaperStatus;
}

variable_types:
dPaperStatus
unsigned char

variable_types:
dPortStatus
unsigned char

variable_types: dTimeIndex int


WaitCurrentPageIsPrintedBeforToSendNext

decompiled_code:
__int64 __fastcall WaitCurrentPageIsPrintedBeforToSendNext(unsigned int a1)
{
int v1; // ebx
unsigned int v2; // er12

v1 = 50;
fwrite(
"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Waiting for print end .............. \n",
1uLL,
0x55uLL,
stderr);
while ( 1 )
{
v2 = Send_Packet_a0_a0(a1);
PrintBuffer();
if ( (_BYTE)v2 )
break;
if ( RETBUF[0] == -96 && RETBUF[1] == -96 && RETBUF[12] && RETBUF[14] && RETBUF[16] )
{
fwrite("llcapt:WaitCurrentPageIsPrintedBeforToSendNext Send next page. p50 \n", 1uLL, 0x44uLL, stderr);
goto LABEL_10;
}
sleep(1u);
if ( !--v1 )
goto LABEL_10;
}
v2 = 5;
fwrite("llcapt:WaitCurrentPageIsPrintedBeforToSendNext Check printer on/off \n", 1uLL, 0x45uLL, stderr);
LABEL_10:
fwrite("llcapt:WaitCurrentPageIsPrintedBeforToSendNext Page is printed. \n", 1uLL, 0x41uLL, stderr);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
int v1; // ebx
unsigned int v2; // er12

v1 = 50;
fwrite(
"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Waiting for print end .............. \n",
1uLL,
0x55uLL,
stderr);
while ( 1 )
{
v2 = Send_Packet_a0_a0(a1);
func2();
if ( (_BYTE)v2 )
break;
if ( RETBUF[0] == -96 && RETBUF[1] == -96 && RETBUF[12] && RETBUF[14] && RETBUF[16] )
{
fwrite("llcapt:WaitCurrentPageIsPrintedBeforToSendNext Send next page. p50 \n", 1uLL, 0x44uLL, stderr);
goto LABEL_10;
}
sleep(1u);
if ( !--v1 )
goto LABEL_10;
}
v2 = 5;
fwrite("llcapt:WaitCurrentPageIsPrintedBeforToSendNext Check printer on/off \n", 1uLL, 0x45uLL, stderr);
LABEL_10:
fwrite("llcapt:WaitCurrentPageIsPrintedBeforToSendNext Page is printed. \n", 1uLL, 0x41uLL, stderr);
return v2;
}

original_code:
BYTE WaitCurrentPageIsPrintedBeforToSendNext(int a_dDevice){
  BYTE dPaperStatus=0x00;
  BYTE dPortStatus=0x00;
  int dTimeIndex=0;
  fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Waiting for print end .............. \n");
  for (dTimeIndex=0;dTimeIndex<50;dTimeIndex++){
      dPortStatus=Send_Packet_a0_a0(a_dDevice);
      PrintBuffer();
      if (dPortStatus!= 0x00){
 dPaperStatus=0x05;
        fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Check printer on/off \n");
 break;
      }
      if (RETBUF[0]==0xA0 && RETBUF[1]==0xA0 && RETBUF[12] && RETBUF[14]&& RETBUF[16]) {
  dPaperStatus=0x00;
         fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Send next page. p50 \n");
  break;
      }
      sleep(1);
  }
  fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Page is printed. \n");
 return dPaperStatus;
}

canonical_original_code:
unsigned char WaitCurrentPageIsPrintedBeforToSendNext(int a_dDevice){
  unsigned char dPaperStatus = 0x00;
  unsigned char dPortStatus = 0x00;
  int dTimeIndex = 0;
  fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Waiting for print end .............. \n");
  for (dTimeIndex=0;dTimeIndex<50;dTimeIndex++){
      dPortStatus=Send_Packet_a0_a0(a_dDevice);
      PrintBuffer();
      if (dPortStatus!= 0x00){
 dPaperStatus=0x05;
        fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Check printer on/off \n");
 break;
      }
      if (RETBUF[0]==0xA0 && RETBUF[1]==0xA0 && RETBUF[12] && RETBUF[14]&& RETBUF[16]) {
  dPaperStatus=0x00;
         fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Send next page. p50 \n");
  break;
      }
      sleep(1);
  }
  fprintf(stderr,"llcapt:WaitCurrentPageIsPrintedBeforToSendNext Page is printed. \n");
 return dPaperStatus;
}


WaitForReady

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

while ( !(unsigned __int8)Send_InterBandPacket(a1) )
{
if ( (RETBUF[5] & 1) != 0 )
{
if ( (unsigned __int8)Send_Packet_a0_a0(a1) )
return 5LL;
}
else
{
result = (unsigned __int8)RETBUF[4];
if ( !(_BYTE)result )
return result;
}
}
return 5LL;
}

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

while ( !(unsigned __int8)Send_InterBandPacket(a1) )
{
if ( (RETBUF[5] & 1) != 0 )
{
if ( (unsigned __int8)Send_Packet_a0_a0(a1) )
return 5LL;
}
else
{
result = (unsigned __int8)RETBUF[4];
if ( !(_BYTE)result )
return result;
}
}
return 5LL;
}

original_code:
BYTE WaitForReady(int Device)
{
  BYTE Status=0;
  while(1)
  {
    Status = Send_InterBandPacket(Device);
    if(Status != 0x00)
      return 0x05;
    if((RETBUF[5] & 0x01) != 0)
    {
      if(Send_Packet_a0_a0(Device) != 0x00)
        return 0x05;
    }
    else if(RETBUF[4] == 0x00)
      return 0x00;
  }
}

canonical_original_code:
unsigned char WaitForReady(int Device)
{
  unsigned char Status = 0;
  while(1)
  {
    Status = Send_InterBandPacket(Device);
    if(Status != 0x00)
      return 0x05;
    if((RETBUF[5] & 0x01) != 0)
    {
      if(Send_Packet_a0_a0(Device) != 0x00)
        return 0x05;
    }
    else if(RETBUF[4] == 0x00)
      return 0x00;
  }
}

variable_types:
Status
unsigned char


WaitForPageReady

decompiled_code:
__int64 __fastcall WaitForPageReady(__int64 a1)
{
unsigned __int8 v1; // al
__int64 result; // rax

v1 = Parport_ReadStatus(a1);
fprintf(stderr, "llcapt:WaitForPageReady 10 PortStatus %04X\n", v1);
while ( !(unsigned __int8)Send_InterBandPacket((unsigned int)a1) )
{
if ( (RETBUF[5] & 1) != 0 )
{
if ( (unsigned __int8)Send_Packet_a0_a0((unsigned int)a1) )
return 5LL;
result = (unsigned __int8)RETBUF[4];
if ( !(_BYTE)result )
return result;
}
}
return 5LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
unsigned __int8 v1; // al
__int64 result; // rax

v1 = Parport_ReadStatus(a1);
fprintf(stderr, "llcapt:WaitForPageReady 10 PortStatus %04X\n", v1);
while ( !(unsigned __int8)Send_InterBandPacket((unsigned int)a1) )
{
if ( (RETBUF[5] & 1) != 0 )
{
if ( (unsigned __int8)Send_Packet_a0_a0((unsigned int)a1) )
return 5LL;
result = (unsigned __int8)RETBUF[4];
if ( !(_BYTE)result )
return result;
}
}
return 5LL;
}

original_code:
BYTE WaitForPageReady(int Device)
{
  BYTE Status=0;
  fprintf(stderr, "llcapt:WaitForPageReady 10 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  while(1)
  {
    Status = Send_InterBandPacket(Device);
    if(Status != 0x00)
      return 0x05;
    if((RETBUF[5] & 0x01) != 0)
    {
      if(Send_Packet_a0_a0(Device) != 0x00)
        return 0x05;
      else if(RETBUF[4] == 0x00)
        return 0x00;
    }
  }
}

canonical_original_code:
unsigned char WaitForPageReady(int Device)
{
  unsigned char Status = 0;
  fprintf(stderr, "llcapt:WaitForPageReady 10 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
  while(1)
  {
    Status = Send_InterBandPacket(Device);
    if(Status != 0x00)
      return 0x05;
    if((RETBUF[5] & 0x01) != 0)
    {
      if(Send_Packet_a0_a0(Device) != 0x00)
        return 0x05;
      else if(RETBUF[4] == 0x00)
        return 0x00;
    }
  }
}


StartData

decompiled_code:
__int64 __fastcall StartData(unsigned int a1, unsigned int a2)
{
if ( (unsigned __int8)WaitForReady(a1) || (unsigned __int8)Send_StartDataPacket(a1, a2) )
return 5LL;
Parport_WriteData(a1, 67LL);
Parport_WriteControl(a1, 5LL);
return CheckStatus(a1, 0x78u, 0xFFu, 10000000LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, unsigned int a2)
{
if ( (unsigned __int8)func6(a1) || (unsigned __int8)Send_StartDataPacket(a1, a2) )
return 5LL;
Parport_WriteData(a1, 67LL);
Parport_WriteControl(a1, 5LL);
return func1(a1, 0x78u, 0xFFu, 10000000LL);
}

original_code:
BYTE StartData(int Device, UINT cnt)
{
  if(WaitForReady(Device) != 0x00)
    return 0x05;
  if(Send_StartDataPacket(Device, cnt) != 0x00)
    return 0x05;
  Parport_WriteData(Device, 0x43);
  Parport_WriteControl(Device, 0x05);
  return CheckStatus(Device, 0x78, 0xff, 10000000L);
}

canonical_original_code:
unsigned char StartData(int Device, unsigned int cnt)
{
  if(WaitForReady(Device) != 0x00)
    return 0x05;
  if(Send_StartDataPacket(Device, cnt) != 0x00)
    return 0x05;
  Parport_WriteData(Device, 0x43);
  Parport_WriteControl(Device, 0x05);
  return CheckStatus(Device, 0x78, 0xff, 10000000L);
}

variable_types:
cnt
unsigned int


SendData

decompiled_code:
__int64 __fastcall SendData(unsigned int a1, __int64 a2, unsigned int a3, unsigned __int8 a4, int a5)
{
__int64 result; // rax
int v6; // ebp
__int64 v7; // r13
unsigned __int8 v8; // r14
int v9; // er15
unsigned __int8 v10; // [rsp+7h] [rbp-41h]
__int64 v11; // [rsp+8h] [rbp-40h]

result = 0xFFFFFFFFLL;
v6 = 1;
v10 = a4;
if ( a5 )
v6 = a5;
if ( a3 )
{
v7 = a2;
v8 = a4;
v11 = a2 + a3;
do
{
++v7;
v9 = 0;
v8 ^= *(_BYTE *)(v7 - 1) ^ v10;
do
{
++v9;
Parport_WriteData(a1, v8);
}
while ( v6 != v9 );
}
while ( v7 != v11 );
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, __int64 a2, unsigned int a3, unsigned __int8 a4, int a5)
{
__int64 result; // rax
int v6; // ebp
__int64 v7; // r13
unsigned __int8 v8; // r14
int v9; // er15
unsigned __int8 v10; // [rsp+7h] [rbp-41h]
__int64 v11; // [rsp+8h] [rbp-40h]

result = 0xFFFFFFFFLL;
v6 = 1;
v10 = a4;
if ( a5 )
v6 = a5;
if ( a3 )
{
v7 = a2;
v8 = a4;
v11 = a2 + a3;
do
{
++v7;
v9 = 0;
v8 ^= *(_BYTE *)(v7 - 1) ^ v10;
do
{
++v9;
Parport_WriteData(a1, v8);
}
while ( v6 != v9 );
}
while ( v7 != v11 );
result = 0LL;
}
return result;
}

original_code:
BOOL SendData(int Device, BYTE const *buf, UINT cnt, BYTE Seed, UINT repWrite)
{
  BYTE LastVal;
  UINT i;
  if(repWrite == 0)
    repWrite = 1;
  if(cnt == 0)
    return -1;
  LastVal = Seed;
  while(cnt > 0)
  {
   LastVal ^= Seed;
   LastVal ^= *buf++;
   for(i = 0 ; i < repWrite ; i++){
       Parport_WriteData(Device, LastVal);
 }
    cnt--;
  }
  return 0x00;
}

canonical_original_code:
unsigned char SendData(int Device, unsigned char *buf, unsigned int cnt, unsigned char Seed, unsigned int repWrite)
{
  unsigned char LastVal;
  unsigned int i;
  if(repWrite == 0)
    repWrite = 1;
  if(cnt == 0)
    return -1;
  LastVal = Seed;
  while(cnt > 0)
  {
   LastVal ^= Seed;
   LastVal ^= *buf++;
   for(i = 0 ; i < repWrite ; i++){
       Parport_WriteData(Device, LastVal);
 }
    cnt--;
  }
  return 0x00;
}

variable_types:
Seed
unsigned char

variable_types:
repWrite
unsigned int

variable_types:
LastVal
unsigned char

variable_types:
i
unsigned int


EndData

decompiled_code:
__int64 __fastcall EndData(__int64 a1)
{
__int64 result; // rax
unsigned __int8 v2; // al

Parport_WriteControl(a1, 4LL);
result = CheckStatus(a1, 0xF0u, 0xFFu, 10000000LL);
if ( (_BYTE)result )
{
v2 = Parport_ReadStatus((unsigned int)a1);
fprintf(stderr, "llcapt:EndData 50 PortStatus %04X\n", v2);
result = 5LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 result; // rax
unsigned __int8 v2; // al

Parport_WriteControl(a1, 4LL);
result = func1(a1, 0xF0u, 0xFFu, 10000000LL);
if ( (_BYTE)result )
{
v2 = Parport_ReadStatus((unsigned int)a1);
fprintf(stderr, "llcapt:EndData 50 PortStatus %04X\n", v2);
result = 5LL;
}
return result;
}

original_code:
BYTE EndData(int Device)
{
  Parport_WriteControl(Device, 0x04);
  if(CheckStatus(Device, 0xf0, 0xff, 10000000L) != 0x00){
      fprintf(stderr, "llcapt:EndData 50 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    return 0x05;
    }
  return 0x00;
}

canonical_original_code:
unsigned char EndData(int Device)
{
  Parport_WriteControl(Device, 0x04);
  if(CheckStatus(Device, 0xf0, 0xff, 10000000L) != 0x00){
      fprintf(stderr, "llcapt:EndData 50 PortStatus %04X\n",(int)Parport_ReadStatus(Device));
    return 0x05;
    }
  return 0x00;
}


decompiled_code:
int Port_Sleep()
{
return usleep(0x2710u);
}

canonical_decompiled_code:
int func11()
{
return usleep(0x2710u);
}

original_code:
void Port_Sleep(void){
     usleep(10000);
}

canonical_original_code:
void Port_Sleep(){
     usleep(10000);
}


decompiled_code:
__int64 __fastcall Send_StartDataPacket(__int64 a1, __int16 a2)
{
StartDataPacket[1] = a2 + 4;
return SendPacket(a1, StartDataPacket, 4LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int16 a2)
{
StartDataPacket[1] = a2 + 4;
return SendPacket(a1, StartDataPacket, 4LL, 0LL);
}

original_code:
inline BYTE Send_StartDataPacket(int Device, UINT DataSize)
{
  DataSize += sizeof(StartDataPacket);
  StartDataPacket[2] = DataSize & 0xFF;
  StartDataPacket[3] = (DataSize & 0xFF00) >> 8;
  return SendPacket(Device, StartDataPacket, sizeof(StartDataPacket), 0);
}

canonical_original_code:
inline unsigned char Send_StartDataPacket(int Device, unsigned int DataSize)
{
  DataSize += sizeof(StartDataPacket);
  StartDataPacket[2] = DataSize & 0xFF;
  StartDataPacket[3] = (DataSize & 0xFF00) >> 8;
  return SendPacket(Device, StartDataPacket, sizeof(StartDataPacket), 0);
}

variable_types:
DataSize
unsigned int


decompiled_code:
__int64 __fastcall Send_InterBandPacket(__int64 a1)
{
return SendPacket(a1, &InterBandPacket, 4LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return SendPacket(a1, &InterBandPacket, 4LL, 255LL);
}

original_code:
inline BYTE Send_InterBandPacket(int Device)
{
  return SendPacket(Device, InterBandPacket, sizeof(InterBandPacket), 255);
}

canonical_original_code:
inline unsigned char Send_InterBandPacket(int Device)
{
  return SendPacket(Device, InterBandPacket, sizeof(InterBandPacket), 255);
}


decompiled_code:
__int64 __fastcall Send_Packet_a0_a0(__int64 a1)
{
return SendPacket(a1, &Packet_a0_a0, 4LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return SendPacket(a1, &Packet_a0_a0, 4LL, 255LL);
}

original_code:
inline BYTE Send_Packet_a0_a0(int Device)
{
  return SendPacket(Device, Packet_a0_a0, sizeof(Packet_a0_a0), 255);
}

canonical_original_code:
inline unsigned char Send_Packet_a0_a0(int Device)
{
  return SendPacket(Device, Packet_a0_a0, sizeof(Packet_a0_a0), 255);
}


decompiled_code:
__int64 __fastcall Send_Packet_a0_a2(__int64 a1)
{
return SendPacket(a1, &Packet_a0_a2, 4LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return SendPacket(a1, &Packet_a0_a2, 4LL, 255LL);
}

original_code:
inline BYTE Send_Packet_a0_a2(int Device)
{
  return SendPacket(Device, Packet_a0_a2, sizeof(Packet_a0_a2), 255);
}

canonical_original_code:
inline unsigned char Send_Packet_a0_a2(int Device)
{
  return SendPacket(Device, Packet_a0_a2, sizeof(Packet_a0_a2), 255);
}


decompiled_code:
__int64 __fastcall Send_PaperPacket(__int64 a1)
{
return SendPacket(a1, &PaperPacket, 4LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return SendPacket(a1, &PaperPacket, 4LL, 255LL);
}

original_code:
inline BYTE Send_PaperPacket(int Device)
{
  return SendPacket(Device, PaperPacket, sizeof(PaperPacket), 255);
}

canonical_original_code:
inline unsigned char Send_PaperPacket(int Device)
{
  return SendPacket(Device, PaperPacket, sizeof(PaperPacket), 255);
}


decompiled_code:
__int64 __fastcall Send_Packet_a1_a1(__int64 a1)
{
return SendPacket(a1, &Packet_a1_a1, 4LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return SendPacket(a1, &Packet_a1_a1, 4LL, 255LL);
}

original_code:
inline BYTE Send_Packet_a1_a1(int Device)
{
  return SendPacket(Device, Packet_a1_a1, sizeof(Packet_a1_a1), 255);
}

canonical_original_code:
inline unsigned char Send_Packet_a1_a1(int Device)
{
  return SendPacket(Device, Packet_a1_a1, sizeof(Packet_a1_a1), 255);
}


decompiled_code:
__int64 __fastcall Send_Packet_a1_d0(__int64 a1)
{
return SendPacket(a1, &Packet_a1_d0, 4LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return SendPacket(a1, &Packet_a1_d0, 4LL, 0LL);
}

original_code:
inline BYTE Send_Packet_a1_d0(int Device)
{
  return SendPacket(Device, Packet_a1_d0, sizeof(Packet_a1_d0), 0);
}

canonical_original_code:
inline unsigned char Send_Packet_a1_d0(int Device)
{
  return SendPacket(Device, Packet_a1_d0, sizeof(Packet_a1_d0), 0);
}


decompiled_code:
__int64 __fastcall Send_Packet_a4_e0(__int64 a1)
{
return SendPacket(a1, &Packet_a4_e0, 4LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return SendPacket(a1, &Packet_a4_e0, 4LL, 255LL);
}

original_code:
inline BYTE Send_Packet_a4_e0(int Device)
{
  return SendPacket(Device, Packet_a4_e0, sizeof(Packet_a4_e0), 255);
}

canonical_original_code:
inline unsigned char Send_Packet_a4_e0(int Device)
{
  return SendPacket(Device, Packet_a4_e0, sizeof(Packet_a4_e0), 255);
}


decompiled_code:
__int64 __fastcall Send_Packet_a0_d0(__int64 a1)
{
return SendPacket(a1, &Packet_a0_d0, 38LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return SendPacket(a1, &Packet_a0_d0, 38LL, 0LL);
}

original_code:
inline BYTE Send_Packet_a0_d0(int Device)
{
  return SendPacket(Device, Packet_a0_d0, sizeof(Packet_a0_d0), 0);
}

canonical_original_code:
inline unsigned char Send_Packet_a0_d0(int Device)
{
  return SendPacket(Device, Packet_a0_d0, sizeof(Packet_a0_d0), 0);
}


decompiled_code:
__int64 __fastcall Send_Packet_a5_e0(__int64 a1)
{
return SendPacket(a1, &Packet_a5_e0, 12LL, 255LL);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return SendPacket(a1, &Packet_a5_e0, 12LL, 255LL);
}

original_code:
inline BYTE Send_Packet_a5_e0(int Device)
{
  return SendPacket(Device, Packet_a5_e0, sizeof(Packet_a5_e0), 255);
}

canonical_original_code:
inline unsigned char Send_Packet_a5_e0(int Device)
{
  return SendPacket(Device, Packet_a5_e0, sizeof(Packet_a5_e0), 255);
}


StartTimer

decompiled_code:
int __fastcall StartTimer(__int64 a1, __int64 a2)
{
int result; // eax

result = gettimeofday((struct timeval *)a1, 0LL);
*(_QWORD *)(a1 + 16) = a2;
*(_BYTE *)(a1 + 24) = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
int result; // eax

result = gettimeofday((struct timeval *)a1, 0LL);
*(_QWORD *)(a1 + 16) = a2;
*(_BYTE *)(a1 + 24) = 0;
return result;
}

original_code:
void StartTimer(TTimeId *ID, long uSec)
{
  gettimeofday(&ID->StartTime, ((void *)0));
  ID->Delay = uSec;
  ID->Done = 0;
}

canonical_original_code:
void StartTimer(struct TTimeId *ID, long uSec)
{
  gettimeofday(&ID->StartTime, ((void *)0));
  ID->Delay = uSec;
  ID->Done = 0;
}

variable_types: struct TTimeId { struct timeval { long tv_sec; long tv_usec; } StartTime; long Delay; unsigned char Done; } *

variable_types: uSec long


Timeout

decompiled_code:
__int64 __fastcall Timeout(__int64 a1)
{
unsigned int v1; // er12
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

v1 = *(unsigned __int8 *)(a1 + 24);
if ( (_BYTE)v1 )
return 1;
gettimeofday(&v3, 0LL);
if ( *(_QWORD *)(a1 + 16) > v3.tv_usec + 1000000 * (v3.tv_sec - *(_QWORD *)a1) - *(_QWORD *)(a1 + 8) )
return v1;
*(_BYTE *)(a1 + 24) = 1;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned int v1; // er12
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

v1 = *(unsigned __int8 *)(a1 + 24);
if ( (_BYTE)v1 )
return 1;
gettimeofday(&v3, 0LL);
if ( *(_QWORD *)(a1 + 16) > v3.tv_usec + 1000000 * (v3.tv_sec - *(_QWORD *)a1) - *(_QWORD *)(a1 + 8) )
return v1;
*(_BYTE *)(a1 + 24) = 1;
return 1LL;
}

original_code:
BOOL Timeout(TTimeId *ID)
{
  struct timeval t;
  long diff;
  if(ID->Done)
    return 1;
  gettimeofday(&t, ((void *)0));
  diff = t.tv_sec - ID->StartTime.tv_sec;
  diff = diff * 1000000L + t.tv_usec - ID->StartTime.tv_usec;
  if(diff >= ID->Delay)
  {
   ID->Done = 1;
   return 1;
  }
  return 0;
}

canonical_original_code:
unsigned char Timeout(struct TTimeId *ID)
{
  struct timeval t;
  long diff;
  if(ID->Done)
    return 1;
  gettimeofday(&t, ((void *)0));
  diff = t.tv_sec - ID->StartTime.tv_sec;
  diff = diff * 1000000L + t.tv_usec - ID->StartTime.tv_usec;
  if(diff >= ID->Delay)
  {
   ID->Done = 1;
   return 1;
  }
  return 0;
}

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

variable_types: diff long


decompiled_code:
bool __fastcall HasLBP800(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebp
char v3; // bl
bool result; // al

v1 = Parport_Open();
if ( v1 == -1 )
{
fprintf(stderr, "parcapt:HasLBP No printer found at %s\n", a1);
result = 0;
}
else
{
v2 = v1;
v3 = InitPrinter(v1);
Parport_Close(v2);
fprintf(stderr, "parcapt:HasLBP Printer found at %s\n", a1);
result = v3 == 0;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func0(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebp
char v3; // bl
bool result; // al

v1 = Parport_Open();
if ( v1 == -1 )
{
fprintf(stderr, "parcapt:HasLBP No printer found at %s\n", a1);
result = 0;
}
else
{
v2 = v1;
v3 = InitPrinter(v1);
Parport_Close(v2);
fprintf(stderr, "parcapt:HasLBP Printer found at %s\n", a1);
result = v3 == 0;
}
return result;
}

original_code:
BOOL HasLBP800(char *DeviceName)
{
  BYTE Status=0;
  int Device=0;
  Device = Parport_Open(DeviceName);
  if (Device == -1){
     fprintf(stderr,"parcapt:HasLBP No printer found at %s\n",DeviceName);
    return 0;
 }
  Status = InitPrinter(Device) ;
  Parport_Close(Device);
  fprintf(stderr,"parcapt:HasLBP Printer found at %s\n",DeviceName);
  return (Status == 0x00);
}

canonical_original_code:
unsigned char HasLBP800(char *DeviceName)
{
  unsigned char Status = 0;
  int Device = 0;
  Device = Parport_Open(DeviceName);
  if (Device == -1){
     fprintf(stderr,"parcapt:HasLBP No printer found at %s\n",DeviceName);
    return 0;
 }
  Status = InitPrinter(Device) ;
  Parport_Close(Device);
  fprintf(stderr,"parcapt:HasLBP Printer found at %s\n",DeviceName);
  return (Status == 0x00);
}

variable_types:
DeviceName
char *

variable_types: Device int


getLastDifference

original_code:
int getLastDifference(TLineBuf LineBuf, BYTE CurLine)
{
  int iPos;
  BYTE *CurrPtr = LineBuf[CurLine] + 592;
  BYTE *PrevPtr = LineBuf[CurLine ? 0 : 1] + 592;
  iPos = 592;
  while( (*(--CurrPtr) == *(--PrevPtr)) && iPos)
    iPos--;
  return iPos;
}

canonical_original_code:
int getLastDifference(unsigned char LineBuf[2][592], unsigned char CurLine)
{
  int iPos;
  unsigned char *CurrPtr = LineBuf[CurLine] + 592;
  unsigned char *PrevPtr = LineBuf[CurLine ? 0 : 1] + 592;
  iPos = 592;
  while( (*(--CurrPtr) == *(--PrevPtr)) && iPos)
    iPos--;
  return iPos;
}

variable_types:
LineBuf
unsigned char [2][592]

variable_types:
CurLine
unsigned char

variable_types: iPos int

variable_types:
CurrPtr
unsigned char *

variable_types:
PrevPtr
unsigned char *


sign

repo: alesgaroth/mauve-db

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

result = (unsigned int)a1;
if ( a1 )
result = (a1 >> 31) | 1u;
return result;
}

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

result = (unsigned int)a1;
if ( a1 )
result = (a1 >> 31) | 1u;
return result;
}

variable_types: v int


decompiled_code:
__int64 __fastcall RowMatcher2(__int64 a1, const char *a2)
{
int v2; // eax

v2 = strcasecmp(*(const char **)(a1 + 8), a2);
return sign(v2);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
int v2; // eax

v2 = strcasecmp(*(const char **)(a1 + 8), a2);
return func0(v2);
}

original_code:
int RowMatcher2(Row r_, Closure c){
 struct row *r = (struct row*)r_;
 char *key = (char *)c;
 return sign(strcasecmp(r->name, key));
}

canonical_original_code:
int RowMatcher2(void *r_, void *c){
 struct row *r = (struct row*)r_;
 char *key = (char *)c;
 return sign(strcasecmp(r->name, key));
}

variable_types:
c
void *

variable_types: struct row { long size; char *name; char *value; } *

variable_types:
key
char *


reporter

decompiled_code:
void __fastcall __noreturn reporter(const char *a1, unsigned int a2, const char *a3)
{
fprintf(stderr, "%s:%d: %s\n", a1, a2, a3);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(const char *a1, unsigned int a2, const char *a3)
{
fprintf(stderr, "%s:%d: %s\n", a1, a2, a3);
exit(1);
}

original_code:
void reporter(const char *file, int line, const char *error){
 fprintf(stderr, "%s:%d: %s\n", file, line, error);
 exit(1);
}

canonical_original_code:
void reporter(char *file, int line, char *error){
 fprintf(stderr, "%s:%d: %s\n", file, line, error);
 exit(1);
}

variable_types:
file
char *

variable_types: line int

variable_types:
error
char *


doinsert

decompiled_code:
__int64 __fastcall doinsert(__int64 a1, __int64 a2, __int64 a3, const char *a4, unsigned int a5)
{
__int64 result; // rax
__int64 (__fastcall *v8)(__int64, __int64); // [rsp+0h] [rbp-28h] BYREF
_QWORD *v9; // [rsp+8h] [rbp-20h]

v8 = RowMatcher;
v9 = newRow(a2, a3);
result = tree_insert(a1, v9, &v8);
if ( !result )
reporter(a4, a5, "expected tree_insert; returned NULL;");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, const char *a4, unsigned int a5)
{
__int64 result; // rax
__int64 (__fastcall *v8)(__int64, __int64); // [rsp+0h] [rbp-28h] BYREF
_QWORD *v9; // [rsp+8h] [rbp-20h]

v8 = RowMatcher;
v9 = newRow(a2, a3);
result = tree_insert(a1, v9, &v8);
if ( !result )
func3(a4, a5, "expected tree_insert; returned NULL;");
return result;
}

original_code:
TreeNode doinsert(TreeNode tn, char *key, char *value, char *file, int line){
 Row r = newRow(key, value);
 struct lookUp lu = { RowMatcher, r};
 tn = tree_insert(tn, r, &lu);
 if (!tn) reporter(file, line, "expected tree_insert;  returned NULL;");
 return tn;
}

canonical_original_code:
void *doinsert(void *tn, char *key, char *value, char *file, int line){
 void *r = newRow(key, value);
 struct lookUp lu = { RowMatcher, r};
 tn = tree_insert(tn, r, &lu);
 if (!tn) reporter(file, line, "expected tree_insert;  returned NULL;");
 return tn;
}

variable_types:
tn
void *

variable_types:
value
char *

variable_types:
r
void *

variable_types: struct lookUp { int (*m)(void *datum, void *c); void *c; }

return_type: void *


dolookup

decompiled_code:
int __fastcall dolookup(__int64 a1, __int64 a2, const char *a3, const char *a4, unsigned int a5)
{
__int64 v8; // rax
int result; // eax
__int64 v10[5]; // [rsp+0h] [rbp-28h] BYREF

v10[1] = a2;
v10[0] = (__int64)RowMatcher2;
v8 = tree_findSingle(a1, v10);
if ( !v8 || (result = strcasecmp(*(const char **)(v8 + 16), a3)) != 0 )
reporter(a4, a5, "looked up by key failed");
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2, const char *a3, const char *a4, unsigned int a5)
{
__int64 v8; // rax
int result; // eax
__int64 v10[5]; // [rsp+0h] [rbp-28h] BYREF

v10[1] = a2;
v10[0] = (__int64)RowMatcher2;
v8 = tree_findSingle(a1, v10);
if ( !v8 || (result = strcasecmp(*(const char **)(v8 + 16), a3)) != 0 )
func3(a4, a5, "looked up by key failed");
return result;
}

original_code:
void dolookup(TreeNode tn, char *key, char *value, char *file, int line){
 struct lookUp lu = (struct lookUp){ RowMatcher2, key};
 Row v = tree_findSingle(tn, &lu);
 if (!v) reporter(file, line, "looked up by key failed");
 if (strcasecmp(((struct row*)v)->value, value)) reporter(file, line, "looked up by key failed");
}

canonical_original_code:
void dolookup(void *tn, char *key, char *value, char *file, int line){
 struct lookUp lu = (struct lookUp){ RowMatcher2, key};
 void *v = tree_findSingle(tn, &lu);
 if (!v) reporter(file, line, "looked up by key failed");
 if (strcasecmp(((struct row *)v)->value, value)) reporter(file, line, "looked up by key failed");
}

variable_types:
v
void *

user_defined_types: struct lookUp { int (*m)(void *datum, void *c); void *c; }

user_defined_types: struct row { long size; char *name; char *value; }


dolookupf

decompiled_code:
__int64 __fastcall dolookupf(__int64 a1, __int64 a2, const char *a3, unsigned int a4)
{
__int64 result; // rax
__int64 v7[5]; // [rsp+0h] [rbp-28h] BYREF

v7[1] = a2;
v7[0] = (__int64)RowMatcher2;
result = tree_findSingle(a1, v7);
if ( result )
reporter(a3, a4, "found key in the tree we should not have");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, const char *a3, unsigned int a4)
{
__int64 result; // rax
__int64 v7[5]; // [rsp+0h] [rbp-28h] BYREF

v7[1] = a2;
v7[0] = (__int64)RowMatcher2;
result = tree_findSingle(a1, v7);
if ( result )
func3(a3, a4, "found key in the tree we should not have");
return result;
}

original_code:
void dolookupf(TreeNode tn, char *key, char *file, int line){
 struct lookUp lu = (struct lookUp){ RowMatcher2, key};
 Row v = tree_findSingle(tn, &lu);
 if (v) reporter(file, line, "found key in the tree we should not have");
}

canonical_original_code:
void dolookupf(void *tn, char *key, char *file, int line){
 struct lookUp lu = (struct lookUp){ RowMatcher2, key};
 void *v = tree_findSingle(tn, &lu);
 if (v) reporter(file, line, "found key in the tree we should not have");
}


dodelete

decompiled_code:
__int64 __fastcall dodelete(__int64 a1, __int64 a2, const char *a3, unsigned int a4)
{
__int64 result; // rax
__int64 v7[5]; // [rsp+0h] [rbp-28h] BYREF

v7[1] = a2;
v7[0] = (__int64)RowMatcher2;
result = tree_deleteSingle(a1, v7);
if ( !result )
reporter(a3, a4, "expected tree_delete; returned NULL;");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, const char *a3, unsigned int a4)
{
__int64 result; // rax
__int64 v7[5]; // [rsp+0h] [rbp-28h] BYREF

v7[1] = a2;
v7[0] = (__int64)RowMatcher2;
result = tree_deleteSingle(a1, v7);
if ( !result )
func3(a3, a4, "expected tree_delete; returned NULL;");
return result;
}

original_code:
TreeNode dodelete(TreeNode tn, char *key, char *file, int line){
 struct lookUp lu = (struct lookUp){ RowMatcher2, key};
 tn = tree_deleteSingle(tn, &lu);
 if (!tn) reporter(file, line, "expected tree_delete;  returned NULL;");
 return tn;
}

canonical_original_code:
void *dodelete(void *tn, char *key, char *file, int line){
 struct lookUp lu = (struct lookUp){ RowMatcher2, key};
 tn = tree_deleteSingle(tn, &lu);
 if (!tn) reporter(file, line, "expected tree_delete;  returned NULL;");
 return tn;
}


printRow

decompiled_code:
int __fastcall printRow(__int64 a1)
{
return printf("%s: %s", *(const char **)(a1 + 8), *(const char **)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
return printf("%s: %s", *(const char **)(a1 + 8), *(const char **)(a1 + 16));
}

original_code:
void printRow(Row r_){
 struct row *r = (struct row*)r_;
 printf("%s: %s", r->name, r->value);
}

canonical_original_code:
void printRow(void *r_){
 struct row *r = (struct row*)r_;
 printf("%s: %s", r->name, r->value);
}


decompiled_code:
char *__fastcall heap_alloc(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // rax
char *v3; // r8
char *v4; // rdx
char *result; // rax
char *v6; // rcx
__int64 v7; // rax
__int64 v8; // rsi
char *v9; // rdx
char *v10; // r8

v1 = (a1 + 7) & 0xFFFFFFFFFFFFFFF8LL;
if ( !v1 )
_assert_fail("numbytes && \"heap_alloc called for nothing\"", "memman.c", 0xFu, "heap_alloc");
v2 = mem;
if ( mem )
{
v3 = *(char **)(mem + 8);
v4 = *(char **)mem;
}
else
{
mem = (__int64)mymem;
mymem[2] = 0x4000000LL;
mymem[0] = (__int64)sbrk(0x4000000LL);
v2 = mem;
v3 = *(char **)mem;
*(_QWORD *)(mem + 8) = *(_QWORD *)mem;
v4 = v3;
}
if ( &v3[v1] > &v4[*(_QWORD *)(v2 + 16)] )
{
v6 = (char *)sbrk(0x4000000LL);
v7 = mem;
v8 = *(_QWORD *)mem;
v9 = *(char **)(mem + 16);
v10 = *(char **)(mem + 8);
if ( v10 >= &v9[*(_QWORD *)mem] )
{
*(_QWORD *)(mem + 8) = v6;
v10 = v6;
v9 = &v6[-v8];
}
*(_QWORD *)(v7 + 16) = v9 + 0x4000000;
*(_QWORD *)(v7 + 8) = &v10[v1];
result = v10;
}
else
{
*(_QWORD *)(v2 + 8) = &v3[v1];
result = v3;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // rax
char *v3; // r8
char *v4; // rdx
char *result; // rax
char *v6; // rcx
__int64 v7; // rax
__int64 v8; // rsi
char *v9; // rdx
char *v10; // r8

v1 = (a1 + 7) & 0xFFFFFFFFFFFFFFF8LL;
if ( !v1 )
_assert_fail("numbytes && \"heap_alloc called for nothing\"", "memman.c", 0xFu, "heap_alloc");
v2 = mem;
if ( mem )
{
v3 = *(char **)(mem + 8);
v4 = *(char **)mem;
}
else
{
mem = (__int64)mymem;
mymem[2] = 0x4000000LL;
mymem[0] = (__int64)sbrk(0x4000000LL);
v2 = mem;
v3 = *(char **)mem;
*(_QWORD *)(mem + 8) = *(_QWORD *)mem;
v4 = v3;
}
if ( &v3[v1] > &v4[*(_QWORD *)(v2 + 16)] )
{
v6 = (char *)sbrk(0x4000000LL);
v7 = mem;
v8 = *(_QWORD *)mem;
v9 = *(char **)(mem + 16);
v10 = *(char **)(mem + 8);
if ( v10 >= &v9[*(_QWORD *)mem] )
{
*(_QWORD *)(mem + 8) = v6;
v10 = v6;
v9 = &v6[-v8];
}
*(_QWORD *)(v7 + 16) = v9 + 0x4000000;
*(_QWORD *)(v7 + 8) = &v10[v1];
result = v10;
}
else
{
*(_QWORD *)(v2 + 8) = &v3[v1];
result = v3;
}
return result;
}

original_code:
void *heap_alloc(size_t numbytes){
 numbytes = (numbytes+7)&(-8);
 ((numbytes && "heap_alloc called for nothing") ? (void) (0) : __assert_fail ("numbytes && \"heap_alloc called for nothing\"", "memman.c", 15, __extension__ __PRETTY_FUNCTION__));
 if (!mem){
  mem = &mymem;
  mem->sizeofmemory = 64 * 1024 *1024;
  mem->startofmem = sbrk(mem->sizeofmemory);
  mem->nexttoalloc = mem->startofmem;
 }
 if (mem->nexttoalloc + numbytes > mem->startofmem + mem->sizeofmemory){
  void *newmem = sbrk(64 * 1024 *1024);
  if (mem->nexttoalloc >= mem->sizeofmemory + mem->startofmem){
   mem->nexttoalloc = newmem;
   mem->sizeofmemory = mem->nexttoalloc - mem->startofmem;
  }
  mem->sizeofmemory += 64 * 1024 *1024;
 }
 void *rmem = mem->nexttoalloc;
 mem->nexttoalloc += numbytes;
 return rmem;
}

canonical_original_code:
void *heap_alloc(unsigned long numbytes){
 numbytes = (numbytes+7)&(-8);
 ((numbytes && "heap_alloc called for nothing") ? (void) (0) : __assert_fail ("numbytes && \"heap_alloc called for nothing\"", "memman.c", 15, __extension__ __PRETTY_FUNCTION__));
 if (!mem){
  mem = &mymem;
  mem->sizeofmemory = 64 * 1024 *1024;
  mem->startofmem = sbrk(mem->sizeofmemory);
  mem->nexttoalloc = mem->startofmem;
 }
 if (mem->nexttoalloc + numbytes > mem->startofmem + mem->sizeofmemory){
  void *newmem = sbrk(64 * 1024 *1024);
  if (mem->nexttoalloc >= mem->sizeofmemory + mem->startofmem){
   mem->nexttoalloc = newmem;
   mem->sizeofmemory = mem->nexttoalloc - mem->startofmem;
  }
  mem->sizeofmemory += 64 * 1024 *1024;
 }
 void *rmem = mem->nexttoalloc;
 mem->nexttoalloc += numbytes;
 return rmem;
}

variable_types:
numbytes
unsigned long

variable_types:
newmem
void *

variable_types:
rmem
void *


rdtsc

repo: alex-guoba/SHM-Ring-Buffer

decompiled_code:
unsigned __int64 rdtsc()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func0()
{
return __rdtsc();
}

original_code:
static inline uint64_t
rdtsc()
{
    unsigned int lo, hi;
    __asm__ __volatile__ (
            "rdtsc"
            : "=a" (lo), "=d" (hi)
            );
    return (uint64_t)hi << 32 | lo;
}

canonical_original_code:
static inline unsigned long rdtsc()
{
    unsigned int lo; unsigned int hi;
    __asm__ __volatile__ (
            "rdtsc"
            : "=a" (lo), "=d" (hi)
            );
    return (unsigned long)hi << 32 | lo;
}

variable_types:
lo
unsigned int

variable_types:
hi
unsigned int

return_type: unsigned long


CalcCpuFreq

decompiled_code:
unsigned __int64 CalcCpuFreq()
{
unsigned __int64 v0; // rbx

v0 = rdtsc();
Sleep_constprop_0_isra_0();
return 10 * (rdtsc() - v0);
}

canonical_decompiled_code:
unsigned __int64 func3()
{
unsigned __int64 v0; // rbx

v0 = func0();
func2();
return 10 * (func0() - v0);
}

original_code:
static uint64_t
CalcCpuFreq()
{
    uint64_t t1;
    uint64_t t2;
    t1 = rdtsc();
    Sleep(100);
    t2 = rdtsc();
    return (t2-t1)*10;
}

canonical_original_code:
static unsigned long CalcCpuFreq()
{
    unsigned long t1;
    unsigned long t2;
    t1 = rdtsc();
    Sleep(100);
    t2 = rdtsc();
    return (t2-t1)*10;
}


decompiled_code:
void __fastcall shmtx_init(__int64 a1, __int64 a2, int a3)
{
*(_QWORD *)a1 = a2;
*(_DWORD *)(a1 + 8) = a3;
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, int a3)
{
*(_QWORD *)a1 = a2;
*(_DWORD *)(a1 + 8) = a3;
}

original_code:
void shmtx_init(shmctx_t *mtx, shmtx_sh_t *addr, unsigned int spin) {
    mtx->lock = &addr->lock;
    mtx->spin = spin;
}

canonical_original_code:
void shmtx_init(struct shmctx_t *mtx, struct shmtx_sh_t *addr, unsigned int spin) {
    mtx->lock = &addr->lock;
    mtx->spin = spin;
}

variable_types: struct shmctx_t { unsigned long *lock; unsigned int spin; } *

variable_types: struct shmtx_sh_t { unsigned long lock; } *

variable_types:
spin
unsigned int


decompiled_code:
_BOOL8 __fastcall shmctx_trylock(volatile signed __int64 **a1, signed __int64 a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( !**a1 )
result = _InterlockedCompareExchange64(*a1, a2, 0LL) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(volatile signed __int64 **a1, signed __int64 a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( !**a1 )
result = _InterlockedCompareExchange64(*a1, a2, 0LL) == 0;
return result;
}

original_code:
uint_t shmctx_trylock(shmctx_t *mtx, uint_t id) {
    return (*mtx->lock == 0 && __sync_bool_compare_and_swap(mtx->lock, 0, id));
}

canonical_original_code:
unsigned long shmctx_trylock(struct shmctx_t *mtx, unsigned long id) {
    return (*mtx->lock == 0 && __sync_bool_compare_and_swap(mtx->lock, 0, id));
}

variable_types:
id
unsigned long

user_defined_types: struct shmctx_t { unsigned long *lock; unsigned int spin; }


decompiled_code:
_BOOL8 __fastcall shmtx_unlock(volatile signed __int64 **a1, signed __int64 a2)
{
return a2 == _InterlockedCompareExchange64(*a1, 0LL, a2);
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(volatile signed __int64 **a1, signed __int64 a2)
{
return a2 == _InterlockedCompareExchange64(*a1, 0LL, a2);
}

original_code:
uint_t shmtx_unlock(shmctx_t *mtx, uint_t id) {
    return __sync_bool_compare_and_swap(mtx->lock, id, 0);
}

canonical_original_code:
unsigned long shmtx_unlock(struct shmctx_t *mtx, unsigned long id) {
    return __sync_bool_compare_and_swap(mtx->lock, id, 0);
}


decompiled_code:
__int64 __fastcall shmtx_force_unlock(volatile __int64 **a1)
{
return _InterlockedExchange64(*a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(volatile __int64 **a1)
{
return _InterlockedExchange64(*a1, 0LL);
}

original_code:
uint_t shmtx_force_unlock(shmctx_t *mtx) {
    return __sync_lock_test_and_set(mtx->lock, 0);
}

canonical_original_code:
unsigned long shmtx_force_unlock(struct shmctx_t *mtx) {
    return __sync_lock_test_and_set(mtx->lock, 0);
}


decompiled_code:
signed __int64 __fastcall shmtx_lock(__int64 a1, signed __int64 a2)
{
volatile signed __int64 *v2; // rdx
__int64 v3; // rbp
signed __int64 v4; // rax
int v5; // edx
int v6; // eax
signed __int64 result; // rax
signed __int64 v8; // rtt

if ( !ticks_per_second_4 )
ticks_per_second_4 = GetCpuFreq();
v2 = *(volatile signed __int64 **)a1;
v3 = 0LL;
v4 = **(_QWORD **)a1;
if ( !v4 )
goto LABEL_13;
while ( 1 )
{
v5 = 1;
if ( *(_DWORD *)(a1 + 8) > 1u )
break;
LABEL_10:
if ( ticks_per_second_4 / 0x32uLL < ++v3 )
{
v3 = 0LL;
shmtx_force_unlock((volatile __int64 **)a1);
}
sched_yield();
v2 = *(volatile signed __int64 **)a1;
v4 = **(_QWORD **)a1;
if ( !v4 )
{
LABEL_13:
v8 = v4;
result = _InterlockedCompareExchange64(v2, a2, v4);
if ( v8 == result )
return result;
}
}
while ( 1 )
{
v6 = 0;
if ( v5 > 0 )
{
do
{
_mm_pause();
++v6;
}
while ( v5 != v6 );
v3 += (unsigned int)(v5 - 1) + 1LL;
}
if ( !**(_QWORD **)a1 )
{
result = _InterlockedCompareExchange64(*(volatile signed __int64 **)a1, a2, 0LL);
if ( !result )
return result;
}
v5 *= 2;
if ( *(_DWORD *)(a1 + 8) <= (unsigned int)v5 )
goto LABEL_10;
}
}

canonical_decompiled_code:
signed __int64 __fastcall func9(__int64 a1, signed __int64 a2)
{
volatile signed __int64 *v2; // rdx
__int64 v3; // rbp
signed __int64 v4; // rax
int v5; // edx
int v6; // eax
signed __int64 result; // rax
signed __int64 v8; // rtt

if ( !ticks_per_second_4 )
ticks_per_second_4 = func4();
v2 = *(volatile signed __int64 **)a1;
v3 = 0LL;
v4 = **(_QWORD **)a1;
if ( !v4 )
goto LABEL_13;
while ( 1 )
{
v5 = 1;
if ( *(_DWORD *)(a1 + 8) > 1u )
break;
LABEL_10:
if ( ticks_per_second_4 / 0x32uLL < ++v3 )
{
v3 = 0LL;
func8((volatile __int64 **)a1);
}
sched_yield();
v2 = *(volatile signed __int64 **)a1;
v4 = **(_QWORD **)a1;
if ( !v4 )
{
LABEL_13:
v8 = v4;
result = _InterlockedCompareExchange64(v2, a2, v4);
if ( v8 == result )
return result;
}
}
while ( 1 )
{
v6 = 0;
if ( v5 > 0 )
{
do
{
_mm_pause();
++v6;
}
while ( v5 != v6 );
v3 += (unsigned int)(v5 - 1) + 1LL;
}
if ( !**(_QWORD **)a1 )
{
result = _InterlockedCompareExchange64(*(volatile signed __int64 **)a1, a2, 0LL);
if ( !result )
return result;
}
v5 *= 2;
if ( *(_DWORD *)(a1 + 8) <= (unsigned int)v5 )
goto LABEL_10;
}
}

original_code:
void shmtx_lock(shmctx_t *mtx, uint_t id) {
    int i, n;
    static unsigned long long ticks_per_second = 0;
    unsigned long long ticks_nop = 0;
    if (__builtin_expect((ticks_per_second == 0),0)) {
        ticks_per_second = GetCpuFreq();
    }
    for ( ;; ) {
        if (*mtx->lock == 0 && __sync_bool_compare_and_swap(mtx->lock, 0, id)) {
            return;
        }
        for (n = 1; n < mtx->spin; n <<= 1) {
            for (i = 0; i < n; i++) {
                __asm__ ("pause");
                ticks_nop++;
            }
            if (*mtx->lock == 0 && __sync_bool_compare_and_swap(mtx->lock, 0, id)) {
                return;
            }
        }
        if (++ticks_nop > ticks_per_second/50) {
            shmtx_force_unlock(mtx);
            ticks_nop = 0;
        }
        sched_yield();
    }
}

canonical_original_code:
void shmtx_lock(struct shmctx_t *mtx, unsigned long id) {
    int i; int n;
    static unsigned long long ticks_per_second = 0;
    unsigned long long ticks_nop = 0;
    if (__builtin_expect((ticks_per_second == 0),0)) {
        ticks_per_second = GetCpuFreq();
    }
    for ( ;; ) {
        if (*mtx->lock == 0 && __sync_bool_compare_and_swap(mtx->lock, 0, id)) {
            return;
        }
        for (n = 1; n < mtx->spin; n <<= 1) {
            for (i = 0; i < n; i++) {
                __asm__ ("pause");
                ticks_nop++;
            }
            if (*mtx->lock == 0 && __sync_bool_compare_and_swap(mtx->lock, 0, id)) {
                return;
            }
        }
        if (++ticks_nop > ticks_per_second/50) {
            shmtx_force_unlock(mtx);
            ticks_nop = 0;
        }
        sched_yield();
    }
}

variable_types: i int

variable_types: n int


original_code:
int ringbuf_init(size_t size, size_t n, int key) {
    size_t initsize = 0;
    void *addr = ((void *)0);
    char *ch;
    int id;
    initsize = sizeof(shm_data_t) + size * n;
    id = shmget(key, initsize, 0400|0200|01000);
    if (id == 0)
        return -1;
    addr = shmat(id, ((void *)0), 0);
    if (addr == (void*)-1)
        return -2;
    shm = (shm_data_t*)addr;
    shm->size = size;
    shm->max = n;
    ch = shm->channel;
    shmtx_init(&mutex_w, (shmtx_sh_t*)ch, 0x800);
    shmtx_init(&mutex_r, (shmtx_sh_t*)(ch + 64), 0x800);
    return 0;
}

canonical_original_code:
int ringbuf_init(unsigned long size, unsigned long n, int key) {
    unsigned long initsize = 0;
    void *addr = ((void *)0);
    char *ch;
    int id;
    initsize = sizeof(shm_data_t) + size * n;
    id = shmget(key, initsize, 0400|0200|01000);
    if (id == 0)
        return -1;
    addr = shmat(id, ((void *)0), 0);
    if (addr == (void *)-1)
        return -2;
    shm = (struct shm_data_t *)addr;
    shm->size = size;
    shm->max = n;
    ch = shm->channel;
    shmtx_init(&mutex_w, (struct shmtx_sh_t *)ch, 0x800);
    shmtx_init(&mutex_r, (struct shmtx_sh_t *)(ch + 64), 0x800);
    return 0;
}

variable_types:
size
unsigned long

variable_types:
n
unsigned long

variable_types: key int

variable_types:
initsize
unsigned long

variable_types:
addr
void *

variable_types:
ch
char *

variable_types: id int

user_defined_types: struct shm_data_t { char channel[256]; unsigned long size; unsigned long max; unsigned int index_r; unsigned int index_w; char pending[60]; char datas[]; }

user_defined_types: struct shmtx_sh_t { unsigned long lock; }


decompiled_code:
__int64 __fastcall ringbuf_push(void *src, size_t n)
{
signed __int64 v3; // rsi
volatile signed __int32 *v4; // rbx
unsigned __int32 v5; // ebp
signed __int32 v6; // er14
__int64 result; // rax
__pid_t v8; // eax

if ( *((_QWORD *)shm + 32) < n )
return 0xFFFFFFFFLL;
v3 = pid_2;
if ( !pid_2 )
{
v8 = getpid();
v3 = v8;
pid_2 = v8;
}
shmtx_lock((__int64)&mutex_w, v3);
v4 = (volatile signed __int32 *)shm;
v5 = *((_DWORD *)shm + 69);
v6 = (unsigned __int64)(v5 + 1) % *((_QWORD *)shm + 33);
if ( *((_DWORD *)shm + 68) == v6 )
{
shmtx_unlock((volatile signed __int64 **)&mutex_w, pid_2);
result = 1LL;
}
else
{
memcpy((char *)shm + *((_QWORD *)shm + 32) * v5 + 340, src, n);
_InterlockedCompareExchange(v4 + 69, v6, v5);
shmtx_unlock((volatile signed __int64 **)&mutex_w, pid_2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(void *src, size_t n)
{
signed __int64 v3; // rsi
volatile signed __int32 *v4; // rbx
unsigned __int32 v5; // ebp
signed __int32 v6; // er14
__int64 result; // rax
__pid_t v8; // eax

if ( *((_QWORD *)shm + 32) < n )
return 0xFFFFFFFFLL;
v3 = pid_2;
if ( !pid_2 )
{
v8 = getpid();
v3 = v8;
pid_2 = v8;
}
func9((__int64)&mutex_w, v3);
v4 = (volatile signed __int32 *)shm;
v5 = *((_DWORD *)shm + 69);
v6 = (unsigned __int64)(v5 + 1) % *((_QWORD *)shm + 33);
if ( *((_DWORD *)shm + 68) == v6 )
{
func7((volatile signed __int64 **)&mutex_w, pid_2);
result = 1LL;
}
else
{
memcpy((char *)shm + *((_QWORD *)shm + 32) * v5 + 340, src, n);
_InterlockedCompareExchange(v4 + 69, v6, v5);
func7((volatile signed __int64 **)&mutex_w, pid_2);
result = 0LL;
}
return result;
}

original_code:
int ringbuf_push(const char *value, size_t len) {
    static pid_t pid = 0;
 if (__builtin_expect((len > shm->size),0))
  return -1;
    if (__builtin_expect((pid == 0),0))
        pid = getpid();
    shmtx_lock(&mutex_w, pid);
    uint32_t index_w = shm->index_w;
    uint32_t next = ((index_w) + 1) % (shm->max);
    if (next == shm->index_r) {
        shmtx_unlock(&mutex_w, pid);
        return Cache_Full;
    }
    memcpy(shm->datas + index_w * shm->size, value, len);
    __sync_bool_compare_and_swap(&shm->index_w, index_w, next);
    if (!shmtx_unlock(&mutex_w, pid)) {
    }
    return 0;
}

canonical_original_code:
int ringbuf_push(char *value, unsigned long len) {
    static int pid = 0;
 if (__builtin_expect((len > shm->size),0))
  return -1;
    if (__builtin_expect((pid == 0),0))
        pid = getpid();
    shmtx_lock(&mutex_w, pid);
    unsigned int index_w = shm->index_w;
    unsigned int next = ((index_w) + 1) % (shm->max);
    if (next == shm->index_r) {
        shmtx_unlock(&mutex_w, pid);
        return Cache_Full;
    }
    memcpy(shm->datas + index_w * shm->size, value, len);
    __sync_bool_compare_and_swap(&shm->index_w, index_w, next);
    if (!shmtx_unlock(&mutex_w, pid)) {
    }
    return 0;
}

variable_types:
value
char *

variable_types:
len
unsigned long

variable_types: pid int

variable_types:
next
unsigned int


decompiled_code:
__int64 __fastcall ringbuf_push_unlock(void *src, unsigned __int64 a2)
{
_DWORD *v2; // rbx
unsigned __int64 v3; // r8
__int64 result; // rax
unsigned __int64 v5; // rbp

v2 = shm;
v3 = *((_QWORD *)shm + 32);
if ( v3 < a2 )
return 0xFFFFFFFFLL;
result = 1LL;
v5 = (unsigned __int64)(unsigned int)(*((_DWORD *)shm + 69) + 1) % *((_QWORD *)shm + 33);
if ( *((_DWORD *)shm + 68) != (_DWORD)v5 )
{
memcpy((char *)shm + v3 * *((unsigned int *)shm + 69) + 340, src, *((_QWORD *)shm + 32));
v2[69] = v5;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(void *src, unsigned __int64 a2)
{
_DWORD *v2; // rbx
unsigned __int64 v3; // r8
__int64 result; // rax
unsigned __int64 v5; // rbp

v2 = shm;
v3 = *((_QWORD *)shm + 32);
if ( v3 < a2 )
return 0xFFFFFFFFLL;
result = 1LL;
v5 = (unsigned __int64)(unsigned int)(*((_DWORD *)shm + 69) + 1) % *((_QWORD *)shm + 33);
if ( *((_DWORD *)shm + 68) != (_DWORD)v5 )
{
memcpy((char *)shm + v3 * *((unsigned int *)shm + 69) + 340, src, *((_QWORD *)shm + 32));
v2[69] = v5;
result = 0LL;
}
return result;
}

original_code:
int ringbuf_push_unlock(const char *value, size_t len) {
 if (__builtin_expect((len > shm->size),0))
  return -1;
    uint32_t next = ((shm->index_w) + 1) % (shm->max);
    if (next == shm->index_r) {
        return Cache_Full;
    }
    memcpy(shm->datas + shm->index_w * shm->size, value, shm->size);
    shm->index_w = next;
    return 0;
}

canonical_original_code:
int ringbuf_push_unlock(char *value, unsigned long len) {
 if (__builtin_expect((len > shm->size),0))
  return -1;
    unsigned int next = ((shm->index_w) + 1) % (shm->max);
    if (next == shm->index_r) {
        return Cache_Full;
    }
    memcpy(shm->datas + shm->index_w * shm->size, value, shm->size);
    shm->index_w = next;
    return 0;
}