decompiled_code:
__int64 eclib_test_button()
{
unsigned int v0; // eax
unsigned int v1; // er12
_DWORD *v2; // rbx
bool v3; // al
__int64 v4; // rcx

v0 = eclib_timer_version();
printf("%s: %s\tv.%x\n", "eclib_test_button.c", "May 23 2025", v0);
_counter_1ms = 1;
eclib_button_init(&buttonContext);
if ( _counter_1ms >= timeSimulation[33] )
{
v4 = 0LL;
v1 = 0;
}
else
{
v1 = 0;
do
{
v2 = timeSimulation;
do
{
while ( *v2 != _counter_1ms )
{
v2 += 3;
if ( v2 == &timeSimulation[36] )
goto LABEL_7;
}
eclib_button_set(&buttonContext, *((unsigned __int8 *)v2 + 4), *((unsigned __int8 *)v2 + 5));
v3 = v2[2] != *((_DWORD *)off_1F8 + 5 * *((unsigned __int8 *)v2 + 4) + 3);
v2 += 3;
v1 += v3;
}
while ( v2 != &timeSimulation[36] );
LABEL_7:
eclib_button_process(&buttonContext);
++_counter_1ms;
}
while ( timeSimulation[33] > _counter_1ms );
v4 = (unsigned __int16)v1;
}
printf("%s: %s\terrors detected: %d\n", "eclib_test_button.c", "May 23 2025", v4);
return v1;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // eax
unsigned int v1; // er12
_DWORD *v2; // rbx
bool v3; // al
__int64 v4; // rcx

v0 = eclib_timer_version();
printf("%s: %s\tv.%x\n", "eclib_test_button.c", "May 23 2025", v0);
_counter_1ms = 1;
eclib_button_init(&buttonContext);
if ( _counter_1ms >= timeSimulation[33] )
{
v4 = 0LL;
v1 = 0;
}
else
{
v1 = 0;
do
{
v2 = timeSimulation;
do
{
while ( *v2 != _counter_1ms )
{
v2 += 3;
if ( v2 == &timeSimulation[36] )
goto LABEL_7;
}
eclib_button_set(&buttonContext, *((unsigned __int8 *)v2 + 4), *((unsigned __int8 *)v2 + 5));
v3 = v2[2] != *((_DWORD *)off_1F8 + 5 * *((unsigned __int8 *)v2 + 4) + 3);
v2 += 3;
v1 += v3;
}
while ( v2 != &timeSimulation[36] );
LABEL_7:
eclib_button_process(&buttonContext);
++_counter_1ms;
}
while ( timeSimulation[33] > _counter_1ms );
v4 = (unsigned __int16)v1;
}
printf("%s: %s\terrors detected: %d\n", "eclib_test_button.c", "May 23 2025", v4);
return v1;
}

original_code:
uint16_t eclib_test_button() {
 uint16_t idx = 0;
 uint16_t errors = 0;
 uint32_t eclib_timer_version_id = eclib_timer_version();
 printf("%s: %s\tv.%x\n", "eclib_test_button.c", "Apr 23 2024", eclib_timer_version_id);
 __counter_1ms = 1;
 eclib_button_init(&buttonContext);
 for (; __counter_1ms < timeSimulation[sizeof(timeSimulation) / sizeof(timeSimulation[0]) - 1].time; __counter_1ms ++) {
  idx = 0;
  while (idx < (sizeof(timeSimulation) / sizeof(timeSimulation[0]))) {
   if (timeSimulation[idx].time == __counter_1ms) {
    eclib_button_set(&buttonContext, timeSimulation[idx].button, timeSimulation[idx].value);
    if (timeSimulation[idx].expState != buttonContext.buttonStatePtr[timeSimulation[idx].button].value) {
     errors ++;
    }
   }
   idx ++;
  }
  eclib_button_process(&buttonContext);
 }
 printf("%s: %s\terrors detected: %d\n", "eclib_test_button.c", "Apr 23 2024", errors);
 return errors;
}

canonical_original_code:
unsigned short eclib_test_button() {
 unsigned short idx = 0;
 unsigned short errors = 0;
 unsigned int eclib_timer_version_id = eclib_timer_version();
 printf("%s: %s\tv.%x\n", "eclib_test_button.c", "Apr 23 2024", eclib_timer_version_id);
 __counter_1ms = 1;
 eclib_button_init(&buttonContext);
 for (; __counter_1ms < timeSimulation[sizeof(timeSimulation) / sizeof(timeSimulation[0]) - 1].time; __counter_1ms ++) {
  idx = 0;
  while (idx < (sizeof(timeSimulation) / sizeof(timeSimulation[0]))) {
   if (timeSimulation[idx].time == __counter_1ms) {
    eclib_button_set(&buttonContext, timeSimulation[idx].button, timeSimulation[idx].value);
    if (timeSimulation[idx].expState != buttonContext.buttonStatePtr[timeSimulation[idx].button].value) {
     errors ++;
    }
   }
   idx ++;
  }
  eclib_button_process(&buttonContext);
 }
 printf("%s: %s\terrors detected: %d\n", "eclib_test_button.c", "Apr 23 2024", errors);
 return errors;
}

variable_types:
idx
unsigned short

variable_types:
errors
unsigned short


repo: dimitarpeikov/eclib

decompiled_code:
__int64 eclib_button_version()
{
return 256LL;
}

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

original_code:
uint32_t eclib_button_version() {
 return 0x0100;
}

canonical_original_code:
unsigned int eclib_button_version() {
 return 0x0100;
}

return_type: unsigned int


original_code:
eclib_status_t eclib_button_init(eclib_button_context_ptr_t buttonsContextPtr) {
 uint16_t idx;
 eclib_status_t result = ECLIB_OK;
 if (((void *)0) != buttonsContextPtr) {
  for (idx = 0; idx < buttonsContextPtr->buttonCount; idx ++) {
   buttonsContextPtr->buttonStatePtr[idx].state = ECLIB_BUTTON_STATE_INIT;
   buttonsContextPtr->buttonStatePtr[idx].value = ECLIB_BUTTON_STATE_INIT;
   buttonsContextPtr->buttonStatePtr[idx].lastChange = 0L;
   buttonsContextPtr->buttonStatePtr[idx].autoRepeatChange = 0L;
   buttonsContextPtr->buttonStatePtr[idx].eventMask = 0L;
  }
 } else {
  result = ECLIB_ERROR;
 }
 return result;
}

canonical_original_code:
enum eclib_status_enum eclib_button_init(struct eclib_button_context_t *buttonsContextPtr) {
 unsigned short idx;
 enum eclib_status_enum result = ECLIB_OK;
 if (((void *)0) != buttonsContextPtr) {
  for (idx = 0; idx < buttonsContextPtr->buttonCount; idx ++) {
   buttonsContextPtr->buttonStatePtr[idx].state = ECLIB_BUTTON_STATE_INIT;
   buttonsContextPtr->buttonStatePtr[idx].value = ECLIB_BUTTON_STATE_INIT;
   buttonsContextPtr->buttonStatePtr[idx].lastChange = 0L;
   buttonsContextPtr->buttonStatePtr[idx].autoRepeatChange = 0L;
   buttonsContextPtr->buttonStatePtr[idx].eventMask = 0L;
  }
 } else {
  result = ECLIB_ERROR;
 }
 return result;
}

variable_types: struct eclib_button_context_t { struct eclib_button_definition_t { unsigned int shortClick; unsigned int normalClick; unsigned int longClick; unsigned int autoRepeat; unsigned int stick; unsigned int eventMask; void (*callback)(unsigned char buttonId, enum eclib_button_reason { ECLIB_BUTTON_NONE=0, ECLIB_BUTTON_SHORT_CLICK=1, ECLIB_BUTTON_NORMAL_CLICK=2, ECLIB_BUTTON_LONG_CLICK=3, ECLIB_BUTTON_AUTO_REPEAT=4, ECLIB_BUTTON_STICK=5 } reason); } *buttonsDefPtr; struct eclib_button_state_t { unsigned int lastChange; unsigned int autoRepeatChange; enum eclib_button_status { ECLIB_BUTTON_STATE_UNINIT=0, ECLIB_BUTTON_STATE_INIT=1, ECLIB_BUTTON_STATE_RELEASED=2, ECLIB_BUTTON_STATE_PRESSED=3, ECLIB_BUTTON_STATE_REPEAT=4, ECLIB_BUTTON_STATE_STICK_FAILURE=5 } state; enum eclib_button_status { ECLIB_BUTTON_STATE_UNINIT=0, ECLIB_BUTTON_STATE_INIT=1, ECLIB_BUTTON_STATE_RELEASED=2, ECLIB_BUTTON_STATE_PRESSED=3, ECLIB_BUTTON_STATE_REPEAT=4, ECLIB_BUTTON_STATE_STICK_FAILURE=5 } value; unsigned int eventMask; } *buttonStatePtr; unsigned int lastProcess; unsigned char buttonCount; } *

variable_types: enum eclib_status_enum { ECLIB_OK=0, ECLIB_INITIALIZED=1, ECLIB_ERROR=2, ECLIB_NULL_POINTER=3, ECLIB_NOT_FULL=4, ECLIB_FULL=5, ECLIB_EMPTY=6, ECLIB_FOUND=7, ECLIB_NOT_FOUND=8, ECLIB_OUT_OF_BOUNDS=9, ECLIB_NOT_IMPLEMENTED=10 }

user_defined_types: struct eclib_button_context_t { struct eclib_button_definition_t *buttonsDefPtr; struct eclib_button_state_t *buttonStatePtr; unsigned int lastProcess; unsigned char buttonCount; }

user_defined_types: struct eclib_button_definition_t { unsigned int shortClick; unsigned int normalClick; unsigned int longClick; unsigned int autoRepeat; unsigned int stick; unsigned int eventMask; void (*callback)(unsigned char buttonId, enum eclib_button_reason reason); }

user_defined_types: struct eclib_button_state_t { unsigned int lastChange; unsigned int autoRepeatChange; enum eclib_button_status state; enum eclib_button_status value; unsigned int eventMask; }


decompiled_code:
__int64 __fastcall eclib_button_process(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int *v3; // rbp
__int64 v4; // r12
unsigned int v5; // er14
int v6; // eax
void (__fastcall *v7)(_QWORD, __int64); // rax
void (__fastcall *v8)(_QWORD, __int64); // rax
void (__fastcall *v9)(_QWORD, __int64); // rax
void (__fastcall *v10)(_QWORD, __int64); // rax
void (__fastcall *v11)(_QWORD, __int64); // rax
void (__fastcall *v12)(_QWORD, __int64); // rax
int v13[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v13[0] = 0;
result = eclib_timer_start_timer(v13);
if ( *(_BYTE *)(a1 + 20) )
{
v2 = 0LL;
do
{
v3 = (unsigned int *)(*(_QWORD *)a1 + 32 * v2);
v4 = *(_QWORD *)(a1 + 8) + 20 * v2;
v5 = v3[5];
if ( (unsigned __int8)eclib_timer_is_timer_started(v4) )
{
v6 = *(_DWORD *)(v4 + 16);
if ( (v6 & 0x10) == 0 )
{
if ( (v5 & 1) != 0 && (v6 & 1) == 0 && (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, *v3) )
{
v10 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v10 )
v10((unsigned int)v2, 1LL);
*(_DWORD *)(v4 + 16) |= 1u;
}
if ( (v5 & 2) != 0
&& (*(_BYTE *)(v4 + 16) & 2) == 0
&& (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, v3[1]) )
{
v12 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v12 )
v12((unsigned int)v2, 2LL);
*(_DWORD *)(v4 + 16) |= 2u;
}
if ( (v5 & 4) != 0
&& (*(_BYTE *)(v4 + 16) & 4) == 0
&& (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, v3[2]) )
{
v11 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v11 )
v11((unsigned int)v2, 3LL);
*(_DWORD *)(v4 + 16) |= 4u;
}
if ( (v5 & 8) != 0 )
{
if ( (*(_BYTE *)(v4 + 16) & 8) == 0 )
goto LABEL_44;
if ( (unsigned __int8)eclib_timer_is_elapsed_timeout(v4 + 4, v3[3]) )
{
v9 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v9 )
v9((unsigned int)v2, 4LL);
*(_DWORD *)(v4 + 16) |= 8u;
*(_DWORD *)(v4 + 8) = 4;
eclib_timer_start_timer(v4 + 4);
}
if ( (*(_BYTE *)(v4 + 16) & 8) == 0 )
{
LABEL_44:
if ( (unsigned __int8)eclib_timer_is_elapsed_timeout(v4 + 4, v3[1] + v3[3]) )
{
v7 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v7 )
v7((unsigned int)v2, 4LL);
*(_DWORD *)(v4 + 16) |= 8u;
*(_DWORD *)(v4 + 8) = 4;
eclib_timer_start_timer(v4 + 4);
}
}
}
if ( (v5 & 0x10) != 0 && (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, v3[4]) )
{
v8 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v8 )
v8((unsigned int)v2, 5LL);
*(_DWORD *)(v4 + 16) |= 0x10u;
}
}
}
result = *(unsigned __int8 *)(a1 + 20);
++v2;
}
while ( (unsigned __int16)result > (unsigned __int16)v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int *v3; // rbp
__int64 v4; // r12
unsigned int v5; // er14
int v6; // eax
void (__fastcall *v7)(_QWORD, __int64); // rax
void (__fastcall *v8)(_QWORD, __int64); // rax
void (__fastcall *v9)(_QWORD, __int64); // rax
void (__fastcall *v10)(_QWORD, __int64); // rax
void (__fastcall *v11)(_QWORD, __int64); // rax
void (__fastcall *v12)(_QWORD, __int64); // rax
int v13[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v13[0] = 0;
result = eclib_timer_start_timer(v13);
if ( *(_BYTE *)(a1 + 20) )
{
v2 = 0LL;
do
{
v3 = (unsigned int *)(*(_QWORD *)a1 + 32 * v2);
v4 = *(_QWORD *)(a1 + 8) + 20 * v2;
v5 = v3[5];
if ( (unsigned __int8)eclib_timer_is_timer_started(v4) )
{
v6 = *(_DWORD *)(v4 + 16);
if ( (v6 & 0x10) == 0 )
{
if ( (v5 & 1) != 0 && (v6 & 1) == 0 && (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, *v3) )
{
v10 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v10 )
v10((unsigned int)v2, 1LL);
*(_DWORD *)(v4 + 16) |= 1u;
}
if ( (v5 & 2) != 0
&& (*(_BYTE *)(v4 + 16) & 2) == 0
&& (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, v3[1]) )
{
v12 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v12 )
v12((unsigned int)v2, 2LL);
*(_DWORD *)(v4 + 16) |= 2u;
}
if ( (v5 & 4) != 0
&& (*(_BYTE *)(v4 + 16) & 4) == 0
&& (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, v3[2]) )
{
v11 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v11 )
v11((unsigned int)v2, 3LL);
*(_DWORD *)(v4 + 16) |= 4u;
}
if ( (v5 & 8) != 0 )
{
if ( (*(_BYTE *)(v4 + 16) & 8) == 0 )
goto LABEL_44;
if ( (unsigned __int8)eclib_timer_is_elapsed_timeout(v4 + 4, v3[3]) )
{
v9 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v9 )
v9((unsigned int)v2, 4LL);
*(_DWORD *)(v4 + 16) |= 8u;
*(_DWORD *)(v4 + 8) = 4;
eclib_timer_start_timer(v4 + 4);
}
if ( (*(_BYTE *)(v4 + 16) & 8) == 0 )
{
LABEL_44:
if ( (unsigned __int8)eclib_timer_is_elapsed_timeout(v4 + 4, v3[1] + v3[3]) )
{
v7 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v7 )
v7((unsigned int)v2, 4LL);
*(_DWORD *)(v4 + 16) |= 8u;
*(_DWORD *)(v4 + 8) = 4;
eclib_timer_start_timer(v4 + 4);
}
}
}
if ( (v5 & 0x10) != 0 && (unsigned __int8)eclib_timer_is_elapsed_timeout(v4, v3[4]) )
{
v8 = (void (__fastcall *)(_QWORD, __int64))*((_QWORD *)v3 + 3);
if ( v8 )
v8((unsigned int)v2, 5LL);
*(_DWORD *)(v4 + 16) |= 0x10u;
}
}
}
result = *(unsigned __int8 *)(a1 + 20);
++v2;
}
while ( (unsigned __int16)result > (unsigned __int16)v2 );
}
return result;
}

