networkOpenConnection

decompiled_code:
__int64 __fastcall networkOpenConnection(char *name, char *service, int a3, char a4)
{
char v6; // bp
__int64 v8; // rax
char *v9; // rdi
struct addrinfo *v10; // rbx
const char *v11; // rax
__int64 result; // rax
__int64 v14; // rax
const char *v15; // r9
int *v16; // rbp
int v17; // edi
int *v18; // rax
const char *v19; // rcx
const char *v20; // rcx
const char *v21; // [rsp+0h] [rbp-58h]
struct addrinfo *ai; // [rsp+10h] [rbp-48h] BYREF
__int64 v23[8]; // [rsp+18h] [rbp-40h] BYREF

v6 = a4;
if ( !(unsigned __int8)voidBufferAllocate(v23, 48LL, "network connection") )
return 0LL;
*(_DWORD *)v23[0] = -1;
*(_DWORD *)(v23[0] + 4) = a3;
v8 = v23[0];
*(_BYTE *)(v23[0] + 8) = 0;
*(_QWORD *)(v8 + 16) = 0LL;
*(_DWORD *)(v8 + 24) = 0;
*(_QWORD *)(v8 + 32) = 0LL;
*(_DWORD *)(v8 + 40) = 0;
v9 = 0LL;
if ( v6 )
v9 = name;
if ( (unsigned __int8)networkGetAddressInfo(v9, service, a3, &ai) )
{
v10 = ai;
if ( ai )
{
v11 = "bind";
if ( v6 )
v11 = "connect";
v21 = v11;
while ( 1 )
{
v16 = (int *)v23[0];
if ( *(_DWORD *)v23[0] != -1 )
{
if ( v10 )
goto LABEL_27;
goto LABEL_31;
}
*v16 = socket(v10->ai_family, v10->ai_socktype, v10->ai_protocol);
v17 = *(_DWORD *)v23[0];
if ( *(_DWORD *)v23[0] == -1 )
{
v18 = _errno_location();
v19 = (const char *)&unk_83A;
if ( v10->ai_next )
v19 = " (going to try another address)";
logWrite(
1LL,
"network.c",
"Cannot open a socket for the network connection%s. (errno = %d)",
v19,
(unsigned int)*v18);
*(_DWORD *)v23[0] = -1;
v10 = v10->ai_next;
goto LABEL_15;
}
if ( a4 ? connect(v17, v10->ai_addr, v10->ai_addrlen) : bind(v17, v10->ai_addr, v10->ai_addrlen) )
break;
if ( (unsigned __int8)networkCopyAddressInfo(v23[0], name, service) )
{
*(_BYTE *)(v23[0] + 8) = a4;
}
else
{
networkCloseSocket(v23[0]);
v10 = v10->ai_next;
LABEL_15:
if ( !v10 )
goto LABEL_31;
}
}
v14 = (unsigned int)*_errno_location();
v15 = (const char *)&unk_83A;
if ( v10->ai_next )
v15 = " (going to try another address)";
logWrite(
1LL,
"network.c",
"Cannot %s network connection to host \"%s\" on the opened socket%s. (errno = %d)",
v21,
name,
v15,
v14);
networkCloseSocket(v23[0]);
v10 = v10->ai_next;
goto LABEL_15;
}
LABEL_31:
networkCloseConnection(v23);
LABEL_27:
freeaddrinfo(ai);
v20 = "UDP";
if ( a3 != 1 )
{
v20 = "TCP";
if ( a3 )
v20 = "(unknown type)";
}
logWrite(4LL, "network.c", "Opened network %s connection to host %s on port %s", v20, name, service);
result = v23[0];
}
else
{
networkCloseConnection(v23);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *name, char *service, int a3, char a4)
{
char v6; // bp
__int64 v8; // rax
char *v9; // rdi
struct addrinfo *v10; // rbx
const char *v11; // rax
__int64 result; // rax
__int64 v14; // rax
const char *v15; // r9
int *v16; // rbp
int v17; // edi
int *v18; // rax
const char *v19; // rcx
const char *v20; // rcx
const char *v21; // [rsp+0h] [rbp-58h]
struct addrinfo *ai; // [rsp+10h] [rbp-48h] BYREF
__int64 v23[8]; // [rsp+18h] [rbp-40h] BYREF

v6 = a4;
if ( !(unsigned __int8)voidBufferAllocate(v23, 48LL, "network connection") )
return 0LL;
*(_DWORD *)v23[0] = -1;
*(_DWORD *)(v23[0] + 4) = a3;
v8 = v23[0];
*(_BYTE *)(v23[0] + 8) = 0;
*(_QWORD *)(v8 + 16) = 0LL;
*(_DWORD *)(v8 + 24) = 0;
*(_QWORD *)(v8 + 32) = 0LL;
*(_DWORD *)(v8 + 40) = 0;
v9 = 0LL;
if ( v6 )
v9 = name;
if ( (unsigned __int8)func0(v9, service, a3, &ai) )
{
v10 = ai;
if ( ai )
{
v11 = "bind";
if ( v6 )
v11 = "connect";
v21 = v11;
while ( 1 )
{
v16 = (int *)v23[0];
if ( *(_DWORD *)v23[0] != -1 )
{
if ( v10 )
goto LABEL_27;
goto LABEL_31;
}
*v16 = socket(v10->ai_family, v10->ai_socktype, v10->ai_protocol);
v17 = *(_DWORD *)v23[0];
if ( *(_DWORD *)v23[0] == -1 )
{
v18 = _errno_location();
v19 = (const char *)&unk_83A;
if ( v10->ai_next )
v19 = " (going to try another address)";
logWrite(
1LL,
"network.c",
"Cannot open a socket for the network connection%s. (errno = %d)",
v19,
(unsigned int)*v18);
*(_DWORD *)v23[0] = -1;
v10 = v10->ai_next;
goto LABEL_15;
}
if ( a4 ? connect(v17, v10->ai_addr, v10->ai_addrlen) : bind(v17, v10->ai_addr, v10->ai_addrlen) )
break;
if ( (unsigned __int8)func2(v23[0], name, service) )
{
*(_BYTE *)(v23[0] + 8) = a4;
}
else
{
func3(v23[0]);
v10 = v10->ai_next;
LABEL_15:
if ( !v10 )
goto LABEL_31;
}
}
v14 = (unsigned int)*_errno_location();
v15 = (const char *)&unk_83A;
if ( v10->ai_next )
v15 = " (going to try another address)";
logWrite(
1LL,
"network.c",
"Cannot %s network connection to host \"%s\" on the opened socket%s. (errno = %d)",
v21,
name,
v15,
v14);
func3(v23[0]);
v10 = v10->ai_next;
goto LABEL_15;
}
LABEL_31:
func12(v23);
LABEL_27:
freeaddrinfo(ai);
v20 = "UDP";
if ( a3 != 1 )
{
v20 = "TCP";
if ( a3 )
v20 = "(unknown type)";
}
logWrite(4LL, "network.c", "Opened network %s connection to host %s on port %s", v20, name, service);
result = v23[0];
}
else
{
func12(v23);
result = 0LL;
}
return result;
}

original_code:
NetworkConnection *networkOpenConnection(const char *hostName, const char *portName, NetworkConnectionType connectionType, _Bool makeClient) {
 NetworkConnection *networkConnection;
 struct addrinfo* addressInfoResult;
 struct addrinfo* addressInfo;
 int connectResult;
 if(!voidBufferAllocate((void **)&networkConnection, sizeof(NetworkConnection), "network connection")) {
  return ((void *)0);
 }
 networkConnection->socketDescriptor = -1;
 networkConnection->connectionType = connectionType;
 networkConnection->isClient = 0;
 networkConnection->localAddress = ((void *)0);
 networkConnection->localAddressSize = 0;
 networkConnection->remoteAddress = ((void *)0);
 networkConnection->remoteAddressSize = 0;
 if(!networkGetAddressInfo(makeClient ? hostName : ((void *)0), portName, connectionType, &addressInfoResult)) {
  networkCloseConnection(&networkConnection);
  return ((void *)0);
 }
 addressInfo = addressInfoResult;
 while(addressInfo != ((void *)0) && networkConnection->socketDescriptor == -1) {
  networkConnection->socketDescriptor = socket(addressInfo->ai_family, addressInfo->ai_socktype, addressInfo->ai_protocol);
  if(networkConnection->socketDescriptor != -1) {
   if(makeClient) {
    connectResult = connect(networkConnection->socketDescriptor, addressInfo->ai_addr, addressInfo->ai_addrlen);
   } else {
    connectResult = bind(networkConnection->socketDescriptor, addressInfo->ai_addr, addressInfo->ai_addrlen);
   }
   if(connectResult == 0) {
    if(networkCopyAddressInfo(networkConnection, hostName, portName)) {
     networkConnection->isClient = makeClient;
    } else {
     networkCloseSocket(networkConnection);
     addressInfo = addressInfo->ai_next;
    }
   } else {
    logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot %s network connection to host \"%s\" on the opened socket%s. (errno = %d)", makeClient ? "connect" : "bind", hostName, addressInfo->ai_next != ((void *)0) ? " (going to try another address)" : "", (*__errno_location ()));
    networkCloseSocket(networkConnection);
    addressInfo = addressInfo->ai_next;
   }
  } else {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot open a socket for the network connection%s. (errno = %d)", addressInfo->ai_next != ((void *)0) ? " (going to try another address)" : "", (*__errno_location ()));
   networkConnection->socketDescriptor = -1;
   addressInfo = addressInfo->ai_next;
  }
 }
 if(addressInfo == ((void *)0)) {
  networkCloseConnection(&networkConnection);
 }
 freeaddrinfo(addressInfoResult);
 logWrite(LOG_LEVEL_DEBUG, LOG_COMPONENT_NAME, "Opened network %s connection to host %s on port %s", connectionType == UDP_CONNECTION ? "UDP" : (connectionType == TCP_CONNECTION ? "TCP" : "(unknown type)"), hostName, portName);
 return networkConnection;
}

canonical_original_code:
struct NetworkConnectionStruct *networkOpenConnection(char *hostName, char *portName, enum NetworkConnectionType connectionType, _Bool makeClient) {
 struct NetworkConnectionStruct *networkConnection;
 struct addrinfo *addressInfoResult;
 struct addrinfo *addressInfo;
 int connectResult;
 if(!voidBufferAllocate((void **)&networkConnection, sizeof(NetworkConnection), "network connection")) {
  return ((void *)0);
 }
 networkConnection->socketDescriptor = -1;
 networkConnection->connectionType = connectionType;
 networkConnection->isClient = 0;
 networkConnection->localAddress = ((void *)0);
 networkConnection->localAddressSize = 0;
 networkConnection->remoteAddress = ((void *)0);
 networkConnection->remoteAddressSize = 0;
 if(!networkGetAddressInfo(makeClient ? hostName : ((void *)0), portName, connectionType, &addressInfoResult)) {
  networkCloseConnection(&networkConnection);
  return ((void *)0);
 }
 addressInfo = addressInfoResult;
 while(addressInfo != ((void *)0) && networkConnection->socketDescriptor == -1) {
  networkConnection->socketDescriptor = socket(addressInfo->ai_family, addressInfo->ai_socktype, addressInfo->ai_protocol);
  if(networkConnection->socketDescriptor != -1) {
   if(makeClient) {
    connectResult = connect(networkConnection->socketDescriptor, addressInfo->ai_addr, addressInfo->ai_addrlen);
   } else {
    connectResult = bind(networkConnection->socketDescriptor, addressInfo->ai_addr, addressInfo->ai_addrlen);
   }
   if(connectResult == 0) {
    if(networkCopyAddressInfo(networkConnection, hostName, portName)) {
     networkConnection->isClient = makeClient;
    } else {
     networkCloseSocket(networkConnection);
     addressInfo = addressInfo->ai_next;
    }
   } else {
    logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot %s network connection to host \"%s\" on the opened socket%s. (errno = %d)", makeClient ? "connect" : "bind", hostName, addressInfo->ai_next != ((void *)0) ? " (going to try another address)" : "", (*__errno_location ()));
    networkCloseSocket(networkConnection);
    addressInfo = addressInfo->ai_next;
   }
  } else {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot open a socket for the network connection%s. (errno = %d)", addressInfo->ai_next != ((void *)0) ? " (going to try another address)" : "", (*__errno_location ()));
   networkConnection->socketDescriptor = -1;
   addressInfo = addressInfo->ai_next;
  }
 }
 if(addressInfo == ((void *)0)) {
  networkCloseConnection(&networkConnection);
 }
 freeaddrinfo(addressInfoResult);
 logWrite(LOG_LEVEL_DEBUG, LOG_COMPONENT_NAME, "Opened network %s connection to host %s on port %s", connectionType == UDP_CONNECTION ? "UDP" : (connectionType == TCP_CONNECTION ? "TCP" : "(unknown type)"), hostName, portName);
 return networkConnection;
}

variable_types:
hostName
char *

variable_types:
portName
char *

variable_types: struct NetworkConnectionStruct { int socketDescriptor; enum NetworkConnectionType { TCP_CONNECTION=0, UDP_CONNECTION=1 } connectionType; _Bool isClient; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *localAddress; unsigned int localAddressSize; struct sockaddr *remoteAddress; unsigned int remoteAddressSize; } *

variable_types: connectResult int


rtspResponseCreate

repo: ErikOnBike/light-play

decompiled_code:
_QWORD *rtspResponseCreate()
{
_QWORD *result; // rax
_QWORD *v1; // [rsp+8h] [rbp-10h] BYREF

if ( !(unsigned __int8)voidBufferAllocate(&v1, 24LL, "RTSP response") )
return 0LL;
*v1 = 0LL;
result = v1;
v1[1] = 0LL;
result[2] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *result; // rax
_QWORD *v1; // [rsp+8h] [rbp-10h] BYREF

if ( !(unsigned __int8)voidBufferAllocate(&v1, 24LL, "RTSP response") )
return 0LL;
*v1 = 0LL;
result = v1;
v1[1] = 0LL;
result[2] = 0LL;
return result;
}

