decompiled_code:
__int64 __fastcall ap_log_add_debug_handle(int fd)
{
unsigned int v1; // er12
unsigned int v2; // er12
int v3; // ebx
__int64 v4; // rsi
__int64 result; // rax

v1 = ap_log_get_lock();
if ( !v1 )
return v1;
v2 = is_debug_handle_internal(fd);
if ( v2 )
{
LABEL_6:
ap_log_release_lock();
return 1;
}
v3 = debug_handles_count;
if ( debug_handles_count == 5 )
{
ap_log_release_lock();
result = v2;
}
else
{
v4 = 38LL * debug_handles_count;
debug_handles[v4] = fd;
if ( !fstat(fd, (struct stat *)((char *)&unk_F48 + v4 * 4)) )
{
debug_handles_count = v3 + 1;
goto LABEL_6;
}
ap_log_release_lock();
ap_error_set_custom("ap_log_add_debug_handle()", "fstat()");
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd)
{
unsigned int v1; // er12
unsigned int v2; // er12
int v3; // ebx
__int64 v4; // rsi
__int64 result; // rax

v1 = func2();
if ( !v1 )
return v1;
v2 = func0(fd);
if ( v2 )
{
LABEL_6:
func3();
return 1;
}
v3 = debug_handles_count;
if ( debug_handles_count == 5 )
{
func3();
result = v2;
}
else
{
v4 = 38LL * debug_handles_count;
debug_handles[v4] = fd;
if ( !fstat(fd, (struct stat *)((char *)&unk_F48 + v4 * 4)) )
{
debug_handles_count = v3 + 1;
goto LABEL_6;
}
func3();
ap_error_set_custom("ap_log_add_debug_handle()", "fstat()");
result = v2;
}
return result;
}

original_code:
int ap_log_add_debug_handle(int fd)
{
    if ( !ap_log_get_lock() )
        return 0;
    if ( is_debug_handle_internal(fd) )
    {
        ap_log_release_lock();
        return 1;
    }
    if ( debug_handles_count == 5 )
    {
        ap_log_release_lock();
        return 0;
    }
    debug_handles[debug_handles_count].fd = fd;
    if ( 0 != fstat(fd, &debug_handles[debug_handles_count].statbuf) )
    {
        ap_log_release_lock();
        ap_error_set_custom("ap_log_add_debug_handle()", "fstat()");
        return 0;
    }
    debug_handles_count++;
    ap_log_release_lock();
    return 1;
}


repo: kadavris/aps-toolkit

decompiled_code:
void __fastcall ap_log_remove_debug_handle(int a1)
{
if ( (unsigned int)ap_log_get_lock() )
{
remove_debug_handle_internal(a1);
ap_log_release_lock();
}
}

canonical_decompiled_code:
void __fastcall func5(int a1)
{
if ( (unsigned int)func2() )
{
func1(a1);
func3();
}
}

original_code:
int ap_log_remove_debug_handle(int fd)
{
    int retcode;
    if ( ! ap_log_get_lock() )
        return 0;
    retcode = remove_debug_handle_internal(fd);
    ap_log_release_lock();
    return retcode;
}

variable_types: fd int

variable_types: retcode int

return_type: int


decompiled_code:
void __fastcall ap_log_is_debug_handle(int a1)
{
if ( (unsigned int)ap_log_get_lock() )
{
is_debug_handle_internal(a1);
ap_log_release_lock();
}
}

canonical_decompiled_code:
void __fastcall func6(int a1)
{
if ( (unsigned int)func2() )
{
func0(a1);
func3();
}
}

original_code:
int ap_log_is_debug_handle(int fd)
{
    int retcode;
    if ( ! ap_log_get_lock() )
        return 0;
    retcode = is_debug_handle_internal(fd);
    ap_log_release_lock();
    return retcode;
}


decompiled_code:
ssize_t __fastcall ap_log_debug_log_raw(void *buf, int a2)
{
ssize_t result; // rax
int v4; // ebx
int v5; // edi

if ( ap_log_debug_to_tty )
fputs((const char *)buf, stderr);
result = (unsigned int)debug_handles_count;
v4 = 0;
if ( debug_handles_count > 0 )
{
while ( 1 )
{
v5 = debug_handles[38 * v4];
if ( (dword_F60[38 * v4] & 0xF000) == 0x8000 )
break;
result = send(v5, buf, a2, 0x4000);
if ( result > 0 )
{
++v4;
LABEL_6:
if ( debug_handles_count <= v4 )
return result;
}
else
{
result = remove_debug_handle_internal(debug_handles[38 * v4]);
if ( debug_handles_count <= v4 )
return result;
}
}
++v4;
result = write(v5, buf, a2);
goto LABEL_6;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func7(void *buf, int a2)
{
ssize_t result; // rax
int v4; // ebx
int v5; // edi

if ( ap_log_debug_to_tty )
fputs((const char *)buf, stderr);
result = (unsigned int)debug_handles_count;
v4 = 0;
if ( debug_handles_count > 0 )
{
while ( 1 )
{
v5 = debug_handles[38 * v4];
if ( (dword_F60[38 * v4] & 0xF000) == 0x8000 )
break;
result = send(v5, buf, a2, 0x4000);
if ( result > 0 )
{
++v4;
LABEL_6:
if ( debug_handles_count <= v4 )
return result;
}
else
{
result = func1(debug_handles[38 * v4]);
if ( debug_handles_count <= v4 )
return result;
}
}
++v4;
result = write(v5, buf, a2);
goto LABEL_6;
}
return result;
}

original_code:
void ap_log_debug_log_raw(char *buf, int buflen)
{
    int i;
    if ( ap_log_debug_to_tty )
        fputs(buf, stderr);
    for ( i = 0; i < debug_handles_count; ++i )
    {
     if ( ((((debug_handles[i].statbuf.st_mode)) & 0170000) == (0100000)) )
     {
            write(debug_handles[i].fd, buf, buflen);
            continue;
     }
  if ( 0 >= send(debug_handles[i].fd, buf, buflen, MSG_NOSIGNAL))
  {
            remove_debug_handle_internal(debug_handles[i].fd);
            i--;
  }
    }
}

variable_types:
buf
char *

variable_types: buflen int

variable_types: i int

return_type: void


decompiled_code:
void ap_log_debug_log(char *format, ...)
{
int v1; // eax
int v2; // er13
int v3; // er12
int v4; // eax
time_t v5; // rax
time_t v6; // rbx
gcc_va_list arg; // [rsp+8h] [rbp-4E0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4C8h] BYREF

if ( (unsigned int)ap_log_get_lock() )
{
va_start(arg, format);
v1 = vsnprintf(s, 0x3FFuLL, format, arg);
v2 = repeats_2;
v3 = v1;
if ( lastlen_4 == v1 && !strcmp(lastmsg_3, s) && (++v2, repeats_2 = v2, v6 = last_time_1 + 2, v6 < time(0LL)) )
{
if ( v3 > 30 || v2 > 1 )
v3 = sprintf(s, "... Last message repeated %d time(s)\n", (unsigned int)v2);
ap_log_debug_log_raw(s, v3);
repeats_2 = 0;
last_time_1 = time(0LL);
ap_log_release_lock();
}
else
{
if ( v2 > 1 )
{
v4 = sprintf(lastmsg_3, "... Last message repeated %d time(s)\n", (unsigned int)v2);
v2 = repeats_2;
lastlen_4 = v4;
}
if ( v2 > 0 )
ap_log_debug_log_raw(lastmsg_3, lastlen_4);
strcpy(lastmsg_3, s);
lastlen_4 = v3;
v5 = time(0LL);
repeats_2 = 0;
last_time_1 = v5;
ap_log_debug_log_raw(s, v3);
ap_log_release_lock();
}
}
}

canonical_decompiled_code:
void func8(char *format, ...)
{
int v1; // eax
int v2; // er13
int v3; // er12
int v4; // eax
time_t v5; // rax
time_t v6; // rbx
gcc_va_list arg; // [rsp+8h] [rbp-4E0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4C8h] BYREF

if ( (unsigned int)func2() )
{
va_start(arg, format);
v1 = vsnprintf(s, 0x3FFuLL, format, arg);
v2 = repeats_2;
v3 = v1;
if ( lastlen_4 == v1 && !strcmp(lastmsg_3, s) && (++v2, repeats_2 = v2, v6 = last_time_1 + 2, v6 < time(0LL)) )
{
if ( v3 > 30 || v2 > 1 )
v3 = sprintf(s, "... Last message repeated %d time(s)\n", (unsigned int)v2);
func7(s, v3);
repeats_2 = 0;
last_time_1 = time(0LL);
func3();
}
else
{
if ( v2 > 1 )
{
v4 = sprintf(lastmsg_3, "... Last message repeated %d time(s)\n", (unsigned int)v2);
v2 = repeats_2;
lastlen_4 = v4;
}
if ( v2 > 0 )
func7(lastmsg_3, lastlen_4);
strcpy(lastmsg_3, s);
lastlen_4 = v3;
v5 = time(0LL);
repeats_2 = 0;
last_time_1 = v5;
func7(s, v3);
func3();
}
}
}

original_code:
void ap_log_debug_log(char *fmt, ...)
{
    va_list vl;
    int buflen;
    char buf[1024];
    static int repeats = 0;
    static char lastmsg[1024];
    static int lastlen = 0;
    static time_t last_time;
    static char* msg_repeat = "... Last message repeated %d time(s)\n";
    if ( ! ap_log_get_lock() )
        return;
    __builtin_va_start(vl,fmt);
    buflen = vsnprintf(buf, 1023, fmt, vl);
    __builtin_va_end(vl);
    if ( lastlen == buflen && 0 == strcmp(lastmsg, buf) )
    {
        ++repeats;
        if ( last_time + 3 <= time(((void *)0)) )
        {
            if ( repeats > 1 || lastlen > 30 )
                buflen = sprintf(buf, msg_repeat, repeats);
            ap_log_debug_log_raw(buf, buflen);
            repeats = 0;
            last_time = time(((void *)0));
            ap_log_release_lock();
            return;
        }
    }
    if ( repeats > 1 )
        lastlen = sprintf(lastmsg, msg_repeat, repeats);
    if ( repeats > 0 )
        ap_log_debug_log_raw(lastmsg, lastlen);
    strcpy(lastmsg, buf);
    lastlen = buflen;
    last_time = time(((void *)0));
    repeats = 0;
    ap_log_debug_log_raw(buf, buflen);
    ap_log_release_lock();
}

canonical_original_code:
void ap_log_debug_log(char *fmt, ...)
{
    __builtin_va_list vl;
    int buflen;
    char buf[1024];
    static int repeats = 0;
    static char lastmsg[1024];
    static int lastlen = 0;
    static long last_time;
    static char *msg_repeat = "... Last message repeated %d time(s)\n";
    if ( ! ap_log_get_lock() )
        return;
    __builtin_va_start(vl,fmt);
    buflen = vsnprintf(buf, 1023, fmt, vl);
    __builtin_va_end(vl);
    if ( lastlen == buflen && 0 == strcmp(lastmsg, buf) )
    {
        ++repeats;
        if ( last_time + 3 <= time(((void *)0)) )
        {
            if ( repeats > 1 || lastlen > 30 )
                buflen = sprintf(buf, msg_repeat, repeats);
            ap_log_debug_log_raw(buf, buflen);
            repeats = 0;
            last_time = time(((void *)0));
            ap_log_release_lock();
            return;
        }
    }
    if ( repeats > 1 )
        lastlen = sprintf(lastmsg, msg_repeat, repeats);
    if ( repeats > 0 )
        ap_log_debug_log_raw(lastmsg, lastlen);
    strcpy(lastmsg, buf);
    lastlen = buflen;
    last_time = time(((void *)0));
    repeats = 0;
    ap_log_debug_log_raw(buf, buflen);
    ap_log_release_lock();
}

variable_types:
fmt
char *

variable_types:
buf
char [1024]

variable_types: repeats int

variable_types:
lastmsg
char [1024]

variable_types: lastlen int


decompiled_code:
void ap_log_do_syslog(int pri, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x3FFuLL, format, arg);
syslog(pri, s);
if ( ap_log_debug_level )
ap_log_debug_log(s);
}

canonical_decompiled_code:
void func9(int pri, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x3FFuLL, format, arg);
syslog(pri, s);
if ( ap_log_debug_level )
func8(s);
}

original_code:
void ap_log_do_syslog(int priority, char *fmt, ...)
{
    va_list vl;
    char buf[1024];
    __builtin_va_start(vl,fmt);
    vsnprintf(buf, 1023, fmt, vl);
    __builtin_va_end(vl);
    syslog(priority, buf);
    if ( ap_log_debug_level )
        ap_log_debug_log(buf);
}

canonical_original_code:
void ap_log_do_syslog(int priority, char *fmt, ...)
{
    __builtin_va_list vl;
    char buf[1024];
    __builtin_va_start(vl,fmt);
    vsnprintf(buf, 1023, fmt, vl);
    __builtin_va_end(vl);
    syslog(priority, buf);
    if ( ap_log_debug_level )
        ap_log_debug_log(buf);
}

variable_types: priority int


decompiled_code:
ssize_t ap_log_hprintf(int fd, char *format, ...)
{
int v2; // eax
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-4E0h]
gcc_va_list arg; // [rsp+18h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+30h] [rbp-4B8h] BYREF

va_start(arg, format);
v2 = vsnprintf(s, 0x400uLL, format, arg);
result = write(fd, s, v2);
if ( (int)result <= 0 )
{
v4 = result;
ap_error_set("ap_log_hprintf()", 1LL);
result = v4;
}
return result;
}

