decompiled_code:
__int64 __fastcall Display_Stage_2(__int64 a1)
{
int v1; // eax
int v2; // ebp
__int64 v3; // rax
__int64 v4; // r12
char v5; // bl
unsigned __int16 i; // r13
__int64 v7; // r11
int v8; // er10
int v9; // ecx
_BYTE *v10; // r9
_BYTE *v11; // r8
__int64 v12; // rsi
int v13; // eax
__int64 v14; // rdi
unsigned __int16 v15; // r14
int v16; // eax
int v17; // er8
__int64 result; // rax

cnt = 0;
v1 = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8];
DisplayOrgPtr = a1;
Currentframe = v1;
v2 = (unsigned __int16)epd_GetCurrentTimeTick();
v3 = (unsigned __int16)EPD_Type_Index;
do
{
v4 = DisplayOrgPtr;
if ( COG_Parameters[10 * v3 + 6] )
{
v5 = -64;
for ( i = 0; ; v5 = ScanTable[i & 3] )
{
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * v3 + 4]));
v7 = (unsigned __int16)EPD_Type_Index;
v8 = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 5];
v9 = v8 - 1;
if ( (_WORD)v8 )
{
v10 = DataLineOdd;
v11 = DataLineEven;
v12 = 0LL;
do
{
v13 = *(unsigned __int8 *)(v4 + v12);
v14 = (unsigned __int16)v9--;
v10[v12] = *(_BYTE *)(v4 + v12) & 0xAA;
++v12;
v11[v14] = ((_BYTE)v13 << 7) | (v13 >> 1) & 8 | (8 * v13) & 0x20 | (v13 >> 5) & 2;
}
while ( (_WORD)v9 != 0xFFFF );
v4 += (unsigned __int16)v8;
}
v15 = i++ >> 2;
*(_BYTE *)(DataLineScan + v15) = v5;
epd_SPI_Send(10LL, &COG_Line, LOBYTE(COG_Parameters[10 * v7 + 7]));
epd_SPI_Send_Byte(2LL, 37LL);
*(_BYTE *)(DataLineScan + v15) = 0;
v3 = (unsigned __int16)EPD_Type_Index;
if ( (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 6] <= i )
break;
}
}
if ( COG_Parameters[10 * v3 + 8] )
{
while ( (unsigned int)epd_GetCurrentTimeTick() - v2 < Currentframe
&& (unsigned int)epd_GetCurrentTimeTick() - v2 < Currentframe )
;
}
v16 = epd_GetCurrentTimeTick();
++cnt;
v17 = v16;
v3 = (unsigned __int16)EPD_Type_Index;
Currentframe = v17 + (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8] - v2;
}
while ( Currentframe < (unsigned int)StageTime );
do
result = (unsigned int)epd_GetCurrentTimeTick() - v2;
while ( (unsigned int)result < StageTime );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v1; // eax
int v2; // ebp
__int64 v3; // rax
__int64 v4; // r12
char v5; // bl
unsigned __int16 i; // r13
__int64 v7; // r11
int v8; // er10
int v9; // ecx
_BYTE *v10; // r9
_BYTE *v11; // r8
__int64 v12; // rsi
int v13; // eax
__int64 v14; // rdi
unsigned __int16 v15; // r14
int v16; // eax
int v17; // er8
__int64 result; // rax

cnt = 0;
v1 = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8];
DisplayOrgPtr = a1;
Currentframe = v1;
v2 = (unsigned __int16)epd_GetCurrentTimeTick();
v3 = (unsigned __int16)EPD_Type_Index;
do
{
v4 = DisplayOrgPtr;
if ( COG_Parameters[10 * v3 + 6] )
{
v5 = -64;
for ( i = 0; ; v5 = ScanTable[i & 3] )
{
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * v3 + 4]));
v7 = (unsigned __int16)EPD_Type_Index;
v8 = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 5];
v9 = v8 - 1;
if ( (_WORD)v8 )
{
v10 = DataLineOdd;
v11 = DataLineEven;
v12 = 0LL;
do
{
v13 = *(unsigned __int8 *)(v4 + v12);
v14 = (unsigned __int16)v9--;
v10[v12] = *(_BYTE *)(v4 + v12) & 0xAA;
++v12;
v11[v14] = ((_BYTE)v13 << 7) | (v13 >> 1) & 8 | (8 * v13) & 0x20 | (v13 >> 5) & 2;
}
while ( (_WORD)v9 != 0xFFFF );
v4 += (unsigned __int16)v8;
}
v15 = i++ >> 2;
*(_BYTE *)(DataLineScan + v15) = v5;
epd_SPI_Send(10LL, &COG_Line, LOBYTE(COG_Parameters[10 * v7 + 7]));
epd_SPI_Send_Byte(2LL, 37LL);
*(_BYTE *)(DataLineScan + v15) = 0;
v3 = (unsigned __int16)EPD_Type_Index;
if ( (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 6] <= i )
break;
}
}
if ( COG_Parameters[10 * v3 + 8] )
{
while ( (unsigned int)epd_GetCurrentTimeTick() - v2 < Currentframe
&& (unsigned int)epd_GetCurrentTimeTick() - v2 < Currentframe )
;
}
v16 = epd_GetCurrentTimeTick();
++cnt;
v17 = v16;
v3 = (unsigned __int16)EPD_Type_Index;
Currentframe = v17 + (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8] - v2;
}
while ( Currentframe < (unsigned int)StageTime );
do
result = (unsigned int)epd_GetCurrentTimeTick() - v2;
while ( (unsigned int)result < StageTime );
return result;
}

original_code:
static void Display_Stage_2 (uint8_t *PreviousPicture)
{
 uint16_t i, j, k;
 uint8_t TempByte;
 uint16_t StartClock;
 DisplayOrgPtr = PreviousPicture;
 cnt = 0;
 Currentframe = (uint32_t)COG_Parameters[EPD_Type_Index].FrameTime;
 StartClock = epd_GetCurrentTimeTick();
 do
 {
     PreviousPicture = DisplayOrgPtr;
  for (i = 0; i < COG_Parameters[EPD_Type_Index].VERTICAL; i++)
  {
   epd_SPI_Send_Byte(0x04, COG_Parameters[EPD_Type_Index].VoltageLevel);
      k = COG_Parameters[EPD_Type_Index].HORIZONTAL-1;
   for (j = 0; j < COG_Parameters[EPD_Type_Index].HORIZONTAL; j++)
   {
    TempByte =(*PreviousPicture++);
       DataLineOdd[j] = ((TempByte & 0x80) ? (0x80) : (0x00))
          | ((TempByte & 0x20) ? (0x20) : (0x00))
          | ((TempByte & 0x08) ? (0x08) : (0x00))
          | ((TempByte & 0x02) ? (0x02) : (0x00));
    DataLineEven[k--] = ((TempByte & 0x01) ? (0x80) : (0x00))
          | ((TempByte & 0x04) ? (0x20) : (0x00))
          | ((TempByte & 0x10) ? (0x08) : (0x00))
          | ((TempByte & 0x40) ? (0x02) : (0x00));
   }
   DataLineScan[(i>>2)] = ScanTable[(i%4)];
   epd_SPI_Send(0x0a, (uint8_t *)&COG_Line.uint8, COG_Parameters[EPD_Type_Index].DataLineSize);
   epd_SPI_Send_Byte(0x02, 0x25);
   DataLineScan[(i>>2)] = 0;
  }
  if(COG_Parameters[EPD_Type_Index].FrameTime>0)
  {
   while(Currentframe>(epd_GetCurrentTimeTick() - StartClock));
  }
  Currentframe=(uint32_t)(epd_GetCurrentTimeTick() - StartClock)+COG_Parameters[EPD_Type_Index].FrameTime ;
  cnt++;
 }while (StageTime>Currentframe);
 while(StageTime>(epd_GetCurrentTimeTick() - StartClock));
}

canonical_original_code:
static void Display_Stage_2(unsigned char *PreviousPicture)
{
 unsigned short i; unsigned short j; unsigned short k;
 unsigned char TempByte;
 unsigned short StartClock;
 DisplayOrgPtr = PreviousPicture;
 cnt = 0;
 Currentframe = (unsigned int)COG_Parameters[EPD_Type_Index].FrameTime;
 StartClock = epd_GetCurrentTimeTick();
 do
 {
     PreviousPicture = DisplayOrgPtr;
  for (i = 0; i < COG_Parameters[EPD_Type_Index].VERTICAL; i++)
  {
   epd_SPI_Send_Byte(0x04, COG_Parameters[EPD_Type_Index].VoltageLevel);
      k = COG_Parameters[EPD_Type_Index].HORIZONTAL-1;
   for (j = 0; j < COG_Parameters[EPD_Type_Index].HORIZONTAL; j++)
   {
    TempByte =(*PreviousPicture++);
       DataLineOdd[j] = ((TempByte & 0x80) ? (0x80) : (0x00))
          | ((TempByte & 0x20) ? (0x20) : (0x00))
          | ((TempByte & 0x08) ? (0x08) : (0x00))
          | ((TempByte & 0x02) ? (0x02) : (0x00));
    DataLineEven[k--] = ((TempByte & 0x01) ? (0x80) : (0x00))
          | ((TempByte & 0x04) ? (0x20) : (0x00))
          | ((TempByte & 0x10) ? (0x08) : (0x00))
          | ((TempByte & 0x40) ? (0x02) : (0x00));
   }
   DataLineScan[(i>>2)] = ScanTable[(i%4)];
   epd_SPI_Send(0x0a, (unsigned char *)&COG_Line.uint8, COG_Parameters[EPD_Type_Index].DataLineSize);
   epd_SPI_Send_Byte(0x02, 0x25);
   DataLineScan[(i>>2)] = 0;
  }
  if(COG_Parameters[EPD_Type_Index].FrameTime>0)
  {
   while(Currentframe>(epd_GetCurrentTimeTick() - StartClock));
  }
  Currentframe=(unsigned int)(epd_GetCurrentTimeTick() - StartClock)+COG_Parameters[EPD_Type_Index].FrameTime ;
  cnt++;
 }while (StageTime>Currentframe);
 while(StageTime>(epd_GetCurrentTimeTick() - StartClock));
}


repo: deoryp/epaper

