decompiled_code:
void __fastcall parse_get(const char **a1, int a2)
{
const char **v2; // r12
const char **v3; // rbx
char *v4; // rbp
unsigned int *v5; // rdi

if ( a2 )
{
v2 = &a1[a2];
v3 = a1;
do
{
while ( 1 )
{
v4 = (char *)*v3;
if ( !strchr(*v3, 61) )
break;
if ( ++v3 == v2 )
return;
}
v5 = (unsigned int *)lookup_pin(v4);
if ( !v5 )
{
fprintf(stderr, "unrecognized pin \"%s\"\n", *v3);
exit(1);
}
get_pin(v5);
++v3;
}
while ( v3 != v2 );
}
}

canonical_decompiled_code:
void __fastcall func10(const char **a1, int a2)
{
const char **v2; // r12
const char **v3; // rbx
char *v4; // rbp
unsigned int *v5; // rdi

if ( a2 )
{
v2 = &a1[a2];
v3 = a1;
do
{
while ( 1 )
{
v4 = (char *)*v3;
if ( !strchr(*v3, 61) )
break;
if ( ++v3 == v2 )
return;
}
v5 = (unsigned int *)func8(v4);
if ( !v5 )
{
fprintf(stderr, "unrecognized pin \"%s\"\n", *v3);
exit(1);
}
func6(v5);
++v3;
}
while ( v3 != v2 );
}
}

original_code:
static void parse_get(const char **args, int num_args)
{
 const struct pin *pin;
 while (num_args--) {
  if (!strchr(*args, '=')) {
   pin = lookup_pin(*args);
   if (!pin) {
    fprintf(stderr, "unrecognized pin \"%s\"\n",
        *args);
    exit(1);
   }
   get_pin(pin);
  }
  args++;
 }
}

canonical_original_code:
static void parse_get(char **args, int num_args)
{
 struct pin *pin;
 while (num_args--) {
  if (!strchr(*args, '=')) {
   pin = lookup_pin(*args);
   if (!pin) {
    fprintf(stderr, "unrecognized pin \"%s\"\n",
        *args);
    exit(1);
   }
   get_pin(pin);
  }
  args++;
 }
}


usage

repo: wpwrak/idbg

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(
stderr,
"usage: %s\n %s pin[=value] ...\n %s -l\n\n -l list valid pin names\n\nPin values are 0, 1, and R.\n",
a1,
a1,
a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
fprintf(
stderr,
"usage: %s\n %s pin[=value] ...\n %s -l\n\n -l list valid pin names\n\nPin values are 0, 1, and R.\n",
a1,
a1,
a1);
exit(1);
}

original_code:
static void usage(const char *name)
{
 fprintf(stderr,
"usage: %s\n"
"       %s pin[=value] ...\n"
"       %s -l\n\n"
"  -l  list valid pin names\n\n"
"Pin values are 0, 1, and R.\n"
  , name, name, name);
 exit(1);
}

canonical_original_code:
static void usage(char *name)
{
 fprintf(stderr,
"usage: %s\n"
"       %s pin[=value] ...\n"
"       %s -l\n\n"
"  -l  list valid pin names\n\n"
"Pin values are 0, 1, and R.\n"
  , name, name, name);
 exit(1);
}

variable_types:
name
char *

return_type: void


enqueue

decompiled_code:
__int64 __fastcall enqueue(__int64 *a1, __int64 a2)
{
__int64 result; // rax

result = *a1;
if ( *a1 )
{
result = a1[1];
*(_QWORD *)(result + 8) = a2;
}
else
{
*a1 = a2;
}
a1[1] = a2;
*(_QWORD *)(a2 + 8) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1, __int64 a2)
{
__int64 result; // rax

result = *a1;
if ( *a1 )
{
result = a1[1];
*(_QWORD *)(result + 8) = a2;
}
else
{
*a1 = a2;
}
a1[1] = a2;
*(_QWORD *)(a2 + 8) = 0LL;
return result;
}

original_code:
static void enqueue( struct queue *q, struct urb *urb)
{
 if (q->head) {
  q->tail->next = urb;
 } else {
  q->head = urb;
 }
 q->tail = urb;
 urb->next = ((void *)0);
}

canonical_original_code:
static void enqueue(struct queue *q, struct urb *urb)
{
 if (q->head) {
  q->tail->next = urb;
 } else {
  q->head = urb;
 }
 q->tail = urb;
 urb->next = ((void *)0);
}

variable_types: struct queue { struct urb { unsigned char buf[2]; unsigned char len; struct urb *next; } *head; struct urb *tail; } *

variable_types:
urb
struct urb *

user_defined_types: struct queue { struct urb *head; struct urb *tail; }


dequeue

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

result = *a1;
if ( *a1 )
*a1 = *(_QWORD *)(result + 8);
return result;
}

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

result = *a1;
if ( *a1 )
*a1 = *(_QWORD *)(result + 8);
return result;
}

original_code:
static struct urb *dequeue( struct queue *q)
{
 struct urb *urb = q->head;
 if (urb)
  q->head = urb->next;
 return urb;
}

canonical_original_code:
static struct urb *dequeue(struct queue *q)
{
 struct urb *urb = q->head;
 if (urb)
  q->head = urb->next;
 return urb;
}

variable_types: struct queue { struct urb *head; struct urb *tail; } *

variable_types: struct urb { unsigned char buf[2]; unsigned char len; struct urb *next; } *


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

result = dequeue(&rx_uart_q);
if ( result )
{
*(_BYTE *)(result + 2) = rx_pos;
result = enqueue(&rx_wait_q, result);
rx_pos = 0;
}
return result;
}

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

result = func8(&rx_uart_q);
if ( result )
{
*(_BYTE *)(result + 2) = rx_pos;
result = func7(&rx_wait_q, result);
rx_pos = 0;
}
return result;
}

original_code:
static void rx_submit(void)
{
 struct urb *urb;
 urb = dequeue(&rx_uart_q);
 if (!urb)
  return;
 urb->len = rx_pos;
 enqueue(&rx_wait_q, urb);
 rx_pos = 0;
}

canonical_original_code:
static void rx_submit()
{
 struct urb *urb;
 urb = dequeue(&rx_uart_q);
 if (!urb)
  return;
 urb->len = rx_pos;
 enqueue(&rx_wait_q, urb);
 rx_pos = 0;
}

user_defined_types: struct urb { unsigned char buf[2]; unsigned char len; struct urb *next; }


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

enqueue(&rx_uart_q, rxing);
result = (unsigned __int8)rx_pos;
rxing = 0LL;
if ( rx_pos )
{
result = rx_wait_q;
if ( !rx_wait_q )
result = rx_submit();
}
return result;
}

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

func7(&rx_uart_q, rxing);
result = (unsigned __int8)rx_pos;
rxing = 0LL;
if ( rx_pos )
{
result = rx_wait_q;
if ( !rx_wait_q )
result = func10();
}
return result;
}

original_code:
static void rx_done(void *user)
{
 user;
 enqueue(&rx_uart_q, rxing);
 rxing = ((void *)0);
 if (rx_pos && !rx_wait_q.head)
  rx_submit();
}

variable_types:
user
void *


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

if ( rx_uart_q )
{
if ( (*(_BYTE *)(rx_uart_q + (unsigned __int8)rx_pos) = a1, ++rx_pos, !rxing) && !rx_wait_q
|| (result = (unsigned __int8)rx_pos, rx_pos == 2) )
{
result = rx_submit();
}
}
return result;
}

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

if ( rx_uart_q )
{
if ( (*(_BYTE *)(rx_uart_q + (unsigned __int8)rx_pos) = a1, ++rx_pos, !rxing) && !rx_wait_q
|| (result = (unsigned __int8)rx_pos, rx_pos == 2) )
{
result = func10();
}
}
return result;
}

original_code:
static void rx_enqueue(char ch)
{
 struct urb *urb;
 urb = rx_uart_q.head;
 if (!urb)
  return;
 urb->buf[rx_pos] = ch;
 rx_pos++;
 if ((!rxing && !rx_wait_q.head) || rx_pos == 2)
  rx_submit();
}

variable_types: ch char


callback

decompiled_code:
__int64 callback()
{
return sim_callback(sim_user);
}

canonical_decompiled_code:
__int64 func13()
{
return sim_callback(sim_user);
}

original_code:
static void callback(void)
{
 sim_callback(sim_user);
}

canonical_original_code:
static void callback()
{
 sim_callback(sim_user);
}


decompiled_code:
void tx_done()
{
TI0 = 1;
}

canonical_decompiled_code:
void func14()
{
TI0 = 1;
}

original_code:
static void tx_done(void)
{
 TI0 = 1;
}

canonical_original_code:
static void tx_done()
{
 TI0 = 1;
}


decompiled_code:
__int64 __fastcall usb_arrive(void *src)
{
size_t v1; // rax

fprintf(stderr, "usb_arrive \"%s\"\n", (const char *)src);
v1 = strlen((const char *)src);
memcpy(sim_buf, src, v1);
return callback();
}

canonical_decompiled_code:
__int64 __fastcall func15(void *src)
{
size_t v1; // rax

fprintf(stderr, "usb_arrive \"%s\"\n", (const char *)src);
v1 = strlen((const char *)src);
memcpy(sim_buf, src, v1);
return func13();
}

original_code:
static void usb_arrive(void *buf)
{
 fprintf(stderr, "usb_arrive \"%s\"\n", (char *) buf);
 memcpy(sim_buf, buf, strlen(buf));
 callback();
}

variable_types:
buf
void *


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

if ( RI0 )
{
result = rx_enqueue(SBUF0);
RI0 = 0;
}
return result;
}

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

if ( RI0 )
{
result = func12(SBUF0);
RI0 = 0;
}
return result;
}

original_code:
void uart_isr(void)
{
 if (!RI0)
  return;
 rx_enqueue(SBUF0);
 RI0 = 0;
}

canonical_original_code:
void uart_isr()
{
 if (!RI0)
  return;
 rx_enqueue(SBUF0);
 RI0 = 0;
}


rx

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

SBUF0 = a1;
RI0 = 1;
fprintf(stderr, "RX %c\n", (unsigned int)a1);
result = uart_isr();
RI0 = 0;
return result;
}

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

SBUF0 = a1;
RI0 = 1;
fprintf(stderr, "RX %c\n", (unsigned int)a1);
result = func16();
RI0 = 0;
return result;
}

original_code:
static void rx(char c)
{
 SBUF0 = c;
 RI0 = 1;
fprintf(stderr, "RX %c\n", c);
 uart_isr();
 RI0 = 0;
}

variable_types: c char


decompiled_code:
void do_rx()
{
unsigned __int8 *v0; // rax

if ( !rxing )
{
v0 = (unsigned __int8 *)dequeue(&rx_wait_q);
rxing = (__int64)v0;
if ( v0 )
_usb_send("usb_send: &ep1in", v0, v0[2], (__int64 (__fastcall *)(_QWORD))rx_done, 0LL);
}
}

canonical_decompiled_code:
void func19()
{
unsigned __int8 *v0; // rax

if ( !rxing )
{
v0 = (unsigned __int8 *)func8(&rx_wait_q);
rxing = (__int64)v0;
if ( v0 )
func18("usb_send: &ep1in", v0, v0[2], (__int64 (__fastcall *)(_QWORD))rx_done, 0LL);
}
}

original_code:
static void do_rx(void)
{
 if (rxing)
  return;
 {
  rxing = dequeue(&rx_wait_q);
  if (rxing)
   __usb_send("usb_send: " "&ep1in", rxing->buf, rxing->len, rx_done, ((void *)0));
 }
}

canonical_original_code:
static void do_rx()
{
 if (rxing)
  return;
 {
  rxing = dequeue(&rx_wait_q);
  if (rxing)
   __usb_send("usb_send: " "&ep1in", rxing->buf, rxing->len, rx_done, ((void *)0));
 }
}


decompiled_code:
void do_tx()
{
__int64 v0; // rax
__int64 v1; // rcx

if ( tx_uart_q )
{
txing = 1;
SBUF0 = *(_BYTE *)(tx_uart_q + (unsigned __int8)tx_pos++);
if ( tx_pos == *(_BYTE *)(tx_uart_q + 2) )
{
tx_pos = 0;
v0 = dequeue(&tx_uart_q);
enqueue(&tx_usb_q, v0);
if ( tx_usb_q == v1 )
_usb_recv("usb_recv: &ep1out", (void *)tx_usb_q, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, tx_usb_q);
}
}
else
{
txing = 0;
}
}

canonical_decompiled_code:
void func21()
{
__int64 v0; // rax
__int64 v1; // rcx

if ( tx_uart_q )
{
txing = 1;
SBUF0 = *(_BYTE *)(tx_uart_q + (unsigned __int8)tx_pos++);
if ( tx_pos == *(_BYTE *)(tx_uart_q + 2) )
{
tx_pos = 0;
v0 = func8(&tx_uart_q);
func7(&tx_usb_q, v0);
if ( tx_usb_q == v1 )
func20("usb_recv: &ep1out", (void *)tx_usb_q, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, tx_usb_q);
}
}
else
{
txing = 0;
}
}

original_code:
static void do_tx(void)
{
 struct urb *urb;
 urb = tx_uart_q.head;
 if (!urb) {
  txing = 0;
  return;
 }
 txing = 1;
 SBUF0 = urb->buf[tx_pos];
 tx_pos++;
 if (tx_pos != urb->len)
  return;
 tx_pos = 0;
 enqueue(&tx_usb_q, dequeue(&tx_uart_q));
 if (tx_usb_q.head == urb)
  __usb_recv("usb_recv: " "&ep1out", urb->buf, 2, got_tx, urb);
}

canonical_original_code:
static void do_tx()
{
 struct urb *urb;
 urb = tx_uart_q.head;
 if (!urb) {
  txing = 0;
  return;
 }
 txing = 1;
 SBUF0 = urb->buf[tx_pos];
 tx_pos++;
 if (tx_pos != urb->len)
  return;
 tx_pos = 0;
 enqueue(&tx_usb_q, dequeue(&tx_uart_q));
 if (tx_usb_q.head == urb)
  __usb_recv("usb_recv: " "&ep1out", urb->buf, 2, got_tx, urb);
}


decompiled_code:
void serial_poll()
{
do_rx();
if ( TI0 || !txing )
{
TI0 = 0;
do_tx();
}
}

original_code:
void serial_poll(void)
{
 do_rx();
 if (TI0 || !txing) {
  TI0 = 0;
  do_tx();
 }
}

canonical_original_code:
void serial_poll()
{
 do_rx();
 if (TI0 || !txing) {
  TI0 = 0;
  do_tx();
 }
}


tx

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

SBUF0 = 0;
serial_poll();
if ( SBUF0 )
result = fprintf(stderr, "tx %c\n", (unsigned __int8)SBUF0);
else
result = fwrite("----\n", 1uLL, 5uLL, stderr);
return result;
}

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

SBUF0 = 0;
func22();
if ( SBUF0 )
result = fprintf(stderr, "tx %c\n", (unsigned __int8)SBUF0);
else
result = fwrite("----\n", 1uLL, 5uLL, stderr);
return result;
}

