decompiled_code:
__int64 server_msg_command_print(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-2D0h] BYREF
char v4[512]; // [rsp+20h] [rbp-2B8h] BYREF

va_start(va, a2);
xvsnprintf(v4, 512LL, a2, va);
return server_write_client(*(_QWORD *)(a1 + 8), 7LL, v4, 512LL);
}

canonical_decompiled_code:
__int64 func1(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-2D0h] BYREF
char v4[512]; // [rsp+20h] [rbp-2B8h] BYREF

va_start(va, a2);
xvsnprintf(v4, 512LL, a2, va);
return server_write_client(*(_QWORD *)(a1 + 8), 7LL, v4, 512LL);
}

original_code:
void __attribute__ ((format (printf, 2, 3)))
server_msg_command_print(struct cmd_ctx *ctx, const char *fmt, ...)
{
 struct msg_print_data data;
 va_list ap;
 __builtin_va_start(ap,fmt);
 xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
 __builtin_va_end(ap);
 server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
}

canonical_original_code:
void server_msg_command_print(struct cmd_ctx *ctx, char *fmt, ...)
{
 struct msg_print_data data;
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
 __builtin_va_end(ap);
 server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
}

user_defined_types: struct msg_print_data { char msg[512]; }

user_defined_types: struct cmd_ctx { struct client *curclient; struct client *cmdclient; 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 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 msg_command_data { int pid; unsigned int idx; int argc; char argv[2048]; }

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 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 buf_read { unsigned char buf[65535]; unsigned char *rptr; unsigned long wpos; }

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 screen_sel { int flag; unsigned int sx; unsigned int sy; unsigned int ex; unsigned int ey; struct grid_cell cell; }

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 buffer { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; }

user_defined_types: struct winlink_stack { struct winlink *slh_first; }

user_defined_types: struct winlinks { struct winlink *rbh_root; }

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

user_defined_types: struct imsg_fd { struct { struct imsg_fd *tqe_next; struct imsg_fd **tqe_prev; } entry; int fd; }

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

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

user_defined_types: struct session_alert { struct winlink *wl; int type; struct { struct session_alert *sle_next; } entry; }

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

user_defined_types: 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_CANCEL=26, MODEKEYCOPY_CLEARSELECTION=27, MODEKEYCOPY_COPYSELECTION=28, MODEKEYCOPY_DOWN=29, MODEKEYCOPY_ENDOFLINE=30, MODEKEYCOPY_GOTOLINE=31, MODEKEYCOPY_HALFPAGEDOWN=32, MODEKEYCOPY_HALFPAGEUP=33, MODEKEYCOPY_LEFT=34, MODEKEYCOPY_NEXTPAGE=35, MODEKEYCOPY_NEXTWORD=36, MODEKEYCOPY_PREVIOUSPAGE=37, MODEKEYCOPY_PREVIOUSWORD=38, MODEKEYCOPY_RIGHT=39, MODEKEYCOPY_SEARCHAGAIN=40, MODEKEYCOPY_SEARCHDOWN=41, MODEKEYCOPY_SEARCHUP=42, MODEKEYCOPY_STARTOFLINE=43, MODEKEYCOPY_STARTSELECTION=44, MODEKEYCOPY_UP=45 }

user_defined_types: struct paste_buffer { char *data; unsigned long size; struct timeval tv; }

user_defined_types: struct grid_utf8 { unsigned char width; unsigned char data[8]; }

user_defined_types: struct window_panes { struct window_pane *tqh_first; struct window_pane **tqh_last; }

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

user_defined_types: struct options_tree { struct options_entry *sph_root; }

user_defined_types: struct screen_write_ctx { struct window_pane *wp; struct screen *s; }

user_defined_types: 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 *); }

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

user_defined_types: enum layout_type { LAYOUT_LEFTRIGHT=0, LAYOUT_TOPBOTTOM=1, LAYOUT_WINDOWPANE=2 }

user_defined_types: struct layout_cells { struct layout_cell *tqh_first; struct layout_cell **tqh_last; }

user_defined_types: struct input_arg { unsigned char data[64]; unsigned long used; }


repo: ThomasAdam/tmux-ARCHIVED

decompiled_code:
__int64 server_msg_command_info(__int64 a1, __int64 a2, ...)
{
__int64 result; // rax
gcc_va_list va; // [rsp+8h] [rbp-2D0h] BYREF
char v4[512]; // [rsp+20h] [rbp-2B8h] BYREF

if ( !be_quiet )
{
va_start(va, a2);
xvsnprintf(v4, 512LL, a2, va);
result = server_write_client(*(_QWORD *)(a1 + 8), 7LL, v4, 512LL);
}
return result;
}

