main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // er13
__int64 v5; // rsi
int i; // er12
int v7; // ebp
int v8; // ebx
unsigned int v10; // [rsp+0h] [rbp-48h] BYREF
unsigned int v11; // [rsp+4h] [rbp-44h] BYREF
unsigned int v12; // [rsp+8h] [rbp-40h] BYREF
unsigned int v13; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v14; // [rsp+10h] [rbp-38h] BYREF
__int64 v15[6]; // [rsp+18h] [rbp-30h] BYREF

v15[0] = 0LL;
v14 = 0LL;
grid_init(&v14, v15, envp);
rand_init();
grid_fill(v14, v15[0]);
if ( GATE_COUNT <= 0 )
{
v4 = 0;
}
else
{
v3 = 0;
v4 = 0;
do
{
v5 = (unsigned int)v3++;
v4 += grid_routeLen(v15[0], v5);
}
while ( GATE_COUNT > v3 );
}
grid_dumpGrid(v14);
printf("Manhattan distance: %d\n", v4);
for ( i = 0; i <= 1000000; i += v7 == v8 )
{
while ( 1 )
{
rand_rdrand(&v13, 25LL);
rand_rdrand(&v12, 25LL);
rand_rdrand(&v11, 25LL);
rand_rdrand(&v10, 25LL);
v7 = grid_partialLen(v14, v15[0], v13, v11, v12, v10);
swap(v14, v13, v11, v12, v10);
v8 = grid_partialLen(v14, v15[0], v13, v11, v12, v10);
if ( v8 - v7 >= 0 )
break;
v4 += v8 - v7;
i = 0;
}
swap(v14, v13, v11, v12, v10);
}
grid_dumpGrid(v14);
printf("Manhattan Length: %d\n", v4);
grid_free(v14, v15[0]);
return 0;
}

original_code:
int main()
{
   gate_t* gates = ((void *)0);
   grid_t* grid = ((void *)0);
   grid_init(&grid,&gates);
   rand_init();
   grid_fill(grid,gates);
   int totalLen = 0;
   int count = 0;
   int x1,x2,y1,y2;
   int befLen, aftLen;
   int repeats = 0;
   while (count < GATE_COUNT) {
      totalLen += grid_routeLen(gates,count++);
   }
   grid_dumpGrid(grid);
   printf("Manhattan distance: %d\n",totalLen);
   while (1) {
      rand_rdrand(&x1,25);
      rand_rdrand(&x2,25);
      rand_rdrand(&y1,25);
      rand_rdrand(&y2,25);
      befLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      swap(grid, x1, y1, x2, y2);
      aftLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      if ( aftLen-befLen < 0 ) {
         repeats = 0;
         totalLen += aftLen-befLen;
      } else {
         swap(grid, x1, y1, x2, y2);
         if ( aftLen == befLen ) {
            repeats++;
         }
         if (repeats > 1000000) {
            break;
         }
      }
   }
   grid_dumpGrid(grid);
   printf("Manhattan Length: %d\n",totalLen);
   grid_free(grid,gates);
   return 0;
}

canonical_original_code:
int main()
{
   struct gate *gates = ((void *)0);
   struct gate **grid = ((void *)0);
   grid_init(&grid,&gates);
   rand_init();
   grid_fill(grid,gates);
   int totalLen = 0;
   int count = 0;
   int x1; int x2; int y1; int y2;
   int befLen; int aftLen;
   int repeats = 0;
   while (count < GATE_COUNT) {
      totalLen += grid_routeLen(gates,count++);
   }
   grid_dumpGrid(grid);
   printf("Manhattan distance: %d\n",totalLen);
   while (1) {
      rand_rdrand(&x1,25);
      rand_rdrand(&x2,25);
      rand_rdrand(&y1,25);
      rand_rdrand(&y2,25);
      befLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      swap(grid, x1, y1, x2, y2);
      aftLen = grid_partialLen(grid, gates, x1, y1, x2, y2);
      if ( aftLen-befLen < 0 ) {
         repeats = 0;
         totalLen += aftLen-befLen;
      } else {
         swap(grid, x1, y1, x2, y2);
         if ( aftLen == befLen ) {
            repeats++;
         }
         if (repeats > 1000000) {
            break;
         }
      }
   }
   grid_dumpGrid(grid);
   printf("Manhattan Length: %d\n",totalLen);
   grid_free(grid,gates);
   return 0;
}

variable_types: struct gate { int gateNum; int row; int col; int netToCount; int *netsTo; int netFromCount; int *netsFrom; } *

variable_types: totalLen int

variable_types: befLen int

variable_types: aftLen int

variable_types: repeats int


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

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

original_code:
int rand_init() {
   return 0;
}

canonical_original_code:
int rand_init() {
   return 0;
}

return_type: int


decompiled_code:
__int64 __fastcall rand_rdrand(unsigned int *a1, int a2)
{
char v2; // cf
unsigned int v3; // edx

v3 = 10;
do
{
__asm { rdrand rax }
if ( v2 )
break;
--v3;
}
while ( v3 );
*a1 = (unsigned int)(a2 * WORD1(_RAX)) >> 16;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, int a2)
{
char v2; // cf
unsigned int v3; // edx

v3 = 10;
do
{
__asm { rdrand rax }
if ( v2 )
break;
--v3;
}
while ( v3 );
*a1 = (unsigned int)(a2 * WORD1(_RAX)) >> 16;
return v3;
}

original_code:
inline int rand_rdrand(int *v, int range) {
   int ok;
    asm volatile("1: " ".byte 0x48,0x0f,0xc7,0xf0" "\n\t"
             "jc 2f\n\t"
             "decl %0\n\t"
             "jnz 1b\n\t"
             "2:"
             : "=r" (ok), "=a" (*v)
             : "0" (10));
   *v = (((unsigned int)*v>>16)*range)>>16;
   return ok;
}

variable_types:
v
int *

variable_types: range int

variable_types: ok int


decompiled_code:
void draw_triangle()
{
;
}

canonical_decompiled_code:
void func15()
{
;
}

original_code:
void
draw_triangle(float x0, float y0, float x1, float y1, float x2, float y2,
    byte r, byte g, byte b)
{
}

canonical_original_code:
void draw_triangle(float x0, float y0, float x1, float y1, float x2, float y2, unsigned char r, unsigned char g, unsigned char b)
{
}

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char

return_type: void


decompiled_code:
void draw_triangle_optimized()
{
;
}

canonical_decompiled_code:
void func16()
{
;
}

original_code:
void
draw_triangle_optimized(float x0, float y0, float x1, float y1, float x2, float y2,
    byte r, byte g, byte b)
{
}

canonical_original_code:
void draw_triangle_optimized(float x0, float y0, float x1, float y1, float x2, float y2, unsigned char r, unsigned char g, unsigned char b)
{
}


printDebugv

decompiled_code:
int __fastcall printDebugv(const char *a1, const char *a2, unsigned int a3)
{
return fprintf(stderr, "%s : line %d in %s\n", a1, a3, a2);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const char *a2, unsigned int a3)
{
return fprintf(stderr, "%s : line %d in %s\n", a1, a3, a2);
}

original_code:
void printDebugv( const char *msg, const char *file, const int line) {
 fprintf(stderr, "%s : line %d in %s\n", msg, line, file);
}

canonical_original_code:
void printDebugv(char *msg, char *file, int line) {
 fprintf(stderr, "%s : line %d in %s\n", msg, line, file);
}

variable_types:
msg
char *

variable_types:
file
char *

variable_types: line int


printDebug