original_code:
static void tx(void)
{
 SBUF0 = 0;
 serial_poll();
 if (SBUF0)
  fprintf(stderr, "tx %c\n", SBUF0);
 else
  fprintf(stderr, "----\n");
}

canonical_original_code:
static void tx()
{
 SBUF0 = 0;
 serial_poll();
 if (SBUF0)
  fprintf(stderr, "tx %c\n", SBUF0);
 else
  fprintf(stderr, "----\n");
}


txn

decompiled_code:
void __fastcall txn(int a1)
{
int v1; // ebx

v1 = a1 - 1;
do
{
tx();
tx();
tx_done();
}
while ( v1-- != 0 );
}

canonical_decompiled_code:
void __fastcall func24(int a1)
{
int v1; // ebx

v1 = a1 - 1;
do
{
func23();
func23();
func14();
}
while ( v1-- != 0 );
}

original_code:
static void txn(int n)
{
 while (n--) {
  tx();
  tx();
  tx_done();
 }
}

variable_types: n int


decompiled_code:
int __fastcall got_tx(__int64 a1)
{
__int64 v1; // rax
int result; // eax

*(_BYTE *)(a1 + 2) = 2;
v1 = dequeue(&tx_usb_q);
result = enqueue(&tx_uart_q, v1);
if ( tx_usb_q )
result = _usb_recv("usb_recv: &ep1out", (void *)tx_usb_q, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, tx_usb_q);
return result;
}

canonical_decompiled_code:
int __fastcall func25(__int64 a1)
{
__int64 v1; // rax
int result; // eax

*(_BYTE *)(a1 + 2) = 2;
v1 = func8(&tx_usb_q);
result = func7(&tx_uart_q, v1);
if ( tx_usb_q )
result = func20("usb_recv: &ep1out", (void *)tx_usb_q, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, tx_usb_q);
return result;
}

original_code:
static void got_tx(void *user)
{
 struct urb *urb = user;
 urb->len = 2 -0;
 enqueue(&tx_uart_q, dequeue(&tx_usb_q));
 urb = tx_usb_q.head;
 if (urb)
  __usb_recv("usb_recv: " "&ep1out", urb->buf, 2, got_tx, urb);
}


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

init_queue(&tx_usb_q, (__int64)&tx_urbs, 2u);
init_queue(&tx_uart_q, 0LL, 0);
init_queue(&rx_uart_q, (__int64)&rx_urbs, 0xAu);
init_queue(&rx_wait_q, 0LL, 0);
result = _usb_recv("usb_recv: &ep1out", &tx_urbs, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, (__int64)&tx_urbs);
REN0 = 1;
ES0 = 1;
return result;
}

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

func9(&tx_usb_q, (__int64)&tx_urbs, 2u);
func9(&tx_uart_q, 0LL, 0);
func9(&rx_uart_q, (__int64)&rx_urbs, 0xAu);
func9(&rx_wait_q, 0LL, 0);
result = func20("usb_recv: &ep1out", &tx_urbs, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, (__int64)&tx_urbs);
REN0 = 1;
ES0 = 1;
return result;
}

original_code:
void serial_init(void)
{
 init_queue(&tx_usb_q, tx_urbs, 2);
 init_queue(&tx_uart_q, ((void *)0), 0);
 init_queue(&rx_uart_q, rx_urbs, 10);
 init_queue(&rx_wait_q, ((void *)0), 0);
 __usb_recv("usb_recv: " "&ep1out", tx_urbs->buf, 2, got_tx, tx_urbs);
 REN0 = 1;
 ES0 = 1;
}

canonical_original_code:
void serial_init()
{
 init_queue(&tx_usb_q, tx_urbs, 2);
 init_queue(&tx_uart_q, ((void *)0), 0);
 init_queue(&rx_uart_q, rx_urbs, 10);
 init_queue(&rx_wait_q, ((void *)0), 0);
 __usb_recv("usb_recv: " "&ep1out", tx_urbs->buf, 2, got_tx, tx_urbs);
 REN0 = 1;
 ES0 = 1;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
serial_init(argc, argv, envp);
serial_poll();
usb_arrive("TEST");
usb_arrive("MORE");
txn(4LL);
txn(5LL);
usb_arrive("XXXX");
usb_arrive("YYYY");
txn(10LL);
rx(65LL);
serial_poll();
rx(66LL);
rx(67LL);
rx(68LL);
rx(69LL);
rx(70LL);
rx(71LL);
serial_poll();
serial_poll();
callback();
serial_poll();
serial_poll();
callback();
rx(72LL);
serial_poll();
serial_poll();
callback();
serial_poll();
serial_poll();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func26(argc, argv, envp);
func22();
func15("TEST");
func15("MORE");
func24(4LL);
func24(5LL);
func15("XXXX");
func15("YYYY");
func24(10LL);
func17(65LL);
func22();
func17(66LL);
func17(67LL);
func17(68LL);
func17(69LL);
func17(70LL);
func17(71LL);
func22();
func22();
func13();
func22();
func22();
func13();
func17(72LL);
func22();
func22();
func13();
func22();
func22();
return 0;
}

original_code:
int main(void)
{
 serial_init();
 serial_poll();
 usb_arrive("TEST");
 usb_arrive("MORE");
 txn(4);
 txn(5);
 usb_arrive("XXXX");
 usb_arrive("YYYY");
 txn(10);
 rx('A');
 serial_poll();
 rx('B');
 rx('C');
 rx('D');
 rx('E');
 rx('F');
 rx('G');
 serial_poll();
 serial_poll();
 callback();
 serial_poll();
 serial_poll();
 callback();
 rx('H');
 serial_poll();
 serial_poll();
 callback();
 serial_poll();
 serial_poll();
 return 0;
}

canonical_original_code:
int main()
{
 serial_init();
 serial_poll();
 usb_arrive("TEST");
 usb_arrive("MORE");
 txn(4);
 txn(5);
 usb_arrive("XXXX");
 usb_arrive("YYYY");
 txn(10);
 rx('A');
 serial_poll();
 rx('B');
 rx('C');
 rx('D');
 rx('E');
 rx('F');
 rx('G');
 serial_poll();
 serial_poll();
 callback();
 serial_poll();
 serial_poll();
 callback();
 rx('H');
 serial_poll();
 serial_poll();
 callback();
 serial_poll();
 serial_poll();
 return 0;
}

return_type: int


cleanup

decompiled_code:
ssize_t cleanup()
{
if ( tcsetattr(0, 0, &console) < 0 )
perror("tcsetattr");
return write(1, "\n", 1uLL);
}

canonical_decompiled_code:
ssize_t func7()
{
if ( tcsetattr(0, 0, &console) < 0 )
perror("tcsetattr");
return write(1, "\n", 1uLL);
}

original_code:
static void cleanup(void)
{
 if (tcsetattr(0, 0, &console) < 0)
  perror("tcsetattr");
 write(1, "\n", 1);
}

canonical_original_code:
static void cleanup()
{
 if (tcsetattr(0, 0, &console) < 0)
  perror("tcsetattr");
 write(1, "\n", 1);
}


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(stderr, "usage: %s [-d vendor:product]\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
fprintf(stderr, "usage: %s [-d vendor:product]\n", a1);
exit(1);
}

original_code:
static void usage(const char *name)
{
 fprintf(stderr, "usage: %s [-d vendor:product]\n", name);
 exit(1);
}

canonical_original_code:
static void usage(char *name)
{
 fprintf(stderr, "usage: %s [-d vendor:product]\n", name);
 exit(1);
}


decompiled_code:
void __fastcall __noreturn bad_id(const char *a1)
{
fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func10(const char *a1)
{
fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", a1);
exit(1);
}

original_code:
static void bad_id(const char *id)
{
 fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id);
 exit(1);
}

canonical_original_code:
static void bad_id(char *id)
{
 fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id);
 exit(1);
}

variable_types:
id
char *


