decompiled_code:
__int64 __fastcall send_data(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // er12
int v5; // eax

if ( !a2 )
return 1LL;
v3 = a3;
if ( !a3 )
return 1LL;
v4 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), v3);
if ( v5 < 0 )
break;
v3 -= v5;
v4 += v5;
if ( v3 <= 0 )
return 1LL;
}
printf("write response to socket %d failed\n", (unsigned int)fd);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // er12
int v5; // eax

if ( !a2 )
return 1LL;
v3 = a3;
if ( !a3 )
return 1LL;
v4 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), v3);
if ( v5 < 0 )
break;
v3 -= v5;
v4 += v5;
if ( v3 <= 0 )
return 1LL;
}
printf("write response to socket %d failed\n", (unsigned int)fd);
return 0LL;
}

original_code:
static int send_data(int fd, void * data, int size)
{
 int written = 0;
 int remain = size;
 int ret;
 if(data == ((void *)0) || size == 0)
  return 1;
 for(; ;) {
  ret = write(fd, (char *)data + written, remain);
  if(ret < 0) {
   if(ret == 4)
    continue;
   printf("write response to socket %d failed\n", fd);
   return 0;
  }
  written += ret;
  remain -= ret;
  if(remain <= 0)
   break;
 }
 return 1;
}

canonical_original_code:
static int send_data(int fd, void *data, int size)
{
 int written = 0;
 int remain = size;
 int ret;
 if(data == ((void *)0) || size == 0)
  return 1;
 for(; ;) {
  ret = write(fd, (char *)data + written, remain);
  if(ret < 0) {
   if(ret == 4)
    continue;
   printf("write response to socket %d failed\n", fd);
   return 0;
  }
  written += ret;
  remain -= ret;
  if(remain <= 0)
   break;
 }
 return 1;
}

variable_types:
data
void *

variable_types: size int

variable_types: written int

variable_types: remain int

variable_types: ret int


main

canonical_name: main

repo: tracymacding/GtmyMusic

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // bx
int v4; // eax
int v5; // er13
socklen_t v6; // edx
pthread_t *v7; // r12
unsigned int v8; // er14
char *v9; // rax
pthread_t *v10; // rax
pthread_t *v11; // rbx
pthread_t *v12; // r14
int v13; // ebp
int *v14; // rax
char *v15; // rax
pthread_t *v16; // rbx
pthread_t v17; // rbx
int *v19; // rax
char *v20; // rsi
int *v21; // rax
char *v22; // rsi
int *v23; // rax
char *v24; // rax
char optval[4]; // [rsp+8h] [rbp-50h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr v27; // [rsp+10h] [rbp-48h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-38h] BYREF

addr_len = 16;
if ( argc <= 2 )
{
puts("Usage: ./server port file_dir");
exit(1);
}
v3 = atoi(argv[1]);
strcpy(g_dir, argv[2]);
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 < 0 )
{
v23 = _errno_location();
v24 = strerror(*v23);
printf("create listen socket failed: %s\n", v24);
exit(1);
}
setsockopt(v4, 1, 2, optval, 4u);
*(_QWORD *)&v27.sa_data[6] = 0LL;
v27.sa_family = 2;
*(_QWORD *)&v27.sa_data[2] = (unsigned int)_bswap_32_constprop_0(v3, &v27, 16LL);
*(_WORD *)v27.sa_data = _bswap_16(v3);
if ( bind(v5, &v27, v6) < 0 )
{
v21 = _errno_location();
v22 = strerror(*v21);
printf("bind error: %s\n", v22);
LABEL_19:
close(v5);
exit(1);
}
if ( listen(v5, 10) < 0 )
{
v19 = _errno_location();
v20 = strerror(*v19);
printf("listen error: %s\n", v20);
goto LABEL_19;
}
v7 = 0LL;
while ( 1 )
{
v13 = accept(v5, &addr, &addr_len);
if ( v13 < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
printf("accept error: %s, we should exit\n", v15);
g_shutdown = 1;
sleep(5u);
goto LABEL_10;
}
v8 = *(unsigned __int16 *)addr.sa_data;
v9 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("Reveive connect from %s, port %d\n", v9, v8);
v10 = (pthread_t *)malloc(0x18uLL);
v11 = v10;
if ( !v10 )
break;
*(_DWORD *)v10 = v13;
v12 = v10 + 1;
*(_OWORD *)(v10 + 1) = 0LL;
v10[2] = (pthread_t)v7;
v7 = v10;
*((_DWORD *)v10 + 1) = 1;
printf("newfd = %d\n", (unsigned int)v13);
pthread_create(v12, 0LL, thread_loop, v11);
}
puts("malloc failed for new thread failed");
LABEL_10:
g_shutdown = 1;
if ( v7 )
{
v16 = v7;
do
{
while ( !*((_DWORD *)v16 + 1) )
{
v16 = (pthread_t *)v16[2];
if ( !v16 )
goto LABEL_15;
}
pthread_join(v16[1], 0LL);
v16 = (pthread_t *)v16[2];
}
while ( v16 );
do
{
LABEL_15:
v17 = v7[2];
free(v7);
}
while ( v17 );
}
close(v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // bx
int v4; // eax
int v5; // er13
socklen_t v6; // edx
pthread_t *v7; // r12
unsigned int v8; // er14
char *v9; // rax
pthread_t *v10; // rax
pthread_t *v11; // rbx
pthread_t *v12; // r14
int v13; // ebp
int *v14; // rax
char *v15; // rax
pthread_t *v16; // rbx
pthread_t v17; // rbx
int *v19; // rax
char *v20; // rsi
int *v21; // rax
char *v22; // rsi
int *v23; // rax
char *v24; // rax
char optval[4]; // [rsp+8h] [rbp-50h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr v27; // [rsp+10h] [rbp-48h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-38h] BYREF

addr_len = 16;
if ( argc <= 2 )
{
puts("Usage: ./server port file_dir");
exit(1);
}
v3 = atoi(argv[1]);
strcpy(g_dir, argv[2]);
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 < 0 )
{
v23 = _errno_location();
v24 = strerror(*v23);
printf("create listen socket failed: %s\n", v24);
exit(1);
}
setsockopt(v4, 1, 2, optval, 4u);
*(_QWORD *)&v27.sa_data[6] = 0LL;
v27.sa_family = 2;
*(_QWORD *)&v27.sa_data[2] = (unsigned int)func4(v3, &v27, 16LL);
*(_WORD *)v27.sa_data = func3(v3);
if ( bind(v5, &v27, v6) < 0 )
{
v21 = _errno_location();
v22 = strerror(*v21);
printf("bind error: %s\n", v22);
LABEL_19:
close(v5);
exit(1);
}
if ( listen(v5, 10) < 0 )
{
v19 = _errno_location();
v20 = strerror(*v19);
printf("listen error: %s\n", v20);
goto LABEL_19;
}
v7 = 0LL;
while ( 1 )
{
v13 = accept(v5, &addr, &addr_len);
if ( v13 < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
printf("accept error: %s, we should exit\n", v15);
g_shutdown = 1;
sleep(5u);
goto LABEL_10;
}
v8 = *(unsigned __int16 *)addr.sa_data;
v9 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("Reveive connect from %s, port %d\n", v9, v8);
v10 = (pthread_t *)malloc(0x18uLL);
v11 = v10;
if ( !v10 )
break;
*(_DWORD *)v10 = v13;
v12 = v10 + 1;
*(_OWORD *)(v10 + 1) = 0LL;
v10[2] = (pthread_t)v7;
v7 = v10;
*((_DWORD *)v10 + 1) = 1;
printf("newfd = %d\n", (unsigned int)v13);
pthread_create(v12, 0LL, thread_loop, v11);
}
puts("malloc failed for new thread failed");
LABEL_10:
g_shutdown = 1;
if ( v7 )
{
v16 = v7;
do
{
while ( !*((_DWORD *)v16 + 1) )
{
v16 = (pthread_t *)v16[2];
if ( !v16 )
goto LABEL_15;
}
pthread_join(v16[1], 0LL);
v16 = (pthread_t *)v16[2];
}
while ( v16 );
do
{
LABEL_15:
v17 = v7[2];
free(v7);
}
while ( v17 );
}
close(v5);
return 0;
}