decompiled_code:
int __fastcall printDebug(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

original_code:
void printDebug( const char *msg) {
 fprintf(stderr, "%s\n", msg);
}

canonical_original_code:
void printDebug(char *msg) {
 fprintf(stderr, "%s\n", msg);
}


escape

decompiled_code:
char *__fastcall escape(char *src)
{
int v1; // ebp
char *v2; // rdx
char *v3; // rdi
int v4; // eax
int v5; // ecx

v1 = strlen(src);
memset(empty, 0, 0x1388uLL);
strcpy(empty, src);
memset(buf, 0, sizeof(buf));
if ( v1 > 0 )
{
v2 = src;
v3 = &src[v1 - 1 + 1];
v4 = 0;
do
{
if ( *v2 == 34 )
{
buf[v4] = 38;
buf[v4 + 1] = 113;
buf[v4 + 2] = 117;
buf[v4 + 3] = 111;
v5 = v4 + 5;
buf[v4 + 4] = 116;
v4 += 6;
buf[v5] = 59;
}
else
{
buf[v4++] = *v2;
}
++v2;
}
while ( v2 != v3 );
}
return buf;
}

canonical_decompiled_code:
char *__fastcall func5(char *src)
{
int v1; // ebp
char *v2; // rdx
char *v3; // rdi
int v4; // eax
int v5; // ecx

v1 = strlen(src);
memset(empty, 0, 0x1388uLL);
strcpy(empty, src);
memset(buf, 0, sizeof(buf));
if ( v1 > 0 )
{
v2 = src;
v3 = &src[v1 - 1 + 1];
v4 = 0;
do
{
if ( *v2 == 34 )
{
buf[v4] = 38;
buf[v4 + 1] = 113;
buf[v4 + 2] = 117;
buf[v4 + 3] = 111;
v5 = v4 + 5;
buf[v4 + 4] = 116;
v4 += 6;
buf[v5] = 59;
}
else
{
buf[v4++] = *v2;
}
++v2;
}
while ( v2 != v3 );
}
return buf;
}

original_code:
char* escape(char *msg) {
 int len = strlen(msg);
 int i,b=0;
 memset ( empty, 0, 5000);
 strcpy( empty, msg );
 memset ( buf, 0, 5000);
 for(i=0; i<len; i++) {
  if (msg[i] == '"') {
   buf[b++] = '&';
   buf[b++] = 'q';
   buf[b++] = 'u';
   buf[b++] = 'o';
   buf[b++] = 't';
   buf[b++] = ';';
  }else{
   buf[b++] = msg[i];
  }
 }
 return buf;
}

canonical_original_code:
char *escape(char *msg) {
 int len = strlen(msg);
 int i; int b = 0;
 memset ( empty, 0, 5000);
 strcpy( empty, msg );
 memset ( buf, 0, 5000);
 for(i=0; i<len; i++) {
  if (msg[i] == '"') {
   buf[b++] = '&';
   buf[b++] = 'q';
   buf[b++] = 'u';
   buf[b++] = 'o';
   buf[b++] = 't';
   buf[b++] = ';';
  }else{
   buf[b++] = msg[i];
  }
 }
 return buf;
}

variable_types: len int

variable_types: i int

variable_types: b int

return_type: char *


suma

decompiled_code:
__int64 __fastcall suma(_DWORD *a1, int a2)
{
_DWORD *v2; // rax
unsigned int v3; // edx

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = 0;
do
v3 += *v2++;
while ( v2 != &a1[a2 - 1 + 1] );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, int a2)
{
_DWORD *v2; // rax
unsigned int v3; // edx

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = 0;
do
v3 += *v2++;
while ( v2 != &a1[a2 - 1 + 1] );
return v3;
}

original_code:
int suma(int tab[], int size)
{
  int result = 0;
  for (int i = 0; i < size; ++i)
    result += tab[i];
  return result;
}

variable_types:
tab
int []

variable_types: size int

variable_types: result int


decompiled_code:
double __fastcall srednia_arytmetyczna(_DWORD *a1, int a2)
{
return (double)(int)suma(a1, a2) / (double)a2;
}

canonical_decompiled_code:
double __fastcall func8(_DWORD *a1, int a2)
{
return (double)(int)func7(a1, a2) / (double)a2;
}

original_code:
double srednia_arytmetyczna(int tab[], int size)
{
  return suma(tab, size) / (double)size;
}

return_type: double


decompiled_code:
double __fastcall srednia_geometryczna(int *a1, int a2)
{
int *v2; // rax
double v3; // xmm0_8

if ( a2 <= 0 )
{
v3 = 1.0;
}
else
{
v2 = a1;
v3 = 1.0;
do
v3 = v3 * (double)*v2++;
while ( v2 != &a1[a2 - 1 + 1] );
}
return pow(v3, 1.0 / (double)a2);
}

canonical_decompiled_code:
double __fastcall func9(int *a1, int a2)
{
int *v2; // rax
double v3; // xmm0_8

if ( a2 <= 0 )
{
v3 = 1.0;
}
else
{
v2 = a1;
v3 = 1.0;
do
v3 = v3 * (double)*v2++;
while ( v2 != &a1[a2 - 1 + 1] );
}
return pow(v3, 1.0 / (double)a2);
}

original_code:
double srednia_geometryczna(int tab[], int size)
{
  double result = 1.0;
  for (int i = 0; i < size; ++i)
    result *= tab[i];
  return pow(result, 1.0/size);
}

variable_types: result double


decompiled_code:
__int64 __fastcall rownanie_kwadratowe(double *a1, double *a2, double a3, double a4, double a5)
{
double v5; // xmm1_8
__int64 result; // rax
double v7; // xmm5_8
double v8; // xmm0_8
double v11; // [rsp+18h] [rbp-20h]

v5 = a4 * a4 - a3 * 4.0 * a5;
if ( v5 > 0.0 )
{
v7 = -a4;
if ( v5 < 0.0 )
{
v11 = a3 + a3;
*a1 = (v7 - sqrt(v5)) / (a3 + a3);
v8 = sqrt(v5);
}
else
{
v8 = sqrt(v5);
v11 = a3 + a3;
*a1 = (v7 - v8) / v11;
}
*a2 = (v8 - a4) / v11;
result = 2LL;
}
else
{
result = 0LL;
if ( v5 >= 0.0 )
{
*a1 = -a4 / (a3 + a3);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(double *a1, double *a2, double a3, double a4, double a5)
{
double v5; // xmm1_8
__int64 result; // rax
double v7; // xmm5_8
double v8; // xmm0_8
double v11; // [rsp+18h] [rbp-20h]

v5 = a4 * a4 - a3 * 4.0 * a5;
if ( v5 > 0.0 )
{
v7 = -a4;
if ( v5 < 0.0 )
{
v11 = a3 + a3;
*a1 = (v7 - sqrt(v5)) / (a3 + a3);
v8 = sqrt(v5);
}
else
{
v8 = sqrt(v5);
v11 = a3 + a3;
*a1 = (v7 - v8) / v11;
}
*a2 = (v8 - a4) / v11;
result = 2LL;
}
else
{
result = 0LL;
if ( v5 >= 0.0 )
{
*a1 = -a4 / (a3 + a3);
result = 1LL;
}
}
return result;
}

original_code:
int rownanie_kwadratowe(double a, double b, double c, double *x1, double *x2)
{
  double delta = b*b - 4*a*c;
  if (delta > 0) {
    *x1 = (-b - sqrt(delta))/(2*a);
    *x2 = (-b + sqrt(delta))/(2*a);
    return 2;
  } else if (delta < 0) {
    return 0;
  } else {
    *x1 = -b/(2*a);
    return 1;
  }
}

variable_types: a double

variable_types: b double

variable_types: c double

variable_types: delta double


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // rbp
double v4; // r12
double v5; // xmm0_8
int v6; // er13
int result; // eax
double v8; // [rsp+0h] [rbp-38h] BYREF
double v9[6]; // [rsp+8h] [rbp-30h] BYREF

puts(s);
if ( argc <= 3 )
{
puts(aSpos);
result = -1;
}
else
{
v3 = atof(argv[1]);
v4 = atof(argv[2]);
v5 = atof(argv[3]);
if ( v3 == 0.0 )
{
puts(aToNieJestR);
result = -1;
}
else
{
v6 = rownanie_kwadratowe(v9, &v8, v3, v4, v5);
printf(format, v3, v4, v5);
if ( v6 == 2 )
{
printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", v9[0], v8);
result = 0;
}
else
{
if ( v6 == 1 )
printf(aR_0, v9[0]);
else
puts(aR);
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // rbp
double v4; // r12
double v5; // xmm0_8
int v6; // er13
int result; // eax
double v8; // [rsp+0h] [rbp-38h] BYREF
double v9[6]; // [rsp+8h] [rbp-30h] BYREF

puts(s);
if ( argc <= 3 )
{
puts(aSpos);
result = -1;
}
else
{
v3 = atof(argv[1]);
v4 = atof(argv[2]);
v5 = atof(argv[3]);
if ( v3 == 0.0 )
{
puts(aToNieJestR);
result = -1;
}
else
{
v6 = func7(v9, &v8, v3, v4, v5);
printf(format, v3, v4, v5);
if ( v6 == 2 )
{
printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", v9[0], v8);
result = 0;
}
else
{
if ( v6 == 1 )
printf(aR_0, v9[0]);
else
puts(aR);
result = 0;
}
}
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
  puts("Program obliczający pierwiastki równania kwadratowego.");
  if (argc < 4) {
    puts("Sposób użycia: cw08-03 a b c");
    return -1;
  }
  double a = atof(argv[1]);
  double b = atof(argv[2]);
  double c = atof(argv[3]);
  if (a == 0.0) {
    puts("To nie jest równanie kwadratowe!");
    return -1;
  }
  double x1, x2;
  int ilosc_rozwiazan;
  ilosc_rozwiazan = rownanie_kwadratowe(a, b, c, &x1, &x2);
  printf("Dla równania kwadratowego o współczynnikach:\n a = %.2f, b = %.2f i c = %.2f\n", a, b, c);
  if (ilosc_rozwiazan == 2) {
    printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", x1, x2);
  } else if (ilosc_rozwiazan == 1) {
    printf("Równanie ma jedno rozwiązanie:\n x = %.2f\n", x1);
  } else {
    puts("Równanie nie posiada rozwiązań");
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  puts("Program obliczający pierwiastki równania kwadratowego.");
  if (argc < 4) {
    puts("Sposób użycia: cw08-03 a b c");
    return -1;
  }
  double a = atof(argv[1]);
  double b = atof(argv[2]);
  double c = atof(argv[3]);
  if (a == 0.0) {
    puts("To nie jest równanie kwadratowe!");
    return -1;
  }
  double x1; double x2;
  int ilosc_rozwiazan;
  ilosc_rozwiazan = rownanie_kwadratowe(a, b, c, &x1, &x2);
  printf("Dla równania kwadratowego o współczynnikach:\n a = %.2f, b = %.2f i c = %.2f\n", a, b, c);
  if (ilosc_rozwiazan == 2) {
    printf("Obliczone pierwiastki to:\n x1 = %.2f, x2 = %.2f\n", x1, x2);
  } else if (ilosc_rozwiazan == 1) {
    printf("Równanie ma jedno rozwiązanie:\n x = %.2f\n", x1);
  } else {
    puts("Równanie nie posiada rozwiązań");
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


distance

decompiled_code:
double __fastcall distance(double a1, double a2, double a3, double a4)
{
double v4; // xmm2_8
double result; // xmm0_8

v4 = (a3 - a1) * (a3 - a1) + (a4 - a2) * (a4 - a2);
if ( v4 < 0.0 )
result = sqrt(v4);
else
result = sqrt(v4);
return result;
}

canonical_decompiled_code:
double __fastcall func7(double a1, double a2, double a3, double a4)
{
double v4; // xmm2_8
double result; // xmm0_8

v4 = (a3 - a1) * (a3 - a1) + (a4 - a2) * (a4 - a2);
if ( v4 < 0.0 )
result = sqrt(v4);
else
result = sqrt(v4);
return result;
}

original_code:
double distance(struct Point p1, struct Point p2)
{
  double dx = p2.x - p1.x;
  double dy = p2.y - p1.y;
  return sqrt(dx*dx + dy*dy);
}

variable_types: dx double

variable_types: dy double


podaj

decompiled_code:
double podaj()
{
double v1; // [rsp+10h] [rbp-10h]
char v2; // [rsp+1Fh] [rbp-1h]

do
{
v2 = 0;
std::operator<<<char>();
std::istream::operator>>();
if ( (unsigned __int8)std::ios::fail() )
{
v2 = 1;
std::ios::clear();
std::istream::ignore((std::istream *)&std::cin, 80LL, 10);
}
else if ( v1 <= 0.0 )
{
v2 = 1;
}
if ( v2 )
std::operator<<<std::char_traits<char>>();
}
while ( v2 );
return v1;
}

canonical_decompiled_code:
double func7()
{
double v1; // [rsp+10h] [rbp-10h]
char v2; // [rsp+1Fh] [rbp-1h]

do
{
v2 = 0;
std::operator<<<char>();
std::istream::operator>>();
if ( (unsigned __int8)std::ios::fail() )
{
v2 = 1;
std::ios::clear();
std::istream::ignore((std::istream *)&std::cin, 80LL, 10);
}
else if ( v1 <= 0.0 )
{
v2 = 1;
}
if ( v2 )
std::operator<<<std::char_traits<char>>();
}
while ( v2 );
return v1;
}

original_code:
double podaj(char *nazwa)
{
  int poprawnie_odczytane = 0;
  double wartosc;
  do {
    printf("Podaj %s: ", nazwa);
    poprawnie_odczytane = scanf("%lf", &wartosc);
    getchar();
  } while (poprawnie_odczytane != 1 || wartosc <= 0);
  return wartosc;
}

variable_types:
nazwa
char *

variable_types: wartosc double


decompiled_code:
__int64 pole_prostokata(void)
{
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
podaj();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
podaj();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
return std::ostream::operator<<();
}

canonical_decompiled_code:
__int64 func8(void)
{
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
func7();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
func7();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
return std::ostream::operator<<();
}

original_code:
void pole_prostokata()
{
  double a = podaj("a");
  double b = podaj("b");
  printf("Pole prostokąta wynosi: %f\n", a*b);
}


decompiled_code:
__int64 pole_kola(void)
{
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
podaj();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
return std::ostream::operator<<();
}

canonical_decompiled_code:
__int64 func9(void)
{
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
func7();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
return std::ostream::operator<<();
}

variable_types: r double


decompiled_code:
__int64 pole_trojkata(void)
{
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
podaj();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
podaj();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
return std::ostream::operator<<();
}

canonical_decompiled_code:
__int64 func10(void)
{
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
func7();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
func7();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
return std::ostream::operator<<();
}

original_code:
void pole_trojkata()
{
  double a = podaj("a");
  double h = podaj("h");
  printf("Pole trójkąta wynosi: %f\n", 0.5*a*h);
}

variable_types: h double


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char v3[43]; // [rsp+0h] [rbp-40h] BYREF
int v4; // [rsp+2Ch] [rbp-14h]

while ( 1 )
{
std::ostream::operator<<();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
v4 = (int)podaj(v3, &unk_40208C);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
if ( v4 == 4 )
{
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
exit(0);
}
if ( v4 > 4 )
{
LABEL_11:
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
}
else
{
switch ( v4 )
{
case 3:
pole_trojkata();
break;
case 1:
pole_prostokata();
break;
case 2:
pole_kola();
break;
default:
goto LABEL_11;
}
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char v3[43]; // [rsp+0h] [rbp-40h] BYREF
int v4; // [rsp+2Ch] [rbp-14h]

while ( 1 )
{
std::ostream::operator<<();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
std::allocator<char>::allocator();
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string();
v4 = (int)func7(v3, &unk_40208C);
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string();
std::allocator<char>::~allocator();
if ( v4 == 4 )
{
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
exit(0);
}
if ( v4 > 4 )
{
LABEL_11:
std::operator<<<std::char_traits<char>>();
std::ostream::operator<<();
}
else
{
switch ( v4 )
{
case 3:
func10();
break;
case 1:
func8();
break;
case 2:
func9();
break;
default:
goto LABEL_11;
}
}
}
}

original_code:
int main(void)
{
  for (;;) {
    char selection = '\n';
    puts("");
    puts("Siemanko. Co chcesz obliczyć?");
    puts("  1. Pole prostokąta");
    puts("  2. Pole koła");
    puts("  3. Pole trójkąta");
    puts("  4. Chcę wyjść z programu");
    printf("> ");
    while (selection == '\n')
      scanf("%c", &selection);
    switch (selection) {
    case '1':
      pole_prostokata();
      break;
    case '2':
      pole_kola();
      break;
    case '3':
      pole_trojkata();
      break;
    case '4':
      puts("Do zobaczyska!");
      exit(0);
    default:
      puts("Nie kumam. Wybierz jeszcze raz.");
      break;
    }
  }
  return 0;
}

canonical_original_code:
int main()
{
  for (;;) {
    char selection = '\n';
    puts("");
    puts("Siemanko. Co chcesz obliczyć?");
    puts("  1. Pole prostokąta");
    puts("  2. Pole koła");
    puts("  3. Pole trójkąta");
    puts("  4. Chcę wyjść z programu");
    printf("> ");
    while (selection == '\n')
      scanf("%c", &selection);
    switch (selection) {
    case '1':
      pole_prostokata();
      break;
    case '2':
      pole_kola();
      break;
    case '3':
      pole_trojkata();
      break;
    case '4':
      puts("Do zobaczyska!");
      exit(0);
    default:
      puts("Nie kumam. Wybierz jeszcze raz.");
      break;
    }
  }
  return 0;
}

variable_types: selection char


podaj

decompiled_code:
int __fastcall podaj(const char *a1)
{
int v1; // ebx
int result; // eax
double v3; // [rsp+8h] [rbp-20h]

do
{
do
{
printf("Podaj %s: ", a1);
v1 = __isoc99_scanf();
result = getchar();
}
while ( v1 != 1 );
}
while ( v3 <= 0.0 );
return result;
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
int v1; // ebx
int result; // eax
double v3; // [rsp+8h] [rbp-20h]

do
{
do
{
printf("Podaj %s: ", a1);
v1 = __isoc99_scanf();
result = getchar();
}
while ( v1 != 1 );
}
while ( v3 <= 0.0 );
return result;
}

canonical_original_code:
double podaj(char *nazwa)
{
  int poprawnie_odczytane = 0;
  double wartosc;
  do {
    printf("Podaj %s: ", nazwa);
    poprawnie_odczytane = scanf("%lf", &wartosc);
    getchar();
  } while (poprawnie_odczytane != 1 || wartosc <= 0);
  return wartosc;
}


decompiled_code:
int __fastcall pole_prostokata(double a1)
{
podaj("a");
podaj("b");
return printf(aPoleProstok, a1 * a1);
}

canonical_decompiled_code:
int __fastcall func8(double a1)
{
func7("a");
func7("b");
return printf(aPoleProstok, a1 * a1);
}

canonical_original_code:
void pole_prostokata()
{
  double a = podaj("a");
  double b = podaj("b");
  printf("Pole prostokąta wynosi: %f\n", a*b);
}


decompiled_code:
int __fastcall pole_kola(double a1)
{
podaj("r");
return printf(aPoleKo, a1 * 3.141592653589793 * a1);
}

canonical_decompiled_code:
int __fastcall func9(double a1)
{
func7("r");
return printf(aPoleKo, a1 * 3.141592653589793 * a1);
}


decompiled_code:
int __fastcall pole_trojkata(double a1)
{
podaj("a");
podaj("h");
return printf(aPoleTr, a1 * 0.5 * a1);
}

canonical_decompiled_code:
int __fastcall func10(double a1)
{
func7("a");
func7("h");
return printf(aPoleTr, a1 * 0.5 * a1);
}

canonical_original_code:
void pole_trojkata()
{
  double a = podaj("a");
  double h = podaj("h");
  printf("Pole trójkąta wynosi: %f\n", 0.5*a*h);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
puts(&byte_402068);
puts(aSiemankoCoChce);
puts(a1PoleProstok);
puts(a2PoleKo);
puts(a3PoleTr);
puts(a4Chc);
printf("> ");
while ( 1 )
__isoc99_scanf();
}


repo: kadavris/aps-toolkit

decompiled_code:
__int64 __fastcall ap_net_set_str_addr(int af, _WORD *a2, const char *a3, unsigned int a4, __int16 a5)
{
void *v6; // rdx
int v7; // edx
__int64 result; // rax

if ( af == 10 )
{
if ( a4 <= 0x1B )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address buffer length");
return 0LL;
}
*a2 = 10;
a2[1] = _bswap_16(a5);
v6 = a2 + 4;
}
else
{
if ( af != 2 )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address family: %d", (unsigned int)af);
return 0LL;
}
if ( a4 <= 0xF )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address buffer length");
return 0LL;
}
*a2 = 2;
a2[1] = _bswap_16(a5);
v6 = a2 + 2;
}
v7 = inet_pton(af, a3, v6);
result = 1LL;
if ( v7 == -1 )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address: %s", a3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int af, _WORD *a2, const char *a3, unsigned int a4, __int16 a5)
{
void *v6; // rdx
int v7; // edx
__int64 result; // rax

if ( af == 10 )
{
if ( a4 <= 0x1B )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address buffer length");
return 0LL;
}
*a2 = 10;
a2[1] = func0(a5);
v6 = a2 + 4;
}
else
{
if ( af != 2 )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address family: %d", (unsigned int)af);
return 0LL;
}
if ( a4 <= 0xF )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address buffer length");
return 0LL;
}
*a2 = 2;
a2[1] = func0(a5);
v6 = a2 + 2;
}
v7 = inet_pton(af, a3, v6);
result = 1LL;
if ( v7 == -1 )
{
ap_error_set_custom("ap_net_set_str_addr()", "bad address: %s", a3);
result = 0LL;
}
return result;
}

original_code:
int ap_net_set_str_addr(int af, void *destination, const char *address_str, socklen_t addr_len, int port)
{
 const char *_func_name = "ap_net_set_str_addr()";
    if ( af == 10 )
    {
     if ( addr_len < sizeof(struct sockaddr_in6))
     {
      ap_error_set_custom(_func_name, (char*)err_bad_buf_len);
            return 0;
     }
     ((struct sockaddr_in6 *)destination)->sin6_family = 10;
     ((struct sockaddr_in6 *)destination)->sin6_port = htons(port);
     destination = &(((struct sockaddr_in6 *)destination)->sin6_addr);
    }
    else if ( af == 2 )
    {
     if ( addr_len < sizeof(struct sockaddr_in))
     {
      ap_error_set_custom(_func_name, (char*)err_bad_buf_len);
            return 0;
     }
     ((struct sockaddr_in *)destination)->sin_family = 2;
     ((struct sockaddr_in *)destination)->sin_port = htons(port);
     destination = &(((struct sockaddr_in *)destination)->sin_addr);
    }
    else
    {
        ap_error_set_custom(_func_name, "bad address family: %d", af);
        return 0;
    }
    if ( -1 == inet_pton(af, address_str, destination) )
    {
         ap_error_set_custom(_func_name, "bad address: %s", address_str);
         return 0;
    }
    return 1;
}

canonical_original_code:
int ap_net_set_str_addr(int af, void *destination, char *address_str, unsigned int addr_len, int port)
{
 char *_func_name = "ap_net_set_str_addr()";
    if ( af == 10 )
    {
     if ( addr_len < sizeof(struct sockaddr_in6))
     {
      ap_error_set_custom(_func_name, (char *)err_bad_buf_len);
            return 0;
     }
     ((struct sockaddr_in6 *)destination)->sin6_family = 10;
     ((struct sockaddr_in6 *)destination)->sin6_port = htons(port);
     destination = &(((struct sockaddr_in6 *)destination)->sin6_addr);
    }
    else if ( af == 2 )
    {
     if ( addr_len < sizeof(struct sockaddr_in))
     {
      ap_error_set_custom(_func_name, (char *)err_bad_buf_len);
            return 0;
     }
     ((struct sockaddr_in *)destination)->sin_family = 2;
     ((struct sockaddr_in *)destination)->sin_port = htons(port);
     destination = &(((struct sockaddr_in *)destination)->sin_addr);
    }
    else
    {
        ap_error_set_custom(_func_name, "bad address family: %d", af);
        return 0;
    }
    if ( -1 == inet_pton(af, address_str, destination) )
    {
         ap_error_set_custom(_func_name, "bad address: %s", address_str);
         return 0;
    }
    return 1;
}

variable_types: af int

variable_types:
destination
void *

variable_types: port int

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

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

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }


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

if ( (unsigned int)(a3 - 1) > 0xFFFE )
{
ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", "bad port: %d", a3);
result = 0LL;
}
else
{
*(_WORD *)a1 = 2;
*(_WORD *)(a1 + 2) = _bswap_16(a3);
*(_DWORD *)(a1 + 4) = _bswap_32(a2);
result = 1LL;
}
return result;
}

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

if ( (unsigned int)(a3 - 1) > 0xFFFE )
{
ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", "bad port: %d", a3);
result = 0LL;
}
else
{
*(_WORD *)a1 = 2;
*(_WORD *)(a1 + 2) = func0(a3);
*(_DWORD *)(a1 + 4) = func1(a2);
result = 1LL;
}
return result;
}

original_code:
int ap_net_set_ip4_addr(struct sockaddr_in *destination, in_addr_t address4, int port)
{
 if ( port <= 0 || port > 65535 )
    {
         ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", (char*)err_bad_port, port);
         return 0;
    }
 destination->sin_family = 2;
 destination->sin_port = htons(port);
 destination->sin_addr.s_addr = htonl(address4);
    return 1;
}

canonical_original_code:
int ap_net_set_ip4_addr(struct sockaddr_in *destination, unsigned int address4, int port)
{
 if ( port <= 0 || port > 65535 )
    {
         ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", (char *)err_bad_port, port);
         return 0;
    }
 destination->sin_family = 2;
 destination->sin_port = htons(port);
 destination->sin_addr.s_addr = htonl(address4);
    return 1;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *


decompiled_code:
__int64 __fastcall ap_net_set_ip6_addr(__int64 a1, const __m128i *a2, __int64 a3)
{
__int64 result; // rax

if ( (unsigned int)(a3 - 1) > 0xFFFE )
{
ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", "bad port: %d", a3);
result = 0LL;
}
else
{
*(_WORD *)a1 = 10;
*(_WORD *)(a1 + 2) = _bswap_16(a3);
*(__m128i *)(a1 + 8) = _mm_loadu_si128(a2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const __m128i *a2, __int64 a3)
{
__int64 result; // rax

if ( (unsigned int)(a3 - 1) > 0xFFFE )
{
ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", "bad port: %d", a3);
result = 0LL;
}
else
{
*(_WORD *)a1 = 10;
*(_WORD *)(a1 + 2) = func0(a3);
*(__m128i *)(a1 + 8) = _mm_loadu_si128(a2);
result = 1LL;
}
return result;
}

original_code:
int ap_net_set_ip6_addr(struct sockaddr_in6 *destination, struct in6_addr *address6, int port)
{
 if ( port <= 0 || port > 65535 )
    {
         ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", (char*)err_bad_port, port);
         return 0;
    }
 destination->sin6_family = 10;
    destination->sin6_port = htons(port);
    memcpy(&destination->sin6_addr, address6, sizeof(struct in6_addr));
    return 1;
}

canonical_original_code:
int ap_net_set_ip6_addr(struct sockaddr_in6 *destination, struct in6_addr *address6, int port)
{
 if ( port <= 0 || port > 65535 )
    {
         ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", (char *)err_bad_port, port);
         return 0;
    }
 destination->sin6_family = 10;
    destination->sin6_port = htons(port);
    memcpy(&destination->sin6_addr, address6, sizeof(struct in6_addr));
    return 1;
}

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } *


decompiled_code:
__int64 __fastcall ap_net_conn_pool_set_str_addr(__int64 a1, const char *a2, __int16 a3)
{
_WORD *v4; // rsi
unsigned int v5; // ecx
int v6; // edi

v4 = (_WORD *)(a1 + 52);
if ( (*(_BYTE *)(a1 + 16) & 4) != 0 )
{
v5 = 28;
v6 = 10;
}
else
{
v5 = 16;
v6 = 2;
}
return ap_net_set_str_addr(v6, v4, a2, v5, a3);
}

original_code:
int ap_net_conn_pool_set_str_addr(struct ap_net_conn_pool_t *pool, const char *address_str, int port)
{
    int ip6;
    ip6 = (((pool->flags) & (4)) != 0);
    return ap_net_set_str_addr( (ip6 ? 10 : 2), (ip6 ? (void*)&pool->listener.addr6 : (void*)&pool->listener.addr4),
                                 address_str, (ip6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)), port);
}

canonical_original_code:
int ap_net_conn_pool_set_str_addr(struct ap_net_conn_pool_t *pool, char *address_str, int port)
{
    int ip6;
    ip6 = (((pool->flags) & (4)) != 0);
    return ap_net_set_str_addr( (ip6 ? 10 : 2), (ip6 ? (void *)&pool->listener.addr6 : (void *)&pool->listener.addr4),
                                 address_str, (ip6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)), port);
}

variable_types: struct ap_net_conn_pool_t { struct ap_net_connection_t { struct ap_net_conn_pool_t *parent; int idx; int fd; unsigned int flags; union { unsigned short af; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr4; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr6; } local; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; struct timespec { long tv_sec; long tv_nsec; } created_time; struct timespec expire; char *buf; int bufsize; unsigned int state; void *user_data; } *conns; int max_connections; int used_slots; unsigned int flags; unsigned int state; struct ap_net_poll_t { struct ap_net_conn_pool_t *parent; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; int max_events; int last_event_index; int events_count; int epoll_fd; int listen_socket_fd; int listen_socket_flags; int debug; int emit_old_data_signal; } *poller; struct timespec max_conn_ttl; struct { int sock; union { struct sockaddr_in addr4; struct sockaddr_in6 addr6; }; } listener; int (*callback_func)(struct ap_net_connection_t *conn, int signal_type); struct ap_net_stat_t { unsigned int conn_count; unsigned int timedout; unsigned int queue_full_count; unsigned int active_conn_count; struct timespec total_time; } stat; } *

user_defined_types: struct ap_net_conn_pool_t { struct ap_net_connection_t *conns; int max_connections; int used_slots; unsigned int flags; unsigned int state; struct ap_net_poll_t *poller; struct timespec max_conn_ttl; struct { int sock; union { struct sockaddr_in addr4; struct sockaddr_in6 addr6; }; } listener; int (*callback_func)(struct ap_net_connection_t *conn, int signal_type); struct ap_net_stat_t stat; }

user_defined_types: struct ap_net_connection_t { struct ap_net_conn_pool_t *parent; int idx; int fd; unsigned int flags; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } local; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; struct timespec created_time; struct timespec expire; char *buf; int bufsize; unsigned int state; void *user_data; }

user_defined_types: struct ap_net_poll_t { struct ap_net_conn_pool_t *parent; struct epoll_event *events; int max_events; int last_event_index; int events_count; int epoll_fd; int listen_socket_fd; int listen_socket_flags; int debug; int emit_old_data_signal; }

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }


decompiled_code:
__int64 __fastcall ap_net_conn_pool_set_ip4_addr(__int64 a1, unsigned int a2, __int64 a3)
{
if ( (*(_BYTE *)(a1 + 16) & 4) == 0 )
return ap_net_set_ip4_addr(a1 + 52, a2, a3);
ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", "Pool is set to IPv6 mode");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2, __int64 a3)
{
if ( (*(_BYTE *)(a1 + 16) & 4) == 0 )
return func3(a1 + 52, a2, a3);
ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", "Pool is set to IPv6 mode");
return 0LL;
}

original_code:
int ap_net_conn_pool_set_ip4_addr(struct ap_net_conn_pool_t *pool, in_addr_t address4, int port)
{
    if ( (((pool->flags) & (4)) != 0) )
    {
        ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", "Pool is set to IPv6 mode");
        return 0;
    }
    return ap_net_set_ip4_addr(&pool->listener.addr4, address4, port);
}

canonical_original_code:
int ap_net_conn_pool_set_ip4_addr(struct ap_net_conn_pool_t *pool, unsigned int address4, int port)
{
    if ( (((pool->flags) & (4)) != 0) )
    {
        ap_error_set_custom("ap_net_conn_pool_set_ip4_addr()", "Pool is set to IPv6 mode");
        return 0;
    }
    return ap_net_set_ip4_addr(&pool->listener.addr4, address4, port);
}

user_defined_types: struct ap_net_stat_t { unsigned int conn_count; unsigned int timedout; unsigned int queue_full_count; unsigned int active_conn_count; struct timespec total_time; }

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

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }


decompiled_code:
__int64 __fastcall ap_net_conn_pool_set_ip6_addr(__int64 a1, const __m128i *a2, __int64 a3)
{
if ( (*(_BYTE *)(a1 + 16) & 4) != 0 )
return ap_net_set_ip6_addr(a1 + 52, a2, a3);
ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", "Pool is set to IPv4 mode");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const __m128i *a2, __int64 a3)
{
if ( (*(_BYTE *)(a1 + 16) & 4) != 0 )
return func4(a1 + 52, a2, a3);
ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", "Pool is set to IPv4 mode");
return 0LL;
}

original_code:
int ap_net_conn_pool_set_ip6_addr(struct ap_net_conn_pool_t *pool, struct in6_addr *address6, int port)
{
    if ( ! (((pool->flags) & (4)) != 0) )
    {
        ap_error_set_custom("ap_net_conn_pool_set_ip6_addr()", "Pool is set to IPv4 mode");
        return 0;
    }
    return ap_net_set_ip6_addr(&pool->listener.addr6, address6, port);
}


decompiled_code:
__int64 __fastcall ap_str_makestr(void **a1, const char *a2)
{
void *v3; // rdi
__int64 result; // rax
size_t v5; // rax
char *v6; // rdi

v3 = *a1;
if ( v3 )
free(v3);
*a1 = 0LL;
result = 0LL;
if ( a2 )
{
v5 = strlen(a2);
v6 = (char *)malloc(v5 + 1);
*a1 = v6;
if ( v6 )
{
strcpy(v6, a2);
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void **a1, const char *a2)
{
void *v3; // rdi
__int64 result; // rax
size_t v5; // rax
char *v6; // rdi

v3 = *a1;
if ( v3 )
free(v3);
*a1 = 0LL;
result = 0LL;
if ( a2 )
{
v5 = strlen(a2);
v6 = (char *)malloc(v5 + 1);
*a1 = v6;
if ( v6 )
{
strcpy(v6, a2);
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int ap_str_makestr(char **d, const char *s)
{
    if ( *d != ((void *)0) )
        free(*d);
    *d = ((void *)0);
    if ( s == ((void *)0) )
        return 0;
    if ( ((void *)0) == (*d = (char*)malloc(strlen(s) + 1) ) )
        return 0;
    strcpy( (char*)(*d), s );
    return 1;
}

canonical_original_code:
int ap_str_makestr(char **d, char *s)
{
    if ( *d != ((void *)0) )
        free(*d);
    *d = ((void *)0);
    if ( s == ((void *)0) )
        return 0;
    if ( ((void *)0) == (*d = (char *)malloc(strlen(s) + 1) ) )
        return 0;
    strcpy( (char *)(*d), s );
    return 1;
}

variable_types:
d
char **

variable_types:
s
char *


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

result = malloc(a1);
if ( !result )
{
if ( a2 )
ap_log_do_syslog(3LL, a2);
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
if ( a2 )
ap_log_do_syslog(3LL, a2);
exit(1);
}
return result;
}

original_code:
void *ap_str_getmem(int size, char *errmsg)
{
    void *ptr;
    ptr = malloc(size);
    if ( ptr == ((void *)0) )
    {
        if (errmsg != ((void *)0))
            ap_log_do_syslog(3, errmsg);
        exit(1);
    }
    return ptr;
}

variable_types: size int

variable_types:
errmsg
char *

variable_types:
ptr
void *

return_type: void *


decompiled_code:
__int64 __fastcall ap_str_fix_buf_size(void **a1, _DWORD *a2, int *a3, int a4)
{
int v5; // edx
int v6; // eax
unsigned int v7; // esi
int v9; // ebp

v5 = *a3;
v6 = *a2 - v5;
v7 = 1;
if ( v6 < a4 )
{
v9 = v5 + a4;
if ( realloc(*a1, v5 + a4) )
{
*a2 = v9;
v7 = 1;
}
else
{
v7 = 0;
}
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(void **a1, _DWORD *a2, int *a3, int a4)
{
int v5; // edx
int v6; // eax
unsigned int v7; // esi
int v9; // ebp

v5 = *a3;
v6 = *a2 - v5;
v7 = 1;
if ( v6 < a4 )
{
v9 = v5 + a4;
if ( realloc(*a1, v5 + a4) )
{
*a2 = v9;
v7 = 1;
}
else
{
v7 = 0;
}
}
return v7;
}

original_code:
int ap_str_fix_buf_size(char **dst_buf, int *dst_buf_size, int *dst_buf_pos, int need_bytes)
{
    int n;
    if ( *dst_buf_size - *dst_buf_pos < need_bytes )
    {
        n = *dst_buf_size + need_bytes - (*dst_buf_size - *dst_buf_pos);
        if ( ((void *)0) == realloc(*dst_buf, n) )
            return 0;
        *dst_buf_size = n;
    }
    return 1;
}

variable_types: n int


decompiled_code:
__int64 __fastcall ap_str_put_to_buf(void **a1, _DWORD *a2, int *a3, const void *a4, int a5)
{
__int64 result; // rax

result = ap_str_fix_buf_size(a1, a2, a3, a5);
if ( (_DWORD)result )
{
memcpy((char *)*a1 + *a3, a4, a5);
*a3 += a5;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(void **a1, _DWORD *a2, int *a3, const void *a4, int a5)
{
__int64 result; // rax

result = func2(a1, a2, a3, a5);
if ( (_DWORD)result )
{
memcpy((char *)*a1 + *a3, a4, a5);
*a3 += a5;
result = 1LL;
}
return result;
}

original_code:
int ap_str_put_to_buf(char **dst_buf, int *dst_buf_size, int *dst_buf_fill, void *src_buf, int src_len)
{
    if ( ! ap_str_fix_buf_size(dst_buf, dst_buf_size, dst_buf_fill, src_len) )
        return 0;
    memcpy(*dst_buf + *dst_buf_fill, src_buf, src_len);
    *dst_buf_fill += src_len;
    return 1;
}


decompiled_code:
char **__fastcall ap_str_parse_init(char *src, const char *a2)
{
const char *v2; // rbp
void *v3; // rax
char **v4; // rbx
int v5; // eax
char *v6; // rax
char *v7; // rbp

v2 = a2;
v3 = malloc(0x28uLL);
v4 = (char **)v3;
if ( v3 )
{
*((_QWORD *)v3 + 4) = 0LL;
if ( !a2 )
v2 = (const char *)&unk_308;
if ( (unsigned int)ap_str_makestr((void **)v3 + 4, v2)
&& (v5 = strlen(src) + 1, *((_DWORD *)v4 + 4) = v5, v6 = (char *)malloc(v5), v7 = v6, (*v4 = v6) != 0LL) )
{
strcpy(v6, src);
v4[1] = v7;
v4[3] = strsep(v4 + 1, v4[4]);
}
else
{
free(v4);
v4 = 0LL;
}
}
return v4;
}

canonical_decompiled_code:
char **__fastcall func4(char *src, const char *a2)
{
const char *v2; // rbp
void *v3; // rax
char **v4; // rbx
int v5; // eax
char *v6; // rax
char *v7; // rbp

v2 = a2;
v3 = malloc(0x28uLL);
v4 = (char **)v3;
if ( v3 )
{
*((_QWORD *)v3 + 4) = 0LL;
if ( !a2 )
v2 = (const char *)&unk_308;
if ( (unsigned int)func0((void **)v3 + 4, v2)
&& (v5 = strlen(src) + 1, *((_DWORD *)v4 + 4) = v5, v6 = (char *)malloc(v5), v7 = v6, (*v4 = v6) != 0LL) )
{
strcpy(v6, src);
v4[1] = v7;
v4[3] = strsep(v4 + 1, v4[4]);
}
else
{
free(v4);
v4 = 0LL;
}
}
return v4;
}

original_code:
ap_str_parse_rec_t *ap_str_parse_init(char *in_str, char *user_separators)
{
    ap_str_parse_rec_t *r;
    if( ((void *)0) == (r = malloc(sizeof(ap_str_parse_rec_t))) )
        return ((void *)0);
    r->separators = ((void *)0);
    if ( ! ap_str_makestr(&r->separators, ( user_separators != ((void *)0) ) ? user_separators : " \t" ) )
    {
        free(r);
        return ((void *)0);
    }
    r->buf_len = strlen(in_str) + 1;
    if( ((void *)0) == (r->buf = malloc(r->buf_len)) )
    {
        free(r);
        return ((void *)0);
    }
    strcpy(r->buf, in_str);
    r->buf_pos = r->buf;
    r->curr = strsep(&r->buf_pos, r->separators);
    return r;
}

canonical_original_code:
struct ap_str_parse_rec *ap_str_parse_init(char *in_str, char *user_separators)
{
    struct ap_str_parse_rec *r;
    if( ((void *)0) == (r = malloc(sizeof(ap_str_parse_rec_t))) )
        return ((void *)0);
    r->separators = ((void *)0);
    if ( ! ap_str_makestr(&r->separators, ( user_separators != ((void *)0) ) ? user_separators : " \t" ) )
    {
        free(r);
        return ((void *)0);
    }
    r->buf_len = strlen(in_str) + 1;
    if( ((void *)0) == (r->buf = malloc(r->buf_len)) )
    {
        free(r);
        return ((void *)0);
    }
    strcpy(r->buf, in_str);
    r->buf_pos = r->buf;
    r->curr = strsep(&r->buf_pos, r->separators);
    return r;
}

variable_types: struct ap_str_parse_rec { char *buf; int buf_len; char *curr; char *separators; } *


decompiled_code:
void __fastcall ap_str_parse_end(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 4));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 4));
free(ptr);
}

original_code:
void ap_str_parse_end(ap_str_parse_rec_t *r)
{
    free(r->buf);
    free(r->separators);
    free(r);
}

canonical_original_code:
void ap_str_parse_end(struct ap_str_parse_rec *r)
{
    free(r->buf);
    free(r->separators);
    free(r);
}

user_defined_types: struct ap_str_parse_rec { char *buf; int buf_len; char *curr; char *separators; }


decompiled_code:
char *__fastcall ap_str_parse_next_arg(__int64 a1)
{
char *result; // rax

result = strsep((char **)(a1 + 8), *(const char **)(a1 + 32));
*(_QWORD *)(a1 + 24) = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1)
{
char *result; // rax

result = strsep((char **)(a1 + 8), *(const char **)(a1 + 32));
*(_QWORD *)(a1 + 24) = result;
return result;
}

original_code:
char *ap_str_parse_next_arg(ap_str_parse_rec_t *r)
{
    return (r->curr = strsep(&r->buf_pos, r->separators));
}

canonical_original_code:
char *ap_str_parse_next_arg(struct ap_str_parse_rec *r)
{
    return (r->curr = strsep(&r->buf_pos, r->separators));
}


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

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

original_code:
char *ap_str_parse_get_remaining(ap_str_parse_rec_t *r)
{
    return r->buf_pos;
}

canonical_original_code:
char *ap_str_parse_get_remaining(struct ap_str_parse_rec *r)
{
    return r->buf_pos;
}


decompiled_code:
char *__fastcall ap_str_parse_rollback(__int64 a1, int a2)
{
_BYTE *v3; // rax
_BYTE *v4; // rdx

if ( a2 <= 0 )
return *(char **)(a1 + 24);
do
{
v3 = *(_BYTE **)(a1 + 8);
if ( v3 == *(_BYTE **)a1 )
break;
if ( *v3 )
{
*(v3 - 1) = **(_BYTE **)(a1 + 32);
v3 = *(_BYTE **)(a1 + 8);
v4 = *(_BYTE **)a1;
if ( (unsigned __int64)v3 <= *(_QWORD *)a1 )
goto LABEL_6;
while ( *v3 )
{
*(_QWORD *)(a1 + 8) = --v3;
if ( v3 == v4 )
goto LABEL_6;
}
LABEL_5:
*(_QWORD *)(a1 + 8) = v3 + 1;
goto LABEL_6;
}
if ( (unsigned __int64)v3 > *(_QWORD *)a1 )
goto LABEL_5;
LABEL_6:
--a2;
}
while ( a2 );
return ap_str_parse_next_arg(a1);
}

original_code:
char *ap_str_parse_rollback(ap_str_parse_rec_t *r, int roll_count)
{
    if ( roll_count <= 0 )
        return r->curr;
    for(; roll_count; --roll_count)
    {
        if( r->buf_pos == r->buf )
            break;
        if ( *(r->buf_pos) )
            *(r->buf_pos - 1) = *(r->separators);
        for(; r->buf_pos > r->buf; r->buf_pos--)
        {
            if( *r->buf_pos == '\0' )
            {
                r->buf_pos++;
                break;
            }
        }
    }
    return ap_str_parse_next_arg(r);
}

canonical_original_code:
char *ap_str_parse_rollback(struct ap_str_parse_rec *r, int roll_count)
{
    if ( roll_count <= 0 )
        return r->curr;
    for(; roll_count; --roll_count)
    {
        if( r->buf_pos == r->buf )
            break;
        if ( *(r->buf_pos) )
            *(r->buf_pos - 1) = *(r->separators);
        for(; r->buf_pos > r->buf; r->buf_pos--)
        {
            if( *r->buf_pos == '\0' )
            {
                r->buf_pos++;
                break;
            }
        }
    }
    return ap_str_parse_next_arg(r);
}


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

result = *(char **)(a1 + 8);
do
{
if ( !a2 )
break;
result = ap_str_parse_next_arg(a1);
--a2;
}
while ( result );
return result;
}

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

result = *(char **)(a1 + 8);
do
{
if ( !a2 )
break;
result = func6(a1);
--a2;
}
while ( result );
return result;
}

original_code:
char *ap_str_parse_skip(ap_str_parse_rec_t *r, int skip_count)
{
    char *s;
    s = r->buf_pos;
    while( skip_count-- && ((void *)0) != (s = ap_str_parse_next_arg(r)) );
    return s;
}

canonical_original_code:
char *ap_str_parse_skip(struct ap_str_parse_rec *r, int skip_count)
{
    char *s;
    s = r->buf_pos;
    while( skip_count-- && ((void *)0) != (s = ap_str_parse_next_arg(r)) );
    return s;
}


decompiled_code:
__int64 __fastcall str_parse_set_separators(__int64 a1, const char *a2)
{
if ( !a2 )
a2 = (const char *)&unk_308;
return ap_str_makestr((void **)(a1 + 32), a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, const char *a2)
{
if ( !a2 )
a2 = (const char *)&unk_308;
return func0((void **)(a1 + 32), a2);
}

original_code:
int str_parse_set_separators(ap_str_parse_rec_t *r, char *separators)
{
    return ap_str_makestr(&(r->separators), ( separators != ((void *)0) ) ? separators : " \t" );
}

canonical_original_code:
int str_parse_set_separators(struct ap_str_parse_rec *r, char *separators)
{
    return ap_str_makestr(&(r->separators), ( separators != ((void *)0) ) ? separators : " \t" );
}

variable_types:
separators
char *


decompiled_code:
__int64 __fastcall ap_str_parse_get_bool(__int64 a1)
{
char *v1; // rax
const char *v2; // rbp
__int64 v3; // rbx

v1 = ap_str_parse_next_arg(a1);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
v3 = 0LL;
while ( strcasecmp((const char *)*(&keywords + 2 * v3), v2) )
{
if ( ++v3 == 8 )
return 0xFFFFFFFFLL;
}
return (unsigned int)dword_348[4 * (int)v3];
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
char *v1; // rax
const char *v2; // rbp
__int64 v3; // rbx

v1 = func6(a1);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
v3 = 0LL;
while ( strcasecmp((const char *)*(&keywords + 2 * v3), v2) )
{
if ( ++v3 == 8 )
return 0xFFFFFFFFLL;
}
return (unsigned int)dword_348[4 * (int)v3];
}

original_code:
int ap_str_parse_get_bool(ap_str_parse_rec_t *r)
{
    char *s;
    int i;
    s = ap_str_parse_next_arg(r);
    if ( s == ((void *)0) )
        return -1;
    for( i = 0; i < 8; ++i )
        if ( 0 == strcasecmp(keywords[i].s, s ) )
            return keywords[i].result;
    return -1;
}

canonical_original_code:
int ap_str_parse_get_bool(struct ap_str_parse_rec *r)
{
    char *s;
    int i;
    s = ap_str_parse_next_arg(r);
    if ( s == ((void *)0) )
        return -1;
    for( i = 0; i < 8; ++i )
        if ( 0 == strcasecmp(keywords[i].s, s ) )
            return keywords[i].result;
    return -1;
}

variable_types: i int


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

result = (unsigned int)*_errno_location();
ap_log_syserrno = result;
ap_log_err_errno = a2;
ap_log_err_function_name = a1;
ap_log_err_details = 0;
return result;
}

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

result = (unsigned int)*_errno_location();
ap_log_syserrno = result;
ap_log_err_errno = a2;
ap_log_err_function_name = a1;
ap_log_err_details = 0;
return result;
}

original_code:
void ap_error_set(const char *in_function_name, int in_errno)
{
    ap_log_syserrno = (*__errno_location ());
    ap_log_err_errno = in_errno;
    ap_log_err_function_name = (char *)in_function_name;
    *ap_log_err_details = '\0';
}

canonical_original_code:
void ap_error_set(char *in_function_name, int in_errno)
{
    ap_log_syserrno = (*__errno_location ());
    ap_log_err_errno = in_errno;
    ap_log_err_function_name = (char *)in_function_name;
    *ap_log_err_details = '\0';
}


decompiled_code:
void ap_error_clear()
{
ap_log_syserrno = 0;
ap_log_err_errno = 0;
ap_log_err_function_name = (__int64)"no name is set";
ap_log_err_details = 0;
}

canonical_decompiled_code:
void func1()
{
ap_log_syserrno = 0;
ap_log_err_errno = 0;
ap_log_err_function_name = (__int64)"no name is set";
ap_log_err_details = 0;
}

original_code:
void ap_error_clear(void)
{
    ap_log_err_errno = ap_log_syserrno = 0;
    ap_log_err_function_name = "no name is set";
    *ap_log_err_details = '\0';
}

canonical_original_code:
void ap_error_clear()
{
    ap_log_err_errno = ap_log_syserrno = 0;
    ap_log_err_function_name = "no name is set";
    *ap_log_err_details = '\0';
}


decompiled_code:
__int64 ap_error_get()
{
return (unsigned int)ap_log_err_errno;
}

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

original_code:
int ap_error_get(void)
{
    return ap_log_err_errno;
}

canonical_original_code:
int ap_error_get()
{
    return ap_log_err_errno;
}


decompiled_code:
char *ap_error_get_string()
{
int *v0; // rbx
int v1; // ebx
unsigned int v2; // ebp
char *v3; // rax
char *v4; // rax

if ( !ap_log_err_errno )
{
if ( !ap_log_syserrno && !*_errno_location() )
{
sprintf(buf_0, ap_log_errno_strings);
return buf_0;
}
v0 = _errno_location();
if ( *v0 )
{
v3 = strerror(*v0);
snprintf(
buf_0,
0x400uLL,
"There is no error recorded inside AP's_toolkit functions, but system error is: %d/'%s'. ",
(unsigned int)*v0,
v3);
return buf_0;
}
}
v1 = snprintf(buf_0, 0x400uLL, "ERROR in AP's_toolkit: in function: %s - ", (const char *)ap_log_err_function_name);
v2 = ap_log_syserrno;
if ( ap_log_syserrno )
{
v4 = strerror(ap_log_syserrno);
v1 += snprintf(&buf_0[v1], 1024 - v1, "system error: %d/'%s'. ", v2, v4);
if ( ap_log_err_details )
goto LABEL_7;
if ( ap_log_syserrno )
return buf_0;
LABEL_13:
snprintf(
&buf_0[v1],
1024 - v1,
"%d/'%s'. %s",
(unsigned int)ap_log_err_errno,
(&ap_log_errno_strings)[ap_log_err_errno],
&ap_log_err_details);
return buf_0;
}
if ( !ap_log_err_details )
goto LABEL_13;
LABEL_7:
strncat(&buf_0[v1], &ap_log_err_details, 1024 - v1);
return buf_0;
}

canonical_decompiled_code:
char *func3()
{
int *v0; // rbx
int v1; // ebx
unsigned int v2; // ebp
char *v3; // rax
char *v4; // rax

if ( !ap_log_err_errno )
{
if ( !ap_log_syserrno && !*_errno_location() )
{
sprintf(buf_0, ap_log_errno_strings);
return buf_0;
}
v0 = _errno_location();
if ( *v0 )
{
v3 = strerror(*v0);
snprintf(
buf_0,
0x400uLL,
"There is no error recorded inside AP's_toolkit functions, but system error is: %d/'%s'. ",
(unsigned int)*v0,
v3);
return buf_0;
}
}
v1 = snprintf(buf_0, 0x400uLL, "ERROR in AP's_toolkit: in function: %s - ", (const char *)ap_log_err_function_name);
v2 = ap_log_syserrno;
if ( ap_log_syserrno )
{
v4 = strerror(ap_log_syserrno);
v1 += snprintf(&buf_0[v1], 1024 - v1, "system error: %d/'%s'. ", v2, v4);
if ( ap_log_err_details )
goto LABEL_7;
if ( ap_log_syserrno )
return buf_0;
LABEL_13:
snprintf(
&buf_0[v1],
1024 - v1,
"%d/'%s'. %s",
(unsigned int)ap_log_err_errno,
(&ap_log_errno_strings)[ap_log_err_errno],
&ap_log_err_details);
return buf_0;
}
if ( !ap_log_err_details )
goto LABEL_13;
LABEL_7:
strncat(&buf_0[v1], &ap_log_err_details, 1024 - v1);
return buf_0;
}

original_code:
const char *ap_error_get_string(void)
{
    static char buf[1024 + 1];
    int bufpos;
    if ( ap_log_err_errno == 0 && ap_log_syserrno == 0 && (*__errno_location ()) == 0 )
    {
     sprintf(buf, ap_log_errno_strings[ap_log_err_errno]);
     return buf;
    }
    if ( ap_log_err_errno == 0 && (*__errno_location ()) != 0 )
    {
        snprintf(buf, 1024, "There is no error recorded inside AP's_toolkit functions, but system error is: %d/'%s'. ", (*__errno_location ()), strerror((*__errno_location ())));
        return (const char *)buf;
    }
    bufpos = 0;
    bufpos += snprintf(buf + bufpos, 1024 - bufpos, "ERROR in AP's_toolkit: in function: %s - ", ap_log_err_function_name);
    if ( ap_log_syserrno )
        bufpos += snprintf(buf + bufpos, 1024 - bufpos, "system error: %d/'%s'. ", ap_log_syserrno, strerror(ap_log_syserrno));
 if ( *ap_log_err_details )
     strncat(buf + bufpos, ap_log_err_details, 1024 - bufpos);
    else if ( ! ap_log_syserrno )
     bufpos += snprintf(buf + bufpos, 1024 - bufpos, "%d/'%s'. %s", ap_log_err_errno, ap_log_errno_strings[ap_log_err_errno], ap_log_err_details);
    return (const char *)buf;
}

canonical_original_code:
char *ap_error_get_string()
{
    static char buf[-1];
    int bufpos;
    if ( ap_log_err_errno == 0 && ap_log_syserrno == 0 && (*__errno_location ()) == 0 )
    {
     sprintf(buf, ap_log_errno_strings[ap_log_err_errno]);
     return buf;
    }
    if ( ap_log_err_errno == 0 && (*__errno_location ()) != 0 )
    {
        snprintf(buf, 1024, "There is no error recorded inside AP's_toolkit functions, but system error is: %d/'%s'. ", (*__errno_location ()), strerror((*__errno_location ())));
        return (char *)buf;
    }
    bufpos = 0;
    bufpos += snprintf(buf + bufpos, 1024 - bufpos, "ERROR in AP's_toolkit: in function: %s - ", ap_log_err_function_name);
    if ( ap_log_syserrno )
        bufpos += snprintf(buf + bufpos, 1024 - bufpos, "system error: %d/'%s'. ", ap_log_syserrno, strerror(ap_log_syserrno));
 if ( *ap_log_err_details )
     strncat(buf + bufpos, ap_log_err_details, 1024 - bufpos);
    else if ( ! ap_log_syserrno )
     bufpos += snprintf(buf + bufpos, 1024 - bufpos, "%d/'%s'. %s", ap_log_err_errno, ap_log_errno_strings[ap_log_err_errno], ap_log_err_details);
    return (char *)buf;
}

variable_types:
buf
char [-1]

variable_types: bufpos int


decompiled_code:
__int64 ap_error_set_detailed(__int64 a1, int a2, const char *a3, ...)
{
__int64 result; // rax
char *v5; // rbx
size_t v6; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

result = ap_log_get_lock();
if ( (_DWORD)result )
{
ap_log_syserrno = *_errno_location();
ap_log_err_errno = a2;
ap_log_err_function_name = a1;
va_start(arg, a3);
vsnprintf(&ap_log_err_details, 0x400uLL, a3, arg);
if ( ap_log_debug_level )
{
v5 = ap_error_get_string();
v6 = strlen(v5);
ap_log_debug_log_raw(v5, v6);
}
result = ap_log_release_lock();
}
return result;
}

canonical_decompiled_code:
__int64 func4(__int64 a1, int a2, const char *a3, ...)
{
__int64 result; // rax
char *v5; // rbx
size_t v6; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

result = ap_log_get_lock();
if ( (_DWORD)result )
{
ap_log_syserrno = *_errno_location();
ap_log_err_errno = a2;
ap_log_err_function_name = a1;
va_start(arg, a3);
vsnprintf(&ap_log_err_details, 0x400uLL, a3, arg);
if ( ap_log_debug_level )
{
v5 = func3();
v6 = strlen(v5);
ap_log_debug_log_raw(v5, v6);
}
result = ap_log_release_lock();
}
return result;
}

original_code:
void ap_error_set_detailed(const char *in_function_name, int in_errno, char *fmt, ...)
{
    va_list vl;
    char *debug_msg;
    if ( ! ap_log_get_lock() )
        return;
    ap_log_syserrno = (*__errno_location ());
    ap_log_err_errno = in_errno;
    ap_log_err_function_name = (char *)in_function_name;
    __builtin_va_start(vl,fmt);
    vsnprintf(ap_log_err_details, 1024, fmt, vl);
    __builtin_va_end(vl);
    if (ap_log_debug_level)
    {
        debug_msg = (char *)ap_error_get_string();
        ap_log_debug_log_raw(debug_msg, strlen(debug_msg));
    }
    ap_log_release_lock();
}

canonical_original_code:
void ap_error_set_detailed(char *in_function_name, int in_errno, char *fmt, ...)
{
    __builtin_va_list vl;
    char *debug_msg;
    if ( ! ap_log_get_lock() )
        return;
    ap_log_syserrno = (*__errno_location ());
    ap_log_err_errno = in_errno;
    ap_log_err_function_name = (char *)in_function_name;
    __builtin_va_start(vl,fmt);
    vsnprintf(ap_log_err_details, 1024, fmt, vl);
    __builtin_va_end(vl);
    if (ap_log_debug_level)
    {
        debug_msg = (char *)ap_error_get_string();
        ap_log_debug_log_raw(debug_msg, strlen(debug_msg));
    }
    ap_log_release_lock();
}

variable_types:
fmt
char *


decompiled_code:
__int64 ap_error_set_custom(__int64 a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return ap_error_set_detailed(a1, 2, a2, va);
}

canonical_decompiled_code:
__int64 func5(__int64 a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return func4(a1, 2, a2, va);
}

original_code:
void ap_error_set_custom(const char *in_function_name, char *fmt, ...)
{
    va_list vl;
    __builtin_va_start(vl,fmt);
    ap_error_set_detailed(in_function_name, 2, fmt, vl);
    __builtin_va_end(vl);
}

canonical_original_code:
void ap_error_set_custom(char *in_function_name, char *fmt, ...)
{
    __builtin_va_list vl;
    __builtin_va_start(vl,fmt);
    ap_error_set_detailed(in_function_name, 2, fmt, vl);
    __builtin_va_end(vl);
}


lock

original_code:
static int lock(unsigned int *state)
{
    int lock_try;
    for ( lock_try = 1; *state & 4; ++lock_try )
    {
        if ( lock_try == 10000 / 50 )
            return 0;
        usleep(50 * 1000);
    }
    *state |= 4;
    return 1;
}

variable_types:
state
unsigned int *


decompiled_code:
__int64 __fastcall ap_net_connection_lock(__int64 a1)
{
return lock((int *)(a1 + 132));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return func0((int *)(a1 + 132));
}

original_code:
int ap_net_connection_lock(struct ap_net_connection_t *conn)
{
 return lock(&conn->state);
}

variable_types: struct ap_net_connection_t { struct ap_net_conn_pool_t { struct ap_net_connection_t *conns; int max_connections; int used_slots; unsigned int flags; unsigned int state; struct ap_net_poll_t { struct ap_net_conn_pool_t *parent; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; int max_events; int last_event_index; int events_count; int epoll_fd; int listen_socket_fd; int listen_socket_flags; int debug; int emit_old_data_signal; } *poller; struct timespec { long tv_sec; long tv_nsec; } max_conn_ttl; struct { int sock; union { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr4; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr6; }; } listener; int (*callback_func)(struct ap_net_connection_t *conn, int signal_type); struct ap_net_stat_t { unsigned int conn_count; unsigned int timedout; unsigned int queue_full_count; unsigned int active_conn_count; struct timespec total_time; } stat; } *parent; int idx; int fd; unsigned int flags; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } local; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; struct timespec created_time; struct timespec expire; char *buf; int bufsize; unsigned int state; void *user_data; } *


decompiled_code:
void __fastcall ap_net_connection_unlock(__int64 a1)
{
*(_DWORD *)(a1 + 132) &= 0xFFFFFFFB;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
*(_DWORD *)(a1 + 132) &= 0xFFFFFFFB;
}

original_code:
void ap_net_connection_unlock(struct ap_net_connection_t *conn)
{
    ((conn->state) &= ~(4));
}


decompiled_code:
__int64 __fastcall ap_net_conn_pool_lock(__int64 a1)
{
return lock((int *)(a1 + 20));
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return func0((int *)(a1 + 20));
}

original_code:
int ap_net_conn_pool_lock(struct ap_net_conn_pool_t *pool)
{
 return lock(&pool->state);
}


decompiled_code:
void __fastcall ap_net_conn_pool_unlock(__int64 a1)
{
*(_DWORD *)(a1 + 20) &= 0xFFFFFFFB;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
*(_DWORD *)(a1 + 20) &= 0xFFFFFFFB;
}

original_code:
void ap_net_conn_pool_unlock(struct ap_net_conn_pool_t *pool)
{
    ((pool->state) &= ~(4));
}


decompiled_code:
__int64 __fastcall ap_net_conn_pool_get_conn_by_fd(__int64 *a1, int a2)
{
int v2; // edx
__int64 v3; // rax
__int64 v4; // rcx
__int64 v5; // rdx

v2 = *((_DWORD *)a1 + 2);
if ( v2 <= 0 )
return 0LL;
v3 = *a1;
v4 = *a1 + 144LL * (unsigned int)(v2 - 1) + 144;
while ( 1 )
{
v5 = v3;
if ( *(_DWORD *)(v3 + 12) == a2 )
break;
v3 += 144LL;
if ( v3 == v4 )
return 0LL;
}
return v5;
}

original_code:
struct ap_net_connection_t *ap_net_conn_pool_get_conn_by_fd(struct ap_net_conn_pool_t *pool, int fd)
{
    int i;
    for ( i = 0; i < pool->max_connections; ++i)
        if ( pool->conns[i].fd == fd )
            return &pool->conns[i];
    return ((void *)0);
}

variable_types:
pool
struct ap_net_conn_pool_t *

variable_types: fd int

return_type: struct ap_net_connection_t { struct ap_net_conn_pool_t { struct ap_net_connection_t *conns; int max_connections; int used_slots; unsigned int flags; unsigned int state; struct ap_net_poll_t { struct ap_net_conn_pool_t *parent; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; int max_events; int last_event_index; int events_count; int epoll_fd; int listen_socket_fd; int listen_socket_flags; int debug; int emit_old_data_signal; } *poller; struct timespec { long tv_sec; long tv_nsec; } max_conn_ttl; struct { int sock; union { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr4; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr6; }; } listener; int (*callback_func)(struct ap_net_connection_t *conn, int signal_type); struct ap_net_stat_t { unsigned int conn_count; unsigned int timedout; unsigned int queue_full_count; unsigned int active_conn_count; struct timespec total_time; } stat; } *parent; int idx; int fd; unsigned int flags; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } local; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; struct timespec created_time; struct timespec expire; char *buf; int bufsize; unsigned int state; void *user_data; } *


decompiled_code:
__int64 __fastcall ap_net_conn_pool_get_conn_by_port(__int64 a1, unsigned __int16 a2, int a3)
{
int v5; // er10
__int64 v6; // rdx
int v7; // eax
int v8; // edi
int v9; // esi

v5 = *(_DWORD *)(a1 + 8);
if ( v5 <= 0 )
return 0LL;
v6 = 0LL;
v7 = 0;
v8 = a2;
do
{
if ( a3 )
v9 = *(unsigned __int16 *)(*(_QWORD *)a1 + v6 + 22);
else
v9 = *(unsigned __int16 *)(*(_QWORD *)a1 + v6 + 50);
if ( v9 == v8 )
return *(_QWORD *)a1 + 144LL * v7;
++v7;
v6 += 144LL;
}
while ( v7 != v5 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned __int16 a2, int a3)
{
int v5; // er10
__int64 v6; // rdx
int v7; // eax
int v8; // edi
int v9; // esi

v5 = *(_DWORD *)(a1 + 8);
if ( v5 <= 0 )
return 0LL;
v6 = 0LL;
v7 = 0;
v8 = a2;
do
{
if ( a3 )
v9 = *(unsigned __int16 *)(*(_QWORD *)a1 + v6 + 22);
else
v9 = *(unsigned __int16 *)(*(_QWORD *)a1 + v6 + 50);
if ( v9 == v8 )
return *(_QWORD *)a1 + 144LL * v7;
++v7;
v6 += 144LL;
}
while ( v7 != v5 );
return 0LL;
}

original_code:
struct ap_net_connection_t *ap_net_conn_pool_get_conn_by_port(struct ap_net_conn_pool_t *pool, in_port_t port, int is_local)
{
    int i;
    int ip6;
    for ( i = 0; i < pool->max_connections; ++i)
    {
        ip6 = (is_local ? pool->conns[i].local.af : pool->conns[i].remote.af) == 10;
        if ( port == (is_local ? (ip6 ? pool->conns[i].local.addr6.sin6_port : pool->conns[i].local.addr4.sin_port)
                : (ip6 ? pool->conns[i].remote.addr6.sin6_port : pool->conns[i].remote.addr4.sin_port)) )
            return &pool->conns[i];
    }
    return ((void *)0);
}

canonical_original_code:
struct ap_net_connection_t *ap_net_conn_pool_get_conn_by_port(struct ap_net_conn_pool_t *pool, unsigned short port, int is_local)
{
    int i;
    int ip6;
    for ( i = 0; i < pool->max_connections; ++i)
    {
        ip6 = (is_local ? pool->conns[i].local.af : pool->conns[i].remote.af) == 10;
        if ( port == (is_local ? (ip6 ? pool->conns[i].local.addr6.sin6_port : pool->conns[i].local.addr4.sin_port)
                : (ip6 ? pool->conns[i].remote.addr6.sin6_port : pool->conns[i].remote.addr4.sin_port)) )
            return &pool->conns[i];
    }
    return ((void *)0);
}

variable_types:
port
unsigned short


original_code:
struct ap_net_connection_t *ap_net_conn_pool_get_conn_by_address(struct ap_net_conn_pool_t *pool, struct sockaddr_storage *ss, int is_local)
{
 struct ap_net_connection_t *conn;
 void *address;
 int port, ip6;
 int i;
    ip6 = (ss->ss_family == 10);
 address = (ip6 ? (void*)&((struct sockaddr_in6 *)ss)->sin6_addr : (void*)&((struct sockaddr_in *)ss)->sin_addr);
 port = (ip6 ? ((struct sockaddr_in6 *)ss)->sin6_port : ((struct sockaddr_in *)ss)->sin_port);
    for ( i = 0; i < pool->max_connections; ++i)
    {
     conn = &pool->conns[i];
        if ( port == (is_local ? (ip6 ? conn->local.addr6.sin6_port : conn->local.addr4.sin_port)
                      : (ip6 ? conn->remote.addr6.sin6_port : conn->remote.addr4.sin_port)
            )
             && 0 == memcmp(address,
                     (is_local ? (ip6 ? (void*)&conn->local.addr6.sin6_addr : (void*)&conn->local.addr4.sin_addr)
                         : (ip6 ? (void*)&conn->remote.addr6.sin6_addr : (void*)&conn->remote.addr4.sin_addr)
                     ),
                     ip6 ? sizeof(struct in6_addr) : sizeof(struct in_addr)
                  )
         )
            return conn;
    }
    return ((void *)0);
}

canonical_original_code:
struct ap_net_connection_t *ap_net_conn_pool_get_conn_by_address(struct ap_net_conn_pool_t *pool, struct sockaddr_storage *ss, int is_local)
{
 struct ap_net_connection_t *conn;
 void *address;
 int port; int ip6;
 int i;
    ip6 = (ss->ss_family == 10);
 address = (ip6 ? (void *)&((struct sockaddr_in6 *)ss)->sin6_addr : (void *)&((struct sockaddr_in *)ss)->sin_addr);
 port = (ip6 ? ((struct sockaddr_in6 *)ss)->sin6_port : ((struct sockaddr_in *)ss)->sin_port);
    for ( i = 0; i < pool->max_connections; ++i)
    {
     conn = &pool->conns[i];
        if ( port == (is_local ? (ip6 ? conn->local.addr6.sin6_port : conn->local.addr4.sin_port)
                      : (ip6 ? conn->remote.addr6.sin6_port : conn->remote.addr4.sin_port)
            )
             && 0 == memcmp(address,
                     (is_local ? (ip6 ? (void *)&conn->local.addr6.sin6_addr : (void *)&conn->local.addr4.sin_addr)
                         : (ip6 ? (void *)&conn->remote.addr6.sin6_addr : (void *)&conn->remote.addr4.sin_addr)
                     ),
                     ip6 ? sizeof(struct in6_addr) : sizeof(struct in_addr)
                  )
         )
            return conn;
    }
    return ((void *)0);
}

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *

variable_types:
address
void *

variable_types: port int


decompiled_code:
void __fastcall ap_net_connection_destroy(void *ptr, int a2)
{
void (__fastcall *v2)(void *, __int64); // rax

if ( *(_QWORD *)ptr )
{
v2 = *(void (__fastcall **)(void *, __int64))(*(_QWORD *)ptr + 80LL);
if ( v2 )
v2(ptr, 1LL);
}
free(*((void **)ptr + 14));
if ( a2 )
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr, int a2)
{
void (__fastcall *v2)(void *, __int64); // rax

if ( *(_QWORD *)ptr )
{
v2 = *(void (__fastcall **)(void *, __int64))(*(_QWORD *)ptr + 80LL);
if ( v2 )
v2(ptr, 1LL);
}
free(*((void **)ptr + 14));
if ( a2 )
free(ptr);
}

original_code:
void ap_net_connection_destroy(struct ap_net_connection_t *conn, int free_this)
{
 if (conn->parent != ((void *)0) && conn->parent->callback_func != ((void *)0) )
  conn->parent->callback_func(conn, 1 );
 free(conn->buf);
 if ( free_this )
     free(conn);
}


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

*(_DWORD *)(a1 + 124) = 0;
*(_DWORD *)(a1 + 128) = 0;
if ( a2 <= 0xFF )
result = memset(*(void **)(a1 + 112), a2, *(int *)(a1 + 120));
return result;
}

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

*(_DWORD *)(a1 + 124) = 0;
*(_DWORD *)(a1 + 128) = 0;
if ( a2 <= 0xFF )
result = memset(*(void **)(a1 + 112), a2, *(int *)(a1 + 120));
return result;
}