canonical_decompiled_code:
ssize_t func10(int fd, char *format, ...)
{
int v2; // eax
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-4E0h]
gcc_va_list arg; // [rsp+18h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+30h] [rbp-4B8h] BYREF

va_start(arg, format);
v2 = vsnprintf(s, 0x400uLL, format, arg);
result = write(fd, s, v2);
if ( (int)result <= 0 )
{
v4 = result;
ap_error_set("ap_log_hprintf()", 1LL);
result = v4;
}
return result;
}

original_code:
int ap_log_hprintf(int file_handle, char *fmt, ...)
{
    va_list vl;
    int blen,retcode;
    char buf[1024];
    __builtin_va_start(vl,fmt);
    blen = vsnprintf(buf, 1024, fmt, vl);
    retcode = write(file_handle, buf, blen);
    __builtin_va_end(vl);
    if ( retcode <= 0 )
     ap_error_set("ap_log_hprintf()", 1);
    return retcode;
}

canonical_original_code:
int ap_log_hprintf(int file_handle, char *fmt, ...)
{
    __builtin_va_list vl;
    int blen; int retcode;
    char buf[1024];
    __builtin_va_start(vl,fmt);
    blen = vsnprintf(buf, 1024, fmt, vl);
    retcode = write(file_handle, buf, blen);
    __builtin_va_end(vl);
    if ( retcode <= 0 )
     ap_error_set("ap_log_hprintf()", 1);
    return retcode;
}

variable_types: blen int


decompiled_code:
ssize_t __fastcall ap_log_hputs(void *buf, int fd)
{
size_t v2; // rax
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-20h]

v2 = strlen((const char *)buf);
result = write(fd, buf, v2);
if ( (int)result <= 0 )
{
v4 = result;
ap_error_set("ap_log_hprintf()", 1LL);
result = v4;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func11(void *buf, int fd)
{
size_t v2; // rax
ssize_t result; // rax
ssize_t v4; // [rsp+8h] [rbp-20h]

v2 = strlen((const char *)buf);
result = write(fd, buf, v2);
if ( (int)result <= 0 )
{
v4 = result;
ap_error_set("ap_log_hprintf()", 1LL);
result = v4;
}
return result;
}

original_code:
int ap_log_hputs(char *str, int file_handle)
{
    int retcode;
    retcode = write(file_handle, str, strlen(str));
    if ( retcode <= 0 )
     ap_error_set("ap_log_hprintf()", 1);
    return retcode;
}

variable_types:
str
char *


decompiled_code:
ssize_t __fastcall ap_log_hputc(char a1, int a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(a2, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func12(char a1, int a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(a2, buf, 1uLL);
}

original_code:
int ap_log_hputc(char c, int file_handle)
{
    return write(file_handle, &c, 1);
}

variable_types: c char


original_code:
void ap_log_mem_dump_to_fd(int file_handle, void *memory_area, int len)
{
    int i;
    int addr;
    int showaddr;
    int linelen;
    unsigned char *s;
    if (len == 0)
        return;
    showaddr = (len > 48);
    addr = 0;
    s = (unsigned char*)memory_area;
    for (; len > 0; len -= 16, addr += 16)
    {
        if (showaddr)
            ap_log_hprintf(file_handle, "%04x(%4d):  ", addr, addr);
        linelen = len > 16 ? 16 : len;
        for (i = 0; i < linelen; ++i)
            ap_log_hprintf(file_handle, "%02x %c ", s[i], ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISprint) ? s[i] : '.');
        ap_log_hputc('\n', file_handle);
        s += linelen;
    }
}

canonical_original_code:
void ap_log_mem_dump_to_fd(int file_handle, void *memory_area, int len)
{
    int i;
    int addr;
    int showaddr;
    int linelen;
    unsigned char *s;
    if (len == 0)
        return;
    showaddr = (len > 48);
    addr = 0;
    s = (unsigned char *)memory_area;
    for (; len > 0; len -= 16, addr += 16)
    {
        if (showaddr)
            ap_log_hprintf(file_handle, "%04x(%4d):  ", addr, addr);
        linelen = len > 16 ? 16 : len;
        for (i = 0; i < linelen; ++i)
            ap_log_hprintf(file_handle, "%02x %c ", s[i], ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISprint) ? s[i] : '.');
        ap_log_hputc('\n', file_handle);
        s += linelen;
    }
}

variable_types: len int

variable_types: addr int

variable_types: showaddr int

variable_types: linelen int

variable_types:
s
unsigned char *


decompiled_code:
void __fastcall ap_log_mem_dump(unsigned __int8 *a1, int a2)
{
int *v3; // rbp
int v4; // ebx
int v5; // edi
int v6; // eax

if ( ap_log_debug_to_tty )
{
v6 = fileno(stderr);
ap_log_mem_dump_to_fd(v6, a1, a2);
}
if ( debug_handles_count > 0 )
{
v3 = debug_handles;
v4 = 0;
do
{
v5 = *v3;
++v4;
v3 += 38;
ap_log_mem_dump_to_fd(v5, a1, a2);
}
while ( debug_handles_count > v4 );
}
}

canonical_decompiled_code:
void __fastcall func14(unsigned __int8 *a1, int a2)
{
int *v3; // rbp
int v4; // ebx
int v5; // edi
int v6; // eax

if ( ap_log_debug_to_tty )
{
v6 = fileno(stderr);
func13(v6, a1, a2);
}
if ( debug_handles_count > 0 )
{
v3 = debug_handles;
v4 = 0;
do
{
v5 = *v3;
++v4;
v3 += 38;
func13(v5, a1, a2);
}
while ( debug_handles_count > v4 );
}
}

original_code:
void ap_log_mem_dump(void *memory_area, int len)
{
    int i;
    if (ap_log_debug_to_tty)
        ap_log_mem_dump_to_fd(fileno(stderr), memory_area, len);
    for ( i = 0; i < debug_handles_count; ++i )
        ap_log_mem_dump_to_fd(debug_handles[i].fd, memory_area, len);
}


decompiled_code:
void __fastcall ap_log_mem_dump_bits_to_fd(int fd, unsigned __int8 *a2, int a3)
{
int i; // er14
int v5; // eax
unsigned __int8 *v6; // rbp
__int64 v7; // r13
int v8; // ebx
int v9; // er15
char v10; // di

if ( a3 > 0 )
{
for ( i = a3; i > 0; i -= 4 )
{
ap_log_hputs("\t", fd);
v5 = 4;
v6 = a2;
if ( i <= 4 )
v5 = i;
v7 = (__int64)&a2[v5 - 1 + 1];
do
{
v8 = 8;
v9 = 128;
ap_log_hprintf(fd, "0x%02x: ", *v6);
do
{
v10 = 48 - (((unsigned __int8)(v9 & *v6) == 0) - 1);
v9 >>= 1;
ap_log_hputc(v10, fd);
--v8;
}
while ( v8 );
++v6;
ap_log_hputs(" ", fd);
}
while ( (unsigned __int8 *)v7 != v6 );
ap_log_hputs("\n", fd);
}
}
}

original_code:
void ap_log_mem_dump_bits_to_fd(int file_handle, void *memory_area, int len)
{
    int i;
    int mask;
    int size;
    unsigned char *s;
    if (len == 0)
        return;
    s = (unsigned char*)memory_area;
    for (; len > 0; len -= 4)
    {
        ap_log_hputs("\t", file_handle);
        size = len > 4 ? 4 : len;
        for(i = 0; i < size; ++i)
        {
            ap_log_hprintf(file_handle, "0x%02x: ", s[i]);
            for (mask = 128; mask != 0; mask >>= 1)
                ap_log_hputc( s[i] & mask ? '1' : '0', file_handle );
            ap_log_hputs("   ", file_handle);
        }
        ap_log_hputs("\n", file_handle);
    }
}

canonical_original_code:
void ap_log_mem_dump_bits_to_fd(int file_handle, void *memory_area, int len)
{
    int i;
    int mask;
    int size;
    unsigned char *s;
    if (len == 0)
        return;
    s = (unsigned char *)memory_area;
    for (; len > 0; len -= 4)
    {
        ap_log_hputs("\t", file_handle);
        size = len > 4 ? 4 : len;
        for(i = 0; i < size; ++i)
        {
            ap_log_hprintf(file_handle, "0x%02x: ", s[i]);
            for (mask = 128; mask != 0; mask >>= 1)
                ap_log_hputc( s[i] & mask ? '1' : '0', file_handle );
            ap_log_hputs("   ", file_handle);
        }
        ap_log_hputs("\n", file_handle);
    }
}

variable_types: mask int

variable_types: size int


decompiled_code:
void __fastcall ap_log_mem_dump_bits(unsigned __int8 *a1, int a2)
{
int *v3; // rbp
int v4; // ebx
int v5; // edi
int v6; // eax

if ( ap_log_debug_to_tty )
{
v6 = fileno(stderr);
ap_log_mem_dump_bits_to_fd(v6, a1, a2);
}
if ( debug_handles_count > 0 )
{
v3 = debug_handles;
v4 = 0;
do
{
v5 = *v3;
++v4;
v3 += 38;
ap_log_mem_dump_bits_to_fd(v5, a1, a2);
}
while ( debug_handles_count > v4 );
}
}

canonical_decompiled_code:
void __fastcall func16(unsigned __int8 *a1, int a2)
{
int *v3; // rbp
int v4; // ebx
int v5; // edi
int v6; // eax

if ( ap_log_debug_to_tty )
{
v6 = fileno(stderr);
func15(v6, a1, a2);
}
if ( debug_handles_count > 0 )
{
v3 = debug_handles;
v4 = 0;
do
{
v5 = *v3;
++v4;
v3 += 38;
func15(v5, a1, a2);
}
while ( debug_handles_count > v4 );
}
}

original_code:
void ap_log_mem_dump_bits(void *memory_area, int len)
{
    int i;
    if (ap_log_debug_to_tty)
        ap_log_mem_dump_bits_to_fd(fileno(stderr), memory_area, len);
    for ( i = 0; i < debug_handles_count; ++i )
        ap_log_mem_dump_bits_to_fd(debug_handles[i].fd, memory_area, len);
}

variable_types: i int


decompiled_code:
__int64 __fastcall ap_net_conn_pool_check_state_sel(__int64 a1, int a2)
{
int v2; // er14
__int64 v3; // rbp
__int64 v4; // rbx
int v5; // er12
bool v6; // zf
int buf; // [rsp+Ch] [rbp-1BCh] BYREF
struct timeval timeout; // [rsp+10h] [rbp-1B8h] BYREF
fd_set readfds; // [rsp+20h] [rbp-1A8h] BYREF
fd_set writefds; // [rsp+A0h] [rbp-128h] BYREF
fd_set exceptfds; // [rsp+120h] [rbp-A8h] BYREF

ap_error_clear();
if ( a2 < 0 || *(_DWORD *)(a1 + 8) <= a2 || (v2 = *(_DWORD *)(*(_QWORD *)a1 + 144LL * a2 + 12), v2 <= 0) )
{
v5 = -1;
}
else
{
buf = send(v2, &buf, 0LL, 0);
if ( buf == -1 && *_errno_location() == 32 )
{
v5 = 4;
if ( ap_log_debug_level > 10 )
ap_log_debug_log(
"- ap_net_check_state(%d): send(%d): %d/%m\n",
(unsigned int)a2,
(unsigned int)v2,
0xFFFFFFFFLL);
}
else
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
memset(&exceptfds, 0, sizeof(exceptfds));
v3 = 1LL << v2;
v4 = v2 >> 6;
buf = v2 + 1;
readfds.fds_bits[v4] |= 1LL << v2;
writefds.fds_bits[v4] |= 1LL << v2;
exceptfds.fds_bits[v4] |= 1LL << v2;
timeout.tv_usec = 0LL;
timeout.tv_sec = 0LL;
buf = select(v2 + 1, &readfds, &writefds, &exceptfds, &timeout);
v5 = buf;
if ( ap_log_debug_level > 10 )
{
ap_log_debug_log("- ap_net_check_state(%d): select() error: %m\n", (unsigned int)a2);
v5 = buf;
}
if ( v5 > 0 )
{
v6 = (readfds.fds_bits[v4] & v3) == 0;
buf = 0;
v5 = 0;
if ( !v6 )
{
buf = 1;
v5 = 1;
}
if ( (writefds.fds_bits[v4] & v3) != 0 )
{
v5 |= 2u;
buf = v5;
}
if ( (exceptfds.fds_bits[v4] & v3) != 0 )
{
v5 |= 4u;
buf = v5;
}
if ( ap_log_debug_level > 10 )
{
ap_log_debug_log("- ap_net_check_state(%d): return %d\n", (unsigned int)a2, (unsigned int)v5);
v5 = buf;
}
}
}
}
return (unsigned int)v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
int v2; // er14
__int64 v3; // rbp
__int64 v4; // rbx
int v5; // er12
bool v6; // zf
int buf; // [rsp+Ch] [rbp-1BCh] BYREF
struct timeval timeout; // [rsp+10h] [rbp-1B8h] BYREF
fd_set readfds; // [rsp+20h] [rbp-1A8h] BYREF
fd_set writefds; // [rsp+A0h] [rbp-128h] BYREF
fd_set exceptfds; // [rsp+120h] [rbp-A8h] BYREF

ap_error_clear();
if ( a2 < 0 || *(_DWORD *)(a1 + 8) <= a2 || (v2 = *(_DWORD *)(*(_QWORD *)a1 + 144LL * a2 + 12), v2 <= 0) )
{
v5 = -1;
}
else
{
buf = send(v2, &buf, 0LL, 0);
if ( buf == -1 && *_errno_location() == 32 )
{
v5 = 4;
if ( ap_log_debug_level > 10 )
ap_log_debug_log(
"- ap_net_check_state(%d): send(%d): %d/%m\n",
(unsigned int)a2,
(unsigned int)v2,
0xFFFFFFFFLL);
}
else
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
memset(&exceptfds, 0, sizeof(exceptfds));
v3 = 1LL << v2;
v4 = v2 >> 6;
buf = v2 + 1;
readfds.fds_bits[v4] |= 1LL << v2;
writefds.fds_bits[v4] |= 1LL << v2;
exceptfds.fds_bits[v4] |= 1LL << v2;
timeout.tv_usec = 0LL;
timeout.tv_sec = 0LL;
buf = select(v2 + 1, &readfds, &writefds, &exceptfds, &timeout);
v5 = buf;
if ( ap_log_debug_level > 10 )
{
ap_log_debug_log("- ap_net_check_state(%d): select() error: %m\n", (unsigned int)a2);
v5 = buf;
}
if ( v5 > 0 )
{
v6 = (readfds.fds_bits[v4] & v3) == 0;
buf = 0;
v5 = 0;
if ( !v6 )
{
buf = 1;
v5 = 1;
}
if ( (writefds.fds_bits[v4] & v3) != 0 )
{
v5 |= 2u;
buf = v5;
}
if ( (exceptfds.fds_bits[v4] & v3) != 0 )
{
v5 |= 4u;
buf = v5;
}
if ( ap_log_debug_level > 10 )
{
ap_log_debug_log("- ap_net_check_state(%d): return %d\n", (unsigned int)a2, (unsigned int)v5);
v5 = buf;
}
}
}
}
return (unsigned int)v5;
}

