original_code:
void generate_final() {
 char s_tmp[255];
 while(gqp != ((void *)0)){
  struct quad* q = gqp;
  sprintf(s_tmp, "%s:\n", label(q->label));
  add_machine_code( s_tmp );
  if (!strcmp(q->op,"begin_block")) {
   SYMBOL *tmp = lookup(q->x);
   ((void) sizeof ((tmp != ((void *)0)) ? 1 : 0), __extension__ ({ if (tmp != ((void *)0)) ; else __assert_fail ("tmp != NULL", "cg.c", 187, __extension__ __PRETTY_FUNCTION__); }));
   if (tmp->func.type == PROG) {
   }
   ncur = tmp->level;
  } else if (!strcmp(q->op,"end_block")) {
   SYMBOL *tmp = lookup(q->x);
   ((void) sizeof ((tmp != ((void *)0)) ? 1 : 0), __extension__ ({ if (tmp != ((void *)0)) ; else __assert_fail ("tmp != NULL", "cg.c", 196, __extension__ __PRETTY_FUNCTION__); }));
   if (tmp->func.type != PROG) {
    sprintf(s_tmp, "\tjmp M[R[0]]\n");
             add_machine_code( s_tmp );
   }
  } else if ( strcmp( q->op, "jump") == 0){
            sprintf(s_tmp, "\tjmp %s\n", label(q->z) );
            add_machine_code( s_tmp );
        } else if ( strcmp( q->op, "print") == 0){
   load("R[1]", q->x);
   sprintf(s_tmp, "\touti R[1]\n");
   add_machine_code( s_tmp );
        } else if ( strcmp( q->op, ":=") == 0){
            load("R[1]", q->x);
            store("R[1]", q->z);
        } else if ( strcmp( q->op, "+") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\taddi R[3], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[3]", q->z);
        } else if ( strcmp( q->op, "-") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tsubi R[3], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[3]", q->z);
        } else if ( strcmp( q->op, "*") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tmuli R[1], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[1]", q->z);
        } else if ( strcmp( q->op, "/") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tdivi R[3], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[3]", q->z);
        } else if ( strcmp( q->op, "=") == 0 || strcmp( q->op, "<") == 0 || strcmp( q->op, ">") == 0 || strcmp( q->op, "<=") == 0 || strcmp( q->op, ">=") == 0 || strcmp( q->op, "<>") == 0 ){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tcmpi R[1],R[2]\n" );
            add_machine_code( s_tmp );
            if ( strcmp( q->op, "=") == 0 ){
                sprintf(s_tmp, "\tje %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, "<") == 0 ){
                sprintf(s_tmp, "\tja %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, ">") == 0 ){
                sprintf(s_tmp, "\tjb %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, "<=") == 0 ){
                sprintf(s_tmp, "\tjae %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, ">=") == 0 ){
                sprintf(s_tmp, "\tjbe %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, "<>") == 0 ){
                sprintf(s_tmp, "\tjne %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
        } else if ( strcmp( q->op, "call") == 0){
   SYMBOL *p = lookup( q->z );
   arguments -= p->func.num_arg;
   if ( p->level == ncur ) {
    sprintf(s_tmp, "\tmovi R[255], M[R[0]+4]\n");
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", cur_scope->framelength+4);
             add_machine_code( s_tmp );
   } else if ( p->level > ncur ) {
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[0]\n", cur_scope->framelength+4);
             add_machine_code( s_tmp );
   }
            sprintf(s_tmp, "\tmovi R[255], %d\n", cur_scope->framelength );
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\taddi R[0], R[255], R[0]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi R[255], $\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi R[254], 15\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\taddi R[255], R[255], R[254]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi M[R[0]], R[255]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tjmp L%d\n", p->func.genquad);
   add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi R[255], %d\n", cur_scope->framelength );
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tsubi R[0], R[0], R[255]\n");
            add_machine_code( s_tmp );
        } else if ( strcmp( q->op, "par") == 0){
            SYMBOL *p = lookup( q->y );
            if ( !strcmp(q->x, "CV") ){
    int d = cur_scope->framelength + 12 + 4*arguments;
    arguments++;
                load("R[255]", q->y);
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", d);
                add_machine_code( s_tmp );
            } else if ( !strcmp(q->x, "RET") ){
    sprintf(s_tmp, "\tmovi R[255], R[0]\n");
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\tmovi R[254], %d\n", p->offset);
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\taddi R[255], R[254], R[255]\n");
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", cur_scope->framelength+8);
             add_machine_code( s_tmp );
            } else {
    int d = cur_scope->framelength + 12 + 4*arguments;
    arguments++;
    if ( ncur == p->level ) {
     if (p->type == TYPE_ARG && p->arg.type == INOUT ) {
      sprintf(s_tmp, "\tmovi R[255], R[0]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi R[254], %d\n", p->offset);
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\taddi R[255], R[254], R[255]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi R[1], M[R[255]]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[1]\n", d);
               add_machine_code( s_tmp );
     } else {
      sprintf(s_tmp, "\tmovi R[255], R[0]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi R[254], %d\n", p->offset);
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\taddi R[255], R[254], R[255]\n");
      add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", d);
               add_machine_code( s_tmp );
     }
    } else {
     gnlvcode(p->name);
     if (p->type == TYPE_ARG && p->arg.type == INOUT) {
      sprintf(s_tmp, "\tmovi R[1], M[R[255]]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[1]\n", d);
               add_machine_code( s_tmp );
     } else {
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", d);
               add_machine_code( s_tmp );
     }
    }
   }
        }else if ( strcmp( q->op, "halt") == 0){
   sprintf(s_tmp, "\thalt\n");
      add_machine_code( s_tmp );
      return;
  }else if ( strcmp( q->op, "retv") == 0){
            if( strcmp(q->x,"_") != 0 ){
                load("R[1]", q->x);
            }else{
                load("R[1]", "0");
            }
            sprintf(s_tmp, "\tmovi R[255], M[R[0]+8]\n");
            add_machine_code( s_tmp );
            sprintf(s_tmp, "\tmovi M[R[255]], R[1]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tjmp M[R[0]]\n");
         add_machine_code( s_tmp );
        }
  gqp = gqp->prev;
 }
}

canonical_original_code:
void generate_final() {
 char s_tmp[255];
 while(gqp != ((void *)0)){
  struct quad *q = gqp;
  sprintf(s_tmp, "%s:\n", label(q->label));
  add_machine_code( s_tmp );
  if (!strcmp(q->op,"begin_block")) {
   struct symbol *tmp = lookup(q->x);
   ((void) sizeof ((tmp != ((void *)0)) ? 1 : 0), __extension__ ({ if (tmp != ((void *)0)) ; else __assert_fail ("tmp != NULL", "cg.c", 187, __extension__ __PRETTY_FUNCTION__); }));
   if (tmp->func.type == PROG) {
   }
   ncur = tmp->level;
  } else if (!strcmp(q->op,"end_block")) {
   struct symbol *tmp = lookup(q->x);
   ((void) sizeof ((tmp != ((void *)0)) ? 1 : 0), __extension__ ({ if (tmp != ((void *)0)) ; else __assert_fail ("tmp != NULL", "cg.c", 196, __extension__ __PRETTY_FUNCTION__); }));
   if (tmp->func.type != PROG) {
    sprintf(s_tmp, "\tjmp M[R[0]]\n");
             add_machine_code( s_tmp );
   }
  } else if ( strcmp( q->op, "jump") == 0){
            sprintf(s_tmp, "\tjmp %s\n", label(q->z) );
            add_machine_code( s_tmp );
        } else if ( strcmp( q->op, "print") == 0){
   load("R[1]", q->x);
   sprintf(s_tmp, "\touti R[1]\n");
   add_machine_code( s_tmp );
        } else if ( strcmp( q->op, ":=") == 0){
            load("R[1]", q->x);
            store("R[1]", q->z);
        } else if ( strcmp( q->op, "+") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\taddi R[3], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[3]", q->z);
        } else if ( strcmp( q->op, "-") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tsubi R[3], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[3]", q->z);
        } else if ( strcmp( q->op, "*") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tmuli R[1], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[1]", q->z);
        } else if ( strcmp( q->op, "/") == 0){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tdivi R[3], R[1], R[2]\n" );
            add_machine_code( s_tmp );
            store("R[3]", q->z);
        } else if ( strcmp( q->op, "=") == 0 || strcmp( q->op, "<") == 0 || strcmp( q->op, ">") == 0 || strcmp( q->op, "<=") == 0 || strcmp( q->op, ">=") == 0 || strcmp( q->op, "<>") == 0 ){
            load("R[1]", q->x);
            load("R[2]", q->y);
            sprintf(s_tmp, "\tcmpi R[1],R[2]\n" );
            add_machine_code( s_tmp );
            if ( strcmp( q->op, "=") == 0 ){
                sprintf(s_tmp, "\tje %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, "<") == 0 ){
                sprintf(s_tmp, "\tja %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, ">") == 0 ){
                sprintf(s_tmp, "\tjb %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, "<=") == 0 ){
                sprintf(s_tmp, "\tjae %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, ">=") == 0 ){
                sprintf(s_tmp, "\tjbe %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
            if ( strcmp( q->op, "<>") == 0 ){
                sprintf(s_tmp, "\tjne %s\n", label(q->z) );
                add_machine_code( s_tmp );
            }
        } else if ( strcmp( q->op, "call") == 0){
   struct symbol *p = lookup( q->z );
   arguments -= p->func.num_arg;
   if ( p->level == ncur ) {
    sprintf(s_tmp, "\tmovi R[255], M[R[0]+4]\n");
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", cur_scope->framelength+4);
             add_machine_code( s_tmp );
   } else if ( p->level > ncur ) {
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[0]\n", cur_scope->framelength+4);
             add_machine_code( s_tmp );
   }
            sprintf(s_tmp, "\tmovi R[255], %d\n", cur_scope->framelength );
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\taddi R[0], R[255], R[0]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi R[255], $\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi R[254], 15\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\taddi R[255], R[255], R[254]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi M[R[0]], R[255]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tjmp L%d\n", p->func.genquad);
   add_machine_code( s_tmp );
   sprintf(s_tmp, "\tmovi R[255], %d\n", cur_scope->framelength );
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tsubi R[0], R[0], R[255]\n");
            add_machine_code( s_tmp );
        } else if ( strcmp( q->op, "par") == 0){
            struct symbol *p = lookup( q->y );
            if ( !strcmp(q->x, "CV") ){
    int d = cur_scope->framelength + 12 + 4*arguments;
    arguments++;
                load("R[255]", q->y);
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", d);
                add_machine_code( s_tmp );
            } else if ( !strcmp(q->x, "RET") ){
    sprintf(s_tmp, "\tmovi R[255], R[0]\n");
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\tmovi R[254], %d\n", p->offset);
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\taddi R[255], R[254], R[255]\n");
             add_machine_code( s_tmp );
    sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", cur_scope->framelength+8);
             add_machine_code( s_tmp );
            } else {
    int d = cur_scope->framelength + 12 + 4*arguments;
    arguments++;
    if ( ncur == p->level ) {
     if (p->type == TYPE_ARG && p->arg.type == INOUT ) {
      sprintf(s_tmp, "\tmovi R[255], R[0]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi R[254], %d\n", p->offset);
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\taddi R[255], R[254], R[255]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi R[1], M[R[255]]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[1]\n", d);
               add_machine_code( s_tmp );
     } else {
      sprintf(s_tmp, "\tmovi R[255], R[0]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi R[254], %d\n", p->offset);
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\taddi R[255], R[254], R[255]\n");
      add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", d);
               add_machine_code( s_tmp );
     }
    } else {
     gnlvcode(p->name);
     if (p->type == TYPE_ARG && p->arg.type == INOUT) {
      sprintf(s_tmp, "\tmovi R[1], M[R[255]]\n");
               add_machine_code( s_tmp );
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[1]\n", d);
               add_machine_code( s_tmp );
     } else {
      sprintf(s_tmp, "\tmovi M[R[0]+%d], R[255]\n", d);
               add_machine_code( s_tmp );
     }
    }
   }
        }else if ( strcmp( q->op, "halt") == 0){
   sprintf(s_tmp, "\thalt\n");
      add_machine_code( s_tmp );
      return;
  }else if ( strcmp( q->op, "retv") == 0){
            if( strcmp(q->x,"_") != 0 ){
                load("R[1]", q->x);
            }else{
                load("R[1]", "0");
            }
            sprintf(s_tmp, "\tmovi R[255], M[R[0]+8]\n");
            add_machine_code( s_tmp );
            sprintf(s_tmp, "\tmovi M[R[255]], R[1]\n");
            add_machine_code( s_tmp );
   sprintf(s_tmp, "\tjmp M[R[0]]\n");
         add_machine_code( s_tmp );
        }
  gqp = gqp->prev;
 }
}

user_defined_types: struct quad { char op[35]; char x[35]; char label[35]; struct quad *next; struct quad *prev; }


repo: ptigas/ciscal

decompiled_code:
char *__fastcall int2string(unsigned int a1)
{
double v1; // xmm0_8
__m128d v2; // xmm3
__m128d v3; // xmm1
char *v4; // r12

v1 = log10((double)2);
if ( COERCE_DOUBLE(*(_QWORD *)&v1 & LC1) < 4.503599627370496e15 )
{
v2.m128d_f64[0] = v1;
v3.m128d_f64[0] = (double)(int)v1;
*(_QWORD *)&v1 = COERCE_UNSIGNED_INT64(v3.m128d_f64[0] + COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v2, v3) & LC2)) | ~LC1 & *(_QWORD *)&v1;
}
v4 = (char *)malloc((int)v1 + 1);
*(_OWORD *)v4 = 0LL;
*((_OWORD *)v4 + 1) = 0LL;
sprintf(v4, "%d", a1);
return v4;
}

original_code:
char * int2string( int i ){
 char *s;
 int l;
 l = l<2?2:l;
    s = (char*)malloc(((int)ceil(log10(l))+1)*sizeof(char));
 memset(s, 0, 32);
 if(s == ((void *)0)) {
  { fprintf(stderr, "error: %s\r\n", "not enough memory"); exit(1); };
 }
 sprintf(s, "%d", i);
 return s;
}

canonical_original_code:
char *int2string(int i){
 char *s;
 int l;
 l = l<2?2:l;
    s = (char *)malloc(((int)ceil(log10(l))+1)*sizeof(char));
 memset(s, 0, 32);
 if(s == ((void *)0)) {
  { fprintf(stderr, "error: %s\r\n", "not enough memory"); exit(1); };
 }
 sprintf(s, "%d", i);
 return s;
}

variable_types: i int

variable_types:
s
char *

variable_types: l int

return_type: char *


nextquad

decompiled_code:
__int64 nextquad()
{
return (unsigned int)qlabel;
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned int)qlabel;
}

original_code:
int nextquad(){
 return qlabel;
}

canonical_original_code:
int nextquad(){
 return qlabel;
}

return_type: int


newquad

decompiled_code:
char *__fastcall newquad(char *src, char *a2, char *a3, char *a4)
{
char *v6; // rax
__int64 v7; // rdx
char *v8; // r12

v6 = (char *)malloc(0xC0uLL);
v7 = (unsigned int)qlabel;
v8 = v6;
++qlabel;
sprintf(v6 + 140, "%d", v7);
strcpy(v8, src);
strcpy(v8 + 35, a2);
strcpy(v8 + 70, a3);
strcpy(v8 + 105, a4);
return v8;
}

canonical_decompiled_code:
char *__fastcall func2(char *src, char *a2, char *a3, char *a4)
{
char *v6; // rax
__int64 v7; // rdx
char *v8; // r12

v6 = (char *)malloc(0xC0uLL);
v7 = (unsigned int)qlabel;
v8 = v6;
++qlabel;
sprintf(v6 + 140, "%d", v7);
strcpy(v8, src);
strcpy(v8 + 35, a2);
strcpy(v8 + 70, a3);
strcpy(v8 + 105, a4);
return v8;
}

original_code:
struct quad* newquad(char *op, char *x, char *y, char *z){
 struct quad* q;
 q = (struct quad*)malloc(sizeof(struct quad));
 sprintf(q->label,"%d",qlabel++);
 sprintf(q->op,"%s",op);
 sprintf(q->x,"%s",x);
 sprintf(q->y,"%s",y);
 sprintf(q->z,"%s",z);
 return q;
}

canonical_original_code:
struct quad *newquad(char *op, char *x, char *y, char *z){
 struct quad *q;
 q = (struct quad *)malloc(sizeof(struct quad));
 sprintf(q->label,"%d",qlabel++);
 sprintf(q->op,"%s",op);
 sprintf(q->x,"%s",x);
 sprintf(q->y,"%s",y);
 sprintf(q->z,"%s",z);
 return q;
}

variable_types:
op
char *

variable_types:
x
char *

variable_types:
y
char *

variable_types:
z
char *

variable_types: struct quad { char op[35]; char x[35]; char label[35]; struct quad *next; struct quad *prev; } *


genquad

decompiled_code:
__int64 __fastcall genquad(char *a1, char *a2, char *a3, char *a4)
{
char *v4; // rax
__int64 v5; // r8

v4 = newquad(a1, a2, a3, a4);
addtoquads((__int64)v4, &quads, &tail);
if ( !gqp )
gqp = quads;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, char *a2, char *a3, char *a4)
{
char *v4; // rax
__int64 v5; // r8

v4 = func2(a1, a2, a3, a4);
func3((__int64)v4, &quads, &tail);
if ( !gqp )
gqp = quads;
return v5;
}

original_code:
struct quad* genquad(char *op, char *x, char *y, char *z){
 struct quad* q;
 q = newquad(op, x, y, z);
 addtoquads(q, &quads, &tail);
 if ( gqp == ((void *)0) ) {
  gqp = quads;
 }
 if( 0 ){
  printf("%s\t: %s %s %s %s\n", q->label, q->op, q->x, q->y, q->z);
 }
 return q;
}

canonical_original_code:
struct quad *genquad(char *op, char *x, char *y, char *z){
 struct quad *q;
 q = newquad(op, x, y, z);
 addtoquads(q, &quads, &tail);
 if ( gqp == ((void *)0) ) {
  gqp = quads;
 }
 if( 0 ){
  printf("%s\t: %s %s %s %s\n", q->label, q->op, q->x, q->y, q->z);
 }
 return q;
}

return_type: struct quad { char op[35]; char x[35]; char label[35]; struct quad *next; struct quad *prev; } *


fixquadlabels

decompiled_code:
void __fastcall fixquadlabels(unsigned int a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned int v3; // er15
char v4; // r12
char *v5; // rax
char *v6; // rax
char *v7; // rax
int v8; // eax
char *v9; // rax

v2 = tail;
if ( tail )
{
v3 = a1;
v4 = 0;
do
{
while ( 1 )
{
v6 = int2string(a1);
if ( !strcmp((const char *)(v2 + 140), v6) )
break;
v5 = int2string(a2);
if ( strcmp((const char *)(v2 + 140), v5) && (v4 & 1) != 0 )
goto LABEL_9;
v4 = 0;
LABEL_6:
v2 = *(_QWORD *)(v2 + 184);
if ( !v2 )
return;
}
v7 = int2string(a2);
v8 = strcmp((const char *)(v2 + 140), v7);
v4 = v8;
if ( !v8 )
goto LABEL_6;
LABEL_9:
v9 = int2string(v3++);
v4 = 1;
strcpy((char *)(v2 + 140), v9);
v2 = *(_QWORD *)(v2 + 184);
}
while ( v2 );
}
}

canonical_decompiled_code:
void __fastcall func6(unsigned int a1, unsigned int a2)
{
__int64 v2; // rbx
unsigned int v3; // er15
char v4; // r12
char *v5; // rax
char *v6; // rax
char *v7; // rax
int v8; // eax
char *v9; // rax

v2 = tail;
if ( tail )
{
v3 = a1;
v4 = 0;
do
{
while ( 1 )
{
v6 = func0(a1);
if ( !strcmp((const char *)(v2 + 140), v6) )
break;
v5 = func0(a2);
if ( strcmp((const char *)(v2 + 140), v5) && (v4 & 1) != 0 )
goto LABEL_9;
v4 = 0;
LABEL_6:
v2 = *(_QWORD *)(v2 + 184);
if ( !v2 )
return;
}
v7 = func0(a2);
v8 = strcmp((const char *)(v2 + 140), v7);
v4 = v8;
if ( !v8 )
goto LABEL_6;
LABEL_9:
v9 = func0(v3++);
v4 = 1;
strcpy((char *)(v2 + 140), v9);
v2 = *(_QWORD *)(v2 + 184);
}
while ( v2 );
}
}

original_code:
void fixquadlabels(int from, int to){
 struct quad* q = tail;
 int i = from;
 int fix = 0;
 while(q != ((void *)0)){
  if (!strcmp(q->label, int2string(from))) {
   fix = 1;
  }
  if (!strcmp(q->label, int2string(to))) {
   fix = 0;
  }
  if (fix) {
   sprintf(q->label, "%s", int2string(i++));
  }
  q = q->prev;
 }
}

canonical_original_code:
void fixquadlabels(int from, int to){
 struct quad *q = tail;
 int i = from;
 int fix = 0;
 while(q != ((void *)0)){
  if (!strcmp(q->label, int2string(from))) {
   fix = 1;
  }
  if (!strcmp(q->label, int2string(to))) {
   fix = 0;
  }
  if (fix) {
   sprintf(q->label, "%s", int2string(i++));
  }
  q = q->prev;
 }
}

variable_types: from int

variable_types: to int

variable_types: fix int

return_type: void


newtemp

decompiled_code:
char *__fastcall newtemp(const char *a1)
{
__int64 v1; // rcx
char *v2; // rax
char v4[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = (unsigned int)tempcounter++;
sprintf(v4, "%s_%d", a1, v1);
v2 = strdup(v4);
new_variable(v2, 1LL);
return strdup(v4);
}

canonical_decompiled_code:
char *__fastcall func7(const char *a1)
{
__int64 v1; // rcx
char *v2; // rax
char v4[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = (unsigned int)tempcounter++;
sprintf(v4, "%s_%d", a1, v1);
v2 = strdup(v4);
new_variable(v2, 1LL);
return strdup(v4);
}

original_code:
char * newtemp(char *t){
 char s[30], *w;
 sprintf(s, "%s_%d", t, tempcounter++);
    w = strdup(s);
 new_variable(w, 1);
 return strdup(s);
}

canonical_original_code:
char *newtemp(char *t){
 char s[30]; char *w;
 sprintf(s, "%s_%d", t, tempcounter++);
    w = strdup(s);
 new_variable(w, 1);
 return strdup(s);
}

variable_types:
t
char *

variable_types:
s
char [30]

variable_types:
w
char *


makelist

decompiled_code:
char *__fastcall makelist(char *src)
{
char *v1; // rax
char *result; // rax

v1 = (char *)malloc(0x38uLL);
result = strcpy(v1, src);
*((_QWORD *)result + 5) = 0LL;
*((_DWORD *)result + 12) = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func8(char *src)
{
char *v1; // rax
char *result; // rax

v1 = (char *)malloc(0x38uLL);
result = strcpy(v1, src);
*((_QWORD *)result + 5) = 0LL;
*((_DWORD *)result + 12) = 0;
return result;
}

original_code:
struct list * makelist(char *x){
 struct list* tmp = (struct list*)malloc(sizeof(struct list));
 strcpy(tmp->name, x);
 tmp->next = ((void *)0);
 tmp->exitflag = 0;
 return tmp;
}

canonical_original_code:
struct list *makelist(char *x){
 struct list *tmp = (struct list*)malloc(sizeof(struct list));
 strcpy(tmp->name, x);
 tmp->next = ((void *)0);
 tmp->exitflag = 0;
 return tmp;
}

variable_types: struct list { char name[35]; struct list *next; int exitflag; } *


printlist

decompiled_code:
size_t __fastcall printlist(__int64 a1)
{
__int64 v1; // rbx

v1 = a1;
fwrite("[ ", 1uLL, 2uLL, stderr);
if ( a1 )
{
do
{
fprintf(stderr, "%s ", (const char *)v1);
v1 = *(_QWORD *)(v1 + 40);
}
while ( v1 );
}
return fwrite("]\n", 1uLL, 2uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func9(__int64 a1)
{
__int64 v1; // rbx

v1 = a1;
fwrite("[ ", 1uLL, 2uLL, stderr);
if ( a1 )
{
do
{
fprintf(stderr, "%s ", (const char *)v1);
v1 = *(_QWORD *)(v1 + 40);
}
while ( v1 );
}
return fwrite("]\n", 1uLL, 2uLL, stderr);
}

original_code:
void printlist(struct list* l){
 fprintf(stderr, "[ ");
 while(l != ((void *)0)){
  fprintf(stderr, "%s ",l->name);
  l = l->next;
 }
 fprintf(stderr, "]\n");
}

canonical_original_code:
void printlist(struct list *l){
 fprintf(stderr, "[ ");
 while(l != ((void *)0)){
  fprintf(stderr, "%s ",l->name);
  l = l->next;
 }
 fprintf(stderr, "]\n");
}

user_defined_types: struct list { char name[35]; struct list *next; int exitflag; }


exitlist

decompiled_code:
__int64 __fastcall exitlist(__int64 a1)
{
while ( 1 )
{
if ( !a1 )
return 0LL;
if ( *(_DWORD *)(a1 + 48) == 1 )
break;
a1 = *(_QWORD *)(a1 + 40);
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
while ( 1 )
{
if ( !a1 )
return 0LL;
if ( *(_DWORD *)(a1 + 48) == 1 )
break;
a1 = *(_QWORD *)(a1 + 40);
}
return a1;
}

original_code:
struct list * exitlist(struct list *l) {
 while(l != ((void *)0)){
  if(l->exitflag == 1) {
   return l;
  }
  l = l->next;
 }
 return ((void *)0);
}

canonical_original_code:
struct list *exitlist(struct list *l) {
 while(l != ((void *)0)){
  if(l->exitflag == 1) {
   return l;
  }
  l = l->next;
 }
 return ((void *)0);
}

variable_types:
l
struct list *

return_type: struct list { char name[35]; struct list *next; int exitflag; } *


merge

decompiled_code:
__int64 __fastcall merge(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a1 )
return a2;
result = a1;
if ( a2 )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 40);
}
while ( result );
*(_QWORD *)(v3 + 40) = a2;
result = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

if ( !a1 )
return a2;
result = a1;
if ( a2 )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 40);
}
while ( result );
*(_QWORD *)(v3 + 40) = a2;
result = a1;
}
return result;
}

original_code:
struct list * merge(struct list *a, struct list *b){
 struct list *t, *p;
 if(a == ((void *)0)){
  return b;
 }
 if(b == ((void *)0)){
  return a;
 }
 t = a;
    p = t;
 while(t->next != ((void *)0)){
  t = t->next;
 }
 t->next = b;
 return p;
}

canonical_original_code:
struct list *merge(struct list *a, struct list *b){
 struct list *t; struct list *p;
 if(a == ((void *)0)){
  return b;
 }
 if(b == ((void *)0)){
  return a;
 }
 t = a;
    p = t;
 while(t->next != ((void *)0)){
  t = t->next;
 }
 t->next = b;
 return p;
}

variable_types:
a
struct list *

variable_types:
b
struct list *


backpatch

original_code:
void backpatch(struct list* l, char* s){
 struct list* t;
 struct quad* q;
 t = l;
 while(t != ((void *)0)){
  q = quads;
  while(q != ((void *)0)){
   if(t->exitflag == 0 && !strcmp(q->label, t->name) && !strcmp(q->z,"_")){
    strcpy(q->z, s);
   }
   q = q->next;
  }
  t = t->next;
 }
}

canonical_original_code:
void backpatch(struct list *l, char *s){
 struct list *t;
 struct quad *q;
 t = l;
 while(t != ((void *)0)){
  q = quads;
  while(q != ((void *)0)){
   if(t->exitflag == 0 && !strcmp(q->label, t->name) && !strcmp(q->z,"_")){
    strcpy(q->z, s);
   }
   q = q->next;
  }
  t = t->next;
 }
}

variable_types:
s
char *


repo: ptitdoc/Archive-core-agent-linux

decompiled_code:
int fix_display()
{
return setenv("DISPLAY", ":0", 1);
}

canonical_decompiled_code:
int func19()
{
return setenv("DISPLAY", ":0", 1);
}

original_code:
static void fix_display()
{
 setenv("DISPLAY", ":0", 1);
}


decompiled_code:
__pid_t __fastcall produce_message(const char *a1, const char *a2, void *a3)
{
int *v3; // rax
char *v4; // rax
__pid_t result; // eax
char *ptr; // [rsp+8h] [rbp-420h] BYREF
char s[1048]; // [rsp+10h] [rbp-418h] BYREF

vsnprintf(s, 0x400uLL, a2, a3);
v3 = __errno_location();
v4 = strerror(*v3);
asprintf(&ptr, "%s: %s: %s (error type: %s)", program_invocation_short_name, a1, s, v4);
fputs(ptr, stderr);
result = fork();
if ( result == -1 )
LABEL_4:
exit(1);
if ( !result )
{
fix_display();
execlp("zenity", "zenity", "--error", "--text", ptr, 0LL);
goto LABEL_4;
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func20(const char *a1, const char *a2, void *a3)
{
int *v3; // rax
char *v4; // rax
__pid_t result; // eax
char *ptr; // [rsp+8h] [rbp-420h] BYREF
char s[1048]; // [rsp+10h] [rbp-418h] BYREF

vsnprintf(s, 0x400uLL, a2, a3);
v3 = __errno_location();
v4 = strerror(*v3);
asprintf(&ptr, "%s: %s: %s (error type: %s)", program_invocation_short_name, a1, s, v4);
fputs(ptr, stderr);
result = fork();
if ( result == -1 )
LABEL_4:
exit(1);
if ( !result )
{
func19();
execlp("zenity", "zenity", "--error", "--text", ptr, 0LL);
goto LABEL_4;
}
return result;
}

original_code:
static void produce_message(char * type, const char *fmt, va_list args)
{
 char *dialog_msg;
 char buf[1024];
 (void) vsnprintf(buf, sizeof(buf), fmt, args);
 asprintf(&dialog_msg, "%s: %s: %s (error type: %s)",
   program_invocation_short_name, type, buf, strerror((*__errno_location ())));
 fprintf(stderr, "%s", dialog_msg);
 switch (fork()) {
 case -1:
  exit(1);
 case 0:
  fix_display();
  execlp("zenity", "zenity", "--error", "--text", dialog_msg, ((void *)0));
  exit(1);
 default:;
 }
}

canonical_original_code:
static void produce_message(char *type, char *fmt, __builtin_va_list args)
{
 char *dialog_msg;
 char buf[1024];
 (void) vsnprintf(buf, sizeof(buf), fmt, args);
 asprintf(&dialog_msg, "%s: %s: %s (error type: %s)",
   program_invocation_short_name, type, buf, strerror((*__errno_location ())));
 fprintf(stderr, "%s", dialog_msg);
 switch (fork()) {
 case -1:
  exit(1);
 case 0:
  fix_display();
  execlp("zenity", "zenity", "--error", "--text", dialog_msg, ((void *)0));
  exit(1);
 default:;
 }
}

variable_types:
type
char *

variable_types:
fmt
char *

variable_types:
buf
char [1024]


decompiled_code:
void __noreturn gui_fatal(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
produce_message("Fatal error", a1, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func21(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func20("Fatal error", a1, va);
exit(1);
}

original_code:
void gui_fatal(const char *fmt, ...)
{
 va_list args;
 __builtin_va_start(args,fmt);
 produce_message("Fatal error", fmt, args);
 __builtin_va_end(args);
 exit(1);
}

canonical_original_code:
void gui_fatal(char *fmt, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 produce_message("Fatal error", fmt, args);
 __builtin_va_end(args);
 exit(1);
}


decompiled_code:
__pid_t gui_nonfatal(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return produce_message("Information", a1, va);
}

canonical_decompiled_code:
__pid_t func22(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return func20("Information", a1, va);
}

original_code:
void gui_nonfatal(const char *fmt, ...)
{
 va_list args;
 __builtin_va_start(args,fmt);
 produce_message("Information", fmt, args);
 __builtin_va_end(args);
}

canonical_original_code:
void gui_nonfatal(char *fmt, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 produce_message("Information", fmt, args);
 __builtin_va_end(args);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
const char *v4; // rax
__pid_t v5; // eax
const char *v6; // rax
__pid_t v7; // eax
int v9; // ebp
FILE *v10; // r13
int v11; // eax
int v12; // ebp
FILE *v13; // r12
unsigned int v14; // eax
int v15; // edi
int stat_loc; // [rsp+Ch] [rbp-154Ch] BYREF
struct stat v17; // [rsp+10h] [rbp-1548h] BYREF
struct stat v18; // [rsp+A0h] [rbp-14B8h] BYREF
char name[1024]; // [rsp+130h] [rbp-1428h] BYREF
struct stat value[28]; // [rsp+530h] [rbp-1028h] BYREF

v3 = (char *)get_filename(argc, argv, envp);
copy_file(v3);
if ( (unsigned int)stat(v3, &v17) )
{
perror("stat pre");
exit(1);
}
v4 = (const char *)gettime();
fprintf(stderr, "time=%s, waiting for qubes-session\n", v4);
if ( (unsigned int)stat("/tmp/qubes-session-env", &v18) )
{
v5 = fork();
if ( v5 == -1 )
goto LABEL_27;
if ( !v5 )
{
v13 = fopen("/tmp/qubes-session-waiter", "a");
if ( v13 )
{
v14 = getpid();
fprintf(v13, "%d\n", v14);
fclose(v13);
if ( (unsigned int)stat("/tmp/qubes-session-env", &v18) )
pause();
exit(0);
}
perror("fopen waiter_pidfile");
exit(1);
}
waitpid(v5, &stat_loc, 0);
if ( (stat_loc & 0x7F) == 0 )
{
v15 = BYTE1(stat_loc);
if ( BYTE1(stat_loc) )
LABEL_26:
exit(v15);
}
}
v6 = (const char *)gettime();
fprintf(stderr, "time=%s, starting editor\n", v6);
v7 = fork();
if ( v7 == -1 )
{
LABEL_27:
perror("fork");
exit(1);
}
if ( !v7 )
{
v9 = open("/dev/null", 0);
dup2(v9, 0);
close(v9);
v10 = fopen("/tmp/qubes-session-env", "r");
while ( (unsigned int)__isoc99_fscanf() == 2 )
setenv(name, (const char *)value, 1);
fclose(v10);
v11 = open("/tmp/mimeopen.log", 1088, 438LL);
v12 = v11;
if ( v11 != -1 )
{
dup2(v11, 1);
close(v12);
setenv("HOME", "/home/user", 1);
setenv("DISPLAY", ":0", 1);
execl("/usr/bin/mimeopen", "mimeopen", "-n", v3, 0LL);
perror("execl");
exit(1);
}
perror("open /tmp/mimeopen.log");
v15 = 1;
goto LABEL_26;
}
waitpid(v7, &stat_loc, 0);
if ( stat_loc )
{
snprintf(
(char *)value,
0x200uLL,
"HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit s"
"tatus: %d)!' > /tmp/kdialog.log 2>&1 </dev/null",
(unsigned int)stat_loc);
system((const char *)value);
}
if ( (unsigned int)stat(v3, value) )
{
perror("stat post");
exit(1);
}
if ( v17.st_mtim.tv_sec != value[0].st_mtim.tv_sec )
send_file_back(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
const char *v4; // rax
__pid_t v5; // eax
const char *v6; // rax
__pid_t v7; // eax
int v9; // ebp
FILE *v10; // r13
int v11; // eax
int v12; // ebp
FILE *v13; // r12
unsigned int v14; // eax
int v15; // edi
int stat_loc; // [rsp+Ch] [rbp-154Ch] BYREF
struct stat v17; // [rsp+10h] [rbp-1548h] BYREF
struct stat v18; // [rsp+A0h] [rbp-14B8h] BYREF
char name[1024]; // [rsp+130h] [rbp-1428h] BYREF
struct stat value[28]; // [rsp+530h] [rbp-1028h] BYREF

v3 = (char *)func8(argc, argv, envp);
func9(v3);
if ( (unsigned int)func19(v3, &v17) )
{
perror("stat pre");
exit(1);
}
v4 = (const char *)func7();
fprintf(stderr, "time=%s, waiting for qubes-session\n", v4);
if ( (unsigned int)func19("/tmp/qubes-session-env", &v18) )
{
v5 = fork();
if ( v5 == -1 )
goto LABEL_27;
if ( !v5 )
{
v13 = fopen("/tmp/qubes-session-waiter", "a");
if ( v13 )
{
v14 = getpid();
fprintf(v13, "%d\n", v14);
fclose(v13);
if ( (unsigned int)func19("/tmp/qubes-session-env", &v18) )
pause();
exit(0);
}
perror("fopen waiter_pidfile");
exit(1);
}
waitpid(v5, &stat_loc, 0);
if ( (stat_loc & 0x7F) == 0 )
{
v15 = BYTE1(stat_loc);
if ( BYTE1(stat_loc) )
LABEL_26:
exit(v15);
}
}
v6 = (const char *)func7();
fprintf(stderr, "time=%s, starting editor\n", v6);
v7 = fork();
if ( v7 == -1 )
{
LABEL_27:
perror("fork");
exit(1);
}
if ( !v7 )
{
v9 = open("/dev/null", 0);
dup2(v9, 0);
close(v9);
v10 = fopen("/tmp/qubes-session-env", "r");
while ( (unsigned int)__isoc99_fscanf() == 2 )
setenv(name, (const char *)value, 1);
fclose(v10);
v11 = open("/tmp/mimeopen.log", 1088, 438LL);
v12 = v11;
if ( v11 != -1 )
{
dup2(v11, 1);
close(v12);
setenv("HOME", "/home/user", 1);
setenv("DISPLAY", ":0", 1);
execl("/usr/bin/mimeopen", "mimeopen", "-n", v3, 0LL);
perror("execl");
exit(1);
}
perror("open /tmp/mimeopen.log");
v15 = 1;
goto LABEL_26;
}
waitpid(v7, &stat_loc, 0);
if ( stat_loc )
{
snprintf(
(char *)value,
0x200uLL,
"HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit s"
"tatus: %d)!' > /tmp/kdialog.log 2>&1 </dev/null",
(unsigned int)stat_loc);
system((const char *)value);
}
if ( (unsigned int)func19(v3, value) )
{
perror("stat post");
exit(1);
}
if ( v17.st_mtim.tv_sec != value[0].st_mtim.tv_sec )
func10(v3);
return 0;
}

original_code:
int main(int argc, char ** argv)
{
 signal(13, ((__sighandler_t) 1));
 if (argc!=2)
  gui_fatal("OpenInVM - no file given?");
 talk_to_daemon(argv[1]);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 signal(13, ((void (*)(int)) 1));
 if (argc!=2)
  gui_fatal("OpenInVM - no file given?");
 talk_to_daemon(argv[1]);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


gettime

decompiled_code:
char *gettime()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
snprintf(retbuf_1, 0x3CuLL, "%lld.%lld", v1.tv_sec, v1.tv_usec);
return retbuf_1;
}

canonical_decompiled_code:
char *func7()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
snprintf(retbuf_1, 0x3CuLL, "%lld.%lld", v1.tv_sec, v1.tv_usec);
return retbuf_1;
}

original_code:
char *gettime()
{
 static char retbuf[60];
 struct timeval tv;
 gettimeofday(&tv, ((void *)0));
 snprintf(retbuf, sizeof(retbuf), "%lld.%lld",
   (long long) tv.tv_sec, (long long) tv.tv_usec);
 return retbuf;
}

variable_types:
retbuf
char [60]

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


decompiled_code:
char *get_filename()
{
char *v0; // rbx
char v2[256]; // [rsp+0h] [rbp-128h] BYREF
char v3; // [rsp+100h] [rbp-28h] BYREF

if ( !(unsigned int)read_all(0) )
LABEL_10:
exit(1);
if ( index(v2, 47) )
{
fwrite("filename contains /", 1uLL, 0x13uLL, stderr);
goto LABEL_10;
}
v0 = v2;
do
{
if ( !*v0 )
break;
if ( index(" !?\"#$%^&*()[]<>;`~", *v0) )
*v0 = 95;
++v0;
}
while ( v0 != &v3 );
snprintf(retname_0, 0x106uLL, "/tmp/%s", v2);
return retname_0;
}

canonical_decompiled_code:
char *func8()
{
char *v0; // rbx
char v2[256]; // [rsp+0h] [rbp-128h] BYREF
char v3; // [rsp+100h] [rbp-28h] BYREF

if ( !(unsigned int)func15(0) )
LABEL_10:
exit(1);
if ( index(v2, 47) )
{
fwrite("filename contains /", 1uLL, 0x13uLL, stderr);
goto LABEL_10;
}
v0 = v2;
do
{
if ( !*v0 )
break;
if ( index(" !?\"#$%^&*()[]<>;`~", *v0) )
*v0 = 95;
++v0;
}
while ( v0 != &v3 );
snprintf(retname_0, 0x106uLL, "/tmp/%s", v2);
return retname_0;
}

original_code:
char *get_filename()
{
 char buf[256];
 static char retname[sizeof(buf) + sizeof("/tmp/")];
 int i;
 if (!read_all(0, buf, sizeof(buf)))
  exit(1);
 if (index(buf, '/')) {
  fprintf(stderr, "filename contains /");
  exit(1);
 }
 for (i=0; i < 256 && buf[i]!=0; i++) {
  if (index(" !?\"#$%^&*()[]<>;`~", buf[i]))
   buf[i]='_';
 }
 snprintf(retname, sizeof(retname), "/tmp/%s", buf);
 return retname;
}

canonical_original_code:
char *get_filename()
{
 char buf[256];
 static char retname[-1];
 int i;
 if (!read_all(0, buf, sizeof(buf)))
  exit(1);
 if (index(buf, '/')) {
  fprintf(stderr, "filename contains /");
  exit(1);
 }
 for (i=0; i < 256 && buf[i]!=0; i++) {
  if (index(" !?\"#$%^&*()[]<>;`~", buf[i]))
   buf[i]='_';
 }
 snprintf(retname, sizeof(retname), "/tmp/%s", buf);
 return retname;
}

variable_types:
buf
char [256]

variable_types:
retname
char [-1]

variable_types: i int


decompiled_code:
int __fastcall copy_file(const char *a1)
{
int v1; // eax
int v2; // ebp

v1 = open(a1, 65, 384LL);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)copy_fd_all(v1, 0) )
exit(1);
return close(v2);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
int v1; // eax
int v2; // ebp

v1 = open(a1, 65, 384LL);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)func16(v1, 0) )
exit(1);
return close(v2);
}

original_code:
void copy_file(char *filename)
{
 int fd = open(filename, 01 | 0100, 0600);
 if (fd < 0) {
  perror("open file");
  exit(1);
 }
 if (!copy_fd_all(fd, 0))
        exit(1);
 close(fd);
}

variable_types:
filename
char *

variable_types: fd int


decompiled_code:
int __fastcall send_file_back(const char *a1)
{
int v1; // eax
int v2; // ebp

v1 = open(a1, 0);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)copy_fd_all(1, v1) )
exit(1);
return close(v2);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
int v1; // eax
int v2; // ebp

v1 = open(a1, 0);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)func16(1, v1) )
exit(1);
return close(v2);
}

original_code:
void send_file_back(char * filename)
{
 int fd = open(filename, 00);
 if (fd < 0) {
  perror("open file");
  exit(1);
 }
 if (!copy_fd_all(1, fd))
  exit(1);
 close(fd);
}

canonical_original_code:
void send_file_back(char *filename)
{
 int fd = open(filename, 00);
 if (fd < 0) {
  perror("open file");
  exit(1);
 }
 if (!copy_fd_all(1, fd))
  exit(1);
 close(fd);
}


decompiled_code:
void __fastcall perror_wrapper(char *s)
{
int *v1; // rax
int v2; // er12
int *v3; // rbx

v1 = __errno_location();
v2 = *v1;
v3 = v1;
perror(s);
*v3 = v2;
}

canonical_decompiled_code:
void __fastcall func11(char *s)
{
int *v1; // rax
int v2; // er12
int *v3; // rbx

v1 = __errno_location();
v2 = *v1;
v3 = v1;
perror(s);
*v3 = v2;
}

original_code:
void perror_wrapper(char * msg)
{
 int prev=(*__errno_location ());
 perror(msg);
 (*__errno_location ())=prev;
}

canonical_original_code:
void perror_wrapper(char *msg)
{
 int prev = (*__errno_location ());
 perror(msg);
 (*__errno_location ())=prev;
}

variable_types:
msg
char *

variable_types: prev int


decompiled_code:
int __fastcall set_nonblock(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func12(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

original_code:
void set_nonblock(int fd)
{
 int fl = fcntl(fd, 3, 0);
 fcntl(fd, 4, fl | 04000);
}

variable_types: fl int


decompiled_code:
int __fastcall set_block(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) &= 0xF7u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func13(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) &= 0xF7u;
return fcntl(fd, 4, v1);
}

original_code:
void set_block(int fd)
{
 int fl = fcntl(fd, 3, 0);
 fcntl(fd, 4, fl & ~04000);
}


decompiled_code:
__int64 __fastcall write_all(int fd, __int64 a2, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v4 = 0;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *__errno_location() != 4 )
return 0LL;
}
else
{
if ( v5 <= 0 )
return 0LL;
v4 += v5;
}
}
while ( a3 > v4 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(int fd, __int64 a2, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v4 = 0;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *__errno_location() != 4 )
return 0LL;
}
else
{
if ( v5 <= 0 )
return 0LL;
v4 += v5;
}
}
while ( a3 > v4 );
return 1LL;
}

original_code:
int write_all(int fd, void *buf, int size)
{
 int written = 0;
 int ret;
 while (written < size) {
  ret = write(fd, (char *) buf + written, size - written);
  if (ret == -1 && (*__errno_location ()) == 4)
   continue;
  if (ret <= 0) {
   return 0;
  }
  written += ret;
 }
 return 1;
}

variable_types:
buf
void *

variable_types: size int

variable_types: written int

variable_types: ret int


decompiled_code:
__int64 __fastcall read_all(int fd, __int64 a2, int a3)
{
int v4; // ebp
int v5; // eax
int v6; // ebx
unsigned int v7; // er15
int v9; // eax

if ( a3 <= 0 )
return 1;
v4 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v4), a3 - v4);
v6 = v5;
v7 = v5;
if ( v5 != -1 )
break;
v9 = *__errno_location();
if ( v9 != 4 )
goto LABEL_13;
LABEL_9:
if ( v4 >= a3 )
return 1;
}
if ( !v5 )
{
*__errno_location() = 0;
fwrite("EOF\n", 1uLL, 4uLL, stderr);
return v7;
}
if ( v5 >= 0 )
{
if ( !v4 )
set_block(fd);
v4 += v6;
goto LABEL_9;
}
v9 = *__errno_location();
LABEL_13:
v7 = 0;
if ( v9 != 11 )
perror_wrapper("read");
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func15(int fd, __int64 a2, int a3)
{
int v4; // ebp
int v5; // eax
int v6; // ebx
unsigned int v7; // er15
int v9; // eax

if ( a3 <= 0 )
return 1;
v4 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v4), a3 - v4);
v6 = v5;
v7 = v5;
if ( v5 != -1 )
break;
v9 = *__errno_location();
if ( v9 != 4 )
goto LABEL_13;
LABEL_9:
if ( v4 >= a3 )
return 1;
}
if ( !v5 )
{
*__errno_location() = 0;
fwrite("EOF\n", 1uLL, 4uLL, stderr);
return v7;
}
if ( v5 >= 0 )
{
if ( !v4 )
func13(fd);
v4 += v6;
goto LABEL_9;
}
v9 = *__errno_location();
LABEL_13:
v7 = 0;
if ( v9 != 11 )
func11("read");
return v7;
}

