decompiled_code:
_BOOL8 __fastcall mac_fromStr(__int64 a1, const char *a2)
{
return sscanf(a2, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", a1, a1 + 1, a1 + 2, a1 + 3, a1 + 4, a1 + 5) == 6;
}

original_code:
int mac_fromStr(MAC_Address* mac, const char* str)
{
 int cnt = sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  &mac->raw[0],&mac->raw[1],&mac->raw[2],&mac->raw[3],&mac->raw[4],&mac->raw[5]);
 return cnt == sizeof(mac->raw);
}

canonical_original_code:
int mac_fromStr(union MAC_Address_s *mac, char *str)
{
 int cnt = sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  &mac->raw[0],&mac->raw[1],&mac->raw[2],&mac->raw[3],&mac->raw[4],&mac->raw[5]);
 return cnt == sizeof(mac->raw);
}

variable_types: union MAC_Address_s { unsigned char raw[6]; struct { unsigned char vendor[3]; unsigned char serial[3]; } fields; } *

variable_types: cnt int


repo: wkleiminger/pylon

decompiled_code:
__int64 __fastcall mac_toStr(unsigned __int8 *a1, char *a2, size_t a3)
{
snprintf(a2, a3, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", *a1, a1[1], a1[2], a1[3], a1[4], a1[5]);
return 1LL;
}

original_code:
int mac_toStr(const MAC_Address* mac, char* buffer, size_t size)
{
 snprintf(buffer, size, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  mac->raw[0],mac->raw[1],mac->raw[2],mac->raw[3],mac->raw[4],mac->raw[5]);
 return 1;
}

canonical_original_code:
int mac_toStr(union MAC_Address_s *mac, char *buffer, unsigned long size)
{
 snprintf(buffer, size, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  mac->raw[0],mac->raw[1],mac->raw[2],mac->raw[3],mac->raw[4],mac->raw[5]);
 return 1;
}

variable_types:
buffer
char *

variable_types:
size
unsigned long

return_type: int

user_defined_types: union MAC_Address_s { unsigned char raw[6]; struct { unsigned char vendor[3]; unsigned char serial[3]; } fields; }


decompiled_code:
_BOOL8 __fastcall mac_isValid(const char *a1)
{
char v2[14]; // [rsp+Ah] [rbp-Eh] BYREF

return mac_fromStr((__int64)v2, a1);
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(const char *a1)
{
char v2[14]; // [rsp+Ah] [rbp-Eh] BYREF

return func2((__int64)v2, a1);
}

original_code:
int mac_isValid(const char* str)
{
 MAC_Address mac;
 return mac_fromStr(&mac, str);
}

canonical_original_code:
int mac_isValid(char *str)
{
 union MAC_Address_s mac;
 return mac_fromStr(&mac, str);
}

variable_types:
str
char *

variable_types: union MAC_Address_s { unsigned char raw[6]; struct { unsigned char vendor[3]; unsigned char serial[3]; } fields; }


matches

decompiled_code:
_BOOL8 __fastcall matches(char *a1, _BYTE *a2)
{
char v2; // al

while ( 1 )
{
v2 = *a1;
if ( !*a1 )
return *a2 == 0;
if ( v2 != 35 && *a2 != v2 )
break;
++a1;
++a2;
}
return 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(char *a1, _BYTE *a2)
{
char v2; // al

while ( 1 )
{
v2 = *a1;
if ( !*a1 )
return *a2 == 0;
if ( v2 != 35 && *a2 != v2 )
break;
++a1;
++a2;
}
return 0LL;
}

original_code:
int matches(const char* fmt, const char* str)
{
 while (*fmt) {
  if (*fmt != '#' && *str != *fmt) return 0;
  ++fmt;
  ++str;
 }
 return !*str;
}

canonical_original_code:
int matches(char *fmt, char *str)
{
 while (*fmt) {
  if (*fmt != '#' && *str != *fmt) return 0;
  ++fmt;
  ++str;
 }
 return !*str;
}

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall ip_fromHostname(_DWORD *a1, const char *a2)
{
unsigned int v3; // er12
struct addrinfo *v4; // rdi
struct addrinfo *v5; // rax
__int64 result; // rax
const char *v7; // r8
int *v8; // rax
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
pai = 0LL;
req.ai_family = 2;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
v3 = getaddrinfo(a2, 0LL, &req, &pai);
if ( v3 )
{
if ( log_level < 0 )
{
result = 0LL;
}
else
{
if ( v3 == -11 )
{
v8 = _errno_location();
v7 = strerror(*v8);
}
else
{
v7 = gai_strerror(v3);
}
log_message("pylon/ip.c", "ip_fromHostname", 0LL, "getaddrinfo failed: %s\n", v7);
result = 0LL;
}
}
else
{
v4 = pai;
if ( pai )
{
v5 = pai;
while ( v5->ai_family != 2 )
{
v5 = v5->ai_next;
if ( !v5 )
goto LABEL_7;
}
v3 = 1;
*a1 = *(_DWORD *)&v5->ai_addr->sa_data[2];
}
LABEL_7:
freeaddrinfo(v4);
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, const char *a2)
{
unsigned int v3; // er12
struct addrinfo *v4; // rdi
struct addrinfo *v5; // rax
__int64 result; // rax
const char *v7; // r8
int *v8; // rax
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
pai = 0LL;
req.ai_family = 2;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
v3 = getaddrinfo(a2, 0LL, &req, &pai);
if ( v3 )
{
if ( log_level < 0 )
{
result = 0LL;
}
else
{
if ( v3 == -11 )
{
v8 = _errno_location();
v7 = strerror(*v8);
}
else
{
v7 = gai_strerror(v3);
}
log_message("pylon/ip.c", "ip_fromHostname", 0LL, "getaddrinfo failed: %s\n", v7);
result = 0LL;
}
}
else
{
v4 = pai;
if ( pai )
{
v5 = pai;
while ( v5->ai_family != 2 )
{
v5 = v5->ai_next;
if ( !v5 )
goto LABEL_7;
}
v3 = 1;
*a1 = *(_DWORD *)&v5->ai_addr->sa_data[2];
}
LABEL_7:
freeaddrinfo(v4);
result = v3;
}
return result;
}

original_code:
int ip_fromHostname(IP_Address* ip, const char* host)
{
 struct addrinfo hints = {0};
 hints.ai_family = 2;
 struct addrinfo* ai = ((void *)0);
 int error = getaddrinfo(host, ((void *)0), &hints, &ai);
 if (error) {
  if (0 <= log_level) log_message("pylon/ip.c", __func__, 0, "getaddrinfo failed: %s\n", error != -11 ? gai_strerror(error) : strerror((*__errno_location ())));
  return 0;
 }
 int success = 0;
 for (struct addrinfo* it = ai; it; it = it->ai_next) {
  if (it->ai_family == 2) {
   *ip = ((struct sockaddr_in*)it->ai_addr)->sin_addr;
   success = 1;
   break;
  }
 }
 freeaddrinfo(ai);
 return success;
}

canonical_original_code:
int ip_fromHostname(struct in_addr *ip, char *host)
{
 struct addrinfo hints = {0};
 hints.ai_family = 2;
 struct addrinfo *ai = ((void *)0);
 int error = getaddrinfo(host, ((void *)0), &hints, &ai);
 if (error) {
  if (0 <= log_level) log_message("pylon/ip.c", __func__, 0, "getaddrinfo failed: %s\n", error != -11 ? gai_strerror(error) : strerror((*__errno_location ())));
  return 0;
 }
 int success = 0;
 for (struct addrinfo *it = ai; it; it = it->ai_next) {
  if (it->ai_family == 2) {
   *ip = ((struct sockaddr_in *)it->ai_addr)->sin_addr;
   success = 1;
   break;
  }
 }
 freeaddrinfo(ai);
 return success;
}

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types:
host
char *

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

variable_types: error int

variable_types: success int

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

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


decompiled_code:
__int64 __fastcall fluksometer_init(char *a1, __int64 a2)
{
__int64 result; // rax

m_callback = a2;
if ( !a1 )
a1 = "/var/run/spid/delta/out";
result = 1LL;
m_fifo = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, __int64 a2)
{
__int64 result; // rax

m_callback = a2;
if ( !a1 )
a1 = "/var/run/spid/delta/out";
result = 1LL;
m_fifo = a1;
return result;
}

original_code:
int fluksometer_init(const char* fifo, fluksometer_cb callback)
{
 m_fifo = fifo ? fifo : "/var/run/spid/delta/out";
 m_callback = callback;
 return 1;
}

canonical_original_code:
int fluksometer_init(char *fifo, void (*callback)(struct SmartMeter_Data_s *m))
{
 m_fifo = fifo ? fifo : "/var/run/spid/delta/out";
 m_callback = callback;
 return 1;
}

variable_types:
fifo
char *

variable_types: void (*)(struct SmartMeter_Data_s { double val[-1]; } *m)


decompiled_code:
_BOOL8 fluksometer_start()
{
m_handle = meter_start(0xFFFFFFFFLL, performMeasurement);
return m_handle != 0;
}

canonical_decompiled_code:
_BOOL8 func3()
{
m_handle = meter_start(0xFFFFFFFFLL, performMeasurement);
return m_handle != 0;
}

original_code:
int fluksometer_start(void)
{
 m_handle = meter_start(-1, performMeasurement);
 return m_handle != ((void *)0);
}

canonical_original_code:
int fluksometer_start()
{
 m_handle = meter_start(-1, performMeasurement);
 return m_handle != ((void *)0);
}


decompiled_code:
__int64 fluksometer_stop()
{
return meter_stop(m_handle);
}

canonical_decompiled_code:
__int64 func4()
{
return meter_stop(m_handle);
}

original_code:
int fluksometer_stop(void)
{
 return meter_stop(m_handle);
}

canonical_original_code:
int fluksometer_stop()
{
 return meter_stop(m_handle);
}


decompiled_code:
__int64 fluksometer_join()
{
return meter_join(m_handle);
}

canonical_decompiled_code:
__int64 func5()
{
return meter_join(m_handle);
}

original_code:
int fluksometer_join(void)
{
 return meter_join(m_handle);
}

canonical_original_code:
int fluksometer_join()
{
 return meter_join(m_handle);
}


decompiled_code:
_QWORD *strbuilder_create()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
return result;
}

original_code:
StringBuilder* strbuilder_create(void)
{
 StringBuilder* sb = malloc(sizeof(StringBuilder));
 sb->str = ((void *)0);
 sb->len = 0;
 sb->capacity = 0;
 return sb;
}

canonical_original_code:
struct StringBuilder_s *strbuilder_create()
{
 struct StringBuilder_s *sb = malloc(sizeof(StringBuilder));
 sb->str = ((void *)0);
 sb->len = 0;
 sb->capacity = 0;
 return sb;
}

variable_types: struct StringBuilder_s { char *str; unsigned long len; unsigned long capacity; } *


decompiled_code:
void __fastcall strbuilder_free(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

original_code:
void strbuilder_free(StringBuilder* sb)
{
 if (sb) {
  free(sb->str);
  free(sb);
 }
}

canonical_original_code:
void strbuilder_free(struct StringBuilder_s *sb)
{
 if (sb) {
  free(sb->str);
  free(sb);
 }
}

return_type: void

user_defined_types: struct StringBuilder_s { char *str; unsigned long len; unsigned long capacity; }


decompiled_code:
int strbuilder_printf(__int64 a1, const char *a2, ...)
{
int result; // eax
int v4; // ebx
char *v5; // rdi
__int64 v6; // rdi
size_t v7; // rbx
unsigned __int64 v8; // rax
void *v9; // rax
int *v10; // rax
char *v11; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

result = strlen(a2);
while ( 2 )
{
v6 = *(_QWORD *)(a1 + 8);
v7 = *(_QWORD *)(a1 + 16);
v8 = result + v6 + 1;
if ( v7 >= v8 )
goto LABEL_2;
while ( v7 )
{
LABEL_7:
v7 *= 2LL;
if ( v8 <= v7 )
goto LABEL_8;
}
if ( v8 > 0x20 )
{
v7 = 64LL;
if ( v8 <= 0x40 )
goto LABEL_8;
goto LABEL_7;
}
v7 = 32LL;
LABEL_8:
v9 = realloc(*(void **)a1, v7);
if ( v9 )
{
*(_QWORD *)a1 = v9;
v6 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 16) = v7;
LABEL_2:
v4 = v7 - v6;
v5 = (char *)(*(_QWORD *)a1 + v6);
va_start(arg, a2);
result = vsnprintf(v5, v4, a2, arg);
if ( v4 > result )
{
*(_QWORD *)(a1 + 8) += result;
return result;
}
continue;
}
break;
}
if ( log_level >= 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_message("pylon/strbuilder.c", "strbuilder_printf", 0LL, "Failed to grow buffer: %s\n", v11);
}
return -1;
}

canonical_decompiled_code:
int func2(__int64 a1, const char *a2, ...)
{
int result; // eax
int v4; // ebx
char *v5; // rdi
__int64 v6; // rdi
size_t v7; // rbx
unsigned __int64 v8; // rax
void *v9; // rax
int *v10; // rax
char *v11; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

result = strlen(a2);
while ( 2 )
{
v6 = *(_QWORD *)(a1 + 8);
v7 = *(_QWORD *)(a1 + 16);
v8 = result + v6 + 1;
if ( v7 >= v8 )
goto LABEL_2;
while ( v7 )
{
LABEL_7:
v7 *= 2LL;
if ( v8 <= v7 )
goto LABEL_8;
}
if ( v8 > 0x20 )
{
v7 = 64LL;
if ( v8 <= 0x40 )
goto LABEL_8;
goto LABEL_7;
}
v7 = 32LL;
LABEL_8:
v9 = realloc(*(void **)a1, v7);
if ( v9 )
{
*(_QWORD *)a1 = v9;
v6 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 16) = v7;
LABEL_2:
v4 = v7 - v6;
v5 = (char *)(*(_QWORD *)a1 + v6);
va_start(arg, a2);
result = vsnprintf(v5, v4, a2, arg);
if ( v4 > result )
{
*(_QWORD *)(a1 + 8) += result;
return result;
}
continue;
}
break;
}
if ( log_level >= 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_message("pylon/strbuilder.c", "strbuilder_printf", 0LL, "Failed to grow buffer: %s\n", v11);
}
return -1;
}

original_code:
int strbuilder_printf(StringBuilder* sb, const char* format, ...)
{
 int writeSize = strlen(format);
 int done = 0;
 while (!done) {
  size_t minCapacity = sb->len + writeSize + 1;
  if (sb->capacity < minCapacity) {
   size_t newCapacity = sb->capacity;
   while (newCapacity < minCapacity) {
    newCapacity = newCapacity ? newCapacity*2 : 32;
   }
   char* buf = realloc(sb->str, newCapacity);
   if (!buf) {
    if (0 <= log_level) log_message("pylon/strbuilder.c", __func__, 0, "Failed to grow buffer: %s\n", strerror((*__errno_location ())));
    return -1;
   }
   sb->str = buf;
   sb->capacity = newCapacity;
  }
  int size = sb->capacity - sb->len;
  va_list args;
  __builtin_va_start(args,format);
  writeSize = vsnprintf(sb->str + sb->len, size, format, args);
  __builtin_va_end(args);
  done = writeSize < size;
 }
 sb->len += writeSize;
 return writeSize;
}

canonical_original_code:
int strbuilder_printf(struct StringBuilder_s *sb, char *format, ...)
{
 int writeSize = strlen(format);
 int done = 0;
 while (!done) {
  unsigned long minCapacity = sb->len + writeSize + 1;
  if (sb->capacity < minCapacity) {
   unsigned long newCapacity = sb->capacity;
   while (newCapacity < minCapacity) {
    newCapacity = newCapacity ? newCapacity*2 : 32;
   }
   char *buf = realloc(sb->str, newCapacity);
   if (!buf) {
    if (0 <= log_level) log_message("pylon/strbuilder.c", __func__, 0, "Failed to grow buffer: %s\n", strerror((*__errno_location ())));
    return -1;
   }
   sb->str = buf;
   sb->capacity = newCapacity;
  }
  int size = sb->capacity - sb->len;
  __builtin_va_list args;
  __builtin_va_start(args,format);
  writeSize = vsnprintf(sb->str + sb->len, size, format, args);
  __builtin_va_end(args);
  done = writeSize < size;
 }
 sb->len += writeSize;
 return writeSize;
}

variable_types:
format
char *

variable_types: writeSize int

variable_types: done int

variable_types:
minCapacity
unsigned long

variable_types:
newCapacity
unsigned long

variable_types:
buf
char *

variable_types: size int


decompiled_code:
void *__fastcall strbuilder_copy(__int64 a1)
{
const void *v1; // rbp
size_t v2; // r13
void *v3; // rax
void *v4; // r12
int *v6; // rax
char *v7; // rax

v1 = *(const void **)a1;
if ( *(_QWORD *)a1 )
{
v2 = *(_QWORD *)(a1 + 8) + 1LL;
v3 = malloc(v2);
v4 = v3;
if ( v3 )
{
memcpy(v3, v1, v2);
return v4;
}
if ( log_level >= 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
log_message("pylon/strbuilder.c", "strbuilder_copy", 0LL, "malloc failed: %s\n", v7);
return v4;
}
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1)
{
const void *v1; // rbp
size_t v2; // r13
void *v3; // rax
void *v4; // r12
int *v6; // rax
char *v7; // rax

v1 = *(const void **)a1;
if ( *(_QWORD *)a1 )
{
v2 = *(_QWORD *)(a1 + 8) + 1LL;
v3 = malloc(v2);
v4 = v3;
if ( v3 )
{
memcpy(v3, v1, v2);
return v4;
}
if ( log_level >= 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
log_message("pylon/strbuilder.c", "strbuilder_copy", 0LL, "malloc failed: %s\n", v7);
return v4;
}
}
return 0LL;
}

original_code:
char* strbuilder_copy(StringBuilder* sb)
{
 if (!sb->str) {
  return ((void *)0);
 }
 char* str = malloc(sb->len+1);
 if (!str) {
  if (0 <= log_level) log_message("pylon/strbuilder.c", __func__, 0, "malloc failed: %s\n", strerror((*__errno_location ())));
  return ((void *)0);
 }
 memcpy(str, sb->str, sb->len+1);
 return str;
}

canonical_original_code:
char *strbuilder_copy(struct StringBuilder_s *sb)
{
 if (!sb->str) {
  return ((void *)0);
 }
 char *str = malloc(sb->len+1);
 if (!str) {
  if (0 <= log_level) log_message("pylon/strbuilder.c", __func__, 0, "malloc failed: %s\n", strerror((*__errno_location ())));
  return ((void *)0);
 }
 memcpy(str, sb->str, sb->len+1);
 return str;
}

variable_types:
str
char *

return_type: char *


decompiled_code:
__int64 __fastcall strbuilder_str(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
char* strbuilder_str(StringBuilder* sb)
{
 return sb->str;
}

canonical_original_code:
char *strbuilder_str(struct StringBuilder_s *sb)
{
 return sb->str;
}


decompiled_code:
__int64 __fastcall strbuilder_length(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
size_t strbuilder_length(StringBuilder* sb)
{
 return sb->len;
}

canonical_original_code:
unsigned long strbuilder_length(struct StringBuilder_s *sb)
{
 return sb->len;
}

return_type: unsigned long


decompiled_code:
void __fastcall strbuilder_reset(__int64 a1)
{
*(_QWORD *)(a1 + 8) = 0LL;
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
*(_QWORD *)(a1 + 8) = 0LL;
}

original_code:
void strbuilder_reset(StringBuilder* sb)
{
 sb->len = 0;
}

canonical_original_code:
void strbuilder_reset(struct StringBuilder_s *sb)
{
 sb->len = 0;
}


decompiled_code:
void __fastcall strbuilder_pack(__int64 a1)
{
__int64 v1; // rsi
void *v3; // rdi
void *v4; // rax
int *v5; // rax
char *v6; // rax

v1 = *(_QWORD *)(a1 + 8);
v3 = *(void **)a1;
if ( v1 )
{
v4 = realloc(v3, v1 + 1);
if ( v4 )
{
*(_QWORD *)a1 = v4;
*(_QWORD *)(a1 + 16) = *(_QWORD *)(a1 + 8) + 1LL;
}
else if ( log_level > 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
log_message("pylon/strbuilder.c", "strbuilder_pack", 1LL, "Failed to trim buffer: %s\n", v6);
}
}
else
{
free(v3);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
__int64 v1; // rsi
void *v3; // rdi
void *v4; // rax
int *v5; // rax
char *v6; // rax

v1 = *(_QWORD *)(a1 + 8);
v3 = *(void **)a1;
if ( v1 )
{
v4 = realloc(v3, v1 + 1);
if ( v4 )
{
*(_QWORD *)a1 = v4;
*(_QWORD *)(a1 + 16) = *(_QWORD *)(a1 + 8) + 1LL;
}
else if ( log_level > 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
log_message("pylon/strbuilder.c", "strbuilder_pack", 1LL, "Failed to trim buffer: %s\n", v6);
}
}
else
{
free(v3);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
}
}

original_code:
void strbuilder_pack(StringBuilder* sb)
{
 if (sb->len == 0) {
  free(sb->str);
  sb->str = ((void *)0);
  sb->capacity = 0;
  return;
 }
 char* buf = realloc(sb->str, sb->len+1);
 if (!buf) {
  if (1 <= log_level) log_message("pylon/strbuilder.c", __func__, 1, "Failed to trim buffer: %s\n", strerror((*__errno_location ())));
  return;
 }
 sb->str = buf;
 sb->capacity = sb->len+1;
}

canonical_original_code:
void strbuilder_pack(struct StringBuilder_s *sb)
{
 if (sb->len == 0) {
  free(sb->str);
  sb->str = ((void *)0);
  sb->capacity = 0;
  return;
 }
 char *buf = realloc(sb->str, sb->len+1);
 if (!buf) {
  if (1 <= log_level) log_message("pylon/strbuilder.c", __func__, 1, "Failed to trim buffer: %s\n", strerror((*__errno_location ())));
  return;
 }
 sb->str = buf;
 sb->capacity = sb->len+1;
}


decompiled_code:
_BOOL8 __fastcall less_than(const void *a1, const void *a2)
{
return *(double *)a2 > *(double *)a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const void *a1, const void *a2)
{
return *(double *)a2 > *(double *)a1;
}

original_code:
int less_than(const void* a, const void* b)
{
 return *(double*)a < *(double*)b;
}

canonical_original_code:
int less_than(void *a, void *b)
{
 return *(double *)a < *(double *)b;
}

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
int log_message(const char *a1, const char *a2, unsigned int a3, const char *a4, ...)
{
int result; // eax
unsigned int v7; // eax
int v8; // er9
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

result = pthread_mutex_lock(&m_lock);
if ( result )
{
if ( log_level >= 0 )
{
v7 = (unsigned int)strerror(result);
result = log_message(
(unsigned int)"pylon/common.c",
(unsigned int)"log_message",
0,
(unsigned int)"Failed to lock mutex: %s\n",
v7,
v8);
}
}
else
{
fprintf(stderr, "[%s:%s]\t(%d) ", a1, a2, a3);
va_start(arg, a4);
vfprintf(stderr, a4, arg);
result = pthread_mutex_unlock(&m_lock);
}
return result;
}

canonical_decompiled_code:
int func1(const char *a1, const char *a2, unsigned int a3, const char *a4, ...)
{
int result; // eax
unsigned int v7; // eax
int v8; // er9
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

result = pthread_mutex_lock(&m_lock);
if ( result )
{
if ( log_level >= 0 )
{
v7 = (unsigned int)strerror(result);
result = func1(
(unsigned int)"pylon/common.c",
(unsigned int)"log_message",
0,
(unsigned int)"Failed to lock mutex: %s\n",
v7,
v8);
}
}
else
{
fprintf(stderr, "[%s:%s]\t(%d) ", a1, a2, a3);
va_start(arg, a4);
vfprintf(stderr, a4, arg);
result = pthread_mutex_unlock(&m_lock);
}
return result;
}

original_code:
void log_message(const char* file, const char* func, int level, const char *format, ...)
{
 int error = pthread_mutex_lock(&m_lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/common.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return;
 }
 fprintf(stderr, "[%s:%s]\t(%d) ", file, func, level);
  va_list args;
  __builtin_va_start(args,format);
  vfprintf(stderr, format, args);
  __builtin_va_end(args);
  pthread_mutex_unlock(&m_lock);
}

canonical_original_code:
void log_message(char *file, char *func, int level, char *format, ...)
{
 int error = pthread_mutex_lock(&m_lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/common.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return;
 }
 fprintf(stderr, "[%s:%s]\t(%d) ", file, func, level);
  __builtin_va_list args;
  __builtin_va_start(args,format);
  vfprintf(stderr, format, args);
  __builtin_va_end(args);
  pthread_mutex_unlock(&m_lock);
}

variable_types:
file
char *

variable_types:
func
char *

variable_types: level int


mean

decompiled_code:
double __fastcall mean(double *a1, __int64 a2)
{
double result; // xmm0_8
double *v3; // rax

result = 0.0;
if ( a2 )
{
v3 = &a1[a2];
do
result = result + *a1++;
while ( a1 != v3 );
result = result / (double)(int)a2;
}
return result;
}

canonical_decompiled_code:
double __fastcall func2(double *a1, __int64 a2)
{
double result; // xmm0_8
double *v3; // rax

result = 0.0;
if ( a2 )
{
v3 = &a1[a2];
do
result = result + *a1++;
while ( a1 != v3 );
result = result / (double)(int)a2;
}
return result;
}

original_code:
double mean(const double values[], size_t count)
{
 if (count == 0) {
  return 0.0;
 }
 double sum = 0.0;
 for (int i = 0; i < count; i++) {
  sum += values[i];
 }
 return sum / count;
}

canonical_original_code:
double mean(double values[], unsigned long count)
{
 if (count == 0) {
  return 0.0;
 }
 double sum = 0.0;
 for (int i = 0; i < count; i++) {
  sum += values[i];
 }
 return sum / count;
}

variable_types:
values
double []

variable_types:
count
unsigned long

variable_types: sum double

variable_types: i int

return_type: double


median

decompiled_code:
void __fastcall median(void *src, size_t nmemb)
{
void *v2; // rsp
_BYTE v3[24]; // [rsp-18h] [rbp-18h] BYREF

if ( nmemb )
{
v2 = alloca(8 * nmemb);
memcpy(v3, src, 8 * nmemb);
qsort(v3, nmemb, 8uLL, (__compar_fn_t)less_than);
}
}

canonical_decompiled_code:
void __fastcall func3(void *src, size_t nmemb)
{
void *v2; // rsp
_BYTE v3[24]; // [rsp-18h] [rbp-18h] BYREF

if ( nmemb )
{
v2 = alloca(8 * nmemb);
memcpy(v3, src, 8 * nmemb);
qsort(v3, nmemb, 8uLL, (__compar_fn_t)less_than);
}
}

original_code:
double median(const double values[], size_t count)
{
 if (count == 0) {
  return 0.0;
 }
 double tmp[count];
 memcpy(tmp, values, sizeof(tmp));
 qsort(tmp, count, sizeof(double), less_than);
 return tmp[count/2];
}

canonical_original_code:
double median(double values[], unsigned long count)
{
 if (count == 0) {
  return 0.0;
 }
 double tmp[-1];
 memcpy(tmp, values, sizeof(tmp));
 qsort(tmp, count, sizeof(double), less_than);
 return tmp[count/2];
}

variable_types:
tmp
double [-1]


stricmp

decompiled_code:
__int64 __fastcall stricmp(__int64 a1, __int64 a2)
{
const __int32_t **v3; // rax
__int64 v4; // rdx
const __int32_t *v5; // rcx
__int64 v6; // rdi
__int64 result; // rax

v3 = _ctype_toupper_loc();
v4 = 0LL;
v5 = *v3;
do
{
v6 = *(char *)(a1 + v4);
result = (unsigned int)(v5[v6] - v5[*(char *)(a2 + v4)]);
if ( (_DWORD)result )
break;
++v4;
}
while ( (_BYTE)v6 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
const __int32_t **v3; // rax
__int64 v4; // rdx
const __int32_t *v5; // rcx
__int64 v6; // rdi
__int64 result; // rax

v3 = _ctype_toupper_loc();
v4 = 0LL;
v5 = *v3;
do
{
v6 = *(char *)(a1 + v4);
result = (unsigned int)(v5[v6] - v5[*(char *)(a2 + v4)]);
if ( (_DWORD)result )
break;
++v4;
}
while ( (_BYTE)v6 );
return result;
}

original_code:
int stricmp(const char* s1, const char* s2)
{
 int diff;
 while (!(diff = toupper(*s1) - toupper(*s2)) && *s1) {
  ++s1;
  ++s2;
 }
 return diff;
}

canonical_original_code:
int stricmp(char *s1, char *s2)
{
 int diff;
 while (!(diff = toupper(*s1) - toupper(*s2)) && *s1) {
  ++s1;
  ++s2;
 }
 return diff;
}

variable_types: diff int


decompiled_code:
int __fastcall args_printUsage(_QWORD *a1, const char *a2)
{
_QWORD *v2; // rbx
int v3; // ebp
int v4; // er12
const char *v5; // rsi

v2 = a1;
printf("Usage: %s", a2);
if ( *a1 )
{
while ( 1 )
{
v3 = *((_DWORD *)v2 + 6);
if ( (v3 & 0x80u) != 0 )
break;
if ( v3 == 1 )
{
putchar(32);
LABEL_4:
v4 = 1;
putchar(91);
v5 = (const char *)v2[1];
if ( !v5 )
goto LABEL_14;
LABEL_5:
printf("%s", v5);
if ( !v2[1] )
goto LABEL_14;
if ( v3 == 1 )
goto LABEL_9;
putchar(32);
LABEL_8:
printf("%s", (const char *)*v2);
LABEL_9:
if ( !v4 )
goto LABEL_10;
LABEL_16:
v2 += 5;
putchar(93);
if ( !*v2 )
return putchar(10);
}
else
{
LOBYTE(v3) = v3 & 0x7F;
v4 = 0;
putchar(32);
v5 = (const char *)v2[1];
if ( v5 )
goto LABEL_5;
LABEL_14:
if ( v3 != 1 )
goto LABEL_8;
if ( v4 )
goto LABEL_16;
LABEL_10:
v2 += 5;
if ( !*v2 )
return putchar(10);
}
}
LOBYTE(v3) = v3 & 0x7F;
putchar(32);
goto LABEL_4;
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(_QWORD *a1, const char *a2)
{
_QWORD *v2; // rbx
int v3; // ebp
int v4; // er12
const char *v5; // rsi

v2 = a1;
printf("Usage: %s", a2);
if ( *a1 )
{
while ( 1 )
{
v3 = *((_DWORD *)v2 + 6);
if ( (v3 & 0x80u) != 0 )
break;
if ( v3 == 1 )
{
putchar(32);
LABEL_4:
v4 = 1;
putchar(91);
v5 = (const char *)v2[1];
if ( !v5 )
goto LABEL_14;
LABEL_5:
printf("%s", v5);
if ( !v2[1] )
goto LABEL_14;
if ( v3 == 1 )
goto LABEL_9;
putchar(32);
LABEL_8:
printf("%s", (const char *)*v2);
LABEL_9:
if ( !v4 )
goto LABEL_10;
LABEL_16:
v2 += 5;
putchar(93);
if ( !*v2 )
return putchar(10);
}
else
{
LOBYTE(v3) = v3 & 0x7F;
v4 = 0;
putchar(32);
v5 = (const char *)v2[1];
if ( v5 )
goto LABEL_5;
LABEL_14:
if ( v3 != 1 )
goto LABEL_8;
if ( v4 )
goto LABEL_16;
LABEL_10:
v2 += 5;
if ( !*v2 )
return putchar(10);
}
}
LOBYTE(v3) = v3 & 0x7F;
putchar(32);
goto LABEL_4;
}
return putchar(10);
}

original_code:
void args_printUsage(const Argument args[], const char* cmd)
{
 printf("Usage: %s", cmd);
 for (const Argument* it = args; it->name; it++) {
  int brackets = (it->type & 0x80) || (it->type == ARG_FLAG);
  int flag = (it->type &~ 0x80) == ARG_FLAG;
  printf(" ");
  if (brackets) printf("[");
  if (it->prefix) printf("%s", it->prefix);
  if (it->prefix && !flag) printf(" ");
  if (!flag) printf("%s", it->name);
  if (brackets) printf("]");
 }
 printf("\n");
}

canonical_original_code:
void args_printUsage(struct Argument_s args[], char *cmd)
{
 printf("Usage: %s", cmd);
 for (struct Argument_s *it = args; it->name; it++) {
  int brackets = (it->type & 0x80) || (it->type == ARG_FLAG);
  int flag = (it->type &~ 0x80) == ARG_FLAG;
  printf(" ");
  if (brackets) printf("[");
  if (it->prefix) printf("%s", it->prefix);
  if (it->prefix && !flag) printf(" ");
  if (!flag) printf("%s", it->name);
  if (brackets) printf("]");
 }
 printf("\n");
}

variable_types: struct Argument_s { char *name; char *prefix; char *value; enum ArgType { ARG_STRING=0, ARG_FLAG=1, ARG_INT=2, ARG_FLOAT=3 } type; char *description; } []

variable_types:
cmd
char *

variable_types: struct Argument_s { char *name; char *prefix; char *value; enum ArgType { ARG_STRING=0, ARG_FLAG=1, ARG_INT=2, ARG_FLOAT=3 } type; char *description; } *

variable_types: brackets int

variable_types: flag int

user_defined_types: struct Argument_s { char *name; char *prefix; char *value; enum ArgType type; char *description; }


decompiled_code:
void __fastcall args_printInfo(__int64 *a1)
{
__int64 v1; // r14
__int64 *v2; // rbx
__int64 *v3; // r13
unsigned int v4; // er12
const char *v5; // rdi
unsigned int v6; // ebp
int v7; // eax
const char *v8; // rdi
int v9; // eax
void *v10; // r8
const char *v11; // rsi

v1 = *a1;
if ( *a1 )
{
v2 = a1;
v3 = a1;
v4 = 0;
v5 = (const char *)*a1;
v6 = 0;
do
{
v7 = strlen(v5);
v8 = (const char *)v3[2];
if ( (int)v6 < v7 )
v6 = v7;
v9 = 0;
if ( v8 )
v9 = strlen(v8);
if ( (int)v4 < v9 )
v4 = v9;
v5 = (const char *)v3[5];
v3 += 5;
}
while ( v5 );
do
{
v10 = (void *)v2[2];
if ( !v10 )
v10 = &unk_4E6;
printf("%-*s : %-*s", v6, v1, v4, v10);
v11 = (const char *)v2[4];
if ( v11 )
printf(" -- %s", v11);
v2 += 5;
putchar(10);
v1 = *v2;
}
while ( *v2 );
}
}

original_code:
void args_printInfo(const Argument args[])
{
 int nw = 0;
 int vw = 0;
 for (const Argument* it = args; it->name; it++) {
  int len = strlen(it->name);
  if (nw < len) nw = len;
  len = it->value ? strlen(it->value) : 0;
  if (vw < len) vw = len;
 }
 for (const Argument* it = args; it->name; it++) {
  printf("%-*s : %-*s", nw, it->name, vw, it->value ? it->value : "");
  if (it->description) {
   printf(" -- %s", it->description);
  }
  printf("\n");
 }
}

canonical_original_code:
void args_printInfo(struct Argument_s args[])
{
 int nw = 0;
 int vw = 0;
 for (struct Argument_s *it = args; it->name; it++) {
  int len = strlen(it->name);
  if (nw < len) nw = len;
  len = it->value ? strlen(it->value) : 0;
  if (vw < len) vw = len;
 }
 for (struct Argument_s *it = args; it->name; it++) {
  printf("%-*s : %-*s", nw, it->name, vw, it->value ? it->value : "");
  if (it->description) {
   printf(" -- %s", it->description);
  }
  printf("\n");
 }
}

variable_types: nw int

variable_types: vw int

variable_types: len int

user_defined_types: enum ArgType { ARG_STRING=0, ARG_FLAG=1, ARG_INT=2, ARG_FLOAT=3 }


decompiled_code:
const char *__fastcall args_value(const char **a1, const char *a2)
{
const char **v2; // rbx
const char *v3; // rdi

v2 = a1;
v3 = *a1;
if ( v3 )
{
while ( strcmp(v3, a2) )
{
v3 = v2[5];
v2 += 5;
if ( !v3 )
return v3;
}
v3 = v2[2];
}
return v3;
}

canonical_decompiled_code:
const char *__fastcall func2(const char **a1, const char *a2)
{
const char **v2; // rbx
const char *v3; // rdi

v2 = a1;
v3 = *a1;
if ( v3 )
{
while ( strcmp(v3, a2) )
{
v3 = v2[5];
v2 += 5;
if ( !v3 )
return v3;
}
v3 = v2[2];
}
return v3;
}

original_code:
char* args_value(const Argument args[], const char* name)
{
 for (const Argument* it = args; it->name; it++) {
  if (strcmp(it->name, name) == 0) {
   return it->value;
  }
 }
 return ((void *)0);
}

canonical_original_code:
char *args_value(struct Argument_s args[], char *name)
{
 for (struct Argument_s *it = args; it->name; it++) {
  if (strcmp(it->name, name) == 0) {
   return it->value;
  }
 }
 return ((void *)0);
}

variable_types:
name
char *


isValid

decompiled_code:
__int64 __fastcall isValid(__int64 a1)
{
const char *v2; // rdi
unsigned int v3; // eax
__int64 result; // rax
char *endptr[2]; // [rsp+8h] [rbp-10h] BYREF

v2 = *(const char **)(a1 + 16);
v3 = *(_DWORD *)(a1 + 24);
if ( !v2 )
return v3 & 0x80;
endptr[0] = 0LL;
LOBYTE(v3) = v3 & 0x7F;
if ( v3 == 2 )
{
strtol(v2, endptr, 0);
result = *(char **)(a1 + 16) != endptr[0];
}
else if ( v3 > 2 )
{
if ( v3 == 3 )
{
strtod(v2, endptr);
result = *(char **)(a1 + 16) != endptr[0];
}
else
{
result = 0LL;
}
}
else
{
result = v3 != 2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
const char *v2; // rdi
unsigned int v3; // eax
__int64 result; // rax
char *endptr[2]; // [rsp+8h] [rbp-10h] BYREF

v2 = *(const char **)(a1 + 16);
v3 = *(_DWORD *)(a1 + 24);
if ( !v2 )
return v3 & 0x80;
endptr[0] = 0LL;
LOBYTE(v3) = v3 & 0x7F;
if ( v3 == 2 )
{
strtol(v2, endptr, 0);
result = *(char **)(a1 + 16) != endptr[0];
}
else if ( v3 > 2 )
{
if ( v3 == 3 )
{
strtod(v2, endptr);
result = *(char **)(a1 + 16) != endptr[0];
}
else
{
result = 0LL;
}
}
else
{
result = v3 != 2;
}
return result;
}

original_code:
int isValid(const Argument* arg)
{
 if (!arg->value) {
  return arg->type & 0x80;
 }
 char* endptr = ((void *)0);
 switch (arg->type &~ 0x80) {
 case ARG_STRING:
  return 1;
 case ARG_INT:
  strtol(arg->value, &endptr, 0);
  return endptr != arg->value;
 case ARG_FLOAT:
  strtod(arg->value, &endptr);
  return endptr != arg->value;
 case ARG_FLAG:
  return 1;
 default:
  return 0;
 }
}

canonical_original_code:
int isValid(struct Argument_s *arg)
{
 if (!arg->value) {
  return arg->type & 0x80;
 }
 char *endptr = ((void *)0);
 switch (arg->type &~ 0x80) {
 case ARG_STRING:
  return 1;
 case ARG_INT:
  strtol(arg->value, &endptr, 0);
  return endptr != arg->value;
 case ARG_FLOAT:
  strtod(arg->value, &endptr);
  return endptr != arg->value;
 case ARG_FLAG:
  return 1;
 default:
  return 0;
 }
}

variable_types:
endptr
char *


decompiled_code:
__int64 __fastcall args_parse(const char **a1, int a2, __int64 a3)
{
const char **v3; // rbx
const char **v4; // r13
int v5; // er15
const char *v6; // r14
const char *v7; // r14
const char **v8; // rax
const char **v9; // rbp
int v10; // eax
__int64 v13; // [rsp+10h] [rbp-48h]
unsigned int v14; // [rsp+1Ch] [rbp-3Ch]

v3 = a1;
if ( a2 <= 1 )
{
v14 = 1;
}
else
{
v14 = 1;
v4 = a1;
v5 = 1;
do
{
v13 = 8LL * v5;
v7 = *(const char **)(a3 + v13);
v8 = (const char **)lookupByPrefix(a1, v7);
v9 = v8;
if ( v8 )
{
v10 = *((_DWORD *)v8 + 6);
LOBYTE(v10) = v10 & 0x7F;
if ( v10 == 1 )
{
v9[2] = v7;
++v5;
}
else if ( a2 <= v5 + 1 || (v6 = *(const char **)(a3 + v13 + 8), lookupByPrefix(a1, v6)) )
{
++v5;
printf("Missing value for option '%s' after prefix %s\n", *v9, v9[1]);
v14 = 0;
}
else
{
v9[2] = v6;
v5 += 2;
}
}
else
{
while ( v4[1] )
v4 += 5;
if ( *v4 )
{
v4[2] = v7;
++v5;
v4 += 5;
}
else
{
++v5;
printf("Unexpected argument: '%s'\n", v7);
v14 = 0;
}
}
}
while ( a2 > v5 );
}
while ( *v3 )
{
if ( v3[2] )
{
if ( (unsigned int)isValid((__int64)v3) )
goto LABEL_16;
goto LABEL_20;
}
if ( *((char *)v3 + 24) < 0 )
{
if ( (unsigned int)isValid((__int64)v3) )
goto LABEL_16;
LABEL_20:
printf("Invalid value for argument '%s': %s\n", *v3, v3[2]);
v14 = 0;
goto LABEL_16;
}
printf("Missing argument: '%s'\n", *v3);
v14 = 0;
LABEL_16:
v3 += 5;
}
return v14;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char **a1, int a2, __int64 a3)
{
const char **v3; // rbx
const char **v4; // r13
int v5; // er15
const char *v6; // r14
const char *v7; // r14
const char **v8; // rax
const char **v9; // rbp
int v10; // eax
__int64 v13; // [rsp+10h] [rbp-48h]
unsigned int v14; // [rsp+1Ch] [rbp-3Ch]

v3 = a1;
if ( a2 <= 1 )
{
v14 = 1;
}
else
{
v14 = 1;
v4 = a1;
v5 = 1;
do
{
v13 = 8LL * v5;
v7 = *(const char **)(a3 + v13);
v8 = (const char **)lookupByPrefix(a1, v7);
v9 = v8;
if ( v8 )
{
v10 = *((_DWORD *)v8 + 6);
LOBYTE(v10) = v10 & 0x7F;
if ( v10 == 1 )
{
v9[2] = v7;
++v5;
}
else if ( a2 <= v5 + 1 || (v6 = *(const char **)(a3 + v13 + 8), lookupByPrefix(a1, v6)) )
{
++v5;
printf("Missing value for option '%s' after prefix %s\n", *v9, v9[1]);
v14 = 0;
}
else
{
v9[2] = v6;
v5 += 2;
}
}
else
{
while ( v4[1] )
v4 += 5;
if ( *v4 )
{
v4[2] = v7;
++v5;
v4 += 5;
}
else
{
++v5;
printf("Unexpected argument: '%s'\n", v7);
v14 = 0;
}
}
}
while ( a2 > v5 );
}
while ( *v3 )
{
if ( v3[2] )
{
if ( (unsigned int)func3((__int64)v3) )
goto LABEL_16;
goto LABEL_20;
}
if ( *((char *)v3 + 24) < 0 )
{
if ( (unsigned int)func3((__int64)v3) )
goto LABEL_16;
LABEL_20:
printf("Invalid value for argument '%s': %s\n", *v3, v3[2]);
v14 = 0;
goto LABEL_16;
}
printf("Missing argument: '%s'\n", *v3);
v14 = 0;
LABEL_16:
v3 += 5;
}
return v14;
}

original_code:
int args_parse(Argument args[], int argc, char** argv)
{
 int ok = 1;
 Argument* orderedArg = args;
 for (int i = 1; i < argc; i++) {
  Argument* arg = lookupByPrefix(args, argv[i]);
  if (arg) {
   if ((arg->type &~ 0x80) == ARG_FLAG) {
    arg->value = argv[i];
   } else {
    if ((i+1 >= argc) || lookupByPrefix(args, argv[i+1])) {
     printf("Missing value for option '%s' after prefix %s\n", arg->name, arg->prefix);
     ok = 0;
     continue;
    }
    i++;
    arg->value = argv[i];
   }
  } else {
   while (orderedArg->prefix) ++orderedArg;
   if (!orderedArg->name) {
    printf("Unexpected argument: '%s'\n", argv[i]);
    ok = 0;
    continue;
   }
   orderedArg->value = argv[i];
   ++orderedArg;
  }
 }
 for (const Argument* it = args; it->name; it++) {
  if (!it->value && !(it->type & 0x80)) {
   printf("Missing argument: '%s'\n", it->name);
   ok = 0;
  } else if (!isValid(it)) {
   printf("Invalid value for argument '%s': %s\n", it->name, it->value);
   ok = 0;
  }
 }
 return ok;
}

canonical_original_code:
int args_parse(struct Argument_s args[], int argc, char **argv)
{
 int ok = 1;
 struct Argument_s *orderedArg = args;
 for (int i = 1; i < argc; i++) {
  struct Argument_s *arg = lookupByPrefix(args, argv[i]);
  if (arg) {
   if ((arg->type &~ 0x80) == ARG_FLAG) {
    arg->value = argv[i];
   } else {
    if ((i+1 >= argc) || lookupByPrefix(args, argv[i+1])) {
     printf("Missing value for option '%s' after prefix %s\n", arg->name, arg->prefix);
     ok = 0;
     continue;
    }
    i++;
    arg->value = argv[i];
   }
  } else {
   while (orderedArg->prefix) ++orderedArg;
   if (!orderedArg->name) {
    printf("Unexpected argument: '%s'\n", argv[i]);
    ok = 0;
    continue;
   }
   orderedArg->value = argv[i];
   ++orderedArg;
  }
 }
 for (struct Argument_s *it = args; it->name; it++) {
  if (!it->value && !(it->type & 0x80)) {
   printf("Missing argument: '%s'\n", it->name);
   ok = 0;
  } else if (!isValid(it)) {
   printf("Invalid value for argument '%s': %s\n", it->name, it->value);
   ok = 0;
  }
 }
 return ok;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ok int


threadproc

decompiled_code:
void *__fastcall threadproc(void *a1)
{
__int64 v1; // rsi
unsigned int v3; // [rsp+4h] [rbp-14h] BYREF
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

if ( a1 )
{
v4 = 0LL;
while ( *((_DWORD *)a1 + 3) )
{
while ( 1 )
{
v1 = *((unsigned int *)a1 + 2);
if ( (int)v1 >= 0 )
{
v3 = 0;
if ( !(unsigned int)timer_barrier(&v4, v1, &v3) && log_level > 1 )
break;
}
(*((void (__fastcall **)(void *))a1 + 2))(a1);
if ( !*((_DWORD *)a1 + 3) )
return 0LL;
}
log_message(
"pylon/meter.c",
"threadproc",
2LL,
"Can't keep up with measurement interval %d ms, time elapsed: %d ms\n",
*((unsigned int *)a1 + 2),
v3);
(*((void (__fastcall **)(void *))a1 + 2))(a1);
}
}
else if ( log_level >= 0 )
{
log_message("pylon/meter.c", "threadproc", 0LL, "No context specified\n");
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
__int64 v1; // rsi
unsigned int v3; // [rsp+4h] [rbp-14h] BYREF
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

if ( a1 )
{
v4 = 0LL;
while ( *((_DWORD *)a1 + 3) )
{
while ( 1 )
{
v1 = *((unsigned int *)a1 + 2);
if ( (int)v1 >= 0 )
{
v3 = 0;
if ( !(unsigned int)timer_barrier(&v4, v1, &v3) && log_level > 1 )
break;
}
(*((void (__fastcall **)(void *))a1 + 2))(a1);
if ( !*((_DWORD *)a1 + 3) )
return 0LL;
}
log_message(
"pylon/meter.c",
"threadproc",
2LL,
"Can't keep up with measurement interval %d ms, time elapsed: %d ms\n",
*((unsigned int *)a1 + 2),
v3);
(*((void (__fastcall **)(void *))a1 + 2))(a1);
}
}
else if ( log_level >= 0 )
{
log_message("pylon/meter.c", "threadproc", 0LL, "No context specified\n");
}
return 0LL;
}

original_code:
void* threadproc(void* arg)
{
 MeterHandle* handle = (MeterHandle*)arg;
 if (!handle) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "No context specified\n");
  return ((void *)0);
 }
 uint64_t barrier = 0;
 while (handle->running) {
  if (handle->interval >= 0) {
   int elapsed = 0;
   if (!timer_barrier(&barrier, handle->interval, &elapsed)) {
    if (2 <= log_level) log_message("pylon/meter.c", __func__, 2, "Can't keep up with measurement interval %d ms, time elapsed: %d ms\n", handle->interval, elapsed);
   }
  }
  handle->measure(handle);
 }
 return ((void *)0);
}

canonical_original_code:
void *threadproc(void *arg)
{
 struct MeterHandle_s *handle = (MeterHandle*)arg;
 if (!handle) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "No context specified\n");
  return ((void *)0);
 }
 unsigned long barrier = 0;
 while (handle->running) {
  if (handle->interval >= 0) {
   int elapsed = 0;
   if (!timer_barrier(&barrier, handle->interval, &elapsed)) {
    if (2 <= log_level) log_message("pylon/meter.c", __func__, 2, "Can't keep up with measurement interval %d ms, time elapsed: %d ms\n", handle->interval, elapsed);
   }
  }
  handle->measure(handle);
 }
 return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct MeterHandle_s { unsigned long thread; int interval; int running; void (*measure)(struct MeterHandle_s *handle); } *

variable_types:
barrier
unsigned long

variable_types: elapsed int

return_type: void *


decompiled_code:
__int64 __fastcall meter_stop(__int64 a1)
{
unsigned int v1; // er12

if ( !a1 )
{
v1 = 0;
if ( log_level >= 0 )
log_message("pylon/meter.c", "meter_stop", 0LL, "No handle specified\n");
return v1;
}
if ( *(_DWORD *)(a1 + 12) )
{
*(_DWORD *)(a1 + 12) = 0;
return 1;
}
v1 = 1;
if ( log_level <= 0 )
return v1;
log_message("pylon/meter.c", "meter_stop", 1LL, "Not running\n");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // er12

if ( !a1 )
{
v1 = 0;
if ( log_level >= 0 )
log_message("pylon/meter.c", "meter_stop", 0LL, "No handle specified\n");
return v1;
}
if ( *(_DWORD *)(a1 + 12) )
{
*(_DWORD *)(a1 + 12) = 0;
return 1;
}
v1 = 1;
if ( log_level <= 0 )
return v1;
log_message("pylon/meter.c", "meter_stop", 1LL, "Not running\n");
return 1LL;
}

original_code:
int meter_stop(MeterHandle* handle)
{
 if (!handle) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "No handle specified\n");
  return 0;
 }
 if (!handle->running) {
  if (1 <= log_level) log_message("pylon/meter.c", __func__, 1, "Not running\n");
  return 1;
 }
 handle->running = 0;
 return 1;
}

canonical_original_code:
int meter_stop(struct MeterHandle_s *handle)
{
 if (!handle) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "No handle specified\n");
  return 0;
 }
 if (!handle->running) {
  if (1 <= log_level) log_message("pylon/meter.c", __func__, 1, "Not running\n");
  return 1;
 }
 handle->running = 0;
 return 1;
}

user_defined_types: struct MeterHandle_s { unsigned long thread; int interval; int running; void (*measure)(struct MeterHandle_s *handle); }


decompiled_code:
__int64 __fastcall meter_join(void *ptr)
{
int v1; // eax
__int64 result; // rax
char *v3; // rax

if ( ptr )
{
v1 = pthread_join(*(_QWORD *)ptr, 0LL);
if ( v1 )
{
if ( log_level < 0 )
return 0LL;
v3 = strerror(v1);
log_message("pylon/meter.c", "meter_join", 0LL, "Failed to join thread: %s\n", v3);
result = 0LL;
}
else
{
free(ptr);
result = 1LL;
}
}
else
{
if ( log_level < 0 )
return 0LL;
log_message("pylon/meter.c", "meter_join", 0LL, "No handle specified\n");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *ptr)
{
int v1; // eax
__int64 result; // rax
char *v3; // rax

if ( ptr )
{
v1 = pthread_join(*(_QWORD *)ptr, 0LL);
if ( v1 )
{
if ( log_level < 0 )
return 0LL;
v3 = strerror(v1);
log_message("pylon/meter.c", "meter_join", 0LL, "Failed to join thread: %s\n", v3);
result = 0LL;
}
else
{
free(ptr);
result = 1LL;
}
}
else
{
if ( log_level < 0 )
return 0LL;
log_message("pylon/meter.c", "meter_join", 0LL, "No handle specified\n");
result = 0LL;
}
return result;
}

original_code:
int meter_join(MeterHandle* handle)
{
 if (!handle) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "No handle specified\n");
  return 0;
 }
 int error = pthread_join(handle->thread, ((void *)0));
 if (error != 0) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "Failed to join thread: %s\n", strerror(error));
  return 0;
 }
 free(handle);
 return 1;
}

canonical_original_code:
int meter_join(struct MeterHandle_s *handle)
{
 if (!handle) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "No handle specified\n");
  return 0;
 }
 int error = pthread_join(handle->thread, ((void *)0));
 if (error != 0) {
  if (0 <= log_level) log_message("pylon/meter.c", __func__, 0, "Failed to join thread: %s\n", strerror(error));
  return 0;
 }
 free(handle);
 return 1;
}

