wfaCreateTCPServSock

decompiled_code:
__int64 __fastcall wfaCreateTCPServSock(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
int v3; // edx
const void *v4; // rcx
socklen_t v5; // er8
__int64 result; // rax
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", 119LL);
fwrite("createTCPServSock socket() failed", 1uLL, 0x21uLL, stderr);
result = 1LL;
}
else
{
v2 = v1;
addr = 0LL;
wfaGetifAddr(&gnetIf, (__m128i *)&addr);
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(a1);
setsockopt(v2, 1, v3, v4, v5);
if ( bind(v2, &addr, 0x10u) < 0 )
{
fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", 134LL);
fwrite("bind() failed", 1uLL, 0xDuLL, stderr);
result = 1LL;
}
else
{
if ( listen(v2, 2) < 0 )
{
v2 = 1;
fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", 141LL);
fwrite("listen() failed", 1uLL, 0xFuLL, stderr);
}
result = v2;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int16 a1)
{
int v1; // eax
unsigned int v2; // er12
int v3; // edx
const void *v4; // rcx
socklen_t v5; // er8
__int64 result; // rax
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", 119LL);
fwrite("createTCPServSock socket() failed", 1uLL, 0x21uLL, stderr);
result = 1LL;
}
else
{
v2 = v1;
addr = 0LL;
func12(&gnetIf, (__m128i *)&addr);
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(a1);
setsockopt(v2, 1, v3, v4, v5);
if ( bind(v2, &addr, 0x10u) < 0 )
{
fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", 134LL);
fwrite("bind() failed", 1uLL, 0xDuLL, stderr);
result = 1LL;
}
else
{
if ( listen(v2, 2) < 0 )
{
v2 = 1;
fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", 141LL);
fwrite("listen() failed", 1uLL, 0xFuLL, stderr);
}
result = v2;
}
}
return result;
}

original_code:
int wfaCreateTCPServSock(unsigned short port)
{
    int sock;
    struct sockaddr_in servAddr;
    const int on = 1;
    if ((sock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", (long)119); fprintf(stderr, "createTCPServSock socket() failed");
        return WFA_FAILURE;
    }
    memset(&servAddr, 0, sizeof(servAddr));
    wfaGetifAddr(gnetIf, &servAddr);
    servAddr.sin_family = 2;
    servAddr.sin_port = htons(port);
    setsockopt(sock, 1, 2, &on, sizeof(on));
    if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
    {
        fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", (long)134); fprintf(stderr, "bind() failed");
        return WFA_FAILURE;
    }
    if (listen(sock, 2) < 0)
    {
        fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", (long)141); fprintf(stderr, "listen() failed");
        return WFA_FAILURE;
    }
    return sock;
}

canonical_original_code:
int wfaCreateTCPServSock(unsigned short port)
{
    int sock;
    struct sockaddr_in servAddr;
    int on = 1;
    if ((sock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", (long)119); fprintf(stderr, "createTCPServSock socket() failed");
        return WFA_FAILURE;
    }
    memset(&servAddr, 0, sizeof(servAddr));
    wfaGetifAddr(gnetIf, &servAddr);
    servAddr.sin_family = 2;
    servAddr.sin_port = htons(port);
    setsockopt(sock, 1, 2, &on, sizeof(on));
    if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
    {
        fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", (long)134); fprintf(stderr, "bind() failed");
        return WFA_FAILURE;
    }
    if (listen(sock, 2) < 0)
    {
        fprintf(stderr, "File %s, Line %ld: ", "wfa_sock.c", (long)141); fprintf(stderr, "listen() failed");
        return WFA_FAILURE;
    }
    return sock;
}

variable_types:
port
unsigned short

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

variable_types: on int


wfaSetProcPriority

repo: wfapf/TDLS

decompiled_code:
__int64 __fastcall wfaSetProcPriority(int a1)
{
unsigned int v1; // er12
int v2; // eax
struct sched_param param; // [rsp+Ch] [rbp-1Ch] BYREF

param.__sched_priority = 0;
sched_getparam(0, &param);
v1 = param.__sched_priority;
if ( a1 )
{
if ( geteuid() )
{
LABEL_5:
mlockall(3);
return v1;
}
v2 = sched_get_priority_max(1);
if ( v2 != -1 )
{
param.__sched_priority = v2;
sched_setscheduler(0, 1, &param);
goto LABEL_5;
}
v1 = 1;
}
else
{
if ( !geteuid() )
{
param.__sched_priority = 0;
sched_setscheduler(0, 0, &param);
}
munlockall();
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1)
{
unsigned int v1; // er12
int v2; // eax
struct sched_param param; // [rsp+Ch] [rbp-1Ch] BYREF

param.__sched_priority = 0;
sched_getparam(0, &param);
v1 = param.__sched_priority;
if ( a1 )
{
if ( geteuid() )
{
LABEL_5:
mlockall(3);
return v1;
}
v2 = sched_get_priority_max(1);
if ( v2 != -1 )
{
param.__sched_priority = v2;
sched_setscheduler(0, 1, &param);
goto LABEL_5;
}
v1 = 1;
}
else
{
if ( !geteuid() )
{
param.__sched_priority = 0;
sched_setscheduler(0, 0, &param);
}
munlockall();
}
return v1;
}

original_code:
int wfaSetProcPriority(int set)
{
    int maxprio, currprio;
    struct sched_param schp;
    memset(&schp, 0, sizeof(schp));
    sched_getparam(0, &schp);
    currprio = schp.sched_priority;
    if(set != 0)
    {
        if(geteuid() == 0)
        {
           maxprio = sched_get_priority_max(1);
           if(maxprio == -1)
           {
              return WFA_FAILURE;
           }
           schp.sched_priority = maxprio;
           if(sched_setscheduler(0, 1, &schp) != 0)
           {
           }
        }
        if(mlockall(1 | 2) != 0)
        {
        }
    }
    else
    {
        if(geteuid() == 0)
        {
           schp.sched_priority = 0;
           if(sched_setscheduler(0, 0, &schp) != 0)
           {
           }
        }
        munlockall();
    }
    return currprio;
}

canonical_original_code:
int wfaSetProcPriority(int set)
{
    int maxprio; int currprio;
    struct sched_param schp;
    memset(&schp, 0, sizeof(schp));
    sched_getparam(0, &schp);
    currprio = schp.sched_priority;
    if(set != 0)
    {
        if(geteuid() == 0)
        {
           maxprio = sched_get_priority_max(1);
           if(maxprio == -1)
           {
              return WFA_FAILURE;
           }
           schp.sched_priority = maxprio;
           if(sched_setscheduler(0, 1, &schp) != 0)
           {
           }
        }
        if(mlockall(1 | 2) != 0)
        {
        }
    }
    else
    {
        if(geteuid() == 0)
        {
           schp.sched_priority = 0;
           if(sched_setscheduler(0, 0, &schp) != 0)
           {
           }
        }
        munlockall();
    }
    return currprio;
}

variable_types: set int

variable_types: maxprio int

variable_types: currprio int

variable_types: struct sched_param { int sched_priority; }

return_type: int


NotDefinedYet

decompiled_code:
__int64 NotDefinedYet()
{
if ( (wfa_defined_debug & 4) != 0 )
fwrite("The command processing function not defined.\n", 1uLL, 0x2DuLL, stdout);
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
if ( (wfa_defined_debug & 4) != 0 )
fwrite("The command processing function not defined.\n", 1uLL, 0x2DuLL, stdout);
return 0LL;
}

original_code:
int NotDefinedYet(int len, unsigned char *params, int *respLen, BYTE *respBuf)
{
    if(wfa_defined_debug & 0x0004) fprintf(stdout, "The command processing function not defined.\n");
    return WFA_SUCCESS;
}

canonical_original_code:
int NotDefinedYet(int len, unsigned char *params, int *respLen, unsigned char *respBuf)
{
    if(wfa_defined_debug & 0x0004) fprintf(stdout, "The command processing function not defined.\n");
    return WFA_SUCCESS;
}

variable_types: len int

variable_types:
params
unsigned char *

variable_types:
respLen
int *

variable_types:
respBuf
unsigned char *


cname

repo: wfeldt/parti

original_code:
char *cname(void *buf, int len)
{
  static char name[1024];
  int i;
  char *n;
  if(len > sizeof name - 1) len = sizeof name - 1;
  memcpy(name, buf, len);
  name[len] = 0;
  for(n = name, i = len - 1; i >= 0; i--) {
    if(n[i] == 0 || n[i] == 0x20 || n[i] == 0x09 || n[i] == 0x0a) {
      n[i] = 0;
    }
    else {
      break;
    }
  }
  return name;
}

variable_types:
buf
void *

variable_types:
name
char [1024]

variable_types: i int

variable_types:
n
char *

return_type: char *


decompiled_code:
__int64 __fastcall read_qword_le(__int64 a1)
{
__int64 v1; // rsi
unsigned __int8 *v2; // r8

v1 = (unsigned int)read_dword_le((unsigned __int8 *)(a1 + 4));
return (v1 << 32) + (unsigned int)read_dword_le(v2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v1; // rsi
unsigned __int8 *v2; // r8

v1 = (unsigned int)func4((unsigned __int8 *)(a1 + 4));
return (v1 << 32) + (unsigned int)func4(v2);
}

original_code:
uint64_t read_qword_le(void *buf)
{
  return ((uint64_t) read_dword_le(buf + 4) << 32) + read_dword_le(buf);
}

canonical_original_code:
unsigned long read_qword_le(void *buf)
{
  return ((unsigned long) read_dword_le(buf + 4) << 32) + read_dword_le(buf);
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall read_qword_be(unsigned __int8 *a1)
{
__int64 v1; // rsi

v1 = (unsigned int)read_dword_be(a1);
return (v1 << 32) + (unsigned int)read_dword_be(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 *a1)
{
__int64 v1; // rsi

v1 = (unsigned int)func5(a1);
return (v1 << 32) + (unsigned int)func5(a1 + 4);
}

original_code:
uint64_t read_qword_be(void *buf)
{
  return ((uint64_t) read_dword_be(buf) << 32) + read_dword_be(buf + 4);
}

canonical_original_code:
unsigned long read_qword_be(void *buf)
{
  return ((unsigned long) read_dword_be(buf) << 32) + read_dword_be(buf + 4);
}


decompiled_code:
int log_info(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( (opt[16] & 1) == 0 )
{
va_start(arg, format);
result = vfprintf(stdout, format, arg);
}
return result;
}

canonical_decompiled_code:
int func8(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( (opt[16] & 1) == 0 )
{
va_start(arg, format);
result = vfprintf(stdout, format, arg);
}
return result;
}

original_code:
void log_info(const char *format, ...)
{
  if(opt.json) return;
  va_list args;
  __builtin_va_start(args,format);
  vfprintf(stdout, format, args);
  __builtin_va_end(args);
}

canonical_original_code:
void log_info(char *format, ...)
{
  if(opt.json) return;
  __builtin_va_list args;
  __builtin_va_start(args,format);
  vfprintf(stdout, format, args);
  __builtin_va_end(args);
}

variable_types:
format
char *

return_type: void


cmpuid

decompiled_code:
_BOOL8 __fastcall cmpuid(__int64 a1, _DWORD *a2)
{
return *a2 == *(_DWORD *)(a1 + 16);
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1, _DWORD *a2)
{
return *a2 == *(_DWORD *)(a1 + 16);
}

original_code:
static inline int cmpuid(void *d1, void *d2)
{
 struct passwd *pw = (struct passwd *) d1;
 uid_t *uid = (uid_t *) d2;
 return (*uid == pw->pw_uid);
}

canonical_original_code:
static inline int cmpuid(void *d1, void *d2)
{
 struct passwd *pw = (struct passwd *) d1;
 unsigned int *uid = (uid_t *) d2;
 return (*uid == pw->pw_uid);
}

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types:
uid
unsigned int *


decompiled_code:
__int64 __fastcall internal_getpwent(struct passwd *resultbuf, char *buffer, size_t buflen, _DWORD *a4, __int64 a5, __int64 (__fastcall *a6)(struct passwd *, __int64))
{
FILE *v10; // rdi
__int64 result; // rax
int i; // eax
int v13; // er15
struct passwd *v15[8]; // [rsp+18h] [rbp-40h] BYREF

v10 = fpwent;
if ( a5 || !fpwent )
{
result = internal_setent(&fpwent);
if ( (_DWORD)result != 1 )
return result;
v10 = fpwent;
}
for ( i = fgetpwent_r(v10, resultbuf, buffer, buflen, v15); ; i = fgetpwent_r(fpwent, resultbuf, buffer, buflen, v15) )
{
v13 = i;
if ( !a6 )
break;
result = a6(resultbuf, a5);
if ( (_DWORD)result )
break;
if ( v13 )
{
if ( v13 != 34 )
return result;
goto LABEL_12;
}
}
if ( v13 == 34 )
{
LABEL_12:
*a4 = 34;
return 4294967294LL;
}
return v13 == 0;
}

canonical_decompiled_code:
__int64 __fastcall func1(struct passwd *resultbuf, char *buffer, size_t buflen, _DWORD *a4, __int64 a5, __int64 (__fastcall *a6)(struct passwd *, __int64))
{
FILE *v10; // rdi
__int64 result; // rax
int i; // eax
int v13; // er15
struct passwd *v15[8]; // [rsp+18h] [rbp-40h] BYREF

v10 = fpwent;
if ( a5 || !fpwent )
{
result = internal_setent(&fpwent);
if ( (_DWORD)result != 1 )
return result;
v10 = fpwent;
}
for ( i = fgetpwent_r(v10, resultbuf, buffer, buflen, v15); ; i = fgetpwent_r(fpwent, resultbuf, buffer, buflen, v15) )
{
v13 = i;
if ( !a6 )
break;
result = a6(resultbuf, a5);
if ( (_DWORD)result )
break;
if ( v13 )
{
if ( v13 != 34 )
return result;
goto LABEL_12;
}
}
if ( v13 == 34 )
{
LABEL_12:
*a4 = 34;
return 4294967294LL;
}
return v13 == 0;
}

original_code:
static enum nss_status internal_getpwent(struct passwd *pw, char *buffer,
      size_t buflen, int *errnop,
      void *cmpdata,
      int (*cmpFunc)(void *, void *))
{
 struct passwd *pwp;
 int r = 0;
 enum nss_status result = NSS_STATUS_SUCCESS;
 if ((fpwent.stream == ((void *)0)) || (cmpdata != ((void *)0)))
  result = internal_setent(&fpwent);
 if (result != NSS_STATUS_SUCCESS)
  return result;
 while (!r) {
  r = fgetpwent_r(fpwent.stream, pw, buffer, buflen, &pwp);
  if ( (cmpFunc == ((void *)0)) ||
       (cmpFunc(pw, cmpdata)))
   break;
 }
 if (r == 34) {
  *errnop = 34;
  return NSS_STATUS_TRYAGAIN;
 }
 if (r)
  return NSS_STATUS_NOTFOUND;
 return result;
}

canonical_original_code:
static enum nss_status internal_getpwent(struct passwd *pw, char *buffer, unsigned long buflen, int *errnop, void *cmpdata, int (*cmpFunc)(void *, void *))
{
 struct passwd *pwp;
 int r = 0;
 enum nss_status result = NSS_STATUS_SUCCESS;
 if ((fpwent.stream == ((void *)0)) || (cmpdata != ((void *)0)))
  result = internal_setent(&fpwent);
 if (result != NSS_STATUS_SUCCESS)
  return result;
 while (!r) {
  r = fgetpwent_r(fpwent.stream, pw, buffer, buflen, &pwp);
  if ( (cmpFunc == ((void *)0)) ||
       (cmpFunc(pw, cmpdata)))
   break;
 }
 if (r == 34) {
  *errnop = 34;
  return NSS_STATUS_TRYAGAIN;
 }
 if (r)
  return NSS_STATUS_NOTFOUND;
 return result;
}

variable_types:
buffer
char *

variable_types:
buflen
unsigned long

variable_types:
errnop
int *

variable_types:
cmpdata
void *

variable_types:
cmpFunc
int (*)(void *, void *)

variable_types: r int

variable_types: enum nss_status { NSS_STATUS_TRYAGAIN=-2, NSS_STATUS_UNAVAIL=-1, NSS_STATUS_NOTFOUND=0, NSS_STATUS_SUCCESS=1, NSS_STATUS_RETURN=2 }

user_defined_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; }


putlog

repo: wgjtyu/xmbot

decompiled_code:
int __fastcall putlog(const char *a1, unsigned int a2, const char *a3)
{
log = fopen("/tmp/xmbot.log", "a");
fprintf(log, "(%3d)%s:%s\n", a2, a1, a3);
return fclose(log);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, unsigned int a2, const char *a3)
{
log = fopen("/tmp/xmbot.log", "a");
fprintf(log, "(%3d)%s:%s\n", a2, a1, a3);
return fclose(log);
}

original_code:
void putlog(const char *func,int line,const char *detail){
    log=fopen("/tmp/xmbot.log","a");
    fprintf(log,"(%3d)%s:%s\n",line,func,detail);
    fclose(log);
}

canonical_original_code:
void putlog(char *func, int line, char *detail){
    log=fopen("/tmp/xmbot.log","a");
    fprintf(log,"(%3d)%s:%s\n",line,func,detail);
    fclose(log);
}

variable_types:
func
char *

variable_types: line int

variable_types:
detail
char *


decompiled_code:
void __fastcall __noreturn j_error(const char *a1)
{
log = fopen("/tmp/xmbot.log", "a");
fprintf(stderr, "xmbot: %s\n", a1);
fprintf(log, "xmbot: %s\n", a1);
fclose(log);
exit(2);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(const char *a1)
{
log = fopen("/tmp/xmbot.log", "a");
fprintf(stderr, "xmbot: %s\n", a1);
fprintf(log, "xmbot: %s\n", a1);
fclose(log);
exit(2);
}

original_code:
void j_error (char *msg)
{
    log=fopen("/tmp/xmbot.log","a");
    fprintf (stderr, "xmbot: %s\n", msg);
    fprintf(log,"xmbot: %s\n",msg);
    fclose(log);
    exit (2);
}

canonical_original_code:
void j_error(char *msg)
{
    log=fopen("/tmp/xmbot.log","a");
    fprintf (stderr, "xmbot: %s\n", msg);
    fprintf(log,"xmbot: %s\n",msg);
    fclose(log);
    exit (2);
}

variable_types:
msg
char *


decompiled_code:
void __fastcall sig_handler(unsigned int a1)
{
printf("I get %d signal, I will stop\n", a1);
server_run = 0;
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
printf("I get %d signal, I will stop\n", a1);
server_run = 0;
}

original_code:
void sig_handler(int signum) {
    printf("I get %d signal, I will stop\n", signum);
    server_run = 0;
}

variable_types: signum int


decompiled_code:
void __fastcall __noreturn client_thread(void *a1)
{
int v1; // ebp
int v2; // eax
int *v3; // rax
__int64 v4; // r12
char *v5; // rax
int *v6; // rax
__int64 v7; // rbp
char *v8; // rax
int v9; // eax
int *v10; // rax
__int64 v11; // r12
char *v12; // rax
int *v13; // rax
__int64 v14; // r12
char *v15; // rax
int retval; // [rsp+4h] [rbp-44h] BYREF
struct pollfd fds; // [rsp+8h] [rbp-40h] BYREF
__int128 buf; // [rsp+10h] [rbp-38h] BYREF
int v19; // [rsp+20h] [rbp-28h]

v1 = *((_DWORD *)a1 + 1);
retval = 0;
while ( 1 )
{
fds.fd = v1;
fds.events = 1;
v2 = poll(&fds, 1uLL, 3500);
v19 = 0;
buf = 0LL;
if ( v2 == -1 )
break;
if ( v2 && (fds.revents & 1) != 0 )
{
v9 = recv(v1, &buf, 0x14uLL, 256);
if ( v9 == -1 )
{
v13 = __errno_location();
v14 = (unsigned int)*v13;
v15 = strerror(*v13);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 164LL, "client_thread", "Recv", v15, v14);
retval = 2;
LABEL_6:
if ( !close(v1) )
{
LABEL_7:
pthread_mutex_lock((pthread_mutex_t *)((char *)a1 + 24));
*((_DWORD *)a1 + 16) = 0;
pthread_mutex_unlock((pthread_mutex_t *)((char *)a1 + 24));
pthread_exit(&retval);
}
LABEL_9:
v6 = __errno_location();
v7 = (unsigned int)*v6;
v8 = strerror(*v6);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 232LL, "client_thread", "Close socket", v8, v7);
goto LABEL_7;
}
if ( v9 <= 19 )
{
v10 = __errno_location();
v11 = (unsigned int)*v10;
v12 = strerror(*v10);
fprintf(
stderr,
"%s:%d, *%s* %s '%s'[%d]\n",
"server.c",
167LL,
"client_thread",
"Recv, not full data",
v12,
v11);
retval = 3;
goto LABEL_6;
}
if ( (_BYTE)buf )
{
fprintf(stderr, "%s:%d, *%s* %s\n", "server.c", 183LL, "client_thread", "Incorrect command");
retval = 1;
goto LABEL_6;
}
puts("Headbeat command");
}
}
v3 = __errno_location();
v4 = (unsigned int)*v3;
v5 = strerror(*v3);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 155LL, "client_thread", "Poll", v5, v4);
retval = 1;
if ( !close(v1) )
goto LABEL_7;
goto LABEL_9;
}

canonical_decompiled_code:
void __fastcall __noreturn func9(void *a1)
{
int v1; // ebp
int v2; // eax
int *v3; // rax
__int64 v4; // r12
char *v5; // rax
int *v6; // rax
__int64 v7; // rbp
char *v8; // rax
int v9; // eax
int *v10; // rax
__int64 v11; // r12
char *v12; // rax
int *v13; // rax
__int64 v14; // r12
char *v15; // rax
int retval; // [rsp+4h] [rbp-44h] BYREF
struct pollfd fds; // [rsp+8h] [rbp-40h] BYREF
__int128 buf; // [rsp+10h] [rbp-38h] BYREF
int v19; // [rsp+20h] [rbp-28h]

v1 = *((_DWORD *)a1 + 1);
retval = 0;
while ( 1 )
{
fds.fd = v1;
fds.events = 1;
v2 = poll(&fds, 1uLL, 3500);
v19 = 0;
buf = 0LL;
if ( v2 == -1 )
break;
if ( v2 && (fds.revents & 1) != 0 )
{
v9 = recv(v1, &buf, 0x14uLL, 256);
if ( v9 == -1 )
{
v13 = __errno_location();
v14 = (unsigned int)*v13;
v15 = strerror(*v13);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 164LL, "client_thread", "Recv", v15, v14);
retval = 2;
LABEL_6:
if ( !close(v1) )
{
LABEL_7:
pthread_mutex_lock((pthread_mutex_t *)((char *)a1 + 24));
*((_DWORD *)a1 + 16) = 0;
pthread_mutex_unlock((pthread_mutex_t *)((char *)a1 + 24));
pthread_exit(&retval);
}
LABEL_9:
v6 = __errno_location();
v7 = (unsigned int)*v6;
v8 = strerror(*v6);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 232LL, "client_thread", "Close socket", v8, v7);
goto LABEL_7;
}
if ( v9 <= 19 )
{
v10 = __errno_location();
v11 = (unsigned int)*v10;
v12 = strerror(*v10);
fprintf(
stderr,
"%s:%d, *%s* %s '%s'[%d]\n",
"server.c",
167LL,
"client_thread",
"Recv, not full data",
v12,
v11);
retval = 3;
goto LABEL_6;
}
if ( (_BYTE)buf )
{
fprintf(stderr, "%s:%d, *%s* %s\n", "server.c", 183LL, "client_thread", "Incorrect command");
retval = 1;
goto LABEL_6;
}
puts("Headbeat command");
}
}
v3 = __errno_location();
v4 = (unsigned int)*v3;
v5 = strerror(*v3);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 155LL, "client_thread", "Poll", v5, v4);
retval = 1;
if ( !close(v1) )
goto LABEL_7;
goto LABEL_9;
}

