binit

decompiled_code:
__int64 __fastcall binit(int a1)
{
__int64 v1; // rbx
char *v3; // rax
char v4; // [rsp+Bh] [rbp-1Dh] BYREF
unsigned int fd[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = -1LL;
if ( (unsigned int)openSharedMem("/my_barrier", fd, &v4) != -1 && !v4 )
{
ftruncate(fd[0], 552LL);
v3 = (char *)mmap(0LL, 0x228uLL, 3, 1, fd[0], 0LL);
v1 = (__int64)v3;
if ( v3 == (char *)-1LL )
{
perror("mmap failed");
}
else
{
if ( (unsigned int)qinit((pthread_mutex_t *)(v3 + 312)) == -1 )
perror("qinit entr");
if ( (unsigned int)qinit((pthread_mutex_t *)(v1 + 392)) == -1 )
perror("qinit exit");
if ( (unsigned int)qinit((pthread_mutex_t *)(v1 + 472)) == -1 )
perror("qinit critical");
if ( (unsigned int)protmem_init(v1 + 264, v1 + 260) == -1 )
perror("protmem r");
*(_DWORD *)v1 = a1;
strcpy((char *)(v1 + 4), "/my_barrier");
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func18(int a1)
{
__int64 v1; // rbx
char *v3; // rax
char v4; // [rsp+Bh] [rbp-1Dh] BYREF
unsigned int fd[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = -1LL;
if ( (unsigned int)func16("/my_barrier", fd, &v4) != -1 && !v4 )
{
ftruncate(fd[0], 552LL);
v3 = (char *)mmap(0LL, 0x228uLL, 3, 1, fd[0], 0LL);
v1 = (__int64)v3;
if ( v3 == (char *)-1LL )
{
perror("mmap failed");
}
else
{
if ( (unsigned int)func5((pthread_mutex_t *)(v3 + 312)) == -1 )
perror("qinit entr");
if ( (unsigned int)func5((pthread_mutex_t *)(v1 + 392)) == -1 )
perror("qinit exit");
if ( (unsigned int)func5((pthread_mutex_t *)(v1 + 472)) == -1 )
perror("qinit critical");
if ( (unsigned int)func13(v1 + 264, v1 + 260) == -1 )
perror("protmem r");
*(_DWORD *)v1 = a1;
strcpy((char *)(v1 + 4), "/my_barrier");
}
}
return v1;
}

original_code:
barrier_t* binit(unsigned int count)
{
  int fd;
  char existed;
  if(openSharedMem("/my_barrier", &fd, &existed) == -1) {
    return (barrier_t*) - 1;
  }
  if(existed) {
    return (barrier_t*) - 1;
  }
  ftruncate(fd, sizeof(struct barrier));
  barrier_t *mem = (barrier_t*) mmap(((void *)0), sizeof(barrier_t), 0x1 | 0x2, 0x01, fd, 0);
  { { if(((long long)mem) == -1) { perror("mmap failed"); } } };
  if(mem == (void*) - 1) {
    return (void*) - 1;
  }
  { { if((qinit(&mem->entrance)) == -1) { perror("qinit entr"); } } };
  { { if((qinit(&mem->exit)) == -1) { perror("qinit exit"); } } };
  { { if((qinit(&mem->critical)) == -1) { perror("qinit critical"); } } };
  { { if((protmem_init(&mem->releasing, &mem->_m)) == -1) { perror("protmem r"); } } };
  mem->max_val = count;
  strcpy(mem->name, "/my_barrier");
  return mem;
}

canonical_original_code:
struct barrier *binit(unsigned int count)
{
  int fd;
  char existed;
  if(openSharedMem("/my_barrier", &fd, &existed) == -1) {
    return (struct barrier *) - 1;
  }
  if(existed) {
    return (struct barrier *) - 1;
  }
  ftruncate(fd, sizeof(struct barrier));
  struct barrier *mem = (barrier_t*) mmap(((void *)0), sizeof(barrier_t), 0x1 | 0x2, 0x01, fd, 0);
  { { if(((long long)mem) == -1) { perror("mmap failed"); } } };
  if(mem == (void *) - 1) {
    return (void *) - 1;
  }
  { { if((qinit(&mem->entrance)) == -1) { perror("qinit entr"); } } };
  { { if((qinit(&mem->exit)) == -1) { perror("qinit exit"); } } };
  { { if((qinit(&mem->critical)) == -1) { perror("qinit critical"); } } };
  { { if((protmem_init(&mem->releasing, &mem->_m)) == -1) { perror("protmem r"); } } };
  mem->max_val = count;
  strcpy(mem->name, "/my_barrier");
  return mem;
}

variable_types:
count
unsigned int

return_type: struct barrier { int max_val; char name[256]; char _m; struct protmem { char *mem; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } prot; } releasing; struct queue { union pthread_mutex_t prot; int count; union sem_t { char __size[32]; long __align; } sem; } entrance; struct queue exit; struct queue critical; } *


bwait

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

v1 = (_QWORD *)(a1 + 264);
while ( (unsigned int)protmem_f(v1, (void (__fastcall *)(_QWORD))get1B) == 1 )
qwait((pthread_mutex_t *)(a1 + 312));
qincr((pthread_mutex_t *)(a1 + 472));
if ( (unsigned int)qget_count((pthread_mutex_t *)(a1 + 472)) == *(_DWORD *)a1 )
{
protmem_f(v1, (void (__fastcall *)(_QWORD))set1BTrue);
qrelease((pthread_mutex_t *)(a1 + 392), 0);
}
else
{
while ( !(unsigned int)protmem_f(v1, (void (__fastcall *)(_QWORD))get1B) )
qwait((pthread_mutex_t *)(a1 + 392));
}
qdecr((pthread_mutex_t *)(a1 + 472));
if ( !(unsigned int)qget_count((pthread_mutex_t *)(a1 + 472)) )
{
protmem_f(v1, (void (__fastcall *)(_QWORD))set1BFalse);
qrelease((pthread_mutex_t *)(a1 + 312), 0);
}
return 0LL;
}

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

v1 = (_QWORD *)(a1 + 264);
while ( (unsigned int)func14(v1, (void (__fastcall *)(_QWORD))get1B) == 1 )
func8((pthread_mutex_t *)(a1 + 312));
func9((pthread_mutex_t *)(a1 + 472));
if ( (unsigned int)func11((pthread_mutex_t *)(a1 + 472)) == *(_DWORD *)a1 )
{
func14(v1, (void (__fastcall *)(_QWORD))set1BTrue);
func6((pthread_mutex_t *)(a1 + 392), 0);
}
else
{
while ( !(unsigned int)func14(v1, (void (__fastcall *)(_QWORD))get1B) )
func8((pthread_mutex_t *)(a1 + 392));
}
func10((pthread_mutex_t *)(a1 + 472));
if ( !(unsigned int)func11((pthread_mutex_t *)(a1 + 472)) )
{
func14(v1, (void (__fastcall *)(_QWORD))set1BFalse);
func6((pthread_mutex_t *)(a1 + 312), 0);
}
return 0LL;
}

original_code:
int bwait(barrier_t* b)
{
  while(protmem_f(&b->releasing, &get1B) == (int) (1>0) ) {
    qwait(&b->entrance);
  }
  qincr(&b->critical);
  if(qget_count(&b->critical) == b->max_val) {
    protmem_f(&b->releasing, &set1BTrue);
    qrelease(&b->exit, (1<0));
  } else {
    while(protmem_f(&b->releasing, &get1B) == (int) (1<0)) {
      qwait(&b->exit);
    }
  }
  qdecr(&b->critical);
  if(qget_count(&b->critical) == 0) {
    protmem_f(&b->releasing, &set1BFalse);
    qrelease(&b->entrance, (1<0));
  }
  return 0;
}

canonical_original_code:
int bwait(struct barrier *b)
{
  while(protmem_f(&b->releasing, &get1B) == (int) (1>0) ) {
    qwait(&b->entrance);
  }
  qincr(&b->critical);
  if(qget_count(&b->critical) == b->max_val) {
    protmem_f(&b->releasing, &set1BTrue);
    qrelease(&b->exit, (1<0));
  } else {
    while(protmem_f(&b->releasing, &get1B) == (int) (1<0)) {
      qwait(&b->exit);
    }
  }
  qdecr(&b->critical);
  if(qget_count(&b->critical) == 0) {
    protmem_f(&b->releasing, &set1BFalse);
    qrelease(&b->entrance, (1<0));
  }
  return 0;
}

variable_types: struct barrier { int max_val; char name[256]; char _m; struct protmem { char *mem; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } prot; } releasing; struct queue { union pthread_mutex_t prot; int count; union sem_t { char __size[32]; long __align; } sem; } entrance; struct queue exit; struct queue critical; } *

return_type: int

user_defined_types: struct barrier { int max_val; char name[256]; char _m; struct protmem releasing; struct queue entrance; struct queue exit; struct queue critical; }

user_defined_types: struct protmem { char *mem; union pthread_mutex_t prot; }

user_defined_types: struct queue { union pthread_mutex_t prot; int count; union sem_t sem; }

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

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


bdestroy

decompiled_code:
__int64 __fastcall bdestroy(void *addr)
{
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

if ( (unsigned int)qdestroy((pthread_mutex_t *)((char *)addr + 312)) == -1 )
perror("bdstr entr");
if ( (unsigned int)qdestroy((pthread_mutex_t *)((char *)addr + 392)) == -1 )
perror("bdstr exit");
if ( (unsigned int)protmem_destroy((__int64)addr + 264) == -1 )
perror("bdstr rel");
strcpy(v2, (const char *)addr + 4);
if ( munmap(addr, 0x228uLL) == -1 )
perror("bdstr munmap");
if ( shm_unlink(v2) == -1 )
perror("bdstr shm_unlink");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func20(void *addr)
{
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

if ( (unsigned int)func7((pthread_mutex_t *)((char *)addr + 312)) == -1 )
perror("bdstr entr");
if ( (unsigned int)func7((pthread_mutex_t *)((char *)addr + 392)) == -1 )
perror("bdstr exit");
if ( (unsigned int)func15((__int64)addr + 264) == -1 )
perror("bdstr rel");
strcpy(v2, (const char *)addr + 4);
if ( munmap(addr, 0x228uLL) == -1 )
perror("bdstr munmap");
if ( shm_unlink(v2) == -1 )
perror("bdstr shm_unlink");
return 0LL;
}

original_code:
int bdestroy(barrier_t* b)
{
  { { if((qdestroy(&b->entrance)) == -1) { perror("bdstr entr"); } } };
  { { if((qdestroy(&b->exit)) == -1) { perror("bdstr exit"); } } };
  { { if((protmem_destroy(&b->releasing)) == -1) { perror("bdstr rel"); } } };
  char buf[256];
  strcpy(buf, b->name);
  { { if((munmap((void*) b, sizeof(struct barrier))) == -1) { perror("bdstr munmap"); } } };
  { { if((shm_unlink(buf)) == -1) { perror("bdstr shm_unlink"); } } };
  return 0;
}

canonical_original_code:
int bdestroy(struct barrier *b)
{
  { { if((qdestroy(&b->entrance)) == -1) { perror("bdstr entr"); } } };
  { { if((qdestroy(&b->exit)) == -1) { perror("bdstr exit"); } } };
  { { if((protmem_destroy(&b->releasing)) == -1) { perror("bdstr rel"); } } };
  char buf[256];
  strcpy(buf, b->name);
  { { if((munmap((void *) b, sizeof(struct barrier))) == -1) { perror("bdstr munmap"); } } };
  { { if((shm_unlink(buf)) == -1) { perror("bdstr shm_unlink"); } } };
  return 0;
}

variable_types:
buf
char [256]

user_defined_types: union sem_t { char __size[32]; long __align; }

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


tidyUp

decompiled_code:
void __fastcall __noreturn tidyUp(unsigned int a1)
{
__int64 v1; // rbx

fprintf(stderr, "Caught %d, clearing shared mem..\n", a1);
if ( n > 0 )
{
v1 = 0LL;
do
kill(2, *(_DWORD *)(children + 4 * v1++));
while ( n > (int)v1 );
}
bdestroy(b);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func21(unsigned int a1)
{
__int64 v1; // rbx

fprintf(stderr, "Caught %d, clearing shared mem..\n", a1);
if ( n > 0 )
{
v1 = 0LL;
do
kill(2, *(_DWORD *)(children + 4 * v1++));
while ( n > (int)v1 );
}
func20(b);
exit(1);
}

original_code:
void tidyUp(int sig)
{
  fprintf(stderr, "Caught %d, clearing shared mem..\n", sig);
  for (int i = 0; i < n; i++) {
    kill(2, children[i]);
  }
  bdestroy(b);
  _exit(1);
}

variable_types: sig int

variable_types: i int

return_type: void


delay

decompiled_code:
void __fastcall delay(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

canonical_decompiled_code:
void __fastcall func7(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

original_code:
void delay(int r, int m)
{
  struct timespec nSleepTime;
  memset(&nSleepTime, 0, sizeof(struct timespec));
  int t = rand() % (r-m) + m;
  nSleepTime.tv_nsec = t % (1000000000);
  nSleepTime.tv_sec = t / (1000000000);
  { { if((nanosleep(&nSleepTime, &nSleepTime)) == -1) { perror("nanosleep"); } } };
}

variable_types: r int

variable_types: m int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: t int


itoa

original_code:
int itoa(int i, char b[])
{
  char const digit[] = "0123456789";
  char* p = b;
  int shifter = i, r = 0;
  do {
    ++p;
    ++r;
    shifter = shifter / 10;
  } while(shifter);
  *p = '\0';
  do {
    *--p = digit[i % 10];
    i = i / 10;
  } while(i);
  return r;
}

canonical_original_code:
int itoa(int i, char b[])
{
  char digit[] = "0123456789";
  char *p = b;
  int shifter = i; int r = 0;
  do {
    ++p;
    ++r;
    shifter = shifter / 10;
  } while(shifter);
  *p = '\0';
  do {
    *--p = digit[i % 10];
    i = i / 10;
  } while(i);
  return r;
}

variable_types:
b
char []

variable_types:
digit
char []

variable_types:
p
char *

variable_types: shifter int


decompiled_code:
void __fastcall print_int(int a1, char *a2)
{
size_t v2; // rax
ssize_t v3; // rbp

itoa(a1, a2);
v2 = strlen(a2);
v3 = write(1, a2, v2);
if ( v3 != strlen(a2) )
perror("write failed");
}

canonical_decompiled_code:
void __fastcall func9(int a1, char *a2)
{
size_t v2; // rax
ssize_t v3; // rbp

func8(a1, a2);
v2 = strlen(a2);
v3 = write(1, a2, v2);
if ( v3 != strlen(a2) )
perror("write failed");
}

original_code:
void print_int(int i, char* buf)
{
  itoa(i,buf);
  { if(write(1, buf, strlen(buf)) != (ssize_t)strlen(buf)) { perror("write failed"); } };
}

canonical_original_code:
void print_int(int i, char *buf)
{
  itoa(i,buf);
  { if(write(1, buf, strlen(buf)) != (long)strlen(buf)) { perror("write failed"); } };
}

variable_types:
buf
char *


decompiled_code:
ssize_t __fastcall print_str(void *buf)
{
size_t v1; // rax

v1 = strlen((const char *)buf);
return write(1, buf, v1);
}

canonical_decompiled_code:
ssize_t __fastcall func10(void *buf)
{
size_t v1; // rax

v1 = strlen((const char *)buf);
return write(1, buf, v1);
}

original_code:
void print_str(const char* buf)
{
  write(1, buf, strlen(buf));
}

canonical_original_code:
void print_str(char *buf)
{
  write(1, buf, strlen(buf));
}


backend

decompiled_code:
__int64 __fastcall backend(const char *a1, int a2)
{
int v2; // ebx
int v3; // eax
unsigned int v4; // ebx
__int64 v6; // [rsp+0h] [rbp-C8h] BYREF
int v7; // [rsp+8h] [rbp-C0h]
int v8; // [rsp+Ch] [rbp-BCh]
int v9; // [rsp+10h] [rbp-B8h]
int v10; // [rsp+14h] [rbp-B4h]
struct msghdr message; // [rsp+60h] [rbp-68h] BYREF
__int64 v12[5]; // [rsp+A0h] [rbp-28h] BYREF

v2 = open(a1, 0);
print_str("backend (pid: ");
v3 = getpid();
print_int(v3, (char *)&v6);
print_str(") read: ");
if ( read(v2, &v6, 8uLL) != 8 )
perror("read failed");
if ( write(1, &v6, 8uLL) != 8 )
perror("write failed");
print_str("\n");
v10 = 0;
v12[0] = (__int64)"OK";
v12[1] = 2LL;
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v12;
message.msg_iovlen = 1LL;
message.msg_control = &v6;
v7 = 1;
v8 = 1;
v6 = 20LL;
v9 = v2;
message.msg_controllen = 20LL;
v4 = sendmsg(a2, &message, 0);
if ( v4 == -1 )
perror("sendmsg");
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func11(const char *a1, int a2)
{
int v2; // ebx
int v3; // eax
unsigned int v4; // ebx
__int64 v6; // [rsp+0h] [rbp-C8h] BYREF
int v7; // [rsp+8h] [rbp-C0h]
int v8; // [rsp+Ch] [rbp-BCh]
int v9; // [rsp+10h] [rbp-B8h]
int v10; // [rsp+14h] [rbp-B4h]
struct msghdr message; // [rsp+60h] [rbp-68h] BYREF
__int64 v12[5]; // [rsp+A0h] [rbp-28h] BYREF

v2 = open(a1, 0);
func10("backend (pid: ");
v3 = getpid();
func9(v3, (char *)&v6);
func10(") read: ");
if ( read(v2, &v6, 8uLL) != 8 )
perror("read failed");
if ( write(1, &v6, 8uLL) != 8 )
perror("write failed");
func10("\n");
v10 = 0;
v12[0] = (__int64)"OK";
v12[1] = 2LL;
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v12;
message.msg_iovlen = 1LL;
message.msg_control = &v6;
v7 = 1;
v8 = 1;
v6 = 20LL;
v9 = v2;
message.msg_controllen = 20LL;
v4 = sendmsg(a2, &message, 0);
if ( v4 == -1 )
perror("sendmsg");
return v4;
}

original_code:
int backend(const char* fn, int backend_fd)
{
  int fd = open(fn, 00);
  ssize_t msgsize = ((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
  char buf[msgsize + 64];
  print_str("backend (pid: ");
  print_int(getpid(), buf);
  print_str(") read: ");
  { if(read(fd, buf, 8) != 8) { perror("read failed"); } };
  { if(write(1, buf, 8) != 8) { perror("write failed"); } };
  print_str("\n");
  memset(buf, 0, msgsize);
  struct iovec iov;
  iov.iov_base = "OK";
  iov.iov_len = 2;
  struct msghdr mhdr;
  memset(&mhdr, 0, sizeof(struct msghdr));
  mhdr.msg_iov = &iov;
  mhdr.msg_iovlen = 1;
  mhdr.msg_control = buf;
  mhdr.msg_controllen = msgsize;
  struct cmsghdr* cmsg = ((size_t) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0);
  cmsg->cmsg_level = 1;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int)));
  *(int*)((cmsg)->__cmsg_data) = fd;
  mhdr.msg_controllen = cmsg->cmsg_len;
  int ret = 0;
  { { if((ret = sendmsg(backend_fd, &mhdr, 0)) == -1) { perror("sendmsg"); } } };
  return ret;
}

canonical_original_code:
int backend(char *fn, int backend_fd)
{
  int fd = open(fn, 00);
  long msgsize = ((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
  char buf[-1];
  print_str("backend (pid: ");
  print_int(getpid(), buf);
  print_str(") read: ");
  { if(read(fd, buf, 8) != 8) { perror("read failed"); } };
  { if(write(1, buf, 8) != 8) { perror("write failed"); } };
  print_str("\n");
  memset(buf, 0, msgsize);
  struct iovec iov;
  iov.iov_base = "OK";
  iov.iov_len = 2;
  struct msghdr mhdr;
  memset(&mhdr, 0, sizeof(struct msghdr));
  mhdr.msg_iov = &iov;
  mhdr.msg_iovlen = 1;
  mhdr.msg_control = buf;
  mhdr.msg_controllen = msgsize;
  struct cmsghdr *cmsg = ((size_t) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0);
  cmsg->cmsg_level = 1;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int)));
  *(int *)((cmsg)->__cmsg_data) = fd;
  mhdr.msg_controllen = cmsg->cmsg_len;
  int ret = 0;
  { { if((ret = sendmsg(backend_fd, &mhdr, 0)) == -1) { perror("sendmsg"); } } };
  return ret;
}

variable_types:
fn
char *

variable_types: fd int

variable_types: msgsize long

variable_types:
buf
char [-1]

variable_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec { void *iov_base; unsigned long iov_len; } *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

variable_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types: ret int

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }


frontend

decompiled_code:
__int64 __fastcall frontend(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
int v3; // ebp
int v4; // eax
__int64 v6[32]; // [rsp+0h] [rbp-1D8h] BYREF
struct msghdr message; // [rsp+100h] [rbp-D8h] BYREF
__int64 v8[2]; // [rsp+140h] [rbp-98h] BYREF
__int64 buf[8]; // [rsp+150h] [rbp-88h] BYREF
__int64 v10[4]; // [rsp+190h] [rbp-48h] BYREF
__int16 v11; // [rsp+1B0h] [rbp-28h]

buf[0] = 0LL;
buf[1] = 0LL;
buf[2] = 0LL;
buf[3] = 0LL;
buf[4] = 0LL;
buf[5] = 0LL;
buf[6] = 0LL;
buf[7] = 0LL;
v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
v10[3] = 0LL;
v11 = 0;
v8[0] = (__int64)buf;
v8[1] = 63LL;
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v8;
message.msg_iovlen = 1LL;
message.msg_control = v10;
message.msg_controllen = 34LL;
v1 = recvmsg(a1, &message, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("recvmsg");
}
else if ( !v1 )
{
LABEL_11:
v6[0] = 0x7272652047534D43LL;
v6[1] = 684655LL;
v6[2] = 0LL;
v6[3] = 0LL;
v6[4] = 0LL;
v6[5] = 0LL;
v6[6] = 0LL;
v6[7] = 0LL;
v6[8] = 0LL;
v6[9] = 0LL;
v6[10] = 0LL;
v6[11] = 0LL;
v6[12] = 0LL;
v6[13] = 0LL;
v6[14] = 0LL;
v6[15] = 0LL;
v6[16] = 0LL;
v6[17] = 0LL;
v6[18] = 0LL;
v6[19] = 0LL;
v6[20] = 0LL;
v6[21] = 0LL;
v6[22] = 0LL;
v6[23] = 0LL;
v6[24] = 0LL;
v6[25] = 0LL;
v6[26] = 0LL;
v6[27] = 0LL;
v6[28] = 0LL;
v6[29] = 0LL;
v6[30] = 0LL;
v6[31] = 0LL;
write(2, v6, 0x100uLL);
return 1;
}
if ( message.msg_controllen <= 0xF || !message.msg_control )
goto LABEL_11;
v3 = *((unsigned __int8 *)message.msg_control + 16);
print_str("frontend (pid: ");
v4 = getpid();
print_int(v4, (char *)buf);
print_str(") read: ");
if ( read(v3, buf, 8uLL) != 8 )
perror("read failed");
if ( write(1, buf, 8uLL) != 8 )
perror("write failed");
print_str("\n");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
int v3; // ebp
int v4; // eax
__int64 v6[32]; // [rsp+0h] [rbp-1D8h] BYREF
struct msghdr message; // [rsp+100h] [rbp-D8h] BYREF
__int64 v8[2]; // [rsp+140h] [rbp-98h] BYREF
__int64 buf[8]; // [rsp+150h] [rbp-88h] BYREF
__int64 v10[4]; // [rsp+190h] [rbp-48h] BYREF
__int16 v11; // [rsp+1B0h] [rbp-28h]

buf[0] = 0LL;
buf[1] = 0LL;
buf[2] = 0LL;
buf[3] = 0LL;
buf[4] = 0LL;
buf[5] = 0LL;
buf[6] = 0LL;
buf[7] = 0LL;
v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
v10[3] = 0LL;
v11 = 0;
v8[0] = (__int64)buf;
v8[1] = 63LL;
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v8;
message.msg_iovlen = 1LL;
message.msg_control = v10;
message.msg_controllen = 34LL;
v1 = recvmsg(a1, &message, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("recvmsg");
}
else if ( !v1 )
{
LABEL_11:
v6[0] = 0x7272652047534D43LL;
v6[1] = 684655LL;
v6[2] = 0LL;
v6[3] = 0LL;
v6[4] = 0LL;
v6[5] = 0LL;
v6[6] = 0LL;
v6[7] = 0LL;
v6[8] = 0LL;
v6[9] = 0LL;
v6[10] = 0LL;
v6[11] = 0LL;
v6[12] = 0LL;
v6[13] = 0LL;
v6[14] = 0LL;
v6[15] = 0LL;
v6[16] = 0LL;
v6[17] = 0LL;
v6[18] = 0LL;
v6[19] = 0LL;
v6[20] = 0LL;
v6[21] = 0LL;
v6[22] = 0LL;
v6[23] = 0LL;
v6[24] = 0LL;
v6[25] = 0LL;
v6[26] = 0LL;
v6[27] = 0LL;
v6[28] = 0LL;
v6[29] = 0LL;
v6[30] = 0LL;
v6[31] = 0LL;
write(2, v6, 0x100uLL);
return 1;
}
if ( message.msg_controllen <= 0xF || !message.msg_control )
goto LABEL_11;
v3 = *((unsigned __int8 *)message.msg_control + 16);
func10("frontend (pid: ");
v4 = getpid();
func9(v4, (char *)buf);
func10(") read: ");
if ( read(v3, buf, 8uLL) != 8 )
perror("read failed");
if ( write(1, buf, 8uLL) != 8 )
perror("write failed");
func10("\n");
return v2;
}

original_code:
int frontend(int frontend_fd)
{
  char control[10 + ((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)))], data[64];
  memset(data, 0, sizeof(data));
  memset(control, 0, sizeof(control));
  struct iovec iov;
  iov.iov_base = data;
  iov.iov_len = sizeof(data)-1;
  struct msghdr mhdr;
  memset(&mhdr, 0, sizeof(mhdr));
  mhdr.msg_iov = &iov;
  mhdr.msg_iovlen = 1;
  mhdr.msg_control = control;
  mhdr.msg_controllen = sizeof(control);
  int ret = 0;
  { { if((ret = recvmsg(frontend_fd, &mhdr, 0)) == -1) { perror("recvmsg"); } } };
  if ((mhdr.msg_flags & MSG_TRUNC) || (mhdr.msg_flags & MSG_CTRUNC)) {
    { { if((1 > 0) == -1) { perror("msg trunc"); } } };
  }
  if(ret == 0 || ((size_t) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0) == ((void *)0)) {
    char error[256] = "CMSG error\n";
    write(2, error, sizeof(error));
    return 1;
  }
  int fd = ((((size_t) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0))->__cmsg_data)[0];
  print_str("frontend (pid: ");
  print_int(getpid(), data);
  print_str(") read: ");
  { if(read(fd, data, 8) != 8) { perror("read failed"); } };
  { if(write(1, data, 8) != 8) { perror("write failed"); } };
  print_str("\n");
  return ret;
}

canonical_original_code:
int frontend(int frontend_fd)
{
  char control[-1]; char data[64];
  memset(data, 0, sizeof(data));
  memset(control, 0, sizeof(control));
  struct iovec iov;
  iov.iov_base = data;
  iov.iov_len = sizeof(data)-1;
  struct msghdr mhdr;
  memset(&mhdr, 0, sizeof(mhdr));
  mhdr.msg_iov = &iov;
  mhdr.msg_iovlen = 1;
  mhdr.msg_control = control;
  mhdr.msg_controllen = sizeof(control);
  int ret = 0;
  { { if((ret = recvmsg(frontend_fd, &mhdr, 0)) == -1) { perror("recvmsg"); } } };
  if ((mhdr.msg_flags & MSG_TRUNC) || (mhdr.msg_flags & MSG_CTRUNC)) {
    { { if((1 > 0) == -1) { perror("msg trunc"); } } };
  }
  if(ret == 0 || ((unsigned long) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0) == ((void *)0)) {
    char error[256] = "CMSG error\n";
    write(2, error, sizeof(error));
    return 1;
  }
  int fd = ((((size_t) (&mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mhdr)->msg_control : (struct cmsghdr *) 0))->__cmsg_data)[0];
  print_str("frontend (pid: ");
  print_int(getpid(), data);
  print_str(") read: ");
  { if(read(fd, data, 8) != 8) { perror("read failed"); } };
  { if(write(1, data, 8) != 8) { perror("write failed"); } };
  print_str("\n");
  return ret;
}

variable_types:
control
char [-1]

variable_types:
data
char [64]

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

variable_types:
error
char [256]

user_defined_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; }


isReader

decompiled_code:
__int64 __fastcall isReader(_BYTE *a1)
{
return *a1 & 1;
}

canonical_decompiled_code:
__int64 __fastcall func8(_BYTE *a1)
{
return *a1 & 1;
}

original_code:
char isReader(struct thFlags* f)
{
  return f->data & 1;
}

canonical_original_code:
char isReader(struct thFlags *f)
{
  return f->data & 1;
}

variable_types:
f
struct thFlags { char data; } *

return_type: char


isWriter

decompiled_code:
__int64 __fastcall isWriter(_BYTE *a1)
{
return *a1 & 2;
}

canonical_decompiled_code:
__int64 __fastcall func10(_BYTE *a1)
{
return *a1 & 2;
}

original_code:
char isWriter(struct thFlags* f)
{
  return f->data & 2;
}

canonical_original_code:
char isWriter(struct thFlags *f)
{
  return f->data & 2;
}


writeIfHigher

decompiled_code:
void __fastcall writeIfHigher(int a1, _DWORD *a2, pthread_mutex_t *a3)
{
if ( pthread_mutex_lock(a3) )
perror("wIH sem lock failed");
if ( *a2 < a1 )
*a2 = a1;
if ( pthread_mutex_unlock(a3) )
perror("wiH sem unlock failed");
}

canonical_decompiled_code:
void __fastcall func13(int a1, _DWORD *a2, pthread_mutex_t *a3)
{
if ( pthread_mutex_lock(a3) )
perror("wIH sem lock failed");
if ( *a2 < a1 )
*a2 = a1;
if ( pthread_mutex_unlock(a3) )
perror("wiH sem unlock failed");
}

original_code:
void writeIfHigher(int val, int* where, pthread_mutex_t* sem)
{
  { if(pthread_mutex_lock(sem) != 0) { perror("wIH sem lock failed"); } };
  if(*where < val) {
    *where = val;
  }
  { if(pthread_mutex_unlock(sem) != 0) { perror("wiH sem unlock failed"); } };
}

canonical_original_code:
void writeIfHigher(int val, int *where, union pthread_mutex_t *sem)
{
  { if(pthread_mutex_lock(sem) != 0) { perror("wIH sem lock failed"); } };
  if(*where < val) {
    *where = val;
  }
  { if(pthread_mutex_unlock(sem) != 0) { perror("wiH sem unlock failed"); } };
}

variable_types: val int

variable_types:
where
int *

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


Writer

decompiled_code:
void __fastcall Writer(__int64 a1)
{
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem lock failed");
delay(100, 10);
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem unlock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
}

canonical_decompiled_code:
void __fastcall func14(__int64 a1)
{
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem lock failed");
func7(100, 10);
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem unlock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
}

original_code:
void Writer(struct thInfo* t)
{
  { if(pthread_mutex_lock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
  { if(pthread_mutex_lock(t->wSem) != 0) { perror("wSem lock failed"); } };
  delay(100,10);
  { if(pthread_mutex_unlock(t->wSem) != 0) { perror("wSem unlock failed"); } };
  { if(pthread_mutex_unlock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
}

canonical_original_code:
void Writer(struct thInfo *t)
{
  { if(pthread_mutex_lock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
  { if(pthread_mutex_lock(t->wSem) != 0) { perror("wSem lock failed"); } };
  delay(100,10);
  { if(pthread_mutex_unlock(t->wSem) != 0) { perror("wSem unlock failed"); } };
  { if(pthread_mutex_unlock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
}

variable_types: struct thInfo { struct thFlags { char data; } flags; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *wSem; int *readerNr; union pthread_mutex_t *rSem; union pthread_mutex_t *r2Sem; int *readerMaxWait; union pthread_mutex_t *rmSem; int *writerMaxWait; union pthread_mutex_t *wmSem; } *

user_defined_types: struct thInfo { struct thFlags flags; union pthread_mutex_t *wSem; int *readerNr; union pthread_mutex_t *rSem; union pthread_mutex_t *r2Sem; int *readerMaxWait; union pthread_mutex_t *rmSem; int *writerMaxWait; union pthread_mutex_t *wmSem; }


Reader

decompiled_code:
void __fastcall Reader(__int64 a1)
{
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem lock failed");
if ( ++**(_DWORD **)(a1 + 16) == 1 && pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem lock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem unlock failed");
delay(1000, 10);
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem lock failed");
if ( !--**(_DWORD **)(a1 + 16) && pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem unlock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem unlock failed");
}

canonical_decompiled_code:
void __fastcall func15(__int64 a1)
{
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 32)) )
perror("r2Sem lock failed");
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem lock failed");
if ( ++**(_DWORD **)(a1 + 16) == 1 && pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem lock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem unlock failed");
func7(1000, 10);
if ( pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem lock failed");
if ( !--**(_DWORD **)(a1 + 16) && pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 8)) )
perror("wSem unlock failed");
if ( pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 24)) )
perror("rSem unlock failed");
}

original_code:
void Reader(struct thInfo* t)
{
  { if(pthread_mutex_lock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
  { if(pthread_mutex_unlock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
  { if(pthread_mutex_lock(t->rSem) != 0) { perror("rSem lock failed"); } };
  (*t->readerNr)++;
  if(*t->readerNr == 1) {
    { if(pthread_mutex_lock(t->wSem) != 0) { perror("wSem lock failed"); } };
  }
  { if(pthread_mutex_unlock(t->rSem) != 0) { perror("rSem unlock failed"); } };
  delay(1000,10);
  { if(pthread_mutex_lock(t->rSem) != 0) { perror("rSem lock failed"); } };
  (*t->readerNr) = (*t->readerNr) - 1;
  if(*t->readerNr == 0) {
    { if(pthread_mutex_unlock(t->wSem) != 0) { perror("wSem unlock failed"); } };
  }
  { if(pthread_mutex_unlock(t->rSem) != 0) { perror("rSem unlock failed"); } };
}

canonical_original_code:
void Reader(struct thInfo *t)
{
  { if(pthread_mutex_lock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
  { if(pthread_mutex_unlock(t->r2Sem) != 0) { perror("r2Sem lock failed"); } };
  { if(pthread_mutex_lock(t->rSem) != 0) { perror("rSem lock failed"); } };
  (*t->readerNr)++;
  if(*t->readerNr == 1) {
    { if(pthread_mutex_lock(t->wSem) != 0) { perror("wSem lock failed"); } };
  }
  { if(pthread_mutex_unlock(t->rSem) != 0) { perror("rSem unlock failed"); } };
  delay(1000,10);
  { if(pthread_mutex_lock(t->rSem) != 0) { perror("rSem lock failed"); } };
  (*t->readerNr) = (*t->readerNr) - 1;
  if(*t->readerNr == 0) {
    { if(pthread_mutex_unlock(t->wSem) != 0) { perror("wSem unlock failed"); } };
  }
  { if(pthread_mutex_unlock(t->rSem) != 0) { perror("rSem unlock failed"); } };
}


delay

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

canonical_original_code:
void delay(int r, int m)
{
  struct timespec nSleepTime;
  memset(&nSleepTime, 0, sizeof(struct timespec));
  int t = rand() % (r-m) + m;
  nSleepTime.tv_nsec = t % (1000000000);
  nSleepTime.tv_sec = t / (1000000000);
  { { if((nanosleep(&nSleepTime, &nSleepTime)) == -1) { perror("nanosleep"); } } };
}

variable_types: r int

variable_types: m int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: t int


CriticalAdd

decompiled_code:
int __fastcall CriticalAdd(pthread_mutex_t *mutex, _DWORD *a2, int a3)
{
pthread_mutex_lock(mutex);
*a2 += a3;
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int __fastcall func1(pthread_mutex_t *mutex, _DWORD *a2, int a3)
{
pthread_mutex_lock(mutex);
*a2 += a3;
return pthread_mutex_unlock(mutex);
}

original_code:
void CriticalAdd(pthread_mutex_t* m, int* crit, int amount)
{
  { { if((pthread_mutex_lock(m) != 0) == -1) { perror("mutex lock failed"); } } };
  (*crit) = (*crit) + amount;
  { { if((pthread_mutex_unlock(m) != 0) == -1) { perror("mutex unlock failed"); } } };
}

canonical_original_code:
void CriticalAdd(union pthread_mutex_t *m, int *crit, int amount)
{
  { { if((pthread_mutex_lock(m) != 0) == -1) { perror("mutex lock failed"); } } };
  (*crit) = (*crit) + amount;
  { { if((pthread_mutex_unlock(m) != 0) == -1) { perror("mutex unlock failed"); } } };
}

variable_types:
crit
int *

variable_types: amount int


isConsumer

decompiled_code:
__int64 __fastcall isConsumer(_BYTE *a1)
{
return *a1 & 0x80;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1)
{
return *a1 & 0x80;
}

original_code:
char isConsumer(struct thFlags* f)
{
  return f->data & 128;
}

canonical_original_code:
char isConsumer(struct thFlags *f)
{
  return f->data & 128;
}


isProducer

decompiled_code:
__int64 __fastcall isProducer(_BYTE *a1)
{
return *a1 & 0x40;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
return *a1 & 0x40;
}

original_code:
char isProducer(struct thFlags* f)
{
  return f->data & 64;
}

canonical_original_code:
char isProducer(struct thFlags *f)
{
  return f->data & 64;
}


getAmount

decompiled_code:
__int64 __fastcall getAmount(_BYTE *a1)
{
return *a1 & 0x3F;
}

canonical_decompiled_code:
__int64 __fastcall func6(_BYTE *a1)
{
return *a1 & 0x3F;
}

original_code:
unsigned char getAmount(struct thFlags* f)
{
  return f->data & 63;
}

canonical_original_code:
unsigned char getAmount(struct thFlags *f)
{
  return f->data & 63;
}

return_type: unsigned char


ConsumerAction

decompiled_code:
int __fastcall ConsumerAction(__int64 a1)
{
char v1; // al
int result; // eax

delay(100, 10);
--*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL);
fputc(45, stderr);
fflush(stderr);
v1 = getAmount((_BYTE *)a1);
setAmount((_BYTE *)a1, v1 - 1);
result = getAmount((_BYTE *)a1);
if ( !(_BYTE)result )
{
fprintf(stderr, "&(%d)", (unsigned int)cCount);
result = fflush(stderr);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1)
{
char v1; // al
int result; // eax

func0(100, 10);
--*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL);
fputc(45, stderr);
fflush(stderr);
v1 = func6((_BYTE *)a1);
func7((_BYTE *)a1, v1 - 1);
result = func6((_BYTE *)a1);
if ( !(_BYTE)result )
{
fprintf(stderr, "&(%d)", (unsigned int)cCount);
result = fflush(stderr);
}
return result;
}

original_code:
void ConsumerAction(void* info)
{
  struct thInfo* thinfo = (struct thInfo*) info;
  delay(100,10);
  thinfo->persist->itemCount = thinfo->persist->itemCount - 1;
  fprintf(stderr,"-");
  fflush(stderr);
  setAmount(&thinfo->flags, getAmount(&thinfo->flags) - 1);
  if(getAmount(&thinfo->flags) == 0) {
    fprintf(stderr,"&(%d)", cCount);
    fflush(stderr);
  }
}

canonical_original_code:
void ConsumerAction(void *info)
{
  struct thInfo *thinfo = (struct thInfo*) info;
  delay(100,10);
  thinfo->persist->itemCount = thinfo->persist->itemCount - 1;
  fprintf(stderr,"-");
  fflush(stderr);
  setAmount(&thinfo->flags, getAmount(&thinfo->flags) - 1);
  if(getAmount(&thinfo->flags) == 0) {
    fprintf(stderr,"&(%d)", cCount);
    fflush(stderr);
  }
}

variable_types:
info
void *

variable_types: struct thInfo { struct thFlags { char data; } flags; struct thInfoPersist { int maxItems; int itemCount; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } buffer; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } notEmpty; union pthread_cond_t notFull; } *persist; } *

user_defined_types: struct thInfo { struct thFlags flags; struct thInfoPersist *persist; }

user_defined_types: struct thInfoPersist { int maxItems; int itemCount; union pthread_mutex_t buffer; union pthread_cond_t notEmpty; union pthread_cond_t notFull; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }


ProducerAction

decompiled_code:
int __fastcall ProducerAction(__int64 a1)
{
char v1; // al
int result; // eax

delay(100, 10);
++*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL);
fputc(43, stderr);
fflush(stderr);
v1 = getAmount((_BYTE *)a1);
setAmount((_BYTE *)a1, v1 - 1);
result = getAmount((_BYTE *)a1);
if ( !(_BYTE)result )
{
fprintf(stderr, "$(%d)", (unsigned int)pCount);
result = fflush(stderr);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1)
{
char v1; // al
int result; // eax

func0(100, 10);
++*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL);
fputc(43, stderr);
fflush(stderr);
v1 = func6((_BYTE *)a1);
func7((_BYTE *)a1, v1 - 1);
result = func6((_BYTE *)a1);
if ( !(_BYTE)result )
{
fprintf(stderr, "$(%d)", (unsigned int)pCount);
result = fflush(stderr);
}
return result;
}

original_code:
void ProducerAction(void* info)
{
  struct thInfo* thinfo = (struct thInfo*) info;
  delay(100,10);
  thinfo->persist->itemCount = thinfo->persist->itemCount + 1;
  fprintf(stderr,"+");
  fflush(stderr);
  setAmount(&thinfo->flags, getAmount(&thinfo->flags) - 1);
  if(getAmount(&thinfo->flags) == 0) {
    fprintf(stderr,"$(%d)", pCount);
    fflush(stderr);
  }
}

canonical_original_code:
void ProducerAction(void *info)
{
  struct thInfo *thinfo = (struct thInfo*) info;
  delay(100,10);
  thinfo->persist->itemCount = thinfo->persist->itemCount + 1;
  fprintf(stderr,"+");
  fflush(stderr);
  setAmount(&thinfo->flags, getAmount(&thinfo->flags) - 1);
  if(getAmount(&thinfo->flags) == 0) {
    fprintf(stderr,"$(%d)", pCount);
    fflush(stderr);
  }
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }


Producer

decompiled_code:
__int64 __fastcall Producer(signed int *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r15
unsigned int v4; // er12
signed int v5; // er13
__int64 result; // rax
unsigned int v7; // ebx
__int64 v8; // r14
pthread_t v9; // rax
struct timespec tp; // [rsp+20h] [rbp-48h] BYREF

v3 = (pthread_mutex_t *)(a1 + 2);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 2));
v4 = *a1;
v5 = a1[1];
if ( *a1 > v5 )
{
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 1) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v7 = cCount;
v8 = (unsigned int)pCount;
if ( 5 * cCount < pCount )
break;
v9 = pthread_self();
fprintf(stderr, "\n P%u : itemCount: %d maxItems: %d, C%d P%d)\n", v9, (unsigned int)v5, v4, v7, v8);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 1) == -1 )
perror("broadcast failed");
clock_gettime(0, &tp);
tp.tv_sec += 5LL;
pthread_cond_timedwait((pthread_cond_t *)a1 + 2, v3, &tp);
v4 = *a1;
v5 = a1[1];
if ( *a1 > v5 )
goto LABEL_8;
}
fwrite("[+]", 1uLL, 3uLL, stderr);
fflush(stderr);
pthread_mutex_unlock(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(signed int *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r15
unsigned int v4; // er12
signed int v5; // er13
__int64 result; // rax
unsigned int v7; // ebx
__int64 v8; // r14
pthread_t v9; // rax
struct timespec tp; // [rsp+20h] [rbp-48h] BYREF

v3 = (pthread_mutex_t *)(a1 + 2);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 2));
v4 = *a1;
v5 = a1[1];
if ( *a1 > v5 )
{
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 1) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v7 = cCount;
v8 = (unsigned int)pCount;
if ( 5 * cCount < pCount )
break;
v9 = pthread_self();
fprintf(stderr, "\n P%u : itemCount: %d maxItems: %d, C%d P%d)\n", v9, (unsigned int)v5, v4, v7, v8);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 1) == -1 )
perror("broadcast failed");
clock_gettime(0, &tp);
tp.tv_sec += 5LL;
pthread_cond_timedwait((pthread_cond_t *)a1 + 2, v3, &tp);
v4 = *a1;
v5 = a1[1];
if ( *a1 > v5 )
goto LABEL_8;
}
fwrite("[+]", 1uLL, 3uLL, stderr);
fflush(stderr);
pthread_mutex_unlock(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int Producer(struct thInfoPersist* t, void (*action)(void*), void* arg)
{
  { { if((pthread_mutex_lock(&t->buffer) != 0) == -1) { perror("mutex lock failed"); } } };
  while(t->itemCount > t->maxItems - 1) {
    if(cCount * 5 < pCount) {
      fprintf(stderr,"[+]");
      fflush(stderr);
      { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
      return -1;
    }
    fprintf(stderr, "\n P%u : itemCount: %d maxItems: %d, C%d P%d)\n", (unsigned int)pthread_self(), t->itemCount, t->maxItems, cCount, pCount);
    { { if((pthread_cond_broadcast(&t->notEmpty)) == -1) { perror("broadcast failed"); } } };
    struct timespec ts;
    clock_gettime(0, &ts);
    ts.tv_sec+=5;
    pthread_cond_timedwait(&t->notFull, &t->buffer, &ts);
  }
  (*action)(arg);
  { { if((pthread_cond_broadcast(&t->notEmpty)) == -1) { perror("broadcast failed"); } } };
  { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
  return 0;
}

canonical_original_code:
int Producer(struct thInfoPersist *t, void (*action)(void *), void *arg)
{
  { { if((pthread_mutex_lock(&t->buffer) != 0) == -1) { perror("mutex lock failed"); } } };
  while(t->itemCount > t->maxItems - 1) {
    if(cCount * 5 < pCount) {
      fprintf(stderr,"[+]");
      fflush(stderr);
      { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
      return -1;
    }
    fprintf(stderr, "\n P%u : itemCount: %d maxItems: %d, C%d P%d)\n", (unsigned int)pthread_self(), t->itemCount, t->maxItems, cCount, pCount);
    { { if((pthread_cond_broadcast(&t->notEmpty)) == -1) { perror("broadcast failed"); } } };
    struct timespec ts;
    clock_gettime(0, &ts);
    ts.tv_sec+=5;
    pthread_cond_timedwait(&t->notFull, &t->buffer, &ts);
  }
  (*action)(arg);
  { { if((pthread_cond_broadcast(&t->notEmpty)) == -1) { perror("broadcast failed"); } } };
  { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
  return 0;
}

variable_types: struct thInfoPersist { int maxItems; int itemCount; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } buffer; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } notEmpty; union pthread_cond_t notFull; } *

variable_types:
action
void (*)(void *)

variable_types:
arg
void *

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


Consumer

decompiled_code:
__int64 __fastcall Consumer(unsigned int *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r14
int v4; // er12
__int64 result; // rax
__int64 v6; // rbx
unsigned int v7; // er13
pthread_t v8; // rax
struct timespec tp; // [rsp+10h] [rbp-48h] BYREF

v3 = (pthread_mutex_t *)(a1 + 2);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 2));
v4 = a1[1];
if ( v4 > 0 )
{
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 2) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v6 = (unsigned int)pCount;
v7 = cCount;
if ( 5 * pCount < cCount )
break;
v8 = pthread_self();
fprintf(stderr, "\n C%u : itemCount: %d maxItems: %d, C%d P%d \n", v8, (unsigned int)v4, *a1, v7, v6);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 2) == -1 )
perror("broadcast failed");
clock_gettime(0, &tp);
tp.tv_sec += 5LL;
pthread_cond_timedwait((pthread_cond_t *)a1 + 1, v3, &tp);
v4 = a1[1];
if ( v4 > 0 )
goto LABEL_8;
}
fwrite("[-]", 1uLL, 3uLL, stderr);
fflush(stderr);
pthread_mutex_unlock(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r14
int v4; // er12
__int64 result; // rax
__int64 v6; // rbx
unsigned int v7; // er13
pthread_t v8; // rax
struct timespec tp; // [rsp+10h] [rbp-48h] BYREF

v3 = (pthread_mutex_t *)(a1 + 2);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 2));
v4 = a1[1];
if ( v4 > 0 )
{
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 2) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v6 = (unsigned int)pCount;
v7 = cCount;
if ( 5 * pCount < cCount )
break;
v8 = pthread_self();
fprintf(stderr, "\n C%u : itemCount: %d maxItems: %d, C%d P%d \n", v8, (unsigned int)v4, *a1, v7, v6);
if ( pthread_cond_broadcast((pthread_cond_t *)a1 + 2) == -1 )
perror("broadcast failed");
clock_gettime(0, &tp);
tp.tv_sec += 5LL;
pthread_cond_timedwait((pthread_cond_t *)a1 + 1, v3, &tp);
v4 = a1[1];
if ( v4 > 0 )
goto LABEL_8;
}
fwrite("[-]", 1uLL, 3uLL, stderr);
fflush(stderr);
pthread_mutex_unlock(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int Consumer(struct thInfoPersist* t, void (*action)(void*), void* arg)
{
  { { if((pthread_mutex_lock(&t->buffer) != 0) == -1) { perror("mutex lock failed"); } } };
  while(t->itemCount < 1) {
      if(pCount * 5 < cCount) {
      fprintf(stderr,"[-]");
      fflush(stderr);
      { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
      return -1;
    }
    fprintf(stderr, "\n C%u : itemCount: %d maxItems: %d, C%d P%d \n", (unsigned int)pthread_self(), t->itemCount, t->maxItems, cCount, pCount);
    { { if((pthread_cond_broadcast(&t->notFull)) == -1) { perror("broadcast failed"); } } };
    struct timespec ts;
    clock_gettime(0, &ts);
    ts.tv_sec+=5;
    pthread_cond_timedwait(&t->notEmpty, &t->buffer, &ts);
  }
  (*action)(arg);
  { { if((pthread_cond_broadcast(&t->notFull)) == -1) { perror("broadcast failed"); } } };
  { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
  return 0;
}

canonical_original_code:
int Consumer(struct thInfoPersist *t, void (*action)(void *), void *arg)
{
  { { if((pthread_mutex_lock(&t->buffer) != 0) == -1) { perror("mutex lock failed"); } } };
  while(t->itemCount < 1) {
      if(pCount * 5 < cCount) {
      fprintf(stderr,"[-]");
      fflush(stderr);
      { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
      return -1;
    }
    fprintf(stderr, "\n C%u : itemCount: %d maxItems: %d, C%d P%d \n", (unsigned int)pthread_self(), t->itemCount, t->maxItems, cCount, pCount);
    { { if((pthread_cond_broadcast(&t->notFull)) == -1) { perror("broadcast failed"); } } };
    struct timespec ts;
    clock_gettime(0, &ts);
    ts.tv_sec+=5;
    pthread_cond_timedwait(&t->notEmpty, &t->buffer, &ts);
  }
  (*action)(arg);
  { { if((pthread_cond_broadcast(&t->notFull)) == -1) { perror("broadcast failed"); } } };
  { { if((pthread_mutex_unlock(&t->buffer) != 0) == -1) { perror("mutex unlock failed"); } } };
  return 0;
}


delay

decompiled_code:
void __fastcall delay(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

canonical_decompiled_code:
void __fastcall func7(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

original_code:
void delay(int r, int m)
{
  struct timespec nSleepTime;
  memset(&nSleepTime, 0, sizeof(struct timespec));
  int t = rand() % (r-m) + m;
  nSleepTime.tv_nsec = t % (1000000000);
  nSleepTime.tv_sec = t / (1000000000);
  { { if((nanosleep(&nSleepTime, &nSleepTime)) == -1) { perror("nanosleep"); } } };
}


openSharedMem

decompiled_code:
__int64 __fastcall openSharedMem(char *name, unsigned int *a2, _BYTE *a3)
{
int v4; // eax
unsigned int v5; // ebx

v4 = shm_open(name, 194, 0x180u);
*a2 = v4;
if ( v4 == -1 )
{
v5 = -1;
if ( *__errno_location() == 17 )
{
if ( a3 )
*a3 = 1;
v5 = shm_open(name, 2, 0x180u);
*a2 = v5;
}
}
else
{
if ( a3 )
*a3 = 0;
v5 = *a2;
}
return v5;
}

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

v4 = shm_open(name, 194, 0x180u);
*a2 = v4;
if ( v4 == -1 )
{
v5 = -1;
if ( *__errno_location() == 17 )
{
if ( a3 )
*a3 = 1;
v5 = shm_open(name, 2, 0x180u);
*a2 = v5;
}
}
else
{
if ( a3 )
*a3 = 0;
v5 = *a2;
}
return v5;
}

original_code:
int openSharedMem(const char* name, int* fd, char* existed)
{
  int flags = 0200 | 0400;
  (*fd) = shm_open(name, 02 | 0100 | 0200, flags);
  if((*fd) == -1) {
    if((*__errno_location ()) == 17) {
      if(existed != ((void *)0)) {
        (*existed) = 1 > 0;
      }
      (*fd) = shm_open(name, 02, flags);
      return (*fd);
    } else {
      { { if((1 > 0) == -1) { perror("shm_open failed"); } } };
      return (*fd);
    }
  } else {
    if(existed != ((void *)0)) {
      (*existed) = 1 < 0;
    }
    return (*fd);
  }
}

canonical_original_code:
int openSharedMem(char *name, int *fd, char *existed)
{
  int flags = 0200 | 0400;
  (*fd) = shm_open(name, 02 | 0100 | 0200, flags);
  if((*fd) == -1) {
    if((*__errno_location ()) == 17) {
      if(existed != ((void *)0)) {
        (*existed) = 1 > 0;
      }
      (*fd) = shm_open(name, 02, flags);
      return (*fd);
    } else {
      { { if((1 > 0) == -1) { perror("shm_open failed"); } } };
      return (*fd);
    }
  } else {
    if(existed != ((void *)0)) {
      (*existed) = 1 < 0;
    }
    return (*fd);
  }
}

variable_types:
name
char *

variable_types:
fd
int *

variable_types:
existed
char *

variable_types: flags int


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

*(_DWORD *)(a1 + 32) = 0;
*(_DWORD *)(a1 + 36) = 0;
result = sem_init((sem_t *)a1, 1, 0);
if ( !result )
result = pthread_mutex_init((pthread_mutex_t *)(a1 + 40), 0LL);
return result;
}

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

*(_DWORD *)(a1 + 32) = 0;
*(_DWORD *)(a1 + 36) = 0;
result = sem_init((sem_t *)a1, 1, 0);
if ( !result )
result = pthread_mutex_init((pthread_mutex_t *)(a1 + 40), 0LL);
return result;
}

original_code:
int restaurant_init(restaurant_t* r)
{
  int ret=0;
  r->eating = 0;
  r->waiting = 0;
  ret=sem_init(&r->entrance, 1, 0);
  if(ret == 0) {
    ret = pthread_mutex_init(&r->accessVars, ((void *)0));
  }
  return ret;
}

canonical_original_code:
int restaurant_init(struct resturant *r)
{
  int ret = 0;
  r->eating = 0;
  r->waiting = 0;
  ret=sem_init(&r->entrance, 1, 0);
  if(ret == 0) {
    ret = pthread_mutex_init(&r->accessVars, ((void *)0));
  }
  return ret;
}

variable_types: struct resturant { union sem_t { char __size[32]; long __align; } entrance; int eating; int waiting; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } accessVars; } *

variable_types: ret int

user_defined_types: struct resturant { union sem_t entrance; int eating; int waiting; union pthread_mutex_t accessVars; }


decompiled_code:
__int64 __fastcall restaurant_destroy(__int64 a1)
{
unsigned int v1; // ebp

if ( sem_destroy((sem_t *)a1) != -1 )
return (unsigned int)pthread_mutex_destroy((pthread_mutex_t *)(a1 + 40));
v1 = -1;
pthread_mutex_destroy((pthread_mutex_t *)(a1 + 40));
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
unsigned int v1; // ebp

if ( sem_destroy((sem_t *)a1) != -1 )
return (unsigned int)pthread_mutex_destroy((pthread_mutex_t *)(a1 + 40));
v1 = -1;
pthread_mutex_destroy((pthread_mutex_t *)(a1 + 40));
return v1;
}

original_code:
int restaurant_destroy(restaurant_t* r)
{
  int ret = sem_destroy(&r->entrance);
  if(ret != -1) {
    ret = pthread_mutex_destroy(&r->accessVars);
  } else {
    pthread_mutex_destroy(&r->accessVars);
  }
  return ret;
}

canonical_original_code:
int restaurant_destroy(struct resturant *r)
{
  int ret = sem_destroy(&r->entrance);
  if(ret != -1) {
    ret = pthread_mutex_destroy(&r->accessVars);
  } else {
    pthread_mutex_destroy(&r->accessVars);
  }
  return ret;
}


decompiled_code:
void __fastcall start_eating(sem_t *sem)
{
pthread_mutex_t *v1; // rbp

v1 = (pthread_mutex_t *)(&sem[1].__align + 1);
if ( pthread_mutex_lock((pthread_mutex_t *)(&sem[1].__align + 1)) == -1 )
perror("lck");
printf("%d customers at table..\n", *(unsigned int *)sem[1].__size);
if ( SLODWORD(sem[1].__align) > 1 )
{
++HIDWORD(sem[1].__align);
if ( pthread_mutex_unlock(v1) == -1 )
perror("ulck");
puts("waiting for turn..");
if ( sem_wait(sem) == -1 )
perror("wait");
if ( pthread_mutex_lock(v1) == -1 )
perror("lck");
--HIDWORD(sem[1].__align);
}
++*(_DWORD *)sem[1].__size;
if ( pthread_mutex_unlock(v1) == -1 )
perror("ulck");
}

canonical_decompiled_code:
void __fastcall func11(sem_t *sem)
{
pthread_mutex_t *v1; // rbp

v1 = (pthread_mutex_t *)(&sem[1].__align + 1);
if ( pthread_mutex_lock((pthread_mutex_t *)(&sem[1].__align + 1)) == -1 )
perror("lck");
printf("%d customers at table..\n", *(unsigned int *)sem[1].__size);
if ( SLODWORD(sem[1].__align) > 1 )
{
++HIDWORD(sem[1].__align);
if ( pthread_mutex_unlock(v1) == -1 )
perror("ulck");
puts("waiting for turn..");
if ( sem_wait(sem) == -1 )
perror("wait");
if ( pthread_mutex_lock(v1) == -1 )
perror("lck");
--HIDWORD(sem[1].__align);
}
++*(_DWORD *)sem[1].__size;
if ( pthread_mutex_unlock(v1) == -1 )
perror("ulck");
}

original_code:
void start_eating(restaurant_t* place)
{
  { { if((pthread_mutex_lock(&place->accessVars)) == -1) { perror("lck"); } } };
  printf("%d customers at table..\n", place->eating);
  if(place->eating >= 2) {
    place->waiting++;
    { { if((pthread_mutex_unlock(&place->accessVars)) == -1) { perror("ulck"); } } };
    printf("waiting for turn..\n");
    { { if((sem_wait(&place->entrance)) == -1) { perror("wait"); } } };
    { { if((pthread_mutex_lock(&place->accessVars)) == -1) { perror("lck"); } } };
    place->waiting = place->waiting - 1;
  }
  place->eating++;
  { { if((pthread_mutex_unlock(&place->accessVars)) == -1) { perror("ulck"); } } };
}

canonical_original_code:
void start_eating(struct resturant *place)
{
  { { if((pthread_mutex_lock(&place->accessVars)) == -1) { perror("lck"); } } };
  printf("%d customers at table..\n", place->eating);
  if(place->eating >= 2) {
    place->waiting++;
    { { if((pthread_mutex_unlock(&place->accessVars)) == -1) { perror("ulck"); } } };
    printf("waiting for turn..\n");
    { { if((sem_wait(&place->entrance)) == -1) { perror("wait"); } } };
    { { if((pthread_mutex_lock(&place->accessVars)) == -1) { perror("lck"); } } };
    place->waiting = place->waiting - 1;
  }
  place->eating++;
  { { if((pthread_mutex_unlock(&place->accessVars)) == -1) { perror("ulck"); } } };
}


decompiled_code:
void __fastcall end_eating(sem_t *sem)
{
int v1; // eax
int v2; // ebp
int v3; // eax

if ( pthread_mutex_lock((pthread_mutex_t *)(&sem[1].__align + 1)) == -1 )
perror("lck");
v1 = sem[1].__align;
v2 = v1 - 1;
*(_DWORD *)sem[1].__size = v1 - 1;
if ( v1 == 1 && SHIDWORD(sem[1].__align) > 0 )
{
puts("Posting..");
if ( SHIDWORD(sem[1].__align) > 0 )
{
do
{
if ( sem_post(sem) == -1 )
perror("post");
++v2;
v3 = 2;
if ( SHIDWORD(sem[1].__align) <= 2 )
v3 = HIDWORD(sem[1].__align);
}
while ( v3 > v2 );
}
}
if ( pthread_mutex_unlock((pthread_mutex_t *)(&sem[1].__align + 1)) == -1 )
perror("ulck");
}

canonical_decompiled_code:
void __fastcall func12(sem_t *sem)
{
int v1; // eax
int v2; // ebp
int v3; // eax

if ( pthread_mutex_lock((pthread_mutex_t *)(&sem[1].__align + 1)) == -1 )
perror("lck");
v1 = sem[1].__align;
v2 = v1 - 1;
*(_DWORD *)sem[1].__size = v1 - 1;
if ( v1 == 1 && SHIDWORD(sem[1].__align) > 0 )
{
puts("Posting..");
if ( SHIDWORD(sem[1].__align) > 0 )
{
do
{
if ( sem_post(sem) == -1 )
perror("post");
++v2;
v3 = 2;
if ( SHIDWORD(sem[1].__align) <= 2 )
v3 = HIDWORD(sem[1].__align);
}
while ( v3 > v2 );
}
}
if ( pthread_mutex_unlock((pthread_mutex_t *)(&sem[1].__align + 1)) == -1 )
perror("ulck");
}

original_code:
void end_eating(restaurant_t* place)
{
  { { if((pthread_mutex_lock(&place->accessVars)) == -1) { perror("lck"); } } };
  place->eating = place->eating - 1;
  if(place->eating == 0 && place->waiting > 0) {
    printf("Posting..\n");
    for(int i = 0; i < ((2<place->waiting) ? 2 : place->waiting); i++) {
      { { if((sem_post(&place->entrance)) == -1) { perror("post"); } } };
    }
  }
  { { if((pthread_mutex_unlock(&place->accessVars)) == -1) { perror("ulck"); } } };
}

canonical_original_code:
void end_eating(struct resturant *place)
{
  { { if((pthread_mutex_lock(&place->accessVars)) == -1) { perror("lck"); } } };
  place->eating = place->eating - 1;
  if(place->eating == 0 && place->waiting > 0) {
    printf("Posting..\n");
    for(int i = 0; i < ((2<place->waiting) ? 2 : place->waiting); i++) {
      { { if((sem_post(&place->entrance)) == -1) { perror("post"); } } };
    }
  }
  { { if((pthread_mutex_unlock(&place->accessVars)) == -1) { perror("ulck"); } } };
}

variable_types: i int


closefront

decompiled_code:
void __fastcall __noreturn closefront(unsigned int a1)
{
if ( a1 == 2 )
fwrite("Got SIGINT, ending\n", 1uLL, 0x13uLL, stderr);
else
fprintf(stderr, "Got %d, ending\n", a1);
close(frontend_s);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(unsigned int a1)
{
if ( a1 == 2 )
fwrite("Got SIGINT, ending\n", 1uLL, 0x13uLL, stderr);
else
fprintf(stderr, "Got %d, ending\n", a1);
close(frontend_s);
exit(1);
}

original_code:
void closefront(int sig)
{
  if(sig == 2) {
    fprintf(stderr, "Got SIGINT, ending\n");
  } else {
    fprintf(stderr, "Got %d, ending\n", sig);
  }
  close(frontend_s);
  exit(1);
}

variable_types: sig int


delay

canonical_decompiled_code:
void __fastcall func1(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

variable_types: r int

variable_types: m int

variable_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
__int64 __fastcall bit_count(char *a1, int a2)
{
char *v2; // rbx
unsigned int v3; // ebp

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = 0;
do
v3 += _popcountdi2((unsigned int)*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, int a2)
{
char *v2; // rbx
unsigned int v3; // ebp

if ( a2 <= 0 )
return 0;
v2 = a1;
v3 = 0;
do
v3 += _popcountdi2((unsigned int)*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
return v3;
}

original_code:
int bit_count(char* buf, int size)
{
  int sum = 0;
  for(int i = 0; i < size; i = i + 1) {
    sum += (unsigned int) (__builtin_popcount((unsigned int)buf[i]));
  }
  return sum;
}

canonical_original_code:
int bit_count(char *buf, int size)
{
  int sum = 0;
  for(int i = 0; i < size; i = i + 1) {
    sum += (unsigned int) (__builtin_popcount((unsigned int)buf[i]));
  }
  return sum;
}

variable_types:
buf
char *

variable_types: size int

variable_types: sum int


itoa

original_code:
int itoa(int i, char b[])
{
  char const digit[] = "0123456789";
  char* p = b;
  int shifter = i, r = 0;
  do {
    ++p;
    ++r;
    shifter = shifter / 10;
  } while(shifter);
  *p = '\0';
  do {
    *--p = digit[i % 10];
    i = i / 10;
  } while(i);
  return r;
}

canonical_original_code:
int itoa(int i, char b[])
{
  char digit[] = "0123456789";
  char *p = b;
  int shifter = i; int r = 0;
  do {
    ++p;
    ++r;
    shifter = shifter / 10;
  } while(shifter);
  *p = '\0';
  do {
    *--p = digit[i % 10];
    i = i / 10;
  } while(i);
  return r;
}

variable_types:
b
char []

variable_types:
digit
char []

variable_types:
p
char *

variable_types: shifter int


startFrontend

decompiled_code:
__int64 startFrontend()
{
unsigned int v0; // ebx
struct sockaddr v2; // [rsp+0h] [rbp-78h] BYREF
__int64 v3; // [rsp+1Ah] [rbp-5Eh]
__int64 v4; // [rsp+22h] [rbp-56h]
__int64 v5; // [rsp+2Ah] [rbp-4Eh]
__int64 v6; // [rsp+32h] [rbp-46h]
__int64 v7; // [rsp+3Ah] [rbp-3Eh]
__int64 v8; // [rsp+42h] [rbp-36h]
__int64 v9; // [rsp+4Ah] [rbp-2Eh]
__int64 v10; // [rsp+52h] [rbp-26h]
__int64 v11; // [rsp+5Ah] [rbp-1Eh]
__int64 v12; // [rsp+62h] [rbp-16h]
__int16 v13; // [rsp+6Ah] [rbp-Eh]
char v14; // [rsp+6Ch] [rbp-Ch]
char v15; // [rsp+6Dh] [rbp-Bh]

frontend_s = socket(1, 1, 0);
if ( frontend_s == -1 )
{
v0 = -1;
perror("socket");
}
else
{
signal(2, (__sighandler_t)closefront);
v15 = 0;
v2.sa_family = 1;
strcpy(v2.sa_data, "/my_count_socket");
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0;
v14 = 0;
v0 = connect(frontend_s, &v2, 0x6Eu);
if ( v0 == -1 )
perror("connect");
}
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // ebx
struct sockaddr v2; // [rsp+0h] [rbp-78h] BYREF
__int64 v3; // [rsp+1Ah] [rbp-5Eh]
__int64 v4; // [rsp+22h] [rbp-56h]
__int64 v5; // [rsp+2Ah] [rbp-4Eh]
__int64 v6; // [rsp+32h] [rbp-46h]
__int64 v7; // [rsp+3Ah] [rbp-3Eh]
__int64 v8; // [rsp+42h] [rbp-36h]
__int64 v9; // [rsp+4Ah] [rbp-2Eh]
__int64 v10; // [rsp+52h] [rbp-26h]
__int64 v11; // [rsp+5Ah] [rbp-1Eh]
__int64 v12; // [rsp+62h] [rbp-16h]
__int16 v13; // [rsp+6Ah] [rbp-Eh]
char v14; // [rsp+6Ch] [rbp-Ch]
char v15; // [rsp+6Dh] [rbp-Bh]

frontend_s = socket(1, 1, 0);
if ( frontend_s == -1 )
{
v0 = -1;
perror("socket");
}
else
{
signal(2, (__sighandler_t)closefront);
v15 = 0;
v2.sa_family = 1;
strcpy(v2.sa_data, "/my_count_socket");
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0;
v14 = 0;
v0 = connect(frontend_s, &v2, 0x6Eu);
if ( v0 == -1 )
perror("connect");
}
return v0;
}

original_code:
int startFrontend()
{
  int r = 0;
  frontend_s = socket(1, SOCK_STREAM, 0);
  { { if((r = frontend_s) == -1) { perror("socket"); } } };
  if(r == -1) {
    return r;
  }
  signal(2, &closefront);
  struct sockaddr_un s_addr;
  memset(&s_addr, 0, sizeof(struct sockaddr_un));
  s_addr.sun_family = 1;
  strncpy(s_addr.sun_path, "/my_count_socket", sizeof(s_addr.sun_path) - 1);
  { { if((r = connect(frontend_s, &s_addr, sizeof(struct sockaddr_un))) == -1) { perror("connect"); } } };
  return r;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


sendFrontend

decompiled_code:
__int64 __fastcall sendFrontend(void *buf, unsigned int *a2, int a3)
{
unsigned int v4; // eax
unsigned int v5; // er13

if ( (unsigned int)send(frontend_s, buf, (int)*a2, 0) == -1 )
{
v5 = -1;
perror("send");
return v5;
}
v4 = recv(frontend_s, buf, a3, 0);
v5 = v4;
*a2 = v4;
if ( v4 == -1 )
{
perror("rcv");
goto LABEL_8;
}
if ( v4 != 4 )
{
LABEL_8:
fprintf(stderr, "Error receiving, got %d bytes\n", *a2);
return (unsigned int)-1;
}
itoa(*(_DWORD *)buf, buf);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *buf, unsigned int *a2, int a3)
{
unsigned int v4; // eax
unsigned int v5; // er13

if ( (unsigned int)send(frontend_s, buf, (int)*a2, 0) == -1 )
{
v5 = -1;
perror("send");
return v5;
}
v4 = recv(frontend_s, buf, a3, 0);
v5 = v4;
*a2 = v4;
if ( v4 == -1 )
{
perror("rcv");
goto LABEL_8;
}
if ( v4 != 4 )
{
LABEL_8:
fprintf(stderr, "Error receiving, got %d bytes\n", *a2);
return (unsigned int)-1;
}
func3(*(_DWORD *)buf, buf);
return v5;
}

original_code:
int sendFrontend(char* data, int* size, int maxsize)
{
  int r = 0;
  { { if((r = send(frontend_s, data, *size, 0)) == -1) { perror("send"); } } };
  if(r == -1) {
    return r;
  }
  { { if(((*size) = r = recv(frontend_s, data, maxsize, 0)) == -1) { perror("rcv"); } } };
  if((unsigned int)r != sizeof(int)) {
    fprintf(stderr, "Error receiving, got %d bytes\n", (*size));
    return -1;
  } else {
    int count = *(int*)data;
    itoa(count, data);
  }
  return r;
}

canonical_original_code:
int sendFrontend(char *data, int *size, int maxsize)
{
  int r = 0;
  { { if((r = send(frontend_s, data, *size, 0)) == -1) { perror("send"); } } };
  if(r == -1) {
    return r;
  }
  { { if(((*size) = r = recv(frontend_s, data, maxsize, 0)) == -1) { perror("rcv"); } } };
  if((unsigned int)r != sizeof(int)) {
    fprintf(stderr, "Error receiving, got %d bytes\n", (*size));
    return -1;
  } else {
    int count = *(int*)data;
    itoa(count, data);
  }
  return r;
}

variable_types:
data
char *

variable_types:
size
int *

variable_types: maxsize int

variable_types: count int


closeFrontend

decompiled_code:
int closeFrontend()
{
unsigned int v1[66]; // [rsp+0h] [rbp-108h] BYREF

fcntl(frontend_s, 2, 2048LL);
puts("Trying to say goodbye..");
send(frontend_s, "0", 1uLL, 0);
if ( recv(frontend_s, v1, 0x100uLL, 0) != -1 )
printf(" final countdown: %d\n", v1[0]);
send(frontend_s, "0", 1uLL, 0);
return close(frontend_s);
}

canonical_decompiled_code:
int func6()
{
unsigned int v1[66]; // [rsp+0h] [rbp-108h] BYREF

fcntl(frontend_s, 2, 2048LL);
puts("Trying to say goodbye..");
send(frontend_s, "0", 1uLL, 0);
if ( recv(frontend_s, v1, 0x100uLL, 0) != -1 )
printf(" final countdown: %d\n", v1[0]);
send(frontend_s, "0", 1uLL, 0);
return close(frontend_s);
}

original_code:
void closeFrontend()
{
  fcntl(frontend_s, 2, 04000);
  printf("Trying to say goodbye..\n");
  send(frontend_s, "0", 1, 0);
  char buf[256];
  if(recv(frontend_s, buf, 256, 0)!= -1) {
    printf(" final countdown: %d\n", *(int*)buf);
  }
  send(frontend_s, "0", 1, 0);
  close(frontend_s);
}

canonical_original_code:
void closeFrontend()
{
  fcntl(frontend_s, 2, 04000);
  printf("Trying to say goodbye..\n");
  send(frontend_s, "0", 1, 0);
  char buf[256];
  if(recv(frontend_s, buf, 256, 0)!= -1) {
    printf(" final countdown: %d\n", *(int *)buf);
  }
  send(frontend_s, "0", 1, 0);
  close(frontend_s);
}

variable_types:
buf
char [256]


decompiled_code:
__int64 __fastcall swap_conns(int a1, int a2)
{
__int64 v2; // rax
int v3; // ecx
__int16 v4; // dx
__int64 result; // rax

if ( a1 != a2 )
{
v2 = connections[a2];
connections[a2] = connections[a1];
connections[a1] = v2;
v3 = *(&sockets.fd + 2 * a2);
v4 = *(&sockets.events + 4 * a2);
result = *((unsigned __int16 *)&sockets.revents + 4 * a2);
*(&sockets + a2) = *(&sockets + a1);
*(&sockets.fd + 2 * a1) = v3;
*(&sockets.events + 4 * a1) = v4;
*(&sockets.revents + 4 * a1) = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
__int64 v2; // rax
int v3; // ecx
__int16 v4; // dx
__int64 result; // rax

if ( a1 != a2 )
{
v2 = connections[a2];
connections[a2] = connections[a1];
connections[a1] = v2;
v3 = *(&sockets.fd + 2 * a2);
v4 = *(&sockets.events + 4 * a2);
result = *((unsigned __int16 *)&sockets.revents + 4 * a2);
*(&sockets + a2) = *(&sockets + a1);
*(&sockets.fd + 2 * a1) = v3;
*(&sockets.events + 4 * a1) = v4;
*(&sockets.revents + 4 * a1) = result;
}
return result;
}

original_code:
void swap_conns(int i, int j)
{
  if(i == j) {
    return;
  }
  conn_t temp = connections[j];
  connections[j] = connections [i];
  connections[i] = temp;
  pollfd_t temp1 = sockets[j];
  sockets[j] = sockets[i];
  sockets[i] = temp1;
}

canonical_original_code:
void swap_conns(int i, int j)
{
  if(i == j) {
    return;
  }
  struct connection temp = connections[j];
  connections[j] = connections [i];
  connections[i] = temp;
  struct pollfd temp1 = sockets[j];
  sockets[j] = sockets[i];
  sockets[i] = temp1;
}

variable_types: i int

variable_types: j int

variable_types: struct connection { int count; unsigned int zero_count; unsigned int send_info; }

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


decompiled_code:
__int64 first_free_socket()
{
__int64 result; // rax

LOBYTE(result) = active_socket_nr;
if ( (unsigned __int8)active_socket_nr > 0x31u )
return 0xFFFFFFFFLL;
++active_socket_nr;
return (unsigned __int8)result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax

LOBYTE(result) = active_socket_nr;
if ( (unsigned __int8)active_socket_nr > 0x31u )
return 0xFFFFFFFFLL;
++active_socket_nr;
return (unsigned __int8)result;
}

original_code:
int first_free_socket()
{
  if(active_socket_nr < 50) {
    return active_socket_nr++;
  }
  return -1;
}


closesocket

decompiled_code:
int __fastcall closesocket(int a1)
{
unsigned __int8 v2; // si

if ( (unsigned __int8)active_socket_nr > a1 )
{
close(*(&sockets.fd + 2 * a1));
connections[a1] = 0LL;
v2 = active_socket_nr - 1;
active_socket_nr = v2;
if ( v2 )
swap_conns(a1, v2);
}
return printf("Closing socket %d\n", (unsigned int)a1);
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
unsigned __int8 v2; // si

if ( (unsigned __int8)active_socket_nr > a1 )
{
close(*(&sockets.fd + 2 * a1));
connections[a1] = 0LL;
v2 = active_socket_nr - 1;
active_socket_nr = v2;
if ( v2 )
func7(a1, v2);
}
return printf("Closing socket %d\n", (unsigned int)a1);
}

original_code:
void closesocket(int i)
{
  if(i < active_socket_nr) {
    close(sockets[i].fd);
    memset(&connections[i], 0, sizeof(conn_t));
    active_socket_nr = active_socket_nr - 1;
    if(active_socket_nr > 0) {
      swap_conns(i, active_socket_nr);
    }
  }
  printf("Closing socket %d\n", i);
}


closeall

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

if ( a1 == 2 )
fwrite("Got SIGINT, ending\n", 1uLL, 0x13uLL, stderr);
else
fprintf(stderr, "Got %d, ending\n", a1);
v1 = (unsigned __int8)active_socket_nr - 1;
if ( v1 >= 0 )
{
do
closesocket(v1--);
while ( v1 != -1 );
}
close(backend_s);
unlink("/my_count_socket");
fflush(stderr);
fflush(stdout);
exit(1);
}

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

if ( a1 == 2 )
fwrite("Got SIGINT, ending\n", 1uLL, 0x13uLL, stderr);
else
fprintf(stderr, "Got %d, ending\n", a1);
v1 = (unsigned __int8)active_socket_nr - 1;
if ( v1 >= 0 )
{
do
func9(v1--);
while ( v1 != -1 );
}
close(backend_s);
unlink("/my_count_socket");
fflush(stderr);
fflush(stdout);
exit(1);
}

original_code:
void closeall(int sig)
{
  if(sig == 2) {
    fprintf(stderr, "Got SIGINT, ending\n");
  } else {
    fprintf(stderr, "Got %d, ending\n", sig);
  }
  for(int i = active_socket_nr-1; i >= 0; i = i - 1) {
    closesocket(i);
  }
  close(backend_s);
  unlink("/my_count_socket");
  fflush(stderr);
  fflush(stdout);
  exit(1);
}

variable_types: sig int


decompiled_code:
void __fastcall handle_sigpipe(int a1)
{
if ( a1 == 13 )
{
fwrite("Got SIGPIPE!\n", 1uLL, 0xDuLL, stderr);
closesocket(send_to);
}
}

canonical_decompiled_code:
void __fastcall func11(int a1)
{
if ( a1 == 13 )
{
fwrite("Got SIGPIPE!\n", 1uLL, 0xDuLL, stderr);
func9(send_to);
}
}

original_code:
void handle_sigpipe(int sig)
{
  if(sig != 13) {
    return;
  }
  fprintf(stderr, "Got SIGPIPE!\n");
  closesocket(send_to);
}


recvinfo

decompiled_code:
__int64 __fastcall recvinfo(unsigned int a1, void *a2, size_t a3)
{
unsigned int v3; // ebx

v3 = recv(*(&sockets.fd + 2 * (int)a1), a2, a3, 0);
printf(" got something from %d : %d bytes\n", a1, v3);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1, void *a2, size_t a3)
{
unsigned int v3; // ebx

v3 = recv(*(&sockets.fd + 2 * (int)a1), a2, a3, 0);
printf(" got something from %d : %d bytes\n", a1, v3);
return v3;
}

original_code:
int recvinfo(int i, char * buf, ssize_t size)
{
  int r =recv(sockets[i].fd, buf, size, 0);
  printf(" got something from %d : %d bytes\n", i, r);
  return r;
}

canonical_original_code:
int recvinfo(int i, char *buf, long size)
{
  int r = recv(sockets[i].fd, buf, size, 0);
  printf(" got something from %d : %d bytes\n", i, r);
  return r;
}

variable_types:
buf
char *

variable_types: size long

variable_types: r int


sendinfo

decompiled_code:
__int64 __fastcall sendinfo(int a1)
{
unsigned int v2; // eax

if ( connections[a1] >= 0 )
return 0LL;
send_to = a1;
v2 = send(*(&sockets.fd + 2 * a1), &connections[a1], 4uLL, 0);
if ( v2 == -1 )
perror("snd");
else
printf(" sent %d bytes to %d\n", v2, (unsigned int)a1);
if ( (unsigned __int8)active_socket_nr > a1 )
HIBYTE(connections[a1]) &= 0x7Fu;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1)
{
unsigned int v2; // eax

if ( connections[a1] >= 0 )
return 0LL;
send_to = a1;
v2 = send(*(&sockets.fd + 2 * a1), &connections[a1], 4uLL, 0);
if ( v2 == -1 )
perror("snd");
else
printf(" sent %d bytes to %d\n", v2, (unsigned int)a1);
if ( (unsigned __int8)active_socket_nr > a1 )
HIBYTE(connections[a1]) &= 0x7Fu;
return 0LL;
}

original_code:
int sendinfo(int i)
{
  if(connections[i].send_info != 1) {
    return 0;
  }
  send_to = i;
  int r;
  { { if((r = send(sockets[i].fd, &connections[i].count, sizeof(int), 0)) == -1) { perror("snd"); } } };
  if(r != -1) {
    printf(" sent %d bytes to %d\n", r, i);
  }
  if(i < active_socket_nr) {
    connections[i].send_info = 0;
  }
  return 0;
}


startBackend

decompiled_code:
void startBackend()
{
unsigned __int64 v0; // rax
__int64 v1; // r13
unsigned int v2; // eax
unsigned int v3; // ebp
int v4; // eax
int v5; // er12
unsigned int v6; // er14
__int64 v7; // r8
int v8; // eax
__int64 v9; // rbx
__int16 v10; // ax
int v11; // ebx
char v12[268]; // [rsp+0h] [rbp-218h] BYREF
socklen_t addr_len; // [rsp+10Ch] [rbp-10Ch] BYREF
struct sockaddr v14; // [rsp+110h] [rbp-108h] BYREF
struct sockaddr addr; // [rsp+180h] [rbp-98h] BYREF
__int64 v16; // [rsp+19Ah] [rbp-7Eh]
__int64 v17; // [rsp+1A2h] [rbp-76h]
__int64 v18; // [rsp+1AAh] [rbp-6Eh]
__int64 v19; // [rsp+1B2h] [rbp-66h]
__int64 v20; // [rsp+1BAh] [rbp-5Eh]
__int64 v21; // [rsp+1C2h] [rbp-56h]
__int64 v22; // [rsp+1CAh] [rbp-4Eh]
__int64 v23; // [rsp+1D2h] [rbp-46h]
__int64 v24; // [rsp+1DAh] [rbp-3Eh]
__int64 v25; // [rsp+1E2h] [rbp-36h]
__int16 v26; // [rsp+1EAh] [rbp-2Eh]
char v27; // [rsp+1ECh] [rbp-2Ch]
char v28; // [rsp+1EDh] [rbp-2Bh]

unlink("/my_count_socket");
backend_s = socket(1, 2049, 0);
if ( backend_s == -1 )
perror("socket");
v28 = 0;
addr.sa_family = 1;
strcpy(addr.sa_data, "/my_count_socket");
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v20 = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0;
v27 = 0;
if ( bind(backend_s, &addr, 0x6Eu) == -1 )
perror("bind");
addr_len = 110;
listen(backend_s, 50);
v0 = (unsigned int)((_DWORD)&sockets + 4);
do
{
*(_WORD *)v0 = 7;
v0 += 8LL;
}
while ( v0 != (_DWORD)&sockets + 404 );
signal(2, (__sighandler_t)closeall);
signal(13, handle_sigpipe);
v1 = -1LL;
do
{
v11 = accept(backend_s, &v14, &addr_len);
if ( v11 != -1 )
{
puts("New connection!");
v2 = first_free_socket();
v3 = v2;
if ( v2 == -1 )
{
fwrite("Too many connections\n", 1uLL, 0x15uLL, stderr);
}
else
{
printf("Accepting connection at %d\n", v2);
*(&sockets.fd + 2 * (int)v3) = v11;
if ( fcntl(v11, 2, 2048LL) == -1 )
perror("fcntl");
}
}
if ( poll(&sockets, (unsigned __int8)active_socket_nr, 500) == -1 )
perror("poll");
if ( active_socket_nr )
{
v9 = 0LL;
do
{
v10 = *(&sockets.revents + 4 * v9);
if ( v10 )
{
if ( (v10 & 3) == 0 || (v4 = recvinfo(v9, v12, 0x100uLL), v5 = v4, v4 == -1) )
{
if ( (*(_BYTE *)(&sockets.revents + 4 * v9) & 4) != 0 )
sendinfo(v9);
}
else
{
v6 = bit_count(v12, v4);
v7 = LODWORD(connections[v9]) + v6;
LODWORD(connections[v9]) = v7;
v12[v5] = 0;
printf(" bitcount of %s : %d | from %d, total %u \n", v12, v6, (unsigned int)v9, v7);
if ( v6 )
{
HIBYTE(connections[(int)v9]) |= 0x80u;
}
else
{
v8 = HIDWORD(connections[(int)v9]) & 0x7FFFFFFF;
if ( v8 == 1 )
{
printf("Goodbye %d!\n", (unsigned int)v9);
closesocket(v9);
}
else
{
HIDWORD(connections[(int)v9]) = HIDWORD(connections[(int)v9]) & 0x80000000 | (v8 + 1) & 0x7FFFFFFF;
HIBYTE(connections[(int)v9]) |= 0x80u;
}
}
}
}
++v9;
}
while ( (unsigned __int8)active_socket_nr > (int)v9 );
}
--v1;
}
while ( v1 );
}

canonical_decompiled_code:
void func14()
{
unsigned __int64 v0; // rax
__int64 v1; // r13
unsigned int v2; // eax
unsigned int v3; // ebp
int v4; // eax
int v5; // er12
unsigned int v6; // er14
__int64 v7; // r8
int v8; // eax
__int64 v9; // rbx
__int16 v10; // ax
int v11; // ebx
char v12[268]; // [rsp+0h] [rbp-218h] BYREF
socklen_t addr_len; // [rsp+10Ch] [rbp-10Ch] BYREF
struct sockaddr v14; // [rsp+110h] [rbp-108h] BYREF
struct sockaddr addr; // [rsp+180h] [rbp-98h] BYREF
__int64 v16; // [rsp+19Ah] [rbp-7Eh]
__int64 v17; // [rsp+1A2h] [rbp-76h]
__int64 v18; // [rsp+1AAh] [rbp-6Eh]
__int64 v19; // [rsp+1B2h] [rbp-66h]
__int64 v20; // [rsp+1BAh] [rbp-5Eh]
__int64 v21; // [rsp+1C2h] [rbp-56h]
__int64 v22; // [rsp+1CAh] [rbp-4Eh]
__int64 v23; // [rsp+1D2h] [rbp-46h]
__int64 v24; // [rsp+1DAh] [rbp-3Eh]
__int64 v25; // [rsp+1E2h] [rbp-36h]
__int16 v26; // [rsp+1EAh] [rbp-2Eh]
char v27; // [rsp+1ECh] [rbp-2Ch]
char v28; // [rsp+1EDh] [rbp-2Bh]

unlink("/my_count_socket");
backend_s = socket(1, 2049, 0);
if ( backend_s == -1 )
perror("socket");
v28 = 0;
addr.sa_family = 1;
strcpy(addr.sa_data, "/my_count_socket");
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v20 = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0;
v27 = 0;
if ( bind(backend_s, &addr, 0x6Eu) == -1 )
perror("bind");
addr_len = 110;
listen(backend_s, 50);
v0 = (unsigned int)((_DWORD)&sockets + 4);
do
{
*(_WORD *)v0 = 7;
v0 += 8LL;
}
while ( v0 != (_DWORD)&sockets + 404 );
signal(2, (__sighandler_t)closeall);
signal(13, handle_sigpipe);
v1 = -1LL;
do
{
v11 = accept(backend_s, &v14, &addr_len);
if ( v11 != -1 )
{
puts("New connection!");
v2 = func8();
v3 = v2;
if ( v2 == -1 )
{
fwrite("Too many connections\n", 1uLL, 0x15uLL, stderr);
}
else
{
printf("Accepting connection at %d\n", v2);
*(&sockets.fd + 2 * (int)v3) = v11;
if ( fcntl(v11, 2, 2048LL) == -1 )
perror("fcntl");
}
}
if ( poll(&sockets, (unsigned __int8)active_socket_nr, 500) == -1 )
perror("poll");
if ( active_socket_nr )
{
v9 = 0LL;
do
{
v10 = *(&sockets.revents + 4 * v9);
if ( v10 )
{
if ( (v10 & 3) == 0 || (v4 = func12(v9, v12, 0x100uLL), v5 = v4, v4 == -1) )
{
if ( (*(_BYTE *)(&sockets.revents + 4 * v9) & 4) != 0 )
func13(v9);
}
else
{
v6 = func2(v12, v4);
v7 = LODWORD(connections[v9]) + v6;
LODWORD(connections[v9]) = v7;
v12[v5] = 0;
printf(" bitcount of %s : %d | from %d, total %u \n", v12, v6, (unsigned int)v9, v7);
if ( v6 )
{
HIBYTE(connections[(int)v9]) |= 0x80u;
}
else
{
v8 = HIDWORD(connections[(int)v9]) & 0x7FFFFFFF;
if ( v8 == 1 )
{
printf("Goodbye %d!\n", (unsigned int)v9);
func9(v9);
}
else
{
HIDWORD(connections[(int)v9]) = HIDWORD(connections[(int)v9]) & 0x80000000 | (v8 + 1) & 0x7FFFFFFF;
HIBYTE(connections[(int)v9]) |= 0x80u;
}
}
}
}
++v9;
}
while ( (unsigned __int8)active_socket_nr > (int)v9 );
}
--v1;
}
while ( v1 );
}

original_code:
void startBackend()
{
  unlink("/my_count_socket");
  backend_s = socket(1, SOCK_STREAM | SOCK_NONBLOCK, 0);
  { { if((backend_s) == -1) { perror("socket"); } } };
  struct sockaddr_un s_addr;
  memset(&s_addr, 0, sizeof(struct sockaddr_un));
  s_addr.sun_family = 1;
  strncpy(s_addr.sun_path, "/my_count_socket", sizeof(s_addr.sun_path) - 1);
  { { if((bind(backend_s, &s_addr, sizeof(struct sockaddr_un))) == -1) { perror("bind"); } } };
  struct sockaddr_un peer_addr;
  socklen_t peer_addr_size = sizeof(struct sockaddr_un);
  listen(backend_s, 50);
  for(int i=0; i<50; i++) {
    sockets[i].events = 0x001 | 0x002 | 0x004;
  }
  unsigned long long quit = 1;
  signal(2, &closeall);
  signal(13, &handle_sigpipe);
  while(quit!=0) {
    quit++;
    int a = accept(backend_s, &peer_addr, &peer_addr_size);
    if(a != -1) {
      printf("New connection!\n");
      int i = first_free_socket();
      if(i == -1) {
        fprintf(stderr, "Too many connections\n");
      } else {
        printf("Accepting connection at %d\n", i);
        sockets[i].fd = a;
        { { if((fcntl(a, 2, 04000)) == -1) { perror("fcntl"); } } };
      }
    }
    { { if((poll(sockets, (int)active_socket_nr, 500)) == -1) { perror("poll"); } } };
    for (int i = 0; i < active_socket_nr; i++) {
      if(sockets[i].revents != 0) {
        char buf[256];
        int size;
        if((sockets[i].revents & (0x001 | 0x002)) > 0
            && (size = recvinfo(i, buf, 256)) != -1) {
          int ones = bit_count(buf, size);
          connections[i].count = connections[i].count + ones;
          buf[size] = '\0';
          printf("  bitcount of %s : %d | from %d, total %u \n", buf, ones, i, connections[i].count);
          if(ones == 0) {
            if(connections[i].zero_count == 1) {
              printf("Goodbye %d!\n", i);
              closesocket(i);
            } else {
              connections[i].zero_count++;
              connections[i].send_info = 1;
            }
          } else {
            connections[i].send_info = 1;
          }
        } else {
          if((sockets[i].revents & (0x004)) > 0) {
            sendinfo(i);
          }
        }
      }
    }
  }
}

canonical_original_code:
void startBackend()
{
  unlink("/my_count_socket");
  backend_s = socket(1, SOCK_STREAM | SOCK_NONBLOCK, 0);
  { { if((backend_s) == -1) { perror("socket"); } } };
  struct sockaddr_un s_addr;
  memset(&s_addr, 0, sizeof(struct sockaddr_un));
  s_addr.sun_family = 1;
  strncpy(s_addr.sun_path, "/my_count_socket", sizeof(s_addr.sun_path) - 1);
  { { if((bind(backend_s, &s_addr, sizeof(struct sockaddr_un))) == -1) { perror("bind"); } } };
  struct sockaddr_un peer_addr;
  unsigned int peer_addr_size = sizeof(struct sockaddr_un);
  listen(backend_s, 50);
  for(int i = 0; i<50; i++) {
    sockets[i].events = 0x001 | 0x002 | 0x004;
  }
  unsigned long long quit = 1;
  signal(2, &closeall);
  signal(13, &handle_sigpipe);
  while(quit!=0) {
    quit++;
    int a = accept(backend_s, &peer_addr, &peer_addr_size);
    if(a != -1) {
      printf("New connection!\n");
      int i = first_free_socket();
      if(i == -1) {
        fprintf(stderr, "Too many connections\n");
      } else {
        printf("Accepting connection at %d\n", i);
        sockets[i].fd = a;
        { { if((fcntl(a, 2, 04000)) == -1) { perror("fcntl"); } } };
      }
    }
    { { if((poll(sockets, (int)active_socket_nr, 500)) == -1) { perror("poll"); } } };
    for (int i = 0; i < active_socket_nr; i++) {
      if(sockets[i].revents != 0) {
        char buf[256];
        int size;
        if((sockets[i].revents & (0x001 | 0x002)) > 0
            && (size = recvinfo(i, buf, 256)) != -1) {
          int ones = bit_count(buf, size);
          connections[i].count = connections[i].count + ones;
          buf[size] = '\0';
          printf("  bitcount of %s : %d | from %d, total %u \n", buf, ones, i, connections[i].count);
          if(ones == 0) {
            if(connections[i].zero_count == 1) {
              printf("Goodbye %d!\n", i);
              closesocket(i);
            } else {
              connections[i].zero_count++;
              connections[i].send_info = 1;
            }
          } else {
            connections[i].send_info = 1;
          }
        } else {
          if((sockets[i].revents & (0x004)) > 0) {
            sendinfo(i);
          }
        }
      }
    }
  }
}

variable_types:
quit
unsigned long long

variable_types: a int

variable_types: size int

variable_types: ones int

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


fdQCleanUp

decompiled_code:
void fdQCleanUp()
{
void *v0; // rbx
void *v1; // rdi

v0 = fdQ;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 1);
free(v1);
}
}

canonical_decompiled_code:
void func7()
{
void *v0; // rbx
void *v1; // rdi

v0 = fdQ;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 1);
free(v1);
}
}

original_code:
void fdQCleanUp()
{
  for(struct Q* i = fdQ; i != ((void *)0);) {
    struct Q* n = i->next;
    free(i);
    i = n;
  }
}

canonical_original_code:
void fdQCleanUp()
{
  for(struct Q *i = fdQ; i != ((void *)0);) {
    struct Q *n = i->next;
    free(i);
    i = n;
  }
}

variable_types: struct Q { int fd; int pid; struct Q *next; } *


delay

decompiled_code:
void __fastcall delay(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

canonical_decompiled_code:
void __fastcall func8(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

original_code:
void delay(int r, int m)
{
  struct timespec nSleepTime;
  memset(&nSleepTime, 0, sizeof(struct timespec));
  int t = rand() % (r-m) + m;
  nSleepTime.tv_nsec = t % (1000000000);
  nSleepTime.tv_sec = t / (1000000000);
  { { if((nanosleep(&nSleepTime, &nSleepTime)) == -1) { perror("nanosleep"); } } };
}

variable_types: m int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: t int


find

decompiled_code:
__int64 __fastcall find(__int64 a1, int a2)
{
if ( !a1 )
return 0xFFFFFFFFLL;
while ( *(_DWORD *)a1 != a2 )
{
a1 = *(_QWORD *)(a1 + 8);
if ( !a1 )
return 0xFFFFFFFFLL;
}
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
if ( !a1 )
return 0xFFFFFFFFLL;
while ( *(_DWORD *)a1 != a2 )
{
a1 = *(_QWORD *)(a1 + 8);
if ( !a1 )
return 0xFFFFFFFFLL;
}
return *(unsigned int *)(a1 + 4);
}

original_code:
pid_t find(struct Q* start, int f)
{
  for(;start != ((void *)0); start = start->next) {
    if(start->fd == f) {
      return start->pid;
    }
  }
  return -1;
}

canonical_original_code:
int find(struct Q *start, int f)
{
  for(;start != ((void *)0); start = start->next) {
    if(start->fd == f) {
      return start->pid;
    }
  }
  return -1;
}

variable_types: f int

user_defined_types: struct Q { int fd; int pid; struct Q *next; }


mpopen

decompiled_code:
__int64 __fastcall mpopen(const char *a1, _BYTE *a2)
{
int v2; // er12
int v3; // edx
int v4; // er13
unsigned int v5; // ebp
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

if ( pipe(pipedes) == -1 )
{
perror("pipe failed");
if ( fork() != -1 )
goto LABEL_10;
goto LABEL_9;
}
v3 = fork();
v4 = v3;
if ( v3 == -1 )
{
LABEL_9:
perror("fork failed");
LABEL_10:
exit(1);
}
if ( v3 )
{
v5 = pipedes[1];
if ( *a2 == 114 )
v5 = pipedes[0];
push((__int64 *)&fdQ, v5, v3);
signal(17, (__sighandler_t)1);
}
else
{
if ( *a2 == 114 )
{
v2 = pipedes[1];
v4 = 1;
}
else if ( *a2 == 119 )
{
v2 = pipedes[0];
}
else
{
fwrite("Bad mpopen arg!\n", 1uLL, 0x10uLL, stderr);
v4 = 1;
}
printf("executing %s..\n", a1);
signal(1, (__sighandler_t)exit);
dup2(v2, v4);
execl("/usr/bin/sh", "/usr/bin/sh", "-c", a1, 0LL);
v5 = -1;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func11(const char *a1, _BYTE *a2)
{
int v2; // er12
int v3; // edx
int v4; // er13
unsigned int v5; // ebp
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

if ( pipe(pipedes) == -1 )
{
perror("pipe failed");
if ( fork() != -1 )
goto LABEL_10;
goto LABEL_9;
}
v3 = fork();
v4 = v3;
if ( v3 == -1 )
{
LABEL_9:
perror("fork failed");
LABEL_10:
exit(1);
}
if ( v3 )
{
v5 = pipedes[1];
if ( *a2 == 114 )
v5 = pipedes[0];
func9((__int64 *)&fdQ, v5, v3);
signal(17, (__sighandler_t)1);
}
else
{
if ( *a2 == 114 )
{
v2 = pipedes[1];
v4 = 1;
}
else if ( *a2 == 119 )
{
v2 = pipedes[0];
}
else
{
fwrite("Bad mpopen arg!\n", 1uLL, 0x10uLL, stderr);
v4 = 1;
}
printf("executing %s..\n", a1);
signal(1, (__sighandler_t)exit);
dup2(v2, v4);
execl("/usr/bin/sh", "/usr/bin/sh", "-c", a1, 0LL);
v5 = -1;
}
return v5;
}

original_code:
int mpopen(const char* command, const char* type)
{
  int fs[2],rp;
  { { if((rp = pipe(fs)) == -1) { perror("pipe failed"); } } };
  int r;
  { { if((r = fork()) == -1) { perror("fork failed"); } } };
  if(rp == -1 || r == -1)
  {
    exit(1);
  }
  int of,nf;
  if(r == 0) {
    if( (*type) == 'r' ) {
      nf = 1;
      of = fs[1];
    }
    if( (*type) == 'w' ) {
      nf = 0;
      of = fs[0];
    }
    if((*type) != 'w' && (*type) != 'r') {
      fprintf(stderr, "Bad mpopen arg!\n");
    }
    printf("executing %s..\n", command);
    signal(1, &exit);
    char* SH = "/usr/bin/sh";
    dup2(of,nf);
    execl(SH, SH, "-c", command, ((void *)0));
  } else {
    int ret;
    if( (*type) == 'r' ) {
      ret = fs[0];
    } else {
      ret = fs[1];
    }
    push(&fdQ, ret, r);
    signal(17, ((__sighandler_t) 1));
    return ret;
  }
  return -1;
}

canonical_original_code:
int mpopen(char *command, char *type)
{
  int fs[2]; int rp;
  { { if((rp = pipe(fs)) == -1) { perror("pipe failed"); } } };
  int r;
  { { if((r = fork()) == -1) { perror("fork failed"); } } };
  if(rp == -1 || r == -1)
  {
    exit(1);
  }
  int of; int nf;
  if(r == 0) {
    if( (*type) == 'r' ) {
      nf = 1;
      of = fs[1];
    }
    if( (*type) == 'w' ) {
      nf = 0;
      of = fs[0];
    }
    if((*type) != 'w' && (*type) != 'r') {
      fprintf(stderr, "Bad mpopen arg!\n");
    }
    printf("executing %s..\n", command);
    signal(1, &exit);
    char *SH = "/usr/bin/sh";
    dup2(of,nf);
    execl(SH, SH, "-c", command, ((void *)0));
  } else {
    int ret;
    if( (*type) == 'r' ) {
      ret = fs[0];
    } else {
      ret = fs[1];
    }
    push(&fdQ, ret, r);
    signal(17, ((void (*)(int)) 1));
    return ret;
  }
  return -1;
}

variable_types:
command
char *

variable_types:
type
char *

variable_types:
fs
int [2]

variable_types: rp int

variable_types: of int

variable_types: nf int

variable_types:
SH
char *

variable_types: ret int


mpclose

decompiled_code:
__int64 __fastcall mpclose(unsigned int a1)
{
unsigned int v1; // eax
__pid_t v2; // ebx
unsigned int v3; // ebx

v1 = find((__int64)fdQ, a1);
v2 = v1;
if ( v1 == -1 )
perror("file descriptor search failed");
else
printf("\nClosing fd %d : pid %d..\n", a1, v1);
v3 = kill(v2, 1);
if ( v3 == -1 )
perror("kill failed");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1)
{
unsigned int v1; // eax
__pid_t v2; // ebx
unsigned int v3; // ebx

v1 = func10((__int64)fdQ, a1);
v2 = v1;
if ( v1 == -1 )
perror("file descriptor search failed");
else
printf("\nClosing fd %d : pid %d..\n", a1, v1);
v3 = kill(v2, 1);
if ( v3 == -1 )
perror("kill failed");
return v3;
}

original_code:
int mpclose(int f)
{
  pid_t p;
  { { if((p = find(fdQ, f)) == -1) { perror("file descriptor search failed"); } } };
  if(p != -1) {
    printf("\nClosing fd %d : pid %d..\n", f, p);
  }
  { { if((p = kill(p, 1)) == -1) { perror("kill failed"); } } };
  return p;
}

canonical_original_code:
int mpclose(int f)
{
  int p;
  { { if((p = find(fdQ, f)) == -1) { perror("file descriptor search failed"); } } };
  if(p != -1) {
    printf("\nClosing fd %d : pid %d..\n", f, p);
  }
  { { if((p = kill(p, 1)) == -1) { perror("kill failed"); } } };
  return p;
}

variable_types: p int


delay

canonical_decompiled_code:
void __fastcall func7(int a1, int a2)
{
int v2; // edx
struct timespec v3[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3[0].tv_nsec = v2 % 1000000000;
v3[0].tv_sec = v2 / 1000000000;
if ( nanosleep(v3, v3) == -1 )
perror("nanosleep");
}

canonical_original_code:
void delay(int r, int m)
{
  struct timespec nSleepTime;
  memset(&nSleepTime, 0, sizeof(struct timespec));
  int t = rand() % (r-m) + m;
  nSleepTime.tv_nsec = t % (1000000000);
  nSleepTime.tv_sec = t / (1000000000);
  { { if((nanosleep(&nSleepTime, &nSleepTime)) == -1) { perror("nanosleep"); } } };
}

variable_types: r int


decompiled_code:
__int64 cs_wait()
{
if ( mq_receive() == -1 )
perror("receive failed");
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
if ( mq_receive() == -1 )
perror("receive failed");
return 0LL;
}

original_code:
int cs_wait(cs_t* q)
{
  char b[256];
  { { if((mq_receive(q->queue, b, 256, ((void *)0))) == -1) { perror("receive failed"); } } };
  return 0;
}

canonical_original_code:
int cs_wait(struct cs *q)
{
  char b[256];
  { { if((mq_receive(q->queue, b, 256, ((void *)0))) == -1) { perror("receive failed"); } } };
  return 0;
}

variable_types:
q
struct cs { int queue; } *

variable_types:
b
char [256]


decompiled_code:
__int64 cs_post()
{
if ( (unsigned int)mq_send() == -1 )
perror("send failed");
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
if ( (unsigned int)mq_send() == -1 )
perror("send failed");
return 0LL;
}

original_code:
int cs_post(cs_t* q)
{
  { { if((mq_send(q->queue, "a", 1, 100)) == -1) { perror("send failed"); } } };
  return 0;
}

canonical_original_code:
int cs_post(struct cs *q)
{
  { { if((mq_send(q->queue, "a", 1, 100)) == -1) { perror("send failed"); } } };
  return 0;
}


decompiled_code:
__int64 __fastcall cs_close(void *ptr)
{
mq_close();
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(void *ptr)
{
mq_close();
free(ptr);
return 0LL;
}

original_code:
int cs_close(cs_t* c)
{
  mq_close(c->queue);
  free(c);
  return 0;
}

canonical_original_code:
int cs_close(struct cs *c)
{
  mq_close(c->queue);
  free(c);
  return 0;
}

variable_types:
c
struct cs { int queue; } *


decompiled_code:
int write_siginfo()
{
fprintf(stderr, "si_addr: %p\n", (const void *)xmmword_4040D0);
return fprintf(stderr, "si_code: %d\n", (unsigned int)dword_4040C8);
}

canonical_decompiled_code:
int func7()
{
fprintf(stderr, "si_addr: %p\n", (const void *)xmmword_4040D0);
return fprintf(stderr, "si_code: %d\n", (unsigned int)dword_4040C8);
}

original_code:
void write_siginfo()
{
  fprintf(stderr, "si_addr: %p\n", SI._sifields._sigfault.si_addr);
  fprintf(stderr, "si_code: %d\n", SI.si_code);
}