decompiled_code:
int tdisk_finish()
{
__int64 *v0; // rbx
int result; // eax
void *v2; // rdi

v0 = &tdisks;
do
{
v2 = (void *)v0[1];
if ( v2 )
result = munmap(v2, *(unsigned int *)v0);
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)&tdisks + 160) );
return result;
}

canonical_decompiled_code:
int func4()
{
__int64 *v0; // rbx
int result; // eax
void *v2; // rdi

v0 = &tdisks;
do
{
v2 = (void *)v0[1];
if ( v2 )
result = munmap(v2, *(unsigned int *)v0);
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)&tdisks + 160) );
return result;
}

original_code:
void tdisk_finish() {
 int i;
 for (i = 0; i < 4; i++) {
  if (!tdisks[i].image)
   continue;
  munmap(tdisks[i].image, tdisks[i].length);
 }
}


repo: emestee/bk-emulator

decompiled_code:
__int64 __fastcall tdisk_read(__int16 a1, _WORD *a2)
{
int v2; // ebx
__int16 v3; // di
__int16 v5; // ax
__int16 v6; // dx
__int16 v7; // ax
unsigned int v8; // edx
char v9; // dl
__int64 v10; // rbp
char v11; // dl
unsigned __int8 v12; // dl
unsigned __int8 v13; // dl
int v14; // eax
_WORD *v15; // rdx

v2 = selected;
v3 = a1 + 512;
if ( v3 )
{
if ( v3 == 2 )
{
v14 = HIDWORD(tdisks[5 * selected + 3]);
if ( v14 == 4 )
{
*a2 = (BYTE4(tdisks[5 * selected + 4]) << 8) | LOBYTE(tdisks[5 * selected + 3]);
fprintf(stderr, "Trk/sec = %d/%d\n", LOBYTE(tdisks[5 * v2 + 3]), BYTE4(tdisks[5 * v2 + 4]));
}
else if ( v14 == 5 )
{
v15 = (_WORD *)tdisks[5 * selected + 2];
tdisks[5 * selected + 2] = (__int64)(v15 + 1);
*a2 = *v15;
}
else
{
*a2 = 0;
}
}
}
else if ( selected == -1 )
{
*a2 = -32640;
}
else
{
v5 = 5 * selected;
LOBYTE(v5) = LOBYTE(tdisks[5 * selected + 3]) == 0;
v6 = 5 * selected;
LOBYTE(v6) = LOBYTE(tdisks[5 * selected + 4]) != 0;
v7 = (v6 << 11) | (v5 << 9);
LOBYTE(v7) = v7 | 0x90;
*a2 = v7;
if ( BYTE2(tdisks[5 * v2 + 4]) )
{
v8 = HIDWORD(tdisks[5 * v2 + 3]);
if ( v8 == 4 )
{
BYTE4(tdisks[5 * v2 + 4]) = BYTE4(tdisks[5 * v2 + 4]) % 0x1Au + 1;
}
else if ( v8 > 4 )
{
if ( v8 == 5 )
{
v10 = 4LL * v2;
fprintf(stderr, "Reading track %d, sector %d\n", LOBYTE(tdisks[5 * v2 + 3]), BYTE4(tdisks[5 * v2 + 4]));
tdisks[v10 + 2 + v2] = tdisks[v10 + 1 + v2]
+ 2 * (1664LL * LOBYTE(tdisks[v10 + 3 + v2]) + ((BYTE4(tdisks[v10 + 4 + v2]) - 1) << 6));
}
}
else if ( v8 == 2 )
{
v11 = tdisks[5 * v2 + 3];
if ( v11 == 76 )
{
*a2 = v7 | 0x8000;
}
else
{
v12 = v11 + 1;
LOBYTE(tdisks[5 * v2 + 3]) = v12;
fprintf(stderr, "trk = %d\n", v12);
}
}
else if ( v8 == 3 )
{
v9 = tdisks[5 * v2 + 3];
if ( v9 )
{
v13 = v9 - 1;
LOBYTE(tdisks[5 * v2 + 3]) = v13;
fprintf(stderr, "trk = %d\n", v13);
}
else
{
*a2 = v7 | 0x8000;
}
}
BYTE2(tdisks[5 * v2 + 4]) = 0;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 a1, _WORD *a2)
{
int v2; // ebx
__int16 v3; // di
__int16 v5; // ax
__int16 v6; // dx
__int16 v7; // ax
unsigned int v8; // edx
char v9; // dl
__int64 v10; // rbp
char v11; // dl
unsigned __int8 v12; // dl
unsigned __int8 v13; // dl
int v14; // eax
_WORD *v15; // rdx

v2 = selected;
v3 = a1 + 512;
if ( v3 )
{
if ( v3 == 2 )
{
v14 = HIDWORD(tdisks[5 * selected + 3]);
if ( v14 == 4 )
{
*a2 = (BYTE4(tdisks[5 * selected + 4]) << 8) | LOBYTE(tdisks[5 * selected + 3]);
fprintf(stderr, "Trk/sec = %d/%d\n", LOBYTE(tdisks[5 * v2 + 3]), BYTE4(tdisks[5 * v2 + 4]));
}
else if ( v14 == 5 )
{
v15 = (_WORD *)tdisks[5 * selected + 2];
tdisks[5 * selected + 2] = (__int64)(v15 + 1);
*a2 = *v15;
}
else
{
*a2 = 0;
}
}
}
else if ( selected == -1 )
{
*a2 = -32640;
}
else
{
v5 = 5 * selected;
LOBYTE(v5) = LOBYTE(tdisks[5 * selected + 3]) == 0;
v6 = 5 * selected;
LOBYTE(v6) = LOBYTE(tdisks[5 * selected + 4]) != 0;
v7 = (v6 << 11) | (v5 << 9);
LOBYTE(v7) = v7 | 0x90;
*a2 = v7;
if ( BYTE2(tdisks[5 * v2 + 4]) )
{
v8 = HIDWORD(tdisks[5 * v2 + 3]);
if ( v8 == 4 )
{
BYTE4(tdisks[5 * v2 + 4]) = BYTE4(tdisks[5 * v2 + 4]) % 0x1Au + 1;
}
else if ( v8 > 4 )
{
if ( v8 == 5 )
{
v10 = 4LL * v2;
fprintf(stderr, "Reading track %d, sector %d\n", LOBYTE(tdisks[5 * v2 + 3]), BYTE4(tdisks[5 * v2 + 4]));
tdisks[v10 + 2 + v2] = tdisks[v10 + 1 + v2]
+ 2 * (1664LL * LOBYTE(tdisks[v10 + 3 + v2]) + ((BYTE4(tdisks[v10 + 4 + v2]) - 1) << 6));
}
}
else if ( v8 == 2 )
{
v11 = tdisks[5 * v2 + 3];
if ( v11 == 76 )
{
*a2 = v7 | 0x8000;
}
else
{
v12 = v11 + 1;
LOBYTE(tdisks[5 * v2 + 3]) = v12;
fprintf(stderr, "trk = %d\n", v12);
}
}
else if ( v8 == 3 )
{
v9 = tdisks[5 * v2 + 3];
if ( v9 )
{
v13 = v9 - 1;
LOBYTE(tdisks[5 * v2 + 3]) = v13;
fprintf(stderr, "trk = %d\n", v13);
}
else
{
*a2 = v7 | 0x8000;
}
}
BYTE2(tdisks[5 * v2 + 4]) = 0;
}
}
return 0LL;
}

original_code:
int
tdisk_read(c_addr addr, d_word *word) {
 d_word offset = addr - 0177000;
 tdisk_t * pdt = &tdisks[selected];
 int index;
 switch(offset) {
 case 0:
  if (selected == -1) {
  *word = errF | doneF;
  break;
  }
  *word = (pdt->track ? 0 : track0F) | (pdt->ro ? wrprF : 0) | headF | doneF;
  if (!pdt->inprogress) {
   return 0;
  } else switch (pdt->cmd) {
   case nopD:
   case rtcD:
   case writeD:
   case delD:
    break;
   case stepinD:
    if (pdt->track == 76)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track++;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case stepoutD:
    if (pdt->track == 0)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track--;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case readtsD:
    if (pdt->inprogress) {
     pdt->cursec %= 26;
     pdt->cursec++;
    }
    break;
   case readD:
    fprintf(stderr, "Reading track %d, sector %d\n",
     pdt->track, pdt->cursec);
    pdt->ptr = pdt->image +
     pdt->track*26*64 +
     (pdt->cursec-1)*64;
    break;
  }
  pdt->inprogress = 0;
  break;
 case 2:
  switch (pdt->cmd) {
  case readtsD:
   *word = pdt->cursec << 8 | pdt->track;
   fprintf(stderr, "Trk/sec = %d/%d\n", pdt->track, pdt->cursec);
   break;
  case readD:
   *word = *pdt->ptr++;
   break;
  default:
   *word = 0;
  }
 break;
 }
 return 0;
}

canonical_original_code:
int tdisk_read(unsigned long addr, unsigned short *word) {
 unsigned short offset = addr - 0177000;
 struct tdisk_t *pdt = &tdisks[selected];
 int index;
 switch(offset) {
 case 0:
  if (selected == -1) {
  *word = errF | doneF;
  break;
  }
  *word = (pdt->track ? 0 : track0F) | (pdt->ro ? wrprF : 0) | headF | doneF;
  if (!pdt->inprogress) {
   return 0;
  } else switch (pdt->cmd) {
   case nopD:
   case rtcD:
   case writeD:
   case delD:
    break;
   case stepinD:
    if (pdt->track == 76)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track++;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case stepoutD:
    if (pdt->track == 0)
     *word |= errF;
    else if (pdt->inprogress) {
     pdt->track--;
     fprintf(stderr, "trk = %d\n", pdt->track);
    }
    break;
   case readtsD:
    if (pdt->inprogress) {
     pdt->cursec %= 26;
     pdt->cursec++;
    }
    break;
   case readD:
    fprintf(stderr, "Reading track %d, sector %d\n",
     pdt->track, pdt->cursec);
    pdt->ptr = pdt->image +
     pdt->track*26*64 +
     (pdt->cursec-1)*64;
    break;
  }
  pdt->inprogress = 0;
  break;
 case 2:
  switch (pdt->cmd) {
  case readtsD:
   *word = pdt->cursec << 8 | pdt->track;
   fprintf(stderr, "Trk/sec = %d/%d\n", pdt->track, pdt->cursec);
   break;
  case readD:
   *word = *pdt->ptr++;
   break;
  default:
   *word = 0;
  }
 break;
 }
 return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short *

variable_types:
offset
unsigned short

variable_types: struct tdisk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 } cmd; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char cursec; } *

variable_types: index int

return_type: int

user_defined_types: struct tdisk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; enum disk_cmd cmd; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char cursec; }


