repo: dmazzoni/ElaboratoIPC

decompiled_code:
key_t __fastcall generate_key(int proj_id)
{
key_t result; // eax

result = ftok("main.c", proj_id);
if ( result == -1 )
{
write_to_fd(2LL, "ftok failed\n");
exit(1);
}
return result;
}

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

result = ftok("main.c", proj_id);
if ( result == -1 )
{
write_to_fd(2LL, "ftok failed\n");
exit(1);
}
return result;
}

original_code:
static int generate_key(int seed) {
 int key = ftok("main.c", seed);
 if (key == -1) {
  write_to_fd(2, "ftok failed\n");
  exit(1);
 }
 return key;
}

variable_types: seed int

variable_types: key int


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

if ( semctl(ipc_id[0], 0, 0, 0LL) == -1 )
write_with_int(2LL, "Failed to close semaphore set with ID ", (unsigned int)ipc_id[0]);
if ( shmctl(ipc_id[1], 0, 0LL) == -1 )
write_with_int(2LL, "Failed to close shared memory with ID ", (unsigned int)ipc_id[1]);
result = shmctl(ipc_id[2], 0, 0LL);
if ( result == -1 )
result = write_with_int(2LL, "Failed to close shared memory with ID ", (unsigned int)ipc_id[2]);
return result;
}

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

if ( semctl(ipc_id[0], 0, 0, 0LL) == -1 )
write_with_int(2LL, "Failed to close semaphore set with ID ", (unsigned int)ipc_id[0]);
if ( shmctl(ipc_id[1], 0, 0LL) == -1 )
write_with_int(2LL, "Failed to close shared memory with ID ", (unsigned int)ipc_id[1]);
result = shmctl(ipc_id[2], 0, 0LL);
if ( result == -1 )
result = write_with_int(2LL, "Failed to close shared memory with ID ", (unsigned int)ipc_id[2]);
return result;
}

original_code:
void close_ipc(void) {
 int i;
 if(semctl(ipc_id[0], 0, 0, ((void *)0)) == -1)
  write_with_int(2, "Failed to close semaphore set with ID ", ipc_id[0]);
 for(i = 1; i < 3; ++i)
  if(shmctl(ipc_id[i], 0, ((void *)0)) == -1)
   write_with_int(2, "Failed to close shared memory with ID ", ipc_id[i]);
 return;
}

canonical_original_code:
void close_ipc() {
 int i;
 if(semctl(ipc_id[0], 0, 0, ((void *)0)) == -1)
  write_with_int(2, "Failed to close semaphore set with ID ", ipc_id[0]);
 for(i = 1; i < 3; ++i)
  if(shmctl(ipc_id[i], 0, ((void *)0)) == -1)
   write_with_int(2, "Failed to close shared memory with ID ", ipc_id[i]);
 return;
}

variable_types: i int

return_type: void


decompiled_code:
int __fastcall shm_detach(const void *a1)
{
int result; // eax

result = shmdt(a1);
if ( result == -1 )
result = write_to_fd(2LL, "Failed to detach shared memory segment\n");
return result;
}

canonical_decompiled_code:
int __fastcall func2(const void *a1)
{
int result; // eax

result = shmdt(a1);
if ( result == -1 )
result = write_to_fd(2LL, "Failed to detach shared memory segment\n");
return result;
}

original_code:
void shm_detach(void *address) {
 if (shmdt(address) == -1)
  write_to_fd(2, "Failed to detach shared memory segment\n");
}

variable_types:
address
void *


decompiled_code:
int __fastcall kill_group(int sig)
{
int result; // eax

result = kill(0, sig);
if ( result == -1 )
result = write_with_int(2LL, "Failed to send signal ", (unsigned int)sig);
return result;
}

canonical_decompiled_code:
int __fastcall func3(int sig)
{
int result; // eax

result = kill(0, sig);
if ( result == -1 )
result = write_with_int(2LL, "Failed to send signal ", (unsigned int)sig);
return result;
}

original_code:
void kill_group(int signum) {
 if (kill(0, signum) == -1)
  write_with_int(2, "Failed to send signal ", signum);
}

variable_types: signum int


