main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
int v4; // ebx
char buf; // [rsp+Fh] [rbp-259h] BYREF
char v6[4]; // [rsp+10h] [rbp-258h] BYREF
int v7; // [rsp+14h] [rbp-254h]
char v8; // [rsp+18h] [rbp-250h]
int v9; // [rsp+1Ah] [rbp-24Eh]
int v10; // [rsp+20h] [rbp-248h]
char v11; // [rsp+24h] [rbp-244h]
__int64 v12; // [rsp+28h] [rbp-240h]
char v13[256]; // [rsp+30h] [rbp-238h] BYREF
char v14[264]; // [rsp+130h] [rbp-138h] BYREF
__int64 v15; // [rsp+238h] [rbp-30h]

v3 = (unsigned int)openPort("/dev/ttyACM0");
printf("serial status %d\n", v3);
initConsts();
v8 = 1;
v6[0] = DummyPacketID;
v9 = 67305474;
v10 = 1085276160;
v11 = -56;
v12 = -128849018810000LL;
v7 = 0;
DecoderInit(v14, 0LL);
while ( 1 )
{
v4 = 0;
while ( read(v3, &buf, 1uLL) > 0 )
{
if ( v4 )
goto LABEL_8;
v4 = DecoderPutChar(v14, (unsigned int)buf);
}
if ( v4 )
{
LABEL_8:
parsePacket(v15, v6, 0LL);
writePacket(v6, v13, 1LL);
printf(" %s \n", v13);
}
fflush(stdout);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
int v4; // ebx
char buf; // [rsp+Fh] [rbp-259h] BYREF
char v6[4]; // [rsp+10h] [rbp-258h] BYREF
int v7; // [rsp+14h] [rbp-254h]
char v8; // [rsp+18h] [rbp-250h]
int v9; // [rsp+1Ah] [rbp-24Eh]
int v10; // [rsp+20h] [rbp-248h]
char v11; // [rsp+24h] [rbp-244h]
__int64 v12; // [rsp+28h] [rbp-240h]
char v13[256]; // [rsp+30h] [rbp-238h] BYREF
char v14[264]; // [rsp+130h] [rbp-138h] BYREF
__int64 v15; // [rsp+238h] [rbp-30h]

v3 = (unsigned int)func0("/dev/ttyACM0");
printf("serial status %d\n", v3);
initConsts();
v8 = 1;
v6[0] = DummyPacketID;
v9 = 67305474;
v10 = 1085276160;
v11 = -56;
v12 = -128849018810000LL;
v7 = 0;
DecoderInit(v14, 0LL);
while ( 1 )
{
v4 = 0;
while ( read(v3, &buf, 1uLL) > 0 )
{
if ( v4 )
goto LABEL_8;
v4 = DecoderPutChar(v14, (unsigned int)buf);
}
if ( v4 )
{
LABEL_8:
parsePacket(v15, v6, 0LL);
writePacket(v6, v13, 1LL);
printf(" %s \n", v13);
}
fflush(stdout);
}
}

original_code:
int main() {
  int serialFd=openPort("/dev/ttyACM0");
  printf("serial status %d\n",serialFd);
  initConsts();
  struct DummyPacket p;
  char returnData[255];
  p.field_1=1;
  p.field_2=2;
  p.field_3=3;
  p.field_4=4;
  p.field_5=5.5;
  p.field_6=200;
  p.field_7=70000;
  p.field_8=-30000;
  struct Packet pp;
  pp.id=DummyPacketID;
  pp.dummy=p;
  pp.seq=0;
  int ascii = 0;
int canIncrement=0;
struct PacketDecoder pDecoder;
DecoderInit(&pDecoder,ascii);
int complete = 0;
  while(1){
    char c;
    int bytesRead=0;
    while(read(serialFd, &c, 1)>0 && !complete){
      complete = DecoderPutChar(&pDecoder,(unsigned char)c);
    }
    if(complete){
      parsePacket(pDecoder.buffer_start,&pp,ascii);
      char printBuffer[255];
      writePacket(&pp,printBuffer,1);
      printf(" %s \n",printBuffer);
    }
    complete=0;
    fflush(stdout);
  }
return 0;
}

canonical_original_code:
int main() {
  int serialFd = openPort("/dev/ttyACM0");
  printf("serial status %d\n",serialFd);
  initConsts();
  struct DummyPacket p;
  char returnData[255];
  p.field_1=1;
  p.field_2=2;
  p.field_3=3;
  p.field_4=4;
  p.field_5=5.5;
  p.field_6=200;
  p.field_7=70000;
  p.field_8=-30000;
  struct Packet pp;
  pp.id=DummyPacketID;
  pp.dummy=p;
  pp.seq=0;
  int ascii = 0;
int canIncrement = 0;
struct PacketDecoder pDecoder;
DecoderInit(&pDecoder,ascii);
int complete = 0;
  while(1){
    char c;
    int bytesRead = 0;
    while(read(serialFd, &c, 1)>0 && !complete){
      complete = DecoderPutChar(&pDecoder,(unsigned char)c);
    }
    if(complete){
      parsePacket(pDecoder.buffer_start,&pp,ascii);
      char printBuffer[255];
      writePacket(&pp,printBuffer,1);
      printf(" %s \n",printBuffer);
    }
    complete=0;
    fflush(stdout);
  }
return 0;
}

variable_types: serialFd int

variable_types:
returnData
char [255]

variable_types: struct Packet { unsigned char id; unsigned char lenght; unsigned int seq; union { struct DummyPacket { unsigned char field_1; unsigned short field_2; char field_3; char field_4; float field_5; unsigned char field_6; unsigned int field_7; int field_8; } dummy; struct DumbPacket { unsigned char stupid; } dumb; struct SlimStatePacket { unsigned short leftEncoder; unsigned short rightEncoder; } slimState; struct InitPacket { unsigned char init; } init; struct SpeedPacket { short leftTick; short rightTick; } speed; }; }

variable_types: ascii int

variable_types: canIncrement int

variable_types: struct PacketDecoder { int ascii; int status; char buffer[255]; char *buffer_start; char *bufferPtr; unsigned char lenght; unsigned char checksum; }

variable_types: complete int

variable_types: c char

variable_types: bytesRead int

variable_types:
printBuffer
char [255]

user_defined_types: struct Packet { unsigned char id; unsigned char lenght; unsigned int seq; union { struct DummyPacket dummy; struct DumbPacket dumb; struct SlimStatePacket slimState; struct InitPacket init; struct SpeedPacket speed; }; }


initConsts

repo: mauriliodc/capybara

decompiled_code:
void initConsts()
{
DummyPacketID = 1;
DumbPacketID = 2;
SlimStatePacketID = 3;
InitPacketID = 4;
SpeedPacketID = 5;
header_1 = -86;
header_2 = 85;
AsciiHeader = 40;
AsciiFooter = 41;
}

canonical_decompiled_code:
void func0()
{
DummyPacketID = 1;
DumbPacketID = 2;
SlimStatePacketID = 3;
InitPacketID = 4;
SpeedPacketID = 5;
header_1 = -86;
header_2 = 85;
AsciiHeader = 40;
AsciiFooter = 41;
}

original_code:
void initConsts() {
    DummyPacketID = 1;
    DumbPacketID = 2;
    SlimStatePacketID = 3;
    InitPacketID = 4;
    SpeedPacketID = 5;
    header_1 = 0xaa;
    header_2 = 0x55;
    AsciiHeader = '(';
    AsciiFooter = ')';
}

return_type: void


DecoderInit

decompiled_code:
__int64 __fastcall DecoderInit(_QWORD *a1, int a2)
{
__int64 v2; // r8
__int64 result; // rax

ResetDecoderBuffer(a1);
result = v2 + 8;
*(_DWORD *)(v2 + 4) = 0;
*(_QWORD *)(v2 + 264) = v2 + 8;
*(_QWORD *)(v2 + 272) = v2 + 8;
*(_BYTE *)(v2 + 280) = 0;
*(_DWORD *)v2 = a2;
return result;
}

original_code:
void DecoderInit(struct PacketDecoder* d, int ascii) {
    ResetDecoderBuffer(d);
    d->buffer_start = &(d->buffer[0]);
    d->bufferPtr = &(d->buffer[0]);
    d->status = Unsync;
    d->lenght = 0;
    d->ascii = ascii;
}

canonical_original_code:
void DecoderInit(struct PacketDecoder *d, int ascii) {
    ResetDecoderBuffer(d);
    d->buffer_start = &(d->buffer[0]);
    d->bufferPtr = &(d->buffer[0]);
    d->status = Unsync;
    d->lenght = 0;
    d->ascii = ascii;
}

variable_types: struct PacketDecoder { int ascii; int status; char buffer[255]; char *buffer_start; char *bufferPtr; unsigned char lenght; unsigned char checksum; } *


DecoderPutChar

decompiled_code:
__int64 __fastcall DecoderPutChar(unsigned int *a1, char a2)
{
__int64 result; // rax
unsigned int v3; // edx
unsigned int v4; // edx
__int64 v5; // r8
bool v6; // zf
__int64 v7; // r8
char v8; // dl
unsigned int v9; // [rsp+4h] [rbp-4h]

result = *a1;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
v4 = a1[1];
if ( v4 )
{
if ( v4 == 3 )
{
if ( AsciiHeader == a2 )
goto LABEL_6;
if ( AsciiFooter == a2 )
{
a1[1] = 0;
return result;
}
*(_BYTE *)(*((_QWORD *)a1 + 34))++ = a2;
}
}
else
{
ResetDecoderBuffer(a1);
if ( AsciiHeader == a2 )
{
*(_DWORD *)(v7 + 4) = 3;
return 0LL;
}
}
}
return 0LL;
}
v3 = a1[1];
switch ( v3 )
{
case 0u:
v9 = *a1;
ResetDecoderBuffer(a1);
v6 = header_1 == a2;
result = v9;
*(_BYTE *)(v5 + 281) = 0;
if ( v6 )
{
*(_DWORD *)(v5 + 4) = 1;
return result;
}
return 0LL;
case 1u:
if ( header_2 != a2 )
goto LABEL_6;
a1[1] = 2;
break;
case 2u:
*((_BYTE *)a1 + 280) = a2;
a1[1] = 3;
break;
case 3u:
v8 = a2 ^ *((_BYTE *)a1 + 281);
v6 = *((_BYTE *)a1 + 280) == 0;
*((_BYTE *)a1 + 281) = v8;
if ( v6 )
{
a1[1] = 0;
result = v8 == 0;
}
else
{
*(_BYTE *)(*((_QWORD *)a1 + 34))++ = a2;
--*((_BYTE *)a1 + 280);
}
break;
default:
LABEL_6:
a1[1] = 0;
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1, char a2)
{
__int64 result; // rax
unsigned int v3; // edx
unsigned int v4; // edx
__int64 v5; // r8
bool v6; // zf
__int64 v7; // r8
char v8; // dl
unsigned int v9; // [rsp+4h] [rbp-4h]

result = *a1;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
v4 = a1[1];
if ( v4 )
{
if ( v4 == 3 )
{
if ( AsciiHeader == a2 )
goto LABEL_6;
if ( AsciiFooter == a2 )
{
a1[1] = 0;
return result;
}
*(_BYTE *)(*((_QWORD *)a1 + 34))++ = a2;
}
}
else
{
func1(a1);
if ( AsciiHeader == a2 )
{
*(_DWORD *)(v7 + 4) = 3;
return 0LL;
}
}
}
return 0LL;
}
v3 = a1[1];
switch ( v3 )
{
case 0u:
v9 = *a1;
func1(a1);
v6 = header_1 == a2;
result = v9;
*(_BYTE *)(v5 + 281) = 0;
if ( v6 )
{
*(_DWORD *)(v5 + 4) = 1;
return result;
}
return 0LL;
case 1u:
if ( header_2 != a2 )
goto LABEL_6;
a1[1] = 2;
break;
case 2u:
*((_BYTE *)a1 + 280) = a2;
a1[1] = 3;
break;
case 3u:
v8 = a2 ^ *((_BYTE *)a1 + 281);
v6 = *((_BYTE *)a1 + 280) == 0;
*((_BYTE *)a1 + 281) = v8;
if ( v6 )
{
a1[1] = 0;
result = v8 == 0;
}
else
{
*(_BYTE *)(*((_QWORD *)a1 + 34))++ = a2;
--*((_BYTE *)a1 + 280);
}
break;
default:
LABEL_6:
a1[1] = 0;
return 0LL;
}
return result;
}

original_code:
int DecoderPutChar(struct PacketDecoder* d, char c) {
    if (d->ascii == 0) {
        if (d->status == Unsync) {
            ResetDecoderBuffer(d);
            d->checksum = 0;
            if (c == header_1) {
                d->status = Sync;
            }
        } else if (d->status == Sync) {
            if (c == header_2) {
                d->status = Length;
            } else {
                d->status = Unsync;
            }
        } else if (d->status == Length) {
            d->lenght = (uint8_t) c;
            d->status = Payload;
        } else if (d->status == Payload) {
            if (d->lenght > 0) {
                d->checksum ^= c;
                *(d->bufferPtr) = c;
                d->bufferPtr++;
                d->lenght--;
            } else {
                d->checksum ^= c;
                d->status = Unsync;
                if (d->checksum == 0) return 1;
                else return 0;
            }
        } else {
            d->status = Unsync;
        }
    }
    else if (d->ascii == 1) {
        if (d->status == Unsync) {
            ResetDecoderBuffer(d);
            if (c == AsciiHeader) {
                d->status = Payload;
            }
        } else if (d->status == Payload) {
            if (c == AsciiHeader) {
                d->status = Unsync;
            }
            else if (c == AsciiFooter) {
                d->status = Unsync;
                return 1;
            }
            else {
                *(d->bufferPtr) = c;
                d->bufferPtr++;
            }
        }
    }
    return 0;
}