original_code:
void *client_thread(void *ptr) {
    struct thread_conf *my_conf = (struct thread_conf *) ptr;
    int sockfd = my_conf->sockfd;
    int command_loop = 1;
    int skip = 0;
    while(command_loop && !skip) {
        struct pollfd client;
        client.fd = sockfd;
        client.events = 0x001;
        int ret = poll(&client, 1, 3500);
        int parse_comm = 0;
        char com[20];
        memset(com, 0, 20);
        if (-1 == ret) {
            if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 155, __FUNCTION__, "Poll", strerror(myerr), myerr); };
            skip = 1;
        } else if(0 == ret) {
            continue;
        } else {
            int bytes = 0;
            if (client.revents & 0x001) {
                bytes = recv(sockfd, com, 20, MSG_WAITALL);
                if (-1 == bytes) {
                    if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 164, __FUNCTION__, "Recv", strerror(myerr), myerr); };
                    skip = 2;
                } else if (bytes < 20) {
                    if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 167, __FUNCTION__, "Recv, not full data", strerror(myerr), myerr); };
                    skip = 3;
                } else {
                    parse_comm = 1;
                }
            }
            ret = bytes;
        }
        if (parse_comm) {
            switch(com[0]) {
            case 0:
                printf("Headbeat command\n");
                break;
            default:
                if (1) { fprintf(stderr, "%s:%d, *%s* %s\n", "server.c", 183, __FUNCTION__, "Incorrect command"); };
                skip = 1;
            }
        }
    }
 if (close(sockfd)) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 232, __FUNCTION__, "Close socket", strerror(myerr), myerr); };
    }
    pthread_mutex_lock(&(my_conf->lock));
    my_conf->status = 0;
    pthread_mutex_unlock(&(my_conf->lock));
    pthread_exit(&skip);
}

variable_types:
ptr
void *

variable_types: struct thread_conf { int id; int sockfd; int iret; unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; int status; } *

variable_types: sockfd int

variable_types: skip int

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

variable_types: ret int

variable_types:
com
char [20]

variable_types: myerr int

variable_types: bytes int

return_type: void *