variable_types: error int


decompiled_code:
void __fastcall queue_free(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 48));
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 48));
free(*(void **)ptr);
free(ptr);
}
}

original_code:
void queue_free(Queue* queue)
{
 if (queue) {
  pthread_mutex_destroy(&queue->lock);
  free(queue->buffer);
  free(queue);
 }
}

canonical_original_code:
void queue_free(struct Queue_s *queue)
{
 if (queue) {
  pthread_mutex_destroy(&queue->lock);
  free(queue->buffer);
  free(queue);
 }
}

variable_types: struct Queue_s { char *buffer; unsigned long count; unsigned long capacity; unsigned long itemSize; int head; int tail; int level; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; } *

user_defined_types: struct Queue_s { char *buffer; unsigned long count; unsigned long capacity; unsigned long itemSize; int head; int tail; int level; union pthread_mutex_t lock; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall queue_enqueue(__int64 a1, const void *a2)
{
pthread_mutex_t *v2; // r13
int v3; // eax
unsigned int v4; // er14
unsigned __int64 v5; // rdx
unsigned __int64 v6; // rax
unsigned int v7; // ebp
double v8; // xmm3_8
__int64 i; // rbp
double v10; // xmm0_8
int v11; // eax
char *v13; // rax

v2 = (pthread_mutex_t *)(a1 + 48);
v3 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
v4 = v3;
if ( v3 )
{
v4 = 0;
if ( log_level >= 0 )
{
v13 = strerror(v3);
log_message("pylon/queue.c", "queue_enqueue", 0LL, "Failed to lock mutex: %s\n", v13);
}
}
else
{
v5 = *(_QWORD *)(a1 + 8);
v6 = *(_QWORD *)(a1 + 16);
if ( v5 >= v6 )
{
pthread_mutex_unlock(v2);
}
else
{
v7 = *(_DWORD *)(a1 + 40);
if ( v7 <= 4 )
{
v8 = *(double *)&LC5;
for ( i = (int)(v7 + 1); ; ++i )
{
v10 = *(double *)&qword_718[i] + v8;
if ( (v6 & 0x8000000000000000LL) == 0LL )
{
if ( (int)(v10 * (double)(int)v6) != v5 )
goto LABEL_6;
}
else if ( (int)(v10 * ((double)(int)(v6 & 1 | (v6 >> 1)) + (double)(int)(v6 & 1 | (v6 >> 1)))) != v5 )
{
LABEL_6:
if ( (unsigned int)i > 4 )
break;
goto LABEL_7;
}
if ( log_level > 0 )
{
log_message(
"pylon/queue.c",
"queue_enqueue",
1LL,
"Measurement buffer exceeds %.0f%% of its capacity\n",
100.0 * *(double *)&qword_718[i]);
v8 = *(double *)&LC5;
}
*(_DWORD *)(a1 + 40) = i;
if ( (unsigned int)i > 4 )
break;
LABEL_7:
v5 = *(_QWORD *)(a1 + 8);
v6 = *(_QWORD *)(a1 + 16);
}
}
v4 = 1;
memcpy((void *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 24) * *(int *)(a1 + 36)), a2, *(_QWORD *)(a1 + 24));
v11 = *(_DWORD *)(a1 + 36);
++*(_QWORD *)(a1 + 8);
*(_DWORD *)(a1 + 36) = (unsigned __int64)(v11 + 1) % *(_QWORD *)(a1 + 16);
pthread_mutex_unlock(v2);
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const void *a2)
{
pthread_mutex_t *v2; // r13
int v3; // eax
unsigned int v4; // er14
unsigned __int64 v5; // rdx
unsigned __int64 v6; // rax
unsigned int v7; // ebp
double v8; // xmm3_8
__int64 i; // rbp
double v10; // xmm0_8
int v11; // eax
char *v13; // rax

v2 = (pthread_mutex_t *)(a1 + 48);
v3 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
v4 = v3;
if ( v3 )
{
v4 = 0;
if ( log_level >= 0 )
{
v13 = strerror(v3);
log_message("pylon/queue.c", "queue_enqueue", 0LL, "Failed to lock mutex: %s\n", v13);
}
}
else
{
v5 = *(_QWORD *)(a1 + 8);
v6 = *(_QWORD *)(a1 + 16);
if ( v5 >= v6 )
{
pthread_mutex_unlock(v2);
}
else
{
v7 = *(_DWORD *)(a1 + 40);
if ( v7 <= 4 )
{
v8 = *(double *)&LC5;
for ( i = (int)(v7 + 1); ; ++i )
{
v10 = *(double *)&qword_718[i] + v8;
if ( (v6 & 0x8000000000000000LL) == 0LL )
{
if ( (int)(v10 * (double)(int)v6) != v5 )
goto LABEL_6;
}
else if ( (int)(v10 * ((double)(int)(v6 & 1 | (v6 >> 1)) + (double)(int)(v6 & 1 | (v6 >> 1)))) != v5 )
{
LABEL_6:
if ( (unsigned int)i > 4 )
break;
goto LABEL_7;
}
if ( log_level > 0 )
{
log_message(
"pylon/queue.c",
"queue_enqueue",
1LL,
"Measurement buffer exceeds %.0f%% of its capacity\n",
100.0 * *(double *)&qword_718[i]);
v8 = *(double *)&LC5;
}
*(_DWORD *)(a1 + 40) = i;
if ( (unsigned int)i > 4 )
break;
LABEL_7:
v5 = *(_QWORD *)(a1 + 8);
v6 = *(_QWORD *)(a1 + 16);
}
}
v4 = 1;
memcpy((void *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 24) * *(int *)(a1 + 36)), a2, *(_QWORD *)(a1 + 24));
v11 = *(_DWORD *)(a1 + 36);
++*(_QWORD *)(a1 + 8);
*(_DWORD *)(a1 + 36) = (unsigned __int64)(v11 + 1) % *(_QWORD *)(a1 + 16);
pthread_mutex_unlock(v2);
}
}
return v4;
}

original_code:
int queue_enqueue(Queue* queue, const void* item)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return 0;
 }
 if (queue->count >= queue->capacity) {
  pthread_mutex_unlock(&queue->lock);
  return 0;
 }
 for (int i = queue->level; i < (sizeof((s_capLevels))/sizeof((s_capLevels)[0])); i++) {
  if (queue->count == (int)((s_capLevels[i] + 0.01) * queue->capacity)) {
   if (1 <= log_level) log_message("pylon/queue.c", __func__, 1, "Measurement buffer exceeds %.0f%% of its capacity\n", s_capLevels[i]*100);
   queue->level = i+1;
  }
 }
 memcpy(queue->buffer + queue->tail * queue->itemSize, item, queue->itemSize);
 queue->tail = (queue->tail+1) % queue->capacity;
 queue->count++;
  pthread_mutex_unlock(&queue->lock);
 return 1;
}