canonical_original_code:
int DecoderPutChar(struct PacketDecoder *d, char c) {
    if (d->ascii == 0) {
        if (d->status == Unsync) {
            ResetDecoderBuffer(d);
            d->checksum = 0;
            if (c == header_1) {
                d->status = Sync;
            }
        } else if (d->status == Sync) {
            if (c == header_2) {
                d->status = Length;
            } else {
                d->status = Unsync;
            }
        } else if (d->status == Length) {
            d->lenght = (unsigned char) c;
            d->status = Payload;
        } else if (d->status == Payload) {
            if (d->lenght > 0) {
                d->checksum ^= c;
                *(d->bufferPtr) = c;
                d->bufferPtr++;
                d->lenght--;
            } else {
                d->checksum ^= c;
                d->status = Unsync;
                if (d->checksum == 0) return 1;
                else return 0;
            }
        } else {
            d->status = Unsync;
        }
    }
    else if (d->ascii == 1) {
        if (d->status == Unsync) {
            ResetDecoderBuffer(d);
            if (c == AsciiHeader) {
                d->status = Payload;
            }
        } else if (d->status == Payload) {
            if (c == AsciiHeader) {
                d->status = Unsync;
            }
            else if (c == AsciiFooter) {
                d->status = Unsync;
                return 1;
            }
            else {
                *(d->bufferPtr) = c;
                d->bufferPtr++;
            }
        }
    }
    return 0;
}

return_type: int

user_defined_types: struct PacketDecoder { int ascii; int status; char buffer[255]; char *buffer_start; char *bufferPtr; unsigned char lenght; unsigned char checksum; }


parsePacket

decompiled_code:
__int64 __fastcall parsePacket(__int64 a1, _BYTE *a2, unsigned int a3)
{
__int64 v4; // r12
char v5; // al
__int64 result; // rax
char v7[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( !a3 )
{
v4 = readUint8(v7, a1);
v5 = v7[0];
*a2 = v7[0];
if ( v5 != DummyPacketID )
goto LABEL_3;
LABEL_8:
readDummyPacket(a2, v4, a3);
return v4;
}
v4 = readUint8Ascii(v7, a1);
v5 = v7[0];
*a2 = v7[0];
if ( v5 == DummyPacketID )
goto LABEL_8;
LABEL_3:
if ( v5 == DumbPacketID )
{
readDumbPacket(a2, v4, a3);
result = v4;
}
else
{
if ( v5 == SlimStatePacketID )
readSlimStatePacket(a2, v4, a3);
result = v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _BYTE *a2, unsigned int a3)
{
__int64 v4; // r12
char v5; // al
__int64 result; // rax
char v7[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( !a3 )
{
v4 = readUint8(v7, a1);
v5 = v7[0];
*a2 = v7[0];
if ( v5 != DummyPacketID )
goto LABEL_3;
LABEL_8:
readDummyPacket(a2, v4, a3);
return v4;
}
v4 = readUint8Ascii(v7, a1);
v5 = v7[0];
*a2 = v7[0];
if ( v5 == DummyPacketID )
goto LABEL_8;
LABEL_3:
if ( v5 == DumbPacketID )
{
readDumbPacket(a2, v4, a3);
result = v4;
}
else
{
if ( v5 == SlimStatePacketID )
readSlimStatePacket(a2, v4, a3);
result = v4;
}
return result;
}

original_code:
char* parsePacket(char* buffer, struct Packet* p, int ascii) {
    uint8_t id;
    if (!ascii) {
        buffer = readUint8(&id, buffer);
    } else {
        buffer = readUint8Ascii((char*) &id, buffer);
    }
    p->id = id;
    if (id == DummyPacketID) {
        readDummyPacket(p, buffer, ascii);
    } else if (id == DumbPacketID) {
        readDumbPacket(p, buffer, ascii);
    } else if (id == SlimStatePacketID) {
        readSlimStatePacket(p, buffer, ascii);
    }
    return buffer;
}

canonical_original_code:
char *parsePacket(char *buffer, struct Packet *p, int ascii) {
    unsigned char id;
    if (!ascii) {
        buffer = readUint8(&id, buffer);
    } else {
        buffer = readUint8Ascii((char *) &id, buffer);
    }
    p->id = id;
    if (id == DummyPacketID) {
        readDummyPacket(p, buffer, ascii);
    } else if (id == DumbPacketID) {
        readDumbPacket(p, buffer, ascii);
    } else if (id == SlimStatePacketID) {
        readSlimStatePacket(p, buffer, ascii);
    }
    return buffer;
}

variable_types:
buffer
char *

variable_types: struct Packet { unsigned char id; unsigned char lenght; unsigned int seq; union { struct DummyPacket { unsigned char field_1; unsigned short field_2; char field_3; char field_4; float field_5; unsigned char field_6; unsigned int field_7; int field_8; } dummy; struct DumbPacket { unsigned char stupid; } dumb; struct SlimStatePacket { unsigned short leftEncoder; unsigned short rightEncoder; } slimState; struct InitPacket { unsigned char init; } init; struct SpeedPacket { short leftTick; short rightTick; } speed; }; } *

variable_types:
id
unsigned char

return_type: char *


computeChecksum

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

v2 = a2;
LOBYTE(v2) = a2 - 1;
if ( (_BYTE)a2 != 1 )
{
v3 = (_BYTE *)(a1 - (unsigned __int8)v2);
v4 = a1 + (unsigned __int8)(a2 - 2) + 1 - (unsigned __int8)v2;
v2 = 0;
do
LOBYTE(v2) = *v3++ ^ v2;
while ( v3 != (_BYTE *)v4 );
}
return v2;
}

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

v2 = a2;
LOBYTE(v2) = a2 - 1;
if ( (_BYTE)a2 != 1 )
{
v3 = (_BYTE *)(a1 - (unsigned __int8)v2);
v4 = a1 + (unsigned __int8)(a2 - 2) + 1 - (unsigned __int8)v2;
v2 = 0;
do
LOBYTE(v2) = *v3++ ^ v2;
while ( v3 != (_BYTE *)v4 );
}
return v2;
}

original_code:
uint8_t computeChecksum(char* buffer, uint8_t length) {
    uint8_t checksum = 0;
    uint8_t len = length - 1;
    while (len) {
        checksum ^= *(buffer - len);
        len--;
    }
    return checksum;
}

canonical_original_code:
unsigned char computeChecksum(char *buffer, unsigned char length) {
    unsigned char checksum = 0;
    unsigned char len = length - 1;
    while (len) {
        checksum ^= *(buffer - len);
        len--;
    }
    return checksum;
}

variable_types:
length
unsigned char

variable_types:
checksum
unsigned char

variable_types:
len
unsigned char

return_type: unsigned char


writePacket

decompiled_code:
__int64 __fastcall writePacket(char *a1, __int64 a2, unsigned int a3)
{
char v3; // bp
char v6; // al
unsigned __int8 v8; // al

if ( a3 )
{
v6 = *a1;
if ( *a1 != DummyPacketID )
goto LABEL_3;
}
else
{
writeUint8((unsigned __int8)header_1);
a2 = writeUint8((unsigned __int8)header_2);
v6 = *a1;
if ( *a1 != DummyPacketID )
{
LABEL_3:
if ( v6 == DumbPacketID )
{
v3 = a2;
a2 = writeDumbPacket(a1, a2, a3);
}
else if ( v6 == SlimStatePacketID )
{
v3 = a2;
a2 = writeSlimStatePacket(a1, a2, a3);
}
if ( a3 )
return a2;
goto LABEL_10;
}
}
v3 = a2;
a2 = writeDummyPacket(a1, a2, a3);
if ( a3 )
return a2;
LABEL_10:
v8 = computeChecksum(a2, (unsigned __int8)(a2 - v3));
return writeUint8(v8);
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1, __int64 a2, unsigned int a3)
{
char v3; // bp
char v6; // al
unsigned __int8 v8; // al

if ( a3 )
{
v6 = *a1;
if ( *a1 != DummyPacketID )
goto LABEL_3;
}
else
{
writeUint8((unsigned __int8)header_1);
a2 = writeUint8((unsigned __int8)header_2);
v6 = *a1;
if ( *a1 != DummyPacketID )
{
LABEL_3:
if ( v6 == DumbPacketID )
{
v3 = a2;
a2 = writeDumbPacket(a1, a2, a3);
}
else if ( v6 == SlimStatePacketID )
{
v3 = a2;
a2 = writeSlimStatePacket(a1, a2, a3);
}
if ( a3 )
return a2;
goto LABEL_10;
}
}
v3 = a2;
a2 = writeDummyPacket(a1, a2, a3);
if ( a3 )
return a2;
LABEL_10:
v8 = func5(a2, (unsigned __int8)(a2 - v3));
return writeUint8(v8);
}

original_code:
char* writePacket(const struct Packet* p, char* buffer, int ascii) {
    char* buffPtr;
    if (!ascii) {
        buffer = writeUint8(header_1, buffer);
        buffer = writeUint8(header_2, buffer);
    }
    if (p->id == DummyPacketID) {
        buffPtr = buffer;
        buffer = writeDummyPacket(p, buffer, ascii);
    } else if (p->id == DumbPacketID) {
        buffPtr = buffer;
        buffer = writeDumbPacket(p, buffer, ascii);
    } else if (p->id == SlimStatePacketID) {
        buffPtr = buffer;
        buffer = writeSlimStatePacket(p, buffer, ascii);
    }
    if (!ascii) {
        uint8_t checksum = computeChecksum(buffer, buffer - buffPtr);
        buffer = writeUint8(checksum, buffer);
    }
    return buffer;
}

canonical_original_code:
char *writePacket(struct Packet *p, char *buffer, int ascii) {
    char *buffPtr;
    if (!ascii) {
        buffer = writeUint8(header_1, buffer);
        buffer = writeUint8(header_2, buffer);
    }
    if (p->id == DummyPacketID) {
        buffPtr = buffer;
        buffer = writeDummyPacket(p, buffer, ascii);
    } else if (p->id == DumbPacketID) {
        buffPtr = buffer;
        buffer = writeDumbPacket(p, buffer, ascii);
    } else if (p->id == SlimStatePacketID) {
        buffPtr = buffer;
        buffer = writeSlimStatePacket(p, buffer, ascii);
    }
    if (!ascii) {
        unsigned char checksum = computeChecksum(buffer, buffer - buffPtr);
        buffer = writeUint8(checksum, buffer);
    }
    return buffer;
}

variable_types:
buffPtr
char *

user_defined_types: struct DummyPacket { unsigned char field_1; unsigned short field_2; char field_3; char field_4; float field_5; unsigned char field_6; unsigned int field_7; int field_8; }

user_defined_types: struct DumbPacket { unsigned char stupid; }

user_defined_types: struct SlimStatePacket { unsigned short leftEncoder; unsigned short rightEncoder; }

user_defined_types: struct InitPacket { unsigned char init; }

user_defined_types: struct SpeedPacket { short leftTick; short rightTick; }


sendToUart

decompiled_code:
int __fastcall sendToUart(int fd, void *buf, int a3, __useconds_t a4)
{
char *v4; // r13
char *v6; // rbx
int result; // eax

if ( a3 > 0 )
{
v4 = (char *)buf + (unsigned int)(a3 - 1) + 1;
v6 = (char *)buf;
do
{
write(fd, v6++, 1uLL);
usleep(0xC8u);
result = usleep(a4);
}
while ( v6 != v4 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(int fd, void *buf, int a3, __useconds_t a4)
{
char *v4; // r13
char *v6; // rbx
int result; // eax

if ( a3 > 0 )
{
v4 = (char *)buf + (unsigned int)(a3 - 1) + 1;
v6 = (char *)buf;
do
{
write(fd, v6++, 1uLL);
usleep(0xC8u);
result = usleep(a4);
}
while ( v6 != v4 );
}
return result;
}

original_code:
void sendToUart(int device, char* buffer, int length, int intraCharDelayUs) {
    int i = 0;
    while (i < length) {
        write(device, &buffer[i], 1);
        i++;
        usleep(200);
        usleep(intraCharDelayUs);
    }
}

canonical_original_code:
void sendToUart(int device, char *buffer, int length, int intraCharDelayUs) {
    int i = 0;
    while (i < length) {
        write(device, &buffer[i], 1);
        i++;
        usleep(200);
        usleep(intraCharDelayUs);
    }
}

variable_types: device int

variable_types: length int

variable_types: intraCharDelayUs int

variable_types: i int


readDummyPacket

original_code:
char* readDummyPacket(struct Packet* p, char* buffer, int ascii) {
  if (!ascii) {
    buffer = readUint32(&(p->seq), buffer);
    buffer = readUint8(&(p->dummy.field_1), buffer);
    buffer = readUint16(&(p->dummy.field_2), buffer);
    buffer = readUint8((uint8_t*)&(p->dummy.field_3), buffer);
    buffer = readUint8((uint8_t*)&(p->dummy.field_4), buffer);
    buffer = readFloat(&(p->dummy.field_5), buffer);
    buffer = readUint8(&(p->dummy.field_6), buffer);
    buffer = readUint32(&(p->dummy.field_7), buffer);
    buffer = readUint32((uint32_t*)&(p->dummy.field_8), buffer);
  } else {
    long int a, b, c, d, e, g, h, i = 0;
    float f = 0;
    sscanf(buffer, "%ld %ld %ld %ld %ld %f %d %ld %ld",
    &a,
    &b,
    &c,
    &d,
    &e,
    &f,
    &g,
    &h,
    &i);
    p->seq = (uint32_t) a;
    p->dummy.field_1 = (uint8_t) b;
    p->dummy.field_2 = (uint16_t) c;
    p->dummy.field_3 = (int8_t) d;
    p->dummy.field_4 = (char) e;
    p->dummy.field_5 = (float) f;
    p->dummy.field_6 = (unsigned char) g;
    p->dummy.field_7 = (uint32_t) h;
    p->dummy.field_8 = (int32_t) i;
  }
  return buffer;
}

canonical_original_code:
char *readDummyPacket(struct Packet *p, char *buffer, int ascii) {
  if (!ascii) {
    buffer = readUint32(&(p->seq), buffer);
    buffer = readUint8(&(p->dummy.field_1), buffer);
    buffer = readUint16(&(p->dummy.field_2), buffer);
    buffer = readUint8((unsigned char *)&(p->dummy.field_3), buffer);
    buffer = readUint8((unsigned char *)&(p->dummy.field_4), buffer);
    buffer = readFloat(&(p->dummy.field_5), buffer);
    buffer = readUint8(&(p->dummy.field_6), buffer);
    buffer = readUint32(&(p->dummy.field_7), buffer);
    buffer = readUint32((unsigned int *)&(p->dummy.field_8), buffer);
  } else {
    long a; long b; long c; long d; long e; long g; long h; long i = 0;
    float f = 0;
    sscanf(buffer, "%ld %ld %ld %ld %ld %f %d %ld %ld",
    &a,
    &b,
    &c,
    &d,
    &e,
    &f,
    &g,
    &h,
    &i);
    p->seq = (unsigned int) a;
    p->dummy.field_1 = (unsigned char) b;
    p->dummy.field_2 = (unsigned short) c;
    p->dummy.field_3 = (char) d;
    p->dummy.field_4 = (char) e;
    p->dummy.field_5 = (float) f;
    p->dummy.field_6 = (unsigned char) g;
    p->dummy.field_7 = (unsigned int) h;
    p->dummy.field_8 = (int) i;
  }
  return buffer;
}

variable_types: ascii int

variable_types: a long

variable_types: b long

variable_types: c long

variable_types: d long

variable_types: e long

variable_types: g long

variable_types: h long

variable_types: i long

variable_types: f float


writeDummyPacket

decompiled_code:
__int64 __fastcall writeDummyPacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 result; // rax
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rax
__int64 v14; // rax
__int64 v15; // rax
__int64 v16; // rax
__int64 v17; // rax
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // rax
__int64 v21; // rax

if ( a3 )
{
v11 = writeHeaderAscii(a2);
v12 = writeUint8Ascii(DummyPacketID, v11);
v13 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v12);
v14 = writeUint8Ascii(*(unsigned __int8 *)(a1 + 8), v13);
v15 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 10), v14);
v16 = writeUint8Ascii(*(unsigned __int8 *)(a1 + 12), v15);
v17 = writeCharAscii((unsigned int)*(char *)(a1 + 13), v16);
v18 = writeFloatAscii(v17, *(float *)(a1 + 16));
v19 = writeCharAscii((unsigned int)*(char *)(a1 + 20), v18);
v20 = writeUint32Ascii(*(unsigned int *)(a1 + 24), v19);
v21 = writeUint32Ascii(*(unsigned int *)(a1 + 28), v20);
result = writeFooterAscii(v21);
}
else
{
writeUint8(23LL);
v3 = writeUint8(DummyPacketID);
writeUint32(*(unsigned int *)(a1 + 4), v3);
v4 = writeUint8(*(unsigned __int8 *)(a1 + 8));
writeUint16(*(unsigned __int16 *)(a1 + 10), v4);
v5 = writeUint8(*(unsigned __int8 *)(a1 + 12));
v6 = writeChar((unsigned int)*(char *)(a1 + 13), v5);
v7 = writeFloat(v6, *(float *)(a1 + 16));
v8 = writeChar((unsigned int)*(char *)(a1 + 20), v7);
v9 = writeUint32(*(unsigned int *)(a1 + 24), v8);
result = writeUint32(*(unsigned int *)(a1 + 28), v9);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 result; // rax
__int64 v11; // rax
__int64 v12; // rax
__int64 v13; // rax
__int64 v14; // rax
__int64 v15; // rax
__int64 v16; // rax
__int64 v17; // rax
__int64 v18; // rax
__int64 v19; // rax
__int64 v20; // rax
__int64 v21; // rax

if ( a3 )
{
v11 = writeHeaderAscii(a2);
v12 = writeUint8Ascii(DummyPacketID, v11);
v13 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v12);
v14 = writeUint8Ascii(*(unsigned __int8 *)(a1 + 8), v13);
v15 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 10), v14);
v16 = writeUint8Ascii(*(unsigned __int8 *)(a1 + 12), v15);
v17 = writeCharAscii((unsigned int)*(char *)(a1 + 13), v16);
v18 = writeFloatAscii(v17, *(float *)(a1 + 16));
v19 = writeCharAscii((unsigned int)*(char *)(a1 + 20), v18);
v20 = writeUint32Ascii(*(unsigned int *)(a1 + 24), v19);
v21 = writeUint32Ascii(*(unsigned int *)(a1 + 28), v20);
result = writeFooterAscii(v21);
}
else
{
writeUint8(23LL);
v3 = writeUint8(DummyPacketID);
writeUint32(*(unsigned int *)(a1 + 4), v3);
v4 = writeUint8(*(unsigned __int8 *)(a1 + 8));
writeUint16(*(unsigned __int16 *)(a1 + 10), v4);
v5 = writeUint8(*(unsigned __int8 *)(a1 + 12));
v6 = writeChar((unsigned int)*(char *)(a1 + 13), v5);
v7 = writeFloat(v6, *(float *)(a1 + 16));
v8 = writeChar((unsigned int)*(char *)(a1 + 20), v7);
v9 = writeUint32(*(unsigned int *)(a1 + 24), v8);
result = writeUint32(*(unsigned int *)(a1 + 28), v9);
}
return result;
}