user_defined_types: struct thread_conf { int id; int sockfd; int iret; unsigned long thread; union pthread_mutex_t lock; int status; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


server

decompiled_code:
int __fastcall server(unsigned __int16 a1, int a2)
{
int v3; // er14
socklen_t v4; // edx
unsigned int v5; // er13
pthread_mutex_t *v6; // rbx
pthread_mutex_t *v7; // rdi
int v8; // er12
char *v9; // rbx
int v10; // er10
pthread_mutex_t *v11; // rdi
int v12; // eax
char *v13; // r15
pthread_t v14; // rdi
__int64 v15; // rsi
int result; // eax
int *v17; // rax
__int64 v18; // rbx
char *v19; // rax
int *v20; // rax
__int64 v21; // rbx
char *v22; // rax
int *v23; // rax
__int64 v24; // rbx
char *v25; // rax
int *v26; // rax
__int64 v27; // rbx
char *v28; // rax
int v29; // [rsp-10h] [rbp-88h]
char *v30; // [rsp+8h] [rbp-70h]
socklen_t addr_len; // [rsp+1Ch] [rbp-5Ch] BYREF
struct sockaddr v32; // [rsp+20h] [rbp-58h] BYREF
struct sockaddr fds[4]; // [rsp+30h] [rbp-48h] BYREF

v3 = socket(2, 1, 0);
v32.sa_family = 2;
*(_DWORD *)&v32.sa_data[2] = _bswap_32_constprop_0(a1, &v32, 16LL);
*(_WORD *)v32.sa_data = _bswap_16(a1);
if ( bind(v3, &v32, v4) )
{
v26 = __errno_location();
v27 = (unsigned int)*v26;
v28 = strerror(*v26);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 63LL, "server", "Bind", v28, v27);
exit(2);
}
puts("Bind is ok");
v5 = listen(v3, 5);
if ( v5 )
{
v23 = __errno_location();
v24 = (unsigned int)*v23;
v25 = strerror(*v23);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 70LL, "server", "Listen", v25, v24);
exit(3);
}
puts("Listen is ok");
v30 = (char *)malloc(72LL * a2);
if ( a2 > 0 )
{
v6 = (pthread_mutex_t *)(v30 + 24);
do
{
v6[1].__lock = -1;
v7 = v6;
v6 = (pthread_mutex_t *)((char *)v6 + 72);
pthread_mutex_init(v7, 0LL);
}
while ( v6 != (pthread_mutex_t *)&v30[72 * (a2 - 1) + 96] );
v8 = 0;
while ( 1 )
{
while ( 1 )
{
if ( !server_run )
{
LABEL_17:
v13 = v30 + 8;
do
{
v14 = *((_QWORD *)v13 + 1);
v13 += 72;
pthread_join(v14, 0LL);
v15 = v5++;
printf("Thread #%d returns: %d\n", v15, *((unsigned int *)v13 - 18));
}
while ( a2 != v5 );
goto LABEL_19;
}
v9 = &v30[72 * v8];
pthread_mutex_lock((pthread_mutex_t *)(v9 + 24));
v10 = *((_DWORD *)v9 + 16);
if ( v10 )
break;
pthread_join(*((_QWORD *)v9 + 2), 0LL);
printf("Thread #%d returns: %d\n", (unsigned int)v8, *((unsigned int *)v9 + 2));
pthread_mutex_unlock((pthread_mutex_t *)(v9 + 24));
LABEL_12:
puts("server waiting for connection");
if ( server_run )
{
while ( 1 )
{
*(_DWORD *)&fds[0].sa_family = v3;
*(_WORD *)&fds[0].sa_data[2] = 1;
v12 = poll((struct pollfd *)fds, 1uLL, 3500);
if ( v12 == -1 )
{
v20 = __errno_location();
v21 = (unsigned int)*v20;
v22 = strerror(*v20);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 106LL, "server", "Poll", v22, v21);
goto LABEL_17;
}
if ( v12 )
break;
if ( !server_run )
goto LABEL_17;
}
if ( server_run )
{
addr_len = 16;
*((_DWORD *)v9 + 1) = accept(v3, fds, &addr_len);
printf("server crating thread #%d\n", (unsigned int)v8);
*((_DWORD *)v9 + 16) = 1;
*((_DWORD *)v9 + 2) = pthread_create((pthread_t *)v9 + 2, 0LL, (void *(*)(void *))client_thread, v9);
v8 = (v8 + 1) % a2;
if ( a2 > v8 )
continue;
}
}
goto LABEL_17;
}
v11 = (pthread_mutex_t *)(v9 + 24);
if ( v10 <= 0 )
{
pthread_mutex_unlock(v11);
goto LABEL_12;
}
pthread_mutex_unlock(v11);
v8 = (v8 + 1) % a2;
if ( a2 <= v8 )
goto LABEL_17;
}
}
LABEL_19:
result = close(v3);
if ( result )
{
v17 = __errno_location();
v18 = (unsigned int)*v17;
v19 = strerror(*v17);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 133LL, "server", "Close server socket", v19, v18);
result = v29;
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(unsigned __int16 a1, int a2)
{
int v3; // er14
socklen_t v4; // edx
unsigned int v5; // er13
pthread_mutex_t *v6; // rbx
pthread_mutex_t *v7; // rdi
int v8; // er12
char *v9; // rbx
int v10; // er10
pthread_mutex_t *v11; // rdi
int v12; // eax
char *v13; // r15
pthread_t v14; // rdi
__int64 v15; // rsi
int result; // eax
int *v17; // rax
__int64 v18; // rbx
char *v19; // rax
int *v20; // rax
__int64 v21; // rbx
char *v22; // rax
int *v23; // rax
__int64 v24; // rbx
char *v25; // rax
int *v26; // rax
__int64 v27; // rbx
char *v28; // rax
int v29; // [rsp-10h] [rbp-88h]
char *v30; // [rsp+8h] [rbp-70h]
socklen_t addr_len; // [rsp+1Ch] [rbp-5Ch] BYREF
struct sockaddr v32; // [rsp+20h] [rbp-58h] BYREF
struct sockaddr fds[4]; // [rsp+30h] [rbp-48h] BYREF

v3 = socket(2, 1, 0);
v32.sa_family = 2;
*(_DWORD *)&v32.sa_data[2] = func10(a1, &v32, 16LL);
*(_WORD *)v32.sa_data = func8(a1);
if ( bind(v3, &v32, v4) )
{
v26 = __errno_location();
v27 = (unsigned int)*v26;
v28 = strerror(*v26);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 63LL, "server", "Bind", v28, v27);
exit(2);
}
puts("Bind is ok");
v5 = listen(v3, 5);
if ( v5 )
{
v23 = __errno_location();
v24 = (unsigned int)*v23;
v25 = strerror(*v23);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 70LL, "server", "Listen", v25, v24);
exit(3);
}
puts("Listen is ok");
v30 = (char *)malloc(72LL * a2);
if ( a2 > 0 )
{
v6 = (pthread_mutex_t *)(v30 + 24);
do
{
v6[1].__lock = -1;
v7 = v6;
v6 = (pthread_mutex_t *)((char *)v6 + 72);
pthread_mutex_init(v7, 0LL);
}
while ( v6 != (pthread_mutex_t *)&v30[72 * (a2 - 1) + 96] );
v8 = 0;
while ( 1 )
{
while ( 1 )
{
if ( !server_run )
{
LABEL_17:
v13 = v30 + 8;
do
{
v14 = *((_QWORD *)v13 + 1);
v13 += 72;
pthread_join(v14, 0LL);
v15 = v5++;
printf("Thread #%d returns: %d\n", v15, *((unsigned int *)v13 - 18));
}
while ( a2 != v5 );
goto LABEL_19;
}
v9 = &v30[72 * v8];
pthread_mutex_lock((pthread_mutex_t *)(v9 + 24));
v10 = *((_DWORD *)v9 + 16);
if ( v10 )
break;
pthread_join(*((_QWORD *)v9 + 2), 0LL);
printf("Thread #%d returns: %d\n", (unsigned int)v8, *((unsigned int *)v9 + 2));
pthread_mutex_unlock((pthread_mutex_t *)(v9 + 24));
LABEL_12:
puts("server waiting for connection");
if ( server_run )
{
while ( 1 )
{
*(_DWORD *)&fds[0].sa_family = v3;
*(_WORD *)&fds[0].sa_data[2] = 1;
v12 = poll((struct pollfd *)fds, 1uLL, 3500);
if ( v12 == -1 )
{
v20 = __errno_location();
v21 = (unsigned int)*v20;
v22 = strerror(*v20);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 106LL, "server", "Poll", v22, v21);
goto LABEL_17;
}
if ( v12 )
break;
if ( !server_run )
goto LABEL_17;
}
if ( server_run )
{
addr_len = 16;
*((_DWORD *)v9 + 1) = accept(v3, fds, &addr_len);
printf("server crating thread #%d\n", (unsigned int)v8);
*((_DWORD *)v9 + 16) = 1;
*((_DWORD *)v9 + 2) = pthread_create((pthread_t *)v9 + 2, 0LL, (void *(*)(void *))client_thread, v9);
v8 = (v8 + 1) % a2;
if ( a2 > v8 )
continue;
}
}
goto LABEL_17;
}
v11 = (pthread_mutex_t *)(v9 + 24);
if ( v10 <= 0 )
{
pthread_mutex_unlock(v11);
goto LABEL_12;
}
pthread_mutex_unlock(v11);
v8 = (v8 + 1) % a2;
if ( a2 <= v8 )
goto LABEL_17;
}
}
LABEL_19:
result = close(v3);
if ( result )
{
v17 = __errno_location();
v18 = (unsigned int)*v17;
v19 = strerror(*v17);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 133LL, "server", "Close server socket", v19, v18);
result = v29;
}
return result;
}

original_code:
void server(int port, int tcnt) {
    int sockfd = socket(2, SOCK_STREAM, 0);
    struct sockaddr_in server_address;
    server_address.sin_family = 2;
    server_address.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    server_address.sin_port = htons(port);
    if (bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address))) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 63, __FUNCTION__, "Bind", strerror(myerr), myerr); };
        exit(2);
    } else {
        printf("Bind is ok\n");
    }
    if (listen(sockfd, 5)) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 70, __FUNCTION__, "Listen", strerror(myerr), myerr); };
        exit(3);
    } else {
        printf("Listen is ok\n");
    }
    struct thread_conf *threads = (struct thread_conf *) malloc(tcnt * sizeof(struct thread_conf));
    int i = 0;
    for(i = 0; i < tcnt; ++i) {
        threads[i].status = -1;
        pthread_mutex_init(&(threads[i].lock), ((void *)0));
    }
    for(i = 0; (i < tcnt) && server_run; i = (i + 1) % tcnt) {
        pthread_mutex_lock(&(threads[i].lock));
        if (!threads[i].status) {
            pthread_join(threads[i].thread, ((void *)0));
            printf("Thread #%d returns: %d\n", i, threads[i].iret);
            pthread_mutex_unlock(&(threads[i].lock));
        } else if (threads[i].status > 0) {
            pthread_mutex_unlock(&(threads[i].lock));
            continue;
        } else {
            pthread_mutex_unlock(&(threads[i].lock));
        }
        printf("server waiting for connection\n");
        int pret = 0;
        while(server_run) {
            struct pollfd serv;
            serv.fd = sockfd;
            serv.events = 0x001;
            pret = poll(&serv, 1, 3500);
            if (-1 == pret) {
                if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 106, __FUNCTION__, "Poll", strerror(myerr), myerr); };
                break;
            } else if(0 == pret) {
                continue;
            } else {
                break;
            }
        }
        if ((-1 == pret) || !server_run) {
            break;
        }
        struct sockaddr_in client_address;
        int client_len = sizeof(client_address);
        threads[i].sockfd = accept(sockfd, (struct sockaddr *) &client_address, &client_len);
        printf("server crating thread #%d\n", i);
        threads[i].status = 1;
        threads[i].iret = pthread_create(&(threads[i].thread), ((void *)0), client_thread, (void*) &(threads[i]));
    }
    for(i = 0; i < tcnt; ++i) {
        pthread_join(threads[i].thread, ((void *)0));
        printf("Thread #%d returns: %d\n", i, threads[i].iret);
    }
    if (close(sockfd)) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 133, __FUNCTION__, "Close server socket", strerror(myerr), myerr); };
    }
}

canonical_original_code:
void server(int port, int tcnt) {
    int sockfd = socket(2, SOCK_STREAM, 0);
    struct sockaddr_in server_address;
    server_address.sin_family = 2;
    server_address.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    server_address.sin_port = htons(port);
    if (bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address))) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 63, __FUNCTION__, "Bind", strerror(myerr), myerr); };
        exit(2);
    } else {
        printf("Bind is ok\n");
    }
    if (listen(sockfd, 5)) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 70, __FUNCTION__, "Listen", strerror(myerr), myerr); };
        exit(3);
    } else {
        printf("Listen is ok\n");
    }
    struct thread_conf *threads = (struct thread_conf *) malloc(tcnt * sizeof(struct thread_conf));
    int i = 0;
    for(i = 0; i < tcnt; ++i) {
        threads[i].status = -1;
        pthread_mutex_init(&(threads[i].lock), ((void *)0));
    }
    for(i = 0; (i < tcnt) && server_run; i = (i + 1) % tcnt) {
        pthread_mutex_lock(&(threads[i].lock));
        if (!threads[i].status) {
            pthread_join(threads[i].thread, ((void *)0));
            printf("Thread #%d returns: %d\n", i, threads[i].iret);
            pthread_mutex_unlock(&(threads[i].lock));
        } else if (threads[i].status > 0) {
            pthread_mutex_unlock(&(threads[i].lock));
            continue;
        } else {
            pthread_mutex_unlock(&(threads[i].lock));
        }
        printf("server waiting for connection\n");
        int pret = 0;
        while(server_run) {
            struct pollfd serv;
            serv.fd = sockfd;
            serv.events = 0x001;
            pret = poll(&serv, 1, 3500);
            if (-1 == pret) {
                if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 106, __FUNCTION__, "Poll", strerror(myerr), myerr); };
                break;
            } else if(0 == pret) {
                continue;
            } else {
                break;
            }
        }
        if ((-1 == pret) || !server_run) {
            break;
        }
        struct sockaddr_in client_address;
        int client_len = sizeof(client_address);
        threads[i].sockfd = accept(sockfd, (struct sockaddr *) &client_address, &client_len);
        printf("server crating thread #%d\n", i);
        threads[i].status = 1;
        threads[i].iret = pthread_create(&(threads[i].thread), ((void *)0), client_thread, (void *) &(threads[i]));
    }
    for(i = 0; i < tcnt; ++i) {
        pthread_join(threads[i].thread, ((void *)0));
        printf("Thread #%d returns: %d\n", i, threads[i].iret);
    }
    if (close(sockfd)) {
        if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "server.c", 133, __FUNCTION__, "Close server socket", strerror(myerr), myerr); };
    }
}