decompiled_code:
int __fastcall init_ipc(int nsems, int a2, int a3, int a4)
{
key_t v6; // eax
key_t v7; // eax
int v8; // eax
key_t v9; // eax
int result; // eax

v6 = generate_key(1);
ipc_id[0] = semget(v6, nsems, a4);
if ( ipc_id[0] == -1 )
{
write_to_fd(2LL, "Failed to create semaphore set\n");
kill_group(15);
}
v7 = generate_key(2);
v8 = shmget(v7, a2, a4);
ipc_id[1] = v8;
if ( v8 == -1 )
{
write_to_fd(2LL, "Failed to create shared memory for operations\n");
kill_group(15);
}
v9 = generate_key(3);
result = shmget(v9, a3, a4);
ipc_id[2] = result;
if ( result == -1 )
{
write_to_fd(2LL, "Failed to create shared memory for processor states\n");
result = kill_group(15);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int nsems, int a2, int a3, int a4)
{
key_t v6; // eax
key_t v7; // eax
int v8; // eax
key_t v9; // eax
int result; // eax

v6 = func0(1);
ipc_id[0] = semget(v6, nsems, a4);
if ( ipc_id[0] == -1 )
{
write_to_fd(2LL, "Failed to create semaphore set\n");
func3(15);
}
v7 = func0(2);
v8 = shmget(v7, a2, a4);
ipc_id[1] = v8;
if ( v8 == -1 )
{
write_to_fd(2LL, "Failed to create shared memory for operations\n");
func3(15);
}
v9 = func0(3);
result = shmget(v9, a3, a4);
ipc_id[2] = result;
if ( result == -1 )
{
write_to_fd(2LL, "Failed to create shared memory for processor states\n");
result = func3(15);
}
return result;
}

original_code:
void init_ipc(int nsems, int shm1_size, int shm2_size, int flags) {
 ipc_id[0] = semget(generate_key(1), nsems, flags);
 if(ipc_id[0] == -1) {
  write_to_fd(2, "Failed to create semaphore set\n");
  kill_group(15);
 }
 ipc_id[1] = shmget(generate_key(2), shm1_size, flags);
 if(ipc_id[1] == -1) {
  write_to_fd(2, "Failed to create shared memory for operations\n");
  kill_group(15);
 }
 ipc_id[2] = shmget(generate_key(3), shm2_size, flags);
 if(ipc_id[2] == -1) {
  write_to_fd(2, "Failed to create shared memory for processor states\n");
  kill_group(15);
 }
}

variable_types: nsems int

variable_types: flags int


decompiled_code:
int __fastcall init_sems(int a1)
{
void *v1; // rsp
__int64 *v2; // rax
int result; // eax
__int64 savedregs; // [rsp+0h] [rbp+0h] BYREF

v1 = alloca(2LL * (2 * a1 + 2));
if ( a1 > 0 )
{
v2 = &savedregs;
do
{
*(_WORD *)v2 = 1;
*((_WORD *)v2 + 1) = 0;
v2 = (__int64 *)((char *)v2 + 4);
}
while ( v2 != (__int64 *)((char *)&savedregs + 4 * (unsigned int)a1) );
}
*((_WORD *)&savedregs + 2 * a1) = 1;
*((_WORD *)&savedregs + 2 * a1 + 1) = a1;
result = semctl(ipc_id[0], -1, 17, &savedregs);
if ( result == -1 )
{
write_to_fd(2LL, "Failed to initialize semaphore set\n");
result = kill_group(15);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1)
{
void *v1; // rsp
__int64 *v2; // rax
int result; // eax
__int64 savedregs; // [rsp+0h] [rbp+0h] BYREF

v1 = alloca(2LL * (2 * a1 + 2));
if ( a1 > 0 )
{
v2 = &savedregs;
do
{
*(_WORD *)v2 = 1;
*((_WORD *)v2 + 1) = 0;
v2 = (__int64 *)((char *)v2 + 4);
}
while ( v2 != (__int64 *)((char *)&savedregs + 4 * (unsigned int)a1) );
}
*((_WORD *)&savedregs + 2 * a1) = 1;
*((_WORD *)&savedregs + 2 * a1 + 1) = a1;
result = semctl(ipc_id[0], -1, 17, &savedregs);
if ( result == -1 )
{
write_to_fd(2LL, "Failed to initialize semaphore set\n");
result = func3(15);
}
return result;
}

original_code:
void init_sems(int processors) {
 int i, nsems = 2 * processors + 2;
 union semun args;
 unsigned short values[nsems];
 for (i = 0; i < processors; ++i) {
  values[2 * i] = 1;
  values[(2 * i) + 1] = 0;
 }
 values[nsems - 2] = 1;
 values[nsems - 1] = processors;
 args.array = values;
 if (semctl(ipc_id[0], -1, 17, args) == -1) {
  write_to_fd(2, "Failed to initialize semaphore set\n");
  kill_group(15);
 }
}

canonical_original_code:
void init_sems(int processors) {
 int i; int nsems = 2 * processors + 2;
 union semun args;
 unsigned short values[-1];
 for (i = 0; i < processors; ++i) {
  values[2 * i] = 1;
  values[(2 * i) + 1] = 0;
 }
 values[nsems - 2] = 1;
 values[nsems - 1] = processors;
 args.array = values;
 if (semctl(ipc_id[0], -1, 17, args) == -1) {
  write_to_fd(2, "Failed to initialize semaphore set\n");
  kill_group(15);
 }
}

variable_types: processors int

variable_types: union semun { int val; struct semid_ds { struct ipc_perm { int __key; unsigned int uid; unsigned int gid; unsigned int cuid; unsigned int cgid; unsigned short mode; unsigned short __pad1; unsigned short __seq; unsigned short __pad2; unsigned long __glibc_reserved1; unsigned long __glibc_reserved2; } sem_perm; long sem_otime; unsigned long __glibc_reserved1; long sem_ctime; unsigned long __glibc_reserved2; unsigned long sem_nsems; unsigned long __glibc_reserved3; unsigned long __glibc_reserved4; } *buf; unsigned short *array; struct seminfo { int semmap; int semmni; int semmns; int semmnu; int semmsl; int semopm; int semume; int semusz; int semvmx; int semaem; } *__buf; }

variable_types:
values
unsigned short [-1]

user_defined_types: union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }

user_defined_types: struct semid_ds { struct ipc_perm sem_perm; long sem_otime; unsigned long __glibc_reserved1; long sem_ctime; unsigned long __glibc_reserved2; unsigned long sem_nsems; unsigned long __glibc_reserved3; unsigned long __glibc_reserved4; }


decompiled_code:
int __fastcall sem_operation(unsigned __int16 a1, __int16 a2)
{
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

sops.sem_num = a1;
sops.sem_op = a2;
sops.sem_flg = 0;
result = semop(ipc_id[0], &sops, 1uLL);
if ( result == -1 )
{
write_to_fd(2LL, "Failed semaphore operation\n");
result = kill_group(15);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(unsigned __int16 a1, __int16 a2)
{
int result; // eax
struct sembuf sops; // [rsp+Ah] [rbp-Eh] BYREF

sops.sem_num = a1;
sops.sem_op = a2;
sops.sem_flg = 0;
result = semop(ipc_id[0], &sops, 1uLL);
if ( result == -1 )
{
write_to_fd(2LL, "Failed semaphore operation\n");
result = func3(15);
}
return result;
}

original_code:
static void sem_operation(int semnum, short op) {
 struct sembuf sops;
 sops.sem_num = semnum;
 sops.sem_op = op;
 sops.sem_flg = 0;
 if (semop(ipc_id[0], &sops, 1) == -1) {
  write_to_fd(2, "Failed semaphore operation\n");
  kill_group(15);
 }
}

variable_types: semnum int

variable_types: op short

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


decompiled_code:
int __fastcall sem_p(unsigned __int16 a1)
{
return sem_operation(a1, -1);
}

canonical_decompiled_code:
int __fastcall func7(unsigned __int16 a1)
{
return func6(a1, -1);
}

original_code:
void sem_p(int semnum) {
 sem_operation(semnum, -1);
}


decompiled_code:
int __fastcall sem_v(unsigned __int16 a1)
{
return sem_operation(a1, 1);
}

canonical_decompiled_code:
int __fastcall func8(unsigned __int16 a1)
{
return func6(a1, 1);
}

original_code:
void sem_v(int semnum) {
 sem_operation(semnum, 1);
}


decompiled_code:
void *__fastcall shm_attach(int a1)
{
void *v1; // rbx

v1 = shmat(a1, 0LL, 0);
if ( v1 == (void *)-1LL )
{
write_to_fd(2LL, "Failed to attach shared memory segment\n");
kill_group(15);
}
return v1;
}

canonical_decompiled_code:
void *__fastcall func9(int a1)
{
void *v1; // rbx

v1 = shmat(a1, 0LL, 0);
if ( v1 == (void *)-1LL )
{
write_to_fd(2LL, "Failed to attach shared memory segment\n");
func3(15);
}
return v1;
}

original_code:
void* shm_attach(int shm_id) {
 void *ret = shmat(shm_id, ((void *)0), 0);
 if (ret == (void *) -1) {
  write_to_fd(2, "Failed to attach shared memory segment\n");
  kill_group(15);
 }
 return ret;
}

canonical_original_code:
void *shm_attach(int shm_id) {
 void *ret = shmat(shm_id, ((void *)0), 0);
 if (ret == (void *) -1) {
  write_to_fd(2, "Failed to attach shared memory segment\n");
  kill_group(15);
 }
 return ret;
}

variable_types:
ret
void *

return_type: void *


decompiled_code:
__int64 __fastcall find_proc(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebp

sem_p((unsigned int)(2 * processors));
write_to_fd(1LL, "Looking for a free processor\n");
v1 = 0;
do
{
v2 = v1++;
a1 += 4LL;
}
while ( *(int *)(a1 - 4) > 0 );
sem_v((unsigned int)(2 * processors));
write_with_int(1LL, "Found processor ", v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // ebx
unsigned int v2; // ebp

sem_p((unsigned int)(2 * processors));
write_to_fd(1LL, "Looking for a free processor\n");
v1 = 0;
do
{
v2 = v1++;
a1 += 4LL;
}
while ( *(int *)(a1 - 4) > 0 );
sem_v((unsigned int)(2 * processors));
write_with_int(1LL, "Found processor ", v1);
return v2;
}

original_code:
static int find_proc(int *states) {
 int i = 0;
 sem_p(2 * processors);
 write_to_fd(1, "Looking for a free processor\n");
 while(states[i++] > 0);
 sem_v(2 * processors);
 write_with_int(1, "Found processor ", i);
 return i - 1;
}

variable_types:
states
int *

return_type: int


decompiled_code:
void __noreturn stop_execution()
{
write_to_fd(2LL, "SIGTERM received. Closing IPCs\n");
close_ipc();
exit(1);
}

canonical_decompiled_code:
void __noreturn func1()
{
write_to_fd(2LL, "SIGTERM received. Closing IPCs\n");
close_ipc();
exit(1);
}

original_code:
static void stop_execution(int signum) {
 write_to_fd(2, "SIGTERM received. Closing IPCs\n");
 close_ipc();
 exit(1);
}


decompiled_code:
__pid_t start_processors()
{
__pid_t result; // eax
int v1; // ebx
__int64 v2; // [rsp+0h] [rbp-18h] BYREF
char v3[16]; // [rsp+8h] [rbp-10h] BYREF

result = itoa((unsigned int)(2 * processors + 2), &v2, 8LL);
if ( result == -1 )
{
write_to_fd(2LL, "Failed to convert number of semaphores\n");
result = kill_group(15LL);
}
if ( processors > 0 )
{
v1 = 0;
do
{
result = fork();
if ( result == -1 )
{
write_to_fd(2LL, "Failed to fork processor\n");
result = kill_group(15LL);
}
else if ( !result )
{
if ( (unsigned int)itoa((unsigned int)v1, v3, 8LL) != -1 )
execl("processor.x", "processor.x", v3, &v2, 0LL);
result = kill_group(15LL);
}
++v1;
}
while ( processors > v1 );
}
return result;
}

canonical_decompiled_code:
__pid_t func3()
{
__pid_t result; // eax
int v1; // ebx
__int64 v2; // [rsp+0h] [rbp-18h] BYREF
char v3[16]; // [rsp+8h] [rbp-10h] BYREF

result = itoa((unsigned int)(2 * processors + 2), &v2, 8LL);
if ( result == -1 )
{
write_to_fd(2LL, "Failed to convert number of semaphores\n");
result = kill_group(15LL);
}
if ( processors > 0 )
{
v1 = 0;
do
{
result = fork();
if ( result == -1 )
{
write_to_fd(2LL, "Failed to fork processor\n");
result = kill_group(15LL);
}
else if ( !result )
{
if ( (unsigned int)itoa((unsigned int)v1, v3, 8LL) != -1 )
execl("processor.x", "processor.x", v3, &v2, 0LL);
result = kill_group(15LL);
}
++v1;
}
while ( processors > v1 );
}
return result;
}

original_code:
static void start_processors(void) {
 int i, pid;
 char proc_id[8], nsems[8];
 if(itoa((2 * processors) + 2, nsems, 8) == -1) {
  write_to_fd(2, "Failed to convert number of semaphores\n");
  kill_group(15);
 }
 for (i = 0; i < processors; ++i) {
  pid = fork();
  if (pid == -1) {
   write_to_fd(2, "Failed to fork processor\n");
   kill_group(15);
  }
  if (pid == 0) {
   if(itoa(i, proc_id, 8) != -1)
    execl("processor.x", "processor.x", proc_id, nsems, (char *) ((void *)0));
   kill_group(15);
  }
 }
}

canonical_original_code:
static void start_processors() {
 int i; int pid;
 char proc_id[8]; char nsems[8];
 if(itoa((2 * processors) + 2, nsems, 8) == -1) {
  write_to_fd(2, "Failed to convert number of semaphores\n");
  kill_group(15);
 }
 for (i = 0; i < processors; ++i) {
  pid = fork();
  if (pid == -1) {
   write_to_fd(2, "Failed to fork processor\n");
   kill_group(15);
  }
  if (pid == 0) {
   if(itoa(i, proc_id, 8) != -1)
    execl("processor.x", "processor.x", proc_id, nsems, (char *) ((void *)0));
   kill_group(15);
  }
 }
}

variable_types: pid int

variable_types:
nsems
char [8]


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
const char *v4; // rax
int v5; // eax
__int64 v6; // r15
__int64 v7; // rax
unsigned int v8; // er12
int v9; // ebx
unsigned int v10; // er13
unsigned int *v11; // r14
char *v12; // rax
__int64 v13; // rbx
char *v14; // rax
char *v15; // rax
int v16; // ebx
__int64 v17; // rdx
_DWORD *v18; // r13
unsigned int *v19; // r12
int v20; // ebx
int v21; // ebp
__int64 v22; // rdx
int v23; // ebx
char *ptr; // [rsp+10h] [rbp-58h]
__int64 v25; // [rsp+18h] [rbp-50h]
_DWORD *v26; // [rsp+20h] [rbp-48h]
int v27; // [rsp+28h] [rbp-40h]
unsigned int v28; // [rsp+2Ch] [rbp-3Ch]

if ( signal(15, (__sighandler_t)stop_execution) != (__sighandler_t)-1LL )
{
if ( argc != 3 )
{
write_to_fd(2LL, "Usage: main.x <source file> <results file>\n");
exit(1);
}
v3 = parse_file((char *)argv[1]);
v4 = (const char *)list_extract(v3);
v5 = atoi(v4);
processors = v5;
if ( v5 > 0 )
{
write_with_int(1LL, "Number of processors: ", (unsigned int)v5);
v28 = list_count(v3);
if ( !v28 )
{
write_to_fd(2LL, "No operations provided\n");
exit(1);
}
write_with_int(1LL, "Number of operations: ", v28);
v26 = malloc(4LL * (int)v28);
if ( v26 )
{
init_ipc(
(unsigned int)(2 * processors + 2),
(unsigned int)(12 * processors),
(unsigned int)(4 * processors),
1974LL);
write_with_int(1LL, "Created semaphore set with ID ", (unsigned int)ipc_id[0]);
write_with_int(1LL, "Created shm for operations with ID ", (unsigned int)ipc_id[1]);
write_with_int(1LL, "Created shm for states with ID ", (unsigned int)ipc_id[2]);
init_sems((unsigned int)processors);
v25 = shm_attach((unsigned int)ipc_id[1]);
v6 = shm_attach((unsigned int)ipc_id[2]);
if ( processors > 0 )
{
v7 = 0LL;
do
*(_DWORD *)(v6 + 4 * v7++) = 0;
while ( processors > (int)v7 );
}
start_processors();
v8 = 1;
while ( (int)list_count(v3) > 0 )
{
ptr = (char *)list_extract(v3);
write_with_int(1LL, "\nOperation #", v8);
v15 = strtok(ptr, " ");
v16 = atoi(v15);
sem_p((unsigned int)(2 * processors + 1));
if ( v16 )
v9 = v16 - 1;
else
v9 = find_proc(v6);
v10 = v9 + 1;
write_with_int(1LL, "Waiting for processor ", (unsigned int)(v9 + 1));
v27 = 2 * v9;
sem_p((unsigned int)(2 * v9));
write_with_int(1LL, "Delivering operation to processor ", (unsigned int)(v9 + 1));
v11 = (unsigned int *)(v6 + 4LL * v9);
if ( *v11 )
{
v17 = *(unsigned int *)(v25 + 12LL * v9);
v26[~*v11] = v17;
write_with_int(1LL, "Previous result: ", v17);
}
v12 = strtok(0LL, " ");
v13 = v25 + 12LL * v9;
*(_DWORD *)v13 = atoi(v12);
*(_BYTE *)(v13 + 4) = *strtok(0LL, " ");
v14 = strtok(0LL, " ");
*(_DWORD *)(v13 + 8) = atoi(v14);
*v11 = v8;
write_with_int(1LL, "Operation delivered. Unblocking processor ", v10);
sem_v((unsigned int)(v27 + 1));
free(ptr);
++v8;
}
list_destruct(v3);
if ( processors > 0 )
{
v18 = (_DWORD *)v6;
v19 = (unsigned int *)v25;
v20 = 0;
do
{
v21 = 2 * v20;
sem_p((unsigned int)(2 * v20++));
write_with_int(1LL, "\nPassing termination command to processor #", (unsigned int)v20);
if ( *v18 )
{
v22 = *v19;
v26[~*v18] = v22;
write_with_int(1LL, "Last result: ", v22);
}
*((_BYTE *)v19 + 4) = 75;
sem_v((unsigned int)(v21 + 1));
++v18;
v19 += 3;
}
while ( v20 < processors );
if ( processors > 0 )
{
v23 = 0;
do
{
if ( wait(0LL) == -1 )
write_to_fd(2LL, "Wait failed\n");
++v23;
}
while ( processors > v23 );
}
}
write_to_fd(1LL, "\nAll processors exited. Writing output file\n");
write_results(argv[2], v26, v28);
free(v26);
write_to_fd(1LL, "Closing IPCs\n");
shm_detach(v25);
shm_detach(v6);
close_ipc();
exit(0);
}
write_to_fd(2LL, "Failed to allocate results array\n");
exit(1);
}
write_to_fd(2LL, "Invalid number of processors\n");
exit(1);
}
write_to_fd(2LL, "Failed to register signal\n");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
const char *v4; // rax
int v5; // eax
__int64 v6; // r15
__int64 v7; // rax
unsigned int v8; // er12
int v9; // ebx
unsigned int v10; // er13
unsigned int *v11; // r14
char *v12; // rax
__int64 v13; // rbx
char *v14; // rax
char *v15; // rax
int v16; // ebx
__int64 v17; // rdx
_DWORD *v18; // r13
unsigned int *v19; // r12
int v20; // ebx
int v21; // ebp
__int64 v22; // rdx
int v23; // ebx
char *ptr; // [rsp+10h] [rbp-58h]
__int64 v25; // [rsp+18h] [rbp-50h]
_DWORD *v26; // [rsp+20h] [rbp-48h]
int v27; // [rsp+28h] [rbp-40h]
unsigned int v28; // [rsp+2Ch] [rbp-3Ch]

if ( signal(15, (__sighandler_t)stop_execution) != (__sighandler_t)-1LL )
{
if ( argc != 3 )
{
write_to_fd(2LL, "Usage: main.x <source file> <results file>\n");
exit(1);
}
v3 = func2((char *)argv[1]);
v4 = (const char *)list_extract(v3);
v5 = atoi(v4);
processors = v5;
if ( v5 > 0 )
{
write_with_int(1LL, "Number of processors: ", (unsigned int)v5);
v28 = list_count(v3);
if ( !v28 )
{
write_to_fd(2LL, "No operations provided\n");
exit(1);
}
write_with_int(1LL, "Number of operations: ", v28);
v26 = malloc(4LL * (int)v28);
if ( v26 )
{
init_ipc(
(unsigned int)(2 * processors + 2),
(unsigned int)(12 * processors),
(unsigned int)(4 * processors),
1974LL);
write_with_int(1LL, "Created semaphore set with ID ", (unsigned int)ipc_id[0]);
write_with_int(1LL, "Created shm for operations with ID ", (unsigned int)ipc_id[1]);
write_with_int(1LL, "Created shm for states with ID ", (unsigned int)ipc_id[2]);
init_sems((unsigned int)processors);
v25 = shm_attach((unsigned int)ipc_id[1]);
v6 = shm_attach((unsigned int)ipc_id[2]);
if ( processors > 0 )
{
v7 = 0LL;
do
*(_DWORD *)(v6 + 4 * v7++) = 0;
while ( processors > (int)v7 );
}
func3();
v8 = 1;
while ( (int)list_count(v3) > 0 )
{
ptr = (char *)list_extract(v3);
write_with_int(1LL, "\nOperation #", v8);
v15 = strtok(ptr, " ");
v16 = atoi(v15);
sem_p((unsigned int)(2 * processors + 1));
if ( v16 )
v9 = v16 - 1;
else
v9 = func0(v6);
v10 = v9 + 1;
write_with_int(1LL, "Waiting for processor ", (unsigned int)(v9 + 1));
v27 = 2 * v9;
sem_p((unsigned int)(2 * v9));
write_with_int(1LL, "Delivering operation to processor ", (unsigned int)(v9 + 1));
v11 = (unsigned int *)(v6 + 4LL * v9);
if ( *v11 )
{
v17 = *(unsigned int *)(v25 + 12LL * v9);
v26[~*v11] = v17;
write_with_int(1LL, "Previous result: ", v17);
}
v12 = strtok(0LL, " ");
v13 = v25 + 12LL * v9;
*(_DWORD *)v13 = atoi(v12);
*(_BYTE *)(v13 + 4) = *strtok(0LL, " ");
v14 = strtok(0LL, " ");
*(_DWORD *)(v13 + 8) = atoi(v14);
*v11 = v8;
write_with_int(1LL, "Operation delivered. Unblocking processor ", v10);
sem_v((unsigned int)(v27 + 1));
free(ptr);
++v8;
}
list_destruct(v3);
if ( processors > 0 )
{
v18 = (_DWORD *)v6;
v19 = (unsigned int *)v25;
v20 = 0;
do
{
v21 = 2 * v20;
sem_p((unsigned int)(2 * v20++));
write_with_int(1LL, "\nPassing termination command to processor #", (unsigned int)v20);
if ( *v18 )
{
v22 = *v19;
v26[~*v18] = v22;
write_with_int(1LL, "Last result: ", v22);
}
*((_BYTE *)v19 + 4) = 75;
sem_v((unsigned int)(v21 + 1));
++v18;
v19 += 3;
}
while ( v20 < processors );
if ( processors > 0 )
{
v23 = 0;
do
{
if ( wait(0LL) == -1 )
write_to_fd(2LL, "Wait failed\n");
++v23;
}
while ( processors > v23 );
}
}
write_to_fd(1LL, "\nAll processors exited. Writing output file\n");
write_results(argv[2], v26, v28);
free(v26);
write_to_fd(1LL, "Closing IPCs\n");
shm_detach(v25);
shm_detach(v6);
close_ipc();
exit(0);
}
write_to_fd(2LL, "Failed to allocate results array\n");
exit(1);
}
write_to_fd(2LL, "Invalid number of processors\n");
exit(1);
}
write_to_fd(2LL, "Failed to register signal\n");
exit(1);
}

original_code:
int main(int argc, char *argv[]) {
 int proc_id, nsems;
 int *shm_states;
 operation *shm_operations;
 proc_id = atoi(argv[1]);
 write_with_int(1, "\tProcessor - Started as #", proc_id + 1);
 nsems = atoi(argv[2]);
 init_ipc(0, 0, 0, 0666);
 shm_operations = (operation *) shm_attach(ipc_id[1]);
 shm_states = (int *) shm_attach(ipc_id[2]);
 while(1) {
  sem_p((2 * proc_id) + 1);
  if (shm_operations[proc_id].op == 'K')
   break;
  write_with_int(1, "\tOperation received - Processor ", proc_id + 1);
  compute(shm_operations + proc_id);
  sem_p(nsems - 2);
  shm_states[proc_id] *= -1;
  sem_v(nsems - 2);
  write_with_int(1, "\tResult computed. Unblocking main - Processor ", proc_id + 1);
  sem_v(2 * proc_id);
  sem_v(nsems - 1);
 }
 shm_detach((void *) shm_operations);
 shm_detach((void *) shm_states);
 write_with_int(1, "\tExiting - Processor ", proc_id + 1);
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int proc_id; int nsems;
 int *shm_states;
 struct operation *shm_operations;
 proc_id = atoi(argv[1]);
 write_with_int(1, "\tProcessor - Started as #", proc_id + 1);
 nsems = atoi(argv[2]);
 init_ipc(0, 0, 0, 0666);
 shm_operations = (struct operation *) shm_attach(ipc_id[1]);
 shm_states = (int *) shm_attach(ipc_id[2]);
 while(1) {
  sem_p((2 * proc_id) + 1);
  if (shm_operations[proc_id].op == 'K')
   break;
  write_with_int(1, "\tOperation received - Processor ", proc_id + 1);
  compute(shm_operations + proc_id);
  sem_p(nsems - 2);
  shm_states[proc_id] *= -1;
  sem_v(nsems - 2);
  write_with_int(1, "\tResult computed. Unblocking main - Processor ", proc_id + 1);
  sem_v(2 * proc_id);
  sem_v(nsems - 1);
 }
 shm_detach((void *) shm_operations);
 shm_detach((void *) shm_states);
 write_with_int(1, "\tExiting - Processor ", proc_id + 1);
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct operation { int num1; char op; int num2; } *


decompiled_code:
void __fastcall list_destruct_recursive(void *ptr)
{
if ( ptr )
{
list_destruct_recursive(*((void **)ptr + 1));
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
func0(*((void **)ptr + 1));
free(*(void **)ptr);
free(ptr);
}
}

original_code:
static void list_destruct_recursive(list_node *n) {
 if (n) {
  list_destruct_recursive(n->next);
  free(n->data);
  free(n);
 }
}

canonical_original_code:
static void list_destruct_recursive(struct list_node *n) {
 if (n) {
  list_destruct_recursive(n->next);
  free(n->data);
  free(n);
 }
}

variable_types: struct list_node { char *data; struct list_node *next; } *


decompiled_code:
_QWORD *list_construct()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
}
return result;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
}
return result;
}

original_code:
list* list_construct() {
 list *l = (list *) malloc(sizeof(list));
 if(l) {
  l->head = ((void *)0);
  l->tail = ((void *)0);
  l->elements = 0;
 }
 return l;
}

canonical_original_code:
struct list *list_construct() {
 struct list *l = (list *) malloc(sizeof(list));
 if(l) {
  l->head = ((void *)0);
  l->tail = ((void *)0);
  l->elements = 0;
 }
 return l;
}

variable_types: struct list { struct list_node { char *data; struct list_node *next; } *head; struct list_node *tail; int elements; } *

user_defined_types: struct list { struct list_node *head; struct list_node *tail; int elements; }


decompiled_code:
void __fastcall list_destruct(void *ptr)
{
if ( ptr )
{
list_destruct_recursive(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
if ( ptr )
{
func0(*(void **)ptr);
free(ptr);
}
}

original_code:
void list_destruct(list *l) {
 if (l) {
  list_destruct_recursive(l->head);
  free(l);
 }
}

canonical_original_code:
void list_destruct(struct list *l) {
 if (l) {
  list_destruct_recursive(l->head);
  free(l);
 }
}

user_defined_types: struct list_node { char *data; struct list_node *next; }


decompiled_code:
__int64 __fastcall list_append(__int64 a1, char *a2)
{
_QWORD *v2; // rax
int v3; // ecx

v2 = node_construct(a2);
if ( !a1 || !v2 )
return 0xFFFFFFFFLL;
v3 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = v3 + 1;
if ( v3 )
{
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v2;
*(_QWORD *)(a1 + 8) = v2;
}
else
{
*(_QWORD *)(a1 + 8) = v2;
*(_QWORD *)a1 = v2;
}
return 0LL;
}

original_code:
int list_append(list *const l, char *const s) {
 list_node *node = node_construct(s);
 if (!l || !node)
  return -1;
 l->elements += 1;
 if (l->elements == 1)
  l->head = l->tail = node;
 else
  l->tail = l->tail->next = node;
 return 0;
}

canonical_original_code:
int list_append(struct list *l, char *s) {
 struct list_node *node = node_construct(s);
 if (!l || !node)
  return -1;
 l->elements += 1;
 if (l->elements == 1)
  l->head = l->tail = node;
 else
  l->tail = l->tail->next = node;
 return 0;
}

variable_types:
s
char *


original_code:
char* list_extract(list *const l) {
 char *res;
 list_node *new_head;
 if (!l || !l->elements)
  return ((void *)0);
 res = l->head->data;
 new_head = l->head->next;
 free(l->head);
 l->elements -= 1;
 l->head = new_head;
 return res;
}

canonical_original_code:
char *list_extract(struct list *l) {
 char *res;
 struct list_node *new_head;
 if (!l || !l->elements)
  return ((void *)0);
 res = l->head->data;
 new_head = l->head->next;
 free(l->head);
 l->elements -= 1;
 l->head = new_head;
 return res;
}

variable_types:
res
char *

return_type: char *


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

if ( a1 )
result = *(unsigned int *)(a1 + 16);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 )
result = *(unsigned int *)(a1 + 16);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int list_count(const list *const l) {
 return l ? l->elements : -1;
}

canonical_original_code:
int list_count(struct list *l) {
 return l ? l->elements : -1;
}


itoa

original_code:
int itoa(int num, char *const buffer, int buf_len) {
 int i = 0, j = 0;
 char temp;
 if (num == 0)
  buffer[i++] = '0';
 if (num < 0) {
  buffer[i++] = '-';
  num *= -1;
  j++;
 }
 while ((i < buf_len - 1) && (num > 0)) {
  buffer[i++] = num % 10 + '0';
  num /= 10;
 }
 if (num > 0)
  return -1;
 buffer[i--] = '\0';
 while (j < i) {
  temp = buffer[i];
  buffer[i--] = buffer[j];
  buffer[j++] = temp;
 }
 return 0;
}

canonical_original_code:
int itoa(int num, char *buffer, int buf_len) {
 int i = 0; int j = 0;
 char temp;
 if (num == 0)
  buffer[i++] = '0';
 if (num < 0) {
  buffer[i++] = '-';
  num *= -1;
  j++;
 }
 while ((i < buf_len - 1) && (num > 0)) {
  buffer[i++] = num % 10 + '0';
  num /= 10;
 }
 if (num > 0)
  return -1;
 buffer[i--] = '\0';
 while (j < i) {
  temp = buffer[i];
  buffer[i--] = buffer[j];
  buffer[j++] = temp;
 }
 return 0;
}

variable_types: num int

variable_types:
buffer
char *

variable_types: i int

variable_types: j int

variable_types: temp char


decompiled_code:
void __fastcall write_to_fd(int a1, const char *a2)
{
size_t v2; // rax

if ( a2 )
{
v2 = strlen(a2);
if ( write(a1, a2, v2) == -1 )
write_to_fd(2LL, "Write failed\n");
}
}

canonical_decompiled_code:
void __fastcall func1(int a1, const char *a2)
{
size_t v2; // rax

if ( a2 )
{
v2 = strlen(a2);
if ( write(a1, a2, v2) == -1 )
func1(2LL, "Write failed\n");
}
}

original_code:
void write_to_fd(int fd, const char *const s) {
 if (s != ((void *)0))
  if (write(fd, s, strlen(s) * sizeof(char)) == -1)
   write_to_fd(2, "Write failed\n");
}

canonical_original_code:
void write_to_fd(int fd, char *s) {
 if (s != ((void *)0))
  if (write(fd, s, strlen(s) * sizeof(char)) == -1)
   write_to_fd(2, "Write failed\n");
}

variable_types: fd int


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

if ( chars_left_2 )
goto LABEL_2;
v3 = read(a1, buffer_1, 0x200uLL);
chars_left_2 = v3;
i_0 = 0;
if ( v3 )
{
if ( v3 == -1 )
{
write_to_fd(2, "Failed to read from file\n");
exit(1);
}
LABEL_2:
--chars_left_2;
v1 = i_0++;
return (unsigned __int8)buffer_1[v1];
}
return 0xFFFFFFFFLL;
}

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

if ( chars_left_2 )
goto LABEL_2;
v3 = read(a1, buffer_1, 0x200uLL);
chars_left_2 = v3;
i_0 = 0;
if ( v3 )
{
if ( v3 == -1 )
{
func1(2, "Failed to read from file\n");
exit(1);
}
LABEL_2:
--chars_left_2;
v1 = i_0++;
return (unsigned __int8)buffer_1[v1];
}
return 0xFFFFFFFFLL;
}

