decompiled_code:
__int64 __fastcall files_get_dsp_name(unsigned __int8 a1)
{
return list_get_name((__int64)&dspList, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
return func0((__int64)&dspList, a1);
}

original_code:
const volatile char* files_get_dsp_name(u8 idx) {
  return list_get_name(&dspList, idx);
}

canonical_original_code:
char *files_get_dsp_name(unsigned char idx) {
  return list_get_name(&dspList, idx);
}


repo: tehn/aleph

decompiled_code:
__int64 __fastcall files_load_dsp(unsigned __int8 a1)
{
char *v1; // rax

v1 = (char *)files_get_dsp_name(a1);
return files_load_dsp_name(v1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1)
{
char *v1; // rax

v1 = (char *)func4(a1);
return func5(v1);
}

original_code:
u8 files_load_dsp(u8 idx) {
  return files_load_dsp_name((const char*)files_get_dsp_name(idx));
}

canonical_original_code:
unsigned char files_load_dsp(unsigned char idx) {
  return files_load_dsp_name((char *)files_get_dsp_name(idx));
}

variable_types:
idx
unsigned char

return_type: unsigned char


decompiled_code:
__int64 files_get_dsp_count()
{
return (unsigned __int8)byte_10A0;
}

canonical_decompiled_code:
__int64 func7()
{
return (unsigned __int8)byte_10A0;
}

original_code:
u8 files_get_dsp_count(void) {
  return dspList.num;
}

canonical_original_code:
unsigned char files_get_dsp_count() {
  return dspList.num;
}


decompiled_code:
__int64 __fastcall files_get_scene_name(unsigned __int8 a1)
{
return list_get_name((__int64)&sceneList, a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 a1)
{
return func0((__int64)&sceneList, a1);
}

original_code:
const volatile char* files_get_scene_name(u8 idx) {
  return list_get_name(&sceneList, idx);
}

canonical_original_code:
char *files_get_scene_name(unsigned char idx) {
  return list_get_name(&sceneList, idx);
}

return_type: char *


decompiled_code:
__int64 __fastcall files_load_scene(unsigned __int8 a1)
{
char *v1; // rax

v1 = (char *)files_get_scene_name(a1);
return files_load_scene_name(v1);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
char *v1; // rax

v1 = (char *)func8(a1);
return func9(v1);
}

original_code:
u8 files_load_scene(u8 idx) {
  return files_load_scene_name((const char*)files_get_scene_name(idx));
}

canonical_original_code:
unsigned char files_load_scene(unsigned char idx) {
  return files_load_scene_name((char *)files_get_scene_name(idx));
}


decompiled_code:
int __fastcall files_store_scene(unsigned __int8 a1)
{
const char *v1; // rax

v1 = (const char *)files_get_scene_name(a1);
return files_store_scene_name(v1);
}

canonical_decompiled_code:
int __fastcall func12(unsigned __int8 a1)
{
const char *v1; // rax

v1 = (const char *)func8(a1);
return func11(v1);
}

original_code:
void files_store_scene(u8 idx) {
  files_store_scene_name((const char*)files_get_scene_name(idx));
}

canonical_original_code:
void files_store_scene(unsigned char idx) {
  files_store_scene_name((char *)files_get_scene_name(idx));
}

return_type: void


decompiled_code:
__int64 files_get_scene_count()
{
return (unsigned __int8)byte_C40;
}

canonical_decompiled_code:
__int64 func13()
{
return (unsigned __int8)byte_C40;
}

original_code:
u8 files_get_scene_count(void) {
  return sceneList.num;
}

canonical_original_code:
unsigned char files_get_scene_count() {
  return sceneList.num;
}


decompiled_code:
__int64 __fastcall files_get_scaler_name(unsigned __int8 a1)
{
return list_get_name((__int64)&scalerList, a1);
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int8 a1)
{
return func0((__int64)&scalerList, a1);
}

original_code:
const volatile char* files_get_scaler_name(u8 idx) {
  return list_get_name(&scalerList, idx);
}

canonical_original_code:
char *files_get_scaler_name(unsigned char idx) {
  return list_get_name(&scalerList, idx);
}


decompiled_code:
__int64 files_load_scaler_name()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func15()
{
return 0LL;
}

original_code:
u8 files_load_scaler_name(const char* name, s32* dst, u32 dstSize) {
  return 0;
}

canonical_original_code:
unsigned char files_load_scaler_name(char *name, int *dst, unsigned int dstSize) {
  return 0;
}

variable_types:
name
char *

variable_types:
dst
int *

variable_types:
dstSize
unsigned int


decompiled_code:
char __fastcall strip_filename(__int64 a1, unsigned __int8 a2)
{
__int64 i; // rax
char v3; // dl

LOBYTE(i) = a2 - 1;
if ( a2 != 1 )
{
for ( i = (unsigned __int8)i; (_BYTE)i; --i )
{
v3 = *(_BYTE *)(a1 + i);
if ( v3 )
{
if ( v3 != 47 )
return i;
if ( a2 - 1 > (int)i )
*(_BYTE *)(a1 + i + 1) = 0;
}
}
}
return i;
}

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

LOBYTE(i) = a2 - 1;
if ( a2 != 1 )
{
for ( i = (unsigned __int8)i; (_BYTE)i; --i )
{
v3 = *(_BYTE *)(a1 + i);
if ( v3 )
{
if ( v3 != 47 )
return i;
if ( a2 - 1 > (int)i )
*(_BYTE *)(a1 + i + 1) = 0;
}
}
}
return i;
}

original_code:
void strip_filename(char* str, u8 len) {
  u8 i;
  for( i=(len-1); i>0; i-- ) {
    if(str[i] == 0) { continue; }
    else if(str[i] == '/') {
  if(i<(len-1)) {
   str[i+1] = 0;
  }
 }
    else { break; }
  }
}

canonical_original_code:
void strip_filename(char *str, unsigned char len) {
  unsigned char i;
  for( i=(len-1); i>0; i-- ) {
    if(str[i] == 0) { continue; }
    else if(str[i] == '/') {
  if(i<(len-1)) {
   str[i+1] = 0;
  }
 }
    else { break; }
  }
}

variable_types:
str
char *

variable_types:
len
unsigned char

variable_types:
i
unsigned char


decompiled_code:
char *__fastcall scan_ext(const char *a1, char *a2)
{
unsigned int v2; // ecx
const char *v3; // rdx
char *result; // rax

v2 = strlen(a1);
v3 = &a1[v2 - 1];
result = (char *)v2;
while ( (int)result > 0 )
{
result = (char *)(unsigned int)((_DWORD)result - 1);
if ( *v3-- == 46 )
return strncpy(a2, &a1[(int)result], (int)(v2 - (_DWORD)result + 1));
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(const char *a1, char *a2)
{
unsigned int v2; // ecx
const char *v3; // rdx
char *result; // rax

v2 = strlen(a1);
v3 = &a1[v2 - 1];
result = (char *)v2;
while ( (int)result > 0 )
{
result = (char *)(unsigned int)((_DWORD)result - 1);
if ( *v3-- == 46 )
return strncpy(a2, &a1[(int)result], (int)(v2 - (_DWORD)result + 1));
}
return result;
}

original_code:
void scan_ext(char* filename, char* ext) {
  int len;
  int i;
  int dotpos = -1;
  len = strlen(filename);
  i = len;
  while(i > 0) {
    --i;
    if(filename[i] == '.') {
      dotpos = i;
      break;
    }
  }
  if(dotpos >= 0) {
    strncpy(ext, filename + dotpos, len - i + 1);
  }
}

canonical_original_code:
void scan_ext(char *filename, char *ext) {
  int len;
  int i;
  int dotpos = -1;
  len = strlen(filename);
  i = len;
  while(i > 0) {
    --i;
    if(filename[i] == '.') {
      dotpos = i;
      break;
    }
  }
  if(dotpos >= 0) {
    strncpy(ext, filename + dotpos, len - i + 1);
  }
}

variable_types:
filename
char *

variable_types:
ext
char *

variable_types: len int

variable_types: i int

variable_types: dotpos int


decompiled_code:
__int64 write_scn()
{
const char *v0; // rax
__int64 v2[17]; // [rsp+0h] [rbp-88h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 0LL;
v2[5] = 0LL;
v2[6] = 0LL;
v2[7] = 0LL;
strcpy((char *)v2, workingDir);
v0 = (const char *)scene_get_name();
strcat((char *)v2, v0);
strip_space((__int64)v2, 64);
strcat((char *)v2, ".scn");
printf("\r\n writing .scn binary; name: %s", (const char *)v2);
return files_store_scene_name(v2);
}

canonical_decompiled_code:
__int64 func3()
{
const char *v0; // rax
__int64 v2[17]; // [rsp+0h] [rbp-88h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 0LL;
v2[5] = 0LL;
v2[6] = 0LL;
v2[7] = 0LL;
strcpy((char *)v2, workingDir);
v0 = (const char *)scene_get_name();
strcat((char *)v2, v0);
func0((__int64)v2, 64);
strcat((char *)v2, ".scn");
printf("\r\n writing .scn binary; name: %s", (const char *)v2);
return files_store_scene_name(v2);
}

original_code:
void write_scn(void) {
  char str[128];
  memset(str, '\0', 64);
  strcpy(str, workingDir);
  strcat( str, scene_get_name());
  strip_space(str, 64);
  strcat(str, ".scn");
  printf("\r\n writing .scn binary; name: %s", str);
  files_store_scene_name(str);
}

canonical_original_code:
void write_scn() {
  char str[128];
  memset(str, '\0', 64);
  strcpy(str, workingDir);
  strcat( str, scene_get_name());
  strip_space(str, 64);
  strcat(str, ".scn");
  printf("\r\n writing .scn binary; name: %s", str);
  files_store_scene_name(str);
}

variable_types:
str
char [128]


decompiled_code:
int write_json()
{
const char *v0; // rax
__int64 v2[17]; // [rsp+0h] [rbp-88h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 0LL;
v2[5] = 0LL;
v2[6] = 0LL;
v2[7] = 0LL;
strcpy((char *)v2, workingDir);
v0 = (const char *)scene_get_name();
strcat((char *)v2, v0);
strip_space((__int64)v2, 64);
strcat((char *)v2, ".json");
printf("\r\n writing %s...", (const char *)v2);
net_write_json_native(v2);
return printf("\r\n done.");
}

canonical_decompiled_code:
int func4()
{
const char *v0; // rax
__int64 v2[17]; // [rsp+0h] [rbp-88h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v2[2] = 0LL;
v2[3] = 0LL;
v2[4] = 0LL;
v2[5] = 0LL;
v2[6] = 0LL;
v2[7] = 0LL;
strcpy((char *)v2, workingDir);
v0 = (const char *)scene_get_name();
strcat((char *)v2, v0);
func0((__int64)v2, 64);
strcat((char *)v2, ".json");
printf("\r\n writing %s...", (const char *)v2);
net_write_json_native(v2);
return printf("\r\n done.");
}

original_code:
void write_json(void) {
  char str[128];
  memset(str, '\0', 64);
  strcpy(str, workingDir);
  strcat( str, scene_get_name());
  strip_space(str, 64);
  strcat(str, ".json");
  printf("\r\n writing %s...", str);
  net_write_json_native(str);
  printf("\r\n done.");
}

canonical_original_code:
void write_json() {
  char str[128];
  memset(str, '\0', 64);
  strcpy(str, workingDir);
  strcat( str, scene_get_name());
  strip_space(str, 64);
  strcat(str, ".json");
  printf("\r\n writing %s...", str);
  net_write_json_native(str);
  printf("\r\n done.");
}


decompiled_code:
int write_gv()
{
const char *v0; // rax
FILE *v1; // rbx
__int64 v3[17]; // [rsp+0h] [rbp-88h] BYREF

v3[0] = 0LL;
v3[1] = 0LL;
v3[2] = 0LL;
v3[3] = 0LL;
v3[4] = 0LL;
v3[5] = 0LL;
v3[6] = 0LL;
v3[7] = 0LL;
strcpy((char *)v3, workingDir);
v0 = (const char *)scene_get_name();
strcat((char *)v3, v0);
strip_space((__int64)v3, 64);
*(_DWORD *)((char *)v3 + strlen((const char *)v3)) = 7759662;
v1 = fopen((const char *)v3, "w");
net_write_dot(v1);
return fclose(v1);
}

canonical_decompiled_code:
int func5()
{
const char *v0; // rax
FILE *v1; // rbx
__int64 v3[17]; // [rsp+0h] [rbp-88h] BYREF

v3[0] = 0LL;
v3[1] = 0LL;
v3[2] = 0LL;
v3[3] = 0LL;
v3[4] = 0LL;
v3[5] = 0LL;
v3[6] = 0LL;
v3[7] = 0LL;
strcpy((char *)v3, workingDir);
v0 = (const char *)scene_get_name();
strcat((char *)v3, v0);
func0((__int64)v3, 64);
*(_DWORD *)((char *)v3 + strlen((const char *)v3)) = 7759662;
v1 = fopen((const char *)v3, "w");
net_write_dot(v1);
return fclose(v1);
}

original_code:
void write_gv(void) {
  char str[128];
  void* fp;
  memset(str, '\0', 64);
  strcpy(str, workingDir);
  strcat( str, scene_get_name());
  strip_space(str, 64);
  strcat(str, ".gv");
  fp = fopen(str, "w");
  net_write_dot(fp);
  fclose(fp);
}

canonical_original_code:
void write_gv() {
  char str[128];
  void *fp;
  memset(str, '\0', 64);
  strcpy(str, workingDir);
  strcat( str, scene_get_name());
  strip_space(str, 64);
  strcat(str, ".gv");
  fp = fopen(str, "w");
  net_write_dot(fp);
  fclose(fp);
}

variable_types:
fp
void *


decompiled_code:
__int64 __fastcall fract32_create(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > a1 )
result = a1 % a2 * (0xFFFFFFFF / (a2 - 1));
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a2 > a1 )
result = a1 % a2 * (0xFFFFFFFF / (a2 - 1));
return result;
}

original_code:
fract32_t fract32_create(uint32_t inNumerator, uint32_t inDenominator) {
 if(inDenominator <= inNumerator)
  return 0xFFFFFFFF;
 uint32_t tempMod = (inNumerator % inDenominator);
 uint32_t tempDiv = (0xFFFFFFFF / (inDenominator - 1));
 return (tempMod * tempDiv);
}

canonical_original_code:
unsigned int fract32_create(unsigned int inNumerator, unsigned int inDenominator) {
 if(inDenominator <= inNumerator)
  return 0xFFFFFFFF;
 unsigned int tempMod = (inNumerator % inDenominator);
 unsigned int tempDiv = (0xFFFFFFFF / (inDenominator - 1));
 return (tempMod * tempDiv);
}

variable_types:
inNumerator
unsigned int

variable_types:
inDenominator
unsigned int

variable_types:
tempMod
unsigned int

variable_types:
tempDiv
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall fract32_invert(int a1)
{
return (unsigned int)~a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)~a1;
}

original_code:
fract32_t fract32_invert(fract32_t inFract) {
 return (0xFFFFFFFF - inFract);
}

canonical_original_code:
unsigned int fract32_invert(unsigned int inFract) {
 return (0xFFFFFFFF - inFract);
}

variable_types:
inFract
unsigned int


decompiled_code:
unsigned __int64 __fastcall fract32_usmul(unsigned int a1, unsigned int a2)
{
return (a2 * (unsigned __int64)a1) >> 32;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(unsigned int a1, unsigned int a2)
{
return (a2 * (unsigned __int64)a1) >> 32;
}

original_code:
uint32_t fract32_usmul(uint32_t inVal, fract32_t inFract) {
 return (uint32_t)(((uint64_t)inVal * (uint64_t)inFract) >> 32);
}

canonical_original_code:
unsigned int fract32_usmul(unsigned int inVal, unsigned int inFract) {
 return (unsigned int)(((unsigned long)inVal * (unsigned long)inFract) >> 32);
}

variable_types:
inVal
unsigned int


decompiled_code:
unsigned __int64 __fastcall fract32_smul(signed int a1, unsigned int a2)
{
unsigned __int64 result; // rax

if ( a1 < 0 )
result = (unsigned int)-(__int64)fract32_usmul(-a1, a2);
else
result = fract32_usmul(a1, a2);
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(signed int a1, unsigned int a2)
{
unsigned __int64 result; // rax

if ( a1 < 0 )
result = (unsigned int)-(__int64)func2(-a1, a2);
else
result = func2(a1, a2);
return result;
}

original_code:
int32_t fract32_smul(int32_t inVal, fract32_t inFract) {
 if(inVal < 0)
  return -fract32_usmul(-inVal, inFract);
 return fract32_usmul(inVal, inFract);
}

canonical_original_code:
int fract32_smul(int inVal, unsigned int inFract) {
 if(inVal < 0)
  return -fract32_usmul(-inVal, inFract);
 return fract32_usmul(inVal, inFract);
}

variable_types: inVal int

return_type: int


decompiled_code:
__int64 flash_read_scene()
{
return scene_read_buf();
}

canonical_decompiled_code:
__int64 func0()
{
return scene_read_buf();
}

original_code:
void flash_read_scene(void) {
  scene_read_buf();
}

canonical_original_code:
void flash_read_scene() {
  scene_read_buf();
}


decompiled_code:
__int64 flash_write_scene()
{
return scene_write_buf();
}

canonical_decompiled_code:
__int64 func1()
{
return scene_write_buf();
}

original_code:
void flash_write_scene(void) {
  scene_write_buf();
}

canonical_original_code:
void flash_write_scene() {
  scene_write_buf();
}


decompiled_code:
__int64 flash_init_scaler_data()
{
unsigned int v0; // eax
unsigned int v1; // eax
unsigned int i; // ebx
__int64 result; // rax
__int64 v4; // rbp
__int64 v5; // rbp

print_dbg("\r\n initializing NV data for param scaling, total bytes: ");
print_dbg_ulong(24576LL);
print_dbg("\r\n application data address in flash: 0x");
v0 = flash_app_data();
print_dbg_hex(v0);
print_dbg("\r\n scaler data address: 0x");
v1 = flash_app_data();
print_dbg_hex(v1);
flash_app_data();
for ( i = 0; i != 9; ++i )
{
if ( (unsigned int)scaler_get_data_bytes(i) )
{
v4 = scaler_get_data_path(i);
print_dbg("\r\n writing scaler data to flash: ");
print_dbg(v4);
files_load_scaler_name(v4, scalerBuf, 1024LL);
LODWORD(v4) = scaler_get_nv_data(i);
print_dbg("\r\n writing scaler val data to flash at address: 0x");
print_dbg_hex((unsigned int)v4);
}
result = scaler_get_rep_bytes(i);
if ( (_DWORD)result )
{
v5 = scaler_get_rep_path(i);
print_dbg("\r\n writing scaler data to flash: ");
print_dbg(v5);
files_load_scaler_name(v5, scalerBuf, 1024LL);
LODWORD(v5) = scaler_get_nv_rep(i);
print_dbg("\r\n writing scaler rep data to flash at address: 0x");
result = print_dbg_hex((unsigned int)v5);
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax
unsigned int v1; // eax
unsigned int i; // ebx
__int64 result; // rax
__int64 v4; // rbp
__int64 v5; // rbp

print_dbg("\r\n initializing NV data for param scaling, total bytes: ");
print_dbg_ulong(24576LL);
print_dbg("\r\n application data address in flash: 0x");
v0 = flash_app_data();
print_dbg_hex(v0);
print_dbg("\r\n scaler data address: 0x");
v1 = flash_app_data();
print_dbg_hex(v1);
flash_app_data();
for ( i = 0; i != 9; ++i )
{
if ( (unsigned int)scaler_get_data_bytes(i) )
{
v4 = scaler_get_data_path(i);
print_dbg("\r\n writing scaler data to flash: ");
print_dbg(v4);
files_load_scaler_name(v4, scalerBuf, 1024LL);
LODWORD(v4) = scaler_get_nv_data(i);
print_dbg("\r\n writing scaler val data to flash at address: 0x");
print_dbg_hex((unsigned int)v4);
}
result = scaler_get_rep_bytes(i);
if ( (_DWORD)result )
{
v5 = scaler_get_rep_path(i);
print_dbg("\r\n writing scaler data to flash: ");
print_dbg(v5);
files_load_scaler_name(v5, scalerBuf, 1024LL);
LODWORD(v5) = scaler_get_nv_rep(i);
print_dbg("\r\n writing scaler rep data to flash at address: 0x");
result = print_dbg_hex((unsigned int)v5);
}
}
return result;
}

original_code:
void flash_init_scaler_data(void) {
  u32 b;
  const char* path;
  u8 p;
  u8* dst;
  void* scalerBytes;
  print_dbg("\r\n initializing NV data for param scaling, total bytes: ");
  print_dbg_ulong(((1024 * 4 * 2) + 0 + 0 + 0 + (1024 * 4) + (1024 * 4) + (1024 * 4) + 0 + (1024 * 4) ));
  print_dbg("\r\n application data address in flash: 0x");
  print_dbg_hex((u32)flash_app_data());
  print_dbg("\r\n scaler data address: 0x");
  print_dbg_hex((u32) &(((beesFlashData*)(flash_app_data()))->scalerBytes) );
  scalerBytes = (void*)&(((beesFlashData*)(flash_app_data()))->scalerBytes);
  for(p=0; p<eParamNumTypes; ++p) {
    b = scaler_get_data_bytes(p);
    if(b > 0) {
      path = scaler_get_data_path(p);
      print_dbg("\r\n writing scaler data to flash: ");
      print_dbg(path);
      files_load_scaler_name(path, scalerBuf, scalerMaxValues);
      dst = (void*)scaler_get_nv_data(p);
      print_dbg("\r\n writing scaler val data to flash at address: 0x");
      print_dbg_hex((u32)dst);
    }
    b = scaler_get_rep_bytes(p);
    if(b > 0) {
      path = scaler_get_rep_path(p);
      print_dbg("\r\n writing scaler data to flash: ");
      print_dbg(path);
      files_load_scaler_name(path, scalerBuf, scalerMaxValues);
      dst = (void*)scaler_get_nv_rep(p);
      print_dbg("\r\n writing scaler rep data to flash at address: 0x");
      print_dbg_hex((u32)dst);
    }
  }
}

canonical_original_code:
void flash_init_scaler_data() {
  unsigned int b;
  char *path;
  unsigned char p;
  unsigned char *dst;
  void *scalerBytes;
  print_dbg("\r\n initializing NV data for param scaling, total bytes: ");
  print_dbg_ulong(((1024 * 4 * 2) + 0 + 0 + 0 + (1024 * 4) + (1024 * 4) + (1024 * 4) + 0 + (1024 * 4) ));
  print_dbg("\r\n application data address in flash: 0x");
  print_dbg_hex((unsigned int)flash_app_data());
  print_dbg("\r\n scaler data address: 0x");
  print_dbg_hex((u32) &(((struct beesFlashData *)(flash_app_data()))->scalerBytes) );
  scalerBytes = (void *)&(((struct beesFlashData *)(flash_app_data()))->scalerBytes);
  for(p=0; p<eParamNumTypes; ++p) {
    b = scaler_get_data_bytes(p);
    if(b > 0) {
      path = scaler_get_data_path(p);
      print_dbg("\r\n writing scaler data to flash: ");
      print_dbg(path);
      files_load_scaler_name(path, scalerBuf, scalerMaxValues);
      dst = (void *)scaler_get_nv_data(p);
      print_dbg("\r\n writing scaler val data to flash at address: 0x");
      print_dbg_hex((unsigned int)dst);
    }
    b = scaler_get_rep_bytes(p);
    if(b > 0) {
      path = scaler_get_rep_path(p);
      print_dbg("\r\n writing scaler data to flash: ");
      print_dbg(path);
      files_load_scaler_name(path, scalerBuf, scalerMaxValues);
      dst = (void *)scaler_get_nv_rep(p);
      print_dbg("\r\n writing scaler rep data to flash at address: 0x");
      print_dbg_hex((unsigned int)dst);
    }
  }
}

variable_types:
b
unsigned int

variable_types:
path
char *

variable_types:
p
unsigned char

variable_types:
dst
unsigned char *

variable_types:
scalerBytes
void *

user_defined_types: struct beesFlashData { unsigned char scalerBytes[-1]; }


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

result = alloc_mem(4096LL);
scalerBuf = result;
return result;
}

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

result = alloc_mem(4096LL);
scalerBuf = result;
return result;
}

original_code:
void flash_bees_init(void) {
  scalerBuf = (s32*)alloc_mem(scalerMaxValues * 4);
}

canonical_original_code:
void flash_bees_init() {
  scalerBuf = (int *)alloc_mem(scalerMaxValues * 4);
}


original_code:
fix16_t fix16_sqrt(fix16_t inValue) {
  uint8_t neg = (inValue < 0);
  uint32_t num = (neg ? -inValue : inValue);
  uint32_t result = 0;
  uint32_t bit;
  uint8_t n;
  if (num & 0xFFF00000)
    bit = (uint32_t)1 << 30;
  else
    bit = (uint32_t)1 << 18;
  while (bit > num) bit >>= 2;
  for (n = 0; n < 2; n++)
  {
    while (bit)
    {
      if (num >= result + bit)
      {
        num -= result + bit;
        result = (result >> 1) + bit;
      }
      else
      {
        result = (result >> 1);
      }
      bit >>= 2;
    }
    if (n == 0)
    {
      if (num > 65535)
      {
        num -= result;
        num = (num << 16) - 0x8000;
        result = (result << 16) + 0x8000;
      }
      else
      {
        num <<= 16;
        result <<= 16;
      }
      bit = 1 << 14;
    }
  }
  if (num > result)
  {
    result++;
  }
  return (neg ? -result : result);
}

canonical_original_code:
int fix16_sqrt(int inValue) {
  unsigned char neg = (inValue < 0);
  unsigned int num = (neg ? -inValue : inValue);
  unsigned int result = 0;
  unsigned int bit;
  unsigned char n;
  if (num & 0xFFF00000)
    bit = (unsigned int)1 << 30;
  else
    bit = (unsigned int)1 << 18;
  while (bit > num) bit >>= 2;
  for (n = 0; n < 2; n++)
  {
    while (bit)
    {
      if (num >= result + bit)
      {
        num -= result + bit;
        result = (result >> 1) + bit;
      }
      else
      {
        result = (result >> 1);
      }
      bit >>= 2;
    }
    if (n == 0)
    {
      if (num > 65535)
      {
        num -= result;
        num = (num << 16) - 0x8000;
        result = (result << 16) + 0x8000;
      }
      else
      {
        num <<= 16;
        result <<= 16;
      }
      bit = 1 << 14;
    }
  }
  if (num > result)
  {
    result++;
  }
  return (neg ? -result : result);
}

variable_types: inValue int

variable_types:
neg
unsigned char

variable_types:
num
unsigned int

variable_types:
result
unsigned int

variable_types:
bit
unsigned int

variable_types:
n
unsigned char


decompiled_code:
__int64 app_init()
{
print_dbg("\r\n preset_init...");
presets_init();
print_dbg("\r\n net_init... ");
net_init();
print_dbg("\r\n scene_init...");
scene_init();
print_dbg("\r\n files_init...");
files_init();
print_dbg("\r\n render_init...");
render_init();
print_dbg("\r\n flash_bees_init...");
return flash_bees_init();
}

canonical_decompiled_code:
__int64 func0()
{
print_dbg("\r\n preset_init...");
presets_init();
print_dbg("\r\n net_init... ");
net_init();
print_dbg("\r\n scene_init...");
scene_init();
print_dbg("\r\n files_init...");
files_init();
print_dbg("\r\n render_init...");
render_init();
print_dbg("\r\n flash_bees_init...");
return flash_bees_init();
}

original_code:
void app_init(void) {
  print_dbg("\r\n preset_init...");
  presets_init();
  print_dbg("\r\n net_init... ");
  net_init();
  print_dbg("\r\n scene_init...");
  scene_init();
  print_dbg("\r\n files_init...");
  files_init();
  print_dbg("\r\n render_init...");
  render_init();
  print_dbg("\r\n flash_bees_init...");
  flash_bees_init();
}

canonical_original_code:
void app_init() {
  print_dbg("\r\n preset_init...");
  presets_init();
  print_dbg("\r\n net_init... ");
  net_init();
  print_dbg("\r\n scene_init...");
  scene_init();
  print_dbg("\r\n files_init...");
  files_init();
  print_dbg("\r\n render_init...");
  render_init();
  print_dbg("\r\n flash_bees_init...");
  flash_bees_init();
}


decompiled_code:
__int64 __fastcall app_launch(unsigned __int8 a1)
{
print_dbg("\r\n launching app with firstrun: ");
print_dbg_ulong(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1)
{
print_dbg("\r\n launching app with firstrun: ");
print_dbg_ulong(a1);
return 1LL;
}

original_code:
u8 app_launch(u8 firstrun) {
  print_dbg("\r\n launching app with firstrun: ");
  print_dbg_ulong(firstrun);
  return 1;
}

canonical_original_code:
unsigned char app_launch(unsigned char firstrun) {
  print_dbg("\r\n launching app with firstrun: ");
  print_dbg_ulong(firstrun);
  return 1;
}

variable_types:
firstrun
unsigned char


original_code:
uint32_t uint32_log2(uint32_t inVal) {
 if(inVal == 0)
  return 0;
 uint32_t tempOut = 0;
 if(inVal >= (1 << 16)) { inVal >>= 16; tempOut += 16; }
 if(inVal >= (1 << 8)) { inVal >>= 8; tempOut += 8; }
 if(inVal >= (1 << 4)) { inVal >>= 4; tempOut += 4; }
 if(inVal >= (1 << 2)) { inVal >>= 2; tempOut += 2; }
 if(inVal >= (1 << 1)) { tempOut += 1; }
 return tempOut;
}

canonical_original_code:
unsigned int uint32_log2(unsigned int inVal) {
 if(inVal == 0)
  return 0;
 unsigned int tempOut = 0;
 if(inVal >= (1 << 16)) { inVal >>= 16; tempOut += 16; }
 if(inVal >= (1 << 8)) { inVal >>= 8; tempOut += 8; }
 if(inVal >= (1 << 4)) { inVal >>= 4; tempOut += 4; }
 if(inVal >= (1 << 2)) { inVal >>= 2; tempOut += 2; }
 if(inVal >= (1 << 1)) { tempOut += 1; }
 return tempOut;
}

variable_types:
tempOut
unsigned int


shiftStartOffset

repo: tejacques/c-deque

decompiled_code:
__int64 __fastcall shiftStartOffset(_DWORD *a1, int a2)
{
__int64 result; // rax

result = a1[3] & (unsigned int)(*a1 + a2);
*a1 = result;
return result;
}

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

result = a1[3] & (unsigned int)(*a1 + a2);
*a1 = result;
return result;
}

original_code:
int shiftStartOffset(deque *deque, int amount)
{
    return (deque->startOffset =
            (deque->startOffset + amount) & deque->lengthMinusOne);
}

canonical_original_code:
int shiftStartOffset(struct deque *deque, int amount)
{
    return (deque->startOffset =
            (deque->startOffset + amount) & deque->lengthMinusOne);
}

variable_types: struct deque { int startOffset; int endOffset; int length; int lengthMinusOne; int count; void **buffer; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); } *

variable_types: amount int


copyMemory

decompiled_code:
unsigned __int64 __fastcall copyMemory(__int64 a1, int a2, __int64 a3, int a4, int a5)
{
int i; // edx
int v7; // ebx
unsigned __int64 result; // rax
int v9; // edx

if ( a5 <= 0 )
return (unsigned __int64)memcpy((void *)(a3 + 8LL * a4), (const void *)(a1 + 8LL * a2), 8LL * a5);
for ( i = 0; i != a5; ++i )
v7 = i;
memcpy((void *)(a3 + 8LL * a4), (const void *)(a1 + 8LL * a2), 8LL * i);
LODWORD(result) = 0;
do
{
v9 = result;
result = (unsigned int)(result + 1);
}
while ( v7 != v9 );
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(__int64 a1, int a2, __int64 a3, int a4, int a5)
{
int i; // edx
int v7; // ebx
unsigned __int64 result; // rax
int v9; // edx

if ( a5 <= 0 )
return (unsigned __int64)memcpy((void *)(a3 + 8LL * a4), (const void *)(a1 + 8LL * a2), 8LL * a5);
for ( i = 0; i != a5; ++i )
v7 = i;
memcpy((void *)(a3 + 8LL * a4), (const void *)(a1 + 8LL * a2), 8LL * i);
LODWORD(result) = 0;
do
{
v9 = result;
result = (unsigned int)(result + 1);
}
while ( v7 != v9 );
return result;
}

original_code:
void copyMemory(
        void **source,
        int sourceIndex,
        void **dest,
        int destIndex,
        int num)
{
    ;
    ;
    for (int i = 0; i < num; i++)
    {
        ;
    }
    memcpy( (void *)(dest+destIndex),
            (const void *)(source+sourceIndex),
            (size_t)(num * sizeof(void*)));
    ;
    for (int i = 0; i < num; i++)
    {
        ;
    }
    ;
}

canonical_original_code:
void copyMemory(void **source, int sourceIndex, void **dest, int destIndex, int num)
{
    ;
    ;
    for (int i = 0; i < num; i++)
    {
        ;
    }
    memcpy( (void *)(dest+destIndex),
            (void *)(source+sourceIndex),
            (unsigned long)(num * sizeof(void *)));
    ;
    for (int i = 0; i < num; i++)
    {
        ;
    }
    ;
}

variable_types:
source
void **

variable_types: sourceIndex int

variable_types:
dest
void **

variable_types: destIndex int

variable_types: num int

variable_types: i int


resizeDeque

decompiled_code:
__int64 __fastcall resizeDeque(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // ebp
void *v4; // rax
void *v5; // r12

result = 0LL;
if ( a2 > 0 && *(_DWORD *)(a1 + 16) <= a2 )
{
v3 = power_of_two_greater_than((unsigned int)a2);
result = 0LL;
if ( *(_DWORD *)(a1 + 8) != v3 )
{
v4 = malloc(8LL * v3);
v5 = v4;
if ( v4 )
{
copyBuffer(a1, (__int64)v4, v3, 0);
free(*(void **)(a1 + 24));
*(_QWORD *)(a1 + 24) = v5;
*(_DWORD *)a1 = 0;
*(_DWORD *)(a1 + 4) = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 8) = v3;
*(_DWORD *)(a1 + 12) = v3 - 1;
result = 1LL;
}
else
{
result = 0LL;
}
}
}
return result;
}

original_code:
_Bool resizeDeque(deque *deque, int length)
{
    if (length <= 0)
    {
        return 0;
    }
    else if (length < ((deque)->count))
    {
        return 0;
    }
    length = power_of_two_greater_than(length);
    if (length == ((deque)->length))
    {
        return 0;
    }
    void **newBuffer = malloc(sizeof(void*) * length);
    if (!newBuffer)
    {
        return 0;
    }
    copyBuffer(deque, newBuffer, length, 0);
    free(deque->buffer);
    deque->buffer = newBuffer;
    deque->startOffset = 0;
    deque->endOffset = deque->count;
    deque->length = length;
    deque->lengthMinusOne = length-1;
    return 1;
}

canonical_original_code:
_Bool resizeDeque(struct deque *deque, int length)
{
    if (length <= 0)
    {
        return 0;
    }
    else if (length < ((deque)->count))
    {
        return 0;
    }
    length = power_of_two_greater_than(length);
    if (length == ((deque)->length))
    {
        return 0;
    }
    void **newBuffer = malloc(sizeof(void*) * length);
    if (!newBuffer)
    {
        return 0;
    }
    copyBuffer(deque, newBuffer, length, 0);
    free(deque->buffer);
    deque->buffer = newBuffer;
    deque->startOffset = 0;
    deque->endOffset = deque->count;
    deque->length = length;
    deque->lengthMinusOne = length-1;
    return 1;
}

variable_types: length int

variable_types:
newBuffer
void **

user_defined_types: struct deque { int startOffset; int endOffset; int length; int lengthMinusOne; int count; void **buffer; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); }


ensureCapacityFor

decompiled_code:
__int64 __fastcall ensureCapacityFor(__int64 a1, int a2)
{
int v2; // esi
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 16) + a2;
result = 1LL;
if ( v2 > *(_DWORD *)(a1 + 8) )
result = resizeDeque(a1, v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
int v2; // esi
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 16) + a2;
result = 1LL;
if ( v2 > *(_DWORD *)(a1 + 8) )
result = func9(a1, v2);
return result;
}