canonical_original_code:
int queue_enqueue(struct Queue_s *queue, void *item)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return 0;
 }
 if (queue->count >= queue->capacity) {
  pthread_mutex_unlock(&queue->lock);
  return 0;
 }
 for (int i = queue->level; i < (sizeof((s_capLevels))/sizeof((s_capLevels)[0])); i++) {
  if (queue->count == (int)((s_capLevels[i] + 0.01) * queue->capacity)) {
   if (1 <= log_level) log_message("pylon/queue.c", __func__, 1, "Measurement buffer exceeds %.0f%% of its capacity\n", s_capLevels[i]*100);
   queue->level = i+1;
  }
 }
 memcpy(queue->buffer + queue->tail * queue->itemSize, item, queue->itemSize);
 queue->tail = (queue->tail+1) % queue->capacity;
 queue->count++;
  pthread_mutex_unlock(&queue->lock);
 return 1;
}

variable_types:
item
void *

variable_types: i int

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall queue_dequeue(__int64 a1, void *a2)
{
pthread_mutex_t *v2; // r13
int v3; // eax
__int64 v4; // rdx
double v5; // xmm3_8
__int64 v6; // rbp
__int64 v7; // xmm1_8
double v8; // xmm0_8
int v9; // er14
__int64 v10; // rax
__int64 v11; // rcx
int v12; // eax
__int64 result; // rax
char *v14; // rax

v2 = (pthread_mutex_t *)(a1 + 48);
v3 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
if ( v3 )
{
if ( log_level >= 0 )
{
v14 = strerror(v3);
log_message("pylon/queue.c", "queue_dequeue", 0LL, "Failed to lock mutex: %s\n", v14);
}
result = 0LL;
}
else
{
v4 = *(_QWORD *)(a1 + 8);
if ( v4 )
{
if ( *(int *)(a1 + 40) > 0 )
{
v5 = *(double *)&LC5;
v6 = 0LL;
v7 = LC5;
while ( 1 )
{
v10 = *(_QWORD *)(a1 + 16);
v9 = v6;
if ( v10 >= 0 )
{
v8 = (double)(int)v10;
}
else
{
v11 = *(_QWORD *)(a1 + 16) & 1LL | (*(_QWORD *)(a1 + 16) >> 1);
v8 = (double)(int)v11 + (double)(int)v11;
}
if ( (int)(v8 * (*(double *)&v7 - v5)) == v4 )
{
if ( log_level > 0 )
{
log_message(
"pylon/queue.c",
"queue_dequeue",
1LL,
"Measurement buffer falls below %.0f%% of its capacity\n",
100.0 * *(double *)&v7);
v5 = *(double *)&LC5;
}
*(_DWORD *)(a1 + 40) = v6;
}
else
{
v9 = *(_DWORD *)(a1 + 40);
}
if ( v9 <= (int)++v6 )
break;
v7 = s_capLevels[v6];
v4 = *(_QWORD *)(a1 + 8);
}
}
memcpy(a2, (const void *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 24) * *(int *)(a1 + 32)), *(_QWORD *)(a1 + 24));
v12 = *(_DWORD *)(a1 + 32);
--*(_QWORD *)(a1 + 8);
*(_DWORD *)(a1 + 32) = (unsigned __int64)(v12 + 1) % *(_QWORD *)(a1 + 16);
pthread_mutex_unlock(v2);
result = 1LL;
}
else
{
pthread_mutex_unlock(v2);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, void *a2)
{
pthread_mutex_t *v2; // r13
int v3; // eax
__int64 v4; // rdx
double v5; // xmm3_8
__int64 v6; // rbp
__int64 v7; // xmm1_8
double v8; // xmm0_8
int v9; // er14
__int64 v10; // rax
__int64 v11; // rcx
int v12; // eax
__int64 result; // rax
char *v14; // rax

v2 = (pthread_mutex_t *)(a1 + 48);
v3 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
if ( v3 )
{
if ( log_level >= 0 )
{
v14 = strerror(v3);
log_message("pylon/queue.c", "queue_dequeue", 0LL, "Failed to lock mutex: %s\n", v14);
}
result = 0LL;
}
else
{
v4 = *(_QWORD *)(a1 + 8);
if ( v4 )
{
if ( *(int *)(a1 + 40) > 0 )
{
v5 = *(double *)&LC5;
v6 = 0LL;
v7 = LC5;
while ( 1 )
{
v10 = *(_QWORD *)(a1 + 16);
v9 = v6;
if ( v10 >= 0 )
{
v8 = (double)(int)v10;
}
else
{
v11 = *(_QWORD *)(a1 + 16) & 1LL | (*(_QWORD *)(a1 + 16) >> 1);
v8 = (double)(int)v11 + (double)(int)v11;
}
if ( (int)(v8 * (*(double *)&v7 - v5)) == v4 )
{
if ( log_level > 0 )
{
log_message(
"pylon/queue.c",
"queue_dequeue",
1LL,
"Measurement buffer falls below %.0f%% of its capacity\n",
100.0 * *(double *)&v7);
v5 = *(double *)&LC5;
}
*(_DWORD *)(a1 + 40) = v6;
}
else
{
v9 = *(_DWORD *)(a1 + 40);
}
if ( v9 <= (int)++v6 )
break;
v7 = s_capLevels[v6];
v4 = *(_QWORD *)(a1 + 8);
}
}
memcpy(a2, (const void *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 24) * *(int *)(a1 + 32)), *(_QWORD *)(a1 + 24));
v12 = *(_DWORD *)(a1 + 32);
--*(_QWORD *)(a1 + 8);
*(_DWORD *)(a1 + 32) = (unsigned __int64)(v12 + 1) % *(_QWORD *)(a1 + 16);
pthread_mutex_unlock(v2);
result = 1LL;
}
else
{
pthread_mutex_unlock(v2);
result = 0LL;
}
}
return result;
}