original_code:
int ap_net_conn_pool_check_state_sel(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    struct timeval tv;
    fd_set fdr, fdw, fde;
    int n, fd;
    ap_error_clear();
    if ( conn_idx < 0 || conn_idx >= pool->max_connections )
      return -1;
    fd = pool->conns[conn_idx].fd;
    if( fd <= 0 )
        return -1;
    n = send(fd, &n, 0, 0);
    if ( n == -1 && (*__errno_location ()) == 32 )
    {
        if (ap_log_debug_level > 10)
            ap_log_debug_log("- ap_net_check_state(%d): send(%d): %d/%m\n", conn_idx, fd, n);
        return 4;
    }
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdr)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdw)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fde)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&fdr)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fdw)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fde)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    n = fd + 1;
    tv.tv_sec = tv.tv_usec = 0;
    n = select(n, &fdr, &fdw, &fde, &tv);
    if ( ap_log_debug_level > 10 )
        ap_log_debug_log("- ap_net_check_state(%d): select() error: %m\n", conn_idx);
    if ( n <= 0 ) return n;
    n = 0;
    if ( ((((&fdr)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0) )
        n = 1;
    if ( ((((&fdw)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0) )
        n |= 2;
    if ( ((((&fde)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0) )
        n |= 4;
    if ( ap_log_debug_level > 10 )
        ap_log_debug_log("- ap_net_check_state(%d): return %d\n", conn_idx, n);
    return n;
}

canonical_original_code:
int ap_net_conn_pool_check_state_sel(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    struct timeval tv;
    struct fd_set fdr; struct fd_set fdw; struct fd_set fde;
    int n; int fd;
    ap_error_clear();
    if ( conn_idx < 0 || conn_idx >= pool->max_connections )
      return -1;
    fd = pool->conns[conn_idx].fd;
    if( fd <= 0 )
        return -1;
    n = send(fd, &n, 0, 0);
    if ( n == -1 && (*__errno_location ()) == 32 )
    {
        if (ap_log_debug_level > 10)
            ap_log_debug_log("- ap_net_check_state(%d): send(%d): %d/%m\n", conn_idx, fd, n);
        return 4;
    }
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdr)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdw)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fde)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&fdr)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fdw)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fde)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    n = fd + 1;
    tv.tv_sec = tv.tv_usec = 0;
    n = select(n, &fdr, &fdw, &fde, &tv);
    if ( ap_log_debug_level > 10 )
        ap_log_debug_log("- ap_net_check_state(%d): select() error: %m\n", conn_idx);
    if ( n <= 0 ) return n;
    n = 0;
    if ( ((((&fdr)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0) )
        n = 1;
    if ( ((((&fdw)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0) )
        n |= 2;
    if ( ((((&fde)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0) )
        n |= 4;
    if ( ap_log_debug_level > 10 )
        ap_log_debug_log("- ap_net_check_state(%d): return %d\n", conn_idx, n);
    return n;
}

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; } *

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

variable_types:
fdr
struct fd_set { long __fds_bits[-1]; }

variable_types:
fdw
struct fd_set { long __fds_bits[-1]; }

variable_types:
fde
struct fd_set { long __fds_bits[-1]; }

variable_types: n int

variable_types: fd int

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 sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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 epoll_event { unsigned int events; union epoll_data data; }


decompiled_code:
__int64 __fastcall ap_net_poller_single_fd(int fd)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v1 = epoll_create(1);
if ( v1 == -1 )
goto LABEL_9;
event.data.fd = fd;
v2 = v1;
event.events = 5;
v3 = epoll_ctl(v1, 1, fd, &event);
if ( v3 == -1 )
{
ap_error_set_detailed("ap_net_poller_single_fd()", 1LL, "epoll_ctl() add listen_socket_fd");
close(v2);
return v3;
}
if ( epoll_wait(v2, &event, 1, 0) == -1 )
{
LABEL_9:
v3 = -1;
ap_error_set("ap_net_poller_single_fd()", 1LL);
}
else
{
close(v2);
v3 = 4 * (event.events & 1);
if ( (event.events & 4) != 0 )
v3 = (4 * (event.events & 1)) | 8;
if ( (event.events & 0x18) != 0 )
v3 |= 2u;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v1 = epoll_create(1);
if ( v1 == -1 )
goto LABEL_9;
event.data.fd = fd;
v2 = v1;
event.events = 5;
v3 = epoll_ctl(v1, 1, fd, &event);
if ( v3 == -1 )
{
ap_error_set_detailed("ap_net_poller_single_fd()", 1LL, "epoll_ctl() add listen_socket_fd");
close(v2);
return v3;
}
if ( epoll_wait(v2, &event, 1, 0) == -1 )
{
LABEL_9:
v3 = -1;
ap_error_set("ap_net_poller_single_fd()", 1LL);
}
else
{
close(v2);
v3 = 4 * (event.events & 1);
if ( (event.events & 4) != 0 )
v3 = (4 * (event.events & 1)) | 8;
if ( (event.events & 0x18) != 0 )
v3 |= 2u;
}
return v3;
}

original_code:
int ap_net_poller_single_fd(int fd)
{
    int poll_status;
    int epoll_fd;
    struct epoll_event ev;
    epoll_fd = epoll_create(1);
    if (epoll_fd == -1)
    {
        ap_error_set(_func_name, 1);
        return -1;
    }
    ev.events = EPOLLIN | EPOLLOUT;
    ev.data.fd = fd;
    if (epoll_ctl(epoll_fd, 1, fd, &ev) == -1)
    {
        ap_error_set_detailed(_func_name, 1, "epoll_ctl() add listen_socket_fd");
        close(epoll_fd);
        return -1;
    }
    poll_status = epoll_wait(epoll_fd, &ev, 1, 0);
    if (poll_status == -1)
    {
        ap_error_set(_func_name, 1);
        return -1;
    }
    close(epoll_fd);
    poll_status = 0;
    if ( ev.events & EPOLLIN )
     poll_status |= 4;
    if ( ev.events & EPOLLOUT )
     poll_status |= 8;
    if ( ev.events & (EPOLLERR | EPOLLHUP ) )
     poll_status |= 2;
    return poll_status;
}

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }


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

v2 = 0;
v3 = *a1 + 144LL * (int)a2;
if ( (*(_BYTE *)(v3 + 132) & 2) == 0 )
return v2;
if ( (unsigned int)ap_utils_timespec_is_set(v3 + 96) && (int)ap_utils_timespec_cmp_to_now(v3 + 96) >= 0 )
{
ap_net_conn_pool_close_connection(a1, a2);
return v2;
}
v2 = 1;
v5 = ap_net_poller_single_fd(*(unsigned int *)(v3 + 12));
if ( v5 == -1 )
return v2;
v6 = 2 - (((v5 & 0x18) == 0) - 1);
if ( (v5 & 1) != 0 )
v6 = (2 - (((v5 & 0x18) == 0) - 1)) | 8;
if ( (v5 & 4) != 0 )
v6 |= 0x10u;
return v6;
}

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

v2 = 0;
v3 = *a1 + 144LL * (int)a2;
if ( (*(_BYTE *)(v3 + 132) & 2) == 0 )
return v2;
if ( (unsigned int)ap_utils_timespec_is_set(v3 + 96) && (int)ap_utils_timespec_cmp_to_now(v3 + 96) >= 0 )
{
ap_net_conn_pool_close_connection(a1, a2);
return v2;
}
v2 = 1;
v5 = ap_net_poller_single_fd(*(unsigned int *)(v3 + 12));
if ( v5 == -1 )
return v2;
v6 = 2 - (((v5 & 0x18) == 0) - 1);
if ( (v5 & 1) != 0 )
v6 = (2 - (((v5 & 0x18) == 0) - 1)) | 8;
if ( (v5 & 4) != 0 )
v6 |= 0x10u;
return v6;
}

original_code:
int ap_net_conn_pool_connection_is_alive(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    struct ap_net_connection_t *conn;
    int events;
    int status;
    conn = &pool->conns[conn_idx];
    if ( ! ( conn->state & 2 ) )
        return 0;
    if ( ap_utils_timespec_is_set( &conn->expire ) && 0 <= ap_utils_timespec_cmp_to_now(&conn->expire) )
    {
       ap_net_conn_pool_close_connection(pool, conn_idx);
       return 0;
    }
    events = ap_net_poller_single_fd(conn->fd);
    if (events == -1)
        return 1;
    status = 2;
    if ( events & (EPOLLERR | EPOLLHUP ) )
        status |= 1;
    if ( events & EPOLLIN )
        status |= 8;
    if ( events & EPOLLOUT )
        status |= 16;
    return status;
}

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; } *

variable_types: events int

variable_types: status int


