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

result = *(_QWORD *)(a1 + 8);
if ( result )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = TZX_InsertBlockAfter(a1, a2, v3);
}
else
{
*(_QWORD *)(a1 + 8) = a2;
}
return result;
}

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

result = *(_QWORD *)(a1 + 8);
if ( result )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = func0(a1, a2, v3);
}
else
{
*(_QWORD *)(a1 + 8) = a2;
}
return result;
}

original_code:
void TZX_AddBlockToEndOfFile(TZX_FILE *pFile, TZX_BLOCK *pBlock)
{
 if (pFile->pFirstBlock==((void *)0))
 {
  pFile->pFirstBlock = pBlock;
 }
 else
 {
  TZX_BLOCK *pCurrentBlock;
  pCurrentBlock = pFile->pFirstBlock;
  while (pCurrentBlock->pNext!=((void *)0))
   pCurrentBlock = pCurrentBlock->pNext;
  TZX_InsertBlockAfter(pFile,pBlock, pCurrentBlock);
 }
}

canonical_original_code:
void TZX_AddBlockToEndOfFile(struct TZX_FILE *pFile, struct TZX_BLOCK *pBlock)
{
 if (pFile->pFirstBlock==((void *)0))
 {
  pFile->pFirstBlock = pBlock;
 }
 else
 {
  struct TZX_BLOCK *pCurrentBlock;
  pCurrentBlock = pFile->pFirstBlock;
  while (pCurrentBlock->pNext!=((void *)0))
   pCurrentBlock = pCurrentBlock->pNext;
  TZX_InsertBlockAfter(pFile,pBlock, pCurrentBlock);
 }
}

variable_types: struct TZX_BLOCK { struct TZX_BLOCK *pPrev; struct TZX_BLOCK *pNext; unsigned char *pBlockHeader; unsigned long DataBlockSize; unsigned char *pBlockData; } *


repo: ralferoo/cpctools

