decompiled_code:
int __fastcall process_signals(int *a1)
{
int result; // eax
__int128 *v2; // rbx
unsigned int i; // ebp
__int64 v4; // rcx
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( *((int *)&cmd_options + 1) > 1 )
fwrite("Received signal\n", 1uLL, 0x10uLL, stderr);
result = read(*a1, buf, 4uLL);
v2 = xmmword_5A8;
for ( i = 0; i != 66; ++i )
{
v4 = *((unsigned int *)v2 + 32);
if ( (int)v4 > 0 )
{
if ( *((_QWORD *)v2 - 1) )
{
if ( *((int *)&cmd_options + 1) > 1 )
fprintf(stderr, "Received signal %d %d times: Handling\n", i, v4);
result = (*((__int64 (__fastcall **)(__int128 *, _QWORD))v2 - 1))(v2, *((unsigned int *)v2 + 32));
*((_DWORD *)v2 + 32) = 0;
}
else if ( *((int *)&cmd_options + 1) > 1 )
{
result = fprintf(stderr, "Received signal %d %d times: Ignoring\n", i, v4);
}
}
v2 += 9;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int *a1)
{
int result; // eax
__int128 *v2; // rbx
unsigned int i; // ebp
__int64 v4; // rcx
char buf[44]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( *((int *)&cmd_options + 1) > 1 )
fwrite("Received signal\n", 1uLL, 0x10uLL, stderr);
result = read(*a1, buf, 4uLL);
v2 = xmmword_5A8;
for ( i = 0; i != 66; ++i )
{
v4 = *((unsigned int *)v2 + 32);
if ( (int)v4 > 0 )
{
if ( *((_QWORD *)v2 - 1) )
{
if ( *((int *)&cmd_options + 1) > 1 )
fprintf(stderr, "Received signal %d %d times: Handling\n", i, v4);
result = (*((__int64 (__fastcall **)(__int128 *, _QWORD))v2 - 1))(v2, *((unsigned int *)v2 + 32));
*((_DWORD *)v2 + 32) = 0;
}
else if ( *((int *)&cmd_options + 1) > 1 )
{
result = fprintf(stderr, "Received signal %d %d times: Ignoring\n", i, v4);
}
}
v2 += 9;
}
return result;
}

original_code:
static void process_signals(struct loop_fd *fd, int revents) {
 struct signal_fd *signal = (struct signal_fd *)fd;
 char buf[4];
 do { if (cmd_options.verbose >= 2) fprintf(stderr, "Received signal" "\n"); } while (0);
 read(signal->fd.fd, buf, sizeof(buf));
 for (int i = 0; i <= (64 + 1); i++) {
  if (signal_callbacks[i].num_calls > 0) {
   if (signal_callbacks[i].handler) {
    do { if (cmd_options.verbose >= 2) fprintf(stderr, "Received signal %d %d times: Handling" "\n", i, signal_callbacks[i].num_calls); } while (0);
    signal_callbacks[i].handler(&signal_callbacks[i].siginfo,
           signal_callbacks[i].num_calls);
    signal_callbacks[i].num_calls = 0;
   } else {
    do { if (cmd_options.verbose >= 2) fprintf(stderr, "Received signal %d %d times: Ignoring" "\n", i, signal_callbacks[i].num_calls); } while (0);
   }
  }
 }
}

variable_types: struct loop_fd { int fd; int poll_flags; void (*poll_callback)(struct loop_fd *fd, int revents); } *

variable_types: revents int

variable_types: struct signal_fd { struct loop_fd { int fd; int poll_flags; void (*poll_callback)(struct loop_fd *fd, int revents); } fd; int write_fd; _Bool received_sigwinch; } *

variable_types:
buf
char [4]

user_defined_types: struct signal_fd { struct loop_fd fd; int write_fd; _Bool received_sigwinch; }


repo: travisb-ca/tachyon

decompiled_code:
__int64 __fastcall loop_register(_DWORD *a1)
{
int v1; // ebx
int v2; // er15
int v3; // er14
void *v4; // r12
void *v5; // rax
void *v6; // r13
size_t v7; // rax
void *v8; // r15
size_t v9; // rdx
char *v10; // rax
size_t n; // [rsp+8h] [rbp-40h]
void *na; // [rsp+8h] [rbp-40h]

v1 = num_loop_items;
v2 = max_loop_items;
if ( num_loop_items < max_loop_items )
goto LABEL_7;
v3 = 2 * max_loop_items;
if ( !max_loop_items )
v3 = 16;
v4 = calloc(v3, 8uLL);
v5 = calloc(v3, 8uLL);
v6 = v5;
if ( v4 && v5 )
{
v7 = 8LL * v2;
v8 = loop_items;
n = v7;
memmove(v4, loop_items, v7);
v9 = n;
na = fds;
memmove(v6, fds, v9);
free(v8);
loop_items = v4;
free(na);
fds = v6;
max_loop_items = v3;
LABEL_7:
num_loop_items = v1 + 1;
*((_QWORD *)loop_items + v1) = a1;
v10 = (char *)fds + 8 * v1;
*(_DWORD *)v10 = *a1;
*((_WORD *)v10 + 2) = a1[1];
return 0LL;
}
if ( v4 )
free(v4);
if ( v6 )
free(v6);
return 12LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
int v1; // ebx
int v2; // er15
int v3; // er14
void *v4; // r12
void *v5; // rax
void *v6; // r13
size_t v7; // rax
void *v8; // r15
size_t v9; // rdx
char *v10; // rax
size_t n; // [rsp+8h] [rbp-40h]
void *na; // [rsp+8h] [rbp-40h]

v1 = num_loop_items;
v2 = max_loop_items;
if ( num_loop_items < max_loop_items )
goto LABEL_7;
v3 = 2 * max_loop_items;
if ( !max_loop_items )
v3 = 16;
v4 = calloc(v3, 8uLL);
v5 = calloc(v3, 8uLL);
v6 = v5;
if ( v4 && v5 )
{
v7 = 8LL * v2;
v8 = loop_items;
n = v7;
memmove(v4, loop_items, v7);
v9 = n;
na = fds;
memmove(v6, fds, v9);
free(v8);
loop_items = v4;
free(na);
fds = v6;
max_loop_items = v3;
LABEL_7:
num_loop_items = v1 + 1;
*((_QWORD *)loop_items + v1) = a1;
v10 = (char *)fds + 8 * v1;
*(_DWORD *)v10 = *a1;
*((_WORD *)v10 + 2) = a1[1];
return 0LL;
}
if ( v4 )
free(v4);
if ( v6 )
free(v6);
return 12LL;
}

original_code:
int loop_register(struct loop_fd *fd) {
 int i;
 if (num_loop_items >= max_loop_items) {
  struct loop_item *new_items = ((void *)0);
  struct pollfd *new_fds = ((void *)0);
  int new_size;
  if (max_loop_items == 0)
   new_size = 16;
  else
   new_size = 2 * max_loop_items;
  new_items = calloc(new_size, sizeof(*loop_items));
  new_fds = calloc(new_size, sizeof(*loop_items));
  if (!new_items || !new_fds) {
   if (new_items)
    free(new_items);
   if (new_fds)
    free(new_fds);
   return 12;
  }
  memmove(new_items, loop_items, max_loop_items * sizeof(*loop_items));
  memmove(new_fds, fds, max_loop_items * sizeof(*fds));
  free(loop_items);
  loop_items = new_items;
  free(fds);
  fds = new_fds;
  max_loop_items = new_size;
 }
 i = num_loop_items++;
 loop_items[i].fd = fd;
 fds[i].fd = fd->fd;
 fds[i].events = fd->poll_flags;
 return 0;
}

variable_types: i int

variable_types: struct loop_item { struct loop_fd { int fd; int poll_flags; void (*poll_callback)(struct loop_fd *fd, int revents); } *fd; } *

variable_types: struct pollfd { int fd; short events; short revents; } *

return_type: int

user_defined_types: struct loop_item { struct loop_fd *fd; }


decompiled_code:
__int64 init_signals()
{
unsigned int v0; // ebx
__int64 result; // rax
int *v2; // rax
int *v3; // rax
struct sigaction v4; // [rsp+0h] [rbp-A8h] BYREF
int pipedes[2]; // [rsp+98h] [rbp-10h] BYREF

if ( pipe(pipedes) == -1 )
{
result = 32LL;
if ( *((int *)&cmd_options + 1) >= 0 )
{
v2 = _errno_location();
fprintf(stderr, "Unable to create signal pipe %d\n", (unsigned int)*v2);
result = 32LL;
}
}
else
{
close_on_exec((unsigned int)pipedes[0]);
close_on_exec((unsigned int)pipedes[1]);
signal_fd = pipedes[0];
fd = pipedes[1];
dword_2AC4 = 1;
qword_2AC8 = (__int64)process_signals;
memset(&v4.sa_mask, 0, 0x90uLL);
v4.sa_handler = (__sighandler_t)signal_handler;
v4.sa_flags = 268435460;
v0 = sigaction(28, &v4, 0LL);
if ( *((int *)&cmd_options + 1) > 1 )
{
v3 = _errno_location();
fprintf(stderr, "sigaction returned %d %d\n", v0, (unsigned int)*v3);
}
result = loop_register(&signal_fd);
if ( (_DWORD)result )
result = 12LL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // ebx
__int64 result; // rax
int *v2; // rax
int *v3; // rax
struct sigaction v4; // [rsp+0h] [rbp-A8h] BYREF
int pipedes[2]; // [rsp+98h] [rbp-10h] BYREF

if ( pipe(pipedes) == -1 )
{
result = 32LL;
if ( *((int *)&cmd_options + 1) >= 0 )
{
v2 = _errno_location();
fprintf(stderr, "Unable to create signal pipe %d\n", (unsigned int)*v2);
result = 32LL;
}
}
else
{
close_on_exec((unsigned int)pipedes[0]);
close_on_exec((unsigned int)pipedes[1]);
signal_fd = pipedes[0];
fd = pipedes[1];
dword_2AC4 = 1;
qword_2AC8 = (__int64)process_signals;
memset(&v4.sa_mask, 0, 0x90uLL);
v4.sa_handler = (__sighandler_t)signal_handler;
v4.sa_flags = 268435460;
v0 = sigaction(28, &v4, 0LL);
if ( *((int *)&cmd_options + 1) > 1 )
{
v3 = _errno_location();
fprintf(stderr, "sigaction returned %d %d\n", v0, (unsigned int)*v3);
}
result = func2(&signal_fd);
if ( (_DWORD)result )
result = 12LL;
}
return result;
}

original_code:
static int init_signals(void) {
 int pipes[2];
 int result;
 struct sigaction sig;
 result = pipe(pipes);
 if (result == -1) {
  do { if (cmd_options.verbose >= 0) fprintf(stderr, "Unable to create signal pipe %d" "\n", (*__errno_location ())); } while (0);
  return 32;
 }
 close_on_exec(pipes[0]);
 close_on_exec(pipes[1]);
 signal_fd.fd.fd = pipes[0];
 signal_fd.write_fd = pipes[1];
 signal_fd.fd.poll_flags = 0x001;
 signal_fd.fd.poll_callback = process_signals;
 memset(&sig, 0, sizeof(sig));
 sig.__sigaction_handler.sa_sigaction = signal_handler;
 sig.sa_flags = 4 | 0x10000000;
 result = sigaction(28, &sig, ((void *)0));
 do { if (cmd_options.verbose >= 2) fprintf(stderr, "sigaction returned %d %d" "\n", result, (*__errno_location ())); } while (0);
 result = loop_register((struct loop_fd *)&signal_fd);
 if (result)
  return 12;
 return 0;
}

canonical_original_code:
static int init_signals() {
 int pipes[2];
 int result;
 struct sigaction sig;
 result = pipe(pipes);
 if (result == -1) {
  do { if (cmd_options.verbose >= 0) fprintf(stderr, "Unable to create signal pipe %d" "\n", (*__errno_location ())); } while (0);
  return 32;
 }
 close_on_exec(pipes[0]);
 close_on_exec(pipes[1]);
 signal_fd.fd.fd = pipes[0];
 signal_fd.write_fd = pipes[1];
 signal_fd.fd.poll_flags = 0x001;
 signal_fd.fd.poll_callback = process_signals;
 memset(&sig, 0, sizeof(sig));
 sig.__sigaction_handler.sa_sigaction = signal_handler;
 sig.sa_flags = 4 | 0x10000000;
 result = sigaction(28, &sig, ((void *)0));
 do { if (cmd_options.verbose >= 2) fprintf(stderr, "sigaction returned %d %d" "\n", result, (*__errno_location ())); } while (0);
 result = loop_register((struct loop_fd *)&signal_fd);
 if (result)
  return 12;
 return 0;
}

variable_types:
pipes
int [2]

variable_types: result int

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

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

user_defined_types: struct loop_fd { int fd; int poll_flags; void (*poll_callback)(struct loop_fd *fd, int revents); }

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


decompiled_code:
__int64 __fastcall loop_deregister(__int64 a1)
{
int v1; // ecx
_QWORD *v2; // rdx
int v3; // eax
__int64 v4; // rdx

v1 = num_loop_items;
if ( num_loop_items <= 0 )
{
v3 = 0;
}
else
{
v2 = loop_items;
v3 = 0;
while ( *v2 != a1 )
{
++v3;
++v2;
if ( v3 == num_loop_items )
{
v3 = num_loop_items;
break;
}
}
}
v4 = 8LL * num_loop_items;
*((_QWORD *)loop_items + v3) = *(_QWORD *)((char *)loop_items + v4 - 8);
*((_QWORD *)fds + v3) = *(_QWORD *)((char *)fds + v4 - 8);
num_loop_items = v1 - 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v1; // ecx
_QWORD *v2; // rdx
int v3; // eax
__int64 v4; // rdx

v1 = num_loop_items;
if ( num_loop_items <= 0 )
{
v3 = 0;
}
else
{
v2 = loop_items;
v3 = 0;
while ( *v2 != a1 )
{
++v3;
++v2;
if ( v3 == num_loop_items )
{
v3 = num_loop_items;
break;
}
}
}
v4 = 8LL * num_loop_items;
*((_QWORD *)loop_items + v3) = *(_QWORD *)((char *)loop_items + v4 - 8);
*((_QWORD *)fds + v3) = *(_QWORD *)((char *)fds + v4 - 8);
num_loop_items = v1 - 1;
return 0LL;
}

original_code:
int loop_deregister(struct loop_fd *fd) {
 int i;
 for (i = 0; i < num_loop_items; i++)
  if (loop_items[i].fd == fd)
   break;
 memmove(&loop_items[i], &loop_items[num_loop_items - 1], sizeof(*loop_items));
 memmove(&fds[i], &fds[num_loop_items - 1], sizeof(*fds));
 num_loop_items--;
 return 0;
}


decompiled_code:
__int64 loop_init()
{
return init_signals();
}

canonical_decompiled_code:
__int64 func6()
{
return func3();
}

original_code:
int loop_init(void) {
 int result;
 result = init_signals();
 return result;
}

canonical_original_code:
int loop_init() {
 int result;
 result = init_signals();
 return result;
}


decompiled_code:
__int64 loop_run()
{
int v0; // ecx
_QWORD *v1; // rdi
_WORD *v2; // rsi
__int64 v3; // rax
int v4; // ebx
__int64 v5; // rcx
__int64 result; // rax
__int64 v7; // rbx
__int16 v8; // si
__int64 v9; // rdi
void (__fastcall *v10)(__int64, _QWORD); // rax

v0 = num_loop_items;
if ( num_loop_items > 0 )
{
v1 = loop_items;
v2 = fds;
v3 = 0LL;
do
{
v2[4 * v3 + 2] = *(_DWORD *)(v1[v3] + 4LL);
++v3;
}
while ( v3 != v0 );
}
while ( 1 )
{
v4 = pal_poll(fds, num_loop_items, 0xFFFFFFFFLL);
if ( v4 > 0 )
break;
v5 = (unsigned int)*_errno_location();
if ( (_DWORD)v5 != 4 )
{
result = 0LL;
if ( *((int *)&cmd_options + 1) > 0 )
{
fprintf(stderr, "poll failed %d %d\n", (unsigned int)v4, v5);
result = 0LL;
}
return result;
}
}
v7 = 0LL;
result = 1LL;
if ( num_loop_items > 0 )
{
do
{
v8 = *((_WORD *)fds + 4 * v7 + 3);
if ( v8 )
{
v9 = *((_QWORD *)loop_items + v7);
v10 = *(void (__fastcall **)(__int64, _QWORD))(v9 + 8);
if ( v10 )
v10(v9, (unsigned int)v8);
}
++v7;
}
while ( num_loop_items > (int)v7 );
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // ecx
_QWORD *v1; // rdi
_WORD *v2; // rsi
__int64 v3; // rax
int v4; // ebx
__int64 v5; // rcx
__int64 result; // rax
__int64 v7; // rbx
__int16 v8; // si
__int64 v9; // rdi
void (__fastcall *v10)(__int64, _QWORD); // rax

v0 = num_loop_items;
if ( num_loop_items > 0 )
{
v1 = loop_items;
v2 = fds;
v3 = 0LL;
do
{
v2[4 * v3 + 2] = *(_DWORD *)(v1[v3] + 4LL);
++v3;
}
while ( v3 != v0 );
}
while ( 1 )
{
v4 = pal_poll(fds, num_loop_items, 0xFFFFFFFFLL);
if ( v4 > 0 )
break;
v5 = (unsigned int)*_errno_location();
if ( (_DWORD)v5 != 4 )
{
result = 0LL;
if ( *((int *)&cmd_options + 1) > 0 )
{
fprintf(stderr, "poll failed %d %d\n", (unsigned int)v4, v5);
result = 0LL;
}
return result;
}
}
v7 = 0LL;
result = 1LL;
if ( num_loop_items > 0 )
{
do
{
v8 = *((_WORD *)fds + 4 * v7 + 3);
if ( v8 )
{
v9 = *((_QWORD *)loop_items + v7);
v10 = *(void (__fastcall **)(__int64, _QWORD))(v9 + 8);
if ( v10 )
v10(v9, (unsigned int)v8);
}
++v7;
}
while ( num_loop_items > (int)v7 );
result = 1LL;
}
return result;
}

original_code:
_Bool loop_run(void) {
 int result;
 int i;
 for (i = 0; i < num_loop_items; i++)
  fds[i].events = loop_items[i].fd->poll_flags;
poll:
 result = pal_poll(fds, num_loop_items, -1);
 if (result <= 0) {
  if ((*__errno_location ()) == 4) {
   goto poll;
  }
  do { if (cmd_options.verbose >= 1) fprintf(stderr, "poll failed %d %d" "\n", result, (*__errno_location ())); } while (0);
  return 0;
 }
 for (i = 0; i < num_loop_items; i++) {
  if (fds[i].revents && loop_items[i].fd->poll_callback) {
   loop_items[i].fd->poll_callback(loop_items[i].fd, fds[i].revents);
  }
 }
 return 1;
}

canonical_original_code:
_Bool loop_run() {
 int result;
 int i;
 for (i = 0; i < num_loop_items; i++)
  fds[i].events = loop_items[i].fd->poll_flags;
poll:
 result = pal_poll(fds, num_loop_items, -1);
 if (result <= 0) {
  if ((*__errno_location ()) == 4) {
   goto poll;
  }
  do { if (cmd_options.verbose >= 1) fprintf(stderr, "poll failed %d %d" "\n", result, (*__errno_location ())); } while (0);
  return 0;
 }
 for (i = 0; i < num_loop_items; i++) {
  if (fds[i].revents && loop_items[i].fd->poll_callback) {
   loop_items[i].fd->poll_callback(loop_items[i].fd, fds[i].revents);
  }
 }
 return 1;
}


decompiled_code:
unsigned __int64 __fastcall predictor_output_guess(int *a1, __int64 a2, unsigned int a3, const void *a4)
{
int v6; // er14
int v7; // ecx
unsigned __int64 result; // rax

v6 = a1[2];
v7 = *a1;
if ( *a1 > 1000000 )
{
v7 /= 2;
*a1 = v7;
a1[1] /= 2;
goto LABEL_3;
}
if ( v7 )
{
LABEL_3:
result = (unsigned int)(100 * a1[1] / v7);
if ( (int)result <= 69 )
return result;
}
result = 128LL - v6;
if ( result >= (int)a3 )
{
memcpy((char *)a1 + v6 + 12, a4, (int)a3);
a1[2] += a3;
result = buffer_input(a2, a3, a4);
if ( (_DWORD)result )
a1[2] = v6;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(int *a1, __int64 a2, unsigned int a3, const void *a4)
{
int v6; // er14
int v7; // ecx
unsigned __int64 result; // rax

v6 = a1[2];
v7 = *a1;
if ( *a1 > 1000000 )
{
v7 /= 2;
*a1 = v7;
a1[1] /= 2;
goto LABEL_3;
}
if ( v7 )
{
LABEL_3:
result = (unsigned int)(100 * a1[1] / v7);
if ( (int)result <= 69 )
return result;
}
result = 128LL - v6;
if ( result >= (int)a3 )
{
memcpy((char *)a1 + v6 + 12, a4, (int)a3);
a1[2] += a3;
result = buffer_input(a2, a3, a4);
if ( (_DWORD)result )
a1[2] = v6;
}
return result;
}

original_code:
static void predictor_output_guess(struct predictor *predictor, struct buffer *buffer, int size, char *input) {
 int old_history_used;
 _Bool predict;
 int result;
 old_history_used = predictor->history_used;
 if (predictor->num_chars > 1000000) {
  predictor->num_chars /= 2;
  predictor->num_echoed /= 2;
 }
 if (predictor->num_chars == 0)
  predict = 1;
 else
  predict = (predictor->num_echoed * 100) / predictor->num_chars
   >= 70;
 if (predict && sizeof(predictor->history) - predictor->history_used >= size) {
  memcpy(predictor->history + predictor->history_used,
         input, size);
  predictor->history_used += size;
  result = buffer_input(buffer, size, input);
  if (result != 0) {
   predictor->history_used = old_history_used;
  }
 }
}

variable_types: struct predictor { int num_chars; int num_echoed; int history_used; char history[128]; } *

variable_types: struct buffer { struct loop_fd { int fd; int poll_flags; void (*poll_callback)(struct loop_fd *fd, int revents); } fd; struct predictor predictor; int bufid; int buf_out_used; char buf_out[1024]; struct vt { int vt_mode; unsigned short rows; unsigned short cols; struct vt_cursor_mode { unsigned short row; unsigned short col; unsigned long flags; struct { unsigned int data[-1]; } tabstops; } current; struct vt_cursor_mode saved; char window_title[128]; char icon_name[128]; unsigned int flags; struct vt_line { struct vt_line *next; unsigned short len; struct vt_cell { char c; unsigned long flags; } cells[]; } *topmost; struct vt_line *bottommost; struct vt_line **lines; struct { char chars[128]; unsigned short len; } params; } vt; } *

variable_types: size int

variable_types:
input
char *

return_type: void

user_defined_types: struct buffer { struct loop_fd fd; struct predictor predictor; int bufid; int buf_out_used; char buf_out[1024]; struct vt vt; }

user_defined_types: struct vt { int vt_mode; unsigned short rows; unsigned short cols; struct vt_cursor_mode current; struct vt_cursor_mode saved; char window_title[128]; char icon_name[128]; unsigned int flags; struct vt_line *topmost; struct vt_line *bottommost; struct vt_line **lines; struct { char chars[128]; unsigned short len; } params; }

user_defined_types: struct vt_line { struct vt_line *next; unsigned short len; struct vt_cell cells[]; }


decompiled_code:
__int64 __fastcall predictor_init(__int64 a1)
{
char *v1; // rax

*(_DWORD *)a1 = 0;
*(_DWORD *)(a1 + 4) = 0;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 12) = 0LL;
*(_QWORD *)(a1 + 132) = 0LL;
memset(
(void *)((a1 + 20) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 20) & 0xFFFFFFF8) + 140) >> 3));
if ( backspaces != 8 )
{
v1 = &backspaces;
do
*v1++ = 8;
while ( v1 != (char *)&unk_2E0 );
}
return 0LL;
}

original_code:
int predictor_init(struct predictor *predictor) {
 predictor->num_chars = 0;
 predictor->num_echoed = 0;
 predictor->history_used = 0;
 memset(&predictor->history, 0, sizeof(predictor->history));
 if (backspaces[0] != '\b')
  for (int i = 0; i < sizeof(backspaces); i++)
   backspaces[i] = '\b';
 return 0;
}

user_defined_types: struct predictor { int num_chars; int num_echoed; int history_used; char history[128]; }


decompiled_code:
void predictor_free()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void predictor_free(struct predictor *predictor) {
 return;
}


decompiled_code:
__int64 __fastcall predictor_output(int *a1, __int64 a2, unsigned int a3, const void *a4, __int64 (__fastcall *a5)(__int64, _QWORD, const void *))
{
if ( cmd_options )
predictor_output_guess(a1, a2, a3, a4);
return a5(a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, __int64 a2, unsigned int a3, const void *a4, __int64 (__fastcall *a5)(__int64, _QWORD, const void *))
{
if ( cmd_options )
func0(a1, a2, a3, a4);
return a5(a2, a3, a4);
}

original_code:
int predictor_output(struct predictor *predictor, struct buffer *buffer, int size, char *input,
       int (*outfunc)(struct buffer *buffer, int size, char *buf)) {
 if (cmd_options.predict)
  predictor_output_guess(predictor, buffer, size, input);
 return outfunc(buffer, size, input);
}

variable_types: int (*)(struct buffer *buffer, int size, char *buf)

user_defined_types: struct vt_cursor_mode { unsigned short row; unsigned short col; unsigned long flags; struct { unsigned int data[-1]; } tabstops; }

user_defined_types: struct vt_cell { char c; unsigned long flags; }


original_code:
int predictor_learn(struct predictor *predictor, struct buffer *buffer, int size, char *output) {
 int n;
 int result;
 if (cmd_options.predict) {
  for (n = 0; n < predictor->history_used && n < size; n++) {
   predictor->num_chars++;
   if (output[n] != predictor->history[n])
    break;
   predictor->num_echoed++;
  }
  if (output[n] != predictor->history[n]) {
   predictor->num_chars += ((predictor->history_used - n) < (size - n) ? (predictor->history_used - n) : (size - n));
  }
  buffer_input(buffer, predictor->history_used, backspaces);
 }
 result = buffer_input(buffer, size, output);
 if (cmd_options.predict) {
  predictor->history_used = ((0) > (predictor->history_used - size) ? (0) : (predictor->history_used - size));
  memmove(predictor->history, predictor->history + size, predictor->history_used);
  if (result == 0 && predictor->history_used > 0) {
   buffer_input(buffer, predictor->history_used, predictor->history);
  }
 }
 return result;
}

variable_types:
output
char *

variable_types: n int


decompiled_code:
int __fastcall close_on_exec(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1);
return fcntl(fd, 2, v1 | 1u);
}

canonical_decompiled_code:
int __fastcall func0(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1);
return fcntl(fd, 2, v1 | 1u);
}