original_code:
void eclib_button_process(eclib_button_context_ptr_t buttonsContextPtr) {
 uint16_t idx;
 uint32_t localTimer = 0L;
 uint32_t eventButtonMask;
 eclib_button_state_ptr_t btnStatePtr;
 eclib_button_definition_ptr_t btnDefPtr;
 eclib_timer_start_timer(&localTimer);
 for (idx = 0; idx < buttonsContextPtr->buttonCount; idx ++) {
  btnStatePtr = &(buttonsContextPtr->buttonStatePtr[idx]);
  btnDefPtr = &(buttonsContextPtr->buttonsDefPtr[idx]);
  eventButtonMask = btnDefPtr->eventMask;
  if ((0 != eclib_timer_is_timer_started(&btnStatePtr->lastChange)) && !(btnStatePtr->eventMask & 0x10)) {
   if ((eventButtonMask & 0x01) && !(btnStatePtr->eventMask & 0x01)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->shortClick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_SHORT_CLICK);
     }
     btnStatePtr->eventMask |= 0x01;
    }
   }
   if ((eventButtonMask & 0x02) && !(btnStatePtr->eventMask & 0x02)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->normalClick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_NORMAL_CLICK);
     }
     btnStatePtr->eventMask |= 0x02;
    }
   }
   if ((eventButtonMask & 0x04) && !(btnStatePtr->eventMask & 0x04)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->longClick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_LONG_CLICK);
     }
     btnStatePtr->eventMask |= 0x04;
    }
   }
   if ((eventButtonMask & 0x08) && (btnStatePtr->eventMask & 0x08)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->autoRepeatChange, btnDefPtr->autoRepeat)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_AUTO_REPEAT);
     }
     btnStatePtr->state = ECLIB_BUTTON_STATE_REPEAT;
     btnStatePtr->eventMask |= 0x08;
     eclib_timer_start_timer(&btnStatePtr->autoRepeatChange);
    }
   }
   if ((eventButtonMask & 0x08) && !(btnStatePtr->eventMask & 0x08)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->autoRepeatChange, btnDefPtr->normalClick + btnDefPtr->autoRepeat)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_AUTO_REPEAT);
     }
     btnStatePtr->state = ECLIB_BUTTON_STATE_REPEAT;
     btnStatePtr->eventMask |= 0x08;
     eclib_timer_start_timer(&btnStatePtr->autoRepeatChange);
    }
   }
   if (eventButtonMask & 0x10) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->stick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_STATE_STICK_FAILURE);
     }
     btnStatePtr->eventMask |= 0x10;
    }
   }
  }
 }
}

canonical_original_code:
void eclib_button_process(struct eclib_button_context_t *buttonsContextPtr) {
 unsigned short idx;
 unsigned int localTimer = 0L;
 unsigned int eventButtonMask;
 struct eclib_button_state_t *btnStatePtr;
 struct eclib_button_definition_t *btnDefPtr;
 eclib_timer_start_timer(&localTimer);
 for (idx = 0; idx < buttonsContextPtr->buttonCount; idx ++) {
  btnStatePtr = &(buttonsContextPtr->buttonStatePtr[idx]);
  btnDefPtr = &(buttonsContextPtr->buttonsDefPtr[idx]);
  eventButtonMask = btnDefPtr->eventMask;
  if ((0 != eclib_timer_is_timer_started(&btnStatePtr->lastChange)) && !(btnStatePtr->eventMask & 0x10)) {
   if ((eventButtonMask & 0x01) && !(btnStatePtr->eventMask & 0x01)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->shortClick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_SHORT_CLICK);
     }
     btnStatePtr->eventMask |= 0x01;
    }
   }
   if ((eventButtonMask & 0x02) && !(btnStatePtr->eventMask & 0x02)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->normalClick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_NORMAL_CLICK);
     }
     btnStatePtr->eventMask |= 0x02;
    }
   }
   if ((eventButtonMask & 0x04) && !(btnStatePtr->eventMask & 0x04)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->longClick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_LONG_CLICK);
     }
     btnStatePtr->eventMask |= 0x04;
    }
   }
   if ((eventButtonMask & 0x08) && (btnStatePtr->eventMask & 0x08)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->autoRepeatChange, btnDefPtr->autoRepeat)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_AUTO_REPEAT);
     }
     btnStatePtr->state = ECLIB_BUTTON_STATE_REPEAT;
     btnStatePtr->eventMask |= 0x08;
     eclib_timer_start_timer(&btnStatePtr->autoRepeatChange);
    }
   }
   if ((eventButtonMask & 0x08) && !(btnStatePtr->eventMask & 0x08)) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->autoRepeatChange, btnDefPtr->normalClick + btnDefPtr->autoRepeat)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_AUTO_REPEAT);
     }
     btnStatePtr->state = ECLIB_BUTTON_STATE_REPEAT;
     btnStatePtr->eventMask |= 0x08;
     eclib_timer_start_timer(&btnStatePtr->autoRepeatChange);
    }
   }
   if (eventButtonMask & 0x10) {
    if (eclib_timer_is_elapsed_timeout(&btnStatePtr->lastChange, btnDefPtr->stick)) {
     if (btnDefPtr->callback) {
      btnDefPtr->callback(idx, ECLIB_BUTTON_STATE_STICK_FAILURE);
     }
     btnStatePtr->eventMask |= 0x10;
    }
   }
  }
 }
}

variable_types:
localTimer
unsigned int

variable_types:
eventButtonMask
unsigned int

variable_types: struct eclib_button_state_t { unsigned int lastChange; unsigned int autoRepeatChange; enum eclib_button_status { ECLIB_BUTTON_STATE_UNINIT=0, ECLIB_BUTTON_STATE_INIT=1, ECLIB_BUTTON_STATE_RELEASED=2, ECLIB_BUTTON_STATE_PRESSED=3, ECLIB_BUTTON_STATE_REPEAT=4, ECLIB_BUTTON_STATE_STICK_FAILURE=5 } state; enum eclib_button_status { ECLIB_BUTTON_STATE_UNINIT=0, ECLIB_BUTTON_STATE_INIT=1, ECLIB_BUTTON_STATE_RELEASED=2, ECLIB_BUTTON_STATE_PRESSED=3, ECLIB_BUTTON_STATE_REPEAT=4, ECLIB_BUTTON_STATE_STICK_FAILURE=5 } value; unsigned int eventMask; } *

variable_types: struct eclib_button_definition_t { unsigned int shortClick; unsigned int normalClick; unsigned int longClick; unsigned int autoRepeat; unsigned int stick; unsigned int eventMask; void (*callback)(unsigned char buttonId, enum eclib_button_reason { ECLIB_BUTTON_NONE=0, ECLIB_BUTTON_SHORT_CLICK=1, ECLIB_BUTTON_NORMAL_CLICK=2, ECLIB_BUTTON_LONG_CLICK=3, ECLIB_BUTTON_AUTO_REPEAT=4, ECLIB_BUTTON_STICK=5 } reason); } *

return_type: void


