parseOptions

decompiled_code:
__int64 __fastcall parseOptions(int argc, char **argv)
{
unsigned int v2; // er13
int v3; // er14
int v4; // eax
char *v5; // r12
FILE *v6; // rax
const char *v7; // r15
bool v8; // zf
unsigned int v9; // er12
double v10; // r15
double v11; // r15
int v12; // eax
int v13; // er15
int v14; // eax
int v15; // er15
int v16; // eax
int v17; // er15
double v18; // r15
double v19; // r15
double v20; // r13
int v21; // eax
int v22; // er15
int v23; // eax
int v24; // er14
int v26; // [rsp+Ch] [rbp-6Ch]
int v27; // [rsp+10h] [rbp-68h]
int v28; // [rsp+14h] [rbp-64h]
int v29; // [rsp+18h] [rbp-60h]
int v30; // [rsp+1Ch] [rbp-5Ch]
int v31; // [rsp+20h] [rbp-58h]
int v32; // [rsp+24h] [rbp-54h]
int v33; // [rsp+28h] [rbp-50h]
int v34; // [rsp+2Ch] [rbp-4Ch]
int longind[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

*((_QWORD *)&global + 10) = stdout;
*(&global + 18) = 1;
*((_QWORD *)&global + 5) = LC0;
*((_QWORD *)&global + 6) = LC1;
*((_QWORD *)&global + 7) = 0LL;
*((_QWORD *)&global + 8) = LC3;
*(&global + 6) = 0;
*((_QWORD *)&global + 4) = LC4;
*((_QWORD *)&global + 1) = LC4;
*((_QWORD *)&global + 2) = LC5;
global = 100;
*(&global + 19) = 0;
v34 = 0;
v2 = 0;
v31 = 0;
v32 = 0;
v33 = 0;
v30 = 0;
v29 = 0;
v28 = 0;
v27 = 0;
v3 = 0;
v26 = 0;
while ( 1 )
{
longind[0] = 0;
v4 = getopt_long(argc, argv, "ho:n:", &long_options_0, longind);
if ( v4 == -1 )
break;
if ( v4 == 104 )
return (unsigned int)-1;
if ( v4 <= 104 )
{
if ( v4 )
{
if ( v4 != 63 )
goto LABEL_64;
}
else if ( !*((_QWORD *)&long_options_0.flag + 4 * longind[0]) )
{
v7 = (const char *)*((_QWORD *)&long_options_0.name + 4 * longind[0]);
v8 = strcmp(v7, "fat") == 0;
v9 = !v8;
if ( v8 )
{
v10 = atof(optarg);
if ( v10 < 0.0 || v10 > 1.0 )
{
fprintf(stderr, "Error: invalid fat value: %f\n", v10);
v9 = -1;
}
*((double *)&global + 1) = v10;
v27 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "density") != 0;
if ( !v9 )
{
v11 = atof(optarg);
if ( v11 < 0.0 || v11 > 1.0 )
{
fprintf(stderr, "Error: invalid density value: %f\n", v11);
v9 = -1;
}
*((double *)&global + 4) = v11;
v28 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "ccr") != 0;
if ( !v9 )
{
v12 = atoi(optarg);
v13 = v12;
if ( v12 < 0 )
{
fprintf(stderr, "Error: invalid ccr value: %d\n", (unsigned int)v12);
v9 = -1;
}
*(&global + 6) = v13;
v29 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "mindata") != 0;
if ( !v9 )
{
v14 = atoi(optarg);
v15 = v14;
if ( v14 <= 0 )
{
fprintf(stderr, "Error: invalid mindata value: %d\n", (unsigned int)v14);
v9 = -1;
}
*((double *)&global + 5) = (double)v15;
v30 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "maxdata") != 0;
if ( !v9 )
{
v16 = atoi(optarg);
v17 = v16;
if ( v16 <= 0 || *((double *)&global + 5) > (double)v16 )
{
fprintf(stderr, "Error: invalid maxdata value: %d\n", (unsigned int)v16);
v9 = -1;
}
*((double *)&global + 6) = (double)v17;
v33 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "minalpha") != 0;
if ( !v9 )
{
v18 = atof(optarg);
if ( v18 < 0.0 )
{
fprintf(stderr, "Error: invalid minalpha value: %f\n", v18);
v9 = -1;
}
*((double *)&global + 7) = v18;
v32 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "maxalpha") != 0;
if ( !v9 )
{
v19 = atof(optarg);
if ( v19 < 0.0 || *((double *)&global + 7) > v19 )
{
fprintf(stderr, "Error: invalid maxalpha value: %f\n", v19);
v9 = -1;
}
*((double *)&global + 8) = v19;
v31 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "regular") != 0;
if ( !v9 )
{
v20 = atof(optarg);
if ( v20 < 0.0 || v20 > 1.0 )
{
fprintf(stderr, "Error: invalid regular value: %f\n", v20);
v9 = -1;
}
*((double *)&global + 2) = v20;
v2 = 1;
goto LABEL_59;
}
v8 = strcmp(*(&long_options_0.name + 4 * longind[0]), "jump") == 0;
v9 = !v8;
if ( v8 )
{
v21 = atoi(optarg);
v22 = v21;
if ( v21 < 0 )
{
fprintf(stderr, "Error: invalud jump value: %d\n", (unsigned int)v21);
v9 = -1;
}
*(&global + 18) = v22;
v34 = 1;
goto LABEL_59;
}
if ( !strcmp(*(&long_options_0.name + 4 * longind[0]), "dot") )
*(&global + 19) = 1;
else
fprintf(stderr, "unknown switch: %s\n", v7);
}
}
else if ( v4 == 110 )
{
v23 = atoi(optarg);
v24 = v23;
v9 = 0;
if ( v23 <= 0 )
{
fprintf(stderr, "Error: invalid n value: %d\n", (unsigned int)v23);
v9 = -1;
}
global = v24;
v3 = 1;
LABEL_59:
if ( v9 )
return v9;
}
else
{
if ( v4 != 111 )
LABEL_64:
abort();
v5 = optarg;
v6 = fopen(optarg, "w");
*((_QWORD *)&global + 10) = v6;
if ( !v6 )
{
fprintf(stderr, "Error: Cannot open file '%s' for output\n", v5);
return (unsigned int)-1;
}
v26 = 1;
}
}
if ( !v3 )
fprintf(stderr, "Warning: using default n value (%d)\n", (unsigned int)global);
if ( !v26 )
fwrite("Warning: Sending output to stdout\n", 1uLL, 0x22uLL, stderr);
if ( !v34 )
fprintf(stderr, "Warning: using default jump value (%d)\n", (unsigned int)*(&global + 18));
if ( !v27 )
fprintf(stderr, "Warning: using default fat value (%g)\n", *((double *)&global + 1));
if ( !v28 )
fprintf(stderr, "Warning: using default density value (%g)\n", *((double *)&global + 4));
if ( !v29 )
fprintf(stderr, "Warning: using default ccr value (%d)\n", (unsigned int)*(&global + 6));
if ( !v30 )
fprintf(stderr, "Warning: using default mindata value (%g)\n", *((double *)&global + 5));
if ( !v33 )
fprintf(stderr, "Warning: using default maxdata value (%g)\n", *((double *)&global + 6));
if ( !v32 )
fprintf(stderr, "Warning: using default minalpha value (%g)\n", *((double *)&global + 7));
if ( !v31 )
fprintf(stderr, "Warning: using default maxalpha value (%g)\n", *((double *)&global + 8));
if ( v2 )
return 0;
fprintf(stderr, "Warning: using default regular value (%g)\n", *((double *)&global + 2));
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int argc, char **argv)
{
unsigned int v2; // er13
int v3; // er14
int v4; // eax
char *v5; // r12
FILE *v6; // rax
const char *v7; // r15
bool v8; // zf
unsigned int v9; // er12
double v10; // r15
double v11; // r15
int v12; // eax
int v13; // er15
int v14; // eax
int v15; // er15
int v16; // eax
int v17; // er15
double v18; // r15
double v19; // r15
double v20; // r13
int v21; // eax
int v22; // er15
int v23; // eax
int v24; // er14
int v26; // [rsp+Ch] [rbp-6Ch]
int v27; // [rsp+10h] [rbp-68h]
int v28; // [rsp+14h] [rbp-64h]
int v29; // [rsp+18h] [rbp-60h]
int v30; // [rsp+1Ch] [rbp-5Ch]
int v31; // [rsp+20h] [rbp-58h]
int v32; // [rsp+24h] [rbp-54h]
int v33; // [rsp+28h] [rbp-50h]
int v34; // [rsp+2Ch] [rbp-4Ch]
int longind[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

*((_QWORD *)&global + 10) = stdout;
*(&global + 18) = 1;
*((_QWORD *)&global + 5) = LC0;
*((_QWORD *)&global + 6) = LC1;
*((_QWORD *)&global + 7) = 0LL;
*((_QWORD *)&global + 8) = LC3;
*(&global + 6) = 0;
*((_QWORD *)&global + 4) = LC4;
*((_QWORD *)&global + 1) = LC4;
*((_QWORD *)&global + 2) = LC5;
global = 100;
*(&global + 19) = 0;
v34 = 0;
v2 = 0;
v31 = 0;
v32 = 0;
v33 = 0;
v30 = 0;
v29 = 0;
v28 = 0;
v27 = 0;
v3 = 0;
v26 = 0;
while ( 1 )
{
longind[0] = 0;
v4 = getopt_long(argc, argv, "ho:n:", &long_options_0, longind);
if ( v4 == -1 )
break;
if ( v4 == 104 )
return (unsigned int)-1;
if ( v4 <= 104 )
{
if ( v4 )
{
if ( v4 != 63 )
goto LABEL_64;
}
else if ( !*((_QWORD *)&long_options_0.flag + 4 * longind[0]) )
{
v7 = (const char *)*((_QWORD *)&long_options_0.name + 4 * longind[0]);
v8 = strcmp(v7, "fat") == 0;
v9 = !v8;
if ( v8 )
{
v10 = atof(optarg);
if ( v10 < 0.0 || v10 > 1.0 )
{
fprintf(stderr, "Error: invalid fat value: %f\n", v10);
v9 = -1;
}
*((double *)&global + 1) = v10;
v27 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "density") != 0;
if ( !v9 )
{
v11 = atof(optarg);
if ( v11 < 0.0 || v11 > 1.0 )
{
fprintf(stderr, "Error: invalid density value: %f\n", v11);
v9 = -1;
}
*((double *)&global + 4) = v11;
v28 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "ccr") != 0;
if ( !v9 )
{
v12 = atoi(optarg);
v13 = v12;
if ( v12 < 0 )
{
fprintf(stderr, "Error: invalid ccr value: %d\n", (unsigned int)v12);
v9 = -1;
}
*(&global + 6) = v13;
v29 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "mindata") != 0;
if ( !v9 )
{
v14 = atoi(optarg);
v15 = v14;
if ( v14 <= 0 )
{
fprintf(stderr, "Error: invalid mindata value: %d\n", (unsigned int)v14);
v9 = -1;
}
*((double *)&global + 5) = (double)v15;
v30 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "maxdata") != 0;
if ( !v9 )
{
v16 = atoi(optarg);
v17 = v16;
if ( v16 <= 0 || *((double *)&global + 5) > (double)v16 )
{
fprintf(stderr, "Error: invalid maxdata value: %d\n", (unsigned int)v16);
v9 = -1;
}
*((double *)&global + 6) = (double)v17;
v33 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "minalpha") != 0;
if ( !v9 )
{
v18 = atof(optarg);
if ( v18 < 0.0 )
{
fprintf(stderr, "Error: invalid minalpha value: %f\n", v18);
v9 = -1;
}
*((double *)&global + 7) = v18;
v32 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "maxalpha") != 0;
if ( !v9 )
{
v19 = atof(optarg);
if ( v19 < 0.0 || *((double *)&global + 7) > v19 )
{
fprintf(stderr, "Error: invalid maxalpha value: %f\n", v19);
v9 = -1;
}
*((double *)&global + 8) = v19;
v31 = 1;
goto LABEL_59;
}
v9 = strcmp(*(&long_options_0.name + 4 * longind[0]), "regular") != 0;
if ( !v9 )
{
v20 = atof(optarg);
if ( v20 < 0.0 || v20 > 1.0 )
{
fprintf(stderr, "Error: invalid regular value: %f\n", v20);
v9 = -1;
}
*((double *)&global + 2) = v20;
v2 = 1;
goto LABEL_59;
}
v8 = strcmp(*(&long_options_0.name + 4 * longind[0]), "jump") == 0;
v9 = !v8;
if ( v8 )
{
v21 = atoi(optarg);
v22 = v21;
if ( v21 < 0 )
{
fprintf(stderr, "Error: invalud jump value: %d\n", (unsigned int)v21);
v9 = -1;
}
*(&global + 18) = v22;
v34 = 1;
goto LABEL_59;
}
if ( !strcmp(*(&long_options_0.name + 4 * longind[0]), "dot") )
*(&global + 19) = 1;
else
fprintf(stderr, "unknown switch: %s\n", v7);
}
}
else if ( v4 == 110 )
{
v23 = atoi(optarg);
v24 = v23;
v9 = 0;
if ( v23 <= 0 )
{
fprintf(stderr, "Error: invalid n value: %d\n", (unsigned int)v23);
v9 = -1;
}
global = v24;
v3 = 1;
LABEL_59:
if ( v9 )
return v9;
}
else
{
if ( v4 != 111 )
LABEL_64:
abort();
v5 = optarg;
v6 = fopen(optarg, "w");
*((_QWORD *)&global + 10) = v6;
if ( !v6 )
{
fprintf(stderr, "Error: Cannot open file '%s' for output\n", v5);
return (unsigned int)-1;
}
v26 = 1;
}
}
if ( !v3 )
fprintf(stderr, "Warning: using default n value (%d)\n", (unsigned int)global);
if ( !v26 )
fwrite("Warning: Sending output to stdout\n", 1uLL, 0x22uLL, stderr);
if ( !v34 )
fprintf(stderr, "Warning: using default jump value (%d)\n", (unsigned int)*(&global + 18));
if ( !v27 )
fprintf(stderr, "Warning: using default fat value (%g)\n", *((double *)&global + 1));
if ( !v28 )
fprintf(stderr, "Warning: using default density value (%g)\n", *((double *)&global + 4));
if ( !v29 )
fprintf(stderr, "Warning: using default ccr value (%d)\n", (unsigned int)*(&global + 6));
if ( !v30 )
fprintf(stderr, "Warning: using default mindata value (%g)\n", *((double *)&global + 5));
if ( !v33 )
fprintf(stderr, "Warning: using default maxdata value (%g)\n", *((double *)&global + 6));
if ( !v32 )
fprintf(stderr, "Warning: using default minalpha value (%g)\n", *((double *)&global + 7));
if ( !v31 )
fprintf(stderr, "Warning: using default maxalpha value (%g)\n", *((double *)&global + 8));
if ( v2 )
return 0;
fprintf(stderr, "Warning: using default regular value (%g)\n", *((double *)&global + 2));
return v2;
}

original_code:
int parseOptions(int argc, char *const *argv) {
  int ret_val = 0;
  int c;
  int oflag = 0;
  int nflag = 0;
  int fat_flag = 0;
  int density_flag = 0;
  int ccr_flag = 0;
  int mindata_flag = 0;
  int maxdata_flag = 0;
  int minalpha_flag = 0;
  int maxalpha_flag = 0;
  int regular_flag = 0;
  int jump_flag = 0;
  int dot_flag = 0;
  global.output_file=stdout;
  global.jump=1;
  global.mindata=2048;
  global.maxdata=11264;
  global.minalpha=0.0;
  global.maxalpha=0.2;
  global.ccr=0;
  global.density=0.5;
  global.fat=0.5;
  global.regular=0.9;
  global.n=100;
  global.dot_output=0;
  while (ret_val == 0) {
    static struct option long_options[] = {
        { "fat", 1, 0, 0 },
        { "density", 1, 0, 0 },
        { "ccr", 1, 0, 0 },
        { "mindata", 1, 0, 0 },
        { "maxdata", 1, 0, 0 },
        { "minalpha", 1, 0, 0 },
        { "maxalpha", 1, 0, 0 },
        { "regular", 1, 0, 0 },
        { "jump", 1, 0, 0 },
        { "dot", 0, 0, 0},
        { 0, 0, 0, 0 } };
    int option_index = 0;
    c = getopt_long(argc, argv, "ho:n:", long_options, &option_index);
    if (c == -1)
      break;
    switch (c) {
    case 0:
      if (long_options[option_index].flag != 0)
        break;
      const char *optname = long_options[option_index].name;
      if (!strcmp(optname, "fat")) {
        double fat=atof(optarg);
        fat_flag = 1;
        if ((fat < 0.0) || (fat > 1.0)) {
          fprintf(stderr,"Error: invalid fat value: %f\n",fat);
          ret_val = -1;
        }
        global.fat=fat;
      } else if (!strcmp(optname, "density")) {
        double density=atof(optarg);
        density_flag = 1;
        if ((density < 0.0) || (density > 1.0)) {
          fprintf(stderr,"Error: invalid density value: %f\n",density);
          ret_val = -1;
        }
        global.density=density;
      } else if (!strcmp(optname, "ccr")) {
        int ccr=atoi(optarg);
        ccr_flag = 1;
        if (ccr < 0) {
          fprintf(stderr,"Error: invalid ccr value: %d\n",ccr);
          ret_val = -1;
        }
        global.ccr=ccr;
      } else if (!strcmp(optname, "mindata")) {
        int mindata=atoi(optarg);
        mindata_flag = 1;
        if (mindata <= 0) {
          fprintf(stderr,"Error: invalid mindata value: %d\n",mindata);
          ret_val = -1;
        }
        global.mindata=mindata;
      } else if (!strcmp(optname, "maxdata")) {
        int maxdata=atoi(optarg);
        maxdata_flag = 1;
        if ((maxdata <= 0) || (maxdata < global.mindata)) {
          fprintf(stderr,"Error: invalid maxdata value: %d\n",maxdata);
          ret_val = -1;
        }
        global.maxdata=maxdata;
      } else if (!strcmp(optname, "minalpha")) {
        double minalpha=atof(optarg);
        minalpha_flag = 1;
        if (minalpha < 0.0) {
          fprintf(stderr,"Error: invalid minalpha value: %f\n",minalpha);
          ret_val = -1;
        }
        global.minalpha=minalpha;
      } else if (!strcmp(optname, "maxalpha")) {
        double maxalpha=atof(optarg);
        maxalpha_flag = 1;
        if ((maxalpha < 0.0) || (maxalpha < global.minalpha)) {
          fprintf(stderr,"Error: invalid maxalpha value: %f\n",maxalpha);
          ret_val = -1;
        }
        global.maxalpha=maxalpha;
      } else if (!strcmp(optname, "regular")) {
        double regular=atof(optarg);
        regular_flag = 1;
        if ((regular < 0.0) || (regular > 1.0)) {
          fprintf(stderr,"Error: invalid regular value: %f\n",regular);
          ret_val = -1;
        }
        global.regular=regular;
      } else if (!strcmp(optname, "jump")) {
        int jump = atoi(optarg);
        jump_flag = 1;
        if (jump < 0) {
          fprintf(stderr,"Error: invalud jump value: %d\n",jump);
          ret_val = -1;
        }
        global.jump=jump;
      } else if (!strcmp(optname, "dot")) {
        dot_flag = 1;
        global.dot_output=1;
      } else {
        fprintf(stderr, "unknown switch: %s\n", optname);
      }
      break;
    case 'n': {
      int n=atoi(optarg);
      nflag = 1;
      if (n <= 0) {
        fprintf(stderr,"Error: invalid n value: %d\n", n);
        ret_val = -1;
      }
      global.n=n;
    }
    break;
    case 'o':
      oflag = 1;
      const char *filename = optarg;
      if ((global.output_file=fopen(filename, "w")) == ((void *)0)) {
        fprintf(stderr,"Error: Cannot open file '%s' for output\n", filename);
        ret_val = -1;
      }
      break;
    case 'h':
      ret_val = -1;
      break;
    case '?':
      break;
    default:
      abort();
      break;
    }
  }
  if( ret_val == 0 ) {
    if (!nflag) {
      fprintf(stderr,"Warning: using default n value (%d)\n", global.n);
    }
    if (!oflag) {
      fprintf(stderr,"Warning: Sending output to stdout\n");
    }
    if (!jump_flag) {
      fprintf(stderr,"Warning: using default jump value (%d)\n", global.jump);
    }
    if (!fat_flag) {
      fprintf(stderr,"Warning: using default fat value (%g)\n", global.fat);
    }
    if (!density_flag) {
      fprintf(stderr,"Warning: using default density value (%g)\n",
          global.density);
    }
    if (!ccr_flag) {
      fprintf(stderr,"Warning: using default ccr value (%d)\n", global.ccr);
    }
    if (!mindata_flag) {
      fprintf(stderr,"Warning: using default mindata value (%g)\n",
          global.mindata);
    }
    if (!maxdata_flag) {
      fprintf(stderr,"Warning: using default maxdata value (%g)\n",
          global.maxdata);
    }
    if (!minalpha_flag) {
      fprintf(stderr,"Warning: using default minalpha value (%g)\n",
          global.minalpha);
    }
    if (!maxalpha_flag) {
      fprintf(stderr,"Warning: using default maxalpha value (%g)\n",
          global.maxalpha);
    }
    if (!regular_flag) {
      fprintf(stderr,"Warning: using default regular value (%g)\n",
          global.regular);
    }
  }
  return ret_val;
}

canonical_original_code:
int parseOptions(int argc, char **argv) {
  int ret_val = 0;
  int c;
  int oflag = 0;
  int nflag = 0;
  int fat_flag = 0;
  int density_flag = 0;
  int ccr_flag = 0;
  int mindata_flag = 0;
  int maxdata_flag = 0;
  int minalpha_flag = 0;
  int maxalpha_flag = 0;
  int regular_flag = 0;
  int jump_flag = 0;
  int dot_flag = 0;
  global.output_file=stdout;
  global.jump=1;
  global.mindata=2048;
  global.maxdata=11264;
  global.minalpha=0.0;
  global.maxalpha=0.2;
  global.ccr=0;
  global.density=0.5;
  global.fat=0.5;
  global.regular=0.9;
  global.n=100;
  global.dot_output=0;
  while (ret_val == 0) {
    static struct option long_options[] = {
        { "fat", 1, 0, 0 },
        { "density", 1, 0, 0 },
        { "ccr", 1, 0, 0 },
        { "mindata", 1, 0, 0 },
        { "maxdata", 1, 0, 0 },
        { "minalpha", 1, 0, 0 },
        { "maxalpha", 1, 0, 0 },
        { "regular", 1, 0, 0 },
        { "jump", 1, 0, 0 },
        { "dot", 0, 0, 0},
        { 0, 0, 0, 0 } };
    int option_index = 0;
    c = getopt_long(argc, argv, "ho:n:", long_options, &option_index);
    if (c == -1)
      break;
    switch (c) {
    case 0:
      if (long_options[option_index].flag != 0)
        break;
      char *optname = long_options[option_index].name;
      if (!strcmp(optname, "fat")) {
        double fat = atof(optarg);
        fat_flag = 1;
        if ((fat < 0.0) || (fat > 1.0)) {
          fprintf(stderr,"Error: invalid fat value: %f\n",fat);
          ret_val = -1;
        }
        global.fat=fat;
      } else if (!strcmp(optname, "density")) {
        double density = atof(optarg);
        density_flag = 1;
        if ((density < 0.0) || (density > 1.0)) {
          fprintf(stderr,"Error: invalid density value: %f\n",density);
          ret_val = -1;
        }
        global.density=density;
      } else if (!strcmp(optname, "ccr")) {
        int ccr = atoi(optarg);
        ccr_flag = 1;
        if (ccr < 0) {
          fprintf(stderr,"Error: invalid ccr value: %d\n",ccr);
          ret_val = -1;
        }
        global.ccr=ccr;
      } else if (!strcmp(optname, "mindata")) {
        int mindata = atoi(optarg);
        mindata_flag = 1;
        if (mindata <= 0) {
          fprintf(stderr,"Error: invalid mindata value: %d\n",mindata);
          ret_val = -1;
        }
        global.mindata=mindata;
      } else if (!strcmp(optname, "maxdata")) {
        int maxdata = atoi(optarg);
        maxdata_flag = 1;
        if ((maxdata <= 0) || (maxdata < global.mindata)) {
          fprintf(stderr,"Error: invalid maxdata value: %d\n",maxdata);
          ret_val = -1;
        }
        global.maxdata=maxdata;
      } else if (!strcmp(optname, "minalpha")) {
        double minalpha = atof(optarg);
        minalpha_flag = 1;
        if (minalpha < 0.0) {
          fprintf(stderr,"Error: invalid minalpha value: %f\n",minalpha);
          ret_val = -1;
        }
        global.minalpha=minalpha;
      } else if (!strcmp(optname, "maxalpha")) {
        double maxalpha = atof(optarg);
        maxalpha_flag = 1;
        if ((maxalpha < 0.0) || (maxalpha < global.minalpha)) {
          fprintf(stderr,"Error: invalid maxalpha value: %f\n",maxalpha);
          ret_val = -1;
        }
        global.maxalpha=maxalpha;
      } else if (!strcmp(optname, "regular")) {
        double regular = atof(optarg);
        regular_flag = 1;
        if ((regular < 0.0) || (regular > 1.0)) {
          fprintf(stderr,"Error: invalid regular value: %f\n",regular);
          ret_val = -1;
        }
        global.regular=regular;
      } else if (!strcmp(optname, "jump")) {
        int jump = atoi(optarg);
        jump_flag = 1;
        if (jump < 0) {
          fprintf(stderr,"Error: invalud jump value: %d\n",jump);
          ret_val = -1;
        }
        global.jump=jump;
      } else if (!strcmp(optname, "dot")) {
        dot_flag = 1;
        global.dot_output=1;
      } else {
        fprintf(stderr, "unknown switch: %s\n", optname);
      }
      break;
    case 'n': {
      int n = atoi(optarg);
      nflag = 1;
      if (n <= 0) {
        fprintf(stderr,"Error: invalid n value: %d\n", n);
        ret_val = -1;
      }
      global.n=n;
    }
    break;
    case 'o':
      oflag = 1;
      char *filename = optarg;
      if ((global.output_file=fopen(filename, "w")) == ((void *)0)) {
        fprintf(stderr,"Error: Cannot open file '%s' for output\n", filename);
        ret_val = -1;
      }
      break;
    case 'h':
      ret_val = -1;
      break;
    case '?':
      break;
    default:
      abort();
      break;
    }
  }
  if( ret_val == 0 ) {
    if (!nflag) {
      fprintf(stderr,"Warning: using default n value (%d)\n", global.n);
    }
    if (!oflag) {
      fprintf(stderr,"Warning: Sending output to stdout\n");
    }
    if (!jump_flag) {
      fprintf(stderr,"Warning: using default jump value (%d)\n", global.jump);
    }
    if (!fat_flag) {
      fprintf(stderr,"Warning: using default fat value (%g)\n", global.fat);
    }
    if (!density_flag) {
      fprintf(stderr,"Warning: using default density value (%g)\n",
          global.density);
    }
    if (!ccr_flag) {
      fprintf(stderr,"Warning: using default ccr value (%d)\n", global.ccr);
    }
    if (!mindata_flag) {
      fprintf(stderr,"Warning: using default mindata value (%g)\n",
          global.mindata);
    }
    if (!maxdata_flag) {
      fprintf(stderr,"Warning: using default maxdata value (%g)\n",
          global.maxdata);
    }
    if (!minalpha_flag) {
      fprintf(stderr,"Warning: using default minalpha value (%g)\n",
          global.minalpha);
    }
    if (!maxalpha_flag) {
      fprintf(stderr,"Warning: using default maxalpha value (%g)\n",
          global.maxalpha);
    }
    if (!regular_flag) {
      fprintf(stderr,"Warning: using default regular value (%g)\n",
          global.regular);
    }
  }
  return ret_val;
}

variable_types: c int

variable_types: oflag int

variable_types: nflag int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types:
optname
char *

variable_types: fat double

variable_types: density double

variable_types: ccr int

variable_types: mindata int

variable_types: maxdata int

variable_types: minalpha double

variable_types: maxalpha double

variable_types: regular double

variable_types: jump int

variable_types: n int

variable_types:
filename
char *


printUsage

decompiled_code:
size_t printUsage()
{
fwrite(
"daggen [options] [-o <output file>]\n"
"\t -n <number of tasks>\n"
"\t --mindata <minimum data size>\n"
"\t --maxdata <maximum data size>\n"
"\t --minalpha <minimum Amdahl's law parameter value>\n"
"\t --maxalpha <maximum Amdahl's law parameter value>\n",
1uLL,
0xE3uLL,
stderr);
return fwrite(
"\t --fat <dag shape>\n"
"\t\t fat=1.0: fat (maximum parallelism)\n"
"\t\t fat=0.0: thin (minimum parallelism)\n"
"\t --density <density>\n"
"\t density=0.0: minimum number of dependencies\n"
"\t density=1.0: full graph\n"
"\t --regular <regularity for num tasks per level>\n"
"\t regular= 1.0: perfectly regular\n"
"\t regular= 0.0: irregular\n"
"\t --ccr <communication(MBytes) to computation(sec) ratio>\n"
"\t --jump <number of levels spanned by communications>\n"
"\t jump=1: perfectly synchronized levels\n"
"\t --dot: output generated DAG in the DOT format\n"
"\n",
1uLL,
0x204uLL,
stderr);
}

canonical_decompiled_code:
size_t func1()
{
fwrite(
"daggen [options] [-o <output file>]\n"
"\t -n <number of tasks>\n"
"\t --mindata <minimum data size>\n"
"\t --maxdata <maximum data size>\n"
"\t --minalpha <minimum Amdahl's law parameter value>\n"
"\t --maxalpha <maximum Amdahl's law parameter value>\n",
1uLL,
0xE3uLL,
stderr);
return fwrite(
"\t --fat <dag shape>\n"
"\t\t fat=1.0: fat (maximum parallelism)\n"
"\t\t fat=0.0: thin (minimum parallelism)\n"
"\t --density <density>\n"
"\t density=0.0: minimum number of dependencies\n"
"\t density=1.0: full graph\n"
"\t --regular <regularity for num tasks per level>\n"
"\t regular= 1.0: perfectly regular\n"
"\t regular= 0.0: irregular\n"
"\t --ccr <communication(MBytes) to computation(sec) ratio>\n"
"\t --jump <number of levels spanned by communications>\n"
"\t jump=1: perfectly synchronized levels\n"
"\t --dot: output generated DAG in the DOT format\n"
"\n",
1uLL,
0x204uLL,
stderr);
}

original_code:
void printUsage(void)
{
  fprintf(stderr,"daggen [options] [-o <output file>]\n"
      "\t -n <number of tasks>\n"
      "\t --mindata <minimum data size>\n"
      "\t --maxdata <maximum data size>\n"
      "\t --minalpha <minimum Amdahl's law parameter value>\n"
      "\t --maxalpha <maximum Amdahl's law parameter value>\n");
  fprintf(stderr,
      "\t --fat <dag shape>\n"
      "\t	 fat=1.0: fat (maximum parallelism)\n"
      "\t	 fat=0.0: thin (minimum parallelism)\n"
      "\t --density <density>\n"
      "\t    density=0.0: minimum number of dependencies\n"
      "\t    density=1.0: full graph\n"
      "\t --regular <regularity for num tasks per level>\n"
      "\t    regular= 1.0: perfectly regular\n"
      "\t    regular= 0.0: irregular\n"
      "\t --ccr <communication(MBytes) to computation(sec) ratio>\n"
      "\t --jump <number of levels spanned by communications>\n"
      "\t    jump=1: perfectly synchronized levels\n"
      "\t --dot: output generated DAG in the DOT format\n"
      "\n");
  return;
}

canonical_original_code:
void printUsage()
{
  fprintf(stderr,"daggen [options] [-o <output file>]\n"
      "\t -n <number of tasks>\n"
      "\t --mindata <minimum data size>\n"
      "\t --maxdata <maximum data size>\n"
      "\t --minalpha <minimum Amdahl's law parameter value>\n"
      "\t --maxalpha <maximum Amdahl's law parameter value>\n");
  fprintf(stderr,
      "\t --fat <dag shape>\n"
      "\t	 fat=1.0: fat (maximum parallelism)\n"
      "\t	 fat=0.0: thin (minimum parallelism)\n"
      "\t --density <density>\n"
      "\t    density=0.0: minimum number of dependencies\n"
      "\t    density=1.0: full graph\n"
      "\t --regular <regularity for num tasks per level>\n"
      "\t    regular= 1.0: perfectly regular\n"
      "\t    regular= 0.0: irregular\n"
      "\t --ccr <communication(MBytes) to computation(sec) ratio>\n"
      "\t --jump <number of levels spanned by communications>\n"
      "\t    jump=1: perfectly synchronized levels\n"
      "\t --dot: output generated DAG in the DOT format\n"
      "\n");
  return;
}

return_type: void


outputDAG

original_code:
void outputDAG(DAG dag) {
  int i, j, k;
  int node_count=1;
  for (i=0; i<dag->nb_levels; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      dag->levels[i][j]->tag = node_count++;
    }
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      for (k=0; k<dag->levels[i][j]->nb_children; k++) {
        dag->levels[i][j]->transfer_tags[k] = node_count++;
      }
    }
  }
  fprintf((global.output_file),"NODE_COUNT %d\n",node_count+1);
  fprintf((global.output_file),"NODE 0 ");
  for (i=0; i<dag->nb_tasks_per_level[0]-1; i++) {
    fprintf((global.output_file),"%d,",dag->levels[0][i]->tag);
  }
  if (dag->nb_tasks_per_level[0])
    fprintf((global.output_file),"%d ROOT 0.0 0.0\n",dag->levels[0][i]->tag);
  else
    fprintf((global.output_file),"%d ROOT 0.0 0.0\n",node_count);
  for (i=0; i<dag->nb_levels-1; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      fprintf((global.output_file),"NODE %d ",dag->levels[i][j]->tag);
      for (k=0; k<dag->levels[i][j]->nb_children-1; k++) {
        fprintf((global.output_file),"%d,",dag->levels[i][j]->transfer_tags[k]);
      }
      if (dag->levels[i][j]->nb_children) {
        fprintf((global.output_file),"%d COMPUTATION %.0f %.2f\n",
            dag->levels[i][j]->transfer_tags[k],
            dag->levels[i][j]->cost,
            dag->levels[i][j]->alpha);
      } else {
        fprintf((global.output_file),"%d COMPUTATION %.0f %.2f\n",
            node_count,
            dag->levels[i][j]->cost,
            dag->levels[i][j]->alpha);
      }
      for (k=0; k<dag->levels[i][j]->nb_children; k++) {
        fprintf((global.output_file),"NODE %d ",dag->levels[i][j]->transfer_tags[k]);
        fprintf((global.output_file),"%d TRANSFER %.0f 0.0\n",
            dag->levels[i][j]->children[k]->tag,
            dag->levels[i][j]->comm_costs[k]);
      }
    }
  }
  for (j=0; j<dag->nb_tasks_per_level[dag->nb_levels-1]; j++) {
    fprintf((global.output_file),"NODE %d %d COMPUTATION %.0f %.2f\n",
        dag->levels[dag->nb_levels-1][j]->tag,
        node_count,
        dag->levels[dag->nb_levels-1][j]->cost,
        dag->levels[dag->nb_levels-1][j]->alpha);
  }
  fprintf((global.output_file),"NODE %d - END 0.0 0.0\n",node_count);
}

