recognize

decompiled_code:
__int64 __fastcall recognize(int **a1)
{
unsigned int v1; // er13
__int64 v2; // r15
__int64 v3; // r14
int v4; // er12
int v5; // ebp
__int64 i; // rbx
int v7; // eax

v1 = 46;
if ( (int)maxWidth(a1) > 5 )
{
v2 = apply(preprocessor, a1);
calcSlopes(v2);
v3 = models[(unsigned int)getBank()];
samples = v3;
v4 = npatterns[(unsigned int)getBank()];
unknown = v2;
v1 = *(unsigned __int8 *)(*(_QWORD *)v3 + 20LL);
v5 = distance(0);
if ( v4 > 1 )
{
for ( i = 1LL; i != v4; ++i )
{
v7 = distance(i);
if ( v7 < v5 )
{
v1 = *(unsigned __int8 *)(*(_QWORD *)(v3 + 8 * i) + 20LL);
v5 = v7;
}
}
}
deletePattern(v2);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func12(int **a1)
{
unsigned int v1; // er13
__int64 v2; // r15
__int64 v3; // r14
int v4; // er12
int v5; // ebp
__int64 i; // rbx
int v7; // eax

v1 = 46;
if ( (int)func4(a1) > 5 )
{
v2 = apply(preprocessor, a1);
calcSlopes(v2);
v3 = models[(unsigned int)func1()];
samples = v3;
v4 = npatterns[(unsigned int)func1()];
unknown = v2;
v1 = *(unsigned __int8 *)(*(_QWORD *)v3 + 20LL);
v5 = func11(0);
if ( v4 > 1 )
{
for ( i = 1LL; i != v4; ++i )
{
v7 = func11(i);
if ( v7 < v5 )
{
v1 = *(unsigned __int8 *)(*(_QWORD *)(v3 + 8 * i) + 20LL);
v5 = v7;
}
}
}
deletePattern(v2);
}
return v1;
}

original_code:
unsigned char
recognize( Pattern *p )
{
    unsigned char ascii;
    int mind, tmpd, i, nsamples;
    Pattern *np;
    if ( maxWidth( p ) < 6 )
 return '.';
    np = apply( preprocessor, p );
    calcSlopes( np );
    samples = models[ getBank() ];
    nsamples = npatterns[ getBank() ];
    unknown = np;
    ascii = samples[0]->ascii;
    mind = distance( 0 );
    for ( i = 1; i < nsamples; i++ )
    {
 tmpd = distance( i );
 if ( tmpd < mind )
 {
     mind = tmpd;
     ascii = samples[i]->ascii;
 }
    }
    deletePattern( np );
    return ascii;
}

canonical_original_code:
unsigned char recognize(struct pattern *p)
{
    unsigned char ascii;
    int mind; int tmpd; int i; int nsamples;
    struct pattern *np;
    if ( maxWidth( p ) < 6 )
 return '.';
    np = apply( preprocessor, p );
    calcSlopes( np );
    samples = models[ getBank() ];
    nsamples = npatterns[ getBank() ];
    unknown = np;
    ascii = samples[0]->ascii;
    mind = distance( 0 );
    for ( i = 1; i < nsamples; i++ )
    {
 tmpd = distance( i );
 if ( tmpd < mind )
 {
     mind = tmpd;
     ascii = samples[i]->ascii;
 }
    }
    deletePattern( np );
    return ascii;
}

variable_types:
ascii
unsigned char

variable_types: mind int

variable_types: tmpd int

variable_types: nsamples int

return_type: unsigned char


initWizard

repo: peffis/xmerlin

decompiled_code:
void initWizard()
{
int v0; // ebp
__int64 i; // rbx
const char *v2; // rdx

preprocessor = newPreprocessor(&off_9F0, 3LL);
loadPatterns();
v0 = 1;
for ( i = 0LL; ; ++i )
{
if ( !(_DWORD)i )
{
v2 = "NATURAL";
LABEL_3:
printf("Found %d patterns in %s bank\n", (unsigned int)npatterns[i], v2);
goto LABEL_4;
}
if ( (_DWORD)i == 1 )
{
v2 = "NUMERAL";
goto LABEL_3;
}
printf("Found %d patterns in %s bank\n", (unsigned int)npatterns[i], "EXTENDED");
if ( v0 > 2 )
break;
LABEL_4:
++v0;
}
setBank(0);
}

canonical_decompiled_code:
void func15()
{
int v0; // ebp
__int64 i; // rbx
const char *v2; // rdx

preprocessor = newPreprocessor(&off_9F0, 3LL);
func14();
v0 = 1;
for ( i = 0LL; ; ++i )
{
if ( !(_DWORD)i )
{
v2 = "NATURAL";
LABEL_3:
printf("Found %d patterns in %s bank\n", (unsigned int)npatterns[i], v2);
goto LABEL_4;
}
if ( (_DWORD)i == 1 )
{
v2 = "NUMERAL";
goto LABEL_3;
}
printf("Found %d patterns in %s bank\n", (unsigned int)npatterns[i], "EXTENDED");
if ( v0 > 2 )
break;
LABEL_4:
++v0;
}
func0(0);
}

original_code:
void
initWizard()
{
    int i;
    preprocessor = newPreprocessor( fs, 3 );
    loadPatterns();
    for ( i = 0; i < 3; i++ )
 printf("Found %d patterns in %s bank\n",
        npatterns[i],
        (i == 0 ? "NATURAL" : (i == 1 ? "NUMERAL" : "EXTENDED")));
    setBank( NATURAL );
}

variable_types: i int

return_type: void


sizeNormFilter

original_code:
Pattern *
sizeNormFilter( Pattern *p )
{
    int i, xmin, xmax, ymin, ymax;
    float xm, ym, dx, dy, delta, centerx, centery;
    Point tmp, po;
    Pattern *np;
    if ( p == ((void *)0) )
 return ((void *)0);
    np = newPattern( p->size, p->ascii );
    if ( np == ((void *)0) )
 return ((void *)0);
    tmp = p->pv[ 0 ];
    xmin = xmax = tmp.x;
    ymin = ymax = tmp.y;
    for ( i = 1; i < p->nPoints; i++ )
    {
 tmp = p->pv[ i ];
 if ( tmp.x < xmin )
     xmin = tmp.x;
 if ( tmp.y < ymin )
     ymin = tmp.y;
 if ( tmp.x > xmax )
     xmax = tmp.x;
 if ( tmp.y > ymax )
     ymax = tmp.y;
    }
    xm = ( xmax + xmin ) / 2.0;
    ym = ( ymax + ymin ) / 2.0;
    dx = ( xmax - xmin ) / 2.0;
    dy = ( ymax - ymin ) / 2.0;
    delta = ( (dx) > (dy) ? (dx) : (dy));
    centerx = (100 - 1) / 2.0;
    centery = (100 - 1) / 2.0;
    for ( i = 0; i < p->nPoints; i++ )
    {
 tmp = p->pv[ i ];
 po.x = (int) ( centerx + centerx * (( tmp.x - xm ) / delta ));
 po.y = (int) ( centery + centery * (( tmp.y - ym ) / delta ));
 addPoint( np, po );
    }
    return np;
}

canonical_original_code:
struct pattern *sizeNormFilter(struct pattern *p)
{
    int i; int xmin; int xmax; int ymin; int ymax;
    float xm; float ym; float dx; float dy; float delta; float centerx; float centery;
    struct point tmp; struct point po;
    struct pattern *np;
    if ( p == ((void *)0) )
 return ((void *)0);
    np = newPattern( p->size, p->ascii );
    if ( np == ((void *)0) )
 return ((void *)0);
    tmp = p->pv[ 0 ];
    xmin = xmax = tmp.x;
    ymin = ymax = tmp.y;
    for ( i = 1; i < p->nPoints; i++ )
    {
 tmp = p->pv[ i ];
 if ( tmp.x < xmin )
     xmin = tmp.x;
 if ( tmp.y < ymin )
     ymin = tmp.y;
 if ( tmp.x > xmax )
     xmax = tmp.x;
 if ( tmp.y > ymax )
     ymax = tmp.y;
    }
    xm = ( xmax + xmin ) / 2.0;
    ym = ( ymax + ymin ) / 2.0;
    dx = ( xmax - xmin ) / 2.0;
    dy = ( ymax - ymin ) / 2.0;
    delta = ( (dx) > (dy) ? (dx) : (dy));
    centerx = (100 - 1) / 2.0;
    centery = (100 - 1) / 2.0;
    for ( i = 0; i < p->nPoints; i++ )
    {
 tmp = p->pv[ i ];
 po.x = (int) ( centerx + centerx * (( tmp.x - xm ) / delta ));
 po.y = (int) ( centery + centery * (( tmp.y - ym ) / delta ));
 addPoint( np, po );
    }
    return np;
}

variable_types:
p
struct pattern *

variable_types: xmin int

variable_types: xmax int

variable_types: ymin int

variable_types: ymax int

variable_types: xm float

variable_types: ym float

variable_types: dx float

variable_types: dy float

variable_types: delta float

variable_types: centerx float

variable_types: centery float

variable_types: struct pattern { struct point { int x; int y; char isLast; } *pv; int *s; int nPoints; unsigned char ascii; int size; } *

user_defined_types: struct pattern { struct point *pv; int *s; int nPoints; unsigned char ascii; int size; }


distanceSq

decompiled_code:
__int64 __fastcall distanceSq(__int64 a1, __int64 a2, __int64 a3)
{
return (unsigned int)((a1 - a3) * (a1 - a3) + (HIDWORD(a1) - HIDWORD(a3)) * (HIDWORD(a1) - HIDWORD(a3)));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3)
{
return (unsigned int)((a1 - a3) * (a1 - a3) + (HIDWORD(a1) - HIDWORD(a3)) * (HIDWORD(a1) - HIDWORD(a3)));
}

original_code:
int
distanceSq( Point p0, Point p1 )
{
    return (( p0.x - p1.x ) * ( p0.x - p1.x ))
 + (( p0.y - p1.y ) * ( p0.y - p1.y ));
}

canonical_original_code:
int distanceSq(struct point p0, struct point p1)
{
    return (( p0.x - p1.x ) * ( p0.x - p1.x ))
 + (( p0.y - p1.y ) * ( p0.y - p1.y ));
}

variable_types: struct point { int x; int y; char isLast; }

return_type: int


pointDistanceNormFilter

original_code:
Pattern *
pointDistanceNormFilter( Pattern *p )
{
    Pattern *np;
    Point last, tmp;
    int i;
    if ( p == ((void *)0) )
 return ((void *)0);
    np = newPattern( p->size, p->ascii );
    if ( np == ((void *)0) )
 return ((void *)0);
    last = p->pv[ 0 ];
    addPoint( np, last );
    for ( i = 1; i < p->nPoints; i++ )
    {
 tmp = p->pv[ i ];
 if ( distanceSq( tmp, last ) >= 36 )
 {
     last = tmp;
     addPoint( np, last );
 }
    }
    return np;
}

canonical_original_code:
struct pattern *pointDistanceNormFilter(struct pattern *p)
{
    struct pattern *np;
    struct point last; struct point tmp;
    int i;
    if ( p == ((void *)0) )
 return ((void *)0);
    np = newPattern( p->size, p->ascii );
    if ( np == ((void *)0) )
 return ((void *)0);
    last = p->pv[ 0 ];
    addPoint( np, last );
    for ( i = 1; i < p->nPoints; i++ )
    {
 tmp = p->pv[ i ];
 if ( distanceSq( tmp, last ) >= 36 )
 {
     last = tmp;
     addPoint( np, last );
 }
    }
    return np;
}

return_type: struct pattern { struct point { int x; int y; char isLast; } *pv; int *s; int nPoints; unsigned char ascii; int size; } *


smoothFilter

original_code:
Pattern *
smoothFilter( Pattern *p )
{
    Pattern *np;
    Point p1, p2, p3, tmp;
    int i;
    if ( p == ((void *)0) )
 return ((void *)0);
    np = newPattern( p->size, p->ascii );
    if ( np == ((void *)0) )
 return ((void *)0);
    addPoint( np, p->pv[ 0 ] );
    for ( i = 1; i < p->nPoints - 1; i++ )
    {
 p1 = p->pv[ i - 1 ];
 p2 = p->pv[ i ];
 p3 = p->pv[ i + 1 ];
 tmp.x = ( p1.x + p2.x + p3.x ) / 3;
 tmp.y = ( p1.y + p2.y + p3.y ) / 3;
 addPoint( np, tmp );
    }
    addPoint( np, p->pv[ p->nPoints - 1 ] );
    return np;
}

canonical_original_code:
struct pattern *smoothFilter(struct pattern *p)
{
    struct pattern *np;
    struct point p1; struct point p2; struct point p3; struct point tmp;
    int i;
    if ( p == ((void *)0) )
 return ((void *)0);
    np = newPattern( p->size, p->ascii );
    if ( np == ((void *)0) )
 return ((void *)0);
    addPoint( np, p->pv[ 0 ] );
    for ( i = 1; i < p->nPoints - 1; i++ )
    {
 p1 = p->pv[ i - 1 ];
 p2 = p->pv[ i ];
 p3 = p->pv[ i + 1 ];
 tmp.x = ( p1.x + p2.x + p3.x ) / 3;
 tmp.y = ( p1.y + p2.y + p3.y ) / 3;
 addPoint( np, tmp );
    }
    addPoint( np, p->pv[ p->nPoints - 1 ] );
    return np;
}

user_defined_types: struct point { int x; int y; char isLast; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
initGraphics(argc, argv, envp);
initWizard();
current = newPattern(100LL, 97LL);
setBank(0LL);
setMode(0LL);
eventLoop();
return 0;
}

original_code:
int
main(int argc, char **argv)
{
    initGraphics( argc, argv );
    initWizard();
    current = newPattern( 100, 'a' );
    setBank( NATURAL );
    setMode( SINGLE_STROKE );
    eventLoop();
    return 0;
}

variable_types: argc int

variable_types:
argv
char **


assignDefault

decompiled_code:
void assignDefault()
{
window_y = 0;
window_x = 0;
window_width = 200;
window_height = 100;
}

canonical_decompiled_code:
void func0()
{
window_y = 0;
window_x = 0;
window_width = 200;
window_height = 100;
}

original_code:
void
assignDefault()
{
    window_x = window_y = 0;
    window_width = 200;
    window_height = 100;
}


parseArgs

decompiled_code:
_BOOL8 __fastcall parseArgs(int a1, __int64 a2)
{
_BOOL4 v2; // ebx
bool v3; // zf
char v5; // [rsp+Eh] [rbp-Ah] BYREF
char v6; // [rsp+Fh] [rbp-9h] BYREF

v2 = 1;
if ( a1 == 3 )
{
v3 = strcmp("-geometry", *(const char **)(a2 + 8)) == 0;
v2 = !v3;
if ( v3 )
{
if ( (unsigned int)_isoc99_sscanf(
*(_QWORD *)(a2 + 16),
"%dx%d%c%d%c%d\n",
&window_width,
&window_height,
&v6,
&window_x,
&v5,
&window_y) == 6 )
{
if ( v6 == 45 )
window_x = myDisplay->screens[(__int64)myScreen].width - window_width - window_x;
if ( v5 == 45 )
window_y = myDisplay->screens[(__int64)myScreen].height - window_height - window_y;
}
else
{
v2 = 1;
}
}
else
{
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(int a1, __int64 a2)
{
_BOOL4 v2; // ebx
bool v3; // zf
char v5; // [rsp+Eh] [rbp-Ah] BYREF
char v6; // [rsp+Fh] [rbp-9h] BYREF

v2 = 1;
if ( a1 == 3 )
{
v3 = strcmp("-geometry", *(const char **)(a2 + 8)) == 0;
v2 = !v3;
if ( v3 )
{
if ( (unsigned int)_isoc99_sscanf(
*(_QWORD *)(a2 + 16),
"%dx%d%c%d%c%d\n",
&window_width,
&window_height,
&v6,
&window_x,
&v5,
&window_y) == 6 )
{
if ( v6 == 45 )
window_x = myDisplay->screens[(__int64)myScreen].width - window_width - window_x;
if ( v5 == 45 )
window_y = myDisplay->screens[(__int64)myScreen].height - window_height - window_y;
}
else
{
v2 = 1;
}
}
else
{
v2 = 1;
}
}
return v2;
}

original_code:
int
parseArgs( int argc, char *argv[])
{
    char xsign, ysign;
    if ( argc != 3 || strcmp( "-geometry", argv[1] ) )
 return 1;
    if ( sscanf( argv[2], "%dx%d%c%d%c%d\n", &window_width,
   &window_height,
   &xsign, &window_x, &ysign, &window_y ) != 6 )
     return 1;
    if ( xsign == '-' )
    {
 int screen_width = (((&((_XPrivDisplay)(myDisplay))->screens[myScreen]))->width);
 window_x = screen_width - window_width - window_x;
    }
    if ( ysign == '-' )
    {
 int screen_height = (((&((_XPrivDisplay)(myDisplay))->screens[myScreen]))->height);
 window_y = screen_height - window_height - window_y;
    }
    return 0;
}

canonical_original_code:
int parseArgs(int argc, char *argv[])
{
    char xsign; char ysign;
    if ( argc != 3 || strcmp( "-geometry", argv[1] ) )
 return 1;
    if ( sscanf( argv[2], "%dx%d%c%d%c%d\n", &window_width,
   &window_height,
   &xsign, &window_x, &ysign, &window_y ) != 6 )
     return 1;
    if ( xsign == '-' )
    {
 int screen_width = (((&((_XPrivDisplay)(myDisplay))->screens[myScreen]))->width);
 window_x = screen_width - window_width - window_x;
    }
    if ( ysign == '-' )
    {
 int screen_height = (((&((_XPrivDisplay)(myDisplay))->screens[myScreen]))->height);
 window_y = screen_height - window_height - window_y;
    }
    return 0;
}

variable_types:
argv
char *[]

variable_types: xsign char

variable_types: ysign char


drawText

decompiled_code:
int drawText()
{
unsigned __int64 v0; // rax

XDrawString(myDisplay, myWindow, myGC, 3, 16, "abc", 3);
XDrawString(myDisplay, myWindow, myGC, (int)window_width / 2 - 12, 16, "ABC", 3);
XDrawString(myDisplay, myWindow, myGC, window_width - 28, 16, "123", 3);
v0 = getColor(myDisplay, 60, 120, 200);
XSetForeground(myDisplay, myGC, v0);
XDrawString(myDisplay, myWindow, myGC, 2, 15, "abc", 3);
XDrawString(myDisplay, myWindow, myGC, (int)window_width / 2 - 13, 15, "ABC", 3);
XDrawString(myDisplay, myWindow, myGC, window_width - 29, 15, "123", 3);
return XSetForeground(myDisplay, myGC, myDisplay->screens[(__int64)myScreen].black_pixel);
}

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

XDrawString(myDisplay, myWindow, myGC, 3, 16, "abc", 3);
XDrawString(myDisplay, myWindow, myGC, (int)window_width / 2 - 12, 16, "ABC", 3);
XDrawString(myDisplay, myWindow, myGC, window_width - 28, 16, "123", 3);
v0 = func2(myDisplay, 60, 120, 200);
XSetForeground(myDisplay, myGC, v0);
XDrawString(myDisplay, myWindow, myGC, 2, 15, "abc", 3);
XDrawString(myDisplay, myWindow, myGC, (int)window_width / 2 - 13, 15, "ABC", 3);
XDrawString(myDisplay, myWindow, myGC, window_width - 29, 15, "123", 3);
return XSetForeground(myDisplay, myGC, myDisplay->screens[(__int64)myScreen].black_pixel);
}

original_code:
void
drawText()
{
    XDrawString( myDisplay, myWindow, myGC, 2+1, 15+1,
   "abc", strlen("abc"));
    XDrawString( myDisplay, myWindow, myGC, window_width/2 - 13 + 1, 15 + 1,
   "ABC", strlen("ABC"));
    XDrawString( myDisplay, myWindow, myGC, window_width - 3*9 - 2 + 1, 15 + 1,
   "123", strlen("123"));
    XSetForeground (myDisplay, myGC, getColor( myDisplay, 60, 120, 200 ) );
    XDrawString( myDisplay, myWindow, myGC, 2, 15,
   "abc", strlen("abc"));
    XDrawString( myDisplay, myWindow, myGC, window_width/2 - 13, 15,
   "ABC", strlen("ABC"));
    XDrawString( myDisplay, myWindow, myGC, window_width - 3*9 - 2, 15,
   "123", strlen("123"));
    XSetForeground (myDisplay, myGC, ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->black_pixel) );
}


paintPattern

decompiled_code:
void paintPattern()
{
__int64 *v0; // rdx
__int64 v1; // rbx
int v2; // ebp
__int64 v3; // rdx

v0 = (__int64 *)current;
if ( current && *(int *)(current + 16LL) > 1 )
{
v1 = 0LL;
v2 = 0;
do
{
v3 = *v0;
if ( !*(_BYTE *)(v3 + v1 + 8) )
XDrawLine(
myDisplay,
myWindow,
myGC,
*(_DWORD *)(v3 + v1),
*(_DWORD *)(v3 + v1 + 4),
*(_DWORD *)(v3 + v1 + 12),
*(_DWORD *)(v3 + v1 + 16));
++v2;
v0 = (__int64 *)current;
v1 += 12LL;
}
while ( *(_DWORD *)(current + 16LL) - 1 > v2 );
}
}

original_code:
void
paintPattern()
{
    int i;
    if ( current != ((void *)0) && current->nPoints > 1 )
    {
 for ( i = 0; i < current->nPoints - 1; i++ )
     if ( !current->pv[i].isLast )
  XDrawLine( myDisplay, myWindow, myGC,
      current->pv[i].x, current->pv[i].y,
      current->pv[i+1].x, current->pv[i+1].y );
    }
}

variable_types: i int


paint

decompiled_code:
void paint()
{
XClearWindow(myDisplay, myWindow);
XSetLineAttributes(myDisplay, myGC, 1u, 1, 0, 0);
XDrawLine(myDisplay, myWindow, myGC, (int)window_width / 2, 20, (int)window_width / 2, window_height);
XSetLineAttributes(myDisplay, myGC, 1u, 0, 0, 0);
paintPattern();
if ( myFont )
drawText();
}

canonical_decompiled_code:
void func5()
{
XClearWindow(myDisplay, myWindow);
XSetLineAttributes(myDisplay, myGC, 1u, 1, 0, 0);
XDrawLine(myDisplay, myWindow, myGC, (int)window_width / 2, 20, (int)window_width / 2, window_height);
XSetLineAttributes(myDisplay, myGC, 1u, 0, 0, 0);
func4();
if ( myFont )
func3();
}

original_code:
void
paint( void )
{
    XClearWindow( myDisplay, myWindow );
    XSetLineAttributes( myDisplay, myGC, 1, 1,
   0, 0 );
    XDrawLine( myDisplay, myWindow, myGC, window_width / 2,
        20, window_width / 2, window_height );
    XSetLineAttributes( myDisplay, myGC, 1, 0,
   0, 0 );
    paintPattern();
    if ( myFont != ((void *)0) )
 drawText();
}

canonical_original_code:
void paint()
{
    XClearWindow( myDisplay, myWindow );
    XSetLineAttributes( myDisplay, myGC, 1, 1,
   0, 0 );
    XDrawLine( myDisplay, myWindow, myGC, window_width / 2,
        20, window_width / 2, window_height );
    XSetLineAttributes( myDisplay, myGC, 1, 0,
   0, 0 );
    paintPattern();
    if ( myFont != ((void *)0) )
 drawText();
}


initGraphics

decompiled_code:
int __fastcall initGraphics(int a1, __int64 a2)
{
Display *v2; // rax
int v3; // er12
XFontStruct *v4; // rax
XGCValues v6; // [rsp+0h] [rbp-108h] BYREF
XSetWindowAttributes v7; // [rsp+80h] [rbp-88h] BYREF

v2 = XOpenDisplay(&byte_717);
myDisplay = v2;
if ( !v2 )
exit(0);
myScreen = v2->default_screen;
v3 = v2->screens[(__int64)myScreen].root_depth;
if ( parseArgs(a1, a2) )
assignDefault();
v7.border_pixel = myDisplay->screens[(__int64)myScreen].black_pixel;
v7.background_pixel = getColor(myDisplay, 252, 234, 212);
v7.override_redirect = 0;
myWindow = XCreateWindow(
myDisplay,
myDisplay->screens[(__int64)myScreen].root,
window_x,
window_y,
window_width,
window_height,
1u,
v3,
1u,
0LL,
0x20AuLL,
&v7);
myGC = XCreateGC(myDisplay, myWindow, 0LL, &v6);
if ( !myGC )
{
XDestroyWindow(myDisplay, myScreen);
exit(0);
}
v4 = XLoadQueryFont(myDisplay, "9x15");
myFont = (__int64)v4;
if ( v4 )
{
XSetFont(myDisplay, myGC, v4->fid);
}
else
{
fwrite("Cannot get font:9x15.\n", 1uLL, 0x16uLL, stderr);
myFont = 0LL;
}
XSetForeground(myDisplay, myGC, myDisplay->screens[(__int64)myScreen].black_pixel);
XMapWindow(myDisplay, myWindow);
XLowerWindow(myDisplay, myWindow);
paint();
return XFlush(myDisplay);
}

canonical_decompiled_code:
int __fastcall func6(int a1, __int64 a2)
{
Display *v2; // rax
int v3; // er12
XFontStruct *v4; // rax
XGCValues v6; // [rsp+0h] [rbp-108h] BYREF
XSetWindowAttributes v7; // [rsp+80h] [rbp-88h] BYREF

v2 = XOpenDisplay(&byte_717);
myDisplay = v2;
if ( !v2 )
exit(0);
myScreen = v2->default_screen;
v3 = v2->screens[(__int64)myScreen].root_depth;
if ( func1(a1, a2) )
func0();
v7.border_pixel = myDisplay->screens[(__int64)myScreen].black_pixel;
v7.background_pixel = func2(myDisplay, 252, 234, 212);
v7.override_redirect = 0;
myWindow = XCreateWindow(
myDisplay,
myDisplay->screens[(__int64)myScreen].root,
window_x,
window_y,
window_width,
window_height,
1u,
v3,
1u,
0LL,
0x20AuLL,
&v7);
myGC = XCreateGC(myDisplay, myWindow, 0LL, &v6);
if ( !myGC )
{
XDestroyWindow(myDisplay, myScreen);
exit(0);
}
v4 = XLoadQueryFont(myDisplay, "9x15");
myFont = (__int64)v4;
if ( v4 )
{
XSetFont(myDisplay, myGC, v4->fid);
}
else
{
fwrite("Cannot get font:9x15.\n", 1uLL, 0x16uLL, stderr);
myFont = 0LL;
}
XSetForeground(myDisplay, myGC, myDisplay->screens[(__int64)myScreen].black_pixel);
XMapWindow(myDisplay, myWindow);
XLowerWindow(myDisplay, myWindow);
func5();
return XFlush(myDisplay);
}

original_code:
void
initGraphics( int argc, char *argv[] )
{
    XSetWindowAttributes myWindowAttributes;
    int myDepth;
    XGCValues myGCValues;
    unsigned long myWindowMask;
    myDisplay = XOpenDisplay( "" );
    if (myDisplay == ((void *)0))
    {
 exit (0);
    }
    myScreen = (((_XPrivDisplay)(myDisplay))->default_screen);
    myDepth = ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->root_depth);
    if ( parseArgs( argc, argv ) )
 assignDefault();
    myWindowAttributes.border_pixel =
 ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->black_pixel);
    myWindowAttributes.background_pixel =
 getColor( myDisplay, 252, 234, 212 );
    myWindowAttributes.override_redirect = 0;
    myWindowMask = (1L<<1) | (1L<<3) | (1L<<9);
    myWindow = XCreateWindow (myDisplay,
         ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->root),
         window_x,
         window_y,
         window_width, window_height,
         1, myDepth, 1,
         0L, myWindowMask,
         &myWindowAttributes);
    myGC = XCreateGC (myDisplay, myWindow, (unsigned long) 0, &myGCValues);
    if (myGC == 0)
    {
 XDestroyWindow(myDisplay, myScreen);
 exit (0);
    }
    myFont = XLoadQueryFont (myDisplay, "9x15");
    if (myFont == (XFontStruct *)((void *)0))
    {
        fprintf(stderr,"Cannot get font:9x15.\n");
        myFont = ((void *)0);
    }
    else
 XSetFont (myDisplay, myGC, myFont->fid);
    XSetForeground (myDisplay, myGC, ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->black_pixel) );
    XMapWindow (myDisplay, myWindow);
    XLowerWindow( myDisplay, myWindow );
    paint();
    XFlush( myDisplay );
}