decompiled_code:
__int64 __fastcall eclib_button_set(__int64 a1, unsigned __int8 a2, int a3)
{
__int64 result; // rax
int *v5; // rbp
int v6; // eax
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v7[0] = 0;
if ( !a1 )
return 3LL;
result = 0LL;
if ( *(_BYTE *)(a1 + 20) > a2 )
{
v5 = (int *)(*(_QWORD *)(a1 + 8) + 20LL * a2);
if ( v5[2] != a3 )
{
if ( a3 == 3 )
eclib_timer_start_timer(v7);
else
eclib_timer_stop_timer(v7);
eclib_timer_start_timer(v5);
v6 = *v5;
v5[3] = a3;
v5[4] = 0;
v5[1] = v6;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int8 a2, int a3)
{
__int64 result; // rax
int *v5; // rbp
int v6; // eax
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v7[0] = 0;
if ( !a1 )
return 3LL;
result = 0LL;
if ( *(_BYTE *)(a1 + 20) > a2 )
{
v5 = (int *)(*(_QWORD *)(a1 + 8) + 20LL * a2);
if ( v5[2] != a3 )
{
if ( a3 == 3 )
eclib_timer_start_timer(v7);
else
eclib_timer_stop_timer(v7);
eclib_timer_start_timer(v5);
v6 = *v5;
v5[3] = a3;
v5[4] = 0;
v5[1] = v6;
result = 0LL;
}
}
return result;
}

original_code:
eclib_status_t eclib_button_set(eclib_button_context_ptr_t buttonsContextPtr, uint8_t buttonIndex, eclib_button_status_t buttonState) {
 uint32_t timer = 0L;
 eclib_button_state_ptr_t statePtr;
 eclib_status_t result = ECLIB_OK;
 if (0 != buttonsContextPtr) {
  if (buttonIndex < buttonsContextPtr->buttonCount) {
   statePtr = &(buttonsContextPtr->buttonStatePtr[buttonIndex]);
   if (statePtr->state != buttonState) {
    if (ECLIB_BUTTON_STATE_PRESSED == buttonState) {
     eclib_timer_start_timer(&timer);
    } else {
     eclib_timer_stop_timer(&timer);
     if (ECLIB_BUTTON_STATE_RELEASED == statePtr->state) {
      if (statePtr->state & 0x10) {
       statePtr->state = ECLIB_BUTTON_STATE_STICK_FAILURE;
      } else {
       if (statePtr->state & 0x04) {
        statePtr->state = ECLIB_BUTTON_LONG_CLICK;
       } else {
        if (statePtr->state & 0x02) {
         statePtr->state = ECLIB_BUTTON_NORMAL_CLICK;
        } else {
         if (statePtr->state & 0x01) {
          statePtr->state = ECLIB_BUTTON_SHORT_CLICK;
         } else {
         }
        }
       }
      }
     }
    }
    eclib_timer_start_timer(&statePtr->lastChange);
    statePtr->autoRepeatChange = statePtr->lastChange;
    statePtr->value = buttonState;
    statePtr->eventMask = 0L;
   }
  }
 } else {
  result = ECLIB_NULL_POINTER;
 }
 return result;
}

canonical_original_code:
enum eclib_status_enum eclib_button_set(struct eclib_button_context_t *buttonsContextPtr, unsigned char buttonIndex, enum eclib_button_status buttonState) {
 unsigned int timer = 0L;
 struct eclib_button_state_t *statePtr;
 enum eclib_status_enum result = ECLIB_OK;
 if (0 != buttonsContextPtr) {
  if (buttonIndex < buttonsContextPtr->buttonCount) {
   statePtr = &(buttonsContextPtr->buttonStatePtr[buttonIndex]);
   if (statePtr->state != buttonState) {
    if (ECLIB_BUTTON_STATE_PRESSED == buttonState) {
     eclib_timer_start_timer(&timer);
    } else {
     eclib_timer_stop_timer(&timer);
     if (ECLIB_BUTTON_STATE_RELEASED == statePtr->state) {
      if (statePtr->state & 0x10) {
       statePtr->state = ECLIB_BUTTON_STATE_STICK_FAILURE;
      } else {
       if (statePtr->state & 0x04) {
        statePtr->state = ECLIB_BUTTON_LONG_CLICK;
       } else {
        if (statePtr->state & 0x02) {
         statePtr->state = ECLIB_BUTTON_NORMAL_CLICK;
        } else {
         if (statePtr->state & 0x01) {
          statePtr->state = ECLIB_BUTTON_SHORT_CLICK;
         } else {
         }
        }
       }
      }
     }
    }
    eclib_timer_start_timer(&statePtr->lastChange);
    statePtr->autoRepeatChange = statePtr->lastChange;
    statePtr->value = buttonState;
    statePtr->eventMask = 0L;
   }
  }
 } else {
  result = ECLIB_NULL_POINTER;
 }
 return result;
}

variable_types:
buttonIndex
unsigned char

variable_types: enum eclib_button_status { ECLIB_BUTTON_STATE_UNINIT=0, ECLIB_BUTTON_STATE_INIT=1, ECLIB_BUTTON_STATE_RELEASED=2, ECLIB_BUTTON_STATE_PRESSED=3, ECLIB_BUTTON_STATE_REPEAT=4, ECLIB_BUTTON_STATE_STICK_FAILURE=5 }

variable_types:
timer
unsigned int

return_type: enum eclib_status_enum { ECLIB_OK=0, ECLIB_INITIALIZED=1, ECLIB_ERROR=2, ECLIB_NULL_POINTER=3, ECLIB_NOT_FULL=4, ECLIB_FULL=5, ECLIB_EMPTY=6, ECLIB_FOUND=7, ECLIB_NOT_FOUND=8, ECLIB_OUT_OF_BOUNDS=9, ECLIB_NOT_IMPLEMENTED=10 }

user_defined_types: enum eclib_button_reason { ECLIB_BUTTON_NONE=0, ECLIB_BUTTON_SHORT_CLICK=1, ECLIB_BUTTON_NORMAL_CLICK=2, ECLIB_BUTTON_LONG_CLICK=3, ECLIB_BUTTON_AUTO_REPEAT=4, ECLIB_BUTTON_STICK=5 }


decompiled_code:
__int64 eclib_test_queue()
{
unsigned __int16 v0; // bx
unsigned int v1; // eax
unsigned int v2; // er12
unsigned __int16 v3; // bx
__int16 v4; // r12
unsigned __int16 v5; // bx
__int16 v6; // r12
unsigned __int16 v7; // bx
__int16 v8; // r13
int v9; // ebp
__int16 v10; // bx
unsigned int v11; // er14
int v12; // eax
__int16 v13; // r13
int v14; // ebp
__int16 v15; // bx
unsigned int v16; // er14
int v17; // eax
unsigned __int16 i; // [rsp+Ch] [rbp-2Ch] BYREF
char v20[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v0 = 0;
v1 = eclib_queue_version();
printf("%s: %s\tv.%x\n", "eclib_test_queue.c", "May 23 2025", v1);
for ( i = 0; i <= 0xAu; ++i )
v0 -= ((unsigned int)eclib_queue_push(&uint16Queue11, &i) == 0) - 1;
v2 = v0 > 1u;
v3 = 0;
for ( i = 0; i <= 0xAu; ++i )
v3 -= ((unsigned int)eclib_queue_pop(&uint16Queue11, v20) == 0) - 1;
v4 = v2 - ((v3 < 2u) - 1);
v5 = 0;
eclib_queue_init(&uint16Queue11, &uint16Queue11_buffer, 2LL, 5LL);
for ( i = 0; i <= 5u; ++i )
v5 -= ((unsigned int)eclib_queue_push(&uint16Queue11, &i) == 0) - 1;
v6 = v4 - ((v5 < 7u) - 1);
v7 = 0;
for ( i = 0; i <= 5u; ++i )
v7 -= ((unsigned int)eclib_queue_pop(&uint16Queue11, v20) == 0) - 1;
v8 = 100;
LOWORD(v2) = v6 - ((v7 < 7u) - 1);
v9 = clock();
do
{
v10 = 1000;
do
{
eclib_queue_clear(&uint16Queue13);
for ( i = 0; i <= 0x1387u; ++i )
v11 = eclib_queue_push(&uint16Queue13, &i);
--v10;
}
while ( v10 );
--v8;
}
while ( v8 );
v12 = clock();
v13 = 100;
printf("push<%d>( %d , %d )\t%d\n", i, 1000LL, v11, (unsigned int)(v12 - v9));
v14 = clock();
do
{
v15 = 1000;
do
{
eclib_queue_full(&uint16Queue13);
for ( i = 0; i <= 0x1387u; ++i )
v16 = eclib_queue_pop(&uint16Queue13, v20);
--v15;
}
while ( v15 );
--v13;
}
while ( v13 );
v17 = clock();
printf("pop<%d>( %d , %d )\t%d\n", i, 1000LL, v16, (unsigned int)(v17 - v14));
return v2;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned __int16 v0; // bx
unsigned int v1; // eax
unsigned int v2; // er12
unsigned __int16 v3; // bx
__int16 v4; // r12
unsigned __int16 v5; // bx
__int16 v6; // r12
unsigned __int16 v7; // bx
__int16 v8; // r13
int v9; // ebp
__int16 v10; // bx
unsigned int v11; // er14
int v12; // eax
__int16 v13; // r13
int v14; // ebp
__int16 v15; // bx
unsigned int v16; // er14
int v17; // eax
unsigned __int16 i; // [rsp+Ch] [rbp-2Ch] BYREF
char v20[42]; // [rsp+Eh] [rbp-2Ah] BYREF

v0 = 0;
v1 = eclib_queue_version();
printf("%s: %s\tv.%x\n", "eclib_test_queue.c", "May 23 2025", v1);
for ( i = 0; i <= 0xAu; ++i )
v0 -= ((unsigned int)eclib_queue_push(&uint16Queue11, &i) == 0) - 1;
v2 = v0 > 1u;
v3 = 0;
for ( i = 0; i <= 0xAu; ++i )
v3 -= ((unsigned int)eclib_queue_pop(&uint16Queue11, v20) == 0) - 1;
v4 = v2 - ((v3 < 2u) - 1);
v5 = 0;
eclib_queue_init(&uint16Queue11, &uint16Queue11_buffer, 2LL, 5LL);
for ( i = 0; i <= 5u; ++i )
v5 -= ((unsigned int)eclib_queue_push(&uint16Queue11, &i) == 0) - 1;
v6 = v4 - ((v5 < 7u) - 1);
v7 = 0;
for ( i = 0; i <= 5u; ++i )
v7 -= ((unsigned int)eclib_queue_pop(&uint16Queue11, v20) == 0) - 1;
v8 = 100;
LOWORD(v2) = v6 - ((v7 < 7u) - 1);
v9 = clock();
do
{
v10 = 1000;
do
{
eclib_queue_clear(&uint16Queue13);
for ( i = 0; i <= 0x1387u; ++i )
v11 = eclib_queue_push(&uint16Queue13, &i);
--v10;
}
while ( v10 );
--v8;
}
while ( v8 );
v12 = clock();
v13 = 100;
printf("push<%d>( %d , %d )\t%d\n", i, 1000LL, v11, (unsigned int)(v12 - v9));
v14 = clock();
do
{
v15 = 1000;
do
{
eclib_queue_full(&uint16Queue13);
for ( i = 0; i <= 0x1387u; ++i )
v16 = eclib_queue_pop(&uint16Queue13, v20);
--v15;
}
while ( v15 );
--v13;
}
while ( v13 );
v17 = clock();
printf("pop<%d>( %d , %d )\t%d\n", i, 1000LL, v16, (unsigned int)(v17 - v14));
return v2;
}

original_code:
uint16_t eclib_test_queue(void)
{
 uint16_t nErrors = 0;
 uint16_t nErrorsFound;
 uint16_t i, i2;
 uint16_t n, nn;
 uint32_t start_time = 0L;
 uint32_t end_time = 0L;
 eclib_status_t eStatus;
 uint32_t eclib_queue_version_id = eclib_queue_version();
 printf("%s: %s\tv.%x\n", "eclib_test_queue.c", "Apr 23 2024", eclib_queue_version_id);
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 eclib_queue_init(&uint16Queue11, uint16Queue11_buffer, sizeof(uint16_t), 5);
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_clear( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++)
   {
    eStatus = eclib_queue_push(&uint16Queue13, &i);
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("push<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_full( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++ )
   {
    eStatus = eclib_queue_pop( &uint16Queue13, &i2 );
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("pop<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 return nErrors;
}

canonical_original_code:
unsigned short eclib_test_queue()
{
 unsigned short nErrors = 0;
 unsigned short nErrorsFound;
 unsigned short i; unsigned short i2;
 unsigned short n; unsigned short nn;
 unsigned int start_time = 0L;
 unsigned int end_time = 0L;
 enum eclib_status_enum eStatus;
 unsigned int eclib_queue_version_id = eclib_queue_version();
 printf("%s: %s\tv.%x\n", "eclib_test_queue.c", "Apr 23 2024", eclib_queue_version_id);
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 eclib_queue_init(&uint16Queue11, uint16Queue11_buffer, sizeof(unsigned short), 5);
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_clear( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++)
   {
    eStatus = eclib_queue_push(&uint16Queue13, &i);
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("push<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_full( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++ )
   {
    eStatus = eclib_queue_pop( &uint16Queue13, &i2 );
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("pop<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 return nErrors;
}

variable_types:
nErrors
unsigned short

variable_types:
nErrorsFound
unsigned short

variable_types:
i
unsigned short

variable_types:
n
unsigned short

variable_types:
nn
unsigned short

return_type: unsigned short

user_defined_types: enum eclib_status_enum { ECLIB_OK=0, ECLIB_INITIALIZED=1, ECLIB_ERROR=2, ECLIB_NULL_POINTER=3, ECLIB_NOT_FULL=4, ECLIB_FULL=5, ECLIB_EMPTY=6, ECLIB_FOUND=7, ECLIB_NOT_FOUND=8, ECLIB_OUT_OF_BOUNDS=9, ECLIB_NOT_IMPLEMENTED=10 }


main

canonical_name: main

repo: dingdongnigetou/shell

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

getcwd(pwd, 0x401uLL);
initialize_readline();
if ( argc == 1 )
{
INPUT = 0;
source = (__int64)stdin;
}
else
{
v3 = argv[1];
INPUT = 1;
source = (__int64)fopen(v3, "r");
if ( !source )
{
fprintf(stderr, "File %s not found\n", argv[1]);
exit(1);
}
}
parse();
exit(0);
}

original_code:
int main(int argc, char *argv[])
{
 getcwd(pwd, 1024 + 1);
 initialize_readline();
 if (argc == 1){
  INPUT = 0;
  source = stdin;
 }
 else{
  INPUT = 1;
  source = fopen(argv[1], "r");
  if (source == ((void *)0)){
   fprintf(stderr, "File %s not found\n", argv[1]);
   exit(1);
  }
 }
 parse();
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


isbuildin

decompiled_code:
__int64 __fastcall isbuildin(__int64 a1)
{
unsigned int v1; // er8
__int64 result; // rax

if ( *(_BYTE *)a1 == 99 && *(_BYTE *)(a1 + 1) == 100 && !*(_BYTE *)(a1 + 2) )
return 1LL;
v1 = 2;
if ( !strcmp((const char *)a1, "pwd") || (v1 = 3, !strcmp((const char *)a1, "exit")) )
result = v1;
else
result = 4 * (unsigned int)(strcmp((const char *)a1, "history") == 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // er8
__int64 result; // rax

if ( *(_BYTE *)a1 == 99 && *(_BYTE *)(a1 + 1) == 100 && !*(_BYTE *)(a1 + 2) )
return 1LL;
v1 = 2;
if ( !strcmp((const char *)a1, "pwd") || (v1 = 3, !strcmp((const char *)a1, "exit")) )
result = v1;
else
result = 4 * (unsigned int)(strcmp((const char *)a1, "history") == 0);
return result;
}

original_code:
int isbuildin(char *s)
{
 BuildInType command = NO;
 if(!strcmp(s, "cd"))
  command = CD;
 else if (!strcmp(s, "pwd"))
  command = PWD;
 else if (!strcmp(s, "exit"))
  command = EXIT;
 else if (!strcmp(s, "history"))
  command = HISTORY;
 return command;
}

canonical_original_code:
int isbuildin(char *s)
{
 enum BuildInType command = NO;
 if(!strcmp(s, "cd"))
  command = CD;
 else if (!strcmp(s, "pwd"))
  command = PWD;
 else if (!strcmp(s, "exit"))
  command = EXIT;
 else if (!strcmp(s, "history"))
  command = HISTORY;
 return command;
}

variable_types:
s
char *

variable_types: enum BuildInType { NO=0, CD=1, PWD=2, EXIT=3, HISTORY=4 }


decompiled_code:
void do_cd()
{
int *v0; // rbx

if ( arg[1025] )
{
if ( chdir((const char *)(unsigned int)((_DWORD)arg + 1025)) )
{
v0 = _errno_location();
if ( *v0 == 2 )
{
fprintf(stdout, "ddsh: cd: %s: No such file or directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
else if ( *v0 == 20 )
{
fprintf(stdout, "ddsh: cd: %s: Not a directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
}
else
{
strcpy(&pwd, (const char *)(unsigned int)((_DWORD)arg + 1025));
}
}
}

canonical_decompiled_code:
void func1()
{
int *v0; // rbx

if ( arg[1025] )
{
if ( chdir((const char *)(unsigned int)((_DWORD)arg + 1025)) )
{
v0 = _errno_location();
if ( *v0 == 2 )
{
fprintf(stdout, "ddsh: cd: %s: No such file or directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
else if ( *v0 == 20 )
{
fprintf(stdout, "ddsh: cd: %s: Not a directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
}
else
{
strcpy(&pwd, (const char *)(unsigned int)((_DWORD)arg + 1025));
}
}
}

original_code:
void do_cd()
{
 if (arg[1][0] != 0){
  if (!chdir(arg[1]))
   strcpy(pwd, arg[1]);
  else{
   if ((*__errno_location ()) == 2){
    fprintf(stdout, "ddsh: cd: %s: No such file or directory\n", arg[1]);
    (*__errno_location ()) = 0;
   }
   else if ((*__errno_location ()) == 20){
    fprintf(stdout, "ddsh: cd: %s: Not a directory\n", arg[1]);
    (*__errno_location ()) = 0;
   }
  }
 }
}


decompiled_code:
void __noreturn do_exit()
{
int v0; // eax

v0 = atoi((const char *)(unsigned int)((_DWORD)arg + 1025));
exit(v0);
}

canonical_decompiled_code:
void __noreturn func2()
{
int v0; // eax

v0 = atoi((const char *)(unsigned int)((_DWORD)arg + 1025));
exit(v0);
}

original_code:
void do_exit()
{
 exit(atoi(arg[1]));
}

canonical_original_code:
void do_exit()
{
 exit(atoi(arg[1]));
}


decompiled_code:
int do_pwd()
{
return fprintf(stdout, "%s\n", &pwd);
}

canonical_decompiled_code:
int func3()
{
return fprintf(stdout, "%s\n", &pwd);
}

original_code:
void do_pwd()
{
 fprintf(stdout, "%s\n", pwd);
}


runbuildin

decompiled_code:
void __fastcall runbuildin(__int64 a1)
{
if ( (_DWORD)a1 == 2 )
{
do_pwd();
}
else if ( (unsigned int)a1 <= 2 )
{
if ( (_DWORD)a1 == 1 )
do_cd();
}
else
{
if ( (_DWORD)a1 == 3 )
do_exit(a1);
if ( (_DWORD)a1 == 4 )
do_history();
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
if ( (_DWORD)a1 == 2 )
{
func3();
}
else if ( (unsigned int)a1 <= 2 )
{
if ( (_DWORD)a1 == 1 )
func1();
}
else
{
if ( (_DWORD)a1 == 3 )
func2(a1);
if ( (_DWORD)a1 == 4 )
func4();
}
}

original_code:
void runbuildin(BuildInType command)
{
 switch(command)
 {
  case CD:
   do_cd();
   break;
  case PWD:
   do_pwd();
   break;
  case EXIT:
   do_exit();
   break;
  case HISTORY:
   do_history();
   break;
  case NO:
  default:break;
 }
}

canonical_original_code:
void runbuildin(enum BuildInType command)
{
 switch(command)
 {
  case CD:
   do_cd();
   break;
  case PWD:
   do_pwd();
   break;
  case EXIT:
   do_exit();
   break;
  case HISTORY:
   do_history();
   break;
  case NO:
  default:break;
 }
}

user_defined_types: enum BuildInType { NO=0, CD=1, PWD=2, EXIT=3, HISTORY=4 }


decompiled_code:
char *__fastcall command_generator(char *s2, int a2)
{
size_t v2; // r13
const char **v3; // rbx
const char *v4; // rbp

if ( !a2 )
v2 = (int)strlen(s2);
v3 = (const char **)off_2C8;
v4 = "login";
while ( strncmp(v4, s2, v2) )
{
v4 = *v3++;
if ( !v4 )
return 0LL;
}
return strdup(v4);
}

canonical_decompiled_code:
char *__fastcall func0(char *s2, int a2)
{
size_t v2; // r13
const char **v3; // rbx
const char *v4; // rbp

if ( !a2 )
v2 = (int)strlen(s2);
v3 = (const char **)off_2C8;
v4 = "login";
while ( strncmp(v4, s2, v2) )
{
v4 = *v3++;
if ( !v4 )
return 0LL;
}
return strdup(v4);
}

original_code:
char *command_generator(const char *text, int state)
{
 const char *name;
 int list_index;
 int len;
 if (!state){
  list_index = 0;
  len = strlen(text);
 }
 while ( (name = commandarray[list_index++]) )
  if (strncmp(name, text, len) == 0)
   return strdup(name);
 return (char *)0;
}

canonical_original_code:
char *command_generator(char *text, int state)
{
 char *name;
 int list_index;
 int len;
 if (!state){
  list_index = 0;
  len = strlen(text);
 }
 while ( (name = commandarray[list_index++]) )
  if (strncmp(name, text, len) == 0)
   return strdup(name);
 return (char *)0;
}

variable_types:
text
char *

variable_types: state int

variable_types:
name
char *

variable_types: len int

return_type: char *


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

if ( a2 )
result = 0LL;
else
result = rl_completion_matches(a1, command_generator);
return result;
}

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

if ( a2 )
result = 0LL;
else
result = rl_completion_matches(a1, command_generator);
return result;
}

original_code:
char **command_completion(const char *text, int start, int end)
{
 char **matches = ((void *)0);
 if (start == 0)
  matches = rl_completion_matches(text, command_generator);
 return matches;
}

canonical_original_code:
char **command_completion(char *text, int start, int end)
{
 char **matches = ((void *)0);
 if (start == 0)
  matches = rl_completion_matches(text, command_generator);
 return matches;
}

variable_types: start int

variable_types: end int

variable_types:
matches
char **

return_type: char **


decompiled_code:
_BYTE *rl_gets()
{
char *v0; // rax
_BYTE *result; // rax
char v2[72]; // [rsp+0h] [rbp-48h] BYREF

if ( line_read )
{
free(line_read);
line_read = 0LL;
}
v0 = strrchr(&pwd, 47);
sprintf(v2, "[dingdong %s]# ", v0 + 1);
result = (_BYTE *)readline(v2);
line_read = result;
if ( result )
{
if ( *result )
{
add_history(result);
result = line_read;
}
}
return result;
}

canonical_decompiled_code:
_BYTE *func2()
{
char *v0; // rax
_BYTE *result; // rax
char v2[72]; // [rsp+0h] [rbp-48h] BYREF

if ( line_read )
{
free(line_read);
line_read = 0LL;
}
v0 = strrchr(&pwd, 47);
sprintf(v2, "[dingdong %s]# ", v0 + 1);
result = (_BYTE *)readline(v2);
line_read = result;
if ( result )
{
if ( *result )
{
add_history(result);
result = line_read;
}
}
return result;
}

original_code:
char *rl_gets()
{
 if (line_read){
  free(line_read);
  line_read = (char *)0;
 }
 char prompt[50];
 sprintf(prompt, "[dingdong %s]# ", strrchr(pwd, '/') + 1);
 line_read = readline(prompt);
 if (line_read && *line_read)
  add_history(line_read);
 return line_read;
}

variable_types:
prompt
char [50]


decompiled_code:
void initialize_readline()
{
rl_readline_name = "ddsh";
rl_attempted_completion_function = command_completion;
}

canonical_decompiled_code:
void func3()
{
rl_readline_name = "ddsh";
rl_attempted_completion_function = command_completion;
}


clearToken

decompiled_code:
void clearToken()
{
for ( ; token != 10; token = getToken() )
;
}

canonical_decompiled_code:
void func0()
{
for ( ; token != 10; token = getToken() )
;
}

original_code:
static void clearToken()
{
 while (token != NEWLINE)
  token = getToken();
}


match

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

if ( token != a1 )
return fprintf(stderr, "unexpected token: %s\n", tokenString);
result = getToken();
token = result;
return result;
}

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

if ( token != a1 )
return fprintf(stderr, "unexpected token: %s\n", tokenString);
result = getToken();
token = result;
return result;
}

original_code:
static void match(TokenType expected)
{
 if (token == expected)
  token = getToken();
 else
  fprintf(stderr, "unexpected token: %s\n", tokenString);
}

canonical_original_code:
static void match(enum TokenType expected)
{
 if (token == expected)
  token = getToken();
 else
  fprintf(stderr, "unexpected token: %s\n", tokenString);
}

variable_types: enum TokenType { ID=0, NUM=1, ASSIGN=2, SEMI=3, PIPE=4, DAEMON=5, OR=6, AND=7, RPAREN=8, LPAREN=9, NEWLINE=10, ENDFILE=11, COMMAND=12, ERROR=13, PARAM=14, IF=15, THEN=16, ELSE=17, FI=18, WHILE=19, DO=20, DONEWHILE=21 }


decompiled_code:
void while_stmt()
{
int v0; // eax

match(19);
if ( token != 9 )
{
fwrite("missing '[' behinds while\n", 1uLL, 0x1AuLL, stderr);
LABEL_3:
clearToken();
return;
}
match(9);
if ( token != 1 )
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
goto LABEL_3;
}
match(1);
strcpy(number, tokenString);
if ( token != 8 )
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
goto LABEL_3;
}
match(8);
if ( token != 3 )
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
goto LABEL_3;
}
match(3);
if ( token != 20 )
{
fwrite("missing 'do' behinds commands\n", 1uLL, 0x1EuLL, stderr);
goto LABEL_3;
}
match(20);
v0 = token;
while ( v0 != 3 )
{
v0 = getToken();
token = v0;
if ( v0 == 10 )
{
fwrite("missing ';' behinds 'do'\n", 1uLL, 0x19uLL, stderr);
goto LABEL_3;
}
}
match(3);
if ( token != 21 )
{
fwrite("missing 'done' at the end\n", 1uLL, 0x1AuLL, stderr);
goto LABEL_3;
}
match(21);
while ( atoi(number) )
forktoexec();
}

canonical_decompiled_code:
void func2()
{
int v0; // eax

func1(19);
if ( token != 9 )
{
fwrite("missing '[' behinds while\n", 1uLL, 0x1AuLL, stderr);
LABEL_3:
func0();
return;
}
func1(9);
if ( token != 1 )
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
goto LABEL_3;
}
func1(1);
strcpy(number, tokenString);
if ( token != 8 )
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
goto LABEL_3;
}
func1(8);
if ( token != 3 )
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
goto LABEL_3;
}
func1(3);
if ( token != 20 )
{
fwrite("missing 'do' behinds commands\n", 1uLL, 0x1EuLL, stderr);
goto LABEL_3;
}
func1(20);
v0 = token;
while ( v0 != 3 )
{
v0 = getToken();
token = v0;
if ( v0 == 10 )
{
fwrite("missing ';' behinds 'do'\n", 1uLL, 0x19uLL, stderr);
goto LABEL_3;
}
}
func1(3);
if ( token != 21 )
{
fwrite("missing 'done' at the end\n", 1uLL, 0x1AuLL, stderr);
goto LABEL_3;
}
func1(21);
while ( atoi(number) )
forktoexec();
}

original_code:
void while_stmt()
{
 match(WHILE);
 if (token != LPAREN){
  fprintf(stderr, "missing '[' behinds while\n");
  clearToken();
  return;
 }
 match(LPAREN);
 if (token != NUM){
  fprintf(stderr, "missing number in '[]'\n");
  clearToken();
  return;
 }
 match(NUM);
 strcpy(number, tokenString);
 if (token != RPAREN){
  fprintf(stderr, "missing ']'\n");
  clearToken();
  return;
 }
 match(RPAREN);
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds ']'\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != DO){
  fprintf(stderr, "missing 'do' behinds commands\n");
  clearToken();
  return;
 }
 match(DO);
 while(token != SEMI){
  token = getToken();
  if (token == NEWLINE)
   break;
 }
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds 'do'\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != DONEWHILE){
  fprintf(stderr, "missing 'done' at the end\n");
  clearToken();
  return;
 }
 match(DONEWHILE);
 while(atoi(number))
  forktoexec();
}


decompiled_code:
__int64 do_command()
{
_QWORD *v0; // rdx
__int64 result; // rax
unsigned __int64 v2; // rdi
int v3; // ecx

forktoexec();
v0 = &arg;
result = 0LL;
do
{
v2 = (unsigned __int64)(v0 + 1);
v3 = (int)v0;
*v0 = 0LL;
v0 = (_QWORD *)((char *)v0 + 1025);
*(v0 - 1) = 0LL;
memset((void *)(v2 & 0xFFFFFFFFFFFFFFF8LL), 0, 8 * ((v3 - (v2 & 0xFFFFFFF8) + 1025) >> 3));
}
while ( v0 != (_QWORD *)((char *)&arg + 1050625) );
return result;
}

canonical_decompiled_code:
__int64 func3()
{
_QWORD *v0; // rdx
__int64 result; // rax
unsigned __int64 v2; // rdi
int v3; // ecx

forktoexec();
v0 = &arg;
result = 0LL;
do
{
v2 = (unsigned __int64)(v0 + 1);
v3 = (int)v0;
*v0 = 0LL;
v0 = (_QWORD *)((char *)v0 + 1025);
*(v0 - 1) = 0LL;
memset((void *)(v2 & 0xFFFFFFFFFFFFFFF8LL), 0, 8 * ((v3 - (v2 & 0xFFFFFFF8) + 1025) >> 3));
}
while ( v0 != (_QWORD *)((char *)&arg + 1050625) );
return result;
}

original_code:
void do_command()
{
 forktoexec();
 for (int i = 0; i <= 1024; i++) memset(arg + i, 0, 1024 + 1);;
}

variable_types: i int


decompiled_code:
void if_stmt()
{
int v0; // eax

match(15);
if ( token != 9 )
{
fwrite("missing '[' behinds if\n", 1uLL, 0x17uLL, stderr);
LABEL_3:
clearToken();
return;
}
match(9);
if ( token != 1 )
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
goto LABEL_3;
}
match(1);
strcpy(number, tokenString);
if ( token != 8 )
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
goto LABEL_3;
}
match(8);
if ( token != 3 )
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
goto LABEL_3;
}
match(3);
if ( token != 16 )
{
fwrite("missing 'then'\n", 1uLL, 0xFuLL, stderr);
goto LABEL_3;
}
match(16);
v0 = token;
while ( v0 != 3 )
{
v0 = getToken();
token = v0;
if ( v0 == 10 )
{
fwrite("missing ';' behinds commands\n", 1uLL, 0x1DuLL, stderr);
goto LABEL_3;
}
}
match(3);
if ( token != 18 )
{
fwrite("missing 'fi' at the end\n", 1uLL, 0x18uLL, stderr);
goto LABEL_3;
}
match(18);
if ( atoi(number) )
do_command();
}

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

func1(15);
if ( token != 9 )
{
fwrite("missing '[' behinds if\n", 1uLL, 0x17uLL, stderr);
LABEL_3:
func0();
return;
}
func1(9);
if ( token != 1 )
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
goto LABEL_3;
}
func1(1);
strcpy(number, tokenString);
if ( token != 8 )
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
goto LABEL_3;
}
func1(8);
if ( token != 3 )
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
goto LABEL_3;
}
func1(3);
if ( token != 16 )
{
fwrite("missing 'then'\n", 1uLL, 0xFuLL, stderr);
goto LABEL_3;
}
func1(16);
v0 = token;
while ( v0 != 3 )
{
v0 = getToken();
token = v0;
if ( v0 == 10 )
{
fwrite("missing ';' behinds commands\n", 1uLL, 0x1DuLL, stderr);
goto LABEL_3;
}
}
func1(3);
if ( token != 18 )
{
fwrite("missing 'fi' at the end\n", 1uLL, 0x18uLL, stderr);
goto LABEL_3;
}
func1(18);
if ( atoi(number) )
func3();
}

original_code:
void if_stmt()
{
 match(IF);
 if (token != LPAREN){
  fprintf(stderr, "missing '[' behinds if\n");
  clearToken();
  return;
 }
 match(LPAREN);
 if (token != NUM){
  fprintf(stderr, "missing number in '[]'\n");
  clearToken();
  return;
 }
 else{
  match(NUM);
  strcpy(number, tokenString);
 }
 if (token != RPAREN){
  fprintf(stderr, "missing ']'\n");
  clearToken();
  return;
 }
 match(RPAREN);
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds ']'\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != THEN){
  fprintf(stderr, "missing 'then'\n");
  clearToken();
  return;
 }
 match(THEN);
 while(token != SEMI){
  token = getToken();
  if (token == NEWLINE)
   break;
 }
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds commands\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != FI){
  fprintf(stderr, "missing 'fi' at the end\n");
  clearToken();
  return;
 }
 match(FI);
 if (atoi(number))
  do_command();
}


