Producer

decompiled_code:
__int64 __fastcall Producer(int *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r13
unsigned int v4; // er15
int v5; // ecx
unsigned int v6; // ebx
FILE *v7; // r14
pthread_t v8; // rax
__int64 v9; // r9
pthread_cond_t *v10; // rbx
__int64 result; // rax
unsigned int v12; // [rsp+8h] [rbp-60h]
unsigned int v13; // [rsp+Ch] [rbp-5Ch]
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 )
{
v10 = (pthread_cond_t *)(a1 + 12);
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast(v10) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v6 = cCount;
v7 = stderr;
if ( 5 * cCount < pCount )
break;
v13 = v5;
v12 = pCount;
v8 = pthread_self();
v9 = v6;
v10 = (pthread_cond_t *)(a1 + 12);
fprintf(v7, "\n P%u : itemCount: %d maxItems: %d, C%d P%d)\n", v8, v13, v4, v9, v12);
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(int *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r13
unsigned int v4; // er15
int v5; // ecx
unsigned int v6; // ebx
FILE *v7; // r14
pthread_t v8; // rax
__int64 v9; // r9
pthread_cond_t *v10; // rbx
__int64 result; // rax
unsigned int v12; // [rsp+8h] [rbp-60h]
unsigned int v13; // [rsp+Ch] [rbp-5Ch]
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 )
{
v10 = (pthread_cond_t *)(a1 + 12);
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast(v10) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v6 = cCount;
v7 = stderr;
if ( 5 * cCount < pCount )
break;
v13 = v5;
v12 = pCount;
v8 = pthread_self();
v9 = v6;
v10 = (pthread_cond_t *)(a1 + 12);
fprintf(v7, "\n P%u : itemCount: %d maxItems: %d, C%d P%d)\n", v8, v13, v4, v9, v12);
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(pthread_cond_t *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r13
int v4; // er15
__int64 v5; // rbx
FILE *v6; // r14
pthread_t v7; // rax
pthread_cond_t *v8; // rbx
__int64 result; // rax
__int64 v10; // [rsp-10h] [rbp-78h]
unsigned int v11; // [rsp+Ch] [rbp-5Ch]
struct timespec tp; // [rsp+20h] [rbp-48h] BYREF

v3 = (pthread_mutex_t *)(&a1->__align + 1);
pthread_mutex_lock((pthread_mutex_t *)(&a1->__align + 1));
v4 = a1->__futex;
if ( v4 > 0 )
{
v8 = a1 + 2;
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast(v8) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v5 = (unsigned int)pCount;
v6 = stderr;
if ( 5 * pCount < cCount )
break;
v11 = cCount;
v7 = pthread_self();
v10 = v5;
v8 = a1 + 2;
fprintf(
v6,
"\n C%u : itemCount: %d maxItems: %d, C%d P%d \n",
v7,
(unsigned int)v4,
(unsigned int)a1->__lock,
v11,
v10);
if ( pthread_cond_broadcast(a1 + 2) == -1 )
perror("broadcast failed");
clock_gettime(0, &tp);
tp.tv_sec += 5LL;
pthread_cond_timedwait(a1 + 1, v3, &tp);
v4 = a1->__futex;
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(pthread_cond_t *a1, void (__fastcall *a2)(__int64), __int64 a3)
{
pthread_mutex_t *v3; // r13
int v4; // er15
__int64 v5; // rbx
FILE *v6; // r14
pthread_t v7; // rax
pthread_cond_t *v8; // rbx
__int64 result; // rax
__int64 v10; // [rsp-10h] [rbp-78h]
unsigned int v11; // [rsp+Ch] [rbp-5Ch]
struct timespec tp; // [rsp+20h] [rbp-48h] BYREF

v3 = (pthread_mutex_t *)(&a1->__align + 1);
pthread_mutex_lock((pthread_mutex_t *)(&a1->__align + 1));
v4 = a1->__futex;
if ( v4 > 0 )
{
v8 = a1 + 2;
LABEL_8:
a2(a3);
if ( pthread_cond_broadcast(v8) == -1 )
perror("broadcast failed");
pthread_mutex_unlock(v3);
result = 0LL;
}
else
{
while ( 1 )
{
v5 = (unsigned int)pCount;
v6 = stderr;
if ( 5 * pCount < cCount )
break;
v11 = cCount;
v7 = pthread_self();
v10 = v5;
v8 = a1 + 2;
fprintf(
v6,
"\n C%u : itemCount: %d maxItems: %d, C%d P%d \n",
v7,
(unsigned int)v4,
(unsigned int)a1->__lock,
v11,
v10);
if ( pthread_cond_broadcast(a1 + 2) == -1 )
perror("broadcast failed");
clock_gettime(0, &tp);
tp.tv_sec += 5LL;
pthread_cond_timedwait(a1 + 1, v3, &tp);
v4 = a1->__futex;
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;
}

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

return_type: int

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_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long 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; }

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

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


delay

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

v2 = a2 + rand() % (a1 - a2);
v3.tv_sec = v2 / 1000000000;
v3.tv_nsec = 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; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3.tv_sec = v2 / 1000000000;
v3.tv_nsec = 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: t int

return_type: void


openSharedMem

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

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

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

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

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

*(_QWORD *)(a1 + 32) = 0LL;
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

*(_QWORD *)(a1 + 32) = 0LL;
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:
int __fastcall restaurant_destroy(sem_t *a1)
{
int v1; // eax
pthread_mutex_t *v2; // rdi

v1 = sem_destroy(a1);
v2 = (pthread_mutex_t *)(&a1[1].__align + 1);
if ( v1 != -1 )
return pthread_mutex_destroy(v2);
pthread_mutex_destroy(v2);
return -1;
}

canonical_decompiled_code:
int __fastcall func10(sem_t *a1)
{
int v1; // eax
pthread_mutex_t *v2; // rdi

v1 = sem_destroy(a1);
v2 = (pthread_mutex_t *)(&a1[1].__align + 1);
if ( v1 != -1 )
return pthread_mutex_destroy(v2);
pthread_mutex_destroy(v2);
return -1;
}

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

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


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

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);
v2 = sem[1].__align;
if ( v2 > 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);
v2 = sem[1].__align;
}
*(_DWORD *)sem[1].__size = v2 + 1;
if ( pthread_mutex_unlock(v1) == -1 )
perror("ulck");
}

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

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);
v2 = sem[1].__align;
if ( v2 > 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);
v2 = sem[1].__align;
}
*(_DWORD *)sem[1].__size = v2 + 1;
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)
{
pthread_mutex_t *v1; // r12
int v2; // eax
int v3; // ebp
int v4; // eax
int v5; // edx

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

canonical_decompiled_code:
void __fastcall func12(sem_t *sem)
{
pthread_mutex_t *v1; // r12
int v2; // eax
int v3; // ebp
int v4; // eax
int v5; // edx

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

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; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3.tv_sec = v2 / 1000000000;
v3.tv_nsec = 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"); } } };
}


decompiled_code:
__int64 __fastcall bit_count(char *a1, int a2)
{
char *v2; // rbx
unsigned int v3; // er12
__int64 v4; // rbp
__int64 v5; // rdi

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

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, int a2)
{
char *v2; // rbx
unsigned int v3; // er12
__int64 v4; // rbp
__int64 v5; // rdi

if ( a2 <= 0 )
return 0LL;
v2 = a1;
v3 = 0;
v4 = (__int64)&a1[a2 - 1 + 1];
do
{
v5 = (unsigned int)*v2++;
v3 += _popcountdi2(v5);
}
while ( (char *)v4 != v2 );
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:
int startFrontend()
{
int result; // eax
char addr[18]; // [rsp+10h] [rbp-78h] BYREF
__int128 v2; // [rsp+22h] [rbp-66h]
__int128 v3; // [rsp+32h] [rbp-56h]
__int128 v4; // [rsp+42h] [rbp-46h]
__int128 v5; // [rsp+52h] [rbp-36h]
__int128 v6; // [rsp+62h] [rbp-26h]
__int64 v7; // [rsp+72h] [rbp-16h]
__int16 v8; // [rsp+7Ah] [rbp-Eh]
char v9; // [rsp+7Ch] [rbp-Ch]
char v10; // [rsp+7Dh] [rbp-Bh]

frontend_s = socket(1, 1, 0);
if ( frontend_s == -1 )
{
perror("socket");
result = -1;
}
else
{
signal(2, (__sighandler_t)closefront);
v8 = 0;
*(__m128i *)&addr[2] = _mm_load_si128((const __m128i *)&LC5);
v10 = 0;
*(_WORD *)addr = 1;
v7 = 0LL;
v9 = 0;
v2 = 0LL;
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
result = connect(frontend_s, (const struct sockaddr *)addr, 0x6Eu);
if ( result == -1 )
{
perror("connect");
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
char addr[18]; // [rsp+10h] [rbp-78h] BYREF
__int128 v2; // [rsp+22h] [rbp-66h]
__int128 v3; // [rsp+32h] [rbp-56h]
__int128 v4; // [rsp+42h] [rbp-46h]
__int128 v5; // [rsp+52h] [rbp-36h]
__int128 v6; // [rsp+62h] [rbp-26h]
__int64 v7; // [rsp+72h] [rbp-16h]
__int16 v8; // [rsp+7Ah] [rbp-Eh]
char v9; // [rsp+7Ch] [rbp-Ch]
char v10; // [rsp+7Dh] [rbp-Bh]

frontend_s = socket(1, 1, 0);
if ( frontend_s == -1 )
{
perror("socket");
result = -1;
}
else
{
signal(2, (__sighandler_t)closefront);
v8 = 0;
*(__m128i *)&addr[2] = _mm_load_si128((const __m128i *)&LC5);
v10 = 0;
*(_WORD *)addr = 1;
v7 = 0LL;
v9 = 0;
v2 = 0LL;
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
result = connect(frontend_s, (const struct sockaddr *)addr, 0x6Eu);
if ( result == -1 )
{
perror("connect");
result = -1;
}
}
return result;
}

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: r int

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 )
{
v4 = recv(frontend_s, buf, a3, 0);
*a2 = v4;
v5 = v4;
if ( v4 == -1 )
{
perror("rcv");
}
else if ( v4 == 4 )
{
itoa(*(_DWORD *)buf, (char *)buf);
return v5;
}
v5 = -1;
fprintf(stderr, "Error receiving, got %d bytes\n", *a2);
return v5;
}
perror("send");
return 0xFFFFFFFFLL;
}

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 )
{
v4 = recv(frontend_s, buf, a3, 0);
*a2 = v4;
v5 = v4;
if ( v4 == -1 )
{
perror("rcv");
}
else if ( v4 == 4 )
{
func3(*(_DWORD *)buf, (char *)buf);
return v5;
}
v5 = -1;
fprintf(stderr, "Error receiving, got %d bytes\n", *a2);
return v5;
}
perror("send");
return 0xFFFFFFFFLL;
}

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
__int64 v3; // rdx
__int64 result; // rax

