decompiled_code:
__int64 __fastcall wrap_draw_box(int a1, double a2, double a3)
{
double v3; // xmm5_8
double v4; // xmm4_8
double v5; // xmm3_8
double v6; // xmm2_8
__int64 v7; // xmm5_8
__int64 v8; // xmm4_8
double v10; // [rsp+0h] [rbp-48h]
double v11; // [rsp+8h] [rbp-40h]
char v12[32]; // [rsp+10h] [rbp-38h] BYREF
int v13; // [rsp+30h] [rbp-18h]

v3 = a2;
v4 = a3;
v13 = 0;
if ( a1 )
{
v11 = a3;
v10 = a2;
if ( a1 <= 0 )
{
db_drawbounds(1LL, *(double *)&x2old_0, *(double *)&y2old_1, *(double *)&x3old_2, *(double *)&y3old_3, a3, a2);
v8 = *(_QWORD *)&a3;
v7 = *(_QWORD *)&a2;
goto LABEL_5;
}
db_drawbounds(1LL, *(double *)&x2old_0, *(double *)&y2old_1, *(double *)&x3old_2, *(double *)&y3old_3, a3, a2);
v4 = a3;
v3 = a2;
v5 = a3;
v6 = a2;
}
else
{
v5 = a3;
v11 = a3;
v6 = a2;
v10 = a2;
}
db_drawbounds(1LL, *(double *)&x2, *(double *)&y2, v6, v5, v4, v3);
v7 = *(_QWORD *)&v10;
v8 = *(_QWORD *)&v11;
LABEL_5:
x3old_2 = v7;
y3old_3 = v8;
x2old_0 = x2;
y2old_1 = y2;
return jump(v12, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, double a2, double a3)
{
double v3; // xmm5_8
double v4; // xmm4_8
double v5; // xmm3_8
double v6; // xmm2_8
__int64 v7; // xmm5_8
__int64 v8; // xmm4_8
double v10; // [rsp+0h] [rbp-48h]
double v11; // [rsp+8h] [rbp-40h]
char v12[32]; // [rsp+10h] [rbp-38h] BYREF
int v13; // [rsp+30h] [rbp-18h]

v3 = a2;
v4 = a3;
v13 = 0;
if ( a1 )
{
v11 = a3;
v10 = a2;
if ( a1 <= 0 )
{
db_drawbounds(1LL, *(double *)&x2old_0, *(double *)&y2old_1, *(double *)&x3old_2, *(double *)&y3old_3, a3, a2);
v8 = *(_QWORD *)&a3;
v7 = *(_QWORD *)&a2;
goto LABEL_5;
}
db_drawbounds(1LL, *(double *)&x2old_0, *(double *)&y2old_1, *(double *)&x3old_2, *(double *)&y3old_3, a3, a2);
v4 = a3;
v3 = a2;
v5 = a3;
v6 = a2;
}
else
{
v5 = a3;
v11 = a3;
v6 = a2;
v10 = a2;
}
db_drawbounds(1LL, *(double *)&x2, *(double *)&y2, v6, v5, v4, v3);
v7 = *(_QWORD *)&v10;
v8 = *(_QWORD *)&v11;
LABEL_5:
x3old_2 = v7;
y3old_3 = v8;
x2old_0 = x2;
y2old_1 = y2;
return jump(v12, 1LL);
}

original_code:
void wrap_draw_box(double x3, double y3, int count)
{
        static double x2old, x3old, y2old, y3old;
        BOUNDS bb;
        bb.init=0;
        if (count == 0) {
            db_drawbounds(x2,y2,x3,y3,1);
        } else if (count > 0) {
            db_drawbounds(x2old,y2old,x3old,y3old,1);
            db_drawbounds(x2,y2,x3,y3,1);
        } else {
            db_drawbounds(x2old,y2old,x3old,y3old,1);
        }
        x2old=x2;
        y2old=y2;
        x3old=x3;
        y3old=y3;
        jump(&bb, 1);
}

canonical_original_code:
void wrap_draw_box(double x3, double y3, int count)
{
        static double x2old; static double x3old; static double y2old; static double y3old;
        struct bounds bb;
        bb.init=0;
        if (count == 0) {
            db_drawbounds(x2,y2,x3,y3,1);
        } else if (count > 0) {
            db_drawbounds(x2old,y2old,x3old,y3old,1);
            db_drawbounds(x2,y2,x3,y3,1);
        } else {
            db_drawbounds(x2old,y2old,x3old,y3old,1);
        }
        x2old=x2;
        y2old=y2;
        x3old=x3;
        y3old=y3;
        jump(&bb, 1);
}

variable_types: count int

variable_types: struct bounds { double xmin; double ymin; double xmax; double ymax; int init; }


repo: omnister/piglet

decompiled_code:
__int64 __fastcall draw_poly(int a1, double a2, double a3)
{
__int64 v3; // rax
char v5[32]; // [rsp+10h] [rbp-38h] BYREF
int v6; // [rsp+30h] [rbp-18h]

v3 = CP;
qword_6C8 = (__int64)&dbpoly;
v6 = 0;
dbdeflist_1 = 4096;
dbpoly = 1;
*((_QWORD *)&dbpoly + 1) = &popts;
*((_QWORD *)&dbpoly + 2) = v3;
if ( !a1 )
{
jump(v5, 1LL);
goto LABEL_8;
}
if ( a1 > 0 )
{
jump(v5, 1LL);
do_poly(&dbdeflist_1, v5, 1LL);
jump(v5, 1LL);
coord_swap_last(CP, a2, a3);
LABEL_8:
do_poly(&dbdeflist_1, v5, 1LL);
++called_0;
return jump(v5, 1LL);
}
if ( called_0 )
{
jump(v5, 1LL);
do_poly(&dbdeflist_1, v5, 1LL);
}
called_0 = 0;
return jump(v5, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, double a2, double a3)
{
__int64 v3; // rax
char v5[32]; // [rsp+10h] [rbp-38h] BYREF
int v6; // [rsp+30h] [rbp-18h]

v3 = CP;
qword_6C8 = (__int64)&dbpoly;
v6 = 0;
dbdeflist_1 = 4096;
dbpoly = 1;
*((_QWORD *)&dbpoly + 1) = &popts;
*((_QWORD *)&dbpoly + 2) = v3;
if ( !a1 )
{
jump(v5, 1LL);
goto LABEL_8;
}
if ( a1 > 0 )
{
jump(v5, 1LL);
do_poly(&dbdeflist_1, v5, 1LL);
jump(v5, 1LL);
coord_swap_last(CP, a2, a3);
LABEL_8:
do_poly(&dbdeflist_1, v5, 1LL);
++called_0;
return jump(v5, 1LL);
}
if ( called_0 )
{
jump(v5, 1LL);
do_poly(&dbdeflist_1, v5, 1LL);
}
called_0 = 0;
return jump(v5, 1LL);
}

original_code:
void draw_poly(double x2, double y2, int count)
{
 int debug=0;
 BOUNDS bb;
 static int called = 0;
 bb.init=0;
 static DB_DEFLIST dbdeflist;
        dbdeflist.u.p = &dbpoly;
        dbdeflist.type = 4096;
        dbpoly.layer=1;
        dbpoly.opts=&popts;
        dbpoly.coords=CP;
 if (debug) {printf("in draw_poly\n");}
 if (count == 0) {
     jump(&bb, 1);
     do_poly(&dbdeflist, &bb, 1);
     called++;
 } else if (count > 0) {
     jump(&bb, 1);
     do_poly(&dbdeflist, &bb, 1);
     jump(&bb, 1);
     coord_swap_last(CP, x2, y2);
     do_poly(&dbdeflist, &bb, 1);
     called++;
 } else {
     if (called) {
  jump(&bb, 1);
  do_poly(&dbdeflist, &bb, 1);
     }
     called=0;
 }
 jump(&bb, 1);
}

canonical_original_code:
void draw_poly(double x2, double y2, int count)
{
 int debug = 0;
 struct bounds bb;
 static int called = 0;
 bb.init=0;
 static struct db_deflist dbdeflist;
        dbdeflist.u.p = &dbpoly;
        dbdeflist.type = 4096;
        dbpoly.layer=1;
        dbpoly.opts=&popts;
        dbpoly.coords=CP;
 if (debug) {printf("in draw_poly\n");}
 if (count == 0) {
     jump(&bb, 1);
     do_poly(&dbdeflist, &bb, 1);
     called++;
 } else if (count > 0) {
     jump(&bb, 1);
     do_poly(&dbdeflist, &bb, 1);
     jump(&bb, 1);
     coord_swap_last(CP, x2, y2);
     do_poly(&dbdeflist, &bb, 1);
     called++;
 } else {
     if (called) {
  jump(&bb, 1);
  do_poly(&dbdeflist, &bb, 1);
     }
     called=0;
 }
 jump(&bb, 1);
}

variable_types: debug int

variable_types: called int

variable_types: struct db_deflist { int type; union { struct db_arc { int layer; struct opt_list { double font_size; double height; int mirror; int font_num; int justification; int stepflag; int rows; int cols; double rotation; double width; double scale; double aspect_ratio; double slant; char *sname; char *cname; int bezier; } *opts; double x1; } *a; struct db_circ { int layer; struct opt_list *opts; double x1; } *c; struct db_inst { char *name; struct opt_list *opts; double x; double colx; double rowx; } *i; struct db_line { int layer; struct opt_list *opts; struct coord_list { struct coord_pair { double x; } coord; struct coord_list *next; struct coord_list *prev; } *coords; } *l; struct db_note { int layer; struct opt_list *opts; char *text; double x; } *n; struct db_oval { int layer; struct opt_list *opts; double x1; } *o; struct db_poly { int layer; struct opt_list *opts; struct coord_list *coords; } *p; struct db_rect { int layer; struct opt_list *opts; double x1; } *r; struct db_text { int layer; struct opt_list *opts; char *text; double x; } *t; } u; double xmin; struct db_deflist *next; struct db_deflist *prev; }

return_type: void

user_defined_types: struct bounds { double xmin; double ymin; double xmax; double ymax; int init; }

user_defined_types: struct db_deflist { int type; union { struct db_arc *a; struct db_circ *c; struct db_inst *i; struct db_line *l; struct db_note *n; struct db_oval *o; struct db_poly *p; struct db_rect *r; struct db_text *t; } u; double xmin; struct db_deflist *next; struct db_deflist *prev; }

user_defined_types: struct db_arc { int layer; struct opt_list *opts; double x1; }

user_defined_types: struct db_line { int layer; struct opt_list *opts; struct coord_list *coords; }

user_defined_types: struct coord_list { struct coord_pair coord; struct coord_list *next; struct coord_list *prev; }


decompiled_code:
__int64 __fastcall draw_bounds(int a1, double a2, double a3)
{
double v3; // xmm3_8
double v4; // xmm2_8
double v6; // [rsp+0h] [rbp-50h]
double v7; // [rsp+8h] [rbp-48h]
double v8; // [rsp+10h] [rbp-40h]
char v9[56]; // [rsp+18h] [rbp-38h] BYREF

v3 = a2;
v4 = a3;
if ( !a1 )
{
v6 = a3;
v8 = a2;
goto LABEL_8;
}
if ( a1 > 0 )
{
v6 = a3;
v8 = a2;
jump(v9, 1LL, a2, a3, a3, a2);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y1old_3);
a3 = *(double *)&y1old_3;
a2 = *(double *)&x1old_2;
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
LABEL_8:
jump(v9, 1LL, a2, a3, v4, v3);
draw(v9, 1LL, *(double *)&x1, *(double *)&y1);
draw(v9, 1LL, *(double *)&x1, v6);
draw(v9, 1LL, v8, v6);
draw(v9, 1LL, v8, *(double *)&y1);
a3 = *(double *)&y1;
draw(v9, 1LL, *(double *)&x1, *(double *)&y1);
++called_4;
v3 = v8;
v4 = v6;
goto LABEL_6;
}
if ( called_4 )
{
v7 = a3;
jump(v9, 1LL, a2, a3, a3, a2);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y1old_3);
a3 = *(double *)&y1old_3;
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
v4 = v7;
v3 = a2;
}
called_4 = 0;
LABEL_6:
x2old_0 = *(_QWORD *)&v3;
y2old_1 = *(_QWORD *)&v4;
x1old_2 = x1;
y1old_3 = y1;
return jump(v9, 1LL, *(double *)&y1, a3, v4, v3);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, double a2, double a3)
{
double v3; // xmm3_8
double v4; // xmm2_8
double v6; // [rsp+0h] [rbp-50h]
double v7; // [rsp+8h] [rbp-48h]
double v8; // [rsp+10h] [rbp-40h]
char v9[56]; // [rsp+18h] [rbp-38h] BYREF

v3 = a2;
v4 = a3;
if ( !a1 )
{
v6 = a3;
v8 = a2;
goto LABEL_8;
}
if ( a1 > 0 )
{
v6 = a3;
v8 = a2;
jump(v9, 1LL, a2, a3, a3, a2);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y1old_3);
a3 = *(double *)&y1old_3;
a2 = *(double *)&x1old_2;
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
LABEL_8:
jump(v9, 1LL, a2, a3, v4, v3);
draw(v9, 1LL, *(double *)&x1, *(double *)&y1);
draw(v9, 1LL, *(double *)&x1, v6);
draw(v9, 1LL, v8, v6);
draw(v9, 1LL, v8, *(double *)&y1);
a3 = *(double *)&y1;
draw(v9, 1LL, *(double *)&x1, *(double *)&y1);
++called_4;
v3 = v8;
v4 = v6;
goto LABEL_6;
}
if ( called_4 )
{
v7 = a3;
jump(v9, 1LL, a2, a3, a3, a2);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y2old_1);
draw(v9, 1LL, *(double *)&x2old_0, *(double *)&y1old_3);
a3 = *(double *)&y1old_3;
draw(v9, 1LL, *(double *)&x1old_2, *(double *)&y1old_3);
v4 = v7;
v3 = a2;
}
called_4 = 0;
LABEL_6:
x2old_0 = *(_QWORD *)&v3;
y2old_1 = *(_QWORD *)&v4;
x1old_2 = x1;
y1old_3 = y1;
return jump(v9, 1LL, *(double *)&y1, a3, v4, v3);
}

original_code:
void draw_bounds(double x2, double y2, int count)
{
 static double x1old, x2old, y1old, y2old;
 static int called = 0;
 BOUNDS bb;
 if (count == 0) {
     jump(&bb, 1);
     draw(x1,y1, &bb, 1);
     draw(x1,y2, &bb, 1);
     draw(x2,y2, &bb, 1);
     draw(x2,y1, &bb, 1);
     draw(x1,y1, &bb, 1);
     called++;
 } else if (count > 0) {
     jump(&bb, 1);
     draw(x1old,y1old, &bb, 1);
     draw(x1old,y2old, &bb, 1);
     draw(x2old,y2old, &bb, 1);
     draw(x2old,y1old, &bb, 1);
     draw(x1old,y1old, &bb, 1);
     jump(&bb, 1);
     draw(x1,y1, &bb, 1);
     draw(x1,y2, &bb, 1);
     draw(x2,y2, &bb, 1);
     draw(x2,y1, &bb, 1);
     draw(x1,y1, &bb, 1);
     called++;
 } else {
     if (called) {
  jump(&bb, 1);
  draw(x1old,y1old, &bb, 1);
  draw(x1old,y2old, &bb, 1);
  draw(x2old,y2old, &bb, 1);
  draw(x2old,y1old, &bb, 1);
  draw(x1old,y1old, &bb, 1);
     }
     called=0;
 }
 x1old=x1;
 y1old=y1;
 x2old=x2;
 y2old=y2;
 jump(&bb, 1);
}