original_code:
int read_all(int fd, void *buf, int size)
{
 int got_read = 0;
 int ret;
 while (got_read < size) {
  ret = read(fd, (char *) buf + got_read, size - got_read);
  if (ret == -1 && (*__errno_location ()) == 4)
   continue;
  if (ret == 0) {
   (*__errno_location ()) = 0;
   fprintf(stderr, "EOF\n");
   return 0;
  }
  if (ret < 0) {
   if ((*__errno_location ()) != 11)
    perror_wrapper("read");
   return 0;
  }
  if (got_read == 0) {
   set_block(fd);
  }
  got_read += ret;
 }
 return 1;
}


decompiled_code:
__int64 __fastcall copy_fd_all(int fd, int a2)
{
int v2; // eax
char buf[4120]; // [rsp+10h] [rbp-1018h] BYREF

do
{
while ( 1 )
{
v2 = read(a2, buf, 0x1000uLL);
if ( v2 != -1 )
break;
if ( *__errno_location() != 4 )
{
LABEL_7:
perror_wrapper("read");
return 0LL;
}
}
if ( !v2 )
return 1LL;
if ( v2 < 0 )
goto LABEL_7;
}
while ( (unsigned int)write_all(fd, (__int64)buf, v2) );
perror_wrapper("write");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(int fd, int a2)
{
int v2; // eax
char buf[4120]; // [rsp+10h] [rbp-1018h] BYREF

do
{
while ( 1 )
{
v2 = read(a2, buf, 0x1000uLL);
if ( v2 != -1 )
break;
if ( *__errno_location() != 4 )
{
LABEL_7:
func11("read");
return 0LL;
}
}
if ( !v2 )
return 1LL;
if ( v2 < 0 )
goto LABEL_7;
}
while ( (unsigned int)func14(fd, (__int64)buf, v2) );
func11("write");
return 0LL;
}

original_code:
int copy_fd_all(int fdout, int fdin)
{
 int ret;
 char buf[4096];
 for (;;) {
  ret = read(fdin, buf, sizeof(buf));
  if (ret == -1 && (*__errno_location ()) == 4)
   continue;
  if (!ret)
   break;
  if (ret < 0) {
   perror_wrapper("read");
   return 0;
  }
  if (!write_all(fdout, buf, ret)) {
   perror_wrapper("write");
   return 0;
  }
 }
 return 1;
}

variable_types: fdout int

variable_types: fdin int

variable_types:
buf
char [4096]


main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // eax
int v5; // ebp
int v6; // eax
int v7; // ebp
__int64 v8; // r12
__int64 v9; // rsi
__pid_t v10; // eax
unsigned int v11; // er12
int v12; // ebp
size_t v13; // rbx
__int64 v14; // rsi
sigset_t v15; // [rsp+0h] [rbp-10A8h] BYREF
sigset_t buf; // [rsp+80h] [rbp-1028h] BYREF

if ( (unsigned int)(argc - 3) <= 1 )
{
v3 = argc;
v4 = atoi(argv[1]);
*(_QWORD *)&argc = argv[2];
used_mem_change_threshold = v4;
v5 = v4;
v6 = atoi(*(const char **)&argc);
delay = v6;
if ( v5 )
{
if ( v6 )
{
if ( v3 == 4 )
{
v10 = fork();
v11 = v10;
if ( v10 == -1 )
{
perror("fork");
exit(1);
}
if ( v10 )
{
v12 = open(argv[3], 577, 438LL);
if ( v12 >= 0 )
{
v13 = sprintf((char *)&buf, "%d\n", v11);
if ( write(v12, &buf, v13) != v13 )
{
perror("write pid");
exit(1);
}
close(v12);
exit(0);
}
perror("open pidfile");
exit(1);
}
sigemptyset(&v15);
sigaddset(&v15, 10);
sigprocmask(0, &v15, &buf);
usr1_received = 0;
signal(10, usr1_handler);
while ( !usr1_received )
sigsuspend(&buf);
sigprocmask(1, &v15, 0LL);
}
v7 = open("/proc/meminfo", 0);
if ( v7 >= 0 )
{
v8 = xs_domain_open();
if ( v8 )
{
if ( v3 == 3 )
{
*((_BYTE *)buf.__val + (int)pread(v7, &buf, 0x1000uLL, 0LL)) = 0;
v14 = parse(&buf);
if ( v14 )
send_to_qmemman(v8, v14);
if ( fork() > 0 )
exit(0);
}
while ( 1 )
{
*((_BYTE *)buf.__val + (int)pread(v7, &buf, 0x1000uLL, 0LL)) = 0;
v9 = parse(&buf);
if ( v9 )
send_to_qmemman(v8, v9);
usleep(delay);
}
}
perror("xs_domain_open");
exit(1);
}
perror("open meminfo");
exit(1);
}
}
}
usage(*(_QWORD *)&argc, argv, envp);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // eax
int v5; // ebp
int v6; // eax
int v7; // ebp
__int64 v8; // r12
__int64 v9; // rsi
__pid_t v10; // eax
unsigned int v11; // er12
int v12; // ebp
size_t v13; // rbx
__int64 v14; // rsi
sigset_t v15; // [rsp+0h] [rbp-10A8h] BYREF
sigset_t buf; // [rsp+80h] [rbp-1028h] BYREF

if ( (unsigned int)(argc - 3) <= 1 )
{
v3 = argc;
v4 = atoi(argv[1]);
*(_QWORD *)&argc = argv[2];
used_mem_change_threshold = v4;
v5 = v4;
v6 = atoi(*(const char **)&argc);
delay = v6;
if ( v5 )
{
if ( v6 )
{
if ( v3 == 4 )
{
v10 = fork();
v11 = v10;
if ( v10 == -1 )
{
perror("fork");
exit(1);
}
if ( v10 )
{
v12 = open(argv[3], 577, 438LL);
if ( v12 >= 0 )
{
v13 = sprintf((char *)&buf, "%d\n", v11);
if ( write(v12, &buf, v13) != v13 )
{
perror("write pid");
exit(1);
}
close(v12);
exit(0);
}
perror("open pidfile");
exit(1);
}
sigemptyset(&v15);
sigaddset(&v15, 10);
sigprocmask(0, &v15, &buf);
usr1_received = 0;
signal(10, usr1_handler);
while ( !usr1_received )
sigsuspend(&buf);
sigprocmask(1, &v15, 0LL);
}
v7 = open("/proc/meminfo", 0);
if ( v7 >= 0 )
{
v8 = xs_domain_open();
if ( v8 )
{
if ( v3 == 3 )
{
*((_BYTE *)buf.__val + (int)pread(v7, &buf, 0x1000uLL, 0LL)) = 0;
v14 = func8(&buf);
if ( v14 )
func10(v8, v14);
if ( fork() > 0 )
exit(0);
}
while ( 1 )
{
*((_BYTE *)buf.__val + (int)pread(v7, &buf, 0x1000uLL, 0LL)) = 0;
v9 = func8(&buf);
if ( v9 )
func10(v8, v9);
usleep(delay);
}
}
perror("xs_domain_open");
exit(1);
}
perror("open meminfo");
exit(1);
}
}
}
func9(*(_QWORD *)&argc, argv, envp);
}