decompiled_code:
__int64 __fastcall tdisk_write(__int16 a1, unsigned __int16 a2)
{
__int16 v2; // di
__int64 result; // rax
char *v4; // rax
int v5; // edx
__int64 v6; // rbp
__int64 v7; // r13
int v8; // ecx

v2 = a1 + 512;
if ( v2 )
{
result = 0LL;
if ( v2 == 2 )
{
v4 = dcgettext(0LL, "Writing disk data reg, data %06o\n", 5);
fprintf(stderr, v4, a2);
result = 0LL;
}
}
else
{
v5 = unit(a2);
selected = v5;
result = 0LL;
if ( v5 >= 0 )
{
result = 2LL;
if ( !BYTE2(tdisks[5 * v5 + 4]) )
{
v6 = v5;
v7 = 4LL * v5;
BYTE2(tdisks[5 * v5 + 4]) = a2 & 1;
v8 = cmd(a2);
HIDWORD(tdisks[v7 + 3 + v6]) = v8;
result = 0LL;
if ( (a2 & 0x41) == 65 )
{
if ( v8 )
{
if ( v8 == 1 )
{
ev_register(1LL, &service, (unsigned int)(TICK_RATE / 50), 168LL);
}
else
{
fwrite("Interrupt requested\n", 1uLL, 0x14uLL, stderr);
ev_register(1LL, &service, (unsigned int)(TICK_RATE / 1000), 168LL);
}
result = 0LL;
}
else
{
ev_register(1LL, &service, (unsigned int)(4 * TICK_RATE), 168LL);
result = 0LL;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1, unsigned __int16 a2)
{
__int16 v2; // di
__int64 result; // rax
char *v4; // rax
int v5; // edx
__int64 v6; // rbp
__int64 v7; // r13
int v8; // ecx

v2 = a1 + 512;
if ( v2 )
{
result = 0LL;
if ( v2 == 2 )
{
v4 = dcgettext(0LL, "Writing disk data reg, data %06o\n", 5);
fprintf(stderr, v4, a2);
result = 0LL;
}
}
else
{
v5 = func0(a2);
selected = v5;
result = 0LL;
if ( v5 >= 0 )
{
result = 2LL;
if ( !BYTE2(tdisks[5 * v5 + 4]) )
{
v6 = v5;
v7 = 4LL * v5;
BYTE2(tdisks[5 * v5 + 4]) = a2 & 1;
v8 = func1(a2);
HIDWORD(tdisks[v7 + 3 + v6]) = v8;
result = 0LL;
if ( (a2 & 0x41) == 65 )
{
if ( v8 )
{
if ( v8 == 1 )
{
ev_register(1LL, &service, (unsigned int)(TICK_RATE / 50), 168LL);
}
else
{
fwrite("Interrupt requested\n", 1uLL, 0x14uLL, stderr);
ev_register(1LL, &service, (unsigned int)(TICK_RATE / 1000), 168LL);
}
result = 0LL;
}
else
{
ev_register(1LL, &service, (unsigned int)(4 * TICK_RATE), 168LL);
result = 0LL;
}
}
}
}
}
return result;
}

original_code:
int
tdisk_write(c_addr addr, d_word word) {
 d_word offset = addr - 0177000;
 tdisk_t * pdt;
 switch (offset) {
 case 0:
  if (word) {
  }
  selected = unit(word);
  if (selected >= 0) {
   pdt = &tdisks[selected];
   if (pdt->inprogress)
    return 2;
   pdt->inprogress = word & enF;
   pdt->cmd = cmd(word);
   if (pdt->inprogress && word & intrF) switch (pdt->cmd) {
    case nopD:
     ev_register(1, service, TICK_RATE*100/25, 0250);
     break;
    case rtcD:
     ev_register(1, service, TICK_RATE/50, 0250);
     break;
    default:
     fprintf(stderr, "Interrupt requested\n");
     ev_register(1, service, TICK_RATE/1000, 0250);
   }
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing disk data reg, data %06o\n"), word);
  break;
 }
 return 0;
}

canonical_original_code:
int tdisk_write(unsigned long addr, unsigned short word) {
 unsigned short offset = addr - 0177000;
 struct tdisk_t *pdt;
 switch (offset) {
 case 0:
  if (word) {
  }
  selected = unit(word);
  if (selected >= 0) {
   pdt = &tdisks[selected];
   if (pdt->inprogress)
    return 2;
   pdt->inprogress = word & enF;
   pdt->cmd = cmd(word);
   if (pdt->inprogress && word & intrF) switch (pdt->cmd) {
    case nopD:
     ev_register(1, service, TICK_RATE*100/25, 0250);
     break;
    case rtcD:
     ev_register(1, service, TICK_RATE/50, 0250);
     break;
    default:
     fprintf(stderr, "Interrupt requested\n");
     ev_register(1, service, TICK_RATE/1000, 0250);
   }
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing disk data reg, data %06o\n"), word);
  break;
 }
 return 0;
}

variable_types:
word
unsigned short

user_defined_types: enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 }


decompiled_code:
__int64 __fastcall tdisk_bwrite(__int64 a1, unsigned __int8 a2)
{
return disk_write(a1 & 0xFFFFFFFFFFFFFFFELL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 a2)
{
return disk_write(a1 & 0xFFFFFFFFFFFFFFFELL, a2);
}

original_code:
int
tdisk_bwrite(c_addr addr, d_byte byte) {
 return disk_write(addr & ~1, byte);
}

canonical_original_code:
int tdisk_bwrite(unsigned long addr, unsigned char byte) {
 return disk_write(addr & ~1, byte);
}

variable_types:
byte
unsigned char


printop

decompiled_code:
size_t __fastcall printop(unsigned int a1, _QWORD *a2, char **a3)
{
int v4; // er12
char *v5; // rax
char *v6; // rax
char *v7; // rbp
size_t result; // rax
char *v9; // rax
__int64 v10; // rdx
__int16 v11[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v4 = a1 & 7;
if ( a1 <= 0x3F )
{
switch ( a1 >> 3 )
{
case 0u:
sprintf(*a3, regnam[a1 & 7]);
break;
case 1u:
sprintf(*a3, "(%s)", regnam[a1 & 7]);
break;
case 2u:
goto LABEL_6;
case 3u:
v5 = (*a3)++;
*v5 = 64;
LABEL_6:
if ( v4 == 7 )
{
lc_word(*a2, v11);
*a2 = (unsigned __int16)(*a2 + 2);
sprintf(*a3, "#%o", (unsigned __int16)v11[0]);
}
else
{
sprintf(*a3, "(%s)+", regnam[a1 & 7]);
}
break;
case 4u:
goto LABEL_10;
case 5u:
v6 = (*a3)++;
*v6 = 64;
LABEL_10:
sprintf(*a3, "-(%s)", regnam[a1 & 7]);
break;
case 6u:
goto LABEL_13;
case 7u:
v9 = (*a3)++;
*v9 = 64;
LABEL_13:
lc_word(*a2, v11);
v10 = (unsigned __int16)(*a2 + 2);
*a2 = v10;
if ( v4 == 7 )
sprintf(*a3, "%o", (unsigned __int16)(v11[0] + v10));
else
sprintf(*a3, "%o(%s)", (unsigned __int16)v11[0], regnam[a1 & 7]);
break;
}
}
v7 = *a3;
result = strlen(*a3);
*a3 = &v7[result];
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(unsigned int a1, _QWORD *a2, char **a3)
{
int v4; // er12
char *v5; // rax
char *v6; // rax
char *v7; // rbp
size_t result; // rax
char *v9; // rax
__int64 v10; // rdx
__int16 v11[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v4 = a1 & 7;
if ( a1 <= 0x3F )
{
switch ( a1 >> 3 )
{
case 0u:
sprintf(*a3, regnam[a1 & 7]);
break;
case 1u:
sprintf(*a3, "(%s)", regnam[a1 & 7]);
break;
case 2u:
goto LABEL_6;
case 3u:
v5 = (*a3)++;
*v5 = 64;
LABEL_6:
if ( v4 == 7 )
{
lc_word(*a2, v11);
*a2 = (unsigned __int16)(*a2 + 2);
sprintf(*a3, "#%o", (unsigned __int16)v11[0]);
}
else
{
sprintf(*a3, "(%s)+", regnam[a1 & 7]);
}
break;
case 4u:
goto LABEL_10;
case 5u:
v6 = (*a3)++;
*v6 = 64;
LABEL_10:
sprintf(*a3, "-(%s)", regnam[a1 & 7]);
break;
case 6u:
goto LABEL_13;
case 7u:
v9 = (*a3)++;
*v9 = 64;
LABEL_13:
lc_word(*a2, v11);
v10 = (unsigned __int16)(*a2 + 2);
*a2 = v10;
if ( v4 == 7 )
sprintf(*a3, "%o", (unsigned __int16)(v11[0] + v10));
else
sprintf(*a3, "%o(%s)", (unsigned __int16)v11[0], regnam[a1 & 7]);
break;
}
}
v7 = *a3;
result = strlen(*a3);
*a3 = &v7[result];
return result;
}

original_code:
void printop(unsigned rm, c_addr * a, char **destp) {
 d_word word;
 unsigned r = rm & 7;
 unsigned mode = rm >> 3;
 switch (mode) {
 case 0:
  sprintf(*destp, regnam[r]);
  break;
 case 1:
  sprintf(*destp, "(%s)", regnam[r]);
  break;
 case 3:
  *(*destp)++ = '@';
 case 2:
  if (r == 7) {
   lc_word(*a, &word);
   *a += 2;
   *a &= 0177777;
   sprintf(*destp, "#%o", word);
  } else {
   sprintf(*destp, "(%s)+", regnam[r]);
  }
  break;
 case 5:
  *(*destp)++ = '@';
 case 4:
  sprintf(*destp, "-(%s)", regnam[r]);
  break;
 case 7:
  *(*destp)++ = '@';
 case 6:
  lc_word(*a, &word);
  *a += 2;
  *a &= 0177777;
  if (r == 7) {
   sprintf(*destp, "%o", (*a + word) & 0177777);
  } else {
   sprintf(*destp, "%o(%s)", word, regnam[r]);
  }
 }
 *destp += strlen(*destp);
}

canonical_original_code:
void printop(unsigned int rm, unsigned long *a, char **destp) {
 unsigned short word;
 unsigned int r = rm & 7;
 unsigned int mode = rm >> 3;
 switch (mode) {
 case 0:
  sprintf(*destp, regnam[r]);
  break;
 case 1:
  sprintf(*destp, "(%s)", regnam[r]);
  break;
 case 3:
  *(*destp)++ = '@';
 case 2:
  if (r == 7) {
   lc_word(*a, &word);
   *a += 2;
   *a &= 0177777;
   sprintf(*destp, "#%o", word);
  } else {
   sprintf(*destp, "(%s)+", regnam[r]);
  }
  break;
 case 5:
  *(*destp)++ = '@';
 case 4:
  sprintf(*destp, "-(%s)", regnam[r]);
  break;
 case 7:
  *(*destp)++ = '@';
 case 6:
  lc_word(*a, &word);
  *a += 2;
  *a &= 0177777;
  if (r == 7) {
   sprintf(*destp, "%o", (*a + word) & 0177777);
  } else {
   sprintf(*destp, "%o(%s)", word, regnam[r]);
  }
 }
 *destp += strlen(*destp);
}

variable_types:
rm
unsigned int

variable_types:
a
unsigned long *

variable_types:
destp
char **

variable_types:
r
unsigned int

variable_types:
mode
unsigned int

return_type: void


disas

original_code:
c_addr disas (c_addr a, char * dest) {
 d_word inst = 0;
 lc_word(a, &inst);
 a += 2;
 a &= 0177777;
 char * code = base[inst >> 12];
 char soffset;
 unsigned char uoffset;
 switch (*code) {
  case '0': code = decode0[(inst >> 6) & 077]; break;
  case '7': code = decode7[(inst >> 9) & 7]; break;
  case '8': code = decode8[(inst >> 6) & 077]; break;
 }
 switch (*code) {
  case '0': code = decode000[inst & 077]; break;
  case '2': code = decode002[inst & 077]; break;
  case '5':
   if (inst & 0000740)
    code = "N";
   else
    code = decode750[(inst >> 3) & 3];
   break;
 }
 sprintf(dest, "%06o: %s ", a - 2, code + !!((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short int) _ISupper));
 if (((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short int) _ISupper)) {
  dest += strlen(dest);
  switch (*code) {
  case 'B':
   soffset = inst;
   sprintf(dest, "%06o", a + 2 * soffset);
   break;
  case 'F':
   sprintf(dest, "(%s)", regnam[inst & 7]);
   break;
  case 'S':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   uoffset = inst & 077;
   dest += strlen(dest);
   sprintf(dest, "%06o", a - 2 * uoffset);
   break;
  case 'G':
   printop((inst >> 6) & 077, &a, &dest);
   sprintf(dest, ", ");
   dest += 2;
   printop(inst & 077, &a, &dest);
   break;
  case 'R':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   dest += strlen(dest);
  case 'O':
   printop(inst & 077, &a, &dest);
   break;
  case 'T':
   sprintf(dest, "%o", inst & 0377);
   break;
  case 'U':
   sprintf(dest, "%o", inst & 077);
   break;
  case 'N':
   sprintf(dest, "%06o", inst);
   break;
  default: sprintf(dest, gettext ("ERROR code %c"), *code);
 }}
 return a;
}

canonical_original_code:
unsigned long disas(unsigned long a, char *dest) {
 unsigned short inst = 0;
 lc_word(a, &inst);
 a += 2;
 a &= 0177777;
 char *code = base[inst >> 12];
 char soffset;
 unsigned char uoffset;
 switch (*code) {
  case '0': code = decode0[(inst >> 6) & 077]; break;
  case '7': code = decode7[(inst >> 9) & 7]; break;
  case '8': code = decode8[(inst >> 6) & 077]; break;
 }
 switch (*code) {
  case '0': code = decode000[inst & 077]; break;
  case '2': code = decode002[inst & 077]; break;
  case '5':
   if (inst & 0000740)
    code = "N";
   else
    code = decode750[(inst >> 3) & 3];
   break;
 }
 sprintf(dest, "%06o: %s ", a - 2, code + !!((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short) _ISupper));
 if (((*__ctype_b_loc ())[(int) ((*code))] & (unsigned short) _ISupper)) {
  dest += strlen(dest);
  switch (*code) {
  case 'B':
   soffset = inst;
   sprintf(dest, "%06o", a + 2 * soffset);
   break;
  case 'F':
   sprintf(dest, "(%s)", regnam[inst & 7]);
   break;
  case 'S':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   uoffset = inst & 077;
   dest += strlen(dest);
   sprintf(dest, "%06o", a - 2 * uoffset);
   break;
  case 'G':
   printop((inst >> 6) & 077, &a, &dest);
   sprintf(dest, ", ");
   dest += 2;
   printop(inst & 077, &a, &dest);
   break;
  case 'R':
   sprintf(dest, "%s, ", regnam[(inst >> 6) & 7]);
   dest += strlen(dest);
  case 'O':
   printop(inst & 077, &a, &dest);
   break;
  case 'T':
   sprintf(dest, "%o", inst & 0377);
   break;
  case 'U':
   sprintf(dest, "%o", inst & 077);
   break;
  case 'N':
   sprintf(dest, "%06o", inst);
   break;
  default: sprintf(dest, gettext ("ERROR code %c"), *code);
 }}
 return a;
}

variable_types:
a
unsigned long

variable_types:
dest
char *

variable_types:
inst
unsigned short

variable_types:
code
char *

variable_types: soffset char

variable_types:
uoffset
unsigned char

return_type: unsigned long


state

original_code:
char * state(pdp_regs * p) {
 static char buf[80];
 sprintf(buf, "%04x %04x %04x %04x %04x %04x %04x %04x",
  p->regs[0], p->regs[1], p->regs[2], p->regs[3],
  p->regs[4], p->regs[5], p->regs[6], p->psw);
 return buf;
}

canonical_original_code:
char *state(struct _pdp_regs *p) {
 static char buf[80];
 sprintf(buf, "%04x %04x %04x %04x %04x %04x %04x %04x",
  p->regs[0], p->regs[1], p->regs[2], p->regs[3],
  p->regs[4], p->regs[5], p->regs[6], p->psw);
 return buf;
}

variable_types: struct _pdp_regs { unsigned short regs[8]; unsigned char psw; unsigned short ir; unsigned short ea_addr; unsigned long total; unsigned int look_time; } *

variable_types:
buf
char [80]

return_type: char *


halt

decompiled_code:
__int64 halt()
{
io_stop_happened = 4;
return 5LL;
}

canonical_decompiled_code:
__int64 func2()
{
io_stop_happened = 4;
return 5LL;
}

original_code:
int halt(pdp_regs *p) {
 io_stop_happened = 4;
 return 5;
}

canonical_original_code:
int halt(struct _pdp_regs *p) {
 io_stop_happened = 4;
 return 5;
}

user_defined_types: struct _pdp_regs { unsigned short regs[8]; unsigned char psw; unsigned short ir; unsigned short ea_addr; unsigned long total; unsigned int look_time; }


iot

decompiled_code:
__int64 iot()
{
return 11LL;
}

canonical_decompiled_code:
__int64 func3()
{
return 11LL;
}

original_code: int iot() { return 11; }

canonical_original_code: int iot() { return 11; }


emt

decompiled_code:
__int64 emt()
{
return 9LL;
}

canonical_decompiled_code:
__int64 func4()
{
return 9LL;
}

original_code: int emt() { return 9; }

canonical_original_code: int emt() { return 9; }


trap

decompiled_code:
__int64 trap()
{
return 8LL;
}

canonical_decompiled_code:
__int64 func5()
{
return 8LL;
}

original_code: int trap() { return 8; }

canonical_original_code: int trap() { return 8; }


bpt

decompiled_code:
__int64 bpt()
{
return 10LL;
}

canonical_decompiled_code:
__int64 func6()
{
return 10LL;
}

original_code: int bpt() { return 10; }

canonical_original_code: int bpt() { return 10; }


fis

decompiled_code:
__int64 fis()
{
return 4LL;
}

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

original_code: int fis() { return 4; }

canonical_original_code: int fis() { return 4; }


illegal

decompiled_code:
__int64 illegal()
{
return 4LL;
}

canonical_decompiled_code:
__int64 func8()
{
return 4LL;
}

original_code: int illegal() { return 4; }

canonical_original_code: int illegal() { return 4; }


decompiled_code:
__int64 __fastcall force_read(unsigned __int64 a1, _WORD *a2)
{
*a2 = *(_WORD *)((char *)*(&pagemap + (a1 >> 14)) + (a1 & 0x3FFE));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int64 a1, _WORD *a2)
{
*a2 = *(_WORD *)((char *)*(&pagemap + (a1 >> 14)) + (a1 & 0x3FFE));
return 0LL;
}

original_code:
int
force_read(c_addr addr, d_word *word )
{
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
}

canonical_original_code:
int force_read(unsigned long addr, unsigned short *word)
{
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short *


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

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

original_code:
int q_null()
{
 return 0;
}

canonical_original_code:
int q_null()
{
 return 0;
}


decompiled_code:
__int64 __fastcall port_read(__int64 a1, _WORD *a2)
{
*a2 = 0;
fprintf(stderr, "Reading port %06o\n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _WORD *a2)
{
*a2 = 0;
fprintf(stderr, "Reading port %06o\n", a1);
return 0LL;
}

original_code:
int port_read(c_addr a, d_word *d) {
 *d = 0;
 fprintf(stderr, "Reading port %06o\n", a);
 return 0;
}

canonical_original_code:
int port_read(unsigned long a, unsigned short *d) {
 *d = 0;
 fprintf(stderr, "Reading port %06o\n", a);
 return 0;
}

variable_types:
d
unsigned short *


decompiled_code:
__int64 __fastcall port_write(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %06o to port %06o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %06o to port %06o\n", a2, a1);
return 0LL;
}

original_code:
int port_write(c_addr a, d_word d) {
 fprintf(stderr, "Writing %06o to port %06o\n", d, a);
 return 0;
}

canonical_original_code:
int port_write(unsigned long a, unsigned short d) {
 fprintf(stderr, "Writing %06o to port %06o\n", d, a);
 return 0;
}

variable_types:
d
unsigned short


decompiled_code:
__int64 __fastcall port_bwrite(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %03o to port %06o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %03o to port %06o\n", a2, a1);
return 0LL;
}

original_code:
int port_bwrite(c_addr a, d_byte d) {
 fprintf(stderr, "Writing %03o to port %06o\n", d, a);
 return 0;
}

canonical_original_code:
int port_bwrite(unsigned long a, unsigned char d) {
 fprintf(stderr, "Writing %03o to port %06o\n", d, a);
 return 0;
}

variable_types:
d
unsigned char


decompiled_code:
__int64 __fastcall secret_write(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int16 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

original_code:
int secret_write(c_addr a, d_word d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}

canonical_original_code:
int secret_write(unsigned long a, unsigned short d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}


decompiled_code:
__int64 __fastcall secret_bwrite(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 a2)
{
fprintf(stderr, "Writing %o to %o\n", a2, a1);
return 0LL;
}

original_code:
int secret_bwrite(c_addr a, d_byte d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}

canonical_original_code:
int secret_bwrite(unsigned long a, unsigned char d) {
 fprintf(stderr, "Writing %o to %o\n", d, a);
 return 0;
}


decompiled_code:
__int64 __fastcall terak_read(__int64 a1, _WORD *a2)
{
__int64 result; // rax

if ( a1 == 63102 )
{
fwrite("Reading serial num\n", 1uLL, 0x13uLL, stderr);
goto LABEL_5;
}
result = 2LL;
if ( (unsigned __int64)(a1 - 62976) <= 0x7F )
{
LABEL_5:
*a2 = *(_WORD *)((char *)*(&pagemap + 3) + (a1 & 0x3FFE));
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, _WORD *a2)
{
__int64 result; // rax

if ( a1 == 63102 )
{
fwrite("Reading serial num\n", 1uLL, 0x13uLL, stderr);
goto LABEL_5;
}
result = 2LL;
if ( (unsigned __int64)(a1 - 62976) <= 0x7F )
{
LABEL_5:
*a2 = *(_WORD *)((char *)*(&pagemap + 3) + (a1 & 0x3FFE));
result = 0LL;
}
return result;
}

original_code:
int
terak_read(c_addr addr, d_word *word )
{
 if (addr == 0173176) fprintf(stderr, "Reading serial num\n");
 if (addr >= 0173000 && addr < 0173200) {
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
 }
 return 2;
}

canonical_original_code:
int terak_read(unsigned long addr, unsigned short *word)
{
 if (addr == 0173176) fprintf(stderr, "Reading serial num\n");
 if (addr >= 0173000 && addr < 0173200) {
        *word = pagemap[(addr)>>14][((addr) & 037777) >> 1];
        return 0;
 }
 return 2;
}


decompiled_code:
__int64 __fastcall q_err(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

original_code:
int q_err(c_addr x, d_word y)
{
 fprintf(stderr, gettext ("Writing to ROM addr %06o:"), x);
 return 2;
}

canonical_original_code:
int q_err(unsigned long x, unsigned short y)
{
 fprintf(stderr, gettext ("Writing to ROM addr %06o:"), x);
 return 2;
}

variable_types:
x
unsigned long

variable_types:
y
unsigned short


decompiled_code:
__int64 __fastcall q_errb(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing byte to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
char *v1; // rax

v1 = dcgettext(0LL, "Writing byte to ROM addr %06o:", 5);
fprintf(stderr, v1, a1);
return 2LL;
}

original_code:
int q_errb(c_addr x, d_byte y)
{
 fprintf(stderr, gettext ("Writing byte to ROM addr %06o:"), x);
 return 2;
}

canonical_original_code:
int q_errb(unsigned long x, unsigned char y)
{
 fprintf(stderr, gettext ("Writing byte to ROM addr %06o:"), x);
 return 2;
}

variable_types:
y
unsigned char


decompiled_code:
__m128i *plug_printer()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_printer);
result[1] = _mm_load_si128((const __m128i *)&q_printer + 1);
result[2] = _mm_load_si128((const __m128i *)&q_printer + 2);
return result;
}

canonical_decompiled_code:
__m128i *func13()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_printer);
result[1] = _mm_load_si128((const __m128i *)&q_printer + 1);
result[2] = _mm_load_si128((const __m128i *)&q_printer + 2);
return result;
}

original_code: void plug_printer() { qmap[0] = q_printer; }


decompiled_code:
__m128i *plug_mouse()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_mouse);
result[1] = _mm_load_si128((const __m128i *)&q_mouse + 1);
result[2] = _mm_load_si128((const __m128i *)&q_mouse + 2);
return result;
}

canonical_decompiled_code:
__m128i *func14()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_mouse);
result[1] = _mm_load_si128((const __m128i *)&q_mouse + 1);
result[2] = _mm_load_si128((const __m128i *)&q_mouse + 2);
return result;
}

original_code: void plug_mouse() { qmap[0] = q_mouse; }


decompiled_code:
__m128i *plug_covox()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_covox);
result[1] = _mm_load_si128((const __m128i *)&q_covox + 1);
result[2] = _mm_load_si128((const __m128i *)&q_covox + 2);
return result;
}

canonical_decompiled_code:
__m128i *func15()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_covox);
result[1] = _mm_load_si128((const __m128i *)&q_covox + 1);
result[2] = _mm_load_si128((const __m128i *)&q_covox + 2);
return result;
}

original_code: void plug_covox() { qmap[0] = q_covox; }


decompiled_code:
__m128i *plug_synth()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_synth);
result[1] = _mm_load_si128((const __m128i *)&q_synth + 1);
result[2] = _mm_load_si128((const __m128i *)&q_synth + 2);
return result;
}

canonical_decompiled_code:
__m128i *func16()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_synth);
result[1] = _mm_load_si128((const __m128i *)&q_synth + 1);
result[2] = _mm_load_si128((const __m128i *)&q_synth + 2);
return result;
}