original_code:
int main(int argc, char *args[])
{
 int lfd, newfd;
 int reuseaddr;
 struct sockaddr_in server_addr, c_addr;
 int port = 123456;
 struct thread *thread_info = ((void *)0);
 struct thread *t_queue_head = ((void *)0);
 struct thread *entry = ((void *)0);
 struct thread *next = ((void *)0);
 socklen_t sin_len = sizeof(struct sockaddr_in);
 if(argc < 3) {
  printf("Usage: ./server port file_dir\n");
  exit(1);
 }
 port = atoi(args[1]);
 strcpy(g_dir, args[2]);
 lfd = socket(2, SOCK_STREAM, 0);
 if(lfd < 0) {
  printf("create listen socket failed: %s\n", strerror((*__errno_location ())));
  exit(1);
 }
 setsockopt (lfd, 1,2, (const void *) &reuseaddr, sizeof(int));
 bzero(&server_addr, sizeof(server_addr));
 server_addr.sin_family = 2;
 server_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 server_addr.sin_port = htons(port);
 if(bind(lfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) < 0) {
  printf("bind error: %s\n", strerror((*__errno_location ())));
  close(lfd);
  exit(1);
 }
 if(listen(lfd, 10) < 0) {
  printf("listen error: %s\n", strerror((*__errno_location ())));
  close(lfd);
  exit(1);
 }
 for(;;) {
  if((newfd = accept(lfd, (struct sockaddr *)(&c_addr), &sin_len)) < 0) {
   printf("accept error: %s, we should exit\n", strerror((*__errno_location ())));
   g_shutdown = 1;
   break;
  }
  printf("Reveive connect from %s, port %d\n", inet_ntoa(c_addr.sin_addr), c_addr.sin_port);
  if((thread_info = (struct thread *)malloc(sizeof(thread_t))) == ((void *)0)) {
   printf("malloc failed for new thread failed\n");
   goto out;
  }
  memset(thread_info, 0, sizeof(thread_t));
  thread_info->fd = newfd;
  thread_info->running = 1;
  thread_info->next = t_queue_head;
  t_queue_head = thread_info;
  printf("newfd = %d\n", thread_info->fd);
  pthread_create(&(thread_info->pid), ((void *)0), thread_loop, (void *)thread_info);
 }
 sleep(5);
out:
 g_shutdown = 1;
 entry = t_queue_head;
 while(entry != ((void *)0)) {
  if(entry->running)
   pthread_join(entry->pid, ((void *)0));
  entry = entry->next;
 }
 entry = t_queue_head;
 next = entry;
 while(next != ((void *)0)) {
  next = entry->next;
  free(entry);
 }
 close(lfd);
 return 0;
}