original_code:
RTSPResponse *rtspResponseCreate() {
 RTSPResponse *rtspResponse;
 if(!voidBufferAllocate((void **)&rtspResponse, sizeof(RTSPResponse), "RTSP response")) {
  return ((void *)0);
 }
 rtspResponse->responseBuffer = ((void *)0);
 rtspResponse->responseBufferSize = 0;
 rtspResponse->maxResponseBufferSize = 0;
 return rtspResponse;
}

canonical_original_code:
struct RTSPResponseStruct *rtspResponseCreate() {
 struct RTSPResponseStruct *rtspResponse;
 if(!voidBufferAllocate((void **)&rtspResponse, sizeof(RTSPResponse), "RTSP response")) {
  return ((void *)0);
 }
 rtspResponse->responseBuffer = ((void *)0);
 rtspResponse->responseBufferSize = 0;
 rtspResponse->maxResponseBufferSize = 0;
 return rtspResponse;
}

variable_types: struct RTSPResponseStruct { unsigned char *responseBuffer; unsigned long responseBufferSize; unsigned long maxResponseBufferSize; } *


rtspResponseGetStatus

decompiled_code:
__int64 __fastcall rtspResponseGetStatus(__int64 a1, _WORD *a2)
{
unsigned __int8 *v2; // rbx
const unsigned __int16 *v3; // rax
__int64 result; // rax
__int16 v5; // [rsp+Eh] [rbp-1Ah] BYREF

v2 = *(unsigned __int8 **)a1;
if ( *(_QWORD *)a1 && *(_QWORD *)(a1 + 8) > 0xBuLL )
{
if ( !memcmp(*(const void **)a1, "RTSP/", 5uLL)
&& (v3 = *_ctype_b_loc(), (v3[v2[5]] & 0x800) != 0)
&& v2[6] == 46
&& (v3[v2[7]] & 0x800) != 0
&& v2[8] == 32 )
{
if ( (unsigned int)_isoc99_sscanf(v2 + 9, "%hi", &v5) == 1 )
{
*a2 = v5;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read status value from RTSP response");
result = 0LL;
}
}
else
{
logWrite(
1LL,
"rtspresponse.c",
"RTSP Response does not contain correct protocol name and version. Expected \"RTSP/<digit>.<digit><space>\" found \"%.*s\"",
9LL,
v2);
result = 0LL;
}
}
else
{
logWrite(1LL, "rtspresponse.c", "RTSP Response does not contain (enough) buffer content to retrieve status.");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _WORD *a2)
{
unsigned __int8 *v2; // rbx
const unsigned __int16 *v3; // rax
__int64 result; // rax
__int16 v5; // [rsp+Eh] [rbp-1Ah] BYREF

v2 = *(unsigned __int8 **)a1;
if ( *(_QWORD *)a1 && *(_QWORD *)(a1 + 8) > 0xBuLL )
{
if ( !memcmp(*(const void **)a1, "RTSP/", 5uLL)
&& (v3 = *_ctype_b_loc(), (v3[v2[5]] & 0x800) != 0)
&& v2[6] == 46
&& (v3[v2[7]] & 0x800) != 0
&& v2[8] == 32 )
{
if ( (unsigned int)_isoc99_sscanf(v2 + 9, "%hi", &v5) == 1 )
{
*a2 = v5;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read status value from RTSP response");
result = 0LL;
}
}
else
{
logWrite(
1LL,
"rtspresponse.c",
"RTSP Response does not contain correct protocol name and version. Expected \"RTSP/<digit>.<digit><space>\" found \"%.*s\"",
9LL,
v2);
result = 0LL;
}
}
else
{
logWrite(1LL, "rtspresponse.c", "RTSP Response does not contain (enough) buffer content to retrieve status.");
result = 0LL;
}
return result;
}

original_code:
_Bool rtspResponseGetStatus(RTSPResponse *rtspResponse, int16_t *status) {
 int16_t intValue;
 if(rtspResponse->responseBuffer == ((void *)0) || rtspResponse->responseBufferSize < 12) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response does not contain (enough) buffer content to retrieve status.");
  return 0;
 }
 if(memcmp(rtspResponse->responseBuffer, "RTSP/", 5) != 0 || !((*__ctype_b_loc ())[(int) ((rtspResponse->responseBuffer[5]))] & (unsigned short int) _ISdigit) || rtspResponse->responseBuffer[6] != '.' || !((*__ctype_b_loc ())[(int) ((rtspResponse->responseBuffer[7]))] & (unsigned short int) _ISdigit) || rtspResponse->responseBuffer[8] != ' ') {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response does not contain correct protocol name and version. Expected \"RTSP/<digit>.<digit><space>\" found \"%.*s\"", 9, rtspResponse->responseBuffer);
  return 0;
 }
 if(sscanf((char *)rtspResponse->responseBuffer + 9, "%" "hi", &intValue) != 1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read status value from RTSP response");
  return 0;
 }
 *status = intValue;
 return 1;
}

canonical_original_code:
_Bool rtspResponseGetStatus(struct RTSPResponseStruct *rtspResponse, short *status) {
 short intValue;
 if(rtspResponse->responseBuffer == ((void *)0) || rtspResponse->responseBufferSize < 12) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response does not contain (enough) buffer content to retrieve status.");
  return 0;
 }
 if(memcmp(rtspResponse->responseBuffer, "RTSP/", 5) != 0 || !((*__ctype_b_loc ())[(int) ((rtspResponse->responseBuffer[5]))] & (unsigned short) _ISdigit) || rtspResponse->responseBuffer[6] != '.' || !((*__ctype_b_loc ())[(int) ((rtspResponse->responseBuffer[7]))] & (unsigned short) _ISdigit) || rtspResponse->responseBuffer[8] != ' ') {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response does not contain correct protocol name and version. Expected \"RTSP/<digit>.<digit><space>\" found \"%.*s\"", 9, rtspResponse->responseBuffer);
  return 0;
 }
 if(sscanf((char *)rtspResponse->responseBuffer + 9, "%" "hi", &intValue) != 1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read status value from RTSP response");
  return 0;
 }
 *status = intValue;
 return 1;
}

variable_types:
status
short *

variable_types: intValue short

user_defined_types: struct RTSPResponseStruct { unsigned char *responseBuffer; unsigned long responseBufferSize; unsigned long maxResponseBufferSize; }


rtspResponseGetSequenceNumber

decompiled_code:
__int64 __fastcall rtspResponseGetSequenceNumber(__int64 a1, _DWORD *a2)
{
__int64 v2; // rax
__int64 result; // rax
int v4; // [rsp+Ch] [rbp-Ch] BYREF

v2 = rtspResponseFindValueForKey(a1, "CSeq", 0LL);
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%u", &v4) == 1 )
{
*a2 = v4;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read CSeq value from RTSP response");
result = 0LL;
}
}
else
{
logWrite(2LL, "rtspresponse.c", "No CSeq value in RTSP response (continuing anyway)");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _DWORD *a2)
{
__int64 v2; // rax
__int64 result; // rax
int v4; // [rsp+Ch] [rbp-Ch] BYREF

v2 = func0(a1, "CSeq", 0LL);
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%u", &v4) == 1 )
{
*a2 = v4;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read CSeq value from RTSP response");
result = 0LL;
}
}
else
{
logWrite(2LL, "rtspresponse.c", "No CSeq value in RTSP response (continuing anyway)");
result = 1LL;
}
return result;
}

original_code:
_Bool rtspResponseGetSequenceNumber(RTSPResponse *rtspResponse, uint32_t *sequenceNumber) {
 uint8_t *value;
 uint32_t uint32Value;
 value = rtspResponseFindValueForKey(rtspResponse, "CSeq", ((void *)0));
 if(value != ((void *)0)) {
  if(sscanf((char *)value, "%" "u", &uint32Value) != 1) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read CSeq value from RTSP response");
   return 0;
  }
  *sequenceNumber = uint32Value;
 } else {
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "No CSeq value in RTSP response (continuing anyway)");
 }
 return 1;
}

canonical_original_code:
_Bool rtspResponseGetSequenceNumber(struct RTSPResponseStruct *rtspResponse, unsigned int *sequenceNumber) {
 unsigned char *value;
 unsigned int uint32Value;
 value = rtspResponseFindValueForKey(rtspResponse, "CSeq", ((void *)0));
 if(value != ((void *)0)) {
  if(sscanf((char *)value, "%" "u", &uint32Value) != 1) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read CSeq value from RTSP response");
   return 0;
  }
  *sequenceNumber = uint32Value;
 } else {
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "No CSeq value in RTSP response (continuing anyway)");
 }
 return 1;
}

variable_types:
sequenceNumber
unsigned int *

variable_types:
value
unsigned char *


rtspResponseGetSession

decompiled_code:
__int64 __fastcall rtspResponseGetSession(__int64 a1, _DWORD *a2)
{
__int64 v2; // rdi
__int64 result; // rax
int v4; // [rsp+Ch] [rbp-Ch] BYREF

v2 = rtspResponseFindValueForKey(a1, "Session", 0LL);
result = 0LL;
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%x", &v4) == 1 )
{
*a2 = v4;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read Session value from RTSP response");
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, _DWORD *a2)
{
__int64 v2; // rdi
__int64 result; // rax
int v4; // [rsp+Ch] [rbp-Ch] BYREF

v2 = func0(a1, "Session", 0LL);
result = 0LL;
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%x", &v4) == 1 )
{
*a2 = v4;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read Session value from RTSP response");
result = 0LL;
}
}
return result;
}

original_code:
_Bool rtspResponseGetSession(RTSPResponse *rtspResponse, uint32_t *session) {
 uint8_t *value;
 uint32_t uint32Value;
 value = rtspResponseFindValueForKey(rtspResponse, "Session", ((void *)0));
 if(value == ((void *)0)) {
  return 0;
 }
 if(sscanf((char *)value, "%" "x", &uint32Value) != 1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read Session value from RTSP response");
  return 0;
 }
 *session = uint32Value;
 return 1;
}

canonical_original_code:
_Bool rtspResponseGetSession(struct RTSPResponseStruct *rtspResponse, unsigned int *session) {
 unsigned char *value;
 unsigned int uint32Value;
 value = rtspResponseFindValueForKey(rtspResponse, "Session", ((void *)0));
 if(value == ((void *)0)) {
  return 0;
 }
 if(sscanf((char *)value, "%" "x", &uint32Value) != 1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read Session value from RTSP response");
  return 0;
 }
 *session = uint32Value;
 return 1;
}

variable_types:
session
unsigned int *


rtspResponseGetServerPort