original_code: void plug_synth() { qmap[0] = q_synth; }


decompiled_code:
__m128i *plug_bkplip()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_bkplip);
result[1] = _mm_load_si128((const __m128i *)&q_bkplip + 1);
result[2] = _mm_load_si128((const __m128i *)&q_bkplip + 2);
return result;
}

canonical_decompiled_code:
__m128i *func17()
{
__m128i *result; // rax

result = (__m128i *)qmap;
*(__m128i *)qmap = _mm_load_si128((const __m128i *)&q_bkplip);
result[1] = _mm_load_si128((const __m128i *)&q_bkplip + 1);
result[2] = _mm_load_si128((const __m128i *)&q_bkplip + 2);
return result;
}

original_code: void plug_bkplip() { qmap[0] = q_bkplip; }


decompiled_code:
void __fastcall pagereg_write(unsigned __int16 a1)
{
unsigned __int16 v1; // di
char *v2; // rax
char *v3; // rax

if ( oldmap != a1 )
{
oldmap = a1;
*(&pagemap + 1) = (char *)&ram + 0x4000 * (unsigned __int64)((a1 >> 12) & 7);
*(&pagemap + 2) = (char *)&ram + 0x4000 * (unsigned __int64)(HIBYTE(a1) & 7);
v1 = a1 & 0x1B;
switch ( v1 )
{
case 0u:
pdp_ram_map = (void *)((unsigned __int64)pdp_ram_map | 0xFF0000);
pdp_mem_map |= 0xFF0000uLL;
break;
case 1u:
*(&pagemap + 2) = &rom;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map |= 0xFF0000uLL;
break;
case 2u:
*(&pagemap + 2) = (char *)&rom + 0x4000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map |= 0xFF0000uLL;
break;
case 8u:
*(&pagemap + 2) = (char *)&rom + 0x8000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
break;
case 0x10u:
*(&pagemap + 2) = (char *)&rom + 49152;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
break;
default:
fprintf(stderr, "Bad ROM map %o\n", v1);
break;
}
video_map[2] = 0;
video_map[1] = 0;
v2 = (char *)*(&pagemap + 1);
if ( v2 == (char *)&ram + 0x4000 )
{
video_map[1] = 1;
}
else if ( v2 == (char *)&ram + 114688 )
{
video_map[1] = 2;
}
v3 = (char *)*(&pagemap + 2);
if ( v3 == (char *)&ram + 0x4000 )
{
video_map[2] = 1;
}
else if ( v3 == (char *)&ram + 114688 )
{
video_map[2] = 2;
}
}
}

canonical_decompiled_code:
void __fastcall func19(unsigned __int16 a1)
{
unsigned __int16 v1; // di
char *v2; // rax
char *v3; // rax

if ( oldmap != a1 )
{
oldmap = a1;
*(&pagemap + 1) = (char *)&ram + 0x4000 * (unsigned __int64)((a1 >> 12) & 7);
*(&pagemap + 2) = (char *)&ram + 0x4000 * (unsigned __int64)(HIBYTE(a1) & 7);
v1 = a1 & 0x1B;
switch ( v1 )
{
case 0u:
pdp_ram_map = (void *)((unsigned __int64)pdp_ram_map | 0xFF0000);
pdp_mem_map |= 0xFF0000uLL;
break;
case 1u:
*(&pagemap + 2) = &rom;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map |= 0xFF0000uLL;
break;
case 2u:
*(&pagemap + 2) = (char *)&rom + 0x4000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map |= 0xFF0000uLL;
break;
case 8u:
*(&pagemap + 2) = (char *)&rom + 0x8000;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
break;
case 0x10u:
*(&pagemap + 2) = (char *)&rom + 49152;
pdp_ram_map = (void *)((unsigned int)pdp_ram_map & 0xFF00FFFF);
pdp_mem_map = (unsigned int)pdp_mem_map & 0xFF00FFFF;
break;
default:
fprintf(stderr, "Bad ROM map %o\n", v1);
break;
}
video_map[2] = 0;
video_map[1] = 0;
v2 = (char *)*(&pagemap + 1);
if ( v2 == (char *)&ram + 0x4000 )
{
video_map[1] = 1;
}
else if ( v2 == (char *)&ram + 114688 )
{
video_map[1] = 2;
}
v3 = (char *)*(&pagemap + 2);
if ( v3 == (char *)&ram + 0x4000 )
{
video_map[2] = 1;
}
else if ( v3 == (char *)&ram + 114688 )
{
video_map[2] = 2;
}
}
}

