SSPDataIn

original_code:
void SSPDataIn(unsigned char RxChar, SSP_TX_RX_PACKET * ss)
{
 unsigned short crc;
 if (RxChar == 0x7F && ss->rxPtr == 0) {
  ss->rxData[ss->rxPtr++] = RxChar;
 } else {
  if (ss->CheckStuff == 1) {
   if (RxChar != 0x7F) {
    ss->rxData[0] = 0x7F;
    ss->rxData[1] = RxChar;
    ss->rxPtr = 2;
   } else
    ss->rxData[ss->rxPtr++] = RxChar;
   ss->CheckStuff = 0;
  } else {
   if (RxChar == 0x7F)
    ss->CheckStuff = 1;
   else {
    ss->rxData[ss->rxPtr++] = RxChar;
    if (ss->rxPtr == 3)
     ss->rxBufferLength = ss->rxData[2] + 5;
   }
  }
  if (ss->rxPtr == ss->rxBufferLength) {
   if ((ss->rxData[1] & 0x7F) == ss->SSPAddress) {
    crc =
        cal_crc_loop_CCITT_A(ss->rxBufferLength - 3, &ss->rxData[1], 0xFFFF,
        0x8005);
    if ((unsigned char) (crc & 0xFF) == ss->rxData[ss->rxBufferLength - 2]
        && (unsigned char) ((crc >> 8) & 0xFF) == ss->rxData[ss->rxBufferLength - 1])
     ss->NewResponse = 1;
   }
   ss->rxPtr = 0;
   ss->CheckStuff = 0;
  }
 }
}

canonical_original_code:
void SSPDataIn(unsigned char RxChar, struct SSP_TX_RX_PACKET *ss)
{
 unsigned short crc;
 if (RxChar == 0x7F && ss->rxPtr == 0) {
  ss->rxData[ss->rxPtr++] = RxChar;
 } else {
  if (ss->CheckStuff == 1) {
   if (RxChar != 0x7F) {
    ss->rxData[0] = 0x7F;
    ss->rxData[1] = RxChar;
    ss->rxPtr = 2;
   } else
    ss->rxData[ss->rxPtr++] = RxChar;
   ss->CheckStuff = 0;
  } else {
   if (RxChar == 0x7F)
    ss->CheckStuff = 1;
   else {
    ss->rxData[ss->rxPtr++] = RxChar;
    if (ss->rxPtr == 3)
     ss->rxBufferLength = ss->rxData[2] + 5;
   }
  }
  if (ss->rxPtr == ss->rxBufferLength) {
   if ((ss->rxData[1] & 0x7F) == ss->SSPAddress) {
    crc =
        cal_crc_loop_CCITT_A(ss->rxBufferLength - 3, &ss->rxData[1], 0xFFFF,
        0x8005);
    if ((unsigned char) (crc & 0xFF) == ss->rxData[ss->rxBufferLength - 2]
        && (unsigned char) ((crc >> 8) & 0xFF) == ss->rxData[ss->rxBufferLength - 1])
     ss->NewResponse = 1;
   }
   ss->rxPtr = 0;
   ss->CheckStuff = 0;
  }
 }
}

variable_types:
RxChar
unsigned char

user_defined_types: struct SSP_TX_RX_PACKET { unsigned char txData[255]; unsigned char txPtr; unsigned char rxData[255]; unsigned char rxPtr; unsigned char txBufferLength; unsigned char rxBufferLength; unsigned char SSPAddress; unsigned char NewResponse; unsigned char CheckStuff; }


SSPSendCommand

repo: sixtyeight/Payout

decompiled_code:
__int64 __fastcall SSPSendCommand(unsigned int a1, __int64 a2)
{
__int64 result; // rax
char v3; // r15
char v4; // al
__suseconds_t v5; // r14
unsigned __int8 v6; // dl
__int64 v7; // rax
char v8; // dl
__int16 v9; // ax
unsigned __int8 v10; // al
int v11; // edx
__int64 v12; // rcx
__int16 v13; // dx
int v14; // eax
int v15; // edx
__int64 v16; // rax
char v17; // [rsp+0h] [rbp-358h]
char v18[269]; // [rsp+1h] [rbp-357h] BYREF
char v19; // [rsp+10Eh] [rbp-24Ah] BYREF
unsigned __int8 v20; // [rsp+10Fh] [rbp-249h] BYREF
char v21[256]; // [rsp+110h] [rbp-248h] BYREF
char v22[2]; // [rsp+210h] [rbp-148h]
unsigned __int8 v23; // [rsp+212h] [rbp-146h]
char v24; // [rsp+213h] [rbp-145h]
char v25[253]; // [rsp+214h] [rbp-144h] BYREF
unsigned __int8 v26; // [rsp+311h] [rbp-47h]
char v27; // [rsp+313h] [rbp-45h]

result = CompileSSPCommand((unsigned __int8 *)a2, v21);
if ( !(_DWORD)result )
{
*(_BYTE *)(a2 + 292) = 4;
return result;
}
v3 = *(_BYTE *)(a2 + 34);
do
{
v27 = 0;
result = WriteData(v21, (unsigned __int8)v25[252], a1);
if ( !(_DWORD)result )
{
*(_BYTE *)(a2 + 292) = 2;
return result;
}
*(_BYTE *)(a2 + 292) = 3;
v5 = GetClockMs();
if ( v27 )
{
LABEL_13:
v4 = *(_BYTE *)(a2 + 292);
if ( v4 == 3 )
goto LABEL_14;
}
else
{
while ( (unsigned __int64)(GetClockMs() - v5) <= *(_QWORD *)(a2 + 24) )
{
while ( (int)BytesInBuffer(a1) > 0 )
{
ReadData(a1, &v19, 1LL);
SSPDataIn(v19, v21);
}
if ( v27 )
goto LABEL_13;
}
*(_BYTE *)(a2 + 292) = 5;
v4 = 5;
}
--v3;
}
while ( v3 );
if ( v4 == 5 )
{
*(_BYTE *)(a2 + 294) = -1;
return 0LL;
}
LABEL_14:
if ( v24 == 126 )
{
v20 = v23 - 1;
DecryptSSPPacket(v25, v25, &v20, &v20, a2);
v9 = cal_crc_loop_CCITT_A((unsigned int)(__int16)(v20 - 2), v25, 0xFFFFLL, 32773LL);
if ( v22[v23 + 1] != (_BYTE)v9 || v22[v23 + 2] != HIBYTE(v9) )
{
*(_BYTE *)(a2 + 292) = 4;
return 0LL;
}
if ( encPktCount[*(unsigned __int8 *)(a2 + 33)] != ((unsigned __int8)v25[3] << 16)
+ (unsigned __int8)v25[1]
+ ((unsigned __int8)v25[2] << 8)
+ ((unsigned __int8)v25[4] << 24) )
{
*(_BYTE *)(a2 + 292) = 4;
return 0LL;
}
v10 = v25[0] + 5;
v26 = v25[0] + 5;
v17 = v22[0];
v18[0] = v22[1];
v18[1] = v25[0];
v11 = (unsigned __int8)v25[0];
if ( v25[0] )
{
v12 = 0LL;
do
{
v18[v12 + 2] = v25[v12 + 5];
++v12;
}
while ( v11 > (int)v12 );
}
v13 = cal_crc_loop_CCITT_A((unsigned int)(__int16)(v10 - 3), v18, 0xFFFFLL, 32773LL);
v14 = (unsigned __int8)v25[0];
*(&v17 + (unsigned __int8)v25[0] + 3) = v13;
*(&v17 + v14 + 4) = HIBYTE(v13);
v15 = v26;
if ( v26 )
{
v16 = 0LL;
do
{
v22[v16] = *(&v17 + v16);
++v16;
}
while ( v15 > (int)v16 );
}
}
v6 = v23;
*(_BYTE *)(a2 + 293) = v23;
if ( v6 )
{
v7 = 0LL;
do
{
*(_BYTE *)(a2 + v7 + 294) = *(&v24 + v7);
++v7;
}
while ( v6 > (int)v7 );
}
v8 = 0;
if ( *((_BYTE *)&sspSeq + *(unsigned __int8 *)(a2 + 33)) != 0x80 )
v8 = 0x80;
*((_BYTE *)&sspSeq + *(unsigned __int8 *)(a2 + 33)) = v8;
*(_BYTE *)(a2 + 292) = 3;
return 1LL;
}

original_code:
int SSPSendCommand(const SSP_PORT port, SSP_COMMAND * cmd)
{
 SSP_TX_RX_PACKET ssp;
 clock_t txTime, currentTime;
 int i;
 unsigned char encryptLength;
 unsigned short crcR;
 unsigned char buffer;
 unsigned char tData[255];
 unsigned char retry;
 unsigned int slaveCount;
 if (!CompileSSPCommand(cmd, &ssp)) {
  cmd->ResponseStatus = SSP_PACKET_ERROR;
  return 0;
 }
 retry = cmd->RetryLevel;
 do {
  ssp.NewResponse = 0;
  if (WriteData(ssp.txData, ssp.txBufferLength, port) == 0) {
   cmd->ResponseStatus = PORT_ERROR;
   return 0;
  }
  cmd->ResponseStatus = SSP_REPLY_OK;
  txTime = GetClockMs();
  while (!ssp.NewResponse) {
   currentTime = GetClockMs();
   if (currentTime - txTime > cmd->Timeout) {
    cmd->ResponseStatus = SSP_CMD_TIMEOUT;
    break;
   }
   while (BytesInBuffer(port) > 0) {
    ReadData(port, &buffer, 1);
    SSPDataIn(buffer, &ssp);
   }
  }
  if (cmd->ResponseStatus == SSP_REPLY_OK)
   break;
  retry--;
 } while (retry > 0);
 if (cmd->ResponseStatus == SSP_CMD_TIMEOUT) {
  cmd->ResponseData[0] = SSP_RESPONSE_TIMEOUT;
  return 0;
 }
 if (ssp.rxData[3] == 0x7E) {
  encryptLength = ssp.rxData[2] - 1;
  DecryptSSPPacket(&ssp.rxData[4], &ssp.rxData[4], &encryptLength, &encryptLength,
     (unsigned long long *) &cmd->Key);
  crcR = cal_crc_loop_CCITT_A(encryptLength - 2, &ssp.rxData[4], 0xFFFF, 0x8005);
  if ((unsigned char) (crcR & 0xFF) != ssp.rxData[ssp.rxData[2] + 1]
      || (unsigned char) ((crcR >> 8) & 0xFF) != ssp.rxData[ssp.rxData[2] + 2]) {
   cmd->ResponseStatus = SSP_PACKET_ERROR;
   return 0;
  }
  slaveCount = 0;
  for (i = 0; i < 4; i++)
   slaveCount += (unsigned int) (ssp.rxData[5 + i]) << (i * 8);
  if (slaveCount != encPktCount[cmd->SSPAddress]) {
   cmd->ResponseStatus = SSP_PACKET_ERROR;
   return 0;
  }
  ssp.rxBufferLength = ssp.rxData[4] + 5;
  tData[0] = ssp.rxData[0];
  tData[1] = ssp.rxData[1];
  tData[2] = ssp.rxData[4];
  for (i = 0; i < ssp.rxData[4]; i++)
   tData[3 + i] = ssp.rxData[9 + i];
  crcR = cal_crc_loop_CCITT_A(ssp.rxBufferLength - 3, &tData[1], 0xFFFF, 0x8005);
  tData[3 + ssp.rxData[4]] = (unsigned char) (crcR & 0xFF);
  tData[4 + ssp.rxData[4]] = (unsigned char) ((crcR >> 8) & 0xFF);
  for (i = 0; i < ssp.rxBufferLength; i++)
   ssp.rxData[i] = tData[i];
 }
 cmd->ResponseDataLength = ssp.rxData[2];
 for (i = 0; i < cmd->ResponseDataLength; i++)
  cmd->ResponseData[i] = ssp.rxData[i + 3];
 if (sspSeq[cmd->SSPAddress] == 0x80)
  sspSeq[cmd->SSPAddress] = 0;
 else
  sspSeq[cmd->SSPAddress] = 0x80;
 cmd->ResponseStatus = SSP_REPLY_OK;
 return 1;
}

canonical_original_code:
int SSPSendCommand(int port, struct SSP_COMMAND *cmd)
{
 struct SSP_TX_RX_PACKET ssp;
 long txTime; long currentTime;
 int i;
 unsigned char encryptLength;
 unsigned short crcR;
 unsigned char buffer;
 unsigned char tData[255];
 unsigned char retry;
 unsigned int slaveCount;
 if (!CompileSSPCommand(cmd, &ssp)) {
  cmd->ResponseStatus = SSP_PACKET_ERROR;
  return 0;
 }
 retry = cmd->RetryLevel;
 do {
  ssp.NewResponse = 0;
  if (WriteData(ssp.txData, ssp.txBufferLength, port) == 0) {
   cmd->ResponseStatus = PORT_ERROR;
   return 0;
  }
  cmd->ResponseStatus = SSP_REPLY_OK;
  txTime = GetClockMs();
  while (!ssp.NewResponse) {
   currentTime = GetClockMs();
   if (currentTime - txTime > cmd->Timeout) {
    cmd->ResponseStatus = SSP_CMD_TIMEOUT;
    break;
   }
   while (BytesInBuffer(port) > 0) {
    ReadData(port, &buffer, 1);
    SSPDataIn(buffer, &ssp);
   }
  }
  if (cmd->ResponseStatus == SSP_REPLY_OK)
   break;
  retry--;
 } while (retry > 0);
 if (cmd->ResponseStatus == SSP_CMD_TIMEOUT) {
  cmd->ResponseData[0] = SSP_RESPONSE_TIMEOUT;
  return 0;
 }
 if (ssp.rxData[3] == 0x7E) {
  encryptLength = ssp.rxData[2] - 1;
  DecryptSSPPacket(&ssp.rxData[4], &ssp.rxData[4], &encryptLength, &encryptLength,
     (unsigned long long *) &cmd->Key);
  crcR = cal_crc_loop_CCITT_A(encryptLength - 2, &ssp.rxData[4], 0xFFFF, 0x8005);
  if ((unsigned char) (crcR & 0xFF) != ssp.rxData[ssp.rxData[2] + 1]
      || (unsigned char) ((crcR >> 8) & 0xFF) != ssp.rxData[ssp.rxData[2] + 2]) {
   cmd->ResponseStatus = SSP_PACKET_ERROR;
   return 0;
  }
  slaveCount = 0;
  for (i = 0; i < 4; i++)
   slaveCount += (unsigned int) (ssp.rxData[5 + i]) << (i * 8);
  if (slaveCount != encPktCount[cmd->SSPAddress]) {
   cmd->ResponseStatus = SSP_PACKET_ERROR;
   return 0;
  }
  ssp.rxBufferLength = ssp.rxData[4] + 5;
  tData[0] = ssp.rxData[0];
  tData[1] = ssp.rxData[1];
  tData[2] = ssp.rxData[4];
  for (i = 0; i < ssp.rxData[4]; i++)
   tData[3 + i] = ssp.rxData[9 + i];
  crcR = cal_crc_loop_CCITT_A(ssp.rxBufferLength - 3, &tData[1], 0xFFFF, 0x8005);
  tData[3 + ssp.rxData[4]] = (unsigned char) (crcR & 0xFF);
  tData[4 + ssp.rxData[4]] = (unsigned char) ((crcR >> 8) & 0xFF);
  for (i = 0; i < ssp.rxBufferLength; i++)
   ssp.rxData[i] = tData[i];
 }
 cmd->ResponseDataLength = ssp.rxData[2];
 for (i = 0; i < cmd->ResponseDataLength; i++)
  cmd->ResponseData[i] = ssp.rxData[i + 3];
 if (sspSeq[cmd->SSPAddress] == 0x80)
  sspSeq[cmd->SSPAddress] = 0;
 else
  sspSeq[cmd->SSPAddress] = 0x80;
 cmd->ResponseStatus = SSP_REPLY_OK;
 return 1;
}

variable_types: port int

variable_types: struct SSP_COMMAND { struct SSP_FULL_KEY { unsigned long long FixedKey; unsigned long long EncryptKey; } Key; unsigned long BaudRate; unsigned long Timeout; unsigned char PortNumber; unsigned char SSPAddress; unsigned char RetryLevel; unsigned char EncryptionStatus; unsigned char CommandDataLength; unsigned char CommandData[255]; unsigned char ResponseStatus; unsigned char ResponseDataLength; unsigned char ResponseData[255]; unsigned char IgnoreError; } *

variable_types: struct SSP_TX_RX_PACKET { unsigned char txData[255]; unsigned char txPtr; unsigned char rxData[255]; unsigned char rxPtr; unsigned char txBufferLength; unsigned char rxBufferLength; unsigned char SSPAddress; unsigned char NewResponse; unsigned char CheckStuff; }

variable_types: txTime long

variable_types: currentTime long

variable_types: i int

variable_types:
encryptLength
unsigned char

variable_types:
crcR
unsigned short

variable_types:
buffer
unsigned char

variable_types:
tData
unsigned char [255]

variable_types:
retry
unsigned char

variable_types:
slaveCount
unsigned int

return_type: int

user_defined_types: struct SSP_COMMAND { struct SSP_FULL_KEY Key; unsigned long BaudRate; unsigned long Timeout; unsigned char PortNumber; unsigned char SSPAddress; unsigned char RetryLevel; unsigned char EncryptionStatus; unsigned char CommandDataLength; unsigned char CommandData[255]; unsigned char ResponseStatus; unsigned char ResponseDataLength; unsigned char ResponseData[255]; unsigned char IgnoreError; }


changemode

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

if ( a1 == 1 )
{
tcgetattr(0, &oldt_1);
*(__m128i *)&newt_0.c_iflag = _mm_load_si128((const __m128i *)&oldt_1);
*(__m128i *)&newt_0.c_line = _mm_load_si128((const __m128i *)&oldt_1.c_line);
*(__m128i *)&newt_0.c_cc[15] = _mm_load_si128((const __m128i *)&oldt_1.c_cc[15]);
*(_QWORD *)&newt_0.c_cc[31] = *(_QWORD *)&oldt_1.c_cc[31];
newt_0.c_ospeed = oldt_1.c_ospeed;
newt_0.c_lflag &= 0xFFFFFFF5;
result = tcsetattr(0, 0, &newt_0);
}
else if ( a1 == 2 )
{
tcgetattr(0, &oldt_1);
*(__m128i *)&newt_0.c_iflag = _mm_load_si128((const __m128i *)&oldt_1);
*(__m128i *)&newt_0.c_line = _mm_load_si128((const __m128i *)&oldt_1.c_line);
*(__m128i *)&newt_0.c_cc[15] = _mm_load_si128((const __m128i *)&oldt_1.c_cc[15]);
*(_QWORD *)&newt_0.c_cc[31] = *(_QWORD *)&oldt_1.c_cc[31];
newt_0.c_ospeed = oldt_1.c_ospeed;
newt_0.c_lflag = newt_0.c_lflag & 0xFFFFFFFD | 8;
result = tcsetattr(0, 0, &newt_0);
}
else
{
result = tcsetattr(0, 0, &oldt_1);
}
return result;
}

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

if ( a1 == 1 )
{
tcgetattr(0, &oldt_1);
*(__m128i *)&newt_0.c_iflag = _mm_load_si128((const __m128i *)&oldt_1);
*(__m128i *)&newt_0.c_line = _mm_load_si128((const __m128i *)&oldt_1.c_line);
*(__m128i *)&newt_0.c_cc[15] = _mm_load_si128((const __m128i *)&oldt_1.c_cc[15]);
*(_QWORD *)&newt_0.c_cc[31] = *(_QWORD *)&oldt_1.c_cc[31];
newt_0.c_ospeed = oldt_1.c_ospeed;
newt_0.c_lflag &= 0xFFFFFFF5;
result = tcsetattr(0, 0, &newt_0);
}
else if ( a1 == 2 )
{
tcgetattr(0, &oldt_1);
*(__m128i *)&newt_0.c_iflag = _mm_load_si128((const __m128i *)&oldt_1);
*(__m128i *)&newt_0.c_line = _mm_load_si128((const __m128i *)&oldt_1.c_line);
*(__m128i *)&newt_0.c_cc[15] = _mm_load_si128((const __m128i *)&oldt_1.c_cc[15]);
*(_QWORD *)&newt_0.c_cc[31] = *(_QWORD *)&oldt_1.c_cc[31];
newt_0.c_ospeed = oldt_1.c_ospeed;
newt_0.c_lflag = newt_0.c_lflag & 0xFFFFFFFD | 8;
result = tcsetattr(0, 0, &newt_0);
}
else
{
result = tcsetattr(0, 0, &oldt_1);
}
return result;
}