canonical_original_code:
void initGraphics(int argc, char *argv[])
{
    struct XSetWindowAttributes myWindowAttributes;
    int myDepth;
    struct XGCValues myGCValues;
    unsigned long myWindowMask;
    myDisplay = XOpenDisplay( "" );
    if (myDisplay == ((void *)0))
    {
 exit (0);
    }
    myScreen = (((_XPrivDisplay)(myDisplay))->default_screen);
    myDepth = ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->root_depth);
    if ( parseArgs( argc, argv ) )
 assignDefault();
    myWindowAttributes.border_pixel =
 ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->black_pixel);
    myWindowAttributes.background_pixel =
 getColor( myDisplay, 252, 234, 212 );
    myWindowAttributes.override_redirect = 0;
    myWindowMask = (1L<<1) | (1L<<3) | (1L<<9);
    myWindow = XCreateWindow (myDisplay,
         ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->root),
         window_x,
         window_y,
         window_width, window_height,
         1, myDepth, 1,
         0L, myWindowMask,
         &myWindowAttributes);
    myGC = XCreateGC (myDisplay, myWindow, (unsigned long) 0, &myGCValues);
    if (myGC == 0)
    {
 XDestroyWindow(myDisplay, myScreen);
 exit (0);
    }
    myFont = XLoadQueryFont (myDisplay, "9x15");
    if (myFont == (struct XFontStruct *)((void *)0))
    {
        fprintf(stderr,"Cannot get font:9x15.\n");
        myFont = ((void *)0);
    }
    else
 XSetFont (myDisplay, myGC, myFont->fid);
    XSetForeground (myDisplay, myGC, ((&((_XPrivDisplay)(myDisplay))->screens[myScreen])->black_pixel) );
    XMapWindow (myDisplay, myWindow);
    XLowerWindow( myDisplay, myWindow );
    paint();
    XFlush( myDisplay );
}