original_code:
static char read_char(int fd) {
 static char buffer[512];
 static int chars_left = 0;
 static int i = 0;
 if (chars_left == 0) {
  chars_left = read(fd, &buffer, 512 * sizeof(char));
  i = 0;
  if (chars_left == 0)
   return (-1);
  if (chars_left == -1) {
   write_to_fd(2, "Failed to read from file\n");
   exit(1);
  }
 }
 --chars_left;
 return buffer[i++];
}

variable_types:
buffer
char [512]

return_type: char


decompiled_code:
__int64 __fastcall read_line(int a1, __int64 a2, int a3)
{
__int64 v3; // rbp
__int64 v4; // rbx
unsigned int v5; // er12
char v6; // al

if ( a3 <= 0 )
{
LABEL_6:
write_to_fd(2, "Buffer overflow\n");
exit(1);
}
v3 = (unsigned int)a3;
v4 = 0LL;
while ( 1 )
{
v5 = v4;
v6 = read_char(a1);
if ( v6 == -1 )
{
*(_BYTE *)(a2 + (int)v4) = 0;
return (unsigned int)-1;
}
if ( v6 == 10 )
break;
*(_BYTE *)(a2 + v4++) = v6;
if ( v4 == v3 )
goto LABEL_6;
}
*(_BYTE *)(a2 + (int)v4) = 0;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2, int a3)
{
__int64 v3; // rbp
__int64 v4; // rbx
unsigned int v5; // er12
char v6; // al

if ( a3 <= 0 )
{
LABEL_6:
func1(2, "Buffer overflow\n");
exit(1);
}
v3 = (unsigned int)a3;
v4 = 0LL;
while ( 1 )
{
v5 = v4;
v6 = func2(a1);
if ( v6 == -1 )
{
*(_BYTE *)(a2 + (int)v4) = 0;
return (unsigned int)-1;
}
if ( v6 == 10 )
break;
*(_BYTE *)(a2 + v4++) = v6;
if ( v4 == v3 )
goto LABEL_6;
}
*(_BYTE *)(a2 + (int)v4) = 0;
return v5;
}

original_code:
int read_line(int fd, char *const dest, const int max_length) {
 int i = 0;
 char c;
 while (i < max_length) {
  c = read_char(fd);
  if (c == (-1)) {
   dest[i] = '\0';
   return -1;
  } else if (c == '\n') {
   dest[i] = '\0';
   return i;
  } else
   dest[i++] = c;
 }
 write_to_fd(2, "Buffer overflow\n");
 exit(1);
}

canonical_original_code:
int read_line(int fd, char *dest, int max_length) {
 int i = 0;
 char c;
 while (i < max_length) {
  c = read_char(fd);
  if (c == (-1)) {
   dest[i] = '\0';
   return -1;
  } else if (c == '\n') {
   dest[i] = '\0';
   return i;
  } else
   dest[i++] = c;
 }
 write_to_fd(2, "Buffer overflow\n");
 exit(1);
}

variable_types:
dest
char *

variable_types: c char


decompiled_code:
void __fastcall write_with_int(int a1, const char *a2, int a3)
{
int v3; // ebx
size_t v4; // rbp
char *v5; // rbx
char s[52]; // [rsp+4h] [rbp-34h] BYREF

if ( (unsigned int)itoa(a3, s, 12) == -1 )
write_to_fd(2, "Failed to convert integer\n");
v3 = strlen(a2);
v4 = (int)(v3 + strlen(s) + 2);
v5 = (char *)malloc(v4);
if ( !v5 )
write_to_fd(2, "Failed to allocate message string\n");
strcpy(v5, a2);
strcat(v5, s);
v5[v4 - 2] = 10;
v5[v4 - 1] = 0;
write_to_fd(a1, v5);
free(v5);
}

canonical_decompiled_code:
void __fastcall func4(int a1, const char *a2, int a3)
{
int v3; // ebx
size_t v4; // rbp
char *v5; // rbx
char s[52]; // [rsp+4h] [rbp-34h] BYREF

if ( (unsigned int)func0(a3, s, 12) == -1 )
func1(2, "Failed to convert integer\n");
v3 = strlen(a2);
v4 = (int)(v3 + strlen(s) + 2);
v5 = (char *)malloc(v4);
if ( !v5 )
func1(2, "Failed to allocate message string\n");
strcpy(v5, a2);
strcat(v5, s);
v5[v4 - 2] = 10;
v5[v4 - 1] = 0;
func1(a1, v5);
free(v5);
}

original_code:
void write_with_int(int fd, const char *const s, int num) {
 char *message;
 char num_buffer[12];
 int message_len;
 if (itoa(num, num_buffer, 12) == -1)
  write_to_fd(2, "Failed to convert integer\n");
 message_len = strlen(s) + strlen(num_buffer) + 2;
 message = (char *) malloc(message_len * sizeof(char));
 if (message == ((void *)0))
  write_to_fd(2, "Failed to allocate message string\n");
 strcpy(message, s);
 strcat(message, num_buffer);
 message[message_len - 2] = '\n';
 message[message_len - 1] = '\0';
 write_to_fd(fd, message);
 free(message);
}

canonical_original_code:
void write_with_int(int fd, char *s, int num) {
 char *message;
 char num_buffer[12];
 int message_len;
 if (itoa(num, num_buffer, 12) == -1)
  write_to_fd(2, "Failed to convert integer\n");
 message_len = strlen(s) + strlen(num_buffer) + 2;
 message = (char *) malloc(message_len * sizeof(char));
 if (message == ((void *)0))
  write_to_fd(2, "Failed to allocate message string\n");
 strcpy(message, s);
 strcat(message, num_buffer);
 message[message_len - 2] = '\n';
 message[message_len - 1] = '\0';
 write_to_fd(fd, message);
 free(message);
}

variable_types:
message
char *