canonical_original_code:
void draw_bounds(double x2, double y2, int count)
{
 static double x1old; static double x2old; static double y1old; static double y2old;
 static int called = 0;
 struct bounds bb;
 if (count == 0) {
     jump(&bb, 1);
     draw(x1,y1, &bb, 1);
     draw(x1,y2, &bb, 1);
     draw(x2,y2, &bb, 1);
     draw(x2,y1, &bb, 1);
     draw(x1,y1, &bb, 1);
     called++;
 } else if (count > 0) {
     jump(&bb, 1);
     draw(x1old,y1old, &bb, 1);
     draw(x1old,y2old, &bb, 1);
     draw(x2old,y2old, &bb, 1);
     draw(x2old,y1old, &bb, 1);
     draw(x1old,y1old, &bb, 1);
     jump(&bb, 1);
     draw(x1,y1, &bb, 1);
     draw(x1,y2, &bb, 1);
     draw(x2,y2, &bb, 1);
     draw(x2,y1, &bb, 1);
     draw(x1,y1, &bb, 1);
     called++;
 } else {
     if (called) {
  jump(&bb, 1);
  draw(x1old,y1old, &bb, 1);
  draw(x1old,y2old, &bb, 1);
  draw(x2old,y2old, &bb, 1);
  draw(x2old,y1old, &bb, 1);
  draw(x1old,y1old, &bb, 1);
     }
     called=0;
 }
 x1old=x1;
 y1old=y1;
 x2old=x2;
 y2old=y2;
 jump(&bb, 1);
}


getcolor