canonical_decompiled_code:
__int64 func2(__int64 a1, __int64 a2, ...)
{
__int64 result; // rax
gcc_va_list va; // [rsp+8h] [rbp-2D0h] BYREF
char v4[512]; // [rsp+20h] [rbp-2B8h] BYREF

if ( !be_quiet )
{
va_start(va, a2);
xvsnprintf(v4, 512LL, a2, va);
result = server_write_client(*(_QWORD *)(a1 + 8), 7LL, v4, 512LL);
}
return result;
}

original_code:
void __attribute__ ((format (printf, 2, 3)))
server_msg_command_info(struct cmd_ctx *ctx, const char *fmt, ...)
{
 struct msg_print_data data;
 va_list ap;
 if (be_quiet)
  return;
 __builtin_va_start(ap,fmt);
 xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
 __builtin_va_end(ap);
 server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
}

canonical_original_code:
void server_msg_command_info(struct cmd_ctx *ctx, char *fmt, ...)
{
 struct msg_print_data data;
 __builtin_va_list ap;
 if (be_quiet)
  return;
 __builtin_va_start(ap,fmt);
 xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
 __builtin_va_end(ap);
 server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
}

variable_types: struct cmd_ctx { struct client { 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; } ibuf; struct environ { 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; } *rbh_root; } environ; 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 { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; } *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 { int flags; unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; struct grid_line { unsigned int cellsize; struct grid_cell *celldata; unsigned int utf8size; struct grid_utf8 { unsigned char width; unsigned char data[8]; } *utf8data; int flags; } *linedata; } *grid; unsigned int cx; unsigned int cy; unsigned int rupper; unsigned int 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; 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 { 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_CANCEL=26, MODEKEYCOPY_CLEARSELECTION=27, MODEKEYCOPY_COPYSELECTION=28, MODEKEYCOPY_DOWN=29, MODEKEYCOPY_ENDOFLINE=30, MODEKEYCOPY_GOTOLINE=31, MODEKEYCOPY_HALFPAGEDOWN=32, MODEKEYCOPY_HALFPAGEUP=33, MODEKEYCOPY_LEFT=34, MODEKEYCOPY_NEXTPAGE=35, MODEKEYCOPY_NEXTWORD=36, MODEKEYCOPY_PREVIOUSPAGE=37, MODEKEYCOPY_PREVIOUSWORD=38, MODEKEYCOPY_RIGHT=39, MODEKEYCOPY_SEARCHAGAIN=40, MODEKEYCOPY_SEARCHDOWN=41, MODEKEYCOPY_SEARCHUP=42, MODEKEYCOPY_STARTOFLINE=43, MODEKEYCOPY_STARTSELECTION=44, MODEKEYCOPY_UP=45 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; } *sph_root; } *tree; int mode; } 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; struct layout_cell { enum layout_type { LAYOUT_LEFTRIGHT=0, LAYOUT_TOPBOTTOM=1, LAYOUT_WINDOWPANE=2 } type; struct layout_cell *parent; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; struct window_pane *wp; struct layout_cells { struct layout_cell *tqh_first; struct layout_cell **tqh_last; } cells; struct { struct layout_cell *tqe_next; struct layout_cell **tqe_prev; } entry; } *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 { 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; 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 { unsigned char data[64]; unsigned long used; } *list; unsigned int num; unsigned long space; } args; } 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 { 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; int lastlayout; struct layout_cell *layout_root; 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; unsigned long size; 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; struct termios *tio; struct environ environ; int references; } *session; int references; } *curclient; struct client *cmdclient; struct msg_command_data { int pid; unsigned int idx; int argc; char argv[2048]; } *msgdata; void (*print)(struct cmd_ctx *, char *, ...); void (*info)(struct cmd_ctx *, char *, ...); void (*error)(struct cmd_ctx *, char *, ...); } *

variable_types:
fmt
char *

variable_types: struct msg_print_data { char msg[512]; }

return_type: void

user_defined_types: struct cmd_ctx { struct client *curclient; struct client *cmdclient; 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 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 msgbuf { struct { struct buf *tqh_first; struct buf **tqh_last; } bufs; unsigned int queued; int fd; }

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; struct termios *tio; struct environ environ; int references; }

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 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 paste_stack { struct paste_buffer **list; unsigned int num; unsigned long space; }

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


