decompiled_code:
__int64 rf_host_auth()
{
unsigned int *v0; // rsi
unsigned __int32 v1; // eax
__int64 result; // rax
__int64 v3; // rdx

v0 = *(unsigned int **)(cur_slot + 32LL);
if ( *v0 == 0x1000000 )
{
log_write(3LL, "No authentication required at host side");
result = send_client_initmsg();
}
else
{
v1 = _byteswap_ulong(*v0);
if ( v1 == 2 )
{
log_write(5LL, "VNC authentication requested by host");
result = aio_setread(rf_host_crypt, 0LL, 16LL);
}
else if ( v1 )
{
log_write(1LL, "Unknown authentication scheme requested by host");
result = aio_close(0LL);
}
else
{
v3 = _byteswap_ulong(v0[1]);
*(_DWORD *)(cur_slot + 368LL) = v3;
result = aio_setread(rf_host_conn_failed, 0LL, v3);
}
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int *v0; // rsi
unsigned __int32 v1; // eax
__int64 result; // rax
__int64 v3; // rdx

v0 = *(unsigned int **)(cur_slot + 32LL);
if ( *v0 == 0x1000000 )
{
log_write(3LL, "No authentication required at host side");
result = func6();
}
else
{
v1 = _byteswap_ulong(*v0);
if ( v1 == 2 )
{
log_write(5LL, "VNC authentication requested by host");
result = aio_setread(rf_host_crypt, 0LL, 16LL);
}
else if ( v1 )
{
log_write(1LL, "Unknown authentication scheme requested by host");
result = aio_close(0LL);
}
else
{
v3 = _byteswap_ulong(v0[1]);
*(_DWORD *)(cur_slot + 368LL) = v3;
result = aio_setread(rf_host_conn_failed, 0LL, v3);
}
}
return result;
}

original_code:
static void rf_host_auth(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  u_int32_t value32;
  value32 = ((u_int32_t)((u_int8_t *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[3]);;
  switch (value32) {
  case 0:
    hs->temp_len = ((u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[0] << 24 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[1] << 16 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[2] << 8 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[4])[3]);;
    aio_setread(rf_host_conn_failed, ((void *)0), hs->temp_len);
    break;
  case 1:
    log_write(3, "No authentication required at host side");
    send_client_initmsg();
    break;
  case 2:
    log_write(5, "VNC authentication requested by host");
    aio_setread(rf_host_crypt, ((void *)0), 16);
    break;
  default:
    log_write(1, "Unknown authentication scheme requested by host");
    aio_close(0);
    return;
  }
}

canonical_original_code:
static void rf_host_auth()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  unsigned int value32;
  value32 = ((u_int32_t)((unsigned int *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[3]);;
  switch (value32) {
  case 0:
    hs->temp_len = ((u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[0] << 24 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[1] << 16 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[2] << 8 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[4])[3]);;
    aio_setread(rf_host_conn_failed, ((void *)0), hs->temp_len);
    break;
  case 1:
    log_write(3, "No authentication required at host side");
    send_client_initmsg();
    break;
  case 2:
    log_write(5, "VNC authentication requested by host");
    aio_setread(rf_host_crypt, ((void *)0), 16);
    break;
  default:
    log_write(1, "Unknown authentication scheme requested by host");
    aio_close(0);
    return;
  }
}


repo: inovex/vnc-reflector

decompiled_code:
__int64 rf_host_auth_done()
{
unsigned int v0; // edx
unsigned __int32 v1; // eax

v0 = **(_DWORD **)(cur_slot + 32LL);
if ( v0 == 0x1000000 )
{
log_write(1LL, "Authentication failed");
return aio_close(0LL);
}
v1 = _byteswap_ulong(v0);
if ( v1 == 2 )
{
log_write(1LL, "Authentication failed, too many tries");
return aio_close(0LL);
}
if ( v1 )
{
log_write(1LL, "Unknown authentication result");
return aio_close(0LL);
}
log_write(3LL, "Authentication successful");
return send_client_initmsg();
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // edx
unsigned __int32 v1; // eax

v0 = **(_DWORD **)(cur_slot + 32LL);
if ( v0 == 0x1000000 )
{
log_write(1LL, "Authentication failed");
return aio_close(0LL);
}
v1 = _byteswap_ulong(v0);
if ( v1 == 2 )
{
log_write(1LL, "Authentication failed, too many tries");
return aio_close(0LL);
}
if ( v1 )
{
log_write(1LL, "Unknown authentication result");
return aio_close(0LL);
}
log_write(3LL, "Authentication successful");
return func6();
}

original_code:
static void rf_host_auth_done(void)
{
  u_int32_t value32;
  value32 = ((u_int32_t)((u_int8_t *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((u_int8_t *)cur_slot->readbuf)[3]);;
  switch(value32) {
  case 0:
    log_write(3, "Authentication successful");
    send_client_initmsg();
    break;
  case 1:
    log_write(1, "Authentication failed");
    aio_close(0);
    break;
  case 2:
    log_write(1, "Authentication failed, too many tries");
    aio_close(0);
    break;
  default:
    log_write(1, "Unknown authentication result");
    aio_close(0);
  }
}

canonical_original_code:
static void rf_host_auth_done()
{
  unsigned int value32;
  value32 = ((u_int32_t)((unsigned int *)cur_slot->readbuf)[0] << 24 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[1] << 16 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[2] << 8 | (u_int32_t)((unsigned int *)cur_slot->readbuf)[3]);;
  switch(value32) {
  case 0:
    log_write(3, "Authentication successful");
    send_client_initmsg();
    break;
  case 1:
    log_write(1, "Authentication failed");
    aio_close(0);
    break;
  case 2:
    log_write(1, "Authentication failed, too many tries");
    aio_close(0);
    break;
  default:
    log_write(1, "Unknown authentication result");
    aio_close(0);
  }
}

return_type: void


decompiled_code:
__int64 rf_host_crypt()
{
__int64 v1; // [rsp+0h] [rbp-18h] BYREF

log_write(6LL, "Received random challenge");
rfb_crypt(&v1, *(_QWORD *)(cur_slot + 32LL), &s_host_password);
log_write(6LL, "Sending DES-encrypted response");
aio_write(0LL);
return aio_setread(rf_host_auth_done, 0LL, 4LL);
}

canonical_decompiled_code:
__int64 func9()
{
__int64 v1; // [rsp+0h] [rbp-18h] BYREF

log_write(6LL, "Received random challenge");
rfb_crypt(&v1, *(_QWORD *)(cur_slot + 32LL), &s_host_password);
log_write(6LL, "Sending DES-encrypted response");
aio_write(0LL);
return aio_setread(rf_host_auth_done, 0LL, 4LL);
}

original_code:
static void rf_host_crypt(void)
{
  unsigned char response[16];
  log_write(6, "Received random challenge");
  rfb_crypt(response, cur_slot->readbuf, s_host_password);
  log_write(6, "Sending DES-encrypted response");
  aio_write(((void *)0), response, 16);
  aio_setread(rf_host_auth_done, ((void *)0), 4);
}

canonical_original_code:
static void rf_host_crypt()
{
  unsigned char response[16];
  log_write(6, "Received random challenge");
  rfb_crypt(response, cur_slot->readbuf, s_host_password);
  log_write(6, "Sending DES-encrypted response");
  aio_write(((void *)0), response, 16);
  aio_setread(rf_host_auth_done, ((void *)0), 4);
}

variable_types:
response
unsigned char [16]


decompiled_code:
__int64 rf_host_set_formats()
{
__int64 v0; // rbp
__int64 v1; // rdx
__int64 v2; // rcx
size_t v3; // r13
void *v4; // rax
void *v5; // rdi
int v6; // er12
void *v7; // rbx
__int64 v8; // rax
__int64 v9; // rdi
__m256i *v10; // rsi
int *v12; // rax
char *v13; // rax
char v14[28]; // [rsp+4h] [rbp-64h] BYREF
__m256i v15; // [rsp+20h] [rbp-48h] BYREF

v0 = cur_slot;
*(_OWORD *)&v15.m256i_u64[2] = 0LL;
v1 = *(unsigned int *)(cur_slot + 368LL);
v2 = *(_QWORD *)(cur_slot + 32LL);
*(_OWORD *)v15.m256i_i8 = 2uLL;
log_write(4LL, "Remote desktop name: %.*s", v1, v2);
v3 = *(unsigned int *)(v0 + 368);
v4 = malloc(v3 + 1);
if ( v4 )
{
v5 = (void *)*((_QWORD *)&g_screen_info + 3);
v6 = v3;
v7 = v4;
if ( v5 )
{
free(v5);
v3 = *(unsigned int *)(v0 + 368);
v6 = *(_DWORD *)(v0 + 368);
}
v8 = cur_slot;
*((_QWORD *)&g_screen_info + 3) = v7;
*((_DWORD *)&g_screen_info + 5) = v6;
memcpy(v7, *(const void **)(v8 + 32), v3);
*((_BYTE *)v7 + *(unsigned int *)(v0 + 368)) = 0;
}
log_write(5LL, "Setting up pixel format");
((void (__fastcall *)(char *, _QWORD))buf_put_pixfmt)(v14, (unsigned int)((_DWORD)&g_screen_info + 4));
log_write(6LL, "Sending SetPixelFormat message");
aio_write(0LL);
if ( s_request_tight )
{
log_write(5LL, "Requesting Tight encoding");
*(__int64 *)((char *)&v15.m256i_i64[1] + 4) = 0x1000000LL;
*(__int64 *)((char *)v15.m256i_i64 + 4) = 0x500000007000000LL;
*(__int64 *)((char *)&v15.m256i_i64[2] + 4) = 0x21FFFFFF20FFFFFFLL;
if ( (unsigned int)s_tight_level <= 9 )
{
log_write(5LL, "Requesting compression level %d", (unsigned int)s_tight_level);
v15.m256i_i8[31] = s_tight_level;
v15.m256i_i8[30] = (unsigned __int16)(s_tight_level - 256) >> 8;
v15.m256i_i8[28] = (unsigned int)(s_tight_level - 256) >> 24;
v15.m256i_i8[29] = (unsigned int)(s_tight_level - 256) >> 16;
v15.m256i_i16[1] = 1792;
}
else
{
v15.m256i_i16[1] = 1536;
}
}
else
{
log_write(5LL, "Requesting Hextile encoding");
*(__int64 *)((char *)v15.m256i_i64 + 2) = 0x50000000400LL;
*(__int64 *)((char *)&v15.m256i_i64[1] + 2) = 0xFFFF000000000100LL;
v15.m256i_i16[9] = 8703;
}
log_write(6LL, "Sending SetEncodings message");
v9 = 0LL;
v10 = &v15;
aio_write(0LL);
if ( g_framebuffer )
return host_activate(v9, v10);
v9 = (unsigned int)s_cl_listen_port;
v10 = 0LL;
if ( (unsigned int)aio_listen((unsigned int)s_cl_listen_port, 0LL, &af_client_accept, 992LL) )
return host_activate(v9, v10);
v12 = _errno_location();
v13 = strerror(*v12);
log_write(1LL, "Error creating listening socket: %s", v13);
return aio_close(1LL);
}

canonical_decompiled_code:
__int64 func10()
{
__int64 v0; // rbp
__int64 v1; // rdx
__int64 v2; // rcx
size_t v3; // r13
void *v4; // rax
void *v5; // rdi
int v6; // er12
void *v7; // rbx
__int64 v8; // rax
__int64 v9; // rdi
__m256i *v10; // rsi
int *v12; // rax
char *v13; // rax
char v14[28]; // [rsp+4h] [rbp-64h] BYREF
__m256i v15; // [rsp+20h] [rbp-48h] BYREF

v0 = cur_slot;
*(_OWORD *)&v15.m256i_u64[2] = 0LL;
v1 = *(unsigned int *)(cur_slot + 368LL);
v2 = *(_QWORD *)(cur_slot + 32LL);
*(_OWORD *)v15.m256i_i8 = 2uLL;
log_write(4LL, "Remote desktop name: %.*s", v1, v2);
v3 = *(unsigned int *)(v0 + 368);
v4 = malloc(v3 + 1);
if ( v4 )
{
v5 = (void *)*((_QWORD *)&g_screen_info + 3);
v6 = v3;
v7 = v4;
if ( v5 )
{
free(v5);
v3 = *(unsigned int *)(v0 + 368);
v6 = *(_DWORD *)(v0 + 368);
}
v8 = cur_slot;
*((_QWORD *)&g_screen_info + 3) = v7;
*((_DWORD *)&g_screen_info + 5) = v6;
memcpy(v7, *(const void **)(v8 + 32), v3);
*((_BYTE *)v7 + *(unsigned int *)(v0 + 368)) = 0;
}
log_write(5LL, "Setting up pixel format");
((void (__fastcall *)(char *, _QWORD))buf_put_pixfmt)(v14, (unsigned int)((_DWORD)&g_screen_info + 4));
log_write(6LL, "Sending SetPixelFormat message");
aio_write(0LL);
if ( s_request_tight )
{
log_write(5LL, "Requesting Tight encoding");
*(__int64 *)((char *)&v15.m256i_i64[1] + 4) = 0x1000000LL;
*(__int64 *)((char *)v15.m256i_i64 + 4) = 0x500000007000000LL;
*(__int64 *)((char *)&v15.m256i_i64[2] + 4) = 0x21FFFFFF20FFFFFFLL;
if ( (unsigned int)s_tight_level <= 9 )
{
log_write(5LL, "Requesting compression level %d", (unsigned int)s_tight_level);
v15.m256i_i8[31] = s_tight_level;
v15.m256i_i8[30] = (unsigned __int16)(s_tight_level - 256) >> 8;
v15.m256i_i8[28] = (unsigned int)(s_tight_level - 256) >> 24;
v15.m256i_i8[29] = (unsigned int)(s_tight_level - 256) >> 16;
v15.m256i_i16[1] = 1792;
}
else
{
v15.m256i_i16[1] = 1536;
}
}
else
{
log_write(5LL, "Requesting Hextile encoding");
*(__int64 *)((char *)v15.m256i_i64 + 2) = 0x50000000400LL;
*(__int64 *)((char *)&v15.m256i_i64[1] + 2) = 0xFFFF000000000100LL;
v15.m256i_i16[9] = 8703;
}
log_write(6LL, "Sending SetEncodings message");
v9 = 0LL;
v10 = &v15;
aio_write(0LL);
if ( g_framebuffer )
return host_activate(v9, v10);
v9 = (unsigned int)s_cl_listen_port;
v10 = 0LL;
if ( (unsigned int)aio_listen((unsigned int)s_cl_listen_port, 0LL, &af_client_accept, 992LL) )
return host_activate(v9, v10);
v12 = _errno_location();
v13 = strerror(*v12);
log_write(1LL, "Error creating listening socket: %s", v13);
return aio_close(1LL);
}

original_code:
static void rf_host_set_formats(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  u_int8_t *new_name;
  unsigned char setpixfmt_msg[4 + 16];
  unsigned char setenc_msg[32] = {
    2,
    0,
    0, 0
  };
  int setenc_msg_size;
  log_write(4, "Remote desktop name: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  new_name = malloc((size_t)hs->temp_len + 1);
  if (new_name != ((void *)0)) {
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    g_screen_info.name = new_name;
    g_screen_info.name_length = hs->temp_len;
    memcpy(g_screen_info.name, cur_slot->readbuf, hs->temp_len);
    g_screen_info.name[hs->temp_len] = '\0';
  }
  log_write(5, "Setting up pixel format");
  memset(setpixfmt_msg, 0, 4);
  buf_put_pixfmt(&setpixfmt_msg[4], &g_screen_info.pixformat);
  log_write(6, "Sending SetPixelFormat message");
  aio_write(((void *)0), setpixfmt_msg, sizeof(setpixfmt_msg));
  if (s_request_tight) {
    log_write(5, "Requesting Tight encoding");
    { ((u_int8_t *)&setenc_msg[4])[0] = (u_int8_t)(7 >> 24); ((u_int8_t *)&setenc_msg[4])[1] = (u_int8_t)(7 >> 16); ((u_int8_t *)&setenc_msg[4])[2] = (u_int8_t)(7 >> 8); ((u_int8_t *)&setenc_msg[4])[3] = (u_int8_t)7; };
    { ((u_int8_t *)&setenc_msg[8])[0] = (u_int8_t)(5 >> 24); ((u_int8_t *)&setenc_msg[8])[1] = (u_int8_t)(5 >> 16); ((u_int8_t *)&setenc_msg[8])[2] = (u_int8_t)(5 >> 8); ((u_int8_t *)&setenc_msg[8])[3] = (u_int8_t)5; };
    { ((u_int8_t *)&setenc_msg[12])[0] = (u_int8_t)(1 >> 24); ((u_int8_t *)&setenc_msg[12])[1] = (u_int8_t)(1 >> 16); ((u_int8_t *)&setenc_msg[12])[2] = (u_int8_t)(1 >> 8); ((u_int8_t *)&setenc_msg[12])[3] = (u_int8_t)1; };
    { ((u_int8_t *)&setenc_msg[16])[0] = (u_int8_t)(0 >> 24); ((u_int8_t *)&setenc_msg[16])[1] = (u_int8_t)(0 >> 16); ((u_int8_t *)&setenc_msg[16])[2] = (u_int8_t)(0 >> 8); ((u_int8_t *)&setenc_msg[16])[3] = (u_int8_t)0; };
    { ((u_int8_t *)&setenc_msg[20])[0] = (u_int8_t)(0xFFFFFF20 >> 24); ((u_int8_t *)&setenc_msg[20])[1] = (u_int8_t)(0xFFFFFF20 >> 16); ((u_int8_t *)&setenc_msg[20])[2] = (u_int8_t)(0xFFFFFF20 >> 8); ((u_int8_t *)&setenc_msg[20])[3] = (u_int8_t)0xFFFFFF20; };
    { ((u_int8_t *)&setenc_msg[24])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((u_int8_t *)&setenc_msg[24])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((u_int8_t *)&setenc_msg[24])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((u_int8_t *)&setenc_msg[24])[3] = (u_int8_t)0xFFFFFF21; };
    if (s_tight_level >= 0 && s_tight_level <= 9) {
      log_write(5, "Requesting compression level %d", s_tight_level);
      { ((u_int8_t *)&setenc_msg[28])[0] = (u_int8_t)((0xFFFFFF00 + (u_int32_t)s_tight_level) >> 24); ((u_int8_t *)&setenc_msg[28])[1] = (u_int8_t)((0xFFFFFF00 + (u_int32_t)s_tight_level) >> 16); ((u_int8_t *)&setenc_msg[28])[2] = (u_int8_t)((0xFFFFFF00 + (u_int32_t)s_tight_level) >> 8); ((u_int8_t *)&setenc_msg[28])[3] = (u_int8_t)(0xFFFFFF00 + (u_int32_t)s_tight_level); };
      { ((u_int8_t *)&setenc_msg[2])[0] = (u_int8_t)(7 >> 8); ((u_int8_t *)&setenc_msg[2])[1] = (u_int8_t)7; };
      setenc_msg_size = 32;
    } else {
      { ((u_int8_t *)&setenc_msg[2])[0] = (u_int8_t)(6 >> 8); ((u_int8_t *)&setenc_msg[2])[1] = (u_int8_t)6; };
      setenc_msg_size = 28;
    }
  } else {
    log_write(5, "Requesting Hextile encoding");
    { ((u_int8_t *)&setenc_msg[4])[0] = (u_int8_t)(5 >> 24); ((u_int8_t *)&setenc_msg[4])[1] = (u_int8_t)(5 >> 16); ((u_int8_t *)&setenc_msg[4])[2] = (u_int8_t)(5 >> 8); ((u_int8_t *)&setenc_msg[4])[3] = (u_int8_t)5; };
    { ((u_int8_t *)&setenc_msg[8])[0] = (u_int8_t)(1 >> 24); ((u_int8_t *)&setenc_msg[8])[1] = (u_int8_t)(1 >> 16); ((u_int8_t *)&setenc_msg[8])[2] = (u_int8_t)(1 >> 8); ((u_int8_t *)&setenc_msg[8])[3] = (u_int8_t)1; };
    { ((u_int8_t *)&setenc_msg[12])[0] = (u_int8_t)(0 >> 24); ((u_int8_t *)&setenc_msg[12])[1] = (u_int8_t)(0 >> 16); ((u_int8_t *)&setenc_msg[12])[2] = (u_int8_t)(0 >> 8); ((u_int8_t *)&setenc_msg[12])[3] = (u_int8_t)0; };
    { ((u_int8_t *)&setenc_msg[16])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((u_int8_t *)&setenc_msg[16])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((u_int8_t *)&setenc_msg[16])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((u_int8_t *)&setenc_msg[16])[3] = (u_int8_t)0xFFFFFF21; };
    { ((u_int8_t *)&setenc_msg[2])[0] = (u_int8_t)(4 >> 8); ((u_int8_t *)&setenc_msg[2])[1] = (u_int8_t)4; };
    setenc_msg_size = 20;
  }
  log_write(6, "Sending SetEncodings message");
  aio_write(((void *)0), setenc_msg, setenc_msg_size);
  if (g_framebuffer == ((void *)0)) {
    if (!aio_listen(s_cl_listen_port, ((void *)0), af_client_accept,
                    sizeof(CL_SLOT))) {
      log_write(1, "Error creating listening socket: %s",
                strerror((*__errno_location ())));
      aio_close(1);
      return;
    }
  }
  host_activate();
}

canonical_original_code:
static void rf_host_set_formats()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  unsigned int *new_name;
  unsigned char setpixfmt_msg[-1];
  unsigned char setenc_msg[32] = {
    2,
    0,
    0, 0
  };
  int setenc_msg_size;
  log_write(4, "Remote desktop name: %.*s",
            (int)hs->temp_len, cur_slot->readbuf);
  new_name = malloc((unsigned long)hs->temp_len + 1);
  if (new_name != ((void *)0)) {
    if (g_screen_info.name != ((void *)0))
      free(g_screen_info.name);
    g_screen_info.name = new_name;
    g_screen_info.name_length = hs->temp_len;
    memcpy(g_screen_info.name, cur_slot->readbuf, hs->temp_len);
    g_screen_info.name[hs->temp_len] = '\0';
  }
  log_write(5, "Setting up pixel format");
  memset(setpixfmt_msg, 0, 4);
  buf_put_pixfmt(&setpixfmt_msg[4], &g_screen_info.pixformat);
  log_write(6, "Sending SetPixelFormat message");
  aio_write(((void *)0), setpixfmt_msg, sizeof(setpixfmt_msg));
  if (s_request_tight) {
    log_write(5, "Requesting Tight encoding");
    { ((unsigned int *)&setenc_msg[4])[0] = (u_int8_t)(7 >> 24); ((unsigned int *)&setenc_msg[4])[1] = (u_int8_t)(7 >> 16); ((unsigned int *)&setenc_msg[4])[2] = (u_int8_t)(7 >> 8); ((unsigned int *)&setenc_msg[4])[3] = (unsigned int)7; };
    { ((unsigned int *)&setenc_msg[8])[0] = (u_int8_t)(5 >> 24); ((unsigned int *)&setenc_msg[8])[1] = (u_int8_t)(5 >> 16); ((unsigned int *)&setenc_msg[8])[2] = (u_int8_t)(5 >> 8); ((unsigned int *)&setenc_msg[8])[3] = (unsigned int)5; };
    { ((unsigned int *)&setenc_msg[12])[0] = (u_int8_t)(1 >> 24); ((unsigned int *)&setenc_msg[12])[1] = (u_int8_t)(1 >> 16); ((unsigned int *)&setenc_msg[12])[2] = (u_int8_t)(1 >> 8); ((unsigned int *)&setenc_msg[12])[3] = (unsigned int)1; };
    { ((unsigned int *)&setenc_msg[16])[0] = (u_int8_t)(0 >> 24); ((unsigned int *)&setenc_msg[16])[1] = (u_int8_t)(0 >> 16); ((unsigned int *)&setenc_msg[16])[2] = (u_int8_t)(0 >> 8); ((unsigned int *)&setenc_msg[16])[3] = (unsigned int)0; };
    { ((unsigned int *)&setenc_msg[20])[0] = (u_int8_t)(0xFFFFFF20 >> 24); ((unsigned int *)&setenc_msg[20])[1] = (u_int8_t)(0xFFFFFF20 >> 16); ((unsigned int *)&setenc_msg[20])[2] = (u_int8_t)(0xFFFFFF20 >> 8); ((unsigned int *)&setenc_msg[20])[3] = (unsigned int)0xFFFFFF20; };
    { ((unsigned int *)&setenc_msg[24])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((unsigned int *)&setenc_msg[24])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((unsigned int *)&setenc_msg[24])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((unsigned int *)&setenc_msg[24])[3] = (unsigned int)0xFFFFFF21; };
    if (s_tight_level >= 0 && s_tight_level <= 9) {
      log_write(5, "Requesting compression level %d", s_tight_level);
      { ((unsigned int *)&setenc_msg[28])[0] = (u_int8_t)((0xFFFFFF00 + (unsigned int)s_tight_level) >> 24); ((unsigned int *)&setenc_msg[28])[1] = (u_int8_t)((0xFFFFFF00 + (unsigned int)s_tight_level) >> 16); ((unsigned int *)&setenc_msg[28])[2] = (u_int8_t)((0xFFFFFF00 + (unsigned int)s_tight_level) >> 8); ((unsigned int *)&setenc_msg[28])[3] = (u_int8_t)(0xFFFFFF00 + (unsigned int)s_tight_level); };
      { ((unsigned int *)&setenc_msg[2])[0] = (u_int8_t)(7 >> 8); ((unsigned int *)&setenc_msg[2])[1] = (unsigned int)7; };
      setenc_msg_size = 32;
    } else {
      { ((unsigned int *)&setenc_msg[2])[0] = (u_int8_t)(6 >> 8); ((unsigned int *)&setenc_msg[2])[1] = (unsigned int)6; };
      setenc_msg_size = 28;
    }
  } else {
    log_write(5, "Requesting Hextile encoding");
    { ((unsigned int *)&setenc_msg[4])[0] = (u_int8_t)(5 >> 24); ((unsigned int *)&setenc_msg[4])[1] = (u_int8_t)(5 >> 16); ((unsigned int *)&setenc_msg[4])[2] = (u_int8_t)(5 >> 8); ((unsigned int *)&setenc_msg[4])[3] = (unsigned int)5; };
    { ((unsigned int *)&setenc_msg[8])[0] = (u_int8_t)(1 >> 24); ((unsigned int *)&setenc_msg[8])[1] = (u_int8_t)(1 >> 16); ((unsigned int *)&setenc_msg[8])[2] = (u_int8_t)(1 >> 8); ((unsigned int *)&setenc_msg[8])[3] = (unsigned int)1; };
    { ((unsigned int *)&setenc_msg[12])[0] = (u_int8_t)(0 >> 24); ((unsigned int *)&setenc_msg[12])[1] = (u_int8_t)(0 >> 16); ((unsigned int *)&setenc_msg[12])[2] = (u_int8_t)(0 >> 8); ((unsigned int *)&setenc_msg[12])[3] = (unsigned int)0; };
    { ((unsigned int *)&setenc_msg[16])[0] = (u_int8_t)(0xFFFFFF21 >> 24); ((unsigned int *)&setenc_msg[16])[1] = (u_int8_t)(0xFFFFFF21 >> 16); ((unsigned int *)&setenc_msg[16])[2] = (u_int8_t)(0xFFFFFF21 >> 8); ((unsigned int *)&setenc_msg[16])[3] = (unsigned int)0xFFFFFF21; };
    { ((unsigned int *)&setenc_msg[2])[0] = (u_int8_t)(4 >> 8); ((unsigned int *)&setenc_msg[2])[1] = (unsigned int)4; };
    setenc_msg_size = 20;
  }
  log_write(6, "Sending SetEncodings message");
  aio_write(((void *)0), setenc_msg, setenc_msg_size);
  if (g_framebuffer == ((void *)0)) {
    if (!aio_listen(s_cl_listen_port, ((void *)0), af_client_accept,
                    sizeof(CL_SLOT))) {
      log_write(1, "Error creating listening socket: %s",
                strerror((*__errno_location ())));
      aio_close(1);
      return;
    }
  }
  host_activate();
}

variable_types: struct _HOST_SLOT { struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; } *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; } s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; } *

user_defined_types: struct _HOST_SLOT { struct _AIO_SLOT s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; }

user_defined_types: struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; }


decompiled_code:
void __fastcall set_host_encodings(int a1, int a2)
{
s_request_tight = a1;
s_tight_level = a2;
}

canonical_decompiled_code:
void __fastcall func13(int a1, int a2)
{
s_request_tight = a1;
s_tight_level = a2;
}

original_code:
void set_host_encodings(int request_tight, int tight_level)
{
  s_request_tight = request_tight;
  s_tight_level = tight_level;
}


decompiled_code:
__int64 __fastcall connect_to_host(char *a1, int a2)
{
unsigned int v2; // er12
struct hostent *v3; // rax
int v4; // edx
int v5; // eax
unsigned int v6; // er12
__int64 v7; // rdi
__int64 result; // rax
int *v9; // rax
char *v10; // rax
int v11; // edi
int *v12; // rax
char *v13; // rax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
struct sockaddr v18; // [rsp+0h] [rbp-18h] BYREF

if ( a1 )
s_host_info_file = a1;
if ( a2 )
s_cl_listen_port = a2;
v2 = parse_host_info();
if ( !v2 )
return v2;
if ( *(_WORD *)s_hostname != 42 )
{
log_write(3LL, "Connecting to %s, port %d", s_hostname, (unsigned int)s_host_port);
v3 = gethostbyname(s_hostname);
if ( v3 )
{
v18.sa_family = 2;
memcpy(&v18.sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v18.sa_data = _bswap_16(s_host_port);
v5 = socket(2, 1, v4);
v6 = v5;
if ( v5 == -1 )
{
v16 = _errno_location();
v2 = 0;
v17 = strerror(*v16);
log_write(1LL, "Could not create socket: %s", v17);
}
else if ( connect(v5, &v18, 0x10u) )
{
v9 = _errno_location();
v10 = strerror(*v9);
log_write(1LL, "Could not connect: %s", v10);
v11 = v6;
v2 = 0;
close(v11);
}
else
{
log_write(3LL, "Connection established");
v7 = v6;
v2 = 1;
aio_add_slot(v7, 0LL, host_init_hook, 376LL);
}
}
else
{
v14 = _errno_location();
v2 = 0;
v15 = strerror(*v14);
log_write(1LL, "Could not get host address: %s", v15);
}
return v2;
}
if ( (unsigned int)aio_listen((unsigned int)s_host_port, host_listen_init_hook, host_accept_hook, 376LL) )
{
log_write(3LL, "Listening for host connections on port %d", (unsigned int)s_host_port);
result = 1LL;
}
else
{
v12 = _errno_location();
v13 = strerror(*v12);
log_write(1LL, "Error creating listening socket: %s", v13);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *a1, int a2)
{
unsigned int v2; // er12
struct hostent *v3; // rax
int v4; // edx
int v5; // eax
unsigned int v6; // er12
__int64 v7; // rdi
__int64 result; // rax
int *v9; // rax
char *v10; // rax
int v11; // edi
int *v12; // rax
char *v13; // rax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
struct sockaddr v18; // [rsp+0h] [rbp-18h] BYREF

if ( a1 )
s_host_info_file = a1;
if ( a2 )
s_cl_listen_port = a2;
v2 = func11();
if ( !v2 )
return v2;
if ( *(_WORD *)s_hostname != 42 )
{
log_write(3LL, "Connecting to %s, port %d", s_hostname, (unsigned int)s_host_port);
v3 = gethostbyname(s_hostname);
if ( v3 )
{
v18.sa_family = 2;
memcpy(&v18.sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v18.sa_data = func12(s_host_port);
v5 = socket(2, 1, v4);
v6 = v5;
if ( v5 == -1 )
{
v16 = _errno_location();
v2 = 0;
v17 = strerror(*v16);
log_write(1LL, "Could not create socket: %s", v17);
}
else if ( connect(v5, &v18, 0x10u) )
{
v9 = _errno_location();
v10 = strerror(*v9);
log_write(1LL, "Could not connect: %s", v10);
v11 = v6;
v2 = 0;
close(v11);
}
else
{
log_write(3LL, "Connection established");
v7 = v6;
v2 = 1;
aio_add_slot(v7, 0LL, host_init_hook, 376LL);
}
}
else
{
v14 = _errno_location();
v2 = 0;
v15 = strerror(*v14);
log_write(1LL, "Could not get host address: %s", v15);
}
return v2;
}
if ( (unsigned int)aio_listen((unsigned int)s_host_port, host_listen_init_hook, host_accept_hook, 376LL) )
{
log_write(3LL, "Listening for host connections on port %d", (unsigned int)s_host_port);
result = 1LL;
}
else
{
v12 = _errno_location();
v13 = strerror(*v12);
log_write(1LL, "Error creating listening socket: %s", v13);
result = 0LL;
}
return result;
}

original_code:
int connect_to_host(char *host_info_file, int cl_listen_port)
{
  int host_fd;
  struct hostent *phe;
  struct sockaddr_in host_addr;
  if (host_info_file != ((void *)0))
    s_host_info_file = host_info_file;
  if (cl_listen_port != 0)
    s_cl_listen_port = cl_listen_port;
  if (!parse_host_info())
    return 0;
  if (strcmp(s_hostname, "*") != 0) {
    log_write(3, "Connecting to %s, port %d", s_hostname, s_host_port);
    phe = gethostbyname(s_hostname);
    if (phe == ((void *)0)) {
      log_write(1, "Could not get host address: %s", strerror((*__errno_location ())));
      return 0;
    }
    host_addr.sin_family = 2;
    memcpy(&host_addr.sin_addr.s_addr, phe->h_addr_list[0], phe->h_length);
    host_addr.sin_port = htons((unsigned short)s_host_port);
    host_fd = socket(2, SOCK_STREAM, 0);
    if (host_fd == -1) {
      log_write(1, "Could not create socket: %s", strerror((*__errno_location ())));
      return 0;
    }
    if (connect(host_fd, (struct sockaddr *)&host_addr,
                sizeof(host_addr)) != 0) {
      log_write(1, "Could not connect: %s", strerror((*__errno_location ())));
      close(host_fd);
      return 0;
    }
    log_write(3, "Connection established");
    aio_add_slot(host_fd, ((void *)0), host_init_hook, sizeof(HOST_SLOT));
  } else {
    if (!aio_listen(s_host_port, host_listen_init_hook, host_accept_hook,
                   sizeof(HOST_SLOT))) {
      log_write(1, "Error creating listening socket: %s",
                strerror((*__errno_location ())));
      return 0;
    }
    log_write(3, "Listening for host connections on port %d",
              s_host_port);
  }
  return 1;
}

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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

return_type: int

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

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


decompiled_code:
__int64 __fastcall alloc_framebuffer(int a1, int a2)
{
unsigned __int16 v2; // bp
unsigned __int16 v3; // bx
int v4; // er12
int v5; // edx
size_t v6; // r12
unsigned int v7; // eax
unsigned int v8; // er12

v2 = a2;
v3 = a1;
if ( g_framebuffer )
{
if ( g_fb_width >= a1 && g_fb_height >= a2 )
{
v8 = 1;
log_write(5LL, "No need to reallocate framebuffer and cache");
return v8;
}
log_write(5LL, "Freeing the framebuffer");
free(g_framebuffer);
if ( g_fb_width >= a1 )
v3 = g_fb_width;
if ( g_fb_height >= a2 )
v2 = g_fb_height;
g_fb_width = v3;
v4 = v3;
g_fb_height = v2;
v5 = v2;
}
else
{
g_fb_width = a1;
v4 = (unsigned __int16)a1;
v5 = (unsigned __int16)a2;
g_fb_height = a2;
}
v6 = 4LL * v5 * v4;
g_framebuffer = malloc(v6);
if ( !g_framebuffer )
{
v8 = 0;
log_write(1LL, "Error allocating framebuffer");
return v8;
}
log_write(5LL, "(Re)allocated framebuffer, %d bytes", v6);
if ( (unsigned int)allocate_enc_cache() )
{
v7 = sizeof_enc_cache();
v8 = 1;
log_write(5LL, "(Re)allocated cache for encoded data, %d bytes", v7);
return v8;
}
free(g_framebuffer);
g_framebuffer = 0LL;
log_write(1LL, "Error allocating cache for encoded data");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1, int a2)
{
unsigned __int16 v2; // bp
unsigned __int16 v3; // bx
int v4; // er12
int v5; // edx
size_t v6; // r12
unsigned int v7; // eax
unsigned int v8; // er12

v2 = a2;
v3 = a1;
if ( g_framebuffer )
{
if ( g_fb_width >= a1 && g_fb_height >= a2 )
{
v8 = 1;
log_write(5LL, "No need to reallocate framebuffer and cache");
return v8;
}
log_write(5LL, "Freeing the framebuffer");
free(g_framebuffer);
if ( g_fb_width >= a1 )
v3 = g_fb_width;
if ( g_fb_height >= a2 )
v2 = g_fb_height;
g_fb_width = v3;
v4 = v3;
g_fb_height = v2;
v5 = v2;
}
else
{
g_fb_width = a1;
v4 = (unsigned __int16)a1;
v5 = (unsigned __int16)a2;
g_fb_height = a2;
}
v6 = 4LL * v5 * v4;
g_framebuffer = malloc(v6);
if ( !g_framebuffer )
{
v8 = 0;
log_write(1LL, "Error allocating framebuffer");
return v8;
}
log_write(5LL, "(Re)allocated framebuffer, %d bytes", v6);
if ( (unsigned int)allocate_enc_cache() )
{
v7 = sizeof_enc_cache();
v8 = 1;
log_write(5LL, "(Re)allocated cache for encoded data, %d bytes", v7);
return v8;
}
free(g_framebuffer);
g_framebuffer = 0LL;
log_write(1LL, "Error allocating cache for encoded data");
return 0LL;
}

original_code:
int alloc_framebuffer(int w, int h)
{
  int fb_size;
  if (g_framebuffer == ((void *)0)) {
    g_fb_width = w;
    g_fb_height = h;
  } else {
    if (w <= g_fb_width && h <= g_fb_height) {
      log_write(5, "No need to reallocate framebuffer and cache");
      return 1;
    }
    log_write(5, "Freeing the framebuffer");
    free(g_framebuffer);
    g_fb_width = (w > g_fb_width) ? w : g_fb_width;
    g_fb_height = (h > g_fb_height) ? h : g_fb_height;
  }
  fb_size = (int)g_fb_width * (int)g_fb_height;
  g_framebuffer = malloc(fb_size * sizeof(u_int32_t));
  if (g_framebuffer == ((void *)0)) {
    log_write(1, "Error allocating framebuffer");
    return 0;
  }
  log_write(5, "(Re)allocated framebuffer, %d bytes",
            fb_size * sizeof(u_int32_t));
  if (!allocate_enc_cache()) {
    free(g_framebuffer);
    g_framebuffer = ((void *)0);
    log_write(1, "Error allocating cache for encoded data");
    return 0;
  }
  log_write(5, "(Re)allocated cache for encoded data, %d bytes",
            sizeof_enc_cache());
  return 1;
}

variable_types: w int

variable_types: h int


decompiled_code:
void __fastcall sh_reconnect_close(int a1)
{
aio_call_func(safe_reconnect_close, 1LL);
signal(a1, sh_reconnect_close);
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
aio_call_func(safe_reconnect_close, 1LL);
signal(a1, sh_reconnect_close);
}

original_code:
static void sh_reconnect_close(int signo)
{
  aio_call_func(safe_reconnect_close, 1);
  signal(signo, sh_reconnect_close);
}

variable_types: signo int


decompiled_code:
void __fastcall sh_reconnect_noclose(int a1)
{
aio_call_func(safe_reconnect_noclose, 1LL);
signal(a1, sh_reconnect_noclose);
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
aio_call_func(safe_reconnect_noclose, 1LL);
signal(a1, sh_reconnect_noclose);
}

original_code:
static void sh_reconnect_noclose(int signo)
{
  aio_call_func(safe_reconnect_noclose, 1);
  signal(signo, sh_reconnect_noclose);
}


decompiled_code:
void __fastcall sh_disconnect_clients(int a1)
{
aio_call_func(safe_disconnect_clients, 0LL);
signal(a1, sh_disconnect_clients);
}

canonical_decompiled_code:
void __fastcall func2(int a1)
{
aio_call_func(safe_disconnect_clients, 0LL);
signal(a1, sh_disconnect_clients);
}

original_code:
static void sh_disconnect_clients(int signo)
{
  aio_call_func(safe_disconnect_clients, 0);
  signal(signo, sh_disconnect_clients);
}


decompiled_code:
__int64 safe_disconnect_clients()
{
log_write(2LL, "Caught SIGHUP signal, disconnecting all clients");
return aio_walk_slots(fn_close, 1LL);
}

canonical_decompiled_code:
__int64 func3()
{
log_write(2LL, "Caught SIGHUP signal, disconnecting all clients");
return aio_walk_slots(fn_close, 1LL);
}

original_code:
static void safe_disconnect_clients(void)
{
  log_write(2, "Caught SIGHUP signal, disconnecting all clients");
  aio_walk_slots(fn_close, 1);
}

canonical_original_code:
static void safe_disconnect_clients()
{
  log_write(2, "Caught SIGHUP signal, disconnecting all clients");
  aio_walk_slots(fn_close, 1);
}


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

log_write(2LL, "Caught SIGUSR2 signal, (re)connecting to host");
aio_walk_slots(fn_stop_listening, 2LL);
aio_walk_slots(fn_close, 3LL);
result = aio_walk_slots(fn_reconnect_close, 4LL);
if ( !(_DWORD)result )
result = connect_to_host(0LL, 0LL);
return result;
}

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

log_write(2LL, "Caught SIGUSR2 signal, (re)connecting to host");
aio_walk_slots(fn_stop_listening, 2LL);
aio_walk_slots(fn_close, 3LL);
result = aio_walk_slots(fn_reconnect_close, 4LL);
if ( !(_DWORD)result )
result = connect_to_host(0LL, 0LL);
return result;
}

original_code:
static void safe_reconnect_close(void)
{
  log_write(2, "Caught SIGUSR2 signal, (re)connecting to host");
  aio_walk_slots(fn_stop_listening, 2);
  aio_walk_slots(fn_close, 3);
  if (aio_walk_slots(fn_reconnect_close, 4) == 0)
    connect_to_host(((void *)0), 0);
}

canonical_original_code:
static void safe_reconnect_close()
{
  log_write(2, "Caught SIGUSR2 signal, (re)connecting to host");
  aio_walk_slots(fn_stop_listening, 2);
  aio_walk_slots(fn_close, 3);
  if (aio_walk_slots(fn_reconnect_close, 4) == 0)
    connect_to_host(((void *)0), 0);
}


decompiled_code:
__int64 safe_reconnect_noclose()
{
log_write(2LL, "Caught SIGUSR1 signal, trying to (re)connect to host");
aio_walk_slots(fn_stop_listening, 2LL);
aio_walk_slots(fn_close, 3LL);
return connect_to_host(0LL, 0LL);
}

canonical_decompiled_code:
__int64 func5()
{
log_write(2LL, "Caught SIGUSR1 signal, trying to (re)connect to host");
aio_walk_slots(fn_stop_listening, 2LL);
aio_walk_slots(fn_close, 3LL);
return connect_to_host(0LL, 0LL);
}

original_code:
static void safe_reconnect_noclose(void)
{
  log_write(2, "Caught SIGUSR1 signal, trying to (re)connect to host");
  aio_walk_slots(fn_stop_listening, 2);
  aio_walk_slots(fn_close, 3);
  connect_to_host(((void *)0), 0);
}

canonical_original_code:
static void safe_reconnect_noclose()
{
  log_write(2, "Caught SIGUSR1 signal, trying to (re)connect to host");
  aio_walk_slots(fn_stop_listening, 2);
  aio_walk_slots(fn_close, 3);
  connect_to_host(((void *)0), 0);
}


decompiled_code:
__int64 __fastcall fn_reconnect_close(__int64 a1)
{
aio_close_other(a1, 0LL);
return connect_to_host(0LL, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
aio_close_other(a1, 0LL);
return connect_to_host(0LL, 0LL);
}

original_code:
static void fn_reconnect_close(AIO_SLOT *slot)
{
  aio_close_other(slot, 0);
  connect_to_host(((void *)0), 0);
}

canonical_original_code:
static void fn_reconnect_close(struct _AIO_SLOT *slot)
{
  aio_close_other(slot, 0);
  connect_to_host(((void *)0), 0);
}

variable_types: struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; } *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; } *


decompiled_code:
__int64 __fastcall fn_close(__int64 a1)
{
return aio_close_other(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return aio_close_other(a1, 0LL);
}

original_code:
static void fn_close(AIO_SLOT *slot)
{
  aio_close_other(slot, 0);
}

canonical_original_code:
static void fn_close(struct _AIO_SLOT *slot)
{
  aio_close_other(slot, 0);
}

user_defined_types: struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; }


decompiled_code:
__int64 __fastcall fn_stop_listening(__int64 a1)
{
log_write(5LL, "Closing listening slot");
return aio_close_other(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
log_write(5LL, "Closing listening slot");
return aio_close_other(a1, 0LL);
}

original_code:
static void fn_stop_listening(AIO_SLOT *slot)
{
  log_write(5, "Closing listening slot");
  aio_close_other(slot, 0);
}

canonical_original_code:
static void fn_stop_listening(struct _AIO_SLOT *slot)
{
  log_write(5, "Closing listening slot");
  aio_close_other(slot, 0);
}


decompiled_code:
__sighandler_t set_control_signals()
{
signal(1, sh_disconnect_clients);
signal(10, sh_reconnect_noclose);
return signal(12, sh_reconnect_close);
}

canonical_decompiled_code:
__sighandler_t func9()
{
signal(1, sh_disconnect_clients);
signal(10, sh_reconnect_noclose);
return signal(12, sh_reconnect_close);
}

original_code:
void set_control_signals(void)
{
  signal(1, sh_disconnect_clients);
  signal(10, sh_reconnect_noclose);
  signal(12, sh_reconnect_close);
}

canonical_original_code:
void set_control_signals()
{
  signal(1, sh_disconnect_clients);
  signal(10, sh_reconnect_noclose);
  signal(12, sh_reconnect_close);
}


decompiled_code:
__int64 hextile_next_tile()
{
int v0; // eax
int v1; // ecx
__int16 v2; // dx
__int16 v3; // dx
int v5; // eax
__int64 v6; // rdi
__int16 v7; // cx

v0 = (unsigned __int16)s_tile + 16;
v1 = WORD2(s_rect) + (unsigned __int16)s_rect;
v2 = WORD2(s_rect);
if ( v0 < v1 )
{
v3 = s_rect + WORD2(s_rect) - (s_tile + 16);
LOWORD(s_tile) = s_tile + 16;
if ( v1 > (unsigned __int16)v0 + 16 )
v3 = 16;
word_934 = v3;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
v5 = HIWORD(s_tile) + 16;
v6 = WORD3(s_rect) + (unsigned int)WORD1(s_rect);
if ( v5 < (int)v6 )
{
LOWORD(s_tile) = s_rect;
if ( WORD2(s_rect) > 0x10u )
v2 = 16;
v7 = WORD3(s_rect) + WORD1(s_rect) - (HIWORD(s_tile) + 16);
HIWORD(s_tile) += 16;
if ( (int)v6 > (unsigned __int16)v5 + 16 )
v7 = 16;
word_934 = v2;
word_936 = v7;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
return fbupdate_rect_done(v6, HIWORD(s_tile), WORD2(s_rect), WORD1(s_rect), (unsigned __int16)s_rect);
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
int v1; // ecx
__int16 v2; // dx
__int16 v3; // dx
int v5; // eax
__int64 v6; // rdi
__int16 v7; // cx

v0 = (unsigned __int16)s_tile + 16;
v1 = WORD2(s_rect) + (unsigned __int16)s_rect;
v2 = WORD2(s_rect);
if ( v0 < v1 )
{
v3 = s_rect + WORD2(s_rect) - (s_tile + 16);
LOWORD(s_tile) = s_tile + 16;
if ( v1 > (unsigned __int16)v0 + 16 )
v3 = 16;
word_934 = v3;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
v5 = HIWORD(s_tile) + 16;
v6 = WORD3(s_rect) + (unsigned int)WORD1(s_rect);
if ( v5 < (int)v6 )
{
LOWORD(s_tile) = s_rect;
if ( WORD2(s_rect) > 0x10u )
v2 = 16;
v7 = WORD3(s_rect) + WORD1(s_rect) - (HIWORD(s_tile) + 16);
HIWORD(s_tile) += 16;
if ( (int)v6 > (unsigned __int16)v5 + 16 )
v7 = 16;
word_934 = v2;
word_936 = v7;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
return fbupdate_rect_done(v6, HIWORD(s_tile), WORD2(s_rect), WORD1(s_rect), (unsigned __int16)s_rect);
}

original_code:
static void hextile_next_tile(void)
{
  if (s_tile.x + 16 < s_rect.x + s_rect.w) {
    s_tile.x += 16;
    if (s_tile.x + 16 < s_rect.x + s_rect.w)
      s_tile.w = 16;
    else
      s_tile.w = s_rect.x + s_rect.w - s_tile.x;
  } else if (s_tile.y + 16 < s_rect.y + s_rect.h) {
    s_tile.x = s_rect.x;
    s_tile.w = (s_rect.w < 16) ? s_rect.w : 16;
    s_tile.y += 16;
    if (s_tile.y + 16 < s_rect.y + s_rect.h)
      s_tile.h = 16;
    else
      s_tile.h = s_rect.y + s_rect.h - s_tile.y;
  } else {
    fbupdate_rect_done();
    return;
  }
  aio_setread(rf_host_hextile_subenc, ((void *)0), 1);
}

canonical_original_code:
static void hextile_next_tile()
{
  if (s_tile.x + 16 < s_rect.x + s_rect.w) {
    s_tile.x += 16;
    if (s_tile.x + 16 < s_rect.x + s_rect.w)
      s_tile.w = 16;
    else
      s_tile.w = s_rect.x + s_rect.w - s_tile.x;
  } else if (s_tile.y + 16 < s_rect.y + s_rect.h) {
    s_tile.x = s_rect.x;
    s_tile.w = (s_rect.w < 16) ? s_rect.w : 16;
    s_tile.y += 16;
    if (s_tile.y + 16 < s_rect.y + s_rect.h)
      s_tile.h = 16;
    else
      s_tile.h = s_rect.y + s_rect.h - s_tile.y;
  } else {
    fbupdate_rect_done();
    return;
  }
  aio_setread(rf_host_hextile_subenc, ((void *)0), 1);
}


decompiled_code:
__int64 rf_host_hextile_subenc()
{
char v0; // al
__int64 v1; // rdx
int v2; // ecx
unsigned int v3; // ecx

fbs_spool_byte(**(unsigned __int8 **)(cur_slot + 32LL));
v0 = **(_BYTE **)(cur_slot + 32LL);
s_subenc = v0;
if ( (v0 & 1) != 0 )
return aio_setread(
rf_host_hextile_raw,
&hextile_buf,
4 * (unsigned __int16)word_936 * (unsigned int)(unsigned __int16)word_934);
if ( (v0 & 2) != 0 )
{
if ( (v0 & 4) != 0 )
{
v1 = 8LL;
v2 = 8;
goto LABEL_5;
}
v1 = (unsigned int)((v0 & 8) != 0) + 4;
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
fill_fb_rect(&s_tile, (unsigned int)s_bg);
v0 = s_subenc;
if ( (s_subenc & 4) != 0 )
{
v1 = 4LL;
v2 = 4;
LABEL_5:
v3 = v2 + 1;
if ( (v0 & 8) != 0 )
v1 = v3;
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
if ( (s_subenc & 8) != 0 )
{
v1 = 1LL;
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 func1()
{
char v0; // al
__int64 v1; // rdx
int v2; // ecx
unsigned int v3; // ecx

fbs_spool_byte(**(unsigned __int8 **)(cur_slot + 32LL));
v0 = **(_BYTE **)(cur_slot + 32LL);
s_subenc = v0;
if ( (v0 & 1) != 0 )
return aio_setread(
rf_host_hextile_raw,
&hextile_buf,
4 * (unsigned __int16)word_936 * (unsigned int)(unsigned __int16)word_934);
if ( (v0 & 2) != 0 )
{
if ( (v0 & 4) != 0 )
{
v1 = 8LL;
v2 = 8;
goto LABEL_5;
}
v1 = (unsigned int)((v0 & 8) != 0) + 4;
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
fill_fb_rect(&s_tile, (unsigned int)s_bg);
v0 = s_subenc;
if ( (s_subenc & 4) != 0 )
{
v1 = 4LL;
v2 = 4;
LABEL_5:
v3 = v2 + 1;
if ( (v0 & 8) != 0 )
v1 = v3;
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
if ( (s_subenc & 8) != 0 )
{
v1 = 1LL;
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
return func0();
}

original_code:
static void rf_host_hextile_subenc(void)
{
  int data_size;
  fbs_spool_byte(cur_slot->readbuf[0]);
  s_subenc = cur_slot->readbuf[0];
  if (s_subenc & 0x01) {
    data_size = s_tile.w * s_tile.h * sizeof(u_int32_t);
    aio_setread(rf_host_hextile_raw, hextile_buf, data_size);
    return;
  }
  data_size = 0;
  if (s_subenc & 0x02) {
    data_size += sizeof(u_int32_t);
  } else {
    fill_fb_rect(&s_tile, s_bg);
  }
  if (s_subenc & 0x04)
    data_size += sizeof(u_int32_t);
  if (s_subenc & 0x08)
    data_size += sizeof(u_int8_t);
  if (data_size) {
    aio_setread(rf_host_hextile_hex, hextile_buf, data_size);
  } else {
    hextile_next_tile();
  }
}

canonical_original_code:
static void rf_host_hextile_subenc()
{
  int data_size;
  fbs_spool_byte(cur_slot->readbuf[0]);
  s_subenc = cur_slot->readbuf[0];
  if (s_subenc & 0x01) {
    data_size = s_tile.w * s_tile.h * sizeof(u_int32_t);
    aio_setread(rf_host_hextile_raw, hextile_buf, data_size);
    return;
  }
  data_size = 0;
  if (s_subenc & 0x02) {
    data_size += sizeof(u_int32_t);
  } else {
    fill_fb_rect(&s_tile, s_bg);
  }
  if (s_subenc & 0x04)
    data_size += sizeof(u_int32_t);
  if (s_subenc & 0x08)
    data_size += sizeof(u_int8_t);
  if (data_size) {
    aio_setread(rf_host_hextile_hex, hextile_buf, data_size);
  } else {
    hextile_next_tile();
  }
}


decompiled_code:
__int64 rf_host_hextile_hex()
{
char v0; // al
int *v1; // rbx
int v2; // edx
signed __int64 v3; // rsi
int v4; // edx
__int64 v5; // rdx

v0 = s_subenc;
v1 = &hextile_buf;
if ( (s_subenc & 2) != 0 )
{
v1 = (int *)&unk_524;
s_bg = hextile_buf;
fill_fb_rect(&s_tile, (unsigned int)hextile_buf);
v0 = s_subenc;
}
if ( (v0 & 4) != 0 )
{
v2 = *v1++;
s_fg = v2;
}
v3 = (char *)v1 - (char *)&hextile_buf;
if ( (v0 & 8) != 0 )
{
fbs_spool_data(&hextile_buf, v3 + 1);
v4 = *(unsigned __int8 *)v1;
s_num_subrects = *(_BYTE *)v1;
if ( (s_subenc & 0x10) != 0 )
v4 *= 3;
v5 = (unsigned int)(2 * v4);
if ( (_DWORD)v5 )
return aio_setread(rf_host_hextile_subrects, 0LL, v5);
}
else
{
fbs_spool_data(&hextile_buf, v3);
}
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 func2()
{
char v0; // al
int *v1; // rbx
int v2; // edx
signed __int64 v3; // rsi
int v4; // edx
__int64 v5; // rdx

v0 = s_subenc;
v1 = &hextile_buf;
if ( (s_subenc & 2) != 0 )
{
v1 = (int *)&unk_524;
s_bg = hextile_buf;
fill_fb_rect(&s_tile, (unsigned int)hextile_buf);
v0 = s_subenc;
}
if ( (v0 & 4) != 0 )
{
v2 = *v1++;
s_fg = v2;
}
v3 = (char *)v1 - (char *)&hextile_buf;
if ( (v0 & 8) != 0 )
{
fbs_spool_data(&hextile_buf, v3 + 1);
v4 = *(unsigned __int8 *)v1;
s_num_subrects = *(_BYTE *)v1;
if ( (s_subenc & 0x10) != 0 )
v4 *= 3;
v5 = (unsigned int)(2 * v4);
if ( (_DWORD)v5 )
return aio_setread(rf_host_hextile_subrects, 0LL, v5);
}
else
{
fbs_spool_data(&hextile_buf, v3);
}
return func0();
}

original_code:
static void rf_host_hextile_hex(void)
{
  u_int32_t *from_ptr = hextile_buf;
  int data_size;
  if (s_subenc & 0x02) {
    s_bg = *from_ptr++;
    fill_fb_rect(&s_tile, s_bg);
  }
  if (s_subenc & 0x04) {
    s_fg = *from_ptr++;
  }
  if (s_subenc & 0x08) {
    fbs_spool_data(hextile_buf, (from_ptr - hextile_buf) * sizeof(u_int32_t) + 1);
    s_num_subrects = *((u_int8_t *)from_ptr);
    if (s_subenc & 0x10) {
      data_size = 6 * (unsigned int)s_num_subrects;
    } else {
      data_size = 2 * (unsigned int)s_num_subrects;
    }
    if (data_size > 0) {
      aio_setread(rf_host_hextile_subrects, ((void *)0), data_size);
      return;
    }
  } else {
    fbs_spool_data(hextile_buf, (from_ptr - hextile_buf) * sizeof(u_int32_t));
  }
  hextile_next_tile();
}

canonical_original_code:
static void rf_host_hextile_hex()
{
  unsigned int *from_ptr = hextile_buf;
  int data_size;
  if (s_subenc & 0x02) {
    s_bg = *from_ptr++;
    fill_fb_rect(&s_tile, s_bg);
  }
  if (s_subenc & 0x04) {
    s_fg = *from_ptr++;
  }
  if (s_subenc & 0x08) {
    fbs_spool_data(hextile_buf, (from_ptr - hextile_buf) * sizeof(u_int32_t) + 1);
    s_num_subrects = *((unsigned int *)from_ptr);
    if (s_subenc & 0x10) {
      data_size = 6 * (unsigned int)s_num_subrects;
    } else {
      data_size = 2 * (unsigned int)s_num_subrects;
    }
    if (data_size > 0) {
      aio_setread(rf_host_hextile_subrects, ((void *)0), data_size);
      return;
    }
  } else {
    fbs_spool_data(hextile_buf, (from_ptr - hextile_buf) * sizeof(u_int32_t));
  }
  hextile_next_tile();
}


decompiled_code:
__int64 rf_host_hextile_subrects()
{
__int64 v0; // rbx
__int64 v1; // rbp
int v2; // eax
unsigned __int8 v3; // di
unsigned __int8 v4; // si
__int64 v6; // rbp
unsigned __int8 v7; // di
unsigned __int8 v8; // si

v0 = *(_QWORD *)(cur_slot + 32LL);
if ( (s_subenc & 0x10) != 0 )
{
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 6 * (unsigned __int8)s_num_subrects);
if ( s_num_subrects )
{
v1 = v0 + 6LL * (unsigned __int8)s_num_subrects;
do
{
v2 = *(_DWORD *)v0;
v3 = *(_BYTE *)(v0 + 4);
v0 += 6LL;
v4 = *(_BYTE *)(v0 - 1);
s_fg = v2;
hextile_fill_subrect(v3, v4);
}
while ( v0 != v1 );
}
}
else
{
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 2 * (unsigned __int8)s_num_subrects);
if ( s_num_subrects )
{
v6 = v0 + 2LL * (unsigned __int8)s_num_subrects;
do
{
v7 = *(_BYTE *)v0;
v8 = *(_BYTE *)(v0 + 1);
v0 += 2LL;
hextile_fill_subrect(v7, v8);
}
while ( v6 != v0 );
}
}
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbx
__int64 v1; // rbp
int v2; // eax
unsigned __int8 v3; // di
unsigned __int8 v4; // si
__int64 v6; // rbp
unsigned __int8 v7; // di
unsigned __int8 v8; // si

v0 = *(_QWORD *)(cur_slot + 32LL);
if ( (s_subenc & 0x10) != 0 )
{
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 6 * (unsigned __int8)s_num_subrects);
if ( s_num_subrects )
{
v1 = v0 + 6LL * (unsigned __int8)s_num_subrects;
do
{
v2 = *(_DWORD *)v0;
v3 = *(_BYTE *)(v0 + 4);
v0 += 6LL;
v4 = *(_BYTE *)(v0 - 1);
s_fg = v2;
hextile_fill_subrect(v3, v4);
}
while ( v0 != v1 );
}
}
else
{
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 2 * (unsigned __int8)s_num_subrects);
if ( s_num_subrects )
{
v6 = v0 + 2LL * (unsigned __int8)s_num_subrects;
do
{
v7 = *(_BYTE *)v0;
v8 = *(_BYTE *)(v0 + 1);
v0 += 2LL;
hextile_fill_subrect(v7, v8);
}
while ( v6 != v0 );
}
}
return func0();
}

original_code:
static void rf_host_hextile_subrects(void)
{
  u_int8_t *ptr;
  u_int8_t pos, dim;
  int i;
  ptr = cur_slot->readbuf;
  if (s_subenc & 0x10) {
    fbs_spool_data(ptr, s_num_subrects * 6);
    for (i = 0; i < (int)s_num_subrects; i++) {
      memcpy(&s_fg, ptr, sizeof(s_fg));
      ptr += sizeof(s_fg);
      pos = *ptr++;
      dim = *ptr++;
      hextile_fill_subrect(pos, dim);
    }
  } else {
    fbs_spool_data(ptr, s_num_subrects * 2);
    for (i = 0; i < (int)s_num_subrects; i++) {
      pos = *ptr++;
      dim = *ptr++;
      hextile_fill_subrect(pos, dim);
    }
  }
  hextile_next_tile();
}

canonical_original_code:
static void rf_host_hextile_subrects()
{
  unsigned int *ptr;
  unsigned int pos; unsigned int dim;
  int i;
  ptr = cur_slot->readbuf;
  if (s_subenc & 0x10) {
    fbs_spool_data(ptr, s_num_subrects * 6);
    for (i = 0; i < (int)s_num_subrects; i++) {
      memcpy(&s_fg, ptr, sizeof(s_fg));
      ptr += sizeof(s_fg);
      pos = *ptr++;
      dim = *ptr++;
      hextile_fill_subrect(pos, dim);
    }
  } else {
    fbs_spool_data(ptr, s_num_subrects * 2);
    for (i = 0; i < (int)s_num_subrects; i++) {
      pos = *ptr++;
      dim = *ptr++;
      hextile_fill_subrect(pos, dim);
    }
  }
  hextile_next_tile();
}

variable_types:
ptr
unsigned int *

variable_types:
pos
unsigned int

variable_types:
dim
unsigned int

variable_types: i int


decompiled_code:
__int64 rf_host_hextile_raw()
{
int *v0; // rdi
__int64 v1; // rsi
__int64 v2; // rdx
int *v3; // rcx
int *v4; // rbp
int v5; // ebx
char *v6; // rax

v0 = &hextile_buf;
v1 = 4LL * (unsigned __int16)word_936 * (unsigned __int16)word_934;
fbs_spool_data(&hextile_buf, v1);
v2 = g_framebuffer;
v3 = (int *)(g_framebuffer + 4LL * ((unsigned __int16)s_tile + g_fb_width * HIWORD(s_tile)));
if ( word_936 )
{
v2 = (unsigned __int16)word_934;
v4 = &hextile_buf;
v5 = 0;
do
{
v1 = (__int64)v4;
v0 = v3;
++v5;
v6 = (char *)memcpy(v3, v4, 4 * v2);
v2 = (unsigned __int16)word_934;
v4 += (unsigned __int16)word_934;
v3 = (int *)&v6[4 * g_fb_width];
}
while ( (unsigned __int16)word_936 > v5 );
}
return hextile_next_tile(v0, v1, v2, v3);
}

canonical_decompiled_code:
__int64 func4()
{
int *v0; // rdi
__int64 v1; // rsi
__int64 v2; // rdx
int *v3; // rcx
int *v4; // rbp
int v5; // ebx
char *v6; // rax

v0 = &hextile_buf;
v1 = 4LL * (unsigned __int16)word_936 * (unsigned __int16)word_934;
fbs_spool_data(&hextile_buf, v1);
v2 = g_framebuffer;
v3 = (int *)(g_framebuffer + 4LL * ((unsigned __int16)s_tile + g_fb_width * HIWORD(s_tile)));
if ( word_936 )
{
v2 = (unsigned __int16)word_934;
v4 = &hextile_buf;
v5 = 0;
do
{
v1 = (__int64)v4;
v0 = v3;
++v5;
v6 = (char *)memcpy(v3, v4, 4 * v2);
v2 = (unsigned __int16)word_934;
v4 += (unsigned __int16)word_934;
v3 = (int *)&v6[4 * g_fb_width];
}
while ( (unsigned __int16)word_936 > v5 );
}
return func0(v0, v1, v2, v3);
}

original_code:
static void rf_host_hextile_raw(void)
{
  int row;
  u_int32_t *from_ptr;
  u_int32_t *fb_ptr;
  fbs_spool_data(hextile_buf, s_tile.w * s_tile.h * sizeof(u_int32_t));
  from_ptr = hextile_buf;
  fb_ptr = &g_framebuffer[s_tile.y * (int)g_fb_width + s_tile.x];
  for (row = 0; row < s_tile.h; row++) {
    memcpy(fb_ptr, from_ptr, s_tile.w * sizeof(u_int32_t));
    from_ptr += s_tile.w;
    fb_ptr += g_fb_width;
  }
  hextile_next_tile();
}

canonical_original_code:
static void rf_host_hextile_raw()
{
  int row;
  unsigned int *from_ptr;
  unsigned int *fb_ptr;
  fbs_spool_data(hextile_buf, s_tile.w * s_tile.h * sizeof(u_int32_t));
  from_ptr = hextile_buf;
  fb_ptr = &g_framebuffer[s_tile.y * (int)g_fb_width + s_tile.x];
  for (row = 0; row < s_tile.h; row++) {
    memcpy(fb_ptr, from_ptr, s_tile.w * sizeof(u_int32_t));
    from_ptr += s_tile.w;
    fb_ptr += g_fb_width;
  }
  hextile_next_tile();
}

variable_types: row int


decompiled_code:
__int64 __fastcall setread_decode_hextile(const __m128i *a1)
{
__int16 v1; // ax
__int16 v2; // dx

s_rect = (__int128)_mm_loadu_si128(a1);
s_tile = s_rect;
v1 = 16;
v2 = 16;
if ( WORD2(s_rect) <= 0x10u )
v2 = WORD2(s_rect);
if ( WORD3(s_rect) <= 0x10u )
v1 = WORD3(s_rect);
word_934 = v2;
word_936 = v1;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(const __m128i *a1)
{
__int16 v1; // ax
__int16 v2; // dx

s_rect = (__int128)_mm_loadu_si128(a1);
s_tile = s_rect;
v1 = 16;
v2 = 16;
if ( WORD2(s_rect) <= 0x10u )
v2 = WORD2(s_rect);
if ( WORD3(s_rect) <= 0x10u )
v1 = WORD3(s_rect);
word_934 = v2;
word_936 = v1;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}

original_code:
void setread_decode_hextile(FB_RECT *r)
{
  s_rect = *r;
  s_tile.x = s_rect.x;
  s_tile.y = s_rect.y;
  s_tile.w = (s_rect.w < 16) ? s_rect.w : 16;
  s_tile.h = (s_rect.h < 16) ? s_rect.h : 16;
  aio_setread(rf_host_hextile_subenc, ((void *)0), sizeof(u_int8_t));
}

canonical_original_code:
void setread_decode_hextile(struct _FB_RECT *r)
{
  s_rect = *r;
  s_tile.x = s_rect.x;
  s_tile.y = s_rect.y;
  s_tile.w = (s_rect.w < 16) ? s_rect.w : 16;
  s_tile.h = (s_rect.h < 16) ? s_rect.h : 16;
  aio_setread(rf_host_hextile_subenc, ((void *)0), sizeof(u_int8_t));
}

variable_types: struct _FB_RECT { unsigned int x; unsigned int y; unsigned int w; unsigned int h; unsigned int enc; unsigned int src_x; unsigned int src_y; } *


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

result = (unsigned int)warned;
if ( !warned )
{
result = log_write(2LL, "Bad rectangle order in regions - not packing");
warned = 1;
}
return result;
}

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

result = (unsigned int)warned;
if ( !warned )
{
result = log_write(2LL, "Bad rectangle order in regions - not packing");
warned = 1;
}
return result;
}

original_code:
static void report_bad_rect_order(void) {
  if (!warned) {
    log_write(2, "Bad rectangle order in regions - not packing");
    warned = 1;
  }
}

canonical_original_code:
static void report_bad_rect_order() {
  if (!warned) {
    log_write(2, "Bad rectangle order in regions - not packing");
    warned = 1;
  }
}


decompiled_code:
__int64 rf_host_colormap_hdr()
{
unsigned __int16 v0; // ax
__int64 result; // rax

log_write(2LL, "Ignoring SetColourMapEntries message");
v0 = __ROL2__(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 3LL), 8);
if ( v0 )
result = aio_setread(rf_host_colormap_data, 0LL, 6 * (unsigned int)v0);
else
result = aio_setread(rf_host_msg, 0LL, 1LL);
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned __int16 v0; // ax
__int64 result; // rax

log_write(2LL, "Ignoring SetColourMapEntries message");
v0 = __ROL2__(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 3LL), 8);
if ( v0 )
result = aio_setread(rf_host_colormap_data, 0LL, 6 * (unsigned int)v0);
else
result = aio_setread(rf_host_msg, 0LL, 1LL);
return result;
}

original_code:
static void rf_host_colormap_hdr(void)
{
  u_int16_t num_colors;
  log_write(2, "Ignoring SetColourMapEntries message");
  num_colors = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[3])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[3])[1]);
  if (num_colors > 0)
    aio_setread(rf_host_colormap_data, ((void *)0), num_colors * 6);
  else
    aio_setread(rf_host_msg, ((void *)0), 1);
}

canonical_original_code:
static void rf_host_colormap_hdr()
{
  unsigned int num_colors;
  log_write(2, "Ignoring SetColourMapEntries message");
  num_colors = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[3])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[3])[1]);
  if (num_colors > 0)
    aio_setread(rf_host_colormap_data, ((void *)0), num_colors * 6);
  else
    aio_setread(rf_host_msg, ((void *)0), 1);
}


decompiled_code:
__int64 rf_host_colormap_data()
{
return aio_setread(rf_host_msg, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 func1()
{
return aio_setread(rf_host_msg, 0LL, 1LL);
}

original_code:
static void rf_host_colormap_data(void)
{
  aio_setread(rf_host_msg, ((void *)0), 1);
}

canonical_original_code:
static void rf_host_colormap_data()
{
  aio_setread(rf_host_msg, ((void *)0), 1);
}


decompiled_code:
__int64 rf_host_cuttext_data()
{
cut_text = *(_QWORD *)(cur_slot + 32LL);
aio_walk_slots(fn_host_pass_cuttext, 1LL);
return aio_setread(rf_host_msg, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 func2()
{
cut_text = *(_QWORD *)(cur_slot + 32LL);
aio_walk_slots(fn_host_pass_cuttext, 1LL);
return aio_setread(rf_host_msg, 0LL, 1LL);
}

original_code:
static void rf_host_cuttext_data(void)
{
  cut_text = cur_slot->readbuf;
  aio_walk_slots(fn_host_pass_cuttext, 1);
  aio_setread(rf_host_msg, ((void *)0), 1);
}

canonical_original_code:
static void rf_host_cuttext_data()
{
  cut_text = cur_slot->readbuf;
  aio_walk_slots(fn_host_pass_cuttext, 1);
  aio_setread(rf_host_msg, ((void *)0), 1);
}


decompiled_code:
__int64 rf_host_cuttext_hdr()
{
__int64 v0; // rdx
__int64 result; // rax

log_write(5LL, "Receiving ServerCutText message from host, %lu byte(s)", cut_len);
v0 = _byteswap_ulong(*(_DWORD *)(*(_QWORD *)(cur_slot + 32LL) + 3LL));
cut_len = (unsigned int)v0;
if ( (_DWORD)v0 )
result = aio_setread(rf_host_cuttext_data, 0LL, v0);
else
result = rf_host_cuttext_data();
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rdx
__int64 result; // rax

log_write(5LL, "Receiving ServerCutText message from host, %lu byte(s)", cut_len);
v0 = _byteswap_ulong(*(_DWORD *)(*(_QWORD *)(cur_slot + 32LL) + 3LL));
cut_len = (unsigned int)v0;
if ( (_DWORD)v0 )
result = aio_setread(rf_host_cuttext_data, 0LL, v0);
else
result = func2();
return result;
}

original_code:
static void rf_host_cuttext_hdr(void)
{
  log_write(5,
            "Receiving ServerCutText message from host, %lu byte(s)",
            (unsigned long)cut_len);
  cut_len = (size_t)((u_int32_t)((u_int8_t *)&cur_slot->readbuf[3])[0] << 24 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[3])[1] << 16 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[3])[2] << 8 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[3])[3]);;
  if (cut_len > 0)
    aio_setread(rf_host_cuttext_data, ((void *)0), cut_len);
  else
    rf_host_cuttext_data();
}

canonical_original_code:
static void rf_host_cuttext_hdr()
{
  log_write(5,
            "Receiving ServerCutText message from host, %lu byte(s)",
            (unsigned long)cut_len);
  cut_len = (unsigned long)((u_int32_t)((unsigned int *)&cur_slot->readbuf[3])[0] << 24 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[3])[1] << 16 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[3])[2] << 8 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[3])[3]);;
  if (cut_len > 0)
    aio_setread(rf_host_cuttext_data, ((void *)0), cut_len);
  else
    rf_host_cuttext_data();
}


decompiled_code:
__int64 __fastcall fn_host_pass_newfbsize(__int64 a1)
{
int v1; // eax
int v3[6]; // [rsp+0h] [rbp-18h] BYREF

v3[2] = -223;
v1 = *(_DWORD *)(cur_slot + 372LL);
v3[0] = 0;
v3[1] = v1;
return fn_client_add_rect(a1, v3);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v1; // eax
int v3[6]; // [rsp+0h] [rbp-18h] BYREF

v3[2] = -223;
v1 = *(_DWORD *)(cur_slot + 372LL);
v3[0] = 0;
v3[1] = v1;
return fn_client_add_rect(a1, v3);
}

original_code:
static void fn_host_pass_newfbsize(AIO_SLOT *slot)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  FB_RECT r;
  r.enc = 0xFFFFFF21;
  r.x = r.y = 0;
  r.w = hs->fb_width;
  r.h = hs->fb_height;
  fn_client_add_rect(slot, &r);
}

