decompiled_code:
_DWORD *read_intern()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x10uLL);
v1 = v0;
switch ( currentToken )
{
case 4:
*v0 = 4;
v0[1] = intres;
parseToken();
break;
case 5:
*v0 = 5;
*((_QWORD *)v0 + 1) = idres;
parseToken();
break;
case 2:
*v0 = 1;
parseToken();
*((_QWORD *)v1 + 1) = readList();
parseToken();
break;
default:
fprintf(stderr, "unexpected error %i\n", (unsigned int)currentToken);
exit(-1);
}
return v1;
}

canonical_decompiled_code:
_DWORD *func16()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x10uLL);
v1 = v0;
switch ( currentToken )
{
case 4:
*v0 = 4;
v0[1] = intres;
func15();
break;
case 5:
*v0 = 5;
*((_QWORD *)v0 + 1) = idres;
func15();
break;
case 2:
*v0 = 1;
func15();
*((_QWORD *)v1 + 1) = func17();
func15();
break;
default:
fprintf(stderr, "unexpected error %i\n", (unsigned int)currentToken);
exit(-1);
}
return v1;
}

original_code:
union elem *read_intern()
{
        union elem *res = (union elem *) malloc(sizeof(union elem));
        switch (currentToken) {
        case 4:
                res->type = 4;
                res->elemInt.value = intres;
                parseToken();
                break;
                return res;
        case 2:
                res->type = 1;
                parseToken();
                res->elemList.value = readList();
                parseToken();
                break;
                return res;
        case 5:
                res->type = 5;
                res->elemID.value = idres;
                parseToken();
                break;
                return res;
        default:
                fprintf(stderr, "unexpected error %i\n", currentToken);
                exit(-1);
        }
        return res;
}

return_type: union elem { int type; struct elemInt { int type; int value; } elemInt; struct elemList { int type; struct list { union elem *elem; struct list *next; } *value; } elemList; struct elemID { int type; char *value; } elemID; } *


readList

repo: neosam/mylisp

decompiled_code:
__int64 readList()
{
__int64 i; // rbx
_DWORD *v1; // rax

for ( i = 0LL; currentToken != 3; i = appendList(i, v1) )
v1 = read_intern();
return i;
}

canonical_decompiled_code:
__int64 func17()
{
__int64 i; // rbx
_DWORD *v1; // rax

for ( i = 0LL; currentToken != 3; i = func23(i, v1) )
v1 = func16();
return i;
}

original_code:
struct list *readList()
{
        struct list *res = ((void *)0);
        while (1) {
                switch (currentToken) {
                case 3:
                        return res;
                default:
                        res = appendList(res, read_intern());
                        break;
                }
        }
}

variable_types: struct list { union elem { int type; struct elemInt { int type; int value; } elemInt; struct elemList { int type; struct list *value; } elemList; struct elemID { int type; char *value; } elemID; } *elem; struct list *next; } *

user_defined_types: struct list { union elem *elem; struct list *next; }

user_defined_types: union elem { int type; struct elemInt elemInt; struct elemList elemList; struct elemID elemID; }


read

decompiled_code:
_DWORD *read()
{
parseToken();
return read_intern();
}

canonical_decompiled_code:
_DWORD *func18()
{
func15();
return func16();
}

original_code:
union elem *read()
{
        parseToken();
        return read_intern();
}

user_defined_types: struct elemList { int type; struct list *value; }

user_defined_types: struct elemID { int type; char *value; }


evalList

decompiled_code:
void evalList()
{
;
}

canonical_decompiled_code:
void func19()
{
;
}

original_code:
union elem *evalList(union elem *elem, struct state *state)
{
}

variable_types:
elem
union elem *

variable_types: struct state { union stateEntry { struct stateInfo { int type; char *id; } stateInfo; struct stateBuildin { int type; char *id; union elem *(*func)(struct list *lst); } stateBuildin; struct stateVar { int type; char *id; union elem *elem; } stateVar; } *entries; struct state *prev; } *

user_defined_types: struct state { union stateEntry *entries; struct state *prev; }

user_defined_types: union stateEntry { struct stateInfo stateInfo; struct stateBuildin stateBuildin; struct stateVar stateVar; }


lastList

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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 8);
}
while ( a1 );
return result;
}

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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 8);
}
while ( a1 );
return result;
}

original_code:
struct list *lastList(struct list *list)
{
        struct list *res = list;
        while (res->next != ((void *)0))
            res = res->next;
        return res;
}

variable_types:
list
struct list *

return_type: struct list { union elem { int type; struct elemInt { int type; int value; } elemInt; struct elemList { int type; struct list *value; } elemList; struct elemID { int type; char *value; } elemID; } *elem; struct list *next; } *


print

decompiled_code:
int __fastcall print(__int64 a1)
{
int result; // eax

result = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 4 )
return printf("%i", *(unsigned int *)(a1 + 4));
if ( result == 5 )
return printf("%s", *(const char **)(a1 + 8));
if ( result == 1 )
result = printList(*(_QWORD *)(a1 + 8));
return result;
}

canonical_decompiled_code:
int __fastcall func24(__int64 a1)
{
int result; // eax

result = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 4 )
return printf("%i", *(unsigned int *)(a1 + 4));
if ( result == 5 )
return printf("%s", *(const char **)(a1 + 8));
if ( result == 1 )
result = func25(*(_QWORD *)(a1 + 8));
return result;
}

original_code:
void print(union elem *elem)
{
        switch (elem->type) {
        case 1:
                printList(elem->elemList.value);
                break;
        case 4:
                printf("%i", elem->elemInt.value);
                break;
        case 5:
                printf("%s", elem->elemID.value);
                break;
        }
}

variable_types: union elem { int type; struct elemInt { int type; int value; } elemInt; struct elemList { int type; struct list { union elem *elem; struct list *next; } *value; } elemList; struct elemID { int type; char *value; } elemID; } *

return_type: void


printList

decompiled_code:
int __fastcall printList(__int64 *a1)
{
__int64 *i; // rbx
int result; // eax

if ( !a1 )
return printf("()");
if ( a1[1] )
{
fputc(40, stdout);
print(*a1);
for ( i = (__int64 *)a1[1]; i; i = (__int64 *)i[1] )
{
fputc(32, stdout);
print(*i);
fflush(stdout);
}
result = fputc(41, stdout);
}
else
{
putchar(40);
print(*a1);
result = putchar(41);
}
return result;
}

canonical_decompiled_code:
int __fastcall func25(__int64 *a1)
{
__int64 *i; // rbx
int result; // eax

if ( !a1 )
return printf("()");
if ( a1[1] )
{
fputc(40, stdout);
func24(*a1);
for ( i = (__int64 *)a1[1]; i; i = (__int64 *)i[1] )
{
fputc(32, stdout);
func24(*i);
fflush(stdout);
}
result = fputc(41, stdout);
}
else
{
putchar(40);
func24(*a1);
result = putchar(41);
}
return result;
}

original_code:
void printList(struct list *list)
{
        if (list == ((void *)0)) {
                printf("()");
                return;
        }
        if (list->next == ((void *)0)) {
                printf("(");
                print(list->elem);
                printf(")");
                return;
        }
        fputc('(', stdout);
        print(list->elem);
        list = list->next;
        while (list != ((void *)0)) {
                fputc(' ', stdout);
                print(list->elem);
                fflush(stdout);
                list = list->next;
        }
        fputc(')', stdout);
}


dummyWrite

repo: netdata/libnodave

decompiled_code:
void *__fastcall dummyWrite(unsigned __int8 a1, unsigned int a2, int a3, unsigned int a4, _DWORD *a5, const void *a6)
{
int v6; // ebx
int v10; // edx
unsigned int v11; // ebx
const char *v12; // rax

v6 = a3;
v10 = a3 + 7;
if ( v6 < 0 )
v6 = v10;
v11 = v6 >> 3;
v12 = (const char *)daveAreaName(a1);
printf("User write callback should write %d bytes from %s %d to address %d.\n", a4, v12, a2, v11);
*a5 = 0;
return memcpy(&dummyRes[v11], a6, (int)a4);
}

canonical_decompiled_code:
void *__fastcall func2(unsigned __int8 a1, unsigned int a2, int a3, unsigned int a4, _DWORD *a5, const void *a6)
{
int v6; // ebx
int v10; // edx
unsigned int v11; // ebx
const char *v12; // rax

v6 = a3;
v10 = a3 + 7;
if ( v6 < 0 )
v6 = v10;
v11 = v6 >> 3;
v12 = (const char *)daveAreaName(a1);
printf("User write callback should write %d bytes from %s %d to address %d.\n", a4, v12, a2, v11);
*a5 = 0;
return memcpy(&dummyRes[v11], a6, (int)a4);
}

original_code:
void dummyWrite(int area, int DBnumber, int start, int len, int * res, unsigned char*buffer) {
    start/=8;
    printf("User write callback should write %d bytes from %s %d to address %d.\n",
 len, daveAreaName(area),DBnumber,start);
    *res=0;
    memcpy(dummyRes+start,buffer,len);
}

canonical_original_code:
void dummyWrite(int area, int DBnumber, int start, int len, int *res, unsigned char *buffer) {
    start/=8;
    printf("User write callback should write %d bytes from %s %d to address %d.\n",
 len, daveAreaName(area),DBnumber,start);
    *res=0;
    memcpy(dummyRes+start,buffer,len);
}

variable_types: area int

variable_types: DBnumber int

variable_types: start int

variable_types: len int

variable_types:
res
int *

variable_types:
buffer
unsigned char *


accepter

decompiled_code:
void __fastcall __noreturn accepter(void *a1)
{
int v1; // er12
int v2; // ebp
int *v3; // rbx
char *v4; // rax
char *v5; // rax
char *v6; // rax
char *v7; // r13
unsigned int v8; // eax
char *v9; // rax
int optval; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-48h] BYREF
socklen_t addr_len; // [rsp+28h] [rbp-30h] BYREF
unsigned int buf[11]; // [rsp+2Ch] [rbp-2Ch] BYREF

usleep(0x2710u);
v1 = *(_DWORD *)a1;
fprintf(stdout, "accepter.cAccepter : My pipe is:%d\n", *(unsigned int *)a1);
fflush(stdout);
v2 = socket(2, 1, 0);
v3 = _errno_location();
if ( *v3 )
{
v9 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : socket %s\n", v9);
fflush(stdout);
}
fprintf(stdout, "accepter.cAccepter : port %d\n", *((unsigned int *)a1 + 1));
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(*((_WORD *)a1 + 2));
inet_aton("0.0.0.0", (struct in_addr *)&addr.sa_data[2]);
optval = 1;
setsockopt(v2, 1, 2, &optval, 4u);
v4 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : setsockopt %s\n", v4);
addr_len = 16;
bind(v2, &addr, 0x10u);
v5 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : bind %s\n", v5);
listen(v2, 1);
v6 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : listen: %s\n", v6);
while ( 1 )
{
addr_len = 16;
fwrite("accepter.cAccepter : before accept\n", 1uLL, 0x23uLL, stdout);
buf[0] = accept(v2, &addr, &addr_len);
fprintf(stdout, "accepter.cAccepter: after accept. New fd:%d\n", buf[0]);
fflush(stdout);
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
v8 = getpid();
fprintf(stdout, "accepter.cAccepter: PID(%d) client:%s\n", v8, v7);
fflush(stdout);
write(v1, buf, 4uLL);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
int v1; // er12
int v2; // ebp
int *v3; // rbx
char *v4; // rax
char *v5; // rax
char *v6; // rax
char *v7; // r13
unsigned int v8; // eax
char *v9; // rax
int optval; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-48h] BYREF
socklen_t addr_len; // [rsp+28h] [rbp-30h] BYREF
unsigned int buf[11]; // [rsp+2Ch] [rbp-2Ch] BYREF

usleep(0x2710u);
v1 = *(_DWORD *)a1;
fprintf(stdout, "accepter.cAccepter : My pipe is:%d\n", *(unsigned int *)a1);
fflush(stdout);
v2 = socket(2, 1, 0);
v3 = _errno_location();
if ( *v3 )
{
v9 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : socket %s\n", v9);
fflush(stdout);
}
fprintf(stdout, "accepter.cAccepter : port %d\n", *((unsigned int *)a1 + 1));
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(*((_WORD *)a1 + 2));
inet_aton("0.0.0.0", (struct in_addr *)&addr.sa_data[2]);
optval = 1;
setsockopt(v2, 1, 2, &optval, 4u);
v4 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : setsockopt %s\n", v4);
addr_len = 16;
bind(v2, &addr, 0x10u);
v5 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : bind %s\n", v5);
listen(v2, 1);
v6 = strerror(*v3);
fprintf(stdout, "accepter.cAccepter : listen: %s\n", v6);
while ( 1 )
{
addr_len = 16;
fwrite("accepter.cAccepter : before accept\n", 1uLL, 0x23uLL, stdout);
buf[0] = accept(v2, &addr, &addr_len);
fprintf(stdout, "accepter.cAccepter: after accept. New fd:%d\n", buf[0]);
fflush(stdout);
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
v8 = getpid();
fprintf(stdout, "accepter.cAccepter: PID(%d) client:%s\n", v8, v7);
fflush(stdout);
write(v1, buf, 4uLL);
}
}

original_code:
void *accepter(void *arg)
{
    int s;
    int newfd, pipefd;
    socklen_t addrlen;
    struct sockaddr_in addr;
    int i;
    int opt;
    usleep(10000);
    pipefd = ((accepter_info *) arg)->fd;
    fprintf(stdout,"accepter.c" "Accepter : My pipe is:%d\n",pipefd);
    fflush(stdout);
    s = socket(2, SOCK_STREAM, 0);
    if ((*__errno_location ()) != 0) {
 fprintf(stdout,"accepter.c" "Accepter : socket %s\n",strerror((*__errno_location ())));
 fflush(stdout);
    }
    fprintf(stdout,"accepter.c" "Accepter : port %d\n",((accepter_info *) arg)->port);
    addr.sin_family = 2;
    addr.sin_port =htons(((accepter_info *) arg)->port);
    inet_aton("0.0.0.0", &addr.sin_addr);
    opt = 1;
    i = setsockopt(s, 1, 2, &opt, 4);
    fprintf(stdout,"accepter.c" "Accepter : setsockopt %s\n",strerror((*__errno_location ())));
    addrlen = sizeof(addr);
    bind(s, (struct sockaddr *) & addr, addrlen);
    fprintf(stdout,"accepter.c" "Accepter : bind %s\n",strerror((*__errno_location ())));
    listen(s, 1);
    fprintf(stdout,"accepter.c" "Accepter : listen: %s\n",strerror((*__errno_location ())));
    while (1) {
 addrlen = sizeof(addr);
 fprintf(stdout,"accepter.c" "Accepter : before accept\n");
 newfd = (accept(s, (struct sockaddr *) & addr, &addrlen));
 fprintf(stdout,"accepter.c" "Accepter: after accept. New fd:%d\n",newfd);
 fflush(stdout);
 fprintf(stdout,"accepter.c" "Accepter: PID(%d) client:%s\n",getpid(),inet_ntoa(addr.sin_addr));
 fflush(stdout);
 write(pipefd, &newfd, sizeof(newfd));
    }
    shutdown(s, 2);
    fprintf(stdout,"accepter.c" "Accepter : %d shutdown done. Socket is: %d\n",getpid(),s);
}