decompiled_code:
__int64 __fastcall Display_Stage_3(__int64 a1)
{
int v1; // ebp
__int64 v2; // rax
__int64 v3; // r12
char v4; // bl
unsigned __int16 i; // r13
__int64 v6; // r11
unsigned __int16 v7; // di
_BYTE *v8; // r10
_BYTE *v9; // r9
__int64 v10; // rcx
char v11; // dl
unsigned __int16 v12; // si
char v13; // al
unsigned int v14; // eax
unsigned __int16 v15; // r14
int v16; // eax
int v17; // er8
__int64 result; // rax

Currentframe = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8];
DisplayOrgPtr = a1;
cnt = 0;
v1 = (unsigned __int16)epd_GetCurrentTimeTick();
v2 = (unsigned __int16)EPD_Type_Index;
do
{
v3 = DisplayOrgPtr;
if ( COG_Parameters[10 * v2 + 6] )
{
v4 = -64;
for ( i = 0; ; v4 = ScanTable[i & 3] )
{
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * v2 + 4]));
v6 = (unsigned __int16)EPD_Type_Index;
v7 = COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 5];
if ( v7 )
{
v8 = DataLineOdd;
v9 = DataLineEven;
v10 = 0LL;
do
{
v11 = *(_BYTE *)(v3 + v10);
v12 = v7 - 1 - v10;
v13 = (v11 >> 7) & 0xC0;
if ( (v11 & 0x20) != 0 )
v13 = (*(char *)(v3 + v10) >> 7) & 0xC0 | 0x30;
if ( (v11 & 8) != 0 )
v13 |= 0xCu;
if ( (v11 & 2) != 0 )
v13 |= 3u;
v8[v10] = v13;
v14 = -(v11 & 1) & 0xFFFFFFC0;
if ( (v11 & 4) != 0 )
v14 = -(v11 & 1) & 0xFFFFFFC0 | 0x30;
if ( (v11 & 0x10) != 0 )
LOBYTE(v14) = v14 | 0xC;
if ( (v11 & 0x40) != 0 )
LOBYTE(v14) = v14 | 3;
++v10;
v9[v12] = v14;
}
while ( v7 > (unsigned __int16)v10 );
v3 += v7;
}
v15 = i++ >> 2;
*(_BYTE *)(DataLineScan + v15) = v4;
epd_SPI_Send(10LL, &COG_Line, LOBYTE(COG_Parameters[10 * v6 + 7]));
epd_SPI_Send_Byte(2LL, 47LL);
*(_BYTE *)(DataLineScan + v15) = 0;
v2 = (unsigned __int16)EPD_Type_Index;
if ( (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 6] <= i )
break;
}
}
if ( COG_Parameters[10 * v2 + 8] )
{
while ( (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe
&& (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe )
;
}
v16 = epd_GetCurrentTimeTick();
++cnt;
v17 = v16;
v2 = (unsigned __int16)EPD_Type_Index;
Currentframe = v17 + (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8] - v1;
}
while ( Currentframe < (unsigned int)StageTime );
do
result = (unsigned int)epd_GetCurrentTimeTick() - v1;
while ( (unsigned int)result < StageTime );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // ebp
__int64 v2; // rax
__int64 v3; // r12
char v4; // bl
unsigned __int16 i; // r13
__int64 v6; // r11
unsigned __int16 v7; // di
_BYTE *v8; // r10
_BYTE *v9; // r9
__int64 v10; // rcx
char v11; // dl
unsigned __int16 v12; // si
char v13; // al
unsigned int v14; // eax
unsigned __int16 v15; // r14
int v16; // eax
int v17; // er8
__int64 result; // rax

Currentframe = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8];
DisplayOrgPtr = a1;
cnt = 0;
v1 = (unsigned __int16)epd_GetCurrentTimeTick();
v2 = (unsigned __int16)EPD_Type_Index;
do
{
v3 = DisplayOrgPtr;
if ( COG_Parameters[10 * v2 + 6] )
{
v4 = -64;
for ( i = 0; ; v4 = ScanTable[i & 3] )
{
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * v2 + 4]));
v6 = (unsigned __int16)EPD_Type_Index;
v7 = COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 5];
if ( v7 )
{
v8 = DataLineOdd;
v9 = DataLineEven;
v10 = 0LL;
do
{
v11 = *(_BYTE *)(v3 + v10);
v12 = v7 - 1 - v10;
v13 = (v11 >> 7) & 0xC0;
if ( (v11 & 0x20) != 0 )
v13 = (*(char *)(v3 + v10) >> 7) & 0xC0 | 0x30;
if ( (v11 & 8) != 0 )
v13 |= 0xCu;
if ( (v11 & 2) != 0 )
v13 |= 3u;
v8[v10] = v13;
v14 = -(v11 & 1) & 0xFFFFFFC0;
if ( (v11 & 4) != 0 )
v14 = -(v11 & 1) & 0xFFFFFFC0 | 0x30;
if ( (v11 & 0x10) != 0 )
LOBYTE(v14) = v14 | 0xC;
if ( (v11 & 0x40) != 0 )
LOBYTE(v14) = v14 | 3;
++v10;
v9[v12] = v14;
}
while ( v7 > (unsigned __int16)v10 );
v3 += v7;
}
v15 = i++ >> 2;
*(_BYTE *)(DataLineScan + v15) = v4;
epd_SPI_Send(10LL, &COG_Line, LOBYTE(COG_Parameters[10 * v6 + 7]));
epd_SPI_Send_Byte(2LL, 47LL);
*(_BYTE *)(DataLineScan + v15) = 0;
v2 = (unsigned __int16)EPD_Type_Index;
if ( (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 6] <= i )
break;
}
}
if ( COG_Parameters[10 * v2 + 8] )
{
while ( (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe
&& (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe )
;
}
v16 = epd_GetCurrentTimeTick();
++cnt;
v17 = v16;
v2 = (unsigned __int16)EPD_Type_Index;
Currentframe = v17 + (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8] - v1;
}
while ( Currentframe < (unsigned int)StageTime );
do
result = (unsigned int)epd_GetCurrentTimeTick() - v1;
while ( (unsigned int)result < StageTime );
return result;
}

original_code:
static void Display_Stage_3 (uint8_t *Picture)
{
 uint16_t i, j,k;
 uint8_t TempByte;
 uint16_t StartClock;
 DisplayOrgPtr = Picture;
 Currentframe = (uint32_t)COG_Parameters[EPD_Type_Index].FrameTime;
 cnt = 0;
 StartClock = epd_GetCurrentTimeTick();
 do
 {
     Picture = DisplayOrgPtr;
  for (i = 0; i < COG_Parameters[EPD_Type_Index].VERTICAL; i++)
  {
   epd_SPI_Send_Byte(0x04, COG_Parameters[EPD_Type_Index].VoltageLevel);
   k = COG_Parameters[EPD_Type_Index].HORIZONTAL-1;
   for (j = 0; j < COG_Parameters[EPD_Type_Index].HORIZONTAL; j++)
   {
    TempByte = (*Picture++);
    DataLineOdd[j] = ((TempByte & 0x80) ? (0xc0) : (0x00))
         | ((TempByte & 0x20) ? (0x30) : (0x00) )
         | ((TempByte & 0x08) ? (0x0C) : (0x00) )
         | ((TempByte & 0x02) ? (0x03) : (0x00) );
    DataLineEven[k--] = ((TempByte & 0x01) ? (0xc0) : (0x00))
         | ((TempByte & 0x04) ? (0x30) : (0x00) )
         | ((TempByte & 0x10) ? (0x0C) : (0x00) )
         | ((TempByte & 0x40) ? (0x03) : (0x00) );
   }
   DataLineScan[(i>>2)] = ScanTable[(i%4)];
   epd_SPI_Send(0x0a, (uint8_t *)&COG_Line.uint8, COG_Parameters[EPD_Type_Index].DataLineSize);
   epd_SPI_Send_Byte(0x02, 0x2F);
   DataLineScan[(i>>2)] = 0;
  }
  if(COG_Parameters[EPD_Type_Index].FrameTime>0)
  {
   while(Currentframe>(epd_GetCurrentTimeTick() - StartClock));
  }
  Currentframe=(uint32_t)(epd_GetCurrentTimeTick() - StartClock)+COG_Parameters[EPD_Type_Index].FrameTime ;
  cnt++;
 }while (StageTime>Currentframe);
 while(StageTime>(epd_GetCurrentTimeTick() - StartClock));
}

canonical_original_code:
static void Display_Stage_3(unsigned char *Picture)
{
 unsigned short i; unsigned short j; unsigned short k;
 unsigned char TempByte;
 unsigned short StartClock;
 DisplayOrgPtr = Picture;
 Currentframe = (unsigned int)COG_Parameters[EPD_Type_Index].FrameTime;
 cnt = 0;
 StartClock = epd_GetCurrentTimeTick();
 do
 {
     Picture = DisplayOrgPtr;
  for (i = 0; i < COG_Parameters[EPD_Type_Index].VERTICAL; i++)
  {
   epd_SPI_Send_Byte(0x04, COG_Parameters[EPD_Type_Index].VoltageLevel);
   k = COG_Parameters[EPD_Type_Index].HORIZONTAL-1;
   for (j = 0; j < COG_Parameters[EPD_Type_Index].HORIZONTAL; j++)
   {
    TempByte = (*Picture++);
    DataLineOdd[j] = ((TempByte & 0x80) ? (0xc0) : (0x00))
         | ((TempByte & 0x20) ? (0x30) : (0x00) )
         | ((TempByte & 0x08) ? (0x0C) : (0x00) )
         | ((TempByte & 0x02) ? (0x03) : (0x00) );
    DataLineEven[k--] = ((TempByte & 0x01) ? (0xc0) : (0x00))
         | ((TempByte & 0x04) ? (0x30) : (0x00) )
         | ((TempByte & 0x10) ? (0x0C) : (0x00) )
         | ((TempByte & 0x40) ? (0x03) : (0x00) );
   }
   DataLineScan[(i>>2)] = ScanTable[(i%4)];
   epd_SPI_Send(0x0a, (unsigned char *)&COG_Line.uint8, COG_Parameters[EPD_Type_Index].DataLineSize);
   epd_SPI_Send_Byte(0x02, 0x2F);
   DataLineScan[(i>>2)] = 0;
  }
  if(COG_Parameters[EPD_Type_Index].FrameTime>0)
  {
   while(Currentframe>(epd_GetCurrentTimeTick() - StartClock));
  }
  Currentframe=(unsigned int)(epd_GetCurrentTimeTick() - StartClock)+COG_Parameters[EPD_Type_Index].FrameTime ;
  cnt++;
 }while (StageTime>Currentframe);
 while(StageTime>(epd_GetCurrentTimeTick() - StartClock));
}

variable_types:
Picture
unsigned char *

variable_types:
i
unsigned short

variable_types:
j
unsigned short

variable_types:
k
unsigned short

variable_types:
TempByte
unsigned char

variable_types:
StartClock
unsigned short

return_type: void


decompiled_code:
__int64 __fastcall Display_Stage_4(__int64 a1)
{
int v1; // ebp
__int64 v2; // rax
__int64 v3; // r12
char v4; // bl
unsigned __int16 i; // r13
__int64 v6; // r11
unsigned __int16 v7; // di
_BYTE *v8; // r10
_BYTE *v9; // r9
__int64 v10; // rcx
char v11; // dl
__int64 v12; // rsi
unsigned __int16 v13; // r14
int v14; // eax
int v15; // er8
__int64 result; // rax

Currentframe = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8];
DisplayOrgPtr = a1;
cnt = 0;
v1 = (unsigned __int16)epd_GetCurrentTimeTick();
v2 = (unsigned __int16)EPD_Type_Index;
do
{
v3 = DisplayOrgPtr;
if ( COG_Parameters[10 * v2 + 6] )
{
v4 = -64;
for ( i = 0; ; v4 = ScanTable[i & 3] )
{
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * v2 + 4]));
v6 = (unsigned __int16)EPD_Type_Index;
v7 = COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 5];
if ( v7 )
{
v8 = DataLineOdd;
v9 = DataLineEven;
v10 = 0LL;
do
{
v11 = *(_BYTE *)(v3 + v10);
v12 = (unsigned __int16)(v7 - 1 - v10);
v8[v10++] = (((v11 & 2) == 0) + 2) | ((v11 & 8) == 0 ? 12 : 8) | ((v11 & 0x20) == 0 ? 48 : 32) | (((v11 >> 7) & 0xC0) - 64);
v9[v12] = (((v11 & 0x40) == 0) + 2) | ((v11 & 0x10) == 0 ? 12 : 8) | ((v11 & 4) == 0 ? 48 : 32) | ((v11 & 1) == 0 ? -64 : 0x80);
}
while ( v7 > (unsigned __int16)v10 );
v3 += v7;
}
v13 = i++ >> 2;
*(_BYTE *)(DataLineScan + v13) = v4;
epd_SPI_Send(10LL, &COG_Line, LOBYTE(COG_Parameters[10 * v6 + 7]));
epd_SPI_Send_Byte(2LL, 47LL);
*(_BYTE *)(DataLineScan + v13) = 0;
v2 = (unsigned __int16)EPD_Type_Index;
if ( (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 6] <= i )
break;
}
}
if ( COG_Parameters[10 * v2 + 8] )
{
while ( (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe
&& (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe )
;
}
v14 = epd_GetCurrentTimeTick();
++cnt;
v15 = v14;
v2 = (unsigned __int16)EPD_Type_Index;
Currentframe = v15 + (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8] - v1;
}
while ( Currentframe < (unsigned int)StageTime );
do
result = (unsigned int)epd_GetCurrentTimeTick() - v1;
while ( (unsigned int)result < StageTime );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
int v1; // ebp
__int64 v2; // rax
__int64 v3; // r12
char v4; // bl
unsigned __int16 i; // r13
__int64 v6; // r11
unsigned __int16 v7; // di
_BYTE *v8; // r10
_BYTE *v9; // r9
__int64 v10; // rcx
char v11; // dl
__int64 v12; // rsi
unsigned __int16 v13; // r14
int v14; // eax
int v15; // er8
__int64 result; // rax

Currentframe = (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8];
DisplayOrgPtr = a1;
cnt = 0;
v1 = (unsigned __int16)epd_GetCurrentTimeTick();
v2 = (unsigned __int16)EPD_Type_Index;
do
{
v3 = DisplayOrgPtr;
if ( COG_Parameters[10 * v2 + 6] )
{
v4 = -64;
for ( i = 0; ; v4 = ScanTable[i & 3] )
{
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * v2 + 4]));
v6 = (unsigned __int16)EPD_Type_Index;
v7 = COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 5];
if ( v7 )
{
v8 = DataLineOdd;
v9 = DataLineEven;
v10 = 0LL;
do
{
v11 = *(_BYTE *)(v3 + v10);
v12 = (unsigned __int16)(v7 - 1 - v10);
v8[v10++] = (((v11 & 2) == 0) + 2) | ((v11 & 8) == 0 ? 12 : 8) | ((v11 & 0x20) == 0 ? 48 : 32) | (((v11 >> 7) & 0xC0) - 64);
v9[v12] = (((v11 & 0x40) == 0) + 2) | ((v11 & 0x10) == 0 ? 12 : 8) | ((v11 & 4) == 0 ? 48 : 32) | ((v11 & 1) == 0 ? -64 : 0x80);
}
while ( v7 > (unsigned __int16)v10 );
v3 += v7;
}
v13 = i++ >> 2;
*(_BYTE *)(DataLineScan + v13) = v4;
epd_SPI_Send(10LL, &COG_Line, LOBYTE(COG_Parameters[10 * v6 + 7]));
epd_SPI_Send_Byte(2LL, 47LL);
*(_BYTE *)(DataLineScan + v13) = 0;
v2 = (unsigned __int16)EPD_Type_Index;
if ( (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 6] <= i )
break;
}
}
if ( COG_Parameters[10 * v2 + 8] )
{
while ( (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe
&& (unsigned int)epd_GetCurrentTimeTick() - v1 < Currentframe )
;
}
v14 = epd_GetCurrentTimeTick();
++cnt;
v15 = v14;
v2 = (unsigned __int16)EPD_Type_Index;
Currentframe = v15 + (unsigned __int16)COG_Parameters[10 * (unsigned __int16)EPD_Type_Index + 8] - v1;
}
while ( Currentframe < (unsigned int)StageTime );
do
result = (unsigned int)epd_GetCurrentTimeTick() - v1;
while ( (unsigned int)result < StageTime );
return result;
}

