decompiled_code:
__int64 session_get_next_free()
{
_DWORD *v0; // rdx
__int64 result; // rax

if ( nfds == 1 )
{
LABEL_5:
puts("error: file descriptor limit exceeded. use -f.");
exit(1);
}
v0 = (_DWORD *)(session + 64);
result = 0LL;
while ( *v0 )
{
++result;
v0 += 26;
if ( nfds - 1 == result )
goto LABEL_5;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
_DWORD *v0; // rdx
__int64 result; // rax

if ( nfds == 1 )
{
LABEL_5:
puts("error: file descriptor limit exceeded. use -f.");
exit(1);
}
v0 = (_DWORD *)(session + 64);
result = 0LL;
while ( *v0 )
{
++result;
v0 += 26;
if ( nfds - 1 == result )
goto LABEL_5;
}
return result;
}

original_code:
int session_get_next_free(void) {
 int i;
 for (i = 0; i < nfds - 1; ++i)
  if (session[i].used == 0) return i;
 printf("error: file descriptor limit exceeded. use -f.\n");
 exit(1);
 return -1;
}

canonical_original_code:
int session_get_next_free() {
 int i;
 for (i = 0; i < nfds - 1; ++i)
  if (session[i].used == 0) return i;
 printf("error: file descriptor limit exceeded. use -f.\n");
 exit(1);
 return -1;
}

variable_types: i int


repo: tillmannw/prowler

decompiled_code:
__int64 __fastcall session_delete(__int64 a1)
{
__int64 v1; // rbx
void *v2; // rdi
_QWORD *v3; // rbx
__int64 v4; // rax
__int64 result; // rax

v1 = 104 * a1;
v2 = *(void **)(session + 104 * a1 + 24);
if ( v2 )
free(v2);
v3 = (_QWORD *)(session + v1);
v4 = v3[12];
if ( v4 != -1 )
*(_DWORD *)(peerlist + 48 * v4 + 12) = 0;
*v3 = 0LL;
v3[12] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v3 - (((_DWORD)v3 + 8) & 0xFFFFFFF8) + 104) >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rbx
void *v2; // rdi
_QWORD *v3; // rbx
__int64 v4; // rax
__int64 result; // rax

v1 = 104 * a1;
v2 = *(void **)(session + 104 * a1 + 24);
if ( v2 )
free(v2);
v3 = (_QWORD *)(session + v1);
v4 = v3[12];
if ( v4 != -1 )
*(_DWORD *)(peerlist + 48 * v4 + 12) = 0;
*v3 = 0LL;
v3[12] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(v3 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v3 - (((_DWORD)v3 + 8) & 0xFFFFFFF8) + 104) >> 3));
return result;
}

original_code:
void session_delete(size_t i) {
 if (session[i].rx != ((void *)0)) free(session[i].rx);
 if (session[i].peerlist_index != -1)
  peerlist[session[i].peerlist_index].active = 0;
 memset(&session[i], 0, sizeof(session_t));
 return;
}

canonical_original_code:
void session_delete(unsigned long i) {
 if (session[i].rx != ((void *)0)) free(session[i].rx);
 if (session[i].peerlist_index != -1)
  peerlist[session[i].peerlist_index].active = 0;
 memset(&session[i], 0, sizeof(session_t));
 return;
}

variable_types:
i
unsigned long

return_type: void


decompiled_code:
__int64 __fastcall session_done(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbp

result = *(unsigned int *)(session + 104 * a1 + 72);
if ( (_DWORD)result )
{
shutdown(*(_DWORD *)(fds + 8 * a1), 0);
v2 = session + 104 * a1;
result = 0LL;
if ( !*(_QWORD *)(v2 + 56) && !*(_QWORD *)(v2 + 32) )
{
session_delete(a1);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbp

result = *(unsigned int *)(session + 104 * a1 + 72);
if ( (_DWORD)result )
{
shutdown(*(_DWORD *)(fds + 8 * a1), 0);
v2 = session + 104 * a1;
result = 0LL;
if ( !*(_QWORD *)(v2 + 56) && !*(_QWORD *)(v2 + 32) )
{
func4(a1);
result = 1LL;
}
}
return result;
}

original_code:
int session_done(size_t i) {
 if (session[i].closed_by_peer == 0) return 0;
 shutdown(fds[i].fd, SHUT_RD);
 if (session[i].txbytes || session[i].rxbytes) return 0;
 session_delete(i);
 return 1;
}

canonical_original_code:
int session_done(unsigned long i) {
 if (session[i].closed_by_peer == 0) return 0;
 shutdown(fds[i].fd, SHUT_RD);
 if (session[i].txbytes || session[i].rxbytes) return 0;
 session_delete(i);
 return 1;
}

return_type: int


decompiled_code:
__int64 __fastcall session_last_action(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 88);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 88);
}

original_code:
time_t session_last_action(size_t i) {
 return session[i].last_action;
}

canonical_original_code:
long session_last_action(unsigned long i) {
 return session[i].last_action;
}

return_type: long


decompiled_code:
__int64 __fastcall session_start_time(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 80);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(_QWORD *)(session + 104 * a1 + 80);
}

original_code:
time_t session_start_time(size_t i) {
 return session[i].session_start;
}

canonical_original_code:
long session_start_time(unsigned long i) {
 return session[i].session_start;
}


decompiled_code:
ssize_t __fastcall session_receive(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // r12
time_t v3; // rax
__int64 v4; // r12
void *v5; // rax
ssize_t v6; // rax
ssize_t v7; // rbp

v1 = 104 * a1;
v2 = session + 104 * a1;
v3 = time(0LL);
*(_QWORD *)(v2 + 88) = v3;
if ( v3 == -1 )
{
perror("time()");
exit(1);
}
v4 = session + v1;
v5 = realloc(*(void **)(session + v1 + 24), *(_QWORD *)(session + v1 + 32) + 0x2000LL);
*(_QWORD *)(v4 + 24) = v5;
if ( !v5 )
{
perror("realloc()");
exit(1);
}
v6 = recv(
*(_DWORD *)(fds + 8 * a1),
(void *)(*(_QWORD *)(session + v1 + 24) + *(_QWORD *)(session + v1 + 32)),
0x2000uLL,
0);
v7 = v6;
if ( v6 == -1 )
{
if ( *_errno_location() != 11 )
{
perror("recv()");
return v7;
}
}
else if ( !v6 )
{
*(_DWORD *)(session + v1 + 72) = 1;
*(_WORD *)(fds + 8 * a1 + 4) &= 0xFFFEu;
return v7;
}
*(_QWORD *)(session + v1 + 32) += v7;
return v7;
}

original_code:
ssize_t session_receive(size_t i) {
 ssize_t bytes;
 if ((session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 if ((session[i].rx = realloc(session[i].rx, session[i].rxbytes + 8192)) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 switch ((bytes = recv(fds[i].fd, session[i].rx + session[i].rxbytes, 8192, 0))) {
 case -1:
  if ((*__errno_location ()) == 11) break;
  perror("recv()");
  return -1;
 case 0:
  session[i].closed_by_peer = 1;
  fds[i].events &= ~0x001;
  return 0;
 default:
  break;
 }
 session[i].rxbytes += bytes;
 return bytes;
}

canonical_original_code:
long session_receive(unsigned long i) {
 long bytes;
 if ((session[i].last_action = time(((void *)0))) == -1) {
  perror("time()");
  exit(1);
 }
 if ((session[i].rx = realloc(session[i].rx, session[i].rxbytes + 8192)) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 switch ((bytes = recv(fds[i].fd, session[i].rx + session[i].rxbytes, 8192, 0))) {
 case -1:
  if ((*__errno_location ()) == 11) break;
  perror("recv()");
  return -1;
 case 0:
  session[i].closed_by_peer = 1;
  fds[i].events &= ~0x001;
  return 0;
 default:
  break;
 }
 session[i].rxbytes += bytes;
 return bytes;
}

variable_types: bytes long


decompiled_code:
__int64 __fastcall session_prepare_for_send(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v3; // rax
__int64 result; // rax

v3 = (_QWORD *)(session + 104 * a1);
v3[5] = a2;
v3[7] = a3;
v3[6] = 0LL;
result = fds;
*(_WORD *)(fds + 8 * a1 + 4) |= 4u;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v3; // rax
__int64 result; // rax

v3 = (_QWORD *)(session + 104 * a1);
v3[5] = a2;
v3[7] = a3;
v3[6] = 0LL;
result = fds;
*(_WORD *)(fds + 8 * a1 + 4) |= 4u;
return result;
}

original_code:
void session_prepare_for_send(size_t i, unsigned char *data, size_t len) {
 session[i].tx = data;
 session[i].txbytes = len;
 session[i].txoff = 0;
 fds[i].events |= 0x004;
 return;
}

canonical_original_code:
void session_prepare_for_send(unsigned long i, unsigned char *data, unsigned long len) {
 session[i].tx = data;
 session[i].txbytes = len;
 session[i].txoff = 0;
 fds[i].events |= 0x004;
 return;
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned long


original_code:
ssize_t session_send(size_t i) {
 ssize_t bytes;
 if (session[i].tx == ((void *)0) || session[i].txbytes == 0) {
  return 0;
 }
 if ((bytes = send(fds[i].fd, session[i].tx + session[i].txoff, session[i].txbytes, 0)) == -1) {
  if ((*__errno_location ()) != 11) {
   perror("send()");
   return -1;
  }
 }
 memmove(session[i].tx, session[i].tx + bytes, session[i].txbytes - bytes);
 session[i].txoff += bytes;
 session[i].txbytes -= bytes;
 if (session[i].txbytes == 0) { fds[i].events &= ~0x004; }
 return bytes;
}

canonical_original_code:
long session_send(unsigned long i) {
 long bytes;
 if (session[i].tx == ((void *)0) || session[i].txbytes == 0) {
  return 0;
 }
 if ((bytes = send(fds[i].fd, session[i].tx + session[i].txoff, session[i].txbytes, 0)) == -1) {
  if ((*__errno_location ()) != 11) {
   perror("send()");
   return -1;
  }
 }
 memmove(session[i].tx, session[i].tx + bytes, session[i].txbytes - bytes);
 session[i].txoff += bytes;
 session[i].txbytes -= bytes;
 if (session[i].txbytes == 0) { fds[i].events &= ~0x004; }
 return bytes;
}


decompiled_code:
void __fastcall peer_add(int a1, __int16 a2, __int64 a3)
{
_DWORD *v4; // rax
__int64 v5; // rdx
void *v6; // rax
__int64 v7; // rdx
char *v8; // rax

if ( a1 && a2 )
{
if ( peerlist_size )
{
v4 = peerlist;
v5 = 0LL;
while ( *v4 != a1 )
{
++v5;
v4 += 12;
if ( v5 == peerlist_size )
goto LABEL_7;
}
}
else
{
LABEL_7:
v6 = realloc(peerlist, 16 * (3 * peerlist_size + 3));
peerlist = v6;
if ( !v6 )
{
perror("realloc()");
exit(1);
}
*((_DWORD *)v6 + 12 * peerlist_size) = a1;
*((_WORD *)peerlist + 24 * peerlist_size + 2) = a2;
v7 = peerlist_size;
v8 = (char *)peerlist + 48 * peerlist_size;
*((_QWORD *)v8 + 5) = a3;
*((_QWORD *)v8 + 4) = 0LL;
*((_DWORD *)v8 + 3) = 0;
*((_DWORD *)v8 + 2) = 0;
*((_QWORD *)v8 + 2) = v7;
peerlist_size = v7 + 1;
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, __int16 a2, __int64 a3)
{
_DWORD *v4; // rax
__int64 v5; // rdx
void *v6; // rax
__int64 v7; // rdx
char *v8; // rax

if ( a1 && a2 )
{
if ( peerlist_size )
{
v4 = peerlist;
v5 = 0LL;
while ( *v4 != a1 )
{
++v5;
v4 += 12;
if ( v5 == peerlist_size )
goto LABEL_7;
}
}
else
{
LABEL_7:
v6 = realloc(peerlist, 16 * (3 * peerlist_size + 3));
peerlist = v6;
if ( !v6 )
{
perror("realloc()");
exit(1);
}
*((_DWORD *)v6 + 12 * peerlist_size) = a1;
*((_WORD *)peerlist + 24 * peerlist_size + 2) = a2;
v7 = peerlist_size;
v8 = (char *)peerlist + 48 * peerlist_size;
*((_QWORD *)v8 + 5) = a3;
*((_QWORD *)v8 + 4) = 0LL;
*((_DWORD *)v8 + 3) = 0;
*((_DWORD *)v8 + 2) = 0;
*((_QWORD *)v8 + 2) = v7;
peerlist_size = v7 + 1;
}
}
}

original_code:
void peer_add(u_int32_t ipaddr, u_int16_t port, void *data) {
 size_t i;
 if (!ipaddr || !port) return;
 for (i = 0; i < peerlist_size; ++i) {
  if (ipaddr == peerlist[i].ipaddr) return;
 }
 if ((peerlist = realloc(peerlist, (peerlist_size + 1) * sizeof(peer_t))) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 peerlist[peerlist_size].ipaddr = ipaddr;
 peerlist[peerlist_size].port = port;
 peerlist[peerlist_size].data = data;
 peerlist[peerlist_size].last_contact = 0;
 peerlist[peerlist_size].active = 0;
 peerlist[peerlist_size].unresponsive = 0;
 peerlist[peerlist_size].index = peerlist_size;
 peerlist_size++;
 return;
}

canonical_original_code:
void peer_add(unsigned int ipaddr, unsigned int port, void *data) {
 unsigned long i;
 if (!ipaddr || !port) return;
 for (i = 0; i < peerlist_size; ++i) {
  if (ipaddr == peerlist[i].ipaddr) return;
 }
 if ((peerlist = realloc(peerlist, (peerlist_size + 1) * sizeof(peer_t))) == ((void *)0)) {
  perror("realloc()");
  exit(1);
 }
 peerlist[peerlist_size].ipaddr = ipaddr;
 peerlist[peerlist_size].port = port;
 peerlist[peerlist_size].data = data;
 peerlist[peerlist_size].last_contact = 0;
 peerlist[peerlist_size].active = 0;
 peerlist[peerlist_size].unresponsive = 0;
 peerlist[peerlist_size].index = peerlist_size;
 peerlist_size++;
 return;
}

variable_types:
ipaddr
unsigned int

variable_types:
port
unsigned int

variable_types:
data
void *


decompiled_code:
__int64 peerlist_process()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0xFFFFFFFFLL;
}

original_code:
int peerlist_process(size_t peerlist_index, unsigned char *msg, size_t size) {
 return -1;
}

canonical_original_code:
int peerlist_process(unsigned long peerlist_index, unsigned char *msg, unsigned long size) {
 return -1;
}

variable_types:
msg
unsigned char *

variable_types:
size
unsigned long


sigh

decompiled_code:
void __fastcall sigh(int a1)
{
unsigned __int64 i; // rbx
const char *v2; // rax

switch ( a1 )
{
case 10:
ctlevents |= 2u;
if ( write(*((_DWORD *)&pfd + 1), &unk_154, 1uLL) == -1 )
perror("write()");
break;
case 14:
ctlevents |= 1u;
if ( write(*((_DWORD *)&pfd + 1), &unk_154, 1uLL) == -1 )
perror("write()");
alarm(1u);
break;
case 2:
for ( i = 0LL; nfds > i; ++i )
close(*((_DWORD *)fds + 2 * i));
free(fds);
free(session);
v2 = (const char *)timestr();
fprintf(logstd, "\r[%s] -- terminated --\n", v2);
fclose(logstd);
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
unsigned __int64 i; // rbx
const char *v2; // rax

switch ( a1 )
{
case 10:
ctlevents |= 2u;
if ( write(*((_DWORD *)&pfd + 1), &unk_154, 1uLL) == -1 )
perror("write()");
break;
case 14:
ctlevents |= 1u;
if ( write(*((_DWORD *)&pfd + 1), &unk_154, 1uLL) == -1 )
perror("write()");
alarm(1u);
break;
case 2:
for ( i = 0LL; nfds > i; ++i )
close(*((_DWORD *)fds + 2 * i));
free(fds);
free(session);
v2 = (const char *)timestr();
fprintf(logstd, "\r[%s] -- terminated --\n", v2);
fclose(logstd);
exit(0);
}
}

original_code:
void sigh(int s) {
 nfds_t i;
 switch (s) {
 case 10:
  ctlevents |= 2;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  break;
 case 14:
  ctlevents |= 1;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  alarm(1);
  break;
 case 2:
  for (i = 0; i < nfds; ++i) close(fds[i].fd);
  free(fds);
  free(session);
  fprintf(logstd, "\r[%s] -- terminated --\n", timestr());
  fclose(logstd);
  exit(0);
 default:
  break;
 }
 return;
}

canonical_original_code:
void sigh(int s) {
 unsigned long i;
 switch (s) {
 case 10:
  ctlevents |= 2;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  break;
 case 14:
  ctlevents |= 1;
  if (write(pfd[1], "\x01", 1) == -1) {
   perror("write()");
  }
  alarm(1);
  break;
 case 2:
  for (i = 0; i < nfds; ++i) close(fds[i].fd);
  free(fds);
  free(session);
  fprintf(logstd, "\r[%s] -- terminated --\n", timestr());
  fclose(logstd);
  exit(0);
 default:
  break;
 }
 return;
}

variable_types: s int


decompiled_code:
_DWORD *img_alloc()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x18uLL);
v1 = v0;
if ( v0 )
{
v0[2] = 0;
v0[3] = 0;
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 2) = 0LL;
}
else
{
puts("IMGALLOC: Couldn't allocate image structure");
}
return v1;
}

canonical_decompiled_code:
_DWORD *func1()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x18uLL);
v1 = v0;
if ( v0 )
{
v0[2] = 0;
v0[3] = 0;
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 2) = 0LL;
}
else
{
puts("IMGALLOC: Couldn't allocate image structure");
}
return v1;
}

original_code:
IMAGE *img_alloc()
{
  IMAGE *new;
  new = (IMAGE *) malloc (sizeof (IMAGE));
  if (new == ((void *)0)) {
    printf("IMGALLOC: Couldn't allocate image structure\n");
    return (((void *)0));
  }
  new->rows = 0;
  new->cols = 0;
  new->name = ((void *)0);
  new->data = ((void *)0);
  return (new);
}

canonical_original_code:
struct IMAGE *img_alloc()
{
  struct IMAGE *new;
  new = (struct IMAGE *) malloc (sizeof (IMAGE));
  if (new == ((void *)0)) {
    printf("IMGALLOC: Couldn't allocate image structure\n");
    return (((void *)0));
  }
  new->rows = 0;
  new->cols = 0;
  new->name = ((void *)0);
  new->data = ((void *)0);
  return (new);
}

variable_types: struct IMAGE { char *name; int rows; int *data; } *


decompiled_code:
_QWORD *imgl_alloc()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = malloc(0x10uLL);
v1 = v0;
if ( v0 )
{
*(_DWORD *)v0 = 0;
v0[1] = 0LL;
}
else
{
puts("IMGL_ALLOC: Couldn't allocate image list");
}
return v1;
}

canonical_decompiled_code:
_QWORD *func6()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx

v0 = malloc(0x10uLL);
v1 = v0;
if ( v0 )
{
*(_DWORD *)v0 = 0;
v0[1] = 0LL;
}
else
{
puts("IMGL_ALLOC: Couldn't allocate image list");
}
return v1;
}

