command

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

if ( (int)parse_addr((const char *)remote_host, &v8, &v7) < 0 )
return (unsigned int)-1;
v4[0] = 14;
v4[1] = -16;
v4[2] = 1;
v5 = eoj_code;
v6 = byte_12976;
mls_elnet_setup_frame_header(&req, v4, &v5, (unsigned __int8)esv_code, 1LL, 1LL);
v0 = setup_frame_data(byte_896B, 32756, (__int64)set_prop, set_prop_len, (unsigned __int8 *)get_prop, 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, v7, &req, (unsigned int)(v0 + 11), &res, 0x8000LL);
if ( v2 < 0 )
{
fprintf(stderr, "Error mls_elnet_rpc(%d)\n", (unsigned int)v2);
v1 = -10;
}
else if ( (byte_96A & 0xF0) == 80 )
{
fprintf(stderr, "Error response code (%02X)\n", (unsigned __int8)byte_96A);
v1 = -11;
}
else
{
output_data(byte_96B);
v1 = 0;
}
}
return v1;
}

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

if ( (int)func5((const char *)remote_host, &v8, &v7) < 0 )
return (unsigned int)-1;
v4[0] = 14;
v4[1] = -16;
v4[2] = 1;
v5 = eoj_code;
v6 = byte_12976;
mls_elnet_setup_frame_header(&req, v4, &v5, (unsigned __int8)esv_code, 1LL, 1LL);
v0 = func7(byte_896B, 32756, (__int64)set_prop, set_prop_len, (unsigned __int8 *)get_prop, 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, v7, &req, (unsigned int)(v0 + 11), &res, 0x8000LL);
if ( v2 < 0 )
{
fprintf(stderr, "Error mls_elnet_rpc(%d)\n", (unsigned int)v2);
v1 = -10;
}
else if ( (byte_96A & 0xF0) == 80 )
{
fprintf(stderr, "Error response code (%02X)\n", (unsigned __int8)byte_96A);
v1 = -11;
}
else
{
func9(byte_96B);
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; // ebx

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

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

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

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

*a2 = *a1;
a2[1] = a1;
result = *a1;
*(_QWORD *)(*a1 + 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)
{
_QWORD *v8; // rax
_QWORD *v9; // r15

v8 = malloc(0x30uLL);
if ( !v8 )
return (unsigned int)-*_errno_location();
v9 = v8 + 4;
v8[4] = 0LL;
v8[5] = 0LL;
*(_DWORD *)v8 = a2;
*((_DWORD *)v8 + 1) = a3;
v8[1] = a4;
v8[2] = a5;
v8[3] = a1;
mls_dlink_init(v8 + 4);
if ( a2 )
mls_dlink_insert((__int64 *)(a1 + 24), v9);
else
mls_dlink_insert((__int64 *)(a1 + 8), v9);
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 func7(__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
int v2; // er13
size_t v3; // rax
ssize_t v4; // rbx
char *v5; // rax
char serv[32]; // [rsp+0h] [rbp-568h] BYREF
char host[1040]; // [rsp+20h] [rbp-548h] BYREF
char s[8]; // [rsp+430h] [rbp-138h] BYREF
socklen_t addr_len; // [rsp+4BCh] [rbp-ACh] BYREF
struct sockaddr addr; // [rsp+4C0h] [rbp-A8h] BYREF

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

canonical_decompiled_code:
void __fastcall func0(int *a1)
{
int v1; // ebp
int v2; // er13
size_t v3; // rax
ssize_t v4; // rbx
char *v5; // rax
char serv[32]; // [rsp+0h] [rbp-568h] BYREF
char host[1040]; // [rsp+20h] [rbp-548h] BYREF
char s[8]; // [rsp+430h] [rbp-138h] BYREF
socklen_t addr_len; // [rsp+4BCh] [rbp-ACh] BYREF
struct sockaddr addr; // [rsp+4C0h] [rbp-A8h] BYREF

v1 = *a1;
do
{
while ( 1 )
{
addr_len = 128;
v4 = recvfrom(v1, s, 0x80uLL, 0, &addr, &addr_len);
if ( v4 != -1 )
break;
v2 = *_errno_location();
perror("recvfrom");
if ( v2 != 11 )
goto LABEL_7;
}
getnameinfo(&addr, addr_len, host, 0x401u, serv, 0x20u, 3);
fprintf(stdout, "recvfrom:%s:%s:len=%d\n", host, serv, (unsigned int)v4);
LABEL_7:
s[v4] = 0;
v5 = strpbrk(s, "\r\n");
if ( v5 )
*v5 = 0;
fprintf(stdout, "client> '%s'\n", s);
*(_QWORD *)&s[strlen(s)] = 0xA0D4B4F203E2DLL;
v3 = strlen(s);
}
while ( sendto(v1, s, v3, 0, &addr, addr_len) != -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
int *v5; // rax
int *v6; // rbx
unsigned int v8; // eax
char v9[264]; // [rsp+0h] [rbp-108h] 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
{
v8 = mls_net_getaddr_by_ifname(v4, 2LL, v9, 256LL);
if ( v8 )
{
fprintf(stderr, "mls_net_getaddr_by_ifname(%d) error.\n", v8);
return 0;
}
}
fprintf(stdout, "ifaddr => %s\n", v9);
v5 = (int *)mls_net_mcast_open_ctx(argv[1], argv[2], v9);
v6 = v5;
if ( v5 )
{
server_dispatch(v5);
mls_net_mcast_close_ctx(v6);
}
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
int *v5; // rax
int *v6; // rbx
unsigned int v8; // eax
char v9[264]; // [rsp+0h] [rbp-108h] 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
{
v8 = mls_net_getaddr_by_ifname(v4, 2LL, v9, 256LL);
if ( v8 )
{
fprintf(stderr, "mls_net_getaddr_by_ifname(%d) error.\n", v8);
return 0;
}
}
fprintf(stdout, "ifaddr => %s\n", v9);
v5 = (int *)mls_net_mcast_open_ctx(argv[1], argv[2], v9);
v6 = v5;
if ( v5 )
{
func0(v5);
mls_net_mcast_close_ctx(v6);
}
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()
{
mls_log_ini("stderr", 0x8000LL, 3LL, 3LL);
mls_log_set_module(0LL, "MLS", 3LL);
if ( mls_log_is_output(0, 2u) )
mls_log_printf(0LL, 2LL, "mls_el_ini", 169LL, "mls_el_ini() started.\n");
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
mls_log_ini("stderr", 0x8000LL, 3LL, 3LL);
mls_log_set_module(0LL, "MLS", 3LL);
if ( func0(0, 2u) )
mls_log_printf(0LL, 2LL, "mls_el_ini", 169LL, "mls_el_ini() started.\n");
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()
{
if ( mls_log_is_output(0, 2u) )
mls_log_printf(0LL, 2LL, "mls_el_fin", 177LL, "mls_el_fin() finished.\n");
return mls_log_fin();
}

canonical_decompiled_code:
__int64 func10()
{
if ( func0(0, 2u) )
mls_log_printf(0LL, 2LL, "mls_el_fin", 177LL, "mls_el_fin() finished.\n");
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

*a2 = *a1;
a2[1] = a1;
result = *a1;
*(_QWORD *)(*a1 + 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, int a3)
{
mls_dlink_init((_QWORD *)(a1 + 8));
*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 24) = a3;
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int a2, int a3)
{
func7((_QWORD *)(a1 + 8));
*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 24) = a3;
}

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 ( a1 < 0 || *(_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 ( a1 < 0 || *(_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, _DWORD *a3, int *a4)
{
void *v4; // r8
char *v6; // rsi
_BYTE *v7; // rdx
char v8; // al
int v9; // edx

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

canonical_decompiled_code:
void *__fastcall func3(void *src, int a2, _DWORD *a3, int *a4)
{
void *v4; // r8
char *v6; // rsi
_BYTE *v7; // rdx
char v8; // al
int v9; // edx

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

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 = sessions;
if ( sessions == ptr )
{
sessions = *(void **)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 || v4 - 1 == *((_DWORD *)ptr + 2) )
maxfd = v4 - 2;
}
free(ptr);
}

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

v1 = sessions;
if ( sessions == ptr )
{
sessions = *(void **)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 || 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)
{
char *v3; // rbp
int v4; // er13
unsigned int v5; // er12
int v6; // eax
const char *v7; // rbx
char *v8; // rax
char *v9; // rdx
const char *v10; // rbx
int result; // eax
_DWORD *v12; // rax
int v13; // ebx
char *v14; // rax
_QWORD *v15; // rax
int *v16; // rbx
int v17; // eax
int v18; // esi
void *v19; // rsi
signed int v20; // edx
socklen_t v21; // eax
int v22; // eax
int v23; // edx
int v24; // esi
int v25; // edx
int v26; // eax
int v27; // edx
int v28; // edx
int v29; // eax
__int64 v30; // r15
__int64 i; // r13
int v32; // eax
int *v33; // r12
int v34; // eax
int v35; // edi
int v36; // esi
int v37; // eax
int v38; // er14
int v39; // edi
int v40; // esi
int v41; // eax
int v42; // er13
int v43; // er14
int v44; // eax
int v45; // eax
int v46; // edi
int v47; // edi
__int64 v48; // [rsp+8h] [rbp-170h]
int v49; // [rsp+10h] [rbp-168h]
int fd; // [rsp+14h] [rbp-164h]
char *ident; // [rsp+18h] [rbp-160h]
int v52; // [rsp+24h] [rbp-154h] BYREF
socklen_t addr_len; // [rsp+28h] [rbp-150h] BYREF
int optval; // [rsp+2Ch] [rbp-14Ch] BYREF
fd_set writefds; // [rsp+30h] [rbp-148h] BYREF
fd_set readfds; // [rsp+B0h] [rbp-C8h] BYREF
struct sockaddr addr; // [rsp+130h] [rbp-48h] BYREF

optval = 1;
v3 = 0LL;
v4 = 0;
v5 = 23;
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_5;
v5 = atoi(optarg);
}
else if ( v6 == 100 )
{
v4 = 1;
}
else
{
if ( v6 != 105 )
LABEL_5:
usage();
v3 = strdup(optarg);
}
}
v7 = *argv;
v8 = strrchr(*argv, 47);
v9 = v8 + 1;
if ( !v8 )
v9 = (char *)v7;
ident = v9;
if ( !loginpath )
loginpath = "/bin/sh";
v10 = loginpath;
if ( access(loginpath, 1) < 0 )
{
fprintf(stderr, "\"%s\"", v10);
perror_msg_and_die(" is no valid executable!\n");
}
printf("%s: starting\n", ident);
if ( v3 )
{
printf(" port: %i; interface: %s; login program: %s\n", v5, v3, loginpath);
argv_init = loginpath;
sessions = 0LL;
fd = socket(2, 1, 0);
if ( fd >= 0 )
{
setsockopt(fd, 1, 2, &optval, 4u);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(v5);
goto LABEL_21;
}
LABEL_26:
perror("socket");
return 1;
}
printf(" port: %i; interface: %s; login program: %s\n", v5, "any", loginpath);
argv_init = loginpath;
sessions = 0LL;
fd = socket(2, 1, 0);
if ( fd < 0 )
goto LABEL_26;
setsockopt(fd, 1, 2, &optval, 4u);
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(v5);
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
v3 = "any";
LABEL_21:
openlog(ident, 9, 24);
syslog(6, "%s (port: %i, ifname: %s, login: %s) startup succeeded\n", ident, v5, v3, loginpath);
closelog();
if ( bind(fd, &addr, 0x10u) < 0 )
{
perror("bind");
result = 1;
}
else
{
if ( listen(fd, 1) >= 0 )
{
if ( v4 && daemon(0, 1) < 0 )
perror_msg_and_die("daemon");
maxfd = fd;
v49 = fd / 64;
v48 = 1LL << (fd % 64);
while ( 1 )
{
while ( 1 )
{
LABEL_44:
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
readfds.fds_bits[v49] |= v48;
v12 = sessions;
if ( sessions )
{
do
{
if ( (int)v12[12] > 0 )
writefds.fds_bits[v12[3] / 64] |= 1LL << (v12[3] % 64);
if ( (int)v12[12] <= 3999 )
readfds.fds_bits[v12[2] / 64] |= 1LL << (v12[2] % 64);
if ( (int)v12[15] > 0 )
writefds.fds_bits[v12[2] / 64] |= 1LL << (v12[2] % 64);
if ( (int)v12[15] <= 3999 )
readfds.fds_bits[v12[3] / 64] |= 1LL << (v12[3] % 64);
v12 = *(_DWORD **)v12;
}
while ( v12 );
}
result = select(maxfd + 1, &readfds, &writefds, 0LL, 0LL);
if ( !result )
return result;
if ( (readfds.fds_bits[v49] & v48) != 0 )
break;
LABEL_49:
v16 = (int *)sessions;
if ( sessions )
{
while ( 1 )
{
v33 = *(int **)v16;
v46 = v16[12];
if ( v46 && (writefds.fds_bits[v16[3] / 64] & (1LL << (v16[3] % 64))) != 0 )
{
v17 = v16[11];
v18 = 4000 - v17;
if ( 4000 - v17 > v46 )
v18 = v16[12];
v19 = remove_iacs((void *)(*((_QWORD *)v16 + 3) + v17), v18, &v52, (int *)&addr_len);
v20 = addr_len;
v21 = v52 - addr_len;
v16[11] += v52 - addr_len;
v16[12] -= v21;
v22 = write(v16[3], v19, v20);
if ( v22 < 0 )
{
LABEL_58:
perror("write");
free_session(v16);
goto LABEL_107;
}
v23 = v16[11] + v22;
v16[12] -= v22;
if ( v23 == 4000 )
v23 = 0;
v16[11] = v23;
}
v25 = v16[15];
if ( v25 )
break;
v34 = v16[12];
if ( v34 <= 3999 )
{
v39 = v16[2];
if ( (readfds.fds_bits[v39 / 64] & (1LL << (v39 % 64))) != 0 )
goto LABEL_93;
v25 = v16[15];
goto LABEL_90;
}
v35 = v16[3];
if ( (readfds.fds_bits[v35 / 64] & (1LL << (v35 % 64))) != 0 )
goto LABEL_81;
LABEL_105:
if ( !v16[15] )
{
v16[13] = 0;
v16[14] = 0;
}
LABEL_107:
if ( !v33 )
goto LABEL_44;
LABEL_108:
v16 = v33;
}
v47 = v16[2];
if ( (writefds.fds_bits[v47 / 64] & (1LL << (v47 % 64))) != 0 )
{
v24 = v16[14];
if ( 4000 - v24 <= v25 )
v25 = 4000 - v24;
v26 = write(v47, (const void *)(*((_QWORD *)v16 + 4) + v24), v25);
if ( v26 < 0 )
goto LABEL_58;
v27 = v16[14] + v26;
v16[15] -= v26;
if ( v27 == 4000 )
v27 = 0;
v16[14] = v27;
}
v34 = v16[12];
if ( v34 <= 3999 )
{
v39 = v16[2];
if ( (readfds.fds_bits[v39 / 64] & (1LL << (v39 % 64))) != 0 )
{
LABEL_93:
v40 = v16[10];
if ( v40 >= v34 )
v28 = 4000 - v40;
else
v28 = 4000 - v34;
v41 = read(v39, (void *)(*((_QWORD *)v16 + 3) + v40), v28);
v42 = v41;
v43 = v41;
if ( !v41 || v41 < 0 && *_errno_location() != 4 )
{
LABEL_68:
free_session(v16);
goto LABEL_107;
}
v44 = v16[10];
if ( !*(_BYTE *)(v42 + *((_QWORD *)v16 + 3) + v44 - 1LL) )
{
v43 = v42 - 1;
if ( v42 == 1 )
{
v33 = v16;
goto LABEL_108;
}
}
v45 = v43 + v44;
v16[12] += v43;
if ( v45 == 4000 )
v45 = 0;
v16[10] = v45;
}
}
v25 = v16[15];
if ( v25 <= 3999 )
{
LABEL_90:
v35 = v16[3];
if ( (readfds.fds_bits[v35 / 64] & (1LL << (v35 % 64))) != 0 )
{
LABEL_81:
v36 = v16[13];
if ( v36 >= v25 )
v29 = 4000 - v36;
else
v29 = 4000 - v25;
v37 = read(v35, (void *)(*((_QWORD *)v16 + 4) + v36), v29);
v38 = v37;
if ( !v37 )
goto LABEL_68;
if ( v37 >= 0 )
{
v30 = (unsigned int)(v37 - 1);
for ( i = 0LL; ; ++i )
{
if ( *(_BYTE *)(*((_QWORD *)v16 + 4) + i + v16[13]) == 3 )
fwrite("found <CTRL>-<C> in data!\n", 1uLL, 0x1AuLL, stderr);
if ( i == v30 )
break;
}
}
else if ( *_errno_location() != 4 )
{
goto LABEL_68;
}
v32 = v16[13] + v38;
v16[15] += v38;
if ( v32 == 4000 )
v32 = 0;
v16[13] = v32;
}
}
if ( !v16[12] )
{
v16[10] = 0;
v16[11] = 0;
}
goto LABEL_105;
}
}
addr_len = 16;
v13 = accept(fd, &addr, &addr_len);
if ( v13 >= 0 )
{
openlog(ident, 8, 24);
v14 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
syslog(6, "connection from: %s\n", v14);
closelog();
v15 = make_new_session(v13);
if ( v15 )
{
*v15 = sessions;
sessions = v15;
if ( maxfd < v13 )
maxfd = v13;
}
else
{
close(v13);
}
goto LABEL_49;
}
}
}
perror("listen");
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // er13
unsigned int v5; // er12
int v6; // eax
const char *v7; // rbx
char *v8; // rax
char *v9; // rdx
const char *v10; // rbx
int result; // eax
_DWORD *v12; // rax
int v13; // ebx
char *v14; // rax
_QWORD *v15; // rax
int *v16; // rbx
int v17; // eax
int v18; // esi
void *v19; // rsi
signed int v20; // edx
socklen_t v21; // eax
int v22; // eax
int v23; // edx
int v24; // esi
int v25; // edx
int v26; // eax
int v27; // edx
int v28; // edx
int v29; // eax
__int64 v30; // r15
__int64 i; // r13
int v32; // eax
int *v33; // r12
int v34; // eax
int v35; // edi
int v36; // esi
int v37; // eax
int v38; // er14
int v39; // edi
int v40; // esi
int v41; // eax
int v42; // er13
int v43; // er14
int v44; // eax
int v45; // eax
int v46; // edi
int v47; // edi
__int64 v48; // [rsp+8h] [rbp-170h]
int v49; // [rsp+10h] [rbp-168h]
int fd; // [rsp+14h] [rbp-164h]
char *ident; // [rsp+18h] [rbp-160h]
int v52; // [rsp+24h] [rbp-154h] BYREF
socklen_t addr_len; // [rsp+28h] [rbp-150h] BYREF
int optval; // [rsp+2Ch] [rbp-14Ch] BYREF
fd_set writefds; // [rsp+30h] [rbp-148h] BYREF
fd_set readfds; // [rsp+B0h] [rbp-C8h] BYREF
struct sockaddr addr; // [rsp+130h] [rbp-48h] BYREF

optval = 1;
v3 = 0LL;
v4 = 0;
v5 = 23;
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_5;
v5 = atoi(optarg);
}
else if ( v6 == 100 )
{
v4 = 1;
}
else
{
if ( v6 != 105 )
LABEL_5:
func5();
v3 = strdup(optarg);
}
}
v7 = *argv;
v8 = strrchr(*argv, 47);
v9 = v8 + 1;
if ( !v8 )
v9 = (char *)v7;
ident = v9;
if ( !loginpath )
loginpath = "/bin/sh";
v10 = loginpath;
if ( access(loginpath, 1) < 0 )
{
fprintf(stderr, "\"%s\"", v10);
func6(" is no valid executable!\n");
}
printf("%s: starting\n", ident);
if ( v3 )
{
printf(" port: %i; interface: %s; login program: %s\n", v5, v3, loginpath);
argv_init = loginpath;
sessions = 0LL;
fd = socket(2, 1, 0);
if ( fd >= 0 )
{
setsockopt(fd, 1, 2, &optval, 4u);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(v5);
goto LABEL_21;
}
LABEL_26:
perror("socket");
return 1;
}
printf(" port: %i; interface: %s; login program: %s\n", v5, "any", loginpath);
argv_init = loginpath;
sessions = 0LL;
fd = socket(2, 1, 0);
if ( fd < 0 )
goto LABEL_26;
setsockopt(fd, 1, 2, &optval, 4u);
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(v5);
*(_DWORD *)&addr.sa_data[2] = func1(0);
v3 = "any";
LABEL_21:
openlog(ident, 9, 24);
syslog(6, "%s (port: %i, ifname: %s, login: %s) startup succeeded\n", ident, v5, v3, loginpath);
closelog();
if ( bind(fd, &addr, 0x10u) < 0 )
{
perror("bind");
result = 1;
}
else
{
if ( listen(fd, 1) >= 0 )
{
if ( v4 && daemon(0, 1) < 0 )
func6("daemon");
maxfd = fd;
v49 = fd / 64;
v48 = 1LL << (fd % 64);
while ( 1 )
{
while ( 1 )
{
LABEL_44:
memset(&readfds, 0, sizeof(readfds));
memset(&writefds, 0, sizeof(writefds));
readfds.fds_bits[v49] |= v48;
v12 = sessions;
if ( sessions )
{
do
{
if ( (int)v12[12] > 0 )
writefds.fds_bits[v12[3] / 64] |= 1LL << (v12[3] % 64);
if ( (int)v12[12] <= 3999 )
readfds.fds_bits[v12[2] / 64] |= 1LL << (v12[2] % 64);
if ( (int)v12[15] > 0 )
writefds.fds_bits[v12[2] / 64] |= 1LL << (v12[2] % 64);
if ( (int)v12[15] <= 3999 )
readfds.fds_bits[v12[3] / 64] |= 1LL << (v12[3] % 64);
v12 = *(_DWORD **)v12;
}
while ( v12 );
}
result = select(maxfd + 1, &readfds, &writefds, 0LL, 0LL);
if ( !result )
return result;
if ( (readfds.fds_bits[v49] & v48) != 0 )
break;
LABEL_49:
v16 = (int *)sessions;
if ( sessions )
{
while ( 1 )
{
v33 = *(int **)v16;
v46 = v16[12];
if ( v46 && (writefds.fds_bits[v16[3] / 64] & (1LL << (v16[3] % 64))) != 0 )
{
v17 = v16[11];
v18 = 4000 - v17;
if ( 4000 - v17 > v46 )
v18 = v16[12];
v19 = func3((void *)(*((_QWORD *)v16 + 3) + v17), v18, &v52, (int *)&addr_len);
v20 = addr_len;
v21 = v52 - addr_len;
v16[11] += v52 - addr_len;
v16[12] -= v21;
v22 = write(v16[3], v19, v20);
if ( v22 < 0 )
{
LABEL_58:
perror("write");
func4(v16);
goto LABEL_107;
}
v23 = v16[11] + v22;
v16[12] -= v22;
if ( v23 == 4000 )
v23 = 0;
v16[11] = v23;
}
v25 = v16[15];
if ( v25 )
break;
v34 = v16[12];
if ( v34 <= 3999 )
{
v39 = v16[2];
if ( (readfds.fds_bits[v39 / 64] & (1LL << (v39 % 64))) != 0 )
goto LABEL_93;
v25 = v16[15];
goto LABEL_90;
}
v35 = v16[3];
if ( (readfds.fds_bits[v35 / 64] & (1LL << (v35 % 64))) != 0 )
goto LABEL_81;
LABEL_105:
if ( !v16[15] )
{
v16[13] = 0;
v16[14] = 0;
}
LABEL_107:
if ( !v33 )
goto LABEL_44;
LABEL_108:
v16 = v33;
}
v47 = v16[2];
if ( (writefds.fds_bits[v47 / 64] & (1LL << (v47 % 64))) != 0 )
{
v24 = v16[14];
if ( 4000 - v24 <= v25 )
v25 = 4000 - v24;
v26 = write(v47, (const void *)(*((_QWORD *)v16 + 4) + v24), v25);
if ( v26 < 0 )
goto LABEL_58;
v27 = v16[14] + v26;
v16[15] -= v26;
if ( v27 == 4000 )
v27 = 0;
v16[14] = v27;
}
v34 = v16[12];
if ( v34 <= 3999 )
{
v39 = v16[2];
if ( (readfds.fds_bits[v39 / 64] & (1LL << (v39 % 64))) != 0 )
{
LABEL_93:
v40 = v16[10];
if ( v40 >= v34 )
v28 = 4000 - v40;
else
v28 = 4000 - v34;
v41 = read(v39, (void *)(*((_QWORD *)v16 + 3) + v40), v28);
v42 = v41;
v43 = v41;
if ( !v41 || v41 < 0 && *_errno_location() != 4 )
{
LABEL_68:
func4(v16);
goto LABEL_107;
}
v44 = v16[10];
if ( !*(_BYTE *)(v42 + *((_QWORD *)v16 + 3) + v44 - 1LL) )
{
v43 = v42 - 1;
if ( v42 == 1 )
{
v33 = v16;
goto LABEL_108;
}
}
v45 = v43 + v44;
v16[12] += v43;
if ( v45 == 4000 )
v45 = 0;
v16[10] = v45;
}
}
v25 = v16[15];
if ( v25 <= 3999 )
{
LABEL_90:
v35 = v16[3];
if ( (readfds.fds_bits[v35 / 64] & (1LL << (v35 % 64))) != 0 )
{
LABEL_81:
v36 = v16[13];
if ( v36 >= v25 )
v29 = 4000 - v36;
else
v29 = 4000 - v25;
v37 = read(v35, (void *)(*((_QWORD *)v16 + 4) + v36), v29);
v38 = v37;
if ( !v37 )
goto LABEL_68;
if ( v37 >= 0 )
{
v30 = (unsigned int)(v37 - 1);
for ( i = 0LL; ; ++i )
{
if ( *(_BYTE *)(*((_QWORD *)v16 + 4) + i + v16[13]) == 3 )
fwrite("found <CTRL>-<C> in data!\n", 1uLL, 0x1AuLL, stderr);
if ( i == v30 )
break;
}
}
else if ( *_errno_location() != 4 )
{
goto LABEL_68;
}
v32 = v16[13] + v38;
v16[15] += v38;
if ( v32 == 4000 )
v32 = 0;
v16[13] = v32;
}
}
if ( !v16[12] )
{
v16[10] = 0;
v16[11] = 0;
}
goto LABEL_105;
}
}
addr_len = 16;
v13 = accept(fd, &addr, &addr_len);
if ( v13 >= 0 )
{
openlog(ident, 8, 24);
v14 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
syslog(6, "connection from: %s\n", v14);
closelog();
v15 = make_new_session(v13);
if ( v15 )
{
*v15 = sessions;
sessions = v15;
if ( maxfd < v13 )
maxfd = v13;
}
else
{
close(v13);
}
goto LABEL_49;
}
}
}
perror("listen");
result = 1;
}
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()
{
char *v0; // rbx
unsigned int v1; // ebp
__int64 result; // rax

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

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

glPixelStorei(3317LL, 1LL);
glPixelStorei(3333LL, 1LL);
v0 = (char *)&font_data;
v1 = 31;
do
{
glNewList(v1, 4864LL);
glBitmap(6LL, 13LL, v0, 0.0, 0.0, 6.0, 0.0);
result = glEndList();
++v1;
v0 += 13;
}
while ( v0 != (char *)&unk_740 );
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
char v5; // di

v3 = a1;
result = glRasterPos2i(a2, a3);
v5 = *a1;
if ( v5 > 0 )
{
do
{
++v3;
result = glCallList((unsigned int)(v5 + 31));
v5 = *v3;
}
while ( *v3 > 0 );
}
return result;
}

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

v3 = a1;
result = glRasterPos2i(a2, a3);
v5 = *a1;
if ( v5 > 0 )
{
do
{
++v3;
result = glCallList((unsigned int)(v5 + 31));
v5 = *v3;
}
while ( *v3 > 0 );
}
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 func2(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; // rdx
__int64 v1; // rdi
char *result; // rax
char v3; // si

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

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

v0 = bullet_free;
v1 = qword_748;
result = (char *)&bullet_pool;
v3 = 0;
do
{
if ( v0 )
{
*(_QWORD *)(v0 + 8) = result;
*(_QWORD *)result = v0;
}
else
{
v1 = (__int64)result;
*(_QWORD *)result = 0LL;
v3 = 1;
}
v0 = (__int64)result;
*((_QWORD *)result + 1) = 0LL;
result += 72;
}
while ( result != "Too many bullets..." );
bullet_free = (__int64)&unk_24718;
if ( v3 )
qword_748 = 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; // ebp
unsigned int v1; // ebx
__m128i v2; // xmm1
__int64 result; // rax

v0 = SDL_GetTicks();
v1 = v0 - oldtime;
v2 = 0LL;
*(float *)v2.m128i_i32 = (float)(v0 - oldtime) * 0.001;
noob_update_all((unsigned int)(v0 - oldtime), &kgtd_state, *(double *)v2.m128i_i64);
bullet_update_all(v1, *(double *)_mm_cvtsi32_si128(_mm_cvtsi128_si32(v2)).m128i_i64);
tower_update_all(v1);
controls_update(v1, &kgtd_state);
result = level_update(v1, &kgtd_state);
oldtime = v0;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // ebp
unsigned int v1; // ebx
__m128i v2; // xmm1
__int64 result; // rax

v0 = SDL_GetTicks();
v1 = v0 - oldtime;
v2 = 0LL;
*(float *)v2.m128i_i32 = (float)(v0 - oldtime) * 0.001;
noob_update_all((unsigned int)(v0 - oldtime), &kgtd_state, *(double *)v2.m128i_i64);
bullet_update_all(v1, *(double *)_mm_cvtsi32_si128(_mm_cvtsi128_si32(v2)).m128i_i64);
tower_update_all(v1);
controls_update(v1, &kgtd_state);
result = level_update(v1, &kgtd_state);
oldtime = v0;
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 func1(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; // rdx
__int64 v1; // rdi
char *result; // rax
char v3; // si

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

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

v0 = noob_free;
v1 = qword_5B8;
result = (char *)&noobs_pool;
v3 = 0;
do
{
if ( v0 )
{
*(_QWORD *)(v0 + 8) = result;
*(_QWORD *)result = v0;
}
else
{
v1 = (__int64)result;
*(_QWORD *)result = 0LL;
v3 = 1;
}
v0 = (__int64)result;
*((_QWORD *)result + 1) = 0LL;
result += 64;
}
while ( result != "Too many noobs..." );
noob_free = (__int64)&unk_10580;
if ( v3 )
qword_5B8 = 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
_QWORD *v3; // 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 = *(_QWORD **)(a1 + 8);
if ( v3 )
*v3 = *(_QWORD *)a1;
else
noob_list = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)a1 + 8LL) = *(_QWORD *)(a1 + 8);
else
*(&noob_list + 1) = *(_QWORD *)(a1 + 8);
result = noob_free;
if ( noob_free )
{
*(_QWORD *)(noob_free + 8) = a1;
*(_QWORD *)a1 = result;
}
else
{
qword_5B8 = a1;
*(_QWORD *)a1 = 0LL;
}
noob_free = a1;
*(_QWORD *)(a1 + 8) = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v1; // eax
__int64 result; // rax
_QWORD *v3; // 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 = *(_QWORD **)(a1 + 8);
if ( v3 )
*v3 = *(_QWORD *)a1;
else
noob_list = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)a1 + 8LL) = *(_QWORD *)(a1 + 8);
else
*(&noob_list + 1) = *(_QWORD *)(a1 + 8);
result = noob_free;
if ( noob_free )
{
*(_QWORD *)(noob_free + 8) = a1;
*(_QWORD *)a1 = result;
}
else
{
qword_5B8 = a1;
*(_QWORD *)a1 = 0LL;
}
noob_free = a1;
*(_QWORD *)(a1 + 8) = 0LL;
}
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
{
func2((__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; // rbp
float v5; // xmm0_4
float v7; // [rsp+Ch] [rbp-2Ch]

v2 = tt_data[16 * (unsigned __int64)a2 + 8];
v7 = v2 * v2;
v3 = noob_list;
if ( !noob_list )
return noob_list;
v4 = 0LL;
do
{
if ( !(unsigned int)damage_not_worthwhile(v3, a2) )
{
v5 = distance2(a1, (float *)(v3 + 24));
if ( v7 > v5 )
v4 = v3;
v7 = fminf(v5, 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; // rbp
float v5; // xmm0_4
float v7; // [rsp+Ch] [rbp-2Ch]

v2 = tt_data[16 * (unsigned __int64)a2 + 8];
v7 = v2 * v2;
v3 = noob_list;
if ( !noob_list )
return noob_list;
v4 = 0LL;
do
{
if ( !(unsigned int)damage_not_worthwhile(v3, a2) )
{
v5 = func0(a1, (float *)(v3 + 24));
if ( v7 > v5 )
v4 = v3;
v7 = fminf(v5, 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_638 = 0LL;
}

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

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


decompiled_code:
float *__fastcall tower_new(int a1, int a2, int a3, unsigned int a4)
{
float *result; // rax
unsigned __int64 v5; // rcx
__int64 v6; // r8

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

canonical_decompiled_code:
float *__fastcall func3(int a1, int a2, int a3, unsigned int a4)
{
float *result; // rax
unsigned __int64 v5; // rcx
__int64 v6; // r8

if ( *((_DWORD *)&grid + 16 * (a1 + 25LL * a2)) )
return 0LL;
result = (float *)((char *)&grid + 64 * (a1 + 25LL * a2));
*(_DWORD *)result = 1;
result[1] = (float)(32 * a1 + 16);
result[2] = (float)(32 * a2 + 16);
*((_DWORD *)result + 3) = a3;
result[4] = 0.0;
*((_DWORD *)result + 5) = 100000;
*((_DWORD *)result + 7) = a4;
v5 = (unsigned __int64)a4 << 6;
result[6] = *(float *)((char *)&tt_data[8] + v5) * *(float *)((char *)&tt_data[8] + v5);
*((_QWORD *)result + 5) = *(_QWORD *)((char *)&tt_data[6] + v5);
*((_QWORD *)result + 4) = 0LL;
v6 = tower_list;
if ( tower_list )
{
*(_QWORD *)(tower_list + 56) = result;
*((_QWORD *)&grid + 8 * (25LL * a2 + a1) + 6) = v6;
}
else
{
qword_638 = (__int64)&grid + 64 * (a1 + 25LL * a2);
*((_QWORD *)&grid + 8 * (25LL * a2 + a1) + 6) = 0LL;
}
tower_list = (__int64)&grid + 64 * (a1 + 25LL * a2);
*((_QWORD *)&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; }


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 }


decompiled_code:
__int64 __fastcall tower_upd_normal(__int64 a1, int a2)
{
__int64 v2; // rbp
unsigned int v3; // eax
__int64 result; // rax
__int64 v5; // rsi

v2 = *(_QWORD *)(a1 + 32);
if ( v2 )
{
if ( !(unsigned int)damage_not_worthwhile(*(_QWORD *)(a1 + 32), *(unsigned int *)(a1 + 28))
&& distance2((float *)(a1 + 4), (float *)(v2 + 24)) <= *(float *)(a1 + 24) )
{
v3 = *(_DWORD *)(a1 + 16);
if ( v3 < *(_DWORD *)(a1 + 20) )
goto LABEL_6;
LABEL_10:
v5 = *(unsigned int *)(a1 + 20);
*(_DWORD *)(a1 + 16) -= v5;
result = bullet_new(a1 + 4, v5, *(unsigned int *)(a1 + 28), v2);
goto LABEL_11;
}
noob_ref_destroy(v2);
}
v3 = *(_DWORD *)(a1 + 16);
v2 = 0LL;
if ( v3 >= *(_DWORD *)(a1 + 20) )
{
result = noob_find_target(a1 + 4, *(unsigned int *)(a1 + 28));
v2 = result;
if ( !result )
goto LABEL_11;
goto LABEL_10;
}
LABEL_6:
result = *(_DWORD *)(a1 + 12) * a2 + v3;
*(_DWORD *)(a1 + 16) = result;
LABEL_11:
*(_QWORD *)(a1 + 32) = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
__int64 v2; // rbp
unsigned int v3; // eax
__int64 result; // rax
__int64 v5; // rsi

v2 = *(_QWORD *)(a1 + 32);
if ( v2 )
{
if ( !(unsigned int)damage_not_worthwhile(*(_QWORD *)(a1 + 32), *(unsigned int *)(a1 + 28))
&& func0((float *)(a1 + 4), (float *)(v2 + 24)) <= *(float *)(a1 + 24) )
{
v3 = *(_DWORD *)(a1 + 16);
if ( v3 < *(_DWORD *)(a1 + 20) )
goto LABEL_6;
LABEL_10:
v5 = *(unsigned int *)(a1 + 20);
*(_DWORD *)(a1 + 16) -= v5;
result = bullet_new(a1 + 4, v5, *(unsigned int *)(a1 + 28), v2);
goto LABEL_11;
}
noob_ref_destroy(v2);
}
v3 = *(_DWORD *)(a1 + 16);
v2 = 0LL;
if ( v3 >= *(_DWORD *)(a1 + 20) )
{
result = noob_find_target(a1 + 4, *(unsigned int *)(a1 + 28));
v2 = result;
if ( !result )
goto LABEL_11;
goto LABEL_10;
}
LABEL_6:
result = *(_DWORD *)(a1 + 12) * a2 + v3;
*(_DWORD *)(a1 + 16) = result;
LABEL_11:
*(_QWORD *)(a1 + 32) = v2;
return result;
}

original_code:
void tower_upd_normal(tower_t *tower, int dt)
{
 noob_t *target = tower->target;
 if(target != ((void *)0) && (damage_not_worthwhile(target, tower->type) ||
                       distance2(&tower->pos, &target->pos) > tower->range)) {
  noob_ref_destroy(target);
  target = ((void *)0);
 }
 if(tower->energy < tower->energymax) {
  tower->energy += tower->power * dt;
  goto out;
 }
 if(target == ((void *)0)) {
  target = noob_find_target(&tower->pos, tower->type);
  if(target == ((void *)0))
   goto out;
 }
 tower->energy -= tower->energymax;
 bullet_new(&tower->pos, tower->energymax, tower->type, target);
out:
 tower->target = target;
 return;
}

canonical_original_code:
void tower_upd_normal(struct tower_t *tower, int dt)
{
 struct noob_t *target = tower->target;
 if(target != ((void *)0) && (damage_not_worthwhile(target, tower->type) ||
                       distance2(&tower->pos, &target->pos) > tower->range)) {
  noob_ref_destroy(target);
  target = ((void *)0);
 }
 if(tower->energy < tower->energymax) {
  tower->energy += tower->power * dt;
  goto out;
 }
 if(target == ((void *)0)) {
  target = noob_find_target(&tower->pos, tower->type);
  if(target == ((void *)0))
   goto out;
 }
 tower->energy -= tower->energymax;
 bullet_new(&tower->pos, tower->energymax, tower->type, target);
out:
 tower->target = target;
 return;
}


decompiled_code:
__int64 __fastcall tower_update_all(unsigned int a1)
{
__int64 i; // rbx
__int64 result; // rax

for ( i = tower_list; i; i = *(_QWORD *)(i + 48) )
result = (*(__int64 (__fastcall **)(__int64, _QWORD))(i + 40))(i, a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
__int64 i; // rbx
__int64 result; // rax

for ( i = tower_list; i; i = *(_QWORD *)(i + 48) )
result = (*(__int64 (__fastcall **)(__int64, _QWORD))(i + 40))(i, a1);
return result;
}

original_code:
void tower_update_all(int dt)
{
 tower_t *cur;
 for(cur = (&tower_list)->head; cur; cur = (cur)->list.next)
  cur->update(cur, dt);
}

canonical_original_code:
void tower_update_all(int dt)
{
 struct tower_t *cur;
 for(cur = (&tower_list)->head; cur; cur = (cur)->list.next)
  cur->update(cur, dt);
}


decompiled_code:
__int64 tower_draw_all()
{
__int64 i; // rbx
__int64 result; // rax

for ( i = tower_list; i; i = *(_QWORD *)(i + 48) )
result = draw_each(i);
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 i; // rbx
__int64 result; // rax

for ( i = tower_list; i; i = *(_QWORD *)(i + 48) )
result = func1(i);
return result;
}

original_code:
void tower_draw_all()
{
 tower_t *cur;
 for(cur = (&tower_list)->head; cur; cur = (cur)->list.next)
  draw_each(cur);
}

canonical_original_code:
void tower_draw_all()
{
 struct tower_t *cur;
 for(cur = (&tower_list)->head; cur; cur = (cur)->list.next)
  draw_each(cur);
}


decompiled_code:
_DWORD *grid_init()
{
unsigned __int64 v0; // rdx
_DWORD *result; // rax

v0 = (unsigned int)((_DWORD)&grid + 1600);
do
{
result = (_DWORD *)(v0 - 1600);
do
{
*result = 0;
result += 16;
}
while ( result != (_DWORD *)v0 );
v0 += 1600LL;
}
while ( v0 != (_DWORD)&grid + 32000 );
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
unsigned __int64 v0; // rdx
_DWORD *result; // rax

v0 = (unsigned int)((_DWORD)&grid + 1600);
do
{
result = (_DWORD *)(v0 - 1600);
do
{
*result = 0;
result += 16;
}
while ( result != (_DWORD *)v0 );
v0 += 1600LL;
}
while ( v0 != (_DWORD)&grid + 32000 );
return result;
}

variable_types: i int

variable_types: j int


decompiled_code:
__int64 init_prelight()
{
int i; // ebx
__m128i v1; // xmm4
unsigned int v2; // er14
int j; // ebx
__m128i v4; // xmm5
float v5; // er14
int k; // ebx
float v7; // xmm0_4
double v8; // xmm0_8
float v9; // xmm1_4
int l; // ebx
float v11; // xmm0_4
float cosx; // [rsp+8h] [rbp-20h] BYREF
float sinx[7]; // [rsp+Ch] [rbp-1Ch] BYREF

glNewList(19LL, 4864LL);
glColor4f(1.0, 1.0, 1.0);
glBegin(1LL);
for ( i = -64; i != 128; i += 32 )
{
v1 = 0LL;
*(float *)v1.m128i_i32 = (float)i;
v2 = _mm_cvtsi128_si32(v1);
glVertex2f(*(double *)v1.m128i_i64, -96.0);
glVertex2f(*(double *)_mm_cvtsi32_si128(v2).m128i_i64, 128.0);
}
for ( j = -64; j != 128; j += 32 )
{
v4 = 0LL;
*(float *)v4.m128i_i32 = (float)j;
v5 = COERCE_FLOAT(_mm_cvtsi128_si32(v4));
glVertex2f(COERCE_DOUBLE((unsigned __int64)LC1), (float)j);
glVertex2f(COERCE_DOUBLE((unsigned __int64)LC2), v5);
}
((void (*)(void))glEnd)();
glEndList();
glNewList(20LL, 4864LL);
glBegin(6LL);
glColor4f(0.0, 0.0, 0.0);
glVertex2f(0.0, 0.0);
glColor4f(0.0, 0.0, 0.0);
for ( k = 0; k != 65; ++k )
{
v7 = (double)k * 0.09817477042468103;
sincosf(v7, sinx, &cosx);
*(_QWORD *)&v8 = (unsigned int)LC5;
*(float *)&v8 = 96.0 * sinx[0];
v9 = 96.0 * cosx;
glVertex2f(v8, 96.0 * cosx);
}
glEnd(*(float *)&v8, v9, 0.0, COERCE_DOUBLE((unsigned __int64)LC0));
glEndList();
glNewList(21LL, 4864LL);
for ( l = 0; l != 65; ++l )
{
v11 = (double)l * 0.09817477042468103;
sincosf(v11, sinx, &cosx);
glVertex2f(COERCE_DOUBLE((unsigned __int64)LODWORD(sinx[0])), cosx);
}
glEndList();
glNewList(22LL, 4864LL);
glBegin(6LL);
glVertex3f(0.0, 0.0);
glColor4f(0.0, 0.0, 0.0);
glCallList(21LL);
glEnd(0.0, 0.0, 0.0, 0.0);
return glEndList();
}

canonical_decompiled_code:
__int64 func0()
{
int i; // ebx
__m128i v1; // xmm4
unsigned int v2; // er14
int j; // ebx
__m128i v4; // xmm5
float v5; // er14
int k; // ebx
float v7; // xmm0_4
double v8; // xmm0_8
float v9; // xmm1_4
int l; // ebx
float v11; // xmm0_4
float cosx; // [rsp+8h] [rbp-20h] BYREF
float sinx[7]; // [rsp+Ch] [rbp-1Ch] BYREF

glNewList(19LL, 4864LL);
glColor4f(1.0, 1.0, 1.0);
glBegin(1LL);
for ( i = -64; i != 128; i += 32 )
{
v1 = 0LL;
*(float *)v1.m128i_i32 = (float)i;
v2 = _mm_cvtsi128_si32(v1);
glVertex2f(*(double *)v1.m128i_i64, -96.0);
glVertex2f(*(double *)_mm_cvtsi32_si128(v2).m128i_i64, 128.0);
}
for ( j = -64; j != 128; j += 32 )
{
v4 = 0LL;
*(float *)v4.m128i_i32 = (float)j;
v5 = COERCE_FLOAT(_mm_cvtsi128_si32(v4));
glVertex2f(COERCE_DOUBLE((unsigned __int64)LC1), (float)j);
glVertex2f(COERCE_DOUBLE((unsigned __int64)LC2), v5);
}
((void (*)(void))glEnd)();
glEndList();
glNewList(20LL, 4864LL);
glBegin(6LL);
glColor4f(0.0, 0.0, 0.0);
glVertex2f(0.0, 0.0);
glColor4f(0.0, 0.0, 0.0);
for ( k = 0; k != 65; ++k )
{
v7 = (double)k * 0.09817477042468103;
sincosf(v7, sinx, &cosx);
*(_QWORD *)&v8 = (unsigned int)LC5;
*(float *)&v8 = 96.0 * sinx[0];
v9 = 96.0 * cosx;
glVertex2f(v8, 96.0 * cosx);
}
glEnd(*(float *)&v8, v9, 0.0, COERCE_DOUBLE((unsigned __int64)LC0));
glEndList();
glNewList(21LL, 4864LL);
for ( l = 0; l != 65; ++l )
{
v11 = (double)l * 0.09817477042468103;
sincosf(v11, sinx, &cosx);
glVertex2f(COERCE_DOUBLE((unsigned __int64)LODWORD(sinx[0])), cosx);
}
glEndList();
glNewList(22LL, 4864LL);
glBegin(6LL);
glVertex3f(0.0, 0.0);
glColor4f(0.0, 0.0, 0.0);
glCallList(21LL);
glEnd(0.0, 0.0, 0.0, 0.0);
return glEndList();
}