decompiled_code:
__int64 __fastcall rtspResponseGetServerPort(__int64 a1, _WORD *a2)
{
__int64 v2; // rdi
__int64 result; // rax
__int16 v4; // [rsp+Eh] [rbp-Ah] BYREF

v2 = rtspResponseFindValueForKey(a1, "Transport", "server_port");
result = 0LL;
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%hi", &v4) == 1 )
{
*a2 = v4;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read Transport:server_port value from RTSP response");
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, _WORD *a2)
{
__int64 v2; // rdi
__int64 result; // rax
__int16 v4; // [rsp+Eh] [rbp-Ah] BYREF

v2 = func0(a1, "Transport", "server_port");
result = 0LL;
if ( v2 )
{
if ( (unsigned int)_isoc99_sscanf(v2, "%hi", &v4) == 1 )
{
*a2 = v4;
result = 1LL;
}
else
{
logWrite(1LL, "rtspresponse.c", "Cannot read Transport:server_port value from RTSP response");
result = 0LL;
}
}
return result;
}

original_code:
_Bool rtspResponseGetServerPort(RTSPResponse *rtspResponse, int16_t *serverPort) {
 uint8_t *value;
 int16_t int16Value;
 value = rtspResponseFindValueForKey(rtspResponse, "Transport", "server_port");
 if(value == ((void *)0)) {
  return 0;
 }
 if(sscanf((char *)value, "%" "hi", &int16Value) != 1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read Transport:server_port value from RTSP response");
  return 0;
 }
 *serverPort = int16Value;
 return 1;
}

canonical_original_code:
_Bool rtspResponseGetServerPort(struct RTSPResponseStruct *rtspResponse, short *serverPort) {
 unsigned char *value;
 short int16Value;
 value = rtspResponseFindValueForKey(rtspResponse, "Transport", "server_port");
 if(value == ((void *)0)) {
  return 0;
 }
 if(sscanf((char *)value, "%" "hi", &int16Value) != 1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read Transport:server_port value from RTSP response");
  return 0;
 }
 *serverPort = int16Value;
 return 1;
}

variable_types:
serverPort
short *


rtspResponseGetAuthenticationResponse

decompiled_code:
__int64 __fastcall rtspResponseGetAuthenticationResponse(_QWORD *a1, void *a2, unsigned int a3, unsigned int *a4, void *a5, unsigned int a6, unsigned int *a7)
{
char *v8; // rax
char *v9; // rbx
size_t v10; // rbp
_BYTE *v11; // rax
_BYTE *v12; // r12
signed __int64 v13; // rbp
_BYTE *v14; // rbx
const void *v15; // r14
const void *v16; // r13
__int64 result; // rax
_BYTE *v18; // rbp
size_t *v19; // rax
_BYTE *v20; // rbx
_BYTE *v21; // rbp
unsigned int v22; // ebx
unsigned int v23; // ebp
unsigned int n[16]; // [rsp+38h] [rbp-40h] BYREF

v8 = (char *)rtspResponseFindValueForKey((__int64)a1, "WWW-Authenticate", 0LL);
if ( !v8 )
return 0LL;
v9 = v8;
v10 = a1[1] - (_QWORD)&v8[-*a1];
v11 = memchr(v8, 10, v10);
v12 = v11;
if ( v11 )
{
v13 = v11 - v9;
if ( v11 - v9 > 6 && !memcmp(v9, "Digest ", 7uLL) )
{
v14 = v9 + 7;
v15 = 0LL;
v16 = 0LL;
LABEL_9:
while ( v14 < v12 )
{
if ( v16 && v15 )
goto LABEL_44;
while ( *v14 == 32 )
{
if ( v12 == ++v14 )
goto LABEL_9;
}
if ( v14 < v12 )
{
if ( v12 - v14 <= 6 )
{
v21 = v14;
goto LABEL_26;
}
if ( !memcmp(v14, "realm=\"", 7uLL) )
{
v18 = v14 + 7;
v16 = v14 + 7;
v19 = (size_t *)&n[1];
}
else if ( !memcmp(v14, "nonce=\"", 7uLL) )
{
v18 = v14 + 7;
v15 = v14 + 7;
v19 = (size_t *)n;
}
else
{
v21 = v14;
LABEL_26:
while ( *v21 != 34 )
{
if ( v12 <= ++v21 )
goto LABEL_28;
}
if ( v21 >= v12 || *(v21 - 1) != 61 )
{
LABEL_28:
logWrite(1LL, "rtspresponse.c", "Unknown field %.*s in WWW-Authenticate.", v12 - v14, v14);
return 0LL;
}
logWrite(
2LL,
"rtspresponse.c",
"Unknown field %.*s found in WWW-Authenticate. Skipping the value.",
v21 - v14,
v14);
v18 = v21 + 1;
v19 = 0LL;
}
if ( v18 >= v12 )
goto LABEL_24;
v20 = v18;
while ( *v20 != 34 )
{
if ( v12 == ++v20 )
goto LABEL_24;
}
if ( v20 >= v12 )
{
LABEL_24:
logWrite(1LL, "rtspresponse.c", "Incomplete field value (not properly terminated by a double quote).");
return 0LL;
}
if ( v19 )
*(_DWORD *)v19 = (_DWORD)v20 - (_DWORD)v18;
v14 = v20 + 1;
if ( v12 > v14 )
{
do
{
if ( *v14 != 32 && *v14 != 44 )
break;
++v14;
}
while ( v12 != v14 );
}
}
}
if ( !v16 || !v15 )
{
logWrite(1LL, "rtspresponse.c", "RTSP Response incomplete, fields 'realm' or 'nonce' not present.");
return 0LL;
}
LABEL_44:
v22 = n[1];
if ( n[1] <= a3 && (v23 = n[0], n[0] <= a6) )
{
memcpy(a2, v16, n[1]);
*a4 = v22;
memcpy(a5, v15, v23);
*a7 = v23;
result = 1LL;
}
else
{
logWrite(
1LL,
"rtspresponse.c",
"RTSP Response contains value(s) which are too long for buffer. Buffer for 'realm' is %u and buffer for 'nonce' is %u.",
a3,
a6);
result = 0LL;
}
}
else
{
logWrite(1LL, "rtspresponse.c", "RTSP Response has field WWW-Authenticate with unknown method %.*s", v13, v9);
result = 0LL;
}
}
else
{
logWrite(1LL, "rtspresponse.c", "RTSP Response has field WWW-Authenticate with unknown content %.*s", v10, v9);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, void *a2, unsigned int a3, unsigned int *a4, void *a5, unsigned int a6, unsigned int *a7)
{
char *v8; // rax
char *v9; // rbx
size_t v10; // rbp
_BYTE *v11; // rax
_BYTE *v12; // r12
signed __int64 v13; // rbp
_BYTE *v14; // rbx
const void *v15; // r14
const void *v16; // r13
__int64 result; // rax
_BYTE *v18; // rbp
size_t *v19; // rax
_BYTE *v20; // rbx
_BYTE *v21; // rbp
unsigned int v22; // ebx
unsigned int v23; // ebp
unsigned int n[16]; // [rsp+38h] [rbp-40h] BYREF

v8 = (char *)func0((__int64)a1, "WWW-Authenticate", 0LL);
if ( !v8 )
return 0LL;
v9 = v8;
v10 = a1[1] - (_QWORD)&v8[-*a1];
v11 = memchr(v8, 10, v10);
v12 = v11;
if ( v11 )
{
v13 = v11 - v9;
if ( v11 - v9 > 6 && !memcmp(v9, "Digest ", 7uLL) )
{
v14 = v9 + 7;
v15 = 0LL;
v16 = 0LL;
LABEL_9:
while ( v14 < v12 )
{
if ( v16 && v15 )
goto LABEL_44;
while ( *v14 == 32 )
{
if ( v12 == ++v14 )
goto LABEL_9;
}
if ( v14 < v12 )
{
if ( v12 - v14 <= 6 )
{
v21 = v14;
goto LABEL_26;
}
if ( !memcmp(v14, "realm=\"", 7uLL) )
{
v18 = v14 + 7;
v16 = v14 + 7;
v19 = (size_t *)&n[1];
}
else if ( !memcmp(v14, "nonce=\"", 7uLL) )
{
v18 = v14 + 7;
v15 = v14 + 7;
v19 = (size_t *)n;
}
else
{
v21 = v14;
LABEL_26:
while ( *v21 != 34 )
{
if ( v12 <= ++v21 )
goto LABEL_28;
}
if ( v21 >= v12 || *(v21 - 1) != 61 )
{
LABEL_28:
logWrite(1LL, "rtspresponse.c", "Unknown field %.*s in WWW-Authenticate.", v12 - v14, v14);
return 0LL;
}
logWrite(
2LL,
"rtspresponse.c",
"Unknown field %.*s found in WWW-Authenticate. Skipping the value.",
v21 - v14,
v14);
v18 = v21 + 1;
v19 = 0LL;
}
if ( v18 >= v12 )
goto LABEL_24;
v20 = v18;
while ( *v20 != 34 )
{
if ( v12 == ++v20 )
goto LABEL_24;
}
if ( v20 >= v12 )
{
LABEL_24:
logWrite(1LL, "rtspresponse.c", "Incomplete field value (not properly terminated by a double quote).");
return 0LL;
}
if ( v19 )
*(_DWORD *)v19 = (_DWORD)v20 - (_DWORD)v18;
v14 = v20 + 1;
if ( v12 > v14 )
{
do
{
if ( *v14 != 32 && *v14 != 44 )
break;
++v14;
}
while ( v12 != v14 );
}
}
}
if ( !v16 || !v15 )
{
logWrite(1LL, "rtspresponse.c", "RTSP Response incomplete, fields 'realm' or 'nonce' not present.");
return 0LL;
}
LABEL_44:
v22 = n[1];
if ( n[1] <= a3 && (v23 = n[0], n[0] <= a6) )
{
memcpy(a2, v16, n[1]);
*a4 = v22;
memcpy(a5, v15, v23);
*a7 = v23;
result = 1LL;
}
else
{
logWrite(
1LL,
"rtspresponse.c",
"RTSP Response contains value(s) which are too long for buffer. Buffer for 'realm' is %u and buffer for 'nonce' is %u.",
a3,
a6);
result = 0LL;
}
}
else
{
logWrite(1LL, "rtspresponse.c", "RTSP Response has field WWW-Authenticate with unknown method %.*s", v13, v9);
result = 0LL;
}
}
else
{
logWrite(1LL, "rtspresponse.c", "RTSP Response has field WWW-Authenticate with unknown content %.*s", v10, v9);
result = 0LL;
}
return result;
}

original_code:
_Bool rtspResponseGetAuthenticationResponse(RTSPResponse *rtspResponse, char *realmBuffer, uint32_t maxRealmBufferSize, uint32_t *realmBufferSize, char *nonceBuffer, uint32_t maxNonceBufferSize, uint32_t *nonceBufferSize) {
 char *value;
 char *endValue;
 char *realmValue;
 uint32_t realmValueSize;
 char *nonceValue;
 uint32_t nonceValueSize;
 uint32_t *fieldValueSize;
 char *tempValue;
 value = (char *)rtspResponseFindValueForKey(rtspResponse, "WWW-Authenticate", ((void *)0));
 if(value == ((void *)0)) {
  return 0;
 }
 realmValue = ((void *)0);
 nonceValue = ((void *)0);
 endValue = memchr(value, ((uint8_t)'\n'), rtspResponse->responseBufferSize - (value - (char *)rtspResponse->responseBuffer));
 if(endValue == ((void *)0)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response has field WWW-Authenticate with unknown content %.*s", rtspResponse->responseBufferSize - (value - (char *)rtspResponse->responseBuffer), value);
  return 0;
 }
 if(endValue - value < 7 || memcmp(value, "Digest ", 7) != 0) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response has field WWW-Authenticate with unknown method %.*s", endValue - value, value);
  return 0;
 }
 value += 7;
 while(value < endValue && (realmValue == ((void *)0) || nonceValue == ((void *)0))) {
  while(value < endValue && *value == ' ') {
   value++;
  }
  if(value < endValue) {
   if(endValue - value >= 7 && memcmp(value, "realm=\"", 7) == 0) {
    value += 7;
    realmValue = value;
    fieldValueSize = &realmValueSize;
   } else if(endValue - value >= 7 && memcmp(value, "nonce=\"", 7) == 0) {
    value += 7;
    nonceValue = value;
    fieldValueSize = &nonceValueSize;
   } else {
    tempValue = value;
    while(value < endValue && *value != '"') {
     value++;
    }
    if(value >= endValue || *(value - 1) != '=') {
     logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unknown field %.*s in WWW-Authenticate.", endValue - tempValue, tempValue);
     return 0;
    }
    logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Unknown field %.*s found in WWW-Authenticate. Skipping the value.", value - tempValue, tempValue);
    value++;
    fieldValueSize = ((void *)0);
   }
   tempValue = value;
   while(value < endValue && *value != '"') {
    value++;
   }
   if(value >= endValue) {
    logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Incomplete field value (not properly terminated by a double quote).");
    return 0;
   }
   if(fieldValueSize != ((void *)0)) {
    *fieldValueSize = value - tempValue;
   }
   value++;
   while(value < endValue && (*value == ' ' || *value == ',')) {
    value++;
   }
  }
 }
 if(realmValue == ((void *)0) || nonceValue == ((void *)0)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response incomplete, fields 'realm' or 'nonce' not present.");
  return 0;
 }
 if(maxRealmBufferSize < realmValueSize || maxNonceBufferSize < nonceValueSize) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response contains value(s) which are too long for buffer. Buffer for 'realm' is %" "u" " and buffer for 'nonce' is %" "u" ".", maxRealmBufferSize, maxNonceBufferSize);
  return 0;
 }
 memcpy(realmBuffer, realmValue, realmValueSize);
 *realmBufferSize = realmValueSize;
 memcpy(nonceBuffer, nonceValue, nonceValueSize);
 *nonceBufferSize = nonceValueSize;
 return 1;
}

canonical_original_code:
_Bool rtspResponseGetAuthenticationResponse(struct RTSPResponseStruct *rtspResponse, char *realmBuffer, unsigned int maxRealmBufferSize, unsigned int *realmBufferSize, char *nonceBuffer, unsigned int maxNonceBufferSize, unsigned int *nonceBufferSize) {
 char *value;
 char *endValue;
 char *realmValue;
 unsigned int realmValueSize;
 char *nonceValue;
 unsigned int nonceValueSize;
 unsigned int *fieldValueSize;
 char *tempValue;
 value = (char *)rtspResponseFindValueForKey(rtspResponse, "WWW-Authenticate", ((void *)0));
 if(value == ((void *)0)) {
  return 0;
 }
 realmValue = ((void *)0);
 nonceValue = ((void *)0);
 endValue = memchr(value, ((unsigned char)'\n'), rtspResponse->responseBufferSize - (value - (char *)rtspResponse->responseBuffer));
 if(endValue == ((void *)0)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response has field WWW-Authenticate with unknown content %.*s", rtspResponse->responseBufferSize - (value - (char *)rtspResponse->responseBuffer), value);
  return 0;
 }
 if(endValue - value < 7 || memcmp(value, "Digest ", 7) != 0) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response has field WWW-Authenticate with unknown method %.*s", endValue - value, value);
  return 0;
 }
 value += 7;
 while(value < endValue && (realmValue == ((void *)0) || nonceValue == ((void *)0))) {
  while(value < endValue && *value == ' ') {
   value++;
  }
  if(value < endValue) {
   if(endValue - value >= 7 && memcmp(value, "realm=\"", 7) == 0) {
    value += 7;
    realmValue = value;
    fieldValueSize = &realmValueSize;
   } else if(endValue - value >= 7 && memcmp(value, "nonce=\"", 7) == 0) {
    value += 7;
    nonceValue = value;
    fieldValueSize = &nonceValueSize;
   } else {
    tempValue = value;
    while(value < endValue && *value != '"') {
     value++;
    }
    if(value >= endValue || *(value - 1) != '=') {
     logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unknown field %.*s in WWW-Authenticate.", endValue - tempValue, tempValue);
     return 0;
    }
    logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Unknown field %.*s found in WWW-Authenticate. Skipping the value.", value - tempValue, tempValue);
    value++;
    fieldValueSize = ((void *)0);
   }
   tempValue = value;
   while(value < endValue && *value != '"') {
    value++;
   }
   if(value >= endValue) {
    logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Incomplete field value (not properly terminated by a double quote).");
    return 0;
   }
   if(fieldValueSize != ((void *)0)) {
    *fieldValueSize = value - tempValue;
   }
   value++;
   while(value < endValue && (*value == ' ' || *value == ',')) {
    value++;
   }
  }
 }
 if(realmValue == ((void *)0) || nonceValue == ((void *)0)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response incomplete, fields 'realm' or 'nonce' not present.");
  return 0;
 }
 if(maxRealmBufferSize < realmValueSize || maxNonceBufferSize < nonceValueSize) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "RTSP Response contains value(s) which are too long for buffer. Buffer for 'realm' is %" "u" " and buffer for 'nonce' is %" "u" ".", maxRealmBufferSize, maxNonceBufferSize);
  return 0;
 }
 memcpy(realmBuffer, realmValue, realmValueSize);
 *realmBufferSize = realmValueSize;
 memcpy(nonceBuffer, nonceValue, nonceValueSize);
 *nonceBufferSize = nonceValueSize;
 return 1;
}

variable_types:
realmBuffer
char *

variable_types:
maxRealmBufferSize
unsigned int

variable_types:
realmBufferSize
unsigned int *

variable_types:
nonceBuffer
char *

variable_types:
maxNonceBufferSize
unsigned int

variable_types:
nonceBufferSize
unsigned int *

variable_types:
value
char *

variable_types:
endValue
char *

variable_types:
realmValue
char *

variable_types:
realmValueSize
unsigned int

variable_types:
nonceValue
char *

variable_types:
nonceValueSize
unsigned int

variable_types:
fieldValueSize
unsigned int *

variable_types:
tempValue
char *


rtspResponseFree

