decompiled_code:
__int64 __fastcall is_ident_char(unsigned __int8 a1)
{
return (a1 == 95) | (unsigned __int8)(((*_ctype_b_loc())[a1] & 0xC00) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
return (a1 == 95) | (unsigned __int8)(((*_ctype_b_loc())[a1] & 0xC00) != 0);
}

original_code:
static inline int is_ident_char(uint8_t ch)
{
    return ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISalpha) || ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISdigit) || ch == '_';
}

canonical_original_code:
static inline int is_ident_char(unsigned char ch)
{
    return ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISalpha) || ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISdigit) || ch == '_';
}

variable_types:
ch
unsigned char


repo: ajbonkoski/lcm-spy-lite

decompiled_code:
__int64 __fastcall is_first_ident_char(unsigned __int8 a1)
{
return ((a1 == 95) | ((*_ctype_b_loc())[a1] >> 10)) & 1;
}

original_code:
static inline int is_first_ident_char(uint8_t ch)
{
    return ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISalpha) || ch == '_';
}

canonical_original_code:
static inline int is_first_ident_char(unsigned char ch)
{
    return ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISalpha) || ch == '_';
}

return_type: int


decompiled_code:
__suseconds_t timestamp_now()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return v1.tv_usec + 1000000 * v1.tv_sec;
}

canonical_decompiled_code:
__suseconds_t func0()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return v1.tv_usec + 1000000 * v1.tv_sec;
}

original_code:
uint64_t timestamp_now(void)
{
    struct timeval tv;
    gettimeofday(&tv, ((void *)0));
    return (uint64_t) tv.tv_sec * 1000000 + tv.tv_usec;
}

canonical_original_code:
unsigned long timestamp_now()
{
    struct timeval tv;
    gettimeofday(&tv, ((void *)0));
    return (unsigned long) tv.tv_sec * 1000000 + tv.tv_usec;
}

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

return_type: unsigned long


repo: ajbonkoski/pianoserver

decompiled_code:
char *__fastcall get_input(int a1)
{
int v1; // eax
int v2; // edx
int v3; // eax

v1 = read(a1, buf, 0xFFuLL);
if ( !v1 )
return 0LL;
v2 = v1;
v3 = v1 - 1;
if ( buf[v3] == 10 )
v2 = v3;
buf[v2] = 0;
return buf;
}

canonical_decompiled_code:
char *__fastcall func7(int a1)
{
int v1; // eax
int v2; // edx
int v3; // eax

v1 = read(a1, buf, 0xFFuLL);
if ( !v1 )
return 0LL;
v2 = v1;
v3 = v1 - 1;
if ( buf[v3] == 10 )
v2 = v3;
buf[v2] = 0;
return buf;
}

original_code:
char *get_input(int fd){
  int numread = read(fd, buf, 255);
  if(!numread)
    return ((void *)0);
  if(buf[numread-1] == '\n')
    --numread;
  buf[numread] = 0;
  return buf;
}

variable_types: fd int

variable_types: numread int

return_type: char *


decompiled_code:
void __fastcall forward_pianobar_output(int fd)
{
int v1; // eax
_BYTE v2[264]; // [rsp+0h] [rbp-108h] BYREF

if ( pianobar_pid )
{
while ( 1 )
{
v1 = read(pianobar_stdout, v2, 0xFFuLL);
if ( v1 <= 0 )
break;
write(fd, v2, v1);
}
}
}

canonical_decompiled_code:
void __fastcall func8(int fd)
{
int v1; // eax
_BYTE v2[264]; // [rsp+0h] [rbp-108h] BYREF

if ( pianobar_pid )
{
while ( 1 )
{
v1 = read(pianobar_stdout, v2, 0xFFuLL);
if ( v1 <= 0 )
break;
write(fd, v2, v1);
}
}
}

original_code:
void forward_pianobar_output(int fd_out){
  char buf[255 +1];
  int numread;
  if(!(pianobar_pid))
    return;
  do {
    numread = read(pianobar_stdout, buf, 255);
    if(numread > 0)
      write(fd_out, buf, numread);
  } while(numread > 0);
}

canonical_original_code:
void forward_pianobar_output(int fd_out){
  char buf[-1];
  int numread;
  if(!(pianobar_pid))
    return;
  do {
    numread = read(pianobar_stdout, buf, 255);
    if(numread > 0)
      write(fd_out, buf, numread);
  } while(numread > 0);
}

variable_types:
buf
char [-1]

return_type: void


decompiled_code:
int start_pianobar()
{
__pid_t v0; // eax
int result; // eax
int fd; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+4h] [rbp-14h]
int pipedes[2]; // [rsp+8h] [rbp-10h] BYREF

