bold

repo: nlf/git-gh

decompiled_code:
char *__fastcall bold(const char *a1)
{
size_t v1; // rbx
char *v2; // rbp

v1 = (int)(strlen(a1) + 9);
v2 = (char *)calloc(v1, 1uLL);
snprintf(v2, v1, "%s%s%s", "\x1B[1m", a1, "\x1B[0m");
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1)
{
size_t v1; // rbx
char *v2; // rbp

v1 = (int)(strlen(a1) + 9);
v2 = (char *)calloc(v1, 1uLL);
snprintf(v2, v1, "%s%s%s", "\x1B[1m", a1, "\x1B[0m");
return v2;
}

original_code:
char* bold(char* str) {
    int str_len;
    char* newstr;
    char* boldstart = "\x1b[1m";
    char* boldend = "\x1b[0m";
    str_len = strlen(boldstart) + strlen(boldend) + strlen(str) + 1;
    newstr = calloc(str_len, sizeof(char));
    snprintf(newstr, str_len, "%s%s%s", boldstart, str, boldend);
    return newstr;
}

canonical_original_code:
char *bold(char *str) {
    int str_len;
    char *newstr;
    char *boldstart = "\x1b[1m";
    char *boldend = "\x1b[0m";
    str_len = strlen(boldstart) + strlen(boldend) + strlen(str) + 1;
    newstr = calloc(str_len, sizeof(char));
    snprintf(newstr, str_len, "%s%s%s", boldstart, str, boldend);
    return newstr;
}

variable_types:
str
char *

variable_types:
newstr
char *

variable_types:
boldstart
char *

variable_types:
boldend
char *

return_type: char *


decompiled_code:
int print_header()
{
return fprintf(
stdout,
"\x1B[1m%-5s %-100.100s %-15.15s %-15.15s %-5s\x1B[0m\n",
"ID",
"Title",
"Assignee",
"Milestone",
"Comments");
}

canonical_decompiled_code:
int func0()
{
return fprintf(
stdout,
"\x1B[1m%-5s %-100.100s %-15.15s %-15.15s %-5s\x1B[0m\n",
"ID",
"Title",
"Assignee",
"Milestone",
"Comments");
}

original_code:
void print_header() {
    fprintf(stdout, "\x1b[1m%-5s %-100.100s %-15.15s %-15.15s %-5s\x1b[0m\n", "ID", "Title", "Assignee", "Milestone", "Comments");
}

return_type: void


closestate

repo: nlm/throttle

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

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

original_code:
int closestate(int fd)
{
 return close(fd);
}

variable_types: fd int

return_type: int


writestate

decompiled_code:
__int64 __fastcall writestate(int fd, void *buf)
{
int v2; // ebx

lseek(fd, 0LL, 0);
v2 = write(fd, buf, 0x18uLL);
if ( v2 < 0 )
perror("err:");
return (unsigned int)-(v2 != 24);
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, void *buf)
{
int v2; // ebx

lseek(fd, 0LL, 0);
v2 = write(fd, buf, 0x18uLL);
if ( v2 < 0 )
perror("err:");
return (unsigned int)-(v2 != 24);
}

original_code:
int writestate(int fd, const thstate_t *state)
{
 int wsize;
 lseek(fd, 0, 0);
 wsize = write(fd, state, sizeof(thstate_t));
 if (wsize < 0)
  perror("err:");
 if (wsize != sizeof(thstate_t))
  return -1;
 return 0;
}

canonical_original_code:
int writestate(int fd, struct thstate_s *state)
{
 int wsize;
 lseek(fd, 0, 0);
 wsize = write(fd, state, sizeof(thstate_t));
 if (wsize < 0)
  perror("err:");
 if (wsize != sizeof(thstate_t))
  return -1;
 return 0;
}

variable_types: struct thstate_s { char magic[4]; long startts; unsigned int times; } *

variable_types: wsize int


initstate

original_code:
thstate_t *initstate(thstate_t *state)
{
 strncpy(state->magic, "THRO", 4);
 state->startts = time(((void *)0));
 state->times = 0;
 return state;
}

canonical_original_code:
struct thstate_s *initstate(struct thstate_s *state)
{
 strncpy(state->magic, "THRO", 4);
 state->startts = time(((void *)0));
 state->times = 0;
 return state;
}

variable_types:
state
struct thstate_s *

return_type: struct thstate_s { char magic[4]; long startts; unsigned int times; } *


xexec

decompiled_code:
int __fastcall xexec(int a1, const void *a2)
{
size_t v2; // rbx
const char **v3; // rax
const char **v4; // rbp

v2 = 8LL * (a1 + 1);
v3 = (const char **)malloc(v2);
if ( !v3 )
{
perror("error: unable to allocate\n");
exit(102);
}
v4 = v3;
memcpy(v3, a2, v2 - 8);
v4[v2 / 8 - 1] = 0LL;
return execv(*v4, (char *const *)v4);
}

canonical_decompiled_code:
int __fastcall func6(int a1, const void *a2)
{
size_t v2; // rbx
const char **v3; // rax
const char **v4; // rbp

v2 = 8LL * (a1 + 1);
v3 = (const char **)malloc(v2);
if ( !v3 )
{
perror("error: unable to allocate\n");
exit(102);
}
v4 = v3;
memcpy(v3, a2, v2 - 8);
v4[v2 / 8 - 1] = 0LL;
return execv(*v4, (char *const *)v4);
}

original_code:
int xexec(const int argc, char * const *argv)
{
 char **xargv;
 if (!(xargv = malloc(sizeof(char *) * (argc + 1))))
 {
  perror("error: unable to allocate\n");
  exit(102);
 }
 memcpy(xargv, argv, sizeof(char *) * argc);
 xargv[argc] = ((void *)0);
 return execv(xargv[0], xargv);
 free(xargv);
}