decompiled_code:
// attributes: thunk
void TZX_FreeBlock(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func3(void *ptr)
{
free(ptr);
}

original_code:
void TZX_FreeBlock(TZX_BLOCK *pBlock)
{
 free(pBlock);
}

canonical_original_code:
void TZX_FreeBlock(struct TZX_BLOCK *pBlock)
{
 free(pBlock);
}

return_type: void

user_defined_types: struct TZX_BLOCK { struct TZX_BLOCK *pPrev; struct TZX_BLOCK *pNext; unsigned char *pBlockHeader; unsigned long DataBlockSize; unsigned char *pBlockData; }


decompiled_code:
void __fastcall TZX_FreeFile(void *ptr)
{
__int64 *v1; // rbx
__int64 *v2; // r8
void *v3; // r8

v1 = (__int64 *)*((_QWORD *)ptr + 1);
while ( v1 )
{
v2 = v1;
v1 = (__int64 *)v1[1];
TZX_DetachBlock((__int64)ptr, v2);
TZX_FreeBlock(v3);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
__int64 *v1; // rbx
__int64 *v2; // r8
void *v3; // r8

v1 = (__int64 *)*((_QWORD *)ptr + 1);
while ( v1 )
{
v2 = v1;
v1 = (__int64 *)v1[1];
TZX_DetachBlock((__int64)ptr, v2);
func3(v3);
}
free(ptr);
}

original_code:
void TZX_FreeFile(TZX_FILE *pFile)
{
 TZX_BLOCK *pBlock;
 pBlock = pFile->pFirstBlock;
 while (pBlock!=((void *)0))
 {
  TZX_BLOCK *pNextBlock = pBlock->pNext;
  TZX_DetachBlock(pFile, pBlock);
  TZX_FreeBlock(pBlock);
  pBlock = pNextBlock;
 }
 free(pFile);
}

canonical_original_code:
void TZX_FreeFile(struct TZX_FILE *pFile)
{
 struct TZX_BLOCK *pBlock;
 pBlock = pFile->pFirstBlock;
 while (pBlock!=((void *)0))
 {
  struct TZX_BLOCK *pNextBlock = pBlock->pNext;
  TZX_DetachBlock(pFile, pBlock);
  TZX_FreeBlock(pBlock);
  pBlock = pNextBlock;
 }
 free(pFile);
}

variable_types: struct TZX_FILE { unsigned char VersionMajor; unsigned char VersionMinor; struct TZX_BLOCK { struct TZX_BLOCK *pPrev; struct TZX_BLOCK *pNext; unsigned char *pBlockHeader; unsigned long DataBlockSize; unsigned char *pBlockData; } *pFirstBlock; } *

user_defined_types: struct TZX_FILE { unsigned char VersionMajor; unsigned char VersionMinor; struct TZX_BLOCK *pFirstBlock; }


decompiled_code:
__int64 __fastcall TZX_GetBlockHeaderSize(char a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

v1 = a1 - 16;
result = 0LL;
if ( v1 <= 0x10u )
result = (unsigned int)CSWTCH_9[v1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(char a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

v1 = a1 - 16;
result = 0LL;
if ( v1 <= 0x10u )
result = (unsigned int)CSWTCH_9[v1];
return result;
}

original_code:
int TZX_GetBlockHeaderSize(unsigned char ID)
{
 switch (ID)
 {
  case 0x010:
   return 4+1;
  case 0x011:
   return 18+1;
  case 0x020:
   return 2+1;
  case 0x014:
   return 0x0a + 1;
  case 0x015:
   return 0x08 + 1;
  default:
   break;
 }
 return 0;
}

variable_types:
ID
unsigned char

return_type: int


decompiled_code:
_BOOL8 __fastcall TZX_BlockHasData(unsigned __int8 a1)
{
_BOOL8 result; // rax

if ( a1 <= 0x11u )
result = a1 > 0xFu;
else
result = (unsigned __int8)(a1 - 20) <= 1u;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(unsigned __int8 a1)
{
_BOOL8 result; // rax

if ( a1 <= 0x11u )
result = a1 > 0xFu;
else
result = (unsigned __int8)(a1 - 20) <= 1u;
return result;
}

original_code:
BOOL TZX_BlockHasData(unsigned char ID)
{
 switch (ID)
 {
  case 0x010:
  case 0x011:
  case 0x014:
  case 0x015:
   return (1==1);
  case 0x020:
   return (1==0);
  default:
   break;
 }
 return (1==0);
}

canonical_original_code:
int TZX_BlockHasData(unsigned char ID)
{
 switch (ID)
 {
  case 0x010:
  case 0x011:
  case 0x014:
  case 0x015:
   return (1==1);
  case 0x020:
   return (1==0);
  default:
   break;
 }
 return (1==0);
}


decompiled_code:
__int64 __fastcall TZX_GetBlockDataPtr(__int64 a1)
{
return *(_QWORD *)(a1 + 32);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return *(_QWORD *)(a1 + 32);
}

original_code:
unsigned char *TZX_GetBlockDataPtr(TZX_BLOCK *pBlock)
{
 return pBlock->pBlockData;
}

canonical_original_code:
unsigned char *TZX_GetBlockDataPtr(struct TZX_BLOCK *pBlock)
{
 return pBlock->pBlockData;
}

return_type: unsigned char *


decompiled_code:
__int64 __fastcall TZX_GetBlockHeaderPtr(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
unsigned char *TZX_GetBlockHeaderPtr(TZX_BLOCK *pBlock)
{
 return pBlock->pBlockHeader;
}

canonical_original_code:
unsigned char *TZX_GetBlockHeaderPtr(struct TZX_BLOCK *pBlock)
{
 return pBlock->pBlockHeader;
}


decompiled_code:
__int64 __fastcall TZX_SetupPauseBlock(__int64 a1, __int16 a2)
{
__int64 result; // rax

result = TZX_GetBlockHeaderPtr(a1);
if ( result )
{
if ( *(_BYTE *)result == 32 )
*(_WORD *)(result + 1) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int16 a2)
{
__int64 result; // rax

result = func13(a1);
if ( result )
{
if ( *(_BYTE *)result == 32 )
*(_WORD *)(result + 1) = a2;
}
return result;
}

original_code:
void TZX_SetupPauseBlock(TZX_BLOCK *pBlock,unsigned long PauseInMilliseconds)
{
 unsigned char *pHeader = TZX_GetBlockHeaderPtr(pBlock);
 if (pHeader!=((void *)0))
 {
  if (pHeader[0] == 0x020)
  {
   pHeader[1] = (unsigned char)PauseInMilliseconds;
   pHeader[2] = (unsigned char)(PauseInMilliseconds>>8);
  }
 }
}

canonical_original_code:
void TZX_SetupPauseBlock(struct TZX_BLOCK *pBlock, unsigned long PauseInMilliseconds)
{
 unsigned char *pHeader = TZX_GetBlockHeaderPtr(pBlock);
 if (pHeader!=((void *)0))
 {
  if (pHeader[0] == 0x020)
  {
   pHeader[1] = (unsigned char)PauseInMilliseconds;
   pHeader[2] = (unsigned char)(PauseInMilliseconds>>8);
  }
 }
}

variable_types:
PauseInMilliseconds
unsigned long

variable_types:
pHeader
unsigned char *


decompiled_code:
int __fastcall pws_mywrite(int fd, char *src)
{
int v2; // ebx
int *v4; // rax
char *v5; // rax
struct timespec v6; // [rsp+0h] [rbp-68h] BYREF
char dest[88]; // [rsp+10h] [rbp-58h] BYREF

v2 = strlen(src);
strcpy(dest, src);
if ( write(fd, dest, v2) == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
die("power supply write error: %s", v5);
}
v6.tv_sec = 0LL;
v6.tv_nsec = 20000000LL;
return nanosleep(&v6, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int fd, char *src)
{
int v2; // ebx
int *v4; // rax
char *v5; // rax
struct timespec v6; // [rsp+0h] [rbp-68h] BYREF
char dest[88]; // [rsp+10h] [rbp-58h] BYREF

v2 = strlen(src);
strcpy(dest, src);
if ( write(fd, dest, v2) == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
die("power supply write error: %s", v5);
}
v6.tv_sec = 0LL;
v6.tv_nsec = 20000000LL;
return nanosleep(&v6, 0LL);
}

original_code:
void pws_mywrite(int file, char *cmd) {
 int size = strlen(cmd);
 char str[50];
 sprintf(str, "%s", cmd);
 if(write(file, str, size)==-1)
  die("power supply write error: %s", strerror((*__errno_location ())));
 long pause_ms = 20;
 long pause_seconds = pause_ms/1000;
 pause_ms -= pause_seconds*1000;
 struct timespec pausetime;
 pausetime.tv_sec = pause_seconds;
 pausetime.tv_nsec = pause_ms*1e6;
 nanosleep(&pausetime, ((void *)0));
}

variable_types: file int

variable_types:
cmd
char *

variable_types: size int

variable_types:
str
char [50]

variable_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
int init_pws()
{
__int64 v0; // rbx
int result; // eax
char v2[112]; // [rsp+0h] [rbp-E8h] BYREF
char s[120]; // [rsp+70h] [rbp-78h] BYREF

v0 = 1LL;
gettimeofday(&starttime, 0LL);
while ( 1 )
{
engaged[v0 - 1] = 0;
sprintf(v2, "/dev/usbtmc%d", (unsigned int)v0);
result = open(v2, 2);
if ( result >= 0 )
{
files[v0 - 1] = result;
pws_mywrite(result, "*RST");
pws_mywrite(files[v0 - 1], "SYST:REM");
pws_mywrite(files[v0 - 1], "SOUR:VOLT 20V");
engaged[v0 - 1] = 1;
pws_i_control[v0 - 1] = LC5;
sprintf(s, "SOUR:CURR %fA", *(double *)&LC5);
pws_mywrite(files[v0 - 1], s);
result = pws_mywrite(files[v0 - 1], "OUTPUT 1");
}
else
{
files[v0 - 1] = -1;
}
if ( v0 == 2 )
break;
v0 = 2LL;
}
return result;
}

canonical_decompiled_code:
int func2()
{
__int64 v0; // rbx
int result; // eax
char v2[112]; // [rsp+0h] [rbp-E8h] BYREF
char s[120]; // [rsp+70h] [rbp-78h] BYREF

v0 = 1LL;
gettimeofday(&starttime, 0LL);
while ( 1 )
{
engaged[v0 - 1] = 0;
sprintf(v2, "/dev/usbtmc%d", (unsigned int)v0);
result = open(v2, 2);
if ( result >= 0 )
{
files[v0 - 1] = result;
func0(result, "*RST");
func0(files[v0 - 1], "SYST:REM");
func0(files[v0 - 1], "SOUR:VOLT 20V");
engaged[v0 - 1] = 1;
pws_i_control[v0 - 1] = LC5;
sprintf(s, "SOUR:CURR %fA", *(double *)&LC5);
func0(files[v0 - 1], s);
result = func0(files[v0 - 1], "OUTPUT 1");
}
else
{
files[v0 - 1] = -1;
}
if ( v0 == 2 )
break;
v0 = 2LL;
}
return result;
}

original_code:
void init_pws() {
 int i, h;
 char fname[100];
 gettimeofday(&starttime, 0);
 for (i = 0; i < 2; i++) {
  engaged[i] = 0;
  sprintf(fname, "/dev/usbtmc%d", i+1);
  if ((h = open(fname, 02)) >= 0) {
   files[i] = h;
   char buffer[100];
   pws_mywrite(files[i], "*RST");
   pws_mywrite(files[i], "SYST:REM");
   pws_mywrite(files[i], "SOUR:VOLT 20V");
   int control_enabled = 0;
    control_enabled = 1;
    engaged[i] = 1;
    pws_i_control[i] = 0.5;
   if((options_opt.pws_delay != 0 &&
    options_opt.pws_current[i] != 0) || control_enabled)
   {
    sprintf(buffer,"SOUR:CURR %fA",
     0.5);
    pws_mywrite(files[i], buffer);
    pws_mywrite(files[i], "OUTPUT 1");
   }
   else
   {
    pws_mywrite(files[i], "OUTPUT 0");
   }
  }
  else {
   files[i] = -1;
  }
 }
}

canonical_original_code:
void init_pws() {
 int i; int h;
 char fname[100];
 gettimeofday(&starttime, 0);
 for (i = 0; i < 2; i++) {
  engaged[i] = 0;
  sprintf(fname, "/dev/usbtmc%d", i+1);
  if ((h = open(fname, 02)) >= 0) {
   files[i] = h;
   char buffer[100];
   pws_mywrite(files[i], "*RST");
   pws_mywrite(files[i], "SYST:REM");
   pws_mywrite(files[i], "SOUR:VOLT 20V");
   int control_enabled = 0;
    control_enabled = 1;
    engaged[i] = 1;
    pws_i_control[i] = 0.5;
   if((options_opt.pws_delay != 0 &&
    options_opt.pws_current[i] != 0) || control_enabled)
   {
    sprintf(buffer,"SOUR:CURR %fA",
     0.5);
    pws_mywrite(files[i], buffer);
    pws_mywrite(files[i], "OUTPUT 1");
   }
   else
   {
    pws_mywrite(files[i], "OUTPUT 0");
   }
  }
  else {
   files[i] = -1;
  }
 }
}

variable_types: i int

variable_types: h int

variable_types:
fname
char [100]

variable_types:
buffer
char [100]


decompiled_code:
void __fastcall parse_pws(const char *a1, int a2, __int64 a3)
{
*(double *)(a3 + 8LL * a2) = atof(a1);
}

canonical_decompiled_code:
void __fastcall func3(const char *a1, int a2, __int64 a3)
{
*(double *)(a3 + 8LL * a2) = atof(a1);
}

original_code:
void parse_pws(char *buffer, int ch, double *dataArray) {
 dataArray[ch] = atof(buffer);
}

variable_types:
buffer
char *

variable_types: ch int

variable_types:
dataArray
double *


decompiled_code:
void *__fastcall rw_pws(void *a1)
{
__int64 v1; // rbx
int v2; // eax
int v3; // eax
__int64 v4; // rax
char v6[64]; // [rsp+0h] [rbp-D8h] BYREF
char s[152]; // [rsp+40h] [rbp-98h] BYREF

v1 = *(int *)a1;
if ( states[v1] != 2 )
{
do
{
if ( !engaged[v1] )
goto LABEL_5;
while ( 1 )
{
pws_myread(files[v1], "MEAS:VOLT:DC?", s);
parse_pws(s, v1, (__int64)&curr_pws_v);
pws_myread(files[v1], "MEAS:CURR:DC?", s);
parse_pws(s, v1, (__int64)&curr_pws_i);
sprintf(v6, "SOUR:CURR %fA", pws_i_control[v1]);
pws_mywrite(files[v1], v6);
v2 = states[v1];
if ( v2 )
break;
v3 = engaged[v1];
states[v1] = 1;
if ( !v3 )
{
LABEL_5:
gettimeofday((struct timeval *)&now[2 * v1], 0LL);
v4 = SLODWORD(options_opt[30]);
if ( !(_DWORD)v4 || now[2 * v1] - starttime.tv_sec > v4 )
{
sprintf(s, "SOUR:CURR %fA", options_opt[v1 + 28]);
pws_mywrite(files[v1], s);
pws_mywrite(files[v1], "OUTPUT 1");
engaged[v1] = 1;
}
}
}
}
while ( v2 != 2 );
}
states[v1] = 3;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func4(void *a1)
{
__int64 v1; // rbx
int v2; // eax
int v3; // eax
__int64 v4; // rax
char v6[64]; // [rsp+0h] [rbp-D8h] BYREF
char s[152]; // [rsp+40h] [rbp-98h] BYREF

v1 = *(int *)a1;
if ( states[v1] != 2 )
{
do
{
if ( !engaged[v1] )
goto LABEL_5;
while ( 1 )
{
func1(files[v1], "MEAS:VOLT:DC?", s);
func3(s, v1, (__int64)&curr_pws_v);
func1(files[v1], "MEAS:CURR:DC?", s);
func3(s, v1, (__int64)&curr_pws_i);
sprintf(v6, "SOUR:CURR %fA", pws_i_control[v1]);
func0(files[v1], v6);
v2 = states[v1];
if ( v2 )
break;
v3 = engaged[v1];
states[v1] = 1;
if ( !v3 )
{
LABEL_5:
gettimeofday((struct timeval *)&now[2 * v1], 0LL);
v4 = SLODWORD(options_opt[30]);
if ( !(_DWORD)v4 || now[2 * v1] - starttime.tv_sec > v4 )
{
sprintf(s, "SOUR:CURR %fA", options_opt[v1 + 28]);
func0(files[v1], s);
func0(files[v1], "OUTPUT 1");
engaged[v1] = 1;
}
}
}
}
while ( v2 != 2 );
}
states[v1] = 3;
return 0LL;
}

original_code:
void *rw_pws(void *chan) {
 char buffer[100];
 int ch = *((int *)chan);
 while (states[ch] != HALTED) {
  if(!engaged[ch])
  {
   gettimeofday(&now[ch], 0);
   if(options_opt.pws_delay == 0 ||
    (now[ch].tv_sec-starttime.tv_sec > options_opt.pws_delay))
   {
    sprintf(buffer,"SOUR:CURR %fA",
     options_opt.pws_current[ch]);
    pws_mywrite(files[ch], buffer);
    pws_mywrite(files[ch], "OUTPUT 1");
    engaged[ch] = 1;
   }
  }
  pws_myread(files[ch], "MEAS:VOLT:DC?", buffer);
  parse_pws(buffer, ch, curr_pws_v);
  pws_myread(files[ch], "MEAS:CURR:DC?", buffer);
  parse_pws(buffer, ch, curr_pws_i);
   char iset_cmd[50];
   sprintf(iset_cmd, "SOUR:CURR %fA", pws_i_control[ch]);
   pws_mywrite(files[ch], iset_cmd);
  if (states[ch] == INIT) states[ch] = RUNNING;
 }
 states[ch] = TERMINATED;
 return 0;
}

variable_types:
chan
void *

return_type: void *


decompiled_code:
__int64 start_pws()
{
int v0; // ecx
__int64 result; // rax
int v2; // edx
int arg; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

init_pws();
if ( files[0] >= 0 )
{
arg = 0;
states[0] = 0;
pthread_create(&threads, 0LL, rw_pws, &arg);
}
v0 = files[1];
if ( v0 >= 0 )
{
v4[0] = 1;
states[1] = 0;
pthread_create((pthread_t *)(unsigned int)((_DWORD)&threads + 8), 0LL, rw_pws, v4);
v0 = files[1];
}
result = (unsigned int)states[0];
v2 = states[1];
while ( states[0] )
{
if ( v2 || v0 < 0 )
return result;
}
do
{
while ( files[0] >= 0 )
;
}
while ( !v2 && v0 >= 0 );
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // ecx
__int64 result; // rax
int v2; // edx
int arg; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

func2();
if ( files[0] >= 0 )
{
arg = 0;
states[0] = 0;
pthread_create(&threads, 0LL, rw_pws, &arg);
}
v0 = files[1];
if ( v0 >= 0 )
{
v4[0] = 1;
states[1] = 0;
pthread_create((pthread_t *)(unsigned int)((_DWORD)&threads + 8), 0LL, rw_pws, v4);
v0 = files[1];
}
result = (unsigned int)states[0];
v2 = states[1];
while ( states[0] )
{
if ( v2 || v0 < 0 )
return result;
}
do
{
while ( files[0] >= 0 )
;
}
while ( !v2 && v0 >= 0 );
return result;
}

original_code:
void start_pws() {
 int i;
 int inds[2];
 init_pws();
 for (i = 0; i < 2; i++) {
  if (files[i] >= 0) {
   int exit;
   inds[i] = i;
   states[i] = INIT;
   exit = pthread_create(&threads[i], ((void *)0), rw_pws, (void *) &inds[i]);
  }
 }
 int running = 0;
 while (!running) {
  running = 1;
  for (i = 0; i < 2; i++) {
   if (states[i] == INIT && files[i] >= 0) running = 0;
  }
 }
}

variable_types:
inds
int [2]

variable_types: exit int

variable_types: running int


decompiled_code:
int pws_cleanup()
{
__int64 i; // rbx
int result; // eax

for ( i = 0LL; i != 2; ++i )
{
result = files[i];
if ( result >= 0 )
{
states[i] = 2;
do
sleep(1u);
while ( states[i] != 3 );
pws_mywrite(files[i], "OUTPUT 0");
result = close(files[i]);
}
}
return result;
}

canonical_decompiled_code:
int func6()
{
__int64 i; // rbx
int result; // eax

for ( i = 0LL; i != 2; ++i )
{
result = files[i];
if ( result >= 0 )
{
states[i] = 2;
do
sleep(1u);
while ( states[i] != 3 );
func0(files[i], "OUTPUT 0");
result = close(files[i]);
}
}
return result;
}

original_code:
void pws_cleanup() {
 int i;
 for (i = 0; i < 2; i++) {
  if (files[i] >= 0) {
   int exit;
   states[i] = HALTED;
   while(states[i] != TERMINATED)
   {
    sleep(1);
   }
   pws_mywrite(files[i], "OUTPUT 0");
   close(files[i]);
  }
 }
}


mywrite

decompiled_code:
int __fastcall mywrite(int fd, char *s)
{
int v2; // ebx
struct timespec v4; // [rsp+0h] [rbp-68h] BYREF
char sa[88]; // [rsp+10h] [rbp-58h] BYREF

v2 = strlen(s);
sprintf(sa, "%s\n", s);
write(fd, sa, v2 + 1);
v4.tv_sec = 0LL;
v4.tv_nsec = 100000000LL;
return nanosleep(&v4, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int fd, char *s)
{
int v2; // ebx
struct timespec v4; // [rsp+0h] [rbp-68h] BYREF
char sa[88]; // [rsp+10h] [rbp-58h] BYREF

v2 = strlen(s);
sprintf(sa, "%s\n", s);
write(fd, sa, v2 + 1);
v4.tv_sec = 0LL;
v4.tv_nsec = 100000000LL;
return nanosleep(&v4, 0LL);
}

original_code:
void mywrite(int file, char *cmd) {
 int size = strlen(cmd);
 char str[50];
 sprintf(str, "%s\n", cmd);
 write(file, str, size + 1);
 struct timespec waittime;
 waittime.tv_sec = 0;
 waittime.tv_nsec = 100000000;
 nanosleep(&waittime, ((void *)0));
}

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


myread

decompiled_code:
__int64 __fastcall myread(int fd, char *s, void *buf)
{
int v4; // eax

debug_message("READ1\n");
mywrite(fd, s);
v4 = read(fd, buf, 0x186A0uLL);
if ( v4 < 0 )
*(_BYTE *)buf = 0;
else
*((_BYTE *)buf + v4) = 0;
debug_message("BUFFER: %s\n", (const char *)buf);
return debug_message("READ2\n");
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, char *s, void *buf)
{
int v4; // eax

debug_message("READ1\n");
func0(fd, s);
v4 = read(fd, buf, 0x186A0uLL);
if ( v4 < 0 )
*(_BYTE *)buf = 0;
else
*((_BYTE *)buf + v4) = 0;
debug_message("BUFFER: %s\n", (const char *)buf);
return debug_message("READ2\n");
}

original_code:
void myread(int file, char *cmd, char *buffer) {
 debug_message("READ1\n");
 int len;
 mywrite(file, cmd);
 len = read(file, buffer, 100000);
 if (len >= 0) buffer[len] = 0;
 else buffer[0] = 0;
 debug_message("BUFFER: %s\n", buffer);
 debug_message("READ2\n");
}

variable_types: len int


getcond

decompiled_code:
int __fastcall getcond(int a1, void *a2)
{
myread(a1, "STAT:OPER:COND?", a2);
return atoi((const char *)a2);
}

canonical_decompiled_code:
int __fastcall func2(int a1, void *a2)
{
func1(a1, "STAT:OPER:COND?", a2);
return atoi((const char *)a2);
}

original_code:
int getcond(int file, char *buffer) {
 myread(file, "STAT:OPER:COND?", buffer);
 return atoi(buffer);
}

variable_types: file int


decompiled_code:
__int64 init_power()
{
__int64 v0; // rbx
__int64 v1; // rsi
int v2; // er12
int v3; // ebp
char v5[112]; // [rsp+0h] [rbp-198h] BYREF
char buf[296]; // [rsp+70h] [rbp-128h] BYREF

debug_message("INIT1\n");
gettimeofday(&st, 0LL);
if ( NUM_PWR_CHANNELS <= 0 )
{
v1 = 0LL;
}
else
{
v0 = 0LL;
do
{
sprintf(v5, "/dev/usbtmc%d", (unsigned int)v0);
v2 = v0;
v3 = open(v5, 2);
if ( v3 >= 0 )
{
fprintf(stderr, "- POWER: preparing meter %s\n", v5);
myfile[v0] = v3;
mywrite(v3, "*RST");
if ( v0 )
mywrite(myfile[v0], "CONF:VOLT:AC MIN");
else
mywrite(myfile[0], "CONF:VOLT:DC MIN,DEF");
mywrite(myfile[v0], "TRIG:SOUR IMM");
myread(myfile[v0], "SYST:ERR?", buf);
error_message("- POWER: meter %d initialized with error status %s", (unsigned int)v0, buf);
}
++v0;
v1 = (unsigned int)(v2 + 1);
}
while ( NUM_PWR_CHANNELS > (int)v0 );
}
error_message("- detected %d power meters\n", v1);
return debug_message("INIT2\n");
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbx
__int64 v1; // rsi
int v2; // er12
int v3; // ebp
char v5[112]; // [rsp+0h] [rbp-198h] BYREF
char buf[296]; // [rsp+70h] [rbp-128h] BYREF

debug_message("INIT1\n");
gettimeofday(&st, 0LL);
if ( NUM_PWR_CHANNELS <= 0 )
{
v1 = 0LL;
}
else
{
v0 = 0LL;
do
{
sprintf(v5, "/dev/usbtmc%d", (unsigned int)v0);
v2 = v0;
v3 = open(v5, 2);
if ( v3 >= 0 )
{
fprintf(stderr, "- POWER: preparing meter %s\n", v5);
myfile[v0] = v3;
func0(v3, "*RST");
if ( v0 )
func0(myfile[v0], "CONF:VOLT:AC MIN");
else
func0(myfile[0], "CONF:VOLT:DC MIN,DEF");
func0(myfile[v0], "TRIG:SOUR IMM");
func1(myfile[v0], "SYST:ERR?", buf);
error_message("- POWER: meter %d initialized with error status %s", (unsigned int)v0, buf);
}
++v0;
v1 = (unsigned int)(v2 + 1);
}
while ( NUM_PWR_CHANNELS > (int)v0 );
}
error_message("- detected %d power meters\n", v1);
return debug_message("INIT2\n");
}

original_code:
void init_power() {
 debug_message("INIT1\n");
 int k;
 char fname[100];
 char err[256];
 int h;
 gettimeofday(&st, ((void *)0));
 for(k=0; k < NUM_PWR_CHANNELS; k++) {
  sprintf(fname, "/dev/usbtmc%d", k);
  if ((h = open(fname, 02)) >= 0) {
   fprintf(stderr, "- POWER: preparing meter %s\n", fname);
   myfile[k] = h;
   mywrite(myfile[k], "*RST");
   if (k == 0) mywrite(myfile[k], "CONF:VOLT:DC MIN,DEF");
   else mywrite(myfile[k], "CONF:VOLT:AC MIN");
   mywrite(myfile[k], "TRIG:SOUR IMM");
    myread(myfile[k], "SYST:ERR?", err);
    error_message("- POWER: meter %d initialized with error status %s",
     k, err);
  }
 }
 error_message("- detected %d power meters\n", k);
 debug_message("INIT2\n");
}

variable_types: k int

variable_types:
err
char [256]


decompiled_code:
char *__fastcall parse_power(char *a1, int a2)
{
char *result; // rax
const char *v3; // rdi

result = strtok(a1, ",*\\+");
if ( result )
{
v3 = result;
do
{
curr_pwr[a2] = atof(v3);
result = strtok(0LL, ",*\\+");
v3 = result;
}
while ( result );
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(char *a1, int a2)
{
char *result; // rax
const char *v3; // rdi

result = strtok(a1, ",*\\+");
if ( result )
{
v3 = result;
do
{
curr_pwr[a2] = atof(v3);
result = strtok(0LL, ",*\\+");
v3 = result;
}
while ( result );
}
return result;
}

original_code:
void parse_power(char *buffer, int ch) {
 char *val;
 val = strtok(buffer, ",*\\+");
 while (val != ((void *)0)) {
  curr_pwr[ch] = atof(val);
  val = strtok(((void *)0), ",*\\+");
 }
}

variable_types:
val
char *


decompiled_code:
void __fastcall __noreturn read_power(void *a1)
{
__int64 v1; // rbx
int v2; // eax
struct timeval v3; // [rsp+0h] [rbp-186C8h] BYREF
char buf[100024]; // [rsp+10h] [rbp-186B8h] BYREF

debug_message("THREAD READ 1\n");
v1 = *(int *)a1;
states[v1] = 0;
error_message("- reading channel %d\n", (unsigned int)v1);
if ( states[v1] != 2 )
{
do
{
while ( 1 )
{
gettimeofday(&v3, 0LL);
myread(myfile[v1], "READ?", buf);
parse_power(buf, v1);
v2 = states[v1];
if ( v2 )
break;
states[v1] = 1;
}
}
while ( v2 != 2 );
}
debug_message("THREAD READ 2\n");
states[v1] = 3;
pthread_exit(&loc_2);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(void *a1)
{
__int64 v1; // rbx
int v2; // eax
struct timeval v3; // [rsp+0h] [rbp-186C8h] BYREF
char buf[100024]; // [rsp+10h] [rbp-186B8h] BYREF

debug_message("THREAD READ 1\n");
v1 = *(int *)a1;
states[v1] = 0;
error_message("- reading channel %d\n", (unsigned int)v1);
if ( states[v1] != 2 )
{
do
{
while ( 1 )
{
gettimeofday(&v3, 0LL);
func1(myfile[v1], "READ?", buf);
func4(buf, v1);
v2 = states[v1];
if ( v2 )
break;
states[v1] = 1;
}
}
while ( v2 != 2 );
}
debug_message("THREAD READ 2\n");
states[v1] = 3;
pthread_exit(&loc_2);
}

original_code:
void *read_power(void *chan) {
 debug_message(("THREAD READ 1\n"));
 char buffer[100000];
 int ch = *((int *)chan);
 states[ch] = INIT;
 error_message("- reading channel %d\n", ch);
 while (states[ch] != HALTED) {
  int n;
  struct timeval curr;
      gettimeofday(&curr, ((void *)0));
  myread(myfile[ch], "READ?", buffer);
    parse_power(buffer, ch);
    if (states[ch] == INIT) states[ch] = RUNNING;
 }
 debug_message(("THREAD READ 2\n"));
 states[ch] = TERMINATED;
 pthread_exit((void *)2);
}

variable_types:
buffer
char [100000]

variable_types: n int

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


decompiled_code:
__int64 start_power()
{
void *v0; // rsp
__int64 result; // rax
int v2; // ecx
__int64 *v3; // r12
__int64 v4; // rbx
__int64 *v5; // rcx
unsigned int v6; // eax
__int64 v7; // rsi
int v8; // esi
int v9; // edx
__int64 v10; // [rsp+0h] [rbp-10h] BYREF

v0 = alloca(4LL * NUM_PWR_CHANNELS);
result = init_power();
v2 = NUM_PWR_CHANNELS;
if ( NUM_PWR_CHANNELS > 0 )
{
v3 = &v10;
v4 = 0LL;
do
{
v5 = v3;
*(_DWORD *)v3 = v4;
v3 = (__int64 *)((char *)v3 + 4);
v6 = pthread_create((pthread_t *)&threads[v4], 0LL, (void *(*)(void *))read_power, v5);
v7 = (unsigned int)v4++;
result = error_message("- POWER: created power thread #%d with return code %d\n", v7, v6);
v2 = NUM_PWR_CHANNELS;
}
while ( NUM_PWR_CHANNELS > (int)v4 );
}
v8 = 1;
v9 = 0;
while ( 1 )
{
if ( v9 < v2 )
goto LABEL_6;
if ( v8 )
return result;
LABEL_9:
v8 = 1;
v9 = 0;
if ( v2 <= 0 )
return result;
LABEL_6:
result = v9;
while ( 1 )
{
v9 = result + 1;
if ( states[result] )
break;
++result;
v8 = 0;
if ( v2 <= (int)result )
goto LABEL_9;
}
}
}

canonical_decompiled_code:
__int64 func6()
{
void *v0; // rsp
__int64 result; // rax
int v2; // ecx
__int64 *v3; // r12
__int64 v4; // rbx
__int64 *v5; // rcx
unsigned int v6; // eax
__int64 v7; // rsi
int v8; // esi
int v9; // edx
__int64 v10; // [rsp+0h] [rbp-10h] BYREF

v0 = alloca(4LL * NUM_PWR_CHANNELS);
result = func3();
v2 = NUM_PWR_CHANNELS;
if ( NUM_PWR_CHANNELS > 0 )
{
v3 = &v10;
v4 = 0LL;
do
{
v5 = v3;
*(_DWORD *)v3 = v4;
v3 = (__int64 *)((char *)v3 + 4);
v6 = pthread_create((pthread_t *)&threads[v4], 0LL, (void *(*)(void *))read_power, v5);
v7 = (unsigned int)v4++;
result = error_message("- POWER: created power thread #%d with return code %d\n", v7, v6);
v2 = NUM_PWR_CHANNELS;
}
while ( NUM_PWR_CHANNELS > (int)v4 );
}
v8 = 1;
v9 = 0;
while ( 1 )
{
if ( v9 < v2 )
goto LABEL_6;
if ( v8 )
return result;
LABEL_9:
v8 = 1;
v9 = 0;
if ( v2 <= 0 )
return result;
LABEL_6:
result = v9;
while ( 1 )
{
v9 = result + 1;
if ( states[result] )
break;
++result;
v8 = 0;
if ( v2 <= (int)result )
goto LABEL_9;
}
}
}

original_code:
void start_power() {
 int i;
 int inds[NUM_PWR_CHANNELS];
 init_power();
 for (i = 0; i < NUM_PWR_CHANNELS; i++) {
  int exit;
  inds[i] = i;
  exit = pthread_create(&threads[i], ((void *)0), read_power, (void *) &inds[i]);
  error_message("- POWER: created power thread #%d with return code %d\n",
   i, exit);
 }
 int running = 0;
 while(!running) {
  running = 1;
  for (i = 0; i < NUM_PWR_CHANNELS; i++) {
   if (states[i] == INIT) running = 0;
  }
 }
}

canonical_original_code:
void start_power() {
 int i;
 int inds[-1];
 init_power();
 for (i = 0; i < NUM_PWR_CHANNELS; i++) {
  int exit;
  inds[i] = i;
  exit = pthread_create(&threads[i], ((void *)0), read_power, (void *) &inds[i]);
  error_message("- POWER: created power thread #%d with return code %d\n",
   i, exit);
 }
 int running = 0;
 while(!running) {
  running = 1;
  for (i = 0; i < NUM_PWR_CHANNELS; i++) {
   if (states[i] == INIT) running = 0;
  }
 }
}

variable_types: i int

variable_types:
inds
int [-1]


decompiled_code:
__int64 power_cleanup()
{
__int64 v0; // rbx
pthread_t v1; // rdi
__int64 v2; // rbx
int v3; // edi

v0 = 0LL;
debug_message("CLEAUP1\n");
if ( NUM_PWR_CHANNELS > 0 )
{
do
{
states[v0] = 2;
do
sleep(1u);
while ( states[v0] != 3 );
v1 = threads[v0++];
pthread_detach(v1);
}
while ( NUM_PWR_CHANNELS > (int)v0 );
if ( NUM_PWR_CHANNELS > 0 )
{
v2 = 0LL;
do
{
v3 = myfile[v2++];
close(v3);
}
while ( NUM_PWR_CHANNELS > (int)v2 );
}
}
return debug_message("CLEAUP2\n");
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rbx
pthread_t v1; // rdi
__int64 v2; // rbx
int v3; // edi

v0 = 0LL;
debug_message("CLEAUP1\n");
if ( NUM_PWR_CHANNELS > 0 )
{
do
{
states[v0] = 2;
do
sleep(1u);
while ( states[v0] != 3 );
v1 = threads[v0++];
pthread_detach(v1);
}
while ( NUM_PWR_CHANNELS > (int)v0 );
if ( NUM_PWR_CHANNELS > 0 )
{
v2 = 0LL;
do
{
v3 = myfile[v2++];
close(v3);
}
while ( NUM_PWR_CHANNELS > (int)v2 );
}
}
return debug_message("CLEAUP2\n");
}

original_code:
void power_cleanup() {
 debug_message(("CLEAUP1\n"));
 int i;
 for(i = 0; i < NUM_PWR_CHANNELS; i++) {
  int exit;
  states[i] = HALTED;
  void *retval;
  while(states[i] != TERMINATED)
  {
   sleep(1);
  }
  pthread_detach(threads[i]);
 }
 for(i = 0; i < NUM_PWR_CHANNELS; i++) {
  close(myfile[i]);
 }
 debug_message(("CLEAUP2\n"));
}

variable_types:
retval
void *


decompiled_code:
void delete_options()
{
free((void *)*(&options_opt + 16));
}

canonical_decompiled_code:
void func0()
{
free((void *)*(&options_opt + 16));
}

original_code:
void delete_options()
{
 free(options_opt.affinities);
}


decompiled_code:
__int64 __fastcall options_populate(int argc, char **argv)
{
unsigned int v3; // eax
int v4; // eax
__int64 v5; // rbp
__int64 v6; // r13
int v7; // eax
__int64 v8; // r15
_DWORD *v9; // rax
_DWORD *v10; // r14
__int64 v11; // r14
int v12; // eax
const char *v13; // rdi
double v14; // xmm0_8
const char *v15; // rdi
double v16; // xmm0_8
const char *v17; // rdi
double v18; // xmm0_8
const char *v19; // rdi
double v20; // xmm0_8
const char *v21; // rdi
double v22; // xmm0_8
const char *v23; // rdi
double v24; // xmm0_8
const char *v25; // rdi
double v26; // xmm0_8
const char *v27; // rdi
float v28; // xmm0_4
unsigned __int64 v29; // rax
__int64 v30; // rcx
__int64 v31; // rsi
int v32; // er12
__int64 v33; // rbx
__int64 v34; // rbp
int v35; // eax
const char **v36; // rax
__int64 v37; // rbp
float v38; // ebx
const char *v39; // rdi
const char **v40; // r12
float v41; // xmm0_4
int v42; // eax
__int64 result; // rax
__int64 v44; // rsi
unsigned int v45; // eax
int v46; // [rsp+Ch] [rbp-3Ch]

*(__int64 *)((char *)&options_opt + 92) = 0x100000000LL;
*(&options_opt + 17) = 0x42A000000000000ALL;
*(&options_opt + 18) = 0x3F000000447A0000LL;
*(&options_opt + 19) = 0x3DCCCCCD40800000LL;
*(&options_opt + 5) = 0LL;
options_opt = 0LL;
*(&options_opt + 1) = 0LL;
*(&options_opt + 2) = 0LL;
*((_DWORD *)&options_opt + 6) = 0;
*(&options_opt + 13) = 0LL;
*((_DWORD *)&options_opt + 30) = 0;
*(&options_opt + 16) = 0LL;
*(&options_opt + 20) = 0x40A000003F000000LL;
*((_DWORD *)&options_opt + 42) = 1077936128;
opterr = 0;
while ( 1 )
{
v3 = getopt(argc, argv, "c:f:l:i:n:t:o:e:r:ad");
if ( v3 == -1 )
break;
switch ( v3 )
{
case '?':
v29 = (unsigned int)(optopt - 99);
if ( (unsigned int)v29 <= 0x11 && (v30 = 137797LL, _bittest64(&v30, v29)) )
die("-%c requires an argument\n", (unsigned int)optopt);
else
die("Unrecognized option -%c\n", (unsigned int)optopt);
break;
case 'a':
*((_DWORD *)&options_opt + 6) = 1;
break;
case 'c':
v11 = split_runstring(optarg);
if ( (unsigned int)size_runstring(v11) != 9 )
{
v45 = size_runstring(v11);
die("Invalid number of control parameters. %d supplied.%d required.\n", v45, 9LL);
}
v12 = atoi(*(const char **)v11);
v13 = *(const char **)(v11 + 8);
*((_DWORD *)&options_opt + 34) = v12;
v14 = atof(v13);
v15 = *(const char **)(v11 + 16);
*(float *)&v14 = v14;
*((_DWORD *)&options_opt + 35) = LODWORD(v14);
v16 = atof(v15);
v17 = *(const char **)(v11 + 24);
*(float *)&v16 = v16;
*((_DWORD *)&options_opt + 36) = LODWORD(v16);
v18 = atof(v17);
v19 = *(const char **)(v11 + 32);
*(float *)&v18 = v18;
*((_DWORD *)&options_opt + 37) = LODWORD(v18);
v20 = atof(v19);
v21 = *(const char **)(v11 + 40);
*(float *)&v20 = v20;
*((_DWORD *)&options_opt + 38) = LODWORD(v20);
v22 = atof(v21);
v23 = *(const char **)(v11 + 48);
*(float *)&v22 = v22;
*((_DWORD *)&options_opt + 39) = LODWORD(v22);
v24 = atof(v23);
v25 = *(const char **)(v11 + 56);
*(float *)&v24 = v24;
*((_DWORD *)&options_opt + 40) = LODWORD(v24);
v26 = atof(v25);
v27 = *(const char **)(v11 + 64);
*(float *)&v26 = v26;
*((_DWORD *)&options_opt + 41) = LODWORD(v26);
v28 = atof(v27);
*((float *)&options_opt + 42) = v28;
free_runstring(v11);
break;
case 'd':
*((_DWORD *)&options_opt + 23) = 1;
break;
case 'e':
*(&options_opt + 5) = (__int64)optarg;
break;
case 'f':
v5 = 0LL;
v6 = split_runstring(optarg);
v7 = size_runstring(v6);
v8 = v7;
v46 = v7;
v9 = malloc(4LL * v7);
*(&options_opt + 16) = (__int64)v9;
v10 = v9;
if ( v46 > 0 )
{
do
{
v10[v5] = atoi(*(const char **)(v6 + 8 * v5));
++v5;
}
while ( v8 != v5 );
}
free_runstring(v6);
break;
case 'i':
*(&options_opt + 13) = (__int64)optarg;
break;
case 'l':
*((_DWORD *)&options_opt + 30) = atoi(optarg);
break;
case 'n':
*((_DWORD *)&options_opt + 24) = atoi(optarg);
break;
case 'o':
*(&options_opt + 1) = (__int64)optarg;
break;
case 'r':
*(&options_opt + 2) = (__int64)optarg;
break;
case 't':
v4 = atoi(optarg);
LODWORD(options_opt) = 1;
HIDWORD(options_opt) = v4;
break;
default:
die("Getopt error. Character %d\n", v3);
break;
}
}
if ( optind < argc )
die("Unrecognized non-option arguments\n");
if ( (_DWORD)options_opt && (v31 = HIDWORD(options_opt), (int)v31 <= 0) )
{
die("Specify a positive timeout, not %d [s]\n", v31);
if ( *(&options_opt + 5) )
goto LABEL_28;
}
else if ( *(&options_opt + 5) )
{
goto LABEL_28;
}
die("Please specify a program to execute with -e\n");
LABEL_28:
if ( *((int *)&options_opt + 24) <= 0 )
die("Please specify a positive number of processes to execute\n");
if ( *(&options_opt + 16) )
{
if ( *((_DWORD *)&options_opt + 24) != v46 )
die("The number of affinities must matchthe number of processes\n");
v32 = sysconf(84);
if ( v46 > 0 )
{
v33 = 0LL;
v34 = 4LL * v46;
do
{
while ( 1 )
{
v35 = *(_DWORD *)(*(&options_opt + 16) + v33);
if ( v35 < 0 || v35 >= v32 )
break;
v33 += 4LL;
if ( v34 == v33 )
goto LABEL_39;
}
v33 += 4LL;
die("Affinities must range in [0,# processors)\n");
}
while ( v34 != v33 );
}
}
LABEL_39:
if ( !*(&options_opt + 13) )
{
die("Please specify initial currents for the power supplies\n");
v38 = *(float *)&LC13;
if ( *((int *)&options_opt + 30) >= 0 )
goto LABEL_47;
LABEL_67:
die("Please specify a positive power supply delay [s]\n");
goto LABEL_47;
}
v36 = (const char **)((__int64 (*)(void))split_runstring)();
v37 = 1LL;
v38 = *(float *)&LC13;
v39 = *v36;
v40 = v36;
if ( !*v36 )
goto LABEL_65;
do
{
v41 = atof(v39);
if ( v41 < 0.0 || v41 > 5.0 )
die("Power supply currents must be between 0 and 5 [A]\n");
else
*((float *)&options_opt + v37 + 27) = v41;
v42 = v37++;
v39 = v40[v37 - 1];
}
while ( v39 );
if ( v42 != 2 )
LABEL_65:
die("Please specify currents for each of the %d power supplies\n", 2LL);
free_runstring(v40);
if ( *((int *)&options_opt + 30) < 0 )
goto LABEL_67;
LABEL_47:
result = (unsigned int)options_opt;
if ( (_DWORD)options_opt )
{
result = HIDWORD(options_opt);
if ( *((_DWORD *)&options_opt + 30) > (int)result )
result = die("Power supply delay is greater than timeout time\n");
}
v44 = *((unsigned int *)&options_opt + 34);
if ( (int)v44 <= 0 )
result = die("Control interval should be >= 1, not %d\n", v44);
if ( v38 > *((float *)&options_opt + 37) )
result = die("Control minimum current should be >= 0, not %f\n", *((float *)&options_opt + 37));
if ( v38 > *((float *)&options_opt + 38) )
result = die("Control max current should be >= 0, not %f\n", *((float *)&options_opt + 38));
if ( v38 > *((float *)&options_opt + 39) )
result = die("Frequency step should be >= 0, not %f\n", *((float *)&options_opt + 39));
if ( v38 > *((float *)&options_opt + 40) )
result = die("Current step should be >= 0, not %f\n", *((float *)&options_opt + 40));
if ( v38 > *((float *)&options_opt + 41) )
result = die("T hysteresis should be >= 0, not %f\n", *((float *)&options_opt + 41));
if ( v38 > *((float *)&options_opt + 42) )
result = die("P hysteresis should be >= 0, not %f\n", *((float *)&options_opt + 42));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
unsigned int v3; // eax
int v4; // eax
__int64 v5; // rbp
__int64 v6; // r13
int v7; // eax
__int64 v8; // r15
_DWORD *v9; // rax
_DWORD *v10; // r14
__int64 v11; // r14
int v12; // eax
const char *v13; // rdi
double v14; // xmm0_8
const char *v15; // rdi
double v16; // xmm0_8
const char *v17; // rdi
double v18; // xmm0_8
const char *v19; // rdi
double v20; // xmm0_8
const char *v21; // rdi
double v22; // xmm0_8
const char *v23; // rdi
double v24; // xmm0_8
const char *v25; // rdi
double v26; // xmm0_8
const char *v27; // rdi
float v28; // xmm0_4
unsigned __int64 v29; // rax
__int64 v30; // rcx
__int64 v31; // rsi
int v32; // er12
__int64 v33; // rbx
__int64 v34; // rbp
int v35; // eax
const char **v36; // rax
__int64 v37; // rbp
float v38; // ebx
const char *v39; // rdi
const char **v40; // r12
float v41; // xmm0_4
int v42; // eax
__int64 result; // rax
__int64 v44; // rsi
unsigned int v45; // eax
int v46; // [rsp+Ch] [rbp-3Ch]

*(__int64 *)((char *)&options_opt + 92) = 0x100000000LL;
*(&options_opt + 17) = 0x42A000000000000ALL;
*(&options_opt + 18) = 0x3F000000447A0000LL;
*(&options_opt + 19) = 0x3DCCCCCD40800000LL;
*(&options_opt + 5) = 0LL;
options_opt = 0LL;
*(&options_opt + 1) = 0LL;
*(&options_opt + 2) = 0LL;
*((_DWORD *)&options_opt + 6) = 0;
*(&options_opt + 13) = 0LL;
*((_DWORD *)&options_opt + 30) = 0;
*(&options_opt + 16) = 0LL;
*(&options_opt + 20) = 0x40A000003F000000LL;
*((_DWORD *)&options_opt + 42) = 1077936128;
opterr = 0;
while ( 1 )
{
v3 = getopt(argc, argv, "c:f:l:i:n:t:o:e:r:ad");
if ( v3 == -1 )
break;
switch ( v3 )
{
case '?':
v29 = (unsigned int)(optopt - 99);
if ( (unsigned int)v29 <= 0x11 && (v30 = 137797LL, _bittest64(&v30, v29)) )
die("-%c requires an argument\n", (unsigned int)optopt);
else
die("Unrecognized option -%c\n", (unsigned int)optopt);
break;
case 'a':
*((_DWORD *)&options_opt + 6) = 1;
break;
case 'c':
v11 = split_runstring(optarg);
if ( (unsigned int)size_runstring(v11) != 9 )
{
v45 = size_runstring(v11);
die("Invalid number of control parameters. %d supplied.%d required.\n", v45, 9LL);
}
v12 = atoi(*(const char **)v11);
v13 = *(const char **)(v11 + 8);
*((_DWORD *)&options_opt + 34) = v12;
v14 = atof(v13);
v15 = *(const char **)(v11 + 16);
*(float *)&v14 = v14;
*((_DWORD *)&options_opt + 35) = LODWORD(v14);
v16 = atof(v15);
v17 = *(const char **)(v11 + 24);
*(float *)&v16 = v16;
*((_DWORD *)&options_opt + 36) = LODWORD(v16);
v18 = atof(v17);
v19 = *(const char **)(v11 + 32);
*(float *)&v18 = v18;
*((_DWORD *)&options_opt + 37) = LODWORD(v18);
v20 = atof(v19);
v21 = *(const char **)(v11 + 40);
*(float *)&v20 = v20;
*((_DWORD *)&options_opt + 38) = LODWORD(v20);
v22 = atof(v21);
v23 = *(const char **)(v11 + 48);
*(float *)&v22 = v22;
*((_DWORD *)&options_opt + 39) = LODWORD(v22);
v24 = atof(v23);
v25 = *(const char **)(v11 + 56);
*(float *)&v24 = v24;
*((_DWORD *)&options_opt + 40) = LODWORD(v24);
v26 = atof(v25);
v27 = *(const char **)(v11 + 64);
*(float *)&v26 = v26;
*((_DWORD *)&options_opt + 41) = LODWORD(v26);
v28 = atof(v27);
*((float *)&options_opt + 42) = v28;
free_runstring(v11);
break;
case 'd':
*((_DWORD *)&options_opt + 23) = 1;
break;
case 'e':
*(&options_opt + 5) = (__int64)optarg;
break;
case 'f':
v5 = 0LL;
v6 = split_runstring(optarg);
v7 = size_runstring(v6);
v8 = v7;
v46 = v7;
v9 = malloc(4LL * v7);
*(&options_opt + 16) = (__int64)v9;
v10 = v9;
if ( v46 > 0 )
{
do
{
v10[v5] = atoi(*(const char **)(v6 + 8 * v5));
++v5;
}
while ( v8 != v5 );
}
free_runstring(v6);
break;
case 'i':
*(&options_opt + 13) = (__int64)optarg;
break;
case 'l':
*((_DWORD *)&options_opt + 30) = atoi(optarg);
break;
case 'n':
*((_DWORD *)&options_opt + 24) = atoi(optarg);
break;
case 'o':
*(&options_opt + 1) = (__int64)optarg;
break;
case 'r':
*(&options_opt + 2) = (__int64)optarg;
break;
case 't':
v4 = atoi(optarg);
LODWORD(options_opt) = 1;
HIDWORD(options_opt) = v4;
break;
default:
die("Getopt error. Character %d\n", v3);
break;
}
}
if ( optind < argc )
die("Unrecognized non-option arguments\n");
if ( (_DWORD)options_opt && (v31 = HIDWORD(options_opt), (int)v31 <= 0) )
{
die("Specify a positive timeout, not %d [s]\n", v31);
if ( *(&options_opt + 5) )
goto LABEL_28;
}
else if ( *(&options_opt + 5) )
{
goto LABEL_28;
}
die("Please specify a program to execute with -e\n");
LABEL_28:
if ( *((int *)&options_opt + 24) <= 0 )
die("Please specify a positive number of processes to execute\n");
if ( *(&options_opt + 16) )
{
if ( *((_DWORD *)&options_opt + 24) != v46 )
die("The number of affinities must matchthe number of processes\n");
v32 = sysconf(84);
if ( v46 > 0 )
{
v33 = 0LL;
v34 = 4LL * v46;
do
{
while ( 1 )
{
v35 = *(_DWORD *)(*(&options_opt + 16) + v33);
if ( v35 < 0 || v35 >= v32 )
break;
v33 += 4LL;
if ( v34 == v33 )
goto LABEL_39;
}
v33 += 4LL;
die("Affinities must range in [0,# processors)\n");
}
while ( v34 != v33 );
}
}
LABEL_39:
if ( !*(&options_opt + 13) )
{
die("Please specify initial currents for the power supplies\n");
v38 = *(float *)&LC13;
if ( *((int *)&options_opt + 30) >= 0 )
goto LABEL_47;
LABEL_67:
die("Please specify a positive power supply delay [s]\n");
goto LABEL_47;
}
v36 = (const char **)((__int64 (*)(void))split_runstring)();
v37 = 1LL;
v38 = *(float *)&LC13;
v39 = *v36;
v40 = v36;
if ( !*v36 )
goto LABEL_65;
do
{
v41 = atof(v39);
if ( v41 < 0.0 || v41 > 5.0 )
die("Power supply currents must be between 0 and 5 [A]\n");
else
*((float *)&options_opt + v37 + 27) = v41;
v42 = v37++;
v39 = v40[v37 - 1];
}
while ( v39 );
if ( v42 != 2 )
LABEL_65:
die("Please specify currents for each of the %d power supplies\n", 2LL);
free_runstring(v40);
if ( *((int *)&options_opt + 30) < 0 )
goto LABEL_67;
LABEL_47:
result = (unsigned int)options_opt;
if ( (_DWORD)options_opt )
{
result = HIDWORD(options_opt);
if ( *((_DWORD *)&options_opt + 30) > (int)result )
result = die("Power supply delay is greater than timeout time\n");
}
v44 = *((unsigned int *)&options_opt + 34);
if ( (int)v44 <= 0 )
result = die("Control interval should be >= 1, not %d\n", v44);
if ( v38 > *((float *)&options_opt + 37) )
result = die("Control minimum current should be >= 0, not %f\n", *((float *)&options_opt + 37));
if ( v38 > *((float *)&options_opt + 38) )
result = die("Control max current should be >= 0, not %f\n", *((float *)&options_opt + 38));
if ( v38 > *((float *)&options_opt + 39) )
result = die("Frequency step should be >= 0, not %f\n", *((float *)&options_opt + 39));
if ( v38 > *((float *)&options_opt + 40) )
result = die("Current step should be >= 0, not %f\n", *((float *)&options_opt + 40));
if ( v38 > *((float *)&options_opt + 41) )
result = die("T hysteresis should be >= 0, not %f\n", *((float *)&options_opt + 41));
if ( v38 > *((float *)&options_opt + 42) )
result = die("P hysteresis should be >= 0, not %f\n", *((float *)&options_opt + 42));
return result;
}

original_code:
void options_populate(int argc, char **argv)
{
 options_opt.timeout_sec = 0;
 options_opt.cmd = ((void *)0);
 options_opt.output_file = ((void *)0);
 options_opt.timeout = 0;
 options_opt.error_file = ((void *)0);
 options_opt.append_error = 0;
 options_opt.debug = 0;
 options_opt.num_processes = 1;
 options_opt.pws_current_string = ((void *)0);
 options_opt.pws_delay = 0;
 options_opt.affinities = ((void *)0);
 options_opt.control_params.interval = 10;
 options_opt.control_params.tmax = 80;
 options_opt.control_params.pmax = 1000;
 options_opt.control_params.imin = 0.5;
 options_opt.control_params.imax = 4;
 options_opt.control_params.fstep = 0.1;
 options_opt.control_params.istep = 0.5;
 options_opt.control_params.t_hyst = 5;
 options_opt.control_params.p_hyst = 3;
 opterr = 0;
 int c, i;
 int num_aftys;
 char **aftys;
 char **cont_params;
 while((c = getopt(argc, argv, "c:f:l:i:n:t:o:e:r:ad")) != -1)
 {
  switch(c)
  {
   case 'c':
    cont_params = split_runstring(optarg);
    if(size_runstring(cont_params) != 9)
     die("Invalid number of control parameters. %d supplied."
      "%d required.\n", size_runstring(cont_params),
      9);
    options_opt.control_params.interval = atoi(cont_params[0]);
    options_opt.control_params.tmax = atof(cont_params[1]);
    options_opt.control_params.pmax = atof(cont_params[2]);
    options_opt.control_params.imin = atof(cont_params[3]);
    options_opt.control_params.imax = atof(cont_params[4]);
    options_opt.control_params.fstep = atof(cont_params[5]);
    options_opt.control_params.istep = atof(cont_params[6]);
    options_opt.control_params.t_hyst = atof(cont_params[7]);
    options_opt.control_params.p_hyst = atof(cont_params[8]);
    free_runstring(cont_params);
    break;
   case 'f':
    aftys = split_runstring(optarg);
    num_aftys = size_runstring(aftys);
    options_opt.affinities = (int *)malloc(num_aftys*sizeof(int));
    for(i = 0; i < num_aftys; i++)
    {
     options_opt.affinities[i] = atoi(aftys[i]);
    }
    free_runstring(aftys);
    break;
   case 'l':
    options_opt.pws_delay = atoi(optarg);
    break;
   case 'n':
    options_opt.num_processes = atoi(optarg);
    break;
   case 'd':
    options_opt.debug = 1;
    break;
   case 'a':
    options_opt.append_error = 1;
    break;
   case 'r':
    options_opt.error_file = optarg;
    break;
   case 't':
    options_opt.timeout_sec = atoi(optarg);
    options_opt.timeout = 1;
    break;
   case 'o':
    options_opt.output_file = optarg;
    break;
   case 'e':
    options_opt.cmd = optarg;
    break;
   case 'i':
    options_opt.pws_current_string = optarg;
    break;
   case '?':
    if(optopt == 'e' || optopt == 'o' || optopt == 't' || optopt == 'n' || optopt == 'i' || optopt == 'l' || optopt == 'c')
    {
     die("-%c requires an argument\n", optopt);
    }
    else
    {
     die("Unrecognized option -%c\n", optopt);
    }
    break;
   default:
    die("Getopt error. Character %d\n", c);
  }
 }
 if(optind < argc)
 {
  die("Unrecognized non-option arguments\n");
 }
 if(options_opt.timeout)
 {
  if(options_opt.timeout_sec <= 0)
  {
   die("Specify a positive timeout, not %d [s]\n",
    options_opt.timeout_sec);
  }
 }
 if(options_opt.cmd == ((void *)0))
 {
  die("Please specify a program to execute with -e\n");
 }
 if(options_opt.num_processes < 1)
  die("Please specify a positive number of processes to execute\n");
 if(options_opt.affinities != ((void *)0))
 {
  if(num_aftys != options_opt.num_processes)
  {
   die("The number of affinities must match"
    "the number of processes\n");
  }
  int i;
  int num_procr = (int)sysconf(_SC_NPROCESSORS_ONLN);
  for(i = 0; i < num_aftys; i++)
  {
   if(options_opt.affinities[i] < 0 ||
    options_opt.affinities[i] >= num_procr)
   {
    die("Affinities must range in [0,# processors)\n");
   }
  }
 }
  if(options_opt.pws_current_string != ((void *)0))
  {
   char** values = split_runstring(options_opt.pws_current_string);
   int size = 0;
   while(values[size] != ((void *)0))
   {
    float val = atof(values[size]);
    if(val < 0 || val > 5)
     die("Power supply currents must be between 0 and 5 [A]\n");
    else
     options_opt.pws_current[size] = val;
    size++;
   }
   if(size != 2)
    die("Please specify currents for each of the %d power supplies\n", 2);
   free_runstring(values);
  }
  else
   die("Please specify initial currents for the power supplies\n");
  if(options_opt.pws_delay < 0)
   die("Please specify a positive power supply delay [s]\n");
  if(options_opt.timeout &&
   options_opt.pws_delay > options_opt.timeout_sec)
   die("Power supply delay is greater than timeout time\n");
  if(options_opt.control_params.interval < 1)
   die("Control interval should be >= 1, not %d\n",
    options_opt.control_params.interval);
  if(options_opt.control_params.imin < 0)
   die("Control minimum current should be >= 0, not %f\n",
    options_opt.control_params.imin);
  if(options_opt.control_params.imax < 0)
   die("Control max current should be >= 0, not %f\n",
    options_opt.control_params.imax);
  if(options_opt.control_params.fstep < 0)
   die("Frequency step should be >= 0, not %f\n",
    options_opt.control_params.fstep);
  if(options_opt.control_params.istep < 0)
   die("Current step should be >= 0, not %f\n",
    options_opt.control_params.istep);
  if(options_opt.control_params.t_hyst < 0)
   die("T hysteresis should be >= 0, not %f\n",
    options_opt.control_params.t_hyst);
  if(options_opt.control_params.p_hyst < 0)
   die("P hysteresis should be >= 0, not %f\n",
    options_opt.control_params.p_hyst);
}

canonical_original_code:
void options_populate(int argc, char **argv)
{
 options_opt.timeout_sec = 0;
 options_opt.cmd = ((void *)0);
 options_opt.output_file = ((void *)0);
 options_opt.timeout = 0;
 options_opt.error_file = ((void *)0);
 options_opt.append_error = 0;
 options_opt.debug = 0;
 options_opt.num_processes = 1;
 options_opt.pws_current_string = ((void *)0);
 options_opt.pws_delay = 0;
 options_opt.affinities = ((void *)0);
 options_opt.control_params.interval = 10;
 options_opt.control_params.tmax = 80;
 options_opt.control_params.pmax = 1000;
 options_opt.control_params.imin = 0.5;
 options_opt.control_params.imax = 4;
 options_opt.control_params.fstep = 0.1;
 options_opt.control_params.istep = 0.5;
 options_opt.control_params.t_hyst = 5;
 options_opt.control_params.p_hyst = 3;
 opterr = 0;
 int c; int i;
 int num_aftys;
 char **aftys;
 char **cont_params;
 while((c = getopt(argc, argv, "c:f:l:i:n:t:o:e:r:ad")) != -1)
 {
  switch(c)
  {
   case 'c':
    cont_params = split_runstring(optarg);
    if(size_runstring(cont_params) != 9)
     die("Invalid number of control parameters. %d supplied."
      "%d required.\n", size_runstring(cont_params),
      9);
    options_opt.control_params.interval = atoi(cont_params[0]);
    options_opt.control_params.tmax = atof(cont_params[1]);
    options_opt.control_params.pmax = atof(cont_params[2]);
    options_opt.control_params.imin = atof(cont_params[3]);
    options_opt.control_params.imax = atof(cont_params[4]);
    options_opt.control_params.fstep = atof(cont_params[5]);
    options_opt.control_params.istep = atof(cont_params[6]);
    options_opt.control_params.t_hyst = atof(cont_params[7]);
    options_opt.control_params.p_hyst = atof(cont_params[8]);
    free_runstring(cont_params);
    break;
   case 'f':
    aftys = split_runstring(optarg);
    num_aftys = size_runstring(aftys);
    options_opt.affinities = (int *)malloc(num_aftys*sizeof(int));
    for(i = 0; i < num_aftys; i++)
    {
     options_opt.affinities[i] = atoi(aftys[i]);
    }
    free_runstring(aftys);
    break;
   case 'l':
    options_opt.pws_delay = atoi(optarg);
    break;
   case 'n':
    options_opt.num_processes = atoi(optarg);
    break;
   case 'd':
    options_opt.debug = 1;
    break;
   case 'a':
    options_opt.append_error = 1;
    break;
   case 'r':
    options_opt.error_file = optarg;
    break;
   case 't':
    options_opt.timeout_sec = atoi(optarg);
    options_opt.timeout = 1;
    break;
   case 'o':
    options_opt.output_file = optarg;
    break;
   case 'e':
    options_opt.cmd = optarg;
    break;
   case 'i':
    options_opt.pws_current_string = optarg;
    break;
   case '?':
    if(optopt == 'e' || optopt == 'o' || optopt == 't' || optopt == 'n' || optopt == 'i' || optopt == 'l' || optopt == 'c')
    {
     die("-%c requires an argument\n", optopt);
    }
    else
    {
     die("Unrecognized option -%c\n", optopt);
    }
    break;
   default:
    die("Getopt error. Character %d\n", c);
  }
 }
 if(optind < argc)
 {
  die("Unrecognized non-option arguments\n");
 }
 if(options_opt.timeout)
 {
  if(options_opt.timeout_sec <= 0)
  {
   die("Specify a positive timeout, not %d [s]\n",
    options_opt.timeout_sec);
  }
 }
 if(options_opt.cmd == ((void *)0))
 {
  die("Please specify a program to execute with -e\n");
 }
 if(options_opt.num_processes < 1)
  die("Please specify a positive number of processes to execute\n");
 if(options_opt.affinities != ((void *)0))
 {
  if(num_aftys != options_opt.num_processes)
  {
   die("The number of affinities must match"
    "the number of processes\n");
  }
  int i;
  int num_procr = (int)sysconf(_SC_NPROCESSORS_ONLN);
  for(i = 0; i < num_aftys; i++)
  {
   if(options_opt.affinities[i] < 0 ||
    options_opt.affinities[i] >= num_procr)
   {
    die("Affinities must range in [0,# processors)\n");
   }
  }
 }
  if(options_opt.pws_current_string != ((void *)0))
  {
   char **values = split_runstring(options_opt.pws_current_string);
   int size = 0;
   while(values[size] != ((void *)0))
   {
    float val = atof(values[size]);
    if(val < 0 || val > 5)
     die("Power supply currents must be between 0 and 5 [A]\n");
    else
     options_opt.pws_current[size] = val;
    size++;
   }
   if(size != 2)
    die("Please specify currents for each of the %d power supplies\n", 2);
   free_runstring(values);
  }
  else
   die("Please specify initial currents for the power supplies\n");
  if(options_opt.pws_delay < 0)
   die("Please specify a positive power supply delay [s]\n");
  if(options_opt.timeout &&
   options_opt.pws_delay > options_opt.timeout_sec)
   die("Power supply delay is greater than timeout time\n");
  if(options_opt.control_params.interval < 1)
   die("Control interval should be >= 1, not %d\n",
    options_opt.control_params.interval);
  if(options_opt.control_params.imin < 0)
   die("Control minimum current should be >= 0, not %f\n",
    options_opt.control_params.imin);
  if(options_opt.control_params.imax < 0)
   die("Control max current should be >= 0, not %f\n",
    options_opt.control_params.imax);
  if(options_opt.control_params.fstep < 0)
   die("Frequency step should be >= 0, not %f\n",
    options_opt.control_params.fstep);
  if(options_opt.control_params.istep < 0)
   die("Current step should be >= 0, not %f\n",
    options_opt.control_params.istep);
  if(options_opt.control_params.t_hyst < 0)
   die("T hysteresis should be >= 0, not %f\n",
    options_opt.control_params.t_hyst);
  if(options_opt.control_params.p_hyst < 0)
   die("P hysteresis should be >= 0, not %f\n",
    options_opt.control_params.p_hyst);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int

variable_types:
aftys
char **

variable_types:
values
char **

variable_types: size int

variable_types: val float


die

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

va_start(arg, format);
vfprintf(error_file_handle, format, arg);
fwrite("Program failure\n", 1uLL, 0x10uLL, stderr);
exit(1);
}

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

va_start(arg, format);
vfprintf(error_file_handle, format, arg);
fwrite("Program failure\n", 1uLL, 0x10uLL, stderr);
exit(1);
}

original_code:
void die(const char *format, ...)
{
 va_list argptr;
 __builtin_va_start(argptr,format);
 vfprintf(error_file_handle, format, argptr);
 __builtin_va_end(argptr);
 fprintf(stderr, "Program failure\n");
 exit(1);
}

canonical_original_code:
void die(char *format, ...)
{
 __builtin_va_list argptr;
 __builtin_va_start(argptr,format);
 vfprintf(error_file_handle, format, argptr);
 __builtin_va_end(argptr);
 fprintf(stderr, "Program failure\n");
 exit(1);
}

variable_types:
format
char *


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

va_start(arg, format);
return vfprintf(error_file_handle, format, arg);
}

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

va_start(arg, format);
return vfprintf(error_file_handle, format, arg);
}

original_code:
void error_message(const char *format, ...)
{
 va_list argptr;
 __builtin_va_start(argptr,format);
 vfprintf(error_file_handle, format, argptr);
 __builtin_va_end(argptr);
}

canonical_original_code:
void error_message(char *format, ...)
{
 __builtin_va_list argptr;
 __builtin_va_start(argptr,format);
 vfprintf(error_file_handle, format, argptr);
 __builtin_va_end(argptr);
}


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

result = options_opt[23];
if ( (_DWORD)result )
{
va_start(arg, format);
vfprintf(stderr, format, arg);
result = 1LL;
}
return result;
}

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

result = options_opt[23];
if ( (_DWORD)result )
{
va_start(arg, format);
vfprintf(stderr, format, arg);
result = 1LL;
}
return result;
}

original_code:
int debug_message(const char *format, ...)
{
 if(options_opt.debug)
 {
  va_list argptr;
  __builtin_va_start(argptr,format);
  vfprintf(stderr, format, argptr);
  __builtin_va_end(argptr);
  return 1;
 }
 return 0;
}

canonical_original_code:
int debug_message(char *format, ...)
{
 if(options_opt.debug)
 {
  __builtin_va_list argptr;
  __builtin_va_start(argptr,format);
  vfprintf(stderr, format, argptr);
  __builtin_va_end(argptr);
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall size_runstring(_QWORD *a1)
{
__int64 v1; // rax
unsigned int v2; // er8

if ( !*a1 )
return 0LL;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
__int64 v1; // rax
unsigned int v2; // er8

if ( !*a1 )
return 0LL;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

original_code:
int size_runstring(char **array)
{
 int offset = 0;
 while(array[offset] != ((void *)0))
 {
  offset++;
 }
 return offset;
}

variable_types:
array
char **

variable_types: offset int


decompiled_code:
void __fastcall free_runstring(void *ptr)
{
void *v2; // rdi
void **v3; // rbx

v2 = *(void **)ptr;
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v2 = *v3++;
}
while ( v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
void *v2; // rdi
void **v3; // rbx

v2 = *(void **)ptr;
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v2 = *v3++;
}
while ( v2 );
}
free(ptr);
}

original_code:
void free_runstring(char **array)
{
 int offset = 0;
 while(array[offset] != ((void *)0))
 {
  free(array[offset]);
  offset++;
 }
 free(array);
}


decompiled_code:
int __fastcall print_runstring(const char **a1)
{
const char **v1; // rax
const char *v2; // rdi
const char **v3; // rbx

v1 = a1;
v2 = *a1;
if ( v2 )
{
v3 = v1 + 1;
do
{
LODWORD(v1) = puts(v2);
v2 = *v3++;
}
while ( v2 );
}
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func8(const char **a1)
{
const char **v1; // rax
const char *v2; // rdi
const char **v3; // rbx

v1 = a1;
v2 = *a1;
if ( v2 )
{
v3 = v1 + 1;
do
{
LODWORD(v1) = puts(v2);
v2 = *v3++;
}
while ( v2 );
}
return (int)v1;
}

original_code:
void print_runstring(char **array)
{
 int offset = 0;
 while(array[offset] != ((void *)0))
 {
  printf("%s\n", array[offset]);
  offset++;
 }
}


repo: ramalho/CBofN

decompiled_code:
__int64 __fastcall rawplot_point(int a1, int a2, char a3)
{
__int64 result; // rax

if ( a1 >= 0 && rawplot_width > a1 && a2 >= 0 && rawplot_height >= a2 )
{
result = *(_QWORD *)(rawplot_data + 8LL * a1);
*(_BYTE *)(result + a2) = a3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, char a3)
{
__int64 result; // rax

if ( a1 >= 0 && rawplot_width > a1 && a2 >= 0 && rawplot_height >= a2 )
{
result = *(_QWORD *)(rawplot_data + 8LL * a1);
*(_BYTE *)(result + a2) = a3;
}
return result;
}

original_code:
void rawplot_point(int i, int j, int val)
{
  if(i < 0 || i >= rawplot_width || j < 0 || j > rawplot_height)
    return;
  rawplot_data[i][j] = val;
}

variable_types: j int

variable_types: val int


decompiled_code:
int rawplot_finish()
{
__int64 v0; // rbp
int result; // eax
__int64 i; // rbx
__int64 v3; // rsi
__int64 v4; // rax

if ( rawplot_height > 0 )
{
v0 = 0LL;
do
{
result = rawplot_width;
for ( i = 0LL;
rawplot_width > (int)i;
result = printf("%d %d %d\n", v3, (unsigned int)v0, *(unsigned __int8 *)(v4 + v0)) )
{
v3 = (unsigned int)i;
v4 = *(_QWORD *)(rawplot_data + 8 * i++);
}
++v0;
}
while ( rawplot_height > (int)v0 );
}
return result;
}

canonical_decompiled_code:
int func1()
{
__int64 v0; // rbp
int result; // eax
__int64 i; // rbx
__int64 v3; // rsi
__int64 v4; // rax

if ( rawplot_height > 0 )
{
v0 = 0LL;
do
{
result = rawplot_width;
for ( i = 0LL;
rawplot_width > (int)i;
result = printf("%d %d %d\n", v3, (unsigned int)v0, *(unsigned __int8 *)(v4 + v0)) )
{
v3 = (unsigned int)i;
v4 = *(_QWORD *)(rawplot_data + 8 * i++);
}
++v0;
}
while ( rawplot_height > (int)v0 );
}
return result;
}

original_code:
void rawplot_finish(void)
{
  int i, j;
  for(j = 0; j < rawplot_height; j++)
    for(i = 0; i < rawplot_width; i++)
      printf("%d %d %d\n", i, j, rawplot_data[i][j]);
}

canonical_original_code:
void rawplot_finish()
{
  int i; int j;
  for(j = 0; j < rawplot_height; j++)
    for(i = 0; i < rawplot_width; i++)
      printf("%d %d %d\n", i, j, rawplot_data[i][j]);
}


decompiled_code:
int __fastcall int_to_hexstr(unsigned int a1, char *a2)
{
int result; // eax

result = sprintf(a2, "%2x", a1);
if ( *a2 == 32 )
*a2 = 48;
if ( a2[1] == 32 )
a2[1] = 48;
return result;
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1, char *a2)
{
int result; // eax

result = sprintf(a2, "%2x", a1);
if ( *a2 == 32 )
*a2 = 48;
if ( a2[1] == 32 )
a2[1] = 48;
return result;
}

original_code:
static void int_to_hexstr(int i, char *str)
{
   sprintf(str, "%2x", i);
   for(i = 0; i < 2; i++)
     if(str[i] == ' ') str[i] = '0';
}

variable_types:
str
char *


original_code:
static inline double hue_shift(double x)
{
  return(x - floor(x));
}

variable_types: x double

return_type: double


decompiled_code:
__int64 __fastcall X11plot_init(int a1, int a2, int a3)
{
char *v4; // r14
Display *v5; // rax
__int64 v6; // rbx
unsigned __int64 v7; // rcx
__int64 *v8; // rax
unsigned __int64 v9; // rdx
unsigned __int64 *v10; // rax
__int64 result; // rax
__m128d v12; // xmm0
char *v13; // rax
unsigned int v14; // [rsp+Ch] [rbp-6Ch] BYREF
int v15[2]; // [rsp+10h] [rbp-68h] BYREF
char v16[8]; // [rsp+18h] [rbp-60h] BYREF
char v17[8]; // [rsp+20h] [rbp-58h] BYREF
char v18[8]; // [rsp+28h] [rbp-50h] BYREF
XColor v19; // [rsp+30h] [rbp-48h] BYREF
char s[56]; // [rsp+40h] [rbp-38h] BYREF

v4 = getenv("DISPLAY");
v5 = XOpenDisplay(v4);
x_display = v5;
if ( !v5 )
{
v13 = XDisplayName(v4);
fprintf(stderr, "X11plot_init: could not open display %s.\n", v13);
exit(1);
}
x_screen = XDefaultScreen(v5);
x_depth = XDefaultDepth(x_display, x_screen);
x_whitepixel = XWhitePixel(x_display, x_screen);
x_blackpixel = XBlackPixel(x_display, x_screen);
x_colormap = XDefaultColormap(x_display, x_screen);
if ( x_depth != 1 )
{
v6 = 0LL;
if ( a3 != 2 )
{
while ( 1 )
{
if ( v6 )
{
v12 = 0LL;
v12.m128d_f64[0] = (double)(int)v6 / 255.0;
hsb_to_rgb((int *)&v14, v15, v12);
int_to_hexstr(v14, v16);
int_to_hexstr(v15[0], v17);
int_to_hexstr(v15[1], v18);
sprintf(s, "#%s%s%s", v16, v17, v18);
if ( XParseColor(x_display, x_colormap, s, &v19) )
{
XAllocColor(x_display, x_colormap, &v19);
x_colors[v6] = v19.pixel;
}
else
{
fprintf(stderr, "No such color %s\n", s);
}
if ( v6 == 255 )
goto LABEL_8;
}
else
{
x_colors[0] = x_blackpixel;
}
++v6;
}
}
}
v7 = x_blackpixel;
v8 = x_colors;
do
*v8++ = v7;
while ( &unk_E20 != (_UNKNOWN *)v8 );
v9 = x_whitepixel;
v10 = (unsigned __int64 *)&unk_E20;
do
*v10++ = v9;
while ( v10 != &x_whitepixel );
LABEL_8:
x11plot_levels = a3;
result = open_window(a1 * plot_mag, plot_mag * a2);
x_window = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2, int a3)
{
char *v4; // r14
Display *v5; // rax
__int64 v6; // rbx
unsigned __int64 v7; // rcx
__int64 *v8; // rax
unsigned __int64 v9; // rdx
unsigned __int64 *v10; // rax
__int64 result; // rax
__m128d v12; // xmm0
char *v13; // rax
unsigned int v14; // [rsp+Ch] [rbp-6Ch] BYREF
int v15[2]; // [rsp+10h] [rbp-68h] BYREF
char v16[8]; // [rsp+18h] [rbp-60h] BYREF
char v17[8]; // [rsp+20h] [rbp-58h] BYREF
char v18[8]; // [rsp+28h] [rbp-50h] BYREF
XColor v19; // [rsp+30h] [rbp-48h] BYREF
char s[56]; // [rsp+40h] [rbp-38h] BYREF

v4 = getenv("DISPLAY");
v5 = XOpenDisplay(v4);
x_display = v5;
if ( !v5 )
{
v13 = XDisplayName(v4);
fprintf(stderr, "X11plot_init: could not open display %s.\n", v13);
exit(1);
}
x_screen = XDefaultScreen(v5);
x_depth = XDefaultDepth(x_display, x_screen);
x_whitepixel = XWhitePixel(x_display, x_screen);
x_blackpixel = XBlackPixel(x_display, x_screen);
x_colormap = XDefaultColormap(x_display, x_screen);
if ( x_depth != 1 )
{
v6 = 0LL;
if ( a3 != 2 )
{
while ( 1 )
{
if ( v6 )
{
v12 = 0LL;
v12.m128d_f64[0] = (double)(int)v6 / 255.0;
func3((int *)&v14, v15, v12);
func1(v14, v16);
func1(v15[0], v17);
func1(v15[1], v18);
sprintf(s, "#%s%s%s", v16, v17, v18);
if ( XParseColor(x_display, x_colormap, s, &v19) )
{
XAllocColor(x_display, x_colormap, &v19);
x_colors[v6] = v19.pixel;
}
else
{
fprintf(stderr, "No such color %s\n", s);
}
if ( v6 == 255 )
goto LABEL_8;
}
else
{
x_colors[0] = x_blackpixel;
}
++v6;
}
}
}
v7 = x_blackpixel;
v8 = x_colors;
do
*v8++ = v7;
while ( &unk_E20 != (_UNKNOWN *)v8 );
v9 = x_whitepixel;
v10 = (unsigned __int64 *)&unk_E20;
do
*v10++ = v9;
while ( v10 != &x_whitepixel );
LABEL_8:
x11plot_levels = a3;
result = func5(a1 * plot_mag, plot_mag * a2);
x_window = result;
return result;
}

original_code:
void X11plot_init(int width, int height, int levels)
{
  XColor x_hardwarecolor;
  char hexR[8], hexG[8], hexB[8], name[16], *display;
  int status, i, R, G, B;
  double hue;
  display = getenv("DISPLAY");
  if((x_display = XOpenDisplay(display)) == ((void *)0)) {
    fprintf(stderr, "X11plot_init: could not open display %s.\n",
            XDisplayName(display));
    exit(1);
  }
  x_screen = XDefaultScreen(x_display);
  x_depth = XDefaultDepth(x_display, x_screen);
  x_whitepixel = XWhitePixel(x_display, x_screen);
  x_blackpixel = XBlackPixel(x_display, x_screen);
  x_colormap = XDefaultColormap(x_display, x_screen);
  if(x_depth == 1 || levels == 2) {
    for(i = 0; i < 256 / 2; i++) x_colors[i] = x_blackpixel;
    for(i = 256 / 2; i < 256; i++) x_colors[i] = x_whitepixel;
  }
  else {
    for(i = 0; i < 256; i++) {
      if(i > 0) {
        hue = i / (256 - 1.0);
        hsb_to_rgb(hue, &R, &G, &B);
        int_to_hexstr(R, hexR);
        int_to_hexstr(G, hexG);
        int_to_hexstr(B, hexB);
        sprintf(name,"#%s%s%s", hexR, hexG, hexB);
        status = XParseColor(x_display, x_colormap, name, &x_hardwarecolor);
        if (status != 0) {
          status = XAllocColor(x_display, x_colormap, &x_hardwarecolor);
          x_colors[i] = x_hardwarecolor.pixel;
        }
        else
          fprintf(stderr, "No such color %s\n", name);
      }
      else
        x_colors[i] = x_blackpixel;
    }
  }
  x11plot_width = width;
  x11plot_height = height;
  x11plot_levels = levels;
  x_window = open_window(width * plot_mag, height * plot_mag);
}

canonical_original_code:
void X11plot_init(int width, int height, int levels)
{
  struct XColor x_hardwarecolor;
  char hexR[8]; char hexG[8]; char hexB[8]; char name[16]; char *display;
  int status; int i; int R; int G; int B;
  double hue;
  display = getenv("DISPLAY");
  if((x_display = XOpenDisplay(display)) == ((void *)0)) {
    fprintf(stderr, "X11plot_init: could not open display %s.\n",
            XDisplayName(display));
    exit(1);
  }
  x_screen = XDefaultScreen(x_display);
  x_depth = XDefaultDepth(x_display, x_screen);
  x_whitepixel = XWhitePixel(x_display, x_screen);
  x_blackpixel = XBlackPixel(x_display, x_screen);
  x_colormap = XDefaultColormap(x_display, x_screen);
  if(x_depth == 1 || levels == 2) {
    for(i = 0; i < 256 / 2; i++) x_colors[i] = x_blackpixel;
    for(i = 256 / 2; i < 256; i++) x_colors[i] = x_whitepixel;
  }
  else {
    for(i = 0; i < 256; i++) {
      if(i > 0) {
        hue = i / (256 - 1.0);
        hsb_to_rgb(hue, &R, &G, &B);
        int_to_hexstr(R, hexR);
        int_to_hexstr(G, hexG);
        int_to_hexstr(B, hexB);
        sprintf(name,"#%s%s%s", hexR, hexG, hexB);
        status = XParseColor(x_display, x_colormap, name, &x_hardwarecolor);
        if (status != 0) {
          status = XAllocColor(x_display, x_colormap, &x_hardwarecolor);
          x_colors[i] = x_hardwarecolor.pixel;
        }
        else
          fprintf(stderr, "No such color %s\n", name);
      }
      else
        x_colors[i] = x_blackpixel;
    }
  }
  x11plot_width = width;
  x11plot_height = height;
  x11plot_levels = levels;
  x_window = open_window(width * plot_mag, height * plot_mag);
}

variable_types: width int

variable_types: height int

variable_types: levels int

variable_types: struct XColor { unsigned long pixel; unsigned short red; char flags; char pad; }

variable_types:
hexR
char [8]

variable_types:
hexG
char [8]

variable_types:
hexB
char [8]

variable_types:
name
char [16]

variable_types:
display
char *

variable_types: status int

variable_types: i int

variable_types: R int

variable_types: G int

variable_types: B int

variable_types: hue double


decompiled_code:
int __fastcall X11plot_point(int a1, int a2, int a3)
{
double v3; // xmm0_8
int v4; // ecx
int v5; // ebx
int v6; // er13
int v7; // er8
int result; // eax

v3 = 0.0;
if ( a3 >= 0 )
{
v3 = (double)(x11plot_levels - 1);
if ( a3 < x11plot_levels )
v3 = (double)a3;
}
XSetForeground(x_display, x_gc, x_colors[(int)(v3 / (double)(x11plot_levels - 1) * 255.0 + 0.5)]);
v4 = plot_mag;
if ( plot_mag > 0 )
{
v5 = 0;
do
{
v6 = 0;
do
{
v7 = v4 * a2 + v6++;
XDrawPoint(x_display, x_window, x_gc, v5 + a1 * v4, v7);
v4 = plot_mag;
}
while ( plot_mag > v6 );
++v5;
}
while ( plot_mag > v5 && plot_mag > 0 );
}
result = x11_force_flush;
if ( x11_force_flush )
result = XFlush(x_display);
return result;
}

canonical_decompiled_code:
int __fastcall func7(int a1, int a2, int a3)
{
double v3; // xmm0_8
int v4; // ecx
int v5; // ebx
int v6; // er13
int v7; // er8
int result; // eax

v3 = 0.0;
if ( a3 >= 0 )
{
v3 = (double)(x11plot_levels - 1);
if ( a3 < x11plot_levels )
v3 = (double)a3;
}
XSetForeground(x_display, x_gc, x_colors[(int)(v3 / (double)(x11plot_levels - 1) * 255.0 + 0.5)]);
v4 = plot_mag;
if ( plot_mag > 0 )
{
v5 = 0;
do
{
v6 = 0;
do
{
v7 = v4 * a2 + v6++;
XDrawPoint(x_display, x_window, x_gc, v5 + a1 * v4, v7);
v4 = plot_mag;
}
while ( plot_mag > v6 );
++v5;
}
while ( plot_mag > v5 && plot_mag > 0 );
}
result = x11_force_flush;
if ( x11_force_flush )
result = XFlush(x_display);
return result;
}

original_code:
void X11plot_point(int i, int j, int val)
{
  int cval, ii, jj;
  val = (val < 0) ? 0 : (val >= x11plot_levels) ? x11plot_levels - 1: val;
  cval = ((double)val / (x11plot_levels - 1)) * (256 - 1) + 0.5;
  XSetForeground(x_display, x_gc, x_colors[cval]);
  for(ii = 0; ii < plot_mag; ii++)
    for(jj = 0; jj < plot_mag; jj++)
      XDrawPoint(x_display, x_window, x_gc, i * plot_mag + ii,
                 j * plot_mag + jj);
  if(x11_force_flush) XFlush(x_display);
}

canonical_original_code:
void X11plot_point(int i, int j, int val)
{
  int cval; int ii; int jj;
  val = (val < 0) ? 0 : (val >= x11plot_levels) ? x11plot_levels - 1: val;
  cval = ((double)val / (x11plot_levels - 1)) * (256 - 1) + 0.5;
  XSetForeground(x_display, x_gc, x_colors[cval]);
  for(ii = 0; ii < plot_mag; ii++)
    for(jj = 0; jj < plot_mag; jj++)
      XDrawPoint(x_display, x_window, x_gc, i * plot_mag + ii,
                 j * plot_mag + jj);
  if(x11_force_flush) XFlush(x_display);
}

variable_types: j int

variable_types: val int

variable_types: cval int

variable_types: ii int

variable_types: jj int


decompiled_code:
int __fastcall X11plot_line(int a1, int a2, int a3, int a4, int a5)
{
double v7; // xmm0_8
int v8; // er12
double v9; // xmm2_8
int v10; // ebp
signed int v11; // eax
int v12; // ebx
int result; // eax
int v14; // [rsp-10h] [rbp-78h]
double v15; // [rsp+18h] [rbp-50h]

if ( a5 < 0 )
{
v7 = 0.0;
v8 = 0;
}
else
{
if ( a5 > x11plot_levels )
a5 = x11plot_levels;
v7 = (double)a5;
v8 = a5;
}
XSetForeground(x_display, x_gc, x_colors[(int)(v7 / (double)x11plot_levels * 255.0 + 0.5)]);
if ( plot_mag == 1 )
{
XDrawLine(x_display, x_window, x_gc, a1, a2, a3, a4);
result = v14;
}
else if ( a1 == a3 && a2 == a4 )
{
result = X11plot_point(a1, a2, v8);
}
else
{
v9 = 0.0;
v10 = abs32(a2 - a4);
v11 = abs32(a1 - a3);
v15 = (double)a3;
if ( v10 < v11 )
v10 = v11;
v12 = 0;
do
{
++v12;
result = X11plot_point(
(int)((double)a1 * v9 + (1.0 - v9) * v15 + 0.5),
(int)((double)a2 * v9 + (double)a4 * (1.0 - v9) + 0.5),
v8);
v9 = v9 + 1.0 / (double)v10;
}
while ( v10 >= v12 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(int a1, int a2, int a3, int a4, int a5)
{
double v7; // xmm0_8
int v8; // er12
double v9; // xmm2_8
int v10; // ebp
signed int v11; // eax
int v12; // ebx
int result; // eax
int v14; // [rsp-10h] [rbp-78h]
double v15; // [rsp+18h] [rbp-50h]

if ( a5 < 0 )
{
v7 = 0.0;
v8 = 0;
}
else
{
if ( a5 > x11plot_levels )
a5 = x11plot_levels;
v7 = (double)a5;
v8 = a5;
}
XSetForeground(x_display, x_gc, x_colors[(int)(v7 / (double)x11plot_levels * 255.0 + 0.5)]);
if ( plot_mag == 1 )
{
XDrawLine(x_display, x_window, x_gc, a1, a2, a3, a4);
result = v14;
}
else if ( a1 == a3 && a2 == a4 )
{
result = func7(a1, a2, v8);
}
else
{
v9 = 0.0;
v10 = abs32(a2 - a4);
v11 = abs32(a1 - a3);
v15 = (double)a3;
if ( v10 < v11 )
v10 = v11;
v12 = 0;
do
{
++v12;
result = func7(
(int)((double)a1 * v9 + (1.0 - v9) * v15 + 0.5),
(int)((double)a2 * v9 + (double)a4 * (1.0 - v9) + 0.5),
v8);
v9 = v9 + 1.0 / (double)v10;
}
while ( v10 >= v12 );
}
return result;
}

original_code:
void X11plot_line(int i, int j, int k, int l, int val)
{
  int cval, ii, jj;
  val = (val < 0) ? 0 : (val >= x11plot_levels) ? x11plot_levels : val;
  cval = ((double)val / x11plot_levels) * (256 - 1) + 0.5;
  XSetForeground(x_display, x_gc, x_colors[cval]);
  if(plot_mag == 1)
    XDrawLine(x_display, x_window, x_gc, i, j, k, l);
  else {
    double tx, ty, t, dt;
    int len;
    if(i == k && j == l)
      X11plot_point(i, j, val);
    else {
      len = ((((i - k) >= 0 ? (i - k) : -(i - k))) > (((j - l) >= 0 ? (j - l) : -(j - l))) ? (((i - k) >= 0 ? (i - k) : -(i - k))) : (((j - l) >= 0 ? (j - l) : -(j - l))));
      dt = 1.0 / len;
      for(ii = 0, t = 0.0; ii < len + 1; ii++, t += dt) {
        tx = t * i + (1.0 - t) * k + 0.5;
        ty = t * j + (1.0 - t) * l + 0.5;
        X11plot_point(tx, ty, val);
      }
    }
  }
}

canonical_original_code:
void X11plot_line(int i, int j, int k, int l, int val)
{
  int cval; int ii; int jj;
  val = (val < 0) ? 0 : (val >= x11plot_levels) ? x11plot_levels : val;
  cval = ((double)val / x11plot_levels) * (256 - 1) + 0.5;
  XSetForeground(x_display, x_gc, x_colors[cval]);
  if(plot_mag == 1)
    XDrawLine(x_display, x_window, x_gc, i, j, k, l);
  else {
    double tx; double ty; double t; double dt;
    int len;
    if(i == k && j == l)
      X11plot_point(i, j, val);
    else {
      len = ((((i - k) >= 0 ? (i - k) : -(i - k))) > (((j - l) >= 0 ? (j - l) : -(j - l))) ? (((i - k) >= 0 ? (i - k) : -(i - k))) : (((j - l) >= 0 ? (j - l) : -(j - l))));
      dt = 1.0 / len;
      for(ii = 0, t = 0.0; ii < len + 1; ii++, t += dt) {
        tx = t * i + (1.0 - t) * k + 0.5;
        ty = t * j + (1.0 - t) * l + 0.5;
        X11plot_point(tx, ty, val);
      }
    }
  }
}

variable_types: k int

variable_types: l int

variable_types: tx double

variable_types: ty double

variable_types: t double

variable_types: dt double

variable_types: len int


decompiled_code:
__int64 __fastcall plot_line_internal(__int64 a1, __int64 a2, int a3, int a4, unsigned int a5)
{
__int64 result; // rax
double v7; // xmm2_8
int v8; // ebp
signed int v9; // eax
int v10; // ebx
double v11; // [rsp+18h] [rbp-30h]
double v12; // [rsp+28h] [rbp-20h]

if ( (_DWORD)a1 == a3 && (_DWORD)a2 == a4 )
return plot_point(a1, a2, a5);
v7 = 0.0;
v8 = abs32(a2 - a4);
v9 = abs32(a1 - a3);
v11 = (double)a3;
if ( v8 < v9 )
v8 = v9;
v10 = 0;
v12 = (double)a4;
do
{
++v10;
result = plot_point(
(unsigned int)(int)((double)(int)a1 * v7 + (1.0 - v7) * v11 + 0.5),
(unsigned int)(int)((double)(int)a2 * v7 + v12 * (1.0 - v7) + 0.5),
a5);
v7 = v7 + 1.0 / (double)v8;
}
while ( v8 >= v10 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3, int a4, unsigned int a5)
{
__int64 result; // rax
double v7; // xmm2_8
int v8; // ebp
signed int v9; // eax
int v10; // ebx
double v11; // [rsp+18h] [rbp-30h]
double v12; // [rsp+28h] [rbp-20h]

if ( (_DWORD)a1 == a3 && (_DWORD)a2 == a4 )
return func9(a1, a2, a5);
v7 = 0.0;
v8 = abs32(a2 - a4);
v9 = abs32(a1 - a3);
v11 = (double)a3;
if ( v8 < v9 )
v8 = v9;
v10 = 0;
v12 = (double)a4;
do
{
++v10;
result = func9(
(unsigned int)(int)((double)(int)a1 * v7 + (1.0 - v7) * v11 + 0.5),
(unsigned int)(int)((double)(int)a2 * v7 + v12 * (1.0 - v7) + 0.5),
a5);
v7 = v7 + 1.0 / (double)v8;
}
while ( v8 >= v10 );
return result;
}

original_code:
static void plot_line_internal(int ax, int ay, int bx, int by, int val)
{
  double tx, ty, t, dt;
  int len, i;
  if(ax == bx && ay == by)
    _plot_point(ax, ay, val);
  else {
    len = ((((ax - bx) >= 0 ? (ax - bx) : -(ax - bx))) > (((ay - by) >= 0 ? (ay - by) : -(ay - by))) ? (((ax - bx) >= 0 ? (ax - bx) : -(ax - bx))) : (((ay - by) >= 0 ? (ay - by) : -(ay - by))));
    dt = 1.0 / len;
    for(i = 0, t = 0.0; i < len + 1; i++, t += dt) {
      tx = t * ax + (1.0 - t) * bx + 0.5;
      ty = t * ay + (1.0 - t) * by + 0.5;
      _plot_point(tx, ty, val);
    }
  }
}

canonical_original_code:
static void plot_line_internal(int ax, int ay, int bx, int by, int val)
{
  double tx; double ty; double t; double dt;
  int len; int i;
  if(ax == bx && ay == by)
    _plot_point(ax, ay, val);
  else {
    len = ((((ax - bx) >= 0 ? (ax - bx) : -(ax - bx))) > (((ay - by) >= 0 ? (ay - by) : -(ay - by))) ? (((ax - bx) >= 0 ? (ax - bx) : -(ax - bx))) : (((ay - by) >= 0 ? (ay - by) : -(ay - by))));
    dt = 1.0 / len;
    for(i = 0, t = 0.0; i < len + 1; i++, t += dt) {
      tx = t * ax + (1.0 - t) * bx + 0.5;
      ty = t * ay + (1.0 - t) * by + 0.5;
      _plot_point(tx, ty, val);
    }
  }
}

variable_types: ax int

variable_types: ay int

variable_types: bx int

variable_types: by int

variable_types: i int


decompiled_code:
void none_init()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
static void none_init(int width, int height, int levels)
{
}

variable_types: width int

variable_types: height int

variable_types: levels int


decompiled_code:
void none_line()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
static void none_line(int ax, int ay, int bx, int by, int val)
{
}

variable_types: val int


decompiled_code:
void none_finish()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
static void none_finish(void)
{
}

canonical_original_code:
static void none_finish()
{
}


decompiled_code:
void none_point()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
static void none_point(int i, int j, int val)
{
}

variable_types: j int


original_code:
void plot_init(int width, int height, int levels, char *term)
{
  if(!term) term = term_default;
  if(0) ;
  else if(strcmp(term, "x11") == 0) {
    _plot_init = x11plot_init;
    _plot_point = x11plot_point;
    _plot_line = x11plot_line;
    _plot_finish = x11plot_finish;
  }
  else if(strcmp(term, "X11") == 0) {
    _plot_init = X11plot_init;
    _plot_point = X11plot_point;
    _plot_line = X11plot_line;
    _plot_finish = X11plot_finish;
  }
  else if(strcmp(term, "ps") == 0) {
    _plot_init = psplot_init;
    _plot_point = psplot_point;
    _plot_line = psplot_line;
    _plot_finish = psplot_finish;
  }
  else if(strcmp(term, "pgm") == 0) {
    _plot_init = pgmplot_init;
    _plot_point = pgmplot_point;
    _plot_line = plot_line_internal;
    _plot_finish = pgmplot_finish;
  }
  else if(strcmp(term, "raw") == 0) {
    _plot_init = rawplot_init;
    _plot_point = rawplot_point;
    _plot_line = plot_line_internal;
    _plot_finish = rawplot_finish;
  }
  else if(strcmp(term, "none") == 0) {
    _plot_init = none_init;
    _plot_point = none_point;
    _plot_line = none_line;
    _plot_finish = none_finish;
  }
  else {
    plot_init(width, height, levels, term_default);
    return;
  }
  _plot_init(width, height, levels);
  plot_levels = levels;
  plot_width = width;
  plot_height = height;
  plot_xmin = 0.0;
  plot_xmax = width - 1;
  plot_ymin = height - 1;
  plot_ymax = 0.0;
}

variable_types:
term
char *


decompiled_code:
void __fastcall plot_set_range(double a1, double a2, double a3, double a4)
{
plot_xmin = *(_QWORD *)&a1;
plot_xmax = *(_QWORD *)&a2;
plot_ymin = *(_QWORD *)&a3;
plot_ymax = *(_QWORD *)&a4;
}

canonical_decompiled_code:
void __fastcall func7(double a1, double a2, double a3, double a4)
{
plot_xmin = *(_QWORD *)&a1;
plot_xmax = *(_QWORD *)&a2;
plot_ymin = *(_QWORD *)&a3;
plot_ymax = *(_QWORD *)&a4;
}

original_code:
void plot_set_range(double xmin, double xmax, double ymin, double ymax)
{
  plot_xmin = xmin;
  plot_xmax = xmax;
  plot_ymin = ymin;
  plot_ymax = ymax;
}

variable_types: xmin double

variable_types: xmax double

variable_types: ymin double

variable_types: ymax double


decompiled_code:
__int64 __fastcall plot_set_all(unsigned int a1)
{
unsigned int v2; // ebx
__int64 v3; // r8
__int64 v4; // rdi
__int64 v5; // rdx
__int64 result; // rax

if ( plot_width > 0 )
{
v2 = 0;
do
{
v3 = a1;
if ( plot_inverse )
v3 = plot_levels + ~a1;
v4 = v2;
v5 = v2++;
result = plot_line(v4, 0LL, v5, (unsigned int)(plot_height - 1), v3);
}
while ( plot_width > (int)v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1)
{
unsigned int v2; // ebx
__int64 v3; // r8
__int64 v4; // rdi
__int64 v5; // rdx
__int64 result; // rax

if ( plot_width > 0 )
{
v2 = 0;
do
{
v3 = a1;
if ( plot_inverse )
v3 = plot_levels + ~a1;
v4 = v2;
v5 = v2++;
result = func10(v4, 0LL, v5, (unsigned int)(plot_height - 1), v3);
}
while ( plot_width > (int)v2 );
}
return result;
}

original_code:
void plot_set_all(int val)
{
  int i;
  for(i = 0; i < plot_width; i++)
    _plot_line(i, 0, i, plot_height - 1, (plot_inverse ? ((plot_levels - 1) - (val)) : (val)));
}


decompiled_code:
__int64 __fastcall plot_point(unsigned int a1, double a2, double a3)
{
int v4; // edi
double v5; // xmm0_8
__int64 v6; // rdi
__int64 v7; // rsi
__int64 result; // rax

if ( *(double *)&plot_xmax == *(double *)&plot_xmin )
v4 = (int)*(double *)&plot_xmin;
else
v4 = (int)((a2 - *(double *)&plot_xmin) / (*(double *)&plot_xmax - *(double *)&plot_xmin) * (double)plot_width);
v5 = *(double *)&plot_ymin;
v6 = v4 - (unsigned int)(v4 == plot_width);
if ( *(double *)&plot_ymax != *(double *)&plot_ymin )
v5 = ((*(double *)&plot_ymin - a3) / (*(double *)&plot_ymax - *(double *)&plot_ymin) + 1.0) * (double)plot_height;
v7 = (int)v5 - (unsigned int)((int)v5 == plot_height);
result = (int)v7 >= 0;
if ( ((unsigned __int8)result & ((int)v6 >= 0 && (int)v6 < plot_width)) != 0 && (int)v7 < plot_height )
{
if ( plot_inverse )
a1 = plot_levels + ~a1;
result = plot_point(v6, v7, a1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, double a2, double a3)
{
int v4; // edi
double v5; // xmm0_8
__int64 v6; // rdi
__int64 v7; // rsi
__int64 result; // rax

if ( *(double *)&plot_xmax == *(double *)&plot_xmin )
v4 = (int)*(double *)&plot_xmin;
else
v4 = (int)((a2 - *(double *)&plot_xmin) / (*(double *)&plot_xmax - *(double *)&plot_xmin) * (double)plot_width);
v5 = *(double *)&plot_ymin;
v6 = v4 - (unsigned int)(v4 == plot_width);
if ( *(double *)&plot_ymax != *(double *)&plot_ymin )
v5 = ((*(double *)&plot_ymin - a3) / (*(double *)&plot_ymax - *(double *)&plot_ymin) + 1.0) * (double)plot_height;
v7 = (int)v5 - (unsigned int)((int)v5 == plot_height);
result = (int)v7 >= 0;
if ( ((unsigned __int8)result & ((int)v6 >= 0 && (int)v6 < plot_width)) != 0 && (int)v7 < plot_height )
{
if ( plot_inverse )
a1 = plot_levels + ~a1;
result = func9(v6, v7, a1);
}
return result;
}

original_code:
void plot_point(double x, double y, int val)
{
  int xi, yi;
  xi = ((int) (plot_xmax == plot_xmin) ? plot_xmin : ((((x) - plot_xmin) / (plot_xmax - plot_xmin)) * plot_width)); xi = (((xi) == plot_width) ? ((xi) - 1) : (xi));
  yi = ((int) (plot_ymax == plot_ymin) ? plot_ymin : (((plot_ymin - (y)) / (plot_ymax - plot_ymin) + 1.0) * plot_height)); yi = (((yi) == plot_height) ? ((yi) - 1) : (yi));
  if(!(xi < 0 || xi >= plot_width || yi < 0 || yi >= plot_height))
    _plot_point(xi, yi, (plot_inverse ? ((plot_levels - 1) - (val)) : (val)));
}

canonical_original_code:
void plot_point(double x, double y, int val)
{
  int xi; int yi;
  xi = ((int) (plot_xmax == plot_xmin) ? plot_xmin : ((((x) - plot_xmin) / (plot_xmax - plot_xmin)) * plot_width)); xi = (((xi) == plot_width) ? ((xi) - 1) : (xi));
  yi = ((int) (plot_ymax == plot_ymin) ? plot_ymin : (((plot_ymin - (y)) / (plot_ymax - plot_ymin) + 1.0) * plot_height)); yi = (((yi) == plot_height) ? ((yi) - 1) : (yi));
  if(!(xi < 0 || xi >= plot_width || yi < 0 || yi >= plot_height))
    _plot_point(xi, yi, (plot_inverse ? ((plot_levels - 1) - (val)) : (val)));
}

variable_types: x double

variable_types: y double

variable_types: xi int

variable_types: yi int


decompiled_code:
__int64 __fastcall plot_line(unsigned int a1, double a2, double a3, double a4, double a5)
{
__int64 v5; // r8
int v6; // edi
double v7; // xmm0_8
__int64 v8; // rdi
int v9; // esi
__int64 v10; // rsi
int v11; // edx
__int64 v12; // rdx

v5 = a1;
if ( *(double *)&plot_xmax == *(double *)&plot_xmin )
v6 = (int)*(double *)&plot_xmin;
else
v6 = (int)((a2 - *(double *)&plot_xmin) / (*(double *)&plot_xmax - *(double *)&plot_xmin) * (double)plot_width);
v7 = *(double *)&plot_ymin;
v8 = v6 - (unsigned int)(v6 == plot_width);
if ( *(double *)&plot_ymax == *(double *)&plot_ymin )
v9 = (int)*(double *)&plot_ymin;
else
v9 = (int)(((*(double *)&plot_ymin - a3) / (*(double *)&plot_ymax - *(double *)&plot_ymin) + 1.0)
* (double)plot_height);
v10 = v9 - (unsigned int)(v9 == plot_height);
if ( *(double *)&plot_xmax == *(double *)&plot_xmin )
v11 = (int)*(double *)&plot_xmin;
else
v11 = (int)((a4 - *(double *)&plot_xmin) / (*(double *)&plot_xmax - *(double *)&plot_xmin) * (double)plot_width);
v12 = v11 - (unsigned int)(v11 == plot_width);
if ( *(double *)&plot_ymax != *(double *)&plot_ymin )
v7 = ((*(double *)&plot_ymin - a5) / (*(double *)&plot_ymax - *(double *)&plot_ymin) + 1.0) * (double)plot_height;
if ( plot_inverse )
v5 = (unsigned int)(plot_levels + ~(_DWORD)v5);
return plot_line(v8, v10, v12, (int)v7 - (unsigned int)((int)v7 == plot_height), v5);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, double a2, double a3, double a4, double a5)
{
__int64 v5; // r8
int v6; // edi
double v7; // xmm0_8
__int64 v8; // rdi
int v9; // esi
__int64 v10; // rsi
int v11; // edx
__int64 v12; // rdx

v5 = a1;
if ( *(double *)&plot_xmax == *(double *)&plot_xmin )
v6 = (int)*(double *)&plot_xmin;
else
v6 = (int)((a2 - *(double *)&plot_xmin) / (*(double *)&plot_xmax - *(double *)&plot_xmin) * (double)plot_width);
v7 = *(double *)&plot_ymin;
v8 = v6 - (unsigned int)(v6 == plot_width);
if ( *(double *)&plot_ymax == *(double *)&plot_ymin )
v9 = (int)*(double *)&plot_ymin;
else
v9 = (int)(((*(double *)&plot_ymin - a3) / (*(double *)&plot_ymax - *(double *)&plot_ymin) + 1.0)
* (double)plot_height);
v10 = v9 - (unsigned int)(v9 == plot_height);
if ( *(double *)&plot_xmax == *(double *)&plot_xmin )
v11 = (int)*(double *)&plot_xmin;
else
v11 = (int)((a4 - *(double *)&plot_xmin) / (*(double *)&plot_xmax - *(double *)&plot_xmin) * (double)plot_width);
v12 = v11 - (unsigned int)(v11 == plot_width);
if ( *(double *)&plot_ymax != *(double *)&plot_ymin )
v7 = ((*(double *)&plot_ymin - a5) / (*(double *)&plot_ymax - *(double *)&plot_ymin) + 1.0) * (double)plot_height;
if ( plot_inverse )
v5 = (unsigned int)(plot_levels + ~(_DWORD)v5);
return func10(v8, v10, v12, (int)v7 - (unsigned int)((int)v7 == plot_height), v5);
}

original_code:
void plot_line(double x1, double y1, double x2, double y2, int val)
{
  int ax, ay, bx, by;
  ax = ((int) (plot_xmax == plot_xmin) ? plot_xmin : ((((x1) - plot_xmin) / (plot_xmax - plot_xmin)) * plot_width)); ax = (((ax) == plot_width) ? ((ax) - 1) : (ax));
  ay = ((int) (plot_ymax == plot_ymin) ? plot_ymin : (((plot_ymin - (y1)) / (plot_ymax - plot_ymin) + 1.0) * plot_height)); ay = (((ay) == plot_height) ? ((ay) - 1) : (ay));
  bx = ((int) (plot_xmax == plot_xmin) ? plot_xmin : ((((x2) - plot_xmin) / (plot_xmax - plot_xmin)) * plot_width)); bx = (((bx) == plot_width) ? ((bx) - 1) : (bx));
  by = ((int) (plot_ymax == plot_ymin) ? plot_ymin : (((plot_ymin - (y2)) / (plot_ymax - plot_ymin) + 1.0) * plot_height)); by = (((by) == plot_height) ? ((by) - 1) : (by));
  _plot_line(ax, ay, bx, by, (plot_inverse ? ((plot_levels - 1) - (val)) : (val)));
}

canonical_original_code:
void plot_line(double x1, double y1, double x2, double y2, int val)
{
  int ax; int ay; int bx; int by;
  ax = ((int) (plot_xmax == plot_xmin) ? plot_xmin : ((((x1) - plot_xmin) / (plot_xmax - plot_xmin)) * plot_width)); ax = (((ax) == plot_width) ? ((ax) - 1) : (ax));
  ay = ((int) (plot_ymax == plot_ymin) ? plot_ymin : (((plot_ymin - (y1)) / (plot_ymax - plot_ymin) + 1.0) * plot_height)); ay = (((ay) == plot_height) ? ((ay) - 1) : (ay));
  bx = ((int) (plot_xmax == plot_xmin) ? plot_xmin : ((((x2) - plot_xmin) / (plot_xmax - plot_xmin)) * plot_width)); bx = (((bx) == plot_width) ? ((bx) - 1) : (bx));
  by = ((int) (plot_ymax == plot_ymin) ? plot_ymin : (((plot_ymin - (y2)) / (plot_ymax - plot_ymin) + 1.0) * plot_height)); by = (((by) == plot_height) ? ((by) - 1) : (by));
  _plot_line(ax, ay, bx, by, (plot_inverse ? ((plot_levels - 1) - (val)) : (val)));
}

variable_types: ax int

variable_types: ay int

variable_types: bx int


decompiled_code:
// attributes: thunk
__int64 plot_finish()
{
return _plot_finish();
}

canonical_decompiled_code:
__int64 func11()
{
return _plot_finish();
}

original_code:
void plot_finish(void)
{
  _plot_finish();
}

canonical_original_code:
void plot_finish()
{
  _plot_finish();
}