original_code:
static void Display_Stage_4 (uint8_t *Picture)
{
 uint16_t i, j,k;
 uint8_t TempByte;
 uint16_t StartClock;
 DisplayOrgPtr = Picture;
 Currentframe = (uint32_t)COG_Parameters[EPD_Type_Index].FrameTime;
 cnt = 0;
 StartClock = epd_GetCurrentTimeTick();
 do
 {
     Picture = DisplayOrgPtr;
  for (i = 0; i < COG_Parameters[EPD_Type_Index].VERTICAL; i++)
  {
   epd_SPI_Send_Byte(0x04, COG_Parameters[EPD_Type_Index].VoltageLevel);
      k = COG_Parameters[EPD_Type_Index].HORIZONTAL-1;
   for (j = 0; j < COG_Parameters[EPD_Type_Index].HORIZONTAL; j++)
   {
       TempByte =(*Picture++);
     DataLineOdd[j] = ((TempByte & 0x80) ? (0x80) : (0xc0) )
            | ((TempByte & 0x20) ? (0x20) : (0x30) )
            | ((TempByte & 0x08) ? (0x08) : (0x0C) )
            | ((TempByte & 0x02) ? (0x02) : (0x03) );
     DataLineEven[k--] = ((TempByte & 0x01) ? (0x80) : (0xc0) )
            | ((TempByte & 0x04) ? (0x20) : (0x30) )
            | ((TempByte & 0x10) ? (0x08) : (0x0C) )
            | ((TempByte & 0x40) ? (0x02) : (0x03) );
   }
   DataLineScan[(i>>2)] = ScanTable[(i%4)];
   epd_SPI_Send(0x0a, (uint8_t *)&COG_Line.uint8, COG_Parameters[EPD_Type_Index].DataLineSize);
   epd_SPI_Send_Byte(0x02, 0x2F);
      DataLineScan[(i>>2)] = 0;
  }
  if(COG_Parameters[EPD_Type_Index].FrameTime>0)
  {
   while(Currentframe>(epd_GetCurrentTimeTick() - StartClock));
  }
  Currentframe=(uint32_t)(epd_GetCurrentTimeTick() - StartClock)+COG_Parameters[EPD_Type_Index].FrameTime ;
  cnt++;
 }while (StageTime>Currentframe);
 while(StageTime>(epd_GetCurrentTimeTick() - StartClock));
}

canonical_original_code:
static void Display_Stage_4(unsigned char *Picture)
{
 unsigned short i; unsigned short j; unsigned short k;
 unsigned char TempByte;
 unsigned short StartClock;
 DisplayOrgPtr = Picture;
 Currentframe = (unsigned int)COG_Parameters[EPD_Type_Index].FrameTime;
 cnt = 0;
 StartClock = epd_GetCurrentTimeTick();
 do
 {
     Picture = DisplayOrgPtr;
  for (i = 0; i < COG_Parameters[EPD_Type_Index].VERTICAL; i++)
  {
   epd_SPI_Send_Byte(0x04, COG_Parameters[EPD_Type_Index].VoltageLevel);
      k = COG_Parameters[EPD_Type_Index].HORIZONTAL-1;
   for (j = 0; j < COG_Parameters[EPD_Type_Index].HORIZONTAL; j++)
   {
       TempByte =(*Picture++);
     DataLineOdd[j] = ((TempByte & 0x80) ? (0x80) : (0xc0) )
            | ((TempByte & 0x20) ? (0x20) : (0x30) )
            | ((TempByte & 0x08) ? (0x08) : (0x0C) )
            | ((TempByte & 0x02) ? (0x02) : (0x03) );
     DataLineEven[k--] = ((TempByte & 0x01) ? (0x80) : (0xc0) )
            | ((TempByte & 0x04) ? (0x20) : (0x30) )
            | ((TempByte & 0x10) ? (0x08) : (0x0C) )
            | ((TempByte & 0x40) ? (0x02) : (0x03) );
   }
   DataLineScan[(i>>2)] = ScanTable[(i%4)];
   epd_SPI_Send(0x0a, (unsigned char *)&COG_Line.uint8, COG_Parameters[EPD_Type_Index].DataLineSize);
   epd_SPI_Send_Byte(0x02, 0x2F);
      DataLineScan[(i>>2)] = 0;
  }
  if(COG_Parameters[EPD_Type_Index].FrameTime>0)
  {
   while(Currentframe>(epd_GetCurrentTimeTick() - StartClock));
  }
  Currentframe=(unsigned int)(epd_GetCurrentTimeTick() - StartClock)+COG_Parameters[EPD_Type_Index].FrameTime ;
  cnt++;
 }while (StageTime>Currentframe);
 while(StageTime>(epd_GetCurrentTimeTick() - StartClock));
}


decompiled_code:
// attributes: thunk
__int64 epd_HwInit()
{
return epd_InitDisplayHardware();
}

canonical_decompiled_code:
__int64 func8()
{
return epd_InitDisplayHardware();
}

original_code:
void epd_HwInit(void)
{
 epd_InitDisplayHardware();
}

canonical_original_code:
void epd_HwInit()
{
 epd_InitDisplayHardware();
}


decompiled_code:
__int64 epd_PowerOn()
{
epd_discharge_low();
epd_rst_low();
epd_cs_low();
epd_spi_init();
epd_pwm_active(5LL);
epd_panelon_on();
epd_pwm_active(10LL);
epd_cs_high();
epd_rst_high();
epd_pwm_active(5LL);
epd_rst_low();
epd_pwm_active(5LL);
epd_rst_high();
return epd_pwm_active(5LL);
}

canonical_decompiled_code:
__int64 func9()
{
epd_discharge_low();
epd_rst_low();
epd_cs_low();
epd_spi_init();
epd_pwm_active(5LL);
epd_panelon_on();
epd_pwm_active(10LL);
epd_cs_high();
epd_rst_high();
epd_pwm_active(5LL);
epd_rst_low();
epd_pwm_active(5LL);
epd_rst_high();
return epd_pwm_active(5LL);
}

original_code:
void epd_PowerOn(void)
{
 epd_discharge_low();
 epd_rst_low();
 epd_cs_low();
 epd_spi_init();
 epd_pwm_active(5);
 epd_panelon_on();
 epd_pwm_active(10);
 epd_cs_high();
 epd_rst_high();
 epd_pwm_active(5);
 epd_rst_low();
 epd_pwm_active(5);
 epd_rst_high();
 epd_pwm_active(5);
}

canonical_original_code:
void epd_PowerOn()
{
 epd_discharge_low();
 epd_rst_low();
 epd_cs_low();
 epd_spi_init();
 epd_pwm_active(5);
 epd_panelon_on();
 epd_pwm_active(10);
 epd_cs_high();
 epd_rst_high();
 epd_pwm_active(5);
 epd_rst_low();
 epd_pwm_active(5);
 epd_rst_high();
 epd_pwm_active(5);
}


decompiled_code:
__int64 __fastcall epd_InitializeDriver(unsigned __int8 a1)
{
__int16 v2[13]; // [rsp+Eh] [rbp-1Ah] BYREF

EPD_Type_Index = a1;
memset(&COG_Line, 0, 0x70uLL);
Driver_TypeSelect(a1);
SetTemperature_Factor(a1);
epd_SPI_Send(1LL, &COG_Parameters[10 * a1], 8LL);
epd_SPI_Send_Byte(6LL, 255LL);
epd_SPI_Send_Byte(7LL, 157LL);
epd_SPI_Send_Byte(8LL, 0LL);
v2[0] = 208;
epd_SPI_Send(9LL, v2, 2LL);
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * a1 + 4]));
epd_SPI_Send_Byte(3LL, 1LL);
epd_SPI_Send_Byte(3LL, 0LL);
epd_pwm_active(5LL);
epd_SPI_Send_Byte(5LL, 1LL);
epd_pwm_active(30LL);
epd_SPI_Send_Byte(5LL, 3LL);
epd_delay_ms(30LL);
epd_SPI_Send_Byte(5LL, 15LL);
epd_delay_ms(30LL);
return epd_SPI_Send_Byte(2LL, 36LL);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
__int16 v2[13]; // [rsp+Eh] [rbp-1Ah] BYREF

EPD_Type_Index = a1;
memset(&COG_Line, 0, 0x70uLL);
func0(a1);
func3(a1);
epd_SPI_Send(1LL, &COG_Parameters[10 * a1], 8LL);
epd_SPI_Send_Byte(6LL, 255LL);
epd_SPI_Send_Byte(7LL, 157LL);
epd_SPI_Send_Byte(8LL, 0LL);
v2[0] = 208;
epd_SPI_Send(9LL, v2, 2LL);
epd_SPI_Send_Byte(4LL, LOBYTE(COG_Parameters[10 * a1 + 4]));
epd_SPI_Send_Byte(3LL, 1LL);
epd_SPI_Send_Byte(3LL, 0LL);
epd_pwm_active(5LL);
epd_SPI_Send_Byte(5LL, 1LL);
epd_pwm_active(30LL);
epd_SPI_Send_Byte(5LL, 3LL);
epd_delay_ms(30LL);
epd_SPI_Send_Byte(5LL, 15LL);
epd_delay_ms(30LL);
return epd_SPI_Send_Byte(2LL, 36LL);
}

original_code:
void epd_InitializeDriver(uint8_t EPDIndex)
{
 uint8_t SendBuffer[2];
 uint16_t k;
 EPD_Type_Index = EPDIndex;
 for (k = 0; k <= 111; k ++)
 {
  COG_Line.uint8[k] = 0x00;
 }
 Driver_TypeSelect(EPDIndex);
 k = 0;
 SetTemperature_Factor(EPDIndex);
 epd_SPI_Send(0x01, (uint8_t *)&COG_Parameters[EPDIndex].ChannelSelect, 8);
 epd_SPI_Send_Byte(0x06, 0xff);
 epd_SPI_Send_Byte(0x07, 0x9d);
 epd_SPI_Send_Byte(0x08, 0x00);
 SendBuffer[0] = 0xd0;
 SendBuffer[1] = 0x00;
 epd_SPI_Send(0x09, SendBuffer, 2);
 epd_SPI_Send_Byte(0x04,COG_Parameters[EPDIndex].VoltageLevel);
 epd_SPI_Send_Byte(0x03, 0x01);
 epd_SPI_Send_Byte(0x03, 0x00);
 epd_pwm_active(5);
 epd_SPI_Send_Byte(0x05, 0x01);
 epd_pwm_active(30);
 epd_SPI_Send_Byte(0x05, 0x03);
 epd_delay_ms(30);
 epd_SPI_Send_Byte(0x05, 0x0f);
 epd_delay_ms(30);
 epd_SPI_Send_Byte(0x02, 0x24);
}