decompiled_code:
__int64 __fastcall rtspResponseFree(__int64 *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebx
unsigned int v4; // eax

v2 = *a1;
v3 = 1;
if ( v2 )
{
v3 = voidBufferFree(v2);
v4 = voidBufferFree(a1);
if ( !(_BYTE)v4 )
v3 = v4;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebx
unsigned int v4; // eax

v2 = *a1;
v3 = 1;
if ( v2 )
{
v3 = voidBufferFree(v2);
v4 = voidBufferFree(a1);
if ( !(_BYTE)v4 )
v3 = v4;
}
return v3;
}

original_code:
_Bool rtspResponseFree(RTSPResponse **rtspResponse) {
 _Bool result;
 result = 1;
 if(*rtspResponse != ((void *)0)) {
  if(!voidBufferFree((void **)&(*rtspResponse)->responseBuffer)) {
   result = 0;
  }
  if(!voidBufferFree((void **)rtspResponse)) {
   result = 0;
  }
 }
 return result;
}

canonical_original_code:
_Bool rtspResponseFree(struct RTSPResponseStruct **rtspResponse) {
 _Bool result;
 result = 1;
 if(*rtspResponse != ((void *)0)) {
  if(!voidBufferFree((void **)&(*rtspResponse)->responseBuffer)) {
   result = 0;
  }
  if(!voidBufferFree((void **)rtspResponse)) {
   result = 0;
  }
 }
 return result;
}

variable_types: struct RTSPResponseStruct { unsigned char *responseBuffer; unsigned long responseBufferSize; unsigned long maxResponseBufferSize; } **


logSetLogLevel

decompiled_code:
void __fastcall logSetLogLevel(int a1)
{
defaultLogLevel = a1;
}

canonical_decompiled_code:
void __fastcall func2(int a1)
{
defaultLogLevel = a1;
}

original_code:
void logSetLogLevel(LogLevel newDefaultlogLevel) {
 defaultLogLevel = newDefaultlogLevel;
}

canonical_original_code:
void logSetLogLevel(enum LogLevel newDefaultlogLevel) {
 defaultLogLevel = newDefaultlogLevel;
}

variable_types: enum LogLevel { LOG_LEVEL_FATAL=0, LOG_LEVEL_ERROR=1, LOG_LEVEL_WARNING=2, LOG_LEVEL_INFO=3, LOG_LEVEL_DEBUG=4 }

return_type: void


logWrite

decompiled_code:
bool logWrite(unsigned int a1, const char *a2, const char *a3, ...)
{
bool result; // al
FILE *v4; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

result = 1;
if ( defaultLogLevel >= a1 )
{
va_start(va, a3);
v4 = logFile;
if ( !logFile )
v4 = stderr;
result = logWriteMessage(v4, a1, a2, a3, va);
}
return result;
}

canonical_decompiled_code:
bool func3(unsigned int a1, const char *a2, const char *a3, ...)
{
bool result; // al
FILE *v4; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

result = 1;
if ( defaultLogLevel >= a1 )
{
va_start(va, a3);
v4 = logFile;
if ( !logFile )
v4 = stderr;
result = func0(v4, a1, a2, a3, va);
}
return result;
}

original_code:
_Bool logWrite(LogLevel logLevel, const char *componentName, const char *logFormat, ...) {
 _Bool result;
 va_list argumentList;
 if(logLevel > defaultLogLevel) {
  return 1;
 }
 __builtin_va_start(argumentList,logFormat);
 result = logWriteMessage(logFile != ((void *)0) ? logFile : stderr, logLevel, componentName, logFormat, argumentList);
 __builtin_va_end(argumentList);
 return result;
}

canonical_original_code:
_Bool logWrite(enum LogLevel logLevel, char *componentName, char *logFormat, ...) {
 _Bool result;
 __builtin_va_list argumentList;
 if(logLevel > defaultLogLevel) {
  return 1;
 }
 __builtin_va_start(argumentList,logFormat);
 result = logWriteMessage(logFile != ((void *)0) ? logFile : stderr, logLevel, componentName, logFormat, argumentList);
 __builtin_va_end(argumentList);
 return result;
}

variable_types:
componentName
char *

variable_types:
logFormat
char *

user_defined_types: enum LogLevel { LOG_LEVEL_FATAL=0, LOG_LEVEL_ERROR=1, LOG_LEVEL_WARNING=2, LOG_LEVEL_INFO=3, LOG_LEVEL_DEBUG=4 }


logClose

decompiled_code:
__int64 logClose()
{
__int64 result; // rax
int *v1; // rax

result = 1LL;
if ( logFile )
{
if ( logFileOpenedLocally )
{
if ( fclose(logFile) )
{
v1 = _errno_location();
logWriteInternal(1u, "log.c", "Cannot close logfile. (errno = %d)", (unsigned int)*v1);
logFile = 0LL;
result = 0LL;
}
else
{
logFile = 0LL;
result = 1LL;
}
}
else
{
logFile = 0LL;
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
int *v1; // rax

result = 1LL;
if ( logFile )
{
if ( logFileOpenedLocally )
{
if ( fclose(logFile) )
{
v1 = _errno_location();
func1(1u, "log.c", "Cannot close logfile. (errno = %d)", (unsigned int)*v1);
logFile = 0LL;
result = 0LL;
}
else
{
logFile = 0LL;
result = 1LL;
}
}
else
{
logFile = 0LL;
result = 1LL;
}
}
return result;
}

original_code:
_Bool logClose() {
 if(logFile != ((void *)0)) {
  if(logFileOpenedLocally) {
   if(fclose(logFile) != 0) {
    logWriteInternal(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot close logfile. (errno = %d)", (*__errno_location ()));
    logFile = ((void *)0);
    return 0;
   }
  }
  logFile = ((void *)0);
 }
 return 1;
}


rtspRequestReset

original_code:
_Bool rtspRequestReset(RTSPRequest *rtspRequest, RTSPRequestMethod requestMethod) {
 if(rtspRequest->headerBuffer != ((void *)0)) {
  rtspRequest->headerBuffer[0] = '\0';
  rtspRequest->headerBufferSize = 1;
 } else {
  rtspRequest->headerBufferSize = 0;
 }
 rtspRequest->contentBufferSize = 0;
 rtspRequest->requestMethod = requestMethod;
 return 1;
}

canonical_original_code:
_Bool rtspRequestReset(struct RTSPRequestStruct *rtspRequest, enum RTSPRequestMethod requestMethod) {
 if(rtspRequest->headerBuffer != ((void *)0)) {
  rtspRequest->headerBuffer[0] = '\0';
  rtspRequest->headerBufferSize = 1;
 } else {
  rtspRequest->headerBufferSize = 0;
 }
 rtspRequest->contentBufferSize = 0;
 rtspRequest->requestMethod = requestMethod;
 return 1;
}

variable_types: struct RTSPRequestStruct { enum RTSPRequestMethod { RTSP_METHOD_OPTIONS=0, RTSP_METHOD_ANNOUNCE=1, RTSP_METHOD_SETUP=2, RTSP_METHOD_RECORD=3, RTSP_METHOD_SET_PARAMETER=4, RTSP_METHOD_FLUSH=5, RTSP_METHOD_TEARDOWN=6 } requestMethod; unsigned char *headerBuffer; unsigned long headerBufferSize; unsigned long maxHeaderBufferSize; unsigned char *contentBuffer; unsigned long contentBufferSize; } *

user_defined_types: struct RTSPRequestStruct { enum RTSPRequestMethod requestMethod; unsigned char *headerBuffer; unsigned long headerBufferSize; unsigned long maxHeaderBufferSize; unsigned char *contentBuffer; unsigned long contentBufferSize; }


rtspRequestGetMethodName

decompiled_code:
char *__fastcall rtspRequestGetMethodName(unsigned int *a1)
{
return (&METHOD_NAMES)[*a1];
}

canonical_decompiled_code:
char *__fastcall func1(unsigned int *a1)
{
return (&METHOD_NAMES)[*a1];
}

original_code:
const char *rtspRequestGetMethodName(RTSPRequest *rtspRequest) {
 return METHOD_NAMES[rtspRequest->requestMethod];
}

canonical_original_code:
char *rtspRequestGetMethodName(struct RTSPRequestStruct *rtspRequest) {
 return METHOD_NAMES[rtspRequest->requestMethod];
}

return_type: char *

user_defined_types: enum RTSPRequestMethod { RTSP_METHOD_OPTIONS=0, RTSP_METHOD_ANNOUNCE=1, RTSP_METHOD_SETUP=2, RTSP_METHOD_RECORD=3, RTSP_METHOD_SET_PARAMETER=4, RTSP_METHOD_FLUSH=5, RTSP_METHOD_TEARDOWN=6 }


rtspRequestAddHeaderField

decompiled_code:
__int64 __fastcall rtspRequestAddHeaderField(__int64 a1, const char *a2, const char *a3)
{
size_t v4; // rbp
size_t v5; // r14
unsigned int v6; // ebp
int v7; // eax

if ( !*(_QWORD *)(a1 + 8) )
{
*(_QWORD *)(a1 + 24) = 1024LL;
v6 = voidBufferAllocate(a1 + 8, 1024LL, "RTSP request header buffer");
if ( !(_BYTE)v6 )
return v6;
**(_BYTE **)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = 1LL;
}
v4 = strlen(a2);
v5 = v4 + strlen(a3) + 4;
v6 = voidBufferMakeRoom(a1 + 8, a1 + 24, *(_QWORD *)(a1 + 16), v5, 512LL);
if ( (_BYTE)v6 )
{
v7 = snprintf(
(char *)(*(_QWORD *)(a1 + 8) + *(_QWORD *)(a1 + 16) - 1LL),
*(_QWORD *)(a1 + 24) + 1LL - *(_QWORD *)(a1 + 16),
"%s: %s\r\n",
a2,
a3);
if ( v5 == v7 )
{
*(_QWORD *)(a1 + 16) += v7;
}
else
{
logWrite(1LL, "rtsprequest.c", "Cannot add field \"%s\" to RTSP Request header.", a2);
v6 = 0;
}
}
else
{
logWrite(1LL, "rtsprequest.c", "Cannot allocate memory to add field \"%s\" to RTSP Request header.", a2);
voidBufferFree(a1 + 8);
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, const char *a3)
{
size_t v4; // rbp
size_t v5; // r14
unsigned int v6; // ebp
int v7; // eax

if ( !*(_QWORD *)(a1 + 8) )
{
*(_QWORD *)(a1 + 24) = 1024LL;
v6 = voidBufferAllocate(a1 + 8, 1024LL, "RTSP request header buffer");
if ( !(_BYTE)v6 )
return v6;
**(_BYTE **)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = 1LL;
}
v4 = strlen(a2);
v5 = v4 + strlen(a3) + 4;
v6 = voidBufferMakeRoom(a1 + 8, a1 + 24, *(_QWORD *)(a1 + 16), v5, 512LL);
if ( (_BYTE)v6 )
{
v7 = snprintf(
(char *)(*(_QWORD *)(a1 + 8) + *(_QWORD *)(a1 + 16) - 1LL),
*(_QWORD *)(a1 + 24) + 1LL - *(_QWORD *)(a1 + 16),
"%s: %s\r\n",
a2,
a3);
if ( v5 == v7 )
{
*(_QWORD *)(a1 + 16) += v7;
}
else
{
logWrite(1LL, "rtsprequest.c", "Cannot add field \"%s\" to RTSP Request header.", a2);
v6 = 0;
}
}
else
{
logWrite(1LL, "rtsprequest.c", "Cannot allocate memory to add field \"%s\" to RTSP Request header.", a2);
voidBufferFree(a1 + 8);
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
}
return v6;
}

original_code:
_Bool rtspRequestAddHeaderField(RTSPRequest *rtspRequest, const char *fieldName, const char *fieldValue) {
 size_t fieldNameLength;
 size_t fieldValueLength;
 int charsWritten;
 if(rtspRequest->headerBuffer == ((void *)0)) {
  rtspRequest->maxHeaderBufferSize = 1024;
  if(!voidBufferAllocate((void **)&rtspRequest->headerBuffer, rtspRequest->maxHeaderBufferSize, "RTSP request header buffer")) {
   return 0;
  }
  rtspRequest->headerBuffer[0] = '\0';
  rtspRequest->headerBufferSize = 1;
 }
 fieldNameLength = strlen(fieldName);
 fieldValueLength = strlen(fieldValue);
 if(!voidBufferMakeRoom((void **)&rtspRequest->headerBuffer, &rtspRequest->maxHeaderBufferSize, rtspRequest->headerBufferSize, fieldNameLength + fieldValueLength + 4, 512)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot allocate memory to add field \"%s\" to RTSP Request header.", fieldName);
  voidBufferFree((void **)&rtspRequest->headerBuffer);
  rtspRequest->headerBuffer = ((void *)0);
  rtspRequest->headerBufferSize = 0;
  return 0;
 }
 charsWritten = snprintf((char *)rtspRequest->headerBuffer + rtspRequest->headerBufferSize - 1, rtspRequest->maxHeaderBufferSize - (rtspRequest->headerBufferSize - 1), "%s: %s\r\n", fieldName, fieldValue);
 if(charsWritten != fieldNameLength + fieldValueLength + 4) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot add field \"%s\" to RTSP Request header.", fieldName);
  return 0;
 }
 rtspRequest->headerBufferSize += charsWritten;
 return 1;
}

canonical_original_code:
_Bool rtspRequestAddHeaderField(struct RTSPRequestStruct *rtspRequest, char *fieldName, char *fieldValue) {
 unsigned long fieldNameLength;
 unsigned long fieldValueLength;
 int charsWritten;
 if(rtspRequest->headerBuffer == ((void *)0)) {
  rtspRequest->maxHeaderBufferSize = 1024;
  if(!voidBufferAllocate((void **)&rtspRequest->headerBuffer, rtspRequest->maxHeaderBufferSize, "RTSP request header buffer")) {
   return 0;
  }
  rtspRequest->headerBuffer[0] = '\0';
  rtspRequest->headerBufferSize = 1;
 }
 fieldNameLength = strlen(fieldName);
 fieldValueLength = strlen(fieldValue);
 if(!voidBufferMakeRoom((void **)&rtspRequest->headerBuffer, &rtspRequest->maxHeaderBufferSize, rtspRequest->headerBufferSize, fieldNameLength + fieldValueLength + 4, 512)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot allocate memory to add field \"%s\" to RTSP Request header.", fieldName);
  voidBufferFree((void **)&rtspRequest->headerBuffer);
  rtspRequest->headerBuffer = ((void *)0);
  rtspRequest->headerBufferSize = 0;
  return 0;
 }
 charsWritten = snprintf((char *)rtspRequest->headerBuffer + rtspRequest->headerBufferSize - 1, rtspRequest->maxHeaderBufferSize - (rtspRequest->headerBufferSize - 1), "%s: %s\r\n", fieldName, fieldValue);
 if(charsWritten != fieldNameLength + fieldValueLength + 4) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot add field \"%s\" to RTSP Request header.", fieldName);
  return 0;
 }
 rtspRequest->headerBufferSize += charsWritten;
 return 1;
}