original_code:
IMAGELIST *imgl_alloc()
{
  IMAGELIST *new;
  new = (IMAGELIST *) malloc (sizeof (IMAGELIST));
  if (new == ((void *)0)) {
    printf("IMGL_ALLOC: Couldn't allocate image list\n");
    return(((void *)0));
  }
  new->n = 0;
  new->list = ((void *)0);
  return (new);
}

canonical_original_code:
struct IMAGELIST *imgl_alloc()
{
  struct IMAGELIST *new;
  new = (struct IMAGELIST *) malloc (sizeof (IMAGELIST));
  if (new == ((void *)0)) {
    printf("IMGL_ALLOC: Couldn't allocate image list\n");
    return(((void *)0));
  }
  new->n = 0;
  new->list = ((void *)0);
  return (new);
}

variable_types: struct IMAGELIST { int n; struct IMAGE { char *name; int rows; int *data; } **list; } *

user_defined_types: struct IMAGELIST { int n; struct IMAGE **list; }


decompiled_code:
_DWORD *alps_serio_alloc()
{
_DWORD *result; // rax

result = calloc(1uLL, 0x18uLL);
if ( result )
result[4] = -1;
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *result; // rax

result = calloc(1uLL, 0x18uLL);
if ( result )
result[4] = -1;
return result;
}

original_code:
static struct alps_serio_dev *alps_serio_alloc(void)
{
 struct alps_serio_dev *dev;
 dev = calloc(1, sizeof(*dev));
 if (!dev)
  return ((void *)0);
 dev->serio_fd = -1;
 return dev;
}

canonical_original_code:
static struct alps_serio_dev *alps_serio_alloc()
{
 struct alps_serio_dev *dev;
 dev = calloc(1, sizeof(*dev));
 if (!dev)
  return ((void *)0);
 dev->serio_fd = -1;
 return dev;
}

variable_types: struct alps_serio_dev { int proto_version; char *serio_drvctl_path; int serio_fd; } *


decompiled_code:
__int64 __fastcall do_write(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
ssize_t v5; // rax

v4 = 0LL;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return 0xFFFFFFFFLL;
}
else
{
if ( !v5 )
{
puts("Unexpected end of file");
return 0xFFFFFFFFLL;
}
v4 += v5;
}
}
while ( v4 < a3 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
ssize_t v5; // rax

v4 = 0LL;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return 0xFFFFFFFFLL;
}
else
{
if ( !v5 )
{
puts("Unexpected end of file");
return 0xFFFFFFFFLL;
}
v4 += v5;
}
}
while ( v4 < a3 );
return 0LL;
}

original_code:
static int do_write(int fd, const void *buf, size_t count)
{
 size_t bytes_written = 0;
 do {
  size_t ret = write(fd, (const char *)buf + bytes_written,
       count - bytes_written);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_written += ret;
 } while (bytes_written < count);
 return 0;
}

