decompiled_code:
__int64 __fastcall compute_file_md5(const char *a1, char *a2, int a3)
{
int v4; // er12
__int64 v5; // r15
char *v6; // r13
int v7; // ebp
int v8; // eax
char *v9; // rbx
unsigned int v10; // ebx
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
char s[2]; // [rsp+1Dh] [rbp-4Bh] BYREF
char v17; // [rsp+1Fh] [rbp-49h]
char v18[16]; // [rsp+20h] [rbp-48h] BYREF
char v19; // [rsp+30h] [rbp-38h] BYREF

*(_WORD *)s = 0;
v17 = 0;
if ( !a2 )
_assert_fail("md5 != NULL", "command.c", 0xDEu, "compute_file_md5");
v4 = open(a1, 0);
if ( v4 < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
printf("open file %s failed while compute file md5: %s\n", a1, v12);
v10 = 0;
}
else
{
v5 = a3;
v6 = (char *)malloc(a3);
if ( v6 )
{
v7 = 0;
do
{
v8 = read(v4, &v6[v7], a3);
if ( v8 < 0 )
{
v13 = _errno_location();
v14 = strerror(*v13);
printf("Read file %s failed while compute file md5: %s\n", a1, v14);
v10 = 0;
goto LABEL_14;
}
v7 += v8;
a3 -= v8;
}
while ( a3 > 0 );
MD5(v6, v5, v18);
v9 = v18;
do
{
sprintf(s, "%2.2x", (unsigned __int8)*v9);
strcat(a2, s);
++v9;
}
while ( v9 != &v19 );
v10 = 1;
LABEL_14:
free(v6);
if ( v4 > 0 )
close(v4);
}
else
{
puts("Malloc failed while compute md5");
close(v4);
v10 = 0;
}
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, char *a2, int a3)
{
int v4; // er12
__int64 v5; // r15
char *v6; // r13
int v7; // ebp
int v8; // eax
char *v9; // rbx
unsigned int v10; // ebx
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
char s[2]; // [rsp+1Dh] [rbp-4Bh] BYREF
char v17; // [rsp+1Fh] [rbp-49h]
char v18[16]; // [rsp+20h] [rbp-48h] BYREF
char v19; // [rsp+30h] [rbp-38h] BYREF

*(_WORD *)s = 0;
v17 = 0;
if ( !a2 )
_assert_fail("md5 != NULL", "command.c", 0xDEu, "compute_file_md5");
v4 = open(a1, 0);
if ( v4 < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
printf("open file %s failed while compute file md5: %s\n", a1, v12);
v10 = 0;
}
else
{
v5 = a3;
v6 = (char *)malloc(a3);
if ( v6 )
{
v7 = 0;
do
{
v8 = read(v4, &v6[v7], a3);
if ( v8 < 0 )
{
v13 = _errno_location();
v14 = strerror(*v13);
printf("Read file %s failed while compute file md5: %s\n", a1, v14);
v10 = 0;
goto LABEL_14;
}
v7 += v8;
a3 -= v8;
}
while ( a3 > 0 );
MD5(v6, v5, v18);
v9 = v18;
do
{
sprintf(s, "%2.2x", (unsigned __int8)*v9);
strcat(a2, s);
++v9;
}
while ( v9 != &v19 );
v10 = 1;
LABEL_14:
free(v6);
if ( v4 > 0 )
close(v4);
}
else
{
puts("Malloc failed while compute md5");
close(v4);
v10 = 0;
}
}
return v10;
}

original_code:
static int compute_file_md5(char *f_name, char *md5, int f_size)
{
 int fd;
 char *data = ((void *)0);
 int finished = 0;
 int remain = f_size;
 int ret;
 int res = 1;
 unsigned char md[16];
 int i;
 char tmp[3]={'\0'};
 ((void) sizeof ((md5 != ((void *)0)) ? 1 : 0), __extension__ ({ if (md5 != ((void *)0)) ; else __assert_fail ("md5 != NULL", "command.c", 222, __extension__ __PRETTY_FUNCTION__); }));
 fd = open(f_name, 00);
 if(fd < 0) {
  printf("open file %s failed while compute file md5: %s\n", f_name, strerror((*__errno_location ())));
  return 0;
 }
 data = (char *)malloc(f_size);
 if(data == ((void *)0)) {
  printf("Malloc failed while compute md5\n");
  close(fd);
  return 0;
 }
 for(;;) {
  ret = read(fd, data + finished, remain);
  if(ret < 0) {
   res = 0;
   printf("Read file %s failed while compute file md5: %s\n", f_name, strerror((*__errno_location ())));
   goto out;
  }
  finished += ret;
  remain -= ret;
  if(remain <= 0)
   break;
 }
 MD5((unsigned char *)data, f_size, md);
 for (i = 0; i < 16; i++) {
  sprintf(tmp,"%2.2x",md[i]);
  strcat(md5,tmp);
 }
out:
 if(data != ((void *)0))
  free(data);
 if(fd > 0)
  close(fd);
 return res;
}

canonical_original_code:
static int compute_file_md5(char *f_name, char *md5, int f_size)
{
 int fd;
 char *data = ((void *)0);
 int finished = 0;
 int remain = f_size;
 int ret;
 int res = 1;
 unsigned char md[16];
 int i;
 char tmp[3] = {'\0'};
 ((void) sizeof ((md5 != ((void *)0)) ? 1 : 0), __extension__ ({ if (md5 != ((void *)0)) ; else __assert_fail ("md5 != NULL", "command.c", 222, __extension__ __PRETTY_FUNCTION__); }));
 fd = open(f_name, 00);
 if(fd < 0) {
  printf("open file %s failed while compute file md5: %s\n", f_name, strerror((*__errno_location ())));
  return 0;
 }
 data = (char *)malloc(f_size);
 if(data == ((void *)0)) {
  printf("Malloc failed while compute md5\n");
  close(fd);
  return 0;
 }
 for(;;) {
  ret = read(fd, data + finished, remain);
  if(ret < 0) {
   res = 0;
   printf("Read file %s failed while compute file md5: %s\n", f_name, strerror((*__errno_location ())));
   goto out;
  }
  finished += ret;
  remain -= ret;
  if(remain <= 0)
   break;
 }
 MD5((unsigned char *)data, f_size, md);
 for (i = 0; i < 16; i++) {
  sprintf(tmp,"%2.2x",md[i]);
  strcat(md5,tmp);
 }
out:
 if(data != ((void *)0))
  free(data);
 if(fd > 0)
  close(fd);
 return res;
}

variable_types:
data
char *

variable_types: remain int

variable_types: ret int

variable_types:
md
unsigned char [16]

variable_types: i int

variable_types:
tmp
char [3]


errout

repo: traviscross/yealink-keycrack

decompiled_code:
void __noreturn errout()
{
perror("Error");
exit(254);
}

canonical_decompiled_code:
void __noreturn func12()
{
perror("Error");
exit(254);
}

original_code: static void errout() { perror("Error"); exit(254); }

return_type: void


decompiled_code:
void __fastcall __noreturn errout1(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(254);
}

canonical_decompiled_code:
void __fastcall __noreturn func13(const char *a1)
{
fprintf(stderr, "%s\n", a1);
exit(254);
}

original_code: static void errout1(const char *msg) { fprintf(stderr, "%s\n", msg); exit(254); }

canonical_original_code: static void errout1(char *msg) { fprintf(stderr, "%s\n", msg); exit(254); }

variable_types:
msg
char *


decompiled_code:
void __fastcall ms_srand(int a1)
{
ms_rseed = a1;
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
ms_rseed = a1;
}

original_code:
static inline void ms_srand(unsigned int x) {
  ms_rseed = x;
}

variable_types:
x
unsigned int


decompiled_code:
__int64 ms_rand()
{
ms_rseed = (214013 * ms_rseed + 2531011) & 0x7FFFFFFF;
return HIWORD(ms_rseed);
}

canonical_decompiled_code:
__int64 func8()
{
ms_rseed = (214013 * ms_rseed + 2531011) & 0x7FFFFFFF;
return HIWORD(ms_rseed);
}

original_code:
static inline int ms_rand() {
  return (ms_rseed = (ms_rseed * 214013 + 2531011) & MS_RAND_MAX) >> 16;
}

return_type: int


rchar

decompiled_code:
__int64 rchar()
{
return (unsigned __int8)keymap[(int)ms_rand() % 62];
}

canonical_decompiled_code:
__int64 func9()
{
return (unsigned __int8)keymap[(int)func8() % 62];
}

original_code:
static uchar rchar() {
  int i = ms_rand() % 62;
  return keymap[i];
}

canonical_original_code:
static unsigned char rchar() {
  int i = ms_rand() % 62;
  return keymap[i];
}

return_type: unsigned char


repo: trewtzu/JAC-RMIT-OSP

decompiled_code:
void __fastcall free_data(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
static void
free_data (qnode_t *node)
{
  free (node->song);
  free (node);
}

canonical_original_code:
static void free_data(struct qnode *node)
{
  free (node->song);
  free (node);
}

variable_types: struct qnode { char *song; struct qnode *next; } *


decompiled_code:
__int64 __fastcall insert_node(__int64 a1, __int64 a2)
{
int v2; // ebp
__int64 result; // rax

if ( !a1 )
_assert_fail("queue", "queue.c", 0x15u, "insert_node");
*(_QWORD *)(a2 + 8) = 0LL;
if ( *(_DWORD *)(a1 + 16) )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = a2;
else
*(_QWORD *)a1 = a2;
*(_QWORD *)(a1 + 8) = a2;
v2 = *(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 20) = v2 + 16;
result = v2 + (unsigned int)strlen(*(const char **)a2) + 17;
*(_DWORD *)(a1 + 20) = result;
++*(_DWORD *)(a1 + 16);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v2; // ebp
__int64 result; // rax

if ( !a1 )
_assert_fail("queue", "queue.c", 0x15u, "insert_node");
*(_QWORD *)(a2 + 8) = 0LL;
if ( *(_DWORD *)(a1 + 16) )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = a2;
else
*(_QWORD *)a1 = a2;
*(_QWORD *)(a1 + 8) = a2;
v2 = *(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 20) = v2 + 16;
result = v2 + (unsigned int)strlen(*(const char **)a2) + 17;
*(_DWORD *)(a1 + 20) = result;
++*(_DWORD *)(a1 + 16);
return result;
}

original_code:
static void
insert_node (queue_t *queue, qnode_t *node)
{
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 21, __extension__ __PRETTY_FUNCTION__); }));
  node->next = ((void *)0);
  if (queue->count == 0)
    queue->head = node;
  else
    queue->foot->next = node;
  queue->foot = node;
  queue->size += sizeof (*node);
  queue->size += ((strlen(node->song) + 1) * sizeof (char));
  queue->count++;
  return;
}

canonical_original_code:
static void insert_node(struct queue_t *queue, struct qnode *node)
{
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 21, __extension__ __PRETTY_FUNCTION__); }));
  node->next = ((void *)0);
  if (queue->count == 0)
    queue->head = node;
  else
    queue->foot->next = node;
  queue->foot = node;
  queue->size += sizeof (*node);
  queue->size += ((strlen(node->song) + 1) * sizeof (char));
  queue->count++;
  return;
}