original_code:
char* writeDummyPacket(const struct Packet* p, char* buffer, int ascii) {
  if (!ascii) {
    uint8_t lenght = 23;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(DummyPacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint8(p->dummy.field_1, buffer);
    buffer = writeUint16(p->dummy.field_2, buffer);
    buffer = writeUint8(p->dummy.field_3, buffer);
    buffer = writeChar(p->dummy.field_4, buffer);
    buffer = writeFloat(p->dummy.field_5, buffer);
    buffer = writeChar(p->dummy.field_6, buffer);
    buffer = writeUint32(p->dummy.field_7, buffer);
    buffer = writeUint32(p->dummy.field_8, buffer);
  }
  else {
    buffer = writeHeaderAscii(buffer);
    buffer = writeUint8Ascii(DummyPacketID, buffer);
    buffer = writeUint32Ascii(p->seq, buffer);
    buffer = writeUint8Ascii(p->dummy.field_1, buffer);
    buffer = writeUint16Ascii(p->dummy.field_2, buffer);
    buffer = writeUint8Ascii(p->dummy.field_3, buffer);
    buffer = writeCharAscii(p->dummy.field_4, buffer);
    buffer = writeFloatAscii(p->dummy.field_5, buffer);
    buffer = writeCharAscii((char) p->dummy.field_6, buffer);
    buffer = writeUint32Ascii(p->dummy.field_7, buffer);
    buffer = writeUint32Ascii(p->dummy.field_8, buffer);
    buffer = writeFooterAscii(buffer);
  }
  return buffer;
}

canonical_original_code:
char *writeDummyPacket(struct Packet *p, char *buffer, int ascii) {
  if (!ascii) {
    unsigned char lenght = 23;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(DummyPacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint8(p->dummy.field_1, buffer);
    buffer = writeUint16(p->dummy.field_2, buffer);
    buffer = writeUint8(p->dummy.field_3, buffer);
    buffer = writeChar(p->dummy.field_4, buffer);
    buffer = writeFloat(p->dummy.field_5, buffer);
    buffer = writeChar(p->dummy.field_6, buffer);
    buffer = writeUint32(p->dummy.field_7, buffer);
    buffer = writeUint32(p->dummy.field_8, buffer);
  }
  else {
    buffer = writeHeaderAscii(buffer);
    buffer = writeUint8Ascii(DummyPacketID, buffer);
    buffer = writeUint32Ascii(p->seq, buffer);
    buffer = writeUint8Ascii(p->dummy.field_1, buffer);
    buffer = writeUint16Ascii(p->dummy.field_2, buffer);
    buffer = writeUint8Ascii(p->dummy.field_3, buffer);
    buffer = writeCharAscii(p->dummy.field_4, buffer);
    buffer = writeFloatAscii(p->dummy.field_5, buffer);
    buffer = writeCharAscii((char) p->dummy.field_6, buffer);
    buffer = writeUint32Ascii(p->dummy.field_7, buffer);
    buffer = writeUint32Ascii(p->dummy.field_8, buffer);
    buffer = writeFooterAscii(buffer);
  }
  return buffer;
}

variable_types: struct Packet { unsigned char id; unsigned char lenght; unsigned int seq; union { struct DummyPacket { unsigned char field_1; unsigned short field_2; char field_3; char field_4; float field_5; unsigned char field_6; unsigned int field_7; int field_8; } dummy; struct DumbPacket { unsigned char stupid; } dumb; struct SlimStatePacket { unsigned short leftEncoder; unsigned short rightEncoder; } slimState; struct InitPacket { unsigned char init; } init; struct SpeedPacket { short leftTick; short rightTick; } speed; }; } *

variable_types:
buffer
char *

variable_types:
lenght
unsigned char


writeHeaderAscii

decompiled_code:
char *__fastcall writeHeaderAscii(char *a1)
{
sprintf(a1, "%c ", (unsigned int)AsciiHeader);
return a1 + 2;
}

canonical_decompiled_code:
char *__fastcall func1(char *a1)
{
sprintf(a1, "%c ", (unsigned int)AsciiHeader);
return a1 + 2;
}

original_code:
char* writeHeaderAscii(char* buffer) {
    return buffer + sprintf(buffer, "%c ", AsciiHeader);
}

canonical_original_code:
char *writeHeaderAscii(char *buffer) {
    return buffer + sprintf(buffer, "%c ", AsciiHeader);
}


writeFooterAscii

decompiled_code:
char *__fastcall writeFooterAscii(char *a1)
{
sprintf(a1, "%c ", (unsigned int)AsciiFooter);
return a1 + 2;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1)
{
sprintf(a1, "%c ", (unsigned int)AsciiFooter);
return a1 + 2;
}

original_code:
char* writeFooterAscii(char* buffer) {
    return buffer + sprintf(buffer, "%c ", AsciiFooter);
}

canonical_original_code:
char *writeFooterAscii(char *buffer) {
    return buffer + sprintf(buffer, "%c ", AsciiFooter);
}


writeCharAscii

decompiled_code:
char *__fastcall writeCharAscii(char a1, char *a2)
{
return &a2[sprintf(a2, "%u ", (unsigned int)a1)];
}

canonical_decompiled_code:
char *__fastcall func3(char a1, char *a2)
{
return &a2[sprintf(a2, "%u ", (unsigned int)a1)];
}

original_code:
char* writeCharAscii(char value, char* buffer) {
    return buffer + sprintf(buffer, "%u ", value);
}

canonical_original_code:
char *writeCharAscii(char value, char *buffer) {
    return buffer + sprintf(buffer, "%u ", value);
}

variable_types: value char


decompiled_code:
char *__fastcall writeUint8Ascii(unsigned __int8 a1, char *a2)
{
return &a2[sprintf(a2, "%u ", a1)];
}

canonical_decompiled_code:
char *__fastcall func4(unsigned __int8 a1, char *a2)
{
return &a2[sprintf(a2, "%u ", a1)];
}

original_code:
char* writeUint8Ascii(uint8_t value, char* buffer) {
    return buffer + sprintf(buffer, "%u ", value);
}

canonical_original_code:
char *writeUint8Ascii(unsigned char value, char *buffer) {
    return buffer + sprintf(buffer, "%u ", value);
}

variable_types:
value
unsigned char


decompiled_code:
char *__fastcall writeUint16Ascii(unsigned __int16 a1, char *a2)
{
return &a2[sprintf(a2, "%u ", a1)];
}

canonical_decompiled_code:
char *__fastcall func5(unsigned __int16 a1, char *a2)
{
return &a2[sprintf(a2, "%u ", a1)];
}

original_code:
char* writeUint16Ascii(uint16_t value, char* buffer) {
    return buffer + sprintf(buffer, "%u ", value);
}

canonical_original_code:
char *writeUint16Ascii(unsigned short value, char *buffer) {
    return buffer + sprintf(buffer, "%u ", value);
}

variable_types:
value
unsigned short


decompiled_code:
char *__fastcall writeUint32Ascii(unsigned int a1, char *a2)
{
return &a2[sprintf(a2, "%lu ", a1)];
}

canonical_decompiled_code:
char *__fastcall func6(unsigned int a1, char *a2)
{
return &a2[sprintf(a2, "%lu ", a1)];
}

original_code:
char* writeUint32Ascii(uint32_t value, char* buffer) {
    return buffer + sprintf(buffer, "%lu ", value);
}

canonical_original_code:
char *writeUint32Ascii(unsigned int value, char *buffer) {
    return buffer + sprintf(buffer, "%lu ", value);
}

variable_types:
value
unsigned int


writeFloatAscii

decompiled_code:
char *__fastcall writeFloatAscii(char *a1, float a2)
{
return &a1[sprintf(a1, "%f ", a2)];
}

canonical_decompiled_code:
char *__fastcall func7(char *a1, float a2)
{
return &a1[sprintf(a1, "%f ", a2)];
}

original_code:
char* writeFloatAscii(float value, char* buffer) {
    return buffer + sprintf(buffer, "%f ", (double) value);
}

canonical_original_code:
char *writeFloatAscii(float value, char *buffer) {
    return buffer + sprintf(buffer, "%f ", (double) value);
}

variable_types: value float


decompiled_code:
char *__fastcall readUint8Ascii(_BYTE *a1, char *a2)
{
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
*a1 = strtoul(a2, &endptr, 10);
return endptr;
}

canonical_decompiled_code:
char *__fastcall func8(_BYTE *a1, char *a2)
{
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
*a1 = strtoul(a2, &endptr, 10);
return endptr;
}

original_code:
char* readUint8Ascii(char* dest, char* buffer) {
    *(uint8_t*) dest = (uint8_t) strtoul(buffer, &buffer, 10);
    return buffer;
}

canonical_original_code:
char *readUint8Ascii(char *dest, char *buffer) {
    *(unsigned char *) dest = (unsigned char) strtoul(buffer, &buffer, 10);
    return buffer;
}

variable_types:
dest
char *


decompiled_code:
char *__fastcall readUint16Ascii(_WORD *a1, char *a2)
{
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
*a1 = strtoul(a2, &endptr, 10);
return endptr;
}

canonical_decompiled_code:
char *__fastcall func9(_WORD *a1, char *a2)
{
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
*a1 = strtoul(a2, &endptr, 10);
return endptr;
}

original_code:
char* readUint16Ascii(char* dest, char* buffer) {
    *(uint16_t*) dest = strtoul(buffer, &buffer, 10);
    return buffer;
}

canonical_original_code:
char *readUint16Ascii(char *dest, char *buffer) {
    *(unsigned short *) dest = strtoul(buffer, &buffer, 10);
    return buffer;
}


decompiled_code:
char *__fastcall readUint32Ascii(_DWORD *a1, char *a2)
{
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
*a1 = strtoul(a2, &endptr, 10);
return endptr;
}

canonical_decompiled_code:
char *__fastcall func10(_DWORD *a1, char *a2)
{
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
*a1 = strtoul(a2, &endptr, 10);
return endptr;
}

original_code:
char* readUint32Ascii(char* dest, char* buffer) {
    *(uint32_t*) dest = strtoul(buffer, &buffer, 10);
    return buffer;
}

canonical_original_code:
char *readUint32Ascii(char *dest, char *buffer) {
    *(unsigned int *) dest = strtoul(buffer, &buffer, 10);
    return buffer;
}


readFloatAscii

decompiled_code:
char *__fastcall readFloatAscii(float *a1, char *a2)
{
signed __int64 v2; // rax
float v3; // xmm0_4
char *result; // rax
unsigned __int64 v5; // rdx
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
v2 = strtoul(a2, &endptr, 10);
if ( v2 < 0 )
{
v5 = v2 & 1 | ((unsigned __int64)v2 >> 1);
result = endptr;
v3 = (float)(int)v5 + (float)(int)v5;
}
else
{
v3 = (float)(int)v2;
result = endptr;
}
*a1 = v3;
return result;
}

canonical_decompiled_code:
char *__fastcall func11(float *a1, char *a2)
{
signed __int64 v2; // rax
float v3; // xmm0_4
char *result; // rax
unsigned __int64 v5; // rdx
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a2;
v2 = strtoul(a2, &endptr, 10);
if ( v2 < 0 )
{
v5 = v2 & 1 | ((unsigned __int64)v2 >> 1);
result = endptr;
v3 = (float)(int)v5 + (float)(int)v5;
}
else
{
v3 = (float)(int)v2;
result = endptr;
}
*a1 = v3;
return result;
}

original_code:
char* readFloatAscii(char* dest, char* buffer) {
    *(float*) dest = strtoul(buffer, &buffer, 10);
    return buffer;
}

canonical_original_code:
char *readFloatAscii(char *dest, char *buffer) {
    *(float *) dest = strtoul(buffer, &buffer, 10);
    return buffer;
}


writeSpeedPacket

decompiled_code:
__int64 __fastcall writeSpeedPacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rax

if ( a3 )
{
v7 = writeHeaderAscii(a2);
v8 = writeUint8Ascii(SpeedPacketID, v7);
v9 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v8);
v10 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 8), v9);
v11 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 10), v10);
result = writeFooterAscii(v11);
}
else
{
writeUint8(9LL);
v3 = writeUint8(SpeedPacketID);
v4 = writeUint32(*(unsigned int *)(a1 + 4), v3);
v5 = writeUint16(*(unsigned __int16 *)(a1 + 8), v4);
result = writeUint16(*(unsigned __int16 *)(a1 + 10), v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rax

if ( a3 )
{
v7 = writeHeaderAscii(a2);
v8 = writeUint8Ascii(SpeedPacketID, v7);
v9 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v8);
v10 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 8), v9);
v11 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 10), v10);
result = writeFooterAscii(v11);
}
else
{
writeUint8(9LL);
v3 = writeUint8(SpeedPacketID);
v4 = writeUint32(*(unsigned int *)(a1 + 4), v3);
v5 = writeUint16(*(unsigned __int16 *)(a1 + 8), v4);
result = writeUint16(*(unsigned __int16 *)(a1 + 10), v5);
}
return result;
}

