playroutine

decompiled_code:
__int16 playroutine()
{
__int16 *v0; // r13
__int64 j; // r12
__int16 *v2; // rbp
__int64 v3; // rbx
__int16 *v4; // rbp
unsigned int i; // ebx
int v6; // edx
__int16 v7; // cx
__int16 v8; // ax
__int16 v9; // cx
char v10; // dl
__int16 v11; // ax
__int16 result; // ax
unsigned __int8 v13; // di
char v14; // al
int v15; // esi
int v16; // edx
char v17; // al
unsigned __int8 v18; // [rsp+Ah] [rbp-2Eh] BYREF
char v19; // [rsp+Bh] [rbp-2Dh]
unsigned __int8 v20; // [rsp+Ch] [rbp-2Ch]

if ( __PAIR16__(playsong, playtrack) )
{
if ( trackwait )
{
--trackwait;
goto LABEL_4;
}
trackwait = 4;
if ( trackpos )
goto LABEL_10;
if ( !playsong )
goto LABEL_9;
if ( (unsigned __int8)songpos >= songlen )
{
playsong = 0;
LABEL_9:
if ( !playtrack )
goto LABEL_4;
goto LABEL_10;
}
v4 = channel;
for ( i = 0; i != 4; ++i )
{
readsong((unsigned __int8)songpos, i, &v18);
*(_BYTE *)v4 = v18;
*((_BYTE *)v4 + 1) = v19;
v4 += 12;
}
++songpos;
if ( __PAIR16__(playsong, playtrack) )
{
LABEL_10:
v2 = channel;
v3 = 0LL;
while ( !*(_BYTE *)v2 )
{
LABEL_45:
++v3;
v2 += 12;
if ( v3 == 4 )
{
trackpos = (trackpos + 1) & 0x1F;
goto LABEL_4;
}
}
readtrack(*(unsigned __int8 *)v2, (unsigned __int8)trackpos, &v18);
if ( v18 )
{
*((_BYTE *)v2 + 2) = *((_BYTE *)v2 + 1) + v18;
v17 = v19;
if ( !v19 )
{
v17 = *((_BYTE *)v2 + 3);
if ( !v17 )
{
LABEL_43:
if ( v20 )
runcmd(v3, v20);
goto LABEL_45;
}
}
}
else
{
v17 = v19;
if ( !v19 )
goto LABEL_43;
}
*((_BYTE *)v2 + 3) = v17;
*((_BYTE *)v2 + 4) = v17;
*((_BYTE *)v2 + 5) = 0;
*((_BYTE *)v2 + 6) = 0;
v2[5] = 0;
*((_BYTE *)v2 + 8) = 0;
*((_BYTE *)v2 + 12) = 0;
v2[7] = 0;
*((_BYTE *)v2 + 16) = 0;
goto LABEL_43;
}
}
LABEL_4:
v0 = channel;
for ( j = 0LL; j != 4; ++j )
{
v13 = *((_BYTE *)v0 + 4);
if ( v13 )
{
while ( 1 )
{
v14 = *((_BYTE *)v0 + 6);
if ( v14 )
break;
readinstr(v13, *((unsigned __int8 *)v0 + 5), &v18);
++*((_BYTE *)v0 + 5);
runcmd(j, v18);
v13 = *((_BYTE *)v0 + 4);
if ( !v13 )
goto LABEL_33;
}
LABEL_16:
*((_BYTE *)v0 + 6) = v14 - 1;
goto LABEL_17;
}
LABEL_33:
v14 = *((_BYTE *)v0 + 6);
if ( v14 )
goto LABEL_16;
LABEL_17:
v6 = (unsigned __int16)v0[10];
if ( (_WORD)v6 )
{
v7 = v0[11];
v8 = *((_WORD *)&freqtable + *((unsigned __int8 *)v0 + 7)) - v7;
if ( v8 <= 0 )
{
if ( (__int16)(*((_WORD *)&freqtable + *((unsigned __int8 *)v0 + 7)) - v7) < 0 )
{
v15 = -(__int16)v6;
v16 = -v6;
if ( v8 < v15 )
v8 = v16;
}
}
else if ( v8 > (__int16)v6 )
{
v8 = v0[10];
}
v9 = v8 + v7;
v0[11] = v9;
}
else
{
v9 = *((_WORD *)&freqtable + *((unsigned __int8 *)v0 + 7));
}
v10 = *((_BYTE *)v0 + 18);
osc[4 * (int)j] = v9 + v0[5] + ((*((unsigned __int8 *)v0 + 16) * *((char *)&sinetable + (v10 & 0x3F))) >> 2);
v0[5] += *((char *)v0 + 8);
v11 = HIBYTE(osc[4 * (int)j + 3]) + *((char *)v0 + 12);
if ( v11 > 255 )
v11 = 255;
if ( v11 < 0 )
LOBYTE(v11) = 0;
HIBYTE(osc[4 * (int)j + 3]) = v11;
result = v0[7] + osc[4 * (int)j + 2];
if ( (unsigned __int16)result > 0xE000u )
{
result = 0x2000;
}
else if ( (unsigned __int16)result <= 0x1FFFu )
{
result = -8192;
}
osc[4 * (int)j + 2] = result;
*((_BYTE *)v0 + 18) = *((_BYTE *)v0 + 17) + v10;
v0 += 12;
}
return result;
}

canonical_decompiled_code:
__int16 func5()
{
__int16 *v0; // r13
__int64 j; // r12
__int16 *v2; // rbp
__int64 v3; // rbx
__int16 *v4; // rbp
unsigned int i; // ebx
int v6; // edx
__int16 v7; // cx
__int16 v8; // ax
__int16 v9; // cx
char v10; // dl
__int16 v11; // ax
__int16 result; // ax
unsigned __int8 v13; // di
char v14; // al
int v15; // esi
int v16; // edx
char v17; // al
unsigned __int8 v18; // [rsp+Ah] [rbp-2Eh] BYREF
char v19; // [rsp+Bh] [rbp-2Dh]
unsigned __int8 v20; // [rsp+Ch] [rbp-2Ch]

if ( __PAIR16__(playsong, playtrack) )
{
if ( trackwait )
{
--trackwait;
goto LABEL_4;
}
trackwait = 4;
if ( trackpos )
goto LABEL_10;
if ( !playsong )
goto LABEL_9;
if ( (unsigned __int8)songpos >= songlen )
{
playsong = 0;
LABEL_9:
if ( !playtrack )
goto LABEL_4;
goto LABEL_10;
}
v4 = channel;
for ( i = 0; i != 4; ++i )
{
readsong((unsigned __int8)songpos, i, &v18);
*(_BYTE *)v4 = v18;
*((_BYTE *)v4 + 1) = v19;
v4 += 12;
}
++songpos;
if ( __PAIR16__(playsong, playtrack) )
{
LABEL_10:
v2 = channel;
v3 = 0LL;
while ( !*(_BYTE *)v2 )
{
LABEL_45:
++v3;
v2 += 12;
if ( v3 == 4 )
{
trackpos = (trackpos + 1) & 0x1F;
goto LABEL_4;
}
}
readtrack(*(unsigned __int8 *)v2, (unsigned __int8)trackpos, &v18);
if ( v18 )
{
*((_BYTE *)v2 + 2) = *((_BYTE *)v2 + 1) + v18;
v17 = v19;
if ( !v19 )
{
v17 = *((_BYTE *)v2 + 3);
if ( !v17 )
{
LABEL_43:
if ( v20 )
func1(v3, v20);
goto LABEL_45;
}
}
}
else
{
v17 = v19;
if ( !v19 )
goto LABEL_43;
}
*((_BYTE *)v2 + 3) = v17;
*((_BYTE *)v2 + 4) = v17;
*((_BYTE *)v2 + 5) = 0;
*((_BYTE *)v2 + 6) = 0;
v2[5] = 0;
*((_BYTE *)v2 + 8) = 0;
*((_BYTE *)v2 + 12) = 0;
v2[7] = 0;
*((_BYTE *)v2 + 16) = 0;
goto LABEL_43;
}
}
LABEL_4:
v0 = channel;
for ( j = 0LL; j != 4; ++j )
{
v13 = *((_BYTE *)v0 + 4);
if ( v13 )
{
while ( 1 )
{
v14 = *((_BYTE *)v0 + 6);
if ( v14 )
break;
readinstr(v13, *((unsigned __int8 *)v0 + 5), &v18);
++*((_BYTE *)v0 + 5);
func1(j, v18);
v13 = *((_BYTE *)v0 + 4);
if ( !v13 )
goto LABEL_33;
}
LABEL_16:
*((_BYTE *)v0 + 6) = v14 - 1;
goto LABEL_17;
}
LABEL_33:
v14 = *((_BYTE *)v0 + 6);
if ( v14 )
goto LABEL_16;
LABEL_17:
v6 = (unsigned __int16)v0[10];
if ( (_WORD)v6 )
{
v7 = v0[11];
v8 = *((_WORD *)&freqtable + *((unsigned __int8 *)v0 + 7)) - v7;
if ( v8 <= 0 )
{
if ( (__int16)(*((_WORD *)&freqtable + *((unsigned __int8 *)v0 + 7)) - v7) < 0 )
{
v15 = -(__int16)v6;
v16 = -v6;
if ( v8 < v15 )
v8 = v16;
}
}
else if ( v8 > (__int16)v6 )
{
v8 = v0[10];
}
v9 = v8 + v7;
v0[11] = v9;
}
else
{
v9 = *((_WORD *)&freqtable + *((unsigned __int8 *)v0 + 7));
}
v10 = *((_BYTE *)v0 + 18);
osc[4 * (int)j] = v9 + v0[5] + ((*((unsigned __int8 *)v0 + 16) * *((char *)&sinetable + (v10 & 0x3F))) >> 2);
v0[5] += *((char *)v0 + 8);
v11 = HIBYTE(osc[4 * (int)j + 3]) + *((char *)v0 + 12);
if ( v11 > 255 )
v11 = 255;
if ( v11 < 0 )
LOBYTE(v11) = 0;
HIBYTE(osc[4 * (int)j + 3]) = v11;
result = v0[7] + osc[4 * (int)j + 2];
if ( (unsigned __int16)result > 0xE000u )
{
result = 0x2000;
}
else if ( (unsigned __int16)result <= 0x1FFFu )
{
result = -8192;
}
osc[4 * (int)j + 2] = result;
*((_BYTE *)v0 + 18) = *((_BYTE *)v0 + 17) + v10;
v0 += 12;
}
return result;
}

original_code:
void playroutine() {
 u8 ch;
 if(playtrack || playsong) {
  if(trackwait) {
   trackwait--;
  } else {
   trackwait = 4;
   if(!trackpos) {
    if(playsong) {
     if(songpos >= songlen) {
      playsong = 0;
     } else {
      for(ch = 0; ch < 4; ch++) {
       u8 tmp[2];
       readsong(songpos, ch, tmp);
       channel[ch].tnum = tmp[0];
       channel[ch].transp = tmp[1];
      }
      songpos++;
     }
    }
   }
   if(playtrack || playsong) {
    for(ch = 0; ch < 4; ch++) {
     if(channel[ch].tnum) {
      struct trackline tl;
      u8 instr = 0;
      readtrack(channel[ch].tnum, trackpos, &tl);
      if(tl.note) {
       channel[ch].tnote = tl.note + channel[ch].transp;
       instr = channel[ch].lastinstr;
      }
      if(tl.instr) {
       instr = tl.instr;
      }
      if(instr) {
       channel[ch].lastinstr = instr;
       channel[ch].inum = instr;
       channel[ch].iptr = 0;
       channel[ch].iwait = 0;
       channel[ch].bend = 0;
       channel[ch].bendd = 0;
       channel[ch].volumed = 0;
       channel[ch].dutyd = 0;
       channel[ch].vdepth = 0;
      }
      if(tl.cmd[0])
       runcmd(ch, tl.cmd[0], tl.param[0]);
     }
    }
    trackpos++;
    trackpos &= 31;
   }
  }
 }
 for(ch = 0; ch < 4; ch++) {
  s16 vol;
  u16 duty;
  u16 slur;
  while(channel[ch].inum && !channel[ch].iwait) {
   u8 il[2];
   readinstr(channel[ch].inum, channel[ch].iptr, il);
   channel[ch].iptr++;
   runcmd(ch, il[0], il[1]);
  }
  if(channel[ch].iwait) channel[ch].iwait--;
  if(channel[ch].inertia) {
   s16 diff;
   slur = channel[ch].slur;
   diff = freqtable[channel[ch].inote] - slur;
   if(diff > 0) {
    if(diff > channel[ch].inertia) diff = channel[ch].inertia;
   } else if(diff < 0) {
    if(diff < -channel[ch].inertia) diff = -channel[ch].inertia;
   }
   slur += diff;
   channel[ch].slur = slur;
  } else {
   slur = freqtable[channel[ch].inote];
  }
  osc[ch].freq =
   slur +
   channel[ch].bend +
   ((channel[ch].vdepth * sinetable[channel[ch].vpos & 63]) >> 2);
  channel[ch].bend += channel[ch].bendd;
  vol = osc[ch].volume + channel[ch].volumed;
  if(vol < 0) vol = 0;
  if(vol > 255) vol = 255;
  osc[ch].volume = vol;
  duty = osc[ch].duty + channel[ch].dutyd;
  if(duty > 0xe000) duty = 0x2000;
  if(duty < 0x2000) duty = 0xe000;
  osc[ch].duty = duty;
  channel[ch].vpos += channel[ch].vrate;
 }
}

canonical_original_code:
void playroutine() {
 unsigned char ch;
 if(playtrack || playsong) {
  if(trackwait) {
   trackwait--;
  } else {
   trackwait = 4;
   if(!trackpos) {
    if(playsong) {
     if(songpos >= songlen) {
      playsong = 0;
     } else {
      for(ch = 0; ch < 4; ch++) {
       unsigned char tmp[2];
       readsong(songpos, ch, tmp);
       channel[ch].tnum = tmp[0];
       channel[ch].transp = tmp[1];
      }
      songpos++;
     }
    }
   }
   if(playtrack || playsong) {
    for(ch = 0; ch < 4; ch++) {
     if(channel[ch].tnum) {
      struct trackline tl;
      unsigned char instr = 0;
      readtrack(channel[ch].tnum, trackpos, &tl);
      if(tl.note) {
       channel[ch].tnote = tl.note + channel[ch].transp;
       instr = channel[ch].lastinstr;
      }
      if(tl.instr) {
       instr = tl.instr;
      }
      if(instr) {
       channel[ch].lastinstr = instr;
       channel[ch].inum = instr;
       channel[ch].iptr = 0;
       channel[ch].iwait = 0;
       channel[ch].bend = 0;
       channel[ch].bendd = 0;
       channel[ch].volumed = 0;
       channel[ch].dutyd = 0;
       channel[ch].vdepth = 0;
      }
      if(tl.cmd[0])
       runcmd(ch, tl.cmd[0], tl.param[0]);
     }
    }
    trackpos++;
    trackpos &= 31;
   }
  }
 }
 for(ch = 0; ch < 4; ch++) {
  short vol;
  unsigned short duty;
  unsigned short slur;
  while(channel[ch].inum && !channel[ch].iwait) {
   unsigned char il[2];
   readinstr(channel[ch].inum, channel[ch].iptr, il);
   channel[ch].iptr++;
   runcmd(ch, il[0], il[1]);
  }
  if(channel[ch].iwait) channel[ch].iwait--;
  if(channel[ch].inertia) {
   short diff;
   slur = channel[ch].slur;
   diff = freqtable[channel[ch].inote] - slur;
   if(diff > 0) {
    if(diff > channel[ch].inertia) diff = channel[ch].inertia;
   } else if(diff < 0) {
    if(diff < -channel[ch].inertia) diff = -channel[ch].inertia;
   }
   slur += diff;
   channel[ch].slur = slur;
  } else {
   slur = freqtable[channel[ch].inote];
  }
  osc[ch].freq =
   slur +
   channel[ch].bend +
   ((channel[ch].vdepth * sinetable[channel[ch].vpos & 63]) >> 2);
  channel[ch].bend += channel[ch].bendd;
  vol = osc[ch].volume + channel[ch].volumed;
  if(vol < 0) vol = 0;
  if(vol > 255) vol = 255;
  osc[ch].volume = vol;
  duty = osc[ch].duty + channel[ch].dutyd;
  if(duty > 0xe000) duty = 0x2000;
  if(duty < 0x2000) duty = 0xe000;
  osc[ch].duty = duty;
  channel[ch].vpos += channel[ch].vrate;
 }
}