canonical_original_code:
static void fn_host_pass_newfbsize(struct _AIO_SLOT *slot)
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  struct _FB_RECT r;
  r.enc = 0xFFFFFF21;
  r.x = r.y = 0;
  r.w = hs->fb_width;
  r.h = hs->fb_height;
  fn_client_add_rect(slot, &r);
}

variable_types: struct _FB_RECT { unsigned int x; unsigned int y; unsigned int w; unsigned int h; unsigned int enc; unsigned int src_x; unsigned int src_y; }


decompiled_code:
__int64 __fastcall fn_host_add_client_rect(__int64 a1)
{
return fn_client_add_rect(a1, &cur_rect);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return fn_client_add_rect(a1, &cur_rect);
}

original_code:
static void fn_host_add_client_rect(AIO_SLOT *slot)
{
  fn_client_add_rect(slot, &cur_rect);
}

canonical_original_code:
static void fn_host_add_client_rect(struct _AIO_SLOT *slot)
{
  fn_client_add_rect(slot, &cur_rect);
}


decompiled_code:
__int64 rf_host_msg()
{
__int64 v0; // rdx
__int64 result; // rax

v0 = **(unsigned __int8 **)(cur_slot + 32LL);
if ( (_BYTE)v0 == 2 )
{
log_write(5LL, "Received Bell message from host");
fbs_write_data(*(_QWORD *)(cur_slot + 32LL), 1LL);
result = aio_setread(rf_host_msg, 0LL, 1LL);
}
else if ( (unsigned __int8)v0 > 2u )
{
if ( (_BYTE)v0 == 3 )
{
result = aio_setread(rf_host_cuttext_hdr, 0LL, 7LL);
}
else
{
log_write(1LL, "Unknown server message type: %d", v0);
result = aio_close(0LL);
}
}
else if ( (_BYTE)v0 )
{
result = aio_setread(rf_host_colormap_hdr, 0LL, 5LL);
}
else
{
result = aio_setread(rf_host_fbupdate_hdr, 0LL, 3LL);
}
return result;
}

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

