decompiled_code:
__int64 __fastcall topology_parsenet(char *a1, int *a2, int *a3)
{
unsigned int v4; // er11
unsigned int v5; // er9
char v6; // al
unsigned int v7; // edx
char *v8; // rcx
__int64 result; // rax
char v10; // al
char *v11; // rcx
unsigned int v12; // er10
unsigned int v13; // er9
char v14; // al
unsigned int v15; // edx
char *v16; // rdi
char *v17; // rcx
unsigned int v18; // er12
unsigned int v19; // ebx
int v20; // er11
char v21; // al
unsigned int v22; // edx
char *v23; // rdi

v4 = 1;
v5 = 0;
if ( *a1 == 42 )
{
result = 0xFFFFFFFFLL;
if ( !a1[1] )
{
*a2 = 0;
*a3 = -1;
result = 0LL;
}
return result;
}
while ( 1 )
{
v6 = *a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v7 = 0;
do
{
v7 = v6 + 10 * v7 - 48;
v8 = a1++;
if ( v7 > 0xFF )
return 0xFFFFFFFFLL;
v6 = *a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
if ( v4 - 1 > 2 )
break;
if ( v6 != 46 )
return 0xFFFFFFFFLL;
a1 = v8 + 2;
v5 = v7 + (v5 << 8);
LABEL_9:
++v4;
}
v5 = v7 + (v5 << 8);
if ( v4 <= 3 )
goto LABEL_9;
v10 = *a1;
if ( !*a1 )
{
*a2 = v5;
*a3 = v5;
return 0LL;
}
if ( v10 != 47 )
{
if ( v10 != 45 )
return 0xFFFFFFFFLL;
*a2 = v5;
v11 = v8 + 2;
v12 = 1;
v13 = 0;
while ( 2 )
{
v14 = *v11;
if ( (unsigned __int8)(*v11 - 48) > 9u )
return 0xFFFFFFFFLL;
v15 = 0;
do
{
v15 = v14 + 10 * v15 - 48;
v16 = v11++;
if ( v15 > 0xFF )
return 0xFFFFFFFFLL;
v14 = *v11;
}
while ( (unsigned __int8)(*v11 - 48) <= 9u );
if ( v12 - 1 > 2 )
{
v13 = v15 + (v13 << 8);
if ( v12 <= 3 )
goto LABEL_24;
if ( *v11 )
{
result = 0xFFFFFFFFLL;
}
else
{
*a3 = v13;
result = 0LL;
}
}
else
{
if ( v14 == 46 )
{
v11 = v16 + 2;
v13 = v15 + (v13 << 8);
LABEL_24:
++v12;
continue;
}
result = 0xFFFFFFFFLL;
}
return result;
}
}
*a2 = v5;
v17 = v8 + 2;
v18 = 1;
v19 = 0;
v20 = 0;
while ( 2 )
{
v21 = *v17;
if ( (unsigned __int8)(*v17 - 48) > 9u )
return 0xFFFFFFFFLL;
v22 = 0;
do
{
v22 = v21 + 10 * v22 - 48;
v23 = v17++;
if ( v22 > 0xFF )
return 0xFFFFFFFFLL;
v21 = *v17;
}
while ( (unsigned __int8)(*v17 - 48) <= 9u );
if ( v22 <= 0x20 && v21 == 0 && !v19 )
{
v20 = -1;
if ( v22 <= 0x1F )
v20 = -1 << (32 - v22);
goto LABEL_41;
}
if ( v19 > 2 )
{
v20 = v22 + (v20 << 8);
if ( v18 <= 3 )
goto LABEL_36;
if ( *v17 )
return 0xFFFFFFFFLL;
LABEL_41:
*a2 = v20 & v5;
*a3 = ~v20 | v5;
return 0LL;
}
if ( v21 == 46 )
{
v17 = v23 + 2;
v20 = v22 + (v20 << 8);
LABEL_36:
++v19;
++v18;
continue;
}
return 0xFFFFFFFFLL;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, int *a2, int *a3)
{
unsigned int v4; // er11
unsigned int v5; // er9
char v6; // al
unsigned int v7; // edx
char *v8; // rcx
__int64 result; // rax
char v10; // al
char *v11; // rcx
unsigned int v12; // er10
unsigned int v13; // er9
char v14; // al
unsigned int v15; // edx
char *v16; // rdi
char *v17; // rcx
unsigned int v18; // er12
unsigned int v19; // ebx
int v20; // er11
char v21; // al
unsigned int v22; // edx
char *v23; // rdi

v4 = 1;
v5 = 0;
if ( *a1 == 42 )
{
result = 0xFFFFFFFFLL;
if ( !a1[1] )
{
*a2 = 0;
*a3 = -1;
result = 0LL;
}
return result;
}
while ( 1 )
{
v6 = *a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v7 = 0;
do
{
v7 = v6 + 10 * v7 - 48;
v8 = a1++;
if ( v7 > 0xFF )
return 0xFFFFFFFFLL;
v6 = *a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
if ( v4 - 1 > 2 )
break;
if ( v6 != 46 )
return 0xFFFFFFFFLL;
a1 = v8 + 2;
v5 = v7 + (v5 << 8);
LABEL_9:
++v4;
}
v5 = v7 + (v5 << 8);
if ( v4 <= 3 )
goto LABEL_9;
v10 = *a1;
if ( !*a1 )
{
*a2 = v5;
*a3 = v5;
return 0LL;
}
if ( v10 != 47 )
{
if ( v10 != 45 )
return 0xFFFFFFFFLL;
*a2 = v5;
v11 = v8 + 2;
v12 = 1;
v13 = 0;
while ( 2 )
{
v14 = *v11;
if ( (unsigned __int8)(*v11 - 48) > 9u )
return 0xFFFFFFFFLL;
v15 = 0;
do
{
v15 = v14 + 10 * v15 - 48;
v16 = v11++;
if ( v15 > 0xFF )
return 0xFFFFFFFFLL;
v14 = *v11;
}
while ( (unsigned __int8)(*v11 - 48) <= 9u );
if ( v12 - 1 > 2 )
{
v13 = v15 + (v13 << 8);
if ( v12 <= 3 )
goto LABEL_24;
if ( *v11 )
{
result = 0xFFFFFFFFLL;
}
else
{
*a3 = v13;
result = 0LL;
}
}
else
{
if ( v14 == 46 )
{
v11 = v16 + 2;
v13 = v15 + (v13 << 8);
LABEL_24:
++v12;
continue;
}
result = 0xFFFFFFFFLL;
}
return result;
}
}
*a2 = v5;
v17 = v8 + 2;
v18 = 1;
v19 = 0;
v20 = 0;
while ( 2 )
{
v21 = *v17;
if ( (unsigned __int8)(*v17 - 48) > 9u )
return 0xFFFFFFFFLL;
v22 = 0;
do
{
v22 = v21 + 10 * v22 - 48;
v23 = v17++;
if ( v22 > 0xFF )
return 0xFFFFFFFFLL;
v21 = *v17;
}
while ( (unsigned __int8)(*v17 - 48) <= 9u );
if ( v22 <= 0x20 && v21 == 0 && !v19 )
{
v20 = -1;
if ( v22 <= 0x1F )
v20 = -1 << (32 - v22);
goto LABEL_41;
}
if ( v19 > 2 )
{
v20 = v22 + (v20 << 8);
if ( v18 <= 3 )
goto LABEL_36;
if ( *v17 )
return 0xFFFFFFFFLL;
LABEL_41:
*a2 = v20 & v5;
*a3 = ~v20 | v5;
return 0LL;
}
if ( v21 == 46 )
{
v17 = v23 + 2;
v20 = v22 + (v20 << 8);
LABEL_36:
++v19;
++v18;
continue;
}
return 0xFFFFFFFFLL;
}
}

original_code:
int topology_parsenet(char *net,uint32_t *fromip,uint32_t *toip) {
 uint32_t ip,i,octet;
 if (net[0]=='*' && net[1]==0) {
  *fromip = 0;
  *toip = 0xFFFFFFFFU;
  return 0;
 }
 ip=0;
 for (i=0 ; i<4; i++) {
  if (*net>='0' && *net<='9') {
   octet=0;
   while (*net>='0' && *net<='9') {
    octet*=10;
    octet+=(*net)-'0';
    net++;
    if (octet>255) {
     return -1;
    }
   }
  } else {
   return -1;
  }
  if (i<3) {
   if (*net!='.') {
    return -1;
   }
   net++;
  }
  ip*=256;
  ip+=octet;
 }
 if (*net==0) {
  *fromip = ip;
  *toip = ip;
  return 0;
 }
 if (*net=='/') {
  *fromip = ip;
  ip=0;
  net++;
  for (i=0 ; i<4; i++) {
   if (*net>='0' && *net<='9') {
    octet=0;
    while (*net>='0' && *net<='9') {
     octet*=10;
     octet+=(*net)-'0';
     net++;
     if (octet>255) {
      return -1;
     }
    }
   } else {
    return -1;
   }
   if (i==0 && *net==0 && octet<=32) {
    ip = 0xFFFFFFFF;
    if (octet<32) {
     ip<<=32-octet;
    }
    break;
   }
   if (i<3) {
    if (*net!='.') {
     return -1;
    }
    net++;
   }
   ip*=256;
   ip+=octet;
  }
  if (*net!=0) {
   return -1;
  }
  *fromip &= ip;
  *toip = *fromip | (ip ^ 0xFFFFFFFFU);
  return 0;
 }
 if (*net=='-') {
  *fromip = ip;
  ip=0;
  net++;
  for (i=0 ; i<4; i++) {
   if (*net>='0' && *net<='9') {
    octet=0;
    while (*net>='0' && *net<='9') {
     octet*=10;
     octet+=*net-'0';
     net++;
     if (octet>255) {
      return -1;
     }
    }
   } else {
    return -1;
   }
   if (i<3) {
    if (*net!='.') {
     return -1;
    }
    net++;
   }
   ip*=256;
   ip+=octet;
  }
  if (*net!=0) {
   return -1;
  }
  *toip = ip;
  return 0;
 }
 return -1;
}

canonical_original_code:
int topology_parsenet(char *net, unsigned int *fromip, unsigned int *toip) {
 unsigned int ip; unsigned int i; unsigned int octet;
 if (net[0]=='*' && net[1]==0) {
  *fromip = 0;
  *toip = 0xFFFFFFFFU;
  return 0;
 }
 ip=0;
 for (i=0 ; i<4; i++) {
  if (*net>='0' && *net<='9') {
   octet=0;
   while (*net>='0' && *net<='9') {
    octet*=10;
    octet+=(*net)-'0';
    net++;
    if (octet>255) {
     return -1;
    }
   }
  } else {
   return -1;
  }
  if (i<3) {
   if (*net!='.') {
    return -1;
   }
   net++;
  }
  ip*=256;
  ip+=octet;
 }
 if (*net==0) {
  *fromip = ip;
  *toip = ip;
  return 0;
 }
 if (*net=='/') {
  *fromip = ip;
  ip=0;
  net++;
  for (i=0 ; i<4; i++) {
   if (*net>='0' && *net<='9') {
    octet=0;
    while (*net>='0' && *net<='9') {
     octet*=10;
     octet+=(*net)-'0';
     net++;
     if (octet>255) {
      return -1;
     }
    }
   } else {
    return -1;
   }
   if (i==0 && *net==0 && octet<=32) {
    ip = 0xFFFFFFFF;
    if (octet<32) {
     ip<<=32-octet;
    }
    break;
   }
   if (i<3) {
    if (*net!='.') {
     return -1;
    }
    net++;
   }
   ip*=256;
   ip+=octet;
  }
  if (*net!=0) {
   return -1;
  }
  *fromip &= ip;
  *toip = *fromip | (ip ^ 0xFFFFFFFFU);
  return 0;
 }
 if (*net=='-') {
  *fromip = ip;
  ip=0;
  net++;
  for (i=0 ; i<4; i++) {
   if (*net>='0' && *net<='9') {
    octet=0;
    while (*net>='0' && *net<='9') {
     octet*=10;
     octet+=*net-'0';
     net++;
     if (octet>255) {
      return -1;
     }
    }
   } else {
    return -1;
   }
   if (i<3) {
    if (*net!='.') {
     return -1;
    }
    net++;
   }
   ip*=256;
   ip+=octet;
  }
  if (*net!=0) {
   return -1;
  }
  *toip = ip;
  return 0;
 }
 return -1;
}

variable_types:
net
char *

variable_types:
fromip
unsigned int *

variable_types:
toip
unsigned int *

variable_types:
octet
unsigned int


repo: twonly/mfs

decompiled_code:
__int64 __fastcall topology_distance(unsigned int a1, unsigned int a2)
{
__int64 result; // rax
int v3; // ebp
int v4; // eax

result = 0LL;
if ( a1 != a2 )
{
v3 = itree_find(racktree, a1);
v4 = itree_find(racktree, a2);
LOBYTE(v4) = v3 != v4;
result = (unsigned int)(v4 + 1);
}
return result;
}

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

result = 0LL;
if ( a1 != a2 )
{
v3 = itree_find(racktree, a1);
v4 = itree_find(racktree, a2);
LOBYTE(v4) = v3 != v4;
result = (unsigned int)(v4 + 1);
}
return result;
}

original_code:
uint8_t topology_distance(uint32_t ip1,uint32_t ip2) {
 uint32_t rid1,rid2;
 if (ip1==ip2) {
  return 0;
 }
 rid1 = itree_find(racktree,ip1);
 rid2 = itree_find(racktree,ip2);
 return (rid1==rid2)?1:2;
}

canonical_original_code:
unsigned char topology_distance(unsigned int ip1, unsigned int ip2) {
 unsigned int rid1; unsigned int rid2;
 if (ip1==ip2) {
  return 0;
 }
 rid1 = itree_find(racktree,ip1);
 rid2 = itree_find(racktree,ip2);
 return (rid1==rid2)?1:2;
}

return_type: unsigned char


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

v5 = *a1;
if ( *a1 == 35 )
return 0xFFFFFFFFLL;
endptr = a1;
if ( v5 == 32 || v5 == 9 )
{
v10 = a1 + 1;
do
{
do
{
a1 = v10;
endptr = v10;
v11 = *v10++;
}
while ( v11 == 32 );
}
while ( v11 == 9 );
}
v12 = endptr;
v13 = (unsigned __int8)*endptr;
if ( (v13 & 0xDF) != 0 && (_BYTE)v13 != 9 )
{
v14 = endptr + 1;
do
{
v12 = v14;
endptr = v14;
v13 = (unsigned __int8)*v14++;
}
while ( (v13 & 0xDF) != 0 && (_BYTE)v13 != 9 );
}
if ( (unsigned __int8)v13 <= 0xDu && (v15 = 9217LL, _bittest64(&v15, v13)) )
{
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
{
*v12 = 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
{
v16 = endptr;
v17 = *endptr;
if ( *endptr == 32 || v17 == 9 )
{
v18 = endptr + 1;
do
{
do
{
v16 = v18;
endptr = v18;
v17 = *v18++;
}
while ( v17 == 32 );
}
while ( v17 == 9 );
}
if ( (unsigned __int8)(v17 - 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(v16, &endptr, 10);
v19 = (unsigned __int8)*endptr;
if ( (_BYTE)v19 == 32 || (_BYTE)v19 == 9 )
{
v20 = endptr + 1;
do
{
do
{
endptr = v20;
v19 = (unsigned __int8)*v20++;
}
while ( (_BYTE)v19 == 32 );
}
while ( (_BYTE)v19 == 9 );
}
if ( (unsigned __int8)v19 <= 0x23u && (v21 = 0x800002401LL, _bittest64(&v21, v19)) )
{
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)
{
char v5; // al
char *v10; // rax
char v11; // dl
char *v12; // rcx
unsigned __int64 v13; // rdx
char *v14; // rax
__int64 v15; // rax
char *v16; // rdi
char v17; // dl
char *v18; // rax
unsigned __int64 v19; // rdx
char *v20; // rax
__int64 v21; // rax
__int64 result; // rax
char *endptr; // [rsp+0h] [rbp-20h] BYREF

v5 = *a1;
if ( *a1 == 35 )
return 0xFFFFFFFFLL;
endptr = a1;
if ( v5 == 32 || v5 == 9 )
{
v10 = a1 + 1;
do
{
do
{
a1 = v10;
endptr = v10;
v11 = *v10++;
}
while ( v11 == 32 );
}
while ( v11 == 9 );
}
v12 = endptr;
v13 = (unsigned __int8)*endptr;
if ( (v13 & 0xDF) != 0 && (_BYTE)v13 != 9 )
{
v14 = endptr + 1;
do
{
v12 = v14;
endptr = v14;
v13 = (unsigned __int8)*v14++;
}
while ( (v13 & 0xDF) != 0 && (_BYTE)v13 != 9 );
}
if ( (unsigned __int8)v13 <= 0xDu && (v15 = 9217LL, _bittest64(&v15, v13)) )
{
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
{
*v12 = 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
{
v16 = endptr;
v17 = *endptr;
if ( *endptr == 32 || v17 == 9 )
{
v18 = endptr + 1;
do
{
do
{
v16 = v18;
endptr = v18;
v17 = *v18++;
}
while ( v17 == 32 );
}
while ( v17 == 9 );
}
if ( (unsigned __int8)(v17 - 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(v16, &endptr, 10);
v19 = (unsigned __int8)*endptr;
if ( (_BYTE)v19 == 32 || (_BYTE)v19 == 9 )
{
v20 = endptr + 1;
do
{
do
{
endptr = v20;
v19 = (unsigned __int8)*v20++;
}
while ( (_BYTE)v19 == 32 );
}
while ( (_BYTE)v19 == 9 );
}
if ( (unsigned __int8)v19 <= 0x23u && (v21 = 0x800002401LL, _bittest64(&v21, v19)) )
{
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 *

return_type: int


decompiled_code:
int topology_reload()
{
char *v0; // rdi
int v1; // ebx

if ( TopologyFileName )
free(TopologyFileName);
if ( (unsigned int)cfg_isdefined("TOPOLOGY_FILENAME") )
{
TopologyFileName = (char *)cfg_getstr("TOPOLOGY_FILENAME", "/etc/mfs/mfstopology.cfg");
return topology_load();
}
v0 = strdup("/etc/mfs/mfstopology.cfg");
TopologyFileName = v0;
if ( !v0 )
{
fprintf(stderr, "out of memory: %s is NULL\n", "TopologyFileName");
syslog(3, "out of memory: %s is NULL", "TopologyFileName");
abort();
}
v1 = open64(v0, 0);
if ( v1 >= 0 )
goto LABEL_6;
if ( *_errno_location() == 2 )
{
free(TopologyFileName);
TopologyFileName = strdup("/etc/mfstopology.cfg");
v1 = open64(TopologyFileName, 0);
if ( v1 >= 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(v1);
return topology_load();
}
}
return topology_load();
}

canonical_decompiled_code:
int func5()
{
char *v0; // rdi
int v1; // ebx

if ( TopologyFileName )
free(TopologyFileName);
if ( (unsigned int)cfg_isdefined("TOPOLOGY_FILENAME") )
{
TopologyFileName = (char *)cfg_getstr("TOPOLOGY_FILENAME", "/etc/mfs/mfstopology.cfg");
return func4();
}
v0 = strdup("/etc/mfs/mfstopology.cfg");
TopologyFileName = v0;
if ( !v0 )
{
fprintf(stderr, "out of memory: %s is NULL\n", "TopologyFileName");
syslog(3, "out of memory: %s is NULL", "TopologyFileName");
abort();
}
v1 = open64(v0, 0);
if ( v1 >= 0 )
goto LABEL_6;
if ( *_errno_location() == 2 )
{
free(TopologyFileName);
TopologyFileName = strdup("/etc/mfstopology.cfg");
v1 = open64(TopologyFileName, 0);
if ( v1 >= 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(v1);
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;
}


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)
{
__int64 result; // rax

result = (*a1)[1] + (**a1 << 8);
*a1 += 2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 **a1)
{
__int64 result; // rax

result = (*a1)[1] + (**a1 << 8);
*a1 += 2;
return result;
}

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; // rdx
__int64 i; // rax
__int64 *v3; // rbp
__int64 v4; // rbx
unsigned __int8 v5; // al
char v6; // al

while ( 1 )
{
pthread_mutex_lock(&glock);
if ( rterm )
break;
v4 = rdhead;
v3 = &rdhead;
if ( rdhead )
{
do
{
v5 = *(_BYTE *)(v4 + 56);
if ( v5 <= 0xEu )
*(_BYTE *)(v4 + 56) = v5 + 1;
if ( *(_BYTE *)(v4 + 59) )
{
v3 = (__int64 *)(v4 + 112);
}
else if ( *(_BYTE *)(v4 + 58) )
{
if ( *(int *)(v4 + 52) >= 0 )
{
v6 = *(_BYTE *)(v4 + 57);
if ( v6 == 3 )
{
csdb_readdec(*(unsigned int *)(v4 + 44), *(unsigned __int16 *)(v4 + 48));
tcpclose(*(unsigned int *)(v4 + 52));
*(_DWORD *)(v4 + 52) = -1;
}
else
{
*(_BYTE *)(v4 + 57) = v6 + 1;
}
}
v3 = (__int64 *)(v4 + 112);
}
else
{
pthread_cond_destroy((pthread_cond_t *)(v4 + 64));
*v3 = *(_QWORD *)(v4 + 112);
v1 = &rdinodemap[*(_DWORD *)(v4 + 12) & 0x3FF];
for ( i = *v1; *v1; i = *v1 )
{
if ( i == v4 )
*v1 = *(_QWORD *)(v4 + 120);
else
v1 = (__int64 *)(i + 120);
}
free((void *)v4);
}
v4 = *v3;
}
while ( *v3 );
}
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:
__int64 __fastcall read_data_refresh_connection(__int64 a1)
{
unsigned __int8 v1; // al
unsigned __int8 v2; // bp
__int64 v3; // r8
__int64 result; // rax
char *v5; // rax
unsigned int v6; // er15
unsigned __int16 v7; // r13
unsigned int v8; // er12
unsigned int v9; // ebp
unsigned __int16 v10; // r14
unsigned int v11; // eax
unsigned int v12; // er15
unsigned int v13; // ebp
int *v14; // rax
const char *v15; // rax
int *v16; // rax
const char *v17; // rax
__int64 v18; // rcx
int v19; // eax
int *v20; // rax
const char *v21; // rax
int *v22; // rax
const char *v23; // rax
unsigned int v24; // [rsp+4h] [rbp-44h] BYREF
unsigned __int8 *v25[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;
}
v1 = fs_readchunk(*(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 24), a1 + 16, a1 + 32, a1 + 40, v25, &v24);
v2 = v1;
if ( v1 )
{
v5 = mfsstrerr(v1);
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),
v5);
return 4 * (unsigned int)(v2 == 3) + 5;
}
v3 = *(_QWORD *)(a1 + 32);
if ( v3 )
{
if ( !v25[0] )
goto LABEL_7;
goto LABEL_11;
}
if ( v25[0] )
{
LABEL_11:
if ( !v24 )
goto LABEL_7;
v6 = -1;
v7 = 0;
v8 = 0;
if ( v24 <= 5 )
goto LABEL_13;
do
{
v9 = get32bit(v25);
v10 = get16bit(v25);
v24 -= 6;
v11 = csdb_getopcnt(v9, v10);
if ( v11 < v6 )
{
v6 = v11;
v7 = v10;
v8 = v9;
}
}
while ( v24 > 5 && v6 );
if ( v8 && v7 )
{
*(_DWORD *)(a1 + 44) = v8;
*(_WORD *)(a1 + 48) = v7;
v12 = fs_getsrcip();
v13 = 0;
while ( 1 )
{
v19 = tcpsocket();
*(_DWORD *)(a1 + 52) = v19;
if ( v19 < 0 )
break;
if ( v12 && (int)tcpnumbind((unsigned int)v19, v12, 0LL) < 0 )
{
v16 = _errno_location();
v17 = (const char *)strerr((unsigned int)*v16);
syslog(4, "can't bind to given ip: %s", v17);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return 5LL;
}
if ( (v13 & 1) != 0 )
v18 = (unsigned int)(300 << (v13 >> 1));
else
v18 = (unsigned int)(200 << (v13 >> 1));
if ( (int)tcpnumtoconnect(*(unsigned int *)(a1 + 52), v8, v7, v18) >= 0 )
goto LABEL_22;
if ( ++v13 == 10 )
{
v20 = _errno_location();
v21 = (const char *)strerr((unsigned int)*v20);
syslog(4, "can't connect to (%08X:%u): %s", v8, v7, v21);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return 5LL;
}
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
v14 = _errno_location();
v15 = (const char *)strerr((unsigned int)*v14);
syslog(4, "can't create tcp socket: %s", v15);
LABEL_22:
if ( *(int *)(a1 + 52) < 0 )
{
result = 5LL;
}
else
{
if ( (int)tcpnodelay() < 0 )
{
v22 = _errno_location();
v23 = (const char *)strerr((unsigned int)*v22);
syslog(4, "can't set TCP_NODELAY: %s", v23);
}
csdb_readinc(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
pthread_mutex_lock(&glock);
*(_BYTE *)(a1 + 56) = 0;
pthread_mutex_unlock(&glock);
result = 0LL;
}
}
else
{
LABEL_13:
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - there are no valid copies",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40));
result = 6LL;
}
return result;
}
result = 0LL;
if ( v24 )
{
LABEL_7:
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - there are no valid copies",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
v3,
*(unsigned int *)(a1 + 40));
result = 6LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
unsigned __int8 v1; // al
unsigned __int8 v2; // bp
__int64 v3; // r8
__int64 result; // rax
char *v5; // rax
unsigned int v6; // er15
unsigned __int16 v7; // r13
unsigned int v8; // er12
unsigned int v9; // ebp
unsigned __int16 v10; // r14
unsigned int v11; // eax
unsigned int v12; // er15
unsigned int v13; // ebp
int *v14; // rax
const char *v15; // rax
int *v16; // rax
const char *v17; // rax
__int64 v18; // rcx
int v19; // eax
int *v20; // rax
const char *v21; // rax
int *v22; // rax
const char *v23; // rax
unsigned int v24; // [rsp+4h] [rbp-44h] BYREF
unsigned __int8 *v25[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;
}
v1 = fs_readchunk(*(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 24), a1 + 16, a1 + 32, a1 + 40, v25, &v24);
v2 = v1;
if ( v1 )
{
v5 = func0(v1);
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),
v5);
return 4 * (unsigned int)(v2 == 3) + 5;
}
v3 = *(_QWORD *)(a1 + 32);
if ( v3 )
{
if ( !v25[0] )
goto LABEL_7;
goto LABEL_11;
}
if ( v25[0] )
{
LABEL_11:
if ( !v24 )
goto LABEL_7;
v6 = -1;
v7 = 0;
v8 = 0;
if ( v24 <= 5 )
goto LABEL_13;
do
{
v9 = func1(v25);
v10 = func2(v25);
v24 -= 6;
v11 = csdb_getopcnt(v9, v10);
if ( v11 < v6 )
{
v6 = v11;
v7 = v10;
v8 = v9;
}
}
while ( v24 > 5 && v6 );
if ( v8 && v7 )
{
*(_DWORD *)(a1 + 44) = v8;
*(_WORD *)(a1 + 48) = v7;
v12 = fs_getsrcip();
v13 = 0;
while ( 1 )
{
v19 = tcpsocket();
*(_DWORD *)(a1 + 52) = v19;
if ( v19 < 0 )
break;
if ( v12 && (int)tcpnumbind((unsigned int)v19, v12, 0LL) < 0 )
{
v16 = _errno_location();
v17 = (const char *)strerr((unsigned int)*v16);
syslog(4, "can't bind to given ip: %s", v17);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return 5LL;
}
if ( (v13 & 1) != 0 )
v18 = (unsigned int)(300 << (v13 >> 1));
else
v18 = (unsigned int)(200 << (v13 >> 1));
if ( (int)tcpnumtoconnect(*(unsigned int *)(a1 + 52), v8, v7, v18) >= 0 )
goto LABEL_22;
if ( ++v13 == 10 )
{
v20 = _errno_location();
v21 = (const char *)strerr((unsigned int)*v20);
syslog(4, "can't connect to (%08X:%u): %s", v8, v7, v21);
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
return 5LL;
}
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
v14 = _errno_location();
v15 = (const char *)strerr((unsigned int)*v14);
syslog(4, "can't create tcp socket: %s", v15);
LABEL_22:
if ( *(int *)(a1 + 52) < 0 )
{
result = 5LL;
}
else
{
if ( (int)tcpnodelay() < 0 )
{
v22 = _errno_location();
v23 = (const char *)strerr((unsigned int)*v22);
syslog(4, "can't set TCP_NODELAY: %s", v23);
}
csdb_readinc(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
pthread_mutex_lock(&glock);
*(_BYTE *)(a1 + 56) = 0;
pthread_mutex_unlock(&glock);
result = 0LL;
}
}
else
{
LABEL_13:
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - there are no valid copies",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40));
result = 6LL;
}
return result;
}
result = 0LL;
if ( v24 )
{
LABEL_7:
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u - there are no valid copies",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
v3,
*(unsigned int *)(a1 + 40));
result = 6LL;
}
return result;
}

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)
{
pthread_mutex_lock(&glock);
++*(_WORD *)(a1 + 60);
while ( *(_BYTE *)(a1 + 59) )
pthread_cond_wait((pthread_cond_t *)(a1 + 64), &glock);
--*(_WORD *)(a1 + 60);
*(_BYTE *)(a1 + 59) = 1;
*(_BYTE *)(a1 + 58) = 0;
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 func6(__int64 a1)
{
pthread_mutex_lock(&glock);
++*(_WORD *)(a1 + 60);
while ( *(_BYTE *)(a1 + 59) )
pthread_cond_wait((pthread_cond_t *)(a1 + 64), &glock);
--*(_WORD *)(a1 + 60);
*(_BYTE *)(a1 + 59) = 1;
*(_BYTE *)(a1 + 58) = 0;
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)
{
const char *v1; // rax
pthread_attr_t v3; // [rsp+0h] [rbp-48h] BYREF

rterm = 0;
v1 = (const char *)rdinodemap;
do
{
*(_QWORD *)v1 = 0LL;
v1 += 8;
}
while ( v1 != "file: %u, index: %u, chunk: %lu, version: %u - fs_readchunk returns status: %s" );
maxretries = a1;
pthread_mutex_init(&glock, 0LL);
pthread_attr_init(&v3);
pthread_attr_setstacksize(&v3, 0x100000uLL);
pthread_create(&pthid, &v3, read_data_delayed_ops, 0LL);
return pthread_attr_destroy(&v3);
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
const char *v1; // rax
pthread_attr_t v3; // [rsp+0h] [rbp-48h] BYREF

rterm = 0;
v1 = (const char *)rdinodemap;
do
{
*(_QWORD *)v1 = 0LL;
v1 += 8;
}
while ( v1 != "file: %u, index: %u, chunk: %lu, version: %u - fs_readchunk returns status: %s" );
maxretries = a1;
pthread_mutex_init(&glock, 0LL);
pthread_attr_init(&v3);
pthread_attr_setstacksize(&v3, 0x100000uLL);
pthread_create(&pthid, &v3, read_data_delayed_ops, 0LL);
return pthread_attr_destroy(&v3);
}

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

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

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

pthread_mutex_lock(&glock);
rterm = 1;
pthread_mutex_unlock(&glock);
pthread_join(pthid, 0LL);
pthread_mutex_destroy(&glock);
v0 = (const char *)rdinodemap;
do
{
v2 = *(_QWORD *)v0;
while ( v2 )
{
v1 = v2;
v2 = *(_QWORD *)(v2 + 112);
v3 = *(unsigned int *)(v1 + 52);
if ( (int)v3 >= 0 )
tcpclose(v3);
if ( *(_QWORD *)v1 )
free(*(void **)v1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 64));
free((void *)v1);
}
*(_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 )
{
*(_BYTE *)(i + 57) = 3;
*(_BYTE *)(i + 56) = 15;
}
}
return pthread_mutex_unlock(&glock);
}

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

pthread_mutex_lock(&glock);
for ( i = rdinodemap[a1 & 0x3FF]; i; i = *(_QWORD *)(i + 120) )
{
if ( *(_DWORD *)(i + 12) == a1 )
{
*(_BYTE *)(i + 57) = 3;
*(_BYTE *)(i + 56) = 15;
}
}
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


decompiled_code:
__int64 __fastcall read_data(__int64 a1, unsigned __int64 a2, unsigned int *a3, char **a4)
{
unsigned int v5; // ebx
unsigned int v7; // er13
int v8; // er12
char *v9; // rdi
unsigned int v10; // eax
void *v11; // rax
unsigned __int8 v12; // r14
unsigned int v13; // edi
unsigned int v14; // edi
unsigned __int64 v15; // rax
unsigned __int64 v16; // rax
__int64 v17; // rcx
unsigned int v18; // eax
unsigned int v19; // er14
__int64 v20; // rsi
unsigned __int64 v21; // rax
unsigned __int64 v22; // [rsp+8h] [rbp-70h]
char *s; // [rsp+10h] [rbp-68h]
unsigned __int64 v25; // [rsp+20h] [rbp-58h]
__int64 v27; // [rsp+30h] [rbp-48h]
char v28; // [rsp+3Fh] [rbp-39h]

v25 = a2;
if ( !*a3 )
{
v5 = 0;
if ( *a4 )
return v5;
}
pthread_mutex_lock(&glock);
++*(_WORD *)(a1 + 60);
while ( *(_BYTE *)(a1 + 59) )
pthread_cond_wait((pthread_cond_t *)(a1 + 64), &glock);
--*(_WORD *)(a1 + 60);
*(_BYTE *)(a1 + 59) = 1;
if ( *(int *)(a1 + 52) >= 0 && *(_BYTE *)(a1 + 56) == 15 )
{
pthread_mutex_unlock(&glock);
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
else
{
pthread_mutex_unlock(&glock);
}
v5 = 0;
if ( !*a3 )
return v5;
s = *a4;
if ( *a4 )
{
v7 = *a3;
v28 = 1;
LABEL_12:
v22 = a2;
v5 = 5;
v8 = 0;
while ( 1 )
{
v15 = v22 >> 26;
if ( *(int *)(a1 + 52) < 0 || *(_DWORD *)(a1 + 24) != (_DWORD)v15 )
{
*(_DWORD *)(a1 + 24) = v15;
if ( (unsigned __int8)v8 >= (unsigned int)maxretries )
{
LABEL_23:
if ( v28 )
{
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
LABEL_26:
*(_BYTE *)(a1 + 59) = 0;
pthread_mutex_unlock(&glock);
}
return v5;
}
while ( 1 )
{
v12 = v8 + 1;
v5 = read_data_refresh_connection(a1);
if ( !v5 )
break;
syslog(
4,
"file: %u, index: %u - can't connect to proper chunkserver (try counter: %u)",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
(unsigned __int8)(v8 + 1));
if ( v5 == 9 )
{
if ( !v28 )
return v5;
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
goto LABEL_26;
}
if ( v5 == 6 )
{
sleep(0x3Cu);
v8 += 7;
}
else
{
v13 = 11;
if ( v12 <= 0x1Du )
v13 = (unsigned __int8)(v12 / 3u + 1);
sleep(v13);
++v8;
}
if ( (unsigned __int8)v8 >= (unsigned int)maxretries )
goto LABEL_23;
}
if ( v12 >= (unsigned int)maxretries )
goto LABEL_23;
++v8;
}
v16 = *(_QWORD *)(a1 + 16);
if ( v16 > v22 )
{
if ( v16 < v22 + v7 )
v7 = v16 - v22;
v17 = v22 & 0x3FFFFFF;
v18 = 0x4000000 - v17;
if ( v7 + (unsigned int)v17 <= 0x4000000 )
v18 = v7;
v19 = v18;
v20 = *(_QWORD *)(a1 + 32);
if ( v20 )
{
if ( (int)cs_readblock(*(unsigned int *)(a1 + 52), v20, *(unsigned int *)(a1 + 40), v17, v18, s) < 0 )
{
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u, cs: %08X:%u - readblock error (try counter: %u)",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40),
*(unsigned int *)(a1 + 44),
*(unsigned __int16 *)(a1 + 48),
(unsigned __int8)v8);
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
v14 = 11;
if ( (unsigned __int8)v8 <= 0x1Du )
v14 = (unsigned __int8)((unsigned __int8)v8 / 3u + 1);
sleep(v14);
}
else
{
v22 += v19;
v7 -= v19;
s += v19;
}
}
else
{
v27 = v18;
memset(s, 0, v18);
v22 += v27;
v7 -= v19;
s += v27;
}
if ( v7 )
continue;
}
goto LABEL_57;
}
}
if ( *a3 <= *(_DWORD *)(a1 + 8) )
goto LABEL_18;
v9 = *(char **)a1;
if ( *(_QWORD *)a1 )
free(v9);
v10 = *a3;
*(_DWORD *)(a1 + 8) = *a3;
v11 = malloc(v10);
*(_QWORD *)a1 = v11;
if ( !v11 )
{
*(_DWORD *)(a1 + 8) = 0;
syslog(4, "file: %u, index: %u - out of memory", *(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 24));
return 12;
}
s = *a4;
if ( !*a4 )
LABEL_18:
s = *(char **)a1;
v7 = *a3;
if ( *a3 )
{
v28 = 0;
goto LABEL_12;
}
v28 = 0;
LABEL_57:
v21 = *(_QWORD *)(a1 + 16);
if ( v21 > v25 )
{
if ( v21 >= v25 + *a3 )
{
if ( !*a4 )
*a4 = *(char **)a1;
}
else
{
if ( !*a4 )
*a4 = *(char **)a1;
*a3 = *(_DWORD *)(a1 + 16) - v25;
}
}
else
{
*a3 = 0;
}
pthread_mutex_lock(&glock);
*(_BYTE *)(a1 + 57) = 0;
if ( v28 )
{
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
*(_BYTE *)(a1 + 59) = 0;
}
pthread_mutex_unlock(&glock);
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, unsigned __int64 a2, unsigned int *a3, char **a4)
{
unsigned int v5; // ebx
unsigned int v7; // er13
int v8; // er12
char *v9; // rdi
unsigned int v10; // eax
void *v11; // rax
unsigned __int8 v12; // r14
unsigned int v13; // edi
unsigned int v14; // edi
unsigned __int64 v15; // rax
unsigned __int64 v16; // rax
__int64 v17; // rcx
unsigned int v18; // eax
unsigned int v19; // er14
__int64 v20; // rsi
unsigned __int64 v21; // rax
unsigned __int64 v22; // [rsp+8h] [rbp-70h]
char *s; // [rsp+10h] [rbp-68h]
unsigned __int64 v25; // [rsp+20h] [rbp-58h]
__int64 v27; // [rsp+30h] [rbp-48h]
char v28; // [rsp+3Fh] [rbp-39h]

v25 = a2;
if ( !*a3 )
{
v5 = 0;
if ( *a4 )
return v5;
}
pthread_mutex_lock(&glock);
++*(_WORD *)(a1 + 60);
while ( *(_BYTE *)(a1 + 59) )
pthread_cond_wait((pthread_cond_t *)(a1 + 64), &glock);
--*(_WORD *)(a1 + 60);
*(_BYTE *)(a1 + 59) = 1;
if ( *(int *)(a1 + 52) >= 0 && *(_BYTE *)(a1 + 56) == 15 )
{
pthread_mutex_unlock(&glock);
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
}
else
{
pthread_mutex_unlock(&glock);
}
v5 = 0;
if ( !*a3 )
return v5;
s = *a4;
if ( *a4 )
{
v7 = *a3;
v28 = 1;
LABEL_12:
v22 = a2;
v5 = 5;
v8 = 0;
while ( 1 )
{
v15 = v22 >> 26;
if ( *(int *)(a1 + 52) < 0 || *(_DWORD *)(a1 + 24) != (_DWORD)v15 )
{
*(_DWORD *)(a1 + 24) = v15;
if ( (unsigned __int8)v8 >= (unsigned int)maxretries )
{
LABEL_23:
if ( v28 )
{
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
LABEL_26:
*(_BYTE *)(a1 + 59) = 0;
pthread_mutex_unlock(&glock);
}
return v5;
}
while ( 1 )
{
v12 = v8 + 1;
v5 = func4(a1);
if ( !v5 )
break;
syslog(
4,
"file: %u, index: %u - can't connect to proper chunkserver (try counter: %u)",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
(unsigned __int8)(v8 + 1));
if ( v5 == 9 )
{
if ( !v28 )
return v5;
pthread_mutex_lock(&glock);
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
goto LABEL_26;
}
if ( v5 == 6 )
{
sleep(0x3Cu);
v8 += 7;
}
else
{
v13 = 11;
if ( v12 <= 0x1Du )
v13 = (unsigned __int8)(v12 / 3u + 1);
sleep(v13);
++v8;
}
if ( (unsigned __int8)v8 >= (unsigned int)maxretries )
goto LABEL_23;
}
if ( v12 >= (unsigned int)maxretries )
goto LABEL_23;
++v8;
}
v16 = *(_QWORD *)(a1 + 16);
if ( v16 > v22 )
{
if ( v16 < v22 + v7 )
v7 = v16 - v22;
v17 = v22 & 0x3FFFFFF;
v18 = 0x4000000 - v17;
if ( v7 + (unsigned int)v17 <= 0x4000000 )
v18 = v7;
v19 = v18;
v20 = *(_QWORD *)(a1 + 32);
if ( v20 )
{
if ( (int)cs_readblock(*(unsigned int *)(a1 + 52), v20, *(unsigned int *)(a1 + 40), v17, v18, s) < 0 )
{
syslog(
4,
"file: %u, index: %u, chunk: %lu, version: %u, cs: %08X:%u - readblock error (try counter: %u)",
*(unsigned int *)(a1 + 12),
*(unsigned int *)(a1 + 24),
*(_QWORD *)(a1 + 32),
*(unsigned int *)(a1 + 40),
*(unsigned int *)(a1 + 44),
*(unsigned __int16 *)(a1 + 48),
(unsigned __int8)v8);
csdb_readdec(*(unsigned int *)(a1 + 44), *(unsigned __int16 *)(a1 + 48));
tcpclose(*(unsigned int *)(a1 + 52));
*(_DWORD *)(a1 + 52) = -1;
v14 = 11;
if ( (unsigned __int8)v8 <= 0x1Du )
v14 = (unsigned __int8)((unsigned __int8)v8 / 3u + 1);
sleep(v14);
}
else
{
v22 += v19;
v7 -= v19;
s += v19;
}
}
else
{
v27 = v18;
memset(s, 0, v18);
v22 += v27;
v7 -= v19;
s += v27;
}
if ( v7 )
continue;
}
goto LABEL_57;
}
}
if ( *a3 <= *(_DWORD *)(a1 + 8) )
goto LABEL_18;
v9 = *(char **)a1;
if ( *(_QWORD *)a1 )
free(v9);
v10 = *a3;
*(_DWORD *)(a1 + 8) = *a3;
v11 = malloc(v10);
*(_QWORD *)a1 = v11;
if ( !v11 )
{
*(_DWORD *)(a1 + 8) = 0;
syslog(4, "file: %u, index: %u - out of memory", *(unsigned int *)(a1 + 12), *(unsigned int *)(a1 + 24));
return 12;
}
s = *a4;
if ( !*a4 )
LABEL_18:
s = *(char **)a1;
v7 = *a3;
if ( *a3 )
{
v28 = 0;
goto LABEL_12;
}
v28 = 0;
LABEL_57:
v21 = *(_QWORD *)(a1 + 16);
if ( v21 > v25 )
{
if ( v21 >= v25 + *a3 )
{
if ( !*a4 )
*a4 = *(char **)a1;
}
else
{
if ( !*a4 )
*a4 = *(char **)a1;
*a3 = *(_DWORD *)(a1 + 16) - v25;
}
}
else
{
*a3 = 0;
}
pthread_mutex_lock(&glock);
*(_BYTE *)(a1 + 57) = 0;
if ( v28 )
{
if ( *(_WORD *)(a1 + 60) )
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
*(_BYTE *)(a1 + 59) = 0;
}
pthread_mutex_unlock(&glock);
return 0;
}

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 func11(__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 v4; // ebp
__int64 i; // rbx

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

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, unsigned int a2, __int64 a3)
{
unsigned int v4; // ebp
__int64 i; // rbx

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

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; // ebp

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

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

v2 = firstnode;
if ( !firstnode )
return 0;
v3 = 0;
do
{
if ( a2 > v3 )
v3 += func1((char *)(a1 + v3), a2 - v3, (__int64)v2);
v2 = (_QWORD *)v2[6];
}
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);
}


decompiled_code:
void *__fastcall stats_get_subnode(__int64 a1, const char *a2, char a3)
{
void *v4; // r13
void *v5; // rbx
char *v6; // r14
unsigned int v7; // eax
unsigned int v8; // er12
unsigned int v9; // er15
unsigned int v10; // eax
char *v11; // r13
unsigned int v13; // [rsp+Ch] [rbp-3Ch]

pthread_mutex_lock(&glock);
v4 = firstnode;
if ( a1 )
v4 = *(void **)(a1 + 40);
if ( v4 )
{
v5 = v4;
while ( strcmp(*((const char **)v5 + 2), a2) )
{
v5 = (void *)*((_QWORD *)v5 + 6);
if ( !v5 )
goto LABEL_7;
}
}
else
{
LABEL_7:
v5 = malloc(0x38uLL);
*((_QWORD *)v5 + 6) = v4;
*((_QWORD *)v5 + 5) = 0LL;
*(_QWORD *)v5 = 0LL;
*((_BYTE *)v5 + 8) = 0;
*((_BYTE *)v5 + 9) = a3;
v6 = strdup(a2);
*((_QWORD *)v5 + 2) = v6;
v7 = strlen(a2);
v8 = v7;
*((_DWORD *)v5 + 8) = v7;
if ( a1 )
{
v9 = *(_DWORD *)(a1 + 36);
v10 = v9 + v7;
v13 = v10 + 1;
*((_DWORD *)v5 + 9) = v10 + 1;
v11 = (char *)malloc(v10 + 2);
memcpy(v11, *(const void **)(a1 + 24), v9);
v11[v9] = 46;
memcpy(&v11[v9 + 1], v6, v8);
v11[v13] = 0;
*((_QWORD *)v5 + 3) = v11;
*(_QWORD *)(a1 + 40) = v5;
}
else
{
*((_QWORD *)v5 + 3) = v6;
*((_DWORD *)v5 + 9) = v7;
firstnode = v5;
}
}
pthread_mutex_unlock(&glock);
return v5;
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, const char *a2, char a3)
{
void *v4; // r13
void *v5; // rbx
char *v6; // r14
unsigned int v7; // eax
unsigned int v8; // er12
unsigned int v9; // er15
unsigned int v10; // eax
char *v11; // r13
unsigned int v13; // [rsp+Ch] [rbp-3Ch]

pthread_mutex_lock(&glock);
v4 = firstnode;
if ( a1 )
v4 = *(void **)(a1 + 40);
if ( v4 )
{
v5 = v4;
while ( strcmp(*((const char **)v5 + 2), a2) )
{
v5 = (void *)*((_QWORD *)v5 + 6);
if ( !v5 )
goto LABEL_7;
}
}
else
{
LABEL_7:
v5 = malloc(0x38uLL);
*((_QWORD *)v5 + 6) = v4;
*((_QWORD *)v5 + 5) = 0LL;
*(_QWORD *)v5 = 0LL;
*((_BYTE *)v5 + 8) = 0;
*((_BYTE *)v5 + 9) = a3;
v6 = strdup(a2);
*((_QWORD *)v5 + 2) = v6;
v7 = strlen(a2);
v8 = v7;
*((_DWORD *)v5 + 8) = v7;
if ( a1 )
{
v9 = *(_DWORD *)(a1 + 36);
v10 = v9 + v7;
v13 = v10 + 1;
*((_DWORD *)v5 + 9) = v10 + 1;
v11 = (char *)malloc(v10 + 2);
memcpy(v11, *(const void **)(a1 + 24), v9);
v11[v9] = 46;
memcpy(&v11[v9 + 1], v6, v8);
v11[v13] = 0;
*((_QWORD *)v5 + 3) = v11;
*(_QWORD *)(a1 + 40) = v5;
}
else
{
*((_QWORD *)v5 + 3) = v6;
*((_DWORD *)v5 + 9) = v7;
firstnode = v5;
}
}
pthread_mutex_unlock(&glock);
return v5;
}

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)
{
pthread_mutex_lock(&glock);
if ( !*(_BYTE *)(a1 + 8) )
{
*(_BYTE *)(a1 + 8) = 1;
allactiveplengs += *(_DWORD *)(a1 + 36);
++activenodes;
}
pthread_mutex_unlock(&glock);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
pthread_mutex_lock(&glock);
if ( !*(_BYTE *)(a1 + 8) )
{
*(_BYTE *)(a1 + 8) = 1;
allactiveplengs += *(_DWORD *)(a1 + 36);
++activenodes;
}
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()
{
_QWORD *v0; // rbx

pthread_mutex_lock(&glock);
v0 = firstnode;
if ( firstnode )
{
do
{
stats_reset((__int64)v0);
v0 = (_QWORD *)v0[6];
}
while ( v0 );
}
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int func7()
{
_QWORD *v0; // rbx

pthread_mutex_lock(&glock);
v0 = firstnode;
if ( firstnode )
{
do
{
func0((__int64)v0);
v0 = (_QWORD *)v0[6];
}
while ( v0 );
}
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)
{
unsigned int v2; // er12
void *v3; // rax

pthread_mutex_lock(&glock);
v2 = 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)
{
unsigned int v2; // er12
void *v3; // rax

pthread_mutex_lock(&glock);
v2 = 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()
{
char v0; // cl
char v1; // si
__int64 v2; // rax
unsigned __int8 v3; // r8
unsigned int v4; // edx

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

canonical_decompiled_code:
__int64 func1()
{
char v0; // cl
char v1; // si
__int64 v2; // rax
unsigned __int8 v3; // r8
unsigned int v4; // edx

v0 = i;
v1 = p[(unsigned __int8)i];
j = p[(unsigned __int8)(j + v1)];
v2 = (unsigned __int8)j;
v3 = p[(unsigned __int8)j];
v4 = (unsigned __int8)p[(unsigned __int8)(p[v3] + 1)];
p[(unsigned __int8)i] = v3;
p[v2] = v1;
i = v0 + 1;
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


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

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

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

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

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 __fastcall rndu32_ranged(unsigned int a1)
{
unsigned int v1; // ecx

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

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
unsigned int v1; // ecx

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

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)
{
__int64 result; // rax

result = (*a1)[1] + (**a1 << 8);
*a1 += 2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 **a1)
{
__int64 result; // rax

result = (*a1)[1] + (**a1 << 8);
*a1 += 2;
return result;
}

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 *[]