original_code:
void ap_net_connection_buf_clear(struct ap_net_connection_t *conn, int fill_char)
{
 conn->buffill = conn->bufpos = 0;
 if ( fill_char >= 0 && fill_char < 256 )
  memset(conn->buf, fill_char, conn->bufsize);
}


decompiled_code:
void __fastcall ap_net_conn_pool_destroy(void *ptr, int a2)
{
int v3; // edi
__int64 v4; // rbp

v3 = *((_DWORD *)ptr + 12);
if ( v3 != -1 )
close(v3);
if ( *((_QWORD *)ptr + 3) )
ap_net_poller_destroy();
if ( *((int *)ptr + 2) > 0 )
{
v4 = 0LL;
do
{
if ( (*(_BYTE *)(*(_QWORD *)ptr + 144 * v4 + 132) & 2) != 0 )
ap_net_conn_pool_close_connection(ptr, (unsigned int)v4);
ap_net_connection_destroy((void *)(*(_QWORD *)ptr + 144 * v4++), 0);
}
while ( *((_DWORD *)ptr + 2) > (int)v4 );
}
free(*(void **)ptr);
if ( a2 )
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr, int a2)
{
int v3; // edi
__int64 v4; // rbp

v3 = *((_DWORD *)ptr + 12);
if ( v3 != -1 )
close(v3);
if ( *((_QWORD *)ptr + 3) )
ap_net_poller_destroy();
if ( *((int *)ptr + 2) > 0 )
{
v4 = 0LL;
do
{
if ( (*(_BYTE *)(*(_QWORD *)ptr + 144 * v4 + 132) & 2) != 0 )
ap_net_conn_pool_close_connection(ptr, (unsigned int)v4);
func8((void *)(*(_QWORD *)ptr + 144 * v4++), 0);
}
while ( *((_DWORD *)ptr + 2) > (int)v4 );
}
free(*(void **)ptr);
if ( a2 )
free(ptr);
}