original_code:
char* writeSpeedPacket(const struct Packet* p, char* buffer, int ascii) {
  if (!ascii) {
    uint8_t lenght = 9;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(SpeedPacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint16(p->speed.leftTick, buffer);
    buffer = writeUint16(p->speed.rightTick, buffer);
  }
    else {
        buffer = writeHeaderAscii(buffer);
        buffer = writeUint8Ascii(SpeedPacketID, buffer);
        buffer = writeUint32Ascii(p->seq, buffer);
        buffer = writeUint16Ascii(p->speed.leftTick, buffer);
        buffer = writeUint16Ascii(p->speed.rightTick, buffer);
        buffer = writeFooterAscii(buffer);
    }
    return buffer;
}

canonical_original_code:
char *writeSpeedPacket(struct Packet *p, char *buffer, int ascii) {
  if (!ascii) {
    unsigned char lenght = 9;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(SpeedPacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint16(p->speed.leftTick, buffer);
    buffer = writeUint16(p->speed.rightTick, buffer);
  }
    else {
        buffer = writeHeaderAscii(buffer);
        buffer = writeUint8Ascii(SpeedPacketID, buffer);
        buffer = writeUint32Ascii(p->seq, buffer);
        buffer = writeUint16Ascii(p->speed.leftTick, buffer);
        buffer = writeUint16Ascii(p->speed.rightTick, buffer);
        buffer = writeFooterAscii(buffer);
    }
    return buffer;
}

variable_types: ascii int


readSpeedPacket

decompiled_code:
__int64 __fastcall readSpeedPacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 result; // rax
__int64 v6; // [rsp+8h] [rbp-30h] BYREF
__int64 v7; // [rsp+10h] [rbp-28h] BYREF
__int64 v8; // [rsp+18h] [rbp-20h] BYREF

if ( a3 )
{
v8 = 0LL;
_isoc99_sscanf(a2, "%ld %ld %ld", &v6, &v7, &v8);
*(_DWORD *)(a1 + 4) = v6;
*(_WORD *)(a1 + 8) = v7;
*(_WORD *)(a1 + 10) = v8;
result = a2;
}
else
{
v3 = readUint32(a1 + 4);
v4 = readUint16(a1 + 8, v3);
result = readUint16(a1 + 10, v4);
}
return result;
}

original_code:
char* readSpeedPacket(struct Packet* p, char* buffer, int ascii) {
    if (!ascii) {
        buffer = readUint32(&(p->seq), buffer);
        buffer = readUint16((uint16_t*)&(p->speed.leftTick), buffer);
        buffer = readUint16((uint16_t*)&(p->speed.rightTick), buffer);
    } else {
        long int a, b, c = 0;
        sscanf(buffer, "%ld %ld %ld", &a, &b, &c);
        p->seq = (uint32_t) a;
        p->speed.leftTick = (uint16_t) b;
        p->speed.rightTick = (uint16_t) c;
        return buffer;
    }
}

canonical_original_code:
char *readSpeedPacket(struct Packet *p, char *buffer, int ascii) {
    if (!ascii) {
        buffer = readUint32(&(p->seq), buffer);
        buffer = readUint16((unsigned short *)&(p->speed.leftTick), buffer);
        buffer = readUint16((unsigned short *)&(p->speed.rightTick), buffer);
    } else {
        long a; long b; long c = 0;
        sscanf(buffer, "%ld %ld %ld", &a, &b, &c);
        p->seq = (unsigned int) a;
        p->speed.leftTick = (unsigned short) b;
        p->speed.rightTick = (unsigned short) c;
        return buffer;
    }
}

variable_types: a long

variable_types: b long

variable_types: c long


writeSlimStatePacket

decompiled_code:
__int64 __fastcall writeSlimStatePacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rax

if ( a3 )
{
v7 = writeHeaderAscii(a2);
v8 = writeUint8Ascii(SlimStatePacketID, v7);
v9 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v8);
v10 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 8), v9);
v11 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 10), v10);
result = writeFooterAscii(v11);
}
else
{
writeUint8(9LL);
v3 = writeUint8(SlimStatePacketID);
v4 = writeUint32(*(unsigned int *)(a1 + 4), v3);
v5 = writeUint16(*(unsigned __int16 *)(a1 + 8), v4);
result = writeUint16(*(unsigned __int16 *)(a1 + 10), v5);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
__int64 result; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rax

if ( a3 )
{
v7 = writeHeaderAscii(a2);
v8 = writeUint8Ascii(SlimStatePacketID, v7);
v9 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v8);
v10 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 8), v9);
v11 = writeUint16Ascii(*(unsigned __int16 *)(a1 + 10), v10);
result = writeFooterAscii(v11);
}
else
{
writeUint8(9LL);
v3 = writeUint8(SlimStatePacketID);
v4 = writeUint32(*(unsigned int *)(a1 + 4), v3);
v5 = writeUint16(*(unsigned __int16 *)(a1 + 8), v4);
result = writeUint16(*(unsigned __int16 *)(a1 + 10), v5);
}
return result;
}