canonical_original_code:
void outputDAG(struct _DAG *dag) {
  int i; int j; int k;
  int node_count = 1;
  for (i=0; i<dag->nb_levels; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      dag->levels[i][j]->tag = node_count++;
    }
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      for (k=0; k<dag->levels[i][j]->nb_children; k++) {
        dag->levels[i][j]->transfer_tags[k] = node_count++;
      }
    }
  }
  fprintf((global.output_file),"NODE_COUNT %d\n",node_count+1);
  fprintf((global.output_file),"NODE 0 ");
  for (i=0; i<dag->nb_tasks_per_level[0]-1; i++) {
    fprintf((global.output_file),"%d,",dag->levels[0][i]->tag);
  }
  if (dag->nb_tasks_per_level[0])
    fprintf((global.output_file),"%d ROOT 0.0 0.0\n",dag->levels[0][i]->tag);
  else
    fprintf((global.output_file),"%d ROOT 0.0 0.0\n",node_count);
  for (i=0; i<dag->nb_levels-1; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      fprintf((global.output_file),"NODE %d ",dag->levels[i][j]->tag);
      for (k=0; k<dag->levels[i][j]->nb_children-1; k++) {
        fprintf((global.output_file),"%d,",dag->levels[i][j]->transfer_tags[k]);
      }
      if (dag->levels[i][j]->nb_children) {
        fprintf((global.output_file),"%d COMPUTATION %.0f %.2f\n",
            dag->levels[i][j]->transfer_tags[k],
            dag->levels[i][j]->cost,
            dag->levels[i][j]->alpha);
      } else {
        fprintf((global.output_file),"%d COMPUTATION %.0f %.2f\n",
            node_count,
            dag->levels[i][j]->cost,
            dag->levels[i][j]->alpha);
      }
      for (k=0; k<dag->levels[i][j]->nb_children; k++) {
        fprintf((global.output_file),"NODE %d ",dag->levels[i][j]->transfer_tags[k]);
        fprintf((global.output_file),"%d TRANSFER %.0f 0.0\n",
            dag->levels[i][j]->children[k]->tag,
            dag->levels[i][j]->comm_costs[k]);
      }
    }
  }
  for (j=0; j<dag->nb_tasks_per_level[dag->nb_levels-1]; j++) {
    fprintf((global.output_file),"NODE %d %d COMPUTATION %.0f %.2f\n",
        dag->levels[dag->nb_levels-1][j]->tag,
        node_count,
        dag->levels[dag->nb_levels-1][j]->cost,
        dag->levels[dag->nb_levels-1][j]->alpha);
  }
  fprintf((global.output_file),"NODE %d - END 0.0 0.0\n",node_count);
}