variable_types: port int

variable_types: tcnt int

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

variable_types: i int

variable_types: pret int

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

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

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

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


client

decompiled_code:
int __fastcall client(char *cp, __int16 a2)
{
int v2; // ebp
socklen_t v3; // edx
int v4; // ebx
size_t v5; // rdx
int v6; // ecx
int result; // eax
int *v8; // rax
__int64 v9; // rbx
char *v10; // rax
unsigned int v11; // er12
char *v12; // rax
unsigned int v13; // er12
char *v14; // rax
struct sockaddr v15; // [rsp+0h] [rbp-48h] BYREF
char buf[56]; // [rsp+10h] [rbp-38h] BYREF

v2 = socket(2, 1, 0);
v15.sa_family = 2;
*(_DWORD *)&v15.sa_data[2] = inet_addr(cp);
*(_WORD *)v15.sa_data = _bswap_16(a2);
if ( connect(v2, &v15, v3) )
{
v13 = *__errno_location();
v14 = strerror(v13);
printf("Client: Connect: %s [%d]\n", v14, v13);
exit(2);
}
v4 = 10;
while ( 1 )
{
pack_heart_comm((__int64)buf);
if ( (unsigned int)send(v2, buf, v5, v6) != 20 )
break;
sleep(3u);
if ( !--v4 )
{
result = close(v2);
if ( !result )
return result;
goto LABEL_8;
}
}
v8 = __errno_location();
v9 = (unsigned int)*v8;
v10 = strerror(*v8);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "client.c", 53LL, "client", "Can't send command", v10, v9);
result = close(v2);
if ( !result )
return result;
LABEL_8:
v11 = *__errno_location();
v12 = strerror(v11);
return printf("Client: close server socket: %s [%d]\n", v12, v11);
}

canonical_decompiled_code:
int __fastcall func9(char *cp, __int16 a2)
{
int v2; // ebp
socklen_t v3; // edx
int v4; // ebx
size_t v5; // rdx
int v6; // ecx
int result; // eax
int *v8; // rax
__int64 v9; // rbx
char *v10; // rax
unsigned int v11; // er12
char *v12; // rax
unsigned int v13; // er12
char *v14; // rax
struct sockaddr v15; // [rsp+0h] [rbp-48h] BYREF
char buf[56]; // [rsp+10h] [rbp-38h] BYREF

v2 = socket(2, 1, 0);
v15.sa_family = 2;
*(_DWORD *)&v15.sa_data[2] = inet_addr(cp);
*(_WORD *)v15.sa_data = func7(a2);
if ( connect(v2, &v15, v3) )
{
v13 = *__errno_location();
v14 = strerror(v13);
printf("Client: Connect: %s [%d]\n", v14, v13);
exit(2);
}
v4 = 10;
while ( 1 )
{
func8((__int64)buf);
if ( (unsigned int)send(v2, buf, v5, v6) != 20 )
break;
sleep(3u);
if ( !--v4 )
{
result = close(v2);
if ( !result )
return result;
goto LABEL_8;
}
}
v8 = __errno_location();
v9 = (unsigned int)*v8;
v10 = strerror(*v8);
fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "client.c", 53LL, "client", "Can't send command", v10, v9);
result = close(v2);
if ( !result )
return result;
LABEL_8:
v11 = *__errno_location();
v12 = strerror(v11);
return printf("Client: close server socket: %s [%d]\n", v12, v11);
}

original_code:
void client(char *ip, int port) {
    int sockfd = socket (2, SOCK_STREAM, 0);
    struct sockaddr_in address;
    address.sin_family = 2;
    address.sin_addr.s_addr= inet_addr(ip);
    address.sin_port = htons(port);
    if (connect(sockfd, (struct sockaddr *) &address, sizeof(address))) {
        int myerr = (*__errno_location ());
        printf("Client: Connect: %s [%d]\n", strerror(myerr), myerr);
        exit(2);
    }
    char com[20];
    int i = 0;
    for(i = 0; i < 10; ++i) {
        pack_heart_comm(com);
        int rsnd = send(sockfd, (void*) com, 20, 0);
        if (20 != rsnd) {
            if (1) { int myerr = (*__errno_location ()); fprintf(stderr, "%s:%d, *%s* %s '%s'[%d]\n", "client.c", 53, __FUNCTION__, "Can't send command", strerror(myerr), myerr); };
            break;
        }
        sleep(3);
    }
    if (close(sockfd)) {
        int myerr = (*__errno_location ());
        printf("Client: close server socket: %s [%d]\n", strerror(myerr), myerr);
    }
}

variable_types:
ip
char *

variable_types: rsnd int


main

canonical_name: main

repo: whatsbcn/hexin

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_BYTE *v3; // r13
int v4; // er12
char *v5; // rax
int v6; // ebp
int v7; // ebx
int v8; // edi
struct termios *v9; // rdi
__int64 v10; // rcx
struct termios *v11; // rsi
__int64 v12; // rbx
int v13; // er14
int *v14; // rax
int v15; // eax
int v16; // eax
int v17; // eax
int *v18; // rcx
struct termios *v19; // rdi
__int64 v20; // rcx
struct termios *v21; // rsi
__int64 v22; // [rsp+0h] [rbp-D8h]
int v23; // [rsp+8h] [rbp-D0h]
int *v24; // [rsp+8h] [rbp-D0h]
int *v25; // [rsp+10h] [rbp-C8h]
int *v26; // [rsp+10h] [rbp-C8h]
int v27; // [rsp+10h] [rbp-C8h]
int *v28; // [rsp+18h] [rbp-C0h]
fd_set readfds; // [rsp+20h] [rbp-B8h] BYREF

puts("Hexin Terminal v0.2 - z0nKT1g3r & whats");
v3 = malloc(0x400uLL);
v4 = open("/dev/ptmx", 2);
grantpt(v4);
unlockpt(v4);
v5 = ptsname(v4);
v6 = open(v5, 2);
if ( !fork() )
{
setsid();
v7 = 1;
ioctl(v6, 0x540EuLL, 0LL);
dup2(v6, 0);
dup2(v6, 1);
dup2(v6, 2);
close(v6);
do
{
v8 = v7++;
signal(v8, 0LL);
}
while ( v7 != 64 );
putenv("PS1=\\[\\033[1;30m\\][\\[\\033[0;32m\\]\\u\\[\\033[1;32m\\]@\\[\\033[0;32m\\]\\h \\[\\033[1;37m\\]\\W\\[\\033"
"[1;30m\\]]\\[\\033[0m\\]# ");
execl("/bin/bash", "bash", "--norc", "-i", 0LL);
puts("despres");
exit(1);
}
tcgetattr(0, &oldterm);
sleep(1u);
v9 = &newterm;
v10 = 15LL;
v11 = &oldterm;
while ( v10 )
{
v9->c_iflag = v11->c_iflag;
v11 = (struct termios *)((char *)v11 + 4);
v9 = (struct termios *)((char *)v9 + 4);
--v10;
}
newterm.c_lflag &= 0xFFFFFFF4;
newterm.c_iflag &= 0xFFFFEBFF;
tcsetattr(0, 2, &newterm);
signal(17, sig_die);
signal(2, sig_die);
signal(3, sig_die);
signal(4, sig_die);
signal(6, sig_die);
signal(11, sig_die);
signal(15, sig_die);
v23 = 0;
v12 = v4 / 64;
v22 = 1LL << (v4 % 64);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
readfds.fds_bits[v12] |= v22;
v13 = select(v4 + 1, &readfds, 0LL, 0LL, 0LL);
v14 = __errno_location();
if ( v13 >= 0 )
break;
if ( *v14 != 4 )
goto LABEL_16;
}
if ( (readfds.fds_bits[0] & 1) == 0 )
break;
if ( v23 )
{
v26 = v14;
*v3 = 92;
v17 = read(0, v3 + 1, 0x400uLL);
v18 = v26;
if ( v17 <= 0 && *v26 != 4
|| (v27 = v17, v28 = v18, filter(v3, (unsigned int)v17), write(v4, v3, v27) < 0) && *v28 != 4 )
{
LABEL_16:
tcsetattr(0, 2, &oldterm);
exit(0);
}
tcsetattr(0, 2, &newterm);
memset(v3, 0, 0x400uLL);
}
else
{
v24 = v14;
v15 = read(0, v3, 1uLL);
if ( v15 <= 0 && *v24 != 4 )
goto LABEL_16;
if ( *v3 != 92 )
{
if ( write(v4, v3, v15) < 0 && *v24 != 4 )
goto LABEL_16;
goto LABEL_22;
}
putchar(92);
fflush(stdout);
v19 = &tmpterm;
v20 = 15LL;
v21 = &oldterm;
while ( v20 )
{
v19->c_iflag = v21->c_iflag;
v21 = (struct termios *)((char *)v21 + 4);
v19 = (struct termios *)((char *)v19 + 4);
--v20;
}
tmpterm.c_iflag &= 0xFFFFEBFF;
tcsetattr(0, 2, &tmpterm);
v23 = 1;
}
}
if ( (readfds.fds_bits[v12] & v22) != 0 )
{
v25 = v14;
v16 = read(v4, v3, 0x400uLL);
if ( v16 <= 0 && *v25 != 4 )
goto LABEL_16;
if ( v23 )
LABEL_22:
v23 = 0;
else
write(1, v3, v16);
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_BYTE *v3; // r13
int v4; // er12
char *v5; // rax
int v6; // ebp
int v7; // ebx
int v8; // edi
struct termios *v9; // rdi
__int64 v10; // rcx
struct termios *v11; // rsi
__int64 v12; // rbx
int v13; // er14
int *v14; // rax
int v15; // eax
int v16; // eax
int v17; // eax
int *v18; // rcx
struct termios *v19; // rdi
__int64 v20; // rcx
struct termios *v21; // rsi
__int64 v22; // [rsp+0h] [rbp-D8h]
int v23; // [rsp+8h] [rbp-D0h]
int *v24; // [rsp+8h] [rbp-D0h]
int *v25; // [rsp+10h] [rbp-C8h]
int *v26; // [rsp+10h] [rbp-C8h]
int v27; // [rsp+10h] [rbp-C8h]
int *v28; // [rsp+18h] [rbp-C0h]
fd_set readfds; // [rsp+20h] [rbp-B8h] BYREF

puts("Hexin Terminal v0.2 - z0nKT1g3r & whats");
v3 = malloc(0x400uLL);
v4 = open("/dev/ptmx", 2);
grantpt(v4);
unlockpt(v4);
v5 = ptsname(v4);
v6 = open(v5, 2);
if ( !fork() )
{
setsid();
v7 = 1;
ioctl(v6, 0x540EuLL, 0LL);
dup2(v6, 0);
dup2(v6, 1);
dup2(v6, 2);
close(v6);
do
{
v8 = v7++;
signal(v8, 0LL);
}
while ( v7 != 64 );
putenv("PS1=\\[\\033[1;30m\\][\\[\\033[0;32m\\]\\u\\[\\033[1;32m\\]@\\[\\033[0;32m\\]\\h \\[\\033[1;37m\\]\\W\\[\\033"
"[1;30m\\]]\\[\\033[0m\\]# ");
execl("/bin/bash", "bash", "--norc", "-i", 0LL);
puts("despres");
exit(1);
}
tcgetattr(0, &oldterm);
sleep(1u);
v9 = &newterm;
v10 = 15LL;
v11 = &oldterm;
while ( v10 )
{
v9->c_iflag = v11->c_iflag;
v11 = (struct termios *)((char *)v11 + 4);
v9 = (struct termios *)((char *)v9 + 4);
--v10;
}
newterm.c_lflag &= 0xFFFFFFF4;
newterm.c_iflag &= 0xFFFFEBFF;
tcsetattr(0, 2, &newterm);
signal(17, sig_die);
signal(2, sig_die);
signal(3, sig_die);
signal(4, sig_die);
signal(6, sig_die);
signal(11, sig_die);
signal(15, sig_die);
v23 = 0;
v12 = v4 / 64;
v22 = 1LL << (v4 % 64);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
readfds.fds_bits[v12] |= v22;
v13 = select(v4 + 1, &readfds, 0LL, 0LL, 0LL);
v14 = __errno_location();
if ( v13 >= 0 )
break;
if ( *v14 != 4 )
goto LABEL_16;
}
if ( (readfds.fds_bits[0] & 1) == 0 )
break;
if ( v23 )
{
v26 = v14;
*v3 = 92;
v17 = read(0, v3 + 1, 0x400uLL);
v18 = v26;
if ( v17 <= 0 && *v26 != 4
|| (v27 = v17, v28 = v18, func8(v3, (unsigned int)v17), write(v4, v3, v27) < 0) && *v28 != 4 )
{
LABEL_16:
tcsetattr(0, 2, &oldterm);
exit(0);
}
tcsetattr(0, 2, &newterm);
memset(v3, 0, 0x400uLL);
}
else
{
v24 = v14;
v15 = read(0, v3, 1uLL);
if ( v15 <= 0 && *v24 != 4 )
goto LABEL_16;
if ( *v3 != 92 )
{
if ( write(v4, v3, v15) < 0 && *v24 != 4 )
goto LABEL_16;
goto LABEL_22;
}
putchar(92);
fflush(stdout);
v19 = &tmpterm;
v20 = 15LL;
v21 = &oldterm;
while ( v20 )
{
v19->c_iflag = v21->c_iflag;
v21 = (struct termios *)((char *)v21 + 4);
v19 = (struct termios *)((char *)v19 + 4);
--v20;
}
tmpterm.c_iflag &= 0xFFFFEBFF;
tcsetattr(0, 2, &tmpterm);
v23 = 1;
}
}
if ( (readfds.fds_bits[v12] & v22) != 0 )
{
v25 = v14;
v16 = read(v4, v3, 0x400uLL);
if ( v16 <= 0 && *v25 != 4 )
goto LABEL_16;
if ( v23 )
LABEL_22:
v23 = 0;
else
write(1, v3, v16);
}
}
}