decompiled_code:
__int64 __fastcall server_msg_command(__int64 a1, __int64 a2)
{
unsigned int v2; // er13
_QWORD **v3; // r12
_QWORD *v4; // rax
const char *v6; // [rsp+0h] [rbp-68h] BYREF
_QWORD *v7; // [rsp+8h] [rbp-60h] BYREF
__int64 v8[11]; // [rsp+10h] [rbp-58h] BYREF

server_activity = time(0LL);
v8[5] = (__int64)server_msg_command_error;
v8[3] = (__int64)server_msg_command_print;
v8[4] = (__int64)server_msg_command_info;
v8[2] = a2;
v8[0] = 0LL;
v8[1] = a1;
v2 = *(_DWORD *)(a2 + 8);
*(_BYTE *)(a2 + 2059) = 0;
if ( (unsigned int)cmd_unpack_argv(a2 + 12, 2048LL, v2, &v7) )
{
server_msg_command_error((__int64)v8, (__int64)"command too long");
return server_write_client(a1, 3LL, 0LL, 0LL);
}
if ( !v2 )
{
v7 = (_QWORD *)xcalloc(1LL, 8LL);
*v7 = xstrdup("new-session");
v2 = 1;
}
v3 = (_QWORD **)cmd_list_parse(v2, v7, &v6);
if ( !v3 )
{
server_msg_command_error((__int64)v8, (__int64)"%s", v6);
cmd_free_argv(v2, v7);
return server_write_client(a1, 3LL, 0LL, 0LL);
}
cmd_free_argv(v2, v7);
if ( *(_DWORD *)a2 != -1 )
{
v4 = *v3;
if ( *v3 )
{
while ( (*(_BYTE *)(*v4 + 24LL) & 2) == 0 )
{
v4 = (_QWORD *)v4[2];
if ( !v4 )
goto LABEL_9;
}
server_msg_command_error((__int64)v8, (__int64)"sessions should be nested with care. unset $TMUX to force");
cmd_list_free(v3);
return server_write_client(a1, 3LL, 0LL, 0LL);
}
}
LABEL_9:
if ( (unsigned int)cmd_list_exec(v3, v8) != 1 )
server_write_client(a1, 3LL, 0LL, 0LL);
return cmd_list_free(v3);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
unsigned int v2; // er13
_QWORD **v3; // r12
_QWORD *v4; // rax
const char *v6; // [rsp+0h] [rbp-68h] BYREF
_QWORD *v7; // [rsp+8h] [rbp-60h] BYREF
__int64 v8[11]; // [rsp+10h] [rbp-58h] BYREF

server_activity = time(0LL);
v8[5] = (__int64)server_msg_command_error;
v8[3] = (__int64)server_msg_command_print;
v8[4] = (__int64)server_msg_command_info;
v8[2] = a2;
v8[0] = 0LL;
v8[1] = a1;
v2 = *(_DWORD *)(a2 + 8);
*(_BYTE *)(a2 + 2059) = 0;
if ( (unsigned int)cmd_unpack_argv(a2 + 12, 2048LL, v2, &v7) )
{
func0((__int64)v8, (__int64)"command too long");
return server_write_client(a1, 3LL, 0LL, 0LL);
}
if ( !v2 )
{
v7 = (_QWORD *)xcalloc(1LL, 8LL);
*v7 = xstrdup("new-session");
v2 = 1;
}
v3 = (_QWORD **)cmd_list_parse(v2, v7, &v6);
if ( !v3 )
{
func0((__int64)v8, (__int64)"%s", v6);
cmd_free_argv(v2, v7);
return server_write_client(a1, 3LL, 0LL, 0LL);
}
cmd_free_argv(v2, v7);
if ( *(_DWORD *)a2 != -1 )
{
v4 = *v3;
if ( *v3 )
{
while ( (*(_BYTE *)(*v4 + 24LL) & 2) == 0 )
{
v4 = (_QWORD *)v4[2];
if ( !v4 )
goto LABEL_9;
}
func0((__int64)v8, (__int64)"sessions should be nested with care. unset $TMUX to force");
cmd_list_free(v3);
return server_write_client(a1, 3LL, 0LL, 0LL);
}
}
LABEL_9:
if ( (unsigned int)cmd_list_exec(v3, v8) != 1 )
server_write_client(a1, 3LL, 0LL, 0LL);
return cmd_list_free(v3);
}

