decompiled_code:
void __noreturn child_create()
{
unsigned int v0; // eax
unsigned int v1; // ebx
unsigned int v2; // eax
int v3; // eax
int v4; // eax

v0 = getpid();
printf("[PID %d ] I am Child\n", v0);
while ( 1 )
{
while ( 1 )
{
v3 = rand();
sleep(v3 % 5);
sem_wait(sem_bird);
sem_wait(sem);
v4 = *(_DWORD *)shm_addr;
if ( *(int *)shm_addr <= 0 )
break;
worms = *(_DWORD *)shm_addr;
*(_DWORD *)shm_addr = v4 - 1;
v1 = *(_DWORD *)shm_addr;
v2 = getpid();
printf("Worms on plate:%d ... I Bird %d. I'm hangry ... Remain %d Worms\n", (unsigned int)worms, v2, v1);
sem_post(sem);
sem_post(sem_bird);
}
if ( v4 )
{
perror("Child");
exit(1);
}
puts("EHI DAD !!! The plate in Empty... We are hangry yet!!! ");
sem_post(sem);
sem_post(sem_parent);
}
}

canonical_decompiled_code:
void __noreturn func10()
{
unsigned int v0; // eax
unsigned int v1; // ebx
unsigned int v2; // eax
int v3; // eax
int v4; // eax

v0 = getpid();
printf("[PID %d ] I am Child\n", v0);
while ( 1 )
{
while ( 1 )
{
v3 = rand();
sleep(v3 % 5);
sem_wait(sem_bird);
sem_wait(sem);
v4 = *(_DWORD *)shm_addr;
if ( *(int *)shm_addr <= 0 )
break;
worms = *(_DWORD *)shm_addr;
*(_DWORD *)shm_addr = v4 - 1;
v1 = *(_DWORD *)shm_addr;
v2 = getpid();
printf("Worms on plate:%d ... I Bird %d. I'm hangry ... Remain %d Worms\n", (unsigned int)worms, v2, v1);
sem_post(sem);
sem_post(sem_bird);
}
if ( v4 )
{
perror("Child");
exit(1);
}
puts("EHI DAD !!! The plate in Empty... We are hangry yet!!! ");
sem_post(sem);
sem_post(sem_parent);
}
}

original_code:
void
child_create (void)
{
    printf("[PID %d ] I am Child\n", getpid());
    while(1) {
        sleep(rand()%5);
        sem_wait(sem_bird);
        sem_wait(sem);
        if(*shm_addr > 0) {
            worms = *shm_addr;
            *shm_addr = *shm_addr - 1;
            printf("Worms on plate:%d ... I Bird %d. I'm hangry ... "
                   "Remain %d Worms\n",worms, getpid(), *shm_addr);
            sem_post(sem);
            sem_post(sem_bird);
        } else if(*shm_addr == 0){
            printf("EHI DAD !!! The plate in Empty... We are hangry yet!!! \n");
            sem_post(sem);
            sem_post(sem_parent);
        } else {
            perror("Child");
            exit(1) ;
        }
    }
}

canonical_original_code:
void child_create()
{
    printf("[PID %d ] I am Child\n", getpid());
    while(1) {
        sleep(rand()%5);
        sem_wait(sem_bird);
        sem_wait(sem);
        if(*shm_addr > 0) {
            worms = *shm_addr;
            *shm_addr = *shm_addr - 1;
            printf("Worms on plate:%d ... I Bird %d. I'm hangry ... "
                   "Remain %d Worms\n",worms, getpid(), *shm_addr);
            sem_post(sem);
            sem_post(sem_bird);
        } else if(*shm_addr == 0){
            printf("EHI DAD !!! The plate in Empty... We are hangry yet!!! \n");
            sem_post(sem);
            sem_post(sem_parent);
        } else {
            perror("Child");
            exit(1) ;
        }
    }
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
__pid_t v4; // eax

printf("[%d]\n", 1LL);
fflush(stdout);
shm_id = shm_open("MY_SHARED_MEMORY", 66, 0x1B6u);
if ( shm_id == -1 )
perror("shm_open: ");
if ( ftruncate(shm_id, 4LL) == -1 )
perror("ftruncate: ");
shm_addr = (__int64)mmap(0LL, 4uLL, 3, 1, shm_id, 0LL);
if ( shm_addr == -1 )
perror("mmap: ");
*(_DWORD *)shm_addr = worms;
sem = sem_open("SEM_MUTEX", 64, 2LL, 1LL);
if ( !sem )
{
perror("Error initializing semaphore");
exit(1);
}
sem_bird = sem_open("SEM_BIRD", 64, 2LL, 1LL);
if ( !sem_bird )
{
perror("Error initializing semaphore");
exit(1);
}
if ( *(_DWORD *)shm_addr )
{
if ( *(int *)shm_addr <= 0 )
{
perror("Error initializing PARENT semaphore: BAD value Shared Memory");
exit(1);
}
sem_parent = sem_open("SEM_PARENT", 64, 2LL, 0LL);
if ( !sem_parent )
{
perror("Error initializing PARENT semaphore");
exit(1);
}
}
else
{
sem_parent = sem_open("SEM_PARENT", 64, 2LL, 1LL);
if ( !sem_parent )
{
perror("Error initializing PARENT semaphore");
exit(1);
}
}
signal(2, (__sighandler_t)sigterm_handler);
signal(15, (__sighandler_t)sigterm_handler);
signal(17, (__sighandler_t)sigchld_handler);
v3 = fork();
if ( v3 != -1 )
{
if ( !v3 )
goto LABEL_24;
v4 = fork();
if ( v4 != -1 )
{
if ( v4 )
parent_create();
LABEL_24:
signal(2, 0LL);
signal(15, 0LL);
signal(17, 0LL);
child_create();
}
}
perror("Fork Failed");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
__pid_t v4; // eax

printf("[%d]\n", 1LL);
fflush(stdout);
shm_id = shm_open("MY_SHARED_MEMORY", 66, 0x1B6u);
if ( shm_id == -1 )
perror("shm_open: ");
if ( ftruncate(shm_id, 4LL) == -1 )
perror("ftruncate: ");
shm_addr = (__int64)mmap(0LL, 4uLL, 3, 1, shm_id, 0LL);
if ( shm_addr == -1 )
perror("mmap: ");
*(_DWORD *)shm_addr = worms;
sem = sem_open("SEM_MUTEX", 64, 2LL, 1LL);
if ( !sem )
{
perror("Error initializing semaphore");
exit(1);
}
sem_bird = sem_open("SEM_BIRD", 64, 2LL, 1LL);
if ( !sem_bird )
{
perror("Error initializing semaphore");
exit(1);
}
if ( *(_DWORD *)shm_addr )
{
if ( *(int *)shm_addr <= 0 )
{
perror("Error initializing PARENT semaphore: BAD value Shared Memory");
exit(1);
}
sem_parent = sem_open("SEM_PARENT", 64, 2LL, 0LL);
if ( !sem_parent )
{
perror("Error initializing PARENT semaphore");
exit(1);
}
}
else
{
sem_parent = sem_open("SEM_PARENT", 64, 2LL, 1LL);
if ( !sem_parent )
{
perror("Error initializing PARENT semaphore");
exit(1);
}
}
signal(2, (__sighandler_t)sigterm_handler);
signal(15, (__sighandler_t)sigterm_handler);
signal(17, (__sighandler_t)sigchld_handler);
v3 = fork();
if ( v3 != -1 )
{
if ( !v3 )
goto LABEL_24;
v4 = fork();
if ( v4 != -1 )
{
if ( v4 )
func9();
LABEL_24:
signal(2, 0LL);
signal(15, 0LL);
signal(17, 0LL);
func10();
}
}
perror("Fork Failed");
exit(1);
}