variable_types: struct _DAG { int nb_levels; int *nb_tasks_per_level; struct _Task { int tag; double cost; int data_size; double alpha; int nb_children; struct _Task **children; double *comm_costs; int *transfer_tags; enum complexity_t { MIXED=0, N_2=1, N_LOG_N=2, N_3=3 } complexity; } ***levels; } *

variable_types: i int

variable_types: j int

variable_types: k int

user_defined_types: struct _DAG { int nb_levels; int *nb_tasks_per_level; struct _Task ***levels; }

user_defined_types: struct _Task { int tag; double cost; int data_size; double alpha; int nb_children; struct _Task **children; double *comm_costs; int *transfer_tags; enum complexity_t complexity; }


outputDOT

original_code:
void outputDOT(DAG dag) {
  int i, j, k;
  int node_count=1;
  for (i=0; i<dag->nb_levels; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      dag->levels[i][j]->tag = node_count++;
    }
  }
  fprintf((global.output_file),"digraph G {\n");
  for (i=0; i<dag->nb_levels; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      fprintf((global.output_file),"  %d [size=\"%.0f\", alpha=\"%.2f\"]\n",
          dag->levels[i][j]->tag,
          dag->levels[i][j]->cost,
          dag->levels[i][j]->alpha);
      for (k=0; k<dag->levels[i][j]->nb_children; k++) {
        fprintf((global.output_file),"  %d -> %d [size =\"%.f\"]\n",
            dag->levels[i][j]->tag,
            dag->levels[i][j]->children[k]->tag,
            dag->levels[i][j]->comm_costs[k]);
      }
    }
  }
  fprintf((global.output_file),"}\n");
}

