repo: vianney/tmux-applet

decompiled_code:
int __fastcall print_size(unsigned __int64 a1, unsigned int a2)
{
if ( a2 > 4 )
__assert_fail("magnitude >= 0 && magnitude <= 4", "tmux-applet.c", 0x4Du, "print_size");
if ( a2 != 4 )
{
while ( a1 > 0x3FF )
{
++a2;
if ( a1 <= 0x27FF )
return printf("#[bright]%llu.%llu#[nobright]%c", a1 >> 10, (a1 & 0x3FF) / 0x67, (unsigned int)SIZE_SUFFIXES[a2]);
a1 >>= 10;
if ( a2 == 4 )
return printf("#[bright]%llu#[nobright]%c", a1, (unsigned int)SIZE_SUFFIXES[a2]);
}
}
return printf("#[bright]%llu#[nobright]%c", a1, (unsigned int)SIZE_SUFFIXES[a2]);
}

canonical_decompiled_code:
int __fastcall func7(unsigned __int64 a1, unsigned int a2)
{
if ( a2 > 4 )
__assert_fail("magnitude >= 0 && magnitude <= 4", "tmux-applet.c", 0x4Du, "print_size");
if ( a2 != 4 )
{
while ( a1 > 0x3FF )
{
++a2;
if ( a1 <= 0x27FF )
return printf("#[bright]%llu.%llu#[nobright]%c", a1 >> 10, (a1 & 0x3FF) / 0x67, (unsigned int)SIZE_SUFFIXES[a2]);
a1 >>= 10;
if ( a2 == 4 )
return printf("#[bright]%llu#[nobright]%c", a1, (unsigned int)SIZE_SUFFIXES[a2]);
}
}
return printf("#[bright]%llu#[nobright]%c", a1, (unsigned int)SIZE_SUFFIXES[a2]);
}

original_code:
void print_size(unsigned long long size, int magnitude) {
    ((void) sizeof ((magnitude >= 0 && magnitude <= 4) ? 1 : 0), __extension__ ({ if (magnitude >= 0 && magnitude <= 4) ; else __assert_fail ("magnitude >= 0 && magnitude <= 4", "tmux-applet.c", 77, __extension__ __PRETTY_FUNCTION__); }));
    if(magnitude == 4 || size < 1024) {
        printf("#[bright]%llu#[nobright]%c", size, SIZE_SUFFIXES[magnitude]);
    } else if(size < 10*1024) {
        printf("#[bright]%llu.%llu#[nobright]%c", size >> 10, (size & 1023) / 103,
                              SIZE_SUFFIXES[magnitude+1]);
    } else {
        print_size(size >> 10, magnitude + 1);
    }
}

variable_types:
size
unsigned long long

variable_types: magnitude int


decompiled_code:
int __fastcall begin_applet(const char *a1, const char *a2)
{
const char *v2; // rbp

v2 = a1;
if ( !*a1 )
v2 = a2;
if ( nbApplets )
printf(" ");
return printf("#[bright,%s]", v2);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1, const char *a2)
{
const char *v2; // rbp

v2 = a1;
if ( !*a1 )
v2 = a2;
if ( nbApplets )
printf(" ");
return printf("#[bright,%s]", v2);
}

original_code:
void begin_applet(const char* attr, const char* defattr) {
    if(attr[0] == '\0')
        attr = defattr;
    if(nbApplets != 0)
        printf("  ");
    printf("#[bright,%s]", attr);
}

canonical_original_code:
void begin_applet(char *attr, char *defattr) {
    if(attr[0] == '\0')
        attr = defattr;
    if(nbApplets != 0)
        printf("  ");
    printf("#[bright,%s]", attr);
}

variable_types:
attr
char *

variable_types:
defattr
char *

return_type: void


decompiled_code:
int end_applet()
{
int result; // eax

result = printf("#[default]");
++nbApplets;
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax

result = printf("#[default]");
++nbApplets;
return result;
}

original_code:
void end_applet() {
    printf("#[default]");
    nbApplets++;
}


rpcbufInit

repo: vicamo/cndrvcups-common

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

v1 = malloc(0x400uLL);
*(_QWORD *)a1 = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 8) = 1024LL;
result = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 24) = 0;
return result;
}

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

v1 = malloc(0x400uLL);
*(_QWORD *)a1 = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 8) = 1024LL;
result = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 24) = 0;
return result;
}

original_code:
static int rpcbufInit(RPCBuf *bp)
{
    if ((bp->body = malloc(1024)) == ((void *)0)) {
 return -1;
    }
    bp->size = 1024;
    bp->rIndex = bp->wIndex = 0;
    bp->pktTop = bp->pktIndex = 0;
    return 0;
}

canonical_original_code:
static int rpcbufInit(struct RPCBuf *bp)
{
    if ((bp->body = malloc(1024)) == ((void *)0)) {
 return -1;
    }
    bp->size = 1024;
    bp->rIndex = bp->wIndex = 0;
    bp->pktTop = bp->pktIndex = 0;
    return 0;
}

variable_types: struct RPCBuf { char *body; int size; int rIndex; int wIndex; int pktTop; int pktIndex; } *

return_type: int


original_code:
static int oprpc_flushBuffer(RPCHandle *hp, int len)
{
    int wlen = hp->outBuf.wIndex-hp->outBuf.rIndex;
    int mlen;
    while (wlen > 0) {
 int r;
 if ((r = write(hp->outFd,
    hp->outBuf.body+hp->outBuf.rIndex,wlen)) < 0) {
     if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11) continue;
     return -1;
 }
 wlen -= r;
 hp->outBuf.rIndex += r;
    }
    hp->outBuf.rIndex = hp->outBuf.wIndex = 0;
    mlen = len + hp->outBuf.pktIndex - hp->outBuf.pktTop;
    if (hp->outBuf.size < mlen) {
 char *p;
 if ((p = malloc(mlen)) == ((void *)0)) {
     return -1;
 }
 memcpy(p, hp->outBuf.body+hp->outBuf.pktTop,
   hp->outBuf.pktIndex-hp->outBuf.pktTop);
 free(hp->outBuf.body);
 hp->outBuf.size = mlen;
 hp->outBuf.body = p;
    } else {
 memmove(hp->outBuf.body, hp->outBuf.body+hp->outBuf.pktTop,
   hp->outBuf.pktIndex-hp->outBuf.pktTop);
    }
    hp->outBuf.pktIndex -= hp->outBuf.pktTop;
    hp->outBuf.pktTop = 0;
    return 0;
}

canonical_original_code:
static int oprpc_flushBuffer(struct RPCHandle *hp, int len)
{
    int wlen = hp->outBuf.wIndex-hp->outBuf.rIndex;
    int mlen;
    while (wlen > 0) {
 int r;
 if ((r = write(hp->outFd,
    hp->outBuf.body+hp->outBuf.rIndex,wlen)) < 0) {
     if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11) continue;
     return -1;
 }
 wlen -= r;
 hp->outBuf.rIndex += r;
    }
    hp->outBuf.rIndex = hp->outBuf.wIndex = 0;
    mlen = len + hp->outBuf.pktIndex - hp->outBuf.pktTop;
    if (hp->outBuf.size < mlen) {
 char *p;
 if ((p = malloc(mlen)) == ((void *)0)) {
     return -1;
 }
 memcpy(p, hp->outBuf.body+hp->outBuf.pktTop,
   hp->outBuf.pktIndex-hp->outBuf.pktTop);
 free(hp->outBuf.body);
 hp->outBuf.size = mlen;
 hp->outBuf.body = p;
    } else {
 memmove(hp->outBuf.body, hp->outBuf.body+hp->outBuf.pktTop,
   hp->outBuf.pktIndex-hp->outBuf.pktTop);
    }
    hp->outBuf.pktIndex -= hp->outBuf.pktTop;
    hp->outBuf.pktTop = 0;
    return 0;
}

variable_types: struct RPCHandle { int inFd; int outFd; struct RPCBuf { char *body; int size; int rIndex; int wIndex; int pktTop; int pktIndex; } inBuf; struct RPCBuf outBuf; int seqNo; void **shmemStack; int shmemStackSize; int shmemStackPointer; int lastPushSeqNo; } *

variable_types: len int

variable_types: wlen int

variable_types: mlen int

variable_types: r int

variable_types:
p
char *

user_defined_types: struct RPCHandle { int inFd; int outFd; struct RPCBuf inBuf; struct RPCBuf outBuf; int seqNo; void **shmemStack; int shmemStackSize; int shmemStackPointer; int lastPushSeqNo; }


original_code:
static int oprpc_fillBuffer(RPCHandle *hp, int len, int block)
{
    int mlen = hp->inBuf.wIndex - hp->inBuf.pktTop + len;
    if (hp->inBuf.size < mlen) {
 char *p;
 if ((p = malloc(mlen)) == ((void *)0)) {
     return -1;
 }
 memcpy(p, hp->inBuf.body+hp->inBuf.pktTop,
   hp->inBuf.wIndex-hp->inBuf.pktTop);
 free(hp->inBuf.body);
 hp->inBuf.size = mlen;
 hp->inBuf.body = p;
 hp->inBuf.pktIndex -= hp->inBuf.pktTop;
 hp->inBuf.rIndex -= hp->inBuf.pktTop;
 hp->inBuf.wIndex -= hp->inBuf.pktTop;
 hp->inBuf.pktTop = 0;
    } else if (hp->inBuf.size - hp->inBuf.wIndex < len) {
 memmove(hp->inBuf.body, hp->inBuf.body+hp->inBuf.pktTop,
   hp->inBuf.wIndex-hp->inBuf.pktTop);
 hp->inBuf.pktIndex -= hp->inBuf.pktTop;
 hp->inBuf.rIndex -= hp->inBuf.pktTop;
 hp->inBuf.wIndex -= hp->inBuf.pktTop;
 hp->inBuf.pktTop = 0;
    }
    while (len > 0) {
 int r;
 struct pollfd pfd;
 int rlen;
 pfd.fd = hp->inFd;
 pfd.events = 0x001 | 0x008;
 if ((r = poll(&pfd,1,block ? -1 : 0)) <= 0) {
     if (r < 0 && (*__errno_location ()) == 4) continue;
     return r;
 }
 if ((pfd.revents & 0x001) == 0) {
     return -1;
 }
 rlen = hp->inBuf.size - hp->inBuf.wIndex;
 if ((r = read(hp->inFd,
    hp->inBuf.body+hp->inBuf.wIndex,rlen)) <= 0) {
     if (r < 0 && ((*__errno_location ()) == 4|| (*__errno_location ()) == 11)) continue;
     return -1;
 }
 len -= r;
 hp->inBuf.wIndex += r;
    }
    return 1;
}

canonical_original_code:
static int oprpc_fillBuffer(struct RPCHandle *hp, int len, int block)
{
    int mlen = hp->inBuf.wIndex - hp->inBuf.pktTop + len;
    if (hp->inBuf.size < mlen) {
 char *p;
 if ((p = malloc(mlen)) == ((void *)0)) {
     return -1;
 }
 memcpy(p, hp->inBuf.body+hp->inBuf.pktTop,
   hp->inBuf.wIndex-hp->inBuf.pktTop);
 free(hp->inBuf.body);
 hp->inBuf.size = mlen;
 hp->inBuf.body = p;
 hp->inBuf.pktIndex -= hp->inBuf.pktTop;
 hp->inBuf.rIndex -= hp->inBuf.pktTop;
 hp->inBuf.wIndex -= hp->inBuf.pktTop;
 hp->inBuf.pktTop = 0;
    } else if (hp->inBuf.size - hp->inBuf.wIndex < len) {
 memmove(hp->inBuf.body, hp->inBuf.body+hp->inBuf.pktTop,
   hp->inBuf.wIndex-hp->inBuf.pktTop);
 hp->inBuf.pktIndex -= hp->inBuf.pktTop;
 hp->inBuf.rIndex -= hp->inBuf.pktTop;
 hp->inBuf.wIndex -= hp->inBuf.pktTop;
 hp->inBuf.pktTop = 0;
    }
    while (len > 0) {
 int r;
 struct pollfd pfd;
 int rlen;
 pfd.fd = hp->inFd;
 pfd.events = 0x001 | 0x008;
 if ((r = poll(&pfd,1,block ? -1 : 0)) <= 0) {
     if (r < 0 && (*__errno_location ()) == 4) continue;
     return r;
 }
 if ((pfd.revents & 0x001) == 0) {
     return -1;
 }
 rlen = hp->inBuf.size - hp->inBuf.wIndex;
 if ((r = read(hp->inFd,
    hp->inBuf.body+hp->inBuf.wIndex,rlen)) <= 0) {
     if (r < 0 && ((*__errno_location ()) == 4|| (*__errno_location ()) == 11)) continue;
     return -1;
 }
 len -= r;
 hp->inBuf.wIndex += r;
    }
    return 1;
}

variable_types: block int

variable_types: struct pollfd { int fd; short events; short revents; }

variable_types: rlen int

user_defined_types: struct RPCBuf { char *body; int size; int rIndex; int wIndex; int pktTop; int pktIndex; }


decompiled_code:
__int64 __fastcall oprpc_destroy(void *ptr)
{
rpcBufClean_isra_0((__int64)ptr + 8);
rpcBufClean_isra_0((__int64)ptr + 40);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *ptr)
{
func3((__int64)ptr + 8);
func3((__int64)ptr + 40);
free(ptr);
return 0LL;
}

original_code:
int oprpc_destroy(void *ap)
{
    RPCHandle *hp = ap;
    rpcBufClean(&hp->inBuf);
    rpcBufClean(&hp->outBuf);
    free(hp);
    return 0;
}

canonical_original_code:
int oprpc_destroy(void *ap)
{
    struct RPCHandle *hp = ap;
    rpcBufClean(&hp->inBuf);
    rpcBufClean(&hp->outBuf);
    free(hp);
    return 0;
}

variable_types:
ap
void *


