decompiled_code:
__int64 __fastcall server_msg_dispatch(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rbp
int v4; // eax
__int64 v6; // [rsp+8h] [rbp-1D80h] BYREF
unsigned int v7; // [rsp+10h] [rbp-1D78h] BYREF
unsigned __int16 v8; // [rsp+14h] [rbp-1D74h]
int v9; // [rsp+18h] [rbp-1D70h]
unsigned int v10; // [rsp+20h] [rbp-1D68h]
const __m128i *v11; // [rsp+28h] [rbp-1D60h]
__int128 v12[8]; // [rsp+30h] [rbp-1D58h] BYREF
char s[1024]; // [rsp+B0h] [rbp-1CD8h] BYREF
char v14[2064]; // [rsp+4B0h] [rbp-18D8h] BYREF
char v15[4296]; // [rsp+CC0h] [rbp-10C8h] BYREF

v1 = imsg_read();
if ( v1 != -1 && v1 != 0 )
{
while ( 1 )
{
v2 = imsg_get(a1, &v7);
if ( v2 == -1 )
break;
if ( !v2 )
return 0LL;
if ( v9 == 1 )
{
v3 = v8 - 16LL;
log_debug("got %d from client %d", v7, *(unsigned int *)(a1 + 65592));
switch ( v7 )
{
case 0u:
if ( v3 != 2060 )
goto LABEL_30;
qmemcpy(v14, v11, 0x80CuLL);
server_msg_command(a1, (__int64)v14);
break;
case 5u:
if ( v3 )
goto LABEL_31;
*(_QWORD *)(a1 + 66328) = 0LL;
tty_close(a1 + 65624);
server_write_client(a1, 4LL, 0LL, 0LL);
break;
case 6u:
if ( v3 != 4268 )
goto LABEL_32;
qmemcpy(v15, v11, 0x10ACuLL);
server_msg_identify(a1, (__int64)v15, v10);
break;
case 9u:
if ( v3 != 8 )
goto LABEL_33;
v6 = v11->m128i_i64[0];
server_msg_resize(a1, (int *)&v6);
break;
case 0xCu:
if ( v3 != 128 )
goto LABEL_34;
v12[0] = (__int128)_mm_loadu_si128(v11);
v12[1] = (__int128)_mm_loadu_si128(v11 + 1);
v12[2] = (__int128)_mm_loadu_si128(v11 + 2);
v12[3] = (__int128)_mm_loadu_si128(v11 + 3);
v12[4] = (__int128)_mm_loadu_si128(v11 + 4);
v12[5] = (__int128)_mm_loadu_si128(v11 + 5);
v12[6] = (__int128)_mm_loadu_si128(v11 + 6);
v12[7] = (__int128)_mm_loadu_si128(v11 + 7);
HIBYTE(v12[7]) = 0;
v4 = server_unlock(v12);
if ( v4 == -2 )
{
server_write_error(a1, "too many bad passwords, sleeping");
}
else if ( v4 == -1 )
{
server_write_error(a1, "bad password");
}
memset(v12, 0, sizeof(v12));
server_write_client(a1, 3LL, 0LL, 0LL);
break;
case 0xEu:
if ( v3 )
goto LABEL_35;
*(_DWORD *)(a1 + 66184) &= 0xFFFFFFBF;
tty_start_tty(a1 + 65624);
server_redraw_client(a1);
break;
case 0xFu:
if ( v3 != 1024 )
goto LABEL_36;
qmemcpy(s, v11, sizeof(s));
s[1023] = 0;
if ( strchr(s, 61) )
environ_put(a1 + 65600, s);
break;
default:
log_fatalx("%s: %s", "server_msg_dispatch", "unexpected message");
LABEL_30:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_COMMAND size");
LABEL_31:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_EXITING size");
LABEL_32:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_IDENTIFY size");
LABEL_33:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_RESIZE size");
LABEL_34:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_UNLOCK size");
LABEL_35:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_WAKEUP size");
LABEL_36:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_ENVIRON size");
JUMPOUT(0x950LL);
}
imsg_free(&v7);
}
else
{
server_write_client(a1, 13LL, 0LL, 0LL);
*(_DWORD *)(a1 + 66184) |= 0x80u;
imsg_free(&v7);
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rbp
int v4; // eax
__int64 v6; // [rsp+8h] [rbp-1D80h] BYREF
unsigned int v7; // [rsp+10h] [rbp-1D78h] BYREF
unsigned __int16 v8; // [rsp+14h] [rbp-1D74h]
int v9; // [rsp+18h] [rbp-1D70h]
unsigned int v10; // [rsp+20h] [rbp-1D68h]
const __m128i *v11; // [rsp+28h] [rbp-1D60h]
__int128 v12[8]; // [rsp+30h] [rbp-1D58h] BYREF
char s[1024]; // [rsp+B0h] [rbp-1CD8h] BYREF
char v14[2064]; // [rsp+4B0h] [rbp-18D8h] BYREF
char v15[4296]; // [rsp+CC0h] [rbp-10C8h] BYREF

v1 = imsg_read();
if ( v1 != -1 && v1 != 0 )
{
while ( 1 )
{
v2 = imsg_get(a1, &v7);
if ( v2 == -1 )
break;
if ( !v2 )
return 0LL;
if ( v9 == 1 )
{
v3 = v8 - 16LL;
log_debug("got %d from client %d", v7, *(unsigned int *)(a1 + 65592));
switch ( v7 )
{
case 0u:
if ( v3 != 2060 )
goto LABEL_30;
qmemcpy(v14, v11, 0x80CuLL);
func3(a1, (__int64)v14);
break;
case 5u:
if ( v3 )
goto LABEL_31;
*(_QWORD *)(a1 + 66328) = 0LL;
tty_close(a1 + 65624);
server_write_client(a1, 4LL, 0LL, 0LL);
break;
case 6u:
if ( v3 != 4268 )
goto LABEL_32;
qmemcpy(v15, v11, 0x10ACuLL);
func4(a1, (__int64)v15, v10);
break;
case 9u:
if ( v3 != 8 )
goto LABEL_33;
v6 = v11->m128i_i64[0];
func5(a1, (int *)&v6);
break;
case 0xCu:
if ( v3 != 128 )
goto LABEL_34;
v12[0] = (__int128)_mm_loadu_si128(v11);
v12[1] = (__int128)_mm_loadu_si128(v11 + 1);
v12[2] = (__int128)_mm_loadu_si128(v11 + 2);
v12[3] = (__int128)_mm_loadu_si128(v11 + 3);
v12[4] = (__int128)_mm_loadu_si128(v11 + 4);
v12[5] = (__int128)_mm_loadu_si128(v11 + 5);
v12[6] = (__int128)_mm_loadu_si128(v11 + 6);
v12[7] = (__int128)_mm_loadu_si128(v11 + 7);
HIBYTE(v12[7]) = 0;
v4 = server_unlock(v12);
if ( v4 == -2 )
{
server_write_error(a1, "too many bad passwords, sleeping");
}
else if ( v4 == -1 )
{
server_write_error(a1, "bad password");
}
memset(v12, 0, sizeof(v12));
server_write_client(a1, 3LL, 0LL, 0LL);
break;
case 0xEu:
if ( v3 )
goto LABEL_35;
*(_DWORD *)(a1 + 66184) &= 0xFFFFFFBF;
tty_start_tty(a1 + 65624);
server_redraw_client(a1);
break;
case 0xFu:
if ( v3 != 1024 )
goto LABEL_36;
qmemcpy(s, v11, sizeof(s));
s[1023] = 0;
if ( strchr(s, 61) )
environ_put(a1 + 65600, s);
break;
default:
log_fatalx("%s: %s", "server_msg_dispatch", "unexpected message");
LABEL_30:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_COMMAND size");
LABEL_31:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_EXITING size");
LABEL_32:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_IDENTIFY size");
LABEL_33:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_RESIZE size");
LABEL_34:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_UNLOCK size");
LABEL_35:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_WAKEUP size");
LABEL_36:
log_fatalx("%s: %s", "server_msg_dispatch", "bad MSG_ENVIRON size");
JUMPOUT(0x950LL);
}
imsg_free(&v7);
}
else
{
server_write_client(a1, 13LL, 0LL, 0LL);
*(_DWORD *)(a1 + 66184) |= 0x80u;
imsg_free(&v7);
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int
server_msg_dispatch(struct client *c)
{
 struct imsg imsg;
 struct msg_command_data commanddata;
 struct msg_identify_data identifydata;
 struct msg_resize_data resizedata;
 struct msg_unlock_data unlockdata;
 struct msg_environ_data environdata;
 ssize_t n, datalen;
        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                return (-1);
 for (;;) {
  if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
   return (-1);
  if (n == 0)
   return (0);
  datalen = imsg.hdr.len - sizeof(struct imsg_hdr);
  if (imsg.hdr.peerid != 1) {
   server_write_client(c, MSG_VERSION, ((void *)0), 0);
   c->flags |= 0x80;
   imsg_free(&imsg);
   continue;
  }
  log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
  switch (imsg.hdr.type) {
  case MSG_COMMAND:
   if (datalen != sizeof commanddata)
    log_fatalx("%s: %s", __func__, "bad MSG_COMMAND size");;
   memcpy(&commanddata, imsg.data, sizeof commanddata);
   server_msg_command(c, &commanddata);
   break;
  case MSG_IDENTIFY:
   if (datalen != sizeof identifydata)
    log_fatalx("%s: %s", __func__, "bad MSG_IDENTIFY size");;
   memcpy(&identifydata, imsg.data, sizeof identifydata);
   server_msg_identify(c, &identifydata, imsg.fd);
   break;
  case MSG_RESIZE:
   if (datalen != sizeof resizedata)
    log_fatalx("%s: %s", __func__, "bad MSG_RESIZE size");;
   memcpy(&resizedata, imsg.data, sizeof resizedata);
   server_msg_resize(c, &resizedata);
   break;
  case MSG_EXITING:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_EXITING size");;
   c->session = ((void *)0);
   tty_close(&c->tty);
   server_write_client(c, MSG_EXITED, ((void *)0), 0);
   break;
  case MSG_UNLOCK:
   if (datalen != sizeof unlockdata)
    log_fatalx("%s: %s", __func__, "bad MSG_UNLOCK size");;
   memcpy(&unlockdata, imsg.data, sizeof unlockdata);
   unlockdata.pass[(sizeof unlockdata.pass) - 1] = '\0';
   switch (server_unlock(unlockdata.pass)) {
   case -1:
    server_write_error(c, "bad password");
    break;
   case -2:
    server_write_error(c,
        "too many bad passwords, sleeping");
    break;
   }
   memset(&unlockdata, 0, sizeof unlockdata);
   server_write_client(c, MSG_EXIT, ((void *)0), 0);
   break;
  case MSG_WAKEUP:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_WAKEUP size");;
   c->flags &= ~0x40;
   tty_start_tty(&c->tty);
   server_redraw_client(c);
   break;
  case MSG_ENVIRON:
   if (datalen != sizeof environdata)
    log_fatalx("%s: %s", __func__, "bad MSG_ENVIRON size");;
   memcpy(&environdata, imsg.data, sizeof environdata);
   environdata.var[(sizeof environdata.var) - 1] = '\0';
   if (strchr(environdata.var, '=') != ((void *)0))
    environ_put(&c->environ, environdata.var);
   break;
  default:
   log_fatalx("%s: %s", __func__, "unexpected message");;
  }
  imsg_free(&imsg);
 }
}

canonical_original_code:
int server_msg_dispatch(struct client *c)
{
 struct imsg imsg;
 struct msg_command_data commanddata;
 struct msg_identify_data identifydata;
 struct msg_resize_data resizedata;
 struct msg_unlock_data unlockdata;
 struct msg_environ_data environdata;
 long n; long datalen;
        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                return (-1);
 for (;;) {
  if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
   return (-1);
  if (n == 0)
   return (0);
  datalen = imsg.hdr.len - sizeof(struct imsg_hdr);
  if (imsg.hdr.peerid != 1) {
   server_write_client(c, MSG_VERSION, ((void *)0), 0);
   c->flags |= 0x80;
   imsg_free(&imsg);
   continue;
  }
  log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
  switch (imsg.hdr.type) {
  case MSG_COMMAND:
   if (datalen != sizeof commanddata)
    log_fatalx("%s: %s", __func__, "bad MSG_COMMAND size");;
   memcpy(&commanddata, imsg.data, sizeof commanddata);
   server_msg_command(c, &commanddata);
   break;
  case MSG_IDENTIFY:
   if (datalen != sizeof identifydata)
    log_fatalx("%s: %s", __func__, "bad MSG_IDENTIFY size");;
   memcpy(&identifydata, imsg.data, sizeof identifydata);
   server_msg_identify(c, &identifydata, imsg.fd);
   break;
  case MSG_RESIZE:
   if (datalen != sizeof resizedata)
    log_fatalx("%s: %s", __func__, "bad MSG_RESIZE size");;
   memcpy(&resizedata, imsg.data, sizeof resizedata);
   server_msg_resize(c, &resizedata);
   break;
  case MSG_EXITING:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_EXITING size");;
   c->session = ((void *)0);
   tty_close(&c->tty);
   server_write_client(c, MSG_EXITED, ((void *)0), 0);
   break;
  case MSG_UNLOCK:
   if (datalen != sizeof unlockdata)
    log_fatalx("%s: %s", __func__, "bad MSG_UNLOCK size");;
   memcpy(&unlockdata, imsg.data, sizeof unlockdata);
   unlockdata.pass[(sizeof unlockdata.pass) - 1] = '\0';
   switch (server_unlock(unlockdata.pass)) {
   case -1:
    server_write_error(c, "bad password");
    break;
   case -2:
    server_write_error(c,
        "too many bad passwords, sleeping");
    break;
   }
   memset(&unlockdata, 0, sizeof unlockdata);
   server_write_client(c, MSG_EXIT, ((void *)0), 0);
   break;
  case MSG_WAKEUP:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_WAKEUP size");;
   c->flags &= ~0x40;
   tty_start_tty(&c->tty);
   server_redraw_client(c);
   break;
  case MSG_ENVIRON:
   if (datalen != sizeof environdata)
    log_fatalx("%s: %s", __func__, "bad MSG_ENVIRON size");;
   memcpy(&environdata, imsg.data, sizeof environdata);
   environdata.var[(sizeof environdata.var) - 1] = '\0';
   if (strchr(environdata.var, '=') != ((void *)0))
    environ_put(&c->environ, environdata.var);
   break;
  default:
   log_fatalx("%s: %s", __func__, "unexpected message");;
  }
  imsg_free(&imsg);
 }
}

variable_types: struct imsg { struct imsg_hdr { unsigned int type; unsigned int len; unsigned int flags; unsigned int peerid; unsigned int pid; } hdr; int fd; void *data; }

variable_types: struct msg_command_data { int pid; unsigned int idx; int argc; char argv[2048]; }

variable_types: struct msg_identify_data { char tty[32]; char cwd[4096]; char term[128]; int flags; unsigned int sx; unsigned int sy; }

variable_types: struct msg_resize_data { unsigned int sx; unsigned int sy; }

variable_types: struct msg_unlock_data { char pass[128]; }

variable_types: struct msg_environ_data { char var[1024]; }

variable_types: n long

variable_types: datalen long

return_type: int

user_defined_types: struct imsg { struct imsg_hdr hdr; int fd; void *data; }

user_defined_types: struct client { struct imsgbuf ibuf; struct environ environ; char *title; char *cwd; struct tty tty; struct timeval status_timer; struct timeval repeat_timer; struct screen status; int flags; struct timeval identify_timer; char *message_string; struct timeval message_timer; char *prompt_string; char *prompt_buffer; unsigned long prompt_index; int (*prompt_callbackfn)(void *, char *); void (*prompt_freefn)(void *); void *prompt_data; int prompt_flags; unsigned int prompt_hindex; struct { char **list; unsigned int num; unsigned long space; } prompt_hdata; struct mode_key_data prompt_mdata; struct session *session; int references; }

user_defined_types: struct imsgbuf { struct { struct imsg_fd *tqh_first; struct imsg_fd **tqh_last; } fds; struct buf_read r; struct msgbuf w; int fd; int pid; }

user_defined_types: struct environ { struct environ_entry *rbh_root; }

user_defined_types: struct tty { char *path; unsigned int sx; unsigned int sy; unsigned int cx; unsigned int cy; int mode; unsigned int rlower; unsigned int rupper; char *termname; struct tty_term *term; int fd; struct buffer *in; struct buffer *out; int log_fd; struct termios tio; struct grid_cell cell; unsigned char acs[-1]; int flags; int term_flags; struct timeval key_timer; unsigned long ksize; struct tty_keys ktree; }

user_defined_types: struct screen { char *title; struct grid *grid; unsigned int cx; unsigned int cy; unsigned int rupper; unsigned int rlower; int mode; unsigned char *tabs; struct screen_sel sel; }

user_defined_types: struct mode_key_data { struct mode_key_tree *tree; int mode; }

user_defined_types: struct buf_read { unsigned char buf[65535]; unsigned char *rptr; unsigned long wpos; }

user_defined_types: struct msgbuf { struct { struct buf *tqh_first; struct buf **tqh_last; } bufs; unsigned int queued; int fd; }

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

user_defined_types: struct grid_cell { unsigned char attr; unsigned char flags; unsigned char fg; unsigned char bg; unsigned char data; }

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

user_defined_types: struct tty_keys { struct tty_key *rbh_root; }

user_defined_types: struct screen_sel { int flag; unsigned int sx; unsigned int sy; unsigned int ex; unsigned int ey; struct grid_cell cell; }

user_defined_types: struct session { char *name; struct timeval tv; unsigned int sx; unsigned int sy; struct winlink *curw; struct winlink_stack lastw; struct winlinks windows; struct options options; struct paste_stack buffers; struct { struct session_alert *slh_first; } alerts; int flags; struct termios *tio; struct environ environ; int references; }

user_defined_types: struct environ_entry { char *name; char *value; struct { struct environ_entry *rbe_left; struct environ_entry *rbe_right; struct environ_entry *rbe_parent; int rbe_color; } entry; }

user_defined_types: struct tty_term { char *name; unsigned int references; struct tty_code codes[-1]; int flags; struct { struct tty_term *sle_next; } entry; }

user_defined_types: struct buffer { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; }

user_defined_types: struct grid { int flags; unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; struct grid_line *linedata; }

user_defined_types: struct mode_key_tree { struct mode_key_binding *sph_root; }

user_defined_types: struct winlink_stack { struct winlink *slh_first; }

user_defined_types: struct paste_stack { struct paste_buffer **list; unsigned int num; unsigned long space; }

user_defined_types: struct winlink { int idx; struct window *window; struct { struct winlink *rbe_left; struct winlink *rbe_right; struct winlink *rbe_parent; int rbe_color; } entry; struct { struct winlink *sle_next; } sentry; }

user_defined_types: struct tty_code { enum tty_code_type type; union { char *string; int number; int flag; } value; }

user_defined_types: struct grid_line { unsigned int cellsize; struct grid_cell *celldata; unsigned int utf8size; struct grid_utf8 *utf8data; int flags; }

user_defined_types: struct mode_key_binding { int key; int mode; enum mode_key_cmd cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; }

user_defined_types: struct window { char *name; struct timeval name_timer; struct window_pane *active; struct window_panes panes; int lastlayout; struct layout_cell *layout_root; unsigned int sx; unsigned int sy; int flags; struct options options; unsigned int references; }

user_defined_types: struct options { struct options_tree tree; struct options *parent; }

user_defined_types: struct window_pane { struct window *window; struct layout_cell *layout_cell; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; int flags; char *cmd; char *shell; char *cwd; int pid; int fd; char tty[32]; struct buffer *in; struct buffer *out; struct input_ctx ictx; struct screen *screen; struct screen base; unsigned int saved_cx; unsigned int saved_cy; struct grid *saved_grid; struct grid_cell saved_cell; struct window_mode *mode; void *modedata; struct { struct window_pane *tqe_next; struct window_pane **tqe_prev; } entry; }

user_defined_types: struct options_tree { struct options_entry *sph_root; }

user_defined_types: struct input_ctx { struct window_pane *wp; struct screen_write_ctx ctx; unsigned char *buf; unsigned long len; unsigned long off; unsigned long was; struct grid_cell cell; struct grid_cell saved_cell; unsigned int saved_cx; unsigned int saved_cy; unsigned char *string_buf; unsigned long string_len; int string_type; unsigned char utf8_buf[4]; unsigned int utf8_len; unsigned int utf8_off; unsigned char intermediate; void *(*state)(unsigned char, struct input_ctx *); unsigned char private; struct { struct input_arg *list; unsigned int num; unsigned long space; } args; }

user_defined_types: struct layout_cell { enum layout_type type; struct layout_cell *parent; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; struct window_pane *wp; struct layout_cells cells; struct { struct layout_cell *tqe_next; struct layout_cell **tqe_prev; } entry; }


repo: ThomasAdam/tmux-ARCHIVED

decompiled_code:
char *__fastcall client_fill_session(_DWORD *a1)
{
char *result; // rax
char *v2; // rbx
char *v3; // rsi
unsigned __int64 v4; // rsi
char *v5; // r12
char *v6; // rbx
__int64 v7; // [rsp+8h] [rbp-120h] BYREF
char dest[280]; // [rsp+10h] [rbp-118h] BYREF

*a1 = -1;
result = getenv("TMUX");
if ( !result )
return result;
v2 = result;
result = strrchr(result, 44);
if ( v2 == result || !result )
return result;
v3 = result - 1;
if ( v2 < result - 1 )
{
while ( *v3 != 44 )
{
if ( v2 == --v3 )
goto LABEL_10;
}
goto LABEL_8;
}
v2 = result - 1;
LABEL_10:
if ( *v2 == 44 )
{
v3 = v2;
LABEL_8:
v4 = (unsigned __int64)(v3 + 1);
v5 = result + 1;
result -= v4;
v6 = result;
if ( (unsigned __int64)result <= 0xFF )
{
memcpy(dest, (const void *)v4, (size_t)result);
dest[(_QWORD)v6] = 0;
result = (char *)strtonum(dest, 0LL, 0x7FFFFFFFFFFFFFFFLL, &v7);
if ( !v7 )
{
*a1 = (_DWORD)result;
result = (char *)strtonum(v5, 0LL, 0xFFFFFFFFLL, &v7);
if ( !v7 )
a1[1] = (_DWORD)result;
}
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(_DWORD *a1)
{
char *result; // rax
char *v2; // rbx
char *v3; // rsi
unsigned __int64 v4; // rsi
char *v5; // r12
char *v6; // rbx
__int64 v7; // [rsp+8h] [rbp-120h] BYREF
char dest[280]; // [rsp+10h] [rbp-118h] BYREF

*a1 = -1;
result = getenv("TMUX");
if ( !result )
return result;
v2 = result;
result = strrchr(result, 44);
if ( v2 == result || !result )
return result;
v3 = result - 1;
if ( v2 < result - 1 )
{
while ( *v3 != 44 )
{
if ( v2 == --v3 )
goto LABEL_10;
}
goto LABEL_8;
}
v2 = result - 1;
LABEL_10:
if ( *v2 == 44 )
{
v3 = v2;
LABEL_8:
v4 = (unsigned __int64)(v3 + 1);
v5 = result + 1;
result -= v4;
v6 = result;
if ( (unsigned __int64)result <= 0xFF )
{
memcpy(dest, (const void *)v4, (size_t)result);
dest[(_QWORD)v6] = 0;
result = (char *)strtonum(dest, 0LL, 0x7FFFFFFFFFFFFFFFLL, &v7);
if ( !v7 )
{
*a1 = (_DWORD)result;
result = (char *)strtonum(v5, 0LL, 0xFFFFFFFFLL, &v7);
if ( !v7 )
a1[1] = (_DWORD)result;
}
}
}
return result;
}

original_code:
void
client_fill_session(struct msg_command_data *data)
{
 char *env, *ptr1, *ptr2, buf[256];
 size_t len;
 const char *errstr;
 long long ll;
 data->pid = -1;
 if ((env = getenv("TMUX")) == ((void *)0))
  return;
 if ((ptr2 = strrchr(env, ',')) == ((void *)0) || ptr2 == env)
  return;
 for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--)
  ;
 if (*ptr1 != ',')
  return;
 ptr1++;
 ptr2++;
 len = ptr2 - ptr1 - 1;
 if (len > (sizeof buf) - 1)
  return;
 memcpy(buf, ptr1, len);
 buf[len] = '\0';
 ll = strtonum(buf, 0, 0x7fffffffffffffffL, &errstr);
 if (errstr != ((void *)0))
  return;
 data->pid = ll;
 ll = strtonum(ptr2, 0, (0x7fffffff * 2U + 1U), &errstr);
 if (errstr != ((void *)0))
  return;
 data->idx = ll;
}

canonical_original_code:
void client_fill_session(struct msg_command_data *data)
{
 char *env; char *ptr1; char *ptr2; char buf[256];
 unsigned long len;
 char *errstr;
 long long ll;
 data->pid = -1;
 if ((env = getenv("TMUX")) == ((void *)0))
  return;
 if ((ptr2 = strrchr(env, ',')) == ((void *)0) || ptr2 == env)
  return;
 for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--)
  ;
 if (*ptr1 != ',')
  return;
 ptr1++;
 ptr2++;
 len = ptr2 - ptr1 - 1;
 if (len > (sizeof buf) - 1)
  return;
 memcpy(buf, ptr1, len);
 buf[len] = '\0';
 ll = strtonum(buf, 0, 0x7fffffffffffffffL, &errstr);
 if (errstr != ((void *)0))
  return;
 data->pid = ll;
 ll = strtonum(ptr2, 0, (0x7fffffff * 2U + 1U), &errstr);
 if (errstr != ((void *)0))
  return;
 data->idx = ll;
}

variable_types: struct msg_command_data { int pid; unsigned int idx; unsigned long namelen; } *

variable_types:
env
char *

variable_types:
buf
char [256]

variable_types:
len
unsigned long

variable_types:
errstr
char *

variable_types:
ll
long long

return_type: void


decompiled_code:
__int64 __fastcall client_write_server2(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 v11; // rdi
__int64 result; // rax
int v13; // [rsp+0h] [rbp-38h] BYREF
__int64 v14; // [rsp+8h] [rbp-30h]

v11 = *(_QWORD *)(a1 + 16);
v13 = a2;
v14 = a4 + a6;
result = buffer_write(v11, &v13, 16LL);
if ( a3 && a4 )
result = buffer_write(*(_QWORD *)(a1 + 16), a3, a4);
if ( a5 )
{
if ( a6 )
result = buffer_write(*(_QWORD *)(a1 + 16), a5, a6);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 v11; // rdi
__int64 result; // rax
int v13; // [rsp+0h] [rbp-38h] BYREF
__int64 v14; // [rsp+8h] [rbp-30h]

v11 = *(_QWORD *)(a1 + 16);
v13 = a2;
v14 = a4 + a6;
result = buffer_write(v11, &v13, 16LL);
if ( a3 && a4 )
result = buffer_write(*(_QWORD *)(a1 + 16), a3, a4);
if ( a5 )
{
if ( a6 )
result = buffer_write(*(_QWORD *)(a1 + 16), a5, a6);
}
return result;
}

original_code:
void
client_write_server2(struct client_ctx *cctx,
    enum hdrtype type, void *buf1, size_t len1, void *buf2, size_t len2)
{
 struct hdr hdr;
 hdr.type = type;
 hdr.size = len1 + len2;
 buffer_write(cctx->srv_out, &hdr, sizeof hdr);
 if (buf1 != ((void *)0) && len1 > 0)
  buffer_write(cctx->srv_out, buf1, len1);
 if (buf2 != ((void *)0) && len2 > 0)
  buffer_write(cctx->srv_out, buf2, len2);
}

canonical_original_code:
void client_write_server2(struct client_ctx *cctx, enum hdrtype type, void *buf1, unsigned long len1, void *buf2, unsigned long len2)
{
 struct hdr hdr;
 hdr.type = type;
 hdr.size = len1 + len2;
 buffer_write(cctx->srv_out, &hdr, sizeof hdr);
 if (buf1 != ((void *)0) && len1 > 0)
  buffer_write(cctx->srv_out, buf1, len1);
 if (buf2 != ((void *)0) && len2 > 0)
  buffer_write(cctx->srv_out, buf2, len2);
}

variable_types: struct client_ctx { int srv_fd; struct buffer { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; } *srv_in; struct buffer *srv_out; int flags; } *

variable_types: struct hdr { enum hdrtype { MSG_COMMAND=0, MSG_DETACH=1, MSG_ERROR=2, MSG_EXIT=3, MSG_EXITED=4, MSG_EXITING=5, MSG_IDENTIFY=6, MSG_PRINT=7, MSG_READY=8, MSG_RESIZE=9, MSG_SHUTDOWN=10, MSG_SUSPEND=11, MSG_UNLOCK=12, MSG_WAKEUP=13 } type; unsigned long size; }

user_defined_types: struct hdr { enum hdrtype type; unsigned long size; }

user_defined_types: struct client_ctx { int srv_fd; struct buffer *srv_in; struct buffer *srv_out; int flags; }


variable_types:
uv
unsigned int

variable_types:
data
unsigned char *


decompiled_code:
__int64 __fastcall utf8_combine(unsigned __int8 *a1)
{
int v1; // ecx
__int64 result; // rax
__int16 v3; // si
int v4; // edx
unsigned __int8 v5; // al

v1 = a1[1];
result = *a1;
if ( (_BYTE)v1 != 0xFF )
{
v3 = a1[2];
v4 = (unsigned __int8)result;
if ( (_BYTE)v3 == 0xFF )
{
result = v1 & 0x3F | ((unsigned __int8)result << 6) & 0x7C0u;
}
else
{
v5 = a1[3];
if ( v5 == 0xFF )
result = v3 & 0x3F | ((_WORD)v1 << 6) & 0xFC0 | (unsigned int)(unsigned __int16)((_WORD)v4 << 12);
else
result = v5 & 0x3F | (v3 << 6) & 0xFC0 | (v1 << 12) & 0x3F000 | (v4 << 18) & 0xFC0000u;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
int v1; // ecx
__int64 result; // rax
__int16 v3; // si
int v4; // edx
unsigned __int8 v5; // al

v1 = a1[1];
result = *a1;
if ( (_BYTE)v1 != 0xFF )
{
v3 = a1[2];
v4 = (unsigned __int8)result;
if ( (_BYTE)v3 == 0xFF )
{
result = v1 & 0x3F | ((unsigned __int8)result << 6) & 0x7C0u;
}
else
{
v5 = a1[3];
if ( v5 == 0xFF )
result = v3 & 0x3F | ((_WORD)v1 << 6) & 0xFC0 | (unsigned int)(unsigned __int16)((_WORD)v4 << 12);
else
result = v5 & 0x3F | (v3 << 6) & 0xFC0 | (v1 << 12) & 0x3F000 | (v4 << 18) & 0xFC0000u;
}
}
return result;
}

canonical_original_code:
unsigned int utf8_combine(unsigned char *data)
{
 unsigned int uv;
 if (data[1] == 0xff)
  uv = data[0];
 else if (data[2] == 0xff) {
  uv = data[1] & 0x3f;
  uv |= (data[0] & 0x1f) << 6;
 } else if (data[3] == 0xff) {
  uv = data[2] & 0x3f;
  uv |= (data[1] & 0x3f) << 6;
  uv |= (data[0] & 0x0f) << 12;
 } else {
  uv = data[3] & 0x3f;
  uv |= (data[2] & 0x3f) << 6;
  uv |= (data[1] & 0x3f) << 12;
  uv |= (data[0] & 0x3f) << 18;
 }
 return (uv);
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall utf8_width(unsigned int a1)
{
void **v1; // rdx
__int64 v2; // rax

v1 = &utf8_width_table;
v2 = 0LL;
do
{
if ( *(_DWORD *)v1 <= a1 && *((_DWORD *)v1 + 1) >= a1 )
return *((unsigned int *)&utf8_width_table + 3 * v2 + 2);
v2 = (unsigned int)(v2 + 1);
v1 = (void **)((char *)v1 + 12);
}
while ( (_DWORD)v2 != 156 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
void **v1; // rdx
__int64 v2; // rax

v1 = &utf8_width_table;
v2 = 0LL;
do
{
if ( *(_DWORD *)v1 <= a1 && *((_DWORD *)v1 + 1) >= a1 )
return *((unsigned int *)&utf8_width_table + 3 * v2 + 2);
v2 = (unsigned int)(v2 + 1);
v1 = (void **)((char *)v1 + 12);
}
while ( (_DWORD)v2 != 156 );
return 1LL;
}

original_code:
int
utf8_width(u_int uv)
{
 u_int i;
 for (i = 0; i < (sizeof((utf8_width_table)) / sizeof((utf8_width_table)[0])); i++) {
  if (uv < utf8_width_table[i].first)
   continue;
  if (uv > utf8_width_table[i].last)
          continue;
  return (utf8_width_table[i].width);
 }
 return (1);
}

canonical_original_code:
int utf8_width(unsigned int uv)
{
 unsigned int i;
 for (i = 0; i < (sizeof((utf8_width_table)) / sizeof((utf8_width_table)[0])); i++) {
  if (uv < utf8_width_table[i].first)
   continue;
  if (uv > utf8_width_table[i].last)
          continue;
  return (utf8_width_table[i].width);
 }
 return (1);
}

variable_types:
i
unsigned int


decompiled_code:
int client_suspend()
{
__pid_t v0; // eax
int result; // eax
struct sigaction vars0; // [rsp+0h] [rbp+0h] BYREF

memset(&vars0, 0, sizeof(vars0));
sigemptyset(&vars0.sa_mask);
vars0.sa_flags = 0x10000000;
vars0.sa_handler = 0LL;
if ( sigaction(20, &vars0, 0LL) || (vars0.sa_handler = (__sighandler_t)&sighandler, sigaction(18, &vars0, 0LL)) )
{
log_fatal("%s: %s", "client_suspend", "sigaction failed");
result = client_dispatch();
}
else
{
v0 = getpid();
result = kill(v0, 20);
}
return result;
}

canonical_decompiled_code:
int func4()
{
__pid_t v0; // eax
int result; // eax
struct sigaction vars0; // [rsp+0h] [rbp+0h] BYREF

memset(&vars0, 0, sizeof(vars0));
sigemptyset(&vars0.sa_mask);
vars0.sa_flags = 0x10000000;
vars0.sa_handler = 0LL;
if ( sigaction(20, &vars0, 0LL) || (vars0.sa_handler = (__sighandler_t)&sighandler, sigaction(18, &vars0, 0LL)) )
{
log_fatal("%s: %s", "client_suspend", "sigaction failed");
result = func5();
}
else
{
v0 = getpid();
result = kill(v0, 20);
}
return result;
}

original_code:
void
client_suspend(void)
{
 struct sigaction act;
 memset(&act, 0, sizeof act);
 sigemptyset(&act.sa_mask);
 act.sa_flags = 0x10000000;
 act.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
 if (sigaction(20, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 act.__sigaction_handler.sa_handler = sighandler;
 if (sigaction(18, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 kill(getpid(), 20);
}

canonical_original_code:
void client_suspend()
{
 struct sigaction act;
 memset(&act, 0, sizeof act);
 sigemptyset(&act.sa_mask);
 act.sa_flags = 0x10000000;
 act.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
 if (sigaction(20, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 act.__sigaction_handler.sa_handler = sighandler;
 if (sigaction(18, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 kill(getpid(), 20);
}

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

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

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


decompiled_code:
__int64 __fastcall client_send_identify(int a1)
{
char *v1; // rsi
unsigned int v2; // eax
char v4[8]; // [rsp+8h] [rbp-10A0h] BYREF
char buf[4096]; // [rsp+10h] [rbp-1098h] BYREF
char v6[128]; // [rsp+1010h] [rbp-98h] BYREF
int v7; // [rsp+1090h] [rbp-18h]

if ( ioctl(0, 0x5413uLL, v4) == -1 )
{
log_fatal("%s: %s", "client_send_identify", "ioctl(TIOCGWINSZ)");
}
else
{
v7 = a1;
if ( !getcwd(buf, 0x1000uLL) )
buf[0] = 0;
v1 = getenv("TERM");
if ( !v1 || (unsigned __int64)strlcpy(v6, v1, 128LL) > 0x7F )
v6[0] = 0;
v2 = dup(0);
if ( v2 != -1 )
return imsg_compose(&client_ibuf, 6LL, 5LL, 0xFFFFFFFFLL, v2, buf, 4228LL);
}
log_fatal("%s: %s", "client_send_identify", "dup failed");
return client_write_server();
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
char *v1; // rsi
unsigned int v2; // eax
char v4[8]; // [rsp+8h] [rbp-10A0h] BYREF
char buf[4096]; // [rsp+10h] [rbp-1098h] BYREF
char v6[128]; // [rsp+1010h] [rbp-98h] BYREF
int v7; // [rsp+1090h] [rbp-18h]

if ( ioctl(0, 0x5413uLL, v4) == -1 )
{
log_fatal("%s: %s", "client_send_identify", "ioctl(TIOCGWINSZ)");
}
else
{
v7 = a1;
if ( !getcwd(buf, 0x1000uLL) )
buf[0] = 0;
v1 = getenv("TERM");
if ( !v1 || (unsigned __int64)strlcpy(v6, v1, 128LL) > 0x7F )
v6[0] = 0;
v2 = dup(0);
if ( v2 != -1 )
return imsg_compose(&client_ibuf, 6LL, 5LL, 0xFFFFFFFFLL, v2, buf, 4228LL);
}
log_fatal("%s: %s", "client_send_identify", "dup failed");
return func1();
}

original_code:
void
client_send_identify(int flags)
{
 struct msg_identify_data data;
 struct winsize ws;
 char *term;
 int fd;
 if (ioctl(0, 0x5413, &ws) == -1)
  log_fatal("%s: %s", __func__, "ioctl(TIOCGWINSZ)");;
 data.flags = flags;
 if (getcwd(data.cwd, sizeof data.cwd) == ((void *)0))
  *data.cwd = '\0';
 term = getenv("TERM");
 if (term == ((void *)0) ||
     strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
  *data.term = '\0';
 if ((fd = dup(0)) == -1)
  log_fatal("%s: %s", __func__, "dup failed");;
 imsg_compose(&client_ibuf,
     MSG_IDENTIFY, 5, -1, fd, &data, sizeof data);
}

variable_types: flags int

variable_types: struct msg_identify_data { char cwd[4096]; char term[128]; int flags; }

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

variable_types:
term
char *

variable_types: fd int


decompiled_code:
void client_send_environ()
{
char **v0; // rbx
char *i; // rsi
_BYTE v2[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = environ;
for ( i = *environ; i; ++v0 )
{
if ( (unsigned __int64)strlcpy(v2, i, 1024LL) <= 0x3FF )
client_write_server(0xEu, (__int64)v2, 0x400u);
i = v0[1];
}
}

canonical_decompiled_code:
void func2()
{
char **v0; // rbx
char *i; // rsi
_BYTE v2[1032]; // [rsp+0h] [rbp-408h] BYREF

v0 = environ;
for ( i = *environ; i; ++v0 )
{
if ( (unsigned __int64)strlcpy(v2, i, 1024LL) <= 0x3FF )
func1(0xEu, (__int64)v2, 0x400u);
i = v0[1];
}
}

original_code:
void
client_send_environ(void)
{
 struct msg_environ_data data;
 char **var;
  for (var = environ; *var != ((void *)0); var++) {
  if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
   continue;
  client_write_server(MSG_ENVIRON, &data, sizeof data);
 }
}

canonical_original_code:
void client_send_environ()
{
 struct msg_environ_data data;
 char **var;
  for (var = environ; *var != ((void *)0); var++) {
  if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
   continue;
  client_write_server(MSG_ENVIRON, &data, sizeof data);
 }
}

variable_types: struct msg_environ_data { char var[1024]; }

variable_types:
var
char **


decompiled_code:
void *__fastcall client_init(char *name, char a2, int a3)
{
unsigned int v4; // ebp
int v5; // eax
int v6; // eax
unsigned int v7; // eax
int v8; // er8
void *result; // rax
struct sockaddr addr; // [rsp+10h] [rbp-98h] BYREF
int v11; // [rsp+78h] [rbp-30h]
__int16 v12; // [rsp+7Ch] [rbp-2Ch]

memset(&addr, 0, 0x68uLL);
addr.sa_family = 1;
v11 = 0;
v12 = 0;
if ( (unsigned __int64)strlcpy(addr.sa_data, name, 108LL) > 0x6B )
{
*_errno_location() = 36;
goto LABEL_17;
}
v4 = socket(1, 1, 0);
if ( v4 == -1 )
{
LABEL_21:
log_fatal("%s: %s", "client_init", "socket failed");
return (void *)client_suspend();
}
v5 = strlen(addr.sa_data);
if ( connect(v4, &addr, v5 + 2) == -1 )
{
if ( (a2 & 1) != 0 )
{
v6 = *_errno_location();
if ( v6 == 2 || v6 == 111 && !unlink(name) )
{
v4 = server_start(name);
if ( v4 == -1 )
{
log_warnx("server failed to start");
return 0LL;
}
goto LABEL_9;
}
}
LABEL_17:
log_warn("server not found");
return 0LL;
}
LABEL_9:
v7 = fcntl(v4, 3);
if ( v7 == -1 || (BYTE1(v7) |= 8u, fcntl(v4, 4, v7) == -1) || fcntl(v4, 2, 1LL) == -1 )
{
log_fatal("%s: %s", "client_init", "fcntl failed");
goto LABEL_21;
}
imsg_init(&client_ibuf, v4);
if ( (a2 & 4) != 0 )
client_send_environ();
v8 = isatty(0);
result = &client_ibuf;
if ( v8 )
{
client_send_identify(a3);
result = &client_ibuf;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(char *name, char a2, int a3)
{
unsigned int v4; // ebp
int v5; // eax
int v6; // eax
unsigned int v7; // eax
int v8; // er8
void *result; // rax
struct sockaddr addr; // [rsp+10h] [rbp-98h] BYREF
int v11; // [rsp+78h] [rbp-30h]
__int16 v12; // [rsp+7Ch] [rbp-2Ch]

memset(&addr, 0, 0x68uLL);
addr.sa_family = 1;
v11 = 0;
v12 = 0;
if ( (unsigned __int64)strlcpy(addr.sa_data, name, 108LL) > 0x6B )
{
*_errno_location() = 36;
goto LABEL_17;
}
v4 = socket(1, 1, 0);
if ( v4 == -1 )
{
LABEL_21:
log_fatal("%s: %s", "client_init", "socket failed");
return (void *)func4();
}
v5 = strlen(addr.sa_data);
if ( connect(v4, &addr, v5 + 2) == -1 )
{
if ( (a2 & 1) != 0 )
{
v6 = *_errno_location();
if ( v6 == 2 || v6 == 111 && !unlink(name) )
{
v4 = server_start(name);
if ( v4 == -1 )
{
log_warnx("server failed to start");
return 0LL;
}
goto LABEL_9;
}
}
LABEL_17:
log_warn("server not found");
return 0LL;
}
LABEL_9:
v7 = fcntl(v4, 3);
if ( v7 == -1 || (BYTE1(v7) |= 8u, fcntl(v4, 4, v7) == -1) || fcntl(v4, 2, 1LL) == -1 )
{
log_fatal("%s: %s", "client_init", "fcntl failed");
goto LABEL_21;
}
imsg_init(&client_ibuf, v4);
if ( (a2 & 4) != 0 )
func2();
v8 = isatty(0);
result = &client_ibuf;
if ( v8 )
{
func0(a3);
result = &client_ibuf;
}
return result;
}

original_code:
struct imsgbuf *
client_init(char *path, int cmdflags, int flags)
{
 struct sockaddr_un sa;
 size_t size;
 int fd, mode;
 memset(&sa, 0, sizeof sa);
 sa.sun_family = 1;
 size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
 if (size >= sizeof sa.sun_path) {
  (*__errno_location ()) = 36;
  goto not_found;
 }
 if ((fd = socket(1, SOCK_STREAM, 0)) == -1)
  log_fatal("%s: %s", __func__, "socket failed");;
 if (connect(fd, (struct sockaddr *) &sa, ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((&sa)->sun_path))) == -1) {
  if (!(cmdflags & 0x1))
   goto not_found;
  switch ((*__errno_location ())) {
  case 111:
   if (unlink(path) != 0)
    goto not_found;
  case 2:
   if ((fd = server_start(path)) == -1)
    goto start_failed;
   goto server_started;
  }
  goto not_found;
 }
server_started:
 if ((mode = fcntl(fd, 3)) == -1)
  log_fatal("%s: %s", __func__, "fcntl failed");;
 if (fcntl(fd, 4, mode|04000) == -1)
  log_fatal("%s: %s", __func__, "fcntl failed");;
 if (fcntl(fd, 2, 1) == -1)
  log_fatal("%s: %s", __func__, "fcntl failed");;
 imsg_init(&client_ibuf, fd);
 if (cmdflags & 0x4)
  client_send_environ();
 if (isatty(0))
  client_send_identify(flags);
 return (&client_ibuf);
start_failed:
 log_warnx("server failed to start");
 return (((void *)0));
not_found:
 log_warn("server not found");
 return (((void *)0));
}

canonical_original_code:
struct imsgbuf *client_init(char *path, int cmdflags, int flags)
{
 struct sockaddr_un sa;
 unsigned long size;
 int fd; int mode;
 memset(&sa, 0, sizeof sa);
 sa.sun_family = 1;
 size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
 if (size >= sizeof sa.sun_path) {
  (*__errno_location ()) = 36;
  goto not_found;
 }
 if ((fd = socket(1, SOCK_STREAM, 0)) == -1)
  log_fatal("%s: %s", __func__, "socket failed");;
 if (connect(fd, (struct sockaddr *) &sa, ((unsigned long) (((struct sockaddr_un *) 0)->sun_path) + strlen ((&sa)->sun_path))) == -1) {
  if (!(cmdflags & 0x1))
   goto not_found;
  switch ((*__errno_location ())) {
  case 111:
   if (unlink(path) != 0)
    goto not_found;
  case 2:
   if ((fd = server_start(path)) == -1)
    goto start_failed;
   goto server_started;
  }
  goto not_found;
 }
server_started:
 if ((mode = fcntl(fd, 3)) == -1)
  log_fatal("%s: %s", __func__, "fcntl failed");;
 if (fcntl(fd, 4, mode|04000) == -1)
  log_fatal("%s: %s", __func__, "fcntl failed");;
 if (fcntl(fd, 2, 1) == -1)
  log_fatal("%s: %s", __func__, "fcntl failed");;
 imsg_init(&client_ibuf, fd);
 if (cmdflags & 0x4)
  client_send_environ();
 if (isatty(0))
  client_send_identify(flags);
 return (&client_ibuf);
start_failed:
 log_warnx("server failed to start");
 return (((void *)0));
not_found:
 log_warn("server not found");
 return (((void *)0));
}

variable_types:
path
char *

variable_types: cmdflags int

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types:
size
unsigned long

variable_types: mode int

return_type: struct imsgbuf { struct { struct imsg_fd { struct { struct imsg_fd *tqe_next; struct imsg_fd **tqe_prev; } entry; int fd; } *tqh_first; struct imsg_fd **tqh_last; } fds; struct buf_read { unsigned char buf[65535]; unsigned char *rptr; unsigned long wpos; } r; struct msgbuf { struct { struct buf { struct { struct buf *tqe_next; struct buf **tqe_prev; } entry; unsigned char *buf; unsigned long size; unsigned long max; unsigned long wpos; unsigned long rpos; int fd; } *tqh_first; struct buf **tqh_last; } bufs; unsigned int queued; int fd; } w; int fd; int pid; } *

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

user_defined_types: struct imsgbuf { struct { struct imsg_fd *tqh_first; struct imsg_fd **tqh_last; } fds; struct buf_read r; struct msgbuf w; int fd; int pid; }

user_defined_types: struct msgbuf { struct { struct buf *tqh_first; struct buf **tqh_last; } bufs; unsigned int queued; int fd; }


decompiled_code:
__int64 client_dispatch()
{
__int64 v0; // rax
int v2; // [rsp+0h] [rbp-828h] BYREF
__int16 v3; // [rsp+4h] [rbp-824h]
const void *v4; // [rsp+18h] [rbp-810h]
char command[2056]; // [rsp+20h] [rbp-808h] BYREF

while ( 1 )
{
v0 = imsg_get(&client_ibuf, &v2);
if ( v0 == -1 )
break;
if ( !v0 )
return 0LL;
switch ( v2 )
{
case 1:
if ( v3 != 16LL )
goto LABEL_25;
client_write_server(5u, 0LL, 0);
client_exitmsg = (__int64)"detached";
goto LABEL_6;
case 3:
if ( v3 != 16LL )
goto LABEL_23;
client_write_server(5u, 0LL, 0);
client_exitmsg = (__int64)"exited";
goto LABEL_6;
case 4:
if ( v3 != 16LL )
goto LABEL_22;
imsg_free(&v2);
return 0xFFFFFFFFLL;
case 10:
if ( v3 != 16LL )
goto LABEL_20;
client_write_server(5u, 0LL, 0);
client_exitmsg = (__int64)"server exited";
goto LABEL_6;
case 11:
if ( v3 != 16LL )
goto LABEL_21;
client_suspend();
goto LABEL_6;
case 16:
if ( v3 != 2064LL )
goto LABEL_24;
qmemcpy(command, v4, 0x800uLL);
command[2047] = 0;
system(command);
client_write_server(0xFu, 0LL, 0);
LABEL_6:
imsg_free(&v2);
break;
default:
log_fatalx("%s: %s", "client_dispatch", "unexpected message");
goto LABEL_19;
}
}
LABEL_19:
log_fatalx("%s: %s", "client_dispatch", "imsg_get failed");
LABEL_20:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_SHUTDOWN size");
LABEL_21:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_SUSPEND size");
LABEL_22:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_EXITED size");
LABEL_23:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_EXIT size");
LABEL_24:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_LOCK size");
LABEL_25:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_DETACH size");
return client_main();
}

canonical_decompiled_code:
__int64 func5()
{
__int64 v0; // rax
int v2; // [rsp+0h] [rbp-828h] BYREF
__int16 v3; // [rsp+4h] [rbp-824h]
const void *v4; // [rsp+18h] [rbp-810h]
char command[2056]; // [rsp+20h] [rbp-808h] BYREF

while ( 1 )
{
v0 = imsg_get(&client_ibuf, &v2);
if ( v0 == -1 )
break;
if ( !v0 )
return 0LL;
switch ( v2 )
{
case 1:
if ( v3 != 16LL )
goto LABEL_25;
func1(5u, 0LL, 0);
client_exitmsg = (__int64)"detached";
goto LABEL_6;
case 3:
if ( v3 != 16LL )
goto LABEL_23;
func1(5u, 0LL, 0);
client_exitmsg = (__int64)"exited";
goto LABEL_6;
case 4:
if ( v3 != 16LL )
goto LABEL_22;
imsg_free(&v2);
return 0xFFFFFFFFLL;
case 10:
if ( v3 != 16LL )
goto LABEL_20;
func1(5u, 0LL, 0);
client_exitmsg = (__int64)"server exited";
goto LABEL_6;
case 11:
if ( v3 != 16LL )
goto LABEL_21;
func4();
goto LABEL_6;
case 16:
if ( v3 != 2064LL )
goto LABEL_24;
qmemcpy(command, v4, 0x800uLL);
command[2047] = 0;
system(command);
func1(0xFu, 0LL, 0);
LABEL_6:
imsg_free(&v2);
break;
default:
log_fatalx("%s: %s", "client_dispatch", "unexpected message");
goto LABEL_19;
}
}
LABEL_19:
log_fatalx("%s: %s", "client_dispatch", "imsg_get failed");
LABEL_20:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_SHUTDOWN size");
LABEL_21:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_SUSPEND size");
LABEL_22:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_EXITED size");
LABEL_23:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_EXIT size");
LABEL_24:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_LOCK size");
LABEL_25:
log_fatalx("%s: %s", "client_dispatch", "bad MSG_DETACH size");
return func6();
}

original_code:
int
client_dispatch(void)
{
 struct imsg imsg;
 struct msg_lock_data lockdata;
 ssize_t n, datalen;
 for (;;) {
  if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
   log_fatalx("%s: %s", __func__, "imsg_get failed");;
  if (n == 0)
   return (0);
  datalen = imsg.hdr.len - sizeof(struct imsg_hdr);
  switch (imsg.hdr.type) {
  case MSG_DETACH:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_DETACH size");;
   client_write_server(MSG_EXITING, ((void *)0), 0);
   client_exitmsg = "detached";
   break;
  case MSG_EXIT:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_EXIT size");;
   client_write_server(MSG_EXITING, ((void *)0), 0);
   client_exitmsg = "exited";
   break;
  case MSG_EXITED:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_EXITED size");;
   imsg_free(&imsg);
   return (-1);
  case MSG_SHUTDOWN:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_SHUTDOWN size");;
   client_write_server(MSG_EXITING, ((void *)0), 0);
   client_exitmsg = "server exited";
   break;
  case MSG_SUSPEND:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_SUSPEND size");;
   client_suspend();
   break;
  case MSG_LOCK:
   if (datalen != sizeof lockdata)
    log_fatalx("%s: %s", __func__, "bad MSG_LOCK size");;
   memcpy(&lockdata, imsg.data, sizeof lockdata);
   lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
   system(lockdata.cmd);
   client_write_server(MSG_UNLOCK, ((void *)0), 0);
   break;
  default:
   log_fatalx("%s: %s", __func__, "unexpected message");;
  }
  imsg_free(&imsg);
 }
}

canonical_original_code:
int client_dispatch()
{
 struct imsg imsg;
 struct msg_lock_data lockdata;
 long n; long datalen;
 for (;;) {
  if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
   log_fatalx("%s: %s", __func__, "imsg_get failed");;
  if (n == 0)
   return (0);
  datalen = imsg.hdr.len - sizeof(struct imsg_hdr);
  switch (imsg.hdr.type) {
  case MSG_DETACH:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_DETACH size");;
   client_write_server(MSG_EXITING, ((void *)0), 0);
   client_exitmsg = "detached";
   break;
  case MSG_EXIT:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_EXIT size");;
   client_write_server(MSG_EXITING, ((void *)0), 0);
   client_exitmsg = "exited";
   break;
  case MSG_EXITED:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_EXITED size");;
   imsg_free(&imsg);
   return (-1);
  case MSG_SHUTDOWN:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_SHUTDOWN size");;
   client_write_server(MSG_EXITING, ((void *)0), 0);
   client_exitmsg = "server exited";
   break;
  case MSG_SUSPEND:
   if (datalen != 0)
    log_fatalx("%s: %s", __func__, "bad MSG_SUSPEND size");;
   client_suspend();
   break;
  case MSG_LOCK:
   if (datalen != sizeof lockdata)
    log_fatalx("%s: %s", __func__, "bad MSG_LOCK size");;
   memcpy(&lockdata, imsg.data, sizeof lockdata);
   lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
   system(lockdata.cmd);
   client_write_server(MSG_UNLOCK, ((void *)0), 0);
   break;
  default:
   log_fatalx("%s: %s", __func__, "unexpected message");;
  }
  imsg_free(&imsg);
 }
}

variable_types: struct imsg { struct imsg_hdr { unsigned int type; unsigned int len; unsigned int flags; unsigned int peerid; unsigned int pid; } hdr; int fd; void *data; }

variable_types: struct msg_lock_data { char cmd[2048]; }

variable_types: n long

variable_types: datalen long

user_defined_types: struct imsg { struct imsg_hdr hdr; int fd; void *data; }


decompiled_code:
void client_main()
{
int v0; // eax
int v1; // ecx
int v2; // eax
char v3; // al
const char *v4; // rsi
int v5; // eax
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

siginit();
logfile("client");
if ( (unsigned int)client_dispatch() )
{
LABEL_25:
v4 = (const char *)client_exitmsg;
if ( !client_exitmsg )
exit(0);
LABEL_26:
if ( !login_shell )
printf("[%s]\n", v4);
exit(1);
}
while ( 1 )
{
if ( sigterm )
{
client_exitmsg = (__int64)"terminated";
client_write_server(5u, 0LL, 0);
if ( sigchld )
goto LABEL_16;
LABEL_4:
if ( sigwinch )
goto LABEL_20;
if ( sigcont )
{
sigcont = 0;
siginit();
client_write_server(0xDu, 0LL, 0);
}
else
{
v0 = client_ibuf[16398];
v1 = client_ibuf[16396];
fds.events = 1;
fds.fd = v0;
if ( v1 )
fds.events = 5;
v2 = poll(&fds, 1uLL, -1);
if ( v2 == -1 )
{
v5 = *_errno_location();
if ( v5 != 11 && v5 != 4 )
{
log_fatal("%s: %s", "client_main", "poll failed");
LABEL_20:
sigwinch = 0;
client_write_server(9u, 0LL, 0);
}
}
else if ( v2 )
{
v3 = fds.revents;
if ( (fds.revents & 0x38) != 0 )
{
log_fatalx("%s: %s", "client_main", "socket error");
JUMPOUT(0x865LL);
}
if ( (fds.revents & 1) != 0 )
{
if ( (unsigned int)imsg_read(client_ibuf) + 1 <= 1 )
goto LABEL_14;
if ( (unsigned int)client_dispatch() )
goto LABEL_25;
v3 = fds.revents;
}
if ( (v3 & 4) != 0 && (int)msgbuf_write((unsigned int)((_DWORD)client_ibuf + 65568)) < 0 )
{
LABEL_14:
client_exitmsg = (__int64)"lost server";
v4 = "lost server";
goto LABEL_26;
}
}
}
}
else
{
if ( !sigchld )
goto LABEL_4;
LABEL_16:
sigchld = 0;
waitpid(-1, 0LL, 1);
}
}
}

canonical_decompiled_code:
void func6()
{
int v0; // eax
int v1; // ecx
int v2; // eax
char v3; // al
const char *v4; // rsi
int v5; // eax
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

siginit();
logfile("client");
if ( (unsigned int)func5() )
{
LABEL_25:
v4 = (const char *)client_exitmsg;
if ( !client_exitmsg )
exit(0);
LABEL_26:
if ( !login_shell )
printf("[%s]\n", v4);
exit(1);
}
while ( 1 )
{
if ( sigterm )
{
client_exitmsg = (__int64)"terminated";
func1(5u, 0LL, 0);
if ( sigchld )
goto LABEL_16;
LABEL_4:
if ( sigwinch )
goto LABEL_20;
if ( sigcont )
{
sigcont = 0;
siginit();
func1(0xDu, 0LL, 0);
}
else
{
v0 = client_ibuf[16398];
v1 = client_ibuf[16396];
fds.events = 1;
fds.fd = v0;
if ( v1 )
fds.events = 5;
v2 = poll(&fds, 1uLL, -1);
if ( v2 == -1 )
{
v5 = *_errno_location();
if ( v5 != 11 && v5 != 4 )
{
log_fatal("%s: %s", "client_main", "poll failed");
LABEL_20:
sigwinch = 0;
func1(9u, 0LL, 0);
}
}
else if ( v2 )
{
v3 = fds.revents;
if ( (fds.revents & 0x38) != 0 )
{
log_fatalx("%s: %s", "client_main", "socket error");
JUMPOUT(0x865LL);
}
if ( (fds.revents & 1) != 0 )
{
if ( (unsigned int)imsg_read(client_ibuf) + 1 <= 1 )
goto LABEL_14;
if ( (unsigned int)func5() )
goto LABEL_25;
v3 = fds.revents;
}
if ( (v3 & 4) != 0 && (int)msgbuf_write((unsigned int)((_DWORD)client_ibuf + 65568)) < 0 )
{
LABEL_14:
client_exitmsg = (__int64)"lost server";
v4 = "lost server";
goto LABEL_26;
}
}
}
}
else
{
if ( !sigchld )
goto LABEL_4;
LABEL_16:
sigchld = 0;
waitpid(-1, 0LL, 1);
}
}
}

original_code:
__attribute__ ((__noreturn__)) void
client_main(void)
{
 struct pollfd pfd;
 int n, nfds;
 siginit();
 logfile("client");
 if (client_dispatch() != 0)
  goto out;
 for (;;) {
  if (sigterm) {
   client_exitmsg = "terminated";
   client_write_server(MSG_EXITING, ((void *)0), 0);
  }
  if (sigchld) {
   sigchld = 0;
   waitpid((-1), ((void *)0), 1);
   continue;
  }
  if (sigwinch) {
   sigwinch = 0;
   client_write_server(MSG_RESIZE, ((void *)0), 0);
   continue;
  }
  if (sigcont) {
   sigcont = 0;
   siginit();
   client_write_server(MSG_WAKEUP, ((void *)0), 0);
   continue;
  }
  pfd.fd = client_ibuf.fd;
  pfd.events = 0x001;
  if (client_ibuf.w.queued > 0)
   pfd.events |= 0x004;
  if ((nfds = poll(&pfd, 1, -1)) == -1) {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)
    continue;
   log_fatal("%s: %s", __func__, "poll failed");;
  }
  if (nfds == 0)
   continue;
  if (pfd.revents & (0x008|0x010|0x020))
   log_fatalx("%s: %s", __func__, "socket error");;
  if (pfd.revents & 0x001) {
   if ((n = imsg_read(&client_ibuf)) == -1 || n == 0) {
    client_exitmsg = "lost server";
    break;
   }
   if (client_dispatch() != 0)
    break;
  }
  if (pfd.revents & 0x004) {
   if (msgbuf_write(&client_ibuf.w) < 0) {
    client_exitmsg = "lost server";
    break;
   }
  }
 }
out:
 if (client_exitmsg != ((void *)0)) {
  if (!login_shell)
   printf("[%s]\n", client_exitmsg);
  exit(1);
 }
 exit(0);
}

canonical_original_code:
void client_main()
{
 struct pollfd pfd;
 int n; int nfds;
 siginit();
 logfile("client");
 if (client_dispatch() != 0)
  goto out;
 for (;;) {
  if (sigterm) {
   client_exitmsg = "terminated";
   client_write_server(MSG_EXITING, ((void *)0), 0);
  }
  if (sigchld) {
   sigchld = 0;
   waitpid((-1), ((void *)0), 1);
   continue;
  }
  if (sigwinch) {
   sigwinch = 0;
   client_write_server(MSG_RESIZE, ((void *)0), 0);
   continue;
  }
  if (sigcont) {
   sigcont = 0;
   siginit();
   client_write_server(MSG_WAKEUP, ((void *)0), 0);
   continue;
  }
  pfd.fd = client_ibuf.fd;
  pfd.events = 0x001;
  if (client_ibuf.w.queued > 0)
   pfd.events |= 0x004;
  if ((nfds = poll(&pfd, 1, -1)) == -1) {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)
    continue;
   log_fatal("%s: %s", __func__, "poll failed");;
  }
  if (nfds == 0)
   continue;
  if (pfd.revents & (0x008|0x010|0x020))
   log_fatalx("%s: %s", __func__, "socket error");;
  if (pfd.revents & 0x001) {
   if ((n = imsg_read(&client_ibuf)) == -1 || n == 0) {
    client_exitmsg = "lost server";
    break;
   }
   if (client_dispatch() != 0)
    break;
  }
  if (pfd.revents & 0x004) {
   if (msgbuf_write(&client_ibuf.w) < 0) {
    client_exitmsg = "lost server";
    break;
   }
  }
 }
out:
 if (client_exitmsg != ((void *)0)) {
  if (!login_shell)
   printf("[%s]\n", client_exitmsg);
  exit(1);
 }
 exit(0);
}

variable_types: struct pollfd { int fd; short events; short revents; }

variable_types: n int

variable_types: nfds int


decompiled_code:
__int64 __fastcall tty_term_strip(const char *a1)
{
const char *v1; // rbp
char v2; // al
__int64 v3; // rdx
char v4; // cl

v1 = a1;
if ( !strchr(a1, 36) )
return xstrdup(a1);
v2 = *a1;
v3 = 0LL;
if ( *a1 )
{
while ( 1 )
{
if ( v2 == 36 )
{
v4 = v1[1];
if ( v4 == 60 )
{
while ( v4 )
{
if ( v4 == 62 )
{
v2 = v1[2];
v1 += 2;
goto LABEL_4;
}
v4 = v1[2];
++v1;
}
v2 = *++v1;
}
}
LABEL_4:
byte_CDF[++v3] = v2;
if ( v3 == 0x1FFF )
break;
v2 = v1[1];
if ( !v2 )
break;
++v1;
}
}
buf_0[v3] = 0;
return xstrdup(buf_0);
}

original_code:
char *
tty_term_strip(const char *s)
{
 const char *ptr;
 static char buf[8192];
 size_t len;
 if (strchr(s, '$') == ((void *)0))
  return (xstrdup(s));
 len = 0;
 for (ptr = s; *ptr != '\0'; ptr++) {
  if (*ptr == '$' && *(ptr + 1) == '<') {
   while (*ptr != '\0' && *ptr != '>')
    ptr++;
   if (*ptr == '>')
    ptr++;
  }
  buf[len++] = *ptr;
  if (len == (sizeof buf) - 1)
   break;
 }
 buf[len] = '\0';
 return (xstrdup(buf));
}

canonical_original_code:
char *tty_term_strip(char *s)
{
 char *ptr;
 static char buf[8192];
 unsigned long len;
 if (strchr(s, '$') == ((void *)0))
  return (xstrdup(s));
 len = 0;
 for (ptr = s; *ptr != '\0'; ptr++) {
  if (*ptr == '$' && *(ptr + 1) == '<') {
   while (*ptr != '\0' && *ptr != '>')
    ptr++;
   if (*ptr == '>')
    ptr++;
  }
  buf[len++] = *ptr;
  if (len == (sizeof buf) - 1)
   break;
 }
 buf[len] = '\0';
 return (xstrdup(buf));
}

variable_types:
s
char *

variable_types:
ptr
char *

variable_types:
buf
char [8192]

variable_types:
len
unsigned long

return_type: char *


decompiled_code:
_BOOL8 __fastcall tty_term_has(__int64 a1, unsigned int a2)
{
return *(_DWORD *)(16LL * a2 + a1 + 16) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, unsigned int a2)
{
return *(_DWORD *)(16LL * a2 + a1 + 16) != 0;
}

original_code:
int
tty_term_has(struct tty_term *term, enum tty_code_code code)
{
 return (term->codes[code].type != TTYCODE_NONE);
}

variable_types: struct tty_term { char *name; unsigned int references; struct tty_code { enum tty_code_type { TTYCODE_NONE=0, TTYCODE_STRING=1, TTYCODE_NUMBER=2, TTYCODE_FLAG=3 } type; union { char *string; int number; int flag; } value; } codes[-1]; int flags; struct { struct tty_term *sle_next; } entry; } *

variable_types: enum tty_code_code { TTYC_AX=0, TTYC_ACSC=1, TTYC_BEL=2, TTYC_BLINK=3, TTYC_BOLD=4, TTYC_CIVIS=5, TTYC_CLEAR=6, TTYC_CNORM=7, TTYC_COLORS=8, TTYC_CSR=9, TTYC_CUD=10, TTYC_CUD1=11, TTYC_CUP=12, TTYC_DCH=13, TTYC_DCH1=14, TTYC_DIM=15, TTYC_DL=16, TTYC_DL1=17, TTYC_EL=18, TTYC_EL1=19, TTYC_ENACS=20, TTYC_ICH=21, TTYC_ICH1=22, TTYC_IL=23, TTYC_IL1=24, TTYC_INVIS=25, TTYC_IS1=26, TTYC_IS2=27, TTYC_IS3=28, TTYC_KCBT=29, TTYC_KCUB1=30, TTYC_KCUD1=31, TTYC_KCUF1=32, TTYC_KCUU1=33, TTYC_KDCH1=34, TTYC_KEND=35, TTYC_KF1=36, TTYC_KF10=37, TTYC_KF11=38, TTYC_KF12=39, TTYC_KF13=40, TTYC_KF14=41, TTYC_KF15=42, TTYC_KF16=43, TTYC_KF17=44, TTYC_KF18=45, TTYC_KF19=46, TTYC_KF20=47, TTYC_KF2=48, TTYC_KF3=49, TTYC_KF4=50, TTYC_KF5=51, TTYC_KF6=52, TTYC_KF7=53, TTYC_KF8=54, TTYC_KF9=55, TTYC_KHOME=56, TTYC_KICH1=57, TTYC_KMOUS=58, TTYC_KNP=59, TTYC_KPP=60, TTYC_OP=61, TTYC_REV=62, TTYC_RI=63, TTYC_RMACS=64, TTYC_RMCUP=65, TTYC_RMIR=66, TTYC_RMKX=67, TTYC_SETAB=68, TTYC_SETAF=69, TTYC_SGR0=70, TTYC_SMACS=71, TTYC_SMCUP=72, TTYC_SMIR=73, TTYC_SMKX=74, TTYC_SMSO=75, TTYC_SMUL=76, TTYC_XENL=77 }

user_defined_types: struct tty_term { char *name; unsigned int references; struct tty_code codes[-1]; int flags; struct { struct tty_term *sle_next; } entry; }

user_defined_types: struct tty_code { enum tty_code_type type; union { char *string; int number; int flag; } value; }


decompiled_code:
char __fastcall tty_term_quirks(__int64 a1)
{
const void *v1; // r8
bool v3; // zf
__int64 v4; // rax
__int64 v5; // rax

v1 = *(const void **)a1;
if ( !memcmp(*(const void **)a1, "rxvt", 4uLL) && !tty_term_has(a1, 0xEu) )
{
*(_DWORD *)(a1 + 240) = 1;
v5 = xstrdup("\x1B[P");
v1 = *(const void **)a1;
*(_QWORD *)(a1 + 248) = v5;
}
v3 = memcmp(v1, "xterm", 5uLL) == 0;
LOBYTE(v4) = !v3;
if ( v3 )
{
LODWORD(v4) = tty_term_has(a1, 0x16u);
if ( !(_DWORD)v4 )
{
*(_DWORD *)(a1 + 368) = 1;
v4 = xstrdup("\x1B[@");
*(_QWORD *)(a1 + 376) = v4;
}
}
return v4;
}

original_code:
void
tty_term_quirks(struct tty_term *term)
{
 if (strncmp(term->name, "rxvt", 4) == 0) {
  if (!tty_term_has(term, TTYC_DCH1)) {
   term->codes[TTYC_DCH1].type = TTYCODE_STRING;
   term->codes[TTYC_DCH1].value.string = xstrdup("\033[P");
  }
 }
 if (strncmp(term->name, "xterm", 5) == 0) {
  if (!tty_term_has(term, TTYC_ICH1)) {
   term->codes[TTYC_ICH1].type = TTYCODE_STRING;
   term->codes[TTYC_ICH1].value.string = xstrdup("\033[@");
  }
 }
}

user_defined_types: enum tty_code_type { TTYCODE_NONE=0, TTYCODE_STRING=1, TTYCODE_NUMBER=2, TTYCODE_FLAG=3 }


decompiled_code:
void *__fastcall tty_term_string(__int64 a1, unsigned int a2)
{
unsigned int v2; // er8
__int64 v3; // rdi

if ( !tty_term_has(a1, a2) )
return &unk_2CF3;
v3 = 16LL * v2 + a1;
if ( *(_DWORD *)(v3 + 16) == 1 )
return *(void **)(v3 + 24);
log_fatalx("not a string: %d", v2);
return (void *)tty_term_string1();
}

canonical_decompiled_code:
void *__fastcall func4(__int64 a1, unsigned int a2)
{
unsigned int v2; // er8
__int64 v3; // rdi

if ( !func2(a1, a2) )
return &unk_2CF3;
v3 = 16LL * v2 + a1;
if ( *(_DWORD *)(v3 + 16) == 1 )
return *(void **)(v3 + 24);
log_fatalx("not a string: %d", v2);
return (void *)func5();
}

original_code:
const char *
tty_term_string(struct tty_term *term, enum tty_code_code code)
{
 if (!tty_term_has(term, code))
  return ("");
 if (term->codes[code].type != TTYCODE_STRING)
  log_fatalx("not a string: %d", code);
 return (term->codes[code].value.string);
}

canonical_original_code:
char *tty_term_string(struct tty_term *term, enum tty_code_code code)
{
 if (!tty_term_has(term, code))
  return ("");
 if (term->codes[code].type != TTYCODE_STRING)
  log_fatalx("not a string: %d", code);
 return (term->codes[code].value.string);
}

user_defined_types: enum tty_code_code { TTYC_AX=0, TTYC_ACSC=1, TTYC_BEL=2, TTYC_BLINK=3, TTYC_BOLD=4, TTYC_CIVIS=5, TTYC_CLEAR=6, TTYC_CNORM=7, TTYC_COLORS=8, TTYC_CSR=9, TTYC_CUD=10, TTYC_CUD1=11, TTYC_CUP=12, TTYC_DCH=13, TTYC_DCH1=14, TTYC_DIM=15, TTYC_DL=16, TTYC_DL1=17, TTYC_EL=18, TTYC_EL1=19, TTYC_ENACS=20, TTYC_ICH=21, TTYC_ICH1=22, TTYC_IL=23, TTYC_IL1=24, TTYC_INVIS=25, TTYC_IS1=26, TTYC_IS2=27, TTYC_IS3=28, TTYC_KCBT=29, TTYC_KCUB1=30, TTYC_KCUD1=31, TTYC_KCUF1=32, TTYC_KCUU1=33, TTYC_KDCH1=34, TTYC_KEND=35, TTYC_KF1=36, TTYC_KF10=37, TTYC_KF11=38, TTYC_KF12=39, TTYC_KF13=40, TTYC_KF14=41, TTYC_KF15=42, TTYC_KF16=43, TTYC_KF17=44, TTYC_KF18=45, TTYC_KF19=46, TTYC_KF20=47, TTYC_KF2=48, TTYC_KF3=49, TTYC_KF4=50, TTYC_KF5=51, TTYC_KF6=52, TTYC_KF7=53, TTYC_KF8=54, TTYC_KF9=55, TTYC_KHOME=56, TTYC_KICH1=57, TTYC_KMOUS=58, TTYC_KNP=59, TTYC_KPP=60, TTYC_OP=61, TTYC_REV=62, TTYC_RI=63, TTYC_RMACS=64, TTYC_RMCUP=65, TTYC_RMIR=66, TTYC_RMKX=67, TTYC_SETAB=68, TTYC_SETAF=69, TTYC_SGR0=70, TTYC_SMACS=71, TTYC_SMCUP=72, TTYC_SMIR=73, TTYC_SMKX=74, TTYC_SMSO=75, TTYC_SMUL=76, TTYC_XENL=77 }


decompiled_code:
__int64 __fastcall tty_term_number(__int64 a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // er8
__int64 v4; // rdi

result = tty_term_has(a1, a2);
if ( (_DWORD)result )
{
v4 = 16LL * v3 + a1;
if ( *(_DWORD *)(v4 + 16) == 2 )
{
result = *(unsigned int *)(v4 + 24);
}
else
{
log_fatalx("not a number: %d", v3);
result = tty_term_flag();
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // er8
__int64 v4; // rdi

result = func2(a1, a2);
if ( (_DWORD)result )
{
v4 = 16LL * v3 + a1;
if ( *(_DWORD *)(v4 + 16) == 2 )
{
result = *(unsigned int *)(v4 + 24);
}
else
{
log_fatalx("not a number: %d", v3);
result = func8();
}
}
return result;
}

original_code:
int
tty_term_number(struct tty_term *term, enum tty_code_code code)
{
 if (!tty_term_has(term, code))
  return (0);
 if (term->codes[code].type != TTYCODE_NUMBER)
  log_fatalx("not a number: %d", code);
 return (term->codes[code].value.number);
}


decompiled_code:
__int64 __fastcall tty_term_flag(__int64 a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // er8
__int64 v4; // rdi
int v5; // esi

result = tty_term_has(a1, a2);
if ( (_DWORD)result )
{
v4 = 16LL * v3 + a1;
if ( *(_DWORD *)(v4 + 16) == 3 )
{
result = *(unsigned int *)(v4 + 24);
}
else
{
v5 = v3;
log_fatalx("not a flag: %d", v3);
result = tty_term_find("not a flag: %d", v5);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // er8
__int64 v4; // rdi
int v5; // esi

result = func2(a1, a2);
if ( (_DWORD)result )
{
v4 = 16LL * v3 + a1;
if ( *(_DWORD *)(v4 + 16) == 3 )
{
result = *(unsigned int *)(v4 + 24);
}
else
{
v5 = v3;
log_fatalx("not a flag: %d", v3);
result = func9("not a flag: %d", v5);
}
}
return result;
}

original_code:
int
tty_term_flag(struct tty_term *term, enum tty_code_code code)
{
 if (!tty_term_has(term, code))
  return (0);
 if (term->codes[code].type != TTYCODE_FLAG)
  log_fatalx("not a flag: %d", code);
 return (term->codes[code].value.flag);
}


decompiled_code:
__int64 __fastcall cmd_suspend_client_exec(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbp

v2 = cmd_find_client(a2, *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL));
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
tty_stop_tty(v2 + 65792);
*(_DWORD *)(v3 + 66608) |= 0x40u;
server_write_client(v3, 11LL, 0LL, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbp

v2 = cmd_find_client(a2, *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL));
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
tty_stop_tty(v2 + 65792);
*(_DWORD *)(v3 + 66608) |= 0x40u;
server_write_client(v3, 11LL, 0LL, 0LL);
return 0LL;
}

original_code:
int
cmd_suspend_client_exec(struct cmd *self, struct cmd_ctx *ctx)
{
 struct cmd_target_data *data = self->data;
 struct client *c;
 if ((c = cmd_find_client(ctx, data->target)) == ((void *)0))
  return (-1);
 tty_stop_tty(&c->tty);
 c->flags |= 0x40;
 server_write_client(c, MSG_SUSPEND, ((void *)0), 0);
 return (0);
}

variable_types: struct cmd { struct cmd_entry { char *name; char *alias; char *usage; int flags; void (*init)(struct cmd *, int); int (*parse)(struct cmd *, int, char **, char **); int (*exec)(struct cmd *, struct cmd_ctx { struct client { int fd; struct buffer { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; } *in; struct buffer *out; char *title; char *cwd; struct tty { char *path; unsigned int sx; unsigned int sy; unsigned int cx; unsigned int cy; int mode; unsigned int rlower; unsigned int rupper; char *termname; struct tty_term { char *name; unsigned int references; struct tty_code { enum tty_code_type { TTYCODE_NONE=0, TTYCODE_STRING=1, TTYCODE_NUMBER=2, TTYCODE_FLAG=3 } type; union { char *string; int number; int flag; } value; } codes[-1]; int flags; struct { struct tty_term *sle_next; } entry; } *term; int fd; struct buffer *in; struct buffer *out; int log_fd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } tio; struct grid_cell { unsigned char attr; unsigned char flags; unsigned char fg; unsigned char bg; unsigned char data; } cell; unsigned char acs[-1]; int flags; int term_flags; struct timeval { long tv_sec; long tv_usec; } key_timer; unsigned long ksize; struct tty_keys { struct tty_key { int key; char *string; int flags; struct { struct tty_key *rbe_left; struct tty_key *rbe_right; struct tty_key *rbe_parent; int rbe_color; } entry; } *rbh_root; } ktree; } tty; struct timeval status_timer; struct timeval repeat_timer; struct screen { char *title; struct grid { unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; unsigned int *size; struct grid_cell **data; unsigned int *usize; struct grid_utf8 { unsigned char width; unsigned char data[8]; } **udata; } *grid; unsigned int cx; unsigned int cy; unsigned int old_cx; unsigned int old_cy; unsigned int rupper; unsigned int rlower; unsigned int old_rupper; unsigned int old_rlower; int mode; unsigned char *tabs; struct screen_sel { int flag; unsigned int sx; unsigned int sy; unsigned int ex; unsigned int ey; struct grid_cell cell; } sel; } status; int flags; char *message_string; struct timeval message_timer; char *prompt_string; char *prompt_buffer; unsigned long prompt_index; int (*prompt_callback)(void *, char *); void *prompt_data; int prompt_flags; unsigned int prompt_hindex; struct { char **list; unsigned int num; unsigned long space; } prompt_hdata; struct mode_key_data { int type; int flags; } prompt_mdata; struct session { char *name; struct timeval tv; unsigned int sx; unsigned int sy; struct winlink { int idx; struct window { char *name; struct timeval name_timer; struct window_pane { struct window *window; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; int flags; char *cmd; char *cwd; int pid; int fd; char tty[32]; struct buffer *in; struct buffer *out; struct input_ctx { struct window_pane *wp; struct screen_write_ctx { struct window_pane *wp; struct screen *s; } ctx; unsigned char *buf; unsigned long len; unsigned long off; struct grid_cell cell; struct grid_cell saved_cell; unsigned int saved_cx; unsigned int saved_cy; unsigned char *string_buf; unsigned long string_len; int string_type; unsigned char utf8_buf[4]; unsigned int utf8_len; unsigned int utf8_off; unsigned char intermediate; void *(*state)(unsigned char, struct input_ctx *); unsigned char private; struct { struct input_arg { unsigned char data[64]; unsigned long used; } *list; unsigned int num; unsigned long space; } args; } ictx; struct screen *screen; struct screen base; struct window_mode { struct screen *(*init)(struct window_pane *); void (*free)(struct window_pane *); void (*resize)(struct window_pane *, unsigned int, unsigned int); void (*key)(struct window_pane *, struct client *, int); void (*mouse)(struct window_pane *, struct client *, unsigned char, unsigned char, unsigned char); void (*timer)(struct window_pane *); } *mode; void *modedata; struct { struct window_pane *tqe_next; struct window_pane **tqe_prev; } entry; } *active; struct window_panes { struct window_pane *tqh_first; struct window_pane **tqh_last; } panes; unsigned int layout; unsigned int sx; unsigned int sy; int flags; struct options { struct options_tree { struct options_entry { char *name; enum { OPTIONS_STRING=0, OPTIONS_NUMBER=1, OPTIONS_KEY=2 } type; union { char *string; long long number; int key; } value; struct { struct options_entry *spe_left; struct options_entry *spe_right; } entry; } *sph_root; } tree; struct options *parent; } options; unsigned int references; } *window; struct { struct winlink *rbe_left; struct winlink *rbe_right; struct winlink *rbe_parent; int rbe_color; } entry; struct { struct winlink *sle_next; } sentry; } *curw; struct winlink_stack { struct winlink *slh_first; } lastw; struct winlinks { struct winlink *rbh_root; } windows; struct options options; struct paste_stack { struct paste_buffer { char *data; struct timeval tv; } **list; unsigned int num; unsigned long space; } buffers; struct { struct session_alert { struct winlink *wl; int type; struct { struct session_alert *sle_next; } entry; } *slh_first; } alerts; int flags; } *session; } *cmdclient; struct client *curclient; struct session *cursession; struct msg_command_data { int pid; unsigned int idx; unsigned long namelen; } *msgdata; void (*print)(struct cmd_ctx *, char *, ...); void (*info)(struct cmd_ctx *, char *, ...); void (*error)(struct cmd_ctx *, char *, ...); } *); void (*send)(struct cmd *, struct buffer *); void (*recv)(struct cmd *, struct buffer *); void (*free)(struct cmd *); unsigned long (*print)(struct cmd *, char *, unsigned long); } *entry; void *data; struct { struct cmd *tqe_next; struct cmd **tqe_prev; } qentry; } *

variable_types:
ctx
struct cmd_ctx *

variable_types: struct cmd_target_data { int flags; char *target; char *arg; } *

user_defined_types: struct cmd { struct cmd_entry *entry; void *data; struct { struct cmd *tqe_next; struct cmd **tqe_prev; } qentry; }

user_defined_types: struct client { int fd; struct buffer *in; struct buffer *out; char *title; char *cwd; struct tty tty; struct timeval status_timer; struct timeval repeat_timer; struct screen status; int flags; char *message_string; struct timeval message_timer; char *prompt_string; char *prompt_buffer; unsigned long prompt_index; int (*prompt_callback)(void *, char *); void *prompt_data; int prompt_flags; unsigned int prompt_hindex; struct { char **list; unsigned int num; unsigned long space; } prompt_hdata; struct mode_key_data prompt_mdata; struct session *session; }

user_defined_types: struct tty { char *path; unsigned int sx; unsigned int sy; unsigned int cx; unsigned int cy; int mode; unsigned int rlower; unsigned int rupper; char *termname; struct tty_term *term; int fd; struct buffer *in; struct buffer *out; int log_fd; struct termios tio; struct grid_cell cell; unsigned char acs[-1]; int flags; int term_flags; struct timeval key_timer; unsigned long ksize; struct tty_keys ktree; }

user_defined_types: struct screen { char *title; struct grid *grid; unsigned int cx; unsigned int cy; unsigned int old_cx; unsigned int old_cy; unsigned int rupper; unsigned int rlower; unsigned int old_rupper; unsigned int old_rlower; int mode; unsigned char *tabs; struct screen_sel sel; }

user_defined_types: struct cmd_entry { char *name; char *alias; char *usage; int flags; void (*init)(struct cmd *, int); int (*parse)(struct cmd *, int, char **, char **); int (*exec)(struct cmd *, struct cmd_ctx *); void (*send)(struct cmd *, struct buffer *); void (*recv)(struct cmd *, struct buffer *); void (*free)(struct cmd *); unsigned long (*print)(struct cmd *, char *, unsigned long); }

user_defined_types: struct tty_keys { struct tty_key *rbh_root; }

user_defined_types: struct session { char *name; struct timeval tv; unsigned int sx; unsigned int sy; struct winlink *curw; struct winlink_stack lastw; struct winlinks windows; struct options options; struct paste_stack buffers; struct { struct session_alert *slh_first; } alerts; int flags; }

user_defined_types: struct grid { unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; unsigned int *size; struct grid_cell **data; unsigned int *usize; struct grid_utf8 **udata; }

user_defined_types: struct paste_stack { struct paste_buffer **list; unsigned int num; unsigned long space; }

user_defined_types: struct winlink { int idx; struct window *window; struct { struct winlink *rbe_left; struct winlink *rbe_right; struct winlink *rbe_parent; int rbe_color; } entry; struct { struct winlink *sle_next; } sentry; }

user_defined_types: struct cmd_ctx { struct client *cmdclient; struct client *curclient; struct session *cursession; struct msg_command_data *msgdata; void (*print)(struct cmd_ctx *, char *, ...); void (*info)(struct cmd_ctx *, char *, ...); void (*error)(struct cmd_ctx *, char *, ...); }

user_defined_types: struct window { char *name; struct timeval name_timer; struct window_pane *active; struct window_panes panes; unsigned int layout; unsigned int sx; unsigned int sy; int flags; struct options options; unsigned int references; }

user_defined_types: struct options { struct options_tree tree; struct options *parent; }

user_defined_types: struct window_pane { struct window *window; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; int flags; char *cmd; char *cwd; int pid; int fd; char tty[32]; struct buffer *in; struct buffer *out; struct input_ctx ictx; struct screen *screen; struct screen base; struct window_mode *mode; void *modedata; struct { struct window_pane *tqe_next; struct window_pane **tqe_prev; } entry; }

user_defined_types: struct input_ctx { struct window_pane *wp; struct screen_write_ctx ctx; unsigned char *buf; unsigned long len; unsigned long off; struct grid_cell cell; struct grid_cell saved_cell; unsigned int saved_cx; unsigned int saved_cy; unsigned char *string_buf; unsigned long string_len; int string_type; unsigned char utf8_buf[4]; unsigned int utf8_len; unsigned int utf8_off; unsigned char intermediate; void *(*state)(unsigned char, struct input_ctx *); unsigned char private; struct { struct input_arg *list; unsigned int num; unsigned long space; } args; }


original_code: void mode_key_tree_SPLAY_MINMAX(struct mode_key_tree *head, int __comp) { struct mode_key_binding __node, *__left, *__right, *__tmp; (&__node)->entry.spe_left = (&__node)->entry.spe_right = ((void *)0); __left = __right = &__node; while (1) { if (__comp < 0) { __tmp = ((head)->sph_root)->entry.spe_left; if (__tmp == ((void *)0)) break; if (__comp < 0){ do { ((head)->sph_root)->entry.spe_left = (__tmp)->entry.spe_right; (__tmp)->entry.spe_right = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_left == ((void *)0)) break; } do { (__right)->entry.spe_left = (head)->sph_root; __right = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_left; } while (0); } else if (__comp > 0) { __tmp = ((head)->sph_root)->entry.spe_right; if (__tmp == ((void *)0)) break; if (__comp > 0) { do { ((head)->sph_root)->entry.spe_right = (__tmp)->entry.spe_left; (__tmp)->entry.spe_left = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_right == ((void *)0)) break; } do { (__left)->entry.spe_right = (head)->sph_root; __left = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_right; } while (0); } } do { (__left)->entry.spe_right = ((head)->sph_root)->entry.spe_left; (__right)->entry.spe_left = ((head)->sph_root)->entry.spe_right; ((head)->sph_root)->entry.spe_left = (&__node)->entry.spe_right; ((head)->sph_root)->entry.spe_right = (&__node)->entry.spe_left; } while (0); }

canonical_original_code: void mode_key_tree_SPLAY_MINMAX(struct mode_key_tree *head, int __comp) { struct mode_key_binding __node; struct mode_key_binding *__left; struct mode_key_binding *__right; struct mode_key_binding *__tmp; (&__node)->entry.spe_left = (&__node)->entry.spe_right = ((void *)0); __left = __right = &__node; while (1) { if (__comp < 0) { __tmp = ((head)->sph_root)->entry.spe_left; if (__tmp == ((void *)0)) break; if (__comp < 0){ do { ((head)->sph_root)->entry.spe_left = (__tmp)->entry.spe_right; (__tmp)->entry.spe_right = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_left == ((void *)0)) break; } do { (__right)->entry.spe_left = (head)->sph_root; __right = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_left; } while (0); } else if (__comp > 0) { __tmp = ((head)->sph_root)->entry.spe_right; if (__tmp == ((void *)0)) break; if (__comp > 0) { do { ((head)->sph_root)->entry.spe_right = (__tmp)->entry.spe_left; (__tmp)->entry.spe_left = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_right == ((void *)0)) break; } do { (__left)->entry.spe_right = (head)->sph_root; __left = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_right; } while (0); } } do { (__left)->entry.spe_right = ((head)->sph_root)->entry.spe_left; (__right)->entry.spe_left = ((head)->sph_root)->entry.spe_right; ((head)->sph_root)->entry.spe_left = (&__node)->entry.spe_right; ((head)->sph_root)->entry.spe_right = (&__node)->entry.spe_left; } while (0); }

variable_types: struct mode_key_tree { struct mode_key_binding { int key; int mode; enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; } *sph_root; } *

user_defined_types: struct mode_key_binding { int key; int mode; enum mode_key_cmd cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; }

user_defined_types: struct mode_key_tree { struct mode_key_binding *sph_root; }


decompiled_code:
__int64 __fastcall mode_key_cmp(_DWORD *a1, _DWORD *a2)
{
int v2; // eax
int v3; // edx
__int64 result; // rax

v2 = a1[1];
v3 = a2[1];
if ( v2 == v3 )
result = (unsigned int)(*a1 - *a2);
else
result = (unsigned int)(v2 - v3);
return result;
}

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

v2 = a1[1];
v3 = a2[1];
if ( v2 == v3 )
result = (unsigned int)(*a1 - *a2);
else
result = (unsigned int)(v2 - v3);
return result;
}

original_code:
int
mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
{
 if (mbind1->mode != mbind2->mode)
  return (mbind1->mode - mbind2->mode);
 return (mbind1->key - mbind2->key);
}

variable_types: struct mode_key_binding { int key; int mode; enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; } *


original_code: void mode_key_tree_SPLAY(struct mode_key_tree *head, struct mode_key_binding *elm) { struct mode_key_binding __node, *__left, *__right, *__tmp; int __comp; (&__node)->entry.spe_left = (&__node)->entry.spe_right = ((void *)0); __left = __right = &__node; while ((__comp = (mode_key_cmp)(elm, (head)->sph_root))) { if (__comp < 0) { __tmp = ((head)->sph_root)->entry.spe_left; if (__tmp == ((void *)0)) break; if ((mode_key_cmp)(elm, __tmp) < 0){ do { ((head)->sph_root)->entry.spe_left = (__tmp)->entry.spe_right; (__tmp)->entry.spe_right = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_left == ((void *)0)) break; } do { (__right)->entry.spe_left = (head)->sph_root; __right = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_left; } while (0); } else if (__comp > 0) { __tmp = ((head)->sph_root)->entry.spe_right; if (__tmp == ((void *)0)) break; if ((mode_key_cmp)(elm, __tmp) > 0){ do { ((head)->sph_root)->entry.spe_right = (__tmp)->entry.spe_left; (__tmp)->entry.spe_left = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_right == ((void *)0)) break; } do { (__left)->entry.spe_right = (head)->sph_root; __left = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_right; } while (0); } } do { (__left)->entry.spe_right = ((head)->sph_root)->entry.spe_left; (__right)->entry.spe_left = ((head)->sph_root)->entry.spe_right; ((head)->sph_root)->entry.spe_left = (&__node)->entry.spe_right; ((head)->sph_root)->entry.spe_right = (&__node)->entry.spe_left; } while (0); }

canonical_original_code: void mode_key_tree_SPLAY(struct mode_key_tree *head, struct mode_key_binding *elm) { struct mode_key_binding __node; struct mode_key_binding *__left; struct mode_key_binding *__right; struct mode_key_binding *__tmp; int __comp; (&__node)->entry.spe_left = (&__node)->entry.spe_right = ((void *)0); __left = __right = &__node; while ((__comp = (mode_key_cmp)(elm, (head)->sph_root))) { if (__comp < 0) { __tmp = ((head)->sph_root)->entry.spe_left; if (__tmp == ((void *)0)) break; if ((mode_key_cmp)(elm, __tmp) < 0){ do { ((head)->sph_root)->entry.spe_left = (__tmp)->entry.spe_right; (__tmp)->entry.spe_right = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_left == ((void *)0)) break; } do { (__right)->entry.spe_left = (head)->sph_root; __right = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_left; } while (0); } else if (__comp > 0) { __tmp = ((head)->sph_root)->entry.spe_right; if (__tmp == ((void *)0)) break; if ((mode_key_cmp)(elm, __tmp) > 0){ do { ((head)->sph_root)->entry.spe_right = (__tmp)->entry.spe_left; (__tmp)->entry.spe_left = (head)->sph_root; (head)->sph_root = __tmp; } while (0); if (((head)->sph_root)->entry.spe_right == ((void *)0)) break; } do { (__left)->entry.spe_right = (head)->sph_root; __left = (head)->sph_root; (head)->sph_root = ((head)->sph_root)->entry.spe_right; } while (0); } } do { (__left)->entry.spe_right = ((head)->sph_root)->entry.spe_left; (__right)->entry.spe_left = ((head)->sph_root)->entry.spe_right; ((head)->sph_root)->entry.spe_left = (&__node)->entry.spe_right; ((head)->sph_root)->entry.spe_right = (&__node)->entry.spe_left; } while (0); }

variable_types:
elm
struct mode_key_binding *

variable_types: struct mode_key_binding { int key; int mode; enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; }


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

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

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

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

original_code:
const char *
mode_key_tostring(struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
{
 for (; cmdstr->name != ((void *)0); cmdstr++) {
  if (cmdstr->cmd == cmd)
   return (cmdstr->name);
 }
 return (((void *)0));
}

canonical_original_code:
char *mode_key_tostring(struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
{
 for (; cmdstr->name != ((void *)0); cmdstr++) {
  if (cmdstr->cmd == cmd)
   return (cmdstr->name);
 }
 return (((void *)0));
}

variable_types: struct mode_key_cmdstr { enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; char *name; } *

user_defined_types: struct mode_key_cmdstr { enum mode_key_cmd cmd; char *name; }


decompiled_code:
__int64 __fastcall mode_key_fromstring(__int64 a1, const char *a2)
{
_QWORD *v2; // rbx
const char *v3; // rdi

v2 = (_QWORD *)a1;
v3 = *(const char **)(a1 + 8);
if ( !v3 )
return 0LL;
while ( strcasecmp(v3, a2) )
{
v3 = (const char *)v2[3];
v2 += 2;
if ( !v3 )
return 0LL;
}
return *(unsigned int *)v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, const char *a2)
{
_QWORD *v2; // rbx
const char *v3; // rdi

v2 = (_QWORD *)a1;
v3 = *(const char **)(a1 + 8);
if ( !v3 )
return 0LL;
while ( strcasecmp(v3, a2) )
{
v3 = (const char *)v2[3];
v2 += 2;
if ( !v3 )
return 0LL;
}
return *(unsigned int *)v2;
}

original_code:
enum mode_key_cmd
mode_key_fromstring(struct mode_key_cmdstr *cmdstr, const char *name)
{
 for (; cmdstr->name != ((void *)0); cmdstr++) {
  if (strcasecmp(cmdstr->name, name) == 0)
   return (cmdstr->cmd);
 }
 return (MODEKEY_NONE);
}

canonical_original_code:
enum mode_key_cmd mode_key_fromstring(struct mode_key_cmdstr *cmdstr, char *name)
{
 for (; cmdstr->name != ((void *)0); cmdstr++) {
  if (strcasecmp(cmdstr->name, name) == 0)
   return (cmdstr->cmd);
 }
 return (MODEKEY_NONE);
}

variable_types:
name
char *

return_type: enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 }


decompiled_code:
char **__fastcall mode_key_findtable(char *s1)
{
const char *v1; // rsi
char **v2; // r12

v1 = "vi-edit";
v2 = &mode_key_tables;
do
{
if ( !strcasecmp(s1, v1) )
return v2;
v1 = v2[4];
v2 += 4;
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
char **__fastcall func7(char *s1)
{
const char *v1; // rsi
char **v2; // r12

v1 = "vi-edit";
v2 = &mode_key_tables;
do
{
if ( !strcasecmp(s1, v1) )
return v2;
v1 = v2[4];
v2 += 4;
}
while ( v1 );
return 0LL;
}

original_code:
const struct mode_key_table *
mode_key_findtable(const char *name)
{
 const struct mode_key_table *mtab;
 for (mtab = mode_key_tables; mtab->name != ((void *)0); mtab++) {
  if (strcasecmp(name, mtab->name) == 0)
   return (mtab);
 }
 return (((void *)0));
}

canonical_original_code:
struct mode_key_table *mode_key_findtable(char *name)
{
 struct mode_key_table *mtab;
 for (mtab = mode_key_tables; mtab->name != ((void *)0); mtab++) {
  if (strcasecmp(name, mtab->name) == 0)
   return (mtab);
 }
 return (((void *)0));
}

variable_types: struct mode_key_table { char *name; struct mode_key_cmdstr { enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; char *name; } *cmdstr; struct mode_key_tree { struct mode_key_binding { int key; int mode; enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; } *sph_root; } *tree; struct mode_key_entry { int key; int mode; enum mode_key_cmd { MODEKEY_NONE=0, MODEKEY_OTHER=1, MODEKEYEDIT_BACKSPACE=2, MODEKEYEDIT_CANCEL=3, MODEKEYEDIT_COMPLETE=4, MODEKEYEDIT_CURSORLEFT=5, MODEKEYEDIT_CURSORRIGHT=6, MODEKEYEDIT_DELETE=7, MODEKEYEDIT_DELETELINE=8, MODEKEYEDIT_DELETETOENDOFLINE=9, MODEKEYEDIT_ENDOFLINE=10, MODEKEYEDIT_ENTER=11, MODEKEYEDIT_HISTORYDOWN=12, MODEKEYEDIT_HISTORYUP=13, MODEKEYEDIT_PASTE=14, MODEKEYEDIT_STARTOFLINE=15, MODEKEYEDIT_SWITCHMODE=16, MODEKEYEDIT_SWITCHMODEAPPEND=17, MODEKEYEDIT_TRANSPOSECHARS=18, MODEKEYCHOICE_CANCEL=19, MODEKEYCHOICE_CHOOSE=20, MODEKEYCHOICE_DOWN=21, MODEKEYCHOICE_PAGEDOWN=22, MODEKEYCHOICE_PAGEUP=23, MODEKEYCHOICE_UP=24, MODEKEYCOPY_BACKTOINDENTATION=25, MODEKEYCOPY_BOTTOMLINE=26, MODEKEYCOPY_CANCEL=27, MODEKEYCOPY_CLEARSELECTION=28, MODEKEYCOPY_COPYSELECTION=29, MODEKEYCOPY_DOWN=30, MODEKEYCOPY_ENDOFLINE=31, MODEKEYCOPY_GOTOLINE=32, MODEKEYCOPY_HALFPAGEDOWN=33, MODEKEYCOPY_HALFPAGEUP=34, MODEKEYCOPY_LEFT=35, MODEKEYCOPY_MIDDLELINE=36, MODEKEYCOPY_NEXTPAGE=37, MODEKEYCOPY_NEXTWORD=38, MODEKEYCOPY_PREVIOUSPAGE=39, MODEKEYCOPY_PREVIOUSWORD=40, MODEKEYCOPY_RIGHT=41, MODEKEYCOPY_SCROLLDOWN=42, MODEKEYCOPY_SCROLLUP=43, MODEKEYCOPY_SEARCHAGAIN=44, MODEKEYCOPY_SEARCHDOWN=45, MODEKEYCOPY_SEARCHUP=46, MODEKEYCOPY_STARTOFLINE=47, MODEKEYCOPY_STARTSELECTION=48, MODEKEYCOPY_TOPLINE=49, MODEKEYCOPY_UP=50 } cmd; } *table; } *

user_defined_types: struct mode_key_table { char *name; struct mode_key_cmdstr *cmdstr; struct mode_key_tree *tree; struct mode_key_entry *table; }

user_defined_types: struct mode_key_entry { int key; int mode; enum mode_key_cmd cmd; }