gettime

decompiled_code:
char *gettime()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
snprintf(retbuf_1, 0x3CuLL, "%lld.%lld", v1.tv_sec, v1.tv_usec);
return retbuf_1;
}

canonical_decompiled_code:
char *func7()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
snprintf(retbuf_1, 0x3CuLL, "%lld.%lld", v1.tv_sec, v1.tv_usec);
return retbuf_1;
}

original_code:
char *gettime()
{
 static char retbuf[60];
 struct timeval tv;
 gettimeofday(&tv, ((void *)0));
 snprintf(retbuf, sizeof(retbuf), "%lld.%lld",
   (long long) tv.tv_sec, (long long) tv.tv_usec);
 return retbuf;
}

variable_types:
retbuf
char [60]

variable_types: struct timeval { long tv_sec; long tv_usec; }


repo: ptitdoc/Archive-core-agent-linux

decompiled_code:
char *get_filename()
{
char *v0; // rbx
char v2[256]; // [rsp+0h] [rbp-128h] BYREF
char v3; // [rsp+100h] [rbp-28h] BYREF

if ( !(unsigned int)read_all(0) )
exit(1);
if ( index(v2, 47) )
{
fwrite("filename contains /", 1uLL, 0x13uLL, stderr);
exit(1);
}
v0 = v2;
do
{
if ( !*v0 )
break;
if ( index(" !?\"#$%^&*()[]<>;`~", *v0) )
*v0 = 95;
++v0;
}
while ( v0 != &v3 );
snprintf(retname_0, 0x106uLL, "/tmp/%s", v2);
return retname_0;
}

canonical_decompiled_code:
char *func8()
{
char *v0; // rbx
char v2[256]; // [rsp+0h] [rbp-128h] BYREF
char v3; // [rsp+100h] [rbp-28h] BYREF

if ( !(unsigned int)func15(0) )
exit(1);
if ( index(v2, 47) )
{
fwrite("filename contains /", 1uLL, 0x13uLL, stderr);
exit(1);
}
v0 = v2;
do
{
if ( !*v0 )
break;
if ( index(" !?\"#$%^&*()[]<>;`~", *v0) )
*v0 = 95;
++v0;
}
while ( v0 != &v3 );
snprintf(retname_0, 0x106uLL, "/tmp/%s", v2);
return retname_0;
}

original_code:
char *get_filename()
{
 char buf[256];
 static char retname[sizeof(buf) + sizeof("/tmp/")];
 int i;
 if (!read_all(0, buf, sizeof(buf)))
  exit(1);
 if (index(buf, '/')) {
  fprintf(stderr, "filename contains /");
  exit(1);
 }
 for (i=0; i < 256 && buf[i]!=0; i++) {
  if (index(" !?\"#$%^&*()[]<>;`~", buf[i]))
   buf[i]='_';
 }
 snprintf(retname, sizeof(retname), "/tmp/%s", buf);
 return retname;
}

canonical_original_code:
char *get_filename()
{
 char buf[256];
 static char retname[-1];
 int i;
 if (!read_all(0, buf, sizeof(buf)))
  exit(1);
 if (index(buf, '/')) {
  fprintf(stderr, "filename contains /");
  exit(1);
 }
 for (i=0; i < 256 && buf[i]!=0; i++) {
  if (index(" !?\"#$%^&*()[]<>;`~", buf[i]))
   buf[i]='_';
 }
 snprintf(retname, sizeof(retname), "/tmp/%s", buf);
 return retname;
}

variable_types:
buf
char [256]

variable_types:
retname
char [-1]

variable_types: i int

return_type: char *


decompiled_code:
int __fastcall copy_file(const char *a1)
{
int v1; // eax
int v2; // ebx

v1 = open(a1, 65, 384LL);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)copy_fd_all(v1, 0) )
exit(1);
return close(v2);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
int v1; // eax
int v2; // ebx

v1 = open(a1, 65, 384LL);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)func16(v1, 0) )
exit(1);
return close(v2);
}

original_code:
void copy_file(char *filename)
{
 int fd = open(filename, 01 | 0100, 0600);
 if (fd < 0) {
  perror("open file");
  exit(1);
 }
 if (!copy_fd_all(fd, 0))
        exit(1);
 close(fd);
}

variable_types:
filename
char *

variable_types: fd int

return_type: void


decompiled_code:
int __fastcall send_file_back(const char *a1)
{
int v1; // eax
int v2; // ebx

v1 = open(a1, 0);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)copy_fd_all(1, v1) )
exit(1);
return close(v2);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
int v1; // eax
int v2; // ebx

v1 = open(a1, 0);
if ( v1 < 0 )
{
perror("open file");
exit(1);
}
v2 = v1;
if ( !(unsigned int)func16(1, v1) )
exit(1);
return close(v2);
}

original_code:
void send_file_back(char * filename)
{
 int fd = open(filename, 00);
 if (fd < 0) {
  perror("open file");
  exit(1);
 }
 if (!copy_fd_all(1, fd))
  exit(1);
 close(fd);
}

canonical_original_code:
void send_file_back(char *filename)
{
 int fd = open(filename, 00);
 if (fd < 0) {
  perror("open file");
  exit(1);
 }
 if (!copy_fd_all(1, fd))
  exit(1);
 close(fd);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
char *v4; // rax
__pid_t v5; // eax
char *v6; // rax
__pid_t v7; // eax
FILE *v9; // rbx
unsigned int v10; // eax
int v11; // ebx
FILE *v12; // r12
int v13; // eax
int v14; // ebx
char v15[512]; // [rsp+0h] [rbp-17D8h] BYREF
char value[4096]; // [rsp+200h] [rbp-15D8h] BYREF
char name[1036]; // [rsp+1200h] [rbp-5D8h] BYREF
int stat_loc; // [rsp+160Ch] [rbp-1CCh] BYREF
struct stat v19; // [rsp+1610h] [rbp-1C8h] BYREF
struct stat v20; // [rsp+16A0h] [rbp-138h] BYREF
struct stat v21; // [rsp+1730h] [rbp-A8h] BYREF

v3 = get_filename();
copy_file(v3);
if ( (unsigned int)stat(v3, &v21) )
{
perror("stat pre");
exit(1);
}
v4 = gettime();
fprintf(stderr, "time=%s, waiting for qubes-session\n", v4);
if ( (unsigned int)stat("/tmp/qubes-session-env", &v19) )
{
v5 = fork();
if ( v5 == -1 )
{
perror("fork");
exit(1);
}
if ( !v5 )
{
v9 = fopen("/tmp/qubes-session-waiter", "a");
if ( v9 )
{
v10 = getpid();
fprintf(v9, "%d\n", v10);
fclose(v9);
if ( (unsigned int)stat("/tmp/qubes-session-env", &v19) )
pause();
exit(0);
}
perror("fopen waiter_pidfile");
exit(1);
}
waitpid(v5, &stat_loc, 0);
if ( (stat_loc & 0x7F) == 0 && BYTE1(stat_loc) )
exit(BYTE1(stat_loc));
}
v6 = gettime();
fprintf(stderr, "time=%s, starting editor\n", v6);
v7 = fork();
if ( v7 == -1 )
{
perror("fork");
exit(1);
}
if ( !v7 )
{
v11 = open("/dev/null", 0);
dup2(v11, 0);
close(v11);
v12 = fopen("/tmp/qubes-session-env", "r");
while ( (unsigned int)__isoc99_fscanf() == 2 )
setenv(name, value, 1);
fclose(v12);
v13 = open("/tmp/mimeopen.log", 1088, 438LL);
v14 = v13;
if ( v13 != -1 )
{
dup2(v13, 1);
close(v14);
setenv("HOME", "/home/user", 1);
setenv("DISPLAY", ":0", 1);
execl("/usr/bin/mimeopen", "mimeopen", "-n", v3, 0LL);
perror("execl");
exit(1);
}
perror("open /tmp/mimeopen.log");
exit(1);
}
waitpid(v7, &stat_loc, 0);
if ( stat_loc )
{
snprintf(
v15,
0x200uLL,
"HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit s"
"tatus: %d)!' > /tmp/kdialog.log 2>&1 </dev/null",
(unsigned int)stat_loc);
system(v15);
}
if ( (unsigned int)stat(v3, &v20) )
{
perror("stat post");
exit(1);
}
if ( v21.st_mtim.tv_sec != v20.st_mtim.tv_sec )
send_file_back(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
char *v4; // rax
__pid_t v5; // eax
char *v6; // rax
__pid_t v7; // eax
FILE *v9; // rbx
unsigned int v10; // eax
int v11; // ebx
FILE *v12; // r12
int v13; // eax
int v14; // ebx
char v15[512]; // [rsp+0h] [rbp-17D8h] BYREF
char value[4096]; // [rsp+200h] [rbp-15D8h] BYREF
char name[1036]; // [rsp+1200h] [rbp-5D8h] BYREF
int stat_loc; // [rsp+160Ch] [rbp-1CCh] BYREF
struct stat v19; // [rsp+1610h] [rbp-1C8h] BYREF
struct stat v20; // [rsp+16A0h] [rbp-138h] BYREF
struct stat v21; // [rsp+1730h] [rbp-A8h] BYREF

v3 = func8();
func9(v3);
if ( (unsigned int)func19(v3, &v21) )
{
perror("stat pre");
exit(1);
}
v4 = func7();
fprintf(stderr, "time=%s, waiting for qubes-session\n", v4);
if ( (unsigned int)func19("/tmp/qubes-session-env", &v19) )
{
v5 = fork();
if ( v5 == -1 )
{
perror("fork");
exit(1);
}
if ( !v5 )
{
v9 = fopen("/tmp/qubes-session-waiter", "a");
if ( v9 )
{
v10 = getpid();
fprintf(v9, "%d\n", v10);
fclose(v9);
if ( (unsigned int)func19("/tmp/qubes-session-env", &v19) )
pause();
exit(0);
}
perror("fopen waiter_pidfile");
exit(1);
}
waitpid(v5, &stat_loc, 0);
if ( (stat_loc & 0x7F) == 0 && BYTE1(stat_loc) )
exit(BYTE1(stat_loc));
}
v6 = func7();
fprintf(stderr, "time=%s, starting editor\n", v6);
v7 = fork();
if ( v7 == -1 )
{
perror("fork");
exit(1);
}
if ( !v7 )
{
v11 = open("/dev/null", 0);
dup2(v11, 0);
close(v11);
v12 = fopen("/tmp/qubes-session-env", "r");
while ( (unsigned int)__isoc99_fscanf() == 2 )
setenv(name, value, 1);
fclose(v12);
v13 = open("/tmp/mimeopen.log", 1088, 438LL);
v14 = v13;
if ( v13 != -1 )
{
dup2(v13, 1);
close(v14);
setenv("HOME", "/home/user", 1);
setenv("DISPLAY", ":0", 1);
execl("/usr/bin/mimeopen", "mimeopen", "-n", v3, 0LL);
perror("execl");
exit(1);
}
perror("open /tmp/mimeopen.log");
exit(1);
}
waitpid(v7, &stat_loc, 0);
if ( stat_loc )
{
snprintf(
v15,
0x200uLL,
"HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit s"
"tatus: %d)!' > /tmp/kdialog.log 2>&1 </dev/null",
(unsigned int)stat_loc);
system(v15);
}
if ( (unsigned int)func19(v3, &v20) )
{
perror("stat post");
exit(1);
}
if ( v21.st_mtim.tv_sec != v20.st_mtim.tv_sec )
func10(v3);
return 0;
}

original_code:
int main(int argc, char ** argv)
{
 signal(13, ((__sighandler_t) 1));
 if (argc!=2)
  gui_fatal("OpenInVM - no file given?");
 talk_to_daemon(argv[1]);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 signal(13, ((void (*)(int)) 1));
 if (argc!=2)
  gui_fatal("OpenInVM - no file given?");
 talk_to_daemon(argv[1]);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


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

v1 = __errno_location();
v2 = *v1;
perror(s);
*v1 = v2;
}

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

v1 = __errno_location();
v2 = *v1;
perror(s);
*v1 = v2;
}

original_code:
void perror_wrapper(char * msg)
{
 int prev=(*__errno_location ());
 perror(msg);
 (*__errno_location ())=prev;
}

canonical_original_code:
void perror_wrapper(char *msg)
{
 int prev = (*__errno_location ());
 perror(msg);
 (*__errno_location ())=prev;
}

variable_types:
msg
char *

variable_types: prev int


decompiled_code:
int __fastcall set_nonblock(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func12(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

original_code:
void set_nonblock(int fd)
{
 int fl = fcntl(fd, 3, 0);
 fcntl(fd, 4, fl | 04000);
}

variable_types: fl int


decompiled_code:
int __fastcall set_block(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) &= 0xF7u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func13(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3, 0LL);
BYTE1(v1) &= 0xF7u;
return fcntl(fd, 4, v1);
}

original_code:
void set_block(int fd)
{
 int fl = fcntl(fd, 3, 0);
 fcntl(fd, 4, fl & ~04000);
}


decompiled_code:
__int64 __fastcall write_all(int fd, __int64 a2, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v4 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *__errno_location() != 4 )
return 0LL;
goto LABEL_5;
}
if ( v5 <= 0 )
return 0LL;
v4 += v5;
LABEL_5:
if ( v4 >= a3 )
return 1LL;
}
}

canonical_decompiled_code:
__int64 __fastcall func14(int fd, __int64 a2, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v4 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *__errno_location() != 4 )
return 0LL;
goto LABEL_5;
}
if ( v5 <= 0 )
return 0LL;
v4 += v5;
LABEL_5:
if ( v4 >= a3 )
return 1LL;
}
}

original_code:
int write_all(int fd, void *buf, int size)
{
 int written = 0;
 int ret;
 while (written < size) {
  ret = write(fd, (char *) buf + written, size - written);
  if (ret == -1 && (*__errno_location ()) == 4)
   continue;
  if (ret <= 0) {
   return 0;
  }
  written += ret;
 }
 return 1;
}

variable_types:
buf
void *

variable_types: size int

variable_types: written int

variable_types: ret int


