drawBresenhamLine

decompiled_code:
__int64 __fastcall drawBresenhamLine(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 v7; // rdx
bool v8; // r9
bool v10; // di

v7 = a3 >> 32;
v8 = (int)a2 < (int)a3 && SHIDWORD(a2) <= (int)v7;
if ( v8 )
{
if ( (int)a3 - (int)a2 >= (int)v7 - HIDWORD(a2) )
return bresenhamX(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) >= (int)v7 )
{
v8 = (int)a2 > (int)a3;
goto LABEL_13;
}
return bresenhamY(a1, a2, a3, 0, a4);
}
if ( (int)a2 <= (int)a3 && SHIDWORD(a2) < (int)v7 )
{
if ( (int)a3 - (int)a2 >= (int)v7 - HIDWORD(a2) )
goto LABEL_15;
return bresenhamY(a1, a2, a3, 0, a4);
}
v8 = (int)a2 > (int)a3;
if ( SHIDWORD(a2) < (int)v7 && (int)a2 > (int)a3 )
{
if ( (int)a2 - (int)a3 < (int)v7 - HIDWORD(a2) )
return bresenhamY(a1, a2, a3, 1, a4);
if ( SHIDWORD(a2) > (int)v7 )
goto LABEL_8;
return bresenhamX(a1, a3, a2, 1, a4);
}
LABEL_13:
if ( SHIDWORD(a2) <= (int)v7 && v8 )
{
v10 = 0;
if ( (int)a2 - (int)a3 >= (int)v7 - HIDWORD(a2) )
return bresenhamX(a1, a3, a2, 1, a4);
goto LABEL_19;
}
LABEL_15:
v10 = SHIDWORD(a2) > (int)v7;
if ( SHIDWORD(a2) <= (int)v7 || !v8 )
{
if ( (int)a2 >= (int)a3 && SHIDWORD(a2) > (int)v7 )
{
if ( (int)a2 - (int)a3 < HIDWORD(a2) - (int)v7 )
return bresenhamY(a1, a3, a2, 0, a4);
if ( (int)a2 >= (int)a3 )
return bresenhamX(a1, a2, a3, 1, a4);
return bresenhamY(a1, a3, a2, 1, a4);
}
LABEL_19:
if ( (int)a2 >= (int)a3 || !v10 || (int)a3 - (int)a2 > HIDWORD(a2) - (int)v7 )
return bresenhamX(a1, a2, a3, 1, a4);
return bresenhamY(a1, a3, a2, 1, a4);
}
LABEL_8:
if ( (int)a2 - (int)a3 < HIDWORD(a2) - (int)v7 )
return bresenhamY(a1, a3, a2, 0, a4);
return bresenhamX(a1, a3, a2, 0, a4);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 v7; // rdx
bool v8; // r9
bool v10; // di

v7 = a3 >> 32;
v8 = (int)a2 < (int)a3 && SHIDWORD(a2) <= (int)v7;
if ( v8 )
{
if ( (int)a3 - (int)a2 >= (int)v7 - HIDWORD(a2) )
return func6(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) >= (int)v7 )
{
v8 = (int)a2 > (int)a3;
goto LABEL_13;
}
return func5(a1, a2, a3, 0, a4);
}
if ( (int)a2 <= (int)a3 && SHIDWORD(a2) < (int)v7 )
{
if ( (int)a3 - (int)a2 >= (int)v7 - HIDWORD(a2) )
goto LABEL_15;
return func5(a1, a2, a3, 0, a4);
}
v8 = (int)a2 > (int)a3;
if ( SHIDWORD(a2) < (int)v7 && (int)a2 > (int)a3 )
{
if ( (int)a2 - (int)a3 < (int)v7 - HIDWORD(a2) )
return func5(a1, a2, a3, 1, a4);
if ( SHIDWORD(a2) > (int)v7 )
goto LABEL_8;
return func6(a1, a3, a2, 1, a4);
}
LABEL_13:
if ( SHIDWORD(a2) <= (int)v7 && v8 )
{
v10 = 0;
if ( (int)a2 - (int)a3 >= (int)v7 - HIDWORD(a2) )
return func6(a1, a3, a2, 1, a4);
goto LABEL_19;
}
LABEL_15:
v10 = SHIDWORD(a2) > (int)v7;
if ( SHIDWORD(a2) <= (int)v7 || !v8 )
{
if ( (int)a2 >= (int)a3 && SHIDWORD(a2) > (int)v7 )
{
if ( (int)a2 - (int)a3 < HIDWORD(a2) - (int)v7 )
return func5(a1, a3, a2, 0, a4);
if ( (int)a2 >= (int)a3 )
return func6(a1, a2, a3, 1, a4);
return func5(a1, a3, a2, 1, a4);
}
LABEL_19:
if ( (int)a2 >= (int)a3 || !v10 || (int)a3 - (int)a2 > HIDWORD(a2) - (int)v7 )
return func6(a1, a2, a3, 1, a4);
return func5(a1, a3, a2, 1, a4);
}
LABEL_8:
if ( (int)a2 - (int)a3 < HIDWORD(a2) - (int)v7 )
return func5(a1, a3, a2, 0, a4);
return func6(a1, a3, a2, 0, a4);
}