variable_types:
fieldName
char *

variable_types:
fieldValue
char *

variable_types:
fieldNameLength
unsigned long

variable_types:
fieldValueLength
unsigned long

variable_types: charsWritten int


rtspRequestSetContent

decompiled_code:
__int64 __fastcall rtspRequestSetContent(__int64 a1, const void *a2, size_t a3, const char *a4)
{
unsigned int v5; // ebx
char s[51]; // [rsp+5h] [rbp-33h] BYREF

v5 = rtspRequestAddHeaderField(a1, "Content-Type", a4);
if ( (_BYTE)v5 )
{
sprintf(s, "%lu", a3);
v5 = rtspRequestAddHeaderField(a1, "Content-Length", s);
if ( (_BYTE)v5 )
{
if ( !*(_QWORD *)(a1 + 32) || (v5 = voidBufferFree(a1 + 32), (_BYTE)v5) )
{
v5 = voidBufferAllocate(a1 + 32, a3, "RTSP request content buffer");
if ( (_BYTE)v5 )
{
memcpy(*(void **)(a1 + 32), a2, a3);
*(_QWORD *)(a1 + 40) = a3;
}
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const void *a2, size_t a3, const char *a4)
{
unsigned int v5; // ebx
char s[51]; // [rsp+5h] [rbp-33h] BYREF

v5 = func3(a1, "Content-Type", a4);
if ( (_BYTE)v5 )
{
sprintf(s, "%lu", a3);
v5 = func3(a1, "Content-Length", s);
if ( (_BYTE)v5 )
{
if ( !*(_QWORD *)(a1 + 32) || (v5 = voidBufferFree(a1 + 32), (_BYTE)v5) )
{
v5 = voidBufferAllocate(a1 + 32, a3, "RTSP request content buffer");
if ( (_BYTE)v5 )
{
memcpy(*(void **)(a1 + 32), a2, a3);
*(_QWORD *)(a1 + 40) = a3;
}
}
}
}
return v5;
}

original_code:
_Bool rtspRequestSetContent(RTSPRequest *rtspRequest, uint8_t *content, size_t contentSize, char *contentType) {
 char contentSizeString[11];
 if(!rtspRequestAddHeaderField(rtspRequest, "Content-Type", contentType)) {
  return 0;
 }
 sprintf(contentSizeString, "%lu", (unsigned long)contentSize);
 if(!rtspRequestAddHeaderField(rtspRequest, "Content-Length", contentSizeString)) {
  return 0;
 }
 if(rtspRequest->contentBuffer != ((void *)0)) {
  if(!voidBufferFree((void **)&rtspRequest->contentBuffer)) {
   return 0;
  }
 }
 if(!voidBufferAllocate((void **)&rtspRequest->contentBuffer, contentSize, "RTSP request content buffer")) {
  return 0;
 }
 memcpy(rtspRequest->contentBuffer, content, contentSize);
 rtspRequest->contentBufferSize = contentSize;
 return 1;
}

canonical_original_code:
_Bool rtspRequestSetContent(struct RTSPRequestStruct *rtspRequest, unsigned char *content, unsigned long contentSize, char *contentType) {
 char contentSizeString[11];
 if(!rtspRequestAddHeaderField(rtspRequest, "Content-Type", contentType)) {
  return 0;
 }
 sprintf(contentSizeString, "%lu", (unsigned long)contentSize);
 if(!rtspRequestAddHeaderField(rtspRequest, "Content-Length", contentSizeString)) {
  return 0;
 }
 if(rtspRequest->contentBuffer != ((void *)0)) {
  if(!voidBufferFree((void **)&rtspRequest->contentBuffer)) {
   return 0;
  }
 }
 if(!voidBufferAllocate((void **)&rtspRequest->contentBuffer, contentSize, "RTSP request content buffer")) {
  return 0;
 }
 memcpy(rtspRequest->contentBuffer, content, contentSize);
 rtspRequest->contentBufferSize = contentSize;
 return 1;
}

variable_types:
content
unsigned char *

variable_types:
contentSize
unsigned long

variable_types:
contentType
char *

variable_types:
contentSizeString
char [11]


rtspRequestFree

decompiled_code:
__int64 __fastcall rtspRequestFree(__int64 *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // eax

v2 = *a1;
v3 = 1;
if ( v2 )
{
v3 = voidBufferFree(v2 + 8);
v4 = voidBufferFree(*a1 + 32);
if ( !(_BYTE)v4 )
v3 = v4;
v5 = voidBufferFree(a1);
if ( !(_BYTE)v5 )
v3 = v5;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // eax

v2 = *a1;
v3 = 1;
if ( v2 )
{
v3 = voidBufferFree(v2 + 8);
v4 = voidBufferFree(*a1 + 32);
if ( !(_BYTE)v4 )
v3 = v4;
v5 = voidBufferFree(a1);
if ( !(_BYTE)v5 )
v3 = v5;
}
return v3;
}

original_code:
_Bool rtspRequestFree(RTSPRequest **rtspRequest) {
 _Bool result;
 result = 1;
 if(*rtspRequest != ((void *)0)) {
  if(!voidBufferFree((void **)&(*rtspRequest)->headerBuffer)) {
   result = 0;
  }
  if(!voidBufferFree((void **)&(*rtspRequest)->contentBuffer)) {
   result = 0;
  }
  if(!voidBufferFree((void **)rtspRequest)) {
   result = 0;
  }
 }
 return result;
}

canonical_original_code:
_Bool rtspRequestFree(struct RTSPRequestStruct **rtspRequest) {
 _Bool result;
 result = 1;
 if(*rtspRequest != ((void *)0)) {
  if(!voidBufferFree((void **)&(*rtspRequest)->headerBuffer)) {
   result = 0;
  }
  if(!voidBufferFree((void **)&(*rtspRequest)->contentBuffer)) {
   result = 0;
  }
  if(!voidBufferFree((void **)rtspRequest)) {
   result = 0;
  }
 }
 return result;
}

variable_types: struct RTSPRequestStruct { enum RTSPRequestMethod { RTSP_METHOD_OPTIONS=0, RTSP_METHOD_ANNOUNCE=1, RTSP_METHOD_SETUP=2, RTSP_METHOD_RECORD=3, RTSP_METHOD_SET_PARAMETER=4, RTSP_METHOD_FLUSH=5, RTSP_METHOD_TEARDOWN=6 } requestMethod; unsigned char *headerBuffer; unsigned long headerBufferSize; unsigned long maxHeaderBufferSize; unsigned char *contentBuffer; unsigned long contentBufferSize; } **


signalHandler

decompiled_code:
void __fastcall signalHandler(int a1)
{
unsigned int v1[6]; // [rsp+0h] [rbp-18h] BYREF

if ( a1 == 2 )
{
if ( (unsigned __int8)raopClientGetProgress(raopClient, v1) )
logWrite(3LL, "light-play.c", "Progress so far: %u seconds", v1[0]);
logWrite(2LL, "light-play.c", "Stop playing before end of file on user request.");
raopClientStopPlaying(raopClient);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
unsigned int v1[6]; // [rsp+0h] [rbp-18h] BYREF

if ( a1 == 2 )
{
if ( (unsigned __int8)raopClientGetProgress(raopClient, v1) )
logWrite(3LL, "light-play.c", "Progress so far: %u seconds", v1[0]);
logWrite(2LL, "light-play.c", "Stop playing before end of file on user request.");
raopClientStopPlaying(raopClient);
}
}

original_code:
void signalHandler(int signalNumber) {
 struct timespec progress;
 if(signalNumber == 2) {
  if(raopClientGetProgress(raopClient, &progress)) {
   logWrite(LOG_LEVEL_INFO, LOG_COMPONENT_NAME, "Progress so far: %" "u" " seconds", (uint32_t)(progress.tv_sec));
  }
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Stop playing before end of file on user request.");
  raopClientStopPlaying(raopClient);
 }
}

canonical_original_code:
void signalHandler(int signalNumber) {
 struct timespec progress;
 if(signalNumber == 2) {
  if(raopClientGetProgress(raopClient, &progress)) {
   logWrite(LOG_LEVEL_INFO, LOG_COMPONENT_NAME, "Progress so far: %" "u" " seconds", (unsigned int)(progress.tv_sec));
  }
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Stop playing before end of file on user request.");
  raopClientStopPlaying(raopClient);
 }
}

variable_types: signalNumber int

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


printUsage

decompiled_code:
int printUsage(char *s, char *format, ...)
{
char *v2; // rbx
char *v3; // rax
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

v2 = s;
v3 = strrchr(s, 47);
if ( v3 )
v2 = v3 + 1;
result = fprintf(
stderr,
"Usage: %s [-?hcpvlo] <url> <filename>\n"
"\n"
" -? | -h Print this usage message\n"
" -c[ ]<password> Set password for using AirPort Express\n"
" -p[ ]<portname> Set name/number of AirTunes port (default: 5000)\n"
" -v[e|w|i|d] Set logging verbosity (default: w)\n"
" e: only errors\n"
" w: errors and warnings (default)\n"
" i: errors, warnings and info\n"
" d: all (includes debug info)\n"
" -l[ ]<filename> Set logging to specified file\n"
" -o[ ]<offset> Set offset (in seconds) from begin of file where to start playing\n",
v2);
if ( format )
{
va_start(arg, format);
fwrite("\n\n", 1uLL, 2uLL, stderr);
vfprintf(stderr, format, arg);
result = fputc(10, stderr);
}
return result;
}

canonical_decompiled_code:
int func1(char *s, char *format, ...)
{
char *v2; // rbx
char *v3; // rax
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

v2 = s;
v3 = strrchr(s, 47);
if ( v3 )
v2 = v3 + 1;
result = fprintf(
stderr,
"Usage: %s [-?hcpvlo] <url> <filename>\n"
"\n"
" -? | -h Print this usage message\n"
" -c[ ]<password> Set password for using AirPort Express\n"
" -p[ ]<portname> Set name/number of AirTunes port (default: 5000)\n"
" -v[e|w|i|d] Set logging verbosity (default: w)\n"
" e: only errors\n"
" w: errors and warnings (default)\n"
" i: errors, warnings and info\n"
" d: all (includes debug info)\n"
" -l[ ]<filename> Set logging to specified file\n"
" -o[ ]<offset> Set offset (in seconds) from begin of file where to start playing\n",
v2);
if ( format )
{
va_start(arg, format);
fwrite("\n\n", 1uLL, 2uLL, stderr);
vfprintf(stderr, format, arg);
result = fputc(10, stderr);
}
return result;
}

original_code:
void printUsage(const char *appName, const char *printFormat, ...) {
 char *shortAppName;
 va_list argumentList;
 shortAppName = strrchr(appName, '/');
 if(shortAppName == ((void *)0)) {
  shortAppName = (char *)appName;
 } else {
  shortAppName++;
  if(shortAppName == '\0') {
   shortAppName = (char *)appName;
  }
 }
 fprintf(stderr, "Usage: %s [-?hcpvlo] <url> <filename>\n\n" "    -? | -h          Print this usage message\n" "    -c[ ]<password>  Set password for using AirPort Express\n" "    -p[ ]<portname>  Set name/number of AirTunes port (default: 5000)\n"
   "    -v[e|w|i|d]      Set logging verbosity (default: w)\n"
   "                         e: only errors\n"
   "                         w: errors and warnings (default)\n"
   "                         i: errors, warnings and info\n"
                        "                         d: all (includes debug info)\n"
   "    -l[ ]<filename>  Set logging to specified file\n"
   "    -o[ ]<offset>    Set offset (in seconds) from begin of file where to start playing\n", shortAppName);
 if(printFormat != ((void *)0)) {
  __builtin_va_start(argumentList,printFormat);
  fputs("\n\n", stderr);
  vfprintf(stderr, printFormat, argumentList);
  __builtin_va_end(argumentList);
  fputs("\n", stderr);
 }
}

canonical_original_code:
void printUsage(char *appName, char *printFormat, ...) {
 char *shortAppName;
 __builtin_va_list argumentList;
 shortAppName = strrchr(appName, '/');
 if(shortAppName == ((void *)0)) {
  shortAppName = (char *)appName;
 } else {
  shortAppName++;
  if(shortAppName == '\0') {
   shortAppName = (char *)appName;
  }
 }
 fprintf(stderr, "Usage: %s [-?hcpvlo] <url> <filename>\n\n" "    -? | -h          Print this usage message\n" "    -c[ ]<password>  Set password for using AirPort Express\n" "    -p[ ]<portname>  Set name/number of AirTunes port (default: 5000)\n"
   "    -v[e|w|i|d]      Set logging verbosity (default: w)\n"
   "                         e: only errors\n"
   "                         w: errors and warnings (default)\n"
   "                         i: errors, warnings and info\n"
                        "                         d: all (includes debug info)\n"
   "    -l[ ]<filename>  Set logging to specified file\n"
   "    -o[ ]<offset>    Set offset (in seconds) from begin of file where to start playing\n", shortAppName);
 if(printFormat != ((void *)0)) {
  __builtin_va_start(argumentList,printFormat);
  fputs("\n\n", stderr);
  vfprintf(stderr, printFormat, argumentList);
  __builtin_va_end(argumentList);
  fputs("\n", stderr);
 }
}

variable_types:
appName
char *

variable_types:
printFormat
char *

variable_types:
shortAppName
char *


voidBufferAllocate

decompiled_code:
__int64 __fastcall voidBufferAllocate(_QWORD *a1, size_t a2, const char *a3)
{
void *v4; // rax
__int64 result; // rax

v4 = malloc(a2);
if ( v4 )
{
*a1 = v4;
++bufferAllocateCount;
result = 1LL;
}
else
{
if ( !a3 )
a3 = "a buffer";
logWrite(1LL, "buffer.c", "Cannot allocate memory (%lu bytes) for %s.", a2, a3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, size_t a2, const char *a3)
{
void *v4; // rax
__int64 result; // rax

v4 = malloc(a2);
if ( v4 )
{
*a1 = v4;
++bufferAllocateCount;
result = 1LL;
}
else
{
if ( !a3 )
a3 = "a buffer";
logWrite(1LL, "buffer.c", "Cannot allocate memory (%lu bytes) for %s.", a2, a3);
result = 0LL;
}
return result;
}

original_code:
_Bool voidBufferAllocate(void **buffer, size_t bufferSize, const char *purpose) {
 void *newBuffer;
 newBuffer = malloc(bufferSize);
 if(newBuffer == ((void *)0)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot allocate memory (%lu bytes) for %s.", (unsigned long)bufferSize, purpose == ((void *)0) ? "a buffer" : purpose);
  return 0;
 }
 *buffer = newBuffer;
 bufferAllocateCount++;
 return 1;
}

canonical_original_code:
_Bool voidBufferAllocate(void **buffer, unsigned long bufferSize, char *purpose) {
 void *newBuffer;
 newBuffer = malloc(bufferSize);
 if(newBuffer == ((void *)0)) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot allocate memory (%lu bytes) for %s.", (unsigned long)bufferSize, purpose == ((void *)0) ? "a buffer" : purpose);
  return 0;
 }
 *buffer = newBuffer;
 bufferAllocateCount++;
 return 1;
}

variable_types:
buffer
void **

variable_types:
bufferSize
unsigned long

variable_types:
purpose
char *

variable_types:
newBuffer
void *


voidBufferFree

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

v2 = *a1;
if ( v2 )
{
free(v2);
*a1 = 0LL;
--bufferAllocateCount;
}
return 1LL;
}

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

v2 = *a1;
if ( v2 )
{
free(v2);
*a1 = 0LL;
--bufferAllocateCount;
}
return 1LL;
}

original_code:
_Bool voidBufferFree(void **buffer) {
 if(*buffer != ((void *)0)) {
  free(*buffer);
  *buffer = ((void *)0);
  bufferAllocateCount--;
 }
 return 1;
}


bufferGetBuffersInUse

decompiled_code:
__int64 bufferGetBuffersInUse()
{
return (unsigned int)bufferAllocateCount;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)bufferAllocateCount;
}

original_code:
int32_t bufferGetBuffersInUse() {
 return bufferAllocateCount;
}

canonical_original_code:
int bufferGetBuffersInUse() {
 return bufferAllocateCount;
}

return_type: int


voidBufferMakeRoom

decompiled_code:
__int64 __fastcall voidBufferMakeRoom(const void **a1, size_t *a2, size_t a3, __int64 a4, __int64 a5)
{
size_t v5; // rcx
__int64 result; // rax
size_t i; // rbx
void *v9; // [rsp+0h] [rbp-38h] BYREF
void *dest; // [rsp+8h] [rbp-30h] BYREF

v5 = a3 + a4;
result = 1LL;
if ( v5 > *a2 )
{
for ( i = a5 + *a2; v5 > i; i += a5 )
;
result = voidBufferAllocate(&dest, i, "a larger buffer");
if ( (_BYTE)result )
{
memcpy(dest, *a1, a3);
v9 = (void *)*a1;
*a1 = dest;
*a2 = i;
result = voidBufferFree(&v9);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const void **a1, size_t *a2, size_t a3, __int64 a4, __int64 a5)
{
size_t v5; // rcx
__int64 result; // rax
size_t i; // rbx
void *v9; // [rsp+0h] [rbp-38h] BYREF
void *dest; // [rsp+8h] [rbp-30h] BYREF

v5 = a3 + a4;
result = 1LL;
if ( v5 > *a2 )
{
for ( i = a5 + *a2; v5 > i; i += a5 )
;
result = func0(&dest, i, "a larger buffer");
if ( (_BYTE)result )
{
memcpy(dest, *a1, a3);
v9 = (void *)*a1;
*a1 = dest;
*a2 = i;
result = func1(&v9);
}
}
return result;
}

original_code:
_Bool voidBufferMakeRoom(void **buffer, size_t *maxBufferSize, size_t bufferSize, size_t requiredSize, size_t incrementSize) {
 void *newBuffer;
 void *tmpBuffer;
 size_t newSize;
 if(bufferSize + requiredSize <= *maxBufferSize) {
  return 1;
 }
 newSize = *maxBufferSize + incrementSize;
 while(bufferSize + requiredSize > newSize) {
  newSize += incrementSize;
 }
 if(!voidBufferAllocate((void **)&newBuffer, newSize, "a larger buffer")) {
  return 0;
 }
 memcpy(newBuffer, *buffer, bufferSize);
 tmpBuffer = *buffer;
 *buffer = newBuffer;
 *maxBufferSize = newSize;
 return voidBufferFree((void **)&tmpBuffer);
}

canonical_original_code:
_Bool voidBufferMakeRoom(void **buffer, unsigned long *maxBufferSize, unsigned long bufferSize, unsigned long requiredSize, unsigned long incrementSize) {
 void *newBuffer;
 void *tmpBuffer;
 unsigned long newSize;
 if(bufferSize + requiredSize <= *maxBufferSize) {
  return 1;
 }
 newSize = *maxBufferSize + incrementSize;
 while(bufferSize + requiredSize > newSize) {
  newSize += incrementSize;
 }
 if(!voidBufferAllocate((void **)&newBuffer, newSize, "a larger buffer")) {
  return 0;
 }
 memcpy(newBuffer, *buffer, bufferSize);
 tmpBuffer = *buffer;
 *buffer = newBuffer;
 *maxBufferSize = newSize;
 return voidBufferFree((void **)&tmpBuffer);
}

variable_types:
maxBufferSize
unsigned long *

variable_types:
requiredSize
unsigned long

variable_types:
incrementSize
unsigned long

variable_types:
tmpBuffer
void *

variable_types:
newSize
unsigned long


repo: ErikTraedal/MeleProxy

decompiled_code:
void signal_handler()
{
puts("Stopping application");
running = 0;
}

canonical_decompiled_code:
void func0()
{
puts("Stopping application");
running = 0;
}

original_code:
void signal_handler(int number) {
 printf("Stopping application\n");
 running = 0;
}

variable_types: number int


decompiled_code:
int __fastcall create_lookup_table(__int16 *a1)
{
unsigned __int16 *v1; // rbx
const __int16 *v2; // rax
unsigned __int16 v3; // bp
__int64 v4; // rdx
int v5; // eax
unsigned __int16 v6; // r14
unsigned __int16 v7; // bp

puts("Creating lookup table");
v1 = keymap_keys;
v2 = keymap_keys;
do
{
if ( (unsigned int)*v2 > (unsigned __int16)*a1 )
*a1 = *v2;
v2 += 2;
}
while ( v2 != (const __int16 *)&unk_640 );
v3 = *a1;
*((_QWORD *)a1 + 1) = malloc(2LL * (unsigned __int16)*a1);
printf("Filling lookup table with data (number of items = %i\n", v3);
v4 = 0LL;
v5 = 0;
do
{
if ( (unsigned int)(v5 - 1) > 0x52 )
*(_WORD *)(*((_QWORD *)a1 + 1) + v4) = 0;
else
*(_WORD *)(*((_QWORD *)a1 + 1) + v4) = v5;
++v5;
v4 += 2LL;
}
while ( (unsigned __int16)*a1 >= v5 );
do
{
v6 = v1[1];
v7 = *v1;
printf("Replacing key %i with key %i\n", *v1, v6);
*(_WORD *)(*((_QWORD *)a1 + 1) + 2LL * v7) = v6;
v1 += 2;
}
while ( v1 != (unsigned __int16 *)&unk_640 );
return puts("Finished creating lookup table");
}

canonical_decompiled_code:
int __fastcall func1(__int16 *a1)
{
unsigned __int16 *v1; // rbx
const __int16 *v2; // rax
unsigned __int16 v3; // bp
__int64 v4; // rdx
int v5; // eax
unsigned __int16 v6; // r14
unsigned __int16 v7; // bp

puts("Creating lookup table");
v1 = keymap_keys;
v2 = keymap_keys;
do
{
if ( (unsigned int)*v2 > (unsigned __int16)*a1 )
*a1 = *v2;
v2 += 2;
}
while ( v2 != (const __int16 *)&unk_640 );
v3 = *a1;
*((_QWORD *)a1 + 1) = malloc(2LL * (unsigned __int16)*a1);
printf("Filling lookup table with data (number of items = %i\n", v3);
v4 = 0LL;
v5 = 0;
do
{
if ( (unsigned int)(v5 - 1) > 0x52 )
*(_WORD *)(*((_QWORD *)a1 + 1) + v4) = 0;
else
*(_WORD *)(*((_QWORD *)a1 + 1) + v4) = v5;
++v5;
v4 += 2LL;
}
while ( (unsigned __int16)*a1 >= v5 );
do
{
v6 = v1[1];
v7 = *v1;
printf("Replacing key %i with key %i\n", *v1, v6);
*(_WORD *)(*((_QWORD *)a1 + 1) + 2LL * v7) = v6;
v1 += 2;
}
while ( v1 != (unsigned __int16 *)&unk_640 );
return puts("Finished creating lookup table");
}

original_code:
void create_lookup_table(struct key_lookup_table *table) {
 int i, number_of_items;
 printf("Creating lookup table\n");
 number_of_items = sizeof(keymap_keys) / sizeof(keymap_keys[0]);
 for (i = 0; i < number_of_items; i++) {
  if (keymap_keys[i].key > table->max_value)
   table->max_value = keymap_keys[i].key;
 }
 table->key_table = malloc(table->max_value * sizeof(ushort));
 printf("Filling lookup table with data (number of items = %i\n",
   table->max_value);
 for (i = 0; i <= table->max_value; i++) {
  if (i > 0 && i <= 83) {
   *(table->key_table + i) = i;
  } else {
   *(table->key_table + i) = 0;
  }
 }
 for (i = 0; i < number_of_items; i++) {
  printf("Replacing key %i with key %i\n", keymap_keys[i].key,
    keymap_keys[i].new_key);
  *(table->key_table + keymap_keys[i].key) = keymap_keys[i].new_key;
 }
 printf("Finished creating lookup table\n");
}

canonical_original_code:
void create_lookup_table(struct key_lookup_table *table) {
 int i; int number_of_items;
 printf("Creating lookup table\n");
 number_of_items = sizeof(keymap_keys) / sizeof(keymap_keys[0]);
 for (i = 0; i < number_of_items; i++) {
  if (keymap_keys[i].key > table->max_value)
   table->max_value = keymap_keys[i].key;
 }
 table->key_table = malloc(table->max_value * sizeof(ushort));
 printf("Filling lookup table with data (number of items = %i\n",
   table->max_value);
 for (i = 0; i <= table->max_value; i++) {
  if (i > 0 && i <= 83) {
   *(table->key_table + i) = i;
  } else {
   *(table->key_table + i) = 0;
  }
 }
 for (i = 0; i < number_of_items; i++) {
  printf("Replacing key %i with key %i\n", keymap_keys[i].key,
    keymap_keys[i].new_key);
  *(table->key_table + keymap_keys[i].key) = keymap_keys[i].new_key;
 }
 printf("Finished creating lookup table\n");
}

variable_types: struct key_lookup_table { unsigned short max_value; unsigned short *key_table; } *

variable_types: i int


decompiled_code:
int __fastcall show_usage(__int64 a1, const char **a2)
{
printf("Usage: %s [OPTION]\n", *a2);
puts("Starts a proxy service for the Mele F10 remote control\n");
puts("Arguments:");
puts("\t-f\tRun in foreground (don't run in background)");
return puts("\t-d\tShow debug information");
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, const char **a2)
{
printf("Usage: %s [OPTION]\n", *a2);
puts("Starts a proxy service for the Mele F10 remote control\n");
puts("Arguments:");
puts("\t-f\tRun in foreground (don't run in background)");
return puts("\t-d\tShow debug information");
}

original_code:
void show_usage(int argc, char **argv) {
 printf("Usage: %s [OPTION]\n", *argv);
 printf("Starts a proxy service for the Mele F10 remote control\n\n");
 printf("Arguments:\n");
 printf("\t-f\tRun in foreground (don't run in background)\n");
 printf("\t-d\tShow debug information\n");
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
int __fastcall destroy_uinput_device(int fd)
{
int result; // eax

if ( ioctl(fd, 0x5502uLL) < 0 )
result = -1;
else
result = close(fd);
return result;
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
int result; // eax

if ( ioctl(fd, 0x5502uLL) < 0 )
result = -1;
else
result = close(fd);
return result;
}


DisplayHelp

repo: Ethanjfobrien/LanguageProcessorsProject

decompiled_code:
__int64 DisplayHelp()
{
WriteOut(" ?, HELP Display this message\n");
WriteOut(" EXIT Exit program\n");
WriteOut(" LOGGING Toggle logging to file \"%s\"\n", "sim.log");
WriteOut(" RUN [<c>] Run <c> instructions, starting from the\n");
WriteOut(" current PC\n");
WriteOut(" TRACE [<c>] Trace <c> instructions, starting from the\n");
WriteOut(" current PC\n");
WriteOut(" RESET PC = 0, SP = -1, FP = 0\n");
WriteOut(" Initialise stack to zeros\n");
WriteOut(" DR Display Registers\n");
WriteOut(" DDM [<a> [<c>]] Display data (stack) memory from <a>\n");
WriteOut(" displaying <c> words\n");
WriteOut(" DCM [<a> [<c>]] Display code (instruction) memory\n");
WriteOut(" from <a> displaying <c> words\n");
WriteOut(" SPC <n> Set Program Counter to <n>\n");
WriteOut(" SSP <n> Set Stack Pointer to <n>\n");
WriteOut(" SFP <n> Set Frame Pointer to <n>\n");
WriteOut(" SDM <a> <v> Set Data memory at <a> to <v>\n\n");
WriteOut(" SBP <a> Set Breakpoint at code address <a>\n");
WriteOut(" DBP Display all Breakpoints\n");
WriteOut(" CBP Clear all Breakpoints\n\n");
return WriteOut(" Commands may be abbreviated to shortest unique prefix\n");
}

canonical_decompiled_code:
__int64 func0()
{
WriteOut(" ?, HELP Display this message\n");
WriteOut(" EXIT Exit program\n");
WriteOut(" LOGGING Toggle logging to file \"%s\"\n", "sim.log");
WriteOut(" RUN [<c>] Run <c> instructions, starting from the\n");
WriteOut(" current PC\n");
WriteOut(" TRACE [<c>] Trace <c> instructions, starting from the\n");
WriteOut(" current PC\n");
WriteOut(" RESET PC = 0, SP = -1, FP = 0\n");
WriteOut(" Initialise stack to zeros\n");
WriteOut(" DR Display Registers\n");
WriteOut(" DDM [<a> [<c>]] Display data (stack) memory from <a>\n");
WriteOut(" displaying <c> words\n");
WriteOut(" DCM [<a> [<c>]] Display code (instruction) memory\n");
WriteOut(" from <a> displaying <c> words\n");
WriteOut(" SPC <n> Set Program Counter to <n>\n");
WriteOut(" SSP <n> Set Stack Pointer to <n>\n");
WriteOut(" SFP <n> Set Frame Pointer to <n>\n");
WriteOut(" SDM <a> <v> Set Data memory at <a> to <v>\n\n");
WriteOut(" SBP <a> Set Breakpoint at code address <a>\n");
WriteOut(" DBP Display all Breakpoints\n");
WriteOut(" CBP Clear all Breakpoints\n\n");
return WriteOut(" Commands may be abbreviated to shortest unique prefix\n");
}

original_code:
static void DisplayHelp( void )
{
    WriteOut( " ?, HELP          Display this message\n" );
    WriteOut( " EXIT             Exit program\n" );
    WriteOut( " LOGGING          Toggle logging to file \"%s\"\n", "sim.log" );
    WriteOut( " RUN [<c>]        Run <c> instructions, starting from the\n" );
    WriteOut( "                  current PC\n" );
    WriteOut( " TRACE [<c>]      Trace <c> instructions, starting from the\n" );
    WriteOut( "                  current PC\n" );
    WriteOut( " RESET            PC = 0, SP = -1, FP = 0\n" );
    WriteOut( "                  Initialise stack to zeros\n" );
    WriteOut( " DR               Display Registers\n" );
    WriteOut( " DDM [<a> [<c>]]  Display data (stack) memory from <a>\n");
    WriteOut( "                  displaying <c> words\n" );
    WriteOut( " DCM [<a> [<c>]]  Display code (instruction) memory\n" );
    WriteOut( "                  from <a> displaying <c> words\n" );
    WriteOut( " SPC <n>          Set Program Counter to <n>\n" );
    WriteOut( " SSP <n>          Set Stack Pointer to <n>\n" );
    WriteOut( " SFP <n>          Set Frame Pointer to <n>\n" );
    WriteOut( " SDM <a> <v>      Set Data memory at <a> to <v>\n\n" );
    WriteOut( " SBP <a>          Set Breakpoint at code address <a>\n" );
    WriteOut( " DBP              Display all Breakpoints\n" );
    WriteOut( " CBP              Clear all Breakpoints\n\n" );
    WriteOut( " Commands may be abbreviated to shortest unique prefix\n");
}

canonical_original_code:
static void DisplayHelp()
{
    WriteOut( " ?, HELP          Display this message\n" );
    WriteOut( " EXIT             Exit program\n" );
    WriteOut( " LOGGING          Toggle logging to file \"%s\"\n", "sim.log" );
    WriteOut( " RUN [<c>]        Run <c> instructions, starting from the\n" );
    WriteOut( "                  current PC\n" );
    WriteOut( " TRACE [<c>]      Trace <c> instructions, starting from the\n" );
    WriteOut( "                  current PC\n" );
    WriteOut( " RESET            PC = 0, SP = -1, FP = 0\n" );
    WriteOut( "                  Initialise stack to zeros\n" );
    WriteOut( " DR               Display Registers\n" );
    WriteOut( " DDM [<a> [<c>]]  Display data (stack) memory from <a>\n");
    WriteOut( "                  displaying <c> words\n" );
    WriteOut( " DCM [<a> [<c>]]  Display code (instruction) memory\n" );
    WriteOut( "                  from <a> displaying <c> words\n" );
    WriteOut( " SPC <n>          Set Program Counter to <n>\n" );
    WriteOut( " SSP <n>          Set Stack Pointer to <n>\n" );
    WriteOut( " SFP <n>          Set Frame Pointer to <n>\n" );
    WriteOut( " SDM <a> <v>      Set Data memory at <a> to <v>\n\n" );
    WriteOut( " SBP <a>          Set Breakpoint at code address <a>\n" );
    WriteOut( " DBP              Display all Breakpoints\n" );
    WriteOut( " CBP              Clear all Breakpoints\n\n" );
    WriteOut( " Commands may be abbreviated to shortest unique prefix\n");
}


ParseKeyword

original_code:
static int ParseKeyword( int *value )
{
    int keywordcode, state, len, quit;
    char keybuffer[40], *kptr;
    if ( feof(stdin) ) { WriteOut("\n"); return 0; }
    keywordcode = -3;
    len = 0;
    state = 0; quit = 0;
    kptr = keybuffer;
    while ( !quit ) {
        switch( state ) {
            case 0:
                if ( *lptr == '\n' || *lptr == '\0' ) {
                    keywordcode = -1; quit = 1;
                }
                else if ( *lptr == '?' ) {
                    keywordcode = -5; quit = 1;
                }
                else if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short int) _ISspace) ) lptr++;
                else if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short int) _ISalpha) ) {
                    *kptr = (char)toupper( *lptr ); kptr++; lptr++;
                    len = 1; state = 1;
                }
                else if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short int) _ISdigit) ) {
                    *value = *lptr - '0'; lptr++;
                    state = 2;
                }
                else {
                    DisplayError( !1, "Illegal Character in input %c\n", *lptr );
                    keywordcode = -3; quit = 1;
                }
                break;
            case 1:
                if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short int) _ISalpha) ) {
                    if ( len < 40 ) {
                        *kptr = (char)toupper( *lptr ); kptr++;
                    }
                    lptr++; len++;
                }
                else {
                    *kptr = '\0'; keywordcode = -4; quit = 1;
                }
                break;
            case 2:
                if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short int) _ISdigit) ) {
                    if ( *value <= (32750/10) ) {
                        *value *= 10;
                        *value += ( *lptr - '0' );
                        lptr++;
                    }
                    else {
                        DisplayError( !1, "Integer value too large, max = %d\n",
                                      32750 );
                        keywordcode = -3; quit = 1;
                    }
                }
                else {
                    keywordcode = -2; quit = 1;
                }
                break;
            default:
                DisplayError( 1, "Command line scanner, unknown state %d\n",
                              state );
        }
    }
    if ( keywordcode == -4 ) {
        if ( *keybuffer == 'E' ) keywordcode = 0;
        else if ( *keybuffer == 'H' ) keywordcode = -5;
        else if ( *keybuffer == 'L' ) keywordcode = 14;
        else if ( *keybuffer == 'T' ) keywordcode = 5;
        else if ( *keybuffer == 'R' &&
                  *(keybuffer+1) == 'U' ) keywordcode = 4;
        else if ( *keybuffer == 'R' &&
                  *(keybuffer+1) == 'E' ) keywordcode = 13;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'R' ) keywordcode = 1;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'D' ) keywordcode = 2;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'C' ) keywordcode = 3;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'P' ) keywordcode = 6;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'F' ) keywordcode = 8;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'S' ) keywordcode = 7;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'D' ) keywordcode = 9;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'B' ) keywordcode = 10;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'B' ) keywordcode = 11;
        else if ( *keybuffer == 'C' &&
                  *(keybuffer+1) == 'B' ) keywordcode = 12;
        else {
            DisplayError( !1, "Unknown Command \"%s\"\n", keybuffer );
            keywordcode = -3;
        }
    }
    return keywordcode;
}