canonical_original_code:
void *accepter(void *arg)
{
    int s;
    int newfd; int pipefd;
    unsigned int addrlen;
    struct sockaddr_in addr;
    int i;
    int opt;
    usleep(10000);
    pipefd = ((struct accepter_info *) arg)->fd;
    fprintf(stdout,"accepter.c" "Accepter : My pipe is:%d\n",pipefd);
    fflush(stdout);
    s = socket(2, SOCK_STREAM, 0);
    if ((*__errno_location ()) != 0) {
 fprintf(stdout,"accepter.c" "Accepter : socket %s\n",strerror((*__errno_location ())));
 fflush(stdout);
    }
    fprintf(stdout,"accepter.c" "Accepter : port %d\n",((struct accepter_info *) arg)->port);
    addr.sin_family = 2;
    addr.sin_port =htons(((struct accepter_info *) arg)->port);
    inet_aton("0.0.0.0", &addr.sin_addr);
    opt = 1;
    i = setsockopt(s, 1, 2, &opt, 4);
    fprintf(stdout,"accepter.c" "Accepter : setsockopt %s\n",strerror((*__errno_location ())));
    addrlen = sizeof(addr);
    bind(s, (struct sockaddr *) & addr, addrlen);
    fprintf(stdout,"accepter.c" "Accepter : bind %s\n",strerror((*__errno_location ())));
    listen(s, 1);
    fprintf(stdout,"accepter.c" "Accepter : listen: %s\n",strerror((*__errno_location ())));
    while (1) {
 addrlen = sizeof(addr);
 fprintf(stdout,"accepter.c" "Accepter : before accept\n");
 newfd = (accept(s, (struct sockaddr *) & addr, &addrlen));
 fprintf(stdout,"accepter.c" "Accepter: after accept. New fd:%d\n",newfd);
 fflush(stdout);
 fprintf(stdout,"accepter.c" "Accepter: PID(%d) client:%s\n",getpid(),inet_ntoa(addr.sin_addr));
 fflush(stdout);
 write(pipefd, &newfd, sizeof(newfd));
    }
    shutdown(s, 2);
    fprintf(stdout,"accepter.c" "Accepter : %d shutdown done. Socket is: %d\n",getpid(),s);
}

variable_types:
arg
void *

variable_types: s int

variable_types: newfd int

variable_types: pipefd int

variable_types:
addrlen
unsigned 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: i int

variable_types: opt int

return_type: void *

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 accepter_info { int fd; int port; }

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


handleSystemMessage

variable_types: struct PDU { unsigned char *header; unsigned char *param; unsigned char *data; unsigned char *udata; int hlen; int plen; int dlen; int udlen; } *

variable_types: number int

variable_types: count int

variable_types:
pa
unsigned char []

variable_types:
va
unsigned char []

return_type: int


analyze

original_code:
void analyze(daveConnection * dc) {
    ISOpacket * p,* p2;
    unsigned char resp[2000];
    unsigned char r5[]={
     0x03,0x00,0x00,0x16,
     0x11,0xd0,
     0,0,0,1,0,
     0xc0,1,9,
     0x0c1,2,1,2,
     0x0c2,2,1,0,
     };
    int haveResp=0;
    PDU p1,pr;
    p= (ISOpacket*) dc->msgIn;
    dc->needAckNumber=-1;
    printf("Protocol: %d\n",p->ch1);
    printf("Channel: %d\n",p->ch1);
    printf("Channel: %d\n",p->ch2);
    printf("Length:  %d\n",p->len);
    printf("(MPI) func:%d\n",p->func);
    if (p->func==0xf0) {
        printf("0xf0:PDU transport\n");
 dc->PDUstartI=sizeof(ISOpacket);
        _daveSetupReceivedPDU(dc, &p1);
        _daveDumpPDU(&p1);
        pr.header=resp+sizeof(ISOpacket);
        p2= (ISOpacket*) resp;
        p2->ch1=p->ch2;
        p2->ch2=p->ch1;
        if (p1.param[0]==0x04) {
     _daveHandleRead(&p1,&pr);
     haveResp=1;
     p2->len=pr.hlen+pr.plen+pr.dlen+7;
 } else if (p1.param[0]==0x05) {
     _daveHandleWrite(&p1,&pr);
     haveResp=1;
     p2->len=pr.hlen+pr.plen+pr.dlen+7;
 } else if (p1.param[0]==240) {
     printf("PDU function code: %d, negociate PDU len\n",p1.param[0]);
     _daveDump("packet:",dc->msgIn,dc->msgIn[3]);
     memcpy(resp, dc->msgIn, dc->msgIn[3]);
     resp[23]=960 / 0x100;
     resp[24]=960 % 0x100;
     haveResp=1;
 } else if (p1.param[0]==0) {
     printf("PDU function code: %d, system Message ?\n",p1.param[0]);
     haveResp=handleSystemMessage(&p1,&pr);
     pr.header[4]=p1.header[4];
     haveResp=1;
     p2->len=pr.hlen+pr.plen+pr.dlen+7;
 } else {
     printf("Unsupported PDU function code: %d\n",p1.param[0]);
 }
    }
    if (p->func==0xe0) {
 int rack=*(dc->msgIn+17)-1;
 int slot=*(dc->msgIn+18);
        printf("Connect to rack:%d slot:%d \n", rack, slot);
        *(r5+16)=rack+1;
 *(r5+17)=slot;
 memcpy(resp, r5, sizeof(r5));
        haveResp=1;
    }
    if (haveResp) {
 usleep(10000);
 write(dc->iface->fd.wfd,resp,resp[3]);
 _daveDump("I send:",resp,resp[3]);
    }
}

canonical_original_code:
void analyze(struct _daveConnection *dc) {
    struct ISOpacket *p; struct ISOpacket *p2;
    unsigned char resp[2000];
    unsigned char r5[] = {
     0x03,0x00,0x00,0x16,
     0x11,0xd0,
     0,0,0,1,0,
     0xc0,1,9,
     0x0c1,2,1,2,
     0x0c2,2,1,0,
     };
    int haveResp = 0;
    struct PDU p1; struct PDU pr;
    p= (struct ISOpacket *) dc->msgIn;
    dc->needAckNumber=-1;
    printf("Protocol: %d\n",p->ch1);
    printf("Channel: %d\n",p->ch1);
    printf("Channel: %d\n",p->ch2);
    printf("Length:  %d\n",p->len);
    printf("(MPI) func:%d\n",p->func);
    if (p->func==0xf0) {
        printf("0xf0:PDU transport\n");
 dc->PDUstartI=sizeof(ISOpacket);
        _daveSetupReceivedPDU(dc, &p1);
        _daveDumpPDU(&p1);
        pr.header=resp+sizeof(ISOpacket);
        p2= (struct ISOpacket *) resp;
        p2->ch1=p->ch2;
        p2->ch2=p->ch1;
        if (p1.param[0]==0x04) {
     _daveHandleRead(&p1,&pr);
     haveResp=1;
     p2->len=pr.hlen+pr.plen+pr.dlen+7;
 } else if (p1.param[0]==0x05) {
     _daveHandleWrite(&p1,&pr);
     haveResp=1;
     p2->len=pr.hlen+pr.plen+pr.dlen+7;
 } else if (p1.param[0]==240) {
     printf("PDU function code: %d, negociate PDU len\n",p1.param[0]);
     _daveDump("packet:",dc->msgIn,dc->msgIn[3]);
     memcpy(resp, dc->msgIn, dc->msgIn[3]);
     resp[23]=960 / 0x100;
     resp[24]=960 % 0x100;
     haveResp=1;
 } else if (p1.param[0]==0) {
     printf("PDU function code: %d, system Message ?\n",p1.param[0]);
     haveResp=handleSystemMessage(&p1,&pr);
     pr.header[4]=p1.header[4];
     haveResp=1;
     p2->len=pr.hlen+pr.plen+pr.dlen+7;
 } else {
     printf("Unsupported PDU function code: %d\n",p1.param[0]);
 }
    }
    if (p->func==0xe0) {
 int rack = *(dc->msgIn+17)-1;
 int slot = *(dc->msgIn+18);
        printf("Connect to rack:%d slot:%d \n", rack, slot);
        *(r5+16)=rack+1;
 *(r5+17)=slot;
 memcpy(resp, r5, sizeof(r5));
        haveResp=1;
    }
    if (haveResp) {
 usleep(10000);
 write(dc->iface->fd.wfd,resp,resp[3]);
 _daveDump("I send:",resp,resp[3]);
    }
}

variable_types: struct _daveConnection { int AnswLen; unsigned char *resultPointer; int maxPDUlength; int MPIAdr; struct _daveInterface { int timeout; struct dost { int rfd; int wfd; } fd; int localMPI; int users; char *name; int protocol; int speed; int ackPos; int nextConnection; int (*initAdapter)(struct _daveInterface *); int (*connectPLC)(struct _daveConnection *); int (*disconnectPLC)(struct _daveConnection *); int (*disconnectAdapter)(struct _daveInterface *); int (*exchange)(struct _daveConnection *, struct PDU { unsigned char *header; unsigned char *param; unsigned char *data; unsigned char *udata; int hlen; int plen; int dlen; int udlen; } *); int (*sendMessage)(struct _daveConnection *, struct PDU *); int (*getResponse)(struct _daveConnection *); int (*listReachablePartners)(struct _daveInterface *di, char *buf); char realName[20]; int (*ifread)(struct _daveInterface *, char *, int); int (*ifwrite)(struct _daveInterface *, char *, int); int seqNumber; } *iface; int needAckNumber; int PDUnumber; int ibhSrcConn; int ibhDstConn; unsigned char msgIn[2048]; unsigned char msgOut[2048]; unsigned char *_resultPointer; int PDUstartO; int PDUstartI; int rack; int slot; int connectionNumber; int connectionNumber2; unsigned char messageNumber; unsigned char packetNumber; void *hook; struct _daveS5cache { int PAE; int PAA; int flags; int timers; int counters; int systemData; struct _daveS5AreaInfo { int area; int DBnumber; int address; int len; struct _daveS5AreaInfo *next; } *first; } *cache; } *

variable_types: struct ISOpacket { unsigned char prot; unsigned char ch1; unsigned char ch2; unsigned char len; unsigned char xxxx1; unsigned char func; unsigned char xxxx2; } *

variable_types:
resp
unsigned char [2000]

variable_types: haveResp int

variable_types: rack int

variable_types: slot int

user_defined_types: struct _daveConnection { int AnswLen; unsigned char *resultPointer; int maxPDUlength; int MPIAdr; struct _daveInterface *iface; int needAckNumber; int PDUnumber; int ibhSrcConn; int ibhDstConn; unsigned char msgIn[2048]; unsigned char msgOut[2048]; unsigned char *_resultPointer; int PDUstartO; int PDUstartI; int rack; int slot; int connectionNumber; int connectionNumber2; unsigned char messageNumber; unsigned char packetNumber; void *hook; struct _daveS5cache *cache; }

user_defined_types: struct _daveInterface { int timeout; struct dost fd; int localMPI; int users; char *name; int protocol; int speed; int ackPos; int nextConnection; int (*initAdapter)(struct _daveInterface *); int (*connectPLC)(struct _daveConnection *); int (*disconnectPLC)(struct _daveConnection *); int (*disconnectAdapter)(struct _daveInterface *); int (*exchange)(struct _daveConnection *, struct PDU *); int (*sendMessage)(struct _daveConnection *, struct PDU *); int (*getResponse)(struct _daveConnection *); int (*listReachablePartners)(struct _daveInterface *di, char *buf); char realName[20]; int (*ifread)(struct _daveInterface *, char *, int); int (*ifwrite)(struct _daveInterface *, char *, int); int seqNumber; }

user_defined_types: struct _daveS5cache { int PAE; int PAA; int flags; int timers; int counters; int systemData; struct _daveS5AreaInfo *first; }


portServer

decompiled_code:
void *__fastcall portServer(void *a1)
{
_DWORD *v1; // rax
int *v2; // rbx
unsigned int v3; // ebp
int v4; // er13
unsigned int *v5; // r12
int v6; // eax

fprintf(stdout, "portServer: My fd is:%d\n", *(unsigned int *)a1);
fflush(stdout);
daveSetDebug(0LL);
v1 = (_DWORD *)daveNewInterface(
((unsigned __int64)*(unsigned int *)a1 << 32) | *(unsigned int *)a1,
"IF1",
0LL,
122LL,
2LL);
*v1 = 900000;
v2 = (int *)daveNewConnection(v1, 2LL, 0LL, 2LL);
v3 = 0;
v4 = 0;
v5 = (unsigned int *)(v2 + 12);
do
{
while ( 1 )
{
v6 = daveReadISOPacket(*((_QWORD *)v2 + 3), v5);
*v2 = v6;
if ( v6 <= 0 )
break;
fprintf(stdout, "%d ", v3);
daveDump("packet", v5, (unsigned int)*v2);
analyze((__int64)v2);
++v3;
v4 = 0;
}
++v4;
}
while ( v4 <= 99 );
fwrite("portServer: I closed my fd.\n", 1uLL, 0x1CuLL, stdout);
fflush(stdout);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
_DWORD *v1; // rax
int *v2; // rbx
unsigned int v3; // ebp
int v4; // er13
unsigned int *v5; // r12
int v6; // eax

fprintf(stdout, "portServer: My fd is:%d\n", *(unsigned int *)a1);
fflush(stdout);
daveSetDebug(0LL);
v1 = (_DWORD *)daveNewInterface(
((unsigned __int64)*(unsigned int *)a1 << 32) | *(unsigned int *)a1,
"IF1",
0LL,
122LL,
2LL);
*v1 = 900000;
v2 = (int *)daveNewConnection(v1, 2LL, 0LL, 2LL);
v3 = 0;
v4 = 0;
v5 = (unsigned int *)(v2 + 12);
do
{
while ( 1 )
{
v6 = daveReadISOPacket(*((_QWORD *)v2 + 3), v5);
*v2 = v6;
if ( v6 <= 0 )
break;
fprintf(stdout, "%d ", v3);
daveDump("packet", v5, (unsigned int)*v2);
func4((__int64)v2);
++v3;
v4 = 0;
}
++v4;
}
while ( v4 <= 99 );
fwrite("portServer: I closed my fd.\n", 1uLL, 0x1CuLL, stdout);
fflush(stdout);
return 0LL;
}