decompiled_code:
__int64 __fastcall ap_net_conn_pool_poll(__int64 a1)
{
__int64 v1; // rbp
__int64 v3; // r12
__int64 v4; // rsi
int v5; // eax
int v6; // esi
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rcx
__int64 v10; // rsi
__int64 v11; // rbp
__int64 v12; // rax
__int64 v13; // r13
int v14; // eax
int v15; // edx
int v16; // eax
void (__fastcall *v17)(__int64, __int64); // rax
void (__fastcall *v18)(__int64, __int64); // rax
__int64 v19; // rax
int v20; // edi
__int64 i; // r14
void (__fastcall *v22)(_DWORD *, __int64); // rax
_DWORD *v23; // rbp
void (__fastcall *v24)(_DWORD *, __int64); // rax
__int64 v25; // rax
__int64 v26; // rsi
__int64 v27; // rax
__int64 v28; // rbp
int v29; // edx
const void *v31; // rsi
unsigned int v32; // er15
unsigned int v33; // er14
unsigned __int16 v34; // ax
const void *v35; // rdx
socklen_t v36; // ecx
int v37; // edi
unsigned int v38; // er13
const char *v39; // rax
void (__fastcall *v40)(__int64, __int64); // rax
unsigned int v41; // [rsp+Ch] [rbp-BCh]
struct epoll_event event; // [rsp+14h] [rbp-B4h] BYREF
char buf[168]; // [rsp+20h] [rbp-A8h] BYREF

v1 = 0LL;
ap_error_clear();
v3 = *(_QWORD *)(a1 + 24);
if ( *(int *)(a1 + 8) > 0 )
{
do
{
while ( (*(_BYTE *)(*(_QWORD *)a1 + 144 * v1 + 132) & 0x40) == 0 )
{
if ( *(_DWORD *)(a1 + 8) <= (int)++v1 )
goto LABEL_6;
}
v4 = (unsigned int)v1++;
ap_net_conn_pool_close_connection(a1, v4);
}
while ( *(_DWORD *)(a1 + 8) > (int)v1 );
}
LABEL_6:
v5 = epoll_wait(*(_DWORD *)(v3 + 28), *(struct epoll_event **)(v3 + 8), *(_DWORD *)(v3 + 16), 0);
*(_DWORD *)(v3 + 24) = v5;
v6 = v5;
if ( v5 == -1 )
{
ap_error_set_detailed("ap_net_conn_pool_poll()", 1LL, "epoll_wait()");
return 0LL;
}
if ( *(_DWORD *)(v3 + 40) && v5 > 0 )
{
ap_log_debug_log("---P-EVTCNT %d\n", (unsigned int)v5);
v6 = *(_DWORD *)(v3 + 24);
}
v7 = *(_DWORD *)(a1 + 48);
if ( v7 != -1 )
{
if ( v6 <= 0 )
{
LABEL_36:
for ( i = 0LL; *(_DWORD *)(a1 + 8) > (int)i; ++i )
{
v23 = (_DWORD *)(*(_QWORD *)a1 + 144 * i);
if ( (v23[33] & 2) != 0 )
{
if ( (unsigned int)ap_utils_timespec_is_set(v23 + 24) && (int)ap_utils_timespec_cmp_to_now(v23 + 24) <= 0 )
{
v24 = *(void (__fastcall **)(_DWORD *, __int64))(a1 + 80);
v23[33] |= 0x20u;
if ( v24 )
v24(v23, 9LL);
ap_net_conn_pool_close_connection(a1, (unsigned int)i);
if ( *(_DWORD *)(v3 + 40) )
{
v25 = ap_utils_timespec_elapsed(v23 + 24, 0LL, 0LL);
ap_log_debug_log("\t-PEXPIRED %d %ld ms\n", (unsigned int)i, v25);
}
}
if ( *(_DWORD *)(v3 + 44) && v23[32] - v23[31] > 0 )
{
v22 = *(void (__fastcall **)(_DWORD *, __int64))(a1 + 80);
if ( v22 )
v22(v23, 10LL);
}
}
}
return 1LL;
}
v8 = *(_QWORD *)(v3 + 8);
v9 = v8;
v10 = v8 + 12LL * (unsigned int)(v6 - 1) + 12;
while ( v7 != *(_DWORD *)(v9 + 4) )
{
v9 += 12LL;
if ( v10 == v9 )
goto LABEL_15;
}
if ( (*(_BYTE *)v9 & 0x18) != 0 )
{
ap_error_set_detailed("ap_net_conn_pool_poll()", 2LL, "epoll reports error/hangup on listener socket");
return 0LL;
}
v27 = ap_net_conn_pool_accept_connection(a1, v10);
v28 = v27;
if ( !v27 )
{
if ( (unsigned int)ap_error_get() != 8 )
return 0LL;
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log("\t-P-NOACCEPT - denied by callback\n");
goto LABEL_55;
}
if ( !*(_DWORD *)(v3 + 40) )
{
LABEL_55:
v6 = *(_DWORD *)(v3 + 24);
goto LABEL_56;
}
if ( (*(_BYTE *)(v27 + 16) & 1) != 0 )
{
v31 = (const void *)(v27 + 56);
v32 = *(_DWORD *)(v27 + 128);
v33 = *(_DWORD *)(v27 + 124);
v41 = *(_DWORD *)(v27 + 120);
v34 = _bswap_16(*(_WORD *)(v27 + 50));
v37 = *(unsigned __int16 *)(v28 + 48);
v38 = v34;
if ( (_WORD)v37 == 2 )
v31 = v35;
v39 = inet_ntop(v37, v31, buf, v36);
ap_log_debug_log(
"\t-P-DataIn_UDP %d %s @ %d (p:%d f:%d s:%d)\n",
*(unsigned int *)(v28 + 8),
v39,
v38,
v33,
v32,
v41);
v6 = *(_DWORD *)(v3 + 24);
}
else
{
ap_log_debug_log("\t-P-ACCEPT %d\n", *(unsigned int *)(v27 + 8));
v6 = *(_DWORD *)(v3 + 24);
}
}
LABEL_56:
if ( v6 <= 0 )
goto LABEL_36;
v8 = *(_QWORD *)(v3 + 8);
v7 = *(_DWORD *)(a1 + 48);
LABEL_15:
v11 = 0LL;
while ( 1 )
{
if ( *(_DWORD *)(v8 + 12 * v11 + 4) == v7 )
goto LABEL_32;
v12 = ap_net_conn_pool_get_conn_by_fd(a1);
v13 = v12;
if ( v12 )
{
v14 = *(_DWORD *)(v12 + 132);
if ( (v14 & 2) != 0 )
break;
}
v19 = *(_QWORD *)(v3 + 8);
v20 = *(_DWORD *)(v3 + 28);
event.events = 1;
event.data.fd = *(_DWORD *)(v19 + 12 * v11 + 4);
epoll_ctl(v20, 2, event.data.fd, &event);
if ( !*(_DWORD *)(v3 + 40) )
goto LABEL_32;
++v11;
ap_log_debug_log("\t-P-FDERR\n");
if ( *(_DWORD *)(v3 + 24) <= (int)v11 )
goto LABEL_36;
LABEL_33:
v8 = *(_QWORD *)(v3 + 8);
v7 = *(_DWORD *)(a1 + 48);
}
v15 = *(_DWORD *)(*(_QWORD *)(v3 + 8) + 12 * v11);
if ( (v15 & 0x18) != 0 )
{
v26 = *(unsigned int *)(v13 + 8);
*(_DWORD *)(v13 + 132) = v14 | 1;
ap_net_conn_pool_close_connection(a1, v26);
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log("\t-P-ERR %d\n", *(unsigned int *)(v13 + 8));
LABEL_32:
if ( *(_DWORD *)(v3 + 24) <= (int)++v11 )
goto LABEL_36;
goto LABEL_33;
}
if ( (v15 & 1) == 0 )
{
LABEL_28:
if ( (*(_BYTE *)(a1 + 16) & 2) != 0 && (*(_BYTE *)(*(_QWORD *)(v3 + 8) + 12 * v11) & 4) != 0 )
{
v18 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v18 )
v18(v13, 8LL);
}
goto LABEL_32;
}
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log(
"\t-P-DATAIN %d(p:%d f:%d s:%d)",
*(unsigned int *)(v13 + 8),
*(unsigned int *)(v13 + 124),
*(unsigned int *)(v13 + 128),
*(unsigned int *)(v13 + 120));
v16 = ap_net_conn_pool_recv(a1, *(unsigned int *)(v13 + 8));
if ( v16 > 0 )
{
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log(
" > (p:%d f:%d s:%d)\n",
*(unsigned int *)(v13 + 124),
*(unsigned int *)(v13 + 128),
*(unsigned int *)(v13 + 120));
v17 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v17 )
v17(v13, 7LL);
goto LABEL_28;
}
v29 = *(_DWORD *)(v3 + 40);
if ( !v16 )
{
if ( v29 )
ap_log_debug_log(" -P- buffer full --\n");
goto LABEL_28;
}
if ( v16 == -2 )
{
if ( v29 )
ap_log_debug_log("\t-P- Disconnect %d --\n", *(unsigned int *)(v13 + 8));
ap_net_conn_pool_poller_remove_conn(*(_QWORD *)v13, *(unsigned int *)(v13 + 8));
*(_DWORD *)(v13 + 132) |= 0x40u;
if ( !(unsigned int)ap_utils_timespec_is_set(v13 + 96) )
ap_utils_timespec_set(v13 + 96, 2LL, 2000LL);
if ( *(_DWORD *)(v13 + 128) - *(_DWORD *)(v13 + 124) > 0 )
{
v40 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v40 )
v40(v13, 10LL);
}
goto LABEL_32;
}
if ( !v29 )
return 0LL;
ap_log_debug_log("\t-P- ERROR %d --\n", *(unsigned int *)(v13 + 8));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rbp
__int64 v3; // r12
__int64 v4; // rsi
int v5; // eax
int v6; // esi
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rcx
__int64 v10; // rsi
__int64 v11; // rbp
__int64 v12; // rax
__int64 v13; // r13
int v14; // eax
int v15; // edx
int v16; // eax
void (__fastcall *v17)(__int64, __int64); // rax
void (__fastcall *v18)(__int64, __int64); // rax
__int64 v19; // rax
int v20; // edi
__int64 i; // r14
void (__fastcall *v22)(_DWORD *, __int64); // rax
_DWORD *v23; // rbp
void (__fastcall *v24)(_DWORD *, __int64); // rax
__int64 v25; // rax
__int64 v26; // rsi
__int64 v27; // rax
__int64 v28; // rbp
int v29; // edx
const void *v31; // rsi
unsigned int v32; // er15
unsigned int v33; // er14
unsigned __int16 v34; // ax
const void *v35; // rdx
socklen_t v36; // ecx
int v37; // edi
unsigned int v38; // er13
const char *v39; // rax
void (__fastcall *v40)(__int64, __int64); // rax
unsigned int v41; // [rsp+Ch] [rbp-BCh]
struct epoll_event event; // [rsp+14h] [rbp-B4h] BYREF
char buf[168]; // [rsp+20h] [rbp-A8h] BYREF

v1 = 0LL;
ap_error_clear();
v3 = *(_QWORD *)(a1 + 24);
if ( *(int *)(a1 + 8) > 0 )
{
do
{
while ( (*(_BYTE *)(*(_QWORD *)a1 + 144 * v1 + 132) & 0x40) == 0 )
{
if ( *(_DWORD *)(a1 + 8) <= (int)++v1 )
goto LABEL_6;
}
v4 = (unsigned int)v1++;
ap_net_conn_pool_close_connection(a1, v4);
}
while ( *(_DWORD *)(a1 + 8) > (int)v1 );
}
LABEL_6:
v5 = epoll_wait(*(_DWORD *)(v3 + 28), *(struct epoll_event **)(v3 + 8), *(_DWORD *)(v3 + 16), 0);
*(_DWORD *)(v3 + 24) = v5;
v6 = v5;
if ( v5 == -1 )
{
ap_error_set_detailed("ap_net_conn_pool_poll()", 1LL, "epoll_wait()");
return 0LL;
}
if ( *(_DWORD *)(v3 + 40) && v5 > 0 )
{
ap_log_debug_log("---P-EVTCNT %d\n", (unsigned int)v5);
v6 = *(_DWORD *)(v3 + 24);
}
v7 = *(_DWORD *)(a1 + 48);
if ( v7 != -1 )
{
if ( v6 <= 0 )
{
LABEL_36:
for ( i = 0LL; *(_DWORD *)(a1 + 8) > (int)i; ++i )
{
v23 = (_DWORD *)(*(_QWORD *)a1 + 144 * i);
if ( (v23[33] & 2) != 0 )
{
if ( (unsigned int)ap_utils_timespec_is_set(v23 + 24) && (int)ap_utils_timespec_cmp_to_now(v23 + 24) <= 0 )
{
v24 = *(void (__fastcall **)(_DWORD *, __int64))(a1 + 80);
v23[33] |= 0x20u;
if ( v24 )
v24(v23, 9LL);
ap_net_conn_pool_close_connection(a1, (unsigned int)i);
if ( *(_DWORD *)(v3 + 40) )
{
v25 = ap_utils_timespec_elapsed(v23 + 24, 0LL, 0LL);
ap_log_debug_log("\t-PEXPIRED %d %ld ms\n", (unsigned int)i, v25);
}
}
if ( *(_DWORD *)(v3 + 44) && v23[32] - v23[31] > 0 )
{
v22 = *(void (__fastcall **)(_DWORD *, __int64))(a1 + 80);
if ( v22 )
v22(v23, 10LL);
}
}
}
return 1LL;
}
v8 = *(_QWORD *)(v3 + 8);
v9 = v8;
v10 = v8 + 12LL * (unsigned int)(v6 - 1) + 12;
while ( v7 != *(_DWORD *)(v9 + 4) )
{
v9 += 12LL;
if ( v10 == v9 )
goto LABEL_15;
}
if ( (*(_BYTE *)v9 & 0x18) != 0 )
{
ap_error_set_detailed("ap_net_conn_pool_poll()", 2LL, "epoll reports error/hangup on listener socket");
return 0LL;
}
v27 = ap_net_conn_pool_accept_connection(a1, v10);
v28 = v27;
if ( !v27 )
{
if ( (unsigned int)ap_error_get() != 8 )
return 0LL;
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log("\t-P-NOACCEPT - denied by callback\n");
goto LABEL_55;
}
if ( !*(_DWORD *)(v3 + 40) )
{
LABEL_55:
v6 = *(_DWORD *)(v3 + 24);
goto LABEL_56;
}
if ( (*(_BYTE *)(v27 + 16) & 1) != 0 )
{
v31 = (const void *)(v27 + 56);
v32 = *(_DWORD *)(v27 + 128);
v33 = *(_DWORD *)(v27 + 124);
v41 = *(_DWORD *)(v27 + 120);
v34 = func0(*(_WORD *)(v27 + 50));
v37 = *(unsigned __int16 *)(v28 + 48);
v38 = v34;
if ( (_WORD)v37 == 2 )
v31 = v35;
v39 = inet_ntop(v37, v31, buf, v36);
ap_log_debug_log(
"\t-P-DataIn_UDP %d %s @ %d (p:%d f:%d s:%d)\n",
*(unsigned int *)(v28 + 8),
v39,
v38,
v33,
v32,
v41);
v6 = *(_DWORD *)(v3 + 24);
}
else
{
ap_log_debug_log("\t-P-ACCEPT %d\n", *(unsigned int *)(v27 + 8));
v6 = *(_DWORD *)(v3 + 24);
}
}
LABEL_56:
if ( v6 <= 0 )
goto LABEL_36;
v8 = *(_QWORD *)(v3 + 8);
v7 = *(_DWORD *)(a1 + 48);
LABEL_15:
v11 = 0LL;
while ( 1 )
{
if ( *(_DWORD *)(v8 + 12 * v11 + 4) == v7 )
goto LABEL_32;
v12 = ap_net_conn_pool_get_conn_by_fd(a1);
v13 = v12;
if ( v12 )
{
v14 = *(_DWORD *)(v12 + 132);
if ( (v14 & 2) != 0 )
break;
}
v19 = *(_QWORD *)(v3 + 8);
v20 = *(_DWORD *)(v3 + 28);
event.events = 1;
event.data.fd = *(_DWORD *)(v19 + 12 * v11 + 4);
epoll_ctl(v20, 2, event.data.fd, &event);
if ( !*(_DWORD *)(v3 + 40) )
goto LABEL_32;
++v11;
ap_log_debug_log("\t-P-FDERR\n");
if ( *(_DWORD *)(v3 + 24) <= (int)v11 )
goto LABEL_36;
LABEL_33:
v8 = *(_QWORD *)(v3 + 8);
v7 = *(_DWORD *)(a1 + 48);
}
v15 = *(_DWORD *)(*(_QWORD *)(v3 + 8) + 12 * v11);
if ( (v15 & 0x18) != 0 )
{
v26 = *(unsigned int *)(v13 + 8);
*(_DWORD *)(v13 + 132) = v14 | 1;
ap_net_conn_pool_close_connection(a1, v26);
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log("\t-P-ERR %d\n", *(unsigned int *)(v13 + 8));
LABEL_32:
if ( *(_DWORD *)(v3 + 24) <= (int)++v11 )
goto LABEL_36;
goto LABEL_33;
}
if ( (v15 & 1) == 0 )
{
LABEL_28:
if ( (*(_BYTE *)(a1 + 16) & 2) != 0 && (*(_BYTE *)(*(_QWORD *)(v3 + 8) + 12 * v11) & 4) != 0 )
{
v18 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v18 )
v18(v13, 8LL);
}
goto LABEL_32;
}
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log(
"\t-P-DATAIN %d(p:%d f:%d s:%d)",
*(unsigned int *)(v13 + 8),
*(unsigned int *)(v13 + 124),
*(unsigned int *)(v13 + 128),
*(unsigned int *)(v13 + 120));
v16 = ap_net_conn_pool_recv(a1, *(unsigned int *)(v13 + 8));
if ( v16 > 0 )
{
if ( *(_DWORD *)(v3 + 40) )
ap_log_debug_log(
" > (p:%d f:%d s:%d)\n",
*(unsigned int *)(v13 + 124),
*(unsigned int *)(v13 + 128),
*(unsigned int *)(v13 + 120));
v17 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v17 )
v17(v13, 7LL);
goto LABEL_28;
}
v29 = *(_DWORD *)(v3 + 40);
if ( !v16 )
{
if ( v29 )
ap_log_debug_log(" -P- buffer full --\n");
goto LABEL_28;
}
if ( v16 == -2 )
{
if ( v29 )
ap_log_debug_log("\t-P- Disconnect %d --\n", *(unsigned int *)(v13 + 8));
ap_net_conn_pool_poller_remove_conn(*(_QWORD *)v13, *(unsigned int *)(v13 + 8));
*(_DWORD *)(v13 + 132) |= 0x40u;
if ( !(unsigned int)ap_utils_timespec_is_set(v13 + 96) )
ap_utils_timespec_set(v13 + 96, 2LL, 2000LL);
if ( *(_DWORD *)(v13 + 128) - *(_DWORD *)(v13 + 124) > 0 )
{
v40 = *(void (__fastcall **)(__int64, __int64))(a1 + 80);
if ( v40 )
v40(v13, 10LL);
}
goto LABEL_32;
}
if ( !v29 )
return 0LL;
ap_log_debug_log("\t-P- ERROR %d --\n", *(unsigned int *)(v13 + 8));
return 0LL;
}