variable_types: argc int

variable_types:
argv
char **


repo: ptitiano/cpuloadgen

decompiled_code:
void free_buffers()
{
void *v0; // rdi

if ( !threads )
{
v0 = cpuloads;
if ( !cpuloads )
return;
goto LABEL_5;
}
free(threads);
v0 = cpuloads;
if ( cpuloads )
LABEL_5:
free(v0);
}

canonical_decompiled_code:
void func0()
{
void *v0; // rdi

if ( !threads )
{
v0 = cpuloads;
if ( !cpuloads )
return;
goto LABEL_5;
}
free(threads);
v0 = cpuloads;
if ( cpuloads )
LABEL_5:
free(v0);
}

original_code:
static void free_buffers(void)
{
 if (threads != ((void *)0))
  free(threads);
 if (cpuloads != ((void *)0))
  free(cpuloads);
}

canonical_original_code:
static void free_buffers()
{
 if (threads != ((void *)0))
  free(threads);
 if (cpuloads != ((void *)0))
  free(cpuloads);
}


decompiled_code:
void sigterm_handler()
{
puts("Halting load generation...");
fflush(stdout);
free_buffers();
puts("done.\n");
fflush(stdout);
}

canonical_decompiled_code:
void func1()
{
puts("Halting load generation...");
fflush(stdout);
func0();
puts("done.\n");
fflush(stdout);
}