original_code:
int queue_dequeue(Queue* queue, void* item)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return 0;
 }
 if (queue->count == 0) {
  pthread_mutex_unlock(&queue->lock);
  return 0;
 }
 for (int i = 0; i < queue->level; i++) {
  if (queue->count == (int)((s_capLevels[i] - 0.01) * queue->capacity)) {
   if (1 <= log_level) log_message("pylon/queue.c", __func__, 1, "Measurement buffer falls below %.0f%% of its capacity\n", s_capLevels[i]*100);
   queue->level = i;
  }
 }
 memcpy(item, queue->buffer + queue->head * queue->itemSize, queue->itemSize);
 queue->head = (queue->head+1) % queue->capacity;
 queue->count--;
 pthread_mutex_unlock(&queue->lock);
 return 1;
}

canonical_original_code:
int queue_dequeue(struct Queue_s *queue, void *item)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return 0;
 }
 if (queue->count == 0) {
  pthread_mutex_unlock(&queue->lock);
  return 0;
 }
 for (int i = 0; i < queue->level; i++) {
  if (queue->count == (int)((s_capLevels[i] - 0.01) * queue->capacity)) {
   if (1 <= log_level) log_message("pylon/queue.c", __func__, 1, "Measurement buffer falls below %.0f%% of its capacity\n", s_capLevels[i]*100);
   queue->level = i;
  }
 }
 memcpy(item, queue->buffer + queue->head * queue->itemSize, queue->itemSize);
 queue->head = (queue->head+1) % queue->capacity;
 queue->count--;
 pthread_mutex_unlock(&queue->lock);
 return 1;
}


