down

decompiled_code:
int __fastcall down(int a1)
{
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

sops.sem_num = 0;
sops.sem_op = -1;
sops.sem_flg = 0;
return semop(a1, &sops, 1uLL);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

sops.sem_num = 0;
sops.sem_op = -1;
sops.sem_flg = 0;
return semop(a1, &sops, 1uLL);
}

original_code:
int down(int semid)
{
 struct sembuf op;
 op.sem_num = 0;
 op.sem_op = -1;
 op.sem_flg = 0;
 return semop(semid, &op, 1);
}

variable_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }


up

repo: mohic/streams

decompiled_code:
int __fastcall up(int a1)
{
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

sops.sem_num = 0;
sops.sem_op = 1;
sops.sem_flg = 0;
return semop(a1, &sops, 1uLL);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

sops.sem_num = 0;
sops.sem_op = 1;
sops.sem_flg = 0;
return semop(a1, &sops, 1uLL);
}

original_code:
int up(int semid)
{
 struct sembuf op;
 op.sem_num = 0;
 op.sem_op = 1;
 op.sem_flg = 0;
 return semop(semid, &op, 1);
}

variable_types: semid int

return_type: int

user_defined_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }


startgame

decompiled_code:
void __fastcall startgame(int a1)
{
if ( a1 == 14 )
{
printf("Le temps minimum de %d secondes est passe\n", 30LL);
timeElapsed = 1;
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 == 14 )
{
printf("Le temps minimum de %d secondes est passe\n", 30LL);
timeElapsed = 1;
}
}

original_code:
void startgame(int sig)
{
 if (sig == 14) {
  printf("Le temps minimum de %d secondes est passe\n", 30);
  timeElapsed = 1;
 }
}

variable_types: sig int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const void **v5; // r12
const char **v6; // r13
unsigned int v7; // eax
unsigned int v8; // er14
unsigned int v9; // er12
unsigned int v11; // er13
struct timeval *v12; // rsi
int v13; // eax
int v14; // eax
unsigned int v15; // eax
unsigned int v16; // er12
__int64 v17; // r12
bool v18; // zf
int v19; // eax
unsigned int v20; // er15
signed int v21; // er8
int v22; // eax
struct timeval *v23; // r15
struct timeval *v24; // r15
__int64 v25; // rax
int v26; // eax
int v27; // eax
struct timeval *v28; // r12
__int64 v29; // rdi
__int64 v30; // rdi
const char **v31; // rdx
int v32; // [rsp+4h] [rbp-1D4h]
_DWORD *shmaddr; // [rsp+18h] [rbp-1C0h]
int v34; // [rsp+20h] [rbp-1B8h]
int v35; // [rsp+24h] [rbp-1B4h]
int shmid; // [rsp+28h] [rbp-1B0h]
__int64 buf[2]; // [rsp+30h] [rbp-1A8h] BYREF
char v38[35]; // [rsp+40h] [rbp-198h] BYREF
fd_set readfds; // [rsp+70h] [rbp-168h] BYREF
int v40[4]; // [rsp+F0h] [rbp-E8h] BYREF
struct timeval timeout; // [rsp+100h] [rbp-D8h] BYREF
fd_set v42; // [rsp+110h] [rbp-C8h] BYREF
char *endptr; // [rsp+198h] [rbp-40h] BYREF

if ( argc > 1 )
{
v5 = (const void **)(argv + 1);
v6 = &argv[(unsigned int)(argc - 2) + 2];
do
{
if ( !memcmp(*v5, "-f", 2uLL) )
{
puts("Netoyage semaphore");
v7 = createSemaphore(10102LL, 950LL);
deleteSemaphore(v7);
}
++v5;
}
while ( v5 != (const void **)v6 );
}
v8 = createSemaphore(10102LL, 1974LL);
if ( v8 == -1 )
{
puts("Une instance du serveur est deja demaree");
return 0;
}
gameStarted = 0;
timeElapsed = 0;
if ( (unsigned int)(argc - 2) > 1 )
{
printf("Usage: %s port [fichierErreur] [-f]\n", *argv);
return 1;
}
v9 = strtol(argv[1], &endptr, 10);
if ( v9 - 1 > 0xFFFE || *endptr )
{
puts(aLePortDoit);
return 1;
}
v11 = ouvrirSocket((unsigned __int16)v9);
if ( v11 == -1 )
return 1;
printf(aServeurEn, v9);
shmid = shmget(10101, 0x64uLL, 950);
shmaddr = shmat(shmid, 0LL, 0);
down(v8);
*shmaddr = 0;
shmaddr[1] = -1;
shmaddr[2] = -1;
shmaddr[3] = -1;
shmaddr[4] = -1;
*((_BYTE *)shmaddr + 100) = 0;
up(v8);
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
v40[1] = -1;
v40[2] = -1;
v40[3] = -1;
v40[0] = accepterClient(v11);
puts("Appuyer sur CTRL+D pour quitter le serveur");
signal(14, startgame);
alarm(0x1Eu);
v34 = 1;
do
{
while ( 1 )
{
do
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
down(v8);
up(v8);
if ( !gameStarted && timeElapsed && (int)*shmaddr > 1 )
{
gameStarted = 1;
demarrerPartie(v40, (unsigned int)v34, shmaddr, v8);
}
timeout.tv_sec = 0LL;
timeout.tv_usec = 5000LL;
memset(&readfds, 0, sizeof(readfds));
v12 = (struct timeval *)v40;
do
{
v13 = v12->tv_sec;
if ( LODWORD(v12->tv_sec) != -1 )
{
readfds.fds_bits[v13 / 64] |= 1LL << (v13 % 64);
v32 = v13;
}
v12 = (struct timeval *)((char *)v12 + 4);
}
while ( v12 != &timeout );
if ( select(v32 + 1, &readfds, 0LL, 0LL, &timeout) > 0 )
{
v17 = 1LL;
v35 = 0;
do
{
v20 = v17 - 1;
v21 = *((_DWORD *)&readfds.fds_bits[15] + v17 + 1);
if ( v21 != -1 && (readfds.fds_bits[v21 / 64] & (1LL << (v21 % 64))) != 0 )
{
buf[0] = 0LL;
buf[1] = 0LL;
memset(v38, 0, sizeof(v38));
v22 = recevoirMessage((unsigned int)v21, buf);
if ( v22 > 0 )
{
v18 = (unsigned int)traiterMessage(
*((unsigned int *)&readfds.fds_bits[15] + v17 + 1),
buf,
shmaddr,
v20,
v8,
(unsigned int)gameStarted) == -1;
v19 = 1;
if ( !v18 )
v19 = v35;
v35 = v19;
}
else if ( !v22 )
{
down(v8);
printf("Perte de connexion avec le joueur %s\n", (const char *)&shmaddr[5 * v17]);
up(v8);
fermerSocket(*((unsigned int *)&readfds.fds_bits[15] + v17 + 1));
*((_DWORD *)&readfds.fds_bits[15] + v17 + 1) = -1;
--v34;
if ( gameStarted && v34 <= 1 )
{
v23 = (struct timeval *)v40;
do
{
if ( LODWORD(v23->tv_sec) != -1 )
{
puts("Annulation de la partie");
envoyerMessage(LODWORD(v23->tv_sec), "2");
v35 = 1;
}
v23 = (struct timeval *)((char *)v23 + 4);
}
while ( v23 != &timeout );
}
else
{
update(v40, 4LL, shmaddr, v8, 0xFFFFFFFFLL, v20);
}
}
}
++v17;
}
while ( v17 != 5 );
if ( v35 )
{
v24 = (struct timeval *)v40;
goto LABEL_66;
}
}
memset(&v42, 0, sizeof(v42));
v42.fds_bits[(int)v11 / 64] |= 1LL << ((int)v11 % 64);
timeout.tv_sec = 0LL;
timeout.tv_usec = 5000LL;
v14 = select(v11 + 1, &v42, 0LL, 0LL, &timeout);
if ( v14 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_48;
}
if ( v14 <= 0 )
break;
puts(aNouveauJoueurP);
v15 = accepterClient(v11);
v16 = v15;
if ( v34 <= 3 && !gameStarted )
{
v25 = 0LL;
while ( v40[v25] != -1 )
{
if ( ++v25 == 4 )
goto LABEL_52;
}
v40[(int)v25] = v16;
++v34;
break;
}
envoyerMessage(v15, &unk_8FD);
puts("inscription refusee");
fermerSocket(v16);
}
LABEL_52:
timeout.tv_sec = 0LL;
timeout.tv_usec = 5000LL;
memset(&v42, 0, sizeof(v42));
v42.fds_bits[0] |= 1uLL;
v26 = select(1, &v42, 0LL, 0LL, &timeout);
if ( v26 >= 0 )
break;
if ( *_errno_location() != 4 )
{
LABEL_48:
v24 = (struct timeval *)v40;
perror("select");
goto LABEL_66;
}
}
}
while ( v26 <= 0 );
LOWORD(buf[0]) = 0;
BYTE2(buf[0]) = 0;
v27 = read(0, buf, 2uLL);
if ( v27 <= 0 )
break;
while ( read(0, 0LL, 0x50uLL) > 0 )
;
}
}
while ( v27 );
v24 = (struct timeval *)v40;
v28 = (struct timeval *)v40;
do
{
v29 = LODWORD(v28->tv_sec);
if ( (_DWORD)v29 != -1 )
envoyerMessage(v29, "2");
v28 = (struct timeval *)((char *)v28 + 4);
}
while ( v28 != &timeout );
do
{
LABEL_66:
v30 = LODWORD(v24->tv_sec);
if ( (_DWORD)v30 != -1 )
fermerSocket(v30);
v24 = (struct timeval *)((char *)v24 + 4);
}
while ( v24 != &timeout );
deleteSemaphore(v8);
shmdt(shmaddr);
shmctl(shmid, 0, 0LL);
puts("Good bye !!!");
fermerSocket(v11);
main(argc, argv, v31);
return 0;
}