variable_types: struct XSetWindowAttributes { unsigned long background_pixmap; unsigned long background_pixel; unsigned long border_pixmap; unsigned long border_pixel; int bit_gravity; int win_gravity; int backing_store; unsigned long backing_planes; unsigned long backing_pixel; int save_under; long event_mask; long do_not_propagate_mask; int override_redirect; unsigned long colormap; unsigned long cursor; }

variable_types: myDepth int

variable_types: struct XGCValues { int function; unsigned long plane_mask; unsigned long foreground; unsigned long background; int line_width; int line_style; int cap_style; int join_style; int fill_style; int fill_rule; int arc_mode; unsigned long tile; unsigned long stipple; int ts_x_origin; int ts_y_origin; unsigned long font; int subwindow_mode; int graphics_exposures; int clip_x_origin; int clip_y_origin; unsigned long clip_mask; int dash_offset; char dashes; }

variable_types:
myWindowMask
unsigned long

user_defined_types: struct XFontStruct { struct _XExtData *ext_data; unsigned long fid; unsigned int direction; unsigned int min_char_or_byte2; unsigned int max_char_or_byte2; unsigned int min_byte1; unsigned int max_byte1; int all_chars_exist; unsigned int default_char; int n_properties; struct XFontProp *properties; struct XCharStruct min_bounds; struct XCharStruct max_bounds; struct XCharStruct *per_char; int ascent; int descent; }