v0 = **(unsigned __int8 **)(cur_slot + 32LL);
if ( (_BYTE)v0 == 2 )
{
log_write(5LL, "Received Bell message from host");
fbs_write_data(*(_QWORD *)(cur_slot + 32LL), 1LL);
result = aio_setread(rf_host_msg, 0LL, 1LL);
}
else if ( (unsigned __int8)v0 > 2u )
{
if ( (_BYTE)v0 == 3 )
{
result = aio_setread(rf_host_cuttext_hdr, 0LL, 7LL);
}
else
{
log_write(1LL, "Unknown server message type: %d", v0);
result = aio_close(0LL);
}
}
else if ( (_BYTE)v0 )
{
result = aio_setread(rf_host_colormap_hdr, 0LL, 5LL);
}
else
{
result = aio_setread(rf_host_fbupdate_hdr, 0LL, 3LL);
}
return result;
}

original_code:
static void rf_host_msg(void)
{
  int msg_id;
  msg_id = (int)cur_slot->readbuf[0] & 0xFF;
  switch(msg_id) {
  case 0:
    aio_setread(rf_host_fbupdate_hdr, ((void *)0), 3);
    break;
  case 1:
    aio_setread(rf_host_colormap_hdr, ((void *)0), 5);
    break;
  case 2:
    log_write(5, "Received Bell message from host");
    fbs_write_data(cur_slot->readbuf, 1);
    aio_setread(rf_host_msg, ((void *)0), 1);
    break;
  case 3:
    aio_setread(rf_host_cuttext_hdr, ((void *)0), 7);
    break;
  default:
    log_write(1, "Unknown server message type: %d", msg_id);
    aio_close(0);
  }
}