original_code:
void
server_msg_command(struct client *c, struct msg_command_data *data)
{
 struct cmd_ctx ctx;
 struct cmd_list *cmdlist = ((void *)0);
 struct cmd *cmd;
 int argc;
 char **argv, *cause;
 server_activity = time(((void *)0));
 ctx.error = server_msg_command_error;
 ctx.print = server_msg_command_print;
 ctx.info = server_msg_command_info;
 ctx.msgdata = data;
 ctx.curclient = ((void *)0);
 ctx.cmdclient = c;
 argc = data->argc;
 data->argv[(sizeof data->argv) - 1] = '\0';
 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
  server_msg_command_error(&ctx, "command too long");
  goto error;
 }
 if (argc == 0) {
  argc = 1;
  argv = xcalloc(1, sizeof *argv);
  *argv = xstrdup("new-session");
 }
 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == ((void *)0)) {
  server_msg_command_error(&ctx, "%s", cause);
  cmd_free_argv(argc, argv);
  goto error;
 }
 cmd_free_argv(argc, argv);
 if (data->pid != -1) {
  for((cmd) = ((cmdlist)->tqh_first); (cmd) != ((void *)0); (cmd) = ((cmd)->qentry.tqe_next)) {
   if (cmd->entry->flags & 0x2) {
    server_msg_command_error(&ctx,
        "sessions should be nested with care. "
        "unset $TMUX to force");
    goto error;
   }
  }
 }
 if (cmd_list_exec(cmdlist, &ctx) != 1)
  server_write_client(c, MSG_EXIT, ((void *)0), 0);
 cmd_list_free(cmdlist);
 return;
error:
 if (cmdlist != ((void *)0))
  cmd_list_free(cmdlist);
 server_write_client(c, MSG_EXIT, ((void *)0), 0);
}

canonical_original_code:
void server_msg_command(struct client *c, struct msg_command_data *data)
{
 struct cmd_ctx ctx;
 struct cmd_list *cmdlist = ((void *)0);
 struct cmd *cmd;
 int argc;
 char **argv; char *cause;
 server_activity = time(((void *)0));
 ctx.error = server_msg_command_error;
 ctx.print = server_msg_command_print;
 ctx.info = server_msg_command_info;
 ctx.msgdata = data;
 ctx.curclient = ((void *)0);
 ctx.cmdclient = c;
 argc = data->argc;
 data->argv[(sizeof data->argv) - 1] = '\0';
 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
  server_msg_command_error(&ctx, "command too long");
  goto error;
 }
 if (argc == 0) {
  argc = 1;
  argv = xcalloc(1, sizeof *argv);
  *argv = xstrdup("new-session");
 }
 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == ((void *)0)) {
  server_msg_command_error(&ctx, "%s", cause);
  cmd_free_argv(argc, argv);
  goto error;
 }
 cmd_free_argv(argc, argv);
 if (data->pid != -1) {
  for((cmd) = ((cmdlist)->tqh_first); (cmd) != ((void *)0); (cmd) = ((cmd)->qentry.tqe_next)) {
   if (cmd->entry->flags & 0x2) {
    server_msg_command_error(&ctx,
        "sessions should be nested with care. "
        "unset $TMUX to force");
    goto error;
   }
  }
 }
 if (cmd_list_exec(cmdlist, &ctx) != 1)
  server_write_client(c, MSG_EXIT, ((void *)0), 0);
 cmd_list_free(cmdlist);
 return;
error:
 if (cmdlist != ((void *)0))
  cmd_list_free(cmdlist);
 server_write_client(c, MSG_EXIT, ((void *)0), 0);
}

variable_types: struct cmd_list { struct cmd { struct cmd_entry { char *name; char *alias; char *usage; int flags; unsigned long chflags; void (*init)(struct cmd *, int); int (*parse)(struct cmd *, int, char **, char **); int (*exec)(struct cmd *, struct cmd_ctx { struct client { 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; } ibuf; struct environ { 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; } *rbh_root; } environ; 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 { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; } *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 { int flags; unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; struct grid_line { unsigned int cellsize; struct grid_cell *celldata; unsigned int utf8size; struct grid_utf8 { unsigned char width; unsigned char data[8]; } *utf8data; int flags; } *linedata; } *grid; unsigned int cx; unsigned int cy; unsigned int rupper; unsigned int 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; 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 { 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_CANCEL=26, MODEKEYCOPY_CLEARSELECTION=27, MODEKEYCOPY_COPYSELECTION=28, MODEKEYCOPY_DOWN=29, MODEKEYCOPY_ENDOFLINE=30, MODEKEYCOPY_GOTOLINE=31, MODEKEYCOPY_HALFPAGEDOWN=32, MODEKEYCOPY_HALFPAGEUP=33, MODEKEYCOPY_LEFT=34, MODEKEYCOPY_NEXTPAGE=35, MODEKEYCOPY_NEXTWORD=36, MODEKEYCOPY_PREVIOUSPAGE=37, MODEKEYCOPY_PREVIOUSWORD=38, MODEKEYCOPY_RIGHT=39, MODEKEYCOPY_SEARCHAGAIN=40, MODEKEYCOPY_SEARCHDOWN=41, MODEKEYCOPY_SEARCHUP=42, MODEKEYCOPY_STARTOFLINE=43, MODEKEYCOPY_STARTSELECTION=44, MODEKEYCOPY_UP=45 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; } *sph_root; } *tree; int mode; } 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; struct layout_cell { enum layout_type { LAYOUT_LEFTRIGHT=0, LAYOUT_TOPBOTTOM=1, LAYOUT_WINDOWPANE=2 } type; struct layout_cell *parent; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; struct window_pane *wp; struct layout_cells { struct layout_cell *tqh_first; struct layout_cell **tqh_last; } cells; struct { struct layout_cell *tqe_next; struct layout_cell **tqe_prev; } entry; } *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 { 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; 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 { unsigned char data[64]; unsigned long used; } *list; unsigned int num; unsigned long space; } args; } 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 { 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; int lastlayout; struct layout_cell *layout_root; 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; unsigned long size; 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; struct termios *tio; struct environ environ; int references; } *session; int references; } *curclient; struct client *cmdclient; struct msg_command_data { int pid; unsigned int idx; int argc; char argv[2048]; } *msgdata; void (*print)(struct cmd_ctx *, char *, ...); void (*info)(struct cmd_ctx *, char *, ...); void (*error)(struct cmd_ctx *, char *, ...); } *); 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; } *tqh_first; struct cmd **tqh_last; } *