decompiled_code:
__int64 __fastcall getcolor(char *s, int a2)
{
int v2; // ebp
__int64 v3; // rbx
__int64 result; // rax

v2 = 0;
v3 = 0LL;
while ( 1 )
{
switch ( s[v3] )
{
case 'a':
++v2;
result = 4LL;
break;
case 'b':
++v2;
result = 3LL;
break;
case 'g':
++v2;
result = 2LL;
break;
case 'm':
case 'p':
++v2;
result = 5LL;
break;
case 'r':
++v2;
result = 1LL;
break;
case 'w':
++v2;
result = 7LL;
break;
case 'y':
++v2;
result = 6LL;
break;
default:
result = 7LL;
break;
}
if ( v2 == a2 )
break;
if ( strlen(s) < ++v3 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s, int a2)
{
int v2; // ebp
__int64 v3; // rbx
__int64 result; // rax

v2 = 0;
v3 = 0LL;
while ( 1 )
{
switch ( s[v3] )
{
case 'a':
++v2;
result = 4LL;
break;
case 'b':
++v2;
result = 3LL;
break;
case 'g':
++v2;
result = 2LL;
break;
case 'm':
case 'p':
++v2;
result = 5LL;
break;
case 'r':
++v2;
result = 1LL;
break;
case 'w':
++v2;
result = 7LL;
break;
case 'y':
++v2;
result = 6LL;
break;
default:
result = 7LL;
break;
}
if ( v2 == a2 )
break;
if ( strlen(s) < ++v3 )
return 0LL;
}
return result;
}

original_code:
int getcolor(
    char *s,
    int k
) {
    size_t i;
    int n=0;
    int c;
    for (i=0; i<=strlen(s); i++) {
     switch(s[i]) {
     case 'a':
  c=4;
  n++;
      break;
     case 'b':
  c=3;
  n++;
      break;
     case 'g':
  c=2;
  n++;
      break;
     case 'p':
     case 'm':
  c=5;
  n++;
      break;
     case 'r':
  c=1;
  n++;
      break;
     case 'w':
  c=7;
  n++;
      break;
     case 'y':
  c=6;
  n++;
      break;
     default:
  c=7;
      break;
 }
 if (n==k) {
     return(c);
 }
    }
    return(0);
}

canonical_original_code:
int getcolor(char *s, int k) {
    unsigned long i;
    int n = 0;
    int c;
    for (i=0; i<=strlen(s); i++) {
     switch(s[i]) {
     case 'a':
  c=4;
  n++;
      break;
     case 'b':
  c=3;
  n++;
      break;
     case 'g':
  c=2;
  n++;
      break;
     case 'p':
     case 'm':
  c=5;
  n++;
      break;
     case 'r':
  c=1;
  n++;
      break;
     case 'w':
  c=7;
  n++;
      break;
     case 'y':
  c=6;
  n++;
      break;
     default:
  c=7;
      break;
 }
 if (n==k) {
     return(c);
 }
    }
    return(0);
}

variable_types:
s
char *

variable_types: k int

variable_types:
i
unsigned long

variable_types: n int

variable_types: c int

return_type: int


decompiled_code:
__int64 __fastcall draw_inst_bb(int a1, double a2, double a3)
{
__int64 v3; // xmm4_8
__int64 v4; // xmm5_8
char v6[32]; // [rsp+10h] [rbp-38h] BYREF
int v7; // [rsp+30h] [rbp-18h]

v7 = 0;
if ( a1 )
{
db_drawbounds(
1LL,
*(double *)&xold_0 + *(double *)&bb_xmin,
*(double *)&bb_ymin + *(double *)&yold_1,
*(double *)&bb_xmax + *(double *)&xold_0,
*(double *)&bb_ymax + *(double *)&yold_1);
v3 = *(_QWORD *)&a3;
if ( a1 > 0 )
{
db_drawbounds(
1LL,
*(double *)&bb_xmin + a2,
a3 + *(double *)&bb_ymin,
a2 + *(double *)&bb_xmax,
*(double *)&bb_ymax + a3);
v3 = *(_QWORD *)&a3;
}
v4 = *(_QWORD *)&a2;
}
else
{
db_drawbounds(
1LL,
*(double *)&bb_xmin + a2,
*(double *)&bb_ymin + a3,
*(double *)&bb_xmax + a2,
*(double *)&bb_ymax + a3);
v3 = *(_QWORD *)&a3;
v4 = *(_QWORD *)&a2;
}
xold_0 = v4;
yold_1 = v3;
return jump(v6, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, double a2, double a3)
{
__int64 v3; // xmm4_8
__int64 v4; // xmm5_8
char v6[32]; // [rsp+10h] [rbp-38h] BYREF
int v7; // [rsp+30h] [rbp-18h]

v7 = 0;
if ( a1 )
{
db_drawbounds(
1LL,
*(double *)&xold_0 + *(double *)&bb_xmin,
*(double *)&bb_ymin + *(double *)&yold_1,
*(double *)&bb_xmax + *(double *)&xold_0,
*(double *)&bb_ymax + *(double *)&yold_1);
v3 = *(_QWORD *)&a3;
if ( a1 > 0 )
{
db_drawbounds(
1LL,
*(double *)&bb_xmin + a2,
a3 + *(double *)&bb_ymin,
a2 + *(double *)&bb_xmax,
*(double *)&bb_ymax + a3);
v3 = *(_QWORD *)&a3;
}
v4 = *(_QWORD *)&a2;
}
else
{
db_drawbounds(
1LL,
*(double *)&bb_xmin + a2,
*(double *)&bb_ymin + a3,
*(double *)&bb_xmax + a2,
*(double *)&bb_ymax + a3);
v3 = *(_QWORD *)&a3;
v4 = *(_QWORD *)&a2;
}
xold_0 = v4;
yold_1 = v3;
return jump(v6, 1LL);
}

original_code:
void draw_inst_bb(double x, double y, int count)
{
 static double xold, yold;
 BOUNDS bb;
 bb.init=0;
 if (count == 0) {
     db_drawbounds(bb_xmin+x, bb_ymin+y, bb_xmax+x, bb_ymax+y, 1);
 } else if (count > 0) {
     db_drawbounds(bb_xmin+xold, bb_ymin+yold, bb_xmax+xold, bb_ymax+yold, 1);
     db_drawbounds(bb_xmin+x, bb_ymin+y, bb_xmax+x, bb_ymax+y, 1);
 } else {
     db_drawbounds(bb_xmin+xold, bb_ymin+yold, bb_xmax+xold, bb_ymax+yold, 1);
 }
 xold=x;
 yold=y;
 jump(&bb, 1);
}

canonical_original_code:
void draw_inst_bb(double x, double y, int count)
{
 static double xold; static double yold;
 struct bounds bb;
 bb.init=0;
 if (count == 0) {
     db_drawbounds(bb_xmin+x, bb_ymin+y, bb_xmax+x, bb_ymax+y, 1);
 } else if (count > 0) {
     db_drawbounds(bb_xmin+xold, bb_ymin+yold, bb_xmax+xold, bb_ymax+yold, 1);
     db_drawbounds(bb_xmin+x, bb_ymin+y, bb_xmax+x, bb_ymax+y, 1);
 } else {
     db_drawbounds(bb_xmin+xold, bb_ymin+yold, bb_xmax+xold, bb_ymax+yold, 1);
 }
 xold=x;
 yold=y;
 jump(&bb, 1);
}

variable_types: x double

variable_types: y double

variable_types: xold double

variable_types: yold double


decompiled_code:
__int64 __fastcall ident_draw_box(int a1, double a2, double a3)
{
double v3; // xmm5_8
double v4; // xmm4_8
double v5; // xmm3_8
double v6; // xmm2_8
__int64 v7; // xmm5_8
__int64 v8; // xmm4_8
double v10; // [rsp+0h] [rbp-48h]
double v11; // [rsp+8h] [rbp-40h]
char v12[32]; // [rsp+10h] [rbp-38h] BYREF
int v13; // [rsp+30h] [rbp-18h]

v3 = a2;
v4 = a3;
v13 = 0;
if ( a1 )
{
v11 = a3;
v10 = a2;
if ( a1 <= 0 )
{
db_drawbounds(1LL, *(double *)&x1old_0, *(double *)&y1old_1, *(double *)&x2old_2, *(double *)&y2old_3, a3, a2);
v8 = *(_QWORD *)&a3;
v7 = *(_QWORD *)&a2;
goto LABEL_5;
}
db_drawbounds(1LL, *(double *)&x1old_0, *(double *)&y1old_1, *(double *)&x2old_2, *(double *)&y2old_3, a3, a2);
v4 = a3;
v3 = a2;
v5 = a3;
v6 = a2;
}
else
{
v5 = a3;
v11 = a3;
v6 = a2;
v10 = a2;
}
db_drawbounds(1LL, *(double *)&x1, *(double *)&y1, v6, v5, v4, v3);
v7 = *(_QWORD *)&v10;
v8 = *(_QWORD *)&v11;
LABEL_5:
x2old_2 = v7;
y2old_3 = v8;
x1old_0 = x1;
y1old_1 = y1;
return jump(v12, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, double a2, double a3)
{
double v3; // xmm5_8
double v4; // xmm4_8
double v5; // xmm3_8
double v6; // xmm2_8
__int64 v7; // xmm5_8
__int64 v8; // xmm4_8
double v10; // [rsp+0h] [rbp-48h]
double v11; // [rsp+8h] [rbp-40h]
char v12[32]; // [rsp+10h] [rbp-38h] BYREF
int v13; // [rsp+30h] [rbp-18h]

v3 = a2;
v4 = a3;
v13 = 0;
if ( a1 )
{
v11 = a3;
v10 = a2;
if ( a1 <= 0 )
{
db_drawbounds(1LL, *(double *)&x1old_0, *(double *)&y1old_1, *(double *)&x2old_2, *(double *)&y2old_3, a3, a2);
v8 = *(_QWORD *)&a3;
v7 = *(_QWORD *)&a2;
goto LABEL_5;
}
db_drawbounds(1LL, *(double *)&x1old_0, *(double *)&y1old_1, *(double *)&x2old_2, *(double *)&y2old_3, a3, a2);
v4 = a3;
v3 = a2;
v5 = a3;
v6 = a2;
}
else
{
v5 = a3;
v11 = a3;
v6 = a2;
v10 = a2;
}
db_drawbounds(1LL, *(double *)&x1, *(double *)&y1, v6, v5, v4, v3);
v7 = *(_QWORD *)&v10;
v8 = *(_QWORD *)&v11;
LABEL_5:
x2old_2 = v7;
y2old_3 = v8;
x1old_0 = x1;
y1old_1 = y1;
return jump(v12, 1LL);
}

original_code:
void ident_draw_box(double x2, double y2, int count)
{
        static double x1old, x2old, y1old, y2old;
        BOUNDS bb;
        bb.init=0;
        if (count == 0) {
            db_drawbounds(x1,y1,x2,y2,1);
        } else if (count > 0) {
            db_drawbounds(x1old,y1old,x2old,y2old,1);
            db_drawbounds(x1,y1,x2,y2,1);
        } else {
            db_drawbounds(x1old,y1old,x2old,y2old,1);
        }
        x1old=x1;
        y1old=y1;
        x2old=x2;
        y2old=y2;
        jump(&bb, 1);
}

canonical_original_code:
void ident_draw_box(double x2, double y2, int count)
{
        static double x1old; static double x2old; static double y1old; static double y2old;
        struct bounds bb;
        bb.init=0;
        if (count == 0) {
            db_drawbounds(x1,y1,x2,y2,1);
        } else if (count > 0) {
            db_drawbounds(x1old,y1old,x2old,y2old,1);
            db_drawbounds(x1,y1,x2,y2,1);
        } else {
            db_drawbounds(x1old,y1old,x2old,y2old,1);
        }
        x1old=x1;
        y1old=y1;
        x2old=x2;
        y2old=y2;
        jump(&bb, 1);
}


decompiled_code:
__int64 __fastcall decode_5bit(unsigned __int8 a1)
{
__m128i v2; // [rsp+0h] [rbp-28h]
__int64 v3[3]; // [rsp+10h] [rbp-18h] BYREF

qmemcpy(v3, "JKLMNPQRSTVWXYZ", 15);
v2 = _mm_load_si128((const __m128i *)&xmmword_402260);
return v2.m128i_u8[a1];
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1)
{
__m128i v2; // [rsp+0h] [rbp-28h]
__int64 v3[3]; // [rsp+10h] [rbp-18h] BYREF

qmemcpy(v3, "JKLMNPQRSTVWXYZ", 15);
v2 = _mm_load_si128((const __m128i *)&xmmword_402260);
return v2.m128i_u8[a1];
}

variable_types:
encoded
unsigned char

variable_types:
map
unsigned char []

return_type: unsigned char


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

if ( (a1 & 0x40) != 0 )
result = (a1 & 0x1Fu) + 64;
else
result = (a1 & 0xFu) + 48;
return result;
}

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

if ( (a1 & 0x40) != 0 )
result = (a1 & 0x1Fu) + 64;
else
result = (a1 & 0xFu) + 48;
return result;
}

original_code:
unsigned char decode_6bit(unsigned char encoded) {
    if (encoded & 0x40)
        return (encoded & 0x1F) + 'A' - 1;
    else
        return (encoded & 0x0F) + '0';
}


decompiled_code:
__int64 __fastcall encode_5bit(int a1)
{
__int64 result; // rax
__m128i v2; // [rsp+0h] [rbp-28h]
__int64 v3; // [rsp+10h] [rbp-18h]
__int16 v4; // [rsp+18h] [rbp-10h]

v3 = 0x1C1B1AFF19181716LL;
v4 = 7709;
result = (unsigned int)(a1 - 48);
v2 = _mm_load_si128((const __m128i *)&xmmword_402270);
if ( (unsigned __int8)(a1 - 48) > 9u )
{
result = 0xFFFFFFFFLL;
if ( (unsigned __int8)(a1 - 65) <= 0x19u )
result = v2.m128i_u8[(unsigned __int8)a1 - 65];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
__int64 result; // rax
__m128i v2; // [rsp+0h] [rbp-28h]
__int64 v3; // [rsp+10h] [rbp-18h]
__int16 v4; // [rsp+18h] [rbp-10h]

v3 = 0x1C1B1AFF19181716LL;
v4 = 7709;
result = (unsigned int)(a1 - 48);
v2 = _mm_load_si128((const __m128i *)&xmmword_402270);
if ( (unsigned __int8)(a1 - 48) > 9u )
{
result = 0xFFFFFFFFLL;
if ( (unsigned __int8)(a1 - 65) <= 0x19u )
result = v2.m128i_u8[(unsigned __int8)a1 - 65];
}
return result;
}

original_code:
unsigned char encode_5bit(unsigned char c) {
    unsigned char map[] = { 0xFF, 0x0A, 0x0B, 0x0C, 0xFF, 0x0D, 0x0E, 0x0F, 0xFF, 0x10, 0x11, 0x12, 0x13, 0x14, 0xFF,
                   0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E };
    if (c >= '0' && c <= '9')
        return c - '0';
    else if (c >= 'A' && c <= 'Z')
        return map[c - 'A'];
    else
        return 0xFF;
}

variable_types:
c
unsigned char


decompiled_code:
__int64 __fastcall encode_6bit(int a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edi
bool v3; // cf
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (a1 - 48) | 0x30u;
v1 = a1 - 65;
v2 = (a1 - 64) | 0x40;
v3 = v1 < 0x1Au;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edi
bool v3; // cf
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (a1 - 48) | 0x30u;
v1 = a1 - 65;
v2 = (a1 - 64) | 0x40;
v3 = v1 < 0x1Au;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v2;
return result;
}

original_code:
unsigned char encode_6bit(unsigned char c) {
    if (c >= '0' && c <= '9')
        return (c - '0') | 0x30;
    else if (c >= 'A' && c <= 'Z')
        return (c - 'A' + 1) | 0x40;
    else
        return 0xFF;
}


decompiled_code:
void __noreturn invalid_tag()
{
fwrite("Invalid service tag!\n", 1uLL, 0x15uLL, stderr);
fwrite("Service tag must be exactly 7 characters\n", 1uLL, 0x29uLL, stderr);
fwrite("First character can be number [0-9] or character from alphabet [A-Z]\n", 1uLL, 0x45uLL, stderr);
fwrite("Other characters can be numbers [0-9] or characters from alphabet except A,E,I,O,U\n", 1uLL, 0x53uLL, stderr);
exit(2);
}

canonical_decompiled_code:
void __noreturn func11()
{
fwrite("Invalid service tag!\n", 1uLL, 0x15uLL, stderr);
fwrite("Service tag must be exactly 7 characters\n", 1uLL, 0x29uLL, stderr);
fwrite("First character can be number [0-9] or character from alphabet [A-Z]\n", 1uLL, 0x45uLL, stderr);
fwrite("Other characters can be numbers [0-9] or characters from alphabet except A,E,I,O,U\n", 1uLL, 0x53uLL, stderr);
exit(2);
}

original_code:
void invalid_tag(void) {
    fprintf(stderr, "Invalid service tag!\n");
    fprintf(stderr, "Service tag must be exactly 7 characters\n");
    fprintf(stderr, "First character can be number [0-9] or character from alphabet [A-Z]\n");
    fprintf(stderr, "Other characters can be numbers [0-9] or characters from alphabet except A,E,I,O,U\n");
    exit(2);
}

canonical_original_code:
void invalid_tag() {
    fprintf(stderr, "Invalid service tag!\n");
    fprintf(stderr, "Service tag must be exactly 7 characters\n");
    fprintf(stderr, "First character can be number [0-9] or character from alphabet [A-Z]\n");
    fprintf(stderr, "Other characters can be numbers [0-9] or characters from alphabet except A,E,I,O,U\n");
    exit(2);
}


decompiled_code:
__int64 __fastcall detect_relay_card_sainsmart_4_8chan(char *s, _BYTE *a2, __int64 a3, __int64 *a4)
{
__int64 result; // rax
const char *v6; // rax
unsigned int v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( a4 )
{
libusb_init(0LL);
open_device_with_vid_pid_serial_constprop_0_isra_0(a4);
libusb_exit(0LL);
return 0xFFFFFFFFLL;
}
ftdi = ftdi_new();
if ( !ftdi )
{
fwrite("ftdi_new failed\n", 1uLL, 0x10uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( (int)ftdi_usb_open_desc(ftdi, 1027LL, 24577LL, 0LL, a3) < 0 )
{
LABEL_16:
ftdi_free(ftdi);
return 0xFFFFFFFFLL;
}
if ( (int)ftdi_set_bitmode(ftdi, 255LL, 1LL) < 0 )
{
v6 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "unable to set bitbang mode: (%s)\n", v6);
goto LABEL_16;
}
if ( *(_DWORD *)(ftdi + 16) == 3 )
{
ftdi_read_chipid(ftdi, v8);
if ( (unsigned __int8)(config[100] - 1) <= 0xFu )
g_num_relays = config[100];
if ( a2 )
*a2 = g_num_relays;
if ( s )
sprintf(s, "FTDI chipid %X", v8[0]);
ftdi_usb_close(ftdi);
result = 0LL;
}
else
{
fwrite("unable to continue, not an R-type chip\n", 1uLL, 0x27uLL, stderr);
ftdi_free(ftdi);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s, _BYTE *a2, __int64 a3, __int64 *a4)
{
__int64 result; // rax
const char *v6; // rax
unsigned int v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( a4 )
{
libusb_init(0LL);
func0(a4);
libusb_exit(0LL);
return 0xFFFFFFFFLL;
}
ftdi = ftdi_new();
if ( !ftdi )
{
fwrite("ftdi_new failed\n", 1uLL, 0x10uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( (int)ftdi_usb_open_desc(ftdi, 1027LL, 24577LL, 0LL, a3) < 0 )
{
LABEL_16:
ftdi_free(ftdi);
return 0xFFFFFFFFLL;
}
if ( (int)ftdi_set_bitmode(ftdi, 255LL, 1LL) < 0 )
{
v6 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "unable to set bitbang mode: (%s)\n", v6);
goto LABEL_16;
}
if ( *(_DWORD *)(ftdi + 16) == 3 )
{
ftdi_read_chipid(ftdi, v8);
if ( (unsigned __int8)(config[100] - 1) <= 0xFu )
g_num_relays = config[100];
if ( a2 )
*a2 = g_num_relays;
if ( s )
sprintf(s, "FTDI chipid %X", v8[0]);
ftdi_usb_close(ftdi);
result = 0LL;
}
else
{
fwrite("unable to continue, not an R-type chip\n", 1uLL, 0x27uLL, stderr);
ftdi_free(ftdi);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int detect_relay_card_sainsmart_4_8chan(char* portname, uint8_t* num_relays, char* serial, relay_info_t** relay_info)
{
    unsigned int chipid;
   if (relay_info)
   {
      libusb_init(((void *)0));
      open_device_with_vid_pid_serial(0x0403, 0x6001, ((void *)0), relay_info);
      libusb_exit(((void *)0));
      return -1;
   }
   if ((ftdi = ftdi_new()) == 0)
   {
      fprintf(stderr, "ftdi_new failed\n");
      return -1;
   }
   if ((ftdi_usb_open_desc(ftdi, 0x0403, 0x6001, ((void *)0), serial)) < 0)
   {
      ftdi_free(ftdi);
      return -1;
   }
   if (ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG) < 0)
   {
      fprintf(stderr, "unable to set bitbang mode: (%s)\n", ftdi_get_error_string(ftdi));
      ftdi_free(ftdi);
      return -1;
   }
   if (ftdi->type != TYPE_R)
   {
      fprintf(stderr, "unable to continue, not an R-type chip\n");
      ftdi_free(ftdi);
      return -1;
   }
   ftdi_read_chipid(ftdi, &chipid);
   if (config.sainsmart_num_relays >= 1 &&
       config.sainsmart_num_relays <= 16)
   {
      g_num_relays = config.sainsmart_num_relays;
   }
   if (num_relays)
      *num_relays = g_num_relays;
   if (portname)
      sprintf(portname, "FTDI chipid %X", chipid);
   ftdi_usb_close(ftdi);
   return 0;
}

canonical_original_code:
int detect_relay_card_sainsmart_4_8chan(char *portname, unsigned char *num_relays, char *serial, struct relay_info **relay_info)
{
    unsigned int chipid;
   if (relay_info)
   {
      libusb_init(((void *)0));
      open_device_with_vid_pid_serial(0x0403, 0x6001, ((void *)0), relay_info);
      libusb_exit(((void *)0));
      return -1;
   }
   if ((ftdi = ftdi_new()) == 0)
   {
      fprintf(stderr, "ftdi_new failed\n");
      return -1;
   }
   if ((ftdi_usb_open_desc(ftdi, 0x0403, 0x6001, ((void *)0), serial)) < 0)
   {
      ftdi_free(ftdi);
      return -1;
   }
   if (ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG) < 0)
   {
      fprintf(stderr, "unable to set bitbang mode: (%s)\n", ftdi_get_error_string(ftdi));
      ftdi_free(ftdi);
      return -1;
   }
   if (ftdi->type != TYPE_R)
   {
      fprintf(stderr, "unable to continue, not an R-type chip\n");
      ftdi_free(ftdi);
      return -1;
   }
   ftdi_read_chipid(ftdi, &chipid);
   if (config.sainsmart_num_relays >= 1 &&
       config.sainsmart_num_relays <= 16)
   {
      g_num_relays = config.sainsmart_num_relays;
   }
   if (num_relays)
      *num_relays = g_num_relays;
   if (portname)
      sprintf(portname, "FTDI chipid %X", chipid);
   ftdi_usb_close(ftdi);
   return 0;
}

variable_types:
portname
char *

variable_types:
serial
char *

variable_types: struct relay_info { enum relay_type_t { NO_RELAY_TYPE=0, CONRAD_4CHANNEL_USB_RELAY_TYPE=1, SAINSMART_USB_RELAY_TYPE=2, HID_API_RELAY_TYPE=3, SAINSMART16_USB_RELAY_TYPE=4, GENERIC_GPIO_RELAY_TYPE=5, LAST_RELAY_TYPE=6 } relay_type; char serial[32]; struct relay_info *next; } **

variable_types:
chipid
unsigned int

user_defined_types: struct relay_info { enum relay_type_t relay_type; char serial[32]; struct relay_info *next; }


decompiled_code:
__int64 __fastcall get_relay_sainsmart_4_8chan(__int64 a1, unsigned __int8 a2, int *a3, __int64 a4)
{
__int64 v5; // rdi
__int64 result; // rax
const char *v7; // rax
const char *v8; // rax
char v9[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
if ( (int)ftdi_usb_open_desc(ftdi, 1027LL, 24577LL, 0LL, a4) < 0 )
{
v8 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "unable to open ftdi device (get): (%s)\n", v8);
result = 4294967294LL;
}
else if ( (int)ftdi_read_pins(ftdi, v9) < 0 )
{
v7 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "read failed for 0x%x, error %s\n", (unsigned __int8)v9[0], v7);
result = 4294967293LL;
}
else
{
v5 = ftdi;
*a3 = ((int)(unsigned __int8)v9[0] >> (a2 - 1)) & 1;
ftdi_usb_close(v5);
result = 0LL;
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int8 a2, int *a3, __int64 a4)
{
__int64 v5; // rdi
__int64 result; // rax
const char *v7; // rax
const char *v8; // rax
char v9[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
if ( (int)ftdi_usb_open_desc(ftdi, 1027LL, 24577LL, 0LL, a4) < 0 )
{
v8 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "unable to open ftdi device (get): (%s)\n", v8);
result = 4294967294LL;
}
else if ( (int)ftdi_read_pins(ftdi, v9) < 0 )
{
v7 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "read failed for 0x%x, error %s\n", (unsigned __int8)v9[0], v7);
result = 4294967293LL;
}
else
{
v5 = ftdi;
*a3 = ((int)(unsigned __int8)v9[0] >> (a2 - 1)) & 1;
ftdi_usb_close(v5);
result = 0LL;
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int get_relay_sainsmart_4_8chan(char* portname, uint8_t relay, relay_state_t* relay_state, char* serial)
{
   unsigned char buf[1];
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   if ((ftdi_usb_open_desc(ftdi, 0x0403, 0x6001, ((void *)0), serial)) < 0)
   {
      fprintf(stderr, "unable to open ftdi device (get): (%s)\n", ftdi_get_error_string(ftdi));
      return -2;
   }
   if (ftdi_read_pins(ftdi, &buf[0]) < 0)
   {
      fprintf(stderr,"read failed for 0x%x, error %s\n",buf[0], ftdi_get_error_string(ftdi));
      return -3;
   }
   relay = relay-1;
   *relay_state = (buf[0] & (0x01<<relay)) ? ON : OFF;
   ftdi_usb_close(ftdi);
   return 0;
}

canonical_original_code:
int get_relay_sainsmart_4_8chan(char *portname, unsigned char relay, enum relay_state_t *relay_state, char *serial)
{
   unsigned char buf[1];
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   if ((ftdi_usb_open_desc(ftdi, 0x0403, 0x6001, ((void *)0), serial)) < 0)
   {
      fprintf(stderr, "unable to open ftdi device (get): (%s)\n", ftdi_get_error_string(ftdi));
      return -2;
   }
   if (ftdi_read_pins(ftdi, &buf[0]) < 0)
   {
      fprintf(stderr,"read failed for 0x%x, error %s\n",buf[0], ftdi_get_error_string(ftdi));
      return -3;
   }
   relay = relay-1;
   *relay_state = (buf[0] & (0x01<<relay)) ? ON : OFF;
   ftdi_usb_close(ftdi);
   return 0;
}

variable_types:
relay
unsigned char

variable_types: enum relay_state_t { OFF=0, ON=1, PULSE=2, INVALID=3 } *

variable_types:
buf
unsigned char [1]


decompiled_code:
__int64 __fastcall set_relay_sainsmart_4_8chan(__int64 a1, unsigned __int8 a2, int a3, __int64 a4)
{
int v5; // edx
char v6; // al
char v7; // dl
__int64 result; // rax
const char *v9; // rax
const char *v10; // rax
const char *v11; // rax
char v12[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
if ( (int)ftdi_usb_open_desc(ftdi, 1027LL, 24577LL, 0LL, a4) < 0 )
{
v9 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "unable to open ftdi device (set): (%s)\n", v9);
result = 4294967294LL;
}
else if ( (int)ftdi_read_pins(ftdi, v12) < 0 )
{
v11 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "read failed for 0x%x, error %s\n", (unsigned __int8)v12[0], v11);
result = 4294967293LL;
}
else
{
v5 = 1 << (a2 - 1);
v6 = v12[0] | v5;
v7 = v12[0] & ~(_BYTE)v5;
if ( !a3 )
v6 = v7;
v12[0] = v6;
if ( (int)ftdi_write_data(ftdi, v12, 1LL) < 0 )
{
v10 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "read failed for 0x%x, error %s\n", (unsigned __int8)v12[0], v10);
result = 4294967292LL;
}
else
{
ftdi_usb_close(ftdi);
result = 0LL;
}
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int8 a2, int a3, __int64 a4)
{
int v5; // edx
char v6; // al
char v7; // dl
__int64 result; // rax
const char *v9; // rax
const char *v10; // rax
const char *v11; // rax
char v12[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
if ( (int)ftdi_usb_open_desc(ftdi, 1027LL, 24577LL, 0LL, a4) < 0 )
{
v9 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "unable to open ftdi device (set): (%s)\n", v9);
result = 4294967294LL;
}
else if ( (int)ftdi_read_pins(ftdi, v12) < 0 )
{
v11 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "read failed for 0x%x, error %s\n", (unsigned __int8)v12[0], v11);
result = 4294967293LL;
}
else
{
v5 = 1 << (a2 - 1);
v6 = v12[0] | v5;
v7 = v12[0] & ~(_BYTE)v5;
if ( !a3 )
v6 = v7;
v12[0] = v6;
if ( (int)ftdi_write_data(ftdi, v12, 1LL) < 0 )
{
v10 = (const char *)ftdi_get_error_string(ftdi);
fprintf(stderr, "read failed for 0x%x, error %s\n", (unsigned __int8)v12[0], v10);
result = 4294967292LL;
}
else
{
ftdi_usb_close(ftdi);
result = 0LL;
}
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_relay_sainsmart_4_8chan(char* portname, uint8_t relay, relay_state_t relay_state, char* serial)
{
   unsigned char buf[1];
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   if ((ftdi_usb_open_desc(ftdi, 0x0403, 0x6001, ((void *)0), serial)) < 0)
   {
      fprintf(stderr, "unable to open ftdi device (set): (%s)\n", ftdi_get_error_string(ftdi));
      return -2;
   }
   if (ftdi_read_pins(ftdi, buf) < 0)
   {
      fprintf(stderr,"read failed for 0x%x, error %s\n",buf[0], ftdi_get_error_string(ftdi));
      return -3;
   }
   relay = relay-1;
   if (relay_state == OFF)
   {
      buf[0] = buf[0] & ~(0x01<<relay);
   }
   else
   {
      buf[0] = buf[0] | (0x01<<relay);
   }
   if (ftdi_write_data(ftdi, buf, 1) < 0)
   {
      fprintf(stderr,"read failed for 0x%x, error %s\n",buf[0], ftdi_get_error_string(ftdi));
      return -4;
   }
   ftdi_usb_close(ftdi);
   return 0;
}

canonical_original_code:
int set_relay_sainsmart_4_8chan(char *portname, unsigned char relay, enum relay_state_t relay_state, char *serial)
{
   unsigned char buf[1];
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   if ((ftdi_usb_open_desc(ftdi, 0x0403, 0x6001, ((void *)0), serial)) < 0)
   {
      fprintf(stderr, "unable to open ftdi device (set): (%s)\n", ftdi_get_error_string(ftdi));
      return -2;
   }
   if (ftdi_read_pins(ftdi, buf) < 0)
   {
      fprintf(stderr,"read failed for 0x%x, error %s\n",buf[0], ftdi_get_error_string(ftdi));
      return -3;
   }
   relay = relay-1;
   if (relay_state == OFF)
   {
      buf[0] = buf[0] & ~(0x01<<relay);
   }
   else
   {
      buf[0] = buf[0] | (0x01<<relay);
   }
   if (ftdi_write_data(ftdi, buf, 1) < 0)
   {
      fprintf(stderr,"read failed for 0x%x, error %s\n",buf[0], ftdi_get_error_string(ftdi));
      return -4;
   }
   ftdi_usb_close(ftdi);
   return 0;
}

variable_types: enum relay_state_t { OFF=0, ON=1, PULSE=2, INVALID=3 }


decompiled_code:
__int64 __fastcall get_relay_generic_gpio(__int64 a1, unsigned __int8 a2, _DWORD *a3)
{
int v4; // eax
int v5; // ebp
__int64 result; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
char buf; // [rsp+Fh] [rbp-59h] BYREF
char s[88]; // [rsp+10h] [rbp-58h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
snprintf(s, 0x40uLL, "%s%d/value", "/sys/class/gpio/gpio", pins[a2]);
v4 = open(s, 2);
v5 = v4;
if ( v4 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "ERROR: Open %s: %s\n", s, v10);
result = 0xFFFFFFFFLL;
}
else if ( pread(v4, &buf, 1uLL, 0LL) == 1 )
{
close(v5);
if ( g_active_value )
{
if ( g_active_value == 1 )
{
*a3 = buf == 49;
result = 0LL;
}
else
{
fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n", (unsigned __int8)g_active_value);
result = 0xFFFFFFFFLL;
}
}
else
{
*a3 = buf == 48;
result = 0LL;
}
}
else
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "ERROR: Unable to pread gpio value: %s\n", v8);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 a2, _DWORD *a3)
{
int v4; // eax
int v5; // ebp
__int64 result; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
char buf; // [rsp+Fh] [rbp-59h] BYREF
char s[88]; // [rsp+10h] [rbp-58h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
snprintf(s, 0x40uLL, "%s%d/value", "/sys/class/gpio/gpio", pins[a2]);
v4 = open(s, 2);
v5 = v4;
if ( v4 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "ERROR: Open %s: %s\n", s, v10);
result = 0xFFFFFFFFLL;
}
else if ( pread(v4, &buf, 1uLL, 0LL) == 1 )
{
close(v5);
if ( g_active_value )
{
if ( g_active_value == 1 )
{
*a3 = buf == 49;
result = 0LL;
}
else
{
fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n", (unsigned __int8)g_active_value);
result = 0xFFFFFFFFLL;
}
}
else
{
*a3 = buf == 48;
result = 0LL;
}
}
else
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "ERROR: Unable to pread gpio value: %s\n", v8);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int get_relay_generic_gpio(char* portname, uint8_t relay, relay_state_t* relay_state, char* serial)
{
   int fd;
   char b[64];
   char d[1];
   uint16_t pin;
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   pin=pins[relay];
   snprintf(b, sizeof(b), "%s%d/value", "/sys/class/gpio/""gpio", pin);
   fd = open(b, 02);
   if (fd < 0)
   {
      fprintf(stderr, "ERROR: Open %s: %s\n", b, strerror((*__errno_location ())));
      return -1;
   }
   if (pread(fd, d, 1, 0) != 1)
   {
      fprintf(stderr, "ERROR: Unable to pread gpio value: %s\n",
                       strerror((*__errno_location ())));
      return -1;
   }
   close(fd);
   switch (g_active_value)
   {
      case 0:
         *relay_state = (d[0] == '0') ? ON : OFF;
         break;
      case 1:
         *relay_state = (d[0] == '1') ? ON : OFF;
         break;
      default:
         fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n",
                         g_active_value);
         return -1;
   }
   return 0;
}

canonical_original_code:
int get_relay_generic_gpio(char *portname, unsigned char relay, enum relay_state_t *relay_state, char *serial)
{
   int fd;
   char b[64];
   char d[1];
   unsigned short pin;
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   pin=pins[relay];
   snprintf(b, sizeof(b), "%s%d/value", "/sys/class/gpio/""gpio", pin);
   fd = open(b, 02);
   if (fd < 0)
   {
      fprintf(stderr, "ERROR: Open %s: %s\n", b, strerror((*__errno_location ())));
      return -1;
   }
   if (pread(fd, d, 1, 0) != 1)
   {
      fprintf(stderr, "ERROR: Unable to pread gpio value: %s\n",
                       strerror((*__errno_location ())));
      return -1;
   }
   close(fd);
   switch (g_active_value)
   {
      case 0:
         *relay_state = (d[0] == '0') ? ON : OFF;
         break;
      case 1:
         *relay_state = (d[0] == '1') ? ON : OFF;
         break;
      default:
         fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n",
                         g_active_value);
         return -1;
   }
   return 0;
}

variable_types: fd int

variable_types:
b
char [64]

variable_types:
d
char [1]

variable_types:
pin
unsigned short

user_defined_types: enum relay_state_t { OFF=0, ON=1, PULSE=2, INVALID=3 }


decompiled_code:
__int64 __fastcall set_relay_generic_gpio(__int64 a1, unsigned __int8 a2, int a3)
{
int v4; // ebp
__int64 result; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
char buf; // [rsp+Fh] [rbp-59h] BYREF
char s[88]; // [rsp+10h] [rbp-58h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
snprintf(s, 0x40uLL, "%s%d/value", "/sys/class/gpio/gpio", pins[a2]);
v4 = open(s, 2);
if ( v4 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "ERROR: Open %s: %s\n", s, v9);
result = 0xFFFFFFFFLL;
}
else
{
if ( g_active_value )
{
if ( g_active_value != 1 )
{
fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n", (unsigned __int8)g_active_value);
return 0xFFFFFFFFLL;
}
buf = (a3 == 1) + 48;
}
else
{
buf = (a3 == 0) + 48;
}
if ( pwrite(v4, &buf, 1uLL, 0LL) == 1 )
{
close(v4);
result = 0LL;
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "ERROR: Unable to pwrite %c to gpio value: %s\n", (unsigned int)buf, v7);
result = 0xFFFFFFFFLL;
}
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int8 a2, int a3)
{
int v4; // ebp
__int64 result; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
char buf; // [rsp+Fh] [rbp-59h] BYREF
char s[88]; // [rsp+10h] [rbp-58h] BYREF

if ( a2 && (unsigned __int8)g_num_relays >= a2 )
{
snprintf(s, 0x40uLL, "%s%d/value", "/sys/class/gpio/gpio", pins[a2]);
v4 = open(s, 2);
if ( v4 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "ERROR: Open %s: %s\n", s, v9);
result = 0xFFFFFFFFLL;
}
else
{
if ( g_active_value )
{
if ( g_active_value != 1 )
{
fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n", (unsigned __int8)g_active_value);
return 0xFFFFFFFFLL;
}
buf = (a3 == 1) + 48;
}
else
{
buf = (a3 == 0) + 48;
}
if ( pwrite(v4, &buf, 1uLL, 0LL) == 1 )
{
close(v4);
result = 0LL;
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "ERROR: Unable to pwrite %c to gpio value: %s\n", (unsigned int)buf, v7);
result = 0xFFFFFFFFLL;
}
}
}
else
{
fwrite("ERROR: Relay number out of range\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_relay_generic_gpio(char* portname, uint8_t relay, relay_state_t relay_state, char* serial)
{
   int fd;
   char b[64];
   char d[1];
   uint16_t pin;
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   pin=pins[relay];
   snprintf(b, sizeof(b), "%s%d/value", "/sys/class/gpio/""gpio", pin);
   fd = open(b, 02);
   if (fd < 0)
   {
      fprintf(stderr, "ERROR: Open %s: %s\n", b, strerror((*__errno_location ())));
      return -1;
   }
   switch (g_active_value)
   {
      case 0:
         d[0] = (relay_state == OFF ? '1' : '0');
         break;
      case 1:
         d[0] = (relay_state == ON ? '1' : '0');
         break;
      default:
         fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n",
                         g_active_value);
         return -1;
   }
   if (pwrite(fd, d, 1, 0) != 1)
   {
      fprintf(stderr, "ERROR: Unable to pwrite %c to gpio value: %s\n",
                       d[0], strerror((*__errno_location ())));
      return -1;
   }
   close(fd);
   return 0;
}

canonical_original_code:
int set_relay_generic_gpio(char *portname, unsigned char relay, enum relay_state_t relay_state, char *serial)
{
   int fd;
   char b[64];
   char d[1];
   unsigned short pin;
   if (relay<1 || relay>(1 +g_num_relays-1))
   {
      fprintf(stderr, "ERROR: Relay number out of range\n");
      return -1;
   }
   pin=pins[relay];
   snprintf(b, sizeof(b), "%s%d/value", "/sys/class/gpio/""gpio", pin);
   fd = open(b, 02);
   if (fd < 0)
   {
      fprintf(stderr, "ERROR: Open %s: %s\n", b, strerror((*__errno_location ())));
      return -1;
   }
   switch (g_active_value)
   {
      case 0:
         d[0] = (relay_state == OFF ? '1' : '0');
         break;
      case 1:
         d[0] = (relay_state == ON ? '1' : '0');
         break;
      default:
         fprintf(stderr, "ERROR: Invalid active pin value configured: %d\n",
                         g_active_value);
         return -1;
   }
   if (pwrite(fd, d, 1, 0) != 1)
   {
      fprintf(stderr, "ERROR: Unable to pwrite %c to gpio value: %s\n",
                       d[0], strerror((*__errno_location ())));
      return -1;
   }
   close(fd);
   return 0;
}


decompiled_code:
__int64 __fastcall detect_relay_card_generic_gpio(__m128i *a1, _BYTE *a2)
{
int v3; // eax
int v4; // ebp
int v5; // edx
unsigned __int16 v6; // di
__int64 v7; // rax
unsigned __int16 *v8; // rax
unsigned __int16 *v9; // rcx
unsigned __int16 i; // dx
__int64 j; // r14

v3 = open("/sys/class/gpio/export", 1);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
v4 = v3;
v5 = *((unsigned __int8 *)&config + 81);
if ( (unsigned __int8)(v5 - 1) <= 0xFu )
g_num_relays = *((_BYTE *)&config + 81);
else
v5 = (unsigned __int8)g_num_relays;
v6 = *((_WORD *)&config + 42);
g_active_value = *((_BYTE *)&config + 82);
v7 = *(_QWORD *)((char *)&config + 92);
qword_642 = *(_QWORD *)((char *)&config + 84);
qword_64A = v7;
if ( (_BYTE)v5 )
{
v8 = (unsigned __int16 *)&qword_642 + 1;
v9 = (unsigned __int16 *)&qword_642 + (unsigned int)(v5 - 1) + 1;
for ( i = v6; i; i = *v8++ )
{
if ( v8 == v9 )
{
for ( j = 1LL; ; v6 = pins[j] )
{
if ( !(unsigned int)do_export(v6) )
set_relay_generic_gpio(0LL, j, 0);
++j;
LOBYTE(v5) = g_num_relays;
if ( (unsigned __int8)g_num_relays < (int)j )
break;
}
goto LABEL_11;
}
}
return 0xFFFFFFFFLL;
}
LABEL_11:
if ( a2 )
*a2 = v5;
a1[1].m128i_i8[0] = 0;
*a1 = _mm_load_si128((const __m128i *)&LC15);
close(v4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__m128i *a1, _BYTE *a2)
{
int v3; // eax
int v4; // ebp
int v5; // edx
unsigned __int16 v6; // di
__int64 v7; // rax
unsigned __int16 *v8; // rax
unsigned __int16 *v9; // rcx
unsigned __int16 i; // dx
__int64 j; // r14

v3 = open("/sys/class/gpio/export", 1);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
v4 = v3;
v5 = *((unsigned __int8 *)&config + 81);
if ( (unsigned __int8)(v5 - 1) <= 0xFu )
g_num_relays = *((_BYTE *)&config + 81);
else
v5 = (unsigned __int8)g_num_relays;
v6 = *((_WORD *)&config + 42);
g_active_value = *((_BYTE *)&config + 82);
v7 = *(_QWORD *)((char *)&config + 92);
qword_642 = *(_QWORD *)((char *)&config + 84);
qword_64A = v7;
if ( (_BYTE)v5 )
{
v8 = (unsigned __int16 *)&qword_642 + 1;
v9 = (unsigned __int16 *)&qword_642 + (unsigned int)(v5 - 1) + 1;
for ( i = v6; i; i = *v8++ )
{
if ( v8 == v9 )
{
for ( j = 1LL; ; v6 = pins[j] )
{
if ( !(unsigned int)func0(v6) )
func2(0LL, j, 0);
++j;
LOBYTE(v5) = g_num_relays;
if ( (unsigned __int8)g_num_relays < (int)j )
break;
}
goto LABEL_11;
}
}
return 0xFFFFFFFFLL;
}
LABEL_11:
if ( a2 )
*a2 = v5;
a1[1].m128i_i8[0] = 0;
*a1 = _mm_load_si128((const __m128i *)&LC15);
close(v4);
return 0LL;
}

original_code:
int detect_relay_card_generic_gpio(char* portname, uint8_t* num_relays, char* serial, relay_info_t** relay_info)
{
   int fd;
   int i;
   fd = open("/sys/class/gpio/""export", 01);
   if (fd < 0)
   {
      return -1;
   }
   if (config.gpio_num_relays >= 1 &&
       config.gpio_num_relays <= 16)
   {
      g_num_relays = config.gpio_num_relays;
   }
   g_active_value = config.gpio_active_value;
   pins[1] = config.relay1_gpio_pin;
   pins[2] = config.relay2_gpio_pin;
   pins[3] = config.relay3_gpio_pin;
   pins[4] = config.relay4_gpio_pin;
   pins[5] = config.relay5_gpio_pin;
   pins[6] = config.relay6_gpio_pin;
   pins[7] = config.relay7_gpio_pin;
   pins[8] = config.relay8_gpio_pin;
   for (i=1; i<=g_num_relays; i++)
   {
      if (pins[i]==0) return -1;
   }
   for (i=1; i<=g_num_relays; i++)
   {
      if (do_export(pins[i]) == 0)
         set_relay_generic_gpio(((void *)0), i, OFF, ((void *)0));
   }
   if (num_relays!=((void *)0)) *num_relays = g_num_relays;
   strcpy(portname, "/sys/class/gpio/");
   close(fd);
   return 0;
}

canonical_original_code:
int detect_relay_card_generic_gpio(char *portname, unsigned char *num_relays, char *serial, struct relay_info **relay_info)
{
   int fd;
   int i;
   fd = open("/sys/class/gpio/""export", 01);
   if (fd < 0)
   {
      return -1;
   }
   if (config.gpio_num_relays >= 1 &&
       config.gpio_num_relays <= 16)
   {
      g_num_relays = config.gpio_num_relays;
   }
   g_active_value = config.gpio_active_value;
   pins[1] = config.relay1_gpio_pin;
   pins[2] = config.relay2_gpio_pin;
   pins[3] = config.relay3_gpio_pin;
   pins[4] = config.relay4_gpio_pin;
   pins[5] = config.relay5_gpio_pin;
   pins[6] = config.relay6_gpio_pin;
   pins[7] = config.relay7_gpio_pin;
   pins[8] = config.relay8_gpio_pin;
   for (i=1; i<=g_num_relays; i++)
   {
      if (pins[i]==0) return -1;
   }
   for (i=1; i<=g_num_relays; i++)
   {
      if (do_export(pins[i]) == 0)
         set_relay_generic_gpio(((void *)0), i, OFF, ((void *)0));
   }
   if (num_relays!=((void *)0)) *num_relays = g_num_relays;
   strcpy(portname, "/sys/class/gpio/");
   close(fd);
   return 0;
}

variable_types: i int

user_defined_types: enum relay_type_t { NO_RELAY_TYPE=0, CONRAD_4CHANNEL_USB_RELAY_TYPE=1, SAINSMART_USB_RELAY_TYPE=2, HID_API_RELAY_TYPE=3, SAINSMART16_USB_RELAY_TYPE=4, GENERIC_GPIO_RELAY_TYPE=5, LAST_RELAY_TYPE=6 }


decompiled_code:
__int64 __fastcall crelay_detect_all_relay_cards(__int64 a1)
{
char *v1; // rbx
_QWORD *v2; // rax
void (__fastcall *i)(_QWORD, _QWORD, _QWORD, __int64 *); // rax
__int64 v5[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = (char *)&relay_data;
v2 = malloc(0x30uLL);
v5[0] = (__int64)v2;
v2[5] = 0LL;
*(_QWORD *)a1 = v2;
for ( i = (void (__fastcall *)(_QWORD, _QWORD, _QWORD, __int64 *))&detect_relay_card_conrad_4chan;
;
i = (void (__fastcall *)(_QWORD, _QWORD, _QWORD, __int64 *))*((_QWORD *)v1 + 4) )
{
v1 += 32;
i(0LL, 0LL, 0LL, v5);
if ( v1 == (char *)&relay_data + 160 )
break;
}
return (unsigned int)-(*(_QWORD *)(*(_QWORD *)a1 + 40LL) == 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
char *v1; // rbx
_QWORD *v2; // rax
void (__fastcall *i)(_QWORD, _QWORD, _QWORD, __int64 *); // rax
__int64 v5[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = (char *)&relay_data;
v2 = malloc(0x30uLL);
v5[0] = (__int64)v2;
v2[5] = 0LL;
*(_QWORD *)a1 = v2;
for ( i = (void (__fastcall *)(_QWORD, _QWORD, _QWORD, __int64 *))&detect_relay_card_conrad_4chan;
;
i = (void (__fastcall *)(_QWORD, _QWORD, _QWORD, __int64 *))*((_QWORD *)v1 + 4) )
{
v1 += 32;
i(0LL, 0LL, 0LL, v5);
if ( v1 == (char *)&relay_data + 160 )
break;
}
return (unsigned int)-(*(_QWORD *)(*(_QWORD *)a1 + 40LL) == 0LL);
}

original_code:
int crelay_detect_all_relay_cards(relay_info_t** relay_info)
{
   int i;
   relay_info_t* my_relay_info;
   my_relay_info = malloc(sizeof(relay_info_t));
   my_relay_info->next = ((void *)0);
   *relay_info = my_relay_info;
   for (i=1; i<LAST_RELAY_TYPE; i++)
   {
      (*relay_data[i].detect_relay_card_fun)(((void *)0), ((void *)0), ((void *)0), &my_relay_info);
   }
   if ((*relay_info)->next == ((void *)0))
      return -1;
   else
      return 0;
}

canonical_original_code:
int crelay_detect_all_relay_cards(struct relay_info **relay_info)
{
   int i;
   struct relay_info *my_relay_info;
   my_relay_info = malloc(sizeof(relay_info_t));
   my_relay_info->next = ((void *)0);
   *relay_info = my_relay_info;
   for (i=1; i<LAST_RELAY_TYPE; i++)
   {
      (*relay_data[i].detect_relay_card_fun)(((void *)0), ((void *)0), ((void *)0), &my_relay_info);
   }
   if ((*relay_info)->next == ((void *)0))
      return -1;
   else
      return 0;
}

variable_types: struct relay_info { enum relay_type_t { NO_RELAY_TYPE=0, CONRAD_4CHANNEL_USB_RELAY_TYPE=1, SAINSMART_USB_RELAY_TYPE=2, HID_API_RELAY_TYPE=3, SAINSMART16_USB_RELAY_TYPE=4, GENERIC_GPIO_RELAY_TYPE=5, LAST_RELAY_TYPE=6 } relay_type; char serial[32]; struct relay_info *next; } *


decompiled_code:
__int64 __fastcall crelay_detect_relay_card(__int64 a1, __int64 a2, __int64 a3)
{
char *v3; // r14
int v5; // ebx
__int64 result; // rax

v3 = (char *)&relay_data;
v5 = 1;
for ( result = detect_relay_card_conrad_4chan(a1, a2, a3, 0LL);
(_DWORD)result;
result = (*((__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD))v3 + 4))(a1, a2, a3, 0LL) )
{
++v5;
v3 += 32;
if ( v5 == 6 )
{
relay_type = 0;
return 0xFFFFFFFFLL;
}
}
relay_type = v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3)
{
char *v3; // r14
int v5; // ebx
__int64 result; // rax

v3 = (char *)&relay_data;
v5 = 1;
for ( result = detect_relay_card_conrad_4chan(a1, a2, a3, 0LL);
(_DWORD)result;
result = (*((__int64 (__fastcall **)(__int64, __int64, __int64, _QWORD))v3 + 4))(a1, a2, a3, 0LL) )
{
++v5;
v3 += 32;
if ( v5 == 6 )
{
relay_type = 0;
return 0xFFFFFFFFLL;
}
}
relay_type = v5;
return result;
}

original_code:
int crelay_detect_relay_card(char* portname, uint8_t* num_relays, char* serial, relay_info_t** my_relay_info)
{
   int i;
   for (i=1; i<LAST_RELAY_TYPE; i++)
   {
      if ((*relay_data[i].detect_relay_card_fun)(portname, num_relays, serial, ((void *)0)) == 0)
      {
         relay_type=i;
         return 0;
      }
   }
   relay_type = NO_RELAY_TYPE;
   return -1;
}

canonical_original_code:
int crelay_detect_relay_card(char *portname, unsigned char *num_relays, char *serial, struct relay_info **my_relay_info)
{
   int i;
   for (i=1; i<LAST_RELAY_TYPE; i++)
   {
      if ((*relay_data[i].detect_relay_card_fun)(portname, num_relays, serial, ((void *)0)) == 0)
      {
         relay_type=i;
         return 0;
      }
   }
   relay_type = NO_RELAY_TYPE;
   return -1;
}


decompiled_code:
__int64 __fastcall crelay_get_relay(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

if ( relay_type )
result = (*((__int64 (__fastcall **)(__int64, _QWORD))&relay_data + 4 * (unsigned int)relay_type + 1))(a1, a2);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( relay_type )
result = (*((__int64 (__fastcall **)(__int64, _QWORD))&relay_data + 4 * (unsigned int)relay_type + 1))(a1, a2);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int crelay_get_relay(char* portname, uint8_t relay, relay_state_t* relay_state, char* serial)
{
   if (relay_type != NO_RELAY_TYPE)
   {
      return (*relay_data[relay_type].get_relay_fun)(portname, relay, relay_state, serial);
   }
   else
   {
      return -1;
   }
}

canonical_original_code:
int crelay_get_relay(char *portname, unsigned char relay, enum relay_state_t *relay_state, char *serial)
{
   if (relay_type != NO_RELAY_TYPE)
   {
      return (*relay_data[relay_type].get_relay_fun)(portname, relay, relay_state, serial);
   }
   else
   {
      return -1;
   }
}


decompiled_code:
__int64 __fastcall crelay_set_relay(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

if ( relay_type )
result = (*((__int64 (__fastcall **)(__int64, _QWORD))&relay_data + 4 * (unsigned int)relay_type + 2))(a1, a2);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( relay_type )
result = (*((__int64 (__fastcall **)(__int64, _QWORD))&relay_data + 4 * (unsigned int)relay_type + 2))(a1, a2);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int crelay_set_relay(char* portname, uint8_t relay, relay_state_t relay_state, char* serial)
{
   if (relay_type != NO_RELAY_TYPE)
   {
      return (*relay_data[relay_type].set_relay_fun)(portname, relay, relay_state, serial);
   }
   else
   {
      return -1;
   }
}

canonical_original_code:
int crelay_set_relay(char *portname, unsigned char relay, enum relay_state_t relay_state, char *serial)
{
   if (relay_type != NO_RELAY_TYPE)
   {
      return (*relay_data[relay_type].set_relay_fun)(portname, relay, relay_state, serial);
   }
   else
   {
      return -1;
   }
}


decompiled_code:
__int64 crelay_get_relay_card_type()
{
return (unsigned int)relay_type;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)relay_type;
}

canonical_original_code:
enum relay_type_t crelay_get_relay_card_type()
{
   return relay_type;
}

return_type: enum relay_type_t { NO_RELAY_TYPE=0, CONRAD_4CHANNEL_USB_RELAY_TYPE=1, SAINSMART_USB_RELAY_TYPE=2, HID_API_RELAY_TYPE=3, SAINSMART16_USB_RELAY_TYPE=4, GENERIC_GPIO_RELAY_TYPE=5, LAST_RELAY_TYPE=6 }


decompiled_code:
__int64 __fastcall crelay_get_relay_card_name(unsigned int a1, char *a2)
{
if ( !a1 )
return 0xFFFFFFFFLL;
strcpy(a2, *((const char **)&relay_data + 4 * a1 + 3));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, char *a2)
{
if ( !a1 )
return 0xFFFFFFFFLL;
strcpy(a2, *((const char **)&relay_data + 4 * a1 + 3));
return 0LL;
}

original_code:
int crelay_get_relay_card_name(relay_type_t rtype, char* card_name)
{
   if (rtype != NO_RELAY_TYPE)
   {
      strcpy(card_name, relay_data[rtype].card_name);
      return 0;
   }
   else
   {
      return -1;
   }
}

canonical_original_code:
int crelay_get_relay_card_name(enum relay_type_t rtype, char *card_name)
{
   if (rtype != NO_RELAY_TYPE)
   {
      strcpy(card_name, relay_data[rtype].card_name);
      return 0;
   }
   else
   {
      return -1;
   }
}

variable_types: enum relay_type_t { NO_RELAY_TYPE=0, CONRAD_4CHANNEL_USB_RELAY_TYPE=1, SAINSMART_USB_RELAY_TYPE=2, HID_API_RELAY_TYPE=3, SAINSMART16_USB_RELAY_TYPE=4, GENERIC_GPIO_RELAY_TYPE=5, LAST_RELAY_TYPE=6 }


rstrip

decompiled_code:
const char *__fastcall rstrip(const char *a1)
{
size_t v1; // rax
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
__int64 v4; // rdx

v1 = strlen(a1);
v2 = &a1[v1];
if ( a1 < &a1[v1] )
{
v3 = _ctype_b_loc();
do
{
v4 = *(unsigned __int8 *)--v2;
if ( ((*v3)[v4] & 0x2000) == 0 )
break;
*v2 = 0;
}
while ( a1 != v2 );
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func2(const char *a1)
{
size_t v1; // rax
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
__int64 v4; // rdx

v1 = strlen(a1);
v2 = &a1[v1];
if ( a1 < &a1[v1] )
{
v3 = _ctype_b_loc();
do
{
v4 = *(unsigned __int8 *)--v2;
if ( ((*v3)[v4] & 0x2000) == 0 )
break;
*v2 = 0;
}
while ( a1 != v2 );
}
return a1;
}

original_code:
static char* rstrip(char* s)
{
    char* p = s + strlen(s);
    while (p > s && ((*__ctype_b_loc ())[(int) (((unsigned char)(*--p)))] & (unsigned short int) _ISspace))
        *p = '\0';
    return s;
}

canonical_original_code:
static char *rstrip(char *s)
{
    char *p = s + strlen(s);
    while (p > s && ((*__ctype_b_loc ())[(int) (((unsigned char)(*--p)))] & (unsigned short) _ISspace))
        *p = '\0';
    return s;
}

variable_types:
s
char *

variable_types:
p
char *

return_type: char *


decompiled_code:
__int64 __fastcall tst_bringfront(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 *v3; // rdx
unsigned int v4; // ebp
__int64 v5; // rbx
const void *v6; // rdx
__int64 v7; // rsi
__int64 result; // rax
__int64 v9; // rsi
unsigned int v10; // [rsp+Ch] [rbp-34Ch]
__int64 v11[105]; // [rsp+10h] [rbp-348h] BYREF

v2 = *a1;
if ( *a1 )
{
v3 = v11;
v4 = 0;
do
{
*v3 = (__int64)v2;
v2 = (_QWORD *)*v2;
++v4;
++v3;
}
while ( v2 );
v5 = 0LL;
do
{
v6 = (const void *)v11[v5];
v7 = (unsigned int)v5++;
printf("%d = (%p)\n", v7, v6);
}
while ( v4 != v5 );
dlq_printstring(a1, v7);
printf("Select node to print:");
__isoc99_scanf();
result = (int)v10;
if ( v4 > v10 )
{
v9 = v11[v10];
dlq_bringfront(a1, v9);
result = dlq_printstring(a1, v9);
}
}
else
{
dlq_printstring(a1, a2);
printf("Select node to print:");
result = __isoc99_scanf();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 *v3; // rdx
unsigned int v4; // ebp
__int64 v5; // rbx
const void *v6; // rdx
__int64 v7; // rsi
__int64 result; // rax
__int64 v9; // rsi
unsigned int v10; // [rsp+Ch] [rbp-34Ch]
__int64 v11[105]; // [rsp+10h] [rbp-348h] BYREF

v2 = *a1;
if ( *a1 )
{
v3 = v11;
v4 = 0;
do
{
*v3 = (__int64)v2;
v2 = (_QWORD *)*v2;
++v4;
++v3;
}
while ( v2 );
v5 = 0LL;
do
{
v6 = (const void *)v11[v5];
v7 = (unsigned int)v5++;
printf("%d = (%p)\n", v7, v6);
}
while ( v4 != v5 );
func17(a1, v7);
printf("Select node to print:");
__isoc99_scanf();
result = (int)v10;
if ( v4 > v10 )
{
v9 = v11[v10];
func14(a1, v9);
result = func17(a1, v9);
}
}
else
{
func17(a1, a2);
printf("Select node to print:");
result = __isoc99_scanf();
}
return result;
}

original_code:
void tst_bringfront(dlq_hdl_t *hdl) {
 dlq_node_t *nodea[100];
 int ch, j;
 dlq_node_t *temp = hdl->first;
 int nnodes = 0;
 while(temp != ((void *)0)) {
  nodea[nnodes] = temp;
  temp = temp->next;
  nnodes++;
 }
 for(j = 0; j < nnodes; j++) {
  printf("%d = (%p)\n", j, nodea[j]);
 }
 dlq_printstring(hdl);
 printf("Select node to print:");
 scanf("%d", &ch);
 if(ch < 0 || ch >= nnodes) {
  return;
 }
 dlq_bringfront(hdl, nodea[ch]);
 dlq_printstring(hdl);
 return;
}

canonical_original_code:
void tst_bringfront(struct dlq_hdl *hdl) {
 struct dlq_node *nodea[100];
 int ch; int j;
 struct dlq_node *temp = hdl->first;
 int nnodes = 0;
 while(temp != ((void *)0)) {
  nodea[nnodes] = temp;
  temp = temp->next;
  nnodes++;
 }
 for(j = 0; j < nnodes; j++) {
  printf("%d = (%p)\n", j, nodea[j]);
 }
 dlq_printstring(hdl);
 printf("Select node to print:");
 scanf("%d", &ch);
 if(ch < 0 || ch >= nnodes) {
  return;
 }
 dlq_bringfront(hdl, nodea[ch]);
 dlq_printstring(hdl);
 return;
}

variable_types: struct dlq_hdl { struct dlq_node { struct dlq_node *next; struct dlq_node *prev; void *data; } *first; struct dlq_node *last; int capacity; int size; } *

variable_types: struct dlq_node { struct dlq_node *next; struct dlq_node *prev; void *data; } *[100]

variable_types: ch int

variable_types: j int

variable_types: nnodes int

user_defined_types: struct dlq_hdl { struct dlq_node *first; struct dlq_node *last; int capacity; int size; }


decompiled_code:
// attributes: thunk
void dlq_destroy(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func12(void *ptr)
{
free(ptr);
}

original_code:
void
dlq_destroy(dlq_hdl_t *hdl) {
 free(hdl);
 hdl = ((void *)0);
}

canonical_original_code:
void dlq_destroy(struct dlq_hdl *hdl) {
 free(hdl);
 hdl = ((void *)0);
}

user_defined_types: struct dlq_node { struct dlq_node *next; struct dlq_node *prev; void *data; }


decompiled_code:
__int64 __fastcall dlq_dequeue(__int64 a1)
{
_QWORD *v2; // rdi
__int64 v3; // r12
_QWORD *v4; // rax
__int64 result; // rax

if ( !a1 )
return 0LL;
v2 = *(_QWORD **)(a1 + 8);
if ( !v2 )
return 0LL;
v3 = v2[2];
--*(_DWORD *)(a1 + 20);
if ( v2 == *(_QWORD **)a1 )
{
free(v2);
*(_QWORD *)(a1 + 8) = 0LL;
result = v3;
*(_QWORD *)a1 = 0LL;
}
else
{
v4 = (_QWORD *)v2[1];
*(_QWORD *)(a1 + 8) = v4;
*v4 = 0LL;
free(v2);
result = v3;
}
return result;
}

original_code:
void *
dlq_dequeue(dlq_hdl_t *hdl) {
 void *retdata;
 if(hdl == ((void *)0)) {
  return ((void *)0);
 }
 if(hdl->last == ((void *)0)) {
  return ((void *)0);
 }
 retdata = hdl->last->data;
 hdl->size--;
 if(hdl->first == hdl->last) {
  free(hdl->last);
  hdl->last = ((void *)0);
  hdl->first = ((void *)0);
  return retdata;
 }
 dlq_node_t *temp = hdl->last;
 hdl->last = hdl->last->prev;
 hdl->last->next = ((void *)0);
 free(temp);
 temp = ((void *)0);
 return retdata;
}

canonical_original_code:
void *dlq_dequeue(struct dlq_hdl *hdl) {
 void *retdata;
 if(hdl == ((void *)0)) {
  return ((void *)0);
 }
 if(hdl->last == ((void *)0)) {
  return ((void *)0);
 }
 retdata = hdl->last->data;
 hdl->size--;
 if(hdl->first == hdl->last) {
  free(hdl->last);
  hdl->last = ((void *)0);
  hdl->first = ((void *)0);
  return retdata;
 }
 struct dlq_node *temp = hdl->last;
 hdl->last = hdl->last->prev;
 hdl->last->next = ((void *)0);
 free(temp);
 temp = ((void *)0);
 return retdata;
}

variable_types:
retdata
void *

variable_types: struct dlq_node { struct dlq_node *next; struct dlq_node *prev; void *data; } *

return_type: void *


decompiled_code:
__int64 __fastcall dlq_capacity(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
int
dlq_capacity(dlq_hdl_t *hdl) {
 return hdl->capacity;
}

canonical_original_code:
int dlq_capacity(struct dlq_hdl *hdl) {
 return hdl->capacity;
}


decompiled_code:
__int64 __fastcall dlq_size(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

original_code:
int
dlq_size(dlq_hdl_t *hdl) {
 return hdl->size;
}

canonical_original_code:
int dlq_size(struct dlq_hdl *hdl) {
 return hdl->size;
}


decompiled_code:
int __fastcall dlq_printstring(_QWORD **a1)
{
_QWORD *i; // rax

for ( i = *a1; i; i = (_QWORD *)*i )
;
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func17(_QWORD **a1)
{
_QWORD *i; // rax

for ( i = *a1; i; i = (_QWORD *)*i )
;
return putchar(10);
}

original_code:
void
dlq_printstring(dlq_hdl_t *hdl) {
 dlq_node_t *s;
 s = hdl->first;
 while(s != ((void *)0)) {
  s = s->next;
 }
 printf("\n");
 return;
}

canonical_original_code:
void dlq_printstring(struct dlq_hdl *hdl) {
 struct dlq_node *s;
 s = hdl->first;
 while(s != ((void *)0)) {
  s = s->next;
 }
 printf("\n");
 return;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3 = dlq_create(5LL, argv, envp);
if ( v3 )
{
v4 = (void *)v3;
while ( 2 )
{
puts("1 - Enqueue data.");
puts("2 - Dequeue data.");
puts("3 - Print queue.");
puts("4 - Print queue size.");
puts("5 - Bringfront.");
__isoc99_scanf();
switch ( v6[0] )
{
case 1:
tst_enqueue(v4, v6);
goto LABEL_5;
case 2:
tst_dequeue(v4, v6);
goto LABEL_5;
case 3:
tst_print(v4, v6);
goto LABEL_5;
case 4:
tst_size(v4, v6);
goto LABEL_5;
case 5:
tst_bringfront(v4, v6);
LABEL_5:
if ( (unsigned int)(v6[0] - 1) > 4 )
return 1;
continue;
default:
dlq_destroy(v4);
return 0;
}
}
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3 = dlq_create(5LL, argv, envp);
if ( v3 )
{
v4 = (void *)v3;
while ( 2 )
{
puts("1 - Enqueue data.");
puts("2 - Dequeue data.");
puts("3 - Print queue.");
puts("4 - Print queue size.");
puts("5 - Bringfront.");
__isoc99_scanf();
switch ( v6[0] )
{
case 1:
func8(v4, v6);
goto LABEL_5;
case 2:
func9(v4, v6);
goto LABEL_5;
case 3:
func7(v4, v6);
goto LABEL_5;
case 4:
func10(v4, v6);
goto LABEL_5;
case 5:
func11(v4, v6);
LABEL_5:
if ( (unsigned int)(v6[0] - 1) > 4 )
return 1;
continue;
default:
func12(v4);
return 0;
}
}
}
return 1;
}

original_code:
int
main() {
 int ch;
 dlq_hdl_t *q_hdl;
 q_hdl = dlq_create(5);
 if(q_hdl == ((void *)0)) {
  return 1;
 }
 do {
  printf("1 - Enqueue data.\n");
  printf("2 - Dequeue data.\n");
  printf("3 - Print queue.\n");
  printf("4 - Print queue size.\n");
  printf("5 - Bringfront.\n");
  scanf("%d", &ch);
  switch(ch) {
  case 1:
   tst_enqueue(q_hdl);
   break;
  case 2:
   tst_dequeue(q_hdl);
   break;
  case 3:
   tst_print(q_hdl);
   break;
  case 4:
   tst_size(q_hdl);
   break;
  case 5:
   tst_bringfront(q_hdl);
   break;
  default:
   dlq_destroy(q_hdl);
   q_hdl = ((void *)0);
   return 0;
   break;
  }
 } while (ch > 0 && ch < 6);
 return 1;
}

canonical_original_code:
int main() {
 int ch;
 struct dlq_hdl *q_hdl;
 q_hdl = dlq_create(5);
 if(q_hdl == ((void *)0)) {
  return 1;
 }
 do {
  printf("1 - Enqueue data.\n");
  printf("2 - Dequeue data.\n");
  printf("3 - Print queue.\n");
  printf("4 - Print queue size.\n");
  printf("5 - Bringfront.\n");
  scanf("%d", &ch);
  switch(ch) {
  case 1:
   tst_enqueue(q_hdl);
   break;
  case 2:
   tst_dequeue(q_hdl);
   break;
  case 3:
   tst_print(q_hdl);
   break;
  case 4:
   tst_size(q_hdl);
   break;
  case 5:
   tst_bringfront(q_hdl);
   break;
  default:
   dlq_destroy(q_hdl);
   q_hdl = ((void *)0);
   return 0;
   break;
  }
 } while (ch > 0 && ch < 6);
 return 1;
}


decompiled_code:
// attributes: thunk
__int64 __fastcall tst_print(__int64 a1, __int64 a2)
{
return dlq_printstring(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
return func17(a1, a2);
}

original_code:
static void
tst_print(dlq_hdl_t *hdl) {
 dlq_printstring(hdl);
}

canonical_original_code:
static void tst_print(struct dlq_hdl *hdl) {
 dlq_printstring(hdl);
}


decompiled_code:
void __fastcall tst_enqueue(__int64 a1)
{
void *v1; // rbx
_DWORD *v2; // rsi
int v3; // [rsp+4h] [rbp-24h]
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
printf("Enter datalength:");
__isoc99_scanf();
v1 = malloc(v3);
printf("Enter data:");
__isoc99_scanf();
v2 = malloc(0x10uLL);
*(_QWORD *)v2 = v1;
v2[2] = v3;
dlq_enqueue(a1, v2, &ptr);
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1)
{
void *v1; // rbx
_DWORD *v2; // rsi
int v3; // [rsp+4h] [rbp-24h]
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
printf("Enter datalength:");
__isoc99_scanf();
v1 = malloc(v3);
printf("Enter data:");
__isoc99_scanf();
v2 = malloc(0x10uLL);
*(_QWORD *)v2 = v1;
v2[2] = v3;
dlq_enqueue(a1, v2, &ptr);
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

original_code:
static void
tst_enqueue(dlq_hdl_t *hdl) {
 tst_data_t *q_data;
 tst_data_t *ret_data = ((void *)0);
 int in_datalen;
 char *in_data;
 printf("Enter datalength:");
 scanf("%d", &in_datalen);
 in_data = (char *) malloc(sizeof(char)*in_datalen);
 printf("Enter data:");
 scanf("%s", in_data);
 q_data = (tst_data_t *) malloc(sizeof(tst_data_t));
 q_data->tst_datalen = in_datalen;
 q_data->tst_data = in_data;
 dlq_enqueue(hdl, (void *) q_data, (void **)&ret_data);
 if(ret_data != ((void *)0)) {
  free(ret_data->tst_data);
  ret_data->tst_data = ((void *)0);
  free(ret_data);
 }
}

canonical_original_code:
static void tst_enqueue(struct dlq_hdl *hdl) {
 struct tst_data *q_data;
 struct tst_data *ret_data = ((void *)0);
 int in_datalen;
 char *in_data;
 printf("Enter datalength:");
 scanf("%d", &in_datalen);
 in_data = (char *) malloc(sizeof(char)*in_datalen);
 printf("Enter data:");
 scanf("%s", in_data);
 q_data = (struct tst_data *) malloc(sizeof(tst_data_t));
 q_data->tst_datalen = in_datalen;
 q_data->tst_data = in_data;
 dlq_enqueue(hdl, (void *) q_data, (void **)&ret_data);
 if(ret_data != ((void *)0)) {
  free(ret_data->tst_data);
  ret_data->tst_data = ((void *)0);
  free(ret_data);
 }
}

variable_types: struct tst_data { char *tst_data; int tst_datalen; } *


decompiled_code:
void tst_dequeue()
{
const char **v0; // rax
void **v1; // rbp

v0 = (const char **)dlq_dequeue();
if ( v0 )
{
v1 = (void **)v0;
printf("Dequeue data => %s\n", *v0);
free(*v1);
free(v1);
}
else
{
puts("Queue is empty");
}
}

canonical_decompiled_code:
void func9()
{
const char **v0; // rax
void **v1; // rbp

v0 = (const char **)func13();
if ( v0 )
{
v1 = (void **)v0;
printf("Dequeue data => %s\n", *v0);
free(*v1);
free(v1);
}
else
{
puts("Queue is empty");
}
}

original_code:
static void
tst_dequeue(dlq_hdl_t *hdl) {
 tst_data_t *q_data;
 q_data = (tst_data_t *)dlq_dequeue(hdl);
 if(q_data == ((void *)0)) {
  printf("Queue is empty\n");
  return;
 }
 printf("Dequeue data => %s\n", q_data->tst_data);
 free(q_data->tst_data);
 q_data->tst_data = ((void *)0);
 free(q_data);
 q_data = ((void *)0);
}

canonical_original_code:
static void tst_dequeue(struct dlq_hdl *hdl) {
 struct tst_data *q_data;
 q_data = (struct tst_data *)dlq_dequeue(hdl);
 if(q_data == ((void *)0)) {
  printf("Queue is empty\n");
  return;
 }
 printf("Dequeue data => %s\n", q_data->tst_data);
 free(q_data->tst_data);
 q_data->tst_data = ((void *)0);
 free(q_data);
 q_data = ((void *)0);
}

user_defined_types: struct tst_data { char *tst_data; int tst_datalen; }


decompiled_code:
int tst_size()
{
unsigned int v0; // eax

v0 = dlq_size();
return printf("Size of queue is %d\n", v0);
}

canonical_decompiled_code:
int func10()
{
unsigned int v0; // eax

v0 = func16();
return printf("Size of queue is %d\n", v0);
}

original_code:
static void
tst_size(dlq_hdl_t *hdl) {
 printf("Size of queue is %d\n", dlq_size(hdl));
}

canonical_original_code:
static void tst_size(struct dlq_hdl *hdl) {
 printf("Size of queue is %d\n", dlq_size(hdl));
}


decompiled_code:
__int64 __fastcall tst_hash_put(__int64 a1)
{
void *v1; // r12
void *v2; // rbx
_DWORD *v3; // rax
_DWORD *v4; // rbp
_DWORD *v5; // rax
int v7; // [rsp+8h] [rbp-30h]
int v8; // [rsp+Ch] [rbp-2Ch]

printf("Enter keylength :");
__isoc99_scanf();
v1 = malloc(v7);
printf("Enter key:");
__isoc99_scanf();
printf("Enter datalength :");
__isoc99_scanf();
v2 = malloc(v8);
printf("Enter data:");
__isoc99_scanf();
v3 = malloc(0x10uLL);
*(_QWORD *)v3 = v1;
v4 = v3;
v3[2] = v7;
v5 = malloc(0x10uLL);
*(_QWORD *)v5 = v2;
v5[2] = v8;
return hash_put(a1, v4);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
void *v1; // r12
void *v2; // rbx
_DWORD *v3; // rax
_DWORD *v4; // rbp
_DWORD *v5; // rax
int v7; // [rsp+8h] [rbp-30h]
int v8; // [rsp+Ch] [rbp-2Ch]

printf("Enter keylength :");
__isoc99_scanf();
v1 = malloc(v7);
printf("Enter key:");
__isoc99_scanf();
printf("Enter datalength :");
__isoc99_scanf();
v2 = malloc(v8);
printf("Enter data:");
__isoc99_scanf();
v3 = malloc(0x10uLL);
*(_QWORD *)v3 = v1;
v4 = v3;
v3[2] = v7;
v5 = malloc(0x10uLL);
*(_QWORD *)v5 = v2;
v5[2] = v8;
return func15(a1, v4);
}

original_code:
static void
tst_hash_put(hash_hdl_t *hdl) {
 int keylen, datalen;
 char *key, *data;
 tst_key_t *hash_key;
 tst_data_t *hash_data;
 printf("Enter keylength :");
 scanf("%d", &keylen);
 key = (char *)malloc(sizeof(char)*keylen);
 printf("Enter key:");
 scanf("%s", key);
 printf("Enter datalength :");
 scanf("%d", &datalen);
 data = (char *) malloc(sizeof(char)*datalen);
 printf("Enter data:");
 scanf("%s", data);
 hash_key = (tst_key_t *)malloc(sizeof(tst_key_t));
 hash_key->tst_key = key;
 hash_key->tst_keylen = keylen;
 hash_data = (tst_data_t *)malloc(sizeof(tst_data_t));
 hash_data->tst_data = data;
 hash_data->tst_datalen = datalen;
 hash_put(hdl, (void *)hash_key, (void *)hash_data);
}

canonical_original_code:
static void tst_hash_put(struct hash_hdl *hdl) {
 int keylen; int datalen;
 char *key; char *data;
 struct tst_key *hash_key;
 struct tst_data *hash_data;
 printf("Enter keylength :");
 scanf("%d", &keylen);
 key = (char *)malloc(sizeof(char)*keylen);
 printf("Enter key:");
 scanf("%s", key);
 printf("Enter datalength :");
 scanf("%d", &datalen);
 data = (char *) malloc(sizeof(char)*datalen);
 printf("Enter data:");
 scanf("%s", data);
 hash_key = (struct tst_key *)malloc(sizeof(tst_key_t));
 hash_key->tst_key = key;
 hash_key->tst_keylen = keylen;
 hash_data = (struct tst_data *)malloc(sizeof(tst_data_t));
 hash_data->tst_data = data;
 hash_data->tst_datalen = datalen;
 hash_put(hdl, (void *)hash_key, (void *)hash_data);
}

variable_types: struct hash_hdl { int M; int size; struct hash_node { struct hash_node *next; void *key; void *data; } **array; int (*comparefn)(void *key1, void *key2); int (*hashfn)(struct hash_hdl *, void *key); } *

variable_types: keylen int

variable_types: datalen int

variable_types:
key
char *

variable_types:
data
char *

variable_types: struct tst_key { char *tst_key; int tst_keylen; } *

user_defined_types: struct hash_hdl { int M; int size; struct hash_node **array; int (*comparefn)(void *key1, void *key2); int (*hashfn)(struct hash_hdl *, void *key); }


decompiled_code:
void __fastcall tst_hash_get(__int64 a1)
{
char *v1; // r12
const char **v2; // rax
const char **v3; // rbp
const char **v4; // rax
int v5; // [rsp+Ch] [rbp-1Ch]

printf("Enter keylength :");
__isoc99_scanf();
v1 = (char *)malloc(v5);
printf("Enter key:");
__isoc99_scanf();
v2 = (const char **)malloc(0x10uLL);
*v2 = v1;
v3 = v2;
*((_DWORD *)v2 + 2) = v5;
v4 = (const char **)hash_get(a1, v2);
if ( v4 )
printf("Key (%s), Data (%s)\n", *v3, *v4);
free(v1);
free(v3);
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
char *v1; // r12
const char **v2; // rax
const char **v3; // rbp
const char **v4; // rax
int v5; // [rsp+Ch] [rbp-1Ch]

printf("Enter keylength :");
__isoc99_scanf();
v1 = (char *)malloc(v5);
printf("Enter key:");
__isoc99_scanf();
v2 = (const char **)malloc(0x10uLL);
*v2 = v1;
v3 = v2;
*((_DWORD *)v2 + 2) = v5;
v4 = (const char **)func16(a1, v2);
if ( v4 )
printf("Key (%s), Data (%s)\n", *v3, *v4);
free(v1);
free(v3);
}

original_code:
static void
tst_hash_get(hash_hdl_t *hdl) {
 int keylen;
 char *key;
 tst_key_t *hash_key;
 tst_data_t *hash_data;
 printf("Enter keylength :");
 scanf("%d", &keylen);
 key = (char *)malloc(sizeof(char)*keylen);
 printf("Enter key:");
 scanf("%s", key);
 hash_key = (tst_key_t *)malloc(sizeof(tst_key_t));
 hash_key->tst_key = key;
 hash_key->tst_keylen = keylen;
 hash_data = (tst_data_t *)hash_get(hdl, (void *)hash_key);
 if(hash_data != ((void *)0)) {
  printf("Key (%s), Data (%s)\n", hash_key->tst_key,
    hash_data->tst_data);
 }
 free(key);
 key = ((void *)0);
 free(hash_key);
 hash_key = ((void *)0);
}

canonical_original_code:
static void tst_hash_get(struct hash_hdl *hdl) {
 int keylen;
 char *key;
 struct tst_key *hash_key;
 struct tst_data *hash_data;
 printf("Enter keylength :");
 scanf("%d", &keylen);
 key = (char *)malloc(sizeof(char)*keylen);
 printf("Enter key:");
 scanf("%s", key);
 hash_key = (struct tst_key *)malloc(sizeof(tst_key_t));
 hash_key->tst_key = key;
 hash_key->tst_keylen = keylen;
 hash_data = (struct tst_data *)hash_get(hdl, (void *)hash_key);
 if(hash_data != ((void *)0)) {
  printf("Key (%s), Data (%s)\n", hash_key->tst_key,
    hash_data->tst_data);
 }
 free(key);
 key = ((void *)0);
 free(hash_key);
 hash_key = ((void *)0);
}

user_defined_types: struct tst_key { char *tst_key; int tst_keylen; }

user_defined_types: struct hash_node { struct hash_node *next; void *key; void *data; }


decompiled_code:
void __fastcall tst_hash_delete(__int64 a1)
{
void *v1; // r12
_QWORD *v2; // rax
void *v3; // rbp
int v4; // [rsp+Ch] [rbp-2Ch]
void *ptr; // [rsp+10h] [rbp-28h] BYREF
void *v6; // [rsp+18h] [rbp-20h] BYREF

ptr = 0LL;
v6 = 0LL;
printf("Enter keylength :");
__isoc99_scanf();
v1 = malloc(v4);
printf("Enter key:");
__isoc99_scanf();
v2 = malloc(0x10uLL);
*v2 = v1;
v3 = v2;
*((_DWORD *)v2 + 2) = v4;
hash_delete(a1, v2, &ptr, &v6);
free(v3);
free(v1);
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
ptr = 0LL;
}
if ( v6 )
{
free(*(void **)v6);
free(v6);
}
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1)
{
void *v1; // r12
_QWORD *v2; // rax
void *v3; // rbp
int v4; // [rsp+Ch] [rbp-2Ch]
void *ptr; // [rsp+10h] [rbp-28h] BYREF
void *v6; // [rsp+18h] [rbp-20h] BYREF

ptr = 0LL;
v6 = 0LL;
printf("Enter keylength :");
__isoc99_scanf();
v1 = malloc(v4);
printf("Enter key:");
__isoc99_scanf();
v2 = malloc(0x10uLL);
*v2 = v1;
v3 = v2;
*((_DWORD *)v2 + 2) = v4;
func17(a1, v2, &ptr, &v6);
free(v3);
free(v1);
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
ptr = 0LL;
}
if ( v6 )
{
free(*(void **)v6);
free(v6);
}
}

original_code:
static void
tst_hash_delete(hash_hdl_t *hdl) {
 int keylen;
 char *key;
 tst_key_t *hash_key;
 tst_key_t *retkey = ((void *)0);
 tst_data_t *retdata = ((void *)0);
 printf("Enter keylength :");
 scanf("%d", &keylen);
 key = (char *)malloc(sizeof(char)*keylen);
 printf("Enter key:");
 scanf("%s", key);
 hash_key = (tst_key_t *)malloc(sizeof(tst_key_t));
 hash_key->tst_key = key;
 hash_key->tst_keylen = keylen;
 hash_delete(hdl, (void *)hash_key, (void **)&retkey,
   (void **)&retdata);
 free(hash_key);
 hash_key = ((void *)0);
 free(key);
 key = ((void *)0);
 if(retkey != ((void *)0)) {
  free(retkey->tst_key);
  retkey->tst_key = ((void *)0);
  free(retkey);
  retkey = ((void *)0);
 }
 if(retdata != ((void *)0)) {
  free(retdata->tst_data);
  retdata->tst_data = ((void *)0);
  free(retdata);
  retdata = ((void *)0);
 }
}

canonical_original_code:
static void tst_hash_delete(struct hash_hdl *hdl) {
 int keylen;
 char *key;
 struct tst_key *hash_key;
 struct tst_key *retkey = ((void *)0);
 struct tst_data *retdata = ((void *)0);
 printf("Enter keylength :");
 scanf("%d", &keylen);
 key = (char *)malloc(sizeof(char)*keylen);
 printf("Enter key:");
 scanf("%s", key);
 hash_key = (struct tst_key *)malloc(sizeof(tst_key_t));
 hash_key->tst_key = key;
 hash_key->tst_keylen = keylen;
 hash_delete(hdl, (void *)hash_key, (void **)&retkey,
   (void **)&retdata);
 free(hash_key);
 hash_key = ((void *)0);
 free(key);
 key = ((void *)0);
 if(retkey != ((void *)0)) {
  free(retkey->tst_key);
  retkey->tst_key = ((void *)0);
  free(retkey);
  retkey = ((void *)0);
 }
 if(retdata != ((void *)0)) {
  free(retdata->tst_data);
  retdata->tst_data = ((void *)0);
  free(retdata);
  retdata = ((void *)0);
 }
}


decompiled_code:
// attributes: thunk
__int64 __fastcall tst_hash_print(__int64 a1, __int64 a2)
{
return hash_printstring(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2)
{
return func20(a1, a2);
}

original_code:
static void
tst_hash_print(hash_hdl_t *hdl) {
 hash_printstring(hdl);
}

canonical_original_code:
static void tst_hash_print(struct hash_hdl *hdl) {
 hash_printstring(hdl);
}


decompiled_code:
int tst_hash_printsize()
{
unsigned int v0; // eax

v0 = hash_size();
return printf("Size of hash is %d\n", v0);
}

canonical_decompiled_code:
int func13()
{
unsigned int v0; // eax

v0 = func18();
return printf("Size of hash is %d\n", v0);
}

original_code:
static void
tst_hash_printsize(hash_hdl_t *hdl) {
 printf("Size of hash is %d\n", hash_size(hdl));
}

canonical_original_code:
static void tst_hash_printsize(struct hash_hdl *hdl) {
 printf("Size of hash is %d\n", hash_size(hdl));
}


decompiled_code:
void __fastcall hash_destroy(void *ptr)
{
int v2; // eax
_QWORD *v3; // rdi
__int64 v4; // rbp
_QWORD *v5; // rbx
void *v6; // rdi

v2 = *(_DWORD *)ptr;
v3 = (_QWORD *)*((_QWORD *)ptr + 1);
if ( v2 > 0 )
{
v4 = 0LL;
do
{
v5 = (_QWORD *)v3[v4];
if ( v5 )
{
do
{
v6 = v5;
v5 = (_QWORD *)*v5;
free(v6);
}
while ( v5 );
v3 = (_QWORD *)*((_QWORD *)ptr + 1);
}
++v4;
}
while ( *(_DWORD *)ptr > (int)v4 );
}
free(v3);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
int v2; // eax
_QWORD *v3; // rdi
__int64 v4; // rbp
_QWORD *v5; // rbx
void *v6; // rdi

v2 = *(_DWORD *)ptr;
v3 = (_QWORD *)*((_QWORD *)ptr + 1);
if ( v2 > 0 )
{
v4 = 0LL;
do
{
v5 = (_QWORD *)v3[v4];
if ( v5 )
{
do
{
v6 = v5;
v5 = (_QWORD *)*v5;
free(v6);
}
while ( v5 );
v3 = (_QWORD *)*((_QWORD *)ptr + 1);
}
++v4;
}
while ( *(_DWORD *)ptr > (int)v4 );
}
free(v3);
free(ptr);
}

original_code:
void
hash_destroy(hash_hdl_t *hdl) {
 int i;
 for(i = 0; i < hdl->M; i++) {
  hash_node_t *temp1, *temp2;
  temp1 = hdl->array[i];
  while(temp1 != ((void *)0)) {
   temp2 = temp1;
   temp1 = temp1->next;
   free(temp2);
   temp2 = ((void *)0);
  }
 }
 free(hdl->array);
 hdl->array = ((void *)0);
 free(hdl);
 hdl = ((void *)0);
 return;
}

canonical_original_code:
void hash_destroy(struct hash_hdl *hdl) {
 int i;
 for(i = 0; i < hdl->M; i++) {
  struct hash_node *temp1; struct hash_node *temp2;
  temp1 = hdl->array[i];
  while(temp1 != ((void *)0)) {
   temp2 = temp1;
   temp1 = temp1->next;
   free(temp2);
   temp2 = ((void *)0);
  }
 }
 free(hdl->array);
 hdl->array = ((void *)0);
 free(hdl);
 hdl = ((void *)0);
 return;
}

variable_types: i int

variable_types: struct hash_node { struct hash_node *next; void *key; void *data; } *


original_code:
void
hash_put(hash_hdl_t *hdl, void *key, void *data) {
 int hash;
 if(hdl == ((void *)0) || key == ((void *)0) || data == ((void *)0)) {
  return;
 }
 hash = hdl->hashfn(hdl, key);
 if(hash < 0) {
  return;
 }
 hash_node_t *newnode;
 newnode = (hash_node_t *)malloc(sizeof(hash_node_t));
 if(newnode == ((void *)0)) {
  return;
 }
 newnode->next = ((void *)0);
 newnode->key = key;
 newnode->data = data;
 newnode->next = hdl->array[hash];
 hdl->array[hash] = newnode;
 hdl->size++;
 return;
}

canonical_original_code:
void hash_put(struct hash_hdl *hdl, void *key, void *data) {
 int hash;
 if(hdl == ((void *)0) || key == ((void *)0) || data == ((void *)0)) {
  return;
 }
 hash = hdl->hashfn(hdl, key);
 if(hash < 0) {
  return;
 }
 struct hash_node *newnode;
 newnode = (struct hash_node *)malloc(sizeof(hash_node_t));
 if(newnode == ((void *)0)) {
  return;
 }
 newnode->next = ((void *)0);
 newnode->key = key;
 newnode->data = data;
 newnode->next = hdl->array[hash];
 hdl->array[hash] = newnode;
 hdl->size++;
 return;
}

variable_types:
key
void *

variable_types:
data
void *

variable_types: hash int


original_code:
void
hash_delete(hash_hdl_t *hdl, void *key, void **retkey, void **retdata) {
 int hash;
 if(hdl == ((void *)0) || key == ((void *)0)) {
  return;
 }
 hash = hdl->hashfn(hdl, key);
 if(hash < 0) {
  return;
 }
 hash_node_t *bucket;
 bucket = hdl->array[hash];
 if(bucket == ((void *)0)) {
  return;
 }
 if(hdl->comparefn(key, bucket->key) == 0) {
  hdl->array[hash] = bucket->next;
  hdl->size--;
  *retkey = bucket->key;
  *retdata = bucket->data;
  free(bucket);
  bucket = ((void *)0);
  return;
 }
 while(bucket->next != ((void *)0)) {
  int cmp;
  cmp = hdl->comparefn(key, bucket->next->key);
  if(cmp == 0) {
   hash_node_t *temp;
   temp = bucket->next;
   bucket->next = bucket->next->next;
   hdl->size--;
   *retkey = temp->key;
   *retdata = temp->data;
   free(temp);
   temp = ((void *)0);
   return;
  }
  bucket = bucket->next;
 }
 return;
}

canonical_original_code:
void hash_delete(struct hash_hdl *hdl, void *key, void **retkey, void **retdata) {
 int hash;
 if(hdl == ((void *)0) || key == ((void *)0)) {
  return;
 }
 hash = hdl->hashfn(hdl, key);
 if(hash < 0) {
  return;
 }
 struct hash_node *bucket;
 bucket = hdl->array[hash];
 if(bucket == ((void *)0)) {
  return;
 }
 if(hdl->comparefn(key, bucket->key) == 0) {
  hdl->array[hash] = bucket->next;
  hdl->size--;
  *retkey = bucket->key;
  *retdata = bucket->data;
  free(bucket);
  bucket = ((void *)0);
  return;
 }
 while(bucket->next != ((void *)0)) {
  int cmp;
  cmp = hdl->comparefn(key, bucket->next->key);
  if(cmp == 0) {
   struct hash_node *temp;
   temp = bucket->next;
   bucket->next = bucket->next->next;
   hdl->size--;
   *retkey = temp->key;
   *retdata = temp->data;
   free(temp);
   temp = ((void *)0);
   return;
  }
  bucket = bucket->next;
 }
 return;
}

variable_types:
retkey
void **

variable_types:
retdata
void **

variable_types: cmp int


decompiled_code:
__int64 __fastcall hash_size(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
int
hash_size(hash_hdl_t *hdl) {
 return hdl->size;
}

canonical_original_code:
int hash_size(struct hash_hdl *hdl) {
 return hdl->size;
}


decompiled_code:
__int64 __fastcall hash_buckets(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func19(unsigned int *a1)
{
return *a1;
}

original_code:
int
hash_buckets(hash_hdl_t *hdl) {
 return hdl->M;
}

canonical_original_code:
int hash_buckets(struct hash_hdl *hdl) {
 return hdl->M;
}


decompiled_code:
int __fastcall hash_printstring(__int64 a1)
{
int result; // eax
__int64 v2; // rbp
_QWORD *v3; // rbx

result = *(_DWORD *)a1;
if ( *(int *)a1 > 0 )
{
v2 = 0LL;
do
{
v3 = *(_QWORD **)(*(_QWORD *)(a1 + 8) + 8 * v2);
printf("hash[%d] => ", (unsigned int)v2);
for ( ; v3; v3 = (_QWORD *)*v3 )
;
++v2;
result = putchar(10);
}
while ( *(_DWORD *)a1 > (int)v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func20(__int64 a1)
{
int result; // eax
__int64 v2; // rbp
_QWORD *v3; // rbx

result = *(_DWORD *)a1;
if ( *(int *)a1 > 0 )
{
v2 = 0LL;
do
{
v3 = *(_QWORD **)(*(_QWORD *)(a1 + 8) + 8 * v2);
printf("hash[%d] => ", (unsigned int)v2);
for ( ; v3; v3 = (_QWORD *)*v3 )
;
++v2;
result = putchar(10);
}
while ( *(_DWORD *)a1 > (int)v2 );
}
return result;
}

original_code:
void
hash_printstring(hash_hdl_t *hdl) {
 int i;
 for(i = 0; i < hdl->M; i++) {
  hash_node_t *bucket;
  bucket = hdl->array[i];
  printf("hash[%d] => ", i);
  while(bucket != ((void *)0)) {
   bucket = bucket->next;
  }
  printf("\n");
 }
}

canonical_original_code:
void hash_printstring(struct hash_hdl *hdl) {
 int i;
 for(i = 0; i < hdl->M; i++) {
  struct hash_node *bucket;
  bucket = hdl->array[i];
  printf("hash[%d] => ", i);
  while(bucket != ((void *)0)) {
   bucket = bucket->next;
  }
  printf("\n");
 }
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
int v6[4]; // [rsp+8h] [rbp-10h] BYREF

printf("Enter no. of buckets:");
__isoc99_scanf();
v3 = hash_create((unsigned int)v6[1], tst_hash_compare, tst_hash);
if ( v3 )
{
v4 = (void *)v3;
while ( 2 )
{
puts("1 - Add data.");
puts("2 - Get data.");
puts("3 - Del data.");
puts("4 - Print Hash.");
puts("5 - Print Hash size.");
__isoc99_scanf();
switch ( v6[0] )
{
case 1:
tst_hash_put(v4, v6);
goto LABEL_5;
case 2:
tst_hash_get(v4, v6);
goto LABEL_5;
case 3:
tst_hash_delete(v4, v6);
goto LABEL_5;
case 4:
tst_hash_print(v4, v6);
goto LABEL_5;
case 5:
tst_hash_printsize(v4, v6);
LABEL_5:
if ( (unsigned int)(v6[0] - 1) > 4 )
return 1;
continue;
default:
hash_destroy(v4);
return 0;
}
}
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
int v6[4]; // [rsp+8h] [rbp-10h] BYREF

printf("Enter no. of buckets:");
__isoc99_scanf();
v3 = hash_create((unsigned int)v6[1], tst_hash_compare, tst_hash);
if ( v3 )
{
v4 = (void *)v3;
while ( 2 )
{
puts("1 - Add data.");
puts("2 - Get data.");
puts("3 - Del data.");
puts("4 - Print Hash.");
puts("5 - Print Hash size.");
__isoc99_scanf();
switch ( v6[0] )
{
case 1:
func9(v4, v6);
goto LABEL_5;
case 2:
func10(v4, v6);
goto LABEL_5;
case 3:
func11(v4, v6);
goto LABEL_5;
case 4:
func12(v4, v6);
goto LABEL_5;
case 5:
func13(v4, v6);
LABEL_5:
if ( (unsigned int)(v6[0] - 1) > 4 )
return 1;
continue;
default:
func14(v4);
return 0;
}
}
}
return 1;
}

original_code:
int
main() {
 int ch;
 int capacity;
 hash_hdl_t *h_hdl;
 printf("Enter no. of buckets:");
 scanf("%d", &capacity);
 h_hdl = hash_create(capacity, tst_hash_compare, tst_hash);
 if(h_hdl == ((void *)0)) {
  return 1;
 }
 do {
  printf("1 - Add data.\n");
  printf("2 - Get data.\n");
  printf("3 - Del data.\n");
  printf("4 - Print Hash.\n");
  printf("5 - Print Hash size.\n");
  scanf("%d", &ch);
  switch(ch) {
  case 1:
   tst_hash_put(h_hdl);
   break;
  case 2:
   tst_hash_get(h_hdl);
   break;
  case 3:
   tst_hash_delete(h_hdl);
   break;
  case 4:
   tst_hash_print(h_hdl);
   break;
  case 5:
   tst_hash_printsize(h_hdl);
   break;
  default:
   hash_destroy(h_hdl);
   h_hdl = ((void *)0);
   return 0;
   break;
  }
 } while (ch > 0 && ch < 6);
 return 1;
}

canonical_original_code:
int main() {
 int ch;
 int capacity;
 struct hash_hdl *h_hdl;
 printf("Enter no. of buckets:");
 scanf("%d", &capacity);
 h_hdl = hash_create(capacity, tst_hash_compare, tst_hash);
 if(h_hdl == ((void *)0)) {
  return 1;
 }
 do {
  printf("1 - Add data.\n");
  printf("2 - Get data.\n");
  printf("3 - Del data.\n");
  printf("4 - Print Hash.\n");
  printf("5 - Print Hash size.\n");
  scanf("%d", &ch);
  switch(ch) {
  case 1:
   tst_hash_put(h_hdl);
   break;
  case 2:
   tst_hash_get(h_hdl);
   break;
  case 3:
   tst_hash_delete(h_hdl);
   break;
  case 4:
   tst_hash_print(h_hdl);
   break;
  case 5:
   tst_hash_printsize(h_hdl);
   break;
  default:
   hash_destroy(h_hdl);
   h_hdl = ((void *)0);
   return 0;
   break;
  }
 } while (ch > 0 && ch < 6);
 return 1;
}

variable_types: ch int

variable_types: capacity int


decompiled_code:
__int64 __fastcall tst_hash(_DWORD *a1, char **a2)
{
int v2; // eax
char *v4; // rsi
__int64 v5; // rdi
int v6; // eax
char *v7; // rcx
char *v8; // rdi
int v9; // edx
int v10; // eax

v2 = *((_DWORD *)a2 + 2);
if ( v2 <= 0 )
return (unsigned int)(17 % *a1);
v4 = *a2;
v5 = (unsigned int)(v2 - 1);
v6 = 17;
v7 = v4 + 1;
v8 = &v4[v5 + 1];
while ( 1 )
{
v9 = 31 * v6;
v10 = *v4;
v4 = v7;
v6 = v9 + v10;
if ( v7 == v8 )
break;
++v7;
}
return (unsigned int)(v6 % *a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, char **a2)
{
int v2; // eax
char *v4; // rsi
__int64 v5; // rdi
int v6; // eax
char *v7; // rcx
char *v8; // rdi
int v9; // edx
int v10; // eax

v2 = *((_DWORD *)a2 + 2);
if ( v2 <= 0 )
return (unsigned int)(17 % *a1);
v4 = *a2;
v5 = (unsigned int)(v2 - 1);
v6 = 17;
v7 = v4 + 1;
v8 = &v4[v5 + 1];
while ( 1 )
{
v9 = 31 * v6;
v10 = *v4;
v4 = v7;
v6 = v9 + v10;
if ( v7 == v8 )
break;
++v7;
}
return (unsigned int)(v6 % *a1);
}

original_code:
static int
tst_hash(hash_hdl_t *hdl, void *key) {
 tst_key_t *k1;
 int i = 0;
 int hash = 17;
 k1 = (tst_key_t *)key;
 for(i = 0; i < k1->tst_keylen; i++) {
  hash = hash*31 + (int)k1->tst_key[i];
 }
 return (hash) % (hdl->M);
}

canonical_original_code:
static int tst_hash(struct hash_hdl *hdl, void *key) {
 struct tst_key *k1;
 int i = 0;
 int hash = 17;
 k1 = (struct tst_key *)key;
 for(i = 0; i < k1->tst_keylen; i++) {
  hash = hash*31 + (int)k1->tst_key[i];
 }
 return (hash) % (hdl->M);
}


decompiled_code:
int __fastcall tst_hash_compare(__int64 a1, __int64 a2)
{
size_t v2; // rdx
int result; // eax

v2 = *(int *)(a1 + 8);
if ( (_DWORD)v2 == *(_DWORD *)(a2 + 8) )
result = strncmp(*(const char **)a1, *(const char **)a2, v2);
else
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, __int64 a2)
{
size_t v2; // rdx
int result; // eax

v2 = *(int *)(a1 + 8);
if ( (_DWORD)v2 == *(_DWORD *)(a2 + 8) )
result = strncmp(*(const char **)a1, *(const char **)a2, v2);
else
result = -1;
return result;
}

original_code:
static int
tst_hash_compare(void *key1, void *key2) {
 tst_key_t *k1, *k2;
 k1 = (tst_key_t *)key1;
 k2 = (tst_key_t *)key2;
 if(k1->tst_keylen != k2->tst_keylen) {
  return -1;
 }
 return strncmp(k1->tst_key, k2->tst_key, k1->tst_keylen);
}

canonical_original_code:
static int tst_hash_compare(void *key1, void *key2) {
 struct tst_key *k1; struct tst_key *k2;
 k1 = (struct tst_key *)key1;
 k2 = (struct tst_key *)key2;
 if(k1->tst_keylen != k2->tst_keylen) {
  return -1;
 }
 return strncmp(k1->tst_key, k2->tst_key, k1->tst_keylen);
}