statement

decompiled_code:
void statement()
{
int v0; // eax

switch ( token )
{
case 3:
match(3);
break;
case 10:
match(10);
break;
case 12:
do
{
v0 = getToken();
token = v0;
}
while ( v0 != 10 && v0 != 3 );
do_command();
break;
case 15:
if_stmt();
break;
case 19:
while_stmt();
break;
default:
fprintf(stderr, "unexpected token %s\n", tokenString);
break;
}
}

canonical_decompiled_code:
void func5()
{
int v0; // eax

switch ( token )
{
case 3:
func1(3);
break;
case 10:
func1(10);
break;
case 12:
do
{
v0 = getToken();
token = v0;
}
while ( v0 != 10 && v0 != 3 );
func3();
break;
case 15:
func4();
break;
case 19:
func2();
break;
default:
fprintf(stderr, "unexpected token %s\n", tokenString);
break;
}
}

original_code:
void statement()
{
 switch(token)
 {
 case IF:
  if_stmt();
         break;
 case WHILE:
  while_stmt();
         break;
 case COMMAND:
  while(token != SEMI){
   token = getToken();
   if (token == NEWLINE)
    break;
  }
  do_command();
  break;
 case SEMI:
  match(SEMI);
  break;
 case NEWLINE:
  match(NEWLINE);
  break;
 default:
  fprintf(stderr, "unexpected token %s\n", tokenString);
  break;
 }
}


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