decompiled_code:
__int64 __fastcall oprpc_putPkt(__int64 a1, const void *a2, int a3)
{
__int64 v5; // rdi

v5 = *(int *)(a1 + 64);
if ( *(_DWORD *)(a1 + 48) - (int)v5 >= a3 )
goto LABEL_4;
if ( (int)oprpc_flushBuffer(a1, a3) >= 0 )
{
v5 = *(int *)(a1 + 64);
LABEL_4:
memcpy((void *)(*(_QWORD *)(a1 + 40) + v5), a2, a3);
*(_DWORD *)(a1 + 64) += a3;
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int oprpc_putPkt(void *ap, const char *buf, int len)
{
    RPCHandle *hp = ap;
    if (hp->outBuf.size - hp->outBuf.pktIndex < len) {
 if (oprpc_flushBuffer(hp, len) < 0) {
     return -1;
 }
    }
    memcpy(hp->outBuf.body+hp->outBuf.pktIndex,buf,len);
    hp->outBuf.pktIndex += len;
    return 0;
}

canonical_original_code:
int oprpc_putPkt(void *ap, char *buf, int len)
{
    struct RPCHandle *hp = ap;
    if (hp->outBuf.size - hp->outBuf.pktIndex < len) {
 if (oprpc_flushBuffer(hp, len) < 0) {
     return -1;
 }
    }
    memcpy(hp->outBuf.body+hp->outBuf.pktIndex,buf,len);
    hp->outBuf.pktIndex += len;
    return 0;
}

variable_types:
buf
char *


decompiled_code:
__int64 __fastcall oprpc_putPktPointer(__int64 a1, const void *a2, int a3)
{
int v4; // eax
int v5; // edx
int v6; // eax
__int64 result; // rax
char v8[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a2 )
{
v8[0] = 0;
if ( (int)oprpc_putPkt(a1, v8, 1) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v4 = *(_DWORD *)(a1 + 64);
v5 = v4 + 6;
v6 = v4 + 3;
if ( v6 < 0 )
v6 = v5;
*(_DWORD *)(a1 + 64) = v6 & 0xFFFFFFFC;
result = (unsigned int)((int)oprpc_putPkt(a1, a2, a3) >> 31);
}
}
else
{
v8[0] = 2;
result = (unsigned int)((int)oprpc_putPkt(a1, v8, 1) >> 31);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const void *a2, int a3)
{
int v4; // eax
int v5; // edx
int v6; // eax
__int64 result; // rax
char v8[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( a2 )
{
v8[0] = 0;
if ( (int)func6(a1, v8, 1) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v4 = *(_DWORD *)(a1 + 64);
v5 = v4 + 6;
v6 = v4 + 3;
if ( v6 < 0 )
v6 = v5;
*(_DWORD *)(a1 + 64) = v6 & 0xFFFFFFFC;
result = (unsigned int)((int)func6(a1, a2, a3) >> 31);
}
}
else
{
v8[0] = 2;
result = (unsigned int)((int)func6(a1, v8, 1) >> 31);
}
return result;
}

original_code:
int oprpc_putPktPointer(void *ap, const void *p, int len)
{
    RPCHandle *hp = ap;
    char f;
    if (p == ((void *)0)) {
 f = 2;
 if (oprpc_putPkt(ap,&f,1) < 0) {
     return -1;
 }
 return 0;
    }
 f = 0;
 if (oprpc_putPkt(ap,&f,1) < 0) {
     return -1;
 }
 hp->outBuf.pktIndex = ((hp->outBuf.pktIndex+3)/4)*4;
 if (oprpc_putPkt(ap,p,len) < 0) {
     return -1;
 }
    return 0;
}

canonical_original_code:
int oprpc_putPktPointer(void *ap, void *p, int len)
{
    struct RPCHandle *hp = ap;
    char f;
    if (p == ((void *)0)) {
 f = 2;
 if (oprpc_putPkt(ap,&f,1) < 0) {
     return -1;
 }
 return 0;
    }
 f = 0;
 if (oprpc_putPkt(ap,&f,1) < 0) {
     return -1;
 }
 hp->outBuf.pktIndex = ((hp->outBuf.pktIndex+3)/4)*4;
 if (oprpc_putPkt(ap,p,len) < 0) {
     return -1;
 }
    return 0;
}

variable_types:
p
void *

variable_types: f char


decompiled_code:
__int64 __fastcall oprpc_putPktStart(_DWORD *a1, unsigned int a2, int a3)
{
int v3; // eax
int v4; // edx
__int64 result; // rax
int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[4]; // [rsp+Ch] [rbp-1Ch] BYREF
int v8[3]; // [rsp+1Ch] [rbp-Ch] BYREF

v3 = a1[14];
v7[0] = a2;
a1[16] = v3;
a1[15] = v3;
v6 = a3;
v8[0] = 0;
if ( (int)oprpc_putPkt((__int64)a1, v8, 4) < 0 )
goto LABEL_7;
if ( (v7[0] & 0x80000000) != 0 )
{
v4 = a1[18] + 1;
v7[0] = a1[18];
a1[18] = v4;
}
if ( (int)oprpc_putPkt((__int64)a1, v7, 4) < 0 || (int)oprpc_putPkt((__int64)a1, &v6, 4) < 0 )
LABEL_7:
result = 0xFFFFFFFFLL;
else
result = v7[0];
return result;
}

original_code:
int oprpc_putPktStart(void *ap, int sendSeqNo, int reqNo)
{
    RPCHandle *hp = ap;
    int dmy = 0;
    hp->outBuf.pktTop = hp->outBuf.pktIndex = hp->outBuf.wIndex;
    if (oprpc_putPkt(hp,(char *)&dmy,sizeof(dmy)) < 0) {
 return -1;
    }
    if (sendSeqNo < 0) {
 sendSeqNo = hp->seqNo++;
    }
    if (oprpc_putPkt(hp,(char *)&sendSeqNo,sizeof(sendSeqNo)) < 0) {
 return -1;
    }
    if (oprpc_putPkt(hp,(char *)&reqNo,sizeof(reqNo)) < 0) {
 return -1;
    }
    return sendSeqNo;
}

canonical_original_code:
int oprpc_putPktStart(void *ap, int sendSeqNo, int reqNo)
{
    struct RPCHandle *hp = ap;
    int dmy = 0;
    hp->outBuf.pktTop = hp->outBuf.pktIndex = hp->outBuf.wIndex;
    if (oprpc_putPkt(hp,(char *)&dmy,sizeof(dmy)) < 0) {
 return -1;
    }
    if (sendSeqNo < 0) {
 sendSeqNo = hp->seqNo++;
    }
    if (oprpc_putPkt(hp,(char *)&sendSeqNo,sizeof(sendSeqNo)) < 0) {
 return -1;
    }
    if (oprpc_putPkt(hp,(char *)&reqNo,sizeof(reqNo)) < 0) {
 return -1;
    }
    return sendSeqNo;
}

variable_types: sendSeqNo int

variable_types: reqNo int

variable_types: dmy int


original_code:
int oprpc_putPktEnd(void *ap)
{
    int len;
    RPCHandle *hp = ap;
    len = hp->outBuf.pktIndex - hp->outBuf.pktTop - sizeof(int);
    len = ((len+3)/4)*4;
    *((int *)(hp->outBuf.body+hp->outBuf.pktTop)) = len;
    hp->outBuf.wIndex = hp->outBuf.pktIndex
       = hp->outBuf.pktTop+len+sizeof(int);
    return 0;
}

canonical_original_code:
int oprpc_putPktEnd(void *ap)
{
    int len;
    struct RPCHandle *hp = ap;
    len = hp->outBuf.pktIndex - hp->outBuf.pktTop - sizeof(int);
    len = ((len+3)/4)*4;
    *((int *)(hp->outBuf.body+hp->outBuf.pktTop)) = len;
    hp->outBuf.wIndex = hp->outBuf.pktIndex
       = hp->outBuf.pktTop+len+sizeof(int);
    return 0;
}


decompiled_code:
__int64 __fastcall oprpc_flush(__int64 a1)
{
return oprpc_flushBuffer(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return func1(a1, 0);
}

original_code:
int oprpc_flush(void *hp)
{
    return oprpc_flushBuffer(hp,0);
}

variable_types:
hp
void *


decompiled_code:
__int64 __fastcall oprpc_getPkt(__int64 a1, void *a2, int a3)
{
__int64 v5; // rsi
int v6; // eax

v5 = *(int *)(a1 + 32);
v6 = *(_DWORD *)(a1 + 24) - v5;
if ( v6 >= a3 )
goto LABEL_4;
if ( (int)oprpc_fillBuffer(a1, a3 - v6, 1) >= 0 )
{
v5 = *(int *)(a1 + 32);
LABEL_4:
memcpy(a2, (const void *)(*(_QWORD *)(a1 + 8) + v5), a3);
*(_DWORD *)(a1 + 32) += a3;
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int oprpc_getPkt(void *ap, char *buf, int len)
{
    RPCHandle *hp = ap;
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
 if (oprpc_fillBuffer(hp,
      len-(hp->inBuf.wIndex - hp->inBuf.pktIndex),1) < 0) {
     return -1;
 }
    }
    memcpy(buf,hp->inBuf.body+hp->inBuf.pktIndex,len);
    hp->inBuf.pktIndex += len;
    return 0;
}

canonical_original_code:
int oprpc_getPkt(void *ap, char *buf, int len)
{
    struct RPCHandle *hp = ap;
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
 if (oprpc_fillBuffer(hp,
      len-(hp->inBuf.wIndex - hp->inBuf.pktIndex),1) < 0) {
     return -1;
 }
    }
    memcpy(buf,hp->inBuf.body+hp->inBuf.pktIndex,len);
    hp->inBuf.pktIndex += len;
    return 0;
}

variable_types: struct RPCHandle { int inFd; int outFd; struct RPCBuf { char *body; int size; int rIndex; int wIndex; int pktTop; int pktIndex; } inBuf; struct RPCBuf outBuf; int seqNo; void **shmemStack; int shmemStackSize; int shmemStackPointer; int lastPushSeqNo; } *


decompiled_code:
__int64 __fastcall oprpc_getPktNonBlock(__int64 a1, void *a2, int a3)
{
__int64 v5; // rsi
int v6; // eax
__int64 result; // rax

v5 = *(int *)(a1 + 32);
v6 = *(_DWORD *)(a1 + 24) - v5;
if ( v6 < a3 )
{
result = oprpc_fillBuffer(a1, a3 - v6, 0);
if ( (int)result <= 0 )
return result;
v5 = *(int *)(a1 + 32);
}
memcpy(a2, (const void *)(*(_QWORD *)(a1 + 8) + v5), a3);
*(_DWORD *)(a1 + 32) += a3;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, void *a2, int a3)
{
__int64 v5; // rsi
int v6; // eax
__int64 result; // rax

v5 = *(int *)(a1 + 32);
v6 = *(_DWORD *)(a1 + 24) - v5;
if ( v6 < a3 )
{
result = func2(a1, a3 - v6, 0);
if ( (int)result <= 0 )
return result;
v5 = *(int *)(a1 + 32);
}
memcpy(a2, (const void *)(*(_QWORD *)(a1 + 8) + v5), a3);
*(_DWORD *)(a1 + 32) += a3;
return 1LL;
}

original_code:
int oprpc_getPktNonBlock(void *ap, char *buf, int len)
{
    RPCHandle *hp = ap;
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
 int r;
 if ((r = oprpc_fillBuffer(hp,
      len-(hp->inBuf.wIndex - hp->inBuf.pktIndex),0)) <= 0) {
     return r;
 }
    }
    memcpy(buf,hp->inBuf.body+hp->inBuf.pktIndex,len);
    hp->inBuf.pktIndex += len;
    return 1;
}

canonical_original_code:
int oprpc_getPktNonBlock(void *ap, char *buf, int len)
{
    struct RPCHandle *hp = ap;
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
 int r;
 if ((r = oprpc_fillBuffer(hp,
      len-(hp->inBuf.wIndex - hp->inBuf.pktIndex),0)) <= 0) {
     return r;
 }
    }
    memcpy(buf,hp->inBuf.body+hp->inBuf.pktIndex,len);
    hp->inBuf.pktIndex += len;
    return 1;
}

variable_types: len int


decompiled_code:
__int64 __fastcall oprpc_getPktPointer(__int64 a1, _QWORD *a2, int a3)
{
__int64 result; // rax
int v5; // eax
int v6; // edx
int v7; // eax
signed int v8; // eax
char v9[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( (int)oprpc_getPkt(a1, v9, 1) < 0 )
return 0xFFFFFFFFLL;
if ( v9[0] )
{
if ( v9[0] != 2 )
return 0xFFFFFFFFLL;
*a2 = 0LL;
result = 2LL;
}
else
{
v5 = *(_DWORD *)(a1 + 32);
v6 = v5 + 6;
v7 = v5 + 3;
if ( v7 < 0 )
v7 = v6;
v8 = v7 & 0xFFFFFFFC;
*(_DWORD *)(a1 + 32) = v8;
if ( a3 < 0 )
{
*a2 = *(_QWORD *)(a1 + 8) + v8;
result = 0LL;
}
else
{
if ( *(_DWORD *)(a1 + 24) - v8 < a3 )
return 0xFFFFFFFFLL;
*a2 = *(_QWORD *)(a1 + 8) + v8;
*(_DWORD *)(a1 + 32) = a3 + v8;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, _QWORD *a2, int a3)
{
__int64 result; // rax
int v5; // eax
int v6; // edx
int v7; // eax
signed int v8; // eax
char v9[25]; // [rsp+Fh] [rbp-19h] BYREF

if ( (int)func11(a1, v9, 1) < 0 )
return 0xFFFFFFFFLL;
if ( v9[0] )
{
if ( v9[0] != 2 )
return 0xFFFFFFFFLL;
*a2 = 0LL;
result = 2LL;
}
else
{
v5 = *(_DWORD *)(a1 + 32);
v6 = v5 + 6;
v7 = v5 + 3;
if ( v7 < 0 )
v7 = v6;
v8 = v7 & 0xFFFFFFFC;
*(_DWORD *)(a1 + 32) = v8;
if ( a3 < 0 )
{
*a2 = *(_QWORD *)(a1 + 8) + v8;
result = 0LL;
}
else
{
if ( *(_DWORD *)(a1 + 24) - v8 < a3 )
return 0xFFFFFFFFLL;
*a2 = *(_QWORD *)(a1 + 8) + v8;
*(_DWORD *)(a1 + 32) = a3 + v8;
result = 0LL;
}
}
return result;
}

original_code:
int oprpc_getPktPointer(void *ap, void **p, int len)
{
    RPCHandle *hp = ap;
    char f;
    if (oprpc_getPkt(ap,&f,1) < 0) {
 return -1;
    }
    switch (f) {
    case 0:
 hp->inBuf.pktIndex = ((hp->inBuf.pktIndex+3)/4)*4;
 if (len >= 0 && hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
     return -1;
 }
 *p = hp->inBuf.body+hp->inBuf.pktIndex;
 if (len >= 0) hp->inBuf.pktIndex += len;
 break;
    case 1:
 return -1;
 break;
    case 2:
 *p = ((void *)0);
 break;
    default:
 return -1;
 break;
    }
    return f;
}

canonical_original_code:
int oprpc_getPktPointer(void *ap, void **p, int len)
{
    struct RPCHandle *hp = ap;
    char f;
    if (oprpc_getPkt(ap,&f,1) < 0) {
 return -1;
    }
    switch (f) {
    case 0:
 hp->inBuf.pktIndex = ((hp->inBuf.pktIndex+3)/4)*4;
 if (len >= 0 && hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
     return -1;
 }
 *p = hp->inBuf.body+hp->inBuf.pktIndex;
 if (len >= 0) hp->inBuf.pktIndex += len;
 break;
    case 1:
 return -1;
 break;
    case 2:
 *p = ((void *)0);
 break;
    default:
 return -1;
 break;
    }
    return f;
}

variable_types:
p
void **


decompiled_code:
__int64 __fastcall oprpc_addInPktIndex(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 32) += a2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 32) += a2;
return 0LL;
}

original_code:
int oprpc_addInPktIndex(void *ap, int len)
{
    RPCHandle *hp = ap;
    hp->inBuf.pktIndex += len;
    return 0;
}

canonical_original_code:
int oprpc_addInPktIndex(void *ap, int len)
{
    struct RPCHandle *hp = ap;
    hp->inBuf.pktIndex += len;
    return 0;
}


decompiled_code:
__int64 __fastcall oprpc_getStr(__int64 a1, const char **a2)
{
unsigned int v2; // er12

if ( (int)oprpc_getPktPointer(a1, a2, -1) < 0 )
return (unsigned int)-1;
v2 = 0;
if ( *a2 )
*(_DWORD *)(a1 + 32) += strlen(*a2) + 1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, const char **a2)
{
unsigned int v2; // er12

if ( (int)func13(a1, a2, -1) < 0 )
return (unsigned int)-1;
v2 = 0;
if ( *a2 )
*(_DWORD *)(a1 + 32) += strlen(*a2) + 1;
return v2;
}

original_code:
int oprpc_getStr(void *ap, void **sp)
{
    RPCHandle *hp = ap;
    int len;
    if (oprpc_getPktPointer(ap,sp,-1) < 0) {
 return -1;
    }
    if (*sp != ((void *)0)) {
 len = strlen(*sp);
 hp->inBuf.pktIndex += len + 1;
    }
    return 0;
}

canonical_original_code:
int oprpc_getStr(void *ap, void **sp)
{
    struct RPCHandle *hp = ap;
    int len;
    if (oprpc_getPktPointer(ap,sp,-1) < 0) {
 return -1;
    }
    if (*sp != ((void *)0)) {
 len = strlen(*sp);
 hp->inBuf.pktIndex += len + 1;
    }
    return 0;
}

variable_types:
sp
void **


decompiled_code:
__int64 __fastcall oprpc_getPktStart(_DWORD *a1)
{
int v1; // eax
int v2; // eax
__int64 result; // rax
int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (int)oprpc_flush((__int64)a1) < 0
|| (v1 = a1[5], a1[8] = v1, a1[7] = v1, (int)oprpc_getPkt((__int64)a1, &v4, 4) < 0)
|| (v2 = a1[6] - a1[8], v2 < v4) && (int)oprpc_fillBuffer((__int64)a1, v4 - v2, 1) < 0
|| (int)oprpc_getPkt((__int64)a1, v5, 4) < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = v5[0];
}
return result;
}

original_code:
int oprpc_getPktStart(void *ap)
{
    RPCHandle *hp = ap;
    int size;
    int gSeqNo;
    if (oprpc_flush(hp) < 0) {
 return -1;
    }
    hp->inBuf.pktTop = hp->inBuf.pktIndex = hp->inBuf.rIndex;
    if (oprpc_getPkt(hp,(char *)&size,sizeof(size)) < 0) {
 return -1;
    }
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < size) {
 if (oprpc_fillBuffer(hp,
      size - (hp->inBuf.wIndex - hp->inBuf.pktIndex),1) < 0) {
     return -1;
 }
    }
    if (oprpc_getPkt(hp,(char *)&gSeqNo,sizeof(gSeqNo)) < 0) {
 return -1;
    }
    return gSeqNo;
}

canonical_original_code:
int oprpc_getPktStart(void *ap)
{
    struct RPCHandle *hp = ap;
    int size;
    int gSeqNo;
    if (oprpc_flush(hp) < 0) {
 return -1;
    }
    hp->inBuf.pktTop = hp->inBuf.pktIndex = hp->inBuf.rIndex;
    if (oprpc_getPkt(hp,(char *)&size,sizeof(size)) < 0) {
 return -1;
    }
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < size) {
 if (oprpc_fillBuffer(hp,
      size - (hp->inBuf.wIndex - hp->inBuf.pktIndex),1) < 0) {
     return -1;
 }
    }
    if (oprpc_getPkt(hp,(char *)&gSeqNo,sizeof(gSeqNo)) < 0) {
 return -1;
    }
    return gSeqNo;
}

variable_types: size int

variable_types: gSeqNo int


decompiled_code:
__int64 __fastcall oprpc_getPktStartNonBlock(_DWORD *a1)
{
int v1; // eax
__int64 result; // rax
int v3; // eax
int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (int)oprpc_flush((__int64)a1) < 0 )
return 0xFFFFFFFFLL;
v1 = a1[5];
a1[8] = v1;
a1[7] = v1;
result = oprpc_getPktNonBlock((__int64)a1, &v4, 4);
if ( (int)result > 0 )
{
v3 = a1[6] - a1[8];
if ( v3 >= v4 || (result = oprpc_fillBuffer((__int64)a1, v4 - v3, 0), (int)result > 0) )
{
if ( (int)oprpc_getPkt((__int64)a1, v5, 4) >= 0 )
return v5[0];
return 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(_DWORD *a1)
{
int v1; // eax
__int64 result; // rax
int v3; // eax
int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (int)func10((__int64)a1) < 0 )
return 0xFFFFFFFFLL;
v1 = a1[5];
a1[8] = v1;
a1[7] = v1;
result = func12((__int64)a1, &v4, 4);
if ( (int)result > 0 )
{
v3 = a1[6] - a1[8];
if ( v3 >= v4 || (result = func2((__int64)a1, v4 - v3, 0), (int)result > 0) )
{
if ( (int)func11((__int64)a1, v5, 4) >= 0 )
return v5[0];
return 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int oprpc_getPktStartNonBlock(void *ap)
{
    RPCHandle *hp = ap;
    int size;
    int gSeqNo;
    int r;
    if (oprpc_flush(hp) < 0) {
 return -1;
    }
    hp->inBuf.pktTop = hp->inBuf.pktIndex = hp->inBuf.rIndex;
    if ((r = oprpc_getPktNonBlock(hp,(char *)&size,sizeof(size))) <= 0) {
 return r;
    }
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < size) {
 if ((r = oprpc_fillBuffer(hp,
      size - (hp->inBuf.wIndex - hp->inBuf.pktIndex),0)) <= 0) {
     return r;
 }
    }
    if (oprpc_getPkt(hp,(char *)&gSeqNo,sizeof(gSeqNo)) < 0) {
 return -1;
    }
    return gSeqNo;
}

canonical_original_code:
int oprpc_getPktStartNonBlock(void *ap)
{
    struct RPCHandle *hp = ap;
    int size;
    int gSeqNo;
    int r;
    if (oprpc_flush(hp) < 0) {
 return -1;
    }
    hp->inBuf.pktTop = hp->inBuf.pktIndex = hp->inBuf.rIndex;
    if ((r = oprpc_getPktNonBlock(hp,(char *)&size,sizeof(size))) <= 0) {
 return r;
    }
    if (hp->inBuf.wIndex - hp->inBuf.pktIndex < size) {
 if ((r = oprpc_fillBuffer(hp,
      size - (hp->inBuf.wIndex - hp->inBuf.pktIndex),0)) <= 0) {
     return r;
 }
    }
    if (oprpc_getPkt(hp,(char *)&gSeqNo,sizeof(gSeqNo)) < 0) {
 return -1;
    }
    return gSeqNo;
}

variable_types: r int


original_code:
int oprpc_getPktEnd(void *ap)
{
    RPCHandle *hp = ap;
    hp->inBuf.rIndex +=
      *((int *)(hp->inBuf.body+hp->inBuf.pktTop))+sizeof(int);
    return 0;
}

canonical_original_code:
int oprpc_getPktEnd(void *ap)
{
    struct RPCHandle *hp = ap;
    hp->inBuf.rIndex +=
      *((int *)(hp->inBuf.body+hp->inBuf.pktTop))+sizeof(int);
    return 0;
}


decompiled_code:
__int64 __fastcall oprpc_putError(_DWORD *a1, unsigned int a2, int a3, int a4)
{
__int64 result; // rax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a4;
if ( (int)oprpc_putPktStart(a1, a2, a3) < 0 || (int)oprpc_putPkt((__int64)a1, v5, 4) < 0 )
result = 0xFFFFFFFFLL;
else
result = oprpc_putPktEnd((__int64)a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(_DWORD *a1, unsigned int a2, int a3, int a4)
{
__int64 result; // rax
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a4;
if ( (int)func8(a1, a2, a3) < 0 || (int)func6((__int64)a1, v5, 4) < 0 )
result = 0xFFFFFFFFLL;
else
result = func9((__int64)a1);
return result;
}

original_code:
int oprpc_putError(void *ap, int seqNo, int errNo, int reqNo)
{
    if (oprpc_putPktStart(ap, seqNo, errNo) < 0) {
 return -1;
    }
    if (oprpc_putPkt((ap), (const char *)(&reqNo),sizeof(int)) < 0) {
 return -1;
    }
    return oprpc_putPktEnd(ap);
}

canonical_original_code:
int oprpc_putError(void *ap, int seqNo, int errNo, int reqNo)
{
    if (oprpc_putPktStart(ap, seqNo, errNo) < 0) {
 return -1;
    }
    if (oprpc_putPkt((ap), (char *)(&reqNo),sizeof(int)) < 0) {
 return -1;
    }
    return oprpc_putPktEnd(ap);
}

variable_types: seqNo int

variable_types: errNo int


main

canonical_name: main

repo: vichargrave/rlist

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
__int64 v4; // rsi
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v3 = 0;
v5 = 0LL;
do
{
v4 = v3++;
addNode(&v5, v4, envp);
}
while ( v3 != 20 );
printList(v5);
v5 = reverseList(v5);
printList(v5);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
__int64 v4; // rsi
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v3 = 0;
v5 = 0LL;
do
{
v4 = v3++;
addNode(&v5, v4, envp);
}
while ( v3 != 20 );
func7(v5);
v5 = func8(v5);
func7(v5);
exit(0);
}

original_code:
int main(int argc, char** argv)
{
    int i;
    node_t* head = ((void *)0);
    for (i = 0; i < 20; i++) {
        addNode(&head, i);
    }
    printList(head);
    head = reverseList(head);
    printList(head);
    exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i;
    struct node_t *head = ((void *)0);
    for (i = 0; i < 20; i++) {
        addNode(&head, i);
    }
    printList(head);
    head = reverseList(head);
    printList(head);
    exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: struct node_t { int m_value; struct node_t *m_next; } *


printList

decompiled_code:
int __fastcall printList(unsigned int *a1)
{
unsigned int *v1; // rbx

if ( a1 )
{
v1 = a1;
do
{
printf("%d ", *v1);
v1 = (unsigned int *)*((_QWORD *)v1 + 1);
}
while ( v1 );
}
return putchar(10);
}

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

if ( a1 )
{
v1 = a1;
do
{
printf("%d ", *v1);
v1 = (unsigned int *)*((_QWORD *)v1 + 1);
}
while ( v1 );
}
return putchar(10);
}

original_code:
void printList(node_t* list)
{
    while (list)
    {
        printf("%d  ", list->m_value);
        list = list->m_next;
    }
    printf("\n");
}

canonical_original_code:
void printList(struct node_t *list)
{
    while (list)
    {
        printf("%d  ", list->m_value);
        list = list->m_next;
    }
    printf("\n");
}

user_defined_types: struct node_t { int m_value; struct node_t *m_next; }


reverseList

decompiled_code:
__int64 __fastcall reverseList(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
__int64 v3; // rdx

result = a1;
if ( a1 )
{
v2 = 0LL;
while ( 1 )
{
v3 = *(_QWORD *)(result + 8);
*(_QWORD *)(result + 8) = v2;
v2 = result;
if ( !v3 )
break;
result = v3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
__int64 v3; // rdx

result = a1;
if ( a1 )
{
v2 = 0LL;
while ( 1 )
{
v3 = *(_QWORD *)(result + 8);
*(_QWORD *)(result + 8) = v2;
v2 = result;
if ( !v3 )
break;
result = v3;
}
}
return result;
}

original_code:
node_t* reverseList(node_t* head)
{
    node_t* cursor = ((void *)0);
    node_t* next;
    while (head)
    {
        next = head->m_next;
        head->m_next = cursor;
        cursor = head;
        head = next;
    }
    return cursor;
}

canonical_original_code:
struct node_t *reverseList(struct node_t *head)
{
    struct node_t *cursor = ((void *)0);
    struct node_t *next;
    while (head)
    {
        next = head->m_next;
        head->m_next = cursor;
        cursor = head;
        head = next;
    }
    return cursor;
}

variable_types:
head
struct node_t *

return_type: struct node_t { int m_value; struct node_t *m_next; } *


main

repo: vichargrave/sniffer

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // r12
const char **v5; // rbx
size_t v6; // rax
const char *v7; // rax
char v9[256]; // [rsp+0h] [rbp-228h] BYREF
char s[296]; // [rsp+100h] [rbp-128h] BYREF

v9[0] = 0;
s[0] = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hi:n:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'i':
strcpy(v9, optarg);
break;
case 'n':
atoi(optarg);
break;
case 'h':
printf("usage: %s [-h] [-i interface] [-n count] [BPF expression]\n", *argv);
exit(0);
}
}
if ( argc > optind )
{
v4 = &argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
++v4;
v6 = strlen(s);
*(_WORD *)stpcpy(&s[v6], *(v4 - 1)) = 32;
}
while ( v4 != v5 );
}
signal(2, stop_capture);
signal(15, stop_capture);
signal(3, stop_capture);
handle = create_pcap_handle(v9);
if ( handle )
{
get_link_header_len(handle, s);
if ( linkhdrlen )
{
if ( (unsigned int)pcap_loop() != -1 )
stop_capture(0);
v7 = (const char *)pcap_geterr();
fprintf(stderr, "pcap_loop failed: %s\n", v7);
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // r12
const char **v5; // rbx
size_t v6; // rax
const char *v7; // rax
char v9[256]; // [rsp+0h] [rbp-228h] BYREF
char s[296]; // [rsp+100h] [rbp-128h] BYREF

v9[0] = 0;
s[0] = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "hi:n:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'i':
strcpy(v9, optarg);
break;
case 'n':
atoi(optarg);
break;
case 'h':
printf("usage: %s [-h] [-i interface] [-n count] [BPF expression]\n", *argv);
exit(0);
}
}
if ( argc > optind )
{
v4 = &argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
++v4;
v6 = strlen(s);
*(_WORD *)stpcpy(&s[v6], *(v4 - 1)) = 32;
}
while ( v4 != v5 );
}
signal(2, stop_capture);
signal(15, stop_capture);
signal(3, stop_capture);
handle = func11(v9);
if ( handle )
{
func12(handle, s);
if ( linkhdrlen )
{
if ( (unsigned int)pcap_loop() != -1 )
func10(0);
v7 = (const char *)pcap_geterr();
fprintf(stderr, "pcap_loop failed: %s\n", v7);
}
}
return -1;
}

original_code:
int main(int argc, char *argv[])
{
    char device[256];
    char filter[256];
    int count = 0;
    int opt;
    *device = 0;
    *filter = 0;
    while ((opt = getopt(argc, argv, "hi:n:")) != -1)
    {
        switch (opt)
        {
        case 'h':
            printf("usage: %s [-h] [-i interface] [-n count] [BPF expression]\n", argv[0]);
            exit(0);
            break;
        case 'i':
            strcpy(device, optarg);
            break;
        case 'n':
            count = atoi(optarg);
            break;
        }
    }
    for (int i = optind; i < argc; i++) {
        strcat(filter, argv[i]);
        strcat(filter, " ");
    }
    signal(2, stop_capture);
    signal(15, stop_capture);
    signal(3, stop_capture);
    handle = create_pcap_handle(device, filter);
    if (handle == ((void *)0)) {
        return -1;
    }
    get_link_header_len(handle);
    if (linkhdrlen == 0) {
        return -1;
    }
    if (pcap_loop(handle, count, packet_handler, (u_char*)((void *)0)) == -1) {
        fprintf(stderr, "pcap_loop failed: %s\n", pcap_geterr(handle));
        return -1;
    }
    stop_capture(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char device[256];
    char filter[256];
    int count = 0;
    int opt;
    *device = 0;
    *filter = 0;
    while ((opt = getopt(argc, argv, "hi:n:")) != -1)
    {
        switch (opt)
        {
        case 'h':
            printf("usage: %s [-h] [-i interface] [-n count] [BPF expression]\n", argv[0]);
            exit(0);
            break;
        case 'i':
            strcpy(device, optarg);
            break;
        case 'n':
            count = atoi(optarg);
            break;
        }
    }
    for (int i = optind; i < argc; i++) {
        strcat(filter, argv[i]);
        strcat(filter, " ");
    }
    signal(2, stop_capture);
    signal(15, stop_capture);
    signal(3, stop_capture);
    handle = create_pcap_handle(device, filter);
    if (handle == ((void *)0)) {
        return -1;
    }
    get_link_header_len(handle);
    if (linkhdrlen == 0) {
        return -1;
    }
    if (pcap_loop(handle, count, packet_handler, (unsigned char *)((void *)0)) == -1) {
        fprintf(stderr, "pcap_loop failed: %s\n", pcap_geterr(handle));
        return -1;
    }
    stop_capture(0);
}

variable_types:
argv
char *[]

variable_types:
device
char [256]

variable_types:
filter
char [256]

variable_types: count int

variable_types: opt int


original_code:
void packet_handler(u_char *user, const struct pcap_pkthdr *packethdr, const u_char *packetptr)
{
    struct ip* iphdr;
    struct icmp* icmphdr;
    struct tcphdr* tcphdr;
    struct udphdr* udphdr;
    char iphdrInfo[256];
    char srcip[256];
    char dstip[256];
    packetptr += linkhdrlen;
    iphdr = (struct ip*)packetptr;
    strcpy(srcip, inet_ntoa(iphdr->ip_src));
    strcpy(dstip, inet_ntoa(iphdr->ip_dst));
    sprintf(iphdrInfo, "ID:%d TOS:0x%x, TTL:%d IpLen:%d DgLen:%d",
            ntohs(iphdr->ip_id), iphdr->ip_tos, iphdr->ip_ttl,
            4*iphdr->ip_hl, ntohs(iphdr->ip_len));
    packetptr += 4*iphdr->ip_hl;
    switch (iphdr->ip_p)
    {
    case IPPROTO_TCP:
        tcphdr = (struct tcphdr*)packetptr;
        printf("TCP  %s:%d -> %s:%d\n", srcip, ntohs(tcphdr->th_sport),
               dstip, ntohs(tcphdr->th_dport));
        printf("%s\n", iphdrInfo);
        printf("%c%c%c%c%c%c Seq: 0x%x Ack: 0x%x Win: 0x%x TcpLen: %d\n",
               (tcphdr->th_flags & 0x20 ? 'U' : '*'),
               (tcphdr->th_flags & 0x10 ? 'A' : '*'),
               (tcphdr->th_flags & 0x08 ? 'P' : '*'),
               (tcphdr->th_flags & 0x04 ? 'R' : '*'),
               (tcphdr->th_flags & 0x02 ? 'S' : '*'),
               (tcphdr->th_flags & 0x02 ? 'F' : '*'),
               ntohl(tcphdr->th_seq), ntohl(tcphdr->th_ack),
               ntohs(tcphdr->th_win), 4*tcphdr->th_off);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
        packets += 1;
        break;
    case IPPROTO_UDP:
        udphdr = (struct udphdr*)packetptr;
        printf("UDP  %s:%d -> %s:%d\n", srcip, ntohs(udphdr->uh_sport),
               dstip, ntohs(udphdr->uh_dport));
        printf("%s\n", iphdrInfo);
     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
        packets += 1;
        break;
    case IPPROTO_ICMP:
        icmphdr = (struct icmp*)packetptr;
        printf("ICMP %s -> %s\n", srcip, dstip);
        printf("%s\n", iphdrInfo);
        printf("Type:%d Code:%d ID:%d Seq:%d\n", icmphdr->icmp_type, icmphdr->icmp_code,
               ntohs(icmphdr->icmp_hun.ih_idseq.icd_id), ntohs(icmphdr->icmp_hun.ih_idseq.icd_seq));
     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
        packets += 1;
        break;
    }
}

canonical_original_code:
void packet_handler(unsigned char *user, struct pcap_pkthdr *packethdr, unsigned char *packetptr)
{
    struct ip *iphdr;
    struct icmp *icmphdr;
    struct tcphdr *tcphdr;
    struct udphdr *udphdr;
    char iphdrInfo[256];
    char srcip[256];
    char dstip[256];
    packetptr += linkhdrlen;
    iphdr = (struct ip *)packetptr;
    strcpy(srcip, inet_ntoa(iphdr->ip_src));
    strcpy(dstip, inet_ntoa(iphdr->ip_dst));
    sprintf(iphdrInfo, "ID:%d TOS:0x%x, TTL:%d IpLen:%d DgLen:%d",
            ntohs(iphdr->ip_id), iphdr->ip_tos, iphdr->ip_ttl,
            4*iphdr->ip_hl, ntohs(iphdr->ip_len));
    packetptr += 4*iphdr->ip_hl;
    switch (iphdr->ip_p)
    {
    case IPPROTO_TCP:
        tcphdr = (struct tcphdr *)packetptr;
        printf("TCP  %s:%d -> %s:%d\n", srcip, ntohs(tcphdr->th_sport),
               dstip, ntohs(tcphdr->th_dport));
        printf("%s\n", iphdrInfo);
        printf("%c%c%c%c%c%c Seq: 0x%x Ack: 0x%x Win: 0x%x TcpLen: %d\n",
               (tcphdr->th_flags & 0x20 ? 'U' : '*'),
               (tcphdr->th_flags & 0x10 ? 'A' : '*'),
               (tcphdr->th_flags & 0x08 ? 'P' : '*'),
               (tcphdr->th_flags & 0x04 ? 'R' : '*'),
               (tcphdr->th_flags & 0x02 ? 'S' : '*'),
               (tcphdr->th_flags & 0x02 ? 'F' : '*'),
               ntohl(tcphdr->th_seq), ntohl(tcphdr->th_ack),
               ntohs(tcphdr->th_win), 4*tcphdr->th_off);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
        packets += 1;
        break;
    case IPPROTO_UDP:
        udphdr = (struct udphdr *)packetptr;
        printf("UDP  %s:%d -> %s:%d\n", srcip, ntohs(udphdr->uh_sport),
               dstip, ntohs(udphdr->uh_dport));
        printf("%s\n", iphdrInfo);
     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
        packets += 1;
        break;
    case IPPROTO_ICMP:
        icmphdr = (struct icmp *)packetptr;
        printf("ICMP %s -> %s\n", srcip, dstip);
        printf("%s\n", iphdrInfo);
        printf("Type:%d Code:%d ID:%d Seq:%d\n", icmphdr->icmp_type, icmphdr->icmp_code,
               ntohs(icmphdr->icmp_hun.ih_idseq.icd_id), ntohs(icmphdr->icmp_hun.ih_idseq.icd_seq));
     printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
        packets += 1;
        break;
    }
}

variable_types:
user
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
packetptr
unsigned char *

variable_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } *

variable_types: struct icmp { unsigned char icmp_type; unsigned char icmp_code; unsigned short icmp_cksum; union { unsigned char ih_pptr; struct in_addr { unsigned int s_addr; } ih_gwaddr; struct ih_idseq { unsigned short icd_id; unsigned short icd_seq; } ih_idseq; unsigned int ih_void; struct ih_pmtu { unsigned short ipm_void; unsigned short ipm_nextmtu; } ih_pmtu; struct ih_rtradv { unsigned char irt_num_addrs; unsigned char irt_wpa; unsigned short irt_lifetime; } ih_rtradv; } icmp_hun; union { struct { unsigned int its_otime; unsigned int its_rtime; unsigned int its_ttime; } id_ts; struct { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; } idi_ip; } id_ip; struct icmp_ra_addr { unsigned int ira_addr; unsigned int ira_preference; } id_radv; unsigned int id_mask; unsigned char id_data[1]; } icmp_dun; } *

variable_types: struct tcphdr { union { struct { unsigned short th_sport; unsigned short th_dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x2; unsigned char th_off; unsigned char th_flags; unsigned short th_win; unsigned short th_sum; unsigned short th_urp; }; struct { unsigned short source; unsigned short dest; unsigned int seq; unsigned int ack_seq; unsigned short res1; unsigned short doff; unsigned short fin; unsigned short syn; unsigned short rst; unsigned short psh; unsigned short ack; unsigned short urg; unsigned short res2; unsigned short window; unsigned short check; unsigned short urg_ptr; }; }; } *

variable_types: struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } *

variable_types:
iphdrInfo
char [256]

variable_types:
srcip
char [256]

variable_types:
dstip
char [256]

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }

user_defined_types: struct icmp { unsigned char icmp_type; unsigned char icmp_code; unsigned short icmp_cksum; union { unsigned char ih_pptr; struct in_addr ih_gwaddr; struct ih_idseq ih_idseq; unsigned int ih_void; struct ih_pmtu ih_pmtu; struct ih_rtradv ih_rtradv; } icmp_hun; union { struct { unsigned int its_otime; unsigned int its_rtime; unsigned int its_ttime; } id_ts; struct { struct ip idi_ip; } id_ip; struct icmp_ra_addr id_radv; unsigned int id_mask; unsigned char id_data[1]; } icmp_dun; }


decompiled_code:
void __noreturn stop_capture()
{
unsigned int v0; // [rsp+4h] [rbp-14h]
unsigned int v1; // [rsp+8h] [rbp-10h]

if ( (int)pcap_stats() >= 0 )
{
printf("\n%d packets captured\n", (unsigned int)packets);
printf("%d packets received by filter\n", v0);
printf("%d packets dropped\n\n", v1);
}
pcap_close();
exit(0);
}

canonical_decompiled_code:
void __noreturn func10()
{
unsigned int v0; // [rsp+4h] [rbp-14h]
unsigned int v1; // [rsp+8h] [rbp-10h]

if ( (int)pcap_stats() >= 0 )
{
printf("\n%d packets captured\n", (unsigned int)packets);
printf("%d packets received by filter\n", v0);
printf("%d packets dropped\n\n", v1);
}
pcap_close();
exit(0);
}