variable_types: struct queue_t { struct qnode { char *song; struct qnode *next; } *head; struct qnode *foot; int count; int size; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *

variable_types:
node
struct qnode *

user_defined_types: struct queue_t { struct qnode *head; struct qnode *foot; int count; int size; union pthread_mutex_t mutex; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 new_queue()
{
__int64 v0; // rbx

v0 = safe_malloc(64LL);
*(_QWORD *)v0 = 0LL;
*(_QWORD *)(v0 + 8) = 0LL;
*(_DWORD *)(v0 + 16) = 0;
*(_DWORD *)(v0 + 20) = 64;
pthread_mutex_init((pthread_mutex_t *)(v0 + 24), 0LL);
return v0;
}

original_code:
queue_t *
new_queue ()
{
  queue_t *new_queue = ((void *)0);
  new_queue = safe_malloc (sizeof (queue_t));
  new_queue->head = ((void *)0);
  new_queue->foot = ((void *)0);
  new_queue->count = 0;
  new_queue->size = sizeof (*new_queue);
  pthread_mutex_init (&(new_queue->mutex), ((void *)0));
  return (new_queue);
}

canonical_original_code:
struct queue_t *new_queue()
{
  struct queue_t *new_queue = ((void *)0);
  new_queue = safe_malloc (sizeof (queue_t));
  new_queue->head = ((void *)0);
  new_queue->foot = ((void *)0);
  new_queue->count = 0;
  new_queue->size = sizeof (*new_queue);
  pthread_mutex_init (&(new_queue->mutex), ((void *)0));
  return (new_queue);
}

return_type: struct queue_t { struct qnode { char *song; struct qnode *next; } *head; struct qnode *foot; int count; int size; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; } *


decompiled_code:
void __fastcall delete_queue(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free_data(v3);
}
while ( v2 );
}
free(ptr);
}

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

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[1];
func0(v3);
}
while ( v2 );
}
free(ptr);
}

original_code:
void
delete_queue (queue_t *queue)
{
  qnode_t *node = ((void *)0);
  qnode_t *tmp = ((void *)0);
  node = queue->head;
  while (node != ((void *)0))
  {
    tmp = node;
    node = node->next;
    free_data (tmp);
  }
  free (queue);
 return;
}

canonical_original_code:
void delete_queue(struct queue_t *queue)
{
  struct qnode *node = ((void *)0);
  struct qnode *tmp = ((void *)0);
  node = queue->head;
  while (node != ((void *)0))
  {
    tmp = node;
    node = node->next;
    free_data (tmp);
  }
  free (queue);
 return;
}

user_defined_types: struct qnode { char *song; struct qnode *next; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
int __fastcall add_song(__int64 a1, __int64 a2)
{
_QWORD *v2; // r12

if ( !a1 )
_assert_fail("queue", "queue.c", 0x73u, "add_song");
if ( !a2 )
_assert_fail("song", "queue.c", 0x74u, "add_song");
v2 = (_QWORD *)safe_malloc(16LL);
*v2 = safe_strdup(a2);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
insert_node(a1, (__int64)v2);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2)
{
_QWORD *v2; // r12

if ( !a1 )
_assert_fail("queue", "queue.c", 0x73u, "add_song");
if ( !a2 )
_assert_fail("song", "queue.c", 0x74u, "add_song");
v2 = (_QWORD *)safe_malloc(16LL);
*v2 = safe_strdup(a2);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
func1(a1, (__int64)v2);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
}

original_code:
void
add_song (queue_t *queue, char *song)
{
  qnode_t *new_node = ((void *)0);
  char *new_song = ((void *)0);
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 115, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((song) ? 1 : 0), __extension__ ({ if (song) ; else __assert_fail ("song", "queue.c", 116, __extension__ __PRETTY_FUNCTION__); }));
  new_node = safe_malloc (sizeof (qnode_t));
  new_song = safe_strdup (song);
  new_node->song = new_song;
  pthread_mutex_lock (&(queue->mutex));
  insert_node (queue, new_node);
  pthread_mutex_unlock (&(queue->mutex));
  return;
}

canonical_original_code:
void add_song(struct queue_t *queue, char *song)
{
  struct qnode *new_node = ((void *)0);
  char *new_song = ((void *)0);
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 115, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((song) ? 1 : 0), __extension__ ({ if (song) ; else __assert_fail ("song", "queue.c", 116, __extension__ __PRETTY_FUNCTION__); }));
  new_node = safe_malloc (sizeof (qnode_t));
  new_song = safe_strdup (song);
  new_node->song = new_song;
  pthread_mutex_lock (&(queue->mutex));
  insert_node (queue, new_node);
  pthread_mutex_unlock (&(queue->mutex));
  return;
}

variable_types:
song
char *


original_code:
int
queue_song (queue_t *queue, queue_t *list, int index)
{
  int i = 1;
  qnode_t *node = ((void *)0);
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 136, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((list) ? 1 : 0), __extension__ ({ if (list) ; else __assert_fail ("list", "queue.c", 137, __extension__ __PRETTY_FUNCTION__); }));
  if (index < 1 || index > list->count)
    return 0;
  node = list->head;
  while (node != ((void *)0) && i < index)
  {
    node = node->next;
    ++i;
  }
  if (node != ((void *)0))
    add_song (queue, node->song);
  return 1;
}

canonical_original_code:
int queue_song(struct queue_t *queue, struct queue_t *list, int index)
{
  int i = 1;
  struct qnode *node = ((void *)0);
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 136, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((list) ? 1 : 0), __extension__ ({ if (list) ; else __assert_fail ("list", "queue.c", 137, __extension__ __PRETTY_FUNCTION__); }));
  if (index < 1 || index > list->count)
    return 0;
  node = list->head;
  while (node != ((void *)0) && i < index)
  {
    node = node->next;
    ++i;
  }
  if (node != ((void *)0))
    add_song (queue, node->song);
  return 1;
}

variable_types:
list
struct queue_t *

variable_types: index int


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

if ( !a1 )
_assert_fail("queue", "queue.c", 0xA0u, "auto_queue");
if ( !a2 )
_assert_fail("list", "queue.c", 0xA1u, "auto_queue");
v2 = time(0LL);
srand(v2);
v3 = rand();
return queue_song(a1, a2, v3 % *(_DWORD *)(a2 + 16) + 1);
}

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

if ( !a1 )
_assert_fail("queue", "queue.c", 0xA0u, "auto_queue");
if ( !a2 )
_assert_fail("list", "queue.c", 0xA1u, "auto_queue");
v2 = time(0LL);
srand(v2);
v3 = rand();
return func6(a1, a2, v3 % *(_DWORD *)(a2 + 16) + 1);
}

original_code:
void
auto_queue (queue_t *queue, queue_t *list)
{
  int index;
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 160, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((list) ? 1 : 0), __extension__ ({ if (list) ; else __assert_fail ("list", "queue.c", 161, __extension__ __PRETTY_FUNCTION__); }));
  srand(time(((void *)0)));
  index = ((rand() % list->count) + 1);
  queue_song (queue, list, index);
  return;
}

canonical_original_code:
void auto_queue(struct queue_t *queue, struct queue_t *list)
{
  int index;
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 160, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((list) ? 1 : 0), __extension__ ({ if (list) ; else __assert_fail ("list", "queue.c", 161, __extension__ __PRETTY_FUNCTION__); }));
  srand(time(((void *)0)));
  index = ((rand() % list->count) + 1);
  queue_song (queue, list, index);
  return;
}


decompiled_code:
__int64 __fastcall retrieve_song(__int64 a1, __int64 a2)
{
__int64 v2; // rbp

if ( !a1 )
_assert_fail("queue", "queue.c", 0xAFu, "retrieve_song");
v2 = 0LL;
if ( *(_DWORD *)(a1 + 16) )
{
v2 = safe_strdup(**(_QWORD **)a1);
if ( *(_DWORD *)(a1 + 16) == 1 )
auto_queue(a1, a2);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
__int64 v2; // rbp

if ( !a1 )
_assert_fail("queue", "queue.c", 0xAFu, "retrieve_song");
v2 = 0LL;
if ( *(_DWORD *)(a1 + 16) )
{
v2 = safe_strdup(**(_QWORD **)a1);
if ( *(_DWORD *)(a1 + 16) == 1 )
func7(a1, a2);
}
return v2;
}

original_code:
char *
retrieve_song (queue_t *queue, queue_t *list)
{
  char *next_song = ((void *)0);
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 175, __extension__ __PRETTY_FUNCTION__); }));
  if (queue->count == 0)
    return (((void *)0));
  next_song = safe_strdup (queue->head->song);
  if (queue->count == 1)
    auto_queue(queue, list);
  return (next_song);
}

canonical_original_code:
char *retrieve_song(struct queue_t *queue, struct queue_t *list)
{
  char *next_song = ((void *)0);
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 175, __extension__ __PRETTY_FUNCTION__); }));
  if (queue->count == 0)
    return (((void *)0));
  next_song = safe_strdup (queue->head->song);
  if (queue->count == 1)
    auto_queue(queue, list);
  return (next_song);
}

return_type: char *


decompiled_code:
int __fastcall update_queue(__int64 a1)
{
if ( !a1 )
_assert_fail("queue", "queue.c", 0xBFu, "update_queue");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
remove_node((__int64 *)a1);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1)
{
if ( !a1 )
_assert_fail("queue", "queue.c", 0xBFu, "update_queue");
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
func2((__int64 *)a1);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
}

original_code:
void
update_queue (queue_t *queue)
{
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 191, __extension__ __PRETTY_FUNCTION__); }));
  pthread_mutex_lock (&(queue->mutex));
  remove_node (queue, queue->head);
  pthread_mutex_unlock (&(queue->mutex));
  return;
}

canonical_original_code:
void update_queue(struct queue_t *queue)
{
  ((void) sizeof ((queue) ? 1 : 0), __extension__ ({ if (queue) ; else __assert_fail ("queue", "queue.c", 191, __extension__ __PRETTY_FUNCTION__); }));
  pthread_mutex_lock (&(queue->mutex));
  remove_node (queue, queue->head);
  pthread_mutex_unlock (&(queue->mutex));
  return;
}