while ( 1 )
{
statement();
result = (unsigned int)token;
if ( token == 11 )
break;
if ( token == 3 )
match(3);
}
return result;
}

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

while ( 1 )
{
func5();
result = (unsigned int)token;
if ( token == 11 )
break;
if ( token == 3 )
func1(3);
}
return result;
}

original_code:
void stmt_sequence()
{
 statement();
 while (token != ENDFILE){
  if (token == SEMI)
   match(SEMI);
  statement();
 }
}


parse

decompiled_code:
__int64 parse()
{
token = getToken();
return stmt_sequence();
}

canonical_decompiled_code:
__int64 func7()
{
token = getToken();
return func6();
}

original_code:
void parse()
{
 token = getToken();
 stmt_sequence();
}


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

v0 = _errno_location();
result = *v0;
switch ( *v0 )
{
case 2:
result = fprintf(stderr, "ddsh: %s: command not found\n", (const char *)&commands);
break;
case 5:
result = fwrite("ddsh: Input/output error\n", 1uLL, 0x19uLL, stderr);
break;
case 7:
result = fwrite("ddsh: Argument list too long\n", 1uLL, 0x1DuLL, stderr);
break;
case 13:
result = fprintf(stderr, "ddsh: %s: Permission denied\n", (const char *)&commands);
break;
case 16:
result = fwrite("ddsh: Device or resource busy\n", 1uLL, 0x1EuLL, stderr);
break;
case 17:
result = fwrite("ddsh: File exists\n", 1uLL, 0x12uLL, stderr);
break;
case 19:
result = fwrite("ddsh: No such device\n", 1uLL, 0x15uLL, stderr);
break;
case 21:
result = fprintf(stderr, "ddsh: %s: Is a dirextory\n", (const char *)&commands);
break;
case 22:
result = fwrite("ddsh: Invalid argument\n", 1uLL, 0x17uLL, stderr);
break;
case 27:
result = fwrite("ddsh: File too large\n", 1uLL, 0x15uLL, stderr);
break;
default:
break;
}
*v0 = 0;
return result;
}

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

v0 = _errno_location();
result = *v0;
switch ( *v0 )
{
case 2:
result = fprintf(stderr, "ddsh: %s: command not found\n", (const char *)&commands);
break;
case 5:
result = fwrite("ddsh: Input/output error\n", 1uLL, 0x19uLL, stderr);
break;
case 7:
result = fwrite("ddsh: Argument list too long\n", 1uLL, 0x1DuLL, stderr);
break;
case 13:
result = fprintf(stderr, "ddsh: %s: Permission denied\n", (const char *)&commands);
break;
case 16:
result = fwrite("ddsh: Device or resource busy\n", 1uLL, 0x1EuLL, stderr);
break;
case 17:
result = fwrite("ddsh: File exists\n", 1uLL, 0x12uLL, stderr);
break;
case 19:
result = fwrite("ddsh: No such device\n", 1uLL, 0x15uLL, stderr);
break;
case 21:
result = fprintf(stderr, "ddsh: %s: Is a dirextory\n", (const char *)&commands);
break;
case 22:
result = fwrite("ddsh: Invalid argument\n", 1uLL, 0x17uLL, stderr);
break;
case 27:
result = fwrite("ddsh: File too large\n", 1uLL, 0x15uLL, stderr);
break;
default:
break;
}
*v0 = 0;
return result;
}

original_code:
void error_process()
{
 switch((*__errno_location ()))
 {
 case 7:fprintf(stderr, "ddsh: Argument list too long\n");
     break;
 case 13:fprintf(stderr, "ddsh: %s: Permission denied\n", commands);
      break;
 case 16:fprintf(stderr, "ddsh: Device or resource busy\n");
     break;
 case 17:fprintf(stderr, "ddsh: File exists\n");
      break;
 case 27:fprintf(stderr, "ddsh: File too large\n");
     break;
 case 22:fprintf(stderr, "ddsh: Invalid argument\n");
      break;
 case 5:fprintf(stderr, "ddsh: Input/output error\n");
   break;
 case 21:fprintf(stderr, "ddsh: %s: Is a dirextory\n", commands);
      break;
 case 19:fprintf(stderr, "ddsh: No such device\n");
      break;
 case 2:fprintf(stderr, "ddsh: %s: command not found\n", commands);
      break;
 }
 (*__errno_location ()) = 0;
}


ungetNextChar

decompiled_code:
void ungetNextChar()
{
--linepos;
}

canonical_decompiled_code:
void func2()
{
--linepos;
}

original_code:
void ungetNextChar(void)
{
 linepos--;
}

canonical_original_code:
void ungetNextChar()
{
 linepos--;
}


getToken

decompiled_code:
__int64 getToken()
{
int v0; // er14
int v1; // er12
__int64 v2; // rbp
int v3; // ebx
unsigned int v4; // eax
unsigned __int64 v5; // r15
unsigned int v6; // er12
const unsigned __int16 **v7; // r14
__int64 v8; // rax
bool v9; // cl
__int64 v11; // rax
int v12; // er13
__int64 v13; // rax
char v14; // al
char v15; // al
char *v16; // rdi
char v17[1096]; // [rsp+0h] [rbp-448h] BYREF

v0 = 0;
v1 = 0;
v2 = 1LL;
v3 = 0;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v4 = getNextChar();
v5 = v4;
if ( v0 != 3 )
break;
if ( !(_BYTE)v4 )
v0 = 0;
}
if ( v0 == 4 )
goto LABEL_36;
if ( v0 == 1 )
goto LABEL_26;
if ( v0 != 2 )
break;
if ( (_BYTE)v4 == 59 )
{
currentToken = 14;
if ( !v1 )
{
*(_WORD *)&arg[1025 * v2] = 35;
goto LABEL_43;
}
goto LABEL_48;
}
if ( (_BYTE)v4 == 124 )
{
currentToken = 14;
if ( !v1 )
{
*(_WORD *)&arg[1025 * v2] = 35;
return 14;
}
goto LABEL_48;
}
if ( (unsigned __int8)v4 > 0x29u )
goto LABEL_56;
v11 = 0x34000000001LL;
if ( _bittest64(&v11, v5) )
{
currentToken = 14;
v12 = v2;
if ( !v1 )
{
LABEL_49:
*(_WORD *)&arg[1025 * v12] = 35;
if ( (_BYTE)v5 != 59 && (_BYTE)v5 )
return 14;
LABEL_43:
ungetNextChar();
v6 = currentToken;
v9 = currentToken == 12;
if ( currentToken == 12 || currentToken == 1 )
{
*(&tokenString + v3) = 0;
if ( v9 )
goto LABEL_32;
}
return v6;
}
LABEL_48:
v12 = v2 + 1;
v17[v1] = 0;
strcpy(&arg[1025 * v2], v17);
goto LABEL_49;
}
if ( (_BYTE)v5 == 32 || (_BYTE)v5 == 9 )
{
v14 = getNextChar();
if ( v14 == 9 || v14 == 32 )
{
do
{
do
v15 = getNextChar();
while ( v15 == 32 );
}
while ( v15 == 9 );
}
ungetNextChar();
v17[v1] = 0;
v16 = &arg[1025 * v2];
v2 = (int)v2 + 1;
v1 = 0;
strcpy(v16, v17);
}
else
{
LABEL_56:
v13 = v1++;
v17[v13] = v5;
}
}
if ( (_BYTE)v4 != 35 )
break;
v0 = 3;
}
if ( ((*_ctype_b_loc())[(char)v4] & 0x800) == 0 )
break;
if ( v3 > 1024 )
{
LOBYTE(v5) = getNextChar();
LABEL_36:
v7 = _ctype_b_loc();
while ( ((*v7)[(char)v5] & 0x800) != 0 )
{
if ( v3 <= 1024 )
goto LABEL_39;
LOBYTE(v5) = getNextChar();
}
currentToken = 1;
goto LABEL_43;
}
LABEL_39:
v0 = 4;
LABEL_40:
v8 = v3++;
*(&tokenString + v8) = v5;
}
switch ( (_BYTE)v5 )
{
case '[':
currentToken = 9;
return 9;
case ']':
currentToken = 8;
return 8;
case ';':
currentToken = 3;
return 3;
}
if ( (_BYTE)v5 != 32 && (_BYTE)v5 != 9 )
break;
v0 = 0;
}
if ( !(_BYTE)v5 )
{
currentToken = 10;
return 10;
}
if ( (_BYTE)v5 == 0xFF )
{
currentToken = 11;
return 11;
}
if ( currentToken != 12 )
break;
v0 = 2;
strcpy(arg, &tokenString);
ungetNextChar();
}
do
{
if ( v3 <= 1024 )
{
v0 = 1;
goto LABEL_40;
}
v4 = getNextChar();
LOBYTE(v5) = v4;
LABEL_26:
LOBYTE(v4) = (_BYTE)v5 == 124;
if ( (unsigned __int8)v5 <= 0x3Bu )
v4 |= (0x800024100000201uLL >> v5) & 1;
}
while ( !(_BYTE)v4 );
currentToken = 12;
if ( !(_BYTE)v5 || (_BYTE)v5 == 59 )
goto LABEL_43;
*(&tokenString + v3) = 0;
LABEL_32:
currentToken = keywordLookup_constprop_0();
v6 = currentToken;
if ( currentToken == 12 )
strcpy(&commands, &tokenString);
return v6;
}