canonical_original_code:
void epd_InitializeDriver(unsigned char EPDIndex)
{
 unsigned char SendBuffer[2];
 unsigned short k;
 EPD_Type_Index = EPDIndex;
 for (k = 0; k <= 111; k ++)
 {
  COG_Line.uint8[k] = 0x00;
 }
 Driver_TypeSelect(EPDIndex);
 k = 0;
 SetTemperature_Factor(EPDIndex);
 epd_SPI_Send(0x01, (unsigned char *)&COG_Parameters[EPDIndex].ChannelSelect, 8);
 epd_SPI_Send_Byte(0x06, 0xff);
 epd_SPI_Send_Byte(0x07, 0x9d);
 epd_SPI_Send_Byte(0x08, 0x00);
 SendBuffer[0] = 0xd0;
 SendBuffer[1] = 0x00;
 epd_SPI_Send(0x09, SendBuffer, 2);
 epd_SPI_Send_Byte(0x04,COG_Parameters[EPDIndex].VoltageLevel);
 epd_SPI_Send_Byte(0x03, 0x01);
 epd_SPI_Send_Byte(0x03, 0x00);
 epd_pwm_active(5);
 epd_SPI_Send_Byte(0x05, 0x01);
 epd_pwm_active(30);
 epd_SPI_Send_Byte(0x05, 0x03);
 epd_delay_ms(30);
 epd_SPI_Send_Byte(0x05, 0x0f);
 epd_delay_ms(30);
 epd_SPI_Send_Byte(0x02, 0x24);
}

variable_types:
EPDIndex
unsigned char

variable_types:
SendBuffer
unsigned char [2]


decompiled_code:
__int64 __fastcall epd_Display(__int64 a1, __int64 a2)
{
Display_Stage_1(a2);
Display_Stage_2(a2);
Display_Stage_3(a1);
return Display_Stage_4(a1);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
func4(a2);
func5(a2);
func6(a1);
return func7(a1);
}

original_code:
void epd_Display (uint8_t *pNewImg, uint8_t *pPrevImg)
{
 Display_Stage_1(pPrevImg);
 Display_Stage_2(pPrevImg);
 Display_Stage_3(pNewImg);
 Display_Stage_4(pNewImg);
}

canonical_original_code:
void epd_Display(unsigned char *pNewImg, unsigned char *pPrevImg)
{
 Display_Stage_1(pPrevImg);
 Display_Stage_2(pPrevImg);
 Display_Stage_3(pNewImg);
 Display_Stage_4(pNewImg);
}

variable_types:
pNewImg
unsigned char *

variable_types:
pPrevImg
unsigned char *


decompiled_code:
__int64 epd_PowerOff()
{
Display_Nothing();
Dummy_line();
epd_delay_ms(25LL);
epd_SPI_Send_Byte(3LL, 1LL);
epd_SPI_Send_Byte(2LL, 5LL);
epd_SPI_Send_Byte(5LL, 14LL);
epd_SPI_Send_Byte(5LL, 2LL);
epd_SPI_Send_Byte(4LL, 12LL);
epd_delay_ms(120LL);
epd_SPI_Send_Byte(5LL, 0LL);
epd_SPI_Send_Byte(7LL, 13LL);
epd_SPI_Send_Byte(4LL, 80LL);
epd_delay_ms(40LL);
epd_SPI_Send_Byte(4LL, 160LL);
epd_delay_ms(40LL);
epd_SPI_Send_Byte(4LL, 0LL);
epd_rst_low();
epd_cs_low();
epd_spi_detach();
epd_panelon_off();
epd_discharge_high();
epd_delay_ms(150LL);
return epd_discharge_low();
}

canonical_decompiled_code:
__int64 func12()
{
func1();
func2();
epd_delay_ms(25LL);
epd_SPI_Send_Byte(3LL, 1LL);
epd_SPI_Send_Byte(2LL, 5LL);
epd_SPI_Send_Byte(5LL, 14LL);
epd_SPI_Send_Byte(5LL, 2LL);
epd_SPI_Send_Byte(4LL, 12LL);
epd_delay_ms(120LL);
epd_SPI_Send_Byte(5LL, 0LL);
epd_SPI_Send_Byte(7LL, 13LL);
epd_SPI_Send_Byte(4LL, 80LL);
epd_delay_ms(40LL);
epd_SPI_Send_Byte(4LL, 160LL);
epd_delay_ms(40LL);
epd_SPI_Send_Byte(4LL, 0LL);
epd_rst_low();
epd_cs_low();
epd_spi_detach();
epd_panelon_off();
epd_discharge_high();
epd_delay_ms(150LL);
return epd_discharge_low();
}

original_code:
void epd_PowerOff (void)
{
 Display_Nothing();
 Dummy_line();
 epd_delay_ms(25);
 epd_SPI_Send_Byte(0x03, 0x01);
 epd_SPI_Send_Byte(0x02, 0x05);
 epd_SPI_Send_Byte(0x05, 0x0E);
 epd_SPI_Send_Byte(0x05, 0x02);
 epd_SPI_Send_Byte(0x04, 0x0C);
 epd_delay_ms(120);
 epd_SPI_Send_Byte(0x05, 0x00);
 epd_SPI_Send_Byte(0x07, 0x0D);
 epd_SPI_Send_Byte(0x04, 0x50);
 epd_delay_ms(40);
 epd_SPI_Send_Byte(0x04, 0xA0);
 epd_delay_ms(40);
 epd_SPI_Send_Byte(0x04, 0x00);
 epd_rst_low();
 epd_cs_low();
 epd_spi_detach();
 epd_panelon_off();
 epd_discharge_high();
 epd_delay_ms(150);
    epd_discharge_low();
}

canonical_original_code:
void epd_PowerOff()
{
 Display_Nothing();
 Dummy_line();
 epd_delay_ms(25);
 epd_SPI_Send_Byte(0x03, 0x01);
 epd_SPI_Send_Byte(0x02, 0x05);
 epd_SPI_Send_Byte(0x05, 0x0E);
 epd_SPI_Send_Byte(0x05, 0x02);
 epd_SPI_Send_Byte(0x04, 0x0C);
 epd_delay_ms(120);
 epd_SPI_Send_Byte(0x05, 0x00);
 epd_SPI_Send_Byte(0x07, 0x0D);
 epd_SPI_Send_Byte(0x04, 0x50);
 epd_delay_ms(40);
 epd_SPI_Send_Byte(0x04, 0xA0);
 epd_delay_ms(40);
 epd_SPI_Send_Byte(0x04, 0x00);
 epd_rst_low();
 epd_cs_low();
 epd_spi_detach();
 epd_panelon_off();
 epd_discharge_high();
 epd_delay_ms(150);
    epd_discharge_low();
}


atoi

decompiled_code:
__int64 __fastcall atoi(_BYTE *a1)
{
int v1; // eax
unsigned int i; // er12

v1 = (char)*a1;
for ( i = 0; *a1; v1 = (char)*a1 )
{
++a1;
i = v1 + 10 * i - 48;
}
printf("%d\n", i);
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
int v1; // eax
unsigned int i; // er12

v1 = (char)*a1;
for ( i = 0; *a1; v1 = (char)*a1 )
{
++a1;
i = v1 + 10 * i - 48;
}
printf("%d\n", i);
return i;
}

original_code:
int atoi(char *p) {
 int k = 0;
 while (*p) {
  k = (k*10)+(*p)-'0';
  p++;
 }
 printf("%d\n", k);
 return k;
}

variable_types:
p
char *

variable_types: k int

return_type: int


dot

decompiled_code:
__int64 __fastcall dot(unsigned __int8 a1, char a2)
{
__int64 result; // rax

result = 32LL;
if ( (a1 & (1 << a2)) >> a2 <= 0 )
result = 49LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, char a2)
{
__int64 result; // rax

result = 32LL;
if ( (a1 & (1 << a2)) >> a2 <= 0 )
result = 49LL;
return result;
}

original_code:
char dot(uint8_t byte, int bit) {
 return ((((1<<bit) & byte) >> bit) > 0) ? ' ' : '1';
}

canonical_original_code:
char dot(unsigned char byte, int bit) {
 return ((((1<<bit) & byte) >> bit) > 0) ? ' ' : '1';
}

variable_types:
byte
unsigned char

variable_types: bit int

return_type: char


tod

decompiled_code:
__int64 __fastcall tod(char a1, char a2)
{
return (unsigned __int8)(a1 == 32) << a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, char a2)
{
return (unsigned __int8)(a1 == 32) << a2;
}

original_code:
uint8_t tod(char c, int bit) {
 return (uint8_t)((c == ' ' ? 1 : 0) << bit);
}

canonical_original_code:
unsigned char tod(char c, int bit) {
 return (unsigned char)((c == ' ' ? 1 : 0) << bit);
}

variable_types: c char

return_type: unsigned char