if ( a1 != a2 )
{
v2 = connections[a2];
connections[a2] = connections[a1];
v3 = *((_QWORD *)&sockets + a1);
connections[a1] = v2;
result = *((_QWORD *)&sockets + a2);
*((_QWORD *)&sockets + a2) = v3;
*((_QWORD *)&sockets + a1) = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
__int64 v2; // rax
__int64 v3; // rdx
__int64 result; // rax

if ( a1 != a2 )
{
v2 = connections[a2];
connections[a2] = connections[a1];
v3 = *((_QWORD *)&sockets + a1);
connections[a1] = v2;
result = *((_QWORD *)&sockets + a2);
*((_QWORD *)&sockets + a2) = v3;
*((_QWORD *)&sockets + 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: 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

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

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

result = (unsigned __int8)active_socket_nr;
if ( (unsigned __int8)active_socket_nr > 0x31u )
return 0xFFFFFFFFLL;
++active_socket_nr;
return 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)
{
char v2; // al

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

canonical_decompiled_code:
int __fastcall func9(int a1)
{
char v2; // al

if ( (unsigned __int8)active_socket_nr > a1 )
{
close(*(&sockets.fd + 2 * a1));
v2 = active_socket_nr;
connections[a1] = 0LL;
active_socket_nr = v2 - 1;
if ( v2 != 1 )
func7(a1, (unsigned __int8)(v2 - 1));
}
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);
}

variable_types: i int


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 ( active_socket_nr )
{
do
closesocket(v1);
while ( v1-- != 0 );
}
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 ( active_socket_nr )
{
do
func9(v1);
while ( v1-- != 0 );
}
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


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 )
return 0LL;
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 )
return 0LL;
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()
{
int v0; // edi
__int64 v1; // rax
__int64 v2; // r13
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // rbx
__int64 v6; // rbx
__int16 v7; // ax
int v8; // eax
__int64 v9; // r12
int v10; // edi
unsigned int v11; // eax
int v12; // er8
unsigned int v13; // er14
__int64 v14; // r8
int v15; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-20Ch] BYREF
char addr[18]; // [rsp+10h] [rbp-208h] BYREF
__int128 v18; // [rsp+22h] [rbp-1F6h]
__int128 v19; // [rsp+32h] [rbp-1E6h]
__int128 v20; // [rsp+42h] [rbp-1D6h]
__int128 v21; // [rsp+52h] [rbp-1C6h]
__int128 v22; // [rsp+62h] [rbp-1B6h]
__int64 v23; // [rsp+72h] [rbp-1A6h]
__int16 v24; // [rsp+7Ah] [rbp-19Eh]
char v25; // [rsp+7Ch] [rbp-19Ch]
char v26; // [rsp+7Dh] [rbp-19Bh]
struct sockaddr v27; // [rsp+80h] [rbp-198h] BYREF
char v28[296]; // [rsp+F0h] [rbp-128h] BYREF

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

