original_code:
size_t
cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
{
 struct cmd_switch_client_data *data = self->data;
 size_t off = 0;
 off += xsnprintf(buf, len, "%s", self->entry->name);
 if (data == ((void *)0))
  return (off);
 if (off < len && data->name != ((void *)0))
  off += cmd_prarg(buf + off, len - off, " -c ", data->name);
 if (off < len && data->target != ((void *)0))
  off += cmd_prarg(buf + off, len - off, " -t ", data->target);
 return (off);
}

canonical_original_code:
unsigned long cmd_switch_client_print(struct cmd *self, char *buf, unsigned long len)
{
 struct cmd_switch_client_data *data = self->data;
 unsigned long off = 0;
 off += xsnprintf(buf, len, "%s", self->entry->name);
 if (data == ((void *)0))
  return (off);
 if (off < len && data->name != ((void *)0))
  off += cmd_prarg(buf + off, len - off, " -c ", data->name);
 if (off < len && data->target != ((void *)0))
  off += cmd_prarg(buf + off, len - off, " -t ", data->target);
 return (off);
}

variable_types:
buf
char *

variable_types:
len
unsigned long

variable_types:
off
unsigned long

return_type: unsigned long

user_defined_types: struct cmd { struct cmd_entry *entry; void *data; struct { struct cmd *tqe_next; struct cmd **tqe_prev; } qentry; }

user_defined_types: struct cmd_switch_client_data { char *name; char *target; }

user_defined_types: struct cmd_entry { char *name; char *alias; char *usage; int flags; void (*init)(struct cmd *, int); int (*parse)(struct cmd *, int, char **, char **); int (*exec)(struct cmd *, struct cmd_ctx *); void (*send)(struct cmd *, struct buffer *); void (*recv)(struct cmd *, struct buffer *); void (*free)(struct cmd *); unsigned long (*print)(struct cmd *, char *, unsigned long); }

user_defined_types: struct cmd_ctx { struct client *cmdclient; struct client *curclient; struct session *cursession; struct msg_command_data *msgdata; void (*print)(struct cmd_ctx *, char *, ...); void (*info)(struct cmd_ctx *, char *, ...); void (*error)(struct cmd_ctx *, char *, ...); }

user_defined_types: struct client { int fd; struct buffer *in; struct buffer *out; char *title; char *cwd; struct tty tty; struct timeval status_timer; struct timeval repeat_timer; struct screen status; int flags; char *message_string; struct timeval message_timer; char *prompt_string; char *prompt_buffer; unsigned long prompt_index; int (*prompt_callback)(void *, char *); void *prompt_data; int prompt_hidden; unsigned int prompt_hindex; struct { char **list; unsigned int num; unsigned long space; } prompt_hdata; struct mode_key_data prompt_mdata; struct session *session; }

user_defined_types: struct msg_command_data { int pid; unsigned int idx; unsigned long namelen; }

user_defined_types: struct tty { char *path; unsigned int sx; unsigned int sy; unsigned int cx; unsigned int cy; int mode; unsigned int rlower; unsigned int rupper; char *termname; struct tty_term *term; int fd; struct buffer *in; struct buffer *out; int log_fd; struct termios tio; struct grid_cell cell; unsigned char acs[-1]; int flags; int term_flags; struct timeval key_timer; unsigned long ksize; struct tty_keys ktree; }

user_defined_types: struct screen { char *title; struct grid *grid; unsigned int cx; unsigned int cy; unsigned int old_cx; unsigned int old_cy; unsigned int rupper; unsigned int rlower; unsigned int old_rupper; unsigned int old_rlower; int mode; struct screen_sel sel; }

user_defined_types: struct mode_key_data { int type; int flags; }

user_defined_types: struct buffer { unsigned char *base; unsigned long space; unsigned long size; unsigned long off; }

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

user_defined_types: struct grid_cell { unsigned char attr; unsigned char flags; unsigned char fg; unsigned char bg; unsigned char data; }

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

user_defined_types: struct session { char *name; struct timeval tv; unsigned int sx; unsigned int sy; struct winlink *curw; struct winlink_stack lastw; struct winlinks windows; struct options options; struct paste_stack buffers; struct { struct session_alert *slh_first; } alerts; int flags; }

user_defined_types: struct grid { unsigned int sx; unsigned int sy; unsigned int hsize; unsigned int hlimit; unsigned int *size; struct grid_cell **data; unsigned int *usize; struct grid_utf8 **udata; }

user_defined_types: struct winlink { int idx; struct window *window; struct { struct winlink *rbe_left; struct winlink *rbe_right; struct winlink *rbe_parent; int rbe_color; } entry; struct { struct winlink *sle_next; } sentry; }

user_defined_types: struct tty_code { enum tty_code_type type; union { char *string; int number; int flag; } value; }

user_defined_types: struct window { char *name; struct timeval name_timer; struct window_pane *active; struct window_panes panes; unsigned int layout; unsigned int sx; unsigned int sy; int flags; struct options options; unsigned int references; }

user_defined_types: struct options { struct options_tree tree; struct options *parent; }

user_defined_types: struct window_pane { struct window *window; unsigned int sx; unsigned int sy; unsigned int xoff; unsigned int yoff; int flags; char *cmd; char *cwd; int pid; int fd; char tty[32]; struct buffer *in; struct buffer *out; struct input_ctx ictx; struct screen *screen; struct screen base; struct window_mode *mode; void *modedata; struct { struct window_pane *tqe_next; struct window_pane **tqe_prev; } entry; }

user_defined_types: struct options_tree { struct options_entry *sph_root; }

user_defined_types: struct input_ctx { struct window_pane *wp; struct screen_write_ctx ctx; unsigned char *buf; unsigned long len; unsigned long off; struct grid_cell cell; struct grid_cell saved_cell; unsigned int saved_cx; unsigned int saved_cy; unsigned char *string_buf; unsigned long string_len; int string_type; unsigned char utf8_buf[4]; unsigned int utf8_len; unsigned int utf8_off; unsigned char intermediate; void *(*state)(unsigned char, struct input_ctx *); unsigned char private; struct { struct input_arg *list; unsigned int num; unsigned long space; } args; }


repo: ThomasAdam/tmux-ARCHIVED