user_defined_types: struct XCharStruct { short lbearing; short rbearing; short width; short ascent; short descent; unsigned short attributes; }

user_defined_types: struct _XExtData { int number; struct _XExtData *next; int (*free_private)(struct _XExtData *extension); char *private_data; }

user_defined_types: struct XFontProp { unsigned long name; unsigned long card32; }


asciiToKeycode

decompiled_code:
__int64 __fastcall asciiToKeycode(unsigned __int8 a1, _DWORD *a2)
{
*a2 = asciiToKeycode[2 * a1 + 1];
return (unsigned int)asciiToKeycode[2 * a1];
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, _DWORD *a2)
{
*a2 = asciiToKeycode[2 * a1 + 1];
return (unsigned int)asciiToKeycode[2 * a1];
}

original_code:
unsigned int
asciiToKeycode( unsigned char c, unsigned int *state )
{
    *state = _asciiToKeycode[c].state;
    return _asciiToKeycode[c].keycode;
}

canonical_original_code:
unsigned int asciiToKeycode(unsigned char c, unsigned int *state)
{
    *state = _asciiToKeycode[c].state;
    return _asciiToKeycode[c].keycode;
}

variable_types:
c
unsigned char

variable_types:
state
unsigned int *

return_type: unsigned int


deletePattern

decompiled_code:
void __fastcall deletePattern(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
free(*(void **)ptr);
free(ptr);
}

original_code:
void
deletePattern( Pattern *p )
{
    if ( p->s != ((void *)0))
 free( p->s );
    free( p->pv );
    free( p );
}

canonical_original_code:
void deletePattern(struct pattern *p)
{
    if ( p->s != ((void *)0))
 free( p->s );
    free( p->pv );
    free( p );
}


growVector

decompiled_code:
void *__fastcall growVector(void *src, int a2, int a3)
{
void *v3; // rax
void *v4; // rbx

v3 = malloc(12LL * a3);
v4 = v3;
if ( v3 )
memcpy(v3, src, 12LL * a2);
return v4;
}

canonical_decompiled_code:
void *__fastcall func1(void *src, int a2, int a3)
{
void *v3; // rax
void *v4; // rbx

v3 = malloc(12LL * a3);
v4 = v3;
if ( v3 )
memcpy(v3, src, 12LL * a2);
return v4;
}

original_code:
Point *
growVector( Point *old, int oldSize, int newSize )
{
    Point *np = (Point *) malloc( newSize * sizeof( Point ) );
    if (np == ((void *)0))
 return ((void *)0);
    memcpy( np, old, oldSize * sizeof( Point ) );
    return np;
}

canonical_original_code:
struct point *growVector(struct point *old, int oldSize, int newSize)
{
    struct point *np = (Point *) malloc( newSize * sizeof( Point ) );
    if (np == ((void *)0))
 return ((void *)0);
    memcpy( np, old, oldSize * sizeof( Point ) );
    return np;
}

variable_types:
old
struct point *

variable_types: oldSize int

variable_types: newSize int

variable_types: struct point { int x; int y; char isLast; } *


addPoint

original_code:
void
addPoint( Pattern *p, Point po )
{
    po.isLast = 0;
    p->pv[ p->nPoints++ ] = po;
    if ( p->nPoints == p->size )
    {
 Point *old = p->pv;
 int newSize = p->size * 2;
 p->pv = growVector( p->pv, p->size, newSize );
 if ( p->pv != ((void *)0))
 {
     p->size = newSize;
     free( old );
 }
 else
 {
     p->pv = old;
     p->nPoints--;
 }
    }
}

canonical_original_code:
void addPoint(struct pattern *p, struct point po)
{
    po.isLast = 0;
    p->pv[ p->nPoints++ ] = po;
    if ( p->nPoints == p->size )
    {
 struct point *old = p->pv;
 int newSize = p->size * 2;
 p->pv = growVector( p->pv, p->size, newSize );
 if ( p->pv != ((void *)0))
 {
     p->size = newSize;
     free( old );
 }
 else
 {
     p->pv = old;
     p->nPoints--;
 }
    }
}

variable_types: struct pattern { struct point { int x; int y; char isLast; } *pv; int *s; int nPoints; unsigned char ascii; int size; } *

variable_types:
po
struct point


pointSlope

decompiled_code:
__int64 __fastcall pointSlope(__int64 a1, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rbp
int v4; // ebx
double v5; // xmm0_8
double v6; // xmm0_8
float v7; // xmm2_4
float v8; // xmm3_4
float v9; // xmm4_4
float v11; // [rsp+8h] [rbp-40h]

v3 = HIDWORD(a3);
v4 = a1 - a3;
v5 = (double)(v4 * v4 + (HIDWORD(a1) - HIDWORD(a3)) * (HIDWORD(a1) - HIDWORD(a3)));
if ( v5 < 0.0 )
v6 = sqrt(v5);
else
v6 = sqrt(v5);
v7 = v6;
v8 = acos((float)((float)v4 / v7));
v11 = v8;
if ( v7 == 0.0 )
fwrite("Hyp is zero!!!!!!!!!!!!!!!!!!!!!!!!!!\n", 1uLL, 0x26uLL, stderr);
if ( SHIDWORD(a1) < (int)v3 )
{
v9 = 6.283185307179586 - v8;
v11 = v9;
}
return (unsigned int)(int)(v11 * 1000.0);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rbp
int v4; // ebx
double v5; // xmm0_8
double v6; // xmm0_8
float v7; // xmm2_4
float v8; // xmm3_4
float v9; // xmm4_4
float v11; // [rsp+8h] [rbp-40h]

v3 = HIDWORD(a3);
v4 = a1 - a3;
v5 = (double)(v4 * v4 + (HIDWORD(a1) - HIDWORD(a3)) * (HIDWORD(a1) - HIDWORD(a3)));
if ( v5 < 0.0 )
v6 = sqrt(v5);
else
v6 = sqrt(v5);
v7 = v6;
v8 = acos((float)((float)v4 / v7));
v11 = v8;
if ( v7 == 0.0 )
fwrite("Hyp is zero!!!!!!!!!!!!!!!!!!!!!!!!!!\n", 1uLL, 0x26uLL, stderr);
if ( SHIDWORD(a1) < (int)v3 )
{
v9 = 6.283185307179586 - v8;
v11 = v9;
}
return (unsigned int)(int)(v11 * 1000.0);
}