decompiled_code:
int __fastcall write_results(const char *a1, int *a2, int a3)
{
int v4; // ebp
int *v5; // rbx
__int64 v6; // r12
int result; // eax

v4 = open(a1, 577, 384LL);
if ( v4 == -1 )
{
write_to_fd(2, "Failed to open results file\n");
kill_group(15LL);
}
if ( a3 > 0 )
{
v5 = a2;
v6 = (__int64)&a2[a3 - 1 + 1];
do
write_with_int(v4, (const char *)&unk_615, *v5++);
while ( v5 != (int *)v6 );
}
result = close(v4);
if ( result == -1 )
{
write_to_fd(2, "Failed to close results file\n");
result = kill_group(15LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(const char *a1, int *a2, int a3)
{
int v4; // ebp
int *v5; // rbx
__int64 v6; // r12
int result; // eax

v4 = open(a1, 577, 384LL);
if ( v4 == -1 )
{
func1(2, "Failed to open results file\n");
kill_group(15LL);
}
if ( a3 > 0 )
{
v5 = a2;
v6 = (__int64)&a2[a3 - 1 + 1];
do
func4(v4, (const char *)&unk_615, *v5++);
while ( v5 != (int *)v6 );
}
result = close(v4);
if ( result == -1 )
{
func1(2, "Failed to close results file\n");
result = kill_group(15LL);
}
return result;
}

original_code:
void write_results(const char *const pathname, int *results, int length) {
 int fd, i;
 fd = open(pathname, 01 | 0100 | 01000, 0400 | 0200);
 if(fd == -1) {
  write_to_fd(2, "Failed to open results file\n");
  kill_group(15);
 }
 for(i = 0; i < length; ++i) {
  write_with_int(fd, "", results[i]);
 }
 if(close(fd) == -1) {
  write_to_fd(2, "Failed to close results file\n");
  kill_group(15);
 }
}

canonical_original_code:
void write_results(char *pathname, int *results, int length) {
 int fd; int i;
 fd = open(pathname, 01 | 0100 | 01000, 0400 | 0200);
 if(fd == -1) {
  write_to_fd(2, "Failed to open results file\n");
  kill_group(15);
 }
 for(i = 0; i < length; ++i) {
  write_with_int(fd, "", results[i]);
 }
 if(close(fd) == -1) {
  write_to_fd(2, "Failed to close results file\n");
  kill_group(15);
 }
}

variable_types:
pathname
char *

variable_types:
results
int *

variable_types: length int


compute

decompiled_code:
__int64 __fastcall compute(__int64 a1)
{
char v1; // al
__int64 result; // rax
int v3; // ecx

v1 = *(_BYTE *)(a1 + 4);
if ( v1 == 45 )
{
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)a1 -= result;
return result;
}
if ( v1 <= 45 )
{
if ( v1 == 42 )
{
result = (unsigned int)(*(_DWORD *)(a1 + 8) * *(_DWORD *)a1);
*(_DWORD *)a1 = result;
return result;
}
if ( v1 == 43 )
{
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)a1 += result;
return result;
}
goto LABEL_12;
}
if ( v1 != 47 )
{
LABEL_12:
write_to_fd(2LL, "\tInvalid operator\n");
return kill_group(15LL);
}
v3 = *(_DWORD *)(a1 + 8);
if ( v3 )
{
result = (unsigned int)(*(_DWORD *)a1 / v3);
*(_DWORD *)a1 = result;
}
else
{
write_to_fd(2LL, "\tDivision by 0\n");
result = kill_group(15LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
char v1; // al
__int64 result; // rax
int v3; // ecx

v1 = *(_BYTE *)(a1 + 4);
if ( v1 == 45 )
{
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)a1 -= result;
return result;
}
if ( v1 <= 45 )
{
if ( v1 == 42 )
{
result = (unsigned int)(*(_DWORD *)(a1 + 8) * *(_DWORD *)a1);
*(_DWORD *)a1 = result;
return result;
}
if ( v1 == 43 )
{
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)a1 += result;
return result;
}
goto LABEL_12;
}
if ( v1 != 47 )
{
LABEL_12:
write_to_fd(2LL, "\tInvalid operator\n");
return kill_group(15LL);
}
v3 = *(_DWORD *)(a1 + 8);
if ( v3 )
{
result = (unsigned int)(*(_DWORD *)a1 / v3);
*(_DWORD *)a1 = result;
}
else
{
write_to_fd(2LL, "\tDivision by 0\n");
result = kill_group(15LL);
}
return result;
}

original_code:
static void compute(operation *oper) {
 switch(oper->op) {
  case '+': oper->num1 = oper->num1 + oper->num2; break;
  case '-': oper->num1 = oper->num1 - oper->num2; break;
  case '*': oper->num1 = oper->num1 * oper->num2; break;
  case '/': if(oper->num2 != 0)
    oper->num1 = oper->num1 / oper->num2;
     else {
    write_to_fd(2, "\tDivision by 0\n");
    kill_group(15);
     }; break;
  default: write_to_fd(2, "\tInvalid operator\n");
    kill_group(15);
 }
}

canonical_original_code:
static void compute(struct operation *oper) {
 switch(oper->op) {
  case '+': oper->num1 = oper->num1 + oper->num2; break;
  case '-': oper->num1 = oper->num1 - oper->num2; break;
  case '*': oper->num1 = oper->num1 * oper->num2; break;
  case '/': if(oper->num2 != 0)
    oper->num1 = oper->num1 / oper->num2;
     else {
    write_to_fd(2, "\tDivision by 0\n");
    kill_group(15);
     }; break;
  default: write_to_fd(2, "\tInvalid operator\n");
    kill_group(15);
 }
}

variable_types: struct operation { int num1; char op; int num2; } *


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // ebx
int v5; // er14
unsigned int v6; // er15
__int64 v7; // rax
__int64 v8; // rbp
_DWORD *v9; // r13
unsigned int v10; // er12
__int64 v11; // [rsp+10h] [rbp-48h]
__int64 v12; // [rsp+18h] [rbp-40h]

v3 = atoi(argv[1]);
v4 = v3 + 1;
write_with_int(1LL, "\tProcessor - Started as #", (unsigned int)(v3 + 1));
v5 = atoi(argv[2]);
init_ipc(0LL, 0LL, 0LL, 438LL);
v11 = shm_attach(ipc_id[1]);
v12 = shm_attach(ipc_id[2]);
v6 = 2 * v3;
v7 = v3;
v8 = v11 + 12LL * v3;
v9 = (_DWORD *)(v12 + 4 * v7);
v10 = v5 - 2;
while ( 1 )
{
sem_p(v6 + 1);
if ( *(_BYTE *)(v8 + 4) == 75 )
break;
write_with_int(1LL, "\tOperation received - Processor ", v4);
compute(v8);
sem_p(v10);
*v9 = -*v9;
sem_v(v10);
write_with_int(1LL, "\tResult computed. Unblocking main - Processor ", v4);
sem_v(v6);
sem_v((unsigned int)(v5 - 1));
}
shm_detach(v11);
shm_detach(v12);
write_with_int(1LL, "\tExiting - Processor ", v4);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // ebx
int v5; // er14
unsigned int v6; // er15
__int64 v7; // rax
__int64 v8; // rbp
_DWORD *v9; // r13
unsigned int v10; // er12
__int64 v11; // [rsp+10h] [rbp-48h]
__int64 v12; // [rsp+18h] [rbp-40h]

v3 = atoi(argv[1]);
v4 = v3 + 1;
write_with_int(1LL, "\tProcessor - Started as #", (unsigned int)(v3 + 1));
v5 = atoi(argv[2]);
init_ipc(0LL, 0LL, 0LL, 438LL);
v11 = shm_attach(ipc_id[1]);
v12 = shm_attach(ipc_id[2]);
v6 = 2 * v3;
v7 = v3;
v8 = v11 + 12LL * v3;
v9 = (_DWORD *)(v12 + 4 * v7);
v10 = v5 - 2;
while ( 1 )
{
sem_p(v6 + 1);
if ( *(_BYTE *)(v8 + 4) == 75 )
break;
write_with_int(1LL, "\tOperation received - Processor ", v4);
func0(v8);
sem_p(v10);
*v9 = -*v9;
sem_v(v10);
write_with_int(1LL, "\tResult computed. Unblocking main - Processor ", v4);
sem_v(v6);
sem_v((unsigned int)(v5 - 1));
}
shm_detach(v11);
shm_detach(v12);
write_with_int(1LL, "\tExiting - Processor ", v4);
exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: nsems int

user_defined_types: struct operation { int num1; char op; int num2; }


decompiled_code:
__int64 SW_SKY_init()
{
interpolate_monthlyValues(&SW_Sky, (unsigned int)((_DWORD)&SW_Sky + 480));
interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 96), (unsigned int)((_DWORD)&SW_Sky + 3416));
interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 192), (unsigned int)((_DWORD)&SW_Sky + 6352));
interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 288), (unsigned int)((_DWORD)&SW_Sky + 9288));
return interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 384), (unsigned int)((_DWORD)&SW_Sky + 12224));
}

canonical_decompiled_code:
__int64 func1()
{
interpolate_monthlyValues(&SW_Sky, (unsigned int)((_DWORD)&SW_Sky + 480));
interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 96), (unsigned int)((_DWORD)&SW_Sky + 3416));
interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 192), (unsigned int)((_DWORD)&SW_Sky + 6352));
interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 288), (unsigned int)((_DWORD)&SW_Sky + 9288));
return interpolate_monthlyValues((unsigned int)((_DWORD)&SW_Sky + 384), (unsigned int)((_DWORD)&SW_Sky + 12224));
}

original_code:
void SW_SKY_init(void) {
   SW_SKY *v = &SW_Sky;
 interpolate_monthlyValues(v->cloudcov, v->cloudcov_daily);
 interpolate_monthlyValues(v->windspeed, v->windspeed_daily);
 interpolate_monthlyValues(v->r_humidity, v->r_humidity_daily);
 interpolate_monthlyValues(v->transmission, v->transmission_daily);
 interpolate_monthlyValues(v->snow_density, v->snow_density_daily);
}

canonical_original_code:
void SW_SKY_init() {
   struct SW_SKY *v = &SW_Sky;
 interpolate_monthlyValues(v->cloudcov, v->cloudcov_daily);
 interpolate_monthlyValues(v->windspeed, v->windspeed_daily);
 interpolate_monthlyValues(v->r_humidity, v->r_humidity_daily);
 interpolate_monthlyValues(v->transmission, v->transmission_daily);
 interpolate_monthlyValues(v->snow_density, v->snow_density_daily);
}

variable_types: struct SW_SKY { double cloudcov[12]; double cloudcov_daily[-1]; } *


decompiled_code:
void SW_SKY_construct()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void SW_SKY_construct(void) {
}

canonical_original_code:
void SW_SKY_construct() {
}


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

*(_QWORD *)&SW_Markov = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
*((_QWORD *)&SW_Markov + 1) = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
*((_QWORD *)&SW_Markov + 2) = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
result = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
*((_QWORD *)&SW_Markov + 3) = result;
return result;
}

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

*(_QWORD *)&SW_Markov = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
*((_QWORD *)&SW_Markov + 1) = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
*((_QWORD *)&SW_Markov + 2) = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
result = Mem_Calloc(366LL, 8LL, "SW_MKV_construct");
*((_QWORD *)&SW_Markov + 3) = result;
return result;
}

original_code:
void SW_MKV_construct(void) {
  SW_MARKOV *m = &SW_Markov;
  size_t s = sizeof(RealD);
  m->wetprob = (RealD *) Mem_Calloc( 366, s, "SW_MKV_construct");
  m->dryprob = (RealD *) Mem_Calloc( 366, s, "SW_MKV_construct");
  m->avg_ppt = (RealD *) Mem_Calloc( 366, s, "SW_MKV_construct");
  m->std_ppt = (RealD *) Mem_Calloc( 366, s, "SW_MKV_construct");
}

canonical_original_code:
void SW_MKV_construct() {
  struct SW_MARKOV *m = &SW_Markov;
  unsigned long s = sizeof(RealD);
  m->wetprob = (double *) Mem_Calloc( 366, s, "SW_MKV_construct");
  m->dryprob = (double *) Mem_Calloc( 366, s, "SW_MKV_construct");
  m->avg_ppt = (double *) Mem_Calloc( 366, s, "SW_MKV_construct");
  m->std_ppt = (double *) Mem_Calloc( 366, s, "SW_MKV_construct");
}

variable_types: struct SW_MARKOV { double *wetprob; int ppt_events; } *

variable_types:
s
unsigned long


variable_types:
doy
unsigned int

variable_types:
tmax
double *

variable_types:
tmin
double *

variable_types:
rain
double *

variable_types:
week
unsigned int

variable_types: prob float

variable_types: p float

variable_types: x float


DirName

decompiled_code:
char *__fastcall DirName(char *src)
{
char *v1; // rax
size_t v2; // rbx

s_1[0] = 0;
v1 = strrchr(src, 47);
if ( v1 || (v1 = strrchr(src, 92)) != 0LL )
{
v2 = (int)v1 - (int)src + 1;
strncpy(s_1, src, v2);
s_1[v2] = 0;
}
return s_1;
}

canonical_decompiled_code:
char *__fastcall func1(char *src)
{
char *v1; // rax
size_t v2; // rbx

s_1[0] = 0;
v1 = strrchr(src, 47);
if ( v1 || (v1 = strrchr(src, 92)) != 0LL )
{
v2 = (int)v1 - (int)src + 1;
strncpy(s_1, src, v2);
s_1[v2] = 0;
}
return s_1;
}

original_code:
char *DirName( const char *p) {
  static char s[4096];
  char *c;
  int l;
  *s = '\0';
  if (!(c=strrchr(p, '/')) )
    c=strrchr(p, '\\');
  if (c) {
    l = c - p +1;
    strncpy(s, p, l);
    s[l] = '\0';
  }
  return s;
}

canonical_original_code:
char *DirName(char *p) {
  static char s[4096];
  char *c;
  int l;
  *s = '\0';
  if (!(c=strrchr(p, '/')) )
    c=strrchr(p, '\\');
  if (c) {
    l = c - p +1;
    strncpy(s, p, l);
    s[l] = '\0';
  }
  return s;
}

variable_types:
p
char *

variable_types:
c
char *

variable_types: l int


BaseName

decompiled_code:
char *__fastcall BaseName(char *s)
{
char *v1; // rax
char *result; // rax

v1 = strrchr(s, 47);
if ( v1 || (v1 = strrchr(s, 92)) != 0LL )
result = v1 + 1;
else
result = s;
return result;
}

canonical_decompiled_code:
char *__fastcall func2(char *s)
{
char *v1; // rax
char *result; // rax

v1 = strrchr(s, 47);
if ( v1 || (v1 = strrchr(s, 92)) != 0LL )
result = v1 + 1;
else
result = s;
return result;
}

original_code:
const char *BaseName( const char *p) {
  char *c;
  if (!(c=strrchr(p, '/')) )
    c=strrchr(p, '\\');
  return ((c!=((void *)0))? c+1 : p);
}

canonical_original_code:
char *BaseName(char *p) {
  char *c;
  if (!(c=strrchr(p, '/')) )
    c=strrchr(p, '\\');
  return ((c!=((void *)0))? c+1 : p);
}


FileExists

decompiled_code:
__int64 __fastcall FileExists(const char *a1)
{
int v1; // edx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 15) & 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
int v1; // edx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 15) & 1;
return result;
}

original_code:
Bool FileExists(const char *name) {
  struct stat statbuf;
  Bool result=FALSE;
  if (0==stat(name, &statbuf))
    result = (statbuf.st_mode & 0100000) ? TRUE : FALSE;
  return(result);
}

canonical_original_code:
enum Bool FileExists(char *name) {
  struct stat statbuf;
  enum Bool result = FALSE;
  if (0==stat(name, &statbuf))
    result = (statbuf.st_mode & 0100000) ? TRUE : FALSE;
  return(result);
}

variable_types:
name
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]; }

variable_types:
result
enum Bool { FALSE=expr, TRUE=expr }

return_type: enum Bool { FALSE=expr, TRUE=expr }

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


DirExists

decompiled_code:
__int64 __fastcall DirExists(const char *a1)
{
int v1; // edx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 14) & 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
int v1; // edx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 14) & 1;
return result;
}

original_code:
Bool DirExists(const char *dname) {
  struct stat statbuf;
  Bool result=FALSE;
  if (0==stat(dname, &statbuf))
    result = (statbuf.st_mode & 0040000) ? TRUE : FALSE;
  return(result);
}