canonical_decompiled_code:
void func14()
{
int v0; // edi
__int64 v1; // rax
__int64 v2; // r13
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // rbx
__int64 v6; // rbx
__int16 v7; // ax
int v8; // eax
__int64 v9; // r12
int v10; // edi
unsigned int v11; // eax
int v12; // er8
unsigned int v13; // er14
__int64 v14; // r8
int v15; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-20Ch] BYREF
char addr[18]; // [rsp+10h] [rbp-208h] BYREF
__int128 v18; // [rsp+22h] [rbp-1F6h]
__int128 v19; // [rsp+32h] [rbp-1E6h]
__int128 v20; // [rsp+42h] [rbp-1D6h]
__int128 v21; // [rsp+52h] [rbp-1C6h]
__int128 v22; // [rsp+62h] [rbp-1B6h]
__int64 v23; // [rsp+72h] [rbp-1A6h]
__int16 v24; // [rsp+7Ah] [rbp-19Eh]
char v25; // [rsp+7Ch] [rbp-19Ch]
char v26; // [rsp+7Dh] [rbp-19Bh]
struct sockaddr v27; // [rsp+80h] [rbp-198h] BYREF
char v28[296]; // [rsp+F0h] [rbp-128h] BYREF

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

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; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3.tv_sec = v2 / 1000000000;
v3.tv_nsec = 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; // [rsp+0h] [rbp-28h] BYREF

v2 = a2 + rand() % (a1 - a2);
v3.tv_sec = v2 / 1000000000;
v3.tv_nsec = 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


find

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

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2)
{
while ( 1 )
{
if ( !a1 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)a1 == a2 )
break;
a1 = *(_QWORD *)(a1 + 8);
}
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, char *a2)
{
int v2; // ebp
int v3; // edx
char v4; // al
unsigned int v5; // er12
int fd2; // [rsp+Ch] [rbp-2Ch]
int pipedes[2]; // [rsp+18h] [rbp-20h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, char *a2)
{
int v2; // ebp
int v3; // edx
char v4; // al
unsigned int v5; // er12
int fd2; // [rsp+Ch] [rbp-2Ch]
int pipedes[2]; // [rsp+18h] [rbp-20h] BYREF

if ( pipe(pipedes) == -1 )
{
perror("pipe failed");
if ( fork() != -1 )
goto LABEL_10;
goto LABEL_9;
}
v3 = fork();
if ( v3 == -1 )
{
LABEL_9:
perror("fork failed");
LABEL_10:
exit(1);
}
v4 = *a2;
if ( v3 )
{
v5 = pipedes[0];
if ( v4 != 114 )
v5 = pipedes[1];
push(&fdQ, v5, v3);
signal(17, (__sighandler_t)1);
}
else
{
if ( v4 == 114 )
{
v2 = pipedes[1];
v3 = 1;
}
else if ( v4 == 119 )
{
v2 = pipedes[0];
}
else
{
fwrite("Bad mpopen arg!\n", 1uLL, 0x10uLL, stderr);
v3 = 1;
}
fd2 = v3;
printf("executing %s..\n", a1);
v5 = -1;
signal(1, (__sighandler_t)exit);
dup2(v2, fd2);
execl("/usr/bin/sh", "/usr/bin/sh", "-c", a1, 0LL);
}
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:
int __fastcall mpclose(unsigned int a1)
{
unsigned int v1; // eax
__pid_t v2; // ebp
int result; // eax

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);
result = kill(v2, 1);
if ( result == -1 )
{
perror("kill failed");
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(unsigned int a1)
{
unsigned int v1; // eax
__pid_t v2; // ebp
int result; // eax

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

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; // [rsp+0h] [rbp-28h] BYREF

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


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


original_code:
void sigsegv_handler(int sig, siginfo_t* siginfo, void* ptr __attribute__((unused)) )
{
  switch(sig) {
    case 11:
      SI = *siginfo;
      sig_error = 1;
      atexit(write_siginfo);
      exit(0);
      break;
    default:
      sig_error = -1;
  }
}

canonical_original_code:
void sigsegv_handler(int sig, struct siginfo_t *siginfo, void *ptr)
{
  switch(sig) {
    case 11:
      SI = *siginfo;
      sig_error = 1;
      atexit(write_siginfo);
      exit(0);
      break;
    default:
      sig_error = -1;
  }
}

variable_types: sig int

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

variable_types:
ptr
void *

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


fatal

decompiled_code:
void __fastcall __noreturn fatal(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func11(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void fatal(const char* msg )
{
  perror(msg);
  exit(1);
}

canonical_original_code:
void fatal(char *msg)
{
  perror(msg);
  exit(1);
}

variable_types:
msg
char *


decompiled_code:
void *init_mem()
{
void *result; // rax

memsize = 4 * sysconf(30);
result = mmap(0LL, memsize, 3, 34, -1, 0LL);
mem = result;
if ( !result )
fatal("mmap");
return result;
}

canonical_decompiled_code:
void *func12()
{
void *result; // rax

memsize = 4 * sysconf(30);
result = mmap(0LL, memsize, 3, 34, -1, 0LL);
mem = result;
if ( !result )
func11("mmap");
return result;
}

original_code:
void init_mem()
{
  int pagesize = sysconf( _SC_PAGESIZE);
  memsize = 4* pagesize;
  mem = mmap(((void *)0), memsize, 0x1 | 0x2, 0x02 | 0x20, -1, 0);
  if(mem == ((void *)0)) {
    fatal("mmap");
  }
}

variable_types: pagesize int


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

for ( result = 0LL; !sig_error; *((_BYTE *)mem + result++) = a1 )
{
if ( memsize <= (int)result )
break;
}
return result;
}

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

for ( result = 0LL; !sig_error; *((_BYTE *)mem + result++) = a1 )
{
if ( memsize <= (int)result )
break;
}
return result;
}

original_code:
void mem_set(char a)
{
  for (int i = 0; sig_error == 0 && i < memsize; i++) {
    ((char*)mem)[i] = a;
  }
}

canonical_original_code:
void mem_set(char a)
{
  for (int i = 0; sig_error == 0 && i < memsize; i++) {
    ((char *)mem)[i] = a;
  }
}

variable_types: a char

variable_types: i int


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

result = mprotect(mem, memsize, 1);
if ( result == -1 )
fatal("mprotect");
return result;
}

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

result = mprotect(mem, memsize, 1);
if ( result == -1 )
func11("mprotect");
return result;
}

original_code:
void mem_set_protread()
{
  if(mprotect(mem, memsize, 0x1) == -1) {
    fatal("mprotect");
  }
}


decompiled_code:
void simple_sigsegv_handler()
{
sig_error = -1;
}

canonical_decompiled_code:
void func15()
{
sig_error = -1;
}

original_code:
void simple_sigsegv_handler(int sig __attribute__((unused)) )
{
  sig_error = -1;
}

canonical_original_code:
void simple_sigsegv_handler(int sig)
{
  sig_error = -1;
}


decompiled_code:
int set_sigsegvhandler()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigsegv_handler;
memset(&v1.sa_mask, 0, 0x90uLL);
v1.sa_flags = 4;
result = sigaction(11, &v1, 0LL);
if ( result == -1 )
fatal("sigaction");
return result;
}

canonical_decompiled_code:
int func16()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)sigsegv_handler;
memset(&v1.sa_mask, 0, 0x90uLL);
v1.sa_flags = 4;
result = sigaction(11, &v1, 0LL);
if ( result == -1 )
func11("sigaction");
return result;
}

