command

decompiled_code:
__int64 command()
{
int v0; // eax
unsigned int v1; // er12
int v2; // eax
__int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
char v5; // [rsp+Ch] [rbp-1Ch]
__int16 v6; // [rsp+Dh] [rbp-1Bh] BYREF
char v7; // [rsp+Fh] [rbp-19h]
const char *v8; // [rsp+10h] [rbp-18h] BYREF
__int64 v9; // [rsp+18h] [rbp-10h] BYREF

if ( (int)parse_addr((const char *)remote_host, &v8, &v9) < 0 )
return (unsigned int)-1;
v4 = -4082;
v6 = eoj_code;
v5 = 1;
v7 = byte_12956;
mls_elnet_setup_frame_header(&req, &v4, &v6, (unsigned __int8)esv_code, 1LL, 1LL);
v0 = setup_frame_data_constprop_0(set_prop_len, get_prop_len);
v1 = v0;
if ( v0 < 0 )
{
fprintf(stderr, "Error _setup_frame_data(%d)\n", (unsigned int)v0);
}
else
{
v2 = mls_elnet_rpc(cln_ctx, v8, v9, &req, (unsigned int)(v0 + 11), &res, 0x8000LL);
if ( v2 < 0 )
{
v1 = -10;
fprintf(stderr, "Error mls_elnet_rpc(%d)\n", (unsigned int)v2);
}
else if ( (byte_94A & 0xF0) == 80 )
{
v1 = -11;
fprintf(stderr, "Error response code (%02X)\n", (unsigned __int8)byte_94A);
}
else
{
output_data_constprop_0();
v1 = 0;
}
}
return v1;
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // eax
unsigned int v1; // er12
int v2; // eax
__int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
char v5; // [rsp+Ch] [rbp-1Ch]
__int16 v6; // [rsp+Dh] [rbp-1Bh] BYREF
char v7; // [rsp+Fh] [rbp-19h]
const char *v8; // [rsp+10h] [rbp-18h] BYREF
__int64 v9; // [rsp+18h] [rbp-10h] BYREF

if ( (int)func5((const char *)remote_host, &v8, &v9) < 0 )
return (unsigned int)-1;
v4 = -4082;
v6 = eoj_code;
v5 = 1;
v7 = byte_12956;
mls_elnet_setup_frame_header(&req, &v4, &v6, (unsigned __int8)esv_code, 1LL, 1LL);
v0 = func9(set_prop_len, get_prop_len);
v1 = v0;
if ( v0 < 0 )
{
fprintf(stderr, "Error _setup_frame_data(%d)\n", (unsigned int)v0);
}
else
{
v2 = mls_elnet_rpc(cln_ctx, v8, v9, &req, (unsigned int)(v0 + 11), &res, 0x8000LL);
if ( v2 < 0 )
{
v1 = -10;
fprintf(stderr, "Error mls_elnet_rpc(%d)\n", (unsigned int)v2);
}
else if ( (byte_94A & 0xF0) == 80 )
{
v1 = -11;
fprintf(stderr, "Error response code (%02X)\n", (unsigned __int8)byte_94A);
}
else
{
func7();
v1 = 0;
}
}
return v1;
}

original_code:
static int
command(void)
{
    int ret = 0;
    char *addr, *port;
    struct mls_elnet_frame *req = (struct mls_elnet_frame*)_req;
    unsigned int reqlen = sizeof(_req);
    struct mls_elnet_frame *res = (struct mls_elnet_frame*)_res;
    int reslen = sizeof(_res);
    if (parse_addr(_remote_host, &addr, &port) < 0) {
        ret = -1;
        goto out;
    }
    {
        int dlen;
        struct mls_eoj_code seoj, deoj;
        seoj.cgc = ((unsigned char)0x0E);
        seoj.clc = ((unsigned char)0xF0);
        seoj.inc = 0x01;
        deoj = _eoj_code;
        mls_elnet_setup_frame_header(req, &seoj, &deoj, _esv_code, 1, 1);
        dlen = _setup_frame_data((req->data - 1),
                                 (reqlen - sizeof(struct mls_elnet_frame)),
                                 _set_prop, _set_prop_len, _get_prop, _get_prop_len);
        if (dlen < 0) {
            do{ fprintf(stderr, "Error _setup_frame_data(%d)\n", dlen); }while(0);
            ret = dlen;
            goto out;
        }
        reqlen = sizeof(struct mls_elnet_frame) - 1 + dlen;
    }
    reslen = mls_elnet_rpc(_cln_ctx, addr, port, req, reqlen, res, reslen);
    if (reslen < 0) {
        do{ fprintf(stderr, "Error mls_elnet_rpc(%d)\n", reslen); }while(0);
        ret = -10;
        goto out;
    }
    if ((((res->esv) & (unsigned char)0xf0) == ((unsigned char)0x50))) {
        do{ fprintf(stderr, "Error response code (%02X)\n", res->esv); }while(0);
        ret = -11;
        goto out;
    }
    output_data(&(res->opc));
    ret = 0;
out:
    return ret;
}

canonical_original_code:
static int command()
{
    int ret = 0;
    char *addr; char *port;
    struct mls_elnet_frame *req = (struct mls_elnet_frame*)_req;
    unsigned int reqlen = sizeof(_req);
    struct mls_elnet_frame *res = (struct mls_elnet_frame*)_res;
    int reslen = sizeof(_res);
    if (parse_addr(_remote_host, &addr, &port) < 0) {
        ret = -1;
        goto out;
    }
    {
        int dlen;
        struct mls_eoj_code seoj; struct mls_eoj_code deoj;
        seoj.cgc = ((unsigned char)0x0E);
        seoj.clc = ((unsigned char)0xF0);
        seoj.inc = 0x01;
        deoj = _eoj_code;
        mls_elnet_setup_frame_header(req, &seoj, &deoj, _esv_code, 1, 1);
        dlen = _setup_frame_data((req->data - 1),
                                 (reqlen - sizeof(struct mls_elnet_frame)),
                                 _set_prop, _set_prop_len, _get_prop, _get_prop_len);
        if (dlen < 0) {
            do{ fprintf(stderr, "Error _setup_frame_data(%d)\n", dlen); }while(0);
            ret = dlen;
            goto out;
        }
        reqlen = sizeof(struct mls_elnet_frame) - 1 + dlen;
    }
    reslen = mls_elnet_rpc(_cln_ctx, addr, port, req, reqlen, res, reslen);
    if (reslen < 0) {
        do{ fprintf(stderr, "Error mls_elnet_rpc(%d)\n", reslen); }while(0);
        ret = -10;
        goto out;
    }
    if ((((res->esv) & (unsigned char)0xf0) == ((unsigned char)0x50))) {
        do{ fprintf(stderr, "Error response code (%02X)\n", res->esv); }while(0);
        ret = -11;
        goto out;
    }
    output_data(&(res->opc));
    ret = 0;
out:
    return ret;
}

variable_types:
addr
char *

variable_types:
port
char *

variable_types: struct mls_elnet_frame { unsigned char ehd1; unsigned char ehd2; unsigned short tid; struct mls_eoj_code { unsigned char cgc; unsigned char clc; unsigned char inc; } seoj; struct mls_eoj_code deoj; unsigned char esv; unsigned char opc; unsigned char data[]; } *

variable_types:
reqlen
unsigned int

variable_types: reslen int

variable_types: dlen int

user_defined_types: struct mls_elnet_frame { unsigned char ehd1; unsigned char ehd2; unsigned short tid; struct mls_eoj_code seoj; struct mls_eoj_code deoj; unsigned char esv; unsigned char opc; unsigned char data[]; }


main

canonical_name: main

repo: kahnn/Mulus

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12

mls_el_ini(argc, argv, envp);
v3 = 0;
if ( (int)parse_args(argc, (char **)argv) >= 0 )
{
v3 = open_sock((__int64)ifname);
if ( !v3 )
v3 = command();
}
if ( cln_ctx )
mls_net_ucast_close_ctx();
mls_el_fin();
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12

mls_el_ini(argc, argv, envp);
v3 = 0;
if ( (int)func3(argc, (char **)argv) >= 0 )
{
v3 = func4((__int64)ifname);
if ( !v3 )
v3 = func10();
}
if ( cln_ctx )
mls_net_ucast_close_ctx();
mls_el_fin();
return v3;
}

original_code:
int
main(int argc, char* argv[])
{
    int ret = 0;
    (void)mls_el_ini();
    if (parse_args(argc, argv) < 0) {
        goto out;
    }
    if ((ret = open_sock(_ifname)) != 0) {
        goto out;
    }
    if ((ret = command()) != 0) {
        goto out;
    }
out:
    if (((void *)0) != _cln_ctx) {
        mls_net_ucast_close_ctx((struct mls_net_ucast_ctx*)_cln_ctx);
    }
    mls_el_fin();
    return ret;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int ret = 0;
    (void)mls_el_ini();
    if (parse_args(argc, argv) < 0) {
        goto out;
    }
    if ((ret = open_sock(_ifname)) != 0) {
        goto out;
    }
    if ((ret = command()) != 0) {
        goto out;
    }
out:
    if (((void *)0) != _cln_ctx) {
        mls_net_ucast_close_ctx((struct mls_net_ucast_ctx *)_cln_ctx);
    }
    mls_el_fin();
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

return_type: int

user_defined_types: struct mls_net_ucast_ctx { int sock; struct sockaddr_storage to; unsigned int tolen; struct sockaddr_storage from; unsigned int fromlen; }

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


decompiled_code:
__int64 __fastcall mls_dlink_insert(__int64 *a1, _QWORD *a2)
{
__int64 result; // rax

result = *a1;
a2[1] = a1;
*a2 = result;
*(_QWORD *)(result + 8) = a2;
*a1 = (__int64)a2;
return result;
}

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

result = *a1;
a2[1] = a1;
*a2 = result;
*(_QWORD *)(result + 8) = a2;
*a1 = (__int64)a2;
return result;
}

original_code:
static inline void mls_dlink_insert(mls_dlink_t *base, mls_dlink_t *dl)
{
    dl->prev = base->prev;
    dl->next = base;
    base->prev->next = dl;
    base->prev = dl;
}

canonical_original_code:
static inline void mls_dlink_insert(struct mls_dlink *base, struct mls_dlink *dl)
{
    dl->prev = base->prev;
    dl->next = base;
    base->prev->next = dl;
    base->prev = dl;
}

variable_types: struct mls_dlink { struct mls_dlink *prev; struct mls_dlink *next; } *

variable_types:
dl
struct mls_dlink *

return_type: void


decompiled_code:
__int64 __fastcall mls_evt_add_handle(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
char *v9; // rax
_QWORD *v10; // rsi

v9 = (char *)malloc(0x30uLL);
if ( !v9 )
return (unsigned int)-*_errno_location();
*(_DWORD *)v9 = a2;
v10 = v9 + 32;
*((_DWORD *)v9 + 1) = a3;
*((_QWORD *)v9 + 1) = a4;
*((_QWORD *)v9 + 2) = a5;
*((_QWORD *)v9 + 3) = a1;
*((_OWORD *)v9 + 2) = 0LL;
mls_dlink_init((_QWORD *)v9 + 4);
if ( a2 )
mls_dlink_insert((__int64 *)(a1 + 24), v10);
else
mls_dlink_insert((__int64 *)(a1 + 8), v10);
return 0LL;
}

original_code:
int
mls_evt_add_handle(struct mls_evt *evt,
    enum mls_handle_type type, int fd,
    mls_evt_callback_t callback, void *tag)
{
    int ret = 0;
    struct mls_evt_handle* handle = ((void *)0);
    handle = malloc(sizeof(struct mls_evt_handle));
    if (((void *)0) == handle) {
        ret = -(*__errno_location ());
        goto out;
    }
    memset(handle, 0, sizeof(*handle));
    handle->type = type;
    handle->fd = fd;
    handle->callback = callback;
    handle->tag = tag;
    handle->evt = evt;
    mls_dlink_init(&(handle->dlink));
    if (MLS_EVT_FD == type) {
        mls_dlink_insert((&(evt->dlink_fd)), (&(handle->dlink)));
    } else {
        mls_dlink_insert((&(evt->dlink_to)), (&(handle->dlink)));
    }
out:
    return ret;
}

canonical_original_code:
int mls_evt_add_handle(struct mls_evt *evt, enum mls_handle_type type, int fd, void (*callback)(struct mls_evt *, void *), void *tag)
{
    int ret = 0;
    struct mls_evt_handle *handle = ((void *)0);
    handle = malloc(sizeof(struct mls_evt_handle));
    if (((void *)0) == handle) {
        ret = -(*__errno_location ());
        goto out;
    }
    memset(handle, 0, sizeof(*handle));
    handle->type = type;
    handle->fd = fd;
    handle->callback = callback;
    handle->tag = tag;
    handle->evt = evt;
    mls_dlink_init(&(handle->dlink));
    if (MLS_EVT_FD == type) {
        mls_dlink_insert((&(evt->dlink_fd)), (&(handle->dlink)));
    } else {
        mls_dlink_insert((&(evt->dlink_to)), (&(handle->dlink)));
    }
out:
    return ret;
}

variable_types: fd int

variable_types:
callback
void (*)(struct mls_evt *, void *)

variable_types:
tag
void *

variable_types: struct mls_evt_handle { enum mls_handle_type { MLS_EVT_FD=0, MLS_EVT_TIMEOUT=1 } type; int fd; void (*callback)(struct mls_evt { int timeout; int is_active; struct mls_dlink { struct mls_dlink *prev; struct mls_dlink *next; } dlink_fd; struct mls_dlink dlink_to; } *, void *); void *tag; struct mls_evt *evt; struct mls_dlink dlink; } *

user_defined_types: struct mls_evt_handle { enum mls_handle_type type; int fd; void (*callback)(struct mls_evt *, void *); void *tag; struct mls_evt *evt; struct mls_dlink dlink; }

user_defined_types: struct mls_evt { int timeout; int is_active; struct mls_dlink dlink_fd; struct mls_dlink dlink_to; }


decompiled_code:
void __fastcall mls_evt_deactivate(__int64 a1)
{
*(_DWORD *)(a1 + 4) = 0;
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1)
{
*(_DWORD *)(a1 + 4) = 0;
}

original_code:
void
mls_evt_deactivate(struct mls_evt *evt)
{
    evt->is_active = 0;
}

variable_types: struct mls_evt { int timeout; int is_active; struct mls_dlink { struct mls_dlink *prev; struct mls_dlink *next; } dlink_fd; struct mls_dlink dlink_to; } *


decompiled_code:
void __fastcall server_dispatch(int *a1)
{
int v1; // ebp
char *v2; // rax
size_t v3; // rax
socklen_t v4; // er9
ssize_t v5; // rbx
int v6; // er13
socklen_t salen; // [rsp+Ch] [rbp-55Ch] BYREF
char serv[32]; // [rsp+10h] [rbp-558h] BYREF
struct sockaddr sa; // [rsp+30h] [rbp-538h] BYREF
char s[8]; // [rsp+B0h] [rbp-4B8h] BYREF
char host[1080]; // [rsp+130h] [rbp-438h] BYREF

v1 = *a1;
do
{
do
{
salen = 128;
v5 = recvfrom(v1, s, 0x80uLL, 0, &sa, &salen);
if ( v5 != -1 )
{
getnameinfo(&sa, salen, host, 0x401u, serv, 0x20u, 3);
fprintf(stdout, "recvfrom:%s:%s:len=%d\n", host, serv, (unsigned int)v5);
break;
}
v6 = *_errno_location();
perror("recvfrom");
}
while ( v6 == 11 );
s[v5] = 0;
v2 = strpbrk(s, "\r\n");
if ( v2 )
*v2 = 0;
fprintf(stdout, "client> '%s'\n", s);
v3 = strlen(s);
v4 = salen;
*(_QWORD *)&s[v3] = 0xA0D4B4F203E2DLL;
}
while ( sendto(v1, s, v3 + 7, 0, &sa, v4) != -1 );
perror("sendto");
}