original_code:
void stop_capture(int signo)
{
    struct pcap_stat stats;
    if (pcap_stats(handle, &stats) >= 0) {
        printf("\n%d packets captured\n", packets);
        printf("%d packets received by filter\n", stats.ps_recv);
        printf("%d packets dropped\n\n", stats.ps_drop);
    }
    pcap_close(handle);
    exit(0);
}

variable_types: signo int

variable_types: struct pcap_stat { unsigned int ps_recv; unsigned int ps_drop; unsigned int ps_ifdrop; }


repo: vicnils/tetrominic

decompiled_code:
__int64 __fastcall update_next_tetmino(_DWORD *a1)
{
int v1; // eax
int v2; // edx

if ( !a1[27] )
return 0LL;
v1 = a1[38];
if ( v1 <= 21 )
return 0LL;
v2 = a1[40] - 1;
a1[40] = v2;
if ( v2 > 0 )
return 0LL;
a1[38] = v1 - 1;
a1[40] = a1[26];
return 1LL;
}

original_code:
static int update_next_tetmino(struct game *game)
{
 struct tetfield *tf = &game->tetfield;
 if (tf->state == TETFIELD_SPAWN) {
  return 0;
 }
 if (game->next_tetmino.row > (24 - 4) + 1) {
  game->next_tetmino.falling--;
  if (game->next_tetmino.falling > 0) {
   return 0;
  }
  game->next_tetmino.row--;
  game->next_tetmino.falling = tf->gravity;
  return 1;
 }
 return 0;
}

variable_types: struct game { struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } tetgrid; struct tetfield { struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } mino; struct tetgrid *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; } tetfield; struct tetmino next_tetmino; struct tetmino_piece { struct piece { struct copyrect { int width; int di; } r; unsigned char piece[-1]; struct piece *prev; } piece; struct tetmino *tetmino; int mask; struct tetmino_piece *succ; } piece; struct blocks { struct bitmap { unsigned char *bitmap; int width; int height; } bitmap; struct tg_tiles { char chars[16]; unsigned short attrs[16]; } tiles; int x; int rendered; } blocks; unsigned char bitmap[-1]; } *

variable_types: struct tetfield { struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } mino; struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; } *

user_defined_types: struct game { struct tetgrid tetgrid; struct tetfield tetfield; struct tetmino next_tetmino; struct tetmino_piece piece; struct blocks blocks; unsigned char bitmap[-1]; }

user_defined_types: struct tetfield { struct tetmino mino; struct tetgrid *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; }

user_defined_types: struct tetmino_piece { struct piece piece; struct tetmino *tetmino; int mask; struct tetmino_piece *succ; }

user_defined_types: struct blocks { struct bitmap bitmap; struct tg_tiles tiles; int x; int rendered; }

user_defined_types: struct piece { struct copyrect r; unsigned char piece[-1]; struct piece *prev; }


decompiled_code:
__int64 __fastcall spawn_piece(_DWORD *a1)
{
unsigned int v1; // ebp
unsigned int v2; // er12
unsigned int v3; // eax

enter_tetfield(a1 + 16, (unsigned int)a1[37], 6LL);
v1 = a1[26];
v2 = a1[18] + 4;
v3 = rand7();
return init_tetmino(a1 + 36, v3, v2, 6LL, v1);
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
unsigned int v1; // ebp
unsigned int v2; // er12
unsigned int v3; // eax

enter_tetfield(a1 + 16, (unsigned int)a1[37], 6LL);
v1 = a1[26];
v2 = a1[18] + 4;
v3 = rand7();
return init_tetmino(a1 + 36, v3, v2, 6LL, v1);
}

original_code:
static struct tetmino *spawn_piece(struct game *game)
{
 enter_tetfield(&game->tetfield, game->next_tetmino.piece, (3 + 3));
 return init_tetmino(&game->next_tetmino,
  rand7(),
  game->tetfield.mino.row + 4,
  (3 + 3),
  game->tetfield.gravity);
}

variable_types: struct game { struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } tetgrid; struct tetfield { struct tetmino mino; struct tetgrid *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; } tetfield; struct tetmino next_tetmino; struct tetmino_piece { struct piece { struct copyrect { int width; int di; } r; unsigned char piece[-1]; struct piece *prev; } piece; struct tetmino *tetmino; int mask; struct tetmino_piece *succ; } piece; struct blocks { struct bitmap { unsigned char *bitmap; int width; int height; } bitmap; struct tg_tiles { char chars[16]; unsigned short attrs[16]; } tiles; int x; int rendered; } blocks; unsigned char bitmap[-1]; } *