original_code:
char* writeSlimStatePacket(const struct Packet* p, char* buffer, int ascii) {
  if (!ascii) {
    uint8_t lenght = 9;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(SlimStatePacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint16(p->slimState.leftEncoder, buffer);
    buffer = writeUint16(p->slimState.rightEncoder, buffer);
  }
    else {
        buffer = writeHeaderAscii(buffer);
        buffer = writeUint8Ascii(SlimStatePacketID, buffer);
        buffer = writeUint32Ascii(p->seq, buffer);
        buffer = writeUint16Ascii(p->slimState.leftEncoder, buffer);
        buffer = writeUint16Ascii(p->slimState.rightEncoder, buffer);
        buffer = writeFooterAscii(buffer);
    }
    return buffer;
}

canonical_original_code:
char *writeSlimStatePacket(struct Packet *p, char *buffer, int ascii) {
  if (!ascii) {
    unsigned char lenght = 9;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(SlimStatePacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint16(p->slimState.leftEncoder, buffer);
    buffer = writeUint16(p->slimState.rightEncoder, buffer);
  }
    else {
        buffer = writeHeaderAscii(buffer);
        buffer = writeUint8Ascii(SlimStatePacketID, buffer);
        buffer = writeUint32Ascii(p->seq, buffer);
        buffer = writeUint16Ascii(p->slimState.leftEncoder, buffer);
        buffer = writeUint16Ascii(p->slimState.rightEncoder, buffer);
        buffer = writeFooterAscii(buffer);
    }
    return buffer;
}


readSlimStatePacket

decompiled_code:
__int64 __fastcall readSlimStatePacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 result; // rax
__int64 v6; // [rsp+8h] [rbp-30h] BYREF
__int64 v7; // [rsp+10h] [rbp-28h] BYREF
__int64 v8; // [rsp+18h] [rbp-20h] BYREF

if ( a3 )
{
v8 = 0LL;
_isoc99_sscanf(a2, "%ld %ld %ld", &v6, &v7, &v8);
*(_DWORD *)(a1 + 4) = v6;
*(_WORD *)(a1 + 8) = v7;
*(_WORD *)(a1 + 10) = v8;
result = a2;
}
else
{
v3 = readUint32(a1 + 4);
v4 = readUint16(a1 + 8, v3);
result = readUint16(a1 + 10, v4);
}
return result;
}

original_code:
char* readSlimStatePacket(struct Packet* p, char* buffer, int ascii) {
    if (!ascii) {
        buffer = readUint32(&(p->seq), buffer);
        buffer = readUint16((uint16_t*)&(p->slimState.leftEncoder), buffer);
        buffer = readUint16((uint16_t*)&(p->slimState.rightEncoder), buffer);
    } else {
        long int a, b, c = 0;
        sscanf(buffer, "%ld %ld %ld", &a, &b, &c);
        p->seq = (uint32_t) a;
        p->slimState.leftEncoder = (uint16_t) b;
        p->slimState.rightEncoder = (uint16_t) c;
        return buffer;
    }
}

canonical_original_code:
char *readSlimStatePacket(struct Packet *p, char *buffer, int ascii) {
    if (!ascii) {
        buffer = readUint32(&(p->seq), buffer);
        buffer = readUint16((unsigned short *)&(p->slimState.leftEncoder), buffer);
        buffer = readUint16((unsigned short *)&(p->slimState.rightEncoder), buffer);
    } else {
        long a; long b; long c = 0;
        sscanf(buffer, "%ld %ld %ld", &a, &b, &c);
        p->seq = (unsigned int) a;
        p->slimState.leftEncoder = (unsigned short) b;
        p->slimState.rightEncoder = (unsigned short) c;
        return buffer;
    }
}


writeDumbPacket

decompiled_code:
__int64 __fastcall writeDumbPacket(__int64 a1, __int64 a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return a2;
}

original_code:
char* writeDumbPacket(const struct Packet* p, char* buffer, int ascii) {
  if (!ascii) {
  } else {
  }
  return buffer;
}

canonical_original_code:
char *writeDumbPacket(struct Packet *p, char *buffer, int ascii) {
  if (!ascii) {
  } else {
  }
  return buffer;
}


readDumbPacket

decompiled_code:
// attributes: thunk
__int64 __fastcall readDumbPacket(__int64 a1, __int64 a2)
{
return writeDumbPacket(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
return func0(a1, a2);
}

original_code:
char* readDumbPacket(struct Packet* p, char* buffer, int ascii) {
  return buffer;
}

canonical_original_code:
char *readDumbPacket(struct Packet *p, char *buffer, int ascii) {
  return buffer;
}


writeInitPacket

decompiled_code:
__int64 __fastcall writeInitPacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 result; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax

if ( a3 )
{
v6 = writeHeaderAscii(a2);
v7 = writeUint8Ascii(InitPacketID, v6);
v8 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v7);
v9 = writeUint16Ascii(*(unsigned __int8 *)(a1 + 8), v8);
result = writeFooterAscii(v9);
}
else
{
writeUint8(7LL);
v3 = writeUint8(InitPacketID);
v4 = writeUint32(*(unsigned int *)(a1 + 4), v3);
result = writeUint16(*(unsigned __int8 *)(a1 + 8), v4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 result; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
__int64 v9; // rax

if ( a3 )
{
v6 = writeHeaderAscii(a2);
v7 = writeUint8Ascii(InitPacketID, v6);
v8 = writeUint32Ascii(*(unsigned int *)(a1 + 4), v7);
v9 = writeUint16Ascii(*(unsigned __int8 *)(a1 + 8), v8);
result = writeFooterAscii(v9);
}
else
{
writeUint8(7LL);
v3 = writeUint8(InitPacketID);
v4 = writeUint32(*(unsigned int *)(a1 + 4), v3);
result = writeUint16(*(unsigned __int8 *)(a1 + 8), v4);
}
return result;
}

original_code:
char* writeInitPacket(const struct Packet* p, char* buffer, int ascii) {
  if (!ascii) {
    uint8_t lenght = 7;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(InitPacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint16(p->init.init, buffer);
  }
    else {
        buffer = writeHeaderAscii(buffer);
        buffer = writeUint8Ascii(InitPacketID, buffer);
        buffer = writeUint32Ascii(p->seq, buffer);
        buffer = writeUint16Ascii(p->init.init, buffer);
        buffer = writeFooterAscii(buffer);
    }
    return buffer;
}

canonical_original_code:
char *writeInitPacket(struct Packet *p, char *buffer, int ascii) {
  if (!ascii) {
    unsigned char lenght = 7;
    buffer = writeUint8(lenght, buffer);
    buffer = writeUint8(InitPacketID, buffer);
    buffer = writeUint32(p->seq, buffer);
    buffer = writeUint16(p->init.init, buffer);
  }
    else {
        buffer = writeHeaderAscii(buffer);
        buffer = writeUint8Ascii(InitPacketID, buffer);
        buffer = writeUint32Ascii(p->seq, buffer);
        buffer = writeUint16Ascii(p->init.init, buffer);
        buffer = writeFooterAscii(buffer);
    }
    return buffer;
}


readInitPacket

decompiled_code:
__int64 __fastcall readInitPacket(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 result; // rax
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
__int64 v6; // [rsp+8h] [rbp-20h] BYREF

if ( a3 )
{
v6 = 0LL;
_isoc99_sscanf(a2, "%ld %ld", &v5, &v6);
*(_DWORD *)(a1 + 4) = v5;
*(_BYTE *)(a1 + 8) = v6;
result = a2;
}
else
{
v3 = readUint32(a1 + 4);
result = readUint8(a1 + 8, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rax
__int64 result; // rax
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
__int64 v6; // [rsp+8h] [rbp-20h] BYREF

if ( a3 )
{
v6 = 0LL;
_isoc99_sscanf(a2, "%ld %ld", &v5, &v6);
*(_DWORD *)(a1 + 4) = v5;
*(_BYTE *)(a1 + 8) = v6;
result = a2;
}
else
{
v3 = readUint32(a1 + 4);
result = readUint8(a1 + 8, v3);
}
return result;
}

original_code:
char* readInitPacket(struct Packet* p, char* buffer, int ascii) {
    if (!ascii) {
        buffer = readUint32(&(p->seq), buffer);
        buffer = readUint8(&(p->init.init), buffer);
    } else {
        long int a, b = 0;
        sscanf(buffer, "%ld %ld", &a, &b);
        p->seq = (uint32_t) a;
        p->init.init = (uint8_t) b;
        return buffer;
    }
}

canonical_original_code:
char *readInitPacket(struct Packet *p, char *buffer, int ascii) {
    if (!ascii) {
        buffer = readUint32(&(p->seq), buffer);
        buffer = readUint8(&(p->init.init), buffer);
    } else {
        long a; long b = 0;
        sscanf(buffer, "%ld %ld", &a, &b);
        p->seq = (unsigned int) a;
        p->init.init = (unsigned char) b;
        return buffer;
    }
}


main

repo: mauro-belgiovine/cpu-get-stat

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v5; // eax
unsigned int v6; // er12
FILE *v7; // rbp
int result; // eax
__int64 i; // r13
const char *v10; // rsi
int v11; // ebx
unsigned int v12; // eax
__int64 v13; // rcx
char *v14; // rsi
_DWORD *v15; // rdi
_DWORD *v16; // rdi
__int64 v17; // rcx
char *v18; // rsi
float v19; // xmm0_4
char *v20; // rsi
_DWORD *v21; // rdi
__int64 j; // rcx
char *v23; // rsi
_DWORD *v24; // rdi
__int64 k; // rcx
_BYTE v26[4416]; // [rsp-2310h] [rbp-4678h] BYREF
_BYTE v27[4416]; // [rsp-11D0h] [rbp-3538h] BYREF
_BYTE v28[72]; // [rsp-90h] [rbp-23F8h] BYREF
_BYTE v29[80]; // [rsp-48h] [rbp-23B0h] BYREF
time_t timer; // [rsp+8h] [rbp-2360h] BYREF
char v31[80]; // [rsp+10h] [rbp-2358h] BYREF
char v32[80]; // [rsp+60h] [rbp-2308h] BYREF
char v33[4416]; // [rsp+B0h] [rbp-22B8h] BYREF
char v34[4472]; // [rsp+11F0h] [rbp-1178h] BYREF

v3 = -1;
opterr = 0;
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "c:");
if ( v5 == -1 )
{
for ( i = optind; argc > (int)i; ++i )
{
v10 = argv[i];
printf("Non-option argument %s\n", v10);
}
v11 = 1000000000;
get_pid_stat_monitor(v33);
get_cpu_stat_monitor(v31, v3);
puts("Start working stuff... ");
v12 = time(&timer);
srand(v12);
do
{
rand();
--v11;
}
while ( v11 );
putchar(10);
get_pid_stat_monitor(v34);
get_cpu_stat_monitor(v32, v3);
v13 = 18LL;
v14 = v32;
v15 = v29;
while ( v13 )
{
*v15 = *(_DWORD *)v14;
v14 += 4;
++v15;
--v13;
}
v16 = v28;
v17 = 18LL;
v18 = v31;
while ( v17 )
{
*v16 = *(_DWORD *)v18;
v18 += 4;
++v16;
--v17;
}
qmemcpy(v27, v34, sizeof(v27));
qmemcpy(v26, v33, sizeof(v26));
v19 = pid_cpu_usage_percent();
printf("total cpu usage of this process: %.2f %% \n", v19);
v20 = v32;
v21 = v29;
for ( j = 18LL; j; --j )
{
*v21 = *(_DWORD *)v20;
v20 += 4;
++v21;
}
v23 = v31;
v24 = v28;
for ( k = 18LL; k; --k )
{
*v24 = *(_DWORD *)v23;
v23 += 4;
++v24;
}
cpu_usage_stats(v24, v23);
return 0;
}
if ( v5 == 63 )
break;
if ( v5 != 99 )
{
puts("Aborting...");
abort();
}
v3 = atoi(optarg);
}
v6 = optopt;
v7 = stderr;
if ( optopt == 99 )
{
fprintf(stderr, "Option -%c requires an argument.\n", 99LL);
result = 1;
}
else
{
if ( isprint(optopt) )
fprintf(v7, "Unknown option `-%c'.\n", v6);
else
fprintf(v7, "Unknown option character `\\x%x'.\n", v6);
result = 1;
}
return result;
}

original_code:
int main( int argc, char *argv[] )
{
  int i, n, x;
  time_t t;
  int index;
  int c;
  int cpu_number = -1;
  opterr = 0;
  while ((c = getopt (argc, argv, "c:")) != -1)
    switch (c)
    {
      case 'c':
        cpu_number = atoi(optarg);
        break;
      case '?':
        if (optopt == 'c')
          fprintf (stderr, "Option -%c requires an argument.\n", optopt);
        else if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,"Unknown option character `\\x%x'.\n", optopt);
        return 1;
      default:
        printf("Aborting...\n");
        abort ();
    }
  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);
  pid_stat_fields pid_start, pid_stop;
  cpu_stat_fields cpu_start, cpu_stop;
  pid_start = get_pid_stat_monitor();
  cpu_start = get_cpu_stat_monitor(cpu_number);
  printf("Start working stuff... \n");
  n = 1000000000;
  srand((unsigned) time(&t));
  for( i = 0 ; i < n ; i++ ) {
      x = rand() % 50;
      x = x*x;
  }
  printf("\n");
  pid_stop = get_pid_stat_monitor();
  cpu_stop = get_cpu_stat_monitor(cpu_number);
  float percent_usage = pid_cpu_usage_percent(pid_start, pid_stop, cpu_start, cpu_stop);
  printf("total cpu usage of this process: %.2f %% \n", percent_usage);
  cpu_usage_stats(cpu_start, cpu_stop);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int i; int n; int x;
  long t;
  int index;
  int c;
  int cpu_number = -1;
  opterr = 0;
  while ((c = getopt (argc, argv, "c:")) != -1)
    switch (c)
    {
      case 'c':
        cpu_number = atoi(optarg);
        break;
      case '?':
        if (optopt == 'c')
          fprintf (stderr, "Option -%c requires an argument.\n", optopt);
        else if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,"Unknown option character `\\x%x'.\n", optopt);
        return 1;
      default:
        printf("Aborting...\n");
        abort ();
    }
  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);
  struct pid_stat_fields pid_start; struct pid_stat_fields pid_stop;
  struct cpu_stat_fields cpu_start; struct cpu_stat_fields cpu_stop;
  pid_start = get_pid_stat_monitor();
  cpu_start = get_cpu_stat_monitor(cpu_number);
  printf("Start working stuff... \n");
  n = 1000000000;
  srand((unsigned int) time(&t));
  for( i = 0 ; i < n ; i++ ) {
      x = rand() % 50;
      x = x*x;
  }
  printf("\n");
  pid_stop = get_pid_stat_monitor();
  cpu_stop = get_cpu_stat_monitor(cpu_number);
  float percent_usage = pid_cpu_usage_percent(pid_start, pid_stop, cpu_start, cpu_stop);
  printf("total cpu usage of this process: %.2f %% \n", percent_usage);
  cpu_usage_stats(cpu_start, cpu_stop);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

variable_types: n int

variable_types: x int

variable_types: t long

variable_types: index int

variable_types: c int

variable_types: struct pid_stat_fields { long long pid; char tcomm[4096]; char state; long long ppid; long long pgid; long long sid; long long tty_nr; long long tty_pgrp; long long flags; long long min_flt; long long cmin_flt; long long maj_flt; long long cmaj_flt; long long utime; long long stime; long long cutime; long long cstime; long long priority; long long nicev; long long num_threads; long long it_real_value; unsigned long long start_time; long long vsize; long long rss; long long rsslim; long long start_code; long long end_code; long long start_stack; long long esp; long long eip; long long pending; long long blocked; long long sigign; long long sigcatch; long long wchan; long long zero1; long long zero2; long long exit_signal; long long cpu; long long rt_priority; long long policy; }

variable_types: struct cpu_stat_fields { char cpu_label[10]; long long user; long long nice; long long system; long long idle; long long iowait; long long irq; long long softirq; }


decompiled_code:
float __fastcall pid_cpu_usage_percent(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7)
{
float result; // xmm0_4
float v8; // xmm2_4

result = *(float *)&LC10;
if ( !strncmp((const char *)&STACK[0x2290], (const char *)&STACK[0x22D8], 4uLL) && a7 == STACK[0x1150] )
{
sysconf(2);
v8 = (float)(LODWORD(STACK[0x2318])
+ LODWORD(STACK[0x2310])
+ LODWORD(STACK[0x2308])
+ LODWORD(STACK[0x2300])
+ LODWORD(STACK[0x22F0])
+ LODWORD(STACK[0x22E8])
+ LODWORD(STACK[0x22F8])
- (LODWORD(STACK[0x22D0])
+ LODWORD(STACK[0x22C8])
+ LODWORD(STACK[0x22C0])
+ LODWORD(STACK[0x22B8])
+ LODWORD(STACK[0x22A8])
+ LODWORD(STACK[0x22A0])
+ LODWORD(STACK[0x22B0])));
result = (float)((float)((float)(LODWORD(STACK[0x21B8]) - LODWORD(STACK[0x1078])) * 100.0) / v8)
+ (float)((float)((float)(int)(STACK[0x21B0] - LODWORD(STACK[0x1070])) * 100.0) / v8);
}
return result;
}

canonical_decompiled_code:
float __fastcall func7(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7)
{
float result; // xmm0_4
float v8; // xmm2_4

result = *(float *)&LC10;
if ( !strncmp((const char *)&STACK[0x2290], (const char *)&STACK[0x22D8], 4uLL) && a7 == STACK[0x1150] )
{
sysconf(2);
v8 = (float)(LODWORD(STACK[0x2318])
+ LODWORD(STACK[0x2310])
+ LODWORD(STACK[0x2308])
+ LODWORD(STACK[0x2300])
+ LODWORD(STACK[0x22F0])
+ LODWORD(STACK[0x22E8])
+ LODWORD(STACK[0x22F8])
- (LODWORD(STACK[0x22D0])
+ LODWORD(STACK[0x22C8])
+ LODWORD(STACK[0x22C0])
+ LODWORD(STACK[0x22B8])
+ LODWORD(STACK[0x22A8])
+ LODWORD(STACK[0x22A0])
+ LODWORD(STACK[0x22B0])));
result = (float)((float)((float)(LODWORD(STACK[0x21B8]) - LODWORD(STACK[0x1078])) * 100.0) / v8)
+ (float)((float)((float)(int)(STACK[0x21B0] - LODWORD(STACK[0x1070])) * 100.0) / v8);
}
return result;
}

original_code:
float pid_cpu_usage_percent(pid_stat_fields pid_start, pid_stat_fields pid_stop,
       cpu_stat_fields cpu_start, cpu_stat_fields cpu_stop){
  if((0 != strncmp(&(cpu_start.cpu_label), &(cpu_stop.cpu_label), 4)) || (pid_start.pid != pid_stop.pid)) return -1;
  long tickspersec = sysconf(_SC_CLK_TCK);
  long time_total_start, time_total_stop;
  float user_util, sys_util;
  time_total_start = cpu_start.user + cpu_start.system + cpu_start.nice + cpu_start.idle + cpu_start.iowait + cpu_start.irq + cpu_start.softirq;
  time_total_stop = cpu_stop.user + cpu_stop.system + cpu_stop.nice + cpu_stop.idle + cpu_stop.iowait + cpu_stop.irq + cpu_stop.softirq;
  user_util = 100.0f * (pid_stop.utime - pid_start.utime) / (time_total_stop - time_total_start);
  sys_util = 100.0f * (pid_stop.stime - pid_start.stime) / (time_total_stop - time_total_start);
  return (user_util + sys_util);
}

canonical_original_code:
float pid_cpu_usage_percent(struct pid_stat_fields pid_start, struct pid_stat_fields pid_stop, struct cpu_stat_fields cpu_start, struct cpu_stat_fields cpu_stop){
  if((0 != strncmp(&(cpu_start.cpu_label), &(cpu_stop.cpu_label), 4)) || (pid_start.pid != pid_stop.pid)) return -1;
  long tickspersec = sysconf(_SC_CLK_TCK);
  long time_total_start; long time_total_stop;
  float user_util; float sys_util;
  time_total_start = cpu_start.user + cpu_start.system + cpu_start.nice + cpu_start.idle + cpu_start.iowait + cpu_start.irq + cpu_start.softirq;
  time_total_stop = cpu_stop.user + cpu_stop.system + cpu_stop.nice + cpu_stop.idle + cpu_stop.iowait + cpu_stop.irq + cpu_stop.softirq;
  user_util = 100.0f * (pid_stop.utime - pid_start.utime) / (time_total_stop - time_total_start);
  sys_util = 100.0f * (pid_stop.stime - pid_start.stime) / (time_total_stop - time_total_start);
  return (user_util + sys_util);
}

variable_types: tickspersec long

return_type: float