canonical_original_code:
int main(int argc, char *args[])
{
 int lfd; int newfd;
 int reuseaddr;
 struct sockaddr_in server_addr; struct sockaddr_in c_addr;
 int port = 123456;
 struct thread *thread_info = ((void *)0);
 struct thread *t_queue_head = ((void *)0);
 struct thread *entry = ((void *)0);
 struct thread *next = ((void *)0);
 unsigned int sin_len = sizeof(struct sockaddr_in);
 if(argc < 3) {
  printf("Usage: ./server port file_dir\n");
  exit(1);
 }
 port = atoi(args[1]);
 strcpy(g_dir, args[2]);
 lfd = socket(2, SOCK_STREAM, 0);
 if(lfd < 0) {
  printf("create listen socket failed: %s\n", strerror((*__errno_location ())));
  exit(1);
 }
 setsockopt (lfd, 1,2, (void *) &reuseaddr, sizeof(int));
 bzero(&server_addr, sizeof(server_addr));
 server_addr.sin_family = 2;
 server_addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 server_addr.sin_port = htons(port);
 if(bind(lfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) < 0) {
  printf("bind error: %s\n", strerror((*__errno_location ())));
  close(lfd);
  exit(1);
 }
 if(listen(lfd, 10) < 0) {
  printf("listen error: %s\n", strerror((*__errno_location ())));
  close(lfd);
  exit(1);
 }
 for(;;) {
  if((newfd = accept(lfd, (struct sockaddr *)(&c_addr), &sin_len)) < 0) {
   printf("accept error: %s, we should exit\n", strerror((*__errno_location ())));
   g_shutdown = 1;
   break;
  }
  printf("Reveive connect from %s, port %d\n", inet_ntoa(c_addr.sin_addr), c_addr.sin_port);
  if((thread_info = (struct thread *)malloc(sizeof(thread_t))) == ((void *)0)) {
   printf("malloc failed for new thread failed\n");
   goto out;
  }
  memset(thread_info, 0, sizeof(thread_t));
  thread_info->fd = newfd;
  thread_info->running = 1;
  thread_info->next = t_queue_head;
  t_queue_head = thread_info;
  printf("newfd = %d\n", thread_info->fd);
  pthread_create(&(thread_info->pid), ((void *)0), thread_loop, (void *)thread_info);
 }
 sleep(5);
out:
 g_shutdown = 1;
 entry = t_queue_head;
 while(entry != ((void *)0)) {
  if(entry->running)
   pthread_join(entry->pid, ((void *)0));
  entry = entry->next;
 }
 entry = t_queue_head;
 next = entry;
 while(next != ((void *)0)) {
  next = entry->next;
  free(entry);
 }
 close(lfd);
 return 0;
}

variable_types: argc int

variable_types:
args
char *[]

variable_types: lfd int

variable_types: newfd int

variable_types: reuseaddr 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: port int

variable_types: struct thread { int fd; int running; unsigned long pid; struct thread *next; } *

return_type: int

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

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