canonical_original_code:
static int do_write(int fd, void *buf, unsigned long count)
{
 unsigned long bytes_written = 0;
 do {
  unsigned long ret = write(fd, (const char *)buf + bytes_written,
       count - bytes_written);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_written += ret;
 } while (bytes_written < count);
 return 0;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types:
ret
unsigned long


decompiled_code:
int __fastcall serio_mouse_deinit(__int64 a1)
{
int v2; // edi
int result; // eax
int v4; // ebx

v2 = *(_DWORD *)(a1 + 16);
if ( v2 != -1 )
{
close(v2);
*(_DWORD *)(a1 + 16) = -1;
}
result = open(*(const char **)(a1 + 8), 1);
v4 = result;
if ( result != -1 )
{
write(result, "psmouse", 7uLL);
result = close(v4);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
int v2; // edi
int result; // eax
int v4; // ebx

v2 = *(_DWORD *)(a1 + 16);
if ( v2 != -1 )
{
close(v2);
*(_DWORD *)(a1 + 16) = -1;
}
result = open(*(const char **)(a1 + 8), 1);
v4 = result;
if ( result != -1 )
{
write(result, "psmouse", 7uLL);
result = close(v4);
}
return result;
}

original_code:
static void serio_mouse_deinit(struct alps_serio_dev *dev)
{
 int fd;
 if (dev->serio_fd != -1) {
  close(dev->serio_fd);
  dev->serio_fd = -1;
 }
 fd = open(dev->serio_drvctl_path, 01);
 if (fd != -1) {
  write(fd, "psmouse", 7);
  close(fd);
 }
}

user_defined_types: struct alps_serio_dev { int proto_version; char *serio_drvctl_path; int serio_fd; }


decompiled_code:
__int64 __fastcall serio_mouse_init(__int64 a1)
{
char *v1; // rax
char *v2; // r12
int v3; // ebx
__int64 v4; // rbp
int *v5; // rax
char *v6; // rax
int v7; // eax
int v8; // ebx
int v9; // eax
unsigned int v10; // ebp
int *v11; // rax
char *v12; // rax
char *v13; // rax
int v14; // eax
__int64 v16[8]; // [rsp+0h] [rbp-B8h] BYREF
glob_t pglob; // [rsp+40h] [rbp-78h] BYREF

v1 = (char *)malloc(0x1000uLL);
if ( !v1 )
return (unsigned int)-1;
v2 = v1;
pglob.gl_offs = 0LL;
v3 = glob("/sys/bus/serio/devices/serio*", 8199, 0LL, &pglob);
if ( v3 )
goto LABEL_24;
if ( !pglob.gl_pathc )
{
v3 = -1;
goto LABEL_19;
}
v4 = 0LL;
while ( 1 )
{
v16[0] = 0LL;
v16[1] = 0LL;
v16[2] = 0LL;
v16[3] = 0LL;
v16[4] = 0LL;
v16[5] = 0LL;
v16[6] = 0LL;
v16[7] = 0LL;
snprintf(v2, 0x1000uLL, "%sdescription", pglob.gl_pathv[v4]);
v7 = open(v2, 0);
v8 = v7;
if ( v7 != -1 )
break;
v5 = _errno_location();
v6 = strerror(*v5);
printf("Could not open %s for reading: %s\n", v2, v6);
LABEL_8:
if ( ++v4 >= pglob.gl_pathc )
{
v3 = -1;
goto LABEL_19;
}
}
if ( read(v7, v16, 0x40uLL) <= 0 )
{
printf("Could not read from %s\n", v2);
LABEL_7:
close(v8);
goto LABEL_8;
}
if ( strncmp((const char *)v16, "i8042 AUX port", 0xEuLL) )
goto LABEL_7;
printf("Found serio mouse at %s\n", pglob.gl_pathv[v4]);
close(v8);
snprintf(v2, 0x1000uLL, "%sdrvctl", pglob.gl_pathv[v4]);
v9 = open(v2, 1);
v10 = v9;
if ( v9 == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
printf("Could not open %s for writing: %s\n", v2, v12);
return v10;
}
if ( write(v9, "serio_raw", 9uLL) == 9 )
{
v13 = strdup(v2);
*(_QWORD *)(a1 + 8) = v13;
v3 = -(v13 == 0LL);
}
else
{
printf("Error writing to %s\n", v2);
v3 = -1;
}
close(v10);
LABEL_19:
if ( *(_QWORD *)(a1 + 8) )
{
globfree(&pglob);
v3 = glob("/dev/serio_raw*", 1, 0LL, &pglob);
if ( v3 )
{
puts("Could not find serio_raw device node");
}
else
{
v14 = open(*(const char **)pglob.gl_pathv, 2);
if ( v14 == -1 )
{
printf("Failed to open device node %s\n", *(const char **)pglob.gl_pathv);
v3 = -1;
}
else
{
*(_DWORD *)(a1 + 16) = v14;
}
}
}
globfree(&pglob);
LABEL_24:
free(v2);
return (unsigned int)-(v3 != 0);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
char *v1; // rax
char *v2; // r12
int v3; // ebx
__int64 v4; // rbp
int *v5; // rax
char *v6; // rax
int v7; // eax
int v8; // ebx
int v9; // eax
unsigned int v10; // ebp
int *v11; // rax
char *v12; // rax
char *v13; // rax
int v14; // eax
__int64 v16[8]; // [rsp+0h] [rbp-B8h] BYREF
glob_t pglob; // [rsp+40h] [rbp-78h] BYREF

v1 = (char *)malloc(0x1000uLL);
if ( !v1 )
return (unsigned int)-1;
v2 = v1;
pglob.gl_offs = 0LL;
v3 = glob("/sys/bus/serio/devices/serio*", 8199, 0LL, &pglob);
if ( v3 )
goto LABEL_24;
if ( !pglob.gl_pathc )
{
v3 = -1;
goto LABEL_19;
}
v4 = 0LL;
while ( 1 )
{
v16[0] = 0LL;
v16[1] = 0LL;
v16[2] = 0LL;
v16[3] = 0LL;
v16[4] = 0LL;
v16[5] = 0LL;
v16[6] = 0LL;
v16[7] = 0LL;
snprintf(v2, 0x1000uLL, "%sdescription", pglob.gl_pathv[v4]);
v7 = open(v2, 0);
v8 = v7;
if ( v7 != -1 )
break;
v5 = _errno_location();
v6 = strerror(*v5);
printf("Could not open %s for reading: %s\n", v2, v6);
LABEL_8:
if ( ++v4 >= pglob.gl_pathc )
{
v3 = -1;
goto LABEL_19;
}
}
if ( read(v7, v16, 0x40uLL) <= 0 )
{
printf("Could not read from %s\n", v2);
LABEL_7:
close(v8);
goto LABEL_8;
}
if ( strncmp((const char *)v16, "i8042 AUX port", 0xEuLL) )
goto LABEL_7;
printf("Found serio mouse at %s\n", pglob.gl_pathv[v4]);
close(v8);
snprintf(v2, 0x1000uLL, "%sdrvctl", pglob.gl_pathv[v4]);
v9 = open(v2, 1);
v10 = v9;
if ( v9 == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
printf("Could not open %s for writing: %s\n", v2, v12);
return v10;
}
if ( write(v9, "serio_raw", 9uLL) == 9 )
{
v13 = strdup(v2);
*(_QWORD *)(a1 + 8) = v13;
v3 = -(v13 == 0LL);
}
else
{
printf("Error writing to %s\n", v2);
v3 = -1;
}
close(v10);
LABEL_19:
if ( *(_QWORD *)(a1 + 8) )
{
globfree(&pglob);
v3 = glob("/dev/serio_raw*", 1, 0LL, &pglob);
if ( v3 )
{
puts("Could not find serio_raw device node");
}
else
{
v14 = open(*(const char **)pglob.gl_pathv, 2);
if ( v14 == -1 )
{
printf("Failed to open device node %s\n", *(const char **)pglob.gl_pathv);
v3 = -1;
}
else
{
*(_DWORD *)(a1 + 16) = v14;
}
}
}
globfree(&pglob);
LABEL_24:
free(v2);
return (unsigned int)-(v3 != 0);
}

original_code:
static int serio_mouse_init(struct alps_serio_dev *dev)
{
 static const char serio_mouse_desc[] = "i8042 AUX port";
 char *path;
 glob_t globbuf;
 int ret;
 int i;
 int fd;
 path = malloc(4096);
 if (!path)
  return -1;
 globbuf.gl_offs = 0;
 ret = glob("/sys/bus/serio/devices/serio*",
     (1 << 0)|(1 << 1)|(1 << 2)|(1 << 13), ((void *)0), &globbuf);
 if (ret)
  goto free_path;
 ret = -1;
 for (i = 0; i < globbuf.gl_pathc; i++) {
  char read_buf[64] = {0,};
  do { if (verbose) printf("Checking path %s\n", globbuf.gl_pathv[i]); } while (0);
  snprintf(path, 4096, "%sdescription", globbuf.gl_pathv[i]);
  fd = open(path, 00);
  if (fd == -1) {
   printf("Could not open %s for reading: %s\n",
          path, strerror((*__errno_location ())));
   continue;
  }
  if (read(fd, read_buf, sizeof(read_buf)) <= 0) {
   printf("Could not read from %s\n", path);
  } else if (!strncmp(read_buf, serio_mouse_desc,
        sizeof(serio_mouse_desc) - 1)) {
   printf("Found serio mouse at %s\n", globbuf.gl_pathv[i]);
   close(fd);
   snprintf(path, 4096, "%sdrvctl", globbuf.gl_pathv[i]);
   fd = open(path, 01);
   if (fd == -1) {
    printf("Could not open %s for writing: %s\n",
           path, strerror((*__errno_location ())));
    return -1;
   }
   if (write(fd, "serio_raw", 9) != 9) {
    printf("Error writing to %s\n", path);
   } else {
    dev->serio_drvctl_path = strdup(path);
    if (dev->serio_drvctl_path)
     ret = 0;
   }
   close(fd);
   break;
  }
  close(fd);
 }
 if (dev->serio_drvctl_path) {
  globfree(&globbuf);
  ret = glob("/dev/serio_raw*", (1 << 0), ((void *)0), &globbuf);
  if (ret) {
   printf("Could not find serio_raw device node\n");
   goto free_glob;
  }
  ret = open(globbuf.gl_pathv[0], 02);
  if (ret != -1) {
   dev->serio_fd = ret;
   ret = 0;
  } else {
   printf("Failed to open device node %s\n",
    globbuf.gl_pathv[0]);
  }
 }
free_glob:
 globfree(&globbuf);
free_path:
 free(path);
 return ret ? -1 : 0;
}

canonical_original_code:
static int serio_mouse_init(struct alps_serio_dev *dev)
{
 static char serio_mouse_desc[] = "i8042 AUX port";
 char *path;
 struct glob_t globbuf;
 int ret;
 int i;
 int fd;
 path = malloc(4096);
 if (!path)
  return -1;
 globbuf.gl_offs = 0;
 ret = glob("/sys/bus/serio/devices/serio*",
     (1 << 0)|(1 << 1)|(1 << 2)|(1 << 13), ((void *)0), &globbuf);
 if (ret)
  goto free_path;
 ret = -1;
 for (i = 0; i < globbuf.gl_pathc; i++) {
  char read_buf[64] = {0,};
  do { if (verbose) printf("Checking path %s\n", globbuf.gl_pathv[i]); } while (0);
  snprintf(path, 4096, "%sdescription", globbuf.gl_pathv[i]);
  fd = open(path, 00);
  if (fd == -1) {
   printf("Could not open %s for reading: %s\n",
          path, strerror((*__errno_location ())));
   continue;
  }
  if (read(fd, read_buf, sizeof(read_buf)) <= 0) {
   printf("Could not read from %s\n", path);
  } else if (!strncmp(read_buf, serio_mouse_desc,
        sizeof(serio_mouse_desc) - 1)) {
   printf("Found serio mouse at %s\n", globbuf.gl_pathv[i]);
   close(fd);
   snprintf(path, 4096, "%sdrvctl", globbuf.gl_pathv[i]);
   fd = open(path, 01);
   if (fd == -1) {
    printf("Could not open %s for writing: %s\n",
           path, strerror((*__errno_location ())));
    return -1;
   }
   if (write(fd, "serio_raw", 9) != 9) {
    printf("Error writing to %s\n", path);
   } else {
    dev->serio_drvctl_path = strdup(path);
    if (dev->serio_drvctl_path)
     ret = 0;
   }
   close(fd);
   break;
  }
  close(fd);
 }
 if (dev->serio_drvctl_path) {
  globfree(&globbuf);
  ret = glob("/dev/serio_raw*", (1 << 0), ((void *)0), &globbuf);
  if (ret) {
   printf("Could not find serio_raw device node\n");
   goto free_glob;
  }
  ret = open(globbuf.gl_pathv[0], 02);
  if (ret != -1) {
   dev->serio_fd = ret;
   ret = 0;
  } else {
   printf("Failed to open device node %s\n",
    globbuf.gl_pathv[0]);
  }
 }
free_glob:
 globfree(&globbuf);
free_path:
 free(path);
 return ret ? -1 : 0;
}

variable_types:
path
char *

variable_types: struct glob_t { unsigned long gl_pathc; char **gl_pathv; unsigned long gl_offs; int gl_flags; void (*gl_closedir)(void *); void *(*gl_readdir)(void *); void *(*gl_opendir)(char *); int (*gl_lstat)(char *, void *); int (*gl_stat)(char *, void *); }

variable_types: ret int

variable_types: i int


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

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

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

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

original_code:
static void alps_serio_free(struct alps_serio_dev *dev)
{
 if (dev->serio_drvctl_path)
  free(dev->serio_drvctl_path);
 free(dev);
}


decompiled_code:
int __fastcall ps2_drain(__int64 a1)
{
int v1; // eax
__int64 v2; // rbp
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = fcntl(*(_DWORD *)(a1 + 16), 3);
if ( v1 == -1 )
return puts("Could not get serio_fd flags, device will not be drained");
v2 = v1;
BYTE1(v1) |= 8u;
if ( fcntl(*(_DWORD *)(a1 + 16), 4, v1) == -1 )
return puts("Could not set serio_fd flags, device will not be drained");
while ( read(*(_DWORD *)(a1 + 16), buf, 1uLL) == 1 )
;
result = fcntl(*(_DWORD *)(a1 + 16), 4, v2);
if ( result )
result = puts("Error restoring serio_fd flags, left in nonblock mode");
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
int v1; // eax
__int64 v2; // rbp
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v1 = fcntl(*(_DWORD *)(a1 + 16), 3);
if ( v1 == -1 )
return puts("Could not get serio_fd flags, device will not be drained");
v2 = v1;
BYTE1(v1) |= 8u;
if ( fcntl(*(_DWORD *)(a1 + 16), 4, v1) == -1 )
return puts("Could not set serio_fd flags, device will not be drained");
while ( read(*(_DWORD *)(a1 + 16), buf, 1uLL) == 1 )
;
result = fcntl(*(_DWORD *)(a1 + 16), 4, v2);
if ( result )
result = puts("Error restoring serio_fd flags, left in nonblock mode");
return result;
}

original_code:
static void ps2_drain(struct alps_serio_dev *dev)
{
 unsigned char byte;
 long flags;
 flags = fcntl(dev->serio_fd, 3);
 if (flags == -1) {
  printf("Could not get serio_fd flags, device will not be drained\n");
  return;
 }
 if (fcntl(dev->serio_fd, 4, flags | 04000) == -1) {
  printf("Could not set serio_fd flags, device will not be drained\n");
  return;
 }
 while (read(dev->serio_fd, &byte, 1) == 1)
  ;
 if (fcntl(dev->serio_fd, 4, flags))
  printf("Error restoring serio_fd flags, left in nonblock mode\n");
}

variable_types:
byte
unsigned char

variable_types: flags long


decompiled_code:
__int64 __fastcall do_read(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
unsigned int v5; // eax
unsigned int v6; // er14
struct pollfd fds; // [rsp+8h] [rbp-30h] BYREF

fds.fd = fd;
fds.events = 1;
v4 = 0LL;
while ( (unsigned int)(poll(&fds, 1uLL, 500) + 1) > 1 )
{
v5 = read(fd, (void *)(a2 + v4), a3 - v4);
v6 = v5;
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return v6;
}
else
{
if ( !v5 )
{
puts("Unexpected end of file");
return (unsigned int)-1;
}
v4 += (int)v5;
}
if ( v4 >= a3 )
return 0;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbx
unsigned int v5; // eax
unsigned int v6; // er14
struct pollfd fds; // [rsp+8h] [rbp-30h] BYREF

fds.fd = fd;
fds.events = 1;
v4 = 0LL;
while ( (unsigned int)(poll(&fds, 1uLL, 500) + 1) > 1 )
{
v5 = read(fd, (void *)(a2 + v4), a3 - v4);
v6 = v5;
if ( v5 == -1 )
{
if ( *_errno_location() != 11 )
return v6;
}
else
{
if ( !v5 )
{
puts("Unexpected end of file");
return (unsigned int)-1;
}
v4 += (int)v5;
}
if ( v4 >= a3 )
return 0;
}
return (unsigned int)-1;
}

original_code:
static int do_read(int fd, void *buf, size_t count)
{
 size_t bytes_read = 0;
 struct pollfd pfd;
 int ret;
 pfd.fd = fd;
 pfd.events = 0x001;
 do {
  ret = poll(&pfd, 1, 500);
  if (ret == 0 || ret == -1)
   return -1;
  ret = read(fd, (char *)buf + bytes_read, count - bytes_read);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_read += ret;
 } while (bytes_read < count);
 return 0;
}

canonical_original_code:
static int do_read(int fd, void *buf, unsigned long count)
{
 unsigned long bytes_read = 0;
 struct pollfd pfd;
 int ret;
 pfd.fd = fd;
 pfd.events = 0x001;
 do {
  ret = poll(&pfd, 1, 500);
  if (ret == 0 || ret == -1)
   return -1;
  ret = read(fd, (char *)buf + bytes_read, count - bytes_read);
  if (ret == -1) {
   switch ((*__errno_location ())) {
   case 11:
    continue;
   default:
    return -1;
   }
  } else if (ret == 0) {
   printf("Unexpected end of file\n");
   return -1;
  }
  bytes_read += ret;
 } while (bytes_read < count);
 return 0;
}

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


decompiled_code:
__int64 __fastcall ps2_command(__int64 a1, __int64 a2, int a3)
{
int v3; // er13
int v4; // ebp
unsigned __int8 v6; // [rsp+Eh] [rbp-2Ah] BYREF
char v7[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = (a3 >> 12) & 0xF;
v4 = BYTE1(a3) & 0xF;
v7[0] = a3;
if ( ((BYTE1(a3) | (unsigned __int8)(a3 >> 12)) & 0xF) != 0 && !a2 )
return (unsigned int)-1;
if ( (unsigned int)do_write(*(_DWORD *)(a1 + 16), (__int64)v7, 1uLL) )
{
puts("Error writing to serio device");
v4 = -1;
}
else if ( (unsigned int)do_read(*(_DWORD *)(a1 + 16), (__int64)&v6, 1uLL) )
{
puts("Error reading ack byte");
v4 = -1;
}
else if ( v6 == 0xFA )
{
if ( !v3 )
goto LABEL_23;
if ( (unsigned int)do_write(*(_DWORD *)(a1 + 16), a2, v3) )
{
puts("Error writing command data");
return (unsigned int)-1;
}
if ( (unsigned int)do_read(*(_DWORD *)(a1 + 16), (__int64)&v6, 1uLL) )
{
puts("Error reading ack byte after sending data");
return (unsigned int)-1;
}
if ( v6 == 0xFA )
{
LABEL_23:
if ( v4 )
{
v4 = do_read(*(_DWORD *)(a1 + 16), a2, v4);
if ( v4 )
{
puts("Error reading command response");
v4 = -1;
}
}
}
else
{
printf("Invalid ack byte 0x%02hhx after data\n", v6);
v4 = -1;
}
}
else
{
printf("Invalid ack byte 0x%02hhx after command\n", v6);
v4 = -1;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3)
{
int v3; // er13
int v4; // ebp
unsigned __int8 v6; // [rsp+Eh] [rbp-2Ah] BYREF
char v7[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = (a3 >> 12) & 0xF;
v4 = BYTE1(a3) & 0xF;
v7[0] = a3;
if ( ((BYTE1(a3) | (unsigned __int8)(a3 >> 12)) & 0xF) != 0 && !a2 )
return (unsigned int)-1;
if ( (unsigned int)func1(*(_DWORD *)(a1 + 16), (__int64)v7, 1uLL) )
{
puts("Error writing to serio device");
v4 = -1;
}
else if ( (unsigned int)func6(*(_DWORD *)(a1 + 16), (__int64)&v6, 1uLL) )
{
puts("Error reading ack byte");
v4 = -1;
}
else if ( v6 == 0xFA )
{
if ( !v3 )
goto LABEL_23;
if ( (unsigned int)func1(*(_DWORD *)(a1 + 16), a2, v3) )
{
puts("Error writing command data");
return (unsigned int)-1;
}
if ( (unsigned int)func6(*(_DWORD *)(a1 + 16), (__int64)&v6, 1uLL) )
{
puts("Error reading ack byte after sending data");
return (unsigned int)-1;
}
if ( v6 == 0xFA )
{
LABEL_23:
if ( v4 )
{
v4 = func6(*(_DWORD *)(a1 + 16), a2, v4);
if ( v4 )
{
puts("Error reading command response");
v4 = -1;
}
}
}
else
{
printf("Invalid ack byte 0x%02hhx after data\n", v6);
v4 = -1;
}
}
else
{
printf("Invalid ack byte 0x%02hhx after command\n", v6);
v4 = -1;
}
return (unsigned int)v4;
}

original_code:
static int ps2_command(struct alps_serio_dev *dev, unsigned char *data,
         int command)
{
 int send_bytes = (command & 0xf000) >> 12;
 int recv_bytes = (command & 0x0f00) >> 8;
 unsigned char cmd = command & 0x00ff;
 unsigned char ack_byte;
 if ((send_bytes || recv_bytes) && !data)
  return -1;
 if (do_write(dev->serio_fd, &cmd, 1)) {
  printf("Error writing to serio device\n");
  return -1;
 }
 if (do_read(dev->serio_fd, &ack_byte, 1)) {
  printf("Error reading ack byte\n");
  return -1;
 }
 if (ack_byte != 0xfa) {
  printf("Invalid ack byte 0x%02hhx after command\n", ack_byte);
  return -1;
 }
 if (send_bytes) {
  if (do_write(dev->serio_fd, data, send_bytes)) {
   printf("Error writing command data\n");
   return -1;
  } else if (do_read(dev->serio_fd, &ack_byte, 1)) {
   printf("Error reading ack byte after sending data\n");
   return -1;
  } else if (ack_byte != 0xfa) {
   printf("Invalid ack byte 0x%02hhx after data\n", ack_byte);
   return -1;
  }
 }
 if (recv_bytes && do_read(dev->serio_fd, data, recv_bytes)) {
  printf("Error reading command response\n");
  return -1;
 }
 return 0;
}

variable_types:
data
unsigned char *

variable_types: command int

variable_types:
cmd
unsigned char


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
void *v4; // rbx
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = alps_serio_alloc();
v4 = v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)serio_mouse_init((__int64)v3) )
{
puts("Error: Could not locate serio mouse");
serio_mouse_deinit((__int64)v4);
exit(1);
}
ps2_command((__int64)v4, (__int64)v5, 767);
ps2_drain((__int64)v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)ps2_command((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 230)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 230)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 230)
|| (unsigned int)ps2_command((__int64)v4, (__int64)v5, 1001) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( (unsigned int)ps2_command((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 231)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 231)
|| (unsigned int)ps2_command((__int64)v4, 0LL, 231)
|| (unsigned int)ps2_command((__int64)v4, (__int64)v5, 1001) )
{
puts("E7 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[0]) != 115 )
goto LABEL_25;
if ( HIBYTE(v5[0]) != 3 )
{
if ( *(_WORD *)((char *)v5 + 1) == 25602 )
{
puts("V3 ALPS touchpad found");
goto LABEL_10;
}
LABEL_25:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_25;
puts("V5 ALPS touchpad found");
}
LABEL_10:
serio_mouse_deinit((__int64)v4);
alps_serio_free(v4);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rax
void *v4; // rbx
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = func0();
v4 = v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)func3((__int64)v3) )
{
puts("Error: Could not locate serio mouse");
func2((__int64)v4);
exit(1);
}
func7((__int64)v4, (__int64)v5, 767);
func5((__int64)v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)func7((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)func7((__int64)v4, 0LL, 230)
|| (unsigned int)func7((__int64)v4, 0LL, 230)
|| (unsigned int)func7((__int64)v4, 0LL, 230)
|| (unsigned int)func7((__int64)v4, (__int64)v5, 1001) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( (unsigned int)func7((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)func7((__int64)v4, 0LL, 231)
|| (unsigned int)func7((__int64)v4, 0LL, 231)
|| (unsigned int)func7((__int64)v4, 0LL, 231)
|| (unsigned int)func7((__int64)v4, (__int64)v5, 1001) )
{
puts("E7 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[0]) != 115 )
goto LABEL_25;
if ( HIBYTE(v5[0]) != 3 )
{
if ( *(_WORD *)((char *)v5 + 1) == 25602 )
{
puts("V3 ALPS touchpad found");
goto LABEL_10;
}
LABEL_25:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_25;
puts("V5 ALPS touchpad found");
}
LABEL_10:
func2((__int64)v4);
func4(v4);
exit(0);
}

original_code:
int main(void)
{
 struct alps_serio_dev *dev;
 unsigned char param[4];
 if (geteuid() != 0) {
  fprintf(stderr, "Error: Must be run as root\n");
  exit(1);
 }
 dev = alps_serio_alloc();
 if (!dev) {
  printf("Error: Could not allocate device\n");
  exit(1);
 }
 if (serio_mouse_init(dev)) {
  printf("Error: Could not locate serio mouse\n");
  serio_mouse_deinit(dev);
  exit(1);
 }
 ps2_command(dev, param, 0x02ff);
 ps2_drain(dev);
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E6 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] != 0 || param[1] != 0 ||
     (param[2] != 10 && param[2] != 100)) {
  printf("Invalid E6 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
  goto cleanup;
 }
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E7 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x50) {
     printf("V6 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x0a) {
     printf("V5 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x02 && param[2] == 0x64) {
     printf("V3 ALPS touchpad found\n");
 }
 else {
  printf("E7 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
 }
cleanup:
 serio_mouse_deinit(dev);
 alps_serio_free(dev);
 exit(0);
}

canonical_original_code:
int main()
{
 struct alps_serio_dev *dev;
 unsigned char param[4];
 if (geteuid() != 0) {
  fprintf(stderr, "Error: Must be run as root\n");
  exit(1);
 }
 dev = alps_serio_alloc();
 if (!dev) {
  printf("Error: Could not allocate device\n");
  exit(1);
 }
 if (serio_mouse_init(dev)) {
  printf("Error: Could not locate serio mouse\n");
  serio_mouse_deinit(dev);
  exit(1);
 }
 ps2_command(dev, param, 0x02ff);
 ps2_drain(dev);
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, ((void *)0), 0x00e6) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E6 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] != 0 || param[1] != 0 ||
     (param[2] != 10 && param[2] != 100)) {
  printf("Invalid E6 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
  goto cleanup;
 }
 param[0] = 0;
 if (ps2_command(dev, param, 0x10e8) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, ((void *)0), 0x00e7) ||
     ps2_command(dev, param, 0x03e9)) {
  printf("E7 report failed, not an ALPS touchpad\n");
  goto cleanup;
 }
 if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x50) {
     printf("V6 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x0a) {
     printf("V5 ALPS touchpad found\n");
 }
 else if (param[0] == 0x73 && param[1] == 0x02 && param[2] == 0x64) {
     printf("V3 ALPS touchpad found\n");
 }
 else {
  printf("E7 report: %02hhx %02hhx %02hhx\n",
         param[0], param[1], param[2]);
  printf("Not an ALPS touchpad\n");
 }
cleanup:
 serio_mouse_deinit(dev);
 alps_serio_free(dev);
 exit(0);
}

variable_types:
param
unsigned char [4]


decompiled_code:
void __fastcall __noreturn main(int a1, char **a2, char **a3)
{
_DWORD *v3; // rax
void *v4; // rbx
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = sub_401262();
v4 = v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)sub_401349((__int64)v3) )
{
puts("Error: Could not locate serio mouse");
sub_4012F9((__int64)v4);
exit(1);
}
sub_4017A6((__int64)v4, (__int64)v5, 767);
sub_401662((__int64)v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)sub_4017A6((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)sub_4017A6((__int64)v4, 0LL, 230)
|| (unsigned int)sub_4017A6((__int64)v4, 0LL, 230)
|| (unsigned int)sub_4017A6((__int64)v4, 0LL, 230)
|| (unsigned int)sub_4017A6((__int64)v4, (__int64)v5, 1001) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( (unsigned int)sub_4017A6((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)sub_4017A6((__int64)v4, 0LL, 231)
|| (unsigned int)sub_4017A6((__int64)v4, 0LL, 231)
|| (unsigned int)sub_4017A6((__int64)v4, 0LL, 231)
|| (unsigned int)sub_4017A6((__int64)v4, (__int64)v5, 1001) )
{
puts("E7 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[0]) != 115 )
goto LABEL_25;
if ( HIBYTE(v5[0]) != 3 )
{
if ( *(_WORD *)((char *)v5 + 1) == 25602 )
{
puts("V3 ALPS touchpad found");
goto LABEL_10;
}
LABEL_25:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_25;
puts("V5 ALPS touchpad found");
}
LABEL_10:
sub_4012F9((__int64)v4);
sub_401646(v4);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn main(int a1, char **a2, char **a3)
{
_DWORD *v3; // rax
void *v4; // rbx
_WORD v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

if ( geteuid() )
{
fwrite("Error: Must be run as root\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
v3 = func6();
v4 = v3;
if ( !v3 )
{
puts("Error: Could not allocate device");
exit(1);
}
if ( (unsigned int)func9((__int64)v3) )
{
puts("Error: Could not locate serio mouse");
func8((__int64)v4);
exit(1);
}
func13((__int64)v4, (__int64)v5, 767);
func11((__int64)v4);
LOBYTE(v5[0]) = 0;
if ( (unsigned int)func13((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)func13((__int64)v4, 0LL, 230)
|| (unsigned int)func13((__int64)v4, 0LL, 230)
|| (unsigned int)func13((__int64)v4, 0LL, 230)
|| (unsigned int)func13((__int64)v4, (__int64)v5, 1001) )
{
puts("E6 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( v5[0] || LOBYTE(v5[1]) != 10 && LOBYTE(v5[1]) != 100 )
{
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( (unsigned int)func13((__int64)v4, (__int64)v5, 4328)
|| (unsigned int)func13((__int64)v4, 0LL, 231)
|| (unsigned int)func13((__int64)v4, 0LL, 231)
|| (unsigned int)func13((__int64)v4, 0LL, 231)
|| (unsigned int)func13((__int64)v4, (__int64)v5, 1001) )
{
puts("E7 report failed, not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[0]) != 115 )
goto LABEL_25;
if ( HIBYTE(v5[0]) != 3 )
{
if ( *(_WORD *)((char *)v5 + 1) == 25602 )
{
puts("V3 ALPS touchpad found");
goto LABEL_10;
}
LABEL_25:
printf("E7 report: %02hhx %02hhx %02hhx\n", LOBYTE(v5[0]), HIBYTE(v5[0]), LOBYTE(v5[1]));
puts("Not an ALPS touchpad");
goto LABEL_10;
}
if ( LOBYTE(v5[1]) == 80 )
{
puts("V6 ALPS touchpad found");
}
else
{
if ( LOBYTE(v5[1]) != 10 )
goto LABEL_25;
puts("V5 ALPS touchpad found");
}
LABEL_10:
func8((__int64)v4);
func10(v4);
exit(0);
}


a

repo: timjb/tlpi-exercises

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

if ( _setjmp(jmp_env) )
result = puts("jumping with longjmp");
else
result = puts("first time setjmp");
return result;
}

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

if ( _setjmp(jmp_env) )
result = puts("jumping with longjmp");
else
result = puts("first time setjmp");
return result;
}

original_code:
void a (void) {
  if (_setjmp (jmp_env) == 0) {
    printf("first time setjmp\n");
  } else {
    printf("jumping with longjmp\n");
  }
}

canonical_original_code:
void a() {
  if (_setjmp (jmp_env) == 0) {
    printf("first time setjmp\n");
  } else {
    printf("jumping with longjmp\n");
  }
}


b

decompiled_code:
void __noreturn b()
{
longjmp(jmp_env, 1);
}

canonical_decompiled_code:
void __noreturn func8()
{
longjmp(jmp_env, 1);
}

original_code:
void b (void) {
  longjmp(jmp_env, 1);
}

canonical_original_code:
void b() {
  longjmp(jmp_env, 1);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
puts("before a()");
a();
puts("before b()");
b("before b()", argv);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
puts("before a()");
func7();
puts("before b()");
func8("before b()", argv);
}

original_code:
int main (int argc, char *argv[]) {
  printf("before a()\n");
  a();
  printf("before b()\n");
  b();
  printf("after b()\n");
  exit(0);
}

canonical_original_code:
int main(int argc, char *argv[]) {
  printf("before a()\n");
  a();
  printf("before b()\n");
  b();
  printf("after b()\n");
  exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]


repo: timocramer/gbasm

decompiled_code:
const char *__fastcall command_closing(unsigned __int64 a1)
{
const char *result; // rax

if ( a1 == 234 )
return "], a";
if ( a1 > 0xEA )
{
result = "]";
if ( a1 != 240 )
{
result = "";
if ( a1 == 250 )
result = "]";
}
}
else
{
result = "], sp";
if ( a1 != 8 )
{
result = "";
if ( a1 == 224 )
result = "], a";
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func7(unsigned __int64 a1)
{
const char *result; // rax

if ( a1 == 234 )
return "], a";
if ( a1 > 0xEA )
{
result = "]";
if ( a1 != 240 )
{
result = "";
if ( a1 == 250 )
result = "]";
}
}
else
{
result = "], sp";
if ( a1 != 8 )
{
result = "";
if ( a1 == 224 )
result = "], a";
}
}
return result;
}

original_code:
static const char* command_closing(size_t i) {
 switch(i) {
 case 0x08: return "], sp";
 case 0xe0: case 0xea: return "], a";
 case 0xf0: case 0xfa: return "]";
 }
 return "";
}

canonical_original_code:
static char *command_closing(unsigned long i) {
 switch(i) {
 case 0x08: return "], sp";
 case 0xe0: case 0xea: return "], a";
 case 0xf0: case 0xfa: return "]";
 }
 return "";
}

variable_types:
i
unsigned long

return_type: char *


decompiled_code:
__int64 __fastcall read_u16l(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int16 *a1)
{
return *a1;
}

original_code:
static uint16_t read_u16l(unsigned char *x) {
 return ((uint16_t)x[1] << 8) | x[0];
}

canonical_original_code:
static unsigned short read_u16l(unsigned char *x) {
 return ((unsigned short)x[1] << 8) | x[0];
}

variable_types:
x
unsigned char *

return_type: unsigned short


decompiled_code:
__int64 __fastcall byte_arguments(unsigned __int8 a1)
{
void *v1; // rdx
__int64 result; // rax
__int64 v3[3]; // [rsp+0h] [rbp-48h] BYREF
char v4; // [rsp+18h] [rbp-30h]
__int64 s[2]; // [rsp+20h] [rbp-28h] BYREF
char v6; // [rsp+30h] [rbp-18h]

s[0] = 0xC4C3C23121110801LL;
s[1] = 0xEADCDAD4D2CDCCCALL;
v6 = -6;
v3[0] = 0x2826201E18160E06LL;
v3[1] = 0xD6CEC63E3836302ELL;
v3[2] = 0xF8F6F0EEE8E6E0DELL;
v4 = -2;
v1 = memchr(s, a1, 0x11uLL);
result = 2LL;
if ( !v1 )
result = memchr(v3, a1, 0x19uLL) != 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 a1)
{
void *v1; // rdx
__int64 result; // rax
__int64 v3[3]; // [rsp+0h] [rbp-48h] BYREF
char v4; // [rsp+18h] [rbp-30h]
__int64 s[2]; // [rsp+20h] [rbp-28h] BYREF
char v6; // [rsp+30h] [rbp-18h]

s[0] = 0xC4C3C23121110801LL;
s[1] = 0xEADCDAD4D2CDCCCALL;
v6 = -6;
v3[0] = 0x2826201E18160E06LL;
v3[1] = 0xD6CEC63E3836302ELL;
v3[2] = 0xF8F6F0EEE8E6E0DELL;
v4 = -2;
v1 = memchr(s, a1, 0x11uLL);
result = 2LL;
if ( !v1 )
result = memchr(v3, a1, 0x19uLL) != 0LL;
return result;
}

original_code:
static unsigned int byte_arguments(unsigned char opcode) {
 const char two_byte[] = {0x01, 0x08, 0x11, 0x21, 0x31, 0xc2, 0xc3, 0xc4,
 0xca, 0xcc, 0xcd, 0xd2, 0xd4, 0xda, 0xdc, 0xea, 0xfa};
 const char one_byte[] = {0x06, 0x0e, 0x16, 0x18, 0x1e, 0x20, 0x26, 0x28,
 0x2e, 0x30, 0x36, 0x38, 0x3e, 0xc6, 0xce, 0xd6, 0xde, 0xe0, 0xe6, 0xe8,
 0xee, 0xf0, 0xf6, 0xf8, 0xfe};
 if(memchr(two_byte, opcode, sizeof(two_byte)) != ((void *)0))
  return 2;
 if(memchr(one_byte, opcode, sizeof(one_byte)) != ((void *)0))
  return 1;
 return 0;
}

canonical_original_code:
static unsigned int byte_arguments(unsigned char opcode) {
 char two_byte[] = {0x01, 0x08, 0x11, 0x21, 0x31, 0xc2, 0xc3, 0xc4,
 0xca, 0xcc, 0xcd, 0xd2, 0xd4, 0xda, 0xdc, 0xea, 0xfa};
 char one_byte[] = {0x06, 0x0e, 0x16, 0x18, 0x1e, 0x20, 0x26, 0x28,
 0x2e, 0x30, 0x36, 0x38, 0x3e, 0xc6, 0xce, 0xd6, 0xde, 0xe0, 0xe6, 0xe8,
 0xee, 0xf0, 0xf6, 0xf8, 0xfe};
 if(memchr(two_byte, opcode, sizeof(two_byte)) != ((void *)0))
  return 2;
 if(memchr(one_byte, opcode, sizeof(one_byte)) != ((void *)0))
  return 1;
 return 0;
}

variable_types:
opcode
unsigned char

return_type: unsigned int


disassemble

decompiled_code:
void __fastcall disassemble(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbp
unsigned __int64 v3; // r13
const char *v4; // rax
int v5; // er13
int i; // ebx
unsigned __int8 *v7; // r12
unsigned __int8 v8; // bl
int v9; // eax
const char *v10; // r13
unsigned __int16 v11; // ax
int v12; // er13
int j; // ebx
unsigned __int8 *v14; // [rsp+0h] [rbp-48h]

if ( a2 )
{
v2 = 0LL;
do
{
printf("%06zx: ", v2);
v7 = (unsigned __int8 *)(a1 + v2);
v8 = *(_BYTE *)(a1 + v2);
if ( v8 == 0xCB )
{
printf(
"%-20s; 0xcb 0x%02x\n",
&bit_command_map[12 * *(unsigned __int8 *)(a1 + v2 + 1)],
*(unsigned __int8 *)(a1 + v2 + 1));
v2 += 2LL;
}
else
{
v9 = byte_arguments(v8);
switch ( v9 )
{
case 1:
v3 = v2 + 1;
if ( v2 + 1 >= a2 )
{
fprintf(stderr, "%s: The file seems to be incomplete!\n", (const char *)gbasm_filename);
exit(1);
}
v4 = command_closing(v8);
v14 = (unsigned __int8 *)(a1 + v3);
v5 = 20 - printf("%s0x%02x%s", &command_map[12 * v8], *(unsigned __int8 *)(a1 + v3), v4);
if ( v5 > 0 )
{
for ( i = 0; i != v5; ++i )
putchar(32);
}
printf("; 0x%02x 0x%02x\n", *v7, *v14);
v2 += 2LL;
break;
case 2:
if ( v2 + 2 >= a2 )
{
fprintf(stderr, "%s: The file seems to be incomplete!\n", (const char *)gbasm_filename);
exit(1);
}
v10 = command_closing(v8);
v11 = read_u16l((unsigned __int16 *)(a1 + v2 + 1));
v12 = 20 - printf("%s0x%04x%s", &command_map[12 * v8], v11, v10);
if ( v12 > 0 )
{
for ( j = 0; j != v12; ++j )
putchar(32);
}
printf(
"; 0x%02x 0x%02x 0x%02x\n",
*v7,
*(unsigned __int8 *)(a1 + v2 + 1),
*(unsigned __int8 *)(a1 + v2 + 2));
v2 += 3LL;
break;
case 0:
printf("%-20s; 0x%02x\n", &command_map[12 * v8], v8);
++v2;
break;
}
}
}
while ( v2 < a2 );
}
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rbp
unsigned __int64 v3; // r13
const char *v4; // rax
int v5; // er13
int i; // ebx
unsigned __int8 *v7; // r12
unsigned __int8 v8; // bl
int v9; // eax
const char *v10; // r13
unsigned __int16 v11; // ax
int v12; // er13
int j; // ebx
unsigned __int8 *v14; // [rsp+0h] [rbp-48h]

if ( a2 )
{
v2 = 0LL;
do
{
printf("%06zx: ", v2);
v7 = (unsigned __int8 *)(a1 + v2);
v8 = *(_BYTE *)(a1 + v2);
if ( v8 == 0xCB )
{
printf(
"%-20s; 0xcb 0x%02x\n",
&bit_command_map[12 * *(unsigned __int8 *)(a1 + v2 + 1)],
*(unsigned __int8 *)(a1 + v2 + 1));
v2 += 2LL;
}
else
{
v9 = func10(v8);
switch ( v9 )
{
case 1:
v3 = v2 + 1;
if ( v2 + 1 >= a2 )
{
fprintf(stderr, "%s: The file seems to be incomplete!\n", (const char *)gbasm_filename);
exit(1);
}
v4 = func7(v8);
v14 = (unsigned __int8 *)(a1 + v3);
v5 = 20 - printf("%s0x%02x%s", &command_map[12 * v8], *(unsigned __int8 *)(a1 + v3), v4);
if ( v5 > 0 )
{
for ( i = 0; i != v5; ++i )
putchar(32);
}
printf("; 0x%02x 0x%02x\n", *v7, *v14);
v2 += 2LL;
break;
case 2:
if ( v2 + 2 >= a2 )
{
fprintf(stderr, "%s: The file seems to be incomplete!\n", (const char *)gbasm_filename);
exit(1);
}
v10 = func7(v8);
v11 = func8((unsigned __int16 *)(a1 + v2 + 1));
v12 = 20 - printf("%s0x%04x%s", &command_map[12 * v8], v11, v10);
if ( v12 > 0 )
{
for ( j = 0; j != v12; ++j )
putchar(32);
}
printf(
"; 0x%02x 0x%02x 0x%02x\n",
*v7,
*(unsigned __int8 *)(a1 + v2 + 1),
*(unsigned __int8 *)(a1 + v2 + 2));
v2 += 3LL;
break;
case 0:
printf("%-20s; 0x%02x\n", &command_map[12 * v8], v8);
++v2;
break;
}
}
}
while ( v2 < a2 );
}
}

original_code:
static void disassemble(unsigned char *data, size_t size) {
 size_t i = 0;
 int j, written;
 while(i < size) {
  printf("%06zx: ", i);
  if(data[i] == 0xcb) {
   ++i;
   printf("%-20s; 0xcb 0x%02x\n", bit_command_map[data[i]], data[i]);
   ++i;
   continue;
  }
  switch(byte_arguments(data[i])) {
  case 0:
   printf("%-20s; 0x%02x\n", command_map[data[i]], data[i]);
   ++i;
   break;
  case 1:
   if(i + 1 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%02x%s", command_map[data[i]], data[i+1], command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x\n", data[i], data[i+1]);
   i += 2;
   break;
  case 2:
   if(i + 2 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%04x%s", command_map[data[i]], read_u16l(data + i + 1), command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x 0x%02x\n", data[i], data[i+1], data[i+2]);
   i += 3;
  }
 }
}

canonical_original_code:
static void disassemble(unsigned char *data, unsigned long size) {
 unsigned long i = 0;
 int j; int written;
 while(i < size) {
  printf("%06zx: ", i);
  if(data[i] == 0xcb) {
   ++i;
   printf("%-20s; 0xcb 0x%02x\n", bit_command_map[data[i]], data[i]);
   ++i;
   continue;
  }
  switch(byte_arguments(data[i])) {
  case 0:
   printf("%-20s; 0x%02x\n", command_map[data[i]], data[i]);
   ++i;
   break;
  case 1:
   if(i + 1 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%02x%s", command_map[data[i]], data[i+1], command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x\n", data[i], data[i+1]);
   i += 2;
   break;
  case 2:
   if(i + 2 >= size) {
    fprintf(stderr, "%s: The file seems to be incomplete!\n", gbasm_filename);
    exit(1);
   }
   written = printf("%s0x%04x%s", command_map[data[i]], read_u16l(data + i + 1), command_closing(data[i]));
   for(j = 0; j < 20 - written; ++j)
    putchar(' ');
   printf("; 0x%02x 0x%02x 0x%02x\n", data[i], data[i+1], data[i+2]);
   i += 3;
  }
 }
}

variable_types:
size
unsigned long

variable_types: j int

variable_types: written int


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

va_start(arg, format);
fprintf(stderr, "%s: error: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

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

va_start(arg, format);
fprintf(stderr, "%s: error: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void gbasm_error(const char *message, ...) {
 va_list args;
 fprintf(stderr, "%s: error: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

canonical_original_code:
void gbasm_error(char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s: error: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

variable_types:
message
char *


decompiled_code:
void __noreturn no_memory()
{
gbasm_error("not enough memory!");
}

canonical_decompiled_code:
void __noreturn func13()
{
func12("not enough memory!");
}

original_code:
void no_memory(void) {
 gbasm_error("not enough memory!");
}

canonical_original_code:
void no_memory() {
 gbasm_error("not enough memory!");
}


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

va_start(arg, format);
fprintf(stderr, "%s: warning: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

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

va_start(arg, format);
fprintf(stderr, "%s: warning: ", (const char *)gbasm_filename);
vfprintf(stderr, format, arg);
return fputc(10, stderr);
}

original_code:
void gbasm_warning(const char *message, ...) {
 va_list args;
 fprintf(stderr, "%s: warning: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}

canonical_original_code:
void gbasm_warning(char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s: warning: ", gbasm_filename);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}


decompiled_code:
void __noreturn location_error(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: error: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
fputc(10, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func15(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: error: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
fputc(10, stderr);
exit(1);
}

original_code:
void location_error(YYLTYPE loc, char *message, ...) {
 va_list args;
 fprintf(stderr, "%s:%d:%d: error: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

canonical_original_code:
void location_error(struct YYLTYPE loc, char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s:%d:%d: error: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
 exit(1);
}

variable_types: struct YYLTYPE { int first_line; int first_column; int last_line; int last_column; }


decompiled_code:
int location_warning(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: warning: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
return fputc(10, stderr);
}

canonical_decompiled_code:
int func16(unsigned __int64 a1, __int64 a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a3);
fprintf(stderr, "%s:%d:%d: warning: ", (const char *)input_filename, (unsigned int)a1, HIDWORD(a1));
vfprintf(stderr, a3, arg);
return fputc(10, stderr);
}

original_code:
void location_warning(YYLTYPE loc, char *message, ...) {
 va_list args;
 fprintf(stderr, "%s:%d:%d: warning: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}

canonical_original_code:
void location_warning(struct YYLTYPE loc, char *message, ...) {
 __builtin_va_list args;
 fprintf(stderr, "%s:%d:%d: warning: ", input_filename, loc.first_line, loc.first_column);
 __builtin_va_start(args,message);
 vfprintf(stderr, message, args);
 __builtin_va_end(args);
 fputc('\n', stderr);
}

user_defined_types: struct YYLTYPE { int first_line; int first_column; int last_line; int last_column; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int result; // eax
size_t v5; // [rsp+0h] [rbp-18h] BYREF
void *ptr; // [rsp+8h] [rbp-10h] BYREF

v3 = *argv;
gbasm_filename = (__int64)*argv;
if ( argc <= 1 )
{
fprintf(stderr, "%s: please give a filename\n", v3);
result = 1;
}
else
{
input_filename = (__int64)argv[1];
read_file((const char *)input_filename, &v5, &ptr);
disassemble((__int64)ptr, v5);
free(ptr);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int result; // eax
size_t v5; // [rsp+0h] [rbp-18h] BYREF
void *ptr; // [rsp+8h] [rbp-10h] BYREF

v3 = *argv;
gbasm_filename = (__int64)*argv;
if ( argc <= 1 )
{
fprintf(stderr, "%s: please give a filename\n", v3);
result = 1;
}
else
{
input_filename = (__int64)argv[1];
func9((const char *)input_filename, &v5, &ptr);
func11((__int64)ptr, v5);
free(ptr);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv) {
 unsigned char *data;
 size_t size;
 gbasm_filename = argv[0];
 if(argc < 2) {
  fprintf(stderr, "%s: please give a filename\n", gbasm_filename);
  return 1;
 }
 input_filename = argv[1];
 read_file(input_filename, &size, &data);
 disassemble(data, size);
 free(data);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 unsigned char *data;
 unsigned long size;
 gbasm_filename = argv[0];
 if(argc < 2) {
  fprintf(stderr, "%s: please give a filename\n", gbasm_filename);
  return 1;
 }
 input_filename = argv[1];
 read_file(input_filename, &size, &data);
 disassemble(data, size);
 free(data);
 return 0;
}

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall chr2int(unsigned __int8 a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)a1 - 48;
v1 = a1 & 0xDF;
result = (unsigned int)v1 - 55;
if ( (unsigned __int8)(v1 - 65) >= 0x1Au )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
unsigned __int8 v1; // di
__int64 result; // rax

if ( (unsigned __int8)(a1 - 48) <= 9u )
return (unsigned int)a1 - 48;
v1 = a1 & 0xDF;
result = (unsigned int)v1 - 55;
if ( (unsigned __int8)(v1 - 65) >= 0x1Au )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static unsigned int chr2int(unsigned char x) {
 if(x >= '0' && x <= '9')
  return x - '0';
 x &= ~0x20;
 if(x >= 'A' && x <= 'Z')
  return x - 'A' + 10;
 return (0x7fffffff * 2U + 1U);
}

variable_types:
x
unsigned char


decompiled_code:
__int64 __fastcall scan_uint(unsigned int a1)
{
int v1; // ebp
char *v2; // rbx
__int64 result; // rax

v1 = 0;
while ( 1 )
{
v2 = src;
result = chr2int(*src);
if ( (unsigned int)result >= a1 )
break;
v1 = result + a1 * v1;
src = v2 + 1;
++yylloc[3];
}
yylval = v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
int v1; // ebp
char *v2; // rbx
__int64 result; // rax

v1 = 0;
while ( 1 )
{
v2 = src;
result = func0(*src);
if ( (unsigned int)result >= a1 )
break;
v1 = result + a1 * v1;
src = v2 + 1;
++yylloc[3];
}
yylval = v1;
return result;
}

original_code:
static void scan_uint(unsigned int base) {
 unsigned int i = 0;
 unsigned int c;
 while(1) {
  c = chr2int(*src);
  if(c >= base)
   break;
  i *= base;
  i += c;
  ++src;
  ++yylloc.last_column;
 }
 yylval.integer = i;
}

variable_types:
base
unsigned int

variable_types:
i
unsigned int

variable_types:
c
unsigned int


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

switch ( (char)a1 )
{
case 'a':
result = 7LL;
break;
case 'b':
result = 8LL;
break;
case 'e':
result = 27LL;
break;
case 'f':
result = 12LL;
break;
case 'n':
result = 10LL;
break;
case 'r':
result = 13LL;
break;
case 't':
result = 9LL;
break;
case 'v':
result = 11LL;
break;
default:
result = a1;
break;
}
return result;
}

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

switch ( (char)a1 )
{
case 'a':
result = 7LL;
break;
case 'b':
result = 8LL;
break;
case 'e':
result = 27LL;
break;
case 'f':
result = 12LL;
break;
case 'n':
result = 10LL;
break;
case 'r':
result = 13LL;
break;
case 't':
result = 9LL;
break;
case 'v':
result = 11LL;
break;
default:
result = a1;
break;
}
return result;
}

original_code:
static char slash_char(const char c) {
 switch(c) {
 case 'a': return '\a';
 case 'b': return '\b';
 case 'e': return '\e';
 case 'f': return '\f';
 case 'n': return '\n';
 case 'r': return '\r';
 case 't': return '\t';
 case 'v': return '\v';
 default: return c;
 }
}

canonical_original_code:
static char slash_char(char c) {
 switch(c) {
 case 'a': return '\a';
 case 'b': return '\b';
 case 'e': return '\e';
 case 'f': return '\f';
 case 'n': return '\n';
 case 'r': return '\r';
 case 't': return '\t';
 case 'v': return '\v';
 default: return c;
 }
}

variable_types: c char

return_type: char


decompiled_code:
__int64 scan_multibyte_operator()
{
const char *v0; // r13
__int64 v1; // rbp
const char *v2; // r12
size_t v3; // rbx

v0 = src;
v1 = 0LL;
while ( 1 )
{
v2 = multibyte_operators[v1];
v3 = strlen(v2);
if ( !strncmp(v0, v2, v3) )
break;
if ( ++v1 == 8 )
return 0LL;
}
src = (char *)&v0[v3];
yylloc[3] += v3;
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 func3()
{
const char *v0; // r13
__int64 v1; // rbp
const char *v2; // r12
size_t v3; // rbx

v0 = src;
v1 = 0LL;
while ( 1 )
{
v2 = multibyte_operators[v1];
v3 = strlen(v2);
if ( !strncmp(v0, v2, v3) )
break;
if ( ++v1 == 8 )
return 0LL;
}
src = (char *)&v0[v3];
yylloc[3] += v3;
return (unsigned int)v2;
}

original_code:
static int scan_multibyte_operator(void) {
 size_t i;
 size_t len;
 for(i = 0; i < sizeof(multibyte_operators) / sizeof(multibyte_operators[0]); ++i) {
  len = strlen(multibyte_operators[i]);
  if(strncmp(src, multibyte_operators[i], len) == 0) {
   src += len;
   yylloc.last_column += len;
   return (intptr_t)(multibyte_operators[i]);
  }
 }
 return 0;
}

canonical_original_code:
static int scan_multibyte_operator() {
 unsigned long i;
 unsigned long len;
 for(i = 0; i < sizeof(multibyte_operators) / sizeof(multibyte_operators[0]); ++i) {
  len = strlen(multibyte_operators[i]);
  if(strncmp(src, multibyte_operators[i], len) == 0) {
   src += len;
   yylloc.last_column += len;
   return (long)(multibyte_operators[i]);
  }
 }
 return 0;
}

variable_types:
len
unsigned long


decompiled_code:
__int64 scan_char()
{
char *v0; // rdx
__int64 result; // rax
char v2; // cl
char v3; // al

v0 = src;
result = (unsigned __int8)*src;
if ( !(_BYTE)result )
goto LABEL_8;
v2 = src[1];
if ( !v2 )
goto LABEL_8;
if ( (_BYTE)result != 92 )
{
if ( v2 == 39 )
goto LABEL_5;
LABEL_12:
result = location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant is too long");
goto LABEL_13;
}
while ( 1 )
{
src = v0 + 1;
++yylloc[3];
v3 = v0[2];
if ( v3 )
break;
LABEL_8:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant does not end");
}
if ( v3 != 39 )
goto LABEL_12;
result = slash_char(v0[1]);
LABEL_5:
yylval = (char)result;
if ( (_BYTE)result == 10 )
{
LABEL_13:
++yylloc[2];
yylloc[3] = 1;
goto LABEL_7;
}
++yylloc[3];
LABEL_7:
++yylloc[3];
src += 2;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
char *v0; // rdx
__int64 result; // rax
char v2; // cl
char v3; // al

v0 = src;
result = (unsigned __int8)*src;
if ( !(_BYTE)result )
goto LABEL_8;
v2 = src[1];
if ( !v2 )
goto LABEL_8;
if ( (_BYTE)result != 92 )
{
if ( v2 == 39 )
goto LABEL_5;
LABEL_12:
result = location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant is too long");
goto LABEL_13;
}
while ( 1 )
{
src = v0 + 1;
++yylloc[3];
v3 = v0[2];
if ( v3 )
break;
LABEL_8:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "character constant does not end");
}
if ( v3 != 39 )
goto LABEL_12;
result = func2(v0[1]);
LABEL_5:
yylval = (char)result;
if ( (_BYTE)result == 10 )
{
LABEL_13:
++yylloc[2];
yylloc[3] = 1;
goto LABEL_7;
}
++yylloc[3];
LABEL_7:
++yylloc[3];
src += 2;
return result;
}

original_code:
static void scan_char(void) {
 char new_char;
 if(*src == 0 || *(src + 1) == 0) {
  does_not_end:
  location_error(yylloc, "character constant does not end");
  return;
 }
 if(*src == '\\') {
  ++src;
  ++yylloc.last_column;
  if(*(src + 1) == 0)
   goto does_not_end;
  if(*(src + 1) != '\'')
   goto too_long;
  new_char = slash_char(*src);
 }
 else if(*(src + 1) == '\'')
  new_char = *src;
 else {
  too_long:
  location_error(yylloc, "character constant is too long");
  return;
 }
 yylval.integer = new_char;
 if(new_char == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
 }
 else
  ++yylloc.last_column;
 ++yylloc.last_column;
 src += 2;
}

canonical_original_code:
static void scan_char() {
 char new_char;
 if(*src == 0 || *(src + 1) == 0) {
  does_not_end:
  location_error(yylloc, "character constant does not end");
  return;
 }
 if(*src == '\\') {
  ++src;
  ++yylloc.last_column;
  if(*(src + 1) == 0)
   goto does_not_end;
  if(*(src + 1) != '\'')
   goto too_long;
  new_char = slash_char(*src);
 }
 else if(*(src + 1) == '\'')
  new_char = *src;
 else {
  too_long:
  location_error(yylloc, "character constant is too long");
  return;
 }
 yylval.integer = new_char;
 if(new_char == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
 }
 else
  ++yylloc.last_column;
 ++yylloc.last_column;
 src += 2;
}


decompiled_code:
__int64 scan_int()
{
char *v0; // rax
char v1; // bl
const unsigned __int16 *v2; // rdx
__int64 result; // rax
int v4; // edx
char v5; // cl

v0 = src;
v1 = *src;
if ( *src == 39 )
{
++src;
++yylloc[3];
scan_char();
result = 258LL;
}
else if ( v1 == 48 )
{
++src;
v4 = yylloc[3];
yylloc[3] = v4 + 1;
v5 = v0[1];
if ( v5 == 120 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
scan_uint(0x10u);
result = 258LL;
}
else
{
if ( v5 == 98 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
scan_uint(2u);
}
else
{
src = v0;
yylloc[3] = v4;
scan_uint(8u);
}
result = 258LL;
}
}
else
{
v2 = *_ctype_b_loc();
result = 0LL;
if ( (v2[v1] & 0x800) != 0 )
{
scan_uint(0xAu);
result = 258LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
char *v0; // rax
char v1; // bl
const unsigned __int16 *v2; // rdx
__int64 result; // rax
int v4; // edx
char v5; // cl

v0 = src;
v1 = *src;
if ( *src == 39 )
{
++src;
++yylloc[3];
func4();
result = 258LL;
}
else if ( v1 == 48 )
{
++src;
v4 = yylloc[3];
yylloc[3] = v4 + 1;
v5 = v0[1];
if ( v5 == 120 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
func1(0x10u);
result = 258LL;
}
else
{
if ( v5 == 98 )
{
src = v0 + 2;
yylloc[3] = v4 + 2;
func1(2u);
}
else
{
src = v0;
yylloc[3] = v4;
func1(8u);
}
result = 258LL;
}
}
else
{
v2 = *_ctype_b_loc();
result = 0LL;
if ( (v2[v1] & 0x800) != 0 )
{
func1(0xAu);
result = 258LL;
}
}
return result;
}

original_code:
static int scan_int(void) {
 if(*src == '\'') {
  ++src;
  ++yylloc.last_column;
  scan_char();
 }
 else if(*src == '0') {
  ++src;
  ++yylloc.last_column;
  if(*src == 'x') {
   ++src;
   ++yylloc.last_column;
   scan_uint(16);
  }
  else if(*src == 'b') {
   ++src;
   ++yylloc.last_column;
   scan_uint(2);
  }
  else {
   --src;
   --yylloc.last_column;
   scan_uint(8);
  }
 }
 else if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISdigit))
  scan_uint(10);
 else
  return 0;
 return INTEGER;
}

canonical_original_code:
static int scan_int() {
 if(*src == '\'') {
  ++src;
  ++yylloc.last_column;
  scan_char();
 }
 else if(*src == '0') {
  ++src;
  ++yylloc.last_column;
  if(*src == 'x') {
   ++src;
   ++yylloc.last_column;
   scan_uint(16);
  }
  else if(*src == 'b') {
   ++src;
   ++yylloc.last_column;
   scan_uint(2);
  }
  else {
   --src;
   --yylloc.last_column;
   scan_uint(8);
  }
 }
 else if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISdigit))
  scan_uint(10);
 else
  return 0;
 return INTEGER;
}


decompiled_code:
__int64 scan_string()
{
__int64 result; // rax
char *v1; // rdx
char v2; // al
char *v3; // rax
char v4; // di
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

result = 0LL;
if ( *src == 34 )
{
buffer_init(v5);
++yylloc[3];
v1 = src + 1;
src = v1;
v2 = *v1;
if ( *v1 == 34 )
{
LABEL_3:
buffer_add_char(v5, 0LL);
yylval = v5[0];
++yylloc[3];
++src;
return 259LL;
}
while ( 1 )
{
if ( !v2 )
goto LABEL_4;
if ( v2 != 92 )
goto LABEL_6;
++yylloc[3];
src = v1 + 1;
v4 = v1[1];
if ( v4 )
{
if ( v4 != 10 )
{
v2 = slash_char(v4);
LABEL_6:
buffer_add_char(v5, (unsigned int)v2);
v3 = src;
if ( *src == 10 )
{
++yylloc[2];
yylloc[3] = 1;
}
else
{
++yylloc[3];
}
src = v3 + 1;
goto LABEL_9;
}
}
else
{
LABEL_4:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "String does not end");
}
++yylloc[2];
yylloc[3] = 1;
src = v1 + 2;
LABEL_9:
v1 = src;
v2 = *src;
if ( *src == 34 )
goto LABEL_3;
}
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
char *v1; // rdx
char v2; // al
char *v3; // rax
char v4; // di
__int64 v5[5]; // [rsp+0h] [rbp-28h] BYREF

result = 0LL;
if ( *src == 34 )
{
buffer_init(v5);
++yylloc[3];
v1 = src + 1;
src = v1;
v2 = *v1;
if ( *v1 == 34 )
{
LABEL_3:
buffer_add_char(v5, 0LL);
yylval = v5[0];
++yylloc[3];
++src;
return 259LL;
}
while ( 1 )
{
if ( !v2 )
goto LABEL_4;
if ( v2 != 92 )
goto LABEL_6;
++yylloc[3];
src = v1 + 1;
v4 = v1[1];
if ( v4 )
{
if ( v4 != 10 )
{
v2 = func2(v4);
LABEL_6:
buffer_add_char(v5, (unsigned int)v2);
v3 = src;
if ( *src == 10 )
{
++yylloc[2];
yylloc[3] = 1;
}
else
{
++yylloc[3];
}
src = v3 + 1;
goto LABEL_9;
}
}
else
{
LABEL_4:
location_error(*(_QWORD *)yylloc, *(_QWORD *)&yylloc[2], "String does not end");
}
++yylloc[2];
yylloc[3] = 1;
src = v1 + 2;
LABEL_9:
v1 = src;
v2 = *src;
if ( *src == 34 )
goto LABEL_3;
}
}
return result;
}

original_code:
static int scan_string(void) {
 char new_char;
 struct buffer buf;
 if(*src != '"')
  return 0;
 buffer_init(&buf);
 ++yylloc.last_column;
 ++src;
 while(*src != '"') {
  if(*src == 0) {
   string_does_not_end:
   location_error(yylloc, "String does not end");
  }
  if(*src == '\\') {
   ++yylloc.last_column;
   ++src;
   if(*src == 0)
    goto string_does_not_end;
   if(*src == '\n') {
    ++yylloc.last_line;
    yylloc.last_column = 1;
    ++src;
    continue;
   }
   new_char = slash_char(*src);
  }
  else
   new_char = *src;
  buffer_add_char(&buf, new_char);
  if(*src == '\n') {
   ++yylloc.last_line;
   yylloc.last_column = 1;
  }
  else
   ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.string = buf.data;
 ++yylloc.last_column;
 ++src;
 return STRING;
}

canonical_original_code:
static int scan_string() {
 char new_char;
 struct buffer buf;
 if(*src != '"')
  return 0;
 buffer_init(&buf);
 ++yylloc.last_column;
 ++src;
 while(*src != '"') {
  if(*src == 0) {
   string_does_not_end:
   location_error(yylloc, "String does not end");
  }
  if(*src == '\\') {
   ++yylloc.last_column;
   ++src;
   if(*src == 0)
    goto string_does_not_end;
   if(*src == '\n') {
    ++yylloc.last_line;
    yylloc.last_column = 1;
    ++src;
    continue;
   }
   new_char = slash_char(*src);
  }
  else
   new_char = *src;
  buffer_add_char(&buf, new_char);
  if(*src == '\n') {
   ++yylloc.last_line;
   yylloc.last_column = 1;
  }
  else
   ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.string = buf.data;
 ++yylloc.last_column;
 ++src;
 return STRING;
}

variable_types: struct buffer { char *data; unsigned long size; unsigned long alloc_size; unsigned long write_pos; }


decompiled_code:
__int64 scan_identifier()
{
const unsigned __int16 **v0; // rbx
unsigned int v1; // edx
char v3; // si
__int64 v4[5]; // [rsp+0h] [rbp-28h] BYREF

v0 = _ctype_b_loc();
if ( ((*v0)[*src] & 0x400) != 0 || (v1 = 0, *src == 95) )
{
buffer_init(v4);
buffer_add_char(v4, (unsigned int)*src);
++yylloc[3];
v3 = *++src;
if ( ((*v0)[*src] & 8) != 0 || v3 == 95 )
{
do
{
do
{
buffer_add_char(v4, (unsigned int)v3);
++yylloc[3];
v3 = *++src;
}
while ( ((*v0)[*src] & 8) != 0 );
}
while ( v3 == 95 );
}
buffer_add_char(v4, 0LL);
yylval = v4[0];
v1 = 260;
}
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
const unsigned __int16 **v0; // rbx
unsigned int v1; // edx
char v3; // si
__int64 v4[5]; // [rsp+0h] [rbp-28h] BYREF

v0 = _ctype_b_loc();
if ( ((*v0)[*src] & 0x400) != 0 || (v1 = 0, *src == 95) )
{
buffer_init(v4);
buffer_add_char(v4, (unsigned int)*src);
++yylloc[3];
v3 = *++src;
if ( ((*v0)[*src] & 8) != 0 || v3 == 95 )
{
do
{
do
{
buffer_add_char(v4, (unsigned int)v3);
++yylloc[3];
v3 = *++src;
}
while ( ((*v0)[*src] & 8) != 0 );
}
while ( v3 == 95 );
}
buffer_add_char(v4, 0LL);
yylval = v4[0];
v1 = 260;
}
return v1;
}

original_code:
static int scan_identifier(void) {
 struct buffer buf;
 if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalpha) || *src == '_') {
  buffer_init(&buf);
  buffer_add_char(&buf, *src);
 }
 else
  return 0;
 ++yylloc.last_column;
 ++src;
 while((((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalnum) || (*src) == '_')) {
  buffer_add_char(&buf, *src);
  ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.identifier = buf.data;
 return IDENTIFIER;
}

canonical_original_code:
static int scan_identifier() {
 struct buffer buf;
 if(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalpha) || *src == '_') {
  buffer_init(&buf);
  buffer_add_char(&buf, *src);
 }
 else
  return 0;
 ++yylloc.last_column;
 ++src;
 while((((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalnum) || (*src) == '_')) {
  buffer_add_char(&buf, *src);
  ++yylloc.last_column;
  ++src;
 }
 buffer_add_char(&buf, 0);
 yylval.identifier = buf.data;
 return IDENTIFIER;
}

user_defined_types: struct buffer { char *data; unsigned long size; unsigned long alloc_size; unsigned long write_pos; }


decompiled_code:
__int64 scan_special_identifier()
{
const unsigned __int16 *v0; // r14
const char *v1; // r13
unsigned int v2; // edx
const char *v3; // rbp
__int64 i; // r12
size_t v5; // rbx
char v6; // al

v0 = *_ctype_b_loc();
v1 = src;
if ( (v0[*src] & 8) != 0 || (v2 = 0, *src == 95) )
{
v3 = "A";
for ( i = 0LL; i != 72; ++i )
{
v5 = strlen(v3);
if ( !strncasecmp(v1, v3, v5) )
{
v6 = v1[v5];
if ( (v0[v6] & 8) == 0 && v6 != 95 )
{
src = (char *)&v1[v5];
yylloc[3] += v5;
return (unsigned int)dword_788[3 * i];
}
}
v3 += 12;
}
v2 = 0;
}
return v2;
}

original_code:
static int scan_special_identifier(void) {
 size_t i;
 size_t len;
 if(!(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short int) _ISalnum) || (*src) == '_'))
  return 0;
 for(i = 0; i < sizeof(tokentable) / sizeof(tokentable[0]); ++i) {
  len = strlen(tokentable[i].op);
  if(strncasecmp(src, tokentable[i].op, len) == 0 && !(((*__ctype_b_loc ())[(int) ((src[len]))] & (unsigned short int) _ISalnum) || (src[len]) == '_')) {
   src += len;
   yylloc.last_column += len;
   return tokentable[i].tok;
  }
 }
 return 0;
}

canonical_original_code:
static int scan_special_identifier() {
 unsigned long i;
 unsigned long len;
 if(!(((*__ctype_b_loc ())[(int) ((*src))] & (unsigned short) _ISalnum) || (*src) == '_'))
  return 0;
 for(i = 0; i < sizeof(tokentable) / sizeof(tokentable[0]); ++i) {
  len = strlen(tokentable[i].op);
  if(strncasecmp(src, tokentable[i].op, len) == 0 && !(((*__ctype_b_loc ())[(int) ((src[len]))] & (unsigned short) _ISalnum) || (src[len]) == '_')) {
   src += len;
   yylloc.last_column += len;
   return tokentable[i].tok;
  }
 }
 return 0;
}


yylex

decompiled_code:
__int64 yylex()
{
char *v0; // rax
char v1; // dl
char *v2; // rax
char *v3; // rax
char v4; // dl
__int64 result; // rax
char *v6; // rax

while ( 1 )
{
v1 = *src;
if ( *src == 32 || v1 == 9 )
{
v0 = src + 1;
do
{
do
{
++yylloc[3];
src = v0;
v1 = *v0++;
}
while ( v1 == 32 );
}
while ( v1 == 9 );
}
if ( v1 == 59 && *src != 10 && *src )
{
v3 = src + 1;
do
{
++yylloc[3];
src = v3;
v4 = *v3++;
}
while ( v4 != 10 && v4 );
}
v2 = src;
if ( *src != 10 )
break;
++yylloc[2];
yylloc[3] = 1;
src = v2 + 1;
}
yylloc[0] = yylloc[2];
yylloc[1] = yylloc[3];
result = scan_multibyte_operator();
if ( !(_DWORD)result )
{
result = scan_int();
if ( !(_DWORD)result )
{
result = scan_string();
if ( !(_DWORD)result )
{
result = scan_special_identifier();
if ( !(_DWORD)result )
{
result = scan_identifier();
if ( !(_DWORD)result )
{
++yylloc[3];
v6 = src++;
result = (unsigned int)*v6;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
char *v0; // rax
char v1; // dl
char *v2; // rax
char *v3; // rax
char v4; // dl
__int64 result; // rax
char *v6; // rax

while ( 1 )
{
v1 = *src;
if ( *src == 32 || v1 == 9 )
{
v0 = src + 1;
do
{
do
{
++yylloc[3];
src = v0;
v1 = *v0++;
}
while ( v1 == 32 );
}
while ( v1 == 9 );
}
if ( v1 == 59 && *src != 10 && *src )
{
v3 = src + 1;
do
{
++yylloc[3];
src = v3;
v4 = *v3++;
}
while ( v4 != 10 && v4 );
}
v2 = src;
if ( *src != 10 )
break;
++yylloc[2];
yylloc[3] = 1;
src = v2 + 1;
}
yylloc[0] = yylloc[2];
yylloc[1] = yylloc[3];
result = func3();
if ( !(_DWORD)result )
{
result = func5();
if ( !(_DWORD)result )
{
result = func6();
if ( !(_DWORD)result )
{
result = func8();
if ( !(_DWORD)result )
{
result = func7();
if ( !(_DWORD)result )
{
++yylloc[3];
v6 = src++;
result = (unsigned int)*v6;
}
}
}
}
}
return result;
}

original_code:
int yylex(void) {
 int status;
 skip_white_space:
 while(*src == ' ' || *src == '\t') {
  ++yylloc.last_column;
  ++src;
 }
 if(*src == ';') {
  while(*src != '\n' && *src != 0) {
   ++yylloc.last_column;
   ++src;
  }
 }
 if(*src == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
  ++src;
  goto skip_white_space;
 }
 yylloc.first_line = yylloc.last_line;
 yylloc.first_column = yylloc.last_column;
 status = scan_multibyte_operator();
 if(status != 0)
  return status;
 status = scan_int();
 if(status != 0)
  return status;
 status = scan_string();
 if(status != 0)
  return status;
 status = scan_special_identifier();
 if(status != 0)
  return status;
 status = scan_identifier();
 if(status != 0)
  return status;
 ++yylloc.last_column;
 return *src++;
}

canonical_original_code:
int yylex() {
 int status;
 skip_white_space:
 while(*src == ' ' || *src == '\t') {
  ++yylloc.last_column;
  ++src;
 }
 if(*src == ';') {
  while(*src != '\n' && *src != 0) {
   ++yylloc.last_column;
   ++src;
  }
 }
 if(*src == '\n') {
  ++yylloc.last_line;
  yylloc.last_column = 1;
  ++src;
  goto skip_white_space;
 }
 yylloc.first_line = yylloc.last_line;
 yylloc.first_column = yylloc.last_column;
 status = scan_multibyte_operator();
 if(status != 0)
  return status;
 status = scan_int();
 if(status != 0)
  return status;
 status = scan_string();
 if(status != 0)
  return status;
 status = scan_special_identifier();
 if(status != 0)
  return status;
 status = scan_identifier();
 if(status != 0)
  return status;
 ++yylloc.last_column;
 return *src++;
}

variable_types: status int


decompiled_code:
void *__fastcall buffer_resize(__int64 a1, size_t a2)
{
void *result; // rax

result = realloc(*(void **)a1, a2);
if ( result )
{
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 16) = a2;
}
else
{
no_memory();
result = (void *)buffer_init();
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1, size_t a2)
{
void *result; // rax

result = realloc(*(void **)a1, a2);
if ( result )
{
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 16) = a2;
}
else
{
no_memory();
result = (void *)func1();
}
return result;
}

original_code:
static void buffer_resize(struct buffer *b, size_t size) {
 char *temp;
 temp = realloc(b->data, size);
 if(temp == ((void *)0))
  no_memory();
 b->data = temp;
 b->alloc_size = size;
}

canonical_original_code:
static void buffer_resize(struct buffer *b, unsigned long size) {
 char *temp;
 temp = realloc(b->data, size);
 if(temp == ((void *)0))
  no_memory();
 b->data = temp;
 b->alloc_size = size;
}

variable_types: struct buffer { char *data; unsigned long size; unsigned long alloc_size; unsigned long write_pos; } *

variable_types:
temp
char *


decompiled_code:
void __fastcall buffer_add_char(_QWORD *a1, char a2)
{
unsigned __int64 v3; // rax
unsigned __int64 v4; // rdx
unsigned __int64 v5; // rdi
unsigned __int64 v6; // rax

if ( a1 )
{
v3 = a1[3];
if ( v3 >= a1[2] )
buffer_resize((__int64)a1, (v3 & 0xFFFFFFFFFFFFFF80LL) + 128);
v4 = a1[3];
v5 = a1[1];
if ( v4 > v5 )
memset((void *)(*a1 + v5), 0, v4 - v5);
*(_BYTE *)(*a1 + a1[3]) = a2;
v6 = a1[3] + 1LL;
a1[3] = v6;
if ( v6 > a1[1] )
a1[1] = v6;
}
}

original_code:
void buffer_add_char(struct buffer *b, char c) {
 size_t to_alloc;
 if(b == ((void *)0))
  return;
 if(b->write_pos >= b->alloc_size) {
  to_alloc = 128 * (b->write_pos / 128 + 1);
  buffer_resize(b, to_alloc);
 }
 if(b->write_pos > b->size)
  memset(b->data + b->size, 0, b->write_pos - b->size);
 b->data[b->write_pos] = c;
 b->write_pos++;
 if(b->write_pos > b->size)
  b->size = b->write_pos;
}

canonical_original_code:
void buffer_add_char(struct buffer *b, char c) {
 unsigned long to_alloc;
 if(b == ((void *)0))
  return;
 if(b->write_pos >= b->alloc_size) {
  to_alloc = 128 * (b->write_pos / 128 + 1);
  buffer_resize(b, to_alloc);
 }
 if(b->write_pos > b->size)
  memset(b->data + b->size, 0, b->write_pos - b->size);
 b->data[b->write_pos] = c;
 b->write_pos++;
 if(b->write_pos > b->size)
  b->size = b->write_pos;
}


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

v2 = strlen(a2);
return buffer_add_mem(a1, a2, v2);
}

canonical_decompiled_code:
void *__fastcall func4(_QWORD *a1, const char *a2)
{
size_t v2; // rax

v2 = strlen(a2);
return func3(a1, a2, v2);
}

original_code:
void buffer_add_str(struct buffer *b, const char *s) {
 buffer_add_mem(b, s, strlen(s));
}

canonical_original_code:
void buffer_add_str(struct buffer *b, char *s) {
 buffer_add_mem(b, s, strlen(s));
}

variable_types:
s
char *


decompiled_code:
void __fastcall buffer_add_u16l(_QWORD *a1, __int16 a2)
{
buffer_add_char(a1, a2);
buffer_add_char(a1, SHIBYTE(a2));
}

canonical_decompiled_code:
void __fastcall func5(_QWORD *a1, __int16 a2)
{
func2(a1, a2);
func2(a1, SHIBYTE(a2));
}

original_code:
void buffer_add_u16l(struct buffer *b, uint16_t i) {
 buffer_add_char(b, i & 0xff);
 buffer_add_char(b, i >> 8);
}

canonical_original_code:
void buffer_add_u16l(struct buffer *b, unsigned short i) {
 buffer_add_char(b, i & 0xff);
 buffer_add_char(b, i >> 8);
}

variable_types:
i
unsigned short


help

decompiled_code:
int help()
{
return fprintf(
stderr,
"Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
"Options:\n"
" -n gamename set the name that is written into the metadata (max. 15 byte, default: THE GAME)\n"
" -o output-file set the name of the file where the binary will be written to (default: a.gb)\n"
" -m don't write metadata automatically\n"
"\n"
"Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n",
(const char *)gbasm_filename,
(const char *)gbasm_filename);
}

canonical_decompiled_code:
int func0()
{
return fprintf(
stderr,
"Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
"Options:\n"
" -n gamename set the name that is written into the metadata (max. 15 byte, default: THE GAME)\n"
" -o output-file set the name of the file where the binary will be written to (default: a.gb)\n"
" -m don't write metadata automatically\n"
"\n"
"Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n",
(const char *)gbasm_filename,
(const char *)gbasm_filename);
}

original_code:
static void help(void) {
 fprintf(stderr, "Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
 "Options:\n"
 "  -n gamename     set the name that is written into the metadata (max. 15 byte, default: " "THE GAME" ")\n"
 "  -o output-file  set the name of the file where the binary will be written to (default: " "a.gb" ")\n"
 "  -m              don't write metadata automatically\n"
 "\n"
 "Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n", gbasm_filename, gbasm_filename);
}

canonical_original_code:
static void help() {
 fprintf(stderr, "Usage: %s [-n gamename] [-o output-file] [-m] input-file\n"
 "Options:\n"
 "  -n gamename     set the name that is written into the metadata (max. 15 byte, default: " "THE GAME" ")\n"
 "  -o output-file  set the name of the file where the binary will be written to (default: " "a.gb" ")\n"
 "  -m              don't write metadata automatically\n"
 "\n"
 "Example: %s -n \"MY GAME\" -o mygame.gb mygame.asm\n", gbasm_filename, gbasm_filename);
}


decompiled_code:
__int64 __fastcall get_rom_size_code(unsigned __int64 a1)
{
unsigned __int64 v1; // rax
unsigned int v2; // ebx

if ( a1 <= 0x8000 )
return 0;
v1 = 0x8000LL;
v2 = 0;
do
{
v1 *= 2LL;
++v2;
}
while ( a1 > v1 );
if ( (char)v2 > 6 )
gbasm_warning("The assembled binary is most certainly too large");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int64 a1)
{
unsigned __int64 v1; // rax
unsigned int v2; // ebx

if ( a1 <= 0x8000 )
return 0;
v1 = 0x8000LL;
v2 = 0;
do
{
v1 *= 2LL;
++v2;
}
while ( a1 > v1 );
if ( (char)v2 > 6 )
gbasm_warning("The assembled binary is most certainly too large");
return v2;
}

original_code:
static char get_rom_size_code(size_t size) {
 char x = 0;
 size_t s = 32 * 1024;
 while(size > s) {
  s <<= 1;
  ++x;
 }
 if(x > 6)
  gbasm_warning("The assembled binary is most certainly too large");
 return x;
}

canonical_original_code:
static char get_rom_size_code(unsigned long size) {
 char x = 0;
 unsigned long s = 32 * 1024;
 while(size > s) {
  s <<= 1;
  ++x;
 }
 if(x > 6)
  gbasm_warning("The assembled binary is most certainly too large");
 return x;
}

variable_types:
size
unsigned long

variable_types: x char

variable_types:
s
unsigned long


decompiled_code:
unsigned __int64 warn_if_overwrite()
{
unsigned __int64 result; // rax

result = 256LL;
while ( (unsigned __int64)*(&binary + 1) > result )
{
if ( *((_BYTE *)binary + result) )
return gbasm_warning("There is data that is overwritten by metadata");
if ( ++result == 336 )
return result;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func3()
{
unsigned __int64 result; // rax

result = 256LL;
while ( (unsigned __int64)*(&binary + 1) > result )
{
if ( *((_BYTE *)binary + result) )
return gbasm_warning("There is data that is overwritten by metadata");
if ( ++result == 336 )
return result;
}
return result;
}

original_code:
static void warn_if_overwrite(void) {
 size_t i;
 for(i = 0x100; i < 0x150 && i < binary.size; ++i) {
  if(binary.data[i] != 0) {
   gbasm_warning("There is data that is overwritten by metadata");
   return;
  }
 }
}

canonical_original_code:
static void warn_if_overwrite() {
 unsigned long i;
 for(i = 0x100; i < 0x150 && i < binary.size; ++i) {
  if(binary.data[i] != 0) {
   gbasm_warning("There is data that is overwritten by metadata");
   return;
  }
 }
}

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall write_metadata(char *src)
{
__int64 *v1; // rax
char v2; // dl
__int64 v4[6]; // [rsp+0h] [rbp-58h] BYREF
char dest[8]; // [rsp+30h] [rbp-28h] BYREF
__int64 v6; // [rsp+38h] [rbp-20h]
__int64 v7; // [rsp+40h] [rbp-18h]
__int64 v8; // [rsp+48h] [rbp-10h] BYREF

v4[0] = 0x6666EDCE0150C300LL;
v4[1] = 0x830073030B000DCCLL;
v4[2] = 0x1F1108000D000C00LL;
v4[3] = 0xE66ECCDC0E008988LL;
v4[4] = 0x6367BBBB99D9DDDDLL;
v4[5] = 0x9F99DCDDCCEC0E6ELL;
*(_QWORD *)dest = 1043577275LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0x1AC0000000010000LL;
if ( strlen(src) > 0xF )
gbasm_warning("Game name is too long and will be truncated");
strncpy(&dest[4], src, 0xFuLL);
LOBYTE(v8) = get_rom_size_code((unsigned __int64)*(&binary + 1));
v1 = (__int64 *)&dest[4];
v2 = 25;
do
{
v2 += *(_BYTE *)v1;
v1 = (__int64 *)((char *)v1 + 1);
}
while ( v1 != (__int64 *)((char *)&v8 + 5) );
BYTE5(v8) = -v2;
warn_if_overwrite();
*(&binary + 3) = &loc_FD + 3;
return buffer_add_mem(&binary, v4, 80LL);
}

original_code:
static void write_metadata(const char *game_name) {
 unsigned char complement;
 size_t i;
 char m[0x50] = {
  0,
  0xc3,
  0x50, 0x01,
0xCE,0xED,0x66,0x66,0xCC,0x0D,0x00,0x0B,0x03,0x73,0x00,0x83,0x00,0x0C,0x00,0x0D,
0x00,0x08,0x11,0x1F,0x88,0x89,0x00,0x0E,0xDC,0xCC,0x6E,0xE6,0xDD,0xDD,0xD9,0x99,
0xBB,0xBB,0x67,0x63,0x6E,0x0E,0xEC,0xCC,0xDD,0xDC,0x99,0x9F,0xBB,0xB9,0x33,0x3E,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0,
  0, 0,
  0,
  0,
  0,
  0,
  1,
  0,
  0,
  0,
  0xC0,0x1A
 };
 if(strlen(game_name) > 15)
  gbasm_warning("Game name is too long and will be truncated");
 strncpy(m + (4 + 48), game_name, 15);
 m[0x48] = get_rom_size_code(binary.size);
 complement = 25;
 for(i = 0x34; i < 0x4d; ++i)
  complement += m[i];
 m[0x4d] = -complement;
 warn_if_overwrite();
 binary.write_pos = 0x100;
 buffer_add_mem(&binary, m, 0x50);
}

canonical_original_code:
static void write_metadata(char *game_name) {
 unsigned char complement;
 unsigned long i;
 char m[80] = {
  0,
  0xc3,
  0x50, 0x01,
0xCE,0xED,0x66,0x66,0xCC,0x0D,0x00,0x0B,0x03,0x73,0x00,0x83,0x00,0x0C,0x00,0x0D,
0x00,0x08,0x11,0x1F,0x88,0x89,0x00,0x0E,0xDC,0xCC,0x6E,0xE6,0xDD,0xDD,0xD9,0x99,
0xBB,0xBB,0x67,0x63,0x6E,0x0E,0xEC,0xCC,0xDD,0xDC,0x99,0x9F,0xBB,0xB9,0x33,0x3E,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0,
  0, 0,
  0,
  0,
  0,
  0,
  1,
  0,
  0,
  0,
  0xC0,0x1A
 };
 if(strlen(game_name) > 15)
  gbasm_warning("Game name is too long and will be truncated");
 strncpy(m + (4 + 48), game_name, 15);
 m[0x48] = get_rom_size_code(binary.size);
 complement = 25;
 for(i = 0x34; i < 0x4d; ++i)
  complement += m[i];
 m[0x4d] = -complement;
 warn_if_overwrite();
 binary.write_pos = 0x100;
 buffer_add_mem(&binary, m, 0x50);
}

variable_types:
complement
unsigned char

variable_types:
m
char [80]