user_defined_types: struct pid_stat_fields { long long pid; char tcomm[4096]; char state; long long ppid; long long pgid; long long sid; long long tty_nr; long long tty_pgrp; long long flags; long long min_flt; long long cmin_flt; long long maj_flt; long long cmaj_flt; long long utime; long long stime; long long cutime; long long cstime; long long priority; long long nicev; long long num_threads; long long it_real_value; unsigned long long start_time; long long vsize; long long rss; long long rsslim; long long start_code; long long end_code; long long start_stack; long long esp; long long eip; long long pending; long long blocked; long long sigign; long long sigcatch; long long wchan; long long zero1; long long zero2; long long exit_signal; long long cpu; long long rt_priority; long long policy; }

user_defined_types: struct cpu_stat_fields { char cpu_label[10]; long long user; long long nice; long long system; long long idle; long long iowait; long long irq; long long softirq; }


original_code:
void print_graph_bar(float float_value){
  uint i;
  printf("[");
  int int_percent = (float_value >= 0.0f) ? (int) floor(float_value + 0.5) : (int) floor(float_value - 0.5);
  for(i = 0; i < int_percent; i++){
    printf("=");
  }
  printf("]\n");
}

canonical_original_code:
void print_graph_bar(float float_value){
  unsigned int i;
  printf("[");
  int int_percent = (float_value >= 0.0f) ? (int) floor(float_value + 0.5) : (int) floor(float_value - 0.5);
  for(i = 0; i < int_percent; i++){
    printf("=");
  }
  printf("]\n");
}

variable_types:
i
unsigned int