canonical_original_code:
enum Bool DirExists(char *dname) {
  struct stat statbuf;
  enum Bool result = FALSE;
  if (0==stat(dname, &statbuf))
    result = (statbuf.st_mode & 0040000) ? TRUE : FALSE;
  return(result);
}

variable_types:
dname
char *

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


ChDir

decompiled_code:
_BOOL8 __fastcall ChDir(const char *a1)
{
return chdir(a1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(const char *a1)
{
return chdir(a1) == 0;
}

original_code:
Bool ChDir( const char *dname) {
  return (chdir(dname) ==0) ? TRUE : FALSE;
}

canonical_original_code:
enum Bool ChDir(char *dname) {
  return (chdir(dname) ==0) ? TRUE : FALSE;
}


MkDir

decompiled_code:
__int64 __fastcall MkDir(char *s)
{
char *v1; // rdi
__int64 v2; // rbx
int v3; // ebp
char *v4; // rax
int v5; // edx
const char **v6; // rbp
__int64 *v7; // r12
unsigned int v8; // ebx
__int64 v10[259]; // [rsp+0h] [rbp-818h] BYREF

memset(v10, 0, 0x800uLL);
if ( !s )
return 0;
v1 = strdup(s);
if ( !v1 )
{
fwrite("Out of memory making string in MkDir()", 1uLL, 0x26uLL, stderr);
exit(-1);
}
v10[0] = (__int64)strtok(v1, "\\/");
v2 = 1LL;
do
{
v3 = v2;
v4 = strtok(0LL, "\\/");
v10[v2] = (__int64)v4;
v5 = v2++;
}
while ( v4 );
errstr = 0;
if ( v3 <= 0 )
return 1;
v6 = (const char **)v10;
v7 = &v10[(unsigned int)(v5 - 1) + 1];
while ( 1 )
{
strcat(&errstr, *v6);
v8 = DirExists(&errstr);
if ( !v8 && mkdir(&errstr, 0x1FFu) && *_errno_location() == 13 )
break;
*(_WORD *)(&errstr + strlen(&errstr)) = 47;
if ( ++v6 == (const char **)v7 )
return 1;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s)
{
char *v1; // rdi
__int64 v2; // rbx
int v3; // ebp
char *v4; // rax
int v5; // edx
const char **v6; // rbp
__int64 *v7; // r12
unsigned int v8; // ebx
__int64 v10[259]; // [rsp+0h] [rbp-818h] BYREF

memset(v10, 0, 0x800uLL);
if ( !s )
return 0;
v1 = strdup(s);
if ( !v1 )
{
fwrite("Out of memory making string in MkDir()", 1uLL, 0x26uLL, stderr);
exit(-1);
}
v10[0] = (__int64)strtok(v1, "\\/");
v2 = 1LL;
do
{
v3 = v2;
v4 = strtok(0LL, "\\/");
v10[v2] = (__int64)v4;
v5 = v2++;
}
while ( v4 );
errstr = 0;
if ( v3 <= 0 )
return 1;
v6 = (const char **)v10;
v7 = &v10[(unsigned int)(v5 - 1) + 1];
while ( 1 )
{
strcat(&errstr, *v6);
v8 = func5(&errstr);
if ( !v8 && mkdir(&errstr, 0x1FFu) && *_errno_location() == 13 )
break;
*(_WORD *)(&errstr + strlen(&errstr)) = 47;
if ( ++v6 == (const char **)v7 )
return 1;
}
return v8;
}

original_code:
Bool MkDir( const char *dname) {
  int r, i, n;
  Bool result = TRUE;
  char *a[256] = {0},
       *delim = "\\/",
       *c;
  if ((((void *)0) == (dname)) ) return FALSE;
  if (((void *)0) == (c = strdup(dname)) ) {
    fprintf(stderr, "Out of memory making string in MkDir()");
    exit(-1);
  }
  n=0;
  a[n++] = strtok(c, delim);
  while( ((void *)0) != (a[n++] = strtok(((void *)0), delim)) ) ;
  n--;
  errstr[0] = '\0';
  for(i=0; i<n; i++) {
    strcat(errstr, a[i]) ;
    if (!DirExists(errstr)) {
      if ( 0 != (r = mkdir(errstr, 0777)) ) {
        if ((*__errno_location ()) == 13 ) {
          result = FALSE;
          break;
        }
      }
    }
    strcat(errstr, "/");
  }
  return result;
}

canonical_original_code:
enum Bool MkDir(char *dname) {
  int r; int i; int n;
  enum Bool result = TRUE;
  char *a[256] = {0}; char *delim = "\\/"; char *c;
  if ((((void *)0) == (dname)) ) return FALSE;
  if (((void *)0) == (c = strdup(dname)) ) {
    fprintf(stderr, "Out of memory making string in MkDir()");
    exit(-1);
  }
  n=0;
  a[n++] = strtok(c, delim);
  while( ((void *)0) != (a[n++] = strtok(((void *)0), delim)) ) ;
  n--;
  errstr[0] = '\0';
  for(i=0; i<n; i++) {
    strcat(errstr, a[i]) ;
    if (!DirExists(errstr)) {
      if ( 0 != (r = mkdir(errstr, 0777)) ) {
        if ((*__errno_location ()) == 13 ) {
          result = FALSE;
          break;
        }
      }
    }
    strcat(errstr, "/");
  }
  return result;
}

variable_types: r int

variable_types: i int

variable_types: n int

variable_types:
delim
char *


RemoveFiles

decompiled_code:
__int64 __fastcall RemoveFiles(char *src)
{
__int64 result; // rax
__int64 v2; // rbp
char *v3; // rax
int v4; // eax
__int64 v5; // rbx
char *v6; // r12
unsigned int v7; // er12
__int64 v8; // rbx
int v9; // [rsp+0h] [rbp-41Ch] BYREF
char dest[1048]; // [rsp+4h] [rbp-418h] BYREF

result = 1LL;
if ( src )
{
v2 = getfiles(src, &v9);
if ( v2 )
{
v3 = DirName(src);
strcpy(dest, v3);
v4 = strlen(dest);
if ( v9 <= 0 )
{
v7 = 1;
LABEL_12:
Mem_Free(v2);
return v7;
}
v5 = 0LL;
v6 = &dest[v4];
while ( 1 )
{
strcpy(v6, *(const char **)(v2 + 8 * v5));
if ( remove(dest) )
break;
if ( v9 <= (int)++v5 )
{
v7 = 1;
goto LABEL_9;
}
}
v7 = 0;
}
else
{
v7 = 1;
}
LABEL_9:
if ( v9 > 0 )
{
v8 = 0LL;
do
Mem_Free(*(_QWORD *)(v2 + 8 * v8++));
while ( v9 > (int)v8 );
}
goto LABEL_12;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *src)
{
__int64 result; // rax
__int64 v2; // rbp
char *v3; // rax
int v4; // eax
__int64 v5; // rbx
char *v6; // r12
unsigned int v7; // er12
__int64 v8; // rbx
int v9; // [rsp+0h] [rbp-41Ch] BYREF
char dest[1048]; // [rsp+4h] [rbp-418h] BYREF

result = 1LL;
if ( src )
{
v2 = func8(src, &v9);
if ( v2 )
{
v3 = func1(src);
strcpy(dest, v3);
v4 = strlen(dest);
if ( v9 <= 0 )
{
v7 = 1;
LABEL_12:
Mem_Free(v2);
return v7;
}
v5 = 0LL;
v6 = &dest[v4];
while ( 1 )
{
strcpy(v6, *(const char **)(v2 + 8 * v5));
if ( remove(dest) )
break;
if ( v9 <= (int)++v5 )
{
v7 = 1;
goto LABEL_9;
}
}
v7 = 0;
}
else
{
v7 = 1;
}
LABEL_9:
if ( v9 > 0 )
{
v8 = 0LL;
do
Mem_Free(*(_QWORD *)(v2 + 8 * v8++));
while ( v9 > (int)v8 );
}
goto LABEL_12;
}
return result;
}

original_code:
Bool RemoveFiles(const char *fspec) {
  char **flist,
       fname[1024];
  int i, nfiles, dlen, result = TRUE;
  if (fspec == ((void *)0)) return TRUE;
  if ((flist = getfiles(fspec, &nfiles)) ) {
    strcpy(fname, DirName(fspec));
    dlen = strlen(fname);
    for(i=0; i<nfiles; i++) {
      strcpy(fname+dlen, flist[i]);
      if (0 != remove(fname)) {
        result = FALSE;
        break;
      }
    }
  }
  for(i=0; i< nfiles; i++)
    Mem_Free(flist[i]);
  Mem_Free(flist);
  return result;
}

canonical_original_code:
enum Bool RemoveFiles(char *fspec) {
  char **flist; char fname[1024];
  int i; int nfiles; int dlen; int result = TRUE;
  if (fspec == ((void *)0)) return TRUE;
  if ((flist = getfiles(fspec, &nfiles)) ) {
    strcpy(fname, DirName(fspec));
    dlen = strlen(fname);
    for(i=0; i<nfiles; i++) {
      strcpy(fname+dlen, flist[i]);
      if (0 != remove(fname)) {
        result = FALSE;
        break;
      }
    }
  }
  for(i=0; i< nfiles; i++)
    Mem_Free(flist[i]);
  Mem_Free(flist);
  return result;
}

variable_types:
fspec
char *

variable_types:
flist
char **

variable_types:
fname
char [1024]

variable_types: nfiles int

variable_types: dlen int

variable_types: result int


decompiled_code:
const char *__fastcall Str_TrimRight(const char *a1)
{
const char *v1; // rbx

v1 = &a1[strlen(a1)];
do
--v1;
while ( a1 <= v1 && ((*_ctype_b_loc())[*v1] & 0x2000) != 0 );
*((_BYTE *)v1 + 1) = 0;
return a1;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
const char *v1; // rbx

v1 = &a1[strlen(a1)];
do
--v1;
while ( a1 <= v1 && ((*_ctype_b_loc())[*v1] & 0x2000) != 0 );
*((_BYTE *)v1 + 1) = 0;
return a1;
}

original_code:
char *Str_TrimRight(char *s) {
  char *p = s + strlen(s);
  while( (--p)>=s && ((*__ctype_b_loc ())[(int) (((int)*p))] & (unsigned short int) _ISspace));
      *(++p) = '\0';
  return s;
}

canonical_original_code:
char *Str_TrimRight(char *s) {
  char *p = s + strlen(s);
  while( (--p)>=s && ((*__ctype_b_loc ())[(int) (((int)*p))] & (unsigned short) _ISspace));
      *(++p) = '\0';
  return s;
}

variable_types:
s
char *

variable_types:
p
char *


decompiled_code:
__int64 __fastcall Str_CompareI(char *a1, char *a2)
{
size_t v2; // rax
_BYTE *v3; // rbp
size_t v4; // rax
_BYTE *v5; // rbx
const char *v6; // r13
const char *v7; // rax
unsigned int v8; // er12

v2 = strlen(a1);
v3 = malloc(v2 + 1);
v4 = strlen(a2);
v5 = malloc(v4 + 1);
v6 = Str_ToUpper(a2, v5);
v7 = Str_ToUpper(a1, v3);
v8 = strcmp(v7, v6);
free(v3);
free(v5);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, char *a2)
{
size_t v2; // rax
_BYTE *v3; // rbp
size_t v4; // rax
_BYTE *v5; // rbx
const char *v6; // r13
const char *v7; // rax
unsigned int v8; // er12

v2 = strlen(a1);
v3 = malloc(v2 + 1);
v4 = strlen(a2);
v5 = malloc(v4 + 1);
v6 = Str_ToUpper(a2, v5);
v7 = Str_ToUpper(a1, v3);
v8 = strcmp(v7, v6);
free(v3);
free(v5);
return v8;
}

original_code:
int Str_CompareI(char *t, char *s) {
   char *t1 = (char *) malloc(strlen(t)+1);
   char *s1 = (char *) malloc(strlen(s)+1);
   int r = strcmp(Str_ToUpper(t,t1), Str_ToUpper(s,s1));
   free(t1); free(s1);
   return r;
}

variable_types:
t
char *


UnComment

decompiled_code:
const char *__fastcall UnComment(char *haystack)
{
char *v1; // rax
char *v3; // rax

v1 = strchr(haystack, 35);
if ( v1 )
{
*v1 = 0;
}
else
{
v3 = strstr(haystack, "/*");
if ( v3 )
uncomment_cstyle((__int64)v3);
}
return Str_TrimRight(haystack);
}

canonical_decompiled_code:
const char *__fastcall func3(char *haystack)
{
char *v1; // rax
char *v3; // rax

v1 = strchr(haystack, 35);
if ( v1 )
{
*v1 = 0;
}
else
{
v3 = strstr(haystack, "/*");
if ( v3 )
func0((__int64)v3);
}
return func1(haystack);
}

original_code:
void UnComment( char *s) {
  char *p;
  if ( (p=strchr(s,'#')) )
    *p = '\0';
  else if ( (p=strstr(s,"/*")) )
    uncomment_cstyle(p);
  Str_TrimRight(s);
}

canonical_original_code:
void UnComment(char *s) {
  char *p;
  if ( (p=strchr(s,'#')) )
    *p = '\0';
  else if ( (p=strstr(s,"/*")) )
    uncomment_cstyle(p);
  Str_TrimRight(s);
}


variable_types: yr int

variable_types: t int


regression

decompiled_code:
double __fastcall regression(double a1, double a2, double a3, double a4, double a5)
{
return (a4 - a3) / (a2 - a1) * (a5 - a1) + a3;
}

canonical_decompiled_code:
double __fastcall func6(double a1, double a2, double a3, double a4, double a5)
{
return (a4 - a3) / (a2 - a1) * (a5 - a1) + a3;
}

original_code:
double regression( double x1, double x2, double y1, double y2, double deltaX )
{
 return y1 + ( ((y2 - y1) / (x2 - x1)) * (deltaX - x1) );
}

canonical_original_code:
double regression(double x1, double x2, double y1, double y2, double deltaX)
{
 return y1 + ( ((y2 - y1) / (x2 - x1)) * (deltaX - x1) );
}

variable_types: deltaX double

return_type: double


variable_types:
equal
unsigned int *

variable_types:
size
unsigned int

variable_types: depth double

variable_types:
bounds
double []

variable_types:
i
unsigned int


lobf

decompiled_code:
void __fastcall lobf(double *a1, double *a2, __int64 a3, __int64 a4, int a5)
{
*a1 = lobfM(a3, a4, a5);
*a2 = lobfB(a3, a4, a5);
}

canonical_decompiled_code:
void __fastcall func10(double *a1, double *a2, __int64 a3, __int64 a4, int a5)
{
*a1 = func8(a3, a4, a5);
*a2 = func9(a3, a4, a5);
}

original_code:
void lobf( double *m, double *b, double xs[], double ys[], unsigned int n)
{
 *m = lobfM(xs, ys, n);
 *b = lobfB(xs, ys, n);
}

canonical_original_code:
void lobf(double *m, double *b, double xs[], double ys[], unsigned int n)
{
 *m = lobfM(xs, ys, n);
 *b = lobfB(xs, ys, n);
}

variable_types:
m
double *

variable_types:
b
double *

variable_types:
xs
double []

variable_types:
ys
double []

variable_types:
n
unsigned int


decompiled_code:
time_t __fastcall Time_set_mday(int a1)
{
tym.tm_mday = a1;
return remaketime();
}

canonical_decompiled_code:
time_t __fastcall func2(int a1)
{
tym.tm_mday = a1;
return func1();
}

original_code:
void Time_set_mday( const TimeInt day) {
  _tym.tm_mday = (int) day;
  _remaketime();
}

canonical_original_code:
void Time_set_mday(unsigned int day) {
  _tym.tm_mday = (int) day;
  _remaketime();
}

variable_types:
day
unsigned int


decompiled_code:
time_t __fastcall Time_set_month(int a1)
{
tym.tm_mon = a1;
return remaketime();
}

canonical_decompiled_code:
time_t __fastcall func3(int a1)
{
tym.tm_mon = a1;
return func1();
}

original_code:
void Time_set_month( const TimeInt mon) {
  _tym.tm_mon = (int) mon;
  _remaketime();
}

canonical_original_code:
void Time_set_month(unsigned int mon) {
  _tym.tm_mon = (int) mon;
  _remaketime();
}

variable_types:
mon
unsigned int


decompiled_code:
__int64 Time_timestamp()
{
return timestamp;
}

canonical_decompiled_code:
__int64 func4()
{
return timestamp;
}

original_code:
time_t Time_timestamp(void) {
  return _timestamp;
}

canonical_original_code:
long Time_timestamp() {
  return _timestamp;
}

return_type: long


decompiled_code:
time_t Time_timestamp_now()
{
return time(0LL);
}

canonical_decompiled_code:
time_t func5()
{
return time(0LL);
}

original_code:
time_t Time_timestamp_now(void) {
  return time(((void *)0));
}

canonical_original_code:
long Time_timestamp_now() {
  return time(((void *)0));
}


decompiled_code:
__int64 Time_lastDOY()
{
return (unsigned int)dword_74C;
}

canonical_decompiled_code:
__int64 func6()
{
return (unsigned int)dword_74C;
}

original_code:
TimeInt Time_lastDOY(void) {
  return cum_monthdays[Dec];
}

canonical_original_code:
unsigned int Time_lastDOY() {
  return cum_monthdays[Dec];
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall Time_days_in_month(unsigned int a1)
{
return *((unsigned int *)&days_in_month + a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
return *((unsigned int *)&days_in_month + a1);
}

original_code:
TimeInt Time_days_in_month(Months month) {
  return days_in_month[month];
}

canonical_original_code:
unsigned int Time_days_in_month(enum Months month) {
  return days_in_month[month];
}

variable_types: enum Months { Jan=0, Feb=1, Mar=2, Apr=3, May=4, Jun=5, Jul=6, Aug=7, Sep=8, Oct=9, Nov=10, Dec=11, NoMonth=12 }


decompiled_code:
char *Time_printtime()
{
return asctime(&tym);
}

canonical_decompiled_code:
char *func8()
{
return asctime(&tym);
}

original_code:
char *Time_printtime(void) {
  return(asctime(&_tym));
}

canonical_original_code:
char *Time_printtime() {
  return(asctime(&_tym));
}


decompiled_code:
char *Time_daynmshort()
{
strftime(outs, 0xAuLL, "%a", &tym);
return outs;
}

canonical_decompiled_code:
char *func9()
{
strftime(outs, 0xAuLL, "%a", &tym);
return outs;
}

original_code:
char *Time_daynmshort(void) {
  strftime( _outs, 10, "%a", &_tym);
  return _outs;
}

canonical_original_code:
char *Time_daynmshort() {
  strftime( _outs, 10, "%a", &_tym);
  return _outs;
}


decompiled_code:
char *__fastcall Time_daynmshort_dm(int a1, int a2)
{
struct tm v3; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v3.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v3.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v3.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v3.tm_zone = tym.tm_zone;
v3.tm_mday = a1;
v3.tm_mon = a2;
strftime(outs, 0xAuLL, "%a", &v3);
return outs;
}

canonical_decompiled_code:
char *__fastcall func10(int a1, int a2)
{
struct tm v3; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v3.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v3.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v3.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v3.tm_zone = tym.tm_zone;
v3.tm_mday = a1;
v3.tm_mon = a2;
strftime(outs, 0xAuLL, "%a", &v3);
return outs;
}

original_code:
char *Time_daynmshort_dm(const TimeInt mday, const TimeInt mon) {
  struct tm tmp = _tym;
  tmp.tm_mday = mday;
  tmp.tm_mon = mon;
  strftime( _outs, 10, "%a", &tmp);
  return _outs;
}

canonical_original_code:
char *Time_daynmshort_dm(unsigned int mday, unsigned int mon) {
  struct tm tmp = _tym;
  tmp.tm_mday = mday;
  tmp.tm_mon = mon;
  strftime( _outs, 10, "%a", &tmp);
  return _outs;
}

variable_types:
mday
unsigned int

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


decompiled_code:
char *Time_daynmlong()
{
strftime(outs, 0xAuLL, "%A", &tym);
return outs;
}

canonical_decompiled_code:
char *func11()
{
strftime(outs, 0xAuLL, "%A", &tym);
return outs;
}

original_code:
char *Time_daynmlong(void) {
  strftime( _outs, 10, "%A", &_tym);
  return _outs;
}

canonical_original_code:
char *Time_daynmlong() {
  strftime( _outs, 10, "%A", &_tym);
  return _outs;
}


decompiled_code:
char *__fastcall Time_daynmlong_dm(int a1, int a2)
{
struct tm v3; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v3.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v3.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v3.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v3.tm_zone = tym.tm_zone;
v3.tm_mday = a1;
v3.tm_mon = a2;
strftime(outs, 0xAuLL, "%A", &v3);
return outs;
}

canonical_decompiled_code:
char *__fastcall func12(int a1, int a2)
{
struct tm v3; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v3.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v3.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v3.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v3.tm_zone = tym.tm_zone;
v3.tm_mday = a1;
v3.tm_mon = a2;
strftime(outs, 0xAuLL, "%A", &v3);
return outs;
}

original_code:
char *Time_daynmlong_dm(const TimeInt mday, const TimeInt mon) {
  struct tm tmp = _tym;
  tmp.tm_mday = mday;
  tmp.tm_mon = mon;
  strftime( _outs, 10, "%A", &tmp);
  return _outs;
}

canonical_original_code:
char *Time_daynmlong_dm(unsigned int mday, unsigned int mon) {
  struct tm tmp = _tym;
  tmp.tm_mday = mday;
  tmp.tm_mon = mon;
  strftime( _outs, 10, "%A", &tmp);
  return _outs;
}

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


decompiled_code:
// attributes: thunk
__int64 Time_get_year()
{
return yearto4digit_t();
}

canonical_decompiled_code:
__int64 func13()
{
return func0();
}

original_code: TimeInt Time_get_year(void) { return _yearto4digit_t(); }

canonical_original_code: unsigned int Time_get_year() { return _yearto4digit_t(); }


decompiled_code:
__int64 Time_get_doy()
{
return (unsigned int)tym.tm_yday;
}

canonical_decompiled_code:
__int64 func14()
{
return (unsigned int)tym.tm_yday;
}

original_code: TimeInt Time_get_doy(void) { return (TimeInt) _tym.tm_yday; }

canonical_original_code: unsigned int Time_get_doy() { return (unsigned int) _tym.tm_yday; }


decompiled_code:
__int64 Time_get_month()
{
return (unsigned int)tym.tm_mon;
}

canonical_decompiled_code:
__int64 func15()
{
return (unsigned int)tym.tm_mon;
}

original_code: TimeInt Time_get_month(void) { return (TimeInt) _tym.tm_mon; }

canonical_original_code: unsigned int Time_get_month() { return (unsigned int) _tym.tm_mon; }


decompiled_code:
__int64 Time_get_mday()
{
return (unsigned int)tym.tm_mday;
}

canonical_decompiled_code:
__int64 func16()
{
return (unsigned int)tym.tm_mday;
}

original_code: TimeInt Time_get_mday(void) { return (TimeInt) _tym.tm_mday; }

canonical_original_code: unsigned int Time_get_mday() { return (unsigned int) _tym.tm_mday; }


decompiled_code:
__int64 Time_get_hour()
{
return (unsigned int)tym.tm_hour;
}

canonical_decompiled_code:
__int64 func17()
{
return (unsigned int)tym.tm_hour;
}

original_code: TimeInt Time_get_hour(void) { return (TimeInt) _tym.tm_hour; }

canonical_original_code: unsigned int Time_get_hour() { return (unsigned int) _tym.tm_hour; }


decompiled_code:
__int64 Time_get_mins()
{
return (unsigned int)tym.tm_min;
}

canonical_decompiled_code:
__int64 func18()
{
return (unsigned int)tym.tm_min;
}

original_code: TimeInt Time_get_mins(void) { return (TimeInt) _tym.tm_min; }

canonical_original_code: unsigned int Time_get_mins() { return (unsigned int) _tym.tm_min; }


decompiled_code:
__int64 Time_get_secs()
{
return (unsigned int)tym.tm_sec;
}

canonical_decompiled_code:
__int64 func19()
{
return (unsigned int)tym.tm_sec;
}

original_code: TimeInt Time_get_secs(void) { return (TimeInt) _tym.tm_sec; }

canonical_original_code: unsigned int Time_get_secs() { return (unsigned int) _tym.tm_sec; }


decompiled_code:
__int64 Time_get_lastdoy()
{
return (unsigned int)last_doy;
}

canonical_decompiled_code:
__int64 func20()
{
return (unsigned int)last_doy;
}

original_code: TimeInt Time_get_lastdoy(void) {return last_doy; }

canonical_original_code: unsigned int Time_get_lastdoy() {return last_doy; }


decompiled_code:
__int64 __fastcall doy2month(unsigned int a1)
{
__int64 i; // rax
unsigned int v2; // edx

for ( i = 0LL; i != 11; ++i )
{
v2 = i;
if ( *((_DWORD *)&cum_monthdays + i) >= a1 )
break;
v2 = i + 1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func21(unsigned int a1)
{
__int64 i; // rax
unsigned int v2; // edx

for ( i = 0LL; i != 11; ++i )
{
v2 = i;
if ( *((_DWORD *)&cum_monthdays + i) >= a1 )
break;
v2 = i + 1;
}
return v2;
}

original_code:
TimeInt doy2month(const TimeInt doy) {
  TimeInt mon;
  for( mon = Jan;
       mon < Dec &&
       doy > cum_monthdays[mon];
       mon++);
  return mon;
}

canonical_original_code:
unsigned int doy2month(unsigned int doy) {
  unsigned int mon;
  for( mon = Jan;
       mon < Dec &&
       doy > cum_monthdays[mon];
       mon++);
  return mon;
}

variable_types:
doy
unsigned int


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

v1 = a1;
v2 = doy2month(a1);
if ( v2 )
v1 = a1 - *((_DWORD *)&cum_monthdays + (unsigned int)(v2 - 1));
return v1;
}

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

v1 = a1;
v2 = func21(a1);
if ( v2 )
v1 = a1 - *((_DWORD *)&cum_monthdays + (unsigned int)(v2 - 1));
return v1;
}

original_code:
TimeInt doy2mday( const TimeInt doy) {
  TimeInt mon = doy2month(doy);
  return (mon == Jan) ? doy : doy - cum_monthdays[mon-1];
}

canonical_original_code:
unsigned int doy2mday(unsigned int doy) {
  unsigned int mon = doy2month(doy);
  return (mon == Jan) ? doy : doy - cum_monthdays[mon-1];
}


decompiled_code:
time_t __fastcall Time_set_doy(unsigned int a1)
{
tym.tm_yday = a1;
tym.tm_mday = doy2mday(a1);
tym.tm_mon = doy2month(a1);
return remaketime();
}

canonical_decompiled_code:
time_t __fastcall func23(unsigned int a1)
{
tym.tm_yday = a1;
tym.tm_mday = func22(a1);
tym.tm_mon = func21(a1);
return func1();
}

original_code:
void Time_set_doy( const TimeInt doy) {
  _tym.tm_yday = (int) doy;
  _tym.tm_mday = (int) doy2mday(doy);
  _tym.tm_mon = (int) doy2month(doy);
  _remaketime();
}

canonical_original_code:
void Time_set_doy(unsigned int doy) {
  _tym.tm_yday = (int) doy;
  _tym.tm_mday = (int) doy2mday(doy);
  _tym.tm_mon = (int) doy2month(doy);
  _remaketime();
}


decompiled_code:
char *__fastcall Time_daynmshort_d(unsigned int a1)
{
struct tm v2; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v2.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v2.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v2.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v2.tm_zone = tym.tm_zone;
v2.tm_mday = doy2mday(a1);
v2.tm_mon = doy2month(a1);
strftime(outs, 0xAuLL, "%a", &v2);
return outs;
}

canonical_decompiled_code:
char *__fastcall func24(unsigned int a1)
{
struct tm v2; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v2.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v2.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v2.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v2.tm_zone = tym.tm_zone;
v2.tm_mday = func22(a1);
v2.tm_mon = func21(a1);
strftime(outs, 0xAuLL, "%a", &v2);
return outs;
}

original_code:
char *Time_daynmshort_d(const TimeInt doy) {
  struct tm tmp = _tym;
  tmp.tm_mday = doy2mday(doy);
  tmp.tm_mon = doy2month(doy);
  strftime( _outs, 10, "%a", &tmp);
  return _outs;
}

canonical_original_code:
char *Time_daynmshort_d(unsigned int doy) {
  struct tm tmp = _tym;
  tmp.tm_mday = doy2mday(doy);
  tmp.tm_mon = doy2month(doy);
  strftime( _outs, 10, "%a", &tmp);
  return _outs;
}


decompiled_code:
char *__fastcall Time_daynmlong_d(unsigned int a1)
{
struct tm v2; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v2.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v2.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v2.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v2.tm_zone = tym.tm_zone;
v2.tm_mday = doy2mday(a1);
v2.tm_mon = doy2month(a1);
strftime(outs, 0xAuLL, "%A", &v2);
return outs;
}

canonical_decompiled_code:
char *__fastcall func25(unsigned int a1)
{
struct tm v2; // [rsp+0h] [rbp-48h] BYREF

*(__m128i *)&v2.tm_sec = _mm_load_si128((const __m128i *)&tym);
*(__m128i *)&v2.tm_mon = _mm_load_si128((const __m128i *)&tym.tm_mon);
*(__m128i *)&v2.tm_isdst = _mm_load_si128((const __m128i *)&tym.tm_isdst);
v2.tm_zone = tym.tm_zone;
v2.tm_mday = func22(a1);
v2.tm_mon = func21(a1);
strftime(outs, 0xAuLL, "%A", &v2);
return outs;
}

original_code:
char *Time_daynmlong_d(const TimeInt doy) {
  struct tm tmp = _tym;
  tmp.tm_mday = doy2mday(doy);
  tmp.tm_mon = doy2month(doy);
  strftime( _outs, 10, "%A", &tmp);
  return _outs;
}

canonical_original_code:
char *Time_daynmlong_d(unsigned int doy) {
  struct tm tmp = _tym;
  tmp.tm_mday = doy2mday(doy);
  tmp.tm_mon = doy2month(doy);
  strftime( _outs, 10, "%A", &tmp);
  return _outs;
}


decompiled_code:
__int64 __fastcall doy2week(int a1)
{
return (a1 - 1) / 7u;
}

canonical_decompiled_code:
__int64 __fastcall func26(int a1)
{
return (a1 - 1) / 7u;
}

original_code:
TimeInt doy2week(TimeInt doy) {
  return ((TimeInt)(((doy)-1)/7));
}

canonical_original_code:
unsigned int doy2week(unsigned int doy) {
  return ((TimeInt)(((doy)-1)/7));
}


decompiled_code:
__int64 Time_get_week()
{
return doy2week(tym.tm_yday);
}

canonical_decompiled_code:
__int64 func27()
{
return func26(tym.tm_yday);
}

original_code: TimeInt Time_get_week(void) { return doy2week(_tym.tm_yday); }

canonical_original_code: unsigned int Time_get_week() { return doy2week(_tym.tm_yday); }


decompiled_code:
__int64 __fastcall yearto4digit(unsigned int a1)
{
__int64 result; // rax

result = a1;
if ( a1 <= 0x64 )
{
result = a1 + 2000;
if ( a1 > 0x31 )
result = a1 + 1900;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func28(unsigned int a1)
{
__int64 result; // rax

result = a1;
if ( a1 <= 0x64 )
{
result = a1 + 2000;
if ( a1 > 0x31 )
result = a1 + 1900;
}
return result;
}

original_code:
TimeInt yearto4digit( TimeInt yr) {
  return (TimeInt) (( yr > 100) ? yr
                                : (yr<50)?2000+yr:1900+yr);
}

canonical_original_code:
unsigned int yearto4digit(unsigned int yr) {
  return (TimeInt) (( yr > 100) ? yr
                                : (yr<50)?2000+yr:1900+yr);
}

variable_types:
yr
unsigned int


isleapyear

original_code:
Bool isleapyear( const TimeInt year ) {
   int yr = (year > 100) ? year : yearto4digit(year);
   int t = (yr/100) * 100;
   return (Bool) ( (( yr % 4) == 0 ) &&
                 ( (( t ) != yr) ||
                 (( yr % 400) == 0 )
                 ) );
}

canonical_original_code:
enum Bool isleapyear(unsigned int year) {
   int yr = (year > 100) ? year : yearto4digit(year);
   int t = (yr/100) * 100;
   return (Bool) ( (( yr % 4) == 0 ) &&
                 ( (( t ) != yr) ||
                 (( yr % 400) == 0 )
                 ) );
}

variable_types:
year
unsigned int

variable_types: yr int

variable_types: t int


decompiled_code:
__int64 __fastcall Time_get_lastdoy_y(unsigned int a1)
{
return 365 - ((unsigned int)!isleapyear(a1) - 1);
}

canonical_decompiled_code:
__int64 __fastcall func30(unsigned int a1)
{
return 365 - ((unsigned int)!func29(a1) - 1);
}

original_code: TimeInt Time_get_lastdoy_y(TimeInt year) {return isleapyear(year) ? 366 : 365; }

canonical_original_code: unsigned int Time_get_lastdoy_y(unsigned int year) {return isleapyear(year) ? 366 : 365; }


decompiled_code:
_BOOL8 isleapyear_now()
{
unsigned int v0; // eax

v0 = yearto4digit_t();
return isleapyear(v0);
}

canonical_decompiled_code:
_BOOL8 func31()
{
unsigned int v0; // eax

v0 = func0();
return func29(v0);
}

original_code:
Bool isleapyear_now( void ) {
  return isleapyear( _yearto4digit_t() );
}

canonical_original_code:
enum Bool isleapyear_now() {
  return isleapyear( _yearto4digit_t() );
}


decompiled_code:
__int64 Time_now()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
*(__m128i *)&tym.tm_sec = _mm_loadu_si128((const __m128i *)v0);
*(__m128i *)&tym.tm_mon = _mm_loadu_si128((const __m128i *)&v0->tm_mon);
*(__m128i *)&tym.tm_isdst = _mm_loadu_si128((const __m128i *)&v0->tm_isdst);
tym.tm_zone = v0->tm_zone;
return reinit();
}

canonical_decompiled_code:
__int64 func33()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
*(__m128i *)&tym.tm_sec = _mm_loadu_si128((const __m128i *)v0);
*(__m128i *)&tym.tm_mon = _mm_loadu_si128((const __m128i *)&v0->tm_mon);
*(__m128i *)&tym.tm_isdst = _mm_loadu_si128((const __m128i *)&v0->tm_isdst);
tym.tm_zone = v0->tm_zone;
return func32();
}

original_code:
void Time_now(void) {
  time_t x = time(((void *)0));
  memcpy( &_tym, (struct tm *) localtime(&x ), sizeof(struct tm));
  _reinit();
}

canonical_original_code:
void Time_now() {
  long x = time(((void *)0));
  memcpy( &_tym, (struct tm *) localtime(&x ), sizeof(struct tm));
  _reinit();
}

variable_types: x long


decompiled_code:
__int64 Time_init()
{
days_in_month = (__int128)_mm_load_si128((const __m128i *)&monthdays);
xmmword_770 = (__int128)_mm_load_si128((const __m128i *)&xmmword_7B0);
xmmword_780 = (__int128)_mm_load_si128((const __m128i *)&xmmword_7C0);
cum_monthdays = 0LL;
qword_728 = 0LL;
qword_730 = 0LL;
qword_738 = 0LL;
qword_740 = 0LL;
unk_748 = 0LL;
dword_750 = 1000;
return Time_now();
}

canonical_decompiled_code:
__int64 func34()
{
days_in_month = (__int128)_mm_load_si128((const __m128i *)&monthdays);
xmmword_770 = (__int128)_mm_load_si128((const __m128i *)&xmmword_7B0);
xmmword_780 = (__int128)_mm_load_si128((const __m128i *)&xmmword_7C0);
cum_monthdays = 0LL;
qword_728 = 0LL;
qword_730 = 0LL;
qword_738 = 0LL;
qword_740 = 0LL;
unk_748 = 0LL;
dword_750 = 1000;
return func33();
}

original_code:
void Time_init(void) {
    memcpy(days_in_month, monthdays,
           sizeof(TimeInt)*12);
    memset(cum_monthdays, 0,
           sizeof(TimeInt)*12);
    cum_monthdays[NoMonth] = 1000;
    Time_now();
}

canonical_original_code:
void Time_init() {
    memcpy(days_in_month, monthdays,
           sizeof(TimeInt)*12);
    memset(cum_monthdays, 0,
           sizeof(TimeInt)*12);
    cum_monthdays[NoMonth] = 1000;
    Time_now();
}


decompiled_code:
time_t __fastcall Time_new_year(unsigned int a1)
{
tym.tm_year = yearto4digit(a1) - 1900;
reinit();
return Time_set_doy(1u);
}

canonical_decompiled_code:
time_t __fastcall func35(unsigned int a1)
{
tym.tm_year = func28(a1) - 1900;
func32();
return func23(1u);
}

original_code:
void Time_new_year (TimeInt year) {
  year = yearto4digit(year);
  _tym.tm_year = (int) year - 1900;
  _reinit();
  Time_set_doy(1);
}

canonical_original_code:
void Time_new_year(unsigned int year) {
  year = yearto4digit(year);
  _tym.tm_year = (int) year - 1900;
  _reinit();
  Time_set_doy(1);
}


decompiled_code:
time_t Time_next_day()
{
int v0; // edi
time_t result; // rax

v0 = tym.tm_yday;
if ( tym.tm_yday == last_doy )
{
++tym.tm_year;
result = reinit();
}
else
{
++tym.tm_yday;
result = Time_set_doy(v0 + 1);
}
return result;
}

canonical_decompiled_code:
time_t func36()
{
int v0; // edi
time_t result; // rax

v0 = tym.tm_yday;
if ( tym.tm_yday == last_doy )
{
++tym.tm_year;
result = func32();
}
else
{
++tym.tm_yday;
result = func23(v0 + 1);
}
return result;
}

original_code:
void Time_next_day(void) {
  if ((TimeInt)_tym.tm_yday == last_doy) {
    _tym.tm_year++;
    _reinit();
  } else {
    Time_set_doy(++_tym.tm_yday);
  }
}

canonical_original_code:
void Time_next_day() {
  if ((unsigned int)_tym.tm_yday == last_doy) {
    _tym.tm_year++;
    _reinit();
  } else {
    Time_set_doy(++_tym.tm_yday);
  }
}


decompiled_code:
time_t __fastcall Time_set_year(unsigned int a1)
{
int v1; // ebx
time_t result; // rax
int v3; // ebx

v1 = yearto4digit(a1);
result = yearto4digit_t();
if ( (_DWORD)result != v1 )
{
tym.tm_year = v1 - 1900;
reinit();
v3 = tym.tm_yday;
tym.tm_mday = doy2mday(tym.tm_yday);
tym.tm_mon = doy2month(v3);
result = remaketime();
}
return result;
}

canonical_decompiled_code:
time_t __fastcall func37(unsigned int a1)
{
int v1; // ebx
time_t result; // rax
int v3; // ebx

v1 = func28(a1);
result = func0();
if ( (_DWORD)result != v1 )
{
tym.tm_year = v1 - 1900;
func32();
v3 = tym.tm_yday;
tym.tm_mday = func22(tym.tm_yday);
tym.tm_mon = func21(v3);
result = func1();
}
return result;
}

original_code:
void Time_set_year( TimeInt year) {
  year = yearto4digit(year);
  if ( year == _yearto4digit_t())
    return;
  _tym.tm_year = (int) year - 1900;
  _reinit();
  _tym.tm_mday = (int) doy2mday((TimeInt) _tym.tm_yday);
  _tym.tm_mon = (int) doy2month((TimeInt) _tym.tm_yday);
  _remaketime();
}

canonical_original_code:
void Time_set_year(unsigned int year) {
  year = yearto4digit(year);
  if ( year == _yearto4digit_t())
    return;
  _tym.tm_year = (int) year - 1900;
  _reinit();
  _tym.tm_mday = (int) doy2mday((unsigned int) _tym.tm_yday);
  _tym.tm_mon = (int) doy2month((unsigned int) _tym.tm_yday);
  _remaketime();
}


decompiled_code:
__int64 __fastcall interpolate_monthlyValues(__int64 a1, __int64 a2)
{
__int64 i; // rbp
__int64 result; // rax
unsigned int v4; // edx
__int64 v5; // xmm0_8
double v6; // xmm3_8
int v7; // ebx

for ( i = 1LL; i != 367; ++i )
{
v7 = doy2mday(i);
LODWORD(result) = doy2month(i);
if ( v7 == 15 )
{
result = (unsigned int)result;
*(_QWORD *)(a2 + 8 * i) = *(_QWORD *)(a1 + 8LL * (unsigned int)result);
}
else
{
if ( (unsigned int)v7 <= 0xE )
{
if ( (_DWORD)result )
{
v4 = result - 1;
v5 = LC3;
}
else
{
v5 = LC3;
v4 = 11;
}
}
else if ( (_DWORD)result == 11 )
{
v5 = LC2;
v4 = 0;
}
else
{
v4 = result + 1;
v5 = LC2;
}
v6 = *(double *)(a1 + 8LL * (unsigned int)result);
result = *((unsigned int *)&monthdays + (unsigned int)result);
*(double *)(a2 + 8 * i) = (*(double *)(a1 + 8LL * v4) - v6)
* *(double *)&v5
/ (double)(int)result
* ((double)v7 - 15.0)
+ v6;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func38(__int64 a1, __int64 a2)
{
__int64 i; // rbp
__int64 result; // rax
unsigned int v4; // edx
__int64 v5; // xmm0_8
double v6; // xmm3_8
int v7; // ebx

for ( i = 1LL; i != 367; ++i )
{
v7 = func22(i);
LODWORD(result) = func21(i);
if ( v7 == 15 )
{
result = (unsigned int)result;
*(_QWORD *)(a2 + 8 * i) = *(_QWORD *)(a1 + 8LL * (unsigned int)result);
}
else
{
if ( (unsigned int)v7 <= 0xE )
{
if ( (_DWORD)result )
{
v4 = result - 1;
v5 = LC3;
}
else
{
v5 = LC3;
v4 = 11;
}
}
else if ( (_DWORD)result == 11 )
{
v5 = LC2;
v4 = 0;
}
else
{
v4 = result + 1;
v5 = LC2;
}
v6 = *(double *)(a1 + 8LL * (unsigned int)result);
result = *((unsigned int *)&monthdays + (unsigned int)result);
*(double *)(a2 + 8 * i) = (*(double *)(a1 + 8LL * v4) - v6)
* *(double *)&v5
/ (double)(int)result
* ((double)v7 - 15.0)
+ v6;
}
}
return result;
}

original_code:
void interpolate_monthlyValues( double monthlyValues[], double dailyValues[] ){
 unsigned int doy, mday, month, month2=NoMonth;
 double sign = 1.;
 for (doy = 1; doy <= 366; doy++){
  mday = doy2mday(doy);
  month = doy2month(doy);
  if(mday == 15) {
   dailyValues[doy] = monthlyValues[month];
  } else {
   if(mday >= 15) {
    month2 = (month == Dec) ? Jan : month+1;
    sign = 1;
   } else {
    month2 = (month == Jan) ? Dec : month-1;
    sign = -1;
   }
   dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2]-monthlyValues[month])/(monthdays[month])*(mday-15.);
  }
 }
}

canonical_original_code:
void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]){
 unsigned int doy; unsigned int mday; unsigned int month; unsigned int month2 = NoMonth;
 double sign = 1.;
 for (doy = 1; doy <= 366; doy++){
  mday = doy2mday(doy);
  month = doy2month(doy);
  if(mday == 15) {
   dailyValues[doy] = monthlyValues[month];
  } else {
   if(mday >= 15) {
    month2 = (month == Dec) ? Jan : month+1;
    sign = 1;
   } else {
    month2 = (month == Jan) ? Dec : month-1;
    sign = -1;
   }
   dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2]-monthlyValues[month])/(monthdays[month])*(mday-15.);
  }
 }
}

variable_types:
monthlyValues
double []

variable_types:
dailyValues
double []

variable_types:
month
unsigned int

variable_types: sign double


RandSeed

decompiled_code:
void __fastcall RandSeed(__int64 a1)
{
time_t v1; // rax

if ( a1 )
{
randseed = (int)(((int)a1 >> 31) - (((int)a1 >> 31) ^ a1));
}
else
{
v1 = time(0LL);
randseed = v1;
if ( v1 == -1 )
{
fwrite("ERROR: RandSeed(0) called, but time() not available\n", 1uLL, 0x34uLL, stderr);
exit(-1);
}
randseed = -v1;
}
srand(abs32(randseed));
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
time_t v1; // rax

if ( a1 )
{
randseed = (int)(((int)a1 >> 31) - (((int)a1 >> 31) ^ a1));
}
else
{
v1 = time(0LL);
randseed = v1;
if ( v1 == -1 )
{
fwrite("ERROR: RandSeed(0) called, but time() not available\n", 1uLL, 0x34uLL, stderr);
exit(-1);
}
randseed = -v1;
}
srand(abs32(randseed));
}

original_code:
void RandSeed( signed long seed ) {
  if (seed == 0L) {
    _randseed = ((long) time(((void *)0)));
    if (_randseed == -1) {
      fprintf(stderr, "ERROR: RandSeed(0) called, "
                      "but time() not available\n");
      exit(-1);
    }
    _randseed *= -1;
  } else {
    _randseed = abs(seed) * -1;
  }
    srand(abs(_randseed));
}

canonical_original_code:
void RandSeed(long seed) {
  if (seed == 0L) {
    _randseed = ((long) time(((void *)0)));
    if (_randseed == -1) {
      fprintf(stderr, "ERROR: RandSeed(0) called, "
                      "but time() not available\n");
      exit(-1);
    }
    _randseed *= -1;
  } else {
    _randseed = abs(seed) * -1;
  }
    srand(abs(_randseed));
}

variable_types: seed long


decompiled_code:
double RandUni_fast()
{
int v0; // ebx
int *v1; // rbx
double v2; // rbx

if ( first_time_16 )
{
first_time_16 = 0;
v0 = 97;
do
{
rand();
--v0;
}
while ( v0 );
v1 = (int *)&bucket_15;
do
*v1++ = rand();
while ( v1 != (int *)3076 );
rand();
}
v2 = (double)dword_C00 / 2147483647.0;
*(double *)&y_13 = v2;
dword_C00 = rand();
return v2;
}

canonical_decompiled_code:
double func1()
{
int v0; // ebx
int *v1; // rbx
double v2; // rbx

if ( first_time_16 )
{
first_time_16 = 0;
v0 = 97;
do
{
rand();
--v0;
}
while ( v0 );
v1 = (int *)&bucket_15;
do
*v1++ = rand();
while ( v1 != (int *)3076 );
rand();
}
v2 = (double)dword_C00 / 2147483647.0;
*(double *)&y_13 = v2;
dword_C00 = rand();
return v2;
}

original_code:
double RandUni_fast( void ) {
 static short first_time = 1;
 static int bucket[97];
 static double y, rmax = 2147483647;
 int i,j;
  if (first_time ) {
    first_time = 0;
    for(j=0; j<97; j++) rand();
    for(j=0; j<97; j++) bucket[j] = rand();
    y = rand()/rmax;
  }
  i = 1 + (int) ((double)97 * y);
  i = (((97 -1) > ((((i) < (0)) ? (i) : (0)))) ? (97 -1) : ((((i) < (0)) ? (i) : (0))));
  y = bucket[i]/rmax;
  bucket[i] = rand();
  return y;
}

canonical_original_code:
double RandUni_fast() {
 static short first_time = 1;
 static int bucket[97];
 static double y; static double rmax = 2147483647;
 int i; int j;
  if (first_time ) {
    first_time = 0;
    for(j=0; j<97; j++) rand();
    for(j=0; j<97; j++) bucket[j] = rand();
    y = rand()/rmax;
  }
  i = 1 + (int) ((double)97 * y);
  i = (((97 -1) > ((((i) < (0)) ? (i) : (0)))) ? (97 -1) : ((((i) < (0)) ? (i) : (0))));
  y = bucket[i]/rmax;
  bucket[i] = rand();
  return y;
}

variable_types:
bucket
int [97]

variable_types: y double

variable_types: rmax double

variable_types: i int

variable_types: j int


RandUniRange

decompiled_code:
__int64 __fastcall RandUniRange(__int64 a1, __int64 a2)
{
int v2; // ebx
__int64 result; // rax
int v4; // ebp

v2 = a1;
result = (unsigned int)a1;
if ( a1 != a2 )
{
v4 = a2;
if ( a1 <= a2 )
{
v4 = a1;
v2 = a2;
}
result = (unsigned int)(v4 + (int)((double)(v2 - v4 + 1) * RandUni_fast()));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // ebx
__int64 result; // rax
int v4; // ebp

v2 = a1;
result = (unsigned int)a1;
if ( a1 != a2 )
{
v4 = a2;
if ( a1 <= a2 )
{
v4 = a1;
v2 = a2;
}
result = (unsigned int)(v4 + (int)((double)(v2 - v4 + 1) * func1()));
}
return result;
}

original_code:
int RandUniRange( const long first, const long last) {
  long f, l, r;
  if (first==last)
    return first;
  if (first > last) { l = first; f = last; }
  else { f = first; l = last; }
  r = l - f + 1;
  return (long)(RandUni_fast() * r) + f;
}

canonical_original_code:
int RandUniRange(long first, long last) {
  long f; long l; long r;
  if (first==last)
    return first;
  if (first > last) { l = first; f = last; }
  else { f = first; l = last; }
  r = l - f + 1;
  return (long)(RandUni_fast() * r) + f;
}

variable_types: first long

variable_types: last long

variable_types: f long

variable_types: l long

variable_types: r long


RandUniList

decompiled_code:
__int64 __fastcall RandUniList(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
__int64 v4; // rbp
__int64 v5; // rax
__int64 v6; // rbx
__int64 v9; // r12
__int64 v10; // rdx
__int64 i; // rbx
__int64 result; // rax
int v13; // eax
__int64 v14; // rdx
_QWORD *v15; // rax
__int64 j; // rax

v4 = a3 - a2;
v5 = a3 - a2 + 1;
if ( v5 < a1 || (v6 = a2, v5 <= 0) )
{
fwrite("Programmer error in RandUniList: count > range || range <= 0\n", 1uLL, 0x3DuLL, stderr);
exit(-1);
}
if ( v5 == a1 )
{
result = 0LL;
do
{
a4[result] = a2 + result;
++result;
}
while ( a1 > result );
}
else if ( a1 <= 2 )
{
result = (int)RandUniRange(a2, a3);
*a4 = (int)result;
if ( a1 == 2 )
{
do
{
result = (int)RandUniRange(a2, a3);
a4[1] = (int)result;
}
while ( (int)result == *a4 );
}
}
else
{
v9 = Mem_Malloc(8 * v5, "RandUniList");
if ( a3 >= a2 )
{
do
{
v10 = v6++;
*(_QWORD *)(v9 - 8 * a2 + 8 * v6 - 8) = v10;
}
while ( v6 != a3 + 1 );
}
for ( i = 0LL; ; ++i )
{
do
v13 = RandUniRange(0LL, v4);
while ( v13 == i );
v14 = *(_QWORD *)(v9 + 8 * i);
v15 = (_QWORD *)(v9 + 8LL * v13);
*(_QWORD *)(v9 + 8 * i) = *v15;
*v15 = v14;
if ( v4 <= i )
break;
}
for ( j = 0LL; j != a1; ++j )
a4[j] = *(_QWORD *)(v9 + 8 * j);
result = Mem_Free(v9);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
__int64 v4; // rbp
__int64 v5; // rax
__int64 v6; // rbx
__int64 v9; // r12
__int64 v10; // rdx
__int64 i; // rbx
__int64 result; // rax
int v13; // eax
__int64 v14; // rdx
_QWORD *v15; // rax
__int64 j; // rax

v4 = a3 - a2;
v5 = a3 - a2 + 1;
if ( v5 < a1 || (v6 = a2, v5 <= 0) )
{
fwrite("Programmer error in RandUniList: count > range || range <= 0\n", 1uLL, 0x3DuLL, stderr);
exit(-1);
}
if ( v5 == a1 )
{
result = 0LL;
do
{
a4[result] = a2 + result;
++result;
}
while ( a1 > result );
}
else if ( a1 <= 2 )
{
result = (int)func3(a2, a3);
*a4 = (int)result;
if ( a1 == 2 )
{
do
{
result = (int)func3(a2, a3);
a4[1] = (int)result;
}
while ( (int)result == *a4 );
}
}
else
{
v9 = Mem_Malloc(8 * v5, "RandUniList");
if ( a3 >= a2 )
{
do
{
v10 = v6++;
*(_QWORD *)(v9 - 8 * a2 + 8 * v6 - 8) = v10;
}
while ( v6 != a3 + 1 );
}
for ( i = 0LL; ; ++i )
{
do
v13 = func3(0LL, v4);
while ( v13 == i );
v14 = *(_QWORD *)(v9 + 8 * i);
v15 = (_QWORD *)(v9 + 8LL * v13);
*(_QWORD *)(v9 + 8 * i) = *v15;
*v15 = v14;
if ( v4 <= i )
break;
}
for ( j = 0LL; j != a1; ++j )
a4[j] = *(_QWORD *)(v9 + 8 * j);
result = Mem_Free(v9);
}
return result;
}

original_code:
void RandUniList( long count, long first,
                  long last, RandListType list[]) {
  long i, j, c,
      range, *klist;
  range = last - first +1;
  if (count > range || range <= 0) {
    fprintf(stderr, "Programmer error in RandUniList: "
            "count > range || range <= 0\n");
    exit (-1);
  }
  if (count == range) {
    for(i=0; i< count; i++) list[i] = first + i;
    return;
  }
  if (count <= 2) {
    list[0] = (long) RandUniRange(first, last);
    if (count == 2)
       while ( (list[1] = RandUniRange(first, last)) == list[0]);
    return;
  }
  klist = (long *) Mem_Malloc(sizeof(long) * range, "RandUniList");
  for( i=0, j = first; j <= last; klist[i++] = j++);
  for( i=0; i < range; i++) {
    while( (j=RandUniRange(0, range-1) ) == i);
    c = klist[i];
    klist[i] = klist[j];
    klist[j] = c;
  }
  for( i=0; i< count; i++) {
    list[i] = klist[i];
  }
  Mem_Free(klist);
}

canonical_original_code:
void RandUniList(long count, long first, long last, long list[]) {
  long i; long j; long c; long range; long *klist;
  range = last - first +1;
  if (count > range || range <= 0) {
    fprintf(stderr, "Programmer error in RandUniList: "
            "count > range || range <= 0\n");
    exit (-1);
  }
  if (count == range) {
    for(i=0; i< count; i++) list[i] = first + i;
    return;
  }
  if (count <= 2) {
    list[0] = (long) RandUniRange(first, last);
    if (count == 2)
       while ( (list[1] = RandUniRange(first, last)) == list[0]);
    return;
  }
  klist = (long *) Mem_Malloc(sizeof(long) * range, "RandUniList");
  for( i=0, j = first; j <= last; klist[i++] = j++);
  for( i=0; i < range; i++) {
    while( (j=RandUniRange(0, range-1) ) == i);
    c = klist[i];
    klist[i] = klist[j];
    klist[j] = c;
  }
  for( i=0; i< count; i++) {
    list[i] = klist[i];
  }
  Mem_Free(klist);
}

variable_types: count long

variable_types:
list
long []

variable_types: i long

variable_types: j long

variable_types: c long

variable_types: range long

variable_types:
klist
long *


RandNorm

decompiled_code:
double __fastcall RandNorm(double a1, double a2)
{
double v4; // xmm0_8
double v5; // r13
double v6; // xmm0_8
double v7; // rbx
double v8; // xmm1_8
double v9; // xmm1_8
double v10; // xmm1_8

if ( set_6 )
{
gasdev_0 = gset_1;
set_6 = 0;
}
else
{
do
{
v4 = RandUni_fast();
v5 = v4 + v4 - 1.0;
*(double *)&v1_5 = v5;
v6 = RandUni_fast();
v7 = v6 + v6 - 1.0;
v2_4 = *(_QWORD *)&v7;
v8 = v5 * v5 + v7 * v7;
r_3 = *(_QWORD *)&v8;
}
while ( v8 >= 1.0 );
v9 = log(v8) * -2.0 / v8;
if ( v9 < 0.0 )
v10 = sqrt(v9);
else
v10 = sqrt(v9);
*(double *)&gset_1 = v5 * v10;
*(double *)&gasdev_0 = v7 * v10;
set_6 = 1;
}
return a2 * *(double *)&gasdev_0 + a1;
}

canonical_decompiled_code:
double __fastcall func5(double a1, double a2)
{
double v4; // xmm0_8
double v5; // r13
double v6; // xmm0_8
double v7; // rbx
double v8; // xmm1_8
double v9; // xmm1_8
double v10; // xmm1_8

if ( set_6 )
{
gasdev_0 = gset_1;
set_6 = 0;
}
else
{
do
{
v4 = func1();
v5 = v4 + v4 - 1.0;
*(double *)&v1_5 = v5;
v6 = func1();
v7 = v6 + v6 - 1.0;
v2_4 = *(_QWORD *)&v7;
v8 = v5 * v5 + v7 * v7;
r_3 = *(_QWORD *)&v8;
}
while ( v8 >= 1.0 );
v9 = log(v8) * -2.0 / v8;
if ( v9 < 0.0 )
v10 = sqrt(v9);
else
v10 = sqrt(v9);
*(double *)&gset_1 = v5 * v10;
*(double *)&gasdev_0 = v7 * v10;
set_6 = 1;
}
return a2 * *(double *)&gasdev_0 + a1;
}

original_code:
double RandNorm( double mean, double stddev) {
  static short set = 0;
  static double v1, v2, r, fac, gset, gasdev;
  if (!set) {
    do {
      v1 = 2.0 * RandUni_fast() -1.0;
      v2 = 2.0 * RandUni_fast() -1.0;
      r = v1*v1 + v2*v2;
    } while( r >= 1.0 );
    fac = ((sizeof(-2.0 *log(r)/r)==sizeof(float)) ? sqrtf(-2.0 *log(r)/r) : sqrt(-2.0 *log(r)/r));
    gset = v1 * fac;
    gasdev = v2 * fac;
    set = 1;
  } else {
    gasdev = gset;
    set = 0;
  }
  return mean + gasdev * stddev;
}

canonical_original_code:
double RandNorm(double mean, double stddev) {
  static short set = 0;
  static double v1; static double v2; static double r; static double fac; static double gset; static double gasdev;
  if (!set) {
    do {
      v1 = 2.0 * RandUni_fast() -1.0;
      v2 = 2.0 * RandUni_fast() -1.0;
      r = v1*v1 + v2*v2;
    } while( r >= 1.0 );
    fac = ((sizeof(-2.0 *log(r)/r)==sizeof(float)) ? sqrtf(-2.0 *log(r)/r) : sqrt(-2.0 *log(r)/r));
    gset = v1 * fac;
    gasdev = v2 * fac;
    set = 1;
  } else {
    gasdev = gset;
    set = 0;
  }
  return mean + gasdev * stddev;
}

variable_types: mean double

variable_types: stddev double

variable_types: set short

variable_types: r double

variable_types: fac double

variable_types: gset double

variable_types: gasdev double