original_code:
_Bool ensureCapacityFor(deque *deque, int numElements)
{
    if (deque->count + numElements > deque->length)
    {
        return resizeDeque(deque, deque->count + numElements);
    }
    return 1;
}

canonical_original_code:
_Bool ensureCapacityFor(struct deque *deque, int numElements)
{
    if (deque->count + numElements > deque->length)
    {
        return resizeDeque(deque, deque->count + numElements);
    }
    return 1;
}

variable_types: numElements int


dequeCreate

original_code:
deque *dequeCreate(void)
{
    struct deque *deque;
    if ((deque = malloc(sizeof(*deque))) == ((void *)0))
    {
        return ((void *)0);
    }
    deque->length = 8;
    deque->startOffset = 0;
    deque->endOffset = 0;
    deque->lengthMinusOne = deque->length - 1;
    deque->count = 0;
    deque->dup = ((void *)0);
    deque->free = ((void *)0);
    deque->match = ((void *)0);
    if ((deque->buffer = malloc(sizeof(void *) * deque->length)) == ((void *)0))
    {
        free(deque);
        return ((void *)0);
    }
    return deque;
}

canonical_original_code:
struct deque *dequeCreate()
{
    struct deque *deque;
    if ((deque = malloc(sizeof(*deque))) == ((void *)0))
    {
        return ((void *)0);
    }
    deque->length = 8;
    deque->startOffset = 0;
    deque->endOffset = 0;
    deque->lengthMinusOne = deque->length - 1;
    deque->count = 0;
    deque->dup = ((void *)0);
    deque->free = ((void *)0);
    deque->match = ((void *)0);
    if ((deque->buffer = malloc(sizeof(void *) * deque->length)) == ((void *)0))
    {
        free(deque);
        return ((void *)0);
    }
    return deque;
}

