decompiled_code:
__int64 __fastcall connect_socket(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *i; // rbx
int v4; // eax
unsigned int v5; // ebp
int *v6; // rax
char *v7; // rax
const char *v9; // rax
struct addrinfo *pai[3]; // [rsp+0h] [rbp-60h] BYREF
__int128 v11; // [rsp+18h] [rbp-48h]
__int128 v12; // [rsp+28h] [rbp-38h]

pai[1] = (struct addrinfo *)0x200000000LL;
v11 = 0LL;
pai[2] = (struct addrinfo *)0x600000001LL;
v12 = 0LL;
v2 = getaddrinfo(a1, a2, 0LL, pai);
if ( v2 )
{
v5 = -1;
v9 = gai_strerror(v2);
fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", a1, a2, v9);
}
else
{
for ( i = pai[0]; i; i = i->ai_next )
{
v4 = socket(i->ai_family, 1, 6);
v5 = v4;
if ( v4 != -1 )
{
if ( connect(v4, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_8;
close(v5);
}
}
v6 = __errno_location();
v5 = -1;
v7 = strerror(*v6);
fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", a1, a2, v7);
LABEL_8:
freeaddrinfo(pai[0]);
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *i; // rbx
int v4; // eax
unsigned int v5; // ebp
int *v6; // rax
char *v7; // rax
const char *v9; // rax
struct addrinfo *pai[3]; // [rsp+0h] [rbp-60h] BYREF
__int128 v11; // [rsp+18h] [rbp-48h]
__int128 v12; // [rsp+28h] [rbp-38h]

pai[1] = (struct addrinfo *)0x200000000LL;
v11 = 0LL;
pai[2] = (struct addrinfo *)0x600000001LL;
v12 = 0LL;
v2 = getaddrinfo(a1, a2, 0LL, pai);
if ( v2 )
{
v5 = -1;
v9 = gai_strerror(v2);
fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", a1, a2, v9);
}
else
{
for ( i = pai[0]; i; i = i->ai_next )
{
v4 = socket(i->ai_family, 1, 6);
v5 = v4;
if ( v4 != -1 )
{
if ( connect(v4, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_8;
close(v5);
}
}
v6 = __errno_location();
v5 = -1;
v7 = strerror(*v6);
fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", a1, a2, v7);
LABEL_8:
freeaddrinfo(pai[0]);
}
return v5;
}

original_code:
static int connect_socket(const char* host, const char* port)
{
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

canonical_original_code:
static int connect_socket(char *host, char *port)
{
  struct addrinfo hints;
  struct addrinfo *result; struct addrinfo *rp;
  int s; int res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
ssize_t wakeup_monitor()
{
int v0; // eax
ssize_t result; // rax
int *v2; // rax
char *v3; // rax

v0 = get_receive_pipe();
result = write(v0, &LC0, 1uLL);
if ( (_DWORD)result == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = ast_log(4LL, "utils.c", 89LL, "wakeup_monitor", "Failed to write receive_pipe: %s.\n", v3);
}
return result;
}

canonical_decompiled_code:
ssize_t func0()
{
int v0; // eax
ssize_t result; // rax
int *v2; // rax
char *v3; // rax

v0 = get_receive_pipe();
result = write(v0, &LC0, 1uLL);
if ( (_DWORD)result == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = ast_log(4LL, "utils.c", 89LL, "wakeup_monitor", "Failed to write receive_pipe: %s.\n", v3);
}
return result;
}

original_code:
static void wakeup_monitor(void)
{
  int res = write(get_receive_pipe(), "", 1);
  if (res == -1) {
    ast_log(4, "utils.c", 89, __PRETTY_FUNCTION__, "Failed to write receive_pipe: %s.\n", strerror((*__errno_location ())));
  }
}

canonical_original_code:
static void wakeup_monitor()
{
  int res = write(get_receive_pipe(), "", 1);
  if (res == -1) {
    ast_log(4, "utils.c", 89, __PRETTY_FUNCTION__, "Failed to write receive_pipe: %s.\n", strerror((*__errno_location ())));
  }
}

variable_types: res int

return_type: void


decompiled_code:
__int64 timers_wait()
{
__int64 result; // rax

result = mtp_sched_wait(monitor_sched);
if ( (unsigned int)(result - 1) >= 0x1F4 )
result = 500LL;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax

result = mtp_sched_wait(monitor_sched);
if ( (unsigned int)(result - 1) >= 0x1F4 )
result = 500LL;
return result;
}

original_code:
int timers_wait(void)
{
  int timeout = mtp_sched_wait(monitor_sched);
  if(timeout <= 0 || timeout > 500) {
    timeout = 500;
  }
  return timeout;
}

canonical_original_code:
int timers_wait()
{
  int timeout = mtp_sched_wait(monitor_sched);
  if(timeout <= 0 || timeout > 500) {
    timeout = 500;
  }
  return timeout;
}

variable_types: timeout int

return_type: int


decompiled_code:
__int64 __fastcall start_timer(int a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned int v4; // [rsp+Ch] [rbp-Ch]

result = mtp_sched_add(monitor_sched, (unsigned int)a1, a2, a3);
if ( a1 <= 499 )
{
v4 = result;
wakeup_monitor();
result = v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned int v4; // [rsp+Ch] [rbp-Ch]

result = mtp_sched_add(monitor_sched, (unsigned int)a1, a2, a3);
if ( a1 <= 499 )
{
v4 = result;
func0();
result = v4;
}
return result;
}

original_code:
int start_timer(int msec, int (*cb)(const void *), void *data)
{
  int id = mtp_sched_add(monitor_sched, msec, cb, data);
  if(msec < 500) {
    wakeup_monitor();
  }
  return id;
}

canonical_original_code:
int start_timer(int msec, int (*cb)(void *), void *data)
{
  int id = mtp_sched_add(monitor_sched, msec, cb, data);
  if(msec < 500) {
    wakeup_monitor();
  }
  return id;
}

variable_types: msec int

variable_types:
cb
int (*)(void *)

variable_types:
data
void *

variable_types: id int


decompiled_code:
__int64 __fastcall stop_timer(unsigned int a1)
{
__int64 result; // rax

result = mtp_sched_del(monitor_sched, a1);
if ( !(_DWORD)result )
result = ast_log(0LL, "utils.c", 118LL, "stop_timer", "Failed to delete timer\n");
return result;
}

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

result = mtp_sched_del(monitor_sched, a1);
if ( !(_DWORD)result )
result = ast_log(0LL, "utils.c", 118LL, "stop_timer", "Failed to delete timer\n");
return result;
}

original_code:
void stop_timer(int tid)
{
  int res = mtp_sched_del(monitor_sched, tid);
  if (!res) {
    ast_log(0, "utils.c", 118, __PRETTY_FUNCTION__, "Failed to delete timer\n");
  }
}

variable_types: tid int


decompiled_code:
__int64 timers_init()
{
monitor_sched = mtp_sched_context_create();
if ( monitor_sched )
return 0LL;
ast_log(4LL, "utils.c", 128LL, "timers_init", "Unable to create monitor scheduling context.\n");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func5()
{
monitor_sched = mtp_sched_context_create();
if ( monitor_sched )
return 0LL;
ast_log(4LL, "utils.c", 128LL, "timers_init", "Unable to create monitor scheduling context.\n");
return 0xFFFFFFFFLL;
}

original_code:
int timers_init(void)
{
  monitor_sched = mtp_sched_context_create();
  if(monitor_sched == ((void *)0)) {
    ast_log(4, "utils.c", 128, __PRETTY_FUNCTION__, "Unable to create monitor scheduling context.\n");
    return -1;
  }
  return 0;
}

canonical_original_code:
int timers_init()
{
  monitor_sched = mtp_sched_context_create();
  if(monitor_sched == ((void *)0)) {
    ast_log(4, "utils.c", 128, __PRETTY_FUNCTION__, "Unable to create monitor scheduling context.\n");
    return -1;
  }
  return 0;
}


decompiled_code:
__int64 timers_cleanup()
{
__int64 result; // rax

if ( !monitor_sched )
return 0LL;
mtp_sched_context_destroy();
result = 0LL;
monitor_sched = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax

if ( !monitor_sched )
return 0LL;
mtp_sched_context_destroy();
result = 0LL;
monitor_sched = 0LL;
return result;
}

original_code:
int timers_cleanup(void)
{
  if(monitor_sched) {
    mtp_sched_context_destroy(monitor_sched);
    monitor_sched = ((void *)0);
  }
  return 0;
}

canonical_original_code:
int timers_cleanup()
{
  if(monitor_sched) {
    mtp_sched_context_destroy(monitor_sched);
    monitor_sched = ((void *)0);
  }
  return 0;
}


decompiled_code:
int run_timers()
{
pthread_mutex_lock(&glock);
mtp_sched_runq(monitor_sched);
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int func7()
{
pthread_mutex_lock(&glock);
mtp_sched_runq(monitor_sched);
return pthread_mutex_unlock(&glock);
}

original_code:
void run_timers(void)
{
  pthread_mutex_lock(&glock);
  mtp_sched_runq(monitor_sched);
  pthread_mutex_unlock(&glock);
}

canonical_original_code:
void run_timers()
{
  pthread_mutex_lock(&glock);
  mtp_sched_runq(monitor_sched);
  pthread_mutex_unlock(&glock);
}


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

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

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

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


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

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

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

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


decompiled_code:
__int64 __fastcall start_thread(pthread_t *a1, void *(*a2)(void *), _DWORD *a3, int a4)
{
pthread_t v6; // rdi
unsigned int v7; // er12
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct sched_param param; // [rsp+Ch] [rbp-2Ch] BYREF

if ( pthread_create(a1, 0LL, a2, 0LL) )
{
v11 = _errno_location();
v7 = -1;
v12 = strerror(*v11);
ast_log(4LL, "utils.c", 168LL, "start_thread", "Failed to create thread: %s.\n", v12);
}
else
{
v6 = *a1;
v7 = 0;
param.__sched_priority = a4;
if ( pthread_setschedparam(v6, 2, &param) )
{
v9 = _errno_location();
v10 = strerror(*v9);
ast_log(3LL, "utils.c", 176LL, "start_thread", "Failed to set thread to realtime priority: %s.\n", v10);
}
*a3 = 1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func10(pthread_t *a1, void *(*a2)(void *), _DWORD *a3, int a4)
{
pthread_t v6; // rdi
unsigned int v7; // er12
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct sched_param param; // [rsp+Ch] [rbp-2Ch] BYREF

if ( pthread_create(a1, 0LL, a2, 0LL) )
{
v11 = _errno_location();
v7 = -1;
v12 = strerror(*v11);
ast_log(4LL, "utils.c", 168LL, "start_thread", "Failed to create thread: %s.\n", v12);
}
else
{
v6 = *a1;
v7 = 0;
param.__sched_priority = a4;
if ( pthread_setschedparam(v6, 2, &param) )
{
v9 = _errno_location();
v10 = strerror(*v9);
ast_log(3LL, "utils.c", 176LL, "start_thread", "Failed to set thread to realtime priority: %s.\n", v10);
}
*a3 = 1;
}
return v7;
}

original_code:
int start_thread(pthread_t* t, void* (*thread_main)(void*data), int* running, int prio)
{
  struct sched_param sp;
  int res;
  res = pthread_create(t, ((void *)0), thread_main, ((void *)0));
  if(res != 0) {
    ast_log(4, "utils.c", 168, __PRETTY_FUNCTION__, "Failed to create thread: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  memset(&sp, 0, sizeof(sp));
  sp.sched_priority = prio;
  res = pthread_setschedparam(*t, 2, &sp);
  if(res != 0) {
    ast_log(3, "utils.c", 176, __PRETTY_FUNCTION__, "Failed to set thread to realtime priority: %s.\n",
            strerror((*__errno_location ())));
  }
  *running = 1;
  return 0;
}

canonical_original_code:
int start_thread(unsigned long *t, void *(*thread_main)(void *data), int *running, int prio)
{
  struct sched_param sp;
  int res;
  res = pthread_create(t, ((void *)0), thread_main, ((void *)0));
  if(res != 0) {
    ast_log(4, "utils.c", 168, __PRETTY_FUNCTION__, "Failed to create thread: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  memset(&sp, 0, sizeof(sp));
  sp.sched_priority = prio;
  res = pthread_setschedparam(*t, 2, &sp);
  if(res != 0) {
    ast_log(3, "utils.c", 176, __PRETTY_FUNCTION__, "Failed to set thread to realtime priority: %s.\n",
            strerror((*__errno_location ())));
  }
  *running = 1;
  return 0;
}

variable_types:
t
unsigned long *

variable_types:
running
int *

variable_types: prio int

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
int __fastcall stop_thread(pthread_t *a1, int *a2)
{
int result; // eax

result = *a2;
if ( *a2 )
{
result = pthread_join(*a1, 0LL);
*a2 = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(pthread_t *a1, int *a2)
{
int result; // eax

result = *a2;
if ( *a2 )
{
result = pthread_join(*a1, 0LL);
*a2 = 0;
}
return result;
}

original_code:
void stop_thread(pthread_t *t, int* running) {
  if(*running) {
    pthread_join(*t, ((void *)0));
    *running = 0;
  }
}

canonical_original_code:
void stop_thread(unsigned long *t, int *running) {
  if(*running) {
    pthread_join(*t, ((void *)0));
    *running = 0;
  }
}


decompiled_code:
// attributes: thunk
const char *__fastcall inaddr2s(int a1)
{
return mtp_inet_ntoa_constprop_0(a1);
}

canonical_decompiled_code:
const char *__fastcall func12(int a1)
{
return func1(a1);
}

original_code:
const char* inaddr2s(struct in_addr addr)
{
  static char buf[20];
  return mtp_inet_ntoa(buf, sizeof(buf), addr);
}

canonical_original_code:
char *inaddr2s(struct in_addr addr)
{
  static char buf[20];
  return mtp_inet_ntoa(buf, sizeof(buf), addr);
}

variable_types:
addr
struct in_addr { unsigned int s_addr; }

variable_types:
buf
char [20]

return_type: char *


decompiled_code:
// attributes: thunk
void lffifo_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
void lffifo_free(struct lffifo *fifo) {
  free(fifo);
}

variable_types: struct lffifo { int size; int start; int end; unsigned char buf[]; } *


original_code:
int lffifo_put(struct lffifo *fifo, unsigned char *data, int size) {
  int i,j;
  int x;
  int iteration;
  if(size <= 0 || size > 0x10000000) {
    return 1;
  }
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->end;
    for(j = 0; j <= size; j++) {
      x = (j == size ? -1 : data[j]);
      if(iteration == 0 && fifo->buf[i] != 0xfe) {
        return 1;
      }
      if(x == 0xfe || x == 0xfd || x == -1) {
        if(iteration == 1) {
          fifo->buf[i] = 0xfd;
        }
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        if(iteration == 0 && fifo->buf[i] != 0xfe) {
          return 1;
        }
        if(x == 0xfe) {
          x = 0x00;
        } else if(x == 0xfd) {
          x = 0x01;
        } else {
          x = 0x02;
        }
      }
      if(iteration == 1) {
        fifo->buf[i] = x;
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
    }
  }
  fifo->end = i;
  return 0;
}

canonical_original_code:
int lffifo_put(struct lffifo *fifo, unsigned char *data, int size) {
  int i; int j;
  int x;
  int iteration;
  if(size <= 0 || size > 0x10000000) {
    return 1;
  }
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->end;
    for(j = 0; j <= size; j++) {
      x = (j == size ? -1 : data[j]);
      if(iteration == 0 && fifo->buf[i] != 0xfe) {
        return 1;
      }
      if(x == 0xfe || x == 0xfd || x == -1) {
        if(iteration == 1) {
          fifo->buf[i] = 0xfd;
        }
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        if(iteration == 0 && fifo->buf[i] != 0xfe) {
          return 1;
        }
        if(x == 0xfe) {
          x = 0x00;
        } else if(x == 0xfd) {
          x = 0x01;
        } else {
          x = 0x02;
        }
      }
      if(iteration == 1) {
        fifo->buf[i] = x;
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
    }
  }
  fifo->end = i;
  return 0;
}

variable_types:
data
unsigned char *

variable_types: size int

variable_types: i int

variable_types: j int

variable_types: x int

variable_types: iteration int

user_defined_types: struct lffifo { int size; int start; int end; unsigned char buf[]; }


original_code:
int lffifo_get(struct lffifo *fifo, unsigned char *buf, int bufsize) {
  int i,j;
  int x;
  int iteration;
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->start;
    j = 0;
    for(;;) {
      x = fifo->buf[i];
      if(iteration == 0) {
        if(x == 0xfe) {
          return 0;
        }
      } else {
        fifo->buf[i] = 0xfe;
      }
      if(x == 0xfd) {
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        x = fifo->buf[i];
        if(iteration == 0) {
          if(x == 0xfe) {
            return 0;
          }
        } else {
          fifo->buf[i] = 0xfe;
        }
        if(x == 0x00) {
          x = 0xfe;
        } else if(x == 0x01) {
          x = 0xfd;
        } else {
          x = -1;
        }
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
      if(x == -1) {
        if(j > bufsize) {
          if(iteration == 1) {
            fifo->start = i;
          }
          return bufsize - j;
        }
        break;
      } else {
        if(iteration == 1) {
          if(j < bufsize) {
            buf[j] = x;
          }
        }
        j++;
      }
      if(j > fifo->size) {
        fifo->start = fifo->end;
        if(iteration == 0) {
          break;
        } else {
          return 0;
        }
      }
    }
  }
  fifo->start = i;
  return j;
}

canonical_original_code:
int lffifo_get(struct lffifo *fifo, unsigned char *buf, int bufsize) {
  int i; int j;
  int x;
  int iteration;
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->start;
    j = 0;
    for(;;) {
      x = fifo->buf[i];
      if(iteration == 0) {
        if(x == 0xfe) {
          return 0;
        }
      } else {
        fifo->buf[i] = 0xfe;
      }
      if(x == 0xfd) {
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        x = fifo->buf[i];
        if(iteration == 0) {
          if(x == 0xfe) {
            return 0;
          }
        } else {
          fifo->buf[i] = 0xfe;
        }
        if(x == 0x00) {
          x = 0xfe;
        } else if(x == 0x01) {
          x = 0xfd;
        } else {
          x = -1;
        }
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
      if(x == -1) {
        if(j > bufsize) {
          if(iteration == 1) {
            fifo->start = i;
          }
          return bufsize - j;
        }
        break;
      } else {
        if(iteration == 1) {
          if(j < bufsize) {
            buf[j] = x;
          }
        }
        j++;
      }
      if(j > fifo->size) {
        fifo->start = fifo->end;
        if(iteration == 0) {
          break;
        } else {
          return 0;
        }
      }
    }
  }
  fifo->start = i;
  return j;
}

variable_types:
buf
unsigned char *

variable_types: bufsize int


gotpkt

repo: yuri-tolstov/projects

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

result = *(unsigned int *)(a2 + 20);
g_totlen += result;
if ( g_totlen < 0 )
{
g_totlen = 0LL;
g_pktcnt = 0LL;
}
else
{
++g_pktcnt;
}
return result;
}

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

result = *(unsigned int *)(a2 + 20);
g_totlen += result;
if ( g_totlen < 0 )
{
g_totlen = 0LL;
g_pktcnt = 0LL;
}
else
{
++g_pktcnt;
}
return result;
}

original_code:
void gotpkt(u_char *args, const struct pcap_pkthdr *pkth, const u_char *pkt)
{
   g_totlen += pkth->len;
   if (g_totlen < 0) {
      g_pktcnt = g_totlen = 0;
   }
   else {
      g_pktcnt++;
   }
}

canonical_original_code:
void gotpkt(unsigned char *args, struct pcap_pkthdr *pkth, unsigned char *pkt)
{
   g_totlen += pkth->len;
   if (g_totlen < 0) {
      g_pktcnt = g_totlen = 0;
   }
   else {
      g_pktcnt++;
   }
}

variable_types:
args
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
pkt
unsigned char *

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


tmdiff

original_code:
double tmdiff(struct timespec t0, struct timespec t1)
{
   struct timespec tt;
   if ((t1.tv_nsec - t0.tv_nsec) < 0) {
      tt.tv_sec = t1.tv_sec - t0.tv_sec - 1;
      tt.tv_nsec = 1000000000 + t1.tv_nsec - t0.tv_nsec;
   } else {
      tt.tv_sec = t1.tv_sec - t0.tv_sec;
      tt.tv_nsec = t1.tv_nsec - t0.tv_nsec;
   }
   return (double)tt.tv_sec + (double)tt.tv_nsec / 1000000000;
}

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

return_type: double


decompiled_code:
void __fastcall __noreturn monitor_thread(void *a1)
{
__int64 v1; // rbp
__int64 v2; // r13
__int64 v3; // rbx
__int64 v4; // r12
double v5; // xmm0_8
__int64 v6; // r8
__int64 v7; // r9
__int64 v8; // r10
__m128i v9; // [rsp+0h] [rbp-48h] BYREF
struct timespec tp; // [rsp+10h] [rbp-38h] BYREF

v1 = g_pktcnt;
v2 = g_totlen;
clock_gettime(0, (struct timespec *)&v9);
while ( 1 )
{
sleep(1u);
v3 = g_pktcnt;
v4 = g_totlen;
clock_gettime(0, &tp);
if ( v3 - v1 > 0 && v3 > 0 )
{
v5 = tmdiff(v9.m128i_i32[0], v9.m128i_i64[1], tp.tv_sec, tp.tv_nsec);
printf("dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n", v5, v8, (v4 - v2) / v8, v4 / v3, v6, v7);
}
v2 = v4;
v1 = v3;
v9 = _mm_loadu_si128((const __m128i *)&tp);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
__int64 v1; // rbp
__int64 v2; // r13
__int64 v3; // rbx
__int64 v4; // r12
double v5; // xmm0_8
__int64 v6; // r8
__int64 v7; // r9
__int64 v8; // r10
__m128i v9; // [rsp+0h] [rbp-48h] BYREF
struct timespec tp; // [rsp+10h] [rbp-38h] BYREF

v1 = g_pktcnt;
v2 = g_totlen;
clock_gettime(0, (struct timespec *)&v9);
while ( 1 )
{
sleep(1u);
v3 = g_pktcnt;
v4 = g_totlen;
clock_gettime(0, &tp);
if ( v3 - v1 > 0 && v3 > 0 )
{
v5 = func1(v9.m128i_i32[0], v9.m128i_i64[1], tp.tv_sec, tp.tv_nsec);
printf("dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n", v5, v8, (v4 - v2) / v8, v4 / v3, v6, v7);
}
v2 = v4;
v1 = v3;
v9 = _mm_loadu_si128((const __m128i *)&tp);
}
}

original_code:
void* monitor_thread(void *parm)
{
   int64_t n0, n1, dn, l0, l1, dl;
   struct timespec t0, t1;
   double td;
   n0 = g_pktcnt;
   l0 = g_totlen;
   clock_gettime(0, &t0);
   while (1) {
      sleep(1);
      n1 = g_pktcnt;
      l1 = g_totlen;
      clock_gettime(0, &t1);
      td = tmdiff(t0, t1);
      dn = n1 - n0;
      dl = l1 - l0;
      if (dn > 0 && n1 > 0) {
         printf("dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n",
                td, dn, dl / dn, l1 / n1, l1, n1);
      }
      t0 = t1;
      n0 = n1;
      l0 = l1;
   }
   return ((void *)0);
}

canonical_original_code:
void *monitor_thread(void *parm)
{
   long n0; long n1; long dn; long l0; long l1; long dl;
   struct timespec t0; struct timespec t1;
   double td;
   n0 = g_pktcnt;
   l0 = g_totlen;
   clock_gettime(0, &t0);
   while (1) {
      sleep(1);
      n1 = g_pktcnt;
      l1 = g_totlen;
      clock_gettime(0, &t1);
      td = tmdiff(t0, t1);
      dn = n1 - n0;
      dl = l1 - l0;
      if (dn > 0 && n1 > 0) {
         printf("dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n",
                td, dn, dl / dn, l1 / n1, l1, n1);
      }
      t0 = t1;
      n0 = n1;
      l0 = l1;
   }
   return ((void *)0);
}

variable_types:
parm
void *

variable_types: dn long

variable_types: dl long

variable_types: td double

return_type: void *

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


partition

decompiled_code:
__int64 __fastcall partition(__int64 a1, int a2, int a3)
{
int v4; // er8
int *v5; // rbp
int v6; // er9
_DWORD *v7; // rcx
unsigned __int64 v8; // r11
unsigned int v9; // er9

v4 = a2 - 1;
v5 = (int *)(a1 + 4LL * a3);
v6 = *v5;
if ( a3 > a2 )
{
v7 = (_DWORD *)(a1 + 4LL * a2);
v8 = a1 + 4 * (a2 + (unsigned __int64)(unsigned int)(~a2 + a3)) + 4;
do
{
while ( *v7 > v6 )
{
if ( ++v7 == (_DWORD *)v8 )
goto LABEL_6;
}
swap((_DWORD *)(a1 + 4LL * (v4 + 1)), v7);
}
while ( v7 != (_DWORD *)v8 );
}
LABEL_6:
swap((_DWORD *)(a1 + 4LL * v4 + 4), v5);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2, int a3)
{
int v4; // er8
int *v5; // rbp
int v6; // er9
_DWORD *v7; // rcx
unsigned __int64 v8; // r11
unsigned int v9; // er9

v4 = a2 - 1;
v5 = (int *)(a1 + 4LL * a3);
v6 = *v5;
if ( a3 > a2 )
{
v7 = (_DWORD *)(a1 + 4LL * a2);
v8 = a1 + 4 * (a2 + (unsigned __int64)(unsigned int)(~a2 + a3)) + 4;
do
{
while ( *v7 > v6 )
{
if ( ++v7 == (_DWORD *)v8 )
goto LABEL_6;
}
func7((_DWORD *)(a1 + 4LL * (v4 + 1)), v7);
}
while ( v7 != (_DWORD *)v8 );
}
LABEL_6:
func7((_DWORD *)(a1 + 4LL * v4 + 4), v5);
return v9;
}

original_code:
static int partition (int a[], int l, int r)
{
   int x = a[r];
   int i = l - 1;
   for (int j = l; j <= r - 1; j++) {
      if (a[j] <= x) {
         i++;
         swap(&a[i], &a[j]);
      }
   }
   swap(&a[i + 1], &a[r]);
   return i + 1;
}

canonical_original_code:
static int partition(int a[], int l, int r)
{
   int x = a[r];
   int i = l - 1;
   for (int j = l; j <= r - 1; j++) {
      if (a[j] <= x) {
         i++;
         swap(&a[i], &a[j]);
      }
   }
   swap(&a[i + 1], &a[r]);
   return i + 1;
}

variable_types:
a
int []

variable_types: l int

variable_types: r int


quicksort

decompiled_code:
void __fastcall quicksort(__int64 a1, int a2, int a3)
{
unsigned int v4; // ebp
int v5; // ebx

if ( a3 > a2 )
{
v4 = a2;
do
{
v5 = partition(a1, v4, a3);
quicksort(a1, v4, (unsigned int)(v5 - 1));
v4 = v5 + 1;
}
while ( v5 + 1 < a3 );
}
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int a2, int a3)
{
unsigned int v4; // ebp
int v5; // ebx

if ( a3 > a2 )
{
v4 = a2;
do
{
v5 = func8(a1, v4, a3);
func9(a1, v4, (unsigned int)(v5 - 1));
v4 = v5 + 1;
}
while ( v5 + 1 < a3 );
}
}

original_code:
static void quicksort(int a[], int l, int r)
{
   if (l < r) {
      int p = partition(a, l, r);
      quicksort(a, l, p - 1);
      quicksort(a, p + 1, r);
   }
}

variable_types: p int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__int64 v4; // rsi
__int64 vars0[3]; // [rsp+0h] [rbp+0h] BYREF
char vars18[32]; // [rsp+18h] [rbp+18h] BYREF

vars0[0] = 0x70000000ALL;
v3 = (char *)vars0;
vars0[1] = 0x900000008LL;
vars0[2] = 0x500000001LL;
printf("Original array (n=%d): ", 6LL);
do
printf("%d ", LODWORD(vars0[0]));
while ( vars0 != (__int64 *)vars18 );
putchar(10);
quicksort(vars0, 0LL, 5LL);
printf("Sorted array: ");
do
{
v4 = *(unsigned int *)v3;
v3 += 4;
printf("%d ", v4);
}
while ( v3 != vars18 );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__int64 v4; // rsi
__int64 vars0[3]; // [rsp+0h] [rbp+0h] BYREF
char vars18[32]; // [rsp+18h] [rbp+18h] BYREF

vars0[0] = 0x70000000ALL;
v3 = (char *)vars0;
vars0[1] = 0x900000008LL;
vars0[2] = 0x500000001LL;
printf("Original array (n=%d): ", 6LL);
do
printf("%d ", LODWORD(vars0[0]));
while ( vars0 != (__int64 *)vars18 );
putchar(10);
func9(vars0, 0LL, 5LL);
printf("Sorted array: ");
do
{
v4 = *(unsigned int *)v3;
v3 += 4;
printf("%d ", v4);
}
while ( v3 != vars18 );
putchar(10);
return 0;
}

original_code:
int main(void)
{
   int i;
   int a[] = {10, 7, 8, 9, 1, 5};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   quicksort(a, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}

canonical_original_code:
int main()
{
   int i;
   int a[] = {10, 7, 8, 9, 1, 5};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   quicksort(a, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}

variable_types: i int

variable_types: n int


swap

decompiled_code:
__int64 __fastcall swap(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

original_code:
void swap(int *a, int *b)
{
   int t = *a;
   *a = *b;
   *b = t;
}

variable_types:
a
int *

variable_types:
b
int *

variable_types: t int


heapify

original_code:
void heapify(maxheap_t *heap, int idx)
{
    int g = idx;
    int l = (idx << 1) + 1;
    int r = (idx + 1) << 1;
    if ((l < heap->size) && (heap->array[l] > heap->array[g])) {
        g = l;
    }
    if ((r < heap->size) && (heap->array[r] > heap->array[g])) {
        g = r;
    }
    if (g != idx) {
        swap(&heap->array[g], &heap->array[idx]);
        heapify(heap, g);
    }
}

canonical_original_code:
void heapify(struct maxheap_s *heap, int idx)
{
    int g = idx;
    int l = (idx << 1) + 1;
    int r = (idx + 1) << 1;
    if ((l < heap->size) && (heap->array[l] > heap->array[g])) {
        g = l;
    }
    if ((r < heap->size) && (heap->array[r] > heap->array[g])) {
        g = r;
    }
    if (g != idx) {
        swap(&heap->array[g], &heap->array[idx]);
        heapify(heap, g);
    }
}

variable_types: struct maxheap_s { int size; int *array; } *

variable_types: idx int

variable_types: g int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__int64 v4; // rsi
__int64 vars0[3]; // [rsp+0h] [rbp+0h] BYREF
char vars18[32]; // [rsp+18h] [rbp+18h] BYREF

vars0[0] = 0xB0000000CLL;
v3 = (char *)vars0;
vars0[1] = 0x50000000DLL;
vars0[2] = 0x700000006LL;
printf("Original array (n=%d): ", 6LL);
do
printf("%d ", LODWORD(vars0[0]));
while ( vars0 != (__int64 *)vars18 );
putchar(10);
heapsort_constprop_0(vars0);
printf("Sorted array: ");
do
{
v4 = *(unsigned int *)v3;
v3 += 4;
printf("%d ", v4);
}
while ( v3 != vars18 );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__int64 v4; // rsi
__int64 vars0[3]; // [rsp+0h] [rbp+0h] BYREF
char vars18[32]; // [rsp+18h] [rbp+18h] BYREF

vars0[0] = 0xB0000000CLL;
v3 = (char *)vars0;
vars0[1] = 0x50000000DLL;
vars0[2] = 0x700000006LL;
printf("Original array (n=%d): ", 6LL);
do
printf("%d ", LODWORD(vars0[0]));
while ( vars0 != (__int64 *)vars18 );
putchar(10);
func10(vars0);
printf("Sorted array: ");
do
{
v4 = *(unsigned int *)v3;
v3 += 4;
printf("%d ", v4);
}
while ( v3 != vars18 );
putchar(10);
return 0;
}

original_code:
int main(void)
{
   int i;
   int a[] = {12, 11, 13, 5, 6, 7};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   heapsort(a, n);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}

canonical_original_code:
int main()
{
   int i;
   int a[] = {12, 11, 13, 5, 6, 7};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   heapsort(a, n);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}


swap

original_code:
static void swap(int* a, int* b) {
   int t = *a;
   *a = *b;
   *b = t;
}

canonical_original_code:
static void swap(int *a, int *b) {
   int t = *a;
   *a = *b;
   *b = t;
}


merge

variable_types: m int

variable_types: j int

variable_types: k int

variable_types:
la
int [-1]

variable_types:
ra
int [-1]


mergesort

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

if ( (int)a2 < a3 )
{
v4 = ((int)a2 + a3) / 2;
mergesort(a1, a2, v4);
mergesort(a1, v4 + 1, (unsigned int)a3);
result = merge(a1, a2, v4, a3);
}
return result;
}

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

if ( (int)a2 < a3 )
{
v4 = ((int)a2 + a3) / 2;
func8(a1, a2, v4);
func8(a1, v4 + 1, (unsigned int)a3);
result = func7(a1, a2, v4, a3);
}
return result;
}

original_code:
void mergesort(int a[], int l, int r)
{
    int m;
    if (l < r) {
         m = (l + r) / 2;
         mergesort(a, l, m);
         mergesort(a, m + 1, r);
         merge(a, l, m, r);
    }
}

variable_types:
a
int []


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // r12
char *v5; // r12
__int64 v6; // rsi
__int64 v7; // rsi
_BYTE v9[48]; // [rsp+0h] [rbp-48h] BYREF
char v10[24]; // [rsp+30h] [rbp-18h] BYREF

v3 = v9;
v4 = v9;
do
{
v4 += 4;
*((_DWORD *)v4 - 1) = rand() % 12;
}
while ( v4 != v10 );
v5 = v9;
printf("Original array (n=%d): ", 12LL);
do
{
v6 = *(unsigned int *)v5;
v5 += 4;
printf("%d ", v6);
}
while ( v5 != v10 );
putchar(10);
mergesort(v9, 0LL, 11LL);
printf("Sorted array: ");
do
{
v7 = *(unsigned int *)v3;
v3 += 4;
printf("%d ", v7);
}
while ( v3 != v10 );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // r12
char *v5; // r12
__int64 v6; // rsi
__int64 v7; // rsi
_BYTE v9[48]; // [rsp+0h] [rbp-48h] BYREF
char v10[24]; // [rsp+30h] [rbp-18h] BYREF

v3 = v9;
v4 = v9;
do
{
v4 += 4;
*((_DWORD *)v4 - 1) = rand() % 12;
}
while ( v4 != v10 );
v5 = v9;
printf("Original array (n=%d): ", 12LL);
do
{
v6 = *(unsigned int *)v5;
v5 += 4;
printf("%d ", v6);
}
while ( v5 != v10 );
putchar(10);
func8(v9, 0LL, 11LL);
printf("Sorted array: ");
do
{
v7 = *(unsigned int *)v3;
v3 += 4;
printf("%d ", v7);
}
while ( v3 != v10 );
putchar(10);
return 0;
}

original_code:
int main(void)
{
   int i, n = 12;
   int mtable[12];
   for (i = 0; i < 12; i++) {
      mtable[i] = rand() % 12;
   }
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   mergesort(mtable, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   return 0;
}

canonical_original_code:
int main()
{
   int i; int n = 12;
   int mtable[12];
   for (i = 0; i < 12; i++) {
      mtable[i] = rand() % 12;
   }
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   mergesort(mtable, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   return 0;
}

variable_types:
mtable
int [12]


dictGenericDelete

original_code:
static int dictGenericDelete(struct dict *d, const void *key, int nofree)
{
    unsigned int h, idx;
    struct dictEntry *he, *prevHe;
    if (d->size == 0) return 1;
    h = (d)->type->hashFunction(key);
    idx = h & d->sizemask;
    he = d->table[idx];
    prevHe = ((void *)0);
    while(he) {
        if ((((d)->type->keyCompare) ? (d)->type->keyCompare(key, he->key) : (key) == (he->key))) {
            if (prevHe)
                prevHe->next = he->next;
            else
                d->table[idx] = he->next;
            if (!nofree) {
                if ((d)->type->keyDestructor && he->key) (d)->type->keyDestructor((he)->key);
                if ((d)->type->valDestructor && (he)->v.val) (d)->type->valDestructor((he)->v.val);
            }
            wfree(he);
            d->used--;
            return 0;
        }
        prevHe = he;
        he = he->next;
    }
    return 1;
}

canonical_original_code:
static int dictGenericDelete(struct dict *d, void *key, int nofree)
{
    unsigned int h; unsigned int idx;
    struct dictEntry *he; struct dictEntry *prevHe;
    if (d->size == 0) return 1;
    h = (d)->type->hashFunction(key);
    idx = h & d->sizemask;
    he = d->table[idx];
    prevHe = ((void *)0);
    while(he) {
        if ((((d)->type->keyCompare) ? (d)->type->keyCompare(key, he->key) : (key) == (he->key))) {
            if (prevHe)
                prevHe->next = he->next;
            else
                d->table[idx] = he->next;
            if (!nofree) {
                if ((d)->type->keyDestructor && he->key) (d)->type->keyDestructor((he)->key);
                if ((d)->type->valDestructor && (he)->v.val) (d)->type->valDestructor((he)->v.val);
            }
            wfree(he);
            d->used--;
            return 0;
        }
        prevHe = he;
        he = he->next;
    }
    return 1;
}

variable_types: struct dict { struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; } **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType { unsigned int (*hashFunction)(void *key); void *(*keyDup)(void *key); void *(*valDup)(void *obj); int (*keyCompare)(void *key1, void *key2); void (*keyDestructor)(void *key); void (*valDestructor)(void *obj); } *type; } *

variable_types:
key
void *

variable_types: nofree int

variable_types:
h
unsigned int

variable_types:
idx
unsigned int

user_defined_types: struct dict { struct dictEntry **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType *type; }


dictExpand

decompiled_code:
__int64 __fastcall dictExpand(__int64 *a1, unsigned __int64 a2)
{
unsigned __int64 v3; // rax
unsigned int v4; // er8
__int64 v5; // rdi
size_t v6; // r12
void *v7; // rax

v3 = dictNextPower(a2);
if ( a1[3] <= a2 )
{
v5 = *a1;
a1[1] = v3;
v6 = 8 * v3;
a1[2] = v3 - 1;
if ( v5 )
v7 = (void *)wrealloc(v5, 8 * v3);
else
v7 = (void *)wmalloc(8 * v3);
*a1 = (__int64)v7;
memset(v7, 0, v6);
v4 = 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1, unsigned __int64 a2)
{
unsigned __int64 v3; // rax
unsigned int v4; // er8
__int64 v5; // rdi
size_t v6; // r12
void *v7; // rax

v3 = func0(a2);
if ( a1[3] <= a2 )
{
v5 = *a1;
a1[1] = v3;
v6 = 8 * v3;
a1[2] = v3 - 1;
if ( v5 )
v7 = (void *)wrealloc(v5, 8 * v3);
else
v7 = (void *)wmalloc(8 * v3);
*a1 = (__int64)v7;
memset(v7, 0, v6);
v4 = 0;
}
return v4;
}

original_code:
int dictExpand(struct dict *d, unsigned long size)
{
    unsigned long realsize = _dictNextPower(size);
    if (d->used > size)
        return 1;
    d->size = realsize;
    d->sizemask = realsize-1;
    if (d->table)
        d->table = wrealloc(d->table, realsize*sizeof(struct dictEntry*));
    else
        d->table = wmalloc(realsize*sizeof(struct dictEntry*));
    memset(d->table, 0, realsize*sizeof(struct dictEntry*));
    return 0;
}

canonical_original_code:
int dictExpand(struct dict *d, unsigned long size)
{
    unsigned long realsize = _dictNextPower(size);
    if (d->used > size)
        return 1;
    d->size = realsize;
    d->sizemask = realsize-1;
    if (d->table)
        d->table = wrealloc(d->table, realsize*sizeof(struct dictEntry *));
    else
        d->table = wmalloc(realsize*sizeof(struct dictEntry *));
    memset(d->table, 0, realsize*sizeof(struct dictEntry *));
    return 0;
}

variable_types:
size
unsigned long

variable_types:
realsize
unsigned long

user_defined_types: struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; }

user_defined_types: struct dictType { unsigned int (*hashFunction)(void *key); void *(*keyDup)(void *key); void *(*valDup)(void *obj); int (*keyCompare)(void *key1, void *key2); void (*keyDestructor)(void *key); void (*valDestructor)(void *obj); }


dictAdd

decompiled_code:
__int64 __fastcall dictAdd(_QWORD *a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
_QWORD *v5; // rbp
__int64 (__fastcall *v6)(__int64); // rax

v4 = dictAddRaw(a1, a2);
if ( !v4 )
return 1LL;
v5 = v4;
v6 = *(__int64 (__fastcall **)(__int64))(a1[4] + 16LL);
if ( v6 )
v5[1] = v6(a3);
else
v5[1] = a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
_QWORD *v5; // rbp
__int64 (__fastcall *v6)(__int64); // rax

v4 = dictAddRaw(a1, a2);
if ( !v4 )
return 1LL;
v5 = v4;
v6 = *(__int64 (__fastcall **)(__int64))(a1[4] + 16LL);
if ( v6 )
v5[1] = v6(a3);
else
v5[1] = a3;
return 0LL;
}

original_code:
int dictAdd(struct dict *d, void *key, void *val)
{
    struct dictEntry *entry = dictAddRaw(d,key);
    if (!entry) return 1;
    do { if ((d)->type->valDup) entry->v.val = (d)->type->valDup(val); else entry->v.val = (val); } while(0);
    return 0;
}

variable_types:
val
void *

variable_types: struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; } *


dictReplace

original_code:
int dictReplace(struct dict *d, void *key, void *val, int *replace)
{
    struct dictEntry *entry, auxentry;
    if (dictAdd(d, key, val) == 0) {
        if (replace)
            *replace = 0;
        return 0;
    }
    entry = dictFind(d, key);
    if (entry == ((void *)0)) {
        return 1;
    }
    auxentry = *entry;
    do { if ((d)->type->valDup) entry->v.val = (d)->type->valDup(val); else entry->v.val = (val); } while(0);
    if ((d)->type->valDestructor && (&auxentry)->v.val) (d)->type->valDestructor((&auxentry)->v.val);
    if (replace)
        *replace = 1;
    return 0;
}

canonical_original_code:
int dictReplace(struct dict *d, void *key, void *val, int *replace)
{
    struct dictEntry *entry; struct dictEntry auxentry;
    if (dictAdd(d, key, val) == 0) {
        if (replace)
            *replace = 0;
        return 0;
    }
    entry = dictFind(d, key);
    if (entry == ((void *)0)) {
        return 1;
    }
    auxentry = *entry;
    do { if ((d)->type->valDup) entry->v.val = (d)->type->valDup(val); else entry->v.val = (val); } while(0);
    if ((d)->type->valDestructor && (&auxentry)->v.val) (d)->type->valDestructor((&auxentry)->v.val);
    if (replace)
        *replace = 1;
    return 0;
}

variable_types:
replace
int *

variable_types: struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; }


dictDelete

decompiled_code:
__int64 __fastcall dictDelete(__int64 a1, __int64 a2)
{
return dictGenericDelete(a1, a2, 0);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
return func1(a1, a2, 0);
}

original_code:
int dictDelete(struct dict *ht, const void *key) {
    return dictGenericDelete(ht, key, 0);
}

canonical_original_code:
int dictDelete(struct dict *ht, void *key) {
    return dictGenericDelete(ht, key, 0);
}


dictDeleteNoFree

decompiled_code:
__int64 __fastcall dictDeleteNoFree(__int64 a1, __int64 a2)
{
return dictGenericDelete(a1, a2, 1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
return func1(a1, a2, 1);
}

original_code:
int dictDeleteNoFree(struct dict *ht, const void *key) {
    return dictGenericDelete(ht, key, 1);
}

canonical_original_code:
int dictDeleteNoFree(struct dict *ht, void *key) {
    return dictGenericDelete(ht, key, 1);
}


dictRelease

decompiled_code:
__int64 __fastcall dictRelease(__int64 *a1)
{
dictClear(a1);
return wfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 *a1)
{
func9(a1);
return wfree(a1);
}

original_code:
void dictRelease(struct dict *d)
{
    dictClear(d);
    wfree(d);
}


dictNext

decompiled_code:
__int64 __fastcall dictNext(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx
int v3; // eax

if ( a1[2] && (result = a1[3], (a1[2] = result) != 0) )
{
LABEL_5:
a1[3] = *(_QWORD *)(result + 16);
}
else
{
while ( 1 )
{
v2 = *a1;
v3 = *((_DWORD *)a1 + 3) + 1;
*((_DWORD *)a1 + 3) = v3;
if ( v3 >= *(_DWORD *)(v2 + 8) )
break;
result = *(_QWORD *)(*(_QWORD *)v2 + 8LL * v3);
a1[2] = result;
if ( result )
goto LABEL_5;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx
int v3; // eax

if ( a1[2] && (result = a1[3], (a1[2] = result) != 0) )
{
LABEL_5:
a1[3] = *(_QWORD *)(result + 16);
}
else
{
while ( 1 )
{
v2 = *a1;
v3 = *((_DWORD *)a1 + 3) + 1;
*((_DWORD *)a1 + 3) = v3;
if ( v3 >= *(_DWORD *)(v2 + 8) )
break;
result = *(_QWORD *)(*(_QWORD *)v2 + 8LL * v3);
a1[2] = result;
if ( result )
goto LABEL_5;
}
result = 0LL;
}
return result;
}

original_code:
struct dictEntry *dictNext(struct dictIterator *iter)
{
    while (1) {
        if (iter->entry == ((void *)0)) {
            struct dict *d = iter->d;
            iter->index++;
            if (iter->index >= (signed) d->size) {
                break;
            }
            iter->entry = d->table[iter->index];
        } else {
            iter->entry = iter->nextEntry;
        }
        if (iter->entry) {
            iter->nextEntry = iter->entry->next;
            return iter->entry;
        }
    }
    return ((void *)0);
}

canonical_original_code:
struct dictEntry *dictNext(struct dictIterator *iter)
{
    while (1) {
        if (iter->entry == ((void *)0)) {
            struct dict *d = iter->d;
            iter->index++;
            if (iter->index >= (int) d->size) {
                break;
            }
            iter->entry = d->table[iter->index];
        } else {
            iter->entry = iter->nextEntry;
        }
        if (iter->entry) {
            iter->nextEntry = iter->entry->next;
            return iter->entry;
        }
    }
    return ((void *)0);
}

variable_types: struct dictIterator { struct dict { struct dictEntry **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType { unsigned int (*hashFunction)(void *key); void *(*keyDup)(void *key); void *(*valDup)(void *obj); int (*keyCompare)(void *key1, void *key2); void (*keyDestructor)(void *key); void (*valDestructor)(void *obj); } *type; } *d; int table; struct dictEntry *entry; } *

return_type: struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; } *

user_defined_types: struct dictIterator { struct dict *d; int table; struct dictEntry *entry; }


dictReleaseIterator

decompiled_code:
// attributes: thunk
__int64 __fastcall dictReleaseIterator(__int64 a1)
{
return wfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return wfree(a1);
}

original_code:
void dictReleaseIterator(struct dictIterator *iter)
{
    wfree(iter);
}

variable_types: struct dictIterator { struct dict { struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; } **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType { unsigned int (*hashFunction)(void *key); void *(*keyDup)(void *key); void *(*valDup)(void *obj); int (*keyCompare)(void *key1, void *key2); void (*keyDestructor)(void *key); void (*valDestructor)(void *obj); } *type; } *d; int table; struct dictEntry *entry; } *


dictGenHashFunction

original_code:
unsigned int dictGenHashFunction(const void *key, int len) {
    uint32_t seed = dict_hash_function_seed;
    const uint32_t m = 0x5bd1e995;
    const int r = 24;
    uint32_t h = seed ^ len;
    const unsigned char *data = (const unsigned char *)key;
    while(len >= 4) {
        uint32_t k = *(uint32_t*)data;
        k *= m;
        k ^= k >> r;
        k *= m;
        h *= m;
        h ^= k;
        data += 4;
        len -= 4;
    }
    switch(len) {
    case 3: h ^= data[2] << 16;
    case 2: h ^= data[1] << 8;
    case 1: h ^= data[0]; h *= m;
    };
    h ^= h >> 13;
    h *= m;
    h ^= h >> 15;
    return (unsigned int)h;
}

canonical_original_code:
unsigned int dictGenHashFunction(void *key, int len) {
    unsigned int seed = dict_hash_function_seed;
    unsigned int m = 0x5bd1e995;
    int r = 24;
    unsigned int h = seed ^ len;
    unsigned char *data = (const unsigned char *)key;
    while(len >= 4) {
        unsigned int k = *(uint32_t*)data;
        k *= m;
        k ^= k >> r;
        k *= m;
        h *= m;
        h ^= k;
        data += 4;
        len -= 4;
    }
    switch(len) {
    case 3: h ^= data[2] << 16;
    case 2: h ^= data[1] << 8;
    case 1: h ^= data[0]; h *= m;
    };
    h ^= h >> 13;
    h *= m;
    h ^= h >> 15;
    return (unsigned int)h;
}

variable_types: len int

variable_types:
seed
unsigned int

variable_types:
m
unsigned int

variable_types: r int

variable_types:
data
unsigned char *

variable_types:
k
unsigned int

return_type: unsigned int


dictGenCaseHashFunction

decompiled_code:
__int64 __fastcall dictGenCaseHashFunction(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rbx
const __int32_t **v3; // rax
unsigned int v4; // er8
__int64 v5; // rdx

if ( !a2 )
return 2128LL;
v2 = a1;
v3 = _ctype_tolower_loc();
v4 = 2128;
do
{
v5 = *v2++;
v4 += (*v3)[v5] + 32 * v4;
}
while ( v2 != &a1[a2] );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rbx
const __int32_t **v3; // rax
unsigned int v4; // er8
__int64 v5; // rdx

if ( !a2 )
return 2128LL;
v2 = a1;
v3 = _ctype_tolower_loc();
v4 = 2128;
do
{
v5 = *v2++;
v4 += (*v3)[v5] + 32 * v4;
}
while ( v2 != &a1[a2] );
return v4;
}

original_code:
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {
    unsigned int hash = (unsigned int)dict_hash_function_seed;
    while (len--)
        hash = ((hash << 5) + hash) + (tolower(*buf++));
    return hash;
}

canonical_original_code:
unsigned int dictGenCaseHashFunction(unsigned char *buf, int len) {
    unsigned int hash = (unsigned int)dict_hash_function_seed;
    while (len--)
        hash = ((hash << 5) + hash) + (tolower(*buf++));
    return hash;
}

variable_types:
buf
unsigned char *

variable_types:
hash
unsigned int


dictPrint

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

v1 = dictGetIterator(a1);
while ( 1 )
{
v2 = (const char **)dictNext(v1);
if ( !v2 )
break;
printf("%s -> %s \n", *v2, v2[1]);
}
return dictReleaseIterator((__int64)v1);
}

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

v1 = dictGetIterator(a1);
while ( 1 )
{
v2 = (const char **)func11(v1);
if ( !v2 )
break;
printf("%s -> %s \n", *v2, v2[1]);
}
return func12((__int64)v1);
}

original_code:
void dictPrint(struct dict *d)
{
    struct dictIterator *iter = dictGetIterator(d);
    struct dictEntry *entry;
    while ((entry = dictNext(iter)) != ((void *)0)) {
        printf("%s -> %s \n", (char *)entry->key, (char *)entry->v.val);
    }
    dictReleaseIterator(iter);
}

variable_types: struct dict { struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; } **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType { unsigned int (*hashFunction)(void *key); void *(*keyDup)(void *key); void *(*valDup)(void *obj); int (*keyCompare)(void *key1, void *key2); void (*keyDestructor)(void *key); void (*valDestructor)(void *obj); } *type; } *


dictPrintStats

decompiled_code:
int __fastcall dictPrintStats(__int64 a1)
{
__int64 v1; // r12
__int64 *v2; // rcx
__int64 v3; // r14
unsigned __int64 v4; // r13
__int64 v5; // rsi
__int64 v6; // rax
unsigned __int64 v7; // rdx
__int64 v8; // rax
float v9; // xmm1_4
float v10; // xmm0_4
__int64 v11; // rax
float v12; // xmm0_4
__int64 v13; // r12
int result; // eax
__int64 v15; // rax
float v16; // xmm0_4
float v17; // xmm1_4
__int64 v18; // rcx
__int64 v19; // rdx
__int64 v20[55]; // [rsp+4h] [rbp-1B8h] BYREF

if ( *(_QWORD *)(a1 + 24) )
{
v1 = *(_QWORD *)(a1 + 8);
memset(v20, 0, 0x190uLL);
if ( v1 )
{
v2 = *(__int64 **)a1;
v3 = 0LL;
v4 = 0LL;
v5 = *(_QWORD *)a1 + 8 * v1;
v1 = 0LL;
do
{
v6 = *v2;
if ( *v2 )
{
++v1;
v7 = 0LL;
do
{
v6 = *(_QWORD *)(v6 + 16);
++v7;
}
while ( v6 );
v8 = 49LL;
if ( v7 <= 0x31 )
v8 = v7;
++v20[v8];
if ( v4 < v7 )
v4 = v7;
v3 += v7;
}
else
{
++v20[0];
}
++v2;
}
while ( (__int64 *)v5 != v2 );
}
else
{
v3 = 0LL;
v4 = 0LL;
}
puts("Hash table stats:");
printf(" table size: %ld\n", *(_QWORD *)(a1 + 8));
printf(" number of elements: %ld\n", *(_QWORD *)(a1 + 24));
printf(" different slots: %ld\n", v1);
printf(" max chain length: %ld\n", v4);
if ( v1 < 0 )
{
v9 = (float)(v1 & 1 | (unsigned int)((unsigned __int64)v1 >> 1))
+ (float)(v1 & 1 | (unsigned int)((unsigned __int64)v1 >> 1));
if ( v3 >= 0 )
goto LABEL_15;
}
else
{
v9 = (float)(int)v1;
if ( v3 >= 0 )
{
LABEL_15:
v10 = (float)(int)v3;
LABEL_16:
printf(" avg chain length (counted): %.02f\n", (float)(v10 / v9));
v11 = *(_QWORD *)(a1 + 24);
if ( v11 < 0 )
{
v19 = *(_QWORD *)(a1 + 24) & 1LL | (*(_QWORD *)(a1 + 24) >> 1);
v12 = (float)(int)v19 + (float)(int)v19;
}
else
{
v12 = (float)(int)v11;
}
v13 = 0LL;
printf(" avg chain length (computed): %.02f\n", (float)(v12 / v9));
result = puts(" Chain length distribution:");
while ( 1 )
{
v18 = v20[v13];
if ( !v18 )
goto LABEL_22;
if ( v18 >= 0 )
{
v15 = *(_QWORD *)(a1 + 8);
v16 = (float)(int)v18;
if ( v15 < 0 )
goto LABEL_26;
}
else
{
v15 = *(_QWORD *)(a1 + 8);
v16 = (float)(int)(v20[v13] & 1 | ((unsigned __int64)v18 >> 1))
+ (float)(int)(v20[v13] & 1 | ((unsigned __int64)v18 >> 1));
if ( v15 < 0 )
{
LABEL_26:
v17 = (float)(v15 & 1 | (unsigned int)((unsigned __int64)v15 >> 1))
+ (float)(v15 & 1 | (unsigned int)((unsigned __int64)v15 >> 1));
goto LABEL_21;
}
}
v17 = (float)(int)v15;
LABEL_21:
result = printf(
" %s%ld: %ld (%.02f%%)\n",
(const char *)&unk_BA9,
v13,
v18,
(float)((float)(v16 / v17) * 100.0));
LABEL_22:
if ( ++v13 == 49 )
return result;
}
}
}
v10 = (float)(v3 & 1 | (unsigned int)((unsigned __int64)v3 >> 1))
+ (float)(v3 & 1 | (unsigned int)((unsigned __int64)v3 >> 1));
goto LABEL_16;
}
return puts("No stats available for empty dictionaries");
}

original_code:
void dictPrintStats(struct dict *d) {
    unsigned long i, slots = 0, chainlen, maxchainlen = 0;
    unsigned long totchainlen = 0;
    unsigned long clvector[50];
    if (d->used == 0) {
        printf("No stats available for empty dictionaries\n");
        return;
    }
    for (i = 0; i < 50; i++) clvector[i] = 0;
    for (i = 0; i < d->size; i++) {
        struct dictEntry *he;
        if (d->table[i] == ((void *)0)) {
            clvector[0]++;
            continue;
        }
        slots++;
        chainlen = 0;
        he = d->table[i];
        while(he) {
            chainlen++;
            he = he->next;
        }
        clvector[(chainlen < 50) ? chainlen : (50 -1)]++;
        if (chainlen > maxchainlen) maxchainlen = chainlen;
        totchainlen += chainlen;
    }
    printf("Hash table stats:\n");
    printf(" table size: %ld\n", d->size);
    printf(" number of elements: %ld\n", d->used);
    printf(" different slots: %ld\n", slots);
    printf(" max chain length: %ld\n", maxchainlen);
    printf(" avg chain length (counted): %.02f\n", (float)totchainlen/slots);
    printf(" avg chain length (computed): %.02f\n", (float)d->used/slots);
    printf(" Chain length distribution:\n");
    for (i = 0; i < 50 -1; i++) {
        if (clvector[i] == 0) continue;
        printf("   %s%ld: %ld (%.02f%%)\n",(i == 50 -1)?">= ":"", i, clvector[i], ((float)clvector[i]/d->size)*100);
    }
}

canonical_original_code:
void dictPrintStats(struct dict *d) {
    unsigned long i; unsigned long slots = 0; unsigned long chainlen; unsigned long maxchainlen = 0;
    unsigned long totchainlen = 0;
    unsigned long clvector[50];
    if (d->used == 0) {
        printf("No stats available for empty dictionaries\n");
        return;
    }
    for (i = 0; i < 50; i++) clvector[i] = 0;
    for (i = 0; i < d->size; i++) {
        struct dictEntry *he;
        if (d->table[i] == ((void *)0)) {
            clvector[0]++;
            continue;
        }
        slots++;
        chainlen = 0;
        he = d->table[i];
        while(he) {
            chainlen++;
            he = he->next;
        }
        clvector[(chainlen < 50) ? chainlen : (50 -1)]++;
        if (chainlen > maxchainlen) maxchainlen = chainlen;
        totchainlen += chainlen;
    }
    printf("Hash table stats:\n");
    printf(" table size: %ld\n", d->size);
    printf(" number of elements: %ld\n", d->used);
    printf(" different slots: %ld\n", slots);
    printf(" max chain length: %ld\n", maxchainlen);
    printf(" avg chain length (counted): %.02f\n", (float)totchainlen/slots);
    printf(" avg chain length (computed): %.02f\n", (float)d->used/slots);
    printf(" Chain length distribution:\n");
    for (i = 0; i < 50 -1; i++) {
        if (clvector[i] == 0) continue;
        printf("   %s%ld: %ld (%.02f%%)\n",(i == 50 -1)?">= ":"", i, clvector[i], ((float)clvector[i]/d->size)*100);
    }
}

variable_types:
i
unsigned long

variable_types:
slots
unsigned long

variable_types:
chainlen
unsigned long

variable_types:
maxchainlen
unsigned long

variable_types:
totchainlen
unsigned long

variable_types:
clvector
unsigned long [50]


wstrlen

decompiled_code:
__int64 __fastcall wstrlen(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

original_code:
static inline int wstrlen(const wstr s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

canonical_original_code:
static inline int wstrlen(char *s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

variable_types:
s
char *

variable_types: struct wstrhd { int len; int free; char buf[]; } *


readBulkFrom

decompiled_code:
ssize_t __fastcall readBulkFrom(unsigned int a1, __int64 a2)
{
ssize_t result; // rax
int v4; // edi
char *v5; // rax

result = read(a1, *(void **)a2, *(_QWORD *)(a2 + 8));
if ( result == -1 )
{
v4 = *_errno_location();
if ( v4 == 11 )
{
result = 0LL;
}
else
{
v5 = strerror(v4);
wheatLog(2LL, "Reading from fd %d: %s", a1, v5);
result = 0xFFFFFFFFLL;
}
}
else if ( !result )
{
wheatLog(0LL, "Peer close file descriptor %d", a1);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(unsigned int a1, __int64 a2)
{
ssize_t result; // rax
int v4; // edi
char *v5; // rax

result = read(a1, *(void **)a2, *(_QWORD *)(a2 + 8));
if ( result == -1 )
{
v4 = *_errno_location();
if ( v4 == 11 )
{
result = 0LL;
}
else
{
v5 = strerror(v4);
wheatLog(2LL, "Reading from fd %d: %s", a1, v5);
result = 0xFFFFFFFFLL;
}
}
else if ( !result )
{
wheatLog(0LL, "Peer close file descriptor %d", a1);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int readBulkFrom(int fd, struct slice *slice)
{
    ssize_t nread;
    nread = read(fd, slice->data, slice->len);
    if (nread == -1) {
        if ((*__errno_location ()) == 11) {
            nread = 0;
        } else {
            wheatLog(2, "Reading from fd %d: %s", fd, strerror((*__errno_location ())));
            return -1;
        }
    } else if (nread == 0) {
        wheatLog(0, "Peer close file descriptor %d", fd);
        return -1;
    }
    return (int)nread;
}

canonical_original_code:
int readBulkFrom(int fd, struct slice *slice)
{
    long nread;
    nread = read(fd, slice->data, slice->len);
    if (nread == -1) {
        if ((*__errno_location ()) == 11) {
            nread = 0;
        } else {
            wheatLog(2, "Reading from fd %d: %s", fd, strerror((*__errno_location ())));
            return -1;
        }
    } else if (nread == 0) {
        wheatLog(0, "Peer close file descriptor %d", fd);
        return -1;
    }
    return (int)nread;
}

variable_types: fd int

variable_types: struct slice { unsigned char *data; unsigned long len; } *

variable_types: nread long


syncReadBulkFrom

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

do
result = readBulkFrom(a1, a2);
while ( !(_DWORD)result );
return result;
}

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

do
result = func1(a1, a2);
while ( !(_DWORD)result );
return result;
}

original_code:
int syncReadBulkFrom(int fd, struct slice *slice)
{
   int read = 0;
   do {
       read = readBulkFrom(fd, slice);
   } while(read == 0);
   return read;
}

variable_types: read int

user_defined_types: struct slice { unsigned char *data; unsigned long len; }


writeBulkTo

decompiled_code:
__int64 __fastcall writeBulkTo(int a1, __int64 a2)
{
ssize_t v2; // rax
unsigned int v3; // er8
int *v5; // rax
int v6; // edi
char *v7; // rax
char *v8; // rax

v2 = write(a1, *(const void **)a2, *(_QWORD *)(a2 + 8));
v3 = v2;
if ( v2 != -1 )
return v3;
v5 = _errno_location();
v3 = 0;
v6 = *v5;
if ( *v5 == 11 )
return v3;
if ( v6 == 32 )
{
v8 = strerror(32);
wheatLog(0LL, "Receive RST, peer closed", v8);
return (unsigned int)-1;
}
v7 = strerror(v6);
wheatLog(2LL, "Error writing to client: %s", v7);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2)
{
ssize_t v2; // rax
unsigned int v3; // er8
int *v5; // rax
int v6; // edi
char *v7; // rax
char *v8; // rax

v2 = write(a1, *(const void **)a2, *(_QWORD *)(a2 + 8));
v3 = v2;
if ( v2 != -1 )
return v3;
v5 = _errno_location();
v3 = 0;
v6 = *v5;
if ( *v5 == 11 )
return v3;
if ( v6 == 32 )
{
v8 = strerror(32);
wheatLog(0LL, "Receive RST, peer closed", v8);
return (unsigned int)-1;
}
v7 = strerror(v6);
wheatLog(2LL, "Error writing to client: %s", v7);
return 0xFFFFFFFFLL;
}

original_code:
int writeBulkTo(int fd, struct slice *slice)
{
    ssize_t nwritten;
    nwritten = write(fd, slice->data, slice->len);
    if (nwritten == -1) {
        if ((*__errno_location ()) == 11) {
            nwritten = 0;
        } else if ((*__errno_location ()) == 32) {
            wheatLog(0, "Receive RST, peer closed", strerror((*__errno_location ())));
            return -1;
        } else {
            wheatLog(2,
                "Error writing to client: %s", strerror((*__errno_location ())));
            return -1;
        }
    }
    return (int)nwritten;
}

canonical_original_code:
int writeBulkTo(int fd, struct slice *slice)
{
    long nwritten;
    nwritten = write(fd, slice->data, slice->len);
    if (nwritten == -1) {
        if ((*__errno_location ()) == 11) {
            nwritten = 0;
        } else if ((*__errno_location ()) == 32) {
            wheatLog(0, "Receive RST, peer closed", strerror((*__errno_location ())));
            return -1;
        } else {
            wheatLog(2,
                "Error writing to client: %s", strerror((*__errno_location ())));
            return -1;
        }
    }
    return (int)nwritten;
}

variable_types: nwritten long


sendReplyToClient

decompiled_code:
__int64 __fastcall sendReplyToClient(__int64 a1, unsigned int a2, __int64 a3)
{
unsigned __int64 v4; // rbx
int v5; // eax
unsigned __int64 v6; // r12
__int64 result; // rax
_BYTE v8[56]; // [rsp+0h] [rbp-38h] BYREF

v4 = 0LL;
v5 = wstrlen(*(_QWORD *)(a3 + 16));
v6 = v5;
if ( !v5 )
return deleteEvent(a1, a2, 2LL);
while ( 1 )
{
sliceTo(v8);
result = (int)writeBulkTo(*(_DWORD *)a3, (__int64)v8);
if ( (int)result <= 0 )
break;
v4 += (int)result;
if ( v6 <= v4 )
goto LABEL_7;
wstrlen(*(_QWORD *)(a3 + 16));
}
if ( (int)result == -1LL )
return freeMasterClient(a3);
LABEL_7:
if ( v6 == v4 )
return deleteEvent(a1, a2, 2LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int a2, __int64 a3)
{
unsigned __int64 v4; // rbx
int v5; // eax
unsigned __int64 v6; // r12
__int64 result; // rax
_BYTE v8[56]; // [rsp+0h] [rbp-38h] BYREF

v4 = 0LL;
v5 = func0(*(_QWORD *)(a3 + 16));
v6 = v5;
if ( !v5 )
return deleteEvent(a1, a2, 2LL);
while ( 1 )
{
sliceTo(v8);
result = (int)func3(*(_DWORD *)a3, (__int64)v8);
if ( (int)result <= 0 )
break;
v4 += (int)result;
if ( v6 <= v4 )
goto LABEL_7;
func0(*(_QWORD *)(a3 + 16));
}
if ( (int)result == -1LL )
return freeMasterClient(a3);
LABEL_7:
if ( v6 == v4 )
return deleteEvent(a1, a2, 2LL);
return result;
}

original_code:
static void sendReplyToClient(struct evcenter *center, int fd, void *data, int mask)
{
    struct masterClient *client = data;
    size_t bufpos = 0, totallen = wstrlen(client->response_buf);
    ssize_t nwritten = 0;
    while (bufpos < totallen) {
        struct slice slice;
        sliceTo(&slice, (uint8_t *)client->response_buf,
                wstrlen(client->response_buf));
        nwritten = writeBulkTo(client->fd, &slice);
        if (nwritten <= 0)
            break;
        bufpos += nwritten;
    }
    if (nwritten == -1) {
        freeMasterClient(client);
        return ;
    }
    if (bufpos == totallen) {
        deleteEvent(center, fd, 2);
    }
}

canonical_original_code:
static void sendReplyToClient(struct evcenter *center, int fd, void *data, int mask)
{
    struct masterClient *client = data;
    unsigned long bufpos = 0; unsigned long totallen = wstrlen(client->response_buf);
    long nwritten = 0;
    while (bufpos < totallen) {
        struct slice slice;
        sliceTo(&slice, (unsigned char *)client->response_buf,
                wstrlen(client->response_buf));
        nwritten = writeBulkTo(client->fd, &slice);
        if (nwritten <= 0)
            break;
        bufpos += nwritten;
    }
    if (nwritten == -1) {
        freeMasterClient(client);
        return ;
    }
    if (bufpos == totallen) {
        deleteEvent(center, fd, 2);
    }
}

variable_types: struct evcenter { int nevent; struct event { int mask; void *client_data; void (*read_proc)(struct evcenter *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); } *events; struct fired_event { int mask; int fd; } *fired_events; void *apidata; } *

variable_types:
data
void *

variable_types: mask int

variable_types: struct masterClient { int fd; char *request_buf; char *response_buf; int argc; char **argv; } *

variable_types:
bufpos
unsigned long

variable_types:
totallen
unsigned long

variable_types: struct slice { unsigned char *data; unsigned long len; }

user_defined_types: struct evcenter { int nevent; struct event *events; struct fired_event *fired_events; void *apidata; }


syncWriteBulkTo

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

if ( !*(_QWORD *)(a2 + 8) )
return 0LL;
v2 = 0;
while ( 1 )
{
result = writeBulkTo(a1, a2);
if ( (_DWORD)result == -1 )
break;
v2 += result;
if ( (unsigned __int64)(int)v2 >= *(_QWORD *)(a2 + 8) )
return v2;
}
return result;
}

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

if ( !*(_QWORD *)(a2 + 8) )
return 0LL;
v2 = 0;
while ( 1 )
{
result = func3(a1, a2);
if ( (_DWORD)result == -1 )
break;
v2 += result;
if ( (unsigned __int64)(int)v2 >= *(_QWORD *)(a2 + 8) )
return v2;
}
return result;
}

original_code:
int syncWriteBulkTo(int fd, struct slice *slice)
{
    int totallen, ret;
    totallen = 0;
    while (totallen < slice->len) {
        ret = writeBulkTo(fd, slice);
        if (ret == -1)
            return -1;
        totallen += ret;
    }
    return totallen;
}

canonical_original_code:
int syncWriteBulkTo(int fd, struct slice *slice)
{
    int totallen; int ret;
    totallen = 0;
    while (totallen < slice->len) {
        ret = writeBulkTo(fd, slice);
        if (ret == -1)
            return -1;
        totallen += ret;
    }
    return totallen;
}

variable_types: totallen int

variable_types: ret int


isClientPreparedWrite

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

if ( a1 <= 0 )
result = -1LL;
else
result = -(__int64)((unsigned int)createEvent(a2, (unsigned int)a1, 2LL, sendReplyToClient, a3) == -1);
return result;
}

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

if ( a1 <= 0 )
result = -1LL;
else
result = -(__int64)((unsigned int)createEvent(a2, (unsigned int)a1, 2LL, sendReplyToClient, a3) == -1);
return result;
}

original_code:
ssize_t isClientPreparedWrite(int fd, struct evcenter *center, void *c)
{
    if (fd <= 0 || createEvent(center, fd, 2, sendReplyToClient, c) == -1)
        return -1;
    return 0;
}

canonical_original_code:
long isClientPreparedWrite(int fd, struct evcenter *center, void *c)
{
    if (fd <= 0 || createEvent(center, fd, 2, sendReplyToClient, c) == -1)
        return -1;
    return 0;
}

variable_types:
c
void *

return_type: long

user_defined_types: struct event { int mask; void *client_data; void (*read_proc)(struct evcenter *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); }

user_defined_types: struct fired_event { int mask; int fd; }


replyMasterClient

decompiled_code:
__int64 __fastcall replyMasterClient(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

result = isClientPreparedWrite(*(_DWORD *)a1, Server[10507], a1);
if ( result != -1 )
{
result = wstrCatLen(*(_QWORD *)(a1 + 16), a2, a3);
*(_QWORD *)(a1 + 16) = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax

result = func6(*(_DWORD *)a1, Server[10507], a1);
if ( result != -1 )
{
result = wstrCatLen(*(_QWORD *)(a1 + 16), a2, a3);
*(_QWORD *)(a1 + 16) = result;
}
return result;
}

original_code:
void replyMasterClient(struct masterClient *c, const char *buf, size_t len)
{
    if (isClientPreparedWrite(c->fd, Server.master_center, c) == -1)
        return ;
    c->response_buf = wstrCatLen(c->response_buf, buf, len);
}

canonical_original_code:
void replyMasterClient(struct masterClient *c, char *buf, unsigned long len)
{
    if (isClientPreparedWrite(c->fd, Server.master_center, c) == -1)
        return ;
    c->response_buf = wstrCatLen(c->response_buf, buf, len);
}

variable_types:
buf
char *

variable_types:
len
unsigned long

user_defined_types: struct masterClient { int fd; char *request_buf; char *response_buf; int argc; char **argv; }


matchInt

decompiled_code:
_BOOL8 __fastcall matchInt(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

original_code:
int matchInt(void *ptr, void *key)
{
    return (*(int *)ptr == *(int *)key);
}

variable_types:
ptr
void *

variable_types:
key
void *


decompiled_code:
__int64 handleWorkerUsr1()
{
__int64 result; // rax

result = WorkerProcess;
*(_QWORD *)(WorkerProcess + 64LL) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

result = WorkerProcess;
*(_QWORD *)(WorkerProcess + 64LL) = 0LL;
return result;
}

original_code:
void handleWorkerUsr1(int sig)
{
    WorkerProcess->refresh_time = 0;
    return ;
}

variable_types: sig int


handleWorkerQuit

decompiled_code:
__int64 handleWorkerQuit()
{
__int64 result; // rax

result = WorkerProcess;
*(_DWORD *)(WorkerProcess + 24LL) = 0;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax

result = WorkerProcess;
*(_DWORD *)(WorkerProcess + 24LL) = 0;
return result;
}

original_code:
void handleWorkerQuit(int sig)
{
    WorkerProcess->alive = 0;
}


freeInt

decompiled_code:
// attributes: thunk
__int64 freeInt()
{
return wfree();
}

canonical_decompiled_code:
__int64 func3()
{
return wfree();
}

original_code:
void freeInt(void *ptr)
{
    wfree(ptr);
}

canonical_original_code:
void freeInt(void *ptr)
{
    wfree(ptr);
}


createIntList

decompiled_code:
_QWORD *createIntList()
{
_QWORD *result; // rax

result = (_QWORD *)createList();
result[2] = dupInt;
result[4] = freeInt;
result[3] = matchInt;
return result;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *result; // rax

result = (_QWORD *)createList();
result[2] = dupInt;
result[4] = freeInt;
result[3] = matchInt;
return result;
}

original_code:
static struct list *createIntList()
{
    struct list *l = createList();
    ((l)->dup = (dupInt));
    ((l)->free = (freeInt));
    ((l)->match = (matchInt));
    return l;
}

variable_types: struct list { struct listNode { struct listNode *next; struct listNode *prev; void *value; } *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; } *

user_defined_types: struct list { struct listNode *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; }


signalProc

decompiled_code:
__int64 __fastcall signalProc(int a1)
{
__int64 v1; // rdi
__int64 result; // rax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a1;
v1 = Server[10517];
if ( *(_QWORD *)(v1 + 40) <= 4uLL )
{
if ( v3[0] != 17 )
appendToListTail(v1, v3);
result = wakeUp();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
__int64 v1; // rdi
__int64 result; // rax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a1;
v1 = Server[10517];
if ( *(_QWORD *)(v1 + 40) <= 4uLL )
{
if ( v3[0] != 17 )
appendToListTail(v1, v3);
result = wakeUp();
}
return result;
}

original_code:
void signalProc(int signal)
{
    if (((Server.signal_queue)->len) < 5) {
        if (signal != 17)
            appendToListTail(Server.signal_queue, &signal);
        wakeUp();
    }
}

variable_types: signal int


handleWorkerAbort

decompiled_code:
__int64 handleWorkerAbort()
{
*(_DWORD *)(WorkerProcess + 24LL) = 0;
return halt(1LL);
}

canonical_decompiled_code:
__int64 func6()
{
*(_DWORD *)(WorkerProcess + 24LL) = 0;
return halt(1LL);
}

original_code:
void handleWorkerAbort(int sig)
{
    WorkerProcess->alive = 0;
    halt(1);
}


handleSegv

decompiled_code:
int handleSegv()
{
wheat_stacktrace(1LL);
wheatLogRaw(2LL, "Signal SEGV received, core dump");
return raise(11);
}

canonical_decompiled_code:
int func7()
{
wheat_stacktrace(1LL);
wheatLogRaw(2LL, "Signal SEGV received, core dump");
return raise(11);
}

original_code:
void handleSegv()
{
    wheat_stacktrace(1);
    wheatLogRaw(2, "Signal SEGV received, core dump");
    raise(11);
}


initWorkerSignals

decompiled_code:
int initWorkerSignals()
{
unsigned int v0; // er12
unsigned int *i; // rbx
int result; // eax
int *v3; // rax
char *v4; // rax
struct sigaction v5; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v6; // [rsp+A0h] [rbp-B8h] BYREF

v0 = 1;
for ( i = (unsigned int *)&unk_6A4; ; ++i )
{
v5.sa_flags = 0;
switch ( v0 )
{
case 3u:
v5.sa_handler = (__sighandler_t)handleWorkerQuit;
break;
case 0xFu:
case 2u:
v5.sa_handler = (__sighandler_t)handleWorkerAbort;
break;
case 0xAu:
v5.sa_handler = (__sighandler_t)handleWorkerUsr1;
break;
case 0xBu:
v5.sa_flags = 0x80000000;
v5.sa_handler = (__sighandler_t)handleSegv;
break;
default:
v5.sa_handler = 0LL;
break;
}
sigemptyset(&v5.sa_mask);
result = sigaction(v0, &v5, &v6);
if ( result )
break;
if ( i == (unsigned int *)1740 )
return result;
LABEL_9:
v0 = *i;
}
v3 = _errno_location();
v4 = strerror(*v3);
result = wheatLog(2LL, "sigaction error on %d : %s", v0, v4);
if ( i != (unsigned int *)1740 )
goto LABEL_9;
return result;
}

canonical_decompiled_code:
int func8()
{
unsigned int v0; // er12
unsigned int *i; // rbx
int result; // eax
int *v3; // rax
char *v4; // rax
struct sigaction v5; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v6; // [rsp+A0h] [rbp-B8h] BYREF

v0 = 1;
for ( i = (unsigned int *)&unk_6A4; ; ++i )
{
v5.sa_flags = 0;
switch ( v0 )
{
case 3u:
v5.sa_handler = (__sighandler_t)handleWorkerQuit;
break;
case 0xFu:
case 2u:
v5.sa_handler = (__sighandler_t)handleWorkerAbort;
break;
case 0xAu:
v5.sa_handler = (__sighandler_t)handleWorkerUsr1;
break;
case 0xBu:
v5.sa_flags = 0x80000000;
v5.sa_handler = (__sighandler_t)handleSegv;
break;
default:
v5.sa_handler = 0LL;
break;
}
sigemptyset(&v5.sa_mask);
result = sigaction(v0, &v5, &v6);
if ( result )
break;
if ( i == (unsigned int *)1740 )
return result;
LABEL_9:
v0 = *i;
}
v3 = _errno_location();
v4 = strerror(*v3);
result = wheatLog(2LL, "sigaction error on %d : %s", v0, v4);
if ( i != (unsigned int *)1740 )
goto LABEL_9;
return result;
}

original_code:
void initWorkerSignals()
{
    struct sigaction act, oact;
    int i;
    for (i = 0; i < 11; i++) {
        act.sa_flags = 0;
        if (signals[i] == 3)
            act.__sigaction_handler.sa_handler = handleWorkerQuit;
        else if (signals[i] == 15 || signals[i] == 2)
            act.__sigaction_handler.sa_handler = handleWorkerAbort;
        else if (signals[i] == 10)
            act.__sigaction_handler.sa_handler = handleWorkerUsr1;
        else if (signals[i] == 11) {
            act.sa_flags = (int)0x80000000;
            act.__sigaction_handler.sa_handler = handleSegv;
        }
        else
            act.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
        sigemptyset(&act.sa_mask);
        if (sigaction(signals[i], &act, &oact) != 0) {
            wheatLog(2, "sigaction error on %d : %s", signals[i], strerror((*__errno_location ())));
        }
    }
}

canonical_original_code:
void initWorkerSignals()
{
    struct sigaction act; struct sigaction oact;
    int i;
    for (i = 0; i < 11; i++) {
        act.sa_flags = 0;
        if (signals[i] == 3)
            act.__sigaction_handler.sa_handler = handleWorkerQuit;
        else if (signals[i] == 15 || signals[i] == 2)
            act.__sigaction_handler.sa_handler = handleWorkerAbort;
        else if (signals[i] == 10)
            act.__sigaction_handler.sa_handler = handleWorkerUsr1;
        else if (signals[i] == 11) {
            act.sa_flags = (int)0x80000000;
            act.__sigaction_handler.sa_handler = handleSegv;
        }
        else
            act.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
        sigemptyset(&act.sa_mask);
        if (sigaction(signals[i], &act, &oact) != 0) {
            wheatLog(2, "sigaction error on %d : %s", signals[i], strerror((*__errno_location ())));
        }
    }
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: i int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


initMasterSignals

decompiled_code:
int initMasterSignals()
{
unsigned int v0; // er12
unsigned int *v1; // rbx
int *v2; // rax
char *v3; // rax
int result; // eax
int *v5; // rax
char *v6; // rax
struct sigaction v7; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v8; // [rsp+A0h] [rbp-B8h] BYREF

v0 = 1;
v1 = (unsigned int *)&unk_6A4;
v7.sa_handler = (__sighandler_t)signalProc;
Server[10517] = createIntList();
sigemptyset(&v7.sa_mask);
v7.sa_flags = 0;
while ( !sigaction(v0, &v7, &v8) )
{
if ( v1 == (unsigned int *)&unk_6C8 )
goto LABEL_6;
LABEL_3:
v0 = *v1++;
}
v2 = _errno_location();
v3 = strerror(*v2);
wheatLog(4LL, "sigaction error on %d : %s", v0, v3);
if ( v1 != (unsigned int *)&unk_6C8 )
goto LABEL_3;
LABEL_6:
v7.sa_handler = (__sighandler_t)handleSegv;
sigemptyset(&v7.sa_mask);
v7.sa_flags = 0x80000000;
result = sigaction(11, &v7, &v8);
if ( result )
{
v5 = _errno_location();
v6 = strerror(*v5);
result = wheatLog(4LL, "sigaction error on %d : %s", 11LL, v6);
}
return result;
}

canonical_decompiled_code:
int func9()
{
unsigned int v0; // er12
unsigned int *v1; // rbx
int *v2; // rax
char *v3; // rax
int result; // eax
int *v5; // rax
char *v6; // rax
struct sigaction v7; // [rsp+0h] [rbp-158h] BYREF
struct sigaction v8; // [rsp+A0h] [rbp-B8h] BYREF

v0 = 1;
v1 = (unsigned int *)&unk_6A4;
v7.sa_handler = (__sighandler_t)signalProc;
Server[10517] = func4();
sigemptyset(&v7.sa_mask);
v7.sa_flags = 0;
while ( !sigaction(v0, &v7, &v8) )
{
if ( v1 == (unsigned int *)&unk_6C8 )
goto LABEL_6;
LABEL_3:
v0 = *v1++;
}
v2 = _errno_location();
v3 = strerror(*v2);
wheatLog(4LL, "sigaction error on %d : %s", v0, v3);
if ( v1 != (unsigned int *)&unk_6C8 )
goto LABEL_3;
LABEL_6:
v7.sa_handler = (__sighandler_t)handleSegv;
sigemptyset(&v7.sa_mask);
v7.sa_flags = 0x80000000;
result = sigaction(11, &v7, &v8);
if ( result )
{
v5 = _errno_location();
v6 = strerror(*v5);
result = wheatLog(4LL, "sigaction error on %d : %s", 11LL, v6);
}
return result;
}

original_code:
void initMasterSignals()
{
    Server.signal_queue = createIntList();
    struct sigaction act, oact;
    act.__sigaction_handler.sa_handler = signalProc;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    int i;
    for (i = 0; i < 10; i++) {
        if (sigaction(signals[i], &act, &oact) != 0) {
            wheatLog(4, "sigaction error on %d : %s", signals[i], strerror((*__errno_location ())));
        }
    }
    act.__sigaction_handler.sa_handler = handleSegv;
    sigemptyset(&act.sa_mask);
    act.sa_flags = (int)0x80000000;
    if (sigaction(signals[10], &act, &oact) != 0) {
            wheatLog(4, "sigaction error on %d : %s", signals[i], strerror((*__errno_location ())));
    }
}

canonical_original_code:
void initMasterSignals()
{
    Server.signal_queue = createIntList();
    struct sigaction act; struct sigaction oact;
    act.__sigaction_handler.sa_handler = signalProc;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    int i;
    for (i = 0; i < 10; i++) {
        if (sigaction(signals[i], &act, &oact) != 0) {
            wheatLog(4, "sigaction error on %d : %s", signals[i], strerror((*__errno_location ())));
        }
    }
    act.__sigaction_handler.sa_handler = handleSegv;
    sigemptyset(&act.sa_mask);
    act.sa_flags = (int)0x80000000;
    if (sigaction(signals[10], &act, &oact) != 0) {
            wheatLog(4, "sigaction error on %d : %s", signals[i], strerror((*__errno_location ())));
    }
}

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: union sigval { int sival_int; void *sival_ptr; }


handleChld

decompiled_code:
__int64 handleChld()
{
return wakeUp();
}

canonical_decompiled_code:
__int64 func10()
{
return wakeUp();
}

original_code:
void handleChld()
{
    wakeUp();
}

canonical_original_code:
void handleChld()
{
    wakeUp();
}


handleHup

decompiled_code:
__int64 handleHup()
{
wheatLog(2LL, "Hang up: %s", (const char *)(unsigned int)((_DWORD)Server + 1112));
return reload();
}

canonical_decompiled_code:
__int64 func11()
{
wheatLog(2LL, "Hang up: %s", (const char *)(unsigned int)((_DWORD)Server + 1112));
return reload();
}

original_code:
void handleHup()
{
    wheatLog(2, "Hang up: %s", Server.master_name);
    reload();
}


handleQuit

decompiled_code:
__int64 handleQuit()
{
wheatLog(4LL, "Signal sigquit");
return halt(1LL);
}

canonical_decompiled_code:
__int64 func12()
{
wheatLog(4LL, "Signal sigquit");
return halt(1LL);
}

original_code:
void handleQuit()
{
    wheatLog(4, "Signal sigquit");
    halt(1);
}


handleInt

decompiled_code:
__int64 handleInt()
{
wheatLog(4LL, "Signal int");
return halt(0LL);
}

canonical_decompiled_code:
__int64 func13()
{
wheatLog(4LL, "Signal int");
return halt(0LL);
}

original_code:
void handleInt()
{
    wheatLog(4, "Signal int");
    halt(0);
}


handleTerm

decompiled_code:
__int64 handleTerm()
{
wheatLog(4LL, "Signal term");
return halt(0LL);
}

canonical_decompiled_code:
__int64 func14()
{
wheatLog(4LL, "Signal term");
return halt(0LL);
}

original_code:
void handleTerm()
{
    wheatLog(4, "Signal term");
    halt(0);
}


handleTtin

decompiled_code:
__int64 handleTtin()
{
++LODWORD(Server[2]);
return adjustWorkerNumber();
}

canonical_decompiled_code:
__int64 func15()
{
++LODWORD(Server[2]);
return adjustWorkerNumber();
}

original_code:
void handleTtin()
{
    Server.worker_number++;
    adjustWorkerNumber();
}


handleTtou

decompiled_code:
__int64 handleTtou()
{
--LODWORD(Server[2]);
return adjustWorkerNumber();
}

canonical_decompiled_code:
__int64 func16()
{
--LODWORD(Server[2]);
return adjustWorkerNumber();
}

original_code:
void handleTtou()
{
    Server.worker_number--;
    adjustWorkerNumber();
}


decompiled_code:
__int64 handleUsr1()
{
wheatLog(2LL, "Signal usr1: %s", (const char *)(unsigned int)((_DWORD)Server + 1112));
killAllWorkers(10LL);
return logStat();
}

canonical_decompiled_code:
__int64 func17()
{
wheatLog(2LL, "Signal usr1: %s", (const char *)(unsigned int)((_DWORD)Server + 1112));
killAllWorkers(10LL);
return logStat();
}

original_code:
void handleUsr1()
{
    wheatLog(2, "Signal usr1: %s", Server.master_name);
    killAllWorkers(10);
    logStat();
}


decompiled_code:
__int64 handleUsr2()
{
wheatLog(2LL, "Signal usr2: %s reexec", (const char *)(unsigned int)((_DWORD)Server + 1112));
return reexec();
}

canonical_decompiled_code:
__int64 func18()
{
wheatLog(2LL, "Signal usr2: %s reexec", (const char *)(unsigned int)((_DWORD)Server + 1112));
return reexec();
}

original_code:
void handleUsr2()
{
    wheatLog(2, "Signal usr2: %s reexec", Server.master_name);
    reexec();
}


handleWinch

decompiled_code:
__int64 handleWinch()
{
__pid_t v0; // ebx

if ( getppid() != 1 )
{
v0 = getpgrp();
if ( v0 == getpid() )
return wheatLog(2LL, "Signal winch ignored, not daemonize");
}
wheatLog(2LL, "Signal winch: graceful stop of workers");
LODWORD(Server[2]) = 0;
return killAllWorkers(3LL);
}

canonical_decompiled_code:
__int64 func19()
{
__pid_t v0; // ebx

if ( getppid() != 1 )
{
v0 = getpgrp();
if ( v0 == getpid() )
return wheatLog(2LL, "Signal winch ignored, not daemonize");
}
wheatLog(2LL, "Signal winch: graceful stop of workers");
LODWORD(Server[2]) = 0;
return killAllWorkers(3LL);
}

original_code:
void handleWinch()
{
    if (getppid() == 1 || getpgrp() != getpid()) {
        wheatLog(2, "Signal winch: graceful stop of workers");
        Server.worker_number = 0;
        killAllWorkers(3);
    } else {
        wheatLog(2, "Signal winch ignored, not daemonize");
    }
}


signalGenericHandle

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

switch ( a1 )
{
case 17:
return handleChld();
case 1:
return handleHup();
case 3:
return handleQuit();
case 2:
return handleInt();
case 15:
return handleTerm();
case 21:
return handleTtin();
case 22:
return handleTtou();
case 10:
return handleUsr1();
case 12:
return handleUsr2();
case 28:
result = handleWinch();
break;
}
return result;
}

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

switch ( a1 )
{
case 17:
return func10();
case 1:
return func11();
case 3:
return func12();
case 2:
return func13();
case 15:
return func14();
case 21:
return func15();
case 22:
return func16();
case 10:
return func17();
case 12:
return func18();
case 28:
result = func19();
break;
}
return result;
}

original_code:
void signalGenericHandle(int sig)
{
    if (sig == 17)
        handleChld();
    else if (sig == 1)
        handleHup();
    else if (sig == 3)
        handleQuit();
    else if (sig == 2)
        handleInt();
    else if (sig == 15)
        handleTerm();
    else if (sig == 21)
        handleTtin();
    else if (sig == 22)
        handleTtou();
    else if (sig == 10)
        handleUsr1();
    else if (sig == 12)
        handleUsr2();
    else if (sig == 28)
        handleWinch();
}


wstrlen

decompiled_code:
__int64 __fastcall wstrlen(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

original_code:
static inline int wstrlen(const wstr s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

variable_types:
s
char *

variable_types: struct wstrhd { int len; int free; char buf[]; } *


wstrupdatelen

decompiled_code:
__int64 __fastcall wstrupdatelen(__int64 a1, int a2)
{
int v2; // eax
__int64 result; // rax

v2 = *(_DWORD *)(a1 - 8);
*(_DWORD *)(a1 - 8) = a2;
result = (unsigned int)(v2 - a2);
*(_DWORD *)(a1 - 4) += result;
*(_BYTE *)(a1 + a2) = 0;
return result;
}

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

v2 = *(_DWORD *)(a1 - 8);
*(_DWORD *)(a1 - 8) = a2;
result = (unsigned int)(v2 - a2);
*(_DWORD *)(a1 - 4) += result;
*(_BYTE *)(a1 + a2) = 0;
return result;
}

original_code:
static inline void wstrupdatelen(wstr s, int len)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    hd->free += (hd->len - len);
    hd->len = len;
    hd->buf[len] = '\0';
}

canonical_original_code:
static inline void wstrupdatelen(char *s, int len)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    hd->free += (hd->len - len);
    hd->len = len;
    hd->buf[len] = '\0';
}

variable_types: len int

user_defined_types: struct wstrhd { int len; int free; char buf[]; }


wstrEmpty

decompiled_code:
_DWORD *wstrEmpty()
{
return wstrNewLen(0LL, 0);
}

canonical_decompiled_code:
_DWORD *func2()
{
return wstrNewLen(0LL, 0);
}

original_code:
wstr wstrEmpty()
{
    return wstrNewLen(((void *)0), 0);
}

canonical_original_code:
char *wstrEmpty()
{
    return wstrNewLen(((void *)0), 0);
}


wstrClear

decompiled_code:
__int64 __fastcall wstrClear(__int64 a1)
{
return wstrupdatelen(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return func1(a1, 0);
}

original_code:
void wstrClear(wstr s)
{
    wstrupdatelen(s, 0);
}

canonical_original_code:
void wstrClear(char *s)
{
    wstrupdatelen(s, 0);
}


wstrMakeRoom

decompiled_code:
__int64 __fastcall wstrMakeRoom(__int64 a1, unsigned __int64 a2)
{
int v3; // ebp
int v4; // ebx
__int64 v5; // rax
__int64 v6; // r8

if ( *(int *)(a1 - 4) >= a2 )
return a1;
v3 = wstrlen(a1);
v4 = 2 * (v3 + a2);
v5 = wrealloc(a1 - 8, v4 + 9LL);
v6 = v5;
if ( v5 )
{
v6 = v5 + 8;
*(_DWORD *)(v5 + 4) = v4 - v3;
}
return v6;
}

original_code:
wstr wstrMakeRoom(wstr s, size_t add_size)
{
    struct wstrhd *sh = (void *)(s - sizeof(struct wstrhd));
    if (sh->free >= add_size)
        return s;
    int old_len = wstrlen(s);
    int new_len = (old_len + (int)add_size) * 2;
    struct wstrhd *new_sh = wrealloc(sh, sizeof(struct wstrhd)+new_len+1);
    if (new_sh == ((void *)0)) {
        return ((void *)0);
    }
    new_sh->free = new_len - old_len;
    return new_sh->buf;
}

canonical_original_code:
char *wstrMakeRoom(char *s, unsigned long add_size)
{
    struct wstrhd *sh = (void *)(s - sizeof(struct wstrhd));
    if (sh->free >= add_size)
        return s;
    int old_len = wstrlen(s);
    int new_len = (old_len + (int)add_size) * 2;
    struct wstrhd *new_sh = wrealloc(sh, sizeof(struct wstrhd)+new_len+1);
    if (new_sh == ((void *)0)) {
        return ((void *)0);
    }
    new_sh->free = new_len - old_len;
    return new_sh->buf;
}


wstrFree

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

if ( a1 )
result = wfree(a1 - 8);
return result;
}

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

if ( a1 )
result = wfree(a1 - 8);
return result;
}

original_code:
void wstrFree(wstr s)
{
    if (s == ((void *)0)) {
        return ;
    }
    wfree(s - sizeof(struct wstrhd));
}

canonical_original_code:
void wstrFree(char *s)
{
    if (s == ((void *)0)) {
        return ;
    }
    wfree(s - sizeof(struct wstrhd));
}


wstrFreeSplit

original_code:
void wstrFreeSplit(wstr *slots, int count)
{
    int i;
    for (i = 0; i < count; i++) {
        wstrFree(slots[i]);
    }
    wfree(slots);
}

canonical_original_code:
void wstrFreeSplit(char **slots, int count)
{
    int i;
    for (i = 0; i < count; i++) {
        wstrFree(slots[i]);
    }
    wfree(slots);
}

variable_types:
slots
char **

variable_types: count int


wstrCmpNocaseChars

decompiled_code:
int __fastcall wstrCmpNocaseChars(const char *a1, const char *a2, int a3)
{
int v4; // ebx
size_t v5; // rdx
int result; // eax
int v7; // ebx

v4 = wstrlen((__int64)a1);
result = strncasecmp(a1, a2, v5);
v7 = v4 - a3;
if ( !result )
result = v7;
return result;
}

canonical_decompiled_code:
int __fastcall func7(const char *a1, const char *a2, int a3)
{
int v4; // ebx
size_t v5; // rdx
int result; // eax
int v7; // ebx

v4 = func0((__int64)a1);
result = strncasecmp(a1, a2, v5);
v7 = v4 - a3;
if ( !result )
result = v7;
return result;
}

original_code:
int wstrCmpNocaseChars(const wstr s1, const char *s2, size_t l2)
{
    int l1, cmp;
    l1 = wstrlen(s1);
    cmp = strncasecmp(s1, s2, l2);
    if (cmp == 0)
        return (int)(l1 - l2);
    return cmp;
}

canonical_original_code:
int wstrCmpNocaseChars(char *s1, char *s2, unsigned long l2)
{
    int l1; int cmp;
    l1 = wstrlen(s1);
    cmp = strncasecmp(s1, s2, l2);
    if (cmp == 0)
        return (int)(l1 - l2);
    return cmp;
}

variable_types: cmp int


wstrCmpChars

decompiled_code:
int __fastcall wstrCmpChars(const void *a1, const void *a2, unsigned __int64 a3)
{
int v4; // edx
int v5; // ebx
int v6; // ebx
int result; // eax

v4 = wstrlen((__int64)a1);
v5 = v4;
if ( v4 > a3 )
v4 = a3;
v6 = v5 - a3;
result = memcmp(a1, a2, v4);
if ( !result )
result = v6;
return result;
}

canonical_decompiled_code:
int __fastcall func8(const void *a1, const void *a2, unsigned __int64 a3)
{
int v4; // edx
int v5; // ebx
int v6; // ebx
int result; // eax

v4 = func0((__int64)a1);
v5 = v4;
if ( v4 > a3 )
v4 = a3;
v6 = v5 - a3;
result = memcmp(a1, a2, v4);
if ( !result )
result = v6;
return result;
}

original_code:
int wstrCmpChars(const wstr s1, const char *s2, size_t l2)
{
    int minlen, cmp;
    size_t l1 = wstrlen(s1);
    minlen = l1 < l2 ? l1: l2;
    cmp = memcmp(s1, s2, minlen);
    if (cmp == 0)
        return (int)(l1 - l2);
    return cmp;
}

canonical_original_code:
int wstrCmpChars(char *s1, char *s2, unsigned long l2)
{
    int minlen; int cmp;
    unsigned long l1 = wstrlen(s1);
    minlen = l1 < l2 ? l1: l2;
    cmp = memcmp(s1, s2, minlen);
    if (cmp == 0)
        return (int)(l1 - l2);
    return cmp;
}

variable_types: minlen int


wstrCmp

decompiled_code:
int __fastcall wstrCmp(__int64 a1, const void *a2)
{
int v2; // eax
const void *v3; // r8

v2 = wstrlen((__int64)a2);
return wstrCmpChars(v3, a2, v2);
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, const void *a2)
{
int v2; // eax
const void *v3; // r8

v2 = func0((__int64)a2);
return func8(v3, a2, v2);
}

original_code:
int wstrCmp(const wstr s1, const wstr s2)
{
    return wstrCmpChars(s1, s2, wstrlen(s2));
}

canonical_original_code:
int wstrCmp(char *s1, char *s2)
{
    return wstrCmpChars(s1, s2, wstrlen(s2));
}


wstrCatLen

decompiled_code:
__int64 __fastcall wstrCatLen(__int64 a1, const void *a2)
{
int v2; // eax
unsigned __int64 v3; // rdx
size_t v4; // rbx
int v5; // ebp
__int64 v6; // rax
__int64 v7; // r12

if ( !a1 || !a2 )
return 0LL;
v2 = wstrlen(a1);
v4 = v3;
v5 = v2;
v6 = wstrMakeRoom(a1, v3);
v7 = v6;
if ( v6 )
{
memcpy((void *)(v6 + v5), a2, v4);
wstrupdatelen(v7, v5 + v4);
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, const void *a2)
{
int v2; // eax
unsigned __int64 v3; // rdx
size_t v4; // rbx
int v5; // ebp
__int64 v6; // rax
__int64 v7; // r12

if ( !a1 || !a2 )
return 0LL;
v2 = func0(a1);
v4 = v3;
v5 = v2;
v6 = func4(a1, v3);
v7 = v6;
if ( v6 )
{
memcpy((void *)(v6 + v5), a2, v4);
func1(v7, v5 + v4);
}
return v7;
}

original_code:
wstr wstrCatLen(wstr s, const char *t, size_t tlen)
{
    if (s == ((void *)0) || t == ((void *)0))
        return ((void *)0);
    int slen;
    slen = wstrlen(s);
    wstr new_s;
    new_s = wstrMakeRoom(s, tlen);
    if (new_s == ((void *)0))
        return ((void *)0);
    memcpy(new_s+slen, t, tlen);
    wstrupdatelen(new_s, (int)tlen+slen);
    return new_s;
}

canonical_original_code:
char *wstrCatLen(char *s, char *t, unsigned long tlen)
{
    if (s == ((void *)0) || t == ((void *)0))
        return ((void *)0);
    int slen;
    slen = wstrlen(s);
    char *new_s;
    new_s = wstrMakeRoom(s, tlen);
    if (new_s == ((void *)0))
        return ((void *)0);
    memcpy(new_s+slen, t, tlen);
    wstrupdatelen(new_s, (int)tlen+slen);
    return new_s;
}

variable_types:
t
char *

variable_types:
tlen
unsigned long

variable_types: slen int


wstrCat

decompiled_code:
__int64 __fastcall wstrCat(__int64 a1, const char *a2)
{
strlen(a2);
return wstrCatLen(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, const char *a2)
{
strlen(a2);
return func12(a1, a2);
}

original_code:
wstr wstrCat(wstr s, const char *t)
{
    return wstrCatLen(s, t, strlen(t));
}

canonical_original_code:
char *wstrCat(char *s, char *t)
{
    return wstrCatLen(s, t, strlen(t));
}