decompiled_code:
__int64 __fastcall read_all(int fd, __int64 a2, int a3)
{
int v4; // ebp
unsigned int v5; // er15
int v6; // eax
int v7; // ebx

if ( a3 <= 0 )
return 1;
v4 = 0;
while ( 1 )
{
v6 = read(fd, (void *)(a2 + v4), a3 - v4);
v7 = v6;
v5 = v6;
if ( v6 != -1 )
break;
if ( *__errno_location() != 4 )
goto LABEL_4;
LABEL_8:
if ( a3 <= v4 )
return 1;
}
if ( !v6 )
{
*__errno_location() = 0;
fwrite("EOF\n", 1uLL, 4uLL, stderr);
return v5;
}
if ( v6 >= 0 )
{
if ( !v4 )
set_block(fd);
v4 += v7;
goto LABEL_8;
}
LABEL_4:
v5 = 0;
if ( *__errno_location() != 11 )
perror_wrapper("read");
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func15(int fd, __int64 a2, int a3)
{
int v4; // ebp
unsigned int v5; // er15
int v6; // eax
int v7; // ebx

if ( a3 <= 0 )
return 1;
v4 = 0;
while ( 1 )
{
v6 = read(fd, (void *)(a2 + v4), a3 - v4);
v7 = v6;
v5 = v6;
if ( v6 != -1 )
break;
if ( *__errno_location() != 4 )
goto LABEL_4;
LABEL_8:
if ( a3 <= v4 )
return 1;
}
if ( !v6 )
{
*__errno_location() = 0;
fwrite("EOF\n", 1uLL, 4uLL, stderr);
return v5;
}
if ( v6 >= 0 )
{
if ( !v4 )
func13(fd);
v4 += v7;
goto LABEL_8;
}
LABEL_4:
v5 = 0;
if ( *__errno_location() != 11 )
func11("read");
return v5;
}

original_code:
int read_all(int fd, void *buf, int size)
{
 int got_read = 0;
 int ret;
 while (got_read < size) {
  ret = read(fd, (char *) buf + got_read, size - got_read);
  if (ret == -1 && (*__errno_location ()) == 4)
   continue;
  if (ret == 0) {
   (*__errno_location ()) = 0;
   fprintf(stderr, "EOF\n");
   return 0;
  }
  if (ret < 0) {
   if ((*__errno_location ()) != 11)
    perror_wrapper("read");
   return 0;
  }
  if (got_read == 0) {
   set_block(fd);
  }
  got_read += ret;
 }
 return 1;
}


decompiled_code:
__int64 __fastcall copy_fd_all(int fd, int a2)
{
unsigned int v2; // ebx
int v3; // eax
_BYTE savedregs[4136]; // [rsp+0h] [rbp+0h] BYREF

while ( 1 )
{
while ( 1 )
{
v3 = read(a2, savedregs, 0x1000uLL);
if ( v3 != -1 )
break;
if ( *__errno_location() != 4 )
{
LABEL_7:
perror_wrapper("read");
return 0;
}
}
if ( !v3 )
return 1;
if ( v3 < 0 )
goto LABEL_7;
v2 = write_all(fd, (__int64)savedregs, v3);
if ( !v2 )
{
perror_wrapper("write");
return v2;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func16(int fd, int a2)
{
unsigned int v2; // ebx
int v3; // eax
_BYTE savedregs[4136]; // [rsp+0h] [rbp+0h] BYREF

while ( 1 )
{
while ( 1 )
{
v3 = read(a2, savedregs, 0x1000uLL);
if ( v3 != -1 )
break;
if ( *__errno_location() != 4 )
{
LABEL_7:
func11("read");
return 0;
}
}
if ( !v3 )
return 1;
if ( v3 < 0 )
goto LABEL_7;
v2 = func14(fd, (__int64)savedregs, v3);
if ( !v2 )
{
func11("write");
return v2;
}
}
}

original_code:
int copy_fd_all(int fdout, int fdin)
{
 int ret;
 char buf[4096];
 for (;;) {
  ret = read(fdin, buf, sizeof(buf));
  if (ret == -1 && (*__errno_location ()) == 4)
   continue;
  if (!ret)
   break;
  if (ret < 0) {
   perror_wrapper("read");
   return 0;
  }
  if (!write_all(fdout, buf, ret)) {
   perror_wrapper("write");
   return 0;
  }
 }
 return 1;
}

variable_types: fdout int

variable_types: fdin int

variable_types:
buf
char [4096]


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v4; // ebp
const char *v5; // rdi
int v6; // eax
__int64 v7; // rdx
int v8; // ebx
__int64 v9; // rbp
char *v10; // rax
__pid_t v11; // eax
unsigned int v12; // ebp
int v13; // ebx
size_t v14; // rbp
char *v15; // rsi
sigset_t v16; // [rsp+0h] [rbp-1118h] BYREF
sigset_t oset; // [rsp+80h] [rbp-1098h] BYREF
__int64 buf[515]; // [rsp+100h] [rbp-1018h] BYREF

if ( (unsigned int)(argc - 3) <= 1 )
{
v4 = atoi(argv[1]);
used_mem_change_threshold = v4;
v5 = argv[2];
v6 = atoi(v5);
delay = v6;
if ( v4 && v6 )
{
if ( argc == 4 )
{
v11 = fork();
v12 = v11;
if ( v11 == -1 )
{
perror("fork");
exit(1);
}
if ( v11 )
{
v13 = open(argv[3], 577, 438LL);
if ( v13 >= 0 )
{
v14 = sprintf((char *)buf, "%d\n", v12);
if ( write(v13, buf, v14) != v14 )
{
perror("write pid");
exit(1);
}
close(v13);
exit(0);
}
perror("open pidfile");
exit(1);
}
sigemptyset(&v16);
sigaddset(&v16, 10);
sigprocmask(0, &v16, &oset);
usr1_received = 0;
signal(10, (__sighandler_t)usr1_handler);
while ( !usr1_received )
sigsuspend(&oset);
sigprocmask(1, &v16, 0LL);
}
v8 = open("/proc/meminfo", 0);
if ( v8 >= 0 )
{
v9 = xs_domain_open();
if ( v9 )
{
if ( argc == 3 )
{
*((_BYTE *)buf + (int)pread(v8, buf, 0x1000uLL, 0LL)) = 0;
v10 = parse(buf);
if ( v10 )
send_to_qmemman(v9, v10);
if ( fork() > 0 )
exit(0);
}
while ( 1 )
{
*((_BYTE *)buf + (int)pread(v8, buf, 0x1000uLL, 0LL)) = 0;
v15 = parse(buf);
if ( v15 )
send_to_qmemman(v9, v15);
usleep(delay);
}
}
perror("xs_domain_open");
exit(1);
}
perror("open meminfo");
exit(1);
}
usage(v5, argv, v7);
}
usage(argc, argv, envp);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v4; // ebp
const char *v5; // rdi
int v6; // eax
__int64 v7; // rdx
int v8; // ebx
__int64 v9; // rbp
char *v10; // rax
__pid_t v11; // eax
unsigned int v12; // ebp
int v13; // ebx
size_t v14; // rbp
char *v15; // rsi
sigset_t v16; // [rsp+0h] [rbp-1118h] BYREF
sigset_t oset; // [rsp+80h] [rbp-1098h] BYREF
__int64 buf[515]; // [rsp+100h] [rbp-1018h] BYREF

if ( (unsigned int)(argc - 3) <= 1 )
{
v4 = atoi(argv[1]);
used_mem_change_threshold = v4;
v5 = argv[2];
v6 = atoi(v5);
delay = v6;
if ( v4 && v6 )
{
if ( argc == 4 )
{
v11 = fork();
v12 = v11;
if ( v11 == -1 )
{
perror("fork");
exit(1);
}
if ( v11 )
{
v13 = open(argv[3], 577, 438LL);
if ( v13 >= 0 )
{
v14 = sprintf((char *)buf, "%d\n", v12);
if ( write(v13, buf, v14) != v14 )
{
perror("write pid");
exit(1);
}
close(v13);
exit(0);
}
perror("open pidfile");
exit(1);
}
sigemptyset(&v16);
sigaddset(&v16, 10);
sigprocmask(0, &v16, &oset);
usr1_received = 0;
signal(10, (__sighandler_t)usr1_handler);
while ( !usr1_received )
sigsuspend(&oset);
sigprocmask(1, &v16, 0LL);
}
v8 = open("/proc/meminfo", 0);
if ( v8 >= 0 )
{
v9 = xs_domain_open();
if ( v9 )
{
if ( argc == 3 )
{
*((_BYTE *)buf + (int)pread(v8, buf, 0x1000uLL, 0LL)) = 0;
v10 = func8(buf);
if ( v10 )
func10(v9, v10);
if ( fork() > 0 )
exit(0);
}
while ( 1 )
{
*((_BYTE *)buf + (int)pread(v8, buf, 0x1000uLL, 0LL)) = 0;
v15 = func8(buf);
if ( v15 )
func10(v9, v15);
usleep(delay);
}
}
perror("xs_domain_open");
exit(1);
}
perror("open meminfo");
exit(1);
}
func9(v5, argv, v7);
}
func9(argc, argv, envp);
}


repo: ptitiano/cpuloadgen

decompiled_code:
void free_buffers()
{
if ( threads )
free(threads);
if ( cpuloads )
free(cpuloads);
}

canonical_decompiled_code:
void func0()
{
if ( threads )
free(threads);
if ( cpuloads )
free(cpuloads);
}

original_code:
static void free_buffers(void)
{
 if (threads != ((void *)0))
  free(threads);
 if (cpuloads != ((void *)0))
  free(cpuloads);
}

canonical_original_code:
static void free_buffers()
{
 if (threads != ((void *)0))
  free(threads);
 if (cpuloads != ((void *)0))
  free(cpuloads);
}


decompiled_code:
void sigterm_handler()
{
puts("Halting load generation...");
fflush(stdout);
free_buffers();
puts("done.\n");
fflush(stdout);
}

canonical_decompiled_code:
void func1()
{
puts("Halting load generation...");
fflush(stdout);
func0();
puts("done.\n");
fflush(stdout);
}

original_code:
void sigterm_handler(void)
{
 unsigned int i;
 printf("Halting load generation...\n");
 fflush(stdout);
 free_buffers();
 printf("done.\n\n");
 fflush(stdout);
}

canonical_original_code:
void sigterm_handler()
{
 unsigned int i;
 printf("Halting load generation...\n");
 fflush(stdout);
 free_buffers();
 printf("done.\n\n");
 fflush(stdout);
}

variable_types:
i
unsigned int


usage

decompiled_code:
int usage()
{
puts("Usage:");
puts("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n");
puts("Generate adjustable processing load on selected CPU core(s) for a given duration.");
puts("Load is a percentage which may be any integer value between 1 and 100.");
puts("Duration time unit is seconds.");
puts("Arguments may be provided in any order.");
puts("If duration is omitted, generate load(s) until CTRL+C is pressed.");
printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
puts("e.g.:");
printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
puts("\t# cpuloadgen");
printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
puts("\t# cpuloadgen duration=10");
printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
return puts("\t# cpuloadgen cpu3=100 cpu1=50 duration=5\n");
}

canonical_decompiled_code:
int func2()
{
puts("Usage:");
puts("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n");
puts("Generate adjustable processing load on selected CPU core(s) for a given duration.");
puts("Load is a percentage which may be any integer value between 1 and 100.");
puts("Duration time unit is seconds.");
puts("Arguments may be provided in any order.");
puts("If duration is omitted, generate load(s) until CTRL+C is pressed.");
printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
puts("e.g.:");
printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
puts("\t# cpuloadgen");
printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
puts("\t# cpuloadgen duration=10");
printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
return puts("\t# cpuloadgen cpu3=100 cpu1=50 duration=5\n");
}

original_code:
static void usage(void)
{
 printf("Usage:\n");
 printf("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n\n");
 printf("Generate adjustable processing load on selected CPU core(s) for a given duration.\n");
 printf("Load is a percentage which may be any integer value between 1 and 100.\n");
 printf("Duration time unit is seconds.\n");
 printf("Arguments may be provided in any order.\n");
 printf("If duration is omitted, generate load(s) until CTRL+C is pressed.\n");
 printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
 printf("e.g.:\n");
 printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
 printf("	# cpuloadgen\n");
 printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
 printf("	# cpuloadgen duration=10\n");
 printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
 printf("	# cpuloadgen cpu3=100 cpu1=50 duration=5\n\n");
}

canonical_original_code:
static void usage()
{
 printf("Usage:\n");
 printf("\tcpuloadgen [<cpu[n]=load>] [<duration=time>]\n\n");
 printf("Generate adjustable processing load on selected CPU core(s) for a given duration.\n");
 printf("Load is a percentage which may be any integer value between 1 and 100.\n");
 printf("Duration time unit is seconds.\n");
 printf("Arguments may be provided in any order.\n");
 printf("If duration is omitted, generate load(s) until CTRL+C is pressed.\n");
 printf("If no argument is given, generate 100%% load on all online CPU cores indefinitely.\n\n");
 printf("e.g.:\n");
 printf(" - Generate 100%% load on all online CPU cores until CTRL+C is pressed:\n");
 printf("	# cpuloadgen\n");
 printf(" - Generate 100%% load on all online CPU cores during 10 seconds:\n");
 printf("	# cpuloadgen duration=10\n");
 printf(" - Generate 50%% load on CPU1 and 100%% load on CPU3 during 10 seconds:\n");
 printf("	# cpuloadgen cpu3=100 cpu1=50 duration=5\n\n");
}


einval

decompiled_code:
__int64 __fastcall einval(const char *a1)
{
fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", a1);
usage();
free_buffers();
return 4294967274LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", a1);
func2();
func0();
return 4294967274LL;
}

original_code:
static int einval(const char *arg)
{
 fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", arg);
 usage();
 free_buffers();
 return -22;
}

canonical_original_code:
static int einval(char *arg)
{
 fprintf(stderr, "cpuloadgen: invalid argument!!! (%s)\n\n", arg);
 usage();
 free_buffers();
 return -22;
}

variable_types:
arg
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
__int64 v5; // r12
void *v6; // r13
int v7; // eax
int i; // eax
int v10; // eax
int v11; // eax
unsigned int *v12; // rax
const char *v13; // rdi
unsigned int v14; // eax
int v15; // eax
__int64 v16; // [rsp+8h] [rbp-40h] BYREF
unsigned int v17; // [rsp+14h] [rbp-34h] BYREF
int v18; // [rsp+18h] [rbp-30h] BYREF
unsigned int arg[11]; // [rsp+1Ch] [rbp-2Ch] BYREF

signal(15, (__sighandler_t)sigterm_handler);
printf("CPULOADGEN (REV %s built %s)\n\n", "0.94", builddate);
v4 = sysconf(84);
cpu_count = v4;
if ( v4 <= 0 )
{
fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n", (unsigned int)v4);
return cpu_count;
}
v5 = v4;
v6 = malloc(8LL * v4);
threads = v6;
cpuloads = malloc(4 * v5);
if ( !cpuloads || !v6 )
{
fwrite("cpuloadgen: could not allocate buffers!!!\n", 1uLL, 0x2AuLL, stderr);
return -12;
}
if ( argc == 1 )
{
arg[0] = 0;
v10 = 0;
do
{
*((_QWORD *)threads + v10) = -1LL;
*((_DWORD *)cpuloads + (int)arg[0]) = 100;
v10 = arg[0] + 1;
arg[0] = v10;
}
while ( v10 < cpu_count );
duration = -1LL;
}
else
{
arg[0] = 0;
v7 = 0;
do
{
*((_QWORD *)threads + v7) = -1LL;
*((_DWORD *)cpuloads + (int)arg[0]) = -1;
v7 = arg[0] + 1;
arg[0] = v7;
}
while ( v7 < cpu_count );
duration = -1LL;
arg[0] = 1;
if ( argc > 1 )
{
for ( i = 1; i < argc; arg[0] = i )
{
v13 = argv[i];
if ( *v13 == 99 )
{
if ( (unsigned int)_isoc99_sscanf(v13, "cpu%d=%d", &v18, &v17) != 2
|| v18 < 0
|| v18 >= cpu_count
|| v17 - 1 > 0x63 )
{
return einval(argv[arg[0]]);
}
v12 = (unsigned int *)((char *)cpuloads + 4 * v18);
if ( *v12 != -1 )
{
fprintf(stderr, "cpuloadgen: CPU%d was already assigned a load of %d!\n\n", (unsigned int)v18, *v12);
free_buffers();
return -22;
}
*v12 = v17;
}
else
{
if ( *v13 != 100 )
return einval(v13);
if ( (unsigned int)_isoc99_sscanf(v13, "duration=%ld", &v16) != 1 || v16 <= 0 )
return einval(argv[arg[0]]);
if ( duration != -1 )
{
fprintf(stderr, "cpuloadgen: duration was already set to %ld!\n\n", duration);
free_buffers();
return -22;
}
duration = v16;
}
i = arg[0] + 1;
}
}
}
puts("Press CTRL+C to stop load generation at any time.\n");
arg[0] = 0;
if ( cpu_count > 0 )
{
v11 = 0;
do
{
if ( *((_DWORD *)cpuloads + v11) != -1 )
{
pthread_mutex_lock(&mutex1);
v14 = pthread_create((pthread_t *)threads + (int)arg[0], 0LL, thread_loadgen, arg);
if ( v14 )
fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)", arg[0], v14);
}
v11 = arg[0] + 1;
arg[0] = v11;
}
while ( v11 < cpu_count );
}
arg[0] = 0;
if ( cpu_count > 0 )
{
v15 = 0;
do
{
if ( *((_DWORD *)cpuloads + v15) != -1 )
pthread_join(*((_QWORD *)threads + v15), 0LL);
v15 = arg[0] + 1;
arg[0] = v15;
}
while ( v15 < cpu_count );
}
free_buffers();
puts("\ndone.\n");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
__int64 v5; // r12
void *v6; // r13
int v7; // eax
int i; // eax
int v10; // eax
int v11; // eax
unsigned int *v12; // rax
const char *v13; // rdi
unsigned int v14; // eax
int v15; // eax
__int64 v16; // [rsp+8h] [rbp-40h] BYREF
unsigned int v17; // [rsp+14h] [rbp-34h] BYREF
int v18; // [rsp+18h] [rbp-30h] BYREF
unsigned int arg[11]; // [rsp+1Ch] [rbp-2Ch] BYREF

signal(15, (__sighandler_t)sigterm_handler);
printf("CPULOADGEN (REV %s built %s)\n\n", "0.94", builddate);
v4 = sysconf(84);
cpu_count = v4;
if ( v4 <= 0 )
{
fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n", (unsigned int)v4);
return cpu_count;
}
v5 = v4;
v6 = malloc(8LL * v4);
threads = v6;
cpuloads = malloc(4 * v5);
if ( !cpuloads || !v6 )
{
fwrite("cpuloadgen: could not allocate buffers!!!\n", 1uLL, 0x2AuLL, stderr);
return -12;
}
if ( argc == 1 )
{
arg[0] = 0;
v10 = 0;
do
{
*((_QWORD *)threads + v10) = -1LL;
*((_DWORD *)cpuloads + (int)arg[0]) = 100;
v10 = arg[0] + 1;
arg[0] = v10;
}
while ( v10 < cpu_count );
duration = -1LL;
}
else
{
arg[0] = 0;
v7 = 0;
do
{
*((_QWORD *)threads + v7) = -1LL;
*((_DWORD *)cpuloads + (int)arg[0]) = -1;
v7 = arg[0] + 1;
arg[0] = v7;
}
while ( v7 < cpu_count );
duration = -1LL;
arg[0] = 1;
if ( argc > 1 )
{
for ( i = 1; i < argc; arg[0] = i )
{
v13 = argv[i];
if ( *v13 == 99 )
{
if ( (unsigned int)_isoc99_sscanf(v13, "cpu%d=%d", &v18, &v17) != 2
|| v18 < 0
|| v18 >= cpu_count
|| v17 - 1 > 0x63 )
{
return func3(argv[arg[0]]);
}
v12 = (unsigned int *)((char *)cpuloads + 4 * v18);
if ( *v12 != -1 )
{
fprintf(stderr, "cpuloadgen: CPU%d was already assigned a load of %d!\n\n", (unsigned int)v18, *v12);
func0();
return -22;
}
*v12 = v17;
}
else
{
if ( *v13 != 100 )
return func3(v13);
if ( (unsigned int)_isoc99_sscanf(v13, "duration=%ld", &v16) != 1 || v16 <= 0 )
return func3(argv[arg[0]]);
if ( duration != -1 )
{
fprintf(stderr, "cpuloadgen: duration was already set to %ld!\n\n", duration);
func0();
return -22;
}
duration = v16;
}
i = arg[0] + 1;
}
}
}
puts("Press CTRL+C to stop load generation at any time.\n");
arg[0] = 0;
if ( cpu_count > 0 )
{
v11 = 0;
do
{
if ( *((_DWORD *)cpuloads + v11) != -1 )
{
pthread_mutex_lock(&mutex1);
v14 = pthread_create((pthread_t *)threads + (int)arg[0], 0LL, thread_loadgen, arg);
if ( v14 )
fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)", arg[0], v14);
}
v11 = arg[0] + 1;
arg[0] = v11;
}
while ( v11 < cpu_count );
}
arg[0] = 0;
if ( cpu_count > 0 )
{
v15 = 0;
do
{
if ( *((_DWORD *)cpuloads + v15) != -1 )
pthread_join(*((_QWORD *)threads + v15), 0LL);
v15 = arg[0] + 1;
arg[0] = v15;
}
while ( v15 < cpu_count );
}
func0();
puts("\ndone.\n");
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 unsigned int cpu0load, cpu1load, cpu2load, cpu3load;
 int i, ret, n, load;
 long int duration2;
 signal(15, (sighandler_t) sigterm_handler);
 printf("CPULOADGEN (REV %s built %s)\n\n",
  ((const char *) "0.94"), builddate);
 cpu_count = (int) sysconf(_SC_NPROCESSORS_ONLN);
 if (cpu_count < 1) {
  fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n",
   cpu_count);
  return cpu_count;
 }
 ;
 threads = malloc(cpu_count * sizeof(pthread_t));
 cpuloads = malloc(cpu_count * sizeof(int));
 if ((threads == ((void *)0)) || (cpuloads == ((void *)0))) {
  fprintf(stderr, "cpuloadgen: could not allocate buffers!!!\n");
  return -12;
 }
 if (argc == 1) {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = 100;
  }
  duration = -1;
 } else {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = -1;
  }
  duration = -1;
  for (i = 1; i < argc; i++) {
   ;
   if (argv[i][0] == 'c') {
    ret = sscanf(argv[i], "cpu%d=%d", &n, &load);
    if ((ret != 2) ||
     ((n < 0) || (n >= cpu_count)) ||
     ((load < 1) || (load > 100)))
     return einval(argv[i]);
    if (cpuloads[n] != -1) {
     fprintf(stderr,
      "cpuloadgen: CPU%d was already assigned a load of %d!\n\n",
      n, cpuloads[n]);
     free_buffers();
     return -22;
    }
    cpuloads[n] = load;
    ;
   } else if (argv[i][0] == 'd') {
    ret = sscanf(argv[i], "duration=%ld",
     &duration2);
    if ((ret != 1) || (duration2 < 1)) {
     return einval(argv[i]);
    }
    if (duration != -1) {
     fprintf(stderr,
      "cpuloadgen: duration was already set to %ld!\n\n",
      duration);
     free_buffers();
     return -22;
    }
    duration = duration2;
    ;
   } else {
    return einval(argv[i]);
   }
  }
 }
 printf("Press CTRL+C to stop load generation at any time.\n\n");
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   ;
   continue;
  }
  pthread_mutex_lock(&mutex1);
  ret = pthread_create(&threads[i], ((void *)0), thread_loadgen, &i);
  if (ret != 0) {
   fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)",
   i, ret);
   continue;
  }
 }
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   continue;
  }
  pthread_join(threads[i], ((void *)0));
 }
 free_buffers();
 printf("\ndone.\n\n");
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 unsigned int cpu0load; unsigned int cpu1load; unsigned int cpu2load; unsigned int cpu3load;
 int i; int ret; int n; int load;
 long duration2;
 signal(15, (void (*)(int)) sigterm_handler);
 printf("CPULOADGEN (REV %s built %s)\n\n",
  ((char *) "0.94"), builddate);
 cpu_count = (int) sysconf(_SC_NPROCESSORS_ONLN);
 if (cpu_count < 1) {
  fprintf(stderr, "cpuloadgen: could not determine CPU cores count!!! (%d)\n",
   cpu_count);
  return cpu_count;
 }
 ;
 threads = malloc(cpu_count * sizeof(pthread_t));
 cpuloads = malloc(cpu_count * sizeof(int));
 if ((threads == ((void *)0)) || (cpuloads == ((void *)0))) {
  fprintf(stderr, "cpuloadgen: could not allocate buffers!!!\n");
  return -12;
 }
 if (argc == 1) {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = 100;
  }
  duration = -1;
 } else {
  for (i = 0; i < cpu_count; i++) {
   threads[i] = -1;
   cpuloads[i] = -1;
  }
  duration = -1;
  for (i = 1; i < argc; i++) {
   ;
   if (argv[i][0] == 'c') {
    ret = sscanf(argv[i], "cpu%d=%d", &n, &load);
    if ((ret != 2) ||
     ((n < 0) || (n >= cpu_count)) ||
     ((load < 1) || (load > 100)))
     return einval(argv[i]);
    if (cpuloads[n] != -1) {
     fprintf(stderr,
      "cpuloadgen: CPU%d was already assigned a load of %d!\n\n",
      n, cpuloads[n]);
     free_buffers();
     return -22;
    }
    cpuloads[n] = load;
    ;
   } else if (argv[i][0] == 'd') {
    ret = sscanf(argv[i], "duration=%ld",
     &duration2);
    if ((ret != 1) || (duration2 < 1)) {
     return einval(argv[i]);
    }
    if (duration != -1) {
     fprintf(stderr,
      "cpuloadgen: duration was already set to %ld!\n\n",
      duration);
     free_buffers();
     return -22;
    }
    duration = duration2;
    ;
   } else {
    return einval(argv[i]);
   }
  }
 }
 printf("Press CTRL+C to stop load generation at any time.\n\n");
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   ;
   continue;
  }
  pthread_mutex_lock(&mutex1);
  ret = pthread_create(&threads[i], ((void *)0), thread_loadgen, &i);
  if (ret != 0) {
   fprintf(stderr, "cpuloadgen: failed to fork %d! (%d)",
   i, ret);
   continue;
  }
 }
 for (i = 0; i < cpu_count; i++) {
  if (cpuloads[i] == -1) {
   continue;
  }
  pthread_join(threads[i], ((void *)0));
 }
 free_buffers();
 printf("\ndone.\n\n");
 return 0;
}