original_code:
int ap_net_conn_pool_poll(struct ap_net_conn_pool_t *pool)
{
    int event_idx;
    int i;
    int n;
    struct epoll_event ev;
    struct ap_net_poll_t *poller;
    struct ap_net_connection_t *conn;
    char buf[100];
    ap_error_clear();
    poller = pool->poller;
    for (i = 0; i < pool->max_connections; ++i )
    {
     conn = &pool->conns[i];
     if ( (((conn->state) & (64)) != 0) )
     {
            ap_net_conn_pool_close_connection(pool, i);
            continue;
     }
    }
    poller->events_count = epoll_wait(poller->epoll_fd, poller->events, poller->max_events, 0);
    if (poller->events_count == -1)
    {
        ap_error_set_detailed(_func_name, 1, "epoll_wait()");
        return 0;
    }
    if( poller->debug && poller->events_count > 0 )
     ap_log_debug_log("---P-EVTCNT %d\n", poller->events_count);
    if ( pool->listener.sock != -1 )
    {
        for ( event_idx = 0; event_idx < poller->events_count; ++event_idx)
        {
            if ( poller->events[event_idx].data.fd != pool->listener.sock)
             continue;
            if ( (((poller->events[event_idx].events) & ((EPOLLERR | EPOLLHUP))) != 0) )
            {
                ap_error_set_detailed(_func_name, 2, "epoll reports error/hangup on listener socket");
                return 0;
            }
            conn = ap_net_conn_pool_accept_connection(pool);
            if ( conn == ((void *)0) )
            {
             if ( ap_error_get() == 8 )
             {
                    if( poller->debug )
                     ap_log_debug_log("\t-P-NOACCEPT - denied by callback\n");
              break;
             }
                return 0;
            }
            if( poller->debug )
            {
             if ( (((conn->flags) & (1)) != 0) )
                 ap_log_debug_log("\t-P-DataIn_UDP %d %s @ %d (p:%d f:%d s:%d)\n", conn->idx,
                   inet_ntop(conn->remote.af, (conn->remote.af == 2 ? (void*)&conn->remote.addr4.sin_addr : (void*)&conn->remote.addr6.sin6_addr), buf, 100),
                   ntohs(conn->remote.addr4.sin_port), conn->bufpos, conn->buffill, conn->bufsize);
             else
              ap_log_debug_log("\t-P-ACCEPT %d\n", conn->idx);
            }
            break;
        }
    }
    for ( event_idx = 0; event_idx < poller->events_count; ++event_idx)
    {
        if ( poller->events[event_idx].data.fd == pool->listener.sock)
         continue;
        conn = ap_net_conn_pool_get_conn_by_fd(pool, poller->events[event_idx].data.fd);
        if ( conn == ((void *)0) || ! ((((conn->state) & (2)) != 0)) )
        {
            ev.events = EPOLLIN;
            ev.data.fd = poller->events[event_idx].data.fd;
            epoll_ctl(poller->epoll_fd, 2, ev.data.fd, &ev);
            if( poller->debug )
             ap_log_debug_log("\t-P-FDERR\n");
            continue;
        }
        if ( (((poller->events[event_idx].events) & ((EPOLLERR | EPOLLHUP))) != 0) )
        {
            conn->state |= 1;
            ap_net_conn_pool_close_connection(pool, conn->idx);
            if( poller->debug ) ap_log_debug_log("\t-P-ERR %d\n", conn->idx);
            continue;
        }
        if ( (((poller->events[event_idx].events) & (EPOLLIN)) != 0) )
        {
          if( poller->debug)
           ap_log_debug_log("\t-P-DATAIN %d(p:%d f:%d s:%d)", conn->idx, conn->bufpos, conn->buffill, conn->bufsize);
             n = ap_net_conn_pool_recv(pool, conn->idx);
             if ( n > 0 )
             {
              if( poller->debug)
               ap_log_debug_log(" > (p:%d f:%d s:%d)\n", conn->bufpos, conn->buffill, conn->bufsize);
              if ( pool->callback_func != ((void *)0) )
               pool->callback_func(conn, 7);
             }
             else if ( n == 0 )
             {
              if( poller->debug )
               ap_log_debug_log(" -P- buffer full --\n");
             }
             else if ( n == -2 )
             {
              if( poller->debug)
               ap_log_debug_log("\t-P- Disconnect %d --\n", conn->idx);
              ap_net_conn_pool_poller_remove_conn(conn->parent, conn->idx);
              conn->state |= 64;
              if ( ! ap_utils_timespec_is_set(&conn->expire) )
               ap_utils_timespec_set(&conn->expire, 2, 2000);
                 if ( conn->buffill - conn->bufpos > 0 )
                 {
                     if ( pool->callback_func != ((void *)0) )
                          pool->callback_func(conn, 10);
                 }
              continue;
             }
             else
             {
                 if( poller->debug)
                   ap_log_debug_log("\t-P- ERROR %d --\n", conn->idx);
              return 0;
             }
        }
        if ( (((pool->flags) & (2)) != 0) && (((poller->events[event_idx].events) & (EPOLLOUT)) != 0) )
        {
             if ( pool->callback_func != ((void *)0) )
                  pool->callback_func(conn, 8);
        }
    }
    for (i = 0; i < pool->max_connections; ++i )
    {
     conn = &pool->conns[i];
     if ( ! (((conn->state) & (2)) != 0) )
      continue;
     if ( ap_utils_timespec_is_set(&conn->expire) && 0 >= ap_utils_timespec_cmp_to_now( &conn->expire ) )
     {
      conn->state |= 32;
            if ( pool->callback_func != ((void *)0) )
                 pool->callback_func(conn, 9);
      ap_net_conn_pool_close_connection(pool, i);
      if( poller->debug )
       ap_log_debug_log("\t-PEXPIRED %d %ld ms\n", i, ap_utils_timespec_elapsed(&conn->expire, ((void *)0), ((void *)0)));
     }
        if ( poller->emit_old_data_signal && conn->buffill - conn->bufpos > 0 )
        {
            if ( pool->callback_func != ((void *)0) )
                 pool->callback_func(conn, 10);
        }
    }
    return 1;
}

canonical_original_code:
int ap_net_conn_pool_poll(struct ap_net_conn_pool_t *pool)
{
    int event_idx;
    int i;
    int n;
    struct epoll_event ev;
    struct ap_net_poll_t *poller;
    struct ap_net_connection_t *conn;
    char buf[100];
    ap_error_clear();
    poller = pool->poller;
    for (i = 0; i < pool->max_connections; ++i )
    {
     conn = &pool->conns[i];
     if ( (((conn->state) & (64)) != 0) )
     {
            ap_net_conn_pool_close_connection(pool, i);
            continue;
     }
    }
    poller->events_count = epoll_wait(poller->epoll_fd, poller->events, poller->max_events, 0);
    if (poller->events_count == -1)
    {
        ap_error_set_detailed(_func_name, 1, "epoll_wait()");
        return 0;
    }
    if( poller->debug && poller->events_count > 0 )
     ap_log_debug_log("---P-EVTCNT %d\n", poller->events_count);
    if ( pool->listener.sock != -1 )
    {
        for ( event_idx = 0; event_idx < poller->events_count; ++event_idx)
        {
            if ( poller->events[event_idx].data.fd != pool->listener.sock)
             continue;
            if ( (((poller->events[event_idx].events) & ((EPOLLERR | EPOLLHUP))) != 0) )
            {
                ap_error_set_detailed(_func_name, 2, "epoll reports error/hangup on listener socket");
                return 0;
            }
            conn = ap_net_conn_pool_accept_connection(pool);
            if ( conn == ((void *)0) )
            {
             if ( ap_error_get() == 8 )
             {
                    if( poller->debug )
                     ap_log_debug_log("\t-P-NOACCEPT - denied by callback\n");
              break;
             }
                return 0;
            }
            if( poller->debug )
            {
             if ( (((conn->flags) & (1)) != 0) )
                 ap_log_debug_log("\t-P-DataIn_UDP %d %s @ %d (p:%d f:%d s:%d)\n", conn->idx,
                   inet_ntop(conn->remote.af, (conn->remote.af == 2 ? (void *)&conn->remote.addr4.sin_addr : (void *)&conn->remote.addr6.sin6_addr), buf, 100),
                   ntohs(conn->remote.addr4.sin_port), conn->bufpos, conn->buffill, conn->bufsize);
             else
              ap_log_debug_log("\t-P-ACCEPT %d\n", conn->idx);
            }
            break;
        }
    }
    for ( event_idx = 0; event_idx < poller->events_count; ++event_idx)
    {
        if ( poller->events[event_idx].data.fd == pool->listener.sock)
         continue;
        conn = ap_net_conn_pool_get_conn_by_fd(pool, poller->events[event_idx].data.fd);
        if ( conn == ((void *)0) || ! ((((conn->state) & (2)) != 0)) )
        {
            ev.events = EPOLLIN;
            ev.data.fd = poller->events[event_idx].data.fd;
            epoll_ctl(poller->epoll_fd, 2, ev.data.fd, &ev);
            if( poller->debug )
             ap_log_debug_log("\t-P-FDERR\n");
            continue;
        }
        if ( (((poller->events[event_idx].events) & ((EPOLLERR | EPOLLHUP))) != 0) )
        {
            conn->state |= 1;
            ap_net_conn_pool_close_connection(pool, conn->idx);
            if( poller->debug ) ap_log_debug_log("\t-P-ERR %d\n", conn->idx);
            continue;
        }
        if ( (((poller->events[event_idx].events) & (EPOLLIN)) != 0) )
        {
          if( poller->debug)
           ap_log_debug_log("\t-P-DATAIN %d(p:%d f:%d s:%d)", conn->idx, conn->bufpos, conn->buffill, conn->bufsize);
             n = ap_net_conn_pool_recv(pool, conn->idx);
             if ( n > 0 )
             {
              if( poller->debug)
               ap_log_debug_log(" > (p:%d f:%d s:%d)\n", conn->bufpos, conn->buffill, conn->bufsize);
              if ( pool->callback_func != ((void *)0) )
               pool->callback_func(conn, 7);
             }
             else if ( n == 0 )
             {
              if( poller->debug )
               ap_log_debug_log(" -P- buffer full --\n");
             }
             else if ( n == -2 )
             {
              if( poller->debug)
               ap_log_debug_log("\t-P- Disconnect %d --\n", conn->idx);
              ap_net_conn_pool_poller_remove_conn(conn->parent, conn->idx);
              conn->state |= 64;
              if ( ! ap_utils_timespec_is_set(&conn->expire) )
               ap_utils_timespec_set(&conn->expire, 2, 2000);
                 if ( conn->buffill - conn->bufpos > 0 )
                 {
                     if ( pool->callback_func != ((void *)0) )
                          pool->callback_func(conn, 10);
                 }
              continue;
             }
             else
             {
                 if( poller->debug)
                   ap_log_debug_log("\t-P- ERROR %d --\n", conn->idx);
              return 0;
             }
        }
        if ( (((pool->flags) & (2)) != 0) && (((poller->events[event_idx].events) & (EPOLLOUT)) != 0) )
        {
             if ( pool->callback_func != ((void *)0) )
                  pool->callback_func(conn, 8);
        }
    }
    for (i = 0; i < pool->max_connections; ++i )
    {
     conn = &pool->conns[i];
     if ( ! (((conn->state) & (2)) != 0) )
      continue;
     if ( ap_utils_timespec_is_set(&conn->expire) && 0 >= ap_utils_timespec_cmp_to_now( &conn->expire ) )
     {
      conn->state |= 32;
            if ( pool->callback_func != ((void *)0) )
                 pool->callback_func(conn, 9);
      ap_net_conn_pool_close_connection(pool, i);
      if( poller->debug )
       ap_log_debug_log("\t-PEXPIRED %d %ld ms\n", i, ap_utils_timespec_elapsed(&conn->expire, ((void *)0), ((void *)0)));
     }
        if ( poller->emit_old_data_signal && conn->buffill - conn->bufpos > 0 )
        {
            if ( pool->callback_func != ((void *)0) )
                 pool->callback_func(conn, 10);
        }
    }
    return 1;
}