canonical_original_code:
int pointSlope(struct point p1, struct point p0)
{
    float hyp = (float) sqrt( (( p1.x - p0.x ) * ( p1.x - p0.x ))
         + (( p1.y - p0.y ) * ( p1.y - p0.y )) );
    float alpha = (float) acos( ( p1.x - p0.x ) / hyp );
    if (hyp == 0)
 fprintf(stderr, "Hyp is zero!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
    if ( p1.y < p0.y )
 alpha = 2 * 3.14159265358979323846 - alpha;
    return (int) ( 1000.0 * alpha );
}

variable_types: struct point { int x; int y; char isLast; }

variable_types: hyp float

variable_types: alpha float


deletePreprocessor

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

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

original_code:
void
deletePreprocessor( Preprocessor *p )
{
    free( p );
}

canonical_original_code:
void deletePreprocessor(struct preprocessor *p)
{
    free( p );
}

variable_types: struct preprocessor { struct pattern { struct point { int x; int y; char isLast; } *pv; int *s; int nPoints; unsigned char ascii; int size; } *(**fs)(struct pattern *); int nFilters; } *

user_defined_types: struct preprocessor { struct pattern *(**fs)(struct pattern *); int nFilters; }


applyFilter

decompiled_code:
__int64 __fastcall applyFilter(__int64 (__fastcall *a1)(__int64), __int64 a2)
{
return a1(a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 (__fastcall *a1)(__int64), __int64 a2)
{
return a1(a2);
}

original_code:
Pattern *
applyFilter( filter f, Pattern *p)
{
    return f( p );
}

canonical_original_code:
struct pattern *applyFilter(struct pattern *(*f)(struct pattern *), struct pattern *p)
{
    return f( p );
}

variable_types:
f
struct pattern *(*)(struct pattern *)

variable_types:
p
struct pattern *


apply

original_code:
Pattern *
apply( Preprocessor *p, Pattern *pat )
{
    Pattern *np, *old;
    int i;
    if ( p->nFilters == 0 )
 return pat;
    np = applyFilter( p->fs[ 0 ], pat );
    for ( i = 1; i < p->nFilters; i++)
    {
 old = np;
 np = applyFilter( p->fs[ i ], np );
 deletePattern( old );
    }
    return np;
}

canonical_original_code:
struct pattern *apply(struct preprocessor *p, struct pattern *pat)
{
    struct pattern *np; struct pattern *old;
    int i;
    if ( p->nFilters == 0 )
 return pat;
    np = applyFilter( p->fs[ 0 ], pat );
    for ( i = 1; i < p->nFilters; i++)
    {
 old = np;
 np = applyFilter( p->fs[ i ], np );
 deletePattern( old );
    }
    return np;
}

variable_types: struct preprocessor { struct pattern *(**fs)(struct pattern *); int nFilters; } *

variable_types:
pat
struct pattern *

variable_types: i int


intCaptureHandler

repo: pegerto/snooper

decompiled_code:
void intCaptureHandler()
{
pcap_breakloop(handle);
}

canonical_decompiled_code:
void func0()
{
pcap_breakloop(handle);
}

original_code:
void
intCaptureHandler(int dummy)
{
 pcap_breakloop(handle) ;
}

variable_types: dummy int


decompiled_code:
__int64 __fastcall packet_handler(__int64 a1, __int64 a2, __int64 a3)
{
printf("pk %d \n", *(unsigned int *)(a2 + 20));
return cb_pkg_push(cb_pkg, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3)
{
printf("pk %d \n", *(unsigned int *)(a2 + 20));
return cb_pkg_push(cb_pkg, a2, a3);
}

original_code:
void
packet_handler(u_char *user, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
 printf("pk %d \n", header->len);
 cb_pkg_push(cb_pkg, header, pkt_data);
}

canonical_original_code:
void packet_handler(unsigned char *user, struct pcap_pkthdr *header, unsigned char *pkt_data)
{
 printf("pk %d \n", header->len);
 cb_pkg_push(cb_pkg, header, pkt_data);
}

variable_types:
user
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
const char *v4; // rbx
__int64 v5; // rbp
unsigned int *v6; // rbx
int result; // eax
char v8[280]; // [rsp+0h] [rbp-118h] BYREF

cb_pkg = cb_pkg_init(100LL, argv, envp);
v3 = (const char *)pcap_lookupdev(v8);
if ( v3 )
{
v4 = v3;
printf("Using device: %s\n", v3);
v5 = pcap_open_live(v4, 0x2000LL, 1LL, 1000LL, v8);
handle = v5;
if ( v5 )
{
signal(2, (__sighandler_t)intCaptureHandler);
pcap_loop(v5, 0LL, packet_handler, 0LL);
pcap_close(handle);
v6 = (unsigned int *)malloc(0x18uLL);
puts("List the buffer");
while ( 1 )
{
result = cb_pkg_pull(cb_pkg, v6, 0LL);
if ( !result )
break;
printf("b pk %d\n", v6[5]);
}
}
else
{
fprintf(stderr, "Couldn't open the device %s, are you Su\n", v4);
result = 2;
}
}
else
{
fprintf(stderr, "Couldn't find default device: %s\n", v8);
result = 2;
}
return result;
}

original_code:
int
main(int argc, char *argv[])
{
 char *dev;
 char errbuf[256];
 cb_pkg = cb_pkg_init(100);
 dev = pcap_lookupdev(errbuf);
 if (dev == ((void *)0))
 {
  fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
  return(2);
 }
 else
 {
  printf("Using device: %s\n", dev);
 }
 handle = pcap_open_live(dev, 8192, 1, 1000, errbuf);
 if (handle == ((void *)0))
 {
   fprintf(stderr, "Couldn't open the device %s, are you Su\n", dev);
   return(2);
 }
 signal(2, intCaptureHandler);
 pcap_loop(handle,0, packet_handler, ((void *)0));
 pcap_close(handle);
 struct pcap_pkthdr *h;
 h = malloc(sizeof(struct pcap_pkthdr));
 printf("List the buffer\n");
 while(cb_pkg_pull(cb_pkg, h, ((void *)0)))
 {
  printf("b pk %d\n", h->len);
 }
 return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
dev
char *

variable_types:
errbuf
char [256]

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


decompiled_code:
const void *__fastcall cb_pkg_push(__int64 a1, const __m128i *a2)
{
unsigned __int64 v2; // r12
__m128i *v3; // rax
const void *result; // rax
unsigned __int64 v5; // r14
char *v6; // rax
char *v7; // r13
void *v8; // r15

if ( !a1 )
{
fwrite("Error: Package buffer is not started\n", 1uLL, 0x25uLL, stderr);
exit(2);
}
v2 = *(_QWORD *)(a1 + 16);
if ( *(_QWORD *)(a1 + 24) == v2 )
{
v5 = v2 + (v2 >> 1);
v6 = (char *)malloc(24 * v5);
v7 = v6;
if ( !v6 )
{
fwrite("Error: There is not more memory to enlarge pkg buffer\n", 1uLL, 0x36uLL, stderr);
exit(2);
}
v8 = *(void **)a1;
memcpy(v6, *(const void **)a1, 24 * v2);
*(_QWORD *)(a1 + 32) = &v7[*(_QWORD *)(a1 + 32) - (_QWORD)v8];
*(_QWORD *)(a1 + 40) = &v7[*(_QWORD *)(a1 + 40) - (_QWORD)v8];
free(v8);
*(_QWORD *)a1 = v7;
*(_QWORD *)(a1 + 16) = v5;
printf("New header buffer capacity %d\n", v2 + (v2 >> 1));
}
v3 = *(__m128i **)(a1 + 32);
*v3 = _mm_loadu_si128(a2);
v3[1].m128i_i64[0] = a2[1].m128i_i64[0];
result = (const void *)(*(_QWORD *)(a1 + 32) + 24LL);
if ( result == (const void *)(*(_QWORD *)a1 + 24LL * *(_QWORD *)(a1 + 16)) )
result = *(const void **)a1;
*(_QWORD *)(a1 + 32) = result;
++*(_QWORD *)(a1 + 24);
return result;
}

canonical_decompiled_code:
const void *__fastcall func0(__int64 a1, const __m128i *a2)
{
unsigned __int64 v2; // r12
__m128i *v3; // rax
const void *result; // rax
unsigned __int64 v5; // r14
char *v6; // rax
char *v7; // r13
void *v8; // r15

if ( !a1 )
{
fwrite("Error: Package buffer is not started\n", 1uLL, 0x25uLL, stderr);
exit(2);
}
v2 = *(_QWORD *)(a1 + 16);
if ( *(_QWORD *)(a1 + 24) == v2 )
{
v5 = v2 + (v2 >> 1);
v6 = (char *)malloc(24 * v5);
v7 = v6;
if ( !v6 )
{
fwrite("Error: There is not more memory to enlarge pkg buffer\n", 1uLL, 0x36uLL, stderr);
exit(2);
}
v8 = *(void **)a1;
memcpy(v6, *(const void **)a1, 24 * v2);
*(_QWORD *)(a1 + 32) = &v7[*(_QWORD *)(a1 + 32) - (_QWORD)v8];
*(_QWORD *)(a1 + 40) = &v7[*(_QWORD *)(a1 + 40) - (_QWORD)v8];
free(v8);
*(_QWORD *)a1 = v7;
*(_QWORD *)(a1 + 16) = v5;
printf("New header buffer capacity %d\n", v2 + (v2 >> 1));
}
v3 = *(__m128i **)(a1 + 32);
*v3 = _mm_loadu_si128(a2);
v3[1].m128i_i64[0] = a2[1].m128i_i64[0];
result = (const void *)(*(_QWORD *)(a1 + 32) + 24LL);
if ( result == (const void *)(*(_QWORD *)a1 + 24LL * *(_QWORD *)(a1 + 16)) )
result = *(const void **)a1;
*(_QWORD *)(a1 + 32) = result;
++*(_QWORD *)(a1 + 24);
return result;
}

original_code:
void
cb_pkg_push(cb_pkg_buffer *cb_pkg, const struct pcap_pkthdr *header,
   const u_char *pkt_data)
{
 size_t sz = sizeof(struct pcap_pkthdr);
 size_t new_capacity;
 void *new_header_buffer;
 if (cb_pkg == ((void *)0))
 {
  fprintf(stderr, "Error: Package buffer is not started\n" );
  exit(2);
 }
 if (cb_pkg->count == cb_pkg->capacity)
 {
  new_capacity = cb_pkg->capacity + ( cb_pkg-> capacity / 2);
  new_header_buffer = malloc (new_capacity * sz);
  if (new_header_buffer == ((void *)0))
  {
   fprintf(stderr, "Error: There is not more memory to enlarge pkg buffer\n" );
   exit(2);
  }
  memcpy(new_header_buffer, cb_pkg->header_buffer , cb_pkg->capacity * sz);
  cb_pkg->h_head = new_header_buffer + (cb_pkg->h_head - cb_pkg->header_buffer);
  cb_pkg->h_tail = new_header_buffer + (cb_pkg->h_tail - cb_pkg->header_buffer);
  free(cb_pkg->header_buffer);
  cb_pkg->header_buffer = new_header_buffer;
  cb_pkg->capacity = new_capacity;
  printf("New header buffer capacity %d\n", new_capacity);
 }
 memcpy(cb_pkg->h_head, header,sz);
 cb_pkg->h_head = cb_pkg->h_head + sz;
 if (cb_pkg->h_head == (cb_pkg->header_buffer + (sz * cb_pkg->capacity)))
  cb_pkg->h_head = cb_pkg->header_buffer;
 cb_pkg->count++;
}

canonical_original_code:
void cb_pkg_push(struct cb_pkg_buffer *cb_pkg, struct pcap_pkthdr *header, unsigned char *pkt_data)
{
 unsigned long sz = sizeof(struct pcap_pkthdr);
 unsigned long new_capacity;
 void *new_header_buffer;
 if (cb_pkg == ((void *)0))
 {
  fprintf(stderr, "Error: Package buffer is not started\n" );
  exit(2);
 }
 if (cb_pkg->count == cb_pkg->capacity)
 {
  new_capacity = cb_pkg->capacity + ( cb_pkg-> capacity / 2);
  new_header_buffer = malloc (new_capacity * sz);
  if (new_header_buffer == ((void *)0))
  {
   fprintf(stderr, "Error: There is not more memory to enlarge pkg buffer\n" );
   exit(2);
  }
  memcpy(new_header_buffer, cb_pkg->header_buffer , cb_pkg->capacity * sz);
  cb_pkg->h_head = new_header_buffer + (cb_pkg->h_head - cb_pkg->header_buffer);
  cb_pkg->h_tail = new_header_buffer + (cb_pkg->h_tail - cb_pkg->header_buffer);
  free(cb_pkg->header_buffer);
  cb_pkg->header_buffer = new_header_buffer;
  cb_pkg->capacity = new_capacity;
  printf("New header buffer capacity %d\n", new_capacity);
 }
 memcpy(cb_pkg->h_head, header,sz);
 cb_pkg->h_head = cb_pkg->h_head + sz;
 if (cb_pkg->h_head == (cb_pkg->header_buffer + (sz * cb_pkg->capacity)))
  cb_pkg->h_head = cb_pkg->header_buffer;
 cb_pkg->count++;
}

variable_types: struct cb_pkg_buffer { void *header_buffer; void *package_buffer; unsigned long capacity; unsigned long count; void *h_head; void *h_tail; void *p_head; void *p_tail; } *

variable_types:
sz
unsigned long


original_code:
int
cb_pkg_pull(cb_pkg_buffer *cb_pkg, struct pcap_pkthdr *header,
   u_char *pkt_data)
{
 size_t sz = sizeof(struct pcap_pkthdr);
 if (cb_pkg == ((void *)0))
 {
  fprintf(stderr, "Error: Package buffer is not started\n" );
  exit(2);
 }
 if (cb_pkg->count == 0 )
  return 0;
 memcpy(header, cb_pkg->h_tail , sz);
 cb_pkg->h_tail = cb_pkg->h_tail + sz;
 if (cb_pkg->h_tail == (cb_pkg->capacity * sz + cb_pkg->header_buffer))
  cb_pkg->h_tail = cb_pkg->header_buffer;
 cb_pkg->count--;
 return 1;
}

canonical_original_code:
int cb_pkg_pull(struct cb_pkg_buffer *cb_pkg, struct pcap_pkthdr *header, unsigned char *pkt_data)
{
 unsigned long sz = sizeof(struct pcap_pkthdr);
 if (cb_pkg == ((void *)0))
 {
  fprintf(stderr, "Error: Package buffer is not started\n" );
  exit(2);
 }
 if (cb_pkg->count == 0 )
  return 0;
 memcpy(header, cb_pkg->h_tail , sz);
 cb_pkg->h_tail = cb_pkg->h_tail + sz;
 if (cb_pkg->h_tail == (cb_pkg->capacity * sz + cb_pkg->header_buffer))
  cb_pkg->h_tail = cb_pkg->header_buffer;
 cb_pkg->count--;
 return 1;
}

user_defined_types: struct cb_pkg_buffer { void *header_buffer; void *package_buffer; unsigned long capacity; unsigned long count; void *h_head; void *h_tail; void *p_head; void *p_tail; }


repo: pekkanikander/MacCEC

decompiled_code:
__int64 __fastcall p8_decode(__int64 a1, unsigned __int64 *a2)
{
unsigned __int64 v3; // rsi
unsigned __int64 v4; // rbp
unsigned __int64 v6; // r12
_BYTE *v7; // rax
unsigned __int64 v8; // rdi
char *v9; // r11
_QWORD *v10; // rbx
unsigned __int64 v11; // rdx
char *v12; // rdx
unsigned __int8 v13; // r10
char v14; // al
unsigned __int64 v15; // rcx
unsigned __int8 v17; // al
__int64 v18; // rsi
unsigned __int64 v19; // rdx
_BYTE *v20; // rax
unsigned __int64 v21; // rax

v3 = *(_QWORD *)(a1 + 8);
v4 = a1 + 20;
if ( v3 < a1 + 20 || (v6 = a1 + 128, v3 > a1 + 128) )
_assert_fail(
"(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x1Eu,
"p8_decode");
v7 = *(_BYTE **)a1;
if ( v6 < *(_QWORD *)a1 || v4 > (unsigned __int64)v7 )
_assert_fail(
"(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x1Eu,
"p8_decode");
if ( v3 < (unsigned __int64)v7 )
_assert_fail("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 0x1Eu, "p8_decode");
v8 = a2[1];
v9 = (char *)a2 + 20;
if ( v8 < (unsigned __int64)a2 + 20 || (v10 = a2 + 16, v8 > (unsigned __int64)(a2 + 16)) )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x1Fu,
"p8_decode");
v11 = *a2;
if ( (unsigned __int64)v10 < *a2 || (unsigned __int64)v9 > v11 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x1Fu,
"p8_decode");
if ( v8 < v11 )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 0x1Fu, "p8_decode");
if ( (unsigned int)(v3 - (_DWORD)v7) <= 2 )
return p8_frame_error(0LL, a1);
if ( *v7 != 0xFF )
return p8_frame_error(1LL, a1);
v12 = v7 + 2;
v13 = v7[1];
v14 = v7[2];
if ( v3 > (unsigned __int64)v12 && v14 != -2 )
{
v15 = v8 + 1;
while ( v14 != -1 )
{
if ( v14 == -3 )
{
if ( v3 < (unsigned __int64)(v12 + 1) )
return p8_frame_error(0LL, a1);
v8 = v15;
*(_BYTE *)(v15 - 1) = v12[1] - 3;
v12 += 2;
}
else
{
++v12;
v8 = v15;
*(_BYTE *)(v15 - 1) = v14;
}
v14 = *v12;
++v15;
if ( v3 <= (unsigned __int64)v12 || v14 == -2 )
goto LABEL_32;
}
return p8_frame_error(0LL, a1);
}
LABEL_32:
if ( v14 != -2 )
return p8_frame_error(0LL, a1);
v17 = v13 & 0x3F;
if ( (v13 & 0x3Fu) >= P8_CODE_TYPE_COUNT )
return p8_frame_error(3LL, a1);
if ( (*((_BYTE *)&p8_code_types + 5 * (v13 & 0x3F)) & 2) == 0 )
return p8_frame_error(3LL, a1);
v18 = v8 - a2[1];
if ( v18 < *((unsigned __int8 *)&p8_code_types + 5 * v17 + 3)
|| v18 > *((unsigned __int8 *)&p8_code_types + 5 * v17 + 4) )
{
return p8_frame_error(3LL, a1);
}
*(_QWORD *)a1 = v12 + 1;
a2[1] = v8;
v19 = *(_QWORD *)(a1 + 8);
if ( v6 < v19 || v4 > v19 )
_assert_fail(
"(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x47u,
"p8_decode");
v20 = *(_BYTE **)a1;
if ( v6 < *(_QWORD *)a1 || v4 > (unsigned __int64)v20 )
_assert_fail(
"(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x47u,
"p8_decode");
if ( v19 < (unsigned __int64)v20 )
_assert_fail("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 0x47u, "p8_decode");
if ( (unsigned __int64)v10 < v8 || (unsigned __int64)v9 > v8 )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x48u,
"p8_decode");
v21 = *a2;
if ( (unsigned __int64)v10 < *a2 || (unsigned __int64)v9 > v21 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x48u,
"p8_decode");
if ( v21 > v8 )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 0x48u, "p8_decode");
return v13;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int64 *a2)
{
unsigned __int64 v3; // rsi
unsigned __int64 v4; // rbp
unsigned __int64 v6; // r12
_BYTE *v7; // rax
unsigned __int64 v8; // rdi
char *v9; // r11
_QWORD *v10; // rbx
unsigned __int64 v11; // rdx
char *v12; // rdx
unsigned __int8 v13; // r10
char v14; // al
unsigned __int64 v15; // rcx
unsigned __int8 v17; // al
__int64 v18; // rsi
unsigned __int64 v19; // rdx
_BYTE *v20; // rax
unsigned __int64 v21; // rax

v3 = *(_QWORD *)(a1 + 8);
v4 = a1 + 20;
if ( v3 < a1 + 20 || (v6 = a1 + 128, v3 > a1 + 128) )
_assert_fail(
"(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x1Eu,
"p8_decode");
v7 = *(_BYTE **)a1;
if ( v6 < *(_QWORD *)a1 || v4 > (unsigned __int64)v7 )
_assert_fail(
"(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x1Eu,
"p8_decode");
if ( v3 < (unsigned __int64)v7 )
_assert_fail("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 0x1Eu, "p8_decode");
v8 = a2[1];
v9 = (char *)a2 + 20;
if ( v8 < (unsigned __int64)a2 + 20 || (v10 = a2 + 16, v8 > (unsigned __int64)(a2 + 16)) )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x1Fu,
"p8_decode");
v11 = *a2;
if ( (unsigned __int64)v10 < *a2 || (unsigned __int64)v9 > v11 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x1Fu,
"p8_decode");
if ( v8 < v11 )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 0x1Fu, "p8_decode");
if ( (unsigned int)(v3 - (_DWORD)v7) <= 2 )
return p8_frame_error(0LL, a1);
if ( *v7 != 0xFF )
return p8_frame_error(1LL, a1);
v12 = v7 + 2;
v13 = v7[1];
v14 = v7[2];
if ( v3 > (unsigned __int64)v12 && v14 != -2 )
{
v15 = v8 + 1;
while ( v14 != -1 )
{
if ( v14 == -3 )
{
if ( v3 < (unsigned __int64)(v12 + 1) )
return p8_frame_error(0LL, a1);
v8 = v15;
*(_BYTE *)(v15 - 1) = v12[1] - 3;
v12 += 2;
}
else
{
++v12;
v8 = v15;
*(_BYTE *)(v15 - 1) = v14;
}
v14 = *v12;
++v15;
if ( v3 <= (unsigned __int64)v12 || v14 == -2 )
goto LABEL_32;
}
return p8_frame_error(0LL, a1);
}
LABEL_32:
if ( v14 != -2 )
return p8_frame_error(0LL, a1);
v17 = v13 & 0x3F;
if ( (v13 & 0x3Fu) >= P8_CODE_TYPE_COUNT )
return p8_frame_error(3LL, a1);
if ( (*((_BYTE *)&p8_code_types + 5 * (v13 & 0x3F)) & 2) == 0 )
return p8_frame_error(3LL, a1);
v18 = v8 - a2[1];
if ( v18 < *((unsigned __int8 *)&p8_code_types + 5 * v17 + 3)
|| v18 > *((unsigned __int8 *)&p8_code_types + 5 * v17 + 4) )
{
return p8_frame_error(3LL, a1);
}
*(_QWORD *)a1 = v12 + 1;
a2[1] = v8;
v19 = *(_QWORD *)(a1 + 8);
if ( v6 < v19 || v4 > v19 )
_assert_fail(
"(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x47u,
"p8_decode");
v20 = *(_BYTE **)a1;
if ( v6 < *(_QWORD *)a1 || v4 > (unsigned __int64)v20 )
_assert_fail(
"(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)",
"p8_frame_decode.c",
0x47u,
"p8_decode");
if ( v19 < (unsigned __int64)v20 )
_assert_fail("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 0x47u, "p8_decode");
if ( (unsigned __int64)v10 < v8 || (unsigned __int64)v9 > v8 )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x48u,
"p8_decode");
v21 = *a2;
if ( (unsigned __int64)v10 < *a2 || (unsigned __int64)v9 > v21 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_decode.c",
0x48u,
"p8_decode");
if ( v21 > v8 )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 0x48u, "p8_decode");
return v13;
}