decompiled_code:
__int64 __fastcall queue_count(__int64 a1)
{
int v1; // eax
__int64 v2; // r12
char *v4; // rax

v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
if ( !v1 )
{
v2 = *(_QWORD *)(a1 + 8);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
return (int)v2;
}
v2 = -1LL;
if ( log_level < 0 )
return v2;
v4 = strerror(v1);
log_message("pylon/queue.c", "queue_count", 0LL, "Failed to lock mutex: %s\n", v4);
return -1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
int v1; // eax
__int64 v2; // r12
char *v4; // rax

v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
if ( !v1 )
{
v2 = *(_QWORD *)(a1 + 8);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
return (int)v2;
}
v2 = -1LL;
if ( log_level < 0 )
return v2;
v4 = strerror(v1);
log_message("pylon/queue.c", "queue_count", 0LL, "Failed to lock mutex: %s\n", v4);
return -1LL;
}

original_code:
size_t queue_count(Queue* queue)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return -1;
 }
 int count = queue->count;
 pthread_mutex_unlock(&queue->lock);
 return count;
}

canonical_original_code:
unsigned long queue_count(struct Queue_s *queue)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return -1;
 }
 int count = queue->count;
 pthread_mutex_unlock(&queue->lock);
 return count;
}

variable_types: count int


decompiled_code:
int __fastcall queue_clear(__int64 a1)
{
int result; // eax
char *v2; // rax

result = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
if ( result )
{
if ( log_level >= 0 )
{
v2 = strerror(result);
result = log_message("pylon/queue.c", "queue_clear", 0LL, "Failed to lock mutex: %s\n", v2);
}
}
else
{
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 32) = 0LL;
result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
int result; // eax
char *v2; // rax

result = pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
if ( result )
{
if ( log_level >= 0 )
{
v2 = strerror(result);
result = log_message("pylon/queue.c", "queue_clear", 0LL, "Failed to lock mutex: %s\n", v2);
}
}
else
{
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 32) = 0LL;
result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
}
return result;
}

original_code:
void queue_clear(Queue* queue)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return;
 }
 queue->count = 0;
 queue->head = 0;
 queue->tail = 0;
 pthread_mutex_unlock(&queue->lock);
}

canonical_original_code:
void queue_clear(struct Queue_s *queue)
{
 int error = pthread_mutex_lock(&queue->lock);
 if (error) {
  if (0 <= log_level) log_message("pylon/queue.c", __func__, 0, "Failed to lock mutex: %s\n", strerror(error));
  return;
 }
 queue->count = 0;
 queue->head = 0;
 queue->tail = 0;
 pthread_mutex_unlock(&queue->lock);
}


decompiled_code:
char *__fastcall l_trim(char *dest, char *src)
{
char *v2; // rbp

if ( !src )
_assert_fail("szInput != NULL", "modify_config.c", 0xD1u, "l_trim");
if ( !dest )
_assert_fail("szOutput != NULL", "modify_config.c", 0xD2u, "l_trim");
v2 = src;
if ( src == dest )
_assert_fail("szOutput != szInput", "modify_config.c", 0xD3u, "l_trim");
while ( *v2 && isspace(*v2) )
++v2;
return strcpy(dest, v2);
}

canonical_decompiled_code:
char *__fastcall func0(char *dest, char *src)
{
char *v2; // rbp

if ( !src )
_assert_fail("szInput != NULL", "modify_config.c", 0xD1u, "l_trim");
if ( !dest )
_assert_fail("szOutput != NULL", "modify_config.c", 0xD2u, "l_trim");
v2 = src;
if ( src == dest )
_assert_fail("szOutput != szInput", "modify_config.c", 0xD3u, "l_trim");
while ( *v2 && isspace(*v2) )
++v2;
return strcpy(dest, v2);
}

original_code:
char * l_trim(char * szOutput, const char *szInput)
{
 ((void) sizeof ((szInput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szInput != ((void *)0)) ; else __assert_fail ("szInput != NULL", "modify_config.c", 209, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szOutput != ((void *)0)) ; else __assert_fail ("szOutput != NULL", "modify_config.c", 210, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != szInput) ? 1 : 0), __extension__ ({ if (szOutput != szInput) ; else __assert_fail ("szOutput != szInput", "modify_config.c", 211, __extension__ __PRETTY_FUNCTION__); }));
 for (((void *)0); *szInput != '\0' && isspace(*szInput); ++szInput){
  ;
 }
 return strcpy(szOutput, szInput);
}

canonical_original_code:
char *l_trim(char *szOutput, char *szInput)
{
 ((void) sizeof ((szInput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szInput != ((void *)0)) ; else __assert_fail ("szInput != NULL", "modify_config.c", 209, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szOutput != ((void *)0)) ; else __assert_fail ("szOutput != NULL", "modify_config.c", 210, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != szInput) ? 1 : 0), __extension__ ({ if (szOutput != szInput) ; else __assert_fail ("szOutput != szInput", "modify_config.c", 211, __extension__ __PRETTY_FUNCTION__); }));
 for (((void *)0); *szInput != '\0' && isspace(*szInput); ++szInput){
  ;
 }
 return strcpy(szOutput, szInput);
}

variable_types:
szOutput
char *

variable_types:
szInput
char *


decompiled_code:
char *__fastcall r_trim(char *a1, const char *a2)
{
char *i; // rbx

if ( !a2 )
_assert_fail("szInput != NULL", "modify_config.c", 0xDEu, "r_trim");
if ( !a1 )
_assert_fail("szOutput != NULL", "modify_config.c", 0xDFu, "r_trim");
if ( a2 == a1 )
_assert_fail("szOutput != szInput", "modify_config.c", 0xE0u, "r_trim");
for ( i = stpcpy(a1, a2) - 1; a1 <= i; --i )
{
if ( !isspace(*i) )
break;
}
i[1] = 0;
return a1;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1, const char *a2)
{
char *i; // rbx

if ( !a2 )
_assert_fail("szInput != NULL", "modify_config.c", 0xDEu, "r_trim");
if ( !a1 )
_assert_fail("szOutput != NULL", "modify_config.c", 0xDFu, "r_trim");
if ( a2 == a1 )
_assert_fail("szOutput != szInput", "modify_config.c", 0xE0u, "r_trim");
for ( i = stpcpy(a1, a2) - 1; a1 <= i; --i )
{
if ( !isspace(*i) )
break;
}
i[1] = 0;
return a1;
}