original_code:
void
drawBresenhamLine(XPM *canvas, Point pStart, Point pEnd, unsigned short colorIndex)
{
    if(pEnd.x > pStart.x &&
       pEnd.y >= pStart.y &&
       (pEnd.x - pStart.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x >= pStart.x &&
     pEnd.y > pStart.y &&
     (pEnd.x - pStart.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y > pStart.y &&
     (pStart.x - pEnd.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,1,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y >= pStart.y &&
     (pStart.x - pEnd.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pEnd,pStart,1,colorIndex);
    }
    else if (pEnd.x < pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) >= (pStart.y - pEnd.y))
    {
        bresenhamX(canvas,pEnd,pStart,0,colorIndex);
    }
    else if (pEnd.x <= pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) < (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,0,colorIndex);
    }
    else if(pEnd.x > pStart.x &&
     pEnd.y < pStart.y &&
     (pEnd.x - pStart.x) <= (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,1,colorIndex);
    }
    else
    {
        bresenhamX(canvas,pStart,pEnd,1,colorIndex);
    }
}

canonical_original_code:
void drawBresenhamLine(struct XPM *canvas, struct Point pStart, struct Point pEnd, unsigned short colorIndex)
{
    if(pEnd.x > pStart.x &&
       pEnd.y >= pStart.y &&
       (pEnd.x - pStart.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x >= pStart.x &&
     pEnd.y > pStart.y &&
     (pEnd.x - pStart.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,0,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y > pStart.y &&
     (pStart.x - pEnd.x) < (pEnd.y - pStart.y))
    {
        bresenhamY(canvas,pStart,pEnd,1,colorIndex);
    }
    else if(pEnd.x < pStart.x &&
     pEnd.y >= pStart.y &&
     (pStart.x - pEnd.x) >= (pEnd.y - pStart.y))
    {
        bresenhamX(canvas,pEnd,pStart,1,colorIndex);
    }
    else if (pEnd.x < pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) >= (pStart.y - pEnd.y))
    {
        bresenhamX(canvas,pEnd,pStart,0,colorIndex);
    }
    else if (pEnd.x <= pStart.x &&
      pEnd.y < pStart.y &&
      (pStart.x - pEnd.x) < (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,0,colorIndex);
    }
    else if(pEnd.x > pStart.x &&
     pEnd.y < pStart.y &&
     (pEnd.x - pStart.x) <= (pStart.y - pEnd.y))
    {
        bresenhamY(canvas,pEnd,pStart,1,colorIndex);
    }
    else
    {
        bresenhamX(canvas,pStart,pEnd,1,colorIndex);
    }
}

variable_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

variable_types:
pStart
struct Point { int x; int y; }

variable_types:
pEnd
struct Point

variable_types:
colorIndex
unsigned short


renderGElements

repo: alebugariu/egioc

decompiled_code:
void __fastcall renderGElements(__int64 a1, unsigned int *a2)
{
unsigned int *i; // rbx
__int64 v3; // rdx

for ( i = a2; i; i = (unsigned int *)*((_QWORD *)i + 3) )
{
v3 = i[4];
if ( (_DWORD)v3 == 2 )
{
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *i, i[1], i[2], i[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, i, i + 2) == 1 )
{
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *i, i[1], i[2], i[3]);
drawBresenhamLine(a1, *(_QWORD *)i, *((_QWORD *)i + 1), 1LL);
}
else
{
fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
}
}
else
{
fprintf(stderr, "Unhandled type (%d) !\n", v3);
}
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, unsigned int *a2)
{
unsigned int *i; // rbx
__int64 v3; // rdx

for ( i = a2; i; i = (unsigned int *)*((_QWORD *)i + 3) )
{
v3 = i[4];
if ( (_DWORD)v3 == 2 )
{
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *i, i[1], i[2], i[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, i, i + 2) == 1 )
{
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *i, i[1], i[2], i[3]);
drawBresenhamLine(a1, *(_QWORD *)i, *((_QWORD *)i + 1), 1LL);
}
else
{
fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
}
}
else
{
fprintf(stderr, "Unhandled type (%d) !\n", v3);
}
}
}

original_code:
void
renderGElements(XPM *canvas, struct GENode *glist){
  struct GENode *pgl = glist;
  GElement *pge = ((void *)0);
  while(((void *)0) != pgl){
    pge = &pgl->el;
    switch(pge->type){
    case LINE:
      printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
      if(cohenSutherlandFrameLine(canvas, &pge->data.line.st, &pge->data.line.en) == 1){
 printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
 drawBresenhamLine(canvas, pge->data.line.st, pge->data.line.en, 1);
      }
      else{
 fprintf(stderr, "*** Line was totally outside the canvas.\n");
      }
      break;
    default:
      fprintf(stderr, "Unhandled type (%d) !\n", pge->type);
      break;
    }
    pgl = pgl->next;
  }
}

canonical_original_code:
void renderGElements(struct XPM *canvas, struct GENode *glist){
  struct GENode *pgl = glist;
  struct GElement *pge = ((void *)0);
  while(((void *)0) != pgl){
    pge = &pgl->el;
    switch(pge->type){
    case LINE:
      printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
      if(cohenSutherlandFrameLine(canvas, &pge->data.line.st, &pge->data.line.en) == 1){
 printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", pge->data.line.st.x, pge->data.line.st.y, pge->data.line.en.x, pge->data.line.en.y);
 drawBresenhamLine(canvas, pge->data.line.st, pge->data.line.en, 1);
      }
      else{
 fprintf(stderr, "*** Line was totally outside the canvas.\n");
      }
      break;
    default:
      fprintf(stderr, "Unhandled type (%d) !\n", pge->type);
      break;
    }
    pgl = pgl->next;
  }
}

variable_types: struct GENode { struct GElement { union { struct Point { int x; int y; } point; struct { struct Point st; struct Point en; } line; } data; enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 } type; } el; struct GENode *next; } *

variable_types: struct GElement { union { struct Point { int x; int y; } point; struct { struct Point st; struct Point en; } line; } data; enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 } type; } *

return_type: void

user_defined_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor *colta; unsigned short **data; }

user_defined_types: struct GENode { struct GElement el; struct GENode *next; }

user_defined_types: struct GElement { union { struct Point point; struct { struct Point st; struct Point en; } line; } data; enum GElementType type; }

user_defined_types: struct XPMColor { struct Color clr; char *chars; char *key; }

user_defined_types: struct Color { unsigned short red; unsigned short green; unsigned short blue; }


parsePSLine

decompiled_code:
__int64 __fastcall parsePSLine(__int64 a1, __int64 a2)
{
if ( (unsigned int)_isoc99_sscanf(a1, "%d %d %d %d line", a2, a2 + 4, a2 + 8, a2 + 12) == 4 )
*(_DWORD *)(a2 + 16) = 2;
return 0LL;
}

original_code:
procPSLineReturnVal
parsePSLine(const char *line, GElement *dest){
  procPSLineReturnVal returnCode = PSLINE_OK;
  if(sscanf(line, "%d %d %d %d line", &dest->data.line.st.x, &dest->data.line.st.y, &dest->data.line.en.x, &dest->data.line.en.y) == 4){
    dest->type = LINE;
  }
  return returnCode;
}