variable_types:
tmp
unsigned char [2]

variable_types: struct trackline { unsigned char note; unsigned char instr; unsigned char cmd[2]; unsigned char param[2]; }

variable_types:
instr
unsigned char

variable_types: vol short

variable_types:
duty
unsigned short

variable_types:
slur
unsigned short

variable_types:
il
unsigned char [2]

variable_types: diff short


initchip

repo: nwf/hyperbola-chiptunes

decompiled_code:
void initchip()
{
trackwait = 0;
trackpos = 0;
playsong = 0;
playtrack = 0;
HIBYTE(osc[3]) = 0;
LOBYTE(channel[2]) = 0;
HIBYTE(osc[7]) = 0;
LOBYTE(channel[14]) = 0;
HIBYTE(osc[11]) = 0;
LOBYTE(channel[26]) = 0;
HIBYTE(osc[15]) = 0;
LOBYTE(channel[38]) = 0;
}

canonical_decompiled_code:
void func6()
{
trackwait = 0;
trackpos = 0;
playsong = 0;
playtrack = 0;
HIBYTE(osc[3]) = 0;
LOBYTE(channel[2]) = 0;
HIBYTE(osc[7]) = 0;
LOBYTE(channel[14]) = 0;
HIBYTE(osc[11]) = 0;
LOBYTE(channel[26]) = 0;
HIBYTE(osc[15]) = 0;
LOBYTE(channel[38]) = 0;
}

original_code:
void initchip() {
 trackwait = 0;
 trackpos = 0;
 playsong = 0;
 playtrack = 0;
 osc[0].volume = 0;
 channel[0].inum = 0;
 osc[1].volume = 0;
 channel[1].inum = 0;
 osc[2].volume = 0;
 channel[2].inum = 0;
 osc[3].volume = 0;
 channel[3].inum = 0;
}

return_type: void


interrupthandler

decompiled_code:
__int64 interrupthandler()
{
unsigned int v0; // eax
char v1; // di
int v2; // ecx
int v3; // esi
bool v4; // zf
char v5; // al
unsigned __int8 v6; // al

v0 = (unsigned int)noiseseed_0 >> 31;
if ( (noiseseed_0 & 0x1000000) != 0 )
LOBYTE(v0) = noiseseed_0 >= 0;
if ( (noiseseed_0 & 0x40) != 0 )
LOBYTE(v0) = v0 ^ 1;
if ( (noiseseed_0 & 0x200) != 0 )
LOBYTE(v0) = v0 ^ 1;
noiseseed_0 = (2 * noiseseed_0) | (unsigned __int8)v0;
if ( callbackwait )
{
--callbackwait;
}
else
{
playroutine();
callbackwait = -77;
}
v1 = (noiseseed_0 & 0x3F) - 32;
v2 = 0;
v3 = 0;
do
{
v6 = osc[4 * v2 + 3];
if ( v6 == 2 )
{
v5 = (unsigned __int16)osc[4 * v2 + 2] < (unsigned __int16)osc[4 * v2 + 1] ? -32 : 31;
}
else if ( v6 > 2u )
{
v4 = v6 == 3;
v5 = 0;
if ( v4 )
v5 = v1;
}
else if ( v6 )
{
v5 = ((unsigned __int16)osc[4 * v2 + 1] >> 10) - 32;
}
else if ( osc[4 * v2 + 1] < 0 )
{
v5 = 31 - (((unsigned __int16)osc[4 * v2 + 1] - 0x8000) >> 9);
}
else
{
v5 = ((unsigned __int16)osc[4 * v2 + 1] >> 9) - 32;
}
osc[4 * v2 + 1] += osc[4 * v2];
v3 += HIBYTE(osc[4 * v2++ + 3]) * v5;
}
while ( v2 != 4 );
LOWORD(v3) = (__int16)v3 >> 8;
return (unsigned int)(v3 - 128);
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // eax
char v1; // di
int v2; // ecx
int v3; // esi
bool v4; // zf
char v5; // al
unsigned __int8 v6; // al

v0 = (unsigned int)noiseseed_0 >> 31;
if ( (noiseseed_0 & 0x1000000) != 0 )
LOBYTE(v0) = noiseseed_0 >= 0;
if ( (noiseseed_0 & 0x40) != 0 )
LOBYTE(v0) = v0 ^ 1;
if ( (noiseseed_0 & 0x200) != 0 )
LOBYTE(v0) = v0 ^ 1;
noiseseed_0 = (2 * noiseseed_0) | (unsigned __int8)v0;
if ( callbackwait )
{
--callbackwait;
}
else
{
func5();
callbackwait = -77;
}
v1 = (noiseseed_0 & 0x3F) - 32;
v2 = 0;
v3 = 0;
do
{
v6 = osc[4 * v2 + 3];
if ( v6 == 2 )
{
v5 = (unsigned __int16)osc[4 * v2 + 2] < (unsigned __int16)osc[4 * v2 + 1] ? -32 : 31;
}
else if ( v6 > 2u )
{
v4 = v6 == 3;
v5 = 0;
if ( v4 )
v5 = v1;
}
else if ( v6 )
{
v5 = ((unsigned __int16)osc[4 * v2 + 1] >> 10) - 32;
}
else if ( osc[4 * v2 + 1] < 0 )
{
v5 = 31 - (((unsigned __int16)osc[4 * v2 + 1] - 0x8000) >> 9);
}
else
{
v5 = ((unsigned __int16)osc[4 * v2 + 1] >> 9) - 32;
}
osc[4 * v2 + 1] += osc[4 * v2];
v3 += HIBYTE(osc[4 * v2++ + 3]) * v5;
}
while ( v2 != 4 );
LOWORD(v3) = (__int16)v3 >> 8;
return (unsigned int)(v3 - 128);
}

original_code:
u8 interrupthandler()
{
 u8 i;
 s16 acc;
 static u32 noiseseed = 1;
 u8 newbit;
 newbit = 0;
 if(noiseseed & 0x80000000L) newbit ^= 1;
 if(noiseseed & 0x01000000L) newbit ^= 1;
 if(noiseseed & 0x00000040L) newbit ^= 1;
 if(noiseseed & 0x00000200L) newbit ^= 1;
 noiseseed = (noiseseed << 1) | newbit;
 if(callbackwait) {
  callbackwait--;
 } else {
  playroutine();
  callbackwait = 180 - 1;
 }
 acc = 0;
 for(i = 0; i < 4; i++) {
  s8 value;
  switch(osc[i].waveform) {
   case 0x00:
    if(osc[i].phase < 0x8000) {
     value = -32 + (osc[i].phase >> 9);
    } else {
     value = 31 - ((osc[i].phase - 0x8000) >> 9);
    }
    break;
   case 0x01:
    value = -32 + (osc[i].phase >> 10);
    break;
   case 0x02:
    value = (osc[i].phase > osc[i].duty)? -32 : 31;
    break;
   case 0x03:
    value = (noiseseed & 63) - 32;
    break;
   default:
    value = 0;
    break;
  }
  osc[i].phase += osc[i].freq;
  acc += value * osc[i].volume;
 }
 return 128 + (acc >> 8);
}

canonical_original_code:
unsigned char interrupthandler()
{
 unsigned char i;
 short acc;
 static unsigned int noiseseed = 1;
 unsigned char newbit;
 newbit = 0;
 if(noiseseed & 0x80000000L) newbit ^= 1;
 if(noiseseed & 0x01000000L) newbit ^= 1;
 if(noiseseed & 0x00000040L) newbit ^= 1;
 if(noiseseed & 0x00000200L) newbit ^= 1;
 noiseseed = (noiseseed << 1) | newbit;
 if(callbackwait) {
  callbackwait--;
 } else {
  playroutine();
  callbackwait = 180 - 1;
 }
 acc = 0;
 for(i = 0; i < 4; i++) {
  char value;
  switch(osc[i].waveform) {
   case 0x00:
    if(osc[i].phase < 0x8000) {
     value = -32 + (osc[i].phase >> 9);
    } else {
     value = 31 - ((osc[i].phase - 0x8000) >> 9);
    }
    break;
   case 0x01:
    value = -32 + (osc[i].phase >> 10);
    break;
   case 0x02:
    value = (osc[i].phase > osc[i].duty)? -32 : 31;
    break;
   case 0x03:
    value = (noiseseed & 63) - 32;
    break;
   default:
    value = 0;
    break;
  }
  osc[i].phase += osc[i].freq;
  acc += value * osc[i].volume;
 }
 return 128 + (acc >> 8);
}

variable_types:
i
unsigned char

variable_types: acc short

variable_types:
noiseseed
unsigned int

variable_types:
newbit
unsigned char

variable_types: value char

return_type: unsigned char


repo: nwf/lofdc

decompiled_code:
int __fastcall serial_mbis(int a1, int a2)
{
int result; // eax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
result = ioctl(a1, 0x5416uLL, v3);
if ( result )
_assert_fail("res == 0", "controller/common-utils.c", 0x12u, "serial_mbis");
return result;
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2)
{
int result; // eax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
result = ioctl(a1, 0x5416uLL, v3);
if ( result )
_assert_fail("res == 0", "controller/common-utils.c", 0x12u, "serial_mbis");
return result;
}

