strcmp

decompiled_code:
__int64 __fastcall strcmp(unsigned __int8 *a1, unsigned __int8 *a2)
{
unsigned __int8 *v4; // rsi
unsigned __int8 v5; // al
bool v6; // cf
__int64 result; // rax

v4 = a1;
while ( 1 )
{
v5 = *v4++;
v6 = *a2 < v5;
if ( *a2++ != v5 )
break;
if ( !v5 )
return 0LL;
}
result = (unsigned int)-v6;
LOBYTE(result) = v6 ? -1 : 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1, unsigned __int8 *a2)
{
unsigned __int8 *v4; // rsi
unsigned __int8 v5; // al
bool v6; // cf
__int64 result; // rax

v4 = a1;
while ( 1 )
{
v5 = *v4++;
v6 = *a2 < v5;
if ( *a2++ != v5 )
break;
if ( !v5 )
return 0LL;
}
result = (unsigned int)-v6;
LOBYTE(result) = v6 ? -1 : 1;
return result;
}

original_code:
static inline int strcmp(const char * cs,const char * ct)
{
int d0, d1;
register int __res;
__asm__ __volatile__(
 "1:\tlodsb\n\t"
 "scasb\n\t"
 "jne 2f\n\t"
 "testb %%al,%%al\n\t"
 "jne 1b\n\t"
 "xorl %%eax,%%eax\n\t"
 "jmp 3f\n"
 "2:\tsbbl %%eax,%%eax\n\t"
 "orb $1,%%al\n"
 "3:"
 :"=a" (__res), "=&S" (d0), "=&D" (d1)
       :"1" (cs),"2" (ct));
return __res;
}

canonical_original_code:
static inline int strcmp(char *cs, char *ct)
{
int d0; int d1;
register int __res;
__asm__ __volatile__(
 "1:\tlodsb\n\t"
 "scasb\n\t"
 "jne 2f\n\t"
 "testb %%al,%%al\n\t"
 "jne 1b\n\t"
 "xorl %%eax,%%eax\n\t"
 "jmp 3f\n"
 "2:\tsbbl %%eax,%%eax\n\t"
 "orb $1,%%al\n"
 "3:"
 :"=a" (__res), "=&S" (d0), "=&D" (d1)
       :"1" (cs),"2" (ct));
return __res;
}

variable_types:
cs
char *

variable_types:
ct
char *


main

canonical_name: main

repo: mreiferson/libevbuffsock

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
unsigned __int64 v4; // rcx

v3 = new_buffer(0x10uLL);
buffer_add(v3, "asdf", 4LL);
buffer_add(v3, "1234567890", 10LL);
buffer_add(v3, "ghjk", 4LL);
buffer_drain(v3, 8LL);
buffer_add(v3, "1234567890", 10LL);
buffer_add(v3, "!@#$%^", 6LL);
if ( *(_QWORD *)(v3 + 24) != 32LL )
__assert_fail("buf->length == 32", "test.c", 0x11u, "main");
v4 = 27LL;
if ( *(_QWORD *)(v3 + 16) <= 0x1BuLL )
v4 = *(_QWORD *)(v3 + 16);
if ( memcmp(&unk_402032, *(const void **)v3, v4) )
__assert_fail("strncmp(\"567890ghjk1234567890!@#$%^\", buf->data, buf->offset) == 0", "test.c", 0x12u, "main");
return 0;
}