return_type: struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } *


variable_types: w int

variable_types: struct blocks { struct bitmap { unsigned char *bitmap; int width; int height; } bitmap; struct tg_tiles { char chars[16]; unsigned short attrs[16]; } tiles; int x; int rendered; }

user_defined_types: struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; }

user_defined_types: struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; }

user_defined_types: enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 }


original_code:
static enum action get_action(const char *input)
{
 if (!strcmp(input, "j") || !strcmp(input, "left")) {
  return MOVE_LEFT;
 }
 if (!strcmp(input, "k") || !strcmp(input, "up")) {
  return ROTATE_CW;
 }
 if (!strcmp(input, "i")) {
  return ROTATE_CCW;
 }
 if (!strcmp(input, "l") || !strcmp(input, "right")) {
  return MOVE_RIGHT;
 }
 if (!strcmp(input, "m") || !strcmp(input, "down")) {
  return SOFTDROP;
 }
 if (!strcmp(input, "space")) {
  return HARDDROP;
 }
 return NO_ACTION;
}

canonical_original_code:
static enum action get_action(char *input)
{
 if (!strcmp(input, "j") || !strcmp(input, "left")) {
  return MOVE_LEFT;
 }
 if (!strcmp(input, "k") || !strcmp(input, "up")) {
  return ROTATE_CW;
 }
 if (!strcmp(input, "i")) {
  return ROTATE_CCW;
 }
 if (!strcmp(input, "l") || !strcmp(input, "right")) {
  return MOVE_RIGHT;
 }
 if (!strcmp(input, "m") || !strcmp(input, "down")) {
  return SOFTDROP;
 }
 if (!strcmp(input, "space")) {
  return HARDDROP;
 }
 return NO_ACTION;
}

variable_types:
input
char *

return_type: enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 }


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

moveto(terminal[8], terminal[9]);
set_text_attr(0LL);
printf("Score: %lu\n", *(_QWORD *)(a1 + 64));
result = printf("Gravity: %d\n", *(unsigned int *)(a1 + 40));
terminal[7] += 2;
return result;
}

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

moveto(terminal[8], terminal[9]);
set_text_attr(0LL);
printf("Score: %lu\n", *(_QWORD *)(a1 + 64));
result = printf("Gravity: %d\n", *(unsigned int *)(a1 + 40));
terminal[7] += 2;
return result;
}

original_code:
static void render_score(const struct tetfield *tf)
{
 moveto(terminal.x0, terminal.y0);
 set_text_attr(0);
 printf("Score: %lu\n", tf->score);
 printf("Gravity: %d\n", tf->gravity);
 terminal.cursor_y++;
 terminal.cursor_y++;
}

canonical_original_code:
static void render_score(struct tetfield *tf)
{
 moveto(terminal.x0, terminal.y0);
 set_text_attr(0);
 printf("Score: %lu\n", tf->score);
 printf("Gravity: %d\n", tf->gravity);
 terminal.cursor_y++;
 terminal.cursor_y++;
}


original_code:
void init_game(struct game *game, int gravity)
{
 struct tetfield tetfield = { { 0 } };
 tetfield.grid = &game->tetgrid;
 tetfield.gravity = gravity;
 init_tetgrid(&game->tetgrid, 10);
 game->tetfield = tetfield;
 game->next_tetmino.piece = rand7();
 init_tetmino_piece(&game->next_piece, spawn_piece(game), 0x7f,
  init_tetmino_piece(&game->piece, &game->tetfield.mino, 0x77, ((void *)0)));
 init_game_blocks(game);
}

variable_types: gravity int

variable_types: struct tetfield { struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } mino; struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; }

user_defined_types: struct bitmap { unsigned char *bitmap; int width; int height; }

user_defined_types: struct tg_tiles { char chars[16]; unsigned short attrs[16]; }


decompiled_code:
__int64 __fastcall update_game(_DWORD *a1, __int64 a2)
{
__int64 v2; // r14
_DWORD *v3; // r12
int v4; // eax
unsigned __int16 v5; // dx
int v6; // ebx
bool v7; // zf
int v8; // edx
unsigned int i; // ebp
unsigned __int16 v10; // ax
unsigned int v12; // eax
int v13; // eax
int v14[16]; // [rsp+8h] [rbp-40h] BYREF

v2 = (__int64)(a1 + 16);
v3 = a1 + 76;
v4 = update_line_clears();
v5 = terminal[0];
a1[93] = 1;
v6 = v4;
v7 = terminal[1] == 1;
v8 = v5 >> 1;
a1[92] = v8 - 10;
if ( v7 )
{
a1[94] = 0;
draw_acs("x22", 1LL, (unsigned int)(v8 - 11), 1LL, 264LL);
draw_acs("x22", 1LL, (unsigned int)(a1[92] + 20), (unsigned int)a1[93], 264LL);
draw_acs("lq19k", 22LL, (unsigned int)(a1[92] - 1), 0LL, 264LL);
draw_acs("mq19j", 22LL, (unsigned int)(a1[92] - 1), (unsigned int)(a1[93] + 23), 264LL);
render_score(v2);
render_tetmino_blocks(v3, a1 + 60);
}
if ( v6 > 0 )
{
for ( i = v6; (int)i > 0; i = next_cleared_row(a1, i) )
{
v10 = shift_cleared_blocks(a1, i);
render_cleared_blocks(v3, i, v10);
}
render_score(v2);
}
if ( *(_BYTE *)a2 == 113 && !*(_BYTE *)(a2 + 1) )
return 0LL;
v12 = get_action((const char *)a2);
if ( (unsigned int)run_tetfield(v2, v12, v14) )
{
v13 = update_next_tetmino(a1);
if ( v6 > 0 || v13 | v14[1] | v14[0] )
render_tetmino_blocks(v3, a1 + 60);
}
else
{
if ( a1[27] == 3 )
return 0LL;
render_tetmino_piece(v3, a1 + 44, v2, 127LL);
lock_tetfield(v2);
spawn_piece(a1);
render_tetmino_blocks(v3, a1 + 44);
}
flush_output();
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(_DWORD *a1, __int64 a2)
{
__int64 v2; // r14
_DWORD *v3; // r12
int v4; // eax
unsigned __int16 v5; // dx
int v6; // ebx
bool v7; // zf
int v8; // edx
unsigned int i; // ebp
unsigned __int16 v10; // ax
unsigned int v12; // eax
int v13; // eax
int v14[16]; // [rsp+8h] [rbp-40h] BYREF

v2 = (__int64)(a1 + 16);
v3 = a1 + 76;
v4 = update_line_clears();
v5 = terminal[0];
a1[93] = 1;
v6 = v4;
v7 = terminal[1] == 1;
v8 = v5 >> 1;
a1[92] = v8 - 10;
if ( v7 )
{
a1[94] = 0;
draw_acs("x22", 1LL, (unsigned int)(v8 - 11), 1LL, 264LL);
draw_acs("x22", 1LL, (unsigned int)(a1[92] + 20), (unsigned int)a1[93], 264LL);
draw_acs("lq19k", 22LL, (unsigned int)(a1[92] - 1), 0LL, 264LL);
draw_acs("mq19j", 22LL, (unsigned int)(a1[92] - 1), (unsigned int)(a1[93] + 23), 264LL);
func4(v2);
render_tetmino_blocks(v3, a1 + 60);
}
if ( v6 > 0 )
{
for ( i = v6; (int)i > 0; i = next_cleared_row(a1, i) )
{
v10 = shift_cleared_blocks(a1, i);
render_cleared_blocks(v3, i, v10);
}
func4(v2);
}
if ( *(_BYTE *)a2 == 113 && !*(_BYTE *)(a2 + 1) )
return 0LL;
v12 = func3((const char *)a2);
if ( (unsigned int)run_tetfield(v2, v12, v14) )
{
v13 = func0(a1);
if ( v6 > 0 || v13 | v14[1] | v14[0] )
render_tetmino_blocks(v3, a1 + 60);
}
else
{
if ( a1[27] == 3 )
return 0LL;
render_tetmino_piece(v3, a1 + 44, v2, 127LL);
lock_tetfield(v2);
func1(a1);
render_tetmino_blocks(v3, a1 + 44);
}
flush_output();
return 1LL;
}

original_code:
int update_game(struct game *game, const char *input)
{
 struct tetgrid *grid = &game->tetgrid;
 struct tetfield *tf = &game->tetfield;
 struct blocks *b = &game->blocks;
 struct changed changed;
 int cleared = update_line_clears(grid);
 b->x = terminal.width / 2 - 10;
 b->y = 1;
 if (terminal.lines == 1) {
  int attr = ((0) | 8) | 0x100;
  b->rendered = 0;
  draw_acs("x22", 1, b->x - 1, b->y, attr);
  draw_acs("x22", 1, b->x + 10 * 2, b->y, attr);
  draw_acs("lq19k", 22, b->x - 1, 0, attr);
  draw_acs("mq19j", 22, b->x - 1, b->y + (24 - 1), attr);
  render_score(tf);
  render_tetmino_blocks(b, &game->next_piece);
 }
 if (cleared > 0) {
  int row = cleared;
  while (row > 0) {
   blocks_row mask = shift_cleared_blocks(grid, row);
   render_cleared_blocks(&b->bitmap, row, mask);
   row = next_cleared_row(grid, row);
  }
  render_score(tf);
 }
 if (!strcmp(input, "q")) {
  return 0;
 }
 if (!run_tetfield(tf, get_action(input), &changed)) {
  if (tf->state == TETFIELD_TOP_OUT) {
   return 0;
  }
  render_tetmino_piece(&b->bitmap, &game->piece.piece, &tf->mino, 0x7f);
  lock_tetfield(tf);
  spawn_piece(game);
  render_tetmino_blocks(b, &game->piece);
 } else if (update_next_tetmino(game) || cleared > 0 || (changed.moved | changed.dropped)) {
  render_tetmino_blocks(b, &game->next_piece);
 }
 flush_output();
 return 1;
}

canonical_original_code:
int update_game(struct game *game, char *input)
{
 struct tetgrid *grid = &game->tetgrid;
 struct tetfield *tf = &game->tetfield;
 struct blocks *b = &game->blocks;
 struct changed changed;
 int cleared = update_line_clears(grid);
 b->x = terminal.width / 2 - 10;
 b->y = 1;
 if (terminal.lines == 1) {
  int attr = ((0) | 8) | 0x100;
  b->rendered = 0;
  draw_acs("x22", 1, b->x - 1, b->y, attr);
  draw_acs("x22", 1, b->x + 10 * 2, b->y, attr);
  draw_acs("lq19k", 22, b->x - 1, 0, attr);
  draw_acs("mq19j", 22, b->x - 1, b->y + (24 - 1), attr);
  render_score(tf);
  render_tetmino_blocks(b, &game->next_piece);
 }
 if (cleared > 0) {
  int row = cleared;
  while (row > 0) {
   unsigned short mask = shift_cleared_blocks(grid, row);
   render_cleared_blocks(&b->bitmap, row, mask);
   row = next_cleared_row(grid, row);
  }
  render_score(tf);
 }
 if (!strcmp(input, "q")) {
  return 0;
 }
 if (!run_tetfield(tf, get_action(input), &changed)) {
  if (tf->state == TETFIELD_TOP_OUT) {
   return 0;
  }
  render_tetmino_piece(&b->bitmap, &game->piece.piece, &tf->mino, 0x7f);
  lock_tetfield(tf);
  spawn_piece(game);
  render_tetmino_blocks(b, &game->piece);
 } else if (update_next_tetmino(game) || cleared > 0 || (changed.moved | changed.dropped)) {
  render_tetmino_blocks(b, &game->next_piece);
 }
 flush_output();
 return 1;
}

variable_types: struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } *

variable_types: struct blocks { struct bitmap { unsigned char *bitmap; int width; int height; } bitmap; struct tg_tiles { char chars[16]; unsigned short attrs[16]; } tiles; int x; int rendered; } *

variable_types: struct changed { enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } moved; int dropped; }

variable_types: cleared int

variable_types: attr int

variable_types: row int

variable_types:
mask
unsigned short

user_defined_types: struct changed { enum action moved; int dropped; }


decompiled_code:
void __fastcall flush_tg(int *a1)
{
int v1; // ebp
unsigned int v2; // er8
unsigned int v3; // er12
int v4; // ebp

v1 = a1[2];
if ( v1 > 0 && line_is_visible_isra_0(a1[1]) )
{
v3 = a1[3];
*((_BYTE *)a1 + v1 + 16) = 0;
moveto((unsigned int)*a1, v2);
if ( (v3 & 0x1000) != 0 )
{
set_text_attr(v3 | 0x2000);
(*(&terminal + 1))(a1 + 4);
}
else
{
set_text_attr(v3 | 0x1000);
(*(&terminal + 2))(a1 + 4);
}
v4 = *((_DWORD *)&terminal + 6) + v1;
*((_DWORD *)&terminal + 6) = v4;
*a1 = v4;
}
a1[2] = 0;
}

original_code:
void flush_tg(struct tg_buffer *line)
{
 int len = line->len;
 int attr = line->attr;
 if (len > 0 && line_is_visible(line)) {
  line->buf[len] = '\0';
  moveto(line->x, line->y);
  if (attr & 0x1000) {
   set_text_attr(attr | 0x2000);
   terminal.putacs(line->buf);
  } else {
   set_text_attr(attr | 0x1000);
   terminal.puttext(line->buf);
  }
  line->x = (terminal.cursor_x += len);
 }
 line->len = 0;
}

variable_types: struct tg_buffer { int x; int len; int attr; char buf[32]; } *

variable_types: len int


drawchar

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

if ( a3[3] != a2 || (result = a3[2], (unsigned int)result > 0x1E) )
{
flush_tg(a3);
a3[3] = a2;
result = a3[2];
}
a3[2] = result + 1;
*((_BYTE *)a3 + result + 16) = a1;
return result;
}

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

if ( a3[3] != a2 || (result = a3[2], (unsigned int)result > 0x1E) )
{
func1(a3);
a3[3] = a2;
result = a3[2];
}
a3[2] = result + 1;
*((_BYTE *)a3 + result + 16) = a1;
return result;
}

original_code:
void drawchar(int c, int attr, struct tg_buffer *line)
{
 if (attr != line->attr || line->len >= sizeof(line->buf)-1) {
  flush_tg(line);
  line->attr = attr;
 }
 line->buf[line->len++] = c;
}

variable_types: c int

user_defined_types: struct tg_buffer { int x; int len; int attr; char buf[32]; }


original_code:
static void draw_tile(int nibbles, Tiles tiles, struct tg_buffer *line)
{
 int n1 = (nibbles & 0xF0) >> 4,
     n2 = (nibbles & 0x0F);
 drawchar(tiles->chars[n1], tiles->attrs[n1], line);
 drawchar(tiles->chars[n2], tiles->attrs[n2], line);
}

canonical_original_code:
static void draw_tile(int nibbles, struct tg_tiles *tiles, struct tg_buffer *line)
{
 int n1 = (nibbles & 0xF0) >> 4; int n2 = (nibbles & 0x0F);
 drawchar(tiles->chars[n1], tiles->attrs[n1], line);
 drawchar(tiles->chars[n2], tiles->attrs[n2], line);
}

