decompiled_code:
__int64 main_loop()
{
int v0; // ebx
__int64 v1; // rax
__int64 v2; // rax
__int64 result; // rax
int v4; // eax
__int64 v5; // r15
__int64 v6; // rax
__int64 v7; // [rsp+8h] [rbp-70h] BYREF
__int64 v8[2]; // [rsp+10h] [rbp-68h] BYREF
const char *v9; // [rsp+20h] [rbp-58h]
__int64 v10; // [rsp+28h] [rbp-50h]
__int64 v11; // [rsp+30h] [rbp-48h]

v7 = 0LL;
rl_event_hook = check_state;
if ( fflush(0LL) == -1 )
{
v1 = get_progname();
system_log(6LL, 1LL, v1, "main.c", 125LL, "main_loop", "fflush");
}
v0 = 0;
do
{
result = readline(0LL);
v7 = result;
if ( !result )
break;
if ( *(_BYTE *)result )
{
if ( !strcmp((const char *)result, "quit") || !strcmp((const char *)result, "exit") )
return result;
v8[0] = 0LL;
v8[1] = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
if ( create_answer(v8, result) )
{
v4 = fprintf(stdout, "%s\n", v9);
v5 = (unsigned int)v4;
if ( v4 < 0 )
{
v6 = get_progname();
system_log(6LL, 1LL, v6, "main.c", 158LL, "main_loop", "fprintf=%d", v5);
}
}
else
{
v2 = get_progname();
system_log(6LL, 1LL, v2, "main.c", 153LL, "main_loop", "create_calc");
}
if ( (unsigned int)++v0 > 0x63 )
freehistory(&history);
add_history(v7);
destroy_answer(v8);
memfree(&v7, 0LL);
}
else
{
memfree(&v7, 0LL);
}
result = (unsigned int)sig_handled;
}
while ( !sig_handled );
return result;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // ebx
__int64 v1; // rax
__int64 v2; // rax
__int64 result; // rax
int v4; // eax
__int64 v5; // r15
__int64 v6; // rax
__int64 v7; // [rsp+8h] [rbp-70h] BYREF
__int64 v8[2]; // [rsp+10h] [rbp-68h] BYREF
const char *v9; // [rsp+20h] [rbp-58h]
__int64 v10; // [rsp+28h] [rbp-50h]
__int64 v11; // [rsp+30h] [rbp-48h]

v7 = 0LL;
rl_event_hook = check_state;
if ( fflush(0LL) == -1 )
{
v1 = get_progname();
system_log(6LL, 1LL, v1, "main.c", 125LL, "main_loop", "fflush");
}
v0 = 0;
do
{
result = readline(0LL);
v7 = result;
if ( !result )
break;
if ( *(_BYTE *)result )
{
if ( !strcmp((const char *)result, "quit") || !strcmp((const char *)result, "exit") )
return result;
v8[0] = 0LL;
v8[1] = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
if ( create_answer(v8, result) )
{
v4 = fprintf(stdout, "%s\n", v9);
v5 = (unsigned int)v4;
if ( v4 < 0 )
{
v6 = get_progname();
system_log(6LL, 1LL, v6, "main.c", 158LL, "main_loop", "fprintf=%d", v5);
}
}
else
{
v2 = get_progname();
system_log(6LL, 1LL, v2, "main.c", 153LL, "main_loop", "create_calc");
}
if ( (unsigned int)++v0 > 0x63 )
func3(&history);
add_history(v7);
destroy_answer(v8);
memfree(&v7, 0LL);
}
else
{
memfree(&v7, 0LL);
}
result = (unsigned int)sig_handled;
}
while ( !sig_handled );
return result;
}

original_code:
static void
main_loop(void)
{
    int retval = 0;
    calcinfo calc;
    unsigned char *expr = ((void *)0);
    unsigned int hist_no = 0;
    char *prompt = ((void *)0);
    rl_event_hook = &check_state;
    do { } while (0);
    retval = fflush(((void *)0));
    if (retval == (-1))
        system_log(6, 0x01, get_progname(), "main.c", 125, __FUNCTION__, "fflush");
    do {
        do { } while (0);
        expr = (unsigned char *)readline(prompt);
        if (!expr)
            break;
        if (*expr == '\0') {
            do { } while (0);
            memfree((void **)&expr, ((void *)0));
            continue;
        }
        do { } while (0);
        if (!strcmp((char *)expr, "quit") ||
            !strcmp((char *)expr, "exit"))
            break;
        (void)memset(&calc, 0, sizeof(calcinfo));
        if (!create_answer(&calc, expr)) {
            system_log(6, 0x01, get_progname(), "main.c", 153, __FUNCTION__, "create_calc");
        } else {
            do { } while (0);
            retval = fprintf(stdout, "%s\n", (char *)calc.answer);
            if (retval < 0)
                system_log(6, 0x01, get_progname(), "main.c", 158, __FUNCTION__, "fprintf=%d", retval);
        }
        if (MAX_HISTORY <= ++hist_no)
            freehistory(&history);
        add_history((char *)expr);
        destroy_answer(&calc);
        memfree((void **)&expr, ((void *)0));
    } while (!sig_handled);
}

canonical_original_code:
static void main_loop()
{
    int retval = 0;
    struct _calcinfo calc;
    unsigned char *expr = ((void *)0);
    unsigned int hist_no = 0;
    char *prompt = ((void *)0);
    rl_event_hook = &check_state;
    do { } while (0);
    retval = fflush(((void *)0));
    if (retval == (-1))
        system_log(6, 0x01, get_progname(), "main.c", 125, __FUNCTION__, "fflush");
    do {
        do { } while (0);
        expr = (unsigned char *)readline(prompt);
        if (!expr)
            break;
        if (*expr == '\0') {
            do { } while (0);
            memfree((void **)&expr, ((void *)0));
            continue;
        }
        do { } while (0);
        if (!strcmp((char *)expr, "quit") ||
            !strcmp((char *)expr, "exit"))
            break;
        (void)memset(&calc, 0, sizeof(calcinfo));
        if (!create_answer(&calc, expr)) {
            system_log(6, 0x01, get_progname(), "main.c", 153, __FUNCTION__, "create_calc");
        } else {
            do { } while (0);
            retval = fprintf(stdout, "%s\n", (char *)calc.answer);
            if (retval < 0)
                system_log(6, 0x01, get_progname(), "main.c", 158, __FUNCTION__, "fprintf=%d", retval);
        }
        if (MAX_HISTORY <= ++hist_no)
            freehistory(&history);
        add_history((char *)expr);
        destroy_answer(&calc);
        memfree((void **)&expr, ((void *)0));
    } while (!sig_handled);
}

variable_types: retval int

variable_types: struct _calcinfo { int ch; unsigned char *ptr; unsigned char *answer; char fmt[-1]; enum _ER { E_NONE=0, E_DIVBYZERO=1, E_SYNTAX=2, E_NOFUNC=3, E_NAN=4, E_INFINITY=5, MAXERROR=6 } errorcode; }

variable_types:
expr
unsigned char *

variable_types:
prompt
char *


repo: sluchin/calc

decompiled_code:
void sig_handler()
{
sig_handled = 1;
}

canonical_decompiled_code:
void func0()
{
sig_handled = 1;
}

original_code:
static void sig_handler(int signo)
{
    sig_handled = 1;
}

variable_types: signo int

return_type: void