original_code:
void sigterm_handler(void)
{
 unsigned int i;
 printf("Halting load generation...\n");
 fflush(stdout);
 free_buffers();
 printf("done.\n\n");
 fflush(stdout);
}

canonical_original_code:
void sigterm_handler()
{
 unsigned int i;
 printf("Halting load generation...\n");
 fflush(stdout);
 free_buffers();
 printf("done.\n\n");
 fflush(stdout);
}

variable_types:
i
unsigned int


usage

decompiled_code:
int usage()
{
puts("Usage:");
puts("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n");
puts("Generate adjustable processing load on selected CPU core(s) for a given duration.");
puts("Load is a percentage which may be any integer value between 1 and 100.");
puts("Duration time unit is seconds.");
puts("Arguments may be provided in any order.");
puts("If duration is omitted, generate load(s) until CTRL+C is pressed.");
printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
puts("e.g.:");
printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
puts("\t# cpuloadgen");
printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
puts("\t# cpuloadgen duration=10");
printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
return puts("\t# cpuloadgen cpu3=100 cpu1=50 duration=5\n");
}

canonical_decompiled_code:
int func2()
{
puts("Usage:");
puts("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n");
puts("Generate adjustable processing load on selected CPU core(s) for a given duration.");
puts("Load is a percentage which may be any integer value between 1 and 100.");
puts("Duration time unit is seconds.");
puts("Arguments may be provided in any order.");
puts("If duration is omitted, generate load(s) until CTRL+C is pressed.");
printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
puts("e.g.:");
printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
puts("\t# cpuloadgen");
printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
puts("\t# cpuloadgen duration=10");
printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
return puts("\t# cpuloadgen cpu3=100 cpu1=50 duration=5\n");
}