decompiled_code:
__int64 __fastcall read_command_header(int fd, _QWORD *a2)
{
_QWORD *v2; // r13
int i; // ebx
int v4; // eax

if ( fd < 0 )
_assert_fail("fd >= 0", "command.c", 0x22u, "read_command_header");
v2 = calloc(8uLL, 1uLL);
if ( !v2 )
return 0LL;
for ( i = 8; i > 0; i -= v4 )
{
v4 = read(fd, v2, i);
if ( v4 < 0 )
{
free(v2);
return 0LL;
}
}
*a2 = *v2;
free(v2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, _QWORD *a2)
{
_QWORD *v2; // r13
int i; // ebx
int v4; // eax

if ( fd < 0 )
_assert_fail("fd >= 0", "command.c", 0x22u, "read_command_header");
v2 = calloc(8uLL, 1uLL);
if ( !v2 )
return 0LL;
for ( i = 8; i > 0; i -= v4 )
{
v4 = read(fd, v2, i);
if ( v4 < 0 )
{
free(v2);
return 0LL;
}
}
*a2 = *v2;
free(v2);
return 1LL;
}

original_code:
static int read_command_header(int fd, struct command_header *header)
{
 char *buf = ((void *)0);
 int size = sizeof(*header);
 int ret = 0;
 ((void) sizeof ((fd >= 0) ? 1 : 0), __extension__ ({ if (fd >= 0) ; else __assert_fail ("fd >= 0", "command.c", 34, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((header != ((void *)0)) ? 1 : 0), __extension__ ({ if (header != ((void *)0)) ; else __assert_fail ("header != NULL", "command.c", 35, __extension__ __PRETTY_FUNCTION__); }));
 buf = (char *)malloc(size);
 if(buf == ((void *)0))
  return 0;
 memset(buf, 0, size);
 for(;;) {
  ret = read(fd, buf, size);
  if(ret < 0) {
   if(ret == 4)
    continue;
   free(buf);
   return 0;
  }
  size -= ret;
  if(size <= 0) {
   header->type = *(int *)buf;
   header->data_size = *((int *)buf + 1);
   break;
  }
 }
 free(buf);
 return 1;
}

variable_types: fd int

variable_types: struct command_header { int type; int data_size; } *

variable_types:
buf
char *


decompiled_code:
__int64 __fastcall read_command_body(int fd, void **a2, size_t a3)
{
int v3; // ebx
void *v5; // rdi
size_t i; // rdx
int v7; // eax
void *v9; // rax
size_t v10; // [rsp+8h] [rbp-20h]
size_t v11; // [rsp+8h] [rbp-20h]

if ( fd < 0 )
_assert_fail("fd >= 0", "command.c", 0x47u, "read_command_body");
v3 = a3;
if ( (int)a3 <= 0 )
_assert_fail("size > 0", "command.c", 0x48u, "read_command_body");
v5 = *a2;
a3 = (int)a3;
if ( !*a2 )
{
v11 = (int)a3;
v9 = malloc((int)a3);
a3 = v11;
*a2 = v9;
v5 = v9;
if ( !v9 )
return 0LL;
}
v10 = a3;
memset(v5, 0, a3);
for ( i = v10; ; i = v3 )
{
v7 = read(fd, *a2, i);
if ( v7 < 0 )
{
free(*a2);
return 0LL;
}
v3 -= v7;
if ( v3 <= 0 )
break;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void **a2, size_t a3)
{
int v3; // ebx
void *v5; // rdi
size_t i; // rdx
int v7; // eax
void *v9; // rax
size_t v10; // [rsp+8h] [rbp-20h]
size_t v11; // [rsp+8h] [rbp-20h]

if ( fd < 0 )
_assert_fail("fd >= 0", "command.c", 0x47u, "read_command_body");
v3 = a3;
if ( (int)a3 <= 0 )
_assert_fail("size > 0", "command.c", 0x48u, "read_command_body");
v5 = *a2;
a3 = (int)a3;
if ( !*a2 )
{
v11 = (int)a3;
v9 = malloc((int)a3);
a3 = v11;
*a2 = v9;
v5 = v9;
if ( !v9 )
return 0LL;
}
v10 = a3;
memset(v5, 0, a3);
for ( i = v10; ; i = v3 )
{
v7 = read(fd, *a2, i);
if ( v7 < 0 )
{
free(*a2);
return 0LL;
}
v3 -= v7;
if ( v3 <= 0 )
break;
}
return 1LL;
}

original_code:
static int read_command_body(int fd, void **buf, int size)
{
 int ret = 0;
 ((void) sizeof ((fd >= 0) ? 1 : 0), __extension__ ({ if (fd >= 0) ; else __assert_fail ("fd >= 0", "command.c", 71, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((size > 0) ? 1 : 0), __extension__ ({ if (size > 0) ; else __assert_fail ("size > 0", "command.c", 72, __extension__ __PRETTY_FUNCTION__); }));
 if(*buf == ((void *)0)) {
  if((*buf = malloc(size)) == ((void *)0))
   return 0;
 }
 memset(*buf, 0, size);
 for(;;) {
  ret = read(fd, *buf, size);
  if(ret < 0) {
   if(ret == 4)
    continue;
   free(*buf);
   buf = ((void *)0);
   return 0;
  }
  size -= ret;
  if(size <= 0)
   break;
 }
 return 1;
}

variable_types:
buf
void **


decompiled_code:
__int64 __fastcall read_file_data(void *src, int a2, __int64 a3)
{
char *v4; // rax
int v5; // er13
char *v6; // rsi
int v7; // er12
int v8; // er14
int v9; // eax
int *v10; // rax
char *v11; // rax
void *v12; // rdi
__int64 result; // rax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
char *v18; // rax
struct stat v19; // [rsp-30h] [rbp-3B8h] BYREF
char dest[256]; // [rsp+60h] [rbp-328h] BYREF
char file[16]; // [rsp+160h] [rbp-228h] BYREF
char v22[496]; // [rsp+170h] [rbp-218h] BYREF

*(_OWORD *)file = 0LL;
memset(v22, 0, sizeof(v22));
memcpy(dest, src, a2);
dest[a2] = 0;
v4 = stpcpy(file, &g_dir);
strcpy(v4, dest);
if ( stat(file, &v19) < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
printf("get file %s stat failed: %s\n", (const char *)src, v15);
result = 0LL;
}
else
{
v5 = open(file, 0);
if ( v5 < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
printf("open file %s failed: %s\n", (const char *)src, v17);
result = 0LL;
}
else
{
v6 = *(char **)(a3 + 8);
v7 = v19.st_size;
if ( v6 || (v18 = (char *)calloc(v19.st_size, 1uLL), *(_QWORD *)(a3 + 8) = v18, (v6 = v18) != 0LL) )
{
v8 = 0;
while ( 1 )
{
v9 = read(v5, &v6[v8], v7);
if ( v9 < 0 )
break;
v7 -= v9;
v8 += v9;
if ( v7 <= 0 )
{
*(_DWORD *)(a3 + 4) = v19.st_size;
close(v5);
return 1LL;
}
v6 = *(char **)(a3 + 8);
}
v10 = _errno_location();
v11 = strerror(*v10);
printf("read file %s failed: %s\n", (const char *)src, v11);
close(v5);
v12 = *(void **)(a3 + 8);
if ( v12 )
free(v12);
result = 0LL;
}
else
{
close(v5);
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *src, int a2, __int64 a3)
{
char *v4; // rax
int v5; // er13
char *v6; // rsi
int v7; // er12
int v8; // er14
int v9; // eax
int *v10; // rax
char *v11; // rax
void *v12; // rdi
__int64 result; // rax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
char *v18; // rax
struct stat v19; // [rsp-30h] [rbp-3B8h] BYREF
char dest[256]; // [rsp+60h] [rbp-328h] BYREF
char file[16]; // [rsp+160h] [rbp-228h] BYREF
char v22[496]; // [rsp+170h] [rbp-218h] BYREF

*(_OWORD *)file = 0LL;
memset(v22, 0, sizeof(v22));
memcpy(dest, src, a2);
dest[a2] = 0;
v4 = stpcpy(file, &g_dir);
strcpy(v4, dest);
if ( stat(file, &v19) < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
printf("get file %s stat failed: %s\n", (const char *)src, v15);
result = 0LL;
}
else
{
v5 = open(file, 0);
if ( v5 < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
printf("open file %s failed: %s\n", (const char *)src, v17);
result = 0LL;
}
else
{
v6 = *(char **)(a3 + 8);
v7 = v19.st_size;
if ( v6 || (v18 = (char *)calloc(v19.st_size, 1uLL), *(_QWORD *)(a3 + 8) = v18, (v6 = v18) != 0LL) )
{
v8 = 0;
while ( 1 )
{
v9 = read(v5, &v6[v8], v7);
if ( v9 < 0 )
break;
v7 -= v9;
v8 += v9;
if ( v7 <= 0 )
{
*(_DWORD *)(a3 + 4) = v19.st_size;
close(v5);
return 1LL;
}
v6 = *(char **)(a3 + 8);
}
v10 = _errno_location();
v11 = strerror(*v10);
printf("read file %s failed: %s\n", (const char *)src, v11);
close(v5);
v12 = *(void **)(a3 + 8);
if ( v12 )
free(v12);
result = 0LL;
}
else
{
close(v5);
result = 0LL;
}
}
}
return result;
}

original_code:
static int read_file_data(char *file_name, int name_len, struct response *res)
{
 int success = 1;
 struct stat file_stat;
 int fd;
 int finished = 0;
 int remain = 0;
 int ret = 0;
 char full_path[512] = {'\0'};
 char name[256];
 memcpy(name, file_name, name_len);
 name[name_len] = '\0';
 strcpy(full_path, g_dir);
 strcat(full_path, name);
 if(stat(full_path, &file_stat) < 0) {
  printf("get file %s stat failed: %s\n", file_name, strerror((*__errno_location ())));
  return 0;
 }
 if((fd = open(full_path, 00)) < 0) {
  printf("open file %s failed: %s\n", file_name, strerror((*__errno_location ())));
  return 0;
 }
 if(res->data == ((void *)0)) {
  if((res->data = malloc(file_stat.st_size)) == ((void *)0)) {
   close(fd);
   return 0;
  }
  memset(res->data, 0, file_stat.st_size);
 }
 remain = file_stat.st_size;
 for(; ;) {
  ret = read(fd, (char *)res->data + finished, remain);
  if(ret < 0) {
   printf("read file %s failed: %s\n", file_name, strerror((*__errno_location ())));
   success = 0;
   goto out;
  }
  remain -= ret;
  finished += ret;
  if(remain <= 0) {
   res->header.data_size = file_stat.st_size;
   break;
  }
 }
out:
 close(fd);
 if(success != 1 && res->data != ((void *)0))
  free(res->data);
 return success;
}

variable_types: struct response { struct response_header { int status; int data_size; } header; void *data; } *

variable_types: success int

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

variable_types: finished int

variable_types:
name
char [256]

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

user_defined_types: struct response { struct response_header header; void *data; }


decompiled_code:
__int64 __fastcall read_command(int fd, void **a2)
{
unsigned int v2; // er12
int v3; // eax
__int64 result; // rax

if ( !a2 )
_assert_fail("comm != NULL", "command.c", 0x6Du, "read_command");
if ( fd < 0 )
_assert_fail("fd >= 0", "command.c", 0x6Eu, "read_command");
*a2 = 0LL;
v2 = read_command_header(fd, a2);
if ( !v2 )
{
printf("Read command header failed of %d\n", (unsigned int)fd);
return 0LL;
}
v3 = *(_DWORD *)a2;
if ( (unsigned __int8)*(_DWORD *)a2 == 2 )
{
puts("Get PULL command");
result = read_command_body(fd, a2 + 1, *((unsigned int *)a2 + 1));
}
else
{
if ( (char)v3 > 2 )
{
if ( (_BYTE)v3 == 3 )
{
puts("Get LEAVE command");
return v2;
}
}
else
{
if ( !(_BYTE)v3 )
{
puts("Get LIST command");
return v2;
}
if ( (_BYTE)v3 == 1 )
{
puts("Get DIFF command");
return v2;
}
}
puts("Invalid command");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, void **a2)
{
unsigned int v2; // er12
int v3; // eax
__int64 result; // rax

if ( !a2 )
_assert_fail("comm != NULL", "command.c", 0x6Du, "read_command");
if ( fd < 0 )
_assert_fail("fd >= 0", "command.c", 0x6Eu, "read_command");
*a2 = 0LL;
v2 = func0(fd, a2);
if ( !v2 )
{
printf("Read command header failed of %d\n", (unsigned int)fd);
return 0LL;
}
v3 = *(_DWORD *)a2;
if ( (unsigned __int8)*(_DWORD *)a2 == 2 )
{
puts("Get PULL command");
result = func1(fd, a2 + 1, *((unsigned int *)a2 + 1));
}
else
{
if ( (char)v3 > 2 )
{
if ( (_BYTE)v3 == 3 )
{
puts("Get LEAVE command");
return v2;
}
}
else
{
if ( !(_BYTE)v3 )
{
puts("Get LIST command");
return v2;
}
if ( (_BYTE)v3 == 1 )
{
puts("Get DIFF command");
return v2;
}
}
puts("Invalid command");
result = 0LL;
}
return result;
}

original_code:
int read_command(int fd, struct command *comm)
{
 char command;
 int res = 1;
 ((void) sizeof ((comm != ((void *)0)) ? 1 : 0), __extension__ ({ if (comm != ((void *)0)) ; else __assert_fail ("comm != NULL", "command.c", 109, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((fd >= 0) ? 1 : 0), __extension__ ({ if (fd >= 0) ; else __assert_fail ("fd >= 0", "command.c", 110, __extension__ __PRETTY_FUNCTION__); }));
 memset(&(comm->header), 0, sizeof(comm->header));
 res = read_command_header(fd, &(comm->header));
 if(res == 0) {
  printf("Read command header failed of %d\n", fd);
  return 0;
 }
 command = comm->header.type;
 switch(command) {
  case 0:
   printf("Get LIST command\n");
   break;
  case 3:
   printf("Get LEAVE command\n");
   break;
  case 1:
   printf("Get DIFF command\n");
   break;
  case 2:
   printf("Get PULL command\n");
   res = read_command_body(fd, &(comm->data), comm->header.data_size);
   break;
  default:
   printf("Invalid command\n");
   res = 0;
 }
 return res;
}

variable_types: struct command { struct command_header { int type; int data_size; } header; void *data; } *

variable_types: command char

variable_types: res int

user_defined_types: struct command { struct command_header header; void *data; }


decompiled_code:
__int64 __fastcall process_command(void **a1, __int64 a2)
{
int v2; // eax

v2 = *(_DWORD *)a1;
if ( (unsigned __int8)*(_DWORD *)a1 == 2 )
return read_file_data(a1[1], *((_DWORD *)a1 + 1), a2);
if ( (char)v2 > 2 )
return (_BYTE)v2 == 3;
if ( (_BYTE)v2 )
return (_BYTE)v2 == 1;
return list_current_files(a2);
}

canonical_decompiled_code:
__int64 __fastcall func6(void **a1, __int64 a2)
{
int v2; // eax

v2 = *(_DWORD *)a1;
if ( (unsigned __int8)*(_DWORD *)a1 == 2 )
return func2(a1[1], *((_DWORD *)a1 + 1), a2);
if ( (char)v2 > 2 )
return (_BYTE)v2 == 3;
if ( (_BYTE)v2 )
return (_BYTE)v2 == 1;
return func4(a2);
}

original_code:
int process_command(struct command *comm, struct response *res)
{
 char command = comm->header.type;
 switch(command) {
  case 0:
   return list_current_files(res);
   break;
  case 1:
   break;
  case 2:
   return read_file_data((char *)comm->data, comm->header.data_size, res);
   break;
  case 3:
   break;
  default:
   return 0;
   break;
 }
 return 1;
}

user_defined_types: struct command_header { int type; int data_size; }

user_defined_types: struct response_header { int status; int data_size; }


decompiled_code:
__int64 __fastcall compute_file_md5(const char *a1, char *a2, int a3)
{
int v4; // eax
int v5; // ebp
char *v6; // rax
size_t v7; // rdx
char *v8; // r12
int v9; // er14
int v10; // eax
int *v11; // rax
char *v12; // rax
unsigned int v13; // er13
char *v15; // r13
__int64 v16; // rdx
int *v17; // rax
char *v18; // rax
__int64 v19; // [rsp+8h] [rbp-60h]
char s[2]; // [rsp+1Dh] [rbp-4Bh] BYREF
char v21; // [rsp+1Fh] [rbp-49h]
char v22[16]; // [rsp+20h] [rbp-48h] BYREF
char v23; // [rsp+30h] [rbp-38h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, char *a2, int a3)
{
int v4; // eax
int v5; // ebp
char *v6; // rax
size_t v7; // rdx
char *v8; // r12
int v9; // er14
int v10; // eax
int *v11; // rax
char *v12; // rax
unsigned int v13; // er13
char *v15; // r13
__int64 v16; // rdx
int *v17; // rax
char *v18; // rax
__int64 v19; // [rsp+8h] [rbp-60h]
char s[2]; // [rsp+1Dh] [rbp-4Bh] BYREF
char v21; // [rsp+1Fh] [rbp-49h]
char v22[16]; // [rsp+20h] [rbp-48h] BYREF
char v23; // [rsp+30h] [rbp-38h] BYREF

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

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 func10()
{
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 func11(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;
}


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
int v4; // er12
const char *v5; // rdi
int v6; // eax
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v2; // ebp
int v4; // er12
const char *v5; // rdi
int v6; // eax
__int64 result; // rax

if ( !a1 )
_assert_fail("queue", "queue.c", 0x15u, "insert_node");
v2 = *(_DWORD *)(a1 + 16);
*(_QWORD *)(a2 + 8) = 0LL;
if ( v2 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = a2;
else
*(_QWORD *)a1 = a2;
v4 = *(_DWORD *)(a1 + 20);
v5 = *(const char **)a2;
*(_QWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 20) = v4 + 16;
v6 = strlen(v5);
*(_DWORD *)(a1 + 16) = v2 + 1;
result = (unsigned int)(v4 + v6 + 17);
*(_DWORD *)(a1 + 20) = result;
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; // r12

v0 = safe_malloc(64LL);
*(_QWORD *)v0 = 0LL;
*(_QWORD *)(v0 + 8) = 0LL;
*(_QWORD *)(v0 + 16) = 0x4000000000LL;
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; // r13

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

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 result; // rax
__int64 v3; // [rsp+8h] [rbp-20h]

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

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-20h]

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

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_constprop_0(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(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)
{
int result; // eax
const char *v2; // rdx
const char *v3; // rdx
const char *v4; // rdx
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_3;
if ( (int)write(fd, "102", 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)"102", v2);
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)v5, v3);
if ( (int)read(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR reading from socket", (int)v5, v4);
if ( atoi(v5) )
result = puts("Selection added to queue!");
else
LABEL_3:
result = puts("Invalid selection!");
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd)
{
int result; // eax
const char *v2; // rdx
const char *v3; // rdx
const char *v4; // rdx
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_3;
if ( (int)write(fd, "102", 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)"102", v2);
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR writing to socket", (int)v5, v3);
if ( (int)read(fd, v5, 0xFFuLL) < 0 )
error((int)"ERROR reading from socket", (int)v5, v4);
if ( atoi(v5) )
result = puts("Selection added to queue!");
else
LABEL_3:
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; // er14
__int64 v4; // r15
void **v5; // rax
void **v6; // r13
void **v7; // rbx
__int64 v8; // r14
const char *v9; // rdx
const char *v10; // rdx
int v11; // er15
void **v12; // rax
void **v13; // r14
void **v14; // rbx
__int64 v15; // r15
const char *v16; // rdx
void **v17; // rbx
void *i; // rdi
void *v19; // rdi
void **j; // rbx
__int64 v21; // [rsp+8h] [rbp-140h]
char buf[312]; // [rsp+10h] [rbp-138h] BYREF

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

canonical_decompiled_code:
void __fastcall func2(int fd)
{
const char *v1; // rdx
const char *v2; // rdx
int v3; // er14
__int64 v4; // r15
void **v5; // rax
void **v6; // r13
void **v7; // rbx
__int64 v8; // r14
const char *v9; // rdx
const char *v10; // rdx
int v11; // er15
void **v12; // rax
void **v13; // r14
void **v14; // rbx
__int64 v15; // r15
const char *v16; // rdx
void **v17; // rbx
void *i; // rdi
void *v19; // rdi
void **j; // rbx
__int64 v21; // [rsp+8h] [rbp-140h]
char buf[312]; // [rsp+10h] [rbp-138h] BYREF

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

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:
int __fastcall begin(int fd)
{
int v1; // eax
int v2; // er13
int result; // eax
char v4[296]; // [rsp+0h] [rbp-128h] BYREF

print_logo();
LABEL_2:
print_menu();
while ( 1 )
{
while ( 1 )
{
printf("\nOption: ");
memset(v4, 0, 0x100uLL);
fgets(v4, 255, stdin);
if ( strlen(v4) == 2 && (unsigned int)(v4[0] - 48) <= 9 )
break;
v1 = atoi(v4);
if ( v1 == 2 )
{
v2 = 0;
LABEL_17:
add_to_queue(fd);
goto LABEL_9;
}
if ( v1 != 9 && v1 == 1 )
{
v2 = 0;
LABEL_8:
list_request(fd);
LABEL_9:
if ( v2 )
goto LABEL_2;
}
}
result = atoi(v4);
v2 = result;
switch ( result )
{
case 2:
v2 = 1;
goto LABEL_17;
case 9:
return result;
case 1:
goto LABEL_8;
}
}
}

canonical_decompiled_code:
int __fastcall func5(int fd)
{
int v1; // eax
int v2; // er13
int result; // eax
char v4[296]; // [rsp+0h] [rbp-128h] BYREF

func4();
LABEL_2:
func3();
while ( 1 )
{
while ( 1 )
{
printf("\nOption: ");
memset(v4, 0, 0x100uLL);
fgets(v4, 255, stdin);
if ( strlen(v4) == 2 && (unsigned int)(v4[0] - 48) <= 9 )
break;
v1 = atoi(v4);
if ( v1 == 2 )
{
v2 = 0;
LABEL_17:
func0(fd);
goto LABEL_9;
}
if ( v1 != 9 && v1 == 1 )
{
v2 = 0;
LABEL_8:
func2(fd);
LABEL_9:
if ( v2 )
goto LABEL_2;
}
}
result = atoi(v4);
v2 = result;
switch ( result )
{
case 2:
v2 = 1;
goto LABEL_17;
case 9:
return result;
case 1:
goto LABEL_8;
}
}
}

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
__int64 v3; // rax
void *v4; // r15
__int64 v5; // r12
int v6; // eax
__int64 v7; // rdx
int v8; // ebx
__useconds_t v9; // er14
int v10; // ebp
int v11; // eax
int optval; // [rsp+0h] [rbp-74h] BYREF
struct addrinfo *pai; // [rsp+4h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+Ch] [rbp-68h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
optval = 1;
*(_QWORD *)&req.ai_family = 0x200000002LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 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 = retrieve_song(queue, list);
v4 = (void *)v3;
if ( !v3 )
break;
v5 = createWav(v3);
if ( !(unsigned __int8)loadWavData(v5) )
error("ERROR loading wave file");
v6 = getFrameSize(*(_QWORD *)(v5 + 16));
v7 = *(_QWORD *)(v5 + 24);
v8 = *(_DWORD *)v7;
v9 = (int)(1.0 / (double)(*(_DWORD *)(*(_QWORD *)(v5 + 16) + 12LL) * v6 / 0x5BEu) * 955000.0);
if ( *(int *)v7 > 0 )
{
v10 = 0;
while ( 1 )
{
v11 = sendto(v2, (const void *)(*(_QWORD *)(v7 + 8) + v10), 0x5BEuLL, 0, pai->ai_addr, pai->ai_addrlen);
if ( v11 == -1 )
error("ERROR sending stream data");
v8 -= v11;
v10 += v11;
usleep(v9);
if ( v8 <= 0 )
break;
v7 = *(_QWORD *)(v5 + 24);
}
}
update_queue(queue);
deleteWav(v5);
free(v4);
}
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
__int64 v3; // rax
void *v4; // r15
__int64 v5; // r12
int v6; // eax
__int64 v7; // rdx
int v8; // ebx
__useconds_t v9; // er14
int v10; // ebp
int v11; // eax
int optval; // [rsp+0h] [rbp-74h] BYREF
struct addrinfo *pai; // [rsp+4h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+Ch] [rbp-68h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
optval = 1;
*(_QWORD *)&req.ai_family = 0x200000002LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 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 = retrieve_song(queue, list);
v4 = (void *)v3;
if ( !v3 )
break;
v5 = createWav(v3);
if ( !(unsigned __int8)loadWavData(v5) )
func1("ERROR loading wave file");
v6 = getFrameSize(*(_QWORD *)(v5 + 16));
v7 = *(_QWORD *)(v5 + 24);
v8 = *(_DWORD *)v7;
v9 = (int)(1.0 / (double)(*(_DWORD *)(*(_QWORD *)(v5 + 16) + 12LL) * v6 / 0x5BEu) * 955000.0);
if ( *(int *)v7 > 0 )
{
v10 = 0;
while ( 1 )
{
v11 = sendto(v2, (const void *)(*(_QWORD *)(v7 + 8) + v10), 0x5BEuLL, 0, pai->ai_addr, pai->ai_addrlen);
if ( v11 == -1 )
func1("ERROR sending stream data");
v8 -= v11;
v10 += v11;
usleep(v9);
if ( v8 <= 0 )
break;
v7 = *(_QWORD *)(v5 + 24);
}
}
update_queue(queue);
deleteWav(v5);
free(v4);
}
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)
{
_QWORD *v1; // r12
_QWORD *v2; // r13
const void *v3; // rsi
_QWORD *v4; // rbx
const void *v5; // rsi
_QWORD *v6; // rbx
void *v7; // rdi
void **v8; // rbx
void *v9; // rdi
void **v10; // rbx
char v11[296]; // [rsp+0h] [rbp-128h] BYREF

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

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

v1 = (_QWORD *)get_list(list);
v2 = (_QWORD *)get_list(queue);
sprintf(v11, "%d", *(unsigned int *)(list + 16));
if ( (int)write(fd, v11, 0xFFuLL) < 0 )
goto LABEL_19;
v3 = (const void *)*v1;
v4 = v1 + 1;
if ( *v1 )
{
while ( (int)write(fd, v3, 0xFFuLL) >= 0 )
{
++v4;
usleep(0x2710u);
v3 = (const void *)*(v4 - 1);
if ( !v3 )
goto LABEL_5;
}
LABEL_19:
func1("ERROR writing to socket");
}
LABEL_5:
sprintf(v11, "%d", *(unsigned int *)(queue + 16));
if ( (int)write(fd, v11, 0xFFuLL) < 0 )
goto LABEL_19;
v5 = (const void *)*v2;
if ( *v2 )
{
v6 = v2 + 1;
do
{
if ( (int)write(fd, v5, 0xFFuLL) < 0 )
goto LABEL_19;
++v6;
usleep(0x2710u);
v5 = (const void *)*(v6 - 1);
}
while ( v5 );
v7 = (void *)*v1;
if ( !*v1 )
goto LABEL_13;
}
else
{
v7 = (void *)*v1;
if ( !*v1 )
goto LABEL_16;
}
v8 = (void **)(v1 + 1);
do
{
free(v7);
v7 = *v8++;
}
while ( v7 );
LABEL_13:
v9 = (void *)*v2;
if ( *v2 )
{
v10 = (void **)(v2 + 1);
do
{
free(v9);
v9 = *v10++;
}
while ( v9 );
}
LABEL_16:
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; // er12
int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
char v7[280]; // [rsp+0h] [rbp-118h] BYREF

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

canonical_decompiled_code:
int __fastcall func5(void *a1)
{
int v1; // eax
int v2; // er12
int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
char v7[280]; // [rsp+0h] [rbp-118h] BYREF

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

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

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
const char *v4; // rdi
unsigned int v5; // er12
socklen_t v6; // edx
_QWORD *v7; // rax
_QWORD *v8; // rbx
unsigned int v9; // ebx
int *v10; // r12
int v11; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
pthread_t v13; // [rsp+10h] [rbp-48h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-40h] BYREF
struct sockaddr v15; // [rsp+20h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

if ( argc > 1 )
{
v3 = socket(2, 1, 0);
if ( v3 >= 0 )
{
v4 = argv[1];
v15 = 0LL;
v15.sa_family = 2;
v5 = atoi(v4);
*(_WORD *)v15.sa_data = _bswap_16(v5);
if ( bind(v3, &v15, v6) >= 0 )
{
if ( listen(v3, 5) != -1 )
{
printf("\nServer is starting on port: %d\n", v5);
list = new_queue();
queue = new_queue();
read_directory();
v7 = (_QWORD *)import_list();
if ( *v7 )
{
v8 = v7 + 1;
do
{
++v8;
add_song(list);
}
while ( *(v8 - 1) );
}
v9 = 0;
auto_queue(queue, list);
while ( 1 )
{
printf("Preparing thread %d\n", v9);
v10 = (int *)safe_malloc(8LL);
addr_len = 16;
v11 = accept(v3, &addr, &addr_len);
*v10 = v11;
if ( v11 < 0 )
break;
v10[1] = v9;
pthread_create(&newthread, 0LL, (void *(*)(void *))clientAction, v10);
printf("Thread %d live\n", v9);
if ( !v9 )
pthread_create(&v13, 0LL, (void *(*)(void *))beginStreaming, 0LL);
++v9;
}
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; // ebp
const char *v4; // rdi
unsigned int v5; // er12
socklen_t v6; // edx
_QWORD *v7; // rax
_QWORD *v8; // rbx
unsigned int v9; // ebx
int *v10; // r12
int v11; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
pthread_t v13; // [rsp+10h] [rbp-48h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-40h] BYREF
struct sockaddr v15; // [rsp+20h] [rbp-38h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

if ( argc > 1 )
{
v3 = socket(2, 1, 0);
if ( v3 >= 0 )
{
v4 = argv[1];
v15 = 0LL;
v15.sa_family = 2;
v5 = atoi(v4);
*(_WORD *)v15.sa_data = func0(v5);
if ( bind(v3, &v15, v6) >= 0 )
{
if ( listen(v3, 5) != -1 )
{
printf("\nServer is starting on port: %d\n", v5);
list = new_queue();
queue = new_queue();
read_directory();
v7 = (_QWORD *)import_list();
if ( *v7 )
{
v8 = v7 + 1;
do
{
++v8;
add_song(list);
}
while ( *(v8 - 1) );
}
v9 = 0;
auto_queue(queue, list);
while ( 1 )
{
printf("Preparing thread %d\n", v9);
v10 = (int *)safe_malloc(8LL);
addr_len = 16;
v11 = accept(v3, &addr, &addr_len);
*v10 = v11;
if ( v11 < 0 )
break;
v10[1] = v9;
pthread_create(&newthread, 0LL, (void *(*)(void *))clientAction, v10);
printf("Thread %d live\n", v9);
if ( !v9 )
pthread_create(&v13, 0LL, (void *(*)(void *))beginStreaming, 0LL);
++v9;
}
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 client_args_t { int sock; int id; }


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; // er8
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; // er8
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; // r12
__int64 v2; // rax
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // rax
__int64 result; // rax

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

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; // ebx
pthread_mutex_t *v2; // rbp
__int64 i; // r12
unsigned int j; // eax
int v5; // eax
pthread_mutex_t *v6; // rdi
__int64 v7; // rax
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_socktype = 0LL;
req.ai_next = 0LL;
*(_QWORD *)&req.ai_flags = 0x200000001LL;
req.ai_socktype = 2;
*(_OWORD *)&req.ai_addr = 0LL;
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 )
{
v2 = &buffer1Mutex;
freeaddrinfo(pai);
for ( i = buffer1; ; i = v7 )
{
pthread_mutex_lock(v2);
for ( j = *(_DWORD *)(i + 12); j + 1470 < *(_DWORD *)(i + 8); *(_DWORD *)(i + 12) = j )
{
v5 = recvfrom(v1, (void *)(*(_QWORD *)i + j), 0x5BEuLL, 0, 0LL, 0LL);
if ( v5 == -1 )
error("ERROR receiving streaming data");
if ( !v5 )
error("ERROR connection to server closed");
j = *(_DWORD *)(i + 12) + v5;
}
*(_BYTE *)(i + 16) = 1;
pthread_cond_signal(&bufferCV);
v6 = v2;
v2 = &buffer1Mutex;
pthread_mutex_unlock(v6);
v7 = buffer1;
if ( buffer1 == i )
{
v7 = buffer2;
v2 = (pthread_mutex_t *)&buffer2Mutex;
}
}
}
error("ERROR binding socket");
}
error("ERROR creating socket");
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
int v1; // ebx
pthread_mutex_t *v2; // rbp
__int64 i; // r12
unsigned int j; // eax
int v5; // eax
pthread_mutex_t *v6; // rdi
__int64 v7; // rax
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_socktype = 0LL;
req.ai_next = 0LL;
*(_QWORD *)&req.ai_flags = 0x200000001LL;
req.ai_socktype = 2;
*(_OWORD *)&req.ai_addr = 0LL;
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 )
{
v2 = &buffer1Mutex;
freeaddrinfo(pai);
for ( i = buffer1; ; i = v7 )
{
pthread_mutex_lock(v2);
for ( j = *(_DWORD *)(i + 12); j + 1470 < *(_DWORD *)(i + 8); *(_DWORD *)(i + 12) = j )
{
v5 = recvfrom(v1, (void *)(*(_QWORD *)i + j), 0x5BEuLL, 0, 0LL, 0LL);
if ( v5 == -1 )
func1("ERROR receiving streaming data");
if ( !v5 )
func1("ERROR connection to server closed");
j = *(_DWORD *)(i + 12) + v5;
}
*(_BYTE *)(i + 16) = 1;
pthread_cond_signal(&bufferCV);
v6 = v2;
v2 = &buffer1Mutex;
pthread_mutex_unlock(v6);
v7 = buffer1;
if ( buffer1 == i )
{
v7 = buffer2;
v2 = (pthread_mutex_t *)&buffer2Mutex;
}
}
}
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