decompiled_code:
int __fastcall add_to_queue(int fd)
{
const char *v1; // rdx
const char *v2; // rdx
const char *v3; // rdx
int result; // eax
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

printf("\nEnter number of song to add: ");
fgets(v5, 255, stdin);
if ( strlen(v5) != 2 || (unsigned int)(v5[0] - 48) > 9 )
goto LABEL_11;
if ( (int)write(fd, "102", 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)"102", v1);
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)v5, v2);
if ( (int)read(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR reading from socket", (int)v5, v3);
if ( atoi(v5) )
result = puts("Selection added to queue!");
else
LABEL_11:
result = puts("Invalid selection!");
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd)
{
const char *v1; // rdx
const char *v2; // rdx
const char *v3; // rdx
int result; // eax
char v5[264]; // [rsp+0h] [rbp-108h] BYREF

printf("\nEnter number of song to add: ");
fgets(v5, 255, stdin);
if ( strlen(v5) != 2 || (unsigned int)(v5[0] - 48) > 9 )
goto LABEL_11;
if ( (int)write(fd, "102", 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)"102", v1);
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)v5, v2);
if ( (int)read(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR reading from socket", (int)v5, v3);
if ( atoi(v5) )
result = puts("Selection added to queue!");
else
LABEL_11:
result = puts("Invalid selection!");
return result;
}

original_code:
void add_to_queue(int sockfd)
{
    int n, result;
    int song_choice;
    char buffer[255 +1];
    printf ("\nEnter number of song to add: ");
    fgets(buffer, 255, stdin);
    if (strlen(buffer) != 2 || !isdigit(buffer[0]))
    {
      printf("Invalid selection!\n");
      return;
    }
    n = write(sockfd, "102", 255);
    if (n < 0)
      error("ERROR writing to socket");
    n = write(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR writing to socket");
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR reading from socket");
    result = atoi(buffer);
    if (!result)
      printf("Invalid selection!\n");
    else
      printf("Selection added to queue!\n");
    return;
}

canonical_original_code:
void add_to_queue(int sockfd)
{
    int n; int result;
    int song_choice;
    char buffer[-1];
    printf ("\nEnter number of song to add: ");
    fgets(buffer, 255, stdin);
    if (strlen(buffer) != 2 || !isdigit(buffer[0]))
    {
      printf("Invalid selection!\n");
      return;
    }
    n = write(sockfd, "102", 255);
    if (n < 0)
      error("ERROR writing to socket");
    n = write(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR writing to socket");
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR reading from socket");
    result = atoi(buffer);
    if (!result)
      printf("Invalid selection!\n");
    else
      printf("Selection added to queue!\n");
    return;
}

variable_types: sockfd int

variable_types: n int

variable_types: result int

variable_types:
buffer
char [-1]


variable_types:
list
char **

variable_types:
queue
char **


decompiled_code:
void __fastcall list_request(int fd)
{
const char *v1; // rdx
const char *v2; // rdx
int v3; // er12
__int64 v4; // rax
__int64 *v5; // rbp
__int64 *v6; // rbx
const char *v7; // rdx
const char *v8; // rdx
int v9; // er14
__int64 v10; // rax
void **v11; // r12
void **v12; // rbx
const char *v13; // rdx
void *v14; // rdi
__int64 *v15; // rbx
void *v16; // rdi
_QWORD *v17; // rbx
char v18[312]; // [rsp+0h] [rbp-138h] BYREF

if ( (int)write(fd, "101", 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)"101", v1);
memset(v18, 0, 0x100uLL);
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v2);
v3 = atoi(v18);
v4 = safe_malloc(8LL * (v3 + 1));
v5 = (__int64 *)v4;
if ( v3 <= 0 )
{
v3 = 0;
}
else
{
v6 = (__int64 *)v4;
do
{
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v7);
*v6++ = safe_strdup(v18);
}
while ( v6 != &v5[(unsigned int)(v3 - 1) + 1] );
}
v5[v3] = 0LL;
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v8);
v9 = atoi(v18);
v10 = safe_malloc(8LL * (v9 + 1));
v11 = (void **)v10;
if ( v9 <= 0 )
{
v9 = 0;
}
else
{
v12 = (void **)v10;
do
{
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v13);
*v12++ = (void *)safe_strdup(v18);
}
while ( v12 != &v11[(unsigned int)(v9 - 1) + 1] );
}
v11[v9] = 0LL;
list_print(v5, (__int64)v11);
v14 = (void *)*v5;
if ( *v5 )
{
v15 = v5 + 1;
do
{
free(v14);
v14 = (void *)*v15++;
}
while ( v14 );
}
v16 = *v11;
if ( *v11 )
{
v17 = v11 + 1;
do
{
free(v16);
v16 = (void *)*v17++;
}
while ( v16 );
}
free(v5);
free(v11);
}

canonical_decompiled_code:
void __fastcall func2(int fd)
{
const char *v1; // rdx
const char *v2; // rdx
int v3; // er12
__int64 v4; // rax
__int64 *v5; // rbp
__int64 *v6; // rbx
const char *v7; // rdx
const char *v8; // rdx
int v9; // er14
__int64 v10; // rax
void **v11; // r12
void **v12; // rbx
const char *v13; // rdx
void *v14; // rdi
__int64 *v15; // rbx
void *v16; // rdi
_QWORD *v17; // rbx
char v18[312]; // [rsp+0h] [rbp-138h] BYREF

if ( (int)write(fd, "101", 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)"101", v1);
memset(v18, 0, 0x100uLL);
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v2);
v3 = atoi(v18);
v4 = safe_malloc(8LL * (v3 + 1));
v5 = (__int64 *)v4;
if ( v3 <= 0 )
{
v3 = 0;
}
else
{
v6 = (__int64 *)v4;
do
{
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v7);
*v6++ = safe_strdup(v18);
}
while ( v6 != &v5[(unsigned int)(v3 - 1) + 1] );
}
v5[v3] = 0LL;
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v8);
v9 = atoi(v18);
v10 = safe_malloc(8LL * (v9 + 1));
v11 = (void **)v10;
if ( v9 <= 0 )
{
v9 = 0;
}
else
{
v12 = (void **)v10;
do
{
if ( (int)read(fd, v18, 0xFFuLL) < 0 )
error((int)"ERROR read from socket", (int)v18, v13);
*v12++ = (void *)safe_strdup(v18);
}
while ( v12 != &v11[(unsigned int)(v9 - 1) + 1] );
}
v11[v9] = 0LL;
func1(v5, (__int64)v11);
v14 = (void *)*v5;
if ( *v5 )
{
v15 = v5 + 1;
do
{
free(v14);
v14 = (void *)*v15++;
}
while ( v14 );
}
v16 = *v11;
if ( *v11 )
{
v17 = v11 + 1;
do
{
free(v16);
v16 = (void *)*v17++;
}
while ( v16 );
}
free(v5);
free(v11);
}

original_code:
void list_request(int sockfd)
{
    char **allsongs;
    char **qsongs;
 char buffer[255 +1];
    int song_count;
    int q_count;
    int i, n;
    n = write(sockfd, "101", 255);
    if (n < 0)
      error("ERROR writing to socket");
    (memset((buffer), '\0', (255 +1)), (void) 0);
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR read from socket");
    song_count = atoi(buffer);
    allsongs = safe_malloc ((song_count + 1) * sizeof (char *));
    i = 0;
    while(i < song_count)
    {
      n = read(sockfd, buffer, 255);
      if (n < 0)
        error("ERROR read from socket");
      allsongs[i] = safe_strdup (buffer);
      i++;
    }
    allsongs[i] = ((void *)0);
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR read from socket");
    q_count = atoi(buffer);
    qsongs = safe_malloc ((q_count + 1) * sizeof (char *));
    i = 0;
    while(i < q_count)
    {
      n = read(sockfd,buffer,255);
      if (n < 0)
        error("ERROR read from socket");
      qsongs[i] = safe_strdup (buffer);
      i++;
    }
    qsongs[i] = ((void *)0);
    list_print(allsongs, qsongs);
    for (i = 0; allsongs[i] != ((void *)0); i++)
      free (allsongs[i]);
    for (i = 0; qsongs[i] != ((void *)0); i++)
      free (qsongs[i]);
    free (allsongs);
    free (qsongs);
    return;
}

canonical_original_code:
void list_request(int sockfd)
{
    char **allsongs;
    char **qsongs;
 char buffer[-1];
    int song_count;
    int q_count;
    int i; int n;
    n = write(sockfd, "101", 255);
    if (n < 0)
      error("ERROR writing to socket");
    (memset((buffer), '\0', (255 +1)), (void) 0);
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR read from socket");
    song_count = atoi(buffer);
    allsongs = safe_malloc ((song_count + 1) * sizeof (char *));
    i = 0;
    while(i < song_count)
    {
      n = read(sockfd, buffer, 255);
      if (n < 0)
        error("ERROR read from socket");
      allsongs[i] = safe_strdup (buffer);
      i++;
    }
    allsongs[i] = ((void *)0);
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("ERROR read from socket");
    q_count = atoi(buffer);
    qsongs = safe_malloc ((q_count + 1) * sizeof (char *));
    i = 0;
    while(i < q_count)
    {
      n = read(sockfd,buffer,255);
      if (n < 0)
        error("ERROR read from socket");
      qsongs[i] = safe_strdup (buffer);
      i++;
    }
    qsongs[i] = ((void *)0);
    list_print(allsongs, qsongs);
    for (i = 0; allsongs[i] != ((void *)0); i++)
      free (allsongs[i]);
    for (i = 0; qsongs[i] != ((void *)0); i++)
      free (qsongs[i]);
    free (allsongs);
    free (qsongs);
    return;
}

variable_types:
allsongs
char **

variable_types:
qsongs
char **


decompiled_code:
int print_menu()
{
puts("\nMENU --------------------");
puts("1) Display song lists");
puts("2) Add to queue");
return puts("9) Quit Virtual JukeBox");
}

canonical_decompiled_code:
int func3()
{
puts("\nMENU --------------------");
puts("1) Display song lists");
puts("2) Add to queue");
return puts("9) Quit Virtual JukeBox");
}

original_code:
void print_menu()
{
  printf ("\nMENU --------------------\n");
  printf ("1) Display song lists\n");
  printf ("2) Add to queue\n");
  printf ("9) Quit Virtual JukeBox\n");
}


begin

decompiled_code:
void __fastcall begin(int a1)
{
int v1; // eax
int v2; // ebx
_BOOL4 v3; // er12
char v4[296]; // [rsp+0h] [rbp-128h] BYREF

print_logo();
while ( 2 )
{
print_menu();
do
{
while ( 1 )
{
printf("\nOption: ");
memset(v4, 0, 0x100uLL);
fgets(v4, 255, stdin);
v3 = 0;
if ( strlen(v4) == 2 )
v3 = (unsigned int)(v4[0] - 48) <= 9;
v1 = atoi(v4);
v2 = v1;
if ( v1 == 2 )
break;
if ( v1 == 9 )
{
if ( v3 )
return;
}
else if ( v1 == 1 )
{
list_request(a1);
goto LABEL_10;
}
}
add_to_queue(a1);
LABEL_10:
;
}
while ( !v3 );
if ( v2 != 9 )
continue;
break;
}
}

canonical_decompiled_code:
void __fastcall func5(int a1)
{
int v1; // eax
int v2; // ebx
_BOOL4 v3; // er12
char v4[296]; // [rsp+0h] [rbp-128h] BYREF

func4();
while ( 2 )
{
func3();
do
{
while ( 1 )
{
printf("\nOption: ");
memset(v4, 0, 0x100uLL);
fgets(v4, 255, stdin);
v3 = 0;
if ( strlen(v4) == 2 )
v3 = (unsigned int)(v4[0] - 48) <= 9;
v1 = atoi(v4);
v2 = v1;
if ( v1 == 2 )
break;
if ( v1 == 9 )
{
if ( v3 )
return;
}
else if ( v1 == 1 )
{
func2(a1);
goto LABEL_10;
}
}
func0(a1);
LABEL_10:
;
}
while ( !v3 );
if ( v2 != 9 )
continue;
break;
}
}

original_code:
void begin(int sockfd)
{
  char selection[255 +1];
  int sock;
  int choice, valid, result;
  sock = sockfd;
  print_logo();
  choice = 0;
  do
  {
    print_menu();
    do
    {
      valid = 1;
      printf ("\nOption: ");
      (memset((selection), '\0', (255 +1)), (void) 0);
      fgets(selection, 255, stdin);
      if (strlen(selection) != 2)
        valid = 0;
      else if (!isdigit(selection[0]))
        valid = 0;
      choice = atoi(selection);
      switch (choice)
      {
        case 1:
              list_request(sock);
              break;
        case 2:
              add_to_queue(sock);
              break;
        case 9:
              choice = 9;
              break;
        default:
              valid = 0;
              break;
      }
    }while (!valid);
  }while (choice != 9);
}