original_code:
char *r_trim(char *szOutput, const char *szInput)
{
 char *p = ((void *)0);
 ((void) sizeof ((szInput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szInput != ((void *)0)) ; else __assert_fail ("szInput != NULL", "modify_config.c", 222, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szOutput != ((void *)0)) ; else __assert_fail ("szOutput != NULL", "modify_config.c", 223, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != szInput) ? 1 : 0), __extension__ ({ if (szOutput != szInput) ; else __assert_fail ("szOutput != szInput", "modify_config.c", 224, __extension__ __PRETTY_FUNCTION__); }));
 strcpy(szOutput, szInput);
 for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p){
  ;
 }
 *(++p) = '\0';
 return szOutput;
}

canonical_original_code:
char *r_trim(char *szOutput, char *szInput)
{
 char *p = ((void *)0);
 ((void) sizeof ((szInput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szInput != ((void *)0)) ; else __assert_fail ("szInput != NULL", "modify_config.c", 222, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szOutput != ((void *)0)) ; else __assert_fail ("szOutput != NULL", "modify_config.c", 223, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != szInput) ? 1 : 0), __extension__ ({ if (szOutput != szInput) ; else __assert_fail ("szOutput != szInput", "modify_config.c", 224, __extension__ __PRETTY_FUNCTION__); }));
 strcpy(szOutput, szInput);
 for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p){
  ;
 }
 *(++p) = '\0';
 return szOutput;
}

variable_types:
p
char *


decompiled_code:
char *__fastcall a_trim(char *s, char *a2)
{
char *i; // rbx

if ( !a2 )
_assert_fail("szInput != NULL", "modify_config.c", 0xEDu, "a_trim");
if ( !s )
_assert_fail("szOutput != NULL", "modify_config.c", 0xEEu, "a_trim");
l_trim(s, a2);
for ( i = &s[strlen(s) - 1]; s <= i; --i )
{
if ( !isspace(*i) )
break;
}
i[1] = 0;
return s;
}

canonical_decompiled_code:
char *__fastcall func3(char *s, char *a2)
{
char *i; // rbx

if ( !a2 )
_assert_fail("szInput != NULL", "modify_config.c", 0xEDu, "a_trim");
if ( !s )
_assert_fail("szOutput != NULL", "modify_config.c", 0xEEu, "a_trim");
func0(s, a2);
for ( i = &s[strlen(s) - 1]; s <= i; --i )
{
if ( !isspace(*i) )
break;
}
i[1] = 0;
return s;
}

original_code:
char * a_trim(char * szOutput, const char * szInput)
{
 char *p = ((void *)0);
 ((void) sizeof ((szInput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szInput != ((void *)0)) ; else __assert_fail ("szInput != NULL", "modify_config.c", 237, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szOutput != ((void *)0)) ; else __assert_fail ("szOutput != NULL", "modify_config.c", 238, __extension__ __PRETTY_FUNCTION__); }));
 l_trim(szOutput, szInput);
 for (p = szOutput + strlen(szOutput) - 1;p >= szOutput && isspace(*p); --p){
  ;
 }
 *(++p) = '\0';
 return szOutput;
}

canonical_original_code:
char *a_trim(char *szOutput, char *szInput)
{
 char *p = ((void *)0);
 ((void) sizeof ((szInput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szInput != ((void *)0)) ; else __assert_fail ("szInput != NULL", "modify_config.c", 237, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((szOutput != ((void *)0)) ? 1 : 0), __extension__ ({ if (szOutput != ((void *)0)) ; else __assert_fail ("szOutput != NULL", "modify_config.c", 238, __extension__ __PRETTY_FUNCTION__); }));
 l_trim(szOutput, szInput);
 for (p = szOutput + strlen(szOutput) - 1;p >= szOutput && isspace(*p); --p){
  ;
 }
 *(++p) = '\0';
 return szOutput;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
rmConfSection("./sample.ceph.conf", "osd.0", envp);
return 0;
}

original_code:
int main()
{
  rmConfSection("./sample.ceph.conf","osd.0");
  return 0;
}


repo: wlandau/shascii

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er15
int v4; // er14
int v5; // er13
int v6; // er12
const char **v8; // rsi
char v9; // al
__int64 v10; // rdx
char *v11; // r12
size_t v12; // rax
const char *v13; // rsi
int v14; // ebx
char *v15; // rbp
char v16; // al
char *v17; // rdx
size_t v18; // rax
unsigned int v19; // [rsp+4h] [rbp-154h]
unsigned __int64 v20; // [rsp+8h] [rbp-150h]
void *ptr; // [rsp+10h] [rbp-148h] BYREF
char *s; // [rsp+18h] [rbp-140h] BYREF
char dest[312]; // [rsp+20h] [rbp-138h] BYREF

v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
ptr = 0LL;
s = 0LL;
v20 = 0LL;
v19 = 0;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v8 = argv;
v9 = getopt(argc, (char *const *)argv, "acd:f:hpt:x:");
if ( v9 == -1 )
{
if ( !v6 )
{
if ( v4 )
{
v14 = 0;
v15 = (char *)calloc(0x2001uLL, 1uLL);
ptr = calloc(1uLL, 1uLL);
while ( 1 )
{
v16 = fgetc(_bss_start);
v17 = &v15[v6];
if ( v16 == -1 )
break;
*v17 = v16;
v6 = ((_WORD)v6 + 1) & 0x1FFF;
if ( !v6 )
{
ptr = realloc(ptr, (++v14 << 13) + 1);
strcat((char *)ptr, v15);
}
}
*v17 = 0;
v18 = strlen(v15);
ptr = realloc(ptr, (v14 << 13) + v18 + 1);
strcat((char *)ptr, v15);
*((char *)ptr + strlen((const char *)ptr) - 1) = 0;
compute_output(&s, ptr, v20, v19, v3);
puts(s);
free(v15);
}
else
{
if ( !v5 )
LABEL_30:
usage((unsigned int)argc, argv, v10);
LABEL_23:
compute_output(&s, ptr, v20, v19, v3);
puts(s);
}
if ( ptr )
free(ptr);
if ( s )
free(s);
exit(0);
}
read_file(&ptr, dest);
goto LABEL_23;
}
if ( v9 == 97 )
about();
if ( v9 != 99 )
break;
v3 = 1;
}
v10 = 10LL;
if ( v9 != 100 )
break;
LABEL_12:
v19 = 1;
v20 = strtoul(optarg, 0LL, v10);
}
switch ( v9 )
{
case 'f':
v8 = (const char **)optarg;
strcpy(dest, optarg);
v4 |= v5;
if ( v4 )
goto LABEL_37;
v5 = 0;
v6 = 1;
break;
case 'h':
goto LABEL_30;
case 'p':
v6 |= v5;
if ( v6 )
goto LABEL_37;
v5 = 0;
v4 = 1;
break;
case 't':
v4 |= v6;
if ( v4 )
LABEL_37:
handle_error("Flags -f, -p, and -t are mutually exclusive.", v8, v10);
v11 = optarg;
v5 = 1;
v12 = strlen(optarg);
v13 = v11;
v6 = 0;
ptr = calloc(v12, 1uLL);
strcpy((char *)ptr, v13);
break;
case 'x':
LODWORD(v10) = 16;
goto LABEL_12;
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er15
int v4; // er14
int v5; // er13
int v6; // er12
const char **v8; // rsi
char v9; // al
__int64 v10; // rdx
char *v11; // r12
size_t v12; // rax
const char *v13; // rsi
int v14; // ebx
char *v15; // rbp
char v16; // al
char *v17; // rdx
size_t v18; // rax
unsigned int v19; // [rsp+4h] [rbp-154h]
unsigned __int64 v20; // [rsp+8h] [rbp-150h]
void *ptr; // [rsp+10h] [rbp-148h] BYREF
char *s; // [rsp+18h] [rbp-140h] BYREF
char dest[312]; // [rsp+20h] [rbp-138h] BYREF

v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0;
ptr = 0LL;
s = 0LL;
v20 = 0LL;
v19 = 0;
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v8 = argv;
v9 = getopt(argc, (char *const *)argv, "acd:f:hpt:x:");
if ( v9 == -1 )
{
if ( !v6 )
{
if ( v4 )
{
v14 = 0;
v15 = (char *)calloc(0x2001uLL, 1uLL);
ptr = calloc(1uLL, 1uLL);
while ( 1 )
{
v16 = fgetc(_bss_start);
v17 = &v15[v6];
if ( v16 == -1 )
break;
*v17 = v16;
v6 = ((_WORD)v6 + 1) & 0x1FFF;
if ( !v6 )
{
ptr = realloc(ptr, (++v14 << 13) + 1);
strcat((char *)ptr, v15);
}
}
*v17 = 0;
v18 = strlen(v15);
ptr = realloc(ptr, (v14 << 13) + v18 + 1);
strcat((char *)ptr, v15);
*((char *)ptr + strlen((const char *)ptr) - 1) = 0;
func12(&s, ptr, v20, v19, v3);
puts(s);
free(v15);
}
else
{
if ( !v5 )
LABEL_30:
func8((unsigned int)argc, argv, v10);
LABEL_23:
func12(&s, ptr, v20, v19, v3);
puts(s);
}
if ( ptr )
free(ptr);
if ( s )
free(s);
exit(0);
}
func10(&ptr, dest);
goto LABEL_23;
}
if ( v9 == 97 )
func7();
if ( v9 != 99 )
break;
v3 = 1;
}
v10 = 10LL;
if ( v9 != 100 )
break;
LABEL_12:
v19 = 1;
v20 = strtoul(optarg, 0LL, v10);
}
switch ( v9 )
{
case 'f':
v8 = (const char **)optarg;
strcpy(dest, optarg);
v4 |= v5;
if ( v4 )
goto LABEL_37;
v5 = 0;
v6 = 1;
break;
case 'h':
goto LABEL_30;
case 'p':
v6 |= v5;
if ( v6 )
goto LABEL_37;
v5 = 0;
v4 = 1;
break;
case 't':
v4 |= v6;
if ( v4 )
LABEL_37:
func9("Flags -f, -p, and -t are mutually exclusive.", v8, v10);
v11 = optarg;
v5 = 1;
v12 = strlen(optarg);
v13 = v11;
v6 = 0;
ptr = calloc(v12, 1uLL);
strcpy((char *)ptr, v13);
break;
case 'x':
LODWORD(v10) = 16;
goto LABEL_12;
}
}
}

original_code:
int main(int argc, char **argv){
  char c, opt, filename[256], *buf = ((void *)0), *input = ((void *)0), *output = ((void *)0);
  int i, count, cflag = 0, delimflag = 0, pflag = 0, fflag = 0, tflag = 0;
  unsigned long int delim = 0;
  while((opt = getopt(argc, argv, "acd:f:hpt:x:")) != -1){
    if(opt == 'a')
      about();
    else if(opt == 'c')
      cflag = 1;
    else if(opt == 'd')
    {
      delimflag = 1;
      delim = strtoul(optarg, ((void *)0), 10);
    }
    else if(opt == 'f')
    {
      fflag = 1;
      strcpy(filename, optarg);
      if(pflag || tflag)
        handle_error("Flags -f, -p, and -t are mutually exclusive.");
    }
    else if(opt == 'h'){
      usage();
    }
    else if(opt == 'p')
    {
      pflag = 1;
      if(fflag || tflag)
        handle_error("Flags -f, -p, and -t are mutually exclusive.");
    }
    else if(opt == 't')
    {
      tflag = 1;
      if(fflag || pflag)
        handle_error("Flags -f, -p, and -t are mutually exclusive.");
      input = (char*) calloc(strlen(optarg), sizeof(char));
      strcpy(input, optarg);
    }
    else if(opt == 'x')
    {
      delimflag = 1;
      delim = strtoul(optarg, ((void *)0), 16);
    }
  }
  if(fflag)
    read_file(&input, filename);
  else if(pflag)
  {
    i = 0;
    count = 0;
    buf = (char*) calloc(8192 + 1, sizeof(char));
    input = calloc(1, sizeof(char));
    while((c = fgetc(stdin)) != (-1))
    {
      buf[i] = c;
      i = (i + 1) % 8192;
      if(!i)
      {
        input = (char*) realloc(input, ((++count) * 8192 + 1) * sizeof(char));
        strcat(input, buf);
      }
    }
    buf[i] = '\0';
    input = (char*) realloc(input, (count * 8192 + strlen(buf) + 1) * sizeof(char));
    strcat(input, buf);
    input[strlen(input) - 1] = '\0';
  }
  else if(!tflag)
    usage();
  compute_output(&output, input, delim, delimflag, cflag);
  printf("%s\n", output);
  if(buf != ((void *)0))
    free(buf);
  if(input != ((void *)0))
    free(input);
  if(output != ((void *)0))
    free(output);
  exit(0);
}

canonical_original_code:
int main(int argc, char **argv){
  char c; char opt; char filename[256]; char *buf = ((void *)0); char *input = ((void *)0); char *output = ((void *)0);
  int i; int count; int cflag = 0; int delimflag = 0; int pflag = 0; int fflag = 0; int tflag = 0;
  unsigned long delim = 0;
  while((opt = getopt(argc, argv, "acd:f:hpt:x:")) != -1){
    if(opt == 'a')
      about();
    else if(opt == 'c')
      cflag = 1;
    else if(opt == 'd')
    {
      delimflag = 1;
      delim = strtoul(optarg, ((void *)0), 10);
    }
    else if(opt == 'f')
    {
      fflag = 1;
      strcpy(filename, optarg);
      if(pflag || tflag)
        handle_error("Flags -f, -p, and -t are mutually exclusive.");
    }
    else if(opt == 'h'){
      usage();
    }
    else if(opt == 'p')
    {
      pflag = 1;
      if(fflag || tflag)
        handle_error("Flags -f, -p, and -t are mutually exclusive.");
    }
    else if(opt == 't')
    {
      tflag = 1;
      if(fflag || pflag)
        handle_error("Flags -f, -p, and -t are mutually exclusive.");
      input = (char *) calloc(strlen(optarg), sizeof(char));
      strcpy(input, optarg);
    }
    else if(opt == 'x')
    {
      delimflag = 1;
      delim = strtoul(optarg, ((void *)0), 16);
    }
  }
  if(fflag)
    read_file(&input, filename);
  else if(pflag)
  {
    i = 0;
    count = 0;
    buf = (char *) calloc(8192 + 1, sizeof(char));
    input = calloc(1, sizeof(char));
    while((c = fgetc(stdin)) != (-1))
    {
      buf[i] = c;
      i = (i + 1) % 8192;
      if(!i)
      {
        input = (char *) realloc(input, ((++count) * 8192 + 1) * sizeof(char));
        strcat(input, buf);
      }
    }
    buf[i] = '\0';
    input = (char *) realloc(input, (count * 8192 + strlen(buf) + 1) * sizeof(char));
    strcat(input, buf);
    input[strlen(input) - 1] = '\0';
  }
  else if(!tflag)
    usage();
  compute_output(&output, input, delim, delimflag, cflag);
  printf("%s\n", output);
  if(buf != ((void *)0))
    free(buf);
  if(input != ((void *)0))
    free(input);
  if(output != ((void *)0))
    free(output);
  exit(0);
}