original_code:
static void
set_sig_handler(void)
{
    struct sigaction sa;
    sigset_t sigmask;
    (void)memset(&sa, 0, sizeof(struct sigaction));
    if (sigemptyset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 220, __FUNCTION__, "sigemptyset=0x%x", sigmask);
    if (sigfillset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 222, __FUNCTION__, "sigfillset=0x%x", sigmask);
    do { } while (0);
    if (sigaction(2, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 227, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(2, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 231, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    if (sigaction(15, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 234, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(15, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 238, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    if (sigaction(3, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 241, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(3, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 245, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
}

canonical_original_code:
static void set_sig_handler()
{
    struct sigaction sa;
    struct __sigset_t sigmask;
    (void)memset(&sa, 0, sizeof(struct sigaction));
    if (sigemptyset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 220, __FUNCTION__, "sigemptyset=0x%x", sigmask);
    if (sigfillset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 222, __FUNCTION__, "sigfillset=0x%x", sigmask);
    do { } while (0);
    if (sigaction(2, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 227, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(2, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 231, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    if (sigaction(15, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 234, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(15, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 238, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    if (sigaction(3, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 241, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(3, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 245, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
}

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


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax

set_progname(*argv);
set_sig_handler();
if ( setvbuf(stdin, 0LL, 2, 0LL) )
{
v3 = get_progname();
system_log(6LL, 1LL, v3, "main.c", 85LL, "main", "setvbuf: stdin");
}
if ( setvbuf(stdout, 0LL, 2, 0LL) )
{
v4 = get_progname();
system_log(6LL, 1LL, v4, "main.c", 87LL, "main", "setvbuf: stdout");
}
parse_args((unsigned int)argc, argv);
main_loop();
freehistory(&history);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax

set_progname(*argv);
func1();
if ( setvbuf(stdin, 0LL, 2, 0LL) )
{
v3 = get_progname();
system_log(6LL, 1LL, v3, "main.c", 85LL, "main", "setvbuf: stdin");
}
if ( setvbuf(stdout, 0LL, 2, 0LL) )
{
v4 = get_progname();
system_log(6LL, 1LL, v4, "main.c", 87LL, "main", "setvbuf: stdout");
}
parse_args((unsigned int)argc, argv);
func4();
func3(&history);
exit(0);
}

original_code:
int
main(int argc, char *argv[])
{
    do { } while (0);
    set_progname(argv[0]);
    set_sig_handler();
    if (setvbuf(stdin, (char *)((void *)0), 2, 0))
        system_log(6, 0x01, get_progname(), "main.c", 85, __FUNCTION__, "setvbuf: stdin");
    if (setvbuf(stdout, (char *)((void *)0), 2, 0))
        system_log(6, 0x01, get_progname(), "main.c", 87, __FUNCTION__, "setvbuf: stdout");
    parse_args(argc, argv);
    main_loop();
    freehistory(&history);
    exit(0);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


canonical_decompiled_code:
void func1(void)
{
close_sock(&sockfd);
}

original_code:
static void
exit_close_sock(void)
{
    close_sock(&sockfd);
}

canonical_original_code:
static void exit_close_sock()
{
    close_sock(&sockfd);
}


decompiled_code:
_DWORD *sig_handler()
{
_DWORD *result; // rax

result = &g_sig_handled;
g_sig_handled = 1;
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *result; // rax

result = &g_sig_handled;
g_sig_handled = 1;
return result;
}

original_code:
static void sig_handler(int signo)
{
    g_sig_handled = 1;
}


original_code:
static void
set_sig_handler(void)
{
    struct sigaction sa;
    sigset_t sigmask;
    (void)memset(&sa, 0, sizeof(struct sigaction));
    if (sigemptyset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 120, __FUNCTION__, "sigemptyset=0x%x", sigmask);
    if (sigfillset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 122, __FUNCTION__, "sigfillset=0x%x", sigmask);
    do { } while (0);
    if (sigaction(2, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 127, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(2, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 131, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    if (sigaction(15, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 134, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(15, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 138, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    if (sigaction(3, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 141, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(3, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 145, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
}

canonical_original_code:
static void set_sig_handler()
{
    struct sigaction sa;
    struct __sigset_t sigmask;
    (void)memset(&sa, 0, sizeof(struct sigaction));
    if (sigemptyset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 120, __FUNCTION__, "sigemptyset=0x%x", sigmask);
    if (sigfillset(&sigmask) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 122, __FUNCTION__, "sigfillset=0x%x", sigmask);
    do { } while (0);
    if (sigaction(2, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 127, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(2, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 131, __FUNCTION__, "sigaction=%p, SIGINT", &sa);
    if (sigaction(15, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 134, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(15, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 138, __FUNCTION__, "sigaction=%p, SIGTERM", &sa);
    if (sigaction(3, (struct sigaction *)((void *)0), &sa) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 141, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
    sa.__sigaction_handler.sa_handler = sig_handler;
    sa.sa_mask = sigmask;
    if (sigaction(3, &sa, (struct sigaction *)((void *)0)) < 0)
        system_log(6, 0x01, get_progname(), "main.c", 145, __FUNCTION__, "sigaction=%p, SIGQUIT", &sa);
}

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

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


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
int v6; // eax
int v7; // eax

set_progname(*argv);
set_sig_handler();
if ( setvbuf(stdin, 0LL, 2, 0LL) )
{
v4 = get_progname();
system_log(6LL, 1LL, v4, "main.c", 67LL, "main", "setvbuf: stdin");
}
if ( setvbuf(stdout, 0LL, 2, 0LL) )
{
v5 = get_progname();
system_log(6LL, 1LL, v5, "main.c", 69LL, "main", "setvbuf: stdout");
}
parse_args((unsigned int)argc, argv);
if ( atexit(exit_close_sock) )
{
v3 = get_progname();
system_log(6LL, 1LL, v3, "main.c", 76LL, "main", "atexit");
exit(1);
}
v6 = connect_sock();
sockfd = v6;
if ( v6 >= 0 )
{
v7 = client_loop((unsigned int)v6);
exit(v7);
}
fwrite("Connect error\n", 1uLL, 0xEuLL, stderr);
exit(4);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
int v6; // eax
int v7; // eax

set_progname(*argv);
func2();
if ( setvbuf(stdin, 0LL, 2, 0LL) )
{
v4 = get_progname();
system_log(6LL, 1LL, v4, "main.c", 67LL, "main", "setvbuf: stdin");
}
if ( setvbuf(stdout, 0LL, 2, 0LL) )
{
v5 = get_progname();
system_log(6LL, 1LL, v5, "main.c", 69LL, "main", "setvbuf: stdout");
}
parse_args((unsigned int)argc, argv);
if ( atexit(exit_close_sock) )
{
v3 = get_progname();
system_log(6LL, 1LL, v3, "main.c", 76LL, "main", "atexit");
exit(1);
}
v6 = connect_sock();
sockfd = v6;
if ( v6 >= 0 )
{
v7 = client_loop((unsigned int)v6);
exit(v7);
}
fwrite("Connect error\n", 1uLL, 0xEuLL, stderr);
exit(4);
}

original_code:
int main(int argc, char *argv[])
{
    st_client status = EX_SUCCESS;
    do { } while (0);
    set_progname(argv[0]);
    set_sig_handler();
    if (setvbuf(stdin, (char *)((void *)0), 2, 0))
        system_log(6, 0x01, get_progname(), "main.c", 67, __FUNCTION__, "setvbuf: stdin");
    if (setvbuf(stdout, (char *)((void *)0), 2, 0))
        system_log(6, 0x01, get_progname(), "main.c", 69, __FUNCTION__, "setvbuf: stdout");
    parse_args(argc, argv);
    if (atexit(exit_close_sock)) {
        system_log(6, 0x01, get_progname(), "main.c", 76, __FUNCTION__, "atexit");
        exit(EX_FAILURE);
    }
    sockfd = connect_sock();
    if (sockfd < 0) {
        (void)fprintf(stderr, "Connect error\n");
        exit(EX_CONNECT_ERR);
    }
    status = client_loop(sockfd);
    exit(status);
    return status;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    enum _st_client status = EX_SUCCESS;
    do { } while (0);
    set_progname(argv[0]);
    set_sig_handler();
    if (setvbuf(stdin, (char *)((void *)0), 2, 0))
        system_log(6, 0x01, get_progname(), "main.c", 67, __FUNCTION__, "setvbuf: stdin");
    if (setvbuf(stdout, (char *)((void *)0), 2, 0))
        system_log(6, 0x01, get_progname(), "main.c", 69, __FUNCTION__, "setvbuf: stdout");
    parse_args(argc, argv);
    if (atexit(exit_close_sock)) {
        system_log(6, 0x01, get_progname(), "main.c", 76, __FUNCTION__, "atexit");
        exit(EX_FAILURE);
    }
    sockfd = connect_sock();
    if (sockfd < 0) {
        (void)fprintf(stderr, "Connect error\n");
        exit(EX_CONNECT_ERR);
    }
    status = client_loop(sockfd);
    exit(status);
    return status;
}

variable_types: enum _st_client { EX_SUCCESS=0, EX_FAILURE=1, EX_EMPTY=2, EX_QUIT=3, EX_CONNECT_ERR=4, EX_ALLOC_ERR=5, EX_SEND_ERR=6, EX_RECV_ERR=7, EX_SIGNAL=8 }


decompiled_code:
__int64 __fastcall mode_type_flag(unsigned int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rsi

if ( a1 == 2 )
return a2 + 4;
if ( a1 > 2 )
{
v3 = a2 + 12;
result = 0LL;
if ( a1 == 3 )
result = v3;
}
else
{
result = a2 + 8;
if ( a1 )
result = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rsi

if ( a1 == 2 )
return a2 + 4;
if ( a1 > 2 )
{
v3 = a2 + 12;
result = 0LL;
if ( a1 == 3 )
result = v3;
}
else
{
result = a2 + 8;
if ( a1 )
result = a2;
}
return result;
}

original_code:
static tcflag_t *
mode_type_flag(const enum mode_type type, struct termios *mode)
{
    switch (type) {
    case control:
        return &mode->c_cflag;
    case input:
        return &mode->c_iflag;
    case output:
        return &mode->c_oflag;
    case local:
        return &mode->c_lflag;
    default:
        return ((void *)0);
    }
}

canonical_original_code:
static unsigned int *mode_type_flag(enum mode_type type, struct termios *mode)
{
    switch (type) {
    case control:
        return &mode->c_cflag;
    case input:
        return &mode->c_iflag;
    case output:
        return &mode->c_oflag;
    case local:
        return &mode->c_lflag;
    default:
        return ((void *)0);
    }
}

variable_types: enum mode_type { control=0, input=1, output=2, local=3 }

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

return_type: unsigned int *


decompiled_code:
void __fastcall sys_print_termattr(int a1, int a2, const char *a3, const char *a4, unsigned int a5, const char *a6)
{
char *v10; // rax
char *v11; // rbx
struct termios v12; // [rsp+10h] [rbp-78h] BYREF

*(_QWORD *)&v12.c_iflag = 0LL;
*(_QWORD *)&v12.c_cflag = 0LL;
*(_QWORD *)&v12.c_line = 0LL;
*(_QWORD *)&v12.c_cc[7] = 0LL;
*(_QWORD *)&v12.c_cc[15] = 0LL;
*(_QWORD *)&v12.c_cc[23] = 0LL;
*(_QWORD *)&v12.c_cc[31] = 0LL;
v12.c_ospeed = 0;
v10 = get_termattr(0, &v12);
if ( v10 )
{
v11 = v10;
openlog(a3, a2, 40);
syslog(a1, "%s[%d]: %s: %s", a4, a5, a6, v11);
closelog();
free(v11);
}
}

canonical_decompiled_code:
void __fastcall func2(int a1, int a2, const char *a3, const char *a4, unsigned int a5, const char *a6)
{
char *v10; // rax
char *v11; // rbx
struct termios v12; // [rsp+10h] [rbp-78h] BYREF

*(_QWORD *)&v12.c_iflag = 0LL;
*(_QWORD *)&v12.c_cflag = 0LL;
*(_QWORD *)&v12.c_line = 0LL;
*(_QWORD *)&v12.c_cc[7] = 0LL;
*(_QWORD *)&v12.c_cc[15] = 0LL;
*(_QWORD *)&v12.c_cc[23] = 0LL;
*(_QWORD *)&v12.c_cc[31] = 0LL;
v12.c_ospeed = 0;
v10 = func1(0, &v12);
if ( v10 )
{
v11 = v10;
openlog(a3, a2, 40);
syslog(a1, "%s[%d]: %s: %s", a4, a5, a6, v11);
closelog();
free(v11);
}
}

original_code:
void
sys_print_termattr(const int level, const int option,
                   const char *pname, const char *fname,
                   const int line, const char *func, int fd)
{
    struct termios mode;
    char *result = ((void *)0);
    (void)memset(&mode, 0, sizeof(struct termios));
    result = get_termattr(0, &mode);
    if (!result)
        return;
    openlog(pname, option, (5<<3));
    syslog(level, "%s[%d]: %s: %s", fname, line, func, result);
    closelog();
    if (result)
        free(result);
    result = ((void *)0);
}

canonical_original_code:
void sys_print_termattr(int level, int option, char *pname, char *fname, int line, char *func, int fd)
{
    struct termios mode;
    char *result = ((void *)0);
    (void)memset(&mode, 0, sizeof(struct termios));
    result = get_termattr(0, &mode);
    if (!result)
        return;
    openlog(pname, option, (5<<3));
    syslog(level, "%s[%d]: %s: %s", fname, line, func, result);
    closelog();
    if (result)
        free(result);
    result = ((void *)0);
}

variable_types: level int

variable_types: option int

variable_types:
pname
char *

variable_types:
fname
char *

variable_types: line int

variable_types:
func
char *

variable_types: fd int

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

variable_types:
result
char *


memfree

decompiled_code:
char *memfree(void **a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, ...)
{
void *v10; // rdi
void ***v11; // rdx
char *result; // rax
void **v13; // rbx
gcc_va_list va; // [rsp+8h] [rbp-50h] BYREF

v10 = *a1;
if ( v10 )
free(v10);
*a1 = 0LL;
va_start(va, a9);
while ( 1 )
{
if ( va[0].gp_offset > 0x2F )
{
v11 = (void ***)va[0].overflow_arg_area;
result = (char *)va[0].overflow_arg_area + 8;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
else
{
v11 = (void ***)((char *)va[0].reg_save_area + va[0].gp_offset);
result = (char *)(va[0].gp_offset + 8);
va[0].gp_offset += 8;
}
v13 = *v11;
if ( !*v11 )
break;
if ( *v13 )
free(*v13);
*v13 = 0LL;
}
return result;
}

canonical_decompiled_code:
char *func0(void **a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, ...)
{
void *v10; // rdi
void ***v11; // rdx
char *result; // rax
void **v13; // rbx
gcc_va_list va; // [rsp+8h] [rbp-50h] BYREF

v10 = *a1;
if ( v10 )
free(v10);
*a1 = 0LL;
va_start(va, a9);
while ( 1 )
{
if ( va[0].gp_offset > 0x2F )
{
v11 = (void ***)va[0].overflow_arg_area;
result = (char *)va[0].overflow_arg_area + 8;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
else
{
v11 = (void ***)((char *)va[0].reg_save_area + va[0].gp_offset);
result = (char *)(va[0].gp_offset + 8);
va[0].gp_offset += 8;
}
v13 = *v11;
if ( !*v11 )
break;
if ( *v13 )
free(*v13);
*v13 = 0LL;
}
return result;
}

original_code:
void
memfree(void** ptr, ...)
{
    void **mem = ((void *)0);
    va_list ap;
    do { } while (0);
    do { } while (0);
    if (*ptr)
        free(*ptr);
    *ptr = ((void *)0);
    __builtin_va_start(ap,ptr);
    while ((mem = __builtin_va_arg(ap,void **)) != ((void *)0)) {
        do { } while (0);
        if (*mem)
            free(*mem);
        *mem = ((void *)0);
    }
    __builtin_va_end(ap);
}

canonical_original_code:
void memfree(void **ptr, ...)
{
    void **mem = ((void *)0);
    __builtin_va_list ap;
    do { } while (0);
    do { } while (0);
    if (*ptr)
        free(*ptr);
    *ptr = ((void *)0);
    __builtin_va_start(ap,ptr);
    while ((mem = __builtin_va_arg(ap,void **)) != ((void *)0)) {
        do { } while (0);
        if (*mem)
            free(*mem);
        *mem = ((void *)0);
    }
    __builtin_va_end(ap);
}

variable_types:
ptr
void **

variable_types:
mem
void **


repo: slunk/libbitbag

decompiled_code:
__int64 __fastcall bst_dict_size(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int bst_dict_size(void *data)
{
    bst_dict_data_t *bstdata = (bst_dict_data_t *)data;
    return bstdata->size;
}

canonical_original_code:
int bst_dict_size(void *data)
{
    struct _bst_dict_data *bstdata = (bst_dict_data_t *)data;
    return bstdata->size;
}

variable_types:
data
void *

variable_types: struct _bst_dict_data { struct _bst_node { struct _dict_entry { void *key; void *val; int (*keycmp)(void *a, void *b); } entry; struct _bst_node *parent; struct _bst_node *left; struct _bst_node *right; } *root; int size; int (*keycmp)(void *a, void *b); } *

user_defined_types: struct _bst_dict_data { struct _bst_node *root; int size; int (*keycmp)(void *a, void *b); }

user_defined_types: struct _bst_node { struct _dict_entry entry; struct _bst_node *parent; struct _bst_node *left; struct _bst_node *right; }


original_code:
void bst_dict_del(void *data, void *key)
{
    bst_dict_data_t *bstdata = (bst_dict_data_t *)data;
    bst_node_t *node = _bst_dict_find(bstdata, key);
    bst_node_t *succ;
    if (!node)
        return;
    if (node->left && node->right) {
        succ = _bst_dict_succ(node);
        node->entry.key = succ->entry.key;
        node->entry.val = succ->entry.val;
        node = succ;
    }
    if (node->left) {
        if (node->parent) {
            if (node->parent->left == node) {
                node->parent->left = node->left;
            } else {
                node->parent->right = node->left;
            }
        } else {
            bstdata->root = node->left;
        }
    } else if (node->right) {
        if (node->parent) {
            if (node->parent->left == node) {
                node->parent->left = node->right;
            } else {
                node->parent->right = node->right;
            }
        } else {
            bstdata->root = node->right;
        }
    } else {
        if (node->parent) {
            if (node->parent->left == node) {
                node->parent->left = ((void *)0);
            } else {
                node->parent->right = ((void *)0);
            }
        } else {
            bstdata->root = ((void *)0);
        }
    }
    free(node);
}

canonical_original_code:
void bst_dict_del(void *data, void *key)
{
    struct _bst_dict_data *bstdata = (bst_dict_data_t *)data;
    struct _bst_node *node = _bst_dict_find(bstdata, key);
    struct _bst_node *succ;
    if (!node)
        return;
    if (node->left && node->right) {
        succ = _bst_dict_succ(node);
        node->entry.key = succ->entry.key;
        node->entry.val = succ->entry.val;
        node = succ;
    }
    if (node->left) {
        if (node->parent) {
            if (node->parent->left == node) {
                node->parent->left = node->left;
            } else {
                node->parent->right = node->left;
            }
        } else {
            bstdata->root = node->left;
        }
    } else if (node->right) {
        if (node->parent) {
            if (node->parent->left == node) {
                node->parent->left = node->right;
            } else {
                node->parent->right = node->right;
            }
        } else {
            bstdata->root = node->right;
        }
    } else {
        if (node->parent) {
            if (node->parent->left == node) {
                node->parent->left = ((void *)0);
            } else {
                node->parent->right = ((void *)0);
            }
        } else {
            bstdata->root = ((void *)0);
        }
    }
    free(node);
}

variable_types:
key
void *

variable_types: struct _bst_node { struct _dict_entry { void *key; void *val; int (*keycmp)(void *a, void *b); } entry; struct _bst_node *parent; struct _bst_node *left; struct _bst_node *right; } *


decompiled_code:
void __fastcall bst_dict_free(void *ptr)
{
bst_dict_free_helper(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
func7(*(void **)ptr);
free(ptr);
}

original_code:
void bst_dict_free(void *data)
{
    bst_dict_data_t *bstdata = (bst_dict_data_t *)data;
    _bst_dict_free_helper(bstdata->root);
    free(bstdata);
}

canonical_original_code:
void bst_dict_free(void *data)
{
    struct _bst_dict_data *bstdata = (bst_dict_data_t *)data;
    _bst_dict_free_helper(bstdata->root);
    free(bstdata);
}

user_defined_types: struct _dict_entry { void *key; void *val; int (*keycmp)(void *a, void *b); }


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

if ( a2 >= 0 || (result = 0LL, *(_DWORD *)(a1 + 4) > a2) )
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
return result;
}

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

if ( a2 >= 0 || (result = 0LL, *(_DWORD *)(a1 + 4) > a2) )
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
return result;
}

original_code:
void *vector_get(void *data, int idx)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    if (idx >= 0 || idx < vecdat->last) {
        return vecdat->arr[idx];
    }
    return ((void *)0);
}

canonical_original_code:
void *vector_get(void *data, int idx)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    if (idx >= 0 || idx < vecdat->last) {
        return vecdat->arr[idx];
    }
    return ((void *)0);
}

variable_types: idx int

variable_types: struct _vector_data { int size; int last; void **arr; } *

return_type: void *


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

if ( a2 >= 0 || *(_DWORD *)(a1 + 4) > a2 )
{
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(result + 8LL * a2) = a3;
}
return result;
}

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

if ( a2 >= 0 || *(_DWORD *)(a1 + 4) > a2 )
{
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(result + 8LL * a2) = a3;
}
return result;
}

original_code:
void vector_set(void *data, int idx, void *elt)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    if (idx >= 0 || idx < vecdat->last) {
        vecdat->arr[idx] = elt;
    }
}

canonical_original_code:
void vector_set(void *data, int idx, void *elt)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    if (idx >= 0 || idx < vecdat->last) {
        vecdat->arr[idx] = elt;
    }
}

variable_types:
elt
void *

user_defined_types: struct _vector_data { int size; int last; void **arr; }


decompiled_code:
__int64 __fastcall vector_search(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 v3; // rdi
__int64 v4; // rax
unsigned int v5; // ecx

v2 = *(_DWORD *)(a1 + 4);
if ( v2 <= 0 )
return (unsigned int)-1;
v3 = *(_QWORD *)(a1 + 8);
v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( *(_QWORD *)(v3 + 8 * v4) == a2 )
break;
if ( ++v4 == v2 )
return (unsigned int)-1;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 v3; // rdi
__int64 v4; // rax
unsigned int v5; // ecx

v2 = *(_DWORD *)(a1 + 4);
if ( v2 <= 0 )
return (unsigned int)-1;
v3 = *(_QWORD *)(a1 + 8);
v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( *(_QWORD *)(v3 + 8 * v4) == a2 )
break;
if ( ++v4 == v2 )
return (unsigned int)-1;
}
return v5;
}

original_code:
int vector_search(void *data, void *elt)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    int i;
    for (i = 0; i < vecdat->last; i++) {
        if (vecdat->arr[i] == elt)
            return i;
    }
    return -1;
}

canonical_original_code:
int vector_search(void *data, void *elt)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    int i;
    for (i = 0; i < vecdat->last; i++) {
        if (vecdat->arr[i] == elt)
            return i;
    }
    return -1;
}

variable_types: i int


decompiled_code:
__int64 __fastcall vector_del(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx
int v4; // edx

result = (unsigned int)vector_search(a1, a2) + 1;
if ( (int)result > 0 )
{
v3 = 8LL * (int)result;
do
{
if ( *(_DWORD *)(a1 + 4) <= (int)result )
break;
*(_QWORD *)(*(_QWORD *)(a1 + 8) + v3 - 8) = *(_QWORD *)(*(_QWORD *)(a1 + 8) + v3);
result = (unsigned int)(result + 1);
v3 += 8LL;
}
while ( (int)result > 0 );
}
v4 = *(_DWORD *)(a1 + 4);
if ( v4 == (_DWORD)result )
*(_DWORD *)(a1 + 4) = v4 - 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx
int v4; // edx

result = (unsigned int)func2(a1, a2) + 1;
if ( (int)result > 0 )
{
v3 = 8LL * (int)result;
do
{
if ( *(_DWORD *)(a1 + 4) <= (int)result )
break;
*(_QWORD *)(*(_QWORD *)(a1 + 8) + v3 - 8) = *(_QWORD *)(*(_QWORD *)(a1 + 8) + v3);
result = (unsigned int)(result + 1);
v3 += 8LL;
}
while ( (int)result > 0 );
}
v4 = *(_DWORD *)(a1 + 4);
if ( v4 == (_DWORD)result )
*(_DWORD *)(a1 + 4) = v4 - 1;
return result;
}

original_code:
void vector_del(void *data, void *elt)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    int i;
    for (i = vector_search(data, elt) + 1; i > 0 && i < vecdat->last; i++) {
        vecdat->arr[i - 1] = vecdat->arr[i];
    }
    if (i == vecdat->last)
        vecdat->last--;
}

canonical_original_code:
void vector_del(void *data, void *elt)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    int i;
    for (i = vector_search(data, elt) + 1; i > 0 && i < vecdat->last; i++) {
        vecdat->arr[i - 1] = vecdat->arr[i];
    }
    if (i == vecdat->last)
        vecdat->last--;
}


decompiled_code:
__int64 __fastcall vector_size(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
int vector_size(void *data)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    return vecdat->last;
}

canonical_original_code:
int vector_size(void *data)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    return vecdat->last;
}


original_code:
void vector_add(void *data, void *elt)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    int i;
    void **grown;
    if (vecdat->last >= vecdat->size) {
        vecdat->size *= 2;
        grown = malloc(vecdat->size * sizeof(*vecdat->arr));
        for (i = 0; i < vecdat->last; i++) {
            grown[i] = vecdat->arr[i];
        }
        free(vecdat->arr);
        vecdat->arr = grown;
    }
    vecdat->arr[vecdat->last++] = elt;
}

canonical_original_code:
void vector_add(void *data, void *elt)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    int i;
    void **grown;
    if (vecdat->last >= vecdat->size) {
        vecdat->size *= 2;
        grown = malloc(vecdat->size * sizeof(*vecdat->arr));
        for (i = 0; i < vecdat->last; i++) {
            grown[i] = vecdat->arr[i];
        }
        free(vecdat->arr);
        vecdat->arr = grown;
    }
    vecdat->arr[vecdat->last++] = elt;
}

variable_types:
grown
void **


decompiled_code:
void __fastcall vector_free(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void vector_free(void *data)
{
    vector_data_t *vecdat = (vector_data_t *)data;
    free(vecdat->arr);
    free(vecdat);
}

canonical_original_code:
void vector_free(void *data)
{
    struct _vector_data *vecdat = (vector_data_t *)data;
    free(vecdat->arr);
    free(vecdat);
}


decompiled_code:
_QWORD *vector_new()
{
_QWORD *v0; // rbx
_QWORD *v1; // rbp

v0 = malloc(0x40uLL);
v1 = malloc(0x10uLL);
v1[1] = malloc(0x800uLL);
*(_DWORD *)v1 = 256;
*((_DWORD *)v1 + 1) = 0;
*v0 = v1;
v0[1] = vector_add;
v0[2] = vector_del;
v0[3] = vector_get;
v0[4] = vector_set;
v0[5] = vector_search;
v0[6] = vector_size;
v0[7] = vector_free;
return v0;
}

canonical_decompiled_code:
_QWORD *func7()
{
_QWORD *v0; // rbx
_QWORD *v1; // rbp

v0 = malloc(0x40uLL);
v1 = malloc(0x10uLL);
v1[1] = malloc(0x800uLL);
*(_DWORD *)v1 = 256;
*((_DWORD *)v1 + 1) = 0;
*v0 = v1;
v0[1] = vector_add;
v0[2] = vector_del;
v0[3] = vector_get;
v0[4] = vector_set;
v0[5] = vector_search;
v0[6] = vector_size;
v0[7] = vector_free;
return v0;
}

original_code:
list_t *vector_new()
{
    list_t *rval = malloc(sizeof(*rval));
    vector_data_t *data = malloc(sizeof(*data));
    data->arr = malloc(256 * sizeof(*data->arr));
    data->size = 256;
    data->last = 0;
    rval->data = data;
    rval->add = vector_add;
    rval->del = vector_del;
    rval->get = vector_get;
    rval->set = vector_set;
    rval->search = vector_search;
    rval->size = vector_size;
    rval->free = vector_free;
    return rval;
}

canonical_original_code:
struct _list *vector_new()
{
    struct _list *rval = malloc(sizeof(*rval));
    struct _vector_data *data = malloc(sizeof(*data));
    data->arr = malloc(256 * sizeof(*data->arr));
    data->size = 256;
    data->last = 0;
    rval->data = data;
    rval->add = vector_add;
    rval->del = vector_del;
    rval->get = vector_get;
    rval->set = vector_set;
    rval->search = vector_search;
    rval->size = vector_size;
    rval->free = vector_free;
    return rval;
}

variable_types: struct _list { void *data; void (*add)(void *data, void *elt); void (*del)(void *data, void *elt); void *(*get)(void *data, int i); void (*set)(void *data, int i, void *elt); int (*search)(void *data, void *elt); int (*size)(void *data); void (*free)(void *data); } *


decompiled_code:
__int64 __fastcall hash_table_size(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code:
int hash_table_size(void *data)
{
    hash_table_data_t *htdat = (hash_table_data_t *)data;
    return htdat->size;
}

canonical_original_code:
int hash_table_size(void *data)
{
    struct _hash_table_data *htdat = (hash_table_data_t *)data;
    return htdat->size;
}

variable_types: struct _hash_table_data { void **table; int table_size; int size; struct _list { void *data; void (*add)(void *data, void *elt); void (*del)(void *data, void *elt); void *(*get)(void *data, int i); void (*set)(void *data, int i, void *elt); int (*search)(void *data, void *elt); int (*size)(void *data); void (*free)(void *data); } *keys; int (*hash_fn)(void *a); int (*keycmp)(void *a, void *b); } *

user_defined_types: struct _hash_table_data { void **table; int table_size; int size; struct _list *keys; int (*hash_fn)(void *a); int (*keycmp)(void *a, void *b); }


decompiled_code:
void hash_table_free()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void hash_table_free(void *data)
{
    hash_table_data_t *htdat = (hash_table_data_t *)data;
}

canonical_original_code:
void hash_table_free(void *data)
{
    struct _hash_table_data *htdat = (hash_table_data_t *)data;
}

user_defined_types: struct _list { void *data; void (*add)(void *data, void *elt); void (*del)(void *data, void *elt); void *(*get)(void *data, int i); void (*set)(void *data, int i, void *elt); int (*search)(void *data, void *elt); int (*size)(void *data); void (*free)(void *data); }


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

result = hash_table_get_entry(a1, a2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

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

result = func3(a1, a2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
void *hash_table_get(void *data, void *key)
{
    hash_table_data_t *htdat = (hash_table_data_t *)data;
    dict_entry_t *entry = _hash_table_get_entry(htdat, key);
    if (entry)
        return entry->val;
    return ((void *)0);
}

canonical_original_code:
void *hash_table_get(void *data, void *key)
{
    struct _hash_table_data *htdat = (hash_table_data_t *)data;
    struct _dict_entry *entry = _hash_table_get_entry(htdat, key);
    if (entry)
        return entry->val;
    return ((void *)0);
}

variable_types: struct _dict_entry { void *key; void *val; int (*keycmp)(void *a, void *b); } *


decompiled_code:
__int64 __fastcall hash_table_put(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
_QWORD *v5; // r13
__int64 v6; // r12
unsigned int v7; // er14

result = hash_table_get_entry(a1, a2);
if ( result )
{
*(_QWORD *)(result + 8) = a3;
}
else
{
v5 = malloc(0x18uLL);
*v5 = a2;
v5[1] = a3;
v5[2] = *(_QWORD *)(a1 + 32);
v6 = hash_table_get_list(a1, a2);
if ( !v6 )
{
v7 = (*(unsigned int (__fastcall **)(__int64))(a1 + 24))(a2) % *(_DWORD *)(a1 + 8);
printf("idx: %d\n", v7);
v6 = linked_list_new(&dict_keycmp);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v7) = v6;
}
list_add(*(_QWORD *)(a1 + 16), a2);
result = list_add(v6, v5);
++*(_DWORD *)(a1 + 12);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
_QWORD *v5; // r13
__int64 v6; // r12
unsigned int v7; // er14

result = func3(a1, a2);
if ( result )
{
*(_QWORD *)(result + 8) = a3;
}
else
{
v5 = malloc(0x18uLL);
*v5 = a2;
v5[1] = a3;
v5[2] = *(_QWORD *)(a1 + 32);
v6 = func2(a1, a2);
if ( !v6 )
{
v7 = (*(unsigned int (__fastcall **)(__int64))(a1 + 24))(a2) % *(_DWORD *)(a1 + 8);
printf("idx: %d\n", v7);
v6 = linked_list_new(&dict_keycmp);
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v7) = v6;
}
list_add(*(_QWORD *)(a1 + 16), a2);
result = list_add(v6, v5);
++*(_DWORD *)(a1 + 12);
}
return result;
}

original_code:
void hash_table_put(void *data, void *key, void *val)
{
    hash_table_data_t *htdat = (hash_table_data_t *)data;
    dict_entry_t *entry = _hash_table_get_entry(htdat, key);
    list_t *list;
    uint32_t idx;
    if (entry) {
        entry->val = val;
    } else {
        entry = malloc(sizeof(*entry));
        entry->key = key;
        entry->val = val;
        entry->keycmp = htdat->keycmp;
        list = _hash_table_get_list(htdat, key);
        if (!list) {
            idx = (uint32_t)htdat->hash_fn(key) % htdat->table_size;
            printf("idx: %d\n", idx);
            list = linked_list_new(_dict_keycmp);
            htdat->table[idx] = list;
        }
        _list_add(htdat->keys, (void *)key);
        _list_add(list, (void *)entry);
        htdat->size++;
    }
}

canonical_original_code:
void hash_table_put(void *data, void *key, void *val)
{
    struct _hash_table_data *htdat = (hash_table_data_t *)data;
    struct _dict_entry *entry = _hash_table_get_entry(htdat, key);
    struct _list *list;
    unsigned int idx;
    if (entry) {
        entry->val = val;
    } else {
        entry = malloc(sizeof(*entry));
        entry->key = key;
        entry->val = val;
        entry->keycmp = htdat->keycmp;
        list = _hash_table_get_list(htdat, key);
        if (!list) {
            idx = (unsigned int)htdat->hash_fn(key) % htdat->table_size;
            printf("idx: %d\n", idx);
            list = linked_list_new(_dict_keycmp);
            htdat->table[idx] = list;
        }
        _list_add(htdat->keys, (void *)key);
        _list_add(list, (void *)entry);
        htdat->size++;
    }
}

variable_types:
val
void *

variable_types:
idx
unsigned int


decompiled_code:
__int64 __fastcall hash_table_del(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 result; // rax

v2 = hash_table_get_list(a1, a2);
result = hash_table_get_entry(a1, a2);
if ( v2 )
{
if ( result )
{
list_del(v2, result);
result = list_del(*(_QWORD *)(a1 + 16), a2);
--*(_DWORD *)(a1 + 12);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 result; // rax

v2 = func2(a1, a2);
result = func3(a1, a2);
if ( v2 )
{
if ( result )
{
list_del(v2, result);
result = list_del(*(_QWORD *)(a1 + 16), a2);
--*(_DWORD *)(a1 + 12);
}
}
return result;
}

original_code:
void hash_table_del(void *data, void *key)
{
    hash_table_data_t *htdat = (hash_table_data_t *)data;
    list_t *list = _hash_table_get_list(htdat, key);
    dict_entry_t *entry = _hash_table_get_entry(htdat, key);
    if (list && entry) {
        _list_del(list, (void *)entry);;
        _list_del(htdat->keys, (void *)key);;
        htdat->size--;
    }
}

canonical_original_code:
void hash_table_del(void *data, void *key)
{
    struct _hash_table_data *htdat = (hash_table_data_t *)data;
    struct _list *list = _hash_table_get_list(htdat, key);
    struct _dict_entry *entry = _hash_table_get_entry(htdat, key);
    if (list && entry) {
        _list_del(list, (void *)entry);;
        _list_del(htdat->keys, (void *)key);;
        htdat->size--;
    }
}


decompiled_code:
__int64 __fastcall linked_list_get(__int64 *a1, int a2)
{
__int64 v2; // rax

if ( a2 < 0 )
return 0LL;
if ( *((_DWORD *)a1 + 4) < a2 )
return 0LL;
v2 = *a1;
if ( a2 && v2 )
{
do
v2 = *(_QWORD *)(v2 + 8);
while ( v2 );
}
return *(_QWORD *)(v2 + 16);
}

original_code:
void *linked_list_get(void *data, int idx)
{
    linked_list_data_t *lldat = (linked_list_data_t *)data;
    list_entry_t *entry;
    if (idx < 0 || idx > lldat->size)
        return ((void *)0);
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!idx)
            break;
    }
    return entry->data;
}

canonical_original_code:
void *linked_list_get(void *data, int idx)
{
    struct _linked_list_data *lldat = (linked_list_data_t *)data;
    struct _list_entry *entry;
    if (idx < 0 || idx > lldat->size)
        return ((void *)0);
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!idx)
            break;
    }
    return entry->data;
}

variable_types: struct _linked_list_data { struct _list_entry { struct _list_entry *prev; struct _list_entry *next; void *data; } *head; struct _list_entry *tail; int size; int (*keycmp)(void *a, void *b); } *

user_defined_types: struct _linked_list_data { struct _list_entry *head; struct _list_entry *tail; int size; int (*keycmp)(void *a, void *b); }


original_code:
void linked_list_set(void *data, int idx, void *elt)
{
    linked_list_data_t *lldat = (linked_list_data_t *)data;
    list_entry_t *entry;
    if (idx < 0 || idx >= lldat->size)
        return;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!idx)
            break;
    }
    entry->data = elt;
}

canonical_original_code:
void linked_list_set(void *data, int idx, void *elt)
{
    struct _linked_list_data *lldat = (linked_list_data_t *)data;
    struct _list_entry *entry;
    if (idx < 0 || idx >= lldat->size)
        return;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!idx)
            break;
    }
    entry->data = elt;
}

variable_types: struct _list_entry { struct _list_entry *prev; struct _list_entry *next; void *data; } *


decompiled_code:
__int64 __fastcall linked_list_search(__int64 *a1, __int64 a2)
{
__int64 v2; // rbx
unsigned int v3; // ebp

v2 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v3 = 0;
while ( ((unsigned int (__fastcall *)(_QWORD, __int64))a1[3])(*(_QWORD *)(v2 + 16), a2) )
{
++v3;
v2 = *(_QWORD *)(v2 + 8);
if ( !v2 )
return (unsigned int)-1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, __int64 a2)
{
__int64 v2; // rbx
unsigned int v3; // ebp

v2 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v3 = 0;
while ( ((unsigned int (__fastcall *)(_QWORD, __int64))a1[3])(*(_QWORD *)(v2 + 16), a2) )
{
++v3;
v2 = *(_QWORD *)(v2 + 8);
if ( !v2 )
return (unsigned int)-1;
}
return v3;
}

original_code:
int linked_list_search(void *data, void *elt)
{
    linked_list_data_t *lldat = (linked_list_data_t *)data;
    list_entry_t *entry;
    int i = 0;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!lldat->keycmp(entry->data, elt))
            return i;
        i++;
    }
    return -1;
}

canonical_original_code:
int linked_list_search(void *data, void *elt)
{
    struct _linked_list_data *lldat = (linked_list_data_t *)data;
    struct _list_entry *entry;
    int i = 0;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!lldat->keycmp(entry->data, elt))
            return i;
        i++;
    }
    return -1;
}

user_defined_types: struct _list_entry { struct _list_entry *prev; struct _list_entry *next; void *data; }


decompiled_code:
__int64 __fastcall linked_list_size(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
int linked_list_size(void *data)
{
    linked_list_data_t *lldat = (linked_list_data_t *)data;
    return lldat->size;
}

canonical_original_code:
int linked_list_size(void *data)
{
    struct _linked_list_data *lldat = (linked_list_data_t *)data;
    return lldat->size;
}


original_code:
void linked_list_del(void *data, void *elt)
{
    linked_list_data_t *lldat = (linked_list_data_t *)data;
    list_entry_t *entry;
    int i = linked_list_search(data, elt);
    if (i < 0)
        return;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!i--)
            break;
    }
    if (entry->prev) {
        entry->prev->next = entry->next;
    } else {
        lldat->head = entry->next;
    }
    if (entry->next) {
        entry->next->prev = entry->prev;
    } else {
        lldat->tail = entry->prev;
    }
    lldat->size--;
    free(entry);
}

canonical_original_code:
void linked_list_del(void *data, void *elt)
{
    struct _linked_list_data *lldat = (linked_list_data_t *)data;
    struct _list_entry *entry;
    int i = linked_list_search(data, elt);
    if (i < 0)
        return;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        if (!i--)
            break;
    }
    if (entry->prev) {
        entry->prev->next = entry->next;
    } else {
        lldat->head = entry->next;
    }
    if (entry->next) {
        entry->next->prev = entry->prev;
    } else {
        lldat->tail = entry->prev;
    }
    lldat->size--;
    free(entry);
}


decompiled_code:
void __fastcall linked_list_free(void *ptr)
{
_QWORD *v1; // rbx

v1 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
free(v1);
v1 = (_QWORD *)v1[1];
}
while ( v1 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
_QWORD *v1; // rbx

v1 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
free(v1);
v1 = (_QWORD *)v1[1];
}
while ( v1 );
}
free(ptr);
}

original_code:
void linked_list_free(void *data)
{
    linked_list_data_t *lldat = (linked_list_data_t *)data;
    list_entry_t *entry;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        free(entry);
    }
    free(lldat);
}

canonical_original_code:
void linked_list_free(void *data)
{
    struct _linked_list_data *lldat = (linked_list_data_t *)data;
    struct _list_entry *entry;
    for (entry = lldat->head; entry != ((void *)0); entry = entry->next) {
        free(entry);
    }
    free(lldat);
}


main

repo: smabie/unix-utils

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v5; // rsi
__int64 v6; // rdi
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rcx
const char **v10; // rbp
unsigned int v11; // er15
__int64 v12; // rcx
char *v13; // rax
__int64 v14; // r12
__int64 v15; // r13
__int64 v16; // rax
const char **v17; // rbx
int v18; // er14
const char **v19; // r15
FILE *v21; // rbp
char *ptr; // [rsp+8h] [rbp-70h]
const char *v23; // [rsp+18h] [rbp-60h]
char s[88]; // [rsp+20h] [rbp-58h] BYREF

wflg = 0;
lflg = 0;
bflg = 0;
while ( 1 )
{
v5 = argv;
v6 = (unsigned int)argc;
v7 = getopt(argc, (char *const *)argv, "blw");
if ( v7 == -1 )
break;
if ( v7 == 108 )
{
lflg = 1;
}
else if ( v7 == 119 )
{
wflg = 1;
}
else
{
if ( v7 != 98 )
usage((unsigned int)argc, argv, v8, v9);
bflg = 1;
}
}
v10 = &argv[optind];
v11 = argc - optind;
v12 = (unsigned int)(wflg + lflg + bflg);
if ( !(wflg + lflg + bflg) )
lflg = 1;
if ( !v11 || wflg + lflg + bflg > 1 )
usage(v6, v5, (unsigned int)wflg, v12);
strlcpy();
v13 = strchr(s, 45);
if ( v13 )
{
*v13 = 0;
v14 = strtonum();
if ( v23 )
errx(1, "the number %s is %s", s, v23);
v15 = strtonum();
if ( v14 > v15 )
{
v16 = (int)v14;
v14 = v15;
v15 = v16;
}
}
else
{
v14 = strtonum();
v15 = v14;
if ( v23 )
errx(1, "the number %s is %s", s, v23);
}
v17 = v10 + 1;
v18 = v11 - 1;
if ( v11 == 1 )
{
slice((FILE *)stdin, v14, v15);
}
else if ( v18 > 0 )
{
v19 = &v10[v11];
do
{
if ( !strcmp(*v17, "-") )
{
if ( v18 > 1 )
puts("==> stdin <==");
slice((FILE *)stdin, v14, v15);
}
else
{
v21 = fopen(*v17, "r");
if ( !v21 )
err(1, "fopen: %s", *v17);
if ( v18 > 1 )
{
ptr = realpath(*v17, 0LL);
if ( !ptr )
err(1, "realpath: %s", *v17);
printf("==> %s <==\n", ptr);
free(ptr);
}
slice(v21, v14, v15);
fclose(v21);
}
++v17;
}
while ( v17 != v19 );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v5; // rsi
__int64 v6; // rdi
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rcx
const char **v10; // rbp
unsigned int v11; // er15
__int64 v12; // rcx
char *v13; // rax
__int64 v14; // r12
__int64 v15; // r13
__int64 v16; // rax
const char **v17; // rbx
int v18; // er14
const char **v19; // r15
FILE *v21; // rbp
char *ptr; // [rsp+8h] [rbp-70h]
const char *v23; // [rsp+18h] [rbp-60h]
char s[88]; // [rsp+20h] [rbp-58h] BYREF

wflg = 0;
lflg = 0;
bflg = 0;
while ( 1 )
{
v5 = argv;
v6 = (unsigned int)argc;
v7 = getopt(argc, (char *const *)argv, "blw");
if ( v7 == -1 )
break;
if ( v7 == 108 )
{
lflg = 1;
}
else if ( v7 == 119 )
{
wflg = 1;
}
else
{
if ( v7 != 98 )
func7((unsigned int)argc, argv, v8, v9);
bflg = 1;
}
}
v10 = &argv[optind];
v11 = argc - optind;
v12 = (unsigned int)(wflg + lflg + bflg);
if ( !(wflg + lflg + bflg) )
lflg = 1;
if ( !v11 || wflg + lflg + bflg > 1 )
func7(v6, v5, (unsigned int)wflg, v12);
strlcpy();
v13 = strchr(s, 45);
if ( v13 )
{
*v13 = 0;
v14 = strtonum();
if ( v23 )
errx(1, "the number %s is %s", s, v23);
v15 = strtonum();
if ( v14 > v15 )
{
v16 = (int)v14;
v14 = v15;
v15 = v16;
}
}
else
{
v14 = strtonum();
v15 = v14;
if ( v23 )
errx(1, "the number %s is %s", s, v23);
}
v17 = v10 + 1;
v18 = v11 - 1;
if ( v11 == 1 )
{
func8((FILE *)stdin, v14, v15);
}
else if ( v18 > 0 )
{
v19 = &v10[v11];
do
{
if ( !strcmp(*v17, "-") )
{
if ( v18 > 1 )
puts("==> stdin <==");
func8((FILE *)stdin, v14, v15);
}
else
{
v21 = fopen(*v17, "r");
if ( !v21 )
err(1, "fopen: %s", *v17);
if ( v18 > 1 )
{
ptr = realpath(*v17, 0LL);
if ( !ptr )
err(1, "realpath: %s", *v17);
printf("==> %s <==\n", ptr);
free(ptr);
}
func8(v21, v14, v15);
fclose(v21);
}
++v17;
}
while ( v17 != v19 );
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 int c, *fd, nr, nw, fdtmp, n, tmp;
 int aflg;
 char ftmp[] = "/tmp/btee.XXXXXXXXXX";
 char buf[8192];
 aflg = 0;
 while ((c = getopt(argc, argv, "ai")) != -1) {
  switch (c) {
  case 'a':
   aflg = 1;
   break;
  case 'i':
   (void)signal(2, ((__sighandler_t) 1));
   break;
  default:
   (void)fprintf(stderr, "usage: btee [-ai] [file ...]\n");
   exit(1);
  }
 }
 argv += optind;
 argc -= optind;
 if ((fd = calloc(argc + 1, sizeof(int))) == ((void *)0))
  err(1, "calloc");
 if ((fdtmp = mkstemp(ftmp)) == -1)
  err(1, "mkstemp: %s", ftmp);
 while ((tmp = nr = read(0, buf, 8192)) != 0 && nr != -1) {
  do {
   if ((nw = write(fdtmp, buf + tmp - nr, nr)) == -1)
    err(1, "write: %s", ftmp);
  } while (nr -= nw);
 }
 if (nr == -1)
  err(1, "read: stdin");
 for (c = 0; c < argc; c++) {
  if ((fd[c] = open(argv[c],
      aflg ? 01 | 0100 | 02000 :
      01 | 0100 | 01000,
      0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3))) == -1)
   err(1, "open: %s", argv[c]);
 }
 fd[argc] = 1;
 (void)lseek(fdtmp, 0, 0);
 while ((tmp = nr = read(fdtmp, buf, 8192)) != 0 && nr != -1) {
  for (c = 0, n = nr; c < argc + 1; c++, nr = n) {
   do {
    if ((nw = write(fd[c], buf + tmp - nr, nr))
        == -1)
     err(1, "write: %s",
         c == argc ? "stdout" : argv[c]);
   } while (nr -= nw);
  }
 }
 if (nr == -1)
  err(1, "read: %s", ftmp);
 for (c = 0; c < argc; c++)
  (void)close(fd[c]);
 (void)close(fdtmp);
 (void)unlink(ftmp);
 free(fd);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int c; int *fd; int nr; int nw; int fdtmp; int n; int tmp;
 int aflg;
 char ftmp[] = "/tmp/btee.XXXXXXXXXX";
 char buf[8192];
 aflg = 0;
 while ((c = getopt(argc, argv, "ai")) != -1) {
  switch (c) {
  case 'a':
   aflg = 1;
   break;
  case 'i':
   (void)signal(2, ((void (*)(int)) 1));
   break;
  default:
   (void)fprintf(stderr, "usage: btee [-ai] [file ...]\n");
   exit(1);
  }
 }
 argv += optind;
 argc -= optind;
 if ((fd = calloc(argc + 1, sizeof(int))) == ((void *)0))
  err(1, "calloc");
 if ((fdtmp = mkstemp(ftmp)) == -1)
  err(1, "mkstemp: %s", ftmp);
 while ((tmp = nr = read(0, buf, 8192)) != 0 && nr != -1) {
  do {
   if ((nw = write(fdtmp, buf + tmp - nr, nr)) == -1)
    err(1, "write: %s", ftmp);
  } while (nr -= nw);
 }
 if (nr == -1)
  err(1, "read: stdin");
 for (c = 0; c < argc; c++) {
  if ((fd[c] = open(argv[c],
      aflg ? 01 | 0100 | 02000 :
      01 | 0100 | 01000,
      0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3))) == -1)
   err(1, "open: %s", argv[c]);
 }
 fd[argc] = 1;
 (void)lseek(fdtmp, 0, 0);
 while ((tmp = nr = read(fdtmp, buf, 8192)) != 0 && nr != -1) {
  for (c = 0, n = nr; c < argc + 1; c++, nr = n) {
   do {
    if ((nw = write(fd[c], buf + tmp - nr, nr))
        == -1)
     err(1, "write: %s",
         c == argc ? "stdout" : argv[c]);
   } while (nr -= nw);
  }
 }
 if (nr == -1)
  err(1, "read: %s", ftmp);
 for (c = 0; c < argc; c++)
  (void)close(fd[c]);
 (void)close(fdtmp);
 (void)unlink(ftmp);
 free(fd);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int

variable_types:
fd
int *

variable_types: nr int

variable_types: nw int

variable_types: fdtmp int

variable_types: n int

variable_types: tmp int

variable_types: aflg int

variable_types:
ftmp
char []

variable_types:
buf
char [8192]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
FILE *v5; // rax
FILE *v6; // rbp

if ( getopt(argc, (char *const *)argv, "") != -1 )
{
fwrite("usage: pick [file ...]\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
v3 = argv + 1;
tty = fopen("/dev/tty", "r");
if ( !tty )
err(1, "fopen: %s", "/dev/tty");
if ( argc == 1 )
{
ask(stdin);
}
else if ( argc - 1 > 0 )
{
do
{
if ( !strcmp(*v3, "-") )
{
ask(stdin);
}
else
{
v5 = fopen(*v3, "r");
v6 = v5;
if ( !v5 )
err(1, "fopen: %s", *v3);
ask(v5);
fclose(v6);
}
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
FILE *v5; // rax
FILE *v6; // rbp

if ( getopt(argc, (char *const *)argv, "") != -1 )
{
fwrite("usage: pick [file ...]\n", 1uLL, 0x17uLL, stderr);
exit(1);
}
v3 = argv + 1;
tty = fopen("/dev/tty", "r");
if ( !tty )
err(1, "fopen: %s", "/dev/tty");
if ( argc == 1 )
{
func7(stdin);
}
else if ( argc - 1 > 0 )
{
do
{
if ( !strcmp(*v3, "-") )
{
func7(stdin);
}
else
{
v5 = fopen(*v3, "r");
v6 = v5;
if ( !v5 )
err(1, "fopen: %s", *v3);
func7(v5);
fclose(v6);
}
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
}
return 0;
}


bmrktopnt

repo: smaclennan/zedit

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

*(_QWORD *)a2 = a1;
*(_QWORD *)(a2 + 8) = *(_QWORD *)(a1 + 16);
result = *(unsigned int *)(a1 + 32);
*(_DWORD *)(a2 + 16) = result;
return result;
}

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

*(_QWORD *)a2 = a1;
*(_QWORD *)(a2 + 8) = *(_QWORD *)(a1 + 16);
result = *(unsigned int *)(a1 + 32);
*(_DWORD *)(a2 + 16) = result;
return result;
}

original_code:
static inline void bmrktopnt(struct buff *buff, struct mark *mark)
{
 mark->mbuff = buff;
 mark->mpage = buff->curpage;
 mark->moffset = buff->curchar;
}

variable_types: struct buff { struct page { unsigned char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; unsigned char *curcptr; unsigned int curchar; int bmodf; int in_undo; void *undo_tail; } *

variable_types: struct mark { struct buff *mbuff; struct page *mpage; unsigned int moffset; } *

user_defined_types: struct buff { struct page *firstp; struct page *lastp; struct page *curpage; unsigned char *curcptr; unsigned int curchar; int bmodf; int in_undo; void *undo_tail; }


breadfile

original_code:
int breadfile(struct buff *buff, const char *fname, int *compressed)
{
 char buf[4096];
 struct mark start;
 int fd, len;
 fd = open(fname, 00 | 0);
 if (fd < 0)
  return -(*__errno_location ());
 if (compressed)
  *compressed = 0;
 if (((buff)->curpage->plen) && buff->curchar < 4096) {
  pagesplit(buff, buff->curchar);
  if (!newpage(buff))
   return -12;
  makecur(buff, buff->curpage->nextp, 0);
 }
 bmrktopnt(buff, &start);
 while ((len = read(fd, buf, 4096)) > 0) {
  if (((buff)->curpage->plen)) {
   if (!newpage(buff)) {
    close(fd);
    return -12;
   }
   makecur(buff, buff->curpage->nextp, 0);
  }
  memcpy(buff->curcptr, buf, len);
  ((buff)->curpage->plen) = len;
 }
 close(fd);
 bpnttomrk(buff, &start);
 buff->bmodf = 0;
 return 0;
}

canonical_original_code:
int breadfile(struct buff *buff, char *fname, int *compressed)
{
 char buf[4096];
 struct mark start;
 int fd; int len;
 fd = open(fname, 00 | 0);
 if (fd < 0)
  return -(*__errno_location ());
 if (compressed)
  *compressed = 0;
 if (((buff)->curpage->plen) && buff->curchar < 4096) {
  pagesplit(buff, buff->curchar);
  if (!newpage(buff))
   return -12;
  makecur(buff, buff->curpage->nextp, 0);
 }
 bmrktopnt(buff, &start);
 while ((len = read(fd, buf, 4096)) > 0) {
  if (((buff)->curpage->plen)) {
   if (!newpage(buff)) {
    close(fd);
    return -12;
   }
   makecur(buff, buff->curpage->nextp, 0);
  }
  memcpy(buff->curcptr, buf, len);
  ((buff)->curpage->plen) = len;
 }
 close(fd);
 bpnttomrk(buff, &start);
 buff->bmodf = 0;
 return 0;
}

variable_types: struct buff { struct page { char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; char *curcptr; unsigned int curchar; int bmodf; struct mark { struct buff *mbuff; struct page *mpage; unsigned int moffset; struct mark *prev; struct mark *next; } *marks; } *

variable_types:
fname
char *

variable_types:
compressed
int *

variable_types:
buf
char [4096]

variable_types: struct mark { struct buff { struct page { char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; char *curcptr; unsigned int curchar; int bmodf; struct mark *marks; } *mbuff; struct page *mpage; unsigned int moffset; struct mark *prev; struct mark *next; }

variable_types: fd int

variable_types: len int

user_defined_types: struct buff { struct page *firstp; struct page *lastp; struct page *curpage; char *curcptr; unsigned int curchar; int bmodf; struct mark *marks; }


bisend

decompiled_code:
_BOOL8 __fastcall bisend(__int64 a1)
{
__int64 v1; // rdx
_BOOL8 result; // rax

v1 = *(_QWORD *)(a1 + 16);
result = 0LL;
if ( !*(_QWORD *)(v1 + 4112) )
result = *(_DWORD *)(a1 + 32) >= *(_DWORD *)(v1 + 4096);
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1)
{
__int64 v1; // rdx
_BOOL8 result; // rax

v1 = *(_QWORD *)(a1 + 16);
result = 0LL;
if ( !*(_QWORD *)(v1 + 4112) )
result = *(_DWORD *)(a1 + 32) >= *(_DWORD *)(v1 + 4096);
return result;
}

original_code:
static inline int bisend(struct buff *buff)
{
 return buff->curpage->nextp == ((void *)0) &&
  buff->curchar >= buff->curpage->plen;
}

user_defined_types: struct page { unsigned char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; }


makecur

original_code:
static inline void makecur(struct buff *buff, struct page *page, int dist)
{
 buff->curpage = page;
 buff->curchar = dist;
 buff->curcptr = page->pdata + dist;
}

variable_types:
page
struct page *

variable_types: dist int


makeoffset

original_code:
static inline void makeoffset(struct buff *buff, int dist)
{
 buff->curchar = dist;
 buff->curcptr = buff->curpage->pdata + dist;
}


btostart

decompiled_code:
void __fastcall btostart(__int64 *a1)
{
makecur((__int64)a1, *a1, 0);
}

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

original_code:
static inline void btostart(struct buff *buff)
{
 makecur(buff, buff->firstp, 0);
}


btoend

decompiled_code:
void __fastcall btoend(__int64 a1)
{
makecur(a1, *(_QWORD *)(a1 + 8), *(_DWORD *)(*(_QWORD *)(a1 + 8) + 4096LL));
}

original_code:
static inline void btoend(struct buff *buff)
{
 makecur(buff, buff->lastp, buff->lastp->plen);
}


original_code:
static inline void bmove1(struct buff *buff)
{
 if (++buff->curchar < ((buff)->curpage->plen))
  ++buff->curcptr;
 else if (buff->curpage->nextp)
  makecur(buff, buff->curpage->nextp, 0);
 else
  makeoffset(buff, ((buff)->curpage->plen));
}


bmrktopnt

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

*(_QWORD *)a2 = a1;
*(_QWORD *)(a2 + 8) = *(_QWORD *)(a1 + 16);
result = *(unsigned int *)(a1 + 32);
*(_DWORD *)(a2 + 16) = result;
return result;
}

canonical_original_code:
static inline void bmrktopnt(struct buff *buff, struct mark *mark)
{
 mark->mbuff = buff;
 mark->mpage = buff->curpage;
 mark->moffset = buff->curchar;
}

user_defined_types: struct mark { struct buff *mbuff; struct page *mpage; unsigned int moffset; }


mrktomrk

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

*(_QWORD *)a1 = *(_QWORD *)a2;
*(_QWORD *)(a1 + 8) = *(_QWORD *)(a2 + 8);
result = *(unsigned int *)(a2 + 16);
*(_DWORD *)(a1 + 16) = result;
return result;
}

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

*(_QWORD *)a1 = *(_QWORD *)a2;
*(_QWORD *)(a1 + 8) = *(_QWORD *)(a2 + 8);
result = *(unsigned int *)(a2 + 16);
*(_DWORD *)(a1 + 16) = result;
return result;
}

original_code:
static inline void mrktomrk(struct mark *m1, struct mark *m2)
{
 m1->mbuff = m2->mbuff;
 m1->mpage = m2->mpage;
 m1->moffset = m2->moffset;
}

variable_types: struct mark { struct buff { struct page { unsigned char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; unsigned char *curcptr; unsigned int curchar; int bmodf; int in_undo; void *undo_tail; } *mbuff; struct page *mpage; unsigned int moffset; } *


sigchild

decompiled_code:
void sigchild()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code: static void sigchild(int signo) {}

canonical_original_code: static void sigchild(int signo) {}

variable_types: signo int


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

NexterrorCalled = 0;
if ( shell_mark )
return bmrktopnt(Bbuff, shell_mark);
result = bcremark(Bbuff);
shell_mark = result;
return result;
}

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

NexterrorCalled = 0;
if ( shell_mark )
return func6(Bbuff, shell_mark);
result = bcremark(Bbuff);
shell_mark = result;
return result;
}

original_code:
void set_shell_mark(void)
{
 NexterrorCalled = 0;
 if (!shell_mark)
  shell_mark = bcremark(Bbuff);
 else
  bmrktopnt(Bbuff, shell_mark);
}

canonical_original_code:
void set_shell_mark()
{
 NexterrorCalled = 0;
 if (!shell_mark)
  shell_mark = bcremark(Bbuff);
 else
  bmrktopnt(Bbuff, shell_mark);
}


batoi

decompiled_code:
__int64 batoi()
{
__int64 v0; // rdi
char v1; // al
char v2; // al
const unsigned __int16 **v3; // rbp
unsigned int v4; // ebx
unsigned __int8 v5; // al

v0 = Bbuff;
v1 = **(_BYTE **)(Bbuff + 24LL);
if ( v1 == 32 || v1 == 9 )
{
do
{
do
{
bmove1(v0);
v0 = Bbuff;
v2 = **(_BYTE **)(Bbuff + 24LL);
}
while ( v2 == 32 );
}
while ( v2 == 9 );
}
v3 = _ctype_b_loc();
v4 = 0;
while ( 1 )
{
v5 = **(_BYTE **)(Bbuff + 24LL);
if ( ((*v3)[v5] & 0x800) == 0 )
break;
v4 = v5 + 10 * v4 - 48;
bmove1(Bbuff);
}
return v4;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // rdi
char v1; // al
char v2; // al
const unsigned __int16 **v3; // rbp
unsigned int v4; // ebx
unsigned __int8 v5; // al

v0 = Bbuff;
v1 = **(_BYTE **)(Bbuff + 24LL);
if ( v1 == 32 || v1 == 9 )
{
do
{
do
{
func5(v0);
v0 = Bbuff;
v2 = **(_BYTE **)(Bbuff + 24LL);
}
while ( v2 == 32 );
}
while ( v2 == 9 );
}
v3 = _ctype_b_loc();
v4 = 0;
while ( 1 )
{
v5 = **(_BYTE **)(Bbuff + 24LL);
if ( ((*v3)[v5] & 0x800) == 0 )
break;
v4 = v5 + 10 * v4 - 48;
func5(Bbuff);
}
return v4;
}

original_code:
int batoi(void)
{
 int num;
 while ((*Bbuff->curcptr) == ' ' || (*Bbuff->curcptr) == '\t')
  bmove1(Bbuff);
 for (num = 0; ((*__ctype_b_loc ())[(int) (((*Bbuff->curcptr)))] & (unsigned short int) _ISdigit); bmove1(Bbuff))
  num = num * 10 + (*Bbuff->curcptr) - '0';
 return num;
}

canonical_original_code:
int batoi()
{
 int num;
 while ((*Bbuff->curcptr) == ' ' || (*Bbuff->curcptr) == '\t')
  bmove1(Bbuff);
 for (num = 0; ((*__ctype_b_loc ())[(int) (((*Bbuff->curcptr)))] & (unsigned short) _ISdigit); bmove1(Bbuff))
  num = num * 10 + (*Bbuff->curcptr) - '0';
 return num;
}

variable_types: num int


parse

decompiled_code:
__int64 __fastcall parse(unsigned __int8 *a1)
{
const unsigned __int16 **v1; // rbp
unsigned __int8 v3; // al
__int64 result; // rax

while ( !bisend(Bbuff) )
{
v1 = _ctype_b_loc();
while ( 1 )
{
v3 = **(_BYTE **)(Bbuff + 24LL);
if ( ((*v1)[v3] & 0x2000) != 0 || v3 == 58 )
break;
*a1++ = v3;
bmove1(Bbuff);
}
*a1 = 0;
if ( **(_BYTE **)(Bbuff + 24LL) == 58 )
{
bmove1(Bbuff);
result = batoi();
if ( (_DWORD)result )
return result;
}
bcsearch(Bbuff, 10LL);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int8 *a1)
{
const unsigned __int16 **v1; // rbp
unsigned __int8 v3; // al
__int64 result; // rax

while ( !func0(Bbuff) )
{
v1 = _ctype_b_loc();
while ( 1 )
{
v3 = **(_BYTE **)(Bbuff + 24LL);
if ( ((*v1)[v3] & 0x2000) != 0 || v3 == 58 )
break;
*a1++ = v3;
func5(Bbuff);
}
*a1 = 0;
if ( **(_BYTE **)(Bbuff + 24LL) == 58 )
{
func5(Bbuff);
result = func12();
if ( (_DWORD)result )
return result;
}
bcsearch(Bbuff, 10LL);
}
return 0LL;
}

original_code:
static int parse(char *fname)
{
 int line;
 char *p;
 while (!bisend(Bbuff)) {
  p = fname;
  while (!((*__ctype_b_loc ())[(int) (((*Bbuff->curcptr)))] & (unsigned short int) _ISspace) && (*Bbuff->curcptr) != ':') {
   *p++ = (*Bbuff->curcptr);
   bmove1(Bbuff);
  }
  *p = '\0';
  if ((*Bbuff->curcptr) == ':') {
   bmove1(Bbuff);
   line = batoi();
   if (line)
    return line;
  }
  bcsearch(Bbuff, ('\n'));
 }
 return 0;
}

canonical_original_code:
static int parse(char *fname)
{
 int line;
 char *p;
 while (!bisend(Bbuff)) {
  p = fname;
  while (!((*__ctype_b_loc ())[(int) (((*Bbuff->curcptr)))] & (unsigned short) _ISspace) && (*Bbuff->curcptr) != ':') {
   *p++ = (*Bbuff->curcptr);
   bmove1(Bbuff);
  }
  *p = '\0';
  if ((*Bbuff->curcptr) == ':') {
   bmove1(Bbuff);
   line = batoi();
   if (line)
    return line;
  }
  bcsearch(Bbuff, ('\n'));
 }
 return 0;
}

variable_types: line int

variable_types:
p
char *


decompiled_code:
int __fastcall do_chdir(__int64 a1)
{
__int64 v1; // rsi
int result; // eax
char *v3; // rdx
char v4[264]; // [rsp-108h] [rbp-108h] BYREF

v1 = *(_QWORD *)(a1 + 24);
result = 0;
if ( v1 )
{
strlcpy(v4, v1, 256LL);
v3 = strrchr(v4, 47);
result = 0;
if ( v3 )
{
*v3 = 0;
result = chdir(v4);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func14(__int64 a1)
{
__int64 v1; // rsi
int result; // eax
char *v3; // rdx
char v4[264]; // [rsp-108h] [rbp-108h] BYREF

v1 = *(_QWORD *)(a1 + 24);
result = 0;
if ( v1 )
{
strlcpy(v4, v1, 256LL);
v3 = strrchr(v4, 47);
result = 0;
if ( v3 )
{
*v3 = 0;
result = chdir(v4);
}
}
return result;
}

original_code:
int do_chdir(struct zbuff *buff)
{
 if (buff->fname) {
  char dir[255 + 1], *p;
  strlcpy(dir, buff->fname, sizeof(dir));
  p = strrchr(dir, '/');
  if (p) {
   *p = '\0';
   return chdir(dir);
  }
 }
 return 0;
}

canonical_original_code:
int do_chdir(struct zbuff *buff)
{
 if (buff->fname) {
  char dir[-1]; char *p;
  strlcpy(dir, buff->fname, sizeof(dir));
  p = strrchr(dir, '/');
  if (p) {
   *p = '\0';
   return chdir(dir);
  }
 }
 return 0;
}

variable_types: struct zbuff { struct zbuff *prev; struct zbuff *next; char *bname; char *fname; struct mark { struct buff { struct page { unsigned char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; unsigned char *curcptr; unsigned int curchar; int bmodf; int in_undo; void *undo_tail; } *mbuff; struct page *mpage; unsigned int moffset; } *umark; struct buff *buff; long mtime; void *chead; void *ctail; unsigned char comchar; unsigned int bmode; } *

variable_types:
dir
char [-1]

user_defined_types: struct zbuff { struct zbuff *prev; struct zbuff *next; char *bname; char *fname; struct mark *umark; struct buff *buff; long mtime; void *chead; void *ctail; unsigned char comchar; unsigned int bmode; }


message

original_code:
void message(struct zbuff *buff, const char *str)
{
 struct wdo *wdo;
 free(buff->fname);
 buff->fname = strdup(str);
 for (wdo = Whead; wdo; wdo = wdo->next)
  if (wdo->wbuff == buff)
   wdo->modeflags = -1;
 putpaw("%s", str);
}

canonical_original_code:
void message(struct zbuff *buff, char *str)
{
 struct wdo *wdo;
 free(buff->fname);
 buff->fname = strdup(str);
 for (wdo = Whead; wdo; wdo = wdo->next)
  if (wdo->wbuff == buff)
   wdo->modeflags = -1;
 putpaw("%s", str);
}

variable_types:
str
char *

variable_types: struct wdo { struct wdo *next; struct zbuff { struct zbuff *prev; struct zbuff *next; char *bname; char *fname; struct mark { struct buff { struct page { unsigned char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; unsigned char *curcptr; unsigned int curchar; int bmodf; int in_undo; void *undo_tail; } *mbuff; struct page *mpage; unsigned int moffset; } *umark; struct buff *buff; long mtime; void *chead; void *ctail; unsigned char comchar; unsigned int bmode; } *wbuff; struct mark *wpnt; struct mark *wmrk; struct mark *wstart; int umark_set; int first; int modecol; int modeflags; } *

user_defined_types: struct wdo { struct wdo *next; struct zbuff *wbuff; struct mark *wpnt; struct mark *wmrk; struct mark *wstart; int umark_set; int first; int modecol; int modeflags; }


decompiled_code:
__int64 __fastcall exit_status(__int64 a1, __int16 a2)
{
if ( (_BYTE)a2 )
{
message(a1, "Died.");
}
else
{
if ( (a2 & 0xFF00) == 0 )
return message(a1, "Done.");
strfmt(&PawStr, 266LL, "Exit %d.", HIBYTE(a2));
message(a1, (const char *)&PawStr);
}
return tbell();
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int16 a2)
{
if ( (_BYTE)a2 )
{
func15(a1, "Died.");
}
else
{
if ( (a2 & 0xFF00) == 0 )
return func15(a1, "Done.");
strfmt(&PawStr, 266LL, "Exit %d.", HIBYTE(a2));
func15(a1, (const char *)&PawStr);
}
return tbell();
}

original_code:
static void exit_status(struct zbuff *tbuff, int status)
{
 if (status & 0xff)
  message(tbuff, "Died.");
 else {
  status = status >> 8 & 0xff;
  if (status == 0)
   message(tbuff, "Done.");
  else {
   strfmt(PawStr, (256 + 10), "Exit %d.", status);
   message(tbuff, PawStr);
  }
 }
 if (status)
  tbell();
}

variable_types: status int


siginit

decompiled_code:
__sighandler_t siginit()
{
return signal(13, (__sighandler_t)sigchild);
}

canonical_decompiled_code:
__sighandler_t func17()
{
return signal(13, (__sighandler_t)sigchild);
}

original_code:
void siginit(void)
{
 signal(13, sigchild);
}

canonical_original_code:
void siginit()
{
 signal(13, sigchild);
}


checkpipes

decompiled_code:
void __fastcall checkpipes(int a1)
{
__pid_t v1; // eax
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

if ( child != -1 )
{
v1 = waitpid(-1, stat_loc, 1);
if ( v1 > 0 && child == v1 )
{
if ( a1 )
{
while ( (int)readapipe() > 0 )
;
exit_status(pipebuff, stat_loc[0]);
}
set_pipefd(0xFFFFFFFFLL);
close(in_pipe);
in_pipe = -1;
child = -1;
pipebuff = 0LL;
}
}
}

canonical_decompiled_code:
void __fastcall func18(int a1)
{
__pid_t v1; // eax
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

if ( child != -1 )
{
v1 = waitpid(-1, stat_loc, 1);
if ( v1 > 0 && child == v1 )
{
if ( a1 )
{
while ( (int)func19() > 0 )
;
func16(pipebuff, stat_loc[0]);
}
set_pipefd(0xFFFFFFFFLL);
close(in_pipe);
in_pipe = -1;
child = -1;
pipebuff = 0LL;
}
}
}

original_code:
void checkpipes(int type)
{
 int pid = 0, status;
 if (child == -1)
  return;
 pid = waitpid((pid_t)-1, &status, 1);
 if (pid > 0) {
  if (pid == child) {
   if (type) {
    while (readapipe() > 0)
     ;
    exit_status(pipebuff, status);
   }
   set_pipefd(-1);
   (void)close(in_pipe);
   in_pipe = -1;
   child = -1;
   pipebuff = ((void *)0);
  }
 }
}

canonical_original_code:
void checkpipes(int type)
{
 int pid = 0; int status;
 if (child == -1)
  return;
 pid = waitpid((pid_t)-1, &status, 1);
 if (pid > 0) {
  if (pid == child) {
   if (type) {
    while (readapipe() > 0)
     ;
    exit_status(pipebuff, status);
   }
   set_pipefd(-1);
   (void)close(in_pipe);
   in_pipe = -1;
   child = -1;
   pipebuff = ((void *)0);
  }
 }
}

variable_types: type int

variable_types: pid int


readapipe

decompiled_code:
__int64 readapipe()
{
unsigned int v0; // er12
int v1; // ebx
__int64 v2; // r13
char *v3; // rbp
char *v4; // rbx
_BYTE v6[32]; // [rsp+0h] [rbp-448h] BYREF
char buf[1064]; // [rsp+20h] [rbp-428h] BYREF

v0 = 0;
if ( in_pipe != -1 )
{
v1 = read(in_pipe, buf, 0x400uLL);
v0 = v1;
if ( v1 <= 0 )
{
checkpipes(1);
}
else
{
v2 = Curbuff;
zswitchto(pipebuff);
bmrktopnt(Bbuff, (__int64)v6);
btoend(Bbuff);
v3 = &buf[v1 - 1 + 1];
v4 = buf;
do
binsert(Bbuff, (unsigned __int8)*v4++);
while ( v4 != v3 );
bpnttomrk(Bbuff, v6);
zswitchto(v2);
}
}
return v0;
}

canonical_decompiled_code:
__int64 func19()
{
unsigned int v0; // er12
int v1; // ebx
__int64 v2; // r13
char *v3; // rbp
char *v4; // rbx
_BYTE v6[32]; // [rsp+0h] [rbp-448h] BYREF
char buf[1064]; // [rsp+20h] [rbp-428h] BYREF

v0 = 0;
if ( in_pipe != -1 )
{
v1 = read(in_pipe, buf, 0x400uLL);
v0 = v1;
if ( v1 <= 0 )
{
func18(1);
}
else
{
v2 = Curbuff;
zswitchto(pipebuff);
func6(Bbuff, (__int64)v6);
func4(Bbuff);
v3 = &buf[v1 - 1 + 1];
v4 = buf;
do
binsert(Bbuff, (unsigned __int8)*v4++);
while ( v4 != v3 );
bpnttomrk(Bbuff, v6);
zswitchto(v2);
}
}
return v0;
}

original_code:
int readapipe(void)
{
 char buff[1024], *ptr;
 int cnt, i;
 if (in_pipe == -1)
  return 0;
 cnt = i = read(in_pipe, ptr = buff, sizeof(buff));
 if (i > 0) {
  struct mark tmark;
  struct zbuff *save = Curbuff;
  zswitchto(pipebuff);
  bmrktopnt(Bbuff, &tmark);
  btoend(Bbuff);
  while (i-- > 0)
   binsert(Bbuff, *ptr++);
  bpnttomrk(Bbuff, &tmark);
  zswitchto(save);
 } else
  checkpipes(1);
 return cnt;
}

canonical_original_code:
int readapipe()
{
 char buff[1024]; char *ptr;
 int cnt; int i;
 if (in_pipe == -1)
  return 0;
 cnt = i = read(in_pipe, ptr = buff, sizeof(buff));
 if (i > 0) {
  struct mark tmark;
  struct zbuff *save = Curbuff;
  zswitchto(pipebuff);
  bmrktopnt(Bbuff, &tmark);
  btoend(Bbuff);
  while (i-- > 0)
   binsert(Bbuff, *ptr++);
  bpnttomrk(Bbuff, &tmark);
  zswitchto(save);
 } else
  checkpipes(1);
 return cnt;
}

variable_types:
buff
char [1024]

variable_types:
ptr
char *

variable_types: cnt int

variable_types: i int

variable_types: struct mark { struct buff { struct page { unsigned char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; unsigned char *curcptr; unsigned int curchar; int bmodf; int in_undo; void *undo_tail; } *mbuff; struct page *mpage; unsigned int moffset; }


unvoke

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

if ( !pipebuff )
return 0LL;
if ( pipebuff == a1 || (result = 0LL, !a1) )
{
kill(child, 9);
result = 1LL;
}
return result;
}

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

if ( !pipebuff )
return 0LL;
if ( pipebuff == a1 || (result = 0LL, !a1) )
{
kill(child, 9);
result = 1LL;
}
return result;
}

original_code:
_Bool unvoke(struct zbuff *buff)
{
 if (!pipebuff || (buff && buff != pipebuff))
  return 0;
 kill(child, 9);
 return 1;
}


tfini

canonical_decompiled_code:
void func0(void)
{
tcsetattr(0, 2, &save_tty);
tflush();
}

original_code:
static void tfini(void)
{
 tcsetattr(0, 2, &save_tty);
 tflush();
}

canonical_original_code:
static void tfini()
{
 tcsetattr(0, 2, &save_tty);
 tflush();
}


decompiled_code:
void __fastcall __noreturn t_hang_up(int a1)
{
if ( a1 == 15 )
{
terror("\r\nTerminate!\r\n");
}
else if ( a1 == 1 )
{
terror("\r\nHang up!\r\n");
}
else
{
terror("\r\nGot signal!\r\n");
}
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(int a1)
{
if ( a1 == 15 )
{
func2("\r\nTerminate!\r\n");
}
else if ( a1 == 1 )
{
func2("\r\nHang up!\r\n");
}
else
{
func2("\r\nGot signal!\r\n");
}
exit(1);
}

original_code:
static void t_hang_up(int signo)
{
 if (signo == 15)
  terror("\r\nTerminate!\r\n");
 else if (signo == 1)
  terror("\r\nHang up!\r\n");
 else
  terror("\r\nGot signal!\r\n");
 exit(1);
}


tlinit

decompiled_code:
char tlinit()
{
char *v0; // rax
bool v1; // cf
bool v2; // zf
const char *v3; // rdi
__int64 v4; // rcx
char *v5; // rsi
char v6; // dl
bool v7; // cf
bool v8; // zf
__int64 v9; // rcx

v0 = getenv("TERM");
v1 = 0;
v2 = v0 == 0LL;
if ( v0 )
{
v3 = "st";
v4 = 3LL;
v5 = v0;
do
{
if ( !v4 )
break;
v1 = (unsigned __int8)*v5 < (unsigned int)*v3;
v2 = *v5++ == *v3++;
--v4;
}
while ( v2 );
v6 = (!v1 && !v2) - v1;
v7 = 0;
v8 = v6 == 0;
if ( !v6 )
goto LABEL_10;
v3 = "st-";
v9 = 3LL;
v5 = v0;
do
{
if ( !v9 )
break;
v7 = (unsigned __int8)*v5 < (unsigned int)*v3;
v8 = *v5++ == *v3++;
--v9;
}
while ( v8 );
LOBYTE(v0) = (!v7 && !v8) - v7;
if ( !(_BYTE)v0 )
LABEL_10:
LOBYTE(v0) = st_hack(v3, v5);
}
return (char)v0;
}

canonical_decompiled_code:
char func4()
{
char *v0; // rax
bool v1; // cf
bool v2; // zf
const char *v3; // rdi
__int64 v4; // rcx
char *v5; // rsi
char v6; // dl
bool v7; // cf
bool v8; // zf
__int64 v9; // rcx

v0 = getenv("TERM");
v1 = 0;
v2 = v0 == 0LL;
if ( v0 )
{
v3 = "st";
v4 = 3LL;
v5 = v0;
do
{
if ( !v4 )
break;
v1 = (unsigned __int8)*v5 < (unsigned int)*v3;
v2 = *v5++ == *v3++;
--v4;
}
while ( v2 );
v6 = (!v1 && !v2) - v1;
v7 = 0;
v8 = v6 == 0;
if ( !v6 )
goto LABEL_10;
v3 = "st-";
v9 = 3LL;
v5 = v0;
do
{
if ( !v9 )
break;
v7 = (unsigned __int8)*v5 < (unsigned int)*v3;
v8 = *v5++ == *v3++;
--v9;
}
while ( v8 );
LOBYTE(v0) = (!v7 && !v8) - v7;
if ( !(_BYTE)v0 )
LABEL_10:
LOBYTE(v0) = st_hack(v3, v5);
}
return (char)v0;
}

original_code:
static void tlinit(void)
{
 char *term = getenv("TERM");
 if (term)
  if (strcmp(term, "st") == 0 || strncmp(term, "st-", 3) == 0)
   st_hack();
}

canonical_original_code:
static void tlinit()
{
 char *term = getenv("TERM");
 if (term)
  if (strcmp(term, "st") == 0 || strncmp(term, "st-", 3) == 0)
   st_hack();
}

variable_types:
term
char *


tinit

decompiled_code:
int tinit()
{
char *v0; // rax

tcgetattr(0, &save_tty);
tcgetattr(0, &settty);
settty.c_iflag = 0;
settty.c_oflag = 6144;
settty.c_lflag = 48;
settty.c_cc[6] = 1;
settty.c_cc[5] = 1;
tcsetattr(0, 0, &settty);
signal(1, (__sighandler_t)t_hang_up);
signal(15, (__sighandler_t)t_hang_up);
v0 = getenv("NO_TSIZE");
if ( v0 )
no_tsize = strtol(v0, 0LL, 0);
tlinit();
return atexit(tfini);
}

canonical_decompiled_code:
int func5()
{
char *v0; // rax

tcgetattr(0, &save_tty);
tcgetattr(0, &settty);
settty.c_iflag = 0;
settty.c_oflag = 6144;
settty.c_lflag = 48;
settty.c_cc[6] = 1;
settty.c_cc[5] = 1;
tcsetattr(0, 0, &settty);
signal(1, (__sighandler_t)t_hang_up);
signal(15, (__sighandler_t)t_hang_up);
v0 = getenv("NO_TSIZE");
if ( v0 )
no_tsize = strtol(v0, 0LL, 0);
func4();
return atexit(tfini);
}

original_code:
void tinit(void)
{
 tcgetattr(0, &save_tty);
 tcgetattr(0, &settty);
 settty.c_iflag = 0;
 settty.c_oflag = 0014000;
 settty.c_lflag = 0000020 | 0000040;
 settty.c_cc[6] = (char) 1;
 settty.c_cc[5] = (char) 1;
 tcsetattr(0, 0, &settty);
 signal(1, t_hang_up);
 signal(15, t_hang_up);
 char *env = getenv("NO_TSIZE");
 if (env)
  no_tsize = strtol(env, ((void *)0), 0);
 tlinit();
 atexit(tfini);
}

canonical_original_code:
void tinit()
{
 tcgetattr(0, &save_tty);
 tcgetattr(0, &settty);
 settty.c_iflag = 0;
 settty.c_oflag = 0014000;
 settty.c_lflag = 0000020 | 0000040;
 settty.c_cc[6] = (char) 1;
 settty.c_cc[5] = (char) 1;
 tcsetattr(0, 0, &settty);
 signal(1, t_hang_up);
 signal(15, t_hang_up);
 char *env = getenv("NO_TSIZE");
 if (env)
  no_tsize = strtol(env, ((void *)0), 0);
 tlinit();
 atexit(tfini);
}

variable_types:
env
char *


tabsize

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

if ( Tabsize <= 8u && ((1LL << Tabsize) & 0x114) != 0 )
result = Tabsize - (a1 & (unsigned int)(Tabsize - 1));
else
result = (unsigned int)(Tabsize - a1 % Tabsize);
return result;
}

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

if ( Tabsize <= 8u && ((1LL << Tabsize) & 0x114) != 0 )
result = Tabsize - (a1 & (unsigned int)(Tabsize - 1));
else
result = (unsigned int)(Tabsize - a1 % Tabsize);
return result;
}

original_code:
static int tabsize(int col)
{
 switch (Tabsize) {
 case 2:
 case 4:
 case 8:
  return Tabsize - (col & (Tabsize - 1));
 default:
  return Tabsize - (col % Tabsize);
 }
}

variable_types: col int


tafini

decompiled_code:
void tafini(void)
{
putpaw(&unk_4DF);
t_goto((unsigned int)(Rowmax - 1), 0LL);
tstyle(0LL);
}

canonical_decompiled_code:
void func4(void)
{
putpaw(&unk_4DF);
t_goto((unsigned int)(Rowmax - 1), 0LL);
tstyle(0LL);
}

original_code:
static void tafini(void)
{
 putpaw("");
 t_goto(Rowmax - 1, 0);
 tstyle(0);
}

canonical_original_code:
static void tafini()
{
 putpaw("");
 t_goto(Rowmax - 1, 0);
 tstyle(0);
}


termsize

decompiled_code:
__int64 termsize()
{
int v0; // eax
__int64 result; // rax
int v2; // [rsp+8h] [rbp-10h] BYREF
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = 0;
v2 = 0;
tsize(v3, &v2);
v0 = 24;
if ( v3[0] > 0 )
{
v0 = 110;
if ( v3[0] <= 110 )
v0 = v3[0];
}
Rowmax = v0;
result = 80LL;
if ( v2 > 0 )
{
result = 256LL;
if ( v2 <= 256 )
result = (unsigned int)v2;
}
Colmax = result;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // eax
__int64 result; // rax
int v2; // [rsp+8h] [rbp-10h] BYREF
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = 0;
v2 = 0;
tsize(v3, &v2);
v0 = 24;
if ( v3[0] > 0 )
{
v0 = 110;
if ( v3[0] <= 110 )
v0 = v3[0];
}
Rowmax = v0;
result = 80LL;
if ( v2 > 0 )
{
result = 256LL;
if ( v2 <= 256 )
result = (unsigned int)v2;
}
Colmax = result;
return result;
}

original_code:
void termsize(void)
{
 int rows = 0, cols = 0;
 tsize(&rows, &cols);
 Rowmax = rows <= 0 ? 24 : (rows < 110 ? rows : 110);
 Colmax = cols <= 0 ? 80 : (cols < 256 ? cols : 256);
}

canonical_original_code:
void termsize()
{
 int rows = 0; int cols = 0;
 tsize(&rows, &cols);
 Rowmax = rows <= 0 ? 24 : (rows < 110 ? rows : 110);
 Colmax = cols <= 0 ? 80 : (cols < 256 ? cols : 256);
}

variable_types: rows int

variable_types: cols int


sigwinch

decompiled_code:
void sigwinch()
{
if ( Initializing )
{
termsize();
}
else
{
Zredisplay();
zrefresh();
}
}

canonical_decompiled_code:
void func6()
{
if ( Initializing )
{
func5();
}
else
{
Zredisplay();
zrefresh();
}
}

original_code:
static void sigwinch(int sig)
{
 if (Initializing)
  termsize();
 else {
  Zredisplay();
  zrefresh();
 }
}

variable_types: sig int


tprntstr

decompiled_code:
__int64 __fastcall tprntstr(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbx
unsigned __int8 v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
++v1;
result = tprntchar(v2);
v2 = *v1;
}
while ( *v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbx
unsigned __int8 v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
++v1;
result = func9(v2);
v2 = *v1;
}
while ( *v1 );
}
return result;
}

original_code:
void tprntstr(const char *str)
{
 while (*str)
  tprntchar(*str++);
}

canonical_original_code:
void tprntstr(char *str)
{
 while (*str)
  tprntchar(*str++);
}


initline

decompiled_code:
__int64 initline()
{
int v0; // ebx
int i; // ebx

v0 = strconcat(&PawStr, 266LL, "Zedit", " ", "6.3", " Initializing", 0LL);
tclrwind();
t_goto((unsigned int)(Rowmax - 2), 0LL);
tstyle(7LL);
tprntstr((unsigned __int8 *)&PawStr);
for ( i = v0 + 1; Colmax > i; ++i )
tprntchar(32);
tstyle(0LL);
t_goto(0LL, 0LL);
return tflush();
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // ebx
int i; // ebx

v0 = strconcat(&PawStr, 266LL, "Zedit", " ", "6.3", " Initializing", 0LL);
tclrwind();
t_goto((unsigned int)(Rowmax - 2), 0LL);
tstyle(7LL);
func8((unsigned __int8 *)&PawStr);
for ( i = v0 + 1; Colmax > i; ++i )
func9(32);
tstyle(0LL);
t_goto(0LL, 0LL);
return tflush();
}

original_code:
static void initline(void)
{
 int i = strconcat(PawStr, (256 + 10), "Zedit",
       " ", "6.3", "  Initializing", ((void *)0));
 tclrwind();
 t_goto(Rowmax - 2, 0);
 tstyle(7);
 tprntstr(PawStr);
 for (++i; i < Colmax; ++i)
  tprntchar(' ');
 tstyle(0);
 t_goto(0, 0);
 tflush();
}

canonical_original_code:
static void initline()
{
 int i = strconcat(PawStr, (256 + 10), "Zedit",
       " ", "6.3", "  Initializing", ((void *)0));
 tclrwind();
 t_goto(Rowmax - 2, 0);
 tstyle(7);
 tprntstr(PawStr);
 for (++i; i < Colmax; ++i)
  tprntchar(' ');
 tstyle(0);
 t_goto(0, 0);
 tflush();
}


tainit

decompiled_code:
int tainit()
{
signal(1, (__sighandler_t)hang_up);
signal(15, (__sighandler_t)hang_up);
signal(28, (__sighandler_t)sigwinch);
siginit();
termsize();
if ( Rowmax <= 2 )
exit(1);
initline();
return atexit(tafini);
}

canonical_decompiled_code:
int func11()
{
signal(1, (__sighandler_t)hang_up);
signal(15, (__sighandler_t)hang_up);
signal(28, (__sighandler_t)sigwinch);
siginit();
func5();
if ( Rowmax <= 2 )
exit(1);
func10();
return atexit(tafini);
}

original_code:
void tainit(void)
{
 signal(1, hang_up);
 signal(15, hang_up);
 signal(28, sigwinch);
 siginit();
 termsize();
 if (Rowmax < 3) {
  exit(1);
 }
 initline();
 atexit(tafini);
}

canonical_original_code:
void tainit()
{
 signal(1, hang_up);
 signal(15, hang_up);
 signal(28, sigwinch);
 siginit();
 termsize();
 if (Rowmax < 3) {
  exit(1);
 }
 initline();
 atexit(tafini);
}


prefline

decompiled_code:
__int64 prefline()
{
int v0; // ecx
__int64 result; // rax

v0 = *(_DWORD *)(Curwdo + 48LL) - *(_DWORD *)(Curwdo + 44LL);
result = (unsigned int)(10 * v0 / (Rowmax - 2));
if ( v0 <= (int)result )
result = (unsigned int)((*(_DWORD *)(Curwdo + 48LL) - *(_DWORD *)(Curwdo + 44LL)) >> 1);
return result;
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // ecx
__int64 result; // rax

v0 = *(_DWORD *)(Curwdo + 48LL) - *(_DWORD *)(Curwdo + 44LL);
result = (unsigned int)(10 * v0 / (Rowmax - 2));
if ( v0 <= (int)result )
result = (unsigned int)((*(_DWORD *)(Curwdo + 48LL) - *(_DWORD *)(Curwdo + 44LL)) >> 1);
return result;
}

original_code:
int prefline(void)
{
 int line, w;
 w = (Curwdo->last - Curwdo->first);
 line = 10 * w / (Rowmax - 2);
 return line < w ? line : w >> 1;
}

canonical_original_code:
int prefline()
{
 int line; int w;
 w = (Curwdo->last - Curwdo->first);
 line = 10 * w / (Rowmax - 2);
 return line < w ? line : w >> 1;
}

variable_types: line int

variable_types: w int


tbell

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

ring_bell = 1;
result = Curwdo;
if ( Curwdo )
*(_DWORD *)(Curwdo + 56LL) = -1;
return result;
}

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

ring_bell = 1;
result = Curwdo;
if ( Curwdo )
*(_DWORD *)(Curwdo + 56LL) = -1;
return result;
}

original_code:
void tbell(void)
{
 ring_bell = 1;
 if (Curwdo)
  Curwdo->modeflags = -1;
}

canonical_original_code:
void tbell()
{
 ring_bell = 1;
 if (Curwdo)
  Curwdo->modeflags = -1;
}


outchar

decompiled_code:
unsigned __int8 *__fastcall outchar(_QWORD *a1, unsigned __int8 a2)
{
unsigned __int8 *result; // rax

++*((_DWORD *)a1 + 5);
result = (unsigned __int8 *)*a1;
if ( *a1 )
return (unsigned __int8 *)binsert(*a1, a2);
if ( *((int *)a1 + 4) > 0 )
{
result = (unsigned __int8 *)a1[1];
a1[1] = result + 1;
*result = a2;
--*((_DWORD *)a1 + 4);
}
return result;
}

canonical_decompiled_code:
unsigned __int8 *__fastcall func1(_QWORD *a1, unsigned __int8 a2)
{
unsigned __int8 *result; // rax

++*((_DWORD *)a1 + 5);
result = (unsigned __int8 *)*a1;
if ( *a1 )
return (unsigned __int8 *)binsert(*a1, a2);
if ( *((int *)a1 + 4) > 0 )
{
result = (unsigned __int8 *)a1[1];
a1[1] = result + 1;
*result = a2;
--*((_DWORD *)a1 + 4);
}
return result;
}

original_code:
static void outchar(struct outbuff *out, char ch)
{
 out->n++;
 if (out->buff) {
  binsert(out->buff, ch);
  return;
 }
 if (out->len > 0) {
  *out->str++ = ch;
  --out->len;
 }
}

variable_types: struct outbuff { struct buff { struct page { char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; } *firstp; struct page *lastp; struct page *curpage; char *curcptr; unsigned int curchar; int bmodf; struct mark { struct buff *mbuff; struct page *mpage; unsigned int moffset; struct mark *prev; struct mark *next; } *marks; } *buff; char *str; int len; int n; } *

variable_types: ch char

user_defined_types: struct outbuff { struct buff *buff; char *str; int len; int n; }


outmemcpy

original_code:
static void outmemcpy(struct outbuff *out, const char *str, int size)
{
 if (size > 0) {
  out->n += size;
  if (out->buff) {
   for (int i = 0; i < size; ++i)
    binsert(out->buff, str[i]);
   return;
  }
  if (size > out->len)
   size = out->len;
  if (size > 0) {
   memcpy(out->str, str, size);
   out->str += size;
   out->len -= size;
  }
 }
}

canonical_original_code:
static void outmemcpy(struct outbuff *out, char *str, int size)
{
 if (size > 0) {
  out->n += size;
  if (out->buff) {
   for (int i = 0; i < size; ++i)
    binsert(out->buff, str[i]);
   return;
  }
  if (size > out->len)
   size = out->len;
  if (size > 0) {
   memcpy(out->str, str, size);
   out->str += size;
   out->len -= size;
  }
 }
}

variable_types:
str
char *

variable_types: size int

user_defined_types: struct page { char pdata[4096]; unsigned int plen; struct page *prevp; struct page *nextp; }

user_defined_types: struct mark { struct buff *mbuff; struct page *mpage; unsigned int moffset; struct mark *prev; struct mark *next; }


outmemset

decompiled_code:
void *__fastcall outmemset(__int64 a1, unsigned __int8 a2, int a3)
{
int v3; // ebx
int i; // er12
void *result; // rax

if ( a3 > 0 )
{
v3 = a3;
*(_DWORD *)(a1 + 20) += a3;
if ( *(_QWORD *)a1 )
{
for ( i = 0; i != v3; ++i )
result = (void *)binsert(*(_QWORD *)a1, a2);
}
else
{
if ( *(_DWORD *)(a1 + 16) <= a3 )
v3 = *(_DWORD *)(a1 + 16);
if ( v3 > 0 )
{
result = memset(*(void **)(a1 + 8), (char)a2, v3);
*(_QWORD *)(a1 + 8) += v3;
*(_DWORD *)(a1 + 16) -= v3;
}
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, unsigned __int8 a2, int a3)
{
int v3; // ebx
int i; // er12
void *result; // rax

if ( a3 > 0 )
{
v3 = a3;
*(_DWORD *)(a1 + 20) += a3;
if ( *(_QWORD *)a1 )
{
for ( i = 0; i != v3; ++i )
result = (void *)binsert(*(_QWORD *)a1, a2);
}
else
{
if ( *(_DWORD *)(a1 + 16) <= a3 )
v3 = *(_DWORD *)(a1 + 16);
if ( v3 > 0 )
{
result = memset(*(void **)(a1 + 8), (char)a2, v3);
*(_QWORD *)(a1 + 8) += v3;
*(_DWORD *)(a1 + 16) -= v3;
}
}
}
return result;
}

original_code:
static void outmemset(struct outbuff *out, char c, int size)
{
 if (size > 0) {
  out->n += size;
  if (out->buff) {
   for (int i = 0; i < size; ++i)
    binsert(out->buff, c);
   return;
  }
  if (size > out->len)
   size = out->len;
  if (size > 0) {
   memset(out->str, c, size);
   out->str += size;
   out->len -= size;
  }
 }
}

variable_types: c char