canonical_original_code:
enum procPSLineReturnVal parsePSLine(char *line, struct GElement *dest){
  enum procPSLineReturnVal returnCode = PSLINE_OK;
  if(sscanf(line, "%d %d %d %d line", &dest->data.line.st.x, &dest->data.line.st.y, &dest->data.line.en.x, &dest->data.line.en.y) == 4){
    dest->type = LINE;
  }
  return returnCode;
}

variable_types:
line
char *

variable_types: enum procPSLineReturnVal { PSLINE_OK=0, UNRECOGNIZED_ELEMENT=1 }

user_defined_types: enum GElementType { UNDEFINED_ELEMENT=0, BLANK=1, LINE=2, POINT=3 }


renderGElement

decompiled_code:
__int64 __fastcall renderGElement(__int64 a1, unsigned int *a2)
{
if ( a2[4] != 2 )
return fwrite("Unhandled type!\n", 1uLL, 0x10uLL, stderr);
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, a2, a2 + 2) != 1 )
return fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
return drawBresenhamLine(a1, *(_QWORD *)a2, *((_QWORD *)a2 + 1), 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int *a2)
{
if ( a2[4] != 2 )
return fwrite("Unhandled type!\n", 1uLL, 0x10uLL, stderr);
printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
if ( (unsigned int)cohenSutherlandFrameLine(a1, a2, a2 + 2) != 1 )
return fwrite("*** Line was totally outside the canvas.\n", 1uLL, 0x29uLL, stderr);
printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", *a2, a2[1], a2[2], a2[3]);
return drawBresenhamLine(a1, *(_QWORD *)a2, *((_QWORD *)a2 + 1), 1LL);
}

original_code:
void
renderGElement(XPM *canvas, GElement *el){
  switch(el->type){
  case LINE:
    printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
    if(cohenSutherlandFrameLine(canvas, &el->data.line.st, &el->data.line.en) == 1){
      printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
      drawBresenhamLine(canvas, el->data.line.st, el->data.line.en, 1);
    }
    else{
      fprintf(stderr, "*** Line was totally outside the canvas.\n");
    }
    break;
  default:
    fprintf(stderr, "Unhandled type!\n");
    break;
  }
}

canonical_original_code:
void renderGElement(struct XPM *canvas, struct GElement *el){
  switch(el->type){
  case LINE:
    printf("Original Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
    if(cohenSutherlandFrameLine(canvas, &el->data.line.st, &el->data.line.en) == 1){
      printf("Trimed Line := start{x:%4d y:%4d}, end{x:%4d y:%4d}\n", el->data.line.st.x, el->data.line.st.y, el->data.line.en.x, el->data.line.en.y);
      drawBresenhamLine(canvas, el->data.line.st, el->data.line.en, 1);
    }
    else{
      fprintf(stderr, "*** Line was totally outside the canvas.\n");
    }
    break;
  default:
    fprintf(stderr, "Unhandled type!\n");
    break;
  }
}

variable_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

user_defined_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor *colta; unsigned short **data; }


parsePSLine

return_type: enum procPSLineReturnVal { PSLINE_OK=0, UNRECOGNIZED_ELEMENT=1 }


bresenhamY

decompiled_code:
__int64 __fastcall bresenhamY(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int16 a5)
{
int v6; // er9
unsigned int v7; // ebp
__int64 v8; // rcx
int v9; // ebx
__int64 result; // rax
int v11; // edi
int v12; // esi
int v13; // er12
int v14; // er15
int v17; // [rsp+8h] [rbp-38h]

v6 = a4;
v7 = a2;
v8 = a2 >> 32;
v9 = HIDWORD(a2);
result = a3 >> 32;
v11 = a3 - a2;
v12 = a2 - a3;
if ( !v6 )
v12 = v11;
v13 = 2 * v12 - (HIDWORD(a3) - v8);
if ( (int)v8 <= SHIDWORD(a3) )
{
v14 = HIDWORD(a3);
v17 = 2 * (v12 - (HIDWORD(a3) - v8));
do
{
result = putXPMpixel(a1, v7, *(_DWORD *)(a1 + 4) - v9, a5);
if ( v13 > 0 )
{
v13 += v17;
result = v7-- + 1;
if ( !a4 )
v7 = result;
}
else
{
v13 += 2 * v12;
}
++v9;
}
while ( v9 <= v14 );
}
return result;
}

original_code:
static void
bresenhamY (XPM *canvas, Point q, Point r, int symmetry, unsigned short clrIndex)
{
    int dx, dy, D, x, y;
    if(symmetry == 0)
        dx = r.x - q.x;
    else dx = q.x - r.x;
    dy = r.y - q.y;
    D = 2*dx - dy;
    x = q.x;
    for (y = q.y; y <= r.y; y++)
    {
        putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dx;
        else
        {
            D += 2*(dx - dy);
            if(symmetry == 0)
                x++;
            else x--;
        }
    }
}

canonical_original_code:
static void bresenhamY(struct XPM *canvas, struct Point q, struct Point r, int symmetry, unsigned short clrIndex)
{
    int dx; int dy; int D; int x; int y;
    if(symmetry == 0)
        dx = r.x - q.x;
    else dx = q.x - r.x;
    dy = r.y - q.y;
    D = 2*dx - dy;
    x = q.x;
    for (y = q.y; y <= r.y; y++)
    {
        putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dx;
        else
        {
            D += 2*(dx - dy);
            if(symmetry == 0)
                x++;
            else x--;
        }
    }
}

variable_types:
q
struct Point { int x; int y; }

variable_types:
r
struct Point

variable_types: symmetry int

variable_types:
clrIndex
unsigned short

variable_types: dx int

variable_types: dy int

variable_types: D int

variable_types: x int

variable_types: y int


bresenhamX