original_code:
void serial_mbis(int fd, int flags) {
  int res = ioctl(fd, 0x5416, &flags);
  ((void) sizeof ((res == 0) ? 1 : 0), __extension__ ({ if (res == 0) ; else __assert_fail ("res == 0", "controller/common-utils.c", 18, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: fd int

variable_types: flags int

variable_types: res int


decompiled_code:
int __fastcall serial_mbic(int a1, int a2)
{
int result; // eax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
result = ioctl(a1, 0x5417uLL, v3);
if ( result )
_assert_fail("res == 0", "controller/common-utils.c", 0x17u, "serial_mbic");
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2)
{
int result; // eax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
result = ioctl(a1, 0x5417uLL, v3);
if ( result )
_assert_fail("res == 0", "controller/common-utils.c", 0x17u, "serial_mbic");
return result;
}

original_code:
void serial_mbic(int fd, int flags) {
  int res = ioctl(fd, 0x5417, &flags);
  ((void) sizeof ((res == 0) ? 1 : 0), __extension__ ({ if (res == 0) ; else __assert_fail ("res == 0", "controller/common-utils.c", 23, __extension__ __PRETTY_FUNCTION__); }));
}


decompiled_code:
int __fastcall serial_init(int fd, int a2)
{
int result; // eax
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

*(_QWORD *)&v3.c_iflag = 0LL;
*(_QWORD *)&v3.c_cflag = 0LL;
*(_QWORD *)&v3.c_line = 0LL;
*(_QWORD *)&v3.c_cc[7] = 0LL;
*(_QWORD *)&v3.c_cc[15] = 0LL;
*(_QWORD *)&v3.c_cc[23] = 0LL;
*(_QWORD *)&v3.c_cc[31] = 0LL;
v3.c_ospeed = 0;
if ( tcgetattr(fd, &v3) )
_assert_fail("res == 0", "controller/common-utils.c", 0x20u, "serial_init");
v3.c_cflag = a2 | v3.c_cflag & 0x7FFFEFF0 | 0xC00;
result = tcsetattr(fd, 1, &v3);
if ( result )
_assert_fail("res == 0", "controller/common-utils.c", 0x24u, "serial_init");
return result;
}

canonical_decompiled_code:
int __fastcall func2(int fd, int a2)
{
int result; // eax
struct termios v3; // [rsp+0h] [rbp-58h] BYREF

*(_QWORD *)&v3.c_iflag = 0LL;
*(_QWORD *)&v3.c_cflag = 0LL;
*(_QWORD *)&v3.c_line = 0LL;
*(_QWORD *)&v3.c_cc[7] = 0LL;
*(_QWORD *)&v3.c_cc[15] = 0LL;
*(_QWORD *)&v3.c_cc[23] = 0LL;
*(_QWORD *)&v3.c_cc[31] = 0LL;
v3.c_ospeed = 0;
if ( tcgetattr(fd, &v3) )
_assert_fail("res == 0", "controller/common-utils.c", 0x20u, "serial_init");
v3.c_cflag = a2 | v3.c_cflag & 0x7FFFEFF0 | 0xC00;
result = tcsetattr(fd, 1, &v3);
if ( result )
_assert_fail("res == 0", "controller/common-utils.c", 0x24u, "serial_init");
return result;
}

original_code:
void serial_init(int fd, int baudtermio) {
  int res;
  struct termios tios;
  memset(&tios, 0, sizeof(tios));
  res = tcgetattr(fd, &tios);
  ((void) sizeof ((res == 0) ? 1 : 0), __extension__ ({ if (res == 0) ; else __assert_fail ("res == 0", "controller/common-utils.c", 32, __extension__ __PRETTY_FUNCTION__); }));
  tios.c_cflag &= ~(020000000000|0010017|0010000);
  tios.c_cflag |= 0002000 | 0004000 | baudtermio;
  res = tcsetattr(fd, 1, &tios);
  ((void) sizeof ((res == 0) ? 1 : 0), __extension__ ({ if (res == 0) ; else __assert_fail ("res == 0", "controller/common-utils.c", 36, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: baudtermio int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int __fastcall serial_deinit(int fd)
{
tcflag_t v1; // eax
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

tcflush(fd, 2);
*(_QWORD *)&v3.c_iflag = 0LL;
*(_QWORD *)&v3.c_cflag = 0LL;
*(_QWORD *)&v3.c_line = 0LL;
*(_QWORD *)&v3.c_cc[7] = 0LL;
*(_QWORD *)&v3.c_cc[15] = 0LL;
*(_QWORD *)&v3.c_cc[23] = 0LL;
*(_QWORD *)&v3.c_cc[31] = 0LL;
v3.c_ospeed = 0;
tcgetattr(fd, &v3);
v1 = v3.c_cflag & 0x7FFFEFF0;
BYTE1(v1) = ((unsigned __int16)(v3.c_cflag & 0xEFF0) >> 8) | 0xC;
v3.c_cflag = v1;
tcsetattr(fd, 2, &v3);
return close(fd);
}

canonical_decompiled_code:
int __fastcall func3(int fd)
{
tcflag_t v1; // eax
struct termios v3; // [rsp+0h] [rbp-48h] BYREF

tcflush(fd, 2);
*(_QWORD *)&v3.c_iflag = 0LL;
*(_QWORD *)&v3.c_cflag = 0LL;
*(_QWORD *)&v3.c_line = 0LL;
*(_QWORD *)&v3.c_cc[7] = 0LL;
*(_QWORD *)&v3.c_cc[15] = 0LL;
*(_QWORD *)&v3.c_cc[23] = 0LL;
*(_QWORD *)&v3.c_cc[31] = 0LL;
v3.c_ospeed = 0;
tcgetattr(fd, &v3);
v1 = v3.c_cflag & 0x7FFFEFF0;
BYTE1(v1) = ((unsigned __int16)(v3.c_cflag & 0xEFF0) >> 8) | 0xC;
v3.c_cflag = v1;
tcsetattr(fd, 2, &v3);
return close(fd);
}

original_code:
void serial_deinit(int fd) {
  tcflush(fd, 2);
  struct termios tios;
  memset(&tios, 0, sizeof(tios));
  tcgetattr(fd, &tios);
  tios.c_cflag &= ~(0010017|0010000|020000000000);
  tios.c_cflag |= 0002000 | 0004000 | 0000000;
  tcsetattr(fd, 2, &tios);
  close(fd);
}

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
void *__fastcall load_obj(char *file, _QWORD *a2, char **a3)
{
void *v4; // rax
void *v5; // rbx
void *v6; // rbp
char *v7; // rax

dlerror();
v4 = dlopen(file, 2);
v5 = v4;
if ( v4 )
{
if ( a2 )
*a2 = v4;
dlerror();
v6 = dlsym(v5, "_plugin_vtable");
v7 = dlerror();
if ( v7 )
{
if ( a3 )
*a3 = v7;
dlclose(v5);
v6 = 0LL;
}
}
else
{
v6 = a3;
if ( a3 )
{
*a3 = dlerror();
v6 = 0LL;
}
}
return v6;
}

canonical_decompiled_code:
void *__fastcall func5(char *file, _QWORD *a2, char **a3)
{
void *v4; // rax
void *v5; // rbx
void *v6; // rbp
char *v7; // rax

dlerror();
v4 = dlopen(file, 2);
v5 = v4;
if ( v4 )
{
if ( a2 )
*a2 = v4;
dlerror();
v6 = dlsym(v5, "_plugin_vtable");
v7 = dlerror();
if ( v7 )
{
if ( a3 )
*a3 = v7;
dlclose(v5);
v6 = 0LL;
}
}
else
{
v6 = a3;
if ( a3 )
{
*a3 = dlerror();
v6 = 0LL;
}
}
return v6;
}

original_code:
void * load_obj(char *fn, void **odl, char **oerr) {
  void *dl;
  char *err;
  dlerror();
  dl = dlopen(fn, 0 | 0x00002);
  if (!dl) {
    if(oerr) { *oerr = dlerror(); }
    goto out_dl;
  }
  if(odl) { *odl = dl; }
  dlerror();
  void *vt = dlsym(dl, "_plugin_vtable");
  err = dlerror();
  if(err) {
    if(oerr) { *oerr = err; }
    goto out_sym;
  }
  return vt;
out_sym:
  dlclose(dl);
out_dl:
  return ((void *)0);
}

canonical_original_code:
void *load_obj(char *fn, void **odl, char **oerr) {
  void *dl;
  char *err;
  dlerror();
  dl = dlopen(fn, 0 | 0x00002);
  if (!dl) {
    if(oerr) { *oerr = dlerror(); }
    goto out_dl;
  }
  if(odl) { *odl = dl; }
  dlerror();
  void *vt = dlsym(dl, "_plugin_vtable");
  err = dlerror();
  if(err) {
    if(oerr) { *oerr = err; }
    goto out_sym;
  }
  return vt;
out_sym:
  dlclose(dl);
out_dl:
  return ((void *)0);
}

variable_types:
fn
char *

variable_types:
odl
void **

variable_types:
oerr
char **

variable_types:
dl
void *

variable_types:
err
char *

variable_types:
vt
void *

return_type: void *


setnonblock

decompiled_code:
int __fastcall setnonblock(int fd)
{
int v1; // eax
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
_assert_fail("fl >= 0", "controller/common-utils.c", 0x72u, "setnonblock");
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, (unsigned int)v1);
if ( result < 0 )
_assert_fail("fl >= 0", "controller/common-utils.c", 0x75u, "setnonblock");
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd)
{
int v1; // eax
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
_assert_fail("fl >= 0", "controller/common-utils.c", 0x72u, "setnonblock");
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, (unsigned int)v1);
if ( result < 0 )
_assert_fail("fl >= 0", "controller/common-utils.c", 0x75u, "setnonblock");
return result;
}

original_code:
void setnonblock(int fd)
{
  int fl;
  fl = fcntl(fd, 3);
  ((void) sizeof ((fl >= 0) ? 1 : 0), __extension__ ({ if (fl >= 0) ; else __assert_fail ("fl >= 0", "controller/common-utils.c", 114, __extension__ __PRETTY_FUNCTION__); }));
  fl |= 04000;
  fl = fcntl(fd, 4, fl);
  ((void) sizeof ((fl >= 0) ? 1 : 0), __extension__ ({ if (fl >= 0) ; else __assert_fail ("fl >= 0", "controller/common-utils.c", 117, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: fl int


decompiled_code:
void __fastcall tdestroy_finalize(void **a1, void (*a2)(void *nodep))
{
void *v2; // rax

v2 = *a1;
*a1 = 0LL;
tdestroy(v2, a2);
}

canonical_decompiled_code:
void __fastcall func7(void **a1, void (*a2)(void *nodep))
{
void *v2; // rax

v2 = *a1;
*a1 = 0LL;
tdestroy(v2, a2);
}

original_code:
void tdestroy_finalize(void **tree, void (*cb)(void *)) {
  void *root = *tree;
  *tree = ((void *)0);
  tdestroy(root, cb);
}

variable_types:
tree
void **

variable_types:
cb
void (*)(void *)

variable_types:
root
void *


decompiled_code:
__int64 __fastcall init_GPIO(unsigned int a1, int a2)
{
int v2; // eax
int v3; // ebx
int v5; // ebx
char buf[2]; // [rsp+Eh] [rbp-8Ah] BYREF
char dest[64]; // [rsp+10h] [rbp-88h] BYREF
char s[72]; // [rsp+50h] [rbp-48h] BYREF

snprintf(s, 0x24uLL, "/sys/class/gpio/gpio%d/", a1);
if ( access(s, 0) == -1 )
{
v5 = open("/sys/class/gpio/export", 513);
if ( v5 == -1 )
{
puts("Error opening gpio/export");
exit(-1);
}
sprintf(buf, "%d", a1);
if ( write(v5, buf, 2uLL) < 0 )
{
printf("Error exporting pin %d\n", a1);
exit(-1);
}
close(v5);
}
else
{
printf("Warning: GPIO pin %d has already been exported\n", a1);
}
strcpy(dest, s);
strcpy(&dest[strlen(dest)], "direction");
v2 = open(dest, 513);
v3 = v2;
if ( v2 == -1 )
{
puts("Error opening gpio/export");
exit(-1);
}
if ( a2 )
{
if ( a2 == 1 && write(v2, "in", 2uLL) < 0 )
{
printf("Error setting pin %d to IN\n", a1);
exit(-1);
}
}
else if ( write(v2, "out", 3uLL) < 0 )
{
printf("Error setting pin %d to OUT\n", a1);
exit(-1);
}
close(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, int a2)
{
int v2; // eax
int v3; // ebx
int v5; // ebx
char buf[2]; // [rsp+Eh] [rbp-8Ah] BYREF
char dest[64]; // [rsp+10h] [rbp-88h] BYREF
char s[72]; // [rsp+50h] [rbp-48h] BYREF

snprintf(s, 0x24uLL, "/sys/class/gpio/gpio%d/", a1);
if ( access(s, 0) == -1 )
{
v5 = open("/sys/class/gpio/export", 513);
if ( v5 == -1 )
{
puts("Error opening gpio/export");
exit(-1);
}
sprintf(buf, "%d", a1);
if ( write(v5, buf, 2uLL) < 0 )
{
printf("Error exporting pin %d\n", a1);
exit(-1);
}
close(v5);
}
else
{
printf("Warning: GPIO pin %d has already been exported\n", a1);
}
strcpy(dest, s);
strcpy(&dest[strlen(dest)], "direction");
v2 = open(dest, 513);
v3 = v2;
if ( v2 == -1 )
{
puts("Error opening gpio/export");
exit(-1);
}
if ( a2 )
{
if ( a2 == 1 && write(v2, "in", 2uLL) < 0 )
{
printf("Error setting pin %d to IN\n", a1);
exit(-1);
}
}
else if ( write(v2, "out", 3uLL) < 0 )
{
printf("Error setting pin %d to OUT\n", a1);
exit(-1);
}
close(v3);
return 0LL;
}

original_code:
int init_GPIO(int pin, int direction){
 char pinDir[36];
 int fd;
 snprintf(pinDir, sizeof pinDir, "/sys/class/gpio/gpio%d/", pin);
 if( access( pinDir, 0 ) != -1 ) {
     printf("Warning: GPIO pin %d has already been exported\n",pin);
 } else {
  if((fd = open("/sys/class/gpio/export", 01|01000))==-1) {
   printf("Error opening gpio/export\n");
   exit(-1);
  }
  char pinb[2];
  sprintf(pinb, "%d", pin);
  if( write(fd, (char*)pinb, 2) < 0){
   printf("Error exporting pin %d\n", pin);
   exit(-1);
  }
  close(fd);
 }
 char opt[64];
 strcpy(opt,pinDir);
 strcat(opt,"direction");
 if((fd = open(opt, 01|01000)) == -1) {
  printf("Error opening gpio/export\n");
  exit(-1);
 }
 if( direction == 0){
  if(write(fd, (char*)"out", 3) < 0){
   printf("Error setting pin %d to OUT\n", pin);
   exit(-1);
  }
 } else if ( direction == 1){
  if(write(fd, (char*)"in", 2) < 0){
   printf("Error setting pin %d to IN\n", pin);
   exit(-1);
  }
 }
 close(fd);
 return 0;
}

canonical_original_code:
int init_GPIO(int pin, int direction){
 char pinDir[36];
 int fd;
 snprintf(pinDir, sizeof pinDir, "/sys/class/gpio/gpio%d/", pin);
 if( access( pinDir, 0 ) != -1 ) {
     printf("Warning: GPIO pin %d has already been exported\n",pin);
 } else {
  if((fd = open("/sys/class/gpio/export", 01|01000))==-1) {
   printf("Error opening gpio/export\n");
   exit(-1);
  }
  char pinb[2];
  sprintf(pinb, "%d", pin);
  if( write(fd, (char *)pinb, 2) < 0){
   printf("Error exporting pin %d\n", pin);
   exit(-1);
  }
  close(fd);
 }
 char opt[64];
 strcpy(opt,pinDir);
 strcat(opt,"direction");
 if((fd = open(opt, 01|01000)) == -1) {
  printf("Error opening gpio/export\n");
  exit(-1);
 }
 if( direction == 0){
  if(write(fd, (char *)"out", 3) < 0){
   printf("Error setting pin %d to OUT\n", pin);
   exit(-1);
  }
 } else if ( direction == 1){
  if(write(fd, (char *)"in", 2) < 0){
   printf("Error setting pin %d to IN\n", pin);
   exit(-1);
  }
 }
 close(fd);
 return 0;
}

variable_types: pin int

variable_types: direction int

variable_types:
pinDir
char [36]

variable_types:
pinb
char [2]

variable_types:
opt
char [64]

return_type: int


decompiled_code:
__int64 __fastcall set_GPIO(unsigned int a1, unsigned int a2)
{
int v3; // edi
__int64 result; // rax
char v5[64]; // [rsp+0h] [rbp-88h] BYREF
char s[72]; // [rsp+40h] [rbp-48h] BYREF

snprintf(s, 0x24uLL, "/sys/class/gpio/gpio%d/", a1);
if ( access(s, 0) == -1 )
{
printf("Error GPIO pin %d has not been initialized\n", a1);
result = 0LL;
}
else
{
strcpy(v5, s);
strcpy(&v5[strlen(v5)], "value");
v3 = open(v5, 513);
if ( v3 == -1 )
{
printf("Error opening gpio/gpio%d/value\n", a1);
exit(-1);
}
if ( a2 == 1 )
{
if ( write(v3, "1", 3uLL) < 0 )
{
printf("Error setting pin %d to OUT\n", a1);
exit(-1);
}
return 0LL;
}
if ( !a2 )
{
if ( write(v3, "0", 3uLL) < 0 )
{
printf("Error setting pin %d to OUT\n", a1);
exit(-1);
}
return 0LL;
}
printf("Error: value %d out of range\n", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, unsigned int a2)
{
int v3; // edi
__int64 result; // rax
char v5[64]; // [rsp+0h] [rbp-88h] BYREF
char s[72]; // [rsp+40h] [rbp-48h] BYREF

snprintf(s, 0x24uLL, "/sys/class/gpio/gpio%d/", a1);
if ( access(s, 0) == -1 )
{
printf("Error GPIO pin %d has not been initialized\n", a1);
result = 0LL;
}
else
{
strcpy(v5, s);
strcpy(&v5[strlen(v5)], "value");
v3 = open(v5, 513);
if ( v3 == -1 )
{
printf("Error opening gpio/gpio%d/value\n", a1);
exit(-1);
}
if ( a2 == 1 )
{
if ( write(v3, "1", 3uLL) < 0 )
{
printf("Error setting pin %d to OUT\n", a1);
exit(-1);
}
return 0LL;
}
if ( !a2 )
{
if ( write(v3, "0", 3uLL) < 0 )
{
printf("Error setting pin %d to OUT\n", a1);
exit(-1);
}
return 0LL;
}
printf("Error: value %d out of range\n", a2);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int set_GPIO(int pin, int val){
 char pinDir[36];
 int fd;
 snprintf(pinDir, sizeof pinDir, "/sys/class/gpio/gpio%d/", pin);
 if( access( pinDir, 0 ) != -1 ) {
  char opt[64];
  strcpy(opt,pinDir);
  strcat(opt,"value");
  if((fd = open(opt, 01|01000)) == -1) {
   printf("Error opening gpio/gpio%d/value\n", pin);
   exit(-1);
  }
  if(val == 1){
   if(write(fd, (char*)"1", 3) < 0){
    printf("Error setting pin %d to OUT\n", pin);
    exit(-1);
   }
  }else if(val == 0){
   if(write(fd, (char*)"0", 3) < 0){
    printf("Error setting pin %d to OUT\n", pin);
    exit(-1);
   }
  } else {
   printf("Error: value %d out of range\n",val);
   return(-1);
  }
 } else {
  printf("Error GPIO pin %d has not been initialized\n",pin);
 }
 return 0;
}

canonical_original_code:
int set_GPIO(int pin, int val){
 char pinDir[36];
 int fd;
 snprintf(pinDir, sizeof pinDir, "/sys/class/gpio/gpio%d/", pin);
 if( access( pinDir, 0 ) != -1 ) {
  char opt[64];
  strcpy(opt,pinDir);
  strcat(opt,"value");
  if((fd = open(opt, 01|01000)) == -1) {
   printf("Error opening gpio/gpio%d/value\n", pin);
   exit(-1);
  }
  if(val == 1){
   if(write(fd, (char *)"1", 3) < 0){
    printf("Error setting pin %d to OUT\n", pin);
    exit(-1);
   }
  }else if(val == 0){
   if(write(fd, (char *)"0", 3) < 0){
    printf("Error setting pin %d to OUT\n", pin);
    exit(-1);
   }
  } else {
   printf("Error: value %d out of range\n",val);
   return(-1);
  }
 } else {
  printf("Error GPIO pin %d has not been initialized\n",pin);
 }
 return 0;
}

variable_types: val int

variable_types: fd int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init_GPIO(0x15u, 0);
set_GPIO(0x15u, 1u);
sleep(5u);
set_GPIO(0x15u, 0);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7(0x15u, 0);
func8(0x15u, 1u);
sleep(5u);
func8(0x15u, 0);
return 0;
}

original_code:
int main(int argc, char **argv){
 init_GPIO(21, 0);
 set_GPIO(21, 1);
 sleep(5);
 set_GPIO(21, 0);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
int log_printf(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
evbuffer_add_vprintf(log_eb, format, arg);
va_end(arg);
va_start(arg, format);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func1(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
evbuffer_add_vprintf(log_eb, format, arg);
va_end(arg);
va_start(arg, format);
return vfprintf(stderr, format, arg);
}

original_code:
void log_printf(const char *fmt, ...) {
  va_list ap;
  __builtin_va_start(ap,fmt);
  evbuffer_add_vprintf(log_eb, fmt, ap);
  __builtin_va_end(ap);
  __builtin_va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  __builtin_va_end(ap);
}

canonical_original_code:
void log_printf(char *fmt, ...) {
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  evbuffer_add_vprintf(log_eb, fmt, ap);
  __builtin_va_end(ap);
  __builtin_va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  __builtin_va_end(ap);
}

variable_types:
fmt
char *


decompiled_code:
void rfid_common_ctor()
{
all_rfid = 0LL;
}

canonical_decompiled_code:
void func1()
{
all_rfid = 0LL;
}

original_code:
static void __attribute__((constructor)) rfid_common_ctor(void) {
  do { (&all_rfid)->lh_first = ((void *)0); } while ( 0);
}

canonical_original_code:
static void rfid_common_ctor() {
  do { (&all_rfid)->lh_first = ((void *)0); } while ( 0);
}


decompiled_code:
void lock_common_ctor()
{
all_lock = 0LL;
}

canonical_decompiled_code:
void func1()
{
all_lock = 0LL;
}

original_code:
static void __attribute__((constructor)) lock_common_ctor(void) {
  do { (&all_lock)->lh_first = ((void *)0); } while ( 0);
}

canonical_original_code:
static void lock_common_ctor() {
  do { (&all_lock)->lh_first = ((void *)0); } while ( 0);
}


isexist

decompiled_code:
__int64 __fastcall isexist(_DWORD *a1, int a2, int a3)
{
_DWORD *v3; // rax

if ( a2 <= 0 )
return 0LL;
v3 = a1;
while ( *v3 != a3 )
{
v3 += 2;
if ( v3 == &a1[2 * (a2 - 1) + 2] )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1, int a2, int a3)
{
_DWORD *v3; // rax

if ( a2 <= 0 )
return 0LL;
v3 = a1;
while ( *v3 != a3 )
{
v3 += 2;
if ( v3 == &a1[2 * (a2 - 1) + 2] )
return 0LL;
}
return 1LL;
}

original_code:
_Bool isexist(PAGE* arr, int size, int value) {
 int i=0;
 for(; i<size; i++) {
  if(arr[i].page_num==value) {
   return 1;
  }
 }
 return 0;
}

canonical_original_code:
_Bool isexist(struct page *arr, int size, int value) {
 int i = 0;
 for(; i<size; i++) {
  if(arr[i].page_num==value) {
   return 1;
  }
 }
 return 0;
}

variable_types: struct page { int page_num; int recently_used; } *

variable_types: size int

variable_types: value int

variable_types: i int


fifo

original_code:
int fifo(int* arr, int size) {
 int pages_size=16/1;
 PAGE* pages=(PAGE*)malloc(sizeof(PAGE)*pages_size);
 int fault_count=0;
 int victim_frame=0;
 memset(pages,-1,sizeof(PAGE)*pages_size);
 int i;
 for(i=0; i<size; i++) {
  int frame_num=arr[i]/1024;
  if(!isexist(pages, pages_size, frame_num)) {
   fault_count++;
   pages[victim_frame].page_num=frame_num;
   pages[victim_frame].recently_used=0;
   victim_frame++;
   if(victim_frame>pages_size) {
    victim_frame=0;
   }
  }
 }
 free(pages);
 return fault_count;
}

canonical_original_code:
int fifo(int *arr, int size) {
 int pages_size = 16/1;
 struct page *pages = (PAGE*)malloc(sizeof(PAGE)*pages_size);
 int fault_count = 0;
 int victim_frame = 0;
 memset(pages,-1,sizeof(PAGE)*pages_size);
 int i;
 for(i=0; i<size; i++) {
  int frame_num = arr[i]/1024;
  if(!isexist(pages, pages_size, frame_num)) {
   fault_count++;
   pages[victim_frame].page_num=frame_num;
   pages[victim_frame].recently_used=0;
   victim_frame++;
   if(victim_frame>pages_size) {
    victim_frame=0;
   }
  }
 }
 free(pages);
 return fault_count;
}

variable_types:
arr
int *

user_defined_types: struct page { int page_num; int recently_used; }


lru

original_code:
int lru(int* arr, int size) {
 int pages_size=16/1;
 PAGE* pages=(PAGE*)malloc(sizeof(PAGE)*pages_size);
 int fault_count=0;
 int victim_frame=0;
 memset(pages,-1,sizeof(PAGE)*pages_size);
 int i;
 for(i=0; i<size; i++) {
  int frame_num=arr[i]/1024;
  if(!isexist(pages, pages_size, frame_num)) {
   fault_count++;
   int j;
   int high_value=0;
   for(j=0; j<pages_size; j++) {
    int temp=i-pages[j].recently_used;
    if(temp>high_value) {
     victim_frame=j;
     high_value=temp;
    }
   }
   pages[victim_frame].page_num=frame_num;
   pages[victim_frame].recently_used=i;
  }else {
   int j;
   for(j=0; j<size; j++) {
    if(pages[j].page_num==frame_num) {
     pages[j].recently_used=i;
     break;
    }
   }
  }
 }
 free(pages);
 return fault_count;
}

canonical_original_code:
int lru(int *arr, int size) {
 int pages_size = 16/1;
 struct page *pages = (PAGE*)malloc(sizeof(PAGE)*pages_size);
 int fault_count = 0;
 int victim_frame = 0;
 memset(pages,-1,sizeof(PAGE)*pages_size);
 int i;
 for(i=0; i<size; i++) {
  int frame_num = arr[i]/1024;
  if(!isexist(pages, pages_size, frame_num)) {
   fault_count++;
   int j;
   int high_value = 0;
   for(j=0; j<pages_size; j++) {
    int temp = i-pages[j].recently_used;
    if(temp>high_value) {
     victim_frame=j;
     high_value=temp;
    }
   }
   pages[victim_frame].page_num=frame_num;
   pages[victim_frame].recently_used=i;
  }else {
   int j;
   for(j=0; j<size; j++) {
    if(pages[j].page_num==frame_num) {
     pages[j].recently_used=i;
     break;
    }
   }
  }
 }
 free(pages);
 return fault_count;
}

variable_types: j int

variable_types: temp int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // esi
int *v5; // rbp
unsigned int v6; // eax
unsigned int v7; // eax

if ( argc <= 2 )
{
fprintf(stderr, "usage: %s [access_mem_count] [seed]\n", *argv);
exit(-1);
}
v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
if ( (unsigned int)(v3 - 100) > 0xF41DC )
{
fwrite("First parameter is wrong range\n", 1uLL, 0x1FuLL, stderr);
exit(-1);
}
v5 = genAddr(v3, v4);
v6 = fifo(v5, v3);
printf("FIFO: %d\n", v6);
v7 = lru((__int64)v5, v3);
printf("LRU: %d\n", v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // esi
int *v5; // rbp
unsigned int v6; // eax
unsigned int v7; // eax

if ( argc <= 2 )
{
fprintf(stderr, "usage: %s [access_mem_count] [seed]\n", *argv);
exit(-1);
}
v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
if ( (unsigned int)(v3 - 100) > 0xF41DC )
{
fwrite("First parameter is wrong range\n", 1uLL, 0x1FuLL, stderr);
exit(-1);
}
v5 = func7(v3, v4);
v6 = func9(v5, v3);
printf("FIFO: %d\n", v6);
v7 = func10((__int64)v5, v3);
printf("LRU: %d\n", v7);
return 0;
}

original_code:
int main(int argc, char** argv) {
    int i;
    int *arr;
    int access_mem_count;
    int seed;
    if(argc<3) {
     fprintf(stderr,"usage: %s [access_mem_count] [seed]\n",argv[0]);
     exit(-1);
    }
    access_mem_count=atoi(argv[1]);
    seed=atoi(argv[2]);
    if(access_mem_count<100||access_mem_count>1000000) {
     fprintf(stderr,"First parameter is wrong range\n");
     exit(-1);
    }
    arr=(int*)malloc(sizeof(int)*access_mem_count);
 arr = genAddr(access_mem_count, seed);
    printf("FIFO: %d\n",fifo(arr,access_mem_count));
    printf("LRU: %d\n",lru(arr,access_mem_count));
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    int i;
    int *arr;
    int access_mem_count;
    int seed;
    if(argc<3) {
     fprintf(stderr,"usage: %s [access_mem_count] [seed]\n",argv[0]);
     exit(-1);
    }
    access_mem_count=atoi(argv[1]);
    seed=atoi(argv[2]);
    if(access_mem_count<100||access_mem_count>1000000) {
     fprintf(stderr,"First parameter is wrong range\n");
     exit(-1);
    }
    arr=(int *)malloc(sizeof(int)*access_mem_count);
 arr = genAddr(access_mem_count, seed);
    printf("FIFO: %d\n",fifo(arr,access_mem_count));
    printf("LRU: %d\n",lru(arr,access_mem_count));
 return 0;
}

variable_types: seed int


optimal

decompiled_code:
void optimal()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
int optimal(int* arr, int size) {
}

canonical_original_code:
int optimal(int *arr, int size) {
}


original_code:
char *my_strstr(char *ptr,char *q)
{
 int lenth = strlen(q);
 int sum = 0;
 int i,j=0;
 for (i = 0; i < strlen(ptr); i++)
 {
  if (ptr[i] == q[j])
  {
   sum++;
   j++;
  }
  else
  {
   sum = 0;
   j = 0;
  }
  if (sum == lenth)
  {
   j = i-lenth+1;
   break;
  }
 }
 return &ptr[j];
}

canonical_original_code:
char *my_strstr(char *ptr, char *q)
{
 int lenth = strlen(q);
 int sum = 0;
 int i; int j = 0;
 for (i = 0; i < strlen(ptr); i++)
 {
  if (ptr[i] == q[j])
  {
   sum++;
   j++;
  }
  else
  {
   sum = 0;
   j = 0;
  }
  if (sum == lenth)
  {
   j = i-lenth+1;
   break;
  }
 }
 return &ptr[j];
}

variable_types:
ptr
char *

variable_types:
q
char *

variable_types: lenth int

variable_types: sum int

return_type: char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char v5[13]; // [rsp+Eh] [rbp-1Ah] BYREF

strcpy(&v5[5], "hahlfjdla df");
printf("a:%s\n", &v5[5]);
strcpy(v5, "hfad");
printf("b:%s\n", v5);
v3 = my_strstr(&v5[5], v5);
puts(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char v5[13]; // [rsp+Eh] [rbp-1Ah] BYREF

strcpy(&v5[5], "hahlfjdla df");
printf("a:%s\n", &v5[5]);
strcpy(v5, "hfad");
printf("b:%s\n", v5);
v3 = func7(&v5[5], v5);
puts(v3);
return 0;
}

original_code:
int main(int argc,char* argv[])
{
 char a[]="hahlfjdla df";
 printf("a:%s\n",a);
 char b[]="hfad";
 printf("b:%s\n",b);
 char *p = my_strstr(a,b);
 printf("%s\n",p);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char a[] = "hahlfjdla df";
 printf("a:%s\n",a);
 char b[] = "hfad";
 printf("b:%s\n",b);
 char *p = my_strstr(a,b);
 printf("%s\n",p);
 return 0;
}

variable_types:
argv
char *[]

variable_types:
a
char []

variable_types:
b
char []

variable_types:
p
char *


execute

decompiled_code:
__int64 __fastcall execute(char **argv)
{
__int64 v2; // rbx
unsigned int v3; // ebp
bool v4; // cf
bool v5; // zf
const char *v6; // rdi
__int64 v7; // rcx
char *v8; // rsi
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned int)fork();
printf("The PID is %d\n", v2);
v3 = 1;
if ( strcmp(*argv, "exit") && strcmp(*argv, "quit") )
{
if ( (int)v2 < 0 )
{
puts("There was an error with not enough memory");
}
else if ( (_DWORD)v2 )
{
printf("I am the parent! PID %d\n", (unsigned int)v2);
wait((__WAIT_STATUS)&stat_loc);
v3 = 0;
}
else
{
printf("I am the child! PID %d\n", 0LL);
v3 = strcmp(*argv, "cd") != 0;
v4 = 0;
v5 = v3 == 0;
if ( v3 )
{
v6 = "history";
v7 = 8LL;
v8 = *argv;
do
{
if ( !v7 )
break;
v4 = (unsigned __int8)*v8 < (unsigned int)*v6;
v5 = *v8++ == *v6++;
--v7;
}
while ( v5 );
v3 = (char)((!v4 && !v5) - v4);
if ( (!v4 && !v5) == v4 )
{
printf("Viewing history!");
view_hist("Viewing history!", v8);
}
else
{
printf("Executing command: %s\n", *argv);
v3 = (unsigned int)execvp(*argv, argv) >> 31;
}
}
else
{
printf("Changing directory to %s\n", argv[1]);
chdir(argv[1]);
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char **argv)
{
__int64 v2; // rbx
unsigned int v3; // ebp
bool v4; // cf
bool v5; // zf
const char *v6; // rdi
__int64 v7; // rcx
char *v8; // rsi
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (unsigned int)fork();
printf("The PID is %d\n", v2);
v3 = 1;
if ( strcmp(*argv, "exit") && strcmp(*argv, "quit") )
{
if ( (int)v2 < 0 )
{
puts("There was an error with not enough memory");
}
else if ( (_DWORD)v2 )
{
printf("I am the parent! PID %d\n", (unsigned int)v2);
wait((__WAIT_STATUS)&stat_loc);
v3 = 0;
}
else
{
printf("I am the child! PID %d\n", 0LL);
v3 = strcmp(*argv, "cd") != 0;
v4 = 0;
v5 = v3 == 0;
if ( v3 )
{
v6 = "history";
v7 = 8LL;
v8 = *argv;
do
{
if ( !v7 )
break;
v4 = (unsigned __int8)*v8 < (unsigned int)*v6;
v5 = *v8++ == *v6++;
--v7;
}
while ( v5 );
v3 = (char)((!v4 && !v5) - v4);
if ( (!v4 && !v5) == v4 )
{
printf("Viewing history!");
view_hist("Viewing history!", v8);
}
else
{
printf("Executing command: %s\n", *argv);
v3 = (unsigned int)execvp(*argv, argv) >> 31;
}
}
else
{
printf("Changing directory to %s\n", argv[1]);
chdir(argv[1]);
}
}
}
return v3;
}

original_code:
int execute(char** argv) {
    int status;
    int exe_r;
    pid_t pid = fork();
    printf("The PID is %d\n", pid);
    if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit")){
        return 1;
    }
    if (pid < 0){
        printf("There was an error with not enough memory\n");
        return 1;
    } else if (pid == 0){
        printf("I am the child! PID %d\n", pid);
        if (!strcmp(argv[0], "cd")){
            printf("Changing directory to %s\n", argv[1]);
            chdir(argv[1]);
        } else if (!strcmp(argv[0], "history")){
            printf("Viewing history!");
            view_hist();
        } else {
            printf("Executing command: %s\n", argv[0]);
            exe_r = execvp(argv[0], argv);
            if(exe_r < 0){
                return 1;
            }
        }
    } else {
        printf("I am the parent! PID %d\n", pid);
        wait(&status);
    }
    return 0;
}

canonical_original_code:
int execute(char **argv) {
    int status;
    int exe_r;
    int pid = fork();
    printf("The PID is %d\n", pid);
    if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit")){
        return 1;
    }
    if (pid < 0){
        printf("There was an error with not enough memory\n");
        return 1;
    } else if (pid == 0){
        printf("I am the child! PID %d\n", pid);
        if (!strcmp(argv[0], "cd")){
            printf("Changing directory to %s\n", argv[1]);
            chdir(argv[1]);
        } else if (!strcmp(argv[0], "history")){
            printf("Viewing history!");
            view_hist();
        } else {
            printf("Executing command: %s\n", argv[0]);
            exe_r = execvp(argv[0], argv);
            if(exe_r < 0){
                return 1;
            }
        }
    } else {
        printf("I am the parent! PID %d\n", pid);
        wait(&status);
    }
    return 0;
}

variable_types:
argv
char **

variable_types: status int

variable_types: pid int


parse

decompiled_code:
char **__fastcall parse(char *a1)
{
char *v1; // rax
char *i; // rsi

v1 = strtok(a1, " ");
strcpy(*cmd, v1);
for ( i = strtok(0LL, " "); i; i = strtok(0LL, " ") )
strcpy(cmd[++num_args], i);
return cmd;
}

canonical_decompiled_code:
char **__fastcall func1(char *a1)
{
char *v1; // rax
char *i; // rsi

v1 = strtok(a1, " ");
strcpy(*cmd, v1);
for ( i = strtok(0LL, " "); i; i = strtok(0LL, " ") )
strcpy(cmd[++num_args], i);
return cmd;
}

original_code:
char** parse(char* input) {
    char *temp = ((void *)0);
    strcpy(cmd[0], strtok(input, " "));
    temp = strtok(((void *)0), " ");
    while (temp != ((void *)0)) {
        num_args++;
        strcpy(cmd[num_args], temp);
        temp = strtok(((void *)0), " ");
    }
    return cmd;
}

canonical_original_code:
char **parse(char *input) {
    char *temp = ((void *)0);
    strcpy(cmd[0], strtok(input, " "));
    temp = strtok(((void *)0), " ");
    while (temp != ((void *)0)) {
        num_args++;
        strcpy(cmd[num_args], temp);
        temp = strtok(((void *)0), " ");
    }
    return cmd;
}

variable_types:
input
char *

variable_types:
temp
char *

return_type: char **


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
char **v4; // rbp
char *v5; // rbx
int v6; // eax
int v7; // ecx
__int64 j; // rdx
char *v9; // rax
const char *v10; // rbp
unsigned int v11; // eax
char v13[104]; // [rsp+0h] [rbp-68h] BYREF

cmd = (char **)malloc(0x280uLL);
for ( i = 0LL; i != 80; ++i )
{
v4 = &cmd[i];
*v4 = (char *)malloc(0x50uLL);
}
home_path = getwd(home_path);
strcat(home_path, "/.history");
init_history(home_path, argv);
v5 = 0LL;
do
{
v5 = getwd(v5);
printf("jdcsh: %s>", v5);
fflush(stdout);
fgets(v13, 80, stdin);
*cmd = *parse(v13);
v10 = cmd[num_args];
v10[(int)strlen(v10) - 1] = 0;
if ( !strcmp((const char *)cmd, "!!") )
{
v6 = get_hist_count();
*cmd = *(char **)get_from_hist((unsigned int)(v6 - 1));
}
else if ( **cmd == 33 )
{
v11 = atoi(*cmd + 1);
*cmd = *(char **)get_from_hist(v11);
}
save_in_hist(cmd, (unsigned int)num_args);
v7 = execute(cmd);
for ( j = 0LL; j != 80; ++j )
{
v9 = cmd[j];
*(_QWORD *)v9 = 0LL;
*((_QWORD *)v9 + 1) = 0LL;
*((_QWORD *)v9 + 2) = 0LL;
*((_QWORD *)v9 + 3) = 0LL;
*((_QWORD *)v9 + 4) = 0LL;
*((_QWORD *)v9 + 5) = 0LL;
*((_QWORD *)v9 + 6) = 0LL;
*((_QWORD *)v9 + 7) = 0LL;
*((_QWORD *)v9 + 8) = 0LL;
*((_QWORD *)v9 + 9) = 0LL;
}
}
while ( !v7 );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
char **v4; // rbp
char *v5; // rbx
int v6; // eax
int v7; // ecx
__int64 j; // rdx
char *v9; // rax
const char *v10; // rbp
unsigned int v11; // eax
char v13[104]; // [rsp+0h] [rbp-68h] BYREF

cmd = (char **)malloc(0x280uLL);
for ( i = 0LL; i != 80; ++i )
{
v4 = &cmd[i];
*v4 = (char *)malloc(0x50uLL);
}
home_path = getwd(home_path);
strcat(home_path, "/.history");
init_history(home_path, argv);
v5 = 0LL;
do
{
v5 = getwd(v5);
printf("jdcsh: %s>", v5);
fflush(stdout);
fgets(v13, 80, stdin);
*cmd = *func1(v13);
v10 = cmd[num_args];
v10[(int)strlen(v10) - 1] = 0;
if ( !strcmp((const char *)cmd, "!!") )
{
v6 = get_hist_count();
*cmd = *(char **)get_from_hist((unsigned int)(v6 - 1));
}
else if ( **cmd == 33 )
{
v11 = atoi(*cmd + 1);
*cmd = *(char **)get_from_hist(v11);
}
save_in_hist(cmd, (unsigned int)num_args);
v7 = func0(cmd);
for ( j = 0LL; j != 80; ++j )
{
v9 = cmd[j];
*(_QWORD *)v9 = 0LL;
*((_QWORD *)v9 + 1) = 0LL;
*((_QWORD *)v9 + 2) = 0LL;
*((_QWORD *)v9 + 3) = 0LL;
*((_QWORD *)v9 + 4) = 0LL;
*((_QWORD *)v9 + 5) = 0LL;
*((_QWORD *)v9 + 6) = 0LL;
*((_QWORD *)v9 + 7) = 0LL;
*((_QWORD *)v9 + 8) = 0LL;
*((_QWORD *)v9 + 9) = 0LL;
}
}
while ( !v7 );
return 0;
}

original_code:
int main(void) {
    char *cwd = ((void *)0);
    int should_run = 1;
    int history_cmd;
    int i;
    char input[80];
    cmd = (char **) malloc(80*sizeof(char *));
    for(i = 0; i < 80; i++) {
        cmd[i] = (char *) malloc(80*sizeof(char));
    }
    home_path = getwd(home_path);
    strcat(home_path, "/.history");
    init_history(home_path);
    while (should_run){
        cwd = getwd(cwd);
        printf("jdcsh: %s>", cwd);
        fflush(stdout);
        fgets(input, 80, stdin);
        memcpy(cmd , parse(input), sizeof(cmd));
        int lcmd_len = (int) strlen(cmd[num_args]);
        cmd[num_args][lcmd_len-1] = '\0';
        if(!strcmp(cmd, "!!")) {
            memcpy(cmd, get_from_hist(get_hist_count() - 1), sizeof(cmd));
        } else if(cmd[0][0] == '!') {
            history_cmd = atoi(cmd[0] + 1);
            memcpy(cmd, get_from_hist(history_cmd), sizeof(cmd));
        }
        save_in_hist(cmd, num_args);
        should_run = !execute(cmd);
        for(i = 0; i < 80; i++) {
            memset(cmd[i], '\0', 80);
        }
    }
    return 0;
}

canonical_original_code:
int main() {
    char *cwd = ((void *)0);
    int should_run = 1;
    int history_cmd;
    int i;
    char input[80];
    cmd = (char **) malloc(80*sizeof(char *));
    for(i = 0; i < 80; i++) {
        cmd[i] = (char *) malloc(80*sizeof(char));
    }
    home_path = getwd(home_path);
    strcat(home_path, "/.history");
    init_history(home_path);
    while (should_run){
        cwd = getwd(cwd);
        printf("jdcsh: %s>", cwd);
        fflush(stdout);
        fgets(input, 80, stdin);
        memcpy(cmd , parse(input), sizeof(cmd));
        int lcmd_len = (int) strlen(cmd[num_args]);
        cmd[num_args][lcmd_len-1] = '\0';
        if(!strcmp(cmd, "!!")) {
            memcpy(cmd, get_from_hist(get_hist_count() - 1), sizeof(cmd));
        } else if(cmd[0][0] == '!') {
            history_cmd = atoi(cmd[0] + 1);
            memcpy(cmd, get_from_hist(history_cmd), sizeof(cmd));
        }
        save_in_hist(cmd, num_args);
        should_run = !execute(cmd);
        for(i = 0; i < 80; i++) {
            memset(cmd[i], '\0', 80);
        }
    }
    return 0;
}

variable_types:
cwd
char *

variable_types: i int

variable_types:
input
char [80]


decompiled_code:
void *__fastcall init_history(char *src)
{
char *v1; // rbx
__int64 i; // rbx
_QWORD *v3; // rbp
void *result; // rax

v1 = (char *)malloc(0x50uLL);
history_file_path = v1;
strcpy(v1, src);
h_count = 0;
history_file = fopen(v1, "w");
if ( !history_file )
fwrite("history file could not open\n", 1uLL, 0x1CuLL, stderr);
fclose(history_file);
cmd = malloc(0x280uLL);
for ( i = 0LL; i != 640; i += 8LL )
{
v3 = (char *)cmd + i;
result = malloc(0x50uLL);
*v3 = result;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(char *src)
{
char *v1; // rbx
__int64 i; // rbx
_QWORD *v3; // rbp
void *result; // rax

v1 = (char *)malloc(0x50uLL);
history_file_path = v1;
strcpy(v1, src);
h_count = 0;
history_file = fopen(v1, "w");
if ( !history_file )
fwrite("history file could not open\n", 1uLL, 0x1CuLL, stderr);
fclose(history_file);
cmd = malloc(0x280uLL);
for ( i = 0LL; i != 640; i += 8LL )
{
v3 = (char *)cmd + i;
result = malloc(0x50uLL);
*v3 = result;
}
return result;
}

original_code:
void init_history(char* file_path) {
    int i;
    history_file_path = (char *) malloc(80*sizeof(char));
    strcpy(history_file_path, file_path);
    h_count = 0;
    history_file = fopen(history_file_path, "w");
    if (!history_file){
        fprintf(stderr, "history file could not open\n");
    }
    fclose(history_file);
    cmd = (char **) malloc(80*sizeof(char *));
    for(i = 0; i < 80; i++) {
        cmd[i] = (char *) malloc(80*sizeof(char));
    }
}

canonical_original_code:
void init_history(char *file_path) {
    int i;
    history_file_path = (char *) malloc(80*sizeof(char));
    strcpy(history_file_path, file_path);
    h_count = 0;
    history_file = fopen(history_file_path, "w");
    if (!history_file){
        fprintf(stderr, "history file could not open\n");
    }
    fclose(history_file);
    cmd = (char **) malloc(80*sizeof(char *));
    for(i = 0; i < 80; i++) {
        cmd[i] = (char *) malloc(80*sizeof(char));
    }
}


decompiled_code:
void reclaim_history_memory()
{
__int64 i; // rbx

for ( i = 0LL; i != 640; i += 8LL )
{
free(*(void **)((char *)cmd + i));
*(_QWORD *)((char *)cmd + i) = 0LL;
}
free(cmd);
cmd = 0LL;
}

canonical_decompiled_code:
void func1()
{
__int64 i; // rbx

for ( i = 0LL; i != 640; i += 8LL )
{
free(*(void **)((char *)cmd + i));
*(_QWORD *)((char *)cmd + i) = 0LL;
}
free(cmd);
cmd = 0LL;
}

original_code:
void reclaim_history_memory() {
   int i;
   for(i = 0; i < 80; i++) {
        free(cmd[i]);
        cmd[i] = ((void *)0);
    }
    free(cmd);
    cmd = ((void *)0);
}


decompiled_code:
int view_hist()
{
int result; // eax
int v1; // ebx

history_file = fopen(history_file_path, "r");
if ( !history_file )
printf("Could not open the history");
result = putchar(10);
if ( h_count >= 0 )
{
v1 = 0;
do
{
fgets(0LL, 80, history_file);
result = puts(0LL);
++v1;
}
while ( h_count >= v1 );
}
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax
int v1; // ebx

history_file = fopen(history_file_path, "r");
if ( !history_file )
printf("Could not open the history");
result = putchar(10);
if ( h_count >= 0 )
{
v1 = 0;
do
{
fgets(0LL, 80, history_file);
result = puts(0LL);
++v1;
}
while ( h_count >= v1 );
}
return result;
}

original_code:
void view_hist() {
    int i;
    char* temp;
    history_file = fopen(history_file_path, "r");
    if (!history_file){
        printf("Could not open the history");
    }
    printf("\n");
    for (i = 0; i<=h_count; i++){
        fgets(temp, 80, history_file);
        printf("%s\n", temp);
    }
}

canonical_original_code:
void view_hist() {
    int i;
    char *temp;
    history_file = fopen(history_file_path, "r");
    if (!history_file){
        printf("Could not open the history");
    }
    printf("\n");
    for (i = 0; i<=h_count; i++){
        fgets(temp, 80, history_file);
        printf("%s\n", temp);
    }
}


decompiled_code:
__int64 __fastcall save_in_hist(const char **a1, int a2)
{
FILE *v2; // rax
__int64 v3; // rbx
__int64 result; // rax

v2 = fopen(history_file_path, "a");
history_file = v2;
if ( v2 )
{
fprintf(v2, "%d %s ", (unsigned int)h_count, *a1);
if ( a2 > 0 )
{
v3 = 1LL;
do
fprintf(history_file, "%s ", a1[v3++]);
while ( a2 >= (int)v3 );
}
fputc(10, history_file);
fclose(history_file);
++h_count;
result = 0LL;
}
else
{
printf("history read error");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char **a1, int a2)
{
FILE *v2; // rax
__int64 v3; // rbx
__int64 result; // rax

v2 = fopen(history_file_path, "a");
history_file = v2;
if ( v2 )
{
fprintf(v2, "%d %s ", (unsigned int)h_count, *a1);
if ( a2 > 0 )
{
v3 = 1LL;
do
fprintf(history_file, "%s ", a1[v3++]);
while ( a2 >= (int)v3 );
}
fputc(10, history_file);
fclose(history_file);
++h_count;
result = 0LL;
}
else
{
printf("history read error");
result = 1LL;
}
return result;
}

original_code:
int save_in_hist(char** cmd, int args) {
    int i;
    history_file = fopen(history_file_path, "a");
    if (!history_file){
        printf("history read error");
        return 1;
    }
    fprintf (history_file, "%d %s ", h_count, cmd[0]);
    for (i=1; i<=args; i++){
        fprintf (history_file, "%s ", cmd[i]);
    }
    fprintf (history_file, "\n");
    fclose(history_file);
    h_count++;
    return 0;
}

canonical_original_code:
int save_in_hist(char **cmd, int args) {
    int i;
    history_file = fopen(history_file_path, "a");
    if (!history_file){
        printf("history read error");
        return 1;
    }
    fprintf (history_file, "%d %s ", h_count, cmd[0]);
    for (i=1; i<=args; i++){
        fprintf (history_file, "%s ", cmd[i]);
    }
    fprintf (history_file, "\n");
    fclose(history_file);
    h_count++;
    return 0;
}

variable_types:
cmd
char **

variable_types: args int


decompiled_code:
__int64 get_hist_count()
{
return (unsigned int)h_count;
}

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

original_code:
int get_hist_count() {
    return h_count;
}


decompiled_code:
void *__fastcall get_from_hist(int a1)
{
int i; // ebx
char *v2; // rax
char *v3; // rsi
__int64 v4; // rbx

history_file = fopen(history_file_path, "r");
if ( !history_file )
{
puts("History Read Error:");
puts("Those who forget their history are doomed to repeat it...");
}
if ( a1 >= 0 )
{
for ( i = 0; i <= a1; ++i )
fgets(0LL, 80, history_file);
}
fclose(history_file);
strtok(0LL, " ");
v2 = strtok(0LL, " ");
strcpy(*(char **)cmd, v2);
v3 = strtok(0LL, " ");
if ( v3 )
{
v4 = 8LL;
do
{
strcpy(*(char **)((char *)cmd + v4), v3);
v3 = strtok(0LL, " ");
v4 += 8LL;
}
while ( v3 );
}
return cmd;
}

canonical_decompiled_code:
void *__fastcall func5(int a1)
{
int i; // ebx
char *v2; // rax
char *v3; // rsi
__int64 v4; // rbx

history_file = fopen(history_file_path, "r");
if ( !history_file )
{
puts("History Read Error:");
puts("Those who forget their history are doomed to repeat it...");
}
if ( a1 >= 0 )
{
for ( i = 0; i <= a1; ++i )
fgets(0LL, 80, history_file);
}
fclose(history_file);
strtok(0LL, " ");
v2 = strtok(0LL, " ");
strcpy(*(char **)cmd, v2);
v3 = strtok(0LL, " ");
if ( v3 )
{
v4 = 8LL;
do
{
strcpy(*(char **)((char *)cmd + v4), v3);
v3 = strtok(0LL, " ");
v4 += 8LL;
}
while ( v3 );
}
return cmd;
}

original_code:
char** get_from_hist(int num) {
    char* hist_cmd;
    char* temp;
    int num_args = 0;
    int i;
    history_file = fopen(history_file_path, "r");
    if(!history_file) {
        printf("History Read Error:\n");
        printf("Those who forget their history are doomed to repeat it...\n");
    }
    for (i = 0; i <= num; i++){
        fgets(hist_cmd, 80, history_file);
    }
    fclose(history_file);
    strtok(hist_cmd, " ");
    strcpy(cmd[0], strtok(((void *)0), " "));
    temp = strtok(((void *)0), " ");
    while (temp != ((void *)0)) {
        num_args++;
        strcpy(cmd[num_args], temp);
        temp = strtok(((void *)0), " ");
    }
    return cmd;
}

canonical_original_code:
char **get_from_hist(int num) {
    char *hist_cmd;
    char *temp;
    int num_args = 0;
    int i;
    history_file = fopen(history_file_path, "r");
    if(!history_file) {
        printf("History Read Error:\n");
        printf("Those who forget their history are doomed to repeat it...\n");
    }
    for (i = 0; i <= num; i++){
        fgets(hist_cmd, 80, history_file);
    }
    fclose(history_file);
    strtok(hist_cmd, " ");
    strcpy(cmd[0], strtok(((void *)0), " "));
    temp = strtok(((void *)0), " ");
    while (temp != ((void *)0)) {
        num_args++;
        strcpy(cmd[num_args], temp);
        temp = strtok(((void *)0), " ");
    }
    return cmd;
}

variable_types: num int


repo: oakey-dev/moonitor

decompiled_code:
int catch_sigpipe()
{
return puts("caught SIGPIPE");
}

canonical_decompiled_code:
int func1()
{
return puts("caught SIGPIPE");
}

original_code:
void catch_sigpipe(int i){
 printf("caught SIGPIPE\n");
}


decompiled_code:
__int64 listener_thread()
{
int v0; // eax
int v1; // ebp
pthread_t v2; // rax
int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-D8h] BYREF
struct sigaction v6; // [rsp+10h] [rbp-C8h] BYREF
socklen_t addr_len; // [rsp+ACh] [rbp-2Ch] BYREF
pthread_t newthread; // [rsp+B0h] [rbp-28h] BYREF
int arg[7]; // [rsp+BCh] [rbp-1Ch] BYREF

addr_len = 16;
memset(&v6.sa_mask, 0, 0x90uLL);
v6.sa_handler = (__sighandler_t)catch_sigpipe;
sigaction(13, &v6, 0LL);
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[6] = 0LL;
*(_DWORD *)&v5.sa_data[2] = inet_addr("127.0.0.1");
*(_WORD *)v5.sa_data = _bswap_16(2130);
v0 = socket(2, 1, 0);
if ( v0 < 0 )
{
perror("error creating socket. terminating sockshell thread.");
}
else
{
v1 = v0;
if ( bind(v0, &v5, 0x10u) )
{
perror("error binding to DEFAULT_HOST:DEFAULT_PORT. terminating sockshell thread.");
}
else
{
if ( !listen(v1, 1024) )
{
while ( 1 )
{
pthread_mutex_lock(&lock);
arg[0] = accept(v1, &v5, &addr_len);
if ( arg[0] < 0 )
perror("error accepting connections on socket");
v2 = pthread_self();
printf("sockshell main thread (thread id:%ld) accepted connection\n", v2);
v3 = pthread_create(&newthread, 0LL, session, arg);
if ( v3 )
{
*_errno_location() = v3;
perror("error creating thread for sockshell session");
pthread_mutex_unlock(&lock);
}
}
}
perror("error listening on socket. terminating sockshell thread.");
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
int v1; // ebp
pthread_t v2; // rax
int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-D8h] BYREF
struct sigaction v6; // [rsp+10h] [rbp-C8h] BYREF
socklen_t addr_len; // [rsp+ACh] [rbp-2Ch] BYREF
pthread_t newthread; // [rsp+B0h] [rbp-28h] BYREF
int arg[7]; // [rsp+BCh] [rbp-1Ch] BYREF

addr_len = 16;
memset(&v6.sa_mask, 0, 0x90uLL);
v6.sa_handler = (__sighandler_t)catch_sigpipe;
sigaction(13, &v6, 0LL);
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[6] = 0LL;
*(_DWORD *)&v5.sa_data[2] = inet_addr("127.0.0.1");
*(_WORD *)v5.sa_data = func0(2130);
v0 = socket(2, 1, 0);
if ( v0 < 0 )
{
perror("error creating socket. terminating sockshell thread.");
}
else
{
v1 = v0;
if ( bind(v0, &v5, 0x10u) )
{
perror("error binding to DEFAULT_HOST:DEFAULT_PORT. terminating sockshell thread.");
}
else
{
if ( !listen(v1, 1024) )
{
while ( 1 )
{
pthread_mutex_lock(&lock);
arg[0] = accept(v1, &v5, &addr_len);
if ( arg[0] < 0 )
perror("error accepting connections on socket");
v2 = pthread_self();
printf("sockshell main thread (thread id:%ld) accepted connection\n", v2);
v3 = pthread_create(&newthread, 0LL, session, arg);
if ( v3 )
{
*_errno_location() = v3;
perror("error creating thread for sockshell session");
pthread_mutex_unlock(&lock);
}
}
}
perror("error listening on socket. terminating sockshell thread.");
}
}
return 0LL;
}

original_code:
void* listener_thread(void* p_arg){
 ((void) sizeof ((sizeof(void*) >= sizeof(int)) ? 1 : 0), __extension__ ({ if (sizeof(void*) >= sizeof(int)) ; else __assert_fail ("sizeof(void*) >= sizeof(int)", "listener.c", 18, __extension__ __PRETTY_FUNCTION__); }));
 int consock;
 pthread_t last_session;
 socklen_t socksize = sizeof(struct sockaddr_in);
 struct sigaction sig_act;
 memset(&sig_act,0,sizeof(struct sigaction));
 sig_act.__sigaction_handler.sa_handler = catch_sigpipe;
 sigaction(13,&sig_act,((void *)0));
 struct sockaddr_in addr;
 memset(&addr,0,sizeof(addr));
 addr.sin_family = 2;
 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 addr.sin_port = htons(2130);
 int sock = socket(2,SOCK_STREAM,0);
 if(sock < 0){
  perror("error creating socket. terminating sockshell thread.");
  return ((void *)0);
 }
 if( 0 != bind(sock,(struct sockaddr*)&addr,sizeof(addr))){
  perror("error binding to DEFAULT_HOST:DEFAULT_PORT. terminating sockshell thread.");
  return ((void *)0);
 }
 if( 0 != listen(sock,1024)){
  perror("error listening on socket. terminating sockshell thread.");
  return ((void *)0);
 }
 int status;
 for(;;){
  pthread_mutex_lock(&lock);
  consock = accept(sock,(struct sockaddr*)&addr,&socksize);
  if(consock<0){
   perror("error accepting connections on socket");
  }
  printf("sockshell main thread (thread id:%ld) accepted connection\n",(long)pthread_self());
  status = pthread_create(&last_session,((void *)0),session,&consock);
  if(status != 0){
   do{ (*__errno_location ()) = status; perror("error creating thread for sockshell session"); } while(0);
   pthread_mutex_unlock(&lock);
  }
 }
 return 0;
}

canonical_original_code:
void *listener_thread(void *p_arg){
 ((void) sizeof ((sizeof(void *) >= sizeof(int)) ? 1 : 0), __extension__ ({ if (sizeof(void *) >= sizeof(int)) ; else __assert_fail ("sizeof(void*) >= sizeof(int)", "listener.c", 18, __extension__ __PRETTY_FUNCTION__); }));
 int consock;
 unsigned long last_session;
 unsigned int socksize = sizeof(struct sockaddr_in);
 struct sigaction sig_act;
 memset(&sig_act,0,sizeof(struct sigaction));
 sig_act.__sigaction_handler.sa_handler = catch_sigpipe;
 sigaction(13,&sig_act,((void *)0));
 struct sockaddr_in addr;
 memset(&addr,0,sizeof(addr));
 addr.sin_family = 2;
 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 addr.sin_port = htons(2130);
 int sock = socket(2,SOCK_STREAM,0);
 if(sock < 0){
  perror("error creating socket. terminating sockshell thread.");
  return ((void *)0);
 }
 if( 0 != bind(sock,(struct sockaddr *)&addr,sizeof(addr))){
  perror("error binding to DEFAULT_HOST:DEFAULT_PORT. terminating sockshell thread.");
  return ((void *)0);
 }
 if( 0 != listen(sock,1024)){
  perror("error listening on socket. terminating sockshell thread.");
  return ((void *)0);
 }
 int status;
 for(;;){
  pthread_mutex_lock(&lock);
  consock = accept(sock,(struct sockaddr *)&addr,&socksize);
  if(consock<0){
   perror("error accepting connections on socket");
  }
  printf("sockshell main thread (thread id:%ld) accepted connection\n",(long)pthread_self());
  status = pthread_create(&last_session,((void *)0),session,&consock);
  if(status != 0){
   do{ (*__errno_location ()) = status; perror("error creating thread for sockshell session"); } while(0);
   pthread_mutex_unlock(&lock);
  }
 }
 return 0;
}

variable_types: consock int

variable_types:
socksize
unsigned int

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)(); }

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]; }

variable_types: sock int

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 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]; }

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; }


nwrite

decompiled_code:
__int64 __fastcall nwrite(int fd, __int64 a2, size_t a3)
{
size_t v4; // rbx
int v5; // eax
unsigned int v6; // er14

if ( a3 )
{
v4 = 0LL;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), a3);
v6 = v5;
if ( v5 < 0 )
break;
v4 += v5;
if ( a3 <= v4 )
goto LABEL_5;
}
perror("error writing to file descriptor");
}
else
{
LODWORD(v4) = 0;
LABEL_5:
v6 = v4;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2, size_t a3)
{
size_t v4; // rbx
int v5; // eax
unsigned int v6; // er14

if ( a3 )
{
v4 = 0LL;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), a3);
v6 = v5;
if ( v5 < 0 )
break;
v4 += v5;
if ( a3 <= v4 )
goto LABEL_5;
}
perror("error writing to file descriptor");
}
else
{
LODWORD(v4) = 0;
LABEL_5:
v6 = v4;
}
return v6;
}

original_code:
int nwrite(int fd, const void* buf,size_t count){
 int n=0;
 size_t written=0;
 char* bytebuf=(char*)buf;
 while(written < count){
  n = write(fd,(void*)&(bytebuf[written]),count);
  if(n<0){
   perror("error writing to file descriptor");
   return n;
  } else {
   written += n;
  }
 }
 return written;
}

canonical_original_code:
int nwrite(int fd, void *buf, unsigned long count){
 int n = 0;
 unsigned long written = 0;
 char *bytebuf = (char*)buf;
 while(written < count){
  n = write(fd,(void *)&(bytebuf[written]),count);
  if(n<0){
   perror("error writing to file descriptor");
   return n;
  } else {
   written += n;
  }
 }
 return written;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: n int

variable_types:
written
unsigned long

variable_types:
bytebuf
char *


callback

decompiled_code:
__int64 __fastcall callback(int *a1, int a2, __int64 a3, __int64 a4)
{
int v4; // ebp
__int64 i; // rbx
const char *v8; // r12
size_t v9; // rax
const char *v10; // r12
size_t v11; // rax

v4 = *a1;
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; ++i )
{
v8 = *(const char **)(a4 + 8 * i);
v9 = strlen(v8);
nwrite(v4, (__int64)v8, v9);
nwrite(v4, (__int64)"=", 1uLL);
v10 = *(const char **)(a3 + 8 * i);
v11 = strlen(v10);
nwrite(v4, (__int64)v10, v11);
nwrite(v4, (__int64)"\n", 1uLL);
}
}
return 0LL;
}

original_code:
int callback(void* p_sock, int column_count, char** column_text, char** column_names){
 int sock = *(int*)p_sock;
 int i;
 for(i=0;i<column_count;i++){
  nwrite(sock,column_names[i],strlen(column_names[i]));
  nwrite(sock,"=",strlen("="));
  nwrite(sock,column_text[i],strlen(column_text[i]));
  nwrite(sock,"\n",strlen("\n"));
 }
 return 0;
}

canonical_original_code:
int callback(void *p_sock, int column_count, char **column_text, char **column_names){
 int sock = *(int*)p_sock;
 int i;
 for(i=0;i<column_count;i++){
  nwrite(sock,column_names[i],strlen(column_names[i]));
  nwrite(sock,"=",strlen("="));
  nwrite(sock,column_text[i],strlen(column_text[i]));
  nwrite(sock,"\n",strlen("\n"));
 }
 return 0;
}


decompiled_code:
__int64 __fastcall db_query(__int64 a1, __int64 a2)
{
__int64 result; // rax
const char *v3; // [rsp+8h] [rbp-10h] BYREF

v3 = 0LL;
result = sqlite3_exec(sqlite_db, a1, callback, a2, &v3);
if ( (_DWORD)result )
{
fprintf(stderr, "SQL error: %s\n", v3);
sqlite3_free(v3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 result; // rax
const char *v3; // [rsp+8h] [rbp-10h] BYREF

v3 = 0LL;
result = sqlite3_exec(sqlite_db, a1, callback, a2, &v3);
if ( (_DWORD)result )
{
fprintf(stderr, "SQL error: %s\n", v3);
sqlite3_free(v3);
result = 1LL;
}
return result;
}

original_code:
int db_query(char* query,int* p_sock){
 char* errMsg = ((void *)0);
 if ( sqlite3_exec(sqlite_db, query, callback, p_sock, &errMsg) != 0 ) {
  fprintf(stderr, "SQL error: %s\n", errMsg);
  sqlite3_free(errMsg);
  return 1;
 }
 return 0;
}

canonical_original_code:
int db_query(char *query, int *p_sock){
 char *errMsg = ((void *)0);
 if ( sqlite3_exec(sqlite_db, query, callback, p_sock, &errMsg) != 0 ) {
  fprintf(stderr, "SQL error: %s\n", errMsg);
  sqlite3_free(errMsg);
  return 1;
 }
 return 0;
}

variable_types:
query
char *

variable_types:
errMsg
char *


session

decompiled_code:
void *__fastcall session(void *a1)
{
pthread_t v1; // rbx
size_t v2; // rax
char *v3; // rbx
size_t v4; // rax
int v5; // ebx
int fd; // [rsp+Ch] [rbp-ACh] BYREF
char s[168]; // [rsp+10h] [rbp-A8h] BYREF

s[128] = 0;
fd = *(_DWORD *)a1;
pthread_mutex_unlock(&lock);
v1 = pthread_self();
pthread_detach(v1);
snprintf(s, 0x80uLL, "Welcome to moonitor shell. Session thread id: %ld\n", v1);
v2 = strlen(s);
nwrite(fd, (__int64)s, v2);
while ( 1 )
{
v3 = ps;
v4 = strlen(ps);
v5 = nwrite(fd, (__int64)v3, v4);
read(fd, s, 0x80uLL);
if ( memcmp(s, "plugins", 7uLL) )
break;
db_query((__int64)"SELECT id FROM plugin", (__int64)&fd);
LABEL_3:
if ( v5 < 0 )
goto LABEL_9;
}
if ( s[0] != 4 && !memcmp(s, "exit", 4uLL) && !memcmp(s, "quit", 4uLL) )
goto LABEL_3;
nwrite(fd, (__int64)"bye\n", 5uLL);
LABEL_9:
if ( close(fd) )
perror("error closing socket");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func6(void *a1)
{
pthread_t v1; // rbx
size_t v2; // rax
char *v3; // rbx
size_t v4; // rax
int v5; // ebx
int fd; // [rsp+Ch] [rbp-ACh] BYREF
char s[168]; // [rsp+10h] [rbp-A8h] BYREF

s[128] = 0;
fd = *(_DWORD *)a1;
pthread_mutex_unlock(&lock);
v1 = pthread_self();
pthread_detach(v1);
snprintf(s, 0x80uLL, "Welcome to moonitor shell. Session thread id: %ld\n", v1);
v2 = strlen(s);
func3(fd, (__int64)s, v2);
while ( 1 )
{
v3 = ps;
v4 = strlen(ps);
v5 = func3(fd, (__int64)v3, v4);
read(fd, s, 0x80uLL);
if ( memcmp(s, "plugins", 7uLL) )
break;
func5((__int64)"SELECT id FROM plugin", (__int64)&fd);
LABEL_3:
if ( v5 < 0 )
goto LABEL_9;
}
if ( s[0] != 4 && !memcmp(s, "exit", 4uLL) && !memcmp(s, "quit", 4uLL) )
goto LABEL_3;
func3(fd, (__int64)"bye\n", 5uLL);
LABEL_9:
if ( close(fd) )
perror("error closing socket");
return 0LL;
}

original_code:
void* session(void* p_sock){
 char buf[128 +1];
 buf[128] = '\0';
 int consock = *(int*)p_sock;
 pthread_mutex_unlock(&lock);
 pthread_detach(pthread_self());
 snprintf(buf,128,"Welcome to moonitor shell. Session thread id: %ld\n",(long)pthread_self());
 nwrite(consock,buf,strlen(buf));
 int status=0;
 while(status>=0){
  status = nwrite(consock,ps,strlen(ps));
  read(consock,buf,128);
  if(strncmp(buf,"plugins",strlen("plugins")) == 0){
   db_query("SELECT id FROM plugin",&consock);
  }
  else if(buf[0] == 0x04
    || strncmp(buf,"exit",strlen("exit"))
    || strncmp(buf,"quit",strlen("quit"))
         )
  {
   nwrite(consock,"bye\n",5);
   break;
  }
 }
 if(close(consock) != 0){
  perror("error closing socket");
 }
 return 0;
}

canonical_original_code:
void *session(void *p_sock){
 char buf[-1];
 buf[128] = '\0';
 int consock = *(int*)p_sock;
 pthread_mutex_unlock(&lock);
 pthread_detach(pthread_self());
 snprintf(buf,128,"Welcome to moonitor shell. Session thread id: %ld\n",(long)pthread_self());
 nwrite(consock,buf,strlen(buf));
 int status = 0;
 while(status>=0){
  status = nwrite(consock,ps,strlen(ps));
  read(consock,buf,128);
  if(strncmp(buf,"plugins",strlen("plugins")) == 0){
   db_query("SELECT id FROM plugin",&consock);
  }
  else if(buf[0] == 0x04
    || strncmp(buf,"exit",strlen("exit"))
    || strncmp(buf,"quit",strlen("quit"))
         )
  {
   nwrite(consock,"bye\n",5);
   break;
  }
 }
 if(close(consock) != 0){
  perror("error closing socket");
 }
 return 0;
}

variable_types:
buf
char [-1]

variable_types: status int


decompiled_code:
__int64 sql_open()
{
__int64 result; // rax
const char *v1; // rax

result = sqlite3_open("node.db", &sqlite_db);
if ( (_DWORD)result )
{
v1 = (const char *)sqlite3_errmsg(sqlite_db);
fprintf(stderr, "Critical: Can't open database: %s\n", v1);
sqlite3_close(sqlite_db);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
const char *v1; // rax

result = sqlite3_open("node.db", &sqlite_db);
if ( (_DWORD)result )
{
v1 = (const char *)sqlite3_errmsg(sqlite_db);
fprintf(stderr, "Critical: Can't open database: %s\n", v1);
sqlite3_close(sqlite_db);
result = 1LL;
}
return result;
}

original_code:
int sql_open() {
   if ( sqlite3_open("node.db", &sqlite_db) ) {
      fprintf(stderr, "Critical: Can't open database: %s\n", sqlite3_errmsg(sqlite_db));
      sqlite3_close(sqlite_db);
      return 1;
   }
   return 0;
}


decompiled_code:
__int64 sql_close()
{
return sqlite3_close(sqlite_db);
}

canonical_decompiled_code:
__int64 func1()
{
return sqlite3_close(sqlite_db);
}

original_code:
void sql_close() {
   sqlite3_close(sqlite_db);
}


decompiled_code:
__int64 __fastcall sql_exec(__int64 a1)
{
__int64 result; // rax
const char *v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
result = sqlite3_exec(sqlite_db, a1, 0LL, 0LL, &v2);
if ( (_DWORD)result )
{
fprintf(stderr, "SQL error: %s\n", v2);
sqlite3_free(v2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
const char *v2; // [rsp+8h] [rbp-10h] BYREF

v2 = 0LL;
result = sqlite3_exec(sqlite_db, a1, 0LL, 0LL, &v2);
if ( (_DWORD)result )
{
fprintf(stderr, "SQL error: %s\n", v2);
sqlite3_free(v2);
result = 1LL;
}
return result;
}

original_code:
int sql_exec(char* sql) {
   char* errMsg = ((void *)0);
   if ( sqlite3_exec(sqlite_db, sql, ((void *)0), 0, &errMsg) != 0 ) {
      fprintf(stderr, "SQL error: %s\n", errMsg);
      sqlite3_free(errMsg);
      return 1;
   }
   return 0;
}

canonical_original_code:
int sql_exec(char *sql) {
   char *errMsg = ((void *)0);
   if ( sqlite3_exec(sqlite_db, sql, ((void *)0), 0, &errMsg) != 0 ) {
      fprintf(stderr, "SQL error: %s\n", errMsg);
      sqlite3_free(errMsg);
      return 1;
   }
   return 0;
}

variable_types:
sql
char *


decompiled_code:
__int64 sql_init()
{
int v0; // edx
__int64 result; // rax
int v2; // edx

v0 = sql_open();
result = 1LL;
if ( !v0 )
{
v2 = sql_exec((__int64)"CREATE TABLE IF NOT EXISTS plugin ( id VARCHAR(16), c"
"ategory VARCHAR(32), title VARCHAR(64), description V"
"ARCHAR(256), desc_x VARCHAR(64), unit_x VARCHAR(8), "
" desc_y VARCHAR(64), unit_y VARCHAR(8), "
" base_y INT DEFAULT 1024, min_y INT DEFAULT 0, ma"
"x_y INT DEFAULT 0, PRIMARY KEY (id) )");
result = 2LL;
if ( !v2 )
{
result = sql_exec((__int64)"CREATE TABLE IF NOT EXISTS option ( id VARCHAR(16), "
" field VARCHAR(16), draw TINYINT DEFAULT 1, "
" type TINYINT DEFAULT 1, warn INT DEFAULT 0, cr"
"it INT DEFAULT 0, PRIMARY KEY (id, field) )");
if ( (_DWORD)result )
result = 2LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // edx
__int64 result; // rax
int v2; // edx

v0 = func0();
result = 1LL;
if ( !v0 )
{
v2 = func2((__int64)"CREATE TABLE IF NOT EXISTS plugin ( id VARCHAR(16), c"
"ategory VARCHAR(32), title VARCHAR(64), description V"
"ARCHAR(256), desc_x VARCHAR(64), unit_x VARCHAR(8), "
" desc_y VARCHAR(64), unit_y VARCHAR(8), "
" base_y INT DEFAULT 1024, min_y INT DEFAULT 0, ma"
"x_y INT DEFAULT 0, PRIMARY KEY (id) )");
result = 2LL;
if ( !v2 )
{
result = func2((__int64)"CREATE TABLE IF NOT EXISTS option ( id VARCHAR(16), "
" field VARCHAR(16), draw TINYINT DEFAULT 1, "
" type TINYINT DEFAULT 1, warn INT DEFAULT 0, cr"
"it INT DEFAULT 0, PRIMARY KEY (id, field) )");
if ( (_DWORD)result )
result = 2LL;
}
}
return result;
}

original_code:
int sql_init() {
   if ( sql_open() != 0 ) {
      return 1;
   }
   if ( sql_exec("CREATE TABLE IF NOT EXISTS plugin (                     id VARCHAR(16),                     category VARCHAR(32),                     title VARCHAR(64),                     description VARCHAR(256),                     desc_x VARCHAR(64),                     unit_x VARCHAR(8),                     desc_y VARCHAR(64),                     unit_y VARCHAR(8),                     base_y INT DEFAULT 1024,                     min_y INT DEFAULT 0,                     max_y INT DEFAULT 0,                     PRIMARY KEY (id)                 )") != 0 ) {
      return 2;
   }
   if ( sql_exec("CREATE TABLE IF NOT EXISTS option (                     id VARCHAR(16),                     field VARCHAR(16),                     draw TINYINT DEFAULT 1,                     type TINYINT DEFAULT 1,                     warn INT DEFAULT 0,                     crit INT DEFAULT 0,                     PRIMARY KEY (id, field)                 )") != 0 ) {
      return 2;
   }
   return 0;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // cf
bool v4; // zf
const char *v5; // rsi
const char *v6; // rdi
__int64 v7; // rcx
int v8; // ebx
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

v3 = 0;
v4 = 0;
v5 = *argv;
v6 = "./daemon";
v7 = 9LL;
do
{
if ( !v7 )
break;
v3 = *v5 < (unsigned int)*v6;
v4 = *v5++ == *v6++;
--v7;
}
while ( v4 );
if ( (!v3 && !v4) == v3 )
{
v8 = sql_init(v6, v5, envp);
if ( v8 )
{
v8 = 1;
}
else
{
pthread_create(newthread, 0LL, (void *(*)(void *))&listener_thread, 0LL);
collector_thread(0LL);
sql_close();
}
}
else
{
fwrite("Error: while development, daemon musst be called with \"./daemon\"\n", 1uLL, 0x41uLL, stderr);
v8 = 2;
}
return v8;
}

original_code:
int main(int argc, char* argv[]) {
   if ( strcmp(*argv, "./daemon") ) {
      fprintf(stderr, "Error: while development, daemon musst be called with \"./daemon\"\n");
      return 2;
   }
   if ( sql_init() != 0 ) {
      return 1;
   }
   pthread_t last_thread;
   pthread_create(&last_thread,((void *)0),listener_thread,((void *)0));
   collector_thread(((void *)0));
   sql_close();
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
   if ( strcmp(*argv, "./daemon") ) {
      fprintf(stderr, "Error: while development, daemon musst be called with \"./daemon\"\n");
      return 2;
   }
   if ( sql_init() != 0 ) {
      return 1;
   }
   unsigned long last_thread;
   pthread_create(&last_thread,((void *)0),listener_thread,((void *)0));
   collector_thread(((void *)0));
   sql_close();
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


init

decompiled_code:
const char *init()
{
return "CREATE TABLE IF NOT EXISTS cpu_avg ( time TIMESTAMP PRIMARY KEY DEFAULT CURRENT_TIMESTAMP, "
" avg1 INTEGER, avg5 INTEGER, avg15 INTEGER );";
}

canonical_decompiled_code:
const char *func6()
{
return "CREATE TABLE IF NOT EXISTS cpu_avg ( time TIMESTAMP PRIMARY KEY DEFAULT CURRENT_TIMESTAMP, "
" avg1 INTEGER, avg5 INTEGER, avg15 INTEGER );";
}

original_code:
char* init() {
   return "CREATE TABLE IF NOT EXISTS cpu_avg (             time  TIMESTAMP PRIMARY KEY DEFAULT CURRENT_TIMESTAMP,             avg1  INTEGER,             avg5  INTEGER,             avg15 INTEGER          );";
}

canonical_original_code:
char *init() {
   return "CREATE TABLE IF NOT EXISTS cpu_avg (             time  TIMESTAMP PRIMARY KEY DEFAULT CURRENT_TIMESTAMP,             avg1  INTEGER,             avg5  INTEGER,             avg15 INTEGER          );";
}


decompiled_code:
__int64 default_interval()
{
return 60LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 60LL;
}

original_code:
int default_interval() {
   return 60;
}

canonical_original_code:
int default_interval() {
   return 60;
}


decompiled_code:
void unload_plugins(void)
{
__int64 v0; // rbx
__int64 v1; // rbp
char v2[56]; // [rsp+0h] [rbp-38h] BYREF

v0 = *(_QWORD *)plugins;
if ( *(_QWORD *)plugins )
{
v1 = 8LL;
do
{
dlclose(*(void **)(v0 + 16));
snprintf(v2, 0x1AuLL, "/moonit%p", *(const void **)(v0 + 24));
v2[7] = 111;
v2[8] = 114;
sem_unlink(v2);
v0 = *(_QWORD *)(plugins + v1);
v1 += 8LL;
}
while ( v0 );
}
}

canonical_decompiled_code:
void func0(void)
{
__int64 v0; // rbx
__int64 v1; // rbp
char v2[56]; // [rsp+0h] [rbp-38h] BYREF

v0 = *(_QWORD *)plugins;
if ( *(_QWORD *)plugins )
{
v1 = 8LL;
do
{
dlclose(*(void **)(v0 + 16));
snprintf(v2, 0x1AuLL, "/moonit%p", *(const void **)(v0 + 24));
v2[7] = 111;
v2[8] = 114;
sem_unlink(v2);
v0 = *(_QWORD *)(plugins + v1);
v1 += 8LL;
}
while ( v0 );
}
}

original_code:
void unload_plugins(void) {
   int i;
   struct plugin* p;
   for ( i = 0; (p = plugins[i]) != ((void *)0); i++ ) {
      dlclose(p->handle);
      char str[sizeof(void*)*2+1];
      snprintf(str, (sizeof(void*)*2+10), "/moonit%p", p->cmd);
      *(str+7) = 'o';
      *(str+8) = 'r';
      sem_unlink(str);
   }
}

canonical_original_code:
void unload_plugins() {
   int i;
   struct plugin *p;
   for ( i = 0; (p = plugins[i]) != ((void *)0); i++ ) {
      dlclose(p->handle);
      char str[-1];
      snprintf(str, (sizeof(void *)*2+10), "/moonit%p", p->cmd);
      *(str+7) = 'o';
      *(str+8) = 'r';
      sem_unlink(str);
   }
}

variable_types: i int

variable_types: struct plugin { int interval; union sem_t { char __size[32]; long __align; } *lock; void *handle; char *(*cmd)(); } *

variable_types:
str
char [-1]

user_defined_types: struct plugin { int interval; union sem_t *lock; void *handle; char *(*cmd)(); }


decompiled_code:
void __noreturn collector_thread()
{
time_t *v0; // rax
time_t *v1; // r12
void *v2; // rbp
time_t v3; // rax
unsigned int **v4; // rdi
unsigned int *v5; // rbx
__int64 v6; // rbp
int v7; // eax
time_t v8; // rax
time_t *v9; // rbx
time_t v10; // rbx

v0 = (time_t *)malloc(0x18uLL);
if ( v0 )
{
v1 = v0;
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
load_plugins((__int64)v0);
while ( 1 )
{
v10 = *v1;
if ( v10 <= time(0LL) )
{
v4 = (unsigned int **)v1[1];
v5 = *v4;
if ( *v4 )
{
v6 = 1LL;
do
{
printf("running cmd %p, with interval %d\n", *((const void **)v5 + 3), *v5);
if ( sem_trywait(*((sem_t **)v5 + 1)) )
{
fprintf(stderr, "Error: runtime for %p is to short (%d)\n", *((const void **)v5 + 3), *v5);
v8 = time(0LL);
queue_add(v1, (__int64)v5, v8 + 1);
}
else
{
v7 = fork();
if ( !v7 )
{
if ( !fork() )
{
v2 = (void *)(*((__int64 (**)(void))v5 + 3))();
sem_post(*((sem_t **)v5 + 1));
if ( v2 )
sql_exec(v2);
else
fprintf(stderr, "Error: Command %p failed\n", *((const void **)v5 + 3));
free(v2);
}
exit(0);
}
if ( v7 < 0 )
{
fprintf(stderr, "Error: could not fork child %p", *((const void **)v5 + 3));
sem_post(*((sem_t **)v5 + 1));
}
else
{
waitpid(v7, 0LL, 8);
}
v3 = time(0LL);
queue_add(v1, (__int64)v5, v3 + (int)*v5);
}
v4 = (unsigned int **)v1[1];
v5 = v4[v6++];
}
while ( v5 );
}
v9 = (time_t *)v1[2];
free(v4);
free(v1);
v1 = v9;
}
sleep(1u);
}
}
fwrite("Critical: not enough memory!", 1uLL, 0x1CuLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func2()
{
time_t *v0; // rax
time_t *v1; // r12
void *v2; // rbp
time_t v3; // rax
unsigned int **v4; // rdi
unsigned int *v5; // rbx
__int64 v6; // rbp
int v7; // eax
time_t v8; // rax
time_t *v9; // rbx
time_t v10; // rbx

v0 = (time_t *)malloc(0x18uLL);
if ( v0 )
{
v1 = v0;
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
func1((__int64)v0);
while ( 1 )
{
v10 = *v1;
if ( v10 <= time(0LL) )
{
v4 = (unsigned int **)v1[1];
v5 = *v4;
if ( *v4 )
{
v6 = 1LL;
do
{
printf("running cmd %p, with interval %d\n", *((const void **)v5 + 3), *v5);
if ( sem_trywait(*((sem_t **)v5 + 1)) )
{
fprintf(stderr, "Error: runtime for %p is to short (%d)\n", *((const void **)v5 + 3), *v5);
v8 = time(0LL);
queue_add(v1, (__int64)v5, v8 + 1);
}
else
{
v7 = fork();
if ( !v7 )
{
if ( !fork() )
{
v2 = (void *)(*((__int64 (**)(void))v5 + 3))();
sem_post(*((sem_t **)v5 + 1));
if ( v2 )
sql_exec(v2);
else
fprintf(stderr, "Error: Command %p failed\n", *((const void **)v5 + 3));
free(v2);
}
exit(0);
}
if ( v7 < 0 )
{
fprintf(stderr, "Error: could not fork child %p", *((const void **)v5 + 3));
sem_post(*((sem_t **)v5 + 1));
}
else
{
waitpid(v7, 0LL, 8);
}
v3 = time(0LL);
queue_add(v1, (__int64)v5, v3 + (int)*v5);
}
v4 = (unsigned int **)v1[1];
v5 = v4[v6++];
}
while ( v5 );
}
v9 = (time_t *)v1[2];
free(v4);
free(v1);
v1 = v9;
}
sleep(1u);
}
}
fwrite("Critical: not enough memory!", 1uLL, 0x1CuLL, stderr);
exit(1);
}

original_code:
void* collector_thread(void* arg) {
   struct queue *que, *tmp;
   struct plugin* p;
   pid_t pid, cpid;
   int i;
   que = malloc(sizeof(struct queue));
   if ( que == ((void *)0) ) {
      fprintf(stderr, "Critical: not enough memory!");
      exit(1);
   }
   que->timestamp = 0;
   que->plugins = ((void *)0);
   que->next = ((void *)0);
   load_plugins(que);
   while ( 1 ) {
      if ( que->timestamp <= time(((void *)0)) ) {
         for ( i=0; (p = que->plugins[i]) != ((void *)0); i++ ) {
            printf("running cmd %p, with interval %d\n", p->cmd, p->interval);
            if ( sem_trywait(p->lock) == 0 ) {
               if ( (pid = fork()) == 0 ) {
                  if ( (cpid = fork()) == 0 ) {
                     char* ret = (p->cmd)();
                     sem_post(p->lock);
                     if ( ret == ((void *)0) ) {
                        fprintf(stderr, "Error: Command %p failed\n", p->cmd);
                     }
                     else {
                        sql_exec(ret);
                     }
                     free(ret);
                  }
                  else if ( pid < 0 ) {
                     fprintf(stderr, "Error: could not fork daemon %p", p->cmd);
                     sem_post(p->lock);
                     exit(1);
                  }
                  exit(0);
               }
               else if ( pid < 0 ) {
                  fprintf(stderr, "Error: could not fork child %p", p->cmd);
                  sem_post(p->lock);
               }
               else {
                  waitpid(pid, ((void *)0), 8);
               }
               queue_add(que, p, (time_t)(time(((void *)0))+p->interval));
            }
            else {
               fprintf(stderr, "Error: runtime for %p is to short (%d)\n", p->cmd, p->interval);
               queue_add(que, p, (time_t)(time(((void *)0))+1));
            }
         }
         tmp = que->next;
         free(que->plugins);
         free(que);
         que = tmp;
      }
      sleep(1);
   }
}

canonical_original_code:
void *collector_thread(void *arg) {
   struct queue *que; struct queue *tmp;
   struct plugin *p;
   int pid; int cpid;
   int i;
   que = malloc(sizeof(struct queue));
   if ( que == ((void *)0) ) {
      fprintf(stderr, "Critical: not enough memory!");
      exit(1);
   }
   que->timestamp = 0;
   que->plugins = ((void *)0);
   que->next = ((void *)0);
   load_plugins(que);
   while ( 1 ) {
      if ( que->timestamp <= time(((void *)0)) ) {
         for ( i=0; (p = que->plugins[i]) != ((void *)0); i++ ) {
            printf("running cmd %p, with interval %d\n", p->cmd, p->interval);
            if ( sem_trywait(p->lock) == 0 ) {
               if ( (pid = fork()) == 0 ) {
                  if ( (cpid = fork()) == 0 ) {
                     char *ret = (p->cmd)();
                     sem_post(p->lock);
                     if ( ret == ((void *)0) ) {
                        fprintf(stderr, "Error: Command %p failed\n", p->cmd);
                     }
                     else {
                        sql_exec(ret);
                     }
                     free(ret);
                  }
                  else if ( pid < 0 ) {
                     fprintf(stderr, "Error: could not fork daemon %p", p->cmd);
                     sem_post(p->lock);
                     exit(1);
                  }
                  exit(0);
               }
               else if ( pid < 0 ) {
                  fprintf(stderr, "Error: could not fork child %p", p->cmd);
                  sem_post(p->lock);
               }
               else {
                  waitpid(pid, ((void *)0), 8);
               }
               queue_add(que, p, (time_t)(time(((void *)0))+p->interval));
            }
            else {
               fprintf(stderr, "Error: runtime for %p is to short (%d)\n", p->cmd, p->interval);
               queue_add(que, p, (time_t)(time(((void *)0))+1));
            }
         }
         tmp = que->next;
         free(que->plugins);
         free(que);
         que = tmp;
      }
      sleep(1);
   }
}

variable_types:
arg
void *

variable_types: struct queue { long timestamp; struct plugin { int interval; union sem_t { char __size[32]; long __align; } *lock; void *handle; char *(*cmd)(); } **plugins; struct queue *next; } *

variable_types: pid int

variable_types: cpid int

variable_types:
ret
char *

user_defined_types: struct queue { long timestamp; struct plugin **plugins; struct queue *next; }


repo: oasynnoum/c-linked-list

decompiled_code:
_QWORD *init_state_storage()
{
_QWORD *result; // rax

result = malloc(8uLL);
*result = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func7()
{
_QWORD *result; // rax

result = malloc(8uLL);
*result = 0LL;
return result;
}

original_code:
state_storage* init_state_storage() {
    state_storage* storage = (state_storage*)malloc(sizeof (state_storage));
    storage->first = ((void *)0);
    return storage;
}

canonical_original_code:
struct _state_storage *init_state_storage() {
    struct _state_storage *storage = (state_storage*)malloc(sizeof (state_storage));
    storage->first = ((void *)0);
    return storage;
}

variable_types: struct _state_storage { struct _state_element { char *state; struct _state_element *next; } *first; } *

user_defined_types: struct _state_storage { struct _state_element *first; }


decompiled_code:
void __fastcall destroy_state_storage(void *ptr)
{
const char **v2; // rbx
const char **v3; // rdi

v2 = *(const char ***)ptr;
if ( *(_QWORD *)ptr )
{
do
{
printf("current:%s\n", *v2);
v3 = v2;
v2 = (const char **)v2[1];
free(v3);
}
while ( v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
const char **v2; // rbx
const char **v3; // rdi

v2 = *(const char ***)ptr;
if ( *(_QWORD *)ptr )
{
do
{
printf("current:%s\n", *v2);
v3 = v2;
v2 = (const char **)v2[1];
free(v3);
}
while ( v2 );
}
free(ptr);
}

original_code:
void destroy_state_storage(state_storage* storage) {
    state_element* current_element = storage->first;
    while (current_element) {
        printf("current:%s\n", current_element->state);
        state_element* next = current_element->next;
        free(current_element);
        current_element = next;
    }
    free(storage);
}

canonical_original_code:
void destroy_state_storage(struct _state_storage *storage) {
    struct _state_element *current_element = storage->first;
    while (current_element) {
        printf("current:%s\n", current_element->state);
        struct _state_element *next = current_element->next;
        free(current_element);
        current_element = next;
    }
    free(storage);
}

variable_types: struct _state_element { char *state; struct _state_element *next; } *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rbx

v3 = init_state_storage();
store_state_to_first(v3, (__int64)L"321");
store_state_to_first(v3, 4202514LL);
store_state_to_first(v3, 4202516LL);
destroy_state_storage(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rbx

v3 = func7();
store_state_to_first(v3, (__int64)L"321");
store_state_to_first(v3, 4202514LL);
store_state_to_first(v3, 4202516LL);
func8(v3);
return 0;
}

original_code:
int main (int argc, char* argv[]) {
    state_storage* storage = init_state_storage();
    store_state_to_first(storage, "3");
    store_state_to_first(storage, "2");
    store_state_to_first(storage, "1");
    destroy_state_storage(storage);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    struct _state_storage *storage = init_state_storage();
    store_state_to_first(storage, "3");
    store_state_to_first(storage, "2");
    store_state_to_first(storage, "1");
    destroy_state_storage(storage);
    return 0;
}

user_defined_types: struct _state_element { char *state; struct _state_element *next; }