original_code:
void ap_net_conn_pool_destroy(struct ap_net_conn_pool_t *pool, int free_this)
{
    int i;
    if ( pool->listener.sock != -1 )
     close(pool->listener.sock);
    if ( pool->poller != ((void *)0) )
     ap_net_poller_destroy(pool->poller);
    for ( i = 0; i < pool->max_connections; ++i)
    {
        if ( pool->conns[i].state & 2 )
             ap_net_conn_pool_close_connection(pool, i);
        ap_net_connection_destroy(&pool->conns[i], 0);
    }
    free(pool->conns);
    if ( free_this )
     free(pool);
}

variable_types: struct ap_net_conn_pool_t { struct ap_net_connection_t { struct ap_net_conn_pool_t *parent; int idx; int fd; unsigned int flags; union { unsigned short af; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr4; struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } addr6; } local; union { unsigned short af; struct sockaddr_in addr4; struct sockaddr_in6 addr6; } remote; struct timespec { long tv_sec; long tv_nsec; } created_time; struct timespec expire; char *buf; int bufsize; unsigned int state; void *user_data; } *conns; int max_connections; int used_slots; unsigned int flags; unsigned int state; struct ap_net_poll_t { struct ap_net_conn_pool_t *parent; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; int max_events; int last_event_index; int events_count; int epoll_fd; int listen_socket_fd; int listen_socket_flags; int debug; int emit_old_data_signal; } *poller; struct timespec max_conn_ttl; struct { int sock; union { struct sockaddr_in addr4; struct sockaddr_in6 addr6; }; } listener; int (*callback_func)(struct ap_net_connection_t *conn, int signal_type); struct ap_net_stat_t { unsigned int conn_count; unsigned int timedout; unsigned int queue_full_count; unsigned int active_conn_count; struct timespec total_time; } stat; } *


