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


repo: inovex/vnc-reflector

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

return_type: void


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

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:
__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()
{
__int16 v0; // si
int v1; // eax
__int16 v2; // dx
int v3; // ecx
__int16 v4; // dx
__int16 v6; // si
int v7; // eax
int v8; // edi
__int16 v9; // cx

v0 = s_tile;
v1 = (unsigned __int16)s_tile + 16;
v2 = WORD2(s_rect);
v3 = WORD2(s_rect) + (unsigned __int16)s_rect;
if ( v1 < v3 )
{
s_tile += 16;
v4 = s_rect + WORD2(s_rect) - (v0 + 16);
if ( v3 > (unsigned __int16)v1 + 16 )
v4 = 16;
word_8F4 = v4;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
v6 = word_8F2;
v7 = (unsigned __int16)word_8F2 + 16;
v8 = HIWORD(s_rect) + WORD1(s_rect);
if ( v7 < v8 )
{
s_tile = s_rect;
if ( WORD2(s_rect) > 0x10u )
v2 = 16;
word_8F4 = v2;
word_8F2 += 16;
v9 = HIWORD(s_rect) + WORD1(s_rect) - (v6 + 16);
if ( v8 > (unsigned __int16)v7 + 16 )
v9 = 16;
word_8F6 = v9;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
return fbupdate_rect_done();
}

canonical_decompiled_code:
__int64 func1()
{
__int16 v0; // si
int v1; // eax
__int16 v2; // dx
int v3; // ecx
__int16 v4; // dx
__int16 v6; // si
int v7; // eax
int v8; // edi
__int16 v9; // cx

v0 = s_tile;
v1 = (unsigned __int16)s_tile + 16;
v2 = WORD2(s_rect);
v3 = WORD2(s_rect) + (unsigned __int16)s_rect;
if ( v1 < v3 )
{
s_tile += 16;
v4 = s_rect + WORD2(s_rect) - (v0 + 16);
if ( v3 > (unsigned __int16)v1 + 16 )
v4 = 16;
word_8F4 = v4;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
v6 = word_8F2;
v7 = (unsigned __int16)word_8F2 + 16;
v8 = HIWORD(s_rect) + WORD1(s_rect);
if ( v7 < v8 )
{
s_tile = s_rect;
if ( WORD2(s_rect) > 0x10u )
v2 = 16;
word_8F4 = v2;
word_8F2 += 16;
v9 = HIWORD(s_rect) + WORD1(s_rect) - (v6 + 16);
if ( v8 > (unsigned __int16)v7 + 16 )
v9 = 16;
word_8F6 = v9;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}
return fbupdate_rect_done();
}

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

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_8F6 * (unsigned int)(unsigned __int16)word_8F4);
v1 = 4LL;
if ( (v0 & 2) == 0 )
{
fill_fb_rect(&s_tile, (unsigned int)s_bg, 4LL);
v1 = 0LL;
}
if ( (s_subenc & 4) != 0 )
{
v1 = (unsigned int)(v1 + 4);
if ( (s_subenc & 8) == 0 )
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
LABEL_6:
v1 = (unsigned int)(v1 + 1);
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
if ( (s_subenc & 8) != 0 )
goto LABEL_6;
if ( (_DWORD)v1 )
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 func2()
{
char v0; // al
__int64 v1; // rdx

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_8F6 * (unsigned int)(unsigned __int16)word_8F4);
v1 = 4LL;
if ( (v0 & 2) == 0 )
{
fill_fb_rect(&s_tile, (unsigned int)s_bg, 4LL);
v1 = 0LL;
}
if ( (s_subenc & 4) != 0 )
{
v1 = (unsigned int)(v1 + 4);
if ( (s_subenc & 8) == 0 )
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
LABEL_6:
v1 = (unsigned int)(v1 + 1);
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
}
if ( (s_subenc & 8) != 0 )
goto LABEL_6;
if ( (_DWORD)v1 )
return aio_setread(rf_host_hextile_hex, &hextile_buf, v1);
return func1();
}

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 __fastcall rf_host_hextile_hex(__int64 a1, __int64 a2, __int64 a3)
{
int *v3; // rbx
unsigned __int8 v4; // dl
__int64 v5; // rdx

v3 = &hextile_buf;
if ( (s_subenc & 2) != 0 )
{
s_bg = hextile_buf;
fill_fb_rect(&s_tile, (unsigned int)hextile_buf, a3);
v3 = (int *)&unk_4E4;
}
if ( (s_subenc & 4) != 0 )
s_fg = *v3++;
if ( (s_subenc & 8) != 0 )
{
fbs_spool_data(&hextile_buf, (char *)v3 - 1247);
v4 = *(_BYTE *)v3;
s_num_subrects = *(_BYTE *)v3;
if ( (s_subenc & 0x10) != 0 )
v5 = 6 * (unsigned int)v4;
else
v5 = 2 * (unsigned int)v4;
if ( (int)v5 > 0 )
return aio_setread(rf_host_hextile_subrects, 0LL, v5);
}
else
{
fbs_spool_data(&hextile_buf, (char *)v3 - (char *)&hextile_buf);
}
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
int *v3; // rbx
unsigned __int8 v4; // dl
__int64 v5; // rdx

v3 = &hextile_buf;
if ( (s_subenc & 2) != 0 )
{
s_bg = hextile_buf;
fill_fb_rect(&s_tile, (unsigned int)hextile_buf, a3);
v3 = (int *)&unk_4E4;
}
if ( (s_subenc & 4) != 0 )
s_fg = *v3++;
if ( (s_subenc & 8) != 0 )
{
fbs_spool_data(&hextile_buf, (char *)v3 - 1247);
v4 = *(_BYTE *)v3;
s_num_subrects = *(_BYTE *)v3;
if ( (s_subenc & 0x10) != 0 )
v5 = 6 * (unsigned int)v4;
else
v5 = 2 * (unsigned int)v4;
if ( (int)v5 > 0 )
return aio_setread(rf_host_hextile_subrects, 0LL, v5);
}
else
{
fbs_spool_data(&hextile_buf, (char *)v3 - (char *)&hextile_buf);
}
return func1();
}

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()
{
unsigned __int8 *v0; // rbx
unsigned __int8 *v1; // rbp
unsigned __int8 v2; // di
unsigned __int8 *v4; // rbp
unsigned __int8 v5; // di

v0 = *(unsigned __int8 **)(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[6 * (unsigned __int8)s_num_subrects];
do
{
s_fg = *(_DWORD *)v0;
v2 = v0[4];
v0 += 6;
hextile_fill_subrect(v2, *(v0 - 1));
}
while ( v1 != v0 );
}
}
else
{
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 2 * (unsigned __int8)s_num_subrects);
if ( s_num_subrects )
{
v4 = &v0[2 * (unsigned __int8)s_num_subrects];
do
{
v5 = *v0;
v0 += 2;
hextile_fill_subrect(v5, *(v0 - 1));
}
while ( v4 != v0 );
}
}
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 func4()
{
unsigned __int8 *v0; // rbx
unsigned __int8 *v1; // rbp
unsigned __int8 v2; // di
unsigned __int8 *v4; // rbp
unsigned __int8 v5; // di

v0 = *(unsigned __int8 **)(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[6 * (unsigned __int8)s_num_subrects];
do
{
s_fg = *(_DWORD *)v0;
v2 = v0[4];
v0 += 6;
func0(v2, *(v0 - 1));
}
while ( v1 != v0 );
}
}
else
{
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 2 * (unsigned __int8)s_num_subrects);
if ( s_num_subrects )
{
v4 = &v0[2 * (unsigned __int8)s_num_subrects];
do
{
v5 = *v0;
v0 += 2;
func0(v5, *(v0 - 1));
}
while ( v4 != v0 );
}
}
return func1();
}

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()
{
char *v0; // rbx
int *v1; // r13
int v2; // er12

fbs_spool_data(&hextile_buf, 4LL * (unsigned __int16)word_8F6 * (unsigned __int16)word_8F4);
v0 = (char *)(g_framebuffer + 4LL * ((unsigned __int16)s_tile + g_fb_width * (unsigned __int16)word_8F2));
if ( word_8F6 )
{
v1 = &hextile_buf;
v2 = 0;
do
{
memcpy(v0, v1, 4LL * (unsigned __int16)*(&s_tile + 2));
v1 += (unsigned __int16)*(&s_tile + 2);
v0 += 4 * g_fb_width;
++v2;
}
while ( (unsigned __int16)*(&s_tile + 3) > v2 );
}
return hextile_next_tile();
}