decompiled_code:
unsigned __int64 __fastcall parse_usb_id(const char *a1)
{
unsigned __int64 v1; // rax
unsigned __int64 result; // rax
char *endptr[2]; // [rsp+8h] [rbp-10h] BYREF

v1 = strtoul(a1, endptr, 16);
if ( *endptr[0] != 58
|| v1 > 0xFFFF
|| (vendor = v1, result = strtoul(endptr[0] + 1, endptr, 16), *endptr[0])
|| result > 0xFFFF )
{
bad_id(a1);
}
product = result;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func12(const char *a1)
{
unsigned __int64 v1; // rax
unsigned __int64 result; // rax
char *endptr[2]; // [rsp+8h] [rbp-10h] BYREF

v1 = strtoul(a1, endptr, 16);
if ( *endptr[0] != 58
|| v1 > 0xFFFF
|| (vendor = v1, result = strtoul(endptr[0] + 1, endptr, 16), *endptr[0])
|| result > 0xFFFF )
{
func10(a1);
}
product = result;
return result;
}

original_code:
void parse_usb_id(const char *id)
{
 unsigned long tmp;
 char *end;
 tmp = strtoul(id, &end, 16);
 if (*end != ':')
  bad_id(id);
 if (tmp > 0xffff)
  bad_id(id);
 vendor = tmp;
 tmp = strtoul(end+1, &end, 16);
 if (*end)
  bad_id(id);
 if (tmp > 0xffff)
  bad_id(id);
 product = tmp;
}

canonical_original_code:
void parse_usb_id(char *id)
{
 unsigned long tmp;
 char *end;
 tmp = strtoul(id, &end, 16);
 if (*end != ':')
  bad_id(id);
 if (tmp > 0xffff)
  bad_id(id);
 vendor = tmp;
 tmp = strtoul(end+1, &end, 16);
 if (*end)
  bad_id(id);
 if (tmp > 0xffff)
  bad_id(id);
 product = tmp;
}

variable_types:
tmp
unsigned long

variable_types:
end
char *


canonical_decompiled_code:
void __fastcall __noreturn func11(const char *a1)
{
fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", a1);
exit(1);
}


canonical_decompiled_code:
unsigned __int64 __fastcall func13(const char *a1)
{
unsigned __int64 v1; // rax
unsigned __int64 result; // rax
char *endptr[2]; // [rsp+8h] [rbp-10h] BYREF

v1 = strtoul(a1, endptr, 16);
if ( *endptr[0] != 58
|| v1 > 0xFFFF
|| (vendor = v1, result = strtoul(endptr[0] + 1, endptr, 16), *endptr[0])
|| result > 0xFFFF )
{
func11(a1);
}
product = result;
return result;
}


repo: yac/usperf

decompiled_code:
int __fastcall pcounter_enable(int *a1)
{
return ioctl(*a1, 0x2400uLL);
}

canonical_decompiled_code:
int __fastcall func1(int *a1)
{
return ioctl(*a1, 0x2400uLL);
}

variable_types: struct pcounter { int fd; void *base; char state; int prev; } *


decompiled_code:
int __fastcall pcounter_close(__int64 a1)
{
munmap(*(void **)(a1 + 8), (unsigned int)(129 * page_size));
return close(*(_DWORD *)a1);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
munmap(*(void **)(a1 + 8), (unsigned int)(129 * page_size));
return close(*(_DWORD *)a1);
}

original_code:
void pcounter_close(struct pcounter *cnt)
{
 munmap(cnt->base, (mmap_pages + 1) * page_size);
 close(cnt->fd);
}

user_defined_types: struct pcounter { int fd; void *base; char state; int prev; }


parseArgs

decompiled_code:
void __fastcall parseArgs(int a1)
{
if ( a1 != 3 )
{
printf("invalid arguments. expected: shiva --times=\"1234,1,2\" --moves=\"1. wB1, 2.bG1 -wB1, 3.etc\"");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 != 3 )
{
printf("invalid arguments. expected: shiva --times=\"1234,1,2\" --moves=\"1. wB1, 2.bG1 -wB1, 3.etc\"");
exit(0);
}
}

original_code:
void parseArgs(int argCount)
{
 if (argCount != 3)
    {
        printf( "invalid arguments. expected: shiva --times=\"1234,1,2\" --moves=\"1. wB1, 2.bG1 -wB1, 3.etc\"");
  exit(0);
    }
}

variable_types: argCount int


cleanup

decompiled_code:
void cleanup()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void cleanup(void)
{
}

canonical_original_code:
void cleanup()
{
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
parseArgs(argc);
initState();
parseTimeString(argv[1]);
parseMoveStringAndSetupBoardState(argv[2]);
calculateBestMove(moveNumber & 1);
printOutBestMove();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func0(argc);
initState();
parseTimeString(argv[1]);
parseMoveStringAndSetupBoardState(argv[2]);
calculateBestMove(moveNumber & 1);
printOutBestMove();
return 0;
}

original_code:
int main (int argc, const char * argv[])
{
 parseArgs(argc);
 initState();
 parseTimeString(argv[1]);
 parseMoveStringAndSetupBoardState(argv[2]);
 calculateBestMove(moveNumber%2 == 1 ? 1 : 0);
 printOutBestMove();
 cleanup();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 parseArgs(argc);
 initState();
 parseTimeString(argv[1]);
 parseMoveStringAndSetupBoardState(argv[2]);
 calculateBestMove(moveNumber%2 == 1 ? 1 : 0);
 printOutBestMove();
 cleanup();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


alphaBeta

decompiled_code:
__int64 __fastcall alphaBeta(int a1, int a2, int a3, unsigned __int8 a4)
{
unsigned __int8 v4; // r13
unsigned int v6; // eax
__int64 result; // rax
__int64 v8; // r15
unsigned int v9; // er14
unsigned int v10; // ebx
int v11; // eax
int v12; // er13
int v13; // eax
__int64 v14; // [rsp+0h] [rbp-8A8h]
unsigned __int8 v17; // [rsp+Fh] [rbp-899h]
char v18[96]; // [rsp+10h] [rbp-898h] BYREF
int v19[526]; // [rsp+70h] [rbp-838h] BYREF

v4 = a4;
memset(v18, 0, sizeof(v18));
v6 = AddAllLegalMovesToMoveList(v19, v18, a4);
if ( v6 )
{
v17 = v4 ^ 1;
if ( !a1 )
{
LABEL_4:
++numberEvaluations;
AddAllLegalMovesToMoveList(&v19[v6 - 1], v18, v4 ^ 1u);
result = evaluateBoard(v18);
if ( !v4 )
result = (unsigned int)-(int)result;
return result;
}
++numberNodes;
v8 = 0LL;
v14 = v6;
while ( 1 )
{
v9 = pieces[BYTE2(v19[v8])];
v10 = BYTE2(v19[v8]);
playMove(v19, (unsigned int)v8);
v11 = isWin();
if ( !a4 )
v11 = -v11;
v12 = v11 + a1;
if ( v11 <= 1 )
{
if ( v11 < 0 )
{
v12 = v11 - a1;
}
else
{
v12 = 0;
if ( v11 != 1 )
v12 = -(int)alphaBeta((unsigned int)(a1 - 1), (unsigned int)-a3, (unsigned int)-a2, v17);
}
}
undoMove(v9, v10);
if ( v12 >= a3 )
break;
if ( v12 > a2 )
{
a2 = v12;
if ( currentDepth == a1 )
{
v13 = v19[v8];
currentScore = v12;
currMove = v13;
}
}
if ( ++v8 == v14 )
goto LABEL_17;
}
result = (unsigned int)a3;
}
else
{
if ( currentDepth == a1 )
{
printf("pass");
exit(0);
}
if ( !a1 )
goto LABEL_4;
++numberNodes;
LABEL_17:
result = (unsigned int)a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, int a3, unsigned __int8 a4)
{
unsigned __int8 v4; // r13
unsigned int v6; // eax
__int64 result; // rax
__int64 v8; // r15
unsigned int v9; // er14
unsigned int v10; // ebx
int v11; // eax
int v12; // er13
int v13; // eax
__int64 v14; // [rsp+0h] [rbp-8A8h]
unsigned __int8 v17; // [rsp+Fh] [rbp-899h]
char v18[96]; // [rsp+10h] [rbp-898h] BYREF
int v19[526]; // [rsp+70h] [rbp-838h] BYREF

v4 = a4;
memset(v18, 0, sizeof(v18));
v6 = AddAllLegalMovesToMoveList(v19, v18, a4);
if ( v6 )
{
v17 = v4 ^ 1;
if ( !a1 )
{
LABEL_4:
++numberEvaluations;
AddAllLegalMovesToMoveList(&v19[v6 - 1], v18, v4 ^ 1u);
result = evaluateBoard(v18);
if ( !v4 )
result = (unsigned int)-(int)result;
return result;
}
++numberNodes;
v8 = 0LL;
v14 = v6;
while ( 1 )
{
v9 = pieces[BYTE2(v19[v8])];
v10 = BYTE2(v19[v8]);
playMove(v19, (unsigned int)v8);
v11 = isWin();
if ( !a4 )
v11 = -v11;
v12 = v11 + a1;
if ( v11 <= 1 )
{
if ( v11 < 0 )
{
v12 = v11 - a1;
}
else
{
v12 = 0;
if ( v11 != 1 )
v12 = -(int)func0((unsigned int)(a1 - 1), (unsigned int)-a3, (unsigned int)-a2, v17);
}
}
undoMove(v9, v10);
if ( v12 >= a3 )
break;
if ( v12 > a2 )
{
a2 = v12;
if ( currentDepth == a1 )
{
v13 = v19[v8];
currentScore = v12;
currMove = v13;
}
}
if ( ++v8 == v14 )
goto LABEL_17;
}
result = (unsigned int)a3;
}
else
{
if ( currentDepth == a1 )
{
printf("pass");
exit(0);
}
if ( !a1 )
goto LABEL_4;
++numberNodes;
LABEL_17:
result = (unsigned int)a2;
}
return result;
}

original_code:
int alphaBeta(int depth, int alpha, int beta, _Bool whiteToPlay)
{
 int val, i;
 EVALUATE_INFO_t eval = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 uint32_t legalMoves = 0;
 PIECE_t pieceBackup;
 PIECE_e piece;
 MOVE_t moves[(512)];
 legalMoves += AddAllLegalMovesToMoveList(moves, &eval, whiteToPlay);
 if (!legalMoves && depth == currentDepth)
 {
  printf("pass");
  exit(0);
 }
 if (depth == 0)
 {
  numberEvaluations++;
  AddAllLegalMovesToMoveList(&moves[legalMoves-1], &eval, !whiteToPlay);
  val = evaluateBoard(&eval);
  if (!whiteToPlay)
   val = val * -1;
  return val;
 }
 numberNodes++;
 for (i = 0; i < legalMoves; i++)
 {
  piece = (moves[i].move & (0x00FF0000)) >> (16);
  pieceBackup.piece = pieces[piece].piece;
  playMove(moves, i);
  val = isWin();
  if (!whiteToPlay)
  {
   val = val*-1;
  }
  if (val > (1))
  {
   val = val + depth;
  }
  else if (val < 0)
  {
   val = val - depth;
  }
  else if (val == (1))
  {
   val = 0;
  }
  else
  {
   val = -alphaBeta(depth - 1, -beta, -alpha, !whiteToPlay);
  }
  undoMove(pieceBackup, piece);
  if (val >= beta)
  {
   return beta;
  }
  if (val > alpha)
  {
   alpha = val;
   if (depth == currentDepth)
   {
    currMove.move = moves[i].move;
    currentScore = val;
   }
  }
 }
 return alpha;
}

canonical_original_code:
int alphaBeta(int depth, int alpha, int beta, _Bool whiteToPlay)
{
 int val; int i;
 struct EVALUATE_INFO_t eval = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 unsigned int legalMoves = 0;
 union PIECE_t pieceBackup;
 enum PIECE_e piece;
 union MOVE_t moves[-1];
 legalMoves += AddAllLegalMovesToMoveList(moves, &eval, whiteToPlay);
 if (!legalMoves && depth == currentDepth)
 {
  printf("pass");
  exit(0);
 }
 if (depth == 0)
 {
  numberEvaluations++;
  AddAllLegalMovesToMoveList(&moves[legalMoves-1], &eval, !whiteToPlay);
  val = evaluateBoard(&eval);
  if (!whiteToPlay)
   val = val * -1;
  return val;
 }
 numberNodes++;
 for (i = 0; i < legalMoves; i++)
 {
  piece = (moves[i].move & (0x00FF0000)) >> (16);
  pieceBackup.piece = pieces[piece].piece;
  playMove(moves, i);
  val = isWin();
  if (!whiteToPlay)
  {
   val = val*-1;
  }
  if (val > (1))
  {
   val = val + depth;
  }
  else if (val < 0)
  {
   val = val - depth;
  }
  else if (val == (1))
  {
   val = 0;
  }
  else
  {
   val = -alphaBeta(depth - 1, -beta, -alpha, !whiteToPlay);
  }
  undoMove(pieceBackup, piece);
  if (val >= beta)
  {
   return beta;
  }
  if (val > alpha)
  {
   alpha = val;
   if (depth == currentDepth)
   {
    currMove.move = moves[i].move;
    currentScore = val;
   }
  }
 }
 return alpha;
}

variable_types: depth int

variable_types: alpha int

variable_types: beta int

variable_types: val int

variable_types: i int

variable_types: struct EVALUATE_INFO_t { unsigned int validMoves[-1]; unsigned int whiteMoves; unsigned int blackMoves; }

variable_types:
legalMoves
unsigned int

variable_types: union PIECE_t { struct { unsigned int type; unsigned int number; unsigned int color; unsigned int xcoords; unsigned int ycoords; unsigned int zcoords; unsigned int played; } PIECE_BITS_t; unsigned int piece; }

variable_types: union MOVE_t { struct { unsigned int xpos; unsigned int ypos; enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 } piece; unsigned int reserved; } MOVE_BITS_t; unsigned int move; } [-1]

user_defined_types: union MOVE_t { struct { unsigned int xpos; unsigned int ypos; enum PIECE_e piece; unsigned int reserved; } MOVE_BITS_t; unsigned int move; }


calculateBestMove

decompiled_code:
int __fastcall calculateBestMove(unsigned __int8 a1)
{
unsigned int v1; // er12

currentDepth = 4;
startTimer();
alphaBeta(currentDepth, -2147483647, 0x7FFFFFFF, a1);
v1 = getSearchTime();
fprintf(stderr, "Number of nodes searched = %d\n", (unsigned int)numberNodes);
fprintf(stderr, "Number of evaluations = %d\n", (unsigned int)numberEvaluations);
fprintf(stderr, "Time taken = %dms\n", v1);
return fprintf(stderr, "Position score = %d\n", (unsigned int)currentScore);
}

canonical_decompiled_code:
int __fastcall func1(unsigned __int8 a1)
{
unsigned int v1; // er12

currentDepth = 4;
startTimer();
func0(currentDepth, -2147483647, 0x7FFFFFFF, a1);
v1 = getSearchTime();
fprintf(stderr, "Number of nodes searched = %d\n", (unsigned int)numberNodes);
fprintf(stderr, "Number of evaluations = %d\n", (unsigned int)numberEvaluations);
fprintf(stderr, "Time taken = %dms\n", v1);
return fprintf(stderr, "Position score = %d\n", (unsigned int)currentScore);
}

original_code:
void calculateBestMove(_Bool whiteToPlay)
{
 int timeTakenSearching;
 currentDepth = (4);
 startTimer();
 alphaBeta(currentDepth, -(0x7fffffff), (0x7fffffff), whiteToPlay);
 timeTakenSearching = getSearchTime();
 do { if (1) fprintf(stderr, "Number of nodes searched = %d\n", numberNodes); } while (0);
 do { if (1) fprintf(stderr, "Number of evaluations = %d\n", numberEvaluations); } while (0);
 do { if (1) fprintf(stderr, "Time taken = %dms\n", timeTakenSearching); } while (0);
 do { if (1) fprintf(stderr, "Position score = %d\n", currentScore); } while (0);
}

variable_types: timeTakenSearching int


initSearch

decompiled_code:
void initSearch()
{
numberEvaluations = 0;
numberNodes = 0;
currentDepth = 0;
currMove = 0;
currentScore = 0;
}

canonical_decompiled_code:
void func2()
{
numberEvaluations = 0;
numberNodes = 0;
currentDepth = 0;
currMove = 0;
currentScore = 0;
}

original_code:
void initSearch(void)
{
 numberEvaluations = 0;
 numberNodes = 0;
 currentDepth = 0;
 currMove.move = 0;
 currentScore = 0;
}

canonical_original_code:
void initSearch()
{
 numberEvaluations = 0;
 numberNodes = 0;
 currentDepth = 0;
 currMove.move = 0;
 currentScore = 0;
}


getPieceToMove

decompiled_code:
__int64 __fastcall getPieceToMove(char *a1)
{
return translateTypeAndNumberToIndex((unsigned int)*a1, (unsigned int)a1[1], (unsigned int)a1[2]);
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
return translateTypeAndNumberToIndex((unsigned int)*a1, (unsigned int)a1[1], (unsigned int)a1[2]);
}

original_code:
PIECE_e getPieceToMove(char* pieceToMove)
{
 char* colorToMove;
 char* numberToMove;
 char* typeToMove;
 PIECE_e piece;
 colorToMove = &pieceToMove[0];
 typeToMove = &pieceToMove[1];
 numberToMove = &pieceToMove[2];
 piece = translateTypeAndNumberToIndex(*colorToMove, *typeToMove, *numberToMove);
 return piece;
}

canonical_original_code:
enum PIECE_e getPieceToMove(char *pieceToMove)
{
 char *colorToMove;
 char *numberToMove;
 char *typeToMove;
 enum PIECE_e piece;
 colorToMove = &pieceToMove[0];
 typeToMove = &pieceToMove[1];
 numberToMove = &pieceToMove[2];
 piece = translateTypeAndNumberToIndex(*colorToMove, *typeToMove, *numberToMove);
 return piece;
}

variable_types:
pieceToMove
char *

variable_types:
colorToMove
char *

variable_types:
numberToMove
char *

variable_types:
typeToMove
char *

variable_types: enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 }


getAdjacentPiece

decompiled_code:
__int64 __fastcall getAdjacentPiece(char *a1, _DWORD *a2)
{
char v2; // al
char *v3; // rdx
char *v4; // rcx
char v5; // al
char *v6; // rax

v2 = *a1;
v3 = a1 + 1;
v4 = a1 + 2;
if ( *a1 == 45 )
{
*a2 = 4;
v6 = a1 + 3;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
if ( v2 == 92 )
{
*a2 = 5;
v6 = a1 + 3;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
if ( v2 == 47 )
{
*a2 = 3;
v6 = a1 + 3;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
if ( a1[1] != 81 )
{
v5 = a1[3];
if ( v5 != 47 )
goto LABEL_6;
LABEL_14:
v6 = a1 + 2;
*a2 = 0;
v4 = a1 + 1;
v3 = a1;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
v5 = a1[2];
if ( v5 == 47 )
goto LABEL_14;
LABEL_6:
if ( v5 == 45 )
{
v6 = a1 + 2;
*a2 = 1;
v4 = a1 + 1;
v3 = a1;
}
else
{
if ( v5 == 92 )
{
v6 = a1 + 2;
*a2 = 2;
}
else
{
v6 = a1 + 2;
*a2 = 6;
}
v4 = a1 + 1;
v3 = a1;
}
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, _DWORD *a2)
{
char v2; // al
char *v3; // rdx
char *v4; // rcx
char v5; // al
char *v6; // rax

v2 = *a1;
v3 = a1 + 1;
v4 = a1 + 2;
if ( *a1 == 45 )
{
*a2 = 4;
v6 = a1 + 3;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
if ( v2 == 92 )
{
*a2 = 5;
v6 = a1 + 3;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
if ( v2 == 47 )
{
*a2 = 3;
v6 = a1 + 3;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
if ( a1[1] != 81 )
{
v5 = a1[3];
if ( v5 != 47 )
goto LABEL_6;
LABEL_14:
v6 = a1 + 2;
*a2 = 0;
v4 = a1 + 1;
v3 = a1;
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}
v5 = a1[2];
if ( v5 == 47 )
goto LABEL_14;
LABEL_6:
if ( v5 == 45 )
{
v6 = a1 + 2;
*a2 = 1;
v4 = a1 + 1;
v3 = a1;
}
else
{
if ( v5 == 92 )
{
v6 = a1 + 2;
*a2 = 2;
}
else
{
v6 = a1 + 2;
*a2 = 6;
}
v4 = a1 + 1;
v3 = a1;
}
return translateTypeAndNumberToIndex((unsigned int)*v3, (unsigned int)*v4, (unsigned int)*v6);
}

original_code:
PIECE_e getAdjacentPiece(char* adjacentPiece, DIRECTIONS_e* adjacencyDirection)
{
 char* colorToMove;
 char* numberToMove;
 char* typeToMove;
 PIECE_e piece;
 if ('-' == adjacentPiece[0])
 {
  colorToMove = &adjacentPiece[1];
  typeToMove = &adjacentPiece[2];
  numberToMove = &adjacentPiece[3];
  *adjacencyDirection = WEST;
 }
 else if ('\\' == adjacentPiece[0])
 {
  colorToMove = &adjacentPiece[1];
  typeToMove = &adjacentPiece[2];
  numberToMove = &adjacentPiece[3];
  *adjacencyDirection = NORTHWEST;
 }
 else if ('/' == adjacentPiece[0])
 {
  colorToMove = &adjacentPiece[1];
  typeToMove = &adjacentPiece[2];
  numberToMove = &adjacentPiece[3];
  *adjacencyDirection = SOUTHWEST;
 }
 else
 {
  colorToMove = &adjacentPiece[0];
  typeToMove = &adjacentPiece[1];
  numberToMove = &adjacentPiece[2];
  if(*typeToMove == 'Q')
  {
   if ('/' == adjacentPiece[2])
    *adjacencyDirection = NORTHEAST;
   else if ('-' == adjacentPiece[2])
    *adjacencyDirection = EAST;
   else if ('\\' == adjacentPiece[2])
    *adjacencyDirection = SOUTHEAST;
   else
    *adjacencyDirection = MAX_DIR;
  }
  else
  {
   if ('/' == adjacentPiece[3])
    *adjacencyDirection = NORTHEAST;
   else if ('-' == adjacentPiece[3])
    *adjacencyDirection = EAST;
   else if ('\\' == adjacentPiece[3])
    *adjacencyDirection = SOUTHEAST;
   else
    *adjacencyDirection = MAX_DIR;
  }
 }
 piece = translateTypeAndNumberToIndex(*colorToMove, *typeToMove, *numberToMove);
 return piece;
}

canonical_original_code:
enum PIECE_e getAdjacentPiece(char *adjacentPiece, enum DIRECTIONS_e *adjacencyDirection)
{
 char *colorToMove;
 char *numberToMove;
 char *typeToMove;
 enum PIECE_e piece;
 if ('-' == adjacentPiece[0])
 {
  colorToMove = &adjacentPiece[1];
  typeToMove = &adjacentPiece[2];
  numberToMove = &adjacentPiece[3];
  *adjacencyDirection = WEST;
 }
 else if ('\\' == adjacentPiece[0])
 {
  colorToMove = &adjacentPiece[1];
  typeToMove = &adjacentPiece[2];
  numberToMove = &adjacentPiece[3];
  *adjacencyDirection = NORTHWEST;
 }
 else if ('/' == adjacentPiece[0])
 {
  colorToMove = &adjacentPiece[1];
  typeToMove = &adjacentPiece[2];
  numberToMove = &adjacentPiece[3];
  *adjacencyDirection = SOUTHWEST;
 }
 else
 {
  colorToMove = &adjacentPiece[0];
  typeToMove = &adjacentPiece[1];
  numberToMove = &adjacentPiece[2];
  if(*typeToMove == 'Q')
  {
   if ('/' == adjacentPiece[2])
    *adjacencyDirection = NORTHEAST;
   else if ('-' == adjacentPiece[2])
    *adjacencyDirection = EAST;
   else if ('\\' == adjacentPiece[2])
    *adjacencyDirection = SOUTHEAST;
   else
    *adjacencyDirection = MAX_DIR;
  }
  else
  {
   if ('/' == adjacentPiece[3])
    *adjacencyDirection = NORTHEAST;
   else if ('-' == adjacentPiece[3])
    *adjacencyDirection = EAST;
   else if ('\\' == adjacentPiece[3])
    *adjacencyDirection = SOUTHEAST;
   else
    *adjacencyDirection = MAX_DIR;
  }
 }
 piece = translateTypeAndNumberToIndex(*colorToMove, *typeToMove, *numberToMove);
 return piece;
}

variable_types:
adjacentPiece
char *

variable_types: enum DIRECTIONS_e { NORTHEAST=0, EAST=1, SOUTHEAST=2, SOUTHWEST=3, WEST=4, NORTHWEST=5, MAX_DIR=6 } *

return_type: enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 }


parseMoveStringAndSetupBoardState

decompiled_code:
char *__fastcall parseMoveStringAndSetupBoardState(char *a1)
{
char *v1; // rax
unsigned int v2; // eax
char *result; // rax
unsigned int v4; // ebp
unsigned int v5; // eax
char v6; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned int v7; // [rsp+10h] [rbp-18h] BYREF
char v8[5]; // [rsp+16h] [rbp-12h] BYREF
char v9[13]; // [rsp+1Bh] [rbp-Dh] BYREF

moveNumber = 1;
strtok(a1, "=,");
v1 = strtok(0LL, "\",");
if ( !v1 || v1 == "," || (unsigned int)_isoc99_sscanf(v1, "%d. %5s", &v6, v8) == -1 )
{
printf("wS1");
exit(0);
}
v2 = getPieceToMove(v8);
playFirstPiece(v2);
while ( 1 )
{
++moveNumber;
result = strtok(0LL, ",");
if ( result == ", " )
break;
if ( !result )
break;
result = (char *)_isoc99_sscanf(result, "%d. %s %s", &v6, v8, v9);
if ( (_DWORD)result == -1 )
break;
v4 = getPieceToMove(v8);
v5 = getAdjacentPiece(v9, &v7);
movePiece(v4, v5, v7);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1)
{
char *v1; // rax
unsigned int v2; // eax
char *result; // rax
unsigned int v4; // ebp
unsigned int v5; // eax
char v6; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned int v7; // [rsp+10h] [rbp-18h] BYREF
char v8[5]; // [rsp+16h] [rbp-12h] BYREF
char v9[13]; // [rsp+1Bh] [rbp-Dh] BYREF

moveNumber = 1;
strtok(a1, "=,");
v1 = strtok(0LL, "\",");
if ( !v1 || v1 == "," || (unsigned int)_isoc99_sscanf(v1, "%d. %5s", &v6, v8) == -1 )
{
printf("wS1");
exit(0);
}
v2 = func0(v8);
playFirstPiece(v2);
while ( 1 )
{
++moveNumber;
result = strtok(0LL, ",");
if ( result == ", " )
break;
if ( !result )
break;
result = (char *)_isoc99_sscanf(result, "%d. %s %s", &v6, v8, v9);
if ( (_DWORD)result == -1 )
break;
v4 = func0(v8);
v5 = func1(v9, &v7);
movePiece(v4, v5, v7);
}
return result;
}

original_code:
void parseMoveStringAndSetupBoardState(char* moveString)
{
 char* nextMove;
 int moveNum;
 char pieceToMove[5];
 PIECE_e piece;
 char adjacentPiece[5];
 PIECE_e pieceAdjacent;
 DIRECTIONS_e adjacencyDirection;
 moveNumber = 1;
 nextMove = strtok(moveString, "=,");
 nextMove = strtok(((void *)0), "\",");
 if (nextMove == ((void *)0) || nextMove == ",")
 {
  printf("wS1");
  exit(0);
 }
 if ((-1) == sscanf(nextMove, "%d. %5s", &moveNum, pieceToMove))
 {
  printf("wS1");
  exit(0);
 }
 piece = getPieceToMove(pieceToMove);
 playFirstPiece(piece);
 moveNumber++;
 nextMove = strtok(((void *)0), ",");
 while (nextMove != ((void *)0) && nextMove != ", ")
 {
  if ((-1) == sscanf(nextMove, "%d. %s %s", &moveNum, pieceToMove, adjacentPiece))
  {
   break;
  }
  piece = getPieceToMove(pieceToMove);
  pieceAdjacent = getAdjacentPiece(adjacentPiece, &adjacencyDirection);
  movePiece(piece, pieceAdjacent, adjacencyDirection);
  moveNumber++;
  nextMove = strtok(((void *)0), ",");
 }
}

canonical_original_code:
void parseMoveStringAndSetupBoardState(char *moveString)
{
 char *nextMove;
 int moveNum;
 char pieceToMove[5];
 enum PIECE_e piece;
 char adjacentPiece[5];
 enum PIECE_e pieceAdjacent;
 enum DIRECTIONS_e adjacencyDirection;
 moveNumber = 1;
 nextMove = strtok(moveString, "=,");
 nextMove = strtok(((void *)0), "\",");
 if (nextMove == ((void *)0) || nextMove == ",")
 {
  printf("wS1");
  exit(0);
 }
 if ((-1) == sscanf(nextMove, "%d. %5s", &moveNum, pieceToMove))
 {
  printf("wS1");
  exit(0);
 }
 piece = getPieceToMove(pieceToMove);
 playFirstPiece(piece);
 moveNumber++;
 nextMove = strtok(((void *)0), ",");
 while (nextMove != ((void *)0) && nextMove != ", ")
 {
  if ((-1) == sscanf(nextMove, "%d. %s %s", &moveNum, pieceToMove, adjacentPiece))
  {
   break;
  }
  piece = getPieceToMove(pieceToMove);
  pieceAdjacent = getAdjacentPiece(adjacentPiece, &adjacencyDirection);
  movePiece(piece, pieceAdjacent, adjacencyDirection);
  moveNumber++;
  nextMove = strtok(((void *)0), ",");
 }
}

variable_types:
moveString
char *

variable_types:
nextMove
char *

variable_types: moveNum int

variable_types:
pieceToMove
char [5]

variable_types:
adjacentPiece
char [5]

variable_types: enum DIRECTIONS_e { NORTHEAST=0, EAST=1, SOUTHEAST=2, SOUTHWEST=3, WEST=4, NORTHWEST=5, MAX_DIR=6 }

user_defined_types: enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 }


printBoard

decompiled_code:
__int64 __fastcall printBoard(int a1, int a2)
{
int v2; // er15
__int64 result; // rax
int v4; // er12
int v5; // ebp
int v6; // er14
char *v7; // r13
int v8; // ebx
char *v9; // r15
int i; // ebx
char v11; // di
int v12; // [rsp+8h] [rbp-40h]
int v13; // [rsp+Ch] [rbp-3Ch]

v2 = a2 + 126;
result = (unsigned int)(126 - a2);
v12 = result;
if ( a2 + 126 >= (int)result )
{
v4 = 2 * a2;
v5 = a1 + 126;
v6 = 126 - a1;
v13 = v2 - 2 * a2;
v7 = &board[256 * (__int64)(126 - a1) + v2];
do
{
v8 = v4;
if ( v4 > 0 )
{
do
{
fwrite(&off_5810957, 1uLL, 3uLL, stderr);
--v8;
}
while ( v8 );
}
--v4;
v9 = v7;
for ( i = v6; v5 >= i; ++i )
{
while ( 1 )
{
v11 = *v9;
if ( *v9 == 23 )
break;
++i;
v9 += 256;
getPieceName(v11, &pieceNamePtr);
fprintf(stderr, " %s ", (const char *)pieceNamePtr);
if ( v5 < i )
goto LABEL_10;
}
v9 += 256;
fwrite(" - ", 1uLL, 5uLL, stderr);
}
LABEL_10:
--v7;
fputc(10, stderr);
result = (unsigned int)(v4 + v13);
}
while ( v12 <= (int)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2)
{
int v2; // er15
__int64 result; // rax
int v4; // er12
int v5; // ebp
int v6; // er14
char *v7; // r13
int v8; // ebx
char *v9; // r15
int i; // ebx
char v11; // di
int v12; // [rsp+8h] [rbp-40h]
int v13; // [rsp+Ch] [rbp-3Ch]

v2 = a2 + 126;
result = (unsigned int)(126 - a2);
v12 = result;
if ( a2 + 126 >= (int)result )
{
v4 = 2 * a2;
v5 = a1 + 126;
v6 = 126 - a1;
v13 = v2 - 2 * a2;
v7 = &board[256 * (__int64)(126 - a1) + v2];
do
{
v8 = v4;
if ( v4 > 0 )
{
do
{
fwrite(&off_5810957, 1uLL, 3uLL, stderr);
--v8;
}
while ( v8 );
}
--v4;
v9 = v7;
for ( i = v6; v5 >= i; ++i )
{
while ( 1 )
{
v11 = *v9;
if ( *v9 == 23 )
break;
++i;
v9 += 256;
func3(v11, &pieceNamePtr);
fprintf(stderr, " %s ", (const char *)pieceNamePtr);
if ( v5 < i )
goto LABEL_10;
}
v9 += 256;
fwrite(" - ", 1uLL, 5uLL, stderr);
}
LABEL_10:
--v7;
fputc(10, stderr);
result = (unsigned int)(v4 + v13);
}
while ( v12 <= (int)result );
}
return result;
}

original_code:
void printBoard(int xSize, int ySize)
{
 int x, y, z;
 char** pptr = &pieceNamePtr;
 int spaceCount = ySize * 2;
 for (y = (ySize + 126); y >= (-1*ySize + 126); y--)
 {
  for (z = spaceCount; z > 0; z--)
   fprintf(stderr, "   ");
  spaceCount--;
  for (x = (-1*xSize + 126); x <= (xSize + 126); x++)
  {
   if (board[x][y] == EMPTY_SPACE)
   {
    fprintf(stderr, "  -  ");
   }
   else
   {
    getPieceName(board[x][y], pptr);
    fprintf(stderr, " %s ", pieceNamePtr);
   }
  }
  fprintf(stderr, "\n");
 }
}

canonical_original_code:
void printBoard(int xSize, int ySize)
{
 int x; int y; int z;
 char **pptr = &pieceNamePtr;
 int spaceCount = ySize * 2;
 for (y = (ySize + 126); y >= (-1*ySize + 126); y--)
 {
  for (z = spaceCount; z > 0; z--)
   fprintf(stderr, "   ");
  spaceCount--;
  for (x = (-1*xSize + 126); x <= (xSize + 126); x++)
  {
   if (board[x][y] == EMPTY_SPACE)
   {
    fprintf(stderr, "  -  ");
   }
   else
   {
    getPieceName(board[x][y], pptr);
    fprintf(stderr, " %s ", pieceNamePtr);
   }
  }
  fprintf(stderr, "\n");
 }
}

variable_types: xSize int

variable_types: ySize int

variable_types: x int

variable_types: y int

variable_types: z int

variable_types:
pptr
char **

variable_types: spaceCount int


getAdjacentPieceAndQualifier

original_code:
_Bool getAdjacentPieceAndQualifier(PIECE_e piece, uint8_t x, uint8_t y, char** pieceName, char** qualifier)
{
 if (board[(x)][(y+1)] < MAX_PIECES && board[(x)][(y+1)] != piece)
 {
  getPieceName(board[(x)][(y+1)], pieceName);
  *qualifier = "/";
  return 1;
 }
 else if (board[(x+1)][(y)] < MAX_PIECES && board[(x+1)][(y)] != piece)
 {
  getPieceName(board[(x+1)][(y)], pieceName);
  *qualifier = "-";
  return 1;
 }
 else if (board[(x+1)][(y-1)] < MAX_PIECES && board[(x+1)][(y-1)] != piece)
 {
  getPieceName(board[(x+1)][(y-1)], pieceName);
  *qualifier = "\\";
  return 1;
 }
 else if (board[(x)][(y-1)] < MAX_PIECES && board[(x)][(y-1)] != piece)
 {
  getPieceName(board[(x)][(y-1)], pieceName);
  *qualifier = "/";
  return 0;
 }
 else if (board[(x-1)][(y)] < MAX_PIECES && board[(x-1)][(y)] != piece)
 {
  getPieceName(board[(x-1)][(y)], pieceName);
  *qualifier = "-";
  return 0;
 }
 else if (board[(x-1)][(y+1)] < MAX_PIECES && board[(x-1)][(y+1)] != piece)
 {
  getPieceName(board[(x-1)][(y+1)], pieceName);
  *qualifier = "\\";
  return 0;
 }
 return 0;
}

canonical_original_code:
_Bool getAdjacentPieceAndQualifier(enum PIECE_e piece, unsigned char x, unsigned char y, char **pieceName, char **qualifier)
{
 if (board[(x)][(y+1)] < MAX_PIECES && board[(x)][(y+1)] != piece)
 {
  getPieceName(board[(x)][(y+1)], pieceName);
  *qualifier = "/";
  return 1;
 }
 else if (board[(x+1)][(y)] < MAX_PIECES && board[(x+1)][(y)] != piece)
 {
  getPieceName(board[(x+1)][(y)], pieceName);
  *qualifier = "-";
  return 1;
 }
 else if (board[(x+1)][(y-1)] < MAX_PIECES && board[(x+1)][(y-1)] != piece)
 {
  getPieceName(board[(x+1)][(y-1)], pieceName);
  *qualifier = "\\";
  return 1;
 }
 else if (board[(x)][(y-1)] < MAX_PIECES && board[(x)][(y-1)] != piece)
 {
  getPieceName(board[(x)][(y-1)], pieceName);
  *qualifier = "/";
  return 0;
 }
 else if (board[(x-1)][(y)] < MAX_PIECES && board[(x-1)][(y)] != piece)
 {
  getPieceName(board[(x-1)][(y)], pieceName);
  *qualifier = "-";
  return 0;
 }
 else if (board[(x-1)][(y+1)] < MAX_PIECES && board[(x-1)][(y+1)] != piece)
 {
  getPieceName(board[(x-1)][(y+1)], pieceName);
  *qualifier = "\\";
  return 0;
 }
 return 0;
}

variable_types:
x
unsigned char

variable_types:
y
unsigned char

variable_types:
pieceName
char **

variable_types:
qualifier
char **


printOutBestMove

decompiled_code:
int printOutBestMove()
{
unsigned int v0; // ebp
__int16 v1; // bx
char v2; // al
const char *v3; // rcx
const char *v4; // rdx

v0 = BYTE2(currMove);
v1 = currMove;
getPieceName(BYTE2(currMove), &pieceNamePtr);
pickUpPiece(v0);
getAdjacentPieceAndQualifier(v0, v1, HIBYTE(v1), &pieceAdjNamePtr);
if ( v2 )
{
v3 = (const char *)pieceAdjNamePtr;
v4 = (const char *)pieceQualifierPtr;
}
else
{
v3 = (const char *)pieceQualifierPtr;
v4 = (const char *)pieceAdjNamePtr;
}
return printf("%s %s%s", (const char *)pieceNamePtr, v4, v3);
}

canonical_decompiled_code:
int func6()
{
unsigned int v0; // ebp
__int16 v1; // bx
char v2; // al
const char *v3; // rcx
const char *v4; // rdx

v0 = BYTE2(currMove);
v1 = currMove;
func3(BYTE2(currMove), &pieceNamePtr);
pickUpPiece(v0);
func5(v0, v1, HIBYTE(v1), &pieceAdjNamePtr);
if ( v2 )
{
v3 = (const char *)pieceAdjNamePtr;
v4 = (const char *)pieceQualifierPtr;
}
else
{
v3 = (const char *)pieceQualifierPtr;
v4 = (const char *)pieceAdjNamePtr;
}
return printf("%s %s%s", (const char *)pieceNamePtr, v4, v3);
}

original_code:
void printOutBestMove(void)
{
 PIECE_e piece;
 uint8_t x, y;
 char** pptr;
 char** pptr2;
 char** pptr3;
 pptr = &pieceNamePtr;
 pptr2 = &pieceQualifierPtr;
 pptr3 = &pieceAdjNamePtr;
 piece = ((currMove.move & (0x00FF0000)) >> (16));
 x = ((currMove.move & (0x000000FF)) >> (0));
 y = ((currMove.move & (0x0000FF00)) >> (8));
 getPieceName(piece, pptr);
 pickUpPiece(piece);
 if(getAdjacentPieceAndQualifier(piece, x, y, pptr3, pptr2))
 {
  printf("%s %s%s", pieceNamePtr, pieceQualifierPtr, pieceAdjNamePtr);
 }
 else
 {
  printf( "%s %s%s", pieceNamePtr, pieceAdjNamePtr, pieceQualifierPtr);
 }
}

canonical_original_code:
void printOutBestMove()
{
 enum PIECE_e piece;
 unsigned char x; unsigned char y;
 char **pptr;
 char **pptr2;
 char **pptr3;
 pptr = &pieceNamePtr;
 pptr2 = &pieceQualifierPtr;
 pptr3 = &pieceAdjNamePtr;
 piece = ((currMove.move & (0x00FF0000)) >> (16));
 x = ((currMove.move & (0x000000FF)) >> (0));
 y = ((currMove.move & (0x0000FF00)) >> (8));
 getPieceName(piece, pptr);
 pickUpPiece(piece);
 if(getAdjacentPieceAndQualifier(piece, x, y, pptr3, pptr2))
 {
  printf("%s %s%s", pieceNamePtr, pieceQualifierPtr, pieceAdjNamePtr);
 }
 else
 {
  printf( "%s %s%s", pieceNamePtr, pieceAdjNamePtr, pieceQualifierPtr);
 }
}

variable_types: enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 }


initState

decompiled_code:
int initState()
{
__int64 v0; // rbx
void *v1; // rax

initPieces();
initBoard();
initSearch();
initPinnedMap();
pieceNamePtr = (__int64)malloc(5uLL);
v0 = pieceNamePtr;
pieceQualifierPtr = (__int64)malloc(3uLL);
v1 = malloc(5uLL);
pieceAdjNamePtr = (__int64)v1;
if ( !v0 )
LODWORD(v1) = puts("ERROR: Could not allocate char ptr");
return (int)v1;
}

canonical_decompiled_code:
int func7()
{
__int64 v0; // rbx
void *v1; // rax

initPieces();
initBoard();
initSearch();
initPinnedMap();
pieceNamePtr = (__int64)malloc(5uLL);
v0 = pieceNamePtr;
pieceQualifierPtr = (__int64)malloc(3uLL);
v1 = malloc(5uLL);
pieceAdjNamePtr = (__int64)v1;
if ( !v0 )
LODWORD(v1) = puts("ERROR: Could not allocate char ptr");
return (int)v1;
}

original_code:
void initState(void)
{
 initPieces();
 initBoard();
 initSearch();
 initPinnedMap();
 pieceNamePtr = (char*)malloc(sizeof(char)*5);
 pieceQualifierPtr = (char*)malloc(sizeof(char)*3);
 pieceAdjNamePtr = (char*)malloc(sizeof(char)*5);
 if (pieceNamePtr == ((void *)0))
 {
  printf("ERROR: Could not allocate char ptr\n");
 }
}

canonical_original_code:
void initState()
{
 initPieces();
 initBoard();
 initSearch();
 initPinnedMap();
 pieceNamePtr = (char *)malloc(sizeof(char)*5);
 pieceQualifierPtr = (char *)malloc(sizeof(char)*3);
 pieceAdjNamePtr = (char *)malloc(sizeof(char)*5);
 if (pieceNamePtr == ((void *)0))
 {
  printf("ERROR: Could not allocate char ptr\n");
 }
}


explorePieces

decompiled_code:
__int64 __fastcall explorePieces(unsigned int a1)
{
__int64 v1; // rax
unsigned int v2; // er14
unsigned int v3; // er12
int v4; // ebp
unsigned __int8 v5; // r13
unsigned __int8 v6; // al
int v7; // ebx
__int64 v8; // rdx
__int64 v9; // r15
__int64 v10; // rcx
__int64 v11; // r13
__int64 v12; // rbx
int v14; // eax
int v15; // eax
int v16; // eax
int v17; // eax
unsigned __int8 v18; // [rsp+0h] [rbp-48h]
__int64 v19; // [rsp+0h] [rbp-48h]
__int64 v20; // [rsp+0h] [rbp-48h]
__int64 v21; // [rsp+0h] [rbp-48h]
__int64 v22; // [rsp+8h] [rbp-40h]

v1 = a1;
v2 = a1;
v3 = 0;
do
{
v4 = 1;
pieceArray[v1] = 0;
v18 = getXpos(v2);
v5 = getYpos(v2);
v6 = getZpos(v2);
if ( v6 > 1u )
{
while ( (v6 & 1) == 0 )
{
v6 >>= 1;
++v4;
}
}
v7 = v18;
v8 = v18;
v9 = v5 + 1;
if ( (unsigned __int8)board[256 * (unsigned __int64)v18 + v9] <= 0x15u
&& pieceArray[(unsigned __int8)board[256 * (unsigned __int64)v18 + v9]] )
{
v14 = explorePieces();
v8 = v18;
v4 += v14;
}
v10 = v5;
if ( (unsigned __int8)board[256 * (__int64)(v18 + 1) + v5] <= 0x15u
&& pieceArray[(unsigned __int8)board[256 * (__int64)(v18 + 1) + v5]] )
{
v20 = v8;
v16 = explorePieces();
v10 = v5;
v8 = v20;
v4 += v16;
}
v11 = v5 - 1;
if ( (unsigned __int8)board[256 * (__int64)(v7 + 1) + v11] <= 0x15u
&& pieceArray[(unsigned __int8)board[256 * (__int64)(v7 + 1) + v11]] )
{
v22 = v10;
v21 = v8;
v17 = explorePieces();
v10 = v22;
v8 = v21;
v4 += v17;
}
if ( (unsigned __int8)board[256 * v8 + v11] <= 0x15u && pieceArray[(unsigned __int8)board[256 * v8 + v11]] )
{
v19 = v10;
v15 = explorePieces();
v10 = v19;
v4 += v15;
}
v12 = v7 - 1;
if ( (unsigned __int8)board[256 * v12 + v10] <= 0x15u && pieceArray[(unsigned __int8)board[256 * v12 + v10]] )
v4 += explorePieces();
v3 += v4;
v2 = (unsigned __int8)board[256 * v12 + v9];
if ( v2 > 0x15 )
break;
v1 = (unsigned __int8)board[256 * v12 + v9];
}
while ( pieceArray[v1] );
return v3;
}

original_code:
static uint32_t explorePieces(PIECE_e piece)
{
 uint32_t piecesExplored = 0;
 uint8_t xpos, ypos, zpos;
 PIECE_e neighbour;
 pieceArray[piece] = 0;
 piecesExplored++;
 xpos = getXpos(piece);
 ypos = getYpos(piece);
 zpos = getZpos(piece);
 if (zpos > (1))
 {
  while (!(zpos & 1))
  {
   zpos = zpos >> 1;
   piecesExplored++;
  }
 }
 neighbour = board[xpos][ypos+1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos+1][ypos];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos+1][ypos-1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos][ypos-1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos-1][ypos];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos-1][ypos+1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 return piecesExplored;
}

canonical_original_code:
static unsigned int explorePieces(enum PIECE_e piece)
{
 unsigned int piecesExplored = 0;
 unsigned char xpos; unsigned char ypos; unsigned char zpos;
 enum PIECE_e neighbour;
 pieceArray[piece] = 0;
 piecesExplored++;
 xpos = getXpos(piece);
 ypos = getYpos(piece);
 zpos = getZpos(piece);
 if (zpos > (1))
 {
  while (!(zpos & 1))
  {
   zpos = zpos >> 1;
   piecesExplored++;
  }
 }
 neighbour = board[xpos][ypos+1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos+1][ypos];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos+1][ypos-1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos][ypos-1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos-1][ypos];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 neighbour = board[xpos-1][ypos+1];
 if (neighbour < MAX_PIECES && pieceArray[neighbour])
  piecesExplored += explorePieces(neighbour);
 return piecesExplored;
}

variable_types:
piecesExplored
unsigned int

variable_types:
xpos
unsigned char

variable_types:
ypos
unsigned char

variable_types:
zpos
unsigned char

return_type: unsigned int


getOccupiedPositionsAroundCoords

decompiled_code:
char __fastcall getOccupiedPositionsAroundCoords(unsigned __int8 a1, unsigned __int8 a2)
{
__int64 v2; // r9
int v3; // edx
int v4; // er8
char *v5; // rdi
char result; // al
char *v7; // rcx
__int64 v8; // rsi
char *v9; // rdx

v2 = a2;
v3 = a1;
v4 = a2 + 1;
v5 = &board[256 * (unsigned __int64)a1];
result = v5[v4] != 23;
v7 = &board[256 * (__int64)(v3 + 1)];
if ( v7[a2] != 23 )
result |= 2u;
v8 = a2 - 1;
if ( v7[v8] != 23 )
result |= 4u;
if ( v5[v8] != 23 )
result |= 8u;
v9 = &board[256 * (__int64)(v3 - 1)];
if ( v9[v2] != 23 )
result |= 0x10u;
if ( v9[v4] != 23 )
result |= 0x20u;
return result;
}

canonical_decompiled_code:
char __fastcall func1(unsigned __int8 a1, unsigned __int8 a2)
{
__int64 v2; // r9
int v3; // edx
int v4; // er8
char *v5; // rdi
char result; // al
char *v7; // rcx
__int64 v8; // rsi
char *v9; // rdx

v2 = a2;
v3 = a1;
v4 = a2 + 1;
v5 = &board[256 * (unsigned __int64)a1];
result = v5[v4] != 23;
v7 = &board[256 * (__int64)(v3 + 1)];
if ( v7[a2] != 23 )
result |= 2u;
v8 = a2 - 1;
if ( v7[v8] != 23 )
result |= 4u;
if ( v5[v8] != 23 )
result |= 8u;
v9 = &board[256 * (__int64)(v3 - 1)];
if ( v9[v2] != 23 )
result |= 0x10u;
if ( v9[v4] != 23 )
result |= 0x20u;
return result;
}

original_code:
uint8_t getOccupiedPositionsAroundCoords(uint8_t xpos, uint8_t ypos)
{
 uint8_t retVal = 0;
 if (board[xpos][ypos+1] != EMPTY_SPACE)
  retVal |= (1 << NE_SHIFT);
 if (board[xpos+1][ypos] != EMPTY_SPACE)
  retVal |= (1 << E_SHIFT);
 if (board[xpos+1][ypos-1] != EMPTY_SPACE)
  retVal |= (1 << SE_SHIFT);
 if (board[xpos][ypos-1] != EMPTY_SPACE)
  retVal |= (1 << SW_SHIFT);
 if (board[xpos-1][ypos] != EMPTY_SPACE)
  retVal |= (1 << W_SHIFT);
 if (board[xpos-1][ypos+1] != EMPTY_SPACE)
  retVal |= (1 << NW_SHIFT);
 return retVal;
}

canonical_original_code:
unsigned char getOccupiedPositionsAroundCoords(unsigned char xpos, unsigned char ypos)
{
 unsigned char retVal = 0;
 if (board[xpos][ypos+1] != EMPTY_SPACE)
  retVal |= (1 << NE_SHIFT);
 if (board[xpos+1][ypos] != EMPTY_SPACE)
  retVal |= (1 << E_SHIFT);
 if (board[xpos+1][ypos-1] != EMPTY_SPACE)
  retVal |= (1 << SE_SHIFT);
 if (board[xpos][ypos-1] != EMPTY_SPACE)
  retVal |= (1 << SW_SHIFT);
 if (board[xpos-1][ypos] != EMPTY_SPACE)
  retVal |= (1 << W_SHIFT);
 if (board[xpos-1][ypos+1] != EMPTY_SPACE)
  retVal |= (1 << NW_SHIFT);
 return retVal;
}

variable_types:
retVal
unsigned char

return_type: unsigned char


getNumberOfOccupiedSquaresAroundCoords

decompiled_code:
__int64 __fastcall getNumberOfOccupiedSquaresAroundCoords(unsigned __int8 a1, unsigned __int8 a2)
{
unsigned __int8 v2; // al
int v3; // ecx
unsigned int v4; // er8
int v5; // edx
int v6; // eax

v2 = getOccupiedPositionsAroundCoords(a1, a2);
v3 = 0;
v4 = 0;
v5 = v2;
do
{
v6 = v5 >> v3++;
v4 += v6 & 1;
}
while ( v3 != 6 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1, unsigned __int8 a2)
{
unsigned __int8 v2; // al
int v3; // ecx
unsigned int v4; // er8
int v5; // edx
int v6; // eax

v2 = func1(a1, a2);
v3 = 0;
v4 = 0;
v5 = v2;
do
{
v6 = v5 >> v3++;
v4 += v6 & 1;
}
while ( v3 != 6 );
return v4;
}

original_code:
uint8_t getNumberOfOccupiedSquaresAroundCoords(uint8_t xpos, uint8_t ypos)
{
 uint8_t sum = 0;
 uint8_t retVal = getOccupiedPositionsAroundCoords(xpos, ypos);
 for (int i = 0; i < 6; i++)
 {
  if ((retVal >> i) & 1)
   sum +=1;
 }
 return sum;
}

canonical_original_code:
unsigned char getNumberOfOccupiedSquaresAroundCoords(unsigned char xpos, unsigned char ypos)
{
 unsigned char sum = 0;
 unsigned char retVal = getOccupiedPositionsAroundCoords(xpos, ypos);
 for (int i = 0; i < 6; i++)
 {
  if ((retVal >> i) & 1)
   sum +=1;
 }
 return sum;
}

variable_types:
sum
unsigned char

variable_types: i int


initBoard

decompiled_code:
void *initBoard()
{
return memset(board, 23, (size_t)&unk_10000);
}

canonical_decompiled_code:
void *func3()
{
return memset(board, 23, (size_t)&unk_10000);
}

original_code:
void initBoard(void)
{
 int i, j;
 for (i = 0; i < 256; i++)
 {
  for (j = 0; j < 256; j++)
  {
   board[i][j] = EMPTY_SPACE;
  }
 }
}

canonical_original_code:
void initBoard()
{
 int i; int j;
 for (i = 0; i < 256; i++)
 {
  for (j = 0; j < 256; j++)
  {
   board[i][j] = EMPTY_SPACE;
  }
 }
}

variable_types: j int


isHiveBroken

decompiled_code:
bool __fastcall isHiveBroken(unsigned int a1)
{
int v1; // ebp
__int64 i; // rbx
__int64 v3; // rbx
int v4; // eax
__int64 v5; // rcx
__int64 v6; // rdx
unsigned int v7; // edi
__int64 v8; // r8
__int64 v9; // rdi
__int64 v10; // rax
__int64 v11; // rdx
__int64 v12; // rax
__int64 v13; // rdx
__int64 v14; // rdx
int v15; // eax

v1 = 0;
for ( i = 0LL; i != 22; ++i )
{
pieceArray[i] = 0;
if ( (unsigned __int8)hasPieceBeenPlayed((unsigned int)i) )
{
pieceArray[i] = 1;
++v1;
}
}
v3 = (unsigned __int8)getXpos(a1);
v4 = (unsigned __int8)getYpos(a1);
v5 = (unsigned __int8)v4 + 1;
v6 = (unsigned __int8)board[256 * v3 + v5];
if ( (unsigned __int8)v6 <= 0x15u )
{
v7 = (unsigned __int8)v6;
if ( pieceArray[v6] )
{
LABEL_16:
v15 = explorePieces(v7);
return v1 != v15;
}
}
v8 = v4;
v9 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 + 1) + v4];
if ( (unsigned __int8)v9 > 0x15u || !pieceArray[v9] )
{
v10 = v4 - 1;
v11 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 + 1) + v10];
if ( (unsigned __int8)v11 > 0x15u || (v7 = (unsigned __int8)v11, !pieceArray[v11]) )
{
v12 = (unsigned __int8)board[256 * v3 + v10];
if ( (unsigned __int8)v12 > 0x15u || (v7 = (unsigned __int8)v12, !pieceArray[v12]) )
{
v13 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 - 1) + v8];
if ( (unsigned __int8)v13 > 0x15u || (v7 = (unsigned __int8)v13, !pieceArray[v13]) )
{
v14 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 - 1) + v5];
v15 = 0;
if ( (unsigned __int8)v14 > 0x15u )
return v1 != v15;
v15 = pieceArray[v14];
v7 = (unsigned __int8)v14;
if ( !v15 )
return v1 != v15;
}
}
}
goto LABEL_16;
}
return v1 != (unsigned int)explorePieces((unsigned __int8)v9);
}