original_code:
void pagereg_write(d_word word) {
 if (oldmap == word) return;
 oldmap = word;
 pagemap[1] = ram[(word >> 12) & 7];
 pagemap[2] = ram[(word >> 8) & 7];
 switch (word & 033) {
 case 000:
  pdp_ram_map |= 0x00ff0000;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 001:
  pagemap[2] = rom[0];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 002:
  pagemap[2] = rom[1];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 010:
  pagemap[2] = rom[2];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 case 020:
  pagemap[2] = rom[3];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 default: fprintf(stderr, "Bad ROM map %o\n", word & 033);
 }
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

canonical_original_code:
void pagereg_write(unsigned short word) {
 if (oldmap == word) return;
 oldmap = word;
 pagemap[1] = ram[(word >> 12) & 7];
 pagemap[2] = ram[(word >> 8) & 7];
 switch (word & 033) {
 case 000:
  pdp_ram_map |= 0x00ff0000;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 001:
  pagemap[2] = rom[0];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 002:
  pagemap[2] = rom[1];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map |= 0x00ff0000;
  break;
 case 010:
  pagemap[2] = rom[2];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 case 020:
  pagemap[2] = rom[3];
  pdp_ram_map &= 0xff00ffff;
  pdp_mem_map &= 0xff00ffff;
  break;
 default: fprintf(stderr, "Bad ROM map %o\n", word & 033);
 }
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

variable_types:
word
unsigned short


decompiled_code:
unsigned __int64 __fastcall pagereg_bwrite(__int16 a1)
{
unsigned __int64 result; // rax
char *v2; // rdi

result = (unsigned __int16)oldmap;
if ( HIBYTE(oldmap) != (unsigned __int8)a1 )
{
oldmap = (a1 << 8) | (unsigned __int8)oldmap;
result = (unsigned __int64)&ram + 0x4000 * (unsigned __int64)(((unsigned __int8)a1 >> 4) & 7);
*(&pagemap + 1) = (void *)result;
v2 = (char *)&ram + 0x4000 * (unsigned __int64)(a1 & 7);
*(&pagemap + 2) = v2;
video_map[2] = 0;
if ( (_UNKNOWN *)result == (_UNKNOWN *)((char *)&ram + 0x4000) )
{
video_map[1] = 1;
}
else
{
LOBYTE(result) = result == (_QWORD)&ram + 114688;
result = (unsigned int)(2 * result);
video_map[1] = result;
}
if ( v2 == (char *)&ram + 0x4000 )
{
video_map[2] = 1;
}
else if ( v2 == (char *)&ram + 114688 )
{
video_map[2] = 2;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func20(__int16 a1)
{
unsigned __int64 result; // rax
char *v2; // rdi

result = (unsigned __int16)oldmap;
if ( HIBYTE(oldmap) != (unsigned __int8)a1 )
{
oldmap = (a1 << 8) | (unsigned __int8)oldmap;
result = (unsigned __int64)&ram + 0x4000 * (unsigned __int64)(((unsigned __int8)a1 >> 4) & 7);
*(&pagemap + 1) = (void *)result;
v2 = (char *)&ram + 0x4000 * (unsigned __int64)(a1 & 7);
*(&pagemap + 2) = v2;
video_map[2] = 0;
if ( (_UNKNOWN *)result == (_UNKNOWN *)((char *)&ram + 0x4000) )
{
video_map[1] = 1;
}
else
{
LOBYTE(result) = result == (_QWORD)&ram + 114688;
result = (unsigned int)(2 * result);
video_map[1] = result;
}
if ( v2 == (char *)&ram + 0x4000 )
{
video_map[2] = 1;
}
else if ( v2 == (char *)&ram + 114688 )
{
video_map[2] = 2;
}
}
return result;
}

original_code:
void pagereg_bwrite(d_byte byte) {
 if (oldmap >> 8 == byte) return;
 oldmap = oldmap & 0xff | byte << 8;
 pagemap[1] = ram[(byte >> 4) & 7];
 pagemap[2] = ram[byte & 7];
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

canonical_original_code:
void pagereg_bwrite(unsigned char byte) {
 if (oldmap >> 8 == byte) return;
 oldmap = oldmap & 0xff | byte << 8;
 pagemap[1] = ram[(byte >> 4) & 7];
 pagemap[2] = ram[byte & 7];
 video_map[1] = video_map[2] = 0;
 if (pagemap[1] == ram[1]) video_map[1] = 1;
 else if (pagemap[1] == ram[7]) video_map[1] = 2;
 if (pagemap[2] == ram[1]) video_map[2] = 1;
 else if (pagemap[2] == ram[7]) video_map[2] = 2;
}

variable_types:
byte
unsigned char


calc

original_code:
__inline__ static e_int16 calc(PSG *psg)
{
  int i,noise ;
  e_uint32 incr ;
  e_int16 mix = 0 ;
  psg->base_count += base_incr ;
  incr = (psg->base_count >> 24) ;
  psg->base_count &= (1 << 24) - 1 ;
 if(psg->env_enable)
  {
    psg->env_count += incr ;
  if(psg->env_count&0x10000)
    {
   psg->env_ptr = (psg->env_ptr + 1)&31 ;
   if(psg->env_ptr == 0)
      {
    psg->env_enable = psg->env_hold ;
    psg->env_reverse = psg->env_reverse^psg->env_alt ;
   }
    psg->env_count -= psg->env_freq ;
  }
 }
  psg->noise_count += incr ;
  if(psg->noise_count&0x40)
  {
  if(psg->noise_seed&1) psg->noise_seed^= 0x24000 ;
    psg->noise_seed >>= 1 ;
  psg->noise_count -= psg->noise_freq ;
 }
 noise = psg->noise_seed & 1 ;
 for(i=0;i<3;i++)
  {
    psg->count[i] += incr ;
  if(psg->count[i]&0x1000)
    {
       if(psg->freq[i]>1)
       {
         psg->edge[i] = !psg->edge[i] ;
         psg->count[i] -= psg->freq[i] ;
       }
       else
       {
         psg->edge[i] = 1 ;
       }
    }
    if(psg->mute[i]) continue ;
    if((psg->tmask[i]||psg->edge[i])&&(psg->nmask[i]||noise))
    {
   if((psg->volume[i]&32)==0)
        mix += psg->voltbl[psg->volume[i]&31] ;
   else if(psg->env_reverse)
        mix += psg->voltbl[31-psg->env_ptr] ;
   else
        mix += psg->voltbl[psg->env_ptr] ;
  }
    else if(psg->tmask[i]&&psg->nmask[i])
    {
      mix += psg->voltbl[psg->volume[i]&31] ;
    }
 }
 return mix ;
}

canonical_original_code:
__inline__ static short calc(struct PSG *psg)
{
  int i; int noise;
  unsigned int incr;
  short mix = 0;
  psg->base_count += base_incr ;
  incr = (psg->base_count >> 24) ;
  psg->base_count &= (1 << 24) - 1 ;
 if(psg->env_enable)
  {
    psg->env_count += incr ;
  if(psg->env_count&0x10000)
    {
   psg->env_ptr = (psg->env_ptr + 1)&31 ;
   if(psg->env_ptr == 0)
      {
    psg->env_enable = psg->env_hold ;
    psg->env_reverse = psg->env_reverse^psg->env_alt ;
   }
    psg->env_count -= psg->env_freq ;
  }
 }
  psg->noise_count += incr ;
  if(psg->noise_count&0x40)
  {
  if(psg->noise_seed&1) psg->noise_seed^= 0x24000 ;
    psg->noise_seed >>= 1 ;
  psg->noise_count -= psg->noise_freq ;
 }
 noise = psg->noise_seed & 1 ;
 for(i=0;i<3;i++)
  {
    psg->count[i] += incr ;
  if(psg->count[i]&0x1000)
    {
       if(psg->freq[i]>1)
       {
         psg->edge[i] = !psg->edge[i] ;
         psg->count[i] -= psg->freq[i] ;
       }
       else
       {
         psg->edge[i] = 1 ;
       }
    }
    if(psg->mute[i]) continue ;
    if((psg->tmask[i]||psg->edge[i])&&(psg->nmask[i]||noise))
    {
   if((psg->volume[i]&32)==0)
        mix += psg->voltbl[psg->volume[i]&31] ;
   else if(psg->env_reverse)
        mix += psg->voltbl[31-psg->env_ptr] ;
   else
        mix += psg->voltbl[psg->env_ptr] ;
  }
    else if(psg->tmask[i]&&psg->nmask[i])
    {
      mix += psg->voltbl[psg->volume[i]&31] ;
    }
 }
 return mix ;
}

variable_types: struct PSG { unsigned int *voltbl; unsigned char reg[32]; int out; unsigned int count[3]; unsigned int volume[3]; unsigned int freq[3]; unsigned int edge[3]; unsigned int tmask[3]; unsigned int nmask[3]; unsigned int mute[3]; unsigned int base_count; unsigned int env_volume; unsigned int env_ptr; unsigned int env_enable; unsigned int env_reverse; unsigned int env_hold; unsigned int env_alt; unsigned int env_freq; unsigned int env_count; unsigned int noise_seed; unsigned int noise_count; unsigned int noise_freq; unsigned int realstep; unsigned int psgtime; unsigned int psgstep; unsigned int adr; } *

variable_types: i int

variable_types: noise int

variable_types:
incr
unsigned int

variable_types: mix short

return_type: short


decompiled_code:
void __fastcall PSG_set_quality(int a1)
{
if ( a1 )
{
base_incr = 0x1000000;
}
else if ( rate )
{
base_incr = (int)((double)clk * 16777216.0 / (double)(16 * rate));
}
HQ = a1;
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 )
{
base_incr = 0x1000000;
}
else if ( rate )
{
base_incr = (int)((double)clk * 16777216.0 / (double)(16 * rate));
}
HQ = a1;
}

original_code:
void PSG_set_quality(e_uint32 q)
{
  if(q)
    base_incr = 1<<24 ;
  else if(rate!=0)
    base_incr = (e_uint32)((double)clk * (1<<24) / (16*rate) ) ;
  HQ = q ;
}

canonical_original_code:
void PSG_set_quality(unsigned int q)
{
  if(q)
    base_incr = 1<<24 ;
  else if(rate!=0)
    base_incr = (e_uint32)((double)clk * (1<<24) / (16*rate) ) ;
  HQ = q ;
}

variable_types:
q
unsigned int


decompiled_code:
void __fastcall PSG_init(int a1, int a2)
{
clk = a1;
rate = a2;
PSG_set_quality(0);
}

canonical_decompiled_code:
void __fastcall func2(int a1, int a2)
{
clk = a1;
rate = a2;
func1(0);
}

original_code:
void PSG_init(e_uint32 c, e_uint32 r)
{
 clk = c ;
 rate = r ;
  PSG_set_quality(0) ;
}

canonical_original_code:
void PSG_init(unsigned int c, unsigned int r)
{
 clk = c ;
 rate = r ;
  PSG_set_quality(0) ;
}

variable_types:
c
unsigned int

variable_types:
r
unsigned int


decompiled_code:
_QWORD *PSG_new()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = malloc(0xC0uLL);
v1 = v0;
if ( v0 )
PSG_setVolumeMode(v0, 1);
return v1;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = malloc(0xC0uLL);
v1 = v0;
if ( v0 )
func3(v0, 1);
return v1;
}

original_code:
PSG *PSG_new()
{
 PSG *psg ;
 psg = malloc(sizeof(PSG)) ;
 if( psg == ((void *)0) ) return ((void *)0) ;
 PSG_setVolumeMode(psg,1) ;
 return psg ;
}

canonical_original_code:
struct PSG *PSG_new()
{
 struct PSG *psg;
 psg = malloc(sizeof(PSG)) ;
 if( psg == ((void *)0) ) return ((void *)0) ;
 PSG_setVolumeMode(psg,1) ;
 return psg ;
}

return_type: struct PSG { unsigned int *voltbl; unsigned char reg[32]; int out; unsigned int count[3]; unsigned int volume[3]; unsigned int freq[3]; unsigned int edge[3]; unsigned int tmask[3]; unsigned int nmask[3]; unsigned int mute[3]; unsigned int base_count; unsigned int env_volume; unsigned int env_ptr; unsigned int env_enable; unsigned int env_reverse; unsigned int env_hold; unsigned int env_alt; unsigned int env_freq; unsigned int env_count; unsigned int noise_seed; unsigned int noise_count; unsigned int noise_freq; unsigned int realstep; unsigned int psgtime; unsigned int psgstep; unsigned int adr; } *


decompiled_code:
void __fastcall PSG_delete(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func6(void *a1)
{
free(a1);
}

original_code:
void PSG_delete(PSG *psg)
{
 free(psg) ;
}

canonical_original_code:
void PSG_delete(struct PSG *psg)
{
 free(psg) ;
}

user_defined_types: struct PSG { unsigned int *voltbl; unsigned char reg[32]; int out; unsigned int count[3]; unsigned int volume[3]; unsigned int freq[3]; unsigned int edge[3]; unsigned int tmask[3]; unsigned int nmask[3]; unsigned int mute[3]; unsigned int base_count; unsigned int env_volume; unsigned int env_ptr; unsigned int env_enable; unsigned int env_reverse; unsigned int env_hold; unsigned int env_alt; unsigned int env_freq; unsigned int env_count; unsigned int noise_seed; unsigned int noise_count; unsigned int noise_freq; unsigned int realstep; unsigned int psgtime; unsigned int psgstep; unsigned int adr; }


decompiled_code:
void PSG_close()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
void PSG_close()
{
}

canonical_original_code:
void PSG_close()
{
}


decompiled_code:
__int64 __fastcall PSG_readIO(__int64 a1)
{
return *(unsigned __int8 *)(*(unsigned int *)(a1 + 188) + a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned __int8 *)(*(unsigned int *)(a1 + 188) + a1 + 8);
}

original_code:
e_uint8 PSG_readIO(PSG *psg)
{
  return (e_uint8)(psg->reg[psg->adr]) ;
}

canonical_original_code:
unsigned char PSG_readIO(struct PSG *psg)
{
  return (e_uint8)(psg->reg[psg->adr]) ;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall PSG_readReg(__int64 a1, char a2)
{
return *(unsigned __int8 *)((a2 & 0x1F) + a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2)
{
return *(unsigned __int8 *)((a2 & 0x1F) + a1 + 8);
}

original_code:
e_uint8 PSG_readReg(PSG *psg, e_uint32 reg)
{
 return (e_uint8)(psg->reg[reg&0x1f]) ;
}

canonical_original_code:
unsigned char PSG_readReg(struct PSG *psg, unsigned int reg)
{
 return (e_uint8)(psg->reg[reg&0x1f]) ;
}

variable_types:
reg
unsigned int


original_code:
void PSG_writeReg(PSG *psg, e_uint32 reg, e_uint32 val)
{
 int c ;
 if (reg>15) return ;
 psg->reg[reg] = (e_uint8)(val & 0xff) ;
 switch(reg)
  {
 case 0: case 2: case 4:
 case 1: case 3: case 5:
  c = reg >> 1 ;
  psg->freq[c] = ( (psg->reg[c*2+1]&15) << 8 ) + psg->reg[c*2] ;
   break ;
 case 6:
    psg->noise_freq = val==0 ? 1 : ((val & 31) << 1) ;
  break ;
 case 7:
  psg->tmask[0] = (val&1) ;
  psg->tmask[1] = (val&2) ;
  psg->tmask[2] = (val&4) ;
  psg->nmask[0] = (val&8) ;
  psg->nmask[1] = (val&16) ;
  psg->nmask[2] = (val&32) ;
  break ;
 case 8:
 case 9:
 case 10:
  psg->volume[reg-8] = val << 1 ;
    if(val&16) psg->env_enable = 1 ;
    break ;
 case 11:
 case 12:
  psg->env_freq = (psg->reg[12] << 8) + psg->reg[11] ;
  break ;
 case 13:
  val = val&15 ;
  if(val<4) val = 9 ;
    else if(val<8) val = 15 ;
  psg->env_count = 0x10000 - psg->env_freq ;
  psg->env_ptr = 0 ;
  psg->env_hold = (val&1)^1 ;
  psg->env_reverse = ((val>>2)&1)^1 ;
  psg->env_alt = ((val>>1)&1)^(val&1) ;
  break ;
 case 14:
 case 15:
 default:
  break;
 }
 return ;
}

canonical_original_code:
void PSG_writeReg(struct PSG *psg, unsigned int reg, unsigned int val)
{
 int c;
 if (reg>15) return ;
 psg->reg[reg] = (e_uint8)(val & 0xff) ;
 switch(reg)
  {
 case 0: case 2: case 4:
 case 1: case 3: case 5:
  c = reg >> 1 ;
  psg->freq[c] = ( (psg->reg[c*2+1]&15) << 8 ) + psg->reg[c*2] ;
   break ;
 case 6:
    psg->noise_freq = val==0 ? 1 : ((val & 31) << 1) ;
  break ;
 case 7:
  psg->tmask[0] = (val&1) ;
  psg->tmask[1] = (val&2) ;
  psg->tmask[2] = (val&4) ;
  psg->nmask[0] = (val&8) ;
  psg->nmask[1] = (val&16) ;
  psg->nmask[2] = (val&32) ;
  break ;
 case 8:
 case 9:
 case 10:
  psg->volume[reg-8] = val << 1 ;
    if(val&16) psg->env_enable = 1 ;
    break ;
 case 11:
 case 12:
  psg->env_freq = (psg->reg[12] << 8) + psg->reg[11] ;
  break ;
 case 13:
  val = val&15 ;
  if(val<4) val = 9 ;
    else if(val<8) val = 15 ;
  psg->env_count = 0x10000 - psg->env_freq ;
  psg->env_ptr = 0 ;
  psg->env_hold = (val&1)^1 ;
  psg->env_reverse = ((val>>2)&1)^1 ;
  psg->env_alt = ((val>>1)&1)^(val&1) ;
  break ;
 case 14:
 case 15:
 default:
  break;
 }
 return ;
}

variable_types:
val
unsigned int

variable_types: c int


decompiled_code:
void __fastcall PSG_writeIO(__int64 a1, char a2, char a3)
{
if ( (a2 & 1) != 0 )
PSG_writeReg(a1, *(_DWORD *)(a1 + 188), a3);
else
*(_DWORD *)(a1 + 188) = a3 & 0x1F;
}

original_code:
void PSG_writeIO(PSG *psg, e_uint32 adr, e_uint32 val)
{
  if(adr & 1) PSG_writeReg(psg, psg->adr, val) ;
  else psg->adr = val & 0x1f ;
}

canonical_original_code:
void PSG_writeIO(struct PSG *psg, unsigned int adr, unsigned int val)
{
  if(adr & 1) PSG_writeReg(psg, psg->adr, val) ;
  else psg->adr = val & 0x1f ;
}

variable_types:
adr
unsigned int


original_code:
e_int16 PSG_calc(PSG *psg)
{
  if(!HQ) return calc(psg) << 4 ;
  while (psg->realstep > psg->psgtime)
  {
    psg->psgtime += psg->psgstep ;
    psg->out += calc(psg) ;
   psg->out >>= 1 ;
  }
  psg->psgtime = psg->psgtime - psg->realstep ;
  return (e_int16)( psg->out << 4 ) ;
}

canonical_original_code:
short PSG_calc(struct PSG *psg)
{
  if(!HQ) return calc(psg) << 4 ;
  while (psg->realstep > psg->psgtime)
  {
    psg->psgtime += psg->psgstep ;
    psg->out += calc(psg) ;
   psg->out >>= 1 ;
  }
  psg->psgtime = psg->psgtime - psg->realstep ;
  return (e_int16)( psg->out << 4 ) ;
}


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

result = mouseflag;
if ( mouseflag == 1 )
{
mouse_up = 1;
mouse_right = 2;
mouse_down = 4;
mouse_left = 8;
mouse_but0 = 16;
mouse_strobe = 8;
}
else if ( mouseflag == 2 )
{
mouse_up = 256;
mouse_right = 512;
mouse_down = 1024;
mouse_left = 2048;
mouse_but0 = 4096;
mouse_strobe = 0x8000;
}
return result;
}

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

result = mouseflag;
if ( mouseflag == 1 )
{
mouse_up = 1;
mouse_right = 2;
mouse_down = 4;
mouse_left = 8;
mouse_but0 = 16;
mouse_strobe = 8;
}
else if ( mouseflag == 2 )
{
mouse_up = 256;
mouse_right = 512;
mouse_down = 1024;
mouse_left = 2048;
mouse_but0 = 4096;
mouse_strobe = 0x8000;
}
return result;
}

original_code:
int
mouse_init() {
 switch (mouseflag) {
 case 1:
  mouse_up = 1;
  mouse_right = 2;
  mouse_down = 4;
  mouse_left = 8;
  mouse_but0 = 16;
  mouse_strobe = 8;
  break;
 case 2:
  mouse_up = 0x100;
  mouse_right = 0x200;
  mouse_down = 0x400;
  mouse_left = 0x800;
  mouse_but0 = 0x1000;
  mouse_strobe = 0x8000;
  break;
 }
}


decompiled_code:
__int64 __fastcall mouse_read(__int64 a1, _WORD *a2)
{
__int16 v2; // ax
__int16 v3; // dx
__int16 v4; // ax

v2 = mouse_button_state;
*a2 = mouse_button_state;
if ( relx )
{
v3 = mouse_right;
if ( relx <= 0 )
v3 = mouse_left;
*a2 = v3 | v2;
}
if ( rely )
{
v4 = mouse_down;
if ( rely <= 0 )
v4 = mouse_up;
*a2 |= v4;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _WORD *a2)
{
__int16 v2; // ax
__int16 v3; // dx
__int16 v4; // ax

v2 = mouse_button_state;
*a2 = mouse_button_state;
if ( relx )
{
v3 = mouse_right;
if ( relx <= 0 )
v3 = mouse_left;
*a2 = v3 | v2;
}
if ( rely )
{
v4 = mouse_down;
if ( rely <= 0 )
v4 = mouse_up;
*a2 |= v4;
}
return 0LL;
}

original_code:
int mouse_read(c_addr addr, d_word *word) {
 *word = mouse_button_state;
 if (relx) {
  *word |= relx > 0 ? mouse_right : mouse_left;
 }
 if (rely) {
  *word |= rely > 0 ? mouse_down : mouse_up;
 }
 return 0;
}

canonical_original_code:
int mouse_read(unsigned long addr, unsigned short *word) {
 *word = mouse_button_state;
 if (relx) {
  *word |= relx > 0 ? mouse_right : mouse_left;
 }
 if (rely) {
  *word |= rely > 0 ? mouse_down : mouse_up;
 }
 return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short *


decompiled_code:
__int64 __fastcall mouse_write(__int64 a1, unsigned __int16 a2)
{
if ( (a2 & (unsigned __int16)mouse_strobe) == 0 )
{
if ( relx )
relx /= 2;
if ( rely )
rely /= 2;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int16 a2)
{
if ( (a2 & (unsigned __int16)mouse_strobe) == 0 )
{
if ( relx )
relx /= 2;
if ( rely )
rely /= 2;
}
return 0LL;
}

original_code:
int mouse_write(c_addr addr, d_word word) {
 if (word & mouse_strobe)
  return 0;
 if (relx) {
  relx = relx > 0 ? relx/2 : -(-relx/2);
 }
 if (rely) {
  rely = rely > 0 ? rely/2 : -(-rely/2);
 }
 return 0;
}

canonical_original_code:
int mouse_write(unsigned long addr, unsigned short word) {
 if (word & mouse_strobe)
  return 0;
 if (relx) {
  relx = relx > 0 ? relx/2 : -(-relx/2);
 }
 if (rely) {
  rely = rely > 0 ? rely/2 : -(-rely/2);
 }
 return 0;
}


decompiled_code:
__int64 serial_read()
{
__int64 result; // rax
char v1; // cl
int v2; // eax
int v3; // eax
unsigned __int8 v4; // bl
int v5; // eax

if ( !data_ptr )
return 144LL;
result = 144LL;
if ( ready_out )
{
v1 = bitnum;
if ( bitnum == -1 )
{
bitnum = 0;
result = 128LL;
}
else if ( bitnum == 8 )
{
result = 144LL;
}
else
{
v2 = *(unsigned __int8 *)data_ptr;
++bitnum;
v3 = (v2 >> v1) & 1;
if ( v3 )
LOBYTE(v3) = 16;
v4 = v3 | 0x80;
if ( !ser_in )
ser_in = fopen("serial.in", "r");
if ( bitnum == 8 )
{
v5 = getc(ser_in);
if ( v5 == -1 )
data_ptr = 0LL;
retbyte = v5;
bitnum = -1;
}
result = v4;
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
char v1; // cl
int v2; // eax
int v3; // eax
unsigned __int8 v4; // bl
int v5; // eax

if ( !data_ptr )
return 144LL;
result = 144LL;
if ( ready_out )
{
v1 = bitnum;
if ( bitnum == -1 )
{
bitnum = 0;
result = 128LL;
}
else if ( bitnum == 8 )
{
result = 144LL;
}
else
{
v2 = *(unsigned __int8 *)data_ptr;
++bitnum;
v3 = (v2 >> v1) & 1;
if ( v3 )
LOBYTE(v3) = 16;
v4 = v3 | 0x80;
if ( !ser_in )
ser_in = fopen("serial.in", "r");
if ( bitnum == 8 )
{
v5 = getc(ser_in);
if ( v5 == -1 )
data_ptr = 0LL;
retbyte = v5;
bitnum = -1;
}
result = v4;
}
}
return result;
}

original_code:
d_word
serial_read() {
 unsigned char ret = 0200;
 if (!data_ptr || !ready_out) {
  return 0200 | 020;
 }
 if (bitnum == -1) {
  bitnum = 0;
  return 0200 & ~020;
 }
 if (bitnum == 8) {
  return 0200 | 020;
 }
 ret |= *data_ptr & (1<<bitnum++) ? 020 : 0;
 if (!ser_in) {
  ser_in = fopen("serial.in", "r");
 }
 if (bitnum == 8) {
  int c = getc(ser_in);
  if (c == (-1)) data_ptr = 0;
  retbyte = c;
  bitnum = -1;
 }
 return ret;
}

canonical_original_code:
unsigned short serial_read() {
 unsigned char ret = 0200;
 if (!data_ptr || !ready_out) {
  return 0200 | 020;
 }
 if (bitnum == -1) {
  bitnum = 0;
  return 0200 & ~020;
 }
 if (bitnum == 8) {
  return 0200 | 020;
 }
 ret |= *data_ptr & (1<<bitnum++) ? 020 : 0;
 if (!ser_in) {
  ser_in = fopen("serial.in", "r");
 }
 if (bitnum == 8) {
  int c = getc(ser_in);
  if (c == (-1)) data_ptr = 0;
  retbyte = c;
  bitnum = -1;
 }
 return ret;
}

variable_types:
ret
unsigned char

return_type: unsigned short


decompiled_code:
size_t __fastcall serial_write(char a1)
{
size_t result; // rax
char v2; // cl

result = a1 & 0x20;
if ( (unsigned __int8)ready_out != (_DWORD)result )
ready_out = a1 & 0x20;
if ( ready_out )
{
if ( !data_ptr )
{
retbyte = -68;
data_ptr = (__int64)&retbyte;
}
bitnum = -1;
}
else
{
bitnum = 8;
}
v2 = curbit;
if ( curbit < 0 )
{
if ( ser_out && (a1 & 0x10) == 0 )
curbit = 0;
}
else
{
++curbit;
result = (unsigned __int8)((a1 & 0x10) != 0) << v2;
LOBYTE(result) = forming | result;
forming = result;
if ( v2 == 7 )
{
curbit = -1;
if ( (_BYTE)result != 0xBC )
{
data_ptr = 0LL;
result = fwrite("Serial line probe failed\n", 1uLL, 0x19uLL, stderr);
}
}
}
ser_out = (a1 & 0x10) != 0;
return result;
}

canonical_decompiled_code:
size_t __fastcall func1(char a1)
{
size_t result; // rax
char v2; // cl

result = a1 & 0x20;
if ( (unsigned __int8)ready_out != (_DWORD)result )
ready_out = a1 & 0x20;
if ( ready_out )
{
if ( !data_ptr )
{
retbyte = -68;
data_ptr = (__int64)&retbyte;
}
bitnum = -1;
}
else
{
bitnum = 8;
}
v2 = curbit;
if ( curbit < 0 )
{
if ( ser_out && (a1 & 0x10) == 0 )
curbit = 0;
}
else
{
++curbit;
result = (unsigned __int8)((a1 & 0x10) != 0) << v2;
LOBYTE(result) = forming | result;
forming = result;
if ( v2 == 7 )
{
curbit = -1;
if ( (_BYTE)result != 0xBC )
{
data_ptr = 0LL;
result = fwrite("Serial line probe failed\n", 1uLL, 0x19uLL, stderr);
}
}
}
ser_out = (a1 & 0x10) != 0;
return result;
}

original_code:
void
serial_write(d_word w) {
 unsigned char cur = (w & 020) != 0;
 if (ready_out != (w & 040)) {
  ready_out = w & 040;
 }
 if (!ready_out) {
  bitnum = 8;
 } else {
     if (!data_ptr) {
  retbyte = 0xBC;
  data_ptr = &retbyte;
     }
     bitnum = -1;
 }
 if (curbit >= 0) {
  forming |= cur << curbit++;
  if (curbit == 8) {
   curbit = -1;
   if (forming != 0xBC) {
    data_ptr = 0;
    fprintf(stderr, "Serial line probe failed\n");
   }
  }
 } else if (ser_out && !cur) {
  curbit = 0;
 }
 ser_out = cur;
}

canonical_original_code:
void serial_write(unsigned short w) {
 unsigned char cur = (w & 020) != 0;
 if (ready_out != (w & 040)) {
  ready_out = w & 040;
 }
 if (!ready_out) {
  bitnum = 8;
 } else {
     if (!data_ptr) {
  retbyte = 0xBC;
  data_ptr = &retbyte;
     }
     bitnum = -1;
 }
 if (curbit >= 0) {
  forming |= cur << curbit++;
  if (curbit == 8) {
   curbit = -1;
   if (forming != 0xBC) {
    data_ptr = 0;
    fprintf(stderr, "Serial line probe failed\n");
   }
  }
 } else if (ser_out && !cur) {
  curbit = 0;
 }
 ser_out = cur;
}

variable_types:
w
unsigned short

variable_types:
cur
unsigned char


callback

decompiled_code:
__int64 __fastcall callback(__int64 a1, void *a2, int a3)
{
__int64 result; // rax
int v5; // ebx

result = SDL_SemValue(sem);
if ( (_DWORD)result != 2 )
{
v5 = cur_out_buf_1;
memcpy(a2, (const void *)sound_buf[2 * cur_out_buf_1], a3);
cur_out_buf_1 = (v5 + 1) % 2;
result = SDL_SemPost(sem);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, void *a2, int a3)
{
__int64 result; // rax
int v5; // ebx

result = SDL_SemValue(sem);
if ( (_DWORD)result != 2 )
{
v5 = cur_out_buf_1;
memcpy(a2, (const void *)sound_buf[2 * cur_out_buf_1], a3);
cur_out_buf_1 = (v5 + 1) % 2;
result = SDL_SemPost(sem);
}
return result;
}

original_code:
void callback(void * dummy, Uint8 * outbuf, int len)
{
 static int cur_out_buf;
 if (SDL_SemValue(sem) == 2) {
  return;
 }
 memcpy(outbuf, sound_buf[cur_out_buf].buf, len);
 cur_out_buf = (cur_out_buf + 1) % 2;
 SDL_SemPost(sem);
}

canonical_original_code:
void callback(void *dummy, unsigned char *outbuf, int len)
{
 static int cur_out_buf;
 if (SDL_SemValue(sem) == 2) {
  return;
 }
 memcpy(outbuf, sound_buf[cur_out_buf].buf, len);
 cur_out_buf = (cur_out_buf + 1) % 2;
 SDL_SemPost(sem);
}

variable_types:
dummy
void *

variable_types:
outbuf
unsigned char *

variable_types: len int


decompiled_code:
void sound_finish(void)
{
SDL_PauseAudio(1LL);
SDL_DestroySemaphore(sem);
}

canonical_decompiled_code:
void func1(void)
{
SDL_PauseAudio(1LL);
SDL_DestroySemaphore(sem);
}

original_code:
void sound_finish() {
 SDL_PauseAudio(1);
 SDL_DestroySemaphore(sem);
}


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

result = 0;
if ( psg )
result = (__int16)PSG_calc();
return result;
}

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

result = 0;
if ( psg )
result = (__int16)PSG_calc();
return result;
}

original_code:
int
synth_next() {
 int a = psg ? PSG_calc(psg) : 0;
 return a;
}

variable_types: a int


decompiled_code:
void boot_init()
{
if ( !boot_done_0 )
{
boot_done_0 = 1;
if ( terak )
{
load_rom(0xF600u, "TERAK.ROM", 0x80u, 0x80u);
}
else if ( bkmodel )
{
load_rom11((__int64)&system_rom, 0, bos11rom, 0x2000);
load_rom11((__int64)&system_rom, 0x2000, diskrom, 4096);
load_rom11((__int64)&rom, 0, basic11arom, 0x4000);
load_rom11((unsigned int)((_DWORD)&rom + 0x4000), 0, basic11brom, 0x2000);
load_rom11((unsigned int)((_DWORD)&rom + 0x4000), 0x2000, bos11extrom, 0x2000);
}
else
{
load_rom(0x8000u, rompath10, 0x2000u, 0x2000u);
load_rom(0xA000u, rompath12, 0x5F80u, 0x6000u);
load_rom(0xE000u, rompath16, 0x1000u, 0x1000u);
}
}
}

canonical_decompiled_code:
void func2()
{
if ( !boot_done_0 )
{
boot_done_0 = 1;
if ( terak )
{
func0(0xF600u, "TERAK.ROM", 0x80u, 0x80u);
}
else if ( bkmodel )
{
func1((__int64)&system_rom, 0, bos11rom, 0x2000);
func1((__int64)&system_rom, 0x2000, diskrom, 4096);
func1((__int64)&rom, 0, basic11arom, 0x4000);
func1((unsigned int)((_DWORD)&rom + 0x4000), 0, basic11brom, 0x2000);
func1((unsigned int)((_DWORD)&rom + 0x4000), 0x2000, bos11extrom, 0x2000);
}
else
{
func0(0x8000u, rompath10, 0x2000u, 0x2000u);
func0(0xA000u, rompath12, 0x5F80u, 0x6000u);
func0(0xE000u, rompath16, 0x1000u, 0x1000u);
}
}
}

original_code:
int
boot_init()
{
 static unsigned char boot_done = 0;
 if (boot_done) return;
 boot_done = 1;
 if (terak) {
  load_rom(0173000, "TERAK.ROM", 128, 128);
  return;
 }
 if (bkmodel != 0) {
  load_rom11(system_rom, 0, bos11rom, 8192);
  load_rom11(system_rom, 8192, diskrom, 4096);
  load_rom11(rom[0], 0, basic11arom, 16384);
  load_rom11(rom[1], 0, basic11brom, 8192);
  load_rom11(rom[1], 8192, bos11extrom, 8192);
  return;
 }
 load_rom(0100000, rompath10, 8192, 8192);
        load_rom(0120000, rompath12, 24448, 24576);
        load_rom(0160000, rompath16, 4096, 4096);
}


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

result = (unsigned __int16)tty_scroll;
tty_reg = 0;
tty_data = 0;
tty_pending_int = 0;
tty_scroll = 728;
timer_intr_enabled = 0;
if ( (_WORD)result != 728 )
scr_dirty = 1;
return result;
}

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

result = (unsigned __int16)tty_scroll;
tty_reg = 0;
tty_data = 0;
tty_pending_int = 0;
tty_scroll = 728;
timer_intr_enabled = 0;
if ( (_WORD)result != 728 )
scr_dirty = 1;
return result;
}

original_code:
int
tty_init()
{
 int i;
 unsigned short old_scroll = tty_scroll;
 tty_reg = 0;
 tty_data = 0;
 tty_pending_int = 0;
 tty_scroll = 01330;
 timer_intr_enabled = 0;
 if (old_scroll != tty_scroll) {
  scr_dirty = 1;
 }
}

variable_types: i int


notice

repo: emguy/ezfdtd

decompiled_code:
size_t notice(const char *a1, unsigned int a2, const char *a3, const char *a4, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a4);
if ( a1 )
fprintf(stderr, "\x1B[31m%s ", a1);
if ( a2 )
fprintf(stderr, "\x1B[31m%d ", a2);
if ( a3 )
fprintf(stderr, "\x1B[31min %s() ", a3);
fwrite("\x1B[31m: \x1B[36m", 1uLL, 0xCuLL, stderr);
vfprintf(stderr, a4, arg);
return fwrite("\x1B[0m\n", 1uLL, 5uLL, stderr);
}

canonical_decompiled_code:
size_t func0(const char *a1, unsigned int a2, const char *a3, const char *a4, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a4);
if ( a1 )
fprintf(stderr, "\x1B[31m%s ", a1);
if ( a2 )
fprintf(stderr, "\x1B[31m%d ", a2);
if ( a3 )
fprintf(stderr, "\x1B[31min %s() ", a3);
fwrite("\x1B[31m: \x1B[36m", 1uLL, 0xCuLL, stderr);
vfprintf(stderr, a4, arg);
return fwrite("\x1B[0m\n", 1uLL, 5uLL, stderr);
}

original_code:
void notice(const char *file, int line, const char *func, const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 if (1)
 {
  if (file) fprintf(stderr, "\x1B[31m" "%s ", file);
  if (line) fprintf(stderr, "\x1B[31m" "%d ", line);
  if (func) fprintf(stderr, "\x1B[31m" "in %s() ", func);
  fprintf(stderr, "\x1B[31m" ": " "\x1B[36m");
 }
 vfprintf(stderr, fmt, ap);
 __builtin_va_end(ap);
 fprintf(stderr, "\x1B[0m" "\n");
}

canonical_original_code:
void notice(char *file, int line, char *func, char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 if (1)
 {
  if (file) fprintf(stderr, "\x1B[31m" "%s ", file);
  if (line) fprintf(stderr, "\x1B[31m" "%d ", line);
  if (func) fprintf(stderr, "\x1B[31m" "in %s() ", func);
  fprintf(stderr, "\x1B[31m" ": " "\x1B[36m");
 }
 vfprintf(stderr, fmt, ap);
 __builtin_va_end(ap);
 fprintf(stderr, "\x1B[0m" "\n");
}

variable_types:
file
char *

variable_types: line int

variable_types:
func
char *

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall setup_dft(__int64 a1)
{
unsigned int v1; // ebx
__int64 v3; // rbx
int v4; // er12
int v5; // ebp
__int64 v6; // rax
__int64 v7; // rax

if ( (unsigned int)h5_get_attr(a1, "DFT", "enable_DFT", &flag_dft) )
{
if ( *(double *)&flag_dft == 0.0 )
{
v1 = 1;
}
else
{
v1 = h5_get_attr(a1, "DFT", "number_of_frequencies", &total_frequencies);
if ( v1 )
{
v3 = (unsigned int)((_DWORD)&output_ports + 64);
v4 = 1;
if ( total_output_ports <= 0 )
{
v1 = 1;
}
else
{
while ( 1 )
{
v5 = *(_DWORD *)v3;
frequencies = h5_load1(a1, "/DFT/frequencies", total_frequencies, 0.0);
if ( !frequencies )
{
notice("dft.c", 47LL, "setup_dft", "fail to load hdf5 dataset");
return 0;
}
v6 = mem2(2LL, total_frequencies, v5);
*(_QWORD *)(v3 + 16) = v6;
if ( !v6 )
{
notice("dft.c", 49LL, "setup_dft", "fail to allocate memory");
return 0;
}
v7 = mem2(2LL, total_frequencies, v5);
*(_QWORD *)(v3 + 24) = v7;
if ( !v7 )
break;
++v4;
v3 += 48LL;
if ( total_output_ports < v4 )
return 1;
}
notice("dft.c", 51LL, "setup_dft", "fail to allocate memory");
v1 = 0;
}
}
else
{
notice("dft.c", 40LL, "setup_dft", "fail to get h5 attributes");
}
}
}
else
{
v1 = 0;
notice("dft.c", 36LL, "setup_dft", "fail to get h5 attributes");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // ebx
__int64 v3; // rbx
int v4; // er12
int v5; // ebp
__int64 v6; // rax
__int64 v7; // rax

if ( (unsigned int)h5_get_attr(a1, "DFT", "enable_DFT", &flag_dft) )
{
if ( *(double *)&flag_dft == 0.0 )
{
v1 = 1;
}
else
{
v1 = h5_get_attr(a1, "DFT", "number_of_frequencies", &total_frequencies);
if ( v1 )
{
v3 = (unsigned int)((_DWORD)&output_ports + 64);
v4 = 1;
if ( total_output_ports <= 0 )
{
v1 = 1;
}
else
{
while ( 1 )
{
v5 = *(_DWORD *)v3;
frequencies = h5_load1(a1, "/DFT/frequencies", total_frequencies, 0.0);
if ( !frequencies )
{
notice("dft.c", 47LL, "setup_dft", "fail to load hdf5 dataset");
return 0;
}
v6 = mem2(2LL, total_frequencies, v5);
*(_QWORD *)(v3 + 16) = v6;
if ( !v6 )
{
notice("dft.c", 49LL, "setup_dft", "fail to allocate memory");
return 0;
}
v7 = mem2(2LL, total_frequencies, v5);
*(_QWORD *)(v3 + 24) = v7;
if ( !v7 )
break;
++v4;
v3 += 48LL;
if ( total_output_ports < v4 )
return 1;
}
notice("dft.c", 51LL, "setup_dft", "fail to allocate memory");
v1 = 0;
}
}
else
{
notice("dft.c", 40LL, "setup_dft", "fail to get h5 attributes");
}
}
}
else
{
v1 = 0;
notice("dft.c", 36LL, "setup_dft", "fail to get h5 attributes");
}
return v1;
}

original_code:
int setup_dft(char* file_name)
{
    int port_index;
    int total_points;
    int status;
    status = h5_get_attr(file_name, "DFT", "enable_DFT", &flag_dft);
    if (!(status)) {notice("dft.c",36,__func__,"fail to get h5 attributes"); return(0);};
    if (flag_dft == 0) return 1;
    status = h5_get_attr(file_name, "DFT", "number_of_frequencies", &total_frequencies);
    if (!(status)) {notice("dft.c",40,__func__,"fail to get h5 attributes"); return(0);};
    for (port_index = 1; port_index <= total_output_ports; port_index++)
    {
        total_points = output_ports[port_index].total_points;
        frequencies = (double *)h5_load1(file_name, "/DFT/frequencies", total_frequencies);
        if (!(frequencies)) {notice("dft.c",47,__func__,"fail to load hdf5 dataset"); return(0);};
        output_ports[port_index].dft_real = (double **)mem2(type_double, total_frequencies, total_points);
        if (!(output_ports[port_index].dft_real)) {notice("dft.c",49,__func__,"fail to allocate memory"); return(0);};
        output_ports[port_index].dft_imag = (double **)mem2(type_double, total_frequencies, total_points);
        if (!(output_ports[port_index].dft_imag)) {notice("dft.c",51,__func__,"fail to allocate memory"); return(0);};
    }
    return 1;
}

canonical_original_code:
int setup_dft(char *file_name)
{
    int port_index;
    int total_points;
    int status;
    status = h5_get_attr(file_name, "DFT", "enable_DFT", &flag_dft);
    if (!(status)) {notice("dft.c",36,__func__,"fail to get h5 attributes"); return(0);};
    if (flag_dft == 0) return 1;
    status = h5_get_attr(file_name, "DFT", "number_of_frequencies", &total_frequencies);
    if (!(status)) {notice("dft.c",40,__func__,"fail to get h5 attributes"); return(0);};
    for (port_index = 1; port_index <= total_output_ports; port_index++)
    {
        total_points = output_ports[port_index].total_points;
        frequencies = (double *)h5_load1(file_name, "/DFT/frequencies", total_frequencies);
        if (!(frequencies)) {notice("dft.c",47,__func__,"fail to load hdf5 dataset"); return(0);};
        output_ports[port_index].dft_real = (double **)mem2(type_double, total_frequencies, total_points);
        if (!(output_ports[port_index].dft_real)) {notice("dft.c",49,__func__,"fail to allocate memory"); return(0);};
        output_ports[port_index].dft_imag = (double **)mem2(type_double, total_frequencies, total_points);
        if (!(output_ports[port_index].dft_imag)) {notice("dft.c",51,__func__,"fail to allocate memory"); return(0);};
    }
    return 1;
}

variable_types: status int


original_code:
int update_dft(int time_index)
{
    int port_index;
    int probe_index;
    int total_points;
    int frequency_index;
    double value;
    if (flag_dft == 0) return 1;
    for (port_index = 1; port_index <= total_output_ports; port_index++)
    {
        total_points = output_ports[port_index].total_points;
        for (probe_index = 0; probe_index < total_points; probe_index++)
        {
            value = output_ports[port_index].fields[time_index][probe_index];
            for (frequency_index = 0; frequency_index < total_frequencies; frequency_index ++)
            {
                output_ports[port_index].dft_real[frequency_index][probe_index]
                    = output_ports[port_index].dft_real[frequency_index][probe_index]
                    + value * cos(2.0 * 3.1415926535897932384626434 * frequencies[frequency_index] * time_index * d_t);
                output_ports[port_index].dft_imag[frequency_index][probe_index]
                    = output_ports[port_index].dft_imag[frequency_index][probe_index]
                    - value * sin(2.0 * 3.1415926535897932384626434 * frequencies[frequency_index] * time_index * d_t);
            }
        }
    }
    return 1;
}

variable_types: value double


decompiled_code:
__int64 __fastcall write_dft(__int64 a1)
{
unsigned __int64 v1; // rbp
unsigned int v2; // er12
unsigned int v3; // ebx
char v5[72]; // [rsp+0h] [rbp-48h] BYREF

if ( *(double *)&flag_dft == 0.0 )
return 1;
if ( total_output_ports <= 0 )
return 1;
v1 = (unsigned int)((_DWORD)&output_ports + 64);
v2 = 1;
while ( 1 )
{
sprintf(v5, "port_%02d_spectrum_real", v2);
v3 = h5_write2(a1, v5, 2LL, *(_QWORD *)(v1 + 16), total_frequencies, *(int *)v1);
if ( !v3 )
{
notice("dft.c", 96LL, "write_dft", "fail to write h5 dset");
return v3;
}
sprintf(v5, "port_%02d_spectrum_imag", v2);
v3 = h5_write2(a1, v5, 2LL, *(_QWORD *)(v1 + 24), total_frequencies, *(int *)v1);
if ( !v3 )
break;
++v2;
v1 += 48LL;
if ( total_output_ports < (int)v2 )
return 1;
}
notice("dft.c", 99LL, "write_dft", "fail to write h5 dset");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned __int64 v1; // rbp
unsigned int v2; // er12
unsigned int v3; // ebx
char v5[72]; // [rsp+0h] [rbp-48h] BYREF

if ( *(double *)&flag_dft == 0.0 )
return 1;
if ( total_output_ports <= 0 )
return 1;
v1 = (unsigned int)((_DWORD)&output_ports + 64);
v2 = 1;
while ( 1 )
{
sprintf(v5, "port_%02d_spectrum_real", v2);
v3 = h5_write2(a1, v5, 2LL, *(_QWORD *)(v1 + 16), total_frequencies, *(int *)v1);
if ( !v3 )
{
notice("dft.c", 96LL, "write_dft", "fail to write h5 dset");
return v3;
}
sprintf(v5, "port_%02d_spectrum_imag", v2);
v3 = h5_write2(a1, v5, 2LL, *(_QWORD *)(v1 + 24), total_frequencies, *(int *)v1);
if ( !v3 )
break;
++v2;
v1 += 48LL;
if ( total_output_ports < (int)v2 )
return 1;
}
notice("dft.c", 99LL, "write_dft", "fail to write h5 dset");
return v3;
}

original_code:
int write_dft(char* file_name)
{
    int port_index;
    char attr_name[30];
    int status;
    if (flag_dft == 0) return 1;
    for (port_index = 1; port_index <= total_output_ports; port_index++)
    {
        sprintf(attr_name, "port_%02d_spectrum_real", port_index);
        status = h5_write2(file_name, attr_name, type_double, (void **)output_ports[port_index].dft_real, total_frequencies, output_ports[port_index].total_points);
        if (!(status)) {notice("dft.c",96,__func__,"fail to write h5 dset"); return(0);};
        sprintf(attr_name, "port_%02d_spectrum_imag", port_index);
        status = h5_write2(file_name, attr_name, type_double, (void **)output_ports[port_index].dft_imag, total_frequencies, output_ports[port_index].total_points);
        if (!(status)) {notice("dft.c",99,__func__,"fail to write h5 dset"); return(0);};
    }
    return 1;
}

canonical_original_code:
int write_dft(char *file_name)
{
    int port_index;
    char attr_name[30];
    int status;
    if (flag_dft == 0) return 1;
    for (port_index = 1; port_index <= total_output_ports; port_index++)
    {
        sprintf(attr_name, "port_%02d_spectrum_real", port_index);
        status = h5_write2(file_name, attr_name, type_double, (void **)output_ports[port_index].dft_real, total_frequencies, output_ports[port_index].total_points);
        if (!(status)) {notice("dft.c",96,__func__,"fail to write h5 dset"); return(0);};
        sprintf(attr_name, "port_%02d_spectrum_imag", port_index);
        status = h5_write2(file_name, attr_name, type_double, (void **)output_ports[port_index].dft_imag, total_frequencies, output_ports[port_index].total_points);
        if (!(status)) {notice("dft.c",99,__func__,"fail to write h5 dset"); return(0);};
    }
    return 1;
}


compute

decompiled_code:
__int64 __fastcall compute(unsigned int a1)
{
get_h();
apply_hhards(a1);
get_e();
update_mur();
apply_ehards(a1);
return apply_pmc();
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
get_h();
apply_hhards(a1);
get_e();
update_mur();
apply_ehards(a1);
return apply_pmc();
}

original_code:
static void compute (int time_index)
{
    get_h();
    apply_hhards(time_index);
    get_e();
    update_mur();
    apply_ehards(time_index);
    apply_pmc();
}

canonical_original_code:
static void compute(int time_index)
{
    get_h();
    apply_hhards(time_index);
    get_e();
    update_mur();
    apply_ehards(time_index);
    apply_pmc();
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
const char *v4; // rbx
unsigned int i; // ebp
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( argc > 1 )
{
v3 = argv[1];
v4 = argv[2];
if ( !(unsigned int)h5_set_file(v4, argv, envp) )
{
notice("ezfdtd.c", 104LL, "main", "fail to create h5 output files");
exit(1);
}
if ( !(unsigned int)h5_get_attr(v3, "settings", "number_of_timesteps", &total_timesteps) )
{
notice("ezfdtd.c", 107LL, "main", "fail to get h5 attributes");
exit(1);
}
if ( !(unsigned int)h5_get_attr(v3, "settings", "verbosity", v6) || v6[0] < 0 )
{
notice("ezfdtd.c", 109LL, "main", "fail to get h5 attributes");
exit(1);
}
if ( !(unsigned int)h5_get_attr(v3, "boundaries", "pml_type", &pml_type) )
{
notice("ezfdtd.c", 111LL, "main", "fail to get h5 attributes");
exit(1);
}
if ( !(unsigned int)setup_domain(v3) )
{
notice("ezfdtd.c", 114LL, "main", "fail to setup domain");
exit(1);
}
if ( !(unsigned int)setup_fields(v3) )
{
notice("ezfdtd.c", 118LL, "main", "fail to setup fields");
exit(1);
}
if ( !(unsigned int)setup_mur() )
{
notice("ezfdtd.c", 121LL, "main", "fail to setup abc_mur");
exit(1);
}
if ( (unsigned int)setup_classical(v3) )
{
if ( v6[0] > 0 )
puts("setup classical ... DONE");
if ( classical != 1 && !(unsigned int)setup_ade(v3) )
{
notice("ezfdtd.c", 130LL, "main", "fail to setup ade");
exit(1);
}
if ( pml_type )
{
if ( !(unsigned int)setup_cpml(v3) )
{
notice("ezfdtd.c", 136LL, "main", "fail to setup cpml");
exit(1);
}
if ( v6[0] > 0 )
puts("setup cpml ... DONE");
}
else
{
if ( !(unsigned int)setup_pml(v3) )
{
notice("ezfdtd.c", 143LL, "main", "fail to setup pml");
exit(1);
}
if ( v6[0] > 0 )
puts("setup pml ... DONE");
}
if ( (unsigned int)setup_input_ports(v3) )
{
if ( v6[0] > 0 )
puts("setup excitation ... DONE");
if ( (unsigned int)setup_point_sources(v3) )
{
if ( v6[0] > 0 )
puts("setup point sources ... DONE");
if ( (unsigned int)setup_hards(v3) )
{
if ( v6[0] > 0 )
puts("setup hard sources ... DONE ");
if ( (unsigned int)setup_output_ports(v3) )
{
if ( v6[0] > 0 )
puts("setup probes ... DONE");
if ( (unsigned int)setup_planes(v3, v4) )
{
if ( v6[0] > 0 )
puts("setup animations ... DONE");
if ( (unsigned int)setup_dft(v3) )
{
if ( v6[0] > 0 )
puts("setup DFT ... DONE");
for ( i = 0; total_timesteps > i; ++i )
{
if ( v6[0] > 0 && !((int)i % v6[0]) )
printf("Time Index %06d:\n", i);
excite(i);
compute(i);
update_ports(i);
update_dft(i);
update_planes(v4, i);
}
write_ports(v4);
write_dft(v4);
exit(0);
}
notice("ezfdtd.c", 169LL, "main", "fail to setup dft");
exit(1);
}
notice("ezfdtd.c", 165LL, "main", "fail to setup planes");
exit(1);
}
notice("ezfdtd.c", 160LL, "main", "fail to setup output ports");
exit(1);
}
notice("ezfdtd.c", 156LL, "main", "fail to setup hards");
exit(1);
}
notice("ezfdtd.c", 152LL, "main", "fail to setup point sources");
exit(1);
}
notice("ezfdtd.c", 148LL, "main", "fail to setup input ports");
exit(1);
}
notice("ezfdtd.c", 124LL, "main", "fail to setup classical mode");
exit(1);
}
notice("ezfdtd.c", 99LL, "main", "less than two arguments; exit !");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
const char *v4; // rbx
unsigned int i; // ebp
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( argc > 1 )
{
v3 = argv[1];
v4 = argv[2];
if ( !(unsigned int)h5_set_file(v4, argv, envp) )
{
notice("ezfdtd.c", 104LL, "main", "fail to create h5 output files");
exit(1);
}
if ( !(unsigned int)h5_get_attr(v3, "settings", "number_of_timesteps", &total_timesteps) )
{
notice("ezfdtd.c", 107LL, "main", "fail to get h5 attributes");
exit(1);
}
if ( !(unsigned int)h5_get_attr(v3, "settings", "verbosity", v6) || v6[0] < 0 )
{
notice("ezfdtd.c", 109LL, "main", "fail to get h5 attributes");
exit(1);
}
if ( !(unsigned int)h5_get_attr(v3, "boundaries", "pml_type", &pml_type) )
{
notice("ezfdtd.c", 111LL, "main", "fail to get h5 attributes");
exit(1);
}
if ( !(unsigned int)setup_domain(v3) )
{
notice("ezfdtd.c", 114LL, "main", "fail to setup domain");
exit(1);
}
if ( !(unsigned int)setup_fields(v3) )
{
notice("ezfdtd.c", 118LL, "main", "fail to setup fields");
exit(1);
}
if ( !(unsigned int)setup_mur() )
{
notice("ezfdtd.c", 121LL, "main", "fail to setup abc_mur");
exit(1);
}
if ( (unsigned int)setup_classical(v3) )
{
if ( v6[0] > 0 )
puts("setup classical ... DONE");
if ( classical != 1 && !(unsigned int)setup_ade(v3) )
{
notice("ezfdtd.c", 130LL, "main", "fail to setup ade");
exit(1);
}
if ( pml_type )
{
if ( !(unsigned int)setup_cpml(v3) )
{
notice("ezfdtd.c", 136LL, "main", "fail to setup cpml");
exit(1);
}
if ( v6[0] > 0 )
puts("setup cpml ... DONE");
}
else
{
if ( !(unsigned int)setup_pml(v3) )
{
notice("ezfdtd.c", 143LL, "main", "fail to setup pml");
exit(1);
}
if ( v6[0] > 0 )
puts("setup pml ... DONE");
}
if ( (unsigned int)setup_input_ports(v3) )
{
if ( v6[0] > 0 )
puts("setup excitation ... DONE");
if ( (unsigned int)setup_point_sources(v3) )
{
if ( v6[0] > 0 )
puts("setup point sources ... DONE");
if ( (unsigned int)setup_hards(v3) )
{
if ( v6[0] > 0 )
puts("setup hard sources ... DONE ");
if ( (unsigned int)setup_output_ports(v3) )
{
if ( v6[0] > 0 )
puts("setup probes ... DONE");
if ( (unsigned int)setup_planes(v3, v4) )
{
if ( v6[0] > 0 )
puts("setup animations ... DONE");
if ( (unsigned int)setup_dft(v3) )
{
if ( v6[0] > 0 )
puts("setup DFT ... DONE");
for ( i = 0; total_timesteps > i; ++i )
{
if ( v6[0] > 0 && !((int)i % v6[0]) )
printf("Time Index %06d:\n", i);
excite(i);
func0(i);
update_ports(i);
update_dft(i);
update_planes(v4, i);
}
write_ports(v4);
write_dft(v4);
exit(0);
}
notice("ezfdtd.c", 169LL, "main", "fail to setup dft");
exit(1);
}
notice("ezfdtd.c", 165LL, "main", "fail to setup planes");
exit(1);
}
notice("ezfdtd.c", 160LL, "main", "fail to setup output ports");
exit(1);
}
notice("ezfdtd.c", 156LL, "main", "fail to setup hards");
exit(1);
}
notice("ezfdtd.c", 152LL, "main", "fail to setup point sources");
exit(1);
}
notice("ezfdtd.c", 148LL, "main", "fail to setup input ports");
exit(1);
}
notice("ezfdtd.c", 124LL, "main", "fail to setup classical mode");
exit(1);
}
notice("ezfdtd.c", 99LL, "main", "less than two arguments; exit !");
exit(1);
}

original_code:
int main (int argc, char** argv)
{
    char *input_file_name;
    char *output_file_name;
    int time_index;
    int status;
    int verbosity;
    if (!(argc > 1)) {notice("ezfdtd.c",99,__func__,"less than two arguments; exit !"); exit(1);};
    input_file_name = argv[1];
    output_file_name = argv[2];
    status = h5_set_file(output_file_name);
    if (!(status)) {notice("ezfdtd.c",104,__func__,"fail to create h5 output files"); exit(1);};
    status = h5_get_attr(input_file_name, "settings", "number_of_timesteps", &total_timesteps);
    if (!(status)) {notice("ezfdtd.c",107,__func__,"fail to get h5 attributes"); exit(1);};
    status = h5_get_attr(input_file_name, "settings", "verbosity", &verbosity);
    if (!(status && verbosity >= 0)) {notice("ezfdtd.c",109,__func__,"fail to get h5 attributes"); exit(1);};
    status = h5_get_attr(input_file_name, "boundaries", "pml_type", &pml_type);
    if (!(status)) {notice("ezfdtd.c",111,__func__,"fail to get h5 attributes"); exit(1);};
    status = setup_domain(input_file_name);
    if (!(status)) {notice("ezfdtd.c",114,__func__,"fail to setup domain"); exit(1);};
    status = setup_fields(input_file_name);
    if (!(status)) {notice("ezfdtd.c",118,__func__,"fail to setup fields"); exit(1);};
    status = setup_mur();
    if (!(status)) {notice("ezfdtd.c",121,__func__,"fail to setup abc_mur"); exit(1);};
    status = setup_classical(input_file_name);
    if (!(status)) {notice("ezfdtd.c",124,__func__,"fail to setup classical mode"); exit(1);};
    if (verbosity > 0) printf("setup classical ... DONE\n");
    if (classical != 1)
    {
        status = setup_ade(input_file_name);
        if (!(status)) {notice("ezfdtd.c",130,__func__,"fail to setup ade"); exit(1);};
    }
    if (pml_type != 0)
    {
        status = setup_cpml(input_file_name);
        if (!(status)) {notice("ezfdtd.c",136,__func__,"fail to setup cpml"); exit(1);};
        if (verbosity > 0) printf("setup cpml ... DONE\n");
    }
    else
    {
        status = setup_pml(input_file_name);
        if (!(status)) {notice("ezfdtd.c",143,__func__,"fail to setup pml"); exit(1);};
        if (verbosity > 0) printf("setup pml ... DONE\n");
    }
    status = setup_input_ports(input_file_name);
    if (!(status)) {notice("ezfdtd.c",148,__func__,"fail to setup input ports"); exit(1);};
    if (verbosity > 0) printf("setup excitation ... DONE\n");
    status = setup_point_sources(input_file_name);
    if (!(status)) {notice("ezfdtd.c",152,__func__,"fail to setup point sources"); exit(1);};
    if (verbosity > 0) printf("setup point sources ... DONE\n");
    status = setup_hards(input_file_name);
    if (!(status)) {notice("ezfdtd.c",156,__func__,"fail to setup hards"); exit(1);};
    if (verbosity > 0) printf("setup hard sources ... DONE \n");
    status = setup_output_ports(input_file_name);
    if (!(status)) {notice("ezfdtd.c",160,__func__,"fail to setup output ports"); exit(1);};
    if (verbosity > 0) printf("setup probes ... DONE\n");
    status = setup_planes(input_file_name, output_file_name);
    if (!(status)) {notice("ezfdtd.c",165,__func__,"fail to setup planes"); exit(1);};
    if (verbosity > 0) printf("setup animations ... DONE\n");
    status = setup_dft(input_file_name);
    if (!(status)) {notice("ezfdtd.c",169,__func__,"fail to setup dft"); exit(1);};
    if (verbosity > 0) printf("setup DFT ... DONE\n");
    for (time_index = 0; time_index < total_timesteps; time_index++)
    {
        if (verbosity > 0 && time_index%verbosity == 0) printf("Time Index %06d:\n", time_index);
        excite(time_index);
        compute(time_index);
        update_ports(time_index);
        update_dft(time_index);
        update_planes(output_file_name, time_index);
    }
    write_ports(output_file_name);
    write_dft(output_file_name);
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    char *input_file_name;
    char *output_file_name;
    int time_index;
    int status;
    int verbosity;
    if (!(argc > 1)) {notice("ezfdtd.c",99,__func__,"less than two arguments; exit !"); exit(1);};
    input_file_name = argv[1];
    output_file_name = argv[2];
    status = h5_set_file(output_file_name);
    if (!(status)) {notice("ezfdtd.c",104,__func__,"fail to create h5 output files"); exit(1);};
    status = h5_get_attr(input_file_name, "settings", "number_of_timesteps", &total_timesteps);
    if (!(status)) {notice("ezfdtd.c",107,__func__,"fail to get h5 attributes"); exit(1);};
    status = h5_get_attr(input_file_name, "settings", "verbosity", &verbosity);
    if (!(status && verbosity >= 0)) {notice("ezfdtd.c",109,__func__,"fail to get h5 attributes"); exit(1);};
    status = h5_get_attr(input_file_name, "boundaries", "pml_type", &pml_type);
    if (!(status)) {notice("ezfdtd.c",111,__func__,"fail to get h5 attributes"); exit(1);};
    status = setup_domain(input_file_name);
    if (!(status)) {notice("ezfdtd.c",114,__func__,"fail to setup domain"); exit(1);};
    status = setup_fields(input_file_name);
    if (!(status)) {notice("ezfdtd.c",118,__func__,"fail to setup fields"); exit(1);};
    status = setup_mur();
    if (!(status)) {notice("ezfdtd.c",121,__func__,"fail to setup abc_mur"); exit(1);};
    status = setup_classical(input_file_name);
    if (!(status)) {notice("ezfdtd.c",124,__func__,"fail to setup classical mode"); exit(1);};
    if (verbosity > 0) printf("setup classical ... DONE\n");
    if (classical != 1)
    {
        status = setup_ade(input_file_name);
        if (!(status)) {notice("ezfdtd.c",130,__func__,"fail to setup ade"); exit(1);};
    }
    if (pml_type != 0)
    {
        status = setup_cpml(input_file_name);
        if (!(status)) {notice("ezfdtd.c",136,__func__,"fail to setup cpml"); exit(1);};
        if (verbosity > 0) printf("setup cpml ... DONE\n");
    }
    else
    {
        status = setup_pml(input_file_name);
        if (!(status)) {notice("ezfdtd.c",143,__func__,"fail to setup pml"); exit(1);};
        if (verbosity > 0) printf("setup pml ... DONE\n");
    }
    status = setup_input_ports(input_file_name);
    if (!(status)) {notice("ezfdtd.c",148,__func__,"fail to setup input ports"); exit(1);};
    if (verbosity > 0) printf("setup excitation ... DONE\n");
    status = setup_point_sources(input_file_name);
    if (!(status)) {notice("ezfdtd.c",152,__func__,"fail to setup point sources"); exit(1);};
    if (verbosity > 0) printf("setup point sources ... DONE\n");
    status = setup_hards(input_file_name);
    if (!(status)) {notice("ezfdtd.c",156,__func__,"fail to setup hards"); exit(1);};
    if (verbosity > 0) printf("setup hard sources ... DONE \n");
    status = setup_output_ports(input_file_name);
    if (!(status)) {notice("ezfdtd.c",160,__func__,"fail to setup output ports"); exit(1);};
    if (verbosity > 0) printf("setup probes ... DONE\n");
    status = setup_planes(input_file_name, output_file_name);
    if (!(status)) {notice("ezfdtd.c",165,__func__,"fail to setup planes"); exit(1);};
    if (verbosity > 0) printf("setup animations ... DONE\n");
    status = setup_dft(input_file_name);
    if (!(status)) {notice("ezfdtd.c",169,__func__,"fail to setup dft"); exit(1);};
    if (verbosity > 0) printf("setup DFT ... DONE\n");
    for (time_index = 0; time_index < total_timesteps; time_index++)
    {
        if (verbosity > 0 && time_index%verbosity == 0) printf("Time Index %06d:\n", time_index);
        excite(time_index);
        compute(time_index);
        update_ports(time_index);
        update_dft(time_index);
        update_planes(output_file_name, time_index);
    }
    write_ports(output_file_name);
    write_dft(output_file_name);
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: verbosity int


decompiled_code:
__int64 __fastcall setup_classical(__int64 a1)
{
unsigned int v1; // ebp
__int64 v3; // rbx
int v4; // eax
double v5; // xmm1_8
double v6; // xmm0_8
double v7; // xmm1_8

if ( (unsigned int)h5_get_attr(a1, "materials", "classical_mode", &classical) )
{
v1 = classical;
if ( classical == 1 )
{
v3 = h5_load3(a1, "/materials/sigma", total_x, total_y, total_z);
if ( v3 )
{
ca = mem3(2LL, total_x, total_y, total_z);
if ( mode == 4 || mode == 1 || (cb_x = mem3(2LL, total_x, total_y, total_z)) != 0 )
{
if ( mode == 5 || mode == 2 || (cb_y = mem3(2LL, total_x, total_y, total_z)) != 0 )
{
if ( mode == 6 || mode == 3 || (cb_z = mem3(2LL, total_x, total_y, total_z)) != 0 )
{
for ( z = 0; (unsigned int)z < total_z; ++z )
{
for ( y = 0; (unsigned int)y < total_y; ++y )
{
x = 0;
if ( total_x )
{
v4 = 0;
do
{
v5 = *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * v4) + 8LL * y) + 8LL * z);
v6 = *(double *)(*(_QWORD *)(*(_QWORD *)(v3 + 8LL * v4) + 8LL * y) + 8LL * z) * d_t / (v5 + v5);
v7 = v6 + 1.0;
*(double *)(*(_QWORD *)(*(_QWORD *)(ca + 8LL * v4) + 8LL * y) + 8LL * z) = (1.0 - v6) / (v6 + 1.0);
if ( cb_x )
*(double *)(*(_QWORD *)(*(_QWORD *)(cb_x + 8LL * x) + 8LL * y) + 8LL * z) = d_tx / *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * x) + 8LL * y) + 8LL * z) / v7;
if ( cb_y )
*(double *)(*(_QWORD *)(*(_QWORD *)(cb_y + 8LL * x) + 8LL * y) + 8LL * z) = d_ty / *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * x) + 8LL * y) + 8LL * z) / v7;
if ( cb_z )
*(double *)(*(_QWORD *)(*(_QWORD *)(cb_z + 8LL * x) + 8LL * y) + 8LL * z) = d_tz / *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * x) + 8LL * y) + 8LL * z) / v7;
v4 = x + 1;
x = v4;
}
while ( (unsigned int)v4 < total_x );
}
}
}
free_mem3(v3, total_x, total_y);
}
else
{
notice("classical.c", 57LL, "setup_classical", "fail to allocate memory for cb_z");
v1 = 0;
}
}
else
{
notice("classical.c", 52LL, "setup_classical", "fail to allocate memory for cb_y");
v1 = 0;
}
}
else
{
notice("classical.c", 47LL, "setup_classical", "fail to allocate memory for cb_x");
v1 = 0;
}
}
else
{
notice("classical.c", 41LL, "setup_classical", "fail to load hdf5 dataset");
v1 = 0;
}
}
else
{
v1 = 1;
}
}
else
{
v1 = 0;
notice("classical.c", 37LL, "setup_classical", "fail to get h5 attributes");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // ebp
__int64 v3; // rbx
int v4; // eax
double v5; // xmm1_8
double v6; // xmm0_8
double v7; // xmm1_8

if ( (unsigned int)h5_get_attr(a1, "materials", "classical_mode", &classical) )
{
v1 = classical;
if ( classical == 1 )
{
v3 = h5_load3(a1, "/materials/sigma", total_x, total_y, total_z);
if ( v3 )
{
ca = mem3(2LL, total_x, total_y, total_z);
if ( mode == 4 || mode == 1 || (cb_x = mem3(2LL, total_x, total_y, total_z)) != 0 )
{
if ( mode == 5 || mode == 2 || (cb_y = mem3(2LL, total_x, total_y, total_z)) != 0 )
{
if ( mode == 6 || mode == 3 || (cb_z = mem3(2LL, total_x, total_y, total_z)) != 0 )
{
for ( z = 0; (unsigned int)z < total_z; ++z )
{
for ( y = 0; (unsigned int)y < total_y; ++y )
{
x = 0;
if ( total_x )
{
v4 = 0;
do
{
v5 = *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * v4) + 8LL * y) + 8LL * z);
v6 = *(double *)(*(_QWORD *)(*(_QWORD *)(v3 + 8LL * v4) + 8LL * y) + 8LL * z) * d_t / (v5 + v5);
v7 = v6 + 1.0;
*(double *)(*(_QWORD *)(*(_QWORD *)(ca + 8LL * v4) + 8LL * y) + 8LL * z) = (1.0 - v6) / (v6 + 1.0);
if ( cb_x )
*(double *)(*(_QWORD *)(*(_QWORD *)(cb_x + 8LL * x) + 8LL * y) + 8LL * z) = d_tx / *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * x) + 8LL * y) + 8LL * z) / v7;
if ( cb_y )
*(double *)(*(_QWORD *)(*(_QWORD *)(cb_y + 8LL * x) + 8LL * y) + 8LL * z) = d_ty / *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * x) + 8LL * y) + 8LL * z) / v7;
if ( cb_z )
*(double *)(*(_QWORD *)(*(_QWORD *)(cb_z + 8LL * x) + 8LL * y) + 8LL * z) = d_tz / *(double *)(*(_QWORD *)(*(_QWORD *)(epsilon + 8LL * x) + 8LL * y) + 8LL * z) / v7;
v4 = x + 1;
x = v4;
}
while ( (unsigned int)v4 < total_x );
}
}
}
free_mem3(v3, total_x, total_y);
}
else
{
notice("classical.c", 57LL, "setup_classical", "fail to allocate memory for cb_z");
v1 = 0;
}
}
else
{
notice("classical.c", 52LL, "setup_classical", "fail to allocate memory for cb_y");
v1 = 0;
}
}
else
{
notice("classical.c", 47LL, "setup_classical", "fail to allocate memory for cb_x");
v1 = 0;
}
}
else
{
notice("classical.c", 41LL, "setup_classical", "fail to load hdf5 dataset");
v1 = 0;
}
}
else
{
v1 = 1;
}
}
else
{
v1 = 0;
notice("classical.c", 37LL, "setup_classical", "fail to get h5 attributes");
}
return v1;
}