original_code:
static void usage(void)
{
 printf("Usage:\n");
 printf("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n\n");
 printf("Generate adjustable processing load on selected CPU core(s) for a given duration.\n");
 printf("Load is a percentage which may be any integer value between 1 and 100.\n");
 printf("Duration time unit is seconds.\n");
 printf("Arguments may be provided in any order.\n");
 printf("If duration is omitted, generate load(s) until CTRL+C is pressed.\n");
 printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
 printf("e.g.:\n");
 printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
 printf("	# cpuloadgen\n");
 printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
 printf("	# cpuloadgen duration=10\n");
 printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
 printf("	# cpuloadgen cpu3=100 cpu1=50 duration=5\n\n");
}

canonical_original_code:
static void usage()
{
 printf("Usage:\n");
 printf("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n\n");
 printf("Generate adjustable processing load on selected CPU core(s) for a given duration.\n");
 printf("Load is a percentage which may be any integer value between 1 and 100.\n");
 printf("Duration time unit is seconds.\n");
 printf("Arguments may be provided in any order.\n");
 printf("If duration is omitted, generate load(s) until CTRL+C is pressed.\n");
 printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
 printf("e.g.:\n");
 printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
 printf("	# cpuloadgen\n");
 printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
 printf("	# cpuloadgen duration=10\n");
 printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
 printf("	# cpuloadgen cpu3=100 cpu1=50 duration=5\n\n");
}


einval

decompiled_code:
__int64 __fastcall einval(const char *a1)
{
fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", a1);
usage();
free_buffers();
return 4294967274LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", a1);
func2();
func0();
return 4294967274LL;
}

original_code:
static int einval(const char *arg)
{
 fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", arg);
 usage();
 free_buffers();
 return -22;
}

canonical_original_code:
static int einval(char *arg)
{
 fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", arg);
 usage();
 free_buffers();
 return -22;
}

variable_types:
arg
char *


