writeCheck

decompiled_code:
__int16 writeCheck()
{
int v0; // ecx
int v1; // eax
int v2; // er12
signed __int64 v3; // r14
void *v4; // rsp
_WORD *v5; // rbx
_WORD *v6; // r12
_WORD v8[16]; // [rsp+0h] [rbp-20h] BYREF

v0 = fseek((FILE *)drives[2 * drive], 2LL * position, 0);
LOWORD(v1) = 2048 - position;
v2 = -(unsigned __int16)rkwc;
if ( position + (__int16)-rkwc >= 1247232 )
LOWORD(v2) = 2048 - position;
if ( (_WORD)v2 )
{
if ( !v0 )
{
v3 = 2LL * (__int16)v2;
v4 = alloca(v3);
v5 = v8;
v1 = fread(v8, 2uLL, (__int16)v2, (FILE *)drives[2 * drive]);
if ( v1 == v3 && (__int16)v2 > 0 )
{
v6 = &v8[(__int16)v2];
do
{
if ( BigEndian )
*v5 = __ROL2__(*v5, 8);
LOWORD(v1) = getWord((unsigned int)ba);
if ( (_WORD)v1 == *v5 )
{
if ( (rkcs & 0x400) == 0 )
ba += 2;
}
else
{
LOBYTE(rker) = rker | 1;
if ( (rkcs & 0x100) != 0 )
return v1;
}
++v5;
}
while ( v5 != v6 );
}
}
}
return v1;
}

canonical_decompiled_code:
__int16 func4()
{
int v0; // ecx
int v1; // eax
int v2; // er12
signed __int64 v3; // r14
void *v4; // rsp
_WORD *v5; // rbx
_WORD *v6; // r12
_WORD v8[16]; // [rsp+0h] [rbp-20h] BYREF

v0 = fseek((FILE *)drives[2 * drive], 2LL * position, 0);
LOWORD(v1) = 2048 - position;
v2 = -(unsigned __int16)rkwc;
if ( position + (__int16)-rkwc >= 1247232 )
LOWORD(v2) = 2048 - position;
if ( (_WORD)v2 )
{
if ( !v0 )
{
v3 = 2LL * (__int16)v2;
v4 = alloca(v3);
v5 = v8;
v1 = fread(v8, 2uLL, (__int16)v2, (FILE *)drives[2 * drive]);
if ( v1 == v3 && (__int16)v2 > 0 )
{
v6 = &v8[(__int16)v2];
do
{
if ( BigEndian )
*v5 = __ROL2__(*v5, 8);
LOWORD(v1) = getWord((unsigned int)ba);
if ( (_WORD)v1 == *v5 )
{
if ( (rkcs & 0x400) == 0 )
ba += 2;
}
else
{
LOBYTE(rker) = rker | 1;
if ( (rkcs & 0x100) != 0 )
return v1;
}
++v5;
}
while ( v5 != v6 );
}
}
}
return v1;
}

original_code:
void writeCheck() {
 int i;
 int seek = fseek (drives[drive].file, position * sizeof(unsigned short), 0);
 short wc = rkwc * -1;
 if ((position + wc) >= SIZE) {
  wc = SIZE - position;
 }
 if (wc && !seek) {
  unsigned short buffer[wc];
  int re = fread (buffer, sizeof(unsigned short), wc, drives[drive].file);
  if (re != (wc * sizeof(unsigned short))) {
   return;
  }
  for (i = 0; i < wc; i++) {
   if (BigEndian) {
    buffer[i] = ((buffer[i] & 255) << 8) | ((buffer[i]>>8) & 255);
   }
   if (getWord(ba) == buffer[i]) {
    if (!rkcs.INH_BA)
     ba += 2;
   }
   else {
    rker.WCE = 1;
    if (rkcs.SSE)
     return;
   }
  }
 }
}

canonical_original_code:
void writeCheck() {
 int i;
 int seek = fseek (drives[drive].file, position * sizeof(unsigned short), 0);
 short wc = rkwc * -1;
 if ((position + wc) >= SIZE) {
  wc = SIZE - position;
 }
 if (wc && !seek) {
  unsigned short buffer[-1];
  int re = fread (buffer, sizeof(unsigned short), wc, drives[drive].file);
  if (re != (wc * sizeof(unsigned short))) {
   return;
  }
  for (i = 0; i < wc; i++) {
   if (BigEndian) {
    buffer[i] = ((buffer[i] & 255) << 8) | ((buffer[i]>>8) & 255);
   }
   if (getWord(ba) == buffer[i]) {
    if (!rkcs.INH_BA)
     ba += 2;
   }
   else {
    rker.WCE = 1;
    if (rkcs.SSE)
     return;
   }
  }
 }
}

variable_types: re int


readFMT

decompiled_code:
void readFMT()
{
int v0; // ebp
int i; // ebx

v0 = (__int16)-rkwc;
if ( v0 > 0 )
{
for ( i = 0; i != v0; ++i )
{
while ( 1 )
{
setWord((unsigned int)ba, (unsigned __int16)track);
if ( (rkcs & 0x400) == 0 )
ba += 2;
if ( sector == 11 )
break;
++sector;
LABEL_4:
if ( ++i == v0 )
return;
}
sector = 0;
if ( track != 202 )
goto LABEL_9;
if ( !surface )
{
surface = 1;
track = 0;
goto LABEL_4;
}
if ( surface == 1 )
{
HIBYTE(rker) |= 8u;
return;
}
LABEL_9:
++track;
}
}
}

canonical_decompiled_code:
void func5()
{
int v0; // ebp
int i; // ebx

v0 = (__int16)-rkwc;
if ( v0 > 0 )
{
for ( i = 0; i != v0; ++i )
{
while ( 1 )
{
setWord((unsigned int)ba, (unsigned __int16)track);
if ( (rkcs & 0x400) == 0 )
ba += 2;
if ( sector == 11 )
break;
++sector;
LABEL_4:
if ( ++i == v0 )
return;
}
sector = 0;
if ( track != 202 )
goto LABEL_9;
if ( !surface )
{
surface = 1;
track = 0;
goto LABEL_4;
}
if ( surface == 1 )
{
HIBYTE(rker) |= 8u;
return;
}
LABEL_9:
++track;
}
}
}

original_code:
void readFMT() {
 short wc = rkwc * -1;
 int i;
 for (i = 0; i < wc; i++) {
  setWord(ba, track);
  if (!rkcs.INH_BA)
   ba+=2;
  if (sector == (SECTORS-1)) {
   sector = 0;
   if (track == (TRACKSS-1) && surface == 0) {
    surface = 1;
    track = 0;
   }
   else if (track == (TRACKSS-1) && surface == 1) {
    rker.OVR = 1;
    return;
   }
   else {
    track++;
   }
   continue;
  }
  sector++;
 }
}

variable_types: wc short

variable_types: i int

return_type: void


clearErrors

decompiled_code:
void clearErrors()
{
rker &= 0xE000u;
}

canonical_decompiled_code:
void func6()
{
rker &= 0xE000u;
}

original_code:
void clearErrors() {
 rker.WCE = 0;
 rker.CSE = 0;
 rker.NXS = 0;
 rker.NXC = 0;
 rker.NXD = 0;
 rker.TE = 0;
 rker.DLT = 0;
 rker.NXM = 0;
 rker.PGE = 0;
 rker.SKE = 0;
 rker.WLO = 0;
 rker.OVR = 0;
 rker.DRE = 0;
}


clearCont

decompiled_code:
void clearCont()
{
rkcs &= 0xC000u;
}

canonical_decompiled_code:
void func7()
{
rkcs &= 0xC000u;
}

original_code:
void clearCont() {
 rkcs.GO = 0;
 rkcs.FUN = 0;
 rkcs.MEX = 0;
 rkcs.IDE = 0;
 rkcs.RDY = 0;
 rkcs.SSE = 0;
 rkcs.FMT = 0;
 rkcs.INH_BA = 0;
 rkcs.SCP = 0;
 rkcs.HE = 0;
 rkcs.ERROR = 0;
}


rkdsReset

decompiled_code:
void rkdsReset()
{
rkds &= 0xF000u;
}

canonical_decompiled_code:
void func8()
{
rkds &= 0xF000u;
}

original_code:
void rkdsReset() {
 rkds.SC = 0;
 rkds.SCSA = 0;
 rkds.WPS = 0;
 rkds.RWSR = 0;
 rkds.DRY = 0;
 rkds.SOK = 0;
 rkds.SIN = 0;
 rkds.DRU = 0;
 rkds.RK05 = 0;
}


controlReset

decompiled_code:
void controlReset()
{
rkda = 0;
rkba = 0;
clearErrors();
clearCont();
rkdsReset();
LOBYTE(rkcs) = rkcs | 0x80;
}

canonical_decompiled_code:
void func9()
{
rkda = 0;
rkba = 0;
func6();
func7();
func8();
LOBYTE(rkcs) = rkcs | 0x80;
}

original_code:
void controlReset() {
 rkda = 0;
 rkba = 0;
 clearErrors();
 clearCont();
 rkdsReset();
 rkcs.RDY = 1;
}


readRKDS

original_code:
unsigned short readRKDS() {
 unsigned short tmp = 0;
 tmp |= rkds.SC;
 tmp |= (rkds.SCSA << 4);
 tmp |= (rkds.WPS << 5);
 tmp |= (rkds.RWSR << 6);
 tmp |= (rkds.DRY << 7);
 tmp |= (rkds.SOK << 8);
 tmp |= (rkds.SIN << 9);
 tmp |= (rkds.DRU << 10);
 tmp |= (rkds.RK05 << 11);
 tmp |= (rkds.DPL << 12);
 tmp |= (rkds.ID << 13);
 return tmp;
}

variable_types:
tmp
unsigned short

return_type: unsigned short


readRKER

original_code:
unsigned short readRKER() {
 unsigned short tmp = 0;
 tmp |= rker.WCE;
 tmp |= (rker.CSE << 1);
 tmp |= (rker.NXS << 5);
 tmp |= (rker.NXC << 6);
 tmp |= (rker.NXD << 7);
 tmp |= (rker.TE << 8);
 tmp |= (rker.DLT << 9);
 tmp |= (rker.NXM << 10);
 tmp |= (rker.PGE << 11);
 tmp |= (rker.SKE << 12);
 tmp |= (rker.WLO << 13);
 tmp |= (rker.OVR << 14);
 tmp |= (rker.DRE << 15);
 return tmp;
}


done