canonical_original_code:
static void rf_host_msg()
{
  int msg_id;
  msg_id = (int)cur_slot->readbuf[0] & 0xFF;
  switch(msg_id) {
  case 0:
    aio_setread(rf_host_fbupdate_hdr, ((void *)0), 3);
    break;
  case 1:
    aio_setread(rf_host_colormap_hdr, ((void *)0), 5);
    break;
  case 2:
    log_write(5, "Received Bell message from host");
    fbs_write_data(cur_slot->readbuf, 1);
    aio_setread(rf_host_msg, ((void *)0), 1);
    break;
  case 3:
    aio_setread(rf_host_cuttext_hdr, ((void *)0), 7);
    break;
  default:
    log_write(1, "Unknown server message type: %d", msg_id);
    aio_close(0);
  }
}


decompiled_code:
__int64 __fastcall fn_host_pass_cuttext(__int64 a1)
{
return fn_client_send_cuttext(a1, cut_text, cut_len);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return fn_client_send_cuttext(a1, cut_text, cut_len);
}

original_code:
static void fn_host_pass_cuttext(AIO_SLOT *slot)
{
  fn_client_send_cuttext(slot, cut_text, cut_len);
}

canonical_original_code:
static void fn_host_pass_cuttext(struct _AIO_SLOT *slot)
{
  fn_client_send_cuttext(slot, cut_text, cut_len);
}