original_code:
int
p8_decode(p8s_frame_t *iframe, p8_frame_t *oframe) {
    ((void) sizeof (((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 30, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 30, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta <= (iframe)->f_end) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta <= (iframe)->f_end) ; else __assert_fail ("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 30, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 31, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 31, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 31, __extension__ __PRETTY_FUNCTION__); }));
    proto_char_t *end = iframe->f_end;
    proto_char_t *p = iframe->f_sta;
    proto_len_t len = end - p;
    proto_char_t *q = oframe->f_end;
    if (len < 3)
        return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
    if (*p++ != P8_FRAME_BEGIN)
        return p8_frame_error(P8_ERROR_FRAME_BEGIN_MISSING, iframe);
    const proto_char_t code = *p++;
    const proto_char_t p8_code = code & 0x3F;
    while (*p != P8_FRAME_END && p < end) {
        if (*p == P8_FRAME_BEGIN)
            return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
        if (*p == P8_FRAME_ESC) {
            if (++p > end)
                return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
            *q++ = *p++ - (P8_FRAME_BEGIN - P8_FRAME_ESC + 1);
        } else
            *q++ = *p++;
    }
    if (*p++ != P8_FRAME_END)
        return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
    if ( (p8_code >= P8_CODE_TYPE_COUNT)
        || (p8_code_types[p8_code].ct_type & 0x02) == 0
        || (q - oframe->f_end < p8_code_types[p8_code].ct_a2h_min_plen)
        || (q - oframe->f_end > p8_code_types[p8_code].ct_a2h_max_plen)) {
        return p8_frame_error(P8_ERROR_FRAME_MALFORMED, iframe);
    }
    iframe->f_sta = p;
    oframe->f_end = q;
    ((void) sizeof (((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 71, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 71, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta <= (iframe)->f_end) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta <= (iframe)->f_end) ; else __assert_fail ("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 71, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 72, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 72, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 72, __extension__ __PRETTY_FUNCTION__); }));
    return code;
}

canonical_original_code:
int p8_decode(struct proto_frame *iframe, struct proto_frame *oframe) {
    ((void) sizeof (((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 30, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 30, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta <= (iframe)->f_end) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta <= (iframe)->f_end) ; else __assert_fail ("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 30, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 31, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 31, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 31, __extension__ __PRETTY_FUNCTION__); }));
    unsigned char *end = iframe->f_end;
    unsigned char *p = iframe->f_sta;
    unsigned int len = end - p;
    unsigned char *q = oframe->f_end;
    if (len < 3)
        return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
    if (*p++ != P8_FRAME_BEGIN)
        return p8_frame_error(P8_ERROR_FRAME_BEGIN_MISSING, iframe);
    unsigned char code = *p++;
    unsigned char p8_code = code & 0x3F;
    while (*p != P8_FRAME_END && p < end) {
        if (*p == P8_FRAME_BEGIN)
            return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
        if (*p == P8_FRAME_ESC) {
            if (++p > end)
                return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
            *q++ = *p++ - (P8_FRAME_BEGIN - P8_FRAME_ESC + 1);
        } else
            *q++ = *p++;
    }
    if (*p++ != P8_FRAME_END)
        return p8_frame_error(P8_ERROR_FRAME_INCOMPLETE, iframe);
    if ( (p8_code >= P8_CODE_TYPE_COUNT)
        || (p8_code_types[p8_code].ct_type & 0x02) == 0
        || (q - oframe->f_end < p8_code_types[p8_code].ct_a2h_min_plen)
        || (q - oframe->f_end > p8_code_types[p8_code].ct_a2h_max_plen)) {
        return p8_frame_error(P8_ERROR_FRAME_MALFORMED, iframe);
    }
    iframe->f_sta = p;
    oframe->f_end = q;
    ((void) sizeof (((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_end >= (iframe)->f_buf && (iframe)->f_end <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 71, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + ((sizeof((iframe)->f_buf)/sizeof(0[(iframe)->f_buf])) / ((unsigned int)(!(sizeof((iframe)->f_buf) % sizeof(0[(iframe)->f_buf])))))) ; else __assert_fail ("(iframe)->f_sta >= (iframe)->f_buf && (iframe)->f_sta <= (iframe)->f_buf + COUNT_OF((iframe)->f_buf)", "p8_frame_decode.c", 71, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((iframe)->f_sta <= (iframe)->f_end) ? 1 : 0), __extension__ ({ if ((iframe)->f_sta <= (iframe)->f_end) ; else __assert_fail ("(iframe)->f_sta <= (iframe)->f_end", "p8_frame_decode.c", 71, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 72, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_decode.c", 72, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_decode.c", 72, __extension__ __PRETTY_FUNCTION__); }));
    return code;
}

variable_types: struct proto_frame { unsigned char *f_sta; unsigned char *f_end; int f_status; unsigned char f_buf[-1]; } *

variable_types:
oframe
struct proto_frame *

variable_types:
end
unsigned char *

variable_types:
p
unsigned char *

variable_types:
len
unsigned int

variable_types:
q
unsigned char *

variable_types:
code
unsigned char


decompiled_code:
unsigned __int8 *__fastcall encode_char(unsigned __int8 *a1, unsigned __int8 a2)
{
unsigned __int8 v2; // al

v2 = a2;
if ( a2 > 0xFCu )
{
*a1 = -3;
v2 = a2 - 3;
++a1;
}
*a1 = v2;
return a1 + 1;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func0(unsigned __int8 *a1, unsigned __int8 a2)
{
unsigned __int8 v2; // al

v2 = a2;
if ( a2 > 0xFCu )
{
*a1 = -3;
v2 = a2 - 3;
++a1;
}
*a1 = v2;
return a1 + 1;
}

original_code:
static proto_char_t *
encode_char(proto_char_t *p, proto_char_t c) {
    if (c >= P8_FRAME_ESC) {
        *p++ = P8_FRAME_ESC;
        c -= (P8_FRAME_BEGIN - P8_FRAME_ESC + 1);
    }
    *p++ = c;
    return p;
}

canonical_original_code:
static unsigned char *encode_char(unsigned char *p, unsigned char c) {
    if (c >= P8_FRAME_ESC) {
        *p++ = P8_FRAME_ESC;
        c -= (P8_FRAME_BEGIN - P8_FRAME_ESC + 1);
    }
    *p++ = c;
    return p;
}

variable_types:
c
unsigned char

return_type: unsigned char *


decompiled_code:
unsigned __int8 *__fastcall p8_encode_cmd(_QWORD *a1, unsigned int a2, unsigned __int8 *a3, unsigned int a4)
{
_BYTE *v4; // rax
unsigned __int8 *v5; // r13
unsigned __int8 *v8; // r14
unsigned __int64 v9; // rdx
unsigned __int8 *v10; // rdi
__int64 v11; // r12
unsigned __int8 *result; // rax
unsigned __int64 v13; // rdx

v4 = (_BYTE *)a1[1];
v5 = (unsigned __int8 *)a1 + 20;
if ( v4 < (_BYTE *)a1 + 20 || (v8 = (unsigned __int8 *)(a1 + 16), v4 > (_BYTE *)a1 + 128) )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x21u,
"p8_encode_cmd");
v9 = *a1;
if ( (unsigned __int64)v8 < *a1 || (unsigned __int64)v5 > v9 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x21u,
"p8_encode_cmd");
if ( (unsigned __int64)v4 < v9 )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 0x21u, "p8_encode_cmd");
if ( a4 > 0x6B )
_assert_fail("len < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 0x22u, "p8_encode_cmd");
if ( P8_CODE_TYPE_COUNT <= a2 )
_assert_fail("(code) < P8_CODE_TYPE_COUNT", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
if ( (*((_BYTE *)&p8_code_types + 5 * a2) & 1) == 0 )
_assert_fail("p8_code_types[(code)].ct_type & P8_CT_H2A", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
if ( *((unsigned __int8 *)&p8_code_types + 5 * a2 + 1) > a4 )
_assert_fail("(len) >= p8_code_types[(code)].ct_h2a_min_plen", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
if ( *((unsigned __int8 *)&p8_code_types + 5 * a2 + 2) < a4 )
_assert_fail("(len) <= p8_code_types[(code)].ct_h2a_max_plen", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
*v4 = -1;
v10 = v4 + 2;
v4[1] = a2;
if ( a4 )
{
v11 = (__int64)&a3[a4];
do
v10 = encode_char(v10, *a3++);
while ( a3 != (unsigned __int8 *)v11 );
}
result = v10 + 1;
*v10 = -2;
a1[1] = v10 + 1;
if ( (unsigned __int64)(v10 + 1 - v5) > 0x6B )
_assert_fail("p - oframe->f_buf < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 0x30u, "p8_encode_cmd");
if ( v8 < result || v5 > result )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x32u,
"p8_encode_cmd");
v13 = *a1;
if ( (unsigned __int64)v8 < *a1 || (unsigned __int64)v5 > v13 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x32u,
"p8_encode_cmd");
if ( v13 > (unsigned __int64)result )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 0x32u, "p8_encode_cmd");
return result;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func1(_QWORD *a1, unsigned int a2, unsigned __int8 *a3, unsigned int a4)
{
_BYTE *v4; // rax
unsigned __int8 *v5; // r13
unsigned __int8 *v8; // r14
unsigned __int64 v9; // rdx
unsigned __int8 *v10; // rdi
__int64 v11; // r12
unsigned __int8 *result; // rax
unsigned __int64 v13; // rdx

v4 = (_BYTE *)a1[1];
v5 = (unsigned __int8 *)a1 + 20;
if ( v4 < (_BYTE *)a1 + 20 || (v8 = (unsigned __int8 *)(a1 + 16), v4 > (_BYTE *)a1 + 128) )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x21u,
"p8_encode_cmd");
v9 = *a1;
if ( (unsigned __int64)v8 < *a1 || (unsigned __int64)v5 > v9 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x21u,
"p8_encode_cmd");
if ( (unsigned __int64)v4 < v9 )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 0x21u, "p8_encode_cmd");
if ( a4 > 0x6B )
_assert_fail("len < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 0x22u, "p8_encode_cmd");
if ( P8_CODE_TYPE_COUNT <= a2 )
_assert_fail("(code) < P8_CODE_TYPE_COUNT", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
if ( (*((_BYTE *)&p8_code_types + 5 * a2) & 1) == 0 )
_assert_fail("p8_code_types[(code)].ct_type & P8_CT_H2A", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
if ( *((unsigned __int8 *)&p8_code_types + 5 * a2 + 1) > a4 )
_assert_fail("(len) >= p8_code_types[(code)].ct_h2a_min_plen", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
if ( *((unsigned __int8 *)&p8_code_types + 5 * a2 + 2) < a4 )
_assert_fail("(len) <= p8_code_types[(code)].ct_h2a_max_plen", "p8_frame_encode.c", 0x23u, "p8_encode_cmd");
*v4 = -1;
v10 = v4 + 2;
v4[1] = a2;
if ( a4 )
{
v11 = (__int64)&a3[a4];
do
v10 = func0(v10, *a3++);
while ( a3 != (unsigned __int8 *)v11 );
}
result = v10 + 1;
*v10 = -2;
a1[1] = v10 + 1;
if ( (unsigned __int64)(v10 + 1 - v5) > 0x6B )
_assert_fail("p - oframe->f_buf < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 0x30u, "p8_encode_cmd");
if ( v8 < result || v5 > result )
_assert_fail(
"(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x32u,
"p8_encode_cmd");
v13 = *a1;
if ( (unsigned __int64)v8 < *a1 || (unsigned __int64)v5 > v13 )
_assert_fail(
"(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)",
"p8_frame_encode.c",
0x32u,
"p8_encode_cmd");
if ( v13 > (unsigned __int64)result )
_assert_fail("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 0x32u, "p8_encode_cmd");
return result;
}

original_code:
void
p8_encode_cmd(p8s_frame_t *oframe, p8_code_t code, proto_char_t *params, proto_len_t len) {
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 33, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 33, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 33, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((len < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ? 1 : 0), __extension__ ({ if (len < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ; else __assert_fail ("len < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 34, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((code) < P8_CODE_TYPE_COUNT) ? 1 : 0), __extension__ ({ if ((code) < P8_CODE_TYPE_COUNT) ; else __assert_fail ("(code) < P8_CODE_TYPE_COUNT", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof ((p8_code_types[(code)].ct_type & 0x01) ? 1 : 0), __extension__ ({ if (p8_code_types[(code)].ct_type & 0x01) ; else __assert_fail ("p8_code_types[(code)].ct_type & P8_CT_H2A", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((len) >= p8_code_types[(code)].ct_h2a_min_plen) ? 1 : 0), __extension__ ({ if ((len) >= p8_code_types[(code)].ct_h2a_min_plen) ; else __assert_fail ("(len) >= p8_code_types[(code)].ct_h2a_min_plen", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((len) <= p8_code_types[(code)].ct_h2a_max_plen) ? 1 : 0), __extension__ ({ if ((len) <= p8_code_types[(code)].ct_h2a_max_plen) ; else __assert_fail ("(len) <= p8_code_types[(code)].ct_h2a_max_plen", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); }));
    proto_char_t *p = oframe->f_end;
    *p++ = P8_FRAME_BEGIN;
    *p++ = code;
    while (len-- > 0) {
        p = encode_char(p, *params++);
    }
    *p++ = P8_FRAME_END;
    oframe->f_end = p;
    ((void) sizeof ((p - oframe->f_buf < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ? 1 : 0), __extension__ ({ if (p - oframe->f_buf < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ; else __assert_fail ("p - oframe->f_buf < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 50, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 50, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 50, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void p8_encode_cmd(struct proto_frame *oframe, enum p8_code code, unsigned char *params, unsigned int len) {
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 33, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 33, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 33, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((len < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ? 1 : 0), __extension__ ({ if (len < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ; else __assert_fail ("len < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 34, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((code) < P8_CODE_TYPE_COUNT) ? 1 : 0), __extension__ ({ if ((code) < P8_CODE_TYPE_COUNT) ; else __assert_fail ("(code) < P8_CODE_TYPE_COUNT", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof ((p8_code_types[(code)].ct_type & 0x01) ? 1 : 0), __extension__ ({ if (p8_code_types[(code)].ct_type & 0x01) ; else __assert_fail ("p8_code_types[(code)].ct_type & P8_CT_H2A", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((len) >= p8_code_types[(code)].ct_h2a_min_plen) ? 1 : 0), __extension__ ({ if ((len) >= p8_code_types[(code)].ct_h2a_min_plen) ; else __assert_fail ("(len) >= p8_code_types[(code)].ct_h2a_min_plen", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((len) <= p8_code_types[(code)].ct_h2a_max_plen) ? 1 : 0), __extension__ ({ if ((len) <= p8_code_types[(code)].ct_h2a_max_plen) ; else __assert_fail ("(len) <= p8_code_types[(code)].ct_h2a_max_plen", "p8_frame_encode.c", 35, __extension__ __PRETTY_FUNCTION__); }));
    unsigned char *p = oframe->f_end;
    *p++ = P8_FRAME_BEGIN;
    *p++ = code;
    while (len-- > 0) {
        p = encode_char(p, *params++);
    }
    *p++ = P8_FRAME_END;
    oframe->f_end = p;
    ((void) sizeof ((p - oframe->f_buf < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ? 1 : 0), __extension__ ({ if (p - oframe->f_buf < ((sizeof(oframe->f_buf)/sizeof(0[oframe->f_buf])) / ((unsigned int)(!(sizeof(oframe->f_buf) % sizeof(0[oframe->f_buf])))))) ; else __assert_fail ("p - oframe->f_buf < COUNT_OF(oframe->f_buf)", "p8_frame_encode.c", 48, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof (((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_end >= (oframe)->f_buf && (oframe)->f_end <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 50, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + ((sizeof((oframe)->f_buf)/sizeof(0[(oframe)->f_buf])) / ((unsigned int)(!(sizeof((oframe)->f_buf) % sizeof(0[(oframe)->f_buf])))))) ; else __assert_fail ("(oframe)->f_sta >= (oframe)->f_buf && (oframe)->f_sta <= (oframe)->f_buf + COUNT_OF((oframe)->f_buf)", "p8_frame_encode.c", 50, __extension__ __PRETTY_FUNCTION__); })), ((void) sizeof (((oframe)->f_sta <= (oframe)->f_end) ? 1 : 0), __extension__ ({ if ((oframe)->f_sta <= (oframe)->f_end) ; else __assert_fail ("(oframe)->f_sta <= (oframe)->f_end", "p8_frame_encode.c", 50, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: enum p8_code { P8_CMD_NONE=0, P8_CMD_PING=1, P8_IND_ERROR_TIMEOUT=2, P8_IND_ERROR_HIGH=3, P8_IND_ERROR_LOW=4, P8_IND_RX_START=5, P8_IND_RX_NEXT=6, P8_IND_RX_FAILED=7, P8_IND_ACK=8, P8_IND_NACK=9, P8_CMD_SET_ACK_MASK=10, P8_CMD_TX=11, P8_CMD_TX_EOM=12, P8_CMD_TX_SET_IDLE=13, P8_CMD_TX_SET_ACK_P=14, P8_CMD_TX_SET_TIMEOUT=15, P8_IND_TX_SUCCEEDED=16, P8_IND_TX_FAIL_LINE=17, P8_IND_TX_FAIL_ACK=18, P8_IND_TX_TIMEOUT_D=19, P8_IND_TX_TIMEOUT_L=20, P8_QRY_FIRMWARE=21, P8_SET_CONTROLLED=24 }

variable_types:
params
unsigned char *

user_defined_types: struct proto_frame { unsigned char *f_sta; unsigned char *f_end; int f_status; unsigned char f_buf[-1]; }