original_code:
void set_sigsegvhandler()
{
  struct sigaction act;
  memset(&act, 0, sizeof(struct sigaction));
  act.sa_flags = 4 ;
  act.__sigaction_handler.sa_sigaction = sigsegv_handler;
  if(sigaction(11, &act, ((void *)0)) == -1) {
    fatal("sigaction");
  }
}

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

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


decompiled_code:
void dirnotify_cleanup(void)
{
;
}

canonical_decompiled_code:
void func0(void)
{
;
}

original_code:
void dirnotify_cleanup()
{
}

canonical_original_code:
void dirnotify_cleanup()
{
}


killpid

decompiled_code:
void killpid(void)
{
sleep(4u);
kill(pid, 2);
}

canonical_decompiled_code:
void func2(void)
{
sleep(4u);
kill(pid, 2);
}

original_code:
void killpid()
{
  sleep(4);
  kill(pid, 2);
}


fatal

canonical_decompiled_code:
void __fastcall __noreturn func5(const char *a1)
{
perror(a1);
exit(1);
}


decompiled_code:
int __fastcall dirnotify_init(char *name)
{
int v1; // eax
int result; // eax

v1 = inotify_init();
notify_fd = v1;
if ( v1 == -1 )
fatal("inotify init");
result = inotify_add_watch(v1, name, 0x1000FFFu);
dir_fd = result;
if ( result == -1 )
fatal("inotify add");
return result;
}

canonical_decompiled_code:
int __fastcall func6(char *name)
{
int v1; // eax
int result; // eax

v1 = inotify_init();
notify_fd = v1;
if ( v1 == -1 )
func5("inotify init");
result = inotify_add_watch(v1, name, 0x1000FFFu);
dir_fd = result;
if ( result == -1 )
func5("inotify add");
return result;
}

variable_types:
dirname
char *


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

result = *(_DWORD *)(a1 + 12);
if ( result )
result = puts((const char *)(a1 + 16));
return result;
}

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

result = *(_DWORD *)(a1 + 12);
if ( result )
result = puts((const char *)(a1 + 16));
return result;
}

original_code:
void dirnotify_printname(const struct inotify_event* event)
{
  if(event->len > 0)
  {
    puts(event->name);
  }
}

canonical_original_code:
void dirnotify_printname(struct inotify_event *event)
{
  if(event->len > 0)
  {
    puts(event->name);
  }
}

variable_types: struct inotify_event { int wd; unsigned int mask; unsigned int cookie; unsigned int len; char name[]; } *


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

result = *(_DWORD *)(a1 + 4);
if ( result > 0x20 )
{
if ( result == 256 )
{
puts("Received: IN_CREATE");
result = dirnotify_printname(a1);
}
else if ( result <= 0x100 )
{
if ( result == 64 )
{
puts("Received: IN_MOVED_FROM");
dirnotify_printname(a1);
result = to_cookie;
if ( to_cookie > 0 && *(_DWORD *)(a1 + 8) == to_cookie )
{
result = printf(" ...renamed from %s\n", &to_name);
from_cookie = -1;
}
else
{
from_cookie = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) )
result = (unsigned int)strcpy(&from_name, (const char *)(a1 + 16));
else
from_name = 0;
}
}
else if ( result == 128 )
{
puts("Received: IN_MOVED_TO");
dirnotify_printname(a1);
if ( from_cookie > 0 && *(_DWORD *)(a1 + 8) == from_cookie )
{
result = printf(" ...renamed to %s\n", &from_name);
to_cookie = -1;
}
else
{
result = *(_DWORD *)(a1 + 12);
to_cookie = *(_DWORD *)(a1 + 8);
if ( result )
result = (unsigned int)strcpy(&to_name, (const char *)(a1 + 16));
else
to_name = 0;
}
}
}
else
{
switch ( result )
{
case 0x400u:
puts("Received: IN_DELETE_SELF");
dirnotify_printname(a1);
fatal("Directory deleted, exiting");
case 0x800u:
puts("Received: IN_MOVE_SELF");
result = dirnotify_printname(a1);
break;
case 0x200u:
puts("Received: IN_DELETE");
result = dirnotify_printname(a1);
break;
}
}
}
else if ( result )
{
switch ( result )
{
case 1u:
puts("Received: IN_ACCESS");
result = dirnotify_printname(a1);
break;
case 2u:
puts("Received: IN_MODIFY");
result = dirnotify_printname(a1);
break;
case 4u:
puts("Received: IN_ATTRIB");
result = dirnotify_printname(a1);
break;
case 8u:
puts("Received: IN_CLOSE_WRITE");
result = dirnotify_printname(a1);
break;
case 0x10u:
puts("Received: IN_CLOSE_NOWRITE");
result = dirnotify_printname(a1);
break;
case 0x20u:
puts("Received: IN_OPEN");
result = dirnotify_printname(a1);
break;
default:
return result;
}
}
return result;
}

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

result = *(_DWORD *)(a1 + 4);
if ( result > 0x20 )
{
if ( result == 256 )
{
puts("Received: IN_CREATE");
result = func7(a1);
}
else if ( result <= 0x100 )
{
if ( result == 64 )
{
puts("Received: IN_MOVED_FROM");
func7(a1);
result = to_cookie;
if ( to_cookie > 0 && *(_DWORD *)(a1 + 8) == to_cookie )
{
result = printf(" ...renamed from %s\n", &to_name);
from_cookie = -1;
}
else
{
from_cookie = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(a1 + 12) )
result = (unsigned int)strcpy(&from_name, (const char *)(a1 + 16));
else
from_name = 0;
}
}
else if ( result == 128 )
{
puts("Received: IN_MOVED_TO");
func7(a1);
if ( from_cookie > 0 && *(_DWORD *)(a1 + 8) == from_cookie )
{
result = printf(" ...renamed to %s\n", &from_name);
to_cookie = -1;
}
else
{
result = *(_DWORD *)(a1 + 12);
to_cookie = *(_DWORD *)(a1 + 8);
if ( result )
result = (unsigned int)strcpy(&to_name, (const char *)(a1 + 16));
else
to_name = 0;
}
}
}
else
{
switch ( result )
{
case 0x400u:
puts("Received: IN_DELETE_SELF");
func7(a1);
func5("Directory deleted, exiting");
case 0x800u:
puts("Received: IN_MOVE_SELF");
result = func7(a1);
break;
case 0x200u:
puts("Received: IN_DELETE");
result = func7(a1);
break;
}
}
}
else if ( result )
{
switch ( result )
{
case 1u:
puts("Received: IN_ACCESS");
result = func7(a1);
break;
case 2u:
puts("Received: IN_MODIFY");
result = func7(a1);
break;
case 4u:
puts("Received: IN_ATTRIB");
result = func7(a1);
break;
case 8u:
puts("Received: IN_CLOSE_WRITE");
result = func7(a1);
break;
case 0x10u:
puts("Received: IN_CLOSE_NOWRITE");
result = func7(a1);
break;
case 0x20u:
puts("Received: IN_OPEN");
result = func7(a1);
break;
default:
return result;
}
}
return result;
}