variable_types: argc int

variable_types:
argv
char **

variable_types:
cause
char *

user_defined_types: struct cmd_ctx { struct client *curclient; struct client *cmdclient; 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 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 cmd_list { struct cmd *tqh_first; struct cmd **tqh_last; }

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 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 cmd_entry { char *name; char *alias; char *usage; int flags; unsigned long chflags; void (*init)(struct cmd *, int); int (*parse)(struct cmd *, int, char **, char **); int (*exec)(struct cmd *, struct cmd_ctx *); void (*free)(struct cmd *); unsigned long (*print)(struct cmd *, char *, unsigned long); }

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 options { struct options_tree tree; struct options *parent; }

user_defined_types: struct options_tree { struct options_entry *sph_root; }


original_code:
void
server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
{
 c->tty.sx = data->sx;
 if (c->tty.sx == 0)
  c->tty.sx = 80;
 c->tty.sy = data->sy;
 if (c->tty.sy == 0)
  c->tty.sy = 24;
 c->cwd = ((void *)0);
 data->cwd[(sizeof data->cwd) - 1] = '\0';
 if (*data->cwd != '\0')
  c->cwd = xstrdup(data->cwd);
 data->tty[(sizeof data->tty) - 1] = '\0';
 data->term[(sizeof data->term) - 1] = '\0';
 tty_init(&c->tty, fd, data->tty, data->term);
 if (data->flags & 0x1)
  c->tty.flags |= 0x8;
 if (data->flags & 0x2)
  c->tty.term_flags |= 0x2;
 else if (data->flags & 0x4)
  c->tty.term_flags |= 0x4;
 if (data->flags & 0x8)
  c->tty.term_flags |= 0x1;
 c->flags |= 0x1;
}

variable_types: struct client { 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; } ibuf; struct environ { 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; } *rbh_root; } environ; 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 { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; } *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 { int flags; unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; struct grid_line { unsigned int cellsize; struct grid_cell *celldata; unsigned int utf8size; struct grid_utf8 { unsigned char width; unsigned char data[8]; } *utf8data; int flags; } *linedata; } *grid; unsigned int cx; unsigned int cy; unsigned int rupper; unsigned int 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; 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 { 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_CANCEL=26, MODEKEYCOPY_CLEARSELECTION=27, MODEKEYCOPY_COPYSELECTION=28, MODEKEYCOPY_DOWN=29, MODEKEYCOPY_ENDOFLINE=30, MODEKEYCOPY_GOTOLINE=31, MODEKEYCOPY_HALFPAGEDOWN=32, MODEKEYCOPY_HALFPAGEUP=33, MODEKEYCOPY_LEFT=34, MODEKEYCOPY_NEXTPAGE=35, MODEKEYCOPY_NEXTWORD=36, MODEKEYCOPY_PREVIOUSPAGE=37, MODEKEYCOPY_PREVIOUSWORD=38, MODEKEYCOPY_RIGHT=39, MODEKEYCOPY_SEARCHAGAIN=40, MODEKEYCOPY_SEARCHDOWN=41, MODEKEYCOPY_SEARCHUP=42, MODEKEYCOPY_STARTOFLINE=43, MODEKEYCOPY_STARTSELECTION=44, MODEKEYCOPY_UP=45 } cmd; struct { struct mode_key_binding *spe_left; struct mode_key_binding *spe_right; } entry; } *sph_root; } *tree; int mode; } 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; struct layout_cell { enum layout_type { LAYOUT_LEFTRIGHT=0, LAYOUT_TOPBOTTOM=1, LAYOUT_WINDOWPANE=2 } type; struct layout_cell *parent; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; struct window_pane *wp; struct layout_cells { struct layout_cell *tqh_first; struct layout_cell **tqh_last; } cells; struct { struct layout_cell *tqe_next; struct layout_cell **tqe_prev; } entry; } *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 { 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; 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 { unsigned char data[64]; unsigned long used; } *list; unsigned int num; unsigned long space; } args; } 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 { 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; int lastlayout; struct layout_cell *layout_root; 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; unsigned long size; 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; struct termios *tio; struct environ environ; int references; } *session; int references; } *

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: fd int

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 msgbuf { struct { struct buf *tqh_first; struct buf **tqh_last; } bufs; unsigned int queued; int fd; }

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; struct termios *tio; struct environ environ; int references; }

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 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 paste_stack { struct paste_buffer **list; unsigned int num; unsigned long space; }

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