variable_types: struct ap_net_poll_t { 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 *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; } *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; } *

variable_types:
buf
char [100]


decompiled_code:
__int64 __fastcall ap_net_conn_pool_poller_remove_conn(_QWORD *a1, int a2)
{
unsigned int v2; // er12
__int64 v4; // rdx
int v5; // edi
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v2 = 0;
ap_error_clear();
v4 = a1[3];
if ( v4 )
{
v5 = *(_DWORD *)(v4 + 28);
event.events = 1;
v2 = 1;
event.data.fd = *(_DWORD *)(*a1 + 144LL * a2 + 12);
if ( epoll_ctl(v5, 2, event.data.fd, &event) == -1 && *_errno_location() != 2 )
{
v2 = 0;
ap_error_set("ap_net_conn_pool_poller_remove_conn()", 1LL);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, int a2)
{
unsigned int v2; // er12
__int64 v4; // rdx
int v5; // edi
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v2 = 0;
ap_error_clear();
v4 = a1[3];
if ( v4 )
{
v5 = *(_DWORD *)(v4 + 28);
event.events = 1;
v2 = 1;
event.data.fd = *(_DWORD *)(*a1 + 144LL * a2 + 12);
if ( epoll_ctl(v5, 2, event.data.fd, &event) == -1 && *_errno_location() != 2 )
{
v2 = 0;
ap_error_set("ap_net_conn_pool_poller_remove_conn()", 1LL);
}
}
return v2;
}

original_code:
int ap_net_conn_pool_poller_remove_conn(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    struct epoll_event ev;
    ap_error_clear();
    if ( pool->poller == ((void *)0) )
     return 0;
    ev.events = EPOLLIN;
    ev.data.fd = pool->conns[conn_idx].fd;
    if (epoll_ctl(pool->poller->epoll_fd, 2, ev.data.fd, &ev) == -1 && (*__errno_location ()) != 2 )
    {
        ap_error_set("ap_net_conn_pool_poller_remove_conn()", 1);
        return 0;
    }
    return 1;
}

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

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: 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_conn_pool_poller_create(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbx

ap_error_clear();
v1 = ap_net_poller_create(*(unsigned int *)(a1 + 48), *(unsigned int *)(a1 + 8));
*(_QWORD *)(a1 + 24) = v1;
if ( !v1 )
return 0LL;
v2 = 0LL;
if ( *(int *)(a1 + 8) > 0 )
{
while ( (*(_DWORD *)(*(_QWORD *)a1 + 144 * v2 + 132) & 0x43) != 2
|| (unsigned int)ap_net_conn_pool_poller_add_conn(a1, (unsigned int)v2) )
{
if ( *(_DWORD *)(a1 + 8) <= (int)++v2 )
return 1LL;
}
return 0LL;
}
return 1LL;
}

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

ap_error_clear();
v1 = ap_net_poller_create(*(unsigned int *)(a1 + 48), *(unsigned int *)(a1 + 8));
*(_QWORD *)(a1 + 24) = v1;
if ( !v1 )
return 0LL;
v2 = 0LL;
if ( *(int *)(a1 + 8) > 0 )
{
while ( (*(_DWORD *)(*(_QWORD *)a1 + 144 * v2 + 132) & 0x43) != 2
|| (unsigned int)func2(a1, (unsigned int)v2) )
{
if ( *(_DWORD *)(a1 + 8) <= (int)++v2 )
return 1LL;
}
return 0LL;
}
return 1LL;
}

original_code:
int ap_net_conn_pool_poller_create(struct ap_net_conn_pool_t *pool)
{
    int conn_idx;
    ap_error_clear();
    pool->poller = ap_net_poller_create(pool->listener.sock, pool->max_connections);
    if ( pool->poller == ((void *)0) )
      return 0;
    for ( conn_idx = 0; conn_idx < pool->max_connections; ++conn_idx )
    {
        if ( ! (pool->conns[conn_idx].state & 2) || (pool->conns[conn_idx].state & (1 | 64)) )
            continue;
        if ( ! ap_net_conn_pool_poller_add_conn(pool, conn_idx))
            return 0;
    }
    return 1;
}


decompiled_code:
__int64 __fastcall ap_net_conn_pool_poller_add_conn(_QWORD *a1, int a2)
{
__int64 v3; // rdx
int v4; // edi
int v5; // er8
__int64 result; // rax
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

ap_error_clear();
v3 = a1[3];
if ( !v3 )
return ap_net_conn_pool_poller_create((__int64)a1);
v4 = *(_DWORD *)(v3 + 28);
event.events = 1;
event.data.fd = *(_DWORD *)(*a1 + 144LL * a2 + 12);
v5 = epoll_ctl(v4, 1, event.data.fd, &event);
result = 1LL;
if ( v5 == -1 )
{
ap_error_set("ap_net_conn_pool_poller_add_conn()", 1LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, int a2)
{
__int64 v3; // rdx
int v4; // edi
int v5; // er8
__int64 result; // rax
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

ap_error_clear();
v3 = a1[3];
if ( !v3 )
return func1((__int64)a1);
v4 = *(_DWORD *)(v3 + 28);
event.events = 1;
event.data.fd = *(_DWORD *)(*a1 + 144LL * a2 + 12);
v5 = epoll_ctl(v4, 1, event.data.fd, &event);
result = 1LL;
if ( v5 == -1 )
{
ap_error_set("ap_net_conn_pool_poller_add_conn()", 1LL);
result = 0LL;
}
return result;
}

original_code:
int ap_net_conn_pool_poller_add_conn(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    struct epoll_event ev;
    ap_error_clear();
    if ( pool->poller == ((void *)0) )
     return ap_net_conn_pool_poller_create(pool);
    ev.events = EPOLLIN;
    ev.data.fd = pool->conns[conn_idx].fd;
    if (epoll_ctl(pool->poller->epoll_fd, 1, ev.data.fd, &ev) == -1)
    {
        ap_error_set("ap_net_conn_pool_poller_add_conn()", 1);
        return 0;
    }
    return 1;
}


decompiled_code:
__int64 __fastcall ap_net_conn_pool_print_stat(__int64 a1, const char *a2)
{
unsigned int v2; // er10
unsigned int v3; // eax
__int64 v4; // rcx

v2 = *(_DWORD *)(a1 + 88);
v3 = 100 * *(_DWORD *)(a1 + 100);
ap_log_debug_log(
"\n# ap_net statistics: %s\n\ttotal conns: %u, avg: %d.%02d, t/o count: %u, queue full: %u times\n",
a2,
v2,
v3 / v2 / 0x64uLL,
v3 / v2
- 20
* (v3 / v2 / 0x64uLL
+ ((unsigned __int64)((0x28F5C28F5C28F5C3LL * (unsigned __int128)((unsigned __int64)(v3 / v2) >> 2)) >> 64) & 0xFFFFFFFFFFFFFFFCLL)),
*(unsigned int *)(a1 + 92),
*(unsigned int *)(a1 + 96));
v4 = *(_QWORD *)(a1 + 112) / 1000000LL + 1000000LL * *(_QWORD *)(a1 + 104);
return ap_log_debug_log(
"\ttotal time: %ld sec, avg per conn: %ld.%03d\n",
*(_QWORD *)(a1 + 104),
v4 / 1000000,
v4 % 1000000);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
unsigned int v2; // er10
unsigned int v3; // eax
__int64 v4; // rcx

v2 = *(_DWORD *)(a1 + 88);
v3 = 100 * *(_DWORD *)(a1 + 100);
ap_log_debug_log(
"\n# ap_net statistics: %s\n\ttotal conns: %u, avg: %d.%02d, t/o count: %u, queue full: %u times\n",
a2,
v2,
v3 / v2 / 0x64uLL,
v3 / v2
- 20
* (v3 / v2 / 0x64uLL
+ ((unsigned __int64)((0x28F5C28F5C28F5C3LL * (unsigned __int128)((unsigned __int64)(v3 / v2) >> 2)) >> 64) & 0xFFFFFFFFFFFFFFFCLL)),
*(unsigned int *)(a1 + 92),
*(unsigned int *)(a1 + 96));
v4 = *(_QWORD *)(a1 + 112) / 1000000LL + 1000000LL * *(_QWORD *)(a1 + 104);
return ap_log_debug_log(
"\ttotal time: %ld sec, avg per conn: %ld.%03d\n",
*(_QWORD *)(a1 + 104),
v4 / 1000000,
v4 % 1000000);
}

original_code:
void ap_net_conn_pool_print_stat(struct ap_net_conn_pool_t *pool, char *intro_message)
{
  long n;
  n = pool->stat.active_conn_count * 100 / pool->stat.conn_count;
  ap_log_debug_log("\n# ap_net statistics: %s\n\ttotal conns: %u, avg: %d.%02d, t/o count: %u, queue full: %u times\n",
            intro_message, pool->stat.conn_count, n / 100, n % 100, pool->stat.timedout, pool->stat.queue_full_count);
  n = pool->stat.total_time.tv_sec * 1000000 + pool->stat.total_time.tv_nsec / 1000000;
  ap_log_debug_log("\ttotal time: %ld sec, avg per conn: %ld.%03d\n", pool->stat.total_time.tv_sec, n / 1000000, n % 1000000);
}

variable_types: n long


decompiled_code:
__int64 __fastcall ap_net_conn_pool_send_async(__int64 a1, int a2, const void *a3, int a4)
{
__int64 v7; // r10
int v8; // eax
__int64 v9; // r14
__int64 v10; // rdi
unsigned int i; // er15
int v12; // eax
int addr_len; // [rsp+4h] [rbp-44h]
struct sockaddr *addr; // [rsp+8h] [rbp-40h]

ap_error_clear();
if ( a2 >= 0
&& *(_DWORD *)(a1 + 8) >= a2
&& (v7 = *(_QWORD *)a1 + 144LL * a2, v8 = *(_DWORD *)(v7 + 132), v9 = v7, (v8 & 2) != 0) )
{
v10 = *(unsigned int *)(v7 + 12);
*(_DWORD *)(v7 + 132) = v8 | 0x10;
addr = (struct sockaddr *)(v7 + 48);
addr_len = (*(_DWORD *)(a1 + 16) & 4) == 0 ? 16 : 28;
if ( (*(_DWORD *)(a1 + 16) & 1) == 0 )
goto LABEL_12;
LABEL_5:
for ( i = ap_net_send(v10, a3, (unsigned int)a4, 1LL); ; i = sendto(v10, a3, a4, 0, addr, addr_len) )
{
*(_DWORD *)(v9 + 132) &= 0xFFFFFFEF;
if ( (int)i > 0 )
break;
v12 = *_errno_location();
if ( v12 == 11 )
{
if ( a4 <= 9 )
return i;
a4 >>= 1;
}
else
{
if ( v12 == 32 && i == -1 )
{
i = -1;
LABEL_18:
if ( ap_log_debug_level )
ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", (unsigned int)a2);
ap_net_conn_pool_close_connection(a1, (unsigned int)a2);
return i;
}
if ( !i )
goto LABEL_18;
}
v10 = *(unsigned int *)(v9 + 12);
if ( (*(_DWORD *)(a1 + 16) & 1) != 0 )
goto LABEL_5;
LABEL_12:
;
}
}
else
{
i = 0;
ap_error_set_detailed("ap_net_conn_pool_send()", 5LL, &unk_23C, (unsigned int)a2);
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, const void *a3, int a4)
{
__int64 v7; // r10
int v8; // eax
__int64 v9; // r14
__int64 v10; // rdi
unsigned int i; // er15
int v12; // eax
int addr_len; // [rsp+4h] [rbp-44h]
struct sockaddr *addr; // [rsp+8h] [rbp-40h]

ap_error_clear();
if ( a2 >= 0
&& *(_DWORD *)(a1 + 8) >= a2
&& (v7 = *(_QWORD *)a1 + 144LL * a2, v8 = *(_DWORD *)(v7 + 132), v9 = v7, (v8 & 2) != 0) )
{
v10 = *(unsigned int *)(v7 + 12);
*(_DWORD *)(v7 + 132) = v8 | 0x10;
addr = (struct sockaddr *)(v7 + 48);
addr_len = (*(_DWORD *)(a1 + 16) & 4) == 0 ? 16 : 28;
if ( (*(_DWORD *)(a1 + 16) & 1) == 0 )
goto LABEL_12;
LABEL_5:
for ( i = ap_net_send(v10, a3, (unsigned int)a4, 1LL); ; i = sendto(v10, a3, a4, 0, addr, addr_len) )
{
*(_DWORD *)(v9 + 132) &= 0xFFFFFFEF;
if ( (int)i > 0 )
break;
v12 = *_errno_location();
if ( v12 == 11 )
{
if ( a4 <= 9 )
return i;
a4 >>= 1;
}
else
{
if ( v12 == 32 && i == -1 )
{
i = -1;
LABEL_18:
if ( ap_log_debug_level )
ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", (unsigned int)a2);
ap_net_conn_pool_close_connection(a1, (unsigned int)a2);
return i;
}
if ( !i )
goto LABEL_18;
}
v10 = *(unsigned int *)(v9 + 12);
if ( (*(_DWORD *)(a1 + 16) & 1) != 0 )
goto LABEL_5;
LABEL_12:
;
}
}
else
{
i = 0;
ap_error_set_detailed("ap_net_conn_pool_send()", 5LL, &unk_23C, (unsigned int)a2);
}
return i;
}

original_code:
int ap_net_conn_pool_send_async(struct ap_net_conn_pool_t *pool, int conn_idx, void *src_buf, int size)
{
    int n;
    int send_chunk;
    socklen_t slen;
    struct ap_net_connection_t *conn;
    ap_error_clear();
    conn = &pool->conns[conn_idx];
    if ( conn_idx < 0 || conn_idx > pool->max_connections || ! (((conn->state) & (2)) != 0) )
    {
        ap_error_set_detailed(_func_name, 5, "%d", conn_idx);
        return 0;
    }
    conn->state |= 16;
 slen = (((pool->flags) & (4)) != 0) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
 send_chunk = size;
    for(;;)
    {
  if ( (((pool->flags) & (1)) != 0) )
   n = ap_net_send(conn->fd, src_buf, send_chunk, 1);
  else
   n = sendto(conn->fd, src_buf, send_chunk, 0, (struct sockaddr *)&conn->remote, slen);
  ((conn->state) &= ~(16));
  if ( n > 0 )
   break;
  if ( (*__errno_location ()) == 11 || (*__errno_location ()) == 11 )
  {
   if ( send_chunk < 10 )
    break;
   send_chunk /= 2;
   continue;
  }
  else if ( (n == -1 && (*__errno_location ()) == 32) || n == 0)
  {
   if (ap_log_debug_level)
    ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", conn_idx);
   ap_net_conn_pool_close_connection(pool, conn_idx);
   break;
  }
    }
    return n;
}

canonical_original_code:
int ap_net_conn_pool_send_async(struct ap_net_conn_pool_t *pool, int conn_idx, void *src_buf, int size)
{
    int n;
    int send_chunk;
    unsigned int slen;
    struct ap_net_connection_t *conn;
    ap_error_clear();
    conn = &pool->conns[conn_idx];
    if ( conn_idx < 0 || conn_idx > pool->max_connections || ! (((conn->state) & (2)) != 0) )
    {
        ap_error_set_detailed(_func_name, 5, "%d", conn_idx);
        return 0;
    }
    conn->state |= 16;
 slen = (((pool->flags) & (4)) != 0) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
 send_chunk = size;
    for(;;)
    {
  if ( (((pool->flags) & (1)) != 0) )
   n = ap_net_send(conn->fd, src_buf, send_chunk, 1);
  else
   n = sendto(conn->fd, src_buf, send_chunk, 0, (struct sockaddr *)&conn->remote, slen);
  ((conn->state) &= ~(16));
  if ( n > 0 )
   break;
  if ( (*__errno_location ()) == 11 || (*__errno_location ()) == 11 )
  {
   if ( send_chunk < 10 )
    break;
   send_chunk /= 2;
   continue;
  }
  else if ( (n == -1 && (*__errno_location ()) == 32) || n == 0)
  {
   if (ap_log_debug_level)
    ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", conn_idx);
   ap_net_conn_pool_close_connection(pool, conn_idx);
   break;
  }
    }
    return n;
}

variable_types:
slen
unsigned int

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


decompiled_code:
__int64 __fastcall ap_net_conn_pool_send(__int64 a1, int a2, const void *a3, unsigned int a4)
{
__int64 v7; // rbx
int v8; // eax
unsigned int v9; // er13
__int64 v11; // rdi
unsigned int v12; // eax

if ( (*(_BYTE *)(a1 + 16) & 2) != 0 )
return ap_net_conn_pool_send_async(a1, a2, a3, a4);
ap_error_clear();
if ( a2 >= 0
&& *(_DWORD *)(a1 + 8) >= a2
&& (v7 = *(_QWORD *)a1 + 144LL * a2, v8 = *(_DWORD *)(v7 + 132), (v8 & 2) != 0) )
{
v11 = *(unsigned int *)(v7 + 12);
*(_DWORD *)(v7 + 132) = v8 | 0x10;
v12 = ap_net_send(v11, a3, a4, 0LL);
*(_DWORD *)(v7 + 132) &= 0xFFFFFFEF;
v9 = v12;
if ( v12 == -1 && *_errno_location() == 32 )
{
if ( ap_log_debug_level )
ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", (unsigned int)a2);
ap_net_conn_pool_close_connection(a1, (unsigned int)a2);
}
}
else
{
v9 = 0;
ap_error_set_detailed("ap_net_conn_pool_send()", 5LL, &unk_23C, (unsigned int)a2);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, const void *a3, unsigned int a4)
{
__int64 v7; // rbx
int v8; // eax
unsigned int v9; // er13
__int64 v11; // rdi
unsigned int v12; // eax

if ( (*(_BYTE *)(a1 + 16) & 2) != 0 )
return func0(a1, a2, a3, a4);
ap_error_clear();
if ( a2 >= 0
&& *(_DWORD *)(a1 + 8) >= a2
&& (v7 = *(_QWORD *)a1 + 144LL * a2, v8 = *(_DWORD *)(v7 + 132), (v8 & 2) != 0) )
{
v11 = *(unsigned int *)(v7 + 12);
*(_DWORD *)(v7 + 132) = v8 | 0x10;
v12 = ap_net_send(v11, a3, a4, 0LL);
*(_DWORD *)(v7 + 132) &= 0xFFFFFFEF;
v9 = v12;
if ( v12 == -1 && *_errno_location() == 32 )
{
if ( ap_log_debug_level )
ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", (unsigned int)a2);
ap_net_conn_pool_close_connection(a1, (unsigned int)a2);
}
}
else
{
v9 = 0;
ap_error_set_detailed("ap_net_conn_pool_send()", 5LL, &unk_23C, (unsigned int)a2);
}
return v9;
}

original_code:
int ap_net_conn_pool_send(struct ap_net_conn_pool_t *pool, int conn_idx, void *src_buf, int size)
{
    int n;
    struct ap_net_connection_t *conn;
    if ( (((pool->flags) & (2)) != 0) )
     return ap_net_conn_pool_send_async(pool, conn_idx, src_buf, size);
    ap_error_clear();
    conn = &pool->conns[conn_idx];
    if ( conn_idx < 0 || conn_idx > pool->max_connections || ! (((conn->state) & (2)) != 0) )
    {
        ap_error_set_detailed(_func_name, 5, "%d", conn_idx);
        return 0;
    }
    conn->state |= 16;
    n = ap_net_send(conn->fd, src_buf, size, 0);
    ((conn->state) &= ~(16));
    if (n == -1 && (*__errno_location ()) == 32)
    {
        if (ap_log_debug_level)
            ap_log_debug_log("? ap_net_conn_pool_send(): Connection #%d is dead prematurely: %m\n", conn_idx);
        ap_net_conn_pool_close_connection(pool, conn_idx);
    }
    return n;
}

variable_types: size int


decompiled_code:
__int64 __fastcall ap_net_conn_pool_recv(__int64 a1, int a2)
{
__int64 v4; // rbx
__int64 v5; // rsi
int v6; // eax
int v7; // er14
int v8; // edx
unsigned int v9; // eax
unsigned int v10; // er13
void *v11; // rsi
socklen_t v12; // eax
int v13; // edi
unsigned int v14; // eax
unsigned int v16; // eax
socklen_t addr_len[11]; // [rsp+Ch] [rbp-2Ch] BYREF

ap_error_clear();
v4 = *(_QWORD *)a1 + 144LL * a2;
if ( a2 < 0 || *(_DWORD *)(a1 + 8) < a2 || (*(_BYTE *)(v4 + 132) & 2) == 0 )
{
v10 = -1;
ap_error_set_detailed("ap_net_conn_pool_recv()", 5LL, &unk_1D7, (unsigned int)a2);
return v10;
}
v5 = *(int *)(v4 + 124);
v6 = *(_DWORD *)(v4 + 120);
if ( (int)v5 < 0 )
goto LABEL_9;
v7 = *(_DWORD *)(v4 + 128);
if ( v7 < 0 )
goto LABEL_9;
v8 = *(_DWORD *)(v4 + 120);
if ( v7 <= v6 )
v8 = *(_DWORD *)(v4 + 128);
if ( (int)v5 >= v8 )
{
LABEL_9:
*(_QWORD *)(v4 + 124) = 0LL;
v7 = 0;
v5 = 0LL;
}
if ( v6 + v6 / -3 < (int)v5 )
{
v7 -= v5;
memmove(*(void **)(v4 + 112), (const void *)(*(_QWORD *)(v4 + 112) + v5), v7);
*(_DWORD *)(v4 + 128) = v7;
v6 = *(_DWORD *)(v4 + 120);
*(_DWORD *)(v4 + 124) = 0;
}
v9 = v6 - v7;
v10 = v9;
if ( v9 )
{
*(_DWORD *)(v4 + 132) |= 8u;
v11 = (void *)(*(_QWORD *)(v4 + 112) + v7);
if ( (*(_BYTE *)(v4 + 16) & 1) != 0 )
{
v12 = 28;
v13 = *(_DWORD *)(a1 + 48);
if ( *(_WORD *)(v4 + 48) != 10 )
v12 = 16;
addr_len[0] = v12;
v14 = recvfrom(v13, v11, (int)v10, 16448, (struct sockaddr *)(v4 + 48), addr_len);
*(_DWORD *)(v4 + 132) &= 0xFFFFFFF7;
v10 = v14;
if ( !v14 )
{
LABEL_17:
if ( *_errno_location() != 11 )
return (unsigned int)-2;
goto LABEL_18;
}
}
else
{
v16 = ap_net_recv(*(unsigned int *)(v4 + 12), v11, v9, 0LL);
*(_DWORD *)(v4 + 132) &= 0xFFFFFFF7;
v10 = v16;
if ( !v16 )
goto LABEL_17;
}
if ( v10 != -1 )
{
LABEL_18:
*(_DWORD *)(v4 + 128) += v10;
return v10;
}
if ( ap_log_debug_level )
{
ap_error_get_string();
ap_log_debug_log(
"? Connection [%d] is dead prematurely. retcode %d, error %s\n",
(unsigned int)a2,
0xFFFFFFFFLL,
&loc_9);
}
ap_net_conn_pool_close_connection(a1, (unsigned int)a2);
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 v4; // rbx
__int64 v5; // rsi
int v6; // eax
int v7; // er14
int v8; // edx
unsigned int v9; // eax
unsigned int v10; // er13
void *v11; // rsi
socklen_t v12; // eax
int v13; // edi
unsigned int v14; // eax
unsigned int v16; // eax
socklen_t addr_len[11]; // [rsp+Ch] [rbp-2Ch] BYREF

ap_error_clear();
v4 = *(_QWORD *)a1 + 144LL * a2;
if ( a2 < 0 || *(_DWORD *)(a1 + 8) < a2 || (*(_BYTE *)(v4 + 132) & 2) == 0 )
{
v10 = -1;
ap_error_set_detailed("ap_net_conn_pool_recv()", 5LL, &unk_1D7, (unsigned int)a2);
return v10;
}
v5 = *(int *)(v4 + 124);
v6 = *(_DWORD *)(v4 + 120);
if ( (int)v5 < 0 )
goto LABEL_9;
v7 = *(_DWORD *)(v4 + 128);
if ( v7 < 0 )
goto LABEL_9;
v8 = *(_DWORD *)(v4 + 120);
if ( v7 <= v6 )
v8 = *(_DWORD *)(v4 + 128);
if ( (int)v5 >= v8 )
{
LABEL_9:
*(_QWORD *)(v4 + 124) = 0LL;
v7 = 0;
v5 = 0LL;
}
if ( v6 + v6 / -3 < (int)v5 )
{
v7 -= v5;
memmove(*(void **)(v4 + 112), (const void *)(*(_QWORD *)(v4 + 112) + v5), v7);
*(_DWORD *)(v4 + 128) = v7;
v6 = *(_DWORD *)(v4 + 120);
*(_DWORD *)(v4 + 124) = 0;
}
v9 = v6 - v7;
v10 = v9;
if ( v9 )
{
*(_DWORD *)(v4 + 132) |= 8u;
v11 = (void *)(*(_QWORD *)(v4 + 112) + v7);
if ( (*(_BYTE *)(v4 + 16) & 1) != 0 )
{
v12 = 28;
v13 = *(_DWORD *)(a1 + 48);
if ( *(_WORD *)(v4 + 48) != 10 )
v12 = 16;
addr_len[0] = v12;
v14 = recvfrom(v13, v11, (int)v10, 16448, (struct sockaddr *)(v4 + 48), addr_len);
*(_DWORD *)(v4 + 132) &= 0xFFFFFFF7;
v10 = v14;
if ( !v14 )
{
LABEL_17:
if ( *_errno_location() != 11 )
return (unsigned int)-2;
goto LABEL_18;
}
}
else
{
v16 = ap_net_recv(*(unsigned int *)(v4 + 12), v11, v9, 0LL);
*(_DWORD *)(v4 + 132) &= 0xFFFFFFF7;
v10 = v16;
if ( !v16 )
goto LABEL_17;
}
if ( v10 != -1 )
{
LABEL_18:
*(_DWORD *)(v4 + 128) += v10;
return v10;
}
if ( ap_log_debug_level )
{
ap_error_get_string();
ap_log_debug_log(
"? Connection [%d] is dead prematurely. retcode %d, error %s\n",
(unsigned int)a2,
0xFFFFFFFFLL,
&loc_9);
}
ap_net_conn_pool_close_connection(a1, (unsigned int)a2);
}
return v10;
}

original_code:
int ap_net_conn_pool_recv(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    int n;
    socklen_t slen;
    int space_left;
    struct ap_net_connection_t *conn;
    ap_error_clear();
    conn = &pool->conns[conn_idx];
    if ( conn_idx < 0 || conn_idx > pool->max_connections || ! (((conn->state) & (2)) != 0) )
    {
        ap_error_set_detailed(_func_name, 5, "%d", conn_idx);
        return -1;
    }
    if ( conn->bufpos < 0 || conn->buffill < 0 || conn->bufpos >= conn->bufsize || conn->bufpos >= conn->buffill )
    {
        conn->bufpos = 0;
        conn->buffill = 0;
    }
    if ( conn->bufpos > (conn->bufsize - conn->bufsize / 3) )
    {
       n = conn->buffill - conn->bufpos;
       memmove(conn->buf, conn->buf + conn->bufpos, n);
       conn->buffill = n;
       conn->bufpos = 0;
    }
    space_left = conn->bufsize - conn->buffill;
    if ( space_left == 0 )
     return 0;
    conn->state |= 8;
    if ( (((conn->flags) & (1)) != 0) )
    {
     slen = (conn->remote.af == 10 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
        n = recvfrom(pool->listener.sock, conn->buf + conn->buffill, space_left, MSG_DONTWAIT | MSG_NOSIGNAL,
          (struct sockaddr*)&conn->remote, &slen
         );
    }
    else
    {
        n = ap_net_recv(conn->fd, conn->buf + conn->buffill, space_left, 0);
    }
    ((conn->state) &= ~(8));
    if ( n == 0 && (*__errno_location ()) != 11 && (*__errno_location ()) != 11 )
        return -2;
    if ( n == -1 )
    {
        if (ap_log_debug_level)
            ap_log_debug_log("? Connection [%d] is dead prematurely. retcode %d, error %s\n", conn_idx, n, 9, ap_error_get_string());
        ap_net_conn_pool_close_connection(pool, conn_idx);
        return -1;
    }
    conn->buffill += n;
    return n;
}

canonical_original_code:
int ap_net_conn_pool_recv(struct ap_net_conn_pool_t *pool, int conn_idx)
{
    int n;
    unsigned int slen;
    int space_left;
    struct ap_net_connection_t *conn;
    ap_error_clear();
    conn = &pool->conns[conn_idx];
    if ( conn_idx < 0 || conn_idx > pool->max_connections || ! (((conn->state) & (2)) != 0) )
    {
        ap_error_set_detailed(_func_name, 5, "%d", conn_idx);
        return -1;
    }
    if ( conn->bufpos < 0 || conn->buffill < 0 || conn->bufpos >= conn->bufsize || conn->bufpos >= conn->buffill )
    {
        conn->bufpos = 0;
        conn->buffill = 0;
    }
    if ( conn->bufpos > (conn->bufsize - conn->bufsize / 3) )
    {
       n = conn->buffill - conn->bufpos;
       memmove(conn->buf, conn->buf + conn->bufpos, n);
       conn->buffill = n;
       conn->bufpos = 0;
    }
    space_left = conn->bufsize - conn->buffill;
    if ( space_left == 0 )
     return 0;
    conn->state |= 8;
    if ( (((conn->flags) & (1)) != 0) )
    {
     slen = (conn->remote.af == 10 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
        n = recvfrom(pool->listener.sock, conn->buf + conn->buffill, space_left, MSG_DONTWAIT | MSG_NOSIGNAL,
          (struct sockaddr *)&conn->remote, &slen
         );
    }
    else
    {
        n = ap_net_recv(conn->fd, conn->buf + conn->buffill, space_left, 0);
    }
    ((conn->state) &= ~(8));
    if ( n == 0 && (*__errno_location ()) != 11 && (*__errno_location ()) != 11 )
        return -2;
    if ( n == -1 )
    {
        if (ap_log_debug_level)
            ap_log_debug_log("? Connection [%d] is dead prematurely. retcode %d, error %s\n", conn_idx, n, 9, ap_error_get_string());
        ap_net_conn_pool_close_connection(pool, conn_idx);
        return -1;
    }
    conn->buffill += n;
    return n;
}


decompiled_code:
__int64 __fastcall ap_net_conn_pool_connection_pre_connect(__int64 a1, int a2, int a3)
{
__int64 v4; // rbx
int v5; // eax
__int64 result; // rax

v4 = *(_QWORD *)a1 + 144LL * a2;
ap_net_connection_lock(v4);
ap_utils_timespec_set(v4 + 80, 2LL, 0LL);
if ( (unsigned int)ap_utils_timespec_is_set(a1 + 32) )
ap_utils_timespec_add(v4 + 80, a1 + 32, v4 + 96);
else
ap_utils_timespec_clear(v4 + 96);
*(_DWORD *)(v4 + 12) = -1;
*(_QWORD *)(v4 + 128) = 0x200000000LL;
v5 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(v4 + 16) = a3;
result = (unsigned int)(v5 + 1);
*(_DWORD *)(v4 + 124) = 0;
++*(_DWORD *)(a1 + 88);
*(_DWORD *)(a1 + 100) += result;
*(_DWORD *)(a1 + 12) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3)
{
__int64 v4; // rbx
int v5; // eax
__int64 result; // rax

v4 = *(_QWORD *)a1 + 144LL * a2;
ap_net_connection_lock(v4);
ap_utils_timespec_set(v4 + 80, 2LL, 0LL);
if ( (unsigned int)ap_utils_timespec_is_set(a1 + 32) )
ap_utils_timespec_add(v4 + 80, a1 + 32, v4 + 96);
else
ap_utils_timespec_clear(v4 + 96);
*(_DWORD *)(v4 + 12) = -1;
*(_QWORD *)(v4 + 128) = 0x200000000LL;
v5 = *(_DWORD *)(a1 + 12);
*(_DWORD *)(v4 + 16) = a3;
result = (unsigned int)(v5 + 1);
*(_DWORD *)(v4 + 124) = 0;
++*(_DWORD *)(a1 + 88);
*(_DWORD *)(a1 + 100) += result;
*(_DWORD *)(a1 + 12) = result;
return result;
}

original_code:
void ap_net_conn_pool_connection_pre_connect(struct ap_net_conn_pool_t *pool, int conn_idx, unsigned flags)
{
    struct ap_net_connection_t *conn;
    conn = &pool->conns[conn_idx];
    ap_net_connection_lock(conn);
    ap_utils_timespec_set(&conn->created_time, 2, 0);
    if ( ap_utils_timespec_is_set( &pool->max_conn_ttl) )
    {
     ap_utils_timespec_add(&conn->created_time, &pool->max_conn_ttl, &conn->expire);
    }
    else
    {
     ap_utils_timespec_clear( &conn->expire );
    }
    conn->fd = -1;
    conn->flags = flags;
    conn->bufpos = 0;
    conn->buffill = 0;
    conn->state = 2;
    pool->used_slots++;
    pool->stat.conn_count++;
    pool->stat.active_conn_count += pool->used_slots;
}

canonical_original_code:
void ap_net_conn_pool_connection_pre_connect(struct ap_net_conn_pool_t *pool, int conn_idx, unsigned int flags)
{
    struct ap_net_connection_t *conn;
    conn = &pool->conns[conn_idx];
    ap_net_connection_lock(conn);
    ap_utils_timespec_set(&conn->created_time, 2, 0);
    if ( ap_utils_timespec_is_set( &pool->max_conn_ttl) )
    {
     ap_utils_timespec_add(&conn->created_time, &pool->max_conn_ttl, &conn->expire);
    }
    else
    {
     ap_utils_timespec_clear( &conn->expire );
    }
    conn->fd = -1;
    conn->flags = flags;
    conn->bufpos = 0;
    conn->buffill = 0;
    conn->state = 2;
    pool->used_slots++;
    pool->stat.conn_count++;
    pool->stat.active_conn_count += pool->used_slots;
}

variable_types:
flags
unsigned int


original_code:
int ap_net_conn_pool_set_max_connections(struct ap_net_conn_pool_t *pool, int new_max, int new_bufsize)
{
    void *new_mem;
    int i;
    int n;
    int retval;
    ap_error_clear();
    if ( new_max == pool->max_connections )
        return 1;
 if ( new_max < pool->used_slots )
 {
  ap_error_set_detailed(_func_name, 4, "Can't downsize pool: too many live connections");
  return 0;
 }
    if ( ! ap_net_conn_pool_lock(pool))
    {
        ap_error_set(_func_name, 6);
     return 0;
    }
    for ( i = 0; i < pool->max_connections; ++i )
     if ( ! ap_net_connection_lock(&pool->conns[i]))
     {
      while(--i)
       ap_net_connection_unlock(&pool->conns[i]);
            ap_net_conn_pool_unlock(pool);
            ap_error_set(_func_name, 6);
      return 0;
     }
    retval = 0;
    if ( new_max < pool->max_connections )
    {
        n = -1;
        for ( i = new_max - 1; i < pool->max_connections; ++i )
        {
         for (++n;; ++n )
          if ( ! (pool->conns[n].state & 2) )
           break;
            ap_net_connection_copy(&pool->conns[n], &pool->conns[i]);
            ((pool->conns[i].state) &= ~(2));
            pool->conns[i].fd = -1;
            if ( pool->callback_func != ((void *)0) )
      {
          pool->callback_func(&pool->conns[n], 5);
          pool->callback_func(&pool->conns[i], 6);
            }
        }
        for ( i = new_max; i < pool->max_connections; ++i )
         ap_net_connection_destroy(&pool->conns[i], 0);
    }
 new_mem = realloc(pool->conns, new_max * sizeof(struct ap_net_connection_t));
    if ( new_mem == ((void *)0) )
    {
       ap_error_set(_func_name, 3);
         goto unlock;
    }
    pool->conns = new_mem;
    if ( new_max > pool->max_connections )
    {
        for ( i = pool->max_connections; i < new_max; ++i)
        {
            pool->conns[i].fd = -1;
            pool->conns[i].idx = i;
            pool->conns[i].parent = pool;
            pool->conns[i].state = 0;
            pool->conns[i].bufpos = 0;
            pool->conns[i].buffill = 0;
            pool->conns[i].bufsize = new_bufsize;
            pool->conns[i].buf = malloc(new_bufsize);
            if ( pool->conns[i].buf == ((void *)0) )
            {
                pool->conns[i].bufsize = 0;
                ap_error_set(_func_name, 3);
                goto unlock;
            }
            pool->conns[i].user_data = ((void *)0);
            if ( pool->callback_func != ((void *)0) )
          pool->callback_func(&pool->conns[i], 0);
        }
    }
    pool->max_connections = new_max;
    retval = 1;
unlock:
 for (n = 0; n < pool->max_connections; ++n )
  ap_net_connection_unlock(&pool->conns[n]);
 ap_net_conn_pool_unlock(pool);
    return retval;
}

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; } *

variable_types: i int

variable_types: retval int


original_code:
void ap_net_connection_copy(struct ap_net_connection_t *dst_conn, struct ap_net_connection_t *src_conn)
{
 void *tmp;
    dst_conn->fd = src_conn->fd;
    memcpy(&dst_conn->remote, &src_conn->remote, sizeof(src_conn->remote));
    memcpy(&dst_conn->local, &src_conn->local, sizeof(src_conn->local));
    memcpy(&dst_conn->created_time, &src_conn->created_time, sizeof(src_conn->created_time));
    memcpy(&dst_conn->expire, &src_conn->expire, sizeof(src_conn->expire));
    memcpy(dst_conn->buf, src_conn->buf, dst_conn->bufsize > src_conn->bufsize ? src_conn->bufsize : dst_conn->bufsize);
    dst_conn->bufpos = src_conn->bufpos;
    dst_conn->buffill = src_conn->buffill;
    if ( dst_conn->bufpos > dst_conn->bufsize )
        dst_conn->bufpos = dst_conn->bufsize;
    if ( dst_conn->buffill > dst_conn->bufsize )
        dst_conn->buffill = dst_conn->bufsize;
    dst_conn->state = src_conn->state;
    tmp = dst_conn->user_data;
    dst_conn->user_data = src_conn->user_data;
    dst_conn->user_data = tmp;
}

variable_types:
tmp
void *