decompiled_code:
__int64 __fastcall asciiImage_printImage(unsigned __int8 *a1, int a2, int a3)
{
int v4; // ebx
__int64 result; // rax
unsigned __int8 *v6; // r12
unsigned __int8 v7; // r13
__int64 v8; // r14
__int64 v9; // rbp
__int64 v10; // rbx
char v11; // al
char v12; // r11
char v13; // r10
__int64 v14; // r8
__int64 v15; // r9
int v16; // [rsp+Ch] [rbp-4Ch]
__int64 v18; // [rsp+18h] [rbp-40h]

v4 = a3;
printf("%d,%d,~\n", (unsigned int)a3, (unsigned int)a2);
result = v4;
v16 = 0;
v18 = v4;
if ( a2 > 0 )
{
do
{
v6 = &a1[v18];
if ( a3 > 0 )
{
do
{
v7 = *a1++;
v8 = (unsigned int)(char)dot(v7, 0);
v9 = (unsigned int)(char)dot(v7, 1);
v10 = (unsigned int)(char)dot(v7, 2);
dot(v7, 3);
dot(v7, 4);
dot(v7, 5);
dot(v7, 6);
v11 = dot(v7, 7);
printf("%c%c%c%c%c%c%c%c", (unsigned int)v11, (unsigned int)v13, (unsigned int)v12, v14, v15, v10, v9, v8);
}
while ( a1 != v6 );
}
puts("~");
result = (unsigned int)++v16;
}
while ( a2 != v16 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 *a1, int a2, int a3)
{
int v4; // ebx
__int64 result; // rax
unsigned __int8 *v6; // r12
unsigned __int8 v7; // r13
__int64 v8; // r14
__int64 v9; // rbp
__int64 v10; // rbx
char v11; // al
char v12; // r11
char v13; // r10
__int64 v14; // r8
__int64 v15; // r9
int v16; // [rsp+Ch] [rbp-4Ch]
__int64 v18; // [rsp+18h] [rbp-40h]

v4 = a3;
printf("%d,%d,~\n", (unsigned int)a3, (unsigned int)a2);
result = v4;
v16 = 0;
v18 = v4;
if ( a2 > 0 )
{
do
{
v6 = &a1[v18];
if ( a3 > 0 )
{
do
{
v7 = *a1++;
v8 = (unsigned int)(char)func1(v7, 0);
v9 = (unsigned int)(char)func1(v7, 1);
v10 = (unsigned int)(char)func1(v7, 2);
func1(v7, 3);
func1(v7, 4);
func1(v7, 5);
func1(v7, 6);
v11 = func1(v7, 7);
printf("%c%c%c%c%c%c%c%c", (unsigned int)v11, (unsigned int)v13, (unsigned int)v12, v14, v15, v10, v9, v8);
}
while ( a1 != v6 );
}
puts("~");
result = (unsigned int)++v16;
}
while ( a2 != v16 );
}
return result;
}

original_code:
void asciiImage_printImage(uint8_t * image, int h, int w) {
 int i, j;
 printf("%d,%d,~\n", w, h);
 for (i = 0; i < h; i++) {
  for (j = 0; j < w; j++) {
   printf("%c%c%c%c%c%c%c%c",
    dot(*image, 7),
    dot(*image, 6),
    dot(*image, 5),
    dot(*image, 4),
    dot(*image, 3),
    dot(*image, 2),
    dot(*image, 1),
    dot(*image, 0));
   image++;
  }
  printf("~\n");
 }
}

canonical_original_code:
void asciiImage_printImage(unsigned char *image, int h, int w) {
 int i; int j;
 printf("%d,%d,~\n", w, h);
 for (i = 0; i < h; i++) {
  for (j = 0; j < w; j++) {
   printf("%c%c%c%c%c%c%c%c",
    dot(*image, 7),
    dot(*image, 6),
    dot(*image, 5),
    dot(*image, 4),
    dot(*image, 3),
    dot(*image, 2),
    dot(*image, 1),
    dot(*image, 0));
   image++;
  }
  printf("~\n");
 }
}

variable_types:
image
unsigned char *

variable_types: h int

variable_types: w int

variable_types: i int

variable_types: j int


main

canonical_name: main

repo: deoxxa/jsm-c

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // ebx
void *v5; // rax
__off_t v6; // rdx
__int64 v7; // rbp
int v8; // eax
int v9; // er12
__off_t v11; // [rsp+8h] [rbp-100h]
char v12[44]; // [rsp+10h] [rbp-F8h] BYREF
int v13; // [rsp+3Ch] [rbp-CCh]
__int64 v14; // [rsp+40h] [rbp-C8h]
struct stat stat_buf; // [rsp+50h] [rbp-B8h] BYREF

if ( argc <= 1 )
{
v9 = -1;
fprintf(_bss_start, "Usage: %s <program.bin>\n", *argv);
}
else
{
fprintf(_bss_start, "> loading program image from `%s'\n", argv[1]);
v4 = open(argv[1], 0);
fstat(v4, &stat_buf);
fprintf(_bss_start, "> program image is %lld bytes\n", stat_buf.st_size);
v11 = stat_buf.st_size;
v5 = malloc(stat_buf.st_size);
v6 = v11;
v7 = (__int64)v5;
while ( 1 )
{
v8 = read(v4, (void *)(v7 + v3), v6 - v3);
v9 = v8;
if ( v8 == -1 )
break;
v3 += v8;
if ( !v8 )
{
fwrite("> read program\n", 1uLL, 0xFuLL, _bss_start);
cpu_init(v12);
v13 = v3;
v14 = v7;
cpu_run(v12);
return v9;
}
v6 = stat_buf.st_size;
}
}
return v9;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // ebx
void *v5; // rax
__off_t v6; // rdx
__int64 v7; // rbp
int v8; // eax
int v9; // er12
__off_t v11; // [rsp+8h] [rbp-100h]
char v12[44]; // [rsp+10h] [rbp-F8h] BYREF
int v13; // [rsp+3Ch] [rbp-CCh]
__int64 v14; // [rsp+40h] [rbp-C8h]
struct stat stat_buf; // [rsp+50h] [rbp-B8h] BYREF

if ( argc <= 1 )
{
v9 = -1;
fprintf(_bss_start, "Usage: %s <program.bin>\n", *argv);
}
else
{
fprintf(_bss_start, "> loading program image from `%s'\n", argv[1]);
v4 = open(argv[1], 0);
func13(v4, &stat_buf);
fprintf(_bss_start, "> program image is %lld bytes\n", stat_buf.st_size);
v11 = stat_buf.st_size;
v5 = malloc(stat_buf.st_size);
v6 = v11;
v7 = (__int64)v5;
while ( 1 )
{
v8 = read(v4, (void *)(v7 + v3), v6 - v3);
v9 = v8;
if ( v8 == -1 )
break;
v3 += v8;
if ( !v8 )
{
fwrite("> read program\n", 1uLL, 0xFuLL, _bss_start);
func8(v12);
v13 = v3;
v14 = v7;
func10(v12);
return v9;
}
v6 = stat_buf.st_size;
}
}
return v9;
}

original_code:
int main(int argc, char** argv) {
  cpu_t cpu;
  struct stat fileinfo;
  unsigned char* image = ((void *)0);
  int fd, rc, nread;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <program.bin>\n", argv[0]);
    return -1;
  }
  fprintf(stderr, "> loading program image from `%s'\n", argv[1]);
  fd = open(argv[1], 00);
  rc = fstat(fd, &fileinfo);
  fprintf(stderr, "> program image is %lld bytes\n", fileinfo.st_size);
  image = malloc(fileinfo.st_size);
  do {
    rc = read(fd, image + nread, fileinfo.st_size - nread);
    if (rc == -1) {
      return -1;
    }
    nread += rc;
  } while (rc != 0);
  fprintf(stderr, "> read program\n");
  cpu_init(&cpu);
  cpu.image_size = nread;
  cpu.image = image;
  cpu_run(&cpu);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  struct cpu_s cpu;
  struct stat fileinfo;
  unsigned char *image = ((void *)0);
  int fd; int rc; int nread;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <program.bin>\n", argv[0]);
    return -1;
  }
  fprintf(stderr, "> loading program image from `%s'\n", argv[1]);
  fd = open(argv[1], 00);
  rc = fstat(fd, &fileinfo);
  fprintf(stderr, "> program image is %lld bytes\n", fileinfo.st_size);
  image = malloc(fileinfo.st_size);
  do {
    rc = read(fd, image + nread, fileinfo.st_size - nread);
    if (rc == -1) {
      return -1;
    }
    nread += rc;
  } while (rc != 0);
  fprintf(stderr, "> read program\n");
  cpu_init(&cpu);
  cpu.image_size = nread;
  cpu.image = image;
  cpu_run(&cpu);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct cpu_s { unsigned int ticks; unsigned int eip; unsigned char running; unsigned int registers[8]; unsigned int image_size; unsigned char *image; }

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: fd int

variable_types: rc int

variable_types: nread int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


canonical_original_code:
unsigned int read_uint32(unsigned char *ptr) {
  return ptr[0] * 16777216 + ptr[1] * 65536 + ptr[2] * 256 + ptr[3];
}

variable_types:
ptr
unsigned char *

return_type: unsigned int


original_code:
void cpu_init(cpu_t* cpu) {
  cpu->ticks = 0;
  cpu->eip = 0;
  cpu->running = 1;
  for (int i=0;i<8;++i) {
    cpu->registers[i] = 0;
  }
  cpu->image_size = 0;
  cpu->image = ((void *)0);
}

canonical_original_code:
void cpu_init(struct cpu_s *cpu) {
  cpu->ticks = 0;
  cpu->eip = 0;
  cpu->running = 1;
  for (int i = 0;i<8;++i) {
    cpu->registers[i] = 0;
  }
  cpu->image_size = 0;
  cpu->image = ((void *)0);
}

variable_types: struct cpu_s { unsigned int ticks; unsigned int eip; unsigned char running; unsigned int registers[8]; unsigned int image_size; unsigned char *image; } *


original_code:
int cpu_tick(cpu_t* cpu) {
  printf("[%04d] [0x%04x] 0x%02x\n", cpu->ticks++, cpu->eip, cpu->image[cpu->eip]);
  switch (cpu->image[cpu->eip]) {
    case OPCODE_NOP: {
      cpu->eip += 1;
      break;
    }
    case OPCODE_SET: {
      cpu->registers[cpu->image[cpu->eip + 1]] = read_uint32(cpu->image + cpu->eip + 2);
      cpu->eip += 6;
      break;
    }
    case OPCODE_CPY: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]];
      cpu->eip += 3;
      break;
    }
    case OPCODE_DRB: {
      cpu->registers[cpu->image[cpu->eip + 1]] = *(uint8_t*)(cpu->image + cpu->registers[cpu->image[cpu->eip + 2]]);
      cpu->eip += 3;
      break;
    }
    case OPCODE_DWB: {
      *(uint8_t*)(cpu->image + cpu->registers[cpu->image[cpu->eip + 2]]) = cpu->registers[cpu->image[cpu->eip + 1]];
      cpu->eip += 3;
      break;
    }
    case OPCODE_SRB: {
      cpu->registers[cpu->image[cpu->eip + 1]] = *(uint8_t*)(cpu->image + cpu->image[cpu->eip + 2]);
      cpu->eip += 6;
      break;
    }
    case OPCODE_SWB: {
      *(uint8_t*)(cpu->image + cpu->image[cpu->eip + 2]) = cpu->registers[cpu->image[cpu->eip + 1]];
      cpu->eip += 6;
      break;
    }
    case OPCODE_ADD: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] + cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_MUL: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] * cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_AND: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] & cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_OR: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] | cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_XOR: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] ^ cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_JMP: {
      cpu->eip = read_uint32(cpu->image + cpu->eip + 1);
      break;
    }
    case OPCODE_JNE: {
      if (cpu->registers[cpu->image[cpu->eip + 1]] != cpu->registers[cpu->image[cpu->eip + 2]]) {
        cpu->eip = read_uint32(cpu->image + cpu->eip + 3);
      } else {
        cpu->eip += 7;
      }
      break;
    }
    case OPCODE_END: {
      cpu->running = 0;
      break;
    }
    default: {
      fprintf(stderr, "unknown opcode seen: 0x%.02x\n", cpu->image[cpu->eip]);
      return -1;
    }
  }
  return 0;
}

canonical_original_code:
int cpu_tick(struct cpu_s *cpu) {
  printf("[%04d] [0x%04x] 0x%02x\n", cpu->ticks++, cpu->eip, cpu->image[cpu->eip]);
  switch (cpu->image[cpu->eip]) {
    case OPCODE_NOP: {
      cpu->eip += 1;
      break;
    }
    case OPCODE_SET: {
      cpu->registers[cpu->image[cpu->eip + 1]] = read_uint32(cpu->image + cpu->eip + 2);
      cpu->eip += 6;
      break;
    }
    case OPCODE_CPY: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]];
      cpu->eip += 3;
      break;
    }
    case OPCODE_DRB: {
      cpu->registers[cpu->image[cpu->eip + 1]] = *(unsigned char *)(cpu->image + cpu->registers[cpu->image[cpu->eip + 2]]);
      cpu->eip += 3;
      break;
    }
    case OPCODE_DWB: {
      *(unsigned char *)(cpu->image + cpu->registers[cpu->image[cpu->eip + 2]]) = cpu->registers[cpu->image[cpu->eip + 1]];
      cpu->eip += 3;
      break;
    }
    case OPCODE_SRB: {
      cpu->registers[cpu->image[cpu->eip + 1]] = *(unsigned char *)(cpu->image + cpu->image[cpu->eip + 2]);
      cpu->eip += 6;
      break;
    }
    case OPCODE_SWB: {
      *(unsigned char *)(cpu->image + cpu->image[cpu->eip + 2]) = cpu->registers[cpu->image[cpu->eip + 1]];
      cpu->eip += 6;
      break;
    }
    case OPCODE_ADD: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] + cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_MUL: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] * cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_AND: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] & cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_OR: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] | cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_XOR: {
      cpu->registers[cpu->image[cpu->eip + 1]] = cpu->registers[cpu->image[cpu->eip + 2]] ^ cpu->registers[cpu->image[cpu->eip + 3]];
      cpu->eip += 4;
      break;
    }
    case OPCODE_JMP: {
      cpu->eip = read_uint32(cpu->image + cpu->eip + 1);
      break;
    }
    case OPCODE_JNE: {
      if (cpu->registers[cpu->image[cpu->eip + 1]] != cpu->registers[cpu->image[cpu->eip + 2]]) {
        cpu->eip = read_uint32(cpu->image + cpu->eip + 3);
      } else {
        cpu->eip += 7;
      }
      break;
    }
    case OPCODE_END: {
      cpu->running = 0;
      break;
    }
    default: {
      fprintf(stderr, "unknown opcode seen: 0x%.02x\n", cpu->image[cpu->eip]);
      return -1;
    }
  }
  return 0;
}

user_defined_types: struct cpu_s { unsigned int ticks; unsigned int eip; unsigned char running; unsigned int registers[8]; unsigned int image_size; unsigned char *image; }