return_type: struct deque { int startOffset; int endOffset; int length; int lengthMinusOne; int count; void **buffer; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); } *


dequeAddTail

decompiled_code:
__int64 __fastcall dequeAddTail(__int64 a1, __int64 a2)
{
char v2; // dl
__int64 result; // rax
__int64 v4; // r12

v2 = ensureCapacityFor(a1, 1);
result = 0LL;
if ( v2 )
{
v4 = *(_QWORD *)(a1 + 24);
*(_QWORD *)(v4 + 8LL * (int)preShiftEndOffset(a1, 1)) = a2;
++*(_DWORD *)(a1 + 16);
result = a1;
}
return result;
}

original_code:
deque *dequeAddTail(deque *deque, void *value)
{
    if (!ensureCapacityFor(deque, 1))
    {
        return ((void *)0);
    }
    deque->buffer[preShiftEndOffset(deque, 1)] = value;
    deque->count++;
    return deque;
}

canonical_original_code:
struct deque *dequeAddTail(struct deque *deque, void *value)
{
    if (!ensureCapacityFor(deque, 1))
    {
        return ((void *)0);
    }
    deque->buffer[preShiftEndOffset(deque, 1)] = value;
    deque->count++;
    return deque;
}

variable_types:
deque
struct deque *

variable_types:
value
void *