canonical_original_code:
void outputDOT(struct _DAG *dag) {
  int i; int j; int k;
  int node_count = 1;
  for (i=0; i<dag->nb_levels; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      dag->levels[i][j]->tag = node_count++;
    }
  }
  fprintf((global.output_file),"digraph G {\n");
  for (i=0; i<dag->nb_levels; i++) {
    for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
      fprintf((global.output_file),"  %d [size=\"%.0f\", alpha=\"%.2f\"]\n",
          dag->levels[i][j]->tag,
          dag->levels[i][j]->cost,
          dag->levels[i][j]->alpha);
      for (k=0; k<dag->levels[i][j]->nb_children; k++) {
        fprintf((global.output_file),"  %d -> %d [size =\"%.f\"]\n",
            dag->levels[i][j]->tag,
            dag->levels[i][j]->children[k]->tag,
            dag->levels[i][j]->comm_costs[k]);
      }
    }
  }
  fprintf((global.output_file),"}\n");
}

user_defined_types: enum complexity_t { MIXED=0, N_2=1, N_LOG_N=2, N_3=3 }


getIntRandomNumberAround

decompiled_code:
__int64 __fastcall getIntRandomNumberAround(int a1, double a2)
{
__int64 result; // rax

result = (unsigned int)(int)((((a2 + a2) * (double)rand() * 4.656612873077393e-10 - a2) / 100.0 + 1.0) * (double)a1);
if ( (int)result <= 0 )
result = 1LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, double a2)
{
__int64 result; // rax

result = (unsigned int)(int)((((a2 + a2) * (double)rand() * 4.656612873077393e-10 - a2) / 100.0 + 1.0) * (double)a1);
if ( (int)result <= 0 )
result = 1LL;
return result;
}

original_code:
int getIntRandomNumberAround(int x, double perc) {
  double r;
  int new_int;
  r = -perc + (2*perc*rand()/(2147483647 +1.0));
  new_int = ((1) > ((int)((double)x * (1.0 + r/100.00))) ? (1) : ((int)((double)x * (1.0 + r/100.00))));
  return new_int;
}

variable_types: x int

variable_types: perc double

variable_types: r double

return_type: int


getRandomNumberBetween

decompiled_code:
double __fastcall getRandomNumberBetween(double a1, double a2)
{
return (double)rand() * (a2 - a1) * 4.656612873077393e-10 + a1;
}

canonical_decompiled_code:
double __fastcall func5(double a1, double a2)
{
return (double)rand() * (a2 - a1) * 4.656612873077393e-10 + a1;
}

original_code:
double getRandomNumberBetween(double x, double y) {
  double r;
  r = x + (y-x)*rand()/(2147483647 +1.0);
  return r;
}

variable_types: x double

variable_types: y double

return_type: double


repo: frsyuki/pxe-pdhcp