original_code:
void changemode(int dir)
{
 static struct termios oldt, newt;
 if (dir == 1) {
  tcgetattr(0, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(0000002 | 0000010);
  tcsetattr(0, 0, &newt);
 } else if (dir == 2) {
  tcgetattr(0, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(0000002);
  newt.c_lflag |= 0000010;
  tcsetattr(0, 0, &newt);
 } else {
  tcsetattr(0, 0, &oldt);
 }
}

canonical_original_code:
void changemode(int dir)
{
 static struct termios oldt; static struct termios newt;
 if (dir == 1) {
  tcgetattr(0, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(0000002 | 0000010);
  tcsetattr(0, 0, &newt);
 } else if (dir == 2) {
  tcgetattr(0, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(0000002);
  newt.c_lflag |= 0000010;
  tcsetattr(0, 0, &newt);
 } else {
  tcsetattr(0, 0, &oldt);
 }
}

variable_types: dir int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

return_type: void


kbhit

decompiled_code:
__int64 kbhit()
{
fd_set v1; // [rsp+0h] [rbp-98h] BYREF
struct timeval timeout; // [rsp+80h] [rbp-18h] BYREF

timeout.tv_sec = 0LL;
timeout.tv_usec = 0LL;
memset(&v1, 0, sizeof(v1));
v1.fds_bits[0] |= 1uLL;
select(1, &v1, 0LL, 0LL, &timeout);
return v1.fds_bits[0] & 1;
}

canonical_decompiled_code:
__int64 func1()
{
fd_set v1; // [rsp+0h] [rbp-98h] BYREF
struct timeval timeout; // [rsp+80h] [rbp-18h] BYREF

timeout.tv_sec = 0LL;
timeout.tv_usec = 0LL;
memset(&v1, 0, sizeof(v1));
v1.fds_bits[0] |= 1uLL;
select(1, &v1, 0LL, 0LL, &timeout);
return v1.fds_bits[0] & 1;
}

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

variable_types:
rdfs
struct fd_set { long __fds_bits[-1]; }


decompiled_code:
_BOOL8 open_ssp_port()
{
open_port = OpenSSPPort();
return open_port != -1;
}

canonical_decompiled_code:
_BOOL8 func2()
{
open_port = OpenSSPPort();
return open_port != -1;
}

original_code:
int open_ssp_port(const char *port)
{
 open_port = OpenSSPPort(port);
 return (open_port != -1);
}

canonical_original_code:
int open_ssp_port(char *port)
{
 open_port = OpenSSPPort(port);
 return (open_port != -1);
}

variable_types:
port
char *


decompiled_code:
__int64 close_ssp_port()
{
return CloseSSPPort((unsigned int)open_port);
}

canonical_decompiled_code:
__int64 func3()
{
return CloseSSPPort((unsigned int)open_port);
}

original_code:
void close_ssp_port()
{
 CloseSSPPort(open_port);
}


decompiled_code:
__int64 __fastcall send_ssp_command(__int64 a1)
{
return SSPSendCommand((unsigned int)open_port, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return SSPSendCommand((unsigned int)open_port, a1);
}

original_code:
int send_ssp_command(SSP_COMMAND * sspC)
{
 return SSPSendCommand(open_port, sspC);
}

canonical_original_code:
int send_ssp_command(struct SSP_COMMAND *sspC)
{
 return SSPSendCommand(open_port, sspC);
}

user_defined_types: struct SSP_FULL_KEY { unsigned long long FixedKey; unsigned long long EncryptKey; }


decompiled_code:
__int64 __fastcall negotiate_ssp_encryption(__int64 a1, __int64 a2)
{
return NegotiateSSPEncryption((unsigned int)open_port, (unsigned int)*(char *)(a1 + 33), a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
return NegotiateSSPEncryption((unsigned int)open_port, (unsigned int)*(char *)(a1 + 33), a2);
}

original_code:
int negotiate_ssp_encryption(SSP_COMMAND * sspC, SSP_FULL_KEY * hostKey)
{
 return NegotiateSSPEncryption(open_port, sspC->SSPAddress, hostKey);
}

canonical_original_code:
int negotiate_ssp_encryption(struct SSP_COMMAND *sspC, struct SSP_FULL_KEY *hostKey)
{
 return NegotiateSSPEncryption(open_port, sspC->SSPAddress, hostKey);
}

variable_types:
hostKey
struct SSP_FULL_KEY *


DownloadDataToTarget

decompiled_code:
__int64 __fastcall DownloadDataToTarget(void *src, size_t n, char *a3, char a4, __int64 a5)
{
__int64 result; // rax
unsigned int *v8; // rbp
_BYTE *v9; // rbx
unsigned __int16 v10; // dx
unsigned int v11; // ebx
pthread_t newthread; // [rsp+18h] [rbp-270h] BYREF
char v14[16]; // [rsp+20h] [rbp-268h] BYREF
__int64 v15; // [rsp+30h] [rbp-258h]
__int64 v16; // [rsp+38h] [rbp-250h]
char v17; // [rsp+41h] [rbp-247h]
char v18; // [rsp+42h] [rbp-246h]
char v19; // [rsp+43h] [rbp-245h]
char v20; // [rsp+44h] [rbp-244h]
char v21; // [rsp+45h] [rbp-243h]
char v22; // [rsp+146h] [rbp-142h]

result = 1048580LL;
if ( download_in_progress != 1 )
{
v8 = (unsigned int *)malloc(0x148uLL);
v9 = malloc(n);
*((_QWORD *)v8 + 2) = v9;
memcpy(v9, src, n);
download_block = 0LL;
if ( *(_WORD *)v9 == 21577 && v9[2] == 76 )
{
v10 = (unsigned __int8)v9[63] + ((unsigned __int8)v9[62] << 8);
if ( !v10 )
v10 = 4096;
*(_QWORD *)v8 = (((unsigned __int64)(unsigned __int8)v9[19] << 8)
+ ((unsigned __int64)(unsigned __int8)v9[17] << 24)
+ ((unsigned __int64)(unsigned __int8)v9[18] << 16)
+ (unsigned __int8)v9[20])
/ v10
- (((((unsigned __int64)(unsigned __int8)v9[19] << 8)
+ ((unsigned __int64)(unsigned __int8)v9[17] << 24)
+ ((unsigned __int64)(unsigned __int8)v9[18] << 16)
+ (unsigned __int8)v9[20])
% v10 == 0)
- 1LL);
v16 = 1000LL;
v15 = 9600LL;
v18 = 2;
v17 = a4;
*((_BYTE *)v8 + 40) = a4;
v11 = OpenSSPPort(a3);
strcpy((char *)v8 + 64, a3);
v8[6] = v11;
if ( v11 == -1 )
{
free(*((void **)v8 + 2));
free(v8);
result = 1048580LL;
}
else
{
v19 = 0;
v20 = 1;
v21 = 17;
if ( (unsigned int)SSPSendCommand(v11, v14) && v22 == -16 )
{
if ( !a5 )
{
LABEL_12:
pthread_create(&newthread, 0LL, DownloadITLTarget, v8);
return *v8;
}
if ( (unsigned int)NegotiateSSPEncryption(v11, (unsigned int)a4, v8 + 12) )
{
*((_BYTE *)v8 + 41) = 1;
*((_QWORD *)v8 + 6) = a5;
goto LABEL_12;
}
}
CloseSSPPort(v11);
free(*((void **)v8 + 2));
free(v8);
result = 1048581LL;
}
}
else
{
free(v9);
free(v8);
result = 1048579LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *src, size_t n, char *a3, char a4, __int64 a5)
{
__int64 result; // rax
unsigned int *v8; // rbp
_BYTE *v9; // rbx
unsigned __int16 v10; // dx
unsigned int v11; // ebx
pthread_t newthread; // [rsp+18h] [rbp-270h] BYREF
char v14[16]; // [rsp+20h] [rbp-268h] BYREF
__int64 v15; // [rsp+30h] [rbp-258h]
__int64 v16; // [rsp+38h] [rbp-250h]
char v17; // [rsp+41h] [rbp-247h]
char v18; // [rsp+42h] [rbp-246h]
char v19; // [rsp+43h] [rbp-245h]
char v20; // [rsp+44h] [rbp-244h]
char v21; // [rsp+45h] [rbp-243h]
char v22; // [rsp+146h] [rbp-142h]

result = 1048580LL;
if ( download_in_progress != 1 )
{
v8 = (unsigned int *)malloc(0x148uLL);
v9 = malloc(n);
*((_QWORD *)v8 + 2) = v9;
memcpy(v9, src, n);
download_block = 0LL;
if ( *(_WORD *)v9 == 21577 && v9[2] == 76 )
{
v10 = (unsigned __int8)v9[63] + ((unsigned __int8)v9[62] << 8);
if ( !v10 )
v10 = 4096;
*(_QWORD *)v8 = (((unsigned __int64)(unsigned __int8)v9[19] << 8)
+ ((unsigned __int64)(unsigned __int8)v9[17] << 24)
+ ((unsigned __int64)(unsigned __int8)v9[18] << 16)
+ (unsigned __int8)v9[20])
/ v10
- (((((unsigned __int64)(unsigned __int8)v9[19] << 8)
+ ((unsigned __int64)(unsigned __int8)v9[17] << 24)
+ ((unsigned __int64)(unsigned __int8)v9[18] << 16)
+ (unsigned __int8)v9[20])
% v10 == 0)
- 1LL);
v16 = 1000LL;
v15 = 9600LL;
v18 = 2;
v17 = a4;
*((_BYTE *)v8 + 40) = a4;
v11 = OpenSSPPort(a3);
strcpy((char *)v8 + 64, a3);
v8[6] = v11;
if ( v11 == -1 )
{
free(*((void **)v8 + 2));
free(v8);
result = 1048580LL;
}
else
{
v19 = 0;
v20 = 1;
v21 = 17;
if ( (unsigned int)SSPSendCommand(v11, v14) && v22 == -16 )
{
if ( !a5 )
{
LABEL_12:
pthread_create(&newthread, 0LL, DownloadITLTarget, v8);
return *v8;
}
if ( (unsigned int)NegotiateSSPEncryption(v11, (unsigned int)a4, v8 + 12) )
{
*((_BYTE *)v8 + 41) = 1;
*((_QWORD *)v8 + 6) = a5;
goto LABEL_12;
}
}
CloseSSPPort(v11);
free(*((void **)v8 + 2));
free(v8);
result = 1048581LL;
}
}
else
{
free(v9);
free(v8);
result = 1048579LL;
}
}
return result;
}

original_code:
int DownloadDataToTarget(const unsigned char *data, const unsigned long dlength, const char *cPort,
    const unsigned char sspAddress, const unsigned long long key)
{
 int i;
 unsigned long numCurBytes;
 unsigned short dBlockSize;
 ITL_FILE_DOWNLOAD *itlFile;
 SSP_COMMAND sspC;
 SSP_PORT port;
 if (download_in_progress == 1)
  return 0x100004;
 itlFile = malloc(sizeof(ITL_FILE_DOWNLOAD));
 itlFile->fData = malloc(dlength);
 memcpy(itlFile->fData, data, dlength);
 download_block = 0;
 if (itlFile->fData[0] == 'I' && itlFile->fData[1] == 'T' && itlFile->fData[2] == 'L') {
  numCurBytes = 0;
  for (i = 0; i < 4; i++) {
   numCurBytes += (unsigned long) itlFile->fData[17 + i] << (8 * (3 - i));
  }
  dBlockSize = (256 * (unsigned short) itlFile->fData[0x3e]) + (unsigned short) itlFile->fData[0x3f];
  if (dBlockSize == 0)
   dBlockSize = 4096;
  itlFile->NumberOfBlocks = numCurBytes / dBlockSize;
  if (numCurBytes % dBlockSize != 0)
   itlFile->NumberOfBlocks += 1;
 } else {
  free(itlFile->fData);
  free(itlFile);
  return 0x100003;
 }
 sspC.Timeout = 1000;
 sspC.BaudRate = 9600;
 sspC.RetryLevel = 2;
 sspC.SSPAddress = sspAddress;
 itlFile->SSPAddress = sspAddress;
 port = OpenSSPPort(cPort);
 strcpy(itlFile->portname, cPort);
 itlFile->port = port;
 if (port == -1) {
  free(itlFile->fData);
  free(itlFile);
  return 0x100004;
 }
 sspC.EncryptionStatus = 0;
 sspC.CommandDataLength = 1;
 sspC.CommandData[0] = 0x11;
 if (SSPSendCommand(port, &sspC) == 0) {
  CloseSSPPort(port);
  free(itlFile->fData);
  free(itlFile);
  return 0x100005;
 }
 if (sspC.ResponseData[0] != SSP_RESPONSE_OK) {
  CloseSSPPort(port);
  free(itlFile->fData);
  free(itlFile);
  return 0x100005;
 }
 if (key > 0) {
  if (NegotiateSSPEncryption(port, sspAddress, &itlFile->Key) == 0) {
   CloseSSPPort(port);
   free(itlFile->fData);
   free(itlFile);
   return 0x100005;
  }
  itlFile->EncryptionStatus = 1;
  itlFile->Key.FixedKey = key;
 }
 pthread_t thread;
 pthread_create(&thread, ((void *)0), (void *) DownloadITLTarget, (void *) itlFile);
 return itlFile->NumberOfBlocks;
}

canonical_original_code:
int DownloadDataToTarget(unsigned char *data, unsigned long dlength, char *cPort, unsigned char sspAddress, unsigned long long key)
{
 int i;
 unsigned long numCurBytes;
 unsigned short dBlockSize;
 struct ITL_FILE_DOWNLOAD *itlFile;
 struct SSP_COMMAND sspC;
 int port;
 if (download_in_progress == 1)
  return 0x100004;
 itlFile = malloc(sizeof(ITL_FILE_DOWNLOAD));
 itlFile->fData = malloc(dlength);
 memcpy(itlFile->fData, data, dlength);
 download_block = 0;
 if (itlFile->fData[0] == 'I' && itlFile->fData[1] == 'T' && itlFile->fData[2] == 'L') {
  numCurBytes = 0;
  for (i = 0; i < 4; i++) {
   numCurBytes += (unsigned long) itlFile->fData[17 + i] << (8 * (3 - i));
  }
  dBlockSize = (256 * (unsigned short) itlFile->fData[0x3e]) + (unsigned short) itlFile->fData[0x3f];
  if (dBlockSize == 0)
   dBlockSize = 4096;
  itlFile->NumberOfBlocks = numCurBytes / dBlockSize;
  if (numCurBytes % dBlockSize != 0)
   itlFile->NumberOfBlocks += 1;
 } else {
  free(itlFile->fData);
  free(itlFile);
  return 0x100003;
 }
 sspC.Timeout = 1000;
 sspC.BaudRate = 9600;
 sspC.RetryLevel = 2;
 sspC.SSPAddress = sspAddress;
 itlFile->SSPAddress = sspAddress;
 port = OpenSSPPort(cPort);
 strcpy(itlFile->portname, cPort);
 itlFile->port = port;
 if (port == -1) {
  free(itlFile->fData);
  free(itlFile);
  return 0x100004;
 }
 sspC.EncryptionStatus = 0;
 sspC.CommandDataLength = 1;
 sspC.CommandData[0] = 0x11;
 if (SSPSendCommand(port, &sspC) == 0) {
  CloseSSPPort(port);
  free(itlFile->fData);
  free(itlFile);
  return 0x100005;
 }
 if (sspC.ResponseData[0] != SSP_RESPONSE_OK) {
  CloseSSPPort(port);
  free(itlFile->fData);
  free(itlFile);
  return 0x100005;
 }
 if (key > 0) {
  if (NegotiateSSPEncryption(port, sspAddress, &itlFile->Key) == 0) {
   CloseSSPPort(port);
   free(itlFile->fData);
   free(itlFile);
   return 0x100005;
  }
  itlFile->EncryptionStatus = 1;
  itlFile->Key.FixedKey = key;
 }
 unsigned long thread;
 pthread_create(&thread, ((void *)0), (void *) DownloadITLTarget, (void *) itlFile);
 return itlFile->NumberOfBlocks;
}

variable_types:
data
unsigned char *

variable_types:
dlength
unsigned long

variable_types:
cPort
char *

variable_types:
sspAddress
unsigned char

variable_types:
key
unsigned long long

variable_types:
numCurBytes
unsigned long

variable_types:
dBlockSize
unsigned short

variable_types: struct ITL_FILE_DOWNLOAD { unsigned long NumberOfBlocks; unsigned long NumberOfRamBytes; unsigned char *fData; int port; unsigned long dwnlBlockSize; unsigned char SSPAddress; unsigned char EncryptionStatus; struct SSP_FULL_KEY { unsigned long long FixedKey; unsigned long long EncryptKey; } Key; char portname[255]; unsigned long baud; } *

variable_types: struct SSP_COMMAND { struct SSP_FULL_KEY { unsigned long long FixedKey; unsigned long long EncryptKey; } Key; unsigned long BaudRate; unsigned long Timeout; unsigned char PortNumber; unsigned char SSPAddress; unsigned char RetryLevel; unsigned char EncryptionStatus; unsigned char CommandDataLength; unsigned char CommandData[255]; unsigned char ResponseStatus; unsigned char ResponseDataLength; unsigned char ResponseData[255]; unsigned char IgnoreError; }

variable_types: port int

variable_types:
thread
unsigned long

user_defined_types: struct ITL_FILE_DOWNLOAD { unsigned long NumberOfBlocks; unsigned long NumberOfRamBytes; unsigned char *fData; int port; unsigned long dwnlBlockSize; unsigned char SSPAddress; unsigned char EncryptionStatus; struct SSP_FULL_KEY Key; char portname[255]; unsigned long baud; }


DownloadITLTarget

decompiled_code:
void __fastcall __noreturn DownloadITLTarget(void *a1)
{
unsigned __int8 *v1; // rdx
int i; // ecx
__int64 v3; // rbp
__m128i v4; // [rsp+0h] [rbp-248h] BYREF
__int64 v5; // [rsp+10h] [rbp-238h]
__int64 v6; // [rsp+18h] [rbp-230h]
char v7; // [rsp+21h] [rbp-227h]
char v8; // [rsp+22h] [rbp-226h]
char v9; // [rsp+23h] [rbp-225h]

download_in_progress = 1;
v6 = 1000LL;
v5 = 9600LL;
v8 = 2;
v7 = *((_BYTE *)a1 + 40);
v9 = 0;
if ( *((_BYTE *)a1 + 41) )
{
v9 = 1;
v4 = _mm_loadu_si128((const __m128i *)a1 + 3);
}
*((_QWORD *)a1 + 1) = 0LL;
v1 = (unsigned __int8 *)(*((_QWORD *)a1 + 2) + 7LL);
for ( i = 24; i != -8; i -= 8 )
*((_QWORD *)a1 + 1) += (unsigned __int64)*v1++ << i;
v3 = download_ram_file((__int64)a1, &v4);
if ( v3 == 0x100000 )
v3 = download_main_file((__int64)a1);
CloseSSPPort(*((unsigned int *)a1 + 6));
free(*((void **)a1 + 2));
free(a1);
download_in_progress = 0;
download_block = v3;
pthread_exit(0LL);
}

original_code:
void DownloadITLTarget(void *itl_file_pointer)
{
 ITL_FILE_DOWNLOAD *itlFile = 0;
 unsigned int i;
 unsigned long return_value;
 SSP_COMMAND sspC;
 itlFile = (ITL_FILE_DOWNLOAD *) itl_file_pointer;
 download_in_progress = 1;
 sspC.Timeout = 1000;
 sspC.BaudRate = 9600;
 sspC.RetryLevel = 2;
 sspC.SSPAddress = itlFile->SSPAddress;
 sspC.EncryptionStatus = 0;
 if (itlFile->EncryptionStatus) {
  sspC.EncryptionStatus = 1;
  sspC.Key = itlFile->Key;
 }
 itlFile->NumberOfRamBytes = 0;
 for (i = 0; i < 4; i++)
  itlFile->NumberOfRamBytes += (unsigned long) itlFile->fData[7 + i] << (8 * (3 - i));
 return_value = _download_ram_file(itlFile, &sspC);
 if (return_value == 0x100000) {
  return_value = _download_main_file(itlFile);
 }
 CloseSSPPort(itlFile->port);
 free(itlFile->fData);
 free(itlFile);
 download_in_progress = 0;
 download_block = return_value;
 pthread_exit(((void *)0));
}

canonical_original_code:
void DownloadITLTarget(void *itl_file_pointer)
{
 struct ITL_FILE_DOWNLOAD *itlFile = 0;
 unsigned int i;
 unsigned long return_value;
 struct SSP_COMMAND sspC;
 itlFile = (struct ITL_FILE_DOWNLOAD *) itl_file_pointer;
 download_in_progress = 1;
 sspC.Timeout = 1000;
 sspC.BaudRate = 9600;
 sspC.RetryLevel = 2;
 sspC.SSPAddress = itlFile->SSPAddress;
 sspC.EncryptionStatus = 0;
 if (itlFile->EncryptionStatus) {
  sspC.EncryptionStatus = 1;
  sspC.Key = itlFile->Key;
 }
 itlFile->NumberOfRamBytes = 0;
 for (i = 0; i < 4; i++)
  itlFile->NumberOfRamBytes += (unsigned long) itlFile->fData[7 + i] << (8 * (3 - i));
 return_value = _download_ram_file(itlFile, &sspC);
 if (return_value == 0x100000) {
  return_value = _download_main_file(itlFile);
 }
 CloseSSPPort(itlFile->port);
 free(itlFile->fData);
 free(itlFile);
 download_in_progress = 0;
 download_block = return_value;
 pthread_exit(((void *)0));
}

variable_types:
i
unsigned int


GetDownloadStatus

decompiled_code:
__int64 GetDownloadStatus()
{
return download_block;
}

canonical_decompiled_code:
__int64 func7()
{
return download_block;
}

original_code:
unsigned long GetDownloadStatus()
{
 return download_block;
}

return_type: unsigned long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // ebx
const char **v5; // rbx
const char *v6; // r13
const char *v7; // r12
const char *v8; // r12
char v10[512]; // [rsp+0h] [rbp-638h] BYREF
char dest[24]; // [rsp+200h] [rbp-438h] BYREF
__int64 v12; // [rsp+218h] [rbp-420h]
char v13[480]; // [rsp+220h] [rbp-418h] BYREF
char v14[16]; // [rsp+400h] [rbp-238h] BYREF
char v15[496]; // [rsp+410h] [rbp-228h] BYREF
struct timespec requested_time; // [rsp+600h] [rbp-38h] BYREF

strcpy(v14, "Verifying");
memset(v15, 0, sizeof(v15));
strcpy(dest, "transmission-remote ");
v12 = 0LL;
memset(v13, 0, sizeof(v13));
if ( argc <= 1 )
{
fwrite("Not enough arguments\n", 1uLL, 0x15uLL, _bss_start);
exit(1);
}
if ( argc == 2 )
{
v3 = atoi(argv[1]);
}
else
{
strcat(dest, argv[1]);
v3 = atoi(argv[2]);
if ( argc > 3 )
{
v5 = argv + 3;
v6 = 0LL;
do
{
v8 = *v5;
if ( **v5 == 45 && v8[1] )
{
v7 = v8 + 1;
if ( !strcmp("ne", v7) || !strcmp("-authenv", v7) )
{
strcpy(&dest[strlen(dest)], " -ne");
}
else if ( !strcmp("n", v7) || !strcmp("-auth", v7) )
{
*(_DWORD *)&dest[strlen(dest)] = 7220512;
v6 = v7;
}
}
else if ( v6 )
{
*(_WORD *)&dest[strlen(dest)] = 32;
strcat(dest, v8);
v6 = 0LL;
}
++v5;
}
while ( v5 != &argv[(unsigned int)(argc - 4) + 4] );
}
}
if ( v3 <= 0 )
{
fwrite("Torrent id is invalid\n", 1uLL, 0x16uLL, _bss_start);
exit(1);
}
snprintf(v10, 0x200uLL, "%s -t %d --verify > /dev/null", dest, (unsigned int)v3);
system(v10);
v4 = 0;
while ( !strcmp(v14, "Verifying") || !strcmp(v14, "Will Verify") )
{
v4 = checkStatus(v3, v14, dest);
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 5000000000LL;
nanosleep(&requested_time, 0LL);
}
if ( v4 > 99 )
{
strcpy(v14, "Complete");
}
else
{
snprintf(v10, 0x200uLL, "%s -t %d -s > /dev/null", dest, (unsigned int)v3);
system(v10);
checkStatus(v3, v14, dest);
}
printf("%s", v14);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // ebx
const char **v5; // rbx
const char *v6; // r13
const char *v7; // r12
const char *v8; // r12
char v10[512]; // [rsp+0h] [rbp-638h] BYREF
char dest[24]; // [rsp+200h] [rbp-438h] BYREF
__int64 v12; // [rsp+218h] [rbp-420h]
char v13[480]; // [rsp+220h] [rbp-418h] BYREF
char v14[16]; // [rsp+400h] [rbp-238h] BYREF
char v15[496]; // [rsp+410h] [rbp-228h] BYREF
struct timespec requested_time; // [rsp+600h] [rbp-38h] BYREF

strcpy(v14, "Verifying");
memset(v15, 0, sizeof(v15));
strcpy(dest, "transmission-remote ");
v12 = 0LL;
memset(v13, 0, sizeof(v13));
if ( argc <= 1 )
{
fwrite("Not enough arguments\n", 1uLL, 0x15uLL, _bss_start);
exit(1);
}
if ( argc == 2 )
{
v3 = atoi(argv[1]);
}
else
{
strcat(dest, argv[1]);
v3 = atoi(argv[2]);
if ( argc > 3 )
{
v5 = argv + 3;
v6 = 0LL;
do
{
v8 = *v5;
if ( **v5 == 45 && v8[1] )
{
v7 = v8 + 1;
if ( !strcmp("ne", v7) || !strcmp("-authenv", v7) )
{
strcpy(&dest[strlen(dest)], " -ne");
}
else if ( !strcmp("n", v7) || !strcmp("-auth", v7) )
{
*(_DWORD *)&dest[strlen(dest)] = 7220512;
v6 = v7;
}
}
else if ( v6 )
{
*(_WORD *)&dest[strlen(dest)] = 32;
strcat(dest, v8);
v6 = 0LL;
}
++v5;
}
while ( v5 != &argv[(unsigned int)(argc - 4) + 4] );
}
}
if ( v3 <= 0 )
{
fwrite("Torrent id is invalid\n", 1uLL, 0x16uLL, _bss_start);
exit(1);
}
snprintf(v10, 0x200uLL, "%s -t %d --verify > /dev/null", dest, (unsigned int)v3);
system(v10);
v4 = 0;
while ( !strcmp(v14, "Verifying") || !strcmp(v14, "Will Verify") )
{
v4 = func8(v3, v14, dest);
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 5000000000LL;
nanosleep(&requested_time, 0LL);
}
if ( v4 > 99 )
{
strcpy(v14, "Complete");
}
else
{
snprintf(v10, 0x200uLL, "%s -t %d -s > /dev/null", dest, (unsigned int)v3);
system(v10);
func8(v3, v14, dest);
}
printf("%s", v14);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
  int tid = 0;
  int done = 0;
  char status[512] = "Verifying";
  char transBuf[512] = "transmission-remote ";
  char cmdBuf[512];
  if (argc < 2)
  {
    fprintf(stderr, "Not enough arguments\n");
    exit(1);
  }
  else if (argc == 2)
  {
    tid = atoi(argv[1]);
  }
  else
  {
    strcat(transBuf, argv[1]);
    tid = atoi(argv[2]);
    if (argc > 3)
    {
      int i;
      char * lastOpt = ((void *)0);
      for (i = 3; i < argc; ++i)
      {
        char * opt = argv[i];
        if (*(opt++) == '-' && *opt != '\0')
        {
          if (!strcmp("ne", opt) || !strcmp("-authenv", opt))
          {
            strcat(transBuf, " -ne");
          }
          else if (!strcmp("n", opt) || !strcmp("-auth", opt))
          {
            strcat(transBuf, " -n");
            lastOpt = opt;
          }
        }
        else if (lastOpt)
        {
          strcat(transBuf, " ");
          strcat(transBuf, (--opt));
          lastOpt = ((void *)0);
        }
      }
    }
  }
  if (tid <= 0)
  {
    fprintf(stderr, "Torrent id is invalid\n");
    exit(1);
  }
  snprintf(cmdBuf, 512, "%s -t %d --verify > /dev/null", transBuf, tid);
  system(cmdBuf);
  while (!strcmp(status, "Verifying") || !strcmp(status, "Will Verify"))
  {
    done = checkStatus(tid, status, transBuf);
    nanosleep((struct timespec[]){{0, (long)5 * 1000000000}}, ((void *)0));
  }
  if (done < 100)
  {
    snprintf(cmdBuf, 512, "%s -t %d -s > /dev/null", transBuf, tid);
    system(cmdBuf);
    checkStatus(tid, status, transBuf);
  }
  else
  {
    strcpy(status, "Complete");
  }
  printf("%s", status);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int tid = 0;
  int done = 0;
  char status[512] = "Verifying";
  char transBuf[512] = "transmission-remote ";
  char cmdBuf[512];
  if (argc < 2)
  {
    fprintf(stderr, "Not enough arguments\n");
    exit(1);
  }
  else if (argc == 2)
  {
    tid = atoi(argv[1]);
  }
  else
  {
    strcat(transBuf, argv[1]);
    tid = atoi(argv[2]);
    if (argc > 3)
    {
      int i;
      char *lastOpt = ((void *)0);
      for (i = 3; i < argc; ++i)
      {
        char *opt = argv[i];
        if (*(opt++) == '-' && *opt != '\0')
        {
          if (!strcmp("ne", opt) || !strcmp("-authenv", opt))
          {
            strcat(transBuf, " -ne");
          }
          else if (!strcmp("n", opt) || !strcmp("-auth", opt))
          {
            strcat(transBuf, " -n");
            lastOpt = opt;
          }
        }
        else if (lastOpt)
        {
          strcat(transBuf, " ");
          strcat(transBuf, (--opt));
          lastOpt = ((void *)0);
        }
      }
    }
  }
  if (tid <= 0)
  {
    fprintf(stderr, "Torrent id is invalid\n");
    exit(1);
  }
  snprintf(cmdBuf, 512, "%s -t %d --verify > /dev/null", transBuf, tid);
  system(cmdBuf);
  while (!strcmp(status, "Verifying") || !strcmp(status, "Will Verify"))
  {
    done = checkStatus(tid, status, transBuf);
    nanosleep((struct timespec []){{0, (long)5 * 1000000000}}, ((void *)0));
  }
  if (done < 100)
  {
    snprintf(cmdBuf, 512, "%s -t %d -s > /dev/null", transBuf, tid);
    system(cmdBuf);
    checkStatus(tid, status, transBuf);
  }
  else
  {
    strcpy(status, "Complete");
  }
  printf("%s", status);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: tid int

variable_types: done int

variable_types:
status
char [512]

variable_types:
transBuf
char [512]

variable_types:
cmdBuf
char [512]

variable_types: i int

variable_types:
lastOpt
char *

variable_types:
opt
char *

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


repo: sjas/sandbox-c

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("Hallo Welt");
return 0;
}

original_code:
int main(void)
{
    printf("Hallo Welt\n");
}

canonical_original_code:
int main()
{
    printf("Hallo Welt\n");
}


UD

original_code:
int UD(int min, int max){
 return((int)floor((double)(min + (max - min + 1)*((float)rand()/(float)2147483647))));
}

variable_types: min int

variable_types: max int


barber

decompiled_code:
void __fastcall barber(void *a1)
{
unsigned int v1; // eax

while ( !done_with_all_customers )
{
puts("Barber: Sleeping ");
pthread_mutex_lock(&barber_state);
pthread_cond_wait(&wake_up, &barber_state);
pthread_mutex_unlock(&barber_state);
if ( done_with_all_customers )
{
puts("Barber: Done for the day. Going home ");
}
else
{
puts("Barber: Cutting hair ");
v1 = UD(4, 8);
sleep(v1);
puts("Barber: Done cut ");
pthread_cond_signal(&done_cut);
}
}
}

canonical_decompiled_code:
void __fastcall func8(void *a1)
{
unsigned int v1; // eax

while ( !done_with_all_customers )
{
puts("Barber: Sleeping ");
pthread_mutex_lock(&barber_state);
pthread_cond_wait(&wake_up, &barber_state);
pthread_mutex_unlock(&barber_state);
if ( done_with_all_customers )
{
puts("Barber: Done for the day. Going home ");
}
else
{
puts("Barber: Cutting hair ");
v1 = func7(4, 8);
sleep(v1);
puts("Barber: Done cut ");
pthread_cond_signal(&done_cut);
}
}
}

original_code:
void *barber(void *arg){
 while(!done_with_all_customers){
  printf("Barber: Sleeping \n");
  pthread_mutex_lock( &barber_state );
  pthread_cond_wait( &wake_up, &barber_state );
  pthread_mutex_unlock( &barber_state );
  if(!done_with_all_customers){
   printf("Barber: Cutting hair \n");
   int waitTime = UD(4, 8);
   sleep(waitTime);
   printf("Barber: Done cut \n");
   pthread_cond_signal( &done_cut );
  }
  else{
   printf("Barber: Done for the day. Going home \n");
  }
 }
}

variable_types:
arg
void *

variable_types: waitTime int

return_type: void *


customer

decompiled_code:
int __fastcall customer(void *a1)
{
unsigned int v1; // ebx
unsigned int v2; // eax

v1 = *(_DWORD *)a1;
printf("Customer %d: Leaving for the barber shop \n", *(unsigned int *)a1);
v2 = UD(4, 8);
sleep(v2);
printf("Customer %d: Arrived at the barber shop \n", v1);
pthread_mutex_lock(&number_seats);
if ( !num_waiting_chairs )
pthread_cond_wait(&seats_available, &number_seats);
--num_waiting_chairs;
pthread_mutex_unlock(&number_seats);
printf("Customer %d: Entering waiting room \n", v1);
pthread_mutex_lock(&barber_chair);
if ( b_chair_taken )
pthread_cond_wait(&barber_free, &barber_chair);
b_chair_taken = 1;
pthread_mutex_unlock(&barber_chair);
pthread_mutex_lock(&number_seats);
++num_waiting_chairs;
pthread_mutex_unlock(&number_seats);
pthread_cond_signal(&seats_available);
pthread_cond_signal(&wake_up);
printf("Customer %d: Getting cut \n", v1);
pthread_mutex_lock(&finished);
pthread_cond_wait(&done_cut, &finished);
all_done = 0;
pthread_mutex_unlock(&finished);
pthread_mutex_lock(&barber_chair);
b_chair_taken = 0;
pthread_mutex_unlock(&barber_chair);
pthread_cond_signal(&barber_free);
return printf("Customer %d: Going home \n", v1);
}

canonical_decompiled_code:
int __fastcall func9(void *a1)
{
unsigned int v1; // ebx
unsigned int v2; // eax

v1 = *(_DWORD *)a1;
printf("Customer %d: Leaving for the barber shop \n", *(unsigned int *)a1);
v2 = func7(4, 8);
sleep(v2);
printf("Customer %d: Arrived at the barber shop \n", v1);
pthread_mutex_lock(&number_seats);
if ( !num_waiting_chairs )
pthread_cond_wait(&seats_available, &number_seats);
--num_waiting_chairs;
pthread_mutex_unlock(&number_seats);
printf("Customer %d: Entering waiting room \n", v1);
pthread_mutex_lock(&barber_chair);
if ( b_chair_taken )
pthread_cond_wait(&barber_free, &barber_chair);
b_chair_taken = 1;
pthread_mutex_unlock(&barber_chair);
pthread_mutex_lock(&number_seats);
++num_waiting_chairs;
pthread_mutex_unlock(&number_seats);
pthread_cond_signal(&seats_available);
pthread_cond_signal(&wake_up);
printf("Customer %d: Getting cut \n", v1);
pthread_mutex_lock(&finished);
pthread_cond_wait(&done_cut, &finished);
all_done = 0;
pthread_mutex_unlock(&finished);
pthread_mutex_lock(&barber_chair);
b_chair_taken = 0;
pthread_mutex_unlock(&barber_chair);
pthread_cond_signal(&barber_free);
return printf("Customer %d: Going home \n", v1);
}

original_code:
void *customer(void *customerNumber){
 int number = *(int *)customerNumber;
 printf("Customer %d: Leaving for the barber shop \n", number);
 int waitTime = UD(4, 8);
 sleep(waitTime);
 printf("Customer %d: Arrived at the barber shop \n", number);
 pthread_mutex_lock( &number_seats );
 if( num_waiting_chairs == 0 )
  pthread_cond_wait( &seats_available, &number_seats);
 num_waiting_chairs--;
 pthread_mutex_unlock( &number_seats );
 printf("Customer %d: Entering waiting room \n", number);
 pthread_mutex_lock( &barber_chair );
 if( b_chair_taken )
  pthread_cond_wait( &barber_free, &barber_chair );
 b_chair_taken = 1;
 pthread_mutex_unlock( &barber_chair );
 pthread_mutex_lock( &number_seats );
 num_waiting_chairs++;
 pthread_mutex_unlock( &number_seats );
 pthread_cond_signal( &seats_available );
 pthread_cond_signal( &wake_up );
 printf("Customer %d: Getting cut \n", number);
 pthread_mutex_lock( &finished );
 if( !all_done );
  pthread_cond_wait( &done_cut, &finished );
 all_done = 0;
 pthread_mutex_unlock( &finished );
 pthread_mutex_lock( &barber_chair );
 b_chair_taken = 0;
 pthread_mutex_unlock( &barber_chair );
 pthread_cond_signal( &barber_free );
 printf("Customer %d: Going home \n", number);
}

variable_types:
customerNumber
void *

variable_types: number int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // rbx
__int64 v6; // rbx
int arg[52]; // [rsp+0h] [rbp-288h] BYREF
pthread_t th[51]; // [rsp+D0h] [rbp-1B8h] BYREF
pthread_t newthread[4]; // [rsp+268h] [rbp-20h] BYREF

if ( argc <= 2 )
{
puts("Usage: sleeping_barber <customers> <chairs> ");
exit(0);
}
v3 = atoi(argv[1]);
num_waiting_chairs = atoi(argv[2]);
v4 = time(0LL);
srand(v4);
if ( v3 > 50 )
{
puts("Number of customers exceeds the maximum capacity of the barber ");
printf("Resetting the number of customers to %d \n", 50LL);
pthread_cond_init(&seats_available, 0LL);
pthread_cond_init(&barber_free, 0LL);
pthread_cond_init(&wake_up, 0LL);
pthread_cond_init(&done_cut, 0LL);
pthread_mutex_init(&barber_chair, 0LL);
pthread_mutex_init(&number_seats, 0LL);
pthread_mutex_init(&finished, 0LL);
pthread_mutex_init(&barber_state, 0LL);
pthread_create(newthread, 0LL, barber, 0LL);
v3 = 50;
}
else
{
pthread_cond_init(&seats_available, 0LL);
pthread_cond_init(&barber_free, 0LL);
pthread_cond_init(&wake_up, 0LL);
pthread_cond_init(&done_cut, 0LL);
pthread_mutex_init(&barber_chair, 0LL);
pthread_mutex_init(&number_seats, 0LL);
pthread_mutex_init(&finished, 0LL);
pthread_mutex_init(&barber_state, 0LL);
pthread_create(newthread, 0LL, barber, 0LL);
if ( v3 <= 0 )
goto LABEL_9;
}
v5 = 0LL;
do
{
arg[v5] = v5;
pthread_create(&th[v5], 0LL, customer, &arg[v5]);
++v5;
}
while ( v3 > (int)v5 );
v6 = 0LL;
do
pthread_join(th[v6++], 0LL);
while ( v3 > (int)v6 );
LABEL_9:
done_with_all_customers = 1;
pthread_cond_signal(&wake_up);
pthread_join(newthread[0], 0LL);
return 0;
}

original_code:
int main(int argc, char **argv){
 if(argc < 3){
  printf("Usage: sleeping_barber <customers> <chairs> \n");
  exit(0);
 }
 int num_customers = atoi(argv[1]);
 num_waiting_chairs = atoi(argv[2]);
 srand((long)time(((void *)0)));
 if(num_customers > 50){
  printf("Number of customers exceeds the maximum capacity of the barber \n");
  printf("Resetting the number of customers to %d \n", (int)50);
  num_customers = 50;
 }
 pthread_cond_init( &seats_available, ((void *)0) );
 pthread_cond_init( &barber_free, ((void *)0) );
 pthread_cond_init( &wake_up, ((void *)0) );
 pthread_cond_init( &done_cut, ((void *)0) );
 pthread_mutex_init( &barber_chair, ((void *)0) );
 pthread_mutex_init( &number_seats, ((void *)0) );
 pthread_mutex_init( &finished, ((void *)0) );
 pthread_mutex_init( &barber_state, ((void *)0) );
 pthread_t btid;
 pthread_t tid[50];
 pthread_create(&btid, 0, barber, 0);
 int customer_ID[50];
 int i;
 for(i = 0; i < num_customers; i++){
  customer_ID[i] = i;
  pthread_create(&tid[i], 0, customer, &customer_ID[i]);
 }
 for(i = 0; i < num_customers; i++)
  pthread_join(tid[i], 0);
 done_with_all_customers = 1;
 pthread_cond_signal( &wake_up );
 pthread_join(btid, 0);
}

canonical_original_code:
int main(int argc, char **argv){
 if(argc < 3){
  printf("Usage: sleeping_barber <customers> <chairs> \n");
  exit(0);
 }
 int num_customers = atoi(argv[1]);
 num_waiting_chairs = atoi(argv[2]);
 srand((long)time(((void *)0)));
 if(num_customers > 50){
  printf("Number of customers exceeds the maximum capacity of the barber \n");
  printf("Resetting the number of customers to %d \n", (int)50);
  num_customers = 50;
 }
 pthread_cond_init( &seats_available, ((void *)0) );
 pthread_cond_init( &barber_free, ((void *)0) );
 pthread_cond_init( &wake_up, ((void *)0) );
 pthread_cond_init( &done_cut, ((void *)0) );
 pthread_mutex_init( &barber_chair, ((void *)0) );
 pthread_mutex_init( &number_seats, ((void *)0) );
 pthread_mutex_init( &finished, ((void *)0) );
 pthread_mutex_init( &barber_state, ((void *)0) );
 unsigned long btid;
 unsigned long tid[50];
 pthread_create(&btid, 0, barber, 0);
 int customer_ID[50];
 int i;
 for(i = 0; i < num_customers; i++){
  customer_ID[i] = i;
  pthread_create(&tid[i], 0, customer, &customer_ID[i]);
 }
 for(i = 0; i < num_customers; i++)
  pthread_join(tid[i], 0);
 done_with_all_customers = 1;
 pthread_cond_signal( &wake_up );
 pthread_join(btid, 0);
}

variable_types:
argv
char **

variable_types:
btid
unsigned long

variable_types:
tid
unsigned long [50]


getErrmsg

repo: sjchao/xtrace

decompiled_code:
void __fastcall getErrmsg(_QWORD *a1)
{
const char *v1; // rbp
unsigned int v2; // eax

if ( a1 )
{
switch ( *__errno_location() )
{
case 1:
v1 = "EPERM: the specified process cannot be tarced, maybe you need more privileges.";
break;
case 3:
v1 = "ESRCH: the specified process does not exist.";
break;
case 5:
v1 = "EIO: request is invalid.";
break;
case 14:
v1 = "EFAULT: attempt to read from or write to an invalid area.";
break;
case 16:
v1 = "EBUSY: error with allocating or freeing a debug register.";
break;
case 22:
v1 = "EINVAL: attemp to set an invalid option.";
break;
default:
v1 = "Unknown error";
break;
}
v2 = strlen(v1);
*a1 = *(_QWORD *)v1;
*(_QWORD *)((char *)a1 + v2 - 8) = *(_QWORD *)&v1[v2 - 8];
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v1 - ((const char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * ((v2 + (_DWORD)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8)) >> 3));
}
}

canonical_decompiled_code:
void __fastcall func7(_QWORD *a1)
{
const char *v1; // rbp
unsigned int v2; // eax

if ( a1 )
{
switch ( *__errno_location() )
{
case 1:
v1 = "EPERM: the specified process cannot be tarced, maybe you need more privileges.";
break;
case 3:
v1 = "ESRCH: the specified process does not exist.";
break;
case 5:
v1 = "EIO: request is invalid.";
break;
case 14:
v1 = "EFAULT: attempt to read from or write to an invalid area.";
break;
case 16:
v1 = "EBUSY: error with allocating or freeing a debug register.";
break;
case 22:
v1 = "EINVAL: attemp to set an invalid option.";
break;
default:
v1 = "Unknown error";
break;
}
v2 = strlen(v1);
*a1 = *(_QWORD *)v1;
*(_QWORD *)((char *)a1 + v2 - 8) = *(_QWORD *)&v1[v2 - 8];
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v1 - ((const char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * ((v2 + (_DWORD)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8)) >> 3));
}
}

original_code:
void getErrmsg(char* errmsg) {
  if (errmsg == ((void *)0)) {
    return;
  }
  char* msg;
  switch ((*__errno_location ())) {
    case 16:
      msg = "EBUSY: error with allocating or freeing a debug register.";
      break;
    case 14:
      msg = "EFAULT: attempt to read from or write to an invalid area.";
      break;
    case 22:
      msg = "EINVAL: attemp to set an invalid option.";
      break;
    case 5:
      msg = "EIO: request is invalid.";
      break;
    case 1:
      msg = "EPERM: the specified process cannot be tarced, maybe you need more privileges.";
      break;
    case 3:
      msg = "ESRCH: the specified process does not exist.";
      break;
    default:
      msg = "Unknown error";
      break;
  }
  memcpy(errmsg, msg, strlen(msg));
}

canonical_original_code:
void getErrmsg(char *errmsg) {
  if (errmsg == ((void *)0)) {
    return;
  }
  char *msg;
  switch ((*__errno_location ())) {
    case 16:
      msg = "EBUSY: error with allocating or freeing a debug register.";
      break;
    case 14:
      msg = "EFAULT: attempt to read from or write to an invalid area.";
      break;
    case 22:
      msg = "EINVAL: attemp to set an invalid option.";
      break;
    case 5:
      msg = "EIO: request is invalid.";
      break;
    case 1:
      msg = "EPERM: the specified process cannot be tarced, maybe you need more privileges.";
      break;
    case 3:
      msg = "ESRCH: the specified process does not exist.";
      break;
    default:
      msg = "Unknown error";
      break;
  }
  memcpy(errmsg, msg, strlen(msg));
}

variable_types:
errmsg
char *

variable_types:
msg
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int result; // eax
_QWORD v5[2]; // [rsp+0h] [rbp-98h] BYREF
unsigned int v6; // [rsp+10h] [rbp-88h]
unsigned int v7; // [rsp+14h] [rbp-84h]
int stat_loc[3]; // [rsp+8Ch] [rbp-Ch] BYREF

if ( argc <= 1 )
{
puts("Usage: xtrace pid");
result = -1;
}
else
{
v3 = (unsigned int)atoi(argv[1]);
if ( ptrace(PTRACE_ATTACH, v3, 0LL, 0LL) )
{
getErrmsg(v5);
printf("Failed to attach to process %d\n\t%s\n", (unsigned int)v3, (const char *)v5);
result = -1;
}
else
{
printf("Attach to process %d\n", (unsigned int)v3);
ptrace(PTRACE_CONT, (unsigned int)v3, 0LL, 0LL);
do
{
printf("Wait process %d signal...\n", (unsigned int)v3);
waitpid(v3, stat_loc, 0);
}
while ( LOBYTE(stat_loc[0]) != 127 );
ptrace(PTRACE_GETSIGINFO, (unsigned int)v3, 0LL, v5);
printf("Receive signal\n\tsigno:%d pid:%d uid:%d\n", LODWORD(v5[0]), v6, v7);
ptrace(PTRACE_CONT, (unsigned int)v3, 0LL, LODWORD(v5[0]));
ptrace(PTRACE_DETACH, (unsigned int)v3, 0LL, 0LL);
printf("Detach from process %d\n", (unsigned int)v3);
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int result; // eax
_QWORD v5[2]; // [rsp+0h] [rbp-98h] BYREF
unsigned int v6; // [rsp+10h] [rbp-88h]
unsigned int v7; // [rsp+14h] [rbp-84h]
int stat_loc[3]; // [rsp+8Ch] [rbp-Ch] BYREF

if ( argc <= 1 )
{
puts("Usage: xtrace pid");
result = -1;
}
else
{
v3 = (unsigned int)atoi(argv[1]);
if ( ptrace(PTRACE_ATTACH, v3, 0LL, 0LL) )
{
func7(v5);
printf("Failed to attach to process %d\n\t%s\n", (unsigned int)v3, (const char *)v5);
result = -1;
}
else
{
printf("Attach to process %d\n", (unsigned int)v3);
ptrace(PTRACE_CONT, (unsigned int)v3, 0LL, 0LL);
do
{
printf("Wait process %d signal...\n", (unsigned int)v3);
waitpid(v3, stat_loc, 0);
}
while ( LOBYTE(stat_loc[0]) != 127 );
ptrace(PTRACE_GETSIGINFO, (unsigned int)v3, 0LL, v5);
printf("Receive signal\n\tsigno:%d pid:%d uid:%d\n", LODWORD(v5[0]), v6, v7);
ptrace(PTRACE_CONT, (unsigned int)v3, 0LL, LODWORD(v5[0]));
ptrace(PTRACE_DETACH, (unsigned int)v3, 0LL, 0LL);
printf("Detach from process %d\n", (unsigned int)v3);
result = 0;
}
}
return result;
}

original_code:
int main(int argc, char** argv) {
  if (argc <= 1) {
    printf("Usage: xtrace pid\n");
    return -1;
  }
  int pid = atoi(argv[1]);
  long ret = ptrace(PTRACE_ATTACH, pid, ((void *)0), ((void *)0));
  if (ret != 0) {
    char msg[100];
    getErrmsg(msg);
    printf("Failed to attach to process %d\n\t%s\n", pid, msg);
    return -1;
  }
  printf("Attach to process %d\n", pid);
  ptrace(PTRACE_CONT, pid, ((void *)0), ((void *)0));
  int status;
  while (1) {
    printf("Wait process %d signal...\n", pid);
    waitpid(pid, &status, 0);
    if ((((status) & 0xff) == 0x7f)) {
      siginfo_t siginfo;
      ptrace(PTRACE_GETSIGINFO, pid, ((void *)0), &siginfo);
      printf("Receive signal\n\tsigno:%d pid:%d uid:%d\n", siginfo.si_signo, siginfo._sifields._kill.si_pid, siginfo._sifields._kill.si_uid);
      ptrace(PTRACE_CONT, pid, ((void *)0), siginfo.si_signo);
      break;
    }
  }
  ptrace(PTRACE_DETACH, pid, ((void *)0), ((void *)0));
  printf("Detach from process %d\n", pid);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  if (argc <= 1) {
    printf("Usage: xtrace pid\n");
    return -1;
  }
  int pid = atoi(argv[1]);
  long ret = ptrace(PTRACE_ATTACH, pid, ((void *)0), ((void *)0));
  if (ret != 0) {
    char msg[100];
    getErrmsg(msg);
    printf("Failed to attach to process %d\n\t%s\n", pid, msg);
    return -1;
  }
  printf("Attach to process %d\n", pid);
  ptrace(PTRACE_CONT, pid, ((void *)0), ((void *)0));
  int status;
  while (1) {
    printf("Wait process %d signal...\n", pid);
    waitpid(pid, &status, 0);
    if ((((status) & 0xff) == 0x7f)) {
      struct siginfo_t siginfo;
      ptrace(PTRACE_GETSIGINFO, pid, ((void *)0), &siginfo);
      printf("Receive signal\n\tsigno:%d pid:%d uid:%d\n", siginfo.si_signo, siginfo._sifields._kill.si_pid, siginfo._sifields._kill.si_uid);
      ptrace(PTRACE_CONT, pid, ((void *)0), siginfo.si_signo);
      break;
    }
  }
  ptrace(PTRACE_DETACH, pid, ((void *)0), ((void *)0));
  printf("Detach from process %d\n", pid);
  return 0;
}

variable_types: argc int

variable_types: pid int

variable_types: ret long

variable_types:
msg
char [100]

variable_types: status int

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

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


repo: sjkingo/acpi-brightness

decompiled_code:
__int64 get_brightness()
{
int v0; // eax
int v1; // ebx
__int64 v3[11]; // [rsp+0h] [rbp-58h] BYREF

v3[9] = __readfsqword(0x28u);
v0 = open("/sys/class/backlight/acpi_video0/brightness", 0);
if ( v0 < 0 )
{
perror("open(/sys/class/backlight/acpi_video0/brightness)");
fwrite("Check the ACPI driver for your backlight\n", 1uLL, 0x29uLL, stderr);
exit(2);
}
v1 = v0;
v3[0] = 0LL;
v3[1] = 0LL;
v3[2] = 0LL;
v3[3] = 0LL;
v3[4] = 0LL;
v3[5] = 0LL;
v3[6] = 0LL;
v3[7] = 0LL;
if ( read(v0, v3, 0x40uLL) < 0 )
{
perror("read");
exit(2);
}
close(v1);
return strtol((const char *)v3, 0LL, 10);
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax
int v1; // ebx
__int64 v3[11]; // [rsp+0h] [rbp-58h] BYREF

v3[9] = __readfsqword(0x28u);
v0 = open("/sys/class/backlight/acpi_video0/brightness", 0);
if ( v0 < 0 )
{
perror("open(/sys/class/backlight/acpi_video0/brightness)");
fwrite("Check the ACPI driver for your backlight\n", 1uLL, 0x29uLL, stderr);
exit(2);
}
v1 = v0;
v3[0] = 0LL;
v3[1] = 0LL;
v3[2] = 0LL;
v3[3] = 0LL;
v3[4] = 0LL;
v3[5] = 0LL;
v3[6] = 0LL;
v3[7] = 0LL;
if ( read(v0, v3, 0x40uLL) < 0 )
{
perror("read");
exit(2);
}
close(v1);
return strtol((const char *)v3, 0LL, 10);
}

original_code:
static int get_brightness(void) {
    int fd = open("/sys/class/backlight/acpi_video0/brightness", 00);
    if (fd < 0) {
        perror("open(" "/sys/class/backlight/acpi_video0/brightness" ")");
        fprintf(stderr, "Check the ACPI driver for your backlight\n");
        exit(2);
    }
    char buf[64];
    memset(buf, 0, sizeof(buf));
    if (read(fd, buf, sizeof(buf)) < 0) {
        perror("read");
        exit(2);
    }
    close(fd);
    int level = strtol(buf, ((void *)0), 10);
    return level;
}

canonical_original_code:
static int get_brightness() {
    int fd = open("/sys/class/backlight/acpi_video0/brightness", 00);
    if (fd < 0) {
        perror("open(" "/sys/class/backlight/acpi_video0/brightness" ")");
        fprintf(stderr, "Check the ACPI driver for your backlight\n");
        exit(2);
    }
    char buf[64];
    memset(buf, 0, sizeof(buf));
    if (read(fd, buf, sizeof(buf)) < 0) {
        perror("read");
        exit(2);
    }
    close(fd);
    int level = strtol(buf, ((void *)0), 10);
    return level;
}

variable_types: fd int

variable_types:
buf
char [64]

variable_types: level int


decompiled_code:
unsigned __int64 __fastcall set_brightness(unsigned int a1)
{
int v1; // ebx
int v2; // eax
__int64 v4[9]; // [rsp+0h] [rbp-68h] BYREF
unsigned __int64 v5; // [rsp+48h] [rbp-20h]

v5 = __readfsqword(0x28u);
if ( a1 > 0xA )
{
fprintf(stderr, "Error: brightness level not in the range [%d..%d]\n", 0LL, 10LL);
exit(2);
}
v1 = open("/sys/class/backlight/acpi_video0/brightness", 1);
if ( v1 < 0 )
{
perror("open(/sys/class/backlight/acpi_video0/brightness)");
fwrite("Check the ACPI driver for your backlight\n", 1uLL, 0x29uLL, stderr);
exit(2);
}
v4[0] = 0LL;
v4[1] = 0LL;
v4[2] = 0LL;
v4[3] = 0LL;
v4[4] = 0LL;
v4[5] = 0LL;
v4[6] = 0LL;
v4[7] = 0LL;
v2 = snprintf((char *)v4, 0x40uLL, "%d", a1);
if ( write(v1, v4, v2) < 0 )
{
if ( *__errno_location() == 22 )
fwrite("Error: invalid brightness given\n", 1uLL, 0x20uLL, stderr);
else
perror("write");
exit(2);
}
close(v1);
return v5 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(unsigned int a1)
{
int v1; // ebx
int v2; // eax
__int64 v4[9]; // [rsp+0h] [rbp-68h] BYREF
unsigned __int64 v5; // [rsp+48h] [rbp-20h]

v5 = __readfsqword(0x28u);
if ( a1 > 0xA )
{
fprintf(stderr, "Error: brightness level not in the range [%d..%d]\n", 0LL, 10LL);
exit(2);
}
v1 = open("/sys/class/backlight/acpi_video0/brightness", 1);
if ( v1 < 0 )
{
perror("open(/sys/class/backlight/acpi_video0/brightness)");
fwrite("Check the ACPI driver for your backlight\n", 1uLL, 0x29uLL, stderr);
exit(2);
}
v4[0] = 0LL;
v4[1] = 0LL;
v4[2] = 0LL;
v4[3] = 0LL;
v4[4] = 0LL;
v4[5] = 0LL;
v4[6] = 0LL;
v4[7] = 0LL;
v2 = snprintf((char *)v4, 0x40uLL, "%d", a1);
if ( write(v1, v4, v2) < 0 )
{
if ( *__errno_location() == 22 )
fwrite("Error: invalid brightness given\n", 1uLL, 0x20uLL, stderr);
else
perror("write");
exit(2);
}
close(v1);
return v5 - __readfsqword(0x28u);
}

original_code:
static void set_brightness(int level) {
    if (level < 0 || level > 10) {
        fprintf(stderr, "Error: brightness level not in the range [%d..%d]\n",
                0, 10);
        exit(2);
    }
    int fd = open("/sys/class/backlight/acpi_video0/brightness", 01);
    if (fd < 0) {
        perror("open(" "/sys/class/backlight/acpi_video0/brightness" ")");
        fprintf(stderr, "Check the ACPI driver for your backlight\n");
        exit(2);
    }
    char buf[64];
    memset(buf, 0, sizeof(buf));
    int len = snprintf(buf, sizeof(buf), "%d", level);
    if (write(fd, buf, len) < 0) {
        int e = (*__errno_location ());
        if (e == 22) {
            fprintf(stderr, "Error: invalid brightness given\n");
        } else {
            perror("write");
        }
        exit(2);
    }
    close(fd);
}

variable_types: len int

variable_types: e int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int64 v4; // rdx
int v5; // ebx
__int64 v6; // rdx
signed int v7; // edi
unsigned int v8; // eax

if ( argc == 1 )
{
v8 = get_brightness(argc, argv, envp);
printf("%d\n", v8);
v5 = 0;
}
else
{
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s [<value>|up|down|max|min]\n", *argv);
return 1;
}
v3 = argv[1];
v5 = strcmp(v3, "up");
if ( v5 )
{
v5 = strcmp(v3, "down");
if ( v5 )
{
v7 = 10;
if ( strcmp(v3, "max") )
{
v7 = strcmp(v3, "min");
if ( v7 )
v7 = strtol(v3, 0LL, 10);
}
goto LABEL_12;
}
v7 = get_brightness(v3, "down", v6) - 1;
if ( v7 >= 0 )
{
LABEL_12:
set_brightness(v7);
return 0;
}
}
else
{
v7 = get_brightness(v3, "up", v4) + 1;
if ( v7 <= 10 )
goto LABEL_12;
}
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int64 v4; // rdx
int v5; // ebx
__int64 v6; // rdx
signed int v7; // edi
unsigned int v8; // eax

if ( argc == 1 )
{
v8 = func7(argc, argv, envp);
printf("%d\n", v8);
v5 = 0;
}
else
{
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s [<value>|up|down|max|min]\n", *argv);
return 1;
}
v3 = argv[1];
v5 = strcmp(v3, "up");
if ( v5 )
{
v5 = strcmp(v3, "down");
if ( v5 )
{
v7 = 10;
if ( strcmp(v3, "max") )
{
v7 = strcmp(v3, "min");
if ( v7 )
v7 = strtol(v3, 0LL, 10);
}
goto LABEL_12;
}
v7 = func7(v3, "down", v6) - 1;
if ( v7 >= 0 )
{
LABEL_12:
func8(v7);
return 0;
}
}
else
{
v7 = func7(v3, "up", v4) + 1;
if ( v7 <= 10 )
goto LABEL_12;
}
}
return v5;
}

original_code:
int main(int argc, char **argv) {
    if (argc == 1) {
        int level = get_brightness();
        printf("%d\n", level);
        return 0;
    } else if (argc == 2) {
        int level;
        if (strcmp(argv[1], "up") == 0) {
            level = get_brightness() + 1;
            if (level > 10) {
                return 0;
            }
        } else if (strcmp(argv[1], "down") == 0) {
            level = get_brightness() - 1;
            if (level < 0) {
                return 0;
            }
        } else if (strcmp(argv[1], "max") == 0) {
            level = 10;
        } else if (strcmp(argv[1], "min") == 0) {
            level = 0;
        } else {
            level = strtol(argv[1], ((void *)0), 10);
        }
        set_brightness(level);
        return 0;
    } else {
        fprintf(stderr, "Usage: %s [<value>|up|down|max|min]\n", argv[0]);
        return 1;
    }
}


fatal

repo: skarger/prxs

decompiled_code:
void __fastcall __noreturn fatal(const char *a1, const char *a2, int a3)
{
fprintf(stderr, "Error: %s,%s\n", a1, a2);
exit(a3);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, const char *a2, int a3)
{
fprintf(stderr, "Error: %s,%s\n", a1, a2);
exit(a3);
}

original_code:
void fatal(char *s1, char *s2, int n)
{
 fprintf(stderr,"Error: %s,%s\n", s1, s2);
 exit(n);
}

variable_types: n int


emalloc

decompiled_code:
void *__fastcall emalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
fatal("out of memory", (const char *)&unk_84, 1);
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
func0("out of memory", (const char *)&unk_84, 1);
return result;
}

original_code:
void * emalloc(size_t n)
{
 void *rv ;
 if ( (rv = malloc(n)) == ((void *)0) )
  fatal("out of memory","",1);
 return rv;
}

canonical_original_code:
void *emalloc(unsigned long n)
{
 void *rv;
 if ( (rv = malloc(n)) == ((void *)0) )
  fatal("out of memory","",1);
 return rv;
}

variable_types:
n
unsigned long

variable_types:
rv
void *


erealloc

decompiled_code:
void *__fastcall erealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
fatal("realloc() failed", (const char *)&unk_84, 1);
return result;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
func0("realloc() failed", (const char *)&unk_84, 1);
return result;
}

original_code:
void * erealloc(void *p, size_t n)
{
 void *rv;
 if ( (rv = realloc(p,n)) == ((void *)0) )
  fatal("realloc() failed","",1);
 return rv;
}

canonical_original_code:
void *erealloc(void *p, unsigned long n)
{
 void *rv;
 if ( (rv = realloc(p,n)) == ((void *)0) )
  fatal("realloc() failed","",1);
 return rv;
}

variable_types:
p
void *


decompiled_code:
__int64 __fastcall make_server_socket(__int16 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(a1);
v2 = socket(2, 1, 0);
v3 = v2;
if ( v2 != -1 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) == -1 )
{
v3 = -1;
}
else if ( bind(v3, &addr, 0x10u) == -1 )
{
v3 = -1;
}
else if ( listen(v3, a2) )
{
v3 = -1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func0(0);
*(_WORD *)addr.sa_data = func1(a1);
v2 = socket(2, 1, 0);
v3 = v2;
if ( v2 != -1 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) == -1 )
{
v3 = -1;
}
else if ( bind(v3, &addr, 0x10u) == -1 )
{
v3 = -1;
}
else if ( listen(v3, a2) )
{
v3 = -1;
}
}
return v3;
}

original_code:
int
make_server_socket( int portnum, int backlog )
{
    struct sockaddr_in saddr;
    int sock_id;
    int on = 1;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = 2;
    saddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    saddr.sin_port = htons(portnum);
    sock_id = socket( 2, SOCK_STREAM, 0 );
    if ( sock_id == -1 ) return -1;
    if ( setsockopt(sock_id,1,2,&on,sizeof(on)) == -1 )
        return -1;
    if ( bind(sock_id,(struct sockaddr*)&saddr, sizeof(saddr)) == -1 )
           return -1;
    if ( listen(sock_id, backlog) != 0 ) return -1;
    return sock_id;
}

canonical_original_code:
int make_server_socket(int portnum, int backlog)
{
    struct sockaddr_in saddr;
    int sock_id;
    int on = 1;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = 2;
    saddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    saddr.sin_port = htons(portnum);
    sock_id = socket( 2, SOCK_STREAM, 0 );
    if ( sock_id == -1 ) return -1;
    if ( setsockopt(sock_id,1,2,&on,sizeof(on)) == -1 )
        return -1;
    if ( bind(sock_id,(struct sockaddr *)&saddr, sizeof(saddr)) == -1 )
           return -1;
    if ( listen(sock_id, backlog) != 0 ) return -1;
    return sock_id;
}

variable_types: portnum int

variable_types: backlog int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: on int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall connect_to_server(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *v3; // rbx
unsigned int v4; // ebp
const char *v6; // rax
int v7; // eax
struct addrinfo v8; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-20h] BYREF

*(_QWORD *)&v8.ai_socktype = 1LL;
*(_QWORD *)&v8.ai_addrlen = 0LL;
v8.ai_addr = 0LL;
v8.ai_canonname = 0LL;
v8.ai_next = 0LL;
v8.ai_family = 2;
v8.ai_flags = 2;
v2 = getaddrinfo(a1, a2, &v8, &pai);
if ( v2 )
{
v6 = gai_strerror(v2);
printf("getaddrinfo: %s\n", v6);
v4 = -1;
}
else
{
v3 = pai;
if ( pai )
{
while ( 1 )
{
v7 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v4 = v7;
if ( v7 != -1 )
{
if ( connect(v7, v3->ai_addr, v3->ai_addrlen) != -1 )
goto LABEL_4;
close(v4);
}
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_4;
}
}
v4 = -1;
LABEL_4:
freeaddrinfo(pai);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *v3; // rbx
unsigned int v4; // ebp
const char *v6; // rax
int v7; // eax
struct addrinfo v8; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-20h] BYREF

*(_QWORD *)&v8.ai_socktype = 1LL;
*(_QWORD *)&v8.ai_addrlen = 0LL;
v8.ai_addr = 0LL;
v8.ai_canonname = 0LL;
v8.ai_next = 0LL;
v8.ai_family = 2;
v8.ai_flags = 2;
v2 = getaddrinfo(a1, a2, &v8, &pai);
if ( v2 )
{
v6 = gai_strerror(v2);
printf("getaddrinfo: %s\n", v6);
v4 = -1;
}
else
{
v3 = pai;
if ( pai )
{
while ( 1 )
{
v7 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v4 = v7;
if ( v7 != -1 )
{
if ( connect(v7, v3->ai_addr, v3->ai_addrlen) != -1 )
goto LABEL_4;
close(v4);
}
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_4;
}
}
v4 = -1;
LABEL_4:
freeaddrinfo(pai);
}
return v4;
}

original_code:
int connect_to_server( char *hostname, char *portnum )
{
    struct addrinfo *result, *rp, hints;
    int serverfd = -1, rc;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0002;
    hints.ai_protocol = 0;
    hints.ai_canonname = ((void *)0);
    hints.ai_addr = ((void *)0);
    hints.ai_next = ((void *)0);
    rc = getaddrinfo(hostname, portnum, &hints, &result);
    if (rc != 0) {
        printf("getaddrinfo: %s\n", gai_strerror(rc));
        return -1;
    }
    for (rp = result; rp != ((void *)0); rp = rp->ai_next) {
        serverfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (serverfd == -1)
            continue;
       if (connect(serverfd, rp->ai_addr, rp->ai_addrlen) != -1) {
            break;
        }
        close(serverfd);
    }
    freeaddrinfo(result);
    return serverfd;
}

canonical_original_code:
int connect_to_server(char *hostname, char *portnum)
{
    struct addrinfo *result; struct addrinfo *rp; struct addrinfo hints;
    int serverfd = -1; int rc;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0002;
    hints.ai_protocol = 0;
    hints.ai_canonname = ((void *)0);
    hints.ai_addr = ((void *)0);
    hints.ai_next = ((void *)0);
    rc = getaddrinfo(hostname, portnum, &hints, &result);
    if (rc != 0) {
        printf("getaddrinfo: %s\n", gai_strerror(rc));
        return -1;
    }
    for (rp = result; rp != ((void *)0); rp = rp->ai_next) {
        serverfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (serverfd == -1)
            continue;
       if (connect(serverfd, rp->ai_addr, rp->ai_addrlen) != -1) {
            break;
        }
        close(serverfd);
    }
    freeaddrinfo(result);
    return serverfd;
}

variable_types:
hostname
char *

variable_types:
portnum
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: serverfd int

variable_types: rc int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


repo: skarger/sos

decompiled_code:
__int64 __fastcall cmd_create(int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // rax

v2 = 6;
if ( a1 == 2 )
{
v2 = svc_myCreate(*(_QWORD *)(a2 + 8));
if ( v2 )
{
v4 = process_ostrm();
efputs("create: error creating file\r\n", v4);
}
}
return v2;
}

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

v2 = 6;
if ( a1 == 2 )
{
v2 = svc_myCreate(*(_QWORD *)(a2 + 8));
if ( v2 )
{
v4 = process_ostrm();
efputs("create: error creating file\r\n", v4);
}
}
return v2;
}