dequeInsertRange

decompiled_code:
__int64 __fastcall dequeInsertRange(__int64 a1, int a2, _QWORD *a3, int a4)
{
int v4; // ebp
bool v6; // al
int v7; // edx
int i; // er13
__int64 v9; // r14
_QWORD *v10; // r15
_QWORD *v11; // r13
int v13; // edx
int v14; // er13
__int64 v15; // r14
_QWORD *v16; // r15
int v18; // [rsp+Ch] [rbp-3Ch]
int v19; // [rsp+Ch] [rbp-3Ch]

v4 = a2;
v6 = indexOutOfRange(a1, a2 - 1);
if ( !a4 || v6 )
return 0LL;
if ( !(unsigned __int8)ensureCapacityFor(a1, a4) )
return 0LL;
v7 = *(_DWORD *)(a1 + 16);
if ( v7 / 2 <= a2 )
{
if ( v7 > a2 )
{
v13 = v7 - a2;
if ( v13 > 0 )
{
v14 = a2;
v19 = v13 + a2;
do
{
v15 = *(_QWORD *)(a1 + 24);
v16 = (_QWORD *)(v15 + 8LL * (int)toBufferIndex((_DWORD *)a1, v14));
*(_QWORD *)(v15 + 8LL * (int)toBufferIndex((_DWORD *)a1, a4 + v14++)) = *v16;
}
while ( v14 != v19 );
}
}
shiftEndOffset(a1, a4);
}
else
{
if ( a2 > 0 )
{
v18 = *(_DWORD *)(a1 + 8) - a4;
for ( i = 0; i != a2; ++i )
{
v9 = *(_QWORD *)(a1 + 24);
v10 = (_QWORD *)(v9 + 8LL * (int)toBufferIndex((_DWORD *)a1, i));
*(_QWORD *)(v9 + 8LL * (int)toBufferIndex((_DWORD *)a1, i + v18)) = *v10;
}
}
shiftStartOffset((_DWORD *)a1, -a4);
}
v11 = a3;
if ( a4 > 0 )
{
do
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)toBufferIndex((_DWORD *)a1, v4++)) = *v11++;
while ( a4 + a2 != v4 );
}
*(_DWORD *)(a1 + 16) += a4;
return a1;
}

original_code:
deque *dequeInsertRange(deque *deque, int index, void **values, int num)
{
    if (indexOutOfRange(deque, index - 1))
    {
        return ((void *)0);
    }
    if (0 == num)
    {
        return ((void *)0);
    }
    if (!ensureCapacityFor(deque, num))
    {
        return ((void *)0);
    }
    if (index < deque->count / 2)
    {
        if (index > 0)
        {
            int copyCount = index;
            int shiftIndex = deque->length - num;
            for (int i = 0; i < copyCount; ++i)
            {
                deque->buffer[toBufferIndex(deque, shiftIndex + i)] =
                    deque->buffer[toBufferIndex(deque, i)];
            }
        }
        shiftStartOffset(deque, -num);
    }
    else
    {
        if (index < deque->count)
        {
            int copyCount = deque->count - index;
            int shiftIndex = index + num;
            for (int i = 0; i < copyCount; ++i)
            {
                deque->buffer[toBufferIndex(deque, shiftIndex + i)] =
                    deque->buffer[toBufferIndex(deque, index + i)];
            }
        }
        shiftEndOffset(deque, num);
    }
    int i = index;
    for(int j = 0; j < num; ++j)
    {
        deque->buffer[toBufferIndex(deque, i)] = values[j];
        ++i;
    }
    deque->count += num;
    return deque;
}

canonical_original_code:
struct deque *dequeInsertRange(struct deque *deque, int index, void **values, int num)
{
    if (indexOutOfRange(deque, index - 1))
    {
        return ((void *)0);
    }
    if (0 == num)
    {
        return ((void *)0);
    }
    if (!ensureCapacityFor(deque, num))
    {
        return ((void *)0);
    }
    if (index < deque->count / 2)
    {
        if (index > 0)
        {
            int copyCount = index;
            int shiftIndex = deque->length - num;
            for (int i = 0; i < copyCount; ++i)
            {
                deque->buffer[toBufferIndex(deque, shiftIndex + i)] =
                    deque->buffer[toBufferIndex(deque, i)];
            }
        }
        shiftStartOffset(deque, -num);
    }
    else
    {
        if (index < deque->count)
        {
            int copyCount = deque->count - index;
            int shiftIndex = index + num;
            for (int i = 0; i < copyCount; ++i)
            {
                deque->buffer[toBufferIndex(deque, shiftIndex + i)] =
                    deque->buffer[toBufferIndex(deque, index + i)];
            }
        }
        shiftEndOffset(deque, num);
    }
    int i = index;
    for(int j = 0; j < num; ++j)
    {
        deque->buffer[toBufferIndex(deque, i)] = values[j];
        ++i;
    }
    deque->count += num;
    return deque;
}

variable_types: index int

variable_types:
values
void **

variable_types: copyCount int

variable_types: shiftIndex int

variable_types: j int


dequeAddHeadRange

decompiled_code:
__int64 __fastcall dequeAddHeadRange(__int64 a1, _QWORD *a2, int a3)
{
return dequeInsertRange(a1, 0, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, _QWORD *a2, int a3)
{
return func14(a1, 0, a2, a3);
}

original_code:
deque *dequeAddHeadRange(deque *deque, void **values, int num)
{
    return dequeInsertRange(deque, 0, values, num);
}

canonical_original_code:
struct deque *dequeAddHeadRange(struct deque *deque, void **values, int num)
{
    return dequeInsertRange(deque, 0, values, num);
}


dequeAddTailRange

decompiled_code:
__int64 __fastcall dequeAddTailRange(__int64 a1, _QWORD *a2, int a3)
{
return dequeInsertRange(a1, *(_DWORD *)(a1 + 16), a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, _QWORD *a2, int a3)
{
return func14(a1, *(_DWORD *)(a1 + 16), a2, a3);
}

original_code:
deque *dequeAddTailRange(deque *deque, void **values, int num)
{
    return dequeInsertRange(deque, deque->count, values, num);
}

canonical_original_code:
struct deque *dequeAddTailRange(struct deque *deque, void **values, int num)
{
    return dequeInsertRange(deque, deque->count, values, num);
}


dequeGet

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

if ( indexOutOfRange(a1, a2) )
result = 0LL;
else
result = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)toBufferIndex((_DWORD *)a1, a2));
return result;
}

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

if ( func1(a1, a2) )
result = 0LL;
else
result = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)func0((_DWORD *)a1, a2));
return result;
}

original_code:
void *dequeGet(deque *deque, int index)
{
    if (indexOutOfRange(deque, index))
    {
        return ((void *)0);
    }
    return deque->buffer[toBufferIndex(deque, index)];
}

canonical_original_code:
void *dequeGet(struct deque *deque, int index)
{
    if (indexOutOfRange(deque, index))
    {
        return ((void *)0);
    }
    return deque->buffer[toBufferIndex(deque, index)];
}

variable_types: struct deque { int startOffset; int endOffset; int length; int lengthMinusOne; int count; void **buffer; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); } *

return_type: void *


dequeRelease

decompiled_code:
void __fastcall dequeRelease(void *ptr)
{
int v1; // ebp
__int64 v2; // rax

if ( *((_QWORD *)ptr + 5) && *((int *)ptr + 4) > 0 )
{
v1 = 0;
do
{
v2 = dequeGet((__int64)ptr, v1);
(*((void (__fastcall **)(__int64))ptr + 5))(v2);
++v1;
}
while ( *((_DWORD *)ptr + 4) > v1 );
}
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
void dequeRelease(deque *deque)
{
    if (deque->free)
    {
        for(int i = 0; i < deque->count; ++i)
        {
            void *val = dequeGet(deque, i);
            deque->free(val);
        }
    }
    free(deque->buffer);
    free(deque);
}

canonical_original_code:
void dequeRelease(struct deque *deque)
{
    if (deque->free)
    {
        for(int i = 0; i < deque->count; ++i)
        {
            void *val = dequeGet(deque, i);
            deque->free(val);
        }
    }
    free(deque->buffer);
    free(deque);
}

variable_types:
val
void *


dequeSet

decompiled_code:
__int64 __fastcall dequeSet(__int64 a1, int a2, __int64 a3)
{
if ( indexOutOfRange(a1, a2) )
return 0LL;
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)toBufferIndex((_DWORD *)a1, a2)) = a3;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, int a2, __int64 a3)
{
if ( func1(a1, a2) )
return 0LL;
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL * (int)func0((_DWORD *)a1, a2)) = a3;
return a1;
}

original_code:
deque *dequeSet(deque *deque, int index, void *value)
{
    if (indexOutOfRange(deque, index))
    {
        return ((void *)0);
    }
    deque->buffer[toBufferIndex(deque, index)] = value;
    return deque;
}

canonical_original_code:
struct deque *dequeSet(struct deque *deque, int index, void *value)
{
    if (indexOutOfRange(deque, index))
    {
        return ((void *)0);
    }
    deque->buffer[toBufferIndex(deque, index)] = value;
    return deque;
}


repo: teknopaul/escape

decompiled_code:
__int64 __fastcall index_of(int a1, __int64 a2, int a3)
{
__int64 v3; // rax
unsigned int v4; // er8

if ( a1 <= 0 )
return (unsigned int)-1;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( *(char *)(a2 + v3) == a3 )
break;
if ( ++v3 == a1 )
return (unsigned int)-1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2, int a3)
{
__int64 v3; // rax
unsigned int v4; // er8

if ( a1 <= 0 )
return (unsigned int)-1;
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( *(char *)(a2 + v3) == a3 )
break;
if ( ++v3 == a1 )
return (unsigned int)-1;
}
return v4;
}

original_code:
static int index_of(int len, const char in_chars[], int ch) {
  int i;
  for(i = 0 ; i < len ; i++) {
    if (ch == in_chars[i]) return i;
  }
  return -1;
}

canonical_original_code:
static int index_of(int len, char in_chars[], int ch) {
  int i;
  for(i = 0 ; i < len ; i++) {
    if (ch == in_chars[i]) return i;
  }
  return -1;
}

variable_types: len int

variable_types: ch int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *i; // r12
int v4; // eax
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
__int64 v13; // r9
__int64 v14; // rdx
__int64 v15; // rcx
__int64 v16; // r8
__int64 v17; // r9
__int64 v18; // rdx
__int64 v19; // rcx
__int64 v20; // r8
__int64 v21; // r9

opterr = 0;
for ( i = "c"; ; i = (const char *)optarg )
{
v4 = getopt(argc, (char *const *)argv, "f:");
if ( v4 == -1 )
{
if ( !strcmp("c", i) )
return escape(stdin, _bss_start, v6, v7, v8, v9, c, off_404148, off_404150);
if ( !strcmp("json", i) )
return escape(stdin, _bss_start, v10, v11, v12, v13, json, off_404178, off_404180);
if ( !strcmp("html", i) )
return escape(stdin, _bss_start, v14, v15, v16, v17, html, off_4040D8, off_4040E0);
if ( !strcmp("xml", i) )
return escape(stdin, _bss_start, v18, v19, v20, v21, xml, off_404088, off_404090);
if ( !strcmp("sanename", i) )
return sanename(stdin, _bss_start);
fprintf(stderr, "Unknown format '%s'\n", i);
return 2;
}
if ( v4 == 63 )
break;
if ( v4 != 102 )
return 1;
}
if ( optopt == 102 )
fwrite("Specify the escape format\n", 1uLL, 0x1AuLL, stderr);
else
fprintf(stderr, "Unknown option character `\\x%x'\n", (unsigned int)optopt);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *i; // r12
int v4; // eax
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v12; // r8
__int64 v13; // r9
__int64 v14; // rdx
__int64 v15; // rcx
__int64 v16; // r8
__int64 v17; // r9
__int64 v18; // rdx
__int64 v19; // rcx
__int64 v20; // r8
__int64 v21; // r9

opterr = 0;
for ( i = "c"; ; i = (const char *)optarg )
{
v4 = getopt(argc, (char *const *)argv, "f:");
if ( v4 == -1 )
{
if ( !strcmp("c", i) )
return func8(stdin, _bss_start, v6, v7, v8, v9, c, off_404148, off_404150);
if ( !strcmp("json", i) )
return func8(stdin, _bss_start, v10, v11, v12, v13, json, off_404178, off_404180);
if ( !strcmp("html", i) )
return func8(stdin, _bss_start, v14, v15, v16, v17, html, off_4040D8, off_4040E0);
if ( !strcmp("xml", i) )
return func8(stdin, _bss_start, v18, v19, v20, v21, xml, off_404088, off_404090);
if ( !strcmp("sanename", i) )
return func9(stdin, _bss_start);
fprintf(stderr, "Unknown format '%s'\n", i);
return 2;
}
if ( v4 == 63 )
break;
if ( v4 != 102 )
return 1;
}
if ( optopt == 102 )
fwrite("Specify the escape format\n", 1uLL, 0x1AuLL, stderr);
else
fprintf(stderr, "Unknown option character `\\x%x'\n", (unsigned int)optopt);
return 1;
}