original_code:
int main(int argc, char **argv){
 printf("Hexin Terminal v0.2 - z0nKT1g3r & whats\n");
 char *buf = malloc(1024);
 int tty, pty;
 extern char *ptsname();
 pty = open("/dev/ptmx", 02);
 grantpt(pty);
 unlockpt(pty);
 tty = open(ptsname(pty), 02);
 if(!fork()) {
  setsid();
  ioctl(tty, 0x540E, ((void *)0));
  dup2(tty, 0);
  dup2(tty, 1);
  dup2(tty, 2);
  close(tty);
  int i;
  for (i = 1; i < 64; i++)
   signal(i, ((__sighandler_t) 0));
  putenv("PS1=\\[\\033[1;30m\\][\\[\\033[0;32m\\]\\u\\[\\033[1;32m\\]@\\[\\033[0;32m\\]\\h \\[\\033[1;37m\\]\\W\\[\\033[1;30m\\]]\\[\\033[0m\\]# ");
  execl("/bin/bash", "bash", "--norc", "-i", ((void *)0));
  printf("despres\n");
  exit(1);
 }
 else{
  tcgetattr(0, &oldterm);
  sleep(1);
  newterm = oldterm;
  newterm.c_lflag &= ~(0000002 | 0000010 | 0000001);
  newterm.c_iflag &= ~(0002000 | 0010000);
  tcsetattr(0, 2, &newterm);
  signal(17, sig_die);
  signal(2, sig_die);
  signal(3, sig_die);
  signal(4, sig_die);
  signal(6, sig_die);
  signal(11, sig_die);
  signal(15, sig_die);
  int state = 0;
  char hex[2];
  while (1) {
    fd_set fds;
    int count;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fds)->__fds_bits)[((pty) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pty) % (8 * (int) sizeof (__fd_mask)))))));
    if (select(pty + 1, &fds, ((void *)0), ((void *)0), ((void *)0)) < 0) {
      if ((*__errno_location ()) == 4)
        continue;
      break;
    }
    if (((((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
     if (state == 0) {
      count = read(0, buf, 1);
      if ((count <= 0) && ((*__errno_location ()) != 4))
        break;
      if (buf[0] == '\\') {
       printf("\\");fflush(stdout);
       state = 1;
          tmpterm = oldterm;
             tmpterm.c_lflag &= ~0000001 | ~0000010 | 0000002;
             tmpterm.c_iflag &= ~(0002000 | 0010000);
       tcsetattr(0, 2, &tmpterm);
      } else {
       if ((write(pty, buf, count) < 0) && ((*__errno_location ()) != 4))
        break;
      }
     } else {
      buf[0] = '\\';
      count = read(0, buf+1, 1024);
      if ((count <= 0) && ((*__errno_location ()) != 4))
        break;
      filter(buf, count);
      if ((write(pty, buf, count) < 0) && ((*__errno_location ()) != 4))
        break;
      tcsetattr(0, 2, &newterm);
      memset(buf, 0, 1024);
     }
    } else if (((((&fds)->__fds_bits)[((pty) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pty) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      count = read(pty, buf, 1024);
      if ((count <= 0) && ((*__errno_location ()) != 4))
        break;
      if (state == 0) write(1, buf, count);
      else state = 0;
    }
  }
  tcsetattr(0, 2, &oldterm);
 }
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv){
 printf("Hexin Terminal v0.2 - z0nKT1g3r & whats\n");
 char *buf = malloc(1024);
 int tty; int pty;
 extern char *ptsname();
 pty = open("/dev/ptmx", 02);
 grantpt(pty);
 unlockpt(pty);
 tty = open(ptsname(pty), 02);
 if(!fork()) {
  setsid();
  ioctl(tty, 0x540E, ((void *)0));
  dup2(tty, 0);
  dup2(tty, 1);
  dup2(tty, 2);
  close(tty);
  int i;
  for (i = 1; i < 64; i++)
   signal(i, ((void (*)(int)) 0));
  putenv("PS1=\\[\\033[1;30m\\][\\[\\033[0;32m\\]\\u\\[\\033[1;32m\\]@\\[\\033[0;32m\\]\\h \\[\\033[1;37m\\]\\W\\[\\033[1;30m\\]]\\[\\033[0m\\]# ");
  execl("/bin/bash", "bash", "--norc", "-i", ((void *)0));
  printf("despres\n");
  exit(1);
 }
 else{
  tcgetattr(0, &oldterm);
  sleep(1);
  newterm = oldterm;
  newterm.c_lflag &= ~(0000002 | 0000010 | 0000001);
  newterm.c_iflag &= ~(0002000 | 0010000);
  tcsetattr(0, 2, &newterm);
  signal(17, sig_die);
  signal(2, sig_die);
  signal(3, sig_die);
  signal(4, sig_die);
  signal(6, sig_die);
  signal(11, sig_die);
  signal(15, sig_die);
  int state = 0;
  char hex[2];
  while (1) {
    struct fd_set fds;
    int count;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
    ((void) (((&fds)->__fds_bits)[((pty) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pty) % (8 * (int) sizeof (__fd_mask)))))));
    if (select(pty + 1, &fds, ((void *)0), ((void *)0), ((void *)0)) < 0) {
      if ((*__errno_location ()) == 4)
        continue;
      break;
    }
    if (((((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
     if (state == 0) {
      count = read(0, buf, 1);
      if ((count <= 0) && ((*__errno_location ()) != 4))
        break;
      if (buf[0] == '\\') {
       printf("\\");fflush(stdout);
       state = 1;
          tmpterm = oldterm;
             tmpterm.c_lflag &= ~0000001 | ~0000010 | 0000002;
             tmpterm.c_iflag &= ~(0002000 | 0010000);
       tcsetattr(0, 2, &tmpterm);
      } else {
       if ((write(pty, buf, count) < 0) && ((*__errno_location ()) != 4))
        break;
      }
     } else {
      buf[0] = '\\';
      count = read(0, buf+1, 1024);
      if ((count <= 0) && ((*__errno_location ()) != 4))
        break;
      filter(buf, count);
      if ((write(pty, buf, count) < 0) && ((*__errno_location ()) != 4))
        break;
      tcsetattr(0, 2, &newterm);
      memset(buf, 0, 1024);
     }
    } else if (((((&fds)->__fds_bits)[((pty) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pty) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      count = read(pty, buf, 1024);
      if ((count <= 0) && ((*__errno_location ()) != 4))
        break;
      if (state == 0) write(1, buf, count);
      else state = 0;
    }
  }
  tcsetattr(0, 2, &oldterm);
 }
 exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
buf
char *

variable_types: tty int

variable_types: pty int

variable_types:
ptsname
char *()

variable_types: state int

variable_types:
hex
char [2]

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

variable_types: count int


decompiled_code:
void __noreturn sig_die()
{
tcsetattr(0, 2, &oldterm);
exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
tcsetattr(0, 2, &oldterm);
exit(1);
}

original_code:
void sig_die(int n) {
 tcsetattr(0, 2, &oldterm);
 exit(1);
}

variable_types: n int


filter

decompiled_code:
__int64 __fastcall filter(_QWORD *a1, int a2)
{
_BYTE *v3; // rax
_QWORD *v4; // rbp
unsigned int v5; // er12
int i; // ebx
char v7; // al
_BYTE *v8; // r13
char v9; // si
char nptr[2]; // [rsp+6h] [rbp-42h] BYREF
char *endptr; // [rsp+8h] [rbp-40h] BYREF

v3 = malloc(0x400uLL);
v4 = v3;
if ( a2 <= 0 )
{
v5 = 0;
}
else
{
v5 = 0;
for ( i = 0; i < a2; ++i )
{
v7 = *((_BYTE *)a1 + i);
v8 = (char *)v4 + (int)v5;
if ( v7 == 92 )
{
v9 = *((_BYTE *)a1 + i + 1);
switch ( v9 )
{
case '\\':
*v8 = 92;
++i;
break;
case 'n':
*v8 = 10;
++i;
break;
case 'r':
*v8 = 13;
++i;
break;
case 't':
*v8 = 9;
++i;
break;
case 'x':
nptr[0] = *((_BYTE *)a1 + i + 2);
nptr[1] = *((_BYTE *)a1 + i + 3);
*v8 = strtol(nptr, &endptr, 16);
i += 3;
break;
default:
++v5;
*v8 = 92;
++i;
*((_BYTE *)v4 + (int)v5) = v9;
break;
}
}
else
{
*v8 = v7;
}
++v5;
}
v3 = (char *)v4 + (int)v5;
}
*v3 = 10;
*a1 = *v4;
a1[127] = v4[127];
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v4 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 1024) >> 3));
free(v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, int a2)
{
_BYTE *v3; // rax
_QWORD *v4; // rbp
unsigned int v5; // er12
int i; // ebx
char v7; // al
_BYTE *v8; // r13
char v9; // si
char nptr[2]; // [rsp+6h] [rbp-42h] BYREF
char *endptr; // [rsp+8h] [rbp-40h] BYREF

v3 = malloc(0x400uLL);
v4 = v3;
if ( a2 <= 0 )
{
v5 = 0;
}
else
{
v5 = 0;
for ( i = 0; i < a2; ++i )
{
v7 = *((_BYTE *)a1 + i);
v8 = (char *)v4 + (int)v5;
if ( v7 == 92 )
{
v9 = *((_BYTE *)a1 + i + 1);
switch ( v9 )
{
case '\\':
*v8 = 92;
++i;
break;
case 'n':
*v8 = 10;
++i;
break;
case 'r':
*v8 = 13;
++i;
break;
case 't':
*v8 = 9;
++i;
break;
case 'x':
nptr[0] = *((_BYTE *)a1 + i + 2);
nptr[1] = *((_BYTE *)a1 + i + 3);
*v8 = strtol(nptr, &endptr, 16);
i += 3;
break;
default:
++v5;
*v8 = 92;
++i;
*((_BYTE *)v4 + (int)v5) = v9;
break;
}
}
else
{
*v8 = v7;
}
++v5;
}
v3 = (char *)v4 + (int)v5;
}
*v3 = 10;
*a1 = *v4;
a1[127] = v4[127];
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v4 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 1024) >> 3));
free(v4);
return v5;
}

original_code:
int filter(char *buf, int size){
 int i;
 int offset;
 char *buf2;
 char *ptr;
 char ch;
 char hex[2];
 buf2=malloc(1024);
 for(i=0,offset=0;i<size;i++,offset++){
  if(buf[i]=='\\'){
   switch(buf[i+1]){
    case '\\':
     buf2[offset]='\\';
     break;
    case 'n':
     buf2[offset]='\n';
     break;
    case 'r':
     buf2[offset]='\r';
     break;
    case 't':
     buf2[offset]='\t';
     break;
    case 'x':
     hex[0]=buf[i+2];
     hex[1]=buf[i+3];
     buf2[offset]=strtol(hex,&ptr,16);
     i+=2;
     break;
    default:
     buf2[offset]='\\';
     buf2[++offset]=buf[i+1];
     break;
   }
   i++;
  }
  else{
   buf2[offset]=buf[i];
  }
 }
 buf2[offset] = '\n';
 memset(buf,0,1024);
 memcpy(buf, buf2, 1024);
 free(buf2);
 return offset;
}

variable_types: size int

variable_types: offset int

variable_types:
ptr
char *

variable_types: ch char


repo: white-pony/planet-lander

decompiled_code:
size_t __fastcall draw_vector_text(char *s, int a2, int a3, float a4)
{
unsigned __int64 v5; // rbp
size_t result; // rax
__int64 v7; // r15
__int64 v8; // rbx
__int64 v9; // r15
float v10; // xmm1_4
float v11; // [rsp+8h] [rbp-40h]
float v12; // [rsp+Ch] [rbp-3Ch]

v5 = 0LL;
result = strlen(s);
if ( result )
{
do
{
result = (unsigned __int8)s[v5];
v12 = (float)a3;
v11 = (float)((float)(1.4 * a4) * (float)(int)v5) + (float)a2;
if ( (char)result <= 31 )
break;
v7 = (char)(result - 32);
v8 = chars_coords[v7];
glBegin(1LL);
if ( char_lines[v7] > 0 )
{
v9 = v8 + 16LL * (unsigned int)(char_lines[v7] - 1) + 16;
do
{
v10 = 1.5 * *(float *)(v8 + 4);
v8 += 16LL;
glVertex2f((float)(*(float *)(v8 - 16) * a4) + v11, (float)(v10 * a4) + v12);
glVertex2f(v11 + (float)(a4 * *(float *)(v8 - 8)), (float)((float)(1.5 * *(float *)(v8 - 4)) * a4) + v12);
}
while ( v9 != v8 );
}
glEnd();
++v5;
result = strlen(s);
}
while ( result > v5 );
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(char *s, int a2, int a3, float a4)
{
unsigned __int64 v5; // rbp
size_t result; // rax
__int64 v7; // r15
__int64 v8; // rbx
__int64 v9; // r15
float v10; // xmm1_4
float v11; // [rsp+8h] [rbp-40h]
float v12; // [rsp+Ch] [rbp-3Ch]

v5 = 0LL;
result = strlen(s);
if ( result )
{
do
{
result = (unsigned __int8)s[v5];
v12 = (float)a3;
v11 = (float)((float)(1.4 * a4) * (float)(int)v5) + (float)a2;
if ( (char)result <= 31 )
break;
v7 = (char)(result - 32);
v8 = chars_coords[v7];
glBegin(1LL);
if ( char_lines[v7] > 0 )
{
v9 = v8 + 16LL * (unsigned int)(char_lines[v7] - 1) + 16;
do
{
v10 = 1.5 * *(float *)(v8 + 4);
v8 += 16LL;
glVertex2f((float)(*(float *)(v8 - 16) * a4) + v11, (float)(v10 * a4) + v12);
glVertex2f(v11 + (float)(a4 * *(float *)(v8 - 8)), (float)((float)(1.5 * *(float *)(v8 - 4)) * a4) + v12);
}
while ( v9 != v8 );
}
glEnd();
++v5;
result = strlen(s);
}
while ( result > v5 );
}
return result;
}

original_code:
void draw_vector_text(const char* text, int x, int y, float font_scale)
{
 for (int i=0; i<strlen(text); i++)
 {
  float char_x = x + SPASING * font_scale * i;
  float char_y = y;
  char current_char = text[i];
  if (current_char < 32)
   return;
  const float* current_char_coords = chars_coords[current_char-32];
  glBegin(0x0001);
  for (int l = 0; l < char_lines[current_char-32]; l++) {
   glVertex2f(char_x + current_char_coords[4*l] * font_scale, char_y + current_char_coords[4*l+1] * HEIGHT_WIDTH_RATIO * font_scale);
   glVertex2f(char_x + current_char_coords[4*l+2] * font_scale, char_y + current_char_coords[4*l+3] * HEIGHT_WIDTH_RATIO * font_scale);
  }
  glEnd();
 }
}

canonical_original_code:
void draw_vector_text(char *text, int x, int y, float font_scale)
{
 for (int i = 0; i<strlen(text); i++)
 {
  float char_x = x + SPASING * font_scale * i;
  float char_y = y;
  char current_char = text[i];
  if (current_char < 32)
   return;
  float *current_char_coords = chars_coords[current_char-32];
  glBegin(0x0001);
  for (int l = 0; l < char_lines[current_char-32]; l++) {
   glVertex2f(char_x + current_char_coords[4*l] * font_scale, char_y + current_char_coords[4*l+1] * HEIGHT_WIDTH_RATIO * font_scale);
   glVertex2f(char_x + current_char_coords[4*l+2] * font_scale, char_y + current_char_coords[4*l+3] * HEIGHT_WIDTH_RATIO * font_scale);
  }
  glEnd();
 }
}

variable_types:
text
char *

variable_types: x int

variable_types: y int

variable_types: i int

variable_types: l int


Glpk

decompiled_code:
__int64 Glpk()
{
int v0; // ebx
unsigned int v1; // er12

if ( NZERO <= 0 )
return 0LL;
v0 = 0;
v1 = 0;
do
{
v0 -= ((unsigned int)Lp_main() == 0) - 1;
printf("SNR is %f, failure number is %d, total number is %d\n", *(float *)&SNR, (unsigned int)v0, ++v1);
}
while ( NZERO > v0 );
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // ebx
unsigned int v1; // er12

if ( NZERO <= 0 )
return 0LL;
v0 = 0;
v1 = 0;
do
{
v0 -= ((unsigned int)Lp_main() == 0) - 1;
printf("SNR is %f, failure number is %d, total number is %d\n", *(float *)&SNR, (unsigned int)v0, ++v1);
}
while ( NZERO > v0 );
return v1;
}

original_code:
int Glpk()
{
 int num_total = 0;
 int num_nzero = 0;
 while(num_nzero < NZERO)
 {
  if(Lp_main())
   num_nzero ++;
  num_total ++;
  printf("SNR is %f, failure number is %d, total number is %d\n", SNR, num_nzero, num_total);
 }
 return num_total;
}


Gunplot

decompiled_code:
void Gunplot()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void Gunplot()
{
}

canonical_original_code:
void Gunplot()
{
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rax
__int64 v4; // rdi
int v5; // eax
int v7; // eax
int v8; // edx
__int64 v9; // rax

memory[0] = 0;
loadAllTheThings(argc, argv, envp);
updatePC((unsigned int)regIR);
while ( dword_4040D0 != 1 )
{
v3 = fopen("tracefile.din", "a");
++countInstr;
ofp = v3;
v4 = (unsigned int)memory[128 * (__int64)dword_404144 + dword_404148];
v5 = memory[128 * (__int64)dword_404144 + dword_404148] >> 9;
if ( (int)v4 <= 3071 )
{
handleIR();
getEffAddr();
switch ( dword_404124 )
{
case 0:
++countAND;
countClock += 2;
HIDWORD(regAC) &= memory[128 * (__int64)dword_40410C + dword_404110];
goto LABEL_20;
case 1:
++countTAD;
countClock += 2;
HIDWORD(regAC) -= memory[128 * (__int64)dword_40410C + dword_404110];
LABEL_20:
updatePC((unsigned int)(regPC + 1));
trace = 0;
dword_404094 = regCPMA;
goto LABEL_9;
case 2:
countClock += 2;
++countISZ;
v9 = dword_404110 + ((__int64)dword_40410C << 7);
if ( memory[v9]++ != -1 )
goto LABEL_14;
v7 = regPC;
LABEL_13:
regPC = v7 + 1;
updatePC((unsigned int)(v7 + 1));
LABEL_14:
trace = 1;
dword_404094 = regCPMA;
if ( dword_4040D0 != 1 )
goto LABEL_15;
goto LABEL_10;
case 3:
countClock += 2;
++countDCA;
v8 = HIDWORD(regAC);
regAC = 0LL;
memory[128 * (__int64)dword_40410C + dword_404110] = v8;
updatePC((unsigned int)(regPC + 1));
trace = 1;
dword_404094 = regCPMA;
goto LABEL_9;
case 4:
countClock += 2;
++countJMS;
updateCPMA((unsigned int)regPC);
v7 = regCPMA;
goto LABEL_13;
case 5:
++countJMP;
++countClock;
regPC = regCPMA;
updatePC((unsigned int)regCPMA);
trace = 2;
dword_404094 = regCPMA;
goto LABEL_9;
default:
goto LABEL_9;
}
}
if ( v5 == 6 )
{
++countIO;
puts("Warning: an I/O instruction was not simulated.");
}
else if ( v5 == 7 )
{
++count7;
++countClock;
if ( (int)v4 >> 8 == 14 )
{
group1If(v4, 7LL, 0LL);
}
else if ( (v4 & 1) != 0 )
{
puts("Warning: Group 3 instructions not supported");
}
else
{
group2If(v4, 7LL, 1LL, 0LL);
}
}
else
{
printf("Warning: Opcode %d is greater than 7\n", (unsigned int)dword_404124);
}
LABEL_9:
if ( dword_4040D0 == 1 )
break;
LABEL_15:
fprintf(ofp, "%d %o\n", (unsigned int)trace, (unsigned int)dword_404094);
fflush(ofp);
}
LABEL_10:
fclose(ofp);
puts("\n_______________ OUTPUT ____________________");
printf("\nTotal clock cycles: %d\n", (unsigned int)countClock);
printf("Total instructions: %d\n", (unsigned int)countInstr);
printf(
"Counts: AND, TAD, ISZ, DCA, JMS, JMP, IO, Micro\n\n %-4d %-4d %-4d %-4d %-4d %-4d %-3d %-6d\n\n",
(unsigned int)countAND,
(unsigned int)countTAD,
(unsigned int)countISZ,
(unsigned int)countDCA,
(unsigned int)countJMS,
(unsigned int)countJMP,
(unsigned int)countIO,
(unsigned int)count7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rax
__int64 v4; // rdi
int v5; // eax
int v7; // eax
int v8; // edx
__int64 v9; // rax

memory[0] = 0;
func13(argc, argv, envp);
func7((unsigned int)regIR);
while ( dword_4040D0 != 1 )
{
v3 = fopen("tracefile.din", "a");
++countInstr;
ofp = v3;
v4 = (unsigned int)memory[128 * (__int64)dword_404144 + dword_404148];
v5 = memory[128 * (__int64)dword_404144 + dword_404148] >> 9;
if ( (int)v4 <= 3071 )
{
func11();
func14();
switch ( dword_404124 )
{
case 0:
++countAND;
countClock += 2;
HIDWORD(regAC) &= memory[128 * (__int64)dword_40410C + dword_404110];
goto LABEL_20;
case 1:
++countTAD;
countClock += 2;
HIDWORD(regAC) -= memory[128 * (__int64)dword_40410C + dword_404110];
LABEL_20:
func7((unsigned int)(regPC + 1));
trace = 0;
dword_404094 = regCPMA;
goto LABEL_9;
case 2:
countClock += 2;
++countISZ;
v9 = dword_404110 + ((__int64)dword_40410C << 7);
if ( memory[v9]++ != -1 )
goto LABEL_14;
v7 = regPC;
LABEL_13:
regPC = v7 + 1;
func7((unsigned int)(v7 + 1));
LABEL_14:
trace = 1;
dword_404094 = regCPMA;
if ( dword_4040D0 != 1 )
goto LABEL_15;
goto LABEL_10;
case 3:
countClock += 2;
++countDCA;
v8 = HIDWORD(regAC);
regAC = 0LL;
memory[128 * (__int64)dword_40410C + dword_404110] = v8;
func7((unsigned int)(regPC + 1));
trace = 1;
dword_404094 = regCPMA;
goto LABEL_9;
case 4:
countClock += 2;
++countJMS;
func10((unsigned int)regPC);
v7 = regCPMA;
goto LABEL_13;
case 5:
++countJMP;
++countClock;
regPC = regCPMA;
func7((unsigned int)regCPMA);
trace = 2;
dword_404094 = regCPMA;
goto LABEL_9;
default:
goto LABEL_9;
}
}
if ( v5 == 6 )
{
++countIO;
puts("Warning: an I/O instruction was not simulated.");
}
else if ( v5 == 7 )
{
++count7;
++countClock;
if ( (int)v4 >> 8 == 14 )
{
func8(v4, 7LL, 0LL);
}
else if ( (v4 & 1) != 0 )
{
puts("Warning: Group 3 instructions not supported");
}
else
{
func9(v4, 7LL, 1LL, 0LL);
}
}
else
{
printf("Warning: Opcode %d is greater than 7\n", (unsigned int)dword_404124);
}
LABEL_9:
if ( dword_4040D0 == 1 )
break;
LABEL_15:
fprintf(ofp, "%d %o\n", (unsigned int)trace, (unsigned int)dword_404094);
fflush(ofp);
}
LABEL_10:
fclose(ofp);
puts("\n_______________ OUTPUT ____________________");
printf("\nTotal clock cycles: %d\n", (unsigned int)countClock);
printf("Total instructions: %d\n", (unsigned int)countInstr);
printf(
"Counts: AND, TAD, ISZ, DCA, JMS, JMP, IO, Micro\n\n %-4d %-4d %-4d %-4d %-4d %-4d %-3d %-6d\n\n",
(unsigned int)countAND,
(unsigned int)countTAD,
(unsigned int)countISZ,
(unsigned int)countDCA,
(unsigned int)countJMS,
(unsigned int)countJMP,
(unsigned int)countIO,
(unsigned int)count7);
return 0;
}

original_code:
int main()
{
  memset(memory, 0, sizeof(int));
  loadAllTheThings();
  int i;
  updatePC(regIR.addr);
  int carryout;
  int c;
  int opcode;
  while (group2.hlt != 1)
  {
      ofp = fopen("tracefile.din", "a");
      countInstr++;
      i = memory[regPC.page][regPC.offset];
      opcode = i >> 9;
      if (opcode < 6)
      {
        handleIR(i);
        getEffAddr();
        switch (regIR.opcode)
        {
          case 0:
            countAND++;
            countClock = countClock + 2;
            regAC.addr = regAC.addr & memory[regCPMA.page][regCPMA.offset];
            updatePC(regPC.addr + 1);
            trace.n = 0;
            trace.addr = regCPMA.addr;
            break;
          case 1:
            countTAD++;
            countClock = countClock + 2;
            regAC.addr = regAC.addr + -memory[regCPMA.page][regCPMA.offset];
            if (carryout)
               regAC.link = -regAC.link;
            updatePC(regPC.addr + 1);
            trace.n = 0;
            trace.addr = regCPMA.addr;
            break;
          case 2:
            countClock = countClock + 2;
            countISZ++;
            memory[regCPMA.page][regCPMA.offset] = memory[regCPMA.page][regCPMA.offset] + 1;
            if (memory[regCPMA.page][regCPMA.offset] == 0)
            {
               regPC.addr = regPC.addr + 1;
               updatePC(regPC.addr);
            }
            regPC.addr = regPC.addr;
            trace.n = 1;
            trace.addr = regCPMA.addr;
            break;
          case 3:
            countClock = countClock + 2;
            countDCA++;
            memory[regCPMA.page][regCPMA.offset] = regAC.addr;
            regAC.addr = 0;
            regAC.link = 0;
            updatePC(regPC.addr + 1);
            trace.n = 1;
            trace.addr = regCPMA.addr;
            break;
          case 4:
            countClock = countClock + 2;
            countJMS++;
            updateCPMA(regPC.addr);
            regPC.addr = regCPMA.addr + 1;
            updatePC(regPC.addr);
            trace.n = 1;
            trace.addr = regCPMA.addr;
            break;
          case 5:
            countJMP++;
            countClock++;
            regPC.addr = regCPMA.addr;
            updatePC(regPC.addr);
            trace.n = 2;
            trace.addr = regCPMA.addr;
            break;
        }
      }
      else if (opcode == 6)
      {
         countIO++;
         printf("Warning: an I/O instruction was not simulated.\n");
      }
      else if (opcode == 7)
      {
         count7++;
         countClock++;
         int bit11;
         int groupbit;
         groupbit = (i >> 8) & 1;
         bit11 = i & 1;
         if (groupbit == 0)
            group1If(i, opcode, groupbit);
         else if (groupbit == 1 && bit11 == 0)
            group2If(i, opcode, groupbit, bit11);
         else
           printf("Warning: Group 3 instructions not supported\n");
      }
      else if (opcode > 7)
         printf("Warning: Opcode %d is greater than 7\n",regIR.opcode);
      if (group2.hlt != 1)
      {
         fprintf(ofp,"%d %o\n",trace.n,trace.addr);
         fflush(ofp);
      }
  }
  fclose(ofp);
  printf("\n_______________ OUTPUT ____________________\n");
  printf("\nTotal clock cycles: %d\n",countClock);
  printf("Total instructions: %d\n",countInstr);
  printf("Counts: AND, TAD, ISZ, DCA, JMS, JMP, IO, Micro\n\n"
         "        %-4d %-4d %-4d %-4d %-4d %-4d %-3d %-6d\n\n",
         countAND,countTAD,countISZ,countDCA,countJMS,countJMP,countIO,count7);
  return 0;
}

variable_types: carryout int

variable_types: c int

variable_types: opcode int

variable_types: groupbit int


updatePC

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

regPC = a1;
result = (unsigned int)(a1 >> 7);
dword_404148 = a1 & 0x7F;
dword_404144 = a1 >> 7;
return result;
}

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

regPC = a1;
result = (unsigned int)(a1 >> 7);
dword_404148 = a1 & 0x7F;
dword_404144 = a1 >> 7;
return result;
}

original_code:
int updatePC(int i)
{
  regPC.addr = i;
  regPC.page = i >> 7;
  regPC.offset = i & 0x7f;
}


decompiled_code:
__int64 __fastcall group1If(int a1, int a2, int a3)
{
int v3; // eax
int v4; // er9
int v6; // er8
int v7; // ecx
int v8; // edx
int v9; // esi
int v10; // edi
int v11; // ecx
int v12; // edx
int v13; // eax
int v14; // er9
int v15; // er10
int v17; // eax

v3 = a1;
v4 = a1;
group1 = a2;
dword_4040E4 = a3;
v6 = (a1 >> 7) & 1;
v7 = a1 >> 5;
v8 = a1 >> 4;
v9 = (a1 >> 6) & 1;
v10 = a1 & 1;
v11 = v7 & 1;
v12 = v8 & 1;
v13 = (v3 >> 3) & 1;
v14 = (v4 >> 2) & 1;
dword_4040EC = v9;
v15 = (a1 >> 1) & 1;
dword_4040E8 = v6;
dword_4040F0 = v11;
dword_4040F4 = v12;
dword_4040F8 = v13;
dword_4040FC = v14;
dword_404100 = v15;
dword_404104 = v10;
if ( v6 )
HIDWORD(regAC) = 0;
if ( v9 )
LODWORD(regAC) = 0;
if ( v11 )
HIDWORD(regAC) = ~HIDWORD(regAC);
if ( v12 )
LODWORD(regAC) = ~(_DWORD)regAC;
if ( v10 )
++HIDWORD(regAC);
if ( v13 )
{
if ( v15 )
{
v17 = ((HIDWORD(regAC) << 10) | (SHIDWORD(regAC) >> 2) & 0xFFF) != 0;
HIDWORD(regAC) = v17;
if ( !v14 )
goto LABEL_13;
LABEL_16:
HIDWORD(regAC) = ((v17 >> 10) | (4 * (_WORD)v17) & 0xFFF) != 0;
goto LABEL_14;
}
v17 = ((HIDWORD(regAC) << 11) | (SHIDWORD(regAC) >> 1) & 0xFFF) != 0;
HIDWORD(regAC) = v17;
if ( !v14 )
goto LABEL_13;
LABEL_19:
HIDWORD(regAC) = ((v17 >> 11) | (2 * (_WORD)v17) & 0xFFF) != 0;
goto LABEL_13;
}
if ( v14 )
{
v17 = HIDWORD(regAC);
if ( v15 )
goto LABEL_16;
goto LABEL_19;
}
LABEL_13:
--countClock;
LABEL_14:
updatePC(regPC + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, int a3)
{
int v3; // eax
int v4; // er9
int v6; // er8
int v7; // ecx
int v8; // edx
int v9; // esi
int v10; // edi
int v11; // ecx
int v12; // edx
int v13; // eax
int v14; // er9
int v15; // er10
int v17; // eax

v3 = a1;
v4 = a1;
group1 = a2;
dword_4040E4 = a3;
v6 = (a1 >> 7) & 1;
v7 = a1 >> 5;
v8 = a1 >> 4;
v9 = (a1 >> 6) & 1;
v10 = a1 & 1;
v11 = v7 & 1;
v12 = v8 & 1;
v13 = (v3 >> 3) & 1;
v14 = (v4 >> 2) & 1;
dword_4040EC = v9;
v15 = (a1 >> 1) & 1;
dword_4040E8 = v6;
dword_4040F0 = v11;
dword_4040F4 = v12;
dword_4040F8 = v13;
dword_4040FC = v14;
dword_404100 = v15;
dword_404104 = v10;
if ( v6 )
HIDWORD(regAC) = 0;
if ( v9 )
LODWORD(regAC) = 0;
if ( v11 )
HIDWORD(regAC) = ~HIDWORD(regAC);
if ( v12 )
LODWORD(regAC) = ~(_DWORD)regAC;
if ( v10 )
++HIDWORD(regAC);
if ( v13 )
{
if ( v15 )
{
v17 = ((HIDWORD(regAC) << 10) | (SHIDWORD(regAC) >> 2) & 0xFFF) != 0;
HIDWORD(regAC) = v17;
if ( !v14 )
goto LABEL_13;
LABEL_16:
HIDWORD(regAC) = ((v17 >> 10) | (4 * (_WORD)v17) & 0xFFF) != 0;
goto LABEL_14;
}
v17 = ((HIDWORD(regAC) << 11) | (SHIDWORD(regAC) >> 1) & 0xFFF) != 0;
HIDWORD(regAC) = v17;
if ( !v14 )
goto LABEL_13;
LABEL_19:
HIDWORD(regAC) = ((v17 >> 11) | (2 * (_WORD)v17) & 0xFFF) != 0;
goto LABEL_13;
}
if ( v14 )
{
v17 = HIDWORD(regAC);
if ( v15 )
goto LABEL_16;
goto LABEL_19;
}
LABEL_13:
--countClock;
LABEL_14:
func7(regPC + 1);
return 0LL;
}

original_code:
int group1If(int i, int opcode, int groupbit)
{
   group1.opcode = opcode;
   group1.bit3 = groupbit;
   group1.cla = (i >> 7) & 1;
   group1.cll = (i >> 6) & 1;
   group1.cma = (i >> 5) & 1;
   group1.cml = (i >> 4) & 1;
   group1.rar = (i >> 3) & 1;
   group1.ral = (i >> 2) & 1;
   group1.rotate = (i >> 1) & 1;
   group1.iac = i & 1;
   if (group1.cla == 1)
      regAC.addr = 0;
   if (group1.cll == 1)
      regAC.link = 0;
   if (group1.cma == 1)
      regAC.addr = ~regAC.addr;
   if (group1.cml == 1)
      regAC.link = ~regAC.link;
   if (group1.iac == 1)
      regAC.addr = regAC.addr + 1;
   if (group1.rar == 1 && group1.rotate == 0)
      regAC.addr = (0xfff & (regAC.addr >> 1)) || (regAC.addr << 11);
   if (group1.rar == 1 && group1.rotate == 1)
      regAC.addr = (0xfff & (regAC.addr >> 2)) || (regAC.addr << 10);
   if (group1.ral == 1 && group1.rotate == 0)
      regAC.addr = (0xfff & (regAC.addr << 1)) || (regAC.addr >> 11);
   if (group1.ral == 1 && group1.rotate == 1)
      regAC.addr = (0xfff & (regAC.addr << 2)) || (regAC.addr >> 10);
   else
      countClock--;
   updatePC(regPC.addr + 1);
   return 0;
}


decompiled_code:
__int64 __fastcall group2If(int a1, int a2, int a3, int a4)
{
int v4; // eax

dword_4040A4 = a3;
group2 = a2;
dword_4040C0 = (a1 >> 4) & 1;
dword_4040A8 = (a1 >> 7) & 1;
dword_4040AC = (a1 >> 6) & 1;
dword_4040C4 = (a1 >> 3) & 1;
v4 = (a1 >> 5) & 1;
dword_4040B8 = v4;
dword_4040D0 = (a1 >> 1) & 1;
dword_4040CC = (a1 >> 2) & 1;
dword_4040D4 = a4;
if ( dword_4040AC && regAC < 0 )
{
updatePC(regPC + 1);
v4 = dword_4040B8;
}
if ( v4 == 1 && !HIDWORD(regAC) )
updatePC(regPC + 1);
if ( dword_4040C0 == 1 && (_DWORD)regAC )
updatePC(regPC + 1);
if ( dword_4040B0 == 1 && SHIDWORD(regAC) > 0 )
updatePC(regPC + 1);
if ( dword_4040B4 == 1 && HIDWORD(regAC) )
updatePC(regPC + 1);
if ( dword_4040BC != 1 || (_DWORD)regAC )
{
if ( dword_4040C8 == 1 )
goto LABEL_15;
LABEL_8:
if ( dword_4040A8 == 1 )
goto LABEL_16;
goto LABEL_9;
}
updatePC(regPC + 1);
if ( dword_4040C8 != 1 )
goto LABEL_8;
LABEL_15:
updatePC(regPC + 1);
if ( dword_4040A8 == 1 )
{
LABEL_16:
updatePC(regPC + 1);
if ( dword_4040CC == 1 )
goto LABEL_17;
goto LABEL_10;
}
LABEL_9:
if ( dword_4040CC == 1 )
LABEL_17:
printf("Warning: Group 2 instruction ORs the SR with the AC; nop.");
LABEL_10:
updatePC(regPC + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2, int a3, int a4)
{
int v4; // eax

dword_4040A4 = a3;
group2 = a2;
dword_4040C0 = (a1 >> 4) & 1;
dword_4040A8 = (a1 >> 7) & 1;
dword_4040AC = (a1 >> 6) & 1;
dword_4040C4 = (a1 >> 3) & 1;
v4 = (a1 >> 5) & 1;
dword_4040B8 = v4;
dword_4040D0 = (a1 >> 1) & 1;
dword_4040CC = (a1 >> 2) & 1;
dword_4040D4 = a4;
if ( dword_4040AC && regAC < 0 )
{
func7(regPC + 1);
v4 = dword_4040B8;
}
if ( v4 == 1 && !HIDWORD(regAC) )
func7(regPC + 1);
if ( dword_4040C0 == 1 && (_DWORD)regAC )
func7(regPC + 1);
if ( dword_4040B0 == 1 && SHIDWORD(regAC) > 0 )
func7(regPC + 1);
if ( dword_4040B4 == 1 && HIDWORD(regAC) )
func7(regPC + 1);
if ( dword_4040BC != 1 || (_DWORD)regAC )
{
if ( dword_4040C8 == 1 )
goto LABEL_15;
LABEL_8:
if ( dword_4040A8 == 1 )
goto LABEL_16;
goto LABEL_9;
}
func7(regPC + 1);
if ( dword_4040C8 != 1 )
goto LABEL_8;
LABEL_15:
func7(regPC + 1);
if ( dword_4040A8 == 1 )
{
LABEL_16:
func7(regPC + 1);
if ( dword_4040CC == 1 )
goto LABEL_17;
goto LABEL_10;
}
LABEL_9:
if ( dword_4040CC == 1 )
LABEL_17:
printf("Warning: Group 2 instruction ORs the SR with the AC; nop.");
LABEL_10:
func7(regPC + 1);
return 0LL;
}

original_code:
int group2If(int i, int opcode, int groupbit, int bit11)
{
   group2.opcode = opcode;
   group2.bit3 = groupbit;
   group2.cla = (i >> 7) & 1;
   group2.sma = (i >> 6) & 1;
   group2.sza = (i >> 5) & 1;
   group2.snl = (i >> 4) & 1;
   group2.rev = (i >> 3) & 1;
   group2.osr = (i >> 2) & 1;
   group2.hlt = (i >> 1) & 1;
   group2.bit11 = bit11;
   if (group2.sma == 1)
   {
      if (regAC.addr < 0)
         updatePC(regPC.addr + 1);
   }
   if (group2.sza == 1)
   {
      if (regAC.addr == 0)
         updatePC(regPC.addr + 1);
   }
   if (group2.snl == 1)
  {
      if (regAC.link != 0)
         updatePC(regPC.addr + 1);
   }
   if (group2.spa == 1)
   {
      if (regAC.addr > 0)
         updatePC(regPC.addr + 1);
   }
   if (group2.sna == 1)
   {
      if (regAC.addr != 0)
         updatePC(regPC.addr + 1);
   }
   if (group2.szl == 1)
   {
      if (regAC.link == 0)
          updatePC(regPC.addr + 1);
   }
   if (group2.skp == 1)
      updatePC(regPC.addr + 1);
   if (group2.cla == 1)
      updatePC(regPC.addr + 1);
   if (group2.osr == 1)
   {
      printf("Warning: Group 2 instruction ORs the SR with the AC; nop.");
   }
   updatePC(regPC.addr + 1);
   return 0;
}


updateCPMA

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

regCPMA = a1;
result = (unsigned int)(a1 >> 7);
dword_404110 = a1 & 0x7F;
dword_40410C = a1 >> 7;
return result;
}

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

regCPMA = a1;
result = (unsigned int)(a1 >> 7);
dword_404110 = a1 & 0x7F;
dword_40410C = a1 >> 7;
return result;
}

original_code:
int updateCPMA(int i)
{
  regCPMA.addr = i;
  regCPMA.page = i >> 7;
  regCPMA.offset = i & 0x7f;
}


handleIR

original_code:
int handleIR(int i)
{
  regIR.addr = i;
  regIR.page = i >> 7;
  regIR.indirectBit = (i >> 8) & 1;
  regIR.pageBit = (i >> 7) & 1;
  regIR.offset = i & 0x7f;
  regIR.opcode = i >> 9;
}


getHex

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

result = (unsigned int)(a1 - 48);
if ( (unsigned int)result > 9 )
{
if ( (unsigned int)(a1 - 97) <= 5 )
{
result = (unsigned int)(a1 - 87);
}
else if ( (unsigned int)(a1 - 65) > 5 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)(a1 - 55);
}
}
return result;
}

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

result = (unsigned int)(a1 - 48);
if ( (unsigned int)result > 9 )
{
if ( (unsigned int)(a1 - 97) <= 5 )
{
result = (unsigned int)(a1 - 87);
}
else if ( (unsigned int)(a1 - 65) > 5 )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)(a1 - 55);
}
}
return result;
}

original_code:
int getHex(int num)
{
  if (48 <= num && num <= 57)
     return num - 48;
  else if (97 <= num && num <= 102)
     return num - 87;
  else if (65 <= num && num <= 70)
     return num - 55;
  else
     return -1;
}

variable_types: num int


loadAllTheThings

decompiled_code:
__int64 loadAllTheThings()
{
int v0; // ebx
int v1; // esi
int v2; // eax
int v3; // edx
int v4; // edi
int v5; // eax
int v6; // edx
int v7; // er8
int v8; // er8
char v10; // [rsp+Bh] [rbp-Dh]
char v11; // [rsp+Ch] [rbp-Ch]
char v12; // [rsp+Dh] [rbp-Bh]
char v13; // [rsp+Eh] [rbp-Ah]

v0 = 0;
ifp = fopen("test.mem", "r");
while ( (unsigned int)__isoc99_fscanf() != -1 )
{
if ( v10 == 64 )
{
++v0;
getHex(v11);
getHex(v12);
v5 = getHex(v13);
updatePC(v5 + 16 * (v6 + v7));
if ( v0 == 1 )
{
handleIR(v8);
}
else if ( v0 != 2 )
{
puts("Warning: Input file lines after the third @ were ignored;\n the program will now execute.\n");
break;
}
}
else
{
if ( !v0 )
{
puts("Warning: The first line of the input file did not contain an @. Halting program.");
break;
}
getHex(v10);
v1 = getHex(v11);
v2 = getHex(v12);
v4 = regPC + 1;
memory[128 * (__int64)dword_404144 + dword_404148] = v2 + 16 * (v1 + v3);
updatePC(v4);
}
}
fclose(ifp);
return 0LL;
}

canonical_decompiled_code:
__int64 func13()
{
int v0; // ebx
int v1; // esi
int v2; // eax
int v3; // edx
int v4; // edi
int v5; // eax
int v6; // edx
int v7; // er8
int v8; // er8
char v10; // [rsp+Bh] [rbp-Dh]
char v11; // [rsp+Ch] [rbp-Ch]
char v12; // [rsp+Dh] [rbp-Bh]
char v13; // [rsp+Eh] [rbp-Ah]

v0 = 0;
ifp = fopen("test.mem", "r");
while ( (unsigned int)__isoc99_fscanf() != -1 )
{
if ( v10 == 64 )
{
++v0;
func12(v11);
func12(v12);
v5 = func12(v13);
func7(v5 + 16 * (v6 + v7));
if ( v0 == 1 )
{
func11(v8);
}
else if ( v0 != 2 )
{
puts("Warning: Input file lines after the third @ were ignored;\n the program will now execute.\n");
break;
}
}
else
{
if ( !v0 )
{
puts("Warning: The first line of the input file did not contain an @. Halting program.");
break;
}
func12(v10);
v1 = func12(v11);
v2 = func12(v12);
v4 = regPC + 1;
memory[128 * (__int64)dword_404144 + dword_404148] = v2 + 16 * (v1 + v3);
func7(v4);
}
}
fclose(ifp);
return 0LL;
}

original_code:
int loadAllTheThings()
{
  int count;
  int input1;
  int input2;
  int input3;
  int i;
  char input[5];
  count = 0;
  ifp = fopen("test.mem", "r");
  while (fscanf(ifp, "%s\n", &input) != (-1))
  {
    if (input[0] == '@')
    {
      count++;
      input1 = getHex(input[1]);
      input2 = getHex(input[2]);
      input3 = getHex(input[3]);
      i = input1*256 + input2*16 + input3;
      updatePC(i);
      if (count == 1)
      {
        handleIR(i);
      }
      else if (count > 2)
      {
         printf("Warning: Input file lines after the third @ were ignored;\n "
                          "    the program will now execute.\n\n");
         break;
      }
    }
    else
    {
         if (count == 0)
         {
            printf("Warning: The first line of the input file did not "
                             "contain an @. Halting program.\n");
     break;
         }
         input1 = getHex(input[0]);
         input2 = getHex(input[1]);
         input3 = getHex(input[2]);
         i = input1*256 + input2*16 + input3;
         memory[regPC.page][regPC.offset] = i;
         updatePC(regPC.addr + 1);
    }
  }
  fclose(ifp);
  return 0;
}

variable_types:
input
char [5]


getEffAddr

decompiled_code:
__int64 getEffAddr()
{
int v0; // eax
int v1; // ecx

if ( dword_40412C )
{
if ( dword_40412C == 1 )
{
v0 = dword_404128;
if ( dword_404128 == 1 )
{
++countClock;
regCPMA = memory[128 * (__int64)dword_404130 + dword_404134];
return 0LL;
}
if ( !dword_404128 )
{
regCPMA = dword_404130 + dword_404134;
return 0LL;
}
}
else
{
if ( dword_40412C > 1 )
{
LABEL_13:
printf("Something's gone badly wrong.");
return 0LL;
}
v0 = dword_404128;
}
LABEL_12:
if ( v0 <= 1 )
return 0LL;
goto LABEL_13;
}
v0 = dword_404128;
if ( dword_404128 != 1 )
{
if ( !dword_404128 )
{
countClock += 2;
v1 = memory[dword_404134] + 1;
regCPMA = memory[dword_404134];
memory[dword_404134] = v1;
return 0LL;
}
goto LABEL_12;
}
++countClock;
regCPMA = memory[dword_404134];
return 0LL;
}

canonical_decompiled_code:
__int64 func14()
{
int v0; // eax
int v1; // ecx

if ( dword_40412C )
{
if ( dword_40412C == 1 )
{
v0 = dword_404128;
if ( dword_404128 == 1 )
{
++countClock;
regCPMA = memory[128 * (__int64)dword_404130 + dword_404134];
return 0LL;
}
if ( !dword_404128 )
{
regCPMA = dword_404130 + dword_404134;
return 0LL;
}
}
else
{
if ( dword_40412C > 1 )
{
LABEL_13:
printf("Something's gone badly wrong.");
return 0LL;
}
v0 = dword_404128;
}
LABEL_12:
if ( v0 <= 1 )
return 0LL;
goto LABEL_13;
}
v0 = dword_404128;
if ( dword_404128 != 1 )
{
if ( !dword_404128 )
{
countClock += 2;
v1 = memory[dword_404134] + 1;
regCPMA = memory[dword_404134];
memory[dword_404134] = v1;
return 0LL;
}
goto LABEL_12;
}
++countClock;
regCPMA = memory[dword_404134];
return 0LL;
}

original_code:
int getEffAddr()
{
        if (regIR.pageBit == 0 && regIR.indirectBit == 1)
        {
          regCPMA.addr = memory[0][regIR.offset];
          countClock++;
        }
        else if (regIR.pageBit == 1 && regIR.indirectBit == 1)
        {
          regCPMA.addr = memory[regIR.page][regIR.offset];
          countClock++;
        }
        else if (regIR.pageBit == 1 && regIR.indirectBit == 0)
        {
          regCPMA.addr = regIR.page + regIR.offset;
        }
        else if (regIR.pageBit == 0 && regIR.indirectBit == 0)
        {
          if (0x8 < regIR.offset < 0xf)
          {
            regCPMA.addr = memory[0][regIR.offset]++;
            countClock = countClock + 2;
          }
          else
          {
            regCPMA.addr = memory[0][regIR.offset];
            countClock++;
          }
        }
        else if (regIR.pageBit > 1 || regIR.indirectBit > 1)
        {
          printf("Something's gone badly wrong.");
        }
 return 0;
}


displayMemory

decompiled_code:
int displayMemory()
{
char *v0; // rbp
char *v1; // rbx
__int64 v2; // rsi
int result; // eax

v0 = (char *)&unk_404340;
puts("Contents of the first pages of memory:");
do
{
v1 = v0 - 480;
do
{
v2 = *(unsigned int *)v1;
v1 += 4;
printf("%x ", v2);
}
while ( v1 != v0 );
v0 = v1 + 512;
result = puts("\n");
}
while ( v1 != (char *)&unk_404740 );
return result;
}

canonical_decompiled_code:
int func15()
{
char *v0; // rbp
char *v1; // rbx
__int64 v2; // rsi
int result; // eax

v0 = (char *)&unk_404340;
puts("Contents of the first pages of memory:");
do
{
v1 = v0 - 480;
do
{
v2 = *(unsigned int *)v1;
v1 += 4;
printf("%x ", v2);
}
while ( v1 != v0 );
v0 = v1 + 512;
result = puts("\n");
}
while ( v1 != (char *)&unk_404740 );
return result;
}

original_code:
void displayMemory()
{
  printf("Contents of the first pages of memory:\n");
  int p;
  int y;
  for (p = 0; p < 3; p++)
  {
    for (y = 0; y < 120; y++)
    {
      printf("%x ",memory[p][y]);
    }
    printf("\n\n");
  }
}

variable_types: p int


decompiled_code:
int displayGroup1()
{
return printf(
"grp1: op bit3 cla cll cma cml rar ral rot iac\n %-3d %-4d %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-3d\n",
(unsigned int)group1,
(unsigned int)dword_4040E4,
(unsigned int)dword_4040E8,
(unsigned int)dword_4040EC,
(unsigned int)dword_4040F0,
(unsigned int)dword_4040F4,
(unsigned int)dword_4040F8,
(unsigned int)dword_4040FC,
(unsigned int)dword_404100,
(unsigned int)dword_404104);
}

canonical_decompiled_code:
int func16()
{
return printf(
"grp1: op bit3 cla cll cma cml rar ral rot iac\n %-3d %-4d %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-3d\n",
(unsigned int)group1,
(unsigned int)dword_4040E4,
(unsigned int)dword_4040E8,
(unsigned int)dword_4040EC,
(unsigned int)dword_4040F0,
(unsigned int)dword_4040F4,
(unsigned int)dword_4040F8,
(unsigned int)dword_4040FC,
(unsigned int)dword_404100,
(unsigned int)dword_404104);
}

original_code:
void displayGroup1()
{
  printf("grp1: op bit3 cla cll cma cml rar ral rot iac\n"
         "      %-3d %-4d %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-3d\n",
         group1.opcode, group1.bit3, group1.cla, group1.cll,
         group1.cma, group1.cml, group1.rar, group1.ral,
         group1.rotate, group1.iac);
}


decompiled_code:
int displayGroup2()
{
return printf(
"grp2: op bit3 cla sma sza snl rev osr hlt bit11\n %-3d %-4d %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-5d\n",
(unsigned int)group2,
(unsigned int)dword_4040A4,
(unsigned int)dword_4040A8,
(unsigned int)dword_4040AC,
(unsigned int)dword_4040B8,
(unsigned int)dword_4040C0,
(unsigned int)dword_4040C4,
(unsigned int)dword_4040CC,
(unsigned int)dword_4040D0,
(unsigned int)dword_4040D4);
}

canonical_decompiled_code:
int func17()
{
return printf(
"grp2: op bit3 cla sma sza snl rev osr hlt bit11\n %-3d %-4d %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-5d\n",
(unsigned int)group2,
(unsigned int)dword_4040A4,
(unsigned int)dword_4040A8,
(unsigned int)dword_4040AC,
(unsigned int)dword_4040B8,
(unsigned int)dword_4040C0,
(unsigned int)dword_4040C4,
(unsigned int)dword_4040CC,
(unsigned int)dword_4040D0,
(unsigned int)dword_4040D4);
}

original_code:
void displayGroup2()
{
  printf("grp2: op bit3 cla sma sza snl rev osr hlt bit11\n"
         "      %-3d %-4d %-3d %-3d %-3d %-3d %-3d %-3d %-3d %-5d\n",
         group2.opcode, group2.bit3, group2.cla, group2.sma,
         group2.sza, group2.snl, group2.rev, group2.osr,
         group2.hlt, group2.bit11);
}