loadgen

decompiled_code:
double __fastcall loadgen(unsigned int a1, unsigned int a2, int a3)
{
double v4; // xmm0_8
double result; // xmm0_8
double v6; // [rsp+0h] [rbp-E8h]
double v7; // [rsp+8h] [rbp-E0h]
struct timezone tz; // [rsp+18h] [rbp-D0h] BYREF
struct timeval tv; // [rsp+20h] [rbp-C8h] BYREF
struct timeval v10; // [rsp+30h] [rbp-B8h] BYREF
cpu_set_t cpuset; // [rsp+40h] [rbp-A8h] BYREF

memset(&cpuset, 0, sizeof(cpuset));
if ( a1 <= 0x3FF )
cpuset.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
sched_setaffinity(0, 8uLL, &cpuset);
printf("Generating %3d%% load on CPU%d...\n", a2, a1);
gettimeofday(&tv, &tz);
v7 = (double)SLODWORD(tv.tv_usec) * 0.000001 + (double)SLODWORD(tv.tv_sec);
if ( a2 == 100 )
{
do
{
do
{
dhryStone(1000000);
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7;
}
while ( result < (double)a3 );
}
else
{
do
{
do
{
v6 = dtime();
dhryStone(200000);
v4 = dtime();
usleep((int)(100.0 / (double)(int)(a2 + 1) * ((v4 - v6) * 1000000.0) - (v4 - v6) * 1000000.0));
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7;
}
while ( result < (double)a3 );
}
return result;
}

canonical_decompiled_code:
double __fastcall func10(unsigned int a1, unsigned int a2, int a3)
{
double v4; // xmm0_8
double result; // xmm0_8
double v6; // [rsp+0h] [rbp-E8h]
double v7; // [rsp+8h] [rbp-E0h]
struct timezone tz; // [rsp+18h] [rbp-D0h] BYREF
struct timeval tv; // [rsp+20h] [rbp-C8h] BYREF
struct timeval v10; // [rsp+30h] [rbp-B8h] BYREF
cpu_set_t cpuset; // [rsp+40h] [rbp-A8h] BYREF

memset(&cpuset, 0, sizeof(cpuset));
if ( a1 <= 0x3FF )
cpuset.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
sched_setaffinity(0, 8uLL, &cpuset);
printf("Generating %3d%% load on CPU%d...\n", a2, a1);
gettimeofday(&tv, &tz);
v7 = (double)SLODWORD(tv.tv_usec) * 0.000001 + (double)SLODWORD(tv.tv_sec);
if ( a2 == 100 )
{
do
{
do
{
func9(1000000);
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7;
}
while ( result < (double)a3 );
}
else
{
do
{
do
{
v6 = dtime();
func9(200000);
v4 = dtime();
usleep((int)(100.0 / (double)(int)(a2 + 1) * ((v4 - v6) * 1000000.0) - (v4 - v6) * 1000000.0));
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7;
}
while ( result < (double)a3 );
}
return result;
}

original_code:
void loadgen(unsigned int cpu, unsigned int load, unsigned int duration)
{
 double dhrystone_start_time, dhrystone_end_time;
 double idle_time_us;
 double loadgen_start_time_us, active_time_us;
 double total_time_us;
 struct timeval tv_cpuloadgen_start, tv_cpuloadgen;
 struct timeval tv_idle_start, tv_idle_stop;
 struct timezone tz;
 double time_us;
 unsigned long mask;
 unsigned int len = sizeof(mask);
 cpu_set_t set;
 do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0);
 (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
 sched_setaffinity(0, len, &set);
 printf("Generating %3d%% load on CPU%d...\n", load, cpu);
 gettimeofday(&tv_cpuloadgen_start, &tz);
 loadgen_start_time_us = ((double) tv_cpuloadgen_start.tv_sec
  + ((double) tv_cpuloadgen_start.tv_usec * 1.0e-6));
 ;
 if (load != 100) {
  while (1) {
   dhrystone_start_time = dtime();
   dhryStone(200000);
   dhrystone_end_time = dtime();
   active_time_us =
    (dhrystone_end_time - dhrystone_start_time) * 1.0e6;
   ;
   total_time_us =
    active_time_us * (100.0 / (double) (load + 1));
   ;
   idle_time_us = total_time_us - active_time_us;
   ;
   usleep((unsigned int) (idle_time_us));
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 } else {
  while (1) {
   dhryStone(1000000);
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 }
 ;
}

canonical_original_code:
void loadgen(unsigned int cpu, unsigned int load, unsigned int duration)
{
 double dhrystone_start_time; double dhrystone_end_time;
 double idle_time_us;
 double loadgen_start_time_us; double active_time_us;
 double total_time_us;
 struct timeval tv_cpuloadgen_start; struct timeval tv_cpuloadgen;
 struct timeval tv_idle_start; struct timeval tv_idle_stop;
 struct timezone tz;
 double time_us;
 unsigned long mask;
 unsigned int len = sizeof(mask);
 struct cpu_set_t set;
 do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0);
 (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&set)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
 sched_setaffinity(0, len, &set);
 printf("Generating %3d%% load on CPU%d...\n", load, cpu);
 gettimeofday(&tv_cpuloadgen_start, &tz);
 loadgen_start_time_us = ((double) tv_cpuloadgen_start.tv_sec
  + ((double) tv_cpuloadgen_start.tv_usec * 1.0e-6));
 ;
 if (load != 100) {
  while (1) {
   dhrystone_start_time = dtime();
   dhryStone(200000);
   dhrystone_end_time = dtime();
   active_time_us =
    (dhrystone_end_time - dhrystone_start_time) * 1.0e6;
   ;
   total_time_us =
    active_time_us * (100.0 / (double) (load + 1));
   ;
   idle_time_us = total_time_us - active_time_us;
   ;
   usleep((unsigned int) (idle_time_us));
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 } else {
  while (1) {
   dhryStone(1000000);
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 }
 ;
}

variable_types:
cpu
unsigned int

variable_types:
load
unsigned int

variable_types:
duration
unsigned int

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

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

variable_types:
mask
unsigned long

variable_types:
len
unsigned int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
void __fastcall __noreturn thread_loadgen(void *a1)
{
unsigned int v1; // er12

v1 = *(_DWORD *)a1;
pthread_mutex_unlock(&mutex1);
if ( cpu_count <= v1 )
fprintf(stderr, "%s: invalid cpu argument!!! (%d)\n", "thread_loadgen", v1);
else
loadgen(v1, *((_DWORD *)cpuloads + v1), duration);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func11(void *a1)
{
unsigned int v1; // er12

v1 = *(_DWORD *)a1;
pthread_mutex_unlock(&mutex1);
if ( cpu_count <= v1 )
fprintf(stderr, "%s: invalid cpu argument!!! (%d)\n", "thread_loadgen", v1);
else
func10(v1, *((_DWORD *)cpuloads + v1), duration);
pthread_exit(0LL);
}

original_code:
void *thread_loadgen(void *ptr)
{
 unsigned int cpu;
 cpu = *((unsigned int *) ptr);
 pthread_mutex_unlock(&mutex1);
 if (cpu < cpu_count) {
  loadgen(cpu, cpuloads[cpu], duration);
 } else {
  fprintf(stderr, "%s: invalid cpu argument!!! (%d)\n",
   __func__, cpu);
 }
 pthread_exit(((void *)0));
}

variable_types:
ptr
void *

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
int v5; // er13
__int64 v6; // r12
void *v7; // r14
void *v8; // rax
_DWORD *v9; // r12
__int64 v10; // r13
int v11; // eax
const char *v12; // rdi
unsigned int *v13; // rax
__int64 v14; // rbx
_DWORD *v15; // rax
int v16; // eax
unsigned int v17; // eax
int v18; // eax
int result; // eax
int arg; // [rsp+Ch] [rbp-3Ch] BYREF
int v21; // [rsp+10h] [rbp-38h] BYREF
unsigned int v22; // [rsp+14h] [rbp-34h] BYREF
__int64 v23; // [rsp+18h] [rbp-30h] BYREF

signal(15, (__sighandler_t)sigterm_handler);
printf("CPULOADGEN (REV %s built %s)\n\n", "0.94", builddate);
v4 = sysconf(84);
cpu_count = v4;
v5 = v4;
if ( v4 <= 0 )
{
fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n", (unsigned int)v4);
return cpu_count;
}
v6 = v4;
threads = malloc(8LL * v4);
v7 = threads;
v8 = malloc(4 * v6);
cpuloads = v8;
v9 = v8;
if ( v7 && v8 )
{
if ( argc == 1 )
{
v14 = (unsigned int)(v5 - 1) + 1LL;
memset(v7, 255, 8 * v14);
v15 = v9;
do
*v15++ = 100;
while ( &v9[v14] != v15 );
arg = v5;
duration = -1LL;
}
else
{
v10 = (unsigned int)(v5 - 1) + 1LL;
memset(v7, 255, 8 * v10);
memset(v9, 255, 4 * v10);
arg = 1;
v11 = 1;
for ( duration = -1LL; v11 < argc; arg = v11 )
{
v12 = argv[v11];
if ( *v12 == 99 )
{
if ( (unsigned int)_isoc99_sscanf(v12, "cpu%d=%d", &v21, &v22) != 2
|| v21 < 0
|| v21 >= cpu_count
|| v22 - 1 > 0x63 )
{
return einval(argv[arg]);
}
v13 = (unsigned int *)((char *)cpuloads + 4 * v21);
if ( *v13 != -1 )
{
fprintf(stderr, "cpuloadgen: CPU%d was already assigned a load of %d!\n\n", (unsigned int)v21, *v13);
free_buffers();
return -22;
}
*v13 = v22;
}
else
{
if ( *v12 != 100 )
return einval(v12);
if ( (unsigned int)_isoc99_sscanf(v12, "duration=%ld", &v23) != 1 || v23 <= 0 )
return einval(argv[arg]);
if ( duration != -1 )
{
fprintf(stderr, "cpuloadgen: duration was already set to %ld!\n\n", duration);
free_buffers();
return -22;
}
duration = v23;
}
v11 = arg + 1;
}
}
puts("Press CTRL+C to stop load generation at any time.\n");
v16 = 0;
for ( arg = 0; arg < cpu_count; v16 = arg )
{
if ( *((_DWORD *)cpuloads + v16) != -1 )
{
pthread_mutex_lock(&mutex1);
v17 = pthread_create((pthread_t *)threads + arg, 0LL, (void *(*)(void *))thread_loadgen, &arg);
if ( v17 )
fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)", (unsigned int)arg, v17);
}
++arg;
}
v18 = 0;
for ( arg = 0; arg < cpu_count; v18 = arg )
{
if ( *((_DWORD *)cpuloads + v18) != -1 )
pthread_join(*((_QWORD *)threads + v18), 0LL);
++arg;
}
free_buffers();
puts("\ndone.\n");
result = 0;
}
else
{
fwrite("cpuloadgen: could not allocate buffers!!!\n", 1uLL, 0x2AuLL, stderr);
result = -12;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
int v5; // er13
__int64 v6; // r12
void *v7; // r14
void *v8; // rax
_DWORD *v9; // r12
__int64 v10; // r13
int v11; // eax
const char *v12; // rdi
unsigned int *v13; // rax
__int64 v14; // rbx
_DWORD *v15; // rax
int v16; // eax
unsigned int v17; // eax
int v18; // eax
int result; // eax
int arg; // [rsp+Ch] [rbp-3Ch] BYREF
int v21; // [rsp+10h] [rbp-38h] BYREF
unsigned int v22; // [rsp+14h] [rbp-34h] BYREF
__int64 v23; // [rsp+18h] [rbp-30h] BYREF

signal(15, (__sighandler_t)sigterm_handler);
printf("CPULOADGEN (REV %s built %s)\n\n", "0.94", builddate);
v4 = sysconf(84);
cpu_count = v4;
v5 = v4;
if ( v4 <= 0 )
{
fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n", (unsigned int)v4);
return cpu_count;
}
v6 = v4;
threads = malloc(8LL * v4);
v7 = threads;
v8 = malloc(4 * v6);
cpuloads = v8;
v9 = v8;
if ( v7 && v8 )
{
if ( argc == 1 )
{
v14 = (unsigned int)(v5 - 1) + 1LL;
memset(v7, 255, 8 * v14);
v15 = v9;
do
*v15++ = 100;
while ( &v9[v14] != v15 );
arg = v5;
duration = -1LL;
}
else
{
v10 = (unsigned int)(v5 - 1) + 1LL;
memset(v7, 255, 8 * v10);
memset(v9, 255, 4 * v10);
arg = 1;
v11 = 1;
for ( duration = -1LL; v11 < argc; arg = v11 )
{
v12 = argv[v11];
if ( *v12 == 99 )
{
if ( (unsigned int)_isoc99_sscanf(v12, "cpu%d=%d", &v21, &v22) != 2
|| v21 < 0
|| v21 >= cpu_count
|| v22 - 1 > 0x63 )
{
return func3(argv[arg]);
}
v13 = (unsigned int *)((char *)cpuloads + 4 * v21);
if ( *v13 != -1 )
{
fprintf(stderr, "cpuloadgen: CPU%d was already assigned a load of %d!\n\n", (unsigned int)v21, *v13);
func0();
return -22;
}
*v13 = v22;
}
else
{
if ( *v12 != 100 )
return func3(v12);
if ( (unsigned int)_isoc99_sscanf(v12, "duration=%ld", &v23) != 1 || v23 <= 0 )
return func3(argv[arg]);
if ( duration != -1 )
{
fprintf(stderr, "cpuloadgen: duration was already set to %ld!\n\n", duration);
func0();
return -22;
}
duration = v23;
}
v11 = arg + 1;
}
}
puts("Press CTRL+C to stop load generation at any time.\n");
v16 = 0;
for ( arg = 0; arg < cpu_count; v16 = arg )
{
if ( *((_DWORD *)cpuloads + v16) != -1 )
{
pthread_mutex_lock(&mutex1);
v17 = pthread_create((pthread_t *)threads + arg, 0LL, (void *(*)(void *))thread_loadgen, &arg);
if ( v17 )
fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)", (unsigned int)arg, v17);
}
++arg;
}
v18 = 0;
for ( arg = 0; arg < cpu_count; v18 = arg )
{
if ( *((_DWORD *)cpuloads + v18) != -1 )
pthread_join(*((_QWORD *)threads + v18), 0LL);
++arg;
}
func0();
puts("\ndone.\n");
result = 0;
}
else
{
fwrite("cpuloadgen: could not allocate buffers!!!\n", 1uLL, 0x2AuLL, stderr);
result = -12;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
 unsigned int cpu0load, cpu1load, cpu2load, cpu3load;
 int i, ret, n, load;
 long int duration2;
 signal(15, (sighandler_t) sigterm_handler);
 printf("CPULOADGEN (REV %s built %s)\n\n",
  ((const char *) "0.94"), builddate);
 cpu_count = (int) sysconf(_SC_NPROCESSORS_ONLN);
 if (cpu_count < 1) {
  fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n",
   cpu_count);
  return cpu_count;
 }
 ;
 threads = malloc(cpu_count * sizeof(pthread_t));
 cpuloads = malloc(cpu_count * sizeof(int));
 if ((threads == ((void *)0)) || (cpuloads == ((void *)0))) {
  fprintf(stderr, "cpuloadgen: could not allocate buffers!!!\n");
  return -12;
 }
 if (argc == 1) {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = 100;
  }
  duration = -1;
 } else {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = -1;
  }
  duration = -1;
  for (i = 1; i < argc; i++) {
   ;
   if (argv[i][0] == 'c') {
    ret = sscanf(argv[i], "cpu%d=%d", &n, &load);
    if ((ret != 2) ||
     ((n < 0) || (n >= cpu_count)) ||
     ((load < 1) || (load > 100)))
     return einval(argv[i]);
    if (cpuloads[n] != -1) {
     fprintf(stderr,
      "cpuloadgen: CPU%d was already assigned a load of %d!\n\n",
      n, cpuloads[n]);
     free_buffers();
     return -22;
    }
    cpuloads[n] = load;
    ;
   } else if (argv[i][0] == 'd') {
    ret = sscanf(argv[i], "duration=%ld",
     &duration2);
    if ((ret != 1) || (duration2 < 1)) {
     return einval(argv[i]);
    }
    if (duration != -1) {
     fprintf(stderr,
      "cpuloadgen: duration was already set to %ld!\n\n",
      duration);
     free_buffers();
     return -22;
    }
    duration = duration2;
    ;
   } else {
    return einval(argv[i]);
   }
  }
 }
 printf("Press CTRL+C to stop load generation at any time.\n\n");
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   ;
   continue;
  }
  pthread_mutex_lock(&mutex1);
  ret = pthread_create(&threads[i], ((void *)0), thread_loadgen, &i);
  if (ret != 0) {
   fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)",
   i, ret);
   continue;
  }
 }
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   continue;
  }
  pthread_join(threads[i], ((void *)0));
 }
 free_buffers();
 printf("\ndone.\n\n");
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 unsigned int cpu0load; unsigned int cpu1load; unsigned int cpu2load; unsigned int cpu3load;
 int i; int ret; int n; int load;
 long duration2;
 signal(15, (void (*)(int)) sigterm_handler);
 printf("CPULOADGEN (REV %s built %s)\n\n",
  ((char *) "0.94"), builddate);
 cpu_count = (int) sysconf(_SC_NPROCESSORS_ONLN);
 if (cpu_count < 1) {
  fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n",
   cpu_count);
  return cpu_count;
 }
 ;
 threads = malloc(cpu_count * sizeof(pthread_t));
 cpuloads = malloc(cpu_count * sizeof(int));
 if ((threads == ((void *)0)) || (cpuloads == ((void *)0))) {
  fprintf(stderr, "cpuloadgen: could not allocate buffers!!!\n");
  return -12;
 }
 if (argc == 1) {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = 100;
  }
  duration = -1;
 } else {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = -1;
  }
  duration = -1;
  for (i = 1; i < argc; i++) {
   ;
   if (argv[i][0] == 'c') {
    ret = sscanf(argv[i], "cpu%d=%d", &n, &load);
    if ((ret != 2) ||
     ((n < 0) || (n >= cpu_count)) ||
     ((load < 1) || (load > 100)))
     return einval(argv[i]);
    if (cpuloads[n] != -1) {
     fprintf(stderr,
      "cpuloadgen: CPU%d was already assigned a load of %d!\n\n",
      n, cpuloads[n]);
     free_buffers();
     return -22;
    }
    cpuloads[n] = load;
    ;
   } else if (argv[i][0] == 'd') {
    ret = sscanf(argv[i], "duration=%ld",
     &duration2);
    if ((ret != 1) || (duration2 < 1)) {
     return einval(argv[i]);
    }
    if (duration != -1) {
     fprintf(stderr,
      "cpuloadgen: duration was already set to %ld!\n\n",
      duration);
     free_buffers();
     return -22;
    }
    duration = duration2;
    ;
   } else {
    return einval(argv[i]);
   }
  }
 }
 printf("Press CTRL+C to stop load generation at any time.\n\n");
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   ;
   continue;
  }
  pthread_mutex_lock(&mutex1);
  ret = pthread_create(&threads[i], ((void *)0), thread_loadgen, &i);
  if (ret != 0) {
   fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)",
   i, ret);
   continue;
  }
 }
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   continue;
  }
  pthread_join(threads[i], ((void *)0));
 }
 free_buffers();
 printf("\ndone.\n\n");
 return 0;
}