decompiled_code:
__int64 __fastcall bresenhamX(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int16 a5)
{
int v6; // ebp
int v7; // edi
__int64 result; // rax
int v9; // er12
signed int v10; // ebx
signed int v11; // er15
int v14; // [rsp+8h] [rbp-38h]
int v15; // [rsp+Ch] [rbp-34h]

v6 = HIDWORD(a2);
v7 = a3 - a2;
result = (unsigned int)(HIDWORD(a2) - HIDWORD(a3));
if ( !a4 )
result = (unsigned int)(HIDWORD(a3) - HIDWORD(a2));
v15 = 2 * result;
v9 = 2 * result - v7;
if ( (int)a2 <= (int)a3 )
{
v10 = a2;
v11 = a3;
v14 = 2 * (result - v7);
do
{
result = putXPMpixel(a1, v10, *(_DWORD *)(a1 + 4) - v6, a5);
if ( v9 > 0 )
{
v9 += v14;
result = (unsigned int)(v6-- + 1);
if ( !a4 )
v6 = result;
}
else
{
v9 += v15;
}
++v10;
}
while ( v10 <= v11 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, int a4, unsigned __int16 a5)
{
int v6; // ebp
int v7; // edi
__int64 result; // rax
int v9; // er12
signed int v10; // ebx
signed int v11; // er15
int v14; // [rsp+8h] [rbp-38h]
int v15; // [rsp+Ch] [rbp-34h]

v6 = HIDWORD(a2);
v7 = a3 - a2;
result = (unsigned int)(HIDWORD(a2) - HIDWORD(a3));
if ( !a4 )
result = (unsigned int)(HIDWORD(a3) - HIDWORD(a2));
v15 = 2 * result;
v9 = 2 * result - v7;
if ( (int)a2 <= (int)a3 )
{
v10 = a2;
v11 = a3;
v14 = 2 * (result - v7);
do
{
result = func3(a1, v10, *(_DWORD *)(a1 + 4) - v6, a5);
if ( v9 > 0 )
{
v9 += v14;
result = (unsigned int)(v6-- + 1);
if ( !a4 )
v6 = result;
}
else
{
v9 += v15;
}
++v10;
}
while ( v10 <= v11 );
}
return result;
}

original_code:
static void
bresenhamX (XPM *canvas, Point q, Point r, int symmetry, unsigned short clrIndex)
{
    int dx, dy, D, x, y;
    dx = r.x - q.x;
    if (symmetry == 0)
        dy = r.y - q.y;
    else dy = q.y - r.y;
    D = 2*dy - dx;
    y = q.y;
    for (x = q.x; x <= r.x; x++)
    {
      putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dy;
        else
        {
            D += 2*(dy - dx);
            if(symmetry == 0)
                y++;
            else y--;
        }
    }
}

canonical_original_code:
static void bresenhamX(struct XPM *canvas, struct Point q, struct Point r, int symmetry, unsigned short clrIndex)
{
    int dx; int dy; int D; int x; int y;
    dx = r.x - q.x;
    if (symmetry == 0)
        dy = r.y - q.y;
    else dy = q.y - r.y;
    D = 2*dy - dx;
    y = q.y;
    for (x = q.x; x <= r.x; x++)
    {
      putXPMpixel(canvas, x, canvas->height - y, clrIndex);
        if (D <= 0) D += 2 * dy;
        else
        {
            D += 2*(dy - dx);
            if(symmetry == 0)
                y++;
            else y--;
        }
    }
}

variable_types: struct XPM { unsigned int width; unsigned int height; struct { int windowTop; int windowLeft; int windowBottom; int windowRight; } displayRegion; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *


cohenSutherlandFrameLine

decompiled_code:
__int64 __fastcall cohenSutherlandFrameLine(int *a1, int *a2, int *a3)
{
unsigned __int8 v4; // bp
unsigned __int8 v5; // al
unsigned int v6; // edx

v4 = getRegionCode(a1, a2);
v5 = getRegionCode(a1, a3);
v6 = 1;
if ( v5 | v4 )
{
v6 = 0;
if ( (v5 & v4) == 0 )
{
trimLineToRegion(a1, a2, a3, v4, v5);
v6 = cohenSutherlandFrameLine(a1, a2, a3);
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func9(int *a1, int *a2, int *a3)
{
unsigned __int8 v4; // bp
unsigned __int8 v5; // al
unsigned int v6; // edx

v4 = func0(a1, a2);
v5 = func0(a1, a3);
v6 = 1;
if ( v5 | v4 )
{
v6 = 0;
if ( (v5 & v4) == 0 )
{
func1(a1, a2, a3, v4, v5);
v6 = func9(a1, a2, a3);
}
}
return v6;
}

original_code:
int
cohenSutherlandFrameLine(XPM *canvas, Point *pStart, Point *pEnd)
{
  unsigned char codeStart = getRegionCode(canvas, pStart);
  unsigned char codeEnd = getRegionCode(canvas,pEnd);
    if((codeStart | codeEnd) == 0) return 1;
    if((codeStart & codeEnd) != 0) return 0;
    trimLineToRegion(canvas, pStart, pEnd, codeStart, codeEnd);
    return cohenSutherlandFrameLine(canvas, pStart, pEnd);
}

canonical_original_code:
int cohenSutherlandFrameLine(struct XPM *canvas, struct Point *pStart, struct Point *pEnd)
{
  unsigned char codeStart = getRegionCode(canvas, pStart);
  unsigned char codeEnd = getRegionCode(canvas,pEnd);
    if((codeStart | codeEnd) == 0) return 1;
    if((codeStart & codeEnd) != 0) return 0;
    trimLineToRegion(canvas, pStart, pEnd, codeStart, codeEnd);
    return cohenSutherlandFrameLine(canvas, pStart, pEnd);
}

variable_types:
pStart
struct Point { int x; int y; } *

variable_types:
pEnd
struct Point *

variable_types:
codeStart
unsigned char

variable_types:
codeEnd
unsigned char

return_type: int


drawBresenhamLine

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, __int64 a3, unsigned __int16 a4)
{
__int64 v7; // rdx
bool v8; // r9
bool v10; // di

v7 = a3 >> 32;
v8 = (int)a2 < (int)a3 && SHIDWORD(a2) <= (int)v7;
if ( v8 )
{
if ( (int)a3 - (int)a2 >= (int)v7 - HIDWORD(a2) )
return func5(a1, a2, a3, 0, a4);
if ( SHIDWORD(a2) >= (int)v7 )
{
v8 = (int)a2 > (int)a3;
goto LABEL_13;
}
return func4(a1, a2, a3, 0, a4);
}
if ( (int)a2 <= (int)a3 && SHIDWORD(a2) < (int)v7 )
{
if ( (int)a3 - (int)a2 >= (int)v7 - HIDWORD(a2) )
goto LABEL_15;
return func4(a1, a2, a3, 0, a4);
}
v8 = (int)a2 > (int)a3;
if ( SHIDWORD(a2) < (int)v7 && (int)a2 > (int)a3 )
{
if ( (int)a2 - (int)a3 < (int)v7 - HIDWORD(a2) )
return func4(a1, a2, a3, 1, a4);
if ( SHIDWORD(a2) > (int)v7 )
goto LABEL_8;
return func5(a1, a3, a2, 1, a4);
}
LABEL_13:
if ( SHIDWORD(a2) <= (int)v7 && v8 )
{
v10 = 0;
if ( (int)a2 - (int)a3 >= (int)v7 - HIDWORD(a2) )
return func5(a1, a3, a2, 1, a4);
goto LABEL_19;
}
LABEL_15:
v10 = SHIDWORD(a2) > (int)v7;
if ( SHIDWORD(a2) <= (int)v7 || !v8 )
{
if ( (int)a2 >= (int)a3 && SHIDWORD(a2) > (int)v7 )
{
if ( (int)a2 - (int)a3 < HIDWORD(a2) - (int)v7 )
return func4(a1, a3, a2, 0, a4);
if ( (int)a2 >= (int)a3 )
return func5(a1, a2, a3, 1, a4);
return func4(a1, a3, a2, 1, a4);
}
LABEL_19:
if ( (int)a2 >= (int)a3 || !v10 || (int)a3 - (int)a2 > HIDWORD(a2) - (int)v7 )
return func5(a1, a2, a3, 1, a4);
return func4(a1, a3, a2, 1, a4);
}
LABEL_8:
if ( (int)a2 - (int)a3 < HIDWORD(a2) - (int)v7 )
return func4(a1, a3, a2, 0, a4);
return func5(a1, a3, a2, 0, a4);
}

variable_types:
pStart
struct Point { int x; int y; }

variable_types:
pEnd
struct Point

variable_types:
colorIndex
unsigned short


assignColorTable

original_code:
void
assignColorTable(XPM *img, const unsigned char vColors[][3], int clrCnt){
  XPMColor clrData;
  int clrIndex = 0;
  char charsFormat[10];
  if(clrCnt > img->ncolors ||
     clrCnt > pow(2, img->chrperpixel * 8) - 1) return;
  for(clrIndex = 1; clrIndex <= clrCnt; clrIndex ++){
    clrData.clr.blue = vColors[clrIndex - 1][0];
    clrData.clr.green = vColors[clrIndex - 1][1];
    clrData.clr.red = vColors[clrIndex - 1][2];
    clrData.key = (char *)malloc(2 * sizeof(char));
    strcpy(clrData.key, "c");
    clrData.chars = (char *)malloc((1 + img->chrperpixel) * sizeof(char));
    switch(img->chrperpixel){
    case 1:
      if(clrCnt <= 0x0F) sprintf(clrData.chars, "%1X", clrIndex);
      else sprintf(clrData.chars, "%c", clrIndex);
      break;
    default:
      sprintf(charsFormat, "%%0%dX", img->chrperpixel);
      sprintf(clrData.chars, charsFormat, clrIndex);
      break;
    }
    setXPMColor(img, clrIndex - 1, clrData);
  }
}

canonical_original_code:
void assignColorTable(struct XPM *img, unsigned char vColors[][3], int clrCnt){
  struct XPMColor clrData;
  int clrIndex = 0;
  char charsFormat[10];
  if(clrCnt > img->ncolors ||
     clrCnt > pow(2, img->chrperpixel * 8) - 1) return;
  for(clrIndex = 1; clrIndex <= clrCnt; clrIndex ++){
    clrData.clr.blue = vColors[clrIndex - 1][0];
    clrData.clr.green = vColors[clrIndex - 1][1];
    clrData.clr.red = vColors[clrIndex - 1][2];
    clrData.key = (char *)malloc(2 * sizeof(char));
    strcpy(clrData.key, "c");
    clrData.chars = (char *)malloc((1 + img->chrperpixel) * sizeof(char));
    switch(img->chrperpixel){
    case 1:
      if(clrCnt <= 0x0F) sprintf(clrData.chars, "%1X", clrIndex);
      else sprintf(clrData.chars, "%c", clrIndex);
      break;
    default:
      sprintf(charsFormat, "%%0%dX", img->chrperpixel);
      sprintf(clrData.chars, charsFormat, clrIndex);
      break;
    }
    setXPMColor(img, clrIndex - 1, clrData);
  }
}

variable_types: struct XPM { unsigned int width; unsigned int height; unsigned short chrperpixel; unsigned int ncolors; struct XPMColor { struct Color { unsigned short red; unsigned short green; unsigned short blue; } clr; char *chars; char *key; } *colta; unsigned short **data; } *

variable_types:
vColors
unsigned char [][3]

variable_types: clrCnt int

variable_types: clrIndex int

variable_types:
charsFormat
char [10]


repo: alech/alphamote

decompiled_code:
void __noreturn release_usb_device()
{
if ( (unsigned int)usb_release_interface(devh, 0LL) )
{
usb_close(devh);
}
else
{
printf("failed to release interface: %d\n", 0LL);
usb_close(devh);
printf("failed to close interface: %d\n", 0LL);
}
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
if ( (unsigned int)usb_release_interface(devh, 0LL) )
{
usb_close(devh);
}
else
{
printf("failed to release interface: %d\n", 0LL);
usb_close(devh);
printf("failed to close interface: %d\n", 0LL);
}
exit(1);
}

original_code:
void release_usb_device(int dummy) {
    int ret;
    ret = usb_release_interface(devh, 0);
    if (!ret)
 printf("failed to release interface: %d\n", ret);
    usb_close(devh);
    if (!ret)
 printf("failed to close interface: %d\n", ret);
    exit(1);
}

variable_types: dummy int

variable_types: ret int


decompiled_code:
__int64 init_usb()
{
__int64 v0; // rax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
_BYTE v5[65544]; // [rsp+0h] [rbp-10008h] BYREF

usb_init();
usb_find_busses();
usb_find_devices();
v0 = find_device(1356, 743);
if ( !v0 )
{
fwrite("No camera found!\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
devh = usb_open(v0);
if ( !devh )
_assert_fail("devh", "alphamote.c", 0x38u, "init_usb");
signal(15, (__sighandler_t)release_usb_device);
usb_get_driver_np(devh, 0LL, v5, 0xFFFFLL);
v1 = usb_claim_interface(devh, 0LL);
if ( v1 )
{
printf("claim failed with error %d\n", v1);
exit(1);
}
if ( (int)usb_set_altinterface(devh, 0LL) < 0 )
_assert_fail("ret >= 0", "alphamote.c", 0x44u, "init_usb");
usb_get_descriptor(devh, 1LL, 0LL, v5, 18LL);
usleep(0xFA0u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 9LL);
usleep(0xBB8u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 39LL);
usleep(0xFA0u);
v2 = usb_release_interface(devh, 0LL);
if ( v2 )
printf("failed to release interface before set_configuration: %d\n", v2);
usb_set_configuration(devh, 1LL);
v3 = usb_claim_interface(devh, 0LL);
if ( v3 )
printf("claim after set_configuration failed with error %d\n", v3);
return usb_set_altinterface(devh, 0LL);
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
_BYTE v5[65544]; // [rsp+0h] [rbp-10008h] BYREF

usb_init();
usb_find_busses();
usb_find_devices();
v0 = func1(1356, 743);
if ( !v0 )
{
fwrite("No camera found!\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
devh = usb_open(v0);
if ( !devh )
_assert_fail("devh", "alphamote.c", 0x38u, "init_usb");
signal(15, (__sighandler_t)release_usb_device);
usb_get_driver_np(devh, 0LL, v5, 0xFFFFLL);
v1 = usb_claim_interface(devh, 0LL);
if ( v1 )
{
printf("claim failed with error %d\n", v1);
exit(1);
}
if ( (int)usb_set_altinterface(devh, 0LL) < 0 )
_assert_fail("ret >= 0", "alphamote.c", 0x44u, "init_usb");
usb_get_descriptor(devh, 1LL, 0LL, v5, 18LL);
usleep(0xFA0u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 9LL);
usleep(0xBB8u);
usb_get_descriptor(devh, 2LL, 0LL, v5, 39LL);
usleep(0xFA0u);
v2 = usb_release_interface(devh, 0LL);
if ( v2 )
printf("failed to release interface before set_configuration: %d\n", v2);
usb_set_configuration(devh, 1LL);
v3 = usb_claim_interface(devh, 0LL);
if ( v3 )
printf("claim after set_configuration failed with error %d\n", v3);
return usb_set_altinterface(devh, 0LL);
}

original_code:
void init_usb() {
    int vendor, product, ret;
    struct usb_device *dev;
    char buf[65535];
    usb_init();
    usb_find_busses();
    usb_find_devices();
    vendor = 0x054c;
    product = 0x02e7;
    dev = find_device(vendor, product);
    if (! dev) {
        fprintf(stderr, "No camera found!\n");
        exit(1);
    }
    devh = usb_open(dev);
    ((void) sizeof ((devh) ? 1 : 0), __extension__ ({ if (devh) ; else __assert_fail ("devh", "alphamote.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    signal(15, release_usb_device);
    ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) {
 printf("claim failed with error %d\n", ret);
 exit(1);
    }
    ret = usb_set_altinterface(devh, 0);
    ((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (ret >= 0) ; else __assert_fail ("ret >= 0", "alphamote.c", 68, __extension__ __PRETTY_FUNCTION__); }));
    ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
    usleep(4*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
    usleep(3*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
    usleep(4*1000);
    ret = usb_release_interface(devh, 0);
    if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
    ret = usb_set_configuration(devh, 0x0000001);
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
    ret = usb_set_altinterface(devh, 0);
    return;
}

canonical_original_code:
void init_usb() {
    int vendor; int product; int ret;
    struct usb_device *dev;
    char buf[65535];
    usb_init();
    usb_find_busses();
    usb_find_devices();
    vendor = 0x054c;
    product = 0x02e7;
    dev = find_device(vendor, product);
    if (! dev) {
        fprintf(stderr, "No camera found!\n");
        exit(1);
    }
    devh = usb_open(dev);
    ((void) sizeof ((devh) ? 1 : 0), __extension__ ({ if (devh) ; else __assert_fail ("devh", "alphamote.c", 56, __extension__ __PRETTY_FUNCTION__); }));
    signal(15, release_usb_device);
    ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) {
 printf("claim failed with error %d\n", ret);
 exit(1);
    }
    ret = usb_set_altinterface(devh, 0);
    ((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (ret >= 0) ; else __assert_fail ("ret >= 0", "alphamote.c", 68, __extension__ __PRETTY_FUNCTION__); }));
    ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
    usleep(4*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
    usleep(3*1000);
    ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
    usleep(4*1000);
    ret = usb_release_interface(devh, 0);
    if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
    ret = usb_set_configuration(devh, 0x0000001);
    ret = usb_claim_interface(devh, 0);
    if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
    ret = usb_set_altinterface(devh, 0);
    return;
}

variable_types: vendor int

variable_types: product int

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

variable_types:
buf
char [65535]

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

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

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

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


InitPrinter

decompiled_code:
__int64 __fastcall InitPrinter(__int64 fd)
{
unsigned __int8 v1; // al
unsigned __int8 v2; // al
unsigned __int8 v3; // al
unsigned __int8 v4; // al
unsigned int v5; // ebp
unsigned __int8 v6; // al
char v7; // bp
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 __int8 v22; // al
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);
v5 = 5;
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 88LL, 255LL, 10000000LL) != 1 )
{
v6 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 140 PortStatus %04X\n", v6);
v7 = 2;
do
{
Parport_WriteControl((unsigned int)fd, 4LL);
v8 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 150 0x04 PortStatus %04X\n", v8);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v9 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 151 0x04 PortStatus %04X\n", v9);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 170LL);
v10 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 155 0x04 PortStatus %04X\n", v10);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 85LL);
v11 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 156 0x04 PortStatus %04X\n", v11);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 0LL);
v12 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 157 0x04 PortStatus %04X\n", v12);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 255LL);
v13 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 200 PortStatus %04X\n", v13);
v14 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 220 0x04 PortStatus %04X\n", v14);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 12LL);
--v7;
}
while ( v7 );
v15 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 250 PortStatus %04X\n", v15);
Parport_WriteData((unsigned int)fd, 40LL);
v16 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 400 0x04 PortStatus %04X\n", v16);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
v17 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 420 out 0x28 PortStatus %04X\n", v17);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
Parport_WriteControl((unsigned int)fd, 6LL);
v18 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 450 ctrl 0x06 PortStatus %04X\n", v18);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 600 PortMode %04X\n", (unsigned int)v24[0]);
v19 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 630 PortStatus %04X\n", v19);
v5 = 5;
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 56LL, 255LL, 10000000LL) != 1 )
{
Parport_WriteControl((unsigned int)fd, 7LL);
v20 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 650 ctrl 0x07 PortStatus %04X\n", v20);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v21 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 670 ctrl 0x04 PortStatus %04X\n", v21);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 216LL, 255LL, 10000000LL) != 1 )
{
v22 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 700 PortStatus %04X\n", v22);
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 900 PortMode %04X\n", (unsigned int)v24[0]);
v5 = Send_PaperPacket((unsigned int)fd);
if ( (_BYTE)v5 )
{
v5 = 5;
}
else
{
fwrite("hlcapt:InitPrinter Point 1500\n", 1uLL, 0x1EuLL, stderr);
PrintBuffer();
}
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 fd)
{
unsigned __int8 v1; // al
unsigned __int8 v2; // al
unsigned __int8 v3; // al
unsigned __int8 v4; // al
unsigned int v5; // ebp
unsigned __int8 v6; // al
char v7; // bp
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 __int8 v22; // al
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);
v5 = 5;
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 88LL, 255LL, 10000000LL) != 1 )
{
v6 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 140 PortStatus %04X\n", v6);
v7 = 2;
do
{
Parport_WriteControl((unsigned int)fd, 4LL);
v8 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 150 0x04 PortStatus %04X\n", v8);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v9 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 151 0x04 PortStatus %04X\n", v9);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 170LL);
v10 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 155 0x04 PortStatus %04X\n", v10);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 85LL);
v11 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 156 0x04 PortStatus %04X\n", v11);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 0LL);
v12 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 157 0x04 PortStatus %04X\n", v12);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteData((unsigned int)fd, 255LL);
v13 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 200 PortStatus %04X\n", v13);
v14 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 220 0x04 PortStatus %04X\n", v14);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 12LL);
--v7;
}
while ( v7 );
v15 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 250 PortStatus %04X\n", v15);
Parport_WriteData((unsigned int)fd, 40LL);
v16 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 400 0x04 PortStatus %04X\n", v16);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
v17 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 420 out 0x28 PortStatus %04X\n", v17);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
Parport_WriteControl((unsigned int)fd, 6LL);
v18 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 450 ctrl 0x06 PortStatus %04X\n", v18);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Port_Sleep();
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 600 PortMode %04X\n", (unsigned int)v24[0]);
v19 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 630 PortStatus %04X\n", v19);
v5 = 5;
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 56LL, 255LL, 10000000LL) != 1 )
{
Parport_WriteControl((unsigned int)fd, 7LL);
v20 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 650 ctrl 0x07 PortStatus %04X\n", v20);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
Parport_WriteControl((unsigned int)fd, 4LL);
v21 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 670 ctrl 0x04 PortStatus %04X\n", v21);
Parport_MonitorStatus((unsigned int)fd, 1000LL);
if ( (unsigned __int8)WaitForStatus((unsigned int)fd, 216LL, 255LL, 10000000LL) != 1 )
{
v22 = Parport_ReadStatus((unsigned int)fd);
fprintf(stderr, "hlcapt:InitPrinter 700 PortStatus %04X\n", v22);
ioctl(fd, 0x80047098uLL, v24);
fprintf(stderr, "hlcapt:InitPrinter 900 PortMode %04X\n", (unsigned int)v24[0]);
v5 = Send_PaperPacket((unsigned int)fd);
if ( (_BYTE)v5 )
{
v5 = 5;
}
else
{
fwrite("hlcapt:InitPrinter Point 1500\n", 1uLL, 0x1EuLL, stderr);
PrintBuffer();
}
}
}
}
return v5;
}

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; // dl
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; // dl
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)
{
char v1; // dl
__int64 result; // rax
char v3; // dl
char v4; // dl
char v5; // dl
char v6; // dl
char v7; // dl
char v8; // dl
char v9; // dl
char v10; // dl
char v11; // dl
char v12; // dl
char v13; // dl
char v14; // dl
char v15; // dl

v1 = Send_Packet_a1_a1();
result = 5LL;
if ( !v1 )
{
v3 = Send_Packet_a0_a2(a1);
result = 5LL;
if ( !v3 )
{
v4 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v4 )
{
v5 = Send_PaperPacket(a1);
result = 5LL;
if ( !v5 )
{
v6 = Send_Packet_a4_e0(a1);
result = 5LL;
if ( !v6 )
{
v7 = Send_Packet_a5_e0(a1);
result = 5LL;
if ( !v7 )
{
v8 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v8 )
{
v9 = Send_Packet_a0_a0(a1);
result = 5LL;
if ( !v9 )
{
v10 = Send_PaperPacket(a1);
result = 5LL;
if ( !v10 )
{
v11 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v11 )
{
v12 = Send_Packet_a0_d0(a1);
result = 5LL;
if ( !v12 )
{
v13 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v13 )
{
v14 = Send_Packet_a1_d0(a1);
result = 5LL;
if ( !v14 )
{
v15 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v15 )
{
result = Send_Packet_a0_a0(a1);
if ( (_BYTE)result )
result = 5LL;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
char v1; // dl
__int64 result; // rax
char v3; // dl
char v4; // dl
char v5; // dl
char v6; // dl
char v7; // dl
char v8; // dl
char v9; // dl
char v10; // dl
char v11; // dl
char v12; // dl
char v13; // dl
char v14; // dl
char v15; // dl

v1 = Send_Packet_a1_a1();
result = 5LL;
if ( !v1 )
{
v3 = Send_Packet_a0_a2(a1);
result = 5LL;
if ( !v3 )
{
v4 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v4 )
{
v5 = Send_PaperPacket(a1);
result = 5LL;
if ( !v5 )
{
v6 = Send_Packet_a4_e0(a1);
result = 5LL;
if ( !v6 )
{
v7 = Send_Packet_a5_e0(a1);
result = 5LL;
if ( !v7 )
{
v8 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v8 )
{
v9 = Send_Packet_a0_a0(a1);
result = 5LL;
if ( !v9 )
{
v10 = Send_PaperPacket(a1);
result = 5LL;
if ( !v10 )
{
v11 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v11 )
{
v12 = Send_Packet_a0_d0(a1);
result = 5LL;
if ( !v12 )
{
v13 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v13 )
{
v14 = Send_Packet_a1_d0(a1);
result = 5LL;
if ( !v14 )
{
v15 = Send_InterBandPacket(a1);
result = 5LL;
if ( !v15 )
{
result = Send_Packet_a0_a0(a1);
if ( (_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; // bp
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; // bp
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);
}


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

v4[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");
close(v2);
v2 = -1;
}
else
{
ioctl(v2, 0x80047098uLL, v4);
fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", v4[0]);
}
return v2;
}

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

v4[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");
close(v2);
v2 = -1;
}
else
{
ioctl(v2, 0x80047098uLL, v4);
fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", v4[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


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; // dl
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; // dl
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; // ebx
_BYTE v9[72]; // [rsp+0h] [rbp-48h] BYREF

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

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

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

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 i; // rbx

fwrite("RETBUF = [", 1uLL, 0xAuLL, stderr);
for ( i = 1LL; ; ++i )
{
fprintf(stderr, "%02x", (unsigned __int8)RETBUF[i - 1]);
if ( i == 20 )
break;
if ( (int)i > 0 )
fwrite(", ", 1uLL, 2uLL, stderr);
}
return fwrite("]\n", 1uLL, 2uLL, stderr);
}

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

fwrite("RETBUF = [", 1uLL, 0xAuLL, stderr);
for ( i = 1LL; ; ++i )
{
fprintf(stderr, "%02x", (unsigned __int8)RETBUF[i - 1]);
if ( i == 20 )
break;
if ( (int)i > 0 )
fwrite(", ", 1uLL, 2uLL, stderr);
}
return fwrite("]\n", 1uLL, 2uLL, stderr);
}

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, unsigned int a3, unsigned int a4)
{
__int64 result; // rax
char v5; // r13
unsigned __int8 v7; // r12
unsigned __int8 *v8; // rbp
__int64 v9; // r13
int v10; // er13
unsigned __int8 v11; // bp
char v12; // al
char v13; // dl
char v14; // dl

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

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int8 *a2, unsigned int a3, unsigned int a4)
{
__int64 result; // rax
char v5; // r13
unsigned __int8 v7; // r12
unsigned __int8 *v8; // rbp
__int64 v9; // r13
int v10; // er13
unsigned __int8 v11; // bp
char v12; // al
char v13; // dl
char v14; // dl

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

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
unsigned int v2; // ebx
char v4; // bl

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

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

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

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; // ebp
unsigned int v2; // ebx

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

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

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

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

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

do
{
while ( 1 )
{
if ( (unsigned __int8)Send_InterBandPacket(a1) )
return 5LL;
if ( (RETBUF[5] & 1) != 0 )
break;
result = (unsigned __int8)RETBUF[4];
if ( !(_BYTE)result )
return result;
}
}
while ( !(unsigned __int8)Send_Packet_a0_a0(a1) );
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 ( 1 )
{
do
{
if ( (unsigned __int8)Send_InterBandPacket((unsigned int)a1) )
return 5LL;
}
while ( (RETBUF[5] & 1) == 0 );
if ( (unsigned __int8)Send_Packet_a0_a0((unsigned int)a1) )
break;
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 ( 1 )
{
do
{
if ( (unsigned __int8)Send_InterBandPacket((unsigned int)a1) )
return 5LL;
}
while ( (RETBUF[5] & 1) == 0 );
if ( (unsigned __int8)Send_Packet_a0_a0((unsigned int)a1) )
break;
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)
{
char v2; // dl
__int64 result; // rax
char v4; // dl

v2 = WaitForReady(a1);
result = 5LL;
if ( !v2 )
{
v4 = Send_StartDataPacket(a1, a2);
result = 5LL;
if ( !v4 )
{
Parport_WriteData(a1, 67LL);
Parport_WriteControl(a1, 5LL);
result = CheckStatus(a1, 0x78u, 0xFFu, 10000000LL);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, unsigned int a2)
{
char v2; // dl
__int64 result; // rax
char v4; // dl

v2 = func6(a1);
result = 5LL;
if ( !v2 )
{
v4 = Send_StartDataPacket(a1, a2);
result = 5LL;
if ( !v4 )
{
Parport_WriteData(a1, 67LL);
Parport_WriteControl(a1, 5LL);
result = func1(a1, 0x78u, 0xFFu, 10000000LL);
}
}
return result;
}

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, _BYTE *a2, int a3, unsigned __int8 a4, int a5)
{
int v5; // er12
__int64 result; // rax
_BYTE *v7; // r14
unsigned __int8 v8; // r15
int i; // ebx
unsigned __int8 v10; // [rsp+7h] [rbp-41h]
_BYTE *v11; // [rsp+8h] [rbp-40h]

v10 = a4;
v5 = 1;
if ( a5 )
v5 = a5;
result = 0xFFFFFFFFLL;
if ( a3 )
{
v7 = a2;
v8 = a4;
v11 = &a2[a3];
do
{
v8 ^= *v7++ ^ v10;
for ( i = 0; i != v5; ++i )
Parport_WriteData(a1, v8);
}
while ( v11 != v7 );
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, _BYTE *a2, int a3, unsigned __int8 a4, int a5)
{
int v5; // er12
__int64 result; // rax
_BYTE *v7; // r14
unsigned __int8 v8; // r15
int i; // ebx
unsigned __int8 v10; // [rsp+7h] [rbp-41h]
_BYTE *v11; // [rsp+8h] [rbp-40h]

v10 = a4;
v5 = 1;
if ( a5 )
v5 = a5;
result = 0xFFFFFFFFLL;
if ( a3 )
{
v7 = a2;
v8 = a4;
v11 = &a2[a3];
do
{
v8 ^= *v7++ ^ v10;
for ( i = 0; i != v5; ++i )
Parport_WriteData(a1, v8);
}
while ( v11 != v7 );
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);
}