canonical_decompiled_code:
bool __fastcall func4(unsigned int a1)
{
int v1; // ebp
__int64 i; // rbx
__int64 v3; // rbx
int v4; // eax
__int64 v5; // rcx
__int64 v6; // rdx
unsigned int v7; // edi
__int64 v8; // r8
__int64 v9; // rdi
__int64 v10; // rax
__int64 v11; // rdx
__int64 v12; // rax
__int64 v13; // rdx
__int64 v14; // rdx
int v15; // eax

v1 = 0;
for ( i = 0LL; i != 22; ++i )
{
pieceArray[i] = 0;
if ( (unsigned __int8)hasPieceBeenPlayed((unsigned int)i) )
{
pieceArray[i] = 1;
++v1;
}
}
v3 = (unsigned __int8)getXpos(a1);
v4 = (unsigned __int8)getYpos(a1);
v5 = (unsigned __int8)v4 + 1;
v6 = (unsigned __int8)board[256 * v3 + v5];
if ( (unsigned __int8)v6 <= 0x15u )
{
v7 = (unsigned __int8)v6;
if ( pieceArray[v6] )
{
LABEL_16:
v15 = func0(v7);
return v1 != v15;
}
}
v8 = v4;
v9 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 + 1) + v4];
if ( (unsigned __int8)v9 > 0x15u || !pieceArray[v9] )
{
v10 = v4 - 1;
v11 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 + 1) + v10];
if ( (unsigned __int8)v11 > 0x15u || (v7 = (unsigned __int8)v11, !pieceArray[v11]) )
{
v12 = (unsigned __int8)board[256 * v3 + v10];
if ( (unsigned __int8)v12 > 0x15u || (v7 = (unsigned __int8)v12, !pieceArray[v12]) )
{
v13 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 - 1) + v8];
if ( (unsigned __int8)v13 > 0x15u || (v7 = (unsigned __int8)v13, !pieceArray[v13]) )
{
v14 = (unsigned __int8)board[256 * (__int64)((unsigned __int8)v3 - 1) + v5];
v15 = 0;
if ( (unsigned __int8)v14 > 0x15u )
return v1 != v15;
v15 = pieceArray[v14];
v7 = (unsigned __int8)v14;
if ( !v15 )
return v1 != v15;
}
}
}
goto LABEL_16;
}
return v1 != (unsigned int)func0((unsigned __int8)v9);
}

