decompiled_code:
void __fastcall sock_del(void *ptr)
{
_QWORD *v1; // rax
__int64 v2; // rdx

v1 = (_QWORD *)*((_QWORD *)ptr + 1);
if ( (void *)sock_list == ptr )
sock_list = *((_QWORD *)ptr + 1);
v2 = *(_QWORD *)ptr;
if ( *(_QWORD *)ptr )
{
*(_QWORD *)(v2 + 8) = v1;
v1 = (_QWORD *)*((_QWORD *)ptr + 1);
}
if ( v1 )
*v1 = v2;
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
_QWORD *v1; // rax
__int64 v2; // rdx

v1 = (_QWORD *)*((_QWORD *)ptr + 1);
if ( (void *)sock_list == ptr )
sock_list = *((_QWORD *)ptr + 1);
v2 = *(_QWORD *)ptr;
if ( *(_QWORD *)ptr )
{
*(_QWORD *)(v2 + 8) = v1;
v1 = (_QWORD *)*((_QWORD *)ptr + 1);
}
if ( v1 )
*v1 = v2;
free(ptr);
}

original_code:
void
sock_del(sock *s)
{
 if (sock_list == s)
  sock_list = (s->next ? s->next : ((void *)0));
 if (s->prev != ((void *)0))
  s->prev->next = s->next;
 if (s->next != ((void *)0))
  s->next->prev = s->prev;
 free(s);
}

canonical_original_code:
void sock_del(struct sock_t *s)
{
 if (sock_list == s)
  sock_list = (s->next ? s->next : ((void *)0));
 if (s->prev != ((void *)0))
  s->prev->next = s->next;
 if (s->next != ((void *)0))
  s->next->prev = s->prev;
 free(s);
}

variable_types: struct sock_t { struct sock_t *prev; struct sock_t *next; int sock_fd; int sock_flags; long sock_last; char sock_buf[16384]; unsigned long sock_buf_len; char *sock_buf_ptr; } *


repo: jmaurice/zorak-services