canonical_original_code:
static int ParseKeyword(int *value)
{
    int keywordcode; int state; int len; int quit;
    char keybuffer[40]; char *kptr;
    if ( feof(stdin) ) { WriteOut("\n"); return 0; }
    keywordcode = -3;
    len = 0;
    state = 0; quit = 0;
    kptr = keybuffer;
    while ( !quit ) {
        switch( state ) {
            case 0:
                if ( *lptr == '\n' || *lptr == '\0' ) {
                    keywordcode = -1; quit = 1;
                }
                else if ( *lptr == '?' ) {
                    keywordcode = -5; quit = 1;
                }
                else if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short) _ISspace) ) lptr++;
                else if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short) _ISalpha) ) {
                    *kptr = (char)toupper( *lptr ); kptr++; lptr++;
                    len = 1; state = 1;
                }
                else if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short) _ISdigit) ) {
                    *value = *lptr - '0'; lptr++;
                    state = 2;
                }
                else {
                    DisplayError( !1, "Illegal Character in input %c\n", *lptr );
                    keywordcode = -3; quit = 1;
                }
                break;
            case 1:
                if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short) _ISalpha) ) {
                    if ( len < 40 ) {
                        *kptr = (char)toupper( *lptr ); kptr++;
                    }
                    lptr++; len++;
                }
                else {
                    *kptr = '\0'; keywordcode = -4; quit = 1;
                }
                break;
            case 2:
                if ( ((*__ctype_b_loc ())[(int) ((*lptr))] & (unsigned short) _ISdigit) ) {
                    if ( *value <= (32750/10) ) {
                        *value *= 10;
                        *value += ( *lptr - '0' );
                        lptr++;
                    }
                    else {
                        DisplayError( !1, "Integer value too large, max = %d\n",
                                      32750 );
                        keywordcode = -3; quit = 1;
                    }
                }
                else {
                    keywordcode = -2; quit = 1;
                }
                break;
            default:
                DisplayError( 1, "Command line scanner, unknown state %d\n",
                              state );
        }
    }
    if ( keywordcode == -4 ) {
        if ( *keybuffer == 'E' ) keywordcode = 0;
        else if ( *keybuffer == 'H' ) keywordcode = -5;
        else if ( *keybuffer == 'L' ) keywordcode = 14;
        else if ( *keybuffer == 'T' ) keywordcode = 5;
        else if ( *keybuffer == 'R' &&
                  *(keybuffer+1) == 'U' ) keywordcode = 4;
        else if ( *keybuffer == 'R' &&
                  *(keybuffer+1) == 'E' ) keywordcode = 13;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'R' ) keywordcode = 1;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'D' ) keywordcode = 2;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'C' ) keywordcode = 3;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'P' ) keywordcode = 6;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'F' ) keywordcode = 8;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'S' ) keywordcode = 7;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'D' ) keywordcode = 9;
        else if ( *keybuffer == 'S' &&
                  *(keybuffer+1) == 'B' ) keywordcode = 10;
        else if ( *keybuffer == 'D' &&
                  *(keybuffer+1) == 'B' ) keywordcode = 11;
        else if ( *keybuffer == 'C' &&
                  *(keybuffer+1) == 'B' ) keywordcode = 12;
        else {
            DisplayError( !1, "Unknown Command \"%s\"\n", keybuffer );
            keywordcode = -3;
        }
    }
    return keywordcode;
}