original_code:
void cpu_usage_stats(cpu_stat_fields cpu_start, cpu_stat_fields cpu_stop){
  long time_total_start, time_total_stop;
  float percent;
  if(0 != strncmp(&(cpu_start.cpu_label), &(cpu_stop.cpu_label), 4))
    printf("Error: the two stats structures passed as argument are not from same cpu\n");
  else{
    time_total_start = cpu_start.user + cpu_start.system + cpu_start.nice + cpu_start.idle + cpu_start.iowait + cpu_start.irq + cpu_start.softirq;
    time_total_stop = cpu_stop.user + cpu_stop.system + cpu_stop.nice + cpu_stop.idle + cpu_stop.iowait + cpu_stop.irq + cpu_stop.softirq;
    printf("\nInfo about %s workload:\n", cpu_start.cpu_label);
    percent = 100.0f * (cpu_stop.user - cpu_start.user) / (time_total_stop - time_total_start);
    printf("- utime:\t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.system - cpu_start.system) / (time_total_stop - time_total_start);
    printf("- stime:\t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.nice - cpu_start.nice) / (time_total_stop - time_total_start);
    printf("- nice: \t%.2f %%\t", percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.idle - cpu_start.idle) / (time_total_stop - time_total_start);
    printf("- idle: \t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.iowait - cpu_start.iowait) / (time_total_stop - time_total_start);
    printf("- iowait:\t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.irq - cpu_start.irq) / (time_total_stop - time_total_start);
    printf("- irq:  \t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.softirq - cpu_start.softirq) / (time_total_stop - time_total_start);
    printf("- softirq:\t%.2f %%\t",percent);
    print_graph_bar(percent);
  }
}

canonical_original_code:
void cpu_usage_stats(struct cpu_stat_fields cpu_start, struct cpu_stat_fields cpu_stop){
  long time_total_start; long time_total_stop;
  float percent;
  if(0 != strncmp(&(cpu_start.cpu_label), &(cpu_stop.cpu_label), 4))
    printf("Error: the two stats structures passed as argument are not from same cpu\n");
  else{
    time_total_start = cpu_start.user + cpu_start.system + cpu_start.nice + cpu_start.idle + cpu_start.iowait + cpu_start.irq + cpu_start.softirq;
    time_total_stop = cpu_stop.user + cpu_stop.system + cpu_stop.nice + cpu_stop.idle + cpu_stop.iowait + cpu_stop.irq + cpu_stop.softirq;
    printf("\nInfo about %s workload:\n", cpu_start.cpu_label);
    percent = 100.0f * (cpu_stop.user - cpu_start.user) / (time_total_stop - time_total_start);
    printf("- utime:\t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.system - cpu_start.system) / (time_total_stop - time_total_start);
    printf("- stime:\t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.nice - cpu_start.nice) / (time_total_stop - time_total_start);
    printf("- nice: \t%.2f %%\t", percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.idle - cpu_start.idle) / (time_total_stop - time_total_start);
    printf("- idle: \t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.iowait - cpu_start.iowait) / (time_total_stop - time_total_start);
    printf("- iowait:\t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.irq - cpu_start.irq) / (time_total_stop - time_total_start);
    printf("- irq:  \t%.2f %%\t",percent);
    print_graph_bar(percent);
    percent = 100.0f * (cpu_stop.softirq - cpu_start.softirq) / (time_total_stop - time_total_start);
    printf("- softirq:\t%.2f %%\t",percent);
    print_graph_bar(percent);
  }
}

variable_types: percent float


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
__int64 v4; // r12
__int64 v5; // rbp
unsigned int v6; // eax
struct sembuf sops; // [rsp+Ah] [rbp-1Eh] BYREF

result = check_args(argc, argv, envp);
if ( result != -1 )
{
v4 = (unsigned int)ftok(argv[1], 0);
printf("key = [%d]\n", v4);
LODWORD(v5) = semget(v4, 1, 384);
if ( (_DWORD)v5 == -1 )
{
v5 = (unsigned int)semget(v4, 1, 896);
printf("sem_id = [%d]\n", v5);
semctl(v5, 0, 16, 4LL);
}
else
{
puts("Already exist");
*(_DWORD *)&sops.sem_num = -65536;
sops.sem_flg = 0;
semop(v5, &sops, 1uLL);
v6 = semctl(v5, 0, 12);
printf("value = %d\n", v6);
}
if ( semctl(v5, 0, 12) )
{
result = 0;
}
else
{
printf("All the players are here");
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
__int64 v4; // r12
__int64 v5; // rbp
unsigned int v6; // eax
struct sembuf sops; // [rsp+Ah] [rbp-1Eh] BYREF

result = func7(argc, argv, envp);
if ( result != -1 )
{
v4 = (unsigned int)ftok(argv[1], 0);
printf("key = [%d]\n", v4);
LODWORD(v5) = semget(v4, 1, 384);
if ( (_DWORD)v5 == -1 )
{
v5 = (unsigned int)semget(v4, 1, 896);
printf("sem_id = [%d]\n", v5);
semctl(v5, 0, 16, 4LL);
}
else
{
puts("Already exist");
*(_DWORD *)&sops.sem_num = -65536;
sops.sem_flg = 0;
semop(v5, &sops, 1uLL);
v6 = semctl(v5, 0, 12);
printf("value = %d\n", v6);
}
if ( semctl(v5, 0, 12) )
{
result = 0;
}
else
{
printf("All the players are here");
result = -1;
}
}
return result;
}

original_code:
int main(int ac, char **av)
{
  key_t key;
  int sem_id;
  struct sembuf sops;
  if (check_args(ac) == -1)
    return (-1);
  key = ftok(av[1], 0);
  printf("key = [%d]\n", key);
  sem_id = semget(key, 1, 0400 | 0200);
  if (sem_id == -1)
    {
      sem_id = semget(key, 1, 01000 | 0400 | 0200);
      printf("sem_id = [%d]\n", sem_id);
      semctl(sem_id, 0, 16, 4);
    }
  else
    {
      printf("Already exist\n");
      sops.sem_num = 0;
      sops.sem_op = -1;
      sops.sem_flg = 0;
      semop(sem_id, &sops, 1);
      printf("value = %d\n", semctl(sem_id, 0, 12));
    }
  if (semctl(sem_id, 0, 12) == 0)
    {
       printf("All the players are here");
       return (-1);
    }
  return (0);
}

canonical_original_code:
int main(int ac, char **av)
{
  int key;
  int sem_id;
  struct sembuf sops;
  if (check_args(ac) == -1)
    return (-1);
  key = ftok(av[1], 0);
  printf("key = [%d]\n", key);
  sem_id = semget(key, 1, 0400 | 0200);
  if (sem_id == -1)
    {
      sem_id = semget(key, 1, 01000 | 0400 | 0200);
      printf("sem_id = [%d]\n", sem_id);
      semctl(sem_id, 0, 16, 4);
    }
  else
    {
      printf("Already exist\n");
      sops.sem_num = 0;
      sops.sem_op = -1;
      sops.sem_flg = 0;
      semop(sem_id, &sops, 1);
      printf("value = %d\n", semctl(sem_id, 0, 12));
    }
  if (semctl(sem_id, 0, 12) == 0)
    {
       printf("All the players are here");
       return (-1);
    }
  return (0);
}

variable_types: ac int

variable_types:
av
char **

variable_types: key int

variable_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }


decompiled_code:
__int64 __fastcall check_args(int a1)
{
if ( a1 == 3 )
return 0LL;
puts("Usage : ./lemipc ...");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
if ( a1 == 3 )
return 0LL;
puts("Usage : ./lemipc ...");
return 0xFFFFFFFFLL;
}

original_code:
int check_args(int ac)
{
  if (ac != 3)
    {
      printf("Usage : ./lemipc ...\n");
      return (-1);
    }
  return (0);
}


repo: maxrosan/elf-interpreter

decompiled_code:
void __fastcall options_free(void **a1)
{
void *v1; // rdi

if ( !a1 )
_assert_fail("options != NULL", "options.c", 0xCu, "options_free");
v1 = *a1;
if ( v1 )
free(v1);
}

canonical_decompiled_code:
void __fastcall func1(void **a1)
{
void *v1; // rdi

if ( !a1 )
_assert_fail("options != NULL", "options.c", 0xCu, "options_free");
v1 = *a1;
if ( v1 )
free(v1);
}

original_code:
void options_free(Options *options) {
 ((void) sizeof ((options != ((void *)0)) ? 1 : 0), __extension__ ({ if (options != ((void *)0)) ; else __assert_fail ("options != NULL", "options.c", 12, __extension__ __PRETTY_FUNCTION__); }));
 if (options->filename) {
  free(options->filename);
 }
}

canonical_original_code:
void options_free(struct Options *options) {
 ((void) sizeof ((options != ((void *)0)) ? 1 : 0), __extension__ ({ if (options != ((void *)0)) ; else __assert_fail ("options != NULL", "options.c", 12, __extension__ __PRETTY_FUNCTION__); }));
 if (options->filename) {
  free(options->filename);
 }
}

variable_types:
options
struct Options { char *filename; } *


decompiled_code:
__int64 __fastcall options_parse(char **a1, int a2, char *const *a3)
{
unsigned int v4; // er13
int v5; // eax

if ( !a1 )
_assert_fail("options != NULL", "options.c", 0x17u, "options_parse");
v4 = 0;
while ( 1 )
{
v5 = getopt(a2, a3, "f:");
if ( v5 == -1 )
break;
if ( v5 == 102 )
{
*a1 = strdup(optarg);
}
else
{
v4 = -1;
fwrite("Unknow option", 1uLL, 0xDuLL, stderr);
}
}
if ( !*a1 )
{
v4 = -1;
fwrite("No file specified\n", 1uLL, 0x12uLL, stderr);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(char **a1, int a2, char *const *a3)
{
unsigned int v4; // er13
int v5; // eax

if ( !a1 )
_assert_fail("options != NULL", "options.c", 0x17u, "options_parse");
v4 = 0;
while ( 1 )
{
v5 = getopt(a2, a3, "f:");
if ( v5 == -1 )
break;
if ( v5 == 102 )
{
*a1 = strdup(optarg);
}
else
{
v4 = -1;
fwrite("Unknow option", 1uLL, 0xDuLL, stderr);
}
}
if ( !*a1 )
{
v4 = -1;
fwrite("No file specified\n", 1uLL, 0x12uLL, stderr);
}
return v4;
}

original_code:
int options_parse(Options *options, int argc, char **argv) {
 int opt, ret = 0;
 ((void) sizeof ((options != ((void *)0)) ? 1 : 0), __extension__ ({ if (options != ((void *)0)) ; else __assert_fail ("options != NULL", "options.c", 23, __extension__ __PRETTY_FUNCTION__); }));
 while ((opt = getopt(argc, argv, "f:")) != -1) {
  switch (opt) {
   case 'f':
    options->filename = strdup(optarg);
   break;
   default:
    fprintf(stderr, "Unknow option");
    ret = -1;
  }
 }
 if (options->filename == ((void *)0)) {
  fprintf(stderr, "No file specified\n");
  ret = -1;
 }
 return ret;
}

canonical_original_code:
int options_parse(struct Options *options, int argc, char **argv) {
 int opt; int ret = 0;
 ((void) sizeof ((options != ((void *)0)) ? 1 : 0), __extension__ ({ if (options != ((void *)0)) ; else __assert_fail ("options != NULL", "options.c", 23, __extension__ __PRETTY_FUNCTION__); }));
 while ((opt = getopt(argc, argv, "f:")) != -1) {
  switch (opt) {
   case 'f':
    options->filename = strdup(optarg);
   break;
   default:
    fprintf(stderr, "Unknow option");
    ret = -1;
  }
 }
 if (options->filename == ((void *)0)) {
  fprintf(stderr, "No file specified\n");
  ret = -1;
 }
 return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types: ret int


range

repo: maxtsai/PhotoRank

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

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 20) > a2 && a3 >= 0 )
result = *(_DWORD *)(a1 + 24) > a3;
return result;
}

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

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 20) > a2 && a3 >= 0 )
result = *(_DWORD *)(a1 + 24) > a3;
return result;
}

original_code:
int range(struct image_info * img, int x, int y)
{
 if ((x < 0) || (x >= img->width)) {
  return(0);
 }
 if ((y < 0) || (y >= img->height)) {
  return(0);
 }
 return(1);
}

canonical_original_code:
int range(struct image_info *img, int x, int y)
{
 if ((x < 0) || (x >= img->width)) {
  return(0);
 }
 if ((y < 0) || (y >= img->height)) {
  return(0);
 }
 return(1);
}

variable_types: struct image_info { unsigned char *raw; unsigned char *raw1; int color_space; int width; int height; int bytes_per_pixel; } *

variable_types: x int

variable_types: y int


trace

decompiled_code:
__int64 __fastcall trace(int a1, int a2, int a3, _QWORD *a4, __int64 a5)
{
_BYTE *v5; // rax
unsigned int v10; // er15
int i; // ebx
unsigned int v12; // er10

v5 = (_BYTE *)(*(_QWORD *)(a5 + 8) + a1 + a2 * *(_DWORD *)(a5 + 20));
if ( *v5 != 0xFF )
return 0;
*v5 = 0;
v10 = a2 - 1;
do
{
for ( i = -1; ; ++i )
{
if ( i | a2 )
{
if ( range((__int64)a4, a1 + i, v10)
&& *(unsigned __int8 *)(*a4 + (int)(i + a1 + v10 * *(_DWORD *)(a5 + 20))) >= a3
&& (unsigned int)trace(v12, v10, (unsigned int)a3, a4, a5) )
{
return 1;
}
if ( i == 1 )
break;
}
}
++v10;
}
while ( a2 + 2 != v10 );
return 1;
}

original_code:
int trace(int x, int y, int low, struct image_info * img_in, struct image_info * img_out)
{
 int y_off, x_off;
 if (img_out->raw1[y * img_out->width + x] == 0xFF)
 {
  img_out->raw1[y * img_out->width + x] = 0x00;
  for (y_off = -1; y_off <=1; y_off++)
  {
   for(x_off = -1; x_off <= 1; x_off++)
   {
    if (!(y == 0 && x_off == 0) &&
      range(img_in, x + x_off, y + y_off) &&
      img_in->raw[(y + y_off) * img_out->width + x + x_off] >= low) {
     if (trace(x + x_off, y + y_off, low, img_in, img_out))
     {
         return(1);
     }
    }
   }
  }
  return(1);
 }
 return(0);
}

canonical_original_code:
int trace(int x, int y, int low, struct image_info *img_in, struct image_info *img_out)
{
 int y_off; int x_off;
 if (img_out->raw1[y * img_out->width + x] == 0xFF)
 {
  img_out->raw1[y * img_out->width + x] = 0x00;
  for (y_off = -1; y_off <=1; y_off++)
  {
   for(x_off = -1; x_off <= 1; x_off++)
   {
    if (!(y == 0 && x_off == 0) &&
      range(img_in, x + x_off, y + y_off) &&
      img_in->raw[(y + y_off) * img_out->width + x + x_off] >= low) {
     if (trace(x + x_off, y + y_off, low, img_in, img_out))
     {
         return(1);
     }
    }
   }
  }
  return(1);
 }
 return(0);
}

variable_types: low int

user_defined_types: struct image_info { unsigned char *raw; unsigned char *raw1; int color_space; int width; int height; int bytes_per_pixel; }


hysteresis

decompiled_code:
__int64 __fastcall hysteresis(int a1, int a2, __int64 a3, __int64 a4)
{
int v6; // ecx
__int64 i; // rax
__int64 result; // rax
int v9; // edx
int v10; // er11
int j; // er9

v6 = *(_DWORD *)(a3 + 24) * *(_DWORD *)(a3 + 20);
if ( v6 > 0 )
{
for ( i = 0LL; i != v6; *(_BYTE *)(*(_QWORD *)(a4 + 8) + i++) = -1 )
;
}
result = *(unsigned int *)(a4 + 24);
if ( (int)result > 0 )
{
v9 = *(_DWORD *)(a4 + 20);
v10 = 0;
do
{
for ( j = 0; v9 > j; v9 = *(_DWORD *)(a4 + 20) )
{
while ( 1 )
{
result = *(unsigned __int8 *)(*(_QWORD *)a3 + j + v10 * v9);
if ( (int)result >= a1 )
break;
if ( v9 <= ++j )
goto LABEL_11;
}
result = trace(j, v10, a2, (_QWORD *)a3, a4);
}
LABEL_11:
++v10;
}
while ( *(_DWORD *)(a4 + 24) > v10 );
}
return result;
}

original_code:
void hysteresis(int high, int low, struct image_info * img_in, struct image_info * img_out)
{
 int x, y, n, max;
 max = img_in->width * img_in->height;
 for (n = 0; n < max; n++)
  img_out->raw1[n] = 0xFF;
 for (y=0; y < img_out->height; y++) {
  for (x=0; x < img_out->width; x++) {
   if (img_in->raw[y * img_out->width + x] >= high) {
    trace (x, y, low, img_in, img_out);
   }
  }
 }
}

canonical_original_code:
void hysteresis(int high, int low, struct image_info *img_in, struct image_info *img_out)
{
 int x; int y; int n; int max;
 max = img_in->width * img_in->height;
 for (n = 0; n < max; n++)
  img_out->raw1[n] = 0xFF;
 for (y=0; y < img_out->height; y++) {
  for (x=0; x < img_out->width; x++) {
   if (img_in->raw[y * img_out->width + x] >= high) {
    trace (x, y, low, img_in, img_out);
   }
  }
 }
}

variable_types: high int

variable_types: n int

variable_types: max int


original_code:
void non_max_suppression(struct image_info *iif, int g[], int dir[]) {
 int w, h, x, y, max_x, max_y;
 w = iif->width;
 h = iif->height;
 max_x = w;
 max_y = w * h;
 for (y = 0; y < max_y; y += w) {
  for (x = 0; x < max_x; x++) {
   switch (dir[x + y]) {
    case 0:
     if (g[x + y] > g[x + y - w] && g[x + y] > g[x + y + w]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    case 1:
     if (g[x + y] > g[x + y - w - 1] && g[x + y] > g[x + y + w + 1]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    case 2:
     if (g[x + y] > g[x + y - 1] && g[x + y] > g[x + y + 1]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    case 3:
     if (g[x + y] > g[x + y - w + 1] && g[x + y] > g[x + y + w - 1]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    default:
     printf("ERROR - direction outside range 0 to 3, dir[%d+%d] = %d\n", x, y, dir[x+y]);
     break;
   }
  }
 }
}

canonical_original_code:
void non_max_suppression(struct image_info *iif, int g[], int dir[]) {
 int w; int h; int x; int y; int max_x; int max_y;
 w = iif->width;
 h = iif->height;
 max_x = w;
 max_y = w * h;
 for (y = 0; y < max_y; y += w) {
  for (x = 0; x < max_x; x++) {
   switch (dir[x + y]) {
    case 0:
     if (g[x + y] > g[x + y - w] && g[x + y] > g[x + y + w]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    case 1:
     if (g[x + y] > g[x + y - w - 1] && g[x + y] > g[x + y + w + 1]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    case 2:
     if (g[x + y] > g[x + y - 1] && g[x + y] > g[x + y + 1]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    case 3:
     if (g[x + y] > g[x + y - w + 1] && g[x + y] > g[x + y + w - 1]) {
      if (g[x + y] > 255)
       iif->raw[x + y] = 0xFF;
      else
       iif->raw[x + y] = g[x + y];
     } else
      iif->raw[x + y] = 0x00;
     break;
    default:
     printf("ERROR - direction outside range 0 to 3, dir[%d+%d] = %d\n", x, y, dir[x+y]);
     break;
   }
  }
 }
}

variable_types:
g
int []

variable_types:
dir
int []

variable_types: w int

variable_types: h int


canonical_original_code:
void calc_gradient_sobel(struct image_info *iif, int g[], int dir[]) {
 int w; int h; int x; int y; int max_x; int max_y; int g_x; int g_y;
 float g_div;
 w = iif->width;
 h = iif->height;
 max_x = w - 3;
 max_y = w * (h - 3);
 for (y = w * 3; y < max_y; y += w) {
  for (x = 3; x < max_x; x++) {
   g_x = (2 * iif->raw[x + y + 1]
    + iif->raw[x + y - w + 1]
    + iif->raw[x + y + w + 1]
    - 2 * iif->raw[x + y - 1]
    - iif->raw[x + y - w - 1]
    - iif->raw[x + y + w - 1]);
   g_y = 2 * iif->raw[x + y - w]
    + iif->raw[x + y - w + 1]
    + iif->raw[x + y - w - 1]
    - 2 * iif->raw[x + y + w]
    - iif->raw[x + y + w + 1]
    - iif->raw[x + y + w - 1];
   g[x + y] = sqrt(g_x * g_x + g_y * g_y);
   if (g_x == 0) {
    dir[x + y] = 2;
   } else {
    g_div = g_y / (float) g_x;
    if (g_div < 0) {
     if (g_div < -2.41421356237) {
      dir[x + y] = 0;
     } else {
      if (g_div < -0.414213562373) {
       dir[x + y] = 1;
      } else {
       dir[x + y] = 2;
      }
     }
    } else {
     if (g_div > 2.41421356237) {
      dir[x + y] = 0;
     } else {
      if (g_div > 0.414213562373) {
       dir[x + y] = 3;
      } else {
       dir[x + y] = 2;
      }
     }
    }
   }
  }
 }
}


decompiled_code:
void __fastcall canny_edge_detect(__int64 a1)
{
size_t v1; // r13
void *v2; // r14
void *v3; // rbp
unsigned __int8 *v4; // r15
unsigned __int8 *v5; // rax
int v6; // [rsp+0h] [rbp-50h] BYREF
int v7; // [rsp+4h] [rbp-4Ch] BYREF
unsigned __int8 *v8[9]; // [rsp+8h] [rbp-48h] BYREF

if ( *(_DWORD *)(a1 + 28) == 1 )
{
v1 = *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20);
v2 = malloc(4 * v1);
v3 = malloc(4 * v1);
v4 = (unsigned __int8 *)malloc(v1);
memset(*(void **)(a1 + 8), 0, v1);
v5 = *(unsigned __int8 **)(a1 + 20);
v8[0] = v4;
*(unsigned __int8 **)((char *)&v8[2] + 4) = v5;
v8[1] = 0LL;
calc_gradient_sobel(a1, (__int64)v2, (__int64)v3);
non_max_suppression((__int64)v8, (__int64)v2, (__int64)v3);
estimate_threshold(v8, &v6, &v7);
hysteresis(v6, v7, (__int64)v8, a1);
free(v2);
free(v3);
free(v4);
}
else
{
puts("Only accept grayscale so far.");
}
}

original_code:
void canny_edge_detect(struct image_info *iif) {
 struct image_info img_scratch;
 int high, low;
 if (iif->bytes_per_pixel != 1) {
  printf("Only accept grayscale so far.\n");
  return;
 }
 int *g = malloc(iif->width * iif->height * sizeof(int));
 int *dir = malloc(iif->width * iif->height * sizeof(int));
 unsigned char *img_scratch_data = malloc(iif->width *iif->height
   * sizeof(char));
 memset(iif->raw1, 0, iif->width * iif->height * iif->bytes_per_pixel);
 img_scratch.width = iif->width;
 img_scratch.height = iif->height;
 img_scratch.raw = img_scratch_data;
 img_scratch.raw1 = ((void *)0);
 calc_gradient_sobel(iif, g, dir);
 non_max_suppression(&img_scratch, g, dir);
 estimate_threshold(&img_scratch, &high, &low);
 hysteresis(high, low, &img_scratch, iif);
 free(g);
 free(dir);
 free(img_scratch_data);
}

canonical_original_code:
void canny_edge_detect(struct image_info *iif) {
 struct image_info img_scratch;
 int high; int low;
 if (iif->bytes_per_pixel != 1) {
  printf("Only accept grayscale so far.\n");
  return;
 }
 int *g = malloc(iif->width * iif->height * sizeof(int));
 int *dir = malloc(iif->width * iif->height * sizeof(int));
 unsigned char *img_scratch_data = malloc(iif->width *iif->height
   * sizeof(char));
 memset(iif->raw1, 0, iif->width * iif->height * iif->bytes_per_pixel);
 img_scratch.width = iif->width;
 img_scratch.height = iif->height;
 img_scratch.raw = img_scratch_data;
 img_scratch.raw1 = ((void *)0);
 calc_gradient_sobel(iif, g, dir);
 non_max_suppression(&img_scratch, g, dir);
 estimate_threshold(&img_scratch, &high, &low);
 hysteresis(high, low, &img_scratch, iif);
 free(g);
 free(dir);
 free(img_scratch_data);
}

variable_types: struct image_info { unsigned char *raw; unsigned char *raw1; int color_space; int width; int height; int bytes_per_pixel; }

variable_types:
g
int *

variable_types:
dir
int *


original_code:
void gaussian_noise_reduce(struct image_info *iif)
{
 int w, h, x, y, max_x, max_y;
 memset(iif->raw1, 0, iif->width * iif->height * iif->bytes_per_pixel);
 w = iif->width;
 h = iif->height;
 max_x = w - 2;
 max_y = w * (h - 2);
 for (y = w * 2; y < max_y; y += w) {
  for (x = 2; x < max_x; x++) {
   iif->raw1[x + y] =
    (2 * iif->raw[x + y - 2 - w - w] +
    4 * iif->raw[x + y - 1 - w - w] +
    5 * iif->raw[x + y - w - w] +
    4 * iif->raw[x + y + 1 - w - w] +
    2 * iif->raw[x + y + 2 - w - w] +
    4 * iif->raw[x + y - 2 - w] +
    9 * iif->raw[x + y - 1 - w] +
    12 * iif->raw[x + y - w] +
    9 * iif->raw[x + y + 1 - w] +
    4 * iif->raw[x + y + 2 - w] +
    5 * iif->raw[x + y - 2] +
    12 * iif->raw[x + y - 1] +
    15 * iif->raw[x + y] +
    12 * iif->raw[x + y + 1] +
    5 * iif->raw[x + y + 2] +
    4 * iif->raw[x + y - 2 + w] +
    9 * iif->raw[x + y - 1 + w] +
    12 * iif->raw[x + y + w] +
    9 * iif->raw[x + y + 1 + w] +
    4 * iif->raw[x + y + 2 + w] +
    2 * iif->raw[x + y - 2 + w + w] +
    4 * iif->raw[x + y - 1 + w + w] +
    5 * iif->raw[x + y + w + w] +
    4 * iif->raw[x + y + 1 + w + w] +
    2 * iif->raw[x + y + 2 + w + w]) / 159;
  }
 }
}

canonical_original_code:
void gaussian_noise_reduce(struct image_info *iif)
{
 int w; int h; int x; int y; int max_x; int max_y;
 memset(iif->raw1, 0, iif->width * iif->height * iif->bytes_per_pixel);
 w = iif->width;
 h = iif->height;
 max_x = w - 2;
 max_y = w * (h - 2);
 for (y = w * 2; y < max_y; y += w) {
  for (x = 2; x < max_x; x++) {
   iif->raw1[x + y] =
    (2 * iif->raw[x + y - 2 - w - w] +
    4 * iif->raw[x + y - 1 - w - w] +
    5 * iif->raw[x + y - w - w] +
    4 * iif->raw[x + y + 1 - w - w] +
    2 * iif->raw[x + y + 2 - w - w] +
    4 * iif->raw[x + y - 2 - w] +
    9 * iif->raw[x + y - 1 - w] +
    12 * iif->raw[x + y - w] +
    9 * iif->raw[x + y + 1 - w] +
    4 * iif->raw[x + y + 2 - w] +
    5 * iif->raw[x + y - 2] +
    12 * iif->raw[x + y - 1] +
    15 * iif->raw[x + y] +
    12 * iif->raw[x + y + 1] +
    5 * iif->raw[x + y + 2] +
    4 * iif->raw[x + y - 2 + w] +
    9 * iif->raw[x + y - 1 + w] +
    12 * iif->raw[x + y + w] +
    9 * iif->raw[x + y + 1 + w] +
    4 * iif->raw[x + y + 2 + w] +
    2 * iif->raw[x + y - 2 + w + w] +
    4 * iif->raw[x + y - 1 + w + w] +
    5 * iif->raw[x + y + w + w] +
    4 * iif->raw[x + y + 1 + w + w] +
    2 * iif->raw[x + y + 2 + w + w]) / 159;
  }
 }
}


decompiled_code:
double __fastcall calc_kurtosis(__int64 a1, int a2)
{
long double v2; // fst7
__int64 v3; // rax
__int64 v4; // rbx
double v5; // xmm2_8
long double v6; // fst7
double v7; // xmm0_8
float v9; // [rsp+0h] [rbp-38h]
double v10; // [rsp+0h] [rbp-38h]

if ( !a1 || a2 <= 3 )
{
printf("%s: invalid input arguments\n", "calc_kurtosis");
abort();
}
v2 = 0.0;
v3 = 0LL;
do
v2 = v2 + *(double *)(a1 + 8 * v3++);
while ( a2 > (int)v3 );
v4 = 0LL;
v5 = 0.0;
v9 = v2 / (long double)a2;
v6 = 0.0;
v10 = v9;
do
{
v5 = v5 + pow(*(double *)(a1 + 8 * v4) - v10, 4.0);
v7 = *(double *)(a1 + 8 * v4++);
v6 = v6 + (v7 - v10) * (v7 - v10);
}
while ( a2 > (int)v4 );
return (double)((a2 - 1) * a2 * (a2 + 1)) * v5 / ((long double)((a2 - 2) * (a2 - 3)) * ((double)v6 * (double)v6));
}

canonical_decompiled_code:
double __fastcall func0(__int64 a1, int a2)
{
long double v2; // fst7
__int64 v3; // rax
__int64 v4; // rbx
double v5; // xmm2_8
long double v6; // fst7
double v7; // xmm0_8
float v9; // [rsp+0h] [rbp-38h]
double v10; // [rsp+0h] [rbp-38h]

if ( !a1 || a2 <= 3 )
{
printf("%s: invalid input arguments\n", "calc_kurtosis");
abort();
}
v2 = 0.0;
v3 = 0LL;
do
v2 = v2 + *(double *)(a1 + 8 * v3++);
while ( a2 > (int)v3 );
v4 = 0LL;
v5 = 0.0;
v9 = v2 / (long double)a2;
v6 = 0.0;
v10 = v9;
do
{
v5 = v5 + pow(*(double *)(a1 + 8 * v4) - v10, 4.0);
v7 = *(double *)(a1 + 8 * v4++);
v6 = v6 + (v7 - v10) * (v7 - v10);
}
while ( a2 > (int)v4 );
return (double)((a2 - 1) * a2 * (a2 + 1)) * v5 / ((long double)((a2 - 2) * (a2 - 3)) * ((double)v6 * (double)v6));
}

original_code:
static double calc_kurtosis(double *edges, int len)
{
 double kurt = 0, accum = 0;
 long double sum = 0, sumdist = 0;
 float mean = 0;
 int i;
 if (!edges || len < 4) {
  printf("%s: invalid input arguments\n", __FUNCTION__);
  abort();
 }
 for (i = 0; i < len; i++)
  sum += edges[i];
 mean = (float) (sum / len);
 for (i = 0; i < len; i++) {
  accum += pow((edges[i] - mean), 4.0);
  sumdist += pow((edges[i] - mean), 2.0);
 }
 sumdist = pow(sumdist, 2.0);
 kurt =
  (len * (len + 1) * (len - 1) * accum) / ((len - 2) * ( len - 3) * sumdist);
 return kurt;
}

canonical_original_code:
static double calc_kurtosis(double *edges, int len)
{
 double kurt = 0; double accum = 0;
 long double sum = 0; long double sumdist = 0;
 float mean = 0;
 int i;
 if (!edges || len < 4) {
  printf("%s: invalid input arguments\n", __FUNCTION__);
  abort();
 }
 for (i = 0; i < len; i++)
  sum += edges[i];
 mean = (float) (sum / len);
 for (i = 0; i < len; i++) {
  accum += pow((edges[i] - mean), 4.0);
  sumdist += pow((edges[i] - mean), 2.0);
 }
 sumdist = pow(sumdist, 2.0);
 kurt =
  (len * (len + 1) * (len - 1) * accum) / ((len - 2) * ( len - 3) * sumdist);
 return kurt;
}

variable_types:
edges
double *

variable_types: len int

variable_types: kurt double

variable_types: accum double

variable_types:
sum
long double

variable_types:
sumdist
long double

variable_types: mean float

variable_types: i int

return_type: double


decompiled_code:
float __fastcall calc_blur(__int64 a1)
{
double *v1; // rbp
float result; // xmm0_4

v1 = (double *)malloc(8LL * *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20));
get_edge_strength_isra_0(a1, v1);
result = calc_kurtosis((__int64)v1, *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20)) * 100000.0;
free(v1);
if ( result > 100.0 )
result = *(float *)&LC5;
return result;
}

canonical_decompiled_code:
float __fastcall func2(__int64 a1)
{
double *v1; // rbp
float result; // xmm0_4

v1 = (double *)malloc(8LL * *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20));
func1(a1, v1);
result = func0((__int64)v1, *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20)) * 100000.0;
free(v1);
if ( result > 100.0 )
result = *(float *)&LC5;
return result;
}

original_code:
float calc_blur(struct image_info *iif)
{
 float rate = 0;
 double *edges = malloc(iif->width * iif->height * sizeof(double));
 get_edge_strength(iif, edges);
 rate = calc_kurtosis(edges, iif->width * iif->height) * 100000.f;
 free(edges);
 return (rate > 100 ? 100 : rate);
}

variable_types: struct image_info { unsigned char *raw; unsigned char *raw1; int color_space; int width; int height; int bytes_per_pixel; } *

variable_types: rate float


decompiled_code:
__int64 __fastcall image_create(__int64 a1)
{
void *v2; // rdi
void *v3; // rdi
size_t v4; // rbp
void *v5; // rax
void *v6; // r12
void *v7; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(int *)(a1 + 20) <= 0 || *(int *)(a1 + 24) <= 0 )
return 0xFFFFFFFFLL;
v2 = *(void **)a1;
if ( v2 )
free(v2);
v3 = *(void **)(a1 + 8);
if ( v3 )
free(v3);
v4 = *(_DWORD *)(a1 + 28) * *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20);
v5 = malloc(v4);
*(_QWORD *)a1 = v5;
v6 = v5;
if ( !v5 )
return 0xFFFFFFFFLL;
v7 = malloc(v4);
*(_QWORD *)(a1 + 8) = v7;
if ( !v7 )
{
free(v6);
return 0xFFFFFFFFLL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
void *v2; // rdi
void *v3; // rdi
size_t v4; // rbp
void *v5; // rax
void *v6; // r12
void *v7; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(int *)(a1 + 20) <= 0 || *(int *)(a1 + 24) <= 0 )
return 0xFFFFFFFFLL;
v2 = *(void **)a1;
if ( v2 )
free(v2);
v3 = *(void **)(a1 + 8);
if ( v3 )
free(v3);
v4 = *(_DWORD *)(a1 + 28) * *(_DWORD *)(a1 + 24) * *(_DWORD *)(a1 + 20);
v5 = malloc(v4);
*(_QWORD *)a1 = v5;
v6 = v5;
if ( !v5 )
return 0xFFFFFFFFLL;
v7 = malloc(v4);
*(_QWORD *)(a1 + 8) = v7;
if ( !v7 )
{
free(v6);
return 0xFFFFFFFFLL;
}
return 0LL;
}

original_code:
int image_create(struct image_info *iif)
{
 if (!iif)
  return -1;
 if (iif->width <= 0 || iif->height <= 0)
  return -1;
 if (iif->raw)
  free(iif->raw);
 if (iif->raw1)
  free(iif->raw1);
 iif->raw = (unsigned char*) malloc(iif->width * iif->height * iif->bytes_per_pixel);
 if (iif->raw == ((void *)0))
  return -1;
 iif->raw1 = (unsigned char*) malloc(iif->width * iif->height * iif->bytes_per_pixel);
 if (iif->raw1 == ((void *)0)) {
  free(iif->raw);
  return -1;
 }
 return 0;
}

canonical_original_code:
int image_create(struct image_info *iif)
{
 if (!iif)
  return -1;
 if (iif->width <= 0 || iif->height <= 0)
  return -1;
 if (iif->raw)
  free(iif->raw);
 if (iif->raw1)
  free(iif->raw1);
 iif->raw = (unsigned char *) malloc(iif->width * iif->height * iif->bytes_per_pixel);
 if (iif->raw == ((void *)0))
  return -1;
 iif->raw1 = (unsigned char *) malloc(iif->width * iif->height * iif->bytes_per_pixel);
 if (iif->raw1 == ((void *)0)) {
  free(iif->raw);
  return -1;
 }
 return 0;
}


decompiled_code:
void __fastcall image_clean(__int64 a1)
{
void *v2; // rdi
void *v3; // rdi

if ( a1 )
{
v2 = *(void **)a1;
if ( v2 )
{
free(v2);
*(_QWORD *)a1 = 0LL;
}
v3 = *(void **)(a1 + 8);
if ( v3 )
{
free(v3);
*(_QWORD *)(a1 + 8) = 0LL;
}
}
}

original_code:
void image_clean(struct image_info *iif)
{
 if (!iif)
  return;
 if (iif->raw) {
  free(iif->raw);
  iif->raw = ((void *)0);
 }
 if (iif->raw1) {
  free(iif->raw1);
  iif->raw1 = ((void *)0);
 }
}


decompiled_code:
__int64 __fastcall add_vertex_to_graph(char *src)
{
_QWORD *v2; // rax
_DWORD *v3; // rbx
_QWORD *v4; // rdi
int v5; // eax
char *v6; // rdx
__int64 result; // rax
__int64 v8; // rcx

v2 = malloc(0x218uLL);
if ( v2 )
{
v3 = graph;
v2[65] = 0LL;
v2[63] = 0LL;
v4 = v2;
v2[64] = 0LL;
v5 = v3[2];
*((_DWORD *)v4 + 125) = v5;
v3[2] = v5 + 1;
v6 = strcpy((char *)v4, src);
result = *(_QWORD *)v3;
if ( *(_QWORD *)v3 )
{
do
{
v8 = result;
result = *(_QWORD *)(result + 520);
}
while ( result );
*(_QWORD *)(v8 + 520) = v6;
*((_QWORD *)v6 + 65) = 0LL;
}
else
{
*(_QWORD *)v3 = v6;
}
}
else
{
puts("Error: Unable to allocate memory for a new vertex.");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src)
{
_QWORD *v2; // rax
_DWORD *v3; // rbx
_QWORD *v4; // rdi
int v5; // eax
char *v6; // rdx
__int64 result; // rax
__int64 v8; // rcx

v2 = malloc(0x218uLL);
if ( v2 )
{
v3 = graph;
v2[65] = 0LL;
v2[63] = 0LL;
v4 = v2;
v2[64] = 0LL;
v5 = v3[2];
*((_DWORD *)v4 + 125) = v5;
v3[2] = v5 + 1;
v6 = strcpy((char *)v4, src);
result = *(_QWORD *)v3;
if ( *(_QWORD *)v3 )
{
do
{
v8 = result;
result = *(_QWORD *)(result + 520);
}
while ( result );
*(_QWORD *)(v8 + 520) = v6;
*((_QWORD *)v6 + 65) = 0LL;
}
else
{
*(_QWORD *)v3 = v6;
}
}
else
{
puts("Error: Unable to allocate memory for a new vertex.");
result = 1LL;
}
return result;
}

original_code:
int add_vertex_to_graph(char * vertexName) {
 Vertex *temp,*right;
 temp = malloc(sizeof(Vertex));
 if(temp == ((void *)0)){
    printf("Error: Unable to allocate memory for a new vertex.\n");
   return 1;
 }
 temp->next = ((void *)0);
 temp->neighbors = ((void *)0);
 temp->originals = ((void *)0);
 temp->vertexID = graph->vertexCounter;
 graph->vertexCounter = graph->vertexCounter + 1;
 strcpy(temp->vertexName, vertexName);
 if(graph->vertices == ((void *)0)) {
  graph->vertices = temp;
 }
 else {
  right = graph->vertices;
  while(right->next != ((void *)0)) {
   right = right->next;
  }
  right->next = temp;
  right = temp;
  right->next = ((void *)0);
 }
 return 0;
}

canonical_original_code:
int add_vertex_to_graph(char *vertexName) {
 struct Vertex *temp; struct Vertex *right;
 temp = malloc(sizeof(Vertex));
 if(temp == ((void *)0)){
    printf("Error: Unable to allocate memory for a new vertex.\n");
   return 1;
 }
 temp->next = ((void *)0);
 temp->neighbors = ((void *)0);
 temp->originals = ((void *)0);
 temp->vertexID = graph->vertexCounter;
 graph->vertexCounter = graph->vertexCounter + 1;
 strcpy(temp->vertexName, vertexName);
 if(graph->vertices == ((void *)0)) {
  graph->vertices = temp;
 }
 else {
  right = graph->vertices;
  while(right->next != ((void *)0)) {
   right = right->next;
  }
  right->next = temp;
  right = temp;
  right->next = ((void *)0);
 }
 return 0;
}

variable_types:
vertexName
char *

variable_types: struct Vertex { char vertexName[500]; int vertexID; struct Neighbor { int vertexID; double weight; struct Neighbor *next; } *neighbors; struct Neighbor *originals; struct Vertex *next; int clusterID; } *

user_defined_types: struct Vertex { char vertexName[500]; int vertexID; struct Neighbor *neighbors; struct Neighbor *originals; struct Vertex *next; int clusterID; }