original_code:
void
server_msg_resize(struct client *c, struct msg_resize_data *data)
{
 c->tty.sx = data->sx;
 if (c->tty.sx == 0)
  c->tty.sx = 80;
 c->tty.sy = data->sy;
 if (c->tty.sy == 0)
  c->tty.sy = 24;
 c->tty.cx = (0x7fffffff * 2U + 1U);
 c->tty.cy = (0x7fffffff * 2U + 1U);
 c->tty.rupper = (0x7fffffff * 2U + 1U);
 c->tty.rlower = (0x7fffffff * 2U + 1U);
 recalculate_sizes();
 server_redraw_client(c);
}

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

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 buf_read { unsigned char buf[65535]; unsigned char *rptr; unsigned long wpos; }

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 screen_sel { int flag; unsigned int sx; unsigned int sy; unsigned int ex; unsigned int ey; struct grid_cell cell; }

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 buffer { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; }

user_defined_types: struct winlink_stack { struct winlink *slh_first; }

user_defined_types: struct winlinks { struct winlink *rbh_root; }

user_defined_types: struct imsg_fd { struct { struct imsg_fd *tqe_next; struct imsg_fd **tqe_prev; } entry; int fd; }

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

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

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 session_alert { struct winlink *wl; int type; struct { struct session_alert *sle_next; } entry; }

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

user_defined_types: 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_CANCEL=26, MODEKEYCOPY_CLEARSELECTION=27, MODEKEYCOPY_COPYSELECTION=28, MODEKEYCOPY_DOWN=29, MODEKEYCOPY_ENDOFLINE=30, MODEKEYCOPY_GOTOLINE=31, MODEKEYCOPY_HALFPAGEDOWN=32, MODEKEYCOPY_HALFPAGEUP=33, MODEKEYCOPY_LEFT=34, MODEKEYCOPY_NEXTPAGE=35, MODEKEYCOPY_NEXTWORD=36, MODEKEYCOPY_PREVIOUSPAGE=37, MODEKEYCOPY_PREVIOUSWORD=38, MODEKEYCOPY_RIGHT=39, MODEKEYCOPY_SEARCHAGAIN=40, MODEKEYCOPY_SEARCHDOWN=41, MODEKEYCOPY_SEARCHUP=42, MODEKEYCOPY_STARTOFLINE=43, MODEKEYCOPY_STARTSELECTION=44, MODEKEYCOPY_UP=45 }

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 paste_buffer { char *data; unsigned long size; struct timeval tv; }

user_defined_types: struct grid_utf8 { unsigned char width; unsigned char data[8]; }

user_defined_types: struct window_panes { struct window_pane *tqh_first; struct window_pane **tqh_last; }

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

user_defined_types: struct screen_write_ctx { struct window_pane *wp; struct screen *s; }

user_defined_types: 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 *); }

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

user_defined_types: enum layout_type { LAYOUT_LEFTRIGHT=0, LAYOUT_TOPBOTTOM=1, LAYOUT_WINDOWPANE=2 }

user_defined_types: struct layout_cells { struct layout_cell *tqh_first; struct layout_cell **tqh_last; }

user_defined_types: struct input_arg { unsigned char data[64]; unsigned long used; }