original_code:
_Bool isHiveBroken(PIECE_e pieceToCheck)
{
 PIECE_e piece, startingPiece;
 uint8_t xpos, ypos;
 uint32_t numberOfPiecesPlayed = 0;
 uint32_t piecesExplored = 0;
 for (piece = 0; piece < MAX_PIECES; piece++)
 {
  pieceArray[piece] = 0;
  if (hasPieceBeenPlayed(piece))
  {
   numberOfPiecesPlayed++;
   pieceArray[piece] = 1;
   startingPiece = piece;
  }
 }
 xpos = getXpos(pieceToCheck);
 ypos = getYpos(pieceToCheck);
 if (board[xpos][ypos+1] < MAX_PIECES && pieceArray[board[xpos][ypos+1]])
  piecesExplored += explorePieces(board[xpos][ypos+1]);
 else if (board[xpos+1][ypos] < MAX_PIECES && pieceArray[board[xpos+1][ypos]])
  piecesExplored += explorePieces(board[xpos+1][ypos]);
 else if (board[xpos+1][ypos-1] < MAX_PIECES && pieceArray[board[xpos+1][ypos-1]])
  piecesExplored += explorePieces(board[xpos+1][ypos-1]);
 else if (board[xpos][ypos-1] < MAX_PIECES && pieceArray[board[xpos][ypos-1]])
  piecesExplored += explorePieces(board[xpos][ypos-1]);
 else if (board[xpos-1][ypos] < MAX_PIECES && pieceArray[board[xpos-1][ypos]])
  piecesExplored += explorePieces(board[xpos-1][ypos]);
 else if (board[xpos-1][ypos+1] < MAX_PIECES && pieceArray[board[xpos-1][ypos+1]])
  piecesExplored += explorePieces(board[xpos-1][ypos+1]);
 if (numberOfPiecesPlayed != piecesExplored)
  return 1;
 return 0;
}