decompiled_code:
__int64 __fastcall check_dhcp_packet(_BYTE *a1)
{
_BYTE *v1; // rbx
unsigned int v2; // esi
char *v3; // rax
char v4; // dl

v1 = a1 + 236;
if ( memcmp(a1 + 236, &unk_21C, 4uLL) )
return (unsigned int)-1;
v2 = 0;
if ( *a1 != 1 )
return (unsigned int)-1;
v3 = a1 + 240;
v4 = a1[240];
if ( v4 != -1 )
{
if ( v3 - v1 <= 1235 )
{
while ( 1 )
{
if ( v4 )
v3 += (unsigned __int8)v3[1] + 2;
else
++v3;
v4 = *v3;
if ( *v3 == -1 )
break;
if ( v3 - v1 > 1235 )
return (unsigned int)-1;
}
}
else
{
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
_BYTE *v1; // rbx
unsigned int v2; // esi
char *v3; // rax
char v4; // dl

v1 = a1 + 236;
if ( memcmp(a1 + 236, &unk_21C, 4uLL) )
return (unsigned int)-1;
v2 = 0;
if ( *a1 != 1 )
return (unsigned int)-1;
v3 = a1 + 240;
v4 = a1[240];
if ( v4 != -1 )
{
if ( v3 - v1 <= 1235 )
{
while ( 1 )
{
if ( v4 )
v3 += (unsigned __int8)v3[1] + 2;
else
++v3;
v4 = *v3;
if ( *v3 == -1 )
break;
if ( v3 - v1 > 1235 )
return (unsigned int)-1;
}
}
else
{
v2 = -1;
}
}
return v2;
}

original_code:
int check_dhcp_packet(struct dhcp_packet *p)
{
 u_char *op;
 if (memcmp(p->options, "\143\202\123\143", 4))
  return -1;
 if (p->op != 1)
  return -1;
 op = p->options + 4;
 while (*op != 255) {
  if (op - p->options >= (1500 - (236 + (20 + 8))))
   return -1;
  else if (*op == 0)
   ++op;
  else
   op += op[1] + 2;
 }
 return 0;
}

canonical_original_code:
int check_dhcp_packet(struct dhcp_packet *p)
{
 unsigned char *op;
 if (memcmp(p->options, "\143\202\123\143", 4))
  return -1;
 if (p->op != 1)
  return -1;
 op = p->options + 4;
 while (*op != 255) {
  if (op - p->options >= (1500 - (236 + (20 + 8))))
   return -1;
  else if (*op == 0)
   ++op;
  else
   op += op[1] + 2;
 }
 return 0;
}

variable_types: struct dhcp_packet { unsigned int op; unsigned int htype; unsigned int hlen; unsigned int hops; unsigned int xid; unsigned int secs; unsigned int flags; struct in_addr { unsigned int s_addr; } ciaddr; struct in_addr yiaddr; struct in_addr siaddr; struct in_addr giaddr; unsigned char chaddr[16]; char sname[64]; char file[128]; unsigned char options[-1]; } *

variable_types:
op
unsigned char *

user_defined_types: struct dhcp_packet { unsigned int op; unsigned int htype; unsigned int hlen; unsigned int hops; unsigned int xid; unsigned int secs; unsigned int flags; struct in_addr ciaddr; struct in_addr yiaddr; struct in_addr siaddr; struct in_addr giaddr; unsigned char chaddr[16]; char sname[64]; char file[128]; unsigned char options[-1]; }


decompiled_code:
__int64 __fastcall get_dhcp_option(__int64 a1, char a2, _QWORD *a3, _BYTE *a4)
{
char *v5; // rdx
char v6; // al

v5 = (char *)(a1 + 240);
v6 = *(_BYTE *)(a1 + 240);
if ( v6 == -1 )
return 0xFFFFFFFFLL;
do
{
if ( a2 == v6 )
{
*a3 = v5 + 2;
*a4 = v5[1];
return 0LL;
}
if ( v6 )
v5 += (unsigned __int8)v5[1] + 2;
else
++v5;
v6 = *v5;
}
while ( *v5 != -1 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char a2, _QWORD *a3, _BYTE *a4)
{
char *v5; // rdx
char v6; // al

v5 = (char *)(a1 + 240);
v6 = *(_BYTE *)(a1 + 240);
if ( v6 == -1 )
return 0xFFFFFFFFLL;
do
{
if ( a2 == v6 )
{
*a3 = v5 + 2;
*a4 = v5[1];
return 0LL;
}
if ( v6 )
v5 += (unsigned __int8)v5[1] + 2;
else
++v5;
v6 = *v5;
}
while ( *v5 != -1 );
return 0xFFFFFFFFLL;
}

original_code:
int get_dhcp_option(struct dhcp_packet *p, u_char type, u_char **opt, u_char *optlen)
{
 u_char *opos = p->options + 4;
 while (*opos != 255) {
  if (*opos == type) {
   *opt = &opos[2];
   *optlen = opos[1];
   return 0;
  }
  if (*opos == 0)
   opos++;
  else
   opos += opos[1] + 2;
 }
 return -1;
}

canonical_original_code:
int get_dhcp_option(struct dhcp_packet *p, unsigned char type, unsigned char **opt, unsigned char *optlen)
{
 unsigned char *opos = p->options + 4;
 while (*opos != 255) {
  if (*opos == type) {
   *opt = &opos[2];
   *optlen = opos[1];
   return 0;
  }
  if (*opos == 0)
   opos++;
  else
   opos += opos[1] + 2;
 }
 return -1;
}

variable_types:
type
unsigned char

variable_types:
opt
unsigned char **

variable_types:
optlen
unsigned char *

variable_types:
opos
unsigned char *


decompiled_code:
__int64 __fastcall get_dhcp_message_type(__int64 a1)
{
__int64 result; // rax
char v2; // [rsp+7h] [rbp-9h] BYREF
unsigned __int8 *v3; // [rsp+8h] [rbp-8h] BYREF

result = get_dhcp_option(a1, 53, &v3, &v2);
if ( (_DWORD)result != -1 )
result = *v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
char v2; // [rsp+7h] [rbp-9h] BYREF
unsigned __int8 *v3; // [rsp+8h] [rbp-8h] BYREF

result = func1(a1, 53, &v3, &v2);
if ( (_DWORD)result != -1 )
result = *v3;
return result;
}

original_code:
int get_dhcp_message_type(struct dhcp_packet *p)
{
 u_char *opt, optlen;
 if (get_dhcp_option(p, 53, &opt, &optlen) == -1) {
  return -1;
 }
 return *opt;
}

canonical_original_code:
int get_dhcp_message_type(struct dhcp_packet *p)
{
 unsigned char *opt; unsigned char optlen;
 if (get_dhcp_option(p, 53, &opt, &optlen) == -1) {
  return -1;
 }
 return *opt;
}

variable_types:
opt
unsigned char *

variable_types:
optlen
unsigned char


decompiled_code:
__int64 __fastcall get_dhcp_packet_len(__int64 a1)
{
char *v1; // rax
char v2; // dl

v1 = (char *)(a1 + 240);
v2 = *(_BYTE *)(a1 + 240);
if ( v2 != -1 )
{
do
{
if ( v2 )
v1 += (unsigned __int8)v1[1] + 2;
else
++v1;
v2 = *v1;
}
while ( *v1 != -1 );
}
return (unsigned int)((_DWORD)v1 - a1 + 1);
}

original_code:
int get_dhcp_packet_len(struct dhcp_packet *p)
{
 u_char *op = p->options;
 op += 4;
 while (*op != 255) {
  if (*op == 0)
   op++;
  else
   op += op[1] + 2;
 }
 return (op - (u_char*)p) + 1;
}

canonical_original_code:
int get_dhcp_packet_len(struct dhcp_packet *p)
{
 unsigned char *op = p->options;
 op += 4;
 while (*op != 255) {
  if (*op == 0)
   op++;
  else
   op += op[1] + 2;
 }
 return (op - (unsigned char *)p) + 1;
}


decompiled_code:
__int64 __fastcall add_dhcp_option(__int64 a1, char a2, _QWORD *a3, unsigned __int8 a4)
{
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // rdx

result = get_dhcp_packet_len(a1);
if ( (_DWORD)result != -1 )
{
if ( a4 + (unsigned int)result + 3 > 0x5C0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v7 = a1 + (int)result - 1;
*(_BYTE *)v7 = a2;
*(_BYTE *)(v7 + 1) = a4;
v8 = v7 + 2;
if ( a4 > 7u )
{
*(_QWORD *)(v7 + 2) = *a3;
*(_QWORD *)(v8 + a4 - 8) = *(_QWORD *)((char *)a3 + a4 - 8);
qmemcpy(
(void *)((v7 + 10) & 0xFFFFFFFFFFFFFFF8LL),
(char *)a3 - (v8 - ((v7 + 10) & 0xFFFFFFFFFFFFFFF8LL)),
8LL * (((unsigned int)v8 - (((_DWORD)v7 + 10) & 0xFFFFFFF8) + a4) >> 3));
}
else if ( (a4 & 4) != 0 )
{
*(_DWORD *)(v7 + 2) = *(_DWORD *)a3;
*(_DWORD *)(v8 + a4 - 4) = *(_DWORD *)((char *)a3 + a4 - 4);
}
else if ( a4 )
{
*(_BYTE *)(v7 + 2) = *(_BYTE *)a3;
if ( (a4 & 2) != 0 )
*(_WORD *)(v7 + a4) = *(_WORD *)((char *)a3 + a4 - 2);
}
*(_BYTE *)(v7 + a4 + 2) = -1;
result = 0LL;
}
}
return result;
}

original_code:
int add_dhcp_option(struct dhcp_packet *p, u_char type,
  u_char *opt, u_char optlen)
{
 u_char *op;
 int len = get_dhcp_packet_len(p);
 if (len == -1) return -1;
 if (len + optlen + 3 > sizeof(struct dhcp_packet))
  return -1;
 op = (u_char*)p + len - 1;
 op[0] = type;
 op[1] = optlen;
 memcpy(&op[2], opt, optlen);
 op[2+optlen] = 255;
 return 0;
}

canonical_original_code:
int add_dhcp_option(struct dhcp_packet *p, unsigned char type, unsigned char *opt, unsigned char optlen)
{
 unsigned char *op;
 int len = get_dhcp_packet_len(p);
 if (len == -1) return -1;
 if (len + optlen + 3 > sizeof(struct dhcp_packet))
  return -1;
 op = (unsigned char *)p + len - 1;
 op[0] = type;
 op[1] = optlen;
 memcpy(&op[2], opt, optlen);
 op[2+optlen] = 255;
 return 0;
}

variable_types: len int


decompiled_code:
int __fastcall signal_handler(unsigned int a1)
{
time_t timer; // [rsp+8h] [rbp-70h] BYREF
char s[8]; // [rsp+10h] [rbp-68h] BYREF
__int64 v4; // [rsp+18h] [rbp-60h]
int v5; // [rsp+20h] [rbp-58h]
struct tm tp; // [rsp+30h] [rbp-48h] BYREF

switch ( a1 )
{
case 0xFu:
stopflag = 1;
break;
case 3u:
stopflag = 1;
break;
case 2u:
stopflag = 1;
break;
}
*(_QWORD *)s = 0LL;
v4 = 0LL;
v5 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got signal %d", a1);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1)
{
time_t timer; // [rsp+8h] [rbp-70h] BYREF
char s[8]; // [rsp+10h] [rbp-68h] BYREF
__int64 v4; // [rsp+18h] [rbp-60h]
int v5; // [rsp+20h] [rbp-58h]
struct tm tp; // [rsp+30h] [rbp-48h] BYREF

switch ( a1 )
{
case 0xFu:
stopflag = 1;
break;
case 3u:
stopflag = 1;
break;
case 2u:
stopflag = 1;
break;
}
*(_QWORD *)s = 0LL;
v4 = 0LL;
v5 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got signal %d", a1);
return putchar(10);
}

original_code:
void signal_handler(int sig)
{
 if (sig == 15) stopflag = 1;
 if (sig == 3) stopflag = 1;
 if (sig == 2) stopflag = 1;
 { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got signal %d", sig); printf("\n"); };
}

canonical_original_code:
void signal_handler(int sig)
{
 if (sig == 15) stopflag = 1;
 if (sig == 3) stopflag = 1;
 if (sig == 2) stopflag = 1;
 { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got signal %d", sig); printf("\n"); };
}

variable_types: sig int

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


decompiled_code:
int __fastcall dhcp_reply(__int64 a1)
{
int result; // eax
int v3; // eax
int *v4; // rax
char *v5; // rax
char v6[3]; // [rsp+5h] [rbp-643h] BYREF
time_t timer; // [rsp+8h] [rbp-640h] BYREF
char s[8]; // [rsp+10h] [rbp-638h] BYREF
__int64 v9; // [rsp+18h] [rbp-630h]
int v10; // [rsp+20h] [rbp-628h]
struct tm tp; // [rsp+30h] [rbp-618h] BYREF
char buf[20]; // [rsp+70h] [rbp-5D8h] BYREF
in_addr_t v13; // [rsp+84h] [rbp-5C4h]
__m128i v14; // [rsp+8Ch] [rbp-5BCh]
char dest[1371]; // [rsp+DCh] [rbp-56Ch] BYREF
char v16; // [rsp+637h] [rbp-11h] BYREF
const void *v17; // [rsp+638h] [rbp-10h] BYREF

*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("dhcp_reply start");
putchar(10);
if ( (unsigned int)check_dhcp_packet(a1) == -1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got a invalid dhcp packet");
result = putchar(10);
}
else if ( (unsigned int)get_dhcp_message_type(a1) == 1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got a DHCPDISCOVER on dhcp");
putchar(10);
if ( (unsigned int)get_dhcp_option(a1, 60LL, &v17, &v16) != -1 && !memcmp(v17, "PXEClient", 9uLL) )
{
init_packet((__int64)buf, a1, 2);
v13 = tftp_ip.s_addr;
v14 = _mm_loadu_si128((const __m128i *)(a1 + 28));
strcpy(dest, nbp_name);
if ( (unsigned int)add_dhcp_option(buf, 60LL, "PXEClient", 9LL) == -1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("add_dhcp_option() failed");
result = putchar(10);
}
else
{
v6[0] = 6;
v6[1] = 1;
v6[2] = 8;
if ( (unsigned int)add_dhcp_option(buf, 43LL, v6, 3LL) == -1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("add_dhcp_option() failed");
result = putchar(10);
}
else
{
v3 = get_dhcp_packet_len(buf);
if ( v3 == (unsigned int)sendto(server_socket, buf, v3, 0, &bcast_address, 0x10u) )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("dhcp_reply end");
}
else
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: pxe-pdhcp.c: ", s);
v4 = _errno_location();
v5 = strerror(*v4);
printf("sendto() failed or was not complete: %s", v5);
}
result = putchar(10);
}
}
}
else
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: pxe-pdhcp.c: ", s);
printf("got a non-PXE dhcp packet");
result = putchar(10);
}
}
else
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got a non-DHCPDISCOVER dhcp packet");
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int result; // eax
int v3; // eax
int *v4; // rax
char *v5; // rax
char v6[3]; // [rsp+5h] [rbp-643h] BYREF
time_t timer; // [rsp+8h] [rbp-640h] BYREF
char s[8]; // [rsp+10h] [rbp-638h] BYREF
__int64 v9; // [rsp+18h] [rbp-630h]
int v10; // [rsp+20h] [rbp-628h]
struct tm tp; // [rsp+30h] [rbp-618h] BYREF
char buf[20]; // [rsp+70h] [rbp-5D8h] BYREF
in_addr_t v13; // [rsp+84h] [rbp-5C4h]
__m128i v14; // [rsp+8Ch] [rbp-5BCh]
char dest[1371]; // [rsp+DCh] [rbp-56Ch] BYREF
char v16; // [rsp+637h] [rbp-11h] BYREF
const void *v17; // [rsp+638h] [rbp-10h] BYREF

*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("dhcp_reply start");
putchar(10);
if ( (unsigned int)check_dhcp_packet(a1) == -1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got a invalid dhcp packet");
result = putchar(10);
}
else if ( (unsigned int)get_dhcp_message_type(a1) == 1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got a DHCPDISCOVER on dhcp");
putchar(10);
if ( (unsigned int)get_dhcp_option(a1, 60LL, &v17, &v16) != -1 && !memcmp(v17, "PXEClient", 9uLL) )
{
func2((__int64)buf, a1, 2);
v13 = tftp_ip.s_addr;
v14 = _mm_loadu_si128((const __m128i *)(a1 + 28));
strcpy(dest, nbp_name);
if ( (unsigned int)add_dhcp_option(buf, 60LL, "PXEClient", 9LL) == -1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("add_dhcp_option() failed");
result = putchar(10);
}
else
{
v6[0] = 6;
v6[1] = 1;
v6[2] = 8;
if ( (unsigned int)add_dhcp_option(buf, 43LL, v6, 3LL) == -1 )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("add_dhcp_option() failed");
result = putchar(10);
}
else
{
v3 = get_dhcp_packet_len(buf);
if ( v3 == (unsigned int)sendto(server_socket, buf, v3, 0, &bcast_address, 0x10u) )
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("dhcp_reply end");
}
else
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: pxe-pdhcp.c: ", s);
v4 = _errno_location();
v5 = strerror(*v4);
printf("sendto() failed or was not complete: %s", v5);
}
result = putchar(10);
}
}
}
else
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: pxe-pdhcp.c: ", s);
printf("got a non-PXE dhcp packet");
result = putchar(10);
}
}
else
{
*(_QWORD *)s = 0LL;
v9 = 0LL;
v10 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: DEBUG: pxe-pdhcp.c: ", s);
printf("got a non-DHCPDISCOVER dhcp packet");
result = putchar(10);
}
return result;
}