variable_types: c char

variable_types: opt char

variable_types:
filename
char [256]

variable_types:
buf
char *

variable_types:
input
char *

variable_types:
output
char *

variable_types: cflag int

variable_types: delimflag int

variable_types: pflag int

variable_types: fflag int

variable_types: tflag int

variable_types:
delim
unsigned long


about

decompiled_code:
void __noreturn about()
{
puts("SHASCII (Show Invisible ASCII) reads a text file and prints");
puts("out the same text withall the original ASCII control characters");
puts("shown explicitly. The outputshows control characters in C language");
puts("character escape code notation when possible:for example, ");
puts("the line feed character is shown, literally, as \\n. For control");
puts("characters with no C language escape notation, caret notation");
puts("is used. If the -c flag is set, caret notation is used for all ASCII");
puts("control characters. All non-control and non-ASCII characters are left alone.\n");
puts("See http://http://en.wikipedia.org/wiki/Ascii for an explanation of ASCII");
puts("control characters, ASCII decimal and hexidecimal codes, and ASCII iteslf.\n");
puts("SHASCII project page: https://github.com/wlandau/shascii.\n");
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
puts("SHASCII (Show Invisible ASCII) reads a text file and prints");
puts("out the same text withall the original ASCII control characters");
puts("shown explicitly. The outputshows control characters in C language");
puts("character escape code notation when possible:for example, ");
puts("the line feed character is shown, literally, as \\n. For control");
puts("characters with no C language escape notation, caret notation");
puts("is used. If the -c flag is set, caret notation is used for all ASCII");
puts("control characters. All non-control and non-ASCII characters are left alone.\n");
puts("See http://http://en.wikipedia.org/wiki/Ascii for an explanation of ASCII");
puts("control characters, ASCII decimal and hexidecimal codes, and ASCII iteslf.\n");
puts("SHASCII project page: https://github.com/wlandau/shascii.\n");
exit(0);
}

original_code:
void about(){
  printf("SHASCII (Show Invisible ASCII) reads a text file and prints\n");
  printf("out the same text withall the original ASCII control characters\n");
  printf("shown explicitly. The outputshows control characters in C language\n");
  printf("character escape code notation when possible:for example, \n");
  printf("the line feed character is shown, literally, as \\n. For control\n");
  printf("characters with no C language escape notation, caret notation\n");
  printf("is used. If the -c flag is set, caret notation is used for all ASCII\n");
  printf("control characters. All non-control and non-ASCII characters are left alone.\n\n");
  printf("See http://http://en.wikipedia.org/wiki/Ascii for an explanation of ASCII\n");
  printf("control characters, ASCII decimal and hexidecimal codes, and ASCII iteslf.\n\n");
  printf("SHASCII project page: https://github.com/wlandau/shascii.\n\n");
  exit(0);
}


usage

decompiled_code:
void __noreturn usage()
{
puts("\nUsage:");
puts(" shascii [flag_1] [arg_1] ... [flag_n] [arg_n] \n");
puts("Options:");
puts(" Flag \tArg \t Description");
puts(" -a \t About.");
puts(" -c \t Print out all ASCII control characters in caret notation.");
puts(" -d \t[dec_index] \t Break up output into lines by the ASCII character with ");
puts(" \t decimal code [dec_index].");
puts(" -f \t[file] \t Use contents of [file] as input.");
puts(" -h \t Usage.");
puts(" -p \t\t Pipe input from another program through stdin.");
puts(" -t \t[text] \t Use [text] as input.");
puts(" -x \t[hex_index] \t Break up output into lines by the ASCII character with ");
puts(" \t hexidecimal code [hex_index].\n");
exit(0);
}

canonical_decompiled_code:
void __noreturn func8()
{
puts("\nUsage:");
puts(" shascii [flag_1] [arg_1] ... [flag_n] [arg_n] \n");
puts("Options:");
puts(" Flag \tArg \t Description");
puts(" -a \t About.");
puts(" -c \t Print out all ASCII control characters in caret notation.");
puts(" -d \t[dec_index] \t Break up output into lines by the ASCII character with ");
puts(" \t decimal code [dec_index].");
puts(" -f \t[file] \t Use contents of [file] as input.");
puts(" -h \t Usage.");
puts(" -p \t\t Pipe input from another program through stdin.");
puts(" -t \t[text] \t Use [text] as input.");
puts(" -x \t[hex_index] \t Break up output into lines by the ASCII character with ");
puts(" \t hexidecimal code [hex_index].\n");
exit(0);
}

original_code:
void usage(){
  printf("\nUsage:\n");
  printf("  shascii [flag_1] [arg_1] ... [flag_n] [arg_n] \n\n");
  printf("Options:\n");
  printf("  Flag \tArg       \t Description\n");
  printf("  -a               \t About.\n");
  printf("  -c               \t Print out all ASCII control characters in caret notation.\n");
  printf("  -d \t[dec_index] \t Break up output into lines by the ASCII character with \n");
  printf("                 \t decimal code [dec_index].\n");
  printf("  -f \t[file]  \t Use contents of [file] as input.\n");
  printf("  -h               \t Usage.\n");
  printf("  -p         \t\t Pipe input from another program through stdin.\n");
  printf("  -t \t[text]     \t Use [text] as input.\n");
  printf("  -x \t[hex_index] \t Break up output into lines by the ASCII character with \n");
  printf("                 \t hexidecimal code [hex_index].\n\n");
  exit(0);
}


decompiled_code:
void __fastcall __noreturn handle_error(const char *a1)
{
fprintf(stderr, "OH NO! %s\nFor usage, enter: shascii -h\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
fprintf(stderr, "OH NO! %s\nFor usage, enter: shascii -h\n", a1);
exit(1);
}

original_code:
void handle_error(char* message){
  fprintf(stderr, "OH NO! %s\nFor usage, enter: shascii -h\n", message);
  exit(1);
}

canonical_original_code:
void handle_error(char *message){
  fprintf(stderr, "OH NO! %s\nFor usage, enter: shascii -h\n", message);
  exit(1);
}

variable_types:
message
char *


decompiled_code:
void __fastcall one_sub(char **a1, char a2, int a3)
{
char *v3; // rdi
bool v4; // zf
const char *v5; // rax
const char *v6; // rdx

v3 = *a1;
if ( a2 )
{
switch ( a2 )
{
case 1:
strcpy(v3, "^A");
return;
case 2:
strcpy(v3, "^B");
return;
case 3:
strcpy(v3, "^C");
return;
case 4:
strcpy(v3, "^D");
return;
case 5:
strcpy(v3, "^E");
return;
case 6:
strcpy(v3, "^F");
return;
case 7:
v4 = a3 == 0;
v5 = "^G";
v6 = "\\a";
break;
case 8:
v4 = a3 == 0;
v5 = "^H";
v6 = "\\b";
break;
case 9:
v4 = a3 == 0;
v5 = "^I";
v6 = "\\t";
break;
case 10:
v4 = a3 == 0;
v5 = "^J";
v6 = "\\n";
break;
case 11:
v4 = a3 == 0;
v5 = "^K";
v6 = "\\v";
break;
case 12:
v4 = a3 == 0;
v5 = "^L";
v6 = "\\f";
break;
case 13:
v4 = a3 == 0;
v5 = "^M";
v6 = (const char *)&unk_40264E;
break;
default:
switch ( a2 )
{
case 14:
strcpy(v3, "^N");
return;
case 15:
strcpy(v3, "^O");
return;
case 16:
strcpy(v3, "^P");
return;
case 17:
strcpy(v3, "^Q");
return;
case 18:
strcpy(v3, "^R");
return;
case 19:
strcpy(v3, "^S");
return;
case 20:
strcpy(v3, "^T");
return;
case 21:
strcpy(v3, "^U");
return;
case 22:
strcpy(v3, "^V");
return;
case 23:
strcpy(v3, "^W");
return;
case 24:
strcpy(v3, "^X");
return;
case 25:
strcpy(v3, "^Y");
return;
case 26:
strcpy(v3, "^Z");
return;
}
if ( a2 != 27 )
{
switch ( a2 )
{
case 28:
strcpy(v3, "^\\");
break;
case 29:
strcpy(v3, "^]");
break;
case 30:
strcpy(v3, "^^");
break;
case 31:
strcpy(v3, "^_");
break;
case 127:
strcpy(v3, "^?");
break;
default:
sprintf(v3, "%c", (unsigned int)a2);
break;
}
return;
}
v4 = a3 == 0;
v5 = "^]";
v6 = "\\e";
break;
}
}
else
{
v4 = a3 == 0;
v5 = "^@";
v6 = "\\0";
}
if ( v4 )
v5 = v6;
*(_WORD *)v3 = *(_WORD *)v5;
v3[2] = v5[2];
}

canonical_decompiled_code:
void __fastcall func11(char **a1, char a2, int a3)
{
char *v3; // rdi
bool v4; // zf
const char *v5; // rax
const char *v6; // rdx

v3 = *a1;
if ( a2 )
{
switch ( a2 )
{
case 1:
strcpy(v3, "^A");
return;
case 2:
strcpy(v3, "^B");
return;
case 3:
strcpy(v3, "^C");
return;
case 4:
strcpy(v3, "^D");
return;
case 5:
strcpy(v3, "^E");
return;
case 6:
strcpy(v3, "^F");
return;
case 7:
v4 = a3 == 0;
v5 = "^G";
v6 = "\\a";
break;
case 8:
v4 = a3 == 0;
v5 = "^H";
v6 = "\\b";
break;
case 9:
v4 = a3 == 0;
v5 = "^I";
v6 = "\\t";
break;
case 10:
v4 = a3 == 0;
v5 = "^J";
v6 = "\\n";
break;
case 11:
v4 = a3 == 0;
v5 = "^K";
v6 = "\\v";
break;
case 12:
v4 = a3 == 0;
v5 = "^L";
v6 = "\\f";
break;
case 13:
v4 = a3 == 0;
v5 = "^M";
v6 = (const char *)&unk_40264E;
break;
default:
switch ( a2 )
{
case 14:
strcpy(v3, "^N");
return;
case 15:
strcpy(v3, "^O");
return;
case 16:
strcpy(v3, "^P");
return;
case 17:
strcpy(v3, "^Q");
return;
case 18:
strcpy(v3, "^R");
return;
case 19:
strcpy(v3, "^S");
return;
case 20:
strcpy(v3, "^T");
return;
case 21:
strcpy(v3, "^U");
return;
case 22:
strcpy(v3, "^V");
return;
case 23:
strcpy(v3, "^W");
return;
case 24:
strcpy(v3, "^X");
return;
case 25:
strcpy(v3, "^Y");
return;
case 26:
strcpy(v3, "^Z");
return;
}
if ( a2 != 27 )
{
switch ( a2 )
{
case 28:
strcpy(v3, "^\\");
break;
case 29:
strcpy(v3, "^]");
break;
case 30:
strcpy(v3, "^^");
break;
case 31:
strcpy(v3, "^_");
break;
case 127:
strcpy(v3, "^?");
break;
default:
sprintf(v3, "%c", (unsigned int)a2);
break;
}
return;
}
v4 = a3 == 0;
v5 = "^]";
v6 = "\\e";
break;
}
}
else
{
v4 = a3 == 0;
v5 = "^@";
v6 = "\\0";
}
if ( v4 )
v5 = v6;
*(_WORD *)v3 = *(_WORD *)v5;
v3[2] = v5[2];
}

original_code:
void one_sub(char **buf, char c, int cflag){
  if(c == 0x0)
    strcpy(*buf, cflag ? "^@" : "\\0");
  else if(c == 0x1)
    strcpy(*buf, "^A");
  else if(c == 0x2)
    strcpy(*buf, "^B");
  else if(c == 0x3)
    strcpy(*buf, "^C");
  else if(c == 0x4)
    strcpy(*buf, "^D");
  else if(c == 0x5)
    strcpy(*buf, "^E");
  else if(c == 0x6)
    strcpy(*buf, "^F");
  else if(c == 0x7)
    strcpy(*buf, cflag ? "^G" : "\\a");
  else if(c == 0x8)
    strcpy(*buf, cflag ? "^H" : "\\b");
  else if(c == 0x9)
    strcpy(*buf, cflag ? "^I" : "\\t");
  else if(c == 0xa)
    strcpy(*buf, cflag ? "^J" : "\\n");
  else if(c == 0xb)
    strcpy(*buf, cflag ? "^K" : "\\v");
  else if(c == 0xc)
    strcpy(*buf, cflag ? "^L" : "\\f");
  else if(c == 0xd)
    strcpy(*buf, cflag ? "^M" : "\\r");
  else if(c == 0xe)
    strcpy(*buf, "^N");
  else if(c == 0xf)
    strcpy(*buf, "^O");
  else if(c == 0x10)
    strcpy(*buf, "^P");
  else if(c == 0x11)
    strcpy(*buf, "^Q");
  else if(c == 0x12)
    strcpy(*buf, "^R");
  else if(c == 0x13)
    strcpy(*buf, "^S");
  else if(c == 0x14)
    strcpy(*buf, "^T");
  else if(c == 0x15)
    strcpy(*buf, "^U");
  else if(c == 0x16)
    strcpy(*buf, "^V");
  else if(c == 0x17)
    strcpy(*buf, "^W");
  else if(c == 0x18)
    strcpy(*buf, "^X");
  else if(c == 0x19)
    strcpy(*buf, "^Y");
  else if(c == 0x1a)
    strcpy(*buf, "^Z");
  else if(c == 0x1b)
    strcpy(*buf, cflag ? "^]" : "\\e");
  else if(c == 0x1c)
    strcpy(*buf, "^\\");
  else if(c == 0x1d)
    strcpy(*buf, "^]");
  else if(c == 0x1e)
    strcpy(*buf, "^^");
  else if(c == 0x1f)
    strcpy(*buf, "^_");
  else if(c == 0x7f)
    strcpy(*buf, "^?");
  else
    sprintf(*buf, "%c", c);
}

variable_types:
buf
char **


original_code:
void compute_output(char **output, char *input, unsigned long int delim,
                 int delimflag, int cflag){
  int i, j, n, buflen = 3;
  char *buf = ((void *)0);
  n = strlen(input);
  *output = (char*) calloc((2*n + 1), sizeof(char));
  buf = (char*) calloc(buflen, sizeof(char));
  for(i = 0; i < n; ++i){
    one_sub(&buf, input[i], cflag);
    strcat(*output, buf);
    for(j = 0; j < buflen; ++j)
      buf[j] = '\0';
    if(delimflag)
      if(delim == input[i])
        strcat(*output, "\n");
  }
  free(buf);
}

canonical_original_code:
void compute_output(char **output, char *input, unsigned long delim, int delimflag, int cflag){
  int i; int j; int n; int buflen = 3;
  char *buf = ((void *)0);
  n = strlen(input);
  *output = (char *) calloc((2*n + 1), sizeof(char));
  buf = (char *) calloc(buflen, sizeof(char));
  for(i = 0; i < n; ++i){
    one_sub(&buf, input[i], cflag);
    strcat(*output, buf);
    for(j = 0; j < buflen; ++j)
      buf[j] = '\0';
    if(delimflag)
      if(delim == input[i])
        strcat(*output, "\n");
  }
  free(buf);
}

variable_types:
output
char **

variable_types: i int

variable_types: j int

variable_types: n int

variable_types: buflen int


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

__outbyte(0x3F9u, 0);
__outbyte(0x3FBu, 0x80u);
__outbyte(0x3F8u, 3u);
__outbyte(0x3F9u, 0);
__outbyte(0x3FBu, 3u);
__outbyte(0x3FAu, 0xC7u);
result = 8LL;
__outbyte(0x3FCu, 8u);
return result;
}

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