canonical_decompiled_code:
__int64 func5()
{
char *v0; // rbx
int *v1; // r13
int v2; // er12

fbs_spool_data(&hextile_buf, 4LL * (unsigned __int16)word_8F6 * (unsigned __int16)word_8F4);
v0 = (char *)(g_framebuffer + 4LL * ((unsigned __int16)s_tile + g_fb_width * (unsigned __int16)word_8F2));
if ( word_8F6 )
{
v1 = &hextile_buf;
v2 = 0;
do
{
memcpy(v0, v1, 4LL * (unsigned __int16)*(&s_tile + 2));
v1 += (unsigned __int16)*(&s_tile + 2);
v0 += 4 * g_fb_width;
++v2;
}
while ( (unsigned __int16)*(&s_tile + 3) > v2 );
}
return func1();
}

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(__int64 *a1)
{
__int64 v1; // rcx
__int16 v2; // ax
__int16 v3; // dx

v1 = a1[1];
s_rect = *a1;
qword_908 = v1;
s_tile = s_rect;
v2 = 16;
v3 = 16;
if ( WORD2(s_rect) <= 0x10u )
v3 = WORD2(s_rect);
word_8F4 = v3;
if ( HIWORD(s_rect) <= 0x10u )
v2 = HIWORD(s_rect);
word_8F6 = v2;
return aio_setread(rf_host_hextile_subenc, 0LL, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 *a1)
{
__int64 v1; // rcx
__int16 v2; // ax
__int16 v3; // dx

v1 = a1[1];
s_rect = *a1;
qword_908 = v1;
s_tile = s_rect;
v2 = 16;
v3 = 16;
if ( WORD2(s_rect) <= 0x10u )
v3 = WORD2(s_rect);
word_8F4 = v3;
if ( HIWORD(s_rect) <= 0x10u )
v2 = HIWORD(s_rect);
word_8F6 = v2;
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

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

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 = _byteswap_ushort(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 3LL));
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 = _byteswap_ushort(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 3LL));
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)
{
__int16 v2[4]; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+8h] [rbp-10h]