canonical_original_code:
void begin(int sockfd)
{
  char selection[-1];
  int sock;
  int choice; int valid; int result;
  sock = sockfd;
  print_logo();
  choice = 0;
  do
  {
    print_menu();
    do
    {
      valid = 1;
      printf ("\nOption: ");
      (memset((selection), '\0', (255 +1)), (void) 0);
      fgets(selection, 255, stdin);
      if (strlen(selection) != 2)
        valid = 0;
      else if (!isdigit(selection[0]))
        valid = 0;
      choice = atoi(selection);
      switch (choice)
      {
        case 1:
              list_request(sock);
              break;
        case 2:
              add_to_queue(sock);
              break;
        case 9:
              choice = 9;
              break;
        default:
              valid = 0;
              break;
      }
    }while (!valid);
  }while (choice != 9);
}

variable_types:
selection
char [-1]

variable_types: sock int

variable_types: choice int

variable_types: valid int


beginStreaming

decompiled_code:
void __fastcall __noreturn beginStreaming(void *a1)
{
int v1; // eax
int v2; // er13
const void *v3; // rsi
__int64 v4; // r12
int v5; // eax
__int64 v6; // rdx
__useconds_t v7; // er15
int v8; // ebx
int v9; // ebp
int v10; // eax
void *ptr; // [rsp+8h] [rbp-80h]
int optval; // [rsp+14h] [rbp-74h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-68h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( getaddrinfo("127.255.255.255", "8909", &req, &pai) )
error("ERROR getting stream address info");
v1 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
v2 = v1;
if ( v1 != -1 )
{
if ( setsockopt(v1, 1, 6, &optval, 4u) != -1 )
{
while ( 1 )
{
v3 = (const void *)list;
ptr = (void *)retrieve_song(queue, list);
if ( !ptr )
break;
v4 = createWav(ptr);
if ( !(unsigned __int8)loadWavData(v4) )
error("ERROR loading wave file");
v5 = getFrameSize(*(_QWORD *)(v4 + 16));
v6 = *(_DWORD *)(*(_QWORD *)(v4 + 16) + 12LL) * v5 % 0x5BEu;
v7 = (int)(1.0 / (double)(*(_DWORD *)(*(_QWORD *)(v4 + 16) + 12LL) * v5 / 0x5BEu) * 955000.0);
v8 = **(_DWORD **)(v4 + 24);
if ( v8 > 0 )
{
v9 = 0;
do
{
v3 = (const void *)(*(_QWORD *)(*(_QWORD *)(v4 + 24) + 8LL) + v9);
v10 = sendto(v2, v3, 0x5BEuLL, 0, pai->ai_addr, pai->ai_addrlen);
if ( v10 == -1 )
error("ERROR sending stream data");
v9 += v10;
v8 -= v10;
usleep(v7);
}
while ( v8 > 0 );
}
update_queue(queue, v3, v6);
deleteWav(v4);
free(ptr);
}
error("ERROR queue is empty");
}
error("ERROR setting sock opt");
}
error("ERROR creating socket");
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
int v1; // eax
int v2; // er13
const void *v3; // rsi
__int64 v4; // r12
int v5; // eax
__int64 v6; // rdx
__useconds_t v7; // er15
int v8; // ebx
int v9; // ebp
int v10; // eax
void *ptr; // [rsp+8h] [rbp-80h]
int optval; // [rsp+14h] [rbp-74h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-68h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( getaddrinfo("127.255.255.255", "8909", &req, &pai) )
func1("ERROR getting stream address info");
v1 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
v2 = v1;
if ( v1 != -1 )
{
if ( setsockopt(v1, 1, 6, &optval, 4u) != -1 )
{
while ( 1 )
{
v3 = (const void *)list;
ptr = (void *)retrieve_song(queue, list);
if ( !ptr )
break;
v4 = createWav(ptr);
if ( !(unsigned __int8)loadWavData(v4) )
func1("ERROR loading wave file");
v5 = getFrameSize(*(_QWORD *)(v4 + 16));
v6 = *(_DWORD *)(*(_QWORD *)(v4 + 16) + 12LL) * v5 % 0x5BEu;
v7 = (int)(1.0 / (double)(*(_DWORD *)(*(_QWORD *)(v4 + 16) + 12LL) * v5 / 0x5BEu) * 955000.0);
v8 = **(_DWORD **)(v4 + 24);
if ( v8 > 0 )
{
v9 = 0;
do
{
v3 = (const void *)(*(_QWORD *)(*(_QWORD *)(v4 + 24) + 8LL) + v9);
v10 = sendto(v2, v3, 0x5BEuLL, 0, pai->ai_addr, pai->ai_addrlen);
if ( v10 == -1 )
func1("ERROR sending stream data");
v9 += v10;
v8 -= v10;
usleep(v7);
}
while ( v8 > 0 );
}
update_queue(queue, v3, v6);
deleteWav(v4);
free(ptr);
}
func1("ERROR queue is empty");
}
func1("ERROR setting sock opt");
}
func1("ERROR creating socket");
}

original_code:
void *beginStreaming(void *args)
{
  int sockfd, err, bytesSent;
  struct addrinfo hints, *destInfo;
  int broadcast = 1;
  WavFile *wav = ((void *)0);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_DGRAM;
  err = getaddrinfo("127.255.255.255", "8909", &hints, &destInfo);
  if (err != 0)
    error("ERROR getting stream address info");
  sockfd = socket(destInfo->ai_family, destInfo->ai_socktype,
    destInfo->ai_protocol);
  if (sockfd == -1)
    error("ERROR creating socket");
  err = setsockopt(sockfd, 1, 6, &broadcast,
    sizeof broadcast);
  if (err == -1)
    error("ERROR setting sock opt");
  while (1){
    char *songName = retrieve_song(queue, list);
    if (songName == ((void *)0))
      error("ERROR queue is empty");
    wav = createWav(songName);
    if (!loadWavData(wav))
      error("ERROR loading wave file");
    int packetSize = 1470;
    uint frameSize = getFrameSize(wav->format);
    uint bps = wav->format->sampleRate * frameSize;
    uint pps = bps / packetSize;
    uint delay = (1.0 / pps) * 955000;
    int totalBytes = 0;
    int bytesLeft = wav->data->size;
    while (bytesLeft > 0){
      int availBytes = packetSize;
      if (bytesLeft < availBytes)
        availBytes = bytesLeft;
      bytesSent = sendto(sockfd, wav->data->data + totalBytes, packetSize, 0,
          destInfo->ai_addr, destInfo->ai_addrlen);
      if (bytesSent == -1)
        error("ERROR sending stream data");
      totalBytes += bytesSent;
      bytesLeft -= bytesSent;
      usleep(delay);
    }
    update_queue(queue);
    deleteWav(wav);
    free(songName);
  }
  freeaddrinfo(destInfo);
  close(sockfd);
}

canonical_original_code:
void *beginStreaming(void *args)
{
  int sockfd; int err; int bytesSent;
  struct addrinfo hints; struct addrinfo *destInfo;
  int broadcast = 1;
  struct WavFile *wav = ((void *)0);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_DGRAM;
  err = getaddrinfo("127.255.255.255", "8909", &hints, &destInfo);
  if (err != 0)
    error("ERROR getting stream address info");
  sockfd = socket(destInfo->ai_family, destInfo->ai_socktype,
    destInfo->ai_protocol);
  if (sockfd == -1)
    error("ERROR creating socket");
  err = setsockopt(sockfd, 1, 6, &broadcast,
    sizeof broadcast);
  if (err == -1)
    error("ERROR setting sock opt");
  while (1){
    char *songName = retrieve_song(queue, list);
    if (songName == ((void *)0))
      error("ERROR queue is empty");
    wav = createWav(songName);
    if (!loadWavData(wav))
      error("ERROR loading wave file");
    int packetSize = 1470;
    unsigned int frameSize = getFrameSize(wav->format);
    unsigned int bps = wav->format->sampleRate * frameSize;
    unsigned int pps = bps / packetSize;
    unsigned int delay = (1.0 / pps) * 955000;
    int totalBytes = 0;
    int bytesLeft = wav->data->size;
    while (bytesLeft > 0){
      int availBytes = packetSize;
      if (bytesLeft < availBytes)
        availBytes = bytesLeft;
      bytesSent = sendto(sockfd, wav->data->data + totalBytes, packetSize, 0,
          destInfo->ai_addr, destInfo->ai_addrlen);
      if (bytesSent == -1)
        error("ERROR sending stream data");
      totalBytes += bytesSent;
      bytesLeft -= bytesSent;
      usleep(delay);
    }
    update_queue(queue);
    deleteWav(wav);
    free(songName);
  }
  freeaddrinfo(destInfo);
  close(sockfd);
}

variable_types:
args
void *

variable_types: err int

variable_types: bytesSent int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: broadcast int

variable_types: struct WavFile { char *filename; unsigned int size; struct FormatChunk { unsigned int size; unsigned int compressionCode; unsigned int numChannels; unsigned int sampleRate; unsigned int averageBPS; unsigned int blockAlign; unsigned int significantBPS; } *format; struct DataChunk { unsigned int size; unsigned char *data; } *data; } *

variable_types:
songName
char *

variable_types: packetSize int

variable_types:
frameSize
unsigned int

variable_types:
bps
unsigned int

variable_types:
pps
unsigned int

variable_types:
delay
unsigned int

variable_types: totalBytes int

variable_types: bytesLeft int

variable_types: availBytes int

return_type: void *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

user_defined_types: struct WavFile { char *filename; unsigned int size; struct FormatChunk *format; struct DataChunk *data; }


usage

decompiled_code:
size_t usage()
{
return fwrite("Usage: server portNum\n", 1uLL, 0x16uLL, stdout);
}

canonical_decompiled_code:
size_t func3()
{
return fwrite("Usage: server portNum\n", 1uLL, 0x16uLL, stdout);
}

original_code:
void usage()
{
 fprintf(stdout,"Usage: server portNum\n");
}


sendListings

decompiled_code:
void __fastcall sendListings(int fd)
{
const void **v1; // r12
const void **v2; // r13
const void *v3; // rsi
_QWORD *i; // rbx
const void *v5; // rsi
_QWORD *v6; // rbx
void *v7; // rdi
_QWORD *v8; // rbx
void *v9; // rdi
_QWORD *v10; // rbx
char v11[296]; // [rsp+0h] [rbp-128h] BYREF

v1 = (const void **)get_list(list);
v2 = (const void **)get_list(queue);
sprintf(v11, "%d", *(unsigned int *)(list + 16));
if ( (int)write(fd, v11, 0xFFuLL) < 0 )
error("ERROR writing to socket");
v3 = *v1;
for ( i = v1 + 1; v3; v3 = (const void *)*(i - 1) )
{
if ( (int)write(fd, v3, 0xFFuLL) < 0 )
error("ERROR writing to socket");
usleep(0x2710u);
++i;
}
sprintf(v11, "%d", *(unsigned int *)(queue + 16));
if ( (int)write(fd, v11, 0xFFuLL) < 0 )
error("ERROR writing to socket");
v5 = *v2;
v6 = v2 + 1;
if ( *v2 )
{
do
{
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
error("ERROR writing to socket");
usleep(0x2710u);
v5 = (const void *)*v6++;
}
while ( v5 );
v7 = (void *)*v1;
if ( !*v1 )
goto LABEL_12;
}
else
{
v7 = (void *)*v1;
if ( !*v1 )
goto LABEL_15;
}
v8 = v1 + 1;
do
{
free(v7);
v7 = (void *)*v8++;
}
while ( v7 );
LABEL_12:
v9 = (void *)*v2;
if ( *v2 )
{
v10 = v2 + 1;
do
{
free(v9);
v9 = (void *)*v10++;
}
while ( v9 );
}
LABEL_15:
free(v1);
free(v2);
}