original_code:
int main (int argc, char* argv[])
{
 int i;
 for (i = 1; i < argc; i++) {
  if (strncmp(argv[i], "-f", 2) == 0) {
   printf("Netoyage semaphore\n");
   int semid = createSemaphore(10102, 01000 | 0666);
   deleteSemaphore(semid);
  }
 }
 int semid = createSemaphore(10102, 01000 | 02000 | 0666);
 if (semid == -1) {
  printf("Une instance du serveur est deja demaree\n");
  return 0;
 }
 gameStarted = 0;
 timeElapsed = 0;
 if (argc <= 1 || argc >= 4) {
  printf("Usage: %s port [fichierErreur] [-f]\n", argv[0]);
  return 1;
 }
 char *partieEnTrop;
 int port = strtol(argv[1], &partieEnTrop, 10);
 if (port < 1 || port > 65535 || partieEnTrop[0] != '\0') {
  printf("Le port doit être entre 1 et 65535\n");
  return 1;
 }
 int sck = ouvrirSocket((unsigned short)port);
 if (sck == -1) {
  return 1;
 }
 printf("Serveur en écoute sur le port %d\n", port);
 int shmid = shmget(10101, sizeof(struct game), 01000 | 0666);
 struct game *g = (struct game *)shmat(shmid, ((void *)0), 0);
 down(semid);
  g->nbrJoueur = 0;
  for(i = 0; i < 4; i++)
   g->score[i] = -1;
   g->nom[i][0] = '\0';
 up(semid);
 fd_set readfs;
 struct timeval tv;
 tv.tv_sec = 1;
 tv.tv_usec = 0;
 int sockets[4];
 for (i = 0; i < 4; i++)
  sockets[i] = -1;
 int nbrJoueurActuel = 0;
 sockets[nbrJoueurActuel++] = accepterClient(sck);
 printf("Appuyer sur CTRL+D pour quitter le serveur\n");
 signal(14, startgame);
 alarm(30);
 int quitter = 0;
 while(!quitter) {
  down(semid);
   int nbr = g->nbrJoueur;
  up(semid);
  if (!gameStarted && timeElapsed && g->nbrJoueur >= 2) {
   gameStarted = 1;
   demarrerPartie(sockets, nbrJoueurActuel, g, semid);
  }
  int ret = 0;
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  fd_set readfsJoueur;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfsJoueur)->__fds_bits)[0]) : "memory"); } while (0);
  int numSck;
  for (i = 0; i < 4; i++)
   if (sockets[i] != -1)
    ((void) (((&readfsJoueur)->__fds_bits)[((numSck = sockets[i]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((numSck = sockets[i]) % (8 * (int) sizeof (__fd_mask)))))));
  ret = select(numSck + 1, &readfsJoueur, ((void *)0), ((void *)0), &tv);
  if (ret > 0) {
   for (i = 0; i < 4; i++) {
    if (sockets[i] != -1 && ((((&readfsJoueur)->__fds_bits)[((sockets[i]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockets[i]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
     char message[50 + 1] = {'\0'};
     int val = recevoirMessage(sockets[i], message);
     if (val > 0) {
      if (traiterMessage(sockets[i], message, g, i, semid, gameStarted) == -1)
       quitter = 1;
     } else if (val == 0) {
      down(semid);
       printf("Perte de connexion avec le joueur %s\n", g->nom[i]);
      up(semid);
      fermerSocket(sockets[i]);
      sockets[i] = -1;
      nbrJoueurActuel--;
      if (gameStarted && nbrJoueurActuel < 2) {
       int j;
       for (j = 0; j < 4; j++) {
        if (sockets[j] != -1) {
         printf("Annulation de la partie\n");
         envoyerMessage(sockets[j], "2");
         quitter = 1;
        }
       }
      } else {
       update(sockets, 4, g, semid, sockets[i], i);
      }
     }
    }
   }
  }
  if (quitter)
   break;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfs)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&readfs)->__fds_bits)[((sck) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sck) % (8 * (int) sizeof (__fd_mask)))))));
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  if ((ret = select(sck + 1, &readfs, ((void *)0), ((void *)0), &tv)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   perror("select");
   break;
  } else if (ret > 0) {
   printf("Nouveau joueur présent\n");
   int sckClient = accepterClient(sck);
   if (nbrJoueurActuel >= 4 || gameStarted) {
    envoyerMessage(sckClient, "1=0");
    printf("inscription refusee\n");
    fermerSocket(sckClient);
    continue;
   }
   for (i = 0; i < 4; i++) {
    if (sockets[i] == -1) {
     sockets[i] = sckClient;
     nbrJoueurActuel++;
     break;
    }
   }
  }
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfs)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&readfs)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  if ((ret = select(0 + 1, &readfs, ((void *)0), ((void *)0), &tv)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   perror("select");
   break;
  } else if (ret > 0) {
   char msg[3] = {'\0'};
   int size = read(0, msg, 2);
   if (size > 0) {
    while(read(0, ((void *)0), 80) > 0);
   }
   if (size == 0) {
    for (i = 0; i < 4; i++)
     if (sockets[i] != -1)
      envoyerMessage(sockets[i], "2");
    break;
   }
  }
 }
 for (i = 0; i < 4; i++) {
  if (sockets[i] != -1)
   fermerSocket(sockets[i]);
 }
 deleteSemaphore(semid);
 shmdt(g);
 shmctl(shmid, 0, 0);
 printf("Good bye !!!\n");
 fermerSocket(sck);
 main(argc, argv);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int i;
 for (i = 1; i < argc; i++) {
  if (strncmp(argv[i], "-f", 2) == 0) {
   printf("Netoyage semaphore\n");
   int semid = createSemaphore(10102, 01000 | 0666);
   deleteSemaphore(semid);
  }
 }
 int semid = createSemaphore(10102, 01000 | 02000 | 0666);
 if (semid == -1) {
  printf("Une instance du serveur est deja demaree\n");
  return 0;
 }
 gameStarted = 0;
 timeElapsed = 0;
 if (argc <= 1 || argc >= 4) {
  printf("Usage: %s port [fichierErreur] [-f]\n", argv[0]);
  return 1;
 }
 char *partieEnTrop;
 int port = strtol(argv[1], &partieEnTrop, 10);
 if (port < 1 || port > 65535 || partieEnTrop[0] != '\0') {
  printf("Le port doit être entre 1 et 65535\n");
  return 1;
 }
 int sck = ouvrirSocket((unsigned short)port);
 if (sck == -1) {
  return 1;
 }
 printf("Serveur en écoute sur le port %d\n", port);
 int shmid = shmget(10101, sizeof(struct game), 01000 | 0666);
 struct game *g = (struct game *)shmat(shmid, ((void *)0), 0);
 down(semid);
  g->nbrJoueur = 0;
  for(i = 0; i < 4; i++)
   g->score[i] = -1;
   g->nom[i][0] = '\0';
 up(semid);
 struct fd_set readfs;
 struct timeval tv;
 tv.tv_sec = 1;
 tv.tv_usec = 0;
 int sockets[4];
 for (i = 0; i < 4; i++)
  sockets[i] = -1;
 int nbrJoueurActuel = 0;
 sockets[nbrJoueurActuel++] = accepterClient(sck);
 printf("Appuyer sur CTRL+D pour quitter le serveur\n");
 signal(14, startgame);
 alarm(30);
 int quitter = 0;
 while(!quitter) {
  down(semid);
   int nbr = g->nbrJoueur;
  up(semid);
  if (!gameStarted && timeElapsed && g->nbrJoueur >= 2) {
   gameStarted = 1;
   demarrerPartie(sockets, nbrJoueurActuel, g, semid);
  }
  int ret = 0;
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  struct fd_set readfsJoueur;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfsJoueur)->__fds_bits)[0]) : "memory"); } while (0);
  int numSck;
  for (i = 0; i < 4; i++)
   if (sockets[i] != -1)
    ((void) (((&readfsJoueur)->__fds_bits)[((numSck = sockets[i]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((numSck = sockets[i]) % (8 * (int) sizeof (__fd_mask)))))));
  ret = select(numSck + 1, &readfsJoueur, ((void *)0), ((void *)0), &tv);
  if (ret > 0) {
   for (i = 0; i < 4; i++) {
    if (sockets[i] != -1 && ((((&readfsJoueur)->__fds_bits)[((sockets[i]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sockets[i]) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
     char message[-1] = {'\0'};
     int val = recevoirMessage(sockets[i], message);
     if (val > 0) {
      if (traiterMessage(sockets[i], message, g, i, semid, gameStarted) == -1)
       quitter = 1;
     } else if (val == 0) {
      down(semid);
       printf("Perte de connexion avec le joueur %s\n", g->nom[i]);
      up(semid);
      fermerSocket(sockets[i]);
      sockets[i] = -1;
      nbrJoueurActuel--;
      if (gameStarted && nbrJoueurActuel < 2) {
       int j;
       for (j = 0; j < 4; j++) {
        if (sockets[j] != -1) {
         printf("Annulation de la partie\n");
         envoyerMessage(sockets[j], "2");
         quitter = 1;
        }
       }
      } else {
       update(sockets, 4, g, semid, sockets[i], i);
      }
     }
    }
   }
  }
  if (quitter)
   break;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfs)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&readfs)->__fds_bits)[((sck) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sck) % (8 * (int) sizeof (__fd_mask)))))));
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  if ((ret = select(sck + 1, &readfs, ((void *)0), ((void *)0), &tv)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   perror("select");
   break;
  } else if (ret > 0) {
   printf("Nouveau joueur présent\n");
   int sckClient = accepterClient(sck);
   if (nbrJoueurActuel >= 4 || gameStarted) {
    envoyerMessage(sckClient, "1=0");
    printf("inscription refusee\n");
    fermerSocket(sckClient);
    continue;
   }
   for (i = 0; i < 4; i++) {
    if (sockets[i] == -1) {
     sockets[i] = sckClient;
     nbrJoueurActuel++;
     break;
    }
   }
  }
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfs)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&readfs)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
  if ((ret = select(0 + 1, &readfs, ((void *)0), ((void *)0), &tv)) < 0) {
   if ((*__errno_location ()) == 4)
    continue;
   perror("select");
   break;
  } else if (ret > 0) {
   char msg[3] = {'\0'};
   int size = read(0, msg, 2);
   if (size > 0) {
    while(read(0, ((void *)0), 80) > 0);
   }
   if (size == 0) {
    for (i = 0; i < 4; i++)
     if (sockets[i] != -1)
      envoyerMessage(sockets[i], "2");
    break;
   }
  }
 }
 for (i = 0; i < 4; i++) {
  if (sockets[i] != -1)
   fermerSocket(sockets[i]);
 }
 deleteSemaphore(semid);
 shmdt(g);
 shmctl(shmid, 0, 0);
 printf("Good bye !!!\n");
 fermerSocket(sck);
 main(argc, argv);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

variable_types:
partieEnTrop
char *

variable_types: port int

variable_types: sck int

variable_types: shmid int

variable_types: struct game { int nbrJoueur; int score[4]; char nom[4][20]; } *

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

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

variable_types:
sockets
int [4]

variable_types: nbrJoueurActuel int

variable_types: quitter int

variable_types: nbr int

variable_types: ret int

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

variable_types: numSck int

variable_types:
message
char [-1]

variable_types: val int

variable_types: j int

variable_types: sckClient int

variable_types:
msg
char [3]

variable_types: size int


finJeu

decompiled_code:
__int64 __fastcall finJeu(__int64 a1, unsigned int a2)
{
__int64 i; // rbx
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rdi

puts("Tous les scores ont ete recus");
down(a2);
for ( i = 1LL; i != 5; ++i )
{
if ( scks[i - 1] != -1 )
printf("%s a eu %d\n", (const char *)(a1 + 20 * i), *(unsigned int *)(a1 + 4 * i));
}
result = up(a2);
if ( tailleScks > 0 )
{
v4 = 0LL;
do
{
v5 = (unsigned int)scks[v4];
if ( (_DWORD)v5 != -1 )
result = envoyerMessage(v5, "5");
++v4;
}
while ( tailleScks > (int)v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
__int64 i; // rbx
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rdi

puts("Tous les scores ont ete recus");
down(a2);
for ( i = 1LL; i != 5; ++i )
{
if ( scks[i - 1] != -1 )
printf("%s a eu %d\n", (const char *)(a1 + 20 * i), *(unsigned int *)(a1 + 4 * i));
}
result = up(a2);
if ( tailleScks > 0 )
{
v4 = 0LL;
do
{
v5 = (unsigned int)scks[v4];
if ( (_DWORD)v5 != -1 )
result = envoyerMessage(v5, "5");
++v4;
}
while ( tailleScks > (int)v4 );
}
return result;
}

original_code:
void finJeu(game *g, int semid)
{
 int i;
 printf("Tous les scores ont ete recus\n");
 down(semid);
  for (i = 0; i < 4; i++)
   if (scks[i] != -1)
    printf("%s a eu %d\n", g->nom[i], g->score[i]);
 up(semid);
 for (i = 0; i < tailleScks; i++)
  if (scks[i] != -1)
   envoyerMessage(scks[i], "5");
}

canonical_original_code:
void finJeu(struct game *g, int semid)
{
 int i;
 printf("Tous les scores ont ete recus\n");
 down(semid);
  for (i = 0; i < 4; i++)
   if (scks[i] != -1)
    printf("%s a eu %d\n", g->nom[i], g->score[i]);
 up(semid);
 for (i = 0; i < tailleScks; i++)
  if (scks[i] != -1)
   envoyerMessage(scks[i], "5");
}

variable_types: semid int

variable_types: i int

user_defined_types: struct game { int nbrJoueur; int score[4]; char nom[4][20]; }


finPartie

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

LODWORD(v2) = puts("Fin de la partie. En attente du score des joueurs");
tuilePlacee = 0;
*(&tuilePlacee + 1) = 0;
*(&tuilePlacee + 2) = 0;
*(&tuilePlacee + 3) = 0;
if ( a2 > 0 )
{
v3 = a1;
v2 = (unsigned int)(a2 - 1);
v4 = (__int64)&a1[v2 + 1];
do
{
v5 = *v3;
if ( (_DWORD)v5 != -1 )
LODWORD(v2) = envoyerMessage(v5, "4");
++v3;
}
while ( (unsigned int *)v4 != v3 );
}
return v2;
}

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

LODWORD(v2) = puts("Fin de la partie. En attente du score des joueurs");
tuilePlacee = 0;
*(&tuilePlacee + 1) = 0;
*(&tuilePlacee + 2) = 0;
*(&tuilePlacee + 3) = 0;
if ( a2 > 0 )
{
v3 = a1;
v2 = (unsigned int)(a2 - 1);
v4 = (__int64)&a1[v2 + 1];
do
{
v5 = *v3;
if ( (_DWORD)v5 != -1 )
LODWORD(v2) = envoyerMessage(v5, "4");
++v3;
}
while ( (unsigned int *)v4 != v3 );
}
return v2;
}

original_code:
void finPartie(int sockets[], int taille, game *g, int semid)
{
 int i;
 printf("Fin de la partie. En attente du score des joueurs\n");
 for (i = 0; i < 4; i++)
  tuilePlacee[i] = 0;
 for (i = 0; i < taille; i++)
  if (sockets[i] != -1)
   envoyerMessage(sockets[i], "4");
}

canonical_original_code:
void finPartie(int sockets[], int taille, struct game *g, int semid)
{
 int i;
 printf("Fin de la partie. En attente du score des joueurs\n");
 for (i = 0; i < 4; i++)
  tuilePlacee[i] = 0;
 for (i = 0; i < taille; i++)
  if (sockets[i] != -1)
   envoyerMessage(sockets[i], "4");
}

variable_types:
sockets
int []

variable_types: taille int

variable_types: struct game { int nbrJoueur; int score[4]; char nom[4][20]; } *


piocherTuile