variable_types:
argv
char *[]

variable_types: i int

variable_types: n int

variable_types: load int


loadgen

decompiled_code:
__int64 __fastcall loadgen(unsigned int a1, unsigned int a2, int a3)
{
double v4; // r12
double v5; // xmm1_8
__int64 result; // rax
double v7; // [rsp+8h] [rbp-D0h]
cpu_set_t cpuset; // [rsp+10h] [rbp-C8h] BYREF
struct timezone tz; // [rsp+98h] [rbp-40h] BYREF
struct timeval v10; // [rsp+A0h] [rbp-38h] BYREF
struct timeval tv; // [rsp+B0h] [rbp-28h] BYREF

memset(&cpuset, 0, sizeof(cpuset));
if ( a1 <= 0x3FF )
cpuset.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
sched_setaffinity(0, 8uLL, &cpuset);
printf("Generating %3d%% load on CPU%d...\n", a2, a1);
gettimeofday(&tv, &tz);
v7 = (double)SLODWORD(tv.tv_usec) * 0.000001 + (double)SLODWORD(tv.tv_sec);
if ( a2 == 100 )
{
do
{
do
{
dhryStone(1000000);
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (unsigned int)a3;
}
while ( (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7 < (double)a3 );
}
else
{
do
{
do
{
v4 = dtime();
dhryStone(200000);
v5 = (dtime() - v4) * 1000000.0;
usleep((int)(100.0 / (double)(int)(a2 + 1) * v5 - v5));
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (unsigned int)a3;
}
while ( (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7 < (double)a3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, unsigned int a2, int a3)
{
double v4; // r12
double v5; // xmm1_8
__int64 result; // rax
double v7; // [rsp+8h] [rbp-D0h]
cpu_set_t cpuset; // [rsp+10h] [rbp-C8h] BYREF
struct timezone tz; // [rsp+98h] [rbp-40h] BYREF
struct timeval v10; // [rsp+A0h] [rbp-38h] BYREF
struct timeval tv; // [rsp+B0h] [rbp-28h] BYREF

memset(&cpuset, 0, sizeof(cpuset));
if ( a1 <= 0x3FF )
cpuset.__bits[(unsigned __int64)a1 >> 6] |= 1LL << a1;
sched_setaffinity(0, 8uLL, &cpuset);
printf("Generating %3d%% load on CPU%d...\n", a2, a1);
gettimeofday(&tv, &tz);
v7 = (double)SLODWORD(tv.tv_usec) * 0.000001 + (double)SLODWORD(tv.tv_sec);
if ( a2 == 100 )
{
do
{
do
{
func9(1000000);
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (unsigned int)a3;
}
while ( (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7 < (double)a3 );
}
else
{
do
{
do
{
v4 = dtime();
func9(200000);
v5 = (dtime() - v4) * 1000000.0;
usleep((int)(100.0 / (double)(int)(a2 + 1) * v5 - v5));
gettimeofday(&v10, &tz);
}
while ( !a3 );
result = (unsigned int)a3;
}
while ( (double)SLODWORD(v10.tv_usec) * 0.000001 + (double)SLODWORD(v10.tv_sec) - v7 < (double)a3 );
}
return result;
}

original_code:
void loadgen(unsigned int cpu, unsigned int load, unsigned int duration)
{
 double dhrystone_start_time, dhrystone_end_time;
 double idle_time_us;
 double loadgen_start_time_us, active_time_us;
 double total_time_us;
 struct timeval tv_cpuloadgen_start, tv_cpuloadgen;
 struct timeval tv_idle_start, tv_idle_stop;
 struct timezone tz;
 double time_us;
 unsigned long mask;
 unsigned int len = sizeof(mask);
 cpu_set_t set;
 do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0);
 (__extension__ ({ size_t __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
 sched_setaffinity(0, len, &set);
 printf("Generating %3d%% load on CPU%d...\n", load, cpu);
 gettimeofday(&tv_cpuloadgen_start, &tz);
 loadgen_start_time_us = ((double) tv_cpuloadgen_start.tv_sec
  + ((double) tv_cpuloadgen_start.tv_usec * 1.0e-6));
 ;
 if (load != 100) {
  while (1) {
   dhrystone_start_time = dtime();
   dhryStone(200000);
   dhrystone_end_time = dtime();
   active_time_us =
    (dhrystone_end_time - dhrystone_start_time) * 1.0e6;
   ;
   total_time_us =
    active_time_us * (100.0 / (double) (load + 1));
   ;
   idle_time_us = total_time_us - active_time_us;
   ;
   usleep((unsigned int) (idle_time_us));
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 } else {
  while (1) {
   dhryStone(1000000);
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 }
 ;
}

canonical_original_code:
void loadgen(unsigned int cpu, unsigned int load, unsigned int duration)
{
 double dhrystone_start_time; double dhrystone_end_time;
 double idle_time_us;
 double loadgen_start_time_us; double active_time_us;
 double total_time_us;
 struct timeval tv_cpuloadgen_start; struct timeval tv_cpuloadgen;
 struct timeval tv_idle_start; struct timeval tv_idle_stop;
 struct timezone tz;
 double time_us;
 unsigned long mask;
 unsigned int len = sizeof(mask);
 struct cpu_set_t set;
 do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0);
 (__extension__ ({ unsigned long __cpu = (cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((unsigned long *) ((&set)->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((unsigned long) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; }));
 sched_setaffinity(0, len, &set);
 printf("Generating %3d%% load on CPU%d...\n", load, cpu);
 gettimeofday(&tv_cpuloadgen_start, &tz);
 loadgen_start_time_us = ((double) tv_cpuloadgen_start.tv_sec
  + ((double) tv_cpuloadgen_start.tv_usec * 1.0e-6));
 ;
 if (load != 100) {
  while (1) {
   dhrystone_start_time = dtime();
   dhryStone(200000);
   dhrystone_end_time = dtime();
   active_time_us =
    (dhrystone_end_time - dhrystone_start_time) * 1.0e6;
   ;
   total_time_us =
    active_time_us * (100.0 / (double) (load + 1));
   ;
   idle_time_us = total_time_us - active_time_us;
   ;
   usleep((unsigned int) (idle_time_us));
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 } else {
  while (1) {
   dhryStone(1000000);
   gettimeofday(&tv_cpuloadgen, &tz);
   time_us = ((double) tv_cpuloadgen.tv_sec
    + ((double) tv_cpuloadgen.tv_usec * 1.0e-6));
   ;
   if ((duration != 0) &&
    (time_us - loadgen_start_time_us) >= duration)
    break;
  }
 }
 ;
}

variable_types:
cpu
unsigned int

variable_types:
load
unsigned int

variable_types:
duration
unsigned int

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: struct timezone { int tz_minuteswest; int tz_dsttime; }

variable_types:
mask
unsigned long

variable_types:
len
unsigned int

variable_types: struct cpu_set_t { unsigned long __bits[-1]; }


decompiled_code:
void __fastcall __noreturn thread_loadgen(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
pthread_mutex_unlock(&mutex1);
if ( cpu_count <= v1 )
fprintf(stderr, "%s: invalid cpu argument!!! (%d)\n", "thread_loadgen", v1);
else
loadgen(v1, *((_DWORD *)cpuloads + v1), duration);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func11(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
pthread_mutex_unlock(&mutex1);
if ( cpu_count <= v1 )
fprintf(stderr, "%s: invalid cpu argument!!! (%d)\n", "thread_loadgen", v1);
else
func10(v1, *((_DWORD *)cpuloads + v1), duration);
pthread_exit(0LL);
}

original_code:
void *thread_loadgen(void *ptr)
{
 unsigned int cpu;
 cpu = *((unsigned int *) ptr);
 pthread_mutex_unlock(&mutex1);
 if (cpu < cpu_count) {
  loadgen(cpu, cpuloads[cpu], duration);
 } else {
  fprintf(stderr, "%s: invalid cpu argument!!! (%d)\n",
   __func__, cpu);
 }
 pthread_exit(((void *)0));
}

variable_types:
ptr
void *

return_type: void *


dtime

decompiled_code:
double dtime()
{
getrusage(RUSAGE_SELF, &rusage);
return (double)(int)rusage.ru_utime.tv_usec * 0.000001 + (double)SLODWORD(rusage.ru_utime.tv_sec);
}

canonical_decompiled_code:
double func0()
{
getrusage(RUSAGE_SELF, &rusage);
return (double)(int)rusage.ru_utime.tv_usec * 0.000001 + (double)SLODWORD(rusage.ru_utime.tv_sec);
}

original_code:
double dtime()
{
 double q;
 getrusage(RUSAGE_SELF,&rusage);
 q = (double)(rusage.ru_utime.tv_sec);
 q = q + (double)(rusage.ru_utime.tv_usec) * 1.0e-06;
 return q;
}

variable_types: q double

return_type: double


myCloseCB

repo: ptorpman/compframe

decompiled_code:
__int64 __fastcall myCloseCB(const void *a1, unsigned int a2, const void *a3)
{
fprintf(stdout, "Connection is closed! %p chan=%d user=%p\n", a1, a2, a3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, unsigned int a2, const void *a3)
{
fprintf(stdout, "Connection is closed! %p chan=%d user=%p\n", a1, a2, a3);
return 1LL;
}

original_code:
static int
myCloseCB(void* conn, int chan,void* userData)
{
  fprintf(stdout,
          "Connection is closed! %p chan=%d user=%p\n",
          conn, chan, userData);
  return 1;
}

canonical_original_code:
static int myCloseCB(void *conn, int chan, void *userData)
{
  fprintf(stdout,
          "Connection is closed! %p chan=%d user=%p\n",
          conn, chan, userData);
  return 1;
}

variable_types:
conn
void *

variable_types: chan int

variable_types:
userData
void *


myErrorCB

decompiled_code:
void __fastcall __noreturn myErrorCB(const void *a1, const char *a2, const void *a3)
{
const char *v3; // rcx

v3 = a2;
if ( !a2 )
v3 = "NONE";
fprintf(stdout, "Connection has errors! %p msg=%s user=%p\n", a1, v3, a3);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const void *a1, const char *a2, const void *a3)
{
const char *v3; // rcx

v3 = a2;
if ( !a2 )
v3 = "NONE";
fprintf(stdout, "Connection has errors! %p msg=%s user=%p\n", a1, v3, a3);
exit(1);
}

original_code:
static int
myErrorCB(void* conn, char* errorMsg, void* userData)
{
  fprintf(stdout,
          "Connection has errors! %p msg=%s user=%p\n",
          conn,
          (errorMsg ? errorMsg : "NONE"),
          userData);
  exit(1);
}

canonical_original_code:
static int myErrorCB(void *conn, char *errorMsg, void *userData)
{
  fprintf(stdout,
          "Connection has errors! %p msg=%s user=%p\n",
          conn,
          (errorMsg ? errorMsg : "NONE"),
          userData);
  exit(1);
}

variable_types:
errorMsg
char *


myMsgCB

decompiled_code:
void __fastcall __noreturn myMsgCB(const void *a1, unsigned int a2, unsigned int a3, const char *a4, const void *a5)
{
fprintf(stderr, "Got a message! conn=%p chan=%d user=%p len=%d\nMSG: >%s<\n", a1, a2, a5, a3, a4);
cfm_channel_close(a1, a2);
cfm_connection_close(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const void *a1, unsigned int a2, unsigned int a3, const char *a4, const void *a5)
{
fprintf(stderr, "Got a message! conn=%p chan=%d user=%p len=%d\nMSG: >%s<\n", a1, a2, a5, a3, a4);
cfm_channel_close(a1, a2);
cfm_connection_close(a1);
exit(1);
}

original_code:
static int
myMsgCB(void* conn, int chan,int len, unsigned char* msg, void* userData)
{
  fprintf(stderr,
          "Got a message! conn=%p chan=%d user=%p len=%d\n"
          "MSG: >%s<\n",
          conn, chan, userData,len, msg);
  cfm_channel_close(conn,chan);
  cfm_connection_close(conn);
  exit(1);
  return 1;
}

canonical_original_code:
static int myMsgCB(void *conn, int chan, int len, unsigned char *msg, void *userData)
{
  fprintf(stderr,
          "Got a message! conn=%p chan=%d user=%p len=%d\n"
          "MSG: >%s<\n",
          conn, chan, userData,len, msg);
  cfm_channel_close(conn,chan);
  cfm_connection_close(conn);
  exit(1);
  return 1;
}

variable_types: len int

variable_types:
msg
unsigned char *


myOpenCB

decompiled_code:
__int64 __fastcall myOpenCB(const void *a1, unsigned int a2, const void *a3)
{
int v3; // eax
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

fprintf(stdout, "Connection is open! %p chan=%d user=%p\n", a1, a2, a3);
strcpy(v5, "Hello, sample2!\n");
v3 = strlen(v5);
return cfm_message_send(a1, a2, (unsigned int)(v3 + 1), v5);
}

canonical_decompiled_code:
__int64 __fastcall func3(const void *a1, unsigned int a2, const void *a3)
{
int v3; // eax
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

fprintf(stdout, "Connection is open! %p chan=%d user=%p\n", a1, a2, a3);
strcpy(v5, "Hello, sample2!\n");
v3 = strlen(v5);
return cfm_message_send(a1, a2, (unsigned int)(v3 + 1), v5);
}

original_code:
static int
myOpenCB(void* conn, int chan,void* userData)
{
  fprintf(stdout,
          "Connection is open! %p chan=%d user=%p\n",
          conn, chan, userData);
  char msg[256];
  strcpy(msg, "Hello, sample2!\n");
  return cfm_message_send(conn,chan, strlen(msg) + 1, (unsigned char*)msg );
}

canonical_original_code:
static int myOpenCB(void *conn, int chan, void *userData)
{
  fprintf(stdout,
          "Connection is open! %p chan=%d user=%p\n",
          conn, chan, userData);
  char msg[256];
  strcpy(msg, "Hello, sample2!\n");
  return cfm_message_send(conn,chan, strlen(msg) + 1, (unsigned char *)msg );
}

variable_types:
msg
char [256]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // eax
__int64 v5; // rbp
__int64 v6; // rax
struct pollfd v7; // [rsp+0h] [rbp-28h] BYREF

if ( argc == 4 )
{
v4 = atoi(argv[2]);
v5 = cfm_connection_open(argv[1], v4);
if ( v5 )
{
if ( (unsigned int)cfm_channel_open(
v5,
"433e76d0-77d1-460d-9321-e2dc8dc8bd59",
argv[3],
myOpenCB,
myCloseCB,
myMsgCB,
myErrorCB,
0LL) == -1 )
{
fwrite("Channel could not be opened\n", 1uLL, 0x1CuLL, stderr);
cfm_connection_close(v5);
}
if ( (unsigned int)cfm_channel_open(
v5,
"433e76d0-77d1-460d-9321-e2dc8dc8bd59",
argv[3],
myOpenCB,
myCloseCB,
myMsgCB,
myErrorCB,
0LL) == -1 )
{
fwrite("Channel could not be opened\n", 1uLL, 0x1CuLL, stderr);
cfm_connection_close(v5);
}
v7.fd = cfm_connection_sd_get(v5);
v7.events = 1;
do
{
do
poll(&v7, 1uLL, 10);
while ( v7.revents != 1 );
}
while ( (int)cfm_sockets_handle((unsigned int)v7.fd) >= 0 );
v6 = cfm_connection_find((unsigned int)v7.fd);
cfm_connection_close(v6);
fwrite("Info: Connection closed!\n", 1uLL, 0x19uLL, stderr);
}
else
{
fwrite("Connection could not be established\n", 1uLL, 0x24uLL, stderr);
}
}
else
{
fwrite("Usage: sample_m_client <host> <port> <receiver>\n", 1uLL, 0x30uLL, stderr);
}
return 1;
}

original_code:
int main(int argc, char** argv)
{
  struct pollfd fds[2];
  if (argc != 4) {
    fprintf(stderr,"Usage: sample_m_client <host> <port> <receiver>\n");
    return 1;
  }
  void* conn = cfm_connection_open(argv[1],atoi(argv[2]));
  if (!conn) {
    fprintf(stderr,"Connection could not be established\n");
    return 1;
  }
  int channel =
    cfm_channel_open(conn,
                     "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                     argv[3],
                     myOpenCB,
                     myCloseCB,
                     myMsgCB,
                     myErrorCB,
                     ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  channel = cfm_channel_open(conn,
                             "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                             argv[3],
                             myOpenCB,
                             myCloseCB,
                             myMsgCB,
                             myErrorCB,
                             ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  fds[0].fd = cfm_connection_sd_get(conn);
  fds[0].events = 0x001;
  int res;
  while (1) {
    res = poll(fds, 1, 10);
    if (fds[0].revents == 0x001) {
      if (cfm_sockets_handle(fds[0].fd) < 0) {
        conn = cfm_connection_find(fds[0].fd);
        cfm_connection_close(conn);
        fprintf(stderr,"Info: Connection closed!\n");
        return 1;
      }
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct pollfd fds[2];
  if (argc != 4) {
    fprintf(stderr,"Usage: sample_m_client <host> <port> <receiver>\n");
    return 1;
  }
  void *conn = cfm_connection_open(argv[1],atoi(argv[2]));
  if (!conn) {
    fprintf(stderr,"Connection could not be established\n");
    return 1;
  }
  int channel = cfm_channel_open(conn,
                     "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                     argv[3],
                     myOpenCB,
                     myCloseCB,
                     myMsgCB,
                     myErrorCB,
                     ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  channel = cfm_channel_open(conn,
                             "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                             argv[3],
                             myOpenCB,
                             myCloseCB,
                             myMsgCB,
                             myErrorCB,
                             ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  fds[0].fd = cfm_connection_sd_get(conn);
  fds[0].events = 0x001;
  int res;
  while (1) {
    res = poll(fds, 1, 10);
    if (fds[0].revents == 0x001) {
      if (cfm_sockets_handle(fds[0].fd) < 0) {
        conn = cfm_connection_find(fds[0].fd);
        cfm_connection_close(conn);
        fprintf(stderr,"Info: Connection closed!\n");
        return 1;
      }
    }
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

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

variable_types: channel int

variable_types: res int


decompiled_code:
__int64 __fastcall uuid__get_os_time(_DWORD *a1)
{
int v1; // eax
__int64 result; // rax
int v3; // ecx
int v4; // edx
int v5; // [rsp+0h] [rbp-28h] BYREF
int v6; // [rsp+4h] [rbp-24h]
int v7; // [rsp+8h] [rbp-20h] BYREF
int v8; // [rsp+Ch] [rbp-1Ch]
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

if ( gettimeofday(&tv, 0LL) )
{
perror("uuid__get_os_time");
exit(-1);
}
uuid__uemul(LODWORD(tv.tv_sec), 10000000LL, &v7);
uuid__uemul(LODWORD(tv.tv_usec), 10LL, &v5);
if ( (v7 ^ v5) < 0 )
{
v3 = v6 + v8;
v8 += v6;
v1 = v7 + v5;
if ( v7 + v5 >= 0 )
{
v8 = v3 + 1;
LABEL_6:
*a1 = v1 + 327237632;
result = (unsigned int)(v8 + 28450258);
a1[1] = result;
return result;
}
}
else
{
v7 += v5;
if ( v5 < 0 )
v8 += v6 + 1;
else
v8 += v6;
v1 = v7;
if ( v7 >= 0 )
goto LABEL_6;
}
result = (unsigned int)(v1 + 327237632);
*a1 = result;
v4 = v8 + 28450258;
if ( (int)result >= 0 )
v4 = v8 + 28450259;
a1[1] = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
int v1; // eax
__int64 result; // rax
int v3; // ecx
int v4; // edx
int v5; // [rsp+0h] [rbp-28h] BYREF
int v6; // [rsp+4h] [rbp-24h]
int v7; // [rsp+8h] [rbp-20h] BYREF
int v8; // [rsp+Ch] [rbp-1Ch]
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

if ( gettimeofday(&tv, 0LL) )
{
perror("uuid__get_os_time");
exit(-1);
}
uuid__uemul(LODWORD(tv.tv_sec), 10000000LL, &v7);
uuid__uemul(LODWORD(tv.tv_usec), 10LL, &v5);
if ( (v7 ^ v5) < 0 )
{
v3 = v6 + v8;
v8 += v6;
v1 = v7 + v5;
if ( v7 + v5 >= 0 )
{
v8 = v3 + 1;
LABEL_6:
*a1 = v1 + 327237632;
result = (unsigned int)(v8 + 28450258);
a1[1] = result;
return result;
}
}
else
{
v7 += v5;
if ( v5 < 0 )
v8 += v6 + 1;
else
v8 += v6;
v1 = v7;
if ( v7 >= 0 )
goto LABEL_6;
}
result = (unsigned int)(v1 + 327237632);
*a1 = result;
v4 = v8 + 28450258;
if ( (int)result >= 0 )
v4 = v8 + 28450259;
a1[1] = v4;
return result;
}

original_code:
void uuid__get_os_time (uuid_time_t * uuid_time)
{
    struct timeval tp;
    unsigned64_t utc,
                        usecs,
                        os_basetime_diff;
    if (gettimeofday (&tp, (struct timezone *) 0))
    {
        perror ("uuid__get_os_time");
        exit (-1);
    }
    uuid__uemul ((long) tp.tv_sec, 10000000, &utc);
    uuid__uemul ((long) tp.tv_usec, 10, &usecs);
    if (!(((&usecs)->lo&0x80000000UL) ^ ((&utc)->lo&0x80000000UL))) { if (((&usecs)->lo&0x80000000UL)) { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi+1 ; } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; } } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; if (!((&utc)->lo&0x80000000UL)) (&utc)->hi++ ; };
    os_basetime_diff.lo = 0x13814000;
    os_basetime_diff.hi = 0x01B21DD2;
    if (!(((&utc)->lo&0x80000000UL) ^ ((&os_basetime_diff)->lo&0x80000000UL))) { if (((&utc)->lo&0x80000000UL)) { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi+1 ; } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; } } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; if (!((uuid_time)->lo&0x80000000UL)) (uuid_time)->hi++ ; };
}

canonical_original_code:
void uuid__get_os_time(struct uuid_time_t *uuid_time)
{
    struct timeval tp;
    struct unsigned64_t utc; struct unsigned64_t usecs; struct unsigned64_t os_basetime_diff;
    if (gettimeofday (&tp, (struct timezone *) 0))
    {
        perror ("uuid__get_os_time");
        exit (-1);
    }
    uuid__uemul ((long) tp.tv_sec, 10000000, &utc);
    uuid__uemul ((long) tp.tv_usec, 10, &usecs);
    if (!(((&usecs)->lo&0x80000000UL) ^ ((&utc)->lo&0x80000000UL))) { if (((&usecs)->lo&0x80000000UL)) { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi+1 ; } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; } } else { (&utc)->lo = (&usecs)->lo + (&utc)->lo ; (&utc)->hi = (&usecs)->hi + (&utc)->hi ; if (!((&utc)->lo&0x80000000UL)) (&utc)->hi++ ; };
    os_basetime_diff.lo = 0x13814000;
    os_basetime_diff.hi = 0x01B21DD2;
    if (!(((&utc)->lo&0x80000000UL) ^ ((&os_basetime_diff)->lo&0x80000000UL))) { if (((&utc)->lo&0x80000000UL)) { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi+1 ; } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; } } else { (uuid_time)->lo = (&utc)->lo + (&os_basetime_diff)->lo ; (uuid_time)->hi = (&utc)->hi + (&os_basetime_diff)->hi ; if (!((uuid_time)->lo&0x80000000UL)) (uuid_time)->hi++ ; };
}

variable_types: struct uuid_time_t { unsigned int lo; unsigned int hi; } *

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: struct unsigned64_t { unsigned int lo; unsigned int hi; }

user_defined_types: struct timezone { int tz_minuteswest; int tz_dsttime; }


decompiled_code:
__pid_t uuid__get_os_pid()
{
return getpid();
}

canonical_decompiled_code:
__pid_t func1()
{
return getpid();
}

original_code:
unsigned32 uuid__get_os_pid ( void )
{
    return ((unsigned32) getpid());
}

canonical_original_code:
unsigned int uuid__get_os_pid()
{
    return ((unsigned int) getpid());
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall cfm_control_msg_handle(__int64 a1)
{
unsigned __int8 v1; // dl
__int64 v2; // rax
unsigned int v3; // ebp
unsigned int v4; // ebx
_QWORD *v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD); // rcx
__int64 v8; // rax
__int64 v9; // rsi
__int64 v10; // rax

v1 = *(_BYTE *)(*(_QWORD *)(a1 + 2096) + 1LL);
switch ( v1 )
{
case 3u:
fwrite("DEBUG: CF_M_CHANNEL_OPEN_OK.\n", 1uLL, 0x1DuLL, stderr);
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( !*(_QWORD *)(a1 + 8 * v2 + 40) )
break;
if ( ++v2 == 255 )
{
v4 = callback_error(a1, "FAILED!", curr_user_data);
callback_open = 0LL;
callback_close = 0LL;
callback_msg = 0LL;
callback_error = 0LL;
curr_user_data = 0LL;
return v4;
}
}
v5 = malloc(0x38uLL);
*(_QWORD *)(a1 + 8LL * (int)v3 + 40) = v5;
v6 = (__int64 (__fastcall *)(__int64, _QWORD))callback_open;
v5[2] = callback_open;
v5[3] = callback_close;
v5[5] = callback_msg;
v5[4] = callback_error;
*((_DWORD *)v5 + 1) = v3;
v5[6] = curr_user_data;
v4 = v6(a1, v3);
callback_open = 0LL;
callback_close = 0LL;
callback_msg = 0LL;
callback_error = 0LL;
curr_user_data = 0LL;
break;
case 4u:
fwrite("DEBUG: CF_M_CHANNEL_OPEN_FAIL.\n", 1uLL, 0x1FuLL, stderr);
v4 = callback_error(a1, "FAILED!", curr_user_data);
callback_open = 0LL;
callback_close = 0LL;
callback_msg = 0LL;
callback_error = 0LL;
curr_user_data = 0LL;
break;
case 5u:
v8 = *(_QWORD *)(a1 + 8LL * channel_being_closed + 40);
v4 = (*(__int64 (__fastcall **)(__int64, _QWORD, _QWORD))(v8 + 24))(
a1,
(unsigned int)channel_being_closed,
*(_QWORD *)(v8 + 48));
channel_being_closed = -1;
fwrite("DEBUG: CF_M_CHANNEL_CLOSE_OK.\n", 1uLL, 0x1EuLL, stderr);
break;
case 6u:
fwrite("DEBUG: CF_M_CHANNEL_CLOSE_FAIL.\n", 1uLL, 0x20uLL, stderr);
v9 = *(unsigned int *)(a1 + 2088);
v10 = *(_QWORD *)(a1 + 8LL * (int)v9 + 40);
v4 = (*(__int64 (__fastcall **)(__int64, __int64, _QWORD))(v10 + 24))(a1, v9, *(_QWORD *)(v10 + 48));
break;
case 7u:
fwrite("DEBUG: CF_M_CHANNEL_ORDER_UNKNOWN.\n", 1uLL, 0x23uLL, stderr);
v4 = 1;
break;
default:
fprintf(stderr, "Error: Unknown message.(%u)\n", v1);
v4 = 1;
break;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned __int8 v1; // dl
__int64 v2; // rax
unsigned int v3; // ebp
unsigned int v4; // ebx
_QWORD *v5; // rax
__int64 (__fastcall *v6)(__int64, _QWORD); // rcx
__int64 v8; // rax
__int64 v9; // rsi
__int64 v10; // rax

v1 = *(_BYTE *)(*(_QWORD *)(a1 + 2096) + 1LL);
switch ( v1 )
{
case 3u:
fwrite("DEBUG: CF_M_CHANNEL_OPEN_OK.\n", 1uLL, 0x1DuLL, stderr);
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( !*(_QWORD *)(a1 + 8 * v2 + 40) )
break;
if ( ++v2 == 255 )
{
v4 = callback_error(a1, "FAILED!", curr_user_data);
callback_open = 0LL;
callback_close = 0LL;
callback_msg = 0LL;
callback_error = 0LL;
curr_user_data = 0LL;
return v4;
}
}
v5 = malloc(0x38uLL);
*(_QWORD *)(a1 + 8LL * (int)v3 + 40) = v5;
v6 = (__int64 (__fastcall *)(__int64, _QWORD))callback_open;
v5[2] = callback_open;
v5[3] = callback_close;
v5[5] = callback_msg;
v5[4] = callback_error;
*((_DWORD *)v5 + 1) = v3;
v5[6] = curr_user_data;
v4 = v6(a1, v3);
callback_open = 0LL;
callback_close = 0LL;
callback_msg = 0LL;
callback_error = 0LL;
curr_user_data = 0LL;
break;
case 4u:
fwrite("DEBUG: CF_M_CHANNEL_OPEN_FAIL.\n", 1uLL, 0x1FuLL, stderr);
v4 = callback_error(a1, "FAILED!", curr_user_data);
callback_open = 0LL;
callback_close = 0LL;
callback_msg = 0LL;
callback_error = 0LL;
curr_user_data = 0LL;
break;
case 5u:
v8 = *(_QWORD *)(a1 + 8LL * channel_being_closed + 40);
v4 = (*(__int64 (__fastcall **)(__int64, _QWORD, _QWORD))(v8 + 24))(
a1,
(unsigned int)channel_being_closed,
*(_QWORD *)(v8 + 48));
channel_being_closed = -1;
fwrite("DEBUG: CF_M_CHANNEL_CLOSE_OK.\n", 1uLL, 0x1EuLL, stderr);
break;
case 6u:
fwrite("DEBUG: CF_M_CHANNEL_CLOSE_FAIL.\n", 1uLL, 0x20uLL, stderr);
v9 = *(unsigned int *)(a1 + 2088);
v10 = *(_QWORD *)(a1 + 8LL * (int)v9 + 40);
v4 = (*(__int64 (__fastcall **)(__int64, __int64, _QWORD))(v10 + 24))(a1, v9, *(_QWORD *)(v10 + 48));
break;
case 7u:
fwrite("DEBUG: CF_M_CHANNEL_ORDER_UNKNOWN.\n", 1uLL, 0x23uLL, stderr);
v4 = 1;
break;
default:
fprintf(stderr, "Error: Unknown message.(%u)\n", v1);
v4 = 1;
break;
}
return v4;
}

original_code:
static int
cfm_control_msg_handle(void *c)
{
    cfm_conn_t *conn = (cfm_conn_t *) c;
    int res;
    int slot = 0;
    switch (conn->msg_buff[1]) {
    case 3:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_OPEN_OK.\n");
        for (slot = 0; slot < 255; slot++) {
            if (conn->peer[slot] == ((void *)0)) {
                conn->peer[slot] = malloc(sizeof(cfm_peer_t));
                conn->peer[slot]->callback_open = callback_open;
                conn->peer[slot]->callback_close = callback_close;
                conn->peer[slot]->callback_msg = callback_msg;
                conn->peer[slot]->callback_error = callback_error;
                conn->peer[slot]->channel = slot;
                conn->peer[slot]->userData = curr_user_data;
                res = callback_open(conn, slot, curr_user_data);
                callback_open = ((void *)0);
                callback_close = ((void *)0);
                callback_msg = ((void *)0);
                callback_error = ((void *)0);
                curr_user_data = ((void *)0);
                return res;
            }
        }
        res = callback_error(conn, "FAILED!", curr_user_data);
        callback_open = ((void *)0);
        callback_close = ((void *)0);
        callback_msg = ((void *)0);
        callback_error = ((void *)0);
        curr_user_data = ((void *)0);
        return res;
    case 4:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_OPEN_FAIL.\n");
        res = callback_error(conn, "FAILED!", curr_user_data);
        callback_open = ((void *)0);
        callback_close = ((void *)0);
        callback_msg = ((void *)0);
        callback_error = ((void *)0);
        curr_user_data = ((void *)0);
        return res;
    case 5:
        res = conn->peer[channel_being_closed]->callback_close(conn,
                                                               channel_being_closed,
                                                               conn->
                                                               peer
                                                               [channel_being_closed]->
                                                               userData);
        channel_being_closed = -1;
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_CLOSE_OK.\n");
        return res;
    case 6:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_CLOSE_FAIL.\n");
        return conn->peer[conn->channel]->callback_close(conn,
                                                         conn->channel,
                                                         conn->peer[conn->
                                                                    channel]->
                                                         userData);
        break;
    case 7:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_ORDER_UNKNOWN.\n");
        break;
    default:
        fprintf(stderr, "Error: Unknown message.(%u)\n", conn->msg_buff[1]);
        break;
    }
    return 1;
}

canonical_original_code:
static int cfm_control_msg_handle(void *c)
{
    struct cfm_conn_t *conn = (cfm_conn_t *) c;
    int res;
    int slot = 0;
    switch (conn->msg_buff[1]) {
    case 3:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_OPEN_OK.\n");
        for (slot = 0; slot < 255; slot++) {
            if (conn->peer[slot] == ((void *)0)) {
                conn->peer[slot] = malloc(sizeof(cfm_peer_t));
                conn->peer[slot]->callback_open = callback_open;
                conn->peer[slot]->callback_close = callback_close;
                conn->peer[slot]->callback_msg = callback_msg;
                conn->peer[slot]->callback_error = callback_error;
                conn->peer[slot]->channel = slot;
                conn->peer[slot]->userData = curr_user_data;
                res = callback_open(conn, slot, curr_user_data);
                callback_open = ((void *)0);
                callback_close = ((void *)0);
                callback_msg = ((void *)0);
                callback_error = ((void *)0);
                curr_user_data = ((void *)0);
                return res;
            }
        }
        res = callback_error(conn, "FAILED!", curr_user_data);
        callback_open = ((void *)0);
        callback_close = ((void *)0);
        callback_msg = ((void *)0);
        callback_error = ((void *)0);
        curr_user_data = ((void *)0);
        return res;
    case 4:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_OPEN_FAIL.\n");
        res = callback_error(conn, "FAILED!", curr_user_data);
        callback_open = ((void *)0);
        callback_close = ((void *)0);
        callback_msg = ((void *)0);
        callback_error = ((void *)0);
        curr_user_data = ((void *)0);
        return res;
    case 5:
        res = conn->peer[channel_being_closed]->callback_close(conn,
                                                               channel_being_closed,
                                                               conn->
                                                               peer
                                                               [channel_being_closed]->
                                                               userData);
        channel_being_closed = -1;
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_CLOSE_OK.\n");
        return res;
    case 6:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_CLOSE_FAIL.\n");
        return conn->peer[conn->channel]->callback_close(conn,
                                                         conn->channel,
                                                         conn->peer[conn->
                                                                    channel]->
                                                         userData);
        break;
    case 7:
        fprintf(stderr, "DEBUG: CF_M_CHANNEL_ORDER_UNKNOWN.\n");
        break;
    default:
        fprintf(stderr, "Error: Unknown message.(%u)\n", conn->msg_buff[1]);
        break;
    }
    return 1;
}

variable_types:
c
void *

variable_types: struct cfm_conn_t { struct cfm_conn_t *next; struct cfm_conn_t *prev; char *host; int port; int socket_fd; unsigned char num_used; struct cfm_peer_t { int sd; int channel; void *localReceiver; int (*callback_open)(void *conn, int chan, void *userData); int (*callback_close)(void *conn, int chan, void *userData); int (*callback_error)(void *conn, char *errorMsg, void *userData); int (*callback_msg)(void *conn, int chan, int len, unsigned char *msg, void *userData); void *userData; } *peer[255]; enum cfm_conn_state_t { CFM_CONN_INIT=0, CFM_CONN_CHANNEL=1, CFM_CONN_LENGTH1=2, CFM_CONN_LENGTH2=3, CFM_CONN_MSGBODY=4, CFM_CONN_MSGREADY=5 } state; int isM; int channel; int msg_len; unsigned char *msg_buff; int msg_pos; } *

variable_types: slot int

user_defined_types: struct cfm_conn_t { struct cfm_conn_t *next; struct cfm_conn_t *prev; char *host; int port; int socket_fd; unsigned char num_used; struct cfm_peer_t *peer[255]; enum cfm_conn_state_t state; int isM; int channel; int msg_len; unsigned char *msg_buff; int msg_pos; }


decompiled_code:
const char **__fastcall cfm_connection_open(char *cp, int a2)
{
const char **v2; // rbx
struct hostent *v3; // rax
int v4; // eax
int v5; // er13
__int64 v6; // rax
struct sockaddr v8; // [rsp+0h] [rbp-48h] BYREF
int optval; // [rsp+18h] [rbp-30h] BYREF
struct in_addr inp; // [rsp+1Ch] [rbp-2Ch] BYREF

if ( cp && a2 >= 0 )
{
v2 = (const char **)connHead;
if ( connHead )
{
while ( strcmp(v2[2], cp) || *((_DWORD *)v2 + 6) != a2 )
{
v2 = (const char **)*v2;
if ( !v2 )
goto LABEL_4;
}
}
else
{
LABEL_4:
v3 = gethostbyname(cp);
v2 = (const char **)&v3->h_name;
if ( v3 )
{
inp.s_addr = **(_DWORD **)v3->h_addr_list;
}
else if ( !inet_aton(cp, &inp) )
{
fprintf(stderr, "ERROR: Unknown host ($%s)\n", cp);
return v2;
}
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 < 0 )
{
fwrite("ERROR: Could not open socket!\n", 1uLL, 0x1EuLL, stderr);
v2 = 0LL;
}
else
{
optval = 1;
setsockopt(v4, 1, 2, &optval, 4u);
v8.sa_family = 2;
*(struct in_addr *)&v8.sa_data[2] = inp;
*(_WORD *)v8.sa_data = _bswap_16(a2);
*(_QWORD *)&v8.sa_data[6] = 0LL;
if ( connect(v5, &v8, 0x10u) == -1 )
{
fwrite("ERROR: Could not connect socket!\n", 1uLL, 0x21uLL, stderr);
v2 = 0LL;
}
else
{
v2 = (const char **)malloc(0x840uLL);
memset(v2, 0, 0x840uLL);
*((_DWORD *)v2 + 7) = v5;
v2[2] = strdup(cp);
*((_DWORD *)v2 + 6) = a2;
v6 = connHead;
*v2 = (const char *)connHead;
if ( v6 )
*(_QWORD *)(v6 + 8) = v2;
connHead = (__int64)v2;
}
}
}
}
else
{
fwrite("ERROR: Bad parameters\n", 1uLL, 0x16uLL, stderr);
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func2(char *cp, int a2)
{
const char **v2; // rbx
struct hostent *v3; // rax
int v4; // eax
int v5; // er13
__int64 v6; // rax
struct sockaddr v8; // [rsp+0h] [rbp-48h] BYREF
int optval; // [rsp+18h] [rbp-30h] BYREF
struct in_addr inp; // [rsp+1Ch] [rbp-2Ch] BYREF

if ( cp && a2 >= 0 )
{
v2 = (const char **)connHead;
if ( connHead )
{
while ( strcmp(v2[2], cp) || *((_DWORD *)v2 + 6) != a2 )
{
v2 = (const char **)*v2;
if ( !v2 )
goto LABEL_4;
}
}
else
{
LABEL_4:
v3 = gethostbyname(cp);
v2 = (const char **)&v3->h_name;
if ( v3 )
{
inp.s_addr = **(_DWORD **)v3->h_addr_list;
}
else if ( !inet_aton(cp, &inp) )
{
fprintf(stderr, "ERROR: Unknown host ($%s)\n", cp);
return v2;
}
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 < 0 )
{
fwrite("ERROR: Could not open socket!\n", 1uLL, 0x1EuLL, stderr);
v2 = 0LL;
}
else
{
optval = 1;
setsockopt(v4, 1, 2, &optval, 4u);
v8.sa_family = 2;
*(struct in_addr *)&v8.sa_data[2] = inp;
*(_WORD *)v8.sa_data = func1(a2);
*(_QWORD *)&v8.sa_data[6] = 0LL;
if ( connect(v5, &v8, 0x10u) == -1 )
{
fwrite("ERROR: Could not connect socket!\n", 1uLL, 0x21uLL, stderr);
v2 = 0LL;
}
else
{
v2 = (const char **)malloc(0x840uLL);
memset(v2, 0, 0x840uLL);
*((_DWORD *)v2 + 7) = v5;
v2[2] = strdup(cp);
*((_DWORD *)v2 + 6) = a2;
v6 = connHead;
*v2 = (const char *)connHead;
if ( v6 )
*(_QWORD *)(v6 + 8) = v2;
connHead = (__int64)v2;
}
}
}
}
else
{
fwrite("ERROR: Bad parameters\n", 1uLL, 0x16uLL, stderr);
v2 = 0LL;
}
return v2;
}

original_code:
void *
cfm_connection_open(char *host, int port)
{
    if (!host || port < 0) {
        fprintf(stderr, "ERROR: Bad parameters\n");
        return ((void *)0);
    }
    if (connHead) {
        for (cfm_conn_t * tmp = connHead; tmp != ((void *)0); tmp = tmp->next) {
            if (strcmp(tmp->host, host) == 0 && tmp->port == port) {
                return tmp;
            }
        }
    }
    struct hostent *he;
    struct in_addr inAddr;
    he = gethostbyname(host);
    if (!he) {
        if (inet_aton(host, &inAddr) == 0) {
            fprintf(stderr, "ERROR: Unknown host ($%s)\n", host);
            return ((void *)0);
        }
    }
    else {
        inAddr = *((struct in_addr *) he->h_addr_list[0]);
    }
    int sd = socket(2, SOCK_STREAM, 0);
    if (sd < 0) {
        fprintf(stderr, "ERROR: Could not open socket!\n");
        return ((void *)0);
    }
    int reuse = 1;
    setsockopt(sd, 1, 2, (char *) &reuse, sizeof(reuse));
    struct sockaddr_in sa;
    sa.sin_family = 2;
    sa.sin_addr = inAddr;
    sa.sin_port = htons(port);
    memset(&(sa.sin_zero), '\0', 8);
    if (connect(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr)) == -1) {
        fprintf(stderr, "ERROR: Could not connect socket!\n");
        return ((void *)0);
    }
    cfm_conn_t *c = malloc(sizeof(cfm_conn_t));
    memset(c, 0, sizeof(cfm_conn_t));
    c->socket_fd = sd;
    c->host = strdup(host);
    c->port = port;
    do { (c)->prev = ((void *)0); (c)->next = (connHead); if ((connHead)) { (connHead)->prev = (c); } (connHead) = (c); } while (0);
    return c;
}

canonical_original_code:
void *cfm_connection_open(char *host, int port)
{
    if (!host || port < 0) {
        fprintf(stderr, "ERROR: Bad parameters\n");
        return ((void *)0);
    }
    if (connHead) {
        for (struct cfm_conn_t *tmp = connHead; tmp != ((void *)0); tmp = tmp->next) {
            if (strcmp(tmp->host, host) == 0 && tmp->port == port) {
                return tmp;
            }
        }
    }
    struct hostent *he;
    struct in_addr inAddr;
    he = gethostbyname(host);
    if (!he) {
        if (inet_aton(host, &inAddr) == 0) {
            fprintf(stderr, "ERROR: Unknown host ($%s)\n", host);
            return ((void *)0);
        }
    }
    else {
        inAddr = *((struct in_addr *) he->h_addr_list[0]);
    }
    int sd = socket(2, SOCK_STREAM, 0);
    if (sd < 0) {
        fprintf(stderr, "ERROR: Could not open socket!\n");
        return ((void *)0);
    }
    int reuse = 1;
    setsockopt(sd, 1, 2, (char *) &reuse, sizeof(reuse));
    struct sockaddr_in sa;
    sa.sin_family = 2;
    sa.sin_addr = inAddr;
    sa.sin_port = htons(port);
    memset(&(sa.sin_zero), '\0', 8);
    if (connect(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr)) == -1) {
        fprintf(stderr, "ERROR: Could not connect socket!\n");
        return ((void *)0);
    }
    struct cfm_conn_t *c = malloc(sizeof(cfm_conn_t));
    memset(c, 0, sizeof(cfm_conn_t));
    c->socket_fd = sd;
    c->host = strdup(host);
    c->port = port;
    do { (c)->prev = ((void *)0); (c)->next = (connHead); if ((connHead)) { (connHead)->prev = (c); } (connHead) = (c); } while (0);
    return c;
}

variable_types:
host
char *

variable_types: port int

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

variable_types:
inAddr
struct in_addr { unsigned int s_addr; }

variable_types: sd int

variable_types: reuse int

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

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 sockaddr { unsigned short sa_family; char sa_data[14]; }

user_defined_types: enum cfm_conn_state_t { CFM_CONN_INIT=0, CFM_CONN_CHANNEL=1, CFM_CONN_LENGTH1=2, CFM_CONN_LENGTH2=3, CFM_CONN_MSGBODY=4, CFM_CONN_MSGREADY=5 }

user_defined_types: struct cfm_peer_t { int sd; int channel; void *localReceiver; int (*callback_open)(void *conn, int chan, void *userData); int (*callback_close)(void *conn, int chan, void *userData); int (*callback_error)(void *conn, char *errorMsg, void *userData); int (*callback_msg)(void *conn, int chan, int len, unsigned char *msg, void *userData); void *userData; }


decompiled_code:
__int64 __fastcall cfm_connection_sd_get(__int64 a1)
{
return *(unsigned int *)(a1 + 28);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return *(unsigned int *)(a1 + 28);
}

original_code:
int
cfm_connection_sd_get(void *conn)
{
    return ((cfm_conn_t *) conn)->socket_fd;
}

canonical_original_code:
int cfm_connection_sd_get(void *conn)
{
    return ((struct cfm_conn_t *) conn)->socket_fd;
}

variable_types:
conn
void *


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

for ( result = connHead; result; result = *(_QWORD *)result )
{
if ( *(_DWORD *)(result + 28) == a1 )
break;
}
return result;
}

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

for ( result = connHead; result; result = *(_QWORD *)result )
{
if ( *(_DWORD *)(result + 28) == a1 )
break;
}
return result;
}

original_code:
void *
cfm_connection_find(int sd)
{
    cfm_conn_t *c;
    for (c = connHead; c != ((void *)0); c = c->next) {
        if (c->socket_fd == sd) {
            return c;
        }
    }
    return ((void *)0);
}

canonical_original_code:
void *cfm_connection_find(int sd)
{
    struct cfm_conn_t *c;
    for (c = connHead; c != ((void *)0); c = c->next) {
        if (c->socket_fd == sd) {
            return c;
        }
    }
    return ((void *)0);
}


decompiled_code:
__int64 __fastcall cfm_connection_close(void *ptr)
{
__int64 *v1; // rax
__int64 v2; // rdx
__int64 i; // rbx
__int64 v4; // rax
__int64 v5; // rax

if ( !connHead )
return 0LL;
if ( !ptr )
return 0LL;
v1 = (__int64 *)*((_QWORD *)ptr + 1);
if ( v1 )
{
v2 = *(_QWORD *)ptr;
if ( *(_QWORD *)ptr )
{
*v1 = v2;
*(_QWORD *)(v2 + 8) = *((_QWORD *)ptr + 1);
}
else
{
*v1 = 0LL;
}
}
else
{
v4 = *(_QWORD *)ptr;
connHead = v4;
if ( v4 )
*(_QWORD *)(v4 + 8) = 0LL;
}
for ( i = 0LL; i != 255; ++i )
{
v5 = *((_QWORD *)ptr + i + 5);
if ( v5 )
{
(*(void (__fastcall **)(void *, _QWORD, _QWORD))(v5 + 24))(ptr, (unsigned int)i, *(_QWORD *)(v5 + 48));
free(*((void **)ptr + i + 5));
*((_QWORD *)ptr + i + 5) = 0LL;
}
}
shutdown(*((_DWORD *)ptr + 7), 2);
free(ptr);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *ptr)
{
__int64 *v1; // rax
__int64 v2; // rdx
__int64 i; // rbx
__int64 v4; // rax
__int64 v5; // rax

if ( !connHead )
return 0LL;
if ( !ptr )
return 0LL;
v1 = (__int64 *)*((_QWORD *)ptr + 1);
if ( v1 )
{
v2 = *(_QWORD *)ptr;
if ( *(_QWORD *)ptr )
{
*v1 = v2;
*(_QWORD *)(v2 + 8) = *((_QWORD *)ptr + 1);
}
else
{
*v1 = 0LL;
}
}
else
{
v4 = *(_QWORD *)ptr;
connHead = v4;
if ( v4 )
*(_QWORD *)(v4 + 8) = 0LL;
}
for ( i = 0LL; i != 255; ++i )
{
v5 = *((_QWORD *)ptr + i + 5);
if ( v5 )
{
(*(void (__fastcall **)(void *, _QWORD, _QWORD))(v5 + 24))(ptr, (unsigned int)i, *(_QWORD *)(v5 + 48));
free(*((void **)ptr + i + 5));
*((_QWORD *)ptr + i + 5) = 0LL;
}
}
shutdown(*((_DWORD *)ptr + 7), 2);
free(ptr);
return 1LL;
}

original_code:
int
cfm_connection_close(void *c)
{
    cfm_conn_t *conn = (cfm_conn_t *) c;
    if (!connHead || !conn) {
        return 0;
    }
    do { if ((conn)->prev == ((void *)0)) { (connHead) = (conn)->next; if ((connHead)) { (connHead)->prev = ((void *)0); } } else if ((conn)->next == ((void *)0)) { (conn)->prev->next = ((void *)0); } else { (conn)->prev->next = (conn)->next; (conn)->next->prev = (conn)->prev; } } while (0);
    for (int i = 0; i < 255; i++) {
        if (conn->peer[i] == ((void *)0)) {
            continue;
        }
        conn->peer[i]->callback_close(conn, i, conn->peer[i]->userData);
        free(conn->peer[i]);
        conn->peer[i] = ((void *)0);
    }
    shutdown(conn->socket_fd, SHUT_RDWR);
    free(conn);
    return 1;
}

canonical_original_code:
int cfm_connection_close(void *c)
{
    struct cfm_conn_t *conn = (cfm_conn_t *) c;
    if (!connHead || !conn) {
        return 0;
    }
    do { if ((conn)->prev == ((void *)0)) { (connHead) = (conn)->next; if ((connHead)) { (connHead)->prev = ((void *)0); } } else if ((conn)->next == ((void *)0)) { (conn)->prev->next = ((void *)0); } else { (conn)->prev->next = (conn)->next; (conn)->next->prev = (conn)->prev; } } while (0);
    for (int i = 0; i < 255; i++) {
        if (conn->peer[i] == ((void *)0)) {
            continue;
        }
        conn->peer[i]->callback_close(conn, i, conn->peer[i]->userData);
        free(conn->peer[i]);
        conn->peer[i] = ((void *)0);
    }
    shutdown(conn->socket_fd, SHUT_RDWR);
    free(conn);
    return 1;
}

variable_types: i int


decompiled_code:
__int64 __fastcall cfm_channel_open(__int64 a1, const __m128i *a2, const char *a3, __int64 a4, __int64 a5, __int64 a6, __int64 (__fastcall *a7)(_QWORD, _QWORD, _QWORD), __int64 a8)
{
_QWORD *v11; // rax
int v12; // eax
int v13; // er15
size_t v14; // rax
int v15; // edx
__int64 result; // rax
char v18; // [rsp+10h] [rbp-838h] BYREF
__int16 v19; // [rsp+11h] [rbp-837h]
char v20; // [rsp+13h] [rbp-835h]
__m128i v21; // [rsp+14h] [rbp-834h]
__m128i v22; // [rsp+24h] [rbp-824h]
int v23; // [rsp+34h] [rbp-814h]
char dest[2064]; // [rsp+38h] [rbp-810h] BYREF

if ( !connHead )
return 0LL;
if ( !a1 || !a3 )
return 0LL;
fprintf(stderr, "DEBUG: Open channel to %s (%s)..\n", a3, a2->m128i_i8);
if ( callback_open )
{
fwrite("Error: Connection in progress. Please, try later..\n", 1uLL, 0x33uLL, stderr);
return 0LL;
}
v11 = (_QWORD *)connHead;
if ( !connHead )
goto LABEL_12;
if ( a1 != connHead )
{
while ( 1 )
{
v11 = (_QWORD *)*v11;
if ( !v11 )
break;
if ( (_QWORD *)a1 == v11 )
goto LABEL_9;
}
LABEL_12:
fwrite("Error: Connection not found!\n", 1uLL, 0x1DuLL, stderr);
return 0LL;
}
LABEL_9:
v12 = strlen(a3);
v13 = v12 + 41;
v18 = -1;
v19 = v12 + 41;
v20 = 0;
v21 = _mm_loadu_si128(a2);
v22 = _mm_loadu_si128(a2 + 1);
v23 = a2[2].m128i_i32[0];
v14 = strlen(a3);
memcpy(dest, a3, v14 + 1);
v15 = write(*(_DWORD *)(a1 + 28), &v18, v13);
result = 0LL;
if ( v15 > 0 )
{
callback_open = a4;
callback_close = a5;
callback_msg = a6;
callback_error = a7;
curr_user_data = a8;
fwrite("DEBUG: Sent CF_M_CHANNEL_OPEN !\n", 1uLL, 0x20uLL, stderr);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, const __m128i *a2, const char *a3, __int64 a4, __int64 a5, __int64 a6, __int64 (__fastcall *a7)(_QWORD, _QWORD, _QWORD), __int64 a8)
{
_QWORD *v11; // rax
int v12; // eax
int v13; // er15
size_t v14; // rax
int v15; // edx
__int64 result; // rax
char v18; // [rsp+10h] [rbp-838h] BYREF
__int16 v19; // [rsp+11h] [rbp-837h]
char v20; // [rsp+13h] [rbp-835h]
__m128i v21; // [rsp+14h] [rbp-834h]
__m128i v22; // [rsp+24h] [rbp-824h]
int v23; // [rsp+34h] [rbp-814h]
char dest[2064]; // [rsp+38h] [rbp-810h] BYREF

if ( !connHead )
return 0LL;
if ( !a1 || !a3 )
return 0LL;
fprintf(stderr, "DEBUG: Open channel to %s (%s)..\n", a3, a2->m128i_i8);
if ( callback_open )
{
fwrite("Error: Connection in progress. Please, try later..\n", 1uLL, 0x33uLL, stderr);
return 0LL;
}
v11 = (_QWORD *)connHead;
if ( !connHead )
goto LABEL_12;
if ( a1 != connHead )
{
while ( 1 )
{
v11 = (_QWORD *)*v11;
if ( !v11 )
break;
if ( (_QWORD *)a1 == v11 )
goto LABEL_9;
}
LABEL_12:
fwrite("Error: Connection not found!\n", 1uLL, 0x1DuLL, stderr);
return 0LL;
}
LABEL_9:
v12 = strlen(a3);
v13 = v12 + 41;
v18 = -1;
v19 = v12 + 41;
v20 = 0;
v21 = _mm_loadu_si128(a2);
v22 = _mm_loadu_si128(a2 + 1);
v23 = a2[2].m128i_i32[0];
v14 = strlen(a3);
memcpy(dest, a3, v14 + 1);
v15 = write(*(_DWORD *)(a1 + 28), &v18, v13);
result = 0LL;
if ( v15 > 0 )
{
callback_open = a4;
callback_close = a5;
callback_msg = a6;
callback_error = a7;
curr_user_data = a8;
fwrite("DEBUG: Sent CF_M_CHANNEL_OPEN !\n", 1uLL, 0x20uLL, stderr);
result = 1LL;
}
return result;
}

original_code:
int
cfm_channel_open(void *cx,
                 const char *uiid,
                 char *name,
                 cfm_callback_open_t openCB,
                 cfm_callback_close_t closeCB,
                 cfm_callback_msg_t msgCB,
                 cfm_callback_error_t errorCB, void *userData)
{
    cfm_conn_t *conn = (cfm_conn_t *) cx;
    if (!connHead || !conn || !name) {
        return 0;
    }
    fprintf(stderr, "DEBUG: Open channel to %s (%s)..\n", name, uiid);
    if (callback_open) {
        fprintf(stderr, "Error: Connection in progress. Please, try later..\n");
        return 0;
    }
    cfm_conn_t *c;
    for (c = connHead; c != ((void *)0); c = c->next) {
        if (c == conn) {
            unsigned char newMsg[2048];
            int len = 4 + 36 + strlen(name) + 1;
            newMsg[0] = 255;
            newMsg[1] = len & 0xFF;
            newMsg[2] = ((len >> 8) & 0xFF);
            newMsg[3] = 0;
            memcpy(&newMsg[4], uiid, 36);
            memcpy(&newMsg[40], name, strlen(name) + 1);
            len = write(conn->socket_fd, newMsg, len);
            if (len <= 0) {
                return 0;
            }
            callback_open = openCB;
            callback_close = closeCB;
            callback_msg = msgCB;
            callback_error = errorCB;
            curr_user_data = userData;
            fprintf(stderr, "DEBUG: Sent CF_M_CHANNEL_OPEN !\n");
            return 1;
        }
    }
    fprintf(stderr, "Error: Connection not found!\n");
    return 0;
}

canonical_original_code:
int cfm_channel_open(void *cx, char *uiid, char *name, int (*openCB)(void *conn, int chan, void *userData), int (*closeCB)(void *conn, int chan, void *userData), int (*msgCB)(void *conn, int chan, int len, unsigned char *msg, void *userData), int (*errorCB)(void *conn, char *errorMsg, void *userData), void *userData)
{
    struct cfm_conn_t *conn = (cfm_conn_t *) cx;
    if (!connHead || !conn || !name) {
        return 0;
    }
    fprintf(stderr, "DEBUG: Open channel to %s (%s)..\n", name, uiid);
    if (callback_open) {
        fprintf(stderr, "Error: Connection in progress. Please, try later..\n");
        return 0;
    }
    struct cfm_conn_t *c;
    for (c = connHead; c != ((void *)0); c = c->next) {
        if (c == conn) {
            unsigned char newMsg[2048];
            int len = 4 + 36 + strlen(name) + 1;
            newMsg[0] = 255;
            newMsg[1] = len & 0xFF;
            newMsg[2] = ((len >> 8) & 0xFF);
            newMsg[3] = 0;
            memcpy(&newMsg[4], uiid, 36);
            memcpy(&newMsg[40], name, strlen(name) + 1);
            len = write(conn->socket_fd, newMsg, len);
            if (len <= 0) {
                return 0;
            }
            callback_open = openCB;
            callback_close = closeCB;
            callback_msg = msgCB;
            callback_error = errorCB;
            curr_user_data = userData;
            fprintf(stderr, "DEBUG: Sent CF_M_CHANNEL_OPEN !\n");
            return 1;
        }
    }
    fprintf(stderr, "Error: Connection not found!\n");
    return 0;
}

variable_types:
cx
void *

variable_types:
uiid
char *

variable_types:
name
char *

variable_types: int (*)(void *conn, int chan, void *userData)

variable_types: int (*)(void *conn, int chan, int len, unsigned char *msg, void *userData)

variable_types: int (*)(void *conn, char *errorMsg, void *userData)

variable_types:
userData
void *

variable_types:
newMsg
unsigned char [2048]

variable_types: len int


decompiled_code:
__int64 __fastcall cfm_channel_close(__int64 a1, int a2)
{
__int64 result; // rax
char buf[29]; // [rsp+1h] [rbp-1Dh] BYREF

if ( !connHead )
return 0LL;
if ( !a1 )
return 0LL;
if ( channel_being_closed != -1 )
fwrite("Error: Could not close channel. Pending close going on...\n", 1uLL, 0x3AuLL, stderr);
channel_being_closed = a2;
fprintf(stderr, "DEBUG: Close channel %d\n", (unsigned int)a2);
if ( *(_QWORD *)(a1 + 8LL * a2 + 40) )
{
buf[0] = -1;
buf[1] = 5;
buf[2] = 0;
buf[3] = 1;
buf[4] = a2;
if ( (int)write(*(_DWORD *)(a1 + 28), buf, 5uLL) <= 0 )
{
fwrite("Error: Write error!\n", 1uLL, 0x14uLL, stderr);
result = 0LL;
}
else
{
fwrite("DEBUG: Sent CF_M_CHANNEL_CLOSE !\n", 1uLL, 0x21uLL, stderr);
result = 1LL;
}
}
else
{
fwrite("Error: Could not close channel. It was not open!\n", 1uLL, 0x31uLL, stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
__int64 result; // rax
char buf[29]; // [rsp+1h] [rbp-1Dh] BYREF

if ( !connHead )
return 0LL;
if ( !a1 )
return 0LL;
if ( channel_being_closed != -1 )
fwrite("Error: Could not close channel. Pending close going on...\n", 1uLL, 0x3AuLL, stderr);
channel_being_closed = a2;
fprintf(stderr, "DEBUG: Close channel %d\n", (unsigned int)a2);
if ( *(_QWORD *)(a1 + 8LL * a2 + 40) )
{
buf[0] = -1;
buf[1] = 5;
buf[2] = 0;
buf[3] = 1;
buf[4] = a2;
if ( (int)write(*(_DWORD *)(a1 + 28), buf, 5uLL) <= 0 )
{
fwrite("Error: Write error!\n", 1uLL, 0x14uLL, stderr);
result = 0LL;
}
else
{
fwrite("DEBUG: Sent CF_M_CHANNEL_CLOSE !\n", 1uLL, 0x21uLL, stderr);
result = 1LL;
}
}
else
{
fwrite("Error: Could not close channel. It was not open!\n", 1uLL, 0x31uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
int
cfm_channel_close(void *c, int chan)
{
    cfm_conn_t *conn = (cfm_conn_t *) c;
    if (!connHead || !conn) {
        return 0;
    }
    if (channel_being_closed != -1) {
        fprintf(stderr,
                "Error: Could not close channel. Pending close going on...\n");
    }
    channel_being_closed = chan;
    fprintf(stderr, "DEBUG: Close channel %d\n", chan);
    if (conn->peer[chan] == ((void *)0)) {
        fprintf(stderr, "Error: Could not close channel. It was not open!\n");
        return 0;
    }
    unsigned char newMsg[5];
    int len;
    newMsg[0] = 255;
    newMsg[1] = 5;
    newMsg[2] = 0;
    newMsg[3] = 1;
    newMsg[4] = chan;
    len = write(conn->socket_fd, newMsg, 5);
    if (len <= 0) {
        fprintf(stderr, "Error: Write error!\n");
        return 0;
    }
    fprintf(stderr, "DEBUG: Sent CF_M_CHANNEL_CLOSE !\n");
    return 1;
}

canonical_original_code:
int cfm_channel_close(void *c, int chan)
{
    struct cfm_conn_t *conn = (cfm_conn_t *) c;
    if (!connHead || !conn) {
        return 0;
    }
    if (channel_being_closed != -1) {
        fprintf(stderr,
                "Error: Could not close channel. Pending close going on...\n");
    }
    channel_being_closed = chan;
    fprintf(stderr, "DEBUG: Close channel %d\n", chan);
    if (conn->peer[chan] == ((void *)0)) {
        fprintf(stderr, "Error: Could not close channel. It was not open!\n");
        return 0;
    }
    unsigned char newMsg[5];
    int len;
    newMsg[0] = 255;
    newMsg[1] = 5;
    newMsg[2] = 0;
    newMsg[3] = 1;
    newMsg[4] = chan;
    len = write(conn->socket_fd, newMsg, 5);
    if (len <= 0) {
        fprintf(stderr, "Error: Write error!\n");
        return 0;
    }
    fprintf(stderr, "DEBUG: Sent CF_M_CHANNEL_CLOSE !\n");
    return 1;
}

variable_types:
c
void *

variable_types: chan int

variable_types: struct cfm_conn_t { struct cfm_conn_t *next; struct cfm_conn_t *prev; char *host; int port; int socket_fd; unsigned char num_used; struct cfm_peer_t { int sd; int channel; void *localReceiver; int (*callback_open)(void *conn, int chan, void *userData); int (*callback_close)(void *conn, int chan, void *userData); int (*callback_error)(void *conn, char *errorMsg, void *userData); int (*callback_msg)(void *conn, int chan, int len, unsigned char *msg, void *userData); void *userData; } *peer[255]; enum cfm_conn_state_t { CFM_CONN_INIT=0, CFM_CONN_CHANNEL=1, CFM_CONN_LENGTH1=2, CFM_CONN_LENGTH2=3, CFM_CONN_MSGBODY=4, CFM_CONN_MSGREADY=5 } state; int isM; int channel; int msg_len; unsigned char *msg_buff; int msg_pos; } *

variable_types:
newMsg
unsigned char [5]


decompiled_code:
_BOOL8 __fastcall cfm_message_send(__int64 a1, char a2, int a3, __int64 a4)
{
struct iovec v5; // [rsp+0h] [rbp-38h] BYREF
__int64 v6; // [rsp+10h] [rbp-28h]
__int64 v7; // [rsp+18h] [rbp-20h]
char v8; // [rsp+2Dh] [rbp-Bh] BYREF
__int16 v9; // [rsp+2Eh] [rbp-Ah]

v8 = a2;
v9 = a3 + 3;
v5.iov_base = &v8;
v5.iov_len = 3LL;
v6 = a4;
v7 = a3;
return (int)writev(*(_DWORD *)(a1 + 28), &v5, 2) >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(__int64 a1, char a2, int a3, __int64 a4)
{
struct iovec v5; // [rsp+0h] [rbp-38h] BYREF
__int64 v6; // [rsp+10h] [rbp-28h]
__int64 v7; // [rsp+18h] [rbp-20h]
char v8; // [rsp+2Dh] [rbp-Bh] BYREF
__int16 v9; // [rsp+2Eh] [rbp-Ah]

v8 = a2;
v9 = a3 + 3;
v5.iov_base = &v8;
v5.iov_len = 3LL;
v6 = a4;
v7 = a3;
return (int)writev(*(_DWORD *)(a1 + 28), &v5, 2) >= 0;
}

original_code:
int
cfm_message_send(void *c, int chan, int len, unsigned char *msg)
{
    cfm_conn_t *conn = (cfm_conn_t *) c;
    unsigned char newMsg[3];
    int tot_len = 3 + len;
    newMsg[0] = chan;
    newMsg[1] = tot_len & 0xFF;
    newMsg[2] = (tot_len >> 8) & 0xFF;
    struct iovec io[2];
    io[0].iov_base = newMsg;
    io[0].iov_len = 3;
    io[1].iov_base = msg;
    io[1].iov_len = len;
    if (writev(conn->socket_fd, io, 2) < 0) {
        return 0;
    }
    else {
        return 1;
    }
}

canonical_original_code:
int cfm_message_send(void *c, int chan, int len, unsigned char *msg)
{
    struct cfm_conn_t *conn = (cfm_conn_t *) c;
    unsigned char newMsg[3];
    int tot_len = 3 + len;
    newMsg[0] = chan;
    newMsg[1] = tot_len & 0xFF;
    newMsg[2] = (tot_len >> 8) & 0xFF;
    struct iovec io[2];
    io[0].iov_base = newMsg;
    io[0].iov_len = 3;
    io[1].iov_base = msg;
    io[1].iov_len = len;
    if (writev(conn->socket_fd, io, 2) < 0) {
        return 0;
    }
    else {
        return 1;
    }
}

variable_types:
msg
unsigned char *

variable_types:
newMsg
unsigned char [3]

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } [2]


original_code:
int
cfm_sockets_handle(int fd)
{
    cfm_conn_t *conn;
    unsigned char buf[0xFFFF];
    for (conn = connHead; conn != ((void *)0); conn = conn->next) {
        if (conn->socket_fd != fd) {
            continue;
        }
        int res;
        int handled = 0;
        res = read(fd, buf, 0xFFFF);
        if (res <= 0) {
            return -1;
        }
        while (handled < res) {
            switch (conn->state) {
            case CFM_CONN_INIT:
                conn->channel = buf[handled++];
                conn->state = CFM_CONN_CHANNEL;
                fprintf(stderr, "DEBUG: M message: Channel (%d)\n",
                        conn->channel);
                break;
            case CFM_CONN_CHANNEL:
                conn->msg_len = buf[handled++];
                conn->state = CFM_CONN_LENGTH1;
                break;
            case CFM_CONN_LENGTH1:
                conn->msg_len = (buf[handled++] << 8) + conn->msg_len;
                conn->state = CFM_CONN_LENGTH2;
                fprintf(stderr, "DEBUG: M message: Length (%d)\n",
                        conn->msg_len);
                break;
            case CFM_CONN_LENGTH2:
                conn->msg_buff = malloc(conn->msg_len * sizeof(unsigned char));
                conn->msg_buff[0] = 0;
                conn->msg_pos = 0;
                conn->state = CFM_CONN_MSGBODY;
                break;
            case CFM_CONN_MSGBODY:
                conn->msg_buff[conn->msg_pos++] = buf[handled++];
                if ((conn->msg_pos + 3) == conn->msg_len) {
                    fprintf(stderr, "DEBUG: M message: All bytes there (%d)\n",
                            conn->msg_pos);
                    conn->state = CFM_CONN_MSGREADY;
                }
                break;
            default:
                fprintf(stderr, "Error in message handling!\n");
                break;
            }
            if (conn->state != CFM_CONN_MSGREADY) {
                continue;
            }
            conn->state = CFM_CONN_INIT;
            if (conn->channel == 255) {
                return cfm_control_msg_handle(conn);
            }
            return conn->peer[conn->channel]->callback_msg(conn,
                                                           conn->channel,
                                                           conn->msg_len,
                                                           conn->msg_buff,
                                                           conn->peer[conn->
                                                                      channel]->
                                                           userData);
        }
    }
    return 0;
}

canonical_original_code:
int cfm_sockets_handle(int fd)
{
    struct cfm_conn_t *conn;
    unsigned char buf[65535];
    for (conn = connHead; conn != ((void *)0); conn = conn->next) {
        if (conn->socket_fd != fd) {
            continue;
        }
        int res;
        int handled = 0;
        res = read(fd, buf, 0xFFFF);
        if (res <= 0) {
            return -1;
        }
        while (handled < res) {
            switch (conn->state) {
            case CFM_CONN_INIT:
                conn->channel = buf[handled++];
                conn->state = CFM_CONN_CHANNEL;
                fprintf(stderr, "DEBUG: M message: Channel (%d)\n",
                        conn->channel);
                break;
            case CFM_CONN_CHANNEL:
                conn->msg_len = buf[handled++];
                conn->state = CFM_CONN_LENGTH1;
                break;
            case CFM_CONN_LENGTH1:
                conn->msg_len = (buf[handled++] << 8) + conn->msg_len;
                conn->state = CFM_CONN_LENGTH2;
                fprintf(stderr, "DEBUG: M message: Length (%d)\n",
                        conn->msg_len);
                break;
            case CFM_CONN_LENGTH2:
                conn->msg_buff = malloc(conn->msg_len * sizeof(unsigned char));
                conn->msg_buff[0] = 0;
                conn->msg_pos = 0;
                conn->state = CFM_CONN_MSGBODY;
                break;
            case CFM_CONN_MSGBODY:
                conn->msg_buff[conn->msg_pos++] = buf[handled++];
                if ((conn->msg_pos + 3) == conn->msg_len) {
                    fprintf(stderr, "DEBUG: M message: All bytes there (%d)\n",
                            conn->msg_pos);
                    conn->state = CFM_CONN_MSGREADY;
                }
                break;
            default:
                fprintf(stderr, "Error in message handling!\n");
                break;
            }
            if (conn->state != CFM_CONN_MSGREADY) {
                continue;
            }
            conn->state = CFM_CONN_INIT;
            if (conn->channel == 255) {
                return cfm_control_msg_handle(conn);
            }
            return conn->peer[conn->channel]->callback_msg(conn,
                                                           conn->channel,
                                                           conn->msg_len,
                                                           conn->msg_buff,
                                                           conn->peer[conn->
                                                                      channel]->
                                                           userData);
        }
    }
    return 0;
}

variable_types: fd int

variable_types:
buf
unsigned char [65535]

variable_types: res int

variable_types: handled int