original_code:
void dhcp_reply(struct dhcp_packet *p, struct sockaddr* client_address, socklen_t client_addrlen)
{
 int packet_len;
 u_char *opt;
 u_char optlen;
 struct dhcp_packet re;
 int res;
 { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("dhcp_reply start"); printf("\n"); };
 if (check_dhcp_packet(p) == -1) {
  { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got a invalid dhcp packet"); printf("\n"); };
  return;
 }
 if (get_dhcp_message_type(p) == 1) {
  { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got a DHCPDISCOVER on dhcp"); printf("\n"); };
 } else {
  { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got a non-DHCPDISCOVER dhcp packet"); printf("\n"); };
  return;
 }
 res = get_dhcp_option(p, 60, &opt, &optlen);
 if (res == -1 || strncmp((char*)opt, "PXEClient", strlen("PXEClient")) != 0) {
  { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: " "pxe-pdhcp.c" ": ", _ts); printf("got a non-PXE dhcp packet"); printf("\n"); };
  return;
 }
 init_packet(&re, p, 2);
 re.siaddr = tftp_ip;
 memcpy(re.chaddr, p->chaddr, sizeof(re.chaddr));
 strcpy(re.file, nbp_name);
 res = add_dhcp_option(&re, 60,
  (u_char*)"PXEClient",
  strlen("PXEClient"));
 if (res == -1) {
  { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("add_dhcp_option() failed"); printf("\n"); };
  return;
 }
 {
  u_char vop[3];
  vop[0] = 6;
  vop[1] = 1;
  vop[2] = 0x08;
  if (add_dhcp_option(&re, 43, vop, 3) == -1) {
   { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("add_dhcp_option() failed"); printf("\n"); };
   return;
  }
 }
 packet_len = get_dhcp_packet_len(&re);
 res = sendto(server_socket, &re, packet_len, 0,
  (struct sockaddr*)&bcast_address, sizeof(bcast_address));
 if (res != packet_len) {
  { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: " "pxe-pdhcp.c" ": ", _ts); printf("sendto() failed or was not complete: %s", strerror((*__errno_location ()))); printf("\n"); };
  return;
 }
 { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("dhcp_reply end"); printf("\n"); };
}

canonical_original_code:
void dhcp_reply(struct dhcp_packet *p, struct sockaddr *client_address, unsigned int client_addrlen)
{
 int packet_len;
 unsigned char *opt;
 unsigned char optlen;
 struct dhcp_packet re;
 int res;
 { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("dhcp_reply start"); printf("\n"); };
 if (check_dhcp_packet(p) == -1) {
  { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got a invalid dhcp packet"); printf("\n"); };
  return;
 }
 if (get_dhcp_message_type(p) == 1) {
  { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got a DHCPDISCOVER on dhcp"); printf("\n"); };
 } else {
  { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("got a non-DHCPDISCOVER dhcp packet"); printf("\n"); };
  return;
 }
 res = get_dhcp_option(p, 60, &opt, &optlen);
 if (res == -1 || strncmp((char *)opt, "PXEClient", strlen("PXEClient")) != 0) {
  { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: " "pxe-pdhcp.c" ": ", _ts); printf("got a non-PXE dhcp packet"); printf("\n"); };
  return;
 }
 init_packet(&re, p, 2);
 re.siaddr = tftp_ip;
 memcpy(re.chaddr, p->chaddr, sizeof(re.chaddr));
 strcpy(re.file, nbp_name);
 res = add_dhcp_option(&re, 60,
  (unsigned char *)"PXEClient",
  strlen("PXEClient"));
 if (res == -1) {
  { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("add_dhcp_option() failed"); printf("\n"); };
  return;
 }
 {
  unsigned char vop[3];
  vop[0] = 6;
  vop[1] = 1;
  vop[2] = 0x08;
  if (add_dhcp_option(&re, 43, vop, 3) == -1) {
   { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("add_dhcp_option() failed"); printf("\n"); };
   return;
  }
 }
 packet_len = get_dhcp_packet_len(&re);
 res = sendto(server_socket, &re, packet_len, 0,
  (struct sockaddr *)&bcast_address, sizeof(bcast_address));
 if (res != packet_len) {
  { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: " "pxe-pdhcp.c" ": ", _ts); printf("sendto() failed or was not complete: %s", strerror((*__errno_location ()))); printf("\n"); };
  return;
 }
 { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: DEBUG: " "pxe-pdhcp.c" ": ", _ts); printf("dhcp_reply end"); printf("\n"); };
}

variable_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; } *

variable_types: struct dhcp_packet { unsigned int op; unsigned int htype; unsigned int hlen; unsigned int hops; unsigned int xid; unsigned int secs; unsigned int flags; struct in_addr { unsigned int s_addr; } ciaddr; struct in_addr yiaddr; struct in_addr siaddr; struct in_addr giaddr; unsigned char chaddr[16]; char sname[64]; char file[128]; unsigned char options[-1]; }

variable_types: res int

variable_types:
vop
unsigned char [3]

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


pdhcp

decompiled_code:
void pdhcp()
{
int *v0; // rax
char *v1; // rax
time_t timer; // [rsp+0h] [rbp-640h] BYREF
char s[8]; // [rsp+8h] [rbp-638h] BYREF
__int64 v4; // [rsp+10h] [rbp-630h]
int v5; // [rsp+18h] [rbp-628h]
struct tm tp; // [rsp+28h] [rbp-618h] BYREF
socklen_t addr_len; // [rsp+64h] [rbp-5DCh] BYREF
struct sockaddr addr; // [rsp+68h] [rbp-5D8h] BYREF
char buf[1480]; // [rsp+78h] [rbp-5C8h] BYREF

while ( !stopflag )
{
memset(buf, 0, 0x5C0uLL);
addr_len = 16;
if ( (unsigned int)recvfrom(server_socket, buf, 0x5C0uLL, 0, &addr, &addr_len) == -1 )
{
*(_QWORD *)s = 0LL;
v4 = 0LL;
v5 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: pxe-pdhcp.c: ", s);
v0 = _errno_location();
v1 = strerror(*v0);
printf("recvfrom() failed: %s", v1);
putchar(10);
}
else
{
dhcp_reply((__int64)buf);
}
}
}

canonical_decompiled_code:
void func4()
{
int *v0; // rax
char *v1; // rax
time_t timer; // [rsp+0h] [rbp-640h] BYREF
char s[8]; // [rsp+8h] [rbp-638h] BYREF
__int64 v4; // [rsp+10h] [rbp-630h]
int v5; // [rsp+18h] [rbp-628h]
struct tm tp; // [rsp+28h] [rbp-618h] BYREF
socklen_t addr_len; // [rsp+64h] [rbp-5DCh] BYREF
struct sockaddr addr; // [rsp+68h] [rbp-5D8h] BYREF
char buf[1480]; // [rsp+78h] [rbp-5C8h] BYREF

while ( !stopflag )
{
memset(buf, 0, 0x5C0uLL);
addr_len = 16;
if ( (unsigned int)recvfrom(server_socket, buf, 0x5C0uLL, 0, &addr, &addr_len) == -1 )
{
*(_QWORD *)s = 0LL;
v4 = 0LL;
v5 = 0;
time(&timer);
localtime_r(&timer, &tp);
strftime(s, 0x13uLL, "%b %d %H:%M:%S", &tp);
printf("%s: pxe-pdhcp.c: ", s);
v0 = _errno_location();
v1 = strerror(*v0);
printf("recvfrom() failed: %s", v1);
putchar(10);
}
else
{
func3((__int64)buf);
}
}
}

original_code:
void pdhcp(void)
{
 struct dhcp_packet dhcp_p;
 struct sockaddr_in client_address;
 socklen_t client_addrlen;
 int res;
 while(!stopflag) {
  memset(&dhcp_p, 0, sizeof(dhcp_p));
  client_addrlen = sizeof(client_address);
  res = recvfrom(server_socket, &dhcp_p, sizeof(dhcp_p), 0,
   (struct sockaddr*)&client_address, &client_addrlen);
  if (res == -1) {
   { char _ts[20] = {0}; time_t _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: " "pxe-pdhcp.c" ": ", _ts); printf("recvfrom() failed: %s", strerror((*__errno_location ()))); printf("\n"); };
   continue;
  }
  dhcp_reply(&dhcp_p, (struct sockaddr*)&client_address,
   client_addrlen);
 }
}

canonical_original_code:
void pdhcp()
{
 struct dhcp_packet dhcp_p;
 struct sockaddr_in client_address;
 unsigned int client_addrlen;
 int res;
 while(!stopflag) {
  memset(&dhcp_p, 0, sizeof(dhcp_p));
  client_addrlen = sizeof(client_address);
  res = recvfrom(server_socket, &dhcp_p, sizeof(dhcp_p), 0,
   (struct sockaddr *)&client_address, &client_addrlen);
  if (res == -1) {
   { char _ts[20] = {0}; long _t; struct tm _tm; time(&_t); localtime_r(&_t, &_tm); strftime(_ts, 19, "%b %d %H:%M:%S", &_tm); printf("%s: " "pxe-pdhcp.c" ": ", _ts); printf("recvfrom() failed: %s", strerror((*__errno_location ()))); printf("\n"); };
   continue;
  }
  dhcp_reply(&dhcp_p, (struct sockaddr *)&client_address,
   client_addrlen);
 }
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
int init_socket()
{
int result; // eax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = socket(2, 2, 0);
server_socket = result;
if ( result != -1 )
{
optval[0] = 1;
result = setsockopt(result, 1, 6, optval, 4u);
if ( result != -1 )
result = -(bind(server_socket, &server_address, 0x10u) == -1);
}
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = socket(2, 2, 0);
server_socket = result;
if ( result != -1 )
{
optval[0] = 1;
result = setsockopt(result, 1, 6, optval, 4u);
if ( result != -1 )
result = -(bind(server_socket, &server_address, 0x10u) == -1);
}
return result;
}

original_code:
int init_socket(void)
{
 int res;
 int on;
 server_socket = socket(2, SOCK_DGRAM, 0);
 if (server_socket == -1) return -1;
 on = 1;
 res = setsockopt(server_socket, 1, 6, &on, sizeof(on));
 if (res == -1) return -1;
 res = bind(server_socket, (struct sockaddr*)&server_address,
   sizeof(server_address));
 if (res == -1) return -1;
 return 0;
}

canonical_original_code:
int init_socket()
{
 int res;
 int on;
 server_socket = socket(2, SOCK_DGRAM, 0);
 if (server_socket == -1) return -1;
 on = 1;
 res = setsockopt(server_socket, 1, 6, &on, sizeof(on));
 if (res == -1) return -1;
 res = bind(server_socket, (struct sockaddr *)&server_address,
   sizeof(server_address));
 if (res == -1) return -1;
 return 0;
}

variable_types: on int


decompiled_code:
int release_all()
{
return close(server_socket);
}

canonical_decompiled_code:
int func6()
{
return close(server_socket);
}

original_code:
void release_all(void)
{
 close(server_socket);
}

canonical_original_code:
void release_all()
{
 close(server_socket);
}


decompiled_code:
int install_signal_handler()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
result = sigaction(15, &v1, 0LL);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
result = sigaction(3, &v1, 0LL);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
result = -(sigaction(2, &v1, 0LL) == -1);
}
}
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
result = sigaction(15, &v1, 0LL);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
result = sigaction(3, &v1, 0LL);
if ( result != -1 )
{
v1.sa_handler = (__sighandler_t)signal_handler;
sigemptyset(&v1.sa_mask);
v1.sa_flags = 0;
result = -(sigaction(2, &v1, 0LL) == -1);
}
}
return result;
}

original_code:
int install_signal_handler(void)
{
 struct sigaction action;
 action.__sigaction_handler.sa_handler = signal_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if (sigaction(15, &action, ((void *)0)) == -1)
  return -1;
 action.__sigaction_handler.sa_handler = signal_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if (sigaction(3, &action, ((void *)0)) == -1)
  return -1;
 action.__sigaction_handler.sa_handler = signal_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if (sigaction(2, &action, ((void *)0)) == -1)
  return -1;
 return 0;
}

canonical_original_code:
int install_signal_handler()
{
 struct sigaction action;
 action.__sigaction_handler.sa_handler = signal_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if (sigaction(15, &action, ((void *)0)) == -1)
  return -1;
 action.__sigaction_handler.sa_handler = signal_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if (sigaction(3, &action, ((void *)0)) == -1)
  return -1;
 action.__sigaction_handler.sa_handler = signal_handler;
 sigemptyset(&action.sa_mask);
 action.sa_flags = 0;
 if (sigaction(2, &action, ((void *)0)) == -1)
  return -1;
 return 0;
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


usage

decompiled_code:
size_t __fastcall usage(const char *a1)
{
fprintf(
stderr,
"usage: %s [-d] [-i interface] [-l listen address] [-t tftp address] [-b broadcast address] <nbp name>\n",
a1);
return fwrite("-i or (-l, -t and -b), and <nbp name> options are needed.\n", 1uLL, 0x3AuLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func8(const char *a1)
{
fprintf(
stderr,
"usage: %s [-d] [-i interface] [-l listen address] [-t tftp address] [-b broadcast address] <nbp name>\n",
a1);
return fwrite("-i or (-l, -t and -b), and <nbp name> options are needed.\n", 1uLL, 0x3AuLL, stderr);
}

original_code:
void usage(const char* prog)
{
 fprintf(stderr, "usage: %s  [-d] [-i interface] [-l listen address] [-t tftp address] [-b broadcast address] <nbp name>\n", prog);
 fprintf(stderr, "-i or (-l, -t and -b), and <nbp name> options are needed.\n");
}

canonical_original_code:
void usage(char *prog)
{
 fprintf(stderr, "usage: %s  [-d] [-i interface] [-l listen address] [-t tftp address] [-b broadcast address] <nbp name>\n", prog);
 fprintf(stderr, "-i or (-l, -t and -b), and <nbp name> options are needed.\n");
}

variable_types:
prog
char *


decompiled_code:
__int16 __fastcall parse_argv(int argc, char **argv)
{
int v2; // er15
int v3; // er13
int v4; // er14
char *v5; // r12
int v6; // eax
bool v7; // bp
int v8; // ebx
__int16 result; // ax
char v10[16]; // [rsp+0h] [rbp-68h] BYREF
__int16 v11; // [rsp+10h] [rbp-58h]
in_addr_t v12; // [rsp+14h] [rbp-54h]

*(_QWORD *)&server_address.sa_family = 0LL;
*(_QWORD *)&server_address.sa_data[6] = 0LL;
*(_QWORD *)&bcast_address.sa_family = 0LL;
*(_QWORD *)&bcast_address.sa_data[6] = 0LL;
tftp_ip.s_addr = 0;
nbp_name = 0LL;
v2 = 0;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( 1 )
{
v6 = getopt(argc, argv, "i:l:b:t:d");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'b':
if ( !inet_aton(optarg, (struct in_addr *)(unsigned int)((_DWORD)&bcast_address + 4)) )
{
perror(optarg);
exit(1);
}
v3 = 1;
break;
case 'd':
foreground = 1;
break;
case 'i':
v5 = optarg;
break;
case 'l':
if ( !inet_aton(optarg, (struct in_addr *)(unsigned int)((_DWORD)&server_address + 4)) )
{
perror(optarg);
exit(1);
}
v4 = 1;
break;
case 't':
if ( !inet_aton(optarg, &tftp_ip) )
{
perror(optarg);
exit(1);
}
v2 = 1;
break;
default:
usage(*argv);
exit(1);
}
}
if ( optind >= argc )
{
usage(*argv);
exit(1);
}
nbp_name = argv[optind];
v7 = v2 == 0 || v4 == 0;
if ( !v3 || v7 )
{
if ( !v5 )
{
usage(*argv);
exit(1);
}
v8 = socket(2, 2, 0);
if ( v8 == -1 )
{
perror("socket() failed");
exit(1);
}
v11 = 2;
strncpy(v10, v5, 0xFuLL);
v10[15] = 0;
if ( !v7 )
goto LABEL_22;
if ( ioctl(v8, 0x8915uLL, v10) == -1 )
{
perror("cannot get ip address of listen interface");
close(v8);
exit(1);
}
if ( v4 || (*(_DWORD *)&server_address.sa_data[2] = v12, !v2) )
tftp_ip.s_addr = v12;
if ( !v3 )
{
LABEL_22:
if ( ioctl(v8, 0x8919uLL, v10) == -1 )
{
perror("cannot get broadcast address of listen interface");
close(v8);
exit(1);
}
*(_DWORD *)&bcast_address.sa_data[2] = v12;
}
close(v8);
}
*(_WORD *)server_address.sa_data = _bswap_16(67);
server_address.sa_family = 2;
result = _bswap_16(68);
*(_WORD *)bcast_address.sa_data = result;
bcast_address.sa_family = 2;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func9(int argc, char **argv)
{
int v2; // er15
int v3; // er13
int v4; // er14
char *v5; // r12
int v6; // eax
bool v7; // bp
int v8; // ebx
__int16 result; // ax
char v10[16]; // [rsp+0h] [rbp-68h] BYREF
__int16 v11; // [rsp+10h] [rbp-58h]
in_addr_t v12; // [rsp+14h] [rbp-54h]

*(_QWORD *)&server_address.sa_family = 0LL;
*(_QWORD *)&server_address.sa_data[6] = 0LL;
*(_QWORD *)&bcast_address.sa_family = 0LL;
*(_QWORD *)&bcast_address.sa_data[6] = 0LL;
tftp_ip.s_addr = 0;
nbp_name = 0LL;
v2 = 0;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( 1 )
{
v6 = getopt(argc, argv, "i:l:b:t:d");
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'b':
if ( !inet_aton(optarg, (struct in_addr *)(unsigned int)((_DWORD)&bcast_address + 4)) )
{
perror(optarg);
exit(1);
}
v3 = 1;
break;
case 'd':
foreground = 1;
break;
case 'i':
v5 = optarg;
break;
case 'l':
if ( !inet_aton(optarg, (struct in_addr *)(unsigned int)((_DWORD)&server_address + 4)) )
{
perror(optarg);
exit(1);
}
v4 = 1;
break;
case 't':
if ( !inet_aton(optarg, &tftp_ip) )
{
perror(optarg);
exit(1);
}
v2 = 1;
break;
default:
func8(*argv);
exit(1);
}
}
if ( optind >= argc )
{
func8(*argv);
exit(1);
}
nbp_name = argv[optind];
v7 = v2 == 0 || v4 == 0;
if ( !v3 || v7 )
{
if ( !v5 )
{
func8(*argv);
exit(1);
}
v8 = socket(2, 2, 0);
if ( v8 == -1 )
{
perror("socket() failed");
exit(1);
}
v11 = 2;
strncpy(v10, v5, 0xFuLL);
v10[15] = 0;
if ( !v7 )
goto LABEL_22;
if ( ioctl(v8, 0x8915uLL, v10) == -1 )
{
perror("cannot get ip address of listen interface");
close(v8);
exit(1);
}
if ( v4 || (*(_DWORD *)&server_address.sa_data[2] = v12, !v2) )
tftp_ip.s_addr = v12;
if ( !v3 )
{
LABEL_22:
if ( ioctl(v8, 0x8919uLL, v10) == -1 )
{
perror("cannot get broadcast address of listen interface");
close(v8);
exit(1);
}
*(_DWORD *)&bcast_address.sa_data[2] = v12;
}
close(v8);
}
*(_WORD *)server_address.sa_data = func1(67);
server_address.sa_family = 2;
result = func1(68);
*(_WORD *)bcast_address.sa_data = result;
bcast_address.sa_family = 2;
return result;
}

original_code:
void parse_argv(int argc, char* argv[])
{
 int opt;
 char* listen_dev;
 int set_server_address = 0;
 int set_bcast_address = 0;
 int set_tftp_ip = 0;
 memset(&server_address, 0, sizeof(server_address));
 memset(&bcast_address, 0, sizeof(bcast_address));
 memset(&tftp_ip, 0, sizeof(tftp_ip));
 listen_dev = ((void *)0);
 nbp_name = ((void *)0);
 while ((opt = getopt(argc, argv, "i:l:b:t:d")) != -1) {
  switch(opt) {
  case 'i':
   listen_dev = optarg;
   break;
  case 'l':
   if (!inet_aton(optarg, &server_address.sin_addr)) {
    perror(optarg);
    exit(1);
   }
   set_server_address = 1;
   break;
  case 'b':
   if (!inet_aton(optarg, &bcast_address.sin_addr)) {
    perror(optarg);
    exit(1);
   }
   set_bcast_address = 1;
   break;
  case 't':
   if (!inet_aton(optarg, &tftp_ip)) {
    perror(optarg);
    exit(1);
   }
   set_tftp_ip = 1;
   break;
  case 'd':
   foreground = 1;
   break;
  default:
   usage(argv[0]);
   exit(1);
  }
 }
 if (optind >= argc) {
  usage(argv[0]);
  exit(1);
 }
 nbp_name = argv[optind];
 if (!set_server_address || !set_tftp_ip || !set_bcast_address) {
  struct ifreq ifr;
  int s;
  if (!listen_dev) {
   usage(argv[0]);
   exit(1);
  }
  s = socket(2, SOCK_DGRAM, 0);
  if (s == -1) {
   perror("socket() failed");
   exit(1);
  }
  ifr.ifr_ifru.ifru_addr.sa_family = 2;
  strncpy(ifr.ifr_ifrn.ifrn_name, listen_dev, 16 -1);
  ifr.ifr_ifrn.ifrn_name[16 -1] = '\0';
  if (!set_server_address || !set_tftp_ip) {
   if (ioctl(s, 0x8915, &ifr) == -1) {
    perror("cannot get ip address of listen interface");
    close(s);
    exit(1);
   }
   if (!set_server_address)
    server_address.sin_addr = ((struct sockaddr_in*)&ifr.ifr_ifru.ifru_addr)->sin_addr;
   if (!set_tftp_ip)
    tftp_ip = ((struct sockaddr_in*)&ifr.ifr_ifru.ifru_addr)->sin_addr;
  }
  if (!set_bcast_address) {
   if (ioctl(s, 0x8919, &ifr) == -1) {
    perror("cannot get broadcast address of listen interface");
    close(s);
    exit(1);
   }
   bcast_address.sin_addr =
    ((struct sockaddr_in*)&ifr.ifr_ifru.ifru_broadaddr)->sin_addr;
  }
  close(s);
 }
 server_address.sin_port = ntohs(67);
 server_address.sin_family = 2;
 bcast_address.sin_port = ntohs(68);
 bcast_address.sin_family = 2;
}

canonical_original_code:
void parse_argv(int argc, char *argv[])
{
 int opt;
 char *listen_dev;
 int set_server_address = 0;
 int set_bcast_address = 0;
 int set_tftp_ip = 0;
 memset(&server_address, 0, sizeof(server_address));
 memset(&bcast_address, 0, sizeof(bcast_address));
 memset(&tftp_ip, 0, sizeof(tftp_ip));
 listen_dev = ((void *)0);
 nbp_name = ((void *)0);
 while ((opt = getopt(argc, argv, "i:l:b:t:d")) != -1) {
  switch(opt) {
  case 'i':
   listen_dev = optarg;
   break;
  case 'l':
   if (!inet_aton(optarg, &server_address.sin_addr)) {
    perror(optarg);
    exit(1);
   }
   set_server_address = 1;
   break;
  case 'b':
   if (!inet_aton(optarg, &bcast_address.sin_addr)) {
    perror(optarg);
    exit(1);
   }
   set_bcast_address = 1;
   break;
  case 't':
   if (!inet_aton(optarg, &tftp_ip)) {
    perror(optarg);
    exit(1);
   }
   set_tftp_ip = 1;
   break;
  case 'd':
   foreground = 1;
   break;
  default:
   usage(argv[0]);
   exit(1);
  }
 }
 if (optind >= argc) {
  usage(argv[0]);
  exit(1);
 }
 nbp_name = argv[optind];
 if (!set_server_address || !set_tftp_ip || !set_bcast_address) {
  struct ifreq ifr;
  int s;
  if (!listen_dev) {
   usage(argv[0]);
   exit(1);
  }
  s = socket(2, SOCK_DGRAM, 0);
  if (s == -1) {
   perror("socket() failed");
   exit(1);
  }
  ifr.ifr_ifru.ifru_addr.sa_family = 2;
  strncpy(ifr.ifr_ifrn.ifrn_name, listen_dev, 16 -1);
  ifr.ifr_ifrn.ifrn_name[16 -1] = '\0';
  if (!set_server_address || !set_tftp_ip) {
   if (ioctl(s, 0x8915, &ifr) == -1) {
    perror("cannot get ip address of listen interface");
    close(s);
    exit(1);
   }
   if (!set_server_address)
    server_address.sin_addr = ((struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr)->sin_addr;
   if (!set_tftp_ip)
    tftp_ip = ((struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr)->sin_addr;
  }
  if (!set_bcast_address) {
   if (ioctl(s, 0x8919, &ifr) == -1) {
    perror("cannot get broadcast address of listen interface");
    close(s);
    exit(1);
   }
   bcast_address.sin_addr =
    ((struct sockaddr_in *)&ifr.ifr_ifru.ifru_broadaddr)->sin_addr;
  }
  close(s);
 }
 server_address.sin_port = ntohs(67);
 server_address.sin_family = 2;
 bcast_address.sin_port = ntohs(68);
 bcast_address.sin_family = 2;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

variable_types: s int

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // bx
char *v4; // rax
unsigned __int16 v5; // bx
char *v6; // rax
char *v7; // rax

parse_argv(argc, (char **)argv);
v3 = _bswap_16(*(_WORD *)server_address.sa_data);
v4 = inet_ntoa(*(struct in_addr *)&server_address.sa_data[2]);
printf("listen address: %s:%d\n", v4, v3);
v5 = _bswap_16(*(_WORD *)bcast_address.sa_data);
v6 = inet_ntoa(*(struct in_addr *)&bcast_address.sa_data[2]);
printf("broadcast address %s:%d\n", v6, v5);
v7 = inet_ntoa(tftp_ip);
printf("tftp address: %s\n", v7);
printf("nbp name: %s\n", nbp_name);
if ( install_signal_handler() == -1 )
{
perror("sigaction() failed");
exit(-1);
}
if ( init_socket() == -1 )
{
perror("init_socket()");
release_all();
exit(1);
}
if ( !foreground )
daemon(0, 0);
pdhcp();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // bx
char *v4; // rax
unsigned __int16 v5; // bx
char *v6; // rax
char *v7; // rax

func9(argc, (char **)argv);
v3 = func1(*(_WORD *)server_address.sa_data);
v4 = inet_ntoa(*(struct in_addr *)&server_address.sa_data[2]);
printf("listen address: %s:%d\n", v4, v3);
v5 = func1(*(_WORD *)bcast_address.sa_data);
v6 = inet_ntoa(*(struct in_addr *)&bcast_address.sa_data[2]);
printf("broadcast address %s:%d\n", v6, v5);
v7 = inet_ntoa(tftp_ip);
printf("tftp address: %s\n", v7);
printf("nbp name: %s\n", nbp_name);
if ( func7() == -1 )
{
perror("sigaction() failed");
exit(-1);
}
if ( func5() == -1 )
{
perror("init_socket()");
func6();
exit(1);
}
if ( !foreground )
daemon(0, 0);
func4();
return 0;
}

original_code:
int main(int argc, char* argv[])
{
 parse_argv(argc, argv);
 printf("listen address:     %s:%d\n", inet_ntoa(server_address.sin_addr),
  ntohs(server_address.sin_port));
 printf("broadcast address   %s:%d\n", inet_ntoa(bcast_address.sin_addr),
  ntohs(bcast_address.sin_port));
 printf("tftp address:       %s\n", inet_ntoa(tftp_ip));
 printf("nbp name:           %s\n", nbp_name);
 if (install_signal_handler() == -1) {
  perror("sigaction() failed");
  exit(-1);
 }
 if (init_socket() == -1) {
  perror("init_socket()");
  release_all();
  exit(1);
 }
 if (foreground) {
  pdhcp();
 } else {
  daemon(0,0);
  pdhcp();
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 parse_argv(argc, argv);
 printf("listen address:     %s:%d\n", inet_ntoa(server_address.sin_addr),
  ntohs(server_address.sin_port));
 printf("broadcast address   %s:%d\n", inet_ntoa(bcast_address.sin_addr),
  ntohs(bcast_address.sin_port));
 printf("tftp address:       %s\n", inet_ntoa(tftp_ip));
 printf("nbp name:           %s\n", nbp_name);
 if (install_signal_handler() == -1) {
  perror("sigaction() failed");
  exit(-1);
 }
 if (init_socket() == -1) {
  perror("init_socket()");
  release_all();
  exit(1);
 }
 if (foreground) {
  pdhcp();
 } else {
  daemon(0,0);
  pdhcp();
 }
 return 0;
}


privatemsg

decompiled_code:
__int64 __fastcall privatemsg(char *src, __int64 a2, unsigned int a3)
{
__int64 result; // rax
int v5; // eax
size_t v6; // rax
size_t v7; // rax
size_t v8; // rax
size_t v9; // rbx
char v10[1024]; // [rsp+0h] [rbp-818h] BYREF
char string[1048]; // [rsp+400h] [rbp-418h] BYREF

result = 0LL;
memset(string, 0, 0x400uLL);
memset(v10, 0, sizeof(v10));
if ( !mute )
{
v5 = usernamecount(a2);
strncpy(v10, (const char *)(a2 + 1), v5);
strcpy(string, "PRIVMSG ");
v6 = strnlen(string, 0x400uLL);
strncat(string, v10, 1023 - v6);
v7 = strnlen(string, 0x400uLL);
strncat(string, " :", 1023 - v7);
v8 = strnlen(string, 0x400uLL);
strncat(string, src, 1023 - v8);
v9 = 1023 - strnlen(string, 0x400uLL);
strncat(string, "\r\n", v9);
if ( logFlag )
msglogprint(string);
result = (unsigned int)-((unsigned int)writeserver(string, a3) == -1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src, __int64 a2, unsigned int a3)
{
__int64 result; // rax
int v5; // eax
size_t v6; // rax
size_t v7; // rax
size_t v8; // rax
size_t v9; // rbx
char v10[1024]; // [rsp+0h] [rbp-818h] BYREF
char string[1048]; // [rsp+400h] [rbp-418h] BYREF

result = 0LL;
memset(string, 0, 0x400uLL);
memset(v10, 0, sizeof(v10));
if ( !mute )
{
v5 = usernamecount(a2);
strncpy(v10, (const char *)(a2 + 1), v5);
strcpy(string, "PRIVMSG ");
v6 = strnlen(string, 0x400uLL);
strncat(string, v10, 1023 - v6);
v7 = strnlen(string, 0x400uLL);
strncat(string, " :", 1023 - v7);
v8 = strnlen(string, 0x400uLL);
strncat(string, src, 1023 - v8);
v9 = 1023 - strnlen(string, 0x400uLL);
strncat(string, "\r\n", v9);
if ( logFlag )
msglogprint(string);
result = (unsigned int)-((unsigned int)writeserver(string, a3) == -1);
}
return result;
}

original_code:
int privatemsg(char *msg, char *serverline, int sckt)
{
    char send[1024];
    char user[1024];
    memset(send, 0, 1024);
    memset(user, 0, 1024);
    if (!mute) {
        strncpy(user, serverline+1, (size_t)usernamecount(serverline));
        snprintf(send, 1024, "%s", "PRIVMSG ");
        strncat(send, user, 1024 -strnlen(send, 1024)-1);
        strncat(send, " :", 1024 -strnlen(send, 1024)-1);
        strncat(send, msg, 1024 -strnlen(send, 1024)-1);
        strncat(send, "\r\n", 1024 -strnlen(send, 1024)-1);
        if (logFlag) {
            msglogprint(send);
        }
        if (writeserver(send, sckt) == -1) {
            return -1;
        }
    } else {
    }
    return 0;
}

canonical_original_code:
int privatemsg(char *msg, char *serverline, int sckt)
{
    char send[1024];
    char user[1024];
    memset(send, 0, 1024);
    memset(user, 0, 1024);
    if (!mute) {
        strncpy(user, serverline+1, (unsigned long)usernamecount(serverline));
        snprintf(send, 1024, "%s", "PRIVMSG ");
        strncat(send, user, 1024 -strnlen(send, 1024)-1);
        strncat(send, " :", 1024 -strnlen(send, 1024)-1);
        strncat(send, msg, 1024 -strnlen(send, 1024)-1);
        strncat(send, "\r\n", 1024 -strnlen(send, 1024)-1);
        if (logFlag) {
            msglogprint(send);
        }
        if (writeserver(send, sckt) == -1) {
            return -1;
        }
    } else {
    }
    return 0;
}

variable_types:
msg
char *

variable_types:
serverline
char *

variable_types: sckt int

variable_types:
send
char [1024]

variable_types:
user
char [1024]


sendcommand

decompiled_code:
__int64 __fastcall sendcommand(const char *a1, unsigned int a2)
{
__int64 result; // rax
char v3[1048]; // [rsp+0h] [rbp-418h] BYREF

memset(v3, 0, 0x400uLL);
snprintf(v3, 0x400uLL, "%s", a1);
result = getmsg(v3, "!send ");
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)writeserver(v3, a2) == -1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, unsigned int a2)
{
__int64 result; // rax
char v3[1048]; // [rsp+0h] [rbp-418h] BYREF

memset(v3, 0, 0x400uLL);
snprintf(v3, 0x400uLL, "%s", a1);
result = getmsg(v3, "!send ");
if ( (_DWORD)result != -1 )
result = (unsigned int)-((unsigned int)writeserver(v3, a2) == -1);
return result;
}

original_code:
int sendcommand(char *s, int sckt)
{
    char send[1024];
    memset(send, 0, 1024);
    snprintf(send, 1024, "%s", s);
    if (getmsg(send, "!send ") == -1) {
        return -1;
    }
    if (writeserver(send, sckt) == -1) {
        return -1;
    }
    return 0;
}

variable_types:
s
char *


getIP

decompiled_code:
__int64 __fastcall getIP(_QWORD *a1, const char *a2, unsigned int a3)
{
size_t v4; // rax
size_t v5; // rax
__int64 result; // rax
int vars0[266]; // [rsp+0h] [rbp+0h] BYREF

memset(vars0, 0, 0x400uLL);
qmemcpy(vars0, "WHOIS ", 6);
v4 = strnlen((const char *)vars0, 0x400uLL);
strncat((char *)vars0, a2, 1023 - v4);
v5 = strnlen((const char *)vars0, 0x400uLL);
strncat((char *)vars0, "\r\n", 1023 - v5);
result = writeserver(vars0, a3);
if ( (_DWORD)result != -1 )
{
while ( 1 )
{
*a1 = 0LL;
a1[127] = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 1024) >> 3));
result = readserver(a1, a3);
if ( (_DWORD)result == -1 )
break;
if ( (unsigned int)strfind(a1, "is connecting from") )
return 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, const char *a2, unsigned int a3)
{
size_t v4; // rax
size_t v5; // rax
__int64 result; // rax
int vars0[266]; // [rsp+0h] [rbp+0h] BYREF

memset(vars0, 0, 0x400uLL);
qmemcpy(vars0, "WHOIS ", 6);
v4 = strnlen((const char *)vars0, 0x400uLL);
strncat((char *)vars0, a2, 1023 - v4);
v5 = strnlen((const char *)vars0, 0x400uLL);
strncat((char *)vars0, "\r\n", 1023 - v5);
result = writeserver(vars0, a3);
if ( (_DWORD)result != -1 )
{
while ( 1 )
{
*a1 = 0LL;
a1[127] = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 1024) >> 3));
result = readserver(a1, a3);
if ( (_DWORD)result == -1 )
break;
if ( (unsigned int)strfind(a1, "is connecting from") )
return 0LL;
}
}
return result;
}

original_code:
int getIP(char *ip, char *nick, int sckt)
{
    char send[1024];
    memset(send, 0, 1024);
    snprintf(send, 1024, "%s", "WHOIS ");
    strncat(send, nick, 1024 -strnlen(send, 1024)-1);
    strncat(send, "\r\n", 1024 -strnlen(send, 1024)-1);
    if (writeserver(send, sckt) == -1) {
        return -1;
    }
    while (1 == 1) {
        memset(ip, 0, 1024);
        if (readserver(ip, sckt) == -1) {
            return -1;
        }
        if (strfind(ip, "is connecting from")) {
            break;
        }
    }
    return 0;
}

variable_types:
ip
char *

variable_types:
nick
char *