original_code:
void *portServer(void *arg)
{
    _daveOSserialType s;
    daveInterface * di;
    daveConnection * dc;
    int waitCount,res,pcount;
    portInfo * pi=(portInfo *) arg;
    fprintf(stdout,"portServer: My fd is:%d\n",pi->fd);
    fflush(stdout);
    waitCount = 0;
    daveSetDebug(0);
    pcount=0;
    s.rfd=pi->fd;
    s.wfd=pi->fd;
    di =daveNewInterface(s,"IF1",0,122,2);
    di->timeout=900000;
    dc=daveNewConnection(di,2,0,2);
    while (waitCount < 100) {
 dc->AnswLen=_daveReadISOPacket(dc->iface, dc->msgIn);
 if (dc->AnswLen>0) {
     res=dc->AnswLen;
     fprintf(stdout,"%d ",pcount);
     _daveDump("packet", dc->msgIn, dc->AnswLen);
     waitCount = 0;
     analyze(dc);
     pcount++;
 } else {
     waitCount++;
 }
    }
    fprintf(stdout,"portServer: I closed my fd.\n");
    fflush(stdout);
    return ((void *)0);
}

canonical_original_code:
void *portServer(void *arg)
{
    struct dost s;
    struct _daveInterface *di;
    struct _daveConnection *dc;
    int waitCount; int res; int pcount;
    struct _portInfo *pi = (portInfo *) arg;
    fprintf(stdout,"portServer: My fd is:%d\n",pi->fd);
    fflush(stdout);
    waitCount = 0;
    daveSetDebug(0);
    pcount=0;
    s.rfd=pi->fd;
    s.wfd=pi->fd;
    di =daveNewInterface(s,"IF1",0,122,2);
    di->timeout=900000;
    dc=daveNewConnection(di,2,0,2);
    while (waitCount < 100) {
 dc->AnswLen=_daveReadISOPacket(dc->iface, dc->msgIn);
 if (dc->AnswLen>0) {
     res=dc->AnswLen;
     fprintf(stdout,"%d ",pcount);
     _daveDump("packet", dc->msgIn, dc->AnswLen);
     waitCount = 0;
     analyze(dc);
     pcount++;
 } else {
     waitCount++;
 }
    }
    fprintf(stdout,"portServer: I closed my fd.\n");
    fflush(stdout);
    return ((void *)0);
}

variable_types:
arg
void *

variable_types:
s
struct dost { int rfd; int wfd; }

variable_types: struct _daveInterface { int timeout; struct dost { int rfd; int wfd; } fd; int localMPI; int users; char *name; int protocol; int speed; int ackPos; int nextConnection; int (*initAdapter)(struct _daveInterface *); int (*connectPLC)(struct _daveConnection { int AnswLen; unsigned char *resultPointer; int maxPDUlength; int MPIAdr; struct _daveInterface *iface; int needAckNumber; int PDUnumber; int ibhSrcConn; int ibhDstConn; unsigned char msgIn[2048]; unsigned char msgOut[2048]; unsigned char *_resultPointer; int PDUstartO; int PDUstartI; int rack; int slot; int connectionNumber; int connectionNumber2; unsigned char messageNumber; unsigned char packetNumber; void *hook; struct _daveS5cache { int PAE; int PAA; int flags; int timers; int counters; int systemData; struct _daveS5AreaInfo { int area; int DBnumber; int address; int len; struct _daveS5AreaInfo *next; } *first; } *cache; } *); int (*disconnectPLC)(struct _daveConnection *); int (*disconnectAdapter)(struct _daveInterface *); int (*exchange)(struct _daveConnection *, struct PDU { unsigned char *header; unsigned char *param; unsigned char *data; unsigned char *udata; int hlen; int plen; int dlen; int udlen; } *); int (*sendMessage)(struct _daveConnection *, struct PDU *); int (*getResponse)(struct _daveConnection *); int (*listReachablePartners)(struct _daveInterface *di, char *buf); char realName[20]; int (*ifread)(struct _daveInterface *, char *, int); int (*ifwrite)(struct _daveInterface *, char *, int); int seqNumber; } *

variable_types: waitCount int

variable_types: res int

variable_types: pcount int

variable_types:
pi
struct _portInfo { int fd; } *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
__int64 v4; // rax
__int64 v5; // rdx
int v6; // eax
char *v7; // rax
int arg[2]; // [rsp+8h] [rbp-110h] BYREF
pthread_t v9; // [rsp+10h] [rbp-108h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-100h] BYREF
pthread_attr_t attr; // [rsp+20h] [rbp-F8h] BYREF
fd_set readfds; // [rsp+60h] [rbp-B8h] BYREF
int pipedes[2]; // [rsp+E4h] [rbp-34h] BYREF
char buf[44]; // [rsp+ECh] [rbp-2Ch] BYREF

puts("CP-Simulation for ISO over TCP");
if ( argc <= 1 )
{
puts("Assuming standard port (102) for ISO over TCP");
puts("If you don't want that do: isotest4 xxx, and I will listen on xxx.\n");
v3 = "102 ";
}
else
{
v3 = argv[1];
}
puts("Assuming rack 0, slot 2 for simulated device. There's currently no way to change that.\n");
readCallBack = dummyRead;
writeCallBack = dummyWrite;
pipe(pipedes);
v4 = atol(v3);
arg[1] = v4;
fprintf(stdout, "Main serv: port: %d\n", v4);
arg[0] = pipedes[1];
fprintf(stdout, "Main serv: Accepter pipe fd: %d\n", (unsigned int)pipedes[1]);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
pthread_create(&newthread, &attr, (void *(*)(void *))accepter, arg);
while ( 1 )
{
do
{
memset(&readfds, 0, sizeof(readfds));
v5 = (unsigned int)pipedes[0];
readfds.fds_bits[pipedes[0] / 64] |= 1LL << (pipedes[0] % 64);
fprintf(stdout, "Main serv: about to select on %d\n", v5);
fflush(stdout);
}
while ( select(pipedes[0] + 1, &readfds, 0LL, &readfds, 0LL) <= 0 );
fwrite("Main serv: about to read\n", 1uLL, 0x19uLL, stdout);
read(pipedes[0], buf, 4uLL);
v9 = 0LL;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v6 = pthread_create(&v9, &attr, portServer, buf);
if ( v6 )
{
v7 = strerror(v6);
fprintf(stdout, "Main serv: create error:%s\n", v7);
close(0);
usleep(0x186A0u);
}
}
}

original_code:
int main(int argc, char **argv)
{
    portInfo pi;
    int filedes[2], res, newfd;
    fd_set FDS;
    char * s2;
    pthread_attr_t attr;
    pthread_t ac, ps;
    accepter_info ai;
    printf("CP-Simulation for ISO over TCP\n");
    if (argc<2) {
 printf("Assuming standard port (102) for ISO over TCP\n");
 printf("If you don't want that do: isotest4 xxx, and I will listen on xxx.\n\n");
 s2 = "102 ";
    } else
 s2 = argv[1];
    printf("Assuming rack 0, slot 2 for simulated device. There's currently no way to change that.\n\n");
    readCallBack=dummyRead;
    writeCallBack=dummyWrite;
    pipe(filedes);
    ai.port = atol(s2);
    fprintf(stdout,"Main serv: port: %d\n",ai.port);
    ai.fd = filedes[1];
    fprintf(stdout,"Main serv: Accepter pipe fd: %d\n",ai.fd);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    res=pthread_create(&ac, &attr, accepter, &ai );
    do {
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&FDS)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&FDS)->__fds_bits)[((filedes[0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((filedes[0]) % (8 * (int) sizeof (__fd_mask)))))));
 fprintf(stdout,"Main serv: about to select on %d\n",filedes[0]);
 fflush(stdout);
 if (select(filedes[0] + 1, &FDS, ((void *)0), &FDS, ((void *)0)) > 0) {
     fprintf(stdout,"Main serv: about to read\n");
     res = read(filedes[0], &pi.fd, sizeof(pi.fd));
     ps=0;
     pthread_attr_init(&attr);
     pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
     res=pthread_create(&ps, &attr, portServer, &pi);
     if(res) {
  fprintf(stdout,"Main serv: create error:%s\n",strerror(res));
  close(newfd);
  usleep(100000);
     }
 }
    }
    while (1);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    struct _portInfo pi;
    int filedes[2]; int res; int newfd;
    struct fd_set FDS;
    char *s2;
    union pthread_attr_t attr;
    unsigned long ac; unsigned long ps;
    struct accepter_info ai;
    printf("CP-Simulation for ISO over TCP\n");
    if (argc<2) {
 printf("Assuming standard port (102) for ISO over TCP\n");
 printf("If you don't want that do: isotest4 xxx, and I will listen on xxx.\n\n");
 s2 = "102 ";
    } else
 s2 = argv[1];
    printf("Assuming rack 0, slot 2 for simulated device. There's currently no way to change that.\n\n");
    readCallBack=dummyRead;
    writeCallBack=dummyWrite;
    pipe(filedes);
    ai.port = atol(s2);
    fprintf(stdout,"Main serv: port: %d\n",ai.port);
    ai.fd = filedes[1];
    fprintf(stdout,"Main serv: Accepter pipe fd: %d\n",ai.fd);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    res=pthread_create(&ac, &attr, accepter, &ai );
    do {
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&FDS)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&FDS)->__fds_bits)[((filedes[0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((filedes[0]) % (8 * (int) sizeof (__fd_mask)))))));
 fprintf(stdout,"Main serv: about to select on %d\n",filedes[0]);
 fflush(stdout);
 if (select(filedes[0] + 1, &FDS, ((void *)0), &FDS, ((void *)0)) > 0) {
     fprintf(stdout,"Main serv: about to read\n");
     res = read(filedes[0], &pi.fd, sizeof(pi.fd));
     ps=0;
     pthread_attr_init(&attr);
     pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
     res=pthread_create(&ps, &attr, portServer, &pi);
     if(res) {
  fprintf(stdout,"Main serv: create error:%s\n",strerror(res));
  close(newfd);
  usleep(100000);
     }
 }
    }
    while (1);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
pi
struct _portInfo { int fd; }

variable_types:
filedes
int [2]

variable_types: newfd int

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

variable_types: union pthread_attr_t { char __size[56]; long __align; }

variable_types:
ac
unsigned long

variable_types:
ps
unsigned long

variable_types: struct accepter_info { int fd; int port; }


openSocket

decompiled_code:
__int64 __fastcall openSocket(__int16 a1, const char *a2)
{
struct hostent *v2; // rax
unsigned int v3; // ebx
int *v4; // rbp
unsigned int v5; // er12
int *v7; // rax
char *v8; // rax
char *v9; // rax
struct sockaddr v10; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( (daveDebug & 0x10000) != 0 )
{
fwrite("openSocket: enter OpenSocket", 1uLL, 0x1CuLL, stdout);
fflush(stdout);
}
v10.sa_family = 2;
*(_WORD *)v10.sa_data = _bswap_16(a1);
v2 = gethostbyname(a2);
v3 = 0;
if ( v2 )
{
*(_DWORD *)&v10.sa_data[2] = **(_DWORD **)v2->h_addr_list;
v3 = socket(2, 1, 0);
if ( (daveDebug & 0x10000) != 0 )
fprintf(stdout, "openSocket: OpenSocket: socket is %d\n", v3);
if ( connect(v3, &v10, 0x10u) )
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stdout, "openSocket: Socket error: %s \n", v8);
close(v3);
v3 = 0;
}
else
{
if ( (daveDebug & 0x10000) != 0 )
fprintf(stdout, "openSocket: Connected to host: %s \n", a2);
v4 = _errno_location();
*v4 = 0;
optval[0] = 1;
v5 = setsockopt(v3, 1, 9, optval, 4u);
if ( (daveDebug & 0x10000) != 0 )
{
v9 = strerror(*v4);
fprintf(stdout, "openSocket: setsockopt %s %d\n", v9, v5);
}
}
fflush(stdout);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, const char *a2)
{
struct hostent *v2; // rax
unsigned int v3; // ebx
int *v4; // rbp
unsigned int v5; // er12
int *v7; // rax
char *v8; // rax
char *v9; // rax
struct sockaddr v10; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( (daveDebug & 0x10000) != 0 )
{
fwrite("openSocket: enter OpenSocket", 1uLL, 0x1CuLL, stdout);
fflush(stdout);
}
v10.sa_family = 2;
*(_WORD *)v10.sa_data = func0(a1);
v2 = gethostbyname(a2);
v3 = 0;
if ( v2 )
{
*(_DWORD *)&v10.sa_data[2] = **(_DWORD **)v2->h_addr_list;
v3 = socket(2, 1, 0);
if ( (daveDebug & 0x10000) != 0 )
fprintf(stdout, "openSocket: OpenSocket: socket is %d\n", v3);
if ( connect(v3, &v10, 0x10u) )
{
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stdout, "openSocket: Socket error: %s \n", v8);
close(v3);
v3 = 0;
}
else
{
if ( (daveDebug & 0x10000) != 0 )
fprintf(stdout, "openSocket: Connected to host: %s \n", a2);
v4 = _errno_location();
*v4 = 0;
optval[0] = 1;
v5 = setsockopt(v3, 1, 9, optval, 4u);
if ( (daveDebug & 0x10000) != 0 )
{
v9 = strerror(*v4);
fprintf(stdout, "openSocket: setsockopt %s %d\n", v9, v5);
}
}
fflush(stdout);
}
return v3;
}