decompiled_code:
int request_update()
{
log_write(6LL, "Sending FramebufferUpdateRequest message");
return aio_write(0LL);
}

canonical_decompiled_code:
int func8()
{
log_write(6LL, "Sending FramebufferUpdateRequest message");
return aio_write(0LL);
}

original_code:
static void request_update(int incr)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  unsigned char fbupdatereq_msg[] = {
    3,
    0,
    0, 0, 0, 0,
    0, 0, 0, 0
  };
  fbupdatereq_msg[1] = (incr) ? 1 : 0;
  { ((u_int8_t *)&fbupdatereq_msg[6])[0] = (u_int8_t)(hs->fb_width >> 8); ((u_int8_t *)&fbupdatereq_msg[6])[1] = (u_int8_t)hs->fb_width; };
  { ((u_int8_t *)&fbupdatereq_msg[8])[0] = (u_int8_t)(hs->fb_height >> 8); ((u_int8_t *)&fbupdatereq_msg[8])[1] = (u_int8_t)hs->fb_height; };
  log_write(6, "Sending FramebufferUpdateRequest message");
  aio_write(((void *)0), fbupdatereq_msg, sizeof(fbupdatereq_msg));
}

canonical_original_code:
static void request_update(int incr)
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  unsigned char fbupdatereq_msg[] = {
    3,
    0,
    0, 0, 0, 0,
    0, 0, 0, 0
  };
  fbupdatereq_msg[1] = (incr) ? 1 : 0;
  { ((unsigned int *)&fbupdatereq_msg[6])[0] = (u_int8_t)(hs->fb_width >> 8); ((unsigned int *)&fbupdatereq_msg[6])[1] = (unsigned int)hs->fb_width; };
  { ((unsigned int *)&fbupdatereq_msg[8])[0] = (u_int8_t)(hs->fb_height >> 8); ((unsigned int *)&fbupdatereq_msg[8])[1] = (unsigned int)hs->fb_height; };
  log_write(6, "Sending FramebufferUpdateRequest message");
  aio_write(((void *)0), fbupdatereq_msg, sizeof(fbupdatereq_msg));
}