variable_types:
value
int *

variable_types: keywordcode int

variable_types: state int

variable_types: len int

variable_types: quit int

variable_types:
keybuffer
char [40]

variable_types:
kptr
char *


ReadCommand

decompiled_code:
__int64 __fastcall ReadCommand(_DWORD *a1, _DWORD *a2)
{
unsigned int v2; // ebx
int v3; // eax
int v5; // eax
int v6; // eax
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 2 )
{
WriteOut("==> ");
lptr = ReadString();
v2 = ParseKeyword(v7);
switch ( v2 )
{
case 0xFFFFFFFB:
DisplayHelp();
continue;
case 0xFFFFFFFD:
case 0xFFFFFFFF:
continue;
case 0xFFFFFFFE:
DisplayError(0LL, "Integer value not allowed here\n");
continue;
case 0u:
case 1u:
case 0xBu:
case 0xCu:
case 0xDu:
case 0xEu:
return v2;
case 2u:
case 3u:
v5 = ParseKeyword(v7);
if ( v5 == -2 )
{
*a1 = v7[0];
v6 = ParseKeyword(v7);
if ( v6 == -2 )
{
*a2 = v7[0];
return v2;
}
if ( v6 == -1 )
{
*a2 = -1;
return v2;
}
}
else if ( v5 == -1 )
{
*a1 = -1;
*a2 = -1;
return v2;
}
goto LABEL_18;
case 4u:
case 5u:
v3 = ParseKeyword(v7);
if ( v3 != -2 )
{
if ( v3 == -1 )
{
*a1 = -1;
*a2 = -1;
return v2;
}
LABEL_18:
DisplayError(0LL, "Integer or end of line expected\n");
continue;
}
*a1 = v7[0];
*a2 = -1;
return v2;
case 6u:
case 7u:
case 8u:
case 0xAu:
if ( (unsigned int)ParseKeyword(v7) != -2 )
goto LABEL_11;
*a1 = v7[0];
*a2 = -1;
return v2;
case 9u:
if ( (unsigned int)ParseKeyword(v7) == -2 )
{
*a1 = v7[0];
if ( (unsigned int)ParseKeyword(v7) == -2 )
{
*a2 = v7[0];
return v2;
}
LABEL_11:
DisplayError(0LL, "Integer expected\n");
}
else
{
DisplayError(0LL, "Two integer values expected\n");
}
continue;
default:
DisplayError(1LL, "Error, unknown state in ReadCommand %d\n", v2);
continue;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, _DWORD *a2)
{
unsigned int v2; // ebx
int v3; // eax
int v5; // eax
int v6; // eax
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 2 )
{
WriteOut("==> ");
lptr = ReadString();
v2 = func1(v7);
switch ( v2 )
{
case 0xFFFFFFFB:
func0();
continue;
case 0xFFFFFFFD:
case 0xFFFFFFFF:
continue;
case 0xFFFFFFFE:
DisplayError(0LL, "Integer value not allowed here\n");
continue;
case 0u:
case 1u:
case 0xBu:
case 0xCu:
case 0xDu:
case 0xEu:
return v2;
case 2u:
case 3u:
v5 = func1(v7);
if ( v5 == -2 )
{
*a1 = v7[0];
v6 = func1(v7);
if ( v6 == -2 )
{
*a2 = v7[0];
return v2;
}
if ( v6 == -1 )
{
*a2 = -1;
return v2;
}
}
else if ( v5 == -1 )
{
*a1 = -1;
*a2 = -1;
return v2;
}
goto LABEL_18;
case 4u:
case 5u:
v3 = func1(v7);
if ( v3 != -2 )
{
if ( v3 == -1 )
{
*a1 = -1;
*a2 = -1;
return v2;
}
LABEL_18:
DisplayError(0LL, "Integer or end of line expected\n");
continue;
}
*a1 = v7[0];
*a2 = -1;
return v2;
case 6u:
case 7u:
case 8u:
case 0xAu:
if ( (unsigned int)func1(v7) != -2 )
goto LABEL_11;
*a1 = v7[0];
*a2 = -1;
return v2;
case 9u:
if ( (unsigned int)func1(v7) == -2 )
{
*a1 = v7[0];
if ( (unsigned int)func1(v7) == -2 )
{
*a2 = v7[0];
return v2;
}
LABEL_11:
DisplayError(0LL, "Integer expected\n");
}
else
{
DisplayError(0LL, "Two integer values expected\n");
}
continue;
default:
DisplayError(1LL, "Error, unknown state in ReadCommand %d\n", v2);
continue;
}
}
}