canonical_original_code:
_Bool isHiveBroken(enum PIECE_e pieceToCheck)
{
 enum PIECE_e piece; enum PIECE_e startingPiece;
 unsigned char xpos; unsigned char ypos;
 unsigned int numberOfPiecesPlayed = 0;
 unsigned int piecesExplored = 0;
 for (piece = 0; piece < MAX_PIECES; piece++)
 {
  pieceArray[piece] = 0;
  if (hasPieceBeenPlayed(piece))
  {
   numberOfPiecesPlayed++;
   pieceArray[piece] = 1;
   startingPiece = piece;
  }
 }
 xpos = getXpos(pieceToCheck);
 ypos = getYpos(pieceToCheck);
 if (board[xpos][ypos+1] < MAX_PIECES && pieceArray[board[xpos][ypos+1]])
  piecesExplored += explorePieces(board[xpos][ypos+1]);
 else if (board[xpos+1][ypos] < MAX_PIECES && pieceArray[board[xpos+1][ypos]])
  piecesExplored += explorePieces(board[xpos+1][ypos]);
 else if (board[xpos+1][ypos-1] < MAX_PIECES && pieceArray[board[xpos+1][ypos-1]])
  piecesExplored += explorePieces(board[xpos+1][ypos-1]);
 else if (board[xpos][ypos-1] < MAX_PIECES && pieceArray[board[xpos][ypos-1]])
  piecesExplored += explorePieces(board[xpos][ypos-1]);
 else if (board[xpos-1][ypos] < MAX_PIECES && pieceArray[board[xpos-1][ypos]])
  piecesExplored += explorePieces(board[xpos-1][ypos]);
 else if (board[xpos-1][ypos+1] < MAX_PIECES && pieceArray[board[xpos-1][ypos+1]])
  piecesExplored += explorePieces(board[xpos-1][ypos+1]);
 if (numberOfPiecesPlayed != piecesExplored)
  return 1;
 return 0;
}

variable_types:
numberOfPiecesPlayed
unsigned int


isGate

decompiled_code:
bool __fastcall isGate(unsigned __int8 a1, unsigned __int8 a2)
{
return (unsigned __int8)getNumberOfOccupiedSquaresAroundCoords(a1, a2) == 5;
}

canonical_decompiled_code:
bool __fastcall func5(unsigned __int8 a1, unsigned __int8 a2)
{
return (unsigned __int8)func2(a1, a2) == 5;
}

original_code:
inline _Bool isGate(uint8_t xpos, uint8_t ypos)
{
 return (5 == getNumberOfOccupiedSquaresAroundCoords(xpos, ypos));
}

canonical_original_code:
inline _Bool isGate(unsigned char xpos, unsigned char ypos)
{
 return (5 == getNumberOfOccupiedSquaresAroundCoords(xpos, ypos));
}


isGateOrUnnattached

decompiled_code:
__int64 __fastcall isGateOrUnnattached(unsigned __int8 a1, unsigned __int8 a2)
{
unsigned int v2; // eax
int v3; // edx

v2 = getNumberOfOccupiedSquaresAroundCoords(a1, a2);
LOBYTE(v3) = (_BYTE)v2 == 5;
LOBYTE(v2) = (_BYTE)v2 == 0;
return v3 | v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1, unsigned __int8 a2)
{
unsigned int v2; // eax
int v3; // edx

v2 = func2(a1, a2);
LOBYTE(v3) = (_BYTE)v2 == 5;
LOBYTE(v2) = (_BYTE)v2 == 0;
return v3 | v2;
}

original_code:
inline _Bool isGateOrUnnattached(uint8_t xpos, uint8_t ypos)
{
 uint8_t temp = getNumberOfOccupiedSquaresAroundCoords(xpos, ypos);
 if (temp == 5 || temp == 0)
  return 1;
 return 0;
}

canonical_original_code:
inline _Bool isGateOrUnnattached(unsigned char xpos, unsigned char ypos)
{
 unsigned char temp = getNumberOfOccupiedSquaresAroundCoords(xpos, ypos);
 if (temp == 5 || temp == 0)
  return 1;
 return 0;
}

variable_types:
temp
unsigned char


isSquareOccupied

decompiled_code:
bool __fastcall isSquareOccupied(unsigned __int8 a1, unsigned __int8 a2)
{
return board[256 * (unsigned __int64)a1 + a2] != 23;
}

canonical_decompiled_code:
bool __fastcall func7(unsigned __int8 a1, unsigned __int8 a2)
{
return board[256 * (unsigned __int64)a1 + a2] != 23;
}

original_code:
inline _Bool isSquareOccupied(uint8_t xpos, uint8_t ypos)
{
 if (board[xpos][ypos] != EMPTY_SPACE)
  return 1;
 return 0;
}

canonical_original_code:
inline _Bool isSquareOccupied(unsigned char xpos, unsigned char ypos)
{
 if (board[xpos][ypos] != EMPTY_SPACE)
  return 1;
 return 0;
}


isSlidable

decompiled_code:
__int64 __fastcall isSlidable(__int64 a1, __int64 a2, int a3)
{
unsigned int v3; // er8
__int64 v5; // rax

a1 = (unsigned __int8)a1;
a2 = (unsigned __int8)a2;
switch ( a3 )
{
case 0:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + (int)a2] <= 0x15u )
{
a2 = (int)a2 + 1;
goto LABEL_4;
}
return v3;
case 1:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + 1 + (int)a2] > 0x15u )
return v3;
a2 = (int)a2 - 1;
break;
case 2:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 - 1 + (int)a2] > 0x15u )
return v3;
break;
case 3:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) - 1 + (int)a2] > 0x15u )
return v3;
goto LABEL_4;
default:
v5 = (int)a2 + 1;
if ( a3 == 4 )
{
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + v5] <= 0x15u )
LOBYTE(v3) = (unsigned __int8)board[256 * a1 - 1 + (int)a2] > 0x15u;
return v3;
}
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + v5] <= 0x15u )
LABEL_4:
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + a2] > 0x15u;
return v3;
}
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + a2] > 0x15u;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, int a3)
{
unsigned int v3; // er8
__int64 v5; // rax

a1 = (unsigned __int8)a1;
a2 = (unsigned __int8)a2;
switch ( a3 )
{
case 0:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + (int)a2] <= 0x15u )
{
a2 = (int)a2 + 1;
goto LABEL_4;
}
return v3;
case 1:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + 1 + (int)a2] > 0x15u )
return v3;
a2 = (int)a2 - 1;
break;
case 2:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 - 1 + (int)a2] > 0x15u )
return v3;
break;
case 3:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) - 1 + (int)a2] > 0x15u )
return v3;
goto LABEL_4;
default:
v5 = (int)a2 + 1;
if ( a3 == 4 )
{
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + v5] <= 0x15u )
LOBYTE(v3) = (unsigned __int8)board[256 * a1 - 1 + (int)a2] > 0x15u;
return v3;
}
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + v5] <= 0x15u )
LABEL_4:
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + a2] > 0x15u;
return v3;
}
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + a2] > 0x15u;
return v3;
}