decompiled_code:
__int64 __fastcall server_msg_dispatch(__int64 a1)
{
__int64 v1; // rax
int v3; // eax
__int64 v4; // rax
__int64 v5; // rbp
char v6[1024]; // [rsp+0h] [rbp-1D88h] BYREF
__int128 v7[8]; // [rsp+400h] [rbp-1988h] BYREF
__int64 v8; // [rsp+488h] [rbp-1900h] BYREF
char v9[4272]; // [rsp+490h] [rbp-18F8h] BYREF
char v10[2064]; // [rsp+1540h] [rbp-848h] BYREF
unsigned int v11; // [rsp+1D50h] [rbp-38h] BYREF
unsigned __int16 v12; // [rsp+1D54h] [rbp-34h]
int v13; // [rsp+1D58h] [rbp-30h]
unsigned int v14; // [rsp+1D60h] [rbp-28h]
const __m128i *v15; // [rsp+1D68h] [rbp-20h]

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

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v1; // rax
int v3; // eax
__int64 v4; // rax
__int64 v5; // rbp
char v6[1024]; // [rsp+0h] [rbp-1D88h] BYREF
__int128 v7[8]; // [rsp+400h] [rbp-1988h] BYREF
__int64 v8; // [rsp+488h] [rbp-1900h] BYREF
char v9[4272]; // [rsp+490h] [rbp-18F8h] BYREF
char v10[2064]; // [rsp+1540h] [rbp-848h] BYREF
unsigned int v11; // [rsp+1D50h] [rbp-38h] BYREF
unsigned __int16 v12; // [rsp+1D54h] [rbp-34h]
int v13; // [rsp+1D58h] [rbp-30h]
unsigned int v14; // [rsp+1D60h] [rbp-28h]
const __m128i *v15; // [rsp+1D68h] [rbp-20h]

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

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 winlinks { struct winlink *rbh_root; }

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

user_defined_types: struct imsg_fd { struct { struct imsg_fd *tqe_next; struct imsg_fd **tqe_prev; } entry; int fd; }

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

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

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 options { struct options_tree tree; struct options *parent; }

user_defined_types: struct options_tree { struct options_entry *sph_root; }


decompiled_code:
char *__fastcall client_fill_session(_DWORD *a1)
{
char *result; // rax
unsigned __int64 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 )
{
v2 = (unsigned __int64)result;
result = strrchr(result, 44);
if ( (char *)v2 != result )
{
if ( result )
{
v3 = result - 1;
if ( v2 >= (unsigned __int64)(result - 1) )
{
LABEL_7:
if ( *v3 != 44 )
return result;
}
else
{
while ( *v3 != 44 )
{
if ( (char *)v2 == --v3 )
goto LABEL_7;
}
}
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
unsigned __int64 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 )
{
v2 = (unsigned __int64)result;
result = strrchr(result, 44);
if ( (char *)v2 != result )
{
if ( result )
{
v3 = result - 1;
if ( v2 >= (unsigned __int64)(result - 1) )
{
LABEL_7:
if ( *v3 != 44 )
return result;
}
else
{
while ( *v3 != 44 )
{
if ( (char *)v2 == --v3 )
goto LABEL_7;
}
}
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


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

v11 = a2;
v12 = a4 + a6;
result = buffer_write(*(_QWORD *)(a1 + 16), &v11, 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 result; // rax
int v11; // [rsp+0h] [rbp-38h] BYREF
__int64 v12; // [rsp+8h] [rbp-30h]

v11 = a2;
v12 = a4 + a6;
result = buffer_write(*(_QWORD *)(a1 + 16), &v11, 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
__int16 v2; // ax
unsigned __int8 v3; // dl
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
int v1; // ecx
__int16 v2; // ax
unsigned __int8 v3; // dl
__int64 result; // rax

v1 = a1[1];
if ( (_BYTE)v1 == 0xFF )
return *a1;
v2 = a1[2];
if ( (_BYTE)v2 == 0xFF )
return v1 & 0x3F | (*a1 << 6) & 0x7C0u;
v3 = a1[3];
if ( v3 == 0xFF )
result = v2 & 0x3F | ((_WORD)v1 << 6) & 0xFC0 | (unsigned int)(unsigned __int16)(*a1 << 12);
else
result = v3 & 0x3F | (v2 << 6) & 0xFC0 | (v1 << 12) & 0x3F000 | (*a1 << 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
unsigned int i; // eax

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

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

v1 = &utf8_width_table;
for ( i = 0; i != 156; ++i )
{
if ( *(_DWORD *)v1 <= a1 && *((_DWORD *)v1 + 1) >= a1 )
return *((unsigned int *)&utf8_width_table + 3 * i + 2);
v1 = (void **)((char *)v1 + 12);
}
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
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

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

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

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

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; // rax
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)");
goto LABEL_9;
}
v7 = a1;
if ( !getcwd(buf, 0x1000uLL) )
LABEL_9:
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; // rax
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)");
goto LABEL_9;
}
v7 = a1;
if ( !getcwd(buf, 0x1000uLL) )
LABEL_9:
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; *v0; i = *v0 )
{
if ( (unsigned __int64)strlcpy(v2, i, 1024LL) <= 0x3FF )
client_write_server(0xEu, (__int64)v2, 0x400u);
++v0;
}
}

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

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

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; // ebx
int v5; // eax
int v6; // eax
unsigned int v7; // eax
int v8; // edx
void *result; // rax
_BYTE v10[104]; // [rsp+0h] [rbp-98h] BYREF
int v11; // [rsp+68h] [rbp-30h]
__int16 v12; // [rsp+6Ch] [rbp-2Ch]

memset(v10, 0, sizeof(v10));
v11 = 0;
v12 = 0;
*(_WORD *)v10 = 1;
if ( (unsigned __int64)strlcpy(&v10[2], name, 108LL) > 0x6B )
{
*_errno_location() = 36;
goto LABEL_16;
}
v4 = socket(1, 1, 0);
if ( v4 == -1 )
{
log_fatal("%s: %s", "client_init", "socket failed");
goto LABEL_19;
}
v5 = strlen(&v10[2]);
if ( connect(v4, (const struct sockaddr *)v10, v5 + 2) != -1 )
goto LABEL_9;
if ( (a2 & 1) == 0 || (v6 = *_errno_location(), v6 != 2) && (v6 != 111 || unlink(name)) )
{
LABEL_16:
log_warn("server not found");
return 0LL;
}
v4 = server_start(name);
if ( v4 == -1 )
{
LABEL_19:
log_warnx("server failed to start");
return 0LL;
}
LABEL_9:
v7 = fcntl(v4, 3);
if ( v7 == -1 )
{
log_fatal("%s: %s", "client_init", "fcntl failed");
goto LABEL_21;
}
BYTE1(v7) |= 8u;
if ( fcntl(v4, 4, v7) == -1 )
{
LABEL_21:
log_fatal("%s: %s", "client_init", "fcntl failed");
goto LABEL_22;
}
if ( fcntl(v4, 2, 1LL) != -1 )
{
imsg_init(&client_ibuf, v4);
if ( (a2 & 4) == 0 )
goto LABEL_13;
goto LABEL_23;
}
LABEL_22:
log_fatal("%s: %s", "client_init", "fcntl failed");
LABEL_23:
client_send_environ();
LABEL_13:
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; // ebx
int v5; // eax
int v6; // eax
unsigned int v7; // eax
int v8; // edx
void *result; // rax
_BYTE v10[104]; // [rsp+0h] [rbp-98h] BYREF
int v11; // [rsp+68h] [rbp-30h]
__int16 v12; // [rsp+6Ch] [rbp-2Ch]

memset(v10, 0, sizeof(v10));
v11 = 0;
v12 = 0;
*(_WORD *)v10 = 1;
if ( (unsigned __int64)strlcpy(&v10[2], name, 108LL) > 0x6B )
{
*_errno_location() = 36;
goto LABEL_16;
}
v4 = socket(1, 1, 0);
if ( v4 == -1 )
{
log_fatal("%s: %s", "client_init", "socket failed");
goto LABEL_19;
}
v5 = strlen(&v10[2]);
if ( connect(v4, (const struct sockaddr *)v10, v5 + 2) != -1 )
goto LABEL_9;
if ( (a2 & 1) == 0 || (v6 = *_errno_location(), v6 != 2) && (v6 != 111 || unlink(name)) )
{
LABEL_16:
log_warn("server not found");
return 0LL;
}
v4 = server_start(name);
if ( v4 == -1 )
{
LABEL_19:
log_warnx("server failed to start");
return 0LL;
}
LABEL_9:
v7 = fcntl(v4, 3);
if ( v7 == -1 )
{
log_fatal("%s: %s", "client_init", "fcntl failed");
goto LABEL_21;
}
BYTE1(v7) |= 8u;
if ( fcntl(v4, 4, v7) == -1 )
{
LABEL_21:
log_fatal("%s: %s", "client_init", "fcntl failed");
goto LABEL_22;
}
if ( fcntl(v4, 2, 1LL) != -1 )
{
imsg_init(&client_ibuf, v4);
if ( (a2 & 4) == 0 )
goto LABEL_13;
goto LABEL_23;
}
LABEL_22:
log_fatal("%s: %s", "client_init", "fcntl failed");
LABEL_23:
func2();
LABEL_13:
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; }