canonical_decompiled_code:
void __fastcall func4(int fd)
{
const void **v1; // r12
const void **v2; // r13
const void *v3; // rsi
_QWORD *i; // rbx
const void *v5; // rsi
_QWORD *v6; // rbx
void *v7; // rdi
_QWORD *v8; // rbx
void *v9; // rdi
_QWORD *v10; // rbx
char v11[296]; // [rsp+0h] [rbp-128h] BYREF

v1 = (const void **)get_list(list);
v2 = (const void **)get_list(queue);
sprintf(v11, "%d", *(unsigned int *)(list + 16));
if ( (int)write(fd, v11, 0xFFuLL) < 0 )
func1("ERROR writing to socket");
v3 = *v1;
for ( i = v1 + 1; v3; v3 = (const void *)*(i - 1) )
{
if ( (int)write(fd, v3, 0xFFuLL) < 0 )
func1("ERROR writing to socket");
usleep(0x2710u);
++i;
}
sprintf(v11, "%d", *(unsigned int *)(queue + 16));
if ( (int)write(fd, v11, 0xFFuLL) < 0 )
func1("ERROR writing to socket");
v5 = *v2;
v6 = v2 + 1;
if ( *v2 )
{
do
{
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
func1("ERROR writing to socket");
usleep(0x2710u);
v5 = (const void *)*v6++;
}
while ( v5 );
v7 = (void *)*v1;
if ( !*v1 )
goto LABEL_12;
}
else
{
v7 = (void *)*v1;
if ( !*v1 )
goto LABEL_15;
}
v8 = v1 + 1;
do
{
free(v7);
v7 = (void *)*v8++;
}
while ( v7 );
LABEL_12:
v9 = (void *)*v2;
if ( *v2 )
{
v10 = v2 + 1;
do
{
free(v9);
v9 = (void *)*v10++;
}
while ( v9 );
}
LABEL_15:
free(v1);
free(v2);
}

original_code:
void sendListings(int clisock, int id){
 int n, i;
 char buffer[255 +1];
 char** all_songs = get_list(list);
 char** q_songs = get_list(queue);
 sprintf(buffer, "%d", list->count);
 n = write(clisock, buffer, 255);
 if (n < 0)
  error("ERROR writing to socket");
 for (i = 0; all_songs[i] != ((void *)0); i++){
  n = write(clisock,all_songs[i],255);
  if (n < 0)
   error("ERROR writing to socket");
   usleep(10000);
 }
 sprintf(buffer, "%d", queue->count);
 n = write(clisock, buffer, 255);
 if (n < 0)
  error("ERROR writing to socket");
 for (i = 0; q_songs[i] != ((void *)0); i++){
  n = write(clisock,q_songs[i],255);
  if (n < 0)
   error("ERROR writing to socket");
   usleep(10000);
 }
 for (i = 0; all_songs[i] != ((void *)0); i++)
  free (all_songs[i]);
 for (i = 0; q_songs[i] != ((void *)0); i++)
  free (q_songs[i]);
 free (all_songs);
 free (q_songs);
 return;
}

canonical_original_code:
void sendListings(int clisock, int id){
 int n; int i;
 char buffer[-1];
 char **all_songs = get_list(list);
 char **q_songs = get_list(queue);
 sprintf(buffer, "%d", list->count);
 n = write(clisock, buffer, 255);
 if (n < 0)
  error("ERROR writing to socket");
 for (i = 0; all_songs[i] != ((void *)0); i++){
  n = write(clisock,all_songs[i],255);
  if (n < 0)
   error("ERROR writing to socket");
   usleep(10000);
 }
 sprintf(buffer, "%d", queue->count);
 n = write(clisock, buffer, 255);
 if (n < 0)
  error("ERROR writing to socket");
 for (i = 0; q_songs[i] != ((void *)0); i++){
  n = write(clisock,q_songs[i],255);
  if (n < 0)
   error("ERROR writing to socket");
   usleep(10000);
 }
 for (i = 0; all_songs[i] != ((void *)0); i++)
  free (all_songs[i]);
 for (i = 0; q_songs[i] != ((void *)0); i++)
  free (q_songs[i]);
 free (all_songs);
 free (q_songs);
 return;
}

variable_types: clisock int

variable_types: id int

variable_types: n int

variable_types: i int

variable_types:
buffer
char [-1]


clientAction

decompiled_code:
int __fastcall clientAction(void *a1)
{
int v1; // eax
int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

memset(v6, 0, 0x100uLL);
while ( 1 )
{
v1 = read(*(_DWORD *)a1, v6, 0xFFuLL);
if ( v1 < 0 )
error("ERROR reading from socket");
if ( !v1 )
break;
v2 = atoi(v6);
if ( v2 == 101 )
{
sendListings(*(_DWORD *)a1);
}
else if ( v2 == 102 )
{
if ( (int)read(*(_DWORD *)a1, v6, 0xFFuLL) < 0 )
error("ERROR reading from socket");
v3 = atoi(v6);
v4 = queue_song(queue, list, v3);
sprintf(v6, "%d", v4);
if ( (int)write(*(_DWORD *)a1, v6, 0xFFuLL) < 0 )
error("ERROR writing to socket");
}
}
return close(*(_DWORD *)a1);
}

canonical_decompiled_code:
int __fastcall func5(void *a1)
{
int v1; // eax
int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

memset(v6, 0, 0x100uLL);
while ( 1 )
{
v1 = read(*(_DWORD *)a1, v6, 0xFFuLL);
if ( v1 < 0 )
func1("ERROR reading from socket");
if ( !v1 )
break;
v2 = atoi(v6);
if ( v2 == 101 )
{
func4(*(_DWORD *)a1);
}
else if ( v2 == 102 )
{
if ( (int)read(*(_DWORD *)a1, v6, 0xFFuLL) < 0 )
func1("ERROR reading from socket");
v3 = atoi(v6);
v4 = queue_song(queue, list, v3);
sprintf(v6, "%d", v4);
if ( (int)write(*(_DWORD *)a1, v6, 0xFFuLL) < 0 )
func1("ERROR writing to socket");
}
}
return close(*(_DWORD *)a1);
}

original_code:
void *clientAction(void *arguments){
 client_args_t *args = ((void *)0);
 int n, choice, add_song, result;
 char *message;
 char buffer[255 +1];
 (memset((buffer), '\0', (255 +1)), (void) 0);
 args = (client_args_t *) arguments;
 do{
 n = read(args->sock, buffer, 255);
 if (n < 0)
  error("ERROR reading from socket");
  else if (n == 0)
  break;
  choice = atoi(buffer);
  if (choice == 101){
   sendListings(args->sock, args->id);
  }
  else if (choice == 102){
   n = read(args->sock, buffer, 255);
   if (n < 0)
    error("ERROR reading from socket");
   add_song = atoi(buffer);
   result = queue_song(queue, list, add_song);
   sprintf(buffer, "%d", result);
   n = write(args->sock, buffer, 255);
   if (n < 0)
    error("ERROR writing to socket");
  }
 } while(1);
 close(args->sock);
}

canonical_original_code:
void *clientAction(void *arguments){
 struct client_args_t *args = ((void *)0);
 int n; int choice; int add_song; int result;
 char *message;
 char buffer[-1];
 (memset((buffer), '\0', (255 +1)), (void) 0);
 args = (struct client_args_t *) arguments;
 do{
 n = read(args->sock, buffer, 255);
 if (n < 0)
  error("ERROR reading from socket");
  else if (n == 0)
  break;
  choice = atoi(buffer);
  if (choice == 101){
   sendListings(args->sock, args->id);
  }
  else if (choice == 102){
   n = read(args->sock, buffer, 255);
   if (n < 0)
    error("ERROR reading from socket");
   add_song = atoi(buffer);
   result = queue_song(queue, list, add_song);
   sprintf(buffer, "%d", result);
   n = write(args->sock, buffer, 255);
   if (n < 0)
    error("ERROR writing to socket");
  }
 } while(1);
 close(args->sock);
}

variable_types:
arguments
void *

variable_types: struct client_args_t { int sock; int id; } *

variable_types: choice int

variable_types: result int

variable_types:
message
char *


error

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

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

original_code:
void error(const char *msg)
{
 perror(msg);
 exit(1);
}

canonical_original_code:
void error(char *msg)
{
 perror(msg);
 exit(1);
}

variable_types:
msg
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // ebx
_QWORD *v5; // rax
_QWORD *v6; // rbx
unsigned int i; // ebp
int *v8; // rbx
int v9; // eax
pthread_t v10; // [rsp+0h] [rbp-58h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-50h] BYREF
struct sockaddr v12; // [rsp+10h] [rbp-48h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-38h] BYREF
socklen_t addr_len[7]; // [rsp+3Ch] [rbp-1Ch] BYREF

if ( argc > 1 )
{
v3 = socket(2, 1, 0);
if ( v3 >= 0 )
{
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v4 = atoi(argv[1]);
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(v4);
if ( bind(v3, &addr, 0x10u) >= 0 )
{
if ( listen(v3, 5) != -1 )
{
printf("\nServer is starting on port: %d\n", v4);
list = new_queue();
queue = new_queue();
read_directory();
v5 = (_QWORD *)import_list();
if ( *v5 )
{
v6 = v5 + 1;
do
{
add_song(list);
++v6;
}
while ( *(v6 - 1) );
}
auto_queue(queue, list);
for ( i = 0; ; ++i )
{
printf("Preparing thread %d\n", i);
v8 = (int *)safe_malloc(8LL);
addr_len[0] = 16;
v9 = accept(v3, &v12, addr_len);
*v8 = v9;
if ( v9 < 0 )
break;
v8[1] = i;
pthread_create(&v10, 0LL, clientAction, v8);
printf("Thread %d live\n", i);
if ( !i )
pthread_create(&newthread, 0LL, (void *(*)(void *))beginStreaming, 0LL);
}
error("ERROR on accept");
}
error("ERROR on listen");
}
error("ERROR on binding");
}
error("ERROR opening socket");
}
fwrite("ERROR, no port provided\n", 1uLL, 0x18uLL, stderr);
usage();
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // ebx
_QWORD *v5; // rax
_QWORD *v6; // rbx
unsigned int i; // ebp
int *v8; // rbx
int v9; // eax
pthread_t v10; // [rsp+0h] [rbp-58h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-50h] BYREF
struct sockaddr v12; // [rsp+10h] [rbp-48h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-38h] BYREF
socklen_t addr_len[7]; // [rsp+3Ch] [rbp-1Ch] BYREF

if ( argc > 1 )
{
v3 = socket(2, 1, 0);
if ( v3 >= 0 )
{
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v4 = atoi(argv[1]);
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(v4);
if ( bind(v3, &addr, 0x10u) >= 0 )
{
if ( listen(v3, 5) != -1 )
{
printf("\nServer is starting on port: %d\n", v4);
list = new_queue();
queue = new_queue();
read_directory();
v5 = (_QWORD *)import_list();
if ( *v5 )
{
v6 = v5 + 1;
do
{
add_song(list);
++v6;
}
while ( *(v6 - 1) );
}
auto_queue(queue, list);
for ( i = 0; ; ++i )
{
printf("Preparing thread %d\n", i);
v8 = (int *)safe_malloc(8LL);
addr_len[0] = 16;
v9 = accept(v3, &v12, addr_len);
*v8 = v9;
if ( v9 < 0 )
break;
v8[1] = i;
pthread_create(&v10, 0LL, clientAction, v8);
printf("Thread %d live\n", i);
if ( !i )
pthread_create(&newthread, 0LL, (void *(*)(void *))beginStreaming, 0LL);
}
func1("ERROR on accept");
}
func1("ERROR on listen");
}
func1("ERROR on binding");
}
func1("ERROR opening socket");
}
fwrite("ERROR, no port provided\n", 1uLL, 0x18uLL, stderr);
func3();
exit(1);
}

original_code:
int main(int argc, char *argv[])
{
 char **all_songs;
 int sockfd, newsockfd, portNum;
 socklen_t clilen;
 struct sockaddr_in serv_addr, cli_addr;
 int n, i;
 if (argc < 2) {
  fprintf(stderr,"ERROR, no port provided\n");
  usage();
  exit(1);
 }
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 (memset(((char *) &serv_addr), '\0', (sizeof(serv_addr))), (void) 0);
 portNum = atoi(argv[1]);
 serv_addr.sin_family = 2;
 serv_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 serv_addr.sin_port = htons(portNum);
 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  error("ERROR on binding");
 if(listen(sockfd,5) == -1)
  error("ERROR on listen");
 printf("\nServer is starting on port: %d\n",portNum);
 list = new_queue ();
 queue = new_queue ();
 read_directory ();
 all_songs = import_list ();
 for (i = 0; all_songs[i] != ((void *)0); i++)
    add_song (list, all_songs[i]);
  auto_queue (queue, list);
  int thread_id=0;
  pthread_t streamThread;
 while(1){
  client_args_t *args = ((void *)0);
  printf("Preparing thread %d\n", thread_id);
  args = safe_malloc (sizeof (client_args_t));
  clilen = sizeof(cli_addr);
  args->sock = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
  if (args->sock < 0)
   error("ERROR on accept");
  args->id = thread_id;
  pthread_t thread;
  pthread_create(&thread,((void *)0),&clientAction,(void *)args);
  printf("Thread %d live\n", thread_id);
  if (thread_id == 0)
  {
   pthread_create(&streamThread, ((void *)0), beginStreaming, ((void *)0));
  }
  thread_id++;
 }
 close(sockfd);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char **all_songs;
 int sockfd; int newsockfd; int portNum;
 unsigned int clilen;
 struct sockaddr_in serv_addr; struct sockaddr_in cli_addr;
 int n; int i;
 if (argc < 2) {
  fprintf(stderr,"ERROR, no port provided\n");
  usage();
  exit(1);
 }
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 (memset(((char *) &serv_addr), '\0', (sizeof(serv_addr))), (void) 0);
 portNum = atoi(argv[1]);
 serv_addr.sin_family = 2;
 serv_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
 serv_addr.sin_port = htons(portNum);
 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  error("ERROR on binding");
 if(listen(sockfd,5) == -1)
  error("ERROR on listen");
 printf("\nServer is starting on port: %d\n",portNum);
 list = new_queue ();
 queue = new_queue ();
 read_directory ();
 all_songs = import_list ();
 for (i = 0; all_songs[i] != ((void *)0); i++)
    add_song (list, all_songs[i]);
  auto_queue (queue, list);
  int thread_id = 0;
  unsigned long streamThread;
 while(1){
  struct client_args_t *args = ((void *)0);
  printf("Preparing thread %d\n", thread_id);
  args = safe_malloc (sizeof (client_args_t));
  clilen = sizeof(cli_addr);
  args->sock = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
  if (args->sock < 0)
   error("ERROR on accept");
  args->id = thread_id;
  unsigned long thread;
  pthread_create(&thread,((void *)0),&clientAction,(void *)args);
  printf("Thread %d live\n", thread_id);
  if (thread_id == 0)
  {
   pthread_create(&streamThread, ((void *)0), beginStreaming, ((void *)0));
  }
  thread_id++;
 }
 close(sockfd);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: sockfd int

variable_types: newsockfd int

variable_types: portNum int

variable_types:
clilen
unsigned int

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

variable_types:
streamThread
unsigned long

variable_types:
thread
unsigned long

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

user_defined_types: struct client_args_t { int sock; int id; }

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


decompiled_code:
void *__fastcall safe_malloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fwrite("*Error during safe_malloc! Aborting..\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
fwrite("*Error during safe_malloc! Aborting..\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
return result;
}

original_code:
void *
safe_malloc (size_t size)
{
  void *mem = ((void *)0);
  if ((mem = (void *)malloc(size)) == ((void *)0))
  {
    fprintf (stderr, "*Error during safe_malloc! Aborting..\n");
    exit (1);
  }
  return (mem);
}

canonical_original_code:
void *safe_malloc(unsigned long size)
{
  void *mem = ((void *)0);
  if ((mem = (void *)malloc(size)) == ((void *)0))
  {
    fprintf (stderr, "*Error during safe_malloc! Aborting..\n");
    exit (1);
  }
  return (mem);
}

variable_types:
size
unsigned long

variable_types:
mem
void *


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

result = realloc(a1, a2);
if ( !result )
{
fwrite("*Error during safe_realloc! Aborting..\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
return result;
}

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

result = realloc(a1, a2);
if ( !result )
{
fwrite("*Error during safe_realloc! Aborting..\n", 1uLL, 0x27uLL, stderr);
exit(1);
}
return result;
}

original_code:
void *
safe_realloc (void * old_mem, size_t new_size)
{
  void *new_mem = ((void *)0);
  if ((new_mem = (void *)realloc (old_mem, new_size)) == ((void *)0))
  {
    fprintf (stderr, "*Error during safe_realloc! Aborting..\n");
    exit (1);
  }
  return (new_mem);
}

canonical_original_code:
void *safe_realloc(void *old_mem, unsigned long new_size)
{
  void *new_mem = ((void *)0);
  if ((new_mem = (void *)realloc (old_mem, new_size)) == ((void *)0))
  {
    fprintf (stderr, "*Error during safe_realloc! Aborting..\n");
    exit (1);
  }
  return (new_mem);
}


decompiled_code:
char *__fastcall safe_strdup(char *src)
{
size_t v1; // rax
char *v2; // rax

v1 = strlen(src);
v2 = (char *)safe_malloc(v1 + 1);
return strcpy(v2, src);
}

canonical_decompiled_code:
char *__fastcall func2(char *src)
{
size_t v1; // rax
char *v2; // rax

v1 = strlen(src);
v2 = (char *)func0(v1 + 1);
return strcpy(v2, src);
}

original_code:
char *
safe_strdup (const char *str)
{
  char *new_str = ((void *)0);
  new_str = safe_malloc ((strlen(str) + 1) * sizeof (char));
  strcpy (new_str, str);
  return (new_str);
}

canonical_original_code:
char *safe_strdup(char *str)
{
  char *new_str = ((void *)0);
  new_str = safe_malloc ((strlen(str) + 1) * sizeof (char));
  strcpy (new_str, str);
  return (new_str);
}

variable_types:
str
char *


decompiled_code:
__int64 __fastcall validate_str(const char *a1)
{
size_t v1; // rax
unsigned int v2; // edx
const char *v3; // rax

v1 = strlen(a1);
v2 = 0;
if ( v1 > 1 )
{
v3 = &a1[v1 - 1];
if ( *v3 == 10 )
{
*v3 = 0;
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
size_t v1; // rax
unsigned int v2; // edx
const char *v3; // rax

v1 = strlen(a1);
v2 = 0;
if ( v1 > 1 )
{
v3 = &a1[v1 - 1];
if ( *v3 == 10 )
{
*v3 = 0;
v2 = 1;
}
}
return v2;
}

original_code:
int
validate_str (char *str)
{
  if (strlen (str) <= 1)
    return (0);
  if (*(str + (strlen(str) - 1)) == '\n')
    *(str + (strlen(str) - 1)) = '\0';
  else
    return (0);
  return 1;
}

canonical_original_code:
int validate_str(char *str)
{
  if (strlen (str) <= 1)
    return (0);
  if (*(str + (strlen(str) - 1)) == '\n')
    *(str + (strlen(str) - 1)) = '\0';
  else
    return (0);
  return 1;
}


decompiled_code:
int read_directory()
{
system("ls -1 music/*.wav >music/list.txt 2>>music/log.txt");
return system("wc -l music/list.txt >music/count.txt 2>>music/log.txt");
}

canonical_decompiled_code:
int func0()
{
system("ls -1 music/*.wav >music/list.txt 2>>music/log.txt");
return system("wc -l music/list.txt >music/count.txt 2>>music/log.txt");
}

original_code:
void
read_directory ()
{
  system ("ls -1 music/*.wav >music/list.txt 2>>music/log.txt");
  system ("wc -l music/list.txt >music/count.txt 2>>music/log.txt");
  return;
}

canonical_original_code:
void read_directory()
{
  system ("ls -1 music/*.wav >music/list.txt 2>>music/log.txt");
  system ("wc -l music/list.txt >music/count.txt 2>>music/log.txt");
  return;
}


createWav

decompiled_code:
__int64 __fastcall createWav(__int64 a1)
{
__int64 v1; // rbx

v1 = safe_malloc(32LL);
*(_QWORD *)v1 = safe_strdup(a1);
*(_DWORD *)(v1 + 8) = 0;
*(_QWORD *)(v1 + 16) = 0LL;
*(_QWORD *)(v1 + 24) = 0LL;
return v1;
}

original_code:
WavFile* createWav(char *filename)
{
  WavFile *wav = ((void *)0);
  wav = safe_malloc(sizeof(WavFile));
  wav->filename = safe_strdup(filename);
  wav->size = 0;
  wav->format = ((void *)0);
  wav->data = ((void *)0);
  return wav;
}

canonical_original_code:
struct WavFile *createWav(char *filename)
{
  struct WavFile *wav = ((void *)0);
  wav = safe_malloc(sizeof(WavFile));
  wav->filename = safe_strdup(filename);
  wav->size = 0;
  wav->format = ((void *)0);
  wav->data = ((void *)0);
  return wav;
}

variable_types:
filename
char *

variable_types: struct WavFile { char *filename; unsigned int size; struct FormatChunk { unsigned int size; unsigned int compressionCode; unsigned int numChannels; unsigned int sampleRate; unsigned int averageBPS; unsigned int blockAlign; unsigned int significantBPS; } *format; struct DataChunk { unsigned int size; unsigned char *data; } *data; } *


deleteWav

decompiled_code:
void __fastcall deleteWav(void *ptr)
{
__int64 v1; // rax

if ( ptr )
{
free(*(void **)ptr);
free(*((void **)ptr + 2));
v1 = *((_QWORD *)ptr + 3);
if ( v1 )
{
free(*(void **)(v1 + 8));
free(*((void **)ptr + 3));
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v1; // rax

if ( ptr )
{
free(*(void **)ptr);
free(*((void **)ptr + 2));
v1 = *((_QWORD *)ptr + 3);
if ( v1 )
{
free(*(void **)(v1 + 8));
free(*((void **)ptr + 3));
}
free(ptr);
}
}

original_code:
void deleteWav(WavFile *wav)
{
  if (wav == ((void *)0))
    return;
  free(wav->filename);
  free(wav->format);
  if (wav->data != ((void *)0)) {
    free(wav->data->data);
    free(wav->data);
  }
  free(wav);
}

canonical_original_code:
void deleteWav(struct WavFile *wav)
{
  if (wav == ((void *)0))
    return;
  free(wav->filename);
  free(wav->format);
  if (wav->data != ((void *)0)) {
    free(wav->data->data);
    free(wav->data);
  }
  free(wav);
}

user_defined_types: struct FormatChunk { unsigned int size; unsigned int compressionCode; unsigned int numChannels; unsigned int sampleRate; unsigned int averageBPS; unsigned int blockAlign; unsigned int significantBPS; }

user_defined_types: struct DataChunk { unsigned int size; unsigned char *data; }


chunkIdCompare

decompiled_code:
__int64 __fastcall chunkIdCompare(__int64 a1, __int64 a2)
{
__int64 v2; // rax

v2 = 0LL;
while ( *(_BYTE *)(a1 + v2) == *(_BYTE *)(a2 + v2) )
{
if ( ++v2 == 4 )
return 1LL;
}
return 0LL;
}

original_code:
_Bool chunkIdCompare(char *id1, char *id2)
{
  int i;
  for (i = 0; i < 4; ++i)
  {
    if (id1[i] != id2[i])
      return 0;
  }
  return 1;
}

variable_types: i int


getFrameSize

decompiled_code:
__int64 __fastcall getFrameSize(__int64 a1)
{
return (unsigned int)(*(_DWORD *)(a1 + 8) * (*(_DWORD *)(a1 + 24) >> 3));
}

original_code:
uint getFrameSize(FormatChunk *format)
{
  int bytesPerSample = format->significantBPS / 8;
  uint frameSize = bytesPerSample * format->numChannels;
  return frameSize;
}

canonical_original_code:
unsigned int getFrameSize(struct FormatChunk *format)
{
  int bytesPerSample = format->significantBPS / 8;
  unsigned int frameSize = bytesPerSample * format->numChannels;
  return frameSize;
}

variable_types: struct FormatChunk { unsigned int size; unsigned int compressionCode; unsigned int numChannels; unsigned int sampleRate; unsigned int averageBPS; unsigned int blockAlign; unsigned int significantBPS; } *

variable_types: bytesPerSample int

variable_types:
frameSize
unsigned int

return_type: unsigned int


receive

decompiled_code:
void __fastcall __noreturn receive(void *a1)
{
int v1; // ebp
__int64 v2; // rbx
pthread_mutex_t *v3; // r12
unsigned int i; // eax
int v5; // eax
__int64 v6; // rax
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF

*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_family = 2;
req.ai_flags = 1;
if ( getaddrinfo(0LL, "8909", &req, &pai) )
error("ERROR getting server address info");
v1 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
if ( v1 != -1 )
{
if ( bind(v1, pai->ai_addr, pai->ai_addrlen) != -1 )
{
freeaddrinfo(pai);
v2 = buffer1;
v3 = &buffer1Mutex;
while ( 1 )
{
pthread_mutex_lock(v3);
for ( i = *(_DWORD *)(v2 + 12); i + 1470 < *(_DWORD *)(v2 + 8); *(_DWORD *)(v2 + 12) = i )
{
v5 = recvfrom(v1, (void *)(*(_QWORD *)v2 + i), 0x5BEuLL, 0, 0LL, 0LL);
if ( v5 == -1 )
error("ERROR receiving streaming data");
if ( !v5 )
error("ERROR connection to server closed");
i = *(_DWORD *)(v2 + 12) + v5;
}
*(_BYTE *)(v2 + 16) = 1;
pthread_cond_signal(&bufferCV);
pthread_mutex_unlock(v3);
v6 = buffer1;
v3 = &buffer1Mutex;
if ( buffer1 == v2 )
{
v6 = buffer2;
v3 = (pthread_mutex_t *)&buffer2Mutex;
}
v2 = v6;
}
}
error("ERROR binding socket");
}
error("ERROR creating socket");
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
int v1; // ebp
__int64 v2; // rbx
pthread_mutex_t *v3; // r12
unsigned int i; // eax
int v5; // eax
__int64 v6; // rax
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF

*(_QWORD *)&req.ai_socktype = 2LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_family = 2;
req.ai_flags = 1;
if ( getaddrinfo(0LL, "8909", &req, &pai) )
func1("ERROR getting server address info");
v1 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
if ( v1 != -1 )
{
if ( bind(v1, pai->ai_addr, pai->ai_addrlen) != -1 )
{
freeaddrinfo(pai);
v2 = buffer1;
v3 = &buffer1Mutex;
while ( 1 )
{
pthread_mutex_lock(v3);
for ( i = *(_DWORD *)(v2 + 12); i + 1470 < *(_DWORD *)(v2 + 8); *(_DWORD *)(v2 + 12) = i )
{
v5 = recvfrom(v1, (void *)(*(_QWORD *)v2 + i), 0x5BEuLL, 0, 0LL, 0LL);
if ( v5 == -1 )
func1("ERROR receiving streaming data");
if ( !v5 )
func1("ERROR connection to server closed");
i = *(_DWORD *)(v2 + 12) + v5;
}
*(_BYTE *)(v2 + 16) = 1;
pthread_cond_signal(&bufferCV);
pthread_mutex_unlock(v3);
v6 = buffer1;
v3 = &buffer1Mutex;
if ( buffer1 == v2 )
{
v6 = buffer2;
v3 = (pthread_mutex_t *)&buffer2Mutex;
}
v2 = v6;
}
}
func1("ERROR binding socket");
}
func1("ERROR creating socket");
}

original_code:
void *receive(void *args)
{
  int sockfd, err, numBytes;
  struct addrinfo hints, *servInfo;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = 0x0001;
  err = getaddrinfo(((void *)0), "8909", &hints, &servInfo);
  if (err != 0)
    error("ERROR getting server address info");
  sockfd = socket(servInfo->ai_family, servInfo->ai_socktype,
    servInfo->ai_protocol);
  if (sockfd == -1)
    error("ERROR creating socket");
  err = bind(sockfd, servInfo->ai_addr, servInfo->ai_addrlen);
  if (err == -1)
    error("ERROR binding socket");
  freeaddrinfo(servInfo);
  uint packetSize = 1470;
  AudioBuffer *currentBuffer = buffer1;
  pthread_mutex_t *currentMutex = &buffer1Mutex;
  uint byteCount = 0;
  while (1){
    uint wholeFrames = 0, i = 0;
    pthread_mutex_lock(currentMutex);
    while(currentBuffer->count + packetSize < currentBuffer->capacity){
      int numBytes = 0;
      numBytes = recvfrom(sockfd, currentBuffer->data + currentBuffer->count, packetSize, 0, ((void *)0), ((void *)0));
      if (numBytes == -1)
        error("ERROR receiving streaming data");
      else if (numBytes == 0)
        error("ERROR connection to server closed");
      currentBuffer->count += numBytes;
    }
    currentBuffer->full = 1;
    pthread_cond_signal(&bufferCV);
    pthread_mutex_unlock(currentMutex);
    if (currentBuffer == buffer1){
      currentBuffer = buffer2;
      currentMutex = &buffer2Mutex;
    }else{
      currentBuffer = buffer1;
      currentMutex = &buffer1Mutex;
    }
  }
  close(sockfd);
}

canonical_original_code:
void *receive(void *args)
{
  int sockfd; int err; int numBytes;
  struct addrinfo hints; struct addrinfo *servInfo;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = 0x0001;
  err = getaddrinfo(((void *)0), "8909", &hints, &servInfo);
  if (err != 0)
    error("ERROR getting server address info");
  sockfd = socket(servInfo->ai_family, servInfo->ai_socktype,
    servInfo->ai_protocol);
  if (sockfd == -1)
    error("ERROR creating socket");
  err = bind(sockfd, servInfo->ai_addr, servInfo->ai_addrlen);
  if (err == -1)
    error("ERROR binding socket");
  freeaddrinfo(servInfo);
  unsigned int packetSize = 1470;
  struct AudioBuffer *currentBuffer = buffer1;
  union pthread_mutex_t *currentMutex = &buffer1Mutex;
  unsigned int byteCount = 0;
  while (1){
    unsigned int wholeFrames = 0; unsigned int i = 0;
    pthread_mutex_lock(currentMutex);
    while(currentBuffer->count + packetSize < currentBuffer->capacity){
      int numBytes = 0;
      numBytes = recvfrom(sockfd, currentBuffer->data + currentBuffer->count, packetSize, 0, ((void *)0), ((void *)0));
      if (numBytes == -1)
        error("ERROR receiving streaming data");
      else if (numBytes == 0)
        error("ERROR connection to server closed");
      currentBuffer->count += numBytes;
    }
    currentBuffer->full = 1;
    pthread_cond_signal(&bufferCV);
    pthread_mutex_unlock(currentMutex);
    if (currentBuffer == buffer1){
      currentBuffer = buffer2;
      currentMutex = &buffer2Mutex;
    }else{
      currentBuffer = buffer1;
      currentMutex = &buffer1Mutex;
    }
  }
  close(sockfd);
}

variable_types:
args
void *

variable_types: err int

variable_types: numBytes int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types:
packetSize
unsigned int

variable_types: struct AudioBuffer { unsigned char *data; unsigned int capacity; unsigned int count; _Bool full; } *

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *

variable_types:
byteCount
unsigned int

variable_types:
wholeFrames
unsigned int

variable_types:
i
unsigned int


createAudioBuffer

decompiled_code:
__int64 __fastcall createAudioBuffer(unsigned int a1)
{
__int64 v1; // rbx

v1 = safe_malloc(24LL);
*(_QWORD *)v1 = safe_malloc(a1);
*(_QWORD *)(v1 + 8) = a1;
*(_BYTE *)(v1 + 16) = 0;
return v1;
}

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

v1 = safe_malloc(24LL);
*(_QWORD *)v1 = safe_malloc(a1);
*(_QWORD *)(v1 + 8) = a1;
*(_BYTE *)(v1 + 16) = 0;
return v1;
}

original_code:
AudioBuffer *createAudioBuffer(uint size)
{
  AudioBuffer *buffer = ((void *)0);
  buffer = safe_malloc(sizeof(AudioBuffer));
  buffer->data = safe_malloc(size);
  buffer->capacity = size;
  buffer->count = 0;
  buffer->full = 0;
  return buffer;
}

canonical_original_code:
struct AudioBuffer *createAudioBuffer(unsigned int size)
{
  struct AudioBuffer *buffer = ((void *)0);
  buffer = safe_malloc(sizeof(AudioBuffer));
  buffer->data = safe_malloc(size);
  buffer->capacity = size;
  buffer->count = 0;
  buffer->full = 0;
  return buffer;
}

variable_types:
size
unsigned int

return_type: struct AudioBuffer { unsigned char *data; unsigned int capacity; unsigned int count; _Bool full; } *


error

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

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

original_code:
void error(const char *msg)
{
 perror(msg);
 exit(0);
}

canonical_original_code:
void error(char *msg)
{
 perror(msg);
 exit(0);
}

variable_types:
msg
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
int v4; // ebp
struct hostent *v5; // rax
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr dest[2]; // [rsp+10h] [rbp-28h] BYREF

if ( argc <= 2 )
{
fprintf(stderr, "usage %s hostname port\n", *argv);
exit(0);
}
v3 = atoi(argv[2]);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
error("ERROR opening socket");
v5 = gethostbyname(argv[1]);
if ( !v5 )
{
fwrite("ERROR, no such host\n", 1uLL, 0x14uLL, stderr);
exit(0);
}
*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
memmove(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)dest[0].sa_data = _bswap_16(v3);
if ( connect(v4, dest, 0x10u) < 0 )
error("ERROR connecting");
pthread_create(&newthread, 0LL, beginPlayback, 0LL);
begin((unsigned int)v4);
close(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
int v4; // ebp
struct hostent *v5; // rax
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr dest[2]; // [rsp+10h] [rbp-28h] BYREF

if ( argc <= 2 )
{
fprintf(stderr, "usage %s hostname port\n", *argv);
exit(0);
}
v3 = atoi(argv[2]);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
func1("ERROR opening socket");
v5 = gethostbyname(argv[1]);
if ( !v5 )
{
fwrite("ERROR, no such host\n", 1uLL, 0x14uLL, stderr);
exit(0);
}
*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
memmove(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)dest[0].sa_data = func0(v3);
if ( connect(v4, dest, 0x10u) < 0 )
func1("ERROR connecting");
pthread_create(&newthread, 0LL, beginPlayback, 0LL);
begin((unsigned int)v4);
close(v4);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 int sockfd, portno, n;
 struct sockaddr_in serv_addr;
 struct hostent *server;
 if (argc < 3) {
  fprintf(stderr,"usage %s hostname port\n", argv[0]);
  exit(0);
 }
 portno = atoi(argv[2]);
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 server = gethostbyname(argv[1]);
 if (server == ((void *)0)) {
  fprintf(stderr,"ERROR, no such host\n");
  exit(0);
 }
 (memset(((char *) &serv_addr), '\0', (sizeof(serv_addr))), (void) 0);
 serv_addr.sin_family = 2;
 bcopy((char *)server->h_addr_list[0],
   (char *)&serv_addr.sin_addr.s_addr,
   server->h_length);
 serv_addr.sin_port = htons(portno);
 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  error("ERROR connecting");
 pthread_t playbackThread;
 pthread_create(&playbackThread, ((void *)0), beginPlayback, ((void *)0));
 begin(sockfd);
 close(sockfd);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int sockfd; int portno; int n;
 struct sockaddr_in serv_addr;
 struct hostent *server;
 if (argc < 3) {
  fprintf(stderr,"usage %s hostname port\n", argv[0]);
  exit(0);
 }
 portno = atoi(argv[2]);
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 server = gethostbyname(argv[1]);
 if (server == ((void *)0)) {
  fprintf(stderr,"ERROR, no such host\n");
  exit(0);
 }
 (memset(((char *) &serv_addr), '\0', (sizeof(serv_addr))), (void) 0);
 serv_addr.sin_family = 2;
 bcopy((char *)server->h_addr_list[0],
   (char *)&serv_addr.sin_addr.s_addr,
   server->h_length);
 serv_addr.sin_port = htons(portno);
 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  error("ERROR connecting");
 unsigned long playbackThread;
 pthread_create(&playbackThread, ((void *)0), beginPlayback, ((void *)0));
 begin(sockfd);
 close(sockfd);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: sockfd int

variable_types: portno int

variable_types: n int

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

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
playbackThread
unsigned long


repo: trezor/trezor-qrenc

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
size_t v4; // rax
__int64 v5; // rbp
int v6; // ebx
int v7; // eax
int v8; // er12
int v9; // er14
int v10; // ebx
int v11; // edx
const char *v12; // rdi
int v13; // ebx
int v14; // eax
char v16; // [rsp+1h] [rbp-1789h]
char v17[2048]; // [rsp+2h] [rbp-1788h] BYREF
char v18[3976]; // [rsp+802h] [rbp-F88h] BYREF

printf("ECC Level [LMQH]: ");
if ( !fgets(v17, 2048, stdin) )
return 1;
switch ( v17[0] )
{
case 'H':
case 'h':
v3 = 3;
break;
case 'L':
case 'l':
v3 = 0;
break;
case 'Q':
case 'q':
v3 = 2;
break;
default:
v3 = 1;
break;
}
printf("Enter string: ");
if ( fgets(v17, 2048, stdin) )
{
v4 = strlen(v17);
if ( *(&v16 + v4) == 10 )
*(&v16 + v4) = 0;
v5 = (unsigned int)qr_encode(v3, 0LL, v17, 0LL, v18);
printf("side: %d\n", v5);
if ( (int)v5 + 1 < 0 )
{
putchar(10);
if ( (int)v5 <= 0 )
{
LABEL_22:
putchar(10);
return 0;
}
}
else
{
v6 = 0;
do
{
printf(&byte_231);
v7 = v6++;
}
while ( (int)v5 >= v7 );
putchar(10);
if ( (int)v5 <= 0 )
{
LABEL_20:
v13 = 0;
do
{
printf(&byte_231);
v14 = v13++;
}
while ( (int)v5 >= v14 );
goto LABEL_22;
}
}
v8 = 0;
v9 = 0;
do
{
printf(&byte_231);
v10 = 0;
do
{
v11 = v10 + v8 + 7;
if ( v10 + v8 >= 0 )
v11 = v10 + v8;
v12 = (const char *)&unk_22E;
if ( (((int)(unsigned __int8)v18[v11 >> 3] >> (7 - (v10 + v8) % 8)) & 1) == 0 )
v12 = &byte_231;
printf(v12);
++v10;
}
while ( (int)v5 > v10 );
printf(&byte_231);
putchar(10);
++v9;
v8 += v5;
}
while ( (int)v5 > v9 );
if ( (int)v5 + 1 < 0 )
goto LABEL_22;
goto LABEL_20;
}
return 1;
}

original_code:
int main(void)
{
 int side, i, j, a, ecclevel;
 uint8_t bitdata[(((40 * 4 + 17) * (40 * 4 + 17) + 7) / 8)];
 char str[2048];
 printf("ECC Level [LMQH]: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 switch (str[0]) {
 case 'l':
 case 'L':
  ecclevel = 0;
  break;
 case 'm':
 case 'M':
 default:
  ecclevel = 1;
  break;
 case 'q':
 case 'Q':
  ecclevel = 2;
  break;
 case 'h':
 case 'H':
  ecclevel = 3;
  break;
 }
 printf("Enter string: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 if (str[strlen(str) - 1] == '\n') {
  str[strlen(str) - 1] = 0;
 }
 side = qr_encode(ecclevel, 0, str, 0, bitdata);
 printf("side: %d\n", side);
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 for (i = 0; i < side; i++) {
  printf("██");
  for (j = 0; j < side; j++) {
   a = i * side + j;
   printf((bitdata[a / 8] & (1 << (7 - a % 8))) ? "  " : "██");
  }
  printf("██");
  printf("\n");
 }
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 return 0;
}

canonical_original_code:
int main()
{
 int side; int i; int j; int a; int ecclevel;
 unsigned char bitdata[-1];
 char str[2048];
 printf("ECC Level [LMQH]: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 switch (str[0]) {
 case 'l':
 case 'L':
  ecclevel = 0;
  break;
 case 'm':
 case 'M':
 default:
  ecclevel = 1;
  break;
 case 'q':
 case 'Q':
  ecclevel = 2;
  break;
 case 'h':
 case 'H':
  ecclevel = 3;
  break;
 }
 printf("Enter string: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 if (str[strlen(str) - 1] == '\n') {
  str[strlen(str) - 1] = 0;
 }
 side = qr_encode(ecclevel, 0, str, 0, bitdata);
 printf("side: %d\n", side);
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 for (i = 0; i < side; i++) {
  printf("██");
  for (j = 0; j < side; j++) {
   a = i * side + j;
   printf((bitdata[a / 8] & (1 << (7 - a % 8))) ? "  " : "██");
  }
  printf("██");
  printf("\n");
 }
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 return 0;
}

variable_types: side int

variable_types: i int

variable_types: j int

variable_types: a int

variable_types: ecclevel int

variable_types:
bitdata
unsigned char [-1]

variable_types:
str
char [2048]


repo: trigonak/ccbench

decompiled_code:
__int64 color_all()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
int color_all(int id)
{
  return 1;
}

canonical_original_code:
int color_all(int id)
{
  return 1;
}

variable_types: id int


decompiled_code:
void __fastcall barrier_init(unsigned int a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD), int a4)
{
_QWORD *v6; // rax
unsigned int v7; // ebp
unsigned int i; // ebx

if ( a1 <= 0xF )
{
v6 = (_QWORD *)(barriers + 40LL * a1);
v6[1] = 0LL;
v6[2] = 0LL;
v6[3] = 0LL;
v6[4] = a3;
if ( a4 )
{
v7 = 0;
for ( i = 0; i != a4; ++i )
v7 += a3(i);
}
else
{
v7 = 0;
}
*(_QWORD *)(barriers + 40LL * a1) = v7;
}
}

canonical_decompiled_code:
void __fastcall func1(unsigned int a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD), int a4)
{
_QWORD *v6; // rax
unsigned int v7; // ebp
unsigned int i; // ebx

if ( a1 <= 0xF )
{
v6 = (_QWORD *)(barriers + 40LL * a1);
v6[1] = 0LL;
v6[2] = 0LL;
v6[3] = 0LL;
v6[4] = a3;
if ( a4 )
{
v7 = 0;
for ( i = 0; i != a4; ++i )
v7 += a3(i);
}
else
{
v7 = 0;
}
*(_QWORD *)(barriers + 40LL * a1) = v7;
}
}

original_code:
void
barrier_init(const uint32_t barrier_num, const uint64_t participants, int (*color)(int),
      const uint32_t total_cores)
{
  if (barrier_num >= 16)
    {
      return;
    }
  barriers[barrier_num].num_crossing1 = 0;
  barriers[barrier_num].num_crossing2 = 0;
  barriers[barrier_num].num_crossing3 = 0;
  barriers[barrier_num].color = color;
  uint32_t ue, num_parts = 0;
  for (ue = 0; ue < total_cores; ue++)
    {
      num_parts += color(ue);
    }
  barriers[barrier_num].num_participants = num_parts;
}

canonical_original_code:
void barrier_init(unsigned int barrier_num, unsigned long participants, int (*color)(int), unsigned int total_cores)
{
  if (barrier_num >= 16)
    {
      return;
    }
  barriers[barrier_num].num_crossing1 = 0;
  barriers[barrier_num].num_crossing2 = 0;
  barriers[barrier_num].num_crossing3 = 0;
  barriers[barrier_num].color = color;
  unsigned int ue; unsigned int num_parts = 0;
  for (ue = 0; ue < total_cores; ue++)
    {
      num_parts += color(ue);
    }
  barriers[barrier_num].num_participants = num_parts;
}

variable_types:
participants
unsigned long

variable_types:
color
int (*)(int)

variable_types:
ue
unsigned int


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

if ( !a1 )
result = shm_unlink("/barrier_mem");
return result;
}

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

if ( !a1 )
result = shm_unlink("/barrier_mem");
return result;
}

original_code:
void
barriers_term(const uint32_t id)
{
  if (id == 0)
    {
      shm_unlink("/barrier_mem");
    }
}

canonical_original_code:
void barriers_term(unsigned int id)
{
  if (id == 0)
    {
      shm_unlink("/barrier_mem");
    }
}

variable_types:
id
unsigned int


getticks

repo: trigonak/sspfd

decompiled_code:
unsigned __int64 getticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func0()
{
return __rdtsc();
}

original_code:
static inline ticks
getticks(void)
{
  unsigned hi, lo;
  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

canonical_original_code:
static inline unsigned long getticks()
{
  unsigned int hi; unsigned int lo;
  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

variable_types:
hi
unsigned int

variable_types:
lo
unsigned int

return_type: unsigned long