canonical_original_code:
int xexec(int argc, char **argv)
{
 char **xargv;
 if (!(xargv = malloc(sizeof(char *) * (argc + 1))))
 {
  perror("error: unable to allocate\n");
  exit(102);
 }
 memcpy(xargv, argv, sizeof(char *) * argc);
 xargv[argc] = ((void *)0);
 return execv(xargv[0], xargv);
 free(xargv);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
xargv
char **


initstate

user_defined_types: struct thstate_s { char magic[4]; long startts; unsigned int times; }


main

canonical_name: main

original_code:
int main(int argc, char **argv)
{
 unsigned int times;
 unsigned int period;
 const char *statefile;
 thstate_t thstate;
 thstate_t *state;
 time_t now;
 int fd;
 if (argc < 4)
 {
  usage();
  exit(101);
 }
 times = atoi(argv[1]);
 period = atoi(argv[2]);
 statefile = argv[3];
 if (times < 1 || period < 1)
 {
  usage();
  exit(101);
 }
 if ((fd = openstate(statefile)) < 0)
 {
  perror("unable to open state file");
  exit(103);
 }
 state = readstate(fd, &thstate);
 if (state == ((void *)0))
 {
  state = initstate(&thstate);
 }
 now = time(((void *)0));
 if (state->startts + period > now)
 {
  state->times++;
 }
 else
 {
  state->startts = now;
  state->times = 1;
 }
 writestate(fd, state);
 closestate(fd);
 if (state->times <= times)
 {
  if (xexec(argc - 4, argv + 4) == -1)
  {
   perror("exec");
   return 1;
  }
 }
 else
 {
  exit(100);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 unsigned int times;
 unsigned int period;
 char *statefile;
 struct thstate_s thstate;
 struct thstate_s *state;
 long now;
 int fd;
 if (argc < 4)
 {
  usage();
  exit(101);
 }
 times = atoi(argv[1]);
 period = atoi(argv[2]);
 statefile = argv[3];
 if (times < 1 || period < 1)
 {
  usage();
  exit(101);
 }
 if ((fd = openstate(statefile)) < 0)
 {
  perror("unable to open state file");
  exit(103);
 }
 state = readstate(fd, &thstate);
 if (state == ((void *)0))
 {
  state = initstate(&thstate);
 }
 now = time(((void *)0));
 if (state->startts + period > now)
 {
  state->times++;
 }
 else
 {
  state->startts = now;
  state->times = 1;
 }
 writestate(fd, state);
 closestate(fd);
 if (state->times <= times)
 {
  if (xexec(argc - 4, argv + 4) == -1)
  {
   perror("exec");
   return 1;
  }
 }
 else
 {
  exit(100);
 }
 return 0;
}

variable_types:
times
unsigned int

variable_types:
period
unsigned int

variable_types:
statefile
char *

variable_types: struct thstate_s { char magic[4]; long startts; unsigned int times; }

variable_types: now long


closestate

canonical_original_code:
int closestate(int fd)
{
 return close(fd);
}


dumpstate

decompiled_code:
int __fastcall dumpstate(__int64 a1)
{
return printf("STATE: startts=%lu, times=%lu\n", *(_QWORD *)(a1 + 8), *(unsigned int *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
return printf("STATE: startts=%lu, times=%lu\n", *(_QWORD *)(a1 + 8), *(unsigned int *)(a1 + 16));
}

original_code:
void dumpstate(const thstate_t *state)
{
 printf("STATE: startts=%lu, times=%lu\n", (long)state->startts, (long)state->times);
}

canonical_original_code:
void dumpstate(struct thstate_s *state)
{
 printf("STATE: startts=%lu, times=%lu\n", (long)state->startts, (long)state->times);
}


pidfilename

repo: nmeyerhans/authd

decompiled_code:
char *__fastcall pidfilename(const char *a1)
{
char *result; // rax

result = pidfile_0;
if ( a1 )
{
result = getenv("LOGNAME");
if ( result )
{
snprintf(pidfile_0, 0x1000uLL, "/tmp/authd-%s/%s", result, a1);
result = pidfile_0;
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func7(const char *a1)
{
char *result; // rax

result = pidfile_0;
if ( a1 )
{
result = getenv("LOGNAME");
if ( result )
{
snprintf(pidfile_0, 0x1000uLL, "/tmp/authd-%s/%s", result, a1);
result = pidfile_0;
}
}
return result;
}

original_code:
char *pidfilename(char *name) {
    static char pidfile[4096];
    char *dir, *logname;
    if(!name)
 return pidfile;
    if(!(logname = getenv("LOGNAME")))
 return 0;
    snprintf(pidfile, 4096, "/tmp/authd-%s/%s", logname, name);
    return pidfile;
}

canonical_original_code:
char *pidfilename(char *name) {
    static char pidfile[4096];
    char *dir; char *logname;
    if(!name)
 return pidfile;
    if(!(logname = getenv("LOGNAME")))
 return 0;
    snprintf(pidfile, 4096, "/tmp/authd-%s/%s", logname, name);
    return pidfile;
}

variable_types:
name
char *

variable_types:
pidfile
char [4096]

variable_types:
dir
char *

variable_types:
logname
char *


decompiled_code:
__int64 __fastcall write_pidfile(char *file)
{
char *v1; // r12
char *v2; // r13
unsigned int v3; // ebx
int v4; // ebp
unsigned int v6; // eax
char *v7; // rbx
size_t v8; // rax
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v1 = strdup(file);
if ( !getenv("LOGNAME") )
return (unsigned int)-1;
v2 = dirname(v1);
v3 = mkdir(v2, 0x1C0u);
if ( v3 == -1 && *__errno_location() != 17 )
{
perror(v2);
}
else
{
v4 = open(file, 193, 384LL);
if ( v4 == -1 )
{
v3 = *__errno_location();
if ( v3 != 17 )
{
perror(v1);
v3 = v4;
}
}
else
{
v6 = getpid();
asprintf(&ptr, "%d\n", v6);
v7 = ptr;
v8 = strlen(ptr);
write(v4, v7, v8);
close(v4);
free(v1);
free(ptr);
v3 = 0;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *file)
{
char *v1; // r12
char *v2; // r13
unsigned int v3; // ebx
int v4; // ebp
unsigned int v6; // eax
char *v7; // rbx
size_t v8; // rax
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v1 = strdup(file);
if ( !getenv("LOGNAME") )
return (unsigned int)-1;
v2 = dirname(v1);
v3 = mkdir(v2, 0x1C0u);
if ( v3 == -1 && *__errno_location() != 17 )
{
perror(v2);
}
else
{
v4 = open(file, 193, 384LL);
if ( v4 == -1 )
{
v3 = *__errno_location();
if ( v3 != 17 )
{
perror(v1);
v3 = v4;
}
}
else
{
v6 = getpid();
asprintf(&ptr, "%d\n", v6);
v7 = ptr;
v8 = strlen(ptr);
write(v4, v7, v8);
close(v4);
free(v1);
free(ptr);
v3 = 0;
}
}
return v3;
}

original_code:
int write_pidfile(const char *file) {
    char *nam, *dir, *path, *pidstr;
    int fd, pid;
    path = strdup(file);
    nam = getenv("LOGNAME");
    if(!nam)
 return -1;
    dir = dirname(path);
    if(mkdir(dir, 0700) == -1) {
 if((*__errno_location ()) != 17) {
     perror(dir);
     return -1;
 }
    }
    if((fd = open(file, 0100|0200|01, 0600)) == -1) {
 if((*__errno_location ()) == 17) {
     return (*__errno_location ());
 }
 perror(path);
 return -1;
    }
    pid = getpid();
    asprintf(&pidstr, "%d\n", pid);
    write(fd, pidstr, strlen(pidstr));
    close(fd);
    free(path);
    free(pidstr);
    return 0;
}

canonical_original_code:
int write_pidfile(char *file) {
    char *nam; char *dir; char *path; char *pidstr;
    int fd; int pid;
    path = strdup(file);
    nam = getenv("LOGNAME");
    if(!nam)
 return -1;
    dir = dirname(path);
    if(mkdir(dir, 0700) == -1) {
 if((*__errno_location ()) != 17) {
     perror(dir);
     return -1;
 }
    }
    if((fd = open(file, 0100|0200|01, 0600)) == -1) {
 if((*__errno_location ()) == 17) {
     return (*__errno_location ());
 }
 perror(path);
 return -1;
    }
    pid = getpid();
    asprintf(&pidstr, "%d\n", pid);
    write(fd, pidstr, strlen(pidstr));
    close(fd);
    free(path);
    free(pidstr);
    return 0;
}

variable_types:
file
char *

variable_types:
nam
char *

variable_types:
path
char *

variable_types:
pidstr
char *

variable_types: pid int


decompiled_code:
int destroy_pidfile()
{
char *v0; // rbx
char *v1; // rax
char *v2; // rbp

v0 = pidfilename(0LL);
v1 = strdup(v0);
v2 = dirname(v1);
unlink(v0);
return rmdir(v2);
}

canonical_decompiled_code:
int func10()
{
char *v0; // rbx
char *v1; // rax
char *v2; // rbp

v0 = func7(0LL);
v1 = strdup(v0);
v2 = dirname(v1);
unlink(v0);
return rmdir(v2);
}

original_code:
void destroy_pidfile() {
    char *pidfile = pidfilename(0);
    char *dir;
    dir = strdup(pidfile);
    dir = dirname(dir);
    unlink(pidfile);
    rmdir(dir);
}

variable_types:
pidfile
char *


decompiled_code:
void __noreturn sig_handler()
{
destroy_pidfile();
exit(0);
}

canonical_decompiled_code:
void __noreturn func11()
{
func10();
exit(0);
}

original_code:
void *sig_handler(int sig) {
    destroy_pidfile();
    exit(0);
}

variable_types: sig int

return_type: void *


decompiled_code:
_BOOL8 __fastcall check_parent(unsigned int a1, const char *a2)
{
int v2; // edx
_BOOL8 result; // rax
int v4; // ebx
char v5[1024]; // [rsp+0h] [rbp-808h] BYREF
char buf[1032]; // [rsp+400h] [rbp-408h] BYREF

v2 = kill(a1, 0);
result = 0LL;
if ( v2 != -1 )
{
snprintf(v5, 0x400uLL, "/proc/%d/cmdline", a1);
v4 = open(v5, 0);
result = 0LL;
if ( v4 != -1 )
{
read(v4, buf, 0x3E8uLL);
close(v4);
result = strcmp(a2, buf) == 0;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(unsigned int a1, const char *a2)
{
int v2; // edx
_BOOL8 result; // rax
int v4; // ebx
char v5[1024]; // [rsp+0h] [rbp-808h] BYREF
char buf[1032]; // [rsp+400h] [rbp-408h] BYREF

v2 = kill(a1, 0);
result = 0LL;
if ( v2 != -1 )
{
snprintf(v5, 0x400uLL, "/proc/%d/cmdline", a1);
v4 = open(v5, 0);
result = 0LL;
if ( v4 != -1 )
{
read(v4, buf, 0x3E8uLL);
close(v4);
result = strcmp(a2, buf) == 0;
}
}
return result;
}

original_code:
int check_parent(int gp_pid, char *cmdline) {
    char seen_cmdline[1000];
    char path[1024];
    int fd;
    if(kill(gp_pid, 0) == -1)
 return 0;
    snprintf(path, 1024, "/proc/%d/cmdline", gp_pid);
    fd = open(path, 00);
    if(fd == -1)
 return 0;
    read(fd, seen_cmdline, 1000);
    close(fd);
    if(strcmp(cmdline, seen_cmdline)) {
 return 0;
    }
    return 1;
}

variable_types:
cmdline
char *

variable_types:
path
char [1024]

variable_types: fd int


decompiled_code:
_BOOL8 __fastcall child_loop(unsigned int a1, const char *a2)
{
int v2; // ebx
_BOOL8 result; // rax

v2 = 0;
while ( 1 )
{
result = check_parent(a1, a2);
if ( !result )
break;
++v2;
sleep(0x3Cu);
if ( 60 * v2 > 3599 )
{
v2 = system("kinit -R > /dev/null");
if ( v2 )
v2 = 0;
else
system("aklog > /dev/null");
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(unsigned int a1, const char *a2)
{
int v2; // ebx
_BOOL8 result; // rax

v2 = 0;
while ( 1 )
{
result = func13(a1, a2);
if ( !result )
break;
++v2;
sleep(0x3Cu);
if ( 60 * v2 > 3599 )
{
v2 = system("kinit -R > /dev/null");
if ( v2 )
v2 = 0;
else
system("aklog > /dev/null");
}
}
return result;
}

original_code:
void child_loop(int gp_pid, char *cmdline) {
    int loop_count = 0;
    while(1) {
 if(loop_count * 60 >= 3600) {
     loop_count = 0;
     if(system("kinit -R > /dev/null") == 0) {
  system("aklog > /dev/null");
     }
 }
 if(!check_parent(gp_pid, cmdline))
     break;
 loop_count++;
 sleep(60);
    }
}


decompiled_code:
ssize_t __fastcall read_ppid_cmdline(unsigned int a1, void *a2, int a3)
{
int v4; // eax
ssize_t result; // rax
char v6[1048]; // [rsp+0h] [rbp-418h] BYREF

sprintf(v6, "/proc/%d/cmdline", a1);
v4 = open(v6, 0);
if ( v4 < 0 )
{
perror("open");
exit(1);
}
result = read(v4, a2, a3);
if ( result < 0 )
{
perror("read");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func15(unsigned int a1, void *a2, int a3)
{
int v4; // eax
ssize_t result; // rax
char v6[1048]; // [rsp+0h] [rbp-418h] BYREF

sprintf(v6, "/proc/%d/cmdline", a1);
v4 = open(v6, 0);
if ( v4 < 0 )
{
perror("open");
exit(1);
}
result = read(v4, a2, a3);
if ( result < 0 )
{
perror("read");
exit(1);
}
return result;
}

original_code:
void read_ppid_cmdline(int pid, char *buf, int bufsz) {
    char path[1024];
    int fd;
    sprintf(path, "/proc/%d/cmdline", pid);
    fd = open(path, 00);
    if(fd < 0) {
 perror("open");
 exit(1);
    }
    if(read(fd, buf, bufsz) < 0) {
 perror("read");
 exit(1);
    }
}

variable_types:
buf
char *

variable_types: bufsz int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
unsigned int v4; // eax
unsigned int v5; // ebx
__pid_t v6; // eax
char *v8; // rax
int v9; // eax
char *v10; // rax
unsigned int v11; // ebx
unsigned int v12; // eax
char *ptr; // [rsp+8h] [rbp-400h] BYREF
char v14[1016]; // [rsp+10h] [rbp-3F8h] BYREF

if ( argc > 1 )
{
v3 = argv[1];
if ( !strcmp(v3, "-h") || !strcmp(v3, "--help") )
{
usage(0);
exit(0);
}
}
v4 = getsid(0);
v5 = v4;
if ( v4 == -1 )
{
perror("getsid()");
exit(1);
}
asprintf(&ptr, "%d", v4);
read_ppid_cmdline(v5, v14, 1000);
v6 = fork();
if ( v6 )
{
if ( v6 < 0 )
{
perror("fork");
exit(1);
}
}
else
{
signal(1, (__sighandler_t)1);
signal(15, (__sighandler_t)sig_handler);
signal(2, (__sighandler_t)sig_handler);
v8 = pidfilename(ptr);
v9 = write_pidfile(v8);
if ( v9 )
{
if ( v9 > 0 )
{
v10 = pidfilename(0LL);
v11 = read_pidfile(v10);
fwrite("authd appears to already be running ", 1uLL, 0x24uLL, stderr);
fprintf(stderr, "in this session with pid %d\n", v11);
exit(1);
}
fwrite("Aborting after something unexpected happened.\n", 1uLL, 0x2EuLL, stderr);
exit(1);
}
v12 = getpid();
printf("authd is running as %d ", v12);
printf("in session %d\n", v5);
close(0);
close(1);
close(2);
child_loop(v5, v14);
destroy_pidfile();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
unsigned int v4; // eax
unsigned int v5; // ebx
__pid_t v6; // eax
char *v8; // rax
int v9; // eax
char *v10; // rax
unsigned int v11; // ebx
unsigned int v12; // eax
char *ptr; // [rsp+8h] [rbp-400h] BYREF
char v14[1016]; // [rsp+10h] [rbp-3F8h] BYREF

if ( argc > 1 )
{
v3 = argv[1];
if ( !strcmp(v3, "-h") || !strcmp(v3, "--help") )
{
func12(0);
exit(0);
}
}
v4 = getsid(0);
v5 = v4;
if ( v4 == -1 )
{
perror("getsid()");
exit(1);
}
asprintf(&ptr, "%d", v4);
func15(v5, v14, 1000);
v6 = fork();
if ( v6 )
{
if ( v6 < 0 )
{
perror("fork");
exit(1);
}
}
else
{
signal(1, (__sighandler_t)1);
signal(15, (__sighandler_t)sig_handler);
signal(2, (__sighandler_t)sig_handler);
v8 = func7(ptr);
v9 = func8(v8);
if ( v9 )
{
if ( v9 > 0 )
{
v10 = func7(0LL);
v11 = func9(v10);
fwrite("authd appears to already be running ", 1uLL, 0x24uLL, stderr);
fprintf(stderr, "in this session with pid %d\n", v11);
exit(1);
}
fwrite("Aborting after something unexpected happened.\n", 1uLL, 0x2EuLL, stderr);
exit(1);
}
v12 = getpid();
printf("authd is running as %d ", v12);
printf("in session %d\n", v5);
close(0);
close(1);
close(2);
func14(v5, v14);
func10();
}
return 0;
}

original_code:
int main(int argc, char **argv) {
    int other_pid, ppid, child_pid;
    int sh_type;
    char cmdline[1000];
    char *pidstr;
    if(argc > 1) {
 if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
     usage(0);
     exit(0);
 }
    }
    if((ppid = getsid(0)) == -1) {
 perror("getsid()");
 exit(1);
    }
    asprintf(&pidstr, "%d", ppid);
    read_ppid_cmdline(ppid, cmdline, 1000);
    if(child_pid = fork()) {
 if(child_pid < 0) {
     perror("fork");
     exit(1);
 }
    }
    else {
 int rv;
 signal(1, ((__sighandler_t) 1));
 signal(15, (void*)sig_handler);
 signal(2, (void*)sig_handler);
 if((rv = write_pidfile(pidfilename(pidstr))) == 0) {
     printf("authd is running as %d ", getpid());
     printf("in session %d\n", ppid);
     close(0); close(1); close(2);
     child_loop(ppid, cmdline);
     destroy_pidfile();
 } else if(rv > 0) {
     int other_pid;
     other_pid = read_pidfile(pidfilename(((void *)0)));
     fprintf(stderr, "authd appears to already be running ");
     fprintf(stderr, "in this session with pid %d\n", other_pid);
     exit(1);
 } else {
     fprintf(stderr, "Aborting after something unexpected happened.\n");
     exit(1);
 }
    }
}

canonical_original_code:
int main(int argc, char **argv) {
    int other_pid; int ppid; int child_pid;
    int sh_type;
    char cmdline[1000];
    char *pidstr;
    if(argc > 1) {
 if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
     usage(0);
     exit(0);
 }
    }
    if((ppid = getsid(0)) == -1) {
 perror("getsid()");
 exit(1);
    }
    asprintf(&pidstr, "%d", ppid);
    read_ppid_cmdline(ppid, cmdline, 1000);
    if(child_pid = fork()) {
 if(child_pid < 0) {
     perror("fork");
     exit(1);
 }
    }
    else {
 int rv;
 signal(1, ((void (*)(int)) 1));
 signal(15, (void *)sig_handler);
 signal(2, (void *)sig_handler);
 if((rv = write_pidfile(pidfilename(pidstr))) == 0) {
     printf("authd is running as %d ", getpid());
     printf("in session %d\n", ppid);
     close(0); close(1); close(2);
     child_loop(ppid, cmdline);
     destroy_pidfile();
 } else if(rv > 0) {
     int other_pid;
     other_pid = read_pidfile(pidfilename(((void *)0)));
     fprintf(stderr, "authd appears to already be running ");
     fprintf(stderr, "in this session with pid %d\n", other_pid);
     exit(1);
 } else {
     fprintf(stderr, "Aborting after something unexpected happened.\n");
     exit(1);
 }
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ppid int

variable_types:
cmdline
char [1000]

variable_types: rv int


repo: nmsoccer/XXGame

decompiled_code:
_DWORD *create_mem_poll()
{
_DWORD *v0; // rbx

v0 = malloc(0xC0uLL);
memset(v0, 0, 0xC0uLL);
v0[8] = 1;
v0[16] = 2;
v0[24] = 3;
v0[32] = 4;
v0[40] = 5;
puts("create mem poll success!");
return v0;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rbx

v0 = malloc(0xC0uLL);
memset(v0, 0, 0xC0uLL);
v0[8] = 1;
v0[16] = 2;
v0[24] = 3;
v0[32] = 4;
v0[40] = 5;
puts("create mem poll success!");
return v0;
}

original_code:
xxmem_poll *create_mem_poll(void){
 xxmem_poll *p;
 int i;
 p = (xxmem_poll *)malloc(sizeof(xxmem_poll));
 memset(p , 0 , sizeof(xxmem_poll));
 for(i=1; i<= 5; i++){
  p->all_index[i].index = i;
 }
 printf("create mem poll success!\n");
 return p;
}

canonical_original_code:
struct _xxmem_poll *create_mem_poll(){
 struct _xxmem_poll *p;
 int i;
 p = (struct _xxmem_poll *)malloc(sizeof(xxmem_poll));
 memset(p , 0 , sizeof(xxmem_poll));
 for(i=1; i<= 5; i++){
  p->all_index[i].index = i;
 }
 printf("create mem poll success!\n");
 return p;
}

variable_types: struct _xxmem_poll { struct _xxmem_index { int index; int used_count; struct _xxmem_node { struct _xxmem_node *next; int real_size; void *data; } *used; int free_count; struct _xxmem_node *free; } all_index[-1]; } *

variable_types: i int

user_defined_types: struct _xxmem_poll { struct _xxmem_index all_index[-1]; }

user_defined_types: struct _xxmem_index { int index; int used_count; struct _xxmem_node *used; int free_count; struct _xxmem_node *free; }


decompiled_code:
__int64 __fastcall delete_mem_poll(void *ptr)
{
char *v1; // r12
int v2; // er13
void **v3; // rbx
void **v4; // rbp
void **v5; // rbx
void **v6; // rbp

if ( !ptr )
return 0xFFFFFFFFLL;
v1 = (char *)ptr + 32;
v2 = 1;
while ( !*(_DWORD *)v1 )
{
LABEL_3:
++v2;
v1 += 32;
if ( v2 == 6 )
{
free(ptr);
puts("delete mem poll success!");
return 0LL;
}
}
if ( *(_DWORD *)v1 == v2 )
{
v3 = (void **)*((_QWORD *)v1 + 1);
while ( v3 )
{
v4 = v3;
v3 = (void **)*v3;
free(v4[2]);
free(v4);
}
v5 = (void **)*((_QWORD *)v1 + 3);
while ( v5 )
{
v6 = v5;
v5 = (void **)*v5;
free(v6[2]);
free(v6);
}
goto LABEL_3;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
char *v1; // r12
int v2; // er13
void **v3; // rbx
void **v4; // rbp
void **v5; // rbx
void **v6; // rbp

if ( !ptr )
return 0xFFFFFFFFLL;
v1 = (char *)ptr + 32;
v2 = 1;
while ( !*(_DWORD *)v1 )
{
LABEL_3:
++v2;
v1 += 32;
if ( v2 == 6 )
{
free(ptr);
puts("delete mem poll success!");
return 0LL;
}
}
if ( *(_DWORD *)v1 == v2 )
{
v3 = (void **)*((_QWORD *)v1 + 1);
while ( v3 )
{
v4 = v3;
v3 = (void **)*v3;
free(v4[2]);
free(v4);
}
v5 = (void **)*((_QWORD *)v1 + 3);
while ( v5 )
{
v6 = v5;
v5 = (void **)*v5;
free(v6[2]);
free(v6);
}
goto LABEL_3;
}
return 0xFFFFFFFFLL;
}

original_code:
int delete_mem_poll(xxmem_poll *poll){
 xxmem_node *now = ((void *)0);
 xxmem_node *tmp = ((void *)0);
 int i;
 if(!poll){
  return -1;
 }
 for(i=1; i<=5; i++){
  if(poll->all_index[i].index == 0){
   continue;
  }
  if(poll->all_index[i].index != i){
   return -1;
  }
  now = poll->all_index[i].used;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
  now = poll->all_index[i].free;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
 }
 free(poll);
 printf("delete mem poll success!\n");
 return 0;
}

canonical_original_code:
int delete_mem_poll(struct _xxmem_poll *poll){
 struct _xxmem_node *now = ((void *)0);
 struct _xxmem_node *tmp = ((void *)0);
 int i;
 if(!poll){
  return -1;
 }
 for(i=1; i<=5; i++){
  if(poll->all_index[i].index == 0){
   continue;
  }
  if(poll->all_index[i].index != i){
   return -1;
  }
  now = poll->all_index[i].used;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
  now = poll->all_index[i].free;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
 }
 free(poll);
 printf("delete mem poll success!\n");
 return 0;
}

variable_types: struct _xxmem_node { struct _xxmem_node *next; int real_size; void *data; } *


original_code:
void *xx_alloc_mem(xxmem_poll *poll , int size){
 xxmem_node *now;
 int index = 0;
 if(!poll || size<=0){
  return ((void *)0);
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(index > 5){
  return ((void *)0);
 }
 if(poll->all_index[index].free_count == 0){
  printf("malloc new size: %d\n" , index * 1024);
  now = (xxmem_node *)malloc(sizeof(xxmem_node));
  now->real_size = size;
  now->data = malloc(index * 1024);
  now->next = poll->all_index[index].used;
  poll->all_index[index].used = now;
  poll->all_index[index].used_count++;
  return now->data;
 }
 printf("use free block! \n ");
 now = poll->all_index[index].free;
 now->real_size = size;
 poll->all_index[index].free = now->next;
 poll->all_index[index].free_count--;
 now->next = poll->all_index[index].used;
 poll->all_index[index].used = now;
 poll->all_index[index].used_count++;
 return now->data;
}

canonical_original_code:
void *xx_alloc_mem(struct _xxmem_poll *poll, int size){
 struct _xxmem_node *now;
 int index = 0;
 if(!poll || size<=0){
  return ((void *)0);
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(index > 5){
  return ((void *)0);
 }
 if(poll->all_index[index].free_count == 0){
  printf("malloc new size: %d\n" , index * 1024);
  now = (struct _xxmem_node *)malloc(sizeof(xxmem_node));
  now->real_size = size;
  now->data = malloc(index * 1024);
  now->next = poll->all_index[index].used;
  poll->all_index[index].used = now;
  poll->all_index[index].used_count++;
  return now->data;
 }
 printf("use free block! \n ");
 now = poll->all_index[index].free;
 now->real_size = size;
 poll->all_index[index].free = now->next;
 poll->all_index[index].free_count--;
 now->next = poll->all_index[index].used;
 poll->all_index[index].used = now;
 poll->all_index[index].used_count++;
 return now->data;
}

variable_types: size int

variable_types: index int

user_defined_types: struct _xxmem_node { struct _xxmem_node *next; int real_size; void *data; }


original_code:
int xx_free_mem(xxmem_poll *poll , void *ptr , int size){
 xxmem_node *now = ((void *)0);
 xxmem_node *prev = ((void *)0);
 int index = 0;
 if(!poll || !ptr || size<=0){
  return -1;
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(poll->all_index[index].used->real_size == size && poll->all_index[index].used->data == ptr){
  now = poll->all_index[index].used;
  poll->all_index[index].used = now->next;
  poll->all_index[index].used_count--;
  now->next = poll->all_index[index].free;
  poll->all_index[index].free = now;
  poll->all_index[index].free_count++;
 }else{
  prev = poll->all_index[index].used;
  while(1){
   now = prev->next;
   if(now == ((void *)0)){
    return -1;
   }
   if(now->real_size == size && now->data == ptr){
    prev->next = now->next;
    poll->all_index[index].used_count--;
    now->next = poll->all_index[index].free;
    poll->all_index[index].free = now;
    poll->all_index[index].free_count++;
    break;
   }
   prev = now;
  }
 }
 return 0;
}

canonical_original_code:
int xx_free_mem(struct _xxmem_poll *poll, void *ptr, int size){
 struct _xxmem_node *now = ((void *)0);
 struct _xxmem_node *prev = ((void *)0);
 int index = 0;
 if(!poll || !ptr || size<=0){
  return -1;
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(poll->all_index[index].used->real_size == size && poll->all_index[index].used->data == ptr){
  now = poll->all_index[index].used;
  poll->all_index[index].used = now->next;
  poll->all_index[index].used_count--;
  now->next = poll->all_index[index].free;
  poll->all_index[index].free = now;
  poll->all_index[index].free_count++;
 }else{
  prev = poll->all_index[index].used;
  while(1){
   now = prev->next;
   if(now == ((void *)0)){
    return -1;
   }
   if(now->real_size == size && now->data == ptr){
    prev->next = now->next;
    poll->all_index[index].used_count--;
    now->next = poll->all_index[index].free;
    poll->all_index[index].free = now;
    poll->all_index[index].free_count++;
    break;
   }
   prev = now;
  }
 }
 return 0;
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall get_index_container(int a1, int a2)
{
unsigned int v2; // esi
__int64 result; // rax
int v4; // ecx

v2 = (a1 | a2) & 0x7FFFFF;
result = v2 % 0xFD;
v4 = 253;
while ( !interface_container[2 * (int)result] || dword_668[4 * (int)result] != v2 )
{
result = (unsigned int)(((int)result + 1) % 253);
if ( !--v4 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
unsigned int v2; // esi
__int64 result; // rax
int v4; // ecx

v2 = (a1 | a2) & 0x7FFFFF;
result = v2 % 0xFD;
v4 = 253;
while ( !interface_container[2 * (int)result] || dword_668[4 * (int)result] != v2 )
{
result = (unsigned int)(((int)result + 1) % 253);
if ( !--v4 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int get_index_container(proc_t proc1 , proc_t proc2){
 int index = 0;
 proc_t or_procs = 0;
 int icount = 0;
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   break;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   return -1;
  }
 }
 return index;
}

canonical_original_code:
static int get_index_container(unsigned int proc1, unsigned int proc2){
 int index = 0;
 unsigned int or_procs = 0;
 int icount = 0;
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   break;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   return -1;
  }
 }
 return index;
}

variable_types: icount int


decompiled_code:
void *__fastcall attach_bus(int a1, int a2)
{
int v2; // edi
void *result; // rax

puts("attach bus...");
v2 = shmget(a2 | a1, 0LL, 0);
result = 0LL;
if ( v2 >= 0 )
result = shmat(v2, 0LL, 0);
return result;
}

canonical_decompiled_code:
void *__fastcall func1(int a1, int a2)
{
int v2; // edi
void *result; // rax

puts("attach bus...");
v2 = shmget(a2 | a1, 0LL, 0);
result = 0LL;
if ( v2 >= 0 )
result = shmat(v2, 0LL, 0);
return result;
}

original_code:
static bus_interface *attach_bus(proc_t proc1 , proc_t proc2){
 int ishm_id = -1;
 key_t key;
 bus_interface *pstbus_interface = ((void *)0);
 printf("attach bus...\n");
 key = proc1 | proc2;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  return ((void *)0);
 }
 pstbus_interface = (bus_interface *)shmat(ishm_id , ((void *)0) , 0);
 if(!pstbus_interface){
  return ((void *)0);
 }
 return pstbus_interface;
}

canonical_original_code:
static struct stbus_interface *attach_bus(unsigned int proc1, unsigned int proc2){
 int ishm_id = -1;
 int key;
 struct stbus_interface *pstbus_interface = ((void *)0);
 printf("attach bus...\n");
 key = proc1 | proc2;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  return ((void *)0);
 }
 pstbus_interface = (struct stbus_interface *)shmat(ishm_id , ((void *)0) , 0);
 if(!pstbus_interface){
  return ((void *)0);
 }
 return pstbus_interface;
}

variable_types: key int

variable_types: struct stbus_interface { unsigned int udwproc_id_recv_ch1; struct _bus_channel { struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; } data[40]; unsigned char package_num; unsigned char head; unsigned char tail; unsigned char spin_lock; } channel_one; unsigned int udwproc_id_recv_ch2; struct _bus_channel channel_two; } *

user_defined_types: struct stbus_interface { unsigned int udwproc_id_recv_ch1; struct _bus_channel channel_one; unsigned int udwproc_id_recv_ch2; struct _bus_channel channel_two; }

user_defined_types: struct _bus_channel { struct _sspackage data[40]; unsigned char package_num; unsigned char head; unsigned char tail; unsigned char spin_lock; }

user_defined_types: struct _sspackage { struct _sshead sshead; union _ssdata data; }

user_defined_types: union _ssdata { struct cspackage_t cs_data; }

user_defined_types: struct cspackage_t { struct _cshead cshead; union _csdata data; }

user_defined_types: union _csdata { struct _req_validate_info req_validate_player; struct _reply_validate_info reply_validate_player; char acdata[512]; }


original_code:
static int send_bus(proc_t recv_proc , proc_t send_proc , bus_interface *bus , sspackage_t *package){
 bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if((recv_proc | send_proc) != (bus->udwproc_id_recv_ch1 | bus->udwproc_id_recv_ch2)){
  return -1;
 }
 do{
  if(recv_proc == bus->udwproc_id_recv_ch1){
   channel = &bus->channel_one;
   break;
  }
  if(recv_proc == bus->udwproc_id_recv_ch2){
   channel = &bus->channel_two;
   break;
  }
  return -1;
 }while(0);
 if(channel->package_num == 40){
  return -2;
 }
 memcpy(&channel->data[channel->tail] , package , sizeof(sspackage_t));
 channel->tail = (channel->tail + 1) % 40;
 channel->package_num++;
 return 0;
}

canonical_original_code:
static int send_bus(unsigned int recv_proc, unsigned int send_proc, struct stbus_interface *bus, struct _sspackage *package){
 struct _bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if((recv_proc | send_proc) != (bus->udwproc_id_recv_ch1 | bus->udwproc_id_recv_ch2)){
  return -1;
 }
 do{
  if(recv_proc == bus->udwproc_id_recv_ch1){
   channel = &bus->channel_one;
   break;
  }
  if(recv_proc == bus->udwproc_id_recv_ch2){
   channel = &bus->channel_two;
   break;
  }
  return -1;
 }while(0);
 if(channel->package_num == 40){
  return -2;
 }
 memcpy(&channel->data[channel->tail] , package , sizeof(sspackage_t));
 channel->tail = (channel->tail + 1) % 40;
 channel->package_num++;
 return 0;
}

variable_types:
package
struct _sspackage *

variable_types: struct _bus_channel { struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; } data[40]; unsigned char package_num; unsigned char head; unsigned char tail; unsigned char spin_lock; } *


original_code:
static int recv_bus(u32 recv_proc , u32 send_proc , bus_interface *bus , sspackage_t *package){
 bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if(recv_proc == bus->udwproc_id_recv_ch1){
  if(send_proc != bus->udwproc_id_recv_ch2){
   return -1;
  }
  channel = &bus->channel_one;
 }else if(recv_proc == bus->udwproc_id_recv_ch2){
  if(send_proc != bus->udwproc_id_recv_ch1){
   return -1;
  }
  channel = &bus->channel_two;
 }else{
  return -1;
 }
 if(channel->package_num == 0){
    return -2;
 }
 memcpy(package , &channel->data[channel->head] , sizeof(sspackage_t));
 channel->head = (channel->head + 1) % 40;
 channel->package_num--;
 return 0;
}

canonical_original_code:
static int recv_bus(unsigned int recv_proc, unsigned int send_proc, struct stbus_interface *bus, struct _sspackage *package){
 struct _bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if(recv_proc == bus->udwproc_id_recv_ch1){
  if(send_proc != bus->udwproc_id_recv_ch2){
   return -1;
  }
  channel = &bus->channel_one;
 }else if(recv_proc == bus->udwproc_id_recv_ch2){
  if(send_proc != bus->udwproc_id_recv_ch1){
   return -1;
  }
  channel = &bus->channel_two;
 }else{
  return -1;
 }
 if(channel->package_num == 0){
    return -2;
 }
 memcpy(package , &channel->data[channel->head] , sizeof(sspackage_t));
 channel->head = (channel->head + 1) % 40;
 channel->package_num--;
 return 0;
}

user_defined_types: struct _sshead { unsigned short package_type; int client_fd; }

user_defined_types: struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; }

user_defined_types: struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; }

user_defined_types: struct _reply_validate_info { unsigned char is_validate; }


decompiled_code:
__int64 __fastcall open_bus(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebp
signed int v3; // ebx
int v4; // edx
int v5; // ecx
__int64 result; // rax
void *v7; // rax
unsigned __int64 v8; // rbx

v2 = (a1 | a2) & 0x7FFFFF;
v3 = v2 % 0xFD;
v4 = 253;
while ( 1 )
{
v5 = dword_668[4 * v3];
if ( !v5 && !interface_container[2 * v3] )
break;
if ( interface_container[2 * v3] && v5 == v2 )
{
printf("bus between %d <-> %d is opened already!!\n", a1, a2);
return 0LL;
}
v3 = (v3 + 1) % 253;
if ( !--v4 )
{
write_log(3LL, "open bus some bad error happened!\n");
return 0xFFFFFFFFLL;
}
}
v7 = attach_bus(a1, a2);
if ( v7 )
{
v8 = 4LL * v3;
interface_container[v8 / 2] = (__int64)v7;
dword_668[v8] = v2;
result = 0LL;
}
else
{
puts("open bus failed!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebp
signed int v3; // ebx
int v4; // edx
int v5; // ecx
__int64 result; // rax
void *v7; // rax
unsigned __int64 v8; // rbx

v2 = (a1 | a2) & 0x7FFFFF;
v3 = v2 % 0xFD;
v4 = 253;
while ( 1 )
{
v5 = dword_668[4 * v3];
if ( !v5 && !interface_container[2 * v3] )
break;
if ( interface_container[2 * v3] && v5 == v2 )
{
printf("bus between %d <-> %d is opened already!!\n", a1, a2);
return 0LL;
}
v3 = (v3 + 1) % 253;
if ( !--v4 )
{
write_log(3LL, "open bus some bad error happened!\n");
return 0xFFFFFFFFLL;
}
}
v7 = func1(a1, a2);
if ( v7 )
{
v8 = 4LL * v3;
interface_container[v8 / 2] = (__int64)v7;
dword_668[v8] = v2;
result = 0LL;
}
else
{
puts("open bus failed!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int open_bus(proc_t proc1 , proc_t proc2){
 int index = 0;
 proc_t or_procs = 0;
 bus_interface *interface_of_procs = ((void *)0);
 int icount = 0;
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "open_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].or_two_proc == 0 && interface_container[index].interface == ((void *)0)){
   break;
  }
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   printf("bus between %d <-> %d is opened already!!\n" , proc1 , proc2);
   return 0;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   write_log(3 , "open bus some bad error happened!\n");
   return -1;
  }
 }
 interface_of_procs = attach_bus(proc1 , proc2);
 if(!interface_of_procs){
  printf("open bus failed!\n");
  return -1;
 }
 interface_container[index].interface = interface_of_procs;
 interface_container[index].or_two_proc = or_procs;
 return 0;
}

canonical_original_code:
int open_bus(unsigned int proc1, unsigned int proc2){
 int index = 0;
 unsigned int or_procs = 0;
 struct stbus_interface *interface_of_procs = ((void *)0);
 int icount = 0;
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "open_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].or_two_proc == 0 && interface_container[index].interface == ((void *)0)){
   break;
  }
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   printf("bus between %d <-> %d is opened already!!\n" , proc1 , proc2);
   return 0;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   write_log(3 , "open bus some bad error happened!\n");
   return -1;
  }
 }
 interface_of_procs = attach_bus(proc1 , proc2);
 if(!interface_of_procs){
  printf("open bus failed!\n");
  return -1;
 }
 interface_container[index].interface = interface_of_procs;
 interface_container[index].or_two_proc = or_procs;
 return 0;
}


decompiled_code:
__int64 __fastcall close_bus(int a1, int a2)
{
int v2; // eax
unsigned __int64 v3; // rbx

v2 = get_index_container(a1, a2);
if ( v2 != -1 )
{
v3 = 4LL * v2;
detach_bus((const void *)interface_container[v3 / 2]);
interface_container[v3 / 2] = 0LL;
dword_668[v3] = 0;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2)
{
int v2; // eax
unsigned __int64 v3; // rbx

v2 = func0(a1, a2);
if ( v2 != -1 )
{
v3 = 4LL * v2;
func2((const void *)interface_container[v3 / 2]);
interface_container[v3 / 2] = 0LL;
dword_668[v3] = 0;
}
return 0LL;
}

original_code:
int close_bus(proc_t proc1 , proc_t proc2){
 int index = -1;
 bus_interface *interface_of_procs = ((void *)0);
 if(proc1 & 0xF0000000 != proc2 & 0xF0000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different world" , proc1 , proc2);
  return -1;
 }
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 index = get_index_container(proc1 , proc2);
 if(index == -1){
  return 0;
 }
 interface_of_procs = interface_container[index].interface;
 detach_bus(interface_of_procs);
 interface_container[index].interface = ((void *)0);
 interface_container[index].or_two_proc = 0;
 return 0;
}

canonical_original_code:
int close_bus(unsigned int proc1, unsigned int proc2){
 int index = -1;
 struct stbus_interface *interface_of_procs = ((void *)0);
 if(proc1 & 0xF0000000 != proc2 & 0xF0000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different world" , proc1 , proc2);
  return -1;
 }
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 index = get_index_container(proc1 , proc2);
 if(index == -1){
  return 0;
 }
 interface_of_procs = interface_container[index].interface;
 detach_bus(interface_of_procs);
 interface_container[index].interface = ((void *)0);
 interface_container[index].or_two_proc = 0;
 return 0;
}


decompiled_code:
__int64 __fastcall send_bus_pkg(int a1, int a2, _QWORD *a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx

v4 = get_index_container(a1, a2);
v5 = v4;
if ( v4 != -1 )
return (unsigned int)send_bus(a1, a2, (_DWORD *)interface_container[2 * (int)v4], a3);
write_log(3LL, "send_bus_pkg:get_index_container index = -1");
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2, _QWORD *a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx

v4 = func0(a1, a2);
v5 = v4;
if ( v4 != -1 )
return (unsigned int)func3(a1, a2, (_DWORD *)interface_container[2 * (int)v4], a3);
write_log(3LL, "send_bus_pkg:get_index_container index = -1");
return v5;
}

original_code:
int send_bus_pkg(proc_t recv_proc , proc_t send_proc , sspackage_t *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "send_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = send_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}

canonical_original_code:
int send_bus_pkg(unsigned int recv_proc, unsigned int send_proc, struct _sspackage *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "send_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = send_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}

variable_types: struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; } *

variable_types: iret int


decompiled_code:
__int64 __fastcall get_bus_pkg(int a1, int a2, _QWORD *a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx

v4 = get_index_container(a1, a2);
v5 = v4;
if ( v4 != -1 )
return (unsigned int)recv_bus(a1, a2, (_DWORD *)interface_container[2 * (int)v4], a3);
write_log(3LL, "recv_bus_pkg:get_index_container index = -1");
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, _QWORD *a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx

v4 = func0(a1, a2);
v5 = v4;
if ( v4 != -1 )
return (unsigned int)func4(a1, a2, (_DWORD *)interface_container[2 * (int)v4], a3);
write_log(3LL, "recv_bus_pkg:get_index_container index = -1");
return v5;
}

original_code:
int get_bus_pkg(u32 recv_proc , u32 send_proc , sspackage_t *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "recv_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = recv_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}

canonical_original_code:
int get_bus_pkg(unsigned int recv_proc, unsigned int send_proc, struct _sspackage *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "recv_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = recv_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}


decompiled_code:
void *__fastcall attach_shm_res(int a1, int a2, int a3)
{
unsigned int v3; // ebx
int v4; // edi
void *v5; // rbp

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return 0LL;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "attach_shm_res: shmget shm res %x failed!", v3);
v5 = 0LL;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( !v5 )
write_log(3LL, "attach_shm_res: shmat shm res %x failed!", v3);
}
return v5;
}

canonical_decompiled_code:
void *__fastcall func9(int a1, int a2, int a3)
{
unsigned int v3; // ebx
int v4; // edi
void *v5; // rbp

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return 0LL;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "attach_shm_res: shmget shm res %x failed!", v3);
v5 = 0LL;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( !v5 )
write_log(3LL, "attach_shm_res: shmat shm res %x failed!", v3);
}
return v5;
}

original_code:
void *attach_shm_res(int res_type , int world_id , int line_id){
 void *addr;
 int ishm_id;
 key_t key;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return ((void *)0);
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "attach_shm_res: shmget shm res %x failed!" , key);
  return ((void *)0);
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "attach_shm_res: shmat shm res %x failed!" , key);
  return ((void *)0);
 }
 return addr;
}

canonical_original_code:
void *attach_shm_res(int res_type, int world_id, int line_id){
 void *addr;
 int ishm_id;
 int key;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return ((void *)0);
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "attach_shm_res: shmget shm res %x failed!" , key);
  return ((void *)0);
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "attach_shm_res: shmat shm res %x failed!" , key);
  return ((void *)0);
 }
 return addr;
}

variable_types:
addr
void *


decompiled_code:
__int64 __fastcall detach_shm_res(int a1, int a2, int a3)
{
unsigned int v3; // ebx
int v4; // edi
void *v5; // rdi
unsigned int v6; // ebp

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return (unsigned int)-1;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "detach_shm_res: shmget shm res %x failed!", v3);
v6 = -1;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( v5 )
{
v6 = shmdt(v5);
if ( v6 == -1 )
write_log(3LL, "detach_shm_res: shmdt shm res %x failed!", v3);
}
else
{
write_log(3LL, "detach_shm_res: shmat shm res %x failed!", v3);
v6 = -1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2, int a3)
{
unsigned int v3; // ebx
int v4; // edi
void *v5; // rdi
unsigned int v6; // ebp

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return (unsigned int)-1;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "detach_shm_res: shmget shm res %x failed!", v3);
v6 = -1;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( v5 )
{
v6 = shmdt(v5);
if ( v6 == -1 )
write_log(3LL, "detach_shm_res: shmdt shm res %x failed!", v3);
}
else
{
write_log(3LL, "detach_shm_res: shmat shm res %x failed!", v3);
v6 = -1;
}
}
return v6;
}

original_code:
int detach_shm_res(int res_type , int world_id , int line_id){
 int ishm_id;
 key_t key;
 void *addr;
 int iret;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return -1;
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "detach_shm_res: shmget shm res %x failed!" , key);
  return -1;
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "detach_shm_res: shmat shm res %x failed!" , key);
  return -1;
 }
 iret = shmdt(addr);
 if(iret == -1){
  write_log(3 , "detach_shm_res: shmdt shm res %x failed!" , key);
  return -1;
 }
 return iret;
}

canonical_original_code:
int detach_shm_res(int res_type, int world_id, int line_id){
 int ishm_id;
 int key;
 void *addr;
 int iret;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return -1;
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "detach_shm_res: shmget shm res %x failed!" , key);
  return -1;
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "detach_shm_res: shmat shm res %x failed!" , key);
  return -1;
 }
 iret = shmdt(addr);
 if(iret == -1){
  write_log(3 , "detach_shm_res: shmdt shm res %x failed!" , key);
  return -1;
 }
 return iret;
}


decompiled_code:
ssize_t __fastcall read_socket(int a1, void *a2)
{
ssize_t result; // rax

if ( a1 >= 0 && a2 )
result = recv(a1, a2, 0x20CuLL, 0);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(int a1, void *a2)
{
ssize_t result; // rax

if ( a1 >= 0 && a2 )
result = recv(a1, a2, 0x20CuLL, 0);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int read_socket(int isock_fd , cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return recv(isock_fd , cspackage , sizeof(cspackage_t) , 0);
}

canonical_original_code:
static int read_socket(int isock_fd, struct cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return recv(isock_fd , cspackage , sizeof(cspackage_t) , 0);
}

variable_types: struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } *


decompiled_code:
ssize_t __fastcall write_socket(int a1, const void *a2)
{
ssize_t result; // rax

if ( a1 >= 0 && a2 )
result = send(a1, a2, 0x20CuLL, 0);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func4(int a1, const void *a2)
{
ssize_t result; // rax

if ( a1 >= 0 && a2 )
result = send(a1, a2, 0x20CuLL, 0);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int write_socket(int isock_fd , cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return send(isock_fd , cspackage, sizeof(cspackage_t) , 0);
}

canonical_original_code:
static int write_socket(int isock_fd, struct cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return send(isock_fd , cspackage, sizeof(cspackage_t) , 0);
}


decompiled_code:
void __fastcall handle_signal(__int64 a1)
{
if ( (_DWORD)a1 == 2 )
{
write_log(a1, "receive SIGINT,ready to exit...");
if ( (int)close_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
write_log(3LL, "connect server: detach bus to connect failed!");
write_log(2LL, "connect server: detach bus to connect success!");
if ( (int)delete_mem_poll(poll) < 0 )
write_log(3LL, "connect server: delete_mem_poll failed!");
write_log(2LL, "connect server: delete_mem_poll success!");
if ( (int)detach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id) < 0 )
write_log(3LL, "connect server: detach runtime env connect failed!");
write_log(2LL, "connect server: detach runtime env success!");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
if ( (_DWORD)a1 == 2 )
{
write_log(a1, "receive SIGINT,ready to exit...");
if ( (int)close_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
write_log(3LL, "connect server: detach bus to connect failed!");
write_log(2LL, "connect server: detach bus to connect success!");
if ( (int)delete_mem_poll(poll) < 0 )
write_log(3LL, "connect server: delete_mem_poll failed!");
write_log(2LL, "connect server: delete_mem_poll success!");
if ( (int)detach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id) < 0 )
write_log(3LL, "connect server: detach runtime env connect failed!");
write_log(2LL, "connect server: detach runtime env success!");
exit(0);
}
}

original_code:
static void handle_signal(int sig_no){
 int iret;
 switch(sig_no){
  case 2:
   write_log(2 , "receive SIGINT,ready to exit...");
   iret = close_bus(connect_server_id , logic_server_id);
   if(iret < 0){
    write_log(3 , "connect server: detach bus to connect failed!");
   }
   write_log(2 , "connect server: detach bus to connect success!");
   iret = delete_mem_poll(poll);
   if(iret < 0){
    write_log(3 , "connect server: delete_mem_poll failed!");
   }
   write_log(2 , "connect server: delete_mem_poll success!");
   iret = detach_shm_res(0x00000001 , world_id , line_id);
   if(iret < 0){
    write_log(3 , "connect server: detach runtime env connect failed!");
   }
   write_log(2 , "connect server: detach runtime env success!");
   exit(0);
  break;
  default:
  break;
 }
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
unsigned int v4; // eax
int v6; // er12
__int64 v7; // rcx
__sigset_t *v8; // rdi
__int64 v9; // rax
__int64 v10; // rbp
unsigned int v11; // er15
__int64 v12; // rbp
int v13; // eax
epoll_data_t *v14; // rbx
unsigned int v15; // ebp
int v16; // eax
int v17; // eax
int v18; // eax
int v19; // eax
epoll_data_t *v20; // r13
int epfd; // [rsp+Ch] [rbp-338Ch]
struct sigaction v22; // [rsp+10h] [rbp-3388h] BYREF
int v23[136]; // [rsp+B0h] [rbp-32E8h] BYREF
struct epoll_event events; // [rsp+2D0h] [rbp-30C8h] BYREF
_DWORD v25[3072]; // [rsp+2E0h] [rbp-30B8h] BYREF
struct epoll_event event; // [rsp+32E0h] [rbp-B8h] BYREF
socklen_t addr_len; // [rsp+32ECh] [rbp-ACh] BYREF
struct sockaddr v28; // [rsp+32F0h] [rbp-A8h] BYREF
struct sockaddr addr; // [rsp+3300h] [rbp-98h] BYREF
char nptr[8]; // [rsp+3310h] [rbp-88h] BYREF
char v31[128]; // [rsp+3318h] [rbp-80h] BYREF

addr_len = 0;
if ( argc <= 1 )
{
write_log(3LL, "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx", envp);
}
else if ( (int)strsplit(argv[1], 46LL, nptr, 10LL, 8LL) <= 1 )
{
write_log(3LL, "connect_server:illegal argument 1:%s , exit!", argv[1]);
}
else
{
v3 = atoi(nptr);
world_id = v3;
v4 = atoi(v31);
line_id = v4;
if ( v3 - 1 <= 0xE && v4 - 1 <= 0xE )
{
printf("world_id:%d , line_id:%d\n", v3, v4);
connect_server_id = (world_id << 28) | (line_id << 24) & 0xF000000 | 1;
logic_server_id = (world_id << 28) | (line_id << 24) & 0xF000000 | 2;
v6 = socket(2, 1, 0);
if ( v6 < 0 )
{
write_log(3LL, "connect_server:create connect server socket failed!");
}
else
{
addr_len = 16;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(8005);
if ( bind(v6, &addr, 0x10u) < 0 )
{
write_log(3LL, "connect_server:bind connect socket failed!");
}
else
{
epfd = epoll_create(1025);
if ( epfd < 0 )
{
write_log(3LL, "connect_server:epoll create failed!");
}
else
{
event.events = 1;
event.data.fd = v6;
if ( epoll_ctl(epfd, 1, v6, &event) < 0 )
{
write_log(3LL, "connect_server: append serve fd to epoll failed!");
}
else
{
listen(v6, 1024);
memset(&client, 0, 0xB008uLL);
v7 = 36LL;
v8 = &v22.sa_mask;
while ( v7 )
{
LODWORD(v8->__val[0]) = 0;
v8 = (__sigset_t *)((char *)v8 + 4);
--v7;
}
v22.sa_handler = (__sighandler_t)handle_signal;
sigaction(2, &v22, 0LL);
poll = create_mem_poll();
if ( poll )
{
write_log(1LL, "connect_server:create mem poll success!");
if ( (int)open_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
{
write_log(3LL, "connect_server:open bus connect <-> logic failed!");
}
else
{
v9 = attach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id);
pruntime_env = v9;
if ( v9 )
{
write_log(1LL, "connect_server:attach runtime env %x success!", *(unsigned int *)(v9 + 4));
v10 = pruntime_env + 72LL * (int)index_last_1bit(1LL);
*(_DWORD *)(v10 + 8) = connect_server_id;
*(_DWORD *)(v10 + 12) = getpid();
write_log(1LL, "start connect_server %s success!", argv[1]);
v11 = 0;
while ( 1 )
{
++v11;
v19 = epoll_wait(epfd, &events, 1025, 200);
if ( v19 < 0 )
break;
if ( v19 > 0 )
{
v14 = &events.data;
v20 = (epoll_data_t *)&v25[3 * (v19 - 1)];
do
{
v15 = v14->fd;
if ( v14->fd == v6 )
{
*(_QWORD *)&v28.sa_family = 0LL;
*(_QWORD *)&v28.sa_data[6] = 0LL;
v12 = (unsigned int)accept(v6, &v28, &addr_len);
write_log(1LL, "connect_server:accept a new client: %d\n", v12);
set_nonblock((unsigned int)v12);
set_sock_buff_size((unsigned int)v12, 5240LL, 2620LL);
v13 = client;
*((_DWORD *)&client + (int)client + 1) = v12;
LODWORD(client) = v13 + 1;
event.data.fd = v12;
event.events = -2147475455;
if ( epoll_ctl(epfd, 1, v12, &event) < 0 )
write_log(3LL, "connect_server:epoll_ctl %d failed!", (unsigned int)v12);
}
else
{
v16 = HIDWORD(v14[-1].u64);
if ( (v16 & 0x2000) != 0 )
{
write_log(1LL, "connect_server:client %d closed!", v15);
close_client(v14->fd);
}
else if ( (v16 & 1) != 0 )
{
memset(v23, 0, 0x210uLL);
v23[132] = 0;
LOWORD(v23[0]) = 1;
v23[1] = v15;
v17 = read_socket(v15, &v23[2]);
if ( v17 == 524 )
{
puts(">>>read package!");
}
else if ( v17 <= 0 )
{
write_log(1LL, "connect server: nothing to read!");
}
else
{
write_log(
3LL,
"connect server: read pakcage bytes len %d < cspackage size %d!",
(unsigned int)v17,
524LL);
}
if ( ((unsigned int)v23[2] >> (LOBYTE(v23[4]) + 1))
+ BYTE1(v23[4]) % (LOWORD(v23[3]) + 255) == HIWORD(v23[3]) )
{
v18 = send_bus_pkg((unsigned int)logic_server_id, (unsigned int)connect_server_id, v23);
if ( v18 )
{
if ( v18 == -2 )
{
write_log(3LL, "connect server: send bus to logic full!");
}
else if ( v18 == -1 )
{
write_log(3LL, "connect server: send to logic failed!");
}
}
send_to_client(v15);
}
else
{
write_log(
1LL,
"connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d",
v15,
(unsigned int)v23[2],
HIWORD(v23[3]));
}
}
}
v14 = (epoll_data_t *)((char *)v14 + 12);
}
while ( v14 != v20 );
}
try_handle_buses();
if ( !(v11 % 5) )
send_to_all_clients();
}
write_log(3LL, "connect_server:epoll wait error!");
}
else
{
write_log(3LL, "connect_server:attach runtime env failed!");
}
}
}
else
{
write_log(3LL, "connect_server:create mem poll failed!");
}
}
}
}
}
}
else
{
write_log(3LL, "connect_server:illegal argument1:%s , exit!", argv[1]);
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
unsigned int v4; // eax
int v6; // er12
__int64 v7; // rcx
__sigset_t *v8; // rdi
__int64 v9; // rax
__int64 v10; // rbp
unsigned int v11; // er15
__int64 v12; // rbp
int v13; // eax
epoll_data_t *v14; // rbx
unsigned int v15; // ebp
int v16; // eax
int v17; // eax
int v18; // eax
int v19; // eax
epoll_data_t *v20; // r13
int epfd; // [rsp+Ch] [rbp-338Ch]
struct sigaction v22; // [rsp+10h] [rbp-3388h] BYREF
int v23[136]; // [rsp+B0h] [rbp-32E8h] BYREF
struct epoll_event events; // [rsp+2D0h] [rbp-30C8h] BYREF
_DWORD v25[3072]; // [rsp+2E0h] [rbp-30B8h] BYREF
struct epoll_event event; // [rsp+32E0h] [rbp-B8h] BYREF
socklen_t addr_len; // [rsp+32ECh] [rbp-ACh] BYREF
struct sockaddr v28; // [rsp+32F0h] [rbp-A8h] BYREF
struct sockaddr addr; // [rsp+3300h] [rbp-98h] BYREF
char nptr[8]; // [rsp+3310h] [rbp-88h] BYREF
char v31[128]; // [rsp+3318h] [rbp-80h] BYREF

addr_len = 0;
if ( argc <= 1 )
{
write_log(3LL, "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx", envp);
}
else if ( (int)strsplit(argv[1], 46LL, nptr, 10LL, 8LL) <= 1 )
{
write_log(3LL, "connect_server:illegal argument 1:%s , exit!", argv[1]);
}
else
{
v3 = atoi(nptr);
world_id = v3;
v4 = atoi(v31);
line_id = v4;
if ( v3 - 1 <= 0xE && v4 - 1 <= 0xE )
{
printf("world_id:%d , line_id:%d\n", v3, v4);
connect_server_id = (world_id << 28) | (line_id << 24) & 0xF000000 | 1;
logic_server_id = (world_id << 28) | (line_id << 24) & 0xF000000 | 2;
v6 = socket(2, 1, 0);
if ( v6 < 0 )
{
write_log(3LL, "connect_server:create connect server socket failed!");
}
else
{
addr_len = 16;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func1(8005);
if ( bind(v6, &addr, 0x10u) < 0 )
{
write_log(3LL, "connect_server:bind connect socket failed!");
}
else
{
epfd = epoll_create(1025);
if ( epfd < 0 )
{
write_log(3LL, "connect_server:epoll create failed!");
}
else
{
event.events = 1;
event.data.fd = v6;
if ( epoll_ctl(epfd, 1, v6, &event) < 0 )
{
write_log(3LL, "connect_server: append serve fd to epoll failed!");
}
else
{
listen(v6, 1024);
memset(&client, 0, 0xB008uLL);
v7 = 36LL;
v8 = &v22.sa_mask;
while ( v7 )
{
LODWORD(v8->__val[0]) = 0;
v8 = (__sigset_t *)((char *)v8 + 4);
--v7;
}
v22.sa_handler = (__sighandler_t)handle_signal;
sigaction(2, &v22, 0LL);
poll = create_mem_poll();
if ( poll )
{
write_log(1LL, "connect_server:create mem poll success!");
if ( (int)open_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
{
write_log(3LL, "connect_server:open bus connect <-> logic failed!");
}
else
{
v9 = attach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id);
pruntime_env = v9;
if ( v9 )
{
write_log(1LL, "connect_server:attach runtime env %x success!", *(unsigned int *)(v9 + 4));
v10 = pruntime_env + 72LL * (int)index_last_1bit(1LL);
*(_DWORD *)(v10 + 8) = connect_server_id;
*(_DWORD *)(v10 + 12) = getpid();
write_log(1LL, "start connect_server %s success!", argv[1]);
v11 = 0;
while ( 1 )
{
++v11;
v19 = epoll_wait(epfd, &events, 1025, 200);
if ( v19 < 0 )
break;
if ( v19 > 0 )
{
v14 = &events.data;
v20 = (epoll_data_t *)&v25[3 * (v19 - 1)];
do
{
v15 = v14->fd;
if ( v14->fd == v6 )
{
*(_QWORD *)&v28.sa_family = 0LL;
*(_QWORD *)&v28.sa_data[6] = 0LL;
v12 = (unsigned int)accept(v6, &v28, &addr_len);
write_log(1LL, "connect_server:accept a new client: %d\n", v12);
set_nonblock((unsigned int)v12);
set_sock_buff_size((unsigned int)v12, 5240LL, 2620LL);
v13 = client;
*((_DWORD *)&client + (int)client + 1) = v12;
LODWORD(client) = v13 + 1;
event.data.fd = v12;
event.events = -2147475455;
if ( epoll_ctl(epfd, 1, v12, &event) < 0 )
write_log(3LL, "connect_server:epoll_ctl %d failed!", (unsigned int)v12);
}
else
{
v16 = HIDWORD(v14[-1].u64);
if ( (v16 & 0x2000) != 0 )
{
write_log(1LL, "connect_server:client %d closed!", v15);
func2(v14->fd);
}
else if ( (v16 & 1) != 0 )
{
memset(v23, 0, 0x210uLL);
v23[132] = 0;
LOWORD(v23[0]) = 1;
v23[1] = v15;
v17 = func3(v15, &v23[2]);
if ( v17 == 524 )
{
puts(">>>read package!");
}
else if ( v17 <= 0 )
{
write_log(1LL, "connect server: nothing to read!");
}
else
{
write_log(
3LL,
"connect server: read pakcage bytes len %d < cspackage size %d!",
(unsigned int)v17,
524LL);
}
if ( ((unsigned int)v23[2] >> (LOBYTE(v23[4]) + 1))
+ BYTE1(v23[4]) % (LOWORD(v23[3]) + 255) == HIWORD(v23[3]) )
{
v18 = send_bus_pkg((unsigned int)logic_server_id, (unsigned int)connect_server_id, v23);
if ( v18 )
{
if ( v18 == -2 )
{
write_log(3LL, "connect server: send bus to logic full!");
}
else if ( v18 == -1 )
{
write_log(3LL, "connect server: send to logic failed!");
}
}
func5(v15);
}
else
{
write_log(
1LL,
"connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d",
v15,
(unsigned int)v23[2],
HIWORD(v23[3]));
}
}
}
v14 = (epoll_data_t *)((char *)v14 + 12);
}
while ( v14 != v20 );
}
func7();
if ( !(v11 % 5) )
func6();
}
write_log(3LL, "connect_server:epoll wait error!");
}
else
{
write_log(3LL, "connect_server:attach runtime env failed!");
}
}
}
else
{
write_log(3LL, "connect_server:create mem poll failed!");
}
}
}
}
}
}
else
{
write_log(3LL, "connect_server:illegal argument1:%s , exit!", argv[1]);
}
}
return -1;
}

original_code:
int main(int argc , char **argv){
 int iserv_fd;
 int iaccept_fd;
 int iepoll_fd;
 int iactive_fds;
 int handle_fd;
 char arg_segs[10][8];
 int icount;
 int index = 0;
 struct sockaddr_in stserv_addr;
 struct sockaddr_in stcli_addr;
 int iaddr_len = 0;
 struct epoll_event stepoll_event;
 struct epoll_event astepoll_events[1024 + 1];
 sspackage_t sspackage_recv;
 struct sigaction stsig_act;
 int iRet = -1;
 int i;
 u32 ticks = 0;
 if(argc < 2){
  write_log(3 , "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx");
  return -1;
 }
 icount = strsplit(argv[1] , '.' , arg_segs , 10 , 8);
 if(icount < 2){
  write_log(3 , "connect_server:illegal argument 1:%s , exit!" , argv[1]);
  return -1;
 }
 world_id = atoi(arg_segs[0]);
 line_id = atoi(arg_segs[1]);
 if(world_id<=0 || world_id>15 || line_id<=0 || line_id>15){
  write_log(3 , "connect_server:illegal argument1:%s , exit!" , argv[1]);
  return -1;
 }
 printf("world_id:%d , line_id:%d\n" , world_id , line_id);
 connect_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000001;
 logic_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000002;
 log_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000004;
 iserv_fd = socket(2 , SOCK_STREAM , 0 );
 if(iserv_fd < 0){
  write_log(3 , "connect_server:create connect server socket failed!");
  return -1;
 }
 iaddr_len = sizeof(struct sockaddr_in);
 memset(&stserv_addr , 0 , sizeof(stserv_addr));
 stserv_addr.sin_family = 2;
 stserv_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 stserv_addr.sin_port = htons(8005);
 iRet = bind(iserv_fd , (struct sockaddr *)&stserv_addr , iaddr_len);
 if(iRet < 0){
  write_log(3 , "connect_server:bind connect socket failed!");
  return -1;
 }
 iepoll_fd = epoll_create(1024 + 1);
 if(iepoll_fd < 0){
  write_log(3 , "connect_server:epoll create failed!");
  return -1;
 }
 stepoll_event.events = EPOLLIN;
 stepoll_event.data.fd = iserv_fd;
 iRet = epoll_ctl(iepoll_fd , 1 , iserv_fd , &stepoll_event);
 if(iRet < 0){
  write_log(3 , "connect_server: append serve fd to epoll failed!");
  return -1;
 }
 listen(iserv_fd , 1024);
 memset(&client , 0 , sizeof(CLIENT));
 memset(&stsig_act , 0 , sizeof(stsig_act));
 stsig_act.__sigaction_handler.sa_handler = handle_signal;
 sigaction(2 , &stsig_act , ((void *)0));
 poll = create_mem_poll();
 if(!poll){
  write_log(3 , "connect_server:create mem poll failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:create mem poll success!");
 }
 iRet = open_bus(connect_server_id , logic_server_id);
 if(iRet < 0){
  write_log(3 , "connect_server:open bus connect <-> logic failed!");
  return -1;
 }
 pruntime_env = (runtime_env_t *)attach_shm_res(0x00000001 , world_id , line_id);
 if(!pruntime_env){
  write_log(3 , "connect_server:attach runtime env failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:attach runtime env %x success!" , pruntime_env->basic.global_id);
 }
 index = index_last_1bit(0x00000001);
 pruntime_env->basic.proc_info[index].global_id = connect_server_id;
 pruntime_env->basic.proc_info[index].pid = getpid();
 write_log(1 , "start connect_server %s success!" , argv[1]);
 while(1){
  ticks++;
  iactive_fds = epoll_wait(iepoll_fd , astepoll_events , 1024 +1 , 200);
  if(iactive_fds < 0){
   write_log(3 , "connect_server:epoll wait error!");
   return -1;
  }
  for(i=0; i<iactive_fds; i++){
   if(astepoll_events[i].data.fd == iserv_fd){
    memset(&stcli_addr , 0 , sizeof(stcli_addr));
    iaccept_fd = accept(iserv_fd , (struct sockaddr *)&stcli_addr , &iaddr_len);
    write_log(1 , "connect_server:accept a new client: %d\n" , iaccept_fd);
    set_nonblock(iaccept_fd);
    set_sock_buff_size(iaccept_fd , 10 * sizeof(cspackage_t) , 5 * sizeof(cspackage_t));
    client.active_fds[client.max_active_fd] = iaccept_fd;
    client.max_active_fd++;
    stepoll_event.data.fd = iaccept_fd;
    stepoll_event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
    iRet = epoll_ctl(iepoll_fd , 1 , iaccept_fd , &stepoll_event);
    if(iRet < 0){
     write_log(3 , "connect_server:epoll_ctl %d failed!" , iaccept_fd);
    }
    continue;
   }
   if(astepoll_events[i].events & EPOLLRDHUP){
    write_log(1 , "connect_server:client %d closed!" , astepoll_events[i].data.fd);
    close_client(astepoll_events[i].data.fd);
    continue;
   }
   if(astepoll_events[i].events & EPOLLIN){
    handle_fd = astepoll_events[i].data.fd;
    memset(&sspackage_recv , 0 , sizeof(sspackage_t));
    sspackage_recv.sshead.package_type = 1;
    sspackage_recv.sshead.client_fd = handle_fd;
    iRet = read_socket(handle_fd , &sspackage_recv.data.cs_data);
    if(iRet == sizeof(cspackage_t)){
     printf(">>>read package!\n");
    }
    else
    if(iRet > 0){
     write_log(3 , "connect server: read pakcage bytes len %d < cspackage size %d!" , iRet , sizeof(cspackage_t));
    }
    else{
     write_log(1 , "connect server: nothing to read!");
    }
    if(((sspackage_recv.data.cs_data.cshead.seq >> sspackage_recv.data.cs_data.cshead.major_version+1) + sspackage_recv.data.cs_data.cshead.minor_version % (sspackage_recv.data.cs_data.cshead.proto_type + 255) ) != sspackage_recv.data.cs_data.cshead.validate_seq){
     write_log(1 , "connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d" , handle_fd ,
       sspackage_recv.data.cs_data.cshead.seq , sspackage_recv.data.cs_data.cshead.validate_seq);
     continue;
    }
    iRet = send_bus_pkg(logic_server_id , connect_server_id , &sspackage_recv);
    if(iRet == 0){
    }
    else
    if(iRet == -2){
     write_log(3 , "connect server: send bus to logic full!");
    }
    else
    if(iRet == -1){
     write_log(3 , "connect server: send to logic failed!");
    }
    send_to_client(handle_fd);
   }
  }
  try_handle_buses();
  if(ticks % 5 == 0){
   send_to_all_clients();
  }
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv){
 int iserv_fd;
 int iaccept_fd;
 int iepoll_fd;
 int iactive_fds;
 int handle_fd;
 char arg_segs[10][8];
 int icount;
 int index = 0;
 struct sockaddr_in stserv_addr;
 struct sockaddr_in stcli_addr;
 int iaddr_len = 0;
 struct epoll_event stepoll_event;
 struct epoll_event astepoll_events[-1];
 struct _sspackage sspackage_recv;
 struct sigaction stsig_act;
 int iRet = -1;
 int i;
 unsigned int ticks = 0;
 if(argc < 2){
  write_log(3 , "connect_server:argc < 2 , please input more information worldid.lineid.xxxx , like:1.1.xxxx");
  return -1;
 }
 icount = strsplit(argv[1] , '.' , arg_segs , 10 , 8);
 if(icount < 2){
  write_log(3 , "connect_server:illegal argument 1:%s , exit!" , argv[1]);
  return -1;
 }
 world_id = atoi(arg_segs[0]);
 line_id = atoi(arg_segs[1]);
 if(world_id<=0 || world_id>15 || line_id<=0 || line_id>15){
  write_log(3 , "connect_server:illegal argument1:%s , exit!" , argv[1]);
  return -1;
 }
 printf("world_id:%d , line_id:%d\n" , world_id , line_id);
 connect_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000001;
 logic_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000002;
 log_server_id = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00000000 | 0x00000004;
 iserv_fd = socket(2 , SOCK_STREAM , 0 );
 if(iserv_fd < 0){
  write_log(3 , "connect_server:create connect server socket failed!");
  return -1;
 }
 iaddr_len = sizeof(struct sockaddr_in);
 memset(&stserv_addr , 0 , sizeof(stserv_addr));
 stserv_addr.sin_family = 2;
 stserv_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
 stserv_addr.sin_port = htons(8005);
 iRet = bind(iserv_fd , (struct sockaddr *)&stserv_addr , iaddr_len);
 if(iRet < 0){
  write_log(3 , "connect_server:bind connect socket failed!");
  return -1;
 }
 iepoll_fd = epoll_create(1024 + 1);
 if(iepoll_fd < 0){
  write_log(3 , "connect_server:epoll create failed!");
  return -1;
 }
 stepoll_event.events = EPOLLIN;
 stepoll_event.data.fd = iserv_fd;
 iRet = epoll_ctl(iepoll_fd , 1 , iserv_fd , &stepoll_event);
 if(iRet < 0){
  write_log(3 , "connect_server: append serve fd to epoll failed!");
  return -1;
 }
 listen(iserv_fd , 1024);
 memset(&client , 0 , sizeof(CLIENT));
 memset(&stsig_act , 0 , sizeof(stsig_act));
 stsig_act.__sigaction_handler.sa_handler = handle_signal;
 sigaction(2 , &stsig_act , ((void *)0));
 poll = create_mem_poll();
 if(!poll){
  write_log(3 , "connect_server:create mem poll failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:create mem poll success!");
 }
 iRet = open_bus(connect_server_id , logic_server_id);
 if(iRet < 0){
  write_log(3 , "connect_server:open bus connect <-> logic failed!");
  return -1;
 }
 pruntime_env = (struct _runtime_env *)attach_shm_res(0x00000001 , world_id , line_id);
 if(!pruntime_env){
  write_log(3 , "connect_server:attach runtime env failed!");
  return -1;
 }else{
  write_log(1 , "connect_server:attach runtime env %x success!" , pruntime_env->basic.global_id);
 }
 index = index_last_1bit(0x00000001);
 pruntime_env->basic.proc_info[index].global_id = connect_server_id;
 pruntime_env->basic.proc_info[index].pid = getpid();
 write_log(1 , "start connect_server %s success!" , argv[1]);
 while(1){
  ticks++;
  iactive_fds = epoll_wait(iepoll_fd , astepoll_events , 1024 +1 , 200);
  if(iactive_fds < 0){
   write_log(3 , "connect_server:epoll wait error!");
   return -1;
  }
  for(i=0; i<iactive_fds; i++){
   if(astepoll_events[i].data.fd == iserv_fd){
    memset(&stcli_addr , 0 , sizeof(stcli_addr));
    iaccept_fd = accept(iserv_fd , (struct sockaddr *)&stcli_addr , &iaddr_len);
    write_log(1 , "connect_server:accept a new client: %d\n" , iaccept_fd);
    set_nonblock(iaccept_fd);
    set_sock_buff_size(iaccept_fd , 10 * sizeof(cspackage_t) , 5 * sizeof(cspackage_t));
    client.active_fds[client.max_active_fd] = iaccept_fd;
    client.max_active_fd++;
    stepoll_event.data.fd = iaccept_fd;
    stepoll_event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
    iRet = epoll_ctl(iepoll_fd , 1 , iaccept_fd , &stepoll_event);
    if(iRet < 0){
     write_log(3 , "connect_server:epoll_ctl %d failed!" , iaccept_fd);
    }
    continue;
   }
   if(astepoll_events[i].events & EPOLLRDHUP){
    write_log(1 , "connect_server:client %d closed!" , astepoll_events[i].data.fd);
    close_client(astepoll_events[i].data.fd);
    continue;
   }
   if(astepoll_events[i].events & EPOLLIN){
    handle_fd = astepoll_events[i].data.fd;
    memset(&sspackage_recv , 0 , sizeof(sspackage_t));
    sspackage_recv.sshead.package_type = 1;
    sspackage_recv.sshead.client_fd = handle_fd;
    iRet = read_socket(handle_fd , &sspackage_recv.data.cs_data);
    if(iRet == sizeof(cspackage_t)){
     printf(">>>read package!\n");
    }
    else
    if(iRet > 0){
     write_log(3 , "connect server: read pakcage bytes len %d < cspackage size %d!" , iRet , sizeof(cspackage_t));
    }
    else{
     write_log(1 , "connect server: nothing to read!");
    }
    if(((sspackage_recv.data.cs_data.cshead.seq >> sspackage_recv.data.cs_data.cshead.major_version+1) + sspackage_recv.data.cs_data.cshead.minor_version % (sspackage_recv.data.cs_data.cshead.proto_type + 255) ) != sspackage_recv.data.cs_data.cshead.validate_seq){
     write_log(1 , "connect_server:recv client %d, no validate seq package: seq:%d vs vali_seq:%d" , handle_fd ,
       sspackage_recv.data.cs_data.cshead.seq , sspackage_recv.data.cs_data.cshead.validate_seq);
     continue;
    }
    iRet = send_bus_pkg(logic_server_id , connect_server_id , &sspackage_recv);
    if(iRet == 0){
    }
    else
    if(iRet == -2){
     write_log(3 , "connect server: send bus to logic full!");
    }
    else
    if(iRet == -1){
     write_log(3 , "connect server: send to logic failed!");
    }
    send_to_client(handle_fd);
   }
  }
  try_handle_buses();
  if(ticks % 5 == 0){
   send_to_all_clients();
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

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: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } [-1]

variable_types: struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: iRet int

variable_types:
ticks
unsigned 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 epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

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

user_defined_types: struct _runtime_env { struct _rdwr_lock rdwr_lock; struct _rt_env_basic basic; }

user_defined_types: struct _rdwr_lock { unsigned char read_lock; unsigned char write_lock; unsigned char rd_lock_count; }

user_defined_types: struct _rt_env_basic { unsigned int global_id; struct _proc_info proc_info[23]; }

user_defined_types: struct _proc_info { unsigned int global_id; int pid; char ctrl_msg[64]; }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
__int64 show_time()
{
puts("Yes,Show Time Now!");
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
puts("Yes,Show Time Now!");
return 0LL;
}

original_code:
int show_time(void)
{
 printf("Yes,Show Time Now!\n");
 return 0;
}

canonical_original_code:
int show_time()
{
 printf("Yes,Show Time Now!\n");
 return 0;
}


original_code:
int module_start(int type , runtime_env_t *pruntime_env , void *ptr)
{
 void *handle = ((void *)0);
 char module_path[256] = "\0";
 module_commond_t *pcommond = (module_commond_t *)ptr;
 printf("module0 starts...\n");
 switch(type)
 {
 case 1:
 break;
 }
 switch(pcommond->type)
 {
 case 1:
  strcpy(module_path , pcommond->data.pmodule_dir_path);
  strcat(module_path , "cs_module0.so");
  printf("~~! reload %s\n" , module_path);
  handle = dlopen(module_path, 0x00001);
  if(!handle)
  {
   printf("load module %s failed!\n" , module_path);
   return -1;
  }
  test = dlsym(handle , "show_time");
  test();
  break;
 default:
  break;
 }
 return -1;
}

canonical_original_code:
int module_start(int type, struct _runtime_env *pruntime_env, void *ptr)
{
 void *handle = ((void *)0);
 char module_path[256] = "\0";
 struct _module_commond *pcommond = (module_commond_t *)ptr;
 printf("module0 starts...\n");
 switch(type)
 {
 case 1:
 break;
 }
 switch(pcommond->type)
 {
 case 1:
  strcpy(module_path , pcommond->data.pmodule_dir_path);
  strcat(module_path , "cs_module0.so");
  printf("~~! reload %s\n" , module_path);
  handle = dlopen(module_path, 0x00001);
  if(!handle)
  {
   printf("load module %s failed!\n" , module_path);
   return -1;
  }
  test = dlsym(handle , "show_time");
  test();
  break;
 default:
  break;
 }
 return -1;
}

variable_types: type int

variable_types: struct _runtime_env { struct _rdwr_lock { unsigned char read_lock; unsigned char write_lock; unsigned char rd_lock_count; } rdwr_lock; struct _rt_env_basic { unsigned int global_id; struct _proc_info { unsigned int global_id; int pid; char ctrl_msg[64]; } proc_info[23]; } basic; } *

variable_types:
ptr
void *

variable_types:
handle
void *

variable_types: struct _module_commond { unsigned char type; union _module_commond_data { char *pmodule_dir_path; void *pdata; } data; } *

user_defined_types: struct _module_commond { unsigned char type; union _module_commond_data data; }


repo: nodeleap/tinyhash

decompiled_code:
__int64 __fastcall tiny_hash_locate(__int64 a1, unsigned int a2, __int64 a3)
{
__int64 v4; // rbx

v4 = *(_QWORD *)a1 + 32LL * (a2 % *(_DWORD *)(a1 + 16));
do
{
if ( (unsigned int)tiny_hash_test(a1, v4, a2, a3) )
break;
v4 = *(_QWORD *)(v4 + 16);
}
while ( v4 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2, __int64 a3)
{
__int64 v4; // rbx

v4 = *(_QWORD *)a1 + 32LL * (a2 % *(_DWORD *)(a1 + 16));
do
{
if ( (unsigned int)func0(a1, v4, a2, a3) )
break;
v4 = *(_QWORD *)(v4 + 16);
}
while ( v4 );
return v4;
}

original_code:
static Slot *
tiny_hash_locate(TinyHash *t, HASH h, const void *k)
{
    Slot *m = (&(t)->slot[(h) % (t)->size]);
    do {
        if (tiny_hash_test(t, m, h, k)) {
            return m;
        }
        else {
            m = m->next;
        }
    } while (m);
    return ((void *)0);
}

canonical_original_code:
static struct Slot *tiny_hash_locate(struct TinyHash *t, unsigned int h, void *k)
{
    struct Slot *m = (&(t)->slot[(h) % (t)->size]);
    do {
        if (tiny_hash_test(t, m, h, k)) {
            return m;
        }
        else {
            m = m->next;
        }
    } while (m);
    return ((void *)0);
}

variable_types: struct TinyHash { struct Slot *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); } *

variable_types:
h
unsigned int

variable_types:
k
void *

variable_types: struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; } *


decompiled_code:
__int64 __fastcall tiny_hash_string_hasher(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-2128831035;
v2 = -2128831035;
do
{
++a1;
v2 = 16777619 * (v2 ^ v1);
v1 = *a1;
}
while ( *a1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-2128831035;
v2 = -2128831035;
do
{
++a1;
v2 = 16777619 * (v2 ^ v1);
v1 = *a1;
}
while ( *a1 );
return v2;
}

original_code:
static HASH tiny_hash_string_hasher(const void *key) {
    unsigned char *s = (unsigned char *) key;
    HASH h = 2166136261;
    while (*s) {
        h ^= (u_int32_t) *s++;
        h *= (HASH) 0x01000193;
    }
    return h;
}

canonical_original_code:
static unsigned int tiny_hash_string_hasher(void *key) {
    unsigned char *s = (unsigned char *) key;
    unsigned int h = 2166136261;
    while (*s) {
        h ^= (u_int32_t) *s++;
        h *= (unsigned int) 0x01000193;
    }
    return h;
}

variable_types:
key
void *

variable_types:
s
unsigned char *

return_type: unsigned int


original_code:
static void
tiny_hash_reclaim(TinyHash *t, Slot *n)
{
    memset(n, 0, sizeof(*n));
    if (n >= t->free_slot) {
        t->free_slot = n + 1;
    }
}

canonical_original_code:
static void tiny_hash_reclaim(struct TinyHash *t, struct Slot *n)
{
    memset(n, 0, sizeof(*n));
    if (n >= t->free_slot) {
        t->free_slot = n + 1;
    }
}

variable_types: struct TinyHash { struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; } *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); } *

variable_types:
n
struct Slot *

user_defined_types: struct TinyHash { struct Slot *slot; struct Slot *free_slot; unsigned int size; unsigned int count; float max_full; unsigned int max_count; unsigned int (*hasher)(void *); int (*tester)(void *, void *); }


decompiled_code:
_BOOL8 __fastcall tiny_hash_string_tester(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

original_code:
static int tiny_hash_string_tester(const void *s1, const void *s2) {
    return !strcmp((const char *) s1, (const char *) s2);
}

canonical_original_code:
static int tiny_hash_string_tester(void *s1, void *s2) {
    return !strcmp((char *) s1, (char *) s2);
}


decompiled_code:
_BOOL8 __fastcall tiny_hash_string_tester_caseless(const char *a1, const char *a2)
{
return strcasecmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(const char *a1, const char *a2)
{
return strcasecmp(a1, a2) == 0;
}

original_code:
static int tiny_hash_string_tester_caseless(const void *s1, const void *s2) {
    return !strcasecmp((const char *) s1, (const char *) s2);
}

canonical_original_code:
static int tiny_hash_string_tester_caseless(void *s1, void *s2) {
    return !strcasecmp((char *) s1, (char *) s2);
}


decompiled_code:
__int64 __fastcall tiny_hash_string_hasher_caseless(unsigned __int8 *a1)
{
unsigned __int8 v1; // bl
unsigned __int8 *v2; // rbp
const __int32_t *v3; // rcx
__int64 result; // rax

v1 = *a1;
if ( !*a1 )
return 2166136261LL;
v2 = a1;
v3 = *_ctype_tolower_loc();
LODWORD(result) = -2128831035;
do
{
++v2;
result = 16777619 * (SLOBYTE(v3[v1]) ^ (unsigned int)result);
v1 = *v2;
}
while ( *v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 *a1)
{
unsigned __int8 v1; // bl
unsigned __int8 *v2; // rbp
const __int32_t *v3; // rcx
__int64 result; // rax

v1 = *a1;
if ( !*a1 )
return 2166136261LL;
v2 = a1;
v3 = *_ctype_tolower_loc();
LODWORD(result) = -2128831035;
do
{
++v2;
result = 16777619 * (SLOBYTE(v3[v1]) ^ (unsigned int)result);
v1 = *v2;
}
while ( *v2 );
return result;
}

original_code:
static HASH tiny_hash_string_hasher_caseless(const void *key) {
    unsigned char *s = (unsigned char *) key;
    HASH h = 2166136261;
    while (*s) {
        char c;
        c = tolower(*s++);
        h ^= (u_int32_t) c;
        h *= (HASH) 0x01000193;
    }
    return h;
}

canonical_original_code:
static unsigned int tiny_hash_string_hasher_caseless(void *key) {
    unsigned char *s = (unsigned char *) key;
    unsigned int h = 2166136261;
    while (*s) {
        char c;
        c = tolower(*s++);
        h ^= (unsigned int) c;
        h *= (unsigned int) 0x01000193;
    }
    return h;
}

variable_types: c char


decompiled_code:
__int64 __fastcall tiny_hash_count(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

original_code:
uint32_t
tiny_hash_count(TinyHash *t)
{
    return t->count;
}

canonical_original_code:
unsigned int tiny_hash_count(struct TinyHash *t)
{
    return t->count;
}

user_defined_types: struct Slot { void *key; void *value; struct Slot *next; unsigned int hash; unsigned char taken; }


decompiled_code:
void __fastcall tiny_hash_destroy(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void
tiny_hash_destroy(TinyHash *t)
{
    free(t->slot);
    free(t);
}

canonical_original_code:
void tiny_hash_destroy(struct TinyHash *t)
{
    free(t->slot);
    free(t);
}


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

if ( *(_DWORD *)(a1 + 20) )
{
memset(*(void **)a1, 0, 32LL * *(unsigned int *)(a1 + 16));
result = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
*(_QWORD *)(a1 + 8) = result;
*(_DWORD *)(a1 + 20) = 0;
}
return result;
}

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

if ( *(_DWORD *)(a1 + 20) )
{
memset(*(void **)a1, 0, 32LL * *(unsigned int *)(a1 + 16));
result = *(_QWORD *)a1 + 32LL * *(unsigned int *)(a1 + 16);
*(_QWORD *)(a1 + 8) = result;
*(_DWORD *)(a1 + 20) = 0;
}
return result;
}

original_code:
void
tiny_hash_clear(TinyHash *t)
{
    if (t->count > 0) {
        memset(t->slot, 0, t->size * sizeof(Slot));
        t->free_slot = t->slot + t->size;
        t->count = 0;
    }
}

canonical_original_code:
void tiny_hash_clear(struct TinyHash *t)
{
    if (t->count > 0) {
        memset(t->slot, 0, t->size * sizeof(Slot));
        t->free_slot = t->slot + t->size;
        t->count = 0;
    }
}


decompiled_code:
_BOOL8 __fastcall tiny_hash_exists(__int64 a1, __int64 a2)
{
unsigned int v2; // eax

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
return tiny_hash_locate(a1, v2, a2) != 0;
}

original_code:
int
tiny_hash_exists(TinyHash *t, const void *key)
{
    HASH h = t->hasher(key);
    Slot *n = tiny_hash_locate(t, h, key);
    return n ? 1 : 0;
}

canonical_original_code:
int tiny_hash_exists(struct TinyHash *t, void *key)
{
    unsigned int h = t->hasher(key);
    struct Slot *n = tiny_hash_locate(t, h, key);
    return n ? 1 : 0;
}


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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
result = tiny_hash_locate(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

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

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 32))(a2);
result = func1(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
void *tiny_hash_get(TinyHash *t, const void *key) {
    HASH h = t->hasher(key);
    Slot *n = tiny_hash_locate(t, h, key);
    return n ? (void *) n->value : ((void *)0);
}

canonical_original_code:
void *tiny_hash_get(struct TinyHash *t, void *key) {
    unsigned int h = t->hasher(key);
    struct Slot *n = tiny_hash_locate(t, h, key);
    return n ? (void *) n->value : ((void *)0);
}


original_code:
int
tiny_hash_remove(TinyHash *t, const void *k)
{
    HASH h = t->hasher(k);
    Slot *m = (&(t)->slot[(h) % (t)->size]);
    Slot *n = m;
    do {
        if (tiny_hash_test(t, n, h, k)) {
            break;
        }
        else {
            m = n;
            n = n->next;
        }
    } while (n);
    if (!n) {
        return -1;
    }
    if (m == n) {
        if (m->next) {
            Slot *p = m->next;
            *m = *m->next;
            tiny_hash_reclaim(t, p);
        }
        else {
            tiny_hash_reclaim(t, n);
        }
    }
    else {
        m->next = n->next;
        tiny_hash_reclaim(t, n);
    }
    ((void) sizeof ((t->count > 0) ? 1 : 0), __extension__ ({ if (t->count > 0) ; else __assert_fail ("t->count > 0", "tinyhash.c", 253, __extension__ __PRETTY_FUNCTION__); }));
    t->count--;
    return 0;
}

canonical_original_code:
int tiny_hash_remove(struct TinyHash *t, void *k)
{
    unsigned int h = t->hasher(k);
    struct Slot *m = (&(t)->slot[(h) % (t)->size]);
    struct Slot *n = m;
    do {
        if (tiny_hash_test(t, n, h, k)) {
            break;
        }
        else {
            m = n;
            n = n->next;
        }
    } while (n);
    if (!n) {
        return -1;
    }
    if (m == n) {
        if (m->next) {
            struct Slot *p = m->next;
            *m = *m->next;
            tiny_hash_reclaim(t, p);
        }
        else {
            tiny_hash_reclaim(t, n);
        }
    }
    else {
        m->next = n->next;
        tiny_hash_reclaim(t, n);
    }
    ((void) sizeof ((t->count > 0) ? 1 : 0), __extension__ ({ if (t->count > 0) ; else __assert_fail ("t->count > 0", "tinyhash.c", 253, __extension__ __PRETTY_FUNCTION__); }));
    t->count--;
    return 0;
}