__outbyte(0x3F9u, 0);
__outbyte(0x3FBu, 0x80u);
__outbyte(0x3F8u, 3u);
__outbyte(0x3F9u, 0);
__outbyte(0x3FBu, 3u);
__outbyte(0x3FAu, 0xC7u);
result = 8LL;
__outbyte(0x3FCu, 8u);
return result;
}


decompiled_code:
__int64 serial_transmit_empty()
{
unsigned __int8 v0; // al

v0 = __inbyte(0x3FDu);
return v0 & 0x20;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned __int8 v0; // al

v0 = __inbyte(0x3FDu);
return v0 & 0x20;
}

original_code:
int serial_transmit_empty()
{
 return _inb(0x3F8 + 5) & 0x20;
}


decompiled_code:
__int64 __fastcall serial_char(unsigned int a1)
{
unsigned int (*v1)(void); // rcx
__int64 result; // rax

v1 = (unsigned int (*)(void))serial_transmit_empty;
while ( !v1() )
;
result = a1;
__outbyte(0x3F8u, a1);
return result;
}

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

v1 = (unsigned int (*)(void))serial_transmit_empty;
while ( !v1() )
;
result = a1;
__outbyte(0x3F8u, a1);
return result;
}

original_code:
void serial_char(char c)
{
 while (serial_transmit_empty() == 0);
 _outb(0x3F8, c);
}

variable_types: c char


decompiled_code:
_WORD *init_text_mode()
{
_WORD *result; // rax

write_y = 0;
result = &write_x;
write_x = 0;
return result;
}

canonical_decompiled_code:
_WORD *func0()
{
_WORD *result; // rax

write_y = 0;
result = &write_x;
write_x = 0;
return result;
}

original_code:
void init_text_mode(void)
{
 write_x = write_y = 0;
}

canonical_original_code:
void init_text_mode()
{
 write_x = write_y = 0;
}


decompiled_code:
_WORD *text_mode_reset()
{
return init_text_mode();
}

canonical_decompiled_code:
_WORD *func1()
{
return func0();
}

original_code:
void text_mode_reset()
{
 write_x = write_y = 0;
}


decompiled_code:
unsigned __int64 display_page()
{
unsigned __int64 v0; // rsi
unsigned int v1; // er8
unsigned __int64 result; // rax
unsigned int v3; // edx
__int64 v4; // rcx

v0 = 0xFFFF8000000B80A0LL;
clear_screen();
do
{
result = v0 - 160;
v3 = v1;
do
{
v4 = v3;
*(_BYTE *)(result + 1) = 9;
result += 2LL;
++v3;
*(_BYTE *)(result - 2) = text[v4];
}
while ( result != v0 );
v0 = result + 160;
v1 += 80;
}
while ( result != 0xFFFF8000000B8FA0LL );
return result;
}

canonical_decompiled_code:
unsigned __int64 func3()
{
unsigned __int64 v0; // rsi
unsigned int v1; // er8
unsigned __int64 result; // rax
unsigned int v3; // edx
__int64 v4; // rcx

v0 = 0xFFFF8000000B80A0LL;
func2();
do
{
result = v0 - 160;
v3 = v1;
do
{
v4 = v3;
*(_BYTE *)(result + 1) = 9;
result += 2LL;
++v3;
*(_BYTE *)(result - 2) = text[v4];
}
while ( result != v0 );
v0 = result + 160;
v1 += 80;
}
while ( result != 0xFFFF8000000B8FA0LL );
return result;
}

original_code:
static void display_page(uint32_t page_offset)
{
 clear_screen();
 uint32_t text_y = page_offset*25;
 for (uint32_t disp_y = 0; disp_y < 25; ++disp_y, ++text_y)
 {
  for (uint32_t disp_x = 0; disp_x < 80; ++disp_x)
  {
   const uint32_t text_offset = text_y*80 + disp_x;
   const uint32_t offset = disp_y*80 + disp_x;
   video[offset].c = text[text_offset];
   video[offset].color = 0x9;
  }
 }
}

canonical_original_code:
static void display_page(unsigned int page_offset)
{
 clear_screen();
 unsigned int text_y = page_offset*25;
 for (unsigned int disp_y = 0; disp_y < 25; ++disp_y, ++text_y)
 {
  for (unsigned int disp_x = 0; disp_x < 80; ++disp_x)
  {
   unsigned int text_offset = text_y*80 + disp_x;
   unsigned int offset = disp_y*80 + disp_x;
   video[offset].c = text[text_offset];
   video[offset].color = 0x9;
  }
 }
}

variable_types:
offset
unsigned int


decompiled_code:
unsigned __int64 page_up()
{
if ( page_offset )
--page_offset;
return display_page();
}

canonical_decompiled_code:
unsigned __int64 func4()
{
if ( page_offset )
--page_offset;
return func3();
}

original_code:
void page_up()
{
 if (page_offset > 0)
 {
  --page_offset;
 }
 display_page(page_offset);
}


decompiled_code:
unsigned __int64 page_down()
{
if ( page_offset < (unsigned __int16)write_y / 0x19u )
++page_offset;
return display_page();
}

canonical_decompiled_code:
unsigned __int64 func5()
{
if ( page_offset < (unsigned __int16)write_y / 0x19u )
++page_offset;
return func3();
}

original_code:
void page_down()
{
 const uint32_t max_page_offset = write_y / 25;
 if (page_offset < max_page_offset)
 {
  ++page_offset;
 }
 display_page(page_offset);
}

canonical_original_code:
void page_down()
{
 unsigned int max_page_offset = write_y / 25;
 if (page_offset < max_page_offset)
 {
  ++page_offset;
 }
 display_page(page_offset);
}


decompiled_code:
unsigned __int64 clear_screen()
{
unsigned __int64 result; // rax

for ( result = 0xFFFF8000000B8000LL; result != 0xFFFF8000000B8FA0LL; *(_WORD *)(result - 2) = 0 )
result += 2LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 func2()
{
unsigned __int64 result; // rax

for ( result = 0xFFFF8000000B8000LL; result != 0xFFFF8000000B8FA0LL; *(_WORD *)(result - 2) = 0 )
result += 2LL;
return result;
}

canonical_original_code:
void clear_screen()
{
 unsigned short *video = (uint16_t*)(0xB8000+0xFFFF800000000000);
 for (int i = 0; i < 80*25; ++i)
 {
  video[i] = 0;
 }
}

variable_types:
video
unsigned short *


decompiled_code:
__int64 __fastcall text_mode_char(char a1)
{
__int16 *v1; // r9
int v3; // edi
unsigned __int16 v4; // si
int v5; // er10
unsigned __int16 v6; // dx
__int64 result; // rax
__int16 v8; // si
int v9; // ecx
__int16 v10; // ax
__int16 v11; // si

v1 = &write_y;
v3 = (unsigned __int16)write_y;
v4 = write_y;
if ( page_offset != (unsigned __int16)write_y / 0x19u )
{
page_offset = (unsigned __int16)write_y / 0x19u;
clear_screen();
}
if ( a1 == 10 || a1 == 13 )
{
v11 = v4 + 1;
if ( (unsigned __int16)v11 > 0x9C3u )
{
write_y = 0;
result = (__int64)&write_x;
}
else
{
result = (__int64)&write_x;
*v1 = v11;
}
write_x = 0;
}
else if ( a1 == 8 )
{
if ( write_x )
{
v10 = write_x - 1;
v9 = (unsigned __int16)(write_x - 1);
}
else if ( v4 )
{
--v4;
v9 = 79;
v10 = 79;
*v1 = v4;
v3 = v4;
}
else
{
v9 = 79;
v10 = 79;
}
write_x = v10;
text[80 * v3 + v9] = 32;
result = v9
+ 80
* (unsigned __int16)(v4
- 25
* ((unsigned __int16)(((unsigned int)(18351 * v3) >> 16)
+ ((unsigned __int16)(v4 - ((unsigned int)(18351 * v3) >> 16)) >> 1)) >> 4));
*(_WORD *)(2 * result - 0x7FFFFFF48000LL) = 2336;
}
else
{
v5 = (unsigned __int16)write_x;
v6 = write_x + 1;
text[80 * v3 + (unsigned __int16)write_x] = a1;
result = 2LL
* (v5
+ 80
* (unsigned __int16)(v4
- 25
* ((unsigned __int16)(((unsigned __int16)(v4 - ((unsigned int)(18351 * v3) >> 16)) >> 1)
+ ((unsigned int)(18351 * v3) >> 16)) >> 4)))
- 0x7FFFFFF48000LL;
*(_BYTE *)result = a1;
*(_BYTE *)(result + 1) = 9;
if ( v6 <= 0x4Fu )
{
write_x = v6;
}
else
{
result = (__int64)&write_x;
v8 = v4 + 1;
write_x = 0;
if ( (unsigned __int16)v8 > 0x9C3u )
{
result = (__int64)&write_y;
write_y = 0;
}
else
{
*v1 = v8;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char a1)
{
__int16 *v1; // r9
int v3; // edi
unsigned __int16 v4; // si
int v5; // er10
unsigned __int16 v6; // dx
__int64 result; // rax
__int16 v8; // si
int v9; // ecx
__int16 v10; // ax
__int16 v11; // si

v1 = &write_y;
v3 = (unsigned __int16)write_y;
v4 = write_y;
if ( page_offset != (unsigned __int16)write_y / 0x19u )
{
page_offset = (unsigned __int16)write_y / 0x19u;
func2();
}
if ( a1 == 10 || a1 == 13 )
{
v11 = v4 + 1;
if ( (unsigned __int16)v11 > 0x9C3u )
{
write_y = 0;
result = (__int64)&write_x;
}
else
{
result = (__int64)&write_x;
*v1 = v11;
}
write_x = 0;
}
else if ( a1 == 8 )
{
if ( write_x )
{
v10 = write_x - 1;
v9 = (unsigned __int16)(write_x - 1);
}
else if ( v4 )
{
--v4;
v9 = 79;
v10 = 79;
*v1 = v4;
v3 = v4;
}
else
{
v9 = 79;
v10 = 79;
}
write_x = v10;
text[80 * v3 + v9] = 32;
result = v9
+ 80
* (unsigned __int16)(v4
- 25
* ((unsigned __int16)(((unsigned int)(18351 * v3) >> 16)
+ ((unsigned __int16)(v4 - ((unsigned int)(18351 * v3) >> 16)) >> 1)) >> 4));
*(_WORD *)(2 * result - 0x7FFFFFF48000LL) = 2336;
}
else
{
v5 = (unsigned __int16)write_x;
v6 = write_x + 1;
text[80 * v3 + (unsigned __int16)write_x] = a1;
result = 2LL
* (v5
+ 80
* (unsigned __int16)(v4
- 25
* ((unsigned __int16)(((unsigned __int16)(v4 - ((unsigned int)(18351 * v3) >> 16)) >> 1)
+ ((unsigned int)(18351 * v3) >> 16)) >> 4)))
- 0x7FFFFFF48000LL;
*(_BYTE *)result = a1;
*(_BYTE *)(result + 1) = 9;
if ( v6 <= 0x4Fu )
{
write_x = v6;
}
else
{
result = (__int64)&write_x;
v8 = v4 + 1;
write_x = 0;
if ( (unsigned __int16)v8 > 0x9C3u )
{
result = (__int64)&write_y;
write_y = 0;
}
else
{
*v1 = v8;
}
}
}
return result;
}

original_code:
void text_mode_char(char c)
{
 const uint32_t current_page = write_y / 25;
 if (current_page != page_offset)
 {
  page_offset = current_page;
  clear_screen();
 }
 switch (c)
 {
  case '\r':
  case '\n':
   {
    ++write_y;
    if (write_y >= (25*(100)))
    {
     write_y = 0;
    }
    write_x = 0;
   }
   break;
  case '\b':
   {
    if (write_x == 0)
    {
     write_x = 80;
     if (write_y > 0)
     {
      --write_y;
     }
    }
    --write_x;
    text[write_y*80 + write_x] = ' ';
    const uint32_t offset = (write_y%25)*80 + write_x;
    video[offset].c = ' ';
    video[offset].color = 0x9;
   }
   break;
  default:
   {
    text[write_y*80 + write_x] = c;
    const uint32_t offset = (write_y%25)*80 + write_x;
    video[offset].c = c;
    video[offset].color = 0x9;
    ++write_x;
    if (write_x >= 80)
    {
     write_x = 0;
     ++write_y;
     if (write_y >= (25*(100)))
     {
      write_y = 0;
     }
    }
   }
   break;
 }
}

canonical_original_code:
void text_mode_char(char c)
{
 unsigned int current_page = write_y / 25;
 if (current_page != page_offset)
 {
  page_offset = current_page;
  clear_screen();
 }
 switch (c)
 {
  case '\r':
  case '\n':
   {
    ++write_y;
    if (write_y >= (25*(100)))
    {
     write_y = 0;
    }
    write_x = 0;
   }
   break;
  case '\b':
   {
    if (write_x == 0)
    {
     write_x = 80;
     if (write_y > 0)
     {
      --write_y;
     }
    }
    --write_x;
    text[write_y*80 + write_x] = ' ';
    unsigned int offset = (write_y%25)*80 + write_x;
    video[offset].c = ' ';
    video[offset].color = 0x9;
   }
   break;
  default:
   {
    text[write_y*80 + write_x] = c;
    unsigned int offset = (write_y%25)*80 + write_x;
    video[offset].c = c;
    video[offset].color = 0x9;
    ++write_x;
    if (write_x >= 80)
    {
     write_x = 0;
     ++write_y;
     if (write_y >= (25*(100)))
     {
      write_y = 0;
     }
    }
   }
   break;
 }
}