original_code:
int cmd_create(int argc, char *argv[]) {
    int res;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    res = svc_myCreate(argv[1]);
    if (res != SUCCESS) {
        efputs("create: error creating file\r\n", (process_ostrm()));
    }
    return res;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: res int


decompiled_code:
__int64 __fastcall cmd_delete(int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // rax

v2 = 6;
if ( a1 == 2 )
{
v2 = svc_myDelete(*(_QWORD *)(a2 + 8));
if ( v2 )
{
v4 = process_ostrm();
efputs("delete: cannot delete file\r\n", v4);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // rax

v2 = 6;
if ( a1 == 2 )
{
v2 = svc_myDelete(*(_QWORD *)(a2 + 8));
if ( v2 )
{
v4 = process_ostrm();
efputs("delete: cannot delete file\r\n", v4);
}
}
return v2;
}

original_code:
int cmd_delete(int argc, char *argv[]) {
    int res;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    res = svc_myDelete(argv[1]);
    if (res != SUCCESS) {
        efputs("delete: cannot delete file\r\n", (process_ostrm()));
    }
    return res;
}


decompiled_code:
__int64 __fastcall cmd_fopen(int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax

result = 6LL;
if ( a1 == 2 )
{
v3 = svc_myFopen(*(_QWORD *)(a2 + 8));
if ( v3 )
{
sprintf(output_string, "file opened with stream ID: %d\r\n", *(unsigned int *)(v3 + 4));
v4 = process_ostrm();
efputs(output_string, v4);
result = 0LL;
}
else
{
v5 = process_ostrm();
efputs("fopen: error opening file\r\n", v5);
result = 11LL;
}
}
return result;
}

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

result = 6LL;
if ( a1 == 2 )
{
v3 = svc_myFopen(*(_QWORD *)(a2 + 8));
if ( v3 )
{
sprintf(output_string, "file opened with stream ID: %d\r\n", *(unsigned int *)(v3 + 4));
v4 = process_ostrm();
efputs(output_string, v4);
result = 0LL;
}
else
{
v5 = process_ostrm();
efputs("fopen: error opening file\r\n", v5);
result = 11LL;
}
}
return result;
}

original_code:
int cmd_fopen(int argc, char *argv[]) {
    Stream *stream;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    stream = svc_myFopen(argv[1]);
    if (stream == ((void *)0)) {
        efputs("fopen: error opening file\r\n", (process_ostrm()));
        return CANNOT_OPEN_FILE;
    }
    sprintf(output_string, "file opened with stream ID: %d\r\n", stream->stream_id);
    efputs(output_string, (process_ostrm()));
    return SUCCESS;
}

canonical_original_code:
int cmd_fopen(int argc, char *argv[]) {
    struct Stream *stream;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    stream = svc_myFopen(argv[1]);
    if (stream == ((void *)0)) {
        efputs("fopen: error opening file\r\n", (process_ostrm()));
        return CANNOT_OPEN_FILE;
    }
    sprintf(output_string, "file opened with stream ID: %d\r\n", stream->stream_id);
    efputs(output_string, (process_ostrm()));
    return SUCCESS;
}

variable_types: struct Stream { enum device_instance { NULL_DEVICE=0, LED_ORANGE=1, LED_YELLOW=2, LED_GREEN=3, LED_BLUE=4, BUTTON_SW1=5, BUTTON_SW2=6, UART2=7, LCD=8, POTENTIOMETER=9, THERMISTOR=10, TOUCH_PAD_E1=11, TOUCH_PAD_E2=12, TOUCH_PAD_E3=13, TOUCH_PAD_E4=14, FILE_SYSTEM=15 } device_instance; unsigned int stream_id; unsigned int pid; struct NamedFile { char *filename; int size; struct Block { int size; char *data; struct Block *next; } *first_block; struct Block *last_block; struct NamedFile *next; } *file; struct Block *write_block; struct Block *read_block; char *next_byte_to_write; char *next_byte_to_read; struct Stream *next; } *

user_defined_types: struct Stream { enum device_instance device_instance; unsigned int stream_id; unsigned int pid; struct NamedFile *file; struct Block *write_block; struct Block *read_block; char *next_byte_to_write; char *next_byte_to_read; struct Stream *next; }

user_defined_types: struct NamedFile { char *filename; int size; struct Block *first_block; struct Block *last_block; struct NamedFile *next; }


decompiled_code:
_BOOL8 __fastcall is_valid_stream_id(int a1, _BYTE *a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 <= 0 )
result = *a2 == 48;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1, _BYTE *a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 <= 0 )
result = *a2 == 48;
return result;
}