original_code:
inline _Bool isSlidable(uint8_t x, uint8_t y, DIRECTIONS_e dir)
{
 if (NORTHEAST == dir)
 {
  if ((board[(x+1)][(y)] < MAX_PIECES) && (board[(x-1)][(y+1)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (EAST == dir)
 {
  if ((board[(x)][(y+1)] < MAX_PIECES) && (board[(x+1)][(y-1)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (SOUTHEAST == dir)
 {
  if ((board[(x)][(y-1)] < MAX_PIECES) && (board[(x+1)][(y)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (SOUTHWEST == dir)
 {
  if ((board[(x+1)][(y-1)] < MAX_PIECES) && (board[(x-1)][(y)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (WEST == dir)
 {
  if ((board[(x-1)][(y+1)] < MAX_PIECES) && (board[(x)][(y-1)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else
 {
  if ((board[(x)][(y+1)] < MAX_PIECES) && (board[(x-1)][(y)] < MAX_PIECES))
   return 0;
  return 1;
 }
}

canonical_original_code:
inline _Bool isSlidable(unsigned char x, unsigned char y, enum DIRECTIONS_e dir)
{
 if (NORTHEAST == dir)
 {
  if ((board[(x+1)][(y)] < MAX_PIECES) && (board[(x-1)][(y+1)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (EAST == dir)
 {
  if ((board[(x)][(y+1)] < MAX_PIECES) && (board[(x+1)][(y-1)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (SOUTHEAST == dir)
 {
  if ((board[(x)][(y-1)] < MAX_PIECES) && (board[(x+1)][(y)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (SOUTHWEST == dir)
 {
  if ((board[(x+1)][(y-1)] < MAX_PIECES) && (board[(x-1)][(y)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else if (WEST == dir)
 {
  if ((board[(x-1)][(y+1)] < MAX_PIECES) && (board[(x)][(y-1)] < MAX_PIECES))
   return 0;
  return 1;
 }
 else
 {
  if ((board[(x)][(y+1)] < MAX_PIECES) && (board[(x-1)][(y)] < MAX_PIECES))
   return 0;
  return 1;
 }
}

variable_types: enum DIRECTIONS_e { NORTHEAST=0, EAST=1, SOUTHEAST=2, SOUTHWEST=3, WEST=4, NORTHWEST=5, MAX_DIR=6 }


isTouchingCommonPiece

decompiled_code:
__int64 __fastcall isTouchingCommonPiece(__int64 a1, __int64 a2, int a3)
{
unsigned int v3; // er8
__int64 v5; // rax

a1 = (unsigned __int8)a1;
a2 = (unsigned __int8)a2;
switch ( a3 )
{
case 0:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + (int)a2] > 0x15u )
{
a2 = (int)a2 + 1;
goto LABEL_4;
}
return v3;
case 1:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + 1 + (int)a2] <= 0x15u )
return v3;
a2 = (int)a2 - 1;
break;
case 2:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 - 1 + (int)a2] <= 0x15u )
return v3;
break;
case 3:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) - 1 + (int)a2] <= 0x15u )
return v3;
goto LABEL_4;
default:
v5 = (int)a2 + 1;
if ( a3 == 4 )
{
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + v5] > 0x15u )
LOBYTE(v3) = (unsigned __int8)board[256 * a1 - 1 + (int)a2] <= 0x15u;
return v3;
}
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + v5] > 0x15u )
LABEL_4:
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + a2] <= 0x15u;
return v3;
}
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + a2] <= 0x15u;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, int a3)
{
unsigned int v3; // er8
__int64 v5; // rax

a1 = (unsigned __int8)a1;
a2 = (unsigned __int8)a2;
switch ( a3 )
{
case 0:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + (int)a2] > 0x15u )
{
a2 = (int)a2 + 1;
goto LABEL_4;
}
return v3;
case 1:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + 1 + (int)a2] <= 0x15u )
return v3;
a2 = (int)a2 - 1;
break;
case 2:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 - 1 + (int)a2] <= 0x15u )
return v3;
break;
case 3:
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 + 1) - 1 + (int)a2] <= 0x15u )
return v3;
goto LABEL_4;
default:
v5 = (int)a2 + 1;
if ( a3 == 4 )
{
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + v5] > 0x15u )
LOBYTE(v3) = (unsigned __int8)board[256 * a1 - 1 + (int)a2] <= 0x15u;
return v3;
}
v3 = 1;
if ( (unsigned __int8)board[256 * (__int64)(int)a1 + v5] > 0x15u )
LABEL_4:
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 - 1) + a2] <= 0x15u;
return v3;
}
LOBYTE(v3) = (unsigned __int8)board[256 * (__int64)((int)a1 + 1) + a2] <= 0x15u;
return v3;
}

original_code:
inline _Bool isTouchingCommonPiece(uint8_t x, uint8_t y, DIRECTIONS_e dir)
{
 if (NORTHEAST == dir)
 {
  if (board[(x+1)][(y)] < MAX_PIECES)
   return 1;
  else if (board[(x-1)][(y+1)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (EAST == dir)
 {
  if (board[(x)][(y+1)] < MAX_PIECES)
   return 1;
  else if (board[(x+1)][(y-1)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (SOUTHEAST == dir)
 {
  if (board[(x)][(y-1)] < MAX_PIECES)
   return 1;
  else if (board[(x+1)][(y)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (SOUTHWEST == dir)
 {
  if (board[(x+1)][(y-1)] < MAX_PIECES)
   return 1;
  else if (board[(x-1)][(y)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (WEST == dir)
 {
  if (board[(x-1)][(y+1)] < MAX_PIECES)
   return 1;
  else if (board[(x)][(y-1)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else
 {
  if (board[(x)][(y+1)] < MAX_PIECES)
   return 1;
  else if (board[(x-1)][(y)] < MAX_PIECES)
   return 1;
  return 0;
 }
}

canonical_original_code:
inline _Bool isTouchingCommonPiece(unsigned char x, unsigned char y, enum DIRECTIONS_e dir)
{
 if (NORTHEAST == dir)
 {
  if (board[(x+1)][(y)] < MAX_PIECES)
   return 1;
  else if (board[(x-1)][(y+1)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (EAST == dir)
 {
  if (board[(x)][(y+1)] < MAX_PIECES)
   return 1;
  else if (board[(x+1)][(y-1)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (SOUTHEAST == dir)
 {
  if (board[(x)][(y-1)] < MAX_PIECES)
   return 1;
  else if (board[(x+1)][(y)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (SOUTHWEST == dir)
 {
  if (board[(x+1)][(y-1)] < MAX_PIECES)
   return 1;
  else if (board[(x-1)][(y)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else if (WEST == dir)
 {
  if (board[(x-1)][(y+1)] < MAX_PIECES)
   return 1;
  else if (board[(x)][(y-1)] < MAX_PIECES)
   return 1;
  return 0;
 }
 else
 {
  if (board[(x)][(y+1)] < MAX_PIECES)
   return 1;
  else if (board[(x-1)][(y)] < MAX_PIECES)
   return 1;
  return 0;
 }
}

user_defined_types: enum DIRECTIONS_e { NORTHEAST=0, EAST=1, SOUTHEAST=2, SOUTHWEST=3, WEST=4, NORTHWEST=5, MAX_DIR=6 }


isTouchingOnlySameColor

decompiled_code:
bool __fastcall isTouchingOnlySameColor(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // ebx
__int64 v4; // r14
__int64 v5; // rdx
__int64 v6; // rbx
bool result; // al
__int64 v8; // [rsp+8h] [rbp-40h]
__int64 v9; // [rsp+8h] [rbp-40h]

v3 = a3;
v4 = a3 + 1;
if ( (unsigned __int8)board[256 * (unsigned __int64)a2 + v4] <= 0x15u && (unsigned __int8)isWhite() != a1 )
return 0;
v5 = v3;
if ( (unsigned __int8)board[256 * (__int64)(a2 + 1) + v3] <= 0x15u )
{
if ( (unsigned __int8)isWhite() != a1 )
return 0;
v5 = v3;
}
v6 = v3 - 1;
if ( (unsigned __int8)board[256 * (__int64)(a2 + 1) + v6] <= 0x15u )
{
v8 = v5;
if ( (unsigned __int8)isWhite() != a1 )
return 0;
v5 = v8;
}
if ( (unsigned __int8)board[256 * (unsigned __int64)a2 + v6] <= 0x15u )
{
v9 = v5;
if ( (unsigned __int8)isWhite() == a1 )
{
v5 = v9;
goto LABEL_5;
}
return 0;
}
LABEL_5:
if ( (unsigned __int8)board[256 * (__int64)(a2 - 1) + v5] <= 0x15u && (unsigned __int8)isWhite() != a1 )
return 0;
result = 1;
if ( (unsigned __int8)board[256 * (__int64)(a2 - 1) + v4] <= 0x15u )
result = (unsigned __int8)isWhite() == a1;
return result;
}

canonical_decompiled_code:
bool __fastcall func10(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // ebx
__int64 v4; // r14
__int64 v5; // rdx
__int64 v6; // rbx
bool result; // al
__int64 v8; // [rsp+8h] [rbp-40h]
__int64 v9; // [rsp+8h] [rbp-40h]

v3 = a3;
v4 = a3 + 1;
if ( (unsigned __int8)board[256 * (unsigned __int64)a2 + v4] <= 0x15u && (unsigned __int8)isWhite() != a1 )
return 0;
v5 = v3;
if ( (unsigned __int8)board[256 * (__int64)(a2 + 1) + v3] <= 0x15u )
{
if ( (unsigned __int8)isWhite() != a1 )
return 0;
v5 = v3;
}
v6 = v3 - 1;
if ( (unsigned __int8)board[256 * (__int64)(a2 + 1) + v6] <= 0x15u )
{
v8 = v5;
if ( (unsigned __int8)isWhite() != a1 )
return 0;
v5 = v8;
}
if ( (unsigned __int8)board[256 * (unsigned __int64)a2 + v6] <= 0x15u )
{
v9 = v5;
if ( (unsigned __int8)isWhite() == a1 )
{
v5 = v9;
goto LABEL_5;
}
return 0;
}
LABEL_5:
if ( (unsigned __int8)board[256 * (__int64)(a2 - 1) + v5] <= 0x15u && (unsigned __int8)isWhite() != a1 )
return 0;
result = 1;
if ( (unsigned __int8)board[256 * (__int64)(a2 - 1) + v4] <= 0x15u )
result = (unsigned __int8)isWhite() == a1;
return result;
}

original_code:
_Bool isTouchingOnlySameColor(_Bool white, uint8_t x, uint8_t y)
{
 if (board[(x)][(y+1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x)][(y+1)]))
   return 0;
 }
 if (board[(x+1)][(y)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x+1)][(y)]))
   return 0;
 }
 if (board[(x+1)][(y-1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x+1)][(y-1)]))
   return 0;
 }
 if (board[(x)][(y-1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x)][(y-1)]))
   return 0;
 }
 if (board[(x-1)][(y)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x-1)][(y)]))
   return 0;
 }
 if (board[(x-1)][(y+1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x-1)][(y+1)]))
   return 0;
 }
 return 1;
}

canonical_original_code:
_Bool isTouchingOnlySameColor(_Bool white, unsigned char x, unsigned char y)
{
 if (board[(x)][(y+1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x)][(y+1)]))
   return 0;
 }
 if (board[(x+1)][(y)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x+1)][(y)]))
   return 0;
 }
 if (board[(x+1)][(y-1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x+1)][(y-1)]))
   return 0;
 }
 if (board[(x)][(y-1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x)][(y-1)]))
   return 0;
 }
 if (board[(x-1)][(y)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x-1)][(y)]))
   return 0;
 }
 if (board[(x-1)][(y+1)] < MAX_PIECES)
 {
  if (white != isWhite(board[(x-1)][(y+1)]))
   return 0;
 }
 return 1;
}


getTime

decompiled_code:
__int64 getTime()
{
return (unsigned int)(int)((double)(int)clock() * 1000.0 / 1000000.0);
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)(int)((double)(int)clock() * 1000.0 / 1000000.0);
}

original_code:
int getTime()
{
    double timeTaken;
    clock_t mark;
    mark = clock();
 timeTaken = (double) mark;
 timeTaken = (1000 * timeTaken) / ((__clock_t) 1000000);
    return (int) timeTaken;
}

canonical_original_code:
int getTime()
{
    double timeTaken;
    long mark;
    mark = clock();
 timeTaken = (double) mark;
 timeTaken = (1000 * timeTaken) / ((long) 1000000);
    return (int) timeTaken;
}

variable_types: timeTaken double

variable_types: mark long


startTimer

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

result = getTime();
searchTime = result;
return result;
}

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

result = func0();
searchTime = result;
return result;
}

original_code:
void startTimer(void)
{
    searchTime = getTime();
}

canonical_original_code:
void startTimer()
{
    searchTime = getTime();
}


getSearchTime

decompiled_code:
__int64 getSearchTime()
{
return (unsigned int)getTime() - searchTime;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)func0() - searchTime;
}

original_code:
int getSearchTime(void)
{
 return (getTime() - searchTime);
}

canonical_original_code:
int getSearchTime()
{
 return (getTime() - searchTime);
}


canonical_original_code:
unsigned long rand64()
{
    return rand() ^ ((unsigned long)rand() << 15) ^ ((unsigned long)rand() << 30) ^
 ((unsigned long)rand() << 45) ^ ((unsigned long)rand() << 60);
}

return_type: unsigned long


probeHash

decompiled_code:
__int64 __fastcall probeHash(int a1, int a2, int a3)
{
unsigned int v4; // er8
__int64 *v5; // rdx
int v7; // eax

v4 = 0;
v5 = &hash_table[3 * (currentZobristKey % 0x2C0000uLL)];
if ( currentZobristKey == *v5 && *((_DWORD *)v5 + 2) >= a1 )
{
v7 = *((_DWORD *)v5 + 3);
if ( v7 == 1 )
{
v4 = *((_DWORD *)v5 + 4);
}
else if ( v7 == 2 )
{
if ( *((_DWORD *)v5 + 4) <= a2 )
v4 = a2;
}
else if ( v7 == 3 && *((_DWORD *)v5 + 4) >= a3 )
{
v4 = a3;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3)
{
unsigned int v4; // er8
__int64 *v5; // rdx
int v7; // eax

v4 = 0;
v5 = &hash_table[3 * (currentZobristKey % 0x2C0000uLL)];
if ( currentZobristKey == *v5 && *((_DWORD *)v5 + 2) >= a1 )
{
v7 = *((_DWORD *)v5 + 3);
if ( v7 == 1 )
{
v4 = *((_DWORD *)v5 + 4);
}
else if ( v7 == 2 )
{
if ( *((_DWORD *)v5 + 4) <= a2 )
v4 = a2;
}
else if ( v7 == 3 && *((_DWORD *)v5 + 4) >= a3 )
{
v4 = a3;
}
}
return v4;
}

original_code:
int probeHash(int depth, int alpha, int beta)
{
    HASH_t *hash = &hash_table[currentZobristKey % (2883584)];
    if (hash->key == currentZobristKey)
 {
        if (hash->depth >= depth)
  {
            if (hash->flags == 1)
                return hash->val;
            if ((hash->flags == 2) &&
                (hash->val <= alpha))
                return alpha;
            if ((hash->flags == 3) &&
                (hash->val >= beta))
                return beta;
        }
    }
    return 0;
}

canonical_original_code:
int probeHash(int depth, int alpha, int beta)
{
    struct HASH_t *hash = &hash_table[currentZobristKey % (2883584)];
    if (hash->key == currentZobristKey)
 {
        if (hash->depth >= depth)
  {
            if (hash->flags == 1)
                return hash->val;
            if ((hash->flags == 2) &&
                (hash->val <= alpha))
                return alpha;
            if ((hash->flags == 3) &&
                (hash->val >= beta))
                return beta;
        }
    }
    return 0;
}

variable_types: depth int

variable_types: alpha int

variable_types: beta int

variable_types: struct HASH_t { unsigned long key; int depth; int flags; int val; union MOVE_t { struct { unsigned int xpos; unsigned int ypos; enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 } piece; unsigned int reserved; } MOVE_BITS_t; unsigned int move; } best; } *

user_defined_types: struct HASH_t { unsigned long key; int depth; int flags; int val; union MOVE_t best; }


recordHash

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

result = 3 * (currentZobristKey % 0x2C0000uLL);
hash_table[result] = currentZobristKey;
LODWORD(hash_table[result + 2]) = a2;
HIDWORD(hash_table[result + 1]) = a3;
LODWORD(hash_table[result + 1]) = a1;
return result * 8;
}

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

result = 3 * (currentZobristKey % 0x2C0000uLL);
hash_table[result] = currentZobristKey;
LODWORD(hash_table[result + 2]) = a2;
HIDWORD(hash_table[result + 1]) = a3;
LODWORD(hash_table[result + 1]) = a1;
return result * 8;
}

original_code:
void recordHash(int depth, int val, int flags)
{
    HASH_t *hash = &hash_table[currentZobristKey % (2883584)];
    hash->key = currentZobristKey;
    hash->val = val;
 hash->flags = flags;
    hash->depth = depth;
}

canonical_original_code:
void recordHash(int depth, int val, int flags)
{
    struct HASH_t *hash = &hash_table[currentZobristKey % (2883584)];
    hash->key = currentZobristKey;
    hash->val = val;
 hash->flags = flags;
    hash->depth = depth;
}

variable_types: val int

variable_types: flags int


getZobristKey

decompiled_code:
__int64 getZobristKey()
{
return currentZobristKey;
}

canonical_decompiled_code:
__int64 func3()
{
return currentZobristKey;
}

original_code:
uint64_t getZobristKey()
{
 return currentZobristKey;
}

canonical_original_code:
unsigned long getZobristKey()
{
 return currentZobristKey;
}


addPieceToZobristKey

decompiled_code:
__int64 __fastcall addPieceToZobristKey(unsigned int a1)
{
unsigned __int8 v1; // r12
__int64 v2; // rbp
__int64 result; // rax

v1 = isWhite(a1);
v2 = (unsigned __int8)getXpos(a1);
result = (unsigned __int8)getYpos(a1) + ((v2 + ((a1 + 22LL * v1) << 8)) << 8);
currentZobristKey ^= zobrist[result] ^ blackToPlayKey;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
unsigned __int8 v1; // r12
__int64 v2; // rbp
__int64 result; // rax

v1 = isWhite(a1);
v2 = (unsigned __int8)getXpos(a1);
result = (unsigned __int8)getYpos(a1) + ((v2 + ((a1 + 22LL * v1) << 8)) << 8);
currentZobristKey ^= zobrist[result] ^ blackToPlayKey;
return result;
}

original_code:
void addPieceToZobristKey(PIECE_e piece)
{
 uint8_t colour, x, y;
 colour = isWhite(piece);
 x = getXpos(piece);
 y = getYpos(piece);
 currentZobristKey ^= zobrist[colour][piece][x][y];
 currentZobristKey ^= blackToPlayKey;
}

canonical_original_code:
void addPieceToZobristKey(enum PIECE_e piece)
{
 unsigned char colour; unsigned char x; unsigned char y;
 colour = isWhite(piece);
 x = getXpos(piece);
 y = getYpos(piece);
 currentZobristKey ^= zobrist[colour][piece][x][y];
 currentZobristKey ^= blackToPlayKey;
}

variable_types: enum PIECE_e { WHITE_QUEEN=0, WHITE_BEETLE_ONE=1, WHITE_BEETLE_TWO=2, WHITE_GRASSHOPPER_ONE=3, WHITE_GRASSHOPPER_TWO=4, WHITE_GRASSHOPPER_THREE=5, WHITE_ANT_ONE=6, WHITE_ANT_TWO=7, WHITE_ANT_THREE=8, WHITE_SPIDER_ONE=9, WHITE_SPIDER_TWO=10, BLACK_QUEEN=11, BLACK_BEETLE_ONE=12, BLACK_BEETLE_TWO=13, BLACK_GRASSHOPPER_ONE=14, BLACK_GRASSHOPPER_TWO=15, BLACK_GRASSHOPPER_THREE=16, BLACK_ANT_ONE=17, BLACK_ANT_TWO=18, BLACK_ANT_THREE=19, BLACK_SPIDER_ONE=20, BLACK_SPIDER_TWO=21, MAX_PIECES=22, EMPTY_SPACE=23 }

variable_types:
colour
unsigned char

variable_types:
x
unsigned char

variable_types:
y
unsigned char


zobristInit

original_code:
void zobristInit(void)
{
 uint32_t colour, piece, x, y;
 for (colour = 0; colour < MAX_COLOURS; colour++)
 {
  for (piece = 0; piece < MAX_PIECES; piece++)
  {
   for (x = 0; x < (256); x++)
   {
    for (y = 0; y < (256); y++)
    {
     zobrist[colour][piece][x][y] = rand64();
    }
   }
  }
 }
 blackToPlayKey = rand64();
 currentZobristKey = 0;
 cacheHits = 0;
 cacheMisses = 0;
}

canonical_original_code:
void zobristInit()
{
 unsigned int colour; unsigned int piece; unsigned int x; unsigned int y;
 for (colour = 0; colour < MAX_COLOURS; colour++)
 {
  for (piece = 0; piece < MAX_PIECES; piece++)
  {
   for (x = 0; x < (256); x++)
   {
    for (y = 0; y < (256); y++)
    {
     zobrist[colour][piece][x][y] = rand64();
    }
   }
  }
 }
 blackToPlayKey = rand64();
 currentZobristKey = 0;
 cacheHits = 0;
 cacheMisses = 0;
}

variable_types:
colour
unsigned int

variable_types:
piece
unsigned int

variable_types:
x
unsigned int

variable_types:
y
unsigned int


evaluateBoard

decompiled_code:
__int64 __fastcall evaluateBoard(__int64 a1)
{
int v1; // er15
int v2; // er14
int v3; // er12
unsigned int i; // ebx
int v5; // eax
unsigned int v6; // ebp
unsigned int v7; // er14
unsigned __int8 v9; // bl
unsigned __int8 v10; // al
char v11; // al
unsigned __int8 v12; // bl
unsigned __int8 v13; // al
char v14; // al

v1 = 0;
v2 = 0;
v3 = 0;
for ( i = 1; ; ++i )
{
v6 = i - 1;
if ( !(unsigned __int8)hasPieceBeenPlayed(i - 1) )
{
if ( i > 0x15 )
break;
continue;
}
v5 = *(_DWORD *)(a1 + 4LL * v6);
if ( !v5 )
v2 += *((_DWORD *)&pinnedMap + v6);
if ( v6 <= 0xA )
{
v3 += v5;
continue;
}
v1 += v5;
if ( i > 0x15 )
break;
}
v7 = 5 * (v3 - v1) + v2;
if ( !(unsigned __int8)hasPieceBeenPlayed(0LL) )
goto LABEL_10;
v9 = getXpos(0LL);
v10 = getYpos(0LL);
v11 = getNumberOfOccupiedSquaresAroundCoords(v9, v10);
switch ( v11 )
{
case 5:
v7 -= 4000;
LABEL_10:
if ( (unsigned __int8)hasPieceBeenPlayed(11LL) )
goto LABEL_19;
return v7;
case 4:
v7 -= 2000;
goto LABEL_10;
case 3:
v7 -= 400;
break;
}
if ( (unsigned __int8)hasPieceBeenPlayed(11LL) )
{
LABEL_19:
v12 = getXpos(11LL);
v13 = getYpos(11LL);
v14 = getNumberOfOccupiedSquaresAroundCoords(v12, v13);
switch ( v14 )
{
case 5:
v7 += 4000;
break;
case 4:
v7 += 2000;
break;
case 3:
v7 += 400;
break;
}
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // er15
int v2; // er14
int v3; // er12
unsigned int i; // ebx
int v5; // eax
unsigned int v6; // ebp
unsigned int v7; // er14
unsigned __int8 v9; // bl
unsigned __int8 v10; // al
char v11; // al
unsigned __int8 v12; // bl
unsigned __int8 v13; // al
char v14; // al

v1 = 0;
v2 = 0;
v3 = 0;
for ( i = 1; ; ++i )
{
v6 = i - 1;
if ( !(unsigned __int8)hasPieceBeenPlayed(i - 1) )
{
if ( i > 0x15 )
break;
continue;
}
v5 = *(_DWORD *)(a1 + 4LL * v6);
if ( !v5 )
v2 += *((_DWORD *)&pinnedMap + v6);
if ( v6 <= 0xA )
{
v3 += v5;
continue;
}
v1 += v5;
if ( i > 0x15 )
break;
}
v7 = 5 * (v3 - v1) + v2;
if ( !(unsigned __int8)hasPieceBeenPlayed(0LL) )
goto LABEL_10;
v9 = getXpos(0LL);
v10 = getYpos(0LL);
v11 = getNumberOfOccupiedSquaresAroundCoords(v9, v10);
switch ( v11 )
{
case 5:
v7 -= 4000;
LABEL_10:
if ( (unsigned __int8)hasPieceBeenPlayed(11LL) )
goto LABEL_19;
return v7;
case 4:
v7 -= 2000;
goto LABEL_10;
case 3:
v7 -= 400;
break;
}
if ( (unsigned __int8)hasPieceBeenPlayed(11LL) )
{
LABEL_19:
v12 = getXpos(11LL);
v13 = getYpos(11LL);
v14 = getNumberOfOccupiedSquaresAroundCoords(v12, v13);
switch ( v14 )
{
case 5:
v7 += 4000;
break;
case 4:
v7 += 2000;
break;
case 3:
v7 += 400;
break;
}
}
return v7;
}

original_code:
int evaluateBoard(EVALUATE_INFO_t *eval)
{
 int score = 0;
 uint8_t x, y, occupied;
 PIECE_e i;
 int whiteMovesForPlayedPieces = 0;
 int blackMovesForPlayedPieces = 0;
 for (i = WHITE_QUEEN; i < MAX_PIECES; i++)
 {
  if (hasPieceBeenPlayed(i))
  {
   if (eval->validMoves[i] == 0)
   {
    score += pinnedMap[i];
   }
   if (i < BLACK_QUEEN)
   {
    whiteMovesForPlayedPieces += eval->validMoves[i];
   }
   else
   {
    blackMovesForPlayedPieces += eval->validMoves[i];
   }
  }
 }
 score += (whiteMovesForPlayedPieces - blackMovesForPlayedPieces) * 5;
 if (hasPieceBeenPlayed(WHITE_QUEEN))
 {
  x = getXpos(WHITE_QUEEN);
  y = getYpos(WHITE_QUEEN);
  occupied = getNumberOfOccupiedSquaresAroundCoords(x, y);
  if (occupied == 5)
  {
   score -= (4000);
  }
  else if (occupied == 4)
  {
   score -= (2000);
  }
  else if (occupied == 3)
  {
   score -= (400);
  }
  else if (occupied == 2)
  {
   score -= (0);
  }
 }
 if (hasPieceBeenPlayed(BLACK_QUEEN))
 {
  x = getXpos(BLACK_QUEEN);
  y = getYpos(BLACK_QUEEN);
  occupied = getNumberOfOccupiedSquaresAroundCoords(x, y);
  if (occupied == 5)
  {
   score += (4000);
  }
  else if (occupied == 4)
  {
   score += (2000);
  }
  else if (occupied == 3)
  {
   score += (400);
  }
  else if (occupied == 2)
  {
   score += (0);
  }
 }
 return score;
}

canonical_original_code:
int evaluateBoard(struct EVALUATE_INFO_t *eval)
{
 int score = 0;
 unsigned char x; unsigned char y; unsigned char occupied;
 enum PIECE_e i;
 int whiteMovesForPlayedPieces = 0;
 int blackMovesForPlayedPieces = 0;
 for (i = WHITE_QUEEN; i < MAX_PIECES; i++)
 {
  if (hasPieceBeenPlayed(i))
  {
   if (eval->validMoves[i] == 0)
   {
    score += pinnedMap[i];
   }
   if (i < BLACK_QUEEN)
   {
    whiteMovesForPlayedPieces += eval->validMoves[i];
   }
   else
   {
    blackMovesForPlayedPieces += eval->validMoves[i];
   }
  }
 }
 score += (whiteMovesForPlayedPieces - blackMovesForPlayedPieces) * 5;
 if (hasPieceBeenPlayed(WHITE_QUEEN))
 {
  x = getXpos(WHITE_QUEEN);
  y = getYpos(WHITE_QUEEN);
  occupied = getNumberOfOccupiedSquaresAroundCoords(x, y);
  if (occupied == 5)
  {
   score -= (4000);
  }
  else if (occupied == 4)
  {
   score -= (2000);
  }
  else if (occupied == 3)
  {
   score -= (400);
  }
  else if (occupied == 2)
  {
   score -= (0);
  }
 }
 if (hasPieceBeenPlayed(BLACK_QUEEN))
 {
  x = getXpos(BLACK_QUEEN);
  y = getYpos(BLACK_QUEEN);
  occupied = getNumberOfOccupiedSquaresAroundCoords(x, y);
  if (occupied == 5)
  {
   score += (4000);
  }
  else if (occupied == 4)
  {
   score += (2000);
  }
  else if (occupied == 3)
  {
   score += (400);
  }
  else if (occupied == 2)
  {
   score += (0);
  }
 }
 return score;
}

variable_types: struct EVALUATE_INFO_t { unsigned int validMoves[-1]; unsigned int whiteMoves; unsigned int blackMoves; } *

variable_types: score int

variable_types:
occupied
unsigned char

variable_types: whiteMovesForPlayedPieces int

variable_types: blackMovesForPlayedPieces int