if ( pianobar_pid )
return fwrite("Error: pianobar is already running...\n", 1uLL, 0x26uLL, stderr);
puts("starting pianobar...");
if ( pipe(pipedes) == -1 )
{
fwrite("Pipe Failed.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
if ( pipe(&fd) == -1 )
{
fwrite("Pipe Failed.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
v0 = fork();
pianobar_pid = v0;
if ( v0 == -1 )
{
fwrite("Fork Failed.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
if ( v0 )
{
close(pipedes[0]);
close(v3);
pianobar_stdin = pipedes[1];
pianobar_stdout = fd;
result = fcntl(fd, 4, 2048LL);
}
else
{
close(pipedes[1]);
close(fd);
close(0);
dup(pipedes[0]);
close(1);
dup(v3);
result = execlp("pianobar", "pianobar", 0LL);
}
return result;
}

canonical_decompiled_code:
int func9()
{
__pid_t v0; // eax
int result; // eax
int fd; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+4h] [rbp-14h]
int pipedes[2]; // [rsp+8h] [rbp-10h] BYREF

if ( pianobar_pid )
return fwrite("Error: pianobar is already running...\n", 1uLL, 0x26uLL, stderr);
puts("starting pianobar...");
if ( pipe(pipedes) == -1 )
{
fwrite("Pipe Failed.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
if ( pipe(&fd) == -1 )
{
fwrite("Pipe Failed.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
v0 = fork();
pianobar_pid = v0;
if ( v0 == -1 )
{
fwrite("Fork Failed.\n", 1uLL, 0xDuLL, stderr);
exit(1);
}
if ( v0 )
{
close(pipedes[0]);
close(v3);
pianobar_stdin = pipedes[1];
pianobar_stdout = fd;
result = fcntl(fd, 4, 2048LL);
}
else
{
close(pipedes[1]);
close(fd);
close(0);
dup(pipedes[0]);
close(1);
dup(v3);
result = execlp("pianobar", "pianobar", 0LL);
}
return result;
}

original_code:
void start_pianobar(){
  if((pianobar_pid)){
    fprintf(stderr, "Error: pianobar is already running...\n");
    return;
  }
  printf("starting pianobar...\n");
  int in[2];
  int out[2];
  if(pipe(in) == -1){
    fprintf(stderr, "Pipe Failed.\n");
    exit(1);
  }
  if(pipe(out) == -1){
    fprintf(stderr, "Pipe Failed.\n");
    exit(1);
  }
  pianobar_pid = fork();
  if(pianobar_pid == -1){
    fprintf(stderr, "Fork Failed.\n");
    exit(1);
  }
  if(pianobar_pid == 0){
    close(in[1]);
    close(out[0]);
    close(0);
    dup(in[0]);
    close(1);
    dup(out[1]);
    execlp("pianobar", "pianobar", ((void *)0));
  }else{
    close(in[0]);
    close(out[1]);
    pianobar_stdin = in[1];
    pianobar_stdout = out[0];
    fcntl(pianobar_stdout, 4, 04000);
  }
}

variable_types:
in
int [2]

variable_types:
out
int [2]


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

puts("stopping pianobar...");
if ( !pianobar_pid )
return fwrite("Error: pianobar is not running...\n", 1uLL, 0x22uLL, stderr);
result = kill(pianobar_pid, 9);
pianobar_pid = 0;
return result;
}

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

puts("stopping pianobar...");
if ( !pianobar_pid )
return fwrite("Error: pianobar is not running...\n", 1uLL, 0x22uLL, stderr);
result = kill(pianobar_pid, 9);
pianobar_pid = 0;
return result;
}

original_code:
void stop_pianobar(){
  printf("stopping pianobar...\n");
  if(!(pianobar_pid)){
    fprintf(stderr, "Error: pianobar is not running...\n");
  }else{
    kill(pianobar_pid, 9);
    pianobar_pid = 0;
  }
}


decompiled_code:
int restart_pianobar()
{
puts("restarting pianobar...");
stop_pianobar();
return start_pianobar();
}

canonical_decompiled_code:
int func11()
{
puts("restarting pianobar...");
func10();
return func9();
}

original_code:
void restart_pianobar(){
    printf("restarting pianobar...\n");
    stop_pianobar();
    start_pianobar();
}


decompiled_code:
int __fastcall handle_daemon_control(const char *a1)
{
if ( !strcmp(a1, "start") || !strcmp(a1, "g") )
return start_pianobar();
if ( !strcmp(a1, "restart") || !strcmp(a1, "re") )
return restart_pianobar();
if ( !strcmp(a1, "stop") || !strcmp(a1, "q") )
return stop_pianobar();
return fprintf(stderr, "Error: could not recognize daemon control string: '%s'\n", a1);
}

canonical_decompiled_code:
int __fastcall func12(const char *a1)
{
if ( !strcmp(a1, "start") || !strcmp(a1, "g") )
return func9();
if ( !strcmp(a1, "restart") || !strcmp(a1, "re") )
return func11();
if ( !strcmp(a1, "stop") || !strcmp(a1, "q") )
return func10();
return fprintf(stderr, "Error: could not recognize daemon control string: '%s'\n", a1);
}

original_code:
void handle_daemon_control(char *in){
  if(strcmp(in, "start") == 0 || strcmp(in, "g") == 0)
    start_pianobar();
  else if(strcmp(in, "restart") == 0 || strcmp(in, "re") == 0)
    restart_pianobar();
  else if(strcmp(in, "stop") == 0 || strcmp(in, "q") == 0)
    stop_pianobar();
  else
    fprintf(stderr, "Error: could not recognize daemon control string: '%s'\n", in);
}

variable_types:
in
char *


decompiled_code:
int __fastcall handle_piano_control(void *buf)
{
size_t v1; // rax

if ( !strcmp((const char *)buf, "q") )
return stop_pianobar();
if ( !pianobar_pid )
return fwrite("Error: pianobar is not running... command will be dropped..\n", 1uLL, 0x3CuLL, stderr);
v1 = strlen((const char *)buf);
return write(pianobar_stdin, buf, v1);
}

canonical_decompiled_code:
int __fastcall func13(void *buf)
{
size_t v1; // rax

if ( !strcmp((const char *)buf, "q") )
return func10();
if ( !pianobar_pid )
return fwrite("Error: pianobar is not running... command will be dropped..\n", 1uLL, 0x3CuLL, stderr);
v1 = strlen((const char *)buf);
return write(pianobar_stdin, buf, v1);
}

original_code:
void handle_piano_control(char *in){
  if(strcmp(in, "q") == 0){
    stop_pianobar();
    return;
  }
  if(!(pianobar_pid)){
    fprintf(stderr, "Error: pianobar is not running... command will be dropped..\n");
    return;
  }
  write(pianobar_stdin, in, strlen(in));
}


decompiled_code:
int __fastcall handle_input(void *buf)
{
int result; // eax

result = strlen((const char *)buf);
if ( result )
{
if ( *(_BYTE *)buf == 37 )
result = handle_daemon_control((const char *)buf + 1);
else
result = handle_piano_control(buf);
}
return result;
}

canonical_decompiled_code:
int __fastcall func14(void *buf)
{
int result; // eax

result = strlen((const char *)buf);
if ( result )
{
if ( *(_BYTE *)buf == 37 )
result = func12((const char *)buf + 1);
else
result = func13(buf);
}
return result;
}

original_code:
void handle_input(char *in){
  int len = strlen(in);
  if(!len) return;
  if(in[0] == '%')
    handle_daemon_control(in+1);
  else
    handle_piano_control(in);
}

variable_types: len int


decompiled_code:
void __fastcall str_toupper(char *a1)
{
char v1; // bl
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( *a1 )
{
v2 = a1;
v3 = __ctype_toupper_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( *v2 );
}
}

canonical_decompiled_code:
void __fastcall func15(char *a1)
{
char v1; // bl
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( *a1 )
{
v2 = a1;
v3 = __ctype_toupper_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( *v2 );
}
}

original_code:
void str_toupper(char *str){
  for (; *str; ++str)
      *str = toupper(*str);
}

variable_types:
str
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // ebp
char *v5; // rdi

if ( mkfifo("/tmp/piano_pipe_in", 0x1B6u) != -1 || *__errno_location() == 17 )
{
v3 = open("/tmp/piano_pipe_in", 2048);
if ( v3 != -1 )
{
v4 = creat("/tmp/piano_output", 0x1A4u);
if ( v4 == -1 )
{
fwrite("Error: Could not open named input pipe\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
while ( 1 )
{
forward_pianobar_output(v4);
v5 = get_input(v3);
if ( v5 )
handle_input(v5);
usleep(0x186A0u);
}
}
fwrite("Error: Could not open named input pipe\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
perror("Error creating named pipe");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // ebp
char *v5; // rdi

if ( mkfifo("/tmp/piano_pipe_in", 0x1B6u) != -1 || *__errno_location() == 17 )
{
v3 = open("/tmp/piano_pipe_in", 2048);
if ( v3 != -1 )
{
v4 = creat("/tmp/piano_output", 0x1A4u);
if ( v4 == -1 )
{
fwrite("Error: Could not open named input pipe\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
while ( 1 )
{
func8(v4);
v5 = func7(v3);
if ( v5 )
func14(v5);
usleep(0x186A0u);
}
}
fwrite("Error: Could not open named input pipe\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
perror("Error creating named pipe");
exit(1);
}

original_code:
int main(int argc, char *argv[]){
  int ret_val;
  ret_val = mkfifo("/tmp/piano_pipe_in", 0666);
  if((ret_val == -1) && ((*__errno_location ()) != 17)){
    perror("Error creating named pipe");
    exit(1);
  }
  int fd_in, fd_out;
  if((fd_in= open("/tmp/piano_pipe_in", 00 | 04000)) == -1){
    fprintf(stderr, "Error: Could not open named input pipe\n");
    exit(1);
  }
  if((fd_out = creat("/tmp/piano_output", 0644)) == -1){
    fprintf(stderr, "Error: Could not open named input pipe\n");
    exit(1);
  }
  while(1){
    forward_pianobar_output(fd_out);
    char *buf = get_input(fd_in);
    if(buf){
      ;
      handle_input(buf);
    }
    usleep(100000);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  int ret_val;
  ret_val = mkfifo("/tmp/piano_pipe_in", 0666);
  if((ret_val == -1) && ((*__errno_location ()) != 17)){
    perror("Error creating named pipe");
    exit(1);
  }
  int fd_in; int fd_out;
  if((fd_in= open("/tmp/piano_pipe_in", 00 | 04000)) == -1){
    fprintf(stderr, "Error: Could not open named input pipe\n");
    exit(1);
  }
  if((fd_out = creat("/tmp/piano_output", 0644)) == -1){
    fprintf(stderr, "Error: Could not open named input pipe\n");
    exit(1);
  }
  while(1){
    forward_pianobar_output(fd_out);
    char *buf = get_input(fd_in);
    if(buf){
      ;
      handle_input(buf);
    }
    usleep(100000);
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
buf
char *


repo: ajdawson/parallel

decompiled_code:
void __fastcall dealloc_array2d(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void dealloc_array2d(double **array)
{
    free(array[0]);
    free(array);
}

variable_types:
array
double **


decompiled_code:
int __fastcall print_double_array(int a1, int a2, _QWORD *a3)
{
_QWORD *v3; // rbp
__int64 v4; // r14
__int64 v5; // rbx
int result; // eax

if ( a1 > 0 )
{
v3 = a3;
v4 = (__int64)&a3[(unsigned int)(a1 - 1) + 1];
do
{
v5 = 0LL;
if ( a2 > 0 )
{
do
{
printf("%4.1f ", *(double *)(*v3 + v5));
v5 += 8LL;
}
while ( v5 != 8LL * (unsigned int)(a2 - 1) + 8 );
}
result = putchar(10);
++v3;
}
while ( v3 != (_QWORD *)v4 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int a1, int a2, _QWORD *a3)
{
_QWORD *v3; // rbp
__int64 v4; // r14
__int64 v5; // rbx
int result; // eax

if ( a1 > 0 )
{
v3 = a3;
v4 = (__int64)&a3[(unsigned int)(a1 - 1) + 1];
do
{
v5 = 0LL;
if ( a2 > 0 )
{
do
{
printf("%4.1f ", *(double *)(*v3 + v5));
v5 += 8LL;
}
while ( v5 != 8LL * (unsigned int)(a2 - 1) + 8 );
}
result = putchar(10);
++v3;
}
while ( v3 != (_QWORD *)v4 );
}
return result;
}

original_code:
void print_double_array(int nrows, int ncols, double **array)
{
    int i, j;
    for (i=0; i<nrows; i++) {
        for (j=0; j<ncols; j++) {
            printf("%4.1f ", array[i][j]);
        }
        printf("\n");
    }
    return;
}

canonical_original_code:
void print_double_array(int nrows, int ncols, double **array)
{
    int i; int j;
    for (i=0; i<nrows; i++) {
        for (j=0; j<ncols; j++) {
            printf("%4.1f ", array[i][j]);
        }
        printf("\n");
    }
    return;
}

variable_types: nrows int

variable_types: ncols int

variable_types: i int

variable_types: j int


repo: ajdiaz/libcalby

variable_types: struct _cal_datetime_s { long year; long mon; long day; long hour; long min; long sec; long off; } *

variable_types: m long

variable_types: y long

variable_types: d long


decompiled_code:
int __fastcall cal_leapsecs_load(int fd)
{
int result; // eax
__off_t v2; // r12
char *v3; // rax
char *v4; // rbp
int v5; // eax
unsigned __int64 v6; // rdx
__int64 v7; // rax
unsigned __int8 *v8; // rbx
__int64 v9; // r12
void *v10; // rdi
unsigned __int64 v11; // [rsp+8h] [rbp-C0h] BYREF
struct stat buf; // [rsp+10h] [rbp-B8h] BYREF

result = fstat(fd, &buf);
if ( result != -1 )
{
v2 = buf.st_size;
v3 = (char *)malloc(buf.st_size);
v4 = v3;
if ( v3 )
{
v5 = read(fd, v3, v2);
if ( v5 == buf.st_size )
{
v6 = (unsigned __int64)v5 >> 3;
v7 = (__int64)v5 >> 3;
cal_leapsecs[0] = v7;
if ( (int)v6 - 1 >= 0 )
{
v8 = (unsigned __int8 *)&v4[8 * (int)v6 - 8];
v9 = (__int64)&v4[8 * v7 - 16 + -8 * (unsigned int)(v6 - 1)];
do
{
cal_tai_unpack(v8, &v11);
*(_QWORD *)v8 = v11;
v8 -= 8;
}
while ( v8 != (unsigned __int8 *)v9 );
}
v10 = (void *)cal_leapsecs[1];
if ( v10 )
free(v10);
cal_leapsecs[1] = v4;
result = cal_leapsecs[0];
}
else
{
free(v4);
result = -1;
}
}
else
{
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd)
{
int result; // eax
__off_t v2; // r12
char *v3; // rax
char *v4; // rbp
int v5; // eax
unsigned __int64 v6; // rdx
__int64 v7; // rax
unsigned __int8 *v8; // rbx
__int64 v9; // r12
void *v10; // rdi
unsigned __int64 v11; // [rsp+8h] [rbp-C0h] BYREF
struct stat buf; // [rsp+10h] [rbp-B8h] BYREF

result = fstat(fd, &buf);
if ( result != -1 )
{
v2 = buf.st_size;
v3 = (char *)malloc(buf.st_size);
v4 = v3;
if ( v3 )
{
v5 = read(fd, v3, v2);
if ( v5 == buf.st_size )
{
v6 = (unsigned __int64)v5 >> 3;
v7 = (__int64)v5 >> 3;
cal_leapsecs[0] = v7;
if ( (int)v6 - 1 >= 0 )
{
v8 = (unsigned __int8 *)&v4[8 * (int)v6 - 8];
v9 = (__int64)&v4[8 * v7 - 16 + -8 * (unsigned int)(v6 - 1)];
do
{
func4(v8, &v11);
*(_QWORD *)v8 = v11;
v8 -= 8;
}
while ( v8 != (unsigned __int8 *)v9 );
}
v10 = (void *)cal_leapsecs[1];
if ( v10 )
free(v10);
cal_leapsecs[1] = v4;
result = cal_leapsecs[0];
}
else
{
free(v4);
result = -1;
}
}
else
{
result = -1;
}
}
return result;
}

original_code:
int
cal_leapsecs_load(int fd)
{
    struct stat st;
    _uint64_t *t, u;
    int n;
    if (fstat(fd,&st) == -1) return -1;
    if ((t = (_uint64_t *) malloc (st.st_size)) == ((void *)0))
        return -1;
    if ((n = read(fd,(char *)t,st.st_size)) != st.st_size)
    {
        free(t);
        return -1;
    }
    n /= sizeof(_uint64_t);
    cal_leapsecs.count = n;
    while (--n >= 0)
    {
        cal_tai_unpack((char *)&t[n],&u);
        t[n] = u;
    }
    if (cal_leapsecs.list != ((void *)0))
        free(cal_leapsecs.list);
    cal_leapsecs.list = t;
    return (int)cal_leapsecs.count;
}

canonical_original_code:
int cal_leapsecs_load(int fd)
{
    struct stat st;
    unsigned long long *t; unsigned long long u;
    int n;
    if (fstat(fd,&st) == -1) return -1;
    if ((t = (unsigned long long *) malloc (st.st_size)) == ((void *)0))
        return -1;
    if ((n = read(fd,(char *)t,st.st_size)) != st.st_size)
    {
        free(t);
        return -1;
    }
    n /= sizeof(_uint64_t);
    cal_leapsecs.count = n;
    while (--n >= 0)
    {
        cal_tai_unpack((char *)&t[n],&u);
        t[n] = u;
    }
    if (cal_leapsecs.list != ((void *)0))
        free(cal_leapsecs.list);
    cal_leapsecs.list = t;
    return (int)cal_leapsecs.count;
}

variable_types: fd int

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:
t
unsigned long long *

variable_types:
u
unsigned long long

variable_types: n int

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


decompiled_code:
__int64 cal_leapsecs_init()
{
unsigned int v0; // ebx
int v2; // eax
int v3; // ebp

v0 = cal_leapsecs[0];
if ( !cal_leapsecs[0] )
{
v2 = open("/etc/leapsecs.dat", 0);
v3 = v2;
if ( v2 == -1 )
{
v0 = -1;
}
else
{
v0 = cal_leapsecs_load(v2);
close(v3);
}
}
return v0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // ebx
int v2; // eax
int v3; // ebp

v0 = cal_leapsecs[0];
if ( !cal_leapsecs[0] )
{
v2 = open("/etc/leapsecs.dat", 0);
v3 = v2;
if ( v2 == -1 )
{
v0 = -1;
}
else
{
v0 = func6(v2);
close(v3);
}
}
return v0;
}

original_code:
int
cal_leapsecs_init(void)
{
    int ret = -1;
    int fd;
    if (cal_leapsecs.count)
        return cal_leapsecs.count;
    if ((fd = open("/etc/leapsecs.dat",00)) == -1)
        return -1;
    ret = cal_leapsecs_load(fd);
    close(fd);
    return ret;
}

canonical_original_code:
int cal_leapsecs_init()
{
    int ret = -1;
    int fd;
    if (cal_leapsecs.count)
        return cal_leapsecs.count;
    if ((fd = open("/etc/leapsecs.dat",00)) == -1)
        return -1;
    ret = cal_leapsecs_load(fd);
    close(fd);
    return ret;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall cal_leapsecs_sub(unsigned __int64 *a1)
{
unsigned int v2; // edx
unsigned __int64 v3; // rdi
__int64 v4; // rax

v2 = cal_leapsecs_init();
if ( v2 != -1 )
{
if ( cal_leapsecs[0] )
{
v3 = *a1;
v4 = 0LL;
do
{
v2 = v4;
if ( v3 < *(_QWORD *)(cal_leapsecs[1] + 8 * v4) )
break;
v2 = ++v4;
}
while ( cal_leapsecs[0] != v4 );
}
else
{
v2 = 0;
}
*a1 -= (int)v2;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int64 *a1)
{
unsigned int v2; // edx
unsigned __int64 v3; // rdi
__int64 v4; // rax

v2 = func7();
if ( v2 != -1 )
{
if ( cal_leapsecs[0] )
{
v3 = *a1;
v4 = 0LL;
do
{
v2 = v4;
if ( v3 < *(_QWORD *)(cal_leapsecs[1] + 8 * v4) )
break;
v2 = ++v4;
}
while ( cal_leapsecs[0] != v4 );
}
else
{
v2 = 0;
}
*a1 -= (int)v2;
}
return v2;
}

original_code:
int
cal_leapsecs_sub(_uint64_t *tai)
{
    int i,r;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (i=0,r=0;i<cal_leapsecs.count;i++,r++)
        if (*tai < cal_leapsecs.list[i]) break;
    *tai -= r;
    return r;
}

canonical_original_code:
int cal_leapsecs_sub(unsigned long long *tai)
{
    int i; int r;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (i=0,r=0;i<cal_leapsecs.count;i++,r++)
        if (*tai < cal_leapsecs.list[i]) break;
    *tai -= r;
    return r;
}

variable_types:
tai
unsigned long long *

variable_types: r int


decompiled_code:
__int64 __fastcall cal_leapsecs_add(unsigned __int64 *a1)
{
__int64 result; // rax
unsigned __int64 v2; // rsi
__int64 v3; // rdx
unsigned __int64 v4; // rcx

result = cal_leapsecs_init();
if ( (_DWORD)result != -1 )
{
if ( cal_leapsecs[0] )
{
v2 = *a1;
v3 = 0LL;
result = 0LL;
do
{
v4 = *(_QWORD *)(cal_leapsecs[1] + 8 * v3);
if ( v2 < v4 )
break;
result = (unsigned int)result - (v2 == v4) + 1;
++v3;
}
while ( v3 != cal_leapsecs[0] );
}
else
{
result = 0LL;
}
*a1 += (int)result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int64 *a1)
{
__int64 result; // rax
unsigned __int64 v2; // rsi
__int64 v3; // rdx
unsigned __int64 v4; // rcx

result = func7();
if ( (_DWORD)result != -1 )
{
if ( cal_leapsecs[0] )
{
v2 = *a1;
v3 = 0LL;
result = 0LL;
do
{
v4 = *(_QWORD *)(cal_leapsecs[1] + 8 * v3);
if ( v2 < v4 )
break;
result = (unsigned int)result - (v2 == v4) + 1;
++v3;
}
while ( v3 != cal_leapsecs[0] );
}
else
{
result = 0LL;
}
*a1 += (int)result;
}
return result;
}

original_code:
int
cal_leapsecs_add(_uint64_t *tai)
{
    int i,r;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (r=0,i=0;i<cal_leapsecs.count;i++,r++)
        if (*tai < cal_leapsecs.list[i]) break;
        else if (*tai == cal_leapsecs.list[i]) r--;
    *tai += r;
    return r;
}

canonical_original_code:
int cal_leapsecs_add(unsigned long long *tai)
{
    int i; int r;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (r=0,i=0;i<cal_leapsecs.count;i++,r++)
        if (*tai < cal_leapsecs.list[i]) break;
        else if (*tai == cal_leapsecs.list[i]) r--;
    *tai += r;
    return r;
}


decompiled_code:
__int64 __fastcall cal_isleapsec(unsigned __int64 a1)
{
__int64 result; // rax
__int64 v2; // rax
unsigned __int64 v3; // rdx

result = cal_leapsecs_init();
if ( (_DWORD)result != -1 )
{
if ( cal_leapsecs[0] )
{
v2 = 0LL;
while ( 1 )
{
v3 = *(_QWORD *)(cal_leapsecs[1] + 8 * v2);
if ( v3 > a1 )
return 0LL;
if ( v3 == a1 )
break;
if ( ++v2 == cal_leapsecs[0] )
return 0LL;
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int64 a1)
{
__int64 result; // rax
__int64 v2; // rax
unsigned __int64 v3; // rdx

result = func7();
if ( (_DWORD)result != -1 )
{
if ( cal_leapsecs[0] )
{
v2 = 0LL;
while ( 1 )
{
v3 = *(_QWORD *)(cal_leapsecs[1] + 8 * v2);
if ( v3 > a1 )
return 0LL;
if ( v3 == a1 )
break;
if ( ++v2 == cal_leapsecs[0] )
return 0LL;
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int
cal_isleapsec(_uint64_t tai)
{
    int i;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (i=0;i<cal_leapsecs.count;i++)
    {
        if (tai < cal_leapsecs.list[i]) return 0;
        if (tai == cal_leapsecs.list[i])
            return 1;
    }
    return 0;
}

canonical_original_code:
int cal_isleapsec(unsigned long long tai)
{
    int i;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (i=0;i<cal_leapsecs.count;i++)
    {
        if (tai < cal_leapsecs.list[i]) return 0;
        if (tai == cal_leapsecs.list[i])
            return 1;
    }
    return 0;
}

variable_types:
tai
unsigned long long


decompiled_code:
__int64 __fastcall cal_leapsecs_get(unsigned __int64 a1)
{
unsigned int v1; // edx
__int64 v2; // rax

v1 = cal_leapsecs_init();
if ( v1 != -1 )
{
if ( cal_leapsecs[0] )
{
v2 = 0LL;
do
{
v1 = v2;
if ( *(_QWORD *)(cal_leapsecs[1] + 8 * v2) > a1 )
break;
v1 = ++v2;
}
while ( cal_leapsecs[0] != v2 );
}
else
{
v1 = 0;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int64 a1)
{
unsigned int v1; // edx
__int64 v2; // rax

v1 = func7();
if ( v1 != -1 )
{
if ( cal_leapsecs[0] )
{
v2 = 0LL;
do
{
v1 = v2;
if ( *(_QWORD *)(cal_leapsecs[1] + 8 * v2) > a1 )
break;
v1 = ++v2;
}
while ( cal_leapsecs[0] != v2 );
}
else
{
v1 = 0;
}
}
return v1;
}

original_code:
int
cal_leapsecs_get(_uint64_t tai)
{
    int i,r=0;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (i=0;i<cal_leapsecs.count;i++,r++)
        if (tai < cal_leapsecs.list[i]) break;
    return r;
}

canonical_original_code:
int cal_leapsecs_get(unsigned long long tai)
{
    int i; int r = 0;
    if (cal_leapsecs_init() == -1)
        return -1;
    for (i=0;i<cal_leapsecs.count;i++,r++)
        if (tai < cal_leapsecs.list[i]) break;
    return r;
}


return_type: double

user_defined_types: struct _cal_datetime_s { long year; long mon; long day; long hour; long min; long sec; long off; }


decompiled_code:
__int64 __fastcall cal_tai(_QWORD *a1)
{
return 86400LL * (unsigned int)(int)cal_mjd((__int64)a1)
+ a1[5]
+ 60 * (a1[4] + 60LL * a1[3] - a1[6])
+ 0x3FFFFFFF2EFBBF8ALL;
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD *a1)
{
return 86400LL * (unsigned int)(int)func12((__int64)a1)
+ a1[5]
+ 60 * (a1[4] + 60LL * a1[3] - a1[6])
+ 0x3FFFFFFF2EFBBF8ALL;
}

original_code:
_uint64_t
cal_tai(const cal_datetime_t *t)
{
    long d,s;
    _uint64_t x;
    d = (long) cal_mjd(t);
    s = t->hour * 60 + t->min;
    s = (s - t->off) * 60 + t->sec;
    x = d * 86400ULL + 4611686014920671114ULL + (long long) s;
    return x;
}

canonical_original_code:
unsigned long long cal_tai(struct _cal_datetime_s *t)
{
    long d; long s;
    unsigned long long x;
    d = (long) cal_mjd(t);
    s = t->hour * 60 + t->min;
    s = (s - t->off) * 60 + t->sec;
    x = d * 86400ULL + 4611686014920671114ULL + (long long) s;
    return x;
}

variable_types: s long

variable_types:
x
unsigned long long

return_type: unsigned long long


repo: ajdunlap/othello

decompiled_code:
__int64 __fastcall learning_static_eval(__int64 a1)
{
_BYTE v2[72]; // [rsp+0h] [rbp-48h] BYREF

compute_board_counts(v2, a1);
return weight_board_counts(&weights, v2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_BYTE v2[72]; // [rsp+0h] [rbp-48h] BYREF

compute_board_counts(v2, a1);
return weight_board_counts(&weights, v2);
}

original_code:
double learning_static_eval (othello_bd *bd) {
  board_counts cts = compute_board_counts(bd);
  return weight_board_counts(&weights,&cts);
}

canonical_original_code:
double learning_static_eval(struct othello_bd *bd) {
  struct board_counts cts = compute_board_counts(bd);
  return weight_board_counts(&weights,&cts);
}

variable_types: struct othello_bd { char board[8][8]; char turn; char just_passed; } *

variable_types: struct board_counts { int turn; int squares_filled; int score; int counts[10]; }


decompiled_code:
bool __fastcall play_human_turn_is_game_over(__int64 a1, _DWORD *a2, _DWORD *a3)
{
bool result; // al

if ( (unsigned int)have_legal_moves() )
{
while ( 1 )
{
while ( 1 )
{
printf("Move for player %d? (type row column) > ", (unsigned int)*(char *)(a1 + 64));
if ( (unsigned int)_isoc99_scanf("%d %d", a2, a3) == 2 )
break;
while ( getchar() != 10 )
;
puts("Bad move format; enter row column.");
}
if ( (unsigned int)play_piece_if_legal(a1, (unsigned int)*a2, (unsigned int)*a3) )
break;
puts("Illegal move");
}
result = 0;
}
else
{
printf("Move for player %d? > (PASS)\n", (unsigned int)*(char *)(a1 + 64));
*a3 = -1;
*a2 = -1;
result = (unsigned int)pass_turn_is_game_over(a1) != 0;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1, _DWORD *a2, _DWORD *a3)
{
bool result; // al

if ( (unsigned int)have_legal_moves() )
{
while ( 1 )
{
while ( 1 )
{
printf("Move for player %d? (type row column) > ", (unsigned int)*(char *)(a1 + 64));
if ( (unsigned int)_isoc99_scanf("%d %d", a2, a3) == 2 )
break;
while ( getchar() != 10 )
;
puts("Bad move format; enter row column.");
}
if ( (unsigned int)play_piece_if_legal(a1, (unsigned int)*a2, (unsigned int)*a3) )
break;
puts("Illegal move");
}
result = 0;
}
else
{
printf("Move for player %d? > (PASS)\n", (unsigned int)*(char *)(a1 + 64));
*a3 = -1;
*a2 = -1;
result = (unsigned int)pass_turn_is_game_over(a1) != 0;
}
return result;
}

original_code:
_Bool play_human_turn_is_game_over(othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  if (have_legal_moves(bd)) {
    while (1) {
      printf("Move for player %d? (type row column) > ",bd->turn);
      if (scanf("%d %d",x,y) != 2) {
        while (getchar() != '\n');
        printf("Bad move format; enter row column.\n");
      } else if (!play_piece_if_legal(bd,*x,*y)) {
        printf("Illegal move\n");
      } else {
        return 0;
      }
    }
  } else {
    printf("Move for player %d? > (PASS)\n",bd->turn);
    *x = *y = -1;
    return pass_turn_is_game_over(bd);
  }
}

canonical_original_code:
_Bool play_human_turn_is_game_over(struct othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  if (have_legal_moves(bd)) {
    while (1) {
      printf("Move for player %d? (type row column) > ",bd->turn);
      if (scanf("%d %d",x,y) != 2) {
        while (getchar() != '\n');
        printf("Bad move format; enter row column.\n");
      } else if (!play_piece_if_legal(bd,*x,*y)) {
        printf("Illegal move\n");
      } else {
        return 0;
      }
    }
  } else {
    printf("Move for player %d? > (PASS)\n",bd->turn);
    *x = *y = -1;
    return pass_turn_is_game_over(bd);
  }
}

variable_types:
x
int *

variable_types:
y
int *

variable_types: depth int

user_defined_types: struct othello_bd { char board[8][8]; char turn; char just_passed; }


decompiled_code:
bool __fastcall play_random_turn_is_game_over(__int64 a1, _DWORD *a2, _DWORD *a3)
{
__int64 v5; // rsi
bool result; // al

random_move();
v5 = (unsigned int)*a2;
if ( (_DWORD)v5 == -1 )
{
*a3 = -1;
*a2 = -1;
result = (unsigned int)pass_turn_is_game_over(a1) != 0;
}
else
{
if ( !(unsigned int)play_piece_if_legal(a1, v5, (unsigned int)*a3) )
_assert_fail("play_piece_if_legal(bd,*x,*y)", "othello.c", 0x36u, "play_random_turn_is_game_over");
result = 0;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1, _DWORD *a2, _DWORD *a3)
{
__int64 v5; // rsi
bool result; // al

random_move();
v5 = (unsigned int)*a2;
if ( (_DWORD)v5 == -1 )
{
*a3 = -1;
*a2 = -1;
result = (unsigned int)pass_turn_is_game_over(a1) != 0;
}
else
{
if ( !(unsigned int)play_piece_if_legal(a1, v5, (unsigned int)*a3) )
_assert_fail("play_piece_if_legal(bd,*x,*y)", "othello.c", 0x36u, "play_random_turn_is_game_over");
result = 0;
}
return result;
}

original_code:
_Bool play_random_turn_is_game_over(othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  random_move(bd,x,y);
  if (*x != -1) {
    ((play_piece_if_legal(bd,*x,*y)) ? (void) (0) : __assert_fail ("play_piece_if_legal(bd,*x,*y)", "othello.c", 54, __extension__ __PRETTY_FUNCTION__));
    return 0;
  } else {
    *x = *y = -1;
    return pass_turn_is_game_over(bd);
  }
}

canonical_original_code:
_Bool play_random_turn_is_game_over(struct othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  random_move(bd,x,y);
  if (*x != -1) {
    ((play_piece_if_legal(bd,*x,*y)) ? (void) (0) : __assert_fail ("play_piece_if_legal(bd,*x,*y)", "othello.c", 54, __extension__ __PRETTY_FUNCTION__));
    return 0;
  } else {
    *x = *y = -1;
    return pass_turn_is_game_over(bd);
  }
}


decompiled_code:
__int64 __fastcall play_minimax_ai_turn_is_game_over(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6, __int64 a7)
{
int v10; // ebx
__int64 v11; // rdi
_QWORD *v12; // rax
_QWORD *v13; // rax

v10 = (*(char *)(a1 + 64) + 1) >> 1;
v11 = trees[v10];
if ( v11 )
{
v12 = (_QWORD *)cut_tree_for_move(v11, a4, a5);
trees[v10] = (__int64)v12;
if ( v12 )
{
if ( !(unsigned __int8)boards_equal(*v12, a1) )
_assert_fail(
"!trees[i] || boards_equal(trees[i]->bd,bd)",
"othello.c",
0x46u,
"play_minimax_ai_turn_is_game_over");
}
}
trees[v10] = build_minimax_tree(a7, trees[v10], 0LL, a1, a6);
if ( (unsigned int)have_legal_moves(a1) )
{
best_move(trees[v10], a2, a3);
if ( !(unsigned int)play_piece_if_legal(a1, (unsigned int)*a2, (unsigned int)*a3) )
_assert_fail("result", "othello.c", 0x4Cu, "play_minimax_ai_turn_is_game_over");
}
else
{
*a3 = -1;
*a2 = -1;
if ( (unsigned int)pass_turn_is_game_over(a1) )
return 1LL;
}
v13 = (_QWORD *)cut_tree_for_move(trees[v10], (unsigned int)*a2, (unsigned int)*a3);
trees[v10] = (__int64)v13;
if ( !v13 )
return 0LL;
if ( !(unsigned __int8)boards_equal(*v13, a1) )
_assert_fail("!trees[i] || boards_equal(trees[i]->bd,bd)", "othello.c", 0x54u, "play_minimax_ai_turn_is_game_over");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6, __int64 a7)
{
int v10; // ebx
__int64 v11; // rdi
_QWORD *v12; // rax
_QWORD *v13; // rax

v10 = (*(char *)(a1 + 64) + 1) >> 1;
v11 = trees[v10];
if ( v11 )
{
v12 = (_QWORD *)cut_tree_for_move(v11, a4, a5);
trees[v10] = (__int64)v12;
if ( v12 )
{
if ( !(unsigned __int8)boards_equal(*v12, a1) )
_assert_fail(
"!trees[i] || boards_equal(trees[i]->bd,bd)",
"othello.c",
0x46u,
"play_minimax_ai_turn_is_game_over");
}
}
trees[v10] = build_minimax_tree(a7, trees[v10], 0LL, a1, a6);
if ( (unsigned int)have_legal_moves(a1) )
{
best_move(trees[v10], a2, a3);
if ( !(unsigned int)play_piece_if_legal(a1, (unsigned int)*a2, (unsigned int)*a3) )
_assert_fail("result", "othello.c", 0x4Cu, "play_minimax_ai_turn_is_game_over");
}
else
{
*a3 = -1;
*a2 = -1;
if ( (unsigned int)pass_turn_is_game_over(a1) )
return 1LL;
}
v13 = (_QWORD *)cut_tree_for_move(trees[v10], (unsigned int)*a2, (unsigned int)*a3);
trees[v10] = (__int64)v13;
if ( !v13 )
return 0LL;
if ( !(unsigned __int8)boards_equal(*v13, a1) )
_assert_fail("!trees[i] || boards_equal(trees[i]->bd,bd)", "othello.c", 0x54u, "play_minimax_ai_turn_is_game_over");
return 0LL;
}

original_code:
_Bool play_minimax_ai_turn_is_game_over(othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth, double (*static_eval)(othello_bd*)) {
  _Bool game_over;
  int i = (1+bd->turn)>>1;
  if (trees[i]) {
    trees[i] = cut_tree_for_move(trees[i],opp_x,opp_y);
    ((!trees[i] || boards_equal(trees[i]->bd,bd)) ? (void) (0) : __assert_fail ("!trees[i] || boards_equal(trees[i]->bd,bd)", "othello.c", 70, __extension__ __PRETTY_FUNCTION__));
  }
  trees[i] = build_minimax_tree(static_eval,trees[i],0,bd,depth);
  if (have_legal_moves(bd)) {
    best_move(trees[i],x,y);
    int result = play_piece_if_legal(bd,*x,*y);
    ((result) ? (void) (0) : __assert_fail ("result", "othello.c", 76, __extension__ __PRETTY_FUNCTION__));
    game_over = 0;
  } else {
    *x = *y = -1;
    game_over = pass_turn_is_game_over(bd);
  }
  if (!game_over) {
    trees[i] = cut_tree_for_move(trees[i],*x,*y);
    ((!trees[i] || boards_equal(trees[i]->bd,bd)) ? (void) (0) : __assert_fail ("!trees[i] || boards_equal(trees[i]->bd,bd)", "othello.c", 84, __extension__ __PRETTY_FUNCTION__));
  }
  return game_over;
}

canonical_original_code:
_Bool play_minimax_ai_turn_is_game_over(struct othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth, double (*static_eval)(struct othello_bd *)) {
  _Bool game_over;
  int i = (1+bd->turn)>>1;
  if (trees[i]) {
    trees[i] = cut_tree_for_move(trees[i],opp_x,opp_y);
    ((!trees[i] || boards_equal(trees[i]->bd,bd)) ? (void) (0) : __assert_fail ("!trees[i] || boards_equal(trees[i]->bd,bd)", "othello.c", 70, __extension__ __PRETTY_FUNCTION__));
  }
  trees[i] = build_minimax_tree(static_eval,trees[i],0,bd,depth);
  if (have_legal_moves(bd)) {
    best_move(trees[i],x,y);
    int result = play_piece_if_legal(bd,*x,*y);
    ((result) ? (void) (0) : __assert_fail ("result", "othello.c", 76, __extension__ __PRETTY_FUNCTION__));
    game_over = 0;
  } else {
    *x = *y = -1;
    game_over = pass_turn_is_game_over(bd);
  }
  if (!game_over) {
    trees[i] = cut_tree_for_move(trees[i],*x,*y);
    ((!trees[i] || boards_equal(trees[i]->bd,bd)) ? (void) (0) : __assert_fail ("!trees[i] || boards_equal(trees[i]->bd,bd)", "othello.c", 84, __extension__ __PRETTY_FUNCTION__));
  }
  return game_over;
}

variable_types: i int

variable_types: result int


decompiled_code:
__int64 __fastcall play_hand_minimax_ai_turn_is_game_over(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
return play_minimax_ai_turn_is_game_over(a1, a2, a3, a4, a5, a6, (__int64)&hand_static_eval);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
return func3(a1, a2, a3, a4, a5, a6, (__int64)&hand_static_eval);
}

original_code:
_Bool play_hand_minimax_ai_turn_is_game_over(othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  return play_minimax_ai_turn_is_game_over(bd, x, y, opp_x, opp_y, depth, hand_static_eval);
}

canonical_original_code:
_Bool play_hand_minimax_ai_turn_is_game_over(struct othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  return play_minimax_ai_turn_is_game_over(bd, x, y, opp_x, opp_y, depth, hand_static_eval);
}


decompiled_code:
__int64 __fastcall play_learning_minimax_ai_turn_is_game_over(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
return play_minimax_ai_turn_is_game_over(a1, a2, a3, a4, a5, a6, (__int64)learning_static_eval);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
return func3(a1, a2, a3, a4, a5, a6, (__int64)learning_static_eval);
}

original_code:
_Bool play_learning_minimax_ai_turn_is_game_over(othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  return play_minimax_ai_turn_is_game_over(bd, x, y, opp_x, opp_y, depth, learning_static_eval);
}

canonical_original_code:
_Bool play_learning_minimax_ai_turn_is_game_over(struct othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  return play_minimax_ai_turn_is_game_over(bd, x, y, opp_x, opp_y, depth, learning_static_eval);
}


decompiled_code:
__int64 __fastcall play_naive_minimax_ai_turn_is_game_over(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
return play_minimax_ai_turn_is_game_over(a1, a2, a3, a4, a5, a6, (__int64)&naive_static_eval);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, _DWORD *a2, _DWORD *a3, unsigned int a4, unsigned int a5, unsigned int a6)
{
return func3(a1, a2, a3, a4, a5, a6, (__int64)&naive_static_eval);
}

original_code:
_Bool play_naive_minimax_ai_turn_is_game_over(othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  return play_minimax_ai_turn_is_game_over(bd, x, y, opp_x, opp_y, depth, naive_static_eval);
}

canonical_original_code:
_Bool play_naive_minimax_ai_turn_is_game_over(struct othello_bd *bd, int *x, int *y, int opp_x, int opp_y, int depth) {
  return play_minimax_ai_turn_is_game_over(bd, x, y, opp_x, opp_y, depth, naive_static_eval);
}


usage

decompiled_code:
size_t usage()
{
return fwrite(
"Usage: othello [-d DEPTH|DEPTH1,DEPTH2] [-n NGAMES] [-q] [PLAYER1 PLAYER2]\n"
"PLAYER1 and PLAYER2 can be any of human, hand-ai, naive-ai, learning-ai, random.\n"
"If PLAYER1 and PLAYER2 are omitted, PLAYER1=human, PLAYER2=hand-ai is assumed.\n"
"Options:\n"
" -d [DEPTH|DEPTH1,DEPTH2] : specify depth of minimax tree search\n"
" DEPTH can be either a single integer or two comma-separated integers\n"
" to specify the depth for each player\n"
" -h : print this help\n"
" -n NGAMES : play n games\n"
" -q : don't print out boards, just winners of each game (for AI-vs-AI play)\n",
1uLL,
0x29AuLL,
stderr);
}

canonical_decompiled_code:
size_t func7()
{
return fwrite(
"Usage: othello [-d DEPTH|DEPTH1,DEPTH2] [-n NGAMES] [-q] [PLAYER1 PLAYER2]\n"
"PLAYER1 and PLAYER2 can be any of human, hand-ai, naive-ai, learning-ai, random.\n"
"If PLAYER1 and PLAYER2 are omitted, PLAYER1=human, PLAYER2=hand-ai is assumed.\n"
"Options:\n"
" -d [DEPTH|DEPTH1,DEPTH2] : specify depth of minimax tree search\n"
" DEPTH can be either a single integer or two comma-separated integers\n"
" to specify the depth for each player\n"
" -h : print this help\n"
" -n NGAMES : play n games\n"
" -q : don't print out boards, just winners of each game (for AI-vs-AI play)\n",
1uLL,
0x29AuLL,
stderr);
}

original_code:
void usage () {
  fprintf(stderr,"Usage: othello [-d DEPTH|DEPTH1,DEPTH2] [-n NGAMES] [-q] [PLAYER1 PLAYER2]\n"
                 "PLAYER1 and PLAYER2 can be any of human, hand-ai, naive-ai, learning-ai, random.\n"
                 "If PLAYER1 and PLAYER2 are omitted, PLAYER1=human, PLAYER2=hand-ai is assumed.\n"
                 "Options:\n"
                 "  -d [DEPTH|DEPTH1,DEPTH2]  : specify depth of minimax tree search\n"
                 "                              DEPTH can be either a single integer or two comma-separated integers\n"
                 "                              to specify the depth for each player\n"
                 "  -h                        : print this help\n"
                 "  -n NGAMES                 : play n games\n"
                 "  -q                        : don't print out boards, just winners of each game (for AI-vs-AI play)\n");
}

canonical_original_code:
void usage() {
  fprintf(stderr,"Usage: othello [-d DEPTH|DEPTH1,DEPTH2] [-n NGAMES] [-q] [PLAYER1 PLAYER2]\n"
                 "PLAYER1 and PLAYER2 can be any of human, hand-ai, naive-ai, learning-ai, random.\n"
                 "If PLAYER1 and PLAYER2 are omitted, PLAYER1=human, PLAYER2=hand-ai is assumed.\n"
                 "Options:\n"
                 "  -d [DEPTH|DEPTH1,DEPTH2]  : specify depth of minimax tree search\n"
                 "                              DEPTH can be either a single integer or two comma-separated integers\n"
                 "                              to specify the depth for each player\n"
                 "  -h                        : print this help\n"
                 "  -n NGAMES                 : play n games\n"
                 "  -q                        : don't print out boards, just winners of each game (for AI-vs-AI play)\n");
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char v3; // r15
int v4; // eax
unsigned int v5; // eax
int v6; // eax
unsigned int v7; // ebx
const char **v8; // r12
struct timeval *v9; // rbx
__int64 *v10; // r13
const char *v11; // rbp
bool (__fastcall *v12)(__int64, _DWORD *, _DWORD *); // rax
__int64 (__fastcall *v13)(__int64, _DWORD *, _DWORD *, unsigned int, unsigned int, unsigned int); // rax
__int64 v14; // rsi
__int64 v15; // rdx
__int64 (__fastcall *v16)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD); // rax
char v17; // bl
__int64 (__fastcall *v18)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD); // rax
int v19; // er14
int v20; // er13
char *v21; // rbx
char v22; // [rsp+8h] [rbp-100h]
double v23; // [rsp+8h] [rbp-100h]
unsigned int v24; // [rsp+18h] [rbp-F0h]
int v25; // [rsp+1Ch] [rbp-ECh]
__int64 v26; // [rsp+20h] [rbp-E8h]
unsigned int v27; // [rsp+28h] [rbp-E0h]
char *ptr; // [rsp+30h] [rbp-D8h]
unsigned int v29; // [rsp+3Ch] [rbp-CCh]
unsigned int v30; // [rsp+40h] [rbp-C8h]
unsigned int v31; // [rsp+44h] [rbp-C4h]
unsigned int v32; // [rsp+48h] [rbp-C0h]
int v33; // [rsp+4Ch] [rbp-BCh]
unsigned int v34; // [rsp+58h] [rbp-B0h] BYREF
unsigned int v35; // [rsp+5Ch] [rbp-ACh] BYREF
char *endptr[8]; // [rsp+60h] [rbp-A8h] BYREF
char v37; // [rsp+A0h] [rbp-68h]
bool (__fastcall *v38)(__int64, _DWORD *, _DWORD *); // [rsp+B0h] [rbp-58h] BYREF
__int64 (__fastcall *v39)(__int64, _DWORD *, _DWORD *, unsigned int, unsigned int, unsigned int); // [rsp+B8h] [rbp-50h]
struct timeval tv; // [rsp+C0h] [rbp-48h] BYREF

gettimeofday(&tv, 0LL);
srand(tv.tv_usec);
v38 = 0LL;
v39 = 0LL;
v27 = 4;
v24 = 4;
v3 = 0;
v25 = 1;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "n:qhd:");
if ( v4 == -1 )
break;
if ( v4 == 104 )
{
usage();
exit(0);
}
if ( v4 > 104 )
{
if ( v4 == 110 )
{
v6 = strtol(optarg, endptr, 10);
v25 = v6;
if ( *endptr[0] || v6 <= 0 )
{
fwrite("Option -n requires a positive integer argument\n", 1uLL, 0x2FuLL, stderr);
usage();
exit(1);
}
}
else if ( v4 == 113 )
{
v3 = 1;
}
}
else
{
if ( v4 == 63 )
{
v7 = optopt;
if ( optopt == 110 )
{
fwrite("Option -n requires an argument\n", 1uLL, 0x1FuLL, stderr);
}
else if ( ((*_ctype_b_loc())[v7] & 0x4000) != 0 )
{
fprintf(stderr, "Unknown option `-%c'.\n", v7);
}
else
{
fprintf(stderr, "Unknown option character `\\x%x'.\n", v7);
}
usage();
exit(1);
}
if ( v4 == 100 )
{
v5 = strtol(optarg, endptr, 10);
v24 = v5;
if ( *endptr[0] )
{
if ( *endptr[0] != 44 )
{
fwrite("Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n", 1uLL, 0x3AuLL, stderr);
usage();
exit(1);
}
if ( !endptr[0][1] )
{
fwrite("Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n", 1uLL, 0x3AuLL, stderr);
usage();
exit(1);
}
v27 = strtol(endptr[0] + 1, endptr, 10);
if ( *endptr[0] )
{
fwrite("Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n", 1uLL, 0x3AuLL, stderr);
usage();
exit(1);
}
}
else
{
v27 = v5;
}
}
}
}
if ( argc <= optind )
{
v12 = play_human_turn_is_game_over;
v22 = 1;
}
else
{
v8 = &argv[optind];
v9 = (struct timeval *)&v38;
v10 = (__int64 *)(&v38 + (unsigned int)(argc + ~optind));
v22 = 0;
do
{
v11 = *v8;
if ( !strcmp(*v8, "human") )
{
v9->tv_sec = (__time_t)play_human_turn_is_game_over;
v22 = 1;
}
else if ( !strcmp(*v8, "hand-ai") )
{
v9->tv_sec = (__time_t)play_hand_minimax_ai_turn_is_game_over;
}
else if ( !strcmp(v11, "naive-ai") )
{
v9->tv_sec = (__time_t)play_naive_minimax_ai_turn_is_game_over;
}
else if ( !strcmp(v11, "learning-ai") )
{
v9->tv_sec = (__time_t)play_learning_minimax_ai_turn_is_game_over;
}
else
{
if ( strcmp(v11, "random") )
{
fprintf(
stderr,
"Unrecognized player type %s. Expected one of human, hand-ai, naive-ai, learning-ai, random.\n",
v11);
usage();
exit(1);
}
v9->tv_sec = (__time_t)play_random_turn_is_game_over;
}
if ( v9 == (struct timeval *)v10 )
break;
++v8;
v9 = (struct timeval *)((char *)v9 + 8);
}
while ( v9 != &tv );
v12 = v38;
if ( !v38 )
{
v12 = play_human_turn_is_game_over;
v22 = 1;
}
}
v38 = v12;
v13 = v39;
if ( !v39 )
v13 = play_hand_minimax_ai_turn_is_game_over;
v39 = v13;
if ( v3 && v22 )
fwrite(
"WARNING: Quiet mode enabled but a human is playing.\n"
" This is going to make the game awfully hard for her or him!\n",
1uLL,
0x79uLL,
stderr);
init_weights(&weights);
v14 = 6660LL;
ptr = (char *)calloc(v25, 0x1A04uLL);
v15 = (unsigned int)(v25 >> 31);
LODWORD(v15) = v25 % 2;
v33 = v25 / 2;
v26 = 0LL;
v32 = 0;
v31 = 0;
v30 = 0;
v29 = 0;
while ( v25 > (int)v26 )
{
init_learning_ai_game_state(&ptr[6660 * v26], v14, v15);
reset_othello_bd(endptr);
do
{
if ( v3 )
{
v18 = (__int64 (__fastcall *)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD))*(&v38 + ((1 - v37) >> 1));
if ( v37 == 1 )
v17 = v18(endptr, &v34, &v35, v34, v35, v24);
else
v17 = v18(endptr, &v34, &v35, v34, v35, v27);
}
else
{
show_othello_bd(stdout, endptr);
v16 = (__int64 (__fastcall *)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD))*(&v38 + ((1 - v37) >> 1));
if ( v37 == 1 )
v17 = v16(endptr, &v34, &v35, v34, v35, v24);
else
v17 = v16(endptr, &v34, &v35, v34, v35, v27);
if ( v34 == -1 )
printf("Player %d passes.\n", (unsigned int)-v37);
else
printf("Player %d's move: %d %d.\n", (unsigned int)-v37, v34, v35);
}
add_board(&ptr[6660 * v26], endptr);
}
while ( !v17 );
if ( !v3 )
show_othello_bd(stdout, endptr);
v23 = score(endptr);
if ( v23 > 0.0 )
{
puts("Player 1 wins!");
if ( v33 < (int)v26 )
++v31;
++v29;
}
else if ( v23 < 0.0 )
{
puts("Player -1 wins!");
++v30;
if ( v33 < (int)v26 )
++v32;
}
else
{
puts("Tie!");
}
if ( trees[0] )
{
free_minimax_tree();
trees[0] = 0LL;
}
if ( trees[1] )
{
free_minimax_tree();
trees[1] = 0LL;
}
v19 = 0;
if ( (int)v26 >= 0 )
v19 = v26;
v20 = 10;
do
{
if ( v19 <= (int)v26 )
{
v21 = &ptr[6660 * v19];
do
{
update_weights_from_game(&weights, v21, (unsigned int)(int)v23, 1LL);
v21 += 6660;
}
while ( v21 != &ptr[6660 * (unsigned int)(v26 - v19) + 6660 + 6660 * (__int64)v19] );
}
--v20;
}
while ( v20 );
printf("WEIGHTS: ");
print_weights(&weights);
v14 = v29;
printf("\nSCORE: 1: %d (%d); 2: %d (%d)\n", v29, v31, v30, v32);
++v26;
}
free(ptr);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char v3; // r15
int v4; // eax
unsigned int v5; // eax
int v6; // eax
unsigned int v7; // ebx
const char **v8; // r12
struct timeval *v9; // rbx
__int64 *v10; // r13
const char *v11; // rbp
bool (__fastcall *v12)(__int64, _DWORD *, _DWORD *); // rax
__int64 (__fastcall *v13)(__int64, _DWORD *, _DWORD *, unsigned int, unsigned int, unsigned int); // rax
__int64 v14; // rsi
__int64 v15; // rdx
__int64 (__fastcall *v16)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD); // rax
char v17; // bl
__int64 (__fastcall *v18)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD); // rax
int v19; // er14
int v20; // er13
char *v21; // rbx
char v22; // [rsp+8h] [rbp-100h]
double v23; // [rsp+8h] [rbp-100h]
unsigned int v24; // [rsp+18h] [rbp-F0h]
int v25; // [rsp+1Ch] [rbp-ECh]
__int64 v26; // [rsp+20h] [rbp-E8h]
unsigned int v27; // [rsp+28h] [rbp-E0h]
char *ptr; // [rsp+30h] [rbp-D8h]
unsigned int v29; // [rsp+3Ch] [rbp-CCh]
unsigned int v30; // [rsp+40h] [rbp-C8h]
unsigned int v31; // [rsp+44h] [rbp-C4h]
unsigned int v32; // [rsp+48h] [rbp-C0h]
int v33; // [rsp+4Ch] [rbp-BCh]
unsigned int v34; // [rsp+58h] [rbp-B0h] BYREF
unsigned int v35; // [rsp+5Ch] [rbp-ACh] BYREF
char *endptr[8]; // [rsp+60h] [rbp-A8h] BYREF
char v37; // [rsp+A0h] [rbp-68h]
bool (__fastcall *v38)(__int64, _DWORD *, _DWORD *); // [rsp+B0h] [rbp-58h] BYREF
__int64 (__fastcall *v39)(__int64, _DWORD *, _DWORD *, unsigned int, unsigned int, unsigned int); // [rsp+B8h] [rbp-50h]
struct timeval tv; // [rsp+C0h] [rbp-48h] BYREF

gettimeofday(&tv, 0LL);
srand(tv.tv_usec);
v38 = 0LL;
v39 = 0LL;
v27 = 4;
v24 = 4;
v3 = 0;
v25 = 1;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "n:qhd:");
if ( v4 == -1 )
break;
if ( v4 == 104 )
{
func7();
exit(0);
}
if ( v4 > 104 )
{
if ( v4 == 110 )
{
v6 = strtol(optarg, endptr, 10);
v25 = v6;
if ( *endptr[0] || v6 <= 0 )
{
fwrite("Option -n requires a positive integer argument\n", 1uLL, 0x2FuLL, stderr);
func7();
exit(1);
}
}
else if ( v4 == 113 )
{
v3 = 1;
}
}
else
{
if ( v4 == 63 )
{
v7 = optopt;
if ( optopt == 110 )
{
fwrite("Option -n requires an argument\n", 1uLL, 0x1FuLL, stderr);
}
else if ( ((*_ctype_b_loc())[v7] & 0x4000) != 0 )
{
fprintf(stderr, "Unknown option `-%c'.\n", v7);
}
else
{
fprintf(stderr, "Unknown option character `\\x%x'.\n", v7);
}
func7();
exit(1);
}
if ( v4 == 100 )
{
v5 = strtol(optarg, endptr, 10);
v24 = v5;
if ( *endptr[0] )
{
if ( *endptr[0] != 44 )
{
fwrite("Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n", 1uLL, 0x3AuLL, stderr);
func7();
exit(1);
}
if ( !endptr[0][1] )
{
fwrite("Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n", 1uLL, 0x3AuLL, stderr);
func7();
exit(1);
}
v27 = strtol(endptr[0] + 1, endptr, 10);
if ( *endptr[0] )
{
fwrite("Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n", 1uLL, 0x3AuLL, stderr);
func7();
exit(1);
}
}
else
{
v27 = v5;
}
}
}
}
if ( argc <= optind )
{
v12 = play_human_turn_is_game_over;
v22 = 1;
}
else
{
v8 = &argv[optind];
v9 = (struct timeval *)&v38;
v10 = (__int64 *)(&v38 + (unsigned int)(argc + ~optind));
v22 = 0;
do
{
v11 = *v8;
if ( !strcmp(*v8, "human") )
{
v9->tv_sec = (__time_t)play_human_turn_is_game_over;
v22 = 1;
}
else if ( !strcmp(*v8, "hand-ai") )
{
v9->tv_sec = (__time_t)play_hand_minimax_ai_turn_is_game_over;
}
else if ( !strcmp(v11, "naive-ai") )
{
v9->tv_sec = (__time_t)play_naive_minimax_ai_turn_is_game_over;
}
else if ( !strcmp(v11, "learning-ai") )
{
v9->tv_sec = (__time_t)play_learning_minimax_ai_turn_is_game_over;
}
else
{
if ( strcmp(v11, "random") )
{
fprintf(
stderr,
"Unrecognized player type %s. Expected one of human, hand-ai, naive-ai, learning-ai, random.\n",
v11);
func7();
exit(1);
}
v9->tv_sec = (__time_t)play_random_turn_is_game_over;
}
if ( v9 == (struct timeval *)v10 )
break;
++v8;
v9 = (struct timeval *)((char *)v9 + 8);
}
while ( v9 != &tv );
v12 = v38;
if ( !v38 )
{
v12 = play_human_turn_is_game_over;
v22 = 1;
}
}
v38 = v12;
v13 = v39;
if ( !v39 )
v13 = play_hand_minimax_ai_turn_is_game_over;
v39 = v13;
if ( v3 && v22 )
fwrite(
"WARNING: Quiet mode enabled but a human is playing.\n"
" This is going to make the game awfully hard for her or him!\n",
1uLL,
0x79uLL,
stderr);
init_weights(&weights);
v14 = 6660LL;
ptr = (char *)calloc(v25, 0x1A04uLL);
v15 = (unsigned int)(v25 >> 31);
LODWORD(v15) = v25 % 2;
v33 = v25 / 2;
v26 = 0LL;
v32 = 0;
v31 = 0;
v30 = 0;
v29 = 0;
while ( v25 > (int)v26 )
{
init_learning_ai_game_state(&ptr[6660 * v26], v14, v15);
reset_othello_bd(endptr);
do
{
if ( v3 )
{
v18 = (__int64 (__fastcall *)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD))*(&v38 + ((1 - v37) >> 1));
if ( v37 == 1 )
v17 = v18(endptr, &v34, &v35, v34, v35, v24);
else
v17 = v18(endptr, &v34, &v35, v34, v35, v27);
}
else
{
show_othello_bd(stdout, endptr);
v16 = (__int64 (__fastcall *)(char **, unsigned int *, unsigned int *, _QWORD, _QWORD, _QWORD))*(&v38 + ((1 - v37) >> 1));
if ( v37 == 1 )
v17 = v16(endptr, &v34, &v35, v34, v35, v24);
else
v17 = v16(endptr, &v34, &v35, v34, v35, v27);
if ( v34 == -1 )
printf("Player %d passes.\n", (unsigned int)-v37);
else
printf("Player %d's move: %d %d.\n", (unsigned int)-v37, v34, v35);
}
add_board(&ptr[6660 * v26], endptr);
}
while ( !v17 );
if ( !v3 )
show_othello_bd(stdout, endptr);
v23 = score(endptr);
if ( v23 > 0.0 )
{
puts("Player 1 wins!");
if ( v33 < (int)v26 )
++v31;
++v29;
}
else if ( v23 < 0.0 )
{
puts("Player -1 wins!");
++v30;
if ( v33 < (int)v26 )
++v32;
}
else
{
puts("Tie!");
}
if ( trees[0] )
{
free_minimax_tree();
trees[0] = 0LL;
}
if ( trees[1] )
{
free_minimax_tree();
trees[1] = 0LL;
}
v19 = 0;
if ( (int)v26 >= 0 )
v19 = v26;
v20 = 10;
do
{
if ( v19 <= (int)v26 )
{
v21 = &ptr[6660 * v19];
do
{
update_weights_from_game(&weights, v21, (unsigned int)(int)v23, 1LL);
v21 += 6660;
}
while ( v21 != &ptr[6660 * (unsigned int)(v26 - v19) + 6660 + 6660 * (__int64)v19] );
}
--v20;
}
while ( v20 );
printf("WEIGHTS: ");
print_weights(&weights);
v14 = v29;
printf("\nSCORE: 1: %d (%d); 2: %d (%d)\n", v29, v31, v30, v32);
++v26;
}
free(ptr);
exit(0);
}

original_code:
int main (int argc, char **argv) {
  struct timeval cur;
  gettimeofday(&cur,((void *)0));
  srand(cur.tv_usec);
  _Bool (*play[2]) (othello_bd*,int*,int*,int,int,int) = { ((void *)0), ((void *)0) };
  int ngames = 1;
  _Bool quiet = 0;
  int depth1 = 4, depth2 = 4;
  int c;
  while (( c = getopt (argc, argv, "n:qhd:")) != -1) {
    char *tail;
    switch (c) {
      case 'n':
        ngames = strtol(optarg,&tail,10);
        if (*tail || ngames <= 0 ) {
          fprintf(stderr,"Option -n requires a positive integer argument\n");
          usage();
          exit(1);
        }
        break;
      case 'd':
        depth1 = strtol(optarg,&tail,10);
        if (!(*tail)) {
          depth2 = depth1;
        } else if (*tail == ',') {
          if (*(tail+1)) {
            depth2 = strtol(tail+1,&tail,10);
            if (*tail) {
              fprintf(stderr,"Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n");
              usage();
              exit(1);
            }
          } else {
            fprintf(stderr,"Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n");
            usage();
            exit(1);
          }
        } else {
          fprintf(stderr,"Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n");
          usage();
          exit(1);
        }
        break;
      case 'q':
        quiet = 1;
        break;
      case 'h':
        usage();
        exit(0);
        break;
      case '?':
        if (optopt == 'n') {
          fprintf(stderr,"Option -n requires an argument\n");
        }
        else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short int) _ISprint)) {
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        } else {
          fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
        }
        usage();
        exit(1);
        break;
    }
  }
  _Bool have_human_player = 0;
  for (int k = optind ; k < argc && k < 2 + optind ; ++k) {
    if (!strcmp(argv[k],"human")) {
      play[k-optind] = play_human_turn_is_game_over;
      have_human_player = 1;
    } else if (!strcmp(argv[k],"hand-ai")) {
      play[k-optind] = play_hand_minimax_ai_turn_is_game_over;
    } else if (!strcmp(argv[k],"naive-ai")) {
      play[k-optind] = play_naive_minimax_ai_turn_is_game_over;
    } else if (!strcmp(argv[k],"learning-ai")) {
      play[k-optind] = play_learning_minimax_ai_turn_is_game_over;
    } else if (!strcmp(argv[k],"random")) {
      play[k-optind] = play_random_turn_is_game_over;
    } else {
      fprintf(stderr,"Unrecognized player type %s. Expected one of human, hand-ai, naive-ai, learning-ai, random.\n",argv[k]);
      usage();
      exit(1);
    }
  }
  if (!play[0]) {
    play[0] = play_human_turn_is_game_over;
    have_human_player = 1;
  }
  if (!play[1]) {
    play[1] = play_hand_minimax_ai_turn_is_game_over;
  }
  if (quiet && have_human_player) {
    fprintf(stderr,"WARNING: Quiet mode enabled but a human is playing.\n"
                   "         This is going to make the game awfully hard for her or him!\n");
  }
  init_weights(&weights);
  int wins1 = 0, wins2 = 0, wins1backhalf = 0, wins2backhalf = 0;
  learning_ai_game_state *lags_arr = (learning_ai_game_state*)calloc(ngames,sizeof(learning_ai_game_state));
  for (int game = 0 ; game < ngames ; ++game) {
    othello_bd the_bd;
    othello_bd *bd = &the_bd;
    init_learning_ai_game_state(&lags_arr[game]);
    reset_othello_bd(bd);
    _Bool game_over = 0;
    while (!game_over) {
      if (!quiet) {
        show_othello_bd(stdout,bd);
      }
      int x,y;
      game_over = (play[(1-bd->turn)>>1])(bd,&x,&y,x,y,bd->turn == 1 ? depth1 : depth2);
      if (!quiet) {
        if (x != -1) {
          printf("Player %d's move: %d %d.\n",-bd->turn,x,y);
        } else {
          printf("Player %d passes.\n",-bd->turn);
        }
      }
      add_board(&lags_arr[game],bd);
    }
    if (!quiet) {
      show_othello_bd(stdout,bd);
    }
    double winner = score(bd);
    if (winner > 0) {
      printf("Player 1 wins!\n");
      if (game > ngames/2) {
        ++wins1backhalf;
      }
      ++wins1;
    } else if (winner < 0) {
      printf("Player -1 wins!\n");
      ++wins2;
      if (game > ngames/2) {
        ++wins2backhalf;
      }
    } else {
      printf("Tie!\n");
    }
    for (int k = 0 ; k < 2 ; ++k) {
      if (trees[k]) {
        free_minimax_tree(trees[k]);
        trees[k] = ((void *)0);
      }
    }
    for (int l = 0 ; l < 10 ; ++l) {
      for (int k = game >= 0 ? game : 0 ; k <= game ; ++k) {
        update_weights_from_game(&weights,&lags_arr[k],winner,1);
      }
    }
    printf("WEIGHTS: ");
    print_weights(&weights);
    printf("\nSCORE: 1: %d (%d); 2: %d (%d)\n",wins1,wins1backhalf,wins2,wins2backhalf);
  }
  free((void*)lags_arr);
  exit(0);
}

canonical_original_code:
int main(int argc, char **argv) {
  struct timeval cur;
  gettimeofday(&cur,((void *)0));
  srand(cur.tv_usec);
  _Bool (*play[2])(struct othello_bd *, int *, int *, int, int, int) = { ((void *)0), ((void *)0) };
  int ngames = 1;
  _Bool quiet = 0;
  int depth1 = 4; int depth2 = 4;
  int c;
  while (( c = getopt (argc, argv, "n:qhd:")) != -1) {
    char *tail;
    switch (c) {
      case 'n':
        ngames = strtol(optarg,&tail,10);
        if (*tail || ngames <= 0 ) {
          fprintf(stderr,"Option -n requires a positive integer argument\n");
          usage();
          exit(1);
        }
        break;
      case 'd':
        depth1 = strtol(optarg,&tail,10);
        if (!(*tail)) {
          depth2 = depth1;
        } else if (*tail == ',') {
          if (*(tail+1)) {
            depth2 = strtol(tail+1,&tail,10);
            if (*tail) {
              fprintf(stderr,"Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n");
              usage();
              exit(1);
            }
          } else {
            fprintf(stderr,"Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n");
            usage();
            exit(1);
          }
        } else {
          fprintf(stderr,"Option -d requires argument either DEPTH or DEPTH1,DEPTH2\n");
          usage();
          exit(1);
        }
        break;
      case 'q':
        quiet = 1;
        break;
      case 'h':
        usage();
        exit(0);
        break;
      case '?':
        if (optopt == 'n') {
          fprintf(stderr,"Option -n requires an argument\n");
        }
        else if (((*__ctype_b_loc ())[(int) ((optopt))] & (unsigned short) _ISprint)) {
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        } else {
          fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
        }
        usage();
        exit(1);
        break;
    }
  }
  _Bool have_human_player = 0;
  for (int k = optind; k < argc && k < 2 + optind ; ++k) {
    if (!strcmp(argv[k],"human")) {
      play[k-optind] = play_human_turn_is_game_over;
      have_human_player = 1;
    } else if (!strcmp(argv[k],"hand-ai")) {
      play[k-optind] = play_hand_minimax_ai_turn_is_game_over;
    } else if (!strcmp(argv[k],"naive-ai")) {
      play[k-optind] = play_naive_minimax_ai_turn_is_game_over;
    } else if (!strcmp(argv[k],"learning-ai")) {
      play[k-optind] = play_learning_minimax_ai_turn_is_game_over;
    } else if (!strcmp(argv[k],"random")) {
      play[k-optind] = play_random_turn_is_game_over;
    } else {
      fprintf(stderr,"Unrecognized player type %s. Expected one of human, hand-ai, naive-ai, learning-ai, random.\n",argv[k]);
      usage();
      exit(1);
    }
  }
  if (!play[0]) {
    play[0] = play_human_turn_is_game_over;
    have_human_player = 1;
  }
  if (!play[1]) {
    play[1] = play_hand_minimax_ai_turn_is_game_over;
  }
  if (quiet && have_human_player) {
    fprintf(stderr,"WARNING: Quiet mode enabled but a human is playing.\n"
                   "         This is going to make the game awfully hard for her or him!\n");
  }
  init_weights(&weights);
  int wins1 = 0; int wins2 = 0; int wins1backhalf = 0; int wins2backhalf = 0;
  struct learning_ai_game_state *lags_arr = (learning_ai_game_state*)calloc(ngames,sizeof(learning_ai_game_state));
  for (int game = 0; game < ngames ; ++game) {
    struct othello_bd the_bd;
    struct othello_bd *bd = &the_bd;
    init_learning_ai_game_state(&lags_arr[game]);
    reset_othello_bd(bd);
    _Bool game_over = 0;
    while (!game_over) {
      if (!quiet) {
        show_othello_bd(stdout,bd);
      }
      int x; int y;
      game_over = (play[(1-bd->turn)>>1])(bd,&x,&y,x,y,bd->turn == 1 ? depth1 : depth2);
      if (!quiet) {
        if (x != -1) {
          printf("Player %d's move: %d %d.\n",-bd->turn,x,y);
        } else {
          printf("Player %d passes.\n",-bd->turn);
        }
      }
      add_board(&lags_arr[game],bd);
    }
    if (!quiet) {
      show_othello_bd(stdout,bd);
    }
    double winner = score(bd);
    if (winner > 0) {
      printf("Player 1 wins!\n");
      if (game > ngames/2) {
        ++wins1backhalf;
      }
      ++wins1;
    } else if (winner < 0) {
      printf("Player -1 wins!\n");
      ++wins2;
      if (game > ngames/2) {
        ++wins2backhalf;
      }
    } else {
      printf("Tie!\n");
    }
    for (int k = 0; k < 2 ; ++k) {
      if (trees[k]) {
        free_minimax_tree(trees[k]);
        trees[k] = ((void *)0);
      }
    }
    for (int l = 0; l < 10 ; ++l) {
      for (int k = game >= 0 ? game : 0; k <= game ; ++k) {
        update_weights_from_game(&weights,&lags_arr[k],winner,1);
      }
    }
    printf("WEIGHTS: ");
    print_weights(&weights);
    printf("\nSCORE: 1: %d (%d); 2: %d (%d)\n",wins1,wins1backhalf,wins2,wins2backhalf);
  }
  free((void *)lags_arr);
  exit(0);
}

variable_types: argc int

variable_types:
argv
char **

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

variable_types: _Bool (*[2])(struct othello_bd { char board[8][8]; char turn; char just_passed; } *, int *, int *, int, int, int)

variable_types: ngames int

variable_types: c int

variable_types:
tail
char *

variable_types: k int

variable_types: struct learning_ai_game_state { int turns; struct board_counts { int turn; int squares_filled; int score; int counts[10]; } cts[-1]; } *

variable_types: game int

variable_types: x int

variable_types: y int

variable_types: winner double

variable_types: l int

user_defined_types: struct learning_ai_game_state { int turns; struct board_counts cts[-1]; }


original_code:
_Bool play_piece_or_legal_move (int play,othello_bd *bd,int x,int y) {
  if (bd->board[x][y]) {
    return 0;
  }
  int legal_move = 0;
  for (int x_dir = -1 ; x_dir <= 1 ; ++x_dir) {
    for (int y_dir = -1 ; y_dir <= 1 ; y_dir += (x_dir ? 1 : 2)) {
      int max_x = x;
      int max_y = y;
      _Bool on_board = 1;
      do {
        max_x += x_dir;
        max_y += y_dir;
      } while ((on_board = (max_x >= 0 && max_y >= 0 && max_x < 8 && max_y < 8)) && bd->board[max_x][max_y] == -bd->turn);
      if (on_board && bd->board[max_x][max_y] == bd->turn) {
        int cur_x = x+x_dir;
        int cur_y = y+y_dir;
        for ( ; cur_x*x_dir < max_x*x_dir || cur_y*y_dir < max_y*y_dir ; cur_x += x_dir, cur_y += y_dir) {
          if (play) {
            legal_move = 1;
            bd->board[cur_x][cur_y] = bd->turn;
          } else {
            return 1;
          }
        }
      }
    }
  }
  if (play && legal_move) {
    bd->just_passed = 0;
    bd->board[x][y] = bd->turn;
    bd->turn = -(bd->turn);
  }
  return legal_move;
}

canonical_original_code:
_Bool play_piece_or_legal_move(int play, struct othello_bd *bd, int x, int y) {
  if (bd->board[x][y]) {
    return 0;
  }
  int legal_move = 0;
  for (int x_dir = -1; x_dir <= 1 ; ++x_dir) {
    for (int y_dir = -1; y_dir <= 1 ; y_dir += (x_dir ? 1 : 2)) {
      int max_x = x;
      int max_y = y;
      _Bool on_board = 1;
      do {
        max_x += x_dir;
        max_y += y_dir;
      } while ((on_board = (max_x >= 0 && max_y >= 0 && max_x < 8 && max_y < 8)) && bd->board[max_x][max_y] == -bd->turn);
      if (on_board && bd->board[max_x][max_y] == bd->turn) {
        int cur_x = x+x_dir;
        int cur_y = y+y_dir;
        for ( ; cur_x*x_dir < max_x*x_dir || cur_y*y_dir < max_y*y_dir ; cur_x += x_dir, cur_y += y_dir) {
          if (play) {
            legal_move = 1;
            bd->board[cur_x][cur_y] = bd->turn;
          } else {
            return 1;
          }
        }
      }
    }
  }
  if (play && legal_move) {
    bd->just_passed = 0;
    bd->board[x][y] = bd->turn;
    bd->turn = -(bd->turn);
  }
  return legal_move;
}

variable_types: play int

variable_types: struct othello_bd { char board[8][8]; char turn; char just_passed; } *


decompiled_code:
__int64 __fastcall play_piece_if_legal(__int64 a1, signed int a2, int a3)
{
return (unsigned __int8)play_piece_or_legal_move(1, a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, signed int a2, int a3)
{
return (unsigned __int8)func0(1, a1, a2, a3);
}

original_code:
int play_piece_if_legal (othello_bd *bd,int x,int y) {
  return play_piece_or_legal_move (1,bd,x,y);
}

canonical_original_code:
int play_piece_if_legal(struct othello_bd *bd, int x, int y) {
  return play_piece_or_legal_move (1,bd,x,y);
}


decompiled_code:
__int64 __fastcall legal_move(__int64 a1, signed int a2, int a3)
{
return (unsigned __int8)play_piece_or_legal_move(0, a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, signed int a2, int a3)
{
return (unsigned __int8)func0(0, a1, a2, a3);
}

original_code:
int legal_move (othello_bd *bd,int x,int y) {
  return play_piece_or_legal_move(0,bd,x,y);
}

canonical_original_code:
int legal_move(struct othello_bd *bd, int x, int y) {
  return play_piece_or_legal_move(0,bd,x,y);
}


decompiled_code:
__int64 __fastcall have_legal_moves(__int64 a1)
{
signed int v1; // ebp
int v2; // ebx
__int64 result; // rax

v1 = 0;
LABEL_3:
v2 = 0;
while ( 1 )
{
result = legal_move(a1, v1, v2);
if ( (_DWORD)result )
return 1LL;
if ( ++v2 == 8 )
{
if ( ++v1 == 8 )
return result;
goto LABEL_3;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
signed int v1; // ebp
int v2; // ebx
__int64 result; // rax

v1 = 0;
LABEL_3:
v2 = 0;
while ( 1 )
{
result = func2(a1, v1, v2);
if ( (_DWORD)result )
return 1LL;
if ( ++v2 == 8 )
{
if ( ++v1 == 8 )
return result;
goto LABEL_3;
}
}
}

original_code:
int have_legal_moves (othello_bd *bd) {
  for (int i = 0 ; i < 8 ; ++i) {
    for (int j = 0 ; j < 8 ; ++j) {
      if (legal_move(bd,i,j)) {
        return 1;
      }
    }
  }
  return 0;
}

canonical_original_code:
int have_legal_moves(struct othello_bd *bd) {
  for (int i = 0; i < 8 ; ++i) {
    for (int j = 0; j < 8 ; ++j) {
      if (legal_move(bd,i,j)) {
        return 1;
      }
    }
  }
  return 0;
}

variable_types: i int

variable_types: j int


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

*(_BYTE *)(a1 + 64) = -*(_BYTE *)(a1 + 64);
result = 1LL;
if ( !*(_BYTE *)(a1 + 65) )
{
*(_BYTE *)(a1 + 65) = 1;
result = 0LL;
}
return result;
}

original_code:
int pass_turn_is_game_over (othello_bd *bd) {
  bd->turn = -(bd->turn);
  if (bd->just_passed) {
    return 1;
  } else {
    bd->just_passed = 1;
    return 0;
  }
}

canonical_original_code:
int pass_turn_is_game_over(struct othello_bd *bd) {
  bd->turn = -(bd->turn);
  if (bd->just_passed) {
    return 1;
  } else {
    bd->just_passed = 1;
    return 0;
  }
}


decompiled_code:
__int64 __fastcall random_move(__int64 a1, unsigned int *a2, unsigned int *a3)
{
unsigned int v4; // ebp
int v5; // er12
unsigned int i; // ebx
__int64 result; // rax
int v8; // eax
int v9; // edx
char v10; // [rsp+Fh] [rbp-49h]
struct timeval tv; // [rsp+10h] [rbp-48h] BYREF

gettimeofday(&tv, 0LL);
srand(tv.tv_usec);
v4 = 0;
v10 = 0;
v5 = 0;
do
{
for ( i = 0; i != 8; ++i )
{
result = legal_move(a1, v4, i);
if ( (_DWORD)result )
{
++v5;
v8 = rand();
v9 = v8 % v5;
result = (unsigned int)(v8 / v5);
v10 = 1;
if ( !v9 )
{
*a2 = v4;
*a3 = i;
}
}
}
++v4;
}
while ( v4 != 8 );
if ( !v10 )
{
*a2 = -1;
*a3 = -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int *a2, unsigned int *a3)
{
unsigned int v4; // ebp
int v5; // er12
unsigned int i; // ebx
__int64 result; // rax
int v8; // eax
int v9; // edx
char v10; // [rsp+Fh] [rbp-49h]
struct timeval tv; // [rsp+10h] [rbp-48h] BYREF

gettimeofday(&tv, 0LL);
srand(tv.tv_usec);
v4 = 0;
v10 = 0;
v5 = 0;
do
{
for ( i = 0; i != 8; ++i )
{
result = legal_move(a1, v4, i);
if ( (_DWORD)result )
{
++v5;
v8 = rand();
v9 = v8 % v5;
result = (unsigned int)(v8 / v5);
v10 = 1;
if ( !v9 )
{
*a2 = v4;
*a3 = i;
}
}
}
++v4;
}
while ( v4 != 8 );
if ( !v10 )
{
*a2 = -1;
*a3 = -1;
}
return result;
}

original_code:
void random_move (othello_bd *bd, int *x, int *y) {
  int sofar = 0;
  struct timeval cur;
  gettimeofday(&cur,((void *)0));
  srand(cur.tv_usec);
  _Bool has_move = 0;
  for (int i = 0 ; i < 8 ; ++i) {
    for (int j = 0 ; j < 8 ; ++j) {
      if (legal_move(bd,i,j)) {
        has_move = 1;
        ++sofar;
        int n = rand() % sofar;
        if (0 == n) {
          *x = i;
          *y = j;
        }
      }
    }
  }
  if (!has_move) {
    *x = -1;
    *y = -1;
  }
}

canonical_original_code:
void random_move(struct othello_bd *bd, int *x, int *y) {
  int sofar = 0;
  struct timeval cur;
  gettimeofday(&cur,((void *)0));
  srand(cur.tv_usec);
  _Bool has_move = 0;
  for (int i = 0; i < 8 ; ++i) {
    for (int j = 0; j < 8 ; ++j) {
      if (legal_move(bd,i,j)) {
        has_move = 1;
        ++sofar;
        int n = rand() % sofar;
        if (0 == n) {
          *x = i;
          *y = j;
        }
      }
    }
  }
  if (!has_move) {
    *x = -1;
    *y = -1;
  }
}

variable_types:
x
int *

variable_types:
y
int *

variable_types: sofar int

variable_types: n int

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


repo: ajraymond/restartd

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

if ( config_process_number > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
result = printf(
"ID=%d\n name=%s\n regexp=%s\n running=%s\n not_running=%s\n",
(unsigned int)v1++,
(const char *)(config_process + v0),
(const char *)(config_process + v0 + 64),
(const char *)(config_process + v0 + 448),
(const char *)(config_process + v0 + 192));
v0 += 992LL;
}
while ( config_process_number > v1 );
}
return result;
}

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

if ( config_process_number > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
result = printf(
"ID=%d\n name=%s\n regexp=%s\n running=%s\n not_running=%s\n",
(unsigned int)v1++,
(const char *)(config_process + v0),
(const char *)(config_process + v0 + 64),
(const char *)(config_process + v0 + 448),
(const char *)(config_process + v0 + 192));
v0 += 992LL;
}
while ( config_process_number > v1 );
}
return result;
}

original_code:
void dump_config(void) {
    int i;
    for(i=0; i<config_process_number; i++) {
        printf("ID=%d\n  name=%s\n  regexp=%s\n  running=%s\n  not_running=%s\n", i,
            config_process[i].name,
            config_process[i].regexp,
            config_process[i].running,
            config_process[i].not_running);
    }
}

canonical_original_code:
void dump_config() {
    int i;
    for(i=0; i<config_process_number; i++) {
        printf("ID=%d\n  name=%s\n  regexp=%s\n  running=%s\n  not_running=%s\n", i,
            config_process[i].name,
            config_process[i].regexp,
            config_process[i].running,
            config_process[i].not_running);
    }
}


decompiled_code:
void __fastcall got_signal(int a1)
{
if ( a1 == 15 )
{
syslog(6, "Got SIGTERM. Exiting.");
unlink("/var/run/restartd.pid");
exit(0);
}
if ( a1 == 1 )
{
syslog(6, "Got SIGHUP. Reloading config file.");
read_config();
syslog(6, "Config file has been read. Found %d process to check.", (unsigned int)config_process_number);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 == 15 )
{
syslog(6, "Got SIGTERM. Exiting.");
unlink("/var/run/restartd.pid");
exit(0);
}
if ( a1 == 1 )
{
syslog(6, "Got SIGHUP. Reloading config file.");
read_config();
syslog(6, "Config file has been read. Found %d process to check.", (unsigned int)config_process_number);
}
}

original_code:
void got_signal(int sig)
{
    if (sig == 15) {
        syslog(6, "Got SIGTERM. Exiting.");
        unlink("/var/run/restartd.pid");
        exit(0);
    }
    if (sig == 1) {
        syslog(6, "Got SIGHUP. Reloading config file.");
        read_config();
        syslog(6,
               "Config file has been read. Found %d process to check.",
               config_process_number);
    }
}

variable_types: sig int


repo: ajrisi/algorithms

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( result )
result = **(_QWORD **)(a1 + 16);
return result;
}

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( result )
result = **(_QWORD **)(a1 + 16);
return result;
}

original_code:
void *dqueue_front(dqueue *q)
{
  if(q == ((void *)0)) {
    return ((void *)0);
  }
  if(q->head == ((void *)0)) {
    return ((void *)0);
  }
  return q->tail->data;
}

canonical_original_code:
void *dqueue_front(struct dqueue_s *q)
{
  if(q == ((void *)0)) {
    return ((void *)0);
  }
  if(q->head == ((void *)0)) {
    return ((void *)0);
  }
  return q->tail->data;
}

variable_types: struct dqueue_s { unsigned int size; struct queue_item_s { void *data; struct queue_item_s *prev; } *head; struct queue_item_s *tail; void *(*itemdup_fn)(void *); void (*itemfree_fn)(void *); } *

return_type: void *

user_defined_types: struct dqueue_s { unsigned int size; struct queue_item_s *head; struct queue_item_s *tail; void *(*itemdup_fn)(void *); void (*itemfree_fn)(void *); }


original_code:
void *dqueue_dequeue(dqueue *q)
{
  queue_item *oldtail;
  void *oldtail_data;
  if(q == ((void *)0)) {
    return ((void *)0);
  }
  if(q->tail == ((void *)0)) {
    return ((void *)0);
  }
  oldtail = q->tail;
  q->tail = oldtail->prev;
  oldtail_data = oldtail->data;
  free(oldtail);
  q->size--;
  if(q->tail == ((void *)0)) {
    q->head = ((void *)0);
  }
  return oldtail_data;
}

canonical_original_code:
void *dqueue_dequeue(struct dqueue_s *q)
{
  struct queue_item_s *oldtail;
  void *oldtail_data;
  if(q == ((void *)0)) {
    return ((void *)0);
  }
  if(q->tail == ((void *)0)) {
    return ((void *)0);
  }
  oldtail = q->tail;
  q->tail = oldtail->prev;
  oldtail_data = oldtail->data;
  free(oldtail);
  q->size--;
  if(q->tail == ((void *)0)) {
    q->head = ((void *)0);
  }
  return oldtail_data;
}

variable_types: struct queue_item_s { void *data; struct queue_item_s *prev; } *


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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *a1;
else
result = 0LL;
return result;
}

original_code:
unsigned int dqueue_size(dqueue *q)
{
  if(q == ((void *)0)) {
    return 0;
  }
  return q->size;
}

canonical_original_code:
unsigned int dqueue_size(struct dqueue_s *q)
{
  if(q == ((void *)0)) {
    return 0;
  }
  return q->size;
}

return_type: unsigned int

user_defined_types: struct queue_item_s { void *data; struct queue_item_s *prev; }


decompiled_code:
_BOOL8 __fastcall dqueue_isempty(_DWORD *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *a1 == 0;
else
result = 1LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_DWORD *a1)
{
_BOOL8 result; // rax

if ( a1 )
result = *a1 == 0;
else
result = 1LL;
return result;
}

original_code:
int dqueue_isempty(dqueue *q)
{
  if(q == ((void *)0)) {
    return 1;
  }
  return (q->size == 0);
}

canonical_original_code:
int dqueue_isempty(struct dqueue_s *q)
{
  if(q == ((void *)0)) {
    return 1;
  }
  return (q->size == 0);
}


decompiled_code:
void __fastcall dqueue_free(void *ptr)
{
__int64 v2; // rdi
void (*v3)(void); // rax

if ( ptr )
{
while ( !dqueue_isempty(ptr) )
{
v2 = dqueue_dequeue((__int64)ptr);
v3 = (void (*)(void))*((_QWORD *)ptr + 4);
if ( v2 )
{
if ( v3 )
v3();
}
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
__int64 v2; // rdi
void (*v3)(void); // rax

if ( ptr )
{
while ( !func3(ptr) )
{
v2 = func1((__int64)ptr);
v3 = (void (*)(void))*((_QWORD *)ptr + 4);
if ( v2 )
{
if ( v3 )
v3();
}
}
free(ptr);
}
}

original_code:
void dqueue_free(dqueue *q)
{
  void *tmp;
  if(q == ((void *)0)) {
    return;
  }
  while(!dqueue_isempty(q)) {
    tmp = dqueue_dequeue(q);
    if((q->itemfree_fn != ((void *)0)) &&
       (tmp != ((void *)0))) {
      q->itemfree_fn(tmp);
    }
  }
  free(q);
}

canonical_original_code:
void dqueue_free(struct dqueue_s *q)
{
  void *tmp;
  if(q == ((void *)0)) {
    return;
  }
  while(!dqueue_isempty(q)) {
    tmp = dqueue_dequeue(q);
    if((q->itemfree_fn != ((void *)0)) &&
       (tmp != ((void *)0))) {
      q->itemfree_fn(tmp);
    }
  }
  free(q);
}

variable_types:
tmp
void *


variable_types: struct prng_s { unsigned long long u; unsigned long long v; unsigned long long w; } *

variable_types:
x
unsigned long long


decompiled_code:
double __fastcall prng_d(__int64 *a1)
{
__int64 v1; // rax
double v2; // xmm0_8

v1 = prng_ll(a1);
if ( v1 < 0 )
v2 = (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1)) + (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1));
else
v2 = (double)(int)v1;
return v2 * 5.421010862427522e-20;
}

canonical_original_code:
double prng_d(struct prng_s *r)
{
  return 5.42101086242752217E-20 * prng_ll(r);
}

user_defined_types: struct prng_s { unsigned long long u; unsigned long long v; unsigned long long w; }


decompiled_code:
// attributes: thunk
__int64 __fastcall prng_ui(__int64 *a1)
{
return prng_ll(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 *a1)
{
return func0(a1);
}

original_code:
unsigned int prng_ui(prng *r)
{
  return (unsigned int)prng_ll(r);
}

canonical_original_code:
unsigned int prng_ui(struct prng_s *r)
{
  return (unsigned int)prng_ll(r);
}


decompiled_code:
void __fastcall prng_free(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void prng_free(prng *r)
{
  if(r == ((void *)0)) {
    return;
  }
  free(r);
}

canonical_original_code:
void prng_free(struct prng_s *r)
{
  if(r == ((void *)0)) {
    return;
  }
  free(r);
}


decompiled_code:
__int64 __fastcall row_col_to_1d(int a1, int a2, int a3)
{
return (unsigned int)(a3 * a1 + a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, int a3)
{
return (unsigned int)(a3 * a1 + a2);
}

original_code:
static int row_col_to_1d(int row, int col, int width)
{
  return(row*width)+col;
}

variable_types: row int

variable_types: col int

variable_types: width int


decompiled_code:
__int64 __fastcall graph_isfull(int *a1)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a1 )
result = (unsigned int)find_next_free_vertex(a1) == -1;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a1 )
result = (unsigned int)func1(a1) == -1;
return result;
}

original_code:
int graph_isfull(graph *g)
{
  if(g == ((void *)0)) {
    return -1;
  }
  return (find_next_free_vertex(g) == -1);
}

canonical_original_code:
int graph_isfull(struct graph_s *g)
{
  if(g == ((void *)0)) {
    return -1;
  }
  return (find_next_free_vertex(g) == -1);
}

variable_types: struct graph_s { int max_verticies; void **edges; void **verticies; int nedges; int nverticies; int next_free_vertex; void *(*dupedge_fn)(void *); void *(*dupvertex_fn)(void *); void (*freeedge_fn)(void *); void (*freevertex_fn)(void *); } *


original_code:
int graph_add_vertex(graph *g, void *vertex)
{
  int v_index;
  void *add_vertex;
  if(g == ((void *)0)) {
    return -1;
  }
  v_index = find_next_free_vertex(g);
  if(v_index == -1) {
    return -1;
  }
  if(g->dupvertex_fn != ((void *)0)) {
    add_vertex = g->dupvertex_fn(vertex);
  } else {
    add_vertex = vertex;
  }
  g->verticies[v_index] = add_vertex;
  return g->nverticies++;
}

canonical_original_code:
int graph_add_vertex(struct graph_s *g, void *vertex)
{
  int v_index;
  void *add_vertex;
  if(g == ((void *)0)) {
    return -1;
  }
  v_index = find_next_free_vertex(g);
  if(v_index == -1) {
    return -1;
  }
  if(g->dupvertex_fn != ((void *)0)) {
    add_vertex = g->dupvertex_fn(vertex);
  } else {
    add_vertex = vertex;
  }
  g->verticies[v_index] = add_vertex;
  return g->nverticies++;
}

variable_types:
vertex
void *

user_defined_types: struct graph_s { int max_verticies; void **edges; void **verticies; int nedges; int nverticies; int next_free_vertex; void *(*dupedge_fn)(void *); void *(*dupvertex_fn)(void *); void (*freeedge_fn)(void *); void (*freevertex_fn)(void *); }


decompiled_code:
__int64 __fastcall graph_get_vertex(__int64 a1, int a2)
{
if ( !a1 )
return 0LL;
if ( *(_DWORD *)a1 <= a2 )
return 0LL;
return *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
if ( !a1 )
return 0LL;
if ( *(_DWORD *)a1 <= a2 )
return 0LL;
return *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a2);
}

original_code:
void *graph_get_vertex(graph *g, int vertex)
{
  if(g == ((void *)0)) {
    return ((void *)0);
  }
  if(vertex >= g->max_verticies) {
    return ((void *)0);
  }
  return g->verticies[vertex];
}

canonical_original_code:
void *graph_get_vertex(struct graph_s *g, int vertex)
{
  if(g == ((void *)0)) {
    return ((void *)0);
  }
  if(vertex >= g->max_verticies) {
    return ((void *)0);
  }
  return g->verticies[vertex];
}

variable_types: vertex int


decompiled_code:
__int64 __fastcall graph_nedges_dst(__int64 a1, int a2)
{
int v2; // ebp
__int64 v3; // r14
unsigned int v4; // er12
int i; // ebx

if ( !a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 <= a2 )
return (unsigned int)-1;
if ( v2 <= 0 )
return 0;
v3 = *(_QWORD *)(a1 + 8);
v4 = 0;
for ( i = 0; i != v2; ++i )
v4 -= (*(_QWORD *)(v3 + 8LL * (int)row_col_to_1d(i, a2, v2)) == 0LL) - 1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
int v2; // ebp
__int64 v3; // r14
unsigned int v4; // er12
int i; // ebx

if ( !a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 <= a2 )
return (unsigned int)-1;
if ( v2 <= 0 )
return 0;
v3 = *(_QWORD *)(a1 + 8);
v4 = 0;
for ( i = 0; i != v2; ++i )
v4 -= (*(_QWORD *)(v3 + 8LL * (int)func0(i, a2, v2)) == 0LL) - 1;
return v4;
}

original_code:
int graph_nedges_dst(graph *g, int vertex)
{
  int i;
  int edge_index;
  int total_edges;
  if(g == ((void *)0)) {
    return -1;
  }
  if(vertex >= g->max_verticies) {
    return -1;
  }
  total_edges = 0;
  for(i = 0; i < g->max_verticies; i++) {
    edge_index = row_col_to_1d(i, vertex, g->max_verticies);
    if(g->edges[edge_index] != ((void *)0)) {
      total_edges++;
    }
  }
  return total_edges;
}

canonical_original_code:
int graph_nedges_dst(struct graph_s *g, int vertex)
{
  int i;
  int edge_index;
  int total_edges;
  if(g == ((void *)0)) {
    return -1;
  }
  if(vertex >= g->max_verticies) {
    return -1;
  }
  total_edges = 0;
  for(i = 0; i < g->max_verticies; i++) {
    edge_index = row_col_to_1d(i, vertex, g->max_verticies);
    if(g->edges[edge_index] != ((void *)0)) {
      total_edges++;
    }
  }
  return total_edges;
}

variable_types: i int


decompiled_code:
__int64 __fastcall graph_nedges_src(__int64 a1, int a2)
{
int v2; // ebp
__int64 v3; // r14
unsigned int v4; // er12
int i; // ebx

if ( !a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 <= a2 )
return (unsigned int)-1;
if ( v2 <= 0 )
return 0;
v3 = *(_QWORD *)(a1 + 8);
v4 = 0;
for ( i = 0; i != v2; ++i )
v4 -= (*(_QWORD *)(v3 + 8LL * (int)row_col_to_1d(a2, i, v2)) == 0LL) - 1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
int v2; // ebp
__int64 v3; // r14
unsigned int v4; // er12
int i; // ebx

if ( !a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 <= a2 )
return (unsigned int)-1;
if ( v2 <= 0 )
return 0;
v3 = *(_QWORD *)(a1 + 8);
v4 = 0;
for ( i = 0; i != v2; ++i )
v4 -= (*(_QWORD *)(v3 + 8LL * (int)func0(a2, i, v2)) == 0LL) - 1;
return v4;
}

original_code:
int graph_nedges_src(graph *g, int vertex)
{
  int i;
  int edge_index;
  int total_edges;
  if(g == ((void *)0)) {
    return -1;
  }
  if(vertex >= g->max_verticies) {
    return -1;
  }
  total_edges = 0;
  for(i = 0; i < g->max_verticies; i++) {
    edge_index = row_col_to_1d(vertex, i, g->max_verticies);
    if(g->edges[edge_index] != ((void *)0)) {
      total_edges++;
    }
  }
  return total_edges;
}

canonical_original_code:
int graph_nedges_src(struct graph_s *g, int vertex)
{
  int i;
  int edge_index;
  int total_edges;
  if(g == ((void *)0)) {
    return -1;
  }
  if(vertex >= g->max_verticies) {
    return -1;
  }
  total_edges = 0;
  for(i = 0; i < g->max_verticies; i++) {
    edge_index = row_col_to_1d(vertex, i, g->max_verticies);
    if(g->edges[edge_index] != ((void *)0)) {
      total_edges++;
    }
  }
  return total_edges;
}


decompiled_code:
__int64 __fastcall graph_nedges(int *a1, int a2)
{
int v2; // er12

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *a1 <= a2 )
return 0xFFFFFFFFLL;
v2 = graph_nedges_src((__int64)a1, a2);
return v2 + (unsigned int)graph_nedges_dst((__int64)a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int *a1, int a2)
{
int v2; // er12

if ( !a1 )
return 0xFFFFFFFFLL;
if ( *a1 <= a2 )
return 0xFFFFFFFFLL;
v2 = func6((__int64)a1, a2);
return v2 + (unsigned int)func5((__int64)a1, a2);
}

original_code:
int graph_nedges(graph *g, int vertex)
{
  int total_edges;
  if(g == ((void *)0)) {
    return -1;
  }
  if(vertex >= g->max_verticies) {
    return -1;
  }
  total_edges = 0;
  total_edges += graph_nedges_src(g, vertex);
  total_edges += graph_nedges_dst(g, vertex);
  return total_edges;
}

canonical_original_code:
int graph_nedges(struct graph_s *g, int vertex)
{
  int total_edges;
  if(g == ((void *)0)) {
    return -1;
  }
  if(vertex >= g->max_verticies) {
    return -1;
  }
  total_edges = 0;
  total_edges += graph_nedges_src(g, vertex);
  total_edges += graph_nedges_dst(g, vertex);
  return total_edges;
}


decompiled_code:
__int64 __fastcall graph_get_edge_dst(_DWORD *a1, int a2)
{
unsigned int v2; // edx

if ( a1 )
v2 = a2 % *a1;
else
v2 = -1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1, int a2)
{
unsigned int v2; // edx

if ( a1 )
v2 = a2 % *a1;
else
v2 = -1;
return v2;
}

original_code:
int graph_get_edge_dst(graph *g, int edge_index)
{
  if(g == ((void *)0)) {
    return -1;
  }
  return edge_index % g->max_verticies;
}

canonical_original_code:
int graph_get_edge_dst(struct graph_s *g, int edge_index)
{
  if(g == ((void *)0)) {
    return -1;
  }
  return edge_index % g->max_verticies;
}


decompiled_code:
__int64 __fastcall graph_get_edge_src(_DWORD *a1, int a2)
{
__int64 result; // rax

if ( a1 )
result = (unsigned int)((int)(a2 - graph_get_edge_dst(a1, a2)) / *a1);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_DWORD *a1, int a2)
{
__int64 result; // rax

if ( a1 )
result = (unsigned int)((int)(a2 - func8(a1, a2)) / *a1);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int graph_get_edge_src(graph *g, int edge_index)
{
  if(g == ((void *)0)) {
    return -1;
  }
  return (edge_index - graph_get_edge_dst(g, edge_index)) / g->max_verticies;
}

canonical_original_code:
int graph_get_edge_src(struct graph_s *g, int edge_index)
{
  if(g == ((void *)0)) {
    return -1;
  }
  return (edge_index - graph_get_edge_dst(g, edge_index)) / g->max_verticies;
}


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

if ( !a1 )
return 0LL;
v2 = graph_get_edge_src((_DWORD *)a1, a2);
result = graph_get_vertex(a1, v2);
if ( result )
{
v4 = graph_get_edge_dst((_DWORD *)a1, a2);
result = graph_get_vertex(a1, v4);
if ( result )
{
if ( *(_DWORD *)a1 * *(_DWORD *)a1 <= a2 )
result = 0LL;
else
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
}
}
return result;
}

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

if ( !a1 )
return 0LL;
v2 = func9((_DWORD *)a1, a2);
result = func4(a1, v2);
if ( result )
{
v4 = func8((_DWORD *)a1, a2);
result = func4(a1, v4);
if ( result )
{
if ( *(_DWORD *)a1 * *(_DWORD *)a1 <= a2 )
result = 0LL;
else
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
}
}
return result;
}

original_code:
void *graph_get_edge(graph *g, int edge_index)
{
  if(g == ((void *)0)) {
    return ((void *)0);
  }
  if((graph_get_vertex(g, graph_get_edge_src(g, edge_index)) == ((void *)0)) ||
     (graph_get_vertex(g, graph_get_edge_dst(g, edge_index)) == ((void *)0))) {
    return ((void *)0);
  }
  if(edge_index >= (g->max_verticies)*(g->max_verticies)) {
    return ((void *)0);
  }
  return g->edges[edge_index];
}

canonical_original_code:
void *graph_get_edge(struct graph_s *g, int edge_index)
{
  if(g == ((void *)0)) {
    return ((void *)0);
  }
  if((graph_get_vertex(g, graph_get_edge_src(g, edge_index)) == ((void *)0)) ||
     (graph_get_vertex(g, graph_get_edge_dst(g, edge_index)) == ((void *)0))) {
    return ((void *)0);
  }
  if(edge_index >= (g->max_verticies)*(g->max_verticies)) {
    return ((void *)0);
  }
  return g->edges[edge_index];
}


decompiled_code:
void __fastcall graph_remove_edge(__int64 a1, int a2)
{
int v3; // eax
int v4; // eax
__int64 v5; // rdi
void (__fastcall *v6)(__int64); // rax

if ( a1 )
{
v3 = graph_get_edge_src((_DWORD *)a1, a2);
if ( graph_get_vertex(a1, v3) )
{
v4 = graph_get_edge_dst((_DWORD *)a1, a2);
if ( graph_get_vertex(a1, v4) )
{
v5 = graph_get_edge(a1, a2);
if ( v5 )
{
v6 = *(void (__fastcall **)(__int64))(a1 + 56);
if ( v6 )
v6(v5);
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2) = 0LL;
--*(_DWORD *)(a1 + 24);
}
}
}
}
}

original_code:
void graph_remove_edge(graph *g, int edge_index)
{
  void *edge_to_remove;
  if(g == ((void *)0)) {
    return;
  }
  if((graph_get_vertex(g, graph_get_edge_src(g, edge_index)) == ((void *)0)) ||
     (graph_get_vertex(g, graph_get_edge_dst(g, edge_index)) == ((void *)0))) {
    return;
  }
  edge_to_remove = graph_get_edge(g, edge_index);
  if(edge_to_remove == ((void *)0)) {
    return;
  }
  if(g->freeedge_fn != ((void *)0)) {
    g->freeedge_fn(edge_to_remove);
  }
  g->edges[edge_index] = ((void *)0);
  g->nedges--;
}

canonical_original_code:
void graph_remove_edge(struct graph_s *g, int edge_index)
{
  void *edge_to_remove;
  if(g == ((void *)0)) {
    return;
  }
  if((graph_get_vertex(g, graph_get_edge_src(g, edge_index)) == ((void *)0)) ||
     (graph_get_vertex(g, graph_get_edge_dst(g, edge_index)) == ((void *)0))) {
    return;
  }
  edge_to_remove = graph_get_edge(g, edge_index);
  if(edge_to_remove == ((void *)0)) {
    return;
  }
  if(g->freeedge_fn != ((void *)0)) {
    g->freeedge_fn(edge_to_remove);
  }
  g->edges[edge_index] = ((void *)0);
  g->nedges--;
}


original_code:
int graph_add_edge(graph *g, void *edge_data, int vertex_from, int vertex_to)
{
  int edge_index;
  void *edge_data_copy;
  if(g == ((void *)0)) {
    return -1;
  }
  if((graph_get_vertex(g, vertex_from) == ((void *)0)) ||
     (graph_get_vertex(g, vertex_to) == ((void *)0))) {
    return -1;
  }
  edge_index = row_col_to_1d(vertex_from, vertex_to, g->max_verticies);
  if(g->dupedge_fn != ((void *)0)) {
    edge_data_copy = g->dupedge_fn(edge_data);
  } else {
    edge_data_copy = edge_data;
  }
  if(g->edges[edge_index] != ((void *)0)) {
    graph_remove_edge(g, edge_index);
  }
  g->edges[edge_index] = edge_data_copy;
  g->nedges++;
  return edge_index;
}

canonical_original_code:
int graph_add_edge(struct graph_s *g, void *edge_data, int vertex_from, int vertex_to)
{
  int edge_index;
  void *edge_data_copy;
  if(g == ((void *)0)) {
    return -1;
  }
  if((graph_get_vertex(g, vertex_from) == ((void *)0)) ||
     (graph_get_vertex(g, vertex_to) == ((void *)0))) {
    return -1;
  }
  edge_index = row_col_to_1d(vertex_from, vertex_to, g->max_verticies);
  if(g->dupedge_fn != ((void *)0)) {
    edge_data_copy = g->dupedge_fn(edge_data);
  } else {
    edge_data_copy = edge_data;
  }
  if(g->edges[edge_index] != ((void *)0)) {
    graph_remove_edge(g, edge_index);
  }
  g->edges[edge_index] = edge_data_copy;
  g->nedges++;
  return edge_index;
}


decompiled_code:
void __fastcall graph_remove_vertex(__int64 a1, int a2)
{
int v2; // edx
int v3; // ebp
int v4; // eax
int v5; // eax
void (__fastcall *v6)(__int64); // rbp
__int64 v7; // rax

if ( a1 )
{
v2 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 > a2 )
{
if ( *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a2) )
{
if ( v2 > 0 )
{
v3 = 0;
do
{
v4 = row_col_to_1d(a2, v3, v2);
graph_remove_edge(a1, v4);
v5 = row_col_to_1d(v3, a2, *(_DWORD *)a1);
graph_remove_edge(a1, v5);
++v3;
v2 = *(_DWORD *)a1;
}
while ( *(_DWORD *)a1 > v3 );
}
v6 = *(void (__fastcall **)(__int64))(a1 + 64);
if ( v6 )
{
v7 = graph_get_vertex(a1, a2);
v6(v7);
}
*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a2) = 0LL;
--*(_DWORD *)(a1 + 28);
}
}
}
}

original_code:
void graph_remove_vertex(graph *g, int vertex)
{
  int i;
  if(g == ((void *)0)) {
    return;
  }
  if(vertex >= g->max_verticies) {
    return;
  }
  if(g->verticies[vertex] == ((void *)0)) {
    return;
  }
  for(i = 0; i < g->max_verticies; i++) {
    graph_remove_edge(g, row_col_to_1d(vertex, i, g->max_verticies));
    graph_remove_edge(g, row_col_to_1d(i, vertex, g->max_verticies));
  }
  if(g->freevertex_fn != ((void *)0)) {
    g->freevertex_fn(graph_get_vertex(g, vertex));
  }
  g->verticies[vertex] = ((void *)0);
  g->nverticies--;
}

canonical_original_code:
void graph_remove_vertex(struct graph_s *g, int vertex)
{
  int i;
  if(g == ((void *)0)) {
    return;
  }
  if(vertex >= g->max_verticies) {
    return;
  }
  if(g->verticies[vertex] == ((void *)0)) {
    return;
  }
  for(i = 0; i < g->max_verticies; i++) {
    graph_remove_edge(g, row_col_to_1d(vertex, i, g->max_verticies));
    graph_remove_edge(g, row_col_to_1d(i, vertex, g->max_verticies));
  }
  if(g->freevertex_fn != ((void *)0)) {
    g->freevertex_fn(graph_get_vertex(g, vertex));
  }
  g->verticies[vertex] = ((void *)0);
  g->nverticies--;
}