decompiled_code:
__int64 __fastcall cpu_run(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // er12
__int64 v4; // rbp
__int64 v5; // rcx
__int64 v6; // rdx

fwrite("> running cpu\n", 1uLL, 0xEuLL, _bss_start);
do
{
v1 = cpu_tick((unsigned int *)a1);
v2 = v1;
if ( v1 == -1 )
return 0xFFFFFFFFLL;
}
while ( *(_BYTE *)(a1 + 8) == 1 );
v4 = 0LL;
fprintf(_bss_start, "> cpu completed running, status is %d\n", v1);
do
{
v5 = *(unsigned int *)(a1 + 4 * v4 + 12);
v6 = (unsigned int)v4++;
fprintf(_bss_start, "> register %02x: 0x%08x\n", v6, v5);
}
while ( v4 != 8 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // er12
__int64 v4; // rbp
__int64 v5; // rcx
__int64 v6; // rdx

fwrite("> running cpu\n", 1uLL, 0xEuLL, _bss_start);
do
{
v1 = func9((unsigned int *)a1);
v2 = v1;
if ( v1 == -1 )
return 0xFFFFFFFFLL;
}
while ( *(_BYTE *)(a1 + 8) == 1 );
v4 = 0LL;
fprintf(_bss_start, "> cpu completed running, status is %d\n", v1);
do
{
v5 = *(unsigned int *)(a1 + 4 * v4 + 12);
v6 = (unsigned int)v4++;
fprintf(_bss_start, "> register %02x: 0x%08x\n", v6, v5);
}
while ( v4 != 8 );
return v2;
}

original_code:
int cpu_run(cpu_t* cpu) {
  int rc, i;
  fprintf(stderr, "> running cpu\n");
  do {
    rc = cpu_tick(cpu);
    if (rc == -1) {
      return -1;
    }
  } while (cpu->running == 1);
  fprintf(stderr, "> cpu completed running, status is %d\n", rc);
  for (i=0;i<8;++i) {
    fprintf(stderr, "> register %02x: 0x%08x\n", i, cpu->registers[i]);
  }
  return rc;
}

canonical_original_code:
int cpu_run(struct cpu_s *cpu) {
  int rc; int i;
  fprintf(stderr, "> running cpu\n");
  do {
    rc = cpu_tick(cpu);
    if (rc == -1) {
      return -1;
    }
  } while (cpu->running == 1);
  fprintf(stderr, "> cpu completed running, status is %d\n", rc);
  for (i=0;i<8;++i) {
    fprintf(stderr, "> register %02x: 0x%08x\n", i, cpu->registers[i]);
  }
  return rc;
}


repo: deoxxa/mqtt-protocol-c

decompiled_code:
int __fastcall mqtt_buffer_dump_ascii(__int64 a1)
{
int result; // eax
__int64 v3; // rbx
int v4; // edi

result = *(_DWORD *)a1;
if ( *(_DWORD *)a1 )
{
v3 = 0LL;
do
{
v4 = *(unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v3++);
result = putchar(v4);
}
while ( *(_DWORD *)a1 > (unsigned int)v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int result; // eax
__int64 v3; // rbx
int v4; // edi

result = *(_DWORD *)a1;
if ( *(_DWORD *)a1 )
{
v3 = 0LL;
do
{
v4 = *(unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v3++);
result = putchar(v4);
}
while ( *(_DWORD *)a1 > (unsigned int)v3 );
}
return result;
}

original_code:
void mqtt_buffer_dump_ascii(mqtt_buffer_t* buffer) {
  for (int i=0;i<buffer->length;++i) {
    printf("%c", buffer->data[i]);
  }
}

canonical_original_code:
void mqtt_buffer_dump_ascii(struct mqtt_buffer_s *buffer) {
  for (int i = 0;i<buffer->length;++i) {
    printf("%c", buffer->data[i]);
  }
}

variable_types: struct mqtt_buffer_s { unsigned int length; unsigned char *data; } *


decompiled_code:
int __fastcall mqtt_buffer_dump_hex(__int64 a1)
{
int result; // eax
__int64 v2; // rbx
__int64 v3; // rsi

result = *(_DWORD *)a1;
if ( *(_DWORD *)a1 )
{
v2 = 0LL;
do
{
v3 = *(unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v2++);
result = printf("%02x ", v3);
}
while ( *(_DWORD *)a1 > (unsigned int)v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
int result; // eax
__int64 v2; // rbx
__int64 v3; // rsi

result = *(_DWORD *)a1;
if ( *(_DWORD *)a1 )
{
v2 = 0LL;
do
{
v3 = *(unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v2++);
result = printf("%02x ", v3);
}
while ( *(_DWORD *)a1 > (unsigned int)v2 );
}
return result;
}

original_code:
void mqtt_buffer_dump_hex(mqtt_buffer_t* buffer) {
  for (int i=0;i<buffer->length;++i) {
    printf("%02x ", buffer->data[i]);
  }
}

canonical_original_code:
void mqtt_buffer_dump_hex(struct mqtt_buffer_s *buffer) {
  for (int i = 0;i<buffer->length;++i) {
    printf("%02x ", buffer->data[i]);
  }
}

user_defined_types: struct mqtt_buffer_s { unsigned int length; unsigned char *data; }


decompiled_code:
int __fastcall mqtt_buffer_dump(unsigned int *a1)
{
_BYTE *v1; // rax
__int64 v2; // rcx

printf("[%d] ", *a1);
if ( !*a1 )
return mqtt_buffer_dump_ascii((__int64)a1);
v1 = (_BYTE *)*((_QWORD *)a1 + 1);
v2 = (__int64)&v1[*a1];
while ( (unsigned __int8)(*v1 - 32) <= 0x5Eu )
{
if ( ++v1 == (_BYTE *)v2 )
return mqtt_buffer_dump_ascii((__int64)a1);
}
return mqtt_buffer_dump_hex((__int64)a1);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int *a1)
{
_BYTE *v1; // rax
__int64 v2; // rcx

printf("[%d] ", *a1);
if ( !*a1 )
return func0((__int64)a1);
v1 = (_BYTE *)*((_QWORD *)a1 + 1);
v2 = (__int64)&v1[*a1];
while ( (unsigned __int8)(*v1 - 32) <= 0x5Eu )
{
if ( ++v1 == (_BYTE *)v2 )
return func0((__int64)a1);
}
return func1((__int64)a1);
}

original_code:
void mqtt_buffer_dump(mqtt_buffer_t* buffer) {
  printf("[%d] ", buffer->length);
  char hex = 0;
  for (int i=0;i<buffer->length;++i) {
    if (buffer->data[i] < 0x20 || buffer->data[i] > 0x7e) {
      hex = 1;
      break;
    }
  }
  if (hex) {
    mqtt_buffer_dump_hex(buffer);
  } else {
    mqtt_buffer_dump_ascii(buffer);
  }
}

canonical_original_code:
void mqtt_buffer_dump(struct mqtt_buffer_s *buffer) {
  printf("[%d] ", buffer->length);
  char hex = 0;
  for (int i = 0;i<buffer->length;++i) {
    if (buffer->data[i] < 0x20 || buffer->data[i] > 0x7e) {
      hex = 1;
      break;
    }
  }
  if (hex) {
    mqtt_buffer_dump_hex(buffer);
  } else {
    mqtt_buffer_dump_ascii(buffer);
  }
}

variable_types: hex char


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
__int64 v4; // rbx
size_t v5; // r12
unsigned __int8 *v6; // r13
unsigned __int8 *v7; // rbx
__int64 v8; // rsi
unsigned int v9; // eax
size_t v11; // r12
void *v12; // rax
const char *v13; // rax
char v14[4]; // [rsp+4h] [rbp-144h] BYREF
size_t v15; // [rsp+8h] [rbp-140h] BYREF
int v16[6]; // [rsp+10h] [rbp-138h] BYREF
size_t size; // [rsp+28h] [rbp-120h]
__int128 s1[6]; // [rsp+30h] [rbp-118h] BYREF
char v19[184]; // [rsp+90h] [rbp-B8h] BYREF

v3 = 0;
s1[0] = (__int128)_mm_load_si128((const __m128i *)&LC15);
s1[1] = (__int128)_mm_load_si128((const __m128i *)&LC16);
s1[2] = (__int128)_mm_load_si128((const __m128i *)&LC17);
s1[3] = (__int128)_mm_load_si128((const __m128i *)&LC18);
s1[4] = (__int128)_mm_load_si128((const __m128i *)&LC19);
s1[5] = (__int128)_mm_load_si128((const __m128i *)&LC20);
mqtt_parser_init(v16, argv, envp);
mqtt_serialiser_init(v14);
mqtt_message_init(v19);
v15 = 0LL;
printf("parser running (%zu bytes)\n", 0x60uLL);
do
{
while ( 1 )
{
printf(" loop %d\n", ++v3);
printf(" state: %d\n", (unsigned int)v16[0]);
printf(" offset: %zu\n", v15);
v4 = (unsigned int)mqtt_parser_execute(v16, v19, s1, "requested: %zu\n", (size_t)&v15);
printf(" rc: %d\n", v4);
if ( (_DWORD)v4 != 4 )
break;
printf(" bytes requested: %zu\n", size);
v11 = size;
v12 = malloc(size);
mqtt_parser_buffer(v16, v12, v11);
}
}
while ( (_DWORD)v4 == 1 );
putchar(10);
puts("parser info");
printf(" state: %d\n", (unsigned int)v16[0]);
if ( !(_DWORD)v4 )
{
v13 = (const char *)mqtt_error_string((unsigned int)v16[1]);
printf(" error: %s\n", v13);
}
printf(" nread: %zd\n", v15);
printf(" loops: %d\n", v3);
putchar(10);
mqtt_message_dump(v19);
putchar(10);
v5 = mqtt_serialiser_size(v14, v19);
v6 = (unsigned __int8 *)malloc(v5);
mqtt_serialiser_write(v14, v19, v6, v5);
printf("packet length: %zu\n", v5);
printf("packet data: ");
if ( v5 )
{
v7 = v6;
do
{
v8 = *v7++;
printf("%02x ", v8);
}
while ( v7 != &v6[v5] );
}
putchar(10);
putchar(10);
v9 = memcmp(s1, v6, v5);
printf("difference: %d\n", v9);
return 0;
}

original_code:
int main(int argc, char** argv) {
  mqtt_parser_t parser;
  mqtt_serialiser_t serialiser;
  mqtt_message_t message;
  uint8_t data[] = {
    0x10,
    0x5e,
      0x00, 0x06,
        0x4d, 0x51, 0x49, 0x73, 0x64, 0x70,
      0x03,
      0xf6,
      0x00, 0x1e,
      0x00, 0x05,
        0x68, 0x65, 0x6c, 0x6c, 0x6f,
      0x00, 0x06,
        0x73, 0x68, 0x6f, 0x75, 0x74, 0x73,
      0x00, 0x16,
        0x59, 0x4f, 0x20, 0x57, 0x48, 0x41, 0x54, 0x27, 0x53, 0x20, 0x55, 0x50, 0x20, 0x4d, 0x59, 0x20, 0x48, 0x4f, 0x4d, 0x49, 0x45, 0x53,
      0x00, 0x07,
        0x74, 0x68, 0x65, 0x75, 0x73, 0x65, 0x72,
      0x00, 0x20,
        0x62, 0x61, 0x33, 0x63, 0x38, 0x33, 0x33, 0x34, 0x38, 0x62, 0x64, 0x64, 0x66, 0x37, 0x62, 0x33, 0x36, 0x38, 0x62, 0x34, 0x37, 0x38, 0x61, 0x63, 0x30, 0x36, 0x64, 0x33, 0x33, 0x34, 0x30, 0x65,
  };
  mqtt_parser_init(&parser);
  mqtt_serialiser_init(&serialiser);
  mqtt_message_init(&message);
  size_t nread = 0;
  int rc = 0, loops = 0;
  printf("parser running (%zu bytes)\n", sizeof data);
  do {
    printf("  loop %d\n", ++loops);
    printf("    state: %d\n", parser.state);
    printf("    offset: %zu\n", nread);
    rc = mqtt_parser_execute(&parser, &message, data, sizeof data, &nread);
    printf("    rc: %d\n", rc);
    if (rc == MQTT_PARSER_RC_WANT_MEMORY) {
      printf("    bytes requested: %zu\n", parser.buffer_length);
      mqtt_parser_buffer(&parser, malloc(parser.buffer_length), parser.buffer_length);
    }
  } while (rc == MQTT_PARSER_RC_CONTINUE || rc == MQTT_PARSER_RC_WANT_MEMORY);
  printf("\n");
  printf("parser info\n");
  printf("  state: %d\n", parser.state);
  if (rc == MQTT_PARSER_RC_ERROR) {
    printf("  error: %s\n", mqtt_error_string(parser.error));
  }
  printf("  nread: %zd\n", nread);
  printf("  loops: %d\n", loops);
  printf("\n");
  mqtt_message_dump(&message);
  printf("\n");
  size_t packet_length = mqtt_serialiser_size(&serialiser, &message);
  uint8_t* packet = malloc(packet_length);
  mqtt_serialiser_write(&serialiser, &message, packet, packet_length);
  printf("packet length: %zu\n", packet_length);
  printf("packet data:   ");
  for (int i=0;i<packet_length;++i) {
    printf("%02x ", packet[i]);
  }
  printf("\n");
  printf("\n");
  printf("difference: %d\n", memcmp(data, packet, packet_length));
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  struct mqtt_parser_s parser;
  struct mqtt_serialiser_s serialiser;
  union mqtt_message_u message;
  unsigned char data[] = {
    0x10,
    0x5e,
      0x00, 0x06,
        0x4d, 0x51, 0x49, 0x73, 0x64, 0x70,
      0x03,
      0xf6,
      0x00, 0x1e,
      0x00, 0x05,
        0x68, 0x65, 0x6c, 0x6c, 0x6f,
      0x00, 0x06,
        0x73, 0x68, 0x6f, 0x75, 0x74, 0x73,
      0x00, 0x16,
        0x59, 0x4f, 0x20, 0x57, 0x48, 0x41, 0x54, 0x27, 0x53, 0x20, 0x55, 0x50, 0x20, 0x4d, 0x59, 0x20, 0x48, 0x4f, 0x4d, 0x49, 0x45, 0x53,
      0x00, 0x07,
        0x74, 0x68, 0x65, 0x75, 0x73, 0x65, 0x72,
      0x00, 0x20,
        0x62, 0x61, 0x33, 0x63, 0x38, 0x33, 0x33, 0x34, 0x38, 0x62, 0x64, 0x64, 0x66, 0x37, 0x62, 0x33, 0x36, 0x38, 0x62, 0x34, 0x37, 0x38, 0x61, 0x63, 0x30, 0x36, 0x64, 0x33, 0x33, 0x34, 0x30, 0x65,
  };
  mqtt_parser_init(&parser);
  mqtt_serialiser_init(&serialiser);
  mqtt_message_init(&message);
  unsigned long nread = 0;
  int rc = 0; int loops = 0;
  printf("parser running (%zu bytes)\n", sizeof data);
  do {
    printf("  loop %d\n", ++loops);
    printf("    state: %d\n", parser.state);
    printf("    offset: %zu\n", nread);
    rc = mqtt_parser_execute(&parser, &message, data, sizeof data, &nread);
    printf("    rc: %d\n", rc);
    if (rc == MQTT_PARSER_RC_WANT_MEMORY) {
      printf("    bytes requested: %zu\n", parser.buffer_length);
      mqtt_parser_buffer(&parser, malloc(parser.buffer_length), parser.buffer_length);
    }
  } while (rc == MQTT_PARSER_RC_CONTINUE || rc == MQTT_PARSER_RC_WANT_MEMORY);
  printf("\n");
  printf("parser info\n");
  printf("  state: %d\n", parser.state);
  if (rc == MQTT_PARSER_RC_ERROR) {
    printf("  error: %s\n", mqtt_error_string(parser.error));
  }
  printf("  nread: %zd\n", nread);
  printf("  loops: %d\n", loops);
  printf("\n");
  mqtt_message_dump(&message);
  printf("\n");
  unsigned long packet_length = mqtt_serialiser_size(&serialiser, &message);
  unsigned char *packet = malloc(packet_length);
  mqtt_serialiser_write(&serialiser, &message, packet, packet_length);
  printf("packet length: %zu\n", packet_length);
  printf("packet data:   ");
  for (int i = 0;i<packet_length;++i) {
    printf("%02x ", packet[i]);
  }
  printf("\n");
  printf("\n");
  printf("difference: %d\n", memcmp(data, packet, packet_length));
  return 0;
}

variable_types: struct mqtt_parser_s { enum mqtt_parser_state_e { MQTT_PARSER_STATE_INITIAL=0, MQTT_PARSER_STATE_REMAINING_LENGTH=1, MQTT_PARSER_STATE_VARIABLE_HEADER=2, MQTT_PARSER_STATE_CONNECT=3, MQTT_PARSER_STATE_CONNECT_PROTOCOL_NAME=4, MQTT_PARSER_STATE_CONNECT_PROTOCOL_VERSION=5, MQTT_PARSER_STATE_CONNECT_FLAGS=6, MQTT_PARSER_STATE_CONNECT_KEEP_ALIVE=7, MQTT_PARSER_STATE_CONNECT_CLIENT_IDENTIFIER=8, MQTT_PARSER_STATE_CONNECT_WILL_TOPIC=9, MQTT_PARSER_STATE_CONNECT_WILL_MESSAGE=10, MQTT_PARSER_STATE_CONNECT_USERNAME=11, MQTT_PARSER_STATE_CONNECT_PASSWORD=12, MQTT_PARSER_STATE_CONNACK=13, MQTT_PARSER_STATE_PUBLISH=14, MQTT_PARSER_STATE_PUBLISH_TOPIC_NAME=15, MQTT_PARSER_STATE_PUBLISH_MESSAGE_ID=16, MQTT_PARSER_STATE_PUBACK=17, MQTT_PARSER_STATE_PUBREC=18, MQTT_PARSER_STATE_PUBREL=19, MQTT_PARSER_STATE_PUBCOMP=20 } state; enum mqtt_error_e { MQTT_ERROR_NONE=0, MQTT_ERROR_PARSER_INVALID_STATE=1, MQTT_ERROR_PARSER_INVALID_REMAINING_LENGTH=2, MQTT_ERROR_PARSER_INVALID_MESSAGE_ID=3, MQTT_ERROR_SERIALISER_INVALID_MESSAGE_ID=4 } error; char buffer_pending; unsigned char *buffer; unsigned long buffer_length; }

variable_types: struct mqtt_serialiser_s { enum mqtt_error_e { MQTT_ERROR_NONE=0, MQTT_ERROR_PARSER_INVALID_STATE=1, MQTT_ERROR_PARSER_INVALID_REMAINING_LENGTH=2, MQTT_ERROR_PARSER_INVALID_MESSAGE_ID=3, MQTT_ERROR_SERIALISER_INVALID_MESSAGE_ID=4 } error; }

variable_types: union mqtt_message_u { struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } common; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; struct mqtt_buffer_s { unsigned int length; unsigned char *data; } protocol_name; unsigned char protocol_version; struct { char username_follows; char password_follows; char will_retain; char will_qos; char will; char clean_session; } flags; unsigned short keep_alive; struct mqtt_buffer_s client_id; struct mqtt_buffer_s will_topic; struct mqtt_buffer_s will_message; struct mqtt_buffer_s username; struct mqtt_buffer_s password; } connect; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned char _unused; unsigned char return_code; } connack; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; struct mqtt_buffer_s topic_name; unsigned short message_id; struct mqtt_buffer_s content; } publish; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } puback; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } pubrec; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } pubrel; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } pubcomp; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; struct mqtt_topicpair_s { struct mqtt_topicpair_s *next; struct mqtt_buffer_s name; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; } *topics; } subscribe; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; struct mqtt_topicpair_s *topics; } suback; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; struct mqtt_topic_s { struct mqtt_topic_s *next; struct mqtt_buffer_s name; } *topics; } unsubscribe; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } unsuback; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } pingreq; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } pingresp; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } disconnect; }

