decompiled_code:
__int64 __fastcall topology_parseline(char *a1, unsigned int a2, int *a3, int *a4, _DWORD *a5)
{
unsigned __int64 v5; // rax
char *v9; // rdx
char *v10; // rdx
char *v11; // rdi
char v12; // dl
char *v13; // rax
unsigned __int64 v14; // rdx
char *v15; // rax
__int64 v16; // rax
__int64 result; // rax
__int64 v18; // r8
char *endptr; // [rsp+0h] [rbp-20h] BYREF

v5 = (unsigned __int8)*a1;
if ( (_BYTE)v5 == 35 )
return 0xFFFFFFFFLL;
endptr = a1;
if ( (_BYTE)v5 == 9 || (_BYTE)v5 == 32 )
{
v9 = a1 + 1;
do
{
do
{
v5 = (unsigned __int8)*v9;
a1 = v9++;
}
while ( (_BYTE)v5 == 32 );
}
while ( (_BYTE)v5 == 9 );
endptr = a1;
}
v10 = a1;
if ( (v5 & 0xDF) != 0 )
{
while ( 1 )
{
endptr = ++v10;
v5 = (unsigned __int8)*v10;
if ( (v5 & 0xDF) == 0 )
break;
if ( (_BYTE)v5 == 9 )
goto LABEL_11;
}
}
if ( (unsigned __int8)v5 <= 0xDu && (v18 = 9217LL, _bittest64(&v18, v5)) )
{
syslog(4, "mfstopology: incomplete definition in line: %u", a2);
fprintf(stderr, "mfstopology: incomplete definition in line: %u\n", a2);
fprintf(stderr, "mfstopology: incomplete definition in line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_11:
*v10 = 0;
++endptr;
if ( (int)topology_parsenet(a1, a3, a4) < 0 )
{
syslog(4, "mfstopology: incorrect ip/network definition in line: %u", a2);
fprintf(stderr, "mfstopology: incorrect ip/network definition in line: %u\n", a2);
fprintf(stderr, "mfstopology: incorrect ip/network definition in line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v11 = endptr;
v12 = *endptr;
if ( *endptr == 9 || v12 == 32 )
{
v13 = endptr + 1;
do
{
do
{
endptr = v13;
v12 = *v13;
v11 = v13++;
}
while ( v12 == 32 );
}
while ( v12 == 9 );
}
if ( (unsigned __int8)(v12 - 48) > 9u )
{
syslog(4, "mfstopology: incorrect rack id in line: %u", a2);
fprintf(stderr, "mfstopology: incorrect rack id in line: %u\n", a2);
fprintf(stderr, "mfstopology: incorrect rack id in line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
*a5 = strtoul(v11, &endptr, 10);
v14 = (unsigned __int8)*endptr;
if ( (_BYTE)v14 == 32 || (_BYTE)v14 == 9 )
{
v15 = endptr + 1;
do
{
do
{
endptr = v15;
v14 = (unsigned __int8)*v15++;
}
while ( (_BYTE)v14 == 32 );
}
while ( (_BYTE)v14 == 9 );
}
if ( (unsigned __int8)v14 <= 0x23u && (v16 = 0x800002401LL, _bittest64(&v16, v14)) )
{
result = 0LL;
}
else
{
syslog(4, "mfstopology: garbage found at the end of line: %u", a2);
fprintf(stderr, "mfstopology: garbage found at the end of line: %u\n", a2);
fprintf(stderr, "mfstopology: garbage found at the end of line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, unsigned int a2, int *a3, int *a4, _DWORD *a5)
{
unsigned __int64 v5; // rax
char *v9; // rdx
char *v10; // rdx
char *v11; // rdi
char v12; // dl
char *v13; // rax
unsigned __int64 v14; // rdx
char *v15; // rax
__int64 v16; // rax
__int64 result; // rax
__int64 v18; // r8
char *endptr; // [rsp+0h] [rbp-20h] BYREF

v5 = (unsigned __int8)*a1;
if ( (_BYTE)v5 == 35 )
return 0xFFFFFFFFLL;
endptr = a1;
if ( (_BYTE)v5 == 9 || (_BYTE)v5 == 32 )
{
v9 = a1 + 1;
do
{
do
{
v5 = (unsigned __int8)*v9;
a1 = v9++;
}
while ( (_BYTE)v5 == 32 );
}
while ( (_BYTE)v5 == 9 );
endptr = a1;
}
v10 = a1;
if ( (v5 & 0xDF) != 0 )
{
while ( 1 )
{
endptr = ++v10;
v5 = (unsigned __int8)*v10;
if ( (v5 & 0xDF) == 0 )
break;
if ( (_BYTE)v5 == 9 )
goto LABEL_11;
}
}
if ( (unsigned __int8)v5 <= 0xDu && (v18 = 9217LL, _bittest64(&v18, v5)) )
{
syslog(4, "mfstopology: incomplete definition in line: %u", a2);
fprintf(stderr, "mfstopology: incomplete definition in line: %u\n", a2);
fprintf(stderr, "mfstopology: incomplete definition in line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_11:
*v10 = 0;
++endptr;
if ( (int)func1(a1, a3, a4) < 0 )
{
syslog(4, "mfstopology: incorrect ip/network definition in line: %u", a2);
fprintf(stderr, "mfstopology: incorrect ip/network definition in line: %u\n", a2);
fprintf(stderr, "mfstopology: incorrect ip/network definition in line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v11 = endptr;
v12 = *endptr;
if ( *endptr == 9 || v12 == 32 )
{
v13 = endptr + 1;
do
{
do
{
endptr = v13;
v12 = *v13;
v11 = v13++;
}
while ( v12 == 32 );
}
while ( v12 == 9 );
}
if ( (unsigned __int8)(v12 - 48) > 9u )
{
syslog(4, "mfstopology: incorrect rack id in line: %u", a2);
fprintf(stderr, "mfstopology: incorrect rack id in line: %u\n", a2);
fprintf(stderr, "mfstopology: incorrect rack id in line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
*a5 = strtoul(v11, &endptr, 10);
v14 = (unsigned __int8)*endptr;
if ( (_BYTE)v14 == 32 || (_BYTE)v14 == 9 )
{
v15 = endptr + 1;
do
{
do
{
endptr = v15;
v14 = (unsigned __int8)*v15++;
}
while ( (_BYTE)v14 == 32 );
}
while ( (_BYTE)v14 == 9 );
}
if ( (unsigned __int8)v14 <= 0x23u && (v16 = 0x800002401LL, _bittest64(&v16, v14)) )
{
result = 0LL;
}
else
{
syslog(4, "mfstopology: garbage found at the end of line: %u", a2);
fprintf(stderr, "mfstopology: garbage found at the end of line: %u\n", a2);
fprintf(stderr, "mfstopology: garbage found at the end of line: %u\n", a2);
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

original_code:
int topology_parseline(char *line,uint32_t lineno,uint32_t *fip,uint32_t *tip,uint32_t *rid) {
 char *net;
 char *p;
 if (*line=='#') {
  return -1;
 }
 p = line;
 while (*p==' ' || *p=='\t') {
  p++;
 }
 net = p;
 while (*p && *p!=' ' && *p!='\t') {
  p++;
 }
 if (*p==0 || *p=='\r' || *p=='\n') {
  { syslog((4),("mfstopology: incomplete definition in line: %""u"), lineno); fprintf(stderr,"mfstopology: incomplete definition in line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: incomplete definition in line: %""u""\n",lineno);
  return -1;
 }
 *p=0;
 p++;
 if (topology_parsenet(net,fip,tip)<0) {
  { syslog((4),("mfstopology: incorrect ip/network definition in line: %""u"), lineno); fprintf(stderr,"mfstopology: incorrect ip/network definition in line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: incorrect ip/network definition in line: %""u""\n",lineno);
  return -1;
 }
 while (*p==' ' || *p=='\t') {
  p++;
 }
 if (*p<'0' || *p>'9') {
  { syslog((4),("mfstopology: incorrect rack id in line: %""u"), lineno); fprintf(stderr,"mfstopology: incorrect rack id in line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: incorrect rack id in line: %""u""\n",lineno);
  return -1;
 }
 *rid = strtoul(p,&p,10);
 while (*p==' ' || *p=='\t') {
  p++;
 }
 if (*p && *p!='\r' && *p!='\n' && *p!='#') {
  { syslog((4),("mfstopology: garbage found at the end of line: %""u"), lineno); fprintf(stderr,"mfstopology: garbage found at the end of line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: garbage found at the end of line: %""u""\n",lineno);
  return -1;
 }
 return 0;
}

canonical_original_code:
int topology_parseline(char *line, unsigned int lineno, unsigned int *fip, unsigned int *tip, unsigned int *rid) {
 char *net;
 char *p;
 if (*line=='#') {
  return -1;
 }
 p = line;
 while (*p==' ' || *p=='\t') {
  p++;
 }
 net = p;
 while (*p && *p!=' ' && *p!='\t') {
  p++;
 }
 if (*p==0 || *p=='\r' || *p=='\n') {
  { syslog((4),("mfstopology: incomplete definition in line: %""u"), lineno); fprintf(stderr,"mfstopology: incomplete definition in line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: incomplete definition in line: %""u""\n",lineno);
  return -1;
 }
 *p=0;
 p++;
 if (topology_parsenet(net,fip,tip)<0) {
  { syslog((4),("mfstopology: incorrect ip/network definition in line: %""u"), lineno); fprintf(stderr,"mfstopology: incorrect ip/network definition in line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: incorrect ip/network definition in line: %""u""\n",lineno);
  return -1;
 }
 while (*p==' ' || *p=='\t') {
  p++;
 }
 if (*p<'0' || *p>'9') {
  { syslog((4),("mfstopology: incorrect rack id in line: %""u"), lineno); fprintf(stderr,"mfstopology: incorrect rack id in line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: incorrect rack id in line: %""u""\n",lineno);
  return -1;
 }
 *rid = strtoul(p,&p,10);
 while (*p==' ' || *p=='\t') {
  p++;
 }
 if (*p && *p!='\r' && *p!='\n' && *p!='#') {
  { syslog((4),("mfstopology: garbage found at the end of line: %""u"), lineno); fprintf(stderr,"mfstopology: garbage found at the end of line: %""u" "\n", lineno); };
  fprintf(stderr,"mfstopology: garbage found at the end of line: %""u""\n",lineno);
  return -1;
 }
 return 0;
}

variable_types:
line
char *

variable_types:
lineno
unsigned int

variable_types:
fip
unsigned int *

variable_types:
tip
unsigned int *

variable_types:
rid
unsigned int *

variable_types:
p
char *


repo: twonly/mfs

decompiled_code:
int topology_reload()
{
int v0; // ebp

if ( TopologyFileName )
free(TopologyFileName);
if ( (unsigned int)cfg_isdefined("TOPOLOGY_FILENAME") )
{
TopologyFileName = (char *)cfg_getstr("TOPOLOGY_FILENAME", "/etc/mfs/mfstopology.cfg");
return topology_load();
}
TopologyFileName = strdup("/etc/mfs/mfstopology.cfg");
if ( !TopologyFileName )
{
fprintf(stderr, "out of memory: %s is NULL\n", "TopologyFileName");
syslog(3, "out of memory: %s is NULL", "TopologyFileName");
abort();
}
v0 = open64(TopologyFileName, 0);
if ( v0 >= 0 )
goto LABEL_6;
if ( *_errno_location() == 2 )
{
free(TopologyFileName);
TopologyFileName = strdup("/etc/mfstopology.cfg");
v0 = open64(TopologyFileName, 0);
if ( v0 >= 0 )
{
syslog(4, "%s", "default sysconf path has changed - please move mfstopology.cfg from /etc/ to /etc/mfs/");
fprintf(stderr, "%s\n", "default sysconf path has changed - please move mfstopology.cfg from /etc/ to /etc/mfs/");
LABEL_6:
close(v0);
return topology_load();
}
}
return topology_load();
}

canonical_decompiled_code:
int func5()
{
int v0; // ebp

if ( TopologyFileName )
free(TopologyFileName);
if ( (unsigned int)cfg_isdefined("TOPOLOGY_FILENAME") )
{
TopologyFileName = (char *)cfg_getstr("TOPOLOGY_FILENAME", "/etc/mfs/mfstopology.cfg");
return func4();
}
TopologyFileName = strdup("/etc/mfs/mfstopology.cfg");
if ( !TopologyFileName )
{
fprintf(stderr, "out of memory: %s is NULL\n", "TopologyFileName");
syslog(3, "out of memory: %s is NULL", "TopologyFileName");
abort();
}
v0 = open64(TopologyFileName, 0);
if ( v0 >= 0 )
goto LABEL_6;
if ( *_errno_location() == 2 )
{
free(TopologyFileName);
TopologyFileName = strdup("/etc/mfstopology.cfg");
v0 = open64(TopologyFileName, 0);
if ( v0 >= 0 )
{
syslog(4, "%s", "default sysconf path has changed - please move mfstopology.cfg from /etc/ to /etc/mfs/");
fprintf(stderr, "%s\n", "default sysconf path has changed - please move mfstopology.cfg from /etc/ to /etc/mfs/");
LABEL_6:
close(v0);
return func4();
}
}
return func4();
}

original_code:
void topology_reload(void) {
 int fd;
 if (TopologyFileName) {
  free(TopologyFileName);
 }
 if (!cfg_isdefined("TOPOLOGY_FILENAME")) {
  TopologyFileName = strdup("/etc" "/mfs/mfstopology.cfg");
  ((TopologyFileName!=((void *)0)) ? (void)0 : (fprintf(stderr,"out of memory: %s is NULL\n","TopologyFileName"),syslog(3,"out of memory: %s is NULL","TopologyFileName"),abort()));
  if ((fd = open(TopologyFileName,00))<0 && (*__errno_location ())==2) {
   free(TopologyFileName);
   TopologyFileName = strdup("/etc" "/mfstopology.cfg");
   if ((fd = open(TopologyFileName,00))>=0) {
    { syslog((4),"%s",("default sysconf path has changed - please move mfstopology.cfg from ""/etc""/ to ""/etc""/mfs/")); fprintf(stderr,"%s\n",("default sysconf path has changed - please move mfstopology.cfg from ""/etc""/ to ""/etc""/mfs/")); };
   }
  }
  if (fd>=0) {
   close(fd);
  }
 } else {
  TopologyFileName = cfg_getstr("TOPOLOGY_FILENAME","/etc" "/mfs/mfstopology.cfg");
 }
 topology_load();
}

canonical_original_code:
void topology_reload() {
 int fd;
 if (TopologyFileName) {
  free(TopologyFileName);
 }
 if (!cfg_isdefined("TOPOLOGY_FILENAME")) {
  TopologyFileName = strdup("/etc" "/mfs/mfstopology.cfg");
  ((TopologyFileName!=((void *)0)) ? (void)0 : (fprintf(stderr,"out of memory: %s is NULL\n","TopologyFileName"),syslog(3,"out of memory: %s is NULL","TopologyFileName"),abort()));
  if ((fd = open(TopologyFileName,00))<0 && (*__errno_location ())==2) {
   free(TopologyFileName);
   TopologyFileName = strdup("/etc" "/mfstopology.cfg");
   if ((fd = open(TopologyFileName,00))>=0) {
    { syslog((4),"%s",("default sysconf path has changed - please move mfstopology.cfg from ""/etc""/ to ""/etc""/mfs/")); fprintf(stderr,"%s\n",("default sysconf path has changed - please move mfstopology.cfg from ""/etc""/ to ""/etc""/mfs/")); };
   }
  }
  if (fd>=0) {
   close(fd);
  }
 } else {
  TopologyFileName = cfg_getstr("TOPOLOGY_FILENAME","/etc" "/mfs/mfstopology.cfg");
 }
 topology_load();
}

variable_types: fd int

return_type: void


decompiled_code:
__int64 topology_init()
{
TopologyFileName = 0LL;
racktree = 0LL;
topology_reload();
main_reloadregister(topology_reload);
main_destructregister(topology_term);
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
TopologyFileName = 0LL;
racktree = 0LL;
func5();
main_reloadregister(topology_reload);
main_destructregister(topology_term);
return 0LL;
}

original_code:
int topology_init(void) {
 TopologyFileName = ((void *)0);
 racktree = ((void *)0);
 topology_reload();
 main_reloadregister(topology_reload);
 main_destructregister(topology_term);
 return 0;
}

canonical_original_code:
int topology_init() {
 TopologyFileName = ((void *)0);
 racktree = ((void *)0);
 topology_reload();
 main_reloadregister(topology_reload);
 main_destructregister(topology_term);
 return 0;
}

return_type: int


mfsstrerr

decompiled_code:
char *__fastcall mfsstrerr(unsigned __int8 a1)
{
if ( a1 > 0x29u )
a1 = 41;
return errtab_0[a1];
}

canonical_decompiled_code:
char *__fastcall func0(unsigned __int8 a1)
{
if ( a1 > 0x29u )
a1 = 41;
return errtab_0[a1];
}

original_code:
static inline const char* mfsstrerr(uint8_t status) {
 static const char* errtab[]={"OK", "Operation not permitted", "Not a directory", "No such file or directory", "Permission denied", "File exists", "Invalid argument", "Directory not empty", "Chunk lost", "Out of memory", "Index too big", "Chunk locked", "No chunk servers", "No such chunk", "Chunk is busy", "Incorrect register BLOB", "None of chunk servers performed requested operation", "File not opened", "Write not started", "Wrong chunk version", "Chunk already exists", "No space left", "IO error", "Incorrect block number", "Incorrect size", "Incorrect offset", "Can't connect", "Incorrect chunk id", "Disconnected", "CRC error", "Operation delayed", "Can't create path", "Data mismatch", "Read-only file system", "Quota exceeded", "Bad session id", "Password is needed", "Incorrect password", "Attribute not found", "Operation not supported", "Result too large", "Unknown MFS error"};
 if (status>41) {
  status=41;
 }
 return errtab[status];
}

canonical_original_code:
static inline char *mfsstrerr(unsigned char status) {
 static char *errtab[] = {"OK", "Operation not permitted", "Not a directory", "No such file or directory", "Permission denied", "File exists", "Invalid argument", "Directory not empty", "Chunk lost", "Out of memory", "Index too big", "Chunk locked", "No chunk servers", "No such chunk", "Chunk is busy", "Incorrect register BLOB", "None of chunk servers performed requested operation", "File not opened", "Write not started", "Wrong chunk version", "Chunk already exists", "No space left", "IO error", "Incorrect block number", "Incorrect size", "Incorrect offset", "Can't connect", "Incorrect chunk id", "Disconnected", "CRC error", "Operation delayed", "Can't create path", "Data mismatch", "Read-only file system", "Quota exceeded", "Bad session id", "Password is needed", "Incorrect password", "Attribute not found", "Operation not supported", "Result too large", "Unknown MFS error"};
 if (status>41) {
  status=41;
 }
 return errtab[status];
}

variable_types:
status
unsigned char

variable_types:
errtab
char *[]

return_type: char *


canonical_original_code:
static inline unsigned int get32bit(unsigned char **ptr) {
 unsigned int t32;
 t32=((*ptr)[3]+256U*((*ptr)[2]+256U*((*ptr)[1]+256U*(*ptr)[0])));
 (*ptr)+=4;
 return t32;
}

variable_types:
ptr
unsigned char **

return_type: unsigned int


decompiled_code:
__int64 __fastcall get16bit(unsigned __int8 **a1)
{
int v1; // eax
int v2; // ecx

v1 = **a1;
v2 = (*a1)[1];
*a1 += 2;
return (unsigned int)(v2 + (v1 << 8));
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 **a1)
{
int v1; // eax
int v2; // ecx

v1 = **a1;
v2 = (*a1)[1];
*a1 += 2;
return (unsigned int)(v2 + (v1 << 8));
}

original_code:
static inline uint16_t get16bit(const uint8_t **ptr) {
 uint32_t t16;
 t16=(*ptr)[1]+256U*(*ptr)[0];
 (*ptr)+=2;
 return t16;
}

canonical_original_code:
static inline unsigned short get16bit(unsigned char **ptr) {
 unsigned int t16;
 t16=(*ptr)[1]+256U*(*ptr)[0];
 (*ptr)+=2;
 return t16;
}

return_type: unsigned short


decompiled_code:
void *__fastcall read_data_delayed_ops(void *a1)
{
__int64 *v1; // rbp
void **v2; // rbx
unsigned __int8 v3; // al
char v4; // al
__int64 *v5; // rdx
__int64 *i; // rax

while ( 1 )
{
pthread_mutex_lock(&glock);
if ( rterm )
break;
v1 = (__int64 *)rdhead;
v2 = &rdhead;
if ( rdhead )
{
do
{
v3 = *((_BYTE *)v1 + 56);
if ( v3 <= 0xEu )
*((_BYTE *)v1 + 56) = v3 + 1;
if ( !*((_BYTE *)v1 + 59) )
{
if ( !*((_BYTE *)v1 + 58) )
{
pthread_cond_destroy((pthread_cond_t *)(v1 + 8));
*v2 = (void *)v1[14];
v5 = &rdinodemap[*((_DWORD *)v1 + 3) & 0x3FF];
for ( i = (__int64 *)*v5; i; *v5 = (__int64)i )
{
while ( i != v1 )
{
v5 = i + 15;
i = (__int64 *)i[15];
if ( !i )
goto LABEL_17;
}
i = (__int64 *)i[15];
}
LABEL_17:
free(v1);
goto LABEL_11;
}
if ( *((int *)v1 + 13) >= 0 )
{
v4 = *((_BYTE *)v1 + 57);
if ( v4 == 3 )
{
csdb_readdec(*((unsigned int *)v1 + 11), *((unsigned __int16 *)v1 + 24));
tcpclose(*((unsigned int *)v1 + 13));
*((_DWORD *)v1 + 13) = -1;
}
else
{
*((_BYTE *)v1 + 57) = v4 + 1;
}
}
}
v2 = (void **)(v1 + 14);
LABEL_11:
v1 = (__int64 *)*v2;
}
while ( *v2 );
}
pthread_mutex_unlock(&glock);
usleep(0x51615u);
}
pthread_mutex_unlock(&glock);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(void *a1)
{
__int64 *v1; // rbp
void **v2; // rbx
unsigned __int8 v3; // al
char v4; // al
__int64 *v5; // rdx
__int64 *i; // rax

while ( 1 )
{
pthread_mutex_lock(&glock);
if ( rterm )
break;
v1 = (__int64 *)rdhead;
v2 = &rdhead;
if ( rdhead )
{
do
{
v3 = *((_BYTE *)v1 + 56);
if ( v3 <= 0xEu )
*((_BYTE *)v1 + 56) = v3 + 1;
if ( !*((_BYTE *)v1 + 59) )
{
if ( !*((_BYTE *)v1 + 58) )
{
pthread_cond_destroy((pthread_cond_t *)(v1 + 8));
*v2 = (void *)v1[14];
v5 = &rdinodemap[*((_DWORD *)v1 + 3) & 0x3FF];
for ( i = (__int64 *)*v5; i; *v5 = (__int64)i )
{
while ( i != v1 )
{
v5 = i + 15;
i = (__int64 *)i[15];
if ( !i )
goto LABEL_17;
}
i = (__int64 *)i[15];
}
LABEL_17:
free(v1);
goto LABEL_11;
}
if ( *((int *)v1 + 13) >= 0 )
{
v4 = *((_BYTE *)v1 + 57);
if ( v4 == 3 )
{
csdb_readdec(*((unsigned int *)v1 + 11), *((unsigned __int16 *)v1 + 24));
tcpclose(*((unsigned int *)v1 + 13));
*((_DWORD *)v1 + 13) = -1;
}
else
{
*((_BYTE *)v1 + 57) = v4 + 1;
}
}
}
v2 = (void **)(v1 + 14);
LABEL_11:
v1 = (__int64 *)*v2;
}
while ( *v2 );
}
pthread_mutex_unlock(&glock);
usleep(0x51615u);
}
pthread_mutex_unlock(&glock);
return 0LL;
}

original_code:
void* read_data_delayed_ops(void *arg) {
 readrec *rrec,**rrecp;
 readrec **rrecmap;
 (void)arg;
 for (;;) {
  pthread_mutex_lock(&glock);
  if (rterm) {
   pthread_mutex_unlock(&glock);
   return ((void *)0);
  }
  rrecp = &rdhead;
  while ((rrec=*rrecp)!=((void *)0)) {
   if (rrec->refcnt<15) {
    rrec->refcnt++;
   }
   if (rrec->locked==0) {
    if (rrec->valid==0) {
     pthread_cond_destroy(&(rrec->cond));
     *rrecp = rrec->next;
     rrecmap = &(rdinodemap[(rrec->inode&((1<<(10))-1))]);
     while (*rrecmap) {
      if ((*rrecmap)==rrec) {
       *rrecmap = rrec->mapnext;
      } else {
       rrecmap = &((*rrecmap)->mapnext);
      }
     }
     free(rrec);
    } else {
     if (rrec->fd>=0) {
      if (rrec->noaccesscnt==3) {
       csdb_readdec(rrec->ip,rrec->port);
       tcpclose(rrec->fd);
       rrec->fd=-1;
      } else {
       rrec->noaccesscnt++;
      }
     }
     rrecp = &(rrec->next);
    }
   } else {
    rrecp = &(rrec->next);
   }
  }
  pthread_mutex_unlock(&glock);
  usleep(333333);
 }
}

canonical_original_code:
void *read_data_delayed_ops(void *arg) {
 struct _readrec *rrec; struct _readrec **rrecp;
 struct _readrec **rrecmap;
 (void)arg;
 for (;;) {
  pthread_mutex_lock(&glock);
  if (rterm) {
   pthread_mutex_unlock(&glock);
   return ((void *)0);
  }
  rrecp = &rdhead;
  while ((rrec=*rrecp)!=((void *)0)) {
   if (rrec->refcnt<15) {
    rrec->refcnt++;
   }
   if (rrec->locked==0) {
    if (rrec->valid==0) {
     pthread_cond_destroy(&(rrec->cond));
     *rrecp = rrec->next;
     rrecmap = &(rdinodemap[(rrec->inode&((1<<(10))-1))]);
     while (*rrecmap) {
      if ((*rrecmap)==rrec) {
       *rrecmap = rrec->mapnext;
      } else {
       rrecmap = &((*rrecmap)->mapnext);
      }
     }
     free(rrec);
    } else {
     if (rrec->fd>=0) {
      if (rrec->noaccesscnt==3) {
       csdb_readdec(rrec->ip,rrec->port);
       tcpclose(rrec->fd);
       rrec->fd=-1;
      } else {
       rrec->noaccesscnt++;
      }
     }
     rrecp = &(rrec->next);
    }
   } else {
    rrecp = &(rrec->next);
   }
  }
  pthread_mutex_unlock(&glock);
  usleep(333333);
 }
}

variable_types:
arg
void *

variable_types: struct _readrec { unsigned char *rbuff; unsigned int rbuffsize; unsigned int inode; unsigned long fleng; unsigned int indx; unsigned long chunkid; unsigned int version; unsigned int ip; unsigned short port; int fd; unsigned char refcnt; unsigned char noaccesscnt; unsigned char valid; unsigned char locked; unsigned short waiting; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; struct _readrec *next; struct _readrec *mapnext; } **

return_type: void *

user_defined_types: struct _readrec { unsigned char *rbuff; unsigned int rbuffsize; unsigned int inode; unsigned long fleng; unsigned int indx; unsigned long chunkid; unsigned int version; unsigned int ip; unsigned short port; int fd; unsigned char refcnt; unsigned char noaccesscnt; unsigned char valid; unsigned char locked; unsigned short waiting; union pthread_cond_t cond; struct _readrec *next; struct _readrec *mapnext; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }


decompiled_code:
unsigned __int8 *__fastcall read_data_refresh_connection(__int64 a1)
{
unsigned __int8 v2; // al
unsigned __int8 v3; // bp
__int64 v4; // r8
unsigned __int8 *result; // rax
unsigned int v6; // er15
unsigned __int16 v7; // r14
unsigned int v8; // er13
unsigned int v9; // ebp
unsigned __int16 v10; // ax
unsigned __int16 v11; // r12
unsigned int v12; // eax
unsigned int v13; // ebp
unsigned int v14; // er12
unsigned int v15; // er8
int v16; // eax
__int64 v17; // rdi
char *v18; // rax
int *v19; // rax
const char *v20; // rax
int *v21; // rax
const char *v22; // rax
int *v23; // rax
const char *v24; // rax
int *v25; // rax
const char *v26; // rax
unsigned int v27; // [rsp+4h] [rbp-44h] BYREF
unsigned __int8 *v28[8]; // [rsp+8h] [rbp-40h] BYREF

if ( *(int *)(a1 + 52) >= 0 )
{
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
v2 = fs_readchunk(*(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 24), a1 + 16, a1 + 32, a1 + 40, v28, &v27);
v3 = v2;
if ( v2 )
{
v18 = mfsstrerr(v2);
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - fs_readchunk returns status: %s",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40),
v18);
result = (_BYTE *)&loc_9;
if ( v3 != 3 )
return (_BYTE *)(&loc_4 + 1);
return result;
}
v4 = *(_QWORD *)(a1 + 32);
result = v28[0];
if ( !v4 )
{
if ( v28[0] )
goto LABEL_6;
if ( !v27 )
return result;
LABEL_27:
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - there are no valid copies",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
v4,
*(unsigned int *)(a1 + 40));
return (_BYTE *)(&loc_4 + 2);
}
if ( !v28[0] )
goto LABEL_27;
LABEL_6:
if ( !v27 )
goto LABEL_27;
v6 = -1;
v7 = 0;
v8 = 0;
if ( v27 <= 5 )
goto LABEL_27;
do
{
v9 = get32bit(v28);
v10 = get16bit(v28);
v27 -= 6;
v11 = v10;
v12 = csdb_getopcnt(v9);
if ( v12 < v6 )
{
v6 = v12;
v7 = v11;
v8 = v9;
}
}
while ( v27 > 5 && v6 );
if ( !v8 || !v7 )
{
v4 = *(_QWORD *)(a1 + 32);
goto LABEL_27;
}
*(_DWORD *)(a1 + 44) = v8;
v13 = 0;
*(_WORD *)(a1 + 48) = v7;
v14 = fs_getsrcip();
while ( 1 )
{
v16 = tcpsocket();
*(_DWORD *)(a1 + 52) = v16;
v17 = (unsigned int)v16;
if ( v16 < 0 )
break;
if ( v14 )
{
if ( (int)tcpnumbind((unsigned int)v16, v14, 0LL) < 0 )
{
v25 = _errno_location();
v26 = (const char *)strerr((unsigned int)*v25);
syslog(4, "can't bind to given ip: %s", v26);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return (_BYTE *)(&loc_4 + 1);
}
v17 = *(unsigned int *)(a1 + 52);
}
v15 = 200 << (v13 >> 1);
if ( (v13 & 1) != 0 )
v15 = 300 << (v13 >> 1);
if ( (int)tcpnumtoconnect(v17, v8, v7, v15) >= 0 )
goto LABEL_33;
if ( ++v13 == 10 )
{
v19 = _errno_location();
v20 = (const char *)strerr((unsigned int)*v19);
syslog(4, "can't connect to (%08X:%u): %s", v8, v7, v20);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return (_BYTE *)(&loc_4 + 1);
}
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
v21 = _errno_location();
v22 = (const char *)strerr((unsigned int)*v21);
syslog(4, "can't create tcp socket: %s", v22);
LABEL_33:
if ( *(int *)(a1 + 52) < 0 )
return (_BYTE *)(&loc_4 + 1);
if ( (int)tcpnodelay() < 0 )
{
v23 = _errno_location();
v24 = (const char *)strerr((unsigned int)*v23);
syslog(4, "can't set TCP_NODELAY: %s", v24);
}
csdb_readinc(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
pthread_mutex_lock(&glock);
*(_BYTE *)(a1 + 56) = 0;
pthread_mutex_unlock(&glock);
return 0LL;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func4(__int64 a1)
{
unsigned __int8 v2; // al
unsigned __int8 v3; // bp
__int64 v4; // r8
unsigned __int8 *result; // rax
unsigned int v6; // er15
unsigned __int16 v7; // r14
unsigned int v8; // er13
unsigned int v9; // ebp
unsigned __int16 v10; // ax
unsigned __int16 v11; // r12
unsigned int v12; // eax
unsigned int v13; // ebp
unsigned int v14; // er12
unsigned int v15; // er8
int v16; // eax
__int64 v17; // rdi
char *v18; // rax
int *v19; // rax
const char *v20; // rax
int *v21; // rax
const char *v22; // rax
int *v23; // rax
const char *v24; // rax
int *v25; // rax
const char *v26; // rax
unsigned int v27; // [rsp+4h] [rbp-44h] BYREF
unsigned __int8 *v28[8]; // [rsp+8h] [rbp-40h] BYREF

if ( *(int *)(a1 + 52) >= 0 )
{
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
v2 = fs_readchunk(*(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 24), a1 + 16, a1 + 32, a1 + 40, v28, &v27);
v3 = v2;
if ( v2 )
{
v18 = func0(v2);
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - fs_readchunk returns status: %s",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40),
v18);
result = (_BYTE *)&loc_9;
if ( v3 != 3 )
return (_BYTE *)(&loc_4 + 1);
return result;
}
v4 = *(_QWORD *)(a1 + 32);
result = v28[0];
if ( !v4 )
{
if ( v28[0] )
goto LABEL_6;
if ( !v27 )
return result;
LABEL_27:
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - there are no valid copies",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
v4,
*(unsigned int *)(a1 + 40));
return (_BYTE *)(&loc_4 + 2);
}
if ( !v28[0] )
goto LABEL_27;
LABEL_6:
if ( !v27 )
goto LABEL_27;
v6 = -1;
v7 = 0;
v8 = 0;
if ( v27 <= 5 )
goto LABEL_27;
do
{
v9 = func1(v28);
v10 = func2(v28);
v27 -= 6;
v11 = v10;
v12 = csdb_getopcnt(v9);
if ( v12 < v6 )
{
v6 = v12;
v7 = v11;
v8 = v9;
}
}
while ( v27 > 5 && v6 );
if ( !v8 || !v7 )
{
v4 = *(_QWORD *)(a1 + 32);
goto LABEL_27;
}
*(_DWORD *)(a1 + 44) = v8;
v13 = 0;
*(_WORD *)(a1 + 48) = v7;
v14 = fs_getsrcip();
while ( 1 )
{
v16 = tcpsocket();
*(_DWORD *)(a1 + 52) = v16;
v17 = (unsigned int)v16;
if ( v16 < 0 )
break;
if ( v14 )
{
if ( (int)tcpnumbind((unsigned int)v16, v14, 0LL) < 0 )
{
v25 = _errno_location();
v26 = (const char *)strerr((unsigned int)*v25);
syslog(4, "can't bind to given ip: %s", v26);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return (_BYTE *)(&loc_4 + 1);
}
v17 = *(unsigned int *)(a1 + 52);
}
v15 = 200 << (v13 >> 1);
if ( (v13 & 1) != 0 )
v15 = 300 << (v13 >> 1);
if ( (int)tcpnumtoconnect(v17, v8, v7, v15) >= 0 )
goto LABEL_33;
if ( ++v13 == 10 )
{
v19 = _errno_location();
v20 = (const char *)strerr((unsigned int)*v19);
syslog(4, "can't connect to (%08X:%u): %s", v8, v7, v20);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return (_BYTE *)(&loc_4 + 1);
}
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
v21 = _errno_location();
v22 = (const char *)strerr((unsigned int)*v21);
syslog(4, "can't create tcp socket: %s", v22);
LABEL_33:
if ( *(int *)(a1 + 52) < 0 )
return (_BYTE *)(&loc_4 + 1);
if ( (int)tcpnodelay() < 0 )
{
v23 = _errno_location();
v24 = (const char *)strerr((unsigned int)*v23);
syslog(4, "can't set TCP_NODELAY: %s", v24);
}
csdb_readinc(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
pthread_mutex_lock(&glock);
*(_BYTE *)(a1 + 56) = 0;
pthread_mutex_unlock(&glock);
return 0LL;
}

original_code:
static int read_data_refresh_connection(readrec *rrec) {
 uint32_t ip,tmpip;
 uint16_t port,tmpport;
 uint32_t cnt,bestcnt;
 const uint8_t *csdata;
 uint32_t csdatasize;
 uint8_t status;
 uint32_t srcip;
 if (rrec->fd>=0) {
  csdb_readdec(rrec->ip,rrec->port);
  tcpclose(rrec->fd);
  rrec->fd = -1;
 }
 status = fs_readchunk(rrec->inode,rrec->indx,&(rrec->fleng),&(rrec->chunkid),&(rrec->version),&csdata,&csdatasize);
 if (status!=0) {
  syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"" - fs_readchunk returns status: %s",rrec->inode,rrec->indx,rrec->chunkid,rrec->version,mfsstrerr(status));
  if (status==3) {
   return 9;
  }
  return 5;
 }
 if (rrec->chunkid==0 && csdata==((void *)0) && csdatasize==0) {
  return 0;
 }
 if (csdata==((void *)0) || csdatasize==0) {
  syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"" - there are no valid copies",rrec->inode,rrec->indx,rrec->chunkid,rrec->version);
  return 6;
 }
 ip = 0;
 port = 0;
 bestcnt = 0xFFFFFFFF;
 while (csdatasize>=6 && bestcnt>0) {
  tmpip = get32bit(&csdata);
  tmpport = get16bit(&csdata);
  csdatasize-=6;
  cnt = csdb_getopcnt(tmpip,tmpport);
  if (cnt<bestcnt) {
   ip = tmpip;
   port = tmpport;
   bestcnt = cnt;
  }
 }
 if (ip==0 || port==0) {
  syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"" - there are no valid copies",rrec->inode,rrec->indx,rrec->chunkid,rrec->version);
  return 6;
 }
 rrec->ip = ip;
 rrec->port = port;
 srcip = fs_getsrcip();
 cnt=0;
 while (cnt<10) {
  rrec->fd = tcpsocket();
  if (rrec->fd<0) {
   syslog(4,"can't create tcp socket: %s",strerr((*__errno_location ())));
   break;
  }
  if (srcip) {
   if (tcpnumbind(rrec->fd,srcip,0)<0) {
    syslog(4,"can't bind to given ip: %s",strerr((*__errno_location ())));
    tcpclose(rrec->fd);
    rrec->fd=-1;
    break;
   }
  }
  if (tcpnumtoconnect(rrec->fd,ip,port,(cnt%2)?(300*(1<<(cnt>>1))):(200*(1<<(cnt>>1))))<0) {
   cnt++;
   if (cnt>=10) {
    syslog(4,"can't connect to (%08""X"":%""u""): %s",ip,port,strerr((*__errno_location ())));
   }
   tcpclose(rrec->fd);
   rrec->fd=-1;
  } else {
   cnt=10;
  }
 }
 if (rrec->fd<0) {
  return 5;
 }
 if (tcpnodelay(rrec->fd)<0) {
  syslog(4,"can't set TCP_NODELAY: %s",strerr((*__errno_location ())));
 }
 csdb_readinc(rrec->ip,rrec->port);
 pthread_mutex_lock(&glock);
 rrec->refcnt = 0;
 pthread_mutex_unlock(&glock);
 return 0;
}

canonical_original_code:
static int read_data_refresh_connection(struct _readrec *rrec) {
 unsigned int ip; unsigned int tmpip;
 unsigned short port; unsigned short tmpport;
 unsigned int cnt; unsigned int bestcnt;
 unsigned char *csdata;
 unsigned int csdatasize;
 unsigned char status;
 unsigned int srcip;
 if (rrec->fd>=0) {
  csdb_readdec(rrec->ip,rrec->port);
  tcpclose(rrec->fd);
  rrec->fd = -1;
 }
 status = fs_readchunk(rrec->inode,rrec->indx,&(rrec->fleng),&(rrec->chunkid),&(rrec->version),&csdata,&csdatasize);
 if (status!=0) {
  syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"" - fs_readchunk returns status: %s",rrec->inode,rrec->indx,rrec->chunkid,rrec->version,mfsstrerr(status));
  if (status==3) {
   return 9;
  }
  return 5;
 }
 if (rrec->chunkid==0 && csdata==((void *)0) && csdatasize==0) {
  return 0;
 }
 if (csdata==((void *)0) || csdatasize==0) {
  syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"" - there are no valid copies",rrec->inode,rrec->indx,rrec->chunkid,rrec->version);
  return 6;
 }
 ip = 0;
 port = 0;
 bestcnt = 0xFFFFFFFF;
 while (csdatasize>=6 && bestcnt>0) {
  tmpip = get32bit(&csdata);
  tmpport = get16bit(&csdata);
  csdatasize-=6;
  cnt = csdb_getopcnt(tmpip,tmpport);
  if (cnt<bestcnt) {
   ip = tmpip;
   port = tmpport;
   bestcnt = cnt;
  }
 }
 if (ip==0 || port==0) {
  syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"" - there are no valid copies",rrec->inode,rrec->indx,rrec->chunkid,rrec->version);
  return 6;
 }
 rrec->ip = ip;
 rrec->port = port;
 srcip = fs_getsrcip();
 cnt=0;
 while (cnt<10) {
  rrec->fd = tcpsocket();
  if (rrec->fd<0) {
   syslog(4,"can't create tcp socket: %s",strerr((*__errno_location ())));
   break;
  }
  if (srcip) {
   if (tcpnumbind(rrec->fd,srcip,0)<0) {
    syslog(4,"can't bind to given ip: %s",strerr((*__errno_location ())));
    tcpclose(rrec->fd);
    rrec->fd=-1;
    break;
   }
  }
  if (tcpnumtoconnect(rrec->fd,ip,port,(cnt%2)?(300*(1<<(cnt>>1))):(200*(1<<(cnt>>1))))<0) {
   cnt++;
   if (cnt>=10) {
    syslog(4,"can't connect to (%08""X"":%""u""): %s",ip,port,strerr((*__errno_location ())));
   }
   tcpclose(rrec->fd);
   rrec->fd=-1;
  } else {
   cnt=10;
  }
 }
 if (rrec->fd<0) {
  return 5;
 }
 if (tcpnodelay(rrec->fd)<0) {
  syslog(4,"can't set TCP_NODELAY: %s",strerr((*__errno_location ())));
 }
 csdb_readinc(rrec->ip,rrec->port);
 pthread_mutex_lock(&glock);
 rrec->refcnt = 0;
 pthread_mutex_unlock(&glock);
 return 0;
}

variable_types: struct _readrec { unsigned char *rbuff; unsigned int rbuffsize; unsigned int inode; unsigned long fleng; unsigned int indx; unsigned long chunkid; unsigned int version; unsigned int ip; unsigned short port; int fd; unsigned char refcnt; unsigned char noaccesscnt; unsigned char valid; unsigned char locked; unsigned short waiting; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; struct _readrec *next; struct _readrec *mapnext; } *

variable_types:
ip
unsigned int

variable_types:
tmpip
unsigned int

variable_types:
port
unsigned short

variable_types:
tmpport
unsigned short

variable_types:
cnt
unsigned int

variable_types:
bestcnt
unsigned int

variable_types:
csdata
unsigned char *

variable_types:
csdatasize
unsigned int

variable_types:
srcip
unsigned int


decompiled_code:
int __fastcall read_data_end(__int64 a1)
{
__int16 v1; // ax
bool v2; // zf

pthread_mutex_lock(&glock);
v1 = *(_WORD *)(a1 + 60);
v2 = *(_BYTE *)(a1 + 59) == 0;
*(_WORD *)(a1 + 60) = v1 + 1;
if ( !v2 )
{
do
pthread_cond_wait((pthread_cond_t *)(a1 + 64), &glock);
while ( *(_BYTE *)(a1 + 59) );
v1 = *(_WORD *)(a1 + 60) - 1;
}
*(_WORD *)(a1 + 60) = v1;
*(_WORD *)(a1 + 58) = 256;
pthread_mutex_unlock(&glock);
if ( *(int *)(a1 + 52) >= 0 )
{
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
if ( *(_QWORD *)a1 )
{
free(*(void **)a1);
*(_QWORD *)a1 = 0LL;
}
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
*(_BYTE *)(a1 + 59) = 0;
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
__int16 v1; // ax
bool v2; // zf

pthread_mutex_lock(&glock);
v1 = *(_WORD *)(a1 + 60);
v2 = *(_BYTE *)(a1 + 59) == 0;
*(_WORD *)(a1 + 60) = v1 + 1;
if ( !v2 )
{
do
pthread_cond_wait((pthread_cond_t *)(a1 + 64), &glock);
while ( *(_BYTE *)(a1 + 59) );
v1 = *(_WORD *)(a1 + 60) - 1;
}
*(_WORD *)(a1 + 60) = v1;
*(_WORD *)(a1 + 58) = 256;
pthread_mutex_unlock(&glock);
if ( *(int *)(a1 + 52) >= 0 )
{
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
if ( *(_QWORD *)a1 )
{
free(*(void **)a1);
*(_QWORD *)a1 = 0LL;
}
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
*(_BYTE *)(a1 + 59) = 0;
return pthread_mutex_unlock(&glock);
}

original_code:
void read_data_end(void* rr) {
 readrec *rrec = (readrec*)rr;
 pthread_mutex_lock(&glock);
 rrec->waiting++;
 while (rrec->locked) {
  pthread_cond_wait(&(rrec->cond),&glock);
 }
 rrec->waiting--;
 rrec->locked = 1;
 rrec->valid = 0;
 pthread_mutex_unlock(&glock);
 if (rrec->fd>=0) {
  csdb_readdec(rrec->ip,rrec->port);
  tcpclose(rrec->fd);
  rrec->fd=-1;
 }
 if (rrec->rbuff!=((void *)0)) {
  free(rrec->rbuff);
  rrec->rbuff=((void *)0);
 }
 pthread_mutex_lock(&glock);
 if (rrec->waiting) {
  pthread_cond_signal(&(rrec->cond));
 }
 rrec->locked = 0;
 pthread_mutex_unlock(&glock);
}

canonical_original_code:
void read_data_end(void *rr) {
 struct _readrec *rrec = (readrec*)rr;
 pthread_mutex_lock(&glock);
 rrec->waiting++;
 while (rrec->locked) {
  pthread_cond_wait(&(rrec->cond),&glock);
 }
 rrec->waiting--;
 rrec->locked = 1;
 rrec->valid = 0;
 pthread_mutex_unlock(&glock);
 if (rrec->fd>=0) {
  csdb_readdec(rrec->ip,rrec->port);
  tcpclose(rrec->fd);
  rrec->fd=-1;
 }
 if (rrec->rbuff!=((void *)0)) {
  free(rrec->rbuff);
  rrec->rbuff=((void *)0);
 }
 pthread_mutex_lock(&glock);
 if (rrec->waiting) {
  pthread_cond_signal(&(rrec->cond));
 }
 rrec->locked = 0;
 pthread_mutex_unlock(&glock);
}

variable_types:
rr
void *

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


decompiled_code:
int __fastcall read_data_init(int a1)
{
pthread_attr_t v2; // [rsp+0h] [rbp-48h] BYREF

maxretries = a1;
memset(rdinodemap, 0, sizeof(rdinodemap));
rterm = 0;
pthread_mutex_init(&glock, 0LL);
pthread_attr_init(&v2);
pthread_attr_setstacksize(&v2, 0x100000uLL);
pthread_create(&pthid, &v2, read_data_delayed_ops, 0LL);
return pthread_attr_destroy(&v2);
}

canonical_decompiled_code:
int __fastcall func6(int a1)
{
pthread_attr_t v2; // [rsp+0h] [rbp-48h] BYREF

maxretries = a1;
memset(rdinodemap, 0, sizeof(rdinodemap));
rterm = 0;
pthread_mutex_init(&glock, 0LL);
pthread_attr_init(&v2);
pthread_attr_setstacksize(&v2, 0x100000uLL);
pthread_create(&pthid, &v2, read_data_delayed_ops, 0LL);
return pthread_attr_destroy(&v2);
}

original_code:
void read_data_init(uint32_t retries) {
 uint32_t i;
 pthread_attr_t thattr;
 rterm = 0;
 for (i=0 ; i<(1<<(10)) ; i++) {
  rdinodemap[i]=((void *)0);
 }
 maxretries=retries;
 pthread_mutex_init(&glock,((void *)0));
 pthread_attr_init(&thattr);
 pthread_attr_setstacksize(&thattr,0x100000);
 pthread_create(&pthid,&thattr,read_data_delayed_ops,((void *)0));
 pthread_attr_destroy(&thattr);
}

canonical_original_code:
void read_data_init(unsigned int retries) {
 unsigned int i;
 union pthread_attr_t thattr;
 rterm = 0;
 for (i=0 ; i<(1<<(10)) ; i++) {
  rdinodemap[i]=((void *)0);
 }
 maxretries=retries;
 pthread_mutex_init(&glock,((void *)0));
 pthread_attr_init(&thattr);
 pthread_attr_setstacksize(&thattr,0x100000);
 pthread_create(&pthid,&thattr,read_data_delayed_ops,((void *)0));
 pthread_attr_destroy(&thattr);
}

variable_types:
retries
unsigned int

variable_types:
i
unsigned int

variable_types: union pthread_attr_t { char __size[56]; long __align; }


decompiled_code:
void read_data_term()
{
const char *v0; // r12
__int64 v1; // rbx
__int64 v2; // rbp
__int64 v3; // rdi

v0 = (const char *)rdinodemap;
pthread_mutex_lock(&glock);
rterm = 1;
pthread_mutex_unlock(&glock);
pthread_join(pthid, 0LL);
pthread_mutex_destroy(&glock);
do
{
v1 = *(_QWORD *)v0;
while ( v1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 112);
v3 = *(unsigned int *)(v2 + 52);
if ( (int)v3 >= 0 )
tcpclose(v3);
if ( *(_QWORD *)v2 )
free(*(void **)v2);
pthread_cond_destroy((pthread_cond_t *)(v2 + 64));
free((void *)v2);
}
*(_QWORD *)v0 = 0LL;
v0 += 8;
}
while ( v0 != "file: %u, index: %u, chunk: %lu, version: %u - fs_readchunk returns status: %s" );
}

canonical_decompiled_code:
void func7()
{
const char *v0; // r12
__int64 v1; // rbx
__int64 v2; // rbp
__int64 v3; // rdi

v0 = (const char *)rdinodemap;
pthread_mutex_lock(&glock);
rterm = 1;
pthread_mutex_unlock(&glock);
pthread_join(pthid, 0LL);
pthread_mutex_destroy(&glock);
do
{
v1 = *(_QWORD *)v0;
while ( v1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 112);
v3 = *(unsigned int *)(v2 + 52);
if ( (int)v3 >= 0 )
tcpclose(v3);
if ( *(_QWORD *)v2 )
free(*(void **)v2);
pthread_cond_destroy((pthread_cond_t *)(v2 + 64));
free((void *)v2);
}
*(_QWORD *)v0 = 0LL;
v0 += 8;
}
while ( v0 != "file: %u, index: %u, chunk: %lu, version: %u - fs_readchunk returns status: %s" );
}

original_code:
void read_data_term(void) {
 uint32_t i;
 readrec *rr,*rrn;
 pthread_mutex_lock(&glock);
 rterm = 1;
 pthread_mutex_unlock(&glock);
 pthread_join(pthid,((void *)0));
 pthread_mutex_destroy(&glock);
 for (i=0 ; i<(1<<(10)) ; i++) {
  for (rr = rdinodemap[i] ; rr ; rr = rrn) {
   rrn = rr->next;
   if (rr->fd>=0) {
    tcpclose(rr->fd);
   }
   if (rr->rbuff!=((void *)0)) {
    free(rr->rbuff);
   }
   pthread_cond_destroy(&(rr->cond));
   free(rr);
  }
  rdinodemap[i] = ((void *)0);
 }
}

canonical_original_code:
void read_data_term() {
 unsigned int i;
 struct _readrec *rr; struct _readrec *rrn;
 pthread_mutex_lock(&glock);
 rterm = 1;
 pthread_mutex_unlock(&glock);
 pthread_join(pthid,((void *)0));
 pthread_mutex_destroy(&glock);
 for (i=0 ; i<(1<<(10)) ; i++) {
  for (rr = rdinodemap[i] ; rr ; rr = rrn) {
   rrn = rr->next;
   if (rr->fd>=0) {
    tcpclose(rr->fd);
   }
   if (rr->rbuff!=((void *)0)) {
    free(rr->rbuff);
   }
   pthread_cond_destroy(&(rr->cond));
   free(rr);
  }
  rdinodemap[i] = ((void *)0);
 }
}


decompiled_code:
int __fastcall read_inode_ops(int a1)
{
__int64 i; // rax

pthread_mutex_lock(&glock);
for ( i = rdinodemap[a1 & 0x3FF]; i; i = *(_QWORD *)(i + 120) )
{
if ( *(_DWORD *)(i + 12) == a1 )
*(_WORD *)(i + 56) = 783;
}
return pthread_mutex_unlock(&glock);
}

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

pthread_mutex_lock(&glock);
for ( i = rdinodemap[a1 & 0x3FF]; i; i = *(_QWORD *)(i + 120) )
{
if ( *(_DWORD *)(i + 12) == a1 )
*(_WORD *)(i + 56) = 783;
}
return pthread_mutex_unlock(&glock);
}

original_code:
void read_inode_ops(uint32_t inode) {
 readrec *rrec;
 pthread_mutex_lock(&glock);
 for (rrec = rdinodemap[(inode&((1<<(10))-1))] ; rrec ; rrec=rrec->mapnext) {
  if (rrec->inode==inode) {
   rrec->noaccesscnt=3;
   rrec->refcnt=15;
  }
 }
 pthread_mutex_unlock(&glock);
}

canonical_original_code:
void read_inode_ops(unsigned int inode) {
 struct _readrec *rrec;
 pthread_mutex_lock(&glock);
 for (rrec = rdinodemap[(inode&((1<<(10))-1))] ; rrec ; rrec=rrec->mapnext) {
  if (rrec->inode==inode) {
   rrec->noaccesscnt=3;
   rrec->refcnt=15;
  }
 }
 pthread_mutex_unlock(&glock);
}

variable_types:
inode
unsigned int


original_code:
int read_data(void *rr, uint64_t offset, uint32_t *size, uint8_t **buff) {
 uint8_t *buffptr;
 uint64_t curroff;
 uint32_t currsize;
 uint32_t indx;
 uint8_t cnt,eb,forcereconnect;
 uint32_t chunkoffset;
 uint32_t chunksize;
 int err;
 readrec *rrec = (readrec*)rr;
 if (*size==0 && *buff!=((void *)0)) {
  return 0;
 }
 pthread_mutex_lock(&glock);
 rrec->waiting++;
 while (rrec->locked) {
  pthread_cond_wait(&(rrec->cond),&glock);
 }
 rrec->waiting--;
 rrec->locked=1;
 forcereconnect = (rrec->fd>=0 && rrec->refcnt==15)?1:0;
 pthread_mutex_unlock(&glock);
 if (forcereconnect) {
  csdb_readdec(rrec->ip,rrec->port);
  tcpclose(rrec->fd);
  rrec->fd=-1;
 }
 if (*size==0) {
  return 0;
 }
 eb=1;
 if (*buff==((void *)0)) {
  eb=0;
  if (*size>rrec->rbuffsize) {
   if (rrec->rbuff!=((void *)0)) {
    free(rrec->rbuff);
   }
   rrec->rbuffsize = *size;
   rrec->rbuff = malloc(rrec->rbuffsize);
   if (rrec->rbuff==((void *)0)) {
    rrec->rbuffsize = 0;
    syslog(4,"file: %""u"", index: %""u"" - out of memory",rrec->inode,rrec->indx);
    return 12;
   }
  }
 }
 err = 5;
 cnt = 0;
 if (*buff==((void *)0)) {
  buffptr = rrec->rbuff;
 } else {
  buffptr = *buff;
 }
 curroff = offset;
 currsize = *size;
 while (currsize>0) {
  indx = (curroff>>26);
  if (rrec->fd<0 || rrec->indx != indx) {
   rrec->indx = indx;
   while (cnt<maxretries) {
    cnt++;
    err = read_data_refresh_connection(rrec);
    if (err==0) {
     break;
    }
    syslog(4,"file: %""u"", index: %""u"" - can't connect to proper chunkserver (try counter: %""u"")",rrec->inode,rrec->indx,cnt);
    if (err==9) {
     if (eb) {
      pthread_mutex_lock(&glock);
      if (rrec->waiting) {
       pthread_cond_signal(&(rrec->cond));
      }
      rrec->locked = 0;
      pthread_mutex_unlock(&glock);
     }
     return err;
    }
    if (err==6) {
     sleep(60);
     cnt+=6;
    } else {
     sleep(1+((cnt<30)?(cnt/3):10));
    }
   }
   if (cnt>=maxretries) {
    if (eb) {
     pthread_mutex_lock(&glock);
     if (rrec->waiting) {
      pthread_cond_signal(&(rrec->cond));
     }
     rrec->locked=0;
     pthread_mutex_unlock(&glock);
    }
    return err;
   }
  }
  if (curroff>=rrec->fleng) {
   break;
  }
  if (curroff+currsize>rrec->fleng) {
   currsize = rrec->fleng-curroff;
  }
  chunkoffset = (curroff&0x03FFFFFF);
  if (chunkoffset+currsize>0x04000000) {
   chunksize = 0x04000000 -chunkoffset;
  } else {
   chunksize = currsize;
  }
  if (rrec->chunkid>0) {
   if (cs_readblock(rrec->fd,rrec->chunkid,rrec->version,chunkoffset,chunksize,buffptr)<0) {
    syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"", cs: %08""X"":%""u"" - readblock error (try counter: %""u"")",rrec->inode,rrec->indx,rrec->chunkid,rrec->version,rrec->ip,rrec->port,cnt);
    csdb_readdec(rrec->ip,rrec->port);
    tcpclose(rrec->fd);
    rrec->fd = -1;
    sleep(1+((cnt<30)?(cnt/3):10));
   } else {
    curroff+=chunksize;
    currsize-=chunksize;
    buffptr+=chunksize;
   }
  } else {
   memset(buffptr,0,chunksize);
   curroff+=chunksize;
   currsize-=chunksize;
   buffptr+=chunksize;
  }
 }
 if (rrec->fleng<=offset) {
  *size = 0;
 } else if (rrec->fleng<(offset+(*size))) {
  if (*buff==((void *)0)) {
   *buff = rrec->rbuff;
  }
  *size = rrec->fleng - offset;
 } else {
  if (*buff==((void *)0)) {
   *buff = rrec->rbuff;
  }
 }
 pthread_mutex_lock(&glock);
 rrec->noaccesscnt=0;
 if (eb) {
  if (rrec->waiting) {
   pthread_cond_signal(&(rrec->cond));
  }
  rrec->locked = 0;
 }
 pthread_mutex_unlock(&glock);
 return 0;
}

canonical_original_code:
int read_data(void *rr, unsigned long offset, unsigned int *size, unsigned char **buff) {
 unsigned char *buffptr;
 unsigned long curroff;
 unsigned int currsize;
 unsigned int indx;
 unsigned char cnt; unsigned char eb; unsigned char forcereconnect;
 unsigned int chunkoffset;
 unsigned int chunksize;
 int err;
 struct _readrec *rrec = (readrec*)rr;
 if (*size==0 && *buff!=((void *)0)) {
  return 0;
 }
 pthread_mutex_lock(&glock);
 rrec->waiting++;
 while (rrec->locked) {
  pthread_cond_wait(&(rrec->cond),&glock);
 }
 rrec->waiting--;
 rrec->locked=1;
 forcereconnect = (rrec->fd>=0 && rrec->refcnt==15)?1:0;
 pthread_mutex_unlock(&glock);
 if (forcereconnect) {
  csdb_readdec(rrec->ip,rrec->port);
  tcpclose(rrec->fd);
  rrec->fd=-1;
 }
 if (*size==0) {
  return 0;
 }
 eb=1;
 if (*buff==((void *)0)) {
  eb=0;
  if (*size>rrec->rbuffsize) {
   if (rrec->rbuff!=((void *)0)) {
    free(rrec->rbuff);
   }
   rrec->rbuffsize = *size;
   rrec->rbuff = malloc(rrec->rbuffsize);
   if (rrec->rbuff==((void *)0)) {
    rrec->rbuffsize = 0;
    syslog(4,"file: %""u"", index: %""u"" - out of memory",rrec->inode,rrec->indx);
    return 12;
   }
  }
 }
 err = 5;
 cnt = 0;
 if (*buff==((void *)0)) {
  buffptr = rrec->rbuff;
 } else {
  buffptr = *buff;
 }
 curroff = offset;
 currsize = *size;
 while (currsize>0) {
  indx = (curroff>>26);
  if (rrec->fd<0 || rrec->indx != indx) {
   rrec->indx = indx;
   while (cnt<maxretries) {
    cnt++;
    err = read_data_refresh_connection(rrec);
    if (err==0) {
     break;
    }
    syslog(4,"file: %""u"", index: %""u"" - can't connect to proper chunkserver (try counter: %""u"")",rrec->inode,rrec->indx,cnt);
    if (err==9) {
     if (eb) {
      pthread_mutex_lock(&glock);
      if (rrec->waiting) {
       pthread_cond_signal(&(rrec->cond));
      }
      rrec->locked = 0;
      pthread_mutex_unlock(&glock);
     }
     return err;
    }
    if (err==6) {
     sleep(60);
     cnt+=6;
    } else {
     sleep(1+((cnt<30)?(cnt/3):10));
    }
   }
   if (cnt>=maxretries) {
    if (eb) {
     pthread_mutex_lock(&glock);
     if (rrec->waiting) {
      pthread_cond_signal(&(rrec->cond));
     }
     rrec->locked=0;
     pthread_mutex_unlock(&glock);
    }
    return err;
   }
  }
  if (curroff>=rrec->fleng) {
   break;
  }
  if (curroff+currsize>rrec->fleng) {
   currsize = rrec->fleng-curroff;
  }
  chunkoffset = (curroff&0x03FFFFFF);
  if (chunkoffset+currsize>0x04000000) {
   chunksize = 0x04000000 -chunkoffset;
  } else {
   chunksize = currsize;
  }
  if (rrec->chunkid>0) {
   if (cs_readblock(rrec->fd,rrec->chunkid,rrec->version,chunkoffset,chunksize,buffptr)<0) {
    syslog(4,"file: %""u"", index: %""u"", chunk: %""l" "u"", version: %""u"", cs: %08""X"":%""u"" - readblock error (try counter: %""u"")",rrec->inode,rrec->indx,rrec->chunkid,rrec->version,rrec->ip,rrec->port,cnt);
    csdb_readdec(rrec->ip,rrec->port);
    tcpclose(rrec->fd);
    rrec->fd = -1;
    sleep(1+((cnt<30)?(cnt/3):10));
   } else {
    curroff+=chunksize;
    currsize-=chunksize;
    buffptr+=chunksize;
   }
  } else {
   memset(buffptr,0,chunksize);
   curroff+=chunksize;
   currsize-=chunksize;
   buffptr+=chunksize;
  }
 }
 if (rrec->fleng<=offset) {
  *size = 0;
 } else if (rrec->fleng<(offset+(*size))) {
  if (*buff==((void *)0)) {
   *buff = rrec->rbuff;
  }
  *size = rrec->fleng - offset;
 } else {
  if (*buff==((void *)0)) {
   *buff = rrec->rbuff;
  }
 }
 pthread_mutex_lock(&glock);
 rrec->noaccesscnt=0;
 if (eb) {
  if (rrec->waiting) {
   pthread_cond_signal(&(rrec->cond));
  }
  rrec->locked = 0;
 }
 pthread_mutex_unlock(&glock);
 return 0;
}

variable_types:
offset
unsigned long

variable_types:
size
unsigned int *

variable_types:
buff
unsigned char **

variable_types:
buffptr
unsigned char *

variable_types:
curroff
unsigned long

variable_types:
currsize
unsigned int

variable_types:
indx
unsigned int

variable_types:
cnt
unsigned char

variable_types:
eb
unsigned char

variable_types:
forcereconnect
unsigned char

variable_types:
chunkoffset
unsigned int

variable_types:
chunksize
unsigned int

variable_types: err int


decompiled_code:
int __fastcall read_data_freebuff(__int64 a1)
{
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
*(_BYTE *)(a1 + 59) = 0;
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
*(_BYTE *)(a1 + 59) = 0;
return pthread_mutex_unlock(&glock);
}

original_code:
void read_data_freebuff(void *rr) {
 readrec *rrec = (readrec*)rr;
 pthread_mutex_lock(&glock);
 if (rrec->waiting) {
  pthread_cond_signal(&(rrec->cond));
 }
 rrec->locked = 0;
 pthread_mutex_unlock(&glock);
}

canonical_original_code:
void read_data_freebuff(void *rr) {
 struct _readrec *rrec = (readrec*)rr;
 pthread_mutex_lock(&glock);
 if (rrec->waiting) {
  pthread_cond_signal(&(rrec->cond));
 }
 rrec->locked = 0;
 pthread_mutex_unlock(&glock);
}

variable_types:
rr
void *

variable_types: struct _readrec { unsigned char *rbuff; unsigned int rbuffsize; unsigned int inode; unsigned long fleng; unsigned int indx; unsigned long chunkid; unsigned int version; unsigned int ip; unsigned short port; int fd; unsigned char refcnt; unsigned char noaccesscnt; unsigned char valid; unsigned char locked; unsigned short waiting; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; struct _readrec *next; struct _readrec *mapnext; } *


decompiled_code:
__int64 __fastcall stats_reset(__int64 a1)
{
__int64 i; // rbx
__int64 result; // rax

if ( !*(_BYTE *)(a1 + 9) )
*(_QWORD *)a1 = 0LL;
for ( i = *(_QWORD *)(a1 + 40); i; i = *(_QWORD *)(i + 48) )
result = stats_reset(i);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 i; // rbx
__int64 result; // rax

if ( !*(_BYTE *)(a1 + 9) )
*(_QWORD *)a1 = 0LL;
for ( i = *(_QWORD *)(a1 + 40); i; i = *(_QWORD *)(i + 48) )
result = func0(i);
return result;
}

original_code:
static inline void stats_reset(statsnode *n) {
 statsnode *a;
 if (n->absolute==0) {
  n->counter = 0;
 }
 for (a=n->firstchild ; a ; a=a->nextsibling) {
  stats_reset(a);
 }
}

canonical_original_code:
static inline void stats_reset(struct _statsnode *n) {
 struct _statsnode *a;
 if (n->absolute==0) {
  n->counter = 0;
 }
 for (a=n->firstchild ; a ; a=a->nextsibling) {
  stats_reset(a);
 }
}

variable_types: struct _statsnode { unsigned long counter; unsigned char active; unsigned char absolute; char *name; char *fullname; unsigned int nleng; unsigned int fnleng; struct _statsnode *firstchild; struct _statsnode *nextsibling; } *


decompiled_code:
__int64 __fastcall stats_print_values(char *a1, unsigned int a2, __int64 a3)
{
unsigned int v3; // er12
__int64 i; // rbx

v3 = 0;
if ( *(_BYTE *)(a3 + 8) )
v3 = snprintf(a1, a2, "%s: %lu\n", *(const char **)(a3 + 24), *(_QWORD *)a3);
for ( i = *(_QWORD *)(a3 + 40); i; i = *(_QWORD *)(i + 48) )
{
if ( a2 > v3 )
v3 += stats_print_values(&a1[v3], a2 - v3, i);
}
return v3;
}

original_code:
static inline uint32_t stats_print_values(char *buff,uint32_t maxleng,statsnode *n) {
 statsnode *a;
 uint32_t l;
 if (n->active) {
  l = snprintf(buff,maxleng,"%s: %""l" "u""\n",n->fullname,n->counter);
 } else {
  l = 0;
 }
 for (a=n->firstchild ; a ; a=a->nextsibling) {
  if (maxleng>l) {
   l += stats_print_values(buff+l,maxleng-l,a);
  }
 }
 return l;
}

canonical_original_code:
static inline unsigned int stats_print_values(char *buff, unsigned int maxleng, struct _statsnode *n) {
 struct _statsnode *a;
 unsigned int l;
 if (n->active) {
  l = snprintf(buff,maxleng,"%s: %""l" "u""\n",n->fullname,n->counter);
 } else {
  l = 0;
 }
 for (a=n->firstchild ; a ; a=a->nextsibling) {
  if (maxleng>l) {
   l += stats_print_values(buff+l,maxleng-l,a);
  }
 }
 return l;
}

variable_types:
buff
char *

variable_types:
maxleng
unsigned int

variable_types:
l
unsigned int

user_defined_types: struct _statsnode { unsigned long counter; unsigned char active; unsigned char absolute; char *name; char *fullname; unsigned int nleng; unsigned int fnleng; struct _statsnode *firstchild; struct _statsnode *nextsibling; }


decompiled_code:
__int64 __fastcall stats_print_total(__int64 a1, unsigned int a2)
{
_QWORD *v2; // rbx
unsigned int v3; // er12
int v4; // eax

v2 = firstnode;
if ( !firstnode )
return 0LL;
v3 = 0;
do
{
while ( a2 <= v3 )
{
v2 = (_QWORD *)v2[6];
if ( !v2 )
return v3;
}
v4 = stats_print_values((char *)(a1 + v3), a2 - v3, (__int64)v2);
v2 = (_QWORD *)v2[6];
v3 += v4;
}
while ( v2 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
_QWORD *v2; // rbx
unsigned int v3; // er12
int v4; // eax

v2 = firstnode;
if ( !firstnode )
return 0LL;
v3 = 0;
do
{
while ( a2 <= v3 )
{
v2 = (_QWORD *)v2[6];
if ( !v2 )
return v3;
}
v4 = func1((char *)(a1 + v3), a2 - v3, (__int64)v2);
v2 = (_QWORD *)v2[6];
v3 += v4;
}
while ( v2 );
return v3;
}

original_code:
static inline uint32_t stats_print_total(char *buff,uint32_t maxleng) {
 statsnode *a;
 uint32_t l;
 l = 0;
 for (a=firstnode ; a ; a=a->nextsibling) {
  if (maxleng>l) {
   l += stats_print_values(buff+l,maxleng-l,a);
  }
 }
 return l;
}

canonical_original_code:
static inline unsigned int stats_print_total(char *buff, unsigned int maxleng) {
 struct _statsnode *a;
 unsigned int l;
 l = 0;
 for (a=firstnode ; a ; a=a->nextsibling) {
  if (maxleng>l) {
   l += stats_print_values(buff+l,maxleng-l,a);
  }
 }
 return l;
}


decompiled_code:
int stats_lock()
{
return pthread_mutex_lock(&glock);
}

canonical_decompiled_code:
int func3()
{
return pthread_mutex_lock(&glock);
}

original_code:
void stats_lock(void) {
 pthread_mutex_lock(&glock);
}

canonical_original_code:
void stats_lock() {
 pthread_mutex_lock(&glock);
}


decompiled_code:
int stats_unlock()
{
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int func4()
{
return pthread_mutex_unlock(&glock);
}

original_code:
void stats_unlock(void) {
 pthread_mutex_unlock(&glock);
}

canonical_original_code:
void stats_unlock() {
 pthread_mutex_unlock(&glock);
}


original_code:
void* stats_get_subnode(void *node,const char *name,uint8_t absolute) {
 statsnode *sn = (statsnode*)node;
 statsnode *a;
 pthread_mutex_lock(&glock);
 for (a=sn?sn->firstchild:firstnode ; a ; a=a->nextsibling) {
  if (strcmp(a->name,name)==0) {
   pthread_mutex_unlock(&glock);
   return a;
  }
 }
 a = malloc(sizeof(statsnode));
 a->nextsibling = sn?sn->firstchild:firstnode;
 a->firstchild = ((void *)0);
 a->counter = 0;
 a->active = 0;
 a->absolute = absolute;
 a->name = strdup(name);
 a->nleng = strlen(name);
 if (sn) {
  char *bstr;
  a->fnleng = sn->fnleng+1+a->nleng;
  bstr = malloc(a->fnleng+1);
  memcpy(bstr,sn->fullname,sn->fnleng);
  bstr[sn->fnleng]='.';
  memcpy(bstr+sn->fnleng+1,a->name,a->nleng);
  bstr[a->fnleng]=0;
  a->fullname = bstr;
 } else {
  a->fullname = a->name;
  a->fnleng = a->nleng;
 }
 if (sn) {
  sn->firstchild = a;
 } else {
  firstnode = a;
 }
 pthread_mutex_unlock(&glock);
 return a;
}

canonical_original_code:
void *stats_get_subnode(void *node, char *name, unsigned char absolute) {
 struct _statsnode *sn = (statsnode*)node;
 struct _statsnode *a;
 pthread_mutex_lock(&glock);
 for (a=sn?sn->firstchild:firstnode ; a ; a=a->nextsibling) {
  if (strcmp(a->name,name)==0) {
   pthread_mutex_unlock(&glock);
   return a;
  }
 }
 a = malloc(sizeof(statsnode));
 a->nextsibling = sn?sn->firstchild:firstnode;
 a->firstchild = ((void *)0);
 a->counter = 0;
 a->active = 0;
 a->absolute = absolute;
 a->name = strdup(name);
 a->nleng = strlen(name);
 if (sn) {
  char *bstr;
  a->fnleng = sn->fnleng+1+a->nleng;
  bstr = malloc(a->fnleng+1);
  memcpy(bstr,sn->fullname,sn->fnleng);
  bstr[sn->fnleng]='.';
  memcpy(bstr+sn->fnleng+1,a->name,a->nleng);
  bstr[a->fnleng]=0;
  a->fullname = bstr;
 } else {
  a->fullname = a->name;
  a->fnleng = a->nleng;
 }
 if (sn) {
  sn->firstchild = a;
 } else {
  firstnode = a;
 }
 pthread_mutex_unlock(&glock);
 return a;
}

variable_types:
node
void *

variable_types:
name
char *

variable_types:
absolute
unsigned char

variable_types:
bstr
char *


decompiled_code:
__int64 __fastcall stats_get_counterptr(__int64 a1)
{
int v1; // eax

pthread_mutex_lock(&glock);
if ( !*(_BYTE *)(a1 + 8) )
{
v1 = *(_DWORD *)(a1 + 36);
++activenodes;
allactiveplengs += v1;
*(_BYTE *)(a1 + 8) = 1;
}
pthread_mutex_unlock(&glock);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // eax

pthread_mutex_lock(&glock);
if ( !*(_BYTE *)(a1 + 8) )
{
v1 = *(_DWORD *)(a1 + 36);
++activenodes;
allactiveplengs += v1;
*(_BYTE *)(a1 + 8) = 1;
}
pthread_mutex_unlock(&glock);
return a1;
}

original_code:
uint64_t* stats_get_counterptr(void *node) {
 statsnode *sn = (statsnode*)node;
 pthread_mutex_lock(&glock);
 if (sn->active==0) {
  sn->active = 1;
  allactiveplengs += sn->fnleng;
  activenodes++;
 }
 pthread_mutex_unlock(&glock);
 return &(sn->counter);
}

canonical_original_code:
unsigned long *stats_get_counterptr(void *node) {
 struct _statsnode *sn = (statsnode*)node;
 pthread_mutex_lock(&glock);
 if (sn->active==0) {
  sn->active = 1;
  allactiveplengs += sn->fnleng;
  activenodes++;
 }
 pthread_mutex_unlock(&glock);
 return &(sn->counter);
}

return_type: unsigned long *


decompiled_code:
int stats_reset_all()
{
void *i; // rax

pthread_mutex_lock(&glock);
for ( i = firstnode; i; i = *(void **)(stats_reset((__int64)i) + 48) )
;
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int func7()
{
void *i; // rax

pthread_mutex_lock(&glock);
for ( i = firstnode; i; i = *(void **)(func0((__int64)i) + 48) )
;
return pthread_mutex_unlock(&glock);
}

original_code:
void stats_reset_all(void) {
 statsnode *a;
 pthread_mutex_lock(&glock);
 for (a=firstnode ; a ; a=a->nextsibling) {
  stats_reset(a);
 }
 pthread_mutex_unlock(&glock);
}

canonical_original_code:
void stats_reset_all() {
 struct _statsnode *a;
 pthread_mutex_lock(&glock);
 for (a=firstnode ; a ; a=a->nextsibling) {
  stats_reset(a);
 }
 pthread_mutex_unlock(&glock);
}


decompiled_code:
int __fastcall stats_show_all(_QWORD *a1, _DWORD *a2)
{
size_t v2; // r12
void *v3; // rax

pthread_mutex_lock(&glock);
v2 = (unsigned int)(23 * activenodes + allactiveplengs + 1);
v3 = malloc(v2);
*a1 = v3;
if ( v3 )
*a2 = stats_print_total((__int64)v3, v2);
else
*a2 = 0;
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int __fastcall func8(_QWORD *a1, _DWORD *a2)
{
size_t v2; // r12
void *v3; // rax

pthread_mutex_lock(&glock);
v2 = (unsigned int)(23 * activenodes + allactiveplengs + 1);
v3 = malloc(v2);
*a1 = v3;
if ( v3 )
*a2 = func2((__int64)v3, v2);
else
*a2 = 0;
return pthread_mutex_unlock(&glock);
}

original_code:
void stats_show_all(char **buff,uint32_t *leng) {
 uint32_t rl;
 pthread_mutex_lock(&glock);
 rl = allactiveplengs + 23*activenodes + 1;
 *buff = malloc(rl);
 if (*buff) {
  *leng = stats_print_total(*buff,rl);
 } else {
  *leng = 0;
 }
 pthread_mutex_unlock(&glock);
}

canonical_original_code:
void stats_show_all(char **buff, unsigned int *leng) {
 unsigned int rl;
 pthread_mutex_lock(&glock);
 rl = allactiveplengs + 23*activenodes + 1;
 *buff = malloc(rl);
 if (*buff) {
  *leng = stats_print_total(*buff,rl);
 } else {
  *leng = 0;
 }
 pthread_mutex_unlock(&glock);
}

variable_types:
buff
char **

variable_types:
leng
unsigned int *

variable_types:
rl
unsigned int


decompiled_code:
void stats_term()
{
void *v0; // rbx
void *v1; // rbp

v0 = firstnode;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 6);
stats_free((__int64)v1);
free(v1);
}
}

canonical_decompiled_code:
void func10()
{
void *v0; // rbx
void *v1; // rbp

v0 = firstnode;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 6);
func9((__int64)v1);
free(v1);
}
}

original_code:
void stats_term(void) {
 statsnode *a,*an;
 for (a=firstnode ; a ; a = an) {
  an = a->nextsibling;
  stats_free(a);
  free(a);
 }
}

canonical_original_code:
void stats_term() {
 struct _statsnode *a; struct _statsnode *an;
 for (a=firstnode ; a ; a = an) {
  an = a->nextsibling;
  stats_free(a);
  free(a);
 }
}


canonical_original_code:
int rnd_init() {
 unsigned char key[64]; unsigned char vkey[64];
 register unsigned char x;
 unsigned short l;
 srandom(time(((void *)0)));
 for (l=0 ; l<64 ; l++) {
  key[l] = random();
  vkey[l] = random();
 }
 for (l=0 ; l<256 ; l++) {
  p[l]=l;
 }
 for (l=0 ; l<768 ; l++) {
  i = l&0xFF;
  x = j+p[i]+key[l%64];
  j = p[x];
  x = p[i];
  p[i] = p[j];
  p[j] = x;
 }
 for (l=0 ; l<768 ; l++) {
  i = l&0xFF;
  x = j+p[i]+vkey[l%64];
  j = p[x];
  x = p[i];
  p[i] = p[j];
  p[j] = x;
 }
 i = 0;
 return 0;
}

variable_types:
key
unsigned char [64]

variable_types:
vkey
unsigned char [64]

variable_types:
x
unsigned char

variable_types:
l
unsigned short


decompiled_code:
__int64 rndu8()
{
__int64 v0; // rdi
char v1; // si
__int64 v2; // rax
__int64 v3; // rdx
unsigned int v4; // er9

v0 = (unsigned __int8)i;
v1 = p[(unsigned __int8)i++];
v2 = (unsigned __int8)p[(unsigned __int8)(v1 + j)];
v3 = (unsigned __int8)p[v2];
j = p[(unsigned __int8)(v1 + j)];
v4 = (unsigned __int8)p[(unsigned __int8)(p[v3] + 1)];
p[v0] = v3;
p[v2] = v1;
return v4;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rdi
char v1; // si
__int64 v2; // rax
__int64 v3; // rdx
unsigned int v4; // er9

v0 = (unsigned __int8)i;
v1 = p[(unsigned __int8)i++];
v2 = (unsigned __int8)p[(unsigned __int8)(v1 + j)];
v3 = (unsigned __int8)p[v2];
j = p[(unsigned __int8)(v1 + j)];
v4 = (unsigned __int8)p[(unsigned __int8)(p[v3] + 1)];
p[v0] = v3;
p[v2] = v1;
return v4;
}

original_code:
uint8_t rndu8() {
 uint8_t r;
 { register uint8_t x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 return r;
}

canonical_original_code:
unsigned char rndu8() {
 unsigned char r;
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 return r;
}

variable_types:
r
unsigned char

return_type: unsigned char


decompiled_code:
__int64 rndu32()
{
char v0; // r8
char v1; // di
char v2; // al
__int64 v3; // rsi
char v4; // dl
char v5; // cl
__int64 v6; // rsi
char v7; // r8
unsigned __int8 v8; // dl
char v9; // cl
__int64 v10; // rsi
char v11; // r8
unsigned __int8 v12; // dl
__int64 v13; // r9
char v14; // cl
char v15; // di
__int64 v16; // rdx
__int64 v17; // rcx
__int64 result; // rax
unsigned int v19; // [rsp+0h] [rbp-4h]

v0 = i;
v1 = p[(unsigned __int8)i];
v2 = i;
v3 = (unsigned __int8)p[(unsigned __int8)(v1 + j)];
v4 = p[(unsigned __int8)(v1 + j)];
v5 = p[(unsigned __int8)(p[(unsigned __int8)p[v3]] + 1)];
p[(unsigned __int8)i] = p[v3];
p[v3] = v1;
v6 = (unsigned __int8)(v0 + 1);
LOBYTE(v19) = v5;
v7 = p[v6];
v8 = p[(unsigned __int8)(v7 + v4)];
v9 = p[(unsigned __int8)(p[(unsigned __int8)p[v8]] + 1)];
p[v6] = p[v8];
v10 = (unsigned __int8)(v2 + 2);
p[v8] = v7;
v11 = p[v10];
BYTE1(v19) = v9;
v12 = p[(unsigned __int8)(v11 + v8)];
v13 = (unsigned __int8)p[v12];
v14 = p[(unsigned __int8)(p[v13] + 1)];
p[v10] = v13;
p[v12] = v11;
v15 = p[(unsigned __int8)(v2 + 3)];
BYTE2(v19) = v14;
v16 = (unsigned __int8)p[(unsigned __int8)(v15 + v12)];
v17 = (unsigned __int8)p[v16];
j = v16;
HIBYTE(v19) = p[(unsigned __int8)(p[v17] + 1)];
p[(unsigned __int8)(v2 + 3)] = v17;
i = v2 + 4;
result = v19;
p[v16] = v15;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
char v0; // r8
char v1; // di
char v2; // al
__int64 v3; // rsi
char v4; // dl
char v5; // cl
__int64 v6; // rsi
char v7; // r8
unsigned __int8 v8; // dl
char v9; // cl
__int64 v10; // rsi
char v11; // r8
unsigned __int8 v12; // dl
__int64 v13; // r9
char v14; // cl
char v15; // di
__int64 v16; // rdx
__int64 v17; // rcx
__int64 result; // rax
unsigned int v19; // [rsp+0h] [rbp-4h]

v0 = i;
v1 = p[(unsigned __int8)i];
v2 = i;
v3 = (unsigned __int8)p[(unsigned __int8)(v1 + j)];
v4 = p[(unsigned __int8)(v1 + j)];
v5 = p[(unsigned __int8)(p[(unsigned __int8)p[v3]] + 1)];
p[(unsigned __int8)i] = p[v3];
p[v3] = v1;
v6 = (unsigned __int8)(v0 + 1);
LOBYTE(v19) = v5;
v7 = p[v6];
v8 = p[(unsigned __int8)(v7 + v4)];
v9 = p[(unsigned __int8)(p[(unsigned __int8)p[v8]] + 1)];
p[v6] = p[v8];
v10 = (unsigned __int8)(v2 + 2);
p[v8] = v7;
v11 = p[v10];
BYTE1(v19) = v9;
v12 = p[(unsigned __int8)(v11 + v8)];
v13 = (unsigned __int8)p[v12];
v14 = p[(unsigned __int8)(p[v13] + 1)];
p[v10] = v13;
p[v12] = v11;
v15 = p[(unsigned __int8)(v2 + 3)];
BYTE2(v19) = v14;
v16 = (unsigned __int8)p[(unsigned __int8)(v15 + v12)];
v17 = (unsigned __int8)p[v16];
j = v16;
HIBYTE(v19) = p[(unsigned __int8)(p[v17] + 1)];
p[(unsigned __int8)(v2 + 3)] = v17;
i = v2 + 4;
result = v19;
p[v16] = v15;
return result;
}

canonical_original_code:
unsigned int rndu32() {
 unsigned int res;
 unsigned char *r = (uint8_t*)&res;
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[0] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[1] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[2] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[3] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 return res;
}

variable_types:
res
unsigned int

variable_types:
r
unsigned char *


canonical_original_code:
unsigned long rndu64() {
 unsigned long res;
 unsigned char *r = (uint8_t*)&res;
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[0] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[1] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[2] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[3] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[4] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[5] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[6] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 { register unsigned char x; x = j+p[i]; j = p[x]; x = p[j]; x = p[x]+1; r[7] = p[x]; x = p[i]; p[i] = p[j]; p[j] = x; i++; };
 return res;
}

variable_types:
res
unsigned long

return_type: unsigned long


original_code:
uint64_t rndu64_ranged(uint64_t range) {
 uint64_t max,r;
 r = rndu64();
 if (range==0) {
  return r;
 }
 max = -(((uint64_t)-range) % range);
 if (max) {
  while (r>=max) {
   r = rndu64();
  }
 }
 return r%range;
}

canonical_original_code:
unsigned long rndu64_ranged(unsigned long range) {
 unsigned long max; unsigned long r;
 r = rndu64();
 if (range==0) {
  return r;
 }
 max = -(((unsigned long)-range) % range);
 if (max) {
  while (r>=max) {
   r = rndu64();
  }
 }
 return r%range;
}

variable_types:
range
unsigned long

variable_types:
max
unsigned long

variable_types:
r
unsigned long


decompiled_code:
__int64 rndu32_ranged()
{
unsigned int v0; // er8
unsigned int v1; // er10
unsigned int v2; // er11

v0 = rndu32();
if ( v1 )
{
if ( -v1 % v1 && v0 >= -(-v1 % v1) )
{
do
v0 = rndu32();
while ( v2 <= v0 );
}
v0 %= v1;
}
return v0;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // er8
unsigned int v1; // er10
unsigned int v2; // er11

v0 = func2();
if ( v1 )
{
if ( -v1 % v1 && v0 >= -(-v1 % v1) )
{
do
v0 = func2();
while ( v2 <= v0 );
}
v0 %= v1;
}
return v0;
}

original_code:
uint32_t rndu32_ranged(uint32_t range) {
 uint32_t max,r;
 r = rndu32();
 if (range==0) {
  return r;
 }
 max = -(((uint32_t)-range) % range);
 if (max) {
  while (r>=max) {
   r = rndu32();
  }
 }
 return r%range;
}

canonical_original_code:
unsigned int rndu32_ranged(unsigned int range) {
 unsigned int max; unsigned int r;
 r = rndu32();
 if (range==0) {
  return r;
 }
 max = -(((unsigned int)-range) % range);
 if (max) {
  while (r>=max) {
   r = rndu32();
  }
 }
 return r%range;
}

variable_types:
range
unsigned int

variable_types:
max
unsigned int

variable_types:
r
unsigned int


variable_types:
ptr
unsigned char **

variable_types:
val
unsigned long


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

result = a2;
**(_BYTE **)a1 = HIBYTE(a2);
*(_BYTE *)(*(_QWORD *)a1 + 1LL) = BYTE2(a2);
*(_BYTE *)(*(_QWORD *)a1 + 2LL) = BYTE1(a2);
*(_BYTE *)(*(_QWORD *)a1 + 3LL) = a2;
*(_QWORD *)a1 += 4LL;
return result;
}

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

result = a2;
**(_BYTE **)a1 = HIBYTE(a2);
*(_BYTE *)(*(_QWORD *)a1 + 1LL) = BYTE2(a2);
*(_BYTE *)(*(_QWORD *)a1 + 2LL) = BYTE1(a2);
*(_BYTE *)(*(_QWORD *)a1 + 3LL) = a2;
*(_QWORD *)a1 += 4LL;
return result;
}

canonical_original_code:
static inline void put32bit(unsigned char **ptr, unsigned int val) {
 (*ptr)[0]=((val)>>24)&0xFF;
 (*ptr)[1]=((val)>>16)&0xFF;
 (*ptr)[2]=((val)>>8)&0xFF;
 (*ptr)[3]=(val)&0xFF;
 (*ptr)+=4;
}

variable_types:
val
unsigned int


canonical_original_code:
static inline unsigned int get32bit(unsigned char **ptr) {
 unsigned int t32;
 t32=((*ptr)[3]+256U*((*ptr)[2]+256U*((*ptr)[1]+256U*(*ptr)[0])));
 (*ptr)+=4;
 return t32;
}


decompiled_code:
__int64 __fastcall get16bit(unsigned __int8 **a1)
{
int v1; // eax
int v2; // ecx

v1 = **a1;
v2 = (*a1)[1];
*a1 += 2;
return (unsigned int)(v2 + (v1 << 8));
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 **a1)
{
int v1; // eax
int v2; // ecx

v1 = **a1;
v2 = (*a1)[1];
*a1 += 2;
return (unsigned int)(v2 + (v1 << 8));
}

original_code:
static inline uint16_t get16bit(const uint8_t **ptr) {
 uint32_t t16;
 t16=(*ptr)[1]+256U*(*ptr)[0];
 (*ptr)+=2;
 return t16;
}

canonical_original_code:
static inline unsigned short get16bit(unsigned char **ptr) {
 unsigned int t16;
 t16=(*ptr)[1]+256U*(*ptr)[0];
 (*ptr)+=2;
 return t16;
}


decompiled_code:
char *__fastcall mfsstrerr(unsigned __int8 a1)
{
if ( a1 > 0x29u )
a1 = 41;
return errtab_0[a1];
}

canonical_decompiled_code:
char *__fastcall func5(unsigned __int8 a1)
{
if ( a1 > 0x29u )
a1 = 41;
return errtab_0[a1];
}

original_code:
static inline const char* mfsstrerr(uint8_t status) {
 static const char* errtab[]={"OK", "Operation not permitted", "Not a directory", "No such file or directory", "Permission denied", "File exists", "Invalid argument", "Directory not empty", "Chunk lost", "Out of memory", "Index too big", "Chunk locked", "No chunk servers", "No such chunk", "Chunk is busy", "Incorrect register BLOB", "None of chunk servers performed requested operation", "File not opened", "Write not started", "Wrong chunk version", "Chunk already exists", "No space left", "IO error", "Incorrect block number", "Incorrect size", "Incorrect offset", "Can't connect", "Incorrect chunk id", "Disconnected", "CRC error", "Operation delayed", "Can't create path", "Data mismatch", "Read-only file system", "Quota exceeded", "Bad session id", "Password is needed", "Incorrect password", "Attribute not found", "Operation not supported", "Result too large", "Unknown MFS error"};
 if (status>41) {
  status=41;
 }
 return errtab[status];
}

canonical_original_code:
static inline char *mfsstrerr(unsigned char status) {
 static char *errtab[] = {"OK", "Operation not permitted", "Not a directory", "No such file or directory", "Permission denied", "File exists", "Invalid argument", "Directory not empty", "Chunk lost", "Out of memory", "Index too big", "Chunk locked", "No chunk servers", "No such chunk", "Chunk is busy", "Incorrect register BLOB", "None of chunk servers performed requested operation", "File not opened", "Write not started", "Wrong chunk version", "Chunk already exists", "No space left", "IO error", "Incorrect block number", "Incorrect size", "Incorrect offset", "Can't connect", "Incorrect chunk id", "Disconnected", "CRC error", "Operation delayed", "Can't create path", "Data mismatch", "Read-only file system", "Quota exceeded", "Bad session id", "Password is needed", "Incorrect password", "Attribute not found", "Operation not supported", "Result too large", "Unknown MFS error"};
 if (status>41) {
  status=41;
 }
 return errtab[status];
}

variable_types:
status
unsigned char

variable_types:
errtab
char *[]


decompiled_code:
__int64 __fastcall cs_readblock(unsigned int a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5, __int64 a6)
{
unsigned int v8; // er10
unsigned int v9; // er11
unsigned int v10; // er8
int v11; // esi
unsigned int v12; // eax
unsigned int v13; // er15
__int64 result; // rax
unsigned __int64 v15; // rax
unsigned int v16; // esi
unsigned int v17; // er13
int v18; // eax
unsigned int v19; // er9
__int64 v20; // rcx
__int64 v21; // rcx
__int64 v22; // rcx
int *v23; // rax
const char *v24; // rax
unsigned __int64 v25; // rax
int *v26; // rax
const char *v27; // rax
int *v28; // rax
const char *v29; // rax
int *v30; // rax
const char *v31; // rax
char *v32; // rax
int v34; // [rsp+4h] [rbp-74h]
char *v36; // [rsp+10h] [rbp-68h] BYREF
unsigned __int8 *v37; // [rsp+18h] [rbp-60h] BYREF
char v38[88]; // [rsp+20h] [rbp-58h] BYREF

v36 = v38;
put32bit((__int64)&v36, 0xC8u);
put32bit((__int64)&v36, 0x14u);
put64bit((__int64 *)&v36, a2);
put32bit((__int64)&v36, v8);
put32bit((__int64)&v36, v9);
put32bit((__int64)&v36, v10);
if ( (unsigned int)tcptowrite(a1, v38, 28LL, 5000LL) == 28 )
{
while ( 1 )
{
while ( 1 )
{
if ( (unsigned int)tcptoread(a1, v38, 8LL, 5000LL) != 8 )
{
v23 = _errno_location();
v24 = (const char *)strerr((unsigned int)*v23);
syslog(5, "readblock; tcpread error: %s", v24);
return 0xFFFFFFFFLL;
}
v37 = (unsigned __int8 *)v38;
v11 = get32bit(&v37);
v12 = get32bit(&v37);
v13 = v12;
if ( v11 == 201 )
{
if ( v12 == 9 )
{
if ( (unsigned int)tcptoread(a1, v38, 9LL, 5000LL) == 9 )
{
v37 = (unsigned __int8 *)v38;
v25 = get64bit(&v37);
if ( *v37 )
{
v32 = mfsstrerr(*v37);
syslog(5, "readblock; READ_STATUS status: %s", v32);
result = 0xFFFFFFFFLL;
}
else if ( a2 == v25 )
{
if ( a5 )
{
syslog(5, "readblock; READ_STATUS incorrect data size (left: %u)", a5);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
syslog(5, "readblock; READ_STATUS incorrect chunkid (got:%lu expected:%lu)", v25, a2);
result = 0xFFFFFFFFLL;
}
}
else
{
v30 = _errno_location();
v31 = (const char *)strerr((unsigned int)*v30);
syslog(5, "readblock; READ_STATUS tcpread error: %s", v31);
result = 0xFFFFFFFFLL;
}
}
else
{
syslog(5, "readblock; READ_STATUS incorrect message size (%u/9)", v12);
result = 0xFFFFFFFFLL;
}
return result;
}
if ( v11 != 202 )
break;
if ( v12 <= 0x13 )
{
syslog(5, "readblock; READ_DATA incorrect message size (%u/>=20)", v12);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)tcptoread(a1, v38, 20LL, 5000LL) != 20 )
goto LABEL_31;
v37 = (unsigned __int8 *)v38;
v15 = get64bit(&v37);
if ( a2 != v15 )
{
syslog(5, "readblock; READ_DATA incorrect chunkid (got:%lu expected:%lu)", v15, a2);
return 0xFFFFFFFFLL;
}
v16 = (unsigned __int16)get16bit(&v37);
get16bit(&v37);
v17 = get32bit(&v37);
v18 = get32bit(&v37);
v20 = v17 + 20;
v34 = v18;
if ( (_DWORD)v20 != v13 )
{
syslog(5, "readblock; READ_DATA incorrect message size (%u/%u)", v13, v20);
return 0xFFFFFFFFLL;
}
if ( !v17 )
{
syslog(5, "readblock; READ_DATA empty block");
return 0xFFFFFFFFLL;
}
v21 = HIWORD(a4);
if ( v16 != (_DWORD)v21 )
{
syslog(5, "readblock; READ_DATA incorrect block number (got:%u expected:%u)", v16, v21);
return 0xFFFFFFFFLL;
}
if ( v19 != (unsigned __int16)a4 )
{
syslog(5, "readblock; READ_DATA incorrect block offset (got:%u expected:%u)", v19, (unsigned __int16)a4);
return 0xFFFFFFFFLL;
}
v22 = 0x10000 - (unsigned int)(unsigned __int16)a4;
if ( a5 <= (unsigned int)v22 )
v22 = a5;
if ( v17 != (_DWORD)v22 )
{
syslog(5, "readblock; READ_DATA incorrect block size (got:%u expected:%u)", v17, v22);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)tcptoread(a1, a6, v17, 5000LL) != v17 )
{
LABEL_31:
v26 = _errno_location();
v27 = (const char *)strerr((unsigned int)*v26);
syslog(5, "readblock; READ_DATA tcpread error: %s", v27);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)mycrc32(0LL, a6, v17) != v34 )
{
syslog(5, "readblock; READ_DATA crc checksum error");
return 0xFFFFFFFFLL;
}
a4 += v17;
a5 -= v17;
a6 += v17;
}
if ( v11 )
break;
if ( v12 )
{
syslog(5, "readblock; NOP incorrect message size (%u/0)", v12);
return 0xFFFFFFFFLL;
}
}
if ( (unsigned int)(v11 - 1) <= 1 )
syslog(5, "readblock; got UNKNOWN_COMMAND/BAD_COMMAND_SIZE !!!");
else
syslog(5, "readblock; unknown message");
result = 0xFFFFFFFFLL;
}
else
{
v28 = _errno_location();
v29 = (const char *)strerr((unsigned int)*v28);
syslog(5, "readblock; tcpwrite error: %s", v29);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5, __int64 a6)
{
unsigned int v8; // er10
unsigned int v9; // er11
unsigned int v10; // er8
int v11; // esi
unsigned int v12; // eax
unsigned int v13; // er15
__int64 result; // rax
unsigned __int64 v15; // rax
unsigned int v16; // esi
unsigned int v17; // er13
int v18; // eax
unsigned int v19; // er9
__int64 v20; // rcx
__int64 v21; // rcx
__int64 v22; // rcx
int *v23; // rax
const char *v24; // rax
unsigned __int64 v25; // rax
int *v26; // rax
const char *v27; // rax
int *v28; // rax
const char *v29; // rax
int *v30; // rax
const char *v31; // rax
char *v32; // rax
int v34; // [rsp+4h] [rbp-74h]
char *v36; // [rsp+10h] [rbp-68h] BYREF
unsigned __int8 *v37; // [rsp+18h] [rbp-60h] BYREF
char v38[88]; // [rsp+20h] [rbp-58h] BYREF

v36 = v38;
func1((__int64)&v36, 0xC8u);
func1((__int64)&v36, 0x14u);
func0((__int64 *)&v36, a2);
func1((__int64)&v36, v8);
func1((__int64)&v36, v9);
func1((__int64)&v36, v10);
if ( (unsigned int)tcptowrite(a1, v38, 28LL, 5000LL) == 28 )
{
while ( 1 )
{
while ( 1 )
{
if ( (unsigned int)tcptoread(a1, v38, 8LL, 5000LL) != 8 )
{
v23 = _errno_location();
v24 = (const char *)strerr((unsigned int)*v23);
syslog(5, "readblock; tcpread error: %s", v24);
return 0xFFFFFFFFLL;
}
v37 = (unsigned __int8 *)v38;
v11 = func3(&v37);
v12 = func3(&v37);
v13 = v12;
if ( v11 == 201 )
{
if ( v12 == 9 )
{
if ( (unsigned int)tcptoread(a1, v38, 9LL, 5000LL) == 9 )
{
v37 = (unsigned __int8 *)v38;
v25 = func2(&v37);
if ( *v37 )
{
v32 = func5(*v37);
syslog(5, "readblock; READ_STATUS status: %s", v32);
result = 0xFFFFFFFFLL;
}
else if ( a2 == v25 )
{
if ( a5 )
{
syslog(5, "readblock; READ_STATUS incorrect data size (left: %u)", a5);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
else
{
syslog(5, "readblock; READ_STATUS incorrect chunkid (got:%lu expected:%lu)", v25, a2);
result = 0xFFFFFFFFLL;
}
}
else
{
v30 = _errno_location();
v31 = (const char *)strerr((unsigned int)*v30);
syslog(5, "readblock; READ_STATUS tcpread error: %s", v31);
result = 0xFFFFFFFFLL;
}
}
else
{
syslog(5, "readblock; READ_STATUS incorrect message size (%u/9)", v12);
result = 0xFFFFFFFFLL;
}
return result;
}
if ( v11 != 202 )
break;
if ( v12 <= 0x13 )
{
syslog(5, "readblock; READ_DATA incorrect message size (%u/>=20)", v12);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)tcptoread(a1, v38, 20LL, 5000LL) != 20 )
goto LABEL_31;
v37 = (unsigned __int8 *)v38;
v15 = func2(&v37);
if ( a2 != v15 )
{
syslog(5, "readblock; READ_DATA incorrect chunkid (got:%lu expected:%lu)", v15, a2);
return 0xFFFFFFFFLL;
}
v16 = (unsigned __int16)func4(&v37);
func4(&v37);
v17 = func3(&v37);
v18 = func3(&v37);
v20 = v17 + 20;
v34 = v18;
if ( (_DWORD)v20 != v13 )
{
syslog(5, "readblock; READ_DATA incorrect message size (%u/%u)", v13, v20);
return 0xFFFFFFFFLL;
}
if ( !v17 )
{
syslog(5, "readblock; READ_DATA empty block");
return 0xFFFFFFFFLL;
}
v21 = HIWORD(a4);
if ( v16 != (_DWORD)v21 )
{
syslog(5, "readblock; READ_DATA incorrect block number (got:%u expected:%u)", v16, v21);
return 0xFFFFFFFFLL;
}
if ( v19 != (unsigned __int16)a4 )
{
syslog(5, "readblock; READ_DATA incorrect block offset (got:%u expected:%u)", v19, (unsigned __int16)a4);
return 0xFFFFFFFFLL;
}
v22 = 0x10000 - (unsigned int)(unsigned __int16)a4;
if ( a5 <= (unsigned int)v22 )
v22 = a5;
if ( v17 != (_DWORD)v22 )
{
syslog(5, "readblock; READ_DATA incorrect block size (got:%u expected:%u)", v17, v22);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)tcptoread(a1, a6, v17, 5000LL) != v17 )
{
LABEL_31:
v26 = _errno_location();
v27 = (const char *)strerr((unsigned int)*v26);
syslog(5, "readblock; READ_DATA tcpread error: %s", v27);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)mycrc32(0LL, a6, v17) != v34 )
{
syslog(5, "readblock; READ_DATA crc checksum error");
return 0xFFFFFFFFLL;
}
a4 += v17;
a5 -= v17;
a6 += v17;
}
if ( v11 )
break;
if ( v12 )
{
syslog(5, "readblock; NOP incorrect message size (%u/0)", v12);
return 0xFFFFFFFFLL;
}
}
if ( (unsigned int)(v11 - 1) <= 1 )
syslog(5, "readblock; got UNKNOWN_COMMAND/BAD_COMMAND_SIZE !!!");
else
syslog(5, "readblock; unknown message");
result = 0xFFFFFFFFLL;
}
else
{
v28 = _errno_location();
v29 = (const char *)strerr((unsigned int)*v28);
syslog(5, "readblock; tcpwrite error: %s", v29);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int cs_readblock(int fd,uint64_t chunkid,uint32_t version,uint32_t offset,uint32_t size,uint8_t *buff) {
 uint8_t *wptr,ibuff[28];
 const uint8_t *rptr;
 wptr = ibuff;
 put32bit(&wptr,(0 +200));
 put32bit(&wptr,20);
 put64bit(&wptr,chunkid);
 put32bit(&wptr,version);
 put32bit(&wptr,offset);
 put32bit(&wptr,size);
 if (tcptowrite(fd,ibuff,28,5000)!=28) {
  syslog(5,"readblock; tcpwrite error: %s",strerr((*__errno_location ())));
  return -1;
 }
 for (;;) {
  uint32_t cmd,l;
  uint64_t t64;
  uint16_t blockno,blockoffset;
  uint32_t breq,blocksize,blockcrc;
  if (tcptoread(fd,ibuff,8,5000)!=8) {
   syslog(5,"readblock; tcpread error: %s",strerr((*__errno_location ())));
   return -1;
  }
  rptr = ibuff;
  cmd = get32bit(&rptr);
  l = get32bit(&rptr);
  if (cmd==(0 +201)) {
   if (l!=9) {
    syslog(5,"readblock; READ_STATUS incorrect message size (%""u""/9)",l);
    return -1;
   }
   if (tcptoread(fd,ibuff,9,5000)!=9) {
    syslog(5,"readblock; READ_STATUS tcpread error: %s",strerr((*__errno_location ())));
    return -1;
   }
   rptr = ibuff;
   t64 = get64bit(&rptr);
   if (*rptr!=0) {
    syslog(5,"readblock; READ_STATUS status: %s",mfsstrerr(*rptr));
    return -1;
   }
   if (t64!=chunkid) {
    syslog(5,"readblock; READ_STATUS incorrect chunkid (got:%""l" "u"" expected:%""l" "u"")",t64,chunkid);
    return -1;
   }
   if (size!=0) {
    syslog(5,"readblock; READ_STATUS incorrect data size (left: %""u"")",size);
    return -1;
   }
   return 0;
  } else if (cmd==(0 +202)) {
   if (l<20) {
    syslog(5,"readblock; READ_DATA incorrect message size (%""u""/>=20)",l);
    return -1;
   }
   if (tcptoread(fd,ibuff,20,5000)!=20) {
    syslog(5,"readblock; READ_DATA tcpread error: %s",strerr((*__errno_location ())));
    return -1;
   }
   rptr = ibuff;
   t64 = get64bit(&rptr);
   if (t64!=chunkid) {
    syslog(5,"readblock; READ_DATA incorrect chunkid (got:%""l" "u"" expected:%""l" "u"")",t64,chunkid);
    return -1;
   }
   blockno = get16bit(&rptr);
   blockoffset = get16bit(&rptr);
   blocksize = get32bit(&rptr);
   blockcrc = get32bit(&rptr);
   if (l!=20+blocksize) {
    syslog(5,"readblock; READ_DATA incorrect message size (%""u""/%""u"")",l,20+blocksize);
    return -1;
   }
   if (blocksize==0) {
    syslog(5,"readblock; READ_DATA empty block");
    return -1;
   }
   if (blockno!=(offset>>16)) {
    syslog(5,"readblock; READ_DATA incorrect block number (got:%""u"" expected:%""u"")",blockno,(offset>>16));
    return -1;
   }
   if (blockoffset!=(offset&0x0FFFF)) {
    syslog(5,"readblock; READ_DATA incorrect block offset (got:%""u"" expected:%""u"")",blockoffset,(offset&0x0FFFF));
    return -1;
   }
   breq = 0x10000 - (uint32_t)blockoffset;
   if (size<breq) {
    breq=size;
   }
   if (blocksize!=breq) {
    syslog(5,"readblock; READ_DATA incorrect block size (got:%""u"" expected:%""u"")",blocksize,breq);
    return -1;
   }
   if (tcptoread(fd,buff,blocksize,5000)!=(int32_t)blocksize) {
    syslog(5,"readblock; READ_DATA tcpread error: %s",strerr((*__errno_location ())));
    return -1;
   }
   if (blockcrc!=mycrc32(0,buff,blocksize)) {
    syslog(5,"readblock; READ_DATA crc checksum error");
    return -1;
   }
   offset+=blocksize;
   size-=blocksize;
   buff+=blocksize;
  } else if (cmd==0) {
   if (l!=0) {
    syslog(5,"readblock; NOP incorrect message size (%""u""/0)",l);
    return -1;
   }
  } else if (cmd==1 || cmd==2) {
   syslog(5,"readblock; got UNKNOWN_COMMAND/BAD_COMMAND_SIZE !!!");
   return -1;
  } else {
   syslog(5,"readblock; unknown message");
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int cs_readblock(int fd, unsigned long chunkid, unsigned int version, unsigned int offset, unsigned int size, unsigned char *buff) {
 unsigned char *wptr; unsigned char ibuff[28];
 unsigned char *rptr;
 wptr = ibuff;
 put32bit(&wptr,(0 +200));
 put32bit(&wptr,20);
 put64bit(&wptr,chunkid);
 put32bit(&wptr,version);
 put32bit(&wptr,offset);
 put32bit(&wptr,size);
 if (tcptowrite(fd,ibuff,28,5000)!=28) {
  syslog(5,"readblock; tcpwrite error: %s",strerr((*__errno_location ())));
  return -1;
 }
 for (;;) {
  unsigned int cmd; unsigned int l;
  unsigned long t64;
  unsigned short blockno; unsigned short blockoffset;
  unsigned int breq; unsigned int blocksize; unsigned int blockcrc;
  if (tcptoread(fd,ibuff,8,5000)!=8) {
   syslog(5,"readblock; tcpread error: %s",strerr((*__errno_location ())));
   return -1;
  }
  rptr = ibuff;
  cmd = get32bit(&rptr);
  l = get32bit(&rptr);
  if (cmd==(0 +201)) {
   if (l!=9) {
    syslog(5,"readblock; READ_STATUS incorrect message size (%""u""/9)",l);
    return -1;
   }
   if (tcptoread(fd,ibuff,9,5000)!=9) {
    syslog(5,"readblock; READ_STATUS tcpread error: %s",strerr((*__errno_location ())));
    return -1;
   }
   rptr = ibuff;
   t64 = get64bit(&rptr);
   if (*rptr!=0) {
    syslog(5,"readblock; READ_STATUS status: %s",mfsstrerr(*rptr));
    return -1;
   }
   if (t64!=chunkid) {
    syslog(5,"readblock; READ_STATUS incorrect chunkid (got:%""l" "u"" expected:%""l" "u"")",t64,chunkid);
    return -1;
   }
   if (size!=0) {
    syslog(5,"readblock; READ_STATUS incorrect data size (left: %""u"")",size);
    return -1;
   }
   return 0;
  } else if (cmd==(0 +202)) {
   if (l<20) {
    syslog(5,"readblock; READ_DATA incorrect message size (%""u""/>=20)",l);
    return -1;
   }
   if (tcptoread(fd,ibuff,20,5000)!=20) {
    syslog(5,"readblock; READ_DATA tcpread error: %s",strerr((*__errno_location ())));
    return -1;
   }
   rptr = ibuff;
   t64 = get64bit(&rptr);
   if (t64!=chunkid) {
    syslog(5,"readblock; READ_DATA incorrect chunkid (got:%""l" "u"" expected:%""l" "u"")",t64,chunkid);
    return -1;
   }
   blockno = get16bit(&rptr);
   blockoffset = get16bit(&rptr);
   blocksize = get32bit(&rptr);
   blockcrc = get32bit(&rptr);
   if (l!=20+blocksize) {
    syslog(5,"readblock; READ_DATA incorrect message size (%""u""/%""u"")",l,20+blocksize);
    return -1;
   }
   if (blocksize==0) {
    syslog(5,"readblock; READ_DATA empty block");
    return -1;
   }
   if (blockno!=(offset>>16)) {
    syslog(5,"readblock; READ_DATA incorrect block number (got:%""u"" expected:%""u"")",blockno,(offset>>16));
    return -1;
   }
   if (blockoffset!=(offset&0x0FFFF)) {
    syslog(5,"readblock; READ_DATA incorrect block offset (got:%""u"" expected:%""u"")",blockoffset,(offset&0x0FFFF));
    return -1;
   }
   breq = 0x10000 - (unsigned int)blockoffset;
   if (size<breq) {
    breq=size;
   }
   if (blocksize!=breq) {
    syslog(5,"readblock; READ_DATA incorrect block size (got:%""u"" expected:%""u"")",blocksize,breq);
    return -1;
   }
   if (tcptoread(fd,buff,blocksize,5000)!=(int)blocksize) {
    syslog(5,"readblock; READ_DATA tcpread error: %s",strerr((*__errno_location ())));
    return -1;
   }
   if (blockcrc!=mycrc32(0,buff,blocksize)) {
    syslog(5,"readblock; READ_DATA crc checksum error");
    return -1;
   }
   offset+=blocksize;
   size-=blocksize;
   buff+=blocksize;
  } else if (cmd==0) {
   if (l!=0) {
    syslog(5,"readblock; NOP incorrect message size (%""u""/0)",l);
    return -1;
   }
  } else if (cmd==1 || cmd==2) {
   syslog(5,"readblock; got UNKNOWN_COMMAND/BAD_COMMAND_SIZE !!!");
   return -1;
  } else {
   syslog(5,"readblock; unknown message");
   return -1;
  }
 }
 return 0;
}

variable_types: fd int

variable_types:
chunkid
unsigned long

variable_types:
version
unsigned int

variable_types:
offset
unsigned int

variable_types:
size
unsigned int

variable_types:
buff
unsigned char *

variable_types:
wptr
unsigned char *

variable_types:
ibuff
unsigned char [28]

variable_types:
rptr
unsigned char *

variable_types:
cmd
unsigned int

variable_types:
l
unsigned int

variable_types:
blockno
unsigned short

variable_types:
blockoffset
unsigned short

variable_types:
breq
unsigned int

variable_types:
blocksize
unsigned int

variable_types:
blockcrc
unsigned int


findfirstlogversion

decompiled_code:
__int64 __fastcall findfirstlogversion(const char *a1)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
__int64 v4; // rdx
__int64 v5; // r8
int v6; // eax
char v8[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open64(a1, 0);
if ( v1 < 0 )
return 0LL;
v2 = v1;
v3 = read(v1, v8, 0x32uLL);
close(v2);
if ( v3 <= 0 )
return 0LL;
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = (unsigned __int8)v8[v4];
if ( (unsigned __int8)(v6 - 48) > 9u )
break;
v5 = v6 - 48 + 10 * v5;
if ( v4 == v3 - 1 )
return 0LL;
++v4;
}
if ( v3 <= (int)v4 || (_BYTE)v6 != 58 )
return 0LL;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
__int64 v4; // rdx
__int64 v5; // r8
int v6; // eax
char v8[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open64(a1, 0);
if ( v1 < 0 )
return 0LL;
v2 = v1;
v3 = read(v1, v8, 0x32uLL);
close(v2);
if ( v3 <= 0 )
return 0LL;
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = (unsigned __int8)v8[v4];
if ( (unsigned __int8)(v6 - 48) > 9u )
break;
v5 = v6 - 48 + 10 * v5;
if ( v4 == v3 - 1 )
return 0LL;
++v4;
}
if ( v3 <= (int)v4 || (_BYTE)v6 != 58 )
return 0LL;
return v5;
}

original_code:
uint64_t findfirstlogversion(const char *fname) {
 uint8_t buff[50];
 int32_t s,p;
 uint64_t fv;
 int fd;
 fd = open(fname,00);
 if (fd<0) {
  return 0;
 }
 s = read(fd,buff,50);
 close(fd);
 if (s<=0) {
  return 0;
 }
 fv = 0;
 p = 0;
 while (p<s && buff[p]>='0' && buff[p]<='9') {
  fv *= 10;
  fv += buff[p]-'0';
  p++;
 }
 if (p>=s || buff[p]!=':') {
  return 0;
 }
 return fv;
}

canonical_original_code:
unsigned long findfirstlogversion(char *fname) {
 unsigned char buff[50];
 int s; int p;
 unsigned long fv;
 int fd;
 fd = open(fname,00);
 if (fd<0) {
  return 0;
 }
 s = read(fd,buff,50);
 close(fd);
 if (s<=0) {
  return 0;
 }
 fv = 0;
 p = 0;
 while (p<s && buff[p]>='0' && buff[p]<='9') {
  fv *= 10;
  fv += buff[p]-'0';
  p++;
 }
 if (p>=s || buff[p]!=':') {
  return 0;
 }
 return fv;
}

variable_types:
fname
char *

variable_types:
buff
unsigned char [50]

variable_types: s int

variable_types: p int

variable_types:
fv
unsigned long


findlastlogversion

original_code:
uint64_t findlastlogversion(const char *fname) {
 struct stat st;
 uint8_t buff[32800];
 uint64_t size;
 uint32_t buffpos;
 uint64_t lastnewline,lv;
 int fd;
 fd = open(fname,00);
 if (fd<0) {
  return 0;
 }
 fstat(fd,&st);
 size = st.st_size;
 memset(buff,0,32);
 lastnewline = 0;
 while (size>0 && size+200000>(uint64_t)(st.st_size)) {
  if (size>32768) {
   memcpy(buff+32768,buff,32);
   size-=32768;
   lseek(fd,size,0);
   if (read(fd,buff,32768)!=32768) {
    close(fd);
    return 0;
   }
   buffpos = 32768;
  } else {
   memmove(buff+size,buff,32);
   lseek(fd,0,0);
   if (read(fd,buff,size)!=(ssize_t)size) {
    close(fd);
    return 0;
   }
   buffpos = size;
   size = 0;
  }
  while (buffpos>0) {
   buffpos--;
   if (buff[buffpos]=='\n') {
    if (lastnewline==0) {
     lastnewline = size + buffpos;
    } else {
     if (lastnewline+1 != (uint64_t)(st.st_size)) {
      close(fd);
      return 0;
     }
     buffpos++;
     lv = 0;
     while (buffpos<32800 && buff[buffpos]>='0' && buff[buffpos]<='9') {
      lv *= 10;
      lv += buff[buffpos]-'0';
      buffpos++;
     }
     if (buffpos==32800 || buff[buffpos]!=':') {
      lv = 0;
     }
     close(fd);
     return lv;
    }
   }
  }
 }
 close(fd);
 return 0;
}

canonical_original_code:
unsigned long findlastlogversion(char *fname) {
 struct stat st;
 unsigned char buff[32800];
 unsigned long size;
 unsigned int buffpos;
 unsigned long lastnewline; unsigned long lv;
 int fd;
 fd = open(fname,00);
 if (fd<0) {
  return 0;
 }
 fstat(fd,&st);
 size = st.st_size;
 memset(buff,0,32);
 lastnewline = 0;
 while (size>0 && size+200000>(unsigned long)(st.st_size)) {
  if (size>32768) {
   memcpy(buff+32768,buff,32);
   size-=32768;
   lseek(fd,size,0);
   if (read(fd,buff,32768)!=32768) {
    close(fd);
    return 0;
   }
   buffpos = 32768;
  } else {
   memmove(buff+size,buff,32);
   lseek(fd,0,0);
   if (read(fd,buff,size)!=(long)size) {
    close(fd);
    return 0;
   }
   buffpos = size;
   size = 0;
  }
  while (buffpos>0) {
   buffpos--;
   if (buff[buffpos]=='\n') {
    if (lastnewline==0) {
     lastnewline = size + buffpos;
    } else {
     if (lastnewline+1 != (unsigned long)(st.st_size)) {
      close(fd);
      return 0;
     }
     buffpos++;
     lv = 0;
     while (buffpos<32800 && buff[buffpos]>='0' && buff[buffpos]<='9') {
      lv *= 10;
      lv += buff[buffpos]-'0';
      buffpos++;
     }
     if (buffpos==32800 || buff[buffpos]!=':') {
      lv = 0;
     }
     close(fd);
     return lv;
    }
   }
  }
 }
 close(fd);
 return 0;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
buff
unsigned char [32800]

variable_types:
size
unsigned long

variable_types:
buffpos
unsigned int

variable_types:
lastnewline
unsigned long

variable_types:
lv
unsigned long

variable_types: fd int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }