decompiled_code:
__int64 __fastcall virtgyro_cb(unsigned int *a1)
{
unsigned int v1; // eax

v1 = _bswap_32(0x4759524Fu);
return common_cb(v1, *a1);
}

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

v1 = func0(0x4759524Fu);
return func1(v1, *a1);
}

original_code:
static int virtgyro_cb (struct pollfd *pfd) {
 return common_cb (htonl((('G') << 24) | (('Y') << 16) | (('R') << 8) | ('O')), pfd->fd);
}

canonical_original_code:
static int virtgyro_cb(struct pollfd *pfd) {
 return common_cb (htonl((('G') << 24) | (('Y') << 16) | (('R') << 8) | ('O')), pfd->fd);
}

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


repo: elderberry-capstone/elderberry

decompiled_code:
__int64 __fastcall virtacc_cb(unsigned int *a1)
{
unsigned int v1; // eax

v1 = _bswap_32(0x4143434Fu);
return common_cb(v1, *a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1)
{
unsigned int v1; // eax

v1 = func0(0x4143434Fu);
return func1(v1, *a1);
}

original_code:
static int virtacc_cb (struct pollfd *pfd) {
 return common_cb (htonl((('A') << 24) | (('C') << 16) | (('C') << 8) | ('O')), pfd->fd);
}

canonical_original_code:
static int virtacc_cb(struct pollfd *pfd) {
 return common_cb (htonl((('A') << 24) | (('C') << 16) | (('C') << 8) | ('O')), pfd->fd);
}

return_type: int

user_defined_types: struct pollfd { int fd; short events; short revents; }


initvirtdev

decompiled_code:
__int64 __fastcall initvirtdev(const char *a1, unsigned int a2, __int64 a3)
{
unsigned int v4; // eax

printf("probing %s: (waiting for connection localhost:%d)\n", a1, a2);
v4 = getsocket(a2);
return fcf_addfd(v4, 1LL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, unsigned int a2, __int64 a3)
{
unsigned int v4; // eax

printf("probing %s: (waiting for connection localhost:%d)\n", a1, a2);
v4 = getsocket(a2);
return fcf_addfd(v4, 1LL, a3);
}

original_code:
static int initvirtdev (const char* devname, int port, pollCallback cb) {
 printf ("probing %s: (waiting for connection localhost:%d)\n", devname, port);
 int fd = getsocket(port);
 int rc = fcf_addfd (fd, 0x001, cb);
 return rc;
}

canonical_original_code:
static int initvirtdev(char *devname, int port, int (*cb)(struct pollfd *pfd)) {
 printf ("probing %s: (waiting for connection localhost:%d)\n", devname, port);
 int fd = getsocket(port);
 int rc = fcf_addfd (fd, 0x001, cb);
 return rc;
}

variable_types:
devname
char *

variable_types: port int

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

variable_types: fd int

variable_types: rc int


decompiled_code:
__int64 init_virtgyro()
{
return initvirtdev("virt gyro", 0x1F91u, (__int64)virtgyro_cb);
}

canonical_decompiled_code:
__int64 func5()
{
return func4("virt gyro", 0x1F91u, (__int64)virtgyro_cb);
}

original_code:
int init_virtgyro() {
 return initvirtdev ("virt gyro", 8081, virtgyro_cb);
}


decompiled_code:
__int64 init_virtacc()
{
return initvirtdev("virt acc", 0x1F92u, (__int64)virtacc_cb);
}

canonical_decompiled_code:
__int64 func6()
{
return func4("virt acc", 0x1F92u, (__int64)virtacc_cb);
}

original_code:
int init_virtacc() {
 return initvirtdev ("virt acc", 8082, virtacc_cb);
}


signalhandler

decompiled_code:
void __fastcall signalhandler(unsigned int a1)
{
printf("\n **signal handler: signum = %d", a1);
if ( a1 == 2 )
stop_main_loop();
}

canonical_decompiled_code:
void __fastcall func1(unsigned int a1)
{
printf("\n **signal handler: signum = %d", a1);
if ( a1 == 2 )
stop_main_loop();
}

original_code:
static void signalhandler(int signum) {
    printf ("\n **signal handler: signum = %d", signum);
    if (signum == 2) {
        stop_main_loop ();
    }
}

variable_types: signum int

return_type: void


handleErrorPoll

decompiled_code:
__int64 handleErrorPoll()
{
int v0; // ebx
char *v1; // rbp
unsigned int v2; // eax

v0 = *_errno_location();
v1 = strerror(v0);
printf("\n poll error handling: errno=%d %s ", (unsigned int)v0, v1);
if ( v0 == 14 )
{
printf("EFAULT");
}
else if ( v0 > 14 )
{
if ( v0 == 22 )
printf("EINVAL");
}
else if ( v0 == 4 )
{
printf("EINTR");
}
else if ( v0 == 12 )
{
printf("ENOMEM");
}
v2 = _bswap_32(0x4C4F4753u);
return printf_tagged_message(v2, "\n poll returned with errno=%d %s", (unsigned int)v0, v1);
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // ebx
char *v1; // rbp
unsigned int v2; // eax

v0 = *_errno_location();
v1 = strerror(v0);
printf("\n poll error handling: errno=%d %s ", (unsigned int)v0, v1);
if ( v0 == 14 )
{
printf("EFAULT");
}
else if ( v0 > 14 )
{
if ( v0 == 22 )
printf("EINVAL");
}
else if ( v0 == 4 )
{
printf("EINTR");
}
else if ( v0 == 12 )
{
printf("ENOMEM");
}
v2 = func0(0x4C4F4753u);
return printf_tagged_message(v2, "\n poll returned with errno=%d %s", (unsigned int)v0, v1);
}

original_code:
void handleErrorPoll(void) {
    int err = (*__errno_location ());
    char *msg = strerror(err);
    printf ("\n poll error handling: errno=%d %s ", err, msg);
    switch(err) {
        case 14: printf ("EFAULT"); break;
        case 4: printf ("EINTR"); break;
        case 22: printf ("EINVAL"); break;
        case 12: printf ("ENOMEM"); break;
    }
    printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "\n poll returned with errno=%d %s", err, msg);
}

canonical_original_code:
void handleErrorPoll() {
    int err = (*__errno_location ());
    char *msg = strerror(err);
    printf ("\n poll error handling: errno=%d %s ", err, msg);
    switch(err) {
        case 14: printf ("EFAULT"); break;
        case 4: printf ("EINTR"); break;
        case 22: printf ("EINVAL"); break;
        case 12: printf ("ENOMEM"); break;
    }
    printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "\n poll returned with errno=%d %s", err, msg);
}

variable_types: err int

variable_types:
msg
char *


decompiled_code:
void start_main_loop()
{
run_fc = 1;
}

canonical_decompiled_code:
void func0()
{
run_fc = 1;
}

original_code:
static void start_main_loop() {
 run_fc = 1;
}


decompiled_code:
int __fastcall debug_fd(const char *a1, unsigned int a2, unsigned int *a3)
{
__int16 v4; // bx

printf(
"%s fd[%d]: fd=%d events=%X revents=%X",
a1,
a2,
*a3,
(unsigned int)*((__int16 *)a3 + 2),
(unsigned int)*((__int16 *)a3 + 3));
v4 = *((_WORD *)a3 + 3);
if ( (v4 & 8) != 0 )
printf("\nPOLLERR - Error condition");
if ( (v4 & 0x10) != 0 )
printf("\nPOLLHUP - Hang up");
if ( (v4 & 0x20) != 0 )
printf("\nPOLLNVAL - Invalid request: fd not open");
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, unsigned int a2, unsigned int *a3)
{
__int16 v4; // bx

printf(
"%s fd[%d]: fd=%d events=%X revents=%X",
a1,
a2,
*a3,
(unsigned int)*((__int16 *)a3 + 2),
(unsigned int)*((__int16 *)a3 + 3));
v4 = *((_WORD *)a3 + 3);
if ( (v4 & 8) != 0 )
printf("\nPOLLERR - Error condition");
if ( (v4 & 0x10) != 0 )
printf("\nPOLLHUP - Hang up");
if ( (v4 & 0x20) != 0 )
printf("\nPOLLNVAL - Invalid request: fd not open");
return fflush(stdout);
}

original_code:
static void debug_fd (const char *msg, int i, struct pollfd *pfd) {
 printf("%s fd[%d]: fd=%d events=%X revents=%X", msg, i, pfd->fd, pfd->events, pfd->revents);
 int re = pfd->revents;
 if (re & 0x008) printf("\nPOLLERR - Error condition");
 if (re & 0x010) printf("\nPOLLHUP - Hang up");
 if (re & 0x020) printf("\nPOLLNVAL - Invalid request: fd not open");
 fflush (stdout);
}

canonical_original_code:
static void debug_fd(char *msg, int i, struct pollfd *pfd) {
 printf("%s fd[%d]: fd=%d events=%X revents=%X", msg, i, pfd->fd, pfd->events, pfd->revents);
 int re = pfd->revents;
 if (re & 0x008) printf("\nPOLLERR - Error condition");
 if (re & 0x010) printf("\nPOLLHUP - Hang up");
 if (re & 0x020) printf("\nPOLLNVAL - Invalid request: fd not open");
 fflush (stdout);
}

variable_types: i int

variable_types: re int


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

result = (unsigned int)nfds;
if ( nfds > 99 )
return 0xFFFFFFFFLL;
++nfds;
*(&fds.fd + 2 * (unsigned int)result) = a1;
*(&fds.events + 4 * (unsigned int)result) = a2;
callbacks[(unsigned int)result] = a3;
cbCat[(unsigned int)result] = 0;
return result;
}

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

result = (unsigned int)nfds;
if ( nfds > 99 )
return 0xFFFFFFFFLL;
++nfds;
*(&fds.fd + 2 * (unsigned int)result) = a1;
*(&fds.events + 4 * (unsigned int)result) = a2;
callbacks[(unsigned int)result] = a3;
cbCat[(unsigned int)result] = 0;
return result;
}

original_code:
int fcf_addfd (int fd, short events, pollCallback cb) {
 if (nfds >= 100) {
  return -1;
 }
 unsigned int i = nfds++;
 fds[i].fd = fd;
 fds[i].events = events;
 callbacks[i] = cb;
 cbCat[i] = standard;
 return i;
}

canonical_original_code:
int fcf_addfd(int fd, short events, int (*cb)(struct pollfd *pfd)) {
 if (nfds >= 100) {
  return -1;
 }
 unsigned int i = nfds++;
 fds[i].fd = fd;
 fds[i].events = events;
 callbacks[i] = cb;
 cbCat[i] = standard;
 return i;
}

variable_types: events short

variable_types:
i
unsigned int


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

result = fcf_addfd(a1, a2, a3);
if ( (int)result >= 0 )
cbCat[(int)result] = 1;
return result;
}

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

result = func2(a1, a2, a3);
if ( (int)result >= 0 )
cbCat[(int)result] = 1;
return result;
}

original_code:
int fcf_addfd_ppc (int fd, short events, pollCallback cb)
{
 int i = fcf_addfd (fd, events, cb);
 if (i >= 0) {
  cbCat[i] = ppc;
 }
 return i;
}

canonical_original_code:
int fcf_addfd_ppc(int fd, short events, int (*cb)(struct pollfd *pfd))
{
 int i = fcf_addfd (fd, events, cb);
 if (i >= 0) {
  cbCat[i] = ppc;
 }
 return i;
}


decompiled_code:
__int64 __fastcall fcf_removefd(int a1)
{
__int64 v1; // rax
int v2; // edx
__int64 result; // rax
int v4; // esi

if ( nfds <= 0 )
return 0xFFFFFFFFLL;
v1 = 0LL;
while ( 1 )
{
v2 = v1;
if ( *(&fds.fd + 2 * v1) == a1 )
break;
if ( ++v1 == nfds )
return 0xFFFFFFFFLL;
}
v4 = nfds - 1;
nfds = v4;
result = 0LL;
if ( v4 > v2 )
{
*(&fds + v2) = *(&fds + v4);
callbacks[v2] = callbacks[v4];
cbCat[v2] = cbCat[v4];
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
__int64 v1; // rax
int v2; // edx
__int64 result; // rax
int v4; // esi

if ( nfds <= 0 )
return 0xFFFFFFFFLL;
v1 = 0LL;
while ( 1 )
{
v2 = v1;
if ( *(&fds.fd + 2 * v1) == a1 )
break;
if ( ++v1 == nfds )
return 0xFFFFFFFFLL;
}
v4 = nfds - 1;
nfds = v4;
result = 0LL;
if ( v4 > v2 )
{
*(&fds + v2) = *(&fds + v4);
callbacks[v2] = callbacks[v4];
cbCat[v2] = cbCat[v4];
result = 0LL;
}
return result;
}

original_code:
int fcf_removefd (int fd) {
 for (int i = 0; i < nfds; i++) {
  if (fds[i].fd == fd) {
   nfds--;
   if (i < nfds) {
    fds[i] = fds[nfds];
    callbacks[i] = callbacks[nfds];
    cbCat[i] = cbCat[nfds];
   }
   return 0;
  }
 }
 return -1;
}

canonical_original_code:
int fcf_removefd(int fd) {
 for (int i = 0; i < nfds; i++) {
  if (fds[i].fd == fd) {
   nfds--;
   if (i < nfds) {
    fds[i] = fds[nfds];
    callbacks[i] = callbacks[nfds];
    cbCat[i] = cbCat[nfds];
   }
   return 0;
  }
 }
 return -1;
}


decompiled_code:
void stop_main_loop()
{
run_fc = 0;
}

canonical_decompiled_code:
void func5()
{
run_fc = 0;
}

original_code:
void stop_main_loop() {
 run_fc = 0;
}

canonical_original_code:
void stop_main_loop() {
 run_fc = 0;
}


decompiled_code:
__int64 run_main_loop()
{
unsigned int i; // ebx
struct pollfd *v1; // r12
signed int j; // ebp
unsigned int *v3; // r13
__int64 v4; // r12
unsigned int v5; // er15
struct pollfd *v6; // rbp
signed int v7; // ebx
__int64 v8; // rbx
int v9; // er13
__int64 *v10; // rdx
int v11; // eax
int v12; // ebp
__int64 v13; // rbx
__int64 v14; // rax
unsigned int v16; // [rsp+Ch] [rbp-35Ch]
__int64 v17[107]; // [rsp+10h] [rbp-358h] BYREF

start_main_loop();
v16 = run_fc;
if ( run_fc )
{
v16 = 0;
for ( i = 0; ; i = v5 )
{
v1 = &fds;
for ( j = 0; nfds > j; ++v1 )
debug_fd("\nbefore poll<<< ", j++, (unsigned int *)v1);
printf("\nwaiting");
fflush(stdout);
v3 = (unsigned int *)_errno_location();
*v3 = 0;
v4 = (unsigned int)poll(&fds, nfds, -1);
v5 = i + 1;
printf("\n%d. poll returned with rc=%d errno=%d", i, v4, *v3);
if ( nfds > 0 )
{
v6 = &fds;
v7 = 0;
do
debug_fd("\n after poll>>> ", v7++, (unsigned int *)v6++);
while ( nfds > v7 );
}
if ( (_DWORD)v4 == -1 )
{
if ( *v3 != 4 )
{
perror("run_main_loop: poll returned with error");
stop_main_loop();
v16 = -1;
}
}
else if ( (_DWORD)v4 )
{
if ( nfds > 0 )
{
v8 = 0LL;
v9 = 0;
if ( (int)v4 > 0 )
{
do
{
v12 = v8;
if ( *(&fds.revents + 4 * v8) )
{
debug_fd("\n active fd ", v8, (unsigned int *)&fds + 2 * v8);
LODWORD(v4) = v4 - 1;
if ( cbCat[v8] )
{
if ( v9 <= 0 )
{
v11 = 0;
}
else
{
v10 = v17;
v11 = 0;
while ( *v10 != callbacks[v8] )
{
++v11;
++v10;
if ( v11 == v9 )
goto LABEL_22;
}
}
if ( v9 == v11 )
LABEL_22:
v17[v9++] = callbacks[v8];
else
printf(
"\n multiple active ppc, ignoring callback for fd[%d]: fd=%d",
(unsigned int)v8,
*((unsigned int *)&fds.fd + 2 * v8));
}
else
{
((void (__fastcall *)(struct pollfd *))callbacks[v8])(&fds + v8);
}
}
++v8;
}
while ( nfds > v12 + 1 && (int)v4 > 0 );
if ( v9 > 0 )
{
v13 = 0LL;
do
{
printf("\n calling ppc callback [%d]", (unsigned int)v13);
((void (__fastcall *)(struct pollfd *))v17[v13])(&fds);
v14 = v13++;
}
while ( v9 - 1 != v14 );
}
}
}
}
else
{
printf("poll timed out");
}
if ( !run_fc )
break;
}
}
printf("\n exiting main loop");
return v16;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int i; // ebx
struct pollfd *v1; // r12
signed int j; // ebp
unsigned int *v3; // r13
__int64 v4; // r12
unsigned int v5; // er15
struct pollfd *v6; // rbp
signed int v7; // ebx
__int64 v8; // rbx
int v9; // er13
__int64 *v10; // rdx
int v11; // eax
int v12; // ebp
__int64 v13; // rbx
__int64 v14; // rax
unsigned int v16; // [rsp+Ch] [rbp-35Ch]
__int64 v17[107]; // [rsp+10h] [rbp-358h] BYREF

func0();
v16 = run_fc;
if ( run_fc )
{
v16 = 0;
for ( i = 0; ; i = v5 )
{
v1 = &fds;
for ( j = 0; nfds > j; ++v1 )
func1("\nbefore poll<<< ", j++, (unsigned int *)v1);
printf("\nwaiting");
fflush(stdout);
v3 = (unsigned int *)_errno_location();
*v3 = 0;
v4 = (unsigned int)poll(&fds, nfds, -1);
v5 = i + 1;
printf("\n%d. poll returned with rc=%d errno=%d", i, v4, *v3);
if ( nfds > 0 )
{
v6 = &fds;
v7 = 0;
do
func1("\n after poll>>> ", v7++, (unsigned int *)v6++);
while ( nfds > v7 );
}
if ( (_DWORD)v4 == -1 )
{
if ( *v3 != 4 )
{
perror("run_main_loop: poll returned with error");
func5();
v16 = -1;
}
}
else if ( (_DWORD)v4 )
{
if ( nfds > 0 )
{
v8 = 0LL;
v9 = 0;
if ( (int)v4 > 0 )
{
do
{
v12 = v8;
if ( *(&fds.revents + 4 * v8) )
{
func1("\n active fd ", v8, (unsigned int *)&fds + 2 * v8);
LODWORD(v4) = v4 - 1;
if ( cbCat[v8] )
{
if ( v9 <= 0 )
{
v11 = 0;
}
else
{
v10 = v17;
v11 = 0;
while ( *v10 != callbacks[v8] )
{
++v11;
++v10;
if ( v11 == v9 )
goto LABEL_22;
}
}
if ( v9 == v11 )
LABEL_22:
v17[v9++] = callbacks[v8];
else
printf(
"\n multiple active ppc, ignoring callback for fd[%d]: fd=%d",
(unsigned int)v8,
*((unsigned int *)&fds.fd + 2 * v8));
}
else
{
((void (__fastcall *)(struct pollfd *))callbacks[v8])(&fds + v8);
}
}
++v8;
}
while ( nfds > v12 + 1 && (int)v4 > 0 );
if ( v9 > 0 )
{
v13 = 0LL;
do
{
printf("\n calling ppc callback [%d]", (unsigned int)v13);
((void (__fastcall *)(struct pollfd *))v17[v13])(&fds);
v14 = v13++;
}
while ( v9 - 1 != v14 );
}
}
}
}
else
{
printf("poll timed out");
}
if ( !run_fc )
break;
}
}
printf("\n exiting main loop");
return v16;
}

original_code:
int run_main_loop() {
 pollCallback ppc[100];
 int nppc= 0;
 int count = 0;
 int ret = 0;
 start_main_loop();
 while (run_fc) {
  for (int i = 0; i < nfds; i++) {
   debug_fd("\nbefore poll<<< ", i, &fds[i]);
  }
  printf("\nwaiting");
  fflush (stdout);
  (*__errno_location ()) = 0;
  int rc = poll(fds, nfds, -1);
  printf ("\n%d. poll returned with rc=%d errno=%d", count++, rc, (*__errno_location ()));
  for (int i = 0; i < nfds; i++) {
   debug_fd("\n   after poll>>> ", i, &fds[i]);
  }
  switch (rc) {
  case -1:
   if ((*__errno_location ()) != 4) {
    perror ("run_main_loop: poll returned with error");
    ret = -1;
    stop_main_loop();
   }
   break;
  case 0:
   printf("poll timed out");
   break;
  default:
   nppc = 0;
   for (int i = 0; i < nfds && rc > 0; i++) {
    if(fds[i].revents != 0) {
     debug_fd("\n active fd ", i, &fds[i]);
     rc--;
     if (cbCat[i] == standard) {
      callbacks[i](&fds[i]);
     } else {
      int j;
      for (j = 0; j < nppc && ppc[j] != callbacks[i]; j++)
      { }
      if (j == nppc) {
       ppc[nppc++] = callbacks[i];
      } else {
       printf ("\n multiple active ppc, ignoring callback for fd[%d]: fd=%d", i, fds[i].fd);
      }
     }
    }
   }
   for (int j = 0; j < nppc; j++) {
    printf ("\n calling ppc callback [%d]", j);
    ppc[j](fds);
   }
   break;
  }
 }
 printf("\n exiting main loop");
 return ret;
}

canonical_original_code:
int run_main_loop() {
 int (*ppc[100])(struct pollfd *pfd);
 int nppc = 0;
 int count = 0;
 int ret = 0;
 start_main_loop();
 while (run_fc) {
  for (int i = 0; i < nfds; i++) {
   debug_fd("\nbefore poll<<< ", i, &fds[i]);
  }
  printf("\nwaiting");
  fflush (stdout);
  (*__errno_location ()) = 0;
  int rc = poll(fds, nfds, -1);
  printf ("\n%d. poll returned with rc=%d errno=%d", count++, rc, (*__errno_location ()));
  for (int i = 0; i < nfds; i++) {
   debug_fd("\n   after poll>>> ", i, &fds[i]);
  }
  switch (rc) {
  case -1:
   if ((*__errno_location ()) != 4) {
    perror ("run_main_loop: poll returned with error");
    ret = -1;
    stop_main_loop();
   }
   break;
  case 0:
   printf("poll timed out");
   break;
  default:
   nppc = 0;
   for (int i = 0; i < nfds && rc > 0; i++) {
    if(fds[i].revents != 0) {
     debug_fd("\n active fd ", i, &fds[i]);
     rc--;
     if (cbCat[i] == standard) {
      callbacks[i](&fds[i]);
     } else {
      int j;
      for (j = 0; j < nppc && ppc[j] != callbacks[i]; j++)
      { }
      if (j == nppc) {
       ppc[nppc++] = callbacks[i];
      } else {
       printf ("\n multiple active ppc, ignoring callback for fd[%d]: fd=%d", i, fds[i].fd);
      }
     }
    }
   }
   for (int j = 0; j < nppc; j++) {
    printf ("\n calling ppc callback [%d]", j);
    ppc[j](fds);
   }
   break;
  }
 }
 printf("\n exiting main loop");
 return ret;
}

variable_types: int (*[100])(struct pollfd { int fd; short events; short revents; } *pfd)

variable_types: nppc int

variable_types: count int

variable_types: ret int

variable_types: j int


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

switch ( a1 )
{
case 0xFFFFFF9D:
result = fprintf(stderr, "**%s: ERROR_OTHER\n", a2);
break;
case 0xFFFFFFF4:
result = fprintf(stderr, "**%s: ERROR_NOT_SUPPORTED\n", a2);
break;
case 0xFFFFFFF5:
result = fprintf(stderr, "**%s: ERROR_NO_MEM\n", a2);
break;
case 0xFFFFFFF6:
result = fprintf(stderr, "**%s: ERROR_INTERRUPTED\n", a2);
break;
case 0xFFFFFFF7:
result = fprintf(stderr, "**%s: ERROR_PIPE\n", a2);
break;
case 0xFFFFFFF8:
result = fprintf(stderr, "**%s: ERROR_OVERFLOW\n", a2);
break;
case 0xFFFFFFF9:
result = fprintf(stderr, "**%s: ERROR_TIMEOUT\n", a2);
break;
case 0xFFFFFFFA:
result = fprintf(stderr, "**%s: ERROR_BUSY\n", a2);
break;
case 0xFFFFFFFB:
result = fprintf(stderr, "**%s: ERROR_NOT_FOUND\n", a2);
break;
case 0xFFFFFFFC:
result = fprintf(stderr, "**%s: ERROR_NO_DEVICE\n", a2);
break;
case 0xFFFFFFFD:
result = fprintf(stderr, "**%s: ERROR_ACCESS\n", a2);
break;
case 0xFFFFFFFE:
result = fprintf(stderr, "**%s: ERROR_INVALID_PARAM\n", a2);
break;
case 0xFFFFFFFF:
result = fprintf(stderr, "**%s: ERROR_IO\n", a2);
break;
case 0u:
result = fprintf(stderr, "**%s: SUCCESS\n", a2);
break;
default:
result = fprintf(stderr, "***%s: unknown error %i ***\n", a2, a1);
break;
}
return result;
}

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

switch ( a1 )
{
case 0xFFFFFF9D:
result = fprintf(stderr, "**%s: ERROR_OTHER\n", a2);
break;
case 0xFFFFFFF4:
result = fprintf(stderr, "**%s: ERROR_NOT_SUPPORTED\n", a2);
break;
case 0xFFFFFFF5:
result = fprintf(stderr, "**%s: ERROR_NO_MEM\n", a2);
break;
case 0xFFFFFFF6:
result = fprintf(stderr, "**%s: ERROR_INTERRUPTED\n", a2);
break;
case 0xFFFFFFF7:
result = fprintf(stderr, "**%s: ERROR_PIPE\n", a2);
break;
case 0xFFFFFFF8:
result = fprintf(stderr, "**%s: ERROR_OVERFLOW\n", a2);
break;
case 0xFFFFFFF9:
result = fprintf(stderr, "**%s: ERROR_TIMEOUT\n", a2);
break;
case 0xFFFFFFFA:
result = fprintf(stderr, "**%s: ERROR_BUSY\n", a2);
break;
case 0xFFFFFFFB:
result = fprintf(stderr, "**%s: ERROR_NOT_FOUND\n", a2);
break;
case 0xFFFFFFFC:
result = fprintf(stderr, "**%s: ERROR_NO_DEVICE\n", a2);
break;
case 0xFFFFFFFD:
result = fprintf(stderr, "**%s: ERROR_ACCESS\n", a2);
break;
case 0xFFFFFFFE:
result = fprintf(stderr, "**%s: ERROR_INVALID_PARAM\n", a2);
break;
case 0xFFFFFFFF:
result = fprintf(stderr, "**%s: ERROR_IO\n", a2);
break;
case 0u:
result = fprintf(stderr, "**%s: SUCCESS\n", a2);
break;
default:
result = fprintf(stderr, "***%s: unknown error %i ***\n", a2, a1);
break;
}
return result;
}

original_code:
void print_libusb_error(int libusberrno, const char* str) {
 switch(libusberrno) {
 case LIBUSB_SUCCESS:
  fprintf(stderr, "**%s: SUCCESS\n",str);
  break;
 case LIBUSB_ERROR_IO:
  fprintf(stderr, "**%s: ERROR_IO\n",str);
  break;
 case LIBUSB_ERROR_INVALID_PARAM:
  fprintf(stderr, "**%s: ERROR_INVALID_PARAM\n",str);
  break;
 case LIBUSB_ERROR_ACCESS:
  fprintf(stderr, "**%s: ERROR_ACCESS\n",str);
  break;
 case LIBUSB_ERROR_NO_DEVICE:
  fprintf(stderr, "**%s: ERROR_NO_DEVICE\n",str);
  break;
 case LIBUSB_ERROR_NOT_FOUND:
  fprintf(stderr, "**%s: ERROR_NOT_FOUND\n",str);
  break;
 case LIBUSB_ERROR_BUSY:
    fprintf(stderr, "**%s: ERROR_BUSY\n",str);
    break;
 case LIBUSB_ERROR_TIMEOUT:
  fprintf(stderr, "**%s: ERROR_TIMEOUT\n",str);
  break;
 case LIBUSB_ERROR_OVERFLOW:
  fprintf(stderr, "**%s: ERROR_OVERFLOW\n",str);
  break;
 case LIBUSB_ERROR_PIPE:
  fprintf(stderr, "**%s: ERROR_PIPE\n",str);
  break;
 case LIBUSB_ERROR_INTERRUPTED:
  fprintf(stderr, "**%s: ERROR_INTERRUPTED\n",str);
  break;
 case LIBUSB_ERROR_NO_MEM:
  fprintf(stderr, "**%s: ERROR_NO_MEM\n",str);
  break;
 case LIBUSB_ERROR_NOT_SUPPORTED:
  fprintf(stderr, "**%s: ERROR_NOT_SUPPORTED\n",str);
  break;
 case LIBUSB_ERROR_OTHER:
  fprintf(stderr, "**%s: ERROR_OTHER\n",str);
  break;
 default:
  fprintf(stderr, "***%s:  unknown error %i ***\n", str, libusberrno);
  break;
 }
}

canonical_original_code:
void print_libusb_error(int libusberrno, char *str) {
 switch(libusberrno) {
 case LIBUSB_SUCCESS:
  fprintf(stderr, "**%s: SUCCESS\n",str);
  break;
 case LIBUSB_ERROR_IO:
  fprintf(stderr, "**%s: ERROR_IO\n",str);
  break;
 case LIBUSB_ERROR_INVALID_PARAM:
  fprintf(stderr, "**%s: ERROR_INVALID_PARAM\n",str);
  break;
 case LIBUSB_ERROR_ACCESS:
  fprintf(stderr, "**%s: ERROR_ACCESS\n",str);
  break;
 case LIBUSB_ERROR_NO_DEVICE:
  fprintf(stderr, "**%s: ERROR_NO_DEVICE\n",str);
  break;
 case LIBUSB_ERROR_NOT_FOUND:
  fprintf(stderr, "**%s: ERROR_NOT_FOUND\n",str);
  break;
 case LIBUSB_ERROR_BUSY:
    fprintf(stderr, "**%s: ERROR_BUSY\n",str);
    break;
 case LIBUSB_ERROR_TIMEOUT:
  fprintf(stderr, "**%s: ERROR_TIMEOUT\n",str);
  break;
 case LIBUSB_ERROR_OVERFLOW:
  fprintf(stderr, "**%s: ERROR_OVERFLOW\n",str);
  break;
 case LIBUSB_ERROR_PIPE:
  fprintf(stderr, "**%s: ERROR_PIPE\n",str);
  break;
 case LIBUSB_ERROR_INTERRUPTED:
  fprintf(stderr, "**%s: ERROR_INTERRUPTED\n",str);
  break;
 case LIBUSB_ERROR_NO_MEM:
  fprintf(stderr, "**%s: ERROR_NO_MEM\n",str);
  break;
 case LIBUSB_ERROR_NOT_SUPPORTED:
  fprintf(stderr, "**%s: ERROR_NOT_SUPPORTED\n",str);
  break;
 case LIBUSB_ERROR_OTHER:
  fprintf(stderr, "**%s: ERROR_OTHER\n",str);
  break;
 default:
  fprintf(stderr, "***%s:  unknown error %i ***\n", str, libusberrno);
  break;
 }
}

variable_types: libusberrno int

variable_types:
str
char *


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

switch ( a1 )
{
case 0u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_COMPLETED\n", a2);
break;
case 1u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_ERROR\n", a2);
break;
case 2u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_TIMED_OUT\n", a2);
break;
case 3u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_CANCELLED\n", a2);
break;
case 4u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_STALL\n", a2);
break;
case 5u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_NO_DEVICE\n", a2);
break;
case 6u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_OVERFLOW\n", a2);
break;
default:
result = fprintf(stderr, "***%s: Unknown transfer status %i***\n", a2, a1);
break;
}
return result;
}

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

switch ( a1 )
{
case 0u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_COMPLETED\n", a2);
break;
case 1u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_ERROR\n", a2);
break;
case 2u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_TIMED_OUT\n", a2);
break;
case 3u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_CANCELLED\n", a2);
break;
case 4u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_STALL\n", a2);
break;
case 5u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_NO_DEVICE\n", a2);
break;
case 6u:
result = fprintf(stderr, "**%s: LIBUSB_TRANSFER_OVERFLOW\n", a2);
break;
default:
result = fprintf(stderr, "***%s: Unknown transfer status %i***\n", a2, a1);
break;
}
return result;
}

original_code:
void print_libusb_transfer_error(int status, const char* str){
 switch(status){
 case LIBUSB_TRANSFER_COMPLETED:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_COMPLETED\n", str);
  break;
 case LIBUSB_TRANSFER_ERROR:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_ERROR\n", str);
  break;
 case LIBUSB_TRANSFER_TIMED_OUT:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_TIMED_OUT\n", str);
  break;
 case LIBUSB_TRANSFER_CANCELLED:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_CANCELLED\n", str);
  break;
 case LIBUSB_TRANSFER_STALL:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_STALL\n", str);
  break;
 case LIBUSB_TRANSFER_NO_DEVICE:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_NO_DEVICE\n", str);
  break;
 case LIBUSB_TRANSFER_OVERFLOW:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_OVERFLOW\n", str);
  break;
 default:
  fprintf(stderr, "***%s: Unknown transfer status %i***\n", str, status);
  break;
 }
}

canonical_original_code:
void print_libusb_transfer_error(int status, char *str){
 switch(status){
 case LIBUSB_TRANSFER_COMPLETED:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_COMPLETED\n", str);
  break;
 case LIBUSB_TRANSFER_ERROR:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_ERROR\n", str);
  break;
 case LIBUSB_TRANSFER_TIMED_OUT:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_TIMED_OUT\n", str);
  break;
 case LIBUSB_TRANSFER_CANCELLED:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_CANCELLED\n", str);
  break;
 case LIBUSB_TRANSFER_STALL:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_STALL\n", str);
  break;
 case LIBUSB_TRANSFER_NO_DEVICE:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_NO_DEVICE\n", str);
  break;
 case LIBUSB_TRANSFER_OVERFLOW:
  fprintf(stderr, "**%s: LIBUSB_TRANSFER_OVERFLOW\n", str);
  break;
 default:
  fprintf(stderr, "***%s: Unknown transfer status %i***\n", str, status);
  break;
 }
}

variable_types: status int


decompiled_code:
__int64 __fastcall usb_fd_removed_cb(unsigned int a1)
{
unsigned int v1; // eax

v1 = _bswap_32(0x4C4F4753u);
printf_tagged_message(v1, "\n usb_fd_removed_cb");
flush_buffers();
return fcf_removefd(a1);
}

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

v1 = func0(0x4C4F4753u);
printf_tagged_message(v1, "\n usb_fd_removed_cb");
flush_buffers();
return fcf_removefd(a1);
}

original_code:
static void usb_fd_removed_cb(int fd, void* source){
 printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "\n usb_fd_removed_cb");
 flush_buffers();
 fcf_removefd (fd);
}

canonical_original_code:
static void usb_fd_removed_cb(int fd, void *source){
 printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "\n usb_fd_removed_cb");
 flush_buffers();
 fcf_removefd (fd);
}

variable_types: fd int

variable_types:
source
void *


decompiled_code:
__int64 __fastcall usb_fd_added_cb(unsigned int a1, __int16 a2)
{
__int64 v3; // rdi

v3 = (unsigned int)_bswap_32(0x4C4F4753u);
printf_tagged_message(v3, "\n usb_fd_added_cb");
flush_buffers();
return fcf_addfd_ppc(a1, (unsigned int)a2, libusb_cb);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int16 a2)
{
__int64 v3; // rdi

v3 = (unsigned int)func0(0x4C4F4753u);
printf_tagged_message(v3, "\n usb_fd_added_cb");
flush_buffers();
return fcf_addfd_ppc(a1, (unsigned int)a2, libusb_cb);
}

original_code:
static void usb_fd_added_cb(int fd, short events, void * source){
 printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "\n usb_fd_added_cb");
 flush_buffers();
 fcf_addfd_ppc (fd, events, libusb_cb);
}

canonical_original_code:
static void usb_fd_added_cb(int fd, short events, void *source){
 printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "\n usb_fd_added_cb");
 flush_buffers();
 fcf_addfd_ppc (fd, events, libusb_cb);
}


decompiled_code:
__int64 libusb_cb()
{
libusb_handle_events_timeout(gcontext, &nonblocking);
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
libusb_handle_events_timeout(gcontext, &nonblocking);
return 0LL;
}

original_code:
static int libusb_cb(struct pollfd *pfd) {
 libusb_handle_events_timeout(gcontext, &nonblocking);
 return 0;
}

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


decompiled_code:
__syscall_slong_t get_timestamp()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

clock_gettime(1, &v1);
return v1.tv_nsec + 1000000000 * (v1.tv_sec - starttime.tv_sec) - starttime.tv_nsec;
}

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

clock_gettime(1, &v1);
return v1.tv_nsec + 1000000000 * (v1.tv_sec - starttime.tv_sec) - starttime.tv_nsec;
}

original_code:
static uint64_t get_timestamp(void)
{
 struct timespec now;
 clock_gettime(1, &now);
 now.tv_sec -= starttime.tv_sec;
 now.tv_nsec -= starttime.tv_nsec;
 return now.tv_nsec + (uint64_t) now.tv_sec * 1000000000;
}

canonical_original_code:
static unsigned long get_timestamp()
{
 struct timespec now;
 clock_gettime(1, &now);
 now.tv_sec -= starttime.tv_sec;
 now.tv_nsec -= starttime.tv_nsec;
 return now.tv_nsec + (unsigned long) now.tv_sec * 1000000000;
}

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

return_type: unsigned long


decompiled_code:
FILE *open_logfile()
{
char *v0; // rbp
unsigned int v1; // ebx
int v2; // eax
int v3; // edi
char *v4; // rax
FILE *result; // rax

mkdir("logs", 0x1FFu);
v0 = (char *)malloc(9uLL);
v1 = 0;
while ( 1 )
{
snprintf(v0, 9uLL, "%s/%03d", "logs", v1);
v2 = open(v0, 193, 292LL);
if ( v2 != -1 )
break;
v3 = *_errno_location();
if ( (v3 & 0xFFFFFFFB) != 17 )
{
v4 = strerror(v3);
printf("permanent failure creating logfile %s: %s\n", v0, v4);
exit(1);
}
if ( ++v1 == 1000 )
{
printf("tried %d filenames but couldn't create any logfile in %s\n", 1000LL, "logs");
exit(1);
}
}
result = fdopen(v2, "w");
logfile = result;
return result;
}

canonical_decompiled_code:
FILE *func1()
{
char *v0; // rbp
unsigned int v1; // ebx
int v2; // eax
int v3; // edi
char *v4; // rax
FILE *result; // rax

mkdir("logs", 0x1FFu);
v0 = (char *)malloc(9uLL);
v1 = 0;
while ( 1 )
{
snprintf(v0, 9uLL, "%s/%03d", "logs", v1);
v2 = open(v0, 193, 292LL);
if ( v2 != -1 )
break;
v3 = *_errno_location();
if ( (v3 & 0xFFFFFFFB) != 17 )
{
v4 = strerror(v3);
printf("permanent failure creating logfile %s: %s\n", v0, v4);
exit(1);
}
if ( ++v1 == 1000 )
{
printf("tried %d filenames but couldn't create any logfile in %s\n", 1000LL, "logs");
exit(1);
}
}
result = fdopen(v2, "w");
logfile = result;
return result;
}

original_code:
static void open_logfile(void)
{
 mkdir(logdir, 0777);
 const int attempt_count = 1e3;
 const int bufsize = strlen(logdir) + 3 + 2;
 char *buf = malloc(bufsize);
 int i;
 for(i = 0; i < attempt_count; ++i)
 {
  snprintf(buf, bufsize, "%s/%0" "3" "d", logdir, i);
  int fd = open(buf, 01 | 0100 | 0200, 0444);
  if(fd == -1)
  {
          if((*__errno_location ()) == 17 || (*__errno_location ()) == 21)
    continue;
   printf("permanent failure creating logfile %s: %s\n", buf, strerror((*__errno_location ())));
   exit(1);
  }
  logfile = fdopen(fd, "w");
  return;
 }
 printf("tried %d filenames but couldn't create any logfile in %s\n", i, logdir);
 exit(1);
}

canonical_original_code:
static void open_logfile()
{
 mkdir(logdir, 0777);
 int attempt_count = 1e3;
 int bufsize = strlen(logdir) + 3 + 2;
 char *buf = malloc(bufsize);
 int i;
 for(i = 0; i < attempt_count; ++i)
 {
  snprintf(buf, bufsize, "%s/%0" "3" "d", logdir, i);
  int fd = open(buf, 01 | 0100 | 0200, 0444);
  if(fd == -1)
  {
          if((*__errno_location ()) == 17 || (*__errno_location ()) == 21)
    continue;
   printf("permanent failure creating logfile %s: %s\n", buf, strerror((*__errno_location ())));
   exit(1);
  }
  logfile = fdopen(fd, "w");
  return;
 }
 printf("tried %d filenames but couldn't create any logfile in %s\n", i, logdir);
 exit(1);
}

variable_types: bufsize int

variable_types:
buf
char *


decompiled_code:
int open_socket()
{
int v0; // ebx
struct sockaddr v2; // [rsp+0h] [rbp-18h] BYREF

v0 = socket(2, 2, 0);
net_fd = v0;
*(_DWORD *)&v2.sa_data[10] = 0;
v2.sa_family = 2;
*(_WORD *)v2.sa_data = _bswap_16(5005);
*(_QWORD *)&v2.sa_data[2] = (unsigned int)_bswap_32(0x7F000001u);
return connect(v0, &v2, 0x10u);
}

canonical_decompiled_code:
int func4()
{
int v0; // ebx
struct sockaddr v2; // [rsp+0h] [rbp-18h] BYREF

v0 = socket(2, 2, 0);
net_fd = v0;
*(_DWORD *)&v2.sa_data[10] = 0;
v2.sa_family = 2;
*(_WORD *)v2.sa_data = func2(5005);
*(_QWORD *)&v2.sa_data[2] = (unsigned int)func3(0x7F000001u);
return connect(v0, &v2, 0x10u);
}

original_code:
static void open_socket(void)
{
 net_fd = socket(2, SOCK_DGRAM, 0);
 struct sockaddr_in remote = { 2 };
 remote.sin_port = htons(5005);
 if(loopback)
  remote.sin_addr.s_addr = htonl(((in_addr_t) 0x7f000001));
 connect(net_fd, (struct sockaddr *) &remote, sizeof(struct sockaddr));
}

canonical_original_code:
static void open_socket()
{
 net_fd = socket(2, SOCK_DGRAM, 0);
 struct sockaddr_in remote = { 2 };
 remote.sin_port = htons(5005);
 if(loopback)
  remote.sin_addr.s_addr = htonl(((unsigned int) 0x7f000001));
 connect(net_fd, (struct sockaddr *) &remote, sizeof(struct sockaddr));
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
void flush_buffers()
{
int v0; // eax

if ( pos != &unk_464 )
{
send(net_fd, &bucket, (_BYTE *)pos - (_BYTE *)&bucket, 0);
pos = &unk_464;
v0 = _bswap_32(bucket);
bucket = _bswap_32(v0 + 1);
fflush(logfile);
}
}

canonical_decompiled_code:
void func5()
{
int v0; // eax

if ( pos != &unk_464 )
{
send(net_fd, &bucket, (_BYTE *)pos - (_BYTE *)&bucket, 0);
pos = &unk_464;
v0 = func3(bucket);
bucket = func3(v0 + 1);
fflush(logfile);
}
}

original_code:
void flush_buffers(void)
{
 if(pos == bucket.buf)
  return;
 send(net_fd, &bucket, pos - (char *) &bucket, 0);
 pos = bucket.buf;
 bucket.seq = htonl(ntohl(bucket.seq) + 1);
 fflush(logfile);
}

canonical_original_code:
void flush_buffers()
{
 if(pos == bucket.buf)
  return;
 send(net_fd, &bucket, pos - (char *) &bucket, 0);
 pos = bucket.buf;
 bucket.seq = htonl(ntohl(bucket.seq) + 1);
 fflush(logfile);
}


decompiled_code:
size_t printf_tagged_message(int a1, const char *a2, ...)
{
unsigned __int16 v2; // ax
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, a2);
v2 = vsnprintf(s, 0x400uLL, a2, arg);
return write_tagged_message(a1, s, v2);
}

canonical_decompiled_code:
size_t func7(int a1, const char *a2, ...)
{
unsigned __int16 v2; // ax
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, a2);
v2 = vsnprintf(s, 0x400uLL, a2, arg);
return func6(a1, s, v2);
}

original_code:
void printf_tagged_message(uint32_t fourcc, const char *fmt, ...)
{
 char buf[1024];
 int len;
 va_list ap;
 __builtin_va_start(ap,fmt);
 len = vsnprintf(buf, sizeof buf, fmt, ap);
 __builtin_va_end(ap);
 write_tagged_message(fourcc, buf, len);
}

canonical_original_code:
void printf_tagged_message(unsigned int fourcc, char *fmt, ...)
{
 char buf[1024];
 int len;
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 len = vsnprintf(buf, sizeof buf, fmt, ap);
 __builtin_va_end(ap);
 write_tagged_message(fourcc, buf, len);
}

variable_types:
fourcc
unsigned int

variable_types:
fmt
char *

variable_types:
buf
char [1024]

variable_types: len int


decompiled_code:
void init_logging()
{
int v0; // eax

open_logfile();
open_socket();
clock_gettime(1, &starttime);
v0 = _bswap_32(0x4C4F4753u);
printf_tagged_message(v0, "initialized");
flush_buffers();
}

canonical_decompiled_code:
void func8()
{
int v0; // eax

func1();
func4();
clock_gettime(1, &starttime);
v0 = func3(0x4C4F4753u);
func7(v0, "initialized");
func5();
}

original_code:
void init_logging(void)
{
 open_logfile();
 open_socket();
 clock_gettime(1, &starttime);
 printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "initialized");
 flush_buffers();
}

canonical_original_code:
void init_logging()
{
 open_logfile();
 open_socket();
 clock_gettime(1, &starttime);
 printf_tagged_message(htonl((('L') << 24) | (('O') << 16) | (('G') << 8) | ('S')), "initialized");
 flush_buffers();
}


printbuffer

decompiled_code:
int __fastcall printbuffer(unsigned int a1, unsigned __int8 *a2, __int64 a3)
{
int v3; // ebp
unsigned __int8 *v4; // rbx
unsigned __int8 v5; // dl

v3 = a3;
printf("\nfourcc=%d buffer act_len=%d buffer: ", a1, a3);
if ( v3 > 0 )
{
v4 = a2;
do
{
v5 = 46;
if ( *v4 >= 0x20u )
v5 = *v4;
printf("%02X(%c) ", *v4++, v5);
}
while ( v4 != &a2[v3 - 1 + 1] );
}
putchar(10);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func9(unsigned int a1, unsigned __int8 *a2, __int64 a3)
{
int v3; // ebp
unsigned __int8 *v4; // rbx
unsigned __int8 v5; // dl

v3 = a3;
printf("\nfourcc=%d buffer act_len=%d buffer: ", a1, a3);
if ( v3 > 0 )
{
v4 = a2;
do
{
v5 = 46;
if ( *v4 >= 0x20u )
v5 = *v4;
printf("%02X(%c) ", *v4++, v5);
}
while ( v4 != &a2[v3 - 1 + 1] );
}
putchar(10);
return fflush(stdout);
}

original_code:
void printbuffer(uint32_t fourcc, const unsigned char *buffer, int act_len)
{
 printf("\nfourcc=%d buffer act_len=%d buffer: ", fourcc, act_len);
 for (int i = 0; i < act_len; i++) {
  unsigned char c = buffer[i];
  printf ("%02X(%c) ", c, c < 32 ? '.': c);
 }
 printf("\n");
 fflush(stdout);
}

canonical_original_code:
void printbuffer(unsigned int fourcc, unsigned char *buffer, int act_len)
{
 printf("\nfourcc=%d buffer act_len=%d buffer: ", fourcc, act_len);
 for (int i = 0; i < act_len; i++) {
  unsigned char c = buffer[i];
  printf ("%02X(%c) ", c, c < 32 ? '.': c);
 }
 printf("\n");
 fflush(stdout);
}

variable_types:
buffer
unsigned char *

variable_types: i int

variable_types:
c
unsigned char


decompiled_code:
__int64 FCFERR_Poll()
{
return handleErrorPoll();
}

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

original_code:
void FCFERR_Poll() {
        handleErrorPoll();
}


decompiled_code:
__int64 __fastcall FCF_Log(__int64 a1, __int64 a2, unsigned int a3)
{
write_tagged_message(a1, a2, (unsigned __int16)a3);
return printbuffer((unsigned int)a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, unsigned int a3)
{
write_tagged_message(a1, a2, (unsigned __int16)a3);
return printbuffer((unsigned int)a1, a2, a3);
}

original_code:
void FCF_Log (uint32_t fourcc, const unsigned char *buf, int act_len) {
  write_tagged_message(fourcc, buf, act_len);
  printbuffer(fourcc, buf, act_len);
}

canonical_original_code:
void FCF_Log(unsigned int fourcc, unsigned char *buf, int act_len) {
  write_tagged_message(fourcc, buf, act_len);
  printbuffer(fourcc, buf, act_len);
}

variable_types:
buf
unsigned char *


decompiled_code:
__int64 FCF_ProfSendMsg()
{
return profReceiveMsg();
}

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

original_code:
void FCF_ProfSendMsg(const char *message){
  profReceiveMsg(message);
}

canonical_original_code:
void FCF_ProfSendMsg(char *message){
  profReceiveMsg(message);
}

variable_types:
message
char *


decompiled_code:
__int64 profiling_cb()
{
FCF_ProfSendMsg(&unk_108);
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
FCF_ProfSendMsg(&unk_108);
return 0LL;
}

original_code:
static int profiling_cb (struct pollfd *pfd) {
 FCF_ProfSendMsg("");
 return 0;
}

canonical_original_code:
static int profiling_cb(struct pollfd *pfd) {
 FCF_ProfSendMsg("");
 return 0;
}


InitProfiling

decompiled_code:
int InitProfiling()
{
unsigned int v0; // ebx

t.it_interval.tv_sec = 0LL;
t.it_interval.tv_nsec = 0LL;
t.it_value.tv_sec = 0LL;
t.it_value.tv_nsec = 1LL;
v0 = timerfd_create(0, 0);
thefd = v0;
timerfd_settime(v0, 0, &t, 0LL);
fcf_addfd(v0, 1LL, profiling_cb);
return printf("\nprofile fd : %d", (unsigned int)thefd);
}

canonical_decompiled_code:
int func1()
{
unsigned int v0; // ebx

t.it_interval.tv_sec = 0LL;
t.it_interval.tv_nsec = 0LL;
t.it_value.tv_sec = 0LL;
t.it_value.tv_nsec = 1LL;
v0 = timerfd_create(0, 0);
thefd = v0;
timerfd_settime(v0, 0, &t, 0LL);
fcf_addfd(v0, 1LL, profiling_cb);
return printf("\nprofile fd : %d", (unsigned int)thefd);
}

original_code:
void InitProfiling() {
 t.it_interval.tv_sec = 0;
 t.it_interval.tv_nsec = 0;
 t.it_value.tv_sec = 0;
 t.it_value.tv_nsec = 1;
    thefd = timerfd_create(0, 0);
    timerfd_settime(thefd, 0, &t, ((void *)0));
    fcf_addfd(thefd, 0x001, profiling_cb);
    printf ("\nprofile fd : %d", thefd);
}


profReceiveMsg

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

if ( count == 10000000 )
result = stop_main_loop();
++count;
return result;
}

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

if ( count == 10000000 )
result = stop_main_loop();
++count;
return result;
}

original_code:
void profReceiveMsg (const char *msg) {
    if (count == 10000000){
        stop_main_loop();
    }
    count++;
}

canonical_original_code:
void profReceiveMsg(char *msg) {
    if (count == 10000000){
        stop_main_loop();
    }
    count++;
}

variable_types:
msg
char *


decompiled_code:
char *find_frames()
{
char *v0; // rbp
unsigned int v1; // eax
_BYTE *v2; // r13
char *result; // rax
char *v4; // rbx
signed __int64 v5; // r14
int v6; // er12

v0 = (char *)&buf;
while ( 1 )
{
v2 = cur;
result = (char *)memchr(v0, 36, (_BYTE *)cur - v0);
v4 = result;
if ( !result )
{
cur = &buf;
return result;
}
v5 = v2 - result;
if ( v2 - result <= 7 )
break;
v0 = result + 1;
if ( !memcmp(result + 1, &unk_11E8, 3uLL) && !v4[5] )
{
v6 = *((unsigned __int16 *)v4 + 3);
if ( v5 < v6 + 12 )
break;
if ( !memcmp(&v4[v6 + 10], &unk_11EC, 2uLL) )
{
v1 = _bswap_32((unsigned __int8)v4[4] | 0x47505300u);
FCF_Log(v1, v4 + 8, (unsigned int)(v6 + 2));
v0 = &v4[v6 + 12];
}
else
{
v0 = v4 + 4;
}
}
}
result = (char *)memmove(&buf, v4, v2 - v4);
cur = &v2[-(v4 - (char *)&buf)];
return result;
}

canonical_decompiled_code:
char *func1()
{
char *v0; // rbp
unsigned int v1; // eax
_BYTE *v2; // r13
char *result; // rax
char *v4; // rbx
signed __int64 v5; // r14
int v6; // er12

v0 = (char *)&buf;
while ( 1 )
{
v2 = cur;
result = (char *)memchr(v0, 36, (_BYTE *)cur - v0);
v4 = result;
if ( !result )
{
cur = &buf;
return result;
}
v5 = v2 - result;
if ( v2 - result <= 7 )
break;
v0 = result + 1;
if ( !memcmp(result + 1, &unk_11E8, 3uLL) && !v4[5] )
{
v6 = *((unsigned __int16 *)v4 + 3);
if ( v5 < v6 + 12 )
break;
if ( !memcmp(&v4[v6 + 10], &unk_11EC, 2uLL) )
{
v1 = func0((unsigned __int8)v4[4] | 0x47505300u);
FCF_Log(v1, v4 + 8, (unsigned int)(v6 + 2));
v0 = &v4[v6 + 12];
}
else
{
v0 = v4 + 4;
}
}
}
result = (char *)memmove(&buf, v4, v2 - v4);
cur = &v2[-(v4 - (char *)&buf)];
return result;
}

original_code:
static void find_frames(void)
{
 unsigned char *pos = buf;
 while((pos = memchr(pos, '$', cur - pos)))
 {
  if(cur - pos < 8)
   break;
  if(memcmp(pos + 1, "BIN", 3) != 0 || pos[5] != 0)
  {
   ++pos;
   continue;
  }
  int data_length = pos[6] | (pos[7] << 8);
  if(cur - pos < data_length + 12)
   break;
  if(memcmp(pos + data_length + 10, "\r\n", 2) != 0)
  {
   pos += 4;
   continue;
  }
  FCF_Log (htonl((('G') << 24) | (('P') << 16) | (('S') << 8) | (pos[4])), pos + 8, data_length + 2);
  pos += data_length + 12;
 }
 if(pos == ((void *)0))
  cur = buf;
 else
 {
  memmove(buf, pos, cur - pos);
  cur -= pos - buf;
 }
}

canonical_original_code:
static void find_frames()
{
 unsigned char *pos = buf;
 while((pos = memchr(pos, '$', cur - pos)))
 {
  if(cur - pos < 8)
   break;
  if(memcmp(pos + 1, "BIN", 3) != 0 || pos[5] != 0)
  {
   ++pos;
   continue;
  }
  int data_length = pos[6] | (pos[7] << 8);
  if(cur - pos < data_length + 12)
   break;
  if(memcmp(pos + data_length + 10, "\r\n", 2) != 0)
  {
   pos += 4;
   continue;
  }
  FCF_Log (htonl((('G') << 24) | (('P') << 16) | (('S') << 8) | (pos[4])), pos + 8, data_length + 2);
  pos += data_length + 12;
 }
 if(pos == ((void *)0))
  cur = buf;
 else
 {
  memmove(buf, pos, cur - pos);
  cur -= pos - buf;
 }
}

variable_types:
pos
unsigned char *


decompiled_code:
__int64 __fastcall read_gps_cb(__int64 a1)
{
__int64 result; // rax
ssize_t v2; // rdx

result = 0LL;
if ( *(_WORD *)(a1 + 6) == 1 )
{
v2 = read(*(_DWORD *)a1, cur, (char *)&device - (_BYTE *)cur);
result = 0LL;
if ( v2 > 0 )
{
cur = (char *)cur + v2;
find_frames();
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 result; // rax
ssize_t v2; // rdx

result = 0LL;
if ( *(_WORD *)(a1 + 6) == 1 )
{
v2 = read(*(_DWORD *)a1, cur, (char *)&device - (_BYTE *)cur);
result = 0LL;
if ( v2 > 0 )
{
cur = (char *)cur + v2;
func1();
result = 1LL;
}
}
return result;
}

original_code:
static int read_gps_cb (struct pollfd *pfd) {
 if (pfd->revents != 0x001) {
  return 0;
 }
 ssize_t nread = read (pfd->fd, cur, sizeof(buf)-(cur-buf));
 if (nread <= 0) {
  return 0;
 }
 cur += nread;
 find_frames();
 return 1;
}

canonical_original_code:
static int read_gps_cb(struct pollfd *pfd) {
 if (pfd->revents != 0x001) {
  return 0;
 }
 long nread = read (pfd->fd, cur, sizeof(buf)-(cur-buf));
 if (nread <= 0) {
  return 0;
 }
 cur += nread;
 find_frames();
 return 1;
}

variable_types: nread long


decompiled_code:
void __fastcall set_gps_devicepath(char *a1)
{
device = a1;
}

canonical_decompiled_code:
void __fastcall func3(char *a1)
{
device = a1;
}

original_code:
void set_gps_devicepath (const char *dev) {
 device = dev;
}

canonical_original_code:
void set_gps_devicepath(char *dev) {
 device = dev;
}

variable_types:
dev
char *


decompiled_code:
void __fastcall common_cb(__int64 a1, unsigned int a2)
{
__int64 v3; // rdi

v3 = *(unsigned int *)(a1 + 16);
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 != 3 )
print_libusb_transfer_error(v3, "common_cb");
}
else
{
FCF_Log(a2, *(_QWORD *)(a1 + 48), *(unsigned int *)(a1 + 24));
if ( (unsigned int)libusb_submit_transfer(a1) )
print_libusb_transfer_error(*(unsigned int *)(a1 + 16), "common_cb resub");
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2)
{
__int64 v3; // rdi

v3 = *(unsigned int *)(a1 + 16);
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 != 3 )
print_libusb_transfer_error(v3, "common_cb");
}
else
{
FCF_Log(a2, *(_QWORD *)(a1 + 48), *(unsigned int *)(a1 + 24));
if ( (unsigned int)libusb_submit_transfer(a1) )
print_libusb_transfer_error(*(unsigned int *)(a1 + 16), "common_cb resub");
}
}

original_code:
static int common_cb (uint32_t fourcc, int fd) {
 int rc = readsocket(fd, buffer, sizeof(buffer));
 if (rc > 0) {
  FCF_Log(fourcc, buffer, rc);
 }
 return rc;
}

canonical_original_code:
static int common_cb(unsigned int fourcc, int fd) {
 int rc = readsocket(fd, buffer, sizeof(buffer));
 if (rc > 0) {
  FCF_Log(fourcc, buffer, rc);
 }
 return rc;
}

variable_types: fd int

variable_types: rc int


decompiled_code:
void __fastcall common_cb(__int64 a1, unsigned int a2)
{
__int64 v3; // rdi
__int64 v5; // rsi
__int64 v6; // rdx

v3 = *(unsigned int *)(a1 + 16);
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 != 3 )
print_libusb_transfer_error(v3, "adis_cb");
}
else
{
v5 = *(_QWORD *)(a1 + 48);
v6 = *(unsigned int *)(a1 + 24);
if ( (_DWORD)v6 == 24 )
FCF_Log(a2, v5, 24LL);
else
FCF_Log(a2, v5, v6);
if ( (unsigned int)libusb_submit_transfer(a1) )
print_libusb_transfer_error(*(unsigned int *)(a1 + 16), "adis_cb resub");
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, unsigned int a2)
{
__int64 v3; // rdi
__int64 v5; // rsi
__int64 v6; // rdx

v3 = *(unsigned int *)(a1 + 16);
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 != 3 )
print_libusb_transfer_error(v3, "adis_cb");
}
else
{
v5 = *(_QWORD *)(a1 + 48);
v6 = *(unsigned int *)(a1 + 24);
if ( (_DWORD)v6 == 24 )
FCF_Log(a2, v5, 24LL);
else
FCF_Log(a2, v5, v6);
if ( (unsigned int)libusb_submit_transfer(a1) )
print_libusb_transfer_error(*(unsigned int *)(a1 + 16), "adis_cb resub");
}
}


decompiled_code:
void __fastcall common_cb(__int64 a1, unsigned int a2)
{
__int64 v3; // rdi

v3 = *(unsigned int *)(a1 + 16);
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 != 3 )
print_libusb_transfer_error(v3, "imu_cb");
}
else
{
FCF_Log(a2, *(_QWORD *)(a1 + 48));
if ( (unsigned int)libusb_submit_transfer(a1) )
print_libusb_transfer_error(*(unsigned int *)(a1 + 16), "imu_cb resub");
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, unsigned int a2)
{
__int64 v3; // rdi

v3 = *(unsigned int *)(a1 + 16);
if ( (_DWORD)v3 )
{
if ( (_DWORD)v3 != 3 )
print_libusb_transfer_error(v3, "imu_cb");
}
else
{
FCF_Log(a2, *(_QWORD *)(a1 + 48));
if ( (unsigned int)libusb_submit_transfer(a1) )
print_libusb_transfer_error(*(unsigned int *)(a1 + 16), "imu_cb resub");
}
}


dieWithUserMessage

decompiled_code:
void __fastcall __noreturn dieWithUserMessage(const char *a1, const char *a2)
{
fputs(a1, stderr);
fwrite(&unk_6A, 1uLL, 2uLL, stderr);
fputs(a2, stderr);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1, const char *a2)
{
fputs(a1, stderr);
fwrite(&unk_6A, 1uLL, 2uLL, stderr);
fputs(a2, stderr);
fputc(10, stderr);
exit(1);
}

original_code:
void dieWithUserMessage(const char* message, const char* detail){
    fputs(message, stderr);
    fputs(": ", stderr);
    fputs(detail, stderr);
    fputc('\n', stderr);
    exit(1);
}

canonical_original_code:
void dieWithUserMessage(char *message, char *detail){
    fputs(message, stderr);
    fputs(": ", stderr);
    fputs(detail, stderr);
    fputc('\n', stderr);
    exit(1);
}

variable_types:
detail
char *


dieWithSystemMessage

decompiled_code:
void __fastcall __noreturn dieWithSystemMessage(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void dieWithSystemMessage(const char* message){
    perror(message);
    exit(1);
}

canonical_original_code:
void dieWithSystemMessage(char *message){
    perror(message);
    exit(1);
}


initSymbolTable

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

if ( a2 )
{
v2 = strlen(a2);
*(_QWORD *)a1 = malloc(v2 + 1);
*(_QWORD *)a1 = strdup(a2);
}
else
{
*(_QWORD *)a1 = 0LL;
}
*(_WORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
*(_QWORD *)(a1 + 32) = 0LL;
}

original_code:
void initSymbolTable(SYMREC* table, const char* name){
    if (name != ((void *)0)){
        table->name = (char*)malloc(strlen(name) + 1);
        table->name = strdup(name);
    }
    else{
        table->name = ((void *)0);
    }
    table->id_type = 0;
    table->value.type = ((void *)0);
    table->location = ((void *)0);
    table->next = ((void *)0);
    return;
}

canonical_original_code:
void initSymbolTable(struct _symrec *table, char *name){
    if (name != ((void *)0)){
        table->name = (char *)malloc(strlen(name) + 1);
        table->name = strdup(name);
    }
    else{
        table->name = ((void *)0);
    }
    table->id_type = 0;
    table->value.type = ((void *)0);
    table->location = ((void *)0);
    table->next = ((void *)0);
    return;
}

variable_types: struct _symrec { char *name; unsigned short id_type; union { char *type; struct _symrec *list; } value; struct _symloc { unsigned int line; unsigned short column; } *location; struct _symrec *next; } *

variable_types:
name
char *

user_defined_types: struct _symrec { char *name; unsigned short id_type; union { char *type; struct _symrec *list; } value; struct _symloc *location; struct _symrec *next; }


freeSymbolTable

decompiled_code:
void __fastcall freeSymbolTable(_QWORD *a1)
{
__int64 i; // rbx

for ( i = a1[4]; i; i = a1[4] )
{
if ( ((*(_WORD *)(i + 8) - 3) & 0xFFFD) == 0 )
freeSymbolTable(*(_QWORD *)(i + 16));
a1[4] = *(_QWORD *)(i + 32);
free(*(void **)(i + 24));
free((void *)i);
}
free(a1);
}

original_code:
void freeSymbolTable(SYMREC* table){
    SYMREC *node;
    node = table->next;
    while (node != ((void *)0)){
        if ((node->id_type == PROCEDURE_DECLARATION)
                || (node->id_type == FUNCTION_DECLARATION)){
            freeSymbolTable(node->value.list);
        }
        table->next = node->next;
        free(node->location);
        free(node);
        node = table->next;
    }
    free(table);
    return;
}

canonical_original_code:
void freeSymbolTable(struct _symrec *table){
    struct _symrec *node;
    node = table->next;
    while (node != ((void *)0)){
        if ((node->id_type == PROCEDURE_DECLARATION)
                || (node->id_type == FUNCTION_DECLARATION)){
            freeSymbolTable(node->value.list);
        }
        table->next = node->next;
        free(node->location);
        free(node);
        node = table->next;
    }
    free(table);
    return;
}

user_defined_types: struct _symloc { unsigned int line; unsigned short column; }


checkSymbolTable

decompiled_code:
int __fastcall checkSymbolTable(__int64 a1, const char *a2, __int16 a3, unsigned int *a4)
{
__int64 v6; // rbx
int result; // eax

v6 = *(_QWORD *)(a1 + 32);
if ( v6 )
{
while ( 1 )
{
result = strcmp(a2, *(const char **)v6);
if ( !result )
break;
v6 = *(_QWORD *)(v6 + 32);
if ( !v6 )
goto LABEL_4;
}
switch ( a3 )
{
case 3:
fprintf(stderr, "Line %u:%u : Multiple Declaration of Procedure '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 0;
break;
case 5:
fprintf(stderr, "Line %u:%u : Multiple Declaration of Function '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 0;
break;
case 1:
fprintf(
stderr,
"Line %u:%u : Multiple Declaration of Identifier '%s'\n",
*a4,
*((unsigned __int16 *)a4 + 2),
a2);
result = 0;
break;
}
}
else
{
LABEL_4:
if ( a3 == 6 )
{
fprintf(stderr, "Line %u:%u : Undefined Function Reference '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 1;
}
else if ( a3 == 7 )
{
fprintf(stderr, "Line %u:%u : Undefined Variable Reference '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 1;
}
else
{
result = 1;
if ( a3 == 4 )
{
fprintf(stderr, "Line %u:%u : Undefined Procedure Reference '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 1;
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, const char *a2, __int16 a3, unsigned int *a4)
{
__int64 v6; // rbx
int result; // eax

v6 = *(_QWORD *)(a1 + 32);
if ( v6 )
{
while ( 1 )
{
result = strcmp(a2, *(const char **)v6);
if ( !result )
break;
v6 = *(_QWORD *)(v6 + 32);
if ( !v6 )
goto LABEL_4;
}
switch ( a3 )
{
case 3:
fprintf(stderr, "Line %u:%u : Multiple Declaration of Procedure '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 0;
break;
case 5:
fprintf(stderr, "Line %u:%u : Multiple Declaration of Function '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 0;
break;
case 1:
fprintf(
stderr,
"Line %u:%u : Multiple Declaration of Identifier '%s'\n",
*a4,
*((unsigned __int16 *)a4 + 2),
a2);
result = 0;
break;
}
}
else
{
LABEL_4:
if ( a3 == 6 )
{
fprintf(stderr, "Line %u:%u : Undefined Function Reference '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 1;
}
else if ( a3 == 7 )
{
fprintf(stderr, "Line %u:%u : Undefined Variable Reference '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 1;
}
else
{
result = 1;
if ( a3 == 4 )
{
fprintf(stderr, "Line %u:%u : Undefined Procedure Reference '%s'\n", *a4, *((unsigned __int16 *)a4 + 2), a2);
result = 1;
}
}
}
return result;
}

original_code:
_Bool checkSymbolTable( const SYMREC* table,
                        const char* name,
                        u_int16 type,
                        const SYMLOC* location){
    SYMREC *node;
    for (node = table->next; node != ((void *)0); node = node->next){
        if (strcmp(name, node->name) == 0){
            switch (type){
                case DECLARATION_LEFT:
                    fprintf(stderr, "Line %u:%u : Multiple Declaration of Identifier '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
                    break;
                case PROCEDURE_DECLARATION:
                    fprintf(stderr, "Line %u:%u : Multiple Declaration of Procedure '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
                    break;
                case FUNCTION_DECLARATION:
                    fprintf(stderr, "Line %u:%u : Multiple Declaration of Function '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
                    break;
                default:
                    break;
            }
            return 0;
        }
    }
    switch (type){
        case PROCEDURE_REFERENCE:
            fprintf(stderr, "Line %u:%u : Undefined Procedure Reference '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
            break;
        case FUNCTION_REFERENCE:
            fprintf(stderr, "Line %u:%u : Undefined Function Reference '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
            break;
        case VARIABLE_REFERENCE:
            fprintf(stderr, "Line %u:%u : Undefined Variable Reference '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
            break;
        default:
            break;
    }
    return 1;
}

canonical_original_code:
_Bool checkSymbolTable(struct _symrec *table, char *name, unsigned short type, struct _symloc *location){
    struct _symrec *node;
    for (node = table->next; node != ((void *)0); node = node->next){
        if (strcmp(name, node->name) == 0){
            switch (type){
                case DECLARATION_LEFT:
                    fprintf(stderr, "Line %u:%u : Multiple Declaration of Identifier '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
                    break;
                case PROCEDURE_DECLARATION:
                    fprintf(stderr, "Line %u:%u : Multiple Declaration of Procedure '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
                    break;
                case FUNCTION_DECLARATION:
                    fprintf(stderr, "Line %u:%u : Multiple Declaration of Function '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
                    break;
                default:
                    break;
            }
            return 0;
        }
    }
    switch (type){
        case PROCEDURE_REFERENCE:
            fprintf(stderr, "Line %u:%u : Undefined Procedure Reference '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
            break;
        case FUNCTION_REFERENCE:
            fprintf(stderr, "Line %u:%u : Undefined Function Reference '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
            break;
        case VARIABLE_REFERENCE:
            fprintf(stderr, "Line %u:%u : Undefined Variable Reference '%s'\n", location->line,
                                                                                location->column,
                                                                                name);
            break;
        default:
            break;
    }
    return 1;
}

variable_types:
type
unsigned short

variable_types:
location
struct _symloc *


putSymbolTable

decompiled_code:
void __fastcall putSymbolTable(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
const char **v3; // rbx

v2 = a2;
while ( v2 )
{
v3 = (const char **)v2;
v2 = *(_QWORD *)(v2 + 32);
v3[4] = 0LL;
if ( (unsigned __int8)checkSymbolTable(a1, *v3, *((_WORD *)v3 + 4), (unsigned int *)v3[3]) )
{
v3[4] = *(const char **)(a1 + 32);
*(_QWORD *)(a1 + 32) = v3;
}
else
{
free(v3);
}
}
}

original_code:
void putSymbolTable(SYMREC* table, SYMREC* list){
    SYMREC *node;
    node = list;
    while (node != ((void *)0)){
        list = node->next;
        node->next = ((void *)0);
        if (checkSymbolTable(table, node->name, node->id_type, node->location)){
            node->next = table->next;
            table->next = node;
        }
        else{
            free(node);
        }
        node = list;
    }
    return;
}

canonical_original_code:
void putSymbolTable(struct _symrec *table, struct _symrec *list){
    struct _symrec *node;
    node = list;
    while (node != ((void *)0)){
        list = node->next;
        node->next = ((void *)0);
        if (checkSymbolTable(table, node->name, node->id_type, node->location)){
            node->next = table->next;
            table->next = node;
        }
        else{
            free(node);
        }
        node = list;
    }
    return;
}

variable_types:
list
struct _symrec *


setIdentifier

original_code:
void setIdentifier( SYMREC* list,
                    u_int16 type,
                    const char *value,
                    SYMREC* child){
    SYMREC *node;
    for (node = list; node != ((void *)0); node = node->next){
        node->id_type = type;
        if ((value == ((void *)0)) && (child == ((void *)0))){
            node->value.type = ((void *)0);
        }
        else if (value != ((void *)0)){
            node->value.type = strdup(value);
        }
        else{
            node->value.list = child;
        }
    }
    return;
}

canonical_original_code:
void setIdentifier(struct _symrec *list, unsigned short type, char *value, struct _symrec *child){
    struct _symrec *node;
    for (node = list; node != ((void *)0); node = node->next){
        node->id_type = type;
        if ((value == ((void *)0)) && (child == ((void *)0))){
            node->value.type = ((void *)0);
        }
        else if (value != ((void *)0)){
            node->value.type = strdup(value);
        }
        else{
            node->value.list = child;
        }
    }
    return;
}

variable_types:
value
char *

variable_types:
child
struct _symrec *


repo: elgitolero/ipc-stuff

decompiled_code:
int __fastcall remove_queue(int a1)
{
return msgctl(a1, 0, 0LL);
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
return msgctl(a1, 0, 0LL);
}

original_code:
void remove_queue(int qid)
{
        msgctl(qid, 0, 0);
}

variable_types: qid int


decompiled_code:
int __fastcall change_queue_mode(int msqid)
{
struct msqid_ds v2; // [rsp+0h] [rbp-98h] BYREF

msgctl(msqid, 2, &v2);
__isoc99_sscanf();
return msgctl(msqid, 1, &v2);
}

canonical_decompiled_code:
int __fastcall func10(int msqid)
{
struct msqid_ds v2; // [rsp+0h] [rbp-98h] BYREF

msgctl(msqid, 2, &v2);
__isoc99_sscanf();
return msgctl(msqid, 1, &v2);
}

original_code:
void change_queue_mode(int qid, char *mode)
{
        struct msqid_ds myqueue_ds;
        msgctl(qid, 2, &myqueue_ds);
        sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);
        msgctl(qid, 1, &myqueue_ds);
}

variable_types:
mode
char *

variable_types: struct msqid_ds { struct ipc_perm { int __key; unsigned int uid; unsigned int gid; unsigned int cuid; unsigned int cgid; unsigned short mode; unsigned short __pad1; unsigned short __seq; unsigned short __pad2; unsigned long __glibc_reserved1; unsigned long __glibc_reserved2; } msg_perm; long msg_stime; long msg_rtime; long msg_ctime; unsigned long __msg_cbytes; unsigned long msg_qnum; unsigned long msg_qbytes; int msg_lspid; int msg_lrpid; unsigned long __glibc_reserved4; unsigned long __glibc_reserved5; }

user_defined_types: struct msqid_ds { struct ipc_perm msg_perm; long msg_stime; long msg_rtime; long msg_ctime; unsigned long __msg_cbytes; unsigned long msg_qnum; unsigned long msg_qbytes; int msg_lspid; int msg_lrpid; unsigned long __glibc_reserved4; unsigned long __glibc_reserved5; }


usage

decompiled_code:
void __noreturn usage()
{
fwrite("msgtool - A utility for tinkering with msg queues\n", 1uLL, 0x32uLL, _bss_start);
fwrite("\nUSAGE: msgtool (s)end <type> <messagetext>\n", 1uLL, 0x2CuLL, _bss_start);
fwrite(" (r)ecv <type>\n", 1uLL, 0x1DuLL, _bss_start);
fwrite(" (d)elete\n", 1uLL, 0x18uLL, _bss_start);
fwrite(" (m)ode <octal mode>\n", 1uLL, 0x23uLL, _bss_start);
exit(1);
}

canonical_decompiled_code:
void __noreturn func11()
{
fwrite("msgtool - A utility for tinkering with msg queues\n", 1uLL, 0x32uLL, _bss_start);
fwrite("\nUSAGE: msgtool (s)end <type> <messagetext>\n", 1uLL, 0x2CuLL, _bss_start);
fwrite(" (r)ecv <type>\n", 1uLL, 0x1DuLL, _bss_start);
fwrite(" (d)elete\n", 1uLL, 0x18uLL, _bss_start);
fwrite(" (m)ode <octal mode>\n", 1uLL, 0x23uLL, _bss_start);
exit(1);
}

original_code:
void usage(void)
{
        fprintf(stderr, "msgtool - A utility for tinkering with msg queues\n");
        fprintf(stderr, "\nUSAGE: msgtool (s)end <type> <messagetext>\n");
        fprintf(stderr, "               (r)ecv <type>\n");
        fprintf(stderr, "               (d)elete\n");
        fprintf(stderr, "               (m)ode <octal mode>\n");
        exit(1);
}

canonical_original_code:
void usage()
{
        fprintf(stderr, "msgtool - A utility for tinkering with msg queues\n");
        fprintf(stderr, "\nUSAGE: msgtool (s)end <type> <messagetext>\n");
        fprintf(stderr, "               (r)ecv <type>\n");
        fprintf(stderr, "               (d)elete\n");
        fprintf(stderr, "               (m)ode <octal mode>\n");
        exit(1);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rdi
int v5; // ebp
const __int32_t **v6; // rax
__int64 v7; // rdx
int v8; // eax
__int64 v9; // rax
__int64 v11; // rax
_BYTE v12[120]; // [rsp+0h] [rbp-78h] BYREF

if ( argc == 1 )
usage(argc, argv, envp);
v3 = ftok(".", 109);
v4 = v3;
v5 = msgget(v3, 944);
if ( v5 == -1 )
{
perror("msgget");
exit(1);
}
v6 = __ctype_tolower_loc();
v7 = *argv[1];
v8 = (*v6)[v7];
if ( v8 == 114 )
{
v11 = atol(argv[2]);
read_message(v5, v12, v11);
return 0;
}
if ( v8 > 114 )
{
if ( v8 == 115 )
{
v9 = atol(argv[2]);
send_message(v5, v12, v9, argv[3]);
return 0;
}
LABEL_15:
usage(v4, 944LL, v7);
}
if ( v8 == 100 )
{
remove_queue(v5);
return 0;
}
if ( v8 != 109 )
goto LABEL_15;
change_queue_mode(v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rdi
int v5; // ebp
const __int32_t **v6; // rax
__int64 v7; // rdx
int v8; // eax
__int64 v9; // rax
__int64 v11; // rax
_BYTE v12[120]; // [rsp+0h] [rbp-78h] BYREF

if ( argc == 1 )
func11(argc, argv, envp);
v3 = ftok(".", 109);
v4 = v3;
v5 = msgget(v3, 944);
if ( v5 == -1 )
{
perror("msgget");
exit(1);
}
v6 = __ctype_tolower_loc();
v7 = *argv[1];
v8 = (*v6)[v7];
if ( v8 == 114 )
{
v11 = atol(argv[2]);
func8(v5, v12, v11);
return 0;
}
if ( v8 > 114 )
{
if ( v8 == 115 )
{
v9 = atol(argv[2]);
func7(v5, v12, v9, argv[3]);
return 0;
}
LABEL_15:
func11(v4, 944LL, v7);
}
if ( v8 == 100 )
{
func9(v5);
return 0;
}
if ( v8 != 109 )
goto LABEL_15;
func10(v5);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
        key_t key;
        int msgqueue_id;
        struct mymsgbuf qbuf;
        if(argc == 1)
                usage();
        key = ftok(".", 'm');
        if((msgqueue_id = msgget(key, 01000|0660)) == -1) {
                perror("msgget");
                exit(1);
        }
        switch(tolower(argv[1][0]))
        {
                case 's': send_message(msgqueue_id, (struct mymsgbuf *)&qbuf,
                                       atol(argv[2]), argv[3]);
                          break;
                case 'r': read_message(msgqueue_id, &qbuf, atol(argv[2]));
                          break;
                case 'd': remove_queue(msgqueue_id);
                          break;
                case 'm': change_queue_mode(msgqueue_id, argv[2]);
                          break;
                 default: usage();
        }
        return(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
        int key;
        int msgqueue_id;
        struct mymsgbuf qbuf;
        if(argc == 1)
                usage();
        key = ftok(".", 'm');
        if((msgqueue_id = msgget(key, 01000|0660)) == -1) {
                perror("msgget");
                exit(1);
        }
        switch(tolower(argv[1][0]))
        {
                case 's': send_message(msgqueue_id, (struct mymsgbuf *)&qbuf,
                                       atol(argv[2]), argv[3]);
                          break;
                case 'r': read_message(msgqueue_id, &qbuf, atol(argv[2]));
                          break;
                case 'd': remove_queue(msgqueue_id);
                          break;
                case 'm': change_queue_mode(msgqueue_id, argv[2]);
                          break;
                 default: usage();
        }
        return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: key int

variable_types: struct mymsgbuf { long mtype; char mtext[80]; }


repo: eliben/libjit-samples

decompiled_code:
void *__fastcall alloc_executable_memory(size_t len)
{
void *result; // rax

result = mmap(0LL, len, 7, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("mmap");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func7(size_t len)
{
void *result; // rax

result = mmap(0LL, len, 7, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("mmap");
result = 0LL;
}
return result;
}

original_code:
void* alloc_executable_memory(size_t size) {
  void* ptr = mmap(0, size,
                   0x1 | 0x2 | 0x4,
                   0x02 | 0x20, -1, 0);
  if (ptr == (void*)-1) {
    perror("mmap");
    return ((void *)0);
  }
  return ptr;
}

canonical_original_code:
void *alloc_executable_memory(unsigned long size) {
  void *ptr = mmap(0, size,
                   0x1 | 0x2 | 0x4,
                   0x02 | 0x20, -1, 0);
  if (ptr == (void *)-1) {
    perror("mmap");
    return ((void *)0);
  }
  return ptr;
}

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


decompiled_code:
void *__fastcall alloc_writable_memory(size_t len)
{
void *result; // rax

result = mmap(0LL, len, 3, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("mmap");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(size_t len)
{
void *result; // rax

result = mmap(0LL, len, 3, 34, -1, 0LL);
if ( result == (void *)-1LL )
{
perror("mmap");
result = 0LL;
}
return result;
}

original_code:
void* alloc_writable_memory(size_t size) {
  void* ptr = mmap(0, size,
                   0x1 | 0x2,
                   0x02 | 0x20, -1, 0);
  if (ptr == (void*)-1) {
    perror("mmap");
    return ((void *)0);
  }
  return ptr;
}

canonical_original_code:
void *alloc_writable_memory(unsigned long size) {
  void *ptr = mmap(0, size,
                   0x1 | 0x2,
                   0x02 | 0x20, -1, 0);
  if (ptr == (void *)-1) {
    perror("mmap");
    return ((void *)0);
  }
  return ptr;
}


decompiled_code:
__int64 __fastcall make_memory_executable(void *a1, size_t a2)
{
unsigned int v2; // ebx

if ( mprotect(a1, a2, 5) != -1 )
return 0;
v2 = -1;
perror("mprotect");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func9(void *a1, size_t a2)
{
unsigned int v2; // ebx

if ( mprotect(a1, a2, 5) != -1 )
return 0;
v2 = -1;
perror("mprotect");
return v2;
}

original_code:
int make_memory_executable(void* m, size_t size) {
  if (mprotect(m, size, 0x1 | 0x4) == -1) {
    perror("mprotect");
    return -1;
  }
  return 0;
}

canonical_original_code:
int make_memory_executable(void *m, unsigned long size) {
  if (mprotect(m, size, 0x1 | 0x4) == -1) {
    perror("mprotect");
    return -1;
  }
  return 0;
}

variable_types:
m
void *


decompiled_code:
__int64 __fastcall emit_code_into_memory(_QWORD *a1)
{
*a1 = 0xC304C08348F88948LL;
return 8LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
*a1 = 0xC304C08348F88948LL;
return 8LL;
}

original_code:
size_t emit_code_into_memory(unsigned char* m) {
  unsigned char code[] = {
    0x48, 0x89, 0xf8,
    0x48, 0x83, 0xc0, 0x04,
    0xc3
  };
  memcpy(m, code, sizeof(code));
  return sizeof(code);
}

canonical_original_code:
unsigned long emit_code_into_memory(unsigned char *m) {
  unsigned char code[] = {
    0x48, 0x89, 0xf8,
    0x48, 0x83, 0xc0, 0x04,
    0xc3
  };
  memcpy(m, code, sizeof(code));
  return sizeof(code);
}

variable_types:
m
unsigned char *

variable_types:
code
unsigned char []


decompiled_code:
int emit_to_rw_run_from_rx()
{
_QWORD *v0; // rbx
__int64 v1; // rax

v0 = alloc_writable_memory(0x400uLL);
emit_code_into_memory(v0);
make_memory_executable(v0, 0x400uLL);
v1 = ((__int64 (__fastcall *)(__int64))v0)(2LL);
return printf("result = %d\n", v1);
}

canonical_decompiled_code:
int func12()
{
_QWORD *v0; // rbx
__int64 v1; // rax

v0 = func8(0x400uLL);
func10(v0);
func9(v0, 0x400uLL);
v1 = ((__int64 (__fastcall *)(__int64))v0)(2LL);
return printf("result = %d\n", v1);
}

original_code:
void emit_to_rw_run_from_rx() {
  void* m = alloc_writable_memory(SIZE);
  emit_code_into_memory(m);
  make_memory_executable(m, SIZE);
  JittedFunc func = m;
  int result = func(2);
  printf("result = %d\n", result);
}

canonical_original_code:
void emit_to_rw_run_from_rx() {
  void *m = alloc_writable_memory(SIZE);
  emit_code_into_memory(m);
  make_memory_executable(m, SIZE);
  long (*func)(long) = m;
  int result = func(2);
  printf("result = %d\n", result);
}

variable_types:
func
long (*)(long)

variable_types: result int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
run_from_rwx();
emit_to_rw_run_from_rx();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func11();
func12();
return 0;
}

original_code:
int main(int argc, char** argv) {
  run_from_rwx();
  emit_to_rw_run_from_rx();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  run_from_rwx();
  emit_to_rw_run_from_rx();
  return 0;
}

variable_types:
argv
char **


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

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

original_code:
void free_map(struct Map *map) {
 free(map->tiles);
}

variable_types: struct Map { struct Tile { unsigned char x; unsigned char y; short type; short direction; } *tiles; unsigned char height; } *

user_defined_types: struct Map { struct Tile *tiles; unsigned char height; }


decompiled_code:
__int64 __fastcall get_tile(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
return *(_QWORD *)a1 + 6LL * (a2 + a3 * *(unsigned __int8 *)(a1 + 9));
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
return *(_QWORD *)a1 + 6LL * (a2 + a3 * *(unsigned __int8 *)(a1 + 9));
}

original_code:
struct Tile *get_tile(struct Map *map, uint8_t x, uint8_t y) {
 return map->tiles + ((y * map->width) + x);
}

canonical_original_code:
struct Tile *get_tile(struct Map *map, unsigned char x, unsigned char y) {
 return map->tiles + ((y * map->width) + x);
}

variable_types: struct Map { struct Tile *tiles; unsigned char height; } *

variable_types:
x
unsigned char

variable_types:
y
unsigned char

return_type: struct Tile { unsigned char x; unsigned char y; short type; short direction; } *


decompiled_code:
unsigned __int64 __fastcall init_map(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // ebx
unsigned __int64 result; // rax
int v5; // ebx
int v6; // er12
__int64 v7; // rax

*(_BYTE *)(a1 + 9) = a2;
*(_BYTE *)(a1 + 8) = a3;
v3 = a3;
result = (unsigned __int64)malloc(6LL * a3 * a2);
*(_QWORD *)a1 = result;
if ( v3 * v3 > 0 )
{
v5 = 0;
do
{
v6 = v5 % *(unsigned __int8 *)(a1 + 9);
v7 = get_tile(a1, v6, v5 / *(unsigned __int8 *)(a1 + 9));
*(_BYTE *)v7 = v6;
*(_BYTE *)(v7 + 1) = v5 / *(unsigned __int8 *)(a1 + 9);
*(_WORD *)(v7 + 2) = 0;
*(_WORD *)(v7 + 4) = 1;
++v5;
result = *(unsigned __int8 *)(a1 + 8) * (unsigned int)*(unsigned __int8 *)(a1 + 8);
}
while ( (int)result > v5 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(__int64 a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // ebx
unsigned __int64 result; // rax
int v5; // ebx
int v6; // er12
__int64 v7; // rax

*(_BYTE *)(a1 + 9) = a2;
*(_BYTE *)(a1 + 8) = a3;
v3 = a3;
result = (unsigned __int64)malloc(6LL * a3 * a2);
*(_QWORD *)a1 = result;
if ( v3 * v3 > 0 )
{
v5 = 0;
do
{
v6 = v5 % *(unsigned __int8 *)(a1 + 9);
v7 = func8(a1, v6, v5 / *(unsigned __int8 *)(a1 + 9));
*(_BYTE *)v7 = v6;
*(_BYTE *)(v7 + 1) = v5 / *(unsigned __int8 *)(a1 + 9);
*(_WORD *)(v7 + 2) = 0;
*(_WORD *)(v7 + 4) = 1;
++v5;
result = *(unsigned __int8 *)(a1 + 8) * (unsigned int)*(unsigned __int8 *)(a1 + 8);
}
while ( (int)result > v5 );
}
return result;
}

original_code:
void init_map(struct Map *map, uint8_t width, uint8_t height) {
 map->width = width;
 map->height = height;
 map->tiles = malloc((map->width * map->height) * sizeof(struct Tile));
 struct Tile *currentTile;
 for (int i = 0; i < map->height * map->height; i++) {
  currentTile = get_tile(map, i % map->width, i / map->width);
  currentTile->x = i % map->width;
  currentTile->y = i / map->width;
  currentTile->type = BLANK;
  currentTile->direction = UP;
 }
}

canonical_original_code:
void init_map(struct Map *map, unsigned char width, unsigned char height) {
 map->width = width;
 map->height = height;
 map->tiles = malloc((map->width * map->height) * sizeof(struct Tile));
 struct Tile *currentTile;
 for (int i = 0; i < map->height * map->height; i++) {
  currentTile = get_tile(map, i % map->width, i / map->width);
  currentTile->x = i % map->width;
  currentTile->y = i / map->width;
  currentTile->type = BLANK;
  currentTile->direction = UP;
 }
}

variable_types:
width
unsigned char

variable_types:
height
unsigned char

variable_types: struct Tile { unsigned char x; unsigned char y; short type; short direction; } *

variable_types: i int


decompiled_code:
__int64 __fastcall manhattan_tile_distance(unsigned __int8 *a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // dl
unsigned __int8 v3; // cl
int v4; // edx
unsigned __int8 v5; // cl
unsigned __int8 v6; // si
int v7; // eax

v2 = *a1;
v3 = *a2;
if ( *a1 <= *a2 )
v4 = v3 - v2;
else
v4 = v2 - v3;
v5 = a1[1];
v6 = a2[1];
if ( v5 <= v6 )
v7 = v6 - v5;
else
v7 = v5 - v6;
return (unsigned int)(v4 + v7);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 *a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // dl
unsigned __int8 v3; // cl
int v4; // edx
unsigned __int8 v5; // cl
unsigned __int8 v6; // si
int v7; // eax

v2 = *a1;
v3 = *a2;
if ( *a1 <= *a2 )
v4 = v3 - v2;
else
v4 = v2 - v3;
v5 = a1[1];
v6 = a2[1];
if ( v5 <= v6 )
v7 = v6 - v5;
else
v7 = v5 - v6;
return (unsigned int)(v4 + v7);
}

original_code:
int manhattan_tile_distance(struct Tile* tileA, struct Tile* tileB) {
 int xDistance = tileA->x > tileB->x ? tileA->x - tileB->x : tileB->x - tileA->x;
 int yDistance = tileA->y > tileB->y ? tileA->y - tileB->y : tileB->y - tileA->y;
 return xDistance + yDistance;
}

canonical_original_code:
int manhattan_tile_distance(struct Tile *tileA, struct Tile *tileB) {
 int xDistance = tileA->x > tileB->x ? tileA->x - tileB->x : tileB->x - tileA->x;
 int yDistance = tileA->y > tileB->y ? tileA->y - tileB->y : tileB->y - tileA->y;
 return xDistance + yDistance;
}

variable_types:
tileB
struct Tile *

variable_types: xDistance int

variable_types: yDistance int

user_defined_types: struct Tile { unsigned char x; unsigned char y; short type; short direction; }


decompiled_code:
__int64 __fastcall generate_random_end_point(__int64 a1, unsigned __int8 *a2)
{
int v2; // ebx
unsigned __int8 v3; // r13
__int64 result; // rax
int v5; // eax
int v6; // ecx

generate_random_end_point:
v2 = rand() % (*(unsigned __int8 *)(a1 + 9) - 1);
v3 = rand() % (*(unsigned __int8 *)(a1 + 8) - 1);
do
{
v5 = a2[1];
if ( v3 >= v5 - 2 )
{
v6 = *a2;
if ( (unsigned __int8)v2 >= v6 - 2 && v3 <= v5 + 2 && (unsigned __int8)v2 <= v6 + 2 )
goto generate_random_end_point;
}
result = get_tile(a1, v2, v3);
}
while ( *(_WORD *)(result + 2) );
*(_WORD *)(result + 2) = 6;
*(_WORD *)(result + 4) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned __int8 *a2)
{
int v2; // ebx
unsigned __int8 v3; // r13
__int64 result; // rax
int v5; // eax
int v6; // ecx

generate_random_end_point:
v2 = rand() % (*(unsigned __int8 *)(a1 + 9) - 1);
v3 = rand() % (*(unsigned __int8 *)(a1 + 8) - 1);
do
{
v5 = a2[1];
if ( v3 >= v5 - 2 )
{
v6 = *a2;
if ( (unsigned __int8)v2 >= v6 - 2 && v3 <= v5 + 2 && (unsigned __int8)v2 <= v6 + 2 )
goto generate_random_end_point;
}
result = func8(a1, v2, v3);
}
while ( *(_WORD *)(result + 2) );
*(_WORD *)(result + 2) = 6;
*(_WORD *)(result + 4) = 1;
return result;
}

original_code:
void generate_random_end_point(struct Map *map, struct Tile* start) {
 uint8_t randX = rand() % (map->width - 1);
 uint8_t randY = rand() % (map->height - 1);
 struct Tile* endTile;
 do {
  while (!(randY < start->y - 2 || randX < start->x - 2 || randY > start->y + 2 || randX > start->x + 2)) {
   randX = rand() % (map->width - 1);
   randY = rand() % (map->height - 1);
  }
  endTile = get_tile(map, randX, randY);
 } while (endTile->type != BLANK);
 endTile->type = END;
 endTile->direction = UP;
}

canonical_original_code:
void generate_random_end_point(struct Map *map, struct Tile *start) {
 unsigned char randX = rand() % (map->width - 1);
 unsigned char randY = rand() % (map->height - 1);
 struct Tile *endTile;
 do {
  while (!(randY < start->y - 2 || randX < start->x - 2 || randY > start->y + 2 || randX > start->x + 2)) {
   randX = rand() % (map->width - 1);
   randY = rand() % (map->height - 1);
  }
  endTile = get_tile(map, randX, randY);
 } while (endTile->type != BLANK);
 endTile->type = END;
 endTile->direction = UP;
}

variable_types:
start
struct Tile *

variable_types:
randX
unsigned char

variable_types:
randY
unsigned char


decompiled_code:
__int64 __fastcall get_possible_successor_orientations(__int64 a1, int a2)
{
__int16 v2; // dx
__int64 result; // rax

v2 = *(_WORD *)(a1 + 2);
if ( v2 == 3 )
{
result = 10LL;
if ( a2 != 1 )
{
result = 5LL;
if ( a2 != 2 )
{
result = 6LL;
if ( a2 != 8 )
result = 9LL;
}
}
}
else if ( v2 == 4 )
{
result = 14LL;
if ( a2 != 1 )
{
result = 13LL;
if ( a2 != 2 )
{
result = 7LL;
if ( a2 != 8 )
result = 11LL;
}
}
}
else
{
result = 1LL;
if ( v2 == 2 )
result = ((a2 - 4) & 0xFFFFFFFB) == 0 ? 8 : 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2)
{
__int16 v2; // dx
__int64 result; // rax

v2 = *(_WORD *)(a1 + 2);
if ( v2 == 3 )
{
result = 10LL;
if ( a2 != 1 )
{
result = 5LL;
if ( a2 != 2 )
{
result = 6LL;
if ( a2 != 8 )
result = 9LL;
}
}
}
else if ( v2 == 4 )
{
result = 14LL;
if ( a2 != 1 )
{
result = 13LL;
if ( a2 != 2 )
{
result = 7LL;
if ( a2 != 8 )
result = 11LL;
}
}
}
else
{
result = 1LL;
if ( v2 == 2 )
result = ((a2 - 4) & 0xFFFFFFFB) == 0 ? 8 : 1;
}
return result;
}

original_code:
int get_possible_successor_orientations(struct Tile *succ, int coming_from_direction) {
 switch (succ->type) {
  case PIPE_STRAIGHT:
   if (coming_from_direction == LEFT || coming_from_direction == RIGHT) {
    return RIGHT;
   } else {
    return UP;
   }
  break;
  case PIPE_TURN:
   if (coming_from_direction == UP) {
    return RIGHT | DOWN;
   } else if (coming_from_direction == DOWN) {
    return LEFT | UP;
   } else if (coming_from_direction == RIGHT) {
    return LEFT | DOWN;
   } else if (coming_from_direction == LEFT) {
    return RIGHT | UP;
   }
  case PIPE_INTERSECT:
   if (coming_from_direction == UP) {
    return RIGHT | LEFT | DOWN;
   } else if (coming_from_direction == DOWN) {
    return RIGHT | LEFT | UP;
   } else if (coming_from_direction == RIGHT) {
    return DOWN | LEFT | UP;
   } else if (coming_from_direction == LEFT) {
    return DOWN | RIGHT | UP;
   }
  break;
  default:
   return UP;
 }
}


decompiled_code:
void get_successors()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void get_successors(struct Map *map, struct TileGroup *group, struct Tile *tile) {
 switch (tile->type) {
  case START:
  case PIPE_CROSS:
   if (tile->x - 1 >= 0) {
   }
   if (tile->x + 1 < map->width) {
   }
   if (tile->y - 1 >= 0) {
   }
   if (tile->y + 1 < map->height) {
   }
  break;
  case PIPE_STRAIGHT:
  break;
  case PIPE_TURN:
  break;
  case PIPE_INTERSECT:
  break;
 }
}

variable_types: struct TileGroup { struct Tile *tile; struct TileGroup *nextTile; } *

variable_types:
tile
struct Tile *


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

if ( !a2 )
return 0xFFFFFFFFLL;
result = *a1;
while ( result )
{
if ( *(_WORD *)result == *a2 && *(_WORD *)(result + 4) == a2[2] )
return 1LL;
}
return result;
}

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

if ( !a2 )
return 0xFFFFFFFFLL;
result = *a1;
while ( result )
{
if ( *(_WORD *)result == *a2 && *(_WORD *)(result + 4) == a2[2] )
return 1LL;
}
return result;
}

original_code:
int tile_is_in_group(struct TileGroup *path, struct Tile *tile) {
 if (tile == ((void *)0)) return -1;
 struct Tile *currentTile;
 while (currentTile = path->tile) {
  if (currentTile->x == tile->x && currentTile->y == tile->y && currentTile->direction == tile->direction) {
   return 1;
  }
 }
 return 0;
}

variable_types: struct TileGroup { struct Tile { unsigned char x; unsigned char y; short type; short direction; } *tile; struct TileGroup *nextTile; } *

user_defined_types: struct TileGroup { struct Tile *tile; struct TileGroup *nextTile; }


decompiled_code:
void __fastcall free_tile_group(void *ptr, int a2)
{
void **v3; // rbp

do
{
v3 = (void **)ptr;
ptr = (void *)*((_QWORD *)ptr + 1);
if ( a2 )
free(*v3);
free(v3);
}
while ( ptr );
}

canonical_decompiled_code:
void __fastcall func15(void *ptr, int a2)
{
void **v3; // rbp

do
{
v3 = (void **)ptr;
ptr = (void *)*((_QWORD *)ptr + 1);
if ( a2 )
free(*v3);
free(v3);
}
while ( ptr );
}

original_code:
void free_tile_group(struct TileGroup *group, int should_free_tiles) {
 struct TileGroup *nextGroup;
 do {
  nextGroup = group->nextTile;
  if (should_free_tiles) {
   free(group->tile);
  }
  free(group);
 } while (group = nextGroup);
}


original_code:
void generate_map(struct Map *map, int points) {
 unsigned short randX, randY;
 randX = rand() % (map->width - 1);
 randY = rand() % (map->height - 1);
 struct Tile* start = get_tile(map, randX, randY);
 start->type = START;
 start->direction = UP;
 while (points-- > 0) {
  generate_random_end_point(map, start);
 }
 uint8_t currentX, currentY;
 struct Tile* currentTilePointer;
 for (int i = 0; i < map->width * map->height; i++) {
  currentX = i % map->width, currentY = i / map->width;
  currentTilePointer = get_tile(map, currentX, currentY);
  if (currentTilePointer->type == BLANK) {
   int tileChoice = rand() % 9;
   if (tileChoice >= 0 && tileChoice <= 2) {
    currentTilePointer->type = PIPE_STRAIGHT;
   } else if (tileChoice >= 3 && tileChoice <= 5) {
    currentTilePointer->type = PIPE_TURN;
   } else if (tileChoice == 6 || tileChoice == 7) {
    currentTilePointer->type = PIPE_INTERSECT;
   } else if (tileChoice == 8) {
    currentTilePointer->type = PIPE_CROSS;
   }
   currentTilePointer->direction = 1 << rand() % 4;
  }
 }
}

canonical_original_code:
void generate_map(struct Map *map, int points) {
 unsigned short randX; unsigned short randY;
 randX = rand() % (map->width - 1);
 randY = rand() % (map->height - 1);
 struct Tile *start = get_tile(map, randX, randY);
 start->type = START;
 start->direction = UP;
 while (points-- > 0) {
  generate_random_end_point(map, start);
 }
 unsigned char currentX; unsigned char currentY;
 struct Tile *currentTilePointer;
 for (int i = 0; i < map->width * map->height; i++) {
  currentX = i % map->width, currentY = i / map->width;
  currentTilePointer = get_tile(map, currentX, currentY);
  if (currentTilePointer->type == BLANK) {
   int tileChoice = rand() % 9;
   if (tileChoice >= 0 && tileChoice <= 2) {
    currentTilePointer->type = PIPE_STRAIGHT;
   } else if (tileChoice >= 3 && tileChoice <= 5) {
    currentTilePointer->type = PIPE_TURN;
   } else if (tileChoice == 6 || tileChoice == 7) {
    currentTilePointer->type = PIPE_INTERSECT;
   } else if (tileChoice == 8) {
    currentTilePointer->type = PIPE_CROSS;
   }
   currentTilePointer->direction = 1 << rand() % 4;
  }
 }
}

variable_types: struct Map { struct Tile { unsigned char x; unsigned char y; short type; short direction; } *tiles; unsigned char height; } *

variable_types: points int

variable_types:
randX
unsigned short

variable_types:
randY
unsigned short

variable_types:
currentX
unsigned char

variable_types:
currentY
unsigned char

variable_types: tileChoice int


decompiled_code:
__int64 free_images()
{
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
return SDL_FreeSurface();
}

canonical_decompiled_code:
__int64 func18()
{
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
return SDL_FreeSurface();
}

original_code:
void free_images() {
 SDL_FreeSurface(tileVertical);
 SDL_FreeSurface(tileHorizontal);
 SDL_FreeSurface(tileStart);
 SDL_FreeSurface(tileEnd);
 SDL_FreeSurface(tileCross);
 SDL_FreeSurface(tileIntersectUp);
 SDL_FreeSurface(tileIntersectDown);
 SDL_FreeSurface(tileIntersectLeft);
 SDL_FreeSurface(tileIntersectRight);
 SDL_FreeSurface(tileTurnDownRight);
 SDL_FreeSurface(tileTurnDownLeft);
 SDL_FreeSurface(tileTurnUpLeft);
 SDL_FreeSurface(tileTurnUpRight);
}


decompiled_code:
__int64 image_draw_test()
{
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
return SDL_UpperBlit();
}

canonical_decompiled_code:
__int64 func19()
{
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
SDL_UpperBlit();
return SDL_UpperBlit();
}

original_code:
void image_draw_test() {
 {
  SDL_Rect tmprect = {0, 0, 20, 20};
  SDL_UpperBlit(tileHorizontal, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {20, 0, 20, 20};
  SDL_UpperBlit(tileVertical, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {40, 0, 20, 20};
  SDL_UpperBlit(tileStart, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {60, 0, 20, 20};
  SDL_UpperBlit(tileEnd, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {80, 0, 20, 20};
  SDL_UpperBlit(tileCross, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {100, 0, 20, 20};
  SDL_UpperBlit(tileIntersectUp, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {0, 20, 20, 20};
  SDL_UpperBlit(tileIntersectRight, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {20, 20, 20, 20};
  SDL_UpperBlit(tileIntersectDown, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {40, 20, 20, 20};
  SDL_UpperBlit(tileIntersectLeft, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {60, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownRight, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {80, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownLeft, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {100, 20, 20, 20};
  SDL_UpperBlit(tileTurnUpLeft, ((void *)0), screen, &tmprect);
 }
 {
  SDL_Rect tmprect = {0, 40, 20, 20};
  SDL_UpperBlit(tileTurnUpRight, ((void *)0), screen, &tmprect);
 }
}

canonical_original_code:
void image_draw_test() {
 {
  struct SDL_Rect tmprect = {0, 0, 20, 20};
  SDL_UpperBlit(tileHorizontal, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {20, 0, 20, 20};
  SDL_UpperBlit(tileVertical, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {40, 0, 20, 20};
  SDL_UpperBlit(tileStart, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {60, 0, 20, 20};
  SDL_UpperBlit(tileEnd, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {80, 0, 20, 20};
  SDL_UpperBlit(tileCross, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {100, 0, 20, 20};
  SDL_UpperBlit(tileIntersectUp, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {0, 20, 20, 20};
  SDL_UpperBlit(tileIntersectRight, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {20, 20, 20, 20};
  SDL_UpperBlit(tileIntersectDown, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {40, 20, 20, 20};
  SDL_UpperBlit(tileIntersectLeft, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {60, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownRight, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {80, 20, 20, 20};
  SDL_UpperBlit(tileTurnDownLeft, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {100, 20, 20, 20};
  SDL_UpperBlit(tileTurnUpLeft, ((void *)0), screen, &tmprect);
 }
 {
  struct SDL_Rect tmprect = {0, 40, 20, 20};
  SDL_UpperBlit(tileTurnUpRight, ((void *)0), screen, &tmprect);
 }
}

variable_types: struct SDL_Rect { short x; unsigned short w; }