v3 = -223;
v2[1] = 0;
v2[0] = 0;
v2[2] = *(_WORD *)(cur_slot + 372LL);
v2[3] = *(_WORD *)(cur_slot + 374LL);
return fn_client_add_rect(a1, v2);
}

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

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

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


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()
{
unsigned __int8 v0; // dl
__int64 result; // rax

v0 = **(_BYTE **)(cur_slot + 32LL);
if ( 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 ( v0 > 2u )
{
if ( 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 ( 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()
{
unsigned __int8 v0; // dl
__int64 result; // rax

v0 = **(_BYTE **)(cur_slot + 32LL);
if ( 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 ( v0 > 2u )
{
if ( 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 ( 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 v2; // rbp
unsigned __int16 v3; // di
unsigned __int16 v4; // si
__int64 result; // rax

v2 = 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);
g_screen_info = *(_WORD *)(a1 + 372);
v3 = g_screen_info;
v4 = *(_WORD *)(a1 + 374);
*((_WORD *)&g_screen_info + 1) = v4;
fbs_open_file(v3, v4);
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 = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 v2; // rbp
unsigned __int16 v3; // di
unsigned __int16 v4; // si
__int64 result; // rax

v2 = 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);
g_screen_info = *(_WORD *)(a1 + 372);
v3 = g_screen_info;
v4 = *(_WORD *)(a1 + 374);
*((_WORD *)&g_screen_info + 1) = v4;
fbs_open_file(v3, v4);
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 = v2;
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 = _byteswap_ushort(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 1LL));
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 = _byteswap_ushort(*(_WORD *)(*(_QWORD *)(cur_slot + 32LL) + 1LL));
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
__int16 v2[2]; // [rsp+0h] [rbp-18h] BYREF
__int16 v3; // [rsp+4h] [rbp-14h]
__int16 v4; // [rsp+6h] [rbp-12h]

v0 = cur_slot;
log_write(5LL, "Clearing framebuffer and cache");
memset(g_framebuffer, 0, 4LL * g_fb_height * g_fb_width);
v2[1] = 0;
v2[0] = 0;
v3 = g_fb_width;
v4 = g_fb_height;
invalidate_enc_cache(v2);
v3 = *(_WORD *)(v0 + 372);
v4 = *(_WORD *)(v0 + 374);
return aio_walk_slots(fn_host_add_client_rect, 1LL);
}

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

v0 = cur_slot;
log_write(5LL, "Clearing framebuffer and cache");
memset(g_framebuffer, 0, 4LL * g_fb_height * g_fb_width);
v2[1] = 0;
v2[0] = 0;
v3 = g_fb_width;
v4 = g_fb_height;
invalidate_enc_cache(v2);
v3 = *(_WORD *)(v0 + 372);
v4 = *(_WORD *)(v0 + 374);
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()
{
char v0; // al
int v1; // edi
char *v2; // rax
__int64 result; // rax
int v4; // edi
char *v5; // rax

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

canonical_decompiled_code:
__int64 func13()
{
char v0; // al
int v1; // edi
char *v2; // rax
__int64 result; // rax
int v4; // edi
char *v5; // rax

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

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
int v4; // ebx

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

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

result = *a1 + g_fb_width * a1[1];
v3 = (char *)g_framebuffer + 4 * result;
if ( a1[2] )
{
result = 0LL;
do
*(_DWORD *)&v3[4 * result++] = a2;
while ( a1[2] > (int)result );
}
if ( a1[3] > 1u )
{
v4 = 1;
do
{
memcpy(&v3[4 * v4 * g_fb_width], v3, 4LL * a1[2]);
++v4;
result = a1[3];
}
while ( (int)result > v4 );
}
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_F04 && word_F06 )
{
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_F04 && word_F06 )
{
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
unsigned __int16 v3; // di
unsigned __int16 v4; // si

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

canonical_decompiled_code:
__int64 func16()
{
__int64 v0; // rbx
__int64 v1; // rdi
__int64 result; // rax
unsigned __int16 v3; // di
unsigned __int16 v4; // si

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


decompiled_code:
__int64 rf_host_copyrect()
{
unsigned __int16 v0; // bx
unsigned __int16 *v1; // rax
unsigned __int16 v2; // r8
unsigned __int16 v3; // r9
int v4; // edx
char *v5; // rbp
char *v6; // r12
__int64 v7; // rbx
int v8; // er13
__int64 result; // rax
char *v10; // rbp
char *v11; // r12
__int64 v12; // rbx
int v13; // er13

v0 = g_fb_width;
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 4LL);
v1 = *(unsigned __int16 **)(cur_slot + 32LL);
v2 = _byteswap_ushort(*v1);
word_F0C = v2;
v3 = _byteswap_ushort(v1[1]);
word_F0E = v3;
if ( v2 >= g_fb_width
|| v2 + (unsigned __int16)word_F04 > g_fb_width
|| v3 >= g_fb_height
|| (v4 = v3 + (unsigned __int16)word_F06, v4 > g_fb_height) )
{
log_write(
1LL,
"CopyRect from outside of the framebuffer: %dx%d from %d,%d",
(unsigned __int16)word_F04,
(unsigned __int16)word_F06,
v2,
v3);
result = aio_close(0LL);
}
else
{
if ( v3 <= (unsigned __int16)word_F02 )
{
v10 = (char *)g_framebuffer + 4 * v0 * (v4 - 1) + 4 * v2;
v11 = (char *)g_framebuffer
+ 4 * (unsigned __int16)cur_rect
+ 4 * ((unsigned __int16)word_F06 + (unsigned __int16)word_F02 - 1) * v0;
if ( word_F06 )
{
v12 = -4LL * v0;
v13 = 0;
do
{
memmove(v11, v10, 4LL * (unsigned __int16)*(&cur_rect + 2));
v10 += v12;
v11 += v12;
++v13;
}
while ( (unsigned __int16)*(&cur_rect + 3) > v13 );
}
}
else
{
v5 = (char *)g_framebuffer + 4 * v0 * v3 + 4 * v2;
v6 = (char *)g_framebuffer + 4 * (unsigned __int16)cur_rect + 4 * (unsigned __int16)word_F02 * v0;
if ( word_F06 )
{
v7 = 4LL * v0;
v8 = 0;
do
{
memmove(v6, v5, 4LL * (unsigned __int16)*(&cur_rect + 2));
v5 += v7;
v6 += v7;
++v8;
}
while ( (unsigned __int16)*(&cur_rect + 3) > v8 );
}
}
result = fbupdate_rect_done();
}
return result;
}

canonical_decompiled_code:
__int64 func17()
{
unsigned __int16 v0; // bx
unsigned __int16 *v1; // rax
unsigned __int16 v2; // r8
unsigned __int16 v3; // r9
int v4; // edx
char *v5; // rbp
char *v6; // r12
__int64 v7; // rbx
int v8; // er13
__int64 result; // rax
char *v10; // rbp
char *v11; // r12
__int64 v12; // rbx
int v13; // er13

v0 = g_fb_width;
fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 4LL);
v1 = *(unsigned __int16 **)(cur_slot + 32LL);
v2 = _byteswap_ushort(*v1);
word_F0C = v2;
v3 = _byteswap_ushort(v1[1]);
word_F0E = v3;
if ( v2 >= g_fb_width
|| v2 + (unsigned __int16)word_F04 > g_fb_width
|| v3 >= g_fb_height
|| (v4 = v3 + (unsigned __int16)word_F06, v4 > g_fb_height) )
{
log_write(
1LL,
"CopyRect from outside of the framebuffer: %dx%d from %d,%d",
(unsigned __int16)word_F04,
(unsigned __int16)word_F06,
v2,
v3);
result = aio_close(0LL);
}
else
{
if ( v3 <= (unsigned __int16)word_F02 )
{
v10 = (char *)g_framebuffer + 4 * v0 * (v4 - 1) + 4 * v2;
v11 = (char *)g_framebuffer
+ 4 * (unsigned __int16)cur_rect
+ 4 * ((unsigned __int16)word_F06 + (unsigned __int16)word_F02 - 1) * v0;
if ( word_F06 )
{
v12 = -4LL * v0;
v13 = 0;
do
{
memmove(v11, v10, 4LL * (unsigned __int16)*(&cur_rect + 2));
v10 += v12;
v11 += v12;
++v13;
}
while ( (unsigned __int16)*(&cur_rect + 3) > v13 );
}
}
else
{
v5 = (char *)g_framebuffer + 4 * v0 * v3 + 4 * v2;
v6 = (char *)g_framebuffer + 4 * (unsigned __int16)cur_rect + 4 * (unsigned __int16)word_F02 * v0;
if ( word_F06 )
{
v7 = 4LL * v0;
v8 = 0;
do
{
memmove(v6, v5, 4LL * (unsigned __int16)*(&cur_rect + 2));
v5 += v7;
v6 += v7;
++v8;
}
while ( (unsigned __int16)*(&cur_rect + 3) > v8 );
}
}
result = func15();
}
return result;
}

original_code:
static void rf_host_copyrect(void)
{
  u_int32_t *src_ptr;
  u_int32_t *dst_ptr;
  int width = (int)g_fb_width;
  int row;
  fbs_spool_data(cur_slot->readbuf, 4);
  cur_rect.src_x = ((u_int16_t)((u_int8_t *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((u_int8_t *)cur_slot->readbuf)[1]);
  cur_rect.src_y = ((u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((u_int8_t *)&cur_slot->readbuf[2])[1]);
  if ( cur_rect.src_x >= g_fb_width ||
       cur_rect.src_x + cur_rect.w > g_fb_width ||
       cur_rect.src_y >= g_fb_height ||
       cur_rect.src_y + cur_rect.h > g_fb_height ) {
    log_write(1,
              "CopyRect from outside of the framebuffer: %dx%d from %d,%d",
              (int)cur_rect.w, (int)cur_rect.h,
              (int)cur_rect.src_x, (int)cur_rect.src_y);
    aio_close(0);
    return;
  }
  if (cur_rect.src_y > cur_rect.y) {
    src_ptr = &g_framebuffer[cur_rect.src_y * width + cur_rect.src_x];
    dst_ptr = &g_framebuffer[cur_rect.y * width + cur_rect.x];
    for (row = 0; row < cur_rect.h; row++) {
      memmove(dst_ptr, src_ptr, cur_rect.w * sizeof(u_int32_t));
      src_ptr += width;
      dst_ptr += width;
    }
  } else {
    src_ptr = &g_framebuffer[(cur_rect.src_y + cur_rect.h - 1) * width +
                             cur_rect.src_x];
    dst_ptr = &g_framebuffer[(cur_rect.y + cur_rect.h - 1) * width +
                             cur_rect.x];
    for (row = 0; row < cur_rect.h; row++) {
      memmove(dst_ptr, src_ptr, cur_rect.w * sizeof(u_int32_t));
      src_ptr -= width;
      dst_ptr -= width;
    }
  }
  fbupdate_rect_done();
}

canonical_original_code:
static void rf_host_copyrect()
{
  unsigned int *src_ptr;
  unsigned int *dst_ptr;
  int width = (int)g_fb_width;
  int row;
  fbs_spool_data(cur_slot->readbuf, 4);
  cur_rect.src_x = ((u_int16_t)((unsigned int *)cur_slot->readbuf)[0] << 8 | (u_int16_t)((unsigned int *)cur_slot->readbuf)[1]);
  cur_rect.src_y = ((u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[0] << 8 | (u_int16_t)((unsigned int *)&cur_slot->readbuf[2])[1]);
  if ( cur_rect.src_x >= g_fb_width ||
       cur_rect.src_x + cur_rect.w > g_fb_width ||
       cur_rect.src_y >= g_fb_height ||
       cur_rect.src_y + cur_rect.h > g_fb_height ) {
    log_write(1,
              "CopyRect from outside of the framebuffer: %dx%d from %d,%d",
              (int)cur_rect.w, (int)cur_rect.h,
              (int)cur_rect.src_x, (int)cur_rect.src_y);
    aio_close(0);
    return;
  }
  if (cur_rect.src_y > cur_rect.y) {
    src_ptr = &g_framebuffer[cur_rect.src_y * width + cur_rect.src_x];
    dst_ptr = &g_framebuffer[cur_rect.y * width + cur_rect.x];
    for (row = 0; row < cur_rect.h; row++) {
      memmove(dst_ptr, src_ptr, cur_rect.w * sizeof(u_int32_t));
      src_ptr += width;
      dst_ptr += width;
    }
  } else {
    src_ptr = &g_framebuffer[(cur_rect.src_y + cur_rect.h - 1) * width +
                             cur_rect.src_x];
    dst_ptr = &g_framebuffer[(cur_rect.y + cur_rect.h - 1) * width +
                             cur_rect.x];
    for (row = 0; row < cur_rect.h; row++) {
      memmove(dst_ptr, src_ptr, cur_rect.w * sizeof(u_int32_t));
      src_ptr -= width;
      dst_ptr -= width;
    }
  }
  fbupdate_rect_done();
}

variable_types: width int


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

fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 4LL * (unsigned __int16)word_F04);
v0 = rect_cur_row + 1;
rect_cur_row = v0;
if ( v0 >= (unsigned __int16)word_F06 )
result = fbupdate_rect_done();
else
result = aio_setread(
rf_host_fbupdate_raw,
(char *)g_framebuffer
+ 4 * (unsigned __int16)cur_rect
+ 4 * g_fb_width * ((unsigned __int16)word_F02 + v0),
4 * (unsigned int)(unsigned __int16)word_F04);
return result;
}

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

fbs_spool_data(*(_QWORD *)(cur_slot + 32LL), 4LL * (unsigned __int16)word_F04);
v0 = rect_cur_row + 1;
rect_cur_row = v0;
if ( v0 >= (unsigned __int16)word_F06 )
result = func15();
else
result = aio_setread(
rf_host_fbupdate_raw,
(char *)g_framebuffer
+ 4 * (unsigned __int16)cur_rect
+ 4 * g_fb_width * ((unsigned __int16)word_F02 + v0),
4 * (unsigned int)(unsigned __int16)word_F04);
return result;
}

original_code:
static void rf_host_fbupdate_raw(void)
{
  int idx;
  fbs_spool_data(cur_slot->readbuf, cur_rect.w * sizeof(u_int32_t));
  if (++rect_cur_row < cur_rect.h) {
    idx = (cur_rect.y + rect_cur_row) * (int)g_fb_width + cur_rect.x;
    aio_setread(rf_host_fbupdate_raw, &g_framebuffer[idx],
                cur_rect.w * sizeof(u_int32_t));
  } else {
    fbupdate_rect_done();
  }
}

canonical_original_code:
static void rf_host_fbupdate_raw()
{
  int idx;
  fbs_spool_data(cur_slot->readbuf, cur_rect.w * sizeof(u_int32_t));
  if (++rect_cur_row < cur_rect.h) {
    idx = (cur_rect.y + rect_cur_row) * (int)g_fb_width + cur_rect.x;
    aio_setread(rf_host_fbupdate_raw, &g_framebuffer[idx],
                cur_rect.w * sizeof(u_int32_t));
  } else {
    fbupdate_rect_done();
  }
}

variable_types: idx int


decompiled_code:
int pass_msg_to_host()
{
int result; // eax
__int64 v1; // rbx

result = s_host_slot;
if ( s_host_slot )
{
v1 = cur_slot;
cur_slot = s_host_slot;
result = aio_write(0LL);
cur_slot = v1;
}
return result;
}

canonical_decompiled_code:
int func19()
{
int result; // eax
__int64 v1; // rbx

result = s_host_slot;
if ( s_host_slot )
{
v1 = cur_slot;
cur_slot = s_host_slot;
result = aio_write(0LL);
cur_slot = v1;
}
return result;
}

original_code:
void pass_msg_to_host(u_int8_t *msg, size_t len)
{
  AIO_SLOT *saved_slot = cur_slot;
  if (s_host_slot != ((void *)0)) {
    cur_slot = s_host_slot;
    aio_write(((void *)0), msg, len);
    cur_slot = saved_slot;
  }
}

canonical_original_code:
void pass_msg_to_host(unsigned int *msg, unsigned long len)
{
  struct _AIO_SLOT *saved_slot = cur_slot;
  if (s_host_slot != ((void *)0)) {
    cur_slot = s_host_slot;
    aio_write(((void *)0), msg, len);
    cur_slot = saved_slot;
  }
}

variable_types:
msg
unsigned int *

variable_types:
len
unsigned long


decompiled_code:
int pass_cuttext_to_host()
{
__int64 v0; // r12
int result; // eax

v0 = cur_slot;
result = s_host_slot;
if ( s_host_slot )
{
cur_slot = s_host_slot;
aio_write(0LL);
result = aio_write(0LL);
cur_slot = v0;
}
return result;
}

canonical_decompiled_code:
int func20()
{
__int64 v0; // r12
int result; // eax

v0 = cur_slot;
result = s_host_slot;
if ( s_host_slot )
{
cur_slot = s_host_slot;
aio_write(0LL);
result = aio_write(0LL);
cur_slot = v0;
}
return result;
}

original_code:
void pass_cuttext_to_host(u_int8_t *text, size_t len)
{
  AIO_SLOT *saved_slot = cur_slot;
  u_int8_t client_cuttext_hdr[8] = {
    6, 0, 0, 0, 0, 0, 0, 0
  };
  if (s_host_slot != ((void *)0)) {
    { ((u_int8_t *)&client_cuttext_hdr[4])[0] = (u_int8_t)((u_int32_t)len >> 24); ((u_int8_t *)&client_cuttext_hdr[4])[1] = (u_int8_t)((u_int32_t)len >> 16); ((u_int8_t *)&client_cuttext_hdr[4])[2] = (u_int8_t)((u_int32_t)len >> 8); ((u_int8_t *)&client_cuttext_hdr[4])[3] = (u_int8_t)(u_int32_t)len; };
    cur_slot = s_host_slot;
    aio_write(((void *)0), client_cuttext_hdr, sizeof(client_cuttext_hdr));
    aio_write(((void *)0), text, len);
    cur_slot = saved_slot;
  }
}

canonical_original_code:
void pass_cuttext_to_host(unsigned int *text, unsigned long len)
{
  struct _AIO_SLOT *saved_slot = cur_slot;
  unsigned int client_cuttext_hdr[8] = {
    6, 0, 0, 0, 0, 0, 0, 0
  };
  if (s_host_slot != ((void *)0)) {
    { ((unsigned int *)&client_cuttext_hdr[4])[0] = (u_int8_t)((unsigned int)len >> 24); ((unsigned int *)&client_cuttext_hdr[4])[1] = (u_int8_t)((unsigned int)len >> 16); ((unsigned int *)&client_cuttext_hdr[4])[2] = (u_int8_t)((unsigned int)len >> 8); ((unsigned int *)&client_cuttext_hdr[4])[3] = (unsigned int)(unsigned int)len; };
    cur_slot = s_host_slot;
    aio_write(((void *)0), client_cuttext_hdr, sizeof(client_cuttext_hdr));
    aio_write(((void *)0), text, len);
    cur_slot = saved_slot;
  }
}

variable_types:
text
unsigned int *

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 set_active_file(char *src)
{
__int64 result; // rax
size_t v3; // rax
char *v4; // rdi

result = 0LL;
if ( src )
{
v3 = strlen(src);
v4 = (char *)malloc(v3 + 1);
active_file = v4;
if ( v4 )
{
strcpy(v4, src);
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src)
{
__int64 result; // rax
size_t v3; // rax
char *v4; // rdi

result = 0LL;
if ( src )
{
v3 = strlen(src);
v4 = (char *)malloc(v3 + 1);
active_file = v4;
if ( v4 )
{
strcpy(v4, src);
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int set_active_file(char *file_path)
{
  if (file_path == ((void *)0))
    return 0;
  active_file = malloc(strlen(file_path) + 1);
  if (active_file == ((void *)0))
    return 0;
  strcpy(active_file, file_path);
  return 1;
}

return_type: int


decompiled_code:
__int64 remove_active_file()
{
const char *v0; // rbx
__int64 result; // rax

v0 = active_file;
result = 1LL;
if ( active_file )
{
if ( unlink(active_file) )
{
log_write(2LL, "Error removing active file marker: %s", v0);
result = 0LL;
}
else
{
log_write(6LL, "Removed active file marker", v0);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
const char *v0; // rbx
__int64 result; // rax

v0 = active_file;
result = 1LL;
if ( active_file )
{
if ( unlink(active_file) )
{
log_write(2LL, "Error removing active file marker: %s", v0);
result = 0LL;
}
else
{
log_write(6LL, "Removed active file marker", v0);
result = 1LL;
}
}
return result;
}

original_code:
int remove_active_file(void)
{
  if (active_file != ((void *)0)) {
    if (unlink(active_file) != 0) {
      log_write(2, "Error removing active file marker: %s", active_file);
      return 0;
    }
    log_write(6, "Removed active file marker", active_file);
  }
  return 1;
}

canonical_original_code:
int remove_active_file()
{
  if (active_file != ((void *)0)) {
    if (unlink(active_file) != 0) {
      log_write(2, "Error removing active file marker: %s", active_file);
      return 0;
    }
    log_write(6, "Removed active file marker", active_file);
  }
  return 1;
}


decompiled_code:
__int64 __fastcall set_actions_file(char *src)
{
__int64 result; // rax
size_t v3; // rax
char *v4; // rdi

result = 0LL;
if ( src )
{
v3 = strlen(src);
v4 = (char *)malloc(v3 + 1);
actions_file = v4;
if ( v4 )
{
strcpy(v4, src);
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src)
{
__int64 result; // rax
size_t v3; // rax
char *v4; // rdi

result = 0LL;
if ( src )
{
v3 = strlen(src);
v4 = (char *)malloc(v3 + 1);
actions_file = v4;
if ( v4 )
{
strcpy(v4, src);
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int set_actions_file(char *file_path)
{
  if (file_path == ((void *)0))
    return 0;
  actions_file = malloc(strlen(file_path) + 1);
  if (actions_file == ((void *)0))
    return 0;
  strcpy(actions_file, file_path);
  return 1;
}


decompiled_code:
__int64 __fastcall perform_action(char *a1)
{
unsigned int v1; // ebx
int v2; // eax
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

v1 = 1;
if ( actions_file )
{
v1 = read_action(a1, v4, 1024);
if ( v1 )
{
if ( v4[0] )
{
v2 = system(v4);
if ( v2 == -1 || v2 == 32512 )
{
log_write(2LL, "Failed to execute action command");
v1 = 0;
}
else
{
if ( BYTE1(v2) )
log_write(4LL, "Action command exited with status %d", BYTE1(v2));
else
log_write(5LL, "Action command done");
v1 = 1;
}
}
else
{
log_write(5LL, "Missing action for `%s'", a1);
v1 = 0;
}
}
else
{
log_write(2LL, "Failed to read actions file: %s", actions_file);
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
unsigned int v1; // ebx
int v2; // eax
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

v1 = 1;
if ( actions_file )
{
v1 = func0(a1, v4, 1024);
if ( v1 )
{
if ( v4[0] )
{
v2 = system(v4);
if ( v2 == -1 || v2 == 32512 )
{
log_write(2LL, "Failed to execute action command");
v1 = 0;
}
else
{
if ( BYTE1(v2) )
log_write(4LL, "Action command exited with status %d", BYTE1(v2));
else
log_write(5LL, "Action command done");
v1 = 1;
}
}
else
{
log_write(5LL, "Missing action for `%s'", a1);
v1 = 0;
}
}
else
{
log_write(2LL, "Failed to read actions file: %s", actions_file);
}
}
return v1;
}

original_code:
int perform_action(char *action_str)
{
  char cmd[1024];
  int status;
  if (actions_file == ((void *)0))
    return 1;
  if (!read_action(action_str, cmd, 1024)) {
    log_write(2, "Failed to read actions file: %s", actions_file);
    return 0;
  }
  if (cmd[0] == '\0') {
    log_write(5, "Missing action for `%s'", action_str);
    return 0;
  }
  status = system(cmd);
  if (status == -1 || status == 0x7F00) {
    log_write(2, "Failed to execute action command");
    return 0;
  }
  status = status >> 8 & 0xFF;
  if (status == 0) {
    log_write(5, "Action command done");
  } else {
    log_write(4, "Action command exited with status %d", status);
  }
  return 1;
}

variable_types:
cmd
char [1024]

variable_types: status int


original_code:
void buf_get_pixfmt(void *buf, RFB_PIXEL_FORMAT *format)
{
  u_int8_t *bbuf = buf;
  memcpy(format, buf, 16);
  format->r_max = ((u_int16_t)((u_int8_t *)&bbuf[4])[0] << 8 | (u_int16_t)((u_int8_t *)&bbuf[4])[1]);
  format->g_max = ((u_int16_t)((u_int8_t *)&bbuf[6])[0] << 8 | (u_int16_t)((u_int8_t *)&bbuf[6])[1]);
  format->b_max = ((u_int16_t)((u_int8_t *)&bbuf[8])[0] << 8 | (u_int16_t)((u_int8_t *)&bbuf[8])[1]);
}

canonical_original_code:
void buf_get_pixfmt(void *buf, struct _RFB_PIXEL_FORMAT *format)
{
  unsigned int *bbuf = buf;
  memcpy(format, buf, 16);
  format->r_max = ((u_int16_t)((unsigned int *)&bbuf[4])[0] << 8 | (u_int16_t)((unsigned int *)&bbuf[4])[1]);
  format->g_max = ((u_int16_t)((unsigned int *)&bbuf[6])[0] << 8 | (u_int16_t)((unsigned int *)&bbuf[6])[1]);
  format->b_max = ((u_int16_t)((unsigned int *)&bbuf[8])[0] << 8 | (u_int16_t)((unsigned int *)&bbuf[8])[1]);
}

variable_types:
buf
void *

variable_types: struct _RFB_PIXEL_FORMAT { unsigned int bits_pixel; unsigned int color_depth; unsigned int big_endian; unsigned int true_color; unsigned int r_max; unsigned int g_max; unsigned int b_max; unsigned int r_shift; unsigned int g_shift; unsigned int b_shift; unsigned int unused[3]; } *

variable_types:
bbuf
unsigned int *


original_code:
void buf_put_pixfmt(void *buf, RFB_PIXEL_FORMAT *format)
{
  u_int8_t *bbuf = buf;
  memcpy(buf, format, 16);
  { ((u_int8_t *)&bbuf[4])[0] = (u_int8_t)(format->r_max >> 8); ((u_int8_t *)&bbuf[4])[1] = (u_int8_t)format->r_max; };
  { ((u_int8_t *)&bbuf[6])[0] = (u_int8_t)(format->g_max >> 8); ((u_int8_t *)&bbuf[6])[1] = (u_int8_t)format->g_max; };
  { ((u_int8_t *)&bbuf[8])[0] = (u_int8_t)(format->b_max >> 8); ((u_int8_t *)&bbuf[8])[1] = (u_int8_t)format->b_max; };
}

canonical_original_code:
void buf_put_pixfmt(void *buf, struct _RFB_PIXEL_FORMAT *format)
{
  unsigned int *bbuf = buf;
  memcpy(buf, format, 16);
  { ((unsigned int *)&bbuf[4])[0] = (u_int8_t)(format->r_max >> 8); ((unsigned int *)&bbuf[4])[1] = (unsigned int)format->r_max; };
  { ((unsigned int *)&bbuf[6])[0] = (u_int8_t)(format->g_max >> 8); ((unsigned int *)&bbuf[6])[1] = (unsigned int)format->g_max; };
  { ((unsigned int *)&bbuf[8])[0] = (u_int8_t)(format->b_max >> 8); ((unsigned int *)&bbuf[8])[1] = (unsigned int)format->b_max; };
}

user_defined_types: struct _RFB_PIXEL_FORMAT { unsigned int bits_pixel; unsigned int color_depth; unsigned int big_endian; unsigned int true_color; unsigned int r_max; unsigned int g_max; unsigned int b_max; unsigned int r_shift; unsigned int g_shift; unsigned int b_shift; unsigned int unused[3]; }


decompiled_code:
__int64 __fastcall rfb_gen_challenge(_BYTE *a1)
{
_BYTE *v1; // rbx
unsigned int v2; // eax
__int64 result; // rax

v1 = a1;
v2 = time(0LL);
srandom(v2);
do
{
result = random();
*v1++ = result;
}
while ( a1 + 16 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1)
{
_BYTE *v1; // rbx
unsigned int v2; // eax
__int64 result; // rax

v1 = a1;
v2 = time(0LL);
srandom(v2);
do
{
result = random();
*v1++ = result;
}
while ( a1 + 16 != v1 );
return result;
}

original_code:
void rfb_gen_challenge(u_int8_t *buf)
{
  int i;
  srandom((unsigned int)time(((void *)0)));
  for (i = 0; i < 16; i++)
    *buf++ = (unsigned char)random();
}

canonical_original_code:
void rfb_gen_challenge(unsigned int *buf)
{
  int i;
  srandom((unsigned int)time(((void *)0)));
  for (i = 0; i < 16; i++)
    *buf++ = (unsigned char)random();
}

variable_types:
buf
unsigned int *

variable_types: i int


decompiled_code:
__int64 __fastcall rfb_crypt(__int64 a1, __int64 a2, const char *a3)
{
char dest[32]; // [rsp+8h] [rbp-20h] BYREF

strncpy(dest, a3, 8uLL);
deskey(dest, 0LL);
des(a2, a1);
return des(a2 + 8, a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, const char *a3)
{
char dest[32]; // [rsp+8h] [rbp-20h] BYREF

strncpy(dest, a3, 8uLL);
deskey(dest, 0LL);
des(a2, a1);
return des(a2 + 8, a1 + 8);
}

original_code:
void rfb_crypt(u_int8_t *dst_buf, u_int8_t *src_buf, unsigned char *password)
{
  unsigned char key[8];
  memset(key, 0, 8);
  strncpy((char *)key, (char *)password, 8);
  deskey(key, 0);
  des(src_buf, dst_buf);
  des(src_buf + 8, dst_buf + 8);
}

canonical_original_code:
void rfb_crypt(unsigned int *dst_buf, unsigned int *src_buf, unsigned char *password)
{
  unsigned char key[8];
  memset(key, 0, 8);
  strncpy((char *)key, (char *)password, 8);
  deskey(key, 0);
  des(src_buf, dst_buf);
  des(src_buf + 8, dst_buf + 8);
}

variable_types:
password
unsigned char *

variable_types:
key
unsigned char [8]


decompiled_code:
void __fastcall fbs_set_prefix(char *a1, int a2)
{
s_fbs_prefix = a1;
s_join_sessions = a2;
s_fbs_idx = 0;
}

canonical_decompiled_code:
void __fastcall func0(char *a1, int a2)
{
s_fbs_prefix = a1;
s_join_sessions = a2;
s_fbs_idx = 0;
}

original_code:
void fbs_set_prefix(char *fbs_prefix, int join_sessions)
{
  s_fbs_prefix = fbs_prefix;
  s_join_sessions = join_sessions;
  s_fbs_idx = 0;
}


decompiled_code:
void fbs_close_file()
{
if ( s_fbs_fp )
{
if ( fclose(s_fbs_fp) )
log_write(2LL, "Could not close FBS file");
s_fbs_fp = 0LL;
free(s_fbs_buffer);
}
}

canonical_decompiled_code:
void func2()
{
if ( s_fbs_fp )
{
if ( fclose(s_fbs_fp) )
log_write(2LL, "Could not close FBS file");
s_fbs_fp = 0LL;
free(s_fbs_buffer);
}
}

original_code:
void fbs_close_file(void)
{
  if (s_fbs_fp != ((void *)0)) {
    if (fclose(s_fbs_fp) != 0)
      log_write(2, "Could not close FBS file");
    s_fbs_fp = ((void *)0);
    free(s_fbs_buffer);
  }
}

canonical_original_code:
void fbs_close_file()
{
  if (s_fbs_fp != ((void *)0)) {
    if (fclose(s_fbs_fp) != 0)
      log_write(2, "Could not close FBS file");
    s_fbs_fp = ((void *)0);
    free(s_fbs_buffer);
  }
}


decompiled_code:
void *fbs_flush_data()
{
void *result; // rax

if ( s_fbs_fp )
{
fbs_write_data(s_fbs_buffer, (_BYTE *)s_fbs_buffer_ptr - (_BYTE *)s_fbs_buffer);
result = s_fbs_buffer;
s_fbs_buffer_ptr = s_fbs_buffer;
}
return result;
}

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

if ( s_fbs_fp )
{
func3(s_fbs_buffer, (_BYTE *)s_fbs_buffer_ptr - (_BYTE *)s_fbs_buffer);
result = s_fbs_buffer;
s_fbs_buffer_ptr = s_fbs_buffer;
}
return result;
}

original_code:
void fbs_flush_data(void)
{
  if (s_fbs_fp != ((void *)0)) {
    fbs_write_data(s_fbs_buffer, s_fbs_buffer_ptr - s_fbs_buffer);
    s_fbs_buffer_ptr = s_fbs_buffer;
  }
}

canonical_original_code:
void fbs_flush_data()
{
  if (s_fbs_fp != ((void *)0)) {
    fbs_write_data(s_fbs_buffer, s_fbs_buffer_ptr - s_fbs_buffer);
    s_fbs_buffer_ptr = s_fbs_buffer;
  }
}