decompiled_code:
__int64 __fastcall ap_net_conn_pool_find_free_slot(__int64 *a1)
{
int v1; // edx
__int64 v2; // rax
__int64 v3; // rcx
__int64 v4; // rdx
__int64 result; // rax

v1 = *((_DWORD *)a1 + 2);
if ( *((_DWORD *)a1 + 3) == v1 )
{
++*((_DWORD *)a1 + 24);
ap_error_set("ap_net_conn_pool_find_free_slot()", 4LL);
result = 0LL;
}
else
{
if ( v1 <= 0 )
{
v4 = 0LL;
}
else
{
v2 = *a1;
v3 = *a1 + 144LL * (unsigned int)(v1 - 1) + 144;
while ( 1 )
{
v4 = v2;
if ( (*(_BYTE *)(v2 + 132) & 2) == 0 )
break;
v2 += 144LL;
if ( v2 == v3 )
{
v4 = 0LL;
break;
}
}
}
result = v4;
}
return result;
}

original_code:
struct ap_net_connection_t *ap_net_conn_pool_find_free_slot(struct ap_net_conn_pool_t *pool)
{
    struct ap_net_connection_t *conn;
    int conn_idx;
    if (pool->used_slots == pool->max_connections)
    {
        pool->stat.queue_full_count++;
        ap_error_set("ap_net_conn_pool_find_free_slot()", 4);
        return ((void *)0);
    }
    conn = ((void *)0);
    for ( conn_idx = 0; conn_idx < pool->max_connections; ++conn_idx )
        if ( ! (pool->conns[conn_idx].state & 2) )
        {
            conn = &pool->conns[conn_idx];
            break;
        }
    return conn;
}