variable_types: incr int


decompiled_code:
__int64 __fastcall host_really_activate(__int64 a1)
{
__int64 v1; // rbp
__int16 v2; // si
__int64 result; // rax

v1 = cur_slot;
log_write(3LL, "Activating new host connection");
*(_DWORD *)a1 = 4;
s_host_slot = a1;
write_active_file();
perform_action("host_activate");
if ( !(unsigned int)alloc_framebuffer(*(unsigned __int16 *)(a1 + 372), *(unsigned __int16 *)(a1 + 374)) )
return aio_close(1LL);
v2 = *(_WORD *)(a1 + 374);
g_screen_info = *(_WORD *)(a1 + 372);
*((_WORD *)&g_screen_info + 1) = v2;
fbs_open_file();
cur_slot = a1;
reset_tight_streams();
log_write(5LL, "Requesting full framebuffer update");
request_update();
aio_setread(rf_host_msg, 0LL, 1LL);
result = aio_walk_slots(fn_host_pass_newfbsize, 1LL);
cur_slot = v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 v1; // rbp
__int16 v2; // si
__int64 result; // rax

v1 = cur_slot;
log_write(3LL, "Activating new host connection");
*(_DWORD *)a1 = 4;
s_host_slot = a1;
write_active_file();
perform_action("host_activate");
if ( !(unsigned int)alloc_framebuffer(*(unsigned __int16 *)(a1 + 372), *(unsigned __int16 *)(a1 + 374)) )
return aio_close(1LL);
v2 = *(_WORD *)(a1 + 374);
g_screen_info = *(_WORD *)(a1 + 372);
*((_WORD *)&g_screen_info + 1) = v2;
fbs_open_file();
cur_slot = a1;
reset_tight_streams();
log_write(5LL, "Requesting full framebuffer update");
func8();
aio_setread(rf_host_msg, 0LL, 1LL);
result = aio_walk_slots(fn_host_pass_newfbsize, 1LL);
cur_slot = v1;
return result;
}

original_code:
static void host_really_activate(AIO_SLOT *slot)
{
  AIO_SLOT *saved_slot = cur_slot;
  HOST_SLOT *hs = (HOST_SLOT *)slot;
  log_write(3, "Activating new host connection");
  slot->type = 4;
  s_host_slot = slot;
  write_active_file();
  perform_action("host_activate");
  if (!alloc_framebuffer(hs->fb_width, hs->fb_height)) {
    aio_close(1);
    return;
  }
  g_screen_info.width = hs->fb_width;
  g_screen_info.height = hs->fb_height;
  fbs_open_file(hs->fb_width, hs->fb_height);
  cur_slot = slot;
  reset_tight_streams();
  log_write(5, "Requesting full framebuffer update");
  request_update(0);
  aio_setread(rf_host_msg, ((void *)0), 1);
  aio_walk_slots(fn_host_pass_newfbsize, 1);
  cur_slot = saved_slot;
}

canonical_original_code:
static void host_really_activate(struct _AIO_SLOT *slot)
{
  struct _AIO_SLOT *saved_slot = cur_slot;
  struct _HOST_SLOT *hs = (HOST_SLOT *)slot;
  log_write(3, "Activating new host connection");
  slot->type = 4;
  s_host_slot = slot;
  write_active_file();
  perform_action("host_activate");
  if (!alloc_framebuffer(hs->fb_width, hs->fb_height)) {
    aio_close(1);
    return;
  }
  g_screen_info.width = hs->fb_width;
  g_screen_info.height = hs->fb_height;
  fbs_open_file(hs->fb_width, hs->fb_height);
  cur_slot = slot;
  reset_tight_streams();
  log_write(5, "Requesting full framebuffer update");
  request_update(0);
  aio_setread(rf_host_msg, ((void *)0), 1);
  aio_walk_slots(fn_host_pass_newfbsize, 1);
  cur_slot = saved_slot;
}


decompiled_code:
__int64 rf_host_fbupdate_hdr()
{
unsigned __int16 v0; // dx
__int64 v1; // rax
char v3; // [rsp+Ch] [rbp-Ch] BYREF
__int16 v4; // [rsp+Dh] [rbp-Bh]
char v5; // [rsp+Fh] [rbp-9h]

v0 = __ROL2__(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 1LL), 8);
rect_count = v0;
if ( v0 == 0xFFFF )
log_write(5LL, "Receiving framebuffer update");
else
log_write(5LL, "Receiving framebuffer update, %d rectangle(s)", v0);
v3 = 0;
v1 = *(_QWORD *)(cur_slot + 32LL);
v4 = *(_WORD *)v1;
v5 = *(_BYTE *)(v1 + 2);
fbs_spool_data(&v3, 4LL);
if ( rect_count )
return aio_setread(rf_host_fbupdate_recthdr, 0LL, 12LL);
log_write(6LL, "Requesting incremental framebuffer update");
request_update();
return aio_setread(rf_host_msg, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 func10()
{
unsigned __int16 v0; // dx
__int64 v1; // rax
char v3; // [rsp+Ch] [rbp-Ch] BYREF
__int16 v4; // [rsp+Dh] [rbp-Bh]
char v5; // [rsp+Fh] [rbp-9h]

v0 = __ROL2__(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 1LL), 8);
rect_count = v0;
if ( v0 == 0xFFFF )
log_write(5LL, "Receiving framebuffer update");
else
log_write(5LL, "Receiving framebuffer update, %d rectangle(s)", v0);
v3 = 0;
v1 = *(_QWORD *)(cur_slot + 32LL);
v4 = *(_WORD *)v1;
v5 = *(_BYTE *)(v1 + 2);
fbs_spool_data(&v3, 4LL);
if ( rect_count )
return aio_setread(rf_host_fbupdate_recthdr, 0LL, 12LL);
log_write(6LL, "Requesting incremental framebuffer update");
func8();
return aio_setread(rf_host_msg, 0LL, 1LL);
}

original_code:
static void rf_host_fbupdate_hdr(void)
{
  u_int8_t hdr_buf[4];
  rect_count = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[1])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[1])[1]);
  if (rect_count == 0xFFFF) {
    log_write(5, "Receiving framebuffer update");
  } else {
    log_write(5, "Receiving framebuffer update, %d rectangle(s)",
              rect_count);
  }
  hdr_buf[0] = 0;
  memcpy(&hdr_buf[1], cur_slot->readbuf, 3);
  fbs_spool_data(hdr_buf, 4);
  if (rect_count) {
    aio_setread(rf_host_fbupdate_recthdr, ((void *)0), 12);
  } else {
    log_write(6, "Requesting incremental framebuffer update");
    request_update(1);
    aio_setread(rf_host_msg, ((void *)0), 1);
  }
}