variable_types: nibbles int

variable_types: struct tg_tiles { char chars[16]; unsigned short attrs[16]; } *


drawacs

original_code:
void drawacs(const char *s, int w, struct tg_buffer *line, int x0)
{
 while (*s && *s != ')' && w > 0) {
  int c = *s++;
  int repeat = 0;
  if (*s > '0' && *s <= '9') {
   do {
    repeat = *s - '0' + 10*repeat;
    s++;
   } while (*s >= '0' && *s <= '9');
  }
  if (c == '(') {
   for (; repeat >= 0; repeat--)
    drawacs(s, w, line, x0);
   s = match_paren(s);
  }
  for (; repeat >= 0; repeat--) {
   if (line->x + line->len >= x0 + w) {
    flush_tg(line);
    line->y++;
    line->x = x0;
   }
   drawchar(c, 0x1000 | line->attr, line);
  }
 }
}

canonical_original_code:
void drawacs(char *s, int w, struct tg_buffer *line, int x0)
{
 while (*s && *s != ')' && w > 0) {
  int c = *s++;
  int repeat = 0;
  if (*s > '0' && *s <= '9') {
   do {
    repeat = *s - '0' + 10*repeat;
    s++;
   } while (*s >= '0' && *s <= '9');
  }
  if (c == '(') {
   for (; repeat >= 0; repeat--)
    drawacs(s, w, line, x0);
   s = match_paren(s);
  }
  for (; repeat >= 0; repeat--) {
   if (line->x + line->len >= x0 + w) {
    flush_tg(line);
    line->y++;
    line->x = x0;
   }
   drawchar(c, 0x1000 | line->attr, line);
  }
 }
}

variable_types:
s
char *

variable_types: w int

variable_types: repeat int


decompiled_code:
void __fastcall draw_acs(char *a1, int a2, unsigned int a3, int a4, int a5)
{
int v5[14]; // [rsp+0h] [rbp-38h] BYREF

v5[0] = a3;
v5[1] = a4;
v5[3] = a5;
v5[2] = 0;
drawacs(a1, a2, v5, a3);
flush_tg(v5);
}

canonical_decompiled_code:
void __fastcall func5(char *a1, int a2, unsigned int a3, int a4, int a5)
{
int v5[14]; // [rsp+0h] [rbp-38h] BYREF

v5[0] = a3;
v5[1] = a4;
v5[3] = a5;
v5[2] = 0;
func4(a1, a2, v5, a3);
func1(v5);
}

original_code:
void draw_acs(const char *s, int w, int x, int y, int attr)
{
 struct tg_buffer line;
 line.x = x;
 line.y = y;
 line.len = 0;
 line.attr = attr;
 drawacs(s, w, &line, x);
 flush_tg(&line);
}

canonical_original_code:
void draw_acs(char *s, int w, int x, int y, int attr)
{
 struct tg_buffer line;
 line.x = x;
 line.y = y;
 line.len = 0;
 line.attr = attr;
 drawacs(s, w, &line, x);
 flush_tg(&line);
}

variable_types: x int

variable_types: y int

variable_types: struct tg_buffer { int x; int len; int attr; char buf[32]; }


drawtiles

original_code:
void drawtiles(unsigned char *front, const unsigned char *back,
        int w, int h, int x, int y, Tiles tiles)
{
 struct tg_buffer line;
 line.y = y;
 line.len = 0;
 line.attr = 0;
 for (; h > 0; h--) {
  const unsigned char *nextRow = back + w;
  line.x = x;
  while (back < nextRow) {
   if (front) {
    if (*front == *back) {
     flush_tg(&line);
     do {
      line.x += 2;
      front++; back++;
     } while (back < nextRow &&
       *front == *back);
     continue;
    }
    *front = *back;
    front++;
   }
   draw_tile(*back, tiles, &line);
   back++;
  }
  flush_tg(&line);
  line.y++;
 }
}

canonical_original_code:
void drawtiles(unsigned char *front, unsigned char *back, int w, int h, int x, int y, struct tg_tiles *tiles)
{
 struct tg_buffer line;
 line.y = y;
 line.len = 0;
 line.attr = 0;
 for (; h > 0; h--) {
  unsigned char *nextRow = back + w;
  line.x = x;
  while (back < nextRow) {
   if (front) {
    if (*front == *back) {
     flush_tg(&line);
     do {
      line.x += 2;
      front++; back++;
     } while (back < nextRow &&
       *front == *back);
     continue;
    }
    *front = *back;
    front++;
   }
   draw_tile(*back, tiles, &line);
   back++;
  }
  flush_tg(&line);
  line.y++;
 }
}

variable_types:
front
unsigned char *

variable_types:
back
unsigned char *

variable_types: h int

variable_types:
nextRow
unsigned char *


cleartoeol

decompiled_code:
size_t cleartoeol()
{
return fwrite(&unk_540, 1uLL, 3uLL, stdout);
}

canonical_decompiled_code:
size_t func0()
{
return fwrite(&unk_540, 1uLL, 3uLL, stdout);
}

original_code:
void cleartoeol()
{
 fputs("\033" "[" "K", stdout);
}


variable_types: attr int

variable_types:
sgr
char [20]

variable_types:
p
char *

variable_types: toggle int


nlto

decompiled_code:
__int64 __fastcall nlto(unsigned int a1)
{
int v1; // ebx
__int64 result; // rax

v1 = a1 - terminal[7];
if ( v1 > 0 )
{
set_text_attr(0);
do
{
putchar(10);
--v1;
}
while ( v1 );
result = terminal[8];
terminal[7] = a1;
terminal[6] = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
int v1; // ebx
__int64 result; // rax

v1 = a1 - terminal[7];
if ( v1 > 0 )
{
func1(0);
do
{
putchar(10);
--v1;
}
while ( v1 );
result = terminal[8];
terminal[7] = a1;
terminal[6] = result;
}
return result;
}

original_code:
static void nlto(int y)
{
 int dy = y - terminal.cursor_y;
 if (dy > 0) {
  set_text_attr(0);
  for (; dy > 0; dy--)
   putchar('\n');
  terminal.cursor_x = terminal.x0;
  terminal.cursor_y = y;
 }
}

variable_types: dy int


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

result = a1 - terminal[9];
if ( (int)terminal[1] <= (int)result )
{
terminal[1] = result + 1;
result = nlto(a1);
}
return result;
}

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

result = a1 - terminal[9];
if ( (int)terminal[1] <= (int)result )
{
terminal[1] = result + 1;
result = func2(a1);
}
return result;
}

original_code:
static void grow_lines(int y)
{
 int y0 = terminal.y0;
 int top = y - y0;
 if (top >= terminal.lines) {
  terminal.lines = top+1;
  nlto(y);
 }
}

variable_types: top int


moveto

decompiled_code:
signed int __fastcall moveto(unsigned int a1, signed int a2)
{
signed int v3; // edx
signed int result; // eax
signed int v5; // ecx
signed int v6; // esi
unsigned int v7; // er12
int v8; // er13

v3 = terminal[1];
result = terminal[9];
v5 = HIWORD(terminal[0]);
if ( v3 > v5 )
{
terminal[1] = v5;
result += v3 - v5;
terminal[9] = result;
}
v6 = terminal[7];
if ( v6 < result )
terminal[9] = v6;
if ( v6 < a2 )
{
result = grow_lines(a2);
v6 = terminal[7];
}
if ( (int)terminal[9] >= a2 )
a2 = terminal[9];
v7 = terminal[6];
terminal[6] = a1;
terminal[7] = a2;
v8 = a1 - v7;
if ( a2 != v6 )
{
if ( a2 - v6 < 0 )
result = printf("\x1B[%dA", (unsigned int)(v6 - a2));
else
result = printf("\x1B[%dB", (unsigned int)(a2 - v6));
}
if ( v8 )
{
if ( v8 <= 0 )
result = printf("\x1B[%dD", v7 - a1);
else
result = printf("\x1B[%dC", (unsigned int)v8);
}
return result;
}

canonical_decompiled_code:
signed int __fastcall func4(unsigned int a1, signed int a2)
{
signed int v3; // edx
signed int result; // eax
signed int v5; // ecx
signed int v6; // esi
unsigned int v7; // er12
int v8; // er13

v3 = terminal[1];
result = terminal[9];
v5 = HIWORD(terminal[0]);
if ( v3 > v5 )
{
terminal[1] = v5;
result += v3 - v5;
terminal[9] = result;
}
v6 = terminal[7];
if ( v6 < result )
terminal[9] = v6;
if ( v6 < a2 )
{
result = func3(a2);
v6 = terminal[7];
}
if ( (int)terminal[9] >= a2 )
a2 = terminal[9];
v7 = terminal[6];
terminal[6] = a1;
terminal[7] = a2;
v8 = a1 - v7;
if ( a2 != v6 )
{
if ( a2 - v6 < 0 )
result = printf("\x1B[%dA", (unsigned int)(v6 - a2));
else
result = printf("\x1B[%dB", (unsigned int)(a2 - v6));
}
if ( v8 )
{
if ( v8 <= 0 )
result = printf("\x1B[%dD", v7 - a1);
else
result = printf("\x1B[%dC", (unsigned int)v8);
}
return result;
}

original_code:
void moveto(int x, int y)
{
 int dx, dy;
 if (terminal.lines > terminal.height) {
  terminal.y0 += terminal.lines - terminal.height;
  terminal.lines = terminal.height;
 }
 if (terminal.y0 > terminal.cursor_y) {
  terminal.y0 = terminal.cursor_y;
 }
 if (y > terminal.cursor_y) {
  grow_lines(y);
 }
 if (y < terminal.y0) y = terminal.y0;
 dx = x - terminal.cursor_x;
 dy = y - terminal.cursor_y;
 terminal.cursor_x = x;
 terminal.cursor_y = y;
 if (dy) {
  if (dy < 0)
   printf("\033" "[" "%dA", -dy);
  else
   printf("\033" "[" "%dB", dy);
 }
 if (dx) {
  if (dx > 0)
   printf("\033" "[" "%dC", dx);
  else
   printf("\033" "[" "%dD", -dx);
 }
}

canonical_original_code:
void moveto(int x, int y)
{
 int dx; int dy;
 if (terminal.lines > terminal.height) {
  terminal.y0 += terminal.lines - terminal.height;
  terminal.lines = terminal.height;
 }
 if (terminal.y0 > terminal.cursor_y) {
  terminal.y0 = terminal.cursor_y;
 }
 if (y > terminal.cursor_y) {
  grow_lines(y);
 }
 if (y < terminal.y0) y = terminal.y0;
 dx = x - terminal.cursor_x;
 dy = y - terminal.cursor_y;
 terminal.cursor_x = x;
 terminal.cursor_y = y;
 if (dy) {
  if (dy < 0)
   printf("\033" "[" "%dA", -dy);
  else
   printf("\033" "[" "%dB", dy);
 }
 if (dx) {
  if (dx > 0)
   printf("\033" "[" "%dC", dx);
  else
   printf("\033" "[" "%dD", -dx);
 }
}

variable_types: dx int


clearscreen

decompiled_code:
__int64 clearscreen()
{
signed int v0; // esi
unsigned int v1; // eax
unsigned int v2; // eax
__int64 result; // rax

v0 = terminal[9];
if ( v0 > 0 )
{
terminal[9] = 0;
v1 = saved_lines_0;
if ( (int)terminal[1] >= saved_lines_0 )
v1 = terminal[1];
v2 = v0 + v1;
v0 = 0;
terminal[1] = v2;
}
moveto(terminal[8], v0);
fwrite("\x1B[J", 1uLL, 3uLL, stdout);
result = terminal[1];
terminal[1] = 1;
saved_lines_0 = result;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
signed int v0; // esi
unsigned int v1; // eax
unsigned int v2; // eax
__int64 result; // rax

v0 = terminal[9];
if ( v0 > 0 )
{
terminal[9] = 0;
v1 = saved_lines_0;
if ( (int)terminal[1] >= saved_lines_0 )
v1 = terminal[1];
v2 = v0 + v1;
v0 = 0;
terminal[1] = v2;
}
func4(terminal[8], v0);
fwrite("\x1B[J", 1uLL, 3uLL, stdout);
result = terminal[1];
terminal[1] = 1;
saved_lines_0 = result;
return result;
}

original_code:
void clearscreen()
{
 static int saved_lines;
 if (terminal.y0 > 0) {
  terminal.lines = terminal.y0 + (
   saved_lines > terminal.lines ? saved_lines : terminal.lines
  );
  terminal.y0 = 0;
 }
 moveto(terminal.x0, terminal.y0);
 fputs("\033" "[" "J", stdout);
 saved_lines = terminal.lines;
 terminal.lines = 1;
}


decompiled_code:
int flush_output()
{
signed int v0; // edi

if ( (int)terminal[6] <= (int)terminal[8] )
return fflush(stdout);
v0 = terminal[7] + 1;
if ( v0 >= (int)(terminal[9] + terminal[1]) )
{
fwrite("\x1B[A", 1uLL, 3uLL, stdout);
v0 = terminal[7];
terminal[7] = v0 - 1;
}
nlto(v0);
return fflush(stdout);
}

canonical_decompiled_code:
int func6()
{
signed int v0; // edi

if ( (int)terminal[6] <= (int)terminal[8] )
return fflush(stdout);
v0 = terminal[7] + 1;
if ( v0 >= (int)(terminal[9] + terminal[1]) )
{
fwrite("\x1B[A", 1uLL, 3uLL, stdout);
v0 = terminal[7];
terminal[7] = v0 - 1;
}
func2(v0);
return fflush(stdout);
}

original_code:
void flush_output()
{
 if (terminal.cursor_x > terminal.x0) {
  if (terminal.cursor_y + 1 >= terminal.y0 + terminal.lines) {
   fputs("\033" "[" "A", stdout);
   terminal.cursor_y--;
  }
  nlto(terminal.cursor_y + 1);
 }
 fflush(stdout);
}


decompiled_code:
size_t hide_cursor()
{
return fwrite("\x1B[?25l", 1uLL, 6uLL, stdout);
}

canonical_decompiled_code:
size_t func7()
{
return fwrite("\x1B[?25l", 1uLL, 6uLL, stdout);
}

original_code:
void hide_cursor()
{
 fputs("\033" "[" "?25l", stdout);
}


decompiled_code:
size_t show_cursor()
{
return fwrite("\x1B[?25h", 1uLL, 6uLL, stdout);
}

canonical_decompiled_code:
size_t func8()
{
return fwrite("\x1B[?25h", 1uLL, 6uLL, stdout);
}

original_code:
void show_cursor()
{
 fputs("\033" "[" "?25h", stdout);
}


decompiled_code:
int __fastcall puttext_ibm(char *s)
{
if ( (ansi_attr & 0x1000) == 0 )
{
fwrite("\x1B[11m", 1uLL, 5uLL, stdout);
ansi_attr |= 0x1000u;
}
return fputs(s, stdout);
}