decompiled_code:
char *__fastcall client_fill_session(_DWORD *a1)
{
char *result; // rax
unsigned __int64 v2; // rbx
char *v3; // rsi
unsigned __int64 v4; // rsi
char *v5; // r12
char *v6; // rbx
__int64 v7; // [rsp+8h] [rbp-120h] BYREF
char dest[280]; // [rsp+10h] [rbp-118h] BYREF

*a1 = -1;
result = getenv("TMUX");
if ( result )
{
v2 = (unsigned __int64)result;
result = strrchr(result, 44);
if ( (char *)v2 != result )
{
if ( result )
{
v3 = result - 1;
if ( v2 >= (unsigned __int64)(result - 1) )
{
LABEL_7:
if ( *v3 != 44 )
return result;
}
else
{
while ( *v3 != 44 )
{
if ( (char *)v2 == --v3 )
goto LABEL_7;
}
}
v4 = (unsigned __int64)(v3 + 1);
v5 = result + 1;
result -= v4;
v6 = result;
if ( (unsigned __int64)result <= 0xFF )
{
memcpy(dest, (const void *)v4, (size_t)result);
dest[(_QWORD)v6] = 0;
result = (char *)strtonum(dest, 0LL, 0x7FFFFFFFFFFFFFFFLL, &v7);
if ( !v7 )
{
*a1 = (_DWORD)result;
result = (char *)strtonum(v5, 0LL, 0xFFFFFFFFLL, &v7);
if ( !v7 )
a1[1] = (_DWORD)result;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(_DWORD *a1)
{
char *result; // rax
unsigned __int64 v2; // rbx
char *v3; // rsi
unsigned __int64 v4; // rsi
char *v5; // r12
char *v6; // rbx
__int64 v7; // [rsp+8h] [rbp-120h] BYREF
char dest[280]; // [rsp+10h] [rbp-118h] BYREF

*a1 = -1;
result = getenv("TMUX");
if ( result )
{
v2 = (unsigned __int64)result;
result = strrchr(result, 44);
if ( (char *)v2 != result )
{
if ( result )
{
v3 = result - 1;
if ( v2 >= (unsigned __int64)(result - 1) )
{
LABEL_7:
if ( *v3 != 44 )
return result;
}
else
{
while ( *v3 != 44 )
{
if ( (char *)v2 == --v3 )
goto LABEL_7;
}
}
v4 = (unsigned __int64)(v3 + 1);
v5 = result + 1;
result -= v4;
v6 = result;
if ( (unsigned __int64)result <= 0xFF )
{
memcpy(dest, (const void *)v4, (size_t)result);
dest[(_QWORD)v6] = 0;
result = (char *)strtonum(dest, 0LL, 0x7FFFFFFFFFFFFFFFLL, &v7);
if ( !v7 )
{
*a1 = (_DWORD)result;
result = (char *)strtonum(v5, 0LL, 0xFFFFFFFFLL, &v7);
if ( !v7 )
a1[1] = (_DWORD)result;
}
}
}
}
}
return result;
}

original_code:
void
client_fill_session(struct msg_command_data *data)
{
 char *env, *ptr1, *ptr2, buf[256];
 size_t len;
 const char *errstr;
 long long ll;
 data->pid = -1;
 if ((env = getenv("TMUX")) == ((void *)0))
  return;
 if ((ptr2 = strrchr(env, ',')) == ((void *)0) || ptr2 == env)
  return;
 for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--)
  ;
 if (*ptr1 != ',')
  return;
 ptr1++;
 ptr2++;
 len = ptr2 - ptr1 - 1;
 if (len > (sizeof buf) - 1)
  return;
 memcpy(buf, ptr1, len);
 buf[len] = '\0';
 ll = strtonum(buf, 0, 0x7fffffffffffffffL, &errstr);
 if (errstr != ((void *)0))
  return;
 data->pid = ll;
 ll = strtonum(ptr2, 0, (0x7fffffff * 2U + 1U), &errstr);
 if (errstr != ((void *)0))
  return;
 data->idx = ll;
}

canonical_original_code:
void client_fill_session(struct msg_command_data *data)
{
 char *env; char *ptr1; char *ptr2; char buf[256];
 unsigned long len;
 char *errstr;
 long long ll;
 data->pid = -1;
 if ((env = getenv("TMUX")) == ((void *)0))
  return;
 if ((ptr2 = strrchr(env, ',')) == ((void *)0) || ptr2 == env)
  return;
 for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--)
  ;
 if (*ptr1 != ',')
  return;
 ptr1++;
 ptr2++;
 len = ptr2 - ptr1 - 1;
 if (len > (sizeof buf) - 1)
  return;
 memcpy(buf, ptr1, len);
 buf[len] = '\0';
 ll = strtonum(buf, 0, 0x7fffffffffffffffL, &errstr);
 if (errstr != ((void *)0))
  return;
 data->pid = ll;
 ll = strtonum(ptr2, 0, (0x7fffffff * 2U + 1U), &errstr);
 if (errstr != ((void *)0))
  return;
 data->idx = ll;
}

variable_types: struct msg_command_data { int pid; unsigned int idx; unsigned long namelen; } *

variable_types:
env
char *

variable_types:
buf
char [256]

variable_types:
errstr
char *

variable_types:
ll
long long

return_type: void


decompiled_code:
int client_suspend()
{
__pid_t v0; // eax
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

memset(&v2, 0, sizeof(v2));
sigemptyset(&v2.sa_mask);
v2.sa_flags = 0x10000000;
v2.sa_handler = 0LL;
if ( sigaction(20, &v2, 0LL) )
{
log_fatal("%s: %s", "client_suspend", "sigaction failed");
goto LABEL_5;
}
v2.sa_handler = (__sighandler_t)&sighandler;
if ( sigaction(18, &v2, 0LL) )
{
LABEL_5:
log_fatal("%s: %s", "client_suspend", "sigaction failed");
JUMPOUT(0x1BALL);
}
v0 = getpid();
return kill(v0, 20);
}

canonical_decompiled_code:
int func2()
{
__pid_t v0; // eax
struct sigaction v2; // [rsp+0h] [rbp-A8h] BYREF

memset(&v2, 0, sizeof(v2));
sigemptyset(&v2.sa_mask);
v2.sa_flags = 0x10000000;
v2.sa_handler = 0LL;
if ( sigaction(20, &v2, 0LL) )
{
log_fatal("%s: %s", "client_suspend", "sigaction failed");
goto LABEL_5;
}
v2.sa_handler = (__sighandler_t)&sighandler;
if ( sigaction(18, &v2, 0LL) )
{
LABEL_5:
log_fatal("%s: %s", "client_suspend", "sigaction failed");
JUMPOUT(0x1BALL);
}
v0 = getpid();
return kill(v0, 20);
}

original_code:
void
client_suspend(void)
{
 struct sigaction act;
 memset(&act, 0, sizeof act);
 sigemptyset(&act.sa_mask);
 act.sa_flags = 0x10000000;
 act.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
 if (sigaction(20, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 act.__sigaction_handler.sa_handler = sighandler;
 if (sigaction(18, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 kill(getpid(), 20);
}

canonical_original_code:
void client_suspend()
{
 struct sigaction act;
 memset(&act, 0, sizeof act);
 sigemptyset(&act.sa_mask);
 act.sa_flags = 0x10000000;
 act.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
 if (sigaction(20, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 act.__sigaction_handler.sa_handler = sighandler;
 if (sigaction(18, &act, ((void *)0)) != 0)
  log_fatal("%s: %s", __func__, "sigaction failed");;
 kill(getpid(), 20);
}

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

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

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


calculaValor

repo: TiagoTT/academic

original_code:
void calculaValor(Circuito* circuito, int node_index)
{
  int i;
  Node* node;
  node = circuito->vectorNodes + node_index ;
  node->valor = tabela1[ ( node->tipo / 2 ) * 2 ]
    [ circuito->vectorNodes[ node->inputs[ 0 ] ].valor ];
  for( i = 1 ; i < node->fanin ; i++ )
    {
      node->valor = tabela2[ ( node->tipo / 2 ) * 2 ]
 [ node->valor ][ circuito->vectorNodes[ node->inputs[ i ] ].valor ];
    }
  if( node->tipo % 2 )
    {
      node->valor = tabela1[ 3 ]
 [ node->valor ];
    }
  return;
}

canonical_original_code:
void calculaValor(struct CircuitoStr *circuito, int node_index)
{
  int i;
  struct NodeStr *node;
  node = circuito->vectorNodes + node_index ;
  node->valor = tabela1[ ( node->tipo / 2 ) * 2 ]
    [ circuito->vectorNodes[ node->inputs[ 0 ] ].valor ];
  for( i = 1 ; i < node->fanin ; i++ )
    {
      node->valor = tabela2[ ( node->tipo / 2 ) * 2 ]
 [ node->valor ][ circuito->vectorNodes[ node->inputs[ i ] ].valor ];
    }
  if( node->tipo % 2 )
    {
      node->valor = tabela1[ 3 ]
 [ node->valor ];
    }
  return;
}

variable_types: struct CircuitoStr { int nNodes; struct NodeStr { char nome[16]; int tipo; int fanin; int *inputs; int valor; char flag; int fanout; char *sa0; char *sa1; int leituras; } *vectorNodes; int nMax; int nInputs; int realNodes; } *

variable_types: i int

user_defined_types: struct CircuitoStr { int nNodes; struct NodeStr *vectorNodes; int nMax; int nInputs; int realNodes; }


valorControlo

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

v2 = *(_DWORD *)(*(_QWORD *)(a1 + 8) + 72LL * a2 + 16);
if ( v2 <= 1 )
result = 2 * ((unsigned int)v2 >> 31);
else
result = (unsigned int)((unsigned int)(v2 - 2) > 1) + 1;
return result;
}

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

v2 = *(_DWORD *)(*(_QWORD *)(a1 + 8) + 72LL * a2 + 16);
if ( v2 <= 1 )
result = 2 * ((unsigned int)v2 >> 31);
else
result = (unsigned int)((unsigned int)(v2 - 2) > 1) + 1;
return result;
}

original_code:
int valorControlo(Circuito* circuito, int node_index)
{
  Node* node;
  node = circuito->vectorNodes + node_index ;
  switch( node->tipo )
    {
    case 0:
    case 1:
      return 0;
    case 2:
    case 3:
      return 1;
    }
  return 2;
}

canonical_original_code:
int valorControlo(struct CircuitoStr *circuito, int node_index)
{
  struct NodeStr *node;
  node = circuito->vectorNodes + node_index ;
  switch( node->tipo )
    {
    case 0:
    case 1:
      return 0;
    case 2:
    case 3:
      return 1;
    }
  return 2;
}

variable_types: struct NodeStr { char nome[16]; int tipo; int fanin; int *inputs; int valor; char flag; int fanout; char *sa0; char *sa1; int leituras; } *

return_type: int


haEntradasControlo

original_code:
int haEntradasControlo(Circuito* circuito, int node_index)
{
  int i;
  int controlo;
  Node* node;
  controlo = valorControlo( circuito, node_index );
  node = circuito->vectorNodes + node_index ;
  for( i = 0 ; i < node->fanin ; i++ )
    {
      if( controlo == circuito->vectorNodes[ node->inputs[ i ] ].valor )
 {
   return 1;
 }
    }
  return 0;
}

canonical_original_code:
int haEntradasControlo(struct CircuitoStr *circuito, int node_index)
{
  int i;
  int controlo;
  struct NodeStr *node;
  controlo = valorControlo( circuito, node_index );
  node = circuito->vectorNodes + node_index ;
  for( i = 0 ; i < node->fanin ; i++ )
    {
      if( controlo == circuito->vectorNodes[ node->inputs[ i ] ].valor )
 {
   return 1;
 }
    }
  return 0;
}

variable_types: controlo int

user_defined_types: struct NodeStr { char nome[16]; int tipo; int fanin; int *inputs; int valor; char flag; int fanout; char *sa0; char *sa1; int leituras; }


IdentificaSoldado

decompiled_code:
__int64 __fastcall IdentificaSoldado(__int64 a1)
{
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_sscanf(a1, "Apresenta-se o Soldado %d", &v2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_sscanf(a1, "Apresenta-se o Soldado %d", &v2);
return v2;
}

original_code:
int IdentificaSoldado(char* msg)
{
 int pid;
 sscanf( msg, "Apresenta-se o Soldado %d", &pid);
 return pid;
}

canonical_original_code:
int IdentificaSoldado(char *msg)
{
 int pid;
 sscanf( msg, "Apresenta-se o Soldado %d", &pid);
 return pid;
}

variable_types:
msg
char *

variable_types: pid int


IdentificaOficial

decompiled_code:
__int64 __fastcall IdentificaOficial(__int64 a1)
{
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_sscanf(a1, "Apresenta-se o Oficial %d", &v2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned int v2; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_sscanf(a1, "Apresenta-se o Oficial %d", &v2);
return v2;
}

original_code:
int IdentificaOficial(char* msg)
{
 int pid;
 sscanf( msg, "Apresenta-se o Oficial %d", &pid);
 return pid;
}

canonical_original_code:
int IdentificaOficial(char *msg)
{
 int pid;
 sscanf( msg, "Apresenta-se o Oficial %d", &pid);
 return pid;
}


GetIndiceMilitar

decompiled_code:
__int64 __fastcall GetIndiceMilitar(int a1)
{
int v1; // ecx
_DWORD *v2; // rdx
__int64 result; // rax

v1 = *((_DWORD *)&clientes + 2);
if ( v1 <= 0 )
return 0xFFFFFFFFLL;
v2 = clientes;
result = 0LL;
while ( *v2 != a1 )
{
result = (unsigned int)(result + 1);
v2 += 6;
if ( (_DWORD)result == v1 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
int v1; // ecx
_DWORD *v2; // rdx
__int64 result; // rax

v1 = *((_DWORD *)&clientes + 2);
if ( v1 <= 0 )
return 0xFFFFFFFFLL;
v2 = clientes;
result = 0LL;
while ( *v2 != a1 )
{
result = (unsigned int)(result + 1);
v2 += 6;
if ( (_DWORD)result == v1 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int GetIndiceMilitar(int pid)
{
 int i;
 for( i = 0 ; i < clientes.n_cli ; i++ )
 {
  if( clientes.vector[i].id == pid )
  {
   return i;
  }
 }
 return -1;
}


RegistaEntrada

decompiled_code:
char *__fastcall RegistaEntrada(int a1, int a2)
{
int v2; // eax
char *result; // rax
int v4; // eax

v2 = *((_DWORD *)&clientes + 3);
if ( *((_DWORD *)&clientes + 2) >= v2 )
{
v4 = v2 + 10;
*((_DWORD *)&clientes + 3) = v4;
clientes = realloc(clientes, 24LL * v4);
}
result = (char *)clientes + 24 * *((int *)&clientes + 2);
*(_DWORD *)result = a1;
*((_DWORD *)result + 1) = a2;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
++*((_DWORD *)&clientes + 2);
return result;
}

canonical_decompiled_code:
char *__fastcall func3(int a1, int a2)
{
int v2; // eax
char *result; // rax
int v4; // eax

v2 = *((_DWORD *)&clientes + 3);
if ( *((_DWORD *)&clientes + 2) >= v2 )
{
v4 = v2 + 10;
*((_DWORD *)&clientes + 3) = v4;
clientes = realloc(clientes, 24LL * v4);
}
result = (char *)clientes + 24 * *((int *)&clientes + 2);
*(_DWORD *)result = a1;
*((_DWORD *)result + 1) = a2;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
++*((_DWORD *)&clientes + 2);
return result;
}

original_code:
void RegistaEntrada(int pid, int tipo)
{
 Militar novo;
 novo.id = pid;
 novo.tipo = tipo;
 novo.pedido = ((void *)0);
 if( clientes.n_cli >= clientes.n_max )
 {
  clientes.n_max += 10;
  clientes.vector = (Militar*)realloc( clientes.vector, sizeof(Militar)*(clientes.n_max) );
 }
 clientes.vector[ clientes.n_cli ] = novo;
 clientes.n_cli ++;
 return;
}

canonical_original_code:
void RegistaEntrada(int pid, int tipo)
{
 struct MilitarStr novo;
 novo.id = pid;
 novo.tipo = tipo;
 novo.pedido = ((void *)0);
 if( clientes.n_cli >= clientes.n_max )
 {
  clientes.n_max += 10;
  clientes.vector = (struct MilitarStr *)realloc( clientes.vector, sizeof(Militar)*(clientes.n_max) );
 }
 clientes.vector[ clientes.n_cli ] = novo;
 clientes.n_cli ++;
 return;
}

variable_types: tipo int

variable_types: struct MilitarStr { unsigned int id; unsigned int tipo; struct PedidoStr { struct ProdutoStr { char nome[20]; char quantidade[5]; } *prod; int n_prod; } *pedido; unsigned long tid; }

user_defined_types: struct MilitarStr { unsigned int id; unsigned int tipo; struct PedidoStr *pedido; unsigned long tid; }

user_defined_types: struct PedidoStr { struct ProdutoStr *prod; int n_prod; }


RegistaSaida

decompiled_code:
int __fastcall RegistaSaida(unsigned int a1)
{
int v1; // ecx
_DWORD *v2; // rax

v1 = *((_DWORD *)&clientes + 2);
if ( v1 <= 0 )
{
LABEL_5:
LODWORD(v2) = fprintf(stderr, "O militar %d nao esta no Refeitorio.\n", a1);
}
else
{
v2 = clientes;
while ( *v2 != a1 )
{
v2 += 6;
if ( v2 == (_DWORD *)((char *)clientes + 24 * (unsigned int)(v1 - 1) + 24) )
goto LABEL_5;
}
*v2 = 0;
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1)
{
int v1; // ecx
_DWORD *v2; // rax

v1 = *((_DWORD *)&clientes + 2);
if ( v1 <= 0 )
{
LABEL_5:
LODWORD(v2) = fprintf(stderr, "O militar %d nao esta no Refeitorio.\n", a1);
}
else
{
v2 = clientes;
while ( *v2 != a1 )
{
v2 += 6;
if ( v2 == (_DWORD *)((char *)clientes + 24 * (unsigned int)(v1 - 1) + 24) )
goto LABEL_5;
}
*v2 = 0;
}
return (int)v2;
}

original_code:
void RegistaSaida(int pid)
{
 int i;
 for( i = 0 ; i < clientes.n_cli ; i++ )
 {
  if( clientes.vector[i].id == pid )
  {
   clientes.vector[i].id = 0;
   return;
  }
 }
 fprintf(stderr, "O militar %d nao esta no Refeitorio.\n", pid);
 return;
}


MandaTodosSair

decompiled_code:
void *MandaTodosSair()
{
__int64 v0; // rbx
int v1; // ebp
void *result; // rax
__pid_t v3; // edi

if ( *((int *)&clientes + 2) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
result = clientes;
v3 = *(_DWORD *)((char *)clientes + v0);
if ( v3 )
{
kill(v3, 2);
result = clientes;
*(_DWORD *)((char *)clientes + v0) = 0;
}
++v1;
v0 += 24LL;
}
while ( *((_DWORD *)&clientes + 2) > v1 );
}
return result;
}

canonical_decompiled_code:
void *func5()
{
__int64 v0; // rbx
int v1; // ebp
void *result; // rax
__pid_t v3; // edi

if ( *((int *)&clientes + 2) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
result = clientes;
v3 = *(_DWORD *)((char *)clientes + v0);
if ( v3 )
{
kill(v3, 2);
result = clientes;
*(_DWORD *)((char *)clientes + v0) = 0;
}
++v1;
v0 += 24LL;
}
while ( *((_DWORD *)&clientes + 2) > v1 );
}
return result;
}

original_code:
void MandaTodosSair()
{
 int i;
 for( i = 0 ; i < clientes.n_cli ; i++ )
 {
  if( clientes.vector[i].id )
  {
   kill( clientes.vector[i].id, 2 );
   clientes.vector[i].id = 0;
  }
 }
 return;
}


IniciaClientes

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

result = malloc(0x1E0uLL);
clientes = result;
*((_DWORD *)&clientes + 2) = 0;
*((_DWORD *)&clientes + 3) = 20;
return result;
}

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

result = malloc(0x1E0uLL);
clientes = result;
*((_DWORD *)&clientes + 2) = 0;
*((_DWORD *)&clientes + 3) = 20;
return result;
}

original_code:
void IniciaClientes()
{
 clientes.vector = ( Militar* )malloc( sizeof( Militar )*20 );
 clientes.n_cli = 0;
 clientes.n_max = 20;
 return;
}

canonical_original_code:
void IniciaClientes()
{
 clientes.vector = ( struct MilitarStr * )malloc( sizeof( Militar )*20 );
 clientes.n_cli = 0;
 clientes.n_max = 20;
 return;
}

user_defined_types: struct ProdutoStr { char nome[20]; char quantidade[5]; }


ReiniciaClientes

decompiled_code:
char *ReiniciaClientes()
{
int v0; // ebp
void *v1; // rax
void *v2; // rbx
__int64 v3; // rsi
__int64 v4; // rax
int v5; // ebp
const __m128i *v6; // rdx
__m128i *v7; // rcx
int v8; // eax
char *result; // rax
int v10; // edx
void *v11; // rcx

v0 = *((_DWORD *)&clientes + 2);
v1 = malloc(24LL * v0);
if ( v0 <= 0 )
{
*((_DWORD *)&clientes + 3) = 20;
result = (char *)realloc(clientes, 0x1E0uLL);
clientes = result;
*((_DWORD *)&clientes + 2) = 0;
return result;
}
v2 = v1;
v3 = 24LL * (unsigned int)v0;
v4 = 0LL;
v5 = 0;
do
{
v6 = (const __m128i *)((char *)clientes + v4);
if ( *(_DWORD *)((char *)clientes + v4) )
{
v7 = (__m128i *)((char *)v2 + 24 * v5);
*v7 = _mm_loadu_si128(v6);
v7[1].m128i_i64[0] = v6[1].m128i_i64[0];
++v5;
}
v4 += 24LL;
}
while ( v4 != v3 );
if ( v5 <= 20 )
{
*((_DWORD *)&clientes + 3) = 20;
result = (char *)realloc(clientes, 0x1E0uLL);
clientes = result;
*((_DWORD *)&clientes + 2) = v5;
if ( v5 <= 0 )
return result;
}
else
{
v8 = 2 * (5 * (v5 / 10) + 5);
*((_DWORD *)&clientes + 3) = v8;
clientes = realloc(clientes, 24LL * v8);
*((_DWORD *)&clientes + 2) = v5;
}
result = 0LL;
v10 = 0;
do
{
v11 = clientes;
*(__m128i *)&result[(_QWORD)clientes] = _mm_loadu_si128((const __m128i *)&result[(_QWORD)v2]);
*(_QWORD *)&result[(_QWORD)v11 + 16] = *(_QWORD *)&result[(_QWORD)v2 + 16];
++v10;
result += 24;
}
while ( *((_DWORD *)&clientes + 2) > v10 );
return result;
}

canonical_decompiled_code:
char *func7()
{
int v0; // ebp
void *v1; // rax
void *v2; // rbx
__int64 v3; // rsi
__int64 v4; // rax
int v5; // ebp
const __m128i *v6; // rdx
__m128i *v7; // rcx
int v8; // eax
char *result; // rax
int v10; // edx
void *v11; // rcx

v0 = *((_DWORD *)&clientes + 2);
v1 = malloc(24LL * v0);
if ( v0 <= 0 )
{
*((_DWORD *)&clientes + 3) = 20;
result = (char *)realloc(clientes, 0x1E0uLL);
clientes = result;
*((_DWORD *)&clientes + 2) = 0;
return result;
}
v2 = v1;
v3 = 24LL * (unsigned int)v0;
v4 = 0LL;
v5 = 0;
do
{
v6 = (const __m128i *)((char *)clientes + v4);
if ( *(_DWORD *)((char *)clientes + v4) )
{
v7 = (__m128i *)((char *)v2 + 24 * v5);
*v7 = _mm_loadu_si128(v6);
v7[1].m128i_i64[0] = v6[1].m128i_i64[0];
++v5;
}
v4 += 24LL;
}
while ( v4 != v3 );
if ( v5 <= 20 )
{
*((_DWORD *)&clientes + 3) = 20;
result = (char *)realloc(clientes, 0x1E0uLL);
clientes = result;
*((_DWORD *)&clientes + 2) = v5;
if ( v5 <= 0 )
return result;
}
else
{
v8 = 2 * (5 * (v5 / 10) + 5);
*((_DWORD *)&clientes + 3) = v8;
clientes = realloc(clientes, 24LL * v8);
*((_DWORD *)&clientes + 2) = v5;
}
result = 0LL;
v10 = 0;
do
{
v11 = clientes;
*(__m128i *)&result[(_QWORD)clientes] = _mm_loadu_si128((const __m128i *)&result[(_QWORD)v2]);
*(_QWORD *)&result[(_QWORD)v11 + 16] = *(_QWORD *)&result[(_QWORD)v2 + 16];
++v10;
result += 24;
}
while ( *((_DWORD *)&clientes + 2) > v10 );
return result;
}

original_code:
void ReiniciaClientes()
{
 int i, n_ofi;
 Militar* tmp;
 tmp = ( Militar* )malloc( sizeof( Militar )*clientes.n_cli );
 for( i = 0, n_ofi = 0 ; i < clientes.n_cli ; i++ )
 {
  if( clientes.vector[i].id )
  {
   tmp[ n_ofi ] = clientes.vector[i];
   n_ofi++;
  }
 }
 clientes.n_max = (n_ofi > 20) ?
  (1+n_ofi/10)*10:
  20;
 clientes.vector = (Militar*)realloc( clientes.vector, sizeof(Militar)*(clientes.n_max) );
 clientes.n_cli = n_ofi;
 for( i = 0 ; i < clientes.n_cli ; i++ )
 {
  clientes.vector[i] = tmp[i];
 }
 return;
}

canonical_original_code:
void ReiniciaClientes()
{
 int i; int n_ofi;
 struct MilitarStr *tmp;
 tmp = ( struct MilitarStr * )malloc( sizeof( Militar )*clientes.n_cli );
 for( i = 0, n_ofi = 0 ; i < clientes.n_cli ; i++ )
 {
  if( clientes.vector[i].id )
  {
   tmp[ n_ofi ] = clientes.vector[i];
   n_ofi++;
  }
 }
 clientes.n_max = (n_ofi > 20) ?
  (1+n_ofi/10)*10:
  20;
 clientes.vector = (struct MilitarStr *)realloc( clientes.vector, sizeof(Militar)*(clientes.n_max) );
 clientes.n_cli = n_ofi;
 for( i = 0 ; i < clientes.n_cli ; i++ )
 {
  clientes.vector[i] = tmp[i];
 }
 return;
}

variable_types: struct MilitarStr { unsigned int id; unsigned int tipo; struct PedidoStr { struct ProdutoStr { char nome[20]; char quantidade[5]; } *prod; int n_prod; } *pedido; unsigned long tid; } *


ThreadOficiais

decompiled_code:
void *__fastcall ThreadOficiais(void *a1)
{
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
return 0LL;
}

original_code:
void* ThreadOficiais(void* argumento)
{
  return 0;
}

canonical_original_code:
void *ThreadOficiais(void *argumento)
{
  return 0;
}

variable_types:
argumento
void *

return_type: void *


CancelaThreadOfic

decompiled_code:
void CancelaThreadOfic()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void CancelaThreadOfic(void)
{
  return;
}

canonical_original_code:
void CancelaThreadOfic()
{
  return;
}


AtendeOficiais

decompiled_code:
void __noreturn AtendeOficiais()
{
void *v0; // rbp
__int64 v1; // r12
bool v2; // cf
bool v3; // zf
const char *v4; // rdi
__int64 v5; // rcx
const char *v6; // rsi
unsigned int v7; // eax
_BYTE v8[88]; // [rsp+0h] [rbp-78h] BYREF
pthread_t newthread[4]; // [rsp+58h] [rbp-20h] BYREF

v0 = (void *)CriarSd("/tmp/sdserv");
v1 = CriaSdAuxiliar();
while ( 1 )
{
RecebMsgSock(v1, v0, v8);
v4 = "Apresenta";
v5 = 9LL;
v6 = v8;
do
{
if ( !v5 )
break;
v2 = *v6 < (unsigned int)*v4;
v3 = *v6++ == *v4++;
--v5;
}
while ( v3 );
if ( (!v2 && !v3) == v2 )
{
v7 = IdentificaOficial(v8, v6);
RegistaEntrada(v7, 2LL);
}
else if ( memcmp(v8, "Saida", 5uLL) )
{
pthread_create(newthread, 0LL, ThreadOficiais, v0);
EnviaMsgSock(v1, "Refeitorio responde!!!", v0);
}
}
}

canonical_decompiled_code:
void __noreturn func2()
{
void *v0; // rbp
__int64 v1; // r12
bool v2; // cf
bool v3; // zf
const char *v4; // rdi
__int64 v5; // rcx
const char *v6; // rsi
unsigned int v7; // eax
_BYTE v8[88]; // [rsp+0h] [rbp-78h] BYREF
pthread_t newthread[4]; // [rsp+58h] [rbp-20h] BYREF

v0 = (void *)CriarSd("/tmp/sdserv");
v1 = CriaSdAuxiliar();
while ( 1 )
{
RecebMsgSock(v1, v0, v8);
v4 = "Apresenta";
v5 = 9LL;
v6 = v8;
do
{
if ( !v5 )
break;
v2 = *v6 < (unsigned int)*v4;
v3 = *v6++ == *v4++;
--v5;
}
while ( v3 );
if ( (!v2 && !v3) == v2 )
{
v7 = IdentificaOficial(v8, v6);
RegistaEntrada(v7, 2LL);
}
else if ( memcmp(v8, "Saida", 5uLL) )
{
pthread_create(newthread, 0LL, ThreadOficiais, v0);
EnviaMsgSock(v1, "Refeitorio responde!!!", v0);
}
}
}

original_code:
void *AtendeOficiais(void* argumento)
{
  pthread_t TidOficiais;
  sock_dgram *socketDgramOfic;
  sock_dgram *socketDgramRef;
  char msg[80];
  socketDgramRef= CriarSd( "/tmp/sdserv" );
  socketDgramOfic= CriaSdAuxiliar();
  while(1)
    {
      RecebMsgSock(socketDgramOfic,socketDgramRef, msg);
      if(strncmp( msg,"Apresenta", strlen("Apresenta") )== 0)
 {
   RegistaEntrada( IdentificaOficial(msg), 2);
 }
      else if (strncmp( msg, "Saida", strlen("Saida") )== 0)
 {
   CancelaThreadOfic();
 }
      else{
 pthread_create(&TidOficiais, ((void *)0),
         ThreadOficiais, (void*) socketDgramRef);
 EnviaMsgSock(socketDgramOfic, "Refeitorio responde!!!\0", socketDgramRef);
  CancelaThreadOfic();
      }
    }
  return 0;
}

canonical_original_code:
void *AtendeOficiais(void *argumento)
{
  unsigned long TidOficiais;
  struct sock_dgram_str *socketDgramOfic;
  struct sock_dgram_str *socketDgramRef;
  char msg[80];
  socketDgramRef= CriarSd( "/tmp/sdserv" );
  socketDgramOfic= CriaSdAuxiliar();
  while(1)
    {
      RecebMsgSock(socketDgramOfic,socketDgramRef, msg);
      if(strncmp( msg,"Apresenta", strlen("Apresenta") )== 0)
 {
   RegistaEntrada( IdentificaOficial(msg), 2);
 }
      else if (strncmp( msg, "Saida", strlen("Saida") )== 0)
 {
   CancelaThreadOfic();
 }
      else{
 pthread_create(&TidOficiais, ((void *)0),
         ThreadOficiais, (void *) socketDgramRef);
 EnviaMsgSock(socketDgramOfic, "Refeitorio responde!!!\0", socketDgramRef);
  CancelaThreadOfic();
      }
    }
  return 0;
}

variable_types:
TidOficiais
unsigned long

variable_types: struct sock_dgram_str { int sd; struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } addr; unsigned int addrlen; } *

variable_types:
msg
char [80]

user_defined_types: struct sock_dgram_str { int sd; struct sockaddr_un addr; unsigned int addrlen; }


CriaSocketStreamEscuta

decompiled_code:
char *__fastcall CriaSocketStreamEscuta(char *name)
{
char *v1; // rbx
size_t v2; // rax
char *v3; // rax
int v4; // eax

v1 = (char *)malloc(0x88uLL);
v2 = strlen(name);
v3 = (char *)malloc(v2 + 1);
*((_QWORD *)v1 + 1) = v3;
strcpy(v3, name);
v4 = socket(1, 1, 0);
*(_DWORD *)v1 = v4;
if ( v4 < 0 )
{
perror("Erro a criar socket");
exit(-1);
}
*((_WORD *)v1 + 8) = 1;
*(_QWORD *)(v1 + 18) = 0LL;
*(_QWORD *)(v1 + 118) = 0LL;
memset(
(void *)((unsigned __int64)(v1 + 26) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)v1 + 18 - (((_DWORD)v1 + 26) & 0xFFFFFFF8) + 108) >> 3));
strcpy(v1 + 18, name);
*((_DWORD *)v1 + 32) = strlen(v1 + 18) + 2;
unlink(name);
if ( bind(*(_DWORD *)v1, (const struct sockaddr *)v1 + 1, *((_DWORD *)v1 + 32)) < 0 )
{
perror("Erro no bind");
exit(-1);
}
if ( listen(*(_DWORD *)v1, 10) < 0 )
{
perror("Erro no listen");
exit(-1);
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func0(char *name)
{
char *v1; // rbx
size_t v2; // rax
char *v3; // rax
int v4; // eax

v1 = (char *)malloc(0x88uLL);
v2 = strlen(name);
v3 = (char *)malloc(v2 + 1);
*((_QWORD *)v1 + 1) = v3;
strcpy(v3, name);
v4 = socket(1, 1, 0);
*(_DWORD *)v1 = v4;
if ( v4 < 0 )
{
perror("Erro a criar socket");
exit(-1);
}
*((_WORD *)v1 + 8) = 1;
*(_QWORD *)(v1 + 18) = 0LL;
*(_QWORD *)(v1 + 118) = 0LL;
memset(
(void *)((unsigned __int64)(v1 + 26) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)v1 + 18 - (((_DWORD)v1 + 26) & 0xFFFFFFF8) + 108) >> 3));
strcpy(v1 + 18, name);
*((_DWORD *)v1 + 32) = strlen(v1 + 18) + 2;
unlink(name);
if ( bind(*(_DWORD *)v1, (const struct sockaddr *)v1 + 1, *((_DWORD *)v1 + 32)) < 0 )
{
perror("Erro no bind");
exit(-1);
}
if ( listen(*(_DWORD *)v1, 10) < 0 )
{
perror("Erro no listen");
exit(-1);
}
return v1;
}

original_code:
SocketStream* CriaSocketStreamEscuta(char* nome)
{
 SocketStream* novo;
 novo = (SocketStream*)malloc( sizeof(SocketStream) );
 novo->nome = (char*)malloc( sizeof(char)*(strlen(nome) + 1) );
 strcpy(novo->nome, nome);
 if ((novo->fd = socket(1, SOCK_STREAM, 0)) < 0 ) {
  perror("Erro a criar socket");
  exit(-1);
 }
 novo->my_addr.sun_family = 1;
 memset(novo->my_addr.sun_path, 0, sizeof(novo->my_addr.sun_path));
 strcpy(novo->my_addr.sun_path, nome);
 novo->addrlen = sizeof(novo->my_addr.sun_family)
   + strlen(novo->my_addr.sun_path);
 unlink(nome);
 if (bind(novo->fd, (struct sockaddr *)&(novo->my_addr), novo->addrlen) < 0 ) {
  perror("Erro no bind");
  exit(-1);
 }
 if (listen(novo->fd, 10) < 0 ) {
  perror("Erro no listen");
  exit(-1);
 }
 return novo;
}

canonical_original_code:
struct SocketStreamStr *CriaSocketStreamEscuta(char *nome)
{
 struct SocketStreamStr *novo;
 novo = (struct SocketStreamStr *)malloc( sizeof(SocketStream) );
 novo->nome = (char *)malloc( sizeof(char)*(strlen(nome) + 1) );
 strcpy(novo->nome, nome);
 if ((novo->fd = socket(1, SOCK_STREAM, 0)) < 0 ) {
  perror("Erro a criar socket");
  exit(-1);
 }
 novo->my_addr.sun_family = 1;
 memset(novo->my_addr.sun_path, 0, sizeof(novo->my_addr.sun_path));
 strcpy(novo->my_addr.sun_path, nome);
 novo->addrlen = sizeof(novo->my_addr.sun_family)
   + strlen(novo->my_addr.sun_path);
 unlink(nome);
 if (bind(novo->fd, (struct sockaddr *)&(novo->my_addr), novo->addrlen) < 0 ) {
  perror("Erro no bind");
  exit(-1);
 }
 if (listen(novo->fd, 10) < 0 ) {
  perror("Erro no listen");
  exit(-1);
 }
 return novo;
}

variable_types:
nome
char *

variable_types: struct SocketStreamStr { int fd; char *nome; struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } my_addr; unsigned int addrlen; } *

user_defined_types: struct SocketStreamStr { int fd; char *nome; struct sockaddr_un my_addr; unsigned int addrlen; }

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


AceitaLigacaoSocketStream

decompiled_code:
void *__fastcall AceitaLigacaoSocketStream(int *a1)
{
void *v1; // rbx
int v2; // eax

v1 = malloc(0x88uLL);
while ( 1 )
{
*((_DWORD *)v1 + 32) = 110;
v2 = accept(*a1, (struct sockaddr *)v1 + 1, (socklen_t *)v1 + 32);
*(_DWORD *)v1 = v2;
if ( v2 != -1 )
break;
if ( *_errno_location() != 4 )
{
perror("Erro da funcao accept");
exit(-1);
}
}
return v1;
}

canonical_decompiled_code:
void *__fastcall func1(int *a1)
{
void *v1; // rbx
int v2; // eax

v1 = malloc(0x88uLL);
while ( 1 )
{
*((_DWORD *)v1 + 32) = 110;
v2 = accept(*a1, (struct sockaddr *)v1 + 1, (socklen_t *)v1 + 32);
*(_DWORD *)v1 = v2;
if ( v2 != -1 )
break;
if ( *_errno_location() != 4 )
{
perror("Erro da funcao accept");
exit(-1);
}
}
return v1;
}

original_code:
SocketStream* AceitaLigacaoSocketStream(SocketStream* aberto)
{
 SocketStream* novo;
 novo = (SocketStream*)malloc( sizeof(SocketStream) );
 while(1) {
  novo->addrlen = sizeof(novo->my_addr);
  if(-1==( novo->fd = accept(aberto->fd,
     (struct sockaddr*)&(novo->my_addr),
     (socklen_t*)&(novo->addrlen)) ))
  {
   if( 4 == (*__errno_location ()) )
    continue;
   else{
    perror("Erro da funcao accept");
    exit( -1 );
   }
  }else
   break;
 }
 return novo;
}

canonical_original_code:
struct SocketStreamStr *AceitaLigacaoSocketStream(struct SocketStreamStr *aberto)
{
 struct SocketStreamStr *novo;
 novo = (struct SocketStreamStr *)malloc( sizeof(SocketStream) );
 while(1) {
  novo->addrlen = sizeof(novo->my_addr);
  if(-1==( novo->fd = accept(aberto->fd,
     (struct sockaddr *)&(novo->my_addr),
     (unsigned int *)&(novo->addrlen)) ))
  {
   if( 4 == (*__errno_location ()) )
    continue;
   else{
    perror("Erro da funcao accept");
    exit( -1 );
   }
  }else
   break;
 }
 return novo;
}

variable_types:
aberto
struct SocketStreamStr *

return_type: struct SocketStreamStr { int fd; char *nome; struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } my_addr; unsigned int addrlen; } *


EnviaSocketStream

decompiled_code:
__int64 __fastcall EnviaSocketStream(int *a1, const char *a2)
{
int v2; // er13
int v3; // eax
int v4; // eax

if ( !a2 )
return 0LL;
v2 = strlen(a2) + 1;
if ( v2 <= 0 )
return 0LL;
do
{
while ( 1 )
{
v3 = write(*a1, a2, v2);
if ( v3 >= 0 )
break;
v4 = *_errno_location();
if ( v4 == 32 )
{
perror("write() falhou: O socket foi fechado !?");
return 0xFFFFFFFFLL;
}
if ( v4 != 4 )
{
perror("Erro no write");
exit(-1);
}
}
v2 -= v3;
}
while ( v2 > 0 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, const char *a2)
{
int v2; // er13
int v3; // eax
int v4; // eax

if ( !a2 )
return 0LL;
v2 = strlen(a2) + 1;
if ( v2 <= 0 )
return 0LL;
do
{
while ( 1 )
{
v3 = write(*a1, a2, v2);
if ( v3 >= 0 )
break;
v4 = *_errno_location();
if ( v4 == 32 )
{
perror("write() falhou: O socket foi fechado !?");
return 0xFFFFFFFFLL;
}
if ( v4 != 4 )
{
perror("Erro no write");
exit(-1);
}
}
v2 -= v3;
}
while ( v2 > 0 );
return 0LL;
}

original_code:
int EnviaSocketStream(SocketStream* destino, char* msg)
{
 int size, writen;
 if(((void *)0) == msg){
  return 0;
 }
 size = strlen(msg) + 1 ;
 while(size > 0){
  while(1){
   if ( (writen=write(destino->fd, msg, size)) < 0){
    if(32 == (*__errno_location ())){
     perror("write() falhou: O socket foi fechado !?");
     return -1;
    }else if(4 == (*__errno_location ())){
     continue;
    }else{
     perror("Erro no write");
     exit(-1);
    }
   }else{
    break;
   }
  }
  size -= writen;
 }
 return 0;
}

canonical_original_code:
int EnviaSocketStream(struct SocketStreamStr *destino, char *msg)
{
 int size; int writen;
 if(((void *)0) == msg){
  return 0;
 }
 size = strlen(msg) + 1 ;
 while(size > 0){
  while(1){
   if ( (writen=write(destino->fd, msg, size)) < 0){
    if(32 == (*__errno_location ())){
     perror("write() falhou: O socket foi fechado !?");
     return -1;
    }else if(4 == (*__errno_location ())){
     continue;
    }else{
     perror("Erro no write");
     exit(-1);
    }
   }else{
    break;
   }
  }
  size -= writen;
 }
 return 0;
}

variable_types: size int

variable_types: writen int

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


RecebeSocketStream

decompiled_code:
char *__fastcall RecebeSocketStream(int *a1)
{
char *v1; // rbp
size_t v2; // rax
int v3; // eax
int v4; // ebx
int v5; // er13
char v7[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = (char *)malloc(1uLL);
*v1 = 0;
v7[0] = 0;
do
{
while ( 1 )
{
v3 = read(*a1, v7, 0x13uLL);
v4 = v3;
if ( v3 >= 0 )
break;
if ( *_errno_location() != 4 )
{
perror("Erro no read");
exit(-1);
}
}
if ( v3 <= 0 )
break;
v5 = v3 - 1;
if ( v7[v3 - 1] )
v7[v3] = 0;
v2 = strlen(v1);
v1 = (char *)realloc(v1, v4 + v2 + 1);
strcat(v1, v7);
}
while ( v7[v5] );
return v1;
}

canonical_decompiled_code:
char *__fastcall func4(int *a1)
{
char *v1; // rbp
size_t v2; // rax
int v3; // eax
int v4; // ebx
int v5; // er13
char v7[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = (char *)malloc(1uLL);
*v1 = 0;
v7[0] = 0;
do
{
while ( 1 )
{
v3 = read(*a1, v7, 0x13uLL);
v4 = v3;
if ( v3 >= 0 )
break;
if ( *_errno_location() != 4 )
{
perror("Erro no read");
exit(-1);
}
}
if ( v3 <= 0 )
break;
v5 = v3 - 1;
if ( v7[v3 - 1] )
v7[v3] = 0;
v2 = strlen(v1);
v1 = (char *)realloc(v1, v4 + v2 + 1);
strcat(v1, v7);
}
while ( v7[v5] );
return v1;
}

original_code:
char* RecebeSocketStream(SocketStream* destino)
{
 char buf[20];
 char* msg;
 int size;
 size = 0;
 msg = (char*)malloc(sizeof(char));
 msg[0]='\0';
 buf[0]='\0';
 do{
  while( (size = read(destino->fd, buf, 20 -1)) < 0 ){
   if(4 == (*__errno_location ())){
    continue;
   }else{
    perror("Erro no read");
    exit(-1);
   }
  }
  if(size > 0){
   if(buf[size-1] != '\0')
    buf[size]='\0';
  }else{
   break;
  }
  msg = (char*)realloc( msg, sizeof(char)*( strlen(msg) + size + 1) );
  strcat(msg, buf);
 }while(buf[size-1] != '\0');
 return msg;
}

canonical_original_code:
char *RecebeSocketStream(struct SocketStreamStr *destino)
{
 char buf[20];
 char *msg;
 int size;
 size = 0;
 msg = (char *)malloc(sizeof(char));
 msg[0]='\0';
 buf[0]='\0';
 do{
  while( (size = read(destino->fd, buf, 20 -1)) < 0 ){
   if(4 == (*__errno_location ())){
    continue;
   }else{
    perror("Erro no read");
    exit(-1);
   }
  }
  if(size > 0){
   if(buf[size-1] != '\0')
    buf[size]='\0';
  }else{
   break;
  }
  msg = (char *)realloc( msg, sizeof(char)*( strlen(msg) + size + 1) );
  strcat(msg, buf);
 }while(buf[size-1] != '\0');
 return msg;
}

variable_types:
buf
char [20]

return_type: char *


FechaSocketStream

decompiled_code:
void __fastcall FechaSocketStream(void *ptr)
{
const char *v2; // rdi

if ( close(*(_DWORD *)ptr) < 0 )
perror("Erro no close");
v2 = (const char *)*((_QWORD *)ptr + 1);
if ( v2 )
{
unlink(v2);
free(*((void **)ptr + 1));
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
const char *v2; // rdi

if ( close(*(_DWORD *)ptr) < 0 )
perror("Erro no close");
v2 = (const char *)*((_QWORD *)ptr + 1);
if ( v2 )
{
unlink(v2);
free(*((void **)ptr + 1));
}
free(ptr);
}

original_code:
void FechaSocketStream(SocketStream* sock){
 if(close(sock->fd) < 0){
  perror("Erro no close");
 }
 if(((void *)0) != sock->nome){
  unlink(sock->nome);
  free(sock->nome);
 }
 free(sock);
 return;
}

canonical_original_code:
void FechaSocketStream(struct SocketStreamStr *sock){
 if(close(sock->fd) < 0){
  perror("Erro no close");
 }
 if(((void *)0) != sock->nome){
  unlink(sock->nome);
  free(sock->nome);
 }
 free(sock);
 return;
}


CriaStock

decompiled_code:
__int64 CriaStock()
{
int v0; // eax
int v1; // ebp
__int64 result; // rax
__int64 i; // rbx
unsigned int v4; // eax

v0 = semget(123, *(&stock + 8), 950);
if ( v0 < 0 )
{
fwrite("Erro em CriaStock: semget().\n", 1uLL, 0x1DuLL, stderr);
perror("Erro em CriaStock: semget().");
exit(-1);
}
v1 = v0;
result = (unsigned int)*(&stock + 8);
for ( i = 0LL; (int)result > (int)i; ++i )
{
v4 = atoi((const char *)(*((_QWORD *)&stock + 1) + 25 * i + 20));
if ( semctl(v1, i, 16, v4) < 0 )
{
fwrite("Erro em CriaStock: semctl().\n", 1uLL, 0x1DuLL, stderr);
perror("Erro em CriaStock: semctl().");
exit(-1);
}
result = (unsigned int)*(&stock + 8);
}
*(&stock + 9) = result;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
int v1; // ebp
__int64 result; // rax
__int64 i; // rbx
unsigned int v4; // eax

v0 = semget(123, *(&stock + 8), 950);
if ( v0 < 0 )
{
fwrite("Erro em CriaStock: semget().\n", 1uLL, 0x1DuLL, stderr);
perror("Erro em CriaStock: semget().");
exit(-1);
}
v1 = v0;
result = (unsigned int)*(&stock + 8);
for ( i = 0LL; (int)result > (int)i; ++i )
{
v4 = atoi((const char *)(*((_QWORD *)&stock + 1) + 25 * i + 20));
if ( semctl(v1, i, 16, v4) < 0 )
{
fwrite("Erro em CriaStock: semctl().\n", 1uLL, 0x1DuLL, stderr);
perror("Erro em CriaStock: semctl().");
exit(-1);
}
result = (unsigned int)*(&stock + 8);
}
*(&stock + 9) = result;
return result;
}

original_code:
void CriaStock()
{
 int id;
 int i;
 struct{
  int val;
 }arg;
 if( ( id = semget( 123, stock.n_prod, 0666|01000 ) ) < 0 ) {
  fprintf( stderr, "Erro em CriaStock: semget().\n" );
  perror("Erro em CriaStock: semget().");
  exit( -1 );
 }
 for( i = 0 ; i < stock.n_prod ; i++ )
 {
  arg.val = atoi( stock.inicio[i].quantidade );
  if( semctl( id, i, 16,arg ) < 0 ) {
   fprintf( stderr, "Erro em CriaStock: semctl().\n" );
   perror("Erro em CriaStock: semctl().");
   exit( -1 );
  }
 }
 stock.n_sem = stock.n_prod;
 return;
}

canonical_original_code:
void CriaStock()
{
 int id;
 int i;
 struct { int val; } arg;
 if( ( id = semget( 123, stock.n_prod, 0666|01000 ) ) < 0 ) {
  fprintf( stderr, "Erro em CriaStock: semget().\n" );
  perror("Erro em CriaStock: semget().");
  exit( -1 );
 }
 for( i = 0 ; i < stock.n_prod ; i++ )
 {
  arg.val = atoi( stock.inicio[i].quantidade );
  if( semctl( id, i, 16,arg ) < 0 ) {
   fprintf( stderr, "Erro em CriaStock: semctl().\n" );
   perror("Erro em CriaStock: semctl().");
   exit( -1 );
  }
 }
 stock.n_sem = stock.n_prod;
 return;
}

variable_types: id int

variable_types: i int

variable_types:
arg
struct { int val; }


DestroiStock

decompiled_code:
int DestroiStock()
{
int v0; // eax
int v1; // er12
__int64 i; // rbx
unsigned int v3; // eax
int result; // eax

v0 = semget(123, 0, 0);
if ( v0 < 0 )
{
fwrite("Erro em DestroiSemaforo: semget().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
v1 = v0;
for ( i = 0LL; *(&stock + 9) > (int)i; ++i )
{
v3 = semctl(v1, i, 12);
sprintf((char *)(*((_QWORD *)&stock + 1) + 25 * i + 20), "%4d", v3);
}
result = semctl(v1, 0, 0);
if ( result < 0 )
{
fwrite("Erro em DestroiSemaforo: semctl().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
int func1()
{
int v0; // eax
int v1; // er12
__int64 i; // rbx
unsigned int v3; // eax
int result; // eax

v0 = semget(123, 0, 0);
if ( v0 < 0 )
{
fwrite("Erro em DestroiSemaforo: semget().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
v1 = v0;
for ( i = 0LL; *(&stock + 9) > (int)i; ++i )
{
v3 = semctl(v1, i, 12);
sprintf((char *)(*((_QWORD *)&stock + 1) + 25 * i + 20), "%4d", v3);
}
result = semctl(v1, 0, 0);
if ( result < 0 )
{
fwrite("Erro em DestroiSemaforo: semctl().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void DestroiStock()
{
 int id, i;
 if( ( id = semget( 123, 0, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em DestroiSemaforo: semget().\n" );
  exit( -1 );
 }
 for( i = 0 ; i < stock.n_sem ; i++ )
 {
  sprintf( stock.inicio[i].quantidade, "%4d", semctl(id, i, 12) );
 }
 if( semctl( id, 0, 0) < 0 ) {
  fprintf( stderr, "Erro em DestroiSemaforo: semctl().\n" );
  exit( -1 );
 }
 return;
}

canonical_original_code:
void DestroiStock()
{
 int id; int i;
 if( ( id = semget( 123, 0, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em DestroiSemaforo: semget().\n" );
  exit( -1 );
 }
 for( i = 0 ; i < stock.n_sem ; i++ )
 {
  sprintf( stock.inicio[i].quantidade, "%4d", semctl(id, i, 12) );
 }
 if( semctl( id, 0, 0) < 0 ) {
  fprintf( stderr, "Erro em DestroiSemaforo: semctl().\n" );
  exit( -1 );
 }
 return;
}


DevolveValorStock

decompiled_code:
int __fastcall DevolveValorStock(int semnum)
{
int v1; // eax
int result; // eax

v1 = semget(123, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em DevolveValorStock: semget().\n", 1uLL, 0x25uLL, stderr);
exit(-1);
}
result = semctl(v1, semnum, 12);
if ( result < 0 )
{
fwrite("Erro em DevolveValorStock: semctl().\n", 1uLL, 0x25uLL, stderr);
perror("Erro em DevolveValorStock: semctl().");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int semnum)
{
int v1; // eax
int result; // eax

v1 = semget(123, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em DevolveValorStock: semget().\n", 1uLL, 0x25uLL, stderr);
exit(-1);
}
result = semctl(v1, semnum, 12);
if ( result < 0 )
{
fwrite("Erro em DevolveValorStock: semctl().\n", 1uLL, 0x25uLL, stderr);
perror("Erro em DevolveValorStock: semctl().");
exit(-1);
}
return result;
}

original_code:
int DevolveValorStock(int indice)
{
 int id;
        int val;
 if( ( id = semget( 123, 1, 0) ) < 0 ) {
  fprintf( stderr, "Erro em DevolveValorStock: semget().\n" );
  exit( -1 );
 }
 if( ( val = semctl( id, indice, 12 ) ) < 0 ) {
  fprintf( stderr, "Erro em DevolveValorStock: semctl().\n" );
  perror("Erro em DevolveValorStock: semctl().");
  exit( -1 );
 }
 return val;
}

variable_types: indice int

variable_types: val int


AdicionaProdMemMap

decompiled_code:
char *__fastcall AdicionaProdMemMap(char *src, char *a2)
{
__int64 v3; // r14
__int64 v4; // rax
unsigned __int64 v5; // rbp
unsigned __int64 v6; // r15
__off_t v7; // r14
void *v8; // rdi
__int64 v9; // rbp
char *v10; // rax
char *result; // rax

v3 = *((_QWORD *)&stock + 3);
v4 = *((_QWORD *)&stock + 2) - *((_QWORD *)&stock + 1);
if ( (unsigned __int64)(v3 - v4) <= 0x18 )
{
v5 = v4 - v3 + 25;
v6 = sysconf(30);
v7 = sysconf(30) * (v5 / v6 + 1) + v3;
v8 = (void *)*((_QWORD *)&stock + 1);
v9 = *((_QWORD *)&stock + 2) - (_QWORD)v8;
munmap(v8, *((_QWORD *)&stock + 3));
*((_QWORD *)&stock + 3) = v7;
if ( ftruncate(stock, v7) < 0 )
_assert_fail("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 0xA9u, "AdicionaProdMemMap");
v10 = (char *)mmap(0LL, *((_QWORD *)&stock + 3), 3, 1, stock, 0LL);
*((_QWORD *)&stock + 1) = v10;
if ( v10 == (char *)-1LL )
_assert_fail(
"( stock.inicio= mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED",
"stock.c",
0xABu,
"AdicionaProdMemMap");
*((_QWORD *)&stock + 2) = &v10[v9];
}
strncpy(*((char **)&stock + 2), src, 0x14uLL);
result = strncpy((char *)(*((_QWORD *)&stock + 2) + 20LL), a2, 5uLL);
*((_QWORD *)&stock + 2) += 25LL;
++*(&stock + 8);
return result;
}

canonical_decompiled_code:
char *__fastcall func3(char *src, char *a2)
{
__int64 v3; // r14
__int64 v4; // rax
unsigned __int64 v5; // rbp
unsigned __int64 v6; // r15
__off_t v7; // r14
void *v8; // rdi
__int64 v9; // rbp
char *v10; // rax
char *result; // rax

v3 = *((_QWORD *)&stock + 3);
v4 = *((_QWORD *)&stock + 2) - *((_QWORD *)&stock + 1);
if ( (unsigned __int64)(v3 - v4) <= 0x18 )
{
v5 = v4 - v3 + 25;
v6 = sysconf(30);
v7 = sysconf(30) * (v5 / v6 + 1) + v3;
v8 = (void *)*((_QWORD *)&stock + 1);
v9 = *((_QWORD *)&stock + 2) - (_QWORD)v8;
munmap(v8, *((_QWORD *)&stock + 3));
*((_QWORD *)&stock + 3) = v7;
if ( ftruncate(stock, v7) < 0 )
_assert_fail("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 0xA9u, "AdicionaProdMemMap");
v10 = (char *)mmap(0LL, *((_QWORD *)&stock + 3), 3, 1, stock, 0LL);
*((_QWORD *)&stock + 1) = v10;
if ( v10 == (char *)-1LL )
_assert_fail(
"( stock.inicio= mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED",
"stock.c",
0xABu,
"AdicionaProdMemMap");
*((_QWORD *)&stock + 2) = &v10[v9];
}
strncpy(*((char **)&stock + 2), src, 0x14uLL);
result = strncpy((char *)(*((_QWORD *)&stock + 2) + 20LL), a2, 5uLL);
*((_QWORD *)&stock + 2) += 25LL;
++*(&stock + 8);
return result;
}

original_code:
void AdicionaProdMemMap(char* nome, char* quantidade)
{
 size_t novadim;
 size_t fim;
 int nbytes;
 size_t nleft;
 nleft = stock.dim - (stock.fim-stock.inicio)*sizeof(Produto);
 nbytes= sizeof(Produto);
 if(nleft < nbytes)
 {
  novadim = stock.dim + (1 + (nbytes-nleft)/sysconf(_SC_PAGESIZE))*sysconf(_SC_PAGESIZE);
  fim = (stock.fim)-(stock.inicio);
  munmap(stock.inicio, stock.dim);
  stock.dim = novadim;
  ((void) sizeof ((ftruncate( stock.fd, stock.dim ) >= 0) ? 1 : 0), __extension__ ({ if (ftruncate( stock.fd, stock.dim ) >= 0) ; else __assert_fail ("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 169, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( stock.inicio= mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ? 1 : 0), __extension__ ({ if (( stock.inicio= mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ; else __assert_fail ("( stock.inicio= mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED", "stock.c", 171, __extension__ __PRETTY_FUNCTION__); }));
  stock.fim= &( stock.inicio[fim] );
 }
 strncpy( stock.fim->nome, nome, 20 );
 strncpy( stock.fim->quantidade, quantidade, 5 );
 stock.fim = &( stock.fim[1] );
 stock.n_prod++;
 return;
}

canonical_original_code:
void AdicionaProdMemMap(char *nome, char *quantidade)
{
 unsigned long novadim;
 unsigned long fim;
 int nbytes;
 unsigned long nleft;
 nleft = stock.dim - (stock.fim-stock.inicio)*sizeof(Produto);
 nbytes= sizeof(Produto);
 if(nleft < nbytes)
 {
  novadim = stock.dim + (1 + (nbytes-nleft)/sysconf(_SC_PAGESIZE))*sysconf(_SC_PAGESIZE);
  fim = (stock.fim)-(stock.inicio);
  munmap(stock.inicio, stock.dim);
  stock.dim = novadim;
  ((void) sizeof ((ftruncate( stock.fd, stock.dim ) >= 0) ? 1 : 0), __extension__ ({ if (ftruncate( stock.fd, stock.dim ) >= 0) ; else __assert_fail ("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 169, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( stock.inicio= mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ? 1 : 0), __extension__ ({ if (( stock.inicio= mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ; else __assert_fail ("( stock.inicio= mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED", "stock.c", 171, __extension__ __PRETTY_FUNCTION__); }));
  stock.fim= &( stock.inicio[fim] );
 }
 strncpy( stock.fim->nome, nome, 20 );
 strncpy( stock.fim->quantidade, quantidade, 5 );
 stock.fim = &( stock.fim[1] );
 stock.n_prod++;
 return;
}

variable_types:
quantidade
char *

variable_types:
novadim
unsigned long

variable_types:
fim
unsigned long

variable_types: nbytes int

variable_types:
nleft
unsigned long


GetIndiceStock

decompiled_code:
__int64 __fastcall GetIndiceStock(char *s2)
{
int v1; // er12
__int64 v2; // r13
unsigned int v3; // ebx

v1 = *(&stock + 8);
if ( v1 <= 0 )
return (unsigned int)-1;
v2 = *((_QWORD *)&stock + 1);
v3 = 0;
while ( strncmp((const char *)(v2 + 25LL * (__int16)v3), s2, 0x14uLL) )
{
if ( (__int16)++v3 >= v1 )
return (unsigned int)-1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s2)
{
int v1; // er12
__int64 v2; // r13
unsigned int v3; // ebx

v1 = *(&stock + 8);
if ( v1 <= 0 )
return (unsigned int)-1;
v2 = *((_QWORD *)&stock + 1);
v3 = 0;
while ( strncmp((const char *)(v2 + 25LL * (__int16)v3), s2, 0x14uLL) )
{
if ( (__int16)++v3 >= v1 )
return (unsigned int)-1;
}
return v3;
}

original_code:
short GetIndiceStock(char* nome)
{
 short i;
 for(i=0 ; i < stock.n_prod ; i++)
 {
  if( strncmp( stock.inicio[i].nome, nome, 20 ) == 0)
  {
    return i;
  }
 }
 return -1;
}

canonical_original_code:
short GetIndiceStock(char *nome)
{
 short i;
 for(i=0 ; i < stock.n_prod ; i++)
 {
  if( strncmp( stock.inicio[i].nome, nome, 20 ) == 0)
  {
    return i;
  }
 }
 return -1;
}

variable_types: i short

return_type: short


AssinalaStock

decompiled_code:
void __fastcall AssinalaStock(char *s2, int a2)
{
int v3; // eax
int v4; // er14
struct sembuf *v5; // rax
struct sembuf *v6; // r13
struct sembuf *v7; // rbx
struct sembuf *v8; // r12

v3 = semget(123, 0, 0);
if ( v3 < 0 )
{
fwrite("Erro em AssinalaStock: semget().\n", 1uLL, 0x21uLL, stderr);
exit(-1);
}
v4 = v3;
v5 = (struct sembuf *)malloc(6LL * a2);
v6 = v5;
if ( a2 > 0 )
{
v7 = v5;
v8 = &v5[a2];
do
{
v7->sem_num = GetIndiceStock(s2);
v7->sem_op = atoi(s2 + 20);
v7->sem_flg = 0;
s2 += 25;
++v7;
}
while ( v7 != v8 );
}
if ( semop(v4, v6, a2) < 0 )
{
fwrite("Erro em AssinalaStock: semop().\n", 1uLL, 0x20uLL, stderr);
exit(-1);
}
free(v6);
}

canonical_decompiled_code:
void __fastcall func5(char *s2, int a2)
{
int v3; // eax
int v4; // er14
struct sembuf *v5; // rax
struct sembuf *v6; // r13
struct sembuf *v7; // rbx
struct sembuf *v8; // r12

v3 = semget(123, 0, 0);
if ( v3 < 0 )
{
fwrite("Erro em AssinalaStock: semget().\n", 1uLL, 0x21uLL, stderr);
exit(-1);
}
v4 = v3;
v5 = (struct sembuf *)malloc(6LL * a2);
v6 = v5;
if ( a2 > 0 )
{
v7 = v5;
v8 = &v5[a2];
do
{
v7->sem_num = func4(s2);
v7->sem_op = atoi(s2 + 20);
v7->sem_flg = 0;
s2 += 25;
++v7;
}
while ( v7 != v8 );
}
if ( semop(v4, v6, a2) < 0 )
{
fwrite("Erro em AssinalaStock: semop().\n", 1uLL, 0x20uLL, stderr);
exit(-1);
}
free(v6);
}

original_code:
void AssinalaStock(Produto* produtos, int n_prod)
{
 struct sembuf* op;
 int id, i;
 if( ( id = semget( 123, 0, 0) ) < 0 ) {
  fprintf( stderr, "Erro em AssinalaStock: semget().\n" );
  exit( -1 );
 }
 op = (struct sembuf*)malloc(sizeof(struct sembuf)*n_prod);
 for( i = 0 ; i < n_prod ; i++ ) {
  op[i].sem_num = GetIndiceStock( produtos[i].nome );
  op[i].sem_op = atoi( produtos[i].quantidade );
  op[i].sem_flg = 0;
 }
 if( semop( id, op, n_prod ) < 0 ) {
  fprintf( stderr, "Erro em AssinalaStock: semop().\n" );
  exit( -1 );
 }
 free(op);
 return;
}

canonical_original_code:
void AssinalaStock(struct ProdutoStr *produtos, int n_prod)
{
 struct sembuf *op;
 int id; int i;
 if( ( id = semget( 123, 0, 0) ) < 0 ) {
  fprintf( stderr, "Erro em AssinalaStock: semget().\n" );
  exit( -1 );
 }
 op = (struct sembuf *)malloc(sizeof(struct sembuf)*n_prod);
 for( i = 0 ; i < n_prod ; i++ ) {
  op[i].sem_num = GetIndiceStock( produtos[i].nome );
  op[i].sem_op = atoi( produtos[i].quantidade );
  op[i].sem_flg = 0;
 }
 if( semop( id, op, n_prod ) < 0 ) {
  fprintf( stderr, "Erro em AssinalaStock: semop().\n" );
  exit( -1 );
 }
 free(op);
 return;
}

variable_types: struct ProdutoStr { char nome[20]; char quantidade[5]; } *

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


EsperaStock

decompiled_code:
int __fastcall EsperaStock(char *s2, int a2)
{
int v3; // eax
int v4; // er14
struct sembuf *v5; // rax
struct sembuf *v6; // r13
struct sembuf *v7; // rbx
struct sembuf *v8; // r12
int result; // eax

v3 = semget(123, 0, 0);
if ( v3 < 0 )
{
fwrite("Erro em EsperaSemaforo: semget().\n", 1uLL, 0x22uLL, stderr);
exit(-1);
}
v4 = v3;
v5 = (struct sembuf *)malloc(6LL * a2);
v6 = v5;
if ( a2 > 0 )
{
v7 = v5;
v8 = &v5[a2];
do
{
v7->sem_num = GetIndiceStock(s2);
*(_DWORD *)&v7->sem_op = (unsigned __int16)-atoi(s2 + 20);
s2 += 25;
++v7;
}
while ( v7 != v8 );
}
result = semop(v4, v6, a2);
if ( result < 0 )
{
fwrite("Erro em EsperaSemaforo: semop().\n", 1uLL, 0x21uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(char *s2, int a2)
{
int v3; // eax
int v4; // er14
struct sembuf *v5; // rax
struct sembuf *v6; // r13
struct sembuf *v7; // rbx
struct sembuf *v8; // r12
int result; // eax

v3 = semget(123, 0, 0);
if ( v3 < 0 )
{
fwrite("Erro em EsperaSemaforo: semget().\n", 1uLL, 0x22uLL, stderr);
exit(-1);
}
v4 = v3;
v5 = (struct sembuf *)malloc(6LL * a2);
v6 = v5;
if ( a2 > 0 )
{
v7 = v5;
v8 = &v5[a2];
do
{
v7->sem_num = func4(s2);
*(_DWORD *)&v7->sem_op = (unsigned __int16)-atoi(s2 + 20);
s2 += 25;
++v7;
}
while ( v7 != v8 );
}
result = semop(v4, v6, a2);
if ( result < 0 )
{
fwrite("Erro em EsperaSemaforo: semop().\n", 1uLL, 0x21uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void EsperaStock(Produto* produtos, int n_prod)
{
 struct sembuf* op;
 int id, i;
 if( ( id = semget( 123, 0, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforo: semget().\n" );
  exit( -1 );
 }
 op = (struct sembuf*)malloc(sizeof(struct sembuf)*n_prod);
 for( i = 0 ; i < n_prod ; i++ ) {
  op[i].sem_num = GetIndiceStock( produtos[i].nome );
  op[i].sem_op = - atoi( produtos[i].quantidade );
  op[i].sem_flg = 0;
 }
 if( semop( id, op, n_prod ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforo: semop().\n" );
  exit( -1 );
 }
 return;
}

canonical_original_code:
void EsperaStock(struct ProdutoStr *produtos, int n_prod)
{
 struct sembuf *op;
 int id; int i;
 if( ( id = semget( 123, 0, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforo: semget().\n" );
  exit( -1 );
 }
 op = (struct sembuf *)malloc(sizeof(struct sembuf)*n_prod);
 for( i = 0 ; i < n_prod ; i++ ) {
  op[i].sem_num = GetIndiceStock( produtos[i].nome );
  op[i].sem_op = - atoi( produtos[i].quantidade );
  op[i].sem_flg = 0;
 }
 if( semop( id, op, n_prod ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforo: semop().\n" );
  exit( -1 );
 }
 return;
}

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


MapeiaFicheiroStock

decompiled_code:
unsigned __int64 __fastcall MapeiaFicheiroStock(const char *a1)
{
int v1; // eax
__off_t v2; // rbx
__int64 v3; // rbp
__off_t v4; // rsi
char *v5; // rcx
unsigned __int64 result; // rax
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

v1 = open(a1, 66, 438LL);
stock = v1;
if ( v1 < 0 )
_assert_fail("( stock.fd = open(nomefiche, O_RDWR | O_CREAT, 0666)) >=0", "stock.c", 0xD2u, "MapeiaFicheiroStock");
fstat(v1, &v7);
v2 = v7.st_size;
v3 = sysconf(30);
v4 = sysconf(30) * (v2 / v3 + 1);
*((_QWORD *)&stock + 3) = v4;
if ( ftruncate(stock, v4) < 0 )
_assert_fail("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 0xE6u, "MapeiaFicheiroStock");
v5 = (char *)mmap(0LL, *((_QWORD *)&stock + 3), 3, 1, stock, 0LL);
*((_QWORD *)&stock + 1) = v5;
if ( v5 == (char *)-1LL )
_assert_fail(
"( stock.inicio = mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED",
"stock.c",
0xE9u,
"MapeiaFicheiroStock");
result = 25 * (v7.st_size / 0x19uLL);
*((_QWORD *)&stock + 2) = &v5[result];
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(const char *a1)
{
int v1; // eax
__off_t v2; // rbx
__int64 v3; // rbp
__off_t v4; // rsi
char *v5; // rcx
unsigned __int64 result; // rax
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

v1 = open(a1, 66, 438LL);
stock = v1;
if ( v1 < 0 )
_assert_fail("( stock.fd = open(nomefiche, O_RDWR | O_CREAT, 0666)) >=0", "stock.c", 0xD2u, "MapeiaFicheiroStock");
fstat(v1, &v7);
v2 = v7.st_size;
v3 = sysconf(30);
v4 = sysconf(30) * (v2 / v3 + 1);
*((_QWORD *)&stock + 3) = v4;
if ( ftruncate(stock, v4) < 0 )
_assert_fail("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 0xE6u, "MapeiaFicheiroStock");
v5 = (char *)mmap(0LL, *((_QWORD *)&stock + 3), 3, 1, stock, 0LL);
*((_QWORD *)&stock + 1) = v5;
if ( v5 == (char *)-1LL )
_assert_fail(
"( stock.inicio = mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED",
"stock.c",
0xE9u,
"MapeiaFicheiroStock");
result = 25 * (v7.st_size / 0x19uLL);
*((_QWORD *)&stock + 2) = &v5[result];
return result;
}

original_code:
void MapeiaFicheiroStock(char* nomefiche)
{
 struct stat infofich;
 ((void) sizeof ((( stock.fd = open(nomefiche, 02 | 0100, 0666)) >=0) ? 1 : 0), __extension__ ({ if (( stock.fd = open(nomefiche, 02 | 0100, 0666)) >=0) ; else __assert_fail ("( stock.fd = open(nomefiche, O_RDWR | O_CREAT, 0666)) >=0", "stock.c", 210, __extension__ __PRETTY_FUNCTION__); }));
 fstat( stock.fd, &infofich);
 stock.dim = (size_t)(1+infofich.st_size/sysconf(_SC_PAGESIZE))*sysconf(_SC_PAGESIZE);
 ((void) sizeof ((ftruncate( stock.fd, stock.dim ) >= 0) ? 1 : 0), __extension__ ({ if (ftruncate( stock.fd, stock.dim ) >= 0) ; else __assert_fail ("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 230, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((( stock.inicio = mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ? 1 : 0), __extension__ ({ if (( stock.inicio = mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ; else __assert_fail ("( stock.inicio = mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED", "stock.c", 233, __extension__ __PRETTY_FUNCTION__); }));
 stock.fim = &( stock.inicio[ infofich.st_size/sizeof(Produto) ] );
 return;
}

canonical_original_code:
void MapeiaFicheiroStock(char *nomefiche)
{
 struct stat infofich;
 ((void) sizeof ((( stock.fd = open(nomefiche, 02 | 0100, 0666)) >=0) ? 1 : 0), __extension__ ({ if (( stock.fd = open(nomefiche, 02 | 0100, 0666)) >=0) ; else __assert_fail ("( stock.fd = open(nomefiche, O_RDWR | O_CREAT, 0666)) >=0", "stock.c", 210, __extension__ __PRETTY_FUNCTION__); }));
 fstat( stock.fd, &infofich);
 stock.dim = (unsigned long)(1+infofich.st_size/sysconf(_SC_PAGESIZE))*sysconf(_SC_PAGESIZE);
 ((void) sizeof ((ftruncate( stock.fd, stock.dim ) >= 0) ? 1 : 0), __extension__ ({ if (ftruncate( stock.fd, stock.dim ) >= 0) ; else __assert_fail ("ftruncate( stock.fd, stock.dim ) >= 0", "stock.c", 230, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((( stock.inicio = mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ? 1 : 0), __extension__ ({ if (( stock.inicio = mmap( ((void *)0), stock.dim, 0x1 | 0x2, 0x01, stock.fd, 0 ) ) != ((void *) -1)) ; else __assert_fail ("( stock.inicio = mmap( NULL, stock.dim, PROT_READ | PROT_WRITE, MAP_SHARED, stock.fd, 0 ) ) != MAP_FAILED", "stock.c", 233, __extension__ __PRETTY_FUNCTION__); }));
 stock.fim = &( stock.inicio[ infofich.st_size/sizeof(Produto) ] );
 return;
}

variable_types:
nomefiche
char *

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

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


DesMapeiaFicheiroStock

decompiled_code:
int DesMapeiaFicheiroStock()
{
munmap(*((void **)&stock + 1), *((_QWORD *)&stock + 3));
ftruncate(stock, *((_QWORD *)&stock + 2) - *((_QWORD *)&stock + 1));
return close(stock);
}

canonical_decompiled_code:
int func8()
{
munmap(*((void **)&stock + 1), *((_QWORD *)&stock + 3));
ftruncate(stock, *((_QWORD *)&stock + 2) - *((_QWORD *)&stock + 1));
return close(stock);
}

original_code:
void DesMapeiaFicheiroStock()
{
 munmap( stock.inicio, stock.dim);
 ftruncate( stock.fd, (stock.fim-stock.inicio)*sizeof(Produto));
 close( stock.fd );
 return;
}


CriaSemaforo

decompiled_code:
int __fastcall CriaSemaforo(key_t a1)
{
int v1; // eax
int result; // eax

v1 = semget(a1, 1, 950);
if ( v1 < 0 )
{
fwrite("Erro em CriaSemaforo: semget().\n", 1uLL, 0x20uLL, stderr);
perror("Erro em CriaSemaforo: semget().");
exit(-1);
}
result = semctl(v1, 0, 16, 0LL);
if ( result < 0 )
{
fwrite("Erro em CriaSemaforo: semctl().\n", 1uLL, 0x20uLL, stderr);
perror("Erro em CriaSemaforo: semctl().");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(key_t a1)
{
int v1; // eax
int result; // eax

v1 = semget(a1, 1, 950);
if ( v1 < 0 )
{
fwrite("Erro em CriaSemaforo: semget().\n", 1uLL, 0x20uLL, stderr);
perror("Erro em CriaSemaforo: semget().");
exit(-1);
}
result = semctl(v1, 0, 16, 0LL);
if ( result < 0 )
{
fwrite("Erro em CriaSemaforo: semctl().\n", 1uLL, 0x20uLL, stderr);
perror("Erro em CriaSemaforo: semctl().");
exit(-1);
}
return result;
}

original_code:
void CriaSemaforo(int key)
{
 int id;
 struct{
  int val;
 }arg;
 if( ( id = semget( key, 1, 0666|01000 ) ) < 0 ) {
  fprintf( stderr, "Erro em CriaSemaforo: semget().\n" );
  perror("Erro em CriaSemaforo: semget().");
  exit( -1 );
 }
 arg.val = 0;
 if( semctl( id, 0, 16,arg ) < 0 ) {
  fprintf( stderr, "Erro em CriaSemaforo: semctl().\n" );
  perror("Erro em CriaSemaforo: semctl().");
  exit( -1 );
 }
 return;
}

canonical_original_code:
void CriaSemaforo(int key)
{
 int id;
 struct { int val; } arg;
 if( ( id = semget( key, 1, 0666|01000 ) ) < 0 ) {
  fprintf( stderr, "Erro em CriaSemaforo: semget().\n" );
  perror("Erro em CriaSemaforo: semget().");
  exit( -1 );
 }
 arg.val = 0;
 if( semctl( id, 0, 16,arg ) < 0 ) {
  fprintf( stderr, "Erro em CriaSemaforo: semctl().\n" );
  perror("Erro em CriaSemaforo: semctl().");
  exit( -1 );
 }
 return;
}

variable_types: key int


DefineSemaforo

decompiled_code:
int __fastcall DefineSemaforo(key_t a1, unsigned int a2)
{
int v2; // eax
int result; // eax

v2 = semget(a1, 1, 950);
if ( v2 < 0 )
{
fwrite("Erro em DefineSemaforo: semget().\n", 1uLL, 0x22uLL, stderr);
perror("Erro em DefineSemaforo: semget().");
exit(-1);
}
result = semctl(v2, 0, 16, a2);
if ( result < 0 )
{
fwrite("Erro em DefineSemaforo: semctl().\n", 1uLL, 0x22uLL, stderr);
perror("Erro em DefineSemaforo: semctl().");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(key_t a1, unsigned int a2)
{
int v2; // eax
int result; // eax

v2 = semget(a1, 1, 950);
if ( v2 < 0 )
{
fwrite("Erro em DefineSemaforo: semget().\n", 1uLL, 0x22uLL, stderr);
perror("Erro em DefineSemaforo: semget().");
exit(-1);
}
result = semctl(v2, 0, 16, a2);
if ( result < 0 )
{
fwrite("Erro em DefineSemaforo: semctl().\n", 1uLL, 0x22uLL, stderr);
perror("Erro em DefineSemaforo: semctl().");
exit(-1);
}
return result;
}

original_code:
void DefineSemaforo(int key, int valor)
{
 int id;
 struct{
  int val;
 }arg;
 if( ( id = semget( key, 1, 0666|01000 ) ) < 0 ) {
  fprintf( stderr, "Erro em DefineSemaforo: semget().\n" );
  perror("Erro em DefineSemaforo: semget().");
  exit( -1 );
 }
 arg.val = valor;
 if( semctl( id, 0, 16,arg ) < 0 ) {
  fprintf( stderr, "Erro em DefineSemaforo: semctl().\n" );
  perror("Erro em DefineSemaforo: semctl().");
  exit( -1 );
 }
 return;
}

canonical_original_code:
void DefineSemaforo(int key, int valor)
{
 int id;
 struct { int val; } arg;
 if( ( id = semget( key, 1, 0666|01000 ) ) < 0 ) {
  fprintf( stderr, "Erro em DefineSemaforo: semget().\n" );
  perror("Erro em DefineSemaforo: semget().");
  exit( -1 );
 }
 arg.val = valor;
 if( semctl( id, 0, 16,arg ) < 0 ) {
  fprintf( stderr, "Erro em DefineSemaforo: semctl().\n" );
  perror("Erro em DefineSemaforo: semctl().");
  exit( -1 );
 }
 return;
}

variable_types: valor int


DevolveSemaforo

decompiled_code:
int __fastcall DevolveSemaforo(key_t a1)
{
int v1; // eax
int result; // eax

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em Devolve Semaforo: semget().\n", 1uLL, 0x24uLL, stderr);
exit(-1);
}
result = semctl(v1, 0, 12);
if ( result < 0 )
{
fwrite("Erro em DevolveSemaforo: semctl().\n", 1uLL, 0x23uLL, stderr);
perror("Erro em DevolveSemaforo: semctl().");
exit(-1);
}
return result;
}

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

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em Devolve Semaforo: semget().\n", 1uLL, 0x24uLL, stderr);
exit(-1);
}
result = semctl(v1, 0, 12);
if ( result < 0 )
{
fwrite("Erro em DevolveSemaforo: semctl().\n", 1uLL, 0x23uLL, stderr);
perror("Erro em DevolveSemaforo: semctl().");
exit(-1);
}
return result;
}

original_code:
int DevolveSemaforo(int key)
{
 int id;
 int val;
 if( ( id = semget( key, 1, 0) ) < 0 ) {
  fprintf( stderr, "Erro em Devolve Semaforo: semget().\n" );
  exit( -1 );
 }
 if( ( val = semctl( id, 0, 12 ) ) < 0 ) {
  fprintf( stderr, "Erro em DevolveSemaforo: semctl().\n" );
  perror("Erro em DevolveSemaforo: semctl().");
  exit( -1 );
 }
 return val;
}

variable_types: id int


AssinalaSemaforo

decompiled_code:
int __fastcall AssinalaSemaforo(key_t a1)
{
int v1; // eax
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em AssinalaSemaforo: semget().\n", 1uLL, 0x24uLL, stderr);
exit(-1);
}
sops.sem_num = 0;
sops.sem_op = 1;
sops.sem_flg = 0;
result = semop(v1, &sops, 1uLL);
if ( result < 0 )
{
fwrite("Erro em AssinalaSemaforo: semop().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

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

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em AssinalaSemaforo: semget().\n", 1uLL, 0x24uLL, stderr);
exit(-1);
}
sops.sem_num = 0;
sops.sem_op = 1;
sops.sem_flg = 0;
result = semop(v1, &sops, 1uLL);
if ( result < 0 )
{
fwrite("Erro em AssinalaSemaforo: semop().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void AssinalaSemaforo(int key)
{
 struct sembuf op;
 int id;
 if( ( id = semget( key, 1, 0) ) < 0 ) {
  fprintf( stderr, "Erro em AssinalaSemaforo: semget().\n" );
  exit( -1 );
 }
 op.sem_num = 0;
 op.sem_op = +1;
 op.sem_flg = 0;
 if( semop( id, &op, 1 ) < 0 ) {
  fprintf( stderr, "Erro em AssinalaSemaforo: semop().\n" );
  exit( -1 );
 }
 return;
}

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


EsperaSemaforo

decompiled_code:
int __fastcall EsperaSemaforo(key_t a1)
{
int v1; // eax
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em EsperaSemaforo: semget().\n", 1uLL, 0x22uLL, stderr);
exit(-1);
}
sops.sem_num = 0;
sops.sem_op = -1;
sops.sem_flg = 0;
result = semop(v1, &sops, 1uLL);
if ( result < 0 )
{
fwrite("Erro em EsperaSemaforo: semop().\n", 1uLL, 0x21uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(key_t a1)
{
int v1; // eax
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em EsperaSemaforo: semget().\n", 1uLL, 0x22uLL, stderr);
exit(-1);
}
sops.sem_num = 0;
sops.sem_op = -1;
sops.sem_flg = 0;
result = semop(v1, &sops, 1uLL);
if ( result < 0 )
{
fwrite("Erro em EsperaSemaforo: semop().\n", 1uLL, 0x21uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void EsperaSemaforo(int key)
{
 struct sembuf op;
 int id;
 if( ( id = semget( key, 1, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforo: semget().\n" );
  exit( -1 );
 }
 op.sem_num = 0;
 op.sem_op = -1;
 op.sem_flg = 0;
 if( semop( id, &op, 1 ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforo: semop().\n" );
  exit( -1 );
 }
 return;
}


EsperaSemaforoZero

decompiled_code:
int __fastcall EsperaSemaforoZero(key_t a1)
{
int v1; // eax
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em EsperaSemaforoZero: semget().\n", 1uLL, 0x26uLL, stderr);
exit(-1);
}
sops.sem_num = 0;
sops.sem_op = 0;
sops.sem_flg = 0;
result = semop(v1, &sops, 1uLL);
if ( result < 0 )
{
fwrite("Erro em EsperaSemaforoZero: semop().\n", 1uLL, 0x25uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(key_t a1)
{
int v1; // eax
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em EsperaSemaforoZero: semget().\n", 1uLL, 0x26uLL, stderr);
exit(-1);
}
sops.sem_num = 0;
sops.sem_op = 0;
sops.sem_flg = 0;
result = semop(v1, &sops, 1uLL);
if ( result < 0 )
{
fwrite("Erro em EsperaSemaforoZero: semop().\n", 1uLL, 0x25uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void EsperaSemaforoZero(int key)
{
 struct sembuf op;
 int id;
 if( ( id = semget( key, 1, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforoZero: semget().\n" );
  exit( -1 );
 }
 op.sem_num = 0;
 op.sem_op = 0;
 op.sem_flg = 0;
 if( semop( id, &op, 1 ) < 0 ) {
  fprintf( stderr, "Erro em EsperaSemaforoZero: semop().\n" );
  exit( -1 );
 }
 return;
}


DestroiSemaforo

decompiled_code:
int __fastcall DestroiSemaforo(key_t a1)
{
int v1; // eax
int result; // eax

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em DestroiSemaforo: semget().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
result = semctl(v1, 0, 0);
if ( result < 0 )
{
fwrite("Erro em DestroiSemaforo: semctl().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(key_t a1)
{
int v1; // eax
int result; // eax

v1 = semget(a1, 1, 0);
if ( v1 < 0 )
{
fwrite("Erro em DestroiSemaforo: semget().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
result = semctl(v1, 0, 0);
if ( result < 0 )
{
fwrite("Erro em DestroiSemaforo: semctl().\n", 1uLL, 0x23uLL, stderr);
exit(-1);
}
return result;
}

original_code:
void DestroiSemaforo(int key)
{
 int id;
 if( ( id = semget( key, 1, 0 ) ) < 0 ) {
  fprintf( stderr, "Erro em DestroiSemaforo: semget().\n" );
  exit( -1 );
 }
 if( semctl( id, 0, 0) < 0 ) {
  fprintf( stderr, "Erro em DestroiSemaforo: semctl().\n" );
  exit( -1 );
 }
 return;
}


traduzTipo

decompiled_code:
__int64 __fastcall traduzTipo(const char *a1)
{
__int64 result; // rax
bool v2; // zf

result = 2LL;
if ( strcmp(a1, "from") )
{
if ( strcmp(a1, "FROM") )
{
if ( strcmp(a1, "inpt") )
{
if ( strcmp(a1, "INPT") )
{
if ( strcmp(a1, "buff") )
{
if ( strcmp(a1, "BUFF") )
{
result = 3LL;
if ( strcmp(a1, "not") )
{
if ( strcmp(a1, "NOT") )
{
v2 = strcmp(a1, "and") == 0;
result = !v2;
if ( !v2 )
{
v2 = strcmp(a1, "AND") == 0;
result = !v2;
if ( !v2 )
{
result = 2LL;
if ( strcmp(a1, "or") )
{
if ( strcmp(a1, "OR") )
{
result = 4LL;
if ( strcmp(a1, "xor") )
{
if ( strcmp(a1, "XOR") )
{
result = 1LL;
if ( strcmp(a1, "nand") )
{
if ( strcmp(a1, "NAND") )
{
result = 3LL;
if ( strcmp(a1, "nor") )
{
if ( strcmp(a1, "NOR") )
{
result = 5LL;
if ( strcmp(a1, "xnor") )
{
if ( !strcmp(a1, "XNOR") )
{
result = 5LL;
}
else
{
fprintf(stderr, "Tipo desconhecido: %s -> ficheiro.cir invalido.\n", a1);
result = 0xFFFFFFFFLL;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
__int64 result; // rax
bool v2; // zf

result = 2LL;
if ( strcmp(a1, "from") )
{
if ( strcmp(a1, "FROM") )
{
if ( strcmp(a1, "inpt") )
{
if ( strcmp(a1, "INPT") )
{
if ( strcmp(a1, "buff") )
{
if ( strcmp(a1, "BUFF") )
{
result = 3LL;
if ( strcmp(a1, "not") )
{
if ( strcmp(a1, "NOT") )
{
v2 = strcmp(a1, "and") == 0;
result = !v2;
if ( !v2 )
{
v2 = strcmp(a1, "AND") == 0;
result = !v2;
if ( !v2 )
{
result = 2LL;
if ( strcmp(a1, "or") )
{
if ( strcmp(a1, "OR") )
{
result = 4LL;
if ( strcmp(a1, "xor") )
{
if ( strcmp(a1, "XOR") )
{
result = 1LL;
if ( strcmp(a1, "nand") )
{
if ( strcmp(a1, "NAND") )
{
result = 3LL;
if ( strcmp(a1, "nor") )
{
if ( strcmp(a1, "NOR") )
{
result = 5LL;
if ( strcmp(a1, "xnor") )
{
if ( !strcmp(a1, "XNOR") )
{
result = 5LL;
}
else
{
fprintf(stderr, "Tipo desconhecido: %s -> ficheiro.cir invalido.\n", a1);
result = 0xFFFFFFFFLL;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}

original_code:
int traduzTipo(char* tipo)
{
  if ( ( strcmp( tipo, "from") == 0 ) || ( strcmp( tipo, "FROM") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "inpt") == 0 ) || ( strcmp( tipo, "INPT") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "buff") == 0 ) || ( strcmp( tipo, "BUFF") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "not") == 0 ) || ( strcmp( tipo, "NOT") == 0 ) )
    return 3;
  else if ( ( strcmp( tipo, "and") == 0 ) || ( strcmp( tipo, "AND") == 0 ) )
    return 0;
  else if ( ( strcmp( tipo, "or") == 0 ) || ( strcmp( tipo, "OR") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "xor") == 0 ) || ( strcmp( tipo, "XOR") == 0) )
    return 4;
  else if ( ( strcmp( tipo, "nand") == 0 ) || ( strcmp( tipo, "NAND") == 0 ) )
    return 1;
  else if ( ( strcmp( tipo, "nor") == 0 ) || ( strcmp( tipo, "NOR") == 0 ) )
    return 3;
  else if ( ( strcmp( tipo, "xnor") == 0 ) || ( strcmp( tipo, "XNOR") == 0 ) )
    return 5;
  else
    {
      fprintf(stderr,"Tipo desconhecido: %s -> ficheiro.cir invalido.\n", tipo );
      return -1;
    }
}

canonical_original_code:
int traduzTipo(char *tipo)
{
  if ( ( strcmp( tipo, "from") == 0 ) || ( strcmp( tipo, "FROM") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "inpt") == 0 ) || ( strcmp( tipo, "INPT") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "buff") == 0 ) || ( strcmp( tipo, "BUFF") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "not") == 0 ) || ( strcmp( tipo, "NOT") == 0 ) )
    return 3;
  else if ( ( strcmp( tipo, "and") == 0 ) || ( strcmp( tipo, "AND") == 0 ) )
    return 0;
  else if ( ( strcmp( tipo, "or") == 0 ) || ( strcmp( tipo, "OR") == 0 ) )
    return 2;
  else if ( ( strcmp( tipo, "xor") == 0 ) || ( strcmp( tipo, "XOR") == 0) )
    return 4;
  else if ( ( strcmp( tipo, "nand") == 0 ) || ( strcmp( tipo, "NAND") == 0 ) )
    return 1;
  else if ( ( strcmp( tipo, "nor") == 0 ) || ( strcmp( tipo, "NOR") == 0 ) )
    return 3;
  else if ( ( strcmp( tipo, "xnor") == 0 ) || ( strcmp( tipo, "XNOR") == 0 ) )
    return 5;
  else
    {
      fprintf(stderr,"Tipo desconhecido: %s -> ficheiro.cir invalido.\n", tipo );
      return -1;
    }
}

variable_types:
tipo
char *


indiceDoNome

decompiled_code:
__int64 __fastcall indiceDoNome(__int64 a1, const char *a2)
{
int v2; // ebx
const char *i; // rbp

v2 = *(_DWORD *)a1;
if ( *(int *)a1 < 0 )
{
v2 = 0;
}
else
{
for ( i = (const char *)(*(_QWORD *)(a1 + 8) + 72LL * v2); strcmp(i, a2); i -= 72 )
{
if ( --v2 == -1 )
return 0;
}
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const char *a2)
{
int v2; // ebx
const char *i; // rbp

v2 = *(_DWORD *)a1;
if ( *(int *)a1 < 0 )
{
v2 = 0;
}
else
{
for ( i = (const char *)(*(_QWORD *)(a1 + 8) + 72LL * v2); strcmp(i, a2); i -= 72 )
{
if ( --v2 == -1 )
return 0;
}
}
return (unsigned int)v2;
}

original_code:
int indiceDoNome(Circuito* circuito, char* nome)
{
  int i;
  for( i = circuito->nNodes; i >= 0 ; i-- )
    if( 0 == strcmp( circuito->vectorNodes[ i ].nome, nome ) )
      return i;
  return 0;
}

canonical_original_code:
int indiceDoNome(struct CircuitoStr *circuito, char *nome)
{
  int i;
  for( i = circuito->nNodes; i >= 0 ; i-- )
    if( 0 == strcmp( circuito->vectorNodes[ i ].nome, nome ) )
      return i;
  return 0;
}

variable_types: struct CircuitoStr { int nNodes; struct NodeStr { char nome[16]; int tipo; int fanin; int *inputs; int valor; char flag; int fanout; char *sa0; char *sa1; int leituras; } *vectorNodes; int nMax; int nInputs; int realNodes; } *

variable_types:
nome
char *

variable_types: i int


Abastece

decompiled_code:
__int64 __fastcall Abastece(char *src, char *a2)
{
char v3[20]; // [rsp+0h] [rbp-38h] BYREF
char dest[36]; // [rsp+14h] [rbp-24h] BYREF

GetIndiceStock();
strcpy(v3, src);
strcpy(dest, a2);
return AssinalaStock(v3, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src, char *a2)
{
char v3[20]; // [rsp+0h] [rbp-38h] BYREF
char dest[36]; // [rsp+14h] [rbp-24h] BYREF

GetIndiceStock();
strcpy(v3, src);
strcpy(dest, a2);
return AssinalaStock(v3, 1LL);
}

original_code:
void Abastece(char* produto, char* quantidade)
{
 int indice;
 int n_prod_antes;
 Produto prod;
 n_prod_antes = stock.n_prod;
 indice = GetIndiceStock( produto );
 strcpy( prod.nome , produto );
 strcpy( prod.quantidade , quantidade );
 if( stock.n_prod == stock.n_prod ){
   AssinalaStock( &prod, 1 );
 }else{
   AdicionaProdMemMap( produto, quantidade);
 }
 return;
}

canonical_original_code:
void Abastece(char *produto, char *quantidade)
{
 int indice;
 int n_prod_antes;
 struct ProdutoStr prod;
 n_prod_antes = stock.n_prod;
 indice = GetIndiceStock( produto );
 strcpy( prod.nome , produto );
 strcpy( prod.quantidade , quantidade );
 if( stock.n_prod == stock.n_prod ){
   AssinalaStock( &prod, 1 );
 }else{
   AdicionaProdMemMap( produto, quantidade);
 }
 return;
}

variable_types:
produto
char *

variable_types:
quantidade
char *

variable_types: indice int

variable_types: struct ProdutoStr { char nome[20]; char quantidade[5]; }


ListaPedidosPendentes

decompiled_code:
void ListaPedidosPendentes()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void ListaPedidosPendentes(int verb)
{
 return;
}

variable_types: verb int


ListaStock

decompiled_code:
int ListaStock()
{
__int64 v0; // rbp
int v1; // ebx
int result; // eax

if ( *((int *)&stock + 8) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
result = printf(
"Produto: %s - %s unidades\n",
(const char *)(*((_QWORD *)&stock + 1) + v0),
(const char *)(*((_QWORD *)&stock + 1) + v0 + 20));
++v1;
v0 += 25LL;
}
while ( *((_DWORD *)&stock + 8) > v1 );
}
return result;
}

canonical_decompiled_code:
int func2()
{
__int64 v0; // rbp
int v1; // ebx
int result; // eax

if ( *((int *)&stock + 8) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
result = printf(
"Produto: %s - %s unidades\n",
(const char *)(*((_QWORD *)&stock + 1) + v0),
(const char *)(*((_QWORD *)&stock + 1) + v0 + 20));
++v1;
v0 += 25LL;
}
while ( *((_DWORD *)&stock + 8) > v1 );
}
return result;
}

original_code:
void ListaStock(int verb)
{
  int i;
  for( i = 0 ; i < stock.n_prod ; i++ )
    {
      printf("Produto: %s - %s unidades\n",
      stock.inicio[i].nome,
      stock.inicio[i].quantidade );
    }
  return;
}


TerminaRefeitorio

decompiled_code:
void __noreturn TerminaRefeitorio()
{
__int64 v0; // rbx
int v1; // er12
int stat_loc[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( *((int *)&clientes + 2) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
kill(*(_DWORD *)(clientes + v0), 2);
waitpid(*(_DWORD *)(clientes + v0), stat_loc, 0);
pthread_cancel(*(_QWORD *)(clientes + v0 + 16));
pthread_join(*(_QWORD *)(clientes + v0 + 16), 0LL);
free(*(void **)(clientes + v0 + 8));
++v1;
v0 += 24LL;
}
while ( *((_DWORD *)&clientes + 2) > v1 );
}
pthread_cancel(ThreadAtendeSoldados);
pthread_cancel(ThreadAtendeOficiais);
pthread_cancel(ThreadAtendeGenerais);
pthread_join(ThreadAtendeSoldados, 0LL);
pthread_join(ThreadAtendeOficiais, 0LL);
pthread_join(ThreadAtendeGenerais, 0LL);
DesMapeiaFicheiroStock();
DestroiSemaforo(1LL);
DestroiSemaforo(2LL);
DestroiSemaforo(3LL);
DestroiSemaforo(4LL);
DestroiSemaforo(5LL);
DestroiSemaforo(6LL);
DestroiSemaforo(7LL);
DestroiSemaforo(8LL);
DestroiSemaforo(9LL);
DestroiSemaforo(10LL);
DestroiSemaforo(11LL);
DestroiSemaforo(12LL);
DestroiSemaforo(13LL);
DestroiSemaforo(14LL);
DestroiSemaforo(15LL);
DestroiSemaforo(5LL);
DestroiMailbox(1LL);
exit(0);
}

canonical_decompiled_code:
void __noreturn func3()
{
__int64 v0; // rbx
int v1; // er12
int stat_loc[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( *((int *)&clientes + 2) > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
kill(*(_DWORD *)(clientes + v0), 2);
waitpid(*(_DWORD *)(clientes + v0), stat_loc, 0);
pthread_cancel(*(_QWORD *)(clientes + v0 + 16));
pthread_join(*(_QWORD *)(clientes + v0 + 16), 0LL);
free(*(void **)(clientes + v0 + 8));
++v1;
v0 += 24LL;
}
while ( *((_DWORD *)&clientes + 2) > v1 );
}
pthread_cancel(ThreadAtendeSoldados);
pthread_cancel(ThreadAtendeOficiais);
pthread_cancel(ThreadAtendeGenerais);
pthread_join(ThreadAtendeSoldados, 0LL);
pthread_join(ThreadAtendeOficiais, 0LL);
pthread_join(ThreadAtendeGenerais, 0LL);
DesMapeiaFicheiroStock();
DestroiSemaforo(1LL);
DestroiSemaforo(2LL);
DestroiSemaforo(3LL);
DestroiSemaforo(4LL);
DestroiSemaforo(5LL);
DestroiSemaforo(6LL);
DestroiSemaforo(7LL);
DestroiSemaforo(8LL);
DestroiSemaforo(9LL);
DestroiSemaforo(10LL);
DestroiSemaforo(11LL);
DestroiSemaforo(12LL);
DestroiSemaforo(13LL);
DestroiSemaforo(14LL);
DestroiSemaforo(15LL);
DestroiSemaforo(5LL);
DestroiMailbox(1LL);
exit(0);
}

original_code:
void TerminaRefeitorio(int arg)
{
  int i, estado;
  for( i = 0 ; i < clientes.n_cli ; i++ )
    {
      kill( clientes.vector[i].id, 2 );
      waitpid( clientes.vector[i].id, &estado,0 );
      pthread_cancel( clientes.vector[i].tid );
      pthread_join( clientes.vector[i].tid, ((void *)0) );
      free( clientes.vector[i].pedido );
    }
  pthread_cancel( ThreadAtendeSoldados );
  pthread_cancel( ThreadAtendeOficiais );
  pthread_cancel( ThreadAtendeGenerais );
  pthread_join( ThreadAtendeSoldados, ((void *)0) );
  pthread_join( ThreadAtendeOficiais, ((void *)0) );
  pthread_join( ThreadAtendeGenerais, ((void *)0) );
  DesMapeiaFicheiroStock();
  DestroiSemaforo( 1 );
  DestroiSemaforo( 2 );
  DestroiSemaforo( 3 );
  DestroiSemaforo( 4 );
  DestroiSemaforo( 5 );
  DestroiSemaforo( 6 );
  DestroiSemaforo( 7 );
  DestroiSemaforo( 8 );
  DestroiSemaforo( 9 );
  DestroiSemaforo( 10 );
  DestroiSemaforo( 11 );
  DestroiSemaforo( 12 );
  DestroiSemaforo( 13 );
  DestroiSemaforo( 14 );
  DestroiSemaforo( 15 );
  DestroiSemaforo( 5 );
  DestroiMailbox( 1 );
  exit( 0 );
}

canonical_original_code:
void TerminaRefeitorio(int arg)
{
  int i; int estado;
  for( i = 0 ; i < clientes.n_cli ; i++ )
    {
      kill( clientes.vector[i].id, 2 );
      waitpid( clientes.vector[i].id, &estado,0 );
      pthread_cancel( clientes.vector[i].tid );
      pthread_join( clientes.vector[i].tid, ((void *)0) );
      free( clientes.vector[i].pedido );
    }
  pthread_cancel( ThreadAtendeSoldados );
  pthread_cancel( ThreadAtendeOficiais );
  pthread_cancel( ThreadAtendeGenerais );
  pthread_join( ThreadAtendeSoldados, ((void *)0) );
  pthread_join( ThreadAtendeOficiais, ((void *)0) );
  pthread_join( ThreadAtendeGenerais, ((void *)0) );
  DesMapeiaFicheiroStock();
  DestroiSemaforo( 1 );
  DestroiSemaforo( 2 );
  DestroiSemaforo( 3 );
  DestroiSemaforo( 4 );
  DestroiSemaforo( 5 );
  DestroiSemaforo( 6 );
  DestroiSemaforo( 7 );
  DestroiSemaforo( 8 );
  DestroiSemaforo( 9 );
  DestroiSemaforo( 10 );
  DestroiSemaforo( 11 );
  DestroiSemaforo( 12 );
  DestroiSemaforo( 13 );
  DestroiSemaforo( 14 );
  DestroiSemaforo( 15 );
  DestroiSemaforo( 5 );
  DestroiMailbox( 1 );
  exit( 0 );
}

variable_types: arg int

variable_types: estado int


decompiled_code:
void __noreturn Menu_Ref()
{
char v0; // al
char v1[27]; // [rsp+0h] [rbp-28h] BYREF
char v2[13]; // [rsp+1Bh] [rbp-Dh] BYREF

while ( 1 )
{
Escreve_Menu_Ref();
v0 = getchar();
if ( v0 == 113 )
{
puts("TerminaRefeitorio();");
TerminaRefeitorio(0LL);
}
if ( v0 > 113 )
{
if ( v0 != 115 )
goto LABEL_16;
if ( (unsigned __int8)getchar() == 118 )
puts("ListaStock(1);");
else
puts("ListaStock(0);");
ListaStock();
}
else if ( v0 == 97 )
{
_isoc99_scanf("%s %s", v1, v2);
printf("Abastece(%s,%s);\n", v1, v2);
Abastece(v1, v2);
}
else if ( v0 == 112 )
{
if ( (unsigned __int8)getchar() == 118 )
puts("ListaPedidosPendentesVerboso();");
else
puts("ListaPedidosPendentes();");
}
else
{
LABEL_16:
puts("opcao invalida");
}
}
}

canonical_decompiled_code:
void __noreturn func5()
{
char v0; // al
char v1[27]; // [rsp+0h] [rbp-28h] BYREF
char v2[13]; // [rsp+1Bh] [rbp-Dh] BYREF

while ( 1 )
{
func4();
v0 = getchar();
if ( v0 == 113 )
{
puts("TerminaRefeitorio();");
func3(0LL);
}
if ( v0 > 113 )
{
if ( v0 != 115 )
goto LABEL_16;
if ( (unsigned __int8)getchar() == 118 )
puts("ListaStock(1);");
else
puts("ListaStock(0);");
func2();
}
else if ( v0 == 97 )
{
_isoc99_scanf("%s %s", v1, v2);
printf("Abastece(%s,%s);\n", v1, v2);
func0(v1, v2);
}
else if ( v0 == 112 )
{
if ( (unsigned __int8)getchar() == 118 )
puts("ListaPedidosPendentesVerboso();");
else
puts("ListaPedidosPendentes();");
}
else
{
LABEL_16:
puts("opcao invalida");
}
}
}

original_code:
void *Menu_Ref(void *argumento)
{
  char opcao;
  char quantidade[5];
  char produto[20];
  while(1)
    {
      Escreve_Menu_Ref();
      opcao = getchar();
      switch( opcao )
 {
 case 'a':
   scanf("%s %s", produto, quantidade);
   printf("Abastece(%s,%s);\n", produto, quantidade);
   Abastece(produto, quantidade);
   break;
 case 'p':
   opcao = getchar();
   if('v' == opcao){
     puts("ListaPedidosPendentesVerboso();");
     ListaPedidosPendentes( 1 );
   }
   else
     {
       puts("ListaPedidosPendentes();");
       ListaPedidosPendentes( 0 );
     }
   break;
 case 's':
   opcao = getchar();
   if('v' == opcao){
     puts("ListaStock(1);");
     ListaStock(1);
   }
   else
     {
       puts("ListaStock(0);");
       ListaStock(0);
     }
   break;
 case 'q':
   puts("TerminaRefeitorio();");
   TerminaRefeitorio(0);
   return 0;
   break;
 default:
   puts("opcao invalida");
   break;
 }
    }
  return 0;
}

variable_types:
argumento
void *

variable_types: opcao char

variable_types:
quantidade
char [5]

variable_types:
produto
char [20]


AtendeSoldados

decompiled_code:
void __noreturn AtendeSoldados()
{
__int64 v0; // rbx
void *v1; // rax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

MenusFicheiroParaMemoriaPartilhada("menus.txt");
DefineSemaforo(9LL, 0LL);
DefineSemaforo(8LL, 1LL);
v0 = CriaSocketStreamEscuta("/tmp/ssserv");
while ( 1 )
{
v1 = (void *)AceitaLigacaoSocketStream(v0);
pthread_create(newthread, 0LL, ThreadSoldado, v1);
}
}

canonical_decompiled_code:
void __noreturn func1()
{
__int64 v0; // rbx
void *v1; // rax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

func0("menus.txt");
DefineSemaforo(9LL, 0LL);
DefineSemaforo(8LL, 1LL);
v0 = CriaSocketStreamEscuta("/tmp/ssserv");
while ( 1 )
{
v1 = (void *)AceitaLigacaoSocketStream(v0);
pthread_create(newthread, 0LL, ThreadSoldado, v1);
}
}

original_code:
void *AtendeSoldados(void * parametros)
{
 pthread_t TidSoldados;
 SocketStream* SocketStreamRef;
 SocketStream* SocketStreamSol;
 MenusFicheiroParaMemoriaPartilhada( "menus.txt", 321 );
 DefineSemaforo( 9, 0 );
 DefineSemaforo( 8, 1 );
 SocketStreamRef = CriaSocketStreamEscuta( "/tmp/ssserv" );
 while(1){
  SocketStreamSol = AceitaLigacaoSocketStream( SocketStreamRef );
  pthread_create(&TidSoldados ,((void *)0), ThreadSoldado, (void*)SocketStreamSol );
 }
 return 0;
}

canonical_original_code:
void *AtendeSoldados(void *parametros)
{
 unsigned long TidSoldados;
 struct SocketStreamStr *SocketStreamRef;
 struct SocketStreamStr *SocketStreamSol;
 MenusFicheiroParaMemoriaPartilhada( "menus.txt", 321 );
 DefineSemaforo( 9, 0 );
 DefineSemaforo( 8, 1 );
 SocketStreamRef = CriaSocketStreamEscuta( "/tmp/ssserv" );
 while(1){
  SocketStreamSol = AceitaLigacaoSocketStream( SocketStreamRef );
  pthread_create(&TidSoldados ,((void *)0), ThreadSoldado, (void *)SocketStreamSol );
 }
 return 0;
}

variable_types:
parametros
void *

variable_types:
TidSoldados
unsigned long

variable_types: struct SocketStreamStr { int fd; char *nome; struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } my_addr; unsigned int addrlen; } *


RetiraPedido

decompiled_code:
__int64 __fastcall RetiraPedido(int a1)
{
__int64 v1; // rbp
int v2; // eax
int v3; // edx
_DWORD *v4; // rax

v1 = AcedeMemoriaPedidos();
v2 = DevolveSemaforo(3LL);
if ( v2 <= 0 )
return 0xFFFFFFFFLL;
v3 = v2;
v4 = (_DWORD *)v1;
while ( *v4 != a1 )
{
v4 += 2;
if ( v4 == (_DWORD *)(v1 + 8LL * (unsigned int)(v3 - 1) + 8) )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4[1];
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 v1; // rbp
int v2; // eax
int v3; // edx
_DWORD *v4; // rax

v1 = AcedeMemoriaPedidos();
v2 = DevolveSemaforo(3LL);
if ( v2 <= 0 )
return 0xFFFFFFFFLL;
v3 = v2;
v4 = (_DWORD *)v1;
while ( *v4 != a1 )
{
v4 += 2;
if ( v4 == (_DWORD *)(v1 + 8LL * (unsigned int)(v3 - 1) + 8) )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4[1];
}

original_code:
int RetiraPedido(int pid)
{
  int i;
  Escolha* pedidos;
  int n_ped;
  pedidos = AcedeMemoriaPedidos();
  n_ped = DevolveSemaforo( 3 );
  for( i = 0 ; i < n_ped ; i++ )
    {
      if( pid == pedidos[i].pid )
 {
   return pedidos[i].menu;
 }
    }
  return -1;
}

canonical_original_code:
int RetiraPedido(int pid)
{
  int i;
  struct EscolhaStr *pedidos;
  int n_ped;
  pedidos = AcedeMemoriaPedidos();
  n_ped = DevolveSemaforo( 3 );
  for( i = 0 ; i < n_ped ; i++ )
    {
      if( pid == pedidos[i].pid )
 {
   return pedidos[i].menu;
 }
    }
  return -1;
}

variable_types: pid int

variable_types: struct EscolhaStr { int pid; int menu; } *


CriaResposta

decompiled_code:
char *__fastcall CriaResposta(int a1)
{
__int64 v1; // r12
char *v2; // rax

v1 = AcedeMemoriaMenus();
v2 = (char *)malloc(0x28uLL);
return strncpy(v2, (const char *)(v1 + 40 * a1), 0x28uLL);
}

canonical_decompiled_code:
char *__fastcall func3(int a1)
{
__int64 v1; // r12
char *v2; // rax

v1 = AcedeMemoriaMenus();
v2 = (char *)malloc(0x28uLL);
return strncpy(v2, (const char *)(v1 + 40 * a1), 0x28uLL);
}

original_code:
char* CriaResposta(int menu)
{
  char *comida;
  Menus menus;
  menus = AcedeMemoriaMenus();
  comida = (char*) malloc ( sizeof(char)*40 );
  strncpy( comida, &menus.texto[ menu*40 ],40 );
  return comida;
}

canonical_original_code:
char *CriaResposta(int menu)
{
  char *comida;
  struct MenusStr menus;
  menus = AcedeMemoriaMenus();
  comida = (char *) malloc ( sizeof(char)*40 );
  strncpy( comida, &menus.texto[ menu*40 ],40 );
  return comida;
}

variable_types: menu int

variable_types:
comida
char *

variable_types: struct MenusStr { char *texto; int linhas; int colunas; }