decompiled_code:
__int64 done()
{
int v0; // edx
__int64 result; // rax
char v2; // si

v0 = HIBYTE(rkda);
rkcs |= 0x880u;
rkds = rkds & 0xFE30 | ((_BYTE)v0 - -53 * (unsigned __int8)((unsigned int)(10331 * v0) >> 21)) & 0xF | 0x1C0;
result = readRKER();
if ( (_WORD)result )
{
v2 = HIBYTE(rkcs);
HIBYTE(rkcs) = v2 | 0x20;
result = readRKER();
if ( (result & 0xFFE0) != 0 )
HIBYTE(rkcs) = v2 | 0x30;
}
if ( (rkcs & 0x40) != 0 )
{
if ( (func & 0xFFFFFFFB) == 0 )
HIBYTE(rkds) = (32 * drive) | HIBYTE(rkds) & 0x1F;
result = busRequest(&BR5, 220LL);
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // edx
__int64 result; // rax
char v2; // si

v0 = HIBYTE(rkda);
rkcs |= 0x880u;
rkds = rkds & 0xFE30 | ((_BYTE)v0 - -53 * (unsigned __int8)((unsigned int)(10331 * v0) >> 21)) & 0xF | 0x1C0;
result = func11();
if ( (_WORD)result )
{
v2 = HIBYTE(rkcs);
HIBYTE(rkcs) = v2 | 0x20;
result = func11();
if ( (result & 0xFFE0) != 0 )
HIBYTE(rkcs) = v2 | 0x30;
}
if ( (rkcs & 0x40) != 0 )
{
if ( (func & 0xFFFFFFFB) == 0 )
HIBYTE(rkds) = (32 * drive) | HIBYTE(rkds) & 0x1F;
result = busRequest(&BR5, 220LL);
}
return result;
}

original_code:
void done() {
 rkcs.RDY = 1;
 rkcs.SCP = 1;
 rkds.SC = ((rkda/WORDS) % TRACKSS);
 rkds.RWSR = 1;
 rkds.DRY = 1;
 rkds.SOK = 1;
 if (readRKER()) {
  rkcs.ERROR = 1;
 }
 if (readRKER() & 0177740) {
  rkcs.HE = 1;
 }
 if (rkcs.IDE) {
 if (func == SEEK || func == CONTROL_RESET) {
  rkcs.SCP = 1;
  rkds.ID = drive;
 }
   busRequest(&BR5, IntVector);
 }
}


go

decompiled_code:
void go()
{
unsigned __int8 v0; // si
__int64 v1; // rdi
__int64 v2; // rax
unsigned __int64 v3; // rsi
int v4; // ecx
__int16 v5; // dx
int v6; // ecx
__int64 v7; // rsi

v0 = ((unsigned __int8)rkcs >> 1) & 7;
v1 = v0;
func = v0;
if ( !v0 )
{
controlReset();
return;
}
LOBYTE(rker) = rker & 0xFC;
if ( !(unsigned __int16)readRKER() )
HIBYTE(rkcs) &= 0xDFu;
rkcs &= 0xF77Fu;
if ( (rkcs & 0x200) != 0 && ((v0 + 7) & 7u) > 1 )
{
HIBYTE(rker) |= 1u;
((void (*)(void))done)();
return;
}
v2 = (unsigned __int16)rkda >> 13;
drive = (unsigned __int16)rkda >> 13;
v3 = (unsigned __int64)&drives[2 * v2];
if ( !*(_QWORD *)v3 )
{
LOBYTE(rker) = rker | 0x10;
done(v1, v3);
return;
}
if ( *(_WORD *)(v3 + 8) )
{
HIBYTE(rker) |= 0x10u;
done(v1, v3);
return;
}
if ( (_DWORD)v1 == 1 )
{
if ( *(_WORD *)(v3 + 10) )
{
HIBYTE(rker) |= 4u;
done(v1, v3);
return;
}
goto LABEL_20;
}
if ( (_DWORD)v1 != 7 )
{
if ( (_DWORD)v1 == 6 )
{
*(_WORD *)(v3 + 10) = 0;
surface = 0;
sector = 0;
track = 0;
func = 4;
LABEL_12:
LOBYTE(rkds) = rkds | 0x10;
done(v1, v3);
v4 = (unsigned __int16)track;
v3 = (unsigned int)func;
v2 = drive;
LABEL_13:
v5 = rkba;
ba = (unsigned __int16)rkba + ((((unsigned __int8)rkcs >> 4) & 3) << 16);
v6 = (unsigned __int16)sector + 12 * v4;
if ( surface )
v6 += 2436;
position = v6 << 8;
LOWORD(drives[2 * v2 + 1]) = 1;
if ( (_DWORD)v3 == 2 )
{
if ( (rkcs & 0x200) != 0 )
readFMT();
else
rk_read();
v5 = ba;
v2 = drive;
}
else if ( (_DWORD)v3 == 3 )
{
writeCheck();
v5 = ba;
v2 = drive;
}
else if ( (_DWORD)v3 == 1 && (rkcs & 0x200) == 0 )
{
rk_write();
v5 = ba;
v2 = drive;
}
rkba = v5;
LOWORD(drives[2 * v2 + 1]) = 0;
rkwc = 0;
done(1LL, v3);
return;
}
LABEL_20:
v7 = rkda & 0xF;
v4 = (unsigned __int8)((unsigned __int16)rkda >> 5);
sector = v7;
track = (unsigned __int8)((unsigned __int16)rkda >> 5);
surface = ((unsigned __int16)rkda >> 4) & 1;
if ( (unsigned __int16)v7 > 0xBu )
{
LOBYTE(rker) = rker | 4;
done(v1, v7);
return;
}
if ( (unsigned __int16)v4 > 0xCAu )
{
LOBYTE(rker) = rker | 8;
done(v1, v7);
return;
}
v3 = (unsigned int)func;
if ( func != 4 )
goto LABEL_13;
goto LABEL_12;
}
*(_WORD *)(v3 + 10) = 1;
done(v1, v3);
if ( (rkcs & 0x40) != 0 )
busRequest(&BR5, 220LL);
}

canonical_decompiled_code:
void func13()
{
unsigned __int8 v0; // si
__int64 v1; // rdi
__int64 v2; // rax
unsigned __int64 v3; // rsi
int v4; // ecx
__int16 v5; // dx
int v6; // ecx
__int64 v7; // rsi

v0 = ((unsigned __int8)rkcs >> 1) & 7;
v1 = v0;
func = v0;
if ( !v0 )
{
func9();
return;
}
LOBYTE(rker) = rker & 0xFC;
if ( !(unsigned __int16)func11() )
HIBYTE(rkcs) &= 0xDFu;
rkcs &= 0xF77Fu;
if ( (rkcs & 0x200) != 0 && ((v0 + 7) & 7u) > 1 )
{
HIBYTE(rker) |= 1u;
((void (*)(void))done)();
return;
}
v2 = (unsigned __int16)rkda >> 13;
drive = (unsigned __int16)rkda >> 13;
v3 = (unsigned __int64)&drives[2 * v2];
if ( !*(_QWORD *)v3 )
{
LOBYTE(rker) = rker | 0x10;
func12(v1, v3);
return;
}
if ( *(_WORD *)(v3 + 8) )
{
HIBYTE(rker) |= 0x10u;
func12(v1, v3);
return;
}
if ( (_DWORD)v1 == 1 )
{
if ( *(_WORD *)(v3 + 10) )
{
HIBYTE(rker) |= 4u;
func12(v1, v3);
return;
}
goto LABEL_20;
}
if ( (_DWORD)v1 != 7 )
{
if ( (_DWORD)v1 == 6 )
{
*(_WORD *)(v3 + 10) = 0;
surface = 0;
sector = 0;
track = 0;
func = 4;
LABEL_12:
LOBYTE(rkds) = rkds | 0x10;
func12(v1, v3);
v4 = (unsigned __int16)track;
v3 = (unsigned int)func;
v2 = drive;
LABEL_13:
v5 = rkba;
ba = (unsigned __int16)rkba + ((((unsigned __int8)rkcs >> 4) & 3) << 16);
v6 = (unsigned __int16)sector + 12 * v4;
if ( surface )
v6 += 2436;
position = v6 << 8;
LOWORD(drives[2 * v2 + 1]) = 1;
if ( (_DWORD)v3 == 2 )
{
if ( (rkcs & 0x200) != 0 )
func5();
else
func2();
v5 = ba;
v2 = drive;
}
else if ( (_DWORD)v3 == 3 )
{
func4();
v5 = ba;
v2 = drive;
}
else if ( (_DWORD)v3 == 1 && (rkcs & 0x200) == 0 )
{
func3();
v5 = ba;
v2 = drive;
}
rkba = v5;
LOWORD(drives[2 * v2 + 1]) = 0;
rkwc = 0;
func12(1LL, v3);
return;
}
LABEL_20:
v7 = rkda & 0xF;
v4 = (unsigned __int8)((unsigned __int16)rkda >> 5);
sector = v7;
track = (unsigned __int8)((unsigned __int16)rkda >> 5);
surface = ((unsigned __int16)rkda >> 4) & 1;
if ( (unsigned __int16)v7 > 0xBu )
{
LOBYTE(rker) = rker | 4;
func12(v1, v7);
return;
}
if ( (unsigned __int16)v4 > 0xCAu )
{
LOBYTE(rker) = rker | 8;
func12(v1, v7);
return;
}
v3 = (unsigned int)func;
if ( func != 4 )
goto LABEL_13;
goto LABEL_12;
}
*(_WORD *)(v3 + 10) = 1;
func12(v1, v3);
if ( (rkcs & 0x40) != 0 )
busRequest(&BR5, 220LL);
}

original_code:
void go() {
 func = rkcs.FUN;
 if (func == CONTROL_RESET) {
  controlReset();
  return;
 }
 rker.WCE = rker.CSE = 0;
 if (readRKER() == 0)
  rkcs.ERROR = 0;
 rkcs.SCP = 0;
 rkcs.RDY = 0;
 if (rkcs.FMT && func != WRITE && func != READ) {
  rker.PGE = 1;
  done();
  return;
 }
 drive = (((rkda) >> RKDA_SDRV) & RKDA_ADRV);
 if (drives[drive].file == ((void *)0)) {
  rker.NXD = 1;
  done();
  return;
 }
 if (drives[drive].busy) {
  rker.DRE = 1;
  done();
  return;
 }
 if (func == WRITE && drives[drive].WPS) {
  rker.WLO = 1;
  done();
  return;
 }
 if (func == WRITE_LOCK) {
  drives[drive].WPS = 1;
  done();
  if (rkcs.IDE) {
   busRequest(&BR5, IntVector);
  }
  return;
 }
 if (func == DRIVE_RESET) {
  drives[drive].WPS = 0;
  track = sector = surface = 0;
  func = SEEK;
 }
 else {
  track = (((rkda) >> RKDA_STRAC) & RKDA_ATRAC);
  sector = (((rkda) >> RDKA_SSECT) & RKDA_ASECT);
  surface = (((rkda) >> RKDA_SSUR) & RKDA_ASUR);
 }
 if (sector >= SECTORS) {
  rker.NXS = 1;
  done();
  return;
 }
 if (track >= TRACKSS) {
  rker.NXC = 1;
  done();
  return;
 }
 if (func == SEEK) {
  rkds.SCSA = 1;
  done();
 }
 ba = (rkcs.MEX << sizeof(unsigned short)*8) + rkba;
 position = ((surface) == 0 ? (((track * SECTORS) + sector) * WORDS) : ((((track * SECTORS) + sector) * WORDS) + (TRACKSS * SECTORS * WORDS)));
 drives[drive].busy = 1;
 switch (func) {
  case WRITE:
   if (rkcs.FMT) {
   }
   else {
    rk_write();
   }
   break;
  case READ:
   if (rkcs.FMT) {
    readFMT();
   }
   else {
    rk_read();
   }
   break;
  case WRITE_CHECK:
   writeCheck();
   break;
  case READ_CHECK:
   break;
 }
 rkba = ba & 0177777;
 rkwc = 0;
 drives[drive].busy = 0;
 done();
}


readRKCS

original_code:
unsigned short readRKCS() {
 unsigned short tmp = 0;
 tmp |= rkcs.FUN << 1;
 tmp |= rkcs.MEX << 4;
 tmp |= rkcs.IDE << 6;
 tmp |= rkcs.RDY << 7;
 tmp |= rkcs.SSE << 8;
 tmp |= rkcs.FMT << 10;
 tmp |= rkcs.INH_BA << 11;
 tmp |= rkcs.SCP << 13;
 tmp |= rkcs.HE << 14;
 tmp |= rkcs.ERROR << 15;
 return tmp;
}


writeRKCS

decompiled_code:
__int64 __fastcall writeRKCS(unsigned __int16 a1)
{
__int64 result; // rax

result = (unsigned __int16)rkcs & 0xF880;
rkcs = result | (a1 >> 1) & 0x200 | a1 & 0x17F | (a1 >> 1) & 0x400;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned __int16 a1)
{
__int64 result; // rax

result = (unsigned __int16)rkcs & 0xF880;
rkcs = result | (a1 >> 1) & 0x200 | a1 & 0x17F | (a1 >> 1) & 0x400;
return result;
}

original_code:
void writeRKCS(unsigned short value) {
 rkcs.GO = value & 1;
 rkcs.FUN = (value >> 1) & 7;
 rkcs.MEX = (value >> 4) & 3;
 rkcs.IDE = (value >> 6) & 1;
 rkcs.SSE = (value >> 8) & 1;
 rkcs.FMT = (value >> 10) & 1;
 rkcs.INH_BA = (value >> 11) & 1;
}

variable_types:
value
unsigned short


rkIO

decompiled_code:
__int64 __fastcall rkIO(const char *a1, __int64 a2, __int64 a3, __int64 a4)
{
bool v4; // cf
bool v5; // zf
const char *v7; // rdi
__int64 v8; // rcx
const char *v9; // rsi
__int64 result; // rax

switch ( (int)a2 )
{
case 261888:
result = readRKDS(a1, a2, a3, a4, a1);
break;
case 261890:
result = readRKER(a1, a2, a3, a4, a1);
break;
case 261892:
v7 = "read";
v8 = 5LL;
v9 = a1;
do
{
if ( !v8 )
break;
v4 = *v9 < (unsigned int)*v7;
v5 = *v9++ == *v7++;
--v8;
}
while ( v5 );
if ( (!v4 && !v5) == v4 )
{
result = readRKCS(v7, v9);
}
else
{
if ( strcmp(a1, "write") )
goto LABEL_7;
writeRKCS(a3 & 0x1FFF);
if ( (rkcs & 1) != 0 )
go();
result = 0LL;
}
break;
case 261894:
result = (unsigned __int16)rkwc;
if ( !strcmp(a1, "write") )
{
rkwc = a3;
result = 0LL;
}
break;
case 261896:
result = (unsigned __int16)rkba;
if ( !strcmp(a1, "write") )
{
rkba = a3;
result = 0LL;
}
break;
case 261898:
rkda = a3;
result = 0LL;
break;
default:
LABEL_7:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(const char *a1, __int64 a2, __int64 a3, __int64 a4)
{
bool v4; // cf
bool v5; // zf
const char *v7; // rdi
__int64 v8; // rcx
const char *v9; // rsi
__int64 result; // rax

switch ( (int)a2 )
{
case 261888:
result = func10(a1, a2, a3, a4, a1);
break;
case 261890:
result = func11(a1, a2, a3, a4, a1);
break;
case 261892:
v7 = "read";
v8 = 5LL;
v9 = a1;
do
{
if ( !v8 )
break;
v4 = *v9 < (unsigned int)*v7;
v5 = *v9++ == *v7++;
--v8;
}
while ( v5 );
if ( (!v4 && !v5) == v4 )
{
result = func14(v7, v9);
}
else
{
if ( strcmp(a1, "write") )
goto LABEL_7;
func15(a3 & 0x1FFF);
if ( (rkcs & 1) != 0 )
func13();
result = 0LL;
}
break;
case 261894:
result = (unsigned __int16)rkwc;
if ( !strcmp(a1, "write") )
{
rkwc = a3;
result = 0LL;
}
break;
case 261896:
result = (unsigned __int16)rkba;
if ( !strcmp(a1, "write") )
{
rkba = a3;
result = 0LL;
}
break;
case 261898:
rkda = a3;
result = 0LL;
break;
default:
LABEL_7:
result = 0LL;
break;
}
return result;
}

original_code:
unsigned short rkIO(const char * command, unsigned int addr, unsigned short value) {
 switch (addr) {
  case RKDSA:
   return readRKDS();
  case RKERA:
   return readRKER();
  case RKCSA:
   if (strcmp(command, "read") == 0) {
    return readRKCS();
   }
   else if (strcmp(command, "write") == 0) {
    writeRKCS(value&017777);
     if (rkcs.GO) {
      go();
     }
   }
   break;
  case RKWCA:
   if (strcmp(command, "write") == 0) {
    rkwc = value;
   }
   else {
    return rkwc;
   }
   break;
  case RKBAA:
   if (strcmp(command, "write") == 0) {
    rkba = value;
   }
   else {
    return rkba;
   }
   break;
  case RKDAA:
   rkda = value;
   break;
 }
 return 0;
}

canonical_original_code:
unsigned short rkIO(char *command, unsigned int addr, unsigned short value) {
 switch (addr) {
  case RKDSA:
   return readRKDS();
  case RKERA:
   return readRKER();
  case RKCSA:
   if (strcmp(command, "read") == 0) {
    return readRKCS();
   }
   else if (strcmp(command, "write") == 0) {
    writeRKCS(value&017777);
     if (rkcs.GO) {
      go();
     }
   }
   break;
  case RKWCA:
   if (strcmp(command, "write") == 0) {
    rkwc = value;
   }
   else {
    return rkwc;
   }
   break;
  case RKBAA:
   if (strcmp(command, "write") == 0) {
    rkba = value;
   }
   else {
    return rkba;
   }
   break;
  case RKDAA:
   rkda = value;
   break;
 }
 return 0;
}

variable_types:
command
char *

variable_types:
addr
unsigned int


original_code:
void rk_boot_hello() {
 reg[7] = 01020;
 setWord (RKWCA, -25);
 setWord (RKBAA, 512);
 setWord (RKDAA, 0);
 setWord (RKCSA, 05);
}


bootStrap

decompiled_code:
__int64 bootStrap()
{
reg[7] = 0;
setWord(261894LL, 65280LL);
return setWord(261892LL, 5LL);
}

original_code:
void bootStrap() {
 reg[7] = 000000;
 setWord (RKWCA, -256);
 setWord (RKCSA, 05);
}


isBigEndian

decompiled_code:
__int64 isBigEndian()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func19()
{
return 0LL;
}

original_code:
int isBigEndian() {
 int no = 1;
 char *chk = (char *)&no;
 if (chk[0] == 1) {
  return 0;
 }
 else {
  return 1;
 }
}

variable_types: no int

variable_types:
chk
char *

return_type: int


decompiled_code:
__int64 rk_init()
{
__int64 result; // rax

configureDevice(rkIO, 261888LL, 261902LL);
result = isBigEndian();
LOBYTE(rkcs) = rkcs | 0x80;
BigEndian = result;
return result;
}

canonical_decompiled_code:
__int64 func20()
{
__int64 result; // rax

configureDevice(rkIO, 261888LL, 261902LL);
result = func19();
LOBYTE(rkcs) = rkcs | 0x80;
BigEndian = result;
return result;
}

original_code:
void rk_init() {
 configureDevice( rkIO, RKDSA, RKDBA);
 BigEndian = isBigEndian();
 rkcs.RDY = 1;
}


bin

decompiled_code:
int __fastcall bin(unsigned __int16 a1)
{
int v1; // ebp
unsigned int v2; // ebx
int result; // eax

v1 = 16;
v2 = 0x8000;
do
{
while ( ((unsigned __int16)v2 & a1) != 0 )
{
v2 >>= 1;
result = waddnstr(iface2, "1", -1);
if ( !--v1 )
return result;
}
v2 >>= 1;
result = waddnstr(iface2, "0", -1);
--v1;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func0(unsigned __int16 a1)
{
int v1; // ebp
unsigned int v2; // ebx
int result; // eax

v1 = 16;
v2 = 0x8000;
do
{
while ( ((unsigned __int16)v2 & a1) != 0 )
{
v2 >>= 1;
result = waddnstr(iface2, "1", -1);
if ( !--v1 )
return result;
}
v2 >>= 1;
result = waddnstr(iface2, "0", -1);
--v1;
}
while ( v1 );
return result;
}

original_code:
void bin(unsigned short n) {
 unsigned int i;
 i = 1<<(sizeof(n) * 8 - 1);
 while (i > 0) {
  if (n & i)
   waddnstr(iface2,"1",-1);
  else
   waddnstr(iface2,"0",-1);
  i >>= 1;
 }
}

variable_types:
n
unsigned short

variable_types:
i
unsigned int


updateregsdisplay

decompiled_code:
int updateregsdisplay()
{
unsigned __int16 v0; // ax
unsigned __int16 v1; // ax
unsigned __int16 v2; // ax
int result; // eax

werase(iface2);
waddnstr(iface2, "Addr reg:", -1);
v0 = getaddrreg();
bin(v0);
waddnstr(iface2, " Data reg:", -1);
v1 = getdatareg();
bin(v1);
waddnstr(iface2, " Switch reg:", -1);
v2 = getswitchreg();
result = bin(v2);
if ( currentwindow == 1 )
result = wrefresh(iface2);
return result;
}

canonical_decompiled_code:
int func1()
{
unsigned __int16 v0; // ax
unsigned __int16 v1; // ax
unsigned __int16 v2; // ax
int result; // eax

werase(iface2);
waddnstr(iface2, "Addr reg:", -1);
v0 = getaddrreg();
func0(v0);
waddnstr(iface2, " Data reg:", -1);
v1 = getdatareg();
func0(v1);
waddnstr(iface2, " Switch reg:", -1);
v2 = getswitchreg();
result = func0(v2);
if ( currentwindow == 1 )
result = wrefresh(iface2);
return result;
}

original_code:
void updateregsdisplay() {
 werase(iface2);
 waddnstr(iface2,"Addr reg:",-1);
 unsigned long addrreg = getaddrreg();
 bin(addrreg);
 waddnstr(iface2," Data reg:",-1);
 unsigned short datareg = getdatareg();
 bin(datareg);
 waddnstr(iface2," Switch reg:",-1);
 signed short switchreg = getswitchreg();
 bin(switchreg);
 if(currentwindow == 1)
  wrefresh(iface2);
}

canonical_original_code:
void updateregsdisplay() {
 werase(iface2);
 waddnstr(iface2,"Addr reg:",-1);
 unsigned long addrreg = getaddrreg();
 bin(addrreg);
 waddnstr(iface2," Data reg:",-1);
 unsigned short datareg = getdatareg();
 bin(datareg);
 waddnstr(iface2," Switch reg:",-1);
 short switchreg = getswitchreg();
 bin(switchreg);
 if(currentwindow == 1)
  wrefresh(iface2);
}

variable_types:
addrreg
unsigned long

variable_types:
datareg
unsigned short

variable_types: switchreg short


decompiled_code:
int iface_printmenu()
{
waddnstr(
iface,
"\n"
"Menu: \n"
" r: update address and data registers \n"
" s: modify switch register \n"
" d: press dep switch \n"
" e: press exam switch \n"
" l: press load switch \n"
" a: press enable switch \n"
" c: press cont switch",
-1);
return wrefresh(iface);
}

canonical_decompiled_code:
int func2()
{
waddnstr(
iface,
"\n"
"Menu: \n"
" r: update address and data registers \n"
" s: modify switch register \n"
" d: press dep switch \n"
" e: press exam switch \n"
" l: press load switch \n"
" a: press enable switch \n"
" c: press cont switch",
-1);
return wrefresh(iface);
}

original_code:
void iface_printmenu() {
 waddnstr(iface,"\nMenu: \n r: update address and data registers \n s: modify switch register" " \n d: press dep switch \n e: press exam switch \n l: press load switch" " \n a: press enable switch \n c: press cont switch",-1);
 wrefresh(iface);
}


decompiled_code:
int __fastcall iface_keypress(char a1)
{
char v2; // al

if ( ifstate )
{
if ( ifstate == 1 )
{
if ( (unsigned __int8)(a1 - 48) <= 1u )
{
if ( (unsigned __int8)switch_bitcount <= 0xFu )
{
waddch(iface, (unsigned int)a1);
v2 = ++switch_bitcount;
if ( a1 == 49 )
current_switch |= 1 << (16 - v2);
}
}
else if ( a1 == 10 )
{
setWord(262008LL, (unsigned __int16)current_switch);
ifstate = 0;
iface_printmenu();
}
}
}
else
{
switch ( a1 )
{
case 'a':
enable_switch();
waddnstr(iface, "\n enable switch pressed \n", -1);
break;
case 'c':
cont_switch();
waddnstr(iface, "\n cont switch pressed \n", -1);
break;
case 'd':
dep_switch();
waddnstr(iface, "\n dep switch pressed \n", -1);
break;
case 'e':
exam_switch();
waddnstr(iface, "\n exam switch pressed \n", -1);
break;
case 'l':
load_adrs_switch();
waddnstr(iface, "\n load switch pressed \n", -1);
break;
case 'r':
updateregsdisplay();
break;
case 's':
ifstate = 1;
waddnstr(iface, "\n Enter switch register in binary and press enter \n", -1);
switch_bitcount = 0;
current_switch = 0;
break;
default:
return wrefresh(iface);
}
}
return wrefresh(iface);
}

canonical_decompiled_code:
int __fastcall func3(char a1)
{
char v2; // al

if ( ifstate )
{
if ( ifstate == 1 )
{
if ( (unsigned __int8)(a1 - 48) <= 1u )
{
if ( (unsigned __int8)switch_bitcount <= 0xFu )
{
waddch(iface, (unsigned int)a1);
v2 = ++switch_bitcount;
if ( a1 == 49 )
current_switch |= 1 << (16 - v2);
}
}
else if ( a1 == 10 )
{
setWord(262008LL, (unsigned __int16)current_switch);
ifstate = 0;
func2();
}
}
}
else
{
switch ( a1 )
{
case 'a':
enable_switch();
waddnstr(iface, "\n enable switch pressed \n", -1);
break;
case 'c':
cont_switch();
waddnstr(iface, "\n cont switch pressed \n", -1);
break;
case 'd':
dep_switch();
waddnstr(iface, "\n dep switch pressed \n", -1);
break;
case 'e':
exam_switch();
waddnstr(iface, "\n exam switch pressed \n", -1);
break;
case 'l':
load_adrs_switch();
waddnstr(iface, "\n load switch pressed \n", -1);
break;
case 'r':
func1();
break;
case 's':
ifstate = 1;
waddnstr(iface, "\n Enter switch register in binary and press enter \n", -1);
switch_bitcount = 0;
current_switch = 0;
break;
default:
return wrefresh(iface);
}
}
return wrefresh(iface);
}

original_code:
void iface_keypress(char ch) {
 if(ifstate == 0) {
  switch(ch) {
   case 'r':
    updateregsdisplay();
   break;
   case 's':
    ifstate = 1;
    waddnstr(iface,"\n Enter switch register in binary and press enter \n",-1);
    switch_bitcount = 0;
    current_switch = 0;
   break;
   case 'd':
    dep_switch();
    waddnstr(iface,"\n dep switch pressed \n",-1);
   break;
   case 'e':
    exam_switch();
    waddnstr(iface,"\n exam switch pressed \n",-1);
   break;
   case 'l':
    load_adrs_switch();
    waddnstr(iface,"\n load switch pressed \n",-1);
   break;
   case 'a':
    enable_switch();
    waddnstr(iface,"\n enable switch pressed \n",-1);
   break;
   case 'c':
    cont_switch();
    waddnstr(iface,"\n cont switch pressed \n",-1);
   break;
  }
 }
 else if(ifstate == 1) {
  if((ch == '0' || ch == '1') && switch_bitcount < 16) {
   waddch(iface, ch);
   switch_bitcount++;
   if(ch == '1')
    current_switch |= 1 << (16 - switch_bitcount);
  }
  else if(ch == 10) {
   setWord(0777570, current_switch);
   ifstate = 0;
   iface_printmenu();
  }
 }
 wrefresh(iface);
}

variable_types: ch char


setupwindow

decompiled_code:
int setupwindow()
{
int v0; // edx

initscr();
noecho();
iface = newwin(0, 0, 1, 0);
tt = newwin(0, 0, 0, 0);
iface2 = newwin(1, 0, 0, 0);
scrollok(iface, 1);
scrollok(tt, 1);
waddnstr(stdscr, "PDP11 Emulator Started", -1);
waddnstr(tt, "tt window\n", -1);
waddnstr(iface, "iface window\n", -1);
waddnstr(iface2, "iface2 here\n", -1);
wrefresh(stdscr);
v0 = -1;
if ( tt )
v0 = tt->_maxy + 1;
wtouchln(tt, 0, v0, 1);
return wrefresh(tt);
}

canonical_decompiled_code:
int func4()
{
int v0; // edx

initscr();
noecho();
iface = newwin(0, 0, 1, 0);
tt = newwin(0, 0, 0, 0);
iface2 = newwin(1, 0, 0, 0);
scrollok(iface, 1);
scrollok(tt, 1);
waddnstr(stdscr, "PDP11 Emulator Started", -1);
waddnstr(tt, "tt window\n", -1);
waddnstr(iface, "iface window\n", -1);
waddnstr(iface2, "iface2 here\n", -1);
wrefresh(stdscr);
v0 = -1;
if ( tt )
v0 = tt->_maxy + 1;
wtouchln(tt, 0, v0, 1);
return wrefresh(tt);
}

original_code:
void setupwindow() {
        initscr();
        noecho();
        iface = newwin(0, 0, 1, 0);
        tt = newwin(0, 0, 0, 0);
 iface2 = newwin(1, 0, 0, 0);
        scrollok(iface, 1);
        scrollok(tt, 1);
        waddnstr(stdscr,("PDP11 Emulator Started"),-1);
        waddnstr(tt,"tt window\n",-1);
        waddnstr(iface,"iface window\n",-1);
 waddnstr(iface2,"iface2 here\n",-1);
        wrefresh(stdscr);
        wtouchln((tt), 0, ((0 != (const void *)((tt))) ? ((tt)->_maxy + 1) : (-1)), 1);
        wrefresh(tt);
}

canonical_original_code:
void setupwindow() {
        initscr();
        noecho();
        iface = newwin(0, 0, 1, 0);
        tt = newwin(0, 0, 0, 0);
 iface2 = newwin(1, 0, 0, 0);
        scrollok(iface, 1);
        scrollok(tt, 1);
        waddnstr(stdscr,("PDP11 Emulator Started"),-1);
        waddnstr(tt,"tt window\n",-1);
        waddnstr(iface,"iface window\n",-1);
 waddnstr(iface2,"iface2 here\n",-1);
        wrefresh(stdscr);
        wtouchln((tt), 0, ((0 != (void *)((tt))) ? ((tt)->_maxy + 1) : (-1)), 1);
        wrefresh(tt);
}


terminal

decompiled_code:
__int64 terminal()
{
unsigned __int8 v0; // al
unsigned __int8 v1; // bl
int v3; // edx
int v4; // edx
int v5; // edx

currentwindow = 0;
do
{
while ( 1 )
{
do
{
v0 = wgetch(stdscr);
v1 = v0;
}
while ( v0 == 0xFF );
if ( v0 != 14 )
break;
v3 = -1;
currentwindow = currentwindow == 0;
if ( iface )
v3 = iface->_maxy + 1;
wtouchln(iface, 0, v3, 1);
v4 = -1;
if ( iface2 )
v4 = iface2->_maxy + 1;
wtouchln(iface2, 0, v4, 1);
v5 = -1;
if ( tt )
v5 = tt->_maxy + 1;
wtouchln(tt, 0, v5, 1);
if ( currentwindow == 1 )
{
ifstate = 0;
iface_printmenu();
wrefresh(iface);
wrefresh(iface2);
}
else
{
wrefresh(tt);
}
}
if ( currentwindow )
{
if ( currentwindow == 1 )
iface_keypress(v0);
}
else
{
on_keypress(v0);
}
}
while ( v1 != 27 );
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned __int8 v0; // al
unsigned __int8 v1; // bl
int v3; // edx
int v4; // edx
int v5; // edx

currentwindow = 0;
do
{
while ( 1 )
{
do
{
v0 = wgetch(stdscr);
v1 = v0;
}
while ( v0 == 0xFF );
if ( v0 != 14 )
break;
v3 = -1;
currentwindow = currentwindow == 0;
if ( iface )
v3 = iface->_maxy + 1;
wtouchln(iface, 0, v3, 1);
v4 = -1;
if ( iface2 )
v4 = iface2->_maxy + 1;
wtouchln(iface2, 0, v4, 1);
v5 = -1;
if ( tt )
v5 = tt->_maxy + 1;
wtouchln(tt, 0, v5, 1);
if ( currentwindow == 1 )
{
ifstate = 0;
func2();
wrefresh(iface);
wrefresh(iface2);
}
else
{
wrefresh(tt);
}
}
if ( currentwindow )
{
if ( currentwindow == 1 )
func3(v0);
}
else
{
on_keypress(v0);
}
}
while ( v1 != 27 );
return 0LL;
}

original_code:
void *terminal() {
        char ch;
 currentwindow = 0;
        while(ch != 27) {
                ch = wgetch(stdscr);
                if(ch == (-1))
   continue;
  if(ch == 14) {
   currentwindow = !currentwindow;
   wtouchln((iface), 0, ((0 != (const void *)((iface))) ? ((iface)->_maxy + 1) : (-1)), 1);
   wtouchln((iface2), 0, ((0 != (const void *)((iface2))) ? ((iface2)->_maxy + 1) : (-1)), 1);
   wtouchln((tt), 0, ((0 != (const void *)((tt))) ? ((tt)->_maxy + 1) : (-1)), 1);
   if(currentwindow == 1) {
    ifstate = 0;
    iface_printmenu();
    wrefresh(iface);
    wrefresh(iface2);
   }
   else {
    wrefresh(tt);
   }
  }
  else if(currentwindow == 0) {
                 on_keypress(ch);
  }
  else if(currentwindow == 1) {
   iface_keypress(ch);
  }
        }
 return 0;
}

canonical_original_code:
void *terminal() {
        char ch;
 currentwindow = 0;
        while(ch != 27) {
                ch = wgetch(stdscr);
                if(ch == (-1))
   continue;
  if(ch == 14) {
   currentwindow = !currentwindow;
   wtouchln((iface), 0, ((0 != (void *)((iface))) ? ((iface)->_maxy + 1) : (-1)), 1);
   wtouchln((iface2), 0, ((0 != (void *)((iface2))) ? ((iface2)->_maxy + 1) : (-1)), 1);
   wtouchln((tt), 0, ((0 != (void *)((tt))) ? ((tt)->_maxy + 1) : (-1)), 1);
   if(currentwindow == 1) {
    ifstate = 0;
    iface_printmenu();
    wrefresh(iface);
    wrefresh(iface2);
   }
   else {
    wrefresh(tt);
   }
  }
  else if(currentwindow == 0) {
                 on_keypress(ch);
  }
  else if(currentwindow == 1) {
   iface_keypress(ch);
  }
        }
 return 0;
}

return_type: void *


consoleio

decompiled_code:
__int16 __fastcall consoleio(const char *a1, int a2, __int16 a3)
{
__int16 result; // ax

updateregsdisplay();
if ( !strcmp(a1, "read") )
{
result = 0;
if ( a2 == 262008 )
result = switchreg;
}
else if ( !strcmp(a1, "write") && a2 == 262008 )
{
result = 0;
switchreg = a3;
}
else
{
result = 0;
}
return result;
}

canonical_decompiled_code:
__int16 __fastcall func0(const char *a1, int a2, __int16 a3)
{
__int16 result; // ax

updateregsdisplay();
if ( !strcmp(a1, "read") )
{
result = 0;
if ( a2 == 262008 )
result = switchreg;
}
else if ( !strcmp(a1, "write") && a2 == 262008 )
{
result = 0;
switchreg = a3;
}
else
{
result = 0;
}
return result;
}

original_code:
unsigned short consoleio(const char * command, unsigned int addr, unsigned short value) {
 updateregsdisplay();
 if(!strcmp(command, "read") && addr == 0777570) {
  return switchreg;
 }
 if(!strcmp(command, "write") && addr == 0777570) {
  switchreg = value;
 }
 return 0;
}

canonical_original_code:
unsigned short consoleio(char *command, unsigned int addr, unsigned short value) {
 updateregsdisplay();
 if(!strcmp(command, "read") && addr == 0777570) {
  return switchreg;
 }
 if(!strcmp(command, "write") && addr == 0777570) {
  switchreg = value;
 }
 return 0;
}


decompiled_code:
__int64 load_adrs_switch()
{
addrreg = (unsigned __int16)switchreg;
return updateregsdisplay();
}

canonical_decompiled_code:
__int64 func1()
{
addrreg = (unsigned __int16)switchreg;
return updateregsdisplay();
}

original_code:
void load_adrs_switch() {
 addrreg = switchreg;
 updateregsdisplay();
}


decompiled_code:
__int64 exam_switch()
{
__int16 v0; // ax

v0 = getWord((unsigned int)addrreg);
++addrreg;
datareg = v0;
return updateregsdisplay();
}

canonical_decompiled_code:
__int64 func2()
{
__int16 v0; // ax

v0 = getWord((unsigned int)addrreg);
++addrreg;
datareg = v0;
return updateregsdisplay();
}

original_code:
void exam_switch() {
 datareg = getWord(addrreg);
 addrreg++;
 updateregsdisplay();
}


decompiled_code:
int cont_switch()
{
int result; // eax

if ( halt )
{
fetchEx();
result = wprintw(tt, "c");
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax

if ( halt )
{
fetchEx();
result = wprintw(tt, "c");
}
return result;
}

original_code:
void cont_switch() {
 if(halt) {
  fetchEx();
  wprintw(tt, "c");
 }
}


decompiled_code:
void enable_switch()
{
halt = halt == 0;
}

canonical_decompiled_code:
void func4()
{
halt = halt == 0;
}

original_code:
void enable_switch() {
 halt = !halt;
}

canonical_original_code:
void enable_switch() {
 halt = !halt;
}


decompiled_code:
void start_switch()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
void start_switch() {
}

canonical_original_code:
void start_switch() {
}


decompiled_code:
__int64 dep_switch()
{
__int64 v0; // rdi

v0 = addrreg;
if ( (addrreg & 1) != 0 )
v0 = --addrreg;
setWord(v0, (unsigned __int16)switchreg);
addrreg += 2LL;
return updateregsdisplay();
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rdi

v0 = addrreg;
if ( (addrreg & 1) != 0 )
v0 = --addrreg;
setWord(v0, (unsigned __int16)switchreg);
addrreg += 2LL;
return updateregsdisplay();
}

original_code:
void dep_switch() {
 if(addrreg % 2 != 0) {
  addrreg--;
 }
 setWord(addrreg, switchreg);
 addrreg += 2;
 updateregsdisplay();
}


gethalt

decompiled_code:
__int64 gethalt()
{
return (unsigned __int8)halt;
}

canonical_decompiled_code:
__int64 func7()
{
return (unsigned __int8)halt;
}

original_code:
char gethalt() {
 return halt;
}

canonical_original_code:
char gethalt() {
 return halt;
}

return_type: char


sethalt

decompiled_code:
void __fastcall sethalt(char a1)
{
halt = a1;
}

canonical_decompiled_code:
void __fastcall func8(char a1)
{
halt = a1;
}

original_code:
void sethalt(char hlt) {
 halt = hlt;
}

canonical_original_code:
void sethalt(char hlt) {
 halt = hlt;
}

variable_types: hlt char


getdatareg

decompiled_code:
__int64 getdatareg()
{
return (unsigned __int16)datareg;
}

canonical_decompiled_code:
__int64 func9()
{
return (unsigned __int16)datareg;
}

original_code:
unsigned short getdatareg() {
 return datareg;
}


getswitchreg

decompiled_code:
__int64 getswitchreg()
{
return (unsigned __int16)switchreg;
}

canonical_decompiled_code:
__int64 func10()
{
return (unsigned __int16)switchreg;
}

original_code:
unsigned short getswitchreg() {
 return switchreg;
}


getaddrreg

decompiled_code:
__int64 getaddrreg()
{
return addrreg;
}

canonical_decompiled_code:
__int64 func11()
{
return addrreg;
}

original_code:
unsigned long getaddrreg() {
 return addrreg;
}

return_type: unsigned long


lineclockio

decompiled_code:
__int64 lineclockio()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
unsigned short lineclockio(const char * command, unsigned int addr, unsigned short value) {
 if(!strcmp(command, "read")) {
  return 1;
 }
 return 1;
}

canonical_original_code:
unsigned short lineclockio(char *command, unsigned int addr, unsigned short value) {
 if(!strcmp(command, "read")) {
  return 1;
 }
 return 1;
}


startlineclock

decompiled_code:
clock_t startlineclock()
{
clock_t result; // rax

result = clock() + 20000;
endclock = result;
return result;
}

canonical_decompiled_code:
clock_t func1()
{
clock_t result; // rax

result = clock() + 20000;
endclock = result;
return result;
}

original_code:
void startlineclock() {
 endclock = clock() + (((__clock_t) 1000000) / 50);
}

canonical_original_code:
void startlineclock() {
 endclock = clock() + (((long) 1000000) / 50);
}


lineclock

decompiled_code:
clock_t lineclock()
{
clock_t result; // rax

result = clock();
if ( result >= endclock )
{
endclock = clock() + 20000;
result = busRequest(&BR6, 64LL);
}
return result;
}

canonical_decompiled_code:
clock_t func2()
{
clock_t result; // rax

result = clock();
if ( result >= endclock )
{
endclock = clock() + 20000;
result = busRequest(&BR6, 64LL);
}
return result;
}

original_code:
void lineclock() {
 if(clock() >= endclock) {
  endclock = clock() + (((__clock_t) 1000000) / 50);
  busRequest(&BR6, 0100);
 }
}

canonical_original_code:
void lineclock() {
 if(clock() >= endclock) {
  endclock = clock() + (((long) 1000000) / 50);
  busRequest(&BR6, 0100);
 }
}


decompiled_code:
int my_segment_init()
{
void *v0; // rax
__int64 v1; // r13
const void *v2; // r12
unsigned int v3; // eax

segment_size = sysconf(30);
v0 = mmap(0LL, segment_size, 3, 34, 0, 0LL);
segment_start = (__int64)v0;
if ( v0 == (void *)-1LL )
{
perror("mmap");
exit(1);
}
v1 = segment_size;
v2 = v0;
v3 = barrier_threadid();
return printf("%x : initialized %ld bytes at %p\n", v3, v1, v2);
}

canonical_decompiled_code:
int func0()
{
void *v0; // rax
__int64 v1; // r13
const void *v2; // r12
unsigned int v3; // eax

segment_size = sysconf(30);
v0 = mmap(0LL, segment_size, 3, 34, 0, 0LL);
segment_start = (__int64)v0;
if ( v0 == (void *)-1LL )
{
perror("mmap");
exit(1);
}
v1 = segment_size;
v2 = v0;
v3 = barrier_threadid();
return printf("%x : initialized %ld bytes at %p\n", v3, v1, v2);
}

original_code:
void my_segment_init()
{
    segment_size = sysconf(_SC_PAGESIZE);
    segment_start = mmap(((void *)0),
            segment_size,
            0x1 | 0x2,
            0x02 | 0x20,
            0, 0);
    if(segment_start == ((void *) -1)) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mmap"); exit(1); } while (0);
    }
    printf("%x : initialized %ld bytes at %p\n", barrier_threadid(), segment_size, segment_start);
}


decompiled_code:
__int64 my_barrier_init()
{
pthread_t v0; // rax
__int64 v1; // rax

v0 = pthread_self();
*(&barrier + 8) = (__int64)&whitelist;
whitelist = v0;
v1 = segment_start;
*((_DWORD *)&barrier + 15) = 3;
barrier = v1;
*(&barrier + 1) = segment_size;
return barrier_init(&barrier);
}

canonical_decompiled_code:
__int64 func1()
{
pthread_t v0; // rax
__int64 v1; // rax

v0 = pthread_self();
*(&barrier + 8) = (__int64)&whitelist;
whitelist = v0;
v1 = segment_start;
*((_DWORD *)&barrier + 15) = 3;
barrier = v1;
*(&barrier + 1) = segment_size;
return barrier_init(&barrier);
}

original_code:
void my_barrier_init()
{
    whitelist[0] = pthread_self();
    barrier.start = (void*)segment_start;
    barrier.size = segment_size;
    barrier.whitelist = whitelist;
    barrier.original_prot = 0x1 | 0x2;
    barrier_init(&barrier);
}

canonical_original_code:
void my_barrier_init()
{
    whitelist[0] = pthread_self();
    barrier.start = (void *)segment_start;
    barrier.size = segment_size;
    barrier.whitelist = whitelist;
    barrier.original_prot = 0x1 | 0x2;
    barrier_init(&barrier);
}


decompiled_code:
int increment_counter()
{
unsigned int *v0; // roff
unsigned int v1; // er12
unsigned int v2; // eax

v0 = (unsigned int *)segment_start;
_InterlockedAdd((volatile signed __int32 *)segment_start, 1u);
v1 = *v0;
v2 = barrier_threadid();
return printf("%x : incremented counter to %d\n", v2, v1);
}

canonical_decompiled_code:
int func2()
{
unsigned int *v0; // roff
unsigned int v1; // er12
unsigned int v2; // eax

v0 = (unsigned int *)segment_start;
_InterlockedAdd((volatile signed __int32 *)segment_start, 1u);
v1 = *v0;
v2 = barrier_threadid();
return printf("%x : incremented counter to %d\n", v2, v1);
}

original_code:
void increment_counter()
{
    volatile sig_atomic_t* counter = (volatile sig_atomic_t*)segment_start;
 __sync_fetch_and_add(counter, 1);
    printf("%x : incremented counter to %d\n", barrier_threadid(), *counter);
}

canonical_original_code:
void increment_counter()
{
    int *counter = (volatile sig_atomic_t*)segment_start;
 __sync_fetch_and_add(counter, 1);
    printf("%x : incremented counter to %d\n", barrier_threadid(), *counter);
}

variable_types:
counter
int *


decompiled_code:
int peek_counter()
{
unsigned int v0; // er12
unsigned int v1; // eax

v0 = *(_DWORD *)segment_start;
v1 = barrier_threadid();
return printf("%x : it looks like the counter is %d\n", v1, v0);
}

canonical_decompiled_code:
int func3()
{
unsigned int v0; // er12
unsigned int v1; // eax

v0 = *(_DWORD *)segment_start;
v1 = barrier_threadid();
return printf("%x : it looks like the counter is %d\n", v1, v0);
}

original_code:
void peek_counter()
{
    printf("%x : it looks like the counter is %d\n", barrier_threadid(), *((sig_atomic_t*)segment_start));
}

canonical_original_code:
void peek_counter()
{
    printf("%x : it looks like the counter is %d\n", barrier_threadid(), *((int *)segment_start));
}


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

v0 = rand();
return sleep(v0 % 10);
}

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

v0 = rand();
return sleep(v0 % 10);
}

original_code:
void rand_sleep()
{
    int sleep_for = rand() % 10;
    sleep(sleep_for);
}


decompiled_code:
void *__fastcall worker_main(void *a1)
{
unsigned int v1; // eax

v1 = barrier_threadid(a1);
printf("%x : started\n", v1);
while ( running )
{
rand_sleep();
increment_counter();
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
unsigned int v1; // eax

v1 = barrier_threadid(a1);
printf("%x : started\n", v1);
while ( running )
{
func4();
func2();
}
return 0LL;
}

original_code:
void* worker_main(void *arg)
{
    printf("%x : started\n", barrier_threadid());
    while(running) {
        rand_sleep();
        increment_counter();
    }
    return 0;
}

canonical_original_code:
void *worker_main(void *arg)
{
    printf("%x : started\n", barrier_threadid());
    while(running) {
        rand_sleep();
        increment_counter();
    }
    return 0;
}

variable_types:
arg
void *


decompiled_code:
int create_workers()
{
pthread_t *v0; // rbx
int result; // eax

v0 = &thread;
srand(1u);
do
{
result = pthread_create(v0, 0LL, worker_main, 0LL);
if ( result )
{
*_errno_location() = result;
perror("pthread_create");
exit(1);
}
++v0;
}
while ( v0 != &thread + 10 );
return result;
}

canonical_decompiled_code:
int func6()
{
pthread_t *v0; // rbx
int result; // eax

v0 = &thread;
srand(1u);
do
{
result = pthread_create(v0, 0LL, worker_main, 0LL);
if ( result )
{
*_errno_location() = result;
perror("pthread_create");
exit(1);
}
++v0;
}
while ( v0 != &thread + 10 );
return result;
}

original_code:
void create_workers()
{
    srand(1);
    int i, code;
    for (i = 0; i < 10; i++) {
        code = pthread_create(&thread[i], ((void *)0), &worker_main, ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_create"); exit(1); } while (0);
    }
}

canonical_original_code:
void create_workers()
{
    srand(1);
    int i; int code;
    for (i = 0; i < 10; i++) {
        code = pthread_create(&thread[i], ((void *)0), &worker_main, ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_create"); exit(1); } while (0);
    }
}

variable_types: code int


decompiled_code:
int join_workers()
{
pthread_t *v0; // rbx
pthread_t v1; // rdi
int v2; // ebp
pthread_t v3; // rbp
unsigned int v4; // eax
int result; // eax

v0 = &thread;
do
{
v1 = *v0;
v2 = pthread_join(*v0, 0LL);
if ( v2 )
{
*_errno_location() = v2;
perror("pthread_join");
exit(1);
}
v3 = *v0++;
v4 = barrier_threadid(v1);
result = printf("%x : waiting for %x\n", v4, (unsigned int)v3);
}
while ( v0 != &thread + 10 );
return result;
}

canonical_decompiled_code:
int func7()
{
pthread_t *v0; // rbx
pthread_t v1; // rdi
int v2; // ebp
pthread_t v3; // rbp
unsigned int v4; // eax
int result; // eax

v0 = &thread;
do
{
v1 = *v0;
v2 = pthread_join(*v0, 0LL);
if ( v2 )
{
*_errno_location() = v2;
perror("pthread_join");
exit(1);
}
v3 = *v0++;
v4 = barrier_threadid(v1);
result = printf("%x : waiting for %x\n", v4, (unsigned int)v3);
}
while ( v0 != &thread + 10 );
return result;
}

original_code:
void join_workers()
{
    int i, code;
    for (i = 0; i < 10; i++) {
        code = pthread_join(thread[i], ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_join"); exit(1); } while (0);
        printf("%x : waiting for %x\n", barrier_threadid(), (unsigned int)thread[i]);
    }
}

canonical_original_code:
void join_workers()
{
    int i; int code;
    for (i = 0; i < 10; i++) {
        code = pthread_join(thread[i], ((void *)0));
        if (code != 0)
            do { (*__errno_location ()) = code; perror("pthread_join"); exit(1); } while (0);
        printf("%x : waiting for %x\n", barrier_threadid(), (unsigned int)thread[i]);
    }
}

variable_types: i int


decompiled_code:
__int64 main_loop()
{
__int64 result; // rax

for ( result = (unsigned int)running; running; result = (unsigned int)running )
{
sleep(6u);
barrier_activate(&barrier);
sleep(2u);
peek_counter();
barrier_deactivate(&barrier);
}
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax

for ( result = (unsigned int)running; running; result = (unsigned int)running )
{
sleep(6u);
barrier_activate(&barrier);
sleep(2u);
func3();
barrier_deactivate(&barrier);
}
return result;
}

original_code:
void main_loop()
{
    while(running) {
        sleep(6);
        barrier_activate(&barrier);
        sleep(2);
        peek_counter();
        barrier_deactivate(&barrier);
    }
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

my_segment_init();
my_barrier_init(argc, argv);
create_workers();
main_loop();
join_workers();
barrier_destroy(&barrier);
v3 = barrier_threadid(&barrier);
printf("%x : exiting", v3);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

func0();
func1(argc, argv);
func6();
func8();
func7();
barrier_destroy(&barrier);
v3 = barrier_threadid(&barrier);
printf("%x : exiting", v3);
exit(0);
}

original_code:
int main(int argc, char *argv[])
{
    my_segment_init();
    my_barrier_init();
    create_workers();
    main_loop();
    join_workers();
    barrier_destroy(&barrier);
    printf("%x : exiting", barrier_threadid());
    exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
int barrierlist_init()
{
init = 1;
return sem_init(&lock, 0, 1u);
}

canonical_decompiled_code:
int func0()
{
init = 1;
return sem_init(&lock, 0, 1u);
}

original_code:
void barrierlist_init()
{
    init = 1;
    sem_init(&lock, 0, 1);
}


decompiled_code:
int __fastcall barrierlist_add(__int64 a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbx
_QWORD *v3; // rax

if ( !init )
barrierlist_init();
sem_wait(&lock);
v1 = &barrierlist;
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v3 = malloc(0x10uLL);
*v3 = a1;
v2[1] = v3;
return sem_post(&lock);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rbx
_QWORD *v3; // rax

if ( !init )
func0();
sem_wait(&lock);
v1 = &barrierlist;
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v3 = malloc(0x10uLL);
*v3 = a1;
v2[1] = v3;
return sem_post(&lock);
}

original_code:
void barrierlist_add(struct barrier* barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist* list = &barrierlist;
    while(list->next != 0) {
        list = list->next;
    }
    struct barrierlist* new = (struct barrierlist*)malloc(sizeof(struct barrierlist));
    new->item = barrier;
    list->next = new;
    sem_post(&lock);
}

canonical_original_code:
void barrierlist_add(struct barrier *barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist *list = &barrierlist;
    while(list->next != 0) {
        list = list->next;
    }
    struct barrierlist *new = (struct barrierlist*)malloc(sizeof(struct barrierlist));
    new->item = barrier;
    list->next = new;
    sem_post(&lock);
}

variable_types: struct barrierlist { struct barrier { char *start; unsigned long size; int locked; union sem_t { char __size[32]; long __align; } wait_sem; int waiting; int original_prot; unsigned long *whitelist; } *item; struct barrierlist *next; } *

user_defined_types: struct barrier { char *start; unsigned long size; int locked; union sem_t wait_sem; int waiting; int original_prot; unsigned long *whitelist; }

user_defined_types: struct barrierlist { struct barrier *item; struct barrierlist *next; }


decompiled_code:
int __fastcall barrierlist_remove(__int64 a1)
{
__int64 *v2; // rax
__int64 v3; // rdx
__int64 *v4; // rdi

if ( !init )
barrierlist_init();
sem_wait(&lock);
v2 = (__int64 *)*((_QWORD *)&barrierlist + 1);
do
{
if ( !v2 )
return sem_post(&lock);
v3 = *v2;
v4 = v2;
v2 = (__int64 *)v2[1];
}
while ( v3 != a1 );
*((_QWORD *)&barrierlist + 1) = v2;
free(v4);
return sem_post(&lock);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
__int64 *v2; // rax
__int64 v3; // rdx
__int64 *v4; // rdi

if ( !init )
func0();
sem_wait(&lock);
v2 = (__int64 *)*((_QWORD *)&barrierlist + 1);
do
{
if ( !v2 )
return sem_post(&lock);
v3 = *v2;
v4 = v2;
v2 = (__int64 *)v2[1];
}
while ( v3 != a1 );
*((_QWORD *)&barrierlist + 1) = v2;
free(v4);
return sem_post(&lock);
}

original_code:
void barrierlist_remove(struct barrier* barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist* list = barrierlist.next;
    struct barrierlist* prev = &barrierlist;
    while(list != 0) {
        if(list->item == barrier) {
            prev->next = list->next;
            free(list);
            break;
        }
        list = list->next;
    }
    sem_post(&lock);
}

canonical_original_code:
void barrierlist_remove(struct barrier *barrier)
{
    if(init == 0) {
        barrierlist_init();
    }
    sem_wait(&lock);
    struct barrierlist *list = barrierlist.next;
    struct barrierlist *prev = &barrierlist;
    while(list != 0) {
        if(list->item == barrier) {
            prev->next = list->next;
            free(list);
            break;
        }
        list = list->next;
    }
    sem_post(&lock);
}

variable_types: struct barrier { char *start; unsigned long size; int locked; union sem_t { char __size[32]; long __align; } wait_sem; int waiting; int original_prot; unsigned long *whitelist; } *


decompiled_code:
int __fastcall handle_segv(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
pthread_t v4; // rax
int v5; // eax
const void *v6; // r12
unsigned int v7; // eax
int v8; // edx
unsigned int v9; // eax
unsigned int v10; // eax

v2 = barrierlist_match(*(_QWORD *)(a2 + 16));
if ( v2 )
{
v3 = v2;
v4 = pthread_self();
v5 = barrier_iswhitelisted(v3, v4);
v6 = *(const void **)v3;
if ( v5 )
{
v10 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p; whitelisted\n", v10, v6);
}
else
{
v7 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p\n", v7, v6);
v8 = *(_DWORD *)(v3 + 16);
++*(_DWORD *)(v3 + 56);
if ( v8 )
{
do
{
LODWORD(v2) = sem_wait((sem_t *)(v3 + 24));
if ( !(_DWORD)v2 )
break;
if ( *_errno_location() != 4 )
{
perror("sem_wait");
exit(1);
}
v9 = barrier_threadid();
printf("%x : woken up by interrupt", v9);
LODWORD(v2) = *(_DWORD *)(v3 + 16);
}
while ( (_DWORD)v2 );
}
}
}
return v2;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
pthread_t v4; // rax
int v5; // eax
const void *v6; // r12
unsigned int v7; // eax
int v8; // edx
unsigned int v9; // eax
unsigned int v10; // eax

v2 = barrierlist_match(*(_QWORD *)(a2 + 16));
if ( v2 )
{
v3 = v2;
v4 = pthread_self();
v5 = barrier_iswhitelisted(v3, v4);
v6 = *(const void **)v3;
if ( v5 )
{
v10 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p; whitelisted\n", v10, v6);
}
else
{
v7 = barrier_threadid();
LODWORD(v2) = printf("%x : hit memory barrier %p\n", v7, v6);
v8 = *(_DWORD *)(v3 + 16);
++*(_DWORD *)(v3 + 56);
if ( v8 )
{
do
{
LODWORD(v2) = sem_wait((sem_t *)(v3 + 24));
if ( !(_DWORD)v2 )
break;
if ( *_errno_location() != 4 )
{
perror("sem_wait");
exit(1);
}
v9 = barrier_threadid();
printf("%x : woken up by interrupt", v9);
LODWORD(v2) = *(_DWORD *)(v3 + 16);
}
while ( (_DWORD)v2 );
}
}
}
return v2;
}

original_code:
void handle_segv(int sig, siginfo_t* siginfo, void* context)
{
    struct barrier* barrier = (struct barrier*) barrierlist_match(siginfo->_sifields._sigfault.si_addr);
    if(barrier != 0) {
        if(barrier_iswhitelisted(barrier, pthread_self())) {
            printf("%x : hit memory barrier %p; whitelisted\n", barrier_threadid(), barrier->start);
        } else {
            printf("%x : hit memory barrier %p\n", barrier_threadid(), barrier->start);
            barrier->waiting++;
            while(barrier->locked && sem_wait(&barrier->wait_sem) != 0) {
                if((*__errno_location ()) != 4) {
                    do { (*__errno_location ()) = (*__errno_location ()); perror("sem_wait"); exit(1); } while (0);
                }
                printf("%x : woken up by interrupt", barrier_threadid());
            }
        }
    }
}

canonical_original_code:
void handle_segv(int sig, struct siginfo_t *siginfo, void *context)
{
    struct barrier *barrier = (struct barrier*) barrierlist_match(siginfo->_sifields._sigfault.si_addr);
    if(barrier != 0) {
        if(barrier_iswhitelisted(barrier, pthread_self())) {
            printf("%x : hit memory barrier %p; whitelisted\n", barrier_threadid(), barrier->start);
        } else {
            printf("%x : hit memory barrier %p\n", barrier_threadid(), barrier->start);
            barrier->waiting++;
            while(barrier->locked && sem_wait(&barrier->wait_sem) != 0) {
                if((*__errno_location ()) != 4) {
                    do { (*__errno_location ()) = (*__errno_location ()); perror("sem_wait"); exit(1); } while (0);
                }
                printf("%x : woken up by interrupt", barrier_threadid());
            }
        }
    }
}

variable_types: sig int

variable_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 { 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; } *

variable_types:
context
void *

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

user_defined_types: union sem_t { char __size[32]; long __align; }


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

result = attached;
if ( !attached )
{
attached = 1;
v1.sa_handler = (__sighandler_t)handle_segv;
v1.sa_flags = 4;
result = sigaction(11, &v1, 0LL);
if ( result )
{
perror("sigaction");
exit(1);
}
}
return result;
}

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

result = attached;
if ( !attached )
{
attached = 1;
v1.sa_handler = (__sighandler_t)handle_segv;
v1.sa_flags = 4;
result = sigaction(11, &v1, 0LL);
if ( result )
{
perror("sigaction");
exit(1);
}
}
return result;
}

original_code:
void segvhandler_attach()
{
    if(attached)
        return;
    attached = 1;
    struct sigaction newact;
    newact.sa_flags = 4;
    newact.__sigaction_handler.sa_sigaction = &handle_segv;
    if(sigaction(11, &newact, ((void *)0)) != 0) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("sigaction"); exit(1); } while (0);
    }
}

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

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


decompiled_code:
void segvhandler_detatch()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void segvhandler_detatch()
{
}

canonical_original_code:
void segvhandler_detatch()
{
}


decompiled_code:
__int64 segvhandler_isattached()
{
return (unsigned int)attached;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)attached;
}

original_code:
int segvhandler_isattached()
{
    return attached;
}


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

if ( *(_DWORD *)(a1 + 60) )
{
if ( *(_QWORD *)(a1 + 8) )
{
if ( *(_QWORD *)a1 )
{
sem_init((sem_t *)(a1 + 24), 0, 0);
barrierlist_add(a1);
segvhandler_attach();
result = 0LL;
}
else
{
perror("start not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("size not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("original_prot not set");
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( *(_DWORD *)(a1 + 60) )
{
if ( *(_QWORD *)(a1 + 8) )
{
if ( *(_QWORD *)a1 )
{
sem_init((sem_t *)(a1 + 24), 0, 0);
barrierlist_add(a1);
segvhandler_attach();
result = 0LL;
}
else
{
perror("start not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("size not set");
result = 0xFFFFFFFFLL;
}
}
else
{
perror("original_prot not set");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int barrier_init(struct barrier* barrier)
{
    if(barrier->original_prot == 0) {
        perror("original_prot not set");
        return -1;
    }
    if(barrier->size == 0) {
        perror("size not set");
        return -1;
    }
    if(barrier->start == 0) {
        perror("start not set");
        return -1;
    }
    sem_init(&barrier->wait_sem, 0, 0);
    barrierlist_add(barrier);
    segvhandler_attach();
 return 0;
}

canonical_original_code:
int barrier_init(struct barrier *barrier)
{
    if(barrier->original_prot == 0) {
        perror("original_prot not set");
        return -1;
    }
    if(barrier->size == 0) {
        perror("size not set");
        return -1;
    }
    if(barrier->start == 0) {
        perror("start not set");
        return -1;
    }
    sem_init(&barrier->wait_sem, 0, 0);
    barrierlist_add(barrier);
    segvhandler_attach();
 return 0;
}


decompiled_code:
__int64 __fastcall barrier_iswhitelisted(__int64 a1, __int64 a2)
{
__int64 *v2; // rax
__int64 v3; // rdx
__int64 *v4; // rax

v2 = *(__int64 **)(a1 + 64);
v3 = *v2;
if ( !*v2 )
return 0LL;
v4 = v2 + 1;
while ( a2 != v3 )
{
v3 = *v4++;
if ( !v3 )
return 0LL;
}
return 1LL;
}

original_code:
int barrier_iswhitelisted(struct barrier* barrier, pthread_t thread)
{
    int i = 0, result = 0;
    pthread_t* whitelist = barrier->whitelist;
    while(whitelist[i] != 0) {
        if(whitelist[i] == thread) {
            result = 1;
            break;
        }
        i++;
    }
    return result;
}

canonical_original_code:
int barrier_iswhitelisted(struct barrier *barrier, unsigned long thread)
{
    int i = 0; int result = 0;
    unsigned long *whitelist = barrier->whitelist;
    while(whitelist[i] != 0) {
        if(whitelist[i] == thread) {
            result = 1;
            break;
        }
        i++;
    }
    return result;
}

variable_types:
thread
unsigned long

variable_types: result int

variable_types:
whitelist
unsigned long *


decompiled_code:
pthread_t barrier_threadid()
{
return pthread_self();
}

canonical_decompiled_code:
pthread_t func2()
{
return pthread_self();
}

original_code:
unsigned int barrier_threadid()
{
    return (unsigned int)pthread_self();
}

return_type: unsigned int


decompiled_code:
int __fastcall barrier_deactivate(__int64 a1)
{
__int64 v1; // r13
unsigned int v2; // er12
int v3; // eax
int result; // eax

v1 = (unsigned int)barrier_threadid();
printf("%x : deactivating memory barrier\n", v1);
if ( mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, *(_DWORD *)(a1 + 60)) )
{
perror("mprotect");
exit(1);
}
v2 = 0;
v3 = *(_DWORD *)(a1 + 56);
*(_DWORD *)(a1 + 16) = 0;
if ( v3 > 0 )
{
do
{
++v2;
sem_post((sem_t *)(a1 + 24));
}
while ( *(_DWORD *)(a1 + 56) > (signed int)v2 );
}
result = printf("%x : woke up %d threads\n", (unsigned int)v1, v2);
*(_DWORD *)(a1 + 56) = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 v1; // r13
unsigned int v2; // er12
int v3; // eax
int result; // eax

v1 = (unsigned int)func2();
printf("%x : deactivating memory barrier\n", v1);
if ( mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, *(_DWORD *)(a1 + 60)) )
{
perror("mprotect");
exit(1);
}
v2 = 0;
v3 = *(_DWORD *)(a1 + 56);
*(_DWORD *)(a1 + 16) = 0;
if ( v3 > 0 )
{
do
{
++v2;
sem_post((sem_t *)(a1 + 24));
}
while ( *(_DWORD *)(a1 + 56) > (signed int)v2 );
}
result = printf("%x : woke up %d threads\n", (unsigned int)v1, v2);
*(_DWORD *)(a1 + 56) = 0;
return result;
}

original_code:
void barrier_deactivate(struct barrier* barrier)
{
    printf("%x : deactivating memory barrier\n", barrier_threadid());
    if(mprotect(barrier->start, barrier->size-1, barrier->original_prot) != 0) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
    barrier->locked = 0;
    int i;
    for(i = 0; i < barrier->waiting; i++) {
        sem_post(&barrier->wait_sem);
    }
    printf("%x : woke up %d threads\n", barrier_threadid(), i);
    barrier->waiting = 0;
}

canonical_original_code:
void barrier_deactivate(struct barrier *barrier)
{
    printf("%x : deactivating memory barrier\n", barrier_threadid());
    if(mprotect(barrier->start, barrier->size-1, barrier->original_prot) != 0) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
    barrier->locked = 0;
    int i;
    for(i = 0; i < barrier->waiting; i++) {
        sem_post(&barrier->wait_sem);
    }
    printf("%x : woke up %d threads\n", barrier_threadid(), i);
    barrier->waiting = 0;
}


decompiled_code:
__int64 __fastcall barrier_destroy(__int64 a1)
{
if ( *(_DWORD *)(a1 + 16) )
barrier_deactivate(a1);
segvhandler_detatch();
return barrierlist_remove(a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
if ( *(_DWORD *)(a1 + 16) )
func3(a1);
segvhandler_detatch();
return barrierlist_remove(a1);
}

original_code:
void barrier_destroy(struct barrier* barrier)
{
    if(barrier->locked != 0) {
        barrier_deactivate(barrier);
    }
    segvhandler_detatch();
    barrierlist_remove(barrier);
}

canonical_original_code:
void barrier_destroy(struct barrier *barrier)
{
    if(barrier->locked != 0) {
        barrier_deactivate(barrier);
    }
    segvhandler_detatch();
    barrierlist_remove(barrier);
}


decompiled_code:
int __fastcall barrier_activate(__int64 a1)
{
unsigned int v1; // eax
int result; // eax

*(_DWORD *)(a1 + 16) = 1;
v1 = barrier_threadid();
printf("%x : activating memory barrier for %p\n", v1, *(const void **)a1);
result = mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, 1);
if ( result )
{
perror("mprotect");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
unsigned int v1; // eax
int result; // eax

*(_DWORD *)(a1 + 16) = 1;
v1 = func2();
printf("%x : activating memory barrier for %p\n", v1, *(const void **)a1);
result = mprotect(*(void **)a1, *(_QWORD *)(a1 + 8) - 1LL, 1);
if ( result )
{
perror("mprotect");
exit(1);
}
return result;
}

original_code:
void barrier_activate(struct barrier* barrier)
{
    barrier->locked = 1;
    printf("%x : activating memory barrier for %p\n", barrier_threadid(), barrier->start);
    if(mprotect(barrier->start, barrier->size - 1, 0x1)) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
}

canonical_original_code:
void barrier_activate(struct barrier *barrier)
{
    barrier->locked = 1;
    printf("%x : activating memory barrier for %p\n", barrier_threadid(), barrier->start);
    if(mprotect(barrier->start, barrier->size - 1, 0x1)) {
        do { (*__errno_location ()) = (*__errno_location ()); perror("mprotect"); exit(1); } while (0);
    }
}


unlock

repo: jonhoo/cryptsetup-gui

decompiled_code:
__int64 __fastcall unlock(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // er12
__int64 result; // rax

LOBYTE(v1) = decrypt((__int64)name, (__int64)device, (const char *)options, a1);
v2 = v1;
if ( (_BYTE)v1 )
{
if ( do_mount )
{
if ( !mount(mountpoint) )
{
v2 = 0;
fprintf(stderr, "Failed to mount device %s\n", name);
}
}
result = v2;
}
else
{
fprintf(stderr, "Failed to decrypt device %s\n", name);
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // er12
__int64 result; // rax

LOBYTE(v1) = func7((__int64)name, (__int64)device, (const char *)options, a1);
v2 = v1;
if ( (_BYTE)v1 )
{
if ( do_mount )
{
if ( !func8(mountpoint) )
{
v2 = 0;
fprintf(stderr, "Failed to mount device %s\n", name);
}
}
result = v2;
}
else
{
fprintf(stderr, "Failed to decrypt device %s\n", name);
result = v2;
}
return result;
}

original_code:
_Bool unlock(char* password) {
  if (!decrypt(name, device, options, password)) {
    fprintf(stderr, "Failed to decrypt device %s\n", name);
    return 0;
  }
  if (do_mount) {
    if (!mount(mountpoint)) {
      fprintf(stderr, "Failed to mount device %s\n", name);
      return 0;
    }
  }
  return 1;
}

canonical_original_code:
_Bool unlock(char *password) {
  if (!decrypt(name, device, options, password)) {
    fprintf(stderr, "Failed to decrypt device %s\n", name);
    return 0;
  }
  if (do_mount) {
    if (!mount(mountpoint)) {
      fprintf(stderr, "Failed to mount device %s\n", name);
      return 0;
    }
  }
  return 1;
}

variable_types:
password
char *


decompiled_code:
__int64 __fastcall is_space(int a1, __int64 a2, int a3)
{
unsigned int v3; // eax

v3 = a1 - 9;
LOBYTE(v3) = (unsigned __int8)(a1 - 9) <= 1u;
LOBYTE(a3) = (_BYTE)a1 == 32;
return a3 | v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, int a3)
{
unsigned int v3; // eax

v3 = a1 - 9;
LOBYTE(v3) = (unsigned __int8)(a1 - 9) <= 1u;
LOBYTE(a3) = (_BYTE)a1 == 32;
return a3 | v3;
}

original_code:
_Bool is_space(char s) {
  return s == ' ' || s == '\t' || s == '\n';
}

variable_types: s char


strstrip

decompiled_code:
const char *__fastcall strstrip(const char *a1, __int64 a2)
{
const char *v2; // r12
size_t v3; // rax
int v4; // edx
const char *i; // rcx
int v6; // edi

v2 = a1;
v3 = strlen(a1);
if ( v3 )
{
for ( i = &a1[v3 - 1]; a1 <= i; --i )
{
if ( !(unsigned __int8)is_space(*i, a2, v4) )
break;
}
*((_BYTE *)i + 1) = 0;
v6 = *a1;
if ( *v2 )
{
do
{
if ( !(unsigned __int8)is_space(v6, a2, v4) )
break;
v6 = *++v2;
}
while ( (_BYTE)v6 );
}
}
return v2;
}

canonical_decompiled_code:
const char *__fastcall func11(const char *a1, __int64 a2)
{
const char *v2; // r12
size_t v3; // rax
int v4; // edx
const char *i; // rcx
int v6; // edi

v2 = a1;
v3 = strlen(a1);
if ( v3 )
{
for ( i = &a1[v3 - 1]; a1 <= i; --i )
{
if ( !(unsigned __int8)func10(*i, a2, v4) )
break;
}
*((_BYTE *)i + 1) = 0;
v6 = *a1;
if ( *v2 )
{
do
{
if ( !(unsigned __int8)func10(v6, a2, v4) )
break;
v6 = *++v2;
}
while ( (_BYTE)v6 );
}
}
return v2;
}

original_code:
char *strstrip(char *s) {
    size_t size;
    char *end;
    size = strlen(s);
    if (!size)
        return s;
    end = s + size - 1;
    while (end >= s && is_space(*end))
        end--;
    *(end + 1) = '\0';
    while (*s && is_space(*s))
        s++;
    return s;
}

canonical_original_code:
char *strstrip(char *s) {
    unsigned long size;
    char *end;
    size = strlen(s);
    if (!size)
        return s;
    end = s + size - 1;
    while (end >= s && is_space(*end))
        end--;
    *(end + 1) = '\0';
    while (*s && is_space(*s))
        s++;
    return s;
}

variable_types:
s
char *

variable_types:
size
unsigned long

variable_types:
end
char *

return_type: char *


usage

decompiled_code:
int usage()
{
return printf("Usage: %s [-m] cryptpoint\n", (const char *)arg0);
}

canonical_decompiled_code:
int func13()
{
return printf("Usage: %s [-m] cryptpoint\n", (const char *)arg0);
}

original_code:
void usage() {
  printf("Usage: %s [-m] cryptpoint\n", arg0);
}


nybble

decompiled_code:
__int64 nybble()
{
__int64 result; // rax

*(_DWORD *)(gpio + 28LL) = 0x2000000;
usleep(0x3E8u);
result = gpio;
*(_DWORD *)(gpio + 40LL) = 0x2000000;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

*(_DWORD *)(gpio + 28LL) = 0x2000000;
usleep(0x3E8u);
result = gpio;
*(_DWORD *)(gpio + 40LL) = 0x2000000;
return result;
}


command

decompiled_code:
__int64 __fastcall command(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: command 0x%x\n", "lcd.c", "command", 25LL, (unsigned int)a1);
*(_DWORD *)(gpio + 40LL) = 2048;
write_output(a1);
nybble();
write_output(16 * a1);
return nybble();
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: command 0x%x\n", "lcd.c", "command", 25LL, (unsigned int)a1);
*(_DWORD *)(gpio + 40LL) = 2048;
func0(a1);
func1();
func0(16 * a1);
return func1();
}

original_code:
void command(char i)
{
 fprintf(stderr, "DEBUG %s:%s:%d: " "command 0x%x" "\n", "lcd.c", __func__, 25, i);
 if (0) { *(gpio+7) = (1 << (11)); } else { *(gpio+10) = (1 << (11)); };
 write_output(i);
 nybble();
 i <<= 4;
 write_output(i);
 nybble();
}

variable_types: i char


decompiled_code:
__int64 __fastcall lcd_write(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: writing character %c\n", "lcd.c", "lcd_write", 36LL, (unsigned int)a1);
*(_DWORD *)(gpio + 28LL) = 2048;
write_output(a1);
nybble();
write_output(16 * a1);
return nybble();
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
fprintf(stderr, "DEBUG %s:%s:%d: writing character %c\n", "lcd.c", "lcd_write", 36LL, (unsigned int)a1);
*(_DWORD *)(gpio + 28LL) = 2048;
func0(a1);
func1();
func0(16 * a1);
return func1();
}

original_code:
void lcd_write(char i)
{
 fprintf(stderr, "DEBUG %s:%s:%d: " "writing character %c" "\n", "lcd.c", __func__, 36, i);
 if (1) { *(gpio+7) = (1 << (11)); } else { *(gpio+10) = (1 << (11)); };
 write_output(i);
 nybble();
 i <<= 4;
 write_output(i);
 nybble();
}


decompiled_code:
__int64 __fastcall lcd_print(char *a1, __int16 a2)
{
char v3; // di
__int64 result; // rax
char i; // di

v3 = -64;
if ( a2 != 2 )
v3 = 0x80;
result = command(v3);
for ( i = *a1; i; ++a1 )
{
result = lcd_write(i);
i = a1[1];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, __int16 a2)
{
char v3; // di
__int64 result; // rax
char i; // di

v3 = -64;
if ( a2 != 2 )
v3 = 0x80;
result = func2(v3);
for ( i = *a1; i; ++a1 )
{
result = func3(i);
i = a1[1];
}
return result;
}

original_code:
void lcd_print(char* str, short line)
{
 command(0x80 | (line == 2 ? 0x40 : 0));
 while (*str) {
  lcd_write(*str++);
 }
}

canonical_original_code:
void lcd_print(char *str, short line)
{
 command(0x80 | (line == 2 ? 0x40 : 0));
 while (*str) {
  lcd_write(*str++);
 }
}

variable_types:
str
char *

variable_types: line short


main

repo: joodicator/utility

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
void *v4; // rax
char *v5; // rax
__int64 v7; // [rsp+8h] [rbp-20h] BYREF
__int64 v8; // [rsp+10h] [rbp-18h] BYREF
char *s; // [rsp+18h] [rbp-10h] BYREF

v3 = 1;
v7 = 1024LL;
v4 = malloc(0x400uLL);
for ( s = (char *)safe(v4, argv); !feof(stdin); fputs(s, _bss_start) )
{
read_line(&s, &v7, &v8);
if ( !v8 )
break;
v5 = &s[v8 - 1];
if ( *v5 == 10 )
*v5 = 0;
if ( !v3 )
fputc(10, _bss_start);
v3 = 0;
}
fflush(_bss_start);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
void *v4; // rax
char *v5; // rax
__int64 v7; // [rsp+8h] [rbp-20h] BYREF
__int64 v8; // [rsp+10h] [rbp-18h] BYREF
char *s; // [rsp+18h] [rbp-10h] BYREF

v3 = 1;
v7 = 1024LL;
v4 = malloc(0x400uLL);
for ( s = (char *)func7(v4, argv); !feof(stdin); fputs(s, _bss_start) )
{
func8(&s, &v7, &v8);
if ( !v8 )
break;
v5 = &s[v8 - 1];
if ( *v5 == 10 )
*v5 = 0;
if ( !v3 )
fputc(10, _bss_start);
v3 = 0;
}
fflush(_bss_start);
return 0;
}

original_code:
int main(void) {
    size_t buf_s = BLOCK_SIZE;
    size_t len;
    char* buf = safe(malloc(buf_s));
    int first = 1;
    while (!feof(stdin)) {
        read_line(&buf, &buf_s, &len);
        if (!len) break;
        if (buf[len-1] == '\n') buf[len-1] = '\0';
        if (first) first = 0;
        else fputc('\n', stdout);
        fputs(buf, stdout);
    }
    fflush(stdout);
    return 0;
}

canonical_original_code:
int main() {
    unsigned long buf_s = BLOCK_SIZE;
    unsigned long len;
    char *buf = safe(malloc(buf_s));
    int first = 1;
    while (!feof(stdin)) {
        read_line(&buf, &buf_s, &len);
        if (!len) break;
        if (buf[len-1] == '\n') buf[len-1] = '\0';
        if (first) first = 0;
        else fputc('\n', stdout);
        fputs(buf, stdout);
    }
    fflush(stdout);
    return 0;
}

variable_types:
len
unsigned long

variable_types:
buf
char *

variable_types: first int


safe

decompiled_code:
__int64 __fastcall safe(__int64 a1)
{
if ( !a1 )
{
perror("perror");
exit(1);
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
if ( !a1 )
{
perror("perror");
exit(1);
}
return a1;
}

original_code:
void* safe(void* ptr) {
    if (ptr != ((void *)0)) return ptr;
    perror("perror");
    exit(1);
}

canonical_original_code:
void *safe(void *ptr) {
    if (ptr != ((void *)0)) return ptr;
    perror("perror");
    exit(1);
}

variable_types:
ptr
void *


decompiled_code:
int __fastcall read_line(const char **a1, _DWORD *a2, size_t *a3)
{
const char *v5; // rax
size_t v7; // rax
FILE *v8; // rdi
size_t v9; // rax
char *v10; // rdi
size_t v11; // rsi
void *v12; // rax

v5 = *a1;
*a3 = 0LL;
*v5 = 0;
while ( 1 )
{
fgets((char *)&(*a1)[*a3], *a2 - *a3, stdin);
if ( ferror(stdin) )
safe(0LL);
v7 = strlen(*a1);
v8 = stdin;
*a3 = v7;
LODWORD(v9) = feof(v8);
if ( (_DWORD)v9 )
break;
v10 = (char *)*a1;
v9 = *a3;
if ( (*a1)[*a3 - 1] == 10 )
break;
v11 = *(_QWORD *)a2 + 1024LL;
*(_QWORD *)a2 = v11;
v12 = realloc(v10, v11);
safe((__int64)v12);
}
return v9;
}

canonical_decompiled_code:
int __fastcall func8(const char **a1, _DWORD *a2, size_t *a3)
{
const char *v5; // rax
size_t v7; // rax
FILE *v8; // rdi
size_t v9; // rax
char *v10; // rdi
size_t v11; // rsi
void *v12; // rax

v5 = *a1;
*a3 = 0LL;
*v5 = 0;
while ( 1 )
{
fgets((char *)&(*a1)[*a3], *a2 - *a3, stdin);
if ( ferror(stdin) )
func7(0LL);
v7 = strlen(*a1);
v8 = stdin;
*a3 = v7;
LODWORD(v9) = feof(v8);
if ( (_DWORD)v9 )
break;
v10 = (char *)*a1;
v9 = *a3;
if ( (*a1)[*a3 - 1] == 10 )
break;
v11 = *(_QWORD *)a2 + 1024LL;
*(_QWORD *)a2 = v11;
v12 = realloc(v10, v11);
func7((__int64)v12);
}
return v9;
}

original_code:
void read_line(char** buf, size_t* buf_s, size_t* len) {
    *len = 0;
    **buf = '\0';
    for (;;) {
        fgets((*buf)+(*len), (*buf_s)-(*len), stdin);
        if (ferror(stdin)) safe(((void *)0));
        *len = strlen(*buf);
        if (feof(stdin) || (*buf)[(*len)-1] == '\n') break;
        *buf_s += BLOCK_SIZE;
        safe(realloc(*buf, *buf_s));
    }
}

canonical_original_code:
void read_line(char **buf, unsigned long *buf_s, unsigned long *len) {
    *len = 0;
    **buf = '\0';
    for (;;) {
        fgets((*buf)+(*len), (*buf_s)-(*len), stdin);
        if (ferror(stdin)) safe(((void *)0));
        *len = strlen(*buf);
        if (feof(stdin) || (*buf)[(*len)-1] == '\n') break;
        *buf_s += BLOCK_SIZE;
        safe(realloc(*buf, *buf_s));
    }
}

variable_types:
buf
char **

variable_types:
len
unsigned long *


repo: jordan-dinwiddy/Somix

decompiled_code:
__int64 __fastcall alloc_zone(unsigned __int16 a1)
{
int v1; // ebx

v1 = alloc_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
if ( v1 < 0 )
panic("alloc_zone(%d): no free zones available", a1);
return (unsigned int)*((unsigned __int16 *)&sb + 4) + v1 - 1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
int v1; // ebx

v1 = alloc_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
if ( v1 < 0 )
panic("alloc_zone(%d): no free zones available", a1);
return (unsigned int)*((unsigned __int16 *)&sb + 4) + v1 - 1;
}

original_code:
zone_nr alloc_zone(zone_nr near_zone)
{
 int bit = near_zone - (sb.s_firstdatazone - 1);
 int b;
 zone_nr z;
 ;
 if((b = alloc_bit(sb.zmap, bit)) < 0)
  panic("alloc_zone(%d): no free zones available", near_zone);
 z = b + (sb.s_firstdatazone - 1);
 return z;
}

canonical_original_code:
unsigned short alloc_zone(unsigned short near_zone)
{
 int bit = near_zone - (sb.s_firstdatazone - 1);
 int b;
 unsigned short z;
 ;
 if((b = alloc_bit(sb.zmap, bit)) < 0)
  panic("alloc_zone(%d): no free zones available", near_zone);
 z = b + (sb.s_firstdatazone - 1);
 return z;
}

variable_types: bit int

variable_types: b int

variable_types:
z
unsigned short


decompiled_code:
__int64 __fastcall free_zone(unsigned __int16 a1)
{
__int64 result; // rax

if ( a1 )
result = free_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
__int64 result; // rax

if ( a1 )
result = free_bit(*((_QWORD *)&sb + 5), a1 - ((unsigned int)*((unsigned __int16 *)&sb + 4) - 1));
return result;
}

original_code:
void free_zone(zone_nr z)
{
 int bit = z - (sb.s_firstdatazone - 1);
 if(z == 0) return;
 ;
 free_bit(sb.zmap, bit);
}

canonical_original_code:
void free_zone(unsigned short z)
{
 int bit = z - (sb.s_firstdatazone - 1);
 if(z == 0) return;
 ;
 free_bit(sb.zmap, bit);
}


original_code:
int write_map(struct minix_inode *inode, int pos, zone_nr new_zone)
{
 zone_nr zone = pos / 1024;
 int excess, index;
 zone_nr *zp;
 zone_nr *dbl_zp;
 char new_ind = 0;
 char new_dbl = 0;
 struct minix_block *blk = ((void *)0);
 if(zone < 7) {
  inode->i_zone[zone] = new_zone;
  inode->i_dirty = 1;
  return 1;
 }
 excess = zone - 7;
 if(excess < (1024/2)) {
  zp = &inode->i_zone[7];
 }
 else {
  dbl_zp = &inode->i_zone[7 +1];
  if(*dbl_zp == 0) {
   *dbl_zp = alloc_zone(inode->i_zone[0]);
   if(*dbl_zp == 0)
    return -28;
   inode->i_dirty = 1;
   new_dbl = 1;
  }
  excess -= (1024/2);
  index = excess / (1024/2);
  excess = excess % (1024/2);
  if(index >= (1024/2)) return -27;
  blk = get_block(*dbl_zp, new_dbl == 1 ? 0 : 1);
  if(new_dbl == 1) zero_block(blk);
  zp = &((zone_nr *)blk->blk_data)[index];
 }
 if(*zp == 0) {
  *zp = alloc_zone(inode->i_zone[0]);
  if(blk != ((void *)0)) blk->blk_dirty = 1;
  else inode->i_dirty = 1;
  if(*zp == 0) {
   if(blk != ((void *)0)) put_block(blk, 2);
   return -28;
  }
  new_ind = 1;
 }
 if(blk != ((void *)0)) put_block(blk, 2);
 blk = get_block(*zp, new_ind == 1 ? 0 : 1);
 if(new_ind == 1) zero_block(blk);
 ;
 ((zone_nr *)blk->blk_data)[excess] = new_zone;
 blk->blk_dirty = 1;
 put_block(blk, 2);
 return 1;
}

canonical_original_code:
int write_map(struct minix_inode *inode, int pos, unsigned short new_zone)
{
 unsigned short zone = pos / 1024;
 int excess; int index;
 unsigned short *zp;
 unsigned short *dbl_zp;
 char new_ind = 0;
 char new_dbl = 0;
 struct minix_block *blk = ((void *)0);
 if(zone < 7) {
  inode->i_zone[zone] = new_zone;
  inode->i_dirty = 1;
  return 1;
 }
 excess = zone - 7;
 if(excess < (1024/2)) {
  zp = &inode->i_zone[7];
 }
 else {
  dbl_zp = &inode->i_zone[7 +1];
  if(*dbl_zp == 0) {
   *dbl_zp = alloc_zone(inode->i_zone[0]);
   if(*dbl_zp == 0)
    return -28;
   inode->i_dirty = 1;
   new_dbl = 1;
  }
  excess -= (1024/2);
  index = excess / (1024/2);
  excess = excess % (1024/2);
  if(index >= (1024/2)) return -27;
  blk = get_block(*dbl_zp, new_dbl == 1 ? 0 : 1);
  if(new_dbl == 1) zero_block(blk);
  zp = &((unsigned short *)blk->blk_data)[index];
 }
 if(*zp == 0) {
  *zp = alloc_zone(inode->i_zone[0]);
  if(blk != ((void *)0)) blk->blk_dirty = 1;
  else inode->i_dirty = 1;
  if(*zp == 0) {
   if(blk != ((void *)0)) put_block(blk, 2);
   return -28;
  }
  new_ind = 1;
 }
 if(blk != ((void *)0)) put_block(blk, 2);
 blk = get_block(*zp, new_ind == 1 ? 0 : 1);
 if(new_ind == 1) zero_block(blk);
 ;
 ((unsigned short *)blk->blk_data)[excess] = new_zone;
 blk->blk_dirty = 1;
 put_block(blk, 2);
 return 1;
}

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *

variable_types: pos int

variable_types:
zone
unsigned short

variable_types: excess int

variable_types: index int

variable_types:
zp
unsigned short *

variable_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *


decompiled_code:
__int64 __fastcall new_block(__int64 a1, int a2)
{
unsigned __int16 v3; // ax
unsigned int v4; // ebp
unsigned __int16 v5; // di
unsigned __int16 v6; // ax
__int64 v7; // rax
__int64 v8; // r8

v3 = read_map();
v4 = v3;
if ( !v3 )
{
v5 = *(_DWORD *)(a1 + 4) ? *(_WORD *)(a1 + 14) : sb[4];
v6 = alloc_zone(v5);
v4 = v6;
if ( (int)write_map(a1, a2, v6) < 0 )
return 0LL;
}
v7 = get_block(v4, 0LL);
zero_block_isra_0(v7);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
unsigned __int16 v3; // ax
unsigned int v4; // ebp
unsigned __int16 v5; // di
unsigned __int16 v6; // ax
__int64 v7; // rax
__int64 v8; // r8

v3 = read_map();
v4 = v3;
if ( !v3 )
{
v5 = *(_DWORD *)(a1 + 4) ? *(_WORD *)(a1 + 14) : sb[4];
v6 = func1(v5);
v4 = v6;
if ( (int)func3(a1, a2, v6) < 0 )
return 0LL;
}
v7 = get_block(v4, 0LL);
func0(v7);
return v8;
}

original_code:
struct minix_block *new_block(struct minix_inode *inode, int pos)
{
 zone_nr z, near_z;
 struct minix_block *retval;
 char new_zone = 0;
 ;
 if((z = read_map(inode, pos)) == 0) {
  if(inode->i_size == 0)
   near_z = sb.s_firstdatazone;
  else
   near_z = inode->i_zone[0];
  z = alloc_zone(near_z);
  new_zone = 1;
  if(write_map(inode, pos, z) < 0) {
   ;
   return ((void *)0);
  }
 }
 retval = get_block(z, 0);
 zero_block(retval);
 ;
 return retval;
}

canonical_original_code:
struct minix_block *new_block(struct minix_inode *inode, int pos)
{
 unsigned short z; unsigned short near_z;
 struct minix_block *retval;
 char new_zone = 0;
 ;
 if((z = read_map(inode, pos)) == 0) {
  if(inode->i_size == 0)
   near_z = sb.s_firstdatazone;
  else
   near_z = inode->i_zone[0];
  z = alloc_zone(near_z);
  new_zone = 1;
  if(write_map(inode, pos, z) < 0) {
   ;
   return ((void *)0);
  }
 }
 retval = get_block(z, 0);
 zero_block(retval);
 ;
 return retval;
}

return_type: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; } *

user_defined_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; }


decompiled_code:
__int64 __fastcall write_chunk(__int64 a1, int a2, unsigned int a3, char *a4)
{
__int64 v5; // rbx
int v6; // ebp
unsigned __int16 v7; // ax
__int64 v8; // rax
__int64 v9; // r8
_DWORD *v10; // rcx

v5 = a3;
v6 = a2 % 1024;
v7 = read_map();
if ( v7 )
{
v9 = get_block(v7, (_DWORD)v5 != 1024);
v10 = (_DWORD *)(*(_QWORD *)(v9 + 40) + v6);
if ( (unsigned int)v5 < 8 )
{
LABEL_4:
if ( (v5 & 4) != 0 )
{
*v10 = *(_DWORD *)a4;
*(_DWORD *)((char *)v10 + v5 - 4) = *(_DWORD *)&a4[v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)v10 = *a4;
if ( (v5 & 2) != 0 )
*(_WORD *)((char *)v10 + v5 - 2) = *(_WORD *)&a4[v5 - 2];
}
goto LABEL_8;
}
LABEL_10:
*(_QWORD *)v10 = *(_QWORD *)a4;
*(_QWORD *)((char *)v10 + (unsigned int)v5 - 8) = *(_QWORD *)&a4[(unsigned int)v5 - 8];
qmemcpy(
(void *)((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a4 - ((char *)v10 - ((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v5 + (_DWORD)v10 - (((_DWORD)v10 + 8) & 0xFFFFFFF8)) >> 3));
LABEL_8:
*(_BYTE *)(v9 + 32) = 1;
put_block(v9, 6LL);
return 1LL;
}
v8 = new_block(a1, a2);
v9 = v8;
if ( v8 )
{
v10 = (_DWORD *)(*(_QWORD *)(v8 + 40) + v6);
if ( (unsigned int)v5 < 8 )
goto LABEL_4;
goto LABEL_10;
}
return 4294967268LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, unsigned int a3, char *a4)
{
__int64 v5; // rbx
int v6; // ebp
unsigned __int16 v7; // ax
__int64 v8; // rax
__int64 v9; // r8
_DWORD *v10; // rcx

v5 = a3;
v6 = a2 % 1024;
v7 = read_map();
if ( v7 )
{
v9 = get_block(v7, (_DWORD)v5 != 1024);
v10 = (_DWORD *)(*(_QWORD *)(v9 + 40) + v6);
if ( (unsigned int)v5 < 8 )
{
LABEL_4:
if ( (v5 & 4) != 0 )
{
*v10 = *(_DWORD *)a4;
*(_DWORD *)((char *)v10 + v5 - 4) = *(_DWORD *)&a4[v5 - 4];
}
else if ( (_DWORD)v5 )
{
*(_BYTE *)v10 = *a4;
if ( (v5 & 2) != 0 )
*(_WORD *)((char *)v10 + v5 - 2) = *(_WORD *)&a4[v5 - 2];
}
goto LABEL_8;
}
LABEL_10:
*(_QWORD *)v10 = *(_QWORD *)a4;
*(_QWORD *)((char *)v10 + (unsigned int)v5 - 8) = *(_QWORD *)&a4[(unsigned int)v5 - 8];
qmemcpy(
(void *)((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a4 - ((char *)v10 - ((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v5 + (_DWORD)v10 - (((_DWORD)v10 + 8) & 0xFFFFFFF8)) >> 3));
LABEL_8:
*(_BYTE *)(v9 + 32) = 1;
put_block(v9, 6LL);
return 1LL;
}
v8 = func4(a1, a2);
v9 = v8;
if ( v8 )
{
v10 = (_DWORD *)(*(_QWORD *)(v8 + 40) + v6);
if ( (unsigned int)v5 < 8 )
goto LABEL_4;
goto LABEL_10;
}
return 4294967268LL;
}

original_code:
static int write_chunk(struct minix_inode *inode, int pos, int chunk,
 const char *buf)
{
 int zone = pos / 1024;
 int off = pos % 1024;
 int b_num = 0;
 struct minix_block *blk;
 ;
 b_num = read_map(inode, pos);
 if(b_num == 0) {
  ;
  blk = new_block(inode, pos);
  if(blk == ((void *)0)) {
   return -28;
  }
 }
 else {
  blk = get_block(b_num, chunk == 1024 ? 0 : 1);
 }
 memcpy(blk->blk_data+off, buf, chunk);
 blk->blk_dirty = 1;
 put_block(blk, 6);
 return 1;
}

canonical_original_code:
static int write_chunk(struct minix_inode *inode, int pos, int chunk, char *buf)
{
 int zone = pos / 1024;
 int off = pos % 1024;
 int b_num = 0;
 struct minix_block *blk;
 ;
 b_num = read_map(inode, pos);
 if(b_num == 0) {
  ;
  blk = new_block(inode, pos);
  if(blk == ((void *)0)) {
   return -28;
  }
 }
 else {
  blk = get_block(b_num, chunk == 1024 ? 0 : 1);
 }
 memcpy(blk->blk_data+off, buf, chunk);
 blk->blk_dirty = 1;
 put_block(blk, 6);
 return 1;
}

variable_types: chunk int

variable_types: zone int

variable_types: off int

user_defined_types: struct minix_block { int blk_nr; struct minix_block *blk_next; struct minix_block *blk_prev; struct minix_block *blk_hash; char blk_dirty; char blk_count; char *blk_data; }


original_code:
struct minix_inode *new_node(struct minix_inode *parent, const char *filename,
 mode_t mode)
{
 struct minix_inode *new_inode;
 new_inode = alloc_inode();
 new_inode->i_mode = (u16) mode;
 new_inode->i_uid = 500;
 new_inode->i_gid = 0;
 new_inode->i_time = time(((void *)0));
 new_inode->i_nlinks++;
 new_inode->i_dirty = 1;
 ;
 dir_add(parent, filename, new_inode->i_num);
 if(((((mode)) & 0170000) == (0040000))) {
  ;
  init_dir(new_inode, parent);
 }
 else {
  ;
 }
 return new_inode;
}

canonical_original_code:
struct minix_inode *new_node(struct minix_inode *parent, char *filename, unsigned int mode)
{
 struct minix_inode *new_inode;
 new_inode = alloc_inode();
 new_inode->i_mode = (unsigned short) mode;
 new_inode->i_uid = 500;
 new_inode->i_gid = 0;
 new_inode->i_time = time(((void *)0));
 new_inode->i_nlinks++;
 new_inode->i_dirty = 1;
 ;
 dir_add(parent, filename, new_inode->i_num);
 if(((((mode)) & 0170000) == (0040000))) {
  ;
  init_dir(new_inode, parent);
 }
 else {
  ;
 }
 return new_inode;
}

variable_types:
parent
struct minix_inode *

variable_types:
filename
char *

variable_types:
mode
unsigned int

return_type: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *


decompiled_code:
__int64 __fastcall write_buf(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v4; // er13
int v5; // er14
unsigned int v6; // er15
signed int v7; // ebx
__int64 result; // rax
int v9; // eax

if ( !a3 )
return 0xFFFFFFFFLL;
if ( (unsigned __int64)sb[3] - a3 < a4 )
return 4294967269LL;
v4 = a4;
if ( (int)a3 <= 0 )
{
v6 = 0;
LABEL_11:
if ( *(_DWORD *)(a1 + 4) < (unsigned int)v4 )
*(_DWORD *)(a1 + 4) = v4;
v9 = time(0LL);
*(_BYTE *)(a1 + 34) = 1;
*(_DWORD *)(a1 + 8) = v9;
result = v6;
}
else
{
v5 = a3;
v6 = 0;
while ( 1 )
{
v7 = 1024 - v4 % 1024;
if ( v7 > v5 )
v7 = v5;
result = write_chunk(a1, v4, v7, (char *)(a2 + (int)v6));
if ( (int)result < 0 )
break;
v5 -= v7;
v4 += v7;
v6 += v7;
if ( v5 <= 0 )
goto LABEL_11;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v4; // er13
int v5; // er14
unsigned int v6; // er15
signed int v7; // ebx
__int64 result; // rax
int v9; // eax

if ( !a3 )
return 0xFFFFFFFFLL;
if ( (unsigned __int64)sb[3] - a3 < a4 )
return 4294967269LL;
v4 = a4;
if ( (int)a3 <= 0 )
{
v6 = 0;
LABEL_11:
if ( *(_DWORD *)(a1 + 4) < (unsigned int)v4 )
*(_DWORD *)(a1 + 4) = v4;
v9 = time(0LL);
*(_BYTE *)(a1 + 34) = 1;
*(_DWORD *)(a1 + 8) = v9;
result = v6;
}
else
{
v5 = a3;
v6 = 0;
while ( 1 )
{
v7 = 1024 - v4 % 1024;
if ( v7 > v5 )
v7 = v5;
result = func5(a1, v4, v7, (char *)(a2 + (int)v6));
if ( (int)result < 0 )
break;
v5 -= v7;
v4 += v7;
v6 += v7;
if ( v5 <= 0 )
goto LABEL_11;
}
}
return result;
}

original_code:
int write_buf(struct minix_inode *inode, const char *buf, size_t size, off_t offset)
{
 int nbytes = size;
 int off;
 int chunk;
 int pos = offset;
 int sbytes = 0;
 int ret;
 ;
 if(size == 0) return -1;
 if(offset > sb.s_max_size - size)
  return -27;
 while(nbytes > 0) {
  off = pos % 1024;
  chunk = ((nbytes) < ((1024 - off)) ? (nbytes) : ((1024 - off)));
  if(chunk < 0)
   panic("write_buf(%d, buf, %d, %d): why is chunk < 0?",
    inode->i_num, (int) size, (int) offset);
  ret = write_chunk(inode, pos, chunk, buf + sbytes);
  if(ret < 0) {
   ;
   return ret;
  }
  nbytes -= chunk;
  pos += chunk;
  sbytes += chunk;
 }
 if(pos > inode->i_size) {
  inode->i_size = pos;
 }
 inode->i_time = time(((void *)0));
 inode->i_dirty = 1;
 return sbytes;
}

canonical_original_code:
int write_buf(struct minix_inode *inode, char *buf, unsigned long size, long offset)
{
 int nbytes = size;
 int off;
 int chunk;
 int pos = offset;
 int sbytes = 0;
 int ret;
 ;
 if(size == 0) return -1;
 if(offset > sb.s_max_size - size)
  return -27;
 while(nbytes > 0) {
  off = pos % 1024;
  chunk = ((nbytes) < ((1024 - off)) ? (nbytes) : ((1024 - off)));
  if(chunk < 0)
   panic("write_buf(%d, buf, %d, %d): why is chunk < 0?",
    inode->i_num, (int) size, (int) offset);
  ret = write_chunk(inode, pos, chunk, buf + sbytes);
  if(ret < 0) {
   ;
   return ret;
  }
  nbytes -= chunk;
  pos += chunk;
  sbytes += chunk;
 }
 if(pos > inode->i_size) {
  inode->i_size = pos;
 }
 inode->i_time = time(((void *)0));
 inode->i_dirty = 1;
 return sbytes;
}

variable_types:
buf
char *

variable_types:
size
unsigned long

variable_types: offset long

variable_types: nbytes int

variable_types: sbytes int

variable_types: ret int


original_code:
int dir_delete(struct minix_inode *p_dir, const char *file)
{
 struct minix_block *blk;
 zone_nr z;
 int c_pos = 0;
 int i;
 ;
 while((z = read_map(p_dir, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    ;
    *((inode_nr *)(blk->blk_data + i)) = 0;
    blk->blk_dirty = 1;
    p_dir->i_time = time(((void *)0));
    p_dir->i_dirty = 1;
    put_block(blk, 1);
    return 1;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 return 0;
}

canonical_original_code:
int dir_delete(struct minix_inode *p_dir, char *file)
{
 struct minix_block *blk;
 unsigned short z;
 int c_pos = 0;
 int i;
 ;
 while((z = read_map(p_dir, c_pos)) != 0) {
  blk = get_block(z, 1);
  for(i = 0; i < 1024; i+= 32) {
   if(strcmp((char *)(blk->blk_data + i + 2), file) == 0) {
    ;
    *((unsigned short *)(blk->blk_data + i)) = 0;
    blk->blk_dirty = 1;
    p_dir->i_time = time(((void *)0));
    p_dir->i_dirty = 1;
    put_block(blk, 1);
    return 1;
   }
  }
  put_block(blk, 1);
  c_pos += 1024;
 }
 return 0;
}

variable_types: struct minix_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; unsigned short i_num; char i_dirty; int i_count; } *

variable_types:
file
char *

variable_types:
z
unsigned short

variable_types: i int