decompiled_code:
__int64 __fastcall sock_set_nonblock(int fd)
{
unsigned int v1; // eax
__int64 result; // rax
__int64 optval; // [rsp+8h] [rbp-10h] BYREF

optval = 0x100000001LL;
v1 = fcntl(fd, 3, 0LL);
if ( v1 == -1 || (BYTE1(v1) |= 8u, fcntl(fd, 4, v1) == -1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(setsockopt(fd, 1, 13, &optval, 8u) == -1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
unsigned int v1; // eax
__int64 result; // rax
__int64 optval; // [rsp+8h] [rbp-10h] BYREF

optval = 0x100000001LL;
v1 = fcntl(fd, 3, 0LL);
if ( v1 == -1 || (BYTE1(v1) |= 8u, fcntl(fd, 4, v1) == -1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(setsockopt(fd, 1, 13, &optval, 8u) == -1);
return result;
}

original_code:
int
sock_set_nonblock(int sock)
{
  struct linger linger_val = { 1, 1 };
  int flags;
  if ((flags = fcntl(sock, 3, 0)) == -1 || fcntl(sock, 4, flags | 04000) == -1)
  return -1;
  if (setsockopt(sock, 1, 13, (void *)&linger_val, sizeof(linger_val)) == -1)
  return -1;
  return 0;
}

variable_types: sock int

variable_types: struct linger { int l_onoff; int l_linger; }

variable_types: flags int

return_type: int


decompiled_code:
__int64 __fastcall sock_get_error(unsigned int a1)
{
char *v2; // rax
unsigned int optval; // [rsp+8h] [rbp-10h] BYREF
socklen_t optlen[3]; // [rsp+Ch] [rbp-Ch] BYREF

optlen[0] = 4;
if ( getsockopt(a1, 1, 4, &optval, optlen) >= 0 )
return optval;
v2 = strerror(optval);
logger(2LL, "getsockopt(SO_ERROR) failed on socket(%d) %d: %s", a1, optval, v2);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
char *v2; // rax
unsigned int optval; // [rsp+8h] [rbp-10h] BYREF
socklen_t optlen[3]; // [rsp+Ch] [rbp-Ch] BYREF

optlen[0] = 4;
if ( getsockopt(a1, 1, 4, &optval, optlen) >= 0 )
return optval;
v2 = strerror(optval);
logger(2LL, "getsockopt(SO_ERROR) failed on socket(%d) %d: %s", a1, optval, v2);
return 0xFFFFFFFFLL;
}

original_code:
int
sock_get_error(int sock)
{
  int errv, errlen = sizeof(errv);
  if (getsockopt(sock, 1, 4, &errv, &errlen) < 0) {
  logger(2, "getsockopt(SO_ERROR) failed on socket(%d) %d: %s", sock, errv, strerror(errv));
  return -1;
  }
  return errv;
}

canonical_original_code:
int sock_get_error(int sock)
{
  int errv; int errlen = sizeof(errv);
  if (getsockopt(sock, 1, 4, &errv, &errlen) < 0) {
  logger(2, "getsockopt(SO_ERROR) failed on socket(%d) %d: %s", sock, errv, strerror(errv));
  return -1;
  }
  return errv;
}

variable_types: errv int

variable_types: errlen int


sendhub

decompiled_code:
__int64 sendhub(char *format, ...)
{
size_t v1; // rax
size_t v2; // rax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x4000uLL, format, arg);
v1 = strlen(s);
if ( write(*((_DWORD *)&me + 16), s, v1) == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
result = logger(5LL, "error writing to hub %s: %s", *((const char **)&me + 12), v5);
}
else
{
v2 = strlen(s);
if ( v2 > 2 )
{
if ( s[v2] == 10 )
{
*((_BYTE *)&arg[0].reg_save_area + v2 + 7) = 0;
v2 = strlen(s);
}
if ( s[v2] == 13 )
*((_BYTE *)&arg[0].reg_save_area + v2 + 7) = 0;
}
result = debug(2LL, "sendhub>: %s", s);
}
return result;
}

canonical_decompiled_code:
__int64 func4(char *format, ...)
{
size_t v1; // rax
size_t v2; // rax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x4000uLL, format, arg);
v1 = strlen(s);
if ( write(*((_DWORD *)&me + 16), s, v1) == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
result = logger(5LL, "error writing to hub %s: %s", *((const char **)&me + 12), v5);
}
else
{
v2 = strlen(s);
if ( v2 > 2 )
{
if ( s[v2] == 10 )
{
*((_BYTE *)&arg[0].reg_save_area + v2 + 7) = 0;
v2 = strlen(s);
}
if ( s[v2] == 13 )
*((_BYTE *)&arg[0].reg_save_area + v2 + 7) = 0;
}
result = debug(2LL, "sendhub>: %s", s);
}
return result;
}

original_code:
void
sendhub(char *fmt, ...)
{
 char msgbuf[16384];
 va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(msgbuf, 16384, fmt, ap);
 if (write(me.hubsock, msgbuf, strlen(msgbuf)) == -1) {
  logger(5, "error writing to hub %s: %s", me.hubserver, strerror((*__errno_location ())));
  return;
 }
 if (strlen(msgbuf) > 2) {
  if (msgbuf[strlen(msgbuf)] == '\n')
   msgbuf[strlen(msgbuf) - 1] = 0;
  if (msgbuf[strlen(msgbuf)] == '\r')
   msgbuf[strlen(msgbuf) - 1] = 0;
 }
 debug(2, "sendhub>: %s", msgbuf);
 __builtin_va_end(ap);
}

canonical_original_code:
void sendhub(char *fmt, ...)
{
 char msgbuf[16384];
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(msgbuf, 16384, fmt, ap);
 if (write(me.hubsock, msgbuf, strlen(msgbuf)) == -1) {
  logger(5, "error writing to hub %s: %s", me.hubserver, strerror((*__errno_location ())));
  return;
 }
 if (strlen(msgbuf) > 2) {
  if (msgbuf[strlen(msgbuf)] == '\n')
   msgbuf[strlen(msgbuf) - 1] = 0;
  if (msgbuf[strlen(msgbuf)] == '\r')
   msgbuf[strlen(msgbuf) - 1] = 0;
 }
 debug(2, "sendhub>: %s", msgbuf);
 __builtin_va_end(ap);
}

variable_types:
fmt
char *

variable_types:
msgbuf
char [16384]

return_type: void


sreply

decompiled_code:
__int64 sreply(int a1, const char *a2, const char *a3, ...)
{
__int64 result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-40E0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40C8h] BYREF

result = 0LL;
if ( (unsigned int)(a1 - 1) <= 4 )
{
va_start(arg, a3);
vsnprintf(s, 0x4000uLL, a3, arg);
sendhub(":%s NOTICE %s :%s\r\n", *((const char **)&sclients + 4 * a1), a2, s);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func5(int a1, const char *a2, const char *a3, ...)
{
__int64 result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-40E0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40C8h] BYREF

result = 0LL;
if ( (unsigned int)(a1 - 1) <= 4 )
{
va_start(arg, a3);
vsnprintf(s, 0x4000uLL, a3, arg);
func4(":%s NOTICE %s :%s\r\n", *((const char **)&sclients + 4 * a1), a2, s);
result = 1LL;
}
return result;
}

original_code:
int
sreply(int sc, const char *to, char *fmt, ...)
{
 char msgbuf[16384];
 va_list ap;
 if (sc < 1 || sc > 5)
  return 0;
 __builtin_va_start(ap,fmt);
 vsnprintf(msgbuf, 16384, fmt, ap);
 sendhub(":%s NOTICE %s :%s\r\n", sclients[sc].nick, to, msgbuf);
 __builtin_va_end(ap);
 return 1;
}

canonical_original_code:
int sreply(int sc, char *to, char *fmt, ...)
{
 char msgbuf[16384];
 __builtin_va_list ap;
 if (sc < 1 || sc > 5)
  return 0;
 __builtin_va_start(ap,fmt);
 vsnprintf(msgbuf, 16384, fmt, ap);
 sendhub(":%s NOTICE %s :%s\r\n", sclients[sc].nick, to, msgbuf);
 __builtin_va_end(ap);
 return 1;
}

variable_types: sc int

variable_types:
to
char *


aborthub

decompiled_code:
__int64 aborthub(char *format, ...)
{
__time_t v1; // rax
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x4000uLL, format, arg);
logger(5LL, "disconnecting from hub %s: %s", *((const char **)&me + 12), s);
close(*((_DWORD *)&me + 16));
client_list_delete();
channel_list_delete();
v1 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v1 + 30;
return logger(6LL, "reconnecting to hub in 30 seconds");
}

canonical_decompiled_code:
__int64 func6(char *format, ...)
{
__time_t v1; // rax
gcc_va_list arg; // [rsp+8h] [rbp-40D0h] BYREF
char s[16384]; // [rsp+20h] [rbp-40B8h] BYREF

va_start(arg, format);
vsnprintf(s, 0x4000uLL, format, arg);
logger(5LL, "disconnecting from hub %s: %s", *((const char **)&me + 12), s);
close(*((_DWORD *)&me + 16));
client_list_delete();
channel_list_delete();
v1 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v1 + 30;
return logger(6LL, "reconnecting to hub in 30 seconds");
}

original_code:
void
aborthub(char *fmt, ...)
{
 char reason[16384];
 va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(reason, 16384, fmt, ap);
 logger(5, "disconnecting from hub %s: %s", me.hubserver, reason);
 __builtin_va_end(ap);
 close(me.hubsock);
 client_list_delete();
 channel_list_delete();
 me.hubstatus = -1;
 me.hubwait = me.now.tv_sec + 30;
 logger(6, "reconnecting to hub in 30 seconds");
}

canonical_original_code:
void aborthub(char *fmt, ...)
{
 char reason[16384];
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 vsnprintf(reason, 16384, fmt, ap);
 logger(5, "disconnecting from hub %s: %s", me.hubserver, reason);
 __builtin_va_end(ap);
 close(me.hubsock);
 client_list_delete();
 channel_list_delete();
 me.hubstatus = -1;
 me.hubwait = me.now.tv_sec + 30;
 logger(6, "reconnecting to hub in 30 seconds");
}

variable_types:
reason
char [16384]


decompiled_code:
void __noreturn io_loop()
{
__int16 v0; // bx
int v1; // eax
const char *v2; // rdi
in_addr_t v3; // eax
__int16 v4; // ax
socklen_t v5; // edx
__int64 v6; // rbp
__int64 v7; // r15
int v8; // ebp
int v9; // eax
int v10; // edi
int v11; // eax
int v12; // eax
unsigned int v13; // eax
unsigned int v14; // ebp
int v15; // er9
int v16; // er8
int v17; // eax
int v18; // ebp
int v19; // ecx
time_t v20; // rax
int v21; // eax
int v22; // edx
int *v23; // rax
int v24; // edi
unsigned int *v25; // rbp
char *v26; // rax
int v27; // eax
const char *v28; // rsi
int v29; // eax
int v30; // edx
signed int v31; // edi
int v32; // eax
int v33; // edi
char *v34; // rax
__time_t v35; // rax
__time_t v36; // rax
int v37; // edi
char *v38; // rax
__time_t v39; // rax
int *v40; // rax
char *v41; // rax
int v42; // [rsp+8h] [rbp-4270h]
int v43; // [rsp+8h] [rbp-4270h]
int v44; // [rsp+Ch] [rbp-426Ch]
struct sockaddr v45; // [rsp+10h] [rbp-4268h] BYREF
struct timeval timeout; // [rsp+20h] [rbp-4258h] BYREF
fd_set readfds; // [rsp+30h] [rbp-4248h] BYREF
fd_set writefds; // [rsp+B0h] [rbp-41C8h] BYREF
fd_set exceptfds; // [rsp+130h] [rbp-4148h] BYREF
struct stat buf; // [rsp+1B0h] [rbp-40C8h] BYREF
char s[16440]; // [rsp+240h] [rbp-4038h] BYREF

v0 = 0;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
memset(&exceptfds, 0, sizeof(exceptfds));
gettimeofday(&me, 0LL);
v1 = *((_DWORD *)&me + 15);
if ( v1 == 4000 )
{
v29 = *((_DWORD *)&me + 16);
v30 = v29 + 63;
if ( v29 >= 0 )
v30 = *((_DWORD *)&me + 16);
writefds.fds_bits[v30 >> 6] |= 1LL << (*((_DWORD *)&me + 16) % 64);
}
else if ( v1 > 4000 )
{
if ( v1 != 6000 && v1 != 10000 )
{
if ( v1 != 5000 )
goto LABEL_8;
logger(5LL, "connected to hub %s", *((const char **)&me + 12));
sendhub("CAPAB :CAP ENCAP QS KLN GLN UNKLN KNOCK TB UNKLN CLUSTER SERVICES RSFNC HUB\r\n");
sendhub("PASS %s :TS\r\n", *((const char **)&me + 10));
sendhub("SERVER %s 0 :%s\r\n", *((const char **)&me + 4), *((const char **)&me + 6));
v20 = time(0LL);
sendhub("SVINFO 5 5 0 :%lu\r\n", v20);
server_create(0LL, *((_QWORD *)&me + 4), *((_QWORD *)&me + 6), 1LL);
services_intro(0LL);
sendhub("PING :%s\r\n", *((const char **)&me + 4));
*((_DWORD *)&me + 15) = 6000;
}
v21 = *((_DWORD *)&me + 16);
v22 = v21 + 63;
if ( v21 >= 0 )
v22 = *((_DWORD *)&me + 16);
readfds.fds_bits[v22 >> 6] |= 1LL << (*((_DWORD *)&me + 16) % 64);
}
else if ( v1 )
{
if ( v1 == 1000 )
{
logger(5LL, "connecting to hub %s:%d", *((const char **)&me + 12), *((unsigned int *)&me + 22));
v2 = (const char *)*((_QWORD *)&me + 9);
v45 = 0LL;
v45.sa_family = 2;
v3 = inet_addr(v2);
LOWORD(v2) = *((_WORD *)&me + 44);
*(_DWORD *)&v45.sa_data[2] = v3;
v4 = _bswap_16((__int16)v2);
LODWORD(v2) = *((_DWORD *)&me + 16);
*(_WORD *)v45.sa_data = v4;
if ( connect((int)v2, &v45, v5) != -1 || (v37 = *_errno_location(), v37 == 115) )
{
*((_DWORD *)&me + 15) = 4000;
}
else
{
v38 = strerror(v37);
logger(5LL, "io_loop: connect to hub %s failed: %s", *((const char **)&me + 12), v38);
v39 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v39 + 30;
}
}
else if ( v1 == -1 && me.tv_sec > *((_QWORD *)&me + 13) )
{
*((_QWORD *)&me + 13) = 0LL;
*((_DWORD *)&me + 15) = 0;
}
}
else
{
memset((void *)(unsigned int)((_DWORD)&me + 112), 0, 0x4000uLL);
*((_DWORD *)&me + 4124) = 0;
v27 = socket(2, 1, 0);
v28 = "io_loop: cannot allocate hub socket!";
*((_DWORD *)&me + 16) = v27;
if ( v27 != -1 )
{
if ( (unsigned int)sock_set_nonblock(v27) != -1 )
{
*((_DWORD *)&me + 15) = 1000;
goto LABEL_8;
}
v28 = "io_loop: cannot set hub socket non-blocking!";
}
logger(2LL, v28);
v36 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v36 + 30;
}
LABEL_8:
v6 = sock_list;
if ( sock_list )
{
while ( 1 )
{
if ( v0 == 1 )
goto LABEL_15;
LABEL_10:
while ( (*(_BYTE *)(v6 + 20) & 1) != 0 )
{
while ( 1 )
{
LABEL_13:
close(*(_DWORD *)(v6 + 16));
v7 = *(_QWORD *)(v6 + 8);
sock_del((void *)v6);
if ( !v7 )
goto LABEL_17;
v6 = v7;
if ( v0 != 1 )
break;
LABEL_15:
logger(5LL, "io_loop: checking fds");
if ( fstat(*(_DWORD *)(v6 + 16), &buf) != -1 )
goto LABEL_10;
logger(5LL, "io_loop: sock %d had closed fd", *(unsigned int *)(v6 + 16));
}
}
if ( me.tv_sec - *(_QWORD *)(v6 + 24) > 300 )
{
logger(5LL, "disconnecting idle socket");
goto LABEL_13;
}
}
}
LABEL_17:
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
debug(6LL, "io_loop: before select()");
v8 = select(1024, &readfds, &writefds, &exceptfds, &timeout);
debug(6LL, "io_loop: after select()");
if ( v8 == -1 )
{
v23 = _errno_location();
v24 = *v23;
v25 = (unsigned int *)v23;
if ( *v23 == 9 )
{
v0 = 1;
}
else if ( v24 != 4 )
{
v26 = strerror(v24);
logger(2LL, "io_loop: select() returned error %d: %s", *v25, v26);
}
}
else if ( v8 )
{
v9 = *((_DWORD *)&me + 15);
if ( v9 == 4000 )
{
v31 = *((_DWORD *)&me + 16);
v32 = v31 + 63;
if ( v31 >= 0 )
v32 = *((_DWORD *)&me + 16);
if ( (writefds.fds_bits[v32 >> 6] & (1LL << (*((_DWORD *)&me + 16) % 64))) != 0 )
{
v33 = sock_get_error(v31);
if ( v33 )
{
v34 = strerror(v33);
logger(5LL, "connection to hub %s failed: %s", *((const char **)&me + 12), v34);
v35 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v35 + 30;
}
else
{
*((_DWORD *)&me + 15) = 5000;
}
}
}
else if ( v9 > 5999 )
{
v10 = *((_DWORD *)&me + 16);
v11 = v10 + 63;
if ( v10 >= 0 )
v11 = *((_DWORD *)&me + 16);
if ( (readfds.fds_bits[v11 >> 6] & (1LL << (*((_DWORD *)&me + 16) % 64))) != 0 )
{
v12 = *((_DWORD *)&me + 4124);
if ( v12 > 16380 )
{
aborthub("recvq exceeded");
}
else
{
v13 = read(v10, (char *)&me + v12 + 112, 0x4000 - v12);
v14 = v13;
if ( v13 == -1 )
{
v40 = _errno_location();
v41 = strerror(*v40);
aborthub("read error: %s", v41);
}
else if ( v13 )
{
debug(6LL, "io_loop: read %d bytes from hub", v13);
v15 = 0;
v16 = 0;
v17 = v14 + *((_DWORD *)&me + 4124);
v18 = 0;
*((_DWORD *)&me + 4124) = v17;
while ( v17 > v18 )
{
v19 = v18 + 1;
if ( *((_BYTE *)&me + v18 + 112) == 13 && *((_BYTE *)&me + v19 + 112) == 10 )
{
v42 = v15;
*((_BYTE *)&me + v18 + 112) = 0;
strlcpy(s, (char *)&me + v16 + 112, 0x4000LL);
parsehub(s);
v16 = v18 + 2;
v19 = v18 + 2;
v15 = v42 + 1;
if ( v18 + 2 > 16382 )
goto LABEL_34;
}
else if ( v19 > 16382 )
{
goto LABEL_34;
}
v17 = *((_DWORD *)&me + 4124);
v18 = v19;
}
v19 = v18;
LABEL_34:
v44 = v19;
v43 = v16;
if ( v15 )
{
memset(s, 0, 0x4000uLL);
memcpy(s, (char *)&me + v43 + 112, v44 - v43);
memcpy((void *)(unsigned int)((_DWORD)&me + 112), s, 0x4000uLL);
*((_DWORD *)&me + 4124) = v44 - v43;
}
}
else
{
aborthub("remote host closed the connection");
}
}
}
}
}
}
}

canonical_decompiled_code:
void __noreturn func7()
{
__int16 v0; // bx
int v1; // eax
const char *v2; // rdi
in_addr_t v3; // eax
__int16 v4; // ax
socklen_t v5; // edx
__int64 v6; // rbp
__int64 v7; // r15
int v8; // ebp
int v9; // eax
int v10; // edi
int v11; // eax
int v12; // eax
unsigned int v13; // eax
unsigned int v14; // ebp
int v15; // er9
int v16; // er8
int v17; // eax
int v18; // ebp
int v19; // ecx
time_t v20; // rax
int v21; // eax
int v22; // edx
int *v23; // rax
int v24; // edi
unsigned int *v25; // rbp
char *v26; // rax
int v27; // eax
const char *v28; // rsi
int v29; // eax
int v30; // edx
signed int v31; // edi
int v32; // eax
int v33; // edi
char *v34; // rax
__time_t v35; // rax
__time_t v36; // rax
int v37; // edi
char *v38; // rax
__time_t v39; // rax
int *v40; // rax
char *v41; // rax
int v42; // [rsp+8h] [rbp-4270h]
int v43; // [rsp+8h] [rbp-4270h]
int v44; // [rsp+Ch] [rbp-426Ch]
struct sockaddr v45; // [rsp+10h] [rbp-4268h] BYREF
struct timeval timeout; // [rsp+20h] [rbp-4258h] BYREF
fd_set readfds; // [rsp+30h] [rbp-4248h] BYREF
fd_set writefds; // [rsp+B0h] [rbp-41C8h] BYREF
fd_set exceptfds; // [rsp+130h] [rbp-4148h] BYREF
struct stat buf; // [rsp+1B0h] [rbp-40C8h] BYREF
char s[16440]; // [rsp+240h] [rbp-4038h] BYREF

v0 = 0;
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
memset(&exceptfds, 0, sizeof(exceptfds));
gettimeofday(&me, 0LL);
v1 = *((_DWORD *)&me + 15);
if ( v1 == 4000 )
{
v29 = *((_DWORD *)&me + 16);
v30 = v29 + 63;
if ( v29 >= 0 )
v30 = *((_DWORD *)&me + 16);
writefds.fds_bits[v30 >> 6] |= 1LL << (*((_DWORD *)&me + 16) % 64);
}
else if ( v1 > 4000 )
{
if ( v1 != 6000 && v1 != 10000 )
{
if ( v1 != 5000 )
goto LABEL_8;
logger(5LL, "connected to hub %s", *((const char **)&me + 12));
func4("CAPAB :CAP ENCAP QS KLN GLN UNKLN KNOCK TB UNKLN CLUSTER SERVICES RSFNC HUB\r\n");
func4("PASS %s :TS\r\n", *((const char **)&me + 10));
func4("SERVER %s 0 :%s\r\n", *((const char **)&me + 4), *((const char **)&me + 6));
v20 = time(0LL);
func4("SVINFO 5 5 0 :%lu\r\n", v20);
server_create(0LL, *((_QWORD *)&me + 4), *((_QWORD *)&me + 6), 1LL);
services_intro(0LL);
func4("PING :%s\r\n", *((const char **)&me + 4));
*((_DWORD *)&me + 15) = 6000;
}
v21 = *((_DWORD *)&me + 16);
v22 = v21 + 63;
if ( v21 >= 0 )
v22 = *((_DWORD *)&me + 16);
readfds.fds_bits[v22 >> 6] |= 1LL << (*((_DWORD *)&me + 16) % 64);
}
else if ( v1 )
{
if ( v1 == 1000 )
{
logger(5LL, "connecting to hub %s:%d", *((const char **)&me + 12), *((unsigned int *)&me + 22));
v2 = (const char *)*((_QWORD *)&me + 9);
v45 = 0LL;
v45.sa_family = 2;
v3 = inet_addr(v2);
LOWORD(v2) = *((_WORD *)&me + 44);
*(_DWORD *)&v45.sa_data[2] = v3;
v4 = func0((__int16)v2);
LODWORD(v2) = *((_DWORD *)&me + 16);
*(_WORD *)v45.sa_data = v4;
if ( connect((int)v2, &v45, v5) != -1 || (v37 = *_errno_location(), v37 == 115) )
{
*((_DWORD *)&me + 15) = 4000;
}
else
{
v38 = strerror(v37);
logger(5LL, "io_loop: connect to hub %s failed: %s", *((const char **)&me + 12), v38);
v39 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v39 + 30;
}
}
else if ( v1 == -1 && me.tv_sec > *((_QWORD *)&me + 13) )
{
*((_QWORD *)&me + 13) = 0LL;
*((_DWORD *)&me + 15) = 0;
}
}
else
{
memset((void *)(unsigned int)((_DWORD)&me + 112), 0, 0x4000uLL);
*((_DWORD *)&me + 4124) = 0;
v27 = socket(2, 1, 0);
v28 = "io_loop: cannot allocate hub socket!";
*((_DWORD *)&me + 16) = v27;
if ( v27 != -1 )
{
if ( (unsigned int)func2(v27) != -1 )
{
*((_DWORD *)&me + 15) = 1000;
goto LABEL_8;
}
v28 = "io_loop: cannot set hub socket non-blocking!";
}
logger(2LL, v28);
v36 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v36 + 30;
}
LABEL_8:
v6 = sock_list;
if ( sock_list )
{
while ( 1 )
{
if ( v0 == 1 )
goto LABEL_15;
LABEL_10:
while ( (*(_BYTE *)(v6 + 20) & 1) != 0 )
{
while ( 1 )
{
LABEL_13:
close(*(_DWORD *)(v6 + 16));
v7 = *(_QWORD *)(v6 + 8);
func1((void *)v6);
if ( !v7 )
goto LABEL_17;
v6 = v7;
if ( v0 != 1 )
break;
LABEL_15:
logger(5LL, "io_loop: checking fds");
if ( fstat(*(_DWORD *)(v6 + 16), &buf) != -1 )
goto LABEL_10;
logger(5LL, "io_loop: sock %d had closed fd", *(unsigned int *)(v6 + 16));
}
}
if ( me.tv_sec - *(_QWORD *)(v6 + 24) > 300 )
{
logger(5LL, "disconnecting idle socket");
goto LABEL_13;
}
}
}
LABEL_17:
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
debug(6LL, "io_loop: before select()");
v8 = select(1024, &readfds, &writefds, &exceptfds, &timeout);
debug(6LL, "io_loop: after select()");
if ( v8 == -1 )
{
v23 = _errno_location();
v24 = *v23;
v25 = (unsigned int *)v23;
if ( *v23 == 9 )
{
v0 = 1;
}
else if ( v24 != 4 )
{
v26 = strerror(v24);
logger(2LL, "io_loop: select() returned error %d: %s", *v25, v26);
}
}
else if ( v8 )
{
v9 = *((_DWORD *)&me + 15);
if ( v9 == 4000 )
{
v31 = *((_DWORD *)&me + 16);
v32 = v31 + 63;
if ( v31 >= 0 )
v32 = *((_DWORD *)&me + 16);
if ( (writefds.fds_bits[v32 >> 6] & (1LL << (*((_DWORD *)&me + 16) % 64))) != 0 )
{
v33 = func3(v31);
if ( v33 )
{
v34 = strerror(v33);
logger(5LL, "connection to hub %s failed: %s", *((const char **)&me + 12), v34);
v35 = me.tv_sec;
*((_DWORD *)&me + 15) = -1;
*((_QWORD *)&me + 13) = v35 + 30;
}
else
{
*((_DWORD *)&me + 15) = 5000;
}
}
}
else if ( v9 > 5999 )
{
v10 = *((_DWORD *)&me + 16);
v11 = v10 + 63;
if ( v10 >= 0 )
v11 = *((_DWORD *)&me + 16);
if ( (readfds.fds_bits[v11 >> 6] & (1LL << (*((_DWORD *)&me + 16) % 64))) != 0 )
{
v12 = *((_DWORD *)&me + 4124);
if ( v12 > 16380 )
{
func6("recvq exceeded");
}
else
{
v13 = read(v10, (char *)&me + v12 + 112, 0x4000 - v12);
v14 = v13;
if ( v13 == -1 )
{
v40 = _errno_location();
v41 = strerror(*v40);
func6("read error: %s", v41);
}
else if ( v13 )
{
debug(6LL, "io_loop: read %d bytes from hub", v13);
v15 = 0;
v16 = 0;
v17 = v14 + *((_DWORD *)&me + 4124);
v18 = 0;
*((_DWORD *)&me + 4124) = v17;
while ( v17 > v18 )
{
v19 = v18 + 1;
if ( *((_BYTE *)&me + v18 + 112) == 13 && *((_BYTE *)&me + v19 + 112) == 10 )
{
v42 = v15;
*((_BYTE *)&me + v18 + 112) = 0;
strlcpy(s, (char *)&me + v16 + 112, 0x4000LL);
parsehub(s);
v16 = v18 + 2;
v19 = v18 + 2;
v15 = v42 + 1;
if ( v18 + 2 > 16382 )
goto LABEL_34;
}
else if ( v19 > 16382 )
{
goto LABEL_34;
}
v17 = *((_DWORD *)&me + 4124);
v18 = v19;
}
v19 = v18;
LABEL_34:
v44 = v19;
v43 = v16;
if ( v15 )
{
memset(s, 0, 0x4000uLL);
memcpy(s, (char *)&me + v43 + 112, v44 - v43);
memcpy((void *)(unsigned int)((_DWORD)&me + 112), s, 0x4000uLL);
*((_DWORD *)&me + 4124) = v44 - v43;
}
}
else
{
func6("remote host closed the connection");
}
}
}
}
}
}
}

original_code:
void
io_loop()
{
 fd_set readfds, writefds, exceptfds;
 struct sockaddr_in sin;
 int errv, r;
 u_short checkfds = 0;
 struct timeval tv;
 struct stat sb;
 sock *s, *t;
 char buf[16384];
 int i, d, x;
loop:
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__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" (&((&writefds)->__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" (&((&exceptfds)->__fds_bits)[0]) : "memory"); } while (0);
 gettimeofday(&me.now, ((void *)0));
 switch (me.hubstatus) {
  case -1:
   if (me.now.tv_sec > me.hubwait) {
    me.hubwait = 0;
    me.hubstatus = 0;
   }
   break;
  case 0:
   memset(&me.hub_read_buf, 0, 16384);
   me.hub_read_len = 0;
   if ((me.hubsock = socket(2, SOCK_STREAM, 0)) == -1) {
    logger(2, "io_loop: cannot allocate hub socket!");
    me.hubstatus = -1;
    me.hubwait = me.now.tv_sec + 30;
    break;
   }
   if (sock_set_nonblock(me.hubsock) == -1) {
    logger(2, "io_loop: cannot set hub socket non-blocking!");
    me.hubstatus = -1;
    me.hubwait = me.now.tv_sec + 30;
    break;
   }
   me.hubstatus = 1000;
   break;
  case 1000:
   logger(5, "connecting to hub %s:%d", me.hubserver, me.hubport);
   memset(&sin, 0, sizeof(sin));
   sin.sin_family = 2;
   sin.sin_addr.s_addr = inet_addr(me.hubhost);
   sin.sin_port = htons(me.hubport);
   if (connect(me.hubsock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && (*__errno_location ()) != 115) {
    logger(5, "io_loop: connect to hub %s failed: %s", me.hubserver, strerror((*__errno_location ())));
    me.hubstatus = -1;
    me.hubwait = me.now.tv_sec + 30;
    break;
   }
   me.hubstatus = 4000;
   break;
  case 4000:
   ((void) (((&writefds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))));
   break;
  case 5000:
   logger(5, "connected to hub %s", me.hubserver);
   sendhub("CAPAB :CAP ENCAP QS KLN GLN UNKLN KNOCK TB UNKLN CLUSTER SERVICES RSFNC HUB\r\n");
   sendhub("PASS %s :TS\r\n", me.hubpasswd);
   sendhub("SERVER %s 0 :%s\r\n", me.servername, me.serverdesc);
   sendhub("SVINFO 5 5 0 :%lu\r\n", time(((void *)0)));
   server_create(((void *)0), me.servername, me.serverdesc, 0x00000001);
   services_intro(0);
   sendhub("PING :%s\r\n", me.servername);
   me.hubstatus = 6000;
   ((void) (((&readfds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))));
   break;
  case 6000:
  case 10000:
   ((void) (((&readfds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))));
   break;
 }
 for (s = sock_list; s;) {
  if (checkfds == 1) {
   logger(5, "io_loop: checking fds");
   if (fstat(s->sock_fd, &sb) == -1) {
    logger(5, "io_loop: sock %d had closed fd", s->sock_fd);
    goto delsock;
   }
  }
  if (s->sock_flags & 0x0001) {
  delsock:
   close(s->sock_fd);
   t = s->next;
   sock_del(s);
   s = t;
   continue;
  }
  if (me.now.tv_sec - s->sock_last > 300) {
   logger(5, "disconnecting idle socket");
   goto delsock;
  }
 }
 tv = (struct timeval){ 1, 0 };
 debug(6, "io_loop: before select()");
 r = select(1024, &readfds, &writefds, &exceptfds, &tv);
 debug(6, "io_loop: after select()");
 switch (r) {
  case -1:
   if ((*__errno_location ()) == 9)
    checkfds = 1;
   else if ((*__errno_location ()) != 4)
    logger(2, "io_loop: select() returned error %d: %s", (*__errno_location ()), strerror((*__errno_location ())));
   goto loop;
  case 0:
   goto loop;
 }
 if (me.hubstatus == 4000 && ((((&writefds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  if ((errv = sock_get_error(me.hubsock)) != 0) {
   logger(5, "connection to hub %s failed: %s", me.hubserver, strerror(errv));
   me.hubstatus = -1;
   me.hubwait = me.now.tv_sec + 30;
   goto loop;
  }
  me.hubstatus = 5000;
 } else if (me.hubstatus >= 6000 && ((((&readfds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  if (me.hub_read_len >= 16384 - 3) {
   aborthub("recvq exceeded");
   goto loop;
  }
  switch ((r = read(me.hubsock, &me.hub_read_buf[me.hub_read_len], 16384 - me.hub_read_len))) {
   case -1:
    aborthub("read error: %s", strerror((*__errno_location ())));
    goto loop;
   case 0:
    aborthub("remote host closed the connection");
    goto loop;
   default:
    debug(6, "io_loop: read %d bytes from hub", r);
    me.hub_read_len += r;
  }
  for (i = d = x = 0; i+1 < 16384 && i+1 <= me.hub_read_len; i++) {
   if (me.hub_read_buf[i] == '\r' && me.hub_read_buf[i+1] == '\n') {
    me.hub_read_buf[i] = 0;
    strlcpy(buf, &me.hub_read_buf[d], 16384);
    parsehub(buf);
    d = i + 2;
    i++;
    x++;
   }
  }
  if (x > 0) {
   memset(buf, 0, 16384);
   memcpy(buf, &me.hub_read_buf[d], (i - d));
   memcpy(&me.hub_read_buf, buf, 16384);
   me.hub_read_len = (i - d);
  }
 }
 goto loop;
}

canonical_original_code:
void io_loop()
{
 struct fd_set readfds; struct fd_set writefds; struct fd_set exceptfds;
 struct sockaddr_in sin;
 int errv; int r;
 unsigned short checkfds = 0;
 struct timeval tv;
 struct stat sb;
 struct sock_t *s; struct sock_t *t;
 char buf[16384];
 int i; int d; int x;
loop:
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__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" (&((&writefds)->__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" (&((&exceptfds)->__fds_bits)[0]) : "memory"); } while (0);
 gettimeofday(&me.now, ((void *)0));
 switch (me.hubstatus) {
  case -1:
   if (me.now.tv_sec > me.hubwait) {
    me.hubwait = 0;
    me.hubstatus = 0;
   }
   break;
  case 0:
   memset(&me.hub_read_buf, 0, 16384);
   me.hub_read_len = 0;
   if ((me.hubsock = socket(2, SOCK_STREAM, 0)) == -1) {
    logger(2, "io_loop: cannot allocate hub socket!");
    me.hubstatus = -1;
    me.hubwait = me.now.tv_sec + 30;
    break;
   }
   if (sock_set_nonblock(me.hubsock) == -1) {
    logger(2, "io_loop: cannot set hub socket non-blocking!");
    me.hubstatus = -1;
    me.hubwait = me.now.tv_sec + 30;
    break;
   }
   me.hubstatus = 1000;
   break;
  case 1000:
   logger(5, "connecting to hub %s:%d", me.hubserver, me.hubport);
   memset(&sin, 0, sizeof(sin));
   sin.sin_family = 2;
   sin.sin_addr.s_addr = inet_addr(me.hubhost);
   sin.sin_port = htons(me.hubport);
   if (connect(me.hubsock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && (*__errno_location ()) != 115) {
    logger(5, "io_loop: connect to hub %s failed: %s", me.hubserver, strerror((*__errno_location ())));
    me.hubstatus = -1;
    me.hubwait = me.now.tv_sec + 30;
    break;
   }
   me.hubstatus = 4000;
   break;
  case 4000:
   ((void) (((&writefds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))));
   break;
  case 5000:
   logger(5, "connected to hub %s", me.hubserver);
   sendhub("CAPAB :CAP ENCAP QS KLN GLN UNKLN KNOCK TB UNKLN CLUSTER SERVICES RSFNC HUB\r\n");
   sendhub("PASS %s :TS\r\n", me.hubpasswd);
   sendhub("SERVER %s 0 :%s\r\n", me.servername, me.serverdesc);
   sendhub("SVINFO 5 5 0 :%lu\r\n", time(((void *)0)));
   server_create(((void *)0), me.servername, me.serverdesc, 0x00000001);
   services_intro(0);
   sendhub("PING :%s\r\n", me.servername);
   me.hubstatus = 6000;
   ((void) (((&readfds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))));
   break;
  case 6000:
  case 10000:
   ((void) (((&readfds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))));
   break;
 }
 for (s = sock_list; s;) {
  if (checkfds == 1) {
   logger(5, "io_loop: checking fds");
   if (fstat(s->sock_fd, &sb) == -1) {
    logger(5, "io_loop: sock %d had closed fd", s->sock_fd);
    goto delsock;
   }
  }
  if (s->sock_flags & 0x0001) {
  delsock:
   close(s->sock_fd);
   t = s->next;
   sock_del(s);
   s = t;
   continue;
  }
  if (me.now.tv_sec - s->sock_last > 300) {
   logger(5, "disconnecting idle socket");
   goto delsock;
  }
 }
 tv = (struct timeval){ 1, 0 };
 debug(6, "io_loop: before select()");
 r = select(1024, &readfds, &writefds, &exceptfds, &tv);
 debug(6, "io_loop: after select()");
 switch (r) {
  case -1:
   if ((*__errno_location ()) == 9)
    checkfds = 1;
   else if ((*__errno_location ()) != 4)
    logger(2, "io_loop: select() returned error %d: %s", (*__errno_location ()), strerror((*__errno_location ())));
   goto loop;
  case 0:
   goto loop;
 }
 if (me.hubstatus == 4000 && ((((&writefds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  if ((errv = sock_get_error(me.hubsock)) != 0) {
   logger(5, "connection to hub %s failed: %s", me.hubserver, strerror(errv));
   me.hubstatus = -1;
   me.hubwait = me.now.tv_sec + 30;
   goto loop;
  }
  me.hubstatus = 5000;
 } else if (me.hubstatus >= 6000 && ((((&readfds)->__fds_bits)[((me.hubsock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((me.hubsock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
  if (me.hub_read_len >= 16384 - 3) {
   aborthub("recvq exceeded");
   goto loop;
  }
  switch ((r = read(me.hubsock, &me.hub_read_buf[me.hub_read_len], 16384 - me.hub_read_len))) {
   case -1:
    aborthub("read error: %s", strerror((*__errno_location ())));
    goto loop;
   case 0:
    aborthub("remote host closed the connection");
    goto loop;
   default:
    debug(6, "io_loop: read %d bytes from hub", r);
    me.hub_read_len += r;
  }
  for (i = d = x = 0; i+1 < 16384 && i+1 <= me.hub_read_len; i++) {
   if (me.hub_read_buf[i] == '\r' && me.hub_read_buf[i+1] == '\n') {
    me.hub_read_buf[i] = 0;
    strlcpy(buf, &me.hub_read_buf[d], 16384);
    parsehub(buf);
    d = i + 2;
    i++;
    x++;
   }
  }
  if (x > 0) {
   memset(buf, 0, 16384);
   memcpy(buf, &me.hub_read_buf[d], (i - d));
   memcpy(&me.hub_read_buf, buf, 16384);
   me.hub_read_len = (i - d);
  }
 }
 goto loop;
}

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

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

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

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

variable_types: r int

variable_types:
checkfds
unsigned short

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

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

variable_types:
buf
char [16384]

variable_types: i int

variable_types: d int

variable_types: x int

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 stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct sock_t { struct sock_t *prev; struct sock_t *next; int sock_fd; int sock_flags; long sock_last; char sock_buf[16384]; unsigned long sock_buf_len; char *sock_buf_ptr; }

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


decompiled_code:
__int64 *client_list_insert()
{
__int64 *result; // rax
__int64 v1; // rdx

result = (__int64 *)lcalloc(72LL, 1LL);
v1 = client_list;
++me[2065];
*result = v1;
client_list = (__int64)result;
if ( v1 )
*(_QWORD *)(v1 + 8) = result;
return result;
}

canonical_decompiled_code:
__int64 *func0()
{
__int64 *result; // rax
__int64 v1; // rdx

result = (__int64 *)lcalloc(72LL, 1LL);
v1 = client_list;
++me[2065];
*result = v1;
client_list = (__int64)result;
if ( v1 )
*(_QWORD *)(v1 + 8) = result;
return result;
}

original_code:
client *
client_list_insert(void)
{
 client *c = lcalloc(sizeof(client), 1);
 me.clients++;
 c->next = client_list;
 client_list = c;
 if (c->next)
  c->next->prev = c;
 return c;
}

canonical_original_code:
struct _client *client_list_insert()
{
 struct _client *c = lcalloc(sizeof(client), 1);
 me.clients++;
 c->next = client_list;
 client_list = c;
 if (c->next)
  c->next->prev = c;
 return c;
}

variable_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *


decompiled_code:
__int64 __fastcall client_list_search(char *s2)
{
__int64 i; // r12

for ( i = client_list; i; i = *(_QWORD *)i )
{
if ( !strcasecmp(*(const char **)(i + 24), s2) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s2)
{
__int64 i; // r12

for ( i = client_list; i; i = *(_QWORD *)i )
{
if ( !strcasecmp(*(const char **)(i + 24), s2) )
break;
}
return i;
}

original_code:
client *
client_list_search(const char *nick)
{
 client *c = client_list;
 for (; c; c = c->next)
  if (strcasecmp(c->nick, nick) == 0)
   return c;
 return ((void *)0);
}

canonical_original_code:
struct _client *client_list_search(char *nick)
{
 struct _client *c = client_list;
 for (; c; c = c->next)
  if (strcasecmp(c->nick, nick) == 0)
   return c;
 return ((void *)0);
}

variable_types:
nick
char *

return_type: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *


decompiled_code:
__int64 __fastcall client_list_remove(__int64 *a1)
{
__int64 *v1; // rdx
__int64 result; // rax

v1 = (__int64 *)a1[1];
result = *a1;
if ( !v1 )
{
client_list = *a1;
if ( !result )
goto LABEL_4;
*(_QWORD *)(result + 8) = 0LL;
v1 = (__int64 *)a1[1];
goto LABEL_3;
}
*v1 = result;
if ( result )
LABEL_3:
*(_QWORD *)(result + 8) = v1;
LABEL_4:
--me[2065];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1)
{
__int64 *v1; // rdx
__int64 result; // rax

v1 = (__int64 *)a1[1];
result = *a1;
if ( !v1 )
{
client_list = *a1;
if ( !result )
goto LABEL_4;
*(_QWORD *)(result + 8) = 0LL;
v1 = (__int64 *)a1[1];
goto LABEL_3;
}
*v1 = result;
if ( result )
LABEL_3:
*(_QWORD *)(result + 8) = v1;
LABEL_4:
--me[2065];
return result;
}

original_code:
void
client_list_remove(client *c)
{
 if (c->prev) {
  c->prev->next = c->next;
 } else {
  client_list = c->next;
  if (c->next)
   c->next->prev = ((void *)0);
 }
 if (c->next)
  c->next->prev = c->prev;
 me.clients--;
}

canonical_original_code:
void client_list_remove(struct _client *c)
{
 if (c->prev) {
  c->prev->next = c->next;
 } else {
  client_list = c->next;
  if (c->next)
   c->next->prev = ((void *)0);
 }
 if (c->next)
  c->next->prev = c->prev;
 me.clients--;
}

user_defined_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; }


decompiled_code:
__int64 __fastcall user_list_search(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 v6; // rbx
int v10; // er15
__int64 v11; // rax

v6 = client_list;
if ( client_list && a2 > 0 )
{
v10 = 0;
do
{
if ( (*(_BYTE *)(v6 + 64) & 0x10) != 0
&& (unsigned int)match(a3, *(_QWORD *)(v6 + 24))
&& (unsigned int)match(a4, *(_QWORD *)(v6 + 32))
&& (unsigned int)match(a5, *(_QWORD *)(v6 + 40))
&& (unsigned int)match(a6, *(_QWORD *)(v6 + 48)) )
{
v11 = v10++;
*(_QWORD *)(a1 + 8 * v11) = v6;
}
v6 = *(_QWORD *)v6;
}
while ( v6 && v10 < a2 );
}
else
{
v10 = 0;
}
return (unsigned int)v10;
}

original_code:
int
user_list_search(client **results, int maxsearch, const char *nm, const char *um, const char *hm, const char *gm)
{
 client *c = client_list;
 int r = 0;
 for (; c && r < maxsearch; c = c->next)
  if ((c->flags & 0x00000010) && match(nm, c->nick) && match(um, c->user) && match(hm, c->host) && match(gm, c->name))
  results[r++] = c;
 return r;
}

canonical_original_code:
int user_list_search(struct _client **results, int maxsearch, char *nm, char *um, char *hm, char *gm)
{
 struct _client *c = client_list;
 int r = 0;
 for (; c && r < maxsearch; c = c->next)
  if ((c->flags & 0x00000010) && match(nm, c->nick) && match(um, c->user) && match(hm, c->host) && match(gm, c->name))
  results[r++] = c;
 return r;
}

variable_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } **

variable_types: maxsearch int

variable_types:
nm
char *

variable_types:
um
char *

variable_types:
hm
char *

variable_types:
gm
char *


decompiled_code:
__int64 __fastcall client_list_debug(const char *a1)
{
__int64 i; // rbx
__int64 result; // rax

for ( i = client_list; i; i = *(_QWORD *)i )
result = sendhub(
":%s NOTICE %s :%s!%s@%s$%s on %s, flags = %x\r\n",
(const char *)me[4],
a1,
*(const char **)(i + 24),
*(const char **)(i + 32),
*(const char **)(i + 40),
*(const char **)(i + 48),
*(const char **)(*(_QWORD *)(i + 16) + 24LL),
*(unsigned int *)(i + 64));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
__int64 i; // rbx
__int64 result; // rax

for ( i = client_list; i; i = *(_QWORD *)i )
result = sendhub(
":%s NOTICE %s :%s!%s@%s$%s on %s, flags = %x\r\n",
(const char *)me[4],
a1,
*(const char **)(i + 24),
*(const char **)(i + 32),
*(const char **)(i + 40),
*(const char **)(i + 48),
*(const char **)(*(_QWORD *)(i + 16) + 24LL),
*(unsigned int *)(i + 64));
return result;
}

original_code:
void
client_list_debug(const char *to)
{
 client *c = client_list;
 for (; c; c = c->next)
  sendhub(":%s NOTICE %s :%s!%s@%s$%s on %s, flags = %x\r\n", me.servername, to, c->nick, c->user, c->host, c->name, c->server->nick, c->flags);
}

canonical_original_code:
void client_list_debug(char *to)
{
 struct _client *c = client_list;
 for (; c; c = c->next)
  sendhub(":%s NOTICE %s :%s!%s@%s$%s on %s, flags = %x\r\n", me.servername, to, c->nick, c->user, c->host, c->name, c->server->nick, c->flags);
}

variable_types:
to
char *


decompiled_code:
_QWORD *channel_list_insert()
{
_QWORD *result; // rax
__int64 v1; // rdx

result = (_QWORD *)lcalloc(72LL, 1LL);
v1 = channel_list;
*result = channel_list;
channel_list = (__int64)result;
if ( v1 )
*(_QWORD *)(v1 + 8) = result;
++me[2066];
return result;
}

canonical_decompiled_code:
_QWORD *func5()
{
_QWORD *result; // rax
__int64 v1; // rdx

result = (_QWORD *)lcalloc(72LL, 1LL);
v1 = channel_list;
*result = channel_list;
channel_list = (__int64)result;
if ( v1 )
*(_QWORD *)(v1 + 8) = result;
++me[2066];
return result;
}

original_code:
channel *
channel_list_insert(void)
{
 channel *c = lcalloc(sizeof(channel), 1);
 c->next = channel_list;
 channel_list = c;
 if (c->next)
  c->next->prev = c;
 me.channels++;
 return c;
}

canonical_original_code:
struct _channel *channel_list_insert()
{
 struct _channel *c = lcalloc(sizeof(channel), 1);
 c->next = channel_list;
 channel_list = c;
 if (c->next)
  c->next->prev = c;
 me.channels++;
 return c;
}

variable_types: struct _channel { struct _channel *next; struct _channel *prev; char *name; long ts; struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *user; int flags; } *members; int flags; char *topic; char *key; int memberlimit; int membercount; } *

user_defined_types: struct _channel { struct _channel *next; struct _channel *prev; char *name; long ts; struct _cmember *members; int flags; char *topic; char *key; int memberlimit; int membercount; }

user_defined_types: struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client *user; int flags; }


decompiled_code:
__int64 __fastcall channel_list_search(char *s2)
{
__int64 i; // r12

for ( i = channel_list; i; i = *(_QWORD *)i )
{
if ( !strcasecmp(*(const char **)(i + 16), s2) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *s2)
{
__int64 i; // r12

for ( i = channel_list; i; i = *(_QWORD *)i )
{
if ( !strcasecmp(*(const char **)(i + 16), s2) )
break;
}
return i;
}

original_code:
channel *
channel_list_search(const char *name)
{
 channel *c = channel_list;
 for (; c; c = c->next)
  if (strcasecmp(c->name, name) == 0)
   return c;
 return ((void *)0);
}

canonical_original_code:
struct _channel *channel_list_search(char *name)
{
 struct _channel *c = channel_list;
 for (; c; c = c->next)
  if (strcasecmp(c->name, name) == 0)
   return c;
 return ((void *)0);
}

variable_types:
name
char *

return_type: struct _channel { struct _channel *next; struct _channel *prev; char *name; long ts; struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *user; int flags; } *members; int flags; char *topic; char *key; int memberlimit; int membercount; } *


decompiled_code:
__int64 __fastcall channel_list_remove(__int64 *a1)
{
__int64 *v1; // rdx
__int64 result; // rax

v1 = (__int64 *)a1[1];
result = *a1;
if ( !v1 )
{
channel_list = *a1;
if ( !result )
goto LABEL_4;
*(_QWORD *)(result + 8) = 0LL;
v1 = (__int64 *)a1[1];
goto LABEL_3;
}
*v1 = result;
if ( result )
LABEL_3:
*(_QWORD *)(result + 8) = v1;
LABEL_4:
--me[2066];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1)
{
__int64 *v1; // rdx
__int64 result; // rax

v1 = (__int64 *)a1[1];
result = *a1;
if ( !v1 )
{
channel_list = *a1;
if ( !result )
goto LABEL_4;
*(_QWORD *)(result + 8) = 0LL;
v1 = (__int64 *)a1[1];
goto LABEL_3;
}
*v1 = result;
if ( result )
LABEL_3:
*(_QWORD *)(result + 8) = v1;
LABEL_4:
--me[2066];
return result;
}

original_code:
void
channel_list_remove(channel *c)
{
 if (c->prev) {
  c->prev->next = c->next;
 } else {
  channel_list = c->next;
  if (c->next)
   c->next->prev = ((void *)0);
 }
 if (c->next)
  c->next->prev = c->prev;
 me.channels--;
}

canonical_original_code:
void channel_list_remove(struct _channel *c)
{
 if (c->prev) {
  c->prev->next = c->next;
 } else {
  channel_list = c->next;
  if (c->next)
   c->next->prev = ((void *)0);
 }
 if (c->next)
  c->next->prev = c->prev;
 me.channels--;
}


decompiled_code:
void __fastcall channel_list_debug(const char *a1)
{
__int64 i; // rbp
const char *v2; // rbx
const char *v3; // rax
__int64 **j; // rbx

for ( i = channel_list; i; i = *(_QWORD *)i )
{
v2 = *(const char **)(i + 48);
v3 = (const char *)channel_mode_display(i);
sendhub(
":%s NOTICE %s :%s %lu %d members: %s, topic is %s\r\n",
(const char *)me[4],
a1,
*(const char **)(i + 16),
*(_QWORD *)(i + 24),
*(unsigned int *)(i + 68),
v3,
v2);
for ( j = *(__int64 ***)(i + 32); j; j = (__int64 **)*j )
sendhub(
":%s NOTICE %s :%s has (%d)%s\r\n",
(const char *)me[4],
a1,
*(const char **)(i + 16),
*((unsigned int *)j + 6),
(const char *)j[2][3]);
}
}

canonical_decompiled_code:
void __fastcall func8(const char *a1)
{
__int64 i; // rbp
const char *v2; // rbx
const char *v3; // rax
__int64 **j; // rbx

for ( i = channel_list; i; i = *(_QWORD *)i )
{
v2 = *(const char **)(i + 48);
v3 = (const char *)channel_mode_display(i);
sendhub(
":%s NOTICE %s :%s %lu %d members: %s, topic is %s\r\n",
(const char *)me[4],
a1,
*(const char **)(i + 16),
*(_QWORD *)(i + 24),
*(unsigned int *)(i + 68),
v3,
v2);
for ( j = *(__int64 ***)(i + 32); j; j = (__int64 **)*j )
sendhub(
":%s NOTICE %s :%s has (%d)%s\r\n",
(const char *)me[4],
a1,
*(const char **)(i + 16),
*((unsigned int *)j + 6),
(const char *)j[2][3]);
}
}

original_code:
void
channel_list_debug(const char *to)
{
 channel *c = channel_list;
 cmember *m;
 for (; c; c = c->next) {
  sendhub(":%s NOTICE %s :%s %lu %d members: %s, topic is %s\r\n", me.servername, to, c->name, c->ts, c->membercount, channel_mode_display(c), c->topic);
  for (m = c->members; m; m = m->next)
   sendhub(":%s NOTICE %s :%s has (%d)%s\r\n", me.servername, to, c->name, m->flags, m->user->nick);
 }
}

canonical_original_code:
void channel_list_debug(char *to)
{
 struct _channel *c = channel_list;
 struct _cmember *m;
 for (; c; c = c->next) {
  sendhub(":%s NOTICE %s :%s %lu %d members: %s, topic is %s\r\n", me.servername, to, c->name, c->ts, c->membercount, channel_mode_display(c), c->topic);
  for (m = c->members; m; m = m->next)
   sendhub(":%s NOTICE %s :%s has (%d)%s\r\n", me.servername, to, c->name, m->flags, m->user->nick);
 }
}

variable_types: struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *user; int flags; } *


decompiled_code:
__int64 __fastcall client_delete(__int64 a1)
{
_QWORD *v2; // rbx
_QWORD *v3; // rdi
__int64 v4; // r12
size_t v5; // rax
__int64 v6; // r12
size_t v7; // rax
__int64 v8; // r12
size_t v9; // rax
__int64 v10; // r12
size_t v11; // rax
_QWORD *v13; // rbx

debug(7LL, "client_delete: %s on %s", *(const char **)(a1 + 24), *(const char **)(*(_QWORD *)(a1 + 16) + 24LL));
if ( (*(_BYTE *)(a1 + 64) & 4) != 0 )
{
--me[2064];
v2 = (_QWORD *)client_list;
while ( v2 )
{
while ( 1 )
{
v3 = v2;
v2 = (_QWORD *)*v2;
if ( v3[2] != a1 )
break;
client_delete();
if ( !v2 )
goto LABEL_6;
}
}
}
else
{
v13 = (_QWORD *)channel_list;
if ( channel_list )
{
do
{
channel_member_del((__int64)v13, a1);
v13 = (_QWORD *)*v13;
}
while ( v13 );
}
}
LABEL_6:
v4 = *(_QWORD *)(a1 + 24);
if ( v4 )
{
v5 = strlen(*(const char **)(a1 + 24));
lfree(v4, v5 + 1);
}
v6 = *(_QWORD *)(a1 + 32);
if ( v6 )
{
v7 = strlen(*(const char **)(a1 + 32));
lfree(v6, v7 + 1);
}
v8 = *(_QWORD *)(a1 + 40);
if ( v8 )
{
v9 = strlen(*(const char **)(a1 + 40));
lfree(v8, v9 + 1);
}
v10 = *(_QWORD *)(a1 + 48);
if ( v10 )
{
v11 = strlen(*(const char **)(a1 + 48));
lfree(v10, v11 + 1);
}
client_list_remove((__int64 *)a1);
return lfree(a1, 72LL);
}

original_code:
void
client_delete(client *c)
{
 client *s, *t = ((void *)0);
 channel *chan;
 debug(7, "client_delete: %s on %s", c->nick, c->server->nick);
 if ((c->flags & 0x00000004)) {
  me.servers--;
  for (s = client_list; s; s = t) {
   t = s->next;
   if (s->server == c)
    client_delete(s);
  }
 } else {
  for (chan = channel_list; chan; chan = chan->next)
   channel_member_del(chan, c);
 }
 if (c->nick)
  lfree(c->nick, strlen(c->nick) + 1);
 if (c->user)
  lfree(c->user, strlen(c->user) + 1);
 if (c->host)
  lfree(c->host, strlen(c->host) + 1);
 if (c->name)
  lfree(c->name, strlen(c->name) + 1);
 client_list_remove(c);
 lfree(c, sizeof(client));
}

canonical_original_code:
void client_delete(struct _client *c)
{
 struct _client *s; struct _client *t = ((void *)0);
 struct _channel *chan;
 debug(7, "client_delete: %s on %s", c->nick, c->server->nick);
 if ((c->flags & 0x00000004)) {
  me.servers--;
  for (s = client_list; s; s = t) {
   t = s->next;
   if (s->server == c)
    client_delete(s);
  }
 } else {
  for (chan = channel_list; chan; chan = chan->next)
   channel_member_del(chan, c);
 }
 if (c->nick)
  lfree(c->nick, strlen(c->nick) + 1);
 if (c->user)
  lfree(c->user, strlen(c->user) + 1);
 if (c->host)
  lfree(c->host, strlen(c->host) + 1);
 if (c->name)
  lfree(c->name, strlen(c->name) + 1);
 client_list_remove(c);
 lfree(c, sizeof(client));
}


decompiled_code:
__int64 client_list_delete()
{
_QWORD *v0; // rbx
__int64 v1; // rdi
__int64 result; // rax

v0 = (_QWORD *)client_list;
if ( client_list )
{
do
{
v1 = (__int64)v0;
v0 = (_QWORD *)*v0;
result = client_delete(v1);
}
while ( v0 );
}
client_list = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
_QWORD *v0; // rbx
__int64 v1; // rdi
__int64 result; // rax

v0 = (_QWORD *)client_list;
if ( client_list )
{
do
{
v1 = (__int64)v0;
v0 = (_QWORD *)*v0;
result = func10(v1);
}
while ( v0 );
}
client_list = 0LL;
return result;
}

original_code:
void
client_list_delete(void)
{
 client *t, *c = client_list;
 for (; c; c = t) {
  t = c->next;
  client_delete(c);
 }
 client_list = ((void *)0);
}

canonical_original_code:
void client_list_delete()
{
 struct _client *t; struct _client *c = client_list;
 for (; c; c = t) {
  t = c->next;
  client_delete(c);
 }
 client_list = ((void *)0);
}


decompiled_code:
__int64 __fastcall user_modify(__int64 a1, char *a2)
{
__int64 v2; // rax

v2 = client_list_search(a2);
if ( v2 && v2 != a1 )
client_delete(v2);
*(_QWORD *)(a1 + 24) = lrestrdup(*(_QWORD *)(a1 + 24), a2);
return a1;
}

original_code:
client *
user_modify(client *c, const char *nick)
{
 client *o;
 if ((o = client_list_search(nick)) && c != o)
  client_delete(o);
 c->nick = lrestrdup(c->nick, nick);
 return c;
}

canonical_original_code:
struct _client *user_modify(struct _client *c, char *nick)
{
 struct _client *o;
 if ((o = client_list_search(nick)) && c != o)
  client_delete(o);
 c->nick = lrestrdup(c->nick, nick);
 return c;
}

variable_types:
c
struct _client *


decompiled_code:
__int64 __fastcall channel_delete(__int64 a1)
{
__int64 i; // rax
__int64 v2; // r12
size_t v3; // rax
__int64 v4; // r12
size_t v5; // rax

debug(3LL, "channel_delete: deleting %s", *(const char **)(a1 + 16));
for ( i = *(_QWORD *)(a1 + 32); i; i = *(_QWORD *)(a1 + 32) )
channel_member_del(a1, *(_QWORD *)(i + 16));
channel_list_remove((__int64 *)a1);
v2 = *(_QWORD *)(a1 + 16);
if ( v2 )
{
v3 = strlen(*(const char **)(a1 + 16));
lfree(v2, v3 + 1);
}
v4 = *(_QWORD *)(a1 + 56);
if ( v4 )
{
v5 = strlen(*(const char **)(a1 + 56));
lfree(v4, v5 + 1);
}
return lfree(a1, 72LL);
}

original_code:
void
channel_delete(channel *c)
{
 debug(3, "channel_delete: deleting %s", c->name);
 while (c->members)
  channel_member_del(c, c->members->user);
 channel_list_remove(c);
 if (c->name)
  lfree(c->name, strlen(c->name) + 1);
 if (c->key)
  lfree(c->key, strlen(c->key) + 1);
 lfree(c, sizeof(channel));
}

canonical_original_code:
void channel_delete(struct _channel *c)
{
 debug(3, "channel_delete: deleting %s", c->name);
 while (c->members)
  channel_member_del(c, c->members->user);
 channel_list_remove(c);
 if (c->name)
  lfree(c->name, strlen(c->name) + 1);
 if (c->key)
  lfree(c->key, strlen(c->key) + 1);
 lfree(c, sizeof(channel));
}


decompiled_code:
__int64 channel_list_delete()
{
_QWORD *v0; // rbx
__int64 v1; // rdi
__int64 result; // rax

v0 = (_QWORD *)channel_list;
if ( channel_list )
{
do
{
v1 = (__int64)v0;
v0 = (_QWORD *)*v0;
result = channel_delete(v1);
}
while ( v0 );
}
channel_list = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func14()
{
_QWORD *v0; // rbx
__int64 v1; // rdi
__int64 result; // rax

v0 = (_QWORD *)channel_list;
if ( channel_list )
{
do
{
v1 = (__int64)v0;
v0 = (_QWORD *)*v0;
result = func13(v1);
}
while ( v0 );
}
channel_list = 0LL;
return result;
}

original_code:
void
channel_list_delete(void)
{
 channel *t, *c = channel_list;
 for (; c; c = t) {
  t = c->next;
  channel_delete(c);
 }
 channel_list = ((void *)0);
}

canonical_original_code:
void channel_list_delete()
{
 struct _channel *t; struct _channel *c = channel_list;
 for (; c; c = t) {
  t = c->next;
  channel_delete(c);
 }
 channel_list = ((void *)0);
}


decompiled_code:
__int64 __fastcall channel_member_add(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // er12
__int64 result; // rax
__int64 v5; // rdx

v3 = a3;
debug(3LL, "channel_member_add: adding (%d) %s to %s", a3, *(const char **)(a2 + 24), *(const char **)(a1 + 16));
if ( channel_member_search(a1, a2) )
channel_member_del(a1, a2);
result = lcalloc(32LL, 1LL);
v5 = *(_QWORD *)(a1 + 32);
*(_QWORD *)result = v5;
*(_QWORD *)(a1 + 32) = result;
if ( v5 )
*(_QWORD *)(v5 + 8) = result;
*(_QWORD *)(result + 16) = a2;
*(_DWORD *)(result + 24) = v3;
++*(_DWORD *)(a1 + 68);
++*(_DWORD *)(a2 + 68);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // er12
__int64 result; // rax
__int64 v5; // rdx

v3 = a3;
debug(3LL, "channel_member_add: adding (%d) %s to %s", a3, *(const char **)(a2 + 24), *(const char **)(a1 + 16));
if ( channel_member_search(a1, a2) )
func9(a1, a2);
result = lcalloc(32LL, 1LL);
v5 = *(_QWORD *)(a1 + 32);
*(_QWORD *)result = v5;
*(_QWORD *)(a1 + 32) = result;
if ( v5 )
*(_QWORD *)(v5 + 8) = result;
*(_QWORD *)(result + 16) = a2;
*(_DWORD *)(result + 24) = v3;
++*(_DWORD *)(a1 + 68);
++*(_DWORD *)(a2 + 68);
return result;
}

original_code:
cmember *
channel_member_add(channel *chan, client *user, int flags)
{
 cmember *m;
 debug(3, "channel_member_add: adding (%d) %s to %s", flags, user->nick, chan->name);
 if ((m = channel_member_search(chan, user)))
  channel_member_del(chan, user);
 m = lcalloc(sizeof(cmember), 1);
 m->next = chan->members;
 chan->members = m;
 if (m->next)
  m->next->prev = m;
 m->user = user;
 m->flags = flags;
 chan->membercount++;
 user->channelcount++;
 return m;
}

canonical_original_code:
struct _cmember *channel_member_add(struct _channel *chan, struct _client *user, int flags)
{
 struct _cmember *m;
 debug(3, "channel_member_add: adding (%d) %s to %s", flags, user->nick, chan->name);
 if ((m = channel_member_search(chan, user)))
  channel_member_del(chan, user);
 m = lcalloc(sizeof(cmember), 1);
 m->next = chan->members;
 chan->members = m;
 if (m->next)
  m->next->prev = m;
 m->user = user;
 m->flags = flags;
 chan->membercount++;
 user->channelcount++;
 return m;
}

variable_types: struct _channel { struct _channel *next; struct _channel *prev; char *name; long ts; struct _cmember *members; int flags; char *topic; char *key; int memberlimit; int membercount; } *

variable_types:
user
struct _client *

variable_types: flags int

return_type: struct _cmember { struct _cmember *next; struct _cmember *prev; struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *user; int flags; } *


decompiled_code:
void *__fastcall user_channels_display(__int64 a1, int a2)
{
__int64 i; // rbx
_QWORD *v3; // rax
_QWORD *v4; // rbp
int v5; // eax

strlcpy(&chanlist_0, &unk_4AED, 0x4000LL);
for ( i = channel_list; i; i = *(_QWORD *)i )
{
v3 = channel_member_search(i, a1);
v4 = v3;
if ( !v3 || !a2 && (*(_BYTE *)(i + 40) & 0x18) != 0 )
continue;
v5 = *((_DWORD *)v3 + 6);
if ( (v5 & 1) != 0 )
{
strlcat(&chanlist_0, "@", 0x4000LL);
v5 = *((_DWORD *)v4 + 6);
if ( (v5 & 2) == 0 )
{
LABEL_4:
if ( (v5 & 4) != 0 )
goto LABEL_13;
goto LABEL_5;
}
}
else if ( (v5 & 2) == 0 )
{
goto LABEL_4;
}
strlcat(&chanlist_0, L"%+ ", 0x4000LL);
if ( (v4[3] & 4) != 0 )
LABEL_13:
strlcat(&chanlist_0, L"+ ", 0x4000LL);
LABEL_5:
strlcat(&chanlist_0, *(_QWORD *)(i + 16), 0x4000LL);
strlcat(&chanlist_0, L" ", 0x4000LL);
}
return &chanlist_0;
}

canonical_decompiled_code:
void *__fastcall func16(__int64 a1, int a2)
{
__int64 i; // rbx
_QWORD *v3; // rax
_QWORD *v4; // rbp
int v5; // eax

strlcpy(&chanlist_0, &unk_4AED, 0x4000LL);
for ( i = channel_list; i; i = *(_QWORD *)i )
{
v3 = channel_member_search(i, a1);
v4 = v3;
if ( !v3 || !a2 && (*(_BYTE *)(i + 40) & 0x18) != 0 )
continue;
v5 = *((_DWORD *)v3 + 6);
if ( (v5 & 1) != 0 )
{
strlcat(&chanlist_0, "@", 0x4000LL);
v5 = *((_DWORD *)v4 + 6);
if ( (v5 & 2) == 0 )
{
LABEL_4:
if ( (v5 & 4) != 0 )
goto LABEL_13;
goto LABEL_5;
}
}
else if ( (v5 & 2) == 0 )
{
goto LABEL_4;
}
strlcat(&chanlist_0, L"%+ ", 0x4000LL);
if ( (v4[3] & 4) != 0 )
LABEL_13:
strlcat(&chanlist_0, L"+ ", 0x4000LL);
LABEL_5:
strlcat(&chanlist_0, *(_QWORD *)(i + 16), 0x4000LL);
strlcat(&chanlist_0, L" ", 0x4000LL);
}
return &chanlist_0;
}

canonical_original_code:
char *user_channels_display(struct _client *u, int showall)
{
 static char chanlist[16384];
 struct _channel *c;
 struct _cmember *m;
 strlcpy(chanlist, "", 16384);
 for (c = channel_list; c; c = c->next) {
  if ((m = channel_member_search(c, u)) && (showall || (!((c->flags & 0x00000010) == (0x00000010)) && !((c->flags & 0x00000008) == (0x00000008))))) {
   if (((m->flags & 0x00000001) == (0x00000001)))
    strlcat(chanlist, "@", 16384);
   if (((m->flags & 0x00000002) == (0x00000002)))
    strlcat(chanlist, "%", 16384);
   if (((m->flags & 0x00000004) == (0x00000004)))
    strlcat(chanlist, "+", 16384);
   strlcat(chanlist, c->name, 16384);
   strlcat(chanlist, " ", 16384);
  }
 }
 return chanlist;
}

variable_types: showall int

variable_types:
chanlist
char [16384]

return_type: char *


lmalloc

decompiled_code:
void *__fastcall lmalloc(size_t a1)
{
void *v1; // r12

v1 = malloc(a1);
if ( v1 )
me[2063] += a1;
else
logger(2LL, "lmalloc(%d): fail!", a1);
return v1;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
void *v1; // r12

v1 = malloc(a1);
if ( v1 )
me[2063] += a1;
else
logger(2LL, "lmalloc(%d): fail!", a1);
return v1;
}

original_code:
void *
lmalloc(size_t size)
{
 void *ptr = malloc(size);
 if (!ptr) {
  logger(2, "lmalloc(%d): fail!", size);
  return ((void *)0);
 }
 me.memusage += size;
 return ptr;
}

canonical_original_code:
void *lmalloc(unsigned long size)
{
 void *ptr = malloc(size);
 if (!ptr) {
  logger(2, "lmalloc(%d): fail!", size);
  return ((void *)0);
 }
 me.memusage += size;
 return ptr;
}

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


lcalloc

decompiled_code:
void *__fastcall lcalloc(size_t a1, size_t a2)
{
void *v2; // r12

v2 = calloc(a1, a2);
if ( v2 )
me[2063] += a2 * a1;
else
logger(2LL, "lcalloc(%d): fail!", a2);
return v2;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1, size_t a2)
{
void *v2; // r12

v2 = calloc(a1, a2);
if ( v2 )
me[2063] += a2 * a1;
else
logger(2LL, "lcalloc(%d): fail!", a2);
return v2;
}

original_code:
void *
lcalloc(size_t number, size_t size)
{
 void *ptr = calloc(number, size);
 if (!ptr) {
  logger(2, "lcalloc(%d): fail!", size);
  return ((void *)0);
 }
 me.memusage += (number * size);
 return ptr;
}

canonical_original_code:
void *lcalloc(unsigned long number, unsigned long size)
{
 void *ptr = calloc(number, size);
 if (!ptr) {
  logger(2, "lcalloc(%d): fail!", size);
  return ((void *)0);
 }
 me.memusage += (number * size);
 return ptr;
}

variable_types:
number
unsigned long


lrealloc

decompiled_code:
void *__fastcall lrealloc(void *ptr, size_t size)
{
void *v2; // r12

me[2063] += ~strlen((const char *)ptr);
v2 = realloc(ptr, size);
if ( v2 )
me[2063] += size;
else
logger(2LL, "lrealloc(%d): fail!", size);
return v2;
}

canonical_decompiled_code:
void *__fastcall func2(void *ptr, size_t size)
{
void *v2; // r12

me[2063] += ~strlen((const char *)ptr);
v2 = realloc(ptr, size);
if ( v2 )
me[2063] += size;
else
logger(2LL, "lrealloc(%d): fail!", size);
return v2;
}

original_code:
void *
lrealloc(void *ptr, size_t size)
{
 void *tmp;
 me.memusage -= strlen(ptr) + 1;
 if (!(tmp = realloc(ptr, size))) {
  logger(2, "lrealloc(%d): fail!", size);
  return ((void *)0);
 }
 me.memusage += size;
 return tmp;
}

canonical_original_code:
void *lrealloc(void *ptr, unsigned long size)
{
 void *tmp;
 me.memusage -= strlen(ptr) + 1;
 if (!(tmp = realloc(ptr, size))) {
  logger(2, "lrealloc(%d): fail!", size);
  return ((void *)0);
 }
 me.memusage += size;
 return tmp;
}

variable_types:
tmp
void *


lfree

decompiled_code:
void __fastcall lfree(void *ptr, __int64 a2)
{
me[2063] -= a2;
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr, __int64 a2)
{
me[2063] -= a2;
free(ptr);
}

original_code:
void
lfree(void *ptr, size_t size)
{
 me.memusage -= size;
 free(ptr);
}

canonical_original_code:
void lfree(void *ptr, unsigned long size)
{
 me.memusage -= size;
 free(ptr);
}


parsehub

decompiled_code:
char *__fastcall parsehub(char *s2)
{
char *v1; // r12
char *result; // rax
char *v3; // rbp
char *v4; // rax
const char *v5; // rdi
char **v6; // r13
int v7; // ebx
char i; // al
unsigned int v9; // er12

v1 = s2;
debug(3LL, "parsehub: %s", s2);
result = 0LL;
memset(parv_0, 0, 0x78uLL);
if ( *s2 == 58 )
{
if ( !s2[1] )
return result;
parv_0[0] = (__int64)(s2 + 1);
result = strchr(s2 + 1, 32);
if ( !result )
return result;
*result = 0;
v1 = result + 1;
}
v3 = 0LL;
v4 = index(v1, 32);
if ( v4 )
{
*v4 = 0;
v3 = v4 + 1;
}
v5 = cmdtab;
v6 = &cmdtab;
if ( !cmdtab )
return (char *)debug(3LL, "parsehub: unknown command %s, ignoring line", v1);
while ( 1 )
{
v7 = strcmp(v5, v1);
if ( !v7 )
break;
v5 = v6[2];
v6 += 2;
if ( !v5 )
return (char *)debug(3LL, "parsehub: unknown command %s, ignoring line", v1);
}
if ( !v3 )
{
v9 = 16;
goto LABEL_26;
}
LABEL_10:
for ( i = *v3; i == 32; i = *++v3 )
LABEL_17:
*v3 = 0;
while ( i )
{
++v7;
if ( i == 58 )
{
parv_0[v7] = (__int64)(v3 + 1);
break;
}
parv_0[v7] = (__int64)v3;
if ( v7 > 14 )
break;
i = *v3;
if ( (*v3 & 0xDF) == 0 )
goto LABEL_10;
do
i = *++v3;
while ( (i & 0xDF) != 0 );
if ( i == 32 )
goto LABEL_17;
}
v3 = (char *)parv_0[0];
if ( !parv_0[0] || (result = (char *)client_list_search(parv_0[0]), (v3 = result) != 0LL) )
{
v9 = v7 + 1;
LABEL_26:
debug(6LL, "parsehub: executing %s(argc = %d)", *v6, v9);
result = (char *)((__int64 (__fastcall *)(char *, _QWORD, __int64 *))v6[1])(v3, v9, parv_0);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(char *s2)
{
char *v1; // r12
char *result; // rax
char *v3; // rbp
char *v4; // rax
const char *v5; // rdi
char **v6; // r13
int v7; // ebx
char i; // al
unsigned int v9; // er12

v1 = s2;
debug(3LL, "parsehub: %s", s2);
result = 0LL;
memset(parv_0, 0, 0x78uLL);
if ( *s2 == 58 )
{
if ( !s2[1] )
return result;
parv_0[0] = (__int64)(s2 + 1);
result = strchr(s2 + 1, 32);
if ( !result )
return result;
*result = 0;
v1 = result + 1;
}
v3 = 0LL;
v4 = index(v1, 32);
if ( v4 )
{
*v4 = 0;
v3 = v4 + 1;
}
v5 = cmdtab;
v6 = &cmdtab;
if ( !cmdtab )
return (char *)debug(3LL, "parsehub: unknown command %s, ignoring line", v1);
while ( 1 )
{
v7 = strcmp(v5, v1);
if ( !v7 )
break;
v5 = v6[2];
v6 += 2;
if ( !v5 )
return (char *)debug(3LL, "parsehub: unknown command %s, ignoring line", v1);
}
if ( !v3 )
{
v9 = 16;
goto LABEL_26;
}
LABEL_10:
for ( i = *v3; i == 32; i = *++v3 )
LABEL_17:
*v3 = 0;
while ( i )
{
++v7;
if ( i == 58 )
{
parv_0[v7] = (__int64)(v3 + 1);
break;
}
parv_0[v7] = (__int64)v3;
if ( v7 > 14 )
break;
i = *v3;
if ( (*v3 & 0xDF) == 0 )
goto LABEL_10;
do
i = *++v3;
while ( (i & 0xDF) != 0 );
if ( i == 32 )
goto LABEL_17;
}
v3 = (char *)parv_0[0];
if ( !parv_0[0] || (result = (char *)client_list_search(parv_0[0]), (v3 = result) != 0LL) )
{
v9 = v7 + 1;
LABEL_26:
debug(6LL, "parsehub: executing %s(argc = %d)", *v6, v9);
result = (char *)((__int64 (__fastcall *)(char *, _QWORD, __int64 *))v6[1])(v3, v9, parv_0);
}
return result;
}

original_code:
void
parsehub(char *buf)
{
 struct command *mptr = cmdtab;
 static char *parv[15 + 2];
 char *tmp, *s;
 client *cptr = ((void *)0);
 int i;
 debug(3, "parsehub: %s", buf);
 for (i = 0; i < 15; i++)
  parv[i] = ((void *)0);
 if (*buf == ':')
 {
  s = &buf[1];
  if(!(*s))
   return;
  parv[0] = s;
  if(!(s = strchr((char *)&buf[1], ' ')))
   return;
  *s++ = '\0';
 }
 else
  s = &buf[0];
 if((tmp = (char *)index(s, ' ')))
  *tmp++ = '\0';
 for (; mptr->cmd; mptr++)
  if (strcmp(mptr->cmd, s) == 0)
   goto next;
 debug(3, "parsehub: unknown command %s, ignoring line", s);
 return;
next:
 if (!(s = tmp))
  goto doit;
 i = 0;
 for (;;)
 {
  while (*s == ' ')
   *s++ = '\0';
  if (*s == '\0')
   break;
  if (*s == ':')
  {
   parv[++i] = s + 1;
   break;
  }
  parv[++i] = s;
  if (i >= 15)
   break;
  for (; *s != ' ' && *s; s++);
 }
 if (parv[0] && !(cptr = client_list_search(parv[0])))
  return;
doit:
 debug(6, "parsehub: executing %s(argc = %d)", mptr->cmd, i + 1);
 (*mptr->func)(cptr, i + 1, parv);
}

canonical_original_code:
void parsehub(char *buf)
{
 struct command *mptr = cmdtab;
 static char *parv[-1];
 char *tmp; char *s;
 struct _client *cptr = ((void *)0);
 int i;
 debug(3, "parsehub: %s", buf);
 for (i = 0; i < 15; i++)
  parv[i] = ((void *)0);
 if (*buf == ':')
 {
  s = &buf[1];
  if(!(*s))
   return;
  parv[0] = s;
  if(!(s = strchr((char *)&buf[1], ' ')))
   return;
  *s++ = '\0';
 }
 else
  s = &buf[0];
 if((tmp = (char *)index(s, ' ')))
  *tmp++ = '\0';
 for (; mptr->cmd; mptr++)
  if (strcmp(mptr->cmd, s) == 0)
   goto next;
 debug(3, "parsehub: unknown command %s, ignoring line", s);
 return;
next:
 if (!(s = tmp))
  goto doit;
 i = 0;
 for (;;)
 {
  while (*s == ' ')
   *s++ = '\0';
  if (*s == '\0')
   break;
  if (*s == ':')
  {
   parv[++i] = s + 1;
   break;
  }
  parv[++i] = s;
  if (i >= 15)
   break;
  for (; *s != ' ' && *s; s++);
 }
 if (parv[0] && !(cptr = client_list_search(parv[0])))
  return;
doit:
 debug(6, "parsehub: executing %s(argc = %d)", mptr->cmd, i + 1);
 (*mptr->func)(cptr, i + 1, parv);
}

variable_types:
buf
char *

variable_types: struct command { char *cmd; int (*func)(); } *

variable_types:
parv
char *[-1]

variable_types:
tmp
char *

variable_types:
s
char *

variable_types: struct _client { struct _client *next; struct _client *prev; struct _client *server; char *nick; char *user; char *host; char *name; long ts; int flags; int channelcount; } *

variable_types: i int


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(_BYTE *a1, char *a2, __int64 a3)
{
char *v5; // rax
unsigned __int64 v6; // rdx
char v7; // cl

v5 = a2;
if ( !a3 )
{
do
{
LABEL_9:
if ( !*v5++ )
break;
++v5;
}
while ( *(v5 - 1) );
return v5 - a2 - 1;
}
if ( a3 == 1 )
{
LABEL_8:
*a1 = 0;
goto LABEL_9;
}
v6 = (unsigned __int64)a1;
while ( 1 )
{
v7 = *v5++;
*(_BYTE *)v6++ = v7;
if ( !v7 )
return v5 - a2 - 1;
if ( !&a1[a3 + ~v6] )
{
a1 = (_BYTE *)v6;
goto LABEL_8;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func5(_BYTE *a1, char *a2, __int64 a3)
{
char *v5; // rax
unsigned __int64 v6; // rdx
char v7; // cl

v5 = a2;
if ( !a3 )
{
do
{
LABEL_9:
if ( !*v5++ )
break;
++v5;
}
while ( *(v5 - 1) );
return v5 - a2 - 1;
}
if ( a3 == 1 )
{
LABEL_8:
*a1 = 0;
goto LABEL_9;
}
v6 = (unsigned __int64)a1;
while ( 1 )
{
v7 = *v5++;
*(_BYTE *)v6++ = v7;
if ( !v7 )
return v5 - a2 - 1;
if ( !&a1[a3 + ~v6] )
{
a1 = (_BYTE *)v6;
goto LABEL_8;
}
}
}

original_code:
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
 register char *d = dst;
 register const char *s = src;
 register size_t n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

canonical_original_code:
unsigned long strlcpy(char *dst, char *src, unsigned long siz)
{
 register char *d = dst;
 register char *s = src;
 register unsigned long n = siz;
 if (n != 0 && --n != 0) {
  do {
   if ((*d++ = *s++) == 0)
    break;
  } while (--n != 0);
 }
 if (n == 0) {
  if (siz != 0)
   *d = '\0';
  while (*s++)
   ;
 }
 return(s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long

variable_types:
d
char *

variable_types:
n
unsigned long

return_type: unsigned long


shtime

decompiled_code:
_BYTE *shtime()
{
_BYTE *v0; // rax

v0 = lctime(me[0]);
strlcpy(buf_2, v0 + 11, 9LL);
return buf_2;
}

canonical_decompiled_code:
_BYTE *func6()
{
_BYTE *v0; // rax

v0 = lctime(me[0]);
func5(buf_2, v0 + 11, 9LL);
return buf_2;
}

original_code:
char *
shtime(void)
{
 static char buf[9];
 strlcpy(buf, lctime(me.now.tv_sec) + 11, 9);
 return buf;
}

canonical_original_code:
char *shtime()
{
 static char buf[9];
 strlcpy(buf, lctime(me.now.tv_sec) + 11, 9);
 return buf;
}

variable_types:
buf
char [9]


match

original_code:
int
match(const char *mask, const char *name)
{
 const unsigned char *m = (const unsigned char *)mask;
 const unsigned char *n = (const unsigned char *)name;
 const unsigned char *ma = (const unsigned char *)mask;
 const unsigned char *na = (const unsigned char *)name;
 int wild = 0;
loop:
 if (*m == '*') {
  while (*m == '*')
   m++;
  wild = 1;
  ma = m;
  na = n;
 }
 if (!*m) {
  if (!*n)
   return 1;
  for (m--; (m > (const unsigned char *)mask) && (*m == '?'); m--) ;
  if (*m == '*' && (m > (const unsigned char *) mask))
   return 1;
  if (!wild)
   return 0;
  m = ma;
  n = ++na;
 } else if (!*n) {
  while (*m == '*')
   m++;
  return (*m == 0);
 }
 if (tolower(*m) != tolower(*n) && *m != '?') {
  if (!wild)
   return 0;
  m = ma;
  n = ++na;
 } else {
  if (*m)
   m++;
  if (*n)
   n++;
 }
 goto loop;
}

canonical_original_code:
int match(char *mask, char *name)
{
 unsigned char *m = (const unsigned char *)mask;
 unsigned char *n = (const unsigned char *)name;
 unsigned char *ma = (const unsigned char *)mask;
 unsigned char *na = (const unsigned char *)name;
 int wild = 0;
loop:
 if (*m == '*') {
  while (*m == '*')
   m++;
  wild = 1;
  ma = m;
  na = n;
 }
 if (!*m) {
  if (!*n)
   return 1;
  for (m--; (m > (unsigned char *)mask) && (*m == '?'); m--) ;
  if (*m == '*' && (m > (unsigned char *) mask))
   return 1;
  if (!wild)
   return 0;
  m = ma;
  n = ++na;
 } else if (!*n) {
  while (*m == '*')
   m++;
  return (*m == 0);
 }
 if (tolower(*m) != tolower(*n) && *m != '?') {
  if (!wild)
   return 0;
  m = ma;
  n = ++na;
 } else {
  if (*m)
   m++;
  if (*n)
   n++;
 }
 goto loop;
}

variable_types:
mask
char *

variable_types:
name
char *

variable_types:
m
unsigned char *

variable_types:
n
unsigned char *

variable_types:
ma
unsigned char *

variable_types: wild int


repo: jmfaleiro/ReplicatedLocking

decompiled_code:
_DWORD *init_cpuinfo()
{
int v0; // er13
int v1; // eax
__int64 v2; // r12
__int64 v3; // rdx
int v4; // er14
void *v5; // rsp
int i; // er12
__int64 v7; // rdi
int v8; // eax
__int64 v9; // r12
int v10; // er15
_DWORD *result; // rax
_QWORD *v12; // rsi
unsigned int j; // er12
int v14; // edx
_DWORD v15[2]; // [rsp+0h] [rbp-40h] BYREF
_QWORD *v16; // [rsp+8h] [rbp-38h]

v0 = numa_num_thread_cpus();
v1 = numa_num_thread_nodes();
v2 = v1;
v3 = 4LL * v1;
cpu_info = v1;
v4 = v1;
v5 = alloca(v3);
if ( v1 <= 0 )
{
result = malloc(8LL * v1);
*((_QWORD *)&cpu_info + 1) = result;
if ( v0 <= 0 )
return result;
}
else
{
memset(v15, 0, v3);
v16 = malloc(8 * v2);
*((_QWORD *)&cpu_info + 1) = v16;
if ( v0 <= 0 )
goto LABEL_6;
}
for ( i = 0; i < v0; ++i )
{
v7 = (unsigned int)i;
v8 = numa_node_of_cpu(v7);
++v15[v8];
}
v16 = (_QWORD *)*((_QWORD *)&cpu_info + 1);
if ( v4 <= 0 )
goto LABEL_9;
LABEL_6:
v9 = 0LL;
do
{
v10 = v15[v9];
result = malloc(4LL * (v10 + 1));
v12 = v16;
v15[v9] = 1;
v12[v9++] = result;
*result = v10;
}
while ( v4 > (int)v9 );
if ( v0 > 0 )
{
LABEL_9:
for ( j = 0; j != v0; ++j )
{
result = (_DWORD *)(int)numa_node_of_cpu(j);
v14 = v15[(int)result];
*(_DWORD *)(*(_QWORD *)(*((_QWORD *)&cpu_info + 1) + 8LL * (int)result) + 4LL * v14) = j;
v15[(int)result] = v14 + 1;
}
}
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
int v0; // er13
int v1; // eax
__int64 v2; // r12
__int64 v3; // rdx
int v4; // er14
void *v5; // rsp
int i; // er12
__int64 v7; // rdi
int v8; // eax
__int64 v9; // r12
int v10; // er15
_DWORD *result; // rax
_QWORD *v12; // rsi
unsigned int j; // er12
int v14; // edx
_DWORD v15[2]; // [rsp+0h] [rbp-40h] BYREF
_QWORD *v16; // [rsp+8h] [rbp-38h]

v0 = numa_num_thread_cpus();
v1 = numa_num_thread_nodes();
v2 = v1;
v3 = 4LL * v1;
cpu_info = v1;
v4 = v1;
v5 = alloca(v3);
if ( v1 <= 0 )
{
result = malloc(8LL * v1);
*((_QWORD *)&cpu_info + 1) = result;
if ( v0 <= 0 )
return result;
}
else
{
memset(v15, 0, v3);
v16 = malloc(8 * v2);
*((_QWORD *)&cpu_info + 1) = v16;
if ( v0 <= 0 )
goto LABEL_6;
}
for ( i = 0; i < v0; ++i )
{
v7 = (unsigned int)i;
v8 = numa_node_of_cpu(v7);
++v15[v8];
}
v16 = (_QWORD *)*((_QWORD *)&cpu_info + 1);
if ( v4 <= 0 )
goto LABEL_9;
LABEL_6:
v9 = 0LL;
do
{
v10 = v15[v9];
result = malloc(4LL * (v10 + 1));
v12 = v16;
v15[v9] = 1;
v12[v9++] = result;
*result = v10;
}
while ( v4 > (int)v9 );
if ( v0 > 0 )
{
LABEL_9:
for ( j = 0; j != v0; ++j )
{
result = (_DWORD *)(int)numa_node_of_cpu(j);
v14 = v15[(int)result];
*(_DWORD *)(*(_QWORD *)(*((_QWORD *)&cpu_info + 1) + 8LL * (int)result) + 4LL * v14) = j;
v15[(int)result] = v14 + 1;
}
}
return result;
}

original_code:
void
init_cpuinfo()
{
    int i;
    int num_cpus = numa_num_thread_cpus();
    int num_numa_nodes = numa_num_thread_nodes();
    cpu_info.num_nodes = num_numa_nodes;
    int cpu_count[num_numa_nodes];
    for (i = 0; i < num_numa_nodes; ++i)
        cpu_count[i] = 0;
    cpu_info.node_map = (int **)malloc(sizeof(int*) * num_numa_nodes);
    for (i = 0; i < num_cpus; ++i)
        cpu_count[numa_node_of_cpu(i)] += 1;
    for (i = 0; i < num_numa_nodes; ++i) {
        cpu_info.node_map[i] = (int *)malloc(sizeof(int) * (1+cpu_count[i]));
        cpu_info.node_map[i][0] = cpu_count[i];
        cpu_count[i] = 1;
    }
    for (i = 0; i < num_cpus; ++i) {
        int numa_node = numa_node_of_cpu(i);
        cpu_info.node_map[numa_node][cpu_count[numa_node]] = i;
        cpu_count[numa_node] += 1;
    }
}

canonical_original_code:
void init_cpuinfo()
{
    int i;
    int num_cpus = numa_num_thread_cpus();
    int num_numa_nodes = numa_num_thread_nodes();
    cpu_info.num_nodes = num_numa_nodes;
    int cpu_count[-1];
    for (i = 0; i < num_numa_nodes; ++i)
        cpu_count[i] = 0;
    cpu_info.node_map = (int **)malloc(sizeof(int *) * num_numa_nodes);
    for (i = 0; i < num_cpus; ++i)
        cpu_count[numa_node_of_cpu(i)] += 1;
    for (i = 0; i < num_numa_nodes; ++i) {
        cpu_info.node_map[i] = (int *)malloc(sizeof(int) * (1+cpu_count[i]));
        cpu_info.node_map[i][0] = cpu_count[i];
        cpu_count[i] = 1;
    }
    for (i = 0; i < num_cpus; ++i) {
        int numa_node = numa_node_of_cpu(i);
        cpu_info.node_map[numa_node][cpu_count[numa_node]] = i;
        cpu_count[numa_node] += 1;
    }
}


decompiled_code:
__int64 __fastcall get_cpu(int a1, int a2)
{
__int64 v3; // rsi
__int64 v4; // rcx
_DWORD *v5; // rsi

v3 = *((_QWORD *)&cpu_info + 1);
v4 = a1 % cpu_info;
if ( a2 )
{
v5 = *(_DWORD **)(v3 + 8 * v4);
LODWORD(v4) = a1 / *v5;
}
else
{
v5 = *(_DWORD **)(v3 + 8LL * (a1 / cpu_info));
}
return (unsigned int)v5[(int)v4 + 1];
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v3; // rsi
__int64 v4; // rcx
_DWORD *v5; // rsi

v3 = *((_QWORD *)&cpu_info + 1);
v4 = a1 % cpu_info;
if ( a2 )
{
v5 = *(_DWORD **)(v3 + 8 * v4);
LODWORD(v4) = a1 / *v5;
}
else
{
v5 = *(_DWORD **)(v3 + 8LL * (a1 / cpu_info));
}
return (unsigned int)v5[(int)v4 + 1];
}

original_code:
int
get_cpu(int index, int striped)
{
    int node, cpu_index;
    if (striped) {
        node = index % cpu_info.num_nodes;
        cpu_index = index / cpu_info.node_map[node][0];
    }
    else {
        node = index / cpu_info.num_nodes;
        cpu_index = index % cpu_info.num_nodes;
    }
    return cpu_info.node_map[node][1+cpu_index];
}

canonical_original_code:
int get_cpu(int index, int striped)
{
    int node; int cpu_index;
    if (striped) {
        node = index % cpu_info.num_nodes;
        cpu_index = index / cpu_info.node_map[node][0];
    }
    else {
        node = index / cpu_info.num_nodes;
        cpu_index = index % cpu_info.num_nodes;
    }
    return cpu_info.node_map[node][1+cpu_index];
}

variable_types: index int

variable_types: striped int

variable_types: node int


main

canonical_name: main

repo: jmjgemini/forfun

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
unsigned int *v7; // rax
void *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

ptr[0] = 0LL;
search_tree_insert(ptr, 2LL, envp);
search_tree_insert(ptr, 1LL, v3);
search_tree_insert(ptr, 5LL, v4);
search_tree_insert(ptr, 4LL, v5);
search_tree_insert(ptr, 3LL, v6);
search_tree_preorder_travelsal(ptr[0], visit);
putchar(10);
v7 = (unsigned int *)search_tree_find_max(ptr[0]);
if ( v7 )
printf("max = %d\n", *v7);
search_tree_delete(ptr, 3LL);
puts("after delete 3");
search_tree_preorder_travelsal(ptr[0], visit);
putchar(10);
puts("level travelsal");
search_tree_level_travelsal(ptr[0], visit);
putchar(10);
search_tree_free(ptr[0]);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
__int64 v4; // rdx
__int64 v5; // rdx
__int64 v6; // rdx
unsigned int *v7; // rax
void *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

ptr[0] = 0LL;
func8(ptr, 2LL, envp);
func8(ptr, 1LL, v3);
func8(ptr, 5LL, v4);
func8(ptr, 4LL, v5);
func8(ptr, 3LL, v6);
func12(ptr[0], visit);
putchar(10);
v7 = (unsigned int *)func11(ptr[0]);
if ( v7 )
printf("max = %d\n", *v7);
func16(ptr, 3LL);
puts("after delete 3");
func12(ptr[0], visit);
putchar(10);
puts("level travelsal");
func15(ptr[0], visit);
putchar(10);
func17(ptr[0]);
return 0;
}

original_code:
int main(void)
{
 SearchTree tree= ((void *)0);
 search_tree_insert(&tree,2);
 search_tree_insert(&tree,1);
 search_tree_insert(&tree,5);
 search_tree_insert(&tree,4);
 search_tree_insert(&tree,3);
 search_tree_preorder_travelsal(tree,visit);
 printf("\n");
 TreeNode *p = search_tree_find_max(tree);
 if (p != ((void *)0))
  printf("max = %d\n",p->key);
 search_tree_delete(&tree,3);
 printf("after delete 3\n");
 search_tree_preorder_travelsal(tree,visit);
 printf("\n");
 printf("level travelsal\n");
 search_tree_level_travelsal(tree,visit);
 printf("\n");
 search_tree_free(tree);
 return 0;
}

canonical_original_code:
int main()
{
 struct _TreeNode *tree = ((void *)0);
 search_tree_insert(&tree,2);
 search_tree_insert(&tree,1);
 search_tree_insert(&tree,5);
 search_tree_insert(&tree,4);
 search_tree_insert(&tree,3);
 search_tree_preorder_travelsal(tree,visit);
 printf("\n");
 struct _TreeNode *p = search_tree_find_max(tree);
 if (p != ((void *)0))
  printf("max = %d\n",p->key);
 search_tree_delete(&tree,3);
 printf("after delete 3\n");
 search_tree_preorder_travelsal(tree,visit);
 printf("\n");
 printf("level travelsal\n");
 search_tree_level_travelsal(tree,visit);
 printf("\n");
 search_tree_free(tree);
 return 0;
}

variable_types: struct _TreeNode { int key; struct _TreeNode *lchild; struct _TreeNode *rchild; } *


visit

decompiled_code:
int __fastcall visit(unsigned int *a1)
{
return printf("%d ", *a1);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int *a1)
{
return printf("%d ", *a1);
}

original_code:
void visit(TreeNode *node)
{
 printf("%d ",node->key);
}

canonical_original_code:
void visit(struct _TreeNode *node)
{
 printf("%d ",node->key);
}

user_defined_types: struct _TreeNode { int key; struct _TreeNode *lchild; struct _TreeNode *rchild; }


decompiled_code:
char *__fastcall search_tree_insert(void **a1, int a2)
{
void **v2; // rbx
char *result; // rax

v2 = a1;
for ( result = (char *)*a1; *v2; result = (char *)*v2 )
{
v2 = (void **)(result + 8);
if ( *(_DWORD *)result <= a2 )
{
if ( *(_DWORD *)result >= a2 )
return result;
v2 = (void **)(result + 16);
}
}
result = (char *)malloc(0x18uLL);
*v2 = result;
if ( !result )
return (char *)fwrite("Error:can not create TreeNode\n", 1uLL, 0x1EuLL, stderr);
*(_DWORD *)result = a2;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
return result;
}

canonical_decompiled_code:
char *__fastcall func8(void **a1, int a2)
{
void **v2; // rbx
char *result; // rax

v2 = a1;
for ( result = (char *)*a1; *v2; result = (char *)*v2 )
{
v2 = (void **)(result + 8);
if ( *(_DWORD *)result <= a2 )
{
if ( *(_DWORD *)result >= a2 )
return result;
v2 = (void **)(result + 16);
}
}
result = (char *)malloc(0x18uLL);
*v2 = result;
if ( !result )
return (char *)fwrite("Error:can not create TreeNode\n", 1uLL, 0x1EuLL, stderr);
*(_DWORD *)result = a2;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
return result;
}

original_code:
void search_tree_insert(SearchTree *t,key_t key)
{
 if (*t == ((void *)0))
 {
  *t = malloc(sizeof(TreeNode));
  if (*t == ((void *)0))
  {
   fprintf(stderr, "Error:can not create TreeNode\n");
   return;
  }
  (*t)->key = key;
  (*t)->lchild = ((void *)0);
  (*t)->rchild = ((void *)0);
 }
 else if (key < (*t)->key)
  search_tree_insert(&((*t)->lchild),key);
 else if (key > (*t)->key)
  search_tree_insert(&((*t)->rchild),key);
}

canonical_original_code:
void search_tree_insert(struct _TreeNode **t, int key)
{
 if (*t == ((void *)0))
 {
  *t = malloc(sizeof(TreeNode));
  if (*t == ((void *)0))
  {
   fprintf(stderr, "Error:can not create TreeNode\n");
   return;
  }
  (*t)->key = key;
  (*t)->lchild = ((void *)0);
  (*t)->rchild = ((void *)0);
 }
 else if (key < (*t)->key)
  search_tree_insert(&((*t)->lchild),key);
 else if (key > (*t)->key)
  search_tree_insert(&((*t)->rchild),key);
}

variable_types: struct _TreeNode { int key; struct _TreeNode *lchild; struct _TreeNode *rchild; } **

variable_types: key int


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

for ( result = a1; result; result = *(_QWORD *)(result + 16) )
{
while ( *(_DWORD *)result > a2 )
{
result = *(_QWORD *)(result + 8);
if ( !result )
return result;
}
if ( *(_DWORD *)result >= a2 )
break;
}
return result;
}

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

for ( result = a1; result; result = *(_QWORD *)(result + 16) )
{
while ( *(_DWORD *)result > a2 )
{
result = *(_QWORD *)(result + 8);
if ( !result )
return result;
}
if ( *(_DWORD *)result >= a2 )
break;
}
return result;
}

original_code:
Position search_tree_find(SearchTree t,key_t key)
{
 if (t == ((void *)0))
  return ((void *)0);
 if (key < t->key)
  return search_tree_find(t->lchild,key);
 else if (key > t->key)
  return search_tree_find(t->rchild,key);
 else
  return t;
}

canonical_original_code:
struct _TreeNode *search_tree_find(struct _TreeNode *t, int key)
{
 if (t == ((void *)0))
  return ((void *)0);
 if (key < t->key)
  return search_tree_find(t->lchild,key);
 else if (key > t->key)
  return search_tree_find(t->rchild,key);
 else
  return t;
}

variable_types:
t
struct _TreeNode *

return_type: struct _TreeNode { int key; struct _TreeNode *lchild; struct _TreeNode *rchild; } *


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

for ( result = a1; result && *(_QWORD *)(result + 8); result = *(_QWORD *)(result + 8) )
;
return result;
}

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

for ( result = a1; result && *(_QWORD *)(result + 8); result = *(_QWORD *)(result + 8) )
;
return result;
}

original_code:
Position search_tree_find_min(SearchTree t)
{
 if (t == ((void *)0))
  return ((void *)0);
 if (t->lchild == ((void *)0))
  return t;
 else
  return search_tree_find_min(t->lchild);
}

canonical_original_code:
struct _TreeNode *search_tree_find_min(struct _TreeNode *t)
{
 if (t == ((void *)0))
  return ((void *)0);
 if (t->lchild == ((void *)0))
  return t;
 else
  return search_tree_find_min(t->lchild);
}


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

for ( result = a1; result && *(_QWORD *)(result + 16); result = *(_QWORD *)(result + 16) )
;
return result;
}

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

for ( result = a1; result && *(_QWORD *)(result + 16); result = *(_QWORD *)(result + 16) )
;
return result;
}

original_code:
Position search_tree_find_max(SearchTree t)
{
 if (t == ((void *)0))
  return ((void *)0);
 if (t->rchild == ((void *)0))
  return t;
 else
  return search_tree_find_max(t->rchild);
}

canonical_original_code:
struct _TreeNode *search_tree_find_max(struct _TreeNode *t)
{
 if (t == ((void *)0))
  return ((void *)0);
 if (t->rchild == ((void *)0))
  return t;
 else
  return search_tree_find_max(t->rchild);
}


decompiled_code:
// attributes: thunk
__int64 __fastcall search_tree_preorder_travelsal(__int64 a1, __int64 a2)
{
return btree_preorder_traversal(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2)
{
return func19(a1, a2);
}

original_code:
void search_tree_preorder_travelsal(SearchTree t,visit_func visit)
{
 btree_preorder_traversal(t,visit);
}

canonical_original_code:
void search_tree_preorder_travelsal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 btree_preorder_traversal(t,visit);
}

variable_types:
visit
void (*)(struct _TreeNode *)


decompiled_code:
// attributes: thunk
__int64 search_tree_postorder_travelsal()
{
return btree_postorder_traversal();
}

canonical_decompiled_code:
__int64 func13()
{
return func20();
}

original_code:
void search_tree_postorder_travelsal(SearchTree t,visit_func visit)
{
 btree_postorder_traversal(t,visit);
}

canonical_original_code:
void search_tree_postorder_travelsal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 btree_postorder_traversal(t,visit);
}


decompiled_code:
// attributes: thunk
__int64 search_tree_middleorder_travesal()
{
return btree_middleorder_traversal();
}

canonical_decompiled_code:
__int64 func14()
{
return func21();
}

original_code:
void search_tree_middleorder_travesal(SearchTree t,visit_func visit)
{
 btree_middleorder_traversal(t,visit);
}

canonical_original_code:
void search_tree_middleorder_travesal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 btree_middleorder_traversal(t,visit);
}


decompiled_code:
// attributes: thunk
__int64 __fastcall search_tree_level_travelsal(__int64 a1, __int64 a2)
{
return btree_level_travelsal(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2)
{
return func22(a1, a2);
}

original_code:
void search_tree_level_travelsal(Btree t,visit_func visit)
{
 btree_level_travelsal(t,visit);
}

canonical_original_code:
void search_tree_level_travelsal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 btree_level_travelsal(t,visit);
}


decompiled_code:
void __fastcall search_tree_delete(void **a1, int a2)
{
void **v2; // rbp
_QWORD *v3; // r8
__int64 v4; // rdx
_QWORD *v5; // rbx
int *v6; // rax
int *v7; // r8

v2 = a1;
v3 = *a1;
LABEL_2:
if ( v3 )
{
while ( 1 )
{
while ( *(_DWORD *)v3 != a2 )
{
v4 = v3[1];
if ( *(_DWORD *)v3 <= a2 )
{
v2 = (void **)(v3 + 2);
v3 = (_QWORD *)v3[2];
goto LABEL_2;
}
v2 = (void **)(v3 + 1);
v3 = (_QWORD *)v3[1];
if ( !v4 )
goto LABEL_6;
}
v5 = (_QWORD *)v3[2];
if ( !v3[1] )
break;
if ( !v5 )
{
v5 = (_QWORD *)v3[1];
break;
}
v2 = (void **)(v3 + 2);
v6 = (int *)search_tree_find_max(v3[2]);
a2 = *v6;
*v7 = *v6;
v3 = v5;
}
free(v3);
*v2 = v5;
}
else
{
LABEL_6:
fwrite("search_tree_delete:Element not found!\n", 1uLL, 0x26uLL, stderr);
}
}

canonical_decompiled_code:
void __fastcall func16(void **a1, int a2)
{
void **v2; // rbp
_QWORD *v3; // r8
__int64 v4; // rdx
_QWORD *v5; // rbx
int *v6; // rax
int *v7; // r8

v2 = a1;
v3 = *a1;
LABEL_2:
if ( v3 )
{
while ( 1 )
{
while ( *(_DWORD *)v3 != a2 )
{
v4 = v3[1];
if ( *(_DWORD *)v3 <= a2 )
{
v2 = (void **)(v3 + 2);
v3 = (_QWORD *)v3[2];
goto LABEL_2;
}
v2 = (void **)(v3 + 1);
v3 = (_QWORD *)v3[1];
if ( !v4 )
goto LABEL_6;
}
v5 = (_QWORD *)v3[2];
if ( !v3[1] )
break;
if ( !v5 )
{
v5 = (_QWORD *)v3[1];
break;
}
v2 = (void **)(v3 + 2);
v6 = (int *)func11(v3[2]);
a2 = *v6;
*v7 = *v6;
v3 = v5;
}
free(v3);
*v2 = v5;
}
else
{
LABEL_6:
fwrite("search_tree_delete:Element not found!\n", 1uLL, 0x26uLL, stderr);
}
}

original_code:
void search_tree_delete(SearchTree *t,key_t key)
{
 if (*t == ((void *)0))
 {
  fprintf(stderr, "search_tree_delete:Element not found!\n");
  return;
 }
 if ((*t)->key == key)
 {
  if ((*t)->lchild && (*t)->rchild)
  {
   Position p = search_tree_find_max((*t)->rchild);
   (*t)->key = p->key;
   search_tree_delete(&((*t)->rchild),p->key);
  }
  else
  {
   Position p;
   if ((*t)->lchild == ((void *)0))
    p = (*t)->rchild;
   else
    p = (*t)->lchild;
   free(*t);
   *t = p;
  }
 }
 else if (key < (*t)->key)
  search_tree_delete(&((*t)->lchild),key);
 else
  search_tree_delete(&((*t)->rchild),key);
}

canonical_original_code:
void search_tree_delete(struct _TreeNode **t, int key)
{
 if (*t == ((void *)0))
 {
  fprintf(stderr, "search_tree_delete:Element not found!\n");
  return;
 }
 if ((*t)->key == key)
 {
  if ((*t)->lchild && (*t)->rchild)
  {
   struct _TreeNode *p = search_tree_find_max((*t)->rchild);
   (*t)->key = p->key;
   search_tree_delete(&((*t)->rchild),p->key);
  }
  else
  {
   struct _TreeNode *p;
   if ((*t)->lchild == ((void *)0))
    p = (*t)->rchild;
   else
    p = (*t)->lchild;
   free(*t);
   *t = p;
  }
 }
 else if (key < (*t)->key)
  search_tree_delete(&((*t)->lchild),key);
 else
  search_tree_delete(&((*t)->rchild),key);
}


decompiled_code:
void __fastcall search_tree_free(void *ptr)
{
if ( ptr )
{
search_tree_free(*((void **)ptr + 1));
search_tree_free(*((void **)ptr + 2));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func17(void *ptr)
{
if ( ptr )
{
func17(*((void **)ptr + 1));
func17(*((void **)ptr + 2));
free(ptr);
}
}

original_code:
void search_tree_free(SearchTree t)
{
 if (t == ((void *)0))
  return;
 search_tree_free(t->lchild);
 search_tree_free(t->rchild);
 free(t);
}

canonical_original_code:
void search_tree_free(struct _TreeNode *t)
{
 if (t == ((void *)0))
  return;
 search_tree_free(t->lchild);
 search_tree_free(t->rchild);
 free(t);
}


decompiled_code:
void __fastcall btree_make_empty(void *ptr)
{
if ( ptr )
{
btree_make_empty(*((void **)ptr + 1));
btree_make_empty(*((void **)ptr + 2));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func18(void *ptr)
{
if ( ptr )
{
func18(*((void **)ptr + 1));
func18(*((void **)ptr + 2));
free(ptr);
}
}

original_code:
void btree_make_empty(Btree t)
{
 if (t != ((void *)0))
 {
  btree_make_empty(t->lchild);
  btree_make_empty(t->rchild);
  free(t);
 }
}

canonical_original_code:
void btree_make_empty(struct _TreeNode *t)
{
 if (t != ((void *)0))
 {
  btree_make_empty(t->lchild);
  btree_make_empty(t->rchild);
  free(t);
 }
}


decompiled_code:
__int64 __fastcall btree_preorder_traversal(__int64 a1, void (__fastcall *a2)(__int64))
{
__int64 v2; // rbx
__int64 result; // rax

if ( a1 )
{
v2 = a1;
do
{
a2(v2);
result = btree_preorder_traversal(*(_QWORD *)(v2 + 8), a2);
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, void (__fastcall *a2)(__int64))
{
__int64 v2; // rbx
__int64 result; // rax

if ( a1 )
{
v2 = a1;
do
{
a2(v2);
result = func19(*(_QWORD *)(v2 + 8), a2);
v2 = *(_QWORD *)(v2 + 16);
}
while ( v2 );
}
return result;
}

original_code:
void btree_preorder_traversal(Btree t,visit_func visit)
{
 if (t == ((void *)0))
  return;
 visit(t);
 btree_preorder_traversal(t->lchild,visit);
 btree_preorder_traversal(t->rchild,visit);
}

canonical_original_code:
void btree_preorder_traversal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 if (t == ((void *)0))
  return;
 visit(t);
 btree_preorder_traversal(t->lchild,visit);
 btree_preorder_traversal(t->rchild,visit);
}


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

if ( a1 )
{
btree_preorder_traversal(*(_QWORD *)(a1 + 8), a2);
btree_preorder_traversal(*(_QWORD *)(a1 + 16), a2);
result = ((__int64 (__fastcall *)(__int64))a2)(a1);
}
return result;
}

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

if ( a1 )
{
func19(*(_QWORD *)(a1 + 8), a2);
func19(*(_QWORD *)(a1 + 16), a2);
result = ((__int64 (__fastcall *)(__int64))a2)(a1);
}
return result;
}

original_code:
void btree_postorder_traversal(Btree t,visit_func visit)
{
 if (t == ((void *)0))
  return;
 btree_preorder_traversal(t->lchild,visit);
 btree_preorder_traversal(t->rchild,visit);
 visit(t);
}

canonical_original_code:
void btree_postorder_traversal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 if (t == ((void *)0))
  return;
 btree_preorder_traversal(t->lchild,visit);
 btree_preorder_traversal(t->rchild,visit);
 visit(t);
}


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

if ( a1 )
{
btree_preorder_traversal(*(_QWORD *)(a1 + 8), a2);
a2(a1);
result = btree_preorder_traversal(*(_QWORD *)(a1 + 16), a2);
}
return result;
}

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

if ( a1 )
{
func19(*(_QWORD *)(a1 + 8), a2);
a2(a1);
result = func19(*(_QWORD *)(a1 + 16), a2);
}
return result;
}

original_code:
void btree_middleorder_traversal(Btree t,visit_func visit)
{
 if (t == ((void *)0))
  return;
 btree_preorder_traversal(t->lchild,visit);
 visit(t);
 btree_preorder_traversal(t->rchild,visit);
}

canonical_original_code:
void btree_middleorder_traversal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 if (t == ((void *)0))
  return;
 btree_preorder_traversal(t->lchild,visit);
 visit(t);
 btree_preorder_traversal(t->rchild,visit);
}


original_code:
void btree_level_travelsal(Btree t,visit_func visit)
{
 TreeNode *que[64];
 int front = 0,rear = 0;
 rear = (rear+1)%64;
 que[rear] = t;
 TreeNode *p;
 if (t != ((void *)0))
 {
  while(front != rear)
  {
   front = (front+1)%64;
   p = que[front];
   visit(p);
   if (p->lchild != ((void *)0))
   {
    if ((rear+1)%64 != front)
    {
     rear = (rear+1)%64;
     que[rear] = p->lchild;
    }
   }
   if (p->rchild != ((void *)0))
   {
    if ((rear+1)%64 != front)
    {
     rear = (rear+1)%64;
     que[rear] = p->rchild;
    }
   }
  }
 }
}

canonical_original_code:
void btree_level_travelsal(struct _TreeNode *t, void (*visit)(struct _TreeNode *))
{
 struct _TreeNode *que[64];
 int front = 0; int rear = 0;
 rear = (rear+1)%64;
 que[rear] = t;
 struct _TreeNode *p;
 if (t != ((void *)0))
 {
  while(front != rear)
  {
   front = (front+1)%64;
   p = que[front];
   visit(p);
   if (p->lchild != ((void *)0))
   {
    if ((rear+1)%64 != front)
    {
     rear = (rear+1)%64;
     que[rear] = p->lchild;
    }
   }
   if (p->rchild != ((void *)0))
   {
    if ((rear+1)%64 != front)
    {
     rear = (rear+1)%64;
     que[rear] = p->rchild;
    }
   }
  }
 }
}

variable_types: struct _TreeNode { int key; struct _TreeNode *lchild; struct _TreeNode *rchild; } *[64]

variable_types: front int

variable_types: rear int