original_code:
int ReadCommand( int *p1, int *p2 )
{
    int quit, command, keyword, value;
    quit = 0;
    command = -3;
    while ( !quit ) {
        WriteOut( "==> " );
        lptr = ReadString();
        command = ParseKeyword( &value );
        switch ( command ) {
            case -1: break;
            case -3: break;
            case -5:
                DisplayHelp();
                break;
            case -2:
                DisplayError( !1, "Integer value not allowed here\n" );
                break;
            case 0:
            case 14:
            case 13:
            case 1:
     case 11:
     case 12:
                quit = 1;
                break;
            case 4:
            case 5:
                if ( -2 == ( keyword = ParseKeyword( &value ) ) ) {
                    *p1 = value; *p2 = -1; quit = 1;
                }
                else if ( keyword == -1 ) {
                    *p1 = -1; *p2 = -1; quit = 1;
                }
                else DisplayError( !1, "Integer or end of line expected\n" );
                break;
            case 6:
            case 7:
            case 8:
     case 10:
                if ( -2 == ParseKeyword( &value ) ) {
                    *p1 = value; *p2 = -1; quit = 1;
                }
                else DisplayError( !1, "Integer expected\n" );
                break;
            case 2:
            case 3:
                if ( -2 == ( keyword = ParseKeyword( &value ) ) ) {
                    *p1 = value;
                    if ( -2 == ( keyword = ParseKeyword( &value ) ) ) {
                        *p2 = value; quit = 1;
                    }
                    else if ( keyword == -1 ) {
                        *p2 = -1; quit = 1;
                    }
                    else DisplayError( !1, "Integer or end of line expected\n" );
                }
                else if ( keyword == -1 ) {
                    *p1 = -1; *p2 = -1; quit = -1;
                }
                else DisplayError( !1, "Integer or end of line expected\n" );
                break;
            case 9:
                if ( -2 == ParseKeyword( &value ) ) {
                    *p1 = value;
                    if ( -2 == ParseKeyword( &value ) ) {
                        *p2 = value; quit = 1;
                    }
                    else DisplayError( !1, "Integer expected\n" );
                }
                else DisplayError( !1, "Two integer values expected\n" );
                break;
            default:
                DisplayError( 1, "Error, unknown state in ReadCommand %d\n",
                              command );
                break;
        }
    }
    return command;
}

canonical_original_code:
int ReadCommand(int *p1, int *p2)
{
    int quit; int command; int keyword; int value;
    quit = 0;
    command = -3;
    while ( !quit ) {
        WriteOut( "==> " );
        lptr = ReadString();
        command = ParseKeyword( &value );
        switch ( command ) {
            case -1: break;
            case -3: break;
            case -5:
                DisplayHelp();
                break;
            case -2:
                DisplayError( !1, "Integer value not allowed here\n" );
                break;
            case 0:
            case 14:
            case 13:
            case 1:
     case 11:
     case 12:
                quit = 1;
                break;
            case 4:
            case 5:
                if ( -2 == ( keyword = ParseKeyword( &value ) ) ) {
                    *p1 = value; *p2 = -1; quit = 1;
                }
                else if ( keyword == -1 ) {
                    *p1 = -1; *p2 = -1; quit = 1;
                }
                else DisplayError( !1, "Integer or end of line expected\n" );
                break;
            case 6:
            case 7:
            case 8:
     case 10:
                if ( -2 == ParseKeyword( &value ) ) {
                    *p1 = value; *p2 = -1; quit = 1;
                }
                else DisplayError( !1, "Integer expected\n" );
                break;
            case 2:
            case 3:
                if ( -2 == ( keyword = ParseKeyword( &value ) ) ) {
                    *p1 = value;
                    if ( -2 == ( keyword = ParseKeyword( &value ) ) ) {
                        *p2 = value; quit = 1;
                    }
                    else if ( keyword == -1 ) {
                        *p2 = -1; quit = 1;
                    }
                    else DisplayError( !1, "Integer or end of line expected\n" );
                }
                else if ( keyword == -1 ) {
                    *p1 = -1; *p2 = -1; quit = -1;
                }
                else DisplayError( !1, "Integer or end of line expected\n" );
                break;
            case 9:
                if ( -2 == ParseKeyword( &value ) ) {
                    *p1 = value;
                    if ( -2 == ParseKeyword( &value ) ) {
                        *p2 = value; quit = 1;
                    }
                    else DisplayError( !1, "Integer expected\n" );
                }
                else DisplayError( !1, "Two integer values expected\n" );
                break;
            default:
                DisplayError( 1, "Error, unknown state in ReadCommand %d\n",
                              command );
                break;
        }
    }
    return command;
}

variable_types: command int

variable_types: keyword int

variable_types: value int


AddressError

decompiled_code:
__int64 __fastcall AddressError(const char *a1)
{
char v2[88]; // [rsp+0h] [rbp-58h] BYREF

Disassemble(&Pmem[2 * PC], v2);
DisplayError(
0LL,
"Error: %s, PC = %d, SP = %d, FP = %d\n",
a1,
(unsigned int)PC,
(unsigned int)dword_6B4,
(unsigned int)FP);
return DisplayError(0LL, " Instruction \"%s\"\n", v2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
char v2[88]; // [rsp+0h] [rbp-58h] BYREF

Disassemble(&Pmem[2 * PC], v2);
DisplayError(
0LL,
"Error: %s, PC = %d, SP = %d, FP = %d\n",
a1,
(unsigned int)PC,
(unsigned int)dword_6B4,
(unsigned int)FP);
return DisplayError(0LL, " Instruction \"%s\"\n", v2);
}

original_code:
static void AddressError( char *ErrorString )
{
    char buffer[80];
    Disassemble( Pmem+PC, buffer );
    DisplayError( !1, "Error: %s, PC = %d, SP = %d, FP = %d\n",
                  ErrorString, PC, SP, FP );
    DisplayError( !1, "       Instruction \"%s\"\n", buffer );
}

canonical_original_code:
static void AddressError(char *ErrorString)
{
    char buffer[80];
    Disassemble( Pmem+PC, buffer );
    DisplayError( !1, "Error: %s, PC = %d, SP = %d, FP = %d\n",
                  ErrorString, PC, SP, FP );
    DisplayError( !1, "       Instruction \"%s\"\n", buffer );
}

variable_types:
ErrorString
char *

variable_types:
buffer
char [80]


CodeAddr

decompiled_code:
__int64 __fastcall CodeAddr(unsigned int a1)
{
__int64 result; // rax
char v2[88]; // [rsp-58h] [rbp-58h] BYREF

result = a1;
if ( a1 > 0x400 )
{
sprintf(v2, "Illegal code address %d", a1);
AddressError(v2);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
__int64 result; // rax
char v2[88]; // [rsp-58h] [rbp-58h] BYREF

result = a1;
if ( a1 > 0x400 )
{
sprintf(v2, "Illegal code address %d", a1);
func0(v2);
exit(1);
}
return result;
}

original_code:
static int CodeAddr( int addr )
{
    char buffer[80];
    if ( addr < 0 || addr > 1024 ) {
        sprintf( buffer, "Illegal code address %d", addr );
        AddressError( buffer );
        exit( 1 );
    }
    return addr;
}

canonical_original_code:
static int CodeAddr(int addr)
{
    char buffer[80];
    if ( addr < 0 || addr > 1024 ) {
        sprintf( buffer, "Illegal code address %d", addr );
        AddressError( buffer );
        exit( 1 );
    }
    return addr;
}

variable_types: addr int


Decrement

decompiled_code:
__int64 __fastcall Decrement(int a1, int a2)
{
__int64 result; // rax
char v3[88]; // [rsp-58h] [rbp-58h] BYREF

result = (unsigned int)(a1 - a2);
if ( (int)result < -1 )
{
sprintf(v3, "Illegal stack pointer address %d", (unsigned int)result);
AddressError(v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
__int64 result; // rax
char v3[88]; // [rsp-58h] [rbp-58h] BYREF

result = (unsigned int)(a1 - a2);
if ( (int)result < -1 )
{
sprintf(v3, "Illegal stack pointer address %d", (unsigned int)result);
func0(v3);
exit(1);
}
return result;
}

original_code:
static int Decrement( int currentSP, int amount )
{
    int newSP;
    char buffer[80];
    newSP = currentSP - amount;
    if ( newSP < -1 ) {
        sprintf( buffer, "Illegal stack pointer address %d", newSP );
        AddressError( buffer );
        exit( 1 );
    }
    return newSP;
}

canonical_original_code:
static int Decrement(int currentSP, int amount)
{
    int newSP;
    char buffer[80];
    newSP = currentSP - amount;
    if ( newSP < -1 ) {
        sprintf( buffer, "Illegal stack pointer address %d", newSP );
        AddressError( buffer );
        exit( 1 );
    }
    return newSP;
}

variable_types: currentSP int

variable_types: amount int

variable_types: newSP int


Push

decompiled_code:
__int64 __fastcall Push(int a1)
{
int v1; // eax
__int64 result; // rax

v1 = dword_6B4;
if ( dword_6B4 > 1022 )
{
AddressError("Stack Overflow");
exit(1);
}
++dword_6B4;
result = v1 + 1;
Dmem[result] = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v1; // eax
__int64 result; // rax

v1 = dword_6B4;
if ( dword_6B4 > 1022 )
{
func0("Stack Overflow");
exit(1);
}
++dword_6B4;
result = v1 + 1;
Dmem[result] = a1;
return result;
}

original_code:
static void Push( int value )
{
    if ( SP >= 1024 -1 ) {
        AddressError( "Stack Overflow" );
        exit( 1 );
    }
    else {
        SP++;
        Dmem[SP] = value;
    }
}

canonical_original_code:
static void Push(int value)
{
    if ( SP >= 1024 -1 ) {
        AddressError( "Stack Overflow" );
        exit( 1 );
    }
    else {
        SP++;
        Dmem[SP] = value;
    }
}


BuildStackFrame

decompiled_code:
__int64 BuildStackFrame()
{
int v0; // ebx
__int64 result; // rax

v0 = dword_6B4;
result = Push(FP);
FP = v0;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // ebx
__int64 result; // rax

v0 = dword_6B4;
result = func3(FP);
FP = v0;
return result;
}

original_code:
static void BuildStackFrame( void )
{
    int temp, oldsp;
    temp = FP;
    oldsp = SP;
    Push( temp );
    FP = oldsp;
}

canonical_original_code:
static void BuildStackFrame()
{
    int temp; int oldsp;
    temp = FP;
    oldsp = SP;
    Push( temp );
    FP = oldsp;
}

variable_types: temp int

variable_types: oldsp int


DataAddr

decompiled_code:
__int64 __fastcall DataAddr(unsigned int a1)
{
__int64 result; // rax
char v2[88]; // [rsp-58h] [rbp-58h] BYREF

result = a1;
if ( a1 > 0x400 )
{
sprintf(v2, "Illegal data address %d", a1);
AddressError(v2);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
__int64 result; // rax
char v2[88]; // [rsp-58h] [rbp-58h] BYREF

result = a1;
if ( a1 > 0x400 )
{
sprintf(v2, "Illegal data address %d", a1);
func0(v2);
exit(1);
}
return result;
}

original_code:
static int DataAddr( int addr )
{
    char buffer[80];
    if ( addr < 0 || addr > 1024 ) {
        sprintf( buffer, "Illegal data address %d", addr );
        AddressError( buffer );
        exit( 1 );
    }
    return addr;
}

canonical_original_code:
static int DataAddr(int addr)
{
    char buffer[80];
    if ( addr < 0 || addr > 1024 ) {
        sprintf( buffer, "Illegal data address %d", addr );
        AddressError( buffer );
        exit( 1 );
    }
    return addr;
}


RemoveStackFrame

decompiled_code:
__int64 RemoveStackFrame()
{
int v0; // eax
__int64 result; // rax

dword_6B4 = DataAddr(FP) - 1;
v0 = DataAddr(FP + 1);
result = DataAddr(Dmem[v0]);
FP = result;
return result;
}

canonical_decompiled_code:
__int64 func6()
{
int v0; // eax
__int64 result; // rax

dword_6B4 = func5(FP) - 1;
v0 = func5(FP + 1);
result = func5(Dmem[v0]);
FP = result;
return result;
}

original_code:
static void RemoveStackFrame( void )
{
    int addr;
    addr = DataAddr( FP );
    SP = addr - 1;
    addr = DataAddr( FP + 1 );
    FP = DataAddr( Dmem[ addr ] );
}

canonical_original_code:
static void RemoveStackFrame()
{
    int addr;
    addr = DataAddr( FP );
    SP = addr - 1;
    addr = DataAddr( FP + 1 );
    FP = DataAddr( Dmem[ addr ] );
}