variable_types:
argv
char *[]

variable_types: i int

variable_types: ret int

variable_types: n int

variable_types: load int


dtime

decompiled_code:
double dtime()
{
getrusage(RUSAGE_SELF, &rusage);
return (double)(int)rusage.ru_utime.tv_usec * 0.000001 + (double)SLODWORD(rusage.ru_utime.tv_sec);
}

canonical_decompiled_code:
double func0()
{
getrusage(RUSAGE_SELF, &rusage);
return (double)(int)rusage.ru_utime.tv_usec * 0.000001 + (double)SLODWORD(rusage.ru_utime.tv_sec);
}

original_code:
double dtime()
{
 double q;
 getrusage(RUSAGE_SELF,&rusage);
 q = (double)(rusage.ru_utime.tv_sec);
 q = q + (double)(rusage.ru_utime.tv_usec) * 1.0e-06;
 return q;
}

variable_types: q double

return_type: double


myCloseCB

repo: ptorpman/compframe

decompiled_code:
__int64 __fastcall myCloseCB(const void *a1, unsigned int a2, const void *a3)
{
fprintf(stdout, "Connection is closed! %p chan=%d user=%p\n", a1, a2, a3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, unsigned int a2, const void *a3)
{
fprintf(stdout, "Connection is closed! %p chan=%d user=%p\n", a1, a2, a3);
return 1LL;
}

original_code:
static int
myCloseCB(void* conn, int chan,void* userData)
{
  fprintf(stdout,
          "Connection is closed! %p chan=%d user=%p\n",
          conn, chan, userData);
  return 1;
}

canonical_original_code:
static int myCloseCB(void *conn, int chan, void *userData)
{
  fprintf(stdout,
          "Connection is closed! %p chan=%d user=%p\n",
          conn, chan, userData);
  return 1;
}

variable_types:
conn
void *

variable_types: chan int

variable_types:
userData
void *


myErrorCB

decompiled_code:
void __fastcall __noreturn myErrorCB(__int64 a1, const char *a2, __int64 a3)
{
const char *v3; // rcx

v3 = a2;
if ( !a2 )
v3 = "NONE";
fprintf(stdout, "Connection has errors! %p msg=%s user=%p\n", a1, v3, a3);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1, const char *a2, __int64 a3)
{
const char *v3; // rcx

v3 = a2;
if ( !a2 )
v3 = "NONE";
fprintf(stdout, "Connection has errors! %p msg=%s user=%p\n", a1, v3, a3);
exit(1);
}

original_code:
static int
myErrorCB(void* conn, char* errorMsg, void* userData)
{
  fprintf(stdout,
          "Connection has errors! %p msg=%s user=%p\n",
          conn,
          (errorMsg ? errorMsg : "NONE"),
          userData);
  exit(1);
}

canonical_original_code:
static int myErrorCB(void *conn, char *errorMsg, void *userData)
{
  fprintf(stdout,
          "Connection has errors! %p msg=%s user=%p\n",
          conn,
          (errorMsg ? errorMsg : "NONE"),
          userData);
  exit(1);
}

variable_types:
errorMsg
char *


myMsgCB

decompiled_code:
void __fastcall __noreturn myMsgCB(const void *a1, unsigned int a2, unsigned int a3, const char *a4, const void *a5)
{
fprintf(stderr, "Got a message! conn=%p chan=%d user=%p len=%d\nMSG: >%s<\n", a1, a2, a5, a3, a4);
cfm_channel_close(a1, a2);
cfm_connection_close(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const void *a1, unsigned int a2, unsigned int a3, const char *a4, const void *a5)
{
fprintf(stderr, "Got a message! conn=%p chan=%d user=%p len=%d\nMSG: >%s<\n", a1, a2, a5, a3, a4);
cfm_channel_close(a1, a2);
cfm_connection_close(a1);
exit(1);
}

original_code:
static int
myMsgCB(void* conn, int chan,int len, unsigned char* msg, void* userData)
{
  fprintf(stderr,
          "Got a message! conn=%p chan=%d user=%p len=%d\n"
          "MSG: >%s<\n",
          conn, chan, userData,len, msg);
  cfm_channel_close(conn,chan);
  cfm_connection_close(conn);
  exit(1);
  return 1;
}

canonical_original_code:
static int myMsgCB(void *conn, int chan, int len, unsigned char *msg, void *userData)
{
  fprintf(stderr,
          "Got a message! conn=%p chan=%d user=%p len=%d\n"
          "MSG: >%s<\n",
          conn, chan, userData,len, msg);
  cfm_channel_close(conn,chan);
  cfm_connection_close(conn);
  exit(1);
  return 1;
}

variable_types: len int

variable_types:
msg
unsigned char *


myOpenCB

decompiled_code:
__int64 __fastcall myOpenCB(const void *a1, unsigned int a2, const void *a3)
{
__m128i v4; // [rsp+0h] [rbp-118h] BYREF
char v5; // [rsp+10h] [rbp-108h]

fprintf(stdout, "Connection is open! %p chan=%d user=%p\n", a1, a2, a3);
v5 = 0;
v4 = _mm_load_si128((const __m128i *)&LC5);
return cfm_message_send(a1, a2, 17LL, &v4);
}

canonical_decompiled_code:
__int64 __fastcall func3(const void *a1, unsigned int a2, const void *a3)
{
__m128i v4; // [rsp+0h] [rbp-118h] BYREF
char v5; // [rsp+10h] [rbp-108h]

fprintf(stdout, "Connection is open! %p chan=%d user=%p\n", a1, a2, a3);
v5 = 0;
v4 = _mm_load_si128((const __m128i *)&LC5);
return cfm_message_send(a1, a2, 17LL, &v4);
}

original_code:
static int
myOpenCB(void* conn, int chan,void* userData)
{
  fprintf(stdout,
          "Connection is open! %p chan=%d user=%p\n",
          conn, chan, userData);
  char msg[256];
  strcpy(msg, "Hello, sample2!\n");
  return cfm_message_send(conn,chan, strlen(msg) + 1, (unsigned char*)msg );
}

canonical_original_code:
static int myOpenCB(void *conn, int chan, void *userData)
{
  fprintf(stdout,
          "Connection is open! %p chan=%d user=%p\n",
          conn, chan, userData);
  char msg[256];
  strcpy(msg, "Hello, sample2!\n");
  return cfm_message_send(conn,chan, strlen(msg) + 1, (unsigned char *)msg );
}

variable_types:
msg
char [256]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v5; // eax
__int64 v6; // rax
__int64 v7; // rbp
const char *v8; // rsi
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rax
__int64 v12; // [rsp-10h] [rbp-48h]
struct pollfd v13; // [rsp+0h] [rbp-38h] BYREF

if ( argc == 4 )
{
v5 = atoi(argv[2]);
v6 = cfm_connection_open(argv[1], v5);
v7 = v6;
if ( v6 )
{
if ( (unsigned int)cfm_channel_open(
v6,
"433e76d0-77d1-460d-9321-e2dc8dc8bd59",
argv[3],
myOpenCB,
myCloseCB,
myMsgCB,
myErrorCB,
0LL) == -1 )
{
fwrite("Channel could not be opened\n", 1uLL, 0x1CuLL, stderr);
cfm_connection_close(v7);
}
v8 = "433e76d0-77d1-460d-9321-e2dc8dc8bd59";
v9 = cfm_channel_open(
v7,
"433e76d0-77d1-460d-9321-e2dc8dc8bd59",
argv[3],
myOpenCB,
myCloseCB,
myMsgCB,
myErrorCB,
0LL);
v10 = v12;
if ( v9 == -1 )
{
v8 = (_BYTE *)(myCloseCB + 1);
fwrite("Channel could not be opened\n", 1uLL, 0x1CuLL, stderr);
cfm_connection_close(v7);
}
v13.events = 1;
v13.fd = cfm_connection_sd_get(v7, v8, v10);
do
{
do
poll(&v13, 1uLL, 10);
while ( v13.revents != 1 );
}
while ( (int)cfm_sockets_handle((unsigned int)v13.fd) >= 0 );
v11 = cfm_connection_find((unsigned int)v13.fd);
cfm_connection_close(v11);
fwrite("Info: Connection closed!\n", 1uLL, 0x19uLL, stderr);
}
else
{
fwrite("Connection could not be established\n", 1uLL, 0x24uLL, stderr);
}
}
else
{
fwrite("Usage: sample_m_client <host> <port> <receiver>\n", 1uLL, 0x30uLL, stderr);
}
return 1;
}

original_code:
int main(int argc, char** argv)
{
  struct pollfd fds[2];
  if (argc != 4) {
    fprintf(stderr,"Usage: sample_m_client <host> <port> <receiver>\n");
    return 1;
  }
  void* conn = cfm_connection_open(argv[1],atoi(argv[2]));
  if (!conn) {
    fprintf(stderr,"Connection could not be established\n");
    return 1;
  }
  int channel =
    cfm_channel_open(conn,
                     "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                     argv[3],
                     myOpenCB,
                     myCloseCB,
                     myMsgCB,
                     myErrorCB,
                     ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  channel = cfm_channel_open(conn,
                             "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                             argv[3],
                             myOpenCB,
                             myCloseCB,
                             myMsgCB,
                             myErrorCB,
                             ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  fds[0].fd = cfm_connection_sd_get(conn);
  fds[0].events = 0x001;
  int res;
  while (1) {
    res = poll(fds, 1, 10);
    if (fds[0].revents == 0x001) {
      if (cfm_sockets_handle(fds[0].fd) < 0) {
        conn = cfm_connection_find(fds[0].fd);
        cfm_connection_close(conn);
        fprintf(stderr,"Info: Connection closed!\n");
        return 1;
      }
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct pollfd fds[2];
  if (argc != 4) {
    fprintf(stderr,"Usage: sample_m_client <host> <port> <receiver>\n");
    return 1;
  }
  void *conn = cfm_connection_open(argv[1],atoi(argv[2]));
  if (!conn) {
    fprintf(stderr,"Connection could not be established\n");
    return 1;
  }
  int channel = cfm_channel_open(conn,
                     "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                     argv[3],
                     myOpenCB,
                     myCloseCB,
                     myMsgCB,
                     myErrorCB,
                     ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  channel = cfm_channel_open(conn,
                             "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                             argv[3],
                             myOpenCB,
                             myCloseCB,
                             myMsgCB,
                             myErrorCB,
                             ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  fds[0].fd = cfm_connection_sd_get(conn);
  fds[0].events = 0x001;
  int res;
  while (1) {
    res = poll(fds, 1, 10);
    if (fds[0].revents == 0x001) {
      if (cfm_sockets_handle(fds[0].fd) < 0) {
        conn = cfm_connection_find(fds[0].fd);
        cfm_connection_close(conn);
        fprintf(stderr,"Info: Connection closed!\n");
        return 1;
      }
    }
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct pollfd { int fd; short events; short revents; } [2]

variable_types: channel int

variable_types: res int


decompiled_code:
__int64 __fastcall uuid__get_os_time(int *a1)
{
int v1; // edx
int v2; // eax
__int64 result; // rax
int v4; // edx
unsigned int v5; // ecx
int v9; // [rsp+0h] [rbp-28h] BYREF
int v10; // [rsp+4h] [rbp-24h]
unsigned int v11; // [rsp+8h] [rbp-20h] BYREF
int v12; // [rsp+Ch] [rbp-1Ch]
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

if ( gettimeofday(&tv, 0LL) )
{
perror("uuid__get_os_time");
exit(-1);
}
uuid__uemul(LODWORD(tv.tv_sec), 10000000LL, &v9);
uuid__uemul(LODWORD(tv.tv_usec), 10LL, &v11);
v1 = v11 + v9;
if ( ((v11 ^ v9) & 0x80000000) != 0 )
{
v2 = v12 + v10;
if ( v1 >= 0 )
{
++v2;
goto LABEL_4;
}
}
else
{
v2 = v12 + v10 - ((v11 < 0x80000000) - 1);
if ( v1 >= 0 )
{
LABEL_4:
result = (unsigned int)(v2 + 28450258);
*a1 = v1 + 327237632;
a1[1] = result;
return result;
}
}
v4 = v1 + 327237632;
v5 = v2 + 28450259;
result = (unsigned int)(v2 + 28450258);
*a1 = v4;
if ( v4 >= 0 )
result = v5;
a1[1] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1)
{
int v1; // edx
int v2; // eax
__int64 result; // rax
int v4; // edx
unsigned int v5; // ecx
int v9; // [rsp+0h] [rbp-28h] BYREF
int v10; // [rsp+4h] [rbp-24h]
unsigned int v11; // [rsp+8h] [rbp-20h] BYREF
int v12; // [rsp+Ch] [rbp-1Ch]
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

if ( gettimeofday(&tv, 0LL) )
{
perror("uuid__get_os_time");
exit(-1);
}
uuid__uemul(LODWORD(tv.tv_sec), 10000000LL, &v9);
uuid__uemul(LODWORD(tv.tv_usec), 10LL, &v11);
v1 = v11 + v9;
if ( ((v11 ^ v9) & 0x80000000) != 0 )
{
v2 = v12 + v10;
if ( v1 >= 0 )
{
++v2;
goto LABEL_4;
}
}
else
{
v2 = v12 + v10 - ((v11 < 0x80000000) - 1);
if ( v1 >= 0 )
{
LABEL_4:
result = (unsigned int)(v2 + 28450258);
*a1 = v1 + 327237632;
a1[1] = result;
return result;
}
}
v4 = v1 + 327237632;
v5 = v2 + 28450259;
result = (unsigned int)(v2 + 28450258);
*a1 = v4;
if ( v4 >= 0 )
result = v5;
a1[1] = result;
return result;
}

original_code:
void uuid__get_os_time (uuid_time_t * uuid_time)
{
    struct timeval tp;
    unsigned64_t utc,
                        usecs,
                        os_basetime_diff;
    if (gettimeofday (&tp, (struct timezone *) 0))
    {
        perror ("uuid__get_os_time");
        exit (-1);
    }
    uuid__uemul ((long) tp.tv_sec, 10000000, &utc);
    uuid__uemul ((long) tp.tv_usec, 10, &usecs);
    if (!(((&usecs)->lo&0x80000000UL) ^ ((&utc)->lo&0x80000000UL))) { if (((&usecs)->lo&0x80000000UL)) { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi+1 ; } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; } } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; if (!((&utc)->lo&0x80000000UL)) (&utc)->hi++ ; };
    os_basetime_diff.lo = 0x13814000;
    os_basetime_diff.hi = 0x01B21DD2;
    if (!(((&utc)->lo&0x80000000UL) ^ ((&os_basetime_diff)->lo&0x80000000UL))) { if (((&utc)->lo&0x80000000UL)) { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi+1 ; } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; } } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; if (!((uuid_time)->lo&0x80000000UL)) (uuid_time)->hi++ ; };
}

canonical_original_code:
void uuid__get_os_time(struct uuid_time_t *uuid_time)
{
    struct timeval tp;
    struct unsigned64_t utc; struct unsigned64_t usecs; struct unsigned64_t os_basetime_diff;
    if (gettimeofday (&tp, (struct timezone *) 0))
    {
        perror ("uuid__get_os_time");
        exit (-1);
    }
    uuid__uemul ((long) tp.tv_sec, 10000000, &utc);
    uuid__uemul ((long) tp.tv_usec, 10, &usecs);
    if (!(((&usecs)->lo&0x80000000UL) ^ ((&utc)->lo&0x80000000UL))) { if (((&usecs)->lo&0x80000000UL)) { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi+1 ; } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; } } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; if (!((&utc)->lo&0x80000000UL)) (&utc)->hi++ ; };
    os_basetime_diff.lo = 0x13814000;
    os_basetime_diff.hi = 0x01B21DD2;
    if (!(((&utc)->lo&0x80000000UL) ^ ((&os_basetime_diff)->lo&0x80000000UL))) { if (((&utc)->lo&0x80000000UL)) { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi+1 ; } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; } } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; if (!((uuid_time)->lo&0x80000000UL)) (uuid_time)->hi++ ; };
}

variable_types: struct uuid_time_t { unsigned int lo; unsigned int hi; } *

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

variable_types: struct unsigned64_t { unsigned int lo; unsigned int hi; }

user_defined_types: struct timezone { int tz_minuteswest; int tz_dsttime; }


decompiled_code:
// attributes: thunk
__pid_t uuid__get_os_pid(void)
{
return getpid();
}

canonical_decompiled_code:
__pid_t func1(void)
{
return getpid();
}

original_code:
unsigned32 uuid__get_os_pid ( void )
{
    return ((unsigned32) getpid());
}

canonical_original_code:
unsigned int uuid__get_os_pid()
{
    return ((unsigned int) getpid());
}

return_type: unsigned int