decompiled_code:
int __fastcall first_come_first_served(__m128i *a1)
{
int v1; // er15
int v2; // er13
int v3; // er12
unsigned __int32 v4; // ebp
__m128i *v5; // rbx
__int32 v6; // eax
int v7; // eax

v1 = 0;
v2 = 0;
v3 = 0;
v4 = 0;
sortProcessesByArrivalTime(a1);
do
{
while ( 1 )
{
v5 = &a1[2 * v3];
if ( v5->m128i_i32[0] <= (signed __int32)v4 )
break;
++v4;
}
printf("Process %d started at time %d\n", v5[1].m128i_u32[3], v4);
v6 = v5[1].m128i_i32[2];
v5->m128i_i32[3] = v4;
if ( v6 )
{
v5[1].m128i_i32[2] = 0;
v4 += v6;
}
v5[1].m128i_i32[0] = v4;
printf("Process %d finished at time %d\n", v5[1].m128i_u32[3], v4);
v7 = v5[1].m128i_i32[0] - v5->m128i_i32[3];
v1 += v7;
if ( v7 > v2 )
v2 = v1;
++v3;
}
while ( v3 != 20 );
printf("Average time from arrival to finish is %d seconds\n", (unsigned int)(v1 / 20));
return printf("Greatest time from arrival to finish is %d seconds\n", (unsigned int)v2);
}

canonical_decompiled_code:
int __fastcall func8(__m128i *a1)
{
int v1; // er15
int v2; // er13
int v3; // er12
unsigned __int32 v4; // ebp
__m128i *v5; // rbx
__int32 v6; // eax
int v7; // eax

v1 = 0;
v2 = 0;
v3 = 0;
v4 = 0;
func7(a1);
do
{
while ( 1 )
{
v5 = &a1[2 * v3];
if ( v5->m128i_i32[0] <= (signed __int32)v4 )
break;
++v4;
}
printf("Process %d started at time %d\n", v5[1].m128i_u32[3], v4);
v6 = v5[1].m128i_i32[2];
v5->m128i_i32[3] = v4;
if ( v6 )
{
v5[1].m128i_i32[2] = 0;
v4 += v6;
}
v5[1].m128i_i32[0] = v4;
printf("Process %d finished at time %d\n", v5[1].m128i_u32[3], v4);
v7 = v5[1].m128i_i32[0] - v5->m128i_i32[3];
v1 += v7;
if ( v7 > v2 )
v2 = v1;
++v3;
}
while ( v3 != 20 );
printf("Average time from arrival to finish is %d seconds\n", (unsigned int)(v1 / 20));
return printf("Greatest time from arrival to finish is %d seconds\n", (unsigned int)v2);
}