original_code:
int setup_classical (char *file_name)
{
    double tmp;
    double*** sigma;
    int status;
    status = h5_get_attr(file_name, "materials", "classical_mode", &classical);
    if (!(status)) {notice("classical.c",37,__func__,"fail to get h5 attributes"); return(0);};
    if (classical != 1) return 1;
    sigma = (double ***)h5_load3(file_name, "/materials/sigma", total_x, total_y, total_z);
    if (!(sigma)) {notice("classical.c",41,__func__,"fail to load hdf5 dataset"); return(0);};
    ca = (double ***)mem3(type_double, total_x, total_y, total_z);
    if (mode != mode_tex && mode != mode_tmx)
    {
        cb_x = (double ***)mem3(type_double, total_x, total_y, total_z);
        if (!(cb_x)) {notice("classical.c",47,__func__,"fail to allocate memory for cb_x"); return(0);};
    }
    if (mode != mode_tey && mode != mode_tmy)
    {
        cb_y = (double ***)mem3(type_double, total_x, total_y, total_z);
        if (!(cb_y)) {notice("classical.c",52,__func__,"fail to allocate memory for cb_y"); return(0);};
    }
    if (mode != mode_tez && mode != mode_tmz)
    {
        cb_z = (double ***)mem3(type_double, total_x, total_y, total_z);
        if (!(cb_z)) {notice("classical.c",57,__func__,"fail to allocate memory for cb_z"); return(0);};
    }
    for (z = 0; z < total_z; z++)
        for (y = 0; y < total_y; y++)
            for (x = 0; x < total_x; x++)
            {
                tmp = sigma[x][y][z] * d_t / (2 * epsilon[x][y][z]);
                ca[x][y][z] = (1 - tmp) / (1 + tmp);
                if (cb_x)
                    cb_x[x][y][z] = d_tx / epsilon[x][y][z] / (1 + tmp);
                if (cb_y)
                    cb_y[x][y][z] = d_ty / epsilon[x][y][z] / (1 + tmp);
                if (cb_z)
                    cb_z[x][y][z] = d_tz / epsilon[x][y][z] / (1 + tmp);
            }
    free_mem3((void ***)sigma, total_x, total_y);
    return 1;
}