canonical_original_code:
static void rf_host_fbupdate_hdr()
{
  unsigned int hdr_buf[4];
  rect_count = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[1])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[1])[1]);
  if (rect_count == 0xFFFF) {
    log_write(5, "Receiving framebuffer update");
  } else {
    log_write(5, "Receiving framebuffer update, %d rectangle(s)",
              rect_count);
  }
  hdr_buf[0] = 0;
  memcpy(&hdr_buf[1], cur_slot->readbuf, 3);
  fbs_spool_data(hdr_buf, 4);
  if (rect_count) {
    aio_setread(rf_host_fbupdate_recthdr, ((void *)0), 12);
  } else {
    log_write(6, "Requesting incremental framebuffer update");
    request_update(1);
    aio_setread(rf_host_msg, ((void *)0), 1);
  }
}


decompiled_code:
__int64 reset_framebuffer()
{
__int64 v0; // rbx
int v2; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+4h] [rbp-14h]

v0 = cur_slot;
log_write(5LL, "Clearing framebuffer and cache");
memset(g_framebuffer, 0, 4LL * g_fb_height * g_fb_width);
v2 = 0;
LOWORD(v3) = g_fb_width;
HIWORD(v3) = g_fb_height;
invalidate_enc_cache(&v2);
v3 = *(_DWORD *)(v0 + 372);
return aio_walk_slots(fn_host_add_client_rect, 1LL);
}

canonical_decompiled_code:
__int64 func11()
{
__int64 v0; // rbx
int v2; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+4h] [rbp-14h]

v0 = cur_slot;
log_write(5LL, "Clearing framebuffer and cache");
memset(g_framebuffer, 0, 4LL * g_fb_height * g_fb_width);
v2 = 0;
LOWORD(v3) = g_fb_width;
HIWORD(v3) = g_fb_height;
invalidate_enc_cache(&v2);
v3 = *(_DWORD *)(v0 + 372);
return aio_walk_slots(fn_host_add_client_rect, 1LL);
}

original_code:
static void reset_framebuffer(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  FB_RECT r;
  log_write(5, "Clearing framebuffer and cache");
  memset(g_framebuffer, 0, g_fb_width * g_fb_height * sizeof(u_int32_t));
  r.x = r.y = 0;
  r.w = g_fb_width;
  r.h = g_fb_height;
  invalidate_enc_cache(&r);
  r.w = hs->fb_width;
  r.h = hs->fb_height;
  aio_walk_slots(fn_host_add_client_rect, 1);
}

canonical_original_code:
static void reset_framebuffer()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  struct _FB_RECT r;
  log_write(5, "Clearing framebuffer and cache");
  memset(g_framebuffer, 0, g_fb_width * g_fb_height * sizeof(u_int32_t));
  r.x = r.y = 0;
  r.w = g_fb_width;
  r.h = g_fb_height;
  invalidate_enc_cache(&r);
  r.w = hs->fb_width;
  r.h = hs->fb_height;
  aio_walk_slots(fn_host_add_client_rect, 1);
}

user_defined_types: struct _FB_RECT { unsigned int x; unsigned int y; unsigned int w; unsigned int h; unsigned int enc; unsigned int src_x; unsigned int src_y; }


decompiled_code:
__int64 host_activate()
{
if ( !s_host_slot )
return host_really_activate(cur_slot);
s_new_slot = cur_slot;
return aio_close_other(s_host_slot, 0LL);
}

canonical_decompiled_code:
__int64 func12()
{
if ( !s_host_slot )
return func9(cur_slot);
s_new_slot = cur_slot;
return aio_close_other(s_host_slot, 0LL);
}

original_code:
void host_activate(void)
{
  if (s_host_slot == ((void *)0)) {
    host_really_activate(cur_slot);
  } else {
    s_new_slot = cur_slot;
    aio_close_other(s_host_slot, 0);
  }
}

canonical_original_code:
void host_activate()
{
  if (s_host_slot == ((void *)0)) {
    host_really_activate(cur_slot);
  } else {
    s_new_slot = cur_slot;
    aio_close_other(s_host_slot, 0);
  }
}


decompiled_code:
__int64 host_close_hook()
{
__int64 v0; // rax
char v1; // dl
int v2; // edi
char *v3; // rax
__int64 result; // rax
int v5; // edi
char *v6; // rax

v0 = cur_slot;
if ( *cur_slot == 4 )
{
fbs_close_file();
reset_framebuffer();
v0 = cur_slot;
s_host_slot = 0LL;
}
v1 = *(_BYTE *)(v0 + 344);
if ( (v1 & 0x20) != 0 )
{
v2 = *(_DWORD *)(v0 + 348);
if ( v2 )
{
v3 = strerror(v2);
log_write(1LL, "Host I/O error, read: %s", v3);
}
else
{
log_write(1LL, "Host I/O error, read");
}
}
else if ( (v1 & 0x40) != 0 )
{
v5 = *(_DWORD *)(v0 + 348);
if ( v5 )
{
v6 = strerror(v5);
log_write(1LL, "Host I/O error, write: %s", v6);
if ( s_new_slot )
goto LABEL_7;
goto LABEL_11;
}
log_write(1LL, "Host I/O error, write");
}
else if ( (*(_BYTE *)(v0 + 344) & 0x10) != 0 )
{
log_write(1LL, "Host I/O error");
}
if ( s_new_slot )
{
LABEL_7:
log_write(4LL, "Closing previous connection to host");
result = host_really_activate(s_new_slot);
s_new_slot = 0LL;
return result;
}
LABEL_11:
log_write(2LL, "Closing connection to host");
if ( !g_framebuffer )
aio_close(1LL);
return remove_active_file();
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rax
char v1; // dl
int v2; // edi
char *v3; // rax
__int64 result; // rax
int v5; // edi
char *v6; // rax

v0 = cur_slot;
if ( *cur_slot == 4 )
{
fbs_close_file();
func11();
v0 = cur_slot;
s_host_slot = 0LL;
}
v1 = *(_BYTE *)(v0 + 344);
if ( (v1 & 0x20) != 0 )
{
v2 = *(_DWORD *)(v0 + 348);
if ( v2 )
{
v3 = strerror(v2);
log_write(1LL, "Host I/O error, read: %s", v3);
}
else
{
log_write(1LL, "Host I/O error, read");
}
}
else if ( (v1 & 0x40) != 0 )
{
v5 = *(_DWORD *)(v0 + 348);
if ( v5 )
{
v6 = strerror(v5);
log_write(1LL, "Host I/O error, write: %s", v6);
if ( s_new_slot )
goto LABEL_7;
goto LABEL_11;
}
log_write(1LL, "Host I/O error, write");
}
else if ( (*(_BYTE *)(v0 + 344) & 0x10) != 0 )
{
log_write(1LL, "Host I/O error");
}
if ( s_new_slot )
{
LABEL_7:
log_write(4LL, "Closing previous connection to host");
result = func9(s_new_slot);
s_new_slot = 0LL;
return result;
}
LABEL_11:
log_write(2LL, "Closing connection to host");
if ( !g_framebuffer )
aio_close(1LL);
return remove_active_file();
}

original_code:
void host_close_hook(void)
{
  if (cur_slot->type == 4) {
    fbs_close_file();
    reset_framebuffer();
    s_host_slot = ((void *)0);
  }
  if (cur_slot->errread_f) {
    if (cur_slot->io_errno) {
      log_write(1, "Host I/O error, read: %s",
                strerror(cur_slot->io_errno));
    } else {
      log_write(1, "Host I/O error, read");
    }
  } else if (cur_slot->errwrite_f) {
    if (cur_slot->io_errno) {
      log_write(1, "Host I/O error, write: %s",
                strerror(cur_slot->io_errno));
    } else {
      log_write(1, "Host I/O error, write");
    }
  } else if (cur_slot->errio_f) {
    log_write(1, "Host I/O error");
  }
  if (s_new_slot == ((void *)0)) {
    log_write(2, "Closing connection to host");
    if (g_framebuffer == ((void *)0))
      aio_close(1);
    remove_active_file();
  } else {
    log_write(4, "Closing previous connection to host");
    host_really_activate(s_new_slot);
    s_new_slot = ((void *)0);
  }
}

canonical_original_code:
void host_close_hook()
{
  if (cur_slot->type == 4) {
    fbs_close_file();
    reset_framebuffer();
    s_host_slot = ((void *)0);
  }
  if (cur_slot->errread_f) {
    if (cur_slot->io_errno) {
      log_write(1, "Host I/O error, read: %s",
                strerror(cur_slot->io_errno));
    } else {
      log_write(1, "Host I/O error, read");
    }
  } else if (cur_slot->errwrite_f) {
    if (cur_slot->io_errno) {
      log_write(1, "Host I/O error, write: %s",
                strerror(cur_slot->io_errno));
    } else {
      log_write(1, "Host I/O error, write");
    }
  } else if (cur_slot->errio_f) {
    log_write(1, "Host I/O error");
  }
  if (s_new_slot == ((void *)0)) {
    log_write(2, "Closing connection to host");
    if (g_framebuffer == ((void *)0))
      aio_close(1);
    remove_active_file();
  } else {
    log_write(4, "Closing previous connection to host");
    host_really_activate(s_new_slot);
    s_new_slot = ((void *)0);
  }
}