original_code:
int openSocket(const int port, const char * peer) {
    int fd,res,opt;
    struct sockaddr_in addr;
    socklen_t addrlen;
    struct hostent *he;
    if (daveDebug & 0x10000) {
 fprintf(stdout,"openSocket: " "enter OpenSocket");
 fflush(stdout);
    }
    addr.sin_family = 2;
    addr.sin_port =htons(port);
    he = gethostbyname(peer);
    if (!he) return 0;
    memcpy(&addr.sin_addr, he->h_addr_list[0], sizeof(addr.sin_addr));
    fd = socket(2, SOCK_STREAM, 0);
    if (daveDebug & 0x10000) {
 fprintf(stdout,"openSocket: " "OpenSocket: socket is %d\n",fd);
    }
    addrlen = sizeof(addr);
    if (connect(fd, (struct sockaddr *) & addr, addrlen)) {
 fprintf(stdout,"openSocket: " "Socket error: %s \n",strerror((*__errno_location ())));
 close(fd);
 fd = 0;
    } else {
 if (daveDebug & 0x10000) {
     fprintf(stdout,"openSocket: " "Connected to host: %s \n",peer);
 }
 (*__errno_location ())=0;
 (*__errno_location ())=0;
 opt=1;
 res=setsockopt(fd, 1, 9, &opt, 4);
 if (daveDebug & 0x10000) {
     fprintf(stdout,"openSocket: " "setsockopt %s %d\n",strerror((*__errno_location ())),res);
 }
    }
    fflush(stdout);
    return fd;
}

canonical_original_code:
int openSocket(int port, char *peer) {
    int fd; int res; int opt;
    struct sockaddr_in addr;
    unsigned int addrlen;
    struct hostent *he;
    if (daveDebug & 0x10000) {
 fprintf(stdout,"openSocket: " "enter OpenSocket");
 fflush(stdout);
    }
    addr.sin_family = 2;
    addr.sin_port =htons(port);
    he = gethostbyname(peer);
    if (!he) return 0;
    memcpy(&addr.sin_addr, he->h_addr_list[0], sizeof(addr.sin_addr));
    fd = socket(2, SOCK_STREAM, 0);
    if (daveDebug & 0x10000) {
 fprintf(stdout,"openSocket: " "OpenSocket: socket is %d\n",fd);
    }
    addrlen = sizeof(addr);
    if (connect(fd, (struct sockaddr *) & addr, addrlen)) {
 fprintf(stdout,"openSocket: " "Socket error: %s \n",strerror((*__errno_location ())));
 close(fd);
 fd = 0;
    } else {
 if (daveDebug & 0x10000) {
     fprintf(stdout,"openSocket: " "Connected to host: %s \n",peer);
 }
 (*__errno_location ())=0;
 (*__errno_location ())=0;
 opt=1;
 res=setsockopt(fd, 1, 9, &opt, 4);
 if (daveDebug & 0x10000) {
     fprintf(stdout,"openSocket: " "setsockopt %s %d\n",strerror((*__errno_location ())),res);
 }
    }
    fflush(stdout);
    return fd;
}

variable_types: port int

variable_types:
peer
char *

variable_types: fd int

variable_types: opt 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:
addrlen
unsigned int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *


closeSocket

decompiled_code:
int __fastcall closeSocket(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
return close(a1);
}

original_code:
int closeSocket(int h) {
    return close(h);
}

variable_types: h int


setPort

decompiled_code:
__int64 __fastcall setPort(const char *a1, const char *a2, char a3)
{
unsigned int v5; // ebx
__int32_t v6; // esi
tcflag_t v7; // eax
struct termios v9; // [rsp+0h] [rbp-98h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-58h] BYREF

v5 = open(a1, 258);
tcgetattr(v5, &termios_p);
if ( !memcmp(a2, "115200", 6uLL) )
{
v9.c_cflag = 4098;
}
else if ( !memcmp(a2, "57600", 5uLL) )
{
v9.c_cflag = 4097;
}
else if ( !memcmp(a2, "38400", 5uLL) )
{
v9.c_cflag = 15;
}
else if ( !memcmp(a2, "19200", 5uLL) )
{
v9.c_cflag = 14;
}
else if ( !memcmp(a2, "9600", 4uLL) )
{
v9.c_cflag = 13;
}
else if ( !memcmp(a2, "4800", 4uLL) )
{
v9.c_cflag = 12;
}
else if ( !memcmp(a2, "2400", 4uLL) )
{
v9.c_cflag = 11;
}
else if ( !memcmp(a2, "1200", 4uLL) )
{
v9.c_cflag = 9;
}
else if ( !memcmp(a2, "600", 3uLL) )
{
v9.c_cflag = 8;
}
else if ( !memcmp(a2, "300", 3uLL) )
{
v9.c_cflag = 7;
}
else
{
printf("setPort : illegal Baudrate: %s\n", a2);
}
v6 = (*_ctype_tolower_loc())[a3];
switch ( (_BYTE)v6 )
{
case 'e':
v7 = v9.c_cflag;
BYTE1(v7) = BYTE1(v9.c_cflag) & 0xFC | 1;
v9.c_cflag = v7;
break;
case 'o':
v9.c_cflag |= 0x300u;
break;
case 'n':
v9.c_cflag &= 0xFFFFFCFF;
break;
default:
printf("setPort : illegal parity mode:%c\n", (unsigned int)(char)v6);
break;
}
v9.c_cflag |= 0x8B0u;
v9.c_iflag = 0;
v9.c_oflag = 16;
v9.c_lflag = 0;
v9.c_cc[6] = 1;
v9.c_cc[5] = 0;
tcflush(v5, 0);
tcsetattr(v5, 0, &v9);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, const char *a2, char a3)
{
unsigned int v5; // ebx
__int32_t v6; // esi
tcflag_t v7; // eax
struct termios v9; // [rsp+0h] [rbp-98h] BYREF
struct termios termios_p; // [rsp+40h] [rbp-58h] BYREF

v5 = open(a1, 258);
tcgetattr(v5, &termios_p);
if ( !memcmp(a2, "115200", 6uLL) )
{
v9.c_cflag = 4098;
}
else if ( !memcmp(a2, "57600", 5uLL) )
{
v9.c_cflag = 4097;
}
else if ( !memcmp(a2, "38400", 5uLL) )
{
v9.c_cflag = 15;
}
else if ( !memcmp(a2, "19200", 5uLL) )
{
v9.c_cflag = 14;
}
else if ( !memcmp(a2, "9600", 4uLL) )
{
v9.c_cflag = 13;
}
else if ( !memcmp(a2, "4800", 4uLL) )
{
v9.c_cflag = 12;
}
else if ( !memcmp(a2, "2400", 4uLL) )
{
v9.c_cflag = 11;
}
else if ( !memcmp(a2, "1200", 4uLL) )
{
v9.c_cflag = 9;
}
else if ( !memcmp(a2, "600", 3uLL) )
{
v9.c_cflag = 8;
}
else if ( !memcmp(a2, "300", 3uLL) )
{
v9.c_cflag = 7;
}
else
{
printf("setPort : illegal Baudrate: %s\n", a2);
}
v6 = (*_ctype_tolower_loc())[a3];
switch ( (_BYTE)v6 )
{
case 'e':
v7 = v9.c_cflag;
BYTE1(v7) = BYTE1(v9.c_cflag) & 0xFC | 1;
v9.c_cflag = v7;
break;
case 'o':
v9.c_cflag |= 0x300u;
break;
case 'n':
v9.c_cflag &= 0xFFFFFCFF;
break;
default:
printf("setPort : illegal parity mode:%c\n", (unsigned int)(char)v6);
break;
}
v9.c_cflag |= 0x8B0u;
v9.c_iflag = 0;
v9.c_oflag = 16;
v9.c_lflag = 0;
v9.c_cc[6] = 1;
v9.c_cc[5] = 0;
tcflush(v5, 0);
tcsetattr(v5, 0, &v9);
return v5;
}

original_code:
int setPort(char * name, char* baud,char parity)
{
    int fd,res;
    struct termios oldtio, newtio;
    fd = open(name, 02 | 0400);
    tcgetattr(fd, &oldtio);
    if (0==strncmp(baud,"115200",6))
 newtio.c_cflag = 0010002;
    else if (0==strncmp(baud,"57600",5))
 newtio.c_cflag = 0010001;
    else if (0==strncmp(baud,"38400",5))
 newtio.c_cflag = 0000017;
    else if (0==strncmp(baud,"19200",5))
 newtio.c_cflag = 0000016;
    else if (0==strncmp(baud,"9600",4))
 newtio.c_cflag = 0000015;
    else if (0==strncmp(baud,"4800",4))
 newtio.c_cflag = 0000014;
    else if (0==strncmp(baud,"2400",4))
 newtio.c_cflag = 0000013;
    else if (0==strncmp(baud,"1200",4))
 newtio.c_cflag = 0000011;
    else if (0==strncmp(baud,"600",3))
 newtio.c_cflag = 0000010;
    else if (0==strncmp(baud,"300",3))
 newtio.c_cflag = 0000007;
    else
 printf("setPort : " "illegal Baudrate: %s\n", baud);
    parity=tolower(parity);
    if (parity == 'e')
 newtio.c_cflag = (newtio.c_cflag & ~0001000) | 0000400;
    else if (parity == 'o')
 newtio.c_cflag = newtio.c_cflag | 0001000 | 0000400;
    else if (parity == 'n')
 newtio.c_cflag = newtio.c_cflag & ~(0001000 | 0000400);
    else
 printf("setPort : " "illegal parity mode:%c\n", parity);
    newtio.c_cflag |= 0000060;
    newtio.c_cflag = newtio.c_cflag | 0004000 | 0000200;
    newtio.c_iflag = 0;
    newtio.c_oflag = 0000020;
    newtio.c_lflag = 0;
    newtio.c_cc[6] = 1;
    newtio.c_cc[5] = 0;
    tcflush(fd, 0);
    tcsetattr(fd, 0, &newtio);
    return (fd);
}

canonical_original_code:
int setPort(char *name, char *baud, char parity)
{
    int fd; int res;
    struct termios oldtio; struct termios newtio;
    fd = open(name, 02 | 0400);
    tcgetattr(fd, &oldtio);
    if (0==strncmp(baud,"115200",6))
 newtio.c_cflag = 0010002;
    else if (0==strncmp(baud,"57600",5))
 newtio.c_cflag = 0010001;
    else if (0==strncmp(baud,"38400",5))
 newtio.c_cflag = 0000017;
    else if (0==strncmp(baud,"19200",5))
 newtio.c_cflag = 0000016;
    else if (0==strncmp(baud,"9600",4))
 newtio.c_cflag = 0000015;
    else if (0==strncmp(baud,"4800",4))
 newtio.c_cflag = 0000014;
    else if (0==strncmp(baud,"2400",4))
 newtio.c_cflag = 0000013;
    else if (0==strncmp(baud,"1200",4))
 newtio.c_cflag = 0000011;
    else if (0==strncmp(baud,"600",3))
 newtio.c_cflag = 0000010;
    else if (0==strncmp(baud,"300",3))
 newtio.c_cflag = 0000007;
    else
 printf("setPort : " "illegal Baudrate: %s\n", baud);
    parity=tolower(parity);
    if (parity == 'e')
 newtio.c_cflag = (newtio.c_cflag & ~0001000) | 0000400;
    else if (parity == 'o')
 newtio.c_cflag = newtio.c_cflag | 0001000 | 0000400;
    else if (parity == 'n')
 newtio.c_cflag = newtio.c_cflag & ~(0001000 | 0000400);
    else
 printf("setPort : " "illegal parity mode:%c\n", parity);
    newtio.c_cflag |= 0000060;
    newtio.c_cflag = newtio.c_cflag | 0004000 | 0000200;
    newtio.c_iflag = 0;
    newtio.c_oflag = 0000020;
    newtio.c_lflag = 0;
    newtio.c_cc[6] = 1;
    newtio.c_cc[5] = 0;
    tcflush(fd, 0);
    tcsetattr(fd, 0, &newtio);
    return (fd);
}

variable_types:
name
char *

variable_types:
baud
char *

variable_types: parity char

variable_types: res int

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


closePort