canonical_decompiled_code:
void __fastcall func0(int *a1)
{
int v1; // ebp
char *v2; // rax
size_t v3; // rax
socklen_t v4; // er9
ssize_t v5; // rbx
int v6; // er13
socklen_t salen; // [rsp+Ch] [rbp-55Ch] BYREF
char serv[32]; // [rsp+10h] [rbp-558h] BYREF
struct sockaddr sa; // [rsp+30h] [rbp-538h] BYREF
char s[8]; // [rsp+B0h] [rbp-4B8h] BYREF
char host[1080]; // [rsp+130h] [rbp-438h] BYREF

v1 = *a1;
do
{
do
{
salen = 128;
v5 = recvfrom(v1, s, 0x80uLL, 0, &sa, &salen);
if ( v5 != -1 )
{
getnameinfo(&sa, salen, host, 0x401u, serv, 0x20u, 3);
fprintf(stdout, "recvfrom:%s:%s:len=%d\n", host, serv, (unsigned int)v5);
break;
}
v6 = *_errno_location();
perror("recvfrom");
}
while ( v6 == 11 );
s[v5] = 0;
v2 = strpbrk(s, "\r\n");
if ( v2 )
*v2 = 0;
fprintf(stdout, "client> '%s'\n", s);
v3 = strlen(s);
v4 = salen;
*(_QWORD *)&s[v3] = 0xA0D4B4F203E2DLL;
}
while ( sendto(v1, s, v3 + 7, 0, &sa, v4) != -1 );
perror("sendto");
}

original_code:
void
server_dispatch(struct mls_net_mcast_ctx* srv)
{
    struct sockaddr_storage from;
    socklen_t fromlen;
    int sock = srv->sock;
    ssize_t len;
    char buf[128];
    while (1) {
        fromlen = sizeof(from);
        len = recvfrom(sock,
            buf, sizeof(buf),
            0,
            (struct sockaddr*)&from, &fromlen);
        if (-1 == len) {
            int back = (*__errno_location ());
            perror("recvfrom");
            if (11 == back)
                continue;
        }
        else {
            char hbuf[1025], sbuf[32];
            getnameinfo((struct sockaddr *) &from, fromlen,
                hbuf, sizeof(hbuf),
                sbuf, sizeof(sbuf),
                1 | 2);
            fprintf(stdout, "recvfrom:%s:%s:len=%d\n", hbuf, sbuf, (int)len);
        }
        {
            char *ptr;
            buf[len] = '\0';
            if ((ptr = strpbrk(buf, "\r\n")) != ((void *)0)) {
                *ptr = '\0';
            }
        }
        fprintf(stdout, "client> \'%s\'\n", buf);
        strncat(buf, "-> OK\r\n", sizeof(buf));
        len = strlen(buf);
        len = sendto(sock,
            buf, len,
            0,
            (struct sockaddr*)&from, fromlen);
        if (-1 == len) {
            perror("sendto");
            break;
        }
    }
}

canonical_original_code:
void server_dispatch(struct mls_net_mcast_ctx *srv)
{
    struct sockaddr_storage from;
    unsigned int fromlen;
    int sock = srv->sock;
    long len;
    char buf[128];
    while (1) {
        fromlen = sizeof(from);
        len = recvfrom(sock,
            buf, sizeof(buf),
            0,
            (struct sockaddr *)&from, &fromlen);
        if (-1 == len) {
            int back = (*__errno_location ());
            perror("recvfrom");
            if (11 == back)
                continue;
        }
        else {
            char hbuf[1025]; char sbuf[32];
            getnameinfo((struct sockaddr *) &from, fromlen,
                hbuf, sizeof(hbuf),
                sbuf, sizeof(sbuf),
                1 | 2);
            fprintf(stdout, "recvfrom:%s:%s:len=%d\n", hbuf, sbuf, (int)len);
        }
        {
            char *ptr;
            buf[len] = '\0';
            if ((ptr = strpbrk(buf, "\r\n")) != ((void *)0)) {
                *ptr = '\0';
            }
        }
        fprintf(stdout, "client> \'%s\'\n", buf);
        strncat(buf, "-> OK\r\n", sizeof(buf));
        len = strlen(buf);
        len = sendto(sock,
            buf, len,
            0,
            (struct sockaddr *)&from, fromlen);
        if (-1 == len) {
            perror("sendto");
            break;
        }
    }
}

variable_types: struct mls_net_mcast_ctx { int sock; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } to; unsigned int tolen; struct sockaddr_storage from; unsigned int fromlen; char maddr[96]; char mport[16]; char ifaddr[96]; } *

variable_types:
fromlen
unsigned int

variable_types: sock int

variable_types: len long

variable_types:
buf
char [128]

variable_types: back int

variable_types:
hbuf
char [1025]

variable_types:
sbuf
char [32]

variable_types:
ptr
char *

user_defined_types: struct mls_net_mcast_ctx { int sock; struct sockaddr_storage to; unsigned int tolen; struct sockaddr_storage from; unsigned int fromlen; char maddr[96]; char mport[16]; char ifaddr[96]; }

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


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const unsigned __int16 **v3; // rax
const char *v4; // rdi
unsigned int v5; // eax
int *v6; // rax
int *v7; // rbp
char v9[280]; // [rsp+0h] [rbp-118h] BYREF