decompiled_code:
__int64 __fastcall fill_fb_rect(unsigned __int16 *a1, int a2)
{
__int64 result; // rax
char *v3; // r12
unsigned __int16 v4; // dx
char *v5; // rcx
int v6; // ebx
int v7; // eax

result = g_fb_width;
v3 = (char *)g_framebuffer + 4 * *a1 + 4 * g_fb_width * a1[1];
v4 = a1[2];
if ( v4 )
{
v5 = (char *)g_framebuffer + 4 * *a1 + 4 * g_fb_width * a1[1];
do
{
*(_DWORD *)v5 = a2;
v5 += 4;
}
while ( &v3[4 * v4] != v5 );
}
v6 = 1;
if ( a1[3] > 1u )
{
while ( 1 )
{
v7 = v6 * result;
++v6;
memcpy(&v3[4 * v7], v3, 4LL * v4);
result = a1[3];
if ( (int)result <= v6 )
break;
v4 = a1[2];
LODWORD(result) = g_fb_width;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned __int16 *a1, int a2)
{
__int64 result; // rax
char *v3; // r12
unsigned __int16 v4; // dx
char *v5; // rcx
int v6; // ebx
int v7; // eax

result = g_fb_width;
v3 = (char *)g_framebuffer + 4 * *a1 + 4 * g_fb_width * a1[1];
v4 = a1[2];
if ( v4 )
{
v5 = (char *)g_framebuffer + 4 * *a1 + 4 * g_fb_width * a1[1];
do
{
*(_DWORD *)v5 = a2;
v5 += 4;
}
while ( &v3[4 * v4] != v5 );
}
v6 = 1;
if ( a1[3] > 1u )
{
while ( 1 )
{
v7 = v6 * result;
++v6;
memcpy(&v3[4 * v7], v3, 4LL * v4);
result = a1[3];
if ( (int)result <= v6 )
break;
v4 = a1[2];
LODWORD(result) = g_fb_width;
}
}
return result;
}

original_code:
void fill_fb_rect(FB_RECT *r, u_int32_t color)
{
  int x, y;
  u_int32_t *fb_ptr;
  fb_ptr = &g_framebuffer[r->y * (int)g_fb_width + r->x];
  for (x = 0; x < r->w; x++)
    fb_ptr[x] = color;
  for (y = 1; y < r->h; y++)
    memcpy(&fb_ptr[y * g_fb_width], fb_ptr, r->w * sizeof(u_int32_t));
}

canonical_original_code:
void fill_fb_rect(struct _FB_RECT *r, unsigned int color)
{
  int x; int y;
  unsigned int *fb_ptr;
  fb_ptr = &g_framebuffer[r->y * (int)g_fb_width + r->x];
  for (x = 0; x < r->w; x++)
    fb_ptr[x] = color;
  for (y = 1; y < r->h; y++)
    memcpy(&fb_ptr[y * g_fb_width], fb_ptr, r->w * sizeof(u_int32_t));
}

variable_types:
color
unsigned int

variable_types: x int

variable_types: y int


decompiled_code:
__int64 fbupdate_rect_done()
{
if ( word_EA4 && word_EA6 )
{
log_write(6LL, "Received rectangle ok");
invalidate_enc_cache(&cur_rect);
fbs_flush_data();
aio_walk_slots(fn_host_add_client_rect, 1LL);
}
if ( --rect_count )
return aio_setread(rf_host_fbupdate_recthdr, 0LL, 12LL);
aio_walk_slots(&fn_client_send_rects, 1LL);
log_write(6LL, "Requesting incremental framebuffer update");
request_update();
return aio_setread(rf_host_msg, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 func15()
{
if ( word_EA4 && word_EA6 )
{
log_write(6LL, "Received rectangle ok");
invalidate_enc_cache(&cur_rect);
fbs_flush_data();
aio_walk_slots(fn_host_add_client_rect, 1LL);
}
if ( --rect_count )
return aio_setread(rf_host_fbupdate_recthdr, 0LL, 12LL);
aio_walk_slots(&fn_client_send_rects, 1LL);
log_write(6LL, "Requesting incremental framebuffer update");
func8();
return aio_setread(rf_host_msg, 0LL, 1LL);
}

original_code:
void fbupdate_rect_done(void)
{
  if (cur_rect.w != 0 && cur_rect.h != 0) {
    log_write(6, "Received rectangle ok");
    invalidate_enc_cache(&cur_rect);
    fbs_flush_data();
    aio_walk_slots(fn_host_add_client_rect, 1);
  }
  if (--rect_count) {
    aio_setread(rf_host_fbupdate_recthdr, ((void *)0), 12);
  } else {
    aio_walk_slots(fn_client_send_rects, 1);
    log_write(6, "Requesting incremental framebuffer update");
    request_update(1);
    aio_setread(rf_host_msg, ((void *)0), 1);
  }
}

canonical_original_code:
void fbupdate_rect_done()
{
  if (cur_rect.w != 0 && cur_rect.h != 0) {
    log_write(6, "Received rectangle ok");
    invalidate_enc_cache(&cur_rect);
    fbs_flush_data();
    aio_walk_slots(fn_host_add_client_rect, 1);
  }
  if (--rect_count) {
    aio_setread(rf_host_fbupdate_recthdr, ((void *)0), 12);
  } else {
    aio_walk_slots(fn_client_send_rects, 1);
    log_write(6, "Requesting incremental framebuffer update");
    request_update(1);
    aio_setread(rf_host_msg, ((void *)0), 1);
  }
}


decompiled_code:
__int64 rf_host_fbupdate_recthdr()
{
__int64 v0; // rbx
__int64 v1; // rdi
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rsi

v0 = cur_slot;
v1 = *(_QWORD *)(cur_slot + 32LL);
LOWORD(cur_rect) = __ROL2__(*(_WORD *)v1, 8);
HIWORD(cur_rect) = __ROL2__(*(_WORD *)(v1 + 2), 8);
word_EA4 = __ROL2__(*(_WORD *)(v1 + 4), 8);
word_EA6 = __ROL2__(*(_WORD *)(v1 + 6), 8);
dword_EA8 = _byteswap_ulong(*(_DWORD *)(v1 + 8));
fbs_spool_data(v1, 12LL);
if ( dword_EA8 == -224 )
{
log_write(6LL, "LastRect marker received from the host");
cur_rect = 0;
rect_count = 1;
return fbupdate_rect_done();
}
if ( !word_EA6 || !word_EA4 )
{
log_write(
2LL,
"Zero-size rectangle %dx%d at %d,%d (ignoring)",
(unsigned __int16)word_EA4,
(unsigned __int16)word_EA6,
(unsigned __int16)cur_rect,
HIWORD(cur_rect));
return fbupdate_rect_done();
}
if ( dword_EA8 == -223 )
{
log_write(4LL, "New host desktop geometry: %dx%d", (unsigned __int16)word_EA4, (unsigned __int16)word_EA6);
v3 = (unsigned __int16)word_EA4;
v4 = (unsigned __int16)word_EA6;
*(_WORD *)(v0 + 372) = word_EA4;
*(_WORD *)(v0 + 374) = v4;
g_screen_info = v3;
*((_WORD *)&g_screen_info + 1) = v4;
if ( !(unsigned int)alloc_framebuffer(v3, v4) )
return aio_close(1LL);
cur_rect = 0;
rect_count = 1;
return fbupdate_rect_done();
}
if ( (unsigned __int16)cur_rect >= g_fb_width
|| (unsigned __int16)cur_rect + (unsigned __int16)word_EA4 > g_fb_width
|| g_fb_height <= HIWORD(cur_rect)
|| HIWORD(cur_rect) + (unsigned __int16)word_EA6 > g_fb_height )
{
log_write(
1LL,
"Rectangle out of framebuffer bounds: %dx%d at %d,%d",
(unsigned __int16)word_EA4,
(unsigned __int16)word_EA6,
(unsigned __int16)cur_rect,
HIWORD(cur_rect));
return aio_close(0LL);
}
log_write(
6LL,
"Receiving rectangle %dx%d at %d,%d",
(unsigned __int16)word_EA4,
(unsigned __int16)word_EA6,
(unsigned __int16)cur_rect,
HIWORD(cur_rect));
if ( dword_EA8 == 5 )
{
log_write(6LL, "Receiving Hextile-encoded data");
result = setread_decode_hextile(&cur_rect);
}
else
{
if ( (unsigned int)dword_EA8 > 5 )
{
if ( dword_EA8 == 7 )
{
log_write(6LL, "Receiving Tight-encoded data");
return setread_decode_tight(&cur_rect);
}
}
else
{
if ( !dword_EA8 )
{
log_write(
6LL,
"Receiving raw data, expecting %d byte(s)",
4LL * (unsigned __int16)word_EA6 * (unsigned __int16)word_EA4);
rect_cur_row = 0;
return aio_setread(
rf_host_fbupdate_raw,
(char *)g_framebuffer + 4 * (unsigned __int16)cur_rect + 4 * g_fb_width * HIWORD(cur_rect),
4 * (unsigned int)(unsigned __int16)word_EA4);
}
if ( dword_EA8 == 1 )
{
log_write(6LL, "Receiving CopyRect instruction");
return aio_setread(rf_host_copyrect, 0LL, 4LL);
}
}
log_write(1LL, "Unknown encoding: 0x%08lX", (unsigned int)dword_EA8);
result = aio_close(0LL);
}
return result;
}

canonical_decompiled_code:
__int64 func16()
{
__int64 v0; // rbx
__int64 v1; // rdi
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rsi

v0 = cur_slot;
v1 = *(_QWORD *)(cur_slot + 32LL);
LOWORD(cur_rect) = __ROL2__(*(_WORD *)v1, 8);
HIWORD(cur_rect) = __ROL2__(*(_WORD *)(v1 + 2), 8);
word_EA4 = __ROL2__(*(_WORD *)(v1 + 4), 8);
word_EA6 = __ROL2__(*(_WORD *)(v1 + 6), 8);
dword_EA8 = _byteswap_ulong(*(_DWORD *)(v1 + 8));
fbs_spool_data(v1, 12LL);
if ( dword_EA8 == -224 )
{
log_write(6LL, "LastRect marker received from the host");
cur_rect = 0;
rect_count = 1;
return func15();
}
if ( !word_EA6 || !word_EA4 )
{
log_write(
2LL,
"Zero-size rectangle %dx%d at %d,%d (ignoring)",
(unsigned __int16)word_EA4,
(unsigned __int16)word_EA6,
(unsigned __int16)cur_rect,
HIWORD(cur_rect));
return func15();
}
if ( dword_EA8 == -223 )
{
log_write(4LL, "New host desktop geometry: %dx%d", (unsigned __int16)word_EA4, (unsigned __int16)word_EA6);
v3 = (unsigned __int16)word_EA4;
v4 = (unsigned __int16)word_EA6;
*(_WORD *)(v0 + 372) = word_EA4;
*(_WORD *)(v0 + 374) = v4;
g_screen_info = v3;
*((_WORD *)&g_screen_info + 1) = v4;
if ( !(unsigned int)alloc_framebuffer(v3, v4) )
return aio_close(1LL);
cur_rect = 0;
rect_count = 1;
return func15();
}
if ( (unsigned __int16)cur_rect >= g_fb_width
|| (unsigned __int16)cur_rect + (unsigned __int16)word_EA4 > g_fb_width
|| g_fb_height <= HIWORD(cur_rect)
|| HIWORD(cur_rect) + (unsigned __int16)word_EA6 > g_fb_height )
{
log_write(
1LL,
"Rectangle out of framebuffer bounds: %dx%d at %d,%d",
(unsigned __int16)word_EA4,
(unsigned __int16)word_EA6,
(unsigned __int16)cur_rect,
HIWORD(cur_rect));
return aio_close(0LL);
}
log_write(
6LL,
"Receiving rectangle %dx%d at %d,%d",
(unsigned __int16)word_EA4,
(unsigned __int16)word_EA6,
(unsigned __int16)cur_rect,
HIWORD(cur_rect));
if ( dword_EA8 == 5 )
{
log_write(6LL, "Receiving Hextile-encoded data");
result = setread_decode_hextile(&cur_rect);
}
else
{
if ( (unsigned int)dword_EA8 > 5 )
{
if ( dword_EA8 == 7 )
{
log_write(6LL, "Receiving Tight-encoded data");
return setread_decode_tight(&cur_rect);
}
}
else
{
if ( !dword_EA8 )
{
log_write(
6LL,
"Receiving raw data, expecting %d byte(s)",
4LL * (unsigned __int16)word_EA6 * (unsigned __int16)word_EA4);
rect_cur_row = 0;
return aio_setread(
rf_host_fbupdate_raw,
(char *)g_framebuffer + 4 * (unsigned __int16)cur_rect + 4 * g_fb_width * HIWORD(cur_rect),
4 * (unsigned int)(unsigned __int16)word_EA4);
}
if ( dword_EA8 == 1 )
{
log_write(6LL, "Receiving CopyRect instruction");
return aio_setread(rf_host_copyrect, 0LL, 4LL);
}
}
log_write(1LL, "Unknown encoding: 0x%08lX", (unsigned int)dword_EA8);
result = aio_close(0LL);
}
return result;
}

original_code:
static void rf_host_fbupdate_recthdr(void)
{
  HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  cur_rect.x = ((u_int16_t)((u_int8_t *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((u_int8_t *)cur_slot->readbuf)[1]);
  cur_rect.y = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[1]);
  cur_rect.w = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[4])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[4])[1]);
  cur_rect.h = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[6])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[6])[1]);
  cur_rect.enc = ((u_int32_t)((u_int8_t *)&cur_slot->readbuf[8])[0] << 24 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[8])[1] << 16 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[8])[2] << 8 | (u_int32_t)((u_int8_t *)&cur_slot->readbuf[8])[3]);;
  fbs_spool_data(cur_slot->readbuf, 12);
  if (cur_rect.enc == 0xFFFFFF20) {
    log_write(6, "LastRect marker received from the host");
    cur_rect.x = cur_rect.y = 0;
    rect_count = 1;
    fbupdate_rect_done();
    return;
  }
  if (cur_rect.h == 0 || cur_rect.w == 0) {
    log_write(2, "Zero-size rectangle %dx%d at %d,%d (ignoring)",
              (int)cur_rect.w, (int)cur_rect.h,
              (int)cur_rect.x, (int)cur_rect.y);
    fbupdate_rect_done();
    return;
  }
  if (cur_rect.enc == 0xFFFFFF21) {
    log_write(4, "New host desktop geometry: %dx%d",
              (int)cur_rect.w, (int)cur_rect.h);
    g_screen_info.width = hs->fb_width = cur_rect.w;
    g_screen_info.height = hs->fb_height = cur_rect.h;
    if (!alloc_framebuffer(hs->fb_width, hs->fb_height)) {
      aio_close(1);
      return;
    }
    cur_rect.x = cur_rect.y = 0;
    rect_count = 1;
    fbupdate_rect_done();
    return;
  }
  if (cur_rect.x >= g_fb_width || cur_rect.x + cur_rect.w > g_fb_width ||
      cur_rect.y >= g_fb_height || cur_rect.y + cur_rect.h > g_fb_height) {
    log_write(1, "Rectangle out of framebuffer bounds: %dx%d at %d,%d",
              (int)cur_rect.w, (int)cur_rect.h,
              (int)cur_rect.x, (int)cur_rect.y);
    aio_close(0);
    return;
  }
  log_write(6, "Receiving rectangle %dx%d at %d,%d",
            (int)cur_rect.w, (int)cur_rect.h,
            (int)cur_rect.x, (int)cur_rect.y);
  switch(cur_rect.enc) {
  case 0:
    log_write(6, "Receiving raw data, expecting %d byte(s)",
              cur_rect.w * cur_rect.h * sizeof(u_int32_t));
    rect_cur_row = 0;
    aio_setread(rf_host_fbupdate_raw,
                &g_framebuffer[cur_rect.y * (int)g_fb_width +
                               cur_rect.x],
                cur_rect.w * sizeof(u_int32_t));
    break;
  case 1:
    log_write(6, "Receiving CopyRect instruction");
    aio_setread(rf_host_copyrect, ((void *)0), 4);
    break;
  case 5:
    log_write(6, "Receiving Hextile-encoded data");
    setread_decode_hextile(&cur_rect);
    break;
  case 7:
    log_write(6, "Receiving Tight-encoded data");
    setread_decode_tight(&cur_rect);
    break;
  default:
    log_write(1, "Unknown encoding: 0x%08lX",
              (unsigned long)cur_rect.enc);
    aio_close(0);
  }
}

canonical_original_code:
static void rf_host_fbupdate_recthdr()
{
  struct _HOST_SLOT *hs = (HOST_SLOT *)cur_slot;
  cur_rect.x = ((u_int16_t)((unsigned int *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((unsigned int *)cur_slot->readbuf)[1]);
  cur_rect.y = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[1]);
  cur_rect.w = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[4])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[4])[1]);
  cur_rect.h = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[6])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[6])[1]);
  cur_rect.enc = ((u_int32_t)((unsigned int *)&cur_slot->readbuf[8])[0] << 24 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[8])[1] << 16 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[8])[2] << 8 | (u_int32_t)((unsigned int *)&cur_slot->readbuf[8])[3]);;
  fbs_spool_data(cur_slot->readbuf, 12);
  if (cur_rect.enc == 0xFFFFFF20) {
    log_write(6, "LastRect marker received from the host");
    cur_rect.x = cur_rect.y = 0;
    rect_count = 1;
    fbupdate_rect_done();
    return;
  }
  if (cur_rect.h == 0 || cur_rect.w == 0) {
    log_write(2, "Zero-size rectangle %dx%d at %d,%d (ignoring)",
              (int)cur_rect.w, (int)cur_rect.h,
              (int)cur_rect.x, (int)cur_rect.y);
    fbupdate_rect_done();
    return;
  }
  if (cur_rect.enc == 0xFFFFFF21) {
    log_write(4, "New host desktop geometry: %dx%d",
              (int)cur_rect.w, (int)cur_rect.h);
    g_screen_info.width = hs->fb_width = cur_rect.w;
    g_screen_info.height = hs->fb_height = cur_rect.h;
    if (!alloc_framebuffer(hs->fb_width, hs->fb_height)) {
      aio_close(1);
      return;
    }
    cur_rect.x = cur_rect.y = 0;
    rect_count = 1;
    fbupdate_rect_done();
    return;
  }
  if (cur_rect.x >= g_fb_width || cur_rect.x + cur_rect.w > g_fb_width ||
      cur_rect.y >= g_fb_height || cur_rect.y + cur_rect.h > g_fb_height) {
    log_write(1, "Rectangle out of framebuffer bounds: %dx%d at %d,%d",
              (int)cur_rect.w, (int)cur_rect.h,
              (int)cur_rect.x, (int)cur_rect.y);
    aio_close(0);
    return;
  }
  log_write(6, "Receiving rectangle %dx%d at %d,%d",
            (int)cur_rect.w, (int)cur_rect.h,
            (int)cur_rect.x, (int)cur_rect.y);
  switch(cur_rect.enc) {
  case 0:
    log_write(6, "Receiving raw data, expecting %d byte(s)",
              cur_rect.w * cur_rect.h * sizeof(u_int32_t));
    rect_cur_row = 0;
    aio_setread(rf_host_fbupdate_raw,
                &g_framebuffer[cur_rect.y * (int)g_fb_width +
                               cur_rect.x],
                cur_rect.w * sizeof(u_int32_t));
    break;
  case 1:
    log_write(6, "Receiving CopyRect instruction");
    aio_setread(rf_host_copyrect, ((void *)0), 4);
    break;
  case 5:
    log_write(6, "Receiving Hextile-encoded data");
    setread_decode_hextile(&cur_rect);
    break;
  case 7:
    log_write(6, "Receiving Tight-encoded data");
    setread_decode_tight(&cur_rect);
    break;
  default:
    log_write(1, "Unknown encoding: 0x%08lX",
              (unsigned long)cur_rect.enc);
    aio_close(0);
  }
}

variable_types: struct _HOST_SLOT { struct _AIO_SLOT { int type; int fd; int idx; char *name; void (*readfunc)(); unsigned char *readbuf; unsigned long bytes_to_read; unsigned long bytes_ready; unsigned char buf256[256]; struct _AIO_BLOCK { struct _AIO_BLOCK *next; void (*func)(); unsigned long data_size; unsigned char data[1]; } *outqueue; struct _AIO_BLOCK *outqueue_last; unsigned long bytes_written; void (*closefunc)(); unsigned int listening_f; unsigned int alloc_f; unsigned int close_f; unsigned int fd_closed_f; unsigned int errio_f; unsigned int errread_f; unsigned int errwrite_f; int io_errno; struct _AIO_SLOT *next; struct _AIO_SLOT *prev; } s; unsigned int temp_len; unsigned int fb_width; unsigned int fb_height; } *