canonical_decompiled_code:
int __fastcall func9(char *s)
{
if ( (ansi_attr & 0x1000) == 0 )
{
fwrite("\x1B[11m", 1uLL, 5uLL, stdout);
ansi_attr |= 0x1000u;
}
return fputs(s, stdout);
}

original_code:
void puttext_ibm(const char *s)
{
 if (!(ansi_attr & 0x1000)) {
  fputs("\033" "[" "11m", stdout);
  ansi_attr |= 0x1000;
 }
 fputs(s, stdout);
}

canonical_original_code:
void puttext_ibm(char *s)
{
 if (!(ansi_attr & 0x1000)) {
  fputs("\033" "[" "11m", stdout);
  ansi_attr |= 0x1000;
 }
 fputs(s, stdout);
}

variable_types:
s
char *


decompiled_code:
int __fastcall putacs_vt100(char *s)
{
int v1; // edx

v1 = ansi_attr;
BYTE1(v1) = BYTE1(ansi_attr) | 0x30;
if ( ansi_attr != v1 )
{
puttext_ibm("\x1B(0");
ansi_attr |= 0x3000u;
}
return fputs(s, stdout);
}

canonical_decompiled_code:
int __fastcall func10(char *s)
{
int v1; // edx

v1 = ansi_attr;
BYTE1(v1) = BYTE1(ansi_attr) | 0x30;
if ( ansi_attr != v1 )
{
func9("\x1B(0");
ansi_attr |= 0x3000u;
}
return fputs(s, stdout);
}

original_code:
void putacs_vt100(const char *s)
{
 if ((ansi_attr | 0x1000 | 0x2000) != ansi_attr) {
  puttext_ibm("\033" "(0");
  ansi_attr |= 0x1000 | 0x2000;
 }
 fputs(s, stdout);
}

canonical_original_code:
void putacs_vt100(char *s)
{
 if ((ansi_attr | 0x1000 | 0x2000) != ansi_attr) {
  puttext_ibm("\033" "(0");
  ansi_attr |= 0x1000 | 0x2000;
 }
 fputs(s, stdout);
}


decompiled_code:
_BOOL8 set_input_mode()
{
_BOOL4 v0; // er8
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

v0 = 0;
*(_QWORD *)&v2.c_cc[31] = *(_QWORD *)&saved_term_attr.c_cc[31];
*(__m128i *)&v2.c_iflag = _mm_load_si128((const __m128i *)&saved_term_attr);
v2.c_ospeed = saved_term_attr.c_ospeed;
*(__m128i *)&v2.c_line = _mm_load_si128((const __m128i *)&saved_term_attr.c_line);
*(__m128i *)&v2.c_cc[15] = _mm_load_si128((const __m128i *)&saved_term_attr.c_cc[15]);
if ( v2.c_lflag )
{
v2.c_lflag &= 0xFFFFFFF5;
*(_WORD *)&v2.c_cc[5] = 2;
v0 = tcsetattr(0, 0, &v2) == 0;
}
return v0;
}

canonical_decompiled_code:
_BOOL8 func0()
{
_BOOL4 v0; // er8
struct termios v2; // [rsp+0h] [rbp-48h] BYREF

v0 = 0;
*(_QWORD *)&v2.c_cc[31] = *(_QWORD *)&saved_term_attr.c_cc[31];
*(__m128i *)&v2.c_iflag = _mm_load_si128((const __m128i *)&saved_term_attr);
v2.c_ospeed = saved_term_attr.c_ospeed;
*(__m128i *)&v2.c_line = _mm_load_si128((const __m128i *)&saved_term_attr.c_line);
*(__m128i *)&v2.c_cc[15] = _mm_load_si128((const __m128i *)&saved_term_attr.c_cc[15]);
if ( v2.c_lflag )
{
v2.c_lflag &= 0xFFFFFFF5;
*(_WORD *)&v2.c_cc[5] = 2;
v0 = tcsetattr(0, 0, &v2) == 0;
}
return v0;
}

original_code:
static int set_input_mode()
{
 struct termios attr = saved_term_attr;
 if (attr.c_lflag == 0) {
  return 0;
 }
 attr.c_lflag &= ~(0000002|0000010);
 attr.c_cc[6] = 0;
 attr.c_cc[5] = 2;
 return tcsetattr(0, 0, &attr) == 0;
}

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


decompiled_code:
_BOOL8 restore_input_mode()
{
_BOOL8 result; // rax

if ( saved_term_attr.c_lflag )
result = tcsetattr(0, 0, &saved_term_attr) == 0;
else
result = 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
_BOOL8 result; // rax

if ( saved_term_attr.c_lflag )
result = tcsetattr(0, 0, &saved_term_attr) == 0;
else
result = 0LL;
return result;
}

original_code:
static int restore_input_mode()
{
 if (saved_term_attr.c_lflag == 0) {
  return 0;
 }
 return tcsetattr(0, 0, &saved_term_attr) == 0;
}


decompiled_code:
int __fastcall get_terminal_size(int a1)
{
int result; // eax
__int16 v2[8]; // [rsp+8h] [rbp-10h] BYREF

result = ioctl(a1, 0x5413uLL, v2);
if ( !result )
{
terminal[0] = v2[1];
result = (unsigned __int16)v2[0];
terminal[1] = v2[0];
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
int result; // eax
__int16 v2[8]; // [rsp+8h] [rbp-10h] BYREF

result = ioctl(a1, 0x5413uLL, v2);
if ( !result )
{
terminal[0] = v2[1];
result = (unsigned __int16)v2[0];
terminal[1] = v2[0];
}
return result;
}

original_code:
static void get_terminal_size(int fd)
{
 struct winsize win;
 if (!ioctl(fd, 0x5413, &win)) {
  terminal.width = win.ws_col;
  terminal.height = win.ws_row;
 }
}

variable_types: fd int

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }


decompiled_code:
__int64 setup_terminal()
{
int v0; // edi

v0 = 1;
if ( set_input_mode() )
{
hide_cursor(1LL);
v0 = 0;
}
get_terminal_size(v0);
set_text_attr(0LL);
return clearscreen();
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // edi

v0 = 1;
if ( func0() )
{
hide_cursor(1LL);
v0 = 0;
}
func2(v0);
set_text_attr(0LL);
return clearscreen();
}

original_code:
void setup_terminal()
{
 int fd = 1;
 if (set_input_mode()) {
  hide_cursor();
  fd = 0;
 }
 get_terminal_size(fd);
 set_text_attr(0);
 clearscreen();
}


decompiled_code:
int init_terminal()
{
int result; // eax

result = isatty(1);
if ( result )
{
tcgetattr(0, &saved_term_attr);
setup_terminal();
result = 1;
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax

result = isatty(1);
if ( result )
{
tcgetattr(0, &saved_term_attr);
func3();
result = 1;
}
return result;
}

original_code:
int init_terminal()
{
 if (!isatty(1)) {
  return 0;
 }
 tcgetattr(0, &saved_term_attr);
 setup_terminal();
 return 1;
}


decompiled_code:
__int64 restore_terminal()
{
int v0; // esi
__int64 v1; // rdi

set_text_attr(0LL);
v0 = *(_DWORD *)&terminal[2];
v1 = *(unsigned int *)&terminal[16];
if ( v0 != 1 || (_DWORD)v1 != *(_DWORD *)&terminal[12] )
moveto(v1, (unsigned int)(*(_DWORD *)&terminal[18] + v0));
*(_DWORD *)&terminal[14] = 0;
*(_DWORD *)&terminal[18] = 0;
clearscreen();
if ( restore_input_mode() )
show_cursor();
return flush_output();
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // esi
__int64 v1; // rdi

set_text_attr(0LL);
v0 = *(_DWORD *)&terminal[2];
v1 = *(unsigned int *)&terminal[16];
if ( v0 != 1 || (_DWORD)v1 != *(_DWORD *)&terminal[12] )
moveto(v1, (unsigned int)(*(_DWORD *)&terminal[18] + v0));
*(_DWORD *)&terminal[14] = 0;
*(_DWORD *)&terminal[18] = 0;
clearscreen();
if ( func1() )
show_cursor();
return flush_output();
}

original_code:
void restore_terminal()
{
 set_text_attr(0);
 if (terminal.lines != 1 || terminal.x0 != terminal.cursor_x) {
  moveto(terminal.x0, terminal.y0 + terminal.lines);
 }
 terminal.cursor_y = 0;
 terminal.y0 = 0;
 clearscreen();
 if (restore_input_mode()) {
  show_cursor();
 }
 flush_output();
}


decompiled_code:
void __fastcall print_error(char *s)
{
int *v1; // rax
int v2; // er12
int *v3; // rbx

v1 = _errno_location();
v2 = *v1;
v3 = v1;
set_text_attr(0LL);
clearscreen();
flush_output();
*v3 = v2;
perror(s);
}

canonical_decompiled_code:
void __fastcall func6(char *s)
{
int *v1; // rax
int v2; // er12
int *v3; // rbx

v1 = _errno_location();
v2 = *v1;
v3 = v1;
set_text_attr(0LL);
clearscreen();
flush_output();
*v3 = v2;
perror(s);
}

original_code:
void print_error(const char *s)
{
 int saved_errno = (*__errno_location ());
 set_text_attr(0);
 clearscreen();
 flush_output();
 (*__errno_location ()) = saved_errno;
 perror(s);
}

canonical_original_code:
void print_error(char *s)
{
 int saved_errno = (*__errno_location ());
 set_text_attr(0);
 clearscreen();
 flush_output();
 (*__errno_location ()) = saved_errno;
 perror(s);
}


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

result = *(unsigned int *)(a1 + 48);
if ( (_DWORD)result == 5 )
{
*(_BYTE *)(a1 + 57) += 22;
*(_DWORD *)(a1 + 48) = 0;
*(_BYTE *)(a1 + 52) = 0;
}
else if ( (_DWORD)result )
{
*(_BYTE *)(a1 + 52) += 22;
*(_DWORD *)(a1 + 48) = 0;
}
return result;
}

original_code:
static void stop_retrying_action(struct tetfield *tf)
{
 if (tf->last_action == HARDDROP) {
  tf->last_action = NO_ACTION;
  tf->timeout[NO_ACTION] = 0;
  tf->timeout[HARDDROP] += 25 - 3;
 }
 if (tf->last_action != NO_ACTION) {
  tf->last_action = NO_ACTION;
  tf->timeout[NO_ACTION] += 25 - 3;
 }
}

variable_types: struct tetfield { struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } mino; struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; } *


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

if ( !*(_BYTE *)(a1 + 52) )
{
if ( a2 <= 2 )
{
if ( a2 )
*(_DWORD *)(a1 + 48) = a2;
}
else
{
result = a2 - 3;
if ( (unsigned int)result <= 3 && !*(_BYTE *)(a1 + a2 + 52) )
{
*(_DWORD *)(a1 + 16) = 1;
*(_BYTE *)(a1 + a2 + 52) = 1;
}
}
}
return result;
}

original_code:
static void update_prespawn(struct tetfield *tf, enum action a)
{
 if (!tf->timeout[NO_ACTION]) {
  switch (a) {
  case ROTATE_CW:
  case ROTATE_CCW:
   tf->last_action = a;
   break;
  case MOVE_RIGHT:
  case MOVE_LEFT:
  case HARDDROP:
  case SOFTDROP:
   if (!tf->timeout[a]) {
    tf->mino.falling = 1;
    tf->timeout[a] = 1;
   }
   break;
  default:
   break;
  }
 }
}

variable_types: enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 }


decompiled_code:
__int64 __fastcall is_movable(_DWORD *a1)
{
__int64 result; // rax

result = 1LL;
if ( !a1[4] )
{
result = (unsigned int)a1[5];
if ( (_DWORD)result )
result = a1[6] != 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
__int64 result; // rax

result = 1LL;
if ( !a1[4] )
{
result = (unsigned int)a1[5];
if ( (_DWORD)result )
result = a1[6] != 0;
}
return result;
}

original_code:
static int is_movable(const struct tetmino *t)
{
 return t->falling || (t->lock_delay_move && t->lock_delay_step);
}

canonical_original_code:
static int is_movable(struct tetmino *t)
{
 return t->falling || (t->lock_delay_move && t->lock_delay_step);
}

variable_types: struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } *


levelup

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

result = *(unsigned int *)(a1 + 40);
if ( (int)result > 1 )
{
*(_DWORD *)(a1 + 72) = 0;
result = (unsigned int)(int)((double)(int)result * 0.9);
*(_DWORD *)(a1 + 40) = result;
}
return result;
}

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

result = *(unsigned int *)(a1 + 40);
if ( (int)result > 1 )
{
*(_DWORD *)(a1 + 72) = 0;
result = (unsigned int)(int)((double)(int)result * 0.9);
*(_DWORD *)(a1 + 40) = result;
}
return result;
}

original_code:
static void levelup(struct tetfield *tf)
{
 if (tf->gravity > 1) {
  tf->gravity *= 0.9;
  tf->pieces = 0;
 }
}


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

if ( *(_BYTE *)(a1 + a2 + 52) || !(unsigned int)control_tetmino(a1, *(_QWORD *)(a1 + 32) + 12LL, a2) )
return 0LL;
v3 = *(unsigned int *)(a1 + 48);
if ( !(_DWORD)v3 || !*(_BYTE *)(a1 + v3 + 52) )
*(_DWORD *)(a1 + 48) = a2;
*(_BYTE *)(a1 + 52) = 3;
result = a2;
*(_BYTE *)(a1 + a2 + 52) = 3;
return result;
}

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

if ( *(_BYTE *)(a1 + a2 + 52) || !(unsigned int)control_tetmino(a1, *(_QWORD *)(a1 + 32) + 12LL, a2) )
return 0LL;
v3 = *(unsigned int *)(a1 + 48);
if ( !(_DWORD)v3 || !*(_BYTE *)(a1 + v3 + 52) )
*(_DWORD *)(a1 + 48) = a2;
*(_BYTE *)(a1 + 52) = 3;
result = a2;
*(_BYTE *)(a1 + a2 + 52) = 3;
return result;
}

original_code:
static enum action make_move(struct tetfield *tf, enum action a)
{
 if (tf->timeout[a] == 0 && control_tetmino(&tf->mino, tf->grid->blocks, a)) {
  if (tf->last_action == NO_ACTION || tf->timeout[tf->last_action] == 0) {
   tf->last_action = a;
  }
  tf->timeout[NO_ACTION] = 3;
  tf->timeout[a] = 3;
  return a;
 }
 return NO_ACTION;
}


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

if ( *(_BYTE *)(a1 + a2 + 52) )
{
result = 0LL;
if ( !*(_BYTE *)(a1 + 52) )
{
if ( !a2 )
return result;
goto LABEL_4;
}
result = make_move(a1, *(_DWORD *)(a1 + 48));
if ( a2 )
{
LABEL_4:
*(_DWORD *)(a1 + 48) = a2;
*(_BYTE *)(a1 + 52) = 3;
return result;
}
}
else
{
result = make_move(a1, a2);
if ( !(_DWORD)result )
{
if ( a2 )
{
*(_DWORD *)(a1 + 48) = a2;
*(_BYTE *)(a1 + 52) = 4;
}
}
}
return result;
}

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