original_code:
TokenType getToken(void)
{
 StateType state = START;
 int tokenStringIndex = 0;
        int save;
 int argrow = 1;
 char temp[1024 + 1];
 int templen = 0;
 while (state != DONE){
  char nextch;
  char ch = getNextChar();
  save = 1;
  switch (state)
  {
  case START:
   if (ch == '#'){
    save = 0;
    state = INCOMMENT;
   }
   else if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISdigit))
    state = INNUM;
   else if (ch == '['){
    save = 0;
    state = DONE;
    currentToken = LPAREN;
   }
   else if (ch == ']'){
    save = 0;
    state = DONE;
    currentToken = RPAREN;
   }
   else if (ch == ';'){
    save = 0;
    state = DONE;
    currentToken = SEMI;
   }
   else if (ch == ' ' || ch == '\t'){
    save = 0;
   }
   else if (ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = NEWLINE;
   }
   else if (ch == (-1) ){
    save = 0;
    state = DONE;
    currentToken = ENDFILE;
   }
          else{
    if (currentToken == COMMAND){
     save = 0;
     state = INPARAM;
     strcpy(arg[0], tokenString);
     ungetNextChar();
    }
    else
     state = INCOMMAND;
   }
   break;
  case INCOMMAND:
   if (ch == ' ' || ch == '\t' || ch == ';' || ch == '&' ||
     ch == '|' || ch == ')' || ch == ')' || ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = COMMAND;
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   break;
  case INPARAM:
   if (ch == ';' || ch == '&' || ch == '|' ||
     ch == '(' || ch == ')' || ch == '\0'){
    state = DONE;
    currentToken = PARAM;
    if (templen != 0){
     temp[templen] = '\0';
     templen = 0;
     strcpy(arg[argrow++], temp);
    }
    strcpy(arg[argrow], "#");
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   else if (ch == ' ' || ch == '\t'){
    nextch = getNextChar();
    while(nextch == ' ' || nextch == '\t')
     nextch = getNextChar();
    ungetNextChar();
    temp[templen] = '\0';
    templen = 0;
    strcpy(arg[argrow++], temp);
   }
   else
    temp[templen++] = ch;
   save = 0;
   break;
  case INCOMMENT:
   save = 0;
   if (ch == '\0')
    state = START;
   break;
  case INNUM:
   if (!((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISdigit)){
    save = 0;
    state = DONE;
    currentToken = NUM;
    ungetNextChar();
   }
   break;
  default:
   state = DONE;
   currentToken = ERROR;
   break;
  }
  if (save && tokenStringIndex <= 1024)
   tokenString[tokenStringIndex++] = ch;
  if (state == DONE && (currentToken == COMMAND || currentToken == NUM) ){
   tokenString[tokenStringIndex] = '\0';
   if (currentToken == COMMAND)
    currentToken = keywordLookup(tokenString);
  }
 }
 if (currentToken == COMMAND)
  strcpy(commands, tokenString);
 return currentToken;
}

canonical_original_code:
enum TokenType getToken()
{
 enum StateType state = START;
 int tokenStringIndex = 0;
        int save;
 int argrow = 1;
 char temp[-1];
 int templen = 0;
 while (state != DONE){
  char nextch;
  char ch = getNextChar();
  save = 1;
  switch (state)
  {
  case START:
   if (ch == '#'){
    save = 0;
    state = INCOMMENT;
   }
   else if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISdigit))
    state = INNUM;
   else if (ch == '['){
    save = 0;
    state = DONE;
    currentToken = LPAREN;
   }
   else if (ch == ']'){
    save = 0;
    state = DONE;
    currentToken = RPAREN;
   }
   else if (ch == ';'){
    save = 0;
    state = DONE;
    currentToken = SEMI;
   }
   else if (ch == ' ' || ch == '\t'){
    save = 0;
   }
   else if (ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = NEWLINE;
   }
   else if (ch == (-1) ){
    save = 0;
    state = DONE;
    currentToken = ENDFILE;
   }
          else{
    if (currentToken == COMMAND){
     save = 0;
     state = INPARAM;
     strcpy(arg[0], tokenString);
     ungetNextChar();
    }
    else
     state = INCOMMAND;
   }
   break;
  case INCOMMAND:
   if (ch == ' ' || ch == '\t' || ch == ';' || ch == '&' ||
     ch == '|' || ch == ')' || ch == ')' || ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = COMMAND;
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   break;
  case INPARAM:
   if (ch == ';' || ch == '&' || ch == '|' ||
     ch == '(' || ch == ')' || ch == '\0'){
    state = DONE;
    currentToken = PARAM;
    if (templen != 0){
     temp[templen] = '\0';
     templen = 0;
     strcpy(arg[argrow++], temp);
    }
    strcpy(arg[argrow], "#");
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   else if (ch == ' ' || ch == '\t'){
    nextch = getNextChar();
    while(nextch == ' ' || nextch == '\t')
     nextch = getNextChar();
    ungetNextChar();
    temp[templen] = '\0';
    templen = 0;
    strcpy(arg[argrow++], temp);
   }
   else
    temp[templen++] = ch;
   save = 0;
   break;
  case INCOMMENT:
   save = 0;
   if (ch == '\0')
    state = START;
   break;
  case INNUM:
   if (!((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISdigit)){
    save = 0;
    state = DONE;
    currentToken = NUM;
    ungetNextChar();
   }
   break;
  default:
   state = DONE;
   currentToken = ERROR;
   break;
  }
  if (save && tokenStringIndex <= 1024)
   tokenString[tokenStringIndex++] = ch;
  if (state == DONE && (currentToken == COMMAND || currentToken == NUM) ){
   tokenString[tokenStringIndex] = '\0';
   if (currentToken == COMMAND)
    currentToken = keywordLookup(tokenString);
  }
 }
 if (currentToken == COMMAND)
  strcpy(commands, tokenString);
 return currentToken;
}

variable_types: enum StateType { START=0, INCOMMAND=1, INPARAM=2, INCOMMENT=3, INNUM=4, DONE=5 }

variable_types: tokenStringIndex int

variable_types: save int

variable_types: argrow int

variable_types:
temp
char [-1]

variable_types: templen int

variable_types: nextch char

variable_types: ch char

return_type: enum TokenType { ID=0, NUM=1, ASSIGN=2, SEMI=3, PIPE=4, DAEMON=5, OR=6, AND=7, RPAREN=8, LPAREN=9, NEWLINE=10, ENDFILE=11, COMMAND=12, ERROR=13, PARAM=14, IF=15, THEN=16, ELSE=17, FI=18, WHILE=19, DO=20, DONEWHILE=21 }


forktoexec

decompiled_code:
__pid_t forktoexec()
{
unsigned int v0; // eax
__pid_t v2; // eax
__pid_t v3; // ebp
char *v4; // rdx
__int64 i; // rcx
char *v6; // rsi
int v7; // eax
int stat_loc; // [rsp+Ch] [rbp-201Ch] BYREF
char *argv[1027]; // [rsp+10h] [rbp-2018h] BYREF

v0 = isbuildin(&tokenString);
if ( v0 )
return runbuildin(v0);
v2 = fork();
v3 = v2;
if ( v2 < 0 )
{
fwrite("fork error...", 1uLL, 0xDuLL, stderr);
}
else if ( !v2 )
{
if ( arg[1025] )
{
v4 = arg;
for ( i = 0LL; ; ++i )
{
v6 = v4;
v7 = (unsigned __int8)*v4 - 35;
if ( *v4 == 35 )
v7 = (unsigned __int8)v4[1];
v4 += 1025;
if ( !v7 )
break;
argv[i] = v6;
}
argv[(int)i] = 0LL;
execvp(&commands, argv);
}
else
{
execlp(&commands, &commands, 0LL);
}
}
error_process();
return waitpid(v3, &stat_loc, 0);
}

canonical_decompiled_code:
__pid_t func0()
{
unsigned int v0; // eax
__pid_t v2; // eax
__pid_t v3; // ebp
char *v4; // rdx
__int64 i; // rcx
char *v6; // rsi
int v7; // eax
int stat_loc; // [rsp+Ch] [rbp-201Ch] BYREF
char *argv[1027]; // [rsp+10h] [rbp-2018h] BYREF

v0 = isbuildin(&tokenString);
if ( v0 )
return runbuildin(v0);
v2 = fork();
v3 = v2;
if ( v2 < 0 )
{
fwrite("fork error...", 1uLL, 0xDuLL, stderr);
}
else if ( !v2 )
{
if ( arg[1025] )
{
v4 = arg;
for ( i = 0LL; ; ++i )
{
v6 = v4;
v7 = (unsigned __int8)*v4 - 35;
if ( *v4 == 35 )
v7 = (unsigned __int8)v4[1];
v4 += 1025;
if ( !v7 )
break;
argv[i] = v6;
}
argv[(int)i] = 0LL;
execvp(&commands, argv);
}
else
{
execlp(&commands, &commands, 0LL);
}
}
error_process();
return waitpid(v3, &stat_loc, 0);
}

original_code:
void forktoexec()
{
 pid_t pid;
 int i;
 int status;
 char *arg_temp[1024 + 1];
 BuildInType command;
 if ( (command = isbuildin(tokenString)) )
  runbuildin(command);
 else{
  if ((pid = fork()) < 0)
   fprintf(stderr, "fork error...");
  else if (pid == 0){
   if (arg[1][0] != 0){
    for (i = 0; strcmp(arg[i], "#") != 0; i++)
     arg_temp[i] = arg[i];
    arg_temp[i] = ((void *)0);
    execvp(commands, arg_temp);
   }
   else{
    execlp(commands, commands, (char *)0);
   }
  }
  error_process();
  if ((pid == waitpid(pid, &status, 0)) < 0)
   fprintf(stderr, "waitpid error...");
  return ;
 }
}

canonical_original_code:
void forktoexec()
{
 int pid;
 int i;
 int status;
 char *arg_temp[-1];
 enum BuildInType command;
 if ( (command = isbuildin(tokenString)) )
  runbuildin(command);
 else{
  if ((pid = fork()) < 0)
   fprintf(stderr, "fork error...");
  else if (pid == 0){
   if (arg[1][0] != 0){
    for (i = 0; strcmp(arg[i], "#") != 0; i++)
     arg_temp[i] = arg[i];
    arg_temp[i] = ((void *)0);
    execvp(commands, arg_temp);
   }
   else{
    execlp(commands, commands, (char *)0);
   }
  }
  error_process();
  if ((pid == waitpid(pid, &status, 0)) < 0)
   fprintf(stderr, "waitpid error...");
  return ;
 }
}

variable_types: pid int

variable_types: status int

variable_types: enum BuildInType { NO=0, CD=1, PWD=2, EXIT=3, HISTORY=4 }


repo: dingmaotu/ldcell

variable_types:
a
double [3]

variable_types:
b
double [3]

variable_types:
c
double [3]


decompiled_code:
void __fastcall real_prod(double *a1, double a2)
{
double v2; // xmm1_8
double v3; // xmm0_8

*a1 = *a1 * a2;
v2 = a1[1] * a2;
v3 = a2 * a1[2];
a1[1] = v2;
a1[2] = v3;
}

original_code:
void real_prod(double a[3], double num)
{
    do{ for(int __i=0; __i<3; ++__i) { a[__i] *= num; } }while(0);
}

canonical_original_code:
void real_prod(double a[3], double num)
{
    do{ for(int __i = 0; __i<3; ++__i) { a[__i] *= num; } }while(0);
}

variable_types: num double


decompiled_code:
void __fastcall scale_cell(double *a1, double a2)
{
double v2; // xmm2_8
double v3; // xmm0_8

v2 = a2;
if ( a2 < 0.0 )
{
v3 = COERCE_DOUBLE(cell_volume(a1));
v2 = pow(COERCE_DOUBLE(*(_QWORD *)&v2 ^ LC1) / v3, 0.3333333333333333);
}
real_prod(a1, v2);
real_prod(a1 + 3, v2);
real_prod(a1 + 6, v2);
}

canonical_decompiled_code:
void __fastcall func3(double *a1, double a2)
{
double v2; // xmm2_8
double v3; // xmm0_8

v2 = a2;
if ( a2 < 0.0 )
{
v3 = COERCE_DOUBLE(cell_volume(a1));
v2 = pow(COERCE_DOUBLE(*(_QWORD *)&v2 ^ LC1) / v3, 0.3333333333333333);
}
func1(a1, v2);
func1(a1 + 3, v2);
func1(a1 + 6, v2);
}

original_code:
void scale_cell(double c[3][3], double s)
{
    if (s < 0)
        s = pow(-s/cell_volume(c), 1.0/3);
    for(int i=0; i<3; ++i) {
        real_prod(c[i], s);
    }
}

canonical_original_code:
void scale_cell(double c[3][3], double s)
{
    if (s < 0)
        s = pow(-s/cell_volume(c), 1.0/3);
    for(int i = 0; i<3; ++i) {
        real_prod(c[i], s);
    }
}

variable_types:
c
double [3][3]

variable_types: s double


sighandler

repo: dingram/led-display

decompiled_code:
void __noreturn sighandler()
{
puts("Cleaning up...");
ldisplay_reset();
ldisplay_cleanup();
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
puts("Cleaning up...");
ldisplay_reset();
ldisplay_cleanup();
exit(0);
}

original_code:
static void sighandler(int sig) {
  printf("Cleaning up...\n");
  ldisplay_reset();
 ldisplay_cleanup();
  exit(0);
}

variable_types: sig int


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
puts("Usage:");
printf(" %s [--no-totally-unnecessary-brightness-flicker]\n\n", a1);
puts(" -s, --no-totally-unnecessary-brightness-flicker Disable brightness flicker");
puts(" when updating the time.");
puts(" --help Display this screen.");
putchar(10);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
puts("Usage:");
printf(" %s [--no-totally-unnecessary-brightness-flicker]\n\n", a1);
puts(" -s, --no-totally-unnecessary-brightness-flicker Disable brightness flicker");
puts(" when updating the time.");
puts(" --help Display this screen.");
putchar(10);
exit(0);
}

original_code:
static void usage(char *progname) {
  printf("Usage:\n");
  printf("    %s [--no-totally-unnecessary-brightness-flicker]\n\n", progname);
  printf("  -s, --no-totally-unnecessary-brightness-flicker  Disable brightness flicker\n");
  printf("                                                   when updating the time.\n");
  printf("  --help                                           Display this screen.\n");
  printf("\n");
  exit(0);
}

variable_types:
progname
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__sigset_t *v4; // rdi
__int64 i; // rcx
const char *v6; // rsi
unsigned int v7; // er13
__int64 v8; // rdx
int v9; // ebp
struct tm *v10; // rax
unsigned int v11; // ebx
unsigned int v12; // eax
int v14; // [rsp+4h] [rbp-D4h] BYREF
int longind[2]; // [rsp+8h] [rbp-D0h] BYREF
struct sigaction longopts; // [rsp+10h] [rbp-C8h] BYREF

longopts.sa_handler = (__sighandler_t)"no-totally-unnecessary-brightness-flicker";
longopts.sa_mask.__val[1] = (unsigned __int64)&v14;
longopts.sa_mask.__val[5] = (unsigned __int64)&v14;
v14 = 1;
LODWORD(longopts.sa_mask.__val[0]) = 0;
LODWORD(longopts.sa_mask.__val[2]) = 0;
longopts.sa_mask.__val[3] = (unsigned __int64)"disable-totally-unnecessary-brightness-flicker";
LODWORD(longopts.sa_mask.__val[4]) = 0;
LODWORD(longopts.sa_mask.__val[6]) = 0;
longopts.sa_mask.__val[7] = (unsigned __int64)"help";
LODWORD(longopts.sa_mask.__val[8]) = 0;
longopts.sa_mask.__val[9] = 0LL;
LODWORD(longopts.sa_mask.__val[10]) = 0;
longopts.sa_mask.__val[11] = 0LL;
LODWORD(longopts.sa_mask.__val[12]) = 0;
longopts.sa_mask.__val[13] = 0LL;
LODWORD(longopts.sa_mask.__val[14]) = 0;
while ( 1 )
{
longind[0] = 0;
v3 = getopt_long(argc, (char *const *)argv, "s", (const struct option *)&longopts, longind);
if ( v3 == -1 )
break;
if ( v3 == 63 )
exit(1);
if ( v3 == 115 )
{
v14 = 0;
}
else if ( v3 )
{
printf("??? getopt returned character code 0x%x ???\n", v3);
}
else if ( longind[0] == 2 )
{
usage(*argv);
}
}
setbuf(stdout, 0LL);
puts("LED Display: clock program");
puts("Written by Dave Ingram");
puts("-----------------------------------------------");
puts(" http://www.dmi.me.uk/code/linux/leddisplay/ \n");
if ( (unsigned int)ldisplay_init() )
{
fwrite("\x1B[1;31mDevice failed to initialise!\x1B[0m\n", 1uLL, 0x28uLL, stderr);
return 1;
}
v4 = &longopts.sa_mask;
for ( i = 36LL; i; --i )
{
LODWORD(v4->__val[0]) = 0;
v4 = (__sigset_t *)((char *)v4 + 4);
}
longopts.sa_handler = (__sighandler_t)sighandler;
sigaction(1, &longopts, 0LL);
sigaction(2, &longopts, 0LL);
v6 = "\x1B[1;31mDevice failed to reset: %d\x1B[0m\n";
v7 = ldisplay_reset();
v8 = v7;
if ( !v7 )
{
ldisplay_setBrightness(0LL, "\x1B[1;31mDevice failed to reset: %d\x1B[0m\n", 0LL);
LABEL_16:
v9 = 5;
*(_QWORD *)longind = time(0LL);
v10 = localtime((const time_t *)longind);
v11 = v10->tm_min + 100 * v10->tm_hour;
while ( !v14 )
{
v12 = ldisplay_showTime(v11, 0LL);
if ( v12 )
goto LABEL_27;
usleep(0x186A0u);
if ( v11 != v7 )
goto LABEL_20;
v6 = 0LL;
v12 = ldisplay_showTime(v11, 0LL);
if ( v12 )
goto LABEL_27;
LABEL_22:
usleep(0x493E0u);
if ( !--v9 )
goto LABEL_16;
}
if ( v11 == v7 )
{
v12 = ldisplay_showTime(v11, 0LL);
if ( !v12 )
{
usleep(0x186A0u);
LABEL_21:
v6 = 0LL;
v12 = ldisplay_showTime(v11, 0LL);
if ( !v12 )
goto LABEL_22;
goto LABEL_27;
}
}
else
{
ldisplay_setBrightness(2LL, v6);
v12 = ldisplay_showTime(v11, 0LL);
if ( !v12 )
{
usleep(0x186A0u);
LABEL_20:
v7 = v11;
ldisplay_setBrightness(0LL, 0LL);
goto LABEL_21;
}
}
LABEL_27:
v8 = v12;
v6 = "\x1B[1;31mDevice failed to respond: %d\x1B[0m\n";
}
fprintf(stderr, v6, v8);
ldisplay_cleanup();
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__sigset_t *v4; // rdi
__int64 i; // rcx
const char *v6; // rsi
unsigned int v7; // er13
__int64 v8; // rdx
int v9; // ebp
struct tm *v10; // rax
unsigned int v11; // ebx
unsigned int v12; // eax
int v14; // [rsp+4h] [rbp-D4h] BYREF
int longind[2]; // [rsp+8h] [rbp-D0h] BYREF
struct sigaction longopts; // [rsp+10h] [rbp-C8h] BYREF

longopts.sa_handler = (__sighandler_t)"no-totally-unnecessary-brightness-flicker";
longopts.sa_mask.__val[1] = (unsigned __int64)&v14;
longopts.sa_mask.__val[5] = (unsigned __int64)&v14;
v14 = 1;
LODWORD(longopts.sa_mask.__val[0]) = 0;
LODWORD(longopts.sa_mask.__val[2]) = 0;
longopts.sa_mask.__val[3] = (unsigned __int64)"disable-totally-unnecessary-brightness-flicker";
LODWORD(longopts.sa_mask.__val[4]) = 0;
LODWORD(longopts.sa_mask.__val[6]) = 0;
longopts.sa_mask.__val[7] = (unsigned __int64)"help";
LODWORD(longopts.sa_mask.__val[8]) = 0;
longopts.sa_mask.__val[9] = 0LL;
LODWORD(longopts.sa_mask.__val[10]) = 0;
longopts.sa_mask.__val[11] = 0LL;
LODWORD(longopts.sa_mask.__val[12]) = 0;
longopts.sa_mask.__val[13] = 0LL;
LODWORD(longopts.sa_mask.__val[14]) = 0;
while ( 1 )
{
longind[0] = 0;
v3 = getopt_long(argc, (char *const *)argv, "s", (const struct option *)&longopts, longind);
if ( v3 == -1 )
break;
if ( v3 == 63 )
exit(1);
if ( v3 == 115 )
{
v14 = 0;
}
else if ( v3 )
{
printf("??? getopt returned character code 0x%x ???\n", v3);
}
else if ( longind[0] == 2 )
{
func1(*argv);
}
}
setbuf(stdout, 0LL);
puts("LED Display: clock program");
puts("Written by Dave Ingram");
puts("-----------------------------------------------");
puts(" http://www.dmi.me.uk/code/linux/leddisplay/ \n");
if ( (unsigned int)ldisplay_init() )
{
fwrite("\x1B[1;31mDevice failed to initialise!\x1B[0m\n", 1uLL, 0x28uLL, stderr);
return 1;
}
v4 = &longopts.sa_mask;
for ( i = 36LL; i; --i )
{
LODWORD(v4->__val[0]) = 0;
v4 = (__sigset_t *)((char *)v4 + 4);
}
longopts.sa_handler = (__sighandler_t)sighandler;
sigaction(1, &longopts, 0LL);
sigaction(2, &longopts, 0LL);
v6 = "\x1B[1;31mDevice failed to reset: %d\x1B[0m\n";
v7 = ldisplay_reset();
v8 = v7;
if ( !v7 )
{
ldisplay_setBrightness(0LL, "\x1B[1;31mDevice failed to reset: %d\x1B[0m\n", 0LL);
LABEL_16:
v9 = 5;
*(_QWORD *)longind = time(0LL);
v10 = localtime((const time_t *)longind);
v11 = v10->tm_min + 100 * v10->tm_hour;
while ( !v14 )
{
v12 = ldisplay_showTime(v11, 0LL);
if ( v12 )
goto LABEL_27;
usleep(0x186A0u);
if ( v11 != v7 )
goto LABEL_20;
v6 = 0LL;
v12 = ldisplay_showTime(v11, 0LL);
if ( v12 )
goto LABEL_27;
LABEL_22:
usleep(0x493E0u);
if ( !--v9 )
goto LABEL_16;
}
if ( v11 == v7 )
{
v12 = ldisplay_showTime(v11, 0LL);
if ( !v12 )
{
usleep(0x186A0u);
LABEL_21:
v6 = 0LL;
v12 = ldisplay_showTime(v11, 0LL);
if ( !v12 )
goto LABEL_22;
goto LABEL_27;
}
}
else
{
ldisplay_setBrightness(2LL, v6);
v12 = ldisplay_showTime(v11, 0LL);
if ( !v12 )
{
usleep(0x186A0u);
LABEL_20:
v7 = v11;
ldisplay_setBrightness(0LL, 0LL);
goto LABEL_21;
}
}
LABEL_27:
v8 = v12;
v6 = "\x1B[1;31mDevice failed to respond: %d\x1B[0m\n";
}
fprintf(stderr, v6, v8);
ldisplay_cleanup();
return 1;
}