original_code:
int main (int argc, char *argv[]) {
  char *fvalue = "c";
  int ch;
  opterr = 0;
  while ((ch = getopt (argc, argv, "f:")) != -1) {
    switch (ch)
    {
      case 'f':
        fvalue = optarg;
        break;
      case '?':
        if (optopt == 'f') {
          fprintf(stderr, "Specify the escape format\n");
        }
        else {
          fprintf(stderr, "Unknown option character `\\x%x'\n", optopt);
        }
        return 1;;
      default:
        return 1;;
    }
  }
  if ( strcmp("c", fvalue) == 0 ) {
    return escape(stdin, stdout, c);
  }
  if ( strcmp("json", fvalue) == 0 ) {
    return escape(stdin, stdout, json);
  }
  if ( strcmp("html", fvalue) == 0 ) {
    return escape(stdin, stdout, html);
  }
  if ( strcmp("xml", fvalue) == 0 ) {
    return escape(stdin, stdout, xml);
  }
  if ( strcmp("sanename", fvalue) == 0 ) {
    return sanename(stdin, stdout);
  }
  fprintf(stderr, "Unknown format '%s'\n", fvalue);
  return 2;;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  char *fvalue = "c";
  int ch;
  opterr = 0;
  while ((ch = getopt (argc, argv, "f:")) != -1) {
    switch (ch)
    {
      case 'f':
        fvalue = optarg;
        break;
      case '?':
        if (optopt == 'f') {
          fprintf(stderr, "Specify the escape format\n");
        }
        else {
          fprintf(stderr, "Unknown option character `\\x%x'\n", optopt);
        }
        return 1;;
      default:
        return 1;;
    }
  }
  if ( strcmp("c", fvalue) == 0 ) {
    return escape(stdin, stdout, c);
  }
  if ( strcmp("json", fvalue) == 0 ) {
    return escape(stdin, stdout, json);
  }
  if ( strcmp("html", fvalue) == 0 ) {
    return escape(stdin, stdout, html);
  }
  if ( strcmp("xml", fvalue) == 0 ) {
    return escape(stdin, stdout, xml);
  }
  if ( strcmp("sanename", fvalue) == 0 ) {
    return sanename(stdin, stdout);
  }
  fprintf(stderr, "Unknown format '%s'\n", fvalue);
  return 2;;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
fvalue
char *


repo: telehash/telehash-c

decompiled_code:
__int64 __fastcall hashname_free(__int128 *a1)
{
__int64 result; // rax

if ( a1 == &hn_vtmp )
return util_sys_log(7LL, "src/lib/hashname.c", 26LL, "hashname_free", "programmer error");
result = (__int64)a1;
if ( a1 )
{
free(a1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int128 *a1)
{
__int64 result; // rax

if ( a1 == &hn_vtmp )
return util_sys_log(7LL, "src/lib/hashname.c", 26LL, "hashname_free", "programmer error");
result = (__int64)a1;
if ( a1 )
{
free(a1);
result = 0LL;
}
return result;
}

original_code:
hashname_t hashname_free(hashname_t hn)
{
  if(hn == &hn_vtmp) return util_sys_log(7, "src/lib/hashname.c", 26, __func__, "programmer error");
  if(hn) free(hn);
  return ((void *)0);
}

canonical_original_code:
struct hashname_struct *hashname_free(struct hashname_struct *hn)
{
  if(hn == &hn_vtmp) return util_sys_log(7, "src/lib/hashname.c", 26, __func__, "programmer error");
  if(hn) free(hn);
  return ((void *)0);
}

variable_types:
hn
struct hashname_struct *

return_type: struct hashname_struct { unsigned char bin[32]; } *


decompiled_code:
__int64 __fastcall hashname_bin(__int64 a1)
{
return a1;
}

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

original_code:
uint8_t *hashname_bin(hashname_t hn)
{
  if(!hn) return ((void *)0);
  return hn->bin;
}

canonical_original_code:
unsigned char *hashname_bin(struct hashname_struct *hn)
{
  if(!hn) return ((void *)0);
  return hn->bin;
}

variable_types: struct hashname_struct { unsigned char bin[32]; } *

return_type: unsigned char *


decompiled_code:
void *__fastcall hashname_char(__int64 a1)
{
if ( !a1 )
return 0LL;
base32_encode(a1, 32LL, &hn_ctmp, 53LL);
return &hn_ctmp;
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1)
{
if ( !a1 )
return 0LL;
base32_encode(a1, 32LL, &hn_ctmp, 53LL);
return &hn_ctmp;
}

original_code:
char *hashname_char(hashname_t hn)
{
  if(!hn) return ((void *)0);
  base32_encode(hn->bin,32,hn_ctmp,53);
  return hn_ctmp;
}

canonical_original_code:
char *hashname_char(struct hashname_struct *hn)
{
  if(!hn) return ((void *)0);
  base32_encode(hn->bin,32,hn_ctmp,53);
  return hn_ctmp;
}

user_defined_types: struct hashname_struct { unsigned char bin[32]; }


decompiled_code:
int __fastcall hashname_cmp(const void *a1, const void *a2)
{
int result; // eax

if ( a1 && a2 )
result = memcmp(a1, a2, 0x20uLL);
else
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func3(const void *a1, const void *a2)
{
int result; // eax

if ( a1 && a2 )
result = memcmp(a1, a2, 0x20uLL);
else
result = -1;
return result;
}

original_code:
int hashname_cmp(hashname_t a, hashname_t b)
{
  if(!a || !b) return -1;
  return memcmp(a->bin,b->bin,32);
}

canonical_original_code:
int hashname_cmp(struct hashname_struct *a, struct hashname_struct *b)
{
  if(!a || !b) return -1;
  return memcmp(a->bin,b->bin,32);
}

variable_types:
b
struct hashname_struct *


decompiled_code:
__int64 __fastcall hashname_id(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
unsigned int v3; // er13
const char *v4; // rax
const char *v5; // rbx
char v7[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( !a1 || !a2 )
return 0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v4 = (const char *)lob_get_index(a1, v2);
v5 = v4;
if ( !v4 )
break;
if ( strlen(v4) == 2 && lob_get(a2, v5) )
{
v7[0] = 0;
util_unhex(v5, 2LL, v7);
if ( (unsigned __int8)v3 < (unsigned int)v7[0] )
v3 = (unsigned __int8)v7[0];
}
v2 += 2;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
unsigned int v3; // er13
const char *v4; // rax
const char *v5; // rbx
char v7[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( !a1 || !a2 )
return 0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v4 = (const char *)lob_get_index(a1, v2);
v5 = v4;
if ( !v4 )
break;
if ( strlen(v4) == 2 && lob_get(a2, v5) )
{
v7[0] = 0;
util_unhex(v5, 2LL, v7);
if ( (unsigned __int8)v3 < (unsigned int)v7[0] )
v3 = (unsigned __int8)v7[0];
}
v2 += 2;
}
return v3;
}

original_code:
uint8_t hashname_id(lob_t a, lob_t b)
{
  uint8_t id, best;
  uint32_t i;
  char *key;
  if(!a || !b) return 0;
  best = 0;
  for(i=0;(key = lob_get_index(a,i));i+=2)
  {
    if(strlen(key) != 2) continue;
    if(!lob_get(b,key)) continue;
    id = 0;
    util_unhex(key,2,&id);
    if(id > best) best = id;
  }
  return best;
}

canonical_original_code:
unsigned char hashname_id(struct lob_struct *a, struct lob_struct *b)
{
  unsigned char id; unsigned char best;
  unsigned int i;
  char *key;
  if(!a || !b) return 0;
  best = 0;
  for(i=0;(key = lob_get_index(a,i));i+=2)
  {
    if(strlen(key) != 2) continue;
    if(!lob_get(b,key)) continue;
    id = 0;
    util_unhex(key,2,&id);
    if(id > best) best = id;
  }
  return best;
}

variable_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *

variable_types:
b
struct lob_struct *

variable_types:
id
unsigned char

variable_types:
best
unsigned char

variable_types:
i
unsigned int

variable_types:
key
char *


decompiled_code:
__int64 __fastcall hashname_im(__int64 a1, char a2)
{
void *v2; // r15
unsigned int i; // er12
size_t v4; // rax
const char *v5; // rbx
const char *v6; // rbp
size_t v7; // rax
size_t v8; // rax
__int64 v9; // r14
size_t v10; // rax
__int64 v12; // [rsp+0h] [rbp-78h]
char v13[17]; // [rsp+Ch] [rbp-6Ch] BYREF
char v14[3]; // [rsp+1Dh] [rbp-5Bh] BYREF
char v15[88]; // [rsp+20h] [rbp-58h] BYREF

v13[0] = a2;
if ( !a1 )
return util_sys_log(7LL, "src/lib/hashname.c", 155LL, "hashname_im", "bad args");
v12 = lob_new();
util_hex(v13, 1LL, v14);
v2 = 0LL;
for ( i = 0; ; i += 2 )
{
v5 = (const char *)lob_get_index(a1, i);
if ( !v5 )
break;
v6 = (const char *)lob_get_index(a1, i + 1);
v7 = strlen(v5);
if ( v6 && v7 == 2 )
{
v8 = strlen(v6);
v9 = base32_decode_floor(v8);
if ( !v13[0] || (unsigned int)util_cmp(v14, v5) )
{
v2 = (void *)util_reallocf(v2, v9);
if ( !v2 )
return lob_free(v12);
v4 = strlen(v6);
if ( base32_decode(v6, v4, v2, v9) == v9 )
{
e3x_hash(v2, v9, v15);
lob_set_base32(v12, v5, v15, 32LL);
}
}
else
{
lob_body(v12, 0LL, v9);
v10 = strlen(v6);
if ( base32_decode(v6, v10, *(_QWORD *)(v12 + 8), v9) == v9 )
lob_set_raw(v12, v5, 0LL, "true", 4LL);
}
}
}
if ( v2 )
free(v2);
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, char a2)
{
void *v2; // r15
unsigned int i; // er12
size_t v4; // rax
const char *v5; // rbx
const char *v6; // rbp
size_t v7; // rax
size_t v8; // rax
__int64 v9; // r14
size_t v10; // rax
__int64 v12; // [rsp+0h] [rbp-78h]
char v13[17]; // [rsp+Ch] [rbp-6Ch] BYREF
char v14[3]; // [rsp+1Dh] [rbp-5Bh] BYREF
char v15[88]; // [rsp+20h] [rbp-58h] BYREF

v13[0] = a2;
if ( !a1 )
return util_sys_log(7LL, "src/lib/hashname.c", 155LL, "hashname_im", "bad args");
v12 = lob_new();
util_hex(v13, 1LL, v14);
v2 = 0LL;
for ( i = 0; ; i += 2 )
{
v5 = (const char *)lob_get_index(a1, i);
if ( !v5 )
break;
v6 = (const char *)lob_get_index(a1, i + 1);
v7 = strlen(v5);
if ( v6 && v7 == 2 )
{
v8 = strlen(v6);
v9 = base32_decode_floor(v8);
if ( !v13[0] || (unsigned int)util_cmp(v14, v5) )
{
v2 = (void *)util_reallocf(v2, v9);
if ( !v2 )
return lob_free(v12);
v4 = strlen(v6);
if ( base32_decode(v6, v4, v2, v9) == v9 )
{
e3x_hash(v2, v9, v15);
lob_set_base32(v12, v5, v15, 32LL);
}
}
else
{
lob_body(v12, 0LL, v9);
v10 = strlen(v6);
if ( base32_decode(v6, v10, *(_QWORD *)(v12 + 8), v9) == v9 )
lob_set_raw(v12, v5, 0LL, "true", 4LL);
}
}
}
if ( v2 )
free(v2);
return v12;
}

original_code:
lob_t hashname_im(lob_t keys, uint8_t id)
{
  uint32_t i;
  size_t len;
  uint8_t *buf, hash[32];
  char *key, *value, hex[3];
  lob_t im;
  if(!keys) return util_sys_log(7, "src/lib/hashname.c", 155, __func__, "bad args");
  im = lob_new();
  buf = ((void *)0);
  util_hex(&id,1,hex);
  for(i=0;(key = lob_get_index(keys,i));i+=2)
  {
    value = lob_get_index(keys,i+1);
    if(strlen(key) != 2 || !value) continue;
    len = base32_decode_floor(strlen(value));
    if(id && util_cmp(hex,key) == 0)
    {
      lob_body(im,((void *)0),len);
      if(base32_decode(value,strlen(value),im->body,len) != len) continue;
      lob_set_raw(im,key,0,"true",4);
    }else{
      buf = util_reallocf(buf,len);
      if(!buf) return lob_free(im);
      if(base32_decode(value,strlen(value),buf,len) != len) continue;
      e3x_hash(buf,len,hash);
      lob_set_base32(im,key,hash,32);
    }
  }
  if(buf) free(buf);
  return im;
}

canonical_original_code:
struct lob_struct *hashname_im(struct lob_struct *keys, unsigned char id)
{
  unsigned int i;
  unsigned long len;
  unsigned char *buf; unsigned char hash[32];
  char *key; char *value; char hex[3];
  struct lob_struct *im;
  if(!keys) return util_sys_log(7, "src/lib/hashname.c", 155, __func__, "bad args");
  im = lob_new();
  buf = ((void *)0);
  util_hex(&id,1,hex);
  for(i=0;(key = lob_get_index(keys,i));i+=2)
  {
    value = lob_get_index(keys,i+1);
    if(strlen(key) != 2 || !value) continue;
    len = base32_decode_floor(strlen(value));
    if(id && util_cmp(hex,key) == 0)
    {
      lob_body(im,((void *)0),len);
      if(base32_decode(value,strlen(value),im->body,len) != len) continue;
      lob_set_raw(im,key,0,"true",4);
    }else{
      buf = util_reallocf(buf,len);
      if(!buf) return lob_free(im);
      if(base32_decode(value,strlen(value),buf,len) != len) continue;
      e3x_hash(buf,len,hash);
      lob_set_base32(im,key,hash,32);
    }
  }
  if(buf) free(buf);
  return im;
}

variable_types:
keys
struct lob_struct *

variable_types:
len
unsigned long

variable_types:
buf
unsigned char *

variable_types:
hash
unsigned char [32]

variable_types:
value
char *

variable_types:
hex
char [3]

return_type: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *


decompiled_code:
__int64 __fastcall hashname_vkeys(__int64 a1)
{
__int64 v1; // rbx
__int128 *v2; // rbp

if ( !a1 )
return util_sys_log(7LL, "src/lib/hashname.c", 96LL, "hashname_vkeys", "bad args");
v1 = hashname_im(a1, 0);
v2 = hashname_vkey(v1, 0);
lob_free(v1);
return (__int64)v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v1; // rbx
__int128 *v2; // rbp

if ( !a1 )
return util_sys_log(7LL, "src/lib/hashname.c", 96LL, "hashname_vkeys", "bad args");
v1 = func5(a1, 0);
v2 = hashname_vkey(v1, 0);
lob_free(v1);
return (__int64)v2;
}

original_code:
hashname_t hashname_vkeys(lob_t keys)
{
  hashname_t hn;
  lob_t im;
  if(!keys) return util_sys_log(7, "src/lib/hashname.c", 96, __func__, "bad args");
  im = hashname_im(keys,0);
  hn = hashname_vkey(im,0);
  lob_free(im);
  return hn;
}

canonical_original_code:
struct hashname_struct *hashname_vkeys(struct lob_struct *keys)
{
  struct hashname_struct *hn;
  struct lob_struct *im;
  if(!keys) return util_sys_log(7, "src/lib/hashname.c", 96, __func__, "bad args");
  im = hashname_im(keys,0);
  hn = hashname_vkey(im,0);
  lob_free(im);
  return hn;
}

user_defined_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; }


decompiled_code:
char *__fastcall hashname_short(__int64 a1)
{
if ( !a1 )
return 0LL;
tog_0 = tog_0 == 0 ? 0x1A : 0;
base32_encode(a1, 5LL, (char *)&hn_ctmp + (unsigned __int8)tog_0, 53 - (unsigned __int8)tog_0);
return (char *)&hn_ctmp + (unsigned __int8)tog_0;
}

canonical_decompiled_code:
char *__fastcall func7(__int64 a1)
{
if ( !a1 )
return 0LL;
tog_0 = tog_0 == 0 ? 0x1A : 0;
base32_encode(a1, 5LL, (char *)&hn_ctmp + (unsigned __int8)tog_0, 53 - (unsigned __int8)tog_0);
return (char *)&hn_ctmp + (unsigned __int8)tog_0;
}

original_code:
char *hashname_short(hashname_t hn)
{
  static uint8_t tog = 1;
  if(!hn) return ((void *)0);
  tog = tog ? 0 : 26;
  base32_encode(hn->bin,5,hn_ctmp+tog,53-tog);
  return hn_ctmp+tog;
}

canonical_original_code:
char *hashname_short(struct hashname_struct *hn)
{
  static unsigned char tog = 1;
  if(!hn) return ((void *)0);
  tog = tog ? 0 : 26;
  base32_encode(hn->bin,5,hn_ctmp+tog,53-tog);
  return hn_ctmp+tog;
}

variable_types:
tog
unsigned char


decompiled_code:
int __fastcall hashname_scmp(const void *a1, const void *a2)
{
int result; // eax

if ( a1 && a2 )
result = memcmp(a1, a2, 5uLL);
else
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func8(const void *a1, const void *a2)
{
int result; // eax

if ( a1 && a2 )
result = memcmp(a1, a2, 5uLL);
else
result = -1;
return result;
}

original_code:
int hashname_scmp(hashname_t a, hashname_t b)
{
  if(!a || !b) return -1;
  return memcmp(a->bin,b->bin,5);
}

canonical_original_code:
int hashname_scmp(struct hashname_struct *a, struct hashname_struct *b)
{
  if(!a || !b) return -1;
  return memcmp(a->bin,b->bin,5);
}


decompiled_code:
__int64 __fastcall hashname_isshort(__int64 a1)
{
_BYTE *v1; // rax

if ( !a1 )
return 0LL;
v1 = (_BYTE *)(a1 + 5);
while ( !*v1 )
{
if ( ++v1 == (_BYTE *)(a1 + 32) )
return a1;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
_BYTE *v1; // rax

if ( !a1 )
return 0LL;
v1 = (_BYTE *)(a1 + 5);
while ( !*v1 )
{
if ( ++v1 == (_BYTE *)(a1 + 32) )
return a1;
}
return 0LL;
}

original_code:
hashname_t hashname_isshort(hashname_t hn)
{
  if(!hn) return ((void *)0);
  uint8_t i;
  for(i=5;i<32;i++) if(hn->bin[i]) return ((void *)0);
  return hn;
}

canonical_original_code:
struct hashname_struct *hashname_isshort(struct hashname_struct *hn)
{
  if(!hn) return ((void *)0);
  unsigned char i;
  for(i=5;i<32;i++) if(hn->bin[i]) return ((void *)0);
  return hn;
}

variable_types:
i
unsigned char


variable_types:
pcarry
unsigned int *

variable_types:
key
void *

variable_types: len int

variable_types:
c
unsigned int

variable_types:
ptr
unsigned char *

variable_types:
end
unsigned char *

variable_types: n int

variable_types: i int


canonical_original_code:
unsigned int PMurHash32_Result(unsigned int h, unsigned int carry, unsigned int total_length)
{
  unsigned int k1;
  int n = carry & 3;
  if(n) {
    k1 = carry >> (4-n)*8;
    k1 *= (0xcc9e2d51); k1 = (((unsigned int)k1 << 15) | ((unsigned int)k1 >> (32 - 15))); k1 *= (0x1b873593); h ^= k1;
  }
  h ^= total_length;
  h ^= h >> 16;
  h *= 0x85ebca6b;
  h ^= h >> 13;
  h *= 0xc2b2ae35;
  h ^= h >> 16;
  return h;
}

variable_types:
h
unsigned int

variable_types:
carry
unsigned int


decompiled_code:
__int64 __fastcall PMurHash32(int a1, unsigned __int64 a2, unsigned int a3)
{
unsigned int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a1;
v5 = 0;
PMurHash32_Process(v6, &v5, a2, a3);
return PMurHash32_Result(v6[0], v5, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, unsigned __int64 a2, unsigned int a3)
{
unsigned int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a1;
v5 = 0;
func0(v6, &v5, a2, a3);
return func1(v6[0], v5, a3);
}

original_code:
uint32_t PMurHash32(uint32_t seed, const void *key, int len)
{
  uint32_t h1=seed, carry=0;
  PMurHash32_Process(&h1, &carry, key, len);
  return PMurHash32_Result(h1, carry, len);
}

canonical_original_code:
unsigned int PMurHash32(unsigned int seed, void *key, int len)
{
  unsigned int h1 = seed; unsigned int carry = 0;
  PMurHash32_Process(&h1, &carry, key, len);
  return PMurHash32_Result(h1, carry, len);
}

variable_types:
seed
unsigned int


decompiled_code:
__int64 __fastcall murmur4(unsigned __int64 a1, unsigned int a2)
{
return PMurHash32(0, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int64 a1, unsigned int a2)
{
return func2(0, a1, a2);
}

original_code:
uint32_t murmur4(const uint8_t *data, uint32_t len)
{
  return PMurHash32(0, data, len);
}

canonical_original_code:
unsigned int murmur4(unsigned char *data, unsigned int len)
{
  return PMurHash32(0, data, len);
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned int


decompiled_code:
char *__fastcall murmur8(unsigned __int64 a1, unsigned int a2, char *a3)
{
unsigned int v4; // eax

v4 = PMurHash32(0, a1, a2);
sprintf(a3, "%08lx", v4);
return a3;
}

canonical_decompiled_code:
char *__fastcall func4(unsigned __int64 a1, unsigned int a2, char *a3)
{
unsigned int v4; // eax

v4 = func2(0, a1, a2);
sprintf(a3, "%08lx", v4);
return a3;
}

original_code:
char *murmur8(const uint8_t *data, uint32_t len, char *hex)
{
  uint32_t hash = PMurHash32(0, data, len);
  sprintf(hex,"%08lx",(unsigned long)hash);
  return hex;
}

canonical_original_code:
char *murmur8(unsigned char *data, unsigned int len, char *hex)
{
  unsigned int hash = PMurHash32(0, data, len);
  sprintf(hex,"%08lx",(unsigned long)hash);
  return hex;
}

variable_types:
hex
char *

variable_types:
hash
unsigned int


decompiled_code:
__int64 cs3a_init()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
e3x_cipher_t cs3a_init(lob_t options)
{
  return ((void *)0);
}

canonical_original_code:
struct e3x_cipher_struct *cs3a_init(struct lob_struct *options)
{
  return ((void *)0);
}

variable_types:
options
struct lob_struct *

return_type: struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *

user_defined_types: struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; }


original_code:
size_t base32_decode(const char *encoded, size_t length, uint8_t *result, size_t bufSize) {
  int buffer = 0;
  size_t bitsLeft = 0;
  size_t count = 0;
  const char *ptr = encoded;
  if(!encoded || !result || bufSize <= 0) return 0;
  if(!length) length = strlen(encoded);
  for (; (size_t)(ptr-encoded) < length && count < bufSize; ++ptr) {
    uint8_t ch = (uint8_t)*ptr;
    if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') {
      continue;
    }
    buffer <<= 5;
    if (ch == '0') {
      ch = 'O';
    } else if (ch == '1') {
      ch = 'L';
    } else if (ch == '8') {
      ch = 'B';
    }
    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
      ch = (ch & 0x1F) - 1;
    } else if (ch >= '2' && ch <= '7') {
      ch -= '2' - 26;
    } else {
      return 0;
    }
    buffer |= ch;
    bitsLeft += 5;
    if (bitsLeft >= 8) {
      result[count++] = buffer >> (bitsLeft - 8);
      bitsLeft -= 8;
    }
  }
  if (count < bufSize) {
    result[count] = '\000';
  }
  return count;
}

canonical_original_code:
unsigned long base32_decode(char *encoded, unsigned long length, unsigned char *result, unsigned long bufSize) {
  int buffer = 0;
  unsigned long bitsLeft = 0;
  unsigned long count = 0;
  char *ptr = encoded;
  if(!encoded || !result || bufSize <= 0) return 0;
  if(!length) length = strlen(encoded);
  for (; (unsigned long)(ptr-encoded) < length && count < bufSize; ++ptr) {
    unsigned char ch = (uint8_t)*ptr;
    if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') {
      continue;
    }
    buffer <<= 5;
    if (ch == '0') {
      ch = 'O';
    } else if (ch == '1') {
      ch = 'L';
    } else if (ch == '8') {
      ch = 'B';
    }
    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
      ch = (ch & 0x1F) - 1;
    } else if (ch >= '2' && ch <= '7') {
      ch -= '2' - 26;
    } else {
      return 0;
    }
    buffer |= ch;
    bitsLeft += 5;
    if (bitsLeft >= 8) {
      result[count++] = buffer >> (bitsLeft - 8);
      bitsLeft -= 8;
    }
  }
  if (count < bufSize) {
    result[count] = '\000';
  }
  return count;
}

variable_types:
encoded
char *

variable_types:
length
unsigned long

variable_types:
result
unsigned char *

variable_types:
bufSize
unsigned long

variable_types: buffer int

variable_types:
bitsLeft
unsigned long

variable_types:
count
unsigned long

variable_types:
ptr
char *

variable_types:
ch
unsigned char

return_type: unsigned long


decompiled_code:
unsigned __int64 __fastcall base32_encode(unsigned __int8 *a1, unsigned __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v6; // er9
unsigned __int64 result; // rax
int v8; // er11
unsigned __int64 v9; // rbx
int v10; // ecx

if ( !a1 || !a3 )
return 0LL;
if ( !a4 || !a2 )
return 0LL;
v6 = *a1;
result = 0LL;
v8 = 8;
v9 = 1LL;
while ( 1 )
{
v10 = v8 - 5;
++result;
*(_BYTE *)(a3 + result - 1) = byte_250[(v6 >> (v8 - 5)) & 0x1F];
if ( a4 <= result )
break;
if ( v10 <= 0 && v9 >= a2 )
{
*(_BYTE *)(a3 + result) = 0;
return result;
}
if ( v10 > 4 )
{
v8 -= 5;
}
else if ( v9 >= a2 )
{
v6 <<= 5 - v10;
v8 = 5;
}
else
{
v6 = a1[v9] | (v6 << 8);
v8 += 3;
++v9;
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(unsigned __int8 *a1, unsigned __int64 a2, __int64 a3, unsigned __int64 a4)
{
int v6; // er9
unsigned __int64 result; // rax
int v8; // er11
unsigned __int64 v9; // rbx
int v10; // ecx

if ( !a1 || !a3 )
return 0LL;
if ( !a4 || !a2 )
return 0LL;
v6 = *a1;
result = 0LL;
v8 = 8;
v9 = 1LL;
while ( 1 )
{
v10 = v8 - 5;
++result;
*(_BYTE *)(a3 + result - 1) = byte_250[(v6 >> (v8 - 5)) & 0x1F];
if ( a4 <= result )
break;
if ( v10 <= 0 && v9 >= a2 )
{
*(_BYTE *)(a3 + result) = 0;
return result;
}
if ( v10 > 4 )
{
v8 -= 5;
}
else if ( v9 >= a2 )
{
v6 <<= 5 - v10;
v8 = 5;
}
else
{
v6 = a1[v9] | (v6 << 8);
v8 += 3;
++v9;
}
}
return result;
}

original_code:
size_t base32_encode(const uint8_t *data, size_t length, char *result, size_t bufSize) {
  if (!data || !result || !bufSize || !length) return 0;
  size_t count = 0;
  if (length > 0) {
    int buffer = data[0];
    size_t next = 1;
    int bitsLeft = 8;
    while (count < bufSize && (bitsLeft > 0 || next < length)) {
      if (bitsLeft < 5) {
        if (next < length) {
          buffer <<= 8;
          buffer |= data[next++] & 0xFF;
          bitsLeft += 8;
        } else {
          int pad = 5 - bitsLeft;
          buffer <<= pad;
          bitsLeft += pad;
        }
      }
      int index = 0x1F & (buffer >> (bitsLeft - 5));
      bitsLeft -= 5;
      result[count++] = "abcdefghijklmnopqrstuvwxyz234567"[index];
    }
  }
  if (count < bufSize) {
    result[count] = '\000';
  }
  return count;
}

canonical_original_code:
unsigned long base32_encode(unsigned char *data, unsigned long length, char *result, unsigned long bufSize) {
  if (!data || !result || !bufSize || !length) return 0;
  unsigned long count = 0;
  if (length > 0) {
    int buffer = data[0];
    unsigned long next = 1;
    int bitsLeft = 8;
    while (count < bufSize && (bitsLeft > 0 || next < length)) {
      if (bitsLeft < 5) {
        if (next < length) {
          buffer <<= 8;
          buffer |= data[next++] & 0xFF;
          bitsLeft += 8;
        } else {
          int pad = 5 - bitsLeft;
          buffer <<= pad;
          bitsLeft += pad;
        }
      }
      int index = 0x1F & (buffer >> (bitsLeft - 5));
      bitsLeft -= 5;
      result[count++] = "abcdefghijklmnopqrstuvwxyz234567"[index];
    }
  }
  if (count < bufSize) {
    result[count] = '\000';
  }
  return count;
}

variable_types:
result
char *

variable_types:
next
unsigned long

variable_types: bitsLeft int

variable_types: pad int

variable_types: index int


decompiled_code:
unsigned __int64 __fastcall base32_encode_length(unsigned __int64 a1)
{
return 8 * a1 / 5
+ (a1 != a1 / 5 + (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)a1) >> 64) & 0xFFFFFFFFFFFFFFFCLL))
+ 1;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(unsigned __int64 a1)
{
return 8 * a1 / 5
+ (a1 != a1 / 5 + (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)a1) >> 64) & 0xFFFFFFFFFFFFFFFCLL))
+ 1;
}

original_code:
size_t base32_encode_length(size_t rawLength)
{
  return ((rawLength * 8) / 5) + ((rawLength % 5) != 0) + 1;
}

canonical_original_code:
unsigned long base32_encode_length(unsigned long rawLength)
{
  return ((rawLength * 8) / 5) + ((rawLength % 5) != 0) + 1;
}

variable_types:
rawLength
unsigned long


decompiled_code:
unsigned __int64 __fastcall base32_decode_floor(__int64 a1)
{
return (unsigned __int64)(5 * a1) >> 3;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(__int64 a1)
{
return (unsigned __int64)(5 * a1) >> 3;
}

original_code:
size_t base32_decode_floor(size_t base32Length)
{
  return ((base32Length * 5) / 8);
}

canonical_original_code:
unsigned long base32_decode_floor(unsigned long base32Length)
{
  return ((base32Length * 5) / 8);
}


decompiled_code:
__int64 __fastcall util_chunk_new(__int64 a1, unsigned __int8 a2)
{
_BYTE *v2; // rax

v2 = malloc(a2 + 16LL);
if ( !v2 )
return util_sys_log(7LL, "src/util/chunks.c", 12LL, "util_chunk_new", &unk_7B9);
*(_QWORD *)&v2[a2 + 8] = 0LL;
memset(v2, 0, 8LL * (((unsigned int)a2 + 15) >> 3));
v2[8] = a2;
*(_QWORD *)v2 = *(_QWORD *)a1;
*(_QWORD *)a1 = v2;
*(_BYTE *)(a1 + 27) = 0;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 a2)
{
_BYTE *v2; // rax

v2 = malloc(a2 + 16LL);
if ( !v2 )
return util_sys_log(7LL, "src/util/chunks.c", 12LL, "util_chunk_new", &unk_7B9);
*(_QWORD *)&v2[a2 + 8] = 0LL;
memset(v2, 0, 8LL * (((unsigned int)a2 + 15) >> 3));
v2[8] = a2;
*(_QWORD *)v2 = *(_QWORD *)a1;
*(_QWORD *)a1 = v2;
*(_BYTE *)(a1 + 27) = 0;
return a1;
}

original_code:
util_chunks_t util_chunk_new(util_chunks_t chunks, uint8_t len)
{
  util_chunk_t chunk;
  size_t size = sizeof (struct util_chunk_struct);
  size += len;
  if(!(chunk = malloc(size))) return util_sys_log(7, "src/util/chunks.c", 12, __func__, "OOM");
  memset(chunk,0,size);
  chunk->size = len;
  chunk->prev = chunks->reading;
  chunks->reading = chunk;
  chunks->readat = 0;
  return chunks;
}

canonical_original_code:
struct util_chunks_struct *util_chunk_new(struct util_chunks_struct *chunks, unsigned char len)
{
  struct util_chunk_struct *chunk;
  unsigned long size = sizeof (struct util_chunk_struct);
  size += len;
  if(!(chunk = malloc(size))) return util_sys_log(7, "src/util/chunks.c", 12, __func__, "OOM");
  memset(chunk,0,size);
  chunk->size = len;
  chunk->prev = chunks->reading;
  chunks->reading = chunk;
  chunks->readat = 0;
  return chunks;
}

variable_types:
chunks
struct util_chunks_struct *

variable_types:
len
unsigned char

variable_types:
size
unsigned long

return_type: struct util_chunks_struct { struct util_chunk_struct { struct util_chunk_struct *prev; unsigned char size; unsigned char data[]; } *reading; struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *writing; unsigned long writeat; unsigned short waitat; unsigned char waiting; unsigned char readat; unsigned char cap; unsigned char blocked; } *

user_defined_types: struct util_chunks_struct { struct util_chunk_struct *reading; struct lob_struct *writing; unsigned long writeat; unsigned short waitat; unsigned char waiting; unsigned char readat; unsigned char cap; unsigned char blocked; }


decompiled_code:
__int64 __fastcall util_chunk_free(__int64 *a1)
{
__int64 v1; // rbx

if ( !a1 )
return 0LL;
v1 = *a1;
free(a1);
return util_chunk_free(v1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1)
{
__int64 v1; // rbx

if ( !a1 )
return 0LL;
v1 = *a1;
free(a1);
return func1(v1);
}

original_code:
util_chunk_t util_chunk_free(util_chunk_t chunk)
{
  if(!chunk) return ((void *)0);
  util_chunk_t prev = chunk->prev;
  free(chunk);
  return util_chunk_free(prev);
}

canonical_original_code:
struct util_chunk_struct *util_chunk_free(struct util_chunk_struct *chunk)
{
  if(!chunk) return ((void *)0);
  struct util_chunk_struct *prev = chunk->prev;
  free(chunk);
  return util_chunk_free(prev);
}

variable_types:
chunk
struct util_chunk_struct *

variable_types: struct util_chunk_struct { struct util_chunk_struct *prev; unsigned char size; unsigned char data[]; } *


decompiled_code:
__int64 __fastcall util_chunks_free(void *ptr)
{
if ( !ptr )
return 0LL;
if ( *((_QWORD *)ptr + 1) )
lob_free();
util_chunk_free(*(__int64 **)ptr);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
if ( !ptr )
return 0LL;
if ( *((_QWORD *)ptr + 1) )
lob_free();
func1(*(__int64 **)ptr);
free(ptr);
return 0LL;
}

original_code:
util_chunks_t util_chunks_free(util_chunks_t chunks)
{
  if(!chunks) return ((void *)0);
  if(chunks->writing) lob_free(chunks->writing);
  util_chunk_free(chunks->reading);
  free(chunks);
  return ((void *)0);
}

canonical_original_code:
struct util_chunks_struct *util_chunks_free(struct util_chunks_struct *chunks)
{
  if(!chunks) return ((void *)0);
  if(chunks->writing) lob_free(chunks->writing);
  util_chunk_free(chunks->reading);
  free(chunks);
  return ((void *)0);
}

user_defined_types: struct util_chunk_struct { struct util_chunk_struct *prev; unsigned char size; unsigned char data[]; }


decompiled_code:
__int64 __fastcall util_chunks_send(__int64 a1, __int64 a2)
{
if ( !a1 || !a2 )
return util_sys_log(7LL, "src/util/chunks.c", 76LL, "util_chunks_send", "bad args");
*(_QWORD *)(a1 + 8) = lob_push(*(_QWORD *)(a1 + 8));
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
if ( !a1 || !a2 )
return util_sys_log(7LL, "src/util/chunks.c", 76LL, "util_chunks_send", "bad args");
*(_QWORD *)(a1 + 8) = lob_push(*(_QWORD *)(a1 + 8));
return a1;
}

original_code:
util_chunks_t util_chunks_send(util_chunks_t chunks, lob_t out)
{
  if(!chunks || !out) return util_sys_log(7, "src/util/chunks.c", 76, __func__, "bad args");
  chunks->writing = lob_push(chunks->writing, out);
  return chunks;
}

canonical_original_code:
struct util_chunks_struct *util_chunks_send(struct util_chunks_struct *chunks, struct lob_struct *out)
{
  if(!chunks || !out) return util_sys_log(7, "src/util/chunks.c", 76, __func__, "bad args");
  chunks->writing = lob_push(chunks->writing, out);
  return chunks;
}

variable_types:
out
struct lob_struct *


original_code:
lob_t util_chunks_receive(util_chunks_t chunks)
{
  util_chunk_t chunk, flush;
  size_t len = 0;
  if(!chunks || !chunks->reading) return ((void *)0);
  for(flush = ((void *)0),chunk = chunks->reading;chunk;chunk=chunk->prev)
  {
    if(chunk->size == 0)
    {
      flush = chunk;
      len = 0;
    }
    if(flush) len += chunk->size;
  }
  if(!flush) return ((void *)0);
  if(chunks->reading == flush)
  {
    chunks->reading = ((void *)0);
  }else{
    for(chunk = chunks->reading;chunk->prev != flush;chunk = chunk->prev);
    chunk->prev = ((void *)0);
  }
  chunk = flush->prev;
  free(flush);
  if(!chunk) return util_chunks_receive(chunks);
  uint8_t *buf = malloc(len);
  if(!buf) return util_sys_log(7, "src/util/chunks.c", 124, __func__, "OOM");
  util_chunk_t prev;
  size_t at;
  for(at=len;chunk;chunk = prev)
  {
    prev = chunk->prev;
    memcpy(buf+(at-chunk->size),chunk->data,chunk->size);
    at -= chunk->size;
    free(chunk);
  }
  chunks->ack = 1;
  lob_t ret = lob_parse(buf,len);
  chunks->err = ret ? 0 : 1;
  free(buf);
  return ret;
}

canonical_original_code:
struct lob_struct *util_chunks_receive(struct util_chunks_struct *chunks)
{
  struct util_chunk_struct *chunk; struct util_chunk_struct *flush;
  unsigned long len = 0;
  if(!chunks || !chunks->reading) return ((void *)0);
  for(flush = ((void *)0),chunk = chunks->reading;chunk;chunk=chunk->prev)
  {
    if(chunk->size == 0)
    {
      flush = chunk;
      len = 0;
    }
    if(flush) len += chunk->size;
  }
  if(!flush) return ((void *)0);
  if(chunks->reading == flush)
  {
    chunks->reading = ((void *)0);
  }else{
    for(chunk = chunks->reading;chunk->prev != flush;chunk = chunk->prev);
    chunk->prev = ((void *)0);
  }
  chunk = flush->prev;
  free(flush);
  if(!chunk) return util_chunks_receive(chunks);
  unsigned char *buf = malloc(len);
  if(!buf) return util_sys_log(7, "src/util/chunks.c", 124, __func__, "OOM");
  struct util_chunk_struct *prev;
  unsigned long at;
  for(at=len;chunk;chunk = prev)
  {
    prev = chunk->prev;
    memcpy(buf+(at-chunk->size),chunk->data,chunk->size);
    at -= chunk->size;
    free(chunk);
  }
  chunks->ack = 1;
  struct lob_struct *ret = lob_parse(buf,len);
  chunks->err = ret ? 0 : 1;
  free(buf);
  return ret;
}

variable_types: struct util_chunks_struct { struct util_chunk_struct { struct util_chunk_struct *prev; unsigned char size; unsigned char data[]; } *reading; struct lob_struct *writing; unsigned long writeat; unsigned short waitat; unsigned char waiting; unsigned char readat; unsigned char cap; unsigned char blocked; } *

variable_types:
len
unsigned long

variable_types:
buf
unsigned char *

variable_types:
at
unsigned long

variable_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *


decompiled_code:
__int64 __fastcall util_chunks_len(__int64 a1)
{
char v1; // dl
__int64 result; // rax
__int64 v3; // rax
unsigned __int64 v4; // rax
unsigned __int16 v5; // dx

if ( !a1 )
return 0LL;
v1 = *(_BYTE *)(a1 + 29);
result = 0LL;
if ( (v1 & 1) == 0 )
{
if ( *(_QWORD *)(a1 + 8) )
{
v3 = lob_len();
if ( !*(_BYTE *)(a1 + 26) )
{
v4 = v3 - *(_QWORD *)(a1 + 16);
if ( *(unsigned __int8 *)(a1 + 28) <= v4 )
LOBYTE(v4) = *(_BYTE *)(a1 + 28);
*(_BYTE *)(a1 + 26) = v4;
}
v5 = *(_WORD *)(a1 + 24);
result = 1LL;
if ( v5 )
result = *(unsigned __int8 *)(a1 + 26) - ((unsigned int)v5 - 1);
}
else
{
result = (v1 & 4) != 0;
}
}
return result;
}

original_code:
uint32_t util_chunks_len(util_chunks_t chunks)
{
  if(!chunks || chunks->blocked) return 0;
  if(!chunks->writing) return (chunks->ack) ? 1 : 0;
  size_t avail = lob_len(chunks->writing) - chunks->writeat;
  if(avail > chunks->cap) avail = chunks->cap;
  if(!chunks->waiting) chunks->waiting = avail;
  if(!chunks->waitat) return 1;
  return chunks->waiting - (chunks->waitat-1);
}

canonical_original_code:
unsigned int util_chunks_len(struct util_chunks_struct *chunks)
{
  if(!chunks || chunks->blocked) return 0;
  if(!chunks->writing) return (chunks->ack) ? 1 : 0;
  unsigned long avail = lob_len(chunks->writing) - chunks->writeat;
  if(avail > chunks->cap) avail = chunks->cap;
  if(!chunks->waiting) chunks->waiting = avail;
  if(!chunks->waitat) return 1;
  return chunks->waiting - (chunks->waitat-1);
}

variable_types: struct util_chunks_struct { struct util_chunk_struct { struct util_chunk_struct *prev; unsigned char size; unsigned char data[]; } *reading; struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *writing; unsigned long writeat; unsigned short waitat; unsigned char waiting; unsigned char readat; unsigned char cap; unsigned char blocked; } *

variable_types:
avail
unsigned long


decompiled_code:
__int64 __fastcall util_chunks_writing(__int64 a1)
{
unsigned int v1; // ebp
__int64 i; // rbx

v1 = 0;
if ( a1 )
{
util_chunks_len(a1);
v1 = lob_len(*(_QWORD *)(a1 + 8)) - *(_DWORD *)(a1 + 16);
for ( i = lob_next(*(_QWORD *)(a1 + 8)); i; i = lob_next(i) )
v1 += lob_len(i);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
unsigned int v1; // ebp
__int64 i; // rbx

v1 = 0;
if ( a1 )
{
func6(a1);
v1 = lob_len(*(_QWORD *)(a1 + 8)) - *(_DWORD *)(a1 + 16);
for ( i = lob_next(*(_QWORD *)(a1 + 8)); i; i = lob_next(i) )
v1 += lob_len(i);
}
return v1;
}

original_code:
uint32_t util_chunks_writing(util_chunks_t chunks)
{
  lob_t cur;
  uint32_t len = 0;
  if(!chunks) return 0;
  util_chunks_len(chunks);
  len = lob_len(chunks->writing) - chunks->writeat;
  for(cur=lob_next(chunks->writing);cur;cur = lob_next(cur)) len += lob_len(cur);
  return len;
}

canonical_original_code:
unsigned int util_chunks_writing(struct util_chunks_struct *chunks)
{
  struct lob_struct *cur;
  unsigned int len = 0;
  if(!chunks) return 0;
  util_chunks_len(chunks);
  len = lob_len(chunks->writing) - chunks->writeat;
  for(cur=lob_next(chunks->writing);cur;cur = lob_next(cur)) len += lob_len(cur);
  return len;
}

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall util_chunks_write(__int64 a1)
{
__int64 result; // rax

if ( !(unsigned int)util_chunks_len(a1) )
return 0LL;
result = a1 + 26;
if ( *(_WORD *)(a1 + 24) )
result = *(_QWORD *)(a1 + 16) + *(unsigned __int16 *)(a1 + 24) + lob_raw(*(_QWORD *)(a1 + 8)) - 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax

if ( !(unsigned int)func6(a1) )
return 0LL;
result = a1 + 26;
if ( *(_WORD *)(a1 + 24) )
result = *(_QWORD *)(a1 + 16) + *(unsigned __int16 *)(a1 + 24) + lob_raw(*(_QWORD *)(a1 + 8)) - 1;
return result;
}

original_code:
uint8_t *util_chunks_write(util_chunks_t chunks)
{
  if(!util_chunks_len(chunks)) return ((void *)0);
  if(!chunks->waitat) return &chunks->waiting;
  return lob_raw(chunks->writing)+chunks->writeat+(chunks->waitat-1);
}

canonical_original_code:
unsigned char *util_chunks_write(struct util_chunks_struct *chunks)
{
  if(!util_chunks_len(chunks)) return ((void *)0);
  if(!chunks->waitat) return &chunks->waiting;
  return lob_raw(chunks->writing)+chunks->writeat+(chunks->waitat-1);
}


original_code:
util_chunks_t util_chunks_written(util_chunks_t chunks, size_t len)
{
  if(!chunks || !len) return chunks;
  if(len > util_chunks_len(chunks)) return util_sys_log(7, "src/util/chunks.c", 216, __func__, "len too big %d > %d",len,util_chunks_len(chunks));
  chunks->waitat += len;
  chunks->ack = 0;
  if(chunks->waitat > chunks->waiting)
  {
    chunks->writeat += chunks->waiting;
    chunks->waiting = chunks->waitat = 0;
    if(chunks->waiting == chunks->cap) chunks->blocked = chunks->blocking;
    if(len == 1 && chunks->writing && chunks->writeat == lob_len(chunks->writing))
    {
      lob_t old = lob_shift(chunks->writing);
      chunks->writing = old->next;
      old->next = ((void *)0);
      lob_free(old);
      chunks->writeat = 0;
      chunks->blocked = chunks->blocking;
    }
  }
  return chunks;
}

canonical_original_code:
struct util_chunks_struct *util_chunks_written(struct util_chunks_struct *chunks, unsigned long len)
{
  if(!chunks || !len) return chunks;
  if(len > util_chunks_len(chunks)) return util_sys_log(7, "src/util/chunks.c", 216, __func__, "len too big %d > %d",len,util_chunks_len(chunks));
  chunks->waitat += len;
  chunks->ack = 0;
  if(chunks->waitat > chunks->waiting)
  {
    chunks->writeat += chunks->waiting;
    chunks->waiting = chunks->waitat = 0;
    if(chunks->waiting == chunks->cap) chunks->blocked = chunks->blocking;
    if(len == 1 && chunks->writing && chunks->writeat == lob_len(chunks->writing))
    {
      struct lob_struct *old = lob_shift(chunks->writing);
      chunks->writing = old->next;
      old->next = ((void *)0);
      lob_free(old);
      chunks->writeat = 0;
      chunks->blocked = chunks->blocking;
    }
  }
  return chunks;
}


decompiled_code:
__int64 __fastcall util_chunks_size(__int64 a1)
{
__int64 result; // rax

if ( (unsigned int)util_chunks_len(a1) )
result = *(unsigned __int8 *)(a1 + 26);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 result; // rax

if ( (unsigned int)func6(a1) )
result = *(unsigned __int8 *)(a1 + 26);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int16_t util_chunks_size(util_chunks_t chunks)
{
  if(!util_chunks_len(chunks)) return -1;
  return chunks->waiting;
}

canonical_original_code:
short util_chunks_size(struct util_chunks_struct *chunks)
{
  if(!util_chunks_len(chunks)) return -1;
  return chunks->waiting;
}

return_type: short


decompiled_code:
__int64 __fastcall util_chunks_frame(__int64 a1)
{
__int64 result; // rax

if ( !a1 )
return 0LL;
result = 0LL;
if ( *(_BYTE *)(a1 + 26) )
result = *(_QWORD *)(a1 + 16) + lob_raw(*(_QWORD *)(a1 + 8));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
__int64 result; // rax

if ( !a1 )
return 0LL;
result = 0LL;
if ( *(_BYTE *)(a1 + 26) )
result = *(_QWORD *)(a1 + 16) + lob_raw(*(_QWORD *)(a1 + 8));
return result;
}

original_code:
uint8_t *util_chunks_frame(util_chunks_t chunks)
{
  if(!chunks || !chunks->waiting) return ((void *)0);
  return lob_raw(chunks->writing)+chunks->writeat;
}

canonical_original_code:
unsigned char *util_chunks_frame(struct util_chunks_struct *chunks)
{
  if(!chunks || !chunks->waiting) return ((void *)0);
  return lob_raw(chunks->writing)+chunks->writeat;
}


decompiled_code:
__int64 __fastcall util_chunks_peek(__int64 a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax

v1 = util_chunks_size(a1);
if ( (__int16)v1 <= 0 )
return 0xFFFFFFFFLL;
v2 = v1;
v3 = lob_len(*(_QWORD *)(a1 + 8));
LOWORD(v2) = *(_WORD *)(a1 + 16) + v2;
return (unsigned int)(v3 - v2);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax

v1 = func10(a1);
if ( (__int16)v1 <= 0 )
return 0xFFFFFFFFLL;
v2 = v1;
v3 = lob_len(*(_QWORD *)(a1 + 8));
LOWORD(v2) = *(_WORD *)(a1 + 16) + v2;
return (unsigned int)(v3 - v2);
}

original_code:
int16_t util_chunks_peek(util_chunks_t chunks)
{
  int16_t size = util_chunks_size(chunks);
  if(size <= 0) return -1;
  return lob_len(chunks->writing) - (chunks->writeat+size);
}

canonical_original_code:
short util_chunks_peek(struct util_chunks_struct *chunks)
{
  short size = util_chunks_size(chunks);
  if(size <= 0) return -1;
  return lob_len(chunks->writing) - (chunks->writeat+size);
}

variable_types: size short


decompiled_code:
__int64 __fastcall util_chunks_next(__int64 a1)
{
__int64 v1; // rbx
__int16 v2; // ax
__int16 v3; // bp

v1 = a1;
v2 = util_chunks_size(a1);
if ( v2 < 0 )
return 0LL;
v3 = v2;
if ( !util_chunks_written(a1, 1uLL) )
return 0LL;
if ( !util_chunks_written(a1, v3) )
v1 = 0LL;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
__int64 v1; // rbx
__int16 v2; // ax
__int16 v3; // bp

v1 = a1;
v2 = func10(a1);
if ( v2 < 0 )
return 0LL;
v3 = v2;
if ( !func9(a1, 1uLL) )
return 0LL;
if ( !func9(a1, v3) )
v1 = 0LL;
return v1;
}

original_code:
util_chunks_t util_chunks_next(util_chunks_t chunks)
{
  int16_t size = util_chunks_size(chunks);
  if(size < 0) return ((void *)0);
  if(!util_chunks_written(chunks,1)) return ((void *)0);
  if(!util_chunks_written(chunks,size)) return ((void *)0);
  return chunks;
}

canonical_original_code:
struct util_chunks_struct *util_chunks_next(struct util_chunks_struct *chunks)
{
  short size = util_chunks_size(chunks);
  if(size < 0) return ((void *)0);
  if(!util_chunks_written(chunks,1)) return ((void *)0);
  if(!util_chunks_written(chunks,size)) return ((void *)0);
  return chunks;
}