original_code:
int main(int argc, char* argv[])
{
    key_t shm_key;
    size_t shm_size;
    printf("[%d]\n", 1);
    fflush(stdout);
    shm_key = 1986;
    shm_size = sizeof(worms);
    shm_id = shm_open( "MY_SHARED_MEMORY", 0100 | 02, 0666 ) ;
    if (shm_id == -1)
        perror("shm_open: ");
    if (ftruncate(shm_id, shm_size) == -1)
        perror("ftruncate: ");
    shm_addr = mmap(((void *)0), shm_size, 0x1 | 0x2, 0x01, shm_id, 0);
    if (shm_addr == ((void *) -1))
        perror("mmap: ");
    *shm_addr = worms;
     if((sem = sem_open("SEM_MUTEX", 0100, 02, 1)) == ((sem_t *) 0)){
         perror("Error initializing semaphore");
        exit(1);
    }
     if((sem_bird = sem_open("SEM_BIRD", 0100, 02, 1)) == ((sem_t *) 0)){
         perror("Error initializing semaphore");
         exit(1);
     }
    if(*shm_addr == 0){
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 1)) == ((sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else if(*shm_addr > 0) {
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 0)) == ((sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else {
        perror("Error initializing PARENT semaphore: BAD value Shared Memory");
        exit(1);
    }
 signal(2, sigterm_handler);
 signal(15, sigterm_handler);
 signal(17, sigchld_handler);
    int i;
    for(i = 0; i < 2; i++ ) {
        switch (fork()) {
            case -1 :
                perror("Fork Failed");
                exit(1);
                break;
            case 0 :
                signal(2, ((__sighandler_t) 0));
                signal(15, ((__sighandler_t) 0));
                signal(17, ((__sighandler_t) 0));
                child_create();
                exit(0);
            default :
                break;
        }
    }
    parent_create() ;
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int shm_key;
    unsigned long shm_size;
    printf("[%d]\n", 1);
    fflush(stdout);
    shm_key = 1986;
    shm_size = sizeof(worms);
    shm_id = shm_open( "MY_SHARED_MEMORY", 0100 | 02, 0666 ) ;
    if (shm_id == -1)
        perror("shm_open: ");
    if (ftruncate(shm_id, shm_size) == -1)
        perror("ftruncate: ");
    shm_addr = mmap(((void *)0), shm_size, 0x1 | 0x2, 0x01, shm_id, 0);
    if (shm_addr == ((void *) -1))
        perror("mmap: ");
    *shm_addr = worms;
     if((sem = sem_open("SEM_MUTEX", 0100, 02, 1)) == ((union sem_t *) 0)){
         perror("Error initializing semaphore");
        exit(1);
    }
     if((sem_bird = sem_open("SEM_BIRD", 0100, 02, 1)) == ((union sem_t *) 0)){
         perror("Error initializing semaphore");
         exit(1);
     }
    if(*shm_addr == 0){
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 1)) == ((union sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else if(*shm_addr > 0) {
        if((sem_parent = sem_open("SEM_PARENT", 0100, 02, 0)) == ((union sem_t *) 0)){
            perror("Error initializing PARENT semaphore");
            exit(1);
        }
    } else {
        perror("Error initializing PARENT semaphore: BAD value Shared Memory");
        exit(1);
    }
 signal(2, sigterm_handler);
 signal(15, sigterm_handler);
 signal(17, sigchld_handler);
    int i;
    for(i = 0; i < 2; i++ ) {
        switch (fork()) {
            case -1 :
                perror("Fork Failed");
                exit(1);
                break;
            case 0 :
                signal(2, ((void (*)(int)) 0));
                signal(15, ((void (*)(int)) 0));
                signal(17, ((void (*)(int)) 0));
                child_create();
                exit(0);
            default :
                break;
        }
    }
    parent_create() ;
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

return_type: int

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


repo: ecoluca/Kernel-Example

decompiled_code:
__int64 __fastcall myhello_write(int a1, const char **a2, const void *a3, size_t a4, int a5, int a6)
{
int v10; // eax
int v11; // ebx
__int64 result; // rax

if ( a1 != 1 )
fprintf(stderr, "Write only to %s\n", *a2);
v10 = open(*a2, a5 == 0 ? 1 : 2049);
v11 = v10;
if ( v10 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
write(v10, a3, a4);
if ( !a6 )
write(v11, "\n", 1uLL);
close(v11);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, const char **a2, const void *a3, size_t a4, int a5, int a6)
{
int v10; // eax
int v11; // ebx
__int64 result; // rax

if ( a1 != 1 )
fprintf(stderr, "Write only to %s\n", *a2);
v10 = open(*a2, a5 == 0 ? 1 : 2049);
v11 = v10;
if ( v10 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
write(v10, a3, a4);
if ( !a6 )
write(v11, "\n", 1uLL);
close(v11);
result = 0LL;
}
return result;
}

original_code:
static int myhello_write(int dev_no, char *dev[], char *buf,
 size_t size, int nonblock, int nonewline)
{
 int fd, flags, retval = 0;
 ssize_t count;
 if (dev_no != 1)
  fprintf(stderr, "Write only to %s\n", dev[0]);
 flags = nonblock ? (01 | 04000) : (01);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 count = write(fd, buf, size);
 if (!nonewline) {
  count += write(fd, "\n", 1);
 }
 close(fd);
 return retval;
err1:
 return retval;
}

canonical_original_code:
static int myhello_write(int dev_no, char *dev[], char *buf, unsigned long size, int nonblock, int nonewline)
{
 int fd; int flags; int retval = 0;
 long count;
 if (dev_no != 1)
  fprintf(stderr, "Write only to %s\n", dev[0]);
 flags = nonblock ? (01 | 04000) : (01);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 count = write(fd, buf, size);
 if (!nonewline) {
  count += write(fd, "\n", 1);
 }
 close(fd);
 return retval;
err1:
 return retval;
}

variable_types:
dev
char *[]

variable_types:
buf
char *

variable_types:
size
unsigned long

variable_types: nonblock int

variable_types: nonewline int

variable_types: fd int

variable_types: flags int

variable_types: retval int

variable_types: count long


decompiled_code:
__int64 __fastcall myhello_read(int a1, const char **a2, int a3)
{
int v4; // er12
void *v5; // rbp
__int64 result; // rax
int v7; // eax
signed __int64 v8; // rbx

if ( a1 != 1 )
fprintf(stderr, "Read only from %s\n", *a2);
if ( a3 )
a3 = 2048;
v4 = open(*a2, a3);
if ( v4 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
v5 = malloc(0x1000uLL);
if ( v5 )
{
while ( 1 )
{
v8 = read(v4, v5, 0x1000uLL);
if ( v8 <= 0 )
break;
v7 = fileno(stdout);
write(v7, v5, v8);
}
free(v5);
close(v4);
result = 0LL;
}
else
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
close(v4);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, const char **a2, int a3)
{
int v4; // er12
void *v5; // rbp
__int64 result; // rax
int v7; // eax
signed __int64 v8; // rbx

if ( a1 != 1 )
fprintf(stderr, "Read only from %s\n", *a2);
if ( a3 )
a3 = 2048;
v4 = open(*a2, a3);
if ( v4 < 0 )
{
fprintf(stderr, "Can't open %s\n", *a2);
result = 0xFFFFFFFFLL;
}
else
{
v5 = malloc(0x1000uLL);
if ( v5 )
{
while ( 1 )
{
v8 = read(v4, v5, 0x1000uLL);
if ( v8 <= 0 )
break;
v7 = fileno(stdout);
write(v7, v5, v8);
}
free(v5);
close(v4);
result = 0LL;
}
else
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
close(v4);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static int myhello_read(int dev_no, char *dev[], int nonblock)
{
 int fd, flags, retval = 0;
 char *buf;
 size_t size;
 ssize_t count;
 if (dev_no != 1)
  fprintf(stderr, "Read only from %s\n", dev[0]);
 flags = nonblock ? (00 | 04000) : (00);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  count = read(fd, buf, size);
  if (count <= 0)
   break;
  count = write(fileno(stdout), buf, count);
 }
 free(buf);
 close(fd);
 return retval;
err2:
 close(fd);
err1:
 return retval;
}

canonical_original_code:
static int myhello_read(int dev_no, char *dev[], int nonblock)
{
 int fd; int flags; int retval = 0;
 char *buf;
 unsigned long size;
 long count;
 if (dev_no != 1)
  fprintf(stderr, "Read only from %s\n", dev[0]);
 flags = nonblock ? (00 | 04000) : (00);
 fd = open(dev[0], flags);
 if (fd < 0) {
  fprintf(stderr, "Can't open %s\n", dev[0]);
  retval = -1;
  goto err1;
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  count = read(fd, buf, size);
  if (count <= 0)
   break;
  count = write(fileno(stdout), buf, count);
 }
 free(buf);
 close(fd);
 return retval;
err2:
 close(fd);
err1:
 return retval;
}


decompiled_code:
__int64 __fastcall myhello_poll(int a1, const char **a2, int a3)
{
int v3; // er12
struct pollfd *v4; // rax
const char **v5; // rbx
__int16 *v6; // rbp
int v7; // eax
void *v8; // r15
__int16 *v9; // rbx
int *v11; // rbx
signed __int64 v12; // rbp
int v13; // eax
struct pollfd *fds; // [rsp+18h] [rbp-50h]

v3 = a3;
if ( a3 )
v3 = 2048;
v4 = (struct pollfd *)calloc(a1, 8uLL);
fds = v4;
if ( a1 > 0 )
{
v5 = a2;
v6 = &v4->events;
do
{
v7 = open(*v5, v3);
*((_DWORD *)v6 - 1) = v7;
*v6 = 1;
if ( v7 < 0 )
{
fprintf(stderr, "Can't open %s\n", *v5);
return 0xFFFFFFFFLL;
}
++v5;
v6 += 4;
}
while ( v5 != &a2[(unsigned int)(a1 - 1) + 1] );
}
v8 = malloc(0x1000uLL);
if ( !v8 )
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
goto LABEL_17;
}
do
{
LABEL_8:
if ( poll(fds, a1, -1) < 0 )
{
fwrite("Can't poll devices\n", 1uLL, 0x13uLL, stderr);
goto LABEL_16;
}
v9 = &fds->revents;
}
while ( a1 <= 0 );
while ( (*(_BYTE *)v9 & 1) == 0 )
{
LABEL_21:
v9 += 4;
if ( v9 == &fds[(unsigned int)(a1 - 1) + 1].revents )
goto LABEL_8;
}
v12 = read(*(_DWORD *)(v9 - 3), v8, 0x1000uLL);
if ( v12 >= 0 )
{
v13 = fileno(stdout);
write(v13, v8, v12);
*v9 &= 0xFFFEu;
goto LABEL_21;
}
fprintf(stderr, "Can't read %s\n", *(const char **)((char *)v9 + (_QWORD)a2 - (_QWORD)fds - 6));
LABEL_16:
free(v8);
LABEL_17:
if ( a1 > 0 )
{
v11 = (int *)fds;
do
{
close(*v11);
v11 += 2;
}
while ( v11 != (int *)&fds[a1] );
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, const char **a2, int a3)
{
int v3; // er12
struct pollfd *v4; // rax
const char **v5; // rbx
__int16 *v6; // rbp
int v7; // eax
void *v8; // r15
__int16 *v9; // rbx
int *v11; // rbx
signed __int64 v12; // rbp
int v13; // eax
struct pollfd *fds; // [rsp+18h] [rbp-50h]

v3 = a3;
if ( a3 )
v3 = 2048;
v4 = (struct pollfd *)calloc(a1, 8uLL);
fds = v4;
if ( a1 > 0 )
{
v5 = a2;
v6 = &v4->events;
do
{
v7 = open(*v5, v3);
*((_DWORD *)v6 - 1) = v7;
*v6 = 1;
if ( v7 < 0 )
{
fprintf(stderr, "Can't open %s\n", *v5);
return 0xFFFFFFFFLL;
}
++v5;
v6 += 4;
}
while ( v5 != &a2[(unsigned int)(a1 - 1) + 1] );
}
v8 = malloc(0x1000uLL);
if ( !v8 )
{
fwrite("Can't get memory\n", 1uLL, 0x11uLL, stderr);
goto LABEL_17;
}
do
{
LABEL_8:
if ( poll(fds, a1, -1) < 0 )
{
fwrite("Can't poll devices\n", 1uLL, 0x13uLL, stderr);
goto LABEL_16;
}
v9 = &fds->revents;
}
while ( a1 <= 0 );
while ( (*(_BYTE *)v9 & 1) == 0 )
{
LABEL_21:
v9 += 4;
if ( v9 == &fds[(unsigned int)(a1 - 1) + 1].revents )
goto LABEL_8;
}
v12 = read(*(_DWORD *)(v9 - 3), v8, 0x1000uLL);
if ( v12 >= 0 )
{
v13 = fileno(stdout);
write(v13, v8, v12);
*v9 &= 0xFFFEu;
goto LABEL_21;
}
fprintf(stderr, "Can't read %s\n", *(const char **)((char *)v9 + (_QWORD)a2 - (_QWORD)fds - 6));
LABEL_16:
free(v8);
LABEL_17:
if ( a1 > 0 )
{
v11 = (int *)fds;
do
{
close(*v11);
v11 += 2;
}
while ( v11 != (int *)&fds[a1] );
}
return 0xFFFFFFFFLL;
}

original_code:
static int myhello_poll(int dev_no, char *dev[], int nonblock)
{
 int flags, i, retval = 0;
 struct pollfd *fds;
 char *buf;
 size_t size;
 ssize_t count;
 flags = nonblock ? (00 | 04000) : (00);
 fds = calloc(dev_no, sizeof(struct pollfd));
 for (i = 0; i < dev_no; i++) {
  fds[i].fd = open(dev[i], flags);
  fds[i].events = 0x001;
  if (fds[i].fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  if (poll(fds, dev_no, -1) < 0) {
   fprintf(stderr, "Can't poll devices\n");
   retval = -1;
   goto err3;
  }
  for (i = 0; i < dev_no; i++) {
   if (fds[i].revents & 0x001) {
    count = read(fds[i].fd, buf, size);
    if (count < 0) {
     fprintf(stderr, "Can't read %s\n", dev[i]);
     retval = -1;
     goto err3;
    }
    count = write(fileno(stdout), buf, count);
    fds[i].revents &= ~0x001;
   }
  }
 }
 free(buf);
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
 return retval;
err3:
 free(buf);
err2:
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
err1:
 return retval;
}

canonical_original_code:
static int myhello_poll(int dev_no, char *dev[], int nonblock)
{
 int flags; int i; int retval = 0;
 struct pollfd *fds;
 char *buf;
 unsigned long size;
 long count;
 flags = nonblock ? (00 | 04000) : (00);
 fds = calloc(dev_no, sizeof(struct pollfd));
 for (i = 0; i < dev_no; i++) {
  fds[i].fd = open(dev[i], flags);
  fds[i].events = 0x001;
  if (fds[i].fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
 }
 size = (4 * 1024);
 buf = malloc(size);
 if (buf == ((void *)0)) {
  fprintf(stderr, "Can't get memory\n");
  retval = -1;
  goto err2;
 }
 while (1) {
  if (poll(fds, dev_no, -1) < 0) {
   fprintf(stderr, "Can't poll devices\n");
   retval = -1;
   goto err3;
  }
  for (i = 0; i < dev_no; i++) {
   if (fds[i].revents & 0x001) {
    count = read(fds[i].fd, buf, size);
    if (count < 0) {
     fprintf(stderr, "Can't read %s\n", dev[i]);
     retval = -1;
     goto err3;
    }
    count = write(fileno(stdout), buf, count);
    fds[i].revents &= ~0x001;
   }
  }
 }
 free(buf);
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
 return retval;
err3:
 free(buf);
err2:
 for (i = 0; i < dev_no; i++)
  close(fds[i].fd);
err1:
 return retval;
}

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


decompiled_code:
__int64 __fastcall myhello_ioctl(int a1, const char **a2, int a3)
{
const char **v4; // rbp
int v5; // eax
int v6; // ebx
unsigned int v8; // [rsp+4h] [rbp-34h] BYREF
unsigned int v9; // [rsp+8h] [rbp-30h] BYREF
unsigned int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

fwrite("device\t\tsize\tfree\tready\n", 1uLL, 0x18uLL, stdout);
fwrite("-------------------------------------\n", 1uLL, 0x26uLL, stdout);
if ( a3 )
a3 = 2048;
if ( a1 <= 0 )
return 0LL;
v4 = a2;
while ( 1 )
{
v5 = open(*v4, a3);
v6 = v5;
if ( v5 < 0 )
{
fprintf(stderr, "Can't open %s\n", *v4);
return 0xFFFFFFFFLL;
}
if ( ioctl(v5, 0x80046D00uLL, v10) < 0 || ioctl(v6, 0x80046D01uLL, &v9) < 0 || ioctl(v6, 0x80046D02uLL, &v8) < 0 )
break;
fprintf(stdout, "%s\t%d\t%d\t%d\n", *v4, v10[0], v9, v8);
close(v6);
if ( ++v4 == &a2[(unsigned int)(a1 - 1) + 1] )
return 0LL;
}
fprintf(stderr, "Can't ioctl %s\n", *v4);
close(v6);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, const char **a2, int a3)
{
const char **v4; // rbp
int v5; // eax
int v6; // ebx
unsigned int v8; // [rsp+4h] [rbp-34h] BYREF
unsigned int v9; // [rsp+8h] [rbp-30h] BYREF
unsigned int v10[11]; // [rsp+Ch] [rbp-2Ch] BYREF

fwrite("device\t\tsize\tfree\tready\n", 1uLL, 0x18uLL, stdout);
fwrite("-------------------------------------\n", 1uLL, 0x26uLL, stdout);
if ( a3 )
a3 = 2048;
if ( a1 <= 0 )
return 0LL;
v4 = a2;
while ( 1 )
{
v5 = open(*v4, a3);
v6 = v5;
if ( v5 < 0 )
{
fprintf(stderr, "Can't open %s\n", *v4);
return 0xFFFFFFFFLL;
}
if ( ioctl(v5, 0x80046D00uLL, v10) < 0 || ioctl(v6, 0x80046D01uLL, &v9) < 0 || ioctl(v6, 0x80046D02uLL, &v8) < 0 )
break;
fprintf(stdout, "%s\t%d\t%d\t%d\n", *v4, v10[0], v9, v8);
close(v6);
if ( ++v4 == &a2[(unsigned int)(a1 - 1) + 1] )
return 0LL;
}
fprintf(stderr, "Can't ioctl %s\n", *v4);
close(v6);
return 0xFFFFFFFFLL;
}

original_code:
static int myhello_ioctl(int dev_no, char *dev[], int nonblock)
{
 int fd, flags, i, retval = 0;
 int dev_size, dev_free, dev_ready;
 fprintf(stdout, "device\t\tsize\tfree\tready\n");
 fprintf(stdout, "-------------------------------------\n");
 flags = nonblock ? (00 | 04000) : (00);
 for (i = 0; i < dev_no; i++) {
  fd = open(dev[i], flags);
  if (fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((0)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_size) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((1)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_free) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((2)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_ready) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  fprintf(stdout, "%s\t%d\t%d\t%d\n", dev[i],
   dev_size, dev_free, dev_ready);
  close(fd);
 }
 return retval;
err2:
 close(fd);
err1:
 return retval;
}

canonical_original_code:
static int myhello_ioctl(int dev_no, char *dev[], int nonblock)
{
 int fd; int flags; int i; int retval = 0;
 int dev_size; int dev_free; int dev_ready;
 fprintf(stdout, "device\t\tsize\tfree\tready\n");
 fprintf(stdout, "-------------------------------------\n");
 flags = nonblock ? (00 | 04000) : (00);
 for (i = 0; i < dev_no; i++) {
  fd = open(dev[i], flags);
  if (fd < 0) {
   fprintf(stderr, "Can't open %s\n", dev[i]);
   retval = -1;
   goto err1;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((0)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_size) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((1)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_free) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  if (ioctl(fd, (((2U) << (((0 +8)+8)+14)) | ((('m')) << (0 +8)) | (((2)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &dev_ready) < 0) {
   fprintf(stderr, "Can't ioctl %s\n", dev[i]);
   retval = -1;
   goto err2;
  }
  fprintf(stdout, "%s\t%d\t%d\t%d\n", dev[i],
   dev_size, dev_free, dev_ready);
  close(fd);
 }
 return retval;
err2:
 close(fd);
err1:
 return retval;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r15
int v4; // er14
int v5; // er13
int v6; // eax
int v7; // edx
int v8; // er12
const char **v9; // rbx
size_t v10; // rax
int v12; // [rsp+0h] [rbp-48h]
int v13; // [rsp+4h] [rbp-44h]
int v14; // [rsp+8h] [rbp-40h]
int v15; // [rsp+Ch] [rbp-3Ch]

v3 = 0LL;
v14 = 0;
v15 = 0;
v4 = 0;
v5 = 0;
v13 = 0;
v12 = 0;
while ( 1 )
{
v6 = getopt_long(argc, (char *const *)argv, "rw:pibnvh", &long_opts, 0LL);
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'b':
v12 = 1;
break;
case 'h':
myhello_usage(stdout);
exit(0);
case 'i':
v14 = 1;
break;
case 'n':
v13 = 1;
break;
case 'p':
v15 = 1;
break;
case 'r':
v5 = 1;
break;
case 'v':
myhello_version(stdout);
exit(0);
case 'w':
v3 = (const char *)optarg;
v4 = 1;
break;
default:
myhello_usage(stderr);
exit(1);
}
}
v7 = v14 + v15 + v5 + v4;
if ( v7 )
{
if ( v7 <= 1 )
{
if ( optind != argc )
{
v8 = argc - optind;
v9 = &argv[optind];
if ( v5 )
{
v6 = myhello_read(v8, v9, v12);
}
else if ( v4 )
{
v10 = strlen(v3);
v6 = myhello_write(v8, v9, v3, v10, v12, v13);
}
else if ( v15 )
{
v6 = myhello_poll(v8, v9, v12);
}
else if ( v14 )
{
v6 = myhello_ioctl(v8, v9, v12);
}
exit(v6 != 0);
}
fwrite("You have to provide at least a device.\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
fwrite(
"You have specified multiple functions.\nYou can only perform one function at a time.\n",
1uLL,
0x54uLL,
stderr);
exit(1);
}
fwrite("You have to provide a function.\n", 1uLL, 0x20uLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r15
int v4; // er14
int v5; // er13
int v6; // eax
int v7; // edx
int v8; // er12
const char **v9; // rbx
size_t v10; // rax
int v12; // [rsp+0h] [rbp-48h]
int v13; // [rsp+4h] [rbp-44h]
int v14; // [rsp+8h] [rbp-40h]
int v15; // [rsp+Ch] [rbp-3Ch]

v3 = 0LL;
v14 = 0;
v15 = 0;
v4 = 0;
v5 = 0;
v13 = 0;
v12 = 0;
while ( 1 )
{
v6 = getopt_long(argc, (char *const *)argv, "rw:pibnvh", &long_opts, 0LL);
if ( v6 == -1 )
break;
switch ( v6 )
{
case 'b':
v12 = 1;
break;
case 'h':
func8(stdout);
exit(0);
case 'i':
v14 = 1;
break;
case 'n':
v13 = 1;
break;
case 'p':
v15 = 1;
break;
case 'r':
v5 = 1;
break;
case 'v':
func7(stdout);
exit(0);
case 'w':
v3 = (const char *)optarg;
v4 = 1;
break;
default:
func8(stderr);
exit(1);
}
}
v7 = v14 + v15 + v5 + v4;
if ( v7 )
{
if ( v7 <= 1 )
{
if ( optind != argc )
{
v8 = argc - optind;
v9 = &argv[optind];
if ( v5 )
{
v6 = func10(v8, v9, v12);
}
else if ( v4 )
{
v10 = strlen(v3);
v6 = func9(v8, v9, v3, v10, v12, v13);
}
else if ( v15 )
{
v6 = func11(v8, v9, v12);
}
else if ( v14 )
{
v6 = func12(v8, v9, v12);
}
exit(v6 != 0);
}
fwrite("You have to provide at least a device.\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
fwrite(
"You have specified multiple functions.\nYou can only perform one function at a time.\n",
1uLL,
0x54uLL,
stderr);
exit(1);
}
fwrite("You have to provide a function.\n", 1uLL, 0x20uLL, stderr);
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
 int opt, opt_nonblock, opt_nonewline;
 int opt_read, opt_write, opt_poll, opt_info;
 char *arg_write;
 int retval = 0;
 opt_nonblock = opt_nonewline = 0;
 opt_read = opt_write = opt_poll = opt_info = 0;
 arg_write = ((void *)0);
 while ((opt = getopt_long(argc, argv, "rw:pibnvh", long_opts, ((void *)0))) != -1) {
  switch (opt) {
  case 'r':
   opt_read = 1;
   break;
  case 'w':
   opt_write = 1;
   arg_write = optarg;
   break;
  case 'p':
   opt_poll = 1;
   break;
  case 'i':
   opt_info = 1;
   break;
  case 'b':
   opt_nonblock = 1;
   break;
  case 'n':
   opt_nonewline = 1;
   break;
  case 'v':
   myhello_version(stdout);
   exit(0);
  case 'h':
   myhello_usage(stdout);
   exit(0);
  default:
   myhello_usage(stderr);
   exit(1);
  }
 }
 if (opt_read + opt_write + opt_poll + opt_info == 0) {
  fprintf(stderr, "You have to provide a function.\n");
  exit(1);
 }
 if (opt_read + opt_write + opt_poll + opt_info > 1) {
  fprintf(stderr, "You have specified multiple functions.\n"
   "You can only perform one function at a time.\n");
  exit(1);
 }
 if (argc == optind) {
  fprintf(stderr, "You have to provide at least a device.\n");
  exit(1);
 }
 argc -= optind;
 argv += optind;
 if (opt_read) {
  retval = myhello_read(argc, argv, opt_nonblock);
 } else if (opt_write) {
  retval = myhello_write(argc, argv, arg_write, strlen(arg_write),
   opt_nonblock, opt_nonewline);
 } else if (opt_poll) {
  retval = myhello_poll(argc, argv, opt_nonblock);
 } else if (opt_info) {
  retval = myhello_ioctl(argc, argv, opt_nonblock);
 } else {
  retval = -1;
 }
 exit(retval == 0 ? 0 : 1);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int opt; int opt_nonblock; int opt_nonewline;
 int opt_read; int opt_write; int opt_poll; int opt_info;
 char *arg_write;
 int retval = 0;
 opt_nonblock = opt_nonewline = 0;
 opt_read = opt_write = opt_poll = opt_info = 0;
 arg_write = ((void *)0);
 while ((opt = getopt_long(argc, argv, "rw:pibnvh", long_opts, ((void *)0))) != -1) {
  switch (opt) {
  case 'r':
   opt_read = 1;
   break;
  case 'w':
   opt_write = 1;
   arg_write = optarg;
   break;
  case 'p':
   opt_poll = 1;
   break;
  case 'i':
   opt_info = 1;
   break;
  case 'b':
   opt_nonblock = 1;
   break;
  case 'n':
   opt_nonewline = 1;
   break;
  case 'v':
   myhello_version(stdout);
   exit(0);
  case 'h':
   myhello_usage(stdout);
   exit(0);
  default:
   myhello_usage(stderr);
   exit(1);
  }
 }
 if (opt_read + opt_write + opt_poll + opt_info == 0) {
  fprintf(stderr, "You have to provide a function.\n");
  exit(1);
 }
 if (opt_read + opt_write + opt_poll + opt_info > 1) {
  fprintf(stderr, "You have specified multiple functions.\n"
   "You can only perform one function at a time.\n");
  exit(1);
 }
 if (argc == optind) {
  fprintf(stderr, "You have to provide at least a device.\n");
  exit(1);
 }
 argc -= optind;
 argv += optind;
 if (opt_read) {
  retval = myhello_read(argc, argv, opt_nonblock);
 } else if (opt_write) {
  retval = myhello_write(argc, argv, arg_write, strlen(arg_write),
   opt_nonblock, opt_nonewline);
 } else if (opt_poll) {
  retval = myhello_poll(argc, argv, opt_nonblock);
 } else if (opt_info) {
  retval = myhello_ioctl(argc, argv, opt_nonblock);
 } else {
  retval = -1;
 }
 exit(retval == 0 ? 0 : 1);
}

variable_types: opt int


doMove

decompiled_code:
_BOOL8 __fastcall doMove(int fd, void *buf, int *a3)
{
void *v4; // rbx
int v5; // eax
bool v6; // zf
_BOOL4 v7; // ebx

v4 = calloc(0xFuLL, 1uLL);
addchar(v4);
if ( (int)read(*a3, v4, 0xFuLL) < 0 )
{
perror("Fehler beim Lesen der Pipe");
v7 = 1;
}
else
{
sendReplyFormatted((unsigned int)fd, v4);
v5 = recv(fd, buf, 0x1FFuLL, 0);
if ( v5 > 0 )
*((_BYTE *)buf + v5) = 0;
v6 = strcmp((const char *)buf, "+ MOVEOK\n") == 0;
v7 = !v6;
if ( v6 )
{
puts("\nZug wurde akzeptiert!");
puts("\n**********************************");
}
else
{
v7 = 1;
}
}
return v7;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int fd, void *buf, int *a3)
{
void *v4; // rbx
int v5; // eax
bool v6; // zf
_BOOL4 v7; // ebx

v4 = calloc(0xFuLL, 1uLL);
addchar(v4);
if ( (int)read(*a3, v4, 0xFuLL) < 0 )
{
perror("Fehler beim Lesen der Pipe");
v7 = 1;
}
else
{
sendReplyFormatted((unsigned int)fd, v4);
v5 = recv(fd, buf, 0x1FFuLL, 0);
if ( v5 > 0 )
*((_BYTE *)buf + v5) = 0;
v6 = strcmp((const char *)buf, "+ MOVEOK\n") == 0;
v7 = !v6;
if ( v6 )
{
puts("\nZug wurde akzeptiert!");
puts("\n**********************************");
}
else
{
v7 = 1;
}
}
return v7;
}

original_code:
int doMove(int sock, char* buffer, int fd[]) {
 int size;
 char* reply = calloc(15,sizeof(char));
 addchar(reply);
 size = read(fd[0], reply, 15);
 if (size < 0) {
  perror("Fehler beim Lesen der Pipe");
  return 1;
 }
 sendReplyFormatted(sock, reply);
 size = recv(sock, buffer, 512 - 1, 0);
 if (size > 0) {
  buffer[size] = '\0';
 }
 if (strcmp(buffer, "+ MOVEOK\n") == 0) {
  printf("\nZug wurde akzeptiert!\n");
  printf("\n**********************************\n");
 } else {
  return 1;
 }
 return 0;
}

canonical_original_code:
int doMove(int sock, char *buffer, int fd[]) {
 int size;
 char *reply = calloc(15,sizeof(char));
 addchar(reply);
 size = read(fd[0], reply, 15);
 if (size < 0) {
  perror("Fehler beim Lesen der Pipe");
  return 1;
 }
 sendReplyFormatted(sock, reply);
 size = recv(sock, buffer, 512 - 1, 0);
 if (size > 0) {
  buffer[size] = '\0';
 }
 if (strcmp(buffer, "+ MOVEOK\n") == 0) {
  printf("\nZug wurde akzeptiert!\n");
  printf("\n**********************************\n");
 } else {
  return 1;
 }
 return 0;
}

variable_types: sock int

variable_types:
buffer
char *

variable_types:
fd
int []

variable_types: size int

variable_types:
reply
char *


waitforfds

decompiled_code:
__int64 __fastcall waitforfds(int a1, void *a2, __int64 a3, int *a4)
{
int v4; // er15
int v5; // er13
unsigned int v6; // ebx
int v7; // ebp
__int64 v8; // r14
int v9; // er12
__int64 v10; // r13
int v11; // ebx
int v12; // eax
struct timeval timeout; // [rsp+20h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+30h] [rbp-B8h] BYREF

v4 = a1;
v5 = *a4;
v6 = 1;
if ( !(unsigned int)((__int64 (*)(void))parseServerReply)() )
{
if ( v5 >= a1 )
v4 = v5;
v7 = a1 / 64;
v8 = 1LL << (a1 % 64);
v9 = v5 / 64;
v10 = 1LL << (v5 % 64);
do
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v8;
readfds.fds_bits[v9] |= v10;
timeout.tv_sec = 30LL;
timeout.tv_usec = 0LL;
v12 = select(v4 + 1, &readfds, 0LL, 0LL, &timeout);
v6 = v12;
if ( v12 == -1 )
{
perror("Error, select failed! \n");
return 1;
}
if ( v12 <= 0 )
break;
if ( (readfds.fds_bits[v7] & v8) != 0 )
{
v11 = recv(a1, a2, 0x1FFuLL, 0);
writelog(logdatei, "src/Select.c:98");
if ( v11 > 0 )
*((_BYTE *)a2 + v11) = 0;
if ( (unsigned int)parseServerReply((unsigned int)a1, a2, a3) )
return 1;
}
else if ( (readfds.fds_bits[v9] & v10) != 0 )
{
doMove(a1, a2, a4);
}
}
}
while ( v12 );
perror("Select timed out!. \n");
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, void *a2, __int64 a3, int *a4)
{
int v4; // er15
int v5; // er13
unsigned int v6; // ebx
int v7; // ebp
__int64 v8; // r14
int v9; // er12
__int64 v10; // r13
int v11; // ebx
int v12; // eax
struct timeval timeout; // [rsp+20h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+30h] [rbp-B8h] BYREF

v4 = a1;
v5 = *a4;
v6 = 1;
if ( !(unsigned int)((__int64 (*)(void))parseServerReply)() )
{
if ( v5 >= a1 )
v4 = v5;
v7 = a1 / 64;
v8 = 1LL << (a1 % 64);
v9 = v5 / 64;
v10 = 1LL << (v5 % 64);
do
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7] |= v8;
readfds.fds_bits[v9] |= v10;
timeout.tv_sec = 30LL;
timeout.tv_usec = 0LL;
v12 = select(v4 + 1, &readfds, 0LL, 0LL, &timeout);
v6 = v12;
if ( v12 == -1 )
{
perror("Error, select failed! \n");
return 1;
}
if ( v12 <= 0 )
break;
if ( (readfds.fds_bits[v7] & v8) != 0 )
{
v11 = recv(a1, a2, 0x1FFuLL, 0);
writelog(logdatei, "src/Select.c:98");
if ( v11 > 0 )
*((_BYTE *)a2 + v11) = 0;
if ( (unsigned int)parseServerReply((unsigned int)a1, a2, a3) )
return 1;
}
else if ( (readfds.fds_bits[v9] & v10) != 0 )
{
func0(a1, a2, a4);
}
}
}
while ( v12 );
perror("Select timed out!. \n");
}
return v6;
}

original_code:
int waitforfds(int sock, char* buffer, sharedmem * shm, int fd[]) {
 int rc, size, status, biggest;
 int pipe = fd[0];
 fd_set fds;
 struct timeval timeout;
 if (pipe > sock) {
  biggest = pipe;
 } else {
  biggest = sock;
 }
 status = parseServerReply(sock, buffer, shm);
 if (status != 0) {
  return 1;
 }
 do {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))));
  timeout.tv_sec = 30;
  timeout.tv_usec = 0;
  rc = select(biggest + 1, &fds, ((void *)0), ((void *)0), &timeout);
  if (rc == -1) {
   perror("Error, select failed! \n");
   return 1;
  }
  if (rc > 0) {
   if (((((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    size = recv(sock, buffer, 512 - 1, 0);
    writelog(logdatei, "src/Select.c" ":" "98");
    if (size > 0) {
     buffer[size] = '\0';
    }
    status = parseServerReply(sock, buffer, shm);
    if (status != 0) {
     return 1;
    }
   } else if (((((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    doMove(sock, buffer, fd);
   }
  } else if (rc == 0) {
   perror("Select timed out!. \n");
  }
 } while (rc != 0);
 return 0;
}

canonical_original_code:
int waitforfds(int sock, char *buffer, struct sharedmem *shm, int fd[]) {
 int rc; int size; int status; int biggest;
 int pipe = fd[0];
 struct fd_set fds;
 struct timeval timeout;
 if (pipe > sock) {
  biggest = pipe;
 } else {
  biggest = sock;
 }
 status = parseServerReply(sock, buffer, shm);
 if (status != 0) {
  return 1;
 }
 do {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))));
  timeout.tv_sec = 30;
  timeout.tv_usec = 0;
  rc = select(biggest + 1, &fds, ((void *)0), ((void *)0), &timeout);
  if (rc == -1) {
   perror("Error, select failed! \n");
   return 1;
  }
  if (rc > 0) {
   if (((((&fds)->__fds_bits)[((sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    size = recv(sock, buffer, 512 - 1, 0);
    writelog(logdatei, "src/Select.c" ":" "98");
    if (size > 0) {
     buffer[size] = '\0';
    }
    status = parseServerReply(sock, buffer, shm);
    if (status != 0) {
     return 1;
    }
   } else if (((((&fds)->__fds_bits)[((pipe) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pipe) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    doMove(sock, buffer, fd);
   }
  } else if (rc == 0) {
   perror("Select timed out!. \n");
  }
 } while (rc != 0);
 return 0;
}

variable_types: struct sharedmem { int pidDad; int pidKid; char gameID[42]; int sock; int *pf; struct player { int playerNumber; char playerName[50]; int playerReady; int playerRegisterd; } player[8]; } *

variable_types: rc int

variable_types: status int

variable_types: biggest int

variable_types: pipe int

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

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

user_defined_types: struct sharedmem { int pidDad; int pidKid; char gameID[42]; int sock; int *pf; struct player player[8]; }


readGameField

original_code:
int readGameField(char *buffer, sharedmem * shm) {
 char* buffer2;
 buffer2 = malloc(sizeof(char) * 128);
 addchar(buffer2);
 int i = 0, znr = 0;
 char tmp = '*';
 buffer2 = strstr(buffer, "+ FIELD");
 buffer2 = strtok(buffer2, "\n");
 buffer2 = strtok(((void *)0), "\n");
 while (strcmp(buffer2, "+ ENDFIELD") != 0) {
  sscanf(buffer2, "%*s %d %[0-9* ]", &znr, buffer2);
  for (i = 0; i < shm->fieldX; i++) {
   *(shm->pf + i + ((znr - 4) * (-1)) * shm->fieldX) = -1;
   if (buffer2 == strchr(buffer2, tmp)) {
    sscanf(buffer2, "%*s %[0-9* ]", buffer2);
   } else {
    sscanf(buffer2, "%d %[0-9* ]", (shm->pf + i + ((znr - 4) * (-1)) * (shm->fieldY)), buffer2);
   }
  }
  buffer2 = strtok(((void *)0), "\n");
 }
 return 0;
}

canonical_original_code:
int readGameField(char *buffer, struct sharedmem *shm) {
 char *buffer2;
 buffer2 = malloc(sizeof(char) * 128);
 addchar(buffer2);
 int i = 0; int znr = 0;
 char tmp = '*';
 buffer2 = strstr(buffer, "+ FIELD");
 buffer2 = strtok(buffer2, "\n");
 buffer2 = strtok(((void *)0), "\n");
 while (strcmp(buffer2, "+ ENDFIELD") != 0) {
  sscanf(buffer2, "%*s %d %[0-9* ]", &znr, buffer2);
  for (i = 0; i < shm->fieldX; i++) {
   *(shm->pf + i + ((znr - 4) * (-1)) * shm->fieldX) = -1;
   if (buffer2 == strchr(buffer2, tmp)) {
    sscanf(buffer2, "%*s %[0-9* ]", buffer2);
   } else {
    sscanf(buffer2, "%d %[0-9* ]", (shm->pf + i + ((znr - 4) * (-1)) * (shm->fieldY)), buffer2);
   }
  }
  buffer2 = strtok(((void *)0), "\n");
 }
 return 0;
}

variable_types: i int

variable_types: znr int

variable_types: tmp char

user_defined_types: struct player { int playerNumber; char playerName[50]; int playerReady; int playerRegisterd; }


printGameField

original_code:
int printGameField(sharedmem * shm) {
 int i, j;
 for (i = 0; i < shm->fieldY; i++) {
  printf("\n %d: ", i + 1);
  for (j = 0; j < shm->fieldX; j++) {
   if (*(shm->pf + j + i * shm->fieldY) == -1) {
    printf(" *");
   } else {
    printf(" %d ", *(shm->pf + j + i * shm->fieldY));
   }
  }
 }
 return 0;
}

canonical_original_code:
int printGameField(struct sharedmem *shm) {
 int i; int j;
 for (i = 0; i < shm->fieldY; i++) {
  printf("\n %d: ", i + 1);
  for (j = 0; j < shm->fieldX; j++) {
   if (*(shm->pf + j + i * shm->fieldY) == -1) {
    printf(" *");
   } else {
    printf(" %d ", *(shm->pf + j + i * shm->fieldY));
   }
  }
 }
 return 0;
}

variable_types: j int


handleRecv

decompiled_code:
__int64 __fastcall handleRecv(int a1, _BYTE *a2)
{
int v2; // eax
__int64 result; // rax

v2 = recv(a1, a2, 0x1FFuLL, 0);
if ( v2 > 0 )
a2[v2] = 0;
result = 0LL;
if ( *a2 == 45 )
{
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", a2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, _BYTE *a2)
{
int v2; // eax
__int64 result; // rax

v2 = recv(a1, a2, 0x1FFuLL, 0);
if ( v2 > 0 )
a2[v2] = 0;
result = 0LL;
if ( *a2 == 45 )
{
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", a2);
result = 1LL;
}
return result;
}

original_code:
int handleRecv(int sock, char* buffer) {
 int err;
 err = recv(sock, buffer, 512 - 1, 0);
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 return 0;
}

canonical_original_code:
int handleRecv(int sock, char *buffer) {
 int err;
 err = recv(sock, buffer, 512 - 1, 0);
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 return 0;
}

variable_types: err int


parseServerReply

decompiled_code:
__int64 __fastcall parseServerReply(int fd, char *haystack, __int64 a3)
{
char *v3; // rbx
unsigned int v6; // er13
int v7; // eax
unsigned int v9; // [rsp+Ch] [rbp-22Ch] BYREF
char v10[552]; // [rsp+10h] [rbp-228h] BYREF

v3 = haystack;
if ( *haystack == 45 )
{
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", haystack);
return 1;
}
if ( !memcmp(haystack, "+ MOVE", 6uLL) && strlen(v3) <= 0xE )
{
_isoc99_sscanf(v3, "%*s %*s %d", a3 + 112);
v6 = 1;
if ( (unsigned int)handleRecv(fd, v3) )
return v6;
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
printf("\nFuer deinen Zug hast du %d ms und ", *(unsigned int *)(a3 + 112));
goto LABEL_14;
}
if ( strcmp(v3, "+ WAIT\n") )
{
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", a3 + 112, a3 + 132, a3 + 124, a3 + 128);
LABEL_14:
if ( strstr(v3, "+ GAMEOVER") )
{
if ( !memcmp(v3, "+ MOVEOK", 8uLL) )
{
strtok(v3, "\n");
v3 = strtok(0LL, "\n");
}
if ( !strcmp(v3, "+ GAMEOVER\n") || !memcmp(v3, "+ GAMEOVER\n+", 0xCuLL) )
{
puts("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.");
}
else
{
_isoc99_sscanf(v3, "%*s %*s %d %[^\n]\n", &v9, v10);
printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", v9, v10);
}
v6 = 2;
}
else if ( strstr(v3, "+ QUIT") )
{
puts("\nDas Spiel ist zu Ende.");
v6 = 2;
}
else
{
printf("\nStein %d ist zu setzen!\n", *(unsigned int *)(a3 + 132));
printf(
"\nUnser momentanes Spielfeld mit Groesse %d x %d:\n",
*(unsigned int *)(a3 + 124),
*(unsigned int *)(a3 + 128));
if ( *(_DWORD *)(a3 + 140)
|| (v7 = shmget(5678, 4 * *(int *)(a3 + 128) * (__int64)*(int *)(a3 + 124) + 3, 1021),
*(_DWORD *)(a3 + 140) = v7,
v7 > 0) )
{
*(_QWORD *)(a3 + 144) = shmat(*(_DWORD *)(a3 + 140), 0LL, 0);
readGameField(v3, a3);
sendReplyFormatted((unsigned int)fd, "THINKING");
v6 = handleRecv(fd, v3);
if ( v6 )
{
v6 = 1;
}
else if ( strcmp(v3, "+ OKTHINK\n") && (unsigned int)handleRecv(fd, v3) )
{
v6 = 1;
}
else
{
*(_DWORD *)(a3 + 116) = 1;
*(_DWORD *)(a3 + 120) = 1;
kill(*(_DWORD *)a3, 10);
}
}
else
{
writelog(logdatei, "src/ParseServerReply.c:176");
perror("\nFehler beim Initalisieren der Shared Memory pf");
v6 = 1;
}
}
return v6;
}
do
{
puts("\nWarte auf andere Spieler.");
send(fd, "OKWAIT\n", 7uLL, 0);
if ( (unsigned int)handleRecv(fd, v3) )
return 1;
}
while ( !strcmp(v3, "+ WAIT\n") );
if ( !memcmp(v3, "+ GAMEOVER", 0xAuLL) || (v6 = 1, !(unsigned int)handleRecv(fd, v3)) )
{
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
goto LABEL_14;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, char *haystack, __int64 a3)
{
char *v3; // rbx
unsigned int v6; // er13
int v7; // eax
unsigned int v9; // [rsp+Ch] [rbp-22Ch] BYREF
char v10[552]; // [rsp+10h] [rbp-228h] BYREF

v3 = haystack;
if ( *haystack == 45 )
{
printf("\nFehler in der Kommunikation, der Server meldet: %s \n", haystack);
return 1;
}
if ( !memcmp(haystack, "+ MOVE", 6uLL) && strlen(v3) <= 0xE )
{
_isoc99_sscanf(v3, "%*s %*s %d", a3 + 112);
v6 = 1;
if ( (unsigned int)func2(fd, v3) )
return v6;
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
printf("\nFuer deinen Zug hast du %d ms und ", *(unsigned int *)(a3 + 112));
goto LABEL_14;
}
if ( strcmp(v3, "+ WAIT\n") )
{
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", a3 + 112, a3 + 132, a3 + 124, a3 + 128);
LABEL_14:
if ( strstr(v3, "+ GAMEOVER") )
{
if ( !memcmp(v3, "+ MOVEOK", 8uLL) )
{
strtok(v3, "\n");
v3 = strtok(0LL, "\n");
}
if ( !strcmp(v3, "+ GAMEOVER\n") || !memcmp(v3, "+ GAMEOVER\n+", 0xCuLL) )
{
puts("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.");
}
else
{
_isoc99_sscanf(v3, "%*s %*s %d %[^\n]\n", &v9, v10);
printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", v9, v10);
}
v6 = 2;
}
else if ( strstr(v3, "+ QUIT") )
{
puts("\nDas Spiel ist zu Ende.");
v6 = 2;
}
else
{
printf("\nStein %d ist zu setzen!\n", *(unsigned int *)(a3 + 132));
printf(
"\nUnser momentanes Spielfeld mit Groesse %d x %d:\n",
*(unsigned int *)(a3 + 124),
*(unsigned int *)(a3 + 128));
if ( *(_DWORD *)(a3 + 140)
|| (v7 = shmget(5678, 4 * *(int *)(a3 + 128) * (__int64)*(int *)(a3 + 124) + 3, 1021),
*(_DWORD *)(a3 + 140) = v7,
v7 > 0) )
{
*(_QWORD *)(a3 + 144) = shmat(*(_DWORD *)(a3 + 140), 0LL, 0);
func0(v3, a3);
sendReplyFormatted((unsigned int)fd, "THINKING");
v6 = func2(fd, v3);
if ( v6 )
{
v6 = 1;
}
else if ( strcmp(v3, "+ OKTHINK\n") && (unsigned int)func2(fd, v3) )
{
v6 = 1;
}
else
{
*(_DWORD *)(a3 + 116) = 1;
*(_DWORD *)(a3 + 120) = 1;
kill(*(_DWORD *)a3, 10);
}
}
else
{
writelog(logdatei, "src/ParseServerReply.c:176");
perror("\nFehler beim Initalisieren der Shared Memory pf");
v6 = 1;
}
}
return v6;
}
do
{
puts("\nWarte auf andere Spieler.");
send(fd, "OKWAIT\n", 7uLL, 0);
if ( (unsigned int)func2(fd, v3) )
return 1;
}
while ( !strcmp(v3, "+ WAIT\n") );
if ( !memcmp(v3, "+ GAMEOVER", 0xAuLL) || (v6 = 1, !(unsigned int)func2(fd, v3)) )
{
_isoc99_sscanf(v3, "%*s %*s %d %*s %*s %d%*[,]%d", a3 + 132, a3 + 124, a3 + 128);
goto LABEL_14;
}
return v6;
}

original_code:
int parseServerReply(int sock, char* buffer, sharedmem * shm) {
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 if (strncmp(buffer, "+ MOVE", 6) == 0 && (strlen(buffer) < 15)) {
  sscanf(buffer, "%*s %*s %d", &(shm->thinkTime));
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
  printf("\nFuer deinen Zug hast du %d ms und ", shm->thinkTime);
 }
 else if (strcmp(buffer, "+ WAIT\n") == 0) {
  do {
   printf("\nWarte auf andere Spieler.\n");
   send(sock, "OKWAIT\n", strlen("OKWAIT\n"), 0);
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  } while (strcmp(buffer, "+ WAIT\n") == 0);
  if (strncmp(buffer, "+ GAMEOVER", 10) != 0) {
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 } else {
  sscanf(buffer, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", &(shm->thinkTime), &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 }
 if (strstr(buffer, "+ GAMEOVER") != ((void *)0) ) {
  int playerNumber;
  char playerName[512];
  if (strncmp(buffer, "+ MOVEOK", 8) == 0) {
   buffer = strtok(buffer, "\n");
   buffer = strtok(((void *)0), "\n");
  }
  if (strcmp(buffer, "+ GAMEOVER\n") == 0 || strncmp(buffer, "+ GAMEOVER\n+", 12) == 0) {
   printf("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.\n");
  } else {
   sscanf(buffer, "%*s %*s %d %[^\n]\n", &playerNumber, playerName);
   printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", playerNumber, playerName);
  }
  return 2;
 }
 if (strstr(buffer, "+ QUIT") != ((void *)0) ) {
  printf("\nDas Spiel ist zu Ende.\n");
  return 2;
 }
 printf("\nStein %d ist zu setzen!\n", shm->StoneToPlace);
 printf("\nUnser momentanes Spielfeld mit Groesse %d x %d:\n", shm->fieldX, shm->fieldY);
 if (shm->pfID == 0) {
  shm->pfID = shmget(5678, (sizeof(int) * (shm->fieldX) * (shm->fieldY) + 3), 01000 | 0775);
  if (shm->pfID < 1) {
   writelog(logdatei, "src/ParseServerReply.c" ":" "176");
   perror("\nFehler beim Initalisieren der Shared Memory pf");
   return 1;
  }
 }
 shm->pf = shmat(shm->pfID, 0, 0);
 readGameField(buffer, shm);
 sendReplyFormatted(sock, "THINKING");
 if (handleRecv(sock, buffer) != 0) {
  return 1;
 }
 if (strcmp(buffer, "+ OKTHINK\n") != 0) {
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
 }
 shm->thinking = 1;
 shm->pleaseThink = 1;
 kill(shm->pidDad, 10);
 return 0;
}

canonical_original_code:
int parseServerReply(int sock, char *buffer, struct sharedmem *shm) {
 if (buffer[0] == '-') {
  printf("\nFehler in der Kommunikation, der Server meldet: %s \n", buffer);
  return 1;
 }
 if (strncmp(buffer, "+ MOVE", 6) == 0 && (strlen(buffer) < 15)) {
  sscanf(buffer, "%*s %*s %d", &(shm->thinkTime));
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
  printf("\nFuer deinen Zug hast du %d ms und ", shm->thinkTime);
 }
 else if (strcmp(buffer, "+ WAIT\n") == 0) {
  do {
   printf("\nWarte auf andere Spieler.\n");
   send(sock, "OKWAIT\n", strlen("OKWAIT\n"), 0);
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  } while (strcmp(buffer, "+ WAIT\n") == 0);
  if (strncmp(buffer, "+ GAMEOVER", 10) != 0) {
   if (handleRecv(sock, buffer) != 0) {
    return 1;
   }
  }
  sscanf(buffer, "%*s %*s %d %*s %*s %d%*[,]%d", &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 } else {
  sscanf(buffer, "%*s %*s %d %*s %*s %d %*s %*s %d%*[,]%d", &(shm->thinkTime), &(shm->StoneToPlace), &(shm->fieldX), &(shm->fieldY));
 }
 if (strstr(buffer, "+ GAMEOVER") != ((void *)0) ) {
  int playerNumber;
  char playerName[512];
  if (strncmp(buffer, "+ MOVEOK", 8) == 0) {
   buffer = strtok(buffer, "\n");
   buffer = strtok(((void *)0), "\n");
  }
  if (strcmp(buffer, "+ GAMEOVER\n") == 0 || strncmp(buffer, "+ GAMEOVER\n+", 12) == 0) {
   printf("\nDas Spiel ist zu Ende. Es gibt keinen Gewinner.\n");
  } else {
   sscanf(buffer, "%*s %*s %d %[^\n]\n", &playerNumber, playerName);
   printf("\nDas Spiel ist zu Ende. Der Gewinner ist: Spieler %d - %s\n", playerNumber, playerName);
  }
  return 2;
 }
 if (strstr(buffer, "+ QUIT") != ((void *)0) ) {
  printf("\nDas Spiel ist zu Ende.\n");
  return 2;
 }
 printf("\nStein %d ist zu setzen!\n", shm->StoneToPlace);
 printf("\nUnser momentanes Spielfeld mit Groesse %d x %d:\n", shm->fieldX, shm->fieldY);
 if (shm->pfID == 0) {
  shm->pfID = shmget(5678, (sizeof(int) * (shm->fieldX) * (shm->fieldY) + 3), 01000 | 0775);
  if (shm->pfID < 1) {
   writelog(logdatei, "src/ParseServerReply.c" ":" "176");
   perror("\nFehler beim Initalisieren der Shared Memory pf");
   return 1;
  }
 }
 shm->pf = shmat(shm->pfID, 0, 0);
 readGameField(buffer, shm);
 sendReplyFormatted(sock, "THINKING");
 if (handleRecv(sock, buffer) != 0) {
  return 1;
 }
 if (strcmp(buffer, "+ OKTHINK\n") != 0) {
  if (handleRecv(sock, buffer) != 0) {
   return 1;
  }
 }
 shm->thinking = 1;
 shm->pleaseThink = 1;
 kill(shm->pidDad, 10);
 return 0;
}

variable_types: sock int

variable_types: playerNumber int

variable_types:
playerName
char [512]


sendReplyFormatted

decompiled_code:
void __fastcall sendReplyFormatted(int fd, char *src)
{
size_t v2; // rax
char *v3; // rbx
size_t v4; // rax

v2 = strlen(src);
v3 = (char *)malloc(v2 + 2);
strcpy(v3, src);
*(_WORD *)&v3[strlen(v3)] = 10;
v4 = strlen(v3);
send(fd, v3, v4, 0);
free(v3);
}

canonical_decompiled_code:
void __fastcall func0(int fd, char *src)
{
size_t v2; // rax
char *v3; // rbx
size_t v4; // rax

v2 = strlen(src);
v3 = (char *)malloc(v2 + 2);
strcpy(v3, src);
*(_WORD *)&v3[strlen(v3)] = 10;
v4 = strlen(v3);
send(fd, v3, v4, 0);
free(v3);
}

original_code:
void sendReplyFormatted(int sock, char* reply) {
 char * container;
 container = malloc(sizeof(char) * (strlen(reply) + 2));
 strcpy(container, reply);
 strcat(container, "\n");
 send(sock, container, strlen(container), 0);
 free(container);
}

canonical_original_code:
void sendReplyFormatted(int sock, char *reply) {
 char *container;
 container = malloc(sizeof(char) * (strlen(reply) + 2));
 strcpy(container, reply);
 strcat(container, "\n");
 send(sock, container, strlen(container), 0);
 free(container);
}

variable_types:
container
char *

return_type: void


antistrcat

decompiled_code:
__int64 __fastcall antistrcat(char *src, const char *a2, char *a3)
{
strcpy(a3, a2);
strcat(a3, src);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src, const char *a2, char *a3)
{
strcpy(a3, a2);
strcat(a3, src);
return 0LL;
}

original_code:
int antistrcat(char* dest, char* src, char* temp) {
 strcpy(temp, src);
 strcat(temp, dest);
 return 0;
}

canonical_original_code:
int antistrcat(char *dest, char *src, char *temp) {
 strcpy(temp, src);
 strcat(temp, dest);
 return 0;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
temp
char *


recvPlayerInfo

decompiled_code:
char *__fastcall recvPlayerInfo(char *dest, __int64 a2)
{
char *v3; // rbx
char *v4; // rax
__int64 v5; // rsi
void *v6; // r13
char *v7; // rax
__int64 v8; // rbx
char *result; // rax
__int64 v10; // rbx
int v11; // er13
char *v12; // rax

v3 = (char *)malloc(0x100uLL);
addchar(v3);
strcpy(v3, dest);
v4 = strtok(v3, "\n");
_isoc99_sscanf(v4, "%*s %*s %i", a2 + 108);
v5 = *(unsigned int *)(a2 + 108);
if ( (int)v5 > 8 )
{
printf("\nDiese Version des Clients unterstuetzt maximal 8 Spieler, die Anzahl ist jedoch %d Spieler!\n", v5);
result = 0LL;
}
else
{
v6 = malloc(0x32uLL);
addchar(v6);
v7 = strtok(0LL, "\n");
v8 = a2 + 272;
while ( strcmp(v7, "+ ENDPLAYERS") )
{
_isoc99_sscanf(v7, "%*s %d %s %d %d", v8 - 56);
v7 = strtok(0LL, "\n");
v8 += 64LL;
}
printf("\nWir sind Spieler: %s\n", (const char *)(a2 + 156));
printf("\nInsgesamt spielen diese %i Spieler:\n", *(unsigned int *)(a2 + 108));
printf("\n%-15s %-25s %-2s\n", "Spielernummer", "Spielername", "Bereit");
printf("%-15d %-25s %-2d\n", *(unsigned int *)(a2 + 152), (const char *)(a2 + 156), 1LL);
if ( *(int *)(a2 + 108) > 1 )
{
v10 = a2 + 220;
v11 = 1;
do
{
printf("%-15d %-25s %-2d\n", *(unsigned int *)(v10 - 4), (const char *)v10, *(unsigned int *)(v10 + 52));
puts("=================================================");
++v11;
v10 += 64LL;
}
while ( *(_DWORD *)(a2 + 108) > v11 );
}
putchar(10);
v12 = strtok(0LL, &byte_A03);
result = strcpy(dest, v12);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(char *dest, __int64 a2)
{
char *v3; // rbx
char *v4; // rax
__int64 v5; // rsi
void *v6; // r13
char *v7; // rax
__int64 v8; // rbx
char *result; // rax
__int64 v10; // rbx
int v11; // er13
char *v12; // rax

v3 = (char *)malloc(0x100uLL);
addchar(v3);
strcpy(v3, dest);
v4 = strtok(v3, "\n");
_isoc99_sscanf(v4, "%*s %*s %i", a2 + 108);
v5 = *(unsigned int *)(a2 + 108);
if ( (int)v5 > 8 )
{
printf("\nDiese Version des Clients unterstuetzt maximal 8 Spieler, die Anzahl ist jedoch %d Spieler!\n", v5);
result = 0LL;
}
else
{
v6 = malloc(0x32uLL);
addchar(v6);
v7 = strtok(0LL, "\n");
v8 = a2 + 272;
while ( strcmp(v7, "+ ENDPLAYERS") )
{
_isoc99_sscanf(v7, "%*s %d %s %d %d", v8 - 56);
v7 = strtok(0LL, "\n");
v8 += 64LL;
}
printf("\nWir sind Spieler: %s\n", (const char *)(a2 + 156));
printf("\nInsgesamt spielen diese %i Spieler:\n", *(unsigned int *)(a2 + 108));
printf("\n%-15s %-25s %-2s\n", "Spielernummer", "Spielername", "Bereit");
printf("%-15d %-25s %-2d\n", *(unsigned int *)(a2 + 152), (const char *)(a2 + 156), 1LL);
if ( *(int *)(a2 + 108) > 1 )
{
v10 = a2 + 220;
v11 = 1;
do
{
printf("%-15d %-25s %-2d\n", *(unsigned int *)(v10 - 4), (const char *)v10, *(unsigned int *)(v10 + 52));
puts("=================================================");
++v11;
v10 += 64LL;
}
while ( *(_DWORD *)(a2 + 108) > v11 );
}
putchar(10);
v12 = strtok(0LL, &byte_A03);
result = strcpy(dest, v12);
}
return result;
}

original_code:
char* recvPlayerInfo(char* buffer, sharedmem * shm) {
 char* buffer2;
 buffer2 = malloc(sizeof(char) * 256);
 addchar(buffer2);
 int i = 1;
 strcpy(buffer2, buffer);
 buffer2 = strtok(buffer2, "\n");
 sscanf(buffer2, "%*s %*s %i", &(shm->playerCount));
 if (shm->playerCount > 8) {
  printf("\nDiese Version des Clients unterstuetzt maximal 8 Spieler, die Anzahl ist jedoch %d Spieler!\n", shm->playerCount);
  return ((void *)0) ;
 }
 int rdy = -1;
 char* temp = malloc(sizeof(char) * 50);
 addchar(temp);
 buffer2 = strtok(((void *)0), "\n");
 while (strcmp(buffer2, "+ ENDPLAYERS") != 0) {
  sscanf(buffer2, "%*s %d %s %d %d", &shm->player[i].playerNumber, shm->player[i].playerName, &shm->player[i].playerReady, &rdy);
  if (rdy != -1) {
   snprintf(temp, 50, " %d", shm->player[i].playerReady);
   strcpy(shm->player[i].playerName, strcat(shm->player[i].playerName, temp));
   shm->player[i].playerReady = rdy;
  }
  buffer2 = strtok(((void *)0), "\n");
  i++;
 }
 printf("\nWir sind Spieler: %s\n", shm->player[0].playerName);
 printf("\nInsgesamt spielen diese %i Spieler:\n", shm->playerCount);
 printf("\n%-15s %-25s %-2s\n", "Spielernummer", "Spielername", "Bereit");
 printf("%-15d %-25s %-2d\n", shm->player[0].playerNumber, shm->player[0].playerName, 1);
 for (i = 1; i < shm->playerCount; i++) {
  printf("%-15d %-25s %-2d\n", shm->player[i].playerNumber, shm->player[i].playerName, shm->player[i].playerReady);
  printf("=================================================\n");
 }
 printf("\n");
 buffer2 = strtok(((void *)0), "");
 strcpy(buffer, buffer2);
 return buffer;
}

canonical_original_code:
char *recvPlayerInfo(char *buffer, struct sharedmem *shm) {
 char *buffer2;
 buffer2 = malloc(sizeof(char) * 256);
 addchar(buffer2);
 int i = 1;
 strcpy(buffer2, buffer);
 buffer2 = strtok(buffer2, "\n");
 sscanf(buffer2, "%*s %*s %i", &(shm->playerCount));
 if (shm->playerCount > 8) {
  printf("\nDiese Version des Clients unterstuetzt maximal 8 Spieler, die Anzahl ist jedoch %d Spieler!\n", shm->playerCount);
  return ((void *)0) ;
 }
 int rdy = -1;
 char *temp = malloc(sizeof(char) * 50);
 addchar(temp);
 buffer2 = strtok(((void *)0), "\n");
 while (strcmp(buffer2, "+ ENDPLAYERS") != 0) {
  sscanf(buffer2, "%*s %d %s %d %d", &shm->player[i].playerNumber, shm->player[i].playerName, &shm->player[i].playerReady, &rdy);
  if (rdy != -1) {
   snprintf(temp, 50, " %d", shm->player[i].playerReady);
   strcpy(shm->player[i].playerName, strcat(shm->player[i].playerName, temp));
   shm->player[i].playerReady = rdy;
  }
  buffer2 = strtok(((void *)0), "\n");
  i++;
 }
 printf("\nWir sind Spieler: %s\n", shm->player[0].playerName);
 printf("\nInsgesamt spielen diese %i Spieler:\n", shm->playerCount);
 printf("\n%-15s %-25s %-2s\n", "Spielernummer", "Spielername", "Bereit");
 printf("%-15d %-25s %-2d\n", shm->player[0].playerNumber, shm->player[0].playerName, 1);
 for (i = 1; i < shm->playerCount; i++) {
  printf("%-15d %-25s %-2d\n", shm->player[i].playerNumber, shm->player[i].playerName, shm->player[i].playerReady);
  printf("=================================================\n");
 }
 printf("\n");
 buffer2 = strtok(((void *)0), "");
 strcpy(buffer, buffer2);
 return buffer;
}

variable_types:
buffer
char *

variable_types: struct sharedmem { int pidDad; int pidKid; char gameID[42]; int sock; int *pf; struct player { int playerNumber; char playerName[50]; int playerReady; int playerRegisterd; } player[8]; } *

variable_types: rdy int

return_type: char *


performConnection

decompiled_code:
_BOOL8 __fastcall performConnection(int fd, __int64 a2, __int64 a3, __int64 a4)
{
const char *v4; // r13
char *v5; // r12
_BYTE *v6; // rbx
int v7; // er15
int v8; // er15
int v9; // er13
int v10; // er12
int v11; // er12
char *v12; // rdx
_BOOL8 result; // rax
int v15; // [rsp+10h] [rbp-48h]

v4 = (const char *)malloc(0x14uLL);
addchar(v4);
v5 = (char *)malloc(0x200uLL);
addchar(v5);
v6 = malloc(0x200uLL);
addchar(v6);
v7 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:120");
_isoc99_sscanf(v6, "%*s%*s%*s%s", v4);
if ( v7 > 0 )
v6[v7] = 0;
printf("\nDie Version des Servers ist: %s\n", v4);
antistrcat((char *)(a3 + 120), "VERSION ", v5);
sendReplyFormatted(fd, v5);
v15 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:130");
if ( v15 > 0 )
v6[v15] = 0;
if ( *v6 == 45 )
{
printf("\nDer Server akzeptiert die Version %s dieses Clients nicht!\n", (const char *)(a3 + 120));
result = 1LL;
}
else
{
puts("\nDie Client-Version wurde akzeptiert, uebertrage Spiel-ID...");
antistrcat((char *)(a2 + 8), "ID ", v5);
sendReplyFormatted(fd, v5);
v8 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:147");
if ( v8 > 0 )
v6[v8] = 0;
_isoc99_sscanf(v6, "%*s%*s%s", v4);
if ( *v6 == 45 )
{
puts("\nDie uebergebene Game-ID fehlt oder ist fehlerhaft! Beende Verbindung");
result = 1LL;
}
else if ( !strcmp(v4, "Quarto") )
{
printf("\nDer Server moechte %s spielen. Und wir auch!\n", v4);
v9 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:167");
if ( v9 > 0 )
v6[v9] = 0;
_isoc99_sscanf(v6, "%*s%s", a2 + 50);
printf("\nSpiel: %s\n", (const char *)(a2 + 50));
antistrcat((char *)(a3 + 125), "PLAYER ", v5);
sendReplyFormatted(fd, v5);
v10 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:177");
if ( v10 > 0 )
v6[v10] = 0;
if ( *v6 == 45 )
{
if ( v6[2] == 45 )
puts("\nEs wurde eine ungueltige Spielernummer eingegeben! Beende Verbindung");
else
puts("\nEs wurde kein freier Platz gefunden, versuchen sie es spaeter noch einmal!");
result = 1LL;
}
else
{
_isoc99_sscanf(v6, "%*s %*s %d %[^\n]\n", a2 + 152);
v11 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:199");
if ( v11 > 0 )
v6[v11] = 0;
if ( *v6 == 45 )
{
puts("\nFehler bei Uebermittlung der Spielparameter!");
result = 1LL;
}
else
{
v12 = recvPlayerInfo(v6, a2);
result = 1LL;
if ( v12 )
result = (unsigned int)waitforfds((unsigned int)fd, v6, a2, a4) != 0;
}
}
}
else
{
puts("\nDas Spiel, das der Server spielt, ist nicht Quarto! Beende Verbindung.");
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int fd, __int64 a2, __int64 a3, __int64 a4)
{
const char *v4; // r13
char *v5; // r12
_BYTE *v6; // rbx
int v7; // er15
int v8; // er15
int v9; // er13
int v10; // er12
int v11; // er12
char *v12; // rdx
_BOOL8 result; // rax
int v15; // [rsp+10h] [rbp-48h]

v4 = (const char *)malloc(0x14uLL);
addchar(v4);
v5 = (char *)malloc(0x200uLL);
addchar(v5);
v6 = malloc(0x200uLL);
addchar(v6);
v7 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:120");
_isoc99_sscanf(v6, "%*s%*s%*s%s", v4);
if ( v7 > 0 )
v6[v7] = 0;
printf("\nDie Version des Servers ist: %s\n", v4);
func1((char *)(a3 + 120), "VERSION ", v5);
func0(fd, v5);
v15 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:130");
if ( v15 > 0 )
v6[v15] = 0;
if ( *v6 == 45 )
{
printf("\nDer Server akzeptiert die Version %s dieses Clients nicht!\n", (const char *)(a3 + 120));
result = 1LL;
}
else
{
puts("\nDie Client-Version wurde akzeptiert, uebertrage Spiel-ID...");
func1((char *)(a2 + 8), "ID ", v5);
func0(fd, v5);
v8 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:147");
if ( v8 > 0 )
v6[v8] = 0;
_isoc99_sscanf(v6, "%*s%*s%s", v4);
if ( *v6 == 45 )
{
puts("\nDie uebergebene Game-ID fehlt oder ist fehlerhaft! Beende Verbindung");
result = 1LL;
}
else if ( !strcmp(v4, "Quarto") )
{
printf("\nDer Server moechte %s spielen. Und wir auch!\n", v4);
v9 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:167");
if ( v9 > 0 )
v6[v9] = 0;
_isoc99_sscanf(v6, "%*s%s", a2 + 50);
printf("\nSpiel: %s\n", (const char *)(a2 + 50));
func1((char *)(a3 + 125), "PLAYER ", v5);
func0(fd, v5);
v10 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:177");
if ( v10 > 0 )
v6[v10] = 0;
if ( *v6 == 45 )
{
if ( v6[2] == 45 )
puts("\nEs wurde eine ungueltige Spielernummer eingegeben! Beende Verbindung");
else
puts("\nEs wurde kein freier Platz gefunden, versuchen sie es spaeter noch einmal!");
result = 1LL;
}
else
{
_isoc99_sscanf(v6, "%*s %*s %d %[^\n]\n", a2 + 152);
v11 = recv(fd, v6, 0x1FFuLL, 0);
writelog(logdatei, "src/PerformConnection.c:199");
if ( v11 > 0 )
v6[v11] = 0;
if ( *v6 == 45 )
{
puts("\nFehler bei Uebermittlung der Spielparameter!");
result = 1LL;
}
else
{
v12 = func2(v6, a2);
result = 1LL;
if ( v12 )
result = (unsigned int)waitforfds((unsigned int)fd, v6, a2, a4) != 0;
}
}
}
else
{
puts("\nDas Spiel, das der Server spielt, ist nicht Quarto! Beende Verbindung.");
result = 1LL;
}
}
return result;
}

original_code:
int performConnection(int sock, sharedmem * shm, config_struct* conf, int fd[]) {
 int err;
 char* reader = malloc(sizeof(char) * 20);
 addchar(reader);
 char* temp = malloc(sizeof(char) * 512);
 addchar(temp);
 char* buffer = malloc(sizeof(char) * 512);
 addchar(buffer);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "120");
 sscanf(buffer, "%*s%*s%*s%s", reader);
 if (err > 0) {
  buffer[err] = '\0';
 }
 printf("\nDie Version des Servers ist: %s\n", reader);
 antistrcat(conf->version, "VERSION ", temp);
 sendReplyFormatted(sock, temp);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "130");
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nDer Server akzeptiert die Version %s dieses Clients nicht!\n", conf->version);
  return 1;
 } else {
  printf("\nDie Client-Version wurde akzeptiert, uebertrage Spiel-ID...\n");
 }
 antistrcat(shm->gameID, "ID ", temp);
 sendReplyFormatted(sock, temp);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "147");
 if (err > 0) {
  buffer[err] = '\0';
 }
 sscanf(buffer, "%*s%*s%s", reader);
 if (buffer[0] == '-') {
  printf("\nDie uebergebene Game-ID fehlt oder ist fehlerhaft! Beende Verbindung\n");
  return 1;
 } else if (strcmp(reader, "Quarto") != 0) {
  printf("\nDas Spiel, das der Server spielt, ist nicht Quarto! Beende Verbindung.\n");
  return 1;
 } else {
  printf("\nDer Server moechte %s spielen. Und wir auch!\n", reader);
 }
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "167");
 if (err > 0) {
  buffer[err] = '\0';
 }
 sscanf(buffer, "%*s%s", shm->gameName);
 printf("\nSpiel: %s\n", shm->gameName);
 antistrcat(conf->playernumber, "PLAYER ", temp);
 sendReplyFormatted(sock, temp);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "177");
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  if (buffer[2] == '-') {
   printf("\nEs wurde eine ungueltige Spielernummer eingegeben! Beende Verbindung\n");
  } else {
   printf("\nEs wurde kein freier Platz gefunden, versuchen sie es spaeter noch einmal!\n");
  }
  return 1;
 } else {
  sscanf(buffer, "%*s %*s %d %[^\n]\n", &(shm->player[0].playerNumber),
    (shm->player[0].playerName));
 }
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "199");
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nFehler bei Uebermittlung der Spielparameter!\n");
  return 1;
 }
 if (recvPlayerInfo(buffer, shm) == ((void *)0) ) {
  return 1;
 }
 if (waitforfds(sock, buffer, shm, fd) != 0) {
  return 1;
 }
 return 0;
}

canonical_original_code:
int performConnection(int sock, struct sharedmem *shm, struct config_struct *conf, int fd[]) {
 int err;
 char *reader = malloc(sizeof(char) * 20);
 addchar(reader);
 char *temp = malloc(sizeof(char) * 512);
 addchar(temp);
 char *buffer = malloc(sizeof(char) * 512);
 addchar(buffer);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "120");
 sscanf(buffer, "%*s%*s%*s%s", reader);
 if (err > 0) {
  buffer[err] = '\0';
 }
 printf("\nDie Version des Servers ist: %s\n", reader);
 antistrcat(conf->version, "VERSION ", temp);
 sendReplyFormatted(sock, temp);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "130");
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nDer Server akzeptiert die Version %s dieses Clients nicht!\n", conf->version);
  return 1;
 } else {
  printf("\nDie Client-Version wurde akzeptiert, uebertrage Spiel-ID...\n");
 }
 antistrcat(shm->gameID, "ID ", temp);
 sendReplyFormatted(sock, temp);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "147");
 if (err > 0) {
  buffer[err] = '\0';
 }
 sscanf(buffer, "%*s%*s%s", reader);
 if (buffer[0] == '-') {
  printf("\nDie uebergebene Game-ID fehlt oder ist fehlerhaft! Beende Verbindung\n");
  return 1;
 } else if (strcmp(reader, "Quarto") != 0) {
  printf("\nDas Spiel, das der Server spielt, ist nicht Quarto! Beende Verbindung.\n");
  return 1;
 } else {
  printf("\nDer Server moechte %s spielen. Und wir auch!\n", reader);
 }
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "167");
 if (err > 0) {
  buffer[err] = '\0';
 }
 sscanf(buffer, "%*s%s", shm->gameName);
 printf("\nSpiel: %s\n", shm->gameName);
 antistrcat(conf->playernumber, "PLAYER ", temp);
 sendReplyFormatted(sock, temp);
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "177");
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  if (buffer[2] == '-') {
   printf("\nEs wurde eine ungueltige Spielernummer eingegeben! Beende Verbindung\n");
  } else {
   printf("\nEs wurde kein freier Platz gefunden, versuchen sie es spaeter noch einmal!\n");
  }
  return 1;
 } else {
  sscanf(buffer, "%*s %*s %d %[^\n]\n", &(shm->player[0].playerNumber),
    (shm->player[0].playerName));
 }
 err = recv(sock, buffer, 512 - 1, 0);
 writelog(logdatei, "src/PerformConnection.c" ":" "199");
 if (err > 0) {
  buffer[err] = '\0';
 }
 if (buffer[0] == '-') {
  printf("\nFehler bei Uebermittlung der Spielparameter!\n");
  return 1;
 }
 if (recvPlayerInfo(buffer, shm) == ((void *)0) ) {
  return 1;
 }
 if (waitforfds(sock, buffer, shm, fd) != 0) {
  return 1;
 }
 return 0;
}

variable_types: struct config_struct { char hostname[100]; char gamekindname[20]; char version[5]; char playernumber[5]; int portnumber; } *

variable_types:
fd
int []

variable_types:
reader
char *


freeall

decompiled_code:
__int64 freeall()
{
__int64 v0; // rbx
__int64 v1; // rbx

if ( countint >= 0 )
{
v0 = 0LL;
do
free((void *)varadr_int[v0++]);
while ( countint >= (int)v0 );
}
if ( countchar >= 0 )
{
v1 = 0LL;
do
free((void *)varadr_char[v1++]);
while ( countchar >= (int)v1 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx
__int64 v1; // rbx

if ( countint >= 0 )
{
v0 = 0LL;
do
free((void *)varadr_int[v0++]);
while ( countint >= (int)v0 );
}
if ( countchar >= 0 )
{
v1 = 0LL;
do
free((void *)varadr_char[v1++]);
while ( countchar >= (int)v1 );
}
return 0LL;
}

original_code:
int freeall() {
 int i;
 for (i = 0; i <= countint; i++) {
  free(varadr_int[i]);
 }
 for (i = 0; i <= countchar; i++) {
  free(varadr_char[i]);
 }
 return 0;
}

variable_types: i int


addint

decompiled_code:
__int64 __fastcall addint(__int64 a1)
{
int v1; // eax

if ( !a1 )
{
perror("\nFehler bei Malloc eines int pointers! \n");
freeall();
exit(1);
}
v1 = countint;
varadr_int[countint] = a1;
countint = v1 + 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
int v1; // eax

if ( !a1 )
{
perror("\nFehler bei Malloc eines int pointers! \n");
func0();
exit(1);
}
v1 = countint;
varadr_int[countint] = a1;
countint = v1 + 1;
return 0LL;
}

original_code:
int addint(int* intadr) {
 if (intadr == ((void *)0) ) {
  perror("\nFehler bei Malloc eines int pointers! \n");
  freeall();
  exit(1);
 }
 varadr_int[countint] = intadr;
 countint++;
 return 0;
}

canonical_original_code:
int addint(int *intadr) {
 if (intadr == ((void *)0) ) {
  perror("\nFehler bei Malloc eines int pointers! \n");
  freeall();
  exit(1);
 }
 varadr_int[countint] = intadr;
 countint++;
 return 0;
}

variable_types:
intadr
int *


addchar

decompiled_code:
__int64 __fastcall addchar(__int64 a1)
{
int v1; // eax

if ( !a1 )
{
perror("\nFehler bei Malloc eines char pointers! \n");
freeall();
exit(1);
}
v1 = countchar;
varadr_char[countchar] = a1;
countchar = v1 + 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // eax

if ( !a1 )
{
perror("\nFehler bei Malloc eines char pointers! \n");
func0();
exit(1);
}
v1 = countchar;
varadr_char[countchar] = a1;
countchar = v1 + 1;
return 0LL;
}

original_code:
int addchar(char* charadr) {
 if (charadr == ((void *)0) ) {
  perror("\nFehler bei Malloc eines char pointers! \n");
  freeall();
  exit(1);
 }
 varadr_char[countchar] = charadr;
 countchar++;
 return 0;
}

canonical_original_code:
int addchar(char *charadr) {
 if (charadr == ((void *)0) ) {
  perror("\nFehler bei Malloc eines char pointers! \n");
  freeall();
  exit(1);
 }
 varadr_char[countchar] = charadr;
 countchar++;
 return 0;
}

variable_types:
charadr
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
_DWORD *v4; // rax
__pid_t v5; // eax
int stat_loc; // [rsp+Ch] [rbp-7Ch] BYREF
void *ptr; // [rsp+10h] [rbp-78h] BYREF
void *shmaddr; // [rsp+18h] [rbp-70h]
int shmid; // [rsp+20h] [rbp-68h]
int pipedes[2]; // [rsp+24h] [rbp-64h] BYREF
__int16 v12; // [rsp+2Ch] [rbp-5Ch] BYREF
unsigned int v13; // [rsp+2Eh] [rbp-5Ah]
__int16 v14; // [rsp+32h] [rbp-56h]
void **v15; // [rsp+34h] [rbp-54h]
int v16; // [rsp+3Ch] [rbp-4Ch]
__int16 v17; // [rsp+48h] [rbp-40h] BYREF
int v18; // [rsp+4Ah] [rbp-3Eh]
__int16 v19; // [rsp+4Eh] [rbp-3Ah]
void **v20; // [rsp+50h] [rbp-38h]
int v21; // [rsp+58h] [rbp-30h]
char *v22; // [rsp+68h] [rbp-20h]
char v23; // [rsp+90h] [rbp+8h] BYREF

v22 = &v23;
v17 = -17599;
v18 = (int)signal_handler_1;
v19 = -17847;
v20 = &ptr;
v21 = -1864106167;
v12 = -17599;
v13 = (unsigned int)signal_handler_0;
v14 = -17847;
v15 = &ptr;
v16 = -1864106167;
logdatei = fopen("log.txt", "w+");
ptr = calloc(5uLL, 0x88uLL);
v3 = shmget(0, 0x298uLL, 2045);
shmid = v3;
if ( v3 <= 0 )
{
perror("\nNo SHM\n");
free(ptr);
fclose(logdatei);
return 1;
}
v4 = shmat(v3, 0LL, 0);
shmaddr = v4;
if ( v4 == (_DWORD *)-1LL )
{
perror("Fehler, shm");
writelog(logdatei, "src/Main.c:46");
return 1;
}
v4[35] = 0;
if ( pipe(pipedes) < 0 )
{
perror("\nFehler beim Einrichten der Pipe.\n");
return 1;
}
v5 = fork();
if ( v5 < 0 )
{
perror("\nFehler bei fork()");
writelog(logdatei, "src/Main.c:64");
shmdt(shmaddr);
fclose(logdatei);
close(pipedes[0]);
close(pipedes[1]);
return 1;
}
if ( !v5 )
{
close(pipedes[1]);
if ( signal(2, (__sighandler_t)&v17) == (__sighandler_t)-1LL )
{
perror("\nFehler beim aufsetzen des Signal Handlers!\n");
close(pipedes[0]);
free(ptr);
fclose(logdatei);
return 1;
}
if ( argc == 1 || strlen(argv[1]) > 0x29 )
{
puts("\nDer uebergebene Parameter hat nicht die korrekte Laenge!");
close(pipedes[0]);
free(ptr);
fclose(logdatei);
return 1;
}
if ( argc == 3 )
{
if ( !(unsigned int)openConfig(argv[2], ptr) )
goto LABEL_11;
}
else if ( !(unsigned int)openConfig("client.conf", ptr) )
{
LABEL_11:
strcpy((char *)shmaddr + 8, argv[1]);
initConnection(shmaddr, ptr, pipedes);
freeall();
LABEL_28:
close(pipedes[0]);
close(pipedes[1]);
free(ptr);
fclose(logdatei);
return 0;
}
close(pipedes[0]);
free(ptr);
fclose(logdatei);
return 1;
}
close(pipedes[0]);
if ( signal(2, (__sighandler_t)&v12) != (__sighandler_t)-1LL
&& signal(10, (__sighandler_t)&v12) != (__sighandler_t)-1LL )
{
do
usleep(0xC350u);
while ( !waitpid(*((_DWORD *)shmaddr + 1), &stat_loc, 1) );
if ( *((_DWORD *)shmaddr + 35) )
{
shmdt(*((const void **)shmaddr + 18));
if ( shmctl(*((_DWORD *)shmaddr + 35), 0, 0LL) == -1 )
perror("\nParent: Fehler bei Zerstoerung von pf \n");
}
shmdt(shmaddr);
if ( shmctl(shmid, 0, 0LL) == -1 )
perror("\nParent: Fehler bei Zerstoerung von shm \n");
freeall();
goto LABEL_28;
}
perror("\nFehler beim Aufsetzen des Signal Handlers\n");
close(pipedes[1]);
free(ptr);
fclose(logdatei);
return 1;
}

original_code:
int main(int argc, char** argv) {
 config_struct *conf;
 int fd[2];
 logdatei = fopen("log.txt", "w+");
 conf = calloc(5, sizeof(config_struct));
 sharedmem *shm;
 int shmID;
 int shmSize = sizeof(struct sharedmem);
 shmID = shmget(((__key_t) 0), shmSize, 01000 | 02000 | 0775);
 if (shmID < 1) {
  perror("\nNo SHM\n");
  free(conf);
  fclose(logdatei);
  return 1;
 }
 shm = shmat(shmID, 0, 0);
 if (shm == (void *) -1) {
  perror("Fehler, shm");
  writelog(logdatei, "src/Main.c" ":" "46");
  return 1;
 }
 shm->pfID = 0;
 if (pipe(fd) < 0) {
  perror("\nFehler beim Einrichten der Pipe.\n");
  return 1;
 }
 pid_t pid = 0;
 pid = fork();
 if ((pid) < 0) {
  perror("\nFehler bei fork()");
  writelog(logdatei, "src/Main.c" ":" "64");
  shmdt(shm);
  fclose(logdatei);
  close(fd[0]);
  close(fd[1]);
  return 1;
 } else if (pid == 0) {
  close(fd[1]);
  void signal_handler(int signum) {
   if (signum == 2) {
    reactToSig(shm, 1, conf, fd, shmID);
   }
  }
  if (signal(2, signal_handler) == ((__sighandler_t) -1) ) {
   perror("\nFehler beim aufsetzen des Signal Handlers!\n");
   close(fd[0]);
   free(conf);
   fclose(logdatei);
   return 1;
  }
  if (argc == 1 || (strlen(argv[1])) >= 42) {
   printf("\nDer uebergebene Parameter hat nicht die korrekte Laenge!\n");
   close(fd[0]);
   free(conf);
   fclose(logdatei);
   return 1;
  } else {
   if (argc == 3) {
    if (openConfig(argv[2], conf) != 0) {
     close(fd[0]);
     free(conf);
     fclose(logdatei);
     return 1;
    }
   } else {
    if (openConfig("client.conf", conf) != 0) {
     close(fd[0]);
     free(conf);
     fclose(logdatei);
     return 1;
    }
   }
   strcpy(shm->gameID, argv[1]);
  }
  initConnection(shm, conf, fd);
  freeall();
 } else {
  close(fd[0]);
  void signal_handler(int signum) {
   if (signum == 10) {
    reactToSig(shm, 0, conf, fd, shmID);
   }
   else if (signum == 2) {
    reactToSig(shm, 1, conf, fd, shmID);
   }
  }
  if (signal(2, signal_handler) == ((__sighandler_t) -1)
    || signal(10, signal_handler) == ((__sighandler_t) -1) ) {
   perror("\nFehler beim Aufsetzen des Signal Handlers\n");
   close(fd[1]);
   free(conf);
   fclose(logdatei);
   return 1;
  }
  int status;
  pid_t result;
  do {
   usleep(50000);
   result = waitpid(shm->pidKid, &status, 1);
  } while (result == 0);
  if (shm->pfID != 0) {
   shmdt(shm->pf);
   if (shmctl(shm->pfID, 0, ((void *)0) ) == -1) {
    perror("\nParent: Fehler bei Zerstoerung von pf \n");
   }
  }
  shmdt(shm);
  if (shmctl(shmID, 0, ((void *)0) ) == -1) {
   perror("\nParent: Fehler bei Zerstoerung von shm \n");
  }
  freeall();
 }
 close(fd[0]);
 close(fd[1]);
 free(conf);
 fclose(logdatei);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 struct config_struct *conf;
 int fd[2];
 logdatei = fopen("log.txt", "w+");
 conf = calloc(5, sizeof(config_struct));
 struct sharedmem *shm;
 int shmID;
 int shmSize = sizeof(struct sharedmem);
 shmID = shmget(((int) 0), shmSize, 01000 | 02000 | 0775);
 if (shmID < 1) {
  perror("\nNo SHM\n");
  free(conf);
  fclose(logdatei);
  return 1;
 }
 shm = shmat(shmID, 0, 0);
 if (shm == (void *) -1) {
  perror("Fehler, shm");
  writelog(logdatei, "src/Main.c" ":" "46");
  return 1;
 }
 shm->pfID = 0;
 if (pipe(fd) < 0) {
  perror("\nFehler beim Einrichten der Pipe.\n");
  return 1;
 }
 int pid = 0;
 pid = fork();
 if ((pid) < 0) {
  perror("\nFehler bei fork()");
  writelog(logdatei, "src/Main.c" ":" "64");
  shmdt(shm);
  fclose(logdatei);
  close(fd[0]);
  close(fd[1]);
  return 1;
 } else if (pid == 0) {
  close(fd[1]);
  void signal_handler(int signum) {
   if (signum == 2) {
    reactToSig(shm, 1, conf, fd, shmID);
   }
  }
  if (signal(2, signal_handler) == ((__sighandler_t) -1) ) {
   perror("\nFehler beim aufsetzen des Signal Handlers!\n");
   close(fd[0]);
   free(conf);
   fclose(logdatei);
   return 1;
  }
  if (argc == 1 || (strlen(argv[1])) >= 42) {
   printf("\nDer uebergebene Parameter hat nicht die korrekte Laenge!\n");
   close(fd[0]);
   free(conf);
   fclose(logdatei);
   return 1;
  } else {
   if (argc == 3) {
    if (openConfig(argv[2], conf) != 0) {
     close(fd[0]);
     free(conf);
     fclose(logdatei);
     return 1;
    }
   } else {
    if (openConfig("client.conf", conf) != 0) {
     close(fd[0]);
     free(conf);
     fclose(logdatei);
     return 1;
    }
   }
   strcpy(shm->gameID, argv[1]);
  }
  initConnection(shm, conf, fd);
  freeall();
 } else {
  close(fd[0]);
  void signal_handler(int signum) {
   if (signum == 10) {
    reactToSig(shm, 0, conf, fd, shmID);
   }
   else if (signum == 2) {
    reactToSig(shm, 1, conf, fd, shmID);
   }
  }
  if (signal(2, signal_handler) == ((__sighandler_t) -1)
    || signal(10, signal_handler) == ((__sighandler_t) -1) ) {
   perror("\nFehler beim Aufsetzen des Signal Handlers\n");
   close(fd[1]);
   free(conf);
   fclose(logdatei);
   return 1;
  }
  int status;
  int result;
  do {
   usleep(50000);
   result = waitpid(shm->pidKid, &status, 1);
  } while (result == 0);
  if (shm->pfID != 0) {
   shmdt(shm->pf);
   if (shmctl(shm->pfID, 0, ((void *)0) ) == -1) {
    perror("\nParent: Fehler bei Zerstoerung von pf \n");
   }
  }
  shmdt(shm);
  if (shmctl(shmID, 0, ((void *)0) ) == -1) {
   perror("\nParent: Fehler bei Zerstoerung von shm \n");
  }
  freeall();
 }
 close(fd[0]);
 close(fd[1]);
 free(conf);
 fclose(logdatei);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
fd
int [2]

variable_types: shmID int

variable_types: shmSize int

variable_types: pid int

variable_types: status int

variable_types: result int

user_defined_types: struct config_struct { char hostname[100]; char gamekindname[20]; char version[5]; char playernumber[5]; int portnumber; }


formatMove

decompiled_code:
void *__fastcall formatMove(int a1)
{
void *result; // rax

result = &unk_E1B;
if ( a1 )
{
result = &unk_E1E;
if ( a1 != 1 )
{
result = &unk_E21;
if ( a1 != 2 )
{
result = &unk_E24;
if ( a1 != 3 )
{
result = &unk_E27;
if ( a1 != 4 )
{
result = &unk_E2A;
if ( a1 != 5 )
{
result = &unk_E2D;
if ( a1 != 6 )
{
result = &unk_E30;
if ( a1 != 7 )
{
result = &unk_E33;
if ( a1 != 8 )
{
result = &unk_E36;
if ( a1 != 9 )
{
result = &unk_E39;
if ( a1 != 10 )
{
result = &unk_E3C;
if ( a1 != 11 )
{
result = &unk_E3F;
if ( a1 != 12 )
{
result = &unk_E18;
if ( a1 != 13 )
{
result = &unk_E42;
if ( a1 != 14 )
{
result = &unk_E45;
if ( a1 != 15 )
result = 0LL;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
void *result; // rax

result = &unk_E1B;
if ( a1 )
{
result = &unk_E1E;
if ( a1 != 1 )
{
result = &unk_E21;
if ( a1 != 2 )
{
result = &unk_E24;
if ( a1 != 3 )
{
result = &unk_E27;
if ( a1 != 4 )
{
result = &unk_E2A;
if ( a1 != 5 )
{
result = &unk_E2D;
if ( a1 != 6 )
{
result = &unk_E30;
if ( a1 != 7 )
{
result = &unk_E33;
if ( a1 != 8 )
{
result = &unk_E36;
if ( a1 != 9 )
{
result = &unk_E39;
if ( a1 != 10 )
{
result = &unk_E3C;
if ( a1 != 11 )
{
result = &unk_E3F;
if ( a1 != 12 )
{
result = &unk_E18;
if ( a1 != 13 )
{
result = &unk_E42;
if ( a1 != 14 )
{
result = &unk_E45;
if ( a1 != 15 )
result = 0LL;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

original_code:
char* formatMove(int move) {
 if (move == 0)
  { return "A4"; }
 if (move == 1)
  { return "B4"; }
 if (move == 2)
  { return "C4"; }
 if (move == 3)
  { return "D4"; }
 if (move == 4)
  { return "A3"; }
 if (move == 5)
  { return "B3"; }
 if (move == 6)
  { return "C3"; }
 if (move == 7)
  { return "D3"; }
 if (move == 8)
  { return "A2"; }
 if (move == 9)
  { return "B2"; }
 if (move == 10)
  { return "C2"; }
 if (move == 11)
  { return "D2"; }
 if (move == 12)
  { return "A1"; }
 if (move == 13)
  { return "B1"; }
 if (move == 14)
  { return "C1"; }
 if (move == 15)
  { return "D1"; }
 return ((void *)0) ;
}

canonical_original_code:
char *formatMove(int move) {
 if (move == 0)
  { return "A4"; }
 if (move == 1)
  { return "B4"; }
 if (move == 2)
  { return "C4"; }
 if (move == 3)
  { return "D4"; }
 if (move == 4)
  { return "A3"; }
 if (move == 5)
  { return "B3"; }
 if (move == 6)
  { return "C3"; }
 if (move == 7)
  { return "D3"; }
 if (move == 8)
  { return "A2"; }
 if (move == 9)
  { return "B2"; }
 if (move == 10)
  { return "C2"; }
 if (move == 11)
  { return "D2"; }
 if (move == 12)
  { return "A1"; }
 if (move == 13)
  { return "B1"; }
 if (move == 14)
  { return "C1"; }
 if (move == 15)
  { return "D1"; }
 return ((void *)0) ;
}

variable_types: move int


testStone

decompiled_code:
__int64 __fastcall testStone(__int64 a1, int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // rdx

v2 = *(_DWORD **)(a1 + 144);
v3 = v2 + 16;
while ( 1 )
{
if ( *v2 == a2 )
return 1LL;
if ( *(_DWORD *)(a1 + 132) == a2 )
break;
if ( ++v2 == v3 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // rdx

v2 = *(_DWORD **)(a1 + 144);
v3 = v2 + 16;
while ( 1 )
{
if ( *v2 == a2 )
return 1LL;
if ( *(_DWORD *)(a1 + 132) == a2 )
break;
if ( ++v2 == v3 )
return 0LL;
}
return 1LL;
}

original_code:
int testStone(sharedmem * shm, int stone) {
 int i = 0;
 for (i = 0; i < 16; i++) {
  if (stone == *(shm->pf + i) || stone == shm->StoneToPlace) {
   return 1;
  }
 }
 return 0;
}

canonical_original_code:
int testStone(struct sharedmem *shm, int stone) {
 int i = 0;
 for (i = 0; i < 16; i++) {
  if (stone == *(shm->pf + i) || stone == shm->StoneToPlace) {
   return 1;
  }
 }
 return 0;
}

variable_types: struct sharedmem { int pidDad; int pidKid; char gameID[42]; int sock; int *pf; struct player { int playerNumber; char playerName[50]; int playerReady; int playerRegisterd; } player[8]; } *

variable_types: stone int


chooseStone

decompiled_code:
__int64 __fastcall chooseStone(__int64 a1)
{
int v1; // ebx
__int64 result; // rax

do
{
v1 = rand() % 16;
result = testStone(a1, v1);
}
while ( (_DWORD)result );
*(_DWORD *)(a1 + 136) = v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // ebx
__int64 result; // rax

do
{
v1 = rand() % 16;
result = func1(a1, v1);
}
while ( (_DWORD)result );
*(_DWORD *)(a1 + 136) = v1;
return result;
}

original_code:
void chooseStone(sharedmem * shm) {
 int check = 0;
 int stone;
 while (check == 0) {
  stone = rand() % 16;
  if (testStone(shm, stone) == 0) {
   check = 1;
  }
 }
 shm->nextStone = stone;
}

canonical_original_code:
void chooseStone(struct sharedmem *shm) {
 int check = 0;
 int stone;
 while (check == 0) {
  stone = rand() % 16;
  if (testStone(shm, stone) == 0) {
   check = 1;
  }
 }
 shm->nextStone = stone;
}

variable_types: check int


checkField

decompiled_code:
_BOOL8 __fastcall checkField(_BYTE *a1)
{
_BYTE *v1; // rcx
int v2; // eax

v1 = a1 + 64;
v2 = 0;
do
{
v2 += *a1 != 42;
a1 += 4;
}
while ( a1 != v1 );
return v2 == 15;
}

original_code:
int checkField(char * field) {
 int i;
 int occupied = 0;
 for (i = 0; i < 64; i = i + 4) {
  if (field[i] != '*') {
   occupied++;
  }
 }
 if (occupied == 15) {
  return 1;
 } else {
  return 0;
 }
}

canonical_original_code:
int checkField(char *field) {
 int i;
 int occupied = 0;
 for (i = 0; i < 64; i = i + 4) {
  if (field[i] != '*') {
   occupied++;
  }
 }
 if (occupied == 15) {
  return 1;
 } else {
  return 0;
 }
}

variable_types:
field
char *

variable_types: occupied int


decompiled_code:
__int64 __fastcall byte_to_binary(int a1, _BYTE *a2)
{
_BYTE *v2; // rdx
unsigned int i; // eax

if ( !a2 )
return 1LL;
v2 = a2;
for ( i = 3; i != -1; --i )
{
if ( _bittest(&a1, i) )
*v2 = 49;
else
*v2 = 48;
++v2;
}
a2[4] = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, _BYTE *a2)
{
_BYTE *v2; // rdx
unsigned int i; // eax

if ( !a2 )
return 1LL;
v2 = a2;
for ( i = 3; i != -1; --i )
{
if ( _bittest(&a1, i) )
*v2 = 49;
else
*v2 = 48;
++v2;
}
a2[4] = 0;
return 0LL;
}

original_code:
int byte_to_binary(int n, char* pointer) {
 int c, d, count;
 count = 0;
 if (pointer == ((void *)0) ) {
  return 1;
 }
 for (c = 3; c >= 0; c--) {
  d = n >> c;
  if (d & 1) {
   *(pointer + count) = 1 + '0';
  } else {
   *(pointer + count) = 0 + '0';
  }
  count++;
 }
 *(pointer + count) = '\0';
 return 0;
}

canonical_original_code:
int byte_to_binary(int n, char *pointer) {
 int c; int d; int count;
 count = 0;
 if (pointer == ((void *)0) ) {
  return 1;
 }
 for (c = 3; c >= 0; c--) {
  d = n >> c;
  if (d & 1) {
   *(pointer + count) = 1 + '0';
  } else {
   *(pointer + count) = 0 + '0';
  }
  count++;
 }
 *(pointer + count) = '\0';
 return 0;
}

variable_types: n int

variable_types:
pointer
char *

variable_types: c int

variable_types: d int

variable_types: count int


calculateMove

original_code:
int calculateMove(sharedmem *shm, char* stones, int silent) {
 char* stone = malloc(sizeof(char) * 5);
 addchar(stone);
 int i, j;
 byte_to_binary(shm->StoneToPlace, stone);
 for (i = 0; i < 64; i = i + 4) {
  if (stones[i] == '*') {
   for (j = 0; j < 4; j++) {
    if (((stone[j]) == (stones[((i + 4) % 16) + (i / 16) * 16 + j]))
      && ((stone[j]) == (stones[((i + 8) % 16) + (i / 16) * 16 + j]))
      && ((stone[j]) == (stones[((i + 12) % 16) + (i / 16) * 16 + j]))) {
     if (silent != 1) {
      printf("\nHorizontale Loesung gefunden (%s)! ", formatMove(i / 4));
     }
     return i;
    }
    if (((stone[j]) == (stones[(i + 16) % 64 + j]))
      && ((stone[j]) == (stones[(i + 32) % 64 + j]))
      && ((stone[j]) == (stones[(i + 48) % 64 + j]))) {
     if (silent != 1) {
      printf("\nVertikale Loesung gefunden (%s)! ", formatMove(i / 4));
     }
     return i;
    }
    if (i % 20 == 0) {
     if (((stone[j]) == (stones[(i + 20) % 80 + j]))
       && ((stone[j]) == (stones[(i + 40) % 80 + j]))
       && ((stone[j]) == (stones[(i + 60) % 80 + j]))) {
      if (silent != 1) {
       printf("\nDiagonale Loesung gefunden! %s\n", formatMove(i / 4));
      }
      return i;
     }
    }
    if ((i != 0) && (i % 12) == 0) {
     if (((stone[j]) == (stones[i % 48 + 12 + j]))
       && ((stone[j]) == (stones[(i + 12) % 48 + 12 + j]))
       && ((stone[j]) == (stones[(i + 24) % 48 + 12 + j]))) {
      if (silent != 1) {
       printf("\nDiagonale Loesung gefunden! %s\n", formatMove(i / 4));
      }
      return i;
     }
    }
   }
  }
 }
 return -1;
}

canonical_original_code:
int calculateMove(struct sharedmem *shm, char *stones, int silent) {
 char *stone = malloc(sizeof(char) * 5);
 addchar(stone);
 int i; int j;
 byte_to_binary(shm->StoneToPlace, stone);
 for (i = 0; i < 64; i = i + 4) {
  if (stones[i] == '*') {
   for (j = 0; j < 4; j++) {
    if (((stone[j]) == (stones[((i + 4) % 16) + (i / 16) * 16 + j]))
      && ((stone[j]) == (stones[((i + 8) % 16) + (i / 16) * 16 + j]))
      && ((stone[j]) == (stones[((i + 12) % 16) + (i / 16) * 16 + j]))) {
     if (silent != 1) {
      printf("\nHorizontale Loesung gefunden (%s)! ", formatMove(i / 4));
     }
     return i;
    }
    if (((stone[j]) == (stones[(i + 16) % 64 + j]))
      && ((stone[j]) == (stones[(i + 32) % 64 + j]))
      && ((stone[j]) == (stones[(i + 48) % 64 + j]))) {
     if (silent != 1) {
      printf("\nVertikale Loesung gefunden (%s)! ", formatMove(i / 4));
     }
     return i;
    }
    if (i % 20 == 0) {
     if (((stone[j]) == (stones[(i + 20) % 80 + j]))
       && ((stone[j]) == (stones[(i + 40) % 80 + j]))
       && ((stone[j]) == (stones[(i + 60) % 80 + j]))) {
      if (silent != 1) {
       printf("\nDiagonale Loesung gefunden! %s\n", formatMove(i / 4));
      }
      return i;
     }
    }
    if ((i != 0) && (i % 12) == 0) {
     if (((stone[j]) == (stones[i % 48 + 12 + j]))
       && ((stone[j]) == (stones[(i + 12) % 48 + 12 + j]))
       && ((stone[j]) == (stones[(i + 24) % 48 + 12 + j]))) {
      if (silent != 1) {
       printf("\nDiagonale Loesung gefunden! %s\n", formatMove(i / 4));
      }
      return i;
     }
    }
   }
  }
 }
 return -1;
}

variable_types:
stones
char *

variable_types: silent int

variable_types:
stone
char *

variable_types: i int

variable_types: j int


original_code:
int convertGameFieldQuarto4x4(sharedmem * shm, char* stones) {
 stones[0] = '\0';
 char* stone = malloc(sizeof(char) * 5);
 int i, j;
 for (i = 0; i < shm->fieldY; i++) {
  for (j = 0; j < shm->fieldX; j++) {
   if (*(shm->pf + j + i * shm->fieldY) == -1) {
    strcat(stones, "****");
   } else {
    byte_to_binary(*(shm->pf + j + i * shm->fieldY), stone);
    strcat(stones, stone);
   }
  }
 }
 free(stone);
 return 0;
}

canonical_original_code:
int convertGameFieldQuarto4x4(struct sharedmem *shm, char *stones) {
 stones[0] = '\0';
 char *stone = malloc(sizeof(char) * 5);
 int i; int j;
 for (i = 0; i < shm->fieldY; i++) {
  for (j = 0; j < shm->fieldX; j++) {
   if (*(shm->pf + j + i * shm->fieldY) == -1) {
    strcat(stones, "****");
   } else {
    byte_to_binary(*(shm->pf + j + i * shm->fieldY), stone);
    strcat(stones, stone);
   }
  }
 }
 free(stone);
 return 0;
}