variable_types:
data
unsigned char []

variable_types:
nread
unsigned long

variable_types: loops int

variable_types:
packet
unsigned char *

user_defined_types: struct mqtt_parser_s { enum mqtt_parser_state_e state; enum mqtt_error_e error; char buffer_pending; unsigned char *buffer; unsigned long buffer_length; }

user_defined_types: struct mqtt_serialiser_s { enum mqtt_error_e error; }

user_defined_types: union mqtt_message_u { struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; } common; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; struct mqtt_buffer_s protocol_name; unsigned char protocol_version; struct { char username_follows; char password_follows; char will_retain; char will_qos; char will; char clean_session; } flags; unsigned short keep_alive; struct mqtt_buffer_s client_id; struct mqtt_buffer_s will_topic; struct mqtt_buffer_s will_message; struct mqtt_buffer_s username; struct mqtt_buffer_s password; } connect; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned char _unused; unsigned char return_code; } connack; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; struct mqtt_buffer_s topic_name; unsigned short message_id; struct mqtt_buffer_s content; } publish; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; } puback; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; } pubrec; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; } pubrel; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; } pubcomp; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; struct mqtt_topicpair_s *topics; } subscribe; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; struct mqtt_topicpair_s *topics; } suback; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; struct mqtt_topic_s *topics; } unsubscribe; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; unsigned short message_id; } unsuback; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; } pingreq; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; } pingresp; struct { enum mqtt_retain_e retain; enum mqtt_qos_e qos; enum mqtt_dup_e dup; enum mqtt_type_e type; unsigned int length; } disconnect; }

user_defined_types: struct mqtt_topicpair_s { struct mqtt_topicpair_s *next; struct mqtt_buffer_s name; enum mqtt_qos_e qos; }


decompiled_code:
__int64 __fastcall mqtt_message_init(_QWORD *a1)
{
int v1; // ecx
unsigned __int64 v2; // rdi
__int64 result; // rax

*a1 = 0LL;
v1 = (int)a1;
v2 = (unsigned __int64)(a1 + 1);
result = 0LL;
*(_QWORD *)(v2 + 120) = 0LL;
memset((void *)(v2 & 0xFFFFFFFFFFFFFFF8LL), 0, 8 * ((v1 - (v2 & 0xFFFFFFF8) + 136) >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
int v1; // ecx
unsigned __int64 v2; // rdi
__int64 result; // rax

*a1 = 0LL;
v1 = (int)a1;
v2 = (unsigned __int64)(a1 + 1);
result = 0LL;
*(_QWORD *)(v2 + 120) = 0LL;
memset((void *)(v2 & 0xFFFFFFFFFFFFFFF8LL), 0, 8 * ((v1 - (v2 & 0xFFFFFFF8) + 136) >> 3));
return result;
}

original_code:
void mqtt_message_init(mqtt_message_t* message) {
  memset(message, 0, sizeof (mqtt_message_t));
}

canonical_original_code:
void mqtt_message_init(union mqtt_message_u *message) {
  memset(message, 0, sizeof (mqtt_message_t));
}

variable_types: union mqtt_message_u { struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } common; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; struct mqtt_buffer_s { unsigned int length; unsigned char *data; } protocol_name; unsigned char protocol_version; struct { char username_follows; char password_follows; char will_retain; char will_qos; char will; char clean_session; } flags; unsigned short keep_alive; struct mqtt_buffer_s client_id; struct mqtt_buffer_s will_topic; struct mqtt_buffer_s will_message; struct mqtt_buffer_s username; struct mqtt_buffer_s password; } connect; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned char _unused; unsigned char return_code; } connack; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; struct mqtt_buffer_s topic_name; unsigned short message_id; struct mqtt_buffer_s content; } publish; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } puback; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } pubrec; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } pubrel; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } pubcomp; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; struct mqtt_topicpair_s { struct mqtt_topicpair_s *next; struct mqtt_buffer_s name; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; } *topics; } subscribe; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; struct mqtt_topicpair_s *topics; } suback; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; struct mqtt_topic_s { struct mqtt_topic_s *next; struct mqtt_buffer_s name; } *topics; } unsubscribe; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; unsigned short message_id; } unsuback; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } pingreq; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } pingresp; struct { enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 } retain; enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 } qos; enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 } dup; enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 } type; unsigned int length; } disconnect; } *