original_code:
void first_come_first_served(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process * currentProc;
 while (procCnt < 20) {
  currentProc = &(proc[procCnt]);
  if (currentProc->arrivaltime <= time) {
   printf("Process %d started at time %d\n", proc[procCnt].number,
     time);
   currentProc->starttime = time;
   while (currentProc->remainingtime) {
    time++;
    currentProc->remainingtime--;
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     time);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = avTime;
   }
   procCnt++;
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

canonical_original_code:
void first_come_first_served(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process *currentProc;
 while (procCnt < 20) {
  currentProc = &(proc[procCnt]);
  if (currentProc->arrivaltime <= time) {
   printf("Process %d started at time %d\n", proc[procCnt].number,
     time);
   currentProc->starttime = time;
   while (currentProc->remainingtime) {
    time++;
    currentProc->remainingtime--;
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     time);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = avTime;
   }
   procCnt++;
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

variable_types: procCnt int

variable_types: time int

variable_types: avTime int

variable_types: greatestTime int

user_defined_types: struct process { int arrivaltime; int runtime; int priority; int starttime; int endtime; int flag; int remainingtime; int number; }


original_code:
void shortest_remaining_time(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process * currentProc;
 currentProc = &(proc[0]);
 while (procCnt < 20) {
  if (currentProc->arrivaltime <= time && currentProc->endtime == 0) {
   newprocess:
   if (currentProc->runtime == currentProc->remainingtime) {
    printf("Process %d started at time %d\n", currentProc->number,
      time);
    currentProc->starttime = time;
   }
   while (currentProc->remainingtime) {
    int j;
    int shortest = 0x7fffffff;
    for (j = 0; j < 20; j++) {
     if (proc[j].arrivaltime <= time
       && proc[j].remainingtime
         < currentProc->remainingtime
       && proc[j].remainingtime > 0
       && proc[j].remainingtime < shortest) {
      currentProc = &(proc[j]);
      shortest = currentProc->remainingtime;
     }
     if (shortest != 0x7fffffff) {
      goto newprocess;
     }
    }
    time++;
    currentProc->remainingtime--;
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     currentProc->endtime);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = avTime;
   }
   procCnt++;
   int i;
   int shortest = 0x7fffffff;
   for (i = 0; i < 20; i++) {
    if (proc[i].arrivaltime <= time && proc[i].remainingtime > 0) {
     if (proc[i].remainingtime < shortest) {
      shortest = proc[i].remainingtime;
      currentProc = &(proc[i]);
     }
    }
   }
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

canonical_original_code:
void shortest_remaining_time(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process *currentProc;
 currentProc = &(proc[0]);
 while (procCnt < 20) {
  if (currentProc->arrivaltime <= time && currentProc->endtime == 0) {
   newprocess:
   if (currentProc->runtime == currentProc->remainingtime) {
    printf("Process %d started at time %d\n", currentProc->number,
      time);
    currentProc->starttime = time;
   }
   while (currentProc->remainingtime) {
    int j;
    int shortest = 0x7fffffff;
    for (j = 0; j < 20; j++) {
     if (proc[j].arrivaltime <= time
       && proc[j].remainingtime
         < currentProc->remainingtime
       && proc[j].remainingtime > 0
       && proc[j].remainingtime < shortest) {
      currentProc = &(proc[j]);
      shortest = currentProc->remainingtime;
     }
     if (shortest != 0x7fffffff) {
      goto newprocess;
     }
    }
    time++;
    currentProc->remainingtime--;
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     currentProc->endtime);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = avTime;
   }
   procCnt++;
   int i;
   int shortest = 0x7fffffff;
   for (i = 0; i < 20; i++) {
    if (proc[i].arrivaltime <= time && proc[i].remainingtime > 0) {
     if (proc[i].remainingtime < shortest) {
      shortest = proc[i].remainingtime;
      currentProc = &(proc[i]);
     }
    }
   }
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

variable_types: struct process { int arrivaltime; int runtime; int priority; int starttime; int endtime; int flag; int remainingtime; int number; } *

variable_types: j int

variable_types: shortest int

variable_types: i int

return_type: void


original_code:
void round_robin(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process * currentProc;
 currentProc = &(proc[0]);
 while (procCnt < 20) {
  if (currentProc->arrivaltime <= time && currentProc->endtime == 0) {
   newprocess:
   if (currentProc->runtime == currentProc->remainingtime) {
    printf("Process %d started at time %d\n", currentProc->number,
      time);
    currentProc->starttime = time;
   }
   while (currentProc->remainingtime) {
    time++;
    currentProc->remainingtime--;
    int i;
    for (i = 0; i < 20; i++) {
     if (proc[i].endtime == 0
       && proc[i].number != currentProc->number) {
      currentProc = &(proc[i]);
      goto newprocess;
     }
    }
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     currentProc->endtime);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = avTime;
   }
   procCnt++;
   int i;
   for (i = 0; i < 20; i++) {
    if (proc[i].endtime == 0) {
     currentProc = &(proc[i]);
     goto newprocess;
    }
   }
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

canonical_original_code:
void round_robin(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process *currentProc;
 currentProc = &(proc[0]);
 while (procCnt < 20) {
  if (currentProc->arrivaltime <= time && currentProc->endtime == 0) {
   newprocess:
   if (currentProc->runtime == currentProc->remainingtime) {
    printf("Process %d started at time %d\n", currentProc->number,
      time);
    currentProc->starttime = time;
   }
   while (currentProc->remainingtime) {
    time++;
    currentProc->remainingtime--;
    int i;
    for (i = 0; i < 20; i++) {
     if (proc[i].endtime == 0
       && proc[i].number != currentProc->number) {
      currentProc = &(proc[i]);
      goto newprocess;
     }
    }
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     currentProc->endtime);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = avTime;
   }
   procCnt++;
   int i;
   for (i = 0; i < 20; i++) {
    if (proc[i].endtime == 0) {
     currentProc = &(proc[i]);
     goto newprocess;
    }
   }
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}


original_code:
void round_robin_priority(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process * currentProc;
 currentProc = &(proc[0]);
 while (procCnt < 20) {
  if (currentProc->arrivaltime <= time && currentProc->endtime == 0) {
   newprocess:
   if (currentProc->runtime == currentProc->remainingtime) {
    printf("Process %d started at time %d\n", currentProc->number,
      time);
    currentProc->starttime = time;
   }
   while (currentProc->remainingtime) {
    time++;
    currentProc->remainingtime--;
    int i;
    int flag = 0;
    int number = currentProc->number;
    int highestPrioFound = currentProc->priority;
    for (i = 0; i < 20; i++) {
     if (proc[i].arrivaltime <= time && proc[i].endtime == 0
       && proc[i].priority >= highestPrioFound
       && proc[i].number != number) {
      currentProc = &(proc[i]);
      highestPrioFound = currentProc->priority;
      flag = 1;
     }
    }
    if (flag) {
     goto newprocess;
    }
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     currentProc->endtime);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = (currentProc->endtime - currentProc->starttime);
   }
   procCnt++;
   int i;
   int highestPrioFound = 0;
   for (i = 0; i < 20; i++) {
    if (currentProc->arrivaltime <= time && proc[i].endtime == 0
      && proc[i].priority >= highestPrioFound) {
     currentProc = &(proc[i]);
     highestPrioFound = currentProc->priority;
    }
   }
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

canonical_original_code:
void round_robin_priority(struct process *proc) {
 sortProcessesByArrivalTime(proc);
 int procCnt = 0;
 int time = 0;
 int avTime = 0;
 int greatestTime = 0;
 struct process *currentProc;
 currentProc = &(proc[0]);
 while (procCnt < 20) {
  if (currentProc->arrivaltime <= time && currentProc->endtime == 0) {
   newprocess:
   if (currentProc->runtime == currentProc->remainingtime) {
    printf("Process %d started at time %d\n", currentProc->number,
      time);
    currentProc->starttime = time;
   }
   while (currentProc->remainingtime) {
    time++;
    currentProc->remainingtime--;
    int i;
    int flag = 0;
    int number = currentProc->number;
    int highestPrioFound = currentProc->priority;
    for (i = 0; i < 20; i++) {
     if (proc[i].arrivaltime <= time && proc[i].endtime == 0
       && proc[i].priority >= highestPrioFound
       && proc[i].number != number) {
      currentProc = &(proc[i]);
      highestPrioFound = currentProc->priority;
      flag = 1;
     }
    }
    if (flag) {
     goto newprocess;
    }
   }
   currentProc->endtime = time;
   printf("Process %d finished at time %d\n", currentProc->number,
     currentProc->endtime);
   avTime += currentProc->endtime - currentProc->starttime;
   if ((currentProc->endtime - currentProc->starttime)
     > greatestTime) {
    greatestTime = (currentProc->endtime - currentProc->starttime);
   }
   procCnt++;
   int i;
   int highestPrioFound = 0;
   for (i = 0; i < 20; i++) {
    if (currentProc->arrivaltime <= time && proc[i].endtime == 0
      && proc[i].priority >= highestPrioFound) {
     currentProc = &(proc[i]);
     highestPrioFound = currentProc->priority;
    }
   }
  }
  else {
  time++;
  }
 }
 avTime /= 20;
 printf("Average time from arrival to finish is %d seconds\n", avTime);
 printf("Greatest time from arrival to finish is %d seconds\n",
   greatestTime);
}

variable_types: flag int

variable_types: number int

variable_types: highestPrioFound int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // rbp
_BYTE *v5; // r13
int v6; // er12
int v7; // eax
__int64 v8; // rcx
__int64 v9; // rsi
__int64 v10; // r8
__int64 v11; // rdx
_BYTE v13[640]; // [rsp+0h] [rbp-528h] BYREF
char v14[680]; // [rsp+280h] [rbp-2A8h] BYREF

v3 = 0;
srand(0xC0FFEEu);
v4 = v13;
v5 = v13;
do
{
v5 += 32;
*((_DWORD *)v5 - 8) = rand() % 100;
v6 = rand() % 30 + 10;
*((_DWORD *)v5 - 7) = v6;
v7 = rand();
*((_DWORD *)v5 - 1) = v3++;
*((_DWORD *)v5 - 5) = 0;
*((_DWORD *)v5 - 2) = v6;
*((_DWORD *)v5 - 4) = 0;
*((_DWORD *)v5 - 3) = 0;
*((_DWORD *)v5 - 6) = v7 % 3;
}
while ( v3 != 20 );
puts("\n\nProcess\t|\tArrival\t|\tRuntime\t|\tPriority");
do
{
v8 = *((unsigned int *)v4 + 1);
v9 = *((unsigned int *)v4 + 7);
v10 = *((unsigned int *)v4 + 2);
v11 = *(unsigned int *)v4;
v4 += 32;
printf("%d\t|\t%d\t|\t%d\t|\t%d\n", v9, v11, v8, v10);
}
while ( v4 != v14 );
puts("\n\nFirst come first served");
qmemcpy(v14, v13, 0x280uLL);
first_come_first_served(v14);
puts("\n\nShortest remaining time");
qmemcpy(v14, v13, 0x280uLL);
shortest_remaining_time(v14);
puts("\n\nRound Robin");
qmemcpy(v14, v13, 0x280uLL);
round_robin(v14);
puts("\n\nRound Robin with priority");
qmemcpy(v14, v13, 0x280uLL);
round_robin_priority(v14);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // rbp
_BYTE *v5; // r13
int v6; // er12
int v7; // eax
__int64 v8; // rcx
__int64 v9; // rsi
__int64 v10; // r8
__int64 v11; // rdx
_BYTE v13[640]; // [rsp+0h] [rbp-528h] BYREF
char v14[680]; // [rsp+280h] [rbp-2A8h] BYREF

v3 = 0;
srand(0xC0FFEEu);
v4 = v13;
v5 = v13;
do
{
v5 += 32;
*((_DWORD *)v5 - 8) = rand() % 100;
v6 = rand() % 30 + 10;
*((_DWORD *)v5 - 7) = v6;
v7 = rand();
*((_DWORD *)v5 - 1) = v3++;
*((_DWORD *)v5 - 5) = 0;
*((_DWORD *)v5 - 2) = v6;
*((_DWORD *)v5 - 4) = 0;
*((_DWORD *)v5 - 3) = 0;
*((_DWORD *)v5 - 6) = v7 % 3;
}
while ( v3 != 20 );
puts("\n\nProcess\t|\tArrival\t|\tRuntime\t|\tPriority");
do
{
v8 = *((unsigned int *)v4 + 1);
v9 = *((unsigned int *)v4 + 7);
v10 = *((unsigned int *)v4 + 2);
v11 = *(unsigned int *)v4;
v4 += 32;
printf("%d\t|\t%d\t|\t%d\t|\t%d\n", v9, v11, v8, v10);
}
while ( v4 != v14 );
puts("\n\nFirst come first served");
qmemcpy(v14, v13, 0x280uLL);
func8(v14);
puts("\n\nShortest remaining time");
qmemcpy(v14, v13, 0x280uLL);
func9(v14);
puts("\n\nRound Robin");
qmemcpy(v14, v13, 0x280uLL);
func10(v14);
puts("\n\nRound Robin with priority");
qmemcpy(v14, v13, 0x280uLL);
func11(v14);
return 0;
}

original_code:
int main() {
 int i;
 struct process proc[20],
 proc_copy[20];
 srand(0xC0FFEE);
 for (i = 0; i < 20; i++) {
  proc[i].arrivaltime = rand() % 100;
  proc[i].runtime = (rand() % 30) + 10;
  proc[i].priority = rand() % 3;
  proc[i].starttime = 0;
  proc[i].endtime = 0;
  proc[i].flag = 0;
  proc[i].remainingtime = proc[i].runtime;
  proc[i].number = i;
 }
 printf("\n\nProcess\t|\tArrival\t|\tRuntime\t|\tPriority\n");
 for (i = 0; i < 20; i++)
  printf("%d\t|\t%d\t|\t%d\t|\t%d\n", proc[i].number, proc[i].arrivaltime,
    proc[i].runtime, proc[i].priority);
 printf("\n\nFirst come first served\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 first_come_first_served(proc_copy);
 printf("\n\nShortest remaining time\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 shortest_remaining_time(proc_copy);
 printf("\n\nRound Robin\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 round_robin(proc_copy);
 printf("\n\nRound Robin with priority\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 round_robin_priority(proc_copy);
 return 0;
}

canonical_original_code:
int main() {
 int i;
 struct process proc[20]; struct process proc_copy[20];
 srand(0xC0FFEE);
 for (i = 0; i < 20; i++) {
  proc[i].arrivaltime = rand() % 100;
  proc[i].runtime = (rand() % 30) + 10;
  proc[i].priority = rand() % 3;
  proc[i].starttime = 0;
  proc[i].endtime = 0;
  proc[i].flag = 0;
  proc[i].remainingtime = proc[i].runtime;
  proc[i].number = i;
 }
 printf("\n\nProcess\t|\tArrival\t|\tRuntime\t|\tPriority\n");
 for (i = 0; i < 20; i++)
  printf("%d\t|\t%d\t|\t%d\t|\t%d\n", proc[i].number, proc[i].arrivaltime,
    proc[i].runtime, proc[i].priority);
 printf("\n\nFirst come first served\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 first_come_first_served(proc_copy);
 printf("\n\nShortest remaining time\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 shortest_remaining_time(proc_copy);
 printf("\n\nRound Robin\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 round_robin(proc_copy);
 printf("\n\nRound Robin with priority\n");
 memcpy(proc_copy, proc, 20 * sizeof(struct process));
 round_robin_priority(proc_copy);
 return 0;
}

variable_types: struct process { int arrivaltime; int runtime; int priority; int starttime; int endtime; int flag; int remainingtime; int number; } [20]

return_type: int


addToList

decompiled_code:
__int64 __fastcall addToList(unsigned int *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *a1;
*(_QWORD *)(a2 + 16) = 0LL;
if ( (_DWORD)result )
{
v3 = *((_QWORD *)a1 + 2);
result = (unsigned int)(result + 1);
*(_QWORD *)(a2 + 24) = v3;
*(_QWORD *)(v3 + 16) = a2;
*((_QWORD *)a1 + 2) = a2;
*a1 = result;
}
else
{
*(_QWORD *)(a2 + 24) = 0LL;
*((_QWORD *)a1 + 1) = a2;
*((_QWORD *)a1 + 2) = a2;
*a1 = 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *a1;
*(_QWORD *)(a2 + 16) = 0LL;
if ( (_DWORD)result )
{
v3 = *((_QWORD *)a1 + 2);
result = (unsigned int)(result + 1);
*(_QWORD *)(a2 + 24) = v3;
*(_QWORD *)(v3 + 16) = a2;
*((_QWORD *)a1 + 2) = a2;
*a1 = result;
}
else
{
*(_QWORD *)(a2 + 24) = 0LL;
*((_QWORD *)a1 + 1) = a2;
*((_QWORD *)a1 + 2) = a2;
*a1 = 1;
}
return result;
}

original_code:
void addToList(ProcessList * procList, Process * proc) {
 Process * newProc = (Process*) malloc(sizeof(Process));
 newProc = proc;
 newProc->next = ((void *)0);
 if (procList->size == 0) {
  newProc->prev = ((void *)0);
  procList->head = newProc;
  procList->foot = procList->head;
  procList->size++;
 } else {
  newProc->prev = procList->foot;
  newProc->prev->next = newProc;
  procList->foot = newProc;
  procList->size++;
 }
}

canonical_original_code:
void addToList(struct ProcessList *procList, struct Process *proc) {
 struct Process *newProc = (Process*) malloc(sizeof(Process));
 newProc = proc;
 newProc->next = ((void *)0);
 if (procList->size == 0) {
  newProc->prev = ((void *)0);
  procList->head = newProc;
  procList->foot = procList->head;
  procList->size++;
 } else {
  newProc->prev = procList->foot;
  newProc->prev->next = newProc;
  procList->foot = newProc;
  procList->size++;
 }
}

variable_types: struct ProcessList { int size; struct Process { char *procname; int pid; struct Process *next; struct Process *prev; } *head; struct Process *foot; } *

variable_types:
proc
struct Process *

user_defined_types: struct ProcessList { int size; struct Process *head; struct Process *foot; }


removeFromList

original_code:
Process * removeFromList(ProcessList * procList, pid_t pid) {
 Process * dummy = procList->head;
 if (procList->size == 0) {
  return ((void *)0);
 }
 dummy = procList->head;
 int i;
 for (i = 0; i < procList->size; i++) {
  if (dummy->pid == pid) {
   if (i == 0) {
    procList->head = dummy->next;
   }
   if (i == procList->size) {
    procList->foot = dummy->prev;
   }
   if (dummy->next != ((void *)0)) {
    dummy->next->prev = dummy->prev;
   }
   if (dummy->prev != ((void *)0)) {
    dummy->prev->next = dummy->next;
   }
   procList->size--;
   return dummy;
  } else {
   dummy = dummy->next;
  }
 }
 return ((void *)0);
}

canonical_original_code:
struct Process *removeFromList(struct ProcessList *procList, int pid) {
 struct Process *dummy = procList->head;
 if (procList->size == 0) {
  return ((void *)0);
 }
 dummy = procList->head;
 int i;
 for (i = 0; i < procList->size; i++) {
  if (dummy->pid == pid) {
   if (i == 0) {
    procList->head = dummy->next;
   }
   if (i == procList->size) {
    procList->foot = dummy->prev;
   }
   if (dummy->next != ((void *)0)) {
    dummy->next->prev = dummy->prev;
   }
   if (dummy->prev != ((void *)0)) {
    dummy->prev->next = dummy->next;
   }
   procList->size--;
   return dummy;
  } else {
   dummy = dummy->next;
  }
 }
 return ((void *)0);
}

variable_types: struct ProcessList { int size; struct Process *head; struct Process *foot; } *

variable_types: pid int

variable_types: struct Process { char *procname; int pid; struct Process *next; struct Process *prev; } *


printProcesses

decompiled_code:
int __fastcall printProcesses(int *a1)
{
__int64 v1; // rsi
int result; // eax
__int64 v3; // rbx
signed int v4; // ebp

v1 = (unsigned int)*a1;
if ( !(_DWORD)v1 )
return puts("No jobs running in background");
printf("%d processes are currently running:\n", v1);
result = *a1;
v3 = *((_QWORD *)a1 + 1);
if ( *a1 > 0 )
{
v4 = 0;
do
{
++v4;
result = printf("[%d] %s is currently running\n", *(unsigned int *)(v3 + 8), *(const char **)v3);
v3 = *(_QWORD *)(v3 + 16);
}
while ( *a1 > v4 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int *a1)
{
__int64 v1; // rsi
int result; // eax
__int64 v3; // rbx
signed int v4; // ebp

v1 = (unsigned int)*a1;
if ( !(_DWORD)v1 )
return puts("No jobs running in background");
printf("%d processes are currently running:\n", v1);
result = *a1;
v3 = *((_QWORD *)a1 + 1);
if ( *a1 > 0 )
{
v4 = 0;
do
{
++v4;
result = printf("[%d] %s is currently running\n", *(unsigned int *)(v3 + 8), *(const char **)v3);
v3 = *(_QWORD *)(v3 + 16);
}
while ( *a1 > v4 );
}
return result;
}

original_code:
void printProcesses(ProcessList * procList) {
 if (procList->size == 0) {
  printf("No jobs running in background\n");
 } else {
  printf("%d processes are currently running:\n", procList->size);
  Process * dummy = procList->head;
  int i;
  for (i = 0; i < procList->size; i++) {
   printf("[%d] %s is currently running\n", dummy->pid,
     dummy->procname);
   dummy = dummy->next;
  }
 }
}

canonical_original_code:
void printProcesses(struct ProcessList *procList) {
 if (procList->size == 0) {
  printf("No jobs running in background\n");
 } else {
  printf("%d processes are currently running:\n", procList->size);
  struct Process *dummy = procList->head;
  int i;
  for (i = 0; i < procList->size; i++) {
   printf("[%d] %s is currently running\n", dummy->pid,
     dummy->procname);
   dummy = dummy->next;
  }
 }
}

user_defined_types: struct Process { char *procname; int pid; struct Process *next; struct Process *prev; }


parallelSort

original_code:
void parallelSort(int * arrayToSortBy, int * arrayToSortWith, int arrSize) {
 int c, d;
 for (c = 0; c < (arrSize - 1); c++) {
  for (d = 0; d < arrSize - c - 1; d++) {
   if (arrayToSortBy[d] > arrayToSortBy[d + 1]) {
    arrayToSortBy[d] ^= arrayToSortBy[d + 1];
    arrayToSortBy[d + 1] ^= arrayToSortBy[d];
    arrayToSortBy[d] ^= arrayToSortBy[d + 1];
    arrayToSortWith[d] ^= arrayToSortWith[d + 1];
    arrayToSortWith[d + 1] ^= arrayToSortWith[d];
    arrayToSortWith[d] ^= arrayToSortWith[d + 1];
   }
  }
 }
}

canonical_original_code:
void parallelSort(int *arrayToSortBy, int *arrayToSortWith, int arrSize) {
 int c; int d;
 for (c = 0; c < (arrSize - 1); c++) {
  for (d = 0; d < arrSize - c - 1; d++) {
   if (arrayToSortBy[d] > arrayToSortBy[d + 1]) {
    arrayToSortBy[d] ^= arrayToSortBy[d + 1];
    arrayToSortBy[d + 1] ^= arrayToSortBy[d];
    arrayToSortBy[d] ^= arrayToSortBy[d + 1];
    arrayToSortWith[d] ^= arrayToSortWith[d + 1];
    arrayToSortWith[d + 1] ^= arrayToSortWith[d];
    arrayToSortWith[d] ^= arrayToSortWith[d + 1];
   }
  }
 }
}

variable_types:
arrayToSortBy
int *

variable_types:
arrayToSortWith
int *

variable_types: arrSize int

variable_types: c int

variable_types: d int


checkDuplicates

decompiled_code:
__int64 __fastcall checkDuplicates(_DWORD *a1, int a2)
{
_DWORD *v2; // rdx
int v3; // er8
_DWORD *v4; // rax

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

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, int a2)
{
_DWORD *v2; // rdx
int v3; // er8
_DWORD *v4; // rax

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

original_code:
int checkDuplicates(int * array, int arrSize) {
 int i, j;
 for (i = 0; i< arrSize; i++){
  for (j = 0; j<i; j++){
   if(array[i] == array[j]){
    return 1;
   }
  }
 }
 return 0;
}

canonical_original_code:
int checkDuplicates(int *array, int arrSize) {
 int i; int j;
 for (i = 0; i< arrSize; i++){
  for (j = 0; j<i; j++){
   if(array[i] == array[j]){
    return 1;
   }
  }
 }
 return 0;
}

variable_types:
array
int *

variable_types: j int


printstatus

decompiled_code:
int __fastcall printstatus(__int16 a1, __int64 a2, const char *a3)
{
int result; // eax

if ( (a1 & 0x7F) == 0 )
result = printf("[%d] %s Exit %d\n", a2, a3, HIBYTE(a1));
if ( (char)((a1 & 0x7F) + 1) > 1 )
result = printf("[%d] %s Killed (%d)\n", (unsigned int)a2, a3, a1 & 0x7F);
return result;
}

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

if ( (a1 & 0x7F) == 0 )
result = printf("[%d] %s Exit %d\n", a2, a3, HIBYTE(a1));
if ( (char)((a1 & 0x7F) + 1) > 1 )
result = printf("[%d] %s Killed (%d)\n", (unsigned int)a2, a3, a1 & 0x7F);
return result;
}

original_code:
void printstatus(int status, int pid, char* arg) {
 if ((((status) & 0x7f) == 0)) {
  printf("[%d] %s Exit %d\n", pid, arg, (((status) & 0xff00) >> 8));
 }
 if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
  printf("[%d] %s Killed (%d)\n", pid, arg, ((status) & 0x7f));
 }
}

canonical_original_code:
void printstatus(int status, int pid, char *arg) {
 if ((((status) & 0x7f) == 0)) {
  printf("[%d] %s Exit %d\n", pid, arg, (((status) & 0xff00) >> 8));
 }
 if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
  printf("[%d] %s Killed (%d)\n", pid, arg, ((status) & 0x7f));
 }
}

variable_types: status int

variable_types:
arg
char *


execute

decompiled_code:
int __fastcall execute(char **argv, __int64 a2)
{
__pid_t v2; // eax
char *v3; // r12
__pid_t v4; // ebp
size_t v5; // rax
void *v6; // rax
char *v7; // r14
void *v8; // r12
size_t v9; // rax
int result; // eax
unsigned int v11; // eax

v2 = fork();
if ( v2 < 0 )
{
puts("ERROR: Forking failed");
exit(1);
}
v3 = *argv;
if ( v2 )
{
v4 = v2;
v5 = strlen(*argv);
v6 = malloc(v5);
*(_QWORD *)a2 = v6;
v7 = *argv;
v8 = v6;
v9 = strlen(*argv);
result = (unsigned int)memcpy(v8, v7, v9);
*(_DWORD *)(a2 + 8) = v4;
}
else
{
v11 = getpid();
printf("[%d] %s\n", v11, v3);
result = execvp(*argv, argv);
if ( result < 0 )
{
puts("ERROR: Executable not found");
exit(1);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(char **argv, __int64 a2)
{
__pid_t v2; // eax
char *v3; // r12
__pid_t v4; // ebp
size_t v5; // rax
void *v6; // rax
char *v7; // r14
void *v8; // r12
size_t v9; // rax
int result; // eax
unsigned int v11; // eax

v2 = fork();
if ( v2 < 0 )
{
puts("ERROR: Forking failed");
exit(1);
}
v3 = *argv;
if ( v2 )
{
v4 = v2;
v5 = strlen(*argv);
v6 = malloc(v5);
*(_QWORD *)a2 = v6;
v7 = *argv;
v8 = v6;
v9 = strlen(*argv);
result = (unsigned int)memcpy(v8, v7, v9);
*(_DWORD *)(a2 + 8) = v4;
}
else
{
v11 = getpid();
printf("[%d] %s\n", v11, v3);
result = execvp(*argv, argv);
if ( result < 0 )
{
puts("ERROR: Executable not found");
exit(1);
}
}
return result;
}

original_code:
void execute(char **argv, Process * executed) {
 pid_t pid;
 if ((pid = fork()) < 0) {
  printf("ERROR: Forking failed\n");
  exit(1);
 } else if (pid == 0) {
  printf("[%d] %s\n", getpid(), argv[0]);
  if (execvp(*argv, argv) < 0) {
   printf("ERROR: Executable not found\n");
   exit(1);
  }
 } else {
  executed->procname = malloc(strlen(argv[0]));
  memcpy((executed->procname), argv[0], strlen(argv[0]));
  executed->pid = pid;
 }
}

canonical_original_code:
void execute(char **argv, struct Process *executed) {
 int pid;
 if ((pid = fork()) < 0) {
  printf("ERROR: Forking failed\n");
  exit(1);
 } else if (pid == 0) {
  printf("[%d] %s\n", getpid(), argv[0]);
  if (execvp(*argv, argv) < 0) {
   printf("ERROR: Executable not found\n");
   exit(1);
  }
 } else {
  executed->procname = malloc(strlen(argv[0]));
  memcpy((executed->procname), argv[0], strlen(argv[0]));
  executed->pid = pid;
 }
}

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall initialize_accounts(int a1)
{
void *v1; // rax

v1 = malloc(4LL * a1);
BANK_accounts = (__int64)v1;
if ( !v1 )
return 0LL;
if ( a1 > 0 )
memset(v1, 0, 4LL * a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
void *v1; // rax

v1 = malloc(4LL * a1);
BANK_accounts = (__int64)v1;
if ( !v1 )
return 0LL;
if ( a1 > 0 )
memset(v1, 0, 4LL * a1);
return 1LL;
}

original_code:
int initialize_accounts( int n )
{
 BANK_accounts = (int *) malloc(sizeof(int) * n);
 if(BANK_accounts == ((void *)0)) return 0;
 int i;
 for( i = 0; i < n; i++)
 {
  BANK_accounts[i] = 0;
 }
 return 1;
}

canonical_original_code:
int initialize_accounts(int n)
{
 BANK_accounts = (int *) malloc(sizeof(int) * n);
 if(BANK_accounts == ((void *)0)) return 0;
 int i;
 for( i = 0; i < n; i++)
 {
  BANK_accounts[i] = 0;
 }
 return 1;
}

variable_types: n int

variable_types: i int


decompiled_code:
__int64 __fastcall read_account(int a1)
{
usleep(0x186A0u);
return *(unsigned int *)(BANK_accounts + 4LL * a1 - 4);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
usleep(0x186A0u);
return *(unsigned int *)(BANK_accounts + 4LL * a1 - 4);
}

original_code:
int read_account( int ID )
{
 usleep( 100000 );
 return BANK_accounts[ID - 1];
}

canonical_original_code:
int read_account(int ID)
{
 usleep( 100000 );
 return BANK_accounts[ID - 1];
}

variable_types: ID int


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

usleep(0x186A0u);
result = BANK_accounts;
*(_DWORD *)(BANK_accounts + 4LL * a1 - 4) = a2;
return result;
}

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

usleep(0x186A0u);
result = BANK_accounts;
*(_DWORD *)(BANK_accounts + 4LL * a1 - 4) = a2;
return result;
}

original_code:
void write_account( int ID, int value)
{
 usleep( 100000 );
 BANK_accounts[ID - 1] = value;
}

canonical_original_code:
void write_account(int ID, int value)
{
 usleep( 100000 );
 BANK_accounts[ID - 1] = value;
}

variable_types: value int


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

v0 = time(0LL);
srand(v0);
v1 = rand();
return puts(message[v1 / 0x1FFFFFFF]);
}

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

v0 = time(0LL);
srand(v0);
v1 = rand();
return puts(message[v1 / 0x1FFFFFFF]);
}

original_code:
void print_message() {
    int index;
    srand(time(((void *)0)));
    index = rand()/(2147483647/4);
    printf("%s\n", message[index]);
}

variable_types: index int


main

repo: doches/progressbar

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rbp
int v5; // ebx
__int64 v6; // rdx
__int64 v7; // rbp
__int64 v8; // rdx
__int64 v9; // rbp
int v10; // ebx
__int64 v11; // rbp
int v12; // ebx
__int64 v13; // rbp
int v14; // ebx
__int64 v15; // rbp
int v16; // ebx
__int64 v17; // rbp

v3 = 60;
v4 = progressbar_new("Smooth", 60LL, envp);
do
{
usleep(0x186A0u);
progressbar_inc(v4);
--v3;
}
while ( v3 );
v5 = 20;
progressbar_finish(v4);
v7 = progressbar_new("Three Second Task with a long label", 3LL, v6);
progressbar_inc(v7);
sleep(1u);
progressbar_inc(v7);
sleep(1u);
progressbar_inc(v7);
sleep(1u);
progressbar_finish(v7);
v9 = progressbar_new("Fast", 20LL, v8);
do
{
usleep(0x186A0u);
progressbar_inc(v9);
--v5;
}
while ( v5 );
v10 = 60;
progressbar_finish(v9);
v11 = progressbar_new_with_format("Custom", 60LL, "<.>");
do
{
usleep(0x186A0u);
progressbar_inc(v11);
--v10;
}
while ( v10 );
v12 = 30;
progressbar_finish(v11);
v13 = statusbar_new("Indeterminate");
do
{
usleep(0x186A0u);
statusbar_inc(v13);
--v12;
}
while ( v12 );
v14 = 10;
statusbar_finish(v13);
v15 = statusbar_new("Status bar with a really long label");
do
{
usleep(0x186A0u);
statusbar_inc(v15);
--v14;
}
while ( v14 );
v16 = 30;
statusbar_finish(v15);
v17 = statusbar_new_with_format("Custom", "(|)|");
do
{
usleep(0x186A0u);
statusbar_inc(v17);
--v16;
}
while ( v16 );
statusbar_finish(v17);
return 0;
}

original_code:
int main(void)
{
    int max = 60;
    progressbar *smooth = progressbar_new("Smooth",max);
    for(int i=0; i < max; i++) {
        usleep(100000);
        progressbar_inc(smooth);
    }
    progressbar_finish(smooth);
    progressbar *longlabel = progressbar_new("Three Second Task with a long label",3);
    for(int i=0; i < 3; i++) {
        progressbar_inc(longlabel);
        sleep(1);
    }
    progressbar_finish(longlabel);
    progressbar *fast = progressbar_new("Fast",20);
    for(int i=0; i < 20; i++) {
        usleep(100000);
        progressbar_inc(fast);
    }
    progressbar_finish(fast);
    progressbar *custom = progressbar_new_with_format("Custom",max,"<.>");
    for(int i=0; i < max; i++) {
      usleep(100000);
      progressbar_inc(custom);
    }
    progressbar_finish(custom);
    statusbar *status = statusbar_new("Indeterminate");
    for(int i=0; i < 30; i++) {
        usleep(100000);
        statusbar_inc(status);
    }
    statusbar_finish(status);
    statusbar *longStatus = statusbar_new("Status bar with a really long label");
    for(int i=0; i < 10; i++) {
        usleep(100000);
        statusbar_inc(longStatus);
    }
    statusbar_finish(longStatus);
    statusbar *customStatus = statusbar_new_with_format("Custom","(|)|");
    for(int i=0; i < 30; i++) {
        usleep(100000);
        statusbar_inc(customStatus);
    }
    statusbar_finish(customStatus);
}

canonical_original_code:
int main()
{
    int max = 60;
    struct _progressbar_t *smooth = progressbar_new("Smooth",max);
    for(int i = 0; i < max; i++) {
        usleep(100000);
        progressbar_inc(smooth);
    }
    progressbar_finish(smooth);
    struct _progressbar_t *longlabel = progressbar_new("Three Second Task with a long label",3);
    for(int i = 0; i < 3; i++) {
        progressbar_inc(longlabel);
        sleep(1);
    }
    progressbar_finish(longlabel);
    struct _progressbar_t *fast = progressbar_new("Fast",20);
    for(int i = 0; i < 20; i++) {
        usleep(100000);
        progressbar_inc(fast);
    }
    progressbar_finish(fast);
    struct _progressbar_t *custom = progressbar_new_with_format("Custom",max,"<.>");
    for(int i = 0; i < max; i++) {
      usleep(100000);
      progressbar_inc(custom);
    }
    progressbar_finish(custom);
    struct _statusbar_t *status = statusbar_new("Indeterminate");
    for(int i = 0; i < 30; i++) {
        usleep(100000);
        statusbar_inc(status);
    }
    statusbar_finish(status);
    struct _statusbar_t *longStatus = statusbar_new("Status bar with a really long label");
    for(int i = 0; i < 10; i++) {
        usleep(100000);
        statusbar_inc(longStatus);
    }
    statusbar_finish(longStatus);
    struct _statusbar_t *customStatus = statusbar_new_with_format("Custom","(|)|");
    for(int i = 0; i < 30; i++) {
        usleep(100000);
        statusbar_inc(customStatus);
    }
    statusbar_finish(customStatus);
}

variable_types: max int

variable_types: struct _progressbar_t { unsigned long max; unsigned long value; long start; char *label; struct { char begin; char fill; char end; } format; } *

variable_types: struct _statusbar_t { unsigned int start_time; char *label; int format_index; int format_length; char *format; int last_printed; } *


decompiled_code:
__int64 __fastcall progressbar_max(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

original_code:
static int progressbar_max(int x, int y) {
  return x > y ? x : y;
}

variable_types: x int

variable_types: y int


decompiled_code:
__int64 __fastcall progressbar_bar_width(int a1, int a2)
{
return progressbar_max(0xAu, a1 - a2 - 15);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
return func0(0xAu, a1 - a2 - 15);
}

original_code:
static int progressbar_bar_width(int screen_width, int label_length) {
  return progressbar_max(MINIMUM_BAR_WIDTH, screen_width - label_length - ETA_FORMAT_LENGTH - WHITESPACE_LENGTH);
}


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

result = a2;
if ( (int)(a2 + a3 + 14) >= a1 )
result = progressbar_max(0, a1 - a3 - 15);
return result;
}

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

result = a2;
if ( (int)(a2 + a3 + 14) >= a1 )
result = func0(0, a1 - a3 - 15);
return result;
}

original_code:
static int progressbar_label_width(int screen_width, int label_length, int bar_width) {
  int eta_width = ETA_FORMAT_LENGTH;
  if (label_length + 1 + bar_width + 1 + ETA_FORMAT_LENGTH > screen_width) {
    return progressbar_max(0, screen_width - bar_width - eta_width - WHITESPACE_LENGTH);
  } else {
    return label_length;
  }
}


decompiled_code:
__int64 __fastcall progressbar_calc_time_components(int a1)
{
__int64 v2; // [rsp+0h] [rbp-14h]

LODWORD(v2) = a1 / 3600;
HIDWORD(v2) = a1 % 3600 / 60;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
__int64 v2; // [rsp+0h] [rbp-14h]

LODWORD(v2) = a1 / 3600;
HIDWORD(v2) = a1 % 3600 / 60;
return v2;
}

original_code:
static progressbar_time_components progressbar_calc_time_components(int seconds) {
  int hours = seconds / 3600;
  seconds -= hours * 3600;
  int minutes = seconds / 60;
  seconds -= minutes * 60;
  progressbar_time_components components = {hours, minutes, seconds};
  return components;
}

canonical_original_code:
static struct progressbar_time_components progressbar_calc_time_components(int seconds) {
  int hours = seconds / 3600;
  seconds -= hours * 3600;
  int minutes = seconds / 60;
  seconds -= minutes * 60;
  struct progressbar_time_components components = {hours, minutes, seconds};
  return components;
}

variable_types: seconds int

variable_types: hours int

variable_types: minutes int

variable_types: struct progressbar_time_components { int hours; int minutes; int seconds; }


decompiled_code:
__int64 __fastcall progressbar_remaining_seconds(_QWORD *a1)
{
unsigned int v1; // er12
time_t v2; // r13
time_t v3; // rax
__int64 v4; // rbp
double v5; // xmm0_8
__int64 v6; // rax
double v7; // xmm1_8
double v8; // xmm2_8
unsigned __int64 v10; // rdx

v1 = 0;
v2 = a1[2];
v3 = time(0LL);
v4 = a1[1];
if ( v4 )
{
v5 = difftime(v3, v2);
if ( v5 > 0.0 )
{
v6 = *a1 - v4;
if ( v6 < 0 )
{
v10 = v6 & 1 | ((unsigned __int64)(*a1 - v4) >> 1);
v7 = (double)(int)v10 + (double)(int)v10;
if ( v4 >= 0 )
goto LABEL_5;
}
else
{
v7 = (double)(int)v6;
if ( v4 >= 0 )
{
LABEL_5:
v8 = (double)(int)v4;
return (unsigned int)(int)(v5 / v8 * v7);
}
}
v8 = (double)(int)(v4 & 1 | ((unsigned __int64)v4 >> 1)) + (double)(int)(v4 & 1 | ((unsigned __int64)v4 >> 1));
return (unsigned int)(int)(v5 / v8 * v7);
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1)
{
unsigned int v1; // er12
time_t v2; // r13
time_t v3; // rax
__int64 v4; // rbp
double v5; // xmm0_8
__int64 v6; // rax
double v7; // xmm1_8
double v8; // xmm2_8
unsigned __int64 v10; // rdx

v1 = 0;
v2 = a1[2];
v3 = time(0LL);
v4 = a1[1];
if ( v4 )
{
v5 = difftime(v3, v2);
if ( v5 > 0.0 )
{
v6 = *a1 - v4;
if ( v6 < 0 )
{
v10 = v6 & 1 | ((unsigned __int64)(*a1 - v4) >> 1);
v7 = (double)(int)v10 + (double)(int)v10;
if ( v4 >= 0 )
goto LABEL_5;
}
else
{
v7 = (double)(int)v6;
if ( v4 >= 0 )
{
LABEL_5:
v8 = (double)(int)v4;
return (unsigned int)(int)(v5 / v8 * v7);
}
}
v8 = (double)(int)(v4 & 1 | ((unsigned __int64)v4 >> 1)) + (double)(int)(v4 & 1 | ((unsigned __int64)v4 >> 1));
return (unsigned int)(int)(v5 / v8 * v7);
}
}
return v1;
}

original_code:
static int progressbar_remaining_seconds(const progressbar* bar) {
  double offset = difftime(time(((void *)0)), bar->start);
  if (bar->value > 0 && offset > 0) {
    return (offset / (double) bar->value) * (bar->max - bar->value);
  } else {
    return 0;
  }
}

canonical_original_code:
static int progressbar_remaining_seconds(struct _progressbar_t *bar) {
  double offset = difftime(time(((void *)0)), bar->start);
  if (bar->value > 0 && offset > 0) {
    return (offset / (double) bar->value) * (bar->max - bar->value);
  } else {
    return 0;
  }
}

variable_types: offset double

user_defined_types: struct _progressbar_t { unsigned long max; unsigned long value; long start; char *label; struct { char begin; char fill; char end; } format; }


decompiled_code:
int get_screen_width()
{
char *v0; // rax
int v1; // er8
int result; // eax
char v3[2056]; // [rsp+0h] [rbp-808h] BYREF

v0 = getenv("TERM");
v1 = tgetent(v3, v0);
result = 80;
if ( v1 >= 0 )
result = tgetnum("co");
return result;
}

canonical_decompiled_code:
int func6()
{
char *v0; // rax
int v1; // er8
int result; // eax
char v3[2056]; // [rsp+0h] [rbp-808h] BYREF

v0 = getenv("TERM");
v1 = tgetent(v3, v0);
result = 80;
if ( v1 >= 0 )
result = tgetnum("co");
return result;
}

original_code:
static unsigned int get_screen_width(void) {
  char termbuf[2048];
  if (tgetent(termbuf, getenv("TERM")) >= 0) {
    return tgetnum("co") ;
  } else {
    return DEFAULT_SCREEN_WIDTH;
  }
}

canonical_original_code:
static unsigned int get_screen_width() {
  char termbuf[2048];
  if (tgetent(termbuf, getenv("TERM")) >= 0) {
    return tgetnum("co") ;
  } else {
    return DEFAULT_SCREEN_WIDTH;
  }
}

variable_types:
termbuf
char [2048]

return_type: unsigned int


decompiled_code:
int __fastcall progressbar_draw(__int64 a1)
{
int v1; // ebp
int v2; // eax
int v3; // ebx
unsigned int v4; // ecx
int v5; // eax
__int64 v6; // rdx
int v7; // ebx
int v8; // er13
__int64 v9; // rax
double v10; // xmm0_8
double v11; // xmm1_8
int v12; // eax
__int64 v13; // r14
unsigned int v14; // edx
time_t v15; // rbp
time_t v16; // rax
double v17; // xmm0_8
unsigned int v18; // edx
__int64 v20; // rcx
__int64 v21; // rdx
__int64 v22; // [rsp+4h] [rbp-34h]
unsigned int v23; // [rsp+Ch] [rbp-2Ch]

v1 = get_screen_width();
v2 = strlen(*(const char **)(a1 + 24));
v3 = progressbar_bar_width(v1, v2);
v5 = progressbar_label_width(v1, v4, v3);
v6 = *(_QWORD *)(a1 + 8);
v7 = v3 - 2;
v8 = v5;
v9 = *(_QWORD *)a1;
if ( (unsigned __int64)v6 >= *(_QWORD *)a1 )
{
v15 = *(_QWORD *)(a1 + 16);
v13 = 0LL;
v16 = time(0LL);
v17 = difftime(v16, v15);
v22 = progressbar_calc_time_components((int)v17);
v23 = v18;
if ( !v8 )
goto LABEL_8;
goto LABEL_6;
}
if ( v6 < 0 )
{
v20 = *(_QWORD *)(a1 + 8) & 1LL | (*(_QWORD *)(a1 + 8) >> 1);
v10 = (double)(int)v20 + (double)(int)v20;
if ( v9 >= 0 )
goto LABEL_4;
}
else
{
v10 = (double)(int)v6;
if ( v9 >= 0 )
{
LABEL_4:
v11 = (double)(int)v9;
goto LABEL_5;
}
}
v21 = *(_QWORD *)a1 & 1LL | (*(_QWORD *)a1 >> 1);
v11 = (double)(int)v21 + (double)(int)v21;
LABEL_5:
v12 = progressbar_remaining_seconds((_QWORD *)a1);
v22 = progressbar_calc_time_components(v12);
v13 = v7 - (int)(v10 / v11 * (double)v7);
v23 = v14;
v7 = (int)(v10 / v11 * (double)v7);
if ( v8 )
{
LABEL_6:
fwrite(*(const void **)(a1 + 24), 1uLL, v8, stderr);
fputc(32, stderr);
}
LABEL_8:
fputc(*(char *)(a1 + 32), stderr);
progressbar_write_char(stderr, *(char *)(a1 + 33), v7);
progressbar_write_char(stderr, 32, v13);
fputc(*(char *)(a1 + 34), stderr);
fputc(32, stderr);
fprintf(stderr, "ETA:%2dh%02dm%02ds", (unsigned int)v22, HIDWORD(v22), v23);
return fputc(13, stderr);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1)
{
int v1; // ebp
int v2; // eax
int v3; // ebx
unsigned int v4; // ecx
int v5; // eax
__int64 v6; // rdx
int v7; // ebx
int v8; // er13
__int64 v9; // rax
double v10; // xmm0_8
double v11; // xmm1_8
int v12; // eax
__int64 v13; // r14
unsigned int v14; // edx
time_t v15; // rbp
time_t v16; // rax
double v17; // xmm0_8
unsigned int v18; // edx
__int64 v20; // rcx
__int64 v21; // rdx
__int64 v22; // [rsp+4h] [rbp-34h]
unsigned int v23; // [rsp+Ch] [rbp-2Ch]

v1 = func6();
v2 = strlen(*(const char **)(a1 + 24));
v3 = func1(v1, v2);
v5 = func2(v1, v4, v3);
v6 = *(_QWORD *)(a1 + 8);
v7 = v3 - 2;
v8 = v5;
v9 = *(_QWORD *)a1;
if ( (unsigned __int64)v6 >= *(_QWORD *)a1 )
{
v15 = *(_QWORD *)(a1 + 16);
v13 = 0LL;
v16 = time(0LL);
v17 = difftime(v16, v15);
v22 = func3((int)v17);
v23 = v18;
if ( !v8 )
goto LABEL_8;
goto LABEL_6;
}
if ( v6 < 0 )
{
v20 = *(_QWORD *)(a1 + 8) & 1LL | (*(_QWORD *)(a1 + 8) >> 1);
v10 = (double)(int)v20 + (double)(int)v20;
if ( v9 >= 0 )
goto LABEL_4;
}
else
{
v10 = (double)(int)v6;
if ( v9 >= 0 )
{
LABEL_4:
v11 = (double)(int)v9;
goto LABEL_5;
}
}
v21 = *(_QWORD *)a1 & 1LL | (*(_QWORD *)a1 >> 1);
v11 = (double)(int)v21 + (double)(int)v21;
LABEL_5:
v12 = func4((_QWORD *)a1);
v22 = func3(v12);
v13 = v7 - (int)(v10 / v11 * (double)v7);
v23 = v14;
v7 = (int)(v10 / v11 * (double)v7);
if ( v8 )
{
LABEL_6:
fwrite(*(const void **)(a1 + 24), 1uLL, v8, stderr);
fputc(32, stderr);
}
LABEL_8:
fputc(*(char *)(a1 + 32), stderr);
func5(stderr, *(char *)(a1 + 33), v7);
func5(stderr, 32, v13);
fputc(*(char *)(a1 + 34), stderr);
fputc(32, stderr);
fprintf(stderr, "ETA:%2dh%02dm%02ds", (unsigned int)v22, HIDWORD(v22), v23);
return fputc(13, stderr);
}

original_code:
static void progressbar_draw(const progressbar *bar)
{
  int screen_width = get_screen_width();
  int label_length = strlen(bar->label);
  int bar_width = progressbar_bar_width(screen_width, label_length);
  int label_width = progressbar_label_width(screen_width, label_length, bar_width);
  int progressbar_completed = (bar->value >= bar->max);
  int bar_piece_count = bar_width - BAR_BORDER_WIDTH;
  int bar_piece_current = (progressbar_completed)
                          ? bar_piece_count
                          : bar_piece_count * ((double) bar->value / bar->max);
  progressbar_time_components eta = (progressbar_completed)
                              ? progressbar_calc_time_components(difftime(time(((void *)0)), bar->start))
                              : progressbar_calc_time_components(progressbar_remaining_seconds(bar));
  if (label_width == 0) {
    bar_width += 1;
  } else {
    fwrite(bar->label, 1, label_width, stderr);
    fputc(' ', stderr);
  }
  fputc(bar->format.begin, stderr);
  progressbar_write_char(stderr, bar->format.fill, bar_piece_current);
  progressbar_write_char(stderr, ' ', bar_piece_count - bar_piece_current);
  fputc(bar->format.end, stderr);
  fputc(' ', stderr);
  fprintf(stderr, ETA_FORMAT, eta.hours, eta.minutes, eta.seconds);
  fputc('\r', stderr);
}

canonical_original_code:
static void progressbar_draw(struct _progressbar_t *bar)
{
  int screen_width = get_screen_width();
  int label_length = strlen(bar->label);
  int bar_width = progressbar_bar_width(screen_width, label_length);
  int label_width = progressbar_label_width(screen_width, label_length, bar_width);
  int progressbar_completed = (bar->value >= bar->max);
  int bar_piece_count = bar_width - BAR_BORDER_WIDTH;
  int bar_piece_current = (progressbar_completed)
                          ? bar_piece_count
                          : bar_piece_count * ((double) bar->value / bar->max);
  struct progressbar_time_components eta = (progressbar_completed)
                              ? progressbar_calc_time_components(difftime(time(((void *)0)), bar->start))
                              : progressbar_calc_time_components(progressbar_remaining_seconds(bar));
  if (label_width == 0) {
    bar_width += 1;
  } else {
    fwrite(bar->label, 1, label_width, stderr);
    fputc(' ', stderr);
  }
  fputc(bar->format.begin, stderr);
  progressbar_write_char(stderr, bar->format.fill, bar_piece_current);
  progressbar_write_char(stderr, ' ', bar_piece_count - bar_piece_current);
  fputc(bar->format.end, stderr);
  fputc(' ', stderr);
  fprintf(stderr, ETA_FORMAT, eta.hours, eta.minutes, eta.seconds);
  fputc('\r', stderr);
}

user_defined_types: struct progressbar_time_components { int hours; int minutes; int seconds; }


decompiled_code:
void __fastcall progressbar_update_label(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 24) = a2;
}

original_code:
void progressbar_update_label(progressbar *bar, const char *label)
{
  bar->label = label;
}

canonical_original_code:
void progressbar_update_label(struct _progressbar_t *bar, char *label)
{
  bar->label = label;
}

variable_types:
label
char *


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

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

original_code:
void progressbar_free(progressbar *bar)
{
  free(bar);
}

canonical_original_code:
void progressbar_free(struct _progressbar_t *bar)
{
  free(bar);
}


decompiled_code:
int __fastcall progressbar_update(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
return progressbar_draw(a1);
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
return func7(a1);
}

original_code:
void progressbar_update(progressbar *bar, unsigned long value)
{
  bar->value = value;
  progressbar_draw(bar);
}

canonical_original_code:
void progressbar_update(struct _progressbar_t *bar, unsigned long value)
{
  bar->value = value;
  progressbar_draw(bar);
}

variable_types:
value
unsigned long


decompiled_code:
int __fastcall progressbar_inc(__int64 a1)
{
return progressbar_update(a1, *(_QWORD *)(a1 + 8) + 1LL);
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1)
{
return func12(a1, *(_QWORD *)(a1 + 8) + 1LL);
}

original_code:
void progressbar_inc(progressbar *bar)
{
  progressbar_update(bar, bar->value+1);
}

canonical_original_code:
void progressbar_inc(struct _progressbar_t *bar)
{
  progressbar_update(bar, bar->value+1);
}


decompiled_code:
void __fastcall progressbar_finish(void *ptr)
{
progressbar_draw((__int64)ptr);
fputc(10, stderr);
progressbar_free(ptr);
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
func7((__int64)ptr);
fputc(10, stderr);
func11(ptr);
}

original_code:
void progressbar_finish(progressbar *bar)
{
  progressbar_draw(bar);
  fprintf(stderr, "\n");
  progressbar_free(bar);
}

canonical_original_code:
void progressbar_finish(struct _progressbar_t *bar)
{
  progressbar_draw(bar);
  fprintf(stderr, "\n");
  progressbar_free(bar);
}


decompiled_code:
void __fastcall statusbar_free(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
void statusbar_free(statusbar *bar)
{
  free(bar->format);
  free(bar);
  return;
}

canonical_original_code:
void statusbar_free(struct _statusbar_t *bar)
{
  free(bar->format);
  free(bar);
  return;
}

user_defined_types: struct _statusbar_t { unsigned int start_time; char *label; int format_index; int format_length; char *format; int last_printed; }


decompiled_code:
int __fastcall statusbar_draw(__int64 a1)
{
int result; // eax

fputc(13, stderr);
result = fprintf(
stderr,
"%s: %c",
*(const char **)(a1 + 8),
(unsigned int)*(char *)(*(_QWORD *)(a1 + 24) + *(int *)(a1 + 16)));
*(_DWORD *)(a1 + 32) = result;
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
int result; // eax

fputc(13, stderr);
result = fprintf(
stderr,
"%s: %c",
*(const char **)(a1 + 8),
(unsigned int)*(char *)(*(_QWORD *)(a1 + 24) + *(int *)(a1 + 16)));
*(_DWORD *)(a1 + 32) = result;
return result;
}

original_code:
void statusbar_draw(statusbar *bar)
{
  fprintf(stderr,"\r");
  bar->last_printed = fprintf(
        stderr,
        "%s: %c",
        bar->label,
        bar->format[bar->format_index]
    );
  return;
}

canonical_original_code:
void statusbar_draw(struct _statusbar_t *bar)
{
  fprintf(stderr,"\r");
  bar->last_printed = fprintf(
        stderr,
        "%s: %c",
        bar->label,
        bar->format[bar->format_index]
    );
  return;
}


decompiled_code:
int __fastcall statusbar_inc(__int64 a1)
{
int v1; // eax

v1 = *(_DWORD *)(a1 + 16) + 1;
*(_DWORD *)(a1 + 16) = v1;
if ( v1 >= *(_DWORD *)(a1 + 20) )
*(_DWORD *)(a1 + 16) = 0;
return statusbar_draw(a1);
}

original_code:
void statusbar_inc(statusbar *bar)
{
  bar->format_index++;
  if (bar->format_index >= bar->format_length) {
    bar->format_index = 0;
  }
  statusbar_draw(bar);
  return;
}

canonical_original_code:
void statusbar_inc(struct _statusbar_t *bar)
{
  bar->format_index++;
  if (bar->format_index >= bar->format_length) {
    bar->format_index = 0;
  }
  statusbar_draw(bar);
  return;
}


decompiled_code:
void __fastcall statusbar_finish(void *ptr)
{
unsigned int v1; // ebx
unsigned int v2; // er12
unsigned int v3; // er13
int v4; // eax
FILE *v5; // rsi

v1 = time(0LL) - *(_DWORD *)ptr;
v2 = v1 / 0xE10;
v1 %= 0xE10u;
v3 = v1 / 0x3C;
v1 %= 0x3Cu;
fputc(13, stderr);
v4 = fprintf(stderr, "%s: %3d:%02d:%02d", *((const char **)ptr + 1), v2, v3, v1);
v5 = stderr;
*((_DWORD *)ptr + 8) = v4;
fputc(13, v5);
fprintf(stderr, "%s: ", *((const char **)ptr + 1));
fprintf(stderr, "%*s", 80 - *((_DWORD *)ptr + 8), (const char *)&unk_237);
fprintf(stderr, "%3d:%02d:%02d\n", v2, v3, v1);
statusbar_free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
unsigned int v1; // ebx
unsigned int v2; // er12
unsigned int v3; // er13
int v4; // eax
FILE *v5; // rsi

v1 = time(0LL) - *(_DWORD *)ptr;
v2 = v1 / 0xE10;
v1 %= 0xE10u;
v3 = v1 / 0x3C;
v1 %= 0x3Cu;
fputc(13, stderr);
v4 = fprintf(stderr, "%s: %3d:%02d:%02d", *((const char **)ptr + 1), v2, v3, v1);
v5 = stderr;
*((_DWORD *)ptr + 8) = v4;
fputc(13, v5);
fprintf(stderr, "%s: ", *((const char **)ptr + 1));
fprintf(stderr, "%*s", 80 - *((_DWORD *)ptr + 8), (const char *)&unk_237);
fprintf(stderr, "%3d:%02d:%02d\n", v2, v3, v1);
func0(ptr);
}

original_code:
void statusbar_finish(statusbar *bar)
{
  unsigned int offset = time(0) - (bar->start_time);
  unsigned int h = offset/3600;
  offset -= h*3600;
  unsigned int m = offset/60;
  offset -= m*60;
  unsigned int s = offset;
  fprintf(stderr,"\r");
  bar->last_printed = fprintf(stderr,"%s: %3d:%02d:%02d",bar->label,h,m,s);
  fprintf(stderr,"\r");
  fprintf(stderr,"%s: ",bar->label);
  fprintf(stderr,"%*s",80 - (bar->last_printed),"");
  fprintf(stderr,"%3d:%02d:%02d\n",h,m,s);
  statusbar_free(bar);
  return;
}

canonical_original_code:
void statusbar_finish(struct _statusbar_t *bar)
{
  unsigned int offset = time(0) - (bar->start_time);
  unsigned int h = offset/3600;
  offset -= h*3600;
  unsigned int m = offset/60;
  offset -= m*60;
  unsigned int s = offset;
  fprintf(stderr,"\r");
  bar->last_printed = fprintf(stderr,"%s: %3d:%02d:%02d",bar->label,h,m,s);
  fprintf(stderr,"\r");
  fprintf(stderr,"%s: ",bar->label);
  fprintf(stderr,"%*s",80 - (bar->last_printed),"");
  fprintf(stderr,"%3d:%02d:%02d\n",h,m,s);
  statusbar_free(bar);
  return;
}

variable_types:
offset
unsigned int

variable_types:
h
unsigned int

variable_types:
m
unsigned int

variable_types:
s
unsigned int


repo: dodgerblue/waldo

decompiled_code:
__int64 __fastcall get_max_message_length(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, int a10)
{
return a1 / a10;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, int a10)
{
return a1 / a10;
}

original_code:
long int get_max_message_length(struct cipher_method_ m, long int data_size) {
 return data_size / m.ratio;
}

canonical_original_code:
long get_max_message_length(struct cipher_method_ m, long data_size) {
 return data_size / m.ratio;
}

variable_types: struct cipher_method_ { unsigned int id; char *codename; char *description; int ratio; char mask; }

return_type: long


original_code:
int message_fits(struct bitmap_image_ *image, struct wrapped_message_ *msg, struct cipher_method_ m) {
 long int max_length = get_max_message_length(m, image->data_size);
 return msg->msg_length <= max_length;
}

canonical_original_code:
int message_fits(struct bitmap_image_ *image, struct wrapped_message_ *msg, struct cipher_method_ m) {
 long max_length = get_max_message_length(m, image->data_size);
 return msg->msg_length <= max_length;
}

variable_types: struct bitmap_image_ { struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; } *bfh; struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; } *bih; long data_size; char *data; long trailer_size; char *trailer; } *

variable_types: struct wrapped_message_ { unsigned int hash_id; unsigned int msg_length; char buffer[]; } *

user_defined_types: struct cipher_method_ { unsigned int id; char *codename; char *description; int ratio; char mask; }

user_defined_types: struct bitmap_image_ { struct bitmap_file_header_ *bfh; struct bitmap_info_header_ *bih; long data_size; char *data; long trailer_size; char *trailer; }


decompiled_code:
__int64 __fastcall is_valid_cipher_method(int a1)
{
int v1; // eax
int *v2; // rdx

v1 = cipher_methods;
if ( cipher_methods == -1 )
return 0LL;
v2 = (int *)(unsigned int)((_DWORD)&cipher_methods + 32);
while ( a1 != v1 )
{
v1 = *v2;
v2 += 8;
if ( v1 == -1 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // eax
int *v2; // rdx

v1 = cipher_methods;
if ( cipher_methods == -1 )
return 0LL;
v2 = (int *)(unsigned int)((_DWORD)&cipher_methods + 32);
while ( a1 != v1 )
{
v1 = *v2;
v2 += 8;
if ( v1 == -1 )
return 0LL;
}
return 1LL;
}

original_code:
int is_valid_cipher_method(int cipher_type) {
 int i;
 for (i = 0; cipher_methods[i].id != (0x7fffffff * 2U + 1U); i++) {
  if (cipher_methods[i].id == cipher_type)
   return 1;
 }
 return 0;
}

variable_types: i int


decompiled_code:
void *__fastcall hash_message(const char *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, char size_4)
{
size_t v8; // r13
int v9; // eax
int v10; // ebp
int v11; // ebp
void *v12; // rax
void *v13; // r12
char v15[136]; // [rsp+0h] [rbp-88h] BYREF

v8 = strlen(a1);
v9 = open("pure_vitamin_c", 66, 438LL);
if ( v9 == -1 )
{
v13 = 0LL;
fprintf(stderr, "Unable to open temporary file %s\n", "pure_vitamin_c");
return v13;
}
v10 = v9;
if ( write(v9, a1, v8) == -1 )
{
v13 = 0LL;
fprintf(stderr, "Unable to write to temporary file %s\n", "pure_vitamin_c");
close(v10);
return v13;
}
close(v10);
snprintf(v15, 0x64uLL, "%s %s > %s", &size_4, "pure_vitamin_c", "tis_but_a_temp");
if ( system(v15) == -1 )
{
v13 = 0LL;
fwrite("Execution of system command failed\n", 1uLL, 0x23uLL, stderr);
return v13;
}
v11 = open("tis_but_a_temp", 0);
if ( v11 == -1 )
{
v13 = 0LL;
fwrite("Unable to open the temporary file for reading the hash\n", 1uLL, 0x37uLL, stderr);
return v13;
}
v12 = malloc(HIDWORD(a7));
v13 = v12;
if ( !v12 )
{
fwrite("Unable to alloc memory for hashed string\n", 1uLL, 0x29uLL, stderr);
return v13;
}
if ( read(v11, v12, HIDWORD(a7)) == -1 )
{
fwrite("Unable to read hash from temporary file\n", 1uLL, 0x28uLL, stderr);
close(v11);
}
else
{
close(v11);
if ( unlink("pure_vitamin_c") == -1 )
{
fprintf(stderr, "Unable to delete %s\n", "pure_vitamin_c");
}
else
{
if ( unlink("tis_but_a_temp") != -1 )
return v13;
fprintf(stderr, "Unable to delete %s\n", "tis_but_a_temp");
}
}
free(v13);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(const char *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, char size_4)
{
size_t v8; // r13
int v9; // eax
int v10; // ebp
int v11; // ebp
void *v12; // rax
void *v13; // r12
char v15[136]; // [rsp+0h] [rbp-88h] BYREF

v8 = strlen(a1);
v9 = open("pure_vitamin_c", 66, 438LL);
if ( v9 == -1 )
{
v13 = 0LL;
fprintf(stderr, "Unable to open temporary file %s\n", "pure_vitamin_c");
return v13;
}
v10 = v9;
if ( write(v9, a1, v8) == -1 )
{
v13 = 0LL;
fprintf(stderr, "Unable to write to temporary file %s\n", "pure_vitamin_c");
close(v10);
return v13;
}
close(v10);
snprintf(v15, 0x64uLL, "%s %s > %s", &size_4, "pure_vitamin_c", "tis_but_a_temp");
if ( system(v15) == -1 )
{
v13 = 0LL;
fwrite("Execution of system command failed\n", 1uLL, 0x23uLL, stderr);
return v13;
}
v11 = open("tis_but_a_temp", 0);
if ( v11 == -1 )
{
v13 = 0LL;
fwrite("Unable to open the temporary file for reading the hash\n", 1uLL, 0x37uLL, stderr);
return v13;
}
v12 = malloc(HIDWORD(a7));
v13 = v12;
if ( !v12 )
{
fwrite("Unable to alloc memory for hashed string\n", 1uLL, 0x29uLL, stderr);
return v13;
}
if ( read(v11, v12, HIDWORD(a7)) == -1 )
{
fwrite("Unable to read hash from temporary file\n", 1uLL, 0x28uLL, stderr);
close(v11);
}
else
{
close(v11);
if ( unlink("pure_vitamin_c") == -1 )
{
fprintf(stderr, "Unable to delete %s\n", "pure_vitamin_c");
}
else
{
if ( unlink("tis_but_a_temp") != -1 )
return v13;
fprintf(stderr, "Unable to delete %s\n", "tis_but_a_temp");
}
}
free(v13);
return 0LL;
}

original_code:
char *hash_message(struct hash_method_ hm, char *message) {
 char cmd[100], *result;
 int fd = -1, ret = -1;
 size_t length = strlen(message);
 ssize_t rw_bytes = 0;
 fd = open("pure_vitamin_c", 02 | 0100, 0666);
 if (fd == -1) {
  fprintf(stderr, "Unable to open temporary file %s\n", "pure_vitamin_c");
  goto out_fail;
 }
 rw_bytes = write(fd, message, length);
 if (rw_bytes == -1) {
  fprintf(stderr, "Unable to write to temporary file %s\n", "pure_vitamin_c");
  close(fd);
  goto out_fail;
 }
 close(fd);
 snprintf(cmd, 100, "%s %s > %s", hm.cmd, "pure_vitamin_c", "tis_but_a_temp");
 ret = system((const char *) cmd);
 if (ret == -1) {
  fprintf(stderr, "Execution of system command failed\n");
  goto out_fail;
 }
 fd = open("tis_but_a_temp", 00);
 if (fd == -1) {
  fprintf(stderr, "Unable to open the temporary file for reading the hash\n");
  goto out_fail;
 }
 result = malloc(hm.hash_length);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for hashed string\n");
  goto out_fail;
 }
 rw_bytes = read(fd, result, hm.hash_length);
 if (rw_bytes == -1) {
  fprintf(stderr, "Unable to read hash from temporary file\n");
  goto out_fail_read;
 }
 close(fd);
 if (unlink("pure_vitamin_c") == -1) {
  fprintf(stderr, "Unable to delete %s\n", "pure_vitamin_c");
  goto out_free_result;
 }
 if (unlink("tis_but_a_temp") == -1) {
  fprintf(stderr, "Unable to delete %s\n", "tis_but_a_temp");
  goto out_free_result;
 }
 return result;
out_fail_read:
 close(fd);
out_free_result:
 free(result);
out_fail:
 return ((void *)0);
}

canonical_original_code:
char *hash_message(struct hash_method_ hm, char *message) {
 char cmd[100]; char *result;
 int fd = -1; int ret = -1;
 unsigned long length = strlen(message);
 long rw_bytes = 0;
 fd = open("pure_vitamin_c", 02 | 0100, 0666);
 if (fd == -1) {
  fprintf(stderr, "Unable to open temporary file %s\n", "pure_vitamin_c");
  goto out_fail;
 }
 rw_bytes = write(fd, message, length);
 if (rw_bytes == -1) {
  fprintf(stderr, "Unable to write to temporary file %s\n", "pure_vitamin_c");
  close(fd);
  goto out_fail;
 }
 close(fd);
 snprintf(cmd, 100, "%s %s > %s", hm.cmd, "pure_vitamin_c", "tis_but_a_temp");
 ret = system((char *) cmd);
 if (ret == -1) {
  fprintf(stderr, "Execution of system command failed\n");
  goto out_fail;
 }
 fd = open("tis_but_a_temp", 00);
 if (fd == -1) {
  fprintf(stderr, "Unable to open the temporary file for reading the hash\n");
  goto out_fail;
 }
 result = malloc(hm.hash_length);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for hashed string\n");
  goto out_fail;
 }
 rw_bytes = read(fd, result, hm.hash_length);
 if (rw_bytes == -1) {
  fprintf(stderr, "Unable to read hash from temporary file\n");
  goto out_fail_read;
 }
 close(fd);
 if (unlink("pure_vitamin_c") == -1) {
  fprintf(stderr, "Unable to delete %s\n", "pure_vitamin_c");
  goto out_free_result;
 }
 if (unlink("tis_but_a_temp") == -1) {
  fprintf(stderr, "Unable to delete %s\n", "tis_but_a_temp");
  goto out_free_result;
 }
 return result;
out_fail_read:
 close(fd);
out_free_result:
 free(result);
out_fail:
 return ((void *)0);
}

variable_types: struct hash_method_ { unsigned int id; unsigned int hash_length; char cmd[20]; char name[20]; }

variable_types:
message
char *

variable_types:
cmd
char [100]

variable_types:
result
char *

variable_types: fd int

variable_types: ret int

variable_types:
length
unsigned long

return_type: char *


decompiled_code:
void *__fastcall wrap_message(const char *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, char a8)
{
size_t v8; // rax
size_t v9; // rbx
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
__int64 v13; // r9
void *v14; // r12
const char *v15; // rbp
char *v16; // rax
void *v18; // rdi

v8 = strlen(a1);
if ( v8 )
{
v9 = v8;
v14 = malloc(HIDWORD(a7) + v8 + 8);
if ( v14 )
{
v15 = (const char *)hash_message(a1, a2, v10, v11, v12, v13, a7, a8);
if ( v15 )
{
*(_DWORD *)v14 = a7;
*((_DWORD *)v14 + 1) = HIDWORD(a7) + v9 + 8;
v16 = strncpy((char *)v14 + 8, a1, v9);
strncpy(&v16[v9], v15, HIDWORD(a7));
}
else
{
fwrite("Unable to hash message\n", 1uLL, 0x17uLL, stderr);
v18 = v14;
v14 = 0LL;
free(v18);
}
}
else
{
fwrite("Unable to alloc memory for the wrapped message\n", 1uLL, 0x2FuLL, stderr);
}
}
else
{
v14 = 0LL;
fwrite("Unable to wrap message of length 0\n", 1uLL, 0x23uLL, stderr);
}
return v14;
}

canonical_decompiled_code:
void *__fastcall func4(const char *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, char a8)
{
size_t v8; // rax
size_t v9; // rbx
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
__int64 v13; // r9
void *v14; // r12
const char *v15; // rbp
char *v16; // rax
void *v18; // rdi

v8 = strlen(a1);
if ( v8 )
{
v9 = v8;
v14 = malloc(HIDWORD(a7) + v8 + 8);
if ( v14 )
{
v15 = (const char *)func3(a1, a2, v10, v11, v12, v13, a7, a8);
if ( v15 )
{
*(_DWORD *)v14 = a7;
*((_DWORD *)v14 + 1) = HIDWORD(a7) + v9 + 8;
v16 = strncpy((char *)v14 + 8, a1, v9);
strncpy(&v16[v9], v15, HIDWORD(a7));
}
else
{
fwrite("Unable to hash message\n", 1uLL, 0x17uLL, stderr);
v18 = v14;
v14 = 0LL;
free(v18);
}
}
else
{
fwrite("Unable to alloc memory for the wrapped message\n", 1uLL, 0x2FuLL, stderr);
}
}
else
{
v14 = 0LL;
fwrite("Unable to wrap message of length 0\n", 1uLL, 0x23uLL, stderr);
}
return v14;
}

original_code:
char *wrap_message(struct hash_method_ hm, char *message) {
 struct wrapped_message_ *result = ((void *)0);
 size_t msg_length = strlen(message);
 char *hash = ((void *)0);
 if (msg_length == 0) {
  fprintf(stderr, "Unable to wrap message of length 0\n");
  goto out_fail;
 }
 result = malloc(sizeof(struct wrapped_message_) + msg_length + hm.hash_length);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for the wrapped message\n");
  goto out_fail;
 }
 hash = hash_message(hm, message);
 if (hash == ((void *)0)) {
  fprintf(stderr, "Unable to hash message\n");
  goto out_fail_hash;
 }
 result->hash_id = hm.id;
 result->msg_length = sizeof(struct wrapped_message_) + msg_length + hm.hash_length;
 strncpy(result->buffer, message, msg_length);
 strncpy(result->buffer + msg_length, hash, hm.hash_length);
 return (char *)result;
out_fail_hash:
 free(result);
out_fail:
 return ((void *)0);
}

canonical_original_code:
char *wrap_message(struct hash_method_ hm, char *message) {
 struct wrapped_message_ *result = ((void *)0);
 unsigned long msg_length = strlen(message);
 char *hash = ((void *)0);
 if (msg_length == 0) {
  fprintf(stderr, "Unable to wrap message of length 0\n");
  goto out_fail;
 }
 result = malloc(sizeof(struct wrapped_message_) + msg_length + hm.hash_length);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for the wrapped message\n");
  goto out_fail;
 }
 hash = hash_message(hm, message);
 if (hash == ((void *)0)) {
  fprintf(stderr, "Unable to hash message\n");
  goto out_fail_hash;
 }
 result->hash_id = hm.id;
 result->msg_length = sizeof(struct wrapped_message_) + msg_length + hm.hash_length;
 strncpy(result->buffer, message, msg_length);
 strncpy(result->buffer + msg_length, hash, hm.hash_length);
 return (char *)result;
out_fail_hash:
 free(result);
out_fail:
 return ((void *)0);
}

variable_types:
hash
char *

user_defined_types: struct hash_method_ { unsigned int id; unsigned int hash_length; char cmd[20]; char name[20]; }

user_defined_types: struct wrapped_message_ { unsigned int hash_id; unsigned int msg_length; char buffer[]; }


decompiled_code:
int __fastcall print_wrapped_message(unsigned int *a1)
{
unsigned int v2; // er12
char *v3; // rbx
__int64 v4; // r13
int v5; // edi
unsigned int v6; // ebx
__int64 v7; // rax

if ( !a1 )
return fwrite("Unable to print NULL message structure", 1uLL, 0x26uLL, stderr);
v2 = a1[1] - 8 - hash_methods[12 * *a1 + 1];
puts("Wrapped message:");
printf("Hash type: %s\n", (const char *)&hash_methods[12 * *a1 + 7]);
printf("Total message length: %u\n", a1[1]);
printf("Message: ");
if ( v2 )
{
v3 = (char *)(a1 + 2);
v4 = (__int64)a1 + v2 + 8;
do
{
v5 = *v3++;
putchar(v5);
}
while ( v3 != (char *)v4 );
}
putchar(10);
printf("Hash: ");
if ( v2 < v2 + hash_methods[12 * *a1 + 1] )
{
v6 = v2;
do
{
v7 = v6++;
putchar(*((char *)a1 + v7 + 8));
}
while ( v2 + hash_methods[12 * *a1 + 1] > v6 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1)
{
unsigned int v2; // er12
char *v3; // rbx
__int64 v4; // r13
int v5; // edi
unsigned int v6; // ebx
__int64 v7; // rax

if ( !a1 )
return fwrite("Unable to print NULL message structure", 1uLL, 0x26uLL, stderr);
v2 = a1[1] - 8 - hash_methods[12 * *a1 + 1];
puts("Wrapped message:");
printf("Hash type: %s\n", (const char *)&hash_methods[12 * *a1 + 7]);
printf("Total message length: %u\n", a1[1]);
printf("Message: ");
if ( v2 )
{
v3 = (char *)(a1 + 2);
v4 = (__int64)a1 + v2 + 8;
do
{
v5 = *v3++;
putchar(v5);
}
while ( v3 != (char *)v4 );
}
putchar(10);
printf("Hash: ");
if ( v2 < v2 + hash_methods[12 * *a1 + 1] )
{
v6 = v2;
do
{
v7 = v6++;
putchar(*((char *)a1 + v7 + 8));
}
while ( v2 + hash_methods[12 * *a1 + 1] > v6 );
}
return putchar(10);
}

original_code:
void print_wrapped_message(struct wrapped_message_ *msg) {
 unsigned int i;
 unsigned int length;
 if (msg == ((void *)0)) {
  fprintf(stderr, "Unable to print NULL message structure");
  return;
 }
 length = msg->msg_length - sizeof(struct wrapped_message_) - hash_methods[msg->hash_id].hash_length;
 printf("Wrapped message:\n");
 printf("Hash type: %s\n", hash_methods[msg->hash_id].name);
 printf("Total message length: %u\n", msg->msg_length);
 printf("Message: ");
 for (i = 0; i < length; i ++) {
  printf("%c", msg->buffer[i]);
 }
 printf("\n");
 printf("Hash: ");
 for (i = length; i < length + hash_methods[msg->hash_id].hash_length; i ++) {
  printf("%c", msg->buffer[i]);
 }
 printf("\n");
}

variable_types:
i
unsigned int

variable_types:
length
unsigned int


decompiled_code:
__int64 __fastcall is_valid_hash_method(int a1)
{
int v1; // eax
int *v2; // rdx

v1 = hash_methods[0];
if ( hash_methods[0] == -1 )
return 0LL;
v2 = (int *)(unsigned int)((_DWORD)hash_methods + 48);
while ( a1 != v1 )
{
v1 = *v2;
v2 += 12;
if ( v1 == -1 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
int v1; // eax
int *v2; // rdx

v1 = hash_methods[0];
if ( hash_methods[0] == -1 )
return 0LL;
v2 = (int *)(unsigned int)((_DWORD)hash_methods + 48);
while ( a1 != v1 )
{
v1 = *v2;
v2 += 12;
if ( v1 == -1 )
return 0LL;
}
return 1LL;
}

original_code:
int is_valid_hash_method(int hash_type) {
 int i;
 for (i = 0; hash_methods[i].id != (0x7fffffff * 2U + 1U); i++) {
  if (hash_methods[i].id == hash_type)
   return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall split_byte(int a1, char *a2)
{
unsigned int v2; // ecx
unsigned int v3; // er8

a1 = (char)a1;
v2 = 0;
v3 = 0;
do
{
if ( !_bittest(&a1, v2) )
{
if ( (*a2 & 1) != 0 )
v3 += 1 << v2;
*a2 >>= 1;
}
++v2;
}
while ( v2 != 8 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, char *a2)
{
unsigned int v2; // ecx
unsigned int v3; // er8

a1 = (char)a1;
v2 = 0;
v3 = 0;
do
{
if ( !_bittest(&a1, v2) )
{
if ( (*a2 & 1) != 0 )
v3 += 1 << v2;
*a2 >>= 1;
}
++v2;
}
while ( v2 != 8 );
return v3;
}

original_code:
char split_byte(char mask, char *byte) {
 char result = 0;
 int i;
 for (i = 0; i < 8; i ++) {
  if (mask & (1 << i))
   continue;
  if (*byte & 1)
   result += 1 << i;
  *byte = *byte >> 1;
 }
 return result;
}

variable_types: mask char

variable_types:
byte
char *

variable_types: result char

return_type: char


decompiled_code:
void __fastcall merge_byte(int a1, char a2, _BYTE *a3)
{
unsigned int v3; // ecx
char v4; // al

a1 = (char)a1;
v3 = 7;
do
{
if ( !_bittest(&a1, v3) )
{
v4 = 2 * *a3;
if ( ((a2 >> v3) & 1) != 0 )
++v4;
*a3 = v4;
}
}
while ( v3-- != 0 );
}

canonical_decompiled_code:
void __fastcall func8(int a1, char a2, _BYTE *a3)
{
unsigned int v3; // ecx
char v4; // al

a1 = (char)a1;
v3 = 7;
do
{
if ( !_bittest(&a1, v3) )
{
v4 = 2 * *a3;
if ( ((a2 >> v3) & 1) != 0 )
++v4;
*a3 = v4;
}
}
while ( v3-- != 0 );
}

original_code:
void merge_byte(char mask, char byte, char *res) {
 int i;
 for (i = 7; i >= 0; i--) {
  if (mask & (1 << i))
   continue;
  *res = *res << 1;
  if (byte & (1 << i))
   *res = *res + 1;
 }
}

variable_types: byte char

variable_types:
res
char *


original_code:
void hide_byte(char *buf, char byte, struct cipher_method_ m) {
 int i;
 for (i = m.ratio - 1; i >= 0; i--) {
  buf[i] &= m.mask;
  buf[i] += split_byte(m.mask, &byte);
 }
}

variable_types:
buf
char *

variable_types: struct cipher_method_ { unsigned int id; char *codename; char *description; int ratio; char mask; }


original_code:
char reveal_byte(char *buf, struct cipher_method_ m) {
 char result = 0;
 int i;
 for (i = 0; i < m.ratio; i++) {
  merge_byte(m.mask, buf[i], &result);
 }
 return result;
}

variable_types: i int


original_code:
int zeroize_image(struct bitmap_image_ *image, struct cipher_method_ m) {
 long int i;
 for (i = 0; i + m.ratio < image->data_size; i+=m.ratio)
  hide_byte(image->data + i, 0, m);
 return 0;
}

canonical_original_code:
int zeroize_image(struct bitmap_image_ *image, struct cipher_method_ m) {
 long i;
 for (i = 0; i + m.ratio < image->data_size; i+=m.ratio)
  hide_byte(image->data + i, 0, m);
 return 0;
}

variable_types: struct bitmap_image_ { struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; } *bfh; struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; } *bih; long data_size; char *data; long trailer_size; char *trailer; } *

variable_types: i long


original_code:
int hide_message_in_image(struct bitmap_image_ *image, struct wrapped_message_ *msg, struct cipher_method_ m) {
 unsigned int length = msg->msg_length;
 unsigned int i;
 char *buf = (char*) msg;
 for (i = 0; i < length; i ++)
  hide_byte(image->data + i*m.ratio, buf[i], m);
 return 0;
}

variable_types: struct wrapped_message_ { unsigned int hash_id; unsigned int msg_length; char buffer[]; } *

user_defined_types: struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; }

user_defined_types: struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; }


decompiled_code:
_BOOL8 __fastcall validate_message(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, char a8)
{
int v8; // eax
size_t v9; // r14
char *v10; // rax
__int64 v11; // rbx
char *v12; // rbp
char *v13; // rax
char *v14; // r13
__int64 v15; // rdx
__int64 v16; // rcx
__int64 v17; // r8
__int64 v18; // r9
const char *v19; // rax
char *v20; // r14
_BOOL4 v21; // er12

v8 = *(_DWORD *)(a1 + 4) - HIDWORD(a7);
v9 = (unsigned int)(v8 - 8);
v10 = (char *)malloc((unsigned int)(v8 - 7));
if ( v10 )
{
v11 = a1 + 8;
v12 = v10;
memcpy(v10, (const void *)(a1 + 8), v9);
v12[v9] = 0;
v13 = (char *)malloc(HIDWORD(a7));
v14 = v13;
if ( v13 )
{
memcpy(v13, (const void *)(v11 + v9), HIDWORD(a7));
v19 = (const char *)hash_message(v12, v11 + v9, v15, v16, v17, v18, a7, a8);
v20 = (char *)v19;
if ( v19 )
{
v21 = strncmp(v14, v19, HIDWORD(a7)) == 0;
free(v20);
}
else
{
v21 = 0;
fwrite("Unable to hash message again\n", 1uLL, 0x1DuLL, stderr);
}
free(v14);
}
else
{
v21 = 0;
fwrite("Unable to alloc memory for message hash\n", 1uLL, 0x28uLL, stderr);
}
free(v12);
}
else
{
v21 = 0;
fwrite("Unable to alloc memory for message string\n", 1uLL, 0x2AuLL, stderr);
}
return v21;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, char a8)
{
int v8; // eax
size_t v9; // r14
char *v10; // rax
__int64 v11; // rbx
char *v12; // rbp
char *v13; // rax
char *v14; // r13
__int64 v15; // rdx
__int64 v16; // rcx
__int64 v17; // r8
__int64 v18; // r9
const char *v19; // rax
char *v20; // r14
_BOOL4 v21; // er12

v8 = *(_DWORD *)(a1 + 4) - HIDWORD(a7);
v9 = (unsigned int)(v8 - 8);
v10 = (char *)malloc((unsigned int)(v8 - 7));
if ( v10 )
{
v11 = a1 + 8;
v12 = v10;
memcpy(v10, (const void *)(a1 + 8), v9);
v12[v9] = 0;
v13 = (char *)malloc(HIDWORD(a7));
v14 = v13;
if ( v13 )
{
memcpy(v13, (const void *)(v11 + v9), HIDWORD(a7));
v19 = (const char *)func3(v12, v11 + v9, v15, v16, v17, v18, a7, a8);
v20 = (char *)v19;
if ( v19 )
{
v21 = strncmp(v14, v19, HIDWORD(a7)) == 0;
free(v20);
}
else
{
v21 = 0;
fwrite("Unable to hash message again\n", 1uLL, 0x1DuLL, stderr);
}
free(v14);
}
else
{
v21 = 0;
fwrite("Unable to alloc memory for message hash\n", 1uLL, 0x28uLL, stderr);
}
free(v12);
}
else
{
v21 = 0;
fwrite("Unable to alloc memory for message string\n", 1uLL, 0x2AuLL, stderr);
}
return v21;
}

original_code:
int validate_message(struct wrapped_message_ *msg, struct hash_method_ hm) {
 unsigned int text_length = msg->msg_length - sizeof(struct wrapped_message_) - hm.hash_length;
 char *message;
 char *hash;
 char *computed_hash;
 int ret = 1;
 message = malloc(text_length + 1);
 if (message == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for message string\n");
  ret = 0;
  goto out;
 }
 memcpy(message, msg->buffer, text_length);
 message[text_length] = '\0';
 hash = malloc(hm.hash_length);
 if (hash == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for message hash\n");
  ret = 0;
  goto out_free_message;
 }
 memcpy(hash, msg->buffer + text_length, hm.hash_length);
 computed_hash = hash_message(hm, message);
 if (computed_hash == ((void *)0)) {
  fprintf(stderr, "Unable to hash message again\n");
  ret = 0;
  goto out_free_hash;
 }
 if (strncmp(hash, computed_hash, hm.hash_length) != 0)
  ret = 0;
 free(computed_hash);
out_free_hash:
 free(hash);
out_free_message:
 free(message);
out:
 return ret;
}

variable_types: struct hash_method_ { unsigned int id; unsigned int hash_length; char cmd[20]; char name[20]; }

variable_types:
message
char *


decompiled_code:
__int64 __fastcall validate_message_all(unsigned int *a1, int a2)
{
unsigned int v2; // er12
__int64 v4; // rbx

if ( a2 != -1 )
return (unsigned int)validate_message(a1);
v2 = is_valid_hash_method(*a1);
if ( v2 )
return (unsigned int)validate_message(a1);
v4 = 0LL;
if ( hash_methods[0] == -1 )
return v2;
while ( !(unsigned int)validate_message(a1) )
{
++v4;
if ( hash_methods[12 * v4] == -1 )
return v2;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, int a2)
{
unsigned int v2; // er12
__int64 v4; // rbx

if ( a2 != -1 )
return (unsigned int)validate_message(a1);
v2 = is_valid_hash_method(*a1);
if ( v2 )
return (unsigned int)validate_message(a1);
v4 = 0LL;
if ( hash_methods[0] == -1 )
return v2;
while ( !(unsigned int)validate_message(a1) )
{
++v4;
if ( hash_methods[12 * v4] == -1 )
return v2;
}
return 1LL;
}

original_code:
int validate_message_all(struct wrapped_message_ *msg, int hash_id) {
 int i;
 if (hash_id != (0x7fffffff * 2U + 1U))
  return validate_message(msg, hash_methods[hash_id]);
 if (is_valid_hash_method(msg->hash_id))
  return validate_message(msg, hash_methods[msg->hash_id]);
 for (i = 0; hash_methods[i].id != (0x7fffffff * 2U + 1U); i++) {
  if (validate_message(msg, hash_methods[i]))
   return 1;
 }
 return 0;
}


decompiled_code:
unsigned int *__fastcall reveal_and_validate_message(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, const char *a8, const char *a9)
{
unsigned int *v9; // rax
unsigned int *v10; // r12
unsigned int *v12; // rdi

if ( debug )
printf("Trying scatter method: %s - %s\n", a8, a9);
v9 = (unsigned int *)recover_message_from_image(a1);
v10 = v9;
if ( v9 )
{
if ( !(unsigned int)validate_message_all(v9, a2) )
{
fwrite("Unable to validate the recovered message\n", 1uLL, 0x29uLL, stderr);
v12 = v10;
v10 = 0LL;
free(v12);
}
}
else
{
fwrite("Unable to recover the wrapped message from the image\n", 1uLL, 0x35uLL, stderr);
}
return v10;
}

canonical_decompiled_code:
unsigned int *__fastcall func2(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, const char *a8, const char *a9)
{
unsigned int *v9; // rax
unsigned int *v10; // r12
unsigned int *v12; // rdi

if ( debug )
printf("Trying scatter method: %s - %s\n", a8, a9);
v9 = (unsigned int *)recover_message_from_image(a1);
v10 = v9;
if ( v9 )
{
if ( !(unsigned int)func1(v9, a2) )
{
fwrite("Unable to validate the recovered message\n", 1uLL, 0x29uLL, stderr);
v12 = v10;
v10 = 0LL;
free(v12);
}
}
else
{
fwrite("Unable to recover the wrapped message from the image\n", 1uLL, 0x35uLL, stderr);
}
return v10;
}

original_code:
struct wrapped_message_* reveal_and_validate_message(struct bitmap_image_ *image, struct cipher_method_ cm, int hash_id) {
 struct wrapped_message_ *result = ((void *)0);
 if (debug)
  printf("Trying scatter method: %s - %s\n", cm.codename, cm.description);
 result = recover_message_from_image(image, cm);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to recover the wrapped message from the image\n");
  goto out_fail;
 }
 if (validate_message_all(result, hash_id) == 0) {
  fprintf(stderr, "Unable to validate the recovered message\n");
  free(result);
  goto out_fail;
 }
 return result;
out_fail:
 return ((void *)0);
}

canonical_original_code:
struct wrapped_message_ *reveal_and_validate_message(struct bitmap_image_ *image, struct cipher_method_ cm, int hash_id) {
 struct wrapped_message_ *result = ((void *)0);
 if (debug)
  printf("Trying scatter method: %s - %s\n", cm.codename, cm.description);
 result = recover_message_from_image(image, cm);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to recover the wrapped message from the image\n");
  goto out_fail;
 }
 if (validate_message_all(result, hash_id) == 0) {
  fprintf(stderr, "Unable to validate the recovered message\n");
  free(result);
  goto out_fail;
 }
 return result;
out_fail:
 return ((void *)0);
}

return_type: struct wrapped_message_ { unsigned int hash_id; unsigned int msg_length; char buffer[]; } *


decompiled_code:
unsigned int *__fastcall reveal_message_from_image(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
int v6; // er12
__int64 v7; // rbx
unsigned int *result; // rax

v6 = a3;
if ( debug )
{
if ( a2 == -1 )
{
puts("Trying all scatter methods");
if ( v6 != -1 )
goto LABEL_4;
}
else
{
printf(
"Trying scatter method: %s - %s\n",
*((const char **)&cipher_methods + 4 * a2 + 1),
*((const char **)&cipher_methods + 4 * a2 + 2));
if ( v6 != -1 )
{
LABEL_4:
printf("Trying hash method %s\n", (const char *)&hash_methods[12 * v6 + 7]);
goto LABEL_5;
}
}
puts("Trying all hash methods (or the one in the message header, in case it is valid)");
}
LABEL_5:
if ( a2 != -1 )
return reveal_and_validate_message(
a1,
v6,
a3,
a4,
a5,
a6,
*((_QWORD *)&cipher_methods + 4 * a2),
*((const char **)&cipher_methods + 4 * a2 + 1),
*((const char **)&cipher_methods + 4 * a2 + 2));
v7 = 0LL;
if ( cipher_methods == -1 )
return 0LL;
while ( 1 )
{
result = reveal_and_validate_message(
a1,
v6,
a3,
a4,
a5,
a6,
*((_QWORD *)&cipher_methods + 4 * (int)v7),
*((const char **)&cipher_methods + 4 * (int)v7 + 1),
*((const char **)&cipher_methods + 4 * (int)v7 + 2));
if ( result )
break;
++v7;
if ( *((_DWORD *)&cipher_methods + 8 * v7) == -1 )
return 0LL;
}
return result;
}

canonical_decompiled_code:
unsigned int *__fastcall func3(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
int v6; // er12
__int64 v7; // rbx
unsigned int *result; // rax

v6 = a3;
if ( debug )
{
if ( a2 == -1 )
{
puts("Trying all scatter methods");
if ( v6 != -1 )
goto LABEL_4;
}
else
{
printf(
"Trying scatter method: %s - %s\n",
*((const char **)&cipher_methods + 4 * a2 + 1),
*((const char **)&cipher_methods + 4 * a2 + 2));
if ( v6 != -1 )
{
LABEL_4:
printf("Trying hash method %s\n", (const char *)&hash_methods[12 * v6 + 7]);
goto LABEL_5;
}
}
puts("Trying all hash methods (or the one in the message header, in case it is valid)");
}
LABEL_5:
if ( a2 != -1 )
return func2(
a1,
v6,
a3,
a4,
a5,
a6,
*((_QWORD *)&cipher_methods + 4 * a2),
*((const char **)&cipher_methods + 4 * a2 + 1),
*((const char **)&cipher_methods + 4 * a2 + 2));
v7 = 0LL;
if ( cipher_methods == -1 )
return 0LL;
while ( 1 )
{
result = func2(
a1,
v6,
a3,
a4,
a5,
a6,
*((_QWORD *)&cipher_methods + 4 * (int)v7),
*((const char **)&cipher_methods + 4 * (int)v7 + 1),
*((const char **)&cipher_methods + 4 * (int)v7 + 2));
if ( result )
break;
++v7;
if ( *((_DWORD *)&cipher_methods + 8 * v7) == -1 )
return 0LL;
}
return result;
}

original_code:
struct wrapped_message_* reveal_message_from_image(struct bitmap_image_ *image, int scatter_id, int hash_id) {
 struct wrapped_message_ *result = ((void *)0);
 int i;
 if (debug) {
  if (scatter_id == (0x7fffffff * 2U + 1U)) printf("Trying all scatter methods\n");
  else printf("Trying scatter method: %s - %s\n", cipher_methods[scatter_id].codename, cipher_methods[scatter_id].description);
  if (hash_id == (0x7fffffff * 2U + 1U)) printf("Trying all hash methods (or the one in the message header, in case it is valid)\n");
  else printf("Trying hash method %s\n", hash_methods[hash_id].name);
 }
 if (scatter_id != (0x7fffffff * 2U + 1U))
  return reveal_and_validate_message(image, cipher_methods[scatter_id], hash_id);
 for (i = 0; cipher_methods[i].id != (0x7fffffff * 2U + 1U); i++)
  if ((result = reveal_and_validate_message(image, cipher_methods[i], hash_id)) != ((void *)0))
   return result;
 return ((void *)0);
}

canonical_original_code:
struct wrapped_message_ *reveal_message_from_image(struct bitmap_image_ *image, int scatter_id, int hash_id) {
 struct wrapped_message_ *result = ((void *)0);
 int i;
 if (debug) {
  if (scatter_id == (0x7fffffff * 2U + 1U)) printf("Trying all scatter methods\n");
  else printf("Trying scatter method: %s - %s\n", cipher_methods[scatter_id].codename, cipher_methods[scatter_id].description);
  if (hash_id == (0x7fffffff * 2U + 1U)) printf("Trying all hash methods (or the one in the message header, in case it is valid)\n");
  else printf("Trying hash method %s\n", hash_methods[hash_id].name);
 }
 if (scatter_id != (0x7fffffff * 2U + 1U))
  return reveal_and_validate_message(image, cipher_methods[scatter_id], hash_id);
 for (i = 0; cipher_methods[i].id != (0x7fffffff * 2U + 1U); i++)
  if ((result = reveal_and_validate_message(image, cipher_methods[i], hash_id)) != ((void *)0))
   return result;
 return ((void *)0);
}


decompiled_code:
__int64 __fastcall write_back_message(unsigned int *a1, const char *a2, int a3)
{
int v4; // eax
size_t v5; // r14
size_t v6; // r15
char *v7; // rax
char *v8; // rbp
int v9; // eax
int v10; // er12
__int64 result; // rax

v4 = a1[1] - hash_methods[12 * *a1 + 1];
v5 = (unsigned int)(v4 - 7);
v6 = (unsigned int)(v4 - 8);
v7 = (char *)malloc(v5);
if ( v7 )
{
v8 = v7;
memcpy(v7, a1 + 2, v6);
v8[v6] = 0;
if ( a3 )
{
printf("\nHIDDEN MESSAGE:\n%s\n", v8);
result = 1LL;
}
else
{
v9 = open(a2, 66, 438LL);
v10 = v9;
if ( v9 == -1 )
{
fwrite("Unable to open filename to store message\n", 1uLL, 0x29uLL, stderr);
}
else
{
if ( write(v9, v8, v5) == -1 )
fwrite("Unable to write the message to the file\n", 1uLL, 0x28uLL, stderr);
else
close(v10);
close(v10);
}
free(v8);
result = 1LL;
}
}
else
{
fwrite("Unable to alloc memory for message\n", 1uLL, 0x23uLL, stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, const char *a2, int a3)
{
int v4; // eax
size_t v5; // r14
size_t v6; // r15
char *v7; // rax
char *v8; // rbp
int v9; // eax
int v10; // er12
__int64 result; // rax

v4 = a1[1] - hash_methods[12 * *a1 + 1];
v5 = (unsigned int)(v4 - 7);
v6 = (unsigned int)(v4 - 8);
v7 = (char *)malloc(v5);
if ( v7 )
{
v8 = v7;
memcpy(v7, a1 + 2, v6);
v8[v6] = 0;
if ( a3 )
{
printf("\nHIDDEN MESSAGE:\n%s\n", v8);
result = 1LL;
}
else
{
v9 = open(a2, 66, 438LL);
v10 = v9;
if ( v9 == -1 )
{
fwrite("Unable to open filename to store message\n", 1uLL, 0x29uLL, stderr);
}
else
{
if ( write(v9, v8, v5) == -1 )
fwrite("Unable to write the message to the file\n", 1uLL, 0x28uLL, stderr);
else
close(v10);
close(v10);
}
free(v8);
result = 1LL;
}
}
else
{
fwrite("Unable to alloc memory for message\n", 1uLL, 0x23uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
int write_back_message(struct wrapped_message_ *msg, char *filename, int to_screen) {
 unsigned int msg_length = msg->msg_length - sizeof(struct wrapped_message_) - hash_methods[msg->hash_id].hash_length;
 char *message;
 int ret = 1;
 int fd;
 message = malloc(msg_length + 1);
 if (message == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for message\n");
  ret = 0;
  goto out;
 }
 memcpy(message, msg->buffer, msg_length);
 message[msg_length] = '\0';
 if (to_screen) {
  printf("\nHIDDEN MESSAGE:\n%s\n", message);
  goto out;
 }
 fd = open(filename, 02 | 0100, 0666);
 if (fd == -1) {
  fprintf(stderr, "Unable to open filename to store message\n");
  ret = 1;
  goto out_free_message;
 }
 if (write(fd, message, msg_length + 1) == -1) {
  fprintf(stderr, "Unable to write the message to the file\n");
  goto out_fail_write;
 }
 close(fd);
out_fail_write:
 close(fd);
out_free_message:
 free(message);
out:
 return ret;
}

variable_types:
filename
char *

variable_types: ret int

variable_types: fd int


decompiled_code:
int __fastcall print_usage(const char *a1)
{
__int64 v1; // rdx
const char *v2; // rbx
const char *v3; // rcx
__int64 v4; // rdx
const char **v5; // rbx
const char *v6; // r8
const char *v7; // rcx

fprintf(
stderr,
"Usage: %s [OPTIONS] <image> <filename>\n"
"\n"
" \timage - the image file with the hidden message\n"
" \tfilename - the file to store the read message to\n"
" \t\n"
" \tOPTIONS can be one or more of the following: \n"
" \t-h <hash method> - use this specific method for verifying the hash\n"
" \t-s <scatter method> - use this specific method to recover the message\n"
" \t-f - instead of storing the message into a file, output it to the console (the filename parameter is ignored)\n"
" \t\n",
a1);
fwrite("\tAvailable hash methods:\n", 1uLL, 0x19uLL, stderr);
v1 = hash_methods;
if ( hash_methods != -1 )
{
v2 = (const char *)(unsigned int)((_DWORD)&hash_methods + 28);
do
{
v3 = v2;
v2 += 48;
fprintf(stderr, "\t%u - %s\n", v1, v3);
v1 = *((unsigned int *)v2 - 7);
}
while ( (_DWORD)v1 != -1 );
}
fputc(10, stderr);
fwrite("\tAvailable scatter methods:\n", 1uLL, 0x1CuLL, stderr);
v4 = cipher_methods;
if ( cipher_methods != -1 )
{
v5 = (const char **)(unsigned int)((_DWORD)&cipher_methods + 8);
do
{
v6 = v5[1];
v7 = *v5;
v5 += 4;
fprintf(stderr, "\t%u - %s - %s\n", v4, v7, v6);
v4 = *((unsigned int *)v5 - 2);
}
while ( (_DWORD)v4 != -1 );
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
__int64 v1; // rdx
const char *v2; // rbx
const char *v3; // rcx
__int64 v4; // rdx
const char **v5; // rbx
const char *v6; // r8
const char *v7; // rcx

fprintf(
stderr,
"Usage: %s [OPTIONS] <image> <filename>\n"
"\n"
" \timage - the image file with the hidden message\n"
" \tfilename - the file to store the read message to\n"
" \t\n"
" \tOPTIONS can be one or more of the following: \n"
" \t-h <hash method> - use this specific method for verifying the hash\n"
" \t-s <scatter method> - use this specific method to recover the message\n"
" \t-f - instead of storing the message into a file, output it to the console (the filename parameter is ignored)\n"
" \t\n",
a1);
fwrite("\tAvailable hash methods:\n", 1uLL, 0x19uLL, stderr);
v1 = hash_methods;
if ( hash_methods != -1 )
{
v2 = (const char *)(unsigned int)((_DWORD)&hash_methods + 28);
do
{
v3 = v2;
v2 += 48;
fprintf(stderr, "\t%u - %s\n", v1, v3);
v1 = *((unsigned int *)v2 - 7);
}
while ( (_DWORD)v1 != -1 );
}
fputc(10, stderr);
fwrite("\tAvailable scatter methods:\n", 1uLL, 0x1CuLL, stderr);
v4 = cipher_methods;
if ( cipher_methods != -1 )
{
v5 = (const char **)(unsigned int)((_DWORD)&cipher_methods + 8);
do
{
v6 = v5[1];
v7 = *v5;
v5 += 4;
fprintf(stderr, "\t%u - %s - %s\n", v4, v7, v6);
v4 = *((unsigned int *)v5 - 2);
}
while ( (_DWORD)v4 != -1 );
}
return fputc(10, stderr);
}

original_code:
void print_usage(char *program_name) {
 int i;
 fprintf(stderr, "Usage: %s [OPTIONS] <image> <filename>\n\n 	image - the image file with the hidden message\n 	filename - the file to store the read message to\n 	\n 	OPTIONS can be one or more of the following: \n 	-h <hash method> - use this specific method for verifying the hash\n 	-s <scatter method> - use this specific method to recover the message\n 	-f - instead of storing the message into a file, output it to the console (the filename parameter is ignored)\n 	\n",
 program_name);
 fprintf(stderr, "\tAvailable hash methods:\n");
 for (i = 0; hash_methods[i].id != (0x7fffffff * 2U + 1U); i ++) {
  fprintf(stderr, "\t%u - %s\n", hash_methods[i].id, hash_methods[i].name);
 }
 fprintf(stderr, "\n");
 fprintf(stderr, "\tAvailable scatter methods:\n");
 for (i = 0; cipher_methods[i].id != (0x7fffffff * 2U + 1U); i ++) {
  fprintf(stderr, "\t%u - %s - %s\n", cipher_methods[i].id, cipher_methods[i].codename, cipher_methods[i].description);
 }
 fprintf(stderr, "\n");
}