original_code:
void dirnotify_process(const struct inotify_event* event)
{
  switch(event->mask) {
    case 0x00000001: puts("Received: " "IN_ACCESS" "");dirnotify_printname(event);
      break;
    case 0x00000004: puts("Received: " "IN_ATTRIB" "");dirnotify_printname(event);
      break;
    case 0x00000008: puts("Received: " "IN_CLOSE_WRITE" "");dirnotify_printname(event);
      break;
    case 0x00000010: puts("Received: " "IN_CLOSE_NOWRITE" "");dirnotify_printname(event);
      break;
    case 0x00000100: puts("Received: " "IN_CREATE" "");dirnotify_printname(event);
      break;
    case 0x00000200: puts("Received: " "IN_DELETE" "");dirnotify_printname(event);
      break;
    case 0x00000400: puts("Received: " "IN_DELETE_SELF" "");dirnotify_printname(event);
      fatal("Directory deleted, exiting");
      break;
    case 0x00000002: puts("Received: " "IN_MODIFY" "");dirnotify_printname(event);
      break;
    case 0x00000800: puts("Received: " "IN_MOVE_SELF" "");dirnotify_printname(event);
      break;
    case 0x00000040: puts("Received: " "IN_MOVED_FROM" "");dirnotify_printname(event);
      if(to_cookie > 0 && event->cookie == (unsigned)to_cookie)
      {
        printf(" ...renamed from %s\n", to_name);
        from_cookie = -1;
      } else {
        from_cookie = event->cookie;
        if(event->len > 0) {
          strcpy(from_name, event->name);
        } else {
          from_name[0] = '\0';
        }
      }
      break;
    case 0x00000080: puts("Received: " "IN_MOVED_TO" "");dirnotify_printname(event);
      if(from_cookie > 0 && event->cookie == (unsigned)from_cookie)
      {
        printf(" ...renamed to %s\n", from_name);
        to_cookie = -1;
      } else {
        to_cookie = event->cookie;
        if(event->len > 0) {
          strcpy(to_name, event->name);
        } else {
          to_name[0] = '\0';
        }
      }
      break;
    case 0x00000020: puts("Received: " "IN_OPEN" "");dirnotify_printname(event);
      break;
  }
}

canonical_original_code:
void dirnotify_process(struct inotify_event *event)
{
  switch(event->mask) {
    case 0x00000001: puts("Received: " "IN_ACCESS" "");dirnotify_printname(event);
      break;
    case 0x00000004: puts("Received: " "IN_ATTRIB" "");dirnotify_printname(event);
      break;
    case 0x00000008: puts("Received: " "IN_CLOSE_WRITE" "");dirnotify_printname(event);
      break;
    case 0x00000010: puts("Received: " "IN_CLOSE_NOWRITE" "");dirnotify_printname(event);
      break;
    case 0x00000100: puts("Received: " "IN_CREATE" "");dirnotify_printname(event);
      break;
    case 0x00000200: puts("Received: " "IN_DELETE" "");dirnotify_printname(event);
      break;
    case 0x00000400: puts("Received: " "IN_DELETE_SELF" "");dirnotify_printname(event);
      fatal("Directory deleted, exiting");
      break;
    case 0x00000002: puts("Received: " "IN_MODIFY" "");dirnotify_printname(event);
      break;
    case 0x00000800: puts("Received: " "IN_MOVE_SELF" "");dirnotify_printname(event);
      break;
    case 0x00000040: puts("Received: " "IN_MOVED_FROM" "");dirnotify_printname(event);
      if(to_cookie > 0 && event->cookie == (unsigned int)to_cookie)
      {
        printf(" ...renamed from %s\n", to_name);
        from_cookie = -1;
      } else {
        from_cookie = event->cookie;
        if(event->len > 0) {
          strcpy(from_name, event->name);
        } else {
          from_name[0] = '\0';
        }
      }
      break;
    case 0x00000080: puts("Received: " "IN_MOVED_TO" "");dirnotify_printname(event);
      if(from_cookie > 0 && event->cookie == (unsigned int)from_cookie)
      {
        printf(" ...renamed to %s\n", from_name);
        to_cookie = -1;
      } else {
        to_cookie = event->cookie;
        if(event->len > 0) {
          strcpy(to_name, event->name);
        } else {
          to_name[0] = '\0';
        }
      }
      break;
    case 0x00000020: puts("Received: " "IN_OPEN" "");dirnotify_printname(event);
      break;
  }
}

user_defined_types: struct inotify_event { int wd; unsigned int mask; unsigned int cookie; unsigned int len; char name[]; }


decompiled_code:
void __noreturn dirnotify_watch()
{
int v0; // eax
int v1; // er14
int v2; // er12
int v3; // ebx
_BYTE v4[304]; // [rsp+0h] [rbp-130h] BYREF

while ( 1 )
{
v0 = read(notify_fd, v4, 0x110uLL);
v1 = v0;
if ( v0 == -1 )
break;
v2 = 0;
if ( v0 > 0 )
{
do
{
dirnotify_process((__int64)&v4[v2]);
v3 = *(_DWORD *)&v4[v2 + 12];
info("next..");
v2 += v3 + 16;
}
while ( v1 > v2 );
}
info("--");
fflush(stdout);
}
fatal("read");
}

original_code:
void dirnotify_watch()
{
  int maxsize = sizeof(struct inotify_event) + 255 + 1;
  char buf[maxsize];
  int size;
  while( 1 > 0) {
    if((size = read(notify_fd, buf, maxsize)) == -1)
      fatal("read");
    for(int i = 0; i < size; i = i + sizeof(struct inotify_event)){
      struct inotify_event* this = (struct inotify_event*)(buf + i);
      dirnotify_process(this);
      i = i + this -> len;
      info("next..");
    }
    info("--");
    fflush(stdout);
  }
}

canonical_original_code:
void dirnotify_watch()
{
  int maxsize = sizeof(struct inotify_event) + 255 + 1;
  char buf[-1];
  int size;
  while( 1 > 0) {
    if((size = read(notify_fd, buf, maxsize)) == -1)
      fatal("read");
    for(int i = 0; i < size; i = i + sizeof(struct inotify_event)){
      struct inotify_event *this = (struct inotify_event*)(buf + i);
      dirnotify_process(this);
      i = i + this -> len;
      info("next..");
    }
    info("--");
    fflush(stdout);
  }
}

variable_types: maxsize int

variable_types:
buf
char [-1]

variable_types: size int


decompiled_code:
int set_siginth()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = sigint_handler;
memset(&v1.sa_mask, 0, 0x90uLL);
result = sigaction(2, &v1, 0LL);
if ( result )
fatal("sigaction");
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = sigint_handler;
memset(&v1.sa_mask, 0, 0x90uLL);
result = sigaction(2, &v1, 0LL);
if ( result )
func5("sigaction");
return result;
}