if ( *(_BYTE *)(a1 + a2 + 52) )
{
result = 0LL;
if ( !*(_BYTE *)(a1 + 52) )
{
if ( !a2 )
return result;
goto LABEL_4;
}
result = func4(a1, *(_DWORD *)(a1 + 48));
if ( a2 )
{
LABEL_4:
*(_DWORD *)(a1 + 48) = a2;
*(_BYTE *)(a1 + 52) = 3;
return result;
}
}
else
{
result = func4(a1, a2);
if ( !(_DWORD)result )
{
if ( a2 )
{
*(_DWORD *)(a1 + 48) = a2;
*(_BYTE *)(a1 + 52) = 4;
}
}
}
return result;
}

original_code:
static enum action update_move(struct tetfield *tf, enum action a)
{
 enum action moved = NO_ACTION;
 if (tf->timeout[a]) {
  if (tf->timeout[NO_ACTION]) {
   moved = make_move(tf, tf->last_action);
  }
  if (a != NO_ACTION) {
   tf->last_action = a;
   tf->timeout[NO_ACTION] = 3;
  }
 } else if ((moved = make_move(tf, a)) == NO_ACTION) {
  if (a != NO_ACTION) {
   tf->last_action = a;
   tf->timeout[NO_ACTION] = 4;
  }
 }
 return moved;
}


decompiled_code:
__int64 __fastcall is_lockable(unsigned int *a1)
{
__int64 v1; // rbx
__int64 v2; // r12

v1 = 0LL;
v2 = *((_QWORD *)a1 + 4) + 2LL * (int)a1[2];
while ( (*(_BYTE *)(v2 + 2 * v1 + 12) & 1) != 0 || !(unsigned int)tetmino_has_row(*a1, (unsigned int)v1) )
{
if ( ++v1 == 4 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1)
{
__int64 v1; // rbx
__int64 v2; // r12

v1 = 0LL;
v2 = *((_QWORD *)a1 + 4) + 2LL * (int)a1[2];
while ( (*(_BYTE *)(v2 + 2 * v1 + 12) & 1) != 0 || !(unsigned int)tetmino_has_row(*a1, (unsigned int)v1) )
{
if ( ++v1 == 4 )
return 1LL;
}
return 0LL;
}

original_code:
static int is_lockable(const struct tetfield *tf)
{
 const blocks_row *blocks = tf->grid->blocks + tf->mino.row;
 int i;
 for (i=0; i < 4; i++) {
  if (!(blocks[i] & 1) && tetmino_has_row(tf->mino.shape, i))
   return 0;
 }
 return 1;
}

canonical_original_code:
static int is_lockable(struct tetfield *tf)
{
 unsigned short *blocks = tf->grid->blocks + tf->mino.row;
 int i;
 for (i=0; i < 4; i++) {
  if (!(blocks[i] & 1) && tetmino_has_row(tf->mino.shape, i))
   return 0;
 }
 return 1;
}

variable_types:
blocks
unsigned short *

variable_types: i int


decompiled_code:
__int64 __fastcall enter_tetfield(__int64 a1, __int64 a2, unsigned int a3)
{
init_tetmino(a1, a2, 21LL, a3, 26LL);
*(_DWORD *)(a1 + 44) = 0;
return stop_retrying_action(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, unsigned int a3)
{
init_tetmino(a1, a2, 21LL, a3, 26LL);
*(_DWORD *)(a1 + 44) = 0;
return func0(a1);
}

original_code:
void enter_tetfield(struct tetfield *tf, int piece, int col)
{
 init_tetmino(&tf->mino, piece, (24 - 4) + 1, col, 25 + 1);
 tf->state = TETFIELD_SPAWN;
 stop_retrying_action(tf);
}

variable_types: piece int

variable_types: col int


decompiled_code:
__int64 __fastcall run_tetfield(__int64 a1, unsigned int a2, __int64 a3)
{
__int64 v4; // r13
unsigned int v5; // eax
int v6; // eax
__int64 v7; // rdx
unsigned int v8; // er8
int v10; // eax
unsigned int v11; // eax

v4 = *(_QWORD *)(a1 + 32);
dec_timeout(a1);
*(_QWORD *)a3 = 0LL;
v5 = *(_DWORD *)(a1 + 44);
if ( v5 == 2 )
{
*(_DWORD *)(a3 + 4) = control_tetmino(a1, v4 + 12, 5LL);
if ( *(_BYTE *)(a1 + 52) != 1 )
goto LABEL_6;
LABEL_16:
stop_retrying_action(a1);
goto LABEL_6;
}
if ( v5 > 2 )
{
if ( v5 == 3 )
return 0LL;
}
else if ( v5 )
{
v6 = update_move(a1, a2);
v7 = *(unsigned int *)(a1 + 40);
*(_DWORD *)a3 = v6;
*(_DWORD *)(a3 + 4) = update_tetmino(a1, v4 + 12, v7);
}
else
{
update_prespawn(a1, a2);
v10 = *(_DWORD *)(a1 + 16);
if ( v10 > 1 )
{
if ( !*(_DWORD *)(v4 + 4) )
*(_DWORD *)(a1 + 16) = v10 - 1;
return 1;
}
v11 = update_tetmino(a1, v4 + 12, *(unsigned int *)(a1 + 40));
*(_DWORD *)(a3 + 4) = v11;
v8 = v11;
if ( !v11 )
{
*(_DWORD *)(a1 + 44) = 3;
return v8;
}
*(_BYTE *)(a1 + 52) = 1;
*(_DWORD *)(a1 + 44) = 1;
*(_DWORD *)a3 = update_move(a1, a2);
}
if ( *(_BYTE *)(a1 + 52) == 1 )
goto LABEL_16;
LABEL_6:
update_removable_rows(v4, a1);
if ( (unsigned int)is_movable((_DWORD *)a1) )
return 1;
*(_DWORD *)(a1 + 44) = 2;
return (unsigned int)is_lockable((unsigned int *)a1) == 0;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned int a2, __int64 a3)
{
__int64 v4; // r13
unsigned int v5; // eax
int v6; // eax
__int64 v7; // rdx
unsigned int v8; // er8
int v10; // eax
unsigned int v11; // eax

v4 = *(_QWORD *)(a1 + 32);
dec_timeout(a1);
*(_QWORD *)a3 = 0LL;
v5 = *(_DWORD *)(a1 + 44);
if ( v5 == 2 )
{
*(_DWORD *)(a3 + 4) = control_tetmino(a1, v4 + 12, 5LL);
if ( *(_BYTE *)(a1 + 52) != 1 )
goto LABEL_6;
LABEL_16:
func0(a1);
goto LABEL_6;
}
if ( v5 > 2 )
{
if ( v5 == 3 )
return 0LL;
}
else if ( v5 )
{
v6 = func5(a1, a2);
v7 = *(unsigned int *)(a1 + 40);
*(_DWORD *)a3 = v6;
*(_DWORD *)(a3 + 4) = update_tetmino(a1, v4 + 12, v7);
}
else
{
func1(a1, a2);
v10 = *(_DWORD *)(a1 + 16);
if ( v10 > 1 )
{
if ( !*(_DWORD *)(v4 + 4) )
*(_DWORD *)(a1 + 16) = v10 - 1;
return 1;
}
v11 = update_tetmino(a1, v4 + 12, *(unsigned int *)(a1 + 40));
*(_DWORD *)(a3 + 4) = v11;
v8 = v11;
if ( !v11 )
{
*(_DWORD *)(a1 + 44) = 3;
return v8;
}
*(_BYTE *)(a1 + 52) = 1;
*(_DWORD *)(a1 + 44) = 1;
*(_DWORD *)a3 = func5(a1, a2);
}
if ( *(_BYTE *)(a1 + 52) == 1 )
goto LABEL_16;
LABEL_6:
update_removable_rows(v4, a1);
if ( (unsigned int)func2((_DWORD *)a1) )
return 1;
*(_DWORD *)(a1 + 44) = 2;
return (unsigned int)func6((unsigned int *)a1) == 0;
}

original_code:
int run_tetfield(struct tetfield *tf, enum action a, struct changed *out)
{
 struct tetgrid *grid = tf->grid;
 dec_timeout(tf);
 out->moved = NO_ACTION;
 out->dropped = 0;
 switch (tf->state) {
 case TETFIELD_SPAWN:
  update_prespawn(tf, a);
  if (tf->mino.falling > 1) {
   if (grid->clearing) return 1;
   tf->mino.falling--;
   return 1;
  }
  out->dropped = update_tetmino(&tf->mino, grid->blocks, tf->gravity);
  if (!out->dropped) {
   tf->state = TETFIELD_TOP_OUT;
   return 0;
  }
  tf->timeout[NO_ACTION] = 1;
  tf->state = TETFIELD_MOVE;
  out->moved = update_move(tf, a);
  break;
 case TETFIELD_MOVE:
  out->moved = update_move(tf, a);
  out->dropped = update_tetmino(&tf->mino, grid->blocks, tf->gravity);
  break;
 case TETFIELD_PLACED:
  out->dropped = control_tetmino(&tf->mino, grid->blocks, HARDDROP);
  break;
 case TETFIELD_TOP_OUT:
  return 0;
 }
 if (tf->timeout[NO_ACTION] == 1) {
  stop_retrying_action(tf);
 }
 update_removable_rows(grid, &tf->mino);
 return is_movable(&tf->mino) || (tf->state = TETFIELD_PLACED, !is_lockable(tf));
}

variable_types: struct changed { enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } moved; int dropped; } *

variable_types: struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } *


original_code:
int lock_tetfield(struct tetfield *tf)
{
 struct tetgrid *grid = tf->grid;
 blocks_row *blocks = grid->blocks;
 int num_lines_cleared = 0;
 int i;
 xor_tetmino(&tf->mino, blocks);
 blocks += tf->mino.row;
 for (i=0; i < 4; i++) {
  blocks_row b = blocks[i] + 1;
  if (!b) {
   num_lines_cleared++;
   blocks[i] ^= 1;
   grid->clearing++;
   grid->delay += 5;
  }
 }
 tf->score += (num_lines_cleared << num_lines_cleared) *
  (grid->cols * grid->cols + tf->pieces) / tf->gravity;
 tf->pieces++;
 if (tf->pieces >= 25 && num_lines_cleared) {
  levelup(tf);
 }
 return num_lines_cleared;
}

canonical_original_code:
int lock_tetfield(struct tetfield *tf)
{
 struct tetgrid *grid = tf->grid;
 unsigned short *blocks = grid->blocks;
 int num_lines_cleared = 0;
 int i;
 xor_tetmino(&tf->mino, blocks);
 blocks += tf->mino.row;
 for (i=0; i < 4; i++) {
  unsigned short b = blocks[i] + 1;
  if (!b) {
   num_lines_cleared++;
   blocks[i] ^= 1;
   grid->clearing++;
   grid->delay += 5;
  }
 }
 tf->score += (num_lines_cleared << num_lines_cleared) *
  (grid->cols * grid->cols + tf->pieces) / tf->gravity;
 tf->pieces++;
 if (tf->pieces >= 25 && num_lines_cleared) {
  levelup(tf);
 }
 return num_lines_cleared;
}

variable_types:
b
unsigned short


decompiled_code:
__int64 __fastcall get_seq_len(_BYTE *a1, __int64 a2)
{
__int64 result; // rax
char v3; // dl
int v4; // er8

result = 0LL;
if ( !a2 )
return result;
result = 1LL;
if ( !*a1 )
return result;
v3 = a1[1];
if ( *a1 != 27 )
{
LABEL_10:
result += v3 == 0;
return result;
}
if ( v3 == 91 )
{
v3 = a1[2];
if ( (unsigned __int8)(v3 - 48) > 0xFu )
{
v4 = 64;
result = 2LL;
}
else
{
result = 2LL;
do
v3 = a1[++result];
while ( (unsigned __int8)(v3 - 48) <= 0xFu );
v4 = 64;
}
}
else
{
v4 = 48;
if ( v3 == 79 )
{
v3 = a1[2];
v4 = 48;
result = 2LL;
goto LABEL_7;
}
}
for ( ; (unsigned __int8)(v3 - 32) <= 0xFu; v3 = a1[result] )
++result;
LABEL_7:
if ( v3 == 127 || v3 < v4 )
{
if ( result != a2 )
goto LABEL_10;
result = 0LL;
}
else
{
++result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2)
{
__int64 result; // rax
char v3; // dl
int v4; // er8

result = 0LL;
if ( !a2 )
return result;
result = 1LL;
if ( !*a1 )
return result;
v3 = a1[1];
if ( *a1 != 27 )
{
LABEL_10:
result += v3 == 0;
return result;
}
if ( v3 == 91 )
{
v3 = a1[2];
if ( (unsigned __int8)(v3 - 48) > 0xFu )
{
v4 = 64;
result = 2LL;
}
else
{
result = 2LL;
do
v3 = a1[++result];
while ( (unsigned __int8)(v3 - 48) <= 0xFu );
v4 = 64;
}
}
else
{
v4 = 48;
if ( v3 == 79 )
{
v3 = a1[2];
v4 = 48;
result = 2LL;
goto LABEL_7;
}
}
for ( ; (unsigned __int8)(v3 - 32) <= 0xFu; v3 = a1[result] )
++result;
LABEL_7:
if ( v3 == 127 || v3 < v4 )
{
if ( result != a2 )
goto LABEL_10;
result = 0LL;
}
else
{
++result;
}
return result;
}

original_code:
static size_t get_seq_len(const char *s, size_t buflen)
{
 size_t i = 0;
 if (buflen == 0) {
  return 0;
 }
 if (s[0]) {
  i = 1;
 }
 if (s[0] == '\033') {
  int final_byte_start = 0x30;
  if (s[1] == '[') {
   final_byte_start = 0x40;
   i = 2;
   while (s[i] >= 0x30 && s[i] < 0x40) {
    i++;
   }
  }
  if (s[1] == 'O') {
   i = 2;
  } else {
   while (s[i] >= 0x20 && s[i] < 0x30) {
    i++;
   }
  }
  if (s[i] >= final_byte_start && s[i] < 0x7f) {
   i++;
   return i;
  }
  if (i == buflen) {
   return 0;
  }
 }
 if (!s[i]) {
  return i + 1;
 }
 return i;
}

canonical_original_code:
static unsigned long get_seq_len(char *s, unsigned long buflen)
{
 unsigned long i = 0;
 if (buflen == 0) {
  return 0;
 }
 if (s[0]) {
  i = 1;
 }
 if (s[0] == '\033') {
  int final_byte_start = 0x30;
  if (s[1] == '[') {
   final_byte_start = 0x40;
   i = 2;
   while (s[i] >= 0x30 && s[i] < 0x40) {
    i++;
   }
  }
  if (s[1] == 'O') {
   i = 2;
  } else {
   while (s[i] >= 0x20 && s[i] < 0x30) {
    i++;
   }
  }
  if (s[i] >= final_byte_start && s[i] < 0x7f) {
   i++;
   return i;
  }
  if (i == buflen) {
   return 0;
  }
 }
 if (!s[i]) {
  return i + 1;
 }
 return i;
}

variable_types:
buflen
unsigned long

variable_types:
i
unsigned long

return_type: unsigned long