original_code:
int main(int argc, char *argv[]) {
  int opt_flicker = 1;
  int ret;
  {
    struct option long_options[] = {
      { "no-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "disable-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "help", 0, 0, 0},
      { 0, 0, 0, 0 }
    };
    while (1) {
      int opt_index=0;
      int c = getopt_long(argc, argv, "s", long_options, &opt_index);
      if (c == -1)
        break;
      switch (c) {
        case 0:
          if (opt_index==2) {
            usage(argv[0]);
          }
          break;
        case 's':
          opt_flicker=0;
          break;
        case '?':
          exit(1);
        default:
          printf("??? getopt returned character code 0x%x ???\n", c);
      }
    }
  }
 setbuf(stdout,0);
  printf("LED Display: clock program\n");
  printf("Written by Dave Ingram\n");
  printf("-----------------------------------------------\n");
  printf("  http://www.dmi.me.uk/code/linux/leddisplay/  \n\n");
 if (ldisplay_init() != 0) {
    fprintf(stderr, "\033[1;31mDevice failed to initialise!\033[0m\n");
    return 1;
  }
  struct sigaction sigact = {
    .__sigaction_handler.sa_handler = sighandler
  };
  sigaction(1, &sigact, ((void *)0));
  sigaction(2, &sigact, ((void *)0));
  if ((ret = ldisplay_reset())) {
    fprintf(stderr, "\033[1;31mDevice failed to reset: %d\033[0m\n", ret);
    ldisplay_cleanup();
    return 1;
  }
  ldisplay_setBrightness(0);
  int oldtime_int=0;
  while (1) {
    time_t t = time(((void *)0));
    struct tm *curTime = localtime(&t);
    int curtime_int = (100*curTime->tm_hour) + curTime->tm_min;
    int i=0;
    for (i=0; i<5; ++i) {
      if (opt_flicker && oldtime_int!=curtime_int)
        ldisplay_setBrightness(2);
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(100000);
      if (oldtime_int!=curtime_int) {
        oldtime_int = curtime_int;
        ldisplay_setBrightness(0);
      }
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(300000);
    }
  }
  ldisplay_reset();
 ldisplay_cleanup();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int opt_flicker = 1;
  int ret;
  {
    struct option long_options[] = {
      { "no-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "disable-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "help", 0, 0, 0},
      { 0, 0, 0, 0 }
    };
    while (1) {
      int opt_index = 0;
      int c = getopt_long(argc, argv, "s", long_options, &opt_index);
      if (c == -1)
        break;
      switch (c) {
        case 0:
          if (opt_index==2) {
            usage(argv[0]);
          }
          break;
        case 's':
          opt_flicker=0;
          break;
        case '?':
          exit(1);
        default:
          printf("??? getopt returned character code 0x%x ???\n", c);
      }
    }
  }
 setbuf(stdout,0);
  printf("LED Display: clock program\n");
  printf("Written by Dave Ingram\n");
  printf("-----------------------------------------------\n");
  printf("  http://www.dmi.me.uk/code/linux/leddisplay/  \n\n");
 if (ldisplay_init() != 0) {
    fprintf(stderr, "\033[1;31mDevice failed to initialise!\033[0m\n");
    return 1;
  }
  struct sigaction sigact = {
    .__sigaction_handler.sa_handler = sighandler
  };
  sigaction(1, &sigact, ((void *)0));
  sigaction(2, &sigact, ((void *)0));
  if ((ret = ldisplay_reset())) {
    fprintf(stderr, "\033[1;31mDevice failed to reset: %d\033[0m\n", ret);
    ldisplay_cleanup();
    return 1;
  }
  ldisplay_setBrightness(0);
  int oldtime_int = 0;
  while (1) {
    long t = time(((void *)0));
    struct tm *curTime = localtime(&t);
    int curtime_int = (100*curTime->tm_hour) + curTime->tm_min;
    int i = 0;
    for (i=0; i<5; ++i) {
      if (opt_flicker && oldtime_int!=curtime_int)
        ldisplay_setBrightness(2);
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(100000);
      if (oldtime_int!=curtime_int) {
        oldtime_int = curtime_int;
        ldisplay_setBrightness(0);
      }
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(300000);
    }
  }
  ldisplay_reset();
 ldisplay_cleanup();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: c int

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

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: i int

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

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: ricpelo/elcirculo

decompiled_code:
const char *__fastcall get_image_dim(_BYTE *a1, int a2, int a3)
{
if ( a3 == 2 )
return get_jpeg_dim(a1, a2);
if ( a3 == 1 )
return get_png_dim((__int64)a1, a2);
return "(unknown)";
}

canonical_decompiled_code:
const char *__fastcall func4(_BYTE *a1, int a2, int a3)
{
if ( a3 == 2 )
return func2(a1, a2);
if ( a3 == 1 )
return func3((__int64)a1, a2);
return "(unknown)";
}

original_code:
static char *get_image_dim(void *img, int32 extent, int fmt)
{
 if (fmt==2) return get_jpeg_dim(img,extent);
 else if (fmt==1) return get_png_dim(img, extent);
 return "(unknown)";
}

canonical_original_code:
static char *get_image_dim(void *img, int extent, int fmt)
{
 if (fmt==2) return get_jpeg_dim(img,extent);
 else if (fmt==1) return get_png_dim(img, extent);
 return "(unknown)";
}

variable_types:
img
void *

variable_types: extent int

variable_types: fmt int


decompiled_code:
const char *get_biblio()
{
int v0; // eax
unsigned int v1; // er12
char *v2; // rbp
char *v3; // rax
const char *v4; // r12
char *v5; // rax
char *v6; // r13
char *v7; // rax
char *v8; // rbx
const char *v9; // r15
char *v10; // rax
char *v11; // r14
char v12; // al
char *v13; // rdx
char *v14; // rax
char *v15; // r12
const char *v16; // r14
char *v17; // rax
char *v18; // rbx
char v19; // al
char *v20; // rdx
size_t v21; // rax
size_t v23; // rax
__m128i v24; // xmm0

v0 = babel_treaty(261LL, 0LL, 0LL);
if ( v0 <= 0 )
return "No bibliographic data";
v1 = v0;
v2 = (char *)my_malloc((unsigned int)v0, "Metadata buffer");
if ( (int)babel_treaty(777LL, v2, v1) < 0 )
return "No bibliographic data";
v3 = strstr(v2, "<bibliographic>");
v4 = v3;
if ( !v3 )
{
free(v2);
return "No bibliographic data";
}
v5 = strstr(v3, "</bibliographic>");
if ( v5 )
*v5 = 0;
v6 = "No bibliographic data";
v7 = strstr(v4, "<title>");
v8 = v7;
if ( v7 )
{
v9 = v7 + 7;
v10 = strstr(v7 + 7, "</title>");
v11 = v10;
if ( v10 )
{
*v10 = 0;
v12 = v8[7];
if ( v12 )
{
v13 = (char *)v9;
do
{
if ( (unsigned __int8)(v12 - 32) > 0x5Eu )
*v13 = 95;
v12 = *++v13;
}
while ( v12 );
}
sprintf(buffer_0, "\"%s\" ", v9);
*v11 = 60;
v6 = buffer_0;
}
else
{
word_CF0 = 32;
*(__m128i *)buffer_0 = _mm_load_si128((const __m128i *)&LC29);
}
}
v14 = strstr(v4, "<author>");
v15 = v14;
if ( v14 )
{
v16 = v14 + 8;
v17 = strstr(v14 + 8, "</author>");
v18 = v17;
if ( v17 )
{
*v17 = 0;
v19 = v15[8];
if ( v19 )
{
v20 = (char *)v16;
do
{
if ( (unsigned __int8)(v19 - 32) > 0x5Eu )
*v20 = 95;
v19 = *++v20;
}
while ( v19 );
}
v6 = buffer_0;
v21 = strlen(buffer_0);
*(_DWORD *)&buffer_0[v21] = 2128226;
strcpy(&buffer_0[v21 + 3], v16);
*v18 = 60;
}
else
{
v23 = strlen(buffer_0);
v24 = _mm_load_si128((const __m128i *)&LC30);
*(__int16 *)((char *)&word_CF0 + v23) = 62;
*(__m128i *)&buffer_0[v23] = v24;
}
}
free(v2);
return v6;
}

canonical_decompiled_code:
const char *func6()
{
int v0; // eax
unsigned int v1; // er12
char *v2; // rbp
char *v3; // rax
const char *v4; // r12
char *v5; // rax
char *v6; // r13
char *v7; // rax
char *v8; // rbx
const char *v9; // r15
char *v10; // rax
char *v11; // r14
char v12; // al
char *v13; // rdx
char *v14; // rax
char *v15; // r12
const char *v16; // r14
char *v17; // rax
char *v18; // rbx
char v19; // al
char *v20; // rdx
size_t v21; // rax
size_t v23; // rax
__m128i v24; // xmm0

v0 = babel_treaty(261LL, 0LL, 0LL);
if ( v0 <= 0 )
return "No bibliographic data";
v1 = v0;
v2 = (char *)my_malloc((unsigned int)v0, "Metadata buffer");
if ( (int)babel_treaty(777LL, v2, v1) < 0 )
return "No bibliographic data";
v3 = strstr(v2, "<bibliographic>");
v4 = v3;
if ( !v3 )
{
free(v2);
return "No bibliographic data";
}
v5 = strstr(v3, "</bibliographic>");
if ( v5 )
*v5 = 0;
v6 = "No bibliographic data";
v7 = strstr(v4, "<title>");
v8 = v7;
if ( v7 )
{
v9 = v7 + 7;
v10 = strstr(v7 + 7, "</title>");
v11 = v10;
if ( v10 )
{
*v10 = 0;
v12 = v8[7];
if ( v12 )
{
v13 = (char *)v9;
do
{
if ( (unsigned __int8)(v12 - 32) > 0x5Eu )
*v13 = 95;
v12 = *++v13;
}
while ( v12 );
}
sprintf(buffer_0, "\"%s\" ", v9);
*v11 = 60;
v6 = buffer_0;
}
else
{
word_CF0 = 32;
*(__m128i *)buffer_0 = _mm_load_si128((const __m128i *)&LC29);
}
}
v14 = strstr(v4, "<author>");
v15 = v14;
if ( v14 )
{
v16 = v14 + 8;
v17 = strstr(v14 + 8, "</author>");
v18 = v17;
if ( v17 )
{
*v17 = 0;
v19 = v15[8];
if ( v19 )
{
v20 = (char *)v16;
do
{
if ( (unsigned __int8)(v19 - 32) > 0x5Eu )
*v20 = 95;
v19 = *++v20;
}
while ( v19 );
}
v6 = buffer_0;
v21 = strlen(buffer_0);
*(_DWORD *)&buffer_0[v21] = 2128226;
strcpy(&buffer_0[v21 + 3], v16);
*v18 = 60;
}
else
{
v23 = strlen(buffer_0);
v24 = _mm_load_si128((const __m128i *)&LC30);
*(__int16 *)((char *)&word_CF0 + v23) = 62;
*(__m128i *)&buffer_0[v23] = v24;
}
}
free(v2);
return v6;
}

original_code:
static char *get_biblio(void)
{
 int32 i;
 char *md;
 char *bib="No bibliographic data";
 char *bibb; char *bibe;
 char *t;
 static char buffer[512];
 i=babel_treaty(0x105,((void *)0),0);
 if (i<=0) return bib;
 md=(char *) my_malloc(i,"Metadata buffer");
 if (babel_treaty(0x309,md,i)<0) return bib;
 bibb=strstr(md,"<bibliographic>");
 if (!bibb) { free(md); return bib; }
 bibe=strstr(bibb,"</bibliographic>");
 if (bibe) *bibe=0;
 t=strstr(bibb,"<title>");
 if (t)
 {
  t+=7;
  bibe=strstr(t,"</title>");
  if (bibe)
  {
    *bibe=0;
    bib=buffer;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    sprintf(buffer, "\"%s\" ",t);
    *bibe='<';
  }
  else strcpy(buffer,"<no title found> ");
 }
 t=strstr(bibb,"<author>");
 if (t)
 {
  t+=8;
  bibe=strstr(t,"</author>");
  if (bibe)
  {
    bib=buffer;
    *bibe=0;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    strcat(buffer, "by ");
    strcat(buffer, t);
    *bibe='<';
  }
  else strcat(buffer, "<no author found>");
 }
 free(md);
 return bib;
}

canonical_original_code:
static char *get_biblio()
{
 int i;
 char *md;
 char *bib = "No bibliographic data";
 char *bibb; char *bibe;
 char *t;
 static char buffer[512];
 i=babel_treaty(0x105,((void *)0),0);
 if (i<=0) return bib;
 md=(char *) my_malloc(i,"Metadata buffer");
 if (babel_treaty(0x309,md,i)<0) return bib;
 bibb=strstr(md,"<bibliographic>");
 if (!bibb) { free(md); return bib; }
 bibe=strstr(bibb,"</bibliographic>");
 if (bibe) *bibe=0;
 t=strstr(bibb,"<title>");
 if (t)
 {
  t+=7;
  bibe=strstr(t,"</title>");
  if (bibe)
  {
    *bibe=0;
    bib=buffer;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    sprintf(buffer, "\"%s\" ",t);
    *bibe='<';
  }
  else strcpy(buffer,"<no title found> ");
 }
 t=strstr(bibb,"<author>");
 if (t)
 {
  t+=8;
  bibe=strstr(t,"</author>");
  if (bibe)
  {
    bib=buffer;
    *bibe=0;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    strcat(buffer, "by ");
    strcat(buffer, t);
    *bibe='<';
  }
  else strcat(buffer, "<no author found>");
 }
 free(md);
 return bib;
}

variable_types:
md
char *

variable_types:
bib
char *

variable_types:
bibb
char *

variable_types:
bibe
char *

variable_types:
t
char *

variable_types:
buffer
char [512]


decompiled_code:
size_t babel_story_ifid()
{
int v0; // ebx
size_t result; // rax
const char *v2; // rsi
char v3[520]; // [rsp+0h] [rbp-208h] BYREF

v0 = babel_treaty(776LL, v3, 512LL);
result = (size_t)strtok(v3, ",");
if ( result )
{
v2 = (const char *)result;
do
{
printf("IFID: %s\n", v2);
result = (size_t)strtok(0LL, ",");
v2 = (const char *)result;
}
while ( result );
}
if ( !v0 )
result = fwrite(
"Unable to create an IFID (A serious problem occurred while loading the file).\n",
1uLL,
0x4EuLL,
stderr);
return result;
}

canonical_decompiled_code:
size_t func7()
{
int v0; // ebx
size_t result; // rax
const char *v2; // rsi
char v3[520]; // [rsp+0h] [rbp-208h] BYREF

v0 = babel_treaty(776LL, v3, 512LL);
result = (size_t)strtok(v3, ",");
if ( result )
{
v2 = (const char *)result;
do
{
printf("IFID: %s\n", v2);
result = (size_t)strtok(0LL, ",");
v2 = (const char *)result;
}
while ( result );
}
if ( !v0 )
result = fwrite(
"Unable to create an IFID (A serious problem occurred while loading the file).\n",
1uLL,
0x4EuLL,
stderr);
return result;
}

original_code:
void babel_story_ifid()
{
  char buffer[512];
  char *ep;
  int i;
  i=babel_treaty(0x308,buffer,512);
  ep=strtok(buffer, ",");
  while(ep)
  {
   printf("IFID: %s\n",ep);
   ep=strtok(((void *)0),",");
  }
  if (!i)
    fprintf(stderr,"Unable to create an IFID (A serious problem occurred while loading the file).\n");
}

variable_types:
ep
char *


decompiled_code:
int babel_story_format()
{
const char *v0; // rbp
int result; // eax

v0 = (const char *)babel_get_format();
if ( !v0 )
v0 = "unknown";
if ( (unsigned int)babel_get_authoritative() )
result = printf("Format: %s\n", v0);
else
result = printf("Format: %s (non-authoritative)\n", v0);
return result;
}

canonical_decompiled_code:
int func8()
{
const char *v0; // rbp
int result; // eax

v0 = (const char *)babel_get_format();
if ( !v0 )
v0 = "unknown";
if ( (unsigned int)babel_get_authoritative() )
result = printf("Format: %s\n", v0);
else
result = printf("Format: %s (non-authoritative)\n", v0);
return result;
}

original_code:
void babel_story_format()
{
  char *b;
  b=babel_get_format();
  if (!b) b="unknown";
  if (!babel_get_authoritative())
   printf("Format: %s (non-authoritative)\n",b);
  else printf("Format: %s\n",b);
}

variable_types:
b
char *


decompiled_code:
void babel_story_ifiction()
{
deep_babel_ifiction(1);
}

canonical_decompiled_code:
void func9()
{
func1(1);
}


decompiled_code:
void babel_story_cover()
{
deep_babel_cover(1);
}

canonical_decompiled_code:
void func10()
{
func5(1);
}


decompiled_code:
void babel_story_fish()
{
deep_babel_ifiction(0);
deep_babel_cover(0);
}

canonical_decompiled_code:
void func11()
{
func1(0);
func5(0);
}


decompiled_code:
int babel_story_identify()
{
const char *v0; // rdi
const char *v1; // rbp
int v2; // eax
int v3; // er12
int v4; // er13
unsigned int v5; // er12
int v6; // eax
int v7; // ebx
const char *v8; // rcx
_BYTE *v10; // r14
const char *v11; // rax
const char *v12; // r14
const char *v13; // r13
size_t v14; // rax
const char *v15; // rcx
char v16[552]; // [rsp+0h] [rbp-228h] BYREF

v0 = get_biblio();
puts(v0);
babel_story_ifid();
v1 = (const char *)babel_get_format();
if ( !v1 )
v1 = "unknown";
v2 = babel_get_length();
v3 = v2 + 1023;
if ( v2 >= 0 )
v3 = v2;
v4 = babel_treaty(262LL, 0LL, 0LL);
v5 = v3 >> 10;
v6 = babel_treaty(263LL, 0LL, 0LL);
if ( v4 <= 0
|| (v7 = v6, v6 <= 0)
|| (v10 = (_BYTE *)my_malloc((unsigned int)v4, "Image buffer"), (int)babel_treaty(778LL, v10, (unsigned int)v4) < 0) )
{
v8 = "no cover";
}
else
{
v11 = get_image_dim(v10, v4, v7);
v12 = v11 + 1;
v13 = v11;
v14 = strlen(v11 + 1);
v15 = "jpeg";
v13[v14] = 0;
if ( v7 != 2 )
{
v15 = "png";
if ( v7 != 1 )
v15 = "unknown format";
}
sprintf(v16, "cover %s %s", v12, v15);
v8 = v16;
}
return printf("%s, %dk, %s\n", v1, v5, v8);
}

canonical_decompiled_code:
int func12()
{
const char *v0; // rdi
const char *v1; // rbp
int v2; // eax
int v3; // er12
int v4; // er13
unsigned int v5; // er12
int v6; // eax
int v7; // ebx
const char *v8; // rcx
_BYTE *v10; // r14
const char *v11; // rax
const char *v12; // r14
const char *v13; // r13
size_t v14; // rax
const char *v15; // rcx
char v16[552]; // [rsp+0h] [rbp-228h] BYREF

v0 = func6();
puts(v0);
func7();
v1 = (const char *)babel_get_format();
if ( !v1 )
v1 = "unknown";
v2 = babel_get_length();
v3 = v2 + 1023;
if ( v2 >= 0 )
v3 = v2;
v4 = babel_treaty(262LL, 0LL, 0LL);
v5 = v3 >> 10;
v6 = babel_treaty(263LL, 0LL, 0LL);
if ( v4 <= 0
|| (v7 = v6, v6 <= 0)
|| (v10 = (_BYTE *)my_malloc((unsigned int)v4, "Image buffer"), (int)babel_treaty(778LL, v10, (unsigned int)v4) < 0) )
{
v8 = "no cover";
}
else
{
v11 = func4(v10, v4, v7);
v12 = v11 + 1;
v13 = v11;
v14 = strlen(v11 + 1);
v15 = "jpeg";
v13[v14] = 0;
if ( v7 != 2 )
{
v15 = "png";
if ( v7 != 1 )
v15 = "unknown format";
}
sprintf(v16, "cover %s %s", v12, v15);
v8 = v16;
}
return printf("%s, %dk, %s\n", v1, v5, v8);
}

original_code:
void babel_story_identify()
{
 int32 i, j, l;
 char *b, *cf, *dim;
 char buffer[512];
 printf("%s\n",get_biblio());
 babel_story_ifid();
 b=babel_get_format();
 if (!b) b="unknown";
 l=babel_get_length() / 1024;
 i=babel_treaty(0x106,((void *)0),0);
 j=babel_treaty(0x107,((void *)0),0);
 if (i<=0 || j<=0)
 {
  cf="no cover";
 }
 else
 {
  char *md=my_malloc(i,"Image buffer");
  if (babel_treaty(0x30A,md,i)<0)
  {
   cf="no cover";
  }
  else
  {
   dim=get_image_dim(md,i,j)+1;
   dim[strlen(dim)-1]=0;
   if (j==2) cf="jpeg";
   else if (j==1) cf="png";
   else cf="unknown format";
   sprintf(buffer,"cover %s %s",dim,cf);
   cf=buffer;
  }
 }
 printf("%s, %dk, %s\n",b, l,cf);
}

canonical_original_code:
void babel_story_identify()
{
 int i; int j; int l;
 char *b; char *cf; char *dim;
 char buffer[512];
 printf("%s\n",get_biblio());
 babel_story_ifid();
 b=babel_get_format();
 if (!b) b="unknown";
 l=babel_get_length() / 1024;
 i=babel_treaty(0x106,((void *)0),0);
 j=babel_treaty(0x107,((void *)0),0);
 if (i<=0 || j<=0)
 {
  cf="no cover";
 }
 else
 {
  char *md = my_malloc(i,"Image buffer");
  if (babel_treaty(0x30A,md,i)<0)
  {
   cf="no cover";
  }
  else
  {
   dim=get_image_dim(md,i,j)+1;
   dim[strlen(dim)-1]=0;
   if (j==2) cf="jpeg";
   else if (j==1) cf="png";
   else cf="unknown format";
   sprintf(buffer,"cover %s %s",dim,cf);
   cf=buffer;
  }
 }
 printf("%s, %dk, %s\n",b, l,cf);
}

variable_types: j int

variable_types: l int

variable_types:
cf
char *

variable_types:
dim
char *


decompiled_code:
void babel_story_meta()
{
deep_babel_ifiction(2);
}

canonical_decompiled_code:
void func13()
{
func1(2);
}


decompiled_code:
int babel_story_unblorb()
{
deep_babel_ifiction(1);
deep_babel_cover(1);
return babel_story_story();
}

canonical_decompiled_code:
int func15()
{
func1(1);
func5(1);
return func14();
}

original_code:
void babel_story_unblorb()
{
 deep_babel_ifiction(1);
 deep_babel_cover(1);
 babel_story_story();
}


decompiled_code:
__int64 __fastcall read_int(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

original_code:
static int32 read_int(unsigned char *mem)
{
  int32 i4 = mem[0],
                    i3 = mem[1],
                    i2 = mem[2],
                    i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

canonical_original_code:
static int read_int(unsigned char *mem)
{
  int i4 = mem[0]; int i3 = mem[1]; int i2 = mem[2]; int i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

variable_types:
mem
unsigned char *