original_code:
void set_siginth()
{
  struct sigaction act;
  memset(&act, 0, sizeof(struct sigaction));
  act.__sigaction_handler.sa_handler = sigint_handler;
  if(sigaction(2, &act, ((void *)0)) != 0) {
    fatal("sigaction");
  }
}

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }

user_defined_types: union sigval { int sival_int; void *sival_ptr; }


watch

decompiled_code:
void __fastcall __noreturn watch(char *format)
{
dirnotify_init(format);
if ( atexit(dirnotify_cleanup) )
fatal("atexit");
info("Watching dir:");
info(format);
dirnotify_watch();
}

canonical_decompiled_code:
void __fastcall __noreturn func11(char *format)
{
func6(format);
if ( atexit(dirnotify_cleanup) )
func5("atexit");
func4("Watching dir:");
func4(format);
func9();
}

original_code:
int watch(const char* name)
{
  dirnotify_init(name);
  if(atexit(dirnotify_cleanup) != 0) {
    fatal("atexit");
  }
  info("Watching dir:");
  info(name);
  dirnotify_watch();
  exit(0);
}

canonical_original_code:
int watch(char *name)
{
  dirnotify_init(name);
  if(atexit(dirnotify_cleanup) != 0) {
    fatal("atexit");
  }
  info("Watching dir:");
  info(name);
  dirnotify_watch();
  exit(0);
}

variable_types:
name
char *


decompiled_code:
int __fastcall open_read(const char *a1, int *a2)
{
int result; // eax

if ( *a2 != -1 )
_assert_fail("(*fd) == -1", "7.c", 0xA1u, "open_read");
result = open(a1, 0);
*a2 = result;
if ( result < 0 )
fatal("openr");
return result;
}

canonical_decompiled_code:
int __fastcall func13(const char *a1, int *a2)
{
int result; // eax

if ( *a2 != -1 )
_assert_fail("(*fd) == -1", "7.c", 0xA1u, "open_read");
result = open(a1, 0);
*a2 = result;
if ( result < 0 )
func5("openr");
return result;
}

original_code:
void open_read(const char* fullname, int* fd)
{
  ((void) sizeof (((*fd) == -1) ? 1 : 0), __extension__ ({ if ((*fd) == -1) ; else __assert_fail ("(*fd) == -1", "7.c", 161, __extension__ __PRETTY_FUNCTION__); }));
  if(((*fd) = open(fullname, 00)) < 0) {
    fatal("openr");
  }
}

canonical_original_code:
void open_read(char *fullname, int *fd)
{
  ((void) sizeof (((*fd) == -1) ? 1 : 0), __extension__ ({ if ((*fd) == -1) ; else __assert_fail ("(*fd) == -1", "7.c", 161, __extension__ __PRETTY_FUNCTION__); }));
  if(((*fd) = open(fullname, 00)) < 0) {
    fatal("openr");
  }
}

variable_types:
fullname
char *

variable_types:
fd
int *


decompiled_code:
int __fastcall open_write(const char *a1, int *a2)
{
int result; // eax

if ( *a2 != -1 )
_assert_fail("(*fd) == -1", "7.c", 0xA9u, "open_write");
result = open(a1, 2);
*a2 = result;
if ( result < 0 )
fatal("openw");
return result;
}

canonical_decompiled_code:
int __fastcall func14(const char *a1, int *a2)
{
int result; // eax

if ( *a2 != -1 )
_assert_fail("(*fd) == -1", "7.c", 0xA9u, "open_write");
result = open(a1, 2);
*a2 = result;
if ( result < 0 )
func5("openw");
return result;
}

original_code:
void open_write(const char* fullname, int* fd)
{
  ((void) sizeof (((*fd) == -1) ? 1 : 0), __extension__ ({ if ((*fd) == -1) ; else __assert_fail ("(*fd) == -1", "7.c", 169, __extension__ __PRETTY_FUNCTION__); }));
  if(((*fd) = open(fullname, 02)) < 0) {
    fatal("openw");
  }
}

canonical_original_code:
void open_write(char *fullname, int *fd)
{
  ((void) sizeof (((*fd) == -1) ? 1 : 0), __extension__ ({ if ((*fd) == -1) ; else __assert_fail ("(*fd) == -1", "7.c", 169, __extension__ __PRETTY_FUNCTION__); }));
  if(((*fd) = open(fullname, 02)) < 0) {
    fatal("openw");
  }
}