decompiled_code:
__int64 __fastcall ap_net_recv(int fd, void *buf, int a3, int a4)
{
int v6; // ebx

ap_error_clear();
v6 = fcntl(fd, 3);
if ( v6 == -1 || (v6 = recv(fd, buf, a3, a4 == 0 ? 0x4000 : 16448), v6 < 0) )
ap_error_set_detailed("ap_net_recv", 1LL, "sock %d", (unsigned int)fd);
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func12(int fd, void *buf, int a3, int a4)
{
int v6; // ebx

ap_error_clear();
v6 = fcntl(fd, 3);
if ( v6 == -1 || (v6 = recv(fd, buf, a3, a4 == 0 ? 0x4000 : 16448), v6 < 0) )
ap_error_set_detailed("ap_net_recv", 1LL, "sock %d", (unsigned int)fd);
return (unsigned int)v6;
}

original_code:
int ap_net_recv(int sh, void *buf, int size, int non_blocking)
{
    int retval;
    int flags;
    ap_error_clear();
    retval = fcntl(sh, 3);
    if ( retval != -1 )
    {
     flags = (non_blocking ? MSG_DONTWAIT : 0) | MSG_NOSIGNAL;
        retval = recv(sh, buf, size, flags);
    }
    if ( retval < 0 )
        ap_error_set_detailed("ap_net_recv", 1, "sock %d", sh);
    return retval;
}

variable_types: sh int

variable_types:
buf
void *

variable_types: size int

variable_types: retval int

variable_types: flags int


decompiled_code:
__int64 __fastcall ap_net_send(int fd, void *buf, int a3, int a4)
{
int v6; // ebx

ap_error_clear();
v6 = fcntl(fd, 3);
if ( v6 == -1 || (v6 = send(fd, buf, a3, a4 == 0 ? 0x4000 : 16448), v6 <= 0) )
ap_error_set_detailed("ap_net_send", 1LL, "sock %d", (unsigned int)fd);
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func13(int fd, void *buf, int a3, int a4)
{
int v6; // ebx

ap_error_clear();
v6 = fcntl(fd, 3);
if ( v6 == -1 || (v6 = send(fd, buf, a3, a4 == 0 ? 0x4000 : 16448), v6 <= 0) )
ap_error_set_detailed("ap_net_send", 1LL, "sock %d", (unsigned int)fd);
return (unsigned int)v6;
}

original_code:
int ap_net_send(int sh, void *buf, int size, int non_blocking)
{
    int retval;
    int flags;
    ap_error_clear();
    retval = fcntl(sh, 3);
    if ( retval != -1 )
    {
     flags = (non_blocking ? MSG_DONTWAIT : 0) | MSG_NOSIGNAL;
        retval = send(sh, buf, size, flags);
    }
    if ( retval <= 0 )
        ap_error_set_detailed("ap_net_send", 1, "sock %d", sh);
    return retval;
}


decompiled_code:
__int64 __fastcall ap_net_conn_pool_accept_connection(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp
socklen_t v3; // eax
int v4; // eax
int v5; // er12
unsigned int v6; // eax
unsigned int (__fastcall *v7)(__int64, __int64); // rax
__int64 v9; // rax
unsigned int v10; // ebp
unsigned __int16 v11; // ax
unsigned int (__fastcall *v12)(__int64, __int64); // rax
void (__fastcall *v13)(__int64, __int64); // rax
unsigned int v14; // er13
unsigned __int16 v15; // bp
unsigned int v16; // eax
struct sockaddr v17[8]; // [rsp+0h] [rbp-B8h] BYREF
socklen_t addr_len; // [rsp+88h] [rbp-30h] BYREF
int buf[11]; // [rsp+8Ch] [rbp-2Ch] BYREF

ap_error_clear();
if ( (*(_BYTE *)(a1 + 16) & 1) != 0 )
{
v1 = ap_net_conn_pool_find_free_slot(a1);
v2 = v1;
if ( v1 )
{
ap_net_conn_pool_connection_pre_connect(a1, *(unsigned int *)(v1 + 8), 0LL);
*(_WORD *)(v2 + 48) = (*(_DWORD *)(a1 + 16) & 4) == 0 ? 2 : 10;
if ( (*(_BYTE *)(a1 + 16) & 4) != 0 )
v3 = 28;
else
v3 = 16;
addr_len = v3;
v4 = accept(*(_DWORD *)(a1 + 48), (struct sockaddr *)(v2 + 48), &addr_len);
v5 = v4;
if ( v4 == -1 )
{
ap_error_set_detailed("ap_net_conn_pool_accept_connection()", 1LL, "accept()");
ap_net_connection_unlock(v2);
v2 = 0LL;
}
else
{
v6 = fcntl(v4, 3);
BYTE1(v6) |= 8u;
fcntl(v5, 4, v6);
*(_DWORD *)(v2 + 12) = v5;
if ( (unsigned int)ap_net_conn_pool_poller_add_conn(a1, *(unsigned int *)(v2 + 8)) )
{
v7 = *(unsigned int (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v7 && !v7(v2, 3LL) )
{
ap_net_conn_pool_close_connection(a1, *(unsigned int *)(v2 + 8));
ap_error_set("ap_net_conn_pool_accept_connection()", 8LL);
v2 = 0LL;
}
else
{
if ( ap_log_debug_level )
ap_log_debug_log("* Got connected at #%d\n", *(unsigned int *)(v2 + 8));
ap_net_connection_unlock(v2);
}
}
else
{
ap_net_conn_pool_close_connection(a1, *(unsigned int *)(v2 + 8));
v2 = 0LL;
}
}
}
}
else
{
addr_len = 128;
buf[0] = recvfrom(*(_DWORD *)(a1 + 48), buf, 1uLL, 66, v17, &addr_len);
if ( buf[0] == -1 )
{
ap_error_set("ap_net_conn_pool_accept_connection()", 1LL);
return 0LL;
}
v9 = ap_net_conn_pool_get_conn_by_address(a1, v17, 0LL);
if ( v9 )
{
v2 = v9;
}
else
{
if ( v17[0].sa_family == 2 )
{
v14 = ap_utils_timespec_to_milliseconds(a1 + 32);
v15 = _bswap_16(*(__int16 *)v17[0].sa_data);
v16 = _bswap_32(*(unsigned int *)&v17[0].sa_data[2]);
v2 = ap_net_conn_pool_connect_ip4(a1, 1LL, v16, v15, v14);
}
else
{
v10 = ap_utils_timespec_to_milliseconds(a1 + 32);
v11 = _bswap_16(*(__int16 *)v17[0].sa_data);
v2 = ap_net_conn_pool_connect_ip6(a1, 1LL, &v17[0].sa_data[6], v11, v10);
}
if ( !v2 )
return v2;
v12 = *(unsigned int (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v12 && !v12(v2, 3LL) )
{
ap_net_conn_pool_close_connection(a1, *(unsigned int *)(v2 + 8));
ap_error_set("ap_net_conn_pool_accept_connection()", 8LL);
return 0LL;
}
}
buf[0] = ap_net_conn_pool_recv(a1, *(unsigned int *)(v2 + 8));
if ( buf[0] > 0 )
{
v13 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v13 )
v13(v2, 7LL);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp
socklen_t v3; // eax
int v4; // eax
int v5; // er12
unsigned int v6; // eax
unsigned int (__fastcall *v7)(__int64, __int64); // rax
__int64 v9; // rax
unsigned int v10; // ebp
unsigned __int16 v11; // ax
unsigned int (__fastcall *v12)(__int64, __int64); // rax
void (__fastcall *v13)(__int64, __int64); // rax
unsigned int v14; // er13
unsigned __int16 v15; // bp
unsigned int v16; // eax
struct sockaddr v17[8]; // [rsp+0h] [rbp-B8h] BYREF
socklen_t addr_len; // [rsp+88h] [rbp-30h] BYREF
int buf[11]; // [rsp+8Ch] [rbp-2Ch] BYREF

ap_error_clear();
if ( (*(_BYTE *)(a1 + 16) & 1) != 0 )
{
v1 = ap_net_conn_pool_find_free_slot(a1);
v2 = v1;
if ( v1 )
{
ap_net_conn_pool_connection_pre_connect(a1, *(unsigned int *)(v1 + 8), 0LL);
*(_WORD *)(v2 + 48) = (*(_DWORD *)(a1 + 16) & 4) == 0 ? 2 : 10;
if ( (*(_BYTE *)(a1 + 16) & 4) != 0 )
v3 = 28;
else
v3 = 16;
addr_len = v3;
v4 = accept(*(_DWORD *)(a1 + 48), (struct sockaddr *)(v2 + 48), &addr_len);
v5 = v4;
if ( v4 == -1 )
{
ap_error_set_detailed("ap_net_conn_pool_accept_connection()", 1LL, "accept()");
ap_net_connection_unlock(v2);
v2 = 0LL;
}
else
{
v6 = fcntl(v4, 3);
BYTE1(v6) |= 8u;
fcntl(v5, 4, v6);
*(_DWORD *)(v2 + 12) = v5;
if ( (unsigned int)ap_net_conn_pool_poller_add_conn(a1, *(unsigned int *)(v2 + 8)) )
{
v7 = *(unsigned int (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v7 && !v7(v2, 3LL) )
{
ap_net_conn_pool_close_connection(a1, *(unsigned int *)(v2 + 8));
ap_error_set("ap_net_conn_pool_accept_connection()", 8LL);
v2 = 0LL;
}
else
{
if ( ap_log_debug_level )
ap_log_debug_log("* Got connected at #%d\n", *(unsigned int *)(v2 + 8));
ap_net_connection_unlock(v2);
}
}
else
{
ap_net_conn_pool_close_connection(a1, *(unsigned int *)(v2 + 8));
v2 = 0LL;
}
}
}
}
else
{
addr_len = 128;
buf[0] = recvfrom(*(_DWORD *)(a1 + 48), buf, 1uLL, 66, v17, &addr_len);
if ( buf[0] == -1 )
{
ap_error_set("ap_net_conn_pool_accept_connection()", 1LL);
return 0LL;
}
v9 = ap_net_conn_pool_get_conn_by_address(a1, v17, 0LL);
if ( v9 )
{
v2 = v9;
}
else
{
if ( v17[0].sa_family == 2 )
{
v14 = ap_utils_timespec_to_milliseconds(a1 + 32);
v15 = func1(*(__int16 *)v17[0].sa_data);
v16 = func0(*(unsigned int *)&v17[0].sa_data[2]);
v2 = ap_net_conn_pool_connect_ip4(a1, 1LL, v16, v15, v14);
}
else
{
v10 = ap_utils_timespec_to_milliseconds(a1 + 32);
v11 = func1(*(__int16 *)v17[0].sa_data);
v2 = ap_net_conn_pool_connect_ip6(a1, 1LL, &v17[0].sa_data[6], v11, v10);
}
if ( !v2 )
return v2;
v12 = *(unsigned int (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v12 && !v12(v2, 3LL) )
{
ap_net_conn_pool_close_connection(a1, *(unsigned int *)(v2 + 8));
ap_error_set("ap_net_conn_pool_accept_connection()", 8LL);
return 0LL;
}
}
buf[0] = ap_net_conn_pool_recv(a1, *(unsigned int *)(v2 + 8));
if ( buf[0] > 0 )
{
v13 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v13 )
v13(v2, 7LL);
}
}
return v2;
}

original_code:
struct ap_net_connection_t *ap_net_conn_pool_accept_connection(struct ap_net_conn_pool_t *pool)
{
    int n;
    socklen_t addr_len;
    int new_sock;
    struct ap_net_connection_t *conn;
    struct sockaddr_storage addr;
    struct sockaddr *remote_addr;
    ap_error_clear();
    if ( (((pool->flags) & (1)) != 0) )
    {
     conn = ap_net_conn_pool_find_free_slot(pool);
     if ( conn == ((void *)0) )
      return ((void *)0);
     ap_net_conn_pool_connection_pre_connect(pool, conn->idx, 0);
     conn->remote.af = (((pool->flags) & (4)) != 0) ? 10 : 2;
        remote_addr = (((pool->flags) & (4)) != 0) ? (struct sockaddr *)&conn->remote.addr6 : (struct sockaddr *)&conn->remote.addr4;
        addr_len = (((pool->flags) & (4)) != 0) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
        new_sock = accept(pool->listener.sock, remote_addr, &addr_len);
        if ( new_sock == -1 )
        {
            ap_error_set_detailed(_func_name, 1, "accept()");
            ap_net_connection_unlock(conn);
            return ((void *)0);
        }
        fcntl(new_sock, 4, fcntl(new_sock, 3) | 04000);
        conn->fd = new_sock;
        if ( ! ap_net_conn_pool_poller_add_conn(pool, conn->idx) )
        {
         ap_net_conn_pool_close_connection(pool, conn->idx);
         return ((void *)0);
        }
     if (pool->callback_func != ((void *)0) && ! pool->callback_func(conn, 3) )
        {
           ap_net_conn_pool_close_connection(pool, conn->idx);
           ap_error_set(_func_name, 8);
            return ((void *)0);
        }
    }
    else
    {
     addr_len = sizeof(addr);
     n = recvfrom(pool->listener.sock, &n, 1, MSG_DONTWAIT | MSG_PEEK, (struct sockaddr *)&addr, &addr_len);
        if ( n == -1 )
        {
            ap_error_set(_func_name, 1);
            return ((void *)0);
        }
        conn = ap_net_conn_pool_get_conn_by_address(pool, &addr, 0);
        if ( conn == ((void *)0) )
        {
         if ( addr.ss_family == 2 )
         {
                conn = ap_net_conn_pool_connect_ip4(pool, 1,
                   ntohl(((struct sockaddr_in*)&addr)->sin_addr.s_addr), ntohs(((struct sockaddr_in*)&addr)->sin_port),
                   ap_utils_timespec_to_milliseconds(&pool->max_conn_ttl));
         }
         else
         {
                conn = ap_net_conn_pool_connect_ip6(pool, 1,
                   &((struct sockaddr_in6*)&addr)->sin6_addr, ntohs(((struct sockaddr_in6*)&addr)->sin6_port),
                   ap_utils_timespec_to_milliseconds(&pool->max_conn_ttl));
         }
            if( conn == ((void *)0) )
                return ((void *)0);
            if (pool->callback_func != ((void *)0) && ! pool->callback_func(conn, 3) )
            {
                ap_net_conn_pool_close_connection(pool, conn->idx);
                ap_error_set(_func_name, 8);
                return ((void *)0);
            }
        }
     n = ap_net_conn_pool_recv(pool, conn->idx);
     if ( n > 0 && pool->callback_func != ((void *)0) )
             pool->callback_func(conn, 7);
        return conn;
    }
    if (ap_log_debug_level)
        ap_log_debug_log("* Got connected at #%d\n", conn->idx);
    ap_net_connection_unlock(conn);
    return conn;
}

canonical_original_code:
struct ap_net_connection_t *ap_net_conn_pool_accept_connection(struct ap_net_conn_pool_t *pool)
{
    int n;
    unsigned int addr_len;
    int new_sock;
    struct ap_net_connection_t *conn;
    struct sockaddr_storage addr;
    struct sockaddr *remote_addr;
    ap_error_clear();
    if ( (((pool->flags) & (1)) != 0) )
    {
     conn = ap_net_conn_pool_find_free_slot(pool);
     if ( conn == ((void *)0) )
      return ((void *)0);
     ap_net_conn_pool_connection_pre_connect(pool, conn->idx, 0);
     conn->remote.af = (((pool->flags) & (4)) != 0) ? 10 : 2;
        remote_addr = (((pool->flags) & (4)) != 0) ? (struct sockaddr *)&conn->remote.addr6 : (struct sockaddr *)&conn->remote.addr4;
        addr_len = (((pool->flags) & (4)) != 0) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
        new_sock = accept(pool->listener.sock, remote_addr, &addr_len);
        if ( new_sock == -1 )
        {
            ap_error_set_detailed(_func_name, 1, "accept()");
            ap_net_connection_unlock(conn);
            return ((void *)0);
        }
        fcntl(new_sock, 4, fcntl(new_sock, 3) | 04000);
        conn->fd = new_sock;
        if ( ! ap_net_conn_pool_poller_add_conn(pool, conn->idx) )
        {
         ap_net_conn_pool_close_connection(pool, conn->idx);
         return ((void *)0);
        }
     if (pool->callback_func != ((void *)0) && ! pool->callback_func(conn, 3) )
        {
           ap_net_conn_pool_close_connection(pool, conn->idx);
           ap_error_set(_func_name, 8);
            return ((void *)0);
        }
    }
    else
    {
     addr_len = sizeof(addr);
     n = recvfrom(pool->listener.sock, &n, 1, MSG_DONTWAIT | MSG_PEEK, (struct sockaddr *)&addr, &addr_len);
        if ( n == -1 )
        {
            ap_error_set(_func_name, 1);
            return ((void *)0);
        }
        conn = ap_net_conn_pool_get_conn_by_address(pool, &addr, 0);
        if ( conn == ((void *)0) )
        {
         if ( addr.ss_family == 2 )
         {
                conn = ap_net_conn_pool_connect_ip4(pool, 1,
                   ntohl(((struct sockaddr_in *)&addr)->sin_addr.s_addr), ntohs(((struct sockaddr_in *)&addr)->sin_port),
                   ap_utils_timespec_to_milliseconds(&pool->max_conn_ttl));
         }
         else
         {
                conn = ap_net_conn_pool_connect_ip6(pool, 1,
                   &((struct sockaddr_in6 *)&addr)->sin6_addr, ntohs(((struct sockaddr_in6 *)&addr)->sin6_port),
                   ap_utils_timespec_to_milliseconds(&pool->max_conn_ttl));
         }
            if( conn == ((void *)0) )
                return ((void *)0);
            if (pool->callback_func != ((void *)0) && ! pool->callback_func(conn, 3) )
            {
                ap_net_conn_pool_close_connection(pool, conn->idx);
                ap_error_set(_func_name, 8);
                return ((void *)0);
            }
        }
     n = ap_net_conn_pool_recv(pool, conn->idx);
     if ( n > 0 && pool->callback_func != ((void *)0) )
             pool->callback_func(conn, 7);
        return conn;
    }
    if (ap_log_debug_level)
        ap_log_debug_log("* Got connected at #%d\n", conn->idx);
    ap_net_connection_unlock(conn);
    return conn;
}

variable_types: n int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; } *


decompiled_code:
__int64 __fastcall ap_utils_timeval_cmp_to_now(__time_t *a1)
{
__int64 result; // rax
bool v2; // al
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v3, 0LL);
if ( *a1 != v3.tv_sec )
{
result = 1LL;
if ( *a1 > v3.tv_sec )
return result;
v2 = *a1 < v3.tv_sec;
goto LABEL_4;
}
result = 1LL;
if ( a1[1] <= v3.tv_usec )
{
v2 = a1[1] < v3.tv_usec;
LABEL_4:
result = (unsigned int)-v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__time_t *a1)
{
__int64 result; // rax
bool v2; // al
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v3, 0LL);
if ( *a1 != v3.tv_sec )
{
result = 1LL;
if ( *a1 > v3.tv_sec )
return result;
v2 = *a1 < v3.tv_sec;
goto LABEL_4;
}
result = 1LL;
if ( a1[1] <= v3.tv_usec )
{
v2 = a1[1] < v3.tv_usec;
LABEL_4:
result = (unsigned int)-v2;
}
return result;
}

original_code:
int ap_utils_timeval_cmp_to_now(struct timeval *tv)
{
    struct timeval now;
    gettimeofday(&now, ((void *)0));
    if ( (((tv)->tv_sec == (&now)->tv_sec) ? ((tv)->tv_usec > (&now)->tv_usec) : ((tv)->tv_sec > (&now)->tv_sec)) )
        return 1;
    else if ( (((tv)->tv_sec == (&now)->tv_sec) ? ((tv)->tv_usec < (&now)->tv_usec) : ((tv)->tv_sec < (&now)->tv_sec)) )
        return -1;
    return 0;
}

variable_types: struct timeval { long tv_sec; long tv_usec; } *


original_code:
int ap_utils_timeval_set(struct timeval *tv, int mode, int msec)
{
   struct timeval tmp;
   if ( msec < 1 )
    return 0;
   switch( mode )
   {
    case 0:
     tmp.tv_sec = msec / 1000;
     tmp.tv_usec = 1000 * (msec % 1000);
     do { (tv)->tv_sec = (tv)->tv_sec + (&tmp)->tv_sec; (tv)->tv_usec = (tv)->tv_usec + (&tmp)->tv_usec; if ((tv)->tv_usec >= 1000000) { ++(tv)->tv_sec; (tv)->tv_usec -= 1000000; } } while (0);
     break;
    case 1:
     tmp.tv_sec = msec / 1000;
     tmp.tv_usec = 1000 * (msec % 1000);
     do { (tv)->tv_sec = (tv)->tv_sec - (&tmp)->tv_sec; (tv)->tv_usec = (tv)->tv_usec - (&tmp)->tv_usec; if ((tv)->tv_usec < 0) { --(tv)->tv_sec; (tv)->tv_usec += 1000000; } } while (0);
     break;
    case 2:
     gettimeofday(tv, ((void *)0));
     tmp.tv_sec = msec / 1000;
     tmp.tv_usec = 1000 * (msec % 1000);
     do { (tv)->tv_sec = (tv)->tv_sec + (&tmp)->tv_sec; (tv)->tv_usec = (tv)->tv_usec + (&tmp)->tv_usec; if ((tv)->tv_usec >= 1000000) { ++(tv)->tv_sec; (tv)->tv_usec -= 1000000; } } while (0);
     break;
    case 3:
     tv->tv_sec = msec / 1000;
     tv->tv_usec = 1000 * (msec % 1000);
     break;
    default:
     return 0;
   }
   return 1;
}

variable_types: mode int

variable_types: msec int

variable_types: struct timeval { long tv_sec; long tv_usec; }