if ( argc <= 3 )
{
fprintf(stderr, "%s mc-address mc-port if-address\n", *argv);
fwrite(" mc-address: 224.0.23.0\n", 1uLL, 0x1AuLL, stderr);
fwrite(" mc-port: 3610\n", 1uLL, 0x14uLL, stderr);
fwrite(" if-address: 0.0.0.0 -or- eth0\n", 1uLL, 0x21uLL, stderr);
}
else
{
v3 = _ctype_b_loc();
v4 = argv[3];
if ( ((*v3)[*v4] & 0x800) != 0 )
{
strncpy(v9, argv[3], 0x100uLL);
}
else
{
v5 = mls_net_getaddr_by_ifname(v4, 2LL, v9, 256LL);
if ( v5 )
{
fprintf(stderr, "mls_net_getaddr_by_ifname(%d) error.\n", v5);
return 0;
}
}
fprintf(stdout, "ifaddr => %s\n", v9);
v6 = (int *)mls_net_mcast_open_ctx(argv[1], argv[2], v9);
v7 = v6;
if ( v6 )
{
server_dispatch(v6);
mls_net_mcast_close_ctx(v7);
}
else
{
fwrite("mls_net_mcast_srv_open() error.\n", 1uLL, 0x20uLL, stderr);
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const unsigned __int16 **v3; // rax
const char *v4; // rdi
unsigned int v5; // eax
int *v6; // rax
int *v7; // rbp
char v9[280]; // [rsp+0h] [rbp-118h] BYREF

if ( argc <= 3 )
{
fprintf(stderr, "%s mc-address mc-port if-address\n", *argv);
fwrite(" mc-address: 224.0.23.0\n", 1uLL, 0x1AuLL, stderr);
fwrite(" mc-port: 3610\n", 1uLL, 0x14uLL, stderr);
fwrite(" if-address: 0.0.0.0 -or- eth0\n", 1uLL, 0x21uLL, stderr);
}
else
{
v3 = _ctype_b_loc();
v4 = argv[3];
if ( ((*v3)[*v4] & 0x800) != 0 )
{
strncpy(v9, argv[3], 0x100uLL);
}
else
{
v5 = mls_net_getaddr_by_ifname(v4, 2LL, v9, 256LL);
if ( v5 )
{
fprintf(stderr, "mls_net_getaddr_by_ifname(%d) error.\n", v5);
return 0;
}
}
fprintf(stdout, "ifaddr => %s\n", v9);
v6 = (int *)mls_net_mcast_open_ctx(argv[1], argv[2], v9);
v7 = v6;
if ( v6 )
{
func0(v6);
mls_net_mcast_close_ctx(v7);
}
else
{
fwrite("mls_net_mcast_srv_open() error.\n", 1uLL, 0x20uLL, stderr);
}
}
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
    struct mls_net_mcast_ctx* srv = ((void *)0);
    char ifaddr[256];
    if (argc <= 3) {
        fprintf(stderr, "%s mc-address mc-port if-address\n", argv[0]);
        fprintf(stderr, "   mc-address: 224.0.23.0\n");
        fprintf(stderr, "   mc-port:    3610\n");
        fprintf(stderr, "   if-address: 0.0.0.0 -or- eth0\n");
        goto out;
    }
    {
        int ret = 0;
        if (((*__ctype_b_loc ())[(int) ((*(argv[3])))] & (unsigned short int) _ISdigit)) {
            strncpy(ifaddr, argv[3], sizeof(ifaddr));
        } else {
            ret = mls_net_getaddr_by_ifname(argv[3], 2,
                ifaddr, sizeof(ifaddr));
            if (0 != ret) {
                fprintf(stderr, "mls_net_getaddr_by_ifname(%d) error.\n", ret);
                goto out;
            }
        }
        fprintf(stdout, "ifaddr => %s\n", ifaddr);
    }
    srv = mls_net_mcast_open_ctx(argv[1], argv[2], ifaddr);
    if (((void *)0) == srv) {
        fprintf(stderr, "mls_net_mcast_srv_open() error.\n");
        goto out;
    }
    server_dispatch(srv);
out:
    if (((void *)0) != srv)
        mls_net_mcast_close_ctx(srv);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct mls_net_mcast_ctx *srv = ((void *)0);
    char ifaddr[256];
    if (argc <= 3) {
        fprintf(stderr, "%s mc-address mc-port if-address\n", argv[0]);
        fprintf(stderr, "   mc-address: 224.0.23.0\n");
        fprintf(stderr, "   mc-port:    3610\n");
        fprintf(stderr, "   if-address: 0.0.0.0 -or- eth0\n");
        goto out;
    }
    {
        int ret = 0;
        if (((*__ctype_b_loc ())[(int) ((*(argv[3])))] & (unsigned short) _ISdigit)) {
            strncpy(ifaddr, argv[3], sizeof(ifaddr));
        } else {
            ret = mls_net_getaddr_by_ifname(argv[3], 2,
                ifaddr, sizeof(ifaddr));
            if (0 != ret) {
                fprintf(stderr, "mls_net_getaddr_by_ifname(%d) error.\n", ret);
                goto out;
            }
        }
        fprintf(stdout, "ifaddr => %s\n", ifaddr);
    }
    srv = mls_net_mcast_open_ctx(argv[1], argv[2], ifaddr);
    if (((void *)0) == srv) {
        fprintf(stderr, "mls_net_mcast_srv_open() error.\n");
        goto out;
    }
    server_dispatch(srv);
out:
    if (((void *)0) != srv)
        mls_net_mcast_close_ctx(srv);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
ifaddr
char [256]

variable_types: ret int


decompiled_code:
__int64 mls_el_ini()
{
__int64 v0; // r9

mls_log_ini("stderr", 0x8000LL, 3LL, 3LL);
mls_log_set_module(0LL, "MLS", 3LL);
if ( mls_log_is_output_constprop_0(2u) )
mls_log_printf(0LL, 2LL, "mls_el_ini", 169LL, "mls_el_ini() started.\n", v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
__int64 v0; // r9

mls_log_ini("stderr", 0x8000LL, 3LL, 3LL);
mls_log_set_module(0LL, "MLS", 3LL);
if ( func0(2u) )
mls_log_printf(0LL, 2LL, "mls_el_ini", 169LL, "mls_el_ini() started.\n", v0);
return 0LL;
}

original_code:
int
mls_el_ini(void)
{
    int ret = 0;
    (void)mls_log_ini("stderr", (32*1024), MLS_LOG_WARN, 3);
    mls_log_set_module((0), "MLS", MLS_LOG_WARN);
    do{ if (mls_log_is_output((0),MLS_LOG_INFO)) mls_log_printf((0), MLS_LOG_INFO, __FUNCTION__, 169, "mls_el_ini() started.\n"); }while(0);
    return ret;
}

canonical_original_code:
int mls_el_ini()
{
    int ret = 0;
    (void)mls_log_ini("stderr", (32*1024), MLS_LOG_WARN, 3);
    mls_log_set_module((0), "MLS", MLS_LOG_WARN);
    do{ if (mls_log_is_output((0),MLS_LOG_INFO)) mls_log_printf((0), MLS_LOG_INFO, __FUNCTION__, 169, "mls_el_ini() started.\n"); }while(0);
    return ret;
}


decompiled_code:
__int64 mls_el_fin()
{
__int64 v0; // r9

if ( mls_log_is_output_constprop_0(2u) )
mls_log_printf(0LL, 2LL, "mls_el_fin", 177LL, "mls_el_fin() finished.\n", v0);
return mls_log_fin();
}

canonical_decompiled_code:
__int64 func10()
{
__int64 v0; // r9

if ( func0(2u) )
mls_log_printf(0LL, 2LL, "mls_el_fin", 177LL, "mls_el_fin() finished.\n", v0);
return mls_log_fin();
}

original_code:
void
mls_el_fin(void)
{
    do{ if (mls_log_is_output((0),MLS_LOG_INFO)) mls_log_printf((0), MLS_LOG_INFO, __FUNCTION__, 177, "mls_el_fin() finished.\n"); }while(0);
    mls_log_fin();
}

canonical_original_code:
void mls_el_fin()
{
    do{ if (mls_log_is_output((0),MLS_LOG_INFO)) mls_log_printf((0), MLS_LOG_INFO, __FUNCTION__, 177, "mls_el_fin() finished.\n"); }while(0);
    mls_log_fin();
}


canonical_decompiled_code:
__int64 __fastcall func8(__int64 *a1, _QWORD *a2)
{
__int64 result; // rax

result = *a1;
a2[1] = a1;
*a2 = result;
*(_QWORD *)(result + 8) = a2;
*a1 = (__int64)a2;
return result;
}

variable_types: struct mls_dlink { struct mls_dlink *prev; struct mls_dlink *next; } *

variable_types:
dl
struct mls_dlink *


decompiled_code:
void __fastcall init_container(__int64 a1, int a2)
{
_DWORD *v2; // rax
int v3; // edx

mls_dlink_init((_QWORD *)(a1 + 8));
*v2 = a2;
v2[6] = v3;
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1, int a2)
{
_DWORD *v2; // rax
int v3; // edx

func7((_QWORD *)(a1 + 8));
*v2 = a2;
v2[6] = v3;
}

original_code:
static void init_container(struct test_container *c, int d1, int d2)
{
    mls_dlink_init(&(c->dlink));
    c->data1 = d1;
    c->data2 = d2;
}

variable_types: struct test_container { int data1; struct mls_dlink { struct mls_dlink *prev; struct mls_dlink *next; } dlink; int data2; } *

user_defined_types: struct test_container { int data1; struct mls_dlink dlink; int data2; }


decompiled_code:
_BOOL8 __fastcall mls_log_is_output(int a1, unsigned int a2)
{
if ( *(_DWORD *)(Mls_log_confp + 12LL) <= a1 )
_assert_fail("0 <= mod && mod < Mls_log_confp->c_nmod", "../include/mls_log.h", 0x35u, "mls_log_is_output");
return *(_DWORD *)(*(_QWORD *)(Mls_log_confp + 16LL) + 40LL * a1 + 36) <= a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int a1, unsigned int a2)
{
if ( *(_DWORD *)(Mls_log_confp + 12LL) <= a1 )
_assert_fail("0 <= mod && mod < Mls_log_confp->c_nmod", "../include/mls_log.h", 0x35u, "mls_log_is_output");
return *(_DWORD *)(*(_QWORD *)(Mls_log_confp + 16LL) + 40LL * a1 + 36) <= a2;
}

original_code:
static inline int
mls_log_is_output(int mod, enum mls_log_level level)
{
    ((void) sizeof ((0 <= mod && mod < Mls_log_confp->c_nmod) ? 1 : 0), __extension__ ({ if (0 <= mod && mod < Mls_log_confp->c_nmod) ; else __assert_fail ("0 <= mod && mod < Mls_log_confp->c_nmod", "../include/mls_log.h", 53, __extension__ __PRETTY_FUNCTION__); }));
    return (Mls_log_confp->c_mods[mod].m_level <= level);
}

variable_types: mod int

variable_types: enum mls_log_level { MLS_LOG_ALL=0, MLS_LOG_DBG=1, MLS_LOG_INFO=2, MLS_LOG_WARN=3, MLS_LOG_ERR=4, MLS_LOG_FATAL=5, MLS_LOG_NONE=6 }


user_defined_types: struct mls_dlink { struct mls_dlink *prev; struct mls_dlink *next; }


decompiled_code:
void *__fastcall remove_iacs(void *src, int a2, int *a3, int *a4)
{
void *v4; // r8
char *v6; // rsi
_BYTE *v7; // rdx
char v8; // al
int v9; // eax
size_t v10; // rdx
int v11; // esi

v4 = src;
v6 = (char *)src + a2;
if ( src >= v6 )
{
v10 = 0LL;
v9 = 0;
v11 = 0;
}
else
{
v7 = src;
do
{
while ( 1 )
{
v8 = *(_BYTE *)src;
if ( *(_BYTE *)src == 0xFF )
break;
src = (char *)src + 1;
*v7++ = v8;
if ( src >= v6 )
goto LABEL_7;
}
if ( v6 <= (char *)src + 2 )
break;
src = (char *)src + 3;
}
while ( src < v6 );
LABEL_7:
v9 = (_DWORD)v7 - (_DWORD)v4;
v10 = v9;
v11 = (_DWORD)src - (_DWORD)v4;
src = (char *)src - v9;
}
*a3 = v11;
*a4 = v9;
return memmove(src, v4, v10);
}

canonical_decompiled_code:
void *__fastcall func1(void *src, int a2, int *a3, int *a4)
{
void *v4; // r8
char *v6; // rsi
_BYTE *v7; // rdx
char v8; // al
int v9; // eax
size_t v10; // rdx
int v11; // esi

v4 = src;
v6 = (char *)src + a2;
if ( src >= v6 )
{
v10 = 0LL;
v9 = 0;
v11 = 0;
}
else
{
v7 = src;
do
{
while ( 1 )
{
v8 = *(_BYTE *)src;
if ( *(_BYTE *)src == 0xFF )
break;
src = (char *)src + 1;
*v7++ = v8;
if ( src >= v6 )
goto LABEL_7;
}
if ( v6 <= (char *)src + 2 )
break;
src = (char *)src + 3;
}
while ( src < v6 );
LABEL_7:
v9 = (_DWORD)v7 - (_DWORD)v4;
v10 = v9;
v11 = (_DWORD)src - (_DWORD)v4;
src = (char *)src - v9;
}
*a3 = v11;
*a4 = v9;
return memmove(src, v4, v10);
}

original_code:
static char *
remove_iacs(unsigned char *bf, int len, int *processed, int *num_totty)
{
  unsigned char *ptr = bf;
  unsigned char *totty = bf;
  unsigned char *end = bf + len;
  while (ptr < end) {
    if (*ptr != 255) {
      *totty++ = *ptr++;
    } else {
      if ((ptr + 2) < end) {
        ptr += 3;
      } else {
        break;
      }
    }
  }
  *processed = ptr - bf;
  *num_totty = totty - bf;
  return memmove(ptr - *num_totty, bf, *num_totty);
}

canonical_original_code:
static char *remove_iacs(unsigned char *bf, int len, int *processed, int *num_totty)
{
  unsigned char *ptr = bf;
  unsigned char *totty = bf;
  unsigned char *end = bf + len;
  while (ptr < end) {
    if (*ptr != 255) {
      *totty++ = *ptr++;
    } else {
      if ((ptr + 2) < end) {
        ptr += 3;
      } else {
        break;
      }
    }
  }
  *processed = ptr - bf;
  *num_totty = totty - bf;
  return memmove(ptr - *num_totty, bf, *num_totty);
}

variable_types:
bf
unsigned char *

variable_types: len int

variable_types:
processed
int *

variable_types:
ptr
unsigned char *

variable_types:
totty
unsigned char *

variable_types:
end
unsigned char *

return_type: char *


decompiled_code:
void __fastcall free_session(void *ptr)
{
_QWORD *v1; // rax
_QWORD *v2; // rdx
int v3; // edx
int v4; // eax

v1 = (_QWORD *)sessions;
if ( (void *)sessions == ptr )
{
sessions = *(_QWORD *)ptr;
}
else
{
do
{
v2 = v1;
v1 = (_QWORD *)*v1;
}
while ( v1 != ptr );
*v2 = *(_QWORD *)ptr;
}
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
kill(*((_DWORD *)ptr + 4), 9);
wait4(*((_DWORD *)ptr + 4), 0LL, 0, 0LL);
close(*((_DWORD *)ptr + 3));
close(*((_DWORD *)ptr + 2));
v3 = *((_DWORD *)ptr + 3);
v4 = maxfd;
if ( v3 == maxfd || maxfd == *((_DWORD *)ptr + 2) )
{
--maxfd;
if ( v3 == v4 - 1 )
{
maxfd = v4 - 2;
}
else if ( v4 - 1 == *((_DWORD *)ptr + 2) )
{
maxfd = v4 - 2;
}
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
_QWORD *v1; // rax
_QWORD *v2; // rdx
int v3; // edx
int v4; // eax

v1 = (_QWORD *)sessions;
if ( (void *)sessions == ptr )
{
sessions = *(_QWORD *)ptr;
}
else
{
do
{
v2 = v1;
v1 = (_QWORD *)*v1;
}
while ( v1 != ptr );
*v2 = *(_QWORD *)ptr;
}
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
kill(*((_DWORD *)ptr + 4), 9);
wait4(*((_DWORD *)ptr + 4), 0LL, 0, 0LL);
close(*((_DWORD *)ptr + 3));
close(*((_DWORD *)ptr + 2));
v3 = *((_DWORD *)ptr + 3);
v4 = maxfd;
if ( v3 == maxfd || maxfd == *((_DWORD *)ptr + 2) )
{
--maxfd;
if ( v3 == v4 - 1 )
{
maxfd = v4 - 2;
}
else if ( v4 - 1 == *((_DWORD *)ptr + 2) )
{
maxfd = v4 - 2;
}
}
free(ptr);
}

original_code:
static void
free_session(struct tsession *ts)
{
  struct tsession *t = sessions;
  if (t == ts)
    sessions = ts->next;
  else {
    while (t->next != ts) {
      t = t->next;
    }
    t->next = ts->next;
  }
  free(ts->buf1);
  free(ts->buf2);
  kill(ts->shell_pid, 9);
  wait4(ts->shell_pid, ((void *)0), 0, ((void *)0));
  close(ts->ptyfd);
  close(ts->sockfd);
  if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
    maxfd--;
  if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
    maxfd--;
  free(ts);
}

variable_types: struct tsession { struct tsession *next; int sockfd; int shell_pid; char *buf1; int rdidx1; int rdidx2; } *


usage

decompiled_code:
void __noreturn usage()
{
puts("Usage: stelnetd [-p port] [-l loginprogram] [-d]");
putchar(10);
puts(" -p port");
puts(" -l loginprogram");
puts(" -d daemonize");
putchar(10);
exit(1);
}

canonical_decompiled_code:
void __noreturn func5()
{
puts("Usage: stelnetd [-p port] [-l loginprogram] [-d]");
putchar(10);
puts(" -p port");
puts(" -l loginprogram");
puts(" -d daemonize");
putchar(10);
exit(1);
}

original_code:
void
usage(void)
{
  printf("Usage: stelnetd [-p port] [-l loginprogram] [-d]\n");
  printf("\n");
  printf("    -p port\n");
  printf("    -l loginprogram\n");
  printf("    -d daemonize\n");
  printf("\n");
  exit(1);
}

canonical_original_code:
void usage()
{
  printf("Usage: stelnetd [-p port] [-l loginprogram] [-d]\n");
  printf("\n");
  printf("    -p port\n");
  printf("    -l loginprogram\n");
  printf("    -d daemonize\n");
  printf("\n");
  exit(1);
}


decompiled_code:
void __fastcall __noreturn perror_msg_and_die(const char *a1)
{
fputs(a1, stderr);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(const char *a1)
{
fputs(a1, stderr);
exit(1);
}

original_code:
void
perror_msg_and_die(char *text)
{
  fprintf(stderr, "%s", text);
  exit(1);
}

variable_types:
text
char *


decompiled_code:
void __fastcall __noreturn error_msg_and_die(const char *a1)
{
perror_msg_and_die(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
func6(a1);
}

original_code:
void
error_msg_and_die(char *text)
{
  perror_msg_and_die(text);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v4; // ebp
char *v5; // rbx
int v6; // eax
const char *v7; // r15
char *v8; // rax
const char *v9; // r13
char *v10; // rdx
int v11; // eax
int v12; // er15
__int64 v13; // r13
__int64 v14; // rax
int v15; // ecx
int v16; // edx
int v17; // edi
int v18; // ecx
int v19; // edx
int v20; // edi
int v21; // ecx
int v22; // edx
int v23; // ecx
int result; // eax
int v25; // ebp
char *v26; // rax
_QWORD *v27; // rdx
__int64 v28; // rax
bool v29; // cc
__int64 v30; // rax
__int64 v31; // rbp
int v32; // eax
int v33; // edx
int v34; // edx
int v35; // eax
int v36; // esi
int v37; // edi
int v38; // eax
bool v39; // zf
int v40; // eax
__int64 v41; // rsi
int v42; // eax
int v43; // eax
int v44; // er13
int v45; // esi
__int64 v46; // r14
int v47; // edi
int v48; // eax
int v49; // eax
int v50; // edx
int v51; // eax
int v52; // er15
int v53; // er13
__int64 v54; // rax
int v55; // eax
int v56; // edi
int v57; // edx
__int64 v58; // rdi
void *v59; // rax
size_t v60; // rdx
int v61; // edi
const void *v62; // rsi
int v63; // eax
int v64; // edx
int v65; // edx
__int64 v66; // rsi
int v67; // edx
int v68; // eax
int v69; // edx
int v70; // edx
int v71; // eax
__int64 v72; // r15
__int64 v73; // r13
__int64 v74; // rdx
int v75; // edx
int v76; // eax
__int64 v77; // [rsp+8h] [rbp-180h]
__int64 v78; // [rsp+10h] [rbp-178h]
char *ident; // [rsp+18h] [rbp-170h]
int v80; // [rsp+20h] [rbp-168h]
int fd; // [rsp+2Ch] [rbp-15Ch]
int optval; // [rsp+34h] [rbp-154h] BYREF
int v83; // [rsp+38h] [rbp-150h] BYREF
socklen_t addr_len; // [rsp+3Ch] [rbp-14Ch] BYREF
struct sockaddr addr; // [rsp+40h] [rbp-148h] BYREF
fd_set readfds; // [rsp+50h] [rbp-138h] BYREF
fd_set writefds; // [rsp+D0h] [rbp-B8h] BYREF

v3 = 23;
v4 = 0;
v5 = 0LL;
optval = 1;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "i:p:l:hd");
if ( v6 == -1 )
break;
if ( v6 == 108 )
{
loginpath = strdup(optarg);
}
else if ( v6 > 108 )
{
if ( v6 != 112 )
goto LABEL_127;
v3 = atoi(optarg);
}
else if ( v6 == 100 )
{
v4 = 1;
}
else
{
if ( v6 != 105 )
LABEL_127:
usage();
v5 = strdup(optarg);
}
}
v7 = *argv;
v8 = strrchr(*argv, 47);
v9 = loginpath;
v10 = v8 + 1;
if ( !v8 )
v10 = (char *)v7;
ident = v10;
if ( !loginpath )
{
loginpath = "/bin/sh";
v9 = "/bin/sh";
}
if ( access(v9, 1) < 0 )
{
fprintf(stderr, "\"%s\"", v9);
perror_msg_and_die(" is no valid executable!\n");
}
printf("%s: starting\n", ident);
if ( v5 )
{
printf(" port: %i; interface: %s; login program: %s\n", v3, v5, loginpath);
sessions = 0LL;
argv_init = loginpath;
v11 = socket(2, 1, 0);
v12 = v11;
if ( v11 >= 0 )
{
setsockopt(v11, 1, 2, &optval, 4u);
addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(v3);
goto LABEL_20;
}
LABEL_116:
perror("socket");
return 1;
}
printf(" port: %i; interface: %s; login program: %s\n", v3, "any", loginpath);
sessions = 0LL;
argv_init = loginpath;
v76 = socket(2, 1, 0);
v12 = v76;
if ( v76 < 0 )
goto LABEL_116;
v5 = "any";
setsockopt(v76, 1, 2, &optval, 4u);
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(v3);
*(_DWORD *)&addr.sa_data[2] = _bswap_32_constprop_0();
LABEL_20:
openlog(ident, 9, 24);
syslog(6, "%s (port: %i, ifname: %s, login: %s) startup succeeded\n", ident, v3, v5, loginpath);
closelog();
if ( bind(v12, &addr, 0x10u) < 0 )
{
perror("bind");
return 1;
}
if ( listen(v12, 1) < 0 )
{
perror("listen");
return 1;
}
if ( v4 && daemon(0, 1) < 0 )
perror_msg_and_die("daemon");
fd = v12;
maxfd = v12;
v13 = v12 >> 6;
v77 = 1LL << v12;
LABEL_24:
while ( 2 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
readfds.fds_bits[v13] |= v77;
v14 = sessions;
if ( sessions )
{
while ( 1 )
{
v20 = *(_DWORD *)(v14 + 48);
if ( v20 <= 0 )
goto LABEL_35;
v15 = *(_DWORD *)(v14 + 12);
v16 = v15 + 63;
if ( v15 >= 0 )
v16 = *(_DWORD *)(v14 + 12);
writefds.fds_bits[v16 >> 6] |= 1LL << (*(_DWORD *)(v14 + 12) % 64);
if ( v20 <= 3999 )
{
LABEL_35:
v21 = *(_DWORD *)(v14 + 8);
v22 = v21 + 63;
if ( v21 >= 0 )
v22 = *(_DWORD *)(v14 + 8);
readfds.fds_bits[v22 >> 6] |= 1LL << (*(_DWORD *)(v14 + 8) % 64);
v17 = *(_DWORD *)(v14 + 60);
if ( v17 <= 0 )
goto LABEL_38;
LABEL_30:
v18 = *(_DWORD *)(v14 + 8);
v19 = v18 + 63;
if ( v18 >= 0 )
v19 = *(_DWORD *)(v14 + 8);
writefds.fds_bits[v19 >> 6] |= 1LL << (*(_DWORD *)(v14 + 8) % 64);
if ( v17 <= 3999 )
goto LABEL_38;
v14 = *(_QWORD *)v14;
if ( !v14 )
break;
}
else
{
v17 = *(_DWORD *)(v14 + 60);
if ( v17 > 0 )
goto LABEL_30;
LABEL_38:
v23 = *(_DWORD *)(v14 + 12);
v14 = *(_QWORD *)v14;
readfds.fds_bits[v23 / 64] |= 1LL << (v23 % 64);
if ( !v14 )
break;
}
}
}
result = select(maxfd + 1, &readfds, &writefds, 0LL, 0LL);
if ( result )
{
if ( (readfds.fds_bits[v13] & v77) != 0 )
{
addr_len = 16;
v25 = accept(fd, &addr, &addr_len);
if ( v25 < 0 )
continue;
openlog(ident, 8, 24);
v26 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
syslog(6, "connection from: %s\n", v26);
closelog();
v27 = make_new_session(v25);
if ( v27 )
{
v28 = sessions;
v29 = maxfd < v25;
sessions = (__int64)v27;
*v27 = v28;
v30 = (__int64)v27;
if ( v29 )
maxfd = v25;
goto LABEL_45;
}
close(v25);
}
v30 = sessions;
LABEL_45:
v31 = v30;
if ( !v30 )
continue;
v78 = v13;
while ( 1 )
{
v45 = *(_DWORD *)(v31 + 48);
v46 = *(_QWORD *)v31;
if ( !v45 )
break;
v32 = *(_DWORD *)(v31 + 12);
v33 = v32 + 63;
if ( v32 >= 0 )
v33 = *(_DWORD *)(v31 + 12);
if ( (writefds.fds_bits[v33 >> 6] & (1LL << (*(_DWORD *)(v31 + 12) % 64))) != 0 )
{
v58 = *(int *)(v31 + 44);
if ( 4000 - (int)v58 <= v45 )
v45 = 4000 - v58;
v59 = remove_iacs((void *)(*(_QWORD *)(v31 + 24) + v58), v45, &v83, (int *)&addr_len);
v60 = (int)addr_len;
v61 = *(_DWORD *)(v31 + 12);
v62 = v59;
LODWORD(v59) = v83 - addr_len;
*(_DWORD *)(v31 + 44) += v83 - addr_len;
*(_DWORD *)(v31 + 48) -= (_DWORD)v59;
v63 = write(v61, v62, v60);
if ( v63 < 0 )
{
LABEL_118:
perror("write");
free_session((void *)v31);
LABEL_61:
if ( !v46 )
goto LABEL_83;
goto LABEL_62;
}
v64 = *(_DWORD *)(v31 + 44);
*(_DWORD *)(v31 + 48) -= v63;
v65 = v63 + v64;
if ( v65 == 4000 )
v65 = 0;
*(_DWORD *)(v31 + 44) = v65;
}
v34 = *(_DWORD *)(v31 + 60);
v35 = v34;
if ( v34 )
goto LABEL_84;
v36 = *(_DWORD *)(v31 + 48);
if ( v36 <= 3999 )
goto LABEL_66;
LABEL_52:
v37 = *(_DWORD *)(v31 + 12);
v38 = v37 + 63;
if ( v37 >= 0 )
v38 = *(_DWORD *)(v31 + 12);
v39 = (readfds.fds_bits[v38 >> 6] & (1LL << (*(_DWORD *)(v31 + 12) % 64))) == 0;
v40 = v34;
if ( !v39 )
{
v41 = *(int *)(v31 + 52);
if ( v34 <= (int)v41 )
v42 = 4000 - v41;
else
v42 = 4000 - v34;
v43 = read(v37, (void *)(*(_QWORD *)(v31 + 32) + v41), v42);
v44 = v43;
if ( !v43 )
goto LABEL_60;
if ( v43 >= 0 )
{
v80 = v43;
v71 = *(_DWORD *)(v31 + 52);
v72 = 0LL;
v73 = (unsigned int)(v44 - 1);
while ( 1 )
{
if ( *(_BYTE *)(v72 + *(_QWORD *)(v31 + 32) + v71) == 3 )
{
fwrite("found <CTRL>-<C> in data!\n", 1uLL, 0x1AuLL, stderr);
v71 = *(_DWORD *)(v31 + 52);
v74 = v72 + 1;
if ( v72 == v73 )
{
LABEL_110:
v44 = v80;
goto LABEL_111;
}
}
else
{
v74 = v72 + 1;
if ( v72 == v73 )
goto LABEL_110;
}
v72 = v74;
}
}
if ( *_errno_location() != 4 )
{
LABEL_60:
free_session((void *)v31);
goto LABEL_61;
}
v71 = *(_DWORD *)(v31 + 52);
LABEL_111:
v75 = v71 + v44;
v40 = v44 + *(_DWORD *)(v31 + 60);
if ( v75 == 4000 )
v75 = 0;
*(_DWORD *)(v31 + 60) = v40;
*(_DWORD *)(v31 + 52) = v75;
}
LABEL_79:
if ( !*(_DWORD *)(v31 + 48) )
*(_QWORD *)(v31 + 40) = 0LL;
if ( v40 )
goto LABEL_61;
*(_QWORD *)(v31 + 52) = 0LL;
if ( !v46 )
{
LABEL_83:
v13 = v78;
goto LABEL_24;
}
LABEL_62:
v31 = v46;
}
v35 = *(_DWORD *)(v31 + 60);
if ( v35 )
{
LABEL_84:
v56 = *(_DWORD *)(v31 + 8);
v57 = v56 + 63;
if ( v56 >= 0 )
v57 = *(_DWORD *)(v31 + 8);
if ( (writefds.fds_bits[v57 >> 6] & (1LL << (*(_DWORD *)(v31 + 8) % 64))) != 0 )
{
v66 = *(int *)(v31 + 56);
v67 = 4000 - v66;
if ( 4000 - (int)v66 > v35 )
v67 = v35;
v68 = write(v56, (const void *)(*(_QWORD *)(v31 + 32) + v66), v67);
if ( v68 < 0 )
goto LABEL_118;
v69 = *(_DWORD *)(v31 + 56);
*(_DWORD *)(v31 + 60) -= v68;
v70 = v68 + v69;
if ( v70 == 4000 )
v70 = 0;
*(_DWORD *)(v31 + 56) = v70;
}
v36 = *(_DWORD *)(v31 + 48);
if ( v36 > 3999 )
{
LABEL_78:
v34 = *(_DWORD *)(v31 + 60);
v40 = v34;
if ( v34 > 3999 )
goto LABEL_79;
goto LABEL_52;
}
}
else
{
v36 = 0;
}
LABEL_66:
v47 = *(_DWORD *)(v31 + 8);
v48 = v47 + 63;
if ( v47 >= 0 )
v48 = *(_DWORD *)(v31 + 8);
if ( (readfds.fds_bits[v48 >> 6] & (1LL << (*(_DWORD *)(v31 + 8) % 64))) != 0 )
{
v49 = *(_DWORD *)(v31 + 40);
if ( v36 <= v49 )
v50 = 4000 - v49;
else
v50 = 4000 - v36;
v51 = read(v47, (void *)(*(_QWORD *)(v31 + 24) + v49), v50);
v52 = v51;
v53 = v51;
if ( !v51 || v51 < 0 && *_errno_location() != 4 )
goto LABEL_60;
v54 = *(int *)(v31 + 40);
if ( !*(_BYTE *)(v52 + *(_QWORD *)(v31 + 24) + v54 - 1) )
{
v53 = v52 - 1;
if ( v52 == 1 )
{
v46 = v31;
goto LABEL_62;
}
}
v55 = v53 + v54;
*(_DWORD *)(v31 + 48) += v53;
if ( v55 == 4000 )
v55 = 0;
*(_DWORD *)(v31 + 40) = v55;
}
goto LABEL_78;
}
return result;
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
int v4; // ebp
char *v5; // rbx
int v6; // eax
const char *v7; // r15
char *v8; // rax
const char *v9; // r13
char *v10; // rdx
int v11; // eax
int v12; // er15
__int64 v13; // r13
__int64 v14; // rax
int v15; // ecx
int v16; // edx
int v17; // edi
int v18; // ecx
int v19; // edx
int v20; // edi
int v21; // ecx
int v22; // edx
int v23; // ecx
int result; // eax
int v25; // ebp
char *v26; // rax
_QWORD *v27; // rdx
__int64 v28; // rax
bool v29; // cc
__int64 v30; // rax
__int64 v31; // rbp
int v32; // eax
int v33; // edx
int v34; // edx
int v35; // eax
int v36; // esi
int v37; // edi
int v38; // eax
bool v39; // zf
int v40; // eax
__int64 v41; // rsi
int v42; // eax
int v43; // eax
int v44; // er13
int v45; // esi
__int64 v46; // r14
int v47; // edi
int v48; // eax
int v49; // eax
int v50; // edx
int v51; // eax
int v52; // er15
int v53; // er13
__int64 v54; // rax
int v55; // eax
int v56; // edi
int v57; // edx
__int64 v58; // rdi
void *v59; // rax
size_t v60; // rdx
int v61; // edi
const void *v62; // rsi
int v63; // eax
int v64; // edx
int v65; // edx
__int64 v66; // rsi
int v67; // edx
int v68; // eax
int v69; // edx
int v70; // edx
int v71; // eax
__int64 v72; // r15
__int64 v73; // r13
__int64 v74; // rdx
int v75; // edx
int v76; // eax
__int64 v77; // [rsp+8h] [rbp-180h]
__int64 v78; // [rsp+10h] [rbp-178h]
char *ident; // [rsp+18h] [rbp-170h]
int v80; // [rsp+20h] [rbp-168h]
int fd; // [rsp+2Ch] [rbp-15Ch]
int optval; // [rsp+34h] [rbp-154h] BYREF
int v83; // [rsp+38h] [rbp-150h] BYREF
socklen_t addr_len; // [rsp+3Ch] [rbp-14Ch] BYREF
struct sockaddr addr; // [rsp+40h] [rbp-148h] BYREF
fd_set readfds; // [rsp+50h] [rbp-138h] BYREF
fd_set writefds; // [rsp+D0h] [rbp-B8h] BYREF

v3 = 23;
v4 = 0;
v5 = 0LL;
optval = 1;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "i:p:l:hd");
if ( v6 == -1 )
break;
if ( v6 == 108 )
{
loginpath = strdup(optarg);
}
else if ( v6 > 108 )
{
if ( v6 != 112 )
goto LABEL_127;
v3 = atoi(optarg);
}
else if ( v6 == 100 )
{
v4 = 1;
}
else
{
if ( v6 != 105 )
LABEL_127:
func5();
v5 = strdup(optarg);
}
}
v7 = *argv;
v8 = strrchr(*argv, 47);
v9 = loginpath;
v10 = v8 + 1;
if ( !v8 )
v10 = (char *)v7;
ident = v10;
if ( !loginpath )
{
loginpath = "/bin/sh";
v9 = "/bin/sh";
}
if ( access(v9, 1) < 0 )
{
fprintf(stderr, "\"%s\"", v9);
func6(" is no valid executable!\n");
}
printf("%s: starting\n", ident);
if ( v5 )
{
printf(" port: %i; interface: %s; login program: %s\n", v3, v5, loginpath);
sessions = 0LL;
argv_init = loginpath;
v11 = socket(2, 1, 0);
v12 = v11;
if ( v11 >= 0 )
{
setsockopt(v11, 1, 2, &optval, 4u);
addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(v3);
goto LABEL_20;
}
LABEL_116:
perror("socket");
return 1;
}
printf(" port: %i; interface: %s; login program: %s\n", v3, "any", loginpath);
sessions = 0LL;
argv_init = loginpath;
v76 = socket(2, 1, 0);
v12 = v76;
if ( v76 < 0 )
goto LABEL_116;
v5 = "any";
setsockopt(v76, 1, 2, &optval, 4u);
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(v3);
*(_DWORD *)&addr.sa_data[2] = func4();
LABEL_20:
openlog(ident, 9, 24);
syslog(6, "%s (port: %i, ifname: %s, login: %s) startup succeeded\n", ident, v3, v5, loginpath);
closelog();
if ( bind(v12, &addr, 0x10u) < 0 )
{
perror("bind");
return 1;
}
if ( listen(v12, 1) < 0 )
{
perror("listen");
return 1;
}
if ( v4 && daemon(0, 1) < 0 )
func6("daemon");
fd = v12;
maxfd = v12;
v13 = v12 >> 6;
v77 = 1LL << v12;
LABEL_24:
while ( 2 )
{
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
readfds.fds_bits[v13] |= v77;
v14 = sessions;
if ( sessions )
{
while ( 1 )
{
v20 = *(_DWORD *)(v14 + 48);
if ( v20 <= 0 )
goto LABEL_35;
v15 = *(_DWORD *)(v14 + 12);
v16 = v15 + 63;
if ( v15 >= 0 )
v16 = *(_DWORD *)(v14 + 12);
writefds.fds_bits[v16 >> 6] |= 1LL << (*(_DWORD *)(v14 + 12) % 64);
if ( v20 <= 3999 )
{
LABEL_35:
v21 = *(_DWORD *)(v14 + 8);
v22 = v21 + 63;
if ( v21 >= 0 )
v22 = *(_DWORD *)(v14 + 8);
readfds.fds_bits[v22 >> 6] |= 1LL << (*(_DWORD *)(v14 + 8) % 64);
v17 = *(_DWORD *)(v14 + 60);
if ( v17 <= 0 )
goto LABEL_38;
LABEL_30:
v18 = *(_DWORD *)(v14 + 8);
v19 = v18 + 63;
if ( v18 >= 0 )
v19 = *(_DWORD *)(v14 + 8);
writefds.fds_bits[v19 >> 6] |= 1LL << (*(_DWORD *)(v14 + 8) % 64);
if ( v17 <= 3999 )
goto LABEL_38;
v14 = *(_QWORD *)v14;
if ( !v14 )
break;
}
else
{
v17 = *(_DWORD *)(v14 + 60);
if ( v17 > 0 )
goto LABEL_30;
LABEL_38:
v23 = *(_DWORD *)(v14 + 12);
v14 = *(_QWORD *)v14;
readfds.fds_bits[v23 / 64] |= 1LL << (v23 % 64);
if ( !v14 )
break;
}
}
}
result = select(maxfd + 1, &readfds, &writefds, 0LL, 0LL);
if ( result )
{
if ( (readfds.fds_bits[v13] & v77) != 0 )
{
addr_len = 16;
v25 = accept(fd, &addr, &addr_len);
if ( v25 < 0 )
continue;
openlog(ident, 8, 24);
v26 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
syslog(6, "connection from: %s\n", v26);
closelog();
v27 = make_new_session(v25);
if ( v27 )
{
v28 = sessions;
v29 = maxfd < v25;
sessions = (__int64)v27;
*v27 = v28;
v30 = (__int64)v27;
if ( v29 )
maxfd = v25;
goto LABEL_45;
}
close(v25);
}
v30 = sessions;
LABEL_45:
v31 = v30;
if ( !v30 )
continue;
v78 = v13;
while ( 1 )
{
v45 = *(_DWORD *)(v31 + 48);
v46 = *(_QWORD *)v31;
if ( !v45 )
break;
v32 = *(_DWORD *)(v31 + 12);
v33 = v32 + 63;
if ( v32 >= 0 )
v33 = *(_DWORD *)(v31 + 12);
if ( (writefds.fds_bits[v33 >> 6] & (1LL << (*(_DWORD *)(v31 + 12) % 64))) != 0 )
{
v58 = *(int *)(v31 + 44);
if ( 4000 - (int)v58 <= v45 )
v45 = 4000 - v58;
v59 = func1((void *)(*(_QWORD *)(v31 + 24) + v58), v45, &v83, (int *)&addr_len);
v60 = (int)addr_len;
v61 = *(_DWORD *)(v31 + 12);
v62 = v59;
LODWORD(v59) = v83 - addr_len;
*(_DWORD *)(v31 + 44) += v83 - addr_len;
*(_DWORD *)(v31 + 48) -= (_DWORD)v59;
v63 = write(v61, v62, v60);
if ( v63 < 0 )
{
LABEL_118:
perror("write");
func2((void *)v31);
LABEL_61:
if ( !v46 )
goto LABEL_83;
goto LABEL_62;
}
v64 = *(_DWORD *)(v31 + 44);
*(_DWORD *)(v31 + 48) -= v63;
v65 = v63 + v64;
if ( v65 == 4000 )
v65 = 0;
*(_DWORD *)(v31 + 44) = v65;
}
v34 = *(_DWORD *)(v31 + 60);
v35 = v34;
if ( v34 )
goto LABEL_84;
v36 = *(_DWORD *)(v31 + 48);
if ( v36 <= 3999 )
goto LABEL_66;
LABEL_52:
v37 = *(_DWORD *)(v31 + 12);
v38 = v37 + 63;
if ( v37 >= 0 )
v38 = *(_DWORD *)(v31 + 12);
v39 = (readfds.fds_bits[v38 >> 6] & (1LL << (*(_DWORD *)(v31 + 12) % 64))) == 0;
v40 = v34;
if ( !v39 )
{
v41 = *(int *)(v31 + 52);
if ( v34 <= (int)v41 )
v42 = 4000 - v41;
else
v42 = 4000 - v34;
v43 = read(v37, (void *)(*(_QWORD *)(v31 + 32) + v41), v42);
v44 = v43;
if ( !v43 )
goto LABEL_60;
if ( v43 >= 0 )
{
v80 = v43;
v71 = *(_DWORD *)(v31 + 52);
v72 = 0LL;
v73 = (unsigned int)(v44 - 1);
while ( 1 )
{
if ( *(_BYTE *)(v72 + *(_QWORD *)(v31 + 32) + v71) == 3 )
{
fwrite("found <CTRL>-<C> in data!\n", 1uLL, 0x1AuLL, stderr);
v71 = *(_DWORD *)(v31 + 52);
v74 = v72 + 1;
if ( v72 == v73 )
{
LABEL_110:
v44 = v80;
goto LABEL_111;
}
}
else
{
v74 = v72 + 1;
if ( v72 == v73 )
goto LABEL_110;
}
v72 = v74;
}
}
if ( *_errno_location() != 4 )
{
LABEL_60:
func2((void *)v31);
goto LABEL_61;
}
v71 = *(_DWORD *)(v31 + 52);
LABEL_111:
v75 = v71 + v44;
v40 = v44 + *(_DWORD *)(v31 + 60);
if ( v75 == 4000 )
v75 = 0;
*(_DWORD *)(v31 + 60) = v40;
*(_DWORD *)(v31 + 52) = v75;
}
LABEL_79:
if ( !*(_DWORD *)(v31 + 48) )
*(_QWORD *)(v31 + 40) = 0LL;
if ( v40 )
goto LABEL_61;
*(_QWORD *)(v31 + 52) = 0LL;
if ( !v46 )
{
LABEL_83:
v13 = v78;
goto LABEL_24;
}
LABEL_62:
v31 = v46;
}
v35 = *(_DWORD *)(v31 + 60);
if ( v35 )
{
LABEL_84:
v56 = *(_DWORD *)(v31 + 8);
v57 = v56 + 63;
if ( v56 >= 0 )
v57 = *(_DWORD *)(v31 + 8);
if ( (writefds.fds_bits[v57 >> 6] & (1LL << (*(_DWORD *)(v31 + 8) % 64))) != 0 )
{
v66 = *(int *)(v31 + 56);
v67 = 4000 - v66;
if ( 4000 - (int)v66 > v35 )
v67 = v35;
v68 = write(v56, (const void *)(*(_QWORD *)(v31 + 32) + v66), v67);
if ( v68 < 0 )
goto LABEL_118;
v69 = *(_DWORD *)(v31 + 56);
*(_DWORD *)(v31 + 60) -= v68;
v70 = v68 + v69;
if ( v70 == 4000 )
v70 = 0;
*(_DWORD *)(v31 + 56) = v70;
}
v36 = *(_DWORD *)(v31 + 48);
if ( v36 > 3999 )
{
LABEL_78:
v34 = *(_DWORD *)(v31 + 60);
v40 = v34;
if ( v34 > 3999 )
goto LABEL_79;
goto LABEL_52;
}
}
else
{
v36 = 0;
}
LABEL_66:
v47 = *(_DWORD *)(v31 + 8);
v48 = v47 + 63;
if ( v47 >= 0 )
v48 = *(_DWORD *)(v31 + 8);
if ( (readfds.fds_bits[v48 >> 6] & (1LL << (*(_DWORD *)(v31 + 8) % 64))) != 0 )
{
v49 = *(_DWORD *)(v31 + 40);
if ( v36 <= v49 )
v50 = 4000 - v49;
else
v50 = 4000 - v36;
v51 = read(v47, (void *)(*(_QWORD *)(v31 + 24) + v49), v50);
v52 = v51;
v53 = v51;
if ( !v51 || v51 < 0 && *_errno_location() != 4 )
goto LABEL_60;
v54 = *(int *)(v31 + 40);
if ( !*(_BYTE *)(v52 + *(_QWORD *)(v31 + 24) + v54 - 1) )
{
v53 = v52 - 1;
if ( v52 == 1 )
{
v46 = v31;
goto LABEL_62;
}
}
v55 = v53 + v54;
*(_DWORD *)(v31 + 48) += v53;
if ( v55 == 4000 )
v55 = 0;
*(_DWORD *)(v31 + 40) = v55;
}
goto LABEL_78;
}
return result;
}
}

original_code:
int
main(int argc, char *argv[])
{
  struct sockaddr_in sa;
  int masterfd;
  fd_set rdfdset, wrfdset;
  int selret;
  int on = 1;
  int portnbr = 23;
  int c, ii;
  int daemonize = 0;
  char *interface_name = ((void *)0);
  char *appname;
  while ((c = getopt(argc, argv, "i:p:l:hd")) != -1) {
    switch (c) {
    case 'p':
      portnbr = atoi(optarg);
      break;
    case 'i':
      interface_name = strdup(optarg);
      break;
    case 'l':
      loginpath = strdup(optarg);
      break;
    case 'd':
      daemonize = 1;
      break;
    case 'h':
    default:
      usage();
      exit(1);
    }
  }
  appname = strrchr(argv[0], '/');
  if (!appname)
    appname = argv[0];
  else
    appname++;
  if (!loginpath) {
    loginpath = "/bin/sh";
  }
  if (access(loginpath, 1) < 0) {
    fprintf(stderr, "\"%s\"", loginpath);
    perror_msg_and_die(" is no valid executable!\n");
  }
  printf("%s: starting\n", appname);
  printf("  port: %i; interface: %s; login program: %s\n",
      portnbr, (interface_name) ? interface_name : "any", loginpath);
  argv_init[0] = loginpath;
  sessions = 0;
  masterfd = socket(2, SOCK_STREAM, 0);
  if (masterfd < 0) {
    perror("socket");
    return 1;
  }
  (void)setsockopt(masterfd, 1, 2, &on, sizeof(on));
  memset((void *)&sa, 0, sizeof(sa));
  sa.sin_family = 2;
  sa.sin_port = htons(portnbr);
  if (interface_name) {
  } else {
    sa.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  }
  openlog(appname, 0x08 | 0x01, (3<<3));
  syslog(6, "%s (port: %i, ifname: %s, login: %s) startup succeeded\n",
      appname, portnbr, (interface_name) ? interface_name : "any", loginpath);
  closelog();
  if (bind(masterfd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    perror("bind");
    return 1;
  }
  if (listen(masterfd, 1) < 0) {
    perror("listen");
    return 1;
  }
  if (daemonize) {
    if (daemon(0, 1) < 0)
      perror_msg_and_die("daemon");
  }
  maxfd = masterfd;
  while (1) {
    struct tsession *ts;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfdset)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wrfdset)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&rdfdset)->__fds_bits)[((masterfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((masterfd) % (8 * (int) sizeof (__fd_mask)))))));
    ts = sessions;
    while (ts) {
      if (ts->size1 > 0) {
        ((void) (((&wrfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (ts->size1 < 4000) {
        ((void) (((&rdfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (ts->size2 > 0) {
        ((void) (((&wrfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (ts->size2 < 4000) {
        ((void) (((&rdfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      ts = ts->next;
    }
    selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
    if (!selret)
      break;
    if (((((&rdfdset)->__fds_bits)[((masterfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((masterfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int fd, salen;
      salen = sizeof(sa);
      if ((fd = accept(masterfd, (struct sockaddr *)&sa, &salen)) < 0) {
        continue;
      }
      struct tsession *new_ts;
      openlog(appname, 0x08, (3<<3));
      syslog(6, "connection from: %s\n", inet_ntoa(sa.sin_addr));
      closelog();
      new_ts = make_new_session(fd);
      if (new_ts) {
        new_ts->next = sessions;
        sessions = new_ts;
        if (fd > maxfd)
          maxfd = fd;
      } else {
        close(fd);
      }
    }
    ts = sessions;
    while (ts) {
      int maxlen, w, r;
      struct tsession *next = ts->next;
      if (ts->size1 && ((((&wrfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        int processed, num_totty;
        char *ptr;
        maxlen = ((4000 - ts->wridx1) > (ts->size1) ? (ts->size1) : (4000 - ts->wridx1));
        ptr = remove_iacs(ts->buf1 + ts->wridx1, maxlen, &processed, &num_totty);
        ts->wridx1 += processed - num_totty;
        ts->size1 -= processed - num_totty;
        w = write(ts->ptyfd, ptr, num_totty);
        if (w < 0) {
          perror("write");
          free_session(ts);
          ts = next;
          continue;
        }
        ts->wridx1 += w;
        ts->size1 -= w;
        if (ts->wridx1 == 4000)
          ts->wridx1 = 0;
      }
      if (ts->size2 && ((((&wrfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        maxlen = ((4000 - ts->wridx2) > (ts->size2) ? (ts->size2) : (4000 - ts->wridx2));
        w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
        if (w < 0) {
          perror("write");
          free_session(ts);
          ts = next;
          continue;
        }
        ts->wridx2 += w;
        ts->size2 -= w;
        if (ts->wridx2 == 4000)
          ts->wridx2 = 0;
      }
      if (ts->size1 < 4000 && ((((&rdfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        maxlen = ((4000 - ts->rdidx1) > (4000 - ts->size1) ? (4000 - ts->size1) : (4000 - ts->rdidx1));
        r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
        if (!r || (r < 0 && (*__errno_location ()) != 4)) {
          free_session(ts);
          ts = next;
          continue;
        }
        if (!*(ts->buf1 + ts->rdidx1 + r - 1)) {
          r--;
          if (!r)
            continue;
        }
        ts->rdidx1 += r;
        ts->size1 += r;
        if (ts->rdidx1 == 4000)
          ts->rdidx1 = 0;
      }
      if (ts->size2 < 4000 && ((((&rdfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        maxlen = ((4000 - ts->rdidx2) > (4000 - ts->size2) ? (4000 - ts->size2) : (4000 - ts->rdidx2));
        r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
        if (!r || (r < 0 && (*__errno_location ()) != 4)) {
          free_session(ts);
          ts = next;
          continue;
        }
        for (ii = 0; ii < r; ii++) {
          if (*(ts->buf2 + ts->rdidx2 + ii) == 3) {
            fprintf(stderr, "found <CTRL>-<C> in data!\n");
          }
        }
        ts->rdidx2 += r;
        ts->size2 += r;
        if (ts->rdidx2 == 4000)
          ts->rdidx2 = 0;
      }
      if (ts->size1 == 0) {
        ts->rdidx1 = 0;
        ts->wridx1 = 0;
      }
      if (ts->size2 == 0) {
        ts->rdidx2 = 0;
        ts->wridx2 = 0;
      }
      ts = next;
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  struct sockaddr_in sa;
  int masterfd;
  struct fd_set rdfdset; struct fd_set wrfdset;
  int selret;
  int on = 1;
  int portnbr = 23;
  int c; int ii;
  int daemonize = 0;
  char *interface_name = ((void *)0);
  char *appname;
  while ((c = getopt(argc, argv, "i:p:l:hd")) != -1) {
    switch (c) {
    case 'p':
      portnbr = atoi(optarg);
      break;
    case 'i':
      interface_name = strdup(optarg);
      break;
    case 'l':
      loginpath = strdup(optarg);
      break;
    case 'd':
      daemonize = 1;
      break;
    case 'h':
    default:
      usage();
      exit(1);
    }
  }
  appname = strrchr(argv[0], '/');
  if (!appname)
    appname = argv[0];
  else
    appname++;
  if (!loginpath) {
    loginpath = "/bin/sh";
  }
  if (access(loginpath, 1) < 0) {
    fprintf(stderr, "\"%s\"", loginpath);
    perror_msg_and_die(" is no valid executable!\n");
  }
  printf("%s: starting\n", appname);
  printf("  port: %i; interface: %s; login program: %s\n",
      portnbr, (interface_name) ? interface_name : "any", loginpath);
  argv_init[0] = loginpath;
  sessions = 0;
  masterfd = socket(2, SOCK_STREAM, 0);
  if (masterfd < 0) {
    perror("socket");
    return 1;
  }
  (void)setsockopt(masterfd, 1, 2, &on, sizeof(on));
  memset((void *)&sa, 0, sizeof(sa));
  sa.sin_family = 2;
  sa.sin_port = htons(portnbr);
  if (interface_name) {
  } else {
    sa.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  }
  openlog(appname, 0x08 | 0x01, (3<<3));
  syslog(6, "%s (port: %i, ifname: %s, login: %s) startup succeeded\n",
      appname, portnbr, (interface_name) ? interface_name : "any", loginpath);
  closelog();
  if (bind(masterfd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    perror("bind");
    return 1;
  }
  if (listen(masterfd, 1) < 0) {
    perror("listen");
    return 1;
  }
  if (daemonize) {
    if (daemon(0, 1) < 0)
      perror_msg_and_die("daemon");
  }
  maxfd = masterfd;
  while (1) {
    struct tsession *ts;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfdset)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wrfdset)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&rdfdset)->__fds_bits)[((masterfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((masterfd) % (8 * (int) sizeof (__fd_mask)))))));
    ts = sessions;
    while (ts) {
      if (ts->size1 > 0) {
        ((void) (((&wrfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (ts->size1 < 4000) {
        ((void) (((&rdfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (ts->size2 > 0) {
        ((void) (((&wrfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      if (ts->size2 < 4000) {
        ((void) (((&rdfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))));
      }
      ts = ts->next;
    }
    selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
    if (!selret)
      break;
    if (((((&rdfdset)->__fds_bits)[((masterfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((masterfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int fd; int salen;
      salen = sizeof(sa);
      if ((fd = accept(masterfd, (struct sockaddr *)&sa, &salen)) < 0) {
        continue;
      }
      struct tsession *new_ts;
      openlog(appname, 0x08, (3<<3));
      syslog(6, "connection from: %s\n", inet_ntoa(sa.sin_addr));
      closelog();
      new_ts = make_new_session(fd);
      if (new_ts) {
        new_ts->next = sessions;
        sessions = new_ts;
        if (fd > maxfd)
          maxfd = fd;
      } else {
        close(fd);
      }
    }
    ts = sessions;
    while (ts) {
      int maxlen; int w; int r;
      struct tsession *next = ts->next;
      if (ts->size1 && ((((&wrfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        int processed; int num_totty;
        char *ptr;
        maxlen = ((4000 - ts->wridx1) > (ts->size1) ? (ts->size1) : (4000 - ts->wridx1));
        ptr = remove_iacs(ts->buf1 + ts->wridx1, maxlen, &processed, &num_totty);
        ts->wridx1 += processed - num_totty;
        ts->size1 -= processed - num_totty;
        w = write(ts->ptyfd, ptr, num_totty);
        if (w < 0) {
          perror("write");
          free_session(ts);
          ts = next;
          continue;
        }
        ts->wridx1 += w;
        ts->size1 -= w;
        if (ts->wridx1 == 4000)
          ts->wridx1 = 0;
      }
      if (ts->size2 && ((((&wrfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        maxlen = ((4000 - ts->wridx2) > (ts->size2) ? (ts->size2) : (4000 - ts->wridx2));
        w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
        if (w < 0) {
          perror("write");
          free_session(ts);
          ts = next;
          continue;
        }
        ts->wridx2 += w;
        ts->size2 -= w;
        if (ts->wridx2 == 4000)
          ts->wridx2 = 0;
      }
      if (ts->size1 < 4000 && ((((&rdfdset)->__fds_bits)[((ts->sockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->sockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        maxlen = ((4000 - ts->rdidx1) > (4000 - ts->size1) ? (4000 - ts->size1) : (4000 - ts->rdidx1));
        r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
        if (!r || (r < 0 && (*__errno_location ()) != 4)) {
          free_session(ts);
          ts = next;
          continue;
        }
        if (!*(ts->buf1 + ts->rdidx1 + r - 1)) {
          r--;
          if (!r)
            continue;
        }
        ts->rdidx1 += r;
        ts->size1 += r;
        if (ts->rdidx1 == 4000)
          ts->rdidx1 = 0;
      }
      if (ts->size2 < 4000 && ((((&rdfdset)->__fds_bits)[((ts->ptyfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->ptyfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        maxlen = ((4000 - ts->rdidx2) > (4000 - ts->size2) ? (4000 - ts->size2) : (4000 - ts->rdidx2));
        r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
        if (!r || (r < 0 && (*__errno_location ()) != 4)) {
          free_session(ts);
          ts = next;
          continue;
        }
        for (ii = 0; ii < r; ii++) {
          if (*(ts->buf2 + ts->rdidx2 + ii) == 3) {
            fprintf(stderr, "found <CTRL>-<C> in data!\n");
          }
        }
        ts->rdidx2 += r;
        ts->size2 += r;
        if (ts->rdidx2 == 4000)
          ts->rdidx2 = 0;
      }
      if (ts->size1 == 0) {
        ts->rdidx1 = 0;
        ts->wridx1 = 0;
      }
      if (ts->size2 == 0) {
        ts->rdidx2 = 0;
        ts->wridx2 = 0;
      }
      ts = next;
    }
  }
  return 0;
}

variable_types: argc int

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

variable_types: masterfd int

variable_types:
rdfdset
struct fd_set { long __fds_bits[-1]; }

variable_types:
wrfdset
struct fd_set { long __fds_bits[-1]; }

variable_types: selret int

variable_types: on int

variable_types: portnbr int

variable_types: c int

variable_types: ii int

variable_types: daemonize int

variable_types:
appname
char *

variable_types: fd int

variable_types: salen int

variable_types: maxlen int

variable_types: w int

variable_types: r int

variable_types: processed int

variable_types:
ptr
char *

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

user_defined_types: struct tsession { struct tsession *next; int sockfd; int shell_pid; char *buf1; int rdidx1; int rdidx2; }


original_code:
int main(int argc, char *const *argv)
{
    int k = 0;
    char buf[9];
    sscanf("ciao mondocrudele", "%*s %s", buf);
    printf("%x\n", k);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int k = 0;
    char buf[9];
    sscanf("ciao mondocrudele", "%*s %s", buf);
    printf("%x\n", k);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: k int

variable_types:
buf
char [9]


repo: kalenedrael/kgtd

decompiled_code:
__int64 text_init()
{
unsigned int v0; // ebp
char *v1; // rbx
__int64 v2; // rdi
char *v3; // rdx
__int64 result; // rax

v0 = 31;
v1 = (char *)&font_data;
glPixelStorei(3317LL, 1LL);
glPixelStorei(3333LL, 1LL);
do
{
v2 = v0++;
glNewList(v2, 4864LL);
v3 = v1;
v1 += 13;
glBitmap(6LL, 13LL, v3, 0.0, 0.0, 6.0, 0.0);
result = glEndList();
}
while ( &unk_740 != (_UNKNOWN *)v1 );
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // ebp
char *v1; // rbx
__int64 v2; // rdi
char *v3; // rdx
__int64 result; // rax

v0 = 31;
v1 = (char *)&font_data;
glPixelStorei(3317LL, 1LL);
glPixelStorei(3333LL, 1LL);
do
{
v2 = v0++;
glNewList(v2, 4864LL);
v3 = v1;
v1 += 13;
glBitmap(6LL, 13LL, v3, 0.0, 0.0, 6.0, 0.0);
result = glEndList();
}
while ( &unk_740 != (_UNKNOWN *)v1 );
return result;
}

original_code:
void text_init()
{
 int i;
 glPixelStorei(0x0CF5, 1);
 glPixelStorei(0x0D05, 1);
 for(i = 0; i < 128; i++) {
  glNewList(DISPLAY_LIST_FONT_BASE + i, 0x1300);
  glBitmap(6, 13, 0, 0, 6, 0,
           font_data[i]);
  glEndList();
 }
}

variable_types: i int


decompiled_code:
__int64 __fastcall text_draw(char *a1, unsigned int a2, unsigned int a3)
{
char *v3; // rbx
__int64 result; // rax
int i; // edi

v3 = a1;
result = glRasterPos2i(a2, a3);
for ( i = *a1; *v3 > 0; i = *v3 )
{
++v3;
result = glCallList((unsigned int)(i + 31));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, unsigned int a2, unsigned int a3)
{
char *v3; // rbx
__int64 result; // rax
int i; // edi

v3 = a1;
result = glRasterPos2i(a2, a3);
for ( i = *a1; *v3 > 0; i = *v3 )
{
++v3;
result = glCallList((unsigned int)(i + 31));
}
return result;
}

original_code:
void text_draw(char *str, int x, int y)
{
 glRasterPos2i(x, y);
 while(*str > 0)
  glCallList(DISPLAY_LIST_FONT_BASE + *str++);
}

variable_types:
str
char *

variable_types: x int

variable_types: y int


decompiled_code:
__int64 __fastcall draw_hit_glow(float *a1)
{
glPushMatrix();
glTranslatef(*a1, a1[1], 0.0);
glScalef(20.0, 20.0, 1.0);
glCallList(22LL);
return glPopMatrix();
}

canonical_decompiled_code:
__int64 __fastcall func0(float *a1)
{
glPushMatrix();
glTranslatef(*a1, a1[1], 0.0);
glScalef(20.0, 20.0, 1.0);
glCallList(22LL);
return glPopMatrix();
}

original_code:
static void draw_hit_glow(pos_t *pos)
{
 glPushMatrix();
 glTranslatef(pos->x, pos->y, 0.0);
 glScalef(20.0, 20.0, 1.0);
 glCallList(DISPLAY_LIST_GLOW);
 glPopMatrix();
}

canonical_original_code:
static void draw_hit_glow(struct pos_t *pos)
{
 glPushMatrix();
 glTranslatef(pos->x, pos->y, 0.0);
 glScalef(20.0, 20.0, 1.0);
 glCallList(DISPLAY_LIST_GLOW);
 glPopMatrix();
}

variable_types:
pos
struct pos_t { float x; float y; } *


decompiled_code:
char *bullet_init()
{
__int64 v0; // rcx
__int64 v1; // rdi
char *result; // rax
char v3; // si
__int64 v4; // rdx

v0 = bullet_free;
v1 = qword_848;
result = (char *)&bullet_pool;
v3 = 0;
do
{
v4 = v0;
v0 = (__int64)result;
if ( v4 )
{
*(_QWORD *)(v4 + 8) = result;
*(_QWORD *)result = v4;
}
else
{
*(_QWORD *)result = 0LL;
v1 = (__int64)result;
v3 = 1;
}
*((_QWORD *)result + 1) = 0LL;
result += 72;
}
while ( result != "Too many bullets..." );
bullet_free = (__int64)&unk_24818;
if ( v3 )
qword_848 = v1;
return result;
}

canonical_decompiled_code:
char *func3()
{
__int64 v0; // rcx
__int64 v1; // rdi
char *result; // rax
char v3; // si
__int64 v4; // rdx

v0 = bullet_free;
v1 = qword_848;
result = (char *)&bullet_pool;
v3 = 0;
do
{
v4 = v0;
v0 = (__int64)result;
if ( v4 )
{
*(_QWORD *)(v4 + 8) = result;
*(_QWORD *)result = v4;
}
else
{
*(_QWORD *)result = 0LL;
v1 = (__int64)result;
v3 = 1;
}
*((_QWORD *)result + 1) = 0LL;
result += 72;
}
while ( result != "Too many bullets..." );
bullet_free = (__int64)&unk_24818;
if ( v3 )
qword_848 = v1;
return result;
}

original_code:
void bullet_init()
{
 int i;
 for(i = 0; i < 2048; i++)
  do { if((&bullet_free)->head != 0) { (&bullet_free)->head->list.prev = &bullet_pool[i]; (&bullet_pool[i])->list.next = (&bullet_free)->head; } else { (&bullet_free)->tail = &bullet_pool[i]; (&bullet_pool[i])->list.next = 0; } (&bullet_free)->head = &bullet_pool[i]; (&bullet_pool[i])->list.prev = 0; } while(0);
}


reset

decompiled_code:
__int64 reset()
{
grid_init();
bullet_init();
noob_init();
tower_init();
state_reset(&kgtd_state);
return level_init(&kgtd_state);
}

canonical_decompiled_code:
__int64 func1()
{
grid_init();
bullet_init();
noob_init();
tower_init();
state_reset(&kgtd_state);
return level_init(&kgtd_state);
}

original_code:
static void reset(void)
{
 grid_init();
 bullet_init();
 noob_init();
 tower_init();
 state_reset(&kgtd_state);
 level_init(&kgtd_state);
}

canonical_original_code:
static void reset()
{
 grid_init();
 bullet_init();
 noob_init();
 tower_init();
 state_reset(&kgtd_state);
 level_init(&kgtd_state);
}


init

decompiled_code:
__int64 init()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax

if ( (int)SDL_Init(33LL) < 0 )
{
v1 = (const char *)SDL_GetError();
printf("Init failed: %s\n", v1);
exit(1);
}
SDL_GL_SetAttribute(7LL, 8LL);
SDL_GL_SetAttribute(5LL, 1LL);
if ( !SDL_SetVideoMode(800LL, 608LL, 32LL, 3LL) )
{
v2 = (const char *)SDL_GetError();
printf("Video mode init failed: %s\n", v2);
exit(1);
}
SDL_WM_SetCaption("kgtd");
atexit((void (*)(void))&SDL_Quit);
glMatrixMode(5888LL);
glLoadIdentity();
glTranslatef(-1.0, 1.0, 0.0);
glScalef(0.0024999999, -0.0032894737, 1.0);
glClearColor(0.0, 0.0, 0.0);
glEnable(3042LL);
glEnable(2960LL);
glBlendFunc(770LL, 1LL);
glDisable(2896LL);
glDisable(2884LL);
glDisable(2929LL);
glDisable(3553LL);
glDisable(2903LL);
controls_init();
graphics_init();
text_init();
reset();
oldtime = SDL_GetTicks();
result = SDL_AddTimer(25LL, timer_cb, 0LL);
if ( !result )
{
puts("Error setting update timer...");
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax

if ( (int)SDL_Init(33LL) < 0 )
{
v1 = (const char *)SDL_GetError();
printf("Init failed: %s\n", v1);
exit(1);
}
SDL_GL_SetAttribute(7LL, 8LL);
SDL_GL_SetAttribute(5LL, 1LL);
if ( !SDL_SetVideoMode(800LL, 608LL, 32LL, 3LL) )
{
v2 = (const char *)SDL_GetError();
printf("Video mode init failed: %s\n", v2);
exit(1);
}
SDL_WM_SetCaption("kgtd");
atexit((void (*)(void))&SDL_Quit);
glMatrixMode(5888LL);
glLoadIdentity();
glTranslatef(-1.0, 1.0, 0.0);
glScalef(0.0024999999, -0.0032894737, 1.0);
glClearColor(0.0, 0.0, 0.0);
glEnable(3042LL);
glEnable(2960LL);
glBlendFunc(770LL, 1LL);
glDisable(2896LL);
glDisable(2884LL);
glDisable(2929LL);
glDisable(3553LL);
glDisable(2903LL);
controls_init();
graphics_init();
text_init();
func1();
oldtime = SDL_GetTicks();
result = SDL_AddTimer(25LL, timer_cb, 0LL);
if ( !result )
{
puts("Error setting update timer...");
exit(1);
}
return result;
}

original_code:
static void init(void)
{
 screen = ((void *)0);
 if(SDL_Init(0x00000020 | 0x00000001) < 0) {
  printf("Init failed: %s\n", SDL_GetError());
  exit(1);
 }
 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 screen = SDL_SetVideoMode(800, 608, 32, 0x00000002 | 0x00000001);
 if(!screen) {
  printf("Video mode init failed: %s\n", SDL_GetError());
  exit(1);
 }
 SDL_WM_SetCaption("kgtd", "kgtd");
 atexit(SDL_Quit);
 glMatrixMode(0x1700);
 glLoadIdentity();
 glTranslatef(-1.0, 1.0, 0.0);
 glScalef(2.0/800, -2.0/608, 1.0);
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glEnable(0x0BE2);
 glEnable(0x0B90);
 glBlendFunc(0x0302, 1);
 glDisable(0x0B50);
 glDisable(0x0B44);
 glDisable(0x0B71);
 glDisable(0x0DE1);
 glDisable(0x0B57);
 controls_init();
 graphics_init();
 text_init();
 reset();
 oldtime = SDL_GetTicks();
 if(SDL_AddTimer(25, timer_cb, ((void *)0)) == ((void *)0)) {
  printf("Error setting update timer...\n");
  exit(1);
 }
}

canonical_original_code:
static void init()
{
 screen = ((void *)0);
 if(SDL_Init(0x00000020 | 0x00000001) < 0) {
  printf("Init failed: %s\n", SDL_GetError());
  exit(1);
 }
 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 screen = SDL_SetVideoMode(800, 608, 32, 0x00000002 | 0x00000001);
 if(!screen) {
  printf("Video mode init failed: %s\n", SDL_GetError());
  exit(1);
 }
 SDL_WM_SetCaption("kgtd", "kgtd");
 atexit(SDL_Quit);
 glMatrixMode(0x1700);
 glLoadIdentity();
 glTranslatef(-1.0, 1.0, 0.0);
 glScalef(2.0/800, -2.0/608, 1.0);
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glEnable(0x0BE2);
 glEnable(0x0B90);
 glBlendFunc(0x0302, 1);
 glDisable(0x0B50);
 glDisable(0x0B44);
 glDisable(0x0B71);
 glDisable(0x0DE1);
 glDisable(0x0B57);
 controls_init();
 graphics_init();
 text_init();
 reset();
 oldtime = SDL_GetTicks();
 if(SDL_AddTimer(25, timer_cb, ((void *)0)) == ((void *)0)) {
  printf("Error setting update timer...\n");
  exit(1);
 }
}


update

decompiled_code:
__int64 update()
{
int v0; // eax
unsigned int v1; // ebp
int v2; // ebx
float v3; // xmm0_4
__int64 result; // rax

v0 = SDL_GetTicks();
v1 = v0 - oldtime;
v2 = v0;
v3 = (float)(v0 - oldtime) * 0.001;
noob_update_all((unsigned int)(v0 - oldtime), &kgtd_state);
bullet_update_all(v1, v3);
tower_update_all(v1);
controls_update(v1, &kgtd_state);
result = level_update(v1, &kgtd_state);
oldtime = v2;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // eax
unsigned int v1; // ebp
int v2; // ebx
float v3; // xmm0_4
__int64 result; // rax

v0 = SDL_GetTicks();
v1 = v0 - oldtime;
v2 = v0;
v3 = (float)(v0 - oldtime) * 0.001;
noob_update_all((unsigned int)(v0 - oldtime), &kgtd_state);
bullet_update_all(v1, v3);
tower_update_all(v1);
controls_update(v1, &kgtd_state);
result = level_update(v1, &kgtd_state);
oldtime = v2;
return result;
}

original_code:
static void update(void)
{
 int newtime = SDL_GetTicks();
 int idt = newtime - oldtime;
 float dt = (float)idt / 1000.0;
 noob_update_all(dt, idt, &kgtd_state);
 bullet_update_all(dt, idt);
 tower_update_all(idt);
 controls_update(idt, &kgtd_state);
 level_update(idt, &kgtd_state);
 oldtime = newtime;
}

canonical_original_code:
static void update()
{
 int newtime = SDL_GetTicks();
 int idt = newtime - oldtime;
 float dt = (float)idt / 1000.0;
 noob_update_all(dt, idt, &kgtd_state);
 bullet_update_all(dt, idt);
 tower_update_all(idt);
 controls_update(idt, &kgtd_state);
 level_update(idt, &kgtd_state);
 oldtime = newtime;
}

variable_types: newtime int

variable_types: idt int

variable_types: dt float


draw

decompiled_code:
__int64 draw()
{
glClear(17664LL);
controls_draw(&kgtd_state);
path_draw_all(&kgtd_state);
noob_draw_all();
tower_draw_all();
bullet_draw_all();
return SDL_GL_SwapBuffers();
}

canonical_decompiled_code:
__int64 func5()
{
glClear(17664LL);
controls_draw(&kgtd_state);
path_draw_all(&kgtd_state);
noob_draw_all();
tower_draw_all();
bullet_draw_all();
return SDL_GL_SwapBuffers();
}

original_code:
static void draw(void)
{
 glClear(0x00004000 | 0x00000100 |
         0x00000400);
 controls_draw(&kgtd_state);
 path_draw_all(&kgtd_state);
 noob_draw_all();
 tower_draw_all();
 bullet_draw_all();
 SDL_GL_SwapBuffers();
}

canonical_original_code:
static void draw()
{
 glClear(0x00004000 | 0x00000100 |
         0x00000400);
 controls_draw(&kgtd_state);
 path_draw_all(&kgtd_state);
 noob_draw_all();
 tower_draw_all();
 bullet_draw_all();
 SDL_GL_SwapBuffers();
}


decompiled_code:
void __fastcall move_dir(float *a1, float a2, float a3)
{
float v3; // xmm3_4
float v4; // xmm3_4

v3 = *a1;
if ( a2 > COERCE_FLOAT(COERCE_UNSIGNED_INT(a3 - *a1) & LC0) )
{
*a1 = a3;
}
else
{
if ( a3 <= v3 )
v4 = v3 - a2;
else
v4 = v3 + a2;
*a1 = v4;
}
}

canonical_decompiled_code:
void __fastcall func0(float *a1, float a2, float a3)
{
float v3; // xmm3_4
float v4; // xmm3_4

v3 = *a1;
if ( a2 > COERCE_FLOAT(COERCE_UNSIGNED_INT(a3 - *a1) & LC0) )
{
*a1 = a3;
}
else
{
if ( a3 <= v3 )
v4 = v3 - a2;
else
v4 = v3 + a2;
*a1 = v4;
}
}

original_code:
static inline float move_dir(float d, float *loc, float dest)
{
 float l;
 if((l = fabs(dest - *loc)) < d) {
  *loc = dest;
  return l;
 }
 if(dest > *loc)
  *loc += d;
 else
  *loc -= d;
 return d;
}

variable_types: d float

variable_types:
loc
float *

variable_types: dest float

variable_types: l float

return_type: float


decompiled_code:
char *noob_init()
{
__int64 v0; // rcx
__int64 v1; // rdi
char *result; // rax
char v3; // si
__int64 v4; // rdx

v0 = noob_free;
v1 = qword_618;
result = (char *)&noobs_pool;
v3 = 0;
do
{
v4 = v0;
v0 = (__int64)result;
if ( v4 )
{
*(_QWORD *)(v4 + 8) = result;
*(_QWORD *)result = v4;
}
else
{
*(_QWORD *)result = 0LL;
v1 = (__int64)result;
v3 = 1;
}
*((_QWORD *)result + 1) = 0LL;
result += 64;
}
while ( result != "Too many noobs..." );
noob_free = (__int64)&unk_105E0;
if ( v3 )
qword_618 = v1;
return result;
}

canonical_decompiled_code:
char *func3()
{
__int64 v0; // rcx
__int64 v1; // rdi
char *result; // rax
char v3; // si
__int64 v4; // rdx

v0 = noob_free;
v1 = qword_618;
result = (char *)&noobs_pool;
v3 = 0;
do
{
v4 = v0;
v0 = (__int64)result;
if ( v4 )
{
*(_QWORD *)(v4 + 8) = result;
*(_QWORD *)result = v4;
}
else
{
*(_QWORD *)result = 0LL;
v1 = (__int64)result;
v3 = 1;
}
*((_QWORD *)result + 1) = 0LL;
result += 64;
}
while ( result != "Too many noobs..." );
noob_free = (__int64)&unk_105E0;
if ( v3 )
qword_618 = v1;
return result;
}

original_code:
void noob_init()
{
 int i;
 for(i = 0; i < 1024; i++)
  do { if((&noob_free)->head != 0) { (&noob_free)->head->list.prev = &noobs_pool[i]; (&noobs_pool[i])->list.next = (&noob_free)->head; } else { (&noob_free)->tail = &noobs_pool[i]; (&noobs_pool[i])->list.next = 0; } (&noob_free)->head = &noobs_pool[i]; (&noobs_pool[i])->list.prev = 0; } while(0);
}


decompiled_code:
__int64 __fastcall noob_ref_destroy(__int64 a1)
{
int v1; // eax
__int64 result; // rax
__int64 *v3; // rdx
__int64 v4; // rax

v1 = *(_DWORD *)(a1 + 56);
if ( !v1 )
_assert_fail("noob->refcnt > 0", "noob.c", 0x33u, "noob_ref_destroy");
result = (unsigned int)(v1 - 1);
*(_DWORD *)(a1 + 56) = result;
if ( !(_DWORD)result )
{
v3 = *(__int64 **)(a1 + 8);
v4 = *(_QWORD *)a1;
if ( v3 )
{
*v3 = v4;
v4 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
goto LABEL_5;
}
else
{
noob_list = *(_QWORD *)a1;
if ( v4 )
{
LABEL_5:
*(_QWORD *)(v4 + 8) = v3;
result = noob_free;
if ( noob_free )
{
LABEL_6:
*(_QWORD *)(result + 8) = a1;
*(_QWORD *)a1 = result;
LABEL_7:
noob_free = a1;
*(_QWORD *)(a1 + 8) = 0LL;
return result;
}
LABEL_11:
qword_618 = a1;
*(_QWORD *)a1 = 0LL;
goto LABEL_7;
}
}
result = noob_free;
*(&noob_list + 1) = (__int64)v3;
if ( result )
goto LABEL_6;
goto LABEL_11;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v1; // eax
__int64 result; // rax
__int64 *v3; // rdx
__int64 v4; // rax

v1 = *(_DWORD *)(a1 + 56);
if ( !v1 )
_assert_fail("noob->refcnt > 0", "noob.c", 0x33u, "noob_ref_destroy");
result = (unsigned int)(v1 - 1);
*(_DWORD *)(a1 + 56) = result;
if ( !(_DWORD)result )
{
v3 = *(__int64 **)(a1 + 8);
v4 = *(_QWORD *)a1;
if ( v3 )
{
*v3 = v4;
v4 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
goto LABEL_5;
}
else
{
noob_list = *(_QWORD *)a1;
if ( v4 )
{
LABEL_5:
*(_QWORD *)(v4 + 8) = v3;
result = noob_free;
if ( noob_free )
{
LABEL_6:
*(_QWORD *)(result + 8) = a1;
*(_QWORD *)a1 = result;
LABEL_7:
noob_free = a1;
*(_QWORD *)(a1 + 8) = 0LL;
return result;
}
LABEL_11:
qword_618 = a1;
*(_QWORD *)a1 = 0LL;
goto LABEL_7;
}
}
result = noob_free;
*(&noob_list + 1) = (__int64)v3;
if ( result )
goto LABEL_6;
goto LABEL_11;
}
return result;
}

original_code:
void noob_ref_destroy(noob_t *noob)
{
 ((void) sizeof ((noob->refcnt > 0) ? 1 : 0), __extension__ ({ if (noob->refcnt > 0) ; else __assert_fail ("noob->refcnt > 0", "noob.c", 51, __extension__ __PRETTY_FUNCTION__); }));
 if(--noob->refcnt)
  return;
 do { if((noob)->list.prev) (noob)->list.prev->list.next = (noob)->list.next; else (&noob_list)->head = (noob)->list.next; if((noob)->list.next) (noob)->list.next->list.prev = (noob)->list.prev; else (&noob_list)->tail = (noob)->list.prev; } while(0);
 do { if((&noob_free)->head != 0) { (&noob_free)->head->list.prev = noob; (noob)->list.next = (&noob_free)->head; } else { (&noob_free)->tail = noob; (noob)->list.next = 0; } (&noob_free)->head = noob; (noob)->list.prev = 0; } while(0);
}

canonical_original_code:
void noob_ref_destroy(struct noob_t *noob)
{
 ((void) sizeof ((noob->refcnt > 0) ? 1 : 0), __extension__ ({ if (noob->refcnt > 0) ; else __assert_fail ("noob->refcnt > 0", "noob.c", 51, __extension__ __PRETTY_FUNCTION__); }));
 if(--noob->refcnt)
  return;
 do { if((noob)->list.prev) (noob)->list.prev->list.next = (noob)->list.next; else (&noob_list)->head = (noob)->list.next; if((noob)->list.next) (noob)->list.next->list.prev = (noob)->list.prev; else (&noob_list)->tail = (noob)->list.prev; } while(0);
 do { if((&noob_free)->head != 0) { (&noob_free)->head->list.prev = noob; (noob)->list.next = (&noob_free)->head; } else { (&noob_free)->tail = noob; (noob)->list.next = 0; } (&noob_free)->head = noob; (noob)->list.prev = 0; } while(0);
}

variable_types: struct noob_t { struct { struct noob_t *next; } list; struct path_t { enum grid_type { GRID_TYPE_NONE=0, GRID_TYPE_TOWER=1, GRID_TYPE_PATH=2 } g; struct pos_t { float x; float y; } pos; struct path_t *next; } *path; struct pos_t pos; float speed; int hp; int max_hp; int shield; int max_shield; int stun_time; unsigned int refcnt; unsigned char armor_type; unsigned char future_stun; unsigned char is_dead; } *

user_defined_types: struct noob_t { struct { struct noob_t *next; } list; struct path_t *path; struct pos_t pos; float speed; int hp; int max_hp; int shield; int max_shield; int stun_time; unsigned int refcnt; unsigned char armor_type; unsigned char future_stun; unsigned char is_dead; }

user_defined_types: struct path_t { enum grid_type g; struct pos_t pos; struct path_t *next; }


decompiled_code:
void noob_draw_all()
{
_QWORD *v0; // rbx

v0 = (_QWORD *)noob_list;
if ( noob_list )
{
do
{
draw_each((__int64)v0);
v0 = (_QWORD *)*v0;
}
while ( v0 );
}
}

canonical_decompiled_code:
void func8()
{
_QWORD *v0; // rbx

v0 = (_QWORD *)noob_list;
if ( noob_list )
{
do
{
func1((__int64)v0);
v0 = (_QWORD *)*v0;
}
while ( v0 );
}
}

original_code:
void noob_draw_all()
{
 noob_t *cur;
 for(cur = (&noob_list)->head; cur; cur = (cur)->list.next)
  draw_each(cur);
}

canonical_original_code:
void noob_draw_all()
{
 struct noob_t *cur;
 for(cur = (&noob_list)->head; cur; cur = (cur)->list.next)
  draw_each(cur);
}

user_defined_types: enum grid_type { GRID_TYPE_NONE=0, GRID_TYPE_TOWER=1, GRID_TYPE_PATH=2 }


decompiled_code:
__int64 __fastcall noob_find_target(float *a1, unsigned int a2)
{
float v2; // xmm0_4
__int64 v3; // rbx
__int64 v4; // r12
float v5; // xmm0_4
bool v6; // cc
float v7; // xmm0_4
float v9; // [rsp+Ch] [rbp-2Ch]

v2 = tt_data[16 * (unsigned __int64)a2 + 8];
v3 = noob_list;
v9 = v2 * v2;
if ( !noob_list )
return 0LL;
v4 = 0LL;
do
{
if ( !(unsigned int)damage_not_worthwhile(v3, a2) )
{
v5 = distance2_isra_0(*a1, a1[1], *(float *)(v3 + 24), *(float *)(v3 + 28));
v6 = v9 <= v5;
v7 = fminf(v5, v9);
if ( !v6 )
v4 = v3;
v9 = v7;
}
v3 = *(_QWORD *)v3;
}
while ( v3 );
if ( v4 )
++*(_DWORD *)(v4 + 56);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(float *a1, unsigned int a2)
{
float v2; // xmm0_4
__int64 v3; // rbx
__int64 v4; // r12
float v5; // xmm0_4
bool v6; // cc
float v7; // xmm0_4
float v9; // [rsp+Ch] [rbp-2Ch]

v2 = tt_data[16 * (unsigned __int64)a2 + 8];
v3 = noob_list;
v9 = v2 * v2;
if ( !noob_list )
return 0LL;
v4 = 0LL;
do
{
if ( !(unsigned int)damage_not_worthwhile(v3, a2) )
{
v5 = func2(*a1, a1[1], *(float *)(v3 + 24), *(float *)(v3 + 28));
v6 = v9 <= v5;
v7 = fminf(v5, v9);
if ( !v6 )
v4 = v3;
v9 = v7;
}
v3 = *(_QWORD *)v3;
}
while ( v3 );
if ( v4 )
++*(_DWORD *)(v4 + 56);
return v4;
}

original_code:
noob_t *noob_find_target(pos_t *pos, ttype_t type)
{
 float mag, min_range = tt_data[type].tower.range;
 noob_t *noob = ((void *)0), *cur;
 min_range = min_range * min_range;
 for(cur = (&noob_list)->head; cur; cur = (cur)->list.next) {
  if(damage_not_worthwhile(cur, type))
   continue;
  mag = distance2(pos, &cur->pos);
  if(mag < min_range) {
   min_range = mag;
   noob = cur;
  }
 }
 if(noob)
  noob->refcnt++;
 return noob;
}

canonical_original_code:
struct noob_t *noob_find_target(struct pos_t *pos, enum ttype type)
{
 float mag; float min_range = tt_data[type].tower.range;
 struct noob_t *noob = ((void *)0); struct noob_t *cur;
 min_range = min_range * min_range;
 for(cur = (&noob_list)->head; cur; cur = (cur)->list.next) {
  if(damage_not_worthwhile(cur, type))
   continue;
  mag = distance2(pos, &cur->pos);
  if(mag < min_range) {
   min_range = mag;
   noob = cur;
  }
 }
 if(noob)
  noob->refcnt++;
 return noob;
}

variable_types:
pos
struct pos_t *

variable_types: enum ttype { TT_PLASMA=0, TT_LTNG=1, TT_CW=2, TT_PULSE=3, TT_APCR=4, TT_APFSDS=5, TT_DU=6, TT_HE=7, TT_HEAT=8, TT_AREA=9, TT_BASIC=10, TT_NONE=11 }

variable_types: mag float

return_type: struct noob_t { struct { struct noob_t *next; } list; struct path_t { enum grid_type { GRID_TYPE_NONE=0, GRID_TYPE_TOWER=1, GRID_TYPE_PATH=2 } g; struct pos_t { float x; float y; } pos; struct path_t *next; } *path; struct pos_t pos; float speed; int hp; int max_hp; int shield; int max_shield; int stun_time; unsigned int refcnt; unsigned char armor_type; unsigned char future_stun; unsigned char is_dead; } *


decompiled_code:
void tower_init()
{
tower_list = 0LL;
qword_648 = 0LL;
}

canonical_decompiled_code:
void func2()
{
tower_list = 0LL;
qword_648 = 0LL;
}

original_code:
void tower_init()
{
 do { (&tower_list)->head = 0; (&tower_list)->tail = 0; } while(0);
}


decompiled_code:
char *__fastcall tower_new(int a1, int a2, int a3, unsigned int a4)
{
__int64 v5; // rdx
char *result; // rax
unsigned __int64 v7; // rcx
__int64 v8; // rdx
__int64 v9; // rdx

v5 = (a1 + 25LL * a2) << 6;
result = (char *)grid + v5;
if ( *(_DWORD *)((char *)grid + v5) )
return 0LL;
*((_DWORD *)result + 7) = a4;
*((_DWORD *)result + 3) = a3;
v7 = (unsigned __int64)a4 << 6;
*(_DWORD *)((char *)grid + v5) = 1;
v8 = *(_QWORD *)((char *)&tt_data[6] + v7);
*((_QWORD *)result + 4) = 0LL;
*((_QWORD *)result + 5) = v8;
v9 = tower_list;
*((float *)result + 1) = (float)(32 * a1 + 16);
*((_QWORD *)result + 2) = 0x186A000000000LL;
*((float *)result + 2) = (float)(32 * a2 + 16);
*((float *)result + 6) = *(float *)((char *)&tt_data[8] + v7) * *(float *)((char *)&tt_data[8] + v7);
if ( v9 )
{
*(_QWORD *)(v9 + 56) = result;
*((_QWORD *)result + 6) = v9;
}
else
{
qword_648 = (__int64)result;
*((_QWORD *)result + 6) = 0LL;
}
tower_list = (__int64)result;
grid[8 * (25LL * a2 + a1) + 7] = 0LL;
return result;
}

canonical_decompiled_code:
char *__fastcall func3(int a1, int a2, int a3, unsigned int a4)
{
__int64 v5; // rdx
char *result; // rax
unsigned __int64 v7; // rcx
__int64 v8; // rdx
__int64 v9; // rdx

v5 = (a1 + 25LL * a2) << 6;
result = (char *)grid + v5;
if ( *(_DWORD *)((char *)grid + v5) )
return 0LL;
*((_DWORD *)result + 7) = a4;
*((_DWORD *)result + 3) = a3;
v7 = (unsigned __int64)a4 << 6;
*(_DWORD *)((char *)grid + v5) = 1;
v8 = *(_QWORD *)((char *)&tt_data[6] + v7);
*((_QWORD *)result + 4) = 0LL;
*((_QWORD *)result + 5) = v8;
v9 = tower_list;
*((float *)result + 1) = (float)(32 * a1 + 16);
*((_QWORD *)result + 2) = 0x186A000000000LL;
*((float *)result + 2) = (float)(32 * a2 + 16);
*((float *)result + 6) = *(float *)((char *)&tt_data[8] + v7) * *(float *)((char *)&tt_data[8] + v7);
if ( v9 )
{
*(_QWORD *)(v9 + 56) = result;
*((_QWORD *)result + 6) = v9;
}
else
{
qword_648 = (__int64)result;
*((_QWORD *)result + 6) = 0LL;
}
tower_list = (__int64)result;
grid[8 * (25LL * a2 + a1) + 7] = 0LL;
return result;
}

original_code:
tower_t *tower_new(int x, int y, unsigned int power, ttype_t type)
{
 if(grid[y][x].g != GRID_TYPE_NONE)
  return ((void *)0);
 tower_t *tower = &(grid[y][x].t);
 tower->g = GRID_TYPE_TOWER;
 tower->pos.x = x * 32 + 32/2;
 tower->pos.y = y * 32 + 32/2;
 tower->power = power;
 tower->energy = 0;
 tower->energymax = 100000;
 tower->type = type;
 tower->range = tt_data[type].tower.range * tt_data[type].tower.range;
 tower->update = tt_data[type].tower.upd;
 tower->target = ((void *)0);
 do { if((&tower_list)->head != 0) { (&tower_list)->head->list.prev = tower; (tower)->list.next = (&tower_list)->head; } else { (&tower_list)->tail = tower; (tower)->list.next = 0; } (&tower_list)->head = tower; (tower)->list.prev = 0; } while(0);
 return tower;
}

canonical_original_code:
struct tower_t *tower_new(int x, int y, unsigned int power, enum ttype type)
{
 if(grid[y][x].g != GRID_TYPE_NONE)
  return ((void *)0);
 struct tower_t *tower = &(grid[y][x].t);
 tower->g = GRID_TYPE_TOWER;
 tower->pos.x = x * 32 + 32/2;
 tower->pos.y = y * 32 + 32/2;
 tower->power = power;
 tower->energy = 0;
 tower->energymax = 100000;
 tower->type = type;
 tower->range = tt_data[type].tower.range * tt_data[type].tower.range;
 tower->update = tt_data[type].tower.upd;
 tower->target = ((void *)0);
 do { if((&tower_list)->head != 0) { (&tower_list)->head->list.prev = tower; (tower)->list.next = (&tower_list)->head; } else { (&tower_list)->tail = tower; (tower)->list.next = 0; } (&tower_list)->head = tower; (tower)->list.prev = 0; } while(0);
 return tower;
}

variable_types:
power
unsigned int

variable_types: struct tower_t { enum grid_type { GRID_TYPE_NONE=0, GRID_TYPE_TOWER=1, GRID_TYPE_PATH=2 } g; struct pos_t { float x; float y; } pos; unsigned int power; unsigned int energy; unsigned int energymax; float range; enum ttype { TT_PLASMA=0, TT_LTNG=1, TT_CW=2, TT_PULSE=3, TT_APCR=4, TT_APFSDS=5, TT_DU=6, TT_HE=7, TT_HEAT=8, TT_AREA=9, TT_BASIC=10, TT_NONE=11 } type; struct noob_t { struct { struct noob_t *next; } list; struct path_t { enum grid_type { GRID_TYPE_NONE=0, GRID_TYPE_TOWER=1, GRID_TYPE_PATH=2 } g; struct pos_t pos; struct path_t *next; } *path; struct pos_t pos; float speed; int hp; int max_hp; int shield; int max_shield; int stun_time; unsigned int refcnt; unsigned char armor_type; unsigned char future_stun; unsigned char is_dead; } *target; void (*update)(struct tower_t *, int); struct { struct tower_t *next; } list; } *

user_defined_types: struct tower_t { enum grid_type g; struct pos_t pos; unsigned int power; unsigned int energy; unsigned int energymax; float range; enum ttype type; struct noob_t *target; void (*update)(struct tower_t *, int); struct { struct tower_t *next; } list; }


decompiled_code:
__int64 __fastcall tower_upd_cw(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v4; // edx
__int64 v5; // rdi

result = *(unsigned int *)(a1 + 12);
if ( (_DWORD)result )
{
v4 = *(_DWORD *)(a1 + 16);
if ( v4 < *(_DWORD *)(a1 + 20) )
{
result = *(_DWORD *)(a1 + 28) == 0 ? 150 : 50;
*(_DWORD *)(a1 + 16) = v4 + result * a2;
}
else
{
v5 = *(_QWORD *)(a1 + 32);
if ( v5 )
{
if ( distance2_isra_0(*(float *)(a1 + 4), *(float *)(a1 + 8), *(float *)(v5 + 24), *(float *)(v5 + 28)) > *(float *)(a1 + 24) )
{
LABEL_7:
noob_ref_destroy(v5);
*(_QWORD *)(a1 + 32) = 0LL;
result = *(unsigned int *)(a1 + 20);
*(_DWORD *)(a1 + 16) -= result;
return result;
}
result = damage_not_worthwhile(v5, *(unsigned int *)(a1 + 28));
if ( (_DWORD)result )
{
v5 = *(_QWORD *)(a1 + 32);
goto LABEL_7;
}
}
else
{
result = noob_find_target(a1 + 4, *(unsigned int *)(a1 + 28));
*(_QWORD *)(a1 + 32) = result;
if ( result )
result = bullet_new(a1 + 4, (unsigned int)(*(_DWORD *)(a1 + 12) << 10), *(unsigned int *)(a1 + 28), result);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v4; // edx
__int64 v5; // rdi

result = *(unsigned int *)(a1 + 12);
if ( (_DWORD)result )
{
v4 = *(_DWORD *)(a1 + 16);
if ( v4 < *(_DWORD *)(a1 + 20) )
{
result = *(_DWORD *)(a1 + 28) == 0 ? 150 : 50;
*(_DWORD *)(a1 + 16) = v4 + result * a2;
}
else
{
v5 = *(_QWORD *)(a1 + 32);
if ( v5 )
{
if ( func1(*(float *)(a1 + 4), *(float *)(a1 + 8), *(float *)(v5 + 24), *(float *)(v5 + 28)) > *(float *)(a1 + 24) )
{
LABEL_7:
noob_ref_destroy(v5);
*(_QWORD *)(a1 + 32) = 0LL;
result = *(unsigned int *)(a1 + 20);
*(_DWORD *)(a1 + 16) -= result;
return result;
}
result = damage_not_worthwhile(v5, *(unsigned int *)(a1 + 28));
if ( (_DWORD)result )
{
v5 = *(_QWORD *)(a1 + 32);
goto LABEL_7;
}
}
else
{
result = noob_find_target(a1 + 4, *(unsigned int *)(a1 + 28));
*(_QWORD *)(a1 + 32) = result;
if ( result )
result = bullet_new(a1 + 4, (unsigned int)(*(_DWORD *)(a1 + 12) << 10), *(unsigned int *)(a1 + 28), result);
}
}
}
return result;
}

original_code:
void tower_upd_cw(tower_t *tower, int dt)
{
 float cur_range = 3.40282346638528859811704183484516925e+38F;
 if(tower->power == 0)
  return;
 if(tower->energy < tower->energymax) {
  tower->energy += (tower->type == TT_PLASMA ? 150 : 50) * dt;
  return;
 }
 if(tower->target == ((void *)0)) {
  tower->target = noob_find_target(&tower->pos, tower->type);
  if(tower->target == ((void *)0))
   return;
  bullet_new(&tower->pos, tower->power * 1024 ,
             tower->type, tower->target);
  return;
 }
 cur_range = distance2(&tower->pos, &tower->target->pos);
 if(cur_range > tower->range ||
    damage_not_worthwhile(tower->target, tower->type)) {
  noob_ref_destroy(tower->target);
  tower->target = ((void *)0);
  tower->energy -= tower->energymax;
 }
}

canonical_original_code:
void tower_upd_cw(struct tower_t *tower, int dt)
{
 float cur_range = 3.40282346638528859811704183484516925e+38F;
 if(tower->power == 0)
  return;
 if(tower->energy < tower->energymax) {
  tower->energy += (tower->type == TT_PLASMA ? 150 : 50) * dt;
  return;
 }
 if(tower->target == ((void *)0)) {
  tower->target = noob_find_target(&tower->pos, tower->type);
  if(tower->target == ((void *)0))
   return;
  bullet_new(&tower->pos, tower->power * 1024 ,
             tower->type, tower->target);
  return;
 }
 cur_range = distance2(&tower->pos, &tower->target->pos);
 if(cur_range > tower->range ||
    damage_not_worthwhile(tower->target, tower->type)) {
  noob_ref_destroy(tower->target);
  tower->target = ((void *)0);
  tower->energy -= tower->energymax;
 }
}

variable_types: dt int

user_defined_types: enum ttype { TT_PLASMA=0, TT_LTNG=1, TT_CW=2, TT_PULSE=3, TT_APCR=4, TT_APFSDS=5, TT_DU=6, TT_HE=7, TT_HEAT=8, TT_AREA=9, TT_BASIC=10, TT_NONE=11 }