decompiled_code:
void __fastcall create_new_file(const char *a1)
{
int v1; // ebx
int fd[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( creat(a1, 0x1C0u) < 0 && *_errno_location() != 17 )
fatal("creat");
v1 = 4;
fd[0] = -1;
open_write(a1, fd);
do
{
write(fd[0], "tt0t0t0t0tnananananananananananananananananananananan", 0xAuLL);
--v1;
}
while ( v1 );
close_descriptor(fd);
}

canonical_decompiled_code:
void __fastcall func15(const char *a1)
{
int v1; // ebx
int fd[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( creat(a1, 0x1C0u) < 0 && *_errno_location() != 17 )
func5("creat");
v1 = 4;
fd[0] = -1;
func14(a1, fd);
do
{
write(fd[0], "tt0t0t0t0tnananananananananananananananananananananan", 0xAuLL);
--v1;
}
while ( v1 );
func12(fd);
}

original_code:
void create_new_file(const char* name)
{
  if(creat(name, (0100 | 0400 | 0200)) < 0 && (*__errno_location ()) != 17) {
    fatal("creat");
  }
  int ind = -1;
  open_write(name, &ind);
  for (int i = 0; i < 4; i++) {
    write(ind, "tt0t0t0t0tnananananananananananananananananananananan", 10);
  }
  close_descriptor(&ind);
}

canonical_original_code:
void create_new_file(char *name)
{
  if(creat(name, (0100 | 0400 | 0200)) < 0 && (*__errno_location ()) != 17) {
    fatal("creat");
  }
  int ind = -1;
  open_write(name, &ind);
  for (int i = 0; i < 4; i++) {
    write(ind, "tt0t0t0t0tnananananananananananananananananananananan", 10);
  }
  close_descriptor(&ind);
}

variable_types: ind int


decompiled_code:
void __fastcall forge_new_file(char *dest, char *src, int *a3)
{
unsigned int v3; // er13
int v4; // eax
char *v5; // rax
char v6[280]; // [rsp+0h] [rbp-118h] BYREF

v3 = *a3;
close_descriptor(a3);
v4 = rand();
sprintf(v6, "lala77%d5aa%ld", v3, v4);
v5 = stpcpy(dest, src);
strcpy(v5, v6);
create_new_file(dest);
}

canonical_decompiled_code:
void __fastcall func16(char *dest, char *src, int *a3)
{
unsigned int v3; // er13
int v4; // eax
char *v5; // rax
char v6[280]; // [rsp+0h] [rbp-118h] BYREF

v3 = *a3;
func12(a3);
v4 = rand();
sprintf(v6, "lala77%d5aa%ld", v3, v4);
v5 = stpcpy(dest, src);
strcpy(v5, v6);
func15(dest);
}

original_code:
void forge_new_file(char* name, const char* prefix, int* fd)
{
  int oldfd = *fd;
  close_descriptor(fd);
  char buf[255];
  sprintf(buf, "lala77%d5aa%ld", oldfd, (size_t) rand());
  strcpy(name, prefix);
  strcat(name, buf);
  create_new_file(name);
}

canonical_original_code:
void forge_new_file(char *name, char *prefix, int *fd)
{
  int oldfd = *fd;
  close_descriptor(fd);
  char buf[255];
  sprintf(buf, "lala77%d5aa%ld", oldfd, (unsigned long) rand());
  strcpy(name, prefix);
  strcat(name, buf);
  create_new_file(name);
}

variable_types:
prefix
char *

variable_types: oldfd int

variable_types:
buf
char [255]


decompiled_code:
int hackNCreate_cleanup()
{
if ( ind != -1 )
close_descriptor(&ind);
unlink(fullname);
return unlink("totally_not_an_old_dir");
}

canonical_decompiled_code:
int func17()
{
if ( ind != -1 )
func12(&ind);
unlink(fullname);
return unlink("totally_not_an_old_dir");
}

original_code:
void hackNCreate_cleanup()
{
  if(ind != -1) {
    close_descriptor(&ind);
  }
  unlink(fullname);
  unlink("totally_not_an_old_dir");
}


hackNCreate

decompiled_code:
void __fastcall hackNCreate(char *path)
{
const char *v2; // rdi
struct timeb v3[2]; // [rsp+0h] [rbp-1E8h] BYREF
char buf[32]; // [rsp+20h] [rbp-1C8h] BYREF
char v5[144]; // [rsp+40h] [rbp-1A8h] BYREF
char dest[280]; // [rsp+D0h] [rbp-118h] BYREF

ftime(v3);
srand(LODWORD(v3[0].time) + v3[0].millitm);
*(_WORD *)stpcpy(dest, path) = 47;
forge_new_file(fullname, dest, &ind);
v3[1] = 0LL;
memset(v5, 0, sizeof(v5));
while ( 1 )
{
switch ( rand() % 12 )
{
case 0:
goto LABEL_8;
case 1:
puts("-------------testing attrib..");
if ( chmod(fullname, 0x1C0u) )
fatal("chmod1");
if ( chmod(fullname, 0x1F0u) )
fatal("chmod2");
break;
case 3:
puts("-------------testing close_nowrite..");
LABEL_8:
puts("-------------testing access..");
close_descriptor(&ind);
open_read(fullname, &ind);
if ( read(ind, buf, 0x14uLL) < 0 )
fatal("read");
goto LABEL_5;
case 4:
goto LABEL_4;
case 5:
v2 = "-------------testing create..";
goto LABEL_22;
case 6:
puts("-------------testing modify..");
LABEL_4:
puts("-------------testing close_write..");
close_descriptor(&ind);
open_write(fullname, &ind);
write(ind, "wriiiiiiting...", 6uLL);
LABEL_5:
close_descriptor(&ind);
break;
case 7:
v2 = "-------------testing delete..";
LABEL_22:
puts(v2);
if ( unlink(fullname) )
goto LABEL_23;
goto LABEL_12;
case 8:
puts("-------------testing delete_self..");
if ( unlink(fullname) )
goto LABEL_23;
close_descriptor(&ind);
if ( !rmdir(path) )
{
fwrite("Deleted dir, exiting..\n", 1uLL, 0x17uLL, stderr);
exit(0);
}
LABEL_20:
fatal("rmdir");
case 9:
puts("-------------testing move_self..");
close_descriptor(&ind);
unlink(fullname);
if ( !rename(path, "totally_not_an_old_dir") )
{
fwrite("Renamed old dir, exiting..\n", 1uLL, 0x1BuLL, stderr);
exit(0);
}
goto LABEL_20;
case 10:
puts("-------------testing move_to..");
create_new_file("OpSys7Thrash");
if ( unlink(fullname) )
goto LABEL_23;
close_descriptor(&ind);
if ( rename("OpSys7Thrash", fullname) )
LABEL_15:
fatal("rename");
return;
case 11:
puts("-------------testing move_from..");
if ( rename(fullname, "OpSys7Thrash") )
goto LABEL_15;
if ( unlink("OpSys7Thrash") )
LABEL_23:
fatal("unlink");
LABEL_12:
forge_new_file(fullname, dest, &ind);
break;
default:
break;
}
sleep(2u);
}
}

canonical_decompiled_code:
void __fastcall func18(char *path)
{
const char *v2; // rdi
struct timeb v3[2]; // [rsp+0h] [rbp-1E8h] BYREF
char buf[32]; // [rsp+20h] [rbp-1C8h] BYREF
char v5[144]; // [rsp+40h] [rbp-1A8h] BYREF
char dest[280]; // [rsp+D0h] [rbp-118h] BYREF

ftime(v3);
srand(LODWORD(v3[0].time) + v3[0].millitm);
*(_WORD *)stpcpy(dest, path) = 47;
func16(fullname, dest, &ind);
v3[1] = 0LL;
memset(v5, 0, sizeof(v5));
while ( 1 )
{
switch ( rand() % 12 )
{
case 0:
goto LABEL_8;
case 1:
puts("-------------testing attrib..");
if ( chmod(fullname, 0x1C0u) )
func5("chmod1");
if ( chmod(fullname, 0x1F0u) )
func5("chmod2");
break;
case 3:
puts("-------------testing close_nowrite..");
LABEL_8:
puts("-------------testing access..");
func12(&ind);
func13(fullname, &ind);
if ( read(ind, buf, 0x14uLL) < 0 )
func5("read");
goto LABEL_5;
case 4:
goto LABEL_4;
case 5:
v2 = "-------------testing create..";
goto LABEL_22;
case 6:
puts("-------------testing modify..");
LABEL_4:
puts("-------------testing close_write..");
func12(&ind);
func14(fullname, &ind);
write(ind, "wriiiiiiting...", 6uLL);
LABEL_5:
func12(&ind);
break;
case 7:
v2 = "-------------testing delete..";
LABEL_22:
puts(v2);
if ( unlink(fullname) )
goto LABEL_23;
goto LABEL_12;
case 8:
puts("-------------testing delete_self..");
if ( unlink(fullname) )
goto LABEL_23;
func12(&ind);
if ( !rmdir(path) )
{
fwrite("Deleted dir, exiting..\n", 1uLL, 0x17uLL, stderr);
exit(0);
}
LABEL_20:
func5("rmdir");
case 9:
puts("-------------testing move_self..");
func12(&ind);
unlink(fullname);
if ( !rename(path, "totally_not_an_old_dir") )
{
fwrite("Renamed old dir, exiting..\n", 1uLL, 0x1BuLL, stderr);
exit(0);
}
goto LABEL_20;
case 10:
puts("-------------testing move_to..");
func15("OpSys7Thrash");
if ( unlink(fullname) )
goto LABEL_23;
func12(&ind);
if ( rename("OpSys7Thrash", fullname) )
LABEL_15:
func5("rename");
return;
case 11:
puts("-------------testing move_from..");
if ( rename(fullname, "OpSys7Thrash") )
goto LABEL_15;
if ( unlink("OpSys7Thrash") )
LABEL_23:
func5("unlink");
LABEL_12:
func16(fullname, dest, &ind);
break;
default:
break;
}
sleep(2u);
}
}

original_code:
void hackNCreate(const char* name)
{
  struct timeb t;
  ftime(&t);
  srand( t.time + t.millitm );
  char prefix[255 + 2];
  strcpy(prefix, name);
  strcat(prefix, "/");
  forge_new_file(fullname, prefix, &ind);
  struct utimbuf ut;
  memset(&ut, 0, sizeof(ut));
  struct stat buf;
  memset(&buf, 0, sizeof(buf));
  while( 1 > 0) {
    switch(rand() % 12) {
      case 3:
        printf("-------------testing close_nowrite..\n");
      case 0:
        printf("-------------testing access..\n");
        close_descriptor(&ind);
        open_read(fullname, &ind);
        char b[20];
        if(read(ind, (void*)b, sizeof(b)) < 0) {
          fatal("read");
        }
        close_descriptor(&ind);
        break;
      case 1:
        printf("-------------testing attrib..\n");
        if(chmod(fullname, (0100 | 0400 | 0200)) != 0) {
          fatal("chmod1");
        }
        if(chmod(fullname, ((0100 | 0400 | 0200) | (0200 >> 3) | (0400 >> 3))) != 0) {
          fatal("chmod2");
        }
        break;
      case 6:
        printf("-------------testing modify..\n");
      case 4:
        printf("-------------testing close_write..\n");
        close_descriptor(&ind);
        open_write(fullname, &ind);
        write(ind, "wriiiiiiting...", 6);
        close_descriptor(&ind);
        break;
      case 5:
        printf("-------------testing create..\n");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        forge_new_file(fullname, prefix, &ind);
        break;
      case 7:
        printf("-------------testing delete..\n");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        forge_new_file(fullname, prefix, &ind);
        break;
      case 10:
        printf("-------------testing move_to..\n");
        create_new_file("OpSys7Thrash");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        close_descriptor(&ind);
        if(rename("OpSys7Thrash", fullname) != 0) {
          fatal("rename");
        }
        break;
      case 11:
        printf("-------------testing move_from..\n");
        if(rename(fullname, "OpSys7Thrash") != 0) {
          fatal("rename");
        }
        if(unlink("OpSys7Thrash") != 0) {
          fatal("unlink");
        }
        forge_new_file(fullname, prefix, &ind);
        break;
      case 8:
        printf("-------------testing delete_self..\n");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        close_descriptor(&ind);
        if(rmdir(name) != 0) {
          fatal("rmdir");
        } else {
          fprintf(stderr, "Deleted dir, exiting..\n");
          exit(0);
        }
        break;
      case 9:
        printf("-------------testing move_self..\n");
        close_descriptor(&ind);
        unlink(fullname);
        if(rename(name, "totally_not_an_old_dir") != 0) {
          fatal("rmdir");
        } else {
          fprintf(stderr, "Renamed old dir, exiting..\n");
          exit(0);
        }
        break;
    }
    sleep(2);
  }
}

canonical_original_code:
void hackNCreate(char *name)
{
  struct timeb t;
  ftime(&t);
  srand( t.time + t.millitm );
  char prefix[-1];
  strcpy(prefix, name);
  strcat(prefix, "/");
  forge_new_file(fullname, prefix, &ind);
  struct utimbuf ut;
  memset(&ut, 0, sizeof(ut));
  struct stat buf;
  memset(&buf, 0, sizeof(buf));
  while( 1 > 0) {
    switch(rand() % 12) {
      case 3:
        printf("-------------testing close_nowrite..\n");
      case 0:
        printf("-------------testing access..\n");
        close_descriptor(&ind);
        open_read(fullname, &ind);
        char b[20];
        if(read(ind, (void *)b, sizeof(b)) < 0) {
          fatal("read");
        }
        close_descriptor(&ind);
        break;
      case 1:
        printf("-------------testing attrib..\n");
        if(chmod(fullname, (0100 | 0400 | 0200)) != 0) {
          fatal("chmod1");
        }
        if(chmod(fullname, ((0100 | 0400 | 0200) | (0200 >> 3) | (0400 >> 3))) != 0) {
          fatal("chmod2");
        }
        break;
      case 6:
        printf("-------------testing modify..\n");
      case 4:
        printf("-------------testing close_write..\n");
        close_descriptor(&ind);
        open_write(fullname, &ind);
        write(ind, "wriiiiiiting...", 6);
        close_descriptor(&ind);
        break;
      case 5:
        printf("-------------testing create..\n");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        forge_new_file(fullname, prefix, &ind);
        break;
      case 7:
        printf("-------------testing delete..\n");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        forge_new_file(fullname, prefix, &ind);
        break;
      case 10:
        printf("-------------testing move_to..\n");
        create_new_file("OpSys7Thrash");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        close_descriptor(&ind);
        if(rename("OpSys7Thrash", fullname) != 0) {
          fatal("rename");
        }
        break;
      case 11:
        printf("-------------testing move_from..\n");
        if(rename(fullname, "OpSys7Thrash") != 0) {
          fatal("rename");
        }
        if(unlink("OpSys7Thrash") != 0) {
          fatal("unlink");
        }
        forge_new_file(fullname, prefix, &ind);
        break;
      case 8:
        printf("-------------testing delete_self..\n");
        if(unlink(fullname) != 0) {
          fatal("unlink");
        }
        close_descriptor(&ind);
        if(rmdir(name) != 0) {
          fatal("rmdir");
        } else {
          fprintf(stderr, "Deleted dir, exiting..\n");
          exit(0);
        }
        break;
      case 9:
        printf("-------------testing move_self..\n");
        close_descriptor(&ind);
        unlink(fullname);
        if(rename(name, "totally_not_an_old_dir") != 0) {
          fatal("rmdir");
        } else {
          fprintf(stderr, "Renamed old dir, exiting..\n");
          exit(0);
        }
        break;
    }
    sleep(2);
  }
}

variable_types: struct timeb { long time; unsigned short millitm; short timezone; short dstflag; }

variable_types:
prefix
char [-1]

variable_types: struct utimbuf { long actime; long modtime; }

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
b
char [20]

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


fatal

decompiled_code:
void __fastcall __noreturn fatal(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void fatal(const char* msg )
{
  perror(msg);
  exit(1);
}

variable_types:
msg
char *


sperm

decompiled_code:
void *sperm()
{
strmode();
return &local_buff_0;
}

canonical_decompiled_code:
void *func10()
{
strmode();
return &local_buff_0;
}

original_code:
char const * sperm(__mode_t mode)
{
  static char local_buff[20] = {0};
  strmode(mode, local_buff);
  return local_buff;
}

canonical_original_code:
char *sperm(unsigned int mode)
{
  static char local_buff[20] = {0};
  strmode(mode, local_buff);
  return local_buff;
}

variable_types:
mode
unsigned int

return_type: char *