decompiled_code:
int __fastcall mqtt_message_dump(__int64 a1)
{
const char *v1; // rsi
const char *v2; // rsi
int result; // eax
const char *v4; // rsi
const char *v5; // rsi
const char *v6; // rsi
const char *v7; // rsi
const char *v8; // rsi

puts("message");
printf(" type: %d\n", *(unsigned int *)(a1 + 12));
printf(" qos: %d\n", *(unsigned int *)(a1 + 4));
v1 = "false";
if ( *(_DWORD *)(a1 + 8) )
v1 = "true";
printf(" dup: %s\n", v1);
v2 = "false";
if ( *(_DWORD *)a1 )
v2 = "true";
result = printf(" retain: %s\n", v2);
if ( *(_DWORD *)(a1 + 12) == 1 )
{
printf(" protocol name: ");
mqtt_buffer_dump(a1 + 24);
putchar(10);
printf(" protocol version: %d\n", *(unsigned __int8 *)(a1 + 40));
v4 = "false";
if ( *(_BYTE *)(a1 + 41) )
v4 = "true";
printf(" has username: %s\n", v4);
v5 = "false";
if ( *(_BYTE *)(a1 + 42) )
v5 = "true";
printf(" has password: %s\n", v5);
v6 = "false";
if ( *(_BYTE *)(a1 + 45) )
v6 = "true";
printf(" has will: %s\n", v6);
printf(" will qos: %d\n", (unsigned int)*(char *)(a1 + 44));
v7 = "false";
if ( *(_BYTE *)(a1 + 43) )
v7 = "true";
printf(" retains will: %s\n", v7);
v8 = "false";
if ( *(_BYTE *)(a1 + 46) )
v8 = "true";
printf(" clean session: %s\n", v8);
printf(" keep alive: %d\n", *(unsigned __int16 *)(a1 + 48));
printf(" client id: ");
mqtt_buffer_dump(a1 + 56);
putchar(10);
printf(" will topic: ");
mqtt_buffer_dump(a1 + 72);
putchar(10);
printf(" will message: ");
mqtt_buffer_dump(a1 + 88);
putchar(10);
printf(" username: ");
mqtt_buffer_dump(a1 + 104);
putchar(10);
printf(" password: ");
mqtt_buffer_dump(a1 + 120);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
const char *v1; // rsi
const char *v2; // rsi
int result; // eax
const char *v4; // rsi
const char *v5; // rsi
const char *v6; // rsi
const char *v7; // rsi
const char *v8; // rsi

puts("message");
printf(" type: %d\n", *(unsigned int *)(a1 + 12));
printf(" qos: %d\n", *(unsigned int *)(a1 + 4));
v1 = "false";
if ( *(_DWORD *)(a1 + 8) )
v1 = "true";
printf(" dup: %s\n", v1);
v2 = "false";
if ( *(_DWORD *)a1 )
v2 = "true";
result = printf(" retain: %s\n", v2);
if ( *(_DWORD *)(a1 + 12) == 1 )
{
printf(" protocol name: ");
mqtt_buffer_dump(a1 + 24);
putchar(10);
printf(" protocol version: %d\n", *(unsigned __int8 *)(a1 + 40));
v4 = "false";
if ( *(_BYTE *)(a1 + 41) )
v4 = "true";
printf(" has username: %s\n", v4);
v5 = "false";
if ( *(_BYTE *)(a1 + 42) )
v5 = "true";
printf(" has password: %s\n", v5);
v6 = "false";
if ( *(_BYTE *)(a1 + 45) )
v6 = "true";
printf(" has will: %s\n", v6);
printf(" will qos: %d\n", (unsigned int)*(char *)(a1 + 44));
v7 = "false";
if ( *(_BYTE *)(a1 + 43) )
v7 = "true";
printf(" retains will: %s\n", v7);
v8 = "false";
if ( *(_BYTE *)(a1 + 46) )
v8 = "true";
printf(" clean session: %s\n", v8);
printf(" keep alive: %d\n", *(unsigned __int16 *)(a1 + 48));
printf(" client id: ");
mqtt_buffer_dump(a1 + 56);
putchar(10);
printf(" will topic: ");
mqtt_buffer_dump(a1 + 72);
putchar(10);
printf(" will message: ");
mqtt_buffer_dump(a1 + 88);
putchar(10);
printf(" username: ");
mqtt_buffer_dump(a1 + 104);
putchar(10);
printf(" password: ");
mqtt_buffer_dump(a1 + 120);
result = putchar(10);
}
return result;
}

original_code:
void mqtt_message_dump(mqtt_message_t* message) {
  printf("message\n");
  printf("  type:              %d\n", message->common.type);
  printf("  qos:               %d\n", message->common.qos);
  printf("  dup:               %s\n", message->common.dup ? "true" : "false");
  printf("  retain:            %s\n", message->common.retain ? "true" : "false");
  if (message->common.type == MQTT_TYPE_CONNECT) {
    printf("  protocol name:     ");
    mqtt_buffer_dump(&(message->connect.protocol_name));
    printf("\n");
    printf("  protocol version:  %d\n", message->connect.protocol_version);
    printf("  has username:      %s\n", message->connect.flags.username_follows ? "true": "false");
    printf("  has password:      %s\n", message->connect.flags.password_follows ? "true": "false");
    printf("  has will:          %s\n", message->connect.flags.will ? "true": "false");
    printf("  will qos:          %d\n", message->connect.flags.will_qos);
    printf("  retains will:      %s\n", message->connect.flags.will_retain ? "true": "false");
    printf("  clean session:     %s\n", message->connect.flags.clean_session ? "true": "false");
    printf("  keep alive:        %d\n", message->connect.keep_alive);
    printf("  client id:         ");
    mqtt_buffer_dump(&(message->connect.client_id));
    printf("\n");
    printf("  will topic:        ");
    mqtt_buffer_dump(&(message->connect.will_topic));
    printf("\n");
    printf("  will message:      ");
    mqtt_buffer_dump(&(message->connect.will_message));
    printf("\n");
    printf("  username:          ");
    mqtt_buffer_dump(&(message->connect.username));
    printf("\n");
    printf("  password:          ");
    mqtt_buffer_dump(&(message->connect.password));
    printf("\n");
  }
}

canonical_original_code:
void mqtt_message_dump(union mqtt_message_u *message) {
  printf("message\n");
  printf("  type:              %d\n", message->common.type);
  printf("  qos:               %d\n", message->common.qos);
  printf("  dup:               %s\n", message->common.dup ? "true" : "false");
  printf("  retain:            %s\n", message->common.retain ? "true" : "false");
  if (message->common.type == MQTT_TYPE_CONNECT) {
    printf("  protocol name:     ");
    mqtt_buffer_dump(&(message->connect.protocol_name));
    printf("\n");
    printf("  protocol version:  %d\n", message->connect.protocol_version);
    printf("  has username:      %s\n", message->connect.flags.username_follows ? "true": "false");
    printf("  has password:      %s\n", message->connect.flags.password_follows ? "true": "false");
    printf("  has will:          %s\n", message->connect.flags.will ? "true": "false");
    printf("  will qos:          %d\n", message->connect.flags.will_qos);
    printf("  retains will:      %s\n", message->connect.flags.will_retain ? "true": "false");
    printf("  clean session:     %s\n", message->connect.flags.clean_session ? "true": "false");
    printf("  keep alive:        %d\n", message->connect.keep_alive);
    printf("  client id:         ");
    mqtt_buffer_dump(&(message->connect.client_id));
    printf("\n");
    printf("  will topic:        ");
    mqtt_buffer_dump(&(message->connect.will_topic));
    printf("\n");
    printf("  will message:      ");
    mqtt_buffer_dump(&(message->connect.will_message));
    printf("\n");
    printf("  username:          ");
    mqtt_buffer_dump(&(message->connect.username));
    printf("\n");
    printf("  password:          ");
    mqtt_buffer_dump(&(message->connect.password));
    printf("\n");
  }
}

user_defined_types: enum mqtt_retain_e { MQTT_RETAIN_FALSE=0, MQTT_RETAIN_TRUE=1 }

user_defined_types: enum mqtt_qos_e { MQTT_QOS_AT_MOST_ONCE=0, MQTT_QOS_AT_LEAST_ONCE=1, MQTT_QOS_EXACTLY_ONCE=2 }

user_defined_types: enum mqtt_dup_e { MQTT_DUP_FALSE=0, MQTT_DUP_TRUE=1 }

user_defined_types: enum mqtt_type_e { MQTT_TYPE_CONNECT=1, MQTT_TYPE_CONNACK=2, MQTT_TYPE_PUBLISH=3, MQTT_TYPE_PUBACK=4, MQTT_TYPE_PUBREC=5, MQTT_TYPE_PUBREL=6, MQTT_TYPE_PUBCOMP=7, MQTT_TYPE_SUBSCRIBE=8, MQTT_TYPE_SUBACK=9, MQTT_TYPE_UNSUBSCRIBE=10, MQTT_TYPE_UNSUBACK=11, MQTT_TYPE_PINGREQ=12, MQTT_TYPE_PINGRESP=13, MQTT_TYPE_DISCONNECT=14 }

user_defined_types: struct mqtt_topic_s { struct mqtt_topic_s *next; struct mqtt_buffer_s name; }


decompiled_code:
void __fastcall mqtt_serialiser_init(_DWORD *a1)
{
*a1 = 0;
}

canonical_decompiled_code:
void __fastcall func0(_DWORD *a1)
{
*a1 = 0;
}

original_code:
void mqtt_serialiser_init(mqtt_serialiser_t* serialiser) {
  memset(serialiser, 0, sizeof (mqtt_serialiser_t));
}

canonical_original_code:
void mqtt_serialiser_init(struct mqtt_serialiser_s *serialiser) {
  memset(serialiser, 0, sizeof (mqtt_serialiser_t));
}

variable_types: struct mqtt_serialiser_s { enum mqtt_error_e { MQTT_ERROR_NONE=0, MQTT_ERROR_PARSER_INVALID_STATE=1, MQTT_ERROR_PARSER_INVALID_REMAINING_LENGTH=2, MQTT_ERROR_PARSER_INVALID_MESSAGE_ID=3, MQTT_ERROR_SERIALISER_INVALID_MESSAGE_ID=4 } error; } *


decompiled_code:
__int64 __fastcall mqtt_serialiser_size(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
__int64 result; // rax
int v4; // edx
bool v5; // cf

v2 = *(_DWORD *)(a2 + 16);
result = 2LL;
if ( v2 > 0x7F )
{
result = 3LL;
if ( v2 > 0x3FFF )
{
result = 4LL;
if ( v2 > 0x1FFFFF )
{
v5 = v2 < 0x10000000;
v4 = *(_DWORD *)(a2 + 12);
result = v5 ? 5LL : 1LL;
if ( v4 == 1 )
goto LABEL_9;
LABEL_4:
if ( v4 == 2 )
result += 2LL;
return result;
}
}
}
v4 = *(_DWORD *)(a2 + 12);
if ( v4 != 1 )
goto LABEL_4;
LABEL_9:
result += *(unsigned int *)(a2 + 24) + (unsigned __int64)*(unsigned int *)(a2 + 56) + 8;
if ( *(_BYTE *)(a2 + 41) )
result += *(unsigned int *)(a2 + 104) + 2LL;
if ( *(_BYTE *)(a2 + 42) )
result += *(unsigned int *)(a2 + 120) + 2LL;
if ( *(_BYTE *)(a2 + 45) )
result += *(unsigned int *)(a2 + 72) + (unsigned __int64)*(unsigned int *)(a2 + 88) + 4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
unsigned int v2; // edx
__int64 result; // rax
int v4; // edx
bool v5; // cf

v2 = *(_DWORD *)(a2 + 16);
result = 2LL;
if ( v2 > 0x7F )
{
result = 3LL;
if ( v2 > 0x3FFF )
{
result = 4LL;
if ( v2 > 0x1FFFFF )
{
v5 = v2 < 0x10000000;
v4 = *(_DWORD *)(a2 + 12);
result = v5 ? 5LL : 1LL;
if ( v4 == 1 )
goto LABEL_9;
LABEL_4:
if ( v4 == 2 )
result += 2LL;
return result;
}
}
}
v4 = *(_DWORD *)(a2 + 12);
if ( v4 != 1 )
goto LABEL_4;
LABEL_9:
result += *(unsigned int *)(a2 + 24) + (unsigned __int64)*(unsigned int *)(a2 + 56) + 8;
if ( *(_BYTE *)(a2 + 41) )
result += *(unsigned int *)(a2 + 104) + 2LL;
if ( *(_BYTE *)(a2 + 42) )
result += *(unsigned int *)(a2 + 120) + 2LL;
if ( *(_BYTE *)(a2 + 45) )
result += *(unsigned int *)(a2 + 72) + (unsigned __int64)*(unsigned int *)(a2 + 88) + 4;
return result;
}

original_code:
size_t mqtt_serialiser_size(mqtt_serialiser_t* serialiser, mqtt_message_t* message) {
  size_t len = 1;
  if (message->common.length <= 127) {
    len += 1;
  } else if (message->common.length <= 16383) {
    len += 2;
  } else if (message->common.length <= 2097151) {
    len += 3;
  } else if (message->common.length <= 268435455) {
    len += 4;
  }
  if (message->common.type == MQTT_TYPE_CONNECT) {
    len += 8;
    len += message->connect.protocol_name.length;
    len += message->connect.client_id.length;
    if (message->connect.flags.username_follows) {
      len += 2;
      len += message->connect.username.length;
    }
    if (message->connect.flags.password_follows) {
      len += 2;
      len += message->connect.password.length;
    }
    if (message->connect.flags.will) {
      len += 4;
      len += message->connect.will_topic.length;
      len += message->connect.will_message.length;
    }
  } else if (message->common.type == MQTT_TYPE_CONNACK) {
    len += 2;
  }
  return len;
}

canonical_original_code:
unsigned long mqtt_serialiser_size(struct mqtt_serialiser_s *serialiser, union mqtt_message_u *message) {
  unsigned long len = 1;
  if (message->common.length <= 127) {
    len += 1;
  } else if (message->common.length <= 16383) {
    len += 2;
  } else if (message->common.length <= 2097151) {
    len += 3;
  } else if (message->common.length <= 268435455) {
    len += 4;
  }
  if (message->common.type == MQTT_TYPE_CONNECT) {
    len += 8;
    len += message->connect.protocol_name.length;
    len += message->connect.client_id.length;
    if (message->connect.flags.username_follows) {
      len += 2;
      len += message->connect.username.length;
    }
    if (message->connect.flags.password_follows) {
      len += 2;
      len += message->connect.password.length;
    }
    if (message->connect.flags.will) {
      len += 4;
      len += message->connect.will_topic.length;
      len += message->connect.will_message.length;
    }
  } else if (message->common.type == MQTT_TYPE_CONNACK) {
    len += 2;
  }
  return len;
}

variable_types:
len
unsigned long

return_type: unsigned long

user_defined_types: enum mqtt_error_e { MQTT_ERROR_NONE=0, MQTT_ERROR_PARSER_INVALID_STATE=1, MQTT_ERROR_PARSER_INVALID_REMAINING_LENGTH=2, MQTT_ERROR_PARSER_INVALID_MESSAGE_ID=3, MQTT_ERROR_SERIALISER_INVALID_MESSAGE_ID=4 }