decompiled_code:
__int64 __fastcall piocherTuile(unsigned int *a1, int a2)
{
int v2; // edx
__int64 v3; // rbx
__int64 result; // rax
unsigned int *v5; // rbx
__int64 v6; // rbp
__int64 v7; // rdi
char s[4]; // [rsp+Bh] [rbp-1Dh] BYREF
char v9; // [rsp+Fh] [rbp-19h]

v2 = 0;
if ( nbrRestant > 0 )
v2 = rand() % nbrRestant;
*(_DWORD *)s = 0;
v9 = 0;
v3 = v2;
sprintf(s, "3=%d", (unsigned int)tuiles[v2]);
printf("Tuile piochee: %d\n", (unsigned int)tuiles[v3]);
result = (unsigned int)(nbrRestant - 1);
tuiles[v3] = tuiles[(int)result];
nbrRestant = result;
--tour;
tuilePlacee = 0;
*(&tuilePlacee + 1) = 0;
*(&tuilePlacee + 2) = 0;
*(&tuilePlacee + 3) = 0;
if ( a2 > 0 )
{
v5 = a1;
result = (unsigned int)(a2 - 1);
v6 = (__int64)&a1[result + 1];
do
{
v7 = *v5;
if ( (_DWORD)v7 != -1 )
result = envoyerMessage(v7, s);
++v5;
}
while ( (unsigned int *)v6 != v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, int a2)
{
int v2; // edx
__int64 v3; // rbx
__int64 result; // rax
unsigned int *v5; // rbx
__int64 v6; // rbp
__int64 v7; // rdi
char s[4]; // [rsp+Bh] [rbp-1Dh] BYREF
char v9; // [rsp+Fh] [rbp-19h]

v2 = 0;
if ( nbrRestant > 0 )
v2 = rand() % nbrRestant;
*(_DWORD *)s = 0;
v9 = 0;
v3 = v2;
sprintf(s, "3=%d", (unsigned int)tuiles[v2]);
printf("Tuile piochee: %d\n", (unsigned int)tuiles[v3]);
result = (unsigned int)(nbrRestant - 1);
tuiles[v3] = tuiles[(int)result];
nbrRestant = result;
--tour;
tuilePlacee = 0;
*(&tuilePlacee + 1) = 0;
*(&tuilePlacee + 2) = 0;
*(&tuilePlacee + 3) = 0;
if ( a2 > 0 )
{
v5 = a1;
result = (unsigned int)(a2 - 1);
v6 = (__int64)&a1[result + 1];
do
{
v7 = *v5;
if ( (_DWORD)v7 != -1 )
result = envoyerMessage(v7, s);
++v5;
}
while ( (unsigned int *)v6 != v5 );
}
return result;
}

original_code:
void piocherTuile(int sockets[], int taille, game *g, int semid)
{
 int i;
 int posTuile = 0;
 if (nbrRestant > 0)
  posTuile = rand() % nbrRestant;
 char msg[5] = {'\0'};
 sprintf(msg, "3=%d", tuiles[posTuile]);
 printf("Tuile piochee: %d\n", tuiles[posTuile]);
 tuiles[posTuile] = tuiles[nbrRestant - 1];
 nbrRestant--;
 tour--;
 for (i = 0; i < 4; i++)
  tuilePlacee[i] = 0;
 for (i = 0; i < taille; i++)
  if (sockets[i] != -1)
   envoyerMessage(sockets[i], msg);
}

canonical_original_code:
void piocherTuile(int sockets[], int taille, struct game *g, int semid)
{
 int i;
 int posTuile = 0;
 if (nbrRestant > 0)
  posTuile = rand() % nbrRestant;
 char msg[5] = {'\0'};
 sprintf(msg, "3=%d", tuiles[posTuile]);
 printf("Tuile piochee: %d\n", tuiles[posTuile]);
 tuiles[posTuile] = tuiles[nbrRestant - 1];
 nbrRestant--;
 tour--;
 for (i = 0; i < 4; i++)
  tuilePlacee[i] = 0;
 for (i = 0; i < taille; i++)
  if (sockets[i] != -1)
   envoyerMessage(sockets[i], msg);
}

variable_types: posTuile int

variable_types:
msg
char [5]


demarrerPartie

decompiled_code:
__int64 __fastcall demarrerPartie(unsigned int *a1, int a2)
{
__int64 v2; // rax
unsigned int v3; // eax
__int64 i; // rax

if ( a2 > 0 )
{
v2 = 0LL;
do
{
scks[v2] = a1[v2];
++v2;
}
while ( a2 > (int)v2 );
}
tailleScks = a2;
tour = 20;
puts("La partie commence");
v3 = time(0LL);
srand(v3);
for ( i = 1LL; i != 11; ++i )
tuiles[i - 1] = i;
tuiles[10] = 11;
tuiles[11] = 12;
tuiles[12] = 13;
tuiles[13] = 14;
tuiles[14] = 15;
tuiles[15] = 16;
tuiles[16] = 17;
tuiles[17] = 18;
tuiles[18] = 19;
tuiles[19] = 11;
tuiles[20] = 12;
tuiles[21] = 13;
tuiles[22] = 14;
tuiles[23] = 15;
tuiles[24] = 16;
tuiles[25] = 17;
tuiles[26] = 18;
tuiles[27] = 19;
tuiles[28] = 20;
tuiles[29] = 21;
tuiles[30] = 22;
tuiles[31] = 23;
tuiles[32] = 24;
tuiles[33] = 25;
tuiles[34] = 26;
tuiles[35] = 27;
tuiles[36] = 28;
tuiles[37] = 29;
tuiles[38] = 30;
tuiles[39] = 42;
nbrRestant = 40;
return piocherTuile(a1, a2);
}

original_code:
void demarrerPartie(int sockets[], int taille, game *g, int semid)
{
 int i;
 for (i = 0; i < taille; i++)
  scks[i] = sockets[i];
 tailleScks = taille;
 tour = 20;
 printf("La partie commence\n");
 srand(time(((void *)0)));
 for (i = 0; i < 10; i++)
  tuiles[i] = i + 1;
 for (i = 0; i < 9; i ++)
  tuiles[i + 10] = i + 11;
 for (i = 0; i < 9; i++)
  tuiles[i + 19] = i + 11;
 for (i = 0; i < 11; i++)
  tuiles[i + 28] = i + 20;
 tuiles[39] = 42;
 nbrRestant = 40;
 piocherTuile(sockets, taille, g, semid);
}

canonical_original_code:
void demarrerPartie(int sockets[], int taille, struct game *g, int semid)
{
 int i;
 for (i = 0; i < taille; i++)
  scks[i] = sockets[i];
 tailleScks = taille;
 tour = 20;
 printf("La partie commence\n");
 srand(time(((void *)0)));
 for (i = 0; i < 10; i++)
  tuiles[i] = i + 1;
 for (i = 0; i < 9; i ++)
  tuiles[i + 10] = i + 11;
 for (i = 0; i < 9; i++)
  tuiles[i + 19] = i + 11;
 for (i = 0; i < 11; i++)
  tuiles[i + 28] = i + 20;
 tuiles[39] = 42;
 nbrRestant = 40;
 piocherTuile(sockets, taille, g, semid);
}


update

decompiled_code:
__int64 __fastcall update(__int64 a1, int a2, int *a3, unsigned int a4, unsigned int a5, int a6)
{
__int64 v10; // rax

if ( a2 > 0 )
{
v10 = 0LL;
do
{
scks[v10] = *(_DWORD *)(a1 + 4 * v10);
++v10;
}
while ( a2 > (int)v10 );
}
tailleScks = a2;
down(a4);
--*a3;
up(a4);
return traiterMessage(a5, "3", a3, a6, a4, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, int *a3, unsigned int a4, unsigned int a5, int a6)
{
__int64 v10; // rax

if ( a2 > 0 )
{
v10 = 0LL;
do
{
scks[v10] = *(_DWORD *)(a1 + 4 * v10);
++v10;
}
while ( a2 > (int)v10 );
}
tailleScks = a2;
down(a4);
--*a3;
up(a4);
return func3(a5, "3", a3, a6, a4, 1u);
}

original_code:
void update(int sockets[], int taille, game *g, int semid, int socketDeconnecte, int joueur)
{
 int i;
 for (i = 0; i < taille; i++)
  scks[i] = sockets[i];
 tailleScks = taille;
 down(semid);
  g->nbrJoueur--;
 up(semid);
 traiterMessage(socketDeconnecte, "3", g, joueur, semid, 1);
}

canonical_original_code:
void update(int sockets[], int taille, struct game *g, int semid, int socketDeconnecte, int joueur)
{
 int i;
 for (i = 0; i < taille; i++)
  scks[i] = sockets[i];
 tailleScks = taille;
 down(semid);
  g->nbrJoueur--;
 up(semid);
 traiterMessage(socketDeconnecte, "3", g, joueur, semid, 1);
}

variable_types: socketDeconnecte int

variable_types: joueur int


traiterMessage

decompiled_code:
__int64 __fastcall traiterMessage(unsigned int a1, const char *a2, int *a3, int a4, unsigned int a5, unsigned int a6)
{
char v6; // al
__int64 result; // rax
size_t i; // r14
__int64 v12; // rbp
int v13; // ebx
__int64 v14; // rax
int v15; // edx
__int64 v16; // rbp
unsigned int v17; // eax
int v18; // ebx
__int64 v19; // rax
int v20; // edx
int v22; // [rsp+14h] [rbp-44h]
__int64 v23; // [rsp+18h] [rbp-40h]

v6 = *a2;
if ( !*a2 )
return 0xFFFFFFFFLL;
switch ( v6 )
{
case '3':
v12 = a4;
tuilePlacee[a4] = 1;
down(a5);
printf(aLeJoueurSAPlac, &a3[5 * v12 + 5]);
v13 = *a3;
up(a5);
v14 = 0LL;
v15 = 0;
do
{
if ( scks[v14] != -1 )
v15 -= (tuilePlacee[v14] == 0) - 1;
++v14;
}
while ( v14 != 4 );
result = 0LL;
if ( v15 >= v13 )
{
if ( tour <= 0 )
finPartie((unsigned int *)scks, tailleScks);
else
piocherTuile((unsigned int *)scks, tailleScks);
result = 0LL;
}
break;
case '5':
v16 = a4;
tuilePlacee[a4] = 1;
down(a5);
v17 = atoi(a2 + 2);
a3[v16 + 1] = v17;
printf("Score du joueur %s: %d\n", (const char *)&a3[5 * v16 + 5], v17);
v18 = *a3;
up(a5);
v19 = 0LL;
v20 = 0;
do
{
if ( scks[v19] != -1 )
v20 -= (tuilePlacee[v19] == 0) - 1;
++v19;
}
while ( v19 != 4 );
if ( v20 >= v18 )
{
finJeu((__int64)a3, a5);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
break;
case '1':
if ( a6 )
{
puts("joueur refuse car partie deja commencee");
envoyerMessage(a1, "1=0");
result = 0LL;
}
else
{
down(a5);
v22 = *a3;
if ( *a3 > 3 )
{
puts("joueur refuse car maximum ateint");
envoyerMessage(a1, "1=0");
result = a6;
}
else
{
v23 = 5LL * a4;
for ( i = 0LL; strlen(a2) - 3 > i; ++i )
*((_BYTE *)&a3[v23 + 5] + i) = a2[i + 2];
*((_BYTE *)&a3[5 * a4 + 5] + (int)i) = 0;
*a3 = v22 + 1;
printf("Nom du joueur: %s\n", (const char *)&a3[v23 + 5]);
up(a5);
envoyerMessage(a1, "1=1");
puts("joueur accepte");
result = a6;
}
}
break;
default:
printf("Message inconnu: %s\n", a2);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, const char *a2, int *a3, int a4, unsigned int a5, unsigned int a6)
{
char v6; // al
__int64 result; // rax
size_t i; // r14
__int64 v12; // rbp
int v13; // ebx
__int64 v14; // rax
int v15; // edx
__int64 v16; // rbp
unsigned int v17; // eax
int v18; // ebx
__int64 v19; // rax
int v20; // edx
int v22; // [rsp+14h] [rbp-44h]
__int64 v23; // [rsp+18h] [rbp-40h]

v6 = *a2;
if ( !*a2 )
return 0xFFFFFFFFLL;
switch ( v6 )
{
case '3':
v12 = a4;
tuilePlacee[a4] = 1;
down(a5);
printf(aLeJoueurSAPlac, &a3[5 * v12 + 5]);
v13 = *a3;
up(a5);
v14 = 0LL;
v15 = 0;
do
{
if ( scks[v14] != -1 )
v15 -= (tuilePlacee[v14] == 0) - 1;
++v14;
}
while ( v14 != 4 );
result = 0LL;
if ( v15 >= v13 )
{
if ( tour <= 0 )
func1((unsigned int *)scks, tailleScks);
else
func2((unsigned int *)scks, tailleScks);
result = 0LL;
}
break;
case '5':
v16 = a4;
tuilePlacee[a4] = 1;
down(a5);
v17 = atoi(a2 + 2);
a3[v16 + 1] = v17;
printf("Score du joueur %s: %d\n", (const char *)&a3[5 * v16 + 5], v17);
v18 = *a3;
up(a5);
v19 = 0LL;
v20 = 0;
do
{
if ( scks[v19] != -1 )
v20 -= (tuilePlacee[v19] == 0) - 1;
++v19;
}
while ( v19 != 4 );
if ( v20 >= v18 )
{
func0((__int64)a3, a5);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
break;
case '1':
if ( a6 )
{
puts("joueur refuse car partie deja commencee");
envoyerMessage(a1, "1=0");
result = 0LL;
}
else
{
down(a5);
v22 = *a3;
if ( *a3 > 3 )
{
puts("joueur refuse car maximum ateint");
envoyerMessage(a1, "1=0");
result = a6;
}
else
{
v23 = 5LL * a4;
for ( i = 0LL; strlen(a2) - 3 > i; ++i )
*((_BYTE *)&a3[v23 + 5] + i) = a2[i + 2];
*((_BYTE *)&a3[5 * a4 + 5] + (int)i) = 0;
*a3 = v22 + 1;
printf("Nom du joueur: %s\n", (const char *)&a3[v23 + 5]);
up(a5);
envoyerMessage(a1, "1=1");
puts("joueur accepte");
result = a6;
}
}
break;
default:
printf("Message inconnu: %s\n", a2);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
int traiterMessage(int sckClient, char *message, game *g, int joueur, int semid, int aDemarre) {
 int i;
 int nbrPlace;
 int nbrJoueur;
 if (strlen(message) <= 0)
  return -1;
 switch(message[0]) {
  case '1':
   if (aDemarre) {
    printf("joueur refuse car partie deja commencee\n");
    envoyerMessage(sckClient, "1=0");
    break;
   }
   down(semid);
    if (g->nbrJoueur >= 4) {
     printf("joueur refuse car maximum ateint\n");
     envoyerMessage(sckClient, "1=0");
     break;
    }
    for (i = 0; i < strlen(message) - 3; i++)
      g->nom[joueur][i] = message[i+2];
    g->nom[joueur][i] = '\0';
    g->nbrJoueur++;
    printf ("Nom du joueur: %s\n", g->nom[joueur]);
   up(semid);
   envoyerMessage(sckClient, "1=1");
   printf("joueur accepte\n");
   break;
  case '3':
   tuilePlacee[joueur] = 1;
   down(semid);
    printf("Le joueur %s a placé sa tuile\n", g->nom[joueur]);
    nbrJoueur = g->nbrJoueur;
   up(semid);
   nbrPlace = 0;
   for (i = 0; i < 4; i++)
    if (scks[i] != -1 && tuilePlacee[i])
     nbrPlace++;
   if (nbrPlace >= nbrJoueur && tour <= 0)
    finPartie(scks, tailleScks, g, semid);
   else if (nbrPlace >= nbrJoueur)
    piocherTuile(scks, tailleScks, g, semid);
   break;
  case '5':
   tuilePlacee[joueur] = 1;
   down(semid);
    message += 2;
    g->score[joueur] = atoi(message);
    printf("Score du joueur %s: %d\n", g->nom[joueur], g->score[joueur]);
    nbrJoueur = g->nbrJoueur;
   up(semid);
   nbrPlace = 0;
   for(i = 0; i < 4; i++)
    if (scks[i] != -1 && tuilePlacee[i])
     nbrPlace++;
   if (nbrPlace >= nbrJoueur) {
    finJeu(g, semid);
    return -1;
   }
   break;
  default:
   printf("Message inconnu: %s\n", message);
   return -1;
 }
 return 0;
}

canonical_original_code:
int traiterMessage(int sckClient, char *message, struct game *g, int joueur, int semid, int aDemarre) {
 int i;
 int nbrPlace;
 int nbrJoueur;
 if (strlen(message) <= 0)
  return -1;
 switch(message[0]) {
  case '1':
   if (aDemarre) {
    printf("joueur refuse car partie deja commencee\n");
    envoyerMessage(sckClient, "1=0");
    break;
   }
   down(semid);
    if (g->nbrJoueur >= 4) {
     printf("joueur refuse car maximum ateint\n");
     envoyerMessage(sckClient, "1=0");
     break;
    }
    for (i = 0; i < strlen(message) - 3; i++)
      g->nom[joueur][i] = message[i+2];
    g->nom[joueur][i] = '\0';
    g->nbrJoueur++;
    printf ("Nom du joueur: %s\n", g->nom[joueur]);
   up(semid);
   envoyerMessage(sckClient, "1=1");
   printf("joueur accepte\n");
   break;
  case '3':
   tuilePlacee[joueur] = 1;
   down(semid);
    printf("Le joueur %s a placé sa tuile\n", g->nom[joueur]);
    nbrJoueur = g->nbrJoueur;
   up(semid);
   nbrPlace = 0;
   for (i = 0; i < 4; i++)
    if (scks[i] != -1 && tuilePlacee[i])
     nbrPlace++;
   if (nbrPlace >= nbrJoueur && tour <= 0)
    finPartie(scks, tailleScks, g, semid);
   else if (nbrPlace >= nbrJoueur)
    piocherTuile(scks, tailleScks, g, semid);
   break;
  case '5':
   tuilePlacee[joueur] = 1;
   down(semid);
    message += 2;
    g->score[joueur] = atoi(message);
    printf("Score du joueur %s: %d\n", g->nom[joueur], g->score[joueur]);
    nbrJoueur = g->nbrJoueur;
   up(semid);
   nbrPlace = 0;
   for(i = 0; i < 4; i++)
    if (scks[i] != -1 && tuilePlacee[i])
     nbrPlace++;
   if (nbrPlace >= nbrJoueur) {
    finJeu(g, semid);
    return -1;
   }
   break;
  default:
   printf("Message inconnu: %s\n", message);
   return -1;
 }
 return 0;
}

variable_types:
message
char *

variable_types: aDemarre int

variable_types: nbrPlace int

variable_types: nbrJoueur int


listerJoueurs

decompiled_code:
__int64 __fastcall listerJoueurs(_DWORD *a1, unsigned int a2)
{
const char *v2; // rbp
int v3; // ebx
size_t v4; // rax
__int64 v6[3]; // [rsp+0h] [rbp-48h] BYREF
__int16 v7; // [rsp+18h] [rbp-30h]
char v8; // [rsp+1Ah] [rbp-2Eh]

write(1, "Liste des joueurs present:\n", 0x1BuLL);
down(a2);
if ( (int)*a1 > 0 )
{
v2 = (const char *)(a1 + 5);
v3 = 0;
do
{
v6[0] = 0LL;
v6[1] = 0LL;
v6[2] = 0LL;
v7 = 0;
v8 = 0;
snprintf((char *)v6, 0x21uLL, "Nom: %s\n", v2);
v4 = strlen((const char *)v6);
write(1, v6, v4);
++v3;
v2 += 20;
}
while ( *a1 > v3 );
}
return up(a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, unsigned int a2)
{
const char *v2; // rbp
int v3; // ebx
size_t v4; // rax
__int64 v6[3]; // [rsp+0h] [rbp-48h] BYREF
__int16 v7; // [rsp+18h] [rbp-30h]
char v8; // [rsp+1Ah] [rbp-2Eh]

write(1, "Liste des joueurs present:\n", 0x1BuLL);
down(a2);
if ( (int)*a1 > 0 )
{
v2 = (const char *)(a1 + 5);
v3 = 0;
do
{
v6[0] = 0LL;
v6[1] = 0LL;
v6[2] = 0LL;
v7 = 0;
v8 = 0;
snprintf((char *)v6, 0x21uLL, "Nom: %s\n", v2);
v4 = strlen((const char *)v6);
write(1, v6, v4);
++v3;
v2 += 20;
}
while ( *a1 > v3 );
}
return up(a2);
}

original_code:
void listerJoueurs(game *g, int semid)
{
 int i;
 char *m = "Liste des joueurs present:\n";
 write(1, m, strlen(m));
 down(semid);
  for (i = 0; i < g->nbrJoueur; i++) {
   char m2[6 + 20 + 1] = {'\0'};
   snprintf(m2, 6 + 6 + 20 + 1, "Nom: %s\n", g->nom[i]);
   write(1, m2, strlen(m2));
  }
 up(semid);
}

canonical_original_code:
void listerJoueurs(struct game *g, int semid)
{
 int i;
 char *m = "Liste des joueurs present:\n";
 write(1, m, strlen(m));
 down(semid);
  for (i = 0; i < g->nbrJoueur; i++) {
   char m2[-1] = {'\0'};
   snprintf(m2, 6 + 6 + 20 + 1, "Nom: %s\n", g->nom[i]);
   write(1, m2, strlen(m2));
  }
 up(semid);
}

variable_types:
m
char *


afficherTuiles

decompiled_code:
ssize_t afficherTuiles()
{
int v0; // ebx
size_t v1; // rax
int *v2; // rbx
__int64 v3; // rcx
size_t v4; // rax
char s[30]; // [rsp+Ah] [rbp-1Eh] BYREF

write(1, "Emplacements : ", 0xFuLL);
v0 = 0;
do
{
snprintf(s, 6uLL, "[%.2d] ", (unsigned int)++v0);
v1 = strlen(s);
write(1, s, v1);
}
while ( v0 != 20 );
write(1, "\n", 1uLL);
write(1, "Valeurs : ", 0xFuLL);
v2 = &tuiles;
do
{
v3 = (unsigned int)*v2;
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 == 42 )
{
write(1, "[ *] ", 5uLL);
}
else
{
snprintf(s, 6uLL, "[%.2d] ", v3);
v4 = strlen(s);
write(1, s, v4);
}
}
else
{
write(1, "[ ] ", 5uLL);
}
++v2;
}
while ( v2 != (int *)(unsigned int)((_DWORD)&tuiles + 80) );
return write(1, "\n", 1uLL);
}

canonical_decompiled_code:
ssize_t func1()
{
int v0; // ebx
size_t v1; // rax
int *v2; // rbx
__int64 v3; // rcx
size_t v4; // rax
char s[30]; // [rsp+Ah] [rbp-1Eh] BYREF

write(1, "Emplacements : ", 0xFuLL);
v0 = 0;
do
{
snprintf(s, 6uLL, "[%.2d] ", (unsigned int)++v0);
v1 = strlen(s);
write(1, s, v1);
}
while ( v0 != 20 );
write(1, "\n", 1uLL);
write(1, "Valeurs : ", 0xFuLL);
v2 = &tuiles;
do
{
v3 = (unsigned int)*v2;
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 == 42 )
{
write(1, "[ *] ", 5uLL);
}
else
{
snprintf(s, 6uLL, "[%.2d] ", v3);
v4 = strlen(s);
write(1, s, v4);
}
}
else
{
write(1, "[ ] ", 5uLL);
}
++v2;
}
while ( v2 != (int *)(unsigned int)((_DWORD)&tuiles + 80) );
return write(1, "\n", 1uLL);
}

original_code:
void afficherTuiles()
{
 int i;
 char *m = "Emplacements : ";
 write(1, m, strlen(m));
 for (i = 0; i < 20; i++) {
  char m2[6];
  snprintf(m2, 6, "[%.2d] ", i + 1);
  write(1, m2, strlen(m2));
 }
 write(1, "\n", 1);
 char *m3 = "Valeurs      : ";
 write(1, m3, strlen(m));
 for(i = 0; i < 20; i++)
  if (tuiles[i] == 0) {
   char *m4 = "[  ] ";
   write(1, m4, strlen(m4));
  } else if (tuiles[i] == 42) {
   char *m5 = "[ *] ";
   write(1, m5, strlen(m5));
  } else {
   char m6[6];
   snprintf(m6, 6, "[%.2d] ", tuiles[i]);
   write(1, m6, strlen(m6));
  }
 write(1, "\n", 1);
}


scoreJoueurs

original_code:
void scoreJoueurs(game *g, int semid)
{
 int i;
 int top = -1;
 int numTop = -1;
 char *m = "Score des joueurs\n";
 write(1, m, strlen(m));
 down(semid);
  for(i = 0; i < g->nbrJoueur; i++) {
   char m2[20 + 11 + 10 + 1] = {'\0'};
   snprintf(m2, 20 + 11 + 10 + 1, "%s a obtenu %d\n", g->nom[i], g->score[i]);
   write(1, m2, strlen(m2));
   if(g->score[i] > top) {
    numTop = i;
    top = g->score[i];
   }
  }
 char gagnant[20 + 16 +1] = {'\0'};
 snprintf(gagnant, 20 + 16 + 1, "Le gagnant est %s\n", g->nom[numTop]);
 write(1, gagnant, strlen(gagnant));
 up(semid);
}

canonical_original_code:
void scoreJoueurs(struct game *g, int semid)
{
 int i;
 int top = -1;
 int numTop = -1;
 char *m = "Score des joueurs\n";
 write(1, m, strlen(m));
 down(semid);
  for(i = 0; i < g->nbrJoueur; i++) {
   char m2[-1] = {'\0'};
   snprintf(m2, 20 + 11 + 10 + 1, "%s a obtenu %d\n", g->nom[i], g->score[i]);
   write(1, m2, strlen(m2));
   if(g->score[i] > top) {
    numTop = i;
    top = g->score[i];
   }
  }
 char gagnant[-1] = {'\0'};
 snprintf(gagnant, 20 + 16 + 1, "Le gagnant est %s\n", g->nom[numTop]);
 write(1, gagnant, strlen(gagnant));
 up(semid);
}

variable_types: top int

variable_types: numTop int

variable_types:
gagnant
char [-1]


placerTuiles

decompiled_code:
__int64 __fastcall placerTuiles(unsigned int a1)
{
size_t v1; // rax
size_t v2; // rax
int v3; // eax
int v4; // ebx
__int64 result; // rax
__int16 buf; // [rsp+Dh] [rbp-4Bh] BYREF
char v7; // [rsp+Fh] [rbp-49h]
char s[72]; // [rsp+10h] [rbp-48h] BYREF

while ( 1 )
{
while ( 1 )
{
if ( a1 == 42 )
{
write(1, "Tuile recue: Joker\n", 0x13uLL);
}
else
{
snprintf(s, 0x18uLL, "Tuile recue: %d\n", a1);
v2 = strlen(s);
write(1, s, v2);
}
afficherTuiles();
write(1, "Veuillez encoder son emplacement: ", 0x22uLL);
buf = 0;
v7 = 0;
read(1, &buf, 2uLL);
write(1, "\n", 1uLL);
v3 = atoi((const char *)&buf);
if ( (unsigned int)(v3 - 1) <= 0x13 )
break;
snprintf(s, 0x28uLL, "Veuillez entrez un nombre entre 1 et %d\n", 20LL);
v1 = strlen(s);
write(1, s, v1);
}
v4 = v3 - 1;
if ( !tuiles[v3 - 1] )
break;
write(1, "Emplacement deja prit. Choisissez en un autre\n", 0x2EuLL);
}
write(1, "Tuile placee. En attente du placement des autres joueurs\n", 0x39uLL);
result = v4;
tuiles[v4] = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
size_t v1; // rax
size_t v2; // rax
int v3; // eax
int v4; // ebx
__int64 result; // rax
__int16 buf; // [rsp+Dh] [rbp-4Bh] BYREF
char v7; // [rsp+Fh] [rbp-49h]
char s[72]; // [rsp+10h] [rbp-48h] BYREF

while ( 1 )
{
while ( 1 )
{
if ( a1 == 42 )
{
write(1, "Tuile recue: Joker\n", 0x13uLL);
}
else
{
snprintf(s, 0x18uLL, "Tuile recue: %d\n", a1);
v2 = strlen(s);
write(1, s, v2);
}
func1();
write(1, "Veuillez encoder son emplacement: ", 0x22uLL);
buf = 0;
v7 = 0;
read(1, &buf, 2uLL);
write(1, "\n", 1uLL);
v3 = atoi((const char *)&buf);
if ( (unsigned int)(v3 - 1) <= 0x13 )
break;
snprintf(s, 0x28uLL, "Veuillez entrez un nombre entre 1 et %d\n", 20LL);
v1 = strlen(s);
write(1, s, v1);
}
v4 = v3 - 1;
if ( !tuiles[v3 - 1] )
break;
write(1, "Emplacement deja prit. Choisissez en un autre\n", 0x2EuLL);
}
write(1, "Tuile placee. En attente du placement des autres joueurs\n", 0x39uLL);
result = v4;
tuiles[v4] = a1;
return result;
}

original_code:
void placerTuiles(int tuile) {
 int placement;
 while(1) {
  if (tuile == 42) {
   char *m1 = "Tuile recue: Joker\n";
   write(1, m1, strlen(m1));
  } else {
   char m[14 + 10];
   snprintf(m, sizeof(m), "Tuile recue: %d\n", tuile);
   write(1, m, strlen(m));
  }
  afficherTuiles();
  char *msg = "Veuillez encoder son emplacement: ";
  write(1, msg, strlen(msg));
  char buffer[3] = {'\0'};
  read(1, buffer, 2);
  write(1, "\n", 1);
  placement = atoi(buffer);
  if (placement < 1 || placement > 20) {
   char m2[40];
   snprintf(m2, 40, "Veuillez entrez un nombre entre 1 et %d\n", 20);
   write(1, m2, strlen(m2));
  } else if (tuiles[placement - 1] != 0) {
   char *m3 = "Emplacement deja prit. Choisissez en un autre\n";
   write(1, m3, strlen(m3));
  } else {
   char *m4 = "Tuile placee. En attente du placement des autres joueurs\n";
   write(1, m4, strlen(m4));
   break;
  }
 }
 tuiles[placement - 1] = tuile;
}

canonical_original_code:
void placerTuiles(int tuile) {
 int placement;
 while(1) {
  if (tuile == 42) {
   char *m1 = "Tuile recue: Joker\n";
   write(1, m1, strlen(m1));
  } else {
   char m[-1];
   snprintf(m, sizeof(m), "Tuile recue: %d\n", tuile);
   write(1, m, strlen(m));
  }
  afficherTuiles();
  char *msg = "Veuillez encoder son emplacement: ";
  write(1, msg, strlen(msg));
  char buffer[3] = {'\0'};
  read(1, buffer, 2);
  write(1, "\n", 1);
  placement = atoi(buffer);
  if (placement < 1 || placement > 20) {
   char m2[40];
   snprintf(m2, 40, "Veuillez entrez un nombre entre 1 et %d\n", 20);
   write(1, m2, strlen(m2));
  } else if (tuiles[placement - 1] != 0) {
   char *m3 = "Emplacement deja prit. Choisissez en un autre\n";
   write(1, m3, strlen(m3));
  } else {
   char *m4 = "Tuile placee. En attente du placement des autres joueurs\n";
   write(1, m4, strlen(m4));
   break;
  }
 }
 tuiles[placement - 1] = tuile;
}

variable_types: tuile int

variable_types: placement int

variable_types:
m
char [-1]

variable_types:
msg
char *

variable_types:
buffer
char [3]


calculerScore

original_code:
int calculerScore() {
 int suite = 0;
 int score = 0;
 int tableauPoints[20] = {0, 1, 3, 5, 7, 9, 11, 15, 20, 25, 30, 35, 40, 50, 60, 70, 85, 100, 150, 300};
 int i;
 for(i = 0; i <= 18; i++) {
  if(tuiles[i+1] == 42) {
   suite++;
   tuiles[i+1] = tuiles[i];
  }
  else if(tuiles[i+1] >= tuiles[i]) {
   suite++;
  } else {
   score = score + tableauPoints[suite];
   suite = 0;
  }
 }
 score = score + tableauPoints[suite];
 return score;
}

variable_types: suite int

variable_types: score int

variable_types:
tableauPoints
int [20]

variable_types: i int


traiterMessage

decompiled_code:
__int64 __fastcall traiterMessage(unsigned int a1, const char *a2, _DWORD *a3, unsigned int a4)
{
__int64 result; // rax
int v8; // eax
unsigned int v9; // eax
size_t v10; // rsi
void *v11; // rsp
size_t v12; // rax
_QWORD v13[6]; // [rsp-68h] [rbp-68h] BYREF
__int16 v14; // [rsp-38h] [rbp-38h]

if ( !*a2 )
return 0xFFFFFFFFLL;
switch ( *a2 )
{
case '1':
if ( a2[2] == 48 )
{
write(1, "Le serveur a refuse votre inscription\n", 0x26uLL);
}
else
{
write(1, "Vous etes maintenant inscrit pour la prochaine partie\n", 0x36uLL);
listerJoueurs(a3, a4);
}
goto LABEL_8;
case '2':
write(1, "La partie est annulee. Il semblerait que comme vous etes le dernier joueur, vous avez gagne\n", 0x5CuLL);
return 2LL;
case '3':
v8 = atoi(a2 + 2);
placerTuiles(v8);
envoyerMessage(a1, "3");
goto LABEL_8;
case '4':
write(1, "Fin de la partie\n", 0x11uLL);
v9 = calculerScore();
v13[0] = 0LL;
v13[1] = 0LL;
v13[2] = 0LL;
v13[3] = 0LL;
v13[4] = 0LL;
v13[5] = 0LL;
v14 = 0;
sprintf((char *)v13, "5=%d", v9);
envoyerMessage(a1, v13);
LABEL_8:
result = 0LL;
break;
case '5':
scoreJoueurs(a3, a4);
result = 2LL;
break;
default:
v10 = strlen(a2) + 18;
v11 = alloca(v10);
snprintf((char *)v13, v10, "Message inconnu: %s\n", a2);
v12 = strlen((const char *)v13);
write(1, v13, v12);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, const char *a2, _DWORD *a3, unsigned int a4)
{
__int64 result; // rax
int v8; // eax
unsigned int v9; // eax
size_t v10; // rsi
void *v11; // rsp
size_t v12; // rax
_QWORD v13[6]; // [rsp-68h] [rbp-68h] BYREF
__int16 v14; // [rsp-38h] [rbp-38h]

if ( !*a2 )
return 0xFFFFFFFFLL;
switch ( *a2 )
{
case '1':
if ( a2[2] == 48 )
{
write(1, "Le serveur a refuse votre inscription\n", 0x26uLL);
}
else
{
write(1, "Vous etes maintenant inscrit pour la prochaine partie\n", 0x36uLL);
func0(a3, a4);
}
goto LABEL_8;
case '2':
write(1, "La partie est annulee. Il semblerait que comme vous etes le dernier joueur, vous avez gagne\n", 0x5CuLL);
return 2LL;
case '3':
v8 = atoi(a2 + 2);
func3(v8);
envoyerMessage(a1, "3");
goto LABEL_8;
case '4':
write(1, "Fin de la partie\n", 0x11uLL);
v9 = func4();
v13[0] = 0LL;
v13[1] = 0LL;
v13[2] = 0LL;
v13[3] = 0LL;
v13[4] = 0LL;
v13[5] = 0LL;
v14 = 0;
sprintf((char *)v13, "5=%d", v9);
envoyerMessage(a1, v13);
LABEL_8:
result = 0LL;
break;
case '5':
func2(a3, a4);
result = 2LL;
break;
default:
v10 = strlen(a2) + 18;
v11 = alloca(v10);
snprintf((char *)v13, v10, "Message inconnu: %s\n", a2);
v12 = strlen((const char *)v13);
write(1, v13, v12);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
int traiterMessage(int sckClient, char *message, game *g, int semid)
{
 int i;
 if (strlen(message) <= 0)
  return -1;
 switch(message[0]) {
  case '1':
   if (message[2] == '0') {
    char *m = "Le serveur a refuse votre inscription\n";
    write(1, m, strlen(m));
   } else {
    char *m2 = "Vous etes maintenant inscrit pour la prochaine partie\n";
    write(1, m2, strlen(m2));
    listerJoueurs(g, semid);
   }
   break;
  case '2':
   i = 1;
   char *m3 = "La partie est annulee. Il semblerait que comme vous etes le dernier joueur, vous avez gagne\n";
   write(1, m3, strlen(m3));
   return 2;
  case '3':
   message += 2;
   placerTuiles(atoi(message));
   envoyerMessage(sckClient, "3");
   break;
  case '4':
   i = 1;
   char *m4 = "Fin de la partie\n";
   write(1, m4, strlen(m4));
   int score = calculerScore();
   char msg[50] = {'\0'};
   sprintf(msg, "5=%d", score);
   envoyerMessage(sckClient, msg);
   break;
  case '5':
   scoreJoueurs(g, semid);
   return 2;
  default:
   i = 1;
   char m5[17 + strlen(message) + 1];
   snprintf(m5, sizeof(m5), "Message inconnu: %s\n", message);
   write(1, m5, strlen(m5));
   return -1;
 }
 return 0;
}

canonical_original_code:
int traiterMessage(int sckClient, char *message, struct game *g, int semid)
{
 int i;
 if (strlen(message) <= 0)
  return -1;
 switch(message[0]) {
  case '1':
   if (message[2] == '0') {
    char *m = "Le serveur a refuse votre inscription\n";
    write(1, m, strlen(m));
   } else {
    char *m2 = "Vous etes maintenant inscrit pour la prochaine partie\n";
    write(1, m2, strlen(m2));
    listerJoueurs(g, semid);
   }
   break;
  case '2':
   i = 1;
   char *m3 = "La partie est annulee. Il semblerait que comme vous etes le dernier joueur, vous avez gagne\n";
   write(1, m3, strlen(m3));
   return 2;
  case '3':
   message += 2;
   placerTuiles(atoi(message));
   envoyerMessage(sckClient, "3");
   break;
  case '4':
   i = 1;
   char *m4 = "Fin de la partie\n";
   write(1, m4, strlen(m4));
   int score = calculerScore();
   char msg[50] = {'\0'};
   sprintf(msg, "5=%d", score);
   envoyerMessage(sckClient, msg);
   break;
  case '5':
   scoreJoueurs(g, semid);
   return 2;
  default:
   i = 1;
   char m5[-1];
   snprintf(m5, sizeof(m5), "Message inconnu: %s\n", message);
   write(1, m5, strlen(m5));
   return -1;
 }
 return 0;
}

variable_types: sckClient int

variable_types:
message
char *

variable_types: struct game { int nbrJoueur; int score[4]; char nom[4][20]; } *

variable_types: semid int

variable_types:
msg
char [50]


accepterClient

decompiled_code:
__int64 __fastcall accepterClient(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
v2 = v1;
if ( v1 == -1 )
perror("accept");
else
printf("Le client avec le socket: %d vient de se connecter\n", v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
v2 = v1;
if ( v1 == -1 )
perror("accept");
else
printf("Le client avec le socket: %d vient de se connecter\n", v1);
return v2;
}

original_code:
int accepterClient(int socket) {
 SOCKADDR_IN sin;
 int structsize = sizeof(sin);
 int sckClient = accept(socket, (SOCKADDR *)&sin, &structsize);
 if (sckClient == -1) {
  perror("accept");
  return -1;
 }
 printf("Le client avec le socket: %d vient de se connecter\n", sckClient);
 return sckClient;
}

canonical_original_code:
int accepterClient(int socket) {
 struct sockaddr_in sin;
 int structsize = sizeof(sin);
 int sckClient = accept(socket, (SOCKADDR *)&sin, &structsize);
 if (sckClient == -1) {
  perror("accept");
  return -1;
 }
 printf("Le client avec le socket: %d vient de se connecter\n", sckClient);
 return sckClient;
}

variable_types: socket int

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

variable_types: structsize int

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


fermerSocket

decompiled_code:
__int64 __fastcall fermerSocket(int a1)
{
unsigned int v1; // ebx

v1 = close(a1);
if ( v1 == -1 )
perror("closesocket");
return v1;
}

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

v1 = close(a1);
if ( v1 == -1 )
perror("closesocket");
return v1;
}

original_code:
int fermerSocket(int socket)
{
 int c = close(socket);
 if (c == -1)
  perror("closesocket");
 return c;
}

variable_types: c int


ouvrirSocket

decompiled_code:
__int64 __fastcall ouvrirSocket(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("socket");
}
else
{
optval[0] = 1;
setsockopt(v1, 1, 2, optval, 4u);
*(_DWORD *)&v4.sa_data[2] = inet_addr("127.0.0.1");
v4.sa_family = 2;
*(_WORD *)v4.sa_data = _bswap_16(a1);
if ( bind(v2, &v4, 0x10u) )
{
perror("bind");
fermerSocket(v2);
v2 = -1;
}
else if ( listen(v2, 5) == -1 )
{
perror("listen");
fermerSocket(v2);
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("socket");
}
else
{
optval[0] = 1;
setsockopt(v1, 1, 2, optval, 4u);
*(_DWORD *)&v4.sa_data[2] = inet_addr("127.0.0.1");
v4.sa_family = 2;
*(_WORD *)v4.sa_data = func0(a1);
if ( bind(v2, &v4, 0x10u) )
{
perror("bind");
func2(v2);
v2 = -1;
}
else if ( listen(v2, 5) == -1 )
{
perror("listen");
func2(v2);
v2 = -1;
}
}
return v2;
}

original_code:
int ouvrirSocket(unsigned short port)
{
 int sck = socket(2, SOCK_STREAM, 0);
 if (sck == -1) {
  perror("socket");
  return -1;
 }
 int on = 1;
 setsockopt(sck, 1, 2, &on, sizeof(on) );
 SOCKADDR_IN sin;
 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
 sin.sin_family = 2;
 sin.sin_port = htons(port);
 if (bind(sck, (SOCKADDR *)&sin, sizeof(sin))) {
  perror("bind");
  fermerSocket(sck);
  return -1;
 }
 if (listen(sck, 5) == -1) {
  perror("listen");
  fermerSocket(sck);
  return -1;
 }
 return sck;
}

canonical_original_code:
int ouvrirSocket(unsigned short port)
{
 int sck = socket(2, SOCK_STREAM, 0);
 if (sck == -1) {
  perror("socket");
  return -1;
 }
 int on = 1;
 setsockopt(sck, 1, 2, &on, sizeof(on) );
 struct sockaddr_in sin;
 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
 sin.sin_family = 2;
 sin.sin_port = htons(port);
 if (bind(sck, (struct sockaddr *)&sin, sizeof(sin))) {
  perror("bind");
  fermerSocket(sck);
  return -1;
 }
 if (listen(sck, 5) == -1) {
  perror("listen");
  fermerSocket(sck);
  return -1;
 }
 return sck;
}

variable_types:
port
unsigned short

variable_types: sck int

variable_types: on int

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


recevoirMessage

decompiled_code:
__int64 __fastcall recevoirMessage(int a1, void *a2)
{
unsigned int v2; // ebx

v2 = recv(a1, a2, 0x32uLL, 0);
if ( v2 == -1 )
perror("recv");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, void *a2)
{
unsigned int v2; // ebx

v2 = recv(a1, a2, 0x32uLL, 0);
if ( v2 == -1 )
perror("recv");
return v2;
}

original_code:
int recevoirMessage(int socket, char *message)
{
 int result = recv(socket, message, 50, 0);
 if (result == -1) {
  perror("recv");
  return -1;
 }
 return result;
}

variable_types: result int


fermerSocket

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

v1 = close(a1);
if ( v1 == -1 )
perror("closesocket");
return v1;
}

canonical_original_code:
int fermerSocket(int socket)
{
 int c = close(socket);
 if (c == -1)
  perror("closesocket");
 return c;
}


connexion

decompiled_code:
__int64 __fastcall connexion(char *cp, __int16 a2)
{
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 == -1 )
{
perror("socket");
}
else
{
*(_DWORD *)&v4.sa_data[2] = inet_addr(cp);
v4.sa_family = 2;
*(_WORD *)v4.sa_data = _bswap_16(a2);
if ( connect(v2, &v4, 0x10u) )
{
if ( *_errno_location() == 111 )
puts("Aucun serveur present");
else
perror("connect");
fermerSocket(v2);
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *cp, __int16 a2)
{
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 == -1 )
{
perror("socket");
}
else
{
*(_DWORD *)&v4.sa_data[2] = inet_addr(cp);
v4.sa_family = 2;
*(_WORD *)v4.sa_data = func0(a2);
if ( connect(v2, &v4, 0x10u) )
{
if ( *_errno_location() == 111 )
puts("Aucun serveur present");
else
perror("connect");
func1(v2);
v2 = -1;
}
}
return v2;
}

original_code:
int connexion(char *addresse, unsigned short port)
{
 int sck = socket(2, SOCK_STREAM, 0);
 if (sck == -1) {
  perror("socket");
  return -1;
 }
 SOCKADDR_IN sin;
 sin.sin_addr.s_addr = inet_addr(addresse);
 sin.sin_family = 2;
 sin.sin_port = htons(port);
 if (connect(sck, (SOCKADDR *)&sin, sizeof(sin))) {
  if ((*__errno_location ()) == 111)
   printf("Aucun serveur present\n");
  else
   perror("connect");
  fermerSocket(sck);
  return -1;
 }
 return sck;
}

canonical_original_code:
int connexion(char *addresse, unsigned short port)
{
 int sck = socket(2, SOCK_STREAM, 0);
 if (sck == -1) {
  perror("socket");
  return -1;
 }
 struct sockaddr_in sin;
 sin.sin_addr.s_addr = inet_addr(addresse);
 sin.sin_family = 2;
 sin.sin_port = htons(port);
 if (connect(sck, (struct sockaddr *)&sin, sizeof(sin))) {
  if ((*__errno_location ()) == 111)
   printf("Aucun serveur present\n");
  else
   perror("connect");
  fermerSocket(sck);
  return -1;
 }
 return sck;
}

variable_types:
addresse
char *


recevoirMessage

canonical_decompiled_code:
__int64 __fastcall func4(int a1, void *a2)
{
unsigned int v2; // ebx

v2 = recv(a1, a2, 0x32uLL, 0);
if ( v2 == -1 )
perror("recv");
return v2;
}

canonical_original_code:
int recevoirMessage(int socket, char *message)
{
 int result = recv(socket, message, 50, 0);
 if (result == -1) {
  perror("recv");
  return -1;
 }
 return result;
}


decompiled_code:
__int64 create_tcp_socket()
{
unsigned int v0; // ebx

v0 = socket(2, 1, 6);
if ( v0 == -1 )
perror("Error while creating TCP socket.");
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx

v0 = socket(2, 1, 6);
if ( v0 == -1 )
perror("Error while creating TCP socket.");
return v0;
}

original_code:
int create_tcp_socket() {
 int sockfd;
 if ((sockfd = socket(2, SOCK_STREAM, IPPROTO_TCP)) == -1)
  perror("Error while creating TCP socket.");
 return sockfd;
}

variable_types: sockfd int


decompiled_code:
__int16 __fastcall populate_sockaddr_in(__int64 a1, const char *a2, __int16 a3)
{
__int16 result; // ax
struct hostent *v4; // rax

*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)a1 = 2;
result = _bswap_16(a3);
*(_WORD *)(a1 + 2) = result;
if ( a2 )
{
v4 = gethostbyname(a2);
result = (unsigned __int16)memmove((void *)(a1 + 4), *(const void **)v4->h_addr_list, v4->h_length);
}
return result;
}

canonical_decompiled_code:
__int16 __fastcall func2(__int64 a1, const char *a2, __int16 a3)
{
__int16 result; // ax
struct hostent *v4; // rax

*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)a1 = 2;
result = func0(a3);
*(_WORD *)(a1 + 2) = result;
if ( a2 )
{
v4 = gethostbyname(a2);
result = (unsigned __int16)memmove((void *)(a1 + 4), *(const void **)v4->h_addr_list, v4->h_length);
}
return result;
}

original_code:
void populate_sockaddr_in(struct sockaddr_in *sk_address, const char* address,
  int port) {
 struct hostent *remote_entry;
 memset(sk_address,0, sizeof(struct sockaddr_in));
 sk_address->sin_family = 2;
 sk_address->sin_port = htons(port);
 if (address != ((void *)0)) {
  remote_entry = gethostbyname(address);
  bcopy((char *) remote_entry->h_addr_list[0],
  (char *)&sk_address->sin_addr.s_addr,
  remote_entry->h_length);
 }
 else
 {
  sk_address->sin_addr.s_addr = ((in_addr_t) 0x00000000);
 }
}

canonical_original_code:
void populate_sockaddr_in(struct sockaddr_in *sk_address, char *address, int port) {
 struct hostent *remote_entry;
 memset(sk_address,0, sizeof(struct sockaddr_in));
 sk_address->sin_family = 2;
 sk_address->sin_port = htons(port);
 if (address != ((void *)0)) {
  remote_entry = gethostbyname(address);
  bcopy((char *) remote_entry->h_addr_list[0],
  (char *)&sk_address->sin_addr.s_addr,
  remote_entry->h_length);
 }
 else
 {
  sk_address->sin_addr.s_addr = ((unsigned int) 0x00000000);
 }
}

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

variable_types:
address
char *

variable_types: port int

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


decompiled_code:
__int64 __fastcall bind_address(int fd, __int64 a2, __int64 a3)
{
int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&v5.sa_family = a2;
*(_QWORD *)&v5.sa_data[6] = a3;
v3 = bind(fd, &v5, 0x10u);
if ( v3 < 0 )
close(fd);
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2, __int64 a3)
{
int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&v5.sa_family = a2;
*(_QWORD *)&v5.sa_data[6] = a3;
v3 = bind(fd, &v5, 0x10u);
if ( v3 < 0 )
close(fd);
return (unsigned int)v3;
}

original_code:
int bind_address(int tcp_sock_fd, struct sockaddr_in tcp_sender) {
 int r_code = bind(tcp_sock_fd, (struct sockaddr *) &tcp_sender, sizeof(tcp_sender));
 if(r_code<0)
  close(tcp_sock_fd);
 return r_code;
}


writeshm

decompiled_code:
char *__fastcall writeshm(char *a1, const char *a2)
{
return strcpy(a1, a2);
}

canonical_decompiled_code:
char *__fastcall func2(char *a1, const char *a2)
{
return strcpy(a1, a2);
}

original_code:
void writeshm(char *segptr, char *text) {
 strcpy(segptr, text);
}

variable_types:
segptr
char *

variable_types:
text
char *


readshm

decompiled_code:
__int64 __fastcall readshm(char *src, char *dest)
{
*(_QWORD *)dest = 0LL;
*((_WORD *)dest + 4) = 0;
strcpy(dest, src);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src, char *dest)
{
*(_QWORD *)dest = 0LL;
*((_WORD *)dest + 4) = 0;
strcpy(dest, src);
return 0LL;
}

original_code:
int readshm(char *segptr, char *s) {
 memset(s, 0, 10);
 strcpy(s, segptr);
 return segptr == ((void *)0);
}

variable_types:
s
char *


decompiled_code:
int __fastcall do_client(__int64 a1, __int64 a2)
{
__pid_t v2; // er12
int v3; // ebx
key_t v4; // eax
int v5; // eax
char *v6; // rbp
unsigned int v7; // eax
__int64 v8; // r13
__int64 v9; // r13
struct sockaddr v11; // [rsp+0h] [rbp-148h] BYREF
char buf[256]; // [rsp+10h] [rbp-138h] BYREF
_BYTE v13[56]; // [rsp+110h] [rbp-38h] BYREF

memset(buf, 0, sizeof(buf));
v2 = getpid();
*(_QWORD *)&v11.sa_family = 0LL;
*(_QWORD *)&v11.sa_data[6] = 0LL;
v3 = create_tcp_socket(v13, a2);
v4 = ftok("./test", 83);
v5 = shmget(v4, 0xAuLL, 438);
if ( v5 < 0 )
{
perror("shmget");
exit(1);
}
v6 = (char *)shmat(v5, 0LL, 0);
if ( v6 == (char *)-1LL )
{
perror("shmat");
exit(1);
}
do
{
readshm(v6, &v13[6]);
printf("do_client(): server port number read from shared memory is: %s\n", &v13[6]);
puts("do_client(): trying to connect the server");
v7 = atoi(&v13[6]);
populate_sockaddr_in(&v11, "localhost", v7);
}
while ( connect(v3, &v11, 0x10u) < 0 );
if ( recv(v3, buf, 0xFFuLL, 0) < 0 )
perror("Error in receiving data for server");
v8 = atol(buf);
printf("do_client: data received from server: %ld\n", v8);
v9 = v2 + v8;
printf("do_client: computed nounce is: %ld\n", v9);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%ld %d\n", v9, (unsigned int)v2);
printf("do_client: data to send to manager: %s\n", buf);
if ( send(v3, buf, 0xFFuLL, 0) < 0 )
perror("Error sending data to server");
return close(v3);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2)
{
__pid_t v2; // er12
int v3; // ebx
key_t v4; // eax
int v5; // eax
char *v6; // rbp
unsigned int v7; // eax
__int64 v8; // r13
__int64 v9; // r13
struct sockaddr v11; // [rsp+0h] [rbp-148h] BYREF
char buf[256]; // [rsp+10h] [rbp-138h] BYREF
_BYTE v13[56]; // [rsp+110h] [rbp-38h] BYREF

memset(buf, 0, sizeof(buf));
v2 = getpid();
*(_QWORD *)&v11.sa_family = 0LL;
*(_QWORD *)&v11.sa_data[6] = 0LL;
v3 = create_tcp_socket(v13, a2);
v4 = ftok("./test", 83);
v5 = shmget(v4, 0xAuLL, 438);
if ( v5 < 0 )
{
perror("shmget");
exit(1);
}
v6 = (char *)shmat(v5, 0LL, 0);
if ( v6 == (char *)-1LL )
{
perror("shmat");
exit(1);
}
do
{
func3(v6, &v13[6]);
printf("do_client(): server port number read from shared memory is: %s\n", &v13[6]);
puts("do_client(): trying to connect the server");
v7 = atoi(&v13[6]);
populate_sockaddr_in(&v11, "localhost", v7);
}
while ( connect(v3, &v11, 0x10u) < 0 );
if ( recv(v3, buf, 0xFFuLL, 0) < 0 )
perror("Error in receiving data for server");
v8 = atol(buf);
printf("do_client: data received from server: %ld\n", v8);
v9 = v2 + v8;
printf("do_client: computed nounce is: %ld\n", v9);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%ld %d\n", v9, (unsigned int)v2);
printf("do_client: data to send to manager: %s\n", buf);
if ( send(v3, buf, 0xFFuLL, 0) < 0 )
perror("Error sending data to server");
return close(v3);
}

original_code:
void do_client() {
 int shmid, server_port = 0;
 long nounce = 0;
 key_t key;
 char *shm, s[10], buffer[256];
 memset(buffer, 0, sizeof(buffer));
 int pid = getpid();
 struct sockaddr_in tcp_server;
 memset(&tcp_server,0,sizeof(tcp_server));
 int tcp_client_sock_fd = create_tcp_socket();
 key = ftok("./test", 'S');
 if ((shmid = shmget(key, 10, 0666)) < 0) {
  perror("shmget");
  exit(1);
 }
 if ((shm = shmat(shmid, ((void *)0), 0)) == (char *) -1) {
  perror("shmat");
  exit(1);
 }
 do {
  readshm(shm, s);
  printf(
    "do_client(): server port number read from shared memory is: %s\n",
    s);
  printf("do_client(): trying to connect the server\n");
  server_port = atoi(s);
  populate_sockaddr_in(&tcp_server, "localhost", server_port);
 } while (connect(tcp_client_sock_fd, (struct sockaddr *) &tcp_server,
   sizeof(tcp_server)) < 0);
 if (recv(tcp_client_sock_fd, buffer, 256 - 1,0) < 0) {
  perror("Error in receiving data for server");
 }
 nounce = atol(buffer);
 printf("do_client: data received from server: %ld\n", nounce);
 nounce += pid;
 printf("do_client: computed nounce is: %ld\n", nounce);
 memset(buffer,0,sizeof(buffer));
 sprintf(buffer, "%ld %d\n", nounce, pid);
 printf("do_client: data to send to manager: %s\n",buffer);
 if (send(tcp_client_sock_fd, buffer, 256 - 1,0) < 0) {
  perror("Error sending data to server");
 }
 close(tcp_client_sock_fd);
}

canonical_original_code:
void do_client() {
 int shmid; int server_port = 0;
 long nounce = 0;
 int key;
 char *shm; char s[10]; char buffer[256];
 memset(buffer, 0, sizeof(buffer));
 int pid = getpid();
 struct sockaddr_in tcp_server;
 memset(&tcp_server,0,sizeof(tcp_server));
 int tcp_client_sock_fd = create_tcp_socket();
 key = ftok("./test", 'S');
 if ((shmid = shmget(key, 10, 0666)) < 0) {
  perror("shmget");
  exit(1);
 }
 if ((shm = shmat(shmid, ((void *)0), 0)) == (char *) -1) {
  perror("shmat");
  exit(1);
 }
 do {
  readshm(shm, s);
  printf(
    "do_client(): server port number read from shared memory is: %s\n",
    s);
  printf("do_client(): trying to connect the server\n");
  server_port = atoi(s);
  populate_sockaddr_in(&tcp_server, "localhost", server_port);
 } while (connect(tcp_client_sock_fd, (struct sockaddr *) &tcp_server,
   sizeof(tcp_server)) < 0);
 if (recv(tcp_client_sock_fd, buffer, 256 - 1,0) < 0) {
  perror("Error in receiving data for server");
 }
 nounce = atol(buffer);
 printf("do_client: data received from server: %ld\n", nounce);
 nounce += pid;
 printf("do_client: computed nounce is: %ld\n", nounce);
 memset(buffer,0,sizeof(buffer));
 sprintf(buffer, "%ld %d\n", nounce, pid);
 printf("do_client: data to send to manager: %s\n",buffer);
 if (send(tcp_client_sock_fd, buffer, 256 - 1,0) < 0) {
  perror("Error sending data to server");
 }
 close(tcp_client_sock_fd);
}

variable_types: shmid int

variable_types: nounce long

variable_types: key int

variable_types:
shm
char *

variable_types:
s
char [10]

variable_types:
buffer
char [256]

variable_types: pid int


decompiled_code:
__int64 __fastcall initialize_with_seed(int a1)
{
int *v1; // rdx
__int64 result; // rax

initialized = 1;
state = a1;
v1 = &state;
do
{
result = 1812433253 * (*v1 ^ ((unsigned int)*v1 >> 30)) + 1;
v1[1] = result;
++v1;
}
while ( v1 != (int *)(unsigned int)((_DWORD)&state + 2492) );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int *v1; // rdx
__int64 result; // rax

initialized = 1;
state = a1;
v1 = &state;
do
{
result = 1812433253 * (*v1 ^ ((unsigned int)*v1 >> 30)) + 1;
v1[1] = result;
++v1;
}
while ( v1 != (int *)(unsigned int)((_DWORD)&state + 2492) );
return result;
}

original_code:
void initialize_with_seed( int seed ) {
  initialized = 1;
  state[0] = seed;
  int i;
  for ( i = 1; i < (624); i++ ) {
    state[i] = ( 0x6c078965U * ( state[i-1] ^ ( state[i-1] >> 30 ) ) + 1 ) % 0x100000000U;
  }
}

canonical_original_code:
void initialize_with_seed(int seed) {
  initialized = 1;
  state[0] = seed;
  int i;
  for ( i = 1; i < (624); i++ ) {
    state[i] = ( 0x6c078965U * ( state[i-1] ^ ( state[i-1] >> 30 ) ) + 1 ) % 0x100000000U;
  }
}

variable_types: seed int

variable_types: i int


decompiled_code:
__int64 initialize_with_rand()
{
int v0; // ebx
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

initialized = 1;
v0 = open("/dev/random", 0);
read(v0, buf, 4uLL);
close(v0);
return initialize_with_seed(buf[0]);
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // ebx
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

initialized = 1;
v0 = open("/dev/random", 0);
read(v0, buf, 4uLL);
close(v0);
return func0(buf[0]);
}

original_code:
void initialize_with_rand() {
  initialized = 1;
  int random_data = open( "/dev/random", 00 );
  int random_seed;
  read( random_data, &random_seed, sizeof(random_seed) );
  close( random_data );
  initialize_with_seed( random_seed );
}


decompiled_code:
__int64 mt_initialized()
{
return (unsigned int)initialized;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)initialized;
}

original_code:
int mt_initialized() {
    return initialized;
}


decompiled_code:
__int64 extract_number()
{
unsigned int v0; // eax

if ( !mt_index )
generate_numbers();
v0 = ((state[mt_index] ^ ((unsigned int)state[mt_index] >> 11)) << 7) & 0x9D2C5680 ^ state[mt_index] ^ ((unsigned int)state[mt_index] >> 11) ^ ((((state[mt_index] ^ ((unsigned int)state[mt_index] >> 11)) << 7) & 0x9D2C5680 ^ state[mt_index] ^ ((unsigned int)state[mt_index] >> 11)) << 15) & 0xEFC60000;
++mt_index;
return (v0 >> 18) ^ v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // eax

if ( !mt_index )
func3();
v0 = ((state[mt_index] ^ ((unsigned int)state[mt_index] >> 11)) << 7) & 0x9D2C5680 ^ state[mt_index] ^ ((unsigned int)state[mt_index] >> 11) ^ ((((state[mt_index] ^ ((unsigned int)state[mt_index] >> 11)) << 7) & 0x9D2C5680 ^ state[mt_index] ^ ((unsigned int)state[mt_index] >> 11)) << 15) & 0xEFC60000;
++mt_index;
return (v0 >> 18) ^ v0;
}

original_code:
unsigned int extract_number() {
  if ( mt_index == 0 ) generate_numbers();
  unsigned int num = state[mt_index];
  num = num ^ ( num >> 11 );
  num = num ^ ( (num << 7) & 0x9d2c5680U );
  num = num ^ ( (num << 15) & 0xefc60000U );
  num = num ^ ( num >> 18 );
  mt_index++;
  return num;
}

variable_types:
num
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall generate_key_string(int a1)
{
_QWORD *v1; // rax
__int64 v2; // rbx

v1 = generate_key(a1);
v2 = v1[1];
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
_QWORD *v1; // rax
__int64 v2; // rbx

v1 = generate_key(a1);
v2 = v1[1];
free(v1);
return v2;
}

original_code:
char *generate_key_string( int bits ) {
  KEY *key = generate_key( bits );
  char *key_string = key->key_chars;
  free(key);
  return key_string;
}

canonical_original_code:
char *generate_key_string(int bits) {
  struct crypt_key *key = generate_key( bits );
  char *key_string = key->key_chars;
  free(key);
  return key_string;
}

variable_types: bits int

variable_types: struct crypt_key { int bytes; char *key_chars; } *

return_type: char *


decompiled_code:
__int64 __fastcall generate_key_string_with_seed(int a1, unsigned int a2)
{
_QWORD *v2; // rax
__int64 v3; // rbx

v2 = generate_key_with_seed(a1, a2);
v3 = v2[1];
free(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, unsigned int a2)
{
_QWORD *v2; // rax
__int64 v3; // rbx

v2 = generate_key_with_seed(a1, a2);
v3 = v2[1];
free(v2);
return v3;
}

original_code:
char *generate_key_string_with_seed( int bits, int seed ) {
  KEY *key = generate_key_with_seed( bits, seed );
  char *key_string = key->key_chars;
  free(key);
  return key_string;
}

canonical_original_code:
char *generate_key_string_with_seed(int bits, int seed) {
  struct crypt_key *key = generate_key_with_seed( bits, seed );
  char *key_string = key->key_chars;
  free(key);
  return key_string;
}

user_defined_types: struct crypt_key { int bytes; char *key_chars; }


decompiled_code:
int __fastcall print_most_likely(__int64 a1, int a2)
{
unsigned int i; // ebx
__int64 v3; // rax
const char *v4; // rbp
int result; // eax

for ( i = 1; i != 26; ++i )
{
v3 = get_shifted_alphabet(i);
v4 = (const char *)encrypt_with_alphabet(a1, v3);
result = plaintext_likelihood(v4);
if ( result > a2 )
result = printf("shift: %d - %s", 26 - i, v4);
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, int a2)
{
unsigned int i; // ebx
__int64 v3; // rax
const char *v4; // rbp
int result; // eax

for ( i = 1; i != 26; ++i )
{
v3 = get_shifted_alphabet(i);
v4 = (const char *)encrypt_with_alphabet(a1, v3);
result = func9(v4);
if ( result > a2 )
result = printf("shift: %d - %s", 26 - i, v4);
}
return result;
}

original_code:
void print_most_likely(char *in, int threshold) {
    int i;
    for (i = 1; i < 26; i++) {
        char *shifted_alphabet = get_shifted_alphabet(i);
        char *ciphertext = encrypt_with_alphabet(in, shifted_alphabet);
        if (plaintext_likelihood(ciphertext) > threshold) {
            printf("shift: %d - %s", 26 - i, ciphertext);
        }
    }
}

variable_types:
in
char *

variable_types: threshold int

variable_types:
ciphertext
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
int v4; // ebx
unsigned int v5; // er14
int v6; // eax
__int64 v8; // rax
__int64 v9; // rbp
__int64 v10; // rax
const char *v11; // rax

v3 = 2;
v4 = 0;
v5 = 3;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "bt:n:");
if ( v6 == -1 )
break;
if ( v6 == 110 )
{
v5 = atoi(optarg);
}
else if ( v6 > 110 )
{
if ( v6 == 116 )
v3 = atoi(optarg);
}
else
{
if ( v6 == 63 )
{
printf("Error with flag: %c (invalid or argument missing)\n", (unsigned int)optopt);
return -1;
}
if ( v6 == 98 )
v4 = 1;
}
}
if ( v4 )
{
v8 = slurp_stdin();
print_most_likely(v8, v3);
v4 = 0;
}
else
{
v9 = get_shifted_alphabet(v5);
v10 = slurp_stdin();
v11 = (const char *)encrypt_with_alphabet(v10, v9);
printf("%s", v11);
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
int v4; // ebx
unsigned int v5; // er14
int v6; // eax
__int64 v8; // rax
__int64 v9; // rbp
__int64 v10; // rax
const char *v11; // rax

v3 = 2;
v4 = 0;
v5 = 3;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "bt:n:");
if ( v6 == -1 )
break;
if ( v6 == 110 )
{
v5 = atoi(optarg);
}
else if ( v6 > 110 )
{
if ( v6 == 116 )
v3 = atoi(optarg);
}
else
{
if ( v6 == 63 )
{
printf("Error with flag: %c (invalid or argument missing)\n", (unsigned int)optopt);
return -1;
}
if ( v6 == 98 )
v4 = 1;
}
}
if ( v4 )
{
v8 = func14();
func7(v8, v3);
v4 = 0;
}
else
{
v9 = get_shifted_alphabet(v5);
v10 = func14();
v11 = (const char *)encrypt_with_alphabet(v10, v9);
printf("%s", v11);
}
return v4;
}

original_code:
int main(int argc, char *argv[]) {
    int c, shift_number = 3;
    int brute_force_break = 0, threshold = 2;
    while ((c = getopt(argc, argv, "bt:n:")) != -1)
        switch (c) {
            case 'b':
                brute_force_break = 1;
                break;
            case 't':
                threshold = atoi(optarg);
                break;
            case 'n':
                shift_number = atoi(optarg);
                break;
            case '?':
                printf("Error with flag: %c (invalid or argument missing)\n",
                    optopt);
                return -1;
        }
    if (brute_force_break) {
        print_most_likely(slurp_stdin(), threshold);
    } else {
        char *shifted_alphabet = get_shifted_alphabet(shift_number);
        printf("%s", encrypt_with_alphabet(slurp_stdin(), shifted_alphabet));
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int c; int shift_number = 3;
    int brute_force_break = 0; int threshold = 2;
    while ((c = getopt(argc, argv, "bt:n:")) != -1)
        switch (c) {
            case 'b':
                brute_force_break = 1;
                break;
            case 't':
                threshold = atoi(optarg);
                break;
            case 'n':
                shift_number = atoi(optarg);
                break;
            case '?':
                printf("Error with flag: %c (invalid or argument missing)\n",
                    optopt);
                return -1;
        }
    if (brute_force_break) {
        print_most_likely(slurp_stdin(), threshold);
    } else {
        char *shifted_alphabet = get_shifted_alphabet(shift_number);
        printf("%s", encrypt_with_alphabet(slurp_stdin(), shifted_alphabet));
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int


decompiled_code:
_BYTE *get_empty_alphabet()
{
_BYTE *result; // rax

result = malloc(0x35uLL);
result[52] = 0;
return result;
}

canonical_decompiled_code:
_BYTE *func8()
{
_BYTE *result; // rax

result = malloc(0x35uLL);
result[52] = 0;
return result;
}

original_code:
char *get_empty_alphabet() {
 char *empty_alphabet = malloc(ALPHABET_STRING_LENGTH);
 empty_alphabet[ALPHABET_STRING_LENGTH - 1] = '\0';
 return empty_alphabet;
}


decompiled_code:
__int64 __fastcall plaintext_likelihood(const char *a1)
{
const char *v1; // r12
const char **v2; // rbx
unsigned int v3; // ebp
__int64 v5[8]; // [rsp+0h] [rbp-68h] BYREF
char v6; // [rsp+40h] [rbp-28h] BYREF

v5[0] = (__int64)"th";
v5[1] = (__int64)"the";
v5[2] = (__int64)"er";
v5[3] = (__int64)"on";
v5[4] = (__int64)"an";
v5[5] = (__int64)"ss";
v5[6] = (__int64)"ee";
v5[7] = (__int64)"tt";
v1 = lowercase_string(a1);
v2 = (const char **)v5;
v3 = 0;
do
v3 -= (strstr(v1, *v2++) == 0LL) - 1;
while ( v2 != (const char **)&v6 );
return v3;
}

original_code:
int plaintext_likelihood(char *in) {
    char *common_letter_combos[8] = {"th", "the", "er", "on", "an", "ss", "ee", "tt"};
    char *low = lowercase_string(in);
    int score = 0, index;
    for (index = 0; index < 8; index++) {
        score += (strstr(low, common_letter_combos[index]) != ((void *)0)) ? 1 : 0;
    }
    return score;
}

canonical_original_code:
int plaintext_likelihood(char *in) {
    char *common_letter_combos[8] = {"th", "the", "er", "on", "an", "ss", "ee", "tt"};
    char *low = lowercase_string(in);
    int score = 0; int index;
    for (index = 0; index < 8; index++) {
        score += (strstr(low, common_letter_combos[index]) != ((void *)0)) ? 1 : 0;
    }
    return score;
}

variable_types:
low
char *

variable_types: score int

variable_types: index int


decompiled_code:
__int64 __fastcall get_char_alphabet_index(char a1)
{
unsigned int v1; // ebx

v1 = a1;
if ( is_uppercase_letter(a1) )
return (unsigned int)(a1 - 65);
if ( is_lowercase_letter(a1) )
v1 = a1 - 71;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func12(char a1)
{
unsigned int v1; // ebx

v1 = a1;
if ( func11(a1) )
return (unsigned int)(a1 - 65);
if ( func10(a1) )
v1 = a1 - 71;
return v1;
}

original_code:
int get_char_alphabet_index (char c) {
 int index;
 if (is_uppercase_letter(c)) {
  index = c - 'A';
 } else if (is_lowercase_letter(c)) {
  index = c - 'A' - 6;
 } else {
  index = c;
 }
 return index;
}

canonical_original_code:
int get_char_alphabet_index(char c) {
 int index;
 if (is_uppercase_letter(c)) {
  index = c - 'A';
 } else if (is_lowercase_letter(c)) {
  index = c - 'A' - 6;
 } else {
  index = c;
 }
 return index;
}

variable_types: c char


decompiled_code:
char *slurp_stdin()
{
char *v0; // rbx
char *v1; // rax
int v2; // er12
char *v3; // rbp
int v4; // eax
size_t v6; // [rsp+0h] [rbp-28h] BYREF
char *s; // [rsp+8h] [rbp-20h] BYREF

v0 = (char *)malloc(1uLL);
v1 = (char *)malloc(1uLL);
s = v1;
v6 = 0LL;
if ( !v1 || !v0 )
{
perror("Failed to allocate content");
exit(1);
}
*v1 = 0;
*v0 = 0;
while ( getline(&s, &v6, stdin) != -1 )
{
v2 = strlen(v0);
v3 = s;
v4 = strlen(s);
v0 = (char *)realloc(v0, v2 + v4 + 1);
strcat(v0, v3);
free(v3);
}
return v0;
}

canonical_decompiled_code:
char *func14()
{
char *v0; // rbx
char *v1; // rax
int v2; // er12
char *v3; // rbp
int v4; // eax
size_t v6; // [rsp+0h] [rbp-28h] BYREF
char *s; // [rsp+8h] [rbp-20h] BYREF

v0 = (char *)malloc(1uLL);
v1 = (char *)malloc(1uLL);
s = v1;
v6 = 0LL;
if ( !v1 || !v0 )
{
perror("Failed to allocate content");
exit(1);
}
*v1 = 0;
*v0 = 0;
while ( getline(&s, &v6, stdin) != -1 )
{
v2 = strlen(v0);
v3 = s;
v4 = strlen(s);
v0 = (char *)realloc(v0, v2 + v4 + 1);
strcat(v0, v3);
free(v3);
}
return v0;
}

original_code:
char *slurp_stdin() {
 char *memo = malloc(1), *input = malloc(1);
 size_t len = 0;
 ssize_t read;
 int new_len;
 if (input == ((void *)0) || memo == ((void *)0)) {
  perror("Failed to allocate content");
  exit(1);
 }
 input[0] = '\0';
 memo[0] = '\0';
 while((read = getline(&input, &len, stdin) != -1)) {
  new_len = strlen(memo) + strlen(input);
  memo = realloc(memo, new_len + 1);
  strcat(memo, input);
  free(input);
 }
 return memo;
}

canonical_original_code:
char *slurp_stdin() {
 char *memo = malloc(1); char *input = malloc(1);
 unsigned long len = 0;
 long read;
 int new_len;
 if (input == ((void *)0) || memo == ((void *)0)) {
  perror("Failed to allocate content");
  exit(1);
 }
 input[0] = '\0';
 memo[0] = '\0';
 while((read = getline(&input, &len, stdin) != -1)) {
  new_len = strlen(memo) + strlen(input);
  memo = realloc(memo, new_len + 1);
  strcat(memo, input);
  free(input);
 }
 return memo;
}

variable_types:
memo
char *

variable_types:
input
char *

variable_types:
len
unsigned long

variable_types: read long


gcd

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

result = a2;
if ( a1 )
result = gcd((unsigned int)((int)a2 % (int)a1), a1);
return result;
}

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

result = a2;
if ( a1 )
result = func15((unsigned int)((int)a2 % (int)a1), a1);
return result;
}

original_code:
int gcd(int a, int b) {
 if (a == 0) return b;
 return gcd(b % a, a);
}

variable_types: a int

variable_types: b int


repo: monaka/B-Free

decompiled_code:
__int64 __fastcall cre_mbf(unsigned int a1, __int64 a2)
{
return call_syscall(36LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2)
{
return call_syscall(36LL, a1, a2);
}

original_code:
ER
cre_mbf (ID id, T_CMBF *pk_cmbf)
{
  return (call_syscall ((36), id, pk_cmbf));
}

canonical_original_code:
int cre_mbf(int id, struct t_cmbf *pk_cmbf)
{
  return (call_syscall ((36), id, pk_cmbf));
}

variable_types: id int

variable_types: struct t_cmbf { void *exinf; unsigned int mbfatr; int bufsz; int maxmsz; } *


decompiled_code:
__int64 __fastcall del_mbf(unsigned int a1, __int64 a2, __int64 a3)
{
return call_syscall(37LL, a1, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3)
{
return call_syscall(37LL, a1, a3);
}

original_code:
ER
del_mbf (ID id)
{
  return (call_syscall ((37), id));
}

canonical_original_code:
int del_mbf(int id)
{
  return (call_syscall ((37), id));
}


decompiled_code:
__int64 __fastcall snd_mbf(unsigned int a1, unsigned int a2)
{
return call_syscall(38LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, unsigned int a2)
{
return call_syscall(38LL, a1, a2);
}

original_code:
ER
snd_mbf (ID id, INT size, VP msg)
{
  return (call_syscall ((38), id, size, msg));
}

canonical_original_code:
int snd_mbf(int id, int size, void *msg)
{
  return (call_syscall ((38), id, size, msg));
}

variable_types: size int

variable_types:
msg
void *


decompiled_code:
__int64 __fastcall psnd_mbf(unsigned int a1, unsigned int a2)
{
return call_syscall(39LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned int a2)
{
return call_syscall(39LL, a1, a2);
}

original_code:
ER
psnd_mbf (ID id, INT size, VP msg)
{
  return (call_syscall ((39), id, size, msg));
}

canonical_original_code:
int psnd_mbf(int id, int size, void *msg)
{
  return (call_syscall ((39), id, size, msg));
}


decompiled_code:
__int64 __fastcall tsnd_mbf(unsigned int a1, unsigned int a2)
{
return call_syscall(40LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, unsigned int a2)
{
return call_syscall(40LL, a1, a2);
}

original_code:
ER
tsnd_mbf (ID id, INT size, VP msg, TMO tmout)
{
  return (call_syscall ((40), id, size, msg, tmout));
}

canonical_original_code:
int tsnd_mbf(int id, int size, void *msg, int tmout)
{
  return (call_syscall ((40), id, size, msg, tmout));
}

variable_types: tmout int


decompiled_code:
__int64 __fastcall rcv_mbf(__int64 a1, __int64 a2)
{
return call_syscall(41LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
return call_syscall(41LL, a1, a2);
}

original_code:
ER
rcv_mbf (VP msg, INT *size, ID id)
{
  return (call_syscall ((41), msg, size, id));
}

canonical_original_code:
int rcv_mbf(void *msg, int *size, int id)
{
  return (call_syscall ((41), msg, size, id));
}

variable_types:
size
int *


decompiled_code:
__int64 __fastcall prcv_mbf(__int64 a1, __int64 a2)
{
return call_syscall(42LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
return call_syscall(42LL, a1, a2);
}

original_code:
ER
prcv_mbf (VP msg, INT *size, ID id)
{
  return (call_syscall ((42), msg, size, id));
}

canonical_original_code:
int prcv_mbf(void *msg, int *size, int id)
{
  return (call_syscall ((42), msg, size, id));
}


decompiled_code:
__int64 __fastcall trcv_mbf(__int64 a1, __int64 a2)
{
return call_syscall(43LL, a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
return call_syscall(43LL, a1, a2);
}

original_code:
ER
trcv_mbf (VP msg, INT *size, ID id, TMO tmout)
{
  return (call_syscall ((43), msg, size, id, tmout));
}

canonical_original_code:
int trcv_mbf(void *msg, int *size, int id, int tmout)
{
  return (call_syscall ((43), msg, size, id, tmout));
}


echo

decompiled_code:
__int64 __fastcall echo(__int64 a1, __int64 a2)
{
__int64 i; // rbx

if ( a1 <= 1 )
return 0LL;
for ( i = 1LL; i != a1; ++i )
puts(*(const char **)(a2 + 8 * i));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 i; // rbx

if ( a1 <= 1 )
return 0LL;
for ( i = 1LL; i != a1; ++i )
puts(*(const char **)(a2 + 8 * i));
return 0LL;
}

original_code:
ER echo (W ac, B **av)
{
  W i;
  for (i = 1; i < ac; i++)
    {
      printf ("%s\n", av[i]);
    }
  return (0);
}

canonical_original_code:
int echo(long ac, char **av)
{
  long i;
  for (i = 1; i < ac; i++)
    {
      printf ("%s\n", av[i]);
    }
  return (0);
}

variable_types: ac long

variable_types:
av
char **

variable_types: i long


exch

decompiled_code:
__int64 exch()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

result = MitnStack_Pop(1LL, &v2);
if ( !(_DWORD)result )
{
result = MitnStack_Pop(1LL, &v1);
if ( !(_DWORD)result )
{
result = MitnStack_Push(1LL, v2);
if ( !(_DWORD)result )
result = MitnStack_Push(1LL, v1);
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

result = MitnStack_Pop(1LL, &v2);
if ( !(_DWORD)result )
{
result = MitnStack_Pop(1LL, &v1);
if ( !(_DWORD)result )
{
result = MitnStack_Push(1LL, v2);
if ( !(_DWORD)result )
result = MitnStack_Push(1LL, v1);
}
}
return result;
}

original_code:
ER exch (void)
{
  Cell *c1;
  Cell *c2;
  ER result;
  result = MitnStack_Pop(1, &c1);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Pop(1, &c2);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Push(1, c1);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Push(1, c2);
  if ((result) != (0)) { return (result); };
  return (0);
}

canonical_original_code:
int exch()
{
  struct _cell *c1;
  struct _cell *c2;
  int result;
  result = MitnStack_Pop(1, &c1);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Pop(1, &c2);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Push(1, c1);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Push(1, c2);
  if ((result) != (0)) { return (result); };
  return (0);
}

variable_types: struct _cell { char mark; unsigned long type; struct _cell *prev; struct _cell *next; union { long inum; double dnum; struct { char str[48]; long length; } string; struct { struct _cell *link; char str[48]; } name; int (*mitn_func)(); struct { long arg_num; int (*func)(int argc, char *argv[]); } trad; struct _cell *link; } value; } *

variable_types: result int


clear

decompiled_code:
__int64 clear()
{
return MitnStack_Clear(1LL);
}

canonical_decompiled_code:
__int64 func2()
{
return MitnStack_Clear(1LL);
}

original_code:
ER clear (void)
{
  ER result;
  result = MitnStack_Clear(1);
  if ((result) != (0)) { return (result); };
  return (0);
}

canonical_original_code:
int clear()
{
  int result;
  result = MitnStack_Clear(1);
  if ((result) != (0)) { return (result); };
  return (0);
}


cleartomark

decompiled_code:
__int64 cleartomark()
{
return MitnStack_ClearToMark(1LL);
}

canonical_decompiled_code:
__int64 func3()
{
return MitnStack_ClearToMark(1LL);
}

original_code:
ER cleartomark (void)
{
  ER result;
  result = MitnStack_ClearToMark(1);
  if ((result) != (0)) { return (result); };
  return (0);
}

canonical_original_code:
int cleartomark()
{
  int result;
  result = MitnStack_ClearToMark(1);
  if ((result) != (0)) { return (result); };
  return (0);
}


copy

decompiled_code:
__int64 copy()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

result = MitnStack_Pop(1LL, &v2);
if ( !(_DWORD)result )
{
result = MitnCell_GetLongFromCell(&v1, v2);
if ( !(_DWORD)result )
result = MitnStack_Duplicate(1LL, v1);
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

result = MitnStack_Pop(1LL, &v2);
if ( !(_DWORD)result )
{
result = MitnCell_GetLongFromCell(&v1, v2);
if ( !(_DWORD)result )
result = MitnStack_Duplicate(1LL, v1);
}
return result;
}

original_code:
ER copy (void)
{
  Cell *c;
  ER result;
  long num;
  result = MitnStack_Pop(1, &c);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetLongFromCell(&num, c);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Duplicate(1, num);
  if ((result) != (0)) { return (result); };
  return (0);
}

canonical_original_code:
int copy()
{
  struct _cell *c;
  int result;
  long num;
  result = MitnStack_Pop(1, &c);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetLongFromCell(&num, c);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Duplicate(1, num);
  if ((result) != (0)) { return (result); };
  return (0);
}

variable_types: num long

user_defined_types: struct _cell { char mark; unsigned long type; struct _cell *prev; struct _cell *next; union { long inum; double dnum; struct { char str[48]; long length; } string; struct { struct _cell *link; char str[48]; } name; int (*mitn_func)(); struct { long arg_num; int (*func)(int argc, char *argv[]); } trad; struct _cell *link; } value; }


roll

decompiled_code:
__int64 roll()
{
__int64 v0; // rcx
unsigned int v2; // eax
__int64 v3; // rdx
__int64 v4; // rax
__int64 v5; // rbx
unsigned int v6; // eax
__int64 v7; // [rsp+8h] [rbp-20h] BYREF
__int64 v8; // [rsp+10h] [rbp-18h] BYREF
__int64 v9[2]; // [rsp+18h] [rbp-10h] BYREF

LODWORD(v0) = MitnStack_Pop(1LL, v9);
if ( !(_DWORD)v0 )
{
LODWORD(v0) = MitnCell_GetLongFromCell(&v7, v9[0]);
if ( !(_DWORD)v0 )
{
LODWORD(v0) = MitnStack_Pop(1LL, v9);
if ( !(_DWORD)v0 )
{
v2 = MitnCell_GetLongFromCell(&v8, v9[0]);
v0 = v2;
if ( !v2 )
{
v3 = v7 % v8;
v4 = v8 - v7 % v8;
if ( v8 <= 0 )
v4 = -(v7 % v8);
v7 = v4;
if ( v4 > 0 )
{
v5 = 0LL;
do
{
v6 = MitnStack_Roll(1LL, v8, v3, v0);
v0 = v6;
if ( v6 )
break;
++v5;
}
while ( v7 > v5 );
}
}
}
}
}
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // rcx
unsigned int v2; // eax
__int64 v3; // rdx
__int64 v4; // rax
__int64 v5; // rbx
unsigned int v6; // eax
__int64 v7; // [rsp+8h] [rbp-20h] BYREF
__int64 v8; // [rsp+10h] [rbp-18h] BYREF
__int64 v9[2]; // [rsp+18h] [rbp-10h] BYREF

LODWORD(v0) = MitnStack_Pop(1LL, v9);
if ( !(_DWORD)v0 )
{
LODWORD(v0) = MitnCell_GetLongFromCell(&v7, v9[0]);
if ( !(_DWORD)v0 )
{
LODWORD(v0) = MitnStack_Pop(1LL, v9);
if ( !(_DWORD)v0 )
{
v2 = MitnCell_GetLongFromCell(&v8, v9[0]);
v0 = v2;
if ( !v2 )
{
v3 = v7 % v8;
v4 = v8 - v7 % v8;
if ( v8 <= 0 )
v4 = -(v7 % v8);
v7 = v4;
if ( v4 > 0 )
{
v5 = 0LL;
do
{
v6 = MitnStack_Roll(1LL, v8, v3, v0);
v0 = v6;
if ( v6 )
break;
++v5;
}
while ( v7 > v5 );
}
}
}
}
}
return (unsigned int)v0;
}

original_code:
ER roll (void)
{
  Cell *c;
  ER result;
  long num;
  long times;
  long i;
  result = MitnStack_Pop(1, &c);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetLongFromCell(&times, c);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Pop(1, &c);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetLongFromCell(&num, c);
  if ((result) != (0)) { return (result); };
  times %= num;
  if (num <= 0) {
    times = 0 - times;
  } else {
    times = num - times;
  }
  for(i = 0; i < times; i++)
    {
      result = MitnStack_Roll(1, num);
      if ((result) != (0)) { return (result); };
    }
  return (0);
}

canonical_original_code:
int roll()
{
  struct _cell *c;
  int result;
  long num;
  long times;
  long i;
  result = MitnStack_Pop(1, &c);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetLongFromCell(&times, c);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Pop(1, &c);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetLongFromCell(&num, c);
  if ((result) != (0)) { return (result); };
  times %= num;
  if (num <= 0) {
    times = 0 - times;
  } else {
    times = num - times;
  }
  for(i = 0; i < times; i++)
    {
      result = MitnStack_Roll(1, num);
      if ((result) != (0)) { return (result); };
    }
  return (0);
}

variable_types: times long


counttomark

decompiled_code:
__int64 counttomark()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

result = MitnStack_CountToMark(1LL, &v2);
if ( !(_DWORD)result )
{
result = MitnCell_GetCellAsLong(&v1, v2);
if ( !(_DWORD)result )
result = MitnStack_Push(1LL, v1);
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
__int64 v1; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

result = MitnStack_CountToMark(1LL, &v2);
if ( !(_DWORD)result )
{
result = MitnCell_GetCellAsLong(&v1, v2);
if ( !(_DWORD)result )
result = MitnStack_Push(1LL, v1);
}
return result;
}

original_code:
ER counttomark (void)
{
  ER result;
  W count;
  Cell *c;
  result = MitnStack_CountToMark(1, &count);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetCellAsLong(&c, count);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Push(1, c);
  if ((result) != (0)) { return (result); };
  return (0);
}

canonical_original_code:
int counttomark()
{
  int result;
  long count;
  struct _cell *c;
  result = MitnStack_CountToMark(1, &count);
  if ((result) != (0)) { return (result); };
  result = MitnCell_GetCellAsLong(&c, count);
  if ((result) != (0)) { return (result); };
  result = MitnStack_Push(1, c);
  if ((result) != (0)) { return (result); };
  return (0);
}

variable_types: count long


mark

decompiled_code:
__int64 mark()
{
return MitnStack_PushMark(1LL);
}

canonical_decompiled_code:
__int64 func8()
{
return MitnStack_PushMark(1LL);
}

original_code:
ER mark (void)
{
  ER result;
  result = MitnStack_PushMark(1);
  if ((result) != (0)) { return (result); };
  return (0);
}

canonical_original_code:
int mark()
{
  int result;
  result = MitnStack_PushMark(1);
  if ((result) != (0)) { return (result); };
  return (0);
}


help

original_code:
ER help (void)
{
  return (-17);
}

canonical_original_code:
int help()
{
  return (-17);
}