canonical_original_code:
int setup_classical(char *file_name)
{
    double tmp;
    double ***sigma;
    int status;
    status = h5_get_attr(file_name, "materials", "classical_mode", &classical);
    if (!(status)) {notice("classical.c",37,__func__,"fail to get h5 attributes"); return(0);};
    if (classical != 1) return 1;
    sigma = (double ***)h5_load3(file_name, "/materials/sigma", total_x, total_y, total_z);
    if (!(sigma)) {notice("classical.c",41,__func__,"fail to load hdf5 dataset"); return(0);};
    ca = (double ***)mem3(type_double, total_x, total_y, total_z);
    if (mode != mode_tex && mode != mode_tmx)
    {
        cb_x = (double ***)mem3(type_double, total_x, total_y, total_z);
        if (!(cb_x)) {notice("classical.c",47,__func__,"fail to allocate memory for cb_x"); return(0);};
    }
    if (mode != mode_tey && mode != mode_tmy)
    {
        cb_y = (double ***)mem3(type_double, total_x, total_y, total_z);
        if (!(cb_y)) {notice("classical.c",52,__func__,"fail to allocate memory for cb_y"); return(0);};
    }
    if (mode != mode_tez && mode != mode_tmz)
    {
        cb_z = (double ***)mem3(type_double, total_x, total_y, total_z);
        if (!(cb_z)) {notice("classical.c",57,__func__,"fail to allocate memory for cb_z"); return(0);};
    }
    for (z = 0; z < total_z; z++)
        for (y = 0; y < total_y; y++)
            for (x = 0; x < total_x; x++)
            {
                tmp = sigma[x][y][z] * d_t / (2 * epsilon[x][y][z]);
                ca[x][y][z] = (1 - tmp) / (1 + tmp);
                if (cb_x)
                    cb_x[x][y][z] = d_tx / epsilon[x][y][z] / (1 + tmp);
                if (cb_y)
                    cb_y[x][y][z] = d_ty / epsilon[x][y][z] / (1 + tmp);
                if (cb_z)
                    cb_z[x][y][z] = d_tz / epsilon[x][y][z] / (1 + tmp);
            }
    free_mem3((void ***)sigma, total_x, total_y);
    return 1;
}

variable_types: tmp double

variable_types:
sigma
double ***