decompiled_code:
int __fastcall closePort(int a1)
{
int result; // eax

result = 0;
if ( a1 != -1 )
result = close(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
int result; // eax

result = 0;
if ( a1 != -1 )
result = close(a1);
return result;
}

original_code:
int closePort(int port){
    int res=0;
    if (port!=-1)
        res=close(port);
    return res;
}

canonical_original_code:
int closePort(int port){
    int res = 0;
    if (port!=-1)
        res=close(port);
    return res;
}


decompiled_code:
__int64 __fastcall word_pop(__int64 *a1)
{
__int64 v1; // rbx

v1 = *a1;
free(a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1)
{
__int64 v1; // rbx

v1 = *a1;
free(a1);
return v1;
}

original_code:
WordStack* word_pop(WordStack* top) {
  WordStack* prev = top->prev;
  free(top);
  return prev;
}

canonical_original_code:
struct WordStack *word_pop(struct WordStack *top) {
  struct WordStack *prev = top->prev;
  free(top);
  return prev;
}

variable_types:
top
struct WordStack *

variable_types: struct WordStack { struct WordStack *prev; char word[]; } *


decompiled_code:
__int64 __fastcall word_peek(__int64 a1)
{
return a1 + 8;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return a1 + 8;
}

original_code:
char* word_peek(WordStack* stack) {
  return stack->word;
}

canonical_original_code:
char *word_peek(struct WordStack *stack) {
  return stack->word;
}

return_type: char *

user_defined_types: struct WordStack { struct WordStack *prev; char word[]; }


decompiled_code:
__int64 __fastcall is_whitespace(unsigned __int8 a1)
{
char v1; // al

v1 = 1;
if ( a1 <= 0x20u )
v1 = ((0x100002600uLL >> a1) ^ 1) & 1;
return (unsigned __int8)v1 ^ 1u;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1)
{
char v1; // al

v1 = 1;
if ( a1 <= 0x20u )
v1 = ((0x100002600uLL >> a1) ^ 1) & 1;
return (unsigned __int8)v1 ^ 1u;
}

original_code:
int is_whitespace(char ch) {
  return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
}

variable_types: ch char


decompiled_code:
_BOOL8 __fastcall is_number(char a1)
{
return (unsigned __int8)(a1 - 48) <= 9u;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(char a1)
{
return (unsigned __int8)(a1 - 48) <= 9u;
}

original_code:
int is_number(char ch) {
  return ch >= '0' && ch <= '9';
}


decompiled_code:
__int64 __fastcall next_word(unsigned __int8 **a1, _DWORD *a2, unsigned __int8 *a3, int a4)
{
unsigned __int8 *v4; // r12
__int64 v5; // rbp
unsigned __int8 *v6; // rbx
unsigned __int8 *v7; // r13
__int64 result; // rax
unsigned __int8 *v9; // r13

v4 = a3;
v5 = a4;
v6 = a3;
v7 = 0LL;
if ( a4 <= 0 )
{
LABEL_4:
result = 0LL;
if ( v7 == (unsigned __int8 *)v5 )
return result;
*a1 = v6;
LODWORD(v9) = (_DWORD)v6;
goto LABEL_11;
}
while ( 1 )
{
result = is_whitespace(*v6);
if ( !(_DWORD)result )
break;
v7 = (unsigned __int8 *)(++v6 - v4);
if ( v6 - v4 >= v5 )
goto LABEL_4;
}
if ( (unsigned __int8 *)v5 != v7 )
{
*a1 = v6;
if ( v6 - v4 >= v5 )
{
LODWORD(v9) = (_DWORD)v6;
}
else
{
v9 = v6;
do
{
if ( (unsigned int)is_whitespace(*v9) )
break;
++v9;
}
while ( v9 - v4 < v5 );
}
LABEL_11:
*a2 = (_DWORD)v9 - (_DWORD)v6;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int8 **a1, _DWORD *a2, unsigned __int8 *a3, int a4)
{
unsigned __int8 *v4; // r12
__int64 v5; // rbp
unsigned __int8 *v6; // rbx
unsigned __int8 *v7; // r13
__int64 result; // rax
unsigned __int8 *v9; // r13

v4 = a3;
v5 = a4;
v6 = a3;
v7 = 0LL;
if ( a4 <= 0 )
{
LABEL_4:
result = 0LL;
if ( v7 == (unsigned __int8 *)v5 )
return result;
*a1 = v6;
LODWORD(v9) = (_DWORD)v6;
goto LABEL_11;
}
while ( 1 )
{
result = func9(*v6);
if ( !(_DWORD)result )
break;
v7 = (unsigned __int8 *)(++v6 - v4);
if ( v6 - v4 >= v5 )
goto LABEL_4;
}
if ( (unsigned __int8 *)v5 != v7 )
{
*a1 = v6;
if ( v6 - v4 >= v5 )
{
LODWORD(v9) = (_DWORD)v6;
}
else
{
v9 = v6;
do
{
if ( (unsigned int)func9(*v9) )
break;
++v9;
}
while ( v9 - v4 < v5 );
}
LABEL_11:
*a2 = (_DWORD)v9 - (_DWORD)v6;
result = 1LL;
}
return result;
}

original_code:
int next_word(char** word_start, int* word_len, char* str, int str_len) {
  char* here = str;
  while(here - str < str_len && is_whitespace(*here)) {
    ++here;
  }
  if(here - str == str_len) return 0;
  *word_start = here;
  while(here - str < str_len && !is_whitespace(*here)) {
    ++here;
  }
  *word_len = here - *word_start;
  return 1;
}

canonical_original_code:
int next_word(char **word_start, int *word_len, char *str, int str_len) {
  char *here = str;
  while(here - str < str_len && is_whitespace(*here)) {
    ++here;
  }
  if(here - str == str_len) return 0;
  *word_start = here;
  while(here - str < str_len && !is_whitespace(*here)) {
    ++here;
  }
  *word_len = here - *word_start;
  return 1;
}

variable_types:
str
char *

variable_types:
here
char *


precedence

decompiled_code:
__int64 __fastcall precedence(char *a1)
{
char v1; // al
__int64 result; // rax

v1 = *a1;
if ( *a1 == 47 || v1 == 42 )
result = 10LL;
else
result = ((v1 - 43) & 0xFD) == 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *a1)
{
char v1; // al
__int64 result; // rax

v1 = *a1;
if ( *a1 == 47 || v1 == 42 )
result = 10LL;
else
result = ((v1 - 43) & 0xFD) == 0;
return result;
}

original_code:
int precedence(char* operator) {
  if(*operator == '*') return 10;
  if(*operator == '/') return 10;
  if(*operator == '+') return 1;
  if(*operator == '-') return 1;
  return 0;
}

canonical_original_code:
int precedence(char *operator) {
  if(*operator == '*') return 10;
  if(*operator == '/') return 10;
  if(*operator == '+') return 1;
  if(*operator == '-') return 1;
  return 0;
}

variable_types:
operator
char *


decompiled_code:
void __noreturn fail_exit(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
fflush(stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func13(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
fflush(stderr);
exit(1);
}

original_code:
void fail_exit(const char * message, ...) {
  va_list args;
  __builtin_va_start(args,message);
  vfprintf(stderr, message, args);
  __builtin_va_end(args);
  fprintf(stderr, "\n");
  fflush(stderr);
  exit(1);
}

canonical_original_code:
void fail_exit(char *message, ...) {
  __builtin_va_list args;
  __builtin_va_start(args,message);
  vfprintf(stderr, message, args);
  __builtin_va_end(args);
  fprintf(stderr, "\n");
  fflush(stderr);
  exit(1);
}

variable_types:
message
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // ecx
int v5; // er13
__int64 *v6; // rbx
char *v7; // rbp
int v8; // er12
const char *v9; // rbp
const char *v10; // rax
int v12; // [rsp+4h] [rbp-34h] BYREF
char *v13; // [rsp+8h] [rbp-30h] BYREF

if ( argc != 2 )
fail_exit("usage: %s \"expression string\"", (char)*argv);
v3 = (char *)argv[1];
v4 = strlen(v3);
v5 = (_DWORD)v3 + v4;
v6 = 0LL;
while ( (unsigned int)next_word((unsigned __int8 **)&v13, &v12, (unsigned __int8 *)v3, v4) )
{
v9 = v13;
if ( is_number(*v13) )
{
printf("%.*s\n", v12, v9);
}
else
{
for ( ; v6; v6 = (__int64 *)word_pop(v6) )
{
v7 = (char *)word_peek((__int64)v6);
v8 = precedence(v7);
if ( v8 <= (int)precedence(v13) )
break;
puts(v7);
}
v6 = word_push((__int64)v6, v13, v12);
}
v3 = &v13[v12];
v4 = v5 - (_DWORD)v3;
}
for ( ; v6; v6 = (__int64 *)word_pop(v6) )
{
v10 = (const char *)word_peek((__int64)v6);
puts(v10);
}
puts("print");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
int v4; // ecx
int v5; // er13
__int64 *v6; // rbx
char *v7; // rbp
int v8; // er12
const char *v9; // rbp
const char *v10; // rax
int v12; // [rsp+4h] [rbp-34h] BYREF
char *v13; // [rsp+8h] [rbp-30h] BYREF

if ( argc != 2 )
func13("usage: %s \"expression string\"", (char)*argv);
v3 = (char *)argv[1];
v4 = strlen(v3);
v5 = (_DWORD)v3 + v4;
v6 = 0LL;
while ( (unsigned int)func11((unsigned __int8 **)&v13, &v12, (unsigned __int8 *)v3, v4) )
{
v9 = v13;
if ( func10(*v13) )
{
printf("%.*s\n", v12, v9);
}
else
{
for ( ; v6; v6 = (__int64 *)func7(v6) )
{
v7 = (char *)func8((__int64)v6);
v8 = func12(v7);
if ( v8 <= (int)func12(v13) )
break;
puts(v7);
}
v6 = word_push((__int64)v6, v13, v12);
}
v3 = &v13[v12];
v4 = v5 - (_DWORD)v3;
}
for ( ; v6; v6 = (__int64 *)func7(v6) )
{
v10 = (const char *)func8((__int64)v6);
puts(v10);
}
puts("print");
return 0;
}

original_code:
int main(int argc, char** argv) {
  if(argc != 2) fail_exit("usage: %s \"expression string\"", argv[0]);
  char* str = argv[1];
  int str_len = strlen(str);
  char* str_end = str + str_len;
  char* word;
  int word_len;
  WordStack* stack = ((void *)0);
  while(next_word(&word, &word_len, str, str_len)) {
    if(is_number(*word)) {
      printf("%.*s\n", word_len, word);
    } else {
      while(stack && precedence(word_peek(stack)) > precedence(word)) {
        printf("%s\n", word_peek(stack));
        stack = word_pop(stack);
      }
      stack = word_push(stack, word, word_len);
    }
    str = word + word_len;
    str_len = str_end - str;
  }
  while(stack) {
    printf("%s\n", word_peek(stack));
    stack = word_pop(stack);
  }
  printf("print\n");
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  if(argc != 2) fail_exit("usage: %s \"expression string\"", argv[0]);
  char *str = argv[1];
  int str_len = strlen(str);
  char *str_end = str + str_len;
  char *word;
  int word_len;
  struct WordStack *stack = ((void *)0);
  while(next_word(&word, &word_len, str, str_len)) {
    if(is_number(*word)) {
      printf("%.*s\n", word_len, word);
    } else {
      while(stack && precedence(word_peek(stack)) > precedence(word)) {
        printf("%s\n", word_peek(stack));
        stack = word_pop(stack);
      }
      stack = word_push(stack, word, word_len);
    }
    str = word + word_len;
    str_len = str_end - str;
  }
  while(stack) {
    printf("%s\n", word_peek(stack));
    stack = word_pop(stack);
  }
  printf("print\n");
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
word
char *


decompiled_code:
void *__fastcall stack_init(__int64 a1)
{
void *result; // rax

*(_DWORD *)(a1 + 12) = 16;
result = malloc(0x100uLL);
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 8) = 0;
return result;
}

canonical_decompiled_code:
void *__fastcall func7(__int64 a1)
{
void *result; // rax

*(_DWORD *)(a1 + 12) = 16;
result = malloc(0x100uLL);
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 8) = 0;
return result;
}

original_code:
void stack_init(IntStack* stack) {
  stack->max = 16;
  stack->values = (int*) malloc(sizeof(IntStack) * stack->max);
  stack->top = 0;
}

canonical_original_code:
void stack_init(struct IntStack *stack) {
  stack->max = 16;
  stack->values = (int *) malloc(sizeof(IntStack) * stack->max);
  stack->top = 0;
}

variable_types: struct IntStack { int *values; int top; int max; } *


decompiled_code:
void *__fastcall stack_push(__int64 a1, int a2)
{
int v2; // eax
void *result; // rax
int v4; // eax

v2 = *(_DWORD *)(a1 + 12);
if ( *(_DWORD *)(a1 + 8) == v2 )
{
v4 = 2 * v2;
*(_DWORD *)(a1 + 12) = v4;
*(_QWORD *)a1 = realloc(*(void **)a1, 4LL * v4);
}
result = *(void **)a1;
*(_DWORD *)(*(_QWORD *)a1 + 4LL * (int)(*(_DWORD *)(a1 + 8))++) = a2;
return result;
}

canonical_decompiled_code:
void *__fastcall func8(__int64 a1, int a2)
{
int v2; // eax
void *result; // rax
int v4; // eax

v2 = *(_DWORD *)(a1 + 12);
if ( *(_DWORD *)(a1 + 8) == v2 )
{
v4 = 2 * v2;
*(_DWORD *)(a1 + 12) = v4;
*(_QWORD *)a1 = realloc(*(void **)a1, 4LL * v4);
}
result = *(void **)a1;
*(_DWORD *)(*(_QWORD *)a1 + 4LL * (int)(*(_DWORD *)(a1 + 8))++) = a2;
return result;
}

original_code:
void stack_push(IntStack* stack, int value) {
  if(stack->top == stack->max) {
    stack->max = stack->max * 2;
    stack->values = (int*)realloc(stack->values, sizeof(int) * stack->max);
  }
  stack->values[stack->top] = value;
  stack->top = stack->top + 1;
}

canonical_original_code:
void stack_push(struct IntStack *stack, int value) {
  if(stack->top == stack->max) {
    stack->max = stack->max * 2;
    stack->values = (int *)realloc(stack->values, sizeof(int) * stack->max);
  }
  stack->values[stack->top] = value;
  stack->top = stack->top + 1;
}

variable_types: value int

user_defined_types: struct IntStack { int *values; int top; int max; }


decompiled_code:
__int64 __fastcall stack_pop(__int64 a1, char a2)
{
int v2; // eax

if ( !*(_DWORD *)(a1 + 8) )
fail_exit("can't pop an empty stack", a2);
v2 = *(_DWORD *)(a1 + 8) - 1;
*(_DWORD *)(a1 + 8) = v2;
return *(unsigned int *)(*(_QWORD *)a1 + 4LL * v2);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2)
{
int v2; // eax

if ( !*(_DWORD *)(a1 + 8) )
func14("can't pop an empty stack", a2);
v2 = *(_DWORD *)(a1 + 8) - 1;
*(_DWORD *)(a1 + 8) = v2;
return *(unsigned int *)(*(_QWORD *)a1 + 4LL * v2);
}

original_code:
int stack_pop(IntStack* stack) {
  if(stack->top == 0) fail_exit("can't pop an empty stack");
  stack->top = stack->top - 1;
  return stack->values[stack->top];
}

canonical_original_code:
int stack_pop(struct IntStack *stack) {
  if(stack->top == 0) fail_exit("can't pop an empty stack");
  stack->top = stack->top - 1;
  return stack->values[stack->top];
}


decompiled_code:
__int64 __fastcall stack_peek(__int64 a1, char a2)
{
if ( !*(_DWORD *)(a1 + 8) )
fail_exit("can't peek an empty stack", a2);
return *(unsigned int *)(*(_QWORD *)a1 + 4LL * *(int *)(a1 + 8) - 4);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char a2)
{
if ( !*(_DWORD *)(a1 + 8) )
func14("can't peek an empty stack", a2);
return *(unsigned int *)(*(_QWORD *)a1 + 4LL * *(int *)(a1 + 8) - 4);
}

original_code:
int stack_peek(IntStack* stack) {
  if(stack->top == 0) fail_exit("can't peek an empty stack");
  return stack->values[stack->top - 1];
}

canonical_original_code:
int stack_peek(struct IntStack *stack) {
  if(stack->top == 0) fail_exit("can't peek an empty stack");
  return stack->values[stack->top - 1];
}


original_code:
char* base_str(int value, int base) {
  int isNegative = 0;
  if(value < 0) {
    isNegative = 1;
    value = -value;
  }
  num_buffer[sizeof(num_buffer - 1)] = '\0';
  int position = sizeof(num_buffer) - 2;
  if(value == 0) {
    num_buffer[position--] = '0';
  } else {
    while(value > 0) {
      int digit = value % base;
      value = value / base;
      if(digit > 9) {
        num_buffer[position--] = 'A' + (digit - 10);
      } else {
        num_buffer[position--] = '0' + digit;
      }
    }
  }
  if(isNegative) {
    num_buffer[position--] = '-';
  }
  return &num_buffer[position + 1];
}

canonical_original_code:
char *base_str(int value, int base) {
  int isNegative = 0;
  if(value < 0) {
    isNegative = 1;
    value = -value;
  }
  num_buffer[sizeof(num_buffer - 1)] = '\0';
  int position = sizeof(num_buffer) - 2;
  if(value == 0) {
    num_buffer[position--] = '0';
  } else {
    while(value > 0) {
      int digit = value % base;
      value = value / base;
      if(digit > 9) {
        num_buffer[position--] = 'A' + (digit - 10);
      } else {
        num_buffer[position--] = '0' + digit;
      }
    }
  }
  if(isNegative) {
    num_buffer[position--] = '-';
  }
  return &num_buffer[position + 1];
}

variable_types: base int

variable_types: isNegative int

variable_types: position int

variable_types: digit int


decompiled_code:
__int64 __fastcall read_base(const char *a1, int a2)
{
int v3; // eax
__int64 v4; // rbp
int v5; // er12
unsigned int v6; // er14
int v7; // ebx
char v8; // si

v3 = strlen(a1);
if ( !v3 )
fail_exit("cannot read empty string", a2);
v4 = v3;
v5 = 1;
v6 = 0;
do
{
v8 = a1[v4 - 1];
if ( (unsigned __int8)(v8 - 48) <= 9u )
{
v7 = v8 - 48;
}
else if ( (unsigned __int8)(v8 - 65) > 0x19u )
{
if ( (unsigned __int8)(v8 - 97) > 0x19u )
LABEL_12:
fail_exit("'%c' is not a valid digit in base %d", v8);
v7 = v8 - 87;
}
else
{
v7 = v8 - 55;
}
if ( v7 >= a2 )
goto LABEL_12;
v6 += v5 * v7;
v5 *= a2;
--v4;
}
while ( (int)v4 > 0 );
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char *a1, int a2)
{
int v3; // eax
__int64 v4; // rbp
int v5; // er12
unsigned int v6; // er14
int v7; // ebx
char v8; // si

v3 = strlen(a1);
if ( !v3 )
func14("cannot read empty string", a2);
v4 = v3;
v5 = 1;
v6 = 0;
do
{
v8 = a1[v4 - 1];
if ( (unsigned __int8)(v8 - 48) <= 9u )
{
v7 = v8 - 48;
}
else if ( (unsigned __int8)(v8 - 65) > 0x19u )
{
if ( (unsigned __int8)(v8 - 97) > 0x19u )
LABEL_12:
func14("'%c' is not a valid digit in base %d", v8);
v7 = v8 - 87;
}
else
{
v7 = v8 - 55;
}
if ( v7 >= a2 )
goto LABEL_12;
v6 += v5 * v7;
v5 *= a2;
--v4;
}
while ( (int)v4 > 0 );
return v6;
}

original_code:
int read_base(const char* str, int base) {
  int position = strlen(str);
  int total = 0;
  int place_value = 1;
  if(position == 0) {
    fail_exit("cannot read empty string");
  }
  do {
    --position;
    int value = base;
    if(str[position] >= '0' && str[position] <= '9') {
      value = str[position] - '0';
    } else if(str[position] >= 'A' && str[position] <= 'Z') {
      value = 10 + (str[position] - 'A');
    } else if(str[position] >= 'a' && str[position] <= 'z') {
      value = 10 + (str[position] - 'a');
    }
    if(value >= base) {
      fail_exit("'%c' is not a valid digit in base %d", str[position], base);
    }
    total += (value * place_value);
    place_value *= base;
  } while(&str[position] > str);
  return total;
}

canonical_original_code:
int read_base(char *str, int base) {
  int position = strlen(str);
  int total = 0;
  int place_value = 1;
  if(position == 0) {
    fail_exit("cannot read empty string");
  }
  do {
    --position;
    int value = base;
    if(str[position] >= '0' && str[position] <= '9') {
      value = str[position] - '0';
    } else if(str[position] >= 'A' && str[position] <= 'Z') {
      value = 10 + (str[position] - 'A');
    } else if(str[position] >= 'a' && str[position] <= 'z') {
      value = 10 + (str[position] - 'a');
    }
    if(value >= base) {
      fail_exit("'%c' is not a valid digit in base %d", str[position], base);
    }
    total += (value * place_value);
    place_value *= base;
  } while(&str[position] > str);
  return total;
}

variable_types: total int


evaluate

decompiled_code:
int __fastcall evaluate(__int64 a1, const char *a2)
{
bool v2; // cf
bool v3; // zf
const char *v5; // rax
const char *v6; // rdi
__int64 v7; // rcx
char v8; // dl
bool v9; // cf
bool v10; // zf
const char *v11; // rdi
__int64 v12; // rcx
const char *v13; // rsi
char v14; // dl
bool v15; // cf
bool v16; // zf
const char *v17; // rdi
__int64 v18; // rcx
const char *v19; // rsi
char v20; // dl
bool v21; // cf
bool v22; // zf
const __int16 *v23; // rdi
__int64 v24; // rcx
const char *v25; // rsi
char v26; // dl
bool v27; // cf
bool v28; // zf
const char *v29; // rdi
__int64 v30; // rcx
const char *v31; // rsi
char v32; // dl
bool v33; // cf
bool v34; // zf
const char *v35; // rdi
__int64 v36; // rcx
const char *v37; // rsi
char v38; // dl
bool v39; // cf
bool v40; // zf
const char *v41; // rdi
__int64 v42; // rcx
const char *v43; // rsi
char v44; // dl
bool v45; // cf
bool v46; // zf
const char *v47; // rdi
__int64 v48; // rcx
const char *v49; // rsi
int v50; // ebp
bool v51; // cf
bool v52; // zf
const char *v53; // rdi
__int64 v54; // rcx
const char *v55; // rsi
int v56; // eax
const char *v57; // rax
int result; // eax
int v59; // er12
int v60; // ebp
int v61; // ebp
int v62; // eax
int v63; // ebp
int v64; // eax
int v65; // ebp
int v66; // eax
int v67; // ebp
int v68; // eax
int v69; // ebp
int v70; // eax
int v71; // er12
int v72; // eax
int v73; // esi
int v74; // eax

v5 = a2;
v6 = "set-base";
v7 = 9LL;
do
{
if ( !v7 )
break;
v2 = *a2 < (unsigned int)*v6;
v3 = *a2++ == *v6++;
--v7;
}
while ( v3 );
v8 = (!v2 && !v3) - v2;
v9 = 0;
v10 = v8 == 0;
if ( v8 )
{
v11 = "swap";
v12 = 5LL;
v13 = v5;
do
{
if ( !v12 )
break;
v9 = *v13 < (unsigned int)*v11;
v10 = *v13++ == *v11++;
--v12;
}
while ( v10 );
v14 = (!v9 && !v10) - v9;
v15 = 0;
v16 = v14 == 0;
if ( v14 )
{
v17 = "+";
v18 = 2LL;
v19 = v5;
do
{
if ( !v18 )
break;
v15 = *v19 < (unsigned int)*v17;
v16 = *v19++ == *v17++;
--v18;
}
while ( v16 );
v20 = (!v15 && !v16) - v15;
v21 = 0;
v22 = v20 == 0;
if ( v20 )
{
v23 = asc_402060;
v24 = 2LL;
v25 = v5;
do
{
if ( !v24 )
break;
v21 = *v25 < *(_BYTE *)v23;
v22 = *v25++ == *(_BYTE *)v23;
v23 = (const __int16 *)((char *)v23 + 1);
--v24;
}
while ( v22 );
v26 = (!v21 && !v22) - v21;
v27 = 0;
v28 = v26 == 0;
if ( v26 )
{
v29 = (_BYTE *)(asc_402060 + 2);
v30 = 2LL;
v31 = v5;
do
{
if ( !v30 )
break;
v27 = *v31 < (unsigned int)*v29;
v28 = *v31++ == *v29++;
--v30;
}
while ( v28 );
v32 = (!v27 && !v28) - v27;
v33 = 0;
v34 = v32 == 0;
if ( v32 )
{
v35 = (_BYTE *)(asc_402060 + 4);
v36 = 2LL;
v37 = v5;
do
{
if ( !v36 )
break;
v33 = *v37 < (unsigned int)*v35;
v34 = *v37++ == *v35++;
--v36;
}
while ( v34 );
v38 = (!v33 && !v34) - v33;
v39 = 0;
v40 = v38 == 0;
if ( v38 )
{
v41 = (_BYTE *)(asc_402060 + 6);
v42 = 2LL;
v43 = v5;
do
{
if ( !v42 )
break;
v39 = *v43 < (unsigned int)*v41;
v40 = *v43++ == *v41++;
--v42;
}
while ( v40 );
v44 = (!v39 && !v40) - v39;
v45 = 0;
v46 = v44 == 0;
if ( v44 )
{
v47 = (_BYTE *)(asc_402060 + 8);
v48 = 2LL;
v49 = v5;
do
{
if ( !v48 )
break;
v45 = *v49 < (unsigned int)*v47;
v46 = *v49++ == *v47++;
--v48;
}
while ( v46 );
v50 = (char)((!v45 && !v46) - v45);
v51 = 0;
v52 = v50 == 0;
if ( v50 )
{
v53 = "print";
v54 = 6LL;
v55 = v5;
do
{
if ( !v54 )
break;
v51 = *v55 < (unsigned int)*v53;
v52 = *v55++ == *v53++;
--v54;
}
while ( v52 );
if ( (!v51 && !v52) == v51 )
{
v56 = stack_pop(a1, (char)v55);
v57 = (const char *)base_str(v56, base);
result = printf(">> %s\n", v57);
}
else
{
v74 = read_base(v5, base);
result = (unsigned int)stack_push(a1, v74);
}
}
else
{
v71 = stack_pop(a1, (char)v49);
v72 = stack_pop(a1, (char)v49);
v73 = v72;
if ( v71 > 0 )
{
while ( v71 != ++v50 )
{
if ( v73 )
v73 *= v72;
else
v73 = v72;
}
}
else
{
v73 = 0;
}
result = (unsigned int)stack_push(a1, v73);
}
}
else
{
v69 = stack_pop(a1, (char)v43);
v70 = stack_pop(a1, (char)v43);
result = (unsigned int)stack_push(a1, v70 * v69);
}
}
else
{
v67 = stack_pop(a1, (char)v37);
v68 = stack_pop(a1, (char)v37);
result = (unsigned int)stack_push(a1, v68 / v67);
}
}
else
{
v65 = stack_pop(a1, (char)v31);
v66 = stack_pop(a1, (char)v31);
result = (unsigned int)stack_push(a1, v66 - v65);
}
}
else
{
v63 = stack_pop(a1, (char)v25);
v64 = stack_pop(a1, (char)v25);
result = (unsigned int)stack_push(a1, v64 % v63);
}
}
else
{
v61 = stack_pop(a1, (char)v19);
v62 = stack_pop(a1, (char)v19);
result = (unsigned int)stack_push(a1, v61 + v62);
}
}
else
{
v59 = stack_pop(a1, (char)v13);
v60 = stack_pop(a1, (char)v13);
stack_push(a1, v59);
result = (unsigned int)stack_push(a1, v60);
}
}
else
{
result = stack_pop(a1, (char)a2);
base = result;
if ( result <= 0 )
fail_exit("invalid base '%d'. Base must be greater than 0", result);
}
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1, const char *a2)
{
bool v2; // cf
bool v3; // zf
const char *v5; // rax
const char *v6; // rdi
__int64 v7; // rcx
char v8; // dl
bool v9; // cf
bool v10; // zf
const char *v11; // rdi
__int64 v12; // rcx
const char *v13; // rsi
char v14; // dl
bool v15; // cf
bool v16; // zf
const char *v17; // rdi
__int64 v18; // rcx
const char *v19; // rsi
char v20; // dl
bool v21; // cf
bool v22; // zf
const __int16 *v23; // rdi
__int64 v24; // rcx
const char *v25; // rsi
char v26; // dl
bool v27; // cf
bool v28; // zf
const char *v29; // rdi
__int64 v30; // rcx
const char *v31; // rsi
char v32; // dl
bool v33; // cf
bool v34; // zf
const char *v35; // rdi
__int64 v36; // rcx
const char *v37; // rsi
char v38; // dl
bool v39; // cf
bool v40; // zf
const char *v41; // rdi
__int64 v42; // rcx
const char *v43; // rsi
char v44; // dl
bool v45; // cf
bool v46; // zf
const char *v47; // rdi
__int64 v48; // rcx
const char *v49; // rsi
int v50; // ebp
bool v51; // cf
bool v52; // zf
const char *v53; // rdi
__int64 v54; // rcx
const char *v55; // rsi
int v56; // eax
const char *v57; // rax
int result; // eax
int v59; // er12
int v60; // ebp
int v61; // ebp
int v62; // eax
int v63; // ebp
int v64; // eax
int v65; // ebp
int v66; // eax
int v67; // ebp
int v68; // eax
int v69; // ebp
int v70; // eax
int v71; // er12
int v72; // eax
int v73; // esi
int v74; // eax

v5 = a2;
v6 = "set-base";
v7 = 9LL;
do
{
if ( !v7 )
break;
v2 = *a2 < (unsigned int)*v6;
v3 = *a2++ == *v6++;
--v7;
}
while ( v3 );
v8 = (!v2 && !v3) - v2;
v9 = 0;
v10 = v8 == 0;
if ( v8 )
{
v11 = "swap";
v12 = 5LL;
v13 = v5;
do
{
if ( !v12 )
break;
v9 = *v13 < (unsigned int)*v11;
v10 = *v13++ == *v11++;
--v12;
}
while ( v10 );
v14 = (!v9 && !v10) - v9;
v15 = 0;
v16 = v14 == 0;
if ( v14 )
{
v17 = "+";
v18 = 2LL;
v19 = v5;
do
{
if ( !v18 )
break;
v15 = *v19 < (unsigned int)*v17;
v16 = *v19++ == *v17++;
--v18;
}
while ( v16 );
v20 = (!v15 && !v16) - v15;
v21 = 0;
v22 = v20 == 0;
if ( v20 )
{
v23 = asc_402060;
v24 = 2LL;
v25 = v5;
do
{
if ( !v24 )
break;
v21 = *v25 < *(_BYTE *)v23;
v22 = *v25++ == *(_BYTE *)v23;
v23 = (const __int16 *)((char *)v23 + 1);
--v24;
}
while ( v22 );
v26 = (!v21 && !v22) - v21;
v27 = 0;
v28 = v26 == 0;
if ( v26 )
{
v29 = (_BYTE *)(asc_402060 + 2);
v30 = 2LL;
v31 = v5;
do
{
if ( !v30 )
break;
v27 = *v31 < (unsigned int)*v29;
v28 = *v31++ == *v29++;
--v30;
}
while ( v28 );
v32 = (!v27 && !v28) - v27;
v33 = 0;
v34 = v32 == 0;
if ( v32 )
{
v35 = (_BYTE *)(asc_402060 + 4);
v36 = 2LL;
v37 = v5;
do
{
if ( !v36 )
break;
v33 = *v37 < (unsigned int)*v35;
v34 = *v37++ == *v35++;
--v36;
}
while ( v34 );
v38 = (!v33 && !v34) - v33;
v39 = 0;
v40 = v38 == 0;
if ( v38 )
{
v41 = (_BYTE *)(asc_402060 + 6);
v42 = 2LL;
v43 = v5;
do
{
if ( !v42 )
break;
v39 = *v43 < (unsigned int)*v41;
v40 = *v43++ == *v41++;
--v42;
}
while ( v40 );
v44 = (!v39 && !v40) - v39;
v45 = 0;
v46 = v44 == 0;
if ( v44 )
{
v47 = (_BYTE *)(asc_402060 + 8);
v48 = 2LL;
v49 = v5;
do
{
if ( !v48 )
break;
v45 = *v49 < (unsigned int)*v47;
v46 = *v49++ == *v47++;
--v48;
}
while ( v46 );
v50 = (char)((!v45 && !v46) - v45);
v51 = 0;
v52 = v50 == 0;
if ( v50 )
{
v53 = "print";
v54 = 6LL;
v55 = v5;
do
{
if ( !v54 )
break;
v51 = *v55 < (unsigned int)*v53;
v52 = *v55++ == *v53++;
--v54;
}
while ( v52 );
if ( (!v51 && !v52) == v51 )
{
v56 = func9(a1, (char)v55);
v57 = (const char *)func11(v56, base);
result = printf(">> %s\n", v57);
}
else
{
v74 = func12(v5, base);
result = (unsigned int)func8(a1, v74);
}
}
else
{
v71 = func9(a1, (char)v49);
v72 = func9(a1, (char)v49);
v73 = v72;
if ( v71 > 0 )
{
while ( v71 != ++v50 )
{
if ( v73 )
v73 *= v72;
else
v73 = v72;
}
}
else
{
v73 = 0;
}
result = (unsigned int)func8(a1, v73);
}
}
else
{
v69 = func9(a1, (char)v43);
v70 = func9(a1, (char)v43);
result = (unsigned int)func8(a1, v70 * v69);
}
}
else
{
v67 = func9(a1, (char)v37);
v68 = func9(a1, (char)v37);
result = (unsigned int)func8(a1, v68 / v67);
}
}
else
{
v65 = func9(a1, (char)v31);
v66 = func9(a1, (char)v31);
result = (unsigned int)func8(a1, v66 - v65);
}
}
else
{
v63 = func9(a1, (char)v25);
v64 = func9(a1, (char)v25);
result = (unsigned int)func8(a1, v64 % v63);
}
}
else
{
v61 = func9(a1, (char)v19);
v62 = func9(a1, (char)v19);
result = (unsigned int)func8(a1, v61 + v62);
}
}
else
{
v59 = func9(a1, (char)v13);
v60 = func9(a1, (char)v13);
func8(a1, v59);
result = (unsigned int)func8(a1, v60);
}
}
else
{
result = func9(a1, (char)a2);
base = result;
if ( result <= 0 )
func14("invalid base '%d'. Base must be greater than 0", result);
}
return result;
}

original_code:
void evaluate(IntStack* stack, char* word) {
  if(strcmp(word, "set-base") == 0) {
    base = stack_pop(stack);
    if(base < 1) fail_exit("invalid base '%d'. Base must be greater than 0", base);
  } else if(strcmp(word, "swap") == 0) {
    int a = stack_pop(stack);
    int b = stack_pop(stack);
    stack_push(stack, a);
    stack_push(stack, b);
  } else if(strcmp(word, "+") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a + b;
    stack_push(stack, result);
  } else if(strcmp(word, "%") == 0) {
    int a = stack_pop(stack);
    int b = stack_pop(stack);
    int result = b % a;
    stack_push(stack, result);
  } else if(strcmp(word, "-") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a - b;
    stack_push(stack, result);
  } else if(strcmp(word, "/") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a / b;
    stack_push(stack, result);
  } else if(strcmp(word, "*") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a * b;
    stack_push(stack, result);
  } else if(strcmp(word, "^") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = 0;
    for(int jj = 0; jj < b; ++jj) {
      if(result == 0) {
        result = a;
      } else {
        result *= a;
      }
    }
    stack_push(stack, result);
  } else if(strcmp(word, "print") == 0) {
    int value = stack_pop(stack);
    printf(">> %s\n", base_str(value, base));
  } else {
    int value = read_base(word, base);
    stack_push(stack, value);
  }
}

canonical_original_code:
void evaluate(struct IntStack *stack, char *word) {
  if(strcmp(word, "set-base") == 0) {
    base = stack_pop(stack);
    if(base < 1) fail_exit("invalid base '%d'. Base must be greater than 0", base);
  } else if(strcmp(word, "swap") == 0) {
    int a = stack_pop(stack);
    int b = stack_pop(stack);
    stack_push(stack, a);
    stack_push(stack, b);
  } else if(strcmp(word, "+") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a + b;
    stack_push(stack, result);
  } else if(strcmp(word, "%") == 0) {
    int a = stack_pop(stack);
    int b = stack_pop(stack);
    int result = b % a;
    stack_push(stack, result);
  } else if(strcmp(word, "-") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a - b;
    stack_push(stack, result);
  } else if(strcmp(word, "/") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a / b;
    stack_push(stack, result);
  } else if(strcmp(word, "*") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = a * b;
    stack_push(stack, result);
  } else if(strcmp(word, "^") == 0) {
    int b = stack_pop(stack);
    int a = stack_pop(stack);
    int result = 0;
    for(int jj = 0; jj < b; ++jj) {
      if(result == 0) {
        result = a;
      } else {
        result *= a;
      }
    }
    stack_push(stack, result);
  } else if(strcmp(word, "print") == 0) {
    int value = stack_pop(stack);
    printf(">> %s\n", base_str(value, base));
  } else {
    int value = read_base(word, base);
    stack_push(stack, value);
  }
}

variable_types: a int

variable_types: b int

variable_types: result int

variable_types: jj int


canonical_decompiled_code:
void __noreturn func14(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
fflush(stderr);
exit(1);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE v4[16]; // [rsp+0h] [rbp-58h] BYREF
char v5[72]; // [rsp+10h] [rbp-48h] BYREF

if ( argc > 1 )
fail_exit("usage: %s (no arguments, all input via stdin)", (char)*argv);
stack_init((__int64)v4);
while ( (int)__isoc99_fscanf() > 0 )
evaluate((__int64)v4, v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE v4[16]; // [rsp+0h] [rbp-58h] BYREF
char v5[72]; // [rsp+10h] [rbp-48h] BYREF

if ( argc > 1 )
func14("usage: %s (no arguments, all input via stdin)", (char)*argv);
func7((__int64)v4);
while ( (int)__isoc99_fscanf() > 0 )
func13((__int64)v4, v5);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
  char word[64];
  IntStack stack;
  if(argc > 1) fail_exit("usage: %s (no arguments, all input via stdin)", argv[0]);
  stack_init(&stack);
  while(fscanf(stdin, "%s", word) > 0) {
    evaluate(&stack, word);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  char word[64];
  struct IntStack stack;
  if(argc > 1) fail_exit("usage: %s (no arguments, all input via stdin)", argv[0]);
  stack_init(&stack);
  while(fscanf(stdin, "%s", word) > 0) {
    evaluate(&stack, word);
  }
  return 0;
}

variable_types:
argv
char *[]

variable_types:
word
char [64]

variable_types: struct IntStack { int *values; int top; int max; }


repo: netik/tcpdstat

decompiled_code:
void __fastcall __noreturn sigint_handler(unsigned int a1)
{
fprintf(stderr, "got signal %d. exiting...\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(unsigned int a1)
{
fprintf(stderr, "got signal %d. exiting...\n", a1);
exit(1);
}

original_code:
static void
sigint_handler(int sig)
{
    fprintf(stderr, "got signal %d. exiting...\n", sig);
    exit(1);
}

variable_types: sig int


usage

decompiled_code:
void __noreturn usage()
{
fwrite("usage: tcpdstat [-dn] [-c count] [-w len_file] [dumpfile]\n", 1uLL, 0x3AuLL, stderr);
fwrite(" -d: debug\n", 1uLL, 0x16uLL, stderr);
fwrite(" -n: no flow info\n", 1uLL, 0x1DuLL, stderr);
fwrite(" -c: exit after \"count\" packets\n", 1uLL, 0x2BuLL, stderr);
fwrite(" -l: write packet length distributions to a file\n", 1uLL, 0x3CuLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func1()
{
fwrite("usage: tcpdstat [-dn] [-c count] [-w len_file] [dumpfile]\n", 1uLL, 0x3AuLL, stderr);
fwrite(" -d: debug\n", 1uLL, 0x16uLL, stderr);
fwrite(" -n: no flow info\n", 1uLL, 0x1DuLL, stderr);
fwrite(" -c: exit after \"count\" packets\n", 1uLL, 0x2BuLL, stderr);
fwrite(" -l: write packet length distributions to a file\n", 1uLL, 0x3CuLL, stderr);
exit(1);
}

original_code:
static void
usage(void)
{
    fprintf(stderr, "usage: tcpdstat [-dn] [-c count] [-w len_file] [dumpfile]\n");
    fprintf(stderr, "            -d: debug\n");
    fprintf(stderr, "            -n: no flow info\n");
    fprintf(stderr, "            -c: exit after \"count\" packets\n");
    fprintf(stderr, "            -l: write packet length distributions to a file\n");
    exit(1);
}

canonical_original_code:
static void usage()
{
    fprintf(stderr, "usage: tcpdstat [-dn] [-c count] [-w len_file] [dumpfile]\n");
    fprintf(stderr, "            -d: debug\n");
    fprintf(stderr, "            -n: no flow info\n");
    fprintf(stderr, "            -c: exit after \"count\" packets\n");
    fprintf(stderr, "            -l: write packet length distributions to a file\n");
    exit(1);
}


decompiled_code:
int pktsize_print()
{
__int64 i; // rbx
int v1; // eax

puts("### Packet Size Distribution (including MAC headers) ###");
puts("<<<<");
for ( i = 0LL; i != 16; ++i )
{
v1 = pktsize_buckets[i];
if ( v1 > 0 )
printf(" [%5d-%5d]: %10d\n", (unsigned int)(1 << i), (unsigned int)((1 << (i + 1)) - 1), (unsigned int)v1);
}
puts(">>>>");
return putchar(10);
}

canonical_decompiled_code:
int func3()
{
__int64 i; // rbx
int v1; // eax

puts("### Packet Size Distribution (including MAC headers) ###");
puts("<<<<");
for ( i = 0LL; i != 16; ++i )
{
v1 = pktsize_buckets[i];
if ( v1 > 0 )
printf(" [%5d-%5d]: %10d\n", (unsigned int)(1 << i), (unsigned int)((1 << (i + 1)) - 1), (unsigned int)v1);
}
puts(">>>>");
return putchar(10);
}

original_code:
static void pktsize_print(void)
{
    int i;
    printf("### Packet Size Distribution (including MAC headers) ###\n");
    printf("<<<<\n");
    for (i = 0; i < 16; i++)
 if (pktsize_buckets[i] > 0)
     printf(" [%5d-%5d]: %10d\n",
     1 << i, (1 << (i+1))-1, pktsize_buckets[i]);
    printf(">>>>\n");
    printf("\n");
}

canonical_original_code:
static void pktsize_print()
{
    int i;
    printf("### Packet Size Distribution (including MAC headers) ###\n");
    printf("<<<<\n");
    for (i = 0; i < 16; i++)
 if (pktsize_buckets[i] > 0)
     printf(" [%5d-%5d]: %10d\n",
     1 << i, (1 << (i+1))-1, pktsize_buckets[i]);
    printf(">>>>\n");
    printf("\n");
}

variable_types: i int


decompiled_code:
int show_stat()
{
struct tm *v0; // rax
char *v1; // rax
char *v2; // rax
__int64 v3; // rax
double v4; // xmm1_8
double v5; // rbx
__int64 i; // rbx
__int64 v7; // rcx
double v8; // xmm2_8
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = start_time;
v0 = localtime(&timer);
printf(
"Id: %.4d%.2d%.2d%.2d%.2d\n",
(unsigned int)(v0->tm_year + 1900),
(unsigned int)(v0->tm_mon + 1),
(unsigned int)v0->tm_mday,
(unsigned int)v0->tm_hour,
(unsigned int)v0->tm_min);
v1 = ctime(&timer);
printf("StartTime: %s", v1);
timer = end_time;
v2 = ctime(&timer);
printf("EndTime: %s", v2);
printf(
"TotalTime: %.2f seconds\n",
(double)(*((_DWORD *)&end_time + 2) - *((_DWORD *)&start_time + 2)) / 1000000.0
+ (double)((int)end_time - (int)start_time));
printf("TotalCapSize: %.2fMB CapLen: %d bytes\n", (double)(int)caplen_total * 0.00000095367431640625, caplen_max);
printf("# of packets: %d", LODWORD(tcpdstat[0]));
v3 = tcpdstat[1];
if ( v3 <= 0x100000 )
printf(" (%.2fKB)\n", (double)(int)v3 * 0.0009765625);
else
printf(" (%.2fMB)\n", (double)(int)v3 * 0.00000095367431640625);
v4 = *(double *)&rate_var / (double)((int)rate_count - 1);
if ( v4 < 0.0 )
v5 = sqrt(v4);
else
v5 = sqrt(v4);
printf("AvgRate: ");
if ( *(double *)&rate_mean > 1000000.0 )
{
printf("%.2fMbps stddev:%.2fM ", *(double *)&rate_mean / 1000000.0, v5 / 1000000.0);
}
else if ( *(double *)&rate_mean <= 1000.0 )
{
printf("%.2fbps stddev:%.2f ", *(double *)&rate_mean, v5);
}
else
{
printf("%.2fKbps stddev:%.2fK ", *(double *)&rate_mean / 1000.0, v5 / 1000.0);
}
printf("PeakRate: ");
if ( *(double *)&rate_max > 1000000.0 )
{
printf("%.2fMbps\n", *(double *)&rate_max / 1000000.0);
}
else if ( *(double *)&rate_max <= 1000.0 )
{
printf("%.2fbps\n", *(double *)&rate_max);
}
else
{
printf("%.2fKbps\n", *(double *)&rate_max / 1000.0);
}
putchar(10);
if ( use_ipflow )
ipflow_show();
pktsize_print();
puts("\n### Protocol Breakdown ###");
puts("<<<<");
puts(" protocol\t\tpackets\t\t\tbytes\t\tbytes/pkt");
puts("------------------------------------------------------------------------");
for ( i = 0LL; i != 760; i += 2LL )
{
v7 = LODWORD(tcpdstat[i]);
if ( (_DWORD)v7 )
{
v8 = (double)(int)tcpdstat[i + 1];
printf(
"[%d] %s %12d (%6.2f%%) %16lld (%6.2f%%) %8.2f\n",
LODWORD((&protos)[i + 1]),
(&protos)[i],
v7,
(double)(int)v7 / (double)SLODWORD(tcpdstat[0]) * 100.0,
tcpdstat[i + 1],
v8 / (double)(int)tcpdstat[1] * 100.0,
v8 / (double)(int)v7);
}
}
puts(">>>>");
return puts("\n");
}

canonical_decompiled_code:
int func4()
{
struct tm *v0; // rax
char *v1; // rax
char *v2; // rax
__int64 v3; // rax
double v4; // xmm1_8
double v5; // rbx
__int64 i; // rbx
__int64 v7; // rcx
double v8; // xmm2_8
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = start_time;
v0 = localtime(&timer);
printf(
"Id: %.4d%.2d%.2d%.2d%.2d\n",
(unsigned int)(v0->tm_year + 1900),
(unsigned int)(v0->tm_mon + 1),
(unsigned int)v0->tm_mday,
(unsigned int)v0->tm_hour,
(unsigned int)v0->tm_min);
v1 = ctime(&timer);
printf("StartTime: %s", v1);
timer = end_time;
v2 = ctime(&timer);
printf("EndTime: %s", v2);
printf(
"TotalTime: %.2f seconds\n",
(double)(*((_DWORD *)&end_time + 2) - *((_DWORD *)&start_time + 2)) / 1000000.0
+ (double)((int)end_time - (int)start_time));
printf("TotalCapSize: %.2fMB CapLen: %d bytes\n", (double)(int)caplen_total * 0.00000095367431640625, caplen_max);
printf("# of packets: %d", LODWORD(tcpdstat[0]));
v3 = tcpdstat[1];
if ( v3 <= 0x100000 )
printf(" (%.2fKB)\n", (double)(int)v3 * 0.0009765625);
else
printf(" (%.2fMB)\n", (double)(int)v3 * 0.00000095367431640625);
v4 = *(double *)&rate_var / (double)((int)rate_count - 1);
if ( v4 < 0.0 )
v5 = sqrt(v4);
else
v5 = sqrt(v4);
printf("AvgRate: ");
if ( *(double *)&rate_mean > 1000000.0 )
{
printf("%.2fMbps stddev:%.2fM ", *(double *)&rate_mean / 1000000.0, v5 / 1000000.0);
}
else if ( *(double *)&rate_mean <= 1000.0 )
{
printf("%.2fbps stddev:%.2f ", *(double *)&rate_mean, v5);
}
else
{
printf("%.2fKbps stddev:%.2fK ", *(double *)&rate_mean / 1000.0, v5 / 1000.0);
}
printf("PeakRate: ");
if ( *(double *)&rate_max > 1000000.0 )
{
printf("%.2fMbps\n", *(double *)&rate_max / 1000000.0);
}
else if ( *(double *)&rate_max <= 1000.0 )
{
printf("%.2fbps\n", *(double *)&rate_max);
}
else
{
printf("%.2fKbps\n", *(double *)&rate_max / 1000.0);
}
putchar(10);
if ( use_ipflow )
ipflow_show();
func3();
puts("\n### Protocol Breakdown ###");
puts("<<<<");
puts(" protocol\t\tpackets\t\t\tbytes\t\tbytes/pkt");
puts("------------------------------------------------------------------------");
for ( i = 0LL; i != 760; i += 2LL )
{
v7 = LODWORD(tcpdstat[i]);
if ( (_DWORD)v7 )
{
v8 = (double)(int)tcpdstat[i + 1];
printf(
"[%d] %s %12d (%6.2f%%) %16lld (%6.2f%%) %8.2f\n",
LODWORD((&protos)[i + 1]),
(&protos)[i],
v7,
(double)(int)v7 / (double)SLODWORD(tcpdstat[0]) * 100.0,
tcpdstat[i + 1],
v8 / (double)(int)tcpdstat[1] * 100.0,
v8 / (double)(int)v7);
}
}
puts(">>>>");
return puts("\n");
}

original_code:
static void show_stat(void)
{
    int i;
    time_t t;
    struct tm *tm;
    double sec, stddev;
    double frac_pkts, frac_bytes, avg_bytes;
    t = start_time.tv_sec;
    tm = localtime(&t);
    printf("Id: %.4d%.2d%.2d%.2d%.2d\n", tm->tm_year + 1900, tm->tm_mon + 1,
    tm->tm_mday, tm->tm_hour, tm->tm_min);
    printf("StartTime: %s", ctime(&t));
    t = end_time.tv_sec;
    printf("EndTime:   %s", ctime(&t));
    sec = (double)(end_time.tv_sec - start_time.tv_sec)
     + (double)(end_time.tv_usec - start_time.tv_usec) / 1000000.0;
    printf("TotalTime: %.2f seconds\n", sec);
    printf("TotalCapSize: %.2fMB  CapLen: %d bytes\n",
    (double)caplen_total/(1024*1024), caplen_max);
    printf("# of packets: %d", tcpdstat[TOTAL].packets);
    if (tcpdstat[TOTAL].bytes > 1024*1024)
     printf(" (%.2fMB)\n",
     (double)tcpdstat[TOTAL].bytes/(1024*1024));
    else
     printf(" (%.2fKB)\n", (double)tcpdstat[TOTAL].bytes / 1024);
    stddev = sqrt(rate_var / (rate_count - 1));
    printf("AvgRate: ");
    if (rate_mean > 1000000.0)
     printf("%.2fMbps  stddev:%.2fM   ",
     rate_mean / 1000000.0, stddev / 1000000.0);
    else if (rate_mean > 1000.0)
     printf("%.2fKbps  stddev:%.2fK   ",
     rate_mean / 1000.0, stddev / 1000.0);
    else
     printf("%.2fbps  stddev:%.2f   ",
     rate_mean, stddev);
    printf("PeakRate: ");
    if (rate_max > 1000000.0)
     printf("%.2fMbps\n",
     rate_max / 1000000.0);
    else if (rate_max > 1000.0)
     printf("%.2fKbps\n",
     rate_max / 1000.0);
    else
     printf("%.2fbps\n",
     rate_max);
    printf("\n");
    if (use_ipflow)
     ipflow_show();
    pktsize_print();
    printf("\n### Protocol Breakdown ###\n");
    printf("<<<<\n");
    printf("     protocol\t\tpackets\t\t\tbytes\t\tbytes/pkt\n");
    printf("------------------------------------------------------------------------\n");
    for (i = 0; i < PROTOTYPE_MAX; i++) {
 if (tcpdstat[i].packets != 0) {
     frac_pkts = (double)tcpdstat[i].packets
  / (double)tcpdstat[TOTAL].packets * 100.0;
     frac_bytes = (double)tcpdstat[i].bytes
  / (double)tcpdstat[TOTAL].bytes * 100.0;
     avg_bytes = (double)tcpdstat[i].bytes
  / (double)tcpdstat[i].packets;
     printf("[%d] %s %12d (%6.2f%%) %16lld (%6.2f%%)  %8.2f\n",
     protos[i].level,
     protos[i].name,
     tcpdstat[i].packets, frac_pkts,
     tcpdstat[i].bytes, frac_bytes,
     avg_bytes);
 }
    }
    printf(">>>>\n");
    printf("\n\n");
}

canonical_original_code:
static void show_stat()
{
    int i;
    long t;
    struct tm *tm;
    double sec; double stddev;
    double frac_pkts; double frac_bytes; double avg_bytes;
    t = start_time.tv_sec;
    tm = localtime(&t);
    printf("Id: %.4d%.2d%.2d%.2d%.2d\n", tm->tm_year + 1900, tm->tm_mon + 1,
    tm->tm_mday, tm->tm_hour, tm->tm_min);
    printf("StartTime: %s", ctime(&t));
    t = end_time.tv_sec;
    printf("EndTime:   %s", ctime(&t));
    sec = (double)(end_time.tv_sec - start_time.tv_sec)
     + (double)(end_time.tv_usec - start_time.tv_usec) / 1000000.0;
    printf("TotalTime: %.2f seconds\n", sec);
    printf("TotalCapSize: %.2fMB  CapLen: %d bytes\n",
    (double)caplen_total/(1024*1024), caplen_max);
    printf("# of packets: %d", tcpdstat[TOTAL].packets);
    if (tcpdstat[TOTAL].bytes > 1024*1024)
     printf(" (%.2fMB)\n",
     (double)tcpdstat[TOTAL].bytes/(1024*1024));
    else
     printf(" (%.2fKB)\n", (double)tcpdstat[TOTAL].bytes / 1024);
    stddev = sqrt(rate_var / (rate_count - 1));
    printf("AvgRate: ");
    if (rate_mean > 1000000.0)
     printf("%.2fMbps  stddev:%.2fM   ",
     rate_mean / 1000000.0, stddev / 1000000.0);
    else if (rate_mean > 1000.0)
     printf("%.2fKbps  stddev:%.2fK   ",
     rate_mean / 1000.0, stddev / 1000.0);
    else
     printf("%.2fbps  stddev:%.2f   ",
     rate_mean, stddev);
    printf("PeakRate: ");
    if (rate_max > 1000000.0)
     printf("%.2fMbps\n",
     rate_max / 1000000.0);
    else if (rate_max > 1000.0)
     printf("%.2fKbps\n",
     rate_max / 1000.0);
    else
     printf("%.2fbps\n",
     rate_max);
    printf("\n");
    if (use_ipflow)
     ipflow_show();
    pktsize_print();
    printf("\n### Protocol Breakdown ###\n");
    printf("<<<<\n");
    printf("     protocol\t\tpackets\t\t\tbytes\t\tbytes/pkt\n");
    printf("------------------------------------------------------------------------\n");
    for (i = 0; i < PROTOTYPE_MAX; i++) {
 if (tcpdstat[i].packets != 0) {
     frac_pkts = (double)tcpdstat[i].packets
  / (double)tcpdstat[TOTAL].packets * 100.0;
     frac_bytes = (double)tcpdstat[i].bytes
  / (double)tcpdstat[TOTAL].bytes * 100.0;
     avg_bytes = (double)tcpdstat[i].bytes
  / (double)tcpdstat[i].packets;
     printf("[%d] %s %12d (%6.2f%%) %16lld (%6.2f%%)  %8.2f\n",
     protos[i].level,
     protos[i].name,
     tcpdstat[i].packets, frac_pkts,
     tcpdstat[i].bytes, frac_bytes,
     avg_bytes);
 }
    }
    printf(">>>>\n");
    printf("\n\n");
}

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: sec double

variable_types: stddev double


decompiled_code:
__int64 __fastcall pktsize_add(int a1)
{
int v1; // eax
__int64 result; // rax

if ( pktlen_file )
{
v1 = 9180;
if ( a1 <= 9180 )
v1 = a1;
++pktlen_dist[v1];
}
if ( a1 <= 1 )
{
LODWORD(result) = 0;
}
else
{
LODWORD(result) = 0;
do
{
a1 >>= 1;
LODWORD(result) = result + 1;
}
while ( a1 > 1 );
}
if ( (int)result > 15 )
LODWORD(result) = 15;
result = (int)result;
++pktsize_buckets[(int)result];
return result;
}

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

if ( pktlen_file )
{
v1 = 9180;
if ( a1 <= 9180 )
v1 = a1;
++pktlen_dist[v1];
}
if ( a1 <= 1 )
{
LODWORD(result) = 0;
}
else
{
LODWORD(result) = 0;
do
{
a1 >>= 1;
LODWORD(result) = result + 1;
}
while ( a1 > 1 );
}
if ( (int)result > 15 )
LODWORD(result) = 15;
result = (int)result;
++pktsize_buckets[(int)result];
return result;
}

original_code:
void pktsize_add(int size)
{
    int n = 0;
    if (pktlen_file != ((void *)0)) {
 int len = size;
 if (len > 9180)
     len = 9180;
 pktlen_dist[len]++;
    }
    while (size > 1) {
 size >>= 1;
 n++;
    }
    if (n >= 16)
 n = 16 - 1;
    pktsize_buckets[n]++;
}

variable_types: size int

variable_types: n int

variable_types: len int