original_code:
int is_valid_stream_id(int stream_id, char *arg) {
    if (stream_id <= 0 && arg[0] != '0') {
        return 0;
    }
    return 1;
}

variable_types:
arg
char *


decompiled_code:
__int64 __fastcall find_stream_from_arg(_BYTE *a1)
{
unsigned int v1; // ebp
_BOOL4 v2; // edx
__int64 result; // rax

v1 = myAtoi();
v2 = is_valid_stream_id(v1, a1);
result = 0LL;
if ( v2 )
result = find_stream(v1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
unsigned int v1; // ebp
_BOOL4 v2; // edx
__int64 result; // rax

v1 = myAtoi();
v2 = func3(v1, a1);
result = 0LL;
if ( v2 )
result = find_stream(v1);
return result;
}

canonical_original_code:
struct Stream *find_stream_from_arg(char *arg) {
    int stream_id = myAtoi(arg);
    if (!is_valid_stream_id(stream_id, arg)) {
        return ((void *)0);
    }
    return find_stream(stream_id);
}

return_type: struct Stream { enum device_instance { NULL_DEVICE=0, LED_ORANGE=1, LED_YELLOW=2, LED_GREEN=3, LED_BLUE=4, BUTTON_SW1=5, BUTTON_SW2=6, UART2=7, LCD=8, POTENTIOMETER=9, THERMISTOR=10, TOUCH_PAD_E1=11, TOUCH_PAD_E2=12, TOUCH_PAD_E3=13, TOUCH_PAD_E4=14, FILE_SYSTEM=15 } device_instance; unsigned int stream_id; unsigned int pid; struct NamedFile { char *filename; int size; struct Block { int size; char *data; struct Block *next; } *first_block; struct Block *last_block; struct NamedFile *next; } *file; struct Block *write_block; struct Block *read_block; char *next_byte_to_write; char *next_byte_to_read; struct Stream *next; } *


decompiled_code:
__int64 __fastcall cmd_fclose(int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rax

result = 6LL;
if ( a1 == 2 )
{
v3 = find_stream_from_arg(*(_BYTE **)(a2 + 8));
if ( v3 )
{
result = svc_myFclose(v3);
}
else
{
v4 = process_ostrm();
efputs("fclose: cannot close stream\r\n", v4);
result = 12LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rax

result = 6LL;
if ( a1 == 2 )
{
v3 = func4(*(_BYTE **)(a2 + 8));
if ( v3 )
{
result = svc_myFclose(v3);
}
else
{
v4 = process_ostrm();
efputs("fclose: cannot close stream\r\n", v4);
result = 12LL;
}
}
return result;
}

original_code:
int cmd_fclose(int argc, char *argv[]) {
    Stream *stream;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream != ((void *)0)) {
        return svc_myFclose(stream);
    }
    efputs("fclose: cannot close stream\r\n", (process_ostrm()));
    return CANNOT_CLOSE_FILE;
}

canonical_original_code:
int cmd_fclose(int argc, char *argv[]) {
    struct Stream *stream;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream != ((void *)0)) {
        return svc_myFclose(stream);
    }
    efputs("fclose: cannot close stream\r\n", (process_ostrm()));
    return CANNOT_CLOSE_FILE;
}

user_defined_types: enum device_instance { NULL_DEVICE=0, LED_ORANGE=1, LED_YELLOW=2, LED_GREEN=3, LED_BLUE=4, BUTTON_SW1=5, BUTTON_SW2=6, UART2=7, LCD=8, POTENTIOMETER=9, THERMISTOR=10, TOUCH_PAD_E1=11, TOUCH_PAD_E2=12, TOUCH_PAD_E3=13, TOUCH_PAD_E4=14, FILE_SYSTEM=15 }

user_defined_types: struct Block { int size; char *data; struct Block *next; }


decompiled_code:
__int64 __fastcall cmd_fgetc(int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // rax
__int64 v5; // rbp
unsigned int v6; // ebx
__int64 v7; // rax
__int64 v8; // rsi
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rax

v2 = 6;
if ( a1 == 2 )
{
v4 = find_stream_from_arg(*(_BYTE **)(a2 + 8));
v5 = v4;
if ( v4 )
{
v6 = svc_myFgetc(v4);
if ( (unsigned int)stream_is_button(v5) )
v6 += 48;
if ( v6 == -1 )
{
v11 = process_ostrm();
efputs("fgetc: end of file\r\n", v11);
v2 = 0;
}
else
{
v7 = process_ostrm();
efputc(v6, v7);
v8 = process_ostrm();
v2 = efputs("\r\n", v8);
if ( v2 )
{
v9 = process_ostrm();
efputs("fgetc: error printing char\r\n", v9);
}
}
}
else
{
v10 = process_ostrm();
efputs("fgetc: cannot get char from null stream\r\n", v10);
v2 = -3;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 a2)
{
unsigned int v2; // ebx
__int64 v4; // rax
__int64 v5; // rbp
unsigned int v6; // ebx
__int64 v7; // rax
__int64 v8; // rsi
__int64 v9; // rax
__int64 v10; // rax
__int64 v11; // rax

v2 = 6;
if ( a1 == 2 )
{
v4 = func4(*(_BYTE **)(a2 + 8));
v5 = v4;
if ( v4 )
{
v6 = svc_myFgetc(v4);
if ( (unsigned int)stream_is_button(v5) )
v6 += 48;
if ( v6 == -1 )
{
v11 = process_ostrm();
efputs("fgetc: end of file\r\n", v11);
v2 = 0;
}
else
{
v7 = process_ostrm();
efputc(v6, v7);
v8 = process_ostrm();
v2 = efputs("\r\n", v8);
if ( v2 )
{
v9 = process_ostrm();
efputs("fgetc: error printing char\r\n", v9);
}
}
}
else
{
v10 = process_ostrm();
efputs("fgetc: cannot get char from null stream\r\n", v10);
v2 = -3;
}
}
return v2;
}

original_code:
int cmd_fgetc(int argc, char *argv[]) {
    Stream *stream;
    int c, res;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream == ((void *)0)) {
        efputs("fgetc: cannot get char from null stream\r\n", (process_ostrm()));
        return -3;
    }
    c = svc_myFgetc(stream);
    if (stream_is_button(stream)) {
        c = c + '0';
    }
    if (c == (-1)) {
        efputs("fgetc: end of file\r\n", (process_ostrm()));
        return SUCCESS;
    }
    res = efputc(c, (process_ostrm()));
    res = efputs("\r\n", (process_ostrm()));
    if (res != SUCCESS) {
        efputs("fgetc: error printing char\r\n", (process_ostrm()));
        return res;
    }
    return SUCCESS;
}

canonical_original_code:
int cmd_fgetc(int argc, char *argv[]) {
    struct Stream *stream;
    int c; int res;
    if (argc != 2) {
        return WRONG_NUMBER_ARGS;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream == ((void *)0)) {
        efputs("fgetc: cannot get char from null stream\r\n", (process_ostrm()));
        return -3;
    }
    c = svc_myFgetc(stream);
    if (stream_is_button(stream)) {
        c = c + '0';
    }
    if (c == (-1)) {
        efputs("fgetc: end of file\r\n", (process_ostrm()));
        return SUCCESS;
    }
    res = efputc(c, (process_ostrm()));
    res = efputs("\r\n", (process_ostrm()));
    if (res != SUCCESS) {
        efputs("fgetc: error printing char\r\n", (process_ostrm()));
        return res;
    }
    return SUCCESS;
}

variable_types: c int


decompiled_code:
__int64 __fastcall cmd_fputc(int a1, __int64 a2)
{
__int64 v2; // rbp
unsigned int v3; // ebx
int v4; // eax
__int64 result; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax

if ( a1 == 3 )
{
if ( (int)string_length(*(_QWORD *)(a2 + 16)) > 1 )
{
v7 = process_ostrm();
efputs("fputc: invalid char\r\n", v7);
result = 8LL;
}
else
{
v2 = find_stream_from_arg(*(_BYTE **)(a2 + 8));
if ( v2 )
{
v3 = **(char **)(a2 + 16);
v4 = stream_is_led(v2);
if ( v3 == 48 )
{
if ( v4 )
v3 = 0;
}
svc_myFputc(v3, v2);
result = 0LL;
}
else
{
v8 = process_ostrm();
efputs("fputc: cannot put char\r\n", v8);
result = 4294967292LL;
}
}
}
else
{
v6 = process_ostrm();
efputs("usage: fputc streamID char\r\n", v6);
result = 6LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2)
{
__int64 v2; // rbp
unsigned int v3; // ebx
int v4; // eax
__int64 result; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax

if ( a1 == 3 )
{
if ( (int)string_length(*(_QWORD *)(a2 + 16)) > 1 )
{
v7 = process_ostrm();
efputs("fputc: invalid char\r\n", v7);
result = 8LL;
}
else
{
v2 = func4(*(_BYTE **)(a2 + 8));
if ( v2 )
{
v3 = **(char **)(a2 + 16);
v4 = stream_is_led(v2);
if ( v3 == 48 )
{
if ( v4 )
v3 = 0;
}
svc_myFputc(v3, v2);
result = 0LL;
}
else
{
v8 = process_ostrm();
efputs("fputc: cannot put char\r\n", v8);
result = 4294967292LL;
}
}
}
else
{
v6 = process_ostrm();
efputs("usage: fputc streamID char\r\n", v6);
result = 6LL;
}
return result;
}

original_code:
int cmd_fputc(int argc, char *argv[]) {
    Stream *stream;
    int c;
    if (argc != 3) {
        efputs("usage: fputc streamID char\r\n", (process_ostrm()));
        return WRONG_NUMBER_ARGS;
    }
    if (string_length(argv[2]) > 1) {
        efputs("fputc: invalid char\r\n", (process_ostrm()));
        return INVALID_INPUT;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream != ((void *)0)) {
        c = argv[2][0];
        if (stream_is_led(stream) && c == '0') {
            c = 0;
        }
        svc_myFputc(c, stream);
        return SUCCESS;
    }
    efputs("fputc: cannot put char\r\n", (process_ostrm()));
    return -4;
}

canonical_original_code:
int cmd_fputc(int argc, char *argv[]) {
    struct Stream *stream;
    int c;
    if (argc != 3) {
        efputs("usage: fputc streamID char\r\n", (process_ostrm()));
        return WRONG_NUMBER_ARGS;
    }
    if (string_length(argv[2]) > 1) {
        efputs("fputc: invalid char\r\n", (process_ostrm()));
        return INVALID_INPUT;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream != ((void *)0)) {
        c = argv[2][0];
        if (stream_is_led(stream) && c == '0') {
            c = 0;
        }
        svc_myFputc(c, stream);
        return SUCCESS;
    }
    efputs("fputc: cannot put char\r\n", (process_ostrm()));
    return -4;
}


decompiled_code:
__int64 __fastcall cmd_fputs(int a1, __int64 a2)
{
__int64 v3; // rsi
__int64 result; // rax
__int64 v5; // rax
__int64 v6; // rax

if ( a1 == 3 )
{
v3 = find_stream_from_arg(*(_BYTE **)(a2 + 8));
if ( v3 )
{
result = svc_myFputs(*(_QWORD *)(a2 + 16), v3);
if ( (_DWORD)result )
{
v6 = process_ostrm();
result = efputs("fputs: cannot put string\r\n", v6);
if ( (_DWORD)result )
result = 2LL;
}
}
else
{
result = 8LL;
}
}
else
{
v5 = process_ostrm();
efputs("usage: fputs streamID string\r\n", v5);
result = 6LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, __int64 a2)
{
__int64 v3; // rsi
__int64 result; // rax
__int64 v5; // rax
__int64 v6; // rax

if ( a1 == 3 )
{
v3 = func4(*(_BYTE **)(a2 + 8));
if ( v3 )
{
result = svc_myFputs(*(_QWORD *)(a2 + 16), v3);
if ( (_DWORD)result )
{
v6 = process_ostrm();
result = efputs("fputs: cannot put string\r\n", v6);
if ( (_DWORD)result )
result = 2LL;
}
}
else
{
result = 8LL;
}
}
else
{
v5 = process_ostrm();
efputs("usage: fputs streamID string\r\n", v5);
result = 6LL;
}
return result;
}

original_code:
int cmd_fputs(int argc, char *argv[]) {
    Stream *stream;
    int res;
    if (argc != 3) {
        efputs("usage: fputs streamID string\r\n", (process_ostrm()));
        return WRONG_NUMBER_ARGS;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream == ((void *)0)) {
        return INVALID_INPUT;
    }
    res = svc_myFputs((const char *) argv[2], stream);
    if (res != SUCCESS) {
        res = efputs("fputs: cannot put string\r\n", (process_ostrm()));
        if (res != SUCCESS) {
            return WRITE_ERROR;
        }
    }
    return SUCCESS;
}

canonical_original_code:
int cmd_fputs(int argc, char *argv[]) {
    struct Stream *stream;
    int res;
    if (argc != 3) {
        efputs("usage: fputs streamID string\r\n", (process_ostrm()));
        return WRONG_NUMBER_ARGS;
    }
    stream = find_stream_from_arg(argv[1]);
    if (stream == ((void *)0)) {
        return INVALID_INPUT;
    }
    res = svc_myFputs((char *) argv[2], stream);
    if (res != SUCCESS) {
        res = efputs("fputs: cannot put string\r\n", (process_ostrm()));
        if (res != SUCCESS) {
            return WRITE_ERROR;
        }
    }
    return SUCCESS;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // [rsp+Ch] [rbp-4h] BYREF

v4 = argc;
puts("Running main() from gtest_main.cc");
testing::InitGoogleTest((testing *)&v4, (int *)argv, (char **)argv);
return RUN_ALL_TESTS();
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // [rsp+Ch] [rbp-4h] BYREF

v4 = argc;
puts("Running main() from gtest_main.cc");
testing::InitGoogleTest((testing *)&v4, (int *)argv, (char **)argv);
return func2();
}

original_code:
int main(void) {
    initialize_system();
    svc_spawn(run_shell);
    while(1) {
        ;
    }
    return 0;
}

canonical_original_code:
int main() {
    initialize_system();
    svc_spawn(run_shell);
    while(1) {
        ;
    }
    return 0;
}


radians

repo: skaslev/catmull-clark

decompiled_code:
float __fastcall radians(float a1)
{
return (float)(a1 * 6.2831855) / 360.0;
}

canonical_decompiled_code:
float __fastcall func0(float a1)
{
return (float)(a1 * 6.2831855) / 360.0;
}

original_code:
static inline float radians(float deg)
{
 return deg * (2.0f * ((float) 3.14159265358979323846)) / 360.0f;
}

variable_types: deg float

return_type: float


decompiled_code:
void __fastcall vec_set(float *a1, float a2, float a3, float a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

canonical_decompiled_code:
void __fastcall func1(float *a1, float a2, float a3, float a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

original_code:
static inline void vec_set(vector r, float x, float y, float z)
{
 r[0] = x;
 r[1] = y;
 r[2] = z;
}

canonical_original_code:
static inline void vec_set(float r[3], float x, float y, float z)
{
 r[0] = x;
 r[1] = y;
 r[2] = z;
}

variable_types:
r
float [3]

variable_types: x float

variable_types: y float

variable_types: z float


original_code:
static inline void vec_mad(vector r, float f, const vector a)
{
 r[0] += f * a[0];
 r[1] += f * a[1];
 r[2] += f * a[2];
}

canonical_original_code:
static inline void vec_mad(float r[3], float f, float a[3])
{
 r[0] += f * a[0];
 r[1] += f * a[1];
 r[2] += f * a[2];
}

variable_types: f float

variable_types:
a
float [3]


idle

decompiled_code:
__int64 idle()
{
return glutPostRedisplay();
}

canonical_decompiled_code:
__int64 func5()
{
return glutPostRedisplay();
}

original_code:
static void idle(void)
{
 glutPostRedisplay();
}

canonical_original_code:
static void idle()
{
 glutPostRedisplay();
}


decompiled_code:
__int64 __fastcall get_camera_frame(float *a1, float *a2, float *a3)
{
_BYTE v5[88]; // [rsp+0h] [rbp-58h] BYREF

vec_set(a1, 1.0, 0.0, 0.0);
vec_set(a2, 0.0, 1.0, 0.0);
vec_set(a3, 0.0, 0.0, 1.0);
mat_rotate(v5, a2, *(float *)&y_rot);
mat_mul_vector(a1, v5, a1);
mat_mul_vector(a3, v5, a3);
mat_rotate(v5, a1, *(float *)&x_rot);
mat_mul_vector(a2, v5, a2);
return mat_mul_vector(a3, v5, a3);
}

canonical_decompiled_code:
__int64 __fastcall func6(float *a1, float *a2, float *a3)
{
_BYTE v5[88]; // [rsp+0h] [rbp-58h] BYREF

func1(a1, 1.0, 0.0, 0.0);
func1(a2, 0.0, 1.0, 0.0);
func1(a3, 0.0, 0.0, 1.0);
mat_rotate(v5, a2, *(float *)&y_rot);
mat_mul_vector(a1, v5, a1);
mat_mul_vector(a3, v5, a3);
mat_rotate(v5, a1, *(float *)&x_rot);
mat_mul_vector(a2, v5, a2);
return mat_mul_vector(a3, v5, a3);
}

original_code:
static void get_camera_frame(vector x, vector y, vector z)
{
 matrix m;
 vec_set(x, 1.0f, 0.0f, 0.0f);
 vec_set(y, 0.0f, 1.0f, 0.0f);
 vec_set(z, 0.0f, 0.0f, 1.0f);
 mat_rotate(m, y, y_rot);
 mat_mul_vector(x, m, x);
 mat_mul_vector(z, m, z);
 mat_rotate(m, x, x_rot);
 mat_mul_vector(y, m, y);
 mat_mul_vector(z, m, z);
}

canonical_original_code:
static void get_camera_frame(float x[3], float y[3], float z[3])
{
 float m[16];
 vec_set(x, 1.0f, 0.0f, 0.0f);
 vec_set(y, 0.0f, 1.0f, 0.0f);
 vec_set(z, 0.0f, 0.0f, 1.0f);
 mat_rotate(m, y, y_rot);
 mat_mul_vector(x, m, x);
 mat_mul_vector(z, m, z);
 mat_rotate(m, x, x_rot);
 mat_mul_vector(y, m, y);
 mat_mul_vector(z, m, z);
}

variable_types:
x
float [3]

variable_types:
y
float [3]

variable_types:
z
float [3]

variable_types:
m
float [16]


mouse

decompiled_code:
__int64 __fastcall mouse(int a1, int a2, int a3, int a4)
{
char v6; // al
__int64 result; // rax

v6 = glutGetModifiers();
if ( a2 )
{
cur_op = 0;
}
else if ( a1 )
{
if ( a1 == 2 )
{
cur_op = 3;
}
else if ( a1 == 1 )
{
cur_op = 2;
}
}
else
{
cur_op = ((v6 & 2) != 0) + 1;
}
result = glutSetCursor((unsigned int)cursor_0[cur_op]);
last_x = a3;
last_y = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, int a3, int a4)
{
char v6; // al
__int64 result; // rax

v6 = glutGetModifiers();
if ( a2 )
{
cur_op = 0;
}
else if ( a1 )
{
if ( a1 == 2 )
{
cur_op = 3;
}
else if ( a1 == 1 )
{
cur_op = 2;
}
}
else
{
cur_op = ((v6 & 2) != 0) + 1;
}
result = glutSetCursor((unsigned int)cursor_0[cur_op]);
last_x = a3;
last_y = a4;
return result;
}

original_code:
static void mouse(int button, int state, int x, int y)
{
 int mods;
 static const int cursor[] = {
  0x0000,
  0x0005,
  0x0009,
  0x000A
 };
 mods = glutGetModifiers();
 if (state == 0x0000) {
  if (button == 0x0000)
   cur_op = (mods & 0x0002) ? PANNING : ROTATING;
  else if (button == 0x0002)
   cur_op = ZOOMING;
  else if (button == 0x0001)
   cur_op = PANNING;
 } else {
  cur_op = NONE;
 }
 glutSetCursor(cursor[cur_op]);
 last_x = x;
 last_y = y;
}

canonical_original_code:
static void mouse(int button, int state, int x, int y)
{
 int mods;
 static int cursor[] = {
  0x0000,
  0x0005,
  0x0009,
  0x000A
 };
 mods = glutGetModifiers();
 if (state == 0x0000) {
  if (button == 0x0000)
   cur_op = (mods & 0x0002) ? PANNING : ROTATING;
  else if (button == 0x0002)
   cur_op = ZOOMING;
  else if (button == 0x0001)
   cur_op = PANNING;
 } else {
  cur_op = NONE;
 }
 glutSetCursor(cursor[cur_op]);
 last_x = x;
 last_y = y;
}

variable_types: button int

variable_types: state int

variable_types: x int

variable_types: y int

variable_types: mods int

variable_types:
cursor
int []


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

*(_QWORD *)a1 = *(_QWORD *)a2;
result = *(unsigned int *)(a2 + 8);
*(_DWORD *)(a1 + 8) = result;
return result;
}

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

*(_QWORD *)a1 = *(_QWORD *)a2;
result = *(unsigned int *)(a2 + 8);
*(_DWORD *)(a1 + 8) = result;
return result;
}

original_code:
static inline void vec_copy(vector r, const vector a)
{
 memcpy(r, a, sizeof(vector));
}

canonical_original_code:
static inline void vec_copy(float r[3], float a[3])
{
 memcpy(r, a, sizeof(vector));
}


decompiled_code:
__int64 __fastcall get_camera(float *a1, __int64 a2, __int64 a3)
{
float v5[3]; // [rsp+Ch] [rbp-3Ch] BYREF
float v6[3]; // [rsp+18h] [rbp-30h] BYREF
float v7[9]; // [rsp+24h] [rbp-24h] BYREF

get_camera_frame(v7, v6, v5);
vec_copy(a2, (__int64)center);
vec_copy((__int64)a1, (__int64)center);
vec_mad(a1, v5, *(float *)&focal_len);
return vec_copy(a3, (__int64)v6);
}

canonical_decompiled_code:
__int64 __fastcall func10(float *a1, __int64 a2, __int64 a3)
{
float v5[3]; // [rsp+Ch] [rbp-3Ch] BYREF
float v6[3]; // [rsp+18h] [rbp-30h] BYREF
float v7[9]; // [rsp+24h] [rbp-24h] BYREF

func6(v7, v6, v5);
func9(a2, (__int64)center);
func9((__int64)a1, (__int64)center);
func4(a1, v5, *(float *)&focal_len);
return func9(a3, (__int64)v6);
}

original_code:
static void get_camera(vector eye, vector at, vector up)
{
 vector x, y, z;
 get_camera_frame(x, y, z);
 vec_copy(at, center);
 vec_copy(eye, center);
 vec_mad(eye, focal_len, z);
 vec_copy(up, y);
}

canonical_original_code:
static void get_camera(float eye[3], float at[3], float up[3])
{
 float x[3]; float y[3]; float z[3];
 get_camera_frame(x, y, z);
 vec_copy(at, center);
 vec_copy(eye, center);
 vec_mad(eye, focal_len, z);
 vec_copy(up, y);
}

variable_types:
eye
float [3]

variable_types:
at
float [3]

variable_types:
up
float [3]