original_code:
int main(int argc, char **argv)
{
    struct Buffer *buf;
    buf = new_buffer(16, 64);
    buffer_add(buf, "asdf", 4);
    buffer_add(buf, "1234567890", 10);
    buffer_add(buf, "ghjk", 4);
    buffer_drain(buf, 8);
    buffer_add(buf, "1234567890", 10);
    buffer_add(buf, "!@#$%^", 6);
    ((void) sizeof ((buf->length == 32) ? 1 : 0), __extension__ ({ if (buf->length == 32) ; else __assert_fail ("buf->length == 32", "test.c", 17, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((strncmp("567890ghjk1234567890!@#$%^", buf->data, buf->offset) == 0) ? 1 : 0), __extension__ ({ if (strncmp("567890ghjk1234567890!@#$%^", buf->data, buf->offset) == 0) ; else __assert_fail ("strncmp(\"567890ghjk1234567890!@#$%^\", buf->data, buf->offset) == 0", "test.c", 18, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct Buffer { char *data; char *orig; unsigned long offset; unsigned long length; unsigned long capacity; } *

return_type: int


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

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

original_code:
void free_buffer(struct Buffer *buf)
{
    if (buf) {
        free(buf->orig);
        free(buf);
    }
}

return_type: void

user_defined_types: struct Buffer { char *data; char *orig; unsigned long offset; unsigned long length; unsigned long capacity; }


decompiled_code:
__int64 __fastcall buffer_expand(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // r13
unsigned __int64 i; // rbx
size_t v4; // rdx
size_t v5; // rbx
__int64 result; // rax
char *v7; // rax

v2 = *(_QWORD *)a1 - *(_QWORD *)(a1 + 8);
fprintf(stdout, "%s: need %lu, pos %lu\n", "buffer_expand", a2, v2);
if ( v2 >= a2 )
{
fprintf(stdout, "%s: re-aligning\n", "buffer_expand");
memmove(*(void **)(a1 + 8), *(const void **)a1, *(_QWORD *)(a1 + 16));
*(_QWORD *)a1 = *(_QWORD *)(a1 + 8);
result = 1LL;
}
else
{
for ( i = *(_QWORD *)(a1 + 24); a2 > i; i *= 2LL )
;
fprintf(stdout, "%s: expanding by %lu\n", "buffer_expand", i);
v4 = *(_QWORD *)(a1 + 32);
v5 = *(_QWORD *)(a1 + 24) + i;
if ( v4 >= v5 || (result = 0LL, !v4) )
{
v7 = (char *)realloc(*(void **)(a1 + 8), v5);
*(_QWORD *)(a1 + 24) = v5;
*(_QWORD *)(a1 + 8) = v7;
*(_QWORD *)a1 = &v7[v2];
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // r13
unsigned __int64 i; // rbx
size_t v4; // rdx
size_t v5; // rbx
__int64 result; // rax
char *v7; // rax

v2 = *(_QWORD *)a1 - *(_QWORD *)(a1 + 8);
fprintf(stdout, "%s: need %lu, pos %lu\n", "buffer_expand", a2, v2);
if ( v2 >= a2 )
{
fprintf(stdout, "%s: re-aligning\n", "buffer_expand");
memmove(*(void **)(a1 + 8), *(const void **)a1, *(_QWORD *)(a1 + 16));
*(_QWORD *)a1 = *(_QWORD *)(a1 + 8);
result = 1LL;
}
else
{
for ( i = *(_QWORD *)(a1 + 24); a2 > i; i *= 2LL )
;
fprintf(stdout, "%s: expanding by %lu\n", "buffer_expand", i);
v4 = *(_QWORD *)(a1 + 32);
v5 = *(_QWORD *)(a1 + 24) + i;
if ( v4 >= v5 || (result = 0LL, !v4) )
{
v7 = (char *)realloc(*(void **)(a1 + 8), v5);
*(_QWORD *)(a1 + 24) = v5;
*(_QWORD *)(a1 + 8) = v7;
*(_QWORD *)a1 = &v7[v2];
result = 1LL;
}
}
return result;
}

original_code:
int buffer_expand(struct Buffer *buf, size_t need)
{
    size_t pos = buf->data - buf->orig;
    size_t expand = 0;
    size_t new_size = 0;
    fprintf(stdout, "%s: need %lu, pos %lu\n", __FUNCTION__, need, pos);
    if (need <= pos) {
        fprintf(stdout, "%s: re-aligning\n", __FUNCTION__);
        memmove(buf->orig, buf->data, buf->offset);
        buf->data = buf->orig;
        return 1;
    }
    expand = buf->length;
    while (expand < need) {
        expand = expand * 2;
    }
    fprintf(stdout, "%s: expanding by %lu\n", __FUNCTION__, expand);
    new_size = buf->length + expand;
    if (buf->capacity > 0 && new_size > buf->capacity) {
        return 0;
    }
    buf->orig = realloc(buf->orig, new_size);
    buf->data = buf->orig + pos;
    buf->length = new_size;
    return 1;
}

canonical_original_code:
int buffer_expand(struct Buffer *buf, unsigned long need)
{
    unsigned long pos = buf->data - buf->orig;
    unsigned long expand = 0;
    unsigned long new_size = 0;
    fprintf(stdout, "%s: need %lu, pos %lu\n", __FUNCTION__, need, pos);
    if (need <= pos) {
        fprintf(stdout, "%s: re-aligning\n", __FUNCTION__);
        memmove(buf->orig, buf->data, buf->offset);
        buf->data = buf->orig;
        return 1;
    }
    expand = buf->length;
    while (expand < need) {
        expand = expand * 2;
    }
    fprintf(stdout, "%s: expanding by %lu\n", __FUNCTION__, expand);
    new_size = buf->length + expand;
    if (buf->capacity > 0 && new_size > buf->capacity) {
        return 0;
    }
    buf->orig = realloc(buf->orig, new_size);
    buf->data = buf->orig + pos;
    buf->length = new_size;
    return 1;
}

variable_types:
need
unsigned long

variable_types:
pos
unsigned long

variable_types:
expand
unsigned long


decompiled_code:
__int64 __fastcall buffer_add(__int64 a1, const char *a2, size_t a3)
{
__int64 v4; // r8
int v5; // ebp
__int64 result; // rax

v4 = *(_QWORD *)(a1 + 16) + *(_QWORD *)a1 - *(_QWORD *)(a1 + 8);
v5 = v4 + a3 - *(_DWORD *)(a1 + 24);
fprintf(stdout, "%s: adding %lu - used %lu, need %d\n", "buffer_add", a3, v4, (unsigned int)v5);
if ( v5 <= 0 || (result = buffer_expand(a1, v5), (_DWORD)result) )
{
memcpy((void *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 16)), a2, a3);
*(_QWORD *)(a1 + 16) += a3;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, const char *a2, size_t a3)
{
__int64 v4; // r8
int v5; // ebp
__int64 result; // rax

v4 = *(_QWORD *)(a1 + 16) + *(_QWORD *)a1 - *(_QWORD *)(a1 + 8);
v5 = v4 + a3 - *(_DWORD *)(a1 + 24);
fprintf(stdout, "%s: adding %lu - used %lu, need %d\n", "buffer_add", a3, v4, (unsigned int)v5);
if ( v5 <= 0 || (result = func9(a1, v5), (_DWORD)result) )
{
memcpy((void *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 16)), a2, a3);
*(_QWORD *)(a1 + 16) += a3;
result = 1LL;
}
return result;
}

original_code:
int buffer_add(struct Buffer *buf, void *source, size_t length)
{
    size_t used = buf->data - buf->orig + buf->offset;
    int32_t need = used + length - buf->length;
    fprintf(stdout, "%s: adding %lu - used %lu, need %d\n", __FUNCTION__, length, used, need);
    if (need > 0) {
        if (!buffer_expand(buf, need)) {
            return 0;
        }
    }
    memcpy(buf->data + buf->offset, source, length);
    buf->offset += length;
    return 1;
}

canonical_original_code:
int buffer_add(struct Buffer *buf, void *source, unsigned long length)
{
    unsigned long used = buf->data - buf->orig + buf->offset;
    int need = used + length - buf->length;
    fprintf(stdout, "%s: adding %lu - used %lu, need %d\n", __FUNCTION__, length, used, need);
    if (need > 0) {
        if (!buffer_expand(buf, need)) {
            return 0;
        }
    }
    memcpy(buf->data + buf->offset, source, length);
    buf->offset += length;
    return 1;
}

variable_types:
source
void *

variable_types:
length
unsigned long

variable_types:
used
unsigned long

variable_types: need int


original_code:
int buffer_read_fd(struct Buffer *buf, int fd)
{
    int n;
    int need;
    if (ioctl(fd, 0x541B, &n) == -1 || n > 4096) {
        n = 4096;
    }
    need = n - ((buf)->length - ((buf)->data - (buf)->orig + (buf)->offset));
    if (need > 0 && !buffer_expand(buf, need)) {
        return -1;
    }
    n = recv(fd, buf->data + buf->offset, n, 0);
    if (n > 0) {
        buf->offset += n;
    }
    return n;
}

variable_types: fd int

variable_types: n int


decompiled_code:
__int64 __fastcall buffer_write_fd(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx

v2 = send(a2, *(const void **)a1, *(_QWORD *)(a1 + 16), 0);
v3 = v2;
if ( v2 > 0 )
buffer_drain((_QWORD *)a1, v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx

v2 = send(a2, *(const void **)a1, *(_QWORD *)(a1 + 16), 0);
v3 = v2;
if ( v2 > 0 )
func11((_QWORD *)a1, v2);
return v3;
}

original_code:
int buffer_write_fd(struct Buffer *buf, int fd)
{
    int n;
    n = send(fd, buf->data, buf->offset, 0);
    if (n > 0) {
        buffer_drain(buf, n);
    }
    return n;
}


decompiled_code:
_BOOL8 __fastcall is_valid_account_owner(_BYTE *a1)
{
_BOOL8 result; // rax

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

canonical_decompiled_code:
_BOOL8 __fastcall func7(_BYTE *a1)
{
_BOOL8 result; // rax

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

original_code:
int is_valid_account_owner (char* o)
{
    return o != ((void *)0) && strlen (o) > 0;
}

canonical_original_code:
int is_valid_account_owner(char *o)
{
    return o != ((void *)0) && strlen (o) > 0;
}

variable_types:
o
char *


decompiled_code:
_BOOL8 __fastcall is_valid_account_number(char *s)
{
_BOOL8 result; // rax
__int64 v2; // rbx
const unsigned __int16 *v3; // rdx
__int64 i; // rax
__int64 v5; // r12

result = 0LL;
if ( s )
{
v2 = *s;
if ( (_BYTE)v2 )
{
v3 = *__ctype_b_loc();
for ( i = 0LL; ; v2 = s[i] )
{
v5 = (int)i++;
if ( (v3[v2] & 0x800) == 0 )
break;
}
result = v5 == strlen(s);
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(char *s)
{
_BOOL8 result; // rax
__int64 v2; // rbx
const unsigned __int16 *v3; // rdx
__int64 i; // rax
__int64 v5; // r12

result = 0LL;
if ( s )
{
v2 = *s;
if ( (_BYTE)v2 )
{
v3 = *__ctype_b_loc();
for ( i = 0LL; ; v2 = s[i] )
{
v5 = (int)i++;
if ( (v3[v2] & 0x800) == 0 )
break;
}
result = v5 == strlen(s);
}
}
return result;
}

original_code:
int is_valid_account_number (char* n)
{
    int result = n != ((void *)0) && strlen (n) > 0;
    if (result)
    {
        int i = 0;
        while (((*__ctype_b_loc ())[(int) ((n[i]))] & (unsigned short int) _ISdigit))
            i++;
        result = i == strlen (n);
    }
    return result;
}

canonical_original_code:
int is_valid_account_number(char *n)
{
    int result = n != ((void *)0) && strlen (n) > 0;
    if (result)
    {
        int i = 0;
        while (((*__ctype_b_loc ())[(int) ((n[i]))] & (unsigned short) _ISdigit))
            i++;
        result = i == strlen (n);
    }
    return result;
}

variable_types:
n
char *

variable_types: result int

variable_types: i int


decompiled_code:
_BOOL8 __fastcall is_valid_account_balance(double a1)
{
return a1 > 0.0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(double a1)
{
return a1 > 0.0;
}

original_code:
int is_valid_account_balance (double b)
{
    return b > 0;
}

canonical_original_code:
int is_valid_account_balance(double b)
{
    return b > 0;
}

variable_types: b double


decompiled_code:
double *__fastcall create_account(void *src, char *s, double a3)
{
double *v3; // r13
size_t v4; // r14
void *v5; // rax
size_t v6; // r14
void *v7; // rax

v3 = 0LL;
if ( is_valid_account_owner(src) && is_valid_account_number(s) && is_valid_account_balance(a3) )
{
v3 = (double *)malloc(0x18uLL);
v4 = strlen((const char *)src) + 1;
v5 = malloc(v4);
*(_QWORD *)v3 = v5;
memcpy(v5, src, v4);
v6 = strlen(s) + 1;
v7 = malloc(v6);
*((_QWORD *)v3 + 1) = v7;
memcpy(v7, s, v6);
v3[2] = a3;
}
return v3;
}

canonical_decompiled_code:
double *__fastcall func10(void *src, char *s, double a3)
{
double *v3; // r13
size_t v4; // r14
void *v5; // rax
size_t v6; // r14
void *v7; // rax

v3 = 0LL;
if ( func7(src) && func8(s) && func9(a3) )
{
v3 = (double *)malloc(0x18uLL);
v4 = strlen((const char *)src) + 1;
v5 = malloc(v4);
*(_QWORD *)v3 = v5;
memcpy(v5, src, v4);
v6 = strlen(s) + 1;
v7 = malloc(v6);
*((_QWORD *)v3 + 1) = v7;
memcpy(v7, s, v6);
v3[2] = a3;
}
return v3;
}

original_code:
account* create_account (char* account_owner, char* account_number, double account_balance)
{
    account* a = ((void *)0);
    if (is_valid_account_owner (account_owner) && is_valid_account_number (account_number)
      && is_valid_account_balance (account_balance))
    {
        a = malloc (sizeof(account));
        a -> owner = malloc ((strlen (account_owner) + 1) * sizeof(char));
        strcpy (a -> owner, account_owner);
        a -> number = malloc ((strlen (account_number) + 1) * sizeof(char));
        strcpy (a -> number, account_number);
        a -> balance = account_balance;
    }
    return a;
}

canonical_original_code:
struct account *create_account(char *account_owner, char *account_number, double account_balance)
{
    struct account *a = ((void *)0);
    if (is_valid_account_owner (account_owner) && is_valid_account_number (account_number)
      && is_valid_account_balance (account_balance))
    {
        a = malloc (sizeof(account));
        a -> owner = malloc ((strlen (account_owner) + 1) * sizeof(char));
        strcpy (a -> owner, account_owner);
        a -> number = malloc ((strlen (account_number) + 1) * sizeof(char));
        strcpy (a -> number, account_number);
        a -> balance = account_balance;
    }
    return a;
}

variable_types: struct account { char *owner; char *number; double balance; } *


decompiled_code:
int __fastcall display_account(__int64 a1)
{
int result; // eax

if ( a1 )
{
printf("Account owner: %s\n", *(const char **)a1);
printf("Account number: %s\n", *(const char **)(a1 + 8));
result = printf("Account balance: %g\n", *(double *)(a1 + 16));
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1)
{
int result; // eax

if ( a1 )
{
printf("Account owner: %s\n", *(const char **)a1);
printf("Account number: %s\n", *(const char **)(a1 + 8));
result = printf("Account balance: %g\n", *(double *)(a1 + 16));
}
return result;
}

original_code:
void display_account (account* acc)
{
    if (acc != ((void *)0))
    {
        printf ("Account owner: %s\n", acc -> owner);
        printf ("Account number: %s\n", acc -> number);
        printf ("Account balance: %g\n", acc -> balance);
    }
}

canonical_original_code:
void display_account(struct account *acc)
{
    if (acc != ((void *)0))
    {
        printf ("Account owner: %s\n", acc -> owner);
        printf ("Account number: %s\n", acc -> number);
        printf ("Account balance: %g\n", acc -> balance);
    }
}

user_defined_types: struct account { char *owner; char *number; double balance; }


deposit

decompiled_code:
__int64 __fastcall deposit(__int64 a1, double a2)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
{
if ( a2 > 0.0 )
{
result = 1LL;
*(double *)(a1 + 16) = a2 + *(double *)(a1 + 16);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, double a2)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
{
if ( a2 > 0.0 )
{
result = 1LL;
*(double *)(a1 + 16) = a2 + *(double *)(a1 + 16);
}
}
return result;
}

original_code:
int deposit (account* acc, double account_deposit)
{
    int returnme = 0;
    if (acc != ((void *)0) && account_deposit > 0)
    {
        returnme = 1;
        acc -> balance += account_deposit;
    }
    return returnme;
}

canonical_original_code:
int deposit(struct account *acc, double account_deposit)
{
    int returnme = 0;
    if (acc != ((void *)0) && account_deposit > 0)
    {
        returnme = 1;
        acc -> balance += account_deposit;
    }
    return returnme;
}

variable_types: returnme int


withdraw

decompiled_code:
__int64 __fastcall withdraw(__int64 a1, double a2)
{
__int64 result; // rax
double v3; // xmm1_8

result = 0LL;
if ( a1 && a2 > 0.0 )
{
v3 = *(double *)(a1 + 16);
if ( v3 >= a2 )
{
result = 1LL;
*(double *)(a1 + 16) = v3 - a2;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, double a2)
{
__int64 result; // rax
double v3; // xmm1_8

result = 0LL;
if ( a1 && a2 > 0.0 )
{
v3 = *(double *)(a1 + 16);
if ( v3 >= a2 )
{
result = 1LL;
*(double *)(a1 + 16) = v3 - a2;
}
}
return result;
}

original_code:
int withdraw (account* acc, double account_withdraw)
{
    int returnme = 0;
    if (acc != ((void *)0) && account_withdraw > 0 && acc -> balance >= account_withdraw)
    {
        returnme = 1;
        acc -> balance -= account_withdraw;
    }
    return returnme;
}

canonical_original_code:
int withdraw(struct account *acc, double account_withdraw)
{
    int returnme = 0;
    if (acc != ((void *)0) && account_withdraw > 0 && acc -> balance >= account_withdraw)
    {
        returnme = 1;
        acc -> balance -= account_withdraw;
    }
    return returnme;
}


decompiled_code:
void __fastcall delete_account(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}
}

original_code:
void delete_account (account* acc)
{
    if (acc != ((void *)0))
    {
        if (acc -> owner != ((void *)0))
          free (acc -> owner);
        if (acc -> number != ((void *)0))
          free (acc -> number);
        free (acc);
    }
}

canonical_original_code:
void delete_account(struct account *acc)
{
    if (acc != ((void *)0))
    {
        if (acc -> owner != ((void *)0))
          free (acc -> owner);
        if (acc -> number != ((void *)0))
          free (acc -> number);
        free (acc);
    }
}


decompiled_code:
void __fastcall __noreturn err_exit(const char *a1)
{
puts(a1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
puts(a1);
exit(0);
}

original_code:
void err_exit (const char* message)
{
 printf ("%s\n", message);
 exit(0);
}

canonical_original_code:
void err_exit(char *message)
{
 printf ("%s\n", message);
 exit(0);
}

variable_types:
message
char *


decompiled_code:
__int64 __fastcall file_size(const char *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
int v4; // eax
char buf[34]; // [rsp+6h] [rbp-22h] BYREF

v1 = open(a1, 0, 420LL);
if ( v1 < 0 )
err_exit("error in opening file for count");
v2 = v1;
v3 = 0;
do
{
v4 = read(v2, buf, 0xAuLL);
if ( v4 < 0 )
err_exit("read error in count");
v3 += v4;
}
while ( v4 );
close(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
int v4; // eax
char buf[34]; // [rsp+6h] [rbp-22h] BYREF

v1 = open(a1, 0, 420LL);
if ( v1 < 0 )
func7("error in opening file for count");
v2 = v1;
v3 = 0;
do
{
v4 = read(v2, buf, 0xAuLL);
if ( v4 < 0 )
func7("read error in count");
v3 += v4;
}
while ( v4 );
close(v2);
return v3;
}

original_code:
int file_size(char* fileName)
{
 int buffSize = 10;
 int fd1 = open (fileName, 00, 0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3));
 if (fd1 < 0)
  err_exit ("error in opening file for count");
 char buf [buffSize];
 int numbytes = 0;
 int size = 0;
 do
 {
  size = read (fd1, buf, buffSize);
  if (size < 0)
   err_exit ("read error in count");
  else
   numbytes += size;
 }
 while (size);
 close (fd1);
 return numbytes;
}

canonical_original_code:
int file_size(char *fileName)
{
 int buffSize = 10;
 int fd1 = open (fileName, 00, 0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3));
 if (fd1 < 0)
  err_exit ("error in opening file for count");
 char buf[-1];
 int numbytes = 0;
 int size = 0;
 do
 {
  size = read (fd1, buf, buffSize);
  if (size < 0)
   err_exit ("read error in count");
  else
   numbytes += size;
 }
 while (size);
 close (fd1);
 return numbytes;
}

variable_types:
fileName
char *

variable_types: buffSize int

variable_types:
buf
char [-1]

variable_types: numbytes int

variable_types: size int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
int v4; // eax
int v5; // ebp
int v6; // er12
__off_t v7; // rbx
__off_t v8; // r13
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = file_size("gbu.txt", argv, envp);
v4 = open("gbu.txt", 0, 420LL);
if ( v4 < 0 )
err_exit("error in opening file for read");
v5 = v4;
v6 = creat("copy.txt", 0x1A4u);
if ( v6 < 0 )
err_exit("error in creating file for write");
v7 = (int)v3 - 1LL;
v8 = v7 - v3;
while ( v7 != v8 )
{
if ( (int)lseek(v5, v7, 0) < 0 )
err_exit("error in seek");
if ( (int)read(v5, buf, 1uLL) < 0 )
err_exit("read error");
--v7;
if ( (int)write(v6, buf, 1uLL) < 0 )
err_exit("write error");
}
close(v5);
close(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
int v4; // eax
int v5; // ebp
int v6; // er12
__off_t v7; // rbx
__off_t v8; // r13
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = func8("gbu.txt", argv, envp);
v4 = open("gbu.txt", 0, 420LL);
if ( v4 < 0 )
func7("error in opening file for read");
v5 = v4;
v6 = creat("copy.txt", 0x1A4u);
if ( v6 < 0 )
func7("error in creating file for write");
v7 = (int)v3 - 1LL;
v8 = v7 - v3;
while ( v7 != v8 )
{
if ( (int)lseek(v5, v7, 0) < 0 )
func7("error in seek");
if ( (int)read(v5, buf, 1uLL) < 0 )
func7("read error");
--v7;
if ( (int)write(v6, buf, 1uLL) < 0 )
func7("write error");
}
close(v5);
close(v6);
return 0;
}

original_code:
int main ()
{
 char* inputFile = "gbu.txt";
 char* outputFile = "copy.txt";
 int numbytes = file_size(inputFile);
 int fd1 = open (inputFile, 00, 0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3));
 if (fd1 < 0)
  err_exit ("error in opening file for read");
 int fd2 = creat (outputFile, 0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3));
 if (fd2 < 0)
  err_exit ("error in creating file for write");
 char buf [1];
 while(numbytes--)
 {
  int result = lseek (fd1, numbytes, 0);
  if (result < 0)
   err_exit ("error in seek");
  result = read (fd1, buf, 1);
  if (result < 0)
   err_exit ("read error");
  result = write (fd2, buf, 1);
  if (result < 0)
   err_exit ("write error");
 }
 close (fd1);
 close (fd2);
 return 0;
}

canonical_original_code:
int main()
{
 char *inputFile = "gbu.txt";
 char *outputFile = "copy.txt";
 int numbytes = file_size(inputFile);
 int fd1 = open (inputFile, 00, 0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3));
 if (fd1 < 0)
  err_exit ("error in opening file for read");
 int fd2 = creat (outputFile, 0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3));
 if (fd2 < 0)
  err_exit ("error in creating file for write");
 char buf[1];
 while(numbytes--)
 {
  int result = lseek (fd1, numbytes, 0);
  if (result < 0)
   err_exit ("error in seek");
  result = read (fd1, buf, 1);
  if (result < 0)
   err_exit ("read error");
  result = write (fd2, buf, 1);
  if (result < 0)
   err_exit ("write error");
 }
 close (fd1);
 close (fd2);
 return 0;
}

variable_types:
inputFile
char *

variable_types:
outputFile
char *

variable_types:
buf
char [1]


repo: mrpeachum/checkers

original_code:
void print_board(bitboard *board)
{
  int i, before = 1, mask, n_rows = 0;;
  printf(" _______________ \t%s\n", board_pos_str[n_rows++]);
  for (i = 0; i < 32; i++) {
    mask = 1<<i;
    if (i%4 == 0) {
      printf("|");
    }
    if (before) printf("  ");
    printf("%c", board[1]&mask ? (board[2]&mask ? 'B' : 'b') :
         (board[0]&mask ? (board[2]&mask ? 'W' : 'w') : ' '));
    if (!before) printf("  ");
    if ((i+1)%4 == 0) {
      printf("|\t%s\n", board_pos_str[n_rows++]);
      before = 1 - before;
    }
    else
      printf(" ");
  }
  printf(" --------------- \t%s\n", board_pos_str[n_rows++]);
}

canonical_original_code:
void print_board(unsigned int *board)
{
  int i; int before = 1; int mask; int n_rows = 0;;
  printf(" _______________ \t%s\n", board_pos_str[n_rows++]);
  for (i = 0; i < 32; i++) {
    mask = 1<<i;
    if (i%4 == 0) {
      printf("|");
    }
    if (before) printf("  ");
    printf("%c", board[1]&mask ? (board[2]&mask ? 'B' : 'b') :
         (board[0]&mask ? (board[2]&mask ? 'W' : 'w') : ' '));
    if (!before) printf("  ");
    if ((i+1)%4 == 0) {
      printf("|\t%s\n", board_pos_str[n_rows++]);
      before = 1 - before;
    }
    else
      printf(" ");
  }
  printf(" --------------- \t%s\n", board_pos_str[n_rows++]);
}

variable_types:
board
unsigned int *

variable_types: before int

variable_types: mask int


original_code:
bool trans_string_move(bitboard *board, struct move *move,
         char *move_str, bool color)
{
  int from, to;
  bitboard from_board, to_board, jump_board;
  bitboard empty, move_up, move_down;
  int high_row, low_row;
  int dir;
  char *p;
  move->board[0] = board[0];
  move->board[1] = board[1];
  move->board[2] = board[2];
  p = move_str;
  while (p && sscanf(p, "%d-%d", &from, &to) == 2) {
    empty = ~(move->board[1] | move->board[0]);
    move_up = color ? move->board[2] : 0xffffffff;
    move_down = !color ? move->board[2] : 0xffffffff;
    from--;
    to--;
    if (from > to) {
      high_row = from / 4;
      low_row = to / 4;
      dir = 1;
    }
    else {
      high_row = to / 4;
      low_row = from / 4;
      dir = 2;
    }
    from_board = 1<<from;
    to_board = 1<<to;
    jump_board = 0;
    if (high_row - low_row == 2) {
      if (dir == 1) {
        if (to % 4 > from % 4)
          jump_board = (((((from_board) & 0xf0f0f0f0) >> up_shift[(0)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) >> up_shift[(0)][1]) & 0xf0f0f0f0));
        else
          jump_board = (((((from_board) & 0xf0f0f0f0) >> up_shift[(1)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) >> up_shift[(1)][1]) & 0xf0f0f0f0));
       } else {
         if (to % 4 > from % 4)
           jump_board = (((((from_board) & 0xf0f0f0f0) << down_shift[(1)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) << down_shift[(1)][1]) & 0xf0f0f0f0));
         else
           jump_board = (((((from_board) & 0xf0f0f0f0) << down_shift[(0)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) << down_shift[(0)][1]) & 0xf0f0f0f0));
       }
      }
      else if (high_row - low_row != 1)
        return 0;
      if (!(move->board[(int)color] & from_board))
        return 0;
      if (!(empty & to_board))
        return 0;
      if (jump_board && !(move->board[(int)!color] & jump_board))
        return 0;
      if (dir == 1) {
        if (!(move->board[(int)color] & move_up))
          return 0;
      }
      else {
        if (!(move->board[(int)color] & move_down))
          return 0;
      }
      move->board[(int)color] |= to_board;
      move->board[(int)color] &= ~from_board;
      move->board[(int)!color] &= ~jump_board;
      move->board[2] &= ~jump_board;
      if (move->board[2] & from_board) {
        move->board[2] |= to_board;
        move->board[2] &= ~from_board;
      }
      else
        move->board[2] |= king_bits[(int)color] & to_board;
      p = strchr(p, '-') + 1;
   }
   return 1;
}

canonical_original_code:
char trans_string_move(unsigned int *board, struct move *move, char *move_str, char color)
{
  int from; int to;
  unsigned int from_board; unsigned int to_board; unsigned int jump_board;
  unsigned int empty; unsigned int move_up; unsigned int move_down;
  int high_row; int low_row;
  int dir;
  char *p;
  move->board[0] = board[0];
  move->board[1] = board[1];
  move->board[2] = board[2];
  p = move_str;
  while (p && sscanf(p, "%d-%d", &from, &to) == 2) {
    empty = ~(move->board[1] | move->board[0]);
    move_up = color ? move->board[2] : 0xffffffff;
    move_down = !color ? move->board[2] : 0xffffffff;
    from--;
    to--;
    if (from > to) {
      high_row = from / 4;
      low_row = to / 4;
      dir = 1;
    }
    else {
      high_row = to / 4;
      low_row = from / 4;
      dir = 2;
    }
    from_board = 1<<from;
    to_board = 1<<to;
    jump_board = 0;
    if (high_row - low_row == 2) {
      if (dir == 1) {
        if (to % 4 > from % 4)
          jump_board = (((((from_board) & 0xf0f0f0f0) >> up_shift[(0)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) >> up_shift[(0)][1]) & 0xf0f0f0f0));
        else
          jump_board = (((((from_board) & 0xf0f0f0f0) >> up_shift[(1)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) >> up_shift[(1)][1]) & 0xf0f0f0f0));
       } else {
         if (to % 4 > from % 4)
           jump_board = (((((from_board) & 0xf0f0f0f0) << down_shift[(1)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) << down_shift[(1)][1]) & 0xf0f0f0f0));
         else
           jump_board = (((((from_board) & 0xf0f0f0f0) << down_shift[(0)][0]) & 0x0f0f0f0f) | ((((from_board) & 0x0f0f0f0f) << down_shift[(0)][1]) & 0xf0f0f0f0));
       }
      }
      else if (high_row - low_row != 1)
        return 0;
      if (!(move->board[(int)color] & from_board))
        return 0;
      if (!(empty & to_board))
        return 0;
      if (jump_board && !(move->board[(int)!color] & jump_board))
        return 0;
      if (dir == 1) {
        if (!(move->board[(int)color] & move_up))
          return 0;
      }
      else {
        if (!(move->board[(int)color] & move_down))
          return 0;
      }
      move->board[(int)color] |= to_board;
      move->board[(int)color] &= ~from_board;
      move->board[(int)!color] &= ~jump_board;
      move->board[2] &= ~jump_board;
      if (move->board[2] & from_board) {
        move->board[2] |= to_board;
        move->board[2] &= ~from_board;
      }
      else
        move->board[2] |= king_bits[(int)color] & to_board;
      p = strchr(p, '-') + 1;
   }
   return 1;
}

variable_types: struct move { unsigned int board[3]; } *

variable_types: color char

variable_types: from int

variable_types: to int

variable_types:
empty
unsigned int

variable_types: dir int

variable_types:
p
char *

return_type: char


decompiled_code:
__int64 __fastcall trans_move_string_recur(int a1, unsigned int *a2)
{
__int64 result; // rax
char v3; // r8
_DWORD *v4; // r12
unsigned int *v5; // rbp
_DWORD *v6; // r14
int v8; // edi
unsigned int v9; // eax
int *v10; // r12
char v11; // r8
int v12; // esi
unsigned int v13; // edi
int v14; // eax

result = 1LL;
if ( a1 )
{
v3 = 4;
v4 = &down_shift;
v5 = a2;
v6 = a2 + 1;
v8 = *(a2 - 1);
LOBYTE(a2) = 3;
while ( 1 )
{
v9 = ((v8 & 0xF0F0F0F) << v3) & 0xF0F0F0F0 | ((v8 & 0xF0F0F0F0) << (char)a2) & 0xF0F0F0F;
if ( (a1 & v9) != 0 )
{
*v5 = ((v9 & 0xF0F0F0F) << v3) & 0xF0F0F0F0 | ((v9 & 0xF0F0F0F0) << (char)a2) & 0xF0F0F0F;
v14 = trans_move_string_recur(a1 ^ v9, v6);
if ( v14 )
goto LABEL_14;
v8 = *(v5 - 1);
}
v4 += 2;
if ( &up_shift == (_UNKNOWN *)v4 )
break;
LODWORD(a2) = *v4;
v3 = 5;
}
v10 = (int *)&up_shift;
v11 = 3;
LOBYTE(v12) = 4;
while ( 1 )
{
v13 = ((v8 & 0xF0F0F0Fu) >> v11) & 0xF0F0F0F0 | ((v8 & 0xF0F0F0F0) >> v12) & 0xF0F0F0F;
if ( (a1 & v13) != 0 )
{
*v5 = ((v13 & 0xF0F0F0F) >> v11) & 0xF0F0F0F0 | ((v13 & 0xF0F0F0F0) >> v12) & 0xF0F0F0F;
v14 = trans_move_string_recur(a1 ^ v13, v5 + 1);
if ( v14 )
break;
}
v10 += 2;
if ( king_bits == v10 )
return 0LL;
v8 = *(v5 - 1);
v12 = *v10;
v11 = 4;
}
LABEL_14:
result = (unsigned int)(v14 + 1);
}
return result;
}

variable_types:
diff
unsigned int

variable_types:
pos
unsigned int *

variable_types: i int

variable_types: n int


decompiled_code:
__int64 __fastcall trans_move_string(unsigned int *a1, __int64 a2, char *a3, char a4)
{
unsigned int v5; // edx
unsigned int v6; // eax
int v7; // er9
unsigned int v8; // edi
int v9; // edx
int v10; // ebx
__int64 i; // rbp
__int64 v12; // rdx
__int64 result; // rax
int v14; // er9
unsigned int v15; // [rsp+0h] [rbp-48h]
unsigned int v16[17]; // [rsp+4h] [rbp-44h] BYREF

v5 = a1[a4];
v6 = v5 & ~*(_DWORD *)(a2 + 4LL * a4);
v7 = ~v5 & *(_DWORD *)(a2 + 4LL * a4);
v15 = v6;
if ( !a4 )
++a1;
v8 = *a1;
v9 = *(_DWORD *)(a2 + 4LL * (a4 == 0));
if ( v8 == v9 )
{
v16[0] = v7;
v10 = 1;
LABEL_5:
for ( i = 0LL; ; v6 = *(&v15 + i) )
{
v12 = 0LL;
if ( v6 )
{
do
{
v12 = (unsigned int)(v12 + 1);
v6 >>= 1;
}
while ( v6 );
*(&v15 + i) = 0;
}
if ( i )
{
++i;
result = sprintf(a3, "-%d", v12);
a3 += (int)result;
if ( v10 < (int)i )
return result;
}
else
{
i = 1LL;
result = sprintf(a3, "%d", v12);
a3 += (int)result;
if ( v10 < 1 )
return result;
}
}
}
v10 = trans_move_string_recur(v9 ^ v8, v16) - 1;
result = v10;
*(&v15 + v10) = v14;
if ( v10 >= 0 )
{
v6 = v15;
goto LABEL_5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1, __int64 a2, char *a3, char a4)
{
unsigned int v5; // edx
unsigned int v6; // eax
int v7; // er9
unsigned int v8; // edi
int v9; // edx
int v10; // ebx
__int64 i; // rbp
__int64 v12; // rdx
__int64 result; // rax
int v14; // er9
unsigned int v15; // [rsp+0h] [rbp-48h]
unsigned int v16[17]; // [rsp+4h] [rbp-44h] BYREF

v5 = a1[a4];
v6 = v5 & ~*(_DWORD *)(a2 + 4LL * a4);
v7 = ~v5 & *(_DWORD *)(a2 + 4LL * a4);
v15 = v6;
if ( !a4 )
++a1;
v8 = *a1;
v9 = *(_DWORD *)(a2 + 4LL * (a4 == 0));
if ( v8 == v9 )
{
v16[0] = v7;
v10 = 1;
LABEL_5:
for ( i = 0LL; ; v6 = *(&v15 + i) )
{
v12 = 0LL;
if ( v6 )
{
do
{
v12 = (unsigned int)(v12 + 1);
v6 >>= 1;
}
while ( v6 );
*(&v15 + i) = 0;
}
if ( i )
{
++i;
result = sprintf(a3, "-%d", v12);
a3 += (int)result;
if ( v10 < (int)i )
return result;
}
else
{
i = 1LL;
result = sprintf(a3, "%d", v12);
a3 += (int)result;
if ( v10 < 1 )
return result;
}
}
}
v10 = func3(v9 ^ v8, v16) - 1;
result = v10;
*(&v15 + v10) = v14;
if ( v10 >= 0 )
{
v6 = v15;
goto LABEL_5;
}
return result;
}

original_code:
void trans_move_string(bitboard *board, struct move *move,
         char *move_str, bool color)
{
  bitboard pos[10];
  int n_pos = 1, i, n;
  bitboard from, to, diff;
  from = (board[(int)color] ^ move->board[(int)color]) & board[(int)color];
  to = (board[(int)color] ^ move->board[(int)color]) & move->board[(int)color];
  diff = board[!color] ^ move->board[!color];
  pos[0] = from;
  if (diff)
    n_pos += trans_move_string_recur(diff, pos + 1) - 2;
  pos[n_pos++] = to;
  for (i = 0; i < n_pos; i++) {
    n = 0;
    while (pos[i]) {
      n++;
      pos[i] = pos[i] >> 1;
    }
    move_str += ((i == 0) ? sprintf(move_str, "%d", n) : sprintf(move_str, "-%d", n));
  }
}

canonical_original_code:
void trans_move_string(unsigned int *board, struct move *move, char *move_str, char color)
{
  unsigned int pos[10];
  int n_pos = 1; int i; int n;
  unsigned int from; unsigned int to; unsigned int diff;
  from = (board[(int)color] ^ move->board[(int)color]) & board[(int)color];
  to = (board[(int)color] ^ move->board[(int)color]) & move->board[(int)color];
  diff = board[!color] ^ move->board[!color];
  pos[0] = from;
  if (diff)
    n_pos += trans_move_string_recur(diff, pos + 1) - 2;
  pos[n_pos++] = to;
  for (i = 0; i < n_pos; i++) {
    n = 0;
    while (pos[i]) {
      n++;
      pos[i] = pos[i] >> 1;
    }
    move_str += ((i == 0) ? sprintf(move_str, "%d", n) : sprintf(move_str, "-%d", n));
  }
}

variable_types:
pos
unsigned int [10]

variable_types:
from
unsigned int

variable_types:
to
unsigned int


decompiled_code:
int __fastcall print_move_list(unsigned int *a1, char a2, _DWORD *a3, int a4)
{
int v6; // ebp
__int64 v8; // rsi
int result; // eax
char v10[168]; // [rsp-A8h] [rbp-A8h] BYREF

if ( a4 > 0 )
{
v6 = 0;
do
{
printf("move option #%d\n", (unsigned int)++v6);
print_board(a3);
v8 = (__int64)a3;
a3 += 3;
trans_move_string(a1, v8, v10, a2);
result = puts(v10);
}
while ( v6 != a4 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1, char a2, _DWORD *a3, int a4)
{
int v6; // ebp
__int64 v8; // rsi
int result; // eax
char v10[168]; // [rsp-A8h] [rbp-A8h] BYREF

if ( a4 > 0 )
{
v6 = 0;
do
{
printf("move option #%d\n", (unsigned int)++v6);
func1(a3);
v8 = (__int64)a3;
a3 += 3;
func4(a1, v8, v10, a2);
result = puts(v10);
}
while ( v6 != a4 );
}
return result;
}

original_code:
void print_move_list(bitboard *board, bool color, struct move *move_list, int n)
{
  char buf[128];
  int i;
  for (i = 0; i < n; i++) {
    printf("move option #%d\n", i+1);
    print_board(move_list[i].board);
    trans_move_string(board, &move_list[i], buf, color);
    printf("%s\n", buf);
  }
}

canonical_original_code:
void print_move_list(unsigned int *board, char color, struct move *move_list, int n)
{
  char buf[128];
  int i;
  for (i = 0; i < n; i++) {
    printf("move option #%d\n", i+1);
    print_board(move_list[i].board);
    trans_move_string(board, &move_list[i], buf, color);
    printf("%s\n", buf);
  }
}

variable_types:
board
unsigned int *

variable_types:
buf
char [128]


decompiled_code:
__int64 __fastcall valid_move_input(char *a1)
{
char v1; // al
char *v2; // rdi

v1 = *a1;
v2 = a1 + 1;
do
{
if ( (unsigned __int8)(v1 - 48) > 9u && v1 != 45 )
return 0LL;
v1 = *v2++;
}
while ( v1 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1)
{
char v1; // al
char *v2; // rdi

v1 = *a1;
v2 = a1 + 1;
do
{
if ( (unsigned __int8)(v1 - 48) > 9u && v1 != 45 )
return 0LL;
v1 = *v2++;
}
while ( v1 );
return 1LL;
}

original_code:
int valid_move_input(const char *input) {
  int c = 0;
  char l = input[c];
  while (1) {
    if ((l <= '9' && l >= '0') || l == '-') {
      c++;
      l = input[c];
      if (l == 0)
 break;
    }
    else {
      return 0;
    }
  }
  return 1;
}

canonical_original_code:
int valid_move_input(char *input) {
  int c = 0;
  char l = input[c];
  while (1) {
    if ((l <= '9' && l >= '0') || l == '-') {
      c++;
      l = input[c];
      if (l == 0)
 break;
    }
    else {
      return 0;
    }
  }
  return 1;
}

variable_types:
input
char *

variable_types: c int

variable_types: l char


okay

decompiled_code:
__int64 okay()
{
char v0; // al
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v0 = v2[0];
do
{
if ( (v0 == 110 || v0 == 121) && v0 )
break;
printf("OPERATOR: Is the board correct? [y/n] ");
fflush(stdout);
gets(v2);
v0 = v2[0];
if ( v2[0] == 121 )
return 1LL;
}
while ( v2[0] != 110 );
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
char v0; // al
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

v0 = v2[0];
do
{
if ( (v0 == 110 || v0 == 121) && v0 )
break;
printf("OPERATOR: Is the board correct? [y/n] ");
fflush(stdout);
gets(v2);
v0 = v2[0];
if ( v2[0] == 121 )
return 1LL;
}
while ( v2[0] != 110 );
return 0LL;
}

original_code:
bool okay() {
  char inpt[1];
  while((inpt[0] != 'y' && inpt[0] != 'n') || inpt[0] == 0) {
    printf("OPERATOR: Is the board correct? [y/n] ");
    fflush(stdout);
    gets(inpt);
    if (inpt[0] == 'y')
      return 1;
    if (inpt[0] == 'n')
      return 0;
  }
  return 0;
}

canonical_original_code:
char okay() {
  char inpt[1];
  while((inpt[0] != 'y' && inpt[0] != 'n') || inpt[0] == 0) {
    printf("OPERATOR: Is the board correct? [y/n] ");
    fflush(stdout);
    gets(inpt);
    if (inpt[0] == 'y')
      return 1;
    if (inpt[0] == 'n')
      return 0;
  }
  return 0;
}

variable_types:
inpt
char [1]


decompiled_code:
__int64 __fastcall pieces_left(char a1)
{
unsigned int v1; // er8
int i; // eax

v1 = 0;
for ( i = *((_DWORD *)&board + a1); i; i &= i - 1 )
++v1;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
unsigned int v1; // er8
int i; // eax

v1 = 0;
for ( i = *((_DWORD *)&board + a1); i; i &= i - 1 )
++v1;
return v1;
}

original_code:
int pieces_left(bool color) {
  bitboard to, next;
  int n = 0;
  to = board[(int)color];
  while (to) {
    next = ((to) & -(to));
    n++;
    to ^= next;
  }
  return n;
}

canonical_original_code:
int pieces_left(char color) {
  unsigned int to; unsigned int next;
  int n = 0;
  to = board[(int)color];
  while (to) {
    next = ((to) & -(to));
    n++;
    to ^= next;
  }
  return n;
}

variable_types: color char

variable_types:
next
unsigned int


decompiled_code:
__int64 my_pieces_left()
{
return pieces_left(color);
}

canonical_decompiled_code:
__int64 func2()
{
return func1(color);
}

original_code:
int my_pieces_left() {
  return pieces_left(color);
}


decompiled_code:
__int64 opponent_pieces_left()
{
return pieces_left(color == 0);
}

canonical_decompiled_code:
__int64 func3()
{
return func1(color == 0);
}

original_code:
int opponent_pieces_left() {
  return pieces_left(!color);
}


decompiled_code:
char __fastcall accept_draw(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
int v4; // er8
unsigned int v5; // eax
int v6; // ecx

v2 = my_pieces_left();
v4 = opponent_pieces_left(a1, a2, v3, v2);
LOBYTE(v5) = 0;
if ( v6 < v4 )
{
if ( color )
v5 = (unsigned int)score >> 31;
else
LOBYTE(v5) = score > 0;
}
return v5;
}

canonical_decompiled_code:
char __fastcall func4(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
int v4; // er8
unsigned int v5; // eax
int v6; // ecx

v2 = func2();
v4 = func3(a1, a2, v3, v2);
LOBYTE(v5) = 0;
if ( v6 < v4 )
{
if ( color )
v5 = (unsigned int)score >> 31;
else
LOBYTE(v5) = score > 0;
}
return v5;
}

original_code:
bool accept_draw() {
  int mp = my_pieces_left(), op = opponent_pieces_left();
  if (mp < op && (color ? score < 0 : score > 0) ) {
    return 1;
  }
  else {
    return 0;
  }
}

canonical_original_code:
char accept_draw() {
  int mp = my_pieces_left(); int op = opponent_pieces_left();
  if (mp < op && (color ? score < 0 : score > 0) ) {
    return 1;
  }
  else {
    return 0;
  }
}

variable_types: mp int

variable_types: op int


decompiled_code:
__int64 opponent_turn()
{
char v0; // bp
char v1; // bl
__int64 v2; // rsi
int v3; // eax
const char *v4; // rcx
const char *v5; // rsi
unsigned int v6; // er12
int v8; // eax
int v9; // ecx
int v10; // edx
int v11; // eax
int v12; // eax
const char *v13; // rsi
char v14; // al
bool v15; // zf
char *v16; // rdi
char v17; // [rsp+Bh] [rbp-2FDh] BYREF
int v18; // [rsp+Ch] [rbp-2FCh] BYREF
int v19; // [rsp+10h] [rbp-2F8h] BYREF
__int64 v20; // [rsp+14h] [rbp-2F4h] BYREF
int v21; // [rsp+1Ch] [rbp-2ECh]
char src[4]; // [rsp+20h] [rbp-2E8h] BYREF
char v23; // [rsp+24h] [rbp-2E4h]
char v24[616]; // [rsp+A0h] [rbp-268h] BYREF

v0 = 0;
v1 = 1;
while ( 1 )
{
LABEL_5:
v2 = 0xFFFFFFFFLL;
v3 = try_capture(&board, 0xFFFFFFFFLL, color == 0, v24);
if ( v3 > 0 && v1 )
{
v4 = (const char *)&unk_A34C;
if ( v3 != 1 )
v4 = "s";
v5 = "White";
if ( !color )
v5 = "Black";
printf("\nWarning: %s has %d possible jump%s.\n", v5, (unsigned int)v3, v4);
v2 = (__int64)"White";
if ( !color )
v2 = (__int64)"Black";
v1 = 0;
v0 = 1;
printf(" %s must jump or %s will lose!\n\n", v2, v2);
}
puts("OPERATOR: Opponent's move was: ");
gets(src);
if ( *(_DWORD *)src == 2002874980 && !v23 )
{
puts("Opponent has offered a draw.");
return (unsigned int)(accept_draw((__int64)"Opponent has offered a draw.", v2) != 1) + 2;
}
if ( (unsigned int)valid_move_input(src)
&& (unsigned __int8)trans_string_move(&board, &v20, src, color == 0)
&& (int)_isoc99_sscanf(src, "%d-%d", &v18, &v19) > 1 )
{
break;
}
LABEL_4:
puts("Invalid move entered!");
}
v8 = v18 + 2;
v9 = v19 - 1;
if ( --v18 >= 0 )
v8 = v18;
v10 = v19 + 2;
--v19;
v11 = v8 >> 2;
if ( v9 >= 0 )
v10 = v9;
v12 = v11 - (v10 >> 2);
if ( v0 && (unsigned int)(v12 + 1) <= 2 )
{
v13 = "Black";
if ( color )
v13 = "White";
printf("\nWarning: This move will cause %s to lose!\n", v13);
v14 = v17;
while ( v14 != 110 && v14 != 121 || !v14 )
{
printf("\nOPERATOR: Are you sure this is correct? [y/n] ");
fflush(stdout);
gets(&v17);
v14 = v17;
if ( v17 == 121 )
return 4;
if ( v17 == 110 )
{
v17 = 0;
goto LABEL_5;
}
}
goto LABEL_4;
}
v15 = logging == 0;
v6 = 1;
board = v20;
*((_DWORD *)&board + 2) = v21;
if ( !v15 )
{
v16 = (char *)&move_list + 128 * (__int64)total_moves++;
strcpy(v16, src);
}
return v6;
}

canonical_decompiled_code:
__int64 func5()
{
char v0; // bp
char v1; // bl
__int64 v2; // rsi
int v3; // eax
const char *v4; // rcx
const char *v5; // rsi
unsigned int v6; // er12
int v8; // eax
int v9; // ecx
int v10; // edx
int v11; // eax
int v12; // eax
const char *v13; // rsi
char v14; // al
bool v15; // zf
char *v16; // rdi
char v17; // [rsp+Bh] [rbp-2FDh] BYREF
int v18; // [rsp+Ch] [rbp-2FCh] BYREF
int v19; // [rsp+10h] [rbp-2F8h] BYREF
__int64 v20; // [rsp+14h] [rbp-2F4h] BYREF
int v21; // [rsp+1Ch] [rbp-2ECh]
char src[4]; // [rsp+20h] [rbp-2E8h] BYREF
char v23; // [rsp+24h] [rbp-2E4h]
char v24[616]; // [rsp+A0h] [rbp-268h] BYREF

v0 = 0;
v1 = 1;
while ( 1 )
{
LABEL_5:
v2 = 0xFFFFFFFFLL;
v3 = try_capture(&board, 0xFFFFFFFFLL, color == 0, v24);
if ( v3 > 0 && v1 )
{
v4 = (const char *)&unk_A34C;
if ( v3 != 1 )
v4 = "s";
v5 = "White";
if ( !color )
v5 = "Black";
printf("\nWarning: %s has %d possible jump%s.\n", v5, (unsigned int)v3, v4);
v2 = (__int64)"White";
if ( !color )
v2 = (__int64)"Black";
v1 = 0;
v0 = 1;
printf(" %s must jump or %s will lose!\n\n", v2, v2);
}
puts("OPERATOR: Opponent's move was: ");
gets(src);
if ( *(_DWORD *)src == 2002874980 && !v23 )
{
puts("Opponent has offered a draw.");
return (unsigned int)(func4((__int64)"Opponent has offered a draw.", v2) != 1) + 2;
}
if ( (unsigned int)valid_move_input(src)
&& (unsigned __int8)trans_string_move(&board, &v20, src, color == 0)
&& (int)_isoc99_sscanf(src, "%d-%d", &v18, &v19) > 1 )
{
break;
}
LABEL_4:
puts("Invalid move entered!");
}
v8 = v18 + 2;
v9 = v19 - 1;
if ( --v18 >= 0 )
v8 = v18;
v10 = v19 + 2;
--v19;
v11 = v8 >> 2;
if ( v9 >= 0 )
v10 = v9;
v12 = v11 - (v10 >> 2);
if ( v0 && (unsigned int)(v12 + 1) <= 2 )
{
v13 = "Black";
if ( color )
v13 = "White";
printf("\nWarning: This move will cause %s to lose!\n", v13);
v14 = v17;
while ( v14 != 110 && v14 != 121 || !v14 )
{
printf("\nOPERATOR: Are you sure this is correct? [y/n] ");
fflush(stdout);
gets(&v17);
v14 = v17;
if ( v17 == 121 )
return 4;
if ( v17 == 110 )
{
v17 = 0;
goto LABEL_5;
}
}
goto LABEL_4;
}
v15 = logging == 0;
v6 = 1;
board = v20;
*((_DWORD *)&board + 2) = v21;
if ( !v15 )
{
v16 = (char *)&move_list + 128 * (__int64)total_moves++;
strcpy(v16, src);
}
return v6;
}

original_code:
int opponent_turn() {
  char ctrl = (int)((void *)0), inp[128], inpt[1];
  struct move opp_move;
  int n = 0, from, to, diff;
  struct move l_moves[48];
  bool must_jump = 0, once = 1, nagflag;
  while (!ctrl) {
    nagflag = 1;
    n = try_capture(board, 0xffffffff, !(int)color, l_moves);
    if (n > 0 && once) {
      printf("\nWarning: %s has %d possible jump%s.\n", (!color ? "Black" : "White"),
               n, (n > 1 ? "s" : ""));
      printf("         %s must jump or %s will lose!\n\n",
              (!color ? "Black" : "White"), (!color ? "Black" : "White"));
      must_jump = 1;
      once = 0;
    }
    printf("OPERATOR: Opponent's move was: \n");
    if (gets(inp) == ((void *)0)) {
      ctrl = (int)((void *)0);
  }
    if (strcmp(inp, "draw") == 0) {
      printf("Opponent has offered a draw.\n");
      if (accept_draw() == 1) {
    return 2;
      }
      else {
    return 3;
      }
    }
    else if (valid_move_input(inp)) {
      if ((trans_string_move(board, &opp_move, inp, !color)) != 0) {
        if (inp && sscanf(inp, "%d-%d", &from, &to) >= 2) {
          from--;to--;
          diff = (from/4+1)-(to/4+1);
          if (must_jump && (diff > -2 && diff < 2)) {
            printf("\nWarning: This move will cause %s to lose!\n", (!color ? "Black" : "White"));
            while((inpt[0] != 'y' && inpt[0] != 'n') || inpt[0] == 0) {
              printf("\nOPERATOR: Are you sure this is correct? [y/n] ");
              fflush(stdout);
              gets(inpt);
              if (inpt[0] == 'y')
                return 4;
              if (inpt[0] == 'n') {
                inpt[0] = (int)((void *)0);
                nagflag = 0;
                break;
              }
            }
          }
          else {
            ctrl = 1;
            board[0] = opp_move.board[0];
            board[1] = opp_move.board[1];
            board[2] = opp_move.board[2];
            if (logging)
              strcpy(move_list[total_moves++], inp);
            break;
          }
        }
      }
    }
    if (nagflag)
      printf("Invalid move entered!\n");
    ctrl = (int)((void *)0);
  }
  return 1;
}

canonical_original_code:
int opponent_turn() {
  char ctrl = (int)((void *)0); char inp[128]; char inpt[1];
  struct move opp_move;
  int n = 0; int from; int to; int diff;
  struct move l_moves[48];
  char must_jump = 0; char once = 1; char nagflag;
  while (!ctrl) {
    nagflag = 1;
    n = try_capture(board, 0xffffffff, !(int)color, l_moves);
    if (n > 0 && once) {
      printf("\nWarning: %s has %d possible jump%s.\n", (!color ? "Black" : "White"),
               n, (n > 1 ? "s" : ""));
      printf("         %s must jump or %s will lose!\n\n",
              (!color ? "Black" : "White"), (!color ? "Black" : "White"));
      must_jump = 1;
      once = 0;
    }
    printf("OPERATOR: Opponent's move was: \n");
    if (gets(inp) == ((void *)0)) {
      ctrl = (int)((void *)0);
  }
    if (strcmp(inp, "draw") == 0) {
      printf("Opponent has offered a draw.\n");
      if (accept_draw() == 1) {
    return 2;
      }
      else {
    return 3;
      }
    }
    else if (valid_move_input(inp)) {
      if ((trans_string_move(board, &opp_move, inp, !color)) != 0) {
        if (inp && sscanf(inp, "%d-%d", &from, &to) >= 2) {
          from--;to--;
          diff = (from/4+1)-(to/4+1);
          if (must_jump && (diff > -2 && diff < 2)) {
            printf("\nWarning: This move will cause %s to lose!\n", (!color ? "Black" : "White"));
            while((inpt[0] != 'y' && inpt[0] != 'n') || inpt[0] == 0) {
              printf("\nOPERATOR: Are you sure this is correct? [y/n] ");
              fflush(stdout);
              gets(inpt);
              if (inpt[0] == 'y')
                return 4;
              if (inpt[0] == 'n') {
                inpt[0] = (int)((void *)0);
                nagflag = 0;
                break;
              }
            }
          }
          else {
            ctrl = 1;
            board[0] = opp_move.board[0];
            board[1] = opp_move.board[1];
            board[2] = opp_move.board[2];
            if (logging)
              strcpy(move_list[total_moves++], inp);
            break;
          }
        }
      }
    }
    if (nagflag)
      printf("Invalid move entered!\n");
    ctrl = (int)((void *)0);
  }
  return 1;
}

variable_types: ctrl char

variable_types:
inp
char [128]

variable_types: from int

variable_types: to int

variable_types: diff int

variable_types: struct move { unsigned int board[3]; } [48]

variable_types: once char

variable_types: nagflag char


decompiled_code:
__int64 __fastcall how_much_time(__int64 a1, __int64 a2)
{
int v2; // ebp
__int64 v3; // rdx
__int64 v4; // rcx
int v5; // ebx
int v6; // er8
__int64 result; // rax
double v8; // xmm0_8
int v9; // eax
double v10; // xmm1_8
double v11; // xmm0_8
__int64 v12; // [rsp+0h] [rbp-258h] BYREF

v2 = my_pieces_left();
v5 = opponent_pieces_left(a1, a2, v3, v4);
v6 = generate_moves(&board, (unsigned int)color, &v12);
result = 1LL;
if ( v6 == 1 )
return result;
if ( color )
{
v8 = *(double *)&secs_left;
v9 = 2 * (score <= 0);
if ( v2 <= 10 )
{
LABEL_4:
if ( v2 > 7 )
{
result = (unsigned int)(v9 + 3);
v10 = (double)(int)result;
}
else if ( v2 > 3 )
{
result = (unsigned int)(v9 + 5);
v10 = (double)(int)result;
}
else if ( v2 > 1 )
{
result = (unsigned int)(v9 + 8);
v10 = (double)(int)result;
}
else
{
result = (unsigned int)((int)v8 / last_forty);
v10 = (double)(int)result;
if ( v5 == 1 && v2 == 1 )
--last_forty;
}
goto LABEL_10;
}
}
else
{
v8 = *(double *)&secs_left;
v9 = 2 * (score >= 0);
if ( v2 <= 10 )
goto LABEL_4;
}
if ( (unsigned int)(v2 - 11) > 1 )
{
result = (unsigned int)((int)v8 / last_forty);
v10 = (double)(int)result;
}
else
{
v10 = *(double *)&LC14;
result = 1LL;
}
LABEL_10:
if ( v10 >= v8 )
{
result = (unsigned int)(int)v8;
v10 = (double)(int)result;
}
v11 = v8 / 30.0;
if ( v10 > v11 )
result = (unsigned int)(int)v11;
if ( (int)result <= 0 )
result = 1LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
int v2; // ebp
__int64 v3; // rdx
__int64 v4; // rcx
int v5; // ebx
int v6; // er8
__int64 result; // rax
double v8; // xmm0_8
int v9; // eax
double v10; // xmm1_8
double v11; // xmm0_8
__int64 v12; // [rsp+0h] [rbp-258h] BYREF

v2 = func2();
v5 = func3(a1, a2, v3, v4);
v6 = generate_moves(&board, (unsigned int)color, &v12);
result = 1LL;
if ( v6 == 1 )
return result;
if ( color )
{
v8 = *(double *)&secs_left;
v9 = 2 * (score <= 0);
if ( v2 <= 10 )
{
LABEL_4:
if ( v2 > 7 )
{
result = (unsigned int)(v9 + 3);
v10 = (double)(int)result;
}
else if ( v2 > 3 )
{
result = (unsigned int)(v9 + 5);
v10 = (double)(int)result;
}
else if ( v2 > 1 )
{
result = (unsigned int)(v9 + 8);
v10 = (double)(int)result;
}
else
{
result = (unsigned int)((int)v8 / last_forty);
v10 = (double)(int)result;
if ( v5 == 1 && v2 == 1 )
--last_forty;
}
goto LABEL_10;
}
}
else
{
v8 = *(double *)&secs_left;
v9 = 2 * (score >= 0);
if ( v2 <= 10 )
goto LABEL_4;
}
if ( (unsigned int)(v2 - 11) > 1 )
{
result = (unsigned int)((int)v8 / last_forty);
v10 = (double)(int)result;
}
else
{
v10 = *(double *)&LC14;
result = 1LL;
}
LABEL_10:
if ( v10 >= v8 )
{
result = (unsigned int)(int)v8;
v10 = (double)(int)result;
}
v11 = v8 / 30.0;
if ( v10 > v11 )
result = (unsigned int)(int)v11;
if ( (int)result <= 0 )
result = 1LL;
return result;
}

original_code:
unsigned int how_much_time() {
  int r = 0, time, p = my_pieces_left(), t = opponent_pieces_left();
 struct move m_list[48];
 if (generate_moves(board, (bool)color, m_list) == 1)
  return 1;
  if (color ? score > 0 : score < 0) {
    r = 0;
  }
  else {
    r = 2;
  }
  switch (p) {
  case 12:
  case 11:
     time = 1;
   break;
  case 10:
  case 9:
  case 8:
   time = 3+r;
   break;
  case 7:
  case 6:
  case 5:
  case 4:
     time = 5+r;
   break;
  case 3:
  case 2:
     time = 8+r;
   break;
  default:
     if (t == 1 && p == 1) {
    time = (int)secs_left / last_forty--;
   }
   else {
    time = (int)secs_left / last_forty;
   }
  }
  if ((double)time >= secs_left)
    time = (int)secs_left;
  if (time > secs_left / 30)
    time = secs_left / 30;
  if (time < 1)
    time = 1;
  return time;
}

canonical_original_code:
unsigned int how_much_time() {
  int r = 0; int time; int p = my_pieces_left(); int t = opponent_pieces_left();
 struct move m_list[48];
 if (generate_moves(board, (char)color, m_list) == 1)
  return 1;
  if (color ? score > 0 : score < 0) {
    r = 0;
  }
  else {
    r = 2;
  }
  switch (p) {
  case 12:
  case 11:
     time = 1;
   break;
  case 10:
  case 9:
  case 8:
   time = 3+r;
   break;
  case 7:
  case 6:
  case 5:
  case 4:
     time = 5+r;
   break;
  case 3:
  case 2:
     time = 8+r;
   break;
  default:
     if (t == 1 && p == 1) {
    time = (int)secs_left / last_forty--;
   }
   else {
    time = (int)secs_left / last_forty;
   }
  }
  if ((double)time >= secs_left)
    time = (int)secs_left;
  if (time > secs_left / 30)
    time = secs_left / 30;
  if (time < 1)
    time = 1;
  return time;
}

variable_types: r int

variable_types: time int

variable_types: p int

variable_types: t int

return_type: unsigned int


decompiled_code:
int __fastcall my_turn(__int64 a1, __int64 a2)
{
bool v2; // zf
const char *v3; // rsi
char *v5; // rdi
double v6; // [rsp+0h] [rbp-A8h]
unsigned int v7; // [rsp+Ch] [rbp-9Ch]
__int64 v8; // [rsp+14h] [rbp-94h] BYREF
int v9; // [rsp+1Ch] [rbp-8Ch]
char src[136]; // [rsp+20h] [rbp-88h] BYREF

clock();
v7 = how_much_time(a1, a2);
if ( !setjmp(env) )
mtdf(&board, &v8, (unsigned int)color, v7);
++moves;
v6 = (double)(int)((double)(int)clock() - *(double *)&time_used);
*(double *)&time_used = *(double *)&time_used + v6;
trans_move_string(&board, &v8, src, (unsigned int)color);
printf(" My move is: %d. %s\n", (unsigned int)moves, src);
v2 = logging == 0;
board = v8;
*((_DWORD *)&board + 2) = v9;
if ( !v2 )
{
v5 = (char *)&move_list + 128 * (__int64)total_moves++;
strcpy(v5, src);
}
*(double *)&secs_left = *(double *)&secs_left - v6 / 1000000.0;
printf("Time on this move: %.2lf\n", v6 / 1000000.0);
printf(" Total time spent: %.2lf\n", 300.0 - *(double *)&secs_left);
score = eval(&board, color == 0);
if ( (unsigned int)(score + 350) > 0x2BC )
{
v3 = "White is winning";
if ( score >= 0 )
v3 = "Black is winning";
}
else if ( (unsigned int)(score + 199) <= 0x18E )
{
if ( (unsigned int)(score + 99) > 0xC6 )
{
v3 = "Large white advantage";
if ( score >= 0 )
v3 = "Large black advantage";
}
else if ( (unsigned int)(score + 29) <= 0x3A )
{
v3 = "Equal";
if ( score )
{
v3 = "Slight white advantage";
if ( score >= 0 )
v3 = "Slight black advantage";
}
}
else
{
v3 = "Medium white advantage";
if ( score >= 0 )
v3 = "Medium black advantage";
}
}
else
{
v3 = "Very large white advantage";
if ( score >= 0 )
v3 = "Very large black advantage";
}
return printf(" Standings: %s (%d)\n", v3, (unsigned int)score);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, __int64 a2)
{
bool v2; // zf
const char *v3; // rsi
char *v5; // rdi
double v6; // [rsp+0h] [rbp-A8h]
unsigned int v7; // [rsp+Ch] [rbp-9Ch]
__int64 v8; // [rsp+14h] [rbp-94h] BYREF
int v9; // [rsp+1Ch] [rbp-8Ch]
char src[136]; // [rsp+20h] [rbp-88h] BYREF

clock();
v7 = func6(a1, a2);
if ( !setjmp(env) )
mtdf(&board, &v8, (unsigned int)color, v7);
++moves;
v6 = (double)(int)((double)(int)clock() - *(double *)&time_used);
*(double *)&time_used = *(double *)&time_used + v6;
trans_move_string(&board, &v8, src, (unsigned int)color);
printf(" My move is: %d. %s\n", (unsigned int)moves, src);
v2 = logging == 0;
board = v8;
*((_DWORD *)&board + 2) = v9;
if ( !v2 )
{
v5 = (char *)&move_list + 128 * (__int64)total_moves++;
strcpy(v5, src);
}
*(double *)&secs_left = *(double *)&secs_left - v6 / 1000000.0;
printf("Time on this move: %.2lf\n", v6 / 1000000.0);
printf(" Total time spent: %.2lf\n", 300.0 - *(double *)&secs_left);
score = eval(&board, color == 0);
if ( (unsigned int)(score + 350) > 0x2BC )
{
v3 = "White is winning";
if ( score >= 0 )
v3 = "Black is winning";
}
else if ( (unsigned int)(score + 199) <= 0x18E )
{
if ( (unsigned int)(score + 99) > 0xC6 )
{
v3 = "Large white advantage";
if ( score >= 0 )
v3 = "Large black advantage";
}
else if ( (unsigned int)(score + 29) <= 0x3A )
{
v3 = "Equal";
if ( score )
{
v3 = "Slight white advantage";
if ( score >= 0 )
v3 = "Slight black advantage";
}
}
else
{
v3 = "Medium white advantage";
if ( score >= 0 )
v3 = "Medium black advantage";
}
}
else
{
v3 = "Very large white advantage";
if ( score >= 0 )
v3 = "Very large black advantage";
}
return printf(" Standings: %s (%d)\n", v3, (unsigned int)score);
}

original_code:
void my_turn() {
  clock_t t = 0;
  double dt;
  unsigned int use;
  struct move best_move;
  char move_str[128],
    *stand;
  t = clock();
  use = how_much_time();
  if (_setjmp (env) == 0)
    mtdf(board, &best_move, color, use);
  moves++;
  t = clock();
  t = t - time_used;
  time_used += t;
  trans_move_string(board, &best_move, move_str, color);
  printf("       My move is: %d. %s\n", moves, move_str);
  board[1] = best_move.board[1];
  board[0] = best_move.board[0];
  board[2] = best_move.board[2];
  if (logging)
    strcpy(move_list[total_moves++], move_str);
  if (t != -1) {
    dt = (double) t / (double) ((__clock_t) 1000000);
    secs_left = secs_left - dt;
    printf("Time on this move: %.2lf\n", dt);
  }
  printf(" Total time spent: %.2lf\n", 300 - secs_left);
  score = eval(board, !color);
  if (score < -350 || score > 350)
    stand = (score < 0) ? "White is winning" : "Black is winning";
  else if (score <= -200 || score >= 200)
    stand = (score < 0) ? "Very large white advantage" : "Very large black advantage";
  else if (score <= -100 || score >= 100)
    stand = (score < 0) ? "Large white advantage" : "Large black advantage";
  else if (score <= -30 || score >= 30)
    stand = (score < 0) ? "Medium white advantage" : "Medium black advantage";
  else if (score > -30 && score < 30 && score != 0)
    stand = (score < 0) ? "Slight white advantage" : "Slight black advantage";
  else if (score == 0)
    stand = "Equal";
  else
    stand = "";
  printf("        Standings: %s (%d)\n", stand, score);
}

canonical_original_code:
void my_turn() {
  long t = 0;
  double dt;
  unsigned int use;
  struct move best_move;
  char move_str[128]; char *stand;
  t = clock();
  use = how_much_time();
  if (_setjmp (env) == 0)
    mtdf(board, &best_move, color, use);
  moves++;
  t = clock();
  t = t - time_used;
  time_used += t;
  trans_move_string(board, &best_move, move_str, color);
  printf("       My move is: %d. %s\n", moves, move_str);
  board[1] = best_move.board[1];
  board[0] = best_move.board[0];
  board[2] = best_move.board[2];
  if (logging)
    strcpy(move_list[total_moves++], move_str);
  if (t != -1) {
    dt = (double) t / (double) ((long) 1000000);
    secs_left = secs_left - dt;
    printf("Time on this move: %.2lf\n", dt);
  }
  printf(" Total time spent: %.2lf\n", 300 - secs_left);
  score = eval(board, !color);
  if (score < -350 || score > 350)
    stand = (score < 0) ? "White is winning" : "Black is winning";
  else if (score <= -200 || score >= 200)
    stand = (score < 0) ? "Very large white advantage" : "Very large black advantage";
  else if (score <= -100 || score >= 100)
    stand = (score < 0) ? "Large white advantage" : "Large black advantage";
  else if (score <= -30 || score >= 30)
    stand = (score < 0) ? "Medium white advantage" : "Medium black advantage";
  else if (score > -30 && score < 30 && score != 0)
    stand = (score < 0) ? "Slight white advantage" : "Slight black advantage";
  else if (score == 0)
    stand = "Equal";
  else
    stand = "";
  printf("        Standings: %s (%d)\n", stand, score);
}

variable_types: t long

variable_types: dt double

variable_types:
use
unsigned int

variable_types:
stand
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
const char *v4; // r12
char v5; // al
const char *v6; // rsi
__int64 v7; // rsi
__int64 v8; // rdx
__int64 v9; // rcx
const char *v10; // rsi
int v11; // eax
const char *v13; // rsi
char v14; // [rsp+Fh] [rbp-269h] BYREF
char v15[616]; // [rsp+10h] [rbp-268h] BYREF

v14 = 1;
puts(aR);
if ( argc > 1 )
{
v3 = argv[1];
v4 = v3;
if ( !strcmp(v3, "--help") )
{
puts("Usage: ./checkers [-lt] [board-file] [log-file]");
puts(" -l Log this game to specified file.");
puts(" -t Ignore time constraints");
puts(" To specify a board path and a log path at the same");
puts(" time, omit both -l and t.");
puts(" Omitting all arguments starts a new game at the ");
puts(" standard position with no logging.");
return 0;
}
if ( strchr(argv[1], 45) )
{
if ( strchr(v3, 108) )
{
if ( argc != 3 )
{
puts("Invalid command!\nType './checkers --help' for command line help.");
return 0;
}
logfile = (__int64)argv[2];
printf("Logging game to %s.\n", (const char *)logfile);
logging = 1;
v3 = argv[1];
}
if ( strchr(v3, 116) )
{
puts("Unlimited time.");
secs_left = LC46;
if ( !logging && argc == 3 )
{
v4 = argv[2];
printf("Using %s instead.\n", v4);
goto LABEL_11;
}
}
}
else
{
if ( argc == 2 )
{
printf("Using %s instead.\n", v3);
goto LABEL_11;
}
if ( argc == 3 )
{
logfile = (__int64)argv[2];
printf("Using %s instead.\n", v3);
printf("Logging game to %s.\n", (const char *)logfile);
logging = 1;
goto LABEL_11;
}
}
}
v4 = "starts/initial.wdp";
LABEL_11:
putchar(10);
do
{
printf("OPERATOR: Enter 'b' if system is black, 'w' if white. [b/w] ");
fflush(stdout);
v5 = getc(stdin);
}
while ( v5 != 98 && v5 != 119 );
color = v5 == 98;
getc(stdin);
if ( (unsigned int)read_wdp(&board, v4, &v14, &secs_left) )
{
sigemptyset(&alarm_set);
sigaddset(&alarm_set, 14);
if ( color != v14 )
{
print_board(&board);
goto LABEL_24;
}
while ( 1 )
{
v6 = "Black";
if ( !color )
v6 = "White";
printf("\n\n%s's turn:\n\n", v6);
if ( (int)generate_moves(&board, (unsigned int)color, v15) <= 0 )
{
puts("\nI cannot make any moves!");
puts("\nI lost the game.\n");
return 0;
}
v7 = color == 0;
if ( (int)generate_moves(&board, v7, v15) <= 0 )
goto LABEL_40;
my_turn((__int64)&board, v7);
if ( *(double *)&secs_left <= 0.0 )
break;
print_board(&board);
if ( logging )
{
v7 = (__int64)&move_list;
write_log(&board, &move_list, logfile, (unsigned int)total_moves, 0LL, *(double *)&secs_left);
}
if ( (int)opponent_pieces_left(&board, v7, v8, v9) <= 0 )
{
LABEL_47:
puts("\nI won the game.\n");
return 0;
}
LABEL_24:
v10 = "White";
if ( !color )
v10 = "Black";
backb = board;
*((_DWORD *)&backb + 2) = *((_DWORD *)&board + 2);
printf("\n\n%s's turn:\n\n", v10);
if ( (int)generate_moves(&board, color == 0, v15) <= 0 )
{
LABEL_40:
puts("\nOpponent cannot make any moves!");
puts("\nI won the game.\n");
return 0;
}
while ( 1 )
{
v11 = opponent_turn();
switch ( v11 )
{
case 2:
puts("Draw has been offered and accepted.");
puts("\nThe game ended in a draw.\n");
return 0;
case 3:
puts("Draw has been offered and refused. Game will continue.");
goto LABEL_24;
case 4:
v13 = "Black";
if ( color )
v13 = "White";
printf("\nIllegal move by %s! Should have jumped.\n", v13);
goto LABEL_47;
}
print_board(&board);
if ( (unsigned __int8)okay() )
break;
board = backb;
*((_DWORD *)&board + 2) = *((_DWORD *)&backb + 2);
}
if ( logging )
write_log(&board, &move_list, logfile, (unsigned int)total_moves, &color, *(double *)&secs_left);
if ( (int)my_pieces_left() <= 0 )
{
puts("\nI lost the game.\n");
return 0;
}
}
puts("\nMy time ran out, so I give up!");
puts("\nI lost the game.\n");
}
else
{
fwrite("Could not load board map!\n", 1uLL, 0x1AuLL, stderr);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
const char *v4; // r12
char v5; // al
const char *v6; // rsi
__int64 v7; // rsi
__int64 v8; // rdx
__int64 v9; // rcx
const char *v10; // rsi
int v11; // eax
const char *v13; // rsi
char v14; // [rsp+Fh] [rbp-269h] BYREF
char v15[616]; // [rsp+10h] [rbp-268h] BYREF

v14 = 1;
puts(aR);
if ( argc > 1 )
{
v3 = argv[1];
v4 = v3;
if ( !strcmp(v3, "--help") )
{
puts("Usage: ./checkers [-lt] [board-file] [log-file]");
puts(" -l Log this game to specified file.");
puts(" -t Ignore time constraints");
puts(" To specify a board path and a log path at the same");
puts(" time, omit both -l and t.");
puts(" Omitting all arguments starts a new game at the ");
puts(" standard position with no logging.");
return 0;
}
if ( strchr(argv[1], 45) )
{
if ( strchr(v3, 108) )
{
if ( argc != 3 )
{
puts("Invalid command!\nType './checkers --help' for command line help.");
return 0;
}
logfile = (__int64)argv[2];
printf("Logging game to %s.\n", (const char *)logfile);
logging = 1;
v3 = argv[1];
}
if ( strchr(v3, 116) )
{
puts("Unlimited time.");
secs_left = LC46;
if ( !logging && argc == 3 )
{
v4 = argv[2];
printf("Using %s instead.\n", v4);
goto LABEL_11;
}
}
}
else
{
if ( argc == 2 )
{
printf("Using %s instead.\n", v3);
goto LABEL_11;
}
if ( argc == 3 )
{
logfile = (__int64)argv[2];
printf("Using %s instead.\n", v3);
printf("Logging game to %s.\n", (const char *)logfile);
logging = 1;
goto LABEL_11;
}
}
}
v4 = "starts/initial.wdp";
LABEL_11:
putchar(10);
do
{
printf("OPERATOR: Enter 'b' if system is black, 'w' if white. [b/w] ");
fflush(stdout);
v5 = getc(stdin);
}
while ( v5 != 98 && v5 != 119 );
color = v5 == 98;
getc(stdin);
if ( (unsigned int)read_wdp(&board, v4, &v14, &secs_left) )
{
sigemptyset(&alarm_set);
sigaddset(&alarm_set, 14);
if ( color != v14 )
{
print_board(&board);
goto LABEL_24;
}
while ( 1 )
{
v6 = "Black";
if ( !color )
v6 = "White";
printf("\n\n%s's turn:\n\n", v6);
if ( (int)generate_moves(&board, (unsigned int)color, v15) <= 0 )
{
puts("\nI cannot make any moves!");
puts("\nI lost the game.\n");
return 0;
}
v7 = color == 0;
if ( (int)generate_moves(&board, v7, v15) <= 0 )
goto LABEL_40;
func7((__int64)&board, v7);
if ( *(double *)&secs_left <= 0.0 )
break;
print_board(&board);
if ( logging )
{
v7 = (__int64)&move_list;
write_log(&board, &move_list, logfile, (unsigned int)total_moves, 0LL, *(double *)&secs_left);
}
if ( (int)func3(&board, v7, v8, v9) <= 0 )
{
LABEL_47:
puts("\nI won the game.\n");
return 0;
}
LABEL_24:
v10 = "White";
if ( !color )
v10 = "Black";
backb = board;
*((_DWORD *)&backb + 2) = *((_DWORD *)&board + 2);
printf("\n\n%s's turn:\n\n", v10);
if ( (int)generate_moves(&board, color == 0, v15) <= 0 )
{
LABEL_40:
puts("\nOpponent cannot make any moves!");
puts("\nI won the game.\n");
return 0;
}
while ( 1 )
{
v11 = func5();
switch ( v11 )
{
case 2:
puts("Draw has been offered and accepted.");
puts("\nThe game ended in a draw.\n");
return 0;
case 3:
puts("Draw has been offered and refused. Game will continue.");
goto LABEL_24;
case 4:
v13 = "Black";
if ( color )
v13 = "White";
printf("\nIllegal move by %s! Should have jumped.\n", v13);
goto LABEL_47;
}
print_board(&board);
if ( (unsigned __int8)func0() )
break;
board = backb;
*((_DWORD *)&board + 2) = *((_DWORD *)&backb + 2);
}
if ( logging )
write_log(&board, &move_list, logfile, (unsigned int)total_moves, &color, *(double *)&secs_left);
if ( (int)func2() <= 0 )
{
puts("\nI lost the game.\n");
return 0;
}
}
puts("\nMy time ran out, so I give up!");
puts("\nI lost the game.\n");
}
else
{
fwrite("Could not load board map!\n", 1uLL, 0x1AuLL, stderr);
}
return 0;
}

original_code:
int main(int argc, char *argv[]) {
  if (argc < 2)
    return 0;
  if (!strcmp(argv[1], "-neighbor"))
    test_neighbor();
  if (argc < 3)
    return 0;
  else if (!strcmp(argv[1], "-move"))
    test_move(argv[2]);
  else if (!strcmp(argv[1], "-eval"))
    test_eval(argv[2]);
  if (argc < 4)
    return 0;
  else if (!strcmp(argv[1], "-search"))
    test_search(argv[2], atoi(argv[3]));
  else if (!strcmp(argv[1], "-trans"))
    test_trans(argv[2], argv[3]);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void __noreturn alarm_handler()
{
printf(
"Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
(unsigned int)(top_depth - 1),
(unsigned int)n_nodes,
(unsigned int)n_evals,
(unsigned int)n_hash,
(double)n_evals / (double)time_allowed);
longjmp(env, 1);
}

canonical_decompiled_code:
void __noreturn func0()
{
printf(
"Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
(unsigned int)(top_depth - 1),
(unsigned int)n_nodes,
(unsigned int)n_evals,
(unsigned int)n_hash,
(double)n_evals / (double)time_allowed);
longjmp(env, 1);
}

original_code:
void alarm_handler(int signal)
{
  printf("Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
  top_depth - 1, n_nodes, n_evals, n_hash, (double)n_evals/time_allowed);
  longjmp(env, 1);
}

variable_types: signal int


original_code:
int alpha_beta(bitboard *board, int alpha, int beta, int depth, const bool color)
{
  int val, next_val, best_alpha, n_moves, i;
  int hash_key;
  struct move move_list[48];
  struct move best_move;
  struct hash_pos *hash_entry;
  bool has_best_move = 0;
  n_nodes++;
  hash_key = (((((((board[0] + ~(board[0] << 15)) ^ (board[0] >> 10)) + (board[0] << 3)) ^ (board[0] >> 6)) + ~(board[0] << 11)) ^ (board[0] >> 16)) ^ ((((((board[1] + ~(board[1] << 15)) ^ (board[1] >> 10)) + (board[1] << 3)) ^ (board[1] >> 6)) + ~(board[1] << 11)) ^ (board[1] >> 16))) % 500000;
  hash_entry = &trans_table[hash_key];
  if (board[0] != hash_entry->board[0] ||
      board[1] != hash_entry->board[1] ||
      board[2] != hash_entry->board[2] ||
      color != hash_entry->color) {
    hash_entry = ((void *)0);
  }
  if (hash_entry) {
    n_hash++;
    if (hash_entry->low_depth >= depth) {
      if (hash_entry->low_bound >= beta) {
 if (depth == top_depth) {
   best_move_p->board[1] = hash_entry->best_move.board[1]; best_move_p->board[0] = hash_entry->best_move.board[0]; best_move_p->board[2] = hash_entry->best_move.board[2];;
 }
 return hash_entry->low_bound;
      }
      if (hash_entry->low_bound > alpha)
 alpha = hash_entry->low_bound;
    }
    if (hash_entry->high_depth >= depth) {
      if (hash_entry->high_bound <= alpha) {
 if (depth == top_depth) {
   best_move_p->board[1] = hash_entry->best_move.board[1]; best_move_p->board[0] = hash_entry->best_move.board[0]; best_move_p->board[2] = hash_entry->best_move.board[2];;
 }
 return hash_entry->high_bound;
      }
      if (hash_entry->high_bound > beta)
 beta = hash_entry->high_bound;
    }
  }
  if (depth == 0) {
    n_evals++;
    val = color ? eval(board, 1) : -eval(board, 0);
  }
  else {
    val = -2000000;
    best_alpha = alpha;
    if (hash_entry && hash_entry->has_best_move) {
      best_move.board[1] = hash_entry->best_move.board[1]; best_move.board[0] = hash_entry->best_move.board[0]; best_move.board[2] = hash_entry->best_move.board[2];;
      has_best_move = 1;
      val = -alpha_beta(hash_entry->best_move.board, -beta, -alpha, depth - 1, !color);
      if (val > best_alpha) {
 best_alpha = val;
 if (depth == top_depth) {
   sigprocmask(0, &alarm_set, ((void *)0));
   best_move_p->board[1] = best_move.board[1]; best_move_p->board[0] = best_move.board[0]; best_move_p->board[2] = best_move.board[2];;
   sigprocmask(1, &alarm_set, ((void *)0));
 }
      }
    }
    n_moves = generate_moves(board, color, move_list);
    for (i = 0; i < n_moves && val < beta; i++) {
      next_val = -alpha_beta(move_list[i].board, -beta, -best_alpha, depth - 1, !color);
      if (next_val > val) {
 val = next_val;
 best_move.board[1] = move_list[i].board[1]; best_move.board[0] = move_list[i].board[0]; best_move.board[2] = move_list[i].board[2];;
 has_best_move = 1;
      }
      if (next_val > best_alpha) {
 best_alpha = next_val;
 if (depth == top_depth) {
   sigprocmask(0, &alarm_set, ((void *)0));
   best_move_p->board[1] = move_list[i].board[1]; best_move_p->board[0] = move_list[i].board[0]; best_move_p->board[2] = move_list[i].board[2];;
   sigprocmask(1, &alarm_set, ((void *)0));
 }
      }
    }
  }
  hash_entry = &trans_table[hash_key];
  sigprocmask(0, &alarm_set, ((void *)0));
  hash_entry->color = color;
  hash_entry->has_best_move = has_best_move;
  hash_entry->board[1] = board[1]; hash_entry->board[0] = board[0]; hash_entry->board[2] = board[2];;
  hash_entry->best_move.board[1] = best_move.board[1]; hash_entry->best_move.board[0] = best_move.board[0]; hash_entry->best_move.board[2] = best_move.board[2];;
  if (val <= alpha) {
    hash_entry->high_bound = val;
    hash_entry->high_depth = depth;
    hash_entry->low_bound = -2000000;
    hash_entry->low_depth = 0;
  }
  else if (val >= beta) {
    hash_entry->high_bound = 2000000;
    hash_entry->high_depth = 0;
    hash_entry->low_bound = val;
    hash_entry->low_depth = depth;
  }
  else {
    hash_entry->high_bound = val;
    hash_entry->high_depth = depth;
    hash_entry->low_bound = val;
    hash_entry->low_depth = depth;
  }
  sigprocmask(1, &alarm_set, ((void *)0));
  return val;
}

canonical_original_code:
int alpha_beta(unsigned int *board, int alpha, int beta, int depth, char color)
{
  int val; int next_val; int best_alpha; int n_moves; int i;
  int hash_key;
  struct move move_list[48];
  struct move best_move;
  struct hash_pos *hash_entry;
  char has_best_move = 0;
  n_nodes++;
  hash_key = (((((((board[0] + ~(board[0] << 15)) ^ (board[0] >> 10)) + (board[0] << 3)) ^ (board[0] >> 6)) + ~(board[0] << 11)) ^ (board[0] >> 16)) ^ ((((((board[1] + ~(board[1] << 15)) ^ (board[1] >> 10)) + (board[1] << 3)) ^ (board[1] >> 6)) + ~(board[1] << 11)) ^ (board[1] >> 16))) % 500000;
  hash_entry = &trans_table[hash_key];
  if (board[0] != hash_entry->board[0] ||
      board[1] != hash_entry->board[1] ||
      board[2] != hash_entry->board[2] ||
      color != hash_entry->color) {
    hash_entry = ((void *)0);
  }
  if (hash_entry) {
    n_hash++;
    if (hash_entry->low_depth >= depth) {
      if (hash_entry->low_bound >= beta) {
 if (depth == top_depth) {
   best_move_p->board[1] = hash_entry->best_move.board[1]; best_move_p->board[0] = hash_entry->best_move.board[0]; best_move_p->board[2] = hash_entry->best_move.board[2];;
 }
 return hash_entry->low_bound;
      }
      if (hash_entry->low_bound > alpha)
 alpha = hash_entry->low_bound;
    }
    if (hash_entry->high_depth >= depth) {
      if (hash_entry->high_bound <= alpha) {
 if (depth == top_depth) {
   best_move_p->board[1] = hash_entry->best_move.board[1]; best_move_p->board[0] = hash_entry->best_move.board[0]; best_move_p->board[2] = hash_entry->best_move.board[2];;
 }
 return hash_entry->high_bound;
      }
      if (hash_entry->high_bound > beta)
 beta = hash_entry->high_bound;
    }
  }
  if (depth == 0) {
    n_evals++;
    val = color ? eval(board, 1) : -eval(board, 0);
  }
  else {
    val = -2000000;
    best_alpha = alpha;
    if (hash_entry && hash_entry->has_best_move) {
      best_move.board[1] = hash_entry->best_move.board[1]; best_move.board[0] = hash_entry->best_move.board[0]; best_move.board[2] = hash_entry->best_move.board[2];;
      has_best_move = 1;
      val = -alpha_beta(hash_entry->best_move.board, -beta, -alpha, depth - 1, !color);
      if (val > best_alpha) {
 best_alpha = val;
 if (depth == top_depth) {
   sigprocmask(0, &alarm_set, ((void *)0));
   best_move_p->board[1] = best_move.board[1]; best_move_p->board[0] = best_move.board[0]; best_move_p->board[2] = best_move.board[2];;
   sigprocmask(1, &alarm_set, ((void *)0));
 }
      }
    }
    n_moves = generate_moves(board, color, move_list);
    for (i = 0; i < n_moves && val < beta; i++) {
      next_val = -alpha_beta(move_list[i].board, -beta, -best_alpha, depth - 1, !color);
      if (next_val > val) {
 val = next_val;
 best_move.board[1] = move_list[i].board[1]; best_move.board[0] = move_list[i].board[0]; best_move.board[2] = move_list[i].board[2];;
 has_best_move = 1;
      }
      if (next_val > best_alpha) {
 best_alpha = next_val;
 if (depth == top_depth) {
   sigprocmask(0, &alarm_set, ((void *)0));
   best_move_p->board[1] = move_list[i].board[1]; best_move_p->board[0] = move_list[i].board[0]; best_move_p->board[2] = move_list[i].board[2];;
   sigprocmask(1, &alarm_set, ((void *)0));
 }
      }
    }
  }
  hash_entry = &trans_table[hash_key];
  sigprocmask(0, &alarm_set, ((void *)0));
  hash_entry->color = color;
  hash_entry->has_best_move = has_best_move;
  hash_entry->board[1] = board[1]; hash_entry->board[0] = board[0]; hash_entry->board[2] = board[2];;
  hash_entry->best_move.board[1] = best_move.board[1]; hash_entry->best_move.board[0] = best_move.board[0]; hash_entry->best_move.board[2] = best_move.board[2];;
  if (val <= alpha) {
    hash_entry->high_bound = val;
    hash_entry->high_depth = depth;
    hash_entry->low_bound = -2000000;
    hash_entry->low_depth = 0;
  }
  else if (val >= beta) {
    hash_entry->high_bound = 2000000;
    hash_entry->high_depth = 0;
    hash_entry->low_bound = val;
    hash_entry->low_depth = depth;
  }
  else {
    hash_entry->high_bound = val;
    hash_entry->high_depth = depth;
    hash_entry->low_bound = val;
    hash_entry->low_depth = depth;
  }
  sigprocmask(1, &alarm_set, ((void *)0));
  return val;
}

variable_types:
board
unsigned int *

variable_types: alpha int

variable_types: beta int

variable_types: depth int

variable_types: color char

variable_types: val int

variable_types: i int

variable_types: struct hash_pos { unsigned int board[3]; char color; char has_best_move; int high_bound; unsigned char high_depth; struct move { unsigned int board[3]; } best_move; } *

user_defined_types: struct hash_pos { unsigned int board[3]; char color; char has_best_move; int high_bound; unsigned char high_depth; struct move best_move; }


mtdf

decompiled_code:
int __fastcall mtdf(_DWORD *a1, __int64 a2, char a3, signed int a4)
{
int v6; // eax
int v7; // ebx
int v8; // er13
int v9; // er12
int v10; // ebp
int v11; // esi
int v12; // er15
int v13; // eax

if ( !trans_table )
trans_table = (__int64)calloc(0x1312D00uLL, 1uLL);
best_move_p = a2;
n_hash = 0;
n_nodes = 0;
n_evals = 0;
top_depth = 1;
v6 = alpha_beta(a1, -2000000, 2000000, 1, a3);
time_allowed = a4;
v7 = v6;
signal(14, (__sighandler_t)alarm_handler);
alarm(a4);
if ( (unsigned int)(v7 + 1999999) <= 0x3D08FE )
{
v8 = 2;
do
{
top_depth = v8;
v9 = 2000000;
v10 = -2000000;
do
{
if ( v7 == v10 )
{
v12 = v10 + 1;
v11 = v10;
}
else
{
v11 = v7 - 1;
v12 = v7;
}
v13 = alpha_beta(a1, v11, v12, v8, a3);
v7 = v13;
if ( v12 <= v13 )
v10 = v13;
if ( v12 > v13 )
v9 = v13;
}
while ( v10 < v9 );
++v8;
}
while ( (unsigned int)(v13 + 1999999) <= 0x3D08FE );
a4 = time_allowed;
}
alarm(0);
return printf(
"Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
(unsigned int)(top_depth - 1),
(unsigned int)n_nodes,
(unsigned int)n_evals,
(unsigned int)n_hash,
(double)n_evals / (double)a4);
}

canonical_decompiled_code:
int __fastcall func2(_DWORD *a1, __int64 a2, char a3, signed int a4)
{
int v6; // eax
int v7; // ebx
int v8; // er13
int v9; // er12
int v10; // ebp
int v11; // esi
int v12; // er15
int v13; // eax

if ( !trans_table )
trans_table = (__int64)calloc(0x1312D00uLL, 1uLL);
best_move_p = a2;
n_hash = 0;
n_nodes = 0;
n_evals = 0;
top_depth = 1;
v6 = func1(a1, -2000000, 2000000, 1, a3);
time_allowed = a4;
v7 = v6;
signal(14, (__sighandler_t)alarm_handler);
alarm(a4);
if ( (unsigned int)(v7 + 1999999) <= 0x3D08FE )
{
v8 = 2;
do
{
top_depth = v8;
v9 = 2000000;
v10 = -2000000;
do
{
if ( v7 == v10 )
{
v12 = v10 + 1;
v11 = v10;
}
else
{
v11 = v7 - 1;
v12 = v7;
}
v13 = func1(a1, v11, v12, v8, a3);
v7 = v13;
if ( v12 <= v13 )
v10 = v13;
if ( v12 > v13 )
v9 = v13;
}
while ( v10 < v9 );
++v8;
}
while ( (unsigned int)(v13 + 1999999) <= 0x3D08FE );
a4 = time_allowed;
}
alarm(0);
return printf(
"Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
(unsigned int)(top_depth - 1),
(unsigned int)n_nodes,
(unsigned int)n_evals,
(unsigned int)n_hash,
(double)n_evals / (double)a4);
}

original_code:
void mtdf(bitboard *board, struct move *best_move,
   const bool color, unsigned int time_s)
{
  int i, val, beta, lower_bound, upper_bound;
  if (trans_table == ((void *)0)) {
    trans_table = (struct hash_pos *)malloc(sizeof(struct hash_pos) * 500000);
    memset(trans_table, 0, sizeof(struct hash_pos) * 500000);
  }
  n_evals = n_nodes = n_hash = 0;
  best_move_p = best_move;
  top_depth = 1;
  val = alpha_beta(board, -2000000, 2000000, 1, color);
  time_allowed = time_s;
  signal(14, alarm_handler);
  alarm(time_s);
  for (i = 2; ;i++) {
    if (val >= 2000000 || val <= -2000000)
      break;
    top_depth = i;
    upper_bound = 2000000;
    lower_bound = -2000000;
    while (upper_bound > lower_bound) {
      if (val == lower_bound)
 beta = val + 1;
      else
 beta = val;
      val = alpha_beta(board, beta - 1, beta, i, color);
      if (val < beta)
 upper_bound = val;
      else
 lower_bound = val;
    }
  }
  alarm(0);
  printf("Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
  top_depth - 1, n_nodes, n_evals, n_hash, (double)n_evals/time_allowed);
}

canonical_original_code:
void mtdf(unsigned int *board, struct move *best_move, char color, unsigned int time_s)
{
  int i; int val; int beta; int lower_bound; int upper_bound;
  if (trans_table == ((void *)0)) {
    trans_table = (struct hash_pos *)malloc(sizeof(struct hash_pos) * 500000);
    memset(trans_table, 0, sizeof(struct hash_pos) * 500000);
  }
  n_evals = n_nodes = n_hash = 0;
  best_move_p = best_move;
  top_depth = 1;
  val = alpha_beta(board, -2000000, 2000000, 1, color);
  time_allowed = time_s;
  signal(14, alarm_handler);
  alarm(time_s);
  for (i = 2; ;i++) {
    if (val >= 2000000 || val <= -2000000)
      break;
    top_depth = i;
    upper_bound = 2000000;
    lower_bound = -2000000;
    while (upper_bound > lower_bound) {
      if (val == lower_bound)
 beta = val + 1;
      else
 beta = val;
      val = alpha_beta(board, beta - 1, beta, i, color);
      if (val < beta)
 upper_bound = val;
      else
 lower_bound = val;
    }
  }
  alarm(0);
  printf("Completed %d depths, %d nodes, %d evals, %d hash hits (%.02lf eval/sec)\n",
  top_depth - 1, n_nodes, n_evals, n_hash, (double)n_evals/time_allowed);
}

variable_types: struct move { unsigned int board[3]; } *


original_code:
int try_move(bitboard *board, const bool color, struct move *next_move)
{
  bitboard next, to, from;
  const bitboard empty = ~(board[1] | board[0]);
  const bitboard move_up = color ? board[2] : 0xffffffff;
  const bitboard move_down = !color ? board[2] : 0xffffffff;
  int n = 0, i;
  for (i = 0; i < 2; i++) {
    to = (((((board[(int)color] & move_down) & 0xf0f0f0f0) << down_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[(int)color] & move_down) & 0x0f0f0f0f) << down_shift[(i)][1]) & 0xf0f0f0f0)) & empty;
    while (to) {
      next = ((to) & -(to));
      from = (((((next) & 0xf0f0f0f0) >> up_shift[(i)][0]) & 0x0f0f0f0f) | ((((next) & 0x0f0f0f0f) >> up_shift[(i)][1]) & 0xf0f0f0f0));
      next_move->board[(int)color] = (board[(int)color] | next) & ~from;
      next_move->board[(int)!color] = board[(int)!color];
      next_move->board[2] = ((board[2] | (next & king_bits[(int)color])) & ~from) |
 ((((((board[2]) & 0xf0f0f0f0) << down_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[2]) & 0x0f0f0f0f) << down_shift[(i)][1]) & 0xf0f0f0f0)) & next);
      next_move++;
      n++;
      to ^= next;
    }
  }
  for (i = 0; i < 2; i++) {
    to = (((((board[(int)color] & move_up) & 0xf0f0f0f0) >> up_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[(int)color] & move_up) & 0x0f0f0f0f) >> up_shift[(i)][1]) & 0xf0f0f0f0)) & empty;
    while (to) {
      next = ((to) & -(to));
      from = (((((next) & 0xf0f0f0f0) << down_shift[(i)][0]) & 0x0f0f0f0f) | ((((next) & 0x0f0f0f0f) << down_shift[(i)][1]) & 0xf0f0f0f0));
      next_move->board[(int)color] = (board[(int)color] | next) & ~from;
      next_move->board[(int)!color] = board[(int)!color];
      next_move->board[2] = ((board[2] | (next & king_bits[(int)color])) & ~from) |
 ((((((board[2]) & 0xf0f0f0f0) >> up_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[2]) & 0x0f0f0f0f) >> up_shift[(i)][1]) & 0xf0f0f0f0)) & next);
      next_move++;
      n++;
      to ^= next;
    }
  }
  return n;
}

canonical_original_code:
int try_move(unsigned int *board, char color, struct move *next_move)
{
  unsigned int next; unsigned int to; unsigned int from;
  unsigned int empty = ~(board[1] | board[0]);
  unsigned int move_up = color ? board[2] : 0xffffffff;
  unsigned int move_down = !color ? board[2] : 0xffffffff;
  int n = 0; int i;
  for (i = 0; i < 2; i++) {
    to = (((((board[(int)color] & move_down) & 0xf0f0f0f0) << down_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[(int)color] & move_down) & 0x0f0f0f0f) << down_shift[(i)][1]) & 0xf0f0f0f0)) & empty;
    while (to) {
      next = ((to) & -(to));
      from = (((((next) & 0xf0f0f0f0) >> up_shift[(i)][0]) & 0x0f0f0f0f) | ((((next) & 0x0f0f0f0f) >> up_shift[(i)][1]) & 0xf0f0f0f0));
      next_move->board[(int)color] = (board[(int)color] | next) & ~from;
      next_move->board[(int)!color] = board[(int)!color];
      next_move->board[2] = ((board[2] | (next & king_bits[(int)color])) & ~from) |
 ((((((board[2]) & 0xf0f0f0f0) << down_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[2]) & 0x0f0f0f0f) << down_shift[(i)][1]) & 0xf0f0f0f0)) & next);
      next_move++;
      n++;
      to ^= next;
    }
  }
  for (i = 0; i < 2; i++) {
    to = (((((board[(int)color] & move_up) & 0xf0f0f0f0) >> up_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[(int)color] & move_up) & 0x0f0f0f0f) >> up_shift[(i)][1]) & 0xf0f0f0f0)) & empty;
    while (to) {
      next = ((to) & -(to));
      from = (((((next) & 0xf0f0f0f0) << down_shift[(i)][0]) & 0x0f0f0f0f) | ((((next) & 0x0f0f0f0f) << down_shift[(i)][1]) & 0xf0f0f0f0));
      next_move->board[(int)color] = (board[(int)color] | next) & ~from;
      next_move->board[(int)!color] = board[(int)!color];
      next_move->board[2] = ((board[2] | (next & king_bits[(int)color])) & ~from) |
 ((((((board[2]) & 0xf0f0f0f0) >> up_shift[(i)][0]) & 0x0f0f0f0f) | ((((board[2]) & 0x0f0f0f0f) >> up_shift[(i)][1]) & 0xf0f0f0f0)) & next);
      next_move++;
      n++;
      to ^= next;
    }
  }
  return n;
}

variable_types:
next
unsigned int

variable_types:
to
unsigned int

variable_types:
from
unsigned int

variable_types:
empty
unsigned int

variable_types: n int