original_code:
void close_on_exec(int fd) {
 int flags = fcntl(fd, 1);
 fcntl(fd, 2, flags | 1);
}

variable_types: fd int

variable_types: flags int


decompiled_code:
int __fastcall pal_poll(struct pollfd *a1, nfds_t a2, int a3)
{
return poll(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func0(struct pollfd *a1, nfds_t a2, int a3)
{
return poll(a1, a2, a3);
}

original_code:
int pal_poll(struct pollfd fds[], nfds_t nfds, int timeout) {
 return poll(fds, nfds, timeout);
}

canonical_original_code:
int pal_poll(struct pollfd fds[], unsigned long nfds, int timeout) {
 return poll(fds, nfds, timeout);
}

variable_types: struct pollfd { int fd; short events; short revents; } []

variable_types:
nfds
unsigned long

variable_types: timeout int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // r12
__int64 v5; // rbp
__suseconds_t v6; // rax
__int64 v7; // rbp
__time_t v8; // rdx
__time_t v9; // rcx
__int64 v10; // rsi
char buf[8]; // [rsp+0h] [rbp-180F8h] BYREF
struct timeval v12; // [rsp+8h] [rbp-180F0h] BYREF
char src[98280]; // [rsp+18h] [rbp-180E0h] BYREF
struct termios termios_p; // [rsp+18000h] [rbp-F8h] BYREF
int v15; // [rsp+1803Ch] [rbp-BCh] BYREF
fd_set readfds; // [rsp+18040h] [rbp-B8h] BYREF
struct timeval tv; // [rsp+180C0h] [rbp-38h] BYREF
struct timeval timeout; // [rsp+180D0h] [rbp-28h] BYREF

v15 = 0;
tcgetattr(0, &termios_p);
termios_p.c_lflag &= 0xFFFFFFB5;
termios_p.c_cc[6] = 1;
tcsetattr(0, 0, &termios_p);
setvbuf(_bss_start, 0LL, 2, 0LL);
v15 = 1;
fcntl(0, 4, &v15);
v15 = 0;
while ( v15 )
{
gettimeofday(&tv, 0LL);
timeout.tv_sec = v12.tv_sec - tv.tv_sec;
timeout.tv_usec = v12.tv_usec - tv.tv_usec;
if ( v12.tv_usec - tv.tv_usec < 0 )
{
timeout.tv_sec = v12.tv_sec - tv.tv_sec - 1;
timeout.tv_usec = v12.tv_usec - tv.tv_usec + 1000000;
}
if ( timeout == 0uLL )
{
LABEL_5:
putc(buf[0], _bss_start);
memmove(buf, src, 24LL * --v15);
}
else
{
LABEL_8:
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
if ( !select(1, &readfds, 0LL, 0LL, &timeout) )
goto LABEL_5;
if ( v15 != 4096 && (unsigned int)read(0, &buf[24 * v15], 1uLL) == 1 )
{
v3 = v15;
v4 = v15;
v5 = 2LL * v15;
gettimeofday((struct timeval *)((char *)&v12 + 24 * v15), 0LL);
v6 = *(&v12.tv_usec + v5 + v4) + 2000000;
v7 = v4 + v5;
*(&v12.tv_usec + v7) = v6;
if ( v6 > 1000000 )
{
v8 = *(&v12.tv_sec + v7) + 1;
do
{
v6 -= 1000000LL;
v9 = v8++;
}
while ( v6 > 1000000 );
v10 = 3LL * v3;
*(&v12.tv_usec + v10) = v6;
*(&v12.tv_sec + v10) = v9;
}
++v15;
}
}
}
timeout.tv_sec = 10000000LL;
timeout.tv_usec = 0LL;
goto LABEL_8;
}

original_code:
int main(int argn, char **args) {
 int result;
 result = process_args(argn, args);
 if (result)
  return result - 1;
 if (set_defaults())
  return 1;
 tty_save_termstate();
 result = tty_configure_control_tty();
 do { if (cmd_options.verbose >= 2) fprintf(stderr, "tty_configure_control_tty %d %d" "\n", result, (*__errno_location ())); } while (0);
 result = loop_init();
 do { if (cmd_options.verbose >= 2) fprintf(stderr, "loop_init %d" "\n", result); } while (0);
 result = controller_init();
 do { if (cmd_options.verbose >= 2) fprintf(stderr, "register out %d" "\n", result); } while (0);
 while (run) {
  if (!loop_run())
   do { if (cmd_options.verbose >= 0) fprintf(stderr, "Running the loop failed" "\n"); } while (0);
 }
 tty_restore_termstate();
 return 0;
}

variable_types: argn int

variable_types:
args
char **


gettid

repo: umbrant/ionice-tester

decompiled_code:
__int64 gettid()
{
return syscall(186LL);
}

canonical_decompiled_code:
__int64 func7()
{
return syscall(186LL);
}

original_code:
int gettid() {
  return syscall(186);
}

canonical_original_code:
int gettid() {
  return syscall(186);
}


decompiled_code:
void __fastcall set_priority(unsigned int a1)
{
__int64 v1; // rbx
__int64 v2; // rcx

v1 = (unsigned int)gettid();
v2 = a1;
BYTE1(v2) = BYTE1(a1) | 0x40;
if ( (int)syscall(251LL, 1LL, v1, v2) < 0 )
{
printf("Error trying to set priority of tid %d\n", (unsigned int)v1);
perror("ioprio_set");
}
else
{
printf("Set priority of thread %d to %d\n", (unsigned int)v1, a1);
}
}

canonical_decompiled_code:
void __fastcall func8(unsigned int a1)
{
__int64 v1; // rbx
__int64 v2; // rcx

v1 = (unsigned int)func7();
v2 = a1;
BYTE1(v2) = BYTE1(a1) | 0x40;
if ( (int)syscall(251LL, 1LL, v1, v2) < 0 )
{
printf("Error trying to set priority of tid %d\n", (unsigned int)v1);
perror("ioprio_set");
}
else
{
printf("Set priority of thread %d to %d\n", (unsigned int)v1, a1);
}
}

original_code:
void set_priority(int new_prio) {
  int tid = gettid();
  int new_class = IOPRIO_CLASS_BE;
  int ret = syscall(251, IOPRIO_WHO_PROCESS, tid, (new_prio | new_class << 13));
  if(ret < 0) {
    printf("Error trying to set priority of tid %d\n", tid);
    perror("ioprio_set");
  }
  else {
    printf("Set priority of thread %d to %d\n", tid, new_prio);
  }
}

variable_types: tid int

variable_types: ret int


reader

decompiled_code:
void __fastcall reader(void *a1)
{
int *v1; // rbp
char *v2; // r13
unsigned int v3; // ebx
int v4; // eax
int v5; // er12
int v6; // eax
__time_t v7; // r15
int v8; // ebx
int v9; // eax
__int64 v10; // rax
int v11; // ebx
int v12; // eax
__suseconds_t v13; // rax
__suseconds_t v14; // rcx
double v15; // rbx
unsigned int v16; // ebp
unsigned int v17; // eax
__suseconds_t v18; // [rsp+8h] [rbp-130h]
__suseconds_t v19; // [rsp+10h] [rbp-128h]
__off_t v20; // [rsp+18h] [rbp-120h]
int v21; // [rsp+24h] [rbp-114h]
__suseconds_t v22; // [rsp+28h] [rbp-110h]
struct timeval v23; // [rsp+30h] [rbp-108h] BYREF
struct timeval v24; // [rsp+40h] [rbp-F8h] BYREF
struct stat stat_buf; // [rsp+50h] [rbp-E8h] BYREF
struct timeval tv; // [rsp+E0h] [rbp-58h] BYREF
void *memptr; // [rsp+F8h] [rbp-40h] BYREF

v3 = gettid();
v4 = open(*(const char **)a1, 0x4000);
if ( v4 == -1 )
{
perror("Reader open");
return;
}
v5 = v4;
printf("[%d] Reader opened file %s\n", v3, *(const char **)a1);
v6 = *((_DWORD *)a1 + 3);
if ( v6 )
{
if ( v6 == 1 )
{
printf("[%d] Doing random scans\n", v3);
v1 = &dword_400000;
}
}
else
{
printf("[%d] Doing random reads\n", v3);
LODWORD(v1) = 0x10000;
}
set_priority(*((_DWORD *)a1 + 4));
gettimeofday(&tv, 0LL);
v7 = tv.tv_sec;
v18 = tv.tv_usec;
v8 = *((_DWORD *)a1 + 2);
v9 = posix_memalign(&memptr, 0x1000uLL, (int)v1);
if ( v9 )
{
if ( v9 < 0 )
{
printf("ERROR with posix_memalign: %d\n", (unsigned int)v9);
return;
}
}
else
{
v2 = (char *)memptr;
}
v22 = v18 + 1000000 * v7;
v19 = v22 + 1000000 * v8;
fstat(v5, &stat_buf);
v20 = stat_buf.st_size / (int)v1;
if ( v22 >= v19 )
{
LODWORD(v13) = v18 + 1000000 * v7;
LODWORD(v18) = 0;
v21 = 0;
}
else
{
v18 = 0LL;
v21 = 0;
do
{
gettimeofday(&v23, 0LL);
v10 = random();
lseek(v5, (int)v1 * (v10 % (int)v20), 0);
v11 = 0;
do
{
v12 = read(v5, &v2[v11], (int)v1 - v11);
if ( v12 < 0 )
{
perror("read");
return;
}
v11 += v12;
}
while ( (int)v1 > v11 );
gettimeofday(&v24, 0LL);
v13 = v24.tv_usec + 1000000 * v24.tv_sec;
v14 = v13 - (v23.tv_usec + 1000000 * v23.tv_sec);
if ( v18 >= v14 )
v14 = v18;
v18 = v14;
++v21;
}
while ( v19 > v13 );
}
v15 = (double)(int)v1 * (double)v21 * 0.0009765625 / ((double)((int)v13 - (int)v22) / 1000.0 / 1000.0);
v16 = *((_DWORD *)a1 + 4);
v17 = gettid();
fprintf(log, "[%d] Priority: %d, Max latency: %.2f ms, Tput: %.2f KiB/s\n", v17, v16, (double)(int)v18 / 1000.0, v15);
free(v2);
close(v5);
}

canonical_decompiled_code:
void __fastcall func9(void *a1)
{
int *v1; // rbp
char *v2; // r13
unsigned int v3; // ebx
int v4; // eax
int v5; // er12
int v6; // eax
__time_t v7; // r15
int v8; // ebx
int v9; // eax
__int64 v10; // rax
int v11; // ebx
int v12; // eax
__suseconds_t v13; // rax
__suseconds_t v14; // rcx
double v15; // rbx
unsigned int v16; // ebp
unsigned int v17; // eax
__suseconds_t v18; // [rsp+8h] [rbp-130h]
__suseconds_t v19; // [rsp+10h] [rbp-128h]
__off_t v20; // [rsp+18h] [rbp-120h]
int v21; // [rsp+24h] [rbp-114h]
__suseconds_t v22; // [rsp+28h] [rbp-110h]
struct timeval v23; // [rsp+30h] [rbp-108h] BYREF
struct timeval v24; // [rsp+40h] [rbp-F8h] BYREF
struct stat stat_buf; // [rsp+50h] [rbp-E8h] BYREF
struct timeval tv; // [rsp+E0h] [rbp-58h] BYREF
void *memptr; // [rsp+F8h] [rbp-40h] BYREF

v3 = func7();
v4 = open(*(const char **)a1, 0x4000);
if ( v4 == -1 )
{
perror("Reader open");
return;
}
v5 = v4;
printf("[%d] Reader opened file %s\n", v3, *(const char **)a1);
v6 = *((_DWORD *)a1 + 3);
if ( v6 )
{
if ( v6 == 1 )
{
printf("[%d] Doing random scans\n", v3);
v1 = &dword_400000;
}
}
else
{
printf("[%d] Doing random reads\n", v3);
LODWORD(v1) = 0x10000;
}
func8(*((_DWORD *)a1 + 4));
gettimeofday(&tv, 0LL);
v7 = tv.tv_sec;
v18 = tv.tv_usec;
v8 = *((_DWORD *)a1 + 2);
v9 = posix_memalign(&memptr, 0x1000uLL, (int)v1);
if ( v9 )
{
if ( v9 < 0 )
{
printf("ERROR with posix_memalign: %d\n", (unsigned int)v9);
return;
}
}
else
{
v2 = (char *)memptr;
}
v22 = v18 + 1000000 * v7;
v19 = v22 + 1000000 * v8;
func12(v5, &stat_buf);
v20 = stat_buf.st_size / (int)v1;
if ( v22 >= v19 )
{
LODWORD(v13) = v18 + 1000000 * v7;
LODWORD(v18) = 0;
v21 = 0;
}
else
{
v18 = 0LL;
v21 = 0;
do
{
gettimeofday(&v23, 0LL);
v10 = random();
lseek(v5, (int)v1 * (v10 % (int)v20), 0);
v11 = 0;
do
{
v12 = read(v5, &v2[v11], (int)v1 - v11);
if ( v12 < 0 )
{
perror("read");
return;
}
v11 += v12;
}
while ( (int)v1 > v11 );
gettimeofday(&v24, 0LL);
v13 = v24.tv_usec + 1000000 * v24.tv_sec;
v14 = v13 - (v23.tv_usec + 1000000 * v23.tv_sec);
if ( v18 >= v14 )
v14 = v18;
v18 = v14;
++v21;
}
while ( v19 > v13 );
}
v15 = (double)(int)v1 * (double)v21 * 0.0009765625 / ((double)((int)v13 - (int)v22) / 1000.0 / 1000.0);
v16 = *((_DWORD *)a1 + 4);
v17 = func7();
fprintf(log, "[%d] Priority: %d, Max latency: %.2f ms, Tput: %.2f KiB/s\n", v17, v16, (double)(int)v18 / 1000.0, v15);
free(v2);
close(v5);
}

original_code:
void* reader(void *arg) {
  param_t* params = (param_t*) arg;
  int ret;
  int tid = gettid();
  int fd = open(params->filename, 00|040000);
  if(fd == -1) {
    perror("Reader open");
    return;
  }
  else {
    printf("[%d] Reader opened file %s\n", tid, params->filename);
  }
  int bufsize;
  switch(params->type) {
    case SEEK:
      printf("[%d] Doing random reads\n", tid);
      bufsize = 64*1024;
      break;
    case SCAN:
      printf("[%d] Doing random scans\n", tid);
      bufsize = 4*1024*1024;
      break;
  }
  set_priority(params->priority);
  long start, now, end;
  struct timeval t;
  gettimeofday(&t, ((void *)0));
  start = (1000000*t.tv_sec) + t.tv_usec;
  end = start + (1000000 * params->duration);
  now = start;
  char * buffer;
  ret = posix_memalign((void**)&buffer, 4096, bufsize);
  if(ret < 0) {
    printf("ERROR with posix_memalign: %d\n", ret);
    return;
  }
  int count = 0;
  struct stat st;
  fstat(fd, &st);
  off_t size = st.st_size;
  int blocks = size / bufsize;
  long max_latency = 0;
  while(now < end) {
    struct timeval t1, t2;
    gettimeofday(&t1, ((void *)0));
    long int seek_off = (random()%blocks)*bufsize;
    lseek(fd, seek_off, 0);
    int total = 0;
    while(total < bufsize) {
      int num_bytes = read(fd, buffer+total, bufsize-total);
      if(num_bytes < 0) {
        perror("read");
        return;
      }
      total += num_bytes;
    }
    gettimeofday(&t2, ((void *)0));
    long t1_usec = (1000000 * t1.tv_sec) + t1.tv_usec;
    long t2_usec = (1000000 * t2.tv_sec) + t2.tv_usec;
    long latency = t2_usec - t1_usec;
    if(latency > max_latency) {
        max_latency = latency;
    }
    now = t2_usec;
    count++;
  }
  long finished = now;
  double duration = (double)(finished - start) / 1000.0 / 1000.0;
  double tput = ((double)count * bufsize) / 1024.0 / duration;
  fprintf(log, "[%d] Priority: %d, Max latency: %.2f ms, Tput: %.2f KiB/s\n",
          gettid(), params->priority, (double)max_latency/1000.0, tput);
  free(buffer);
  close(fd);
}

canonical_original_code:
void *reader(void *arg) {
  struct param_t *params = (param_t*) arg;
  int ret;
  int tid = gettid();
  int fd = open(params->filename, 00|040000);
  if(fd == -1) {
    perror("Reader open");
    return;
  }
  else {
    printf("[%d] Reader opened file %s\n", tid, params->filename);
  }
  int bufsize;
  switch(params->type) {
    case SEEK:
      printf("[%d] Doing random reads\n", tid);
      bufsize = 64*1024;
      break;
    case SCAN:
      printf("[%d] Doing random scans\n", tid);
      bufsize = 4*1024*1024;
      break;
  }
  set_priority(params->priority);
  long start; long now; long end;
  struct timeval t;
  gettimeofday(&t, ((void *)0));
  start = (1000000*t.tv_sec) + t.tv_usec;
  end = start + (1000000 * params->duration);
  now = start;
  char *buffer;
  ret = posix_memalign((void **)&buffer, 4096, bufsize);
  if(ret < 0) {
    printf("ERROR with posix_memalign: %d\n", ret);
    return;
  }
  int count = 0;
  struct stat st;
  fstat(fd, &st);
  long size = st.st_size;
  int blocks = size / bufsize;
  long max_latency = 0;
  while(now < end) {
    struct timeval t1; struct timeval t2;
    gettimeofday(&t1, ((void *)0));
    long seek_off = (random()%blocks)*bufsize;
    lseek(fd, seek_off, 0);
    int total = 0;
    while(total < bufsize) {
      int num_bytes = read(fd, buffer+total, bufsize-total);
      if(num_bytes < 0) {
        perror("read");
        return;
      }
      total += num_bytes;
    }
    gettimeofday(&t2, ((void *)0));
    long t1_usec = (1000000 * t1.tv_sec) + t1.tv_usec;
    long t2_usec = (1000000 * t2.tv_sec) + t2.tv_usec;
    long latency = t2_usec - t1_usec;
    if(latency > max_latency) {
        max_latency = latency;
    }
    now = t2_usec;
    count++;
  }
  long finished = now;
  double duration = (double)(finished - start) / 1000.0 / 1000.0;
  double tput = ((double)count * bufsize) / 1024.0 / duration;
  fprintf(log, "[%d] Priority: %d, Max latency: %.2f ms, Tput: %.2f KiB/s\n",
          gettid(), params->priority, (double)max_latency/1000.0, tput);
  free(buffer);
  close(fd);
}

variable_types:
arg
void *

variable_types: struct param_t { char *filename; int duration; int type; int priority; } *

variable_types: bufsize int

variable_types: start long

variable_types: now long

variable_types: end long

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

variable_types:
buffer
char *

variable_types: count int

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

variable_types: size long

variable_types: blocks int

variable_types: total int

variable_types: latency long

variable_types: finished long

variable_types: duration double

variable_types: tput double

return_type: void *

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
int v4; // eax
const char *v5; // r15
char *v6; // rbx
pthread_attr_t *v7; // r14
__int64 i; // r13
pthread_t *v9; // r12
int result; // eax
__int64 v11; // [rsp-1C0h] [rbp-300h] BYREF
__int64 *v12; // [rsp+0h] [rbp-140h] BYREF
int v13; // [rsp+Ch] [rbp-134h]
pthread_t v14[8]; // [rsp+10h] [rbp-130h] BYREF
char arg[240]; // [rsp+50h] [rbp-F0h] BYREF

v3 = 0LL;
v13 = 60;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "d:o:p");
if ( v4 == -1 )
break;
if ( v4 == 100 )
{
v13 = strtol(optarg, 0LL, 10);
}
else if ( v4 == 111 )
{
v3 = optarg;
}
}
if ( argc - optind == 2 )
{
v5 = argv[optind];
if ( v3 )
{
log = fopen(v3, "w");
if ( log )
{
printf("Using %s for logging\n", v3);
}
else
{
perror("Cannot open log file");
log = (FILE *)stderr;
}
}
else
{
log = (FILE *)stderr;
puts("Defaulting to stderr for logging");
}
v12 = (__int64 *)&v12;
v6 = arg;
v7 = (pthread_attr_t *)&v11;
for ( i = 0LL; i != 8; ++i )
{
*((_DWORD *)v6 + 2) = v13;
*(_QWORD *)v6 = v5;
*((_DWORD *)v6 + 3) = 0;
*((_DWORD *)v6 + 4) = i;
pthread_attr_init(v7);
v9 = v14;
pthread_create(&v14[i], v7, (void *(*)(void *))reader, v6);
v6 += 24;
++v7;
}
do
pthread_join(*v9++, 0LL);
while ( v9 != (pthread_t *)arg );
result = 0;
}
else
{
puts("Usage: ionicer [-d duration] [-o outfile] [-p] scan_file seek_file");
result = 1;
}
return result;
}

original_code:
int main(int argc, char *argv[]) {
  int ret;
  int c;
  int duration = 60;
  int seek_priority = 0;
  char* logfile = ((void *)0);
 while ((c = getopt(argc, argv, "d:o:p")) != (-1)) {
  switch (c) {
  case 'd':
    duration = strtol(optarg, ((void *)0), 10);
    break;
  case 'o':
    logfile = optarg;
    break;
  case 'p':
   seek_priority = 1;
   break;
  }
 }
 char *f1, *f2;
 if(argc - optind == 2) {
   f1 = argv[optind];
   f2 = argv[optind+1];
 }
 else {
   printf("Usage: ionicer [-d duration] [-o outfile] [-p] scan_file seek_file\n");
   return 1;
 }
 if(logfile == ((void *)0)) {
   log = stderr;
    printf("Defaulting to stderr for logging\n");
 } else {
   log = fopen(logfile, "w");
   if(log == ((void *)0)) {
     perror("Cannot open log file");
     log = stderr;
   }
   else {
      printf("Using %s for logging\n", logfile);
   }
 }
  int num_threads = 8;
  pthread_t threads[num_threads];
  pthread_attr_t attrs[num_threads];
  param_t params[num_threads];
  int i;
  for(i=0; i<num_threads; i++) {
    params[i].duration = duration;
    params[i].filename = f1;
    params[i].type = SEEK;
    params[i].priority = i;
    pthread_attr_init(&attrs[i]);
    ret = pthread_create(&threads[i], &attrs[i], reader, (void*)&params[i]);
  }
  for(i=0; i<num_threads; i++) {
    pthread_join(threads[i], ((void *)0));
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int ret;
  int c;
  int duration = 60;
  int seek_priority = 0;
  char *logfile = ((void *)0);
 while ((c = getopt(argc, argv, "d:o:p")) != (-1)) {
  switch (c) {
  case 'd':
    duration = strtol(optarg, ((void *)0), 10);
    break;
  case 'o':
    logfile = optarg;
    break;
  case 'p':
   seek_priority = 1;
   break;
  }
 }
 char *f1; char *f2;
 if(argc - optind == 2) {
   f1 = argv[optind];
   f2 = argv[optind+1];
 }
 else {
   printf("Usage: ionicer [-d duration] [-o outfile] [-p] scan_file seek_file\n");
   return 1;
 }
 if(logfile == ((void *)0)) {
   log = stderr;
    printf("Defaulting to stderr for logging\n");
 } else {
   log = fopen(logfile, "w");
   if(log == ((void *)0)) {
     perror("Cannot open log file");
     log = stderr;
   }
   else {
      printf("Using %s for logging\n", logfile);
   }
 }
  int num_threads = 8;
  unsigned long threads[-1];
  union pthread_attr_t attrs[-1];
  struct param_t params[-1];
  int i;
  for(i=0; i<num_threads; i++) {
    params[i].duration = duration;
    params[i].filename = f1;
    params[i].type = SEEK;
    params[i].priority = i;
    pthread_attr_init(&attrs[i]);
    ret = pthread_create(&threads[i], &attrs[i], reader, (void *)&params[i]);
  }
  for(i=0; i<num_threads; i++) {
    pthread_join(threads[i], ((void *)0));
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

variable_types: c int

variable_types: duration int

variable_types:
logfile
char *

variable_types:
threads
unsigned long [-1]

variable_types: union pthread_attr_t { char __size[56]; long __align; } [-1]

variable_types: struct param_t { char *filename; int duration; int type; int priority; } [-1]

variable_types: i int


die

repo: vlaadbrain/xkev

decompiled_code:
void __noreturn die(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func7(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
void
die(const char *errstr, ...) {
 va_list ap;
 __builtin_va_start(ap,errstr);
 vfprintf(stderr, errstr, ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void die(char *errstr, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,errstr);
 vfprintf(stderr, errstr, ap);
 __builtin_va_end(ap);
 exit(1);
}

variable_types:
errstr
char *


usage

decompiled_code:
void __noreturn usage()
{
char *v0; // rax

v0 = __xpg_basename(argv0);
die("usage: %s [-vsScCmMaAuUhH] [-e <key>]\n", v0);
}

canonical_decompiled_code:
void __noreturn func8()
{
char *v0; // rax

v0 = __xpg_basename(argv0);
func7("usage: %s [-vsScCmMaAuUhH] [-e <key>]\n", v0);
}

original_code:
void
usage(void) {
 die("usage: %s [-vsScCmMaAuUhH] [-e <key>]\n", __xpg_basename(argv0));
}

canonical_original_code:
void usage() {
 die("usage: %s [-vsScCmMaAuUhH] [-e <key>]\n", __xpg_basename(argv0));
}


delay

repo: wpwrak/idbg

decompiled_code:
int delay()
{
return fprintf(_bss_start, "SDA %d SCL %d\n", (unsigned int)I2C_SDA, (unsigned int)I2C_SCL);
}

canonical_decompiled_code:
int func7()
{
return fprintf(_bss_start, "SDA %d SCL %d\n", (unsigned int)I2C_SDA, (unsigned int)I2C_SCL);
}

original_code:
static void delay(void)
{
 fprintf(stderr, "SDA %d SCL %d\n", I2C_SDA, I2C_SCL);
}

canonical_original_code:
static void delay()
{
 fprintf(stderr, "SDA %d SCL %d\n", I2C_SDA, I2C_SCL);
}


decompiled_code:
void __fastcall i2c_start(char a1, __int64 a2, unsigned __int8 a3, __int64 a4, unsigned __int8 a5)
{
wpos = a2;
wbuf = a2;
wend = a3 + a2;
rpos = a4;
rbuf = a4;
rend = a5 + a4;
fetched = 0;
spos = a1;
state = 2;
}

canonical_decompiled_code:
void __fastcall func9(char a1, __int64 a2, unsigned __int8 a3, __int64 a4, unsigned __int8 a5)
{
wpos = a2;
wbuf = a2;
wend = a3 + a2;
rpos = a4;
rbuf = a4;
rend = a5 + a4;
fetched = 0;
spos = a1;
state = 2;
}

original_code:
void i2c_start(uint8_t s, const uint8_t *wdata, uint8_t wlen,
    uint8_t *rdata, uint8_t rlen)
{
 wbuf = wpos = wdata;
 wend = wbuf+wlen;
 rbuf = rpos = rdata;
 rend = rbuf+rlen;
 fetched = 0;
 spos = s;
 state = START;
}

canonical_original_code:
void i2c_start(unsigned char s, unsigned char *wdata, unsigned char wlen, unsigned char *rdata, unsigned char rlen)
{
 wbuf = wpos = wdata;
 wend = wbuf+wlen;
 rbuf = rpos = rdata;
 rend = rbuf+rlen;
 fetched = 0;
 spos = s;
 state = START;
}

variable_types:
s
unsigned char

variable_types:
wdata
unsigned char *

variable_types:
wlen
unsigned char

variable_types:
rdata
unsigned char *

variable_types:
rlen
unsigned char


decompiled_code:
int i2c_poll()
{
int result; // eax
int i; // ebx
int v2; // ebx
int v3; // ebp
_BYTE *v4; // rdx
__int64 v5; // rax

result = state;
if ( state == 3 )
{
if ( spos && wpos - wbuf == (unsigned __int8)spos )
{
I2C_SDA = 1;
delay();
I2C_SCL = 1;
result = delay();
if ( !I2C_SCL )
return result;
I2C_SDA = 0;
spos = 0;
delay();
I2C_SCL = 0;
delay();
}
I2C_SDA = *(_BYTE *)wpos >> 7;
delay();
I2C_SCL = 1;
delay();
I2C_SCL = 0;
for ( i = 6; i != -1; --i )
{
I2C_SDA = ((int)*(unsigned __int8 *)wpos >> i) & 1;
delay();
I2C_SCL = 1;
delay();
I2C_SCL = 0;
}
++wpos;
I2C_SDA = 1;
delay();
I2C_SCL = 1;
delay();
I2C_SCL = 0;
I2C_SDA = 0;
delay();
result = wend;
if ( wpos == wend )
{
result = rend;
if ( rpos == rend )
{
state = 0;
I2C_SCL = 1;
delay();
I2C_SDA = 1;
result = delay();
}
else
{
state = 4;
}
}
}
else if ( state == 4 )
{
I2C_SDA = 1;
v2 = 7;
v3 = 0;
do
{
delay();
I2C_SCL = 1;
delay();
v3 |= I2C_SDA << v2;
I2C_SCL = 0;
--v2;
}
while ( v2 != -1 );
v4 = (_BYTE *)rpos++;
v5 = rpos;
*v4 = v3;
if ( v5 != rend )
I2C_SDA = 0;
delay();
I2C_SCL = 1;
delay();
I2C_SCL = 0;
result = rend;
if ( rpos == rend )
{
delay();
I2C_SDA = 0;
delay();
I2C_SCL = 1;
delay();
I2C_SDA = 1;
result = delay();
state = 0;
}
}
else if ( state == 2 && I2C_SDA )
{
if ( I2C_SCL )
{
I2C_SDA = 0;
delay();
I2C_SCL = 0;
state = 3;
result = delay();
}
}
return result;
}

canonical_decompiled_code:
int func12()
{
int result; // eax
int i; // ebx
int v2; // ebx
int v3; // ebp
_BYTE *v4; // rdx
__int64 v5; // rax

result = state;
if ( state == 3 )
{
if ( spos && wpos - wbuf == (unsigned __int8)spos )
{
I2C_SDA = 1;
func7();
I2C_SCL = 1;
result = func7();
if ( !I2C_SCL )
return result;
I2C_SDA = 0;
spos = 0;
func7();
I2C_SCL = 0;
func7();
}
I2C_SDA = *(_BYTE *)wpos >> 7;
func7();
I2C_SCL = 1;
func7();
I2C_SCL = 0;
for ( i = 6; i != -1; --i )
{
I2C_SDA = ((int)*(unsigned __int8 *)wpos >> i) & 1;
func7();
I2C_SCL = 1;
func7();
I2C_SCL = 0;
}
++wpos;
I2C_SDA = 1;
func7();
I2C_SCL = 1;
func7();
I2C_SCL = 0;
I2C_SDA = 0;
func7();
result = wend;
if ( wpos == wend )
{
result = rend;
if ( rpos == rend )
{
state = 0;
I2C_SCL = 1;
func7();
I2C_SDA = 1;
result = func7();
}
else
{
state = 4;
}
}
}
else if ( state == 4 )
{
I2C_SDA = 1;
v2 = 7;
v3 = 0;
do
{
func7();
I2C_SCL = 1;
func7();
v3 |= I2C_SDA << v2;
I2C_SCL = 0;
--v2;
}
while ( v2 != -1 );
v4 = (_BYTE *)rpos++;
v5 = rpos;
*v4 = v3;
if ( v5 != rend )
I2C_SDA = 0;
func7();
I2C_SCL = 1;
func7();
I2C_SCL = 0;
result = rend;
if ( rpos == rend )
{
func7();
I2C_SDA = 0;
func7();
I2C_SCL = 1;
func7();
I2C_SDA = 1;
result = func7();
state = 0;
}
}
else if ( state == 2 && I2C_SDA )
{
if ( I2C_SCL )
{
I2C_SDA = 0;
func7();
I2C_SCL = 0;
state = 3;
result = func7();
}
}
return result;
}

original_code:
void i2c_poll(void)
{
 int8_t i;
 uint8_t data;
 int nak;
 switch (state) {
 case IDLE:
 case FAILED:
  break;
 case START:
  if (!I2C_SDA || !I2C_SCL)
   break;
  I2C_SDA = 0;
  delay();
  I2C_SCL = 0;
  state = SEND;
  delay();
  break;
 case SEND:
  if (spos && wpos-wbuf == spos) {
   I2C_SDA = 1;
   delay();
   I2C_SCL = 1;
   delay();
   if (!I2C_SCL)
    break;
   I2C_SDA = 0;
   spos = 0;
   delay();
   I2C_SCL = 0;
   delay();
  }
  sda = *wpos >> 7;
  I2C_SDA = sda;
  delay();
  I2C_SCL = 1;
  if (!I2C_SCL)
   break;
  delay();
  I2C_SCL = 0;
  for (i = 6; i != -1; i--) {
   sda = (*wpos >> i) & 1;
   I2C_SDA = sda;
   delay();
   I2C_SCL = 1;
   delay();
   I2C_SCL = 0;
  }
  wpos++;
  I2C_SDA = 1;
  delay();
  I2C_SCL = 1;
  delay();
  nak = I2C_SDA;
  I2C_SCL = 0;
  I2C_SDA = 0;
  delay();
  nak = 0;
  if (nak) {
   state = FAILED;
  } else {
   if (wpos != wend)
    break;
   if (rpos != rend) {
    state = RECV;
    break;
   }
   state = IDLE;
  }
  I2C_SCL = 1;
  delay();
  I2C_SDA = 1;
  delay();
  break;
 case RECV:
  I2C_SDA = 1;
  data = 0;
  for (i = 7; i != -1; i--) {
   delay();
   I2C_SCL = 1;
   delay();
   data |= I2C_SDA << i;
   I2C_SCL = 0;
  }
  *rpos++ = data;
  if (rpos != rend)
   I2C_SDA = 0;
  delay();
  I2C_SCL = 1;
  delay();
  I2C_SCL = 0;
  if (rpos != rend)
   break;
  delay();
  I2C_SDA = 0;
  delay();
  I2C_SCL = 1;
  delay();
  I2C_SDA = 1;
  delay();
  state = IDLE;
  break;
 default:
  break;
 }
}

canonical_original_code:
void i2c_poll()
{
 char i;
 unsigned char data;
 int nak;
 switch (state) {
 case IDLE:
 case FAILED:
  break;
 case START:
  if (!I2C_SDA || !I2C_SCL)
   break;
  I2C_SDA = 0;
  delay();
  I2C_SCL = 0;
  state = SEND;
  delay();
  break;
 case SEND:
  if (spos && wpos-wbuf == spos) {
   I2C_SDA = 1;
   delay();
   I2C_SCL = 1;
   delay();
   if (!I2C_SCL)
    break;
   I2C_SDA = 0;
   spos = 0;
   delay();
   I2C_SCL = 0;
   delay();
  }
  sda = *wpos >> 7;
  I2C_SDA = sda;
  delay();
  I2C_SCL = 1;
  if (!I2C_SCL)
   break;
  delay();
  I2C_SCL = 0;
  for (i = 6; i != -1; i--) {
   sda = (*wpos >> i) & 1;
   I2C_SDA = sda;
   delay();
   I2C_SCL = 1;
   delay();
   I2C_SCL = 0;
  }
  wpos++;
  I2C_SDA = 1;
  delay();
  I2C_SCL = 1;
  delay();
  nak = I2C_SDA;
  I2C_SCL = 0;
  I2C_SDA = 0;
  delay();
  nak = 0;
  if (nak) {
   state = FAILED;
  } else {
   if (wpos != wend)
    break;
   if (rpos != rend) {
    state = RECV;
    break;
   }
   state = IDLE;
  }
  I2C_SCL = 1;
  delay();
  I2C_SDA = 1;
  delay();
  break;
 case RECV:
  I2C_SDA = 1;
  data = 0;
  for (i = 7; i != -1; i--) {
   delay();
   I2C_SCL = 1;
   delay();
   data |= I2C_SDA << i;
   I2C_SCL = 0;
  }
  *rpos++ = data;
  if (rpos != rend)
   I2C_SDA = 0;
  delay();
  I2C_SCL = 1;
  delay();
  I2C_SCL = 0;
  if (rpos != rend)
   break;
  delay();
  I2C_SDA = 0;
  delay();
  I2C_SCL = 1;
  delay();
  I2C_SDA = 1;
  delay();
  state = IDLE;
  break;
 default:
  break;
 }
}

variable_types: i char

variable_types:
data
unsigned char

variable_types: nak int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
I2C_SCL = 1;
I2C_SDA = 1;
delay();
i2c_write(115, 0, "A", 1);
polln(200);
delay();
delay();
delay();
delay();
fprintf(_bss_start, "state %d\n", (unsigned int)state);
i2c_fetch(0LL, 0xAu);
i2c_fetch(0LL, 0xAu);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
I2C_SCL = 1;
I2C_SDA = 1;
func7();
func10(115, 0, "A", 1);
func13(200);
func7();
func7();
func7();
func7();
fprintf(_bss_start, "state %d\n", (unsigned int)state);
func11(0LL, 0xAu);
func11(0LL, 0xAu);
return 0;
}

original_code:
int main(void)
{
 int i;
 uint8_t tmp;
 I2C_SDA = I2C_SCL = 1;
 delay();
 i2c_write(0x73, 0, "A", 1);
 polln(200);
 delay();
 delay();
 delay();
 delay();
 fprintf(stderr, "state %d\n", state);
 i2c_fetch(((void *)0), 10);
 i2c_fetch(((void *)0), 10);
 return 0;
}

canonical_original_code:
int main()
{
 int i;
 unsigned char tmp;
 I2C_SDA = I2C_SCL = 1;
 delay();
 i2c_write(0x73, 0, "A", 1);
 polln(200);
 delay();
 delay();
 delay();
 delay();
 fprintf(stderr, "state %d\n", state);
 i2c_fetch(((void *)0), 10);
 i2c_fetch(((void *)0), 10);
 return 0;
}

variable_types:
tmp
unsigned char


decompiled_code:
__int64 __fastcall set_pin(__int64 a1, const char *a2)
{
int v2; // edx
char v3; // cl
__int64 result; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

translate_pin(a1, v6, &v5);
v2 = v6[0];
v3 = v5;
mask[v6[0]] |= v5;
result = *(unsigned __int8 *)a2;
if ( !(_BYTE)result || a2[1] )
{
fprintf(stderr, "invalid pin state \"%s\"\n", a2);
exit(1);
}
if ( (_BYTE)result == 49 )
{
result = v2;
mode[v2] |= v3;
LABEL_11:
data[v2] |= v3;
return result;
}
if ( (char)result > 49 )
{
result = (unsigned int)result & 0xFFFFFFDF;
if ( (_BYTE)result != 82 )
goto LABEL_6;
goto LABEL_11;
}
if ( (_BYTE)result != 48 )
{
LABEL_6:
fprintf(stderr, "invalid pin state \"%s\"\n", a2);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
int v2; // edx
char v3; // cl
__int64 result; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

func0(a1, v6, &v5);
v2 = v6[0];
v3 = v5;
mask[v6[0]] |= v5;
result = *(unsigned __int8 *)a2;
if ( !(_BYTE)result || a2[1] )
{
fprintf(stderr, "invalid pin state \"%s\"\n", a2);
exit(1);
}
if ( (_BYTE)result == 49 )
{
result = v2;
mode[v2] |= v3;
LABEL_11:
data[v2] |= v3;
return result;
}
if ( (char)result > 49 )
{
result = (unsigned int)result & 0xFFFFFFDF;
if ( (_BYTE)result != 82 )
goto LABEL_6;
goto LABEL_11;
}
if ( (_BYTE)result != 48 )
{
LABEL_6:
fprintf(stderr, "invalid pin state \"%s\"\n", a2);
exit(1);
}
return result;
}

original_code:
static void set_pin(const struct pin *pin, const char *value)
{
 int port, bit;
 translate_pin(pin, &port, &bit);
 mask[port] |= bit;
 if (!*value || value[1]) {
  fprintf(stderr, "invalid pin state \"%s\"\n", value);
  exit(1);
 }
 switch (*value) {
 case '1':
  mode[port] |= bit;
 case 'r':
 case 'R':
  data[port] |= bit;
  break;
 case '0':
  break;
 default:
  fprintf(stderr, "invalid pin state \"%s\"\n", value);
  exit(1);
 }
}

canonical_original_code:
static void set_pin(struct pin *pin, char *value)
{
 int port; int bit;
 translate_pin(pin, &port, &bit);
 mask[port] |= bit;
 if (!*value || value[1]) {
  fprintf(stderr, "invalid pin state \"%s\"\n", value);
  exit(1);
 }
 switch (*value) {
 case '1':
  mode[port] |= bit;
 case 'r':
 case 'R':
  data[port] |= bit;
  break;
 case '0':
  break;
 default:
  fprintf(stderr, "invalid pin state \"%s\"\n", value);
  exit(1);
 }
}

variable_types: struct pin { int pin; int port; char *host_name; char *idbg_name; } *

variable_types:
value
char *

variable_types: port int

variable_types: bit int


decompiled_code:
int __fastcall print_names(unsigned int *a1)
{
const char *v1; // rsi
const char *v2; // rsi

printf("%2d: P%d_%d (", *a1, a1[1], a1[2]);
v1 = (const char *)*((_QWORD *)a1 + 2);
if ( v1 )
{
printf("%s", v1);
if ( *((_QWORD *)a1 + 2) )
{
if ( !*((_QWORD *)a1 + 3) )
return putchar(41);
printf(", ");
}
}
v2 = (const char *)*((_QWORD *)a1 + 3);
if ( v2 )
printf("%s", v2);
return putchar(41);
}

canonical_decompiled_code:
int __fastcall func3(unsigned int *a1)
{
const char *v1; // rsi
const char *v2; // rsi

printf("%2d: P%d_%d (", *a1, a1[1], a1[2]);
v1 = (const char *)*((_QWORD *)a1 + 2);
if ( v1 )
{
printf("%s", v1);
if ( *((_QWORD *)a1 + 2) )
{
if ( !*((_QWORD *)a1 + 3) )
return putchar(41);
printf(", ");
}
}
v2 = (const char *)*((_QWORD *)a1 + 3);
if ( v2 )
printf("%s", v2);
return putchar(41);
}

original_code:
static void print_names(const struct pin *pin)
{
 printf("%2d: P%d_%d (", pin->pin, pin->port, pin->bit);
 if (pin->host_name)
  printf("%s", pin->host_name);
 if (pin->host_name && pin->idbg_name)
  printf(", ");
 if (pin->idbg_name)
  printf("%s", pin->idbg_name);
 printf(")");
}

canonical_original_code:
static void print_names(struct pin *pin)
{
 printf("%2d: P%d_%d (", pin->pin, pin->port, pin->bit);
 if (pin->host_name)
  printf("%s", pin->host_name);
 if (pin->host_name && pin->idbg_name)
  printf(", ");
 if (pin->idbg_name)
  printf("%s", pin->idbg_name);
 printf(")");
}

user_defined_types: struct pin { int pin; int port; char *host_name; char *idbg_name; }


decompiled_code:
int __fastcall dump_pin_table(const char *a1, unsigned int *a2)
{
unsigned int *v2; // rbx
int result; // eax

v2 = a2;
for ( result = puts(a1); *v2; v2 += 8 )
{
printf(" ");
print_names(v2);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(const char *a1, unsigned int *a2)
{
unsigned int *v2; // rbx
int result; // eax

v2 = a2;
for ( result = puts(a1); *v2; v2 += 8 )
{
printf(" ");
func3(v2);
result = putchar(10);
}
return result;
}

original_code:
static void dump_pin_table(const char *device, const struct pin *pin_table)
{
 const struct pin *pin;
 printf("%s\n", device);
 for (pin = pin_table; pin->pin; pin++) {
  printf("  ");
  print_names(pin);
  putchar('\n');
 }
}

canonical_original_code:
static void dump_pin_table(char *device, struct pin *pin_table)
{
 struct pin *pin;
 printf("%s\n", device);
 for (pin = pin_table; pin->pin; pin++) {
  printf("  ");
  print_names(pin);
  putchar('\n');
 }
}

variable_types:
device
char *


decompiled_code:
int list_pins()
{
dump_pin_table("GTA01/GTA02", gta_pins);
dump_pin_table("Ben NanoNote, IDBG V1", ben_v1_pins);
return dump_pin_table("Ben NanoNote, IDBG V2", ben_v2_pins);
}

canonical_decompiled_code:
int func5()
{
func4("GTA01/GTA02", gta_pins);
func4("Ben NanoNote, IDBG V1", ben_v1_pins);
return func4("Ben NanoNote, IDBG V2", ben_v2_pins);
}

original_code:
static void list_pins(void)
{
 dump_pin_table("GTA01/GTA02", gta_pins);
 dump_pin_table("Ben NanoNote, IDBG V1", ben_v1_pins);
 dump_pin_table("Ben NanoNote, IDBG V2", ben_v2_pins);
}

canonical_original_code:
static void list_pins()
{
 dump_pin_table("GTA01/GTA02", gta_pins);
 dump_pin_table("Ben NanoNote, IDBG V1", ben_v1_pins);
 dump_pin_table("Ben NanoNote, IDBG V2", ben_v2_pins);
}


decompiled_code:
int __fastcall get_pin(unsigned int *a1)
{
__int64 v1; // rsi
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

translate_pin((__int64)a1, v4, &v3);
print_names(a1);
v1 = 48LL;
if ( ((unsigned __int8)v3 & (unsigned __int8)data[v4[0]]) != 0 )
v1 = (unsigned __int8)(mode[v4[0]] & v3) == 0 ? 82 : 49;
return printf(" = %c\n", v1);
}

canonical_decompiled_code:
int __fastcall func6(unsigned int *a1)
{
__int64 v1; // rsi
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

func0((__int64)a1, v4, &v3);
func3(a1);
v1 = 48LL;
if ( ((unsigned __int8)v3 & (unsigned __int8)data[v4[0]]) != 0 )
v1 = (unsigned __int8)(mode[v4[0]] & v3) == 0 ? 82 : 49;
return printf(" = %c\n", v1);
}

original_code:
static void get_pin(const struct pin *pin)
{
 int port, bit;
 translate_pin(pin, &port, &bit);
 print_names(pin);
 printf(" = %c\n",
     data[port] & bit ? mode[port] & bit ? '1' : 'R' : '0');
}

canonical_original_code:
static void get_pin(struct pin *pin)
{
 int port; int bit;
 translate_pin(pin, &port, &bit);
 print_names(pin);
 printf(" = %c\n",
     data[port] & bit ? mode[port] & bit ? '1' : 'R' : '0');
}


decompiled_code:
int dump_all()
{
unsigned int *i; // rbx
int result; // eax

for ( i = (unsigned int *)pins; *i; i += 8 )
result = get_pin(i);
return result;
}

canonical_decompiled_code:
int func7()
{
unsigned int *i; // rbx
int result; // eax

for ( i = (unsigned int *)pins; *i; i += 8 )
result = func6(i);
return result;
}

original_code:
static void dump_all(void)
{
 const struct pin *pin;
 for (pin = pins; pin->pin; pin++)
  get_pin(pin);
}

canonical_original_code:
static void dump_all()
{
 struct pin *pin;
 for (pin = pins; pin->pin; pin++)
  get_pin(pin);
}


decompiled_code:
__int64 __fastcall lookup_pin(char *s2)
{
__int64 v2; // rbx
unsigned int v3; // er12
const char *v4; // rdi
const char *v5; // rdi
char s[29]; // [rsp+Bh] [rbp-1Dh] BYREF

v2 = pins;
v3 = *(_DWORD *)pins;
if ( !*(_DWORD *)pins )
return 0LL;
while ( 1 )
{
v5 = *(const char **)(v2 + 16);
if ( v5 )
{
if ( !strcasecmp(v5, s2) )
break;
}
v4 = *(const char **)(v2 + 24);
if ( v4 )
{
if ( !strcasecmp(v4, s2) )
break;
}
sprintf(s, "%d", v3);
if ( !strcmp(s, s2) )
break;
sprintf(s, "P%d_%d", *(unsigned int *)(v2 + 4), *(unsigned int *)(v2 + 8));
if ( !strcasecmp(s, s2) )
break;
v2 += 32LL;
v3 = *(_DWORD *)v2;
if ( !*(_DWORD *)v2 )
return 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s2)
{
__int64 v2; // rbx
unsigned int v3; // er12
const char *v4; // rdi
const char *v5; // rdi
char s[29]; // [rsp+Bh] [rbp-1Dh] BYREF

v2 = pins;
v3 = *(_DWORD *)pins;
if ( !*(_DWORD *)pins )
return 0LL;
while ( 1 )
{
v5 = *(const char **)(v2 + 16);
if ( v5 )
{
if ( !strcasecmp(v5, s2) )
break;
}
v4 = *(const char **)(v2 + 24);
if ( v4 )
{
if ( !strcasecmp(v4, s2) )
break;
}
sprintf(s, "%d", v3);
if ( !strcmp(s, s2) )
break;
sprintf(s, "P%d_%d", *(unsigned int *)(v2 + 4), *(unsigned int *)(v2 + 8));
if ( !strcasecmp(s, s2) )
break;
v2 += 32LL;
v3 = *(_DWORD *)v2;
if ( !*(_DWORD *)v2 )
return 0LL;
}
return v2;
}

original_code:
static const struct pin *lookup_pin(const char *name)
{
 const struct pin *pin;
 char tmp[5];
 for (pin = pins; pin->pin; pin++) {
  if (pin->host_name && !strcasecmp(pin->host_name, name))
   return pin;
  if (pin->idbg_name && !strcasecmp(pin->idbg_name, name))
   return pin;
  sprintf(tmp, "%d", pin->pin);
  if (!strcmp(tmp, name))
   return pin;
  sprintf(tmp, "P%d_%d", pin->port, pin->bit);
  if (!strcasecmp(tmp, name))
   return pin;
 }
 return ((void *)0);
}

canonical_original_code:
static struct pin *lookup_pin(char *name)
{
 struct pin *pin;
 char tmp[5];
 for (pin = pins; pin->pin; pin++) {
  if (pin->host_name && !strcasecmp(pin->host_name, name))
   return pin;
  if (pin->idbg_name && !strcasecmp(pin->idbg_name, name))
   return pin;
  sprintf(tmp, "%d", pin->pin);
  if (!strcmp(tmp, name))
   return pin;
  sprintf(tmp, "P%d_%d", pin->port, pin->bit);
  if (!strcasecmp(tmp, name))
   return pin;
 }
 return ((void *)0);
}

variable_types:
name
char *

variable_types:
tmp
char [5]

return_type: struct pin { int pin; int port; char *host_name; char *idbg_name; } *


decompiled_code:
void __fastcall parse_set(const char **a1, int a2)
{
const char **v2; // rbp
const char **v3; // r13
const char *v4; // r12
char *v5; // rax
char *v6; // rbx
signed __int64 v7; // r14
__int64 v8; // rdi
char v9[152]; // [rsp+0h] [rbp-98h] BYREF

if ( a2 )
{
v2 = a1;
v3 = &a1[a2];
do
{
v4 = *v2;
v5 = strchr(*v2, 61);
v6 = v5;
if ( v5 )
{
v7 = v5 - v4;
memcpy(v9, v4, v5 - v4);
v9[v7] = 0;
v8 = lookup_pin(v9);
if ( !v8 )
{
fprintf(stderr, "unrecognized pin \"%s\"\n", v9);
exit(1);
}
set_pin(v8, v6 + 1);
}
else
{
have_get = 1;
}
++v2;
}
while ( v2 != v3 );
}
}

canonical_decompiled_code:
void __fastcall func9(const char **a1, int a2)
{
const char **v2; // rbp
const char **v3; // r13
const char *v4; // r12
char *v5; // rax
char *v6; // rbx
signed __int64 v7; // r14
__int64 v8; // rdi
char v9[152]; // [rsp+0h] [rbp-98h] BYREF

if ( a2 )
{
v2 = a1;
v3 = &a1[a2];
do
{
v4 = *v2;
v5 = strchr(*v2, 61);
v6 = v5;
if ( v5 )
{
v7 = v5 - v4;
memcpy(v9, v4, v5 - v4);
v9[v7] = 0;
v8 = func8(v9);
if ( !v8 )
{
fprintf(stderr, "unrecognized pin \"%s\"\n", v9);
exit(1);
}
func2(v8, v6 + 1);
}
else
{
have_get = 1;
}
++v2;
}
while ( v2 != v3 );
}
}

original_code:
static void parse_set(const char **args, int num_args)
{
 const struct pin *pin;
 const char *eq;
 char tmp[100];
 while (num_args--) {
  eq = strchr(*args, '=');
  if (!eq) {
   have_get = 1;
  } else {
   memcpy(tmp, *args, eq-*args);
   tmp[eq-*args] = 0;
   pin = lookup_pin(tmp);
   if (!pin) {
    fprintf(stderr, "unrecognized pin \"%s\"\n",
        tmp);
    exit(1);
   }
   set_pin(pin, eq+1);
  }
  args++;
 }
}

canonical_original_code:
static void parse_set(char **args, int num_args)
{
 struct pin *pin;
 char *eq;
 char tmp[100];
 while (num_args--) {
  eq = strchr(*args, '=');
  if (!eq) {
   have_get = 1;
  } else {
   memcpy(tmp, *args, eq-*args);
   tmp[eq-*args] = 0;
   pin = lookup_pin(tmp);
   if (!pin) {
    fprintf(stderr, "unrecognized pin \"%s\"\n",
        tmp);
    exit(1);
   }
   set_pin(pin, eq+1);
  }
  args++;
 }
}

variable_types:
args
char **

variable_types:
eq
char *

variable_types:
tmp
char [100]


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

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

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

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

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

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


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);
rxing = 0LL;
result = (unsigned __int8)rx_pos;
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);
rxing = 0LL;
result = (unsigned __int8)rx_pos;
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()
{
void *v0; // rbx
__int64 v1; // rax

v0 = (void *)tx_uart_q;
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;
v1 = dequeue(&tx_uart_q);
enqueue(&tx_usb_q, v1);
if ( (void *)tx_usb_q == v0 )
_usb_recv("usb_recv: &ep1out", v0, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, (__int64)v0);
}
}
else
{
txing = 0;
}
}

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

v0 = (void *)tx_uart_q;
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;
v1 = func8(&tx_uart_q);
func7(&tx_usb_q, v1);
if ( (void *)tx_usb_q == v0 )
func20("usb_recv: &ep1out", v0, 2u, (__int64 (__fastcall *)(_QWORD))got_tx, (__int64)v0);
}
}
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 i; // ebx

for ( i = a1; i; --i )
{
tx();
tx();
tx_done();
}
}

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

for ( i = a1; i; --i )
{
func23();
func23();
func14();
}
}

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

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

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


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 )
bad_id(a1);
if ( v1 > 0xFFFF )
bad_id(a1);
vendor = v1;
result = strtoul(endptr[0] + 1, endptr, 16);
if ( *endptr[0] )
bad_id(a1);
if ( 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 )
func10(a1);
if ( v1 > 0xFFFF )
func10(a1);
vendor = v1;
result = strtoul(endptr[0] + 1, endptr, 16);
if ( *endptr[0] )
func10(a1);
if ( 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 )
func11(a1);
if ( v1 > 0xFFFF )
func11(a1);
vendor = v1;
result = strtoul(endptr[0] + 1, endptr, 16);
if ( *endptr[0] )
func11(a1);
if ( 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 func11(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 func13(__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


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 *[]


cleanup

decompiled_code:
void cleanup()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void cleanup(void)
{
}

canonical_original_code:
void cleanup()
{
}


alphaBeta

decompiled_code:
__int64 __fastcall alphaBeta(int a1, unsigned int a2, int a3, unsigned __int8 a4)
{
unsigned __int8 v4; // bl
unsigned int v6; // eax
__int64 result; // rax
int v8; // ebp
__int64 v9; // rbx
unsigned int v10; // er12
unsigned int v11; // er14
int v12; // eax
int v13; // [rsp+Ch] [rbp-8ACh]
__int64 v14; // [rsp+10h] [rbp-8A8h]
int v16[512]; // [rsp+20h] [rbp-898h] BYREF
char v17[152]; // [rsp+820h] [rbp-98h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned int a2, int a3, unsigned __int8 a4)
{
unsigned __int8 v4; // bl
unsigned int v6; // eax
__int64 result; // rax
int v8; // ebp
__int64 v9; // rbx
unsigned int v10; // er12
unsigned int v11; // er14
int v12; // eax
int v13; // [rsp+Ch] [rbp-8ACh]
__int64 v14; // [rsp+10h] [rbp-8A8h]
int v16[512]; // [rsp+20h] [rbp-898h] BYREF
char v17[152]; // [rsp+820h] [rbp-98h] BYREF

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

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; // ebx

currentDepth = 4;
startTimer();
alphaBeta(currentDepth, 0x80000001, 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; // ebx

currentDepth = 4;
startTimer();
func0(currentDepth, 0x80000001, 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 }