startServer

decompiled_code:
__int64 __fastcall startServer(__int16 *a1)
{
int v1; // eax
unsigned int v2; // ebx
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
perror("startServer:socket");
v2 = -1;
}
else
{
v2 = v1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(*a1);
if ( (int)bindMod(v2, &addr, 16LL) < 0 )
{
perror("startServer:bind");
v2 = -2;
}
else if ( getsockname(v2, &addr, &len) < 0 )
{
perror("startServer:getsockname");
v2 = -3;
}
else if ( listen(v2, 1024) < 0 )
{
perror("startServer:listen");
v2 = -4;
}
else
{
*a1 = *(_WORD *)addr.sa_data;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int16 *a1)
{
int v1; // eax
unsigned int v2; // ebx
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
perror("startServer:socket");
v2 = -1;
}
else
{
v2 = v1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = func0(0);
*(_WORD *)addr.sa_data = func1(*a1);
if ( (int)bindMod(v2, &addr, 16LL) < 0 )
{
perror("startServer:bind");
v2 = -2;
}
else if ( getsockname(v2, &addr, &len) < 0 )
{
perror("startServer:getsockname");
v2 = -3;
}
else if ( listen(v2, 1024) < 0 )
{
perror("startServer:listen");
v2 = -4;
}
else
{
*a1 = *(_WORD *)addr.sa_data;
}
}
return v2;
}

original_code:
int startServer(in_port_t *portNumber)
{
 int serverSocket;
 struct sockaddr_in local;
 socklen_t len = sizeof(local);
 if ((serverSocket = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  perror("startServer:socket");
  return -1;
 }
 local.sin_family = 2;
 local.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 local.sin_port = htons(*portNumber);
 if(bindMod(serverSocket, (struct sockaddr *) &local, sizeof(local)) < 0) {
  perror("startServer:bind");
  return -2;
 }
 if (getsockname(serverSocket, (struct sockaddr *)&local, &len) < 0) {
  perror("startServer:getsockname");
  return -3;
 }
 if (listen(serverSocket, 1024) < 0) {
  perror("startServer:listen");
  return -4;
 }
 *portNumber = local.sin_port;
 return serverSocket;
}

canonical_original_code:
int startServer(unsigned short *portNumber)
{
 int serverSocket;
 struct sockaddr_in local;
 unsigned int len = sizeof(local);
 if ((serverSocket = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  perror("startServer:socket");
  return -1;
 }
 local.sin_family = 2;
 local.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 local.sin_port = htons(*portNumber);
 if(bindMod(serverSocket, (struct sockaddr *) &local, sizeof(local)) < 0) {
  perror("startServer:bind");
  return -2;
 }
 if (getsockname(serverSocket, (struct sockaddr *)&local, &len) < 0) {
  perror("startServer:getsockname");
  return -3;
 }
 if (listen(serverSocket, 1024) < 0) {
  perror("startServer:listen");
  return -4;
 }
 *portNumber = local.sin_port;
 return serverSocket;
}

variable_types:
portNumber
unsigned short *

variable_types: serverSocket 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:
len
unsigned int


main

canonical_name: main

repo: jtdreisb/networks

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er14
unsigned __int16 v5; // ax
int v6; // er13
__int64 v7; // rbp
__int64 v8[16]; // [rsp+0h] [rbp-B8h] BYREF
double v9; // [rsp+80h] [rbp-38h] BYREF
__int16 v10[21]; // [rsp+8Eh] [rbp-2Ah] BYREF

v10[0] = 0;
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s <error>\n", *argv);
exit(1);
}
v3 = startServer(v10);
v4 = v3;
if ( v3 >= 0 )
{
_isoc99_sscanf(argv[1], "%lf", &v9);
sendErr_init(1LL, 1LL, 0LL, 0LL, v9);
*(_DWORD *)(clientList + 16LL) = 3;
v5 = _bswap_16(v10[0]);
printf("Server is using port %d\n", v5);
v6 = v4 / 64;
v7 = 1LL << (v4 % 64);
while ( 1 )
{
memset(v8, 0, sizeof(v8));
v8[v6] |= v7;
setActiveClientsForSelect((__int64)v8);
if ( (int)selectMod((unsigned int)(*(_DWORD *)(clientList + 16LL) + 1), v8, 0LL, 0LL, 0LL) < 0 )
break;
if ( (v8[v6] & v7) != 0 )
acceptNewClient(v4);
checkActiveClientsAfterSelect((__int64)v8);
}
perror("main:select");
exit(-5);
}
exit(v3);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er14
unsigned __int16 v5; // ax
int v6; // er13
__int64 v7; // rbp
__int64 v8[16]; // [rsp+0h] [rbp-B8h] BYREF
double v9; // [rsp+80h] [rbp-38h] BYREF
__int16 v10[21]; // [rsp+8Eh] [rbp-2Ah] BYREF

v10[0] = 0;
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s <error>\n", *argv);
exit(1);
}
v3 = func12(v10);
v4 = v3;
if ( v3 >= 0 )
{
_isoc99_sscanf(argv[1], "%lf", &v9);
sendErr_init(1LL, 1LL, 0LL, 0LL, v9);
*(_DWORD *)(clientList + 16LL) = 3;
v5 = func1(v10[0]);
printf("Server is using port %d\n", v5);
v6 = v4 / 64;
v7 = 1LL << (v4 % 64);
while ( 1 )
{
memset(v8, 0, sizeof(v8));
v8[v6] |= v7;
func10((__int64)v8);
if ( (int)selectMod((unsigned int)(*(_DWORD *)(clientList + 16LL) + 1), v8, 0LL, 0LL, 0LL) < 0 )
break;
if ( (v8[v6] & v7) != 0 )
func2(v4);
func11((__int64)v8);
}
perror("main:select");
exit(-5);
}
exit(v3);
}

original_code:
int main(int argc, char const *argv[])
{
 int serverSocket = -1;
 in_port_t serverPort = 0;
 if (argc != 2) {
  fprintf(stderr, "Usage: %s <error>\n", argv[0]);
  exit(1);
 }
 serverSocket = startServer(&serverPort);
 if (serverSocket < 0) {
  exit(serverSocket);
 }
 double errorRate;
    sscanf(argv[1], "%lf", &errorRate);
 sendErr_init(errorRate,
  1,
  1,
  0,
  0);
 clientList->maxSocket = 3;
 printf("Server is using port %d\n", ntohs(serverPort));
 for(;;) {
  fd_set fdSet;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdSet)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fdSet)->__fds_bits)[((serverSocket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serverSocket) % (8 * (int) sizeof (__fd_mask)))))));
  setActiveClientsForSelect(&fdSet);
  if (selectMod(clientList->maxSocket+1, &fdSet, ((void *)0), ((void *)0), ((void *)0)) < 0){
   perror("main:select");
   exit(-5);
  }
  else {
   if (((((&fdSet)->__fds_bits)[((serverSocket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((serverSocket) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    acceptNewClient(serverSocket);
   }
   checkActiveClientsAfterSelect(&fdSet);
  }
 }
 close(serverSocket);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int serverSocket = -1;
 unsigned short serverPort = 0;
 if (argc != 2) {
  fprintf(stderr, "Usage: %s <error>\n", argv[0]);
  exit(1);
 }
 serverSocket = startServer(&serverPort);
 if (serverSocket < 0) {
  exit(serverSocket);
 }
 double errorRate;
    sscanf(argv[1], "%lf", &errorRate);
 sendErr_init(errorRate,
  1,
  1,
  0,
  0);
 clientList->maxSocket = 3;
 printf("Server is using port %d\n", ntohs(serverPort));
 for(;;) {
  struct fd_set fdSet;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdSet)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fdSet)->__fds_bits)[((serverSocket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serverSocket) % (8 * (int) sizeof (__fd_mask)))))));
  setActiveClientsForSelect(&fdSet);
  if (selectMod(clientList->maxSocket+1, &fdSet, ((void *)0), ((void *)0), ((void *)0)) < 0){
   perror("main:select");
   exit(-5);
  }
  else {
   if (((((&fdSet)->__fds_bits)[((serverSocket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((serverSocket) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    acceptNewClient(serverSocket);
   }
   checkActiveClientsAfterSelect(&fdSet);
  }
 }
 close(serverSocket);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
serverPort
unsigned short

variable_types: errorRate double

variable_types:
fdSet
struct fd_set { long __fds_bits[-1]; }

return_type: int


clearWindow

decompiled_code:
void *__fastcall clearWindow(__int64 a1)
{
*(_DWORD *)(a1 + 36) = 0;
return memset(*(void **)a1, 0, *(unsigned int *)(a1 + 16));
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1)
{
*(_DWORD *)(a1 + 36) = 0;
return memset(*(void **)a1, 0, *(unsigned int *)(a1 + 16));
}

original_code:
void clearWindow(Window *window)
{
  window->window_index = 0;
  memset(window->registry, 0, window->window_size);
}

canonical_original_code:
void clearWindow(struct Window *window)
{
  window->window_index = 0;
  memset(window->registry, 0, window->window_size);
}

variable_types: struct Window { unsigned char *registry; unsigned char *buffer; unsigned int window_size; unsigned int block_size; unsigned int buffer_size; int data_len; unsigned int base_seq_num; unsigned int window_index; } *

return_type: void


windowIsFull

original_code:
int32_t windowIsFull(Window *window)
{
  int32_t window_size = (int32_t) ceil((1.0 * window->data_len) / window->block_size);
  int i;
  for (i = 0; i < window_size; i++) {
    if(window->registry[i] == 0) {
      return 0;
    }
  }
  return 1;
}

canonical_original_code:
int windowIsFull(struct Window *window)
{
  int window_size = (int32_t) ceil((1.0 * window->data_len) / window->block_size);
  int i;
  for (i = 0; i < window_size; i++) {
    if(window->registry[i] == 0) {
      return 0;
    }
  }
  return 1;
}

variable_types: i int

user_defined_types: struct Window { unsigned char *registry; unsigned char *buffer; unsigned int window_size; unsigned int block_size; unsigned int buffer_size; int data_len; unsigned int base_seq_num; unsigned int window_index; }


maxSequenceNumber

original_code:
uint32_t maxSequenceNumber(Window *window)
{
  uint32_t max_window_index = 0;
  int i = 0;
  for (i = 0; i < window->window_size; i++) {
    if(window->registry[i] == 1) {
      max_window_index = i;
    }
  }
  return window->base_seq_num + max_window_index;
}

canonical_original_code:
unsigned int maxSequenceNumber(struct Window *window)
{
  unsigned int max_window_index = 0;
  int i = 0;
  for (i = 0; i < window->window_size; i++) {
    if(window->registry[i] == 1) {
      max_window_index = i;
    }
  }
  return window->base_seq_num + max_window_index;
}

return_type: unsigned int


nextOpenSequenceNumber

decompiled_code:
__int64 __fastcall nextOpenSequenceNumber(__int64 a1)
{
int v1; // ecx
_BYTE *v2; // rdx
int v3; // eax

v1 = *(_DWORD *)(a1 + 16);
if ( v1 )
{
v2 = *(_BYTE **)a1;
v3 = 0;
do
{
v1 = v3;
if ( !*v2 )
break;
v1 = ++v3;
++v2;
}
while ( v3 != *(_DWORD *)(a1 + 16) );
}
return (unsigned int)(*(_DWORD *)(a1 + 32) + v1);
}

original_code:
uint32_t nextOpenSequenceNumber(Window *window)
{
  int i;
  for (i = 0; i < window->window_size; i++) {
    if(window->registry[i] == 0) {
      break;
    }
  }
  return window->base_seq_num + i;
}

canonical_original_code:
unsigned int nextOpenSequenceNumber(struct Window *window)
{
  int i;
  for (i = 0; i < window->window_size; i++) {
    if(window->registry[i] == 0) {
      break;
    }
  }
  return window->base_seq_num + i;
}


destroyWindow

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

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

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

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

original_code:
void destroyWindow(Window *window)
{
  if (window != ((void *)0)) {
    if (window->buffer != ((void *)0))
      free(window->buffer);
    if(window->registry != ((void *)0))
      free(window->registry);
    free(window);
  }
}

canonical_original_code:
void destroyWindow(struct Window *window)
{
  if (window != ((void *)0)) {
    if (window->buffer != ((void *)0))
      free(window->buffer);
    if(window->registry != ((void *)0))
      free(window->registry);
    free(window);
  }
}


original_code:
int32_t select_call(int32_t socket_num, int32_t seconds, int32_t microseconds, int32_t set_null)
{
  fd_set fdvar;
  struct timeval * timeout = ((void *)0);
  if (set_null == NOT_NULL) {
    timeout = (struct timeval *) malloc(sizeof(struct timeval));
    timeout->tv_sec = seconds;
    timeout->tv_usec = microseconds;
  }
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdvar)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fdvar)->__fds_bits)[((socket_num) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_num) % (8 * (int) sizeof (__fd_mask)))))));
  if (selectMod(socket_num+1,(fd_set *) &fdvar, (fd_set *) 0, (fd_set *) 0, timeout) < 0) {
    perror("select");
    exit(-1);
  }
  if (((((&fdvar)->__fds_bits)[((socket_num) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_num) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 1;
  }
  return 0;
}

canonical_original_code:
int select_call(int socket_num, int seconds, int microseconds, int set_null)
{
  struct fd_set fdvar;
  struct timeval *timeout = ((void *)0);
  if (set_null == NOT_NULL) {
    timeout = (struct timeval *) malloc(sizeof(struct timeval));
    timeout->tv_sec = seconds;
    timeout->tv_usec = microseconds;
  }
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdvar)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fdvar)->__fds_bits)[((socket_num) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_num) % (8 * (int) sizeof (__fd_mask)))))));
  if (selectMod(socket_num+1,(struct fd_set *) &fdvar, (struct fd_set *) 0, (struct fd_set *) 0, timeout) < 0) {
    perror("select");
    exit(-1);
  }
  if (((((&fdvar)->__fds_bits)[((socket_num) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_num) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 1;
  }
  return 0;
}

variable_types: seconds int

variable_types: microseconds int

variable_types:
fdvar
struct fd_set { long __fds_bits[-1]; }

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


decompiled_code:
__int64 __fastcall recv_buf(void *dest, unsigned int a2, unsigned int a3, __int64 a4, _BYTE *a5, _DWORD *a6)
{
int v9; // eax
int v10; // ebx
int v12; // [rsp+Ch] [rbp-82Ch] BYREF
unsigned int v13; // [rsp+10h] [rbp-828h] BYREF
char v14; // [rsp+16h] [rbp-822h]
char src[2081]; // [rsp+17h] [rbp-821h] BYREF

v12 = 16;
v9 = recvfromErr(a3, &v13, a2, 0LL, a4 + 4, &v12);
if ( v9 < 0 )
{
perror("recv_buf:recvfrom");
exit(1);
}
v10 = v9;
*a6 = _bswap_32(v13);
*a5 = v14;
if ( v10 > 7 )
memcpy(dest, src, v10 - 8);
if ( (unsigned __int16)in_cksum(&v13, (unsigned int)v10) )
return 0xFFFFFFFFLL;
*(_DWORD *)(a4 + 20) = v12;
return (unsigned int)(v10 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func10(void *dest, unsigned int a2, unsigned int a3, __int64 a4, _BYTE *a5, _DWORD *a6)
{
int v9; // eax
int v10; // ebx
int v12; // [rsp+Ch] [rbp-82Ch] BYREF
unsigned int v13; // [rsp+10h] [rbp-828h] BYREF
char v14; // [rsp+16h] [rbp-822h]
char src[2081]; // [rsp+17h] [rbp-821h] BYREF

v12 = 16;
v9 = recvfromErr(a3, &v13, a2, 0LL, a4 + 4, &v12);
if ( v9 < 0 )
{
perror("recv_buf:recvfrom");
exit(1);
}
v10 = v9;
*a6 = func1(v13);
*a5 = v14;
if ( v10 > 7 )
memcpy(dest, src, v10 - 8);
if ( (unsigned __int16)in_cksum(&v13, (unsigned int)v10) )
return 0xFFFFFFFFLL;
*(_DWORD *)(a4 + 20) = v12;
return (unsigned int)(v10 - 8);
}

original_code:
int32_t recv_buf(uint8_t *buf, uint32_t len, int32_t recv_sock, Connection *conn, uint8_t *flag, uint32_t *seq_num)
{
  char data_buf[2048];
  int32_t recv_len = 0;
  uint32_t remote_len = sizeof(struct sockaddr_in);
  if ((recv_len = recvfromErr(recv_sock, data_buf, len, 0, (struct sockaddr *) &(conn->remote), &remote_len)) < 0) {
    perror("recv_buf:recvfrom");
    exit(1);
  }
   memcpy(seq_num, data_buf, 4);
  *seq_num = ntohl(*seq_num);
  *flag = data_buf[6];
  if (recv_len > 7) {
    memcpy(buf, &data_buf[7], recv_len-8);
  }
  uint16_t checksum = 0;
  memcpy(&checksum, &data_buf[4], 2);
  if (in_cksum((unsigned short *)data_buf, recv_len) != 0) {
    return CRC_ERROR;
  }
  conn->len = remote_len;
  return (recv_len - 8);
}

canonical_original_code:
int recv_buf(unsigned char *buf, unsigned int len, int recv_sock, struct Connection *conn, unsigned char *flag, unsigned int *seq_num)
{
  char data_buf[2048];
  int recv_len = 0;
  unsigned int remote_len = sizeof(struct sockaddr_in);
  if ((recv_len = recvfromErr(recv_sock, data_buf, len, 0, (struct sockaddr *) &(conn->remote), &remote_len)) < 0) {
    perror("recv_buf:recvfrom");
    exit(1);
  }
   memcpy(seq_num, data_buf, 4);
  *seq_num = ntohl(*seq_num);
  *flag = data_buf[6];
  if (recv_len > 7) {
    memcpy(buf, &data_buf[7], recv_len-8);
  }
  unsigned short checksum = 0;
  memcpy(&checksum, &data_buf[4], 2);
  if (in_cksum((unsigned short *)data_buf, recv_len) != 0) {
    return CRC_ERROR;
  }
  conn->len = remote_len;
  return (recv_len - 8);
}

variable_types:
buf
unsigned char *

variable_types: struct Connection { int socket_num; 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]; } remote; unsigned int len; } *

variable_types:
flag
unsigned char *

variable_types:
checksum
unsigned short

user_defined_types: struct Connection { int socket_num; struct sockaddr_in remote; unsigned int len; }

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

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


decompiled_code:
__int64 __fastcall send_buf(void *src, size_t n, unsigned int *a3, char a4, unsigned int a5)
{
__int64 result; // rax
int v9; // [rsp+0h] [rbp-828h] BYREF
__int16 v10; // [rsp+4h] [rbp-824h]
char v11; // [rsp+6h] [rbp-822h]
char dest[2081]; // [rsp+7h] [rbp-821h] BYREF

if ( (_DWORD)n )
memcpy(dest, src, (unsigned int)n);
v9 = _bswap_32(a5);
v11 = a4;
v10 = 0;
v10 = in_cksum(&v9, (unsigned int)(n + 8));
result = sendtoErr(*a3, &v9, (unsigned int)(n + 8), 0LL, a3 + 1, a3[5]);
if ( (int)result < 0 )
{
perror("send_buf:sendto");
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(void *src, size_t n, unsigned int *a3, char a4, unsigned int a5)
{
__int64 result; // rax
int v9; // [rsp+0h] [rbp-828h] BYREF
__int16 v10; // [rsp+4h] [rbp-824h]
char v11; // [rsp+6h] [rbp-822h]
char dest[2081]; // [rsp+7h] [rbp-821h] BYREF

if ( (_DWORD)n )
memcpy(dest, src, (unsigned int)n);
v9 = func1(a5);
v11 = a4;
v10 = 0;
v10 = in_cksum(&v9, (unsigned int)(n + 8));
result = sendtoErr(*a3, &v9, (unsigned int)(n + 8), 0LL, a3 + 1, a3[5]);
if ( (int)result < 0 )
{
perror("send_buf:sendto");
exit(1);
}
return result;
}

original_code:
int32_t send_buf(uint8_t *buf, uint32_t len, Connection *conn, uint8_t flag, uint32_t seq_num)
{
  uint8_t packet[2048];
  int32_t send_len = 0;
  uint16_t checksum = 0;
  if (len > 0) {
    memcpy(&packet[7], buf, len);
  }
  seq_num = htonl(seq_num);
  memcpy(packet, &seq_num, 4);
  packet[6] = flag;
  memset(&packet[4], 0, 2);
  checksum = in_cksum((unsigned short *)packet, len+8);
  memcpy(&packet[4], &checksum, 2);
  if ((send_len = sendtoErr(conn->socket_num, packet, len+8, 0, (struct sockaddr *)&conn->remote, conn->len)) < 0) {
    perror("send_buf:sendto");
    exit(1);
  }
  return send_len;
}

canonical_original_code:
int send_buf(unsigned char *buf, unsigned int len, struct Connection *conn, unsigned char flag, unsigned int seq_num)
{
  unsigned char packet[2048];
  int send_len = 0;
  unsigned short checksum = 0;
  if (len > 0) {
    memcpy(&packet[7], buf, len);
  }
  seq_num = htonl(seq_num);
  memcpy(packet, &seq_num, 4);
  packet[6] = flag;
  memset(&packet[4], 0, 2);
  checksum = in_cksum((unsigned short *)packet, len+8);
  memcpy(&packet[4], &checksum, 2);
  if ((send_len = sendtoErr(conn->socket_num, packet, len+8, 0, (struct sockaddr *)&conn->remote, conn->len)) < 0) {
    perror("send_buf:sendto");
    exit(1);
  }
  return send_len;
}

variable_types:
flag
unsigned char

variable_types:
packet
unsigned char [2048]


startServer

decompiled_code:
__int64 startServer()
{
int v0; // eax
unsigned int v1; // ebx
unsigned __int16 v2; // ax
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

len = 16;
v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
perror("socket");
exit(-1);
}
v1 = v0;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = _bswap_16(0);
if ( (int)bindMod(v1, &addr, 16LL) < 0 )
{
perror("udp_server, bind");
exit(-1);
}
getsockname(v1, &addr, &len);
v2 = _bswap_16(*(__int16 *)addr.sa_data);
printf("Using Port #: %d\n", v2);
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax
unsigned int v1; // ebx
unsigned __int16 v2; // ax
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

len = 16;
v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
perror("socket");
exit(-1);
}
v1 = v0;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = func0(0);
if ( (int)bindMod(v1, &addr, 16LL) < 0 )
{
perror("udp_server, bind");
exit(-1);
}
getsockname(v1, &addr, &len);
v2 = func0(*(__int16 *)addr.sa_data);
printf("Using Port #: %d\n", v2);
return v1;
}

original_code:
int32_t startServer()
{
  int sk = 0;
  struct sockaddr_in local ;
  uint32_t len = sizeof(local) ;
  if ((sk = socket(2,SOCK_DGRAM,0)) < 0) {
    perror("socket");
    exit(-1);
  }
  local.sin_family = 2 ;
  local.sin_addr.s_addr = ((in_addr_t) 0x00000000) ;
  local.sin_port = htons(0);
  if (bindMod(sk,(struct sockaddr *)&local,sizeof(local)) < 0)
    {
      perror("udp_server, bind");
      exit(-1);
    }
  getsockname(sk,(struct sockaddr *)&local, &len) ;
  printf("Using Port #: %d\n", ntohs(local.sin_port));
  return(sk);
}

canonical_original_code:
int startServer()
{
  int sk = 0;
  struct sockaddr_in local;
  unsigned int len = sizeof(local);
  if ((sk = socket(2,SOCK_DGRAM,0)) < 0) {
    perror("socket");
    exit(-1);
  }
  local.sin_family = 2 ;
  local.sin_addr.s_addr = ((unsigned int) 0x00000000) ;
  local.sin_port = htons(0);
  if (bindMod(sk,(struct sockaddr *)&local,sizeof(local)) < 0)
    {
      perror("udp_server, bind");
      exit(-1);
    }
  getsockname(sk,(struct sockaddr *)&local, &len) ;
  printf("Using Port #: %d\n", ntohs(local.sin_port));
  return(sk);
}

variable_types: sk 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:
len
unsigned int


connectToServer

decompiled_code:
__int64 __fastcall connectToServer(char *name, __int16 a2, __int64 a3)
{
int v4; // eax
struct hostent *v5; // rax
__int64 result; // rax

*(_DWORD *)a3 = 0;
*(_DWORD *)(a3 + 20) = 16;
v4 = socket(2, 2, 0);
*(_DWORD *)a3 = v4;
if ( v4 < 0 )
{
perror("udp_client_setup, socket");
exit(-1);
}
*(_WORD *)(a3 + 4) = 2;
v5 = gethostbyname(name);
if ( v5 )
{
memcpy((void *)(a3 + 8), *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)(a3 + 6) = _bswap_16(a2);
result = 0LL;
}
else
{
printf("Host not found: %s\n", name);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *name, __int16 a2, __int64 a3)
{
int v4; // eax
struct hostent *v5; // rax
__int64 result; // rax

*(_DWORD *)a3 = 0;
*(_DWORD *)(a3 + 20) = 16;
v4 = socket(2, 2, 0);
*(_DWORD *)a3 = v4;
if ( v4 < 0 )
{
perror("udp_client_setup, socket");
exit(-1);
}
*(_WORD *)(a3 + 4) = 2;
v5 = gethostbyname(name);
if ( v5 )
{
memcpy((void *)(a3 + 8), *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)(a3 + 6) = func0(a2);
result = 0LL;
}
else
{
printf("Host not found: %s\n", name);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int32_t connectToServer(char * hostname, uint16_t port_num, Connection * connection)
{
  struct hostent * hp = ((void *)0) ;
  connection->socket_num = 0;
  connection->len = sizeof(struct sockaddr_in);
  if ((connection->socket_num = socket(2,SOCK_DGRAM,0)) < 0)
    {
      perror("udp_client_setup, socket");
      exit(-1);
    }
  connection->remote.sin_family = 2 ;
  hp = gethostbyname(hostname);
  if (hp == ((void *)0))
    {
      printf("Host not found: %s\n", hostname);
      return -1;
    }
  memcpy(&(connection->remote.sin_addr),hp->h_addr_list[0],hp->h_length) ;
  connection->remote.sin_port = htons(port_num);
  return 0;
}

canonical_original_code:
int connectToServer(char *hostname, unsigned short port_num, struct Connection *connection)
{
  struct hostent *hp = ((void *)0);
  connection->socket_num = 0;
  connection->len = sizeof(struct sockaddr_in);
  if ((connection->socket_num = socket(2,SOCK_DGRAM,0)) < 0)
    {
      perror("udp_client_setup, socket");
      exit(-1);
    }
  connection->remote.sin_family = 2 ;
  hp = gethostbyname(hostname);
  if (hp == ((void *)0))
    {
      printf("Host not found: %s\n", hostname);
      return -1;
    }
  memcpy(&(connection->remote.sin_addr),hp->h_addr_list[0],hp->h_length) ;
  connection->remote.sin_port = htons(port_num);
  return 0;
}

variable_types:
hostname
char *

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


decompiled_code:
__int64 __fastcall send_next_data(__int64 a1, __int64 a2)
{
int v4; // er8
__int64 v5; // rsi
unsigned int v6; // edi
unsigned int v7; // eax
unsigned int v8; // ebp

v4 = *(_DWORD *)(a2 + 36);
v5 = *(unsigned int *)(a2 + 20);
v6 = v5 * v4;
v7 = *(_DWORD *)(a2 + 28) - v5 * v4;
v8 = 7;
if ( (unsigned int)v5 >= v7 )
{
v5 = v7;
v8 = 4;
}
send_buf(*(_QWORD *)(a2 + 8) + v6, v5, a1, 1LL, (unsigned int)(*(_DWORD *)(a2 + 32) + v4));
++*(_DWORD *)(a2 + 36);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
int v4; // er8
__int64 v5; // rsi
unsigned int v6; // edi
unsigned int v7; // eax
unsigned int v8; // ebp

v4 = *(_DWORD *)(a2 + 36);
v5 = *(unsigned int *)(a2 + 20);
v6 = v5 * v4;
v7 = *(_DWORD *)(a2 + 28) - v5 * v4;
v8 = 7;
if ( (unsigned int)v5 >= v7 )
{
v5 = v7;
v8 = 4;
}
send_buf(*(_QWORD *)(a2 + 8) + v6, v5, a1, 1LL, (unsigned int)(*(_DWORD *)(a2 + 32) + v4));
++*(_DWORD *)(a2 + 36);
return v8;
}

original_code:
STATE send_next_data(Connection *client, Window *window)
{
 STATE state_to_return = STATE_READ_ACKS;
 uint32_t window_buffer_offset = window->window_index * window->block_size;
 uint32_t packet_len = window->block_size;
 int32_t data_left = window->data_len - window_buffer_offset;
 if (data_left <= window->block_size) {
  packet_len = data_left;
  state_to_return = STATE_WINDOW_CLOSE;
 }
 send_buf(window->buffer + window_buffer_offset, packet_len, client, FLAG_DATA, window->base_seq_num + window->window_index);
 window->window_index++;
 return state_to_return;
}

canonical_original_code:
enum STATE send_next_data(struct Connection *client, struct Window *window)
{
 enum STATE state_to_return = STATE_READ_ACKS;
 unsigned int window_buffer_offset = window->window_index * window->block_size;
 unsigned int packet_len = window->block_size;
 int data_left = window->data_len - window_buffer_offset;
 if (data_left <= window->block_size) {
  packet_len = data_left;
  state_to_return = STATE_WINDOW_CLOSE;
 }
 send_buf(window->buffer + window_buffer_offset, packet_len, client, FLAG_DATA, window->base_seq_num + window->window_index);
 window->window_index++;
 return state_to_return;
}

variable_types: struct Window { unsigned char *registry; unsigned char *buffer; unsigned int window_size; unsigned int block_size; unsigned int buffer_size; int data_len; unsigned int base_seq_num; unsigned int window_index; } *

variable_types: enum STATE { STATE_START=0, STATE_DONE=1, STATE_FILENAME=2, STATE_WINDOW_NEXT=3, STATE_WINDOW_CLOSE=4, STATE_SEND_DATA=5, STATE_SEND_EOF=6, STATE_READ_ACKS=7, STATE_READ_EOF_ACK=8 }


decompiled_code:
__int64 __fastcall send_data(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v3; // r8
unsigned int v4; // ecx
unsigned int v5; // edx
unsigned int v6; // eax

v3 = a3;
v4 = *(_DWORD *)(a2 + 20);
v5 = v4 * ((a3 - 1) % *(_DWORD *)(a2 + 16));
v6 = *(_DWORD *)(a2 + 24) - v5;
if ( v6 > v4 )
v6 = *(_DWORD *)(a2 + 20);
return send_buf(*(_QWORD *)(a2 + 8) + v5, v6, a1, 2LL, v3);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v3; // r8
unsigned int v4; // ecx
unsigned int v5; // edx
unsigned int v6; // eax

v3 = a3;
v4 = *(_DWORD *)(a2 + 20);
v5 = v4 * ((a3 - 1) % *(_DWORD *)(a2 + 16));
v6 = *(_DWORD *)(a2 + 24) - v5;
if ( v6 > v4 )
v6 = *(_DWORD *)(a2 + 20);
return send_buf(*(_QWORD *)(a2 + 8) + v5, v6, a1, 2LL, v3);
}

original_code:
void send_data(Connection *client, Window *window, uint32_t seq_num)
{
 uint32_t window_index = (seq_num-1) % window->window_size;
 uint32_t window_buffer_offset = window_index * window->block_size;
 uint32_t packet_len = window->block_size;
 int32_t data_left = window->buffer_size - window_buffer_offset;
 if (data_left < window->block_size) {
  packet_len = data_left;
 }
 send_buf(window->buffer + window_buffer_offset, packet_len, client, FLAG_DATA_RESENT, seq_num);
}

canonical_original_code:
void send_data(struct Connection *client, struct Window *window, unsigned int seq_num)
{
 unsigned int window_index = (seq_num-1) % window->window_size;
 unsigned int window_buffer_offset = window_index * window->block_size;
 unsigned int packet_len = window->block_size;
 int data_left = window->buffer_size - window_buffer_offset;
 if (data_left < window->block_size) {
  packet_len = data_left;
 }
 send_buf(window->buffer + window_buffer_offset, packet_len, client, FLAG_DATA_RESENT, seq_num);
}


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

v2 = read(a2, *(void **)(a1 + 8), *(unsigned int *)(a1 + 24));
clearWindow(a1);
*(_DWORD *)(a1 + 36) = 0;
if ( v2 == -1 )
{
perror("send_data: read");
exit(1);
}
result = 6LL;
if ( v2 )
{
*(_DWORD *)(a1 + 28) = v2;
result = 5LL;
}
return result;
}

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

v2 = read(a2, *(void **)(a1 + 8), *(unsigned int *)(a1 + 24));
clearWindow(a1);
*(_DWORD *)(a1 + 36) = 0;
if ( v2 == -1 )
{
perror("send_data: read");
exit(1);
}
result = 6LL;
if ( v2 )
{
*(_DWORD *)(a1 + 28) = v2;
result = 5LL;
}
return result;
}

original_code:
STATE next_window(Window *window, int data_file)
{
 int32_t len_read = 0;
 len_read = read(data_file, window->buffer, window->buffer_size);
 clearWindow(window);
 window->window_index = 0;
 switch (len_read) {
  case -1:
   perror("send_data: read");
   exit(1);
   break;
  case 0:
   return STATE_SEND_EOF;
   break;
  default:
   window->data_len = len_read;
   break;
 }
 return STATE_SEND_DATA;
}

canonical_original_code:
enum STATE next_window(struct Window *window, int data_file)
{
 int len_read = 0;
 len_read = read(data_file, window->buffer, window->buffer_size);
 clearWindow(window);
 window->window_index = 0;
 switch (len_read) {
  case -1:
   perror("send_data: read");
   exit(1);
   break;
  case 0:
   return STATE_SEND_EOF;
   break;
  default:
   window->data_len = len_read;
   break;
 }
 return STATE_SEND_DATA;
}

return_type: enum STATE { STATE_START=0, STATE_DONE=1, STATE_FILENAME=2, STATE_WINDOW_NEXT=3, STATE_WINDOW_CLOSE=4, STATE_SEND_DATA=5, STATE_SEND_EOF=6, STATE_READ_ACKS=7, STATE_READ_EOF_ACK=8 }


decompiled_code:
__int64 __fastcall close_window(unsigned int *a1, __int64 a2)
{
int v2; // er13
int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // ecx
__int64 v6; // rcx
__int64 v7; // rax
unsigned int v9; // [rsp+8h] [rbp-830h] BYREF
char v10; // [rsp+Fh] [rbp-829h] BYREF
char v11[2088]; // [rsp+10h] [rbp-828h] BYREF

v10 = 0;
v9 = 0;
v2 = 0;
while ( 1 )
{
v3 = windowIsFull(a2);
if ( v3 )
break;
if ( (unsigned int)select_call(*a1, 1LL, 0LL, 1LL) )
{
if ( (unsigned int)recv_buf(v11, 2048LL, *a1, a1, &v10, &v9) != -1 )
{
v4 = *(_DWORD *)(a2 + 32);
if ( v4 <= v9 )
{
v5 = *(_DWORD *)(a2 + 16);
if ( v9 < v5 + v4 )
{
if ( v10 == 3 )
{
v6 = (v9 - 1) % v5 + 1;
v7 = 0LL;
do
*(_BYTE *)(*(_QWORD *)a2 + v7++) = 1;
while ( v7 != v6 );
v2 = v3;
}
else if ( v10 == 4 )
{
send_data((__int64)a1, a2, v9);
v2 = v3;
}
else
{
fwrite("ERROR: non-ACK flag in wait_on_ack\n", 1uLL, 0x23uLL, stderr);
}
}
}
}
}
else
{
if ( ++v2 > 10 )
{
fwrite("ERROR: No Response from client in 10 seconds", 1uLL, 0x2CuLL, stderr);
exit(1);
}
send_data((__int64)a1, a2, *(_DWORD *)(a2 + 32));
}
}
*(_DWORD *)(a2 + 32) = nextOpenSequenceNumber(a2);
return 3LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, __int64 a2)
{
int v2; // er13
int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // ecx
__int64 v6; // rcx
__int64 v7; // rax
unsigned int v9; // [rsp+8h] [rbp-830h] BYREF
char v10; // [rsp+Fh] [rbp-829h] BYREF
char v11[2088]; // [rsp+10h] [rbp-828h] BYREF

v10 = 0;
v9 = 0;
v2 = 0;
while ( 1 )
{
v3 = windowIsFull(a2);
if ( v3 )
break;
if ( (unsigned int)select_call(*a1, 1LL, 0LL, 1LL) )
{
if ( (unsigned int)recv_buf(v11, 2048LL, *a1, a1, &v10, &v9) != -1 )
{
v4 = *(_DWORD *)(a2 + 32);
if ( v4 <= v9 )
{
v5 = *(_DWORD *)(a2 + 16);
if ( v9 < v5 + v4 )
{
if ( v10 == 3 )
{
v6 = (v9 - 1) % v5 + 1;
v7 = 0LL;
do
*(_BYTE *)(*(_QWORD *)a2 + v7++) = 1;
while ( v7 != v6 );
v2 = v3;
}
else if ( v10 == 4 )
{
func3((__int64)a1, a2, v9);
v2 = v3;
}
else
{
fwrite("ERROR: non-ACK flag in wait_on_ack\n", 1uLL, 0x23uLL, stderr);
}
}
}
}
}
else
{
if ( ++v2 > 10 )
{
fwrite("ERROR: No Response from client in 10 seconds", 1uLL, 0x2CuLL, stderr);
exit(1);
}
func3((__int64)a1, a2, *(_DWORD *)(a2 + 32));
}
}
*(_DWORD *)(a2 + 32) = nextOpenSequenceNumber(a2);
return 3LL;
}

original_code:
STATE close_window(Connection *client, Window *window, int nest_level)
{
 uint32_t recv_len = 0;
 uint8_t buf[2048];
 uint8_t flag = 0;
 uint32_t seq_num = 0;
 int send_count = 0;
 uint32_t window_index = 0;
 int i;
 for (;;) {
  if (windowIsFull(window)) {
   window->base_seq_num = nextOpenSequenceNumber(window);
   return STATE_WINDOW_NEXT;
  }
  if(select_call(client->socket_num, 1, 0, NOT_NULL)) {
   recv_len = recv_buf(buf, 2048, client->socket_num, client, &flag, &seq_num);
   if (recv_len == CRC_ERROR) {
    continue;
   }
   if ((seq_num >= window->base_seq_num) && (seq_num < (window->base_seq_num + window->window_size))) {
    window_index = (seq_num-1) % window->window_size;
    switch (flag) {
     case FLAG_ACK:
      send_count = 0;
      for (i= 0; i <= window_index; i++) {
       window->registry[i] = 1;
      }
      break;
     case FLAG_SREJ:
      send_count = 0;
      send_data(client, window, seq_num);
      break;
     default:
      fprintf(stderr, "ERROR: non-ACK flag in wait_on_ack\n");
      break;
    }
   }
  }
  else {
   send_count++;
   if (send_count > 10) {
    fprintf(stderr, "ERROR: No Response from client in 10 seconds");
    exit(1);
   }
   send_data(client, window, window->base_seq_num);
  }
 }
 return STATE_DONE;
}

canonical_original_code:
enum STATE close_window(struct Connection *client, struct Window *window, int nest_level)
{
 unsigned int recv_len = 0;
 unsigned char buf[2048];
 unsigned char flag = 0;
 unsigned int seq_num = 0;
 int send_count = 0;
 unsigned int window_index = 0;
 int i;
 for (;;) {
  if (windowIsFull(window)) {
   window->base_seq_num = nextOpenSequenceNumber(window);
   return STATE_WINDOW_NEXT;
  }
  if(select_call(client->socket_num, 1, 0, NOT_NULL)) {
   recv_len = recv_buf(buf, 2048, client->socket_num, client, &flag, &seq_num);
   if (recv_len == CRC_ERROR) {
    continue;
   }
   if ((seq_num >= window->base_seq_num) && (seq_num < (window->base_seq_num + window->window_size))) {
    window_index = (seq_num-1) % window->window_size;
    switch (flag) {
     case FLAG_ACK:
      send_count = 0;
      for (i= 0; i <= window_index; i++) {
       window->registry[i] = 1;
      }
      break;
     case FLAG_SREJ:
      send_count = 0;
      send_data(client, window, seq_num);
      break;
     default:
      fprintf(stderr, "ERROR: non-ACK flag in wait_on_ack\n");
      break;
    }
   }
  }
  else {
   send_count++;
   if (send_count > 10) {
    fprintf(stderr, "ERROR: No Response from client in 10 seconds");
    exit(1);
   }
   send_data(client, window, window->base_seq_num);
  }
 }
 return STATE_DONE;
}

variable_types:
buf
unsigned char [2048]

variable_types: i int

user_defined_types: enum STATE { STATE_START=0, STATE_DONE=1, STATE_FILENAME=2, STATE_WINDOW_NEXT=3, STATE_WINDOW_CLOSE=4, STATE_SEND_DATA=5, STATE_SEND_EOF=6, STATE_READ_ACKS=7, STATE_READ_EOF_ACK=8 }


decompiled_code:
__int64 __fastcall send_eof(unsigned int *a1, __int64 a2)
{
int v2; // er12
int v4; // [rsp+8h] [rbp-820h] BYREF
unsigned __int8 v5; // [rsp+Fh] [rbp-819h] BYREF
char v6[2072]; // [rsp+10h] [rbp-818h] BYREF

v5 = 0;
v4 = 0;
v2 = 10;
LABEL_9:
send_buf(0LL, 0LL, a1, 9LL, *(unsigned int *)(a2 + 32));
do
{
while ( 1 )
{
do
{
if ( !(unsigned int)select_call(*a1, 1LL, 0LL, 1LL) )
{
if ( !--v2 )
{
fwrite("ERROR: 10 failed EOF tries. Bailing\n", 1uLL, 0x24uLL, stderr);
exit(1);
}
goto LABEL_9;
}
}
while ( (unsigned int)recv_buf(v6, 2048LL, *a1, a1, &v5, &v4) == -1 );
if ( (unsigned int)v5 - 3 <= 1 )
break;
fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", v5);
}
}
while ( *(_DWORD *)(a2 + 32) != v4 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1, __int64 a2)
{
int v2; // er12
int v4; // [rsp+8h] [rbp-820h] BYREF
unsigned __int8 v5; // [rsp+Fh] [rbp-819h] BYREF
char v6[2072]; // [rsp+10h] [rbp-818h] BYREF

v5 = 0;
v4 = 0;
v2 = 10;
LABEL_9:
send_buf(0LL, 0LL, a1, 9LL, *(unsigned int *)(a2 + 32));
do
{
while ( 1 )
{
do
{
if ( !(unsigned int)select_call(*a1, 1LL, 0LL, 1LL) )
{
if ( !--v2 )
{
fwrite("ERROR: 10 failed EOF tries. Bailing\n", 1uLL, 0x24uLL, stderr);
exit(1);
}
goto LABEL_9;
}
}
while ( (unsigned int)recv_buf(v6, 2048LL, *a1, a1, &v5, &v4) == -1 );
if ( (unsigned int)v5 - 3 <= 1 )
break;
fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", v5);
}
}
while ( *(_DWORD *)(a2 + 32) != v4 );
return 1LL;
}

original_code:
STATE send_eof(Connection *client, Window *window)
{
 uint32_t recv_len = 0;
 uint8_t buf[2048];
 uint8_t flag = 0;
 uint32_t seq_num = 0;
 int select_count = 0;
 for (;;) {
  send_buf(((void *)0), 0, client, FLAG_END_OF_FILE, window->base_seq_num);
  select_count++;
  while(select_call(client->socket_num, 1, 0, NOT_NULL)) {
   recv_len = recv_buf(buf, 2048, client->socket_num, client, &flag, &seq_num);
   if (recv_len == CRC_ERROR) {
    continue;
   }
   switch (flag) {
    case FLAG_SREJ:
    case FLAG_ACK:
     if (seq_num == window->base_seq_num) {
      return STATE_DONE;
     }
     break;
    default:
     fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", flag);
     break;
   }
  }
  if (select_count >= 10) {
   fprintf(stderr, "ERROR: 10 failed EOF tries. Bailing\n");
   exit(1);
  }
 }
 return STATE_DONE;
}

canonical_original_code:
enum STATE send_eof(struct Connection *client, struct Window *window)
{
 unsigned int recv_len = 0;
 unsigned char buf[2048];
 unsigned char flag = 0;
 unsigned int seq_num = 0;
 int select_count = 0;
 for (;;) {
  send_buf(((void *)0), 0, client, FLAG_END_OF_FILE, window->base_seq_num);
  select_count++;
  while(select_call(client->socket_num, 1, 0, NOT_NULL)) {
   recv_len = recv_buf(buf, 2048, client->socket_num, client, &flag, &seq_num);
   if (recv_len == CRC_ERROR) {
    continue;
   }
   switch (flag) {
    case FLAG_SREJ:
    case FLAG_ACK:
     if (seq_num == window->base_seq_num) {
      return STATE_DONE;
     }
     break;
    default:
     fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", flag);
     break;
   }
  }
  if (select_count >= 10) {
   fprintf(stderr, "ERROR: 10 failed EOF tries. Bailing\n");
   exit(1);
  }
 }
 return STATE_DONE;
}


decompiled_code:
void __fastcall process_client(unsigned int *a1, int a2, int *a3, int a4)
{
int v6; // eax
__int64 v7; // [rsp+0h] [rbp-38h] BYREF
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v8[0] = 0;
v7 = 0LL;
if ( a4 <= 9 )
{
v6 = 0;
while ( 2 )
{
switch ( v6 )
{
case 0:
case 2:
v6 = filename(a3, a1, a2, v8, (void **)&v7);
goto LABEL_4;
case 3:
v6 = next_window(v7, v8[0]);
goto LABEL_4;
case 4:
v6 = close_window((unsigned int *)a3, v7);
goto LABEL_4;
case 5:
v6 = send_next_data((__int64)a3, v7);
goto LABEL_4;
case 6:
v6 = send_eof((unsigned int *)a3, v7);
goto LABEL_4;
case 7:
v6 = read_acks(a3, v7, (unsigned int)a4);
LABEL_4:
if ( v6 != 1 )
continue;
goto LABEL_11;
default:
LABEL_11:
if ( v7 )
destroyWindow();
break;
}
break;
}
}
}

canonical_decompiled_code:
void __fastcall func7(unsigned int *a1, int a2, int *a3, int a4)
{
int v6; // eax
__int64 v7; // [rsp+0h] [rbp-38h] BYREF
int v8[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v8[0] = 0;
v7 = 0LL;
if ( a4 <= 9 )
{
v6 = 0;
while ( 2 )
{
switch ( v6 )
{
case 0:
case 2:
v6 = func1(a3, a1, a2, v8, (void **)&v7);
goto LABEL_4;
case 3:
v6 = func4(v7, v8[0]);
goto LABEL_4;
case 4:
v6 = func5((unsigned int *)a3, v7);
goto LABEL_4;
case 5:
v6 = func2((__int64)a3, v7);
goto LABEL_4;
case 6:
v6 = func6((unsigned int *)a3, v7);
goto LABEL_4;
case 7:
v6 = func9(a3, v7, (unsigned int)a4);
LABEL_4:
if ( v6 != 1 )
continue;
goto LABEL_11;
default:
LABEL_11:
if ( v7 )
destroyWindow();
break;
}
break;
}
}
}

original_code:
void process_client(uint8_t *buf, int32_t recv_len, Connection *client, int nest_level)
{
 STATE state = STATE_START;
 int32_t data_file = 0;
 Window *window = ((void *)0);
 if (nest_level < 10) {
  while (state != STATE_DONE) {
   switch (state) {
    case STATE_START:
     state = STATE_FILENAME;
    case STATE_FILENAME:
     state = filename(client, buf, recv_len, &data_file, &window);
     break;
    case STATE_WINDOW_NEXT:
     state = next_window(window, data_file);
     break;
    case STATE_WINDOW_CLOSE:
     state = close_window(client, window, nest_level);
     break;
    case STATE_SEND_DATA:
     state = send_next_data(client, window);
     break;
    case STATE_SEND_EOF:
     state = send_eof(client, window);
     break;
    case STATE_READ_ACKS:
     state = read_acks(client, window, nest_level);
     break;
    case STATE_DONE:
    default:
     state = STATE_DONE;
     break;
   }
  }
 }
 if (window != ((void *)0)) {
  destroyWindow(window);
  window = ((void *)0);
 }
}

canonical_original_code:
void process_client(unsigned char *buf, int recv_len, struct Connection *client, int nest_level)
{
 enum STATE state = STATE_START;
 int data_file = 0;
 struct Window *window = ((void *)0);
 if (nest_level < 10) {
  while (state != STATE_DONE) {
   switch (state) {
    case STATE_START:
     state = STATE_FILENAME;
    case STATE_FILENAME:
     state = filename(client, buf, recv_len, &data_file, &window);
     break;
    case STATE_WINDOW_NEXT:
     state = next_window(window, data_file);
     break;
    case STATE_WINDOW_CLOSE:
     state = close_window(client, window, nest_level);
     break;
    case STATE_SEND_DATA:
     state = send_next_data(client, window);
     break;
    case STATE_SEND_EOF:
     state = send_eof(client, window);
     break;
    case STATE_READ_ACKS:
     state = read_acks(client, window, nest_level);
     break;
    case STATE_DONE:
    default:
     state = STATE_DONE;
     break;
   }
  }
 }
 if (window != ((void *)0)) {
  destroyWindow(window);
  window = ((void *)0);
 }
}


decompiled_code:
void __noreturn server_runloop()
{
unsigned int v0; // ebx
int v1; // ebp
__pid_t v2; // eax
int v3[6]; // [rsp+0h] [rbp-848h] BYREF
char v4[7]; // [rsp+18h] [rbp-830h] BYREF
char v5; // [rsp+1Fh] [rbp-829h] BYREF
unsigned int v6[515]; // [rsp+20h] [rbp-828h] BYREF
int stat_loc[7]; // [rsp+82Ch] [rbp-1Ch] BYREF

v0 = startServer();
while ( 1 )
{
do
{
while ( (unsigned int)select_call(v0, 1LL, 0LL, 1LL) != 1 )
;
v1 = recv_buf(v6, 2048LL, v0, v3, &v5, v4);
}
while ( v1 == -1 );
v2 = fork();
if ( v2 < 0 )
{
perror("mainServerRunLoop:fork");
exit(1);
}
if ( !v2 )
{
process_client(v6, v1, v3, 0);
exit(0);
}
while ( waitpid(-1, stat_loc, 1) > 0 )
;
}
}

canonical_decompiled_code:
void __noreturn func8()
{
unsigned int v0; // ebx
int v1; // ebp
__pid_t v2; // eax
int v3[6]; // [rsp+0h] [rbp-848h] BYREF
char v4[7]; // [rsp+18h] [rbp-830h] BYREF
char v5; // [rsp+1Fh] [rbp-829h] BYREF
unsigned int v6[515]; // [rsp+20h] [rbp-828h] BYREF
int stat_loc[7]; // [rsp+82Ch] [rbp-1Ch] BYREF

v0 = startServer();
while ( 1 )
{
do
{
while ( (unsigned int)select_call(v0, 1LL, 0LL, 1LL) != 1 )
;
v1 = recv_buf(v6, 2048LL, v0, v3, &v5, v4);
}
while ( v1 == -1 );
v2 = fork();
if ( v2 < 0 )
{
perror("mainServerRunLoop:fork");
exit(1);
}
if ( !v2 )
{
func7(v6, v1, v3, 0);
exit(0);
}
while ( waitpid(-1, stat_loc, 1) > 0 )
;
}
}

original_code:
void server_runloop()
{
 pid_t pid = 0;
 int status;
 uint8_t buf[2048];
 uint8_t flag;
 uint32_t recv_len;
 uint32_t seq_num;
 Connection client;
 int serv_sock = startServer();
 for (;;) {
  if (select_call(serv_sock, 1, 0, NOT_NULL) == 1) {
   recv_len = recv_buf(buf, 2048, serv_sock, &client, &flag, &seq_num);
   if (recv_len != CRC_ERROR) {
    if ((pid = fork()) < 0) {
     perror("mainServerRunLoop:fork");
     exit(1);
    }
    if (pid == 0) {
     process_client(buf, recv_len, &client, 0);
     exit(0);
    }
    while (waitpid(-1, &status, 1) > 0) {
    }
   }
  }
 }
}

canonical_original_code:
void server_runloop()
{
 int pid = 0;
 int status;
 unsigned char buf[2048];
 unsigned char flag;
 unsigned int recv_len;
 unsigned int seq_num;
 struct Connection client;
 int serv_sock = startServer();
 for (;;) {
  if (select_call(serv_sock, 1, 0, NOT_NULL) == 1) {
   recv_len = recv_buf(buf, 2048, serv_sock, &client, &flag, &seq_num);
   if (recv_len != CRC_ERROR) {
    if ((pid = fork()) < 0) {
     perror("mainServerRunLoop:fork");
     exit(1);
    }
    if (pid == 0) {
     process_client(buf, recv_len, &client, 0);
     exit(0);
    }
    while (waitpid(-1, &status, 1) > 0) {
    }
   }
  }
 }
}

variable_types: pid int

variable_types: status int

variable_types: struct Connection { int socket_num; 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]; } remote; unsigned int len; }


decompiled_code:
__int64 __fastcall read_acks(int *a1, __int64 a2, int a3)
{
int v5; // eax
unsigned int v6; // edx
unsigned int v7; // esi
unsigned int v8; // edx
__int64 v9; // rax
unsigned int v11; // [rsp+8h] [rbp-820h] BYREF
unsigned __int8 v12; // [rsp+Fh] [rbp-819h] BYREF
unsigned int v13[518]; // [rsp+10h] [rbp-818h] BYREF

v12 = 0;
v11 = 0;
while ( 1 )
{
if ( !(unsigned int)select_call((unsigned int)*a1, 0LL, 0LL, 1LL) )
return 5LL;
v5 = recv_buf(v13, 2048LL, (unsigned int)*a1, a1, &v12, &v11);
if ( v5 == -1 )
break;
v6 = *(_DWORD *)(a2 + 32);
if ( v6 <= v11 )
{
v7 = *(_DWORD *)(a2 + 16);
if ( v11 < v7 + v6 )
{
switch ( v12 )
{
case 4u:
send_data((__int64)a1, a2, v11);
break;
case 6u:
process_client(v13, v5, a1, a3 + 1);
return 1LL;
case 3u:
v8 = (v11 - 1) % v7;
v9 = 0LL;
if ( v8 )
{
do
*(_BYTE *)(*(_QWORD *)a2 + v9++) = 1;
while ( v9 != v8 );
}
break;
default:
fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", v12);
break;
}
}
}
}
return 5LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(int *a1, __int64 a2, int a3)
{
int v5; // eax
unsigned int v6; // edx
unsigned int v7; // esi
unsigned int v8; // edx
__int64 v9; // rax
unsigned int v11; // [rsp+8h] [rbp-820h] BYREF
unsigned __int8 v12; // [rsp+Fh] [rbp-819h] BYREF
unsigned int v13[518]; // [rsp+10h] [rbp-818h] BYREF

v12 = 0;
v11 = 0;
while ( 1 )
{
if ( !(unsigned int)select_call((unsigned int)*a1, 0LL, 0LL, 1LL) )
return 5LL;
v5 = recv_buf(v13, 2048LL, (unsigned int)*a1, a1, &v12, &v11);
if ( v5 == -1 )
break;
v6 = *(_DWORD *)(a2 + 32);
if ( v6 <= v11 )
{
v7 = *(_DWORD *)(a2 + 16);
if ( v11 < v7 + v6 )
{
switch ( v12 )
{
case 4u:
func3((__int64)a1, a2, v11);
break;
case 6u:
func7(v13, v5, a1, a3 + 1);
return 1LL;
case 3u:
v8 = (v11 - 1) % v7;
v9 = 0LL;
if ( v8 )
{
do
*(_BYTE *)(*(_QWORD *)a2 + v9++) = 1;
while ( v9 != v8 );
}
break;
default:
fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", v12);
break;
}
}
}
}
return 5LL;
}

original_code:
STATE read_acks(Connection *client, Window *window, int nest_level)
{
 uint32_t recv_len = 0;
 uint8_t buf[2048];
 uint8_t flag = 0;
 uint32_t seq_num = 0;
 uint32_t window_index;
 int i = 0;
 while(select_call(client->socket_num, 0, 0, NOT_NULL)) {
  recv_len = recv_buf(buf, 2048, client->socket_num, client, &flag, &seq_num);
  if (recv_len == CRC_ERROR) {
   return STATE_SEND_DATA;
  }
  if ((seq_num >= window->base_seq_num) && (seq_num < (window->base_seq_num + window->window_size))) {
   window_index = (seq_num-1) % window->window_size;
   switch (flag) {
    case FLAG_FILENAME_REQ:
     process_client(buf, recv_len, client, nest_level + 1);
     return STATE_DONE;
     break;
    case FLAG_ACK:
     for (i=0; i < window_index; i++) {
      window->registry[i] = 1;
     }
     break;
    case FLAG_SREJ:
     send_data(client, window, seq_num);
     break;
    default:
     fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", flag);
     break;
   }
  }
 }
 return STATE_SEND_DATA;
}

canonical_original_code:
enum STATE read_acks(struct Connection *client, struct Window *window, int nest_level)
{
 unsigned int recv_len = 0;
 unsigned char buf[2048];
 unsigned char flag = 0;
 unsigned int seq_num = 0;
 unsigned int window_index;
 int i = 0;
 while(select_call(client->socket_num, 0, 0, NOT_NULL)) {
  recv_len = recv_buf(buf, 2048, client->socket_num, client, &flag, &seq_num);
  if (recv_len == CRC_ERROR) {
   return STATE_SEND_DATA;
  }
  if ((seq_num >= window->base_seq_num) && (seq_num < (window->base_seq_num + window->window_size))) {
   window_index = (seq_num-1) % window->window_size;
   switch (flag) {
    case FLAG_FILENAME_REQ:
     process_client(buf, recv_len, client, nest_level + 1);
     return STATE_DONE;
     break;
    case FLAG_ACK:
     for (i=0; i < window_index; i++) {
      window->registry[i] = 1;
     }
     break;
    case FLAG_SREJ:
     send_data(client, window, seq_num);
     break;
    default:
     fprintf(stderr, "ERROR: non-ACK flag (%u) in wait_on_ack\n", flag);
     break;
   }
  }
 }
 return STATE_SEND_DATA;
}

variable_types: struct Connection { int socket_num; 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]; } remote; unsigned int len; } *


filename

decompiled_code:
__int64 __fastcall filename(int *a1, unsigned int *a2, int a3, int *a4, void **a5)
{
unsigned int v7; // er14
unsigned int v8; // er15
int v9; // eax
unsigned __int64 v10; // rbx
unsigned int *v11; // rax
__int64 result; // rax
int v13; // eax
unsigned int v14; // ebx
unsigned int v15; // eax
int *v16; // [rsp+8h] [rbp-140h]
char file[312]; // [rsp+10h] [rbp-138h] BYREF

v16 = a4;
v7 = *a2;
v8 = a2[1];
v9 = socket(2, 2, 0);
*a1 = v9;
if ( v9 < 0 )
{
perror("filename:socket");
exit(1);
}
if ( a3 > 263 )
{
send_buf(0LL, 0LL, a1, 8LL, 0LL);
result = 1LL;
}
else
{
v10 = a3 - 8;
v11 = a2 + 2;
if ( v10 >= 8 )
{
*(int **)((char *)&v16 + v10) = *(int **)((char *)a2 + v10);
qmemcpy(file, a2 + 2, 8 * ((v10 - 1) >> 3));
}
else if ( (v10 & 4) != 0 )
{
*(_DWORD *)file = a2[2];
*(_DWORD *)&file[v10 - 4] = *(unsigned int *)((char *)v11 + v10 - 4);
}
else if ( v10 )
{
file[0] = *((_BYTE *)a2 + 8);
if ( (v10 & 2) != 0 )
*(_WORD *)&file[v10 - 2] = *(_WORD *)((char *)v11 + v10 - 2);
}
v13 = open(file, 0);
*v16 = v13;
if ( v13 < 0 )
{
send_buf(0LL, 0LL, a1, 8LL, 0LL);
free(*a5);
*a5 = 0LL;
result = 1LL;
}
else
{
send_buf(0LL, 0LL, a1, 7LL, 0LL);
result = 3LL;
if ( a5 )
{
v14 = _bswap_32(v8);
v15 = _bswap_32(v7);
*a5 = (void *)newWindowWithSizeAndBuffer(v14, v15);
result = 3LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, unsigned int *a2, int a3, int *a4, void **a5)
{
unsigned int v7; // er14
unsigned int v8; // er15
int v9; // eax
unsigned __int64 v10; // rbx
unsigned int *v11; // rax
__int64 result; // rax
int v13; // eax
unsigned int v14; // ebx
unsigned int v15; // eax
int *v16; // [rsp+8h] [rbp-140h]
char file[312]; // [rsp+10h] [rbp-138h] BYREF

v16 = a4;
v7 = *a2;
v8 = a2[1];
v9 = socket(2, 2, 0);
*a1 = v9;
if ( v9 < 0 )
{
perror("filename:socket");
exit(1);
}
if ( a3 > 263 )
{
send_buf(0LL, 0LL, a1, 8LL, 0LL);
result = 1LL;
}
else
{
v10 = a3 - 8;
v11 = a2 + 2;
if ( v10 >= 8 )
{
*(int **)((char *)&v16 + v10) = *(int **)((char *)a2 + v10);
qmemcpy(file, a2 + 2, 8 * ((v10 - 1) >> 3));
}
else if ( (v10 & 4) != 0 )
{
*(_DWORD *)file = a2[2];
*(_DWORD *)&file[v10 - 4] = *(unsigned int *)((char *)v11 + v10 - 4);
}
else if ( v10 )
{
file[0] = *((_BYTE *)a2 + 8);
if ( (v10 & 2) != 0 )
*(_WORD *)&file[v10 - 2] = *(_WORD *)((char *)v11 + v10 - 2);
}
v13 = open(file, 0);
*v16 = v13;
if ( v13 < 0 )
{
send_buf(0LL, 0LL, a1, 8LL, 0LL);
free(*a5);
*a5 = 0LL;
result = 1LL;
}
else
{
send_buf(0LL, 0LL, a1, 7LL, 0LL);
result = 3LL;
if ( a5 )
{
v14 = func0(v8);
v15 = func0(v7);
*a5 = (void *)newWindowWithSizeAndBuffer(v14, v15);
result = 3LL;
}
}
}
return result;
}

original_code:
STATE filename(Connection *client, uint8_t *buf, int32_t recv_len, int32_t *data_file, Window **window)
{
 char fname[255];
 uint32_t buf_size;
 uint32_t window_size;
 memcpy(&buf_size, buf, 4);
 buf_size = ntohl(buf_size);
 memcpy(&window_size, &buf[4], 4);
 window_size = ntohl(window_size);
 if ((client->socket_num = socket(2, SOCK_DGRAM, 0)) < 0) {
  perror("filename:socket");
  exit(1);
 }
 if ((recv_len - 8) > 255) {
  send_buf(((void *)0), 0, client, FLAG_FILENAME_RESP_BAD, 0);
  return STATE_DONE;
 }
 memcpy(fname, &buf[8], recv_len - 8);
 if (((*data_file) = open(fname, 00)) < 0) {
  send_buf(((void *)0), 0, client, FLAG_FILENAME_RESP_BAD, 0);
  free(*window);
  *window = ((void *)0);
  return STATE_DONE;
 }
 send_buf(((void *)0), 0, client, FLAG_FILENAME_RESP_OK, 0);
 if (window != ((void *)0)) {
  *window = newWindowWithSizeAndBuffer(window_size, buf_size);
 }
 return STATE_WINDOW_NEXT;
}

canonical_original_code:
enum STATE filename(struct Connection *client, unsigned char *buf, int recv_len, int *data_file, struct Window **window)
{
 char fname[255];
 unsigned int buf_size;
 unsigned int window_size;
 memcpy(&buf_size, buf, 4);
 buf_size = ntohl(buf_size);
 memcpy(&window_size, &buf[4], 4);
 window_size = ntohl(window_size);
 if ((client->socket_num = socket(2, SOCK_DGRAM, 0)) < 0) {
  perror("filename:socket");
  exit(1);
 }
 if ((recv_len - 8) > 255) {
  send_buf(((void *)0), 0, client, FLAG_FILENAME_RESP_BAD, 0);
  return STATE_DONE;
 }
 memcpy(fname, &buf[8], recv_len - 8);
 if (((*data_file) = open(fname, 00)) < 0) {
  send_buf(((void *)0), 0, client, FLAG_FILENAME_RESP_BAD, 0);
  free(*window);
  *window = ((void *)0);
  return STATE_DONE;
 }
 send_buf(((void *)0), 0, client, FLAG_FILENAME_RESP_OK, 0);
 if (window != ((void *)0)) {
  *window = newWindowWithSizeAndBuffer(window_size, buf_size);
 }
 return STATE_WINDOW_NEXT;
}

variable_types:
buf
unsigned char *

variable_types: struct Window { unsigned char *registry; unsigned char *buffer; unsigned int window_size; unsigned int block_size; unsigned int buffer_size; int data_len; unsigned int base_seq_num; unsigned int window_index; } **

variable_types:
fname
char [255]


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8

if ( argc != 2 )
{
fprintf(stderr, "usage: %s <error_rate>\n", *argv);
exit(-2);
}
v3 = atof(argv[1]);
sendErr_init(1LL, 1LL, 0LL, 0LL, v3);
server_runloop();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8

if ( argc != 2 )
{
fprintf(stderr, "usage: %s <error_rate>\n", *argv);
exit(-2);
}
v3 = atof(argv[1]);
sendErr_init(1LL, 1LL, 0LL, 0LL, v3);
func8();
}

original_code:
int main(int argc, char *argv[])
{
 if (argc != 2) {
       fprintf(stderr, "usage: %s <error_rate>\n" , argv[0]);
       exit(-2);
    }
    sendErr_init(atof(argv[1]),
  1,
  1,
  0,
  0);
 server_runloop();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
uint16_t tcp_checksum(struct TCPFrameHeader *tcp_header, struct IPFrameHeader *ip_header)
{
 uint8_t ipHeaderSize = ((ip_header->ip_version_and_header_length & 0x0F) * 4);
 uint16_t ipHeaderTotalLength = ntohs(ip_header->ip_total_length);
 uint16_t tcpFullHeaderLength = sizeof(struct TCPPsuedoHeader) + ipHeaderTotalLength - ipHeaderSize;
 uint16_t tcpLength = ipHeaderTotalLength - ipHeaderSize;
 struct TCPPsuedoHeader *psuedoHeader = malloc(tcpFullHeaderLength);
 psuedoHeader->tcp_source_address = ip_header->ip_source_address;
 psuedoHeader->tcp_destination_address = ip_header->ip_destination_address;
 psuedoHeader->tcp_zeroes = 0;
 psuedoHeader->tcp_protocol = IP_PROTO_TCP;
 psuedoHeader->tcp_length = htons(tcpLength);
 memcpy(((char *)psuedoHeader)+sizeof(struct TCPPsuedoHeader), tcp_header, tcpLength);
 uint16_t checksum = in_cksum((uint16_t *)psuedoHeader, tcpFullHeaderLength);
 free((void *)psuedoHeader);
 return checksum;
}

canonical_original_code:
unsigned short tcp_checksum(struct TCPFrameHeader *tcp_header, struct IPFrameHeader *ip_header)
{
 unsigned char ipHeaderSize = ((ip_header->ip_version_and_header_length & 0x0F) * 4);
 unsigned short ipHeaderTotalLength = ntohs(ip_header->ip_total_length);
 unsigned short tcpFullHeaderLength = sizeof(struct TCPPsuedoHeader) + ipHeaderTotalLength - ipHeaderSize;
 unsigned short tcpLength = ipHeaderTotalLength - ipHeaderSize;
 struct TCPPsuedoHeader *psuedoHeader = malloc(tcpFullHeaderLength);
 psuedoHeader->tcp_source_address = ip_header->ip_source_address;
 psuedoHeader->tcp_destination_address = ip_header->ip_destination_address;
 psuedoHeader->tcp_zeroes = 0;
 psuedoHeader->tcp_protocol = IP_PROTO_TCP;
 psuedoHeader->tcp_length = htons(tcpLength);
 memcpy(((char *)psuedoHeader)+sizeof(struct TCPPsuedoHeader), tcp_header, tcpLength);
 unsigned short checksum = in_cksum((uint16_t *)psuedoHeader, tcpFullHeaderLength);
 free((void *)psuedoHeader);
 return checksum;
}

variable_types: struct TCPFrameHeader { unsigned short tcp_source_port; unsigned short tcp_destination_port; unsigned int tcp_sequence_number; unsigned int tcp_acknowledgement_number; unsigned char tcp_data_offset; unsigned char tcp_flags; unsigned short tcp_window_size; unsigned short tcp_checksum; unsigned short tcp_urgent_pointer; } *

variable_types: struct IPFrameHeader { unsigned char ip_version_and_header_length; unsigned char ip_dscp_and_ecn; unsigned short ip_total_length; unsigned short ip_identifier; unsigned short ip_flags_and_fragment_offset; unsigned char ip_time_to_live; unsigned char ip_protocol; unsigned short ip_header_checksum; unsigned int ip_source_address; unsigned int ip_destination_address; } *

variable_types:
ipHeaderSize
unsigned char

variable_types:
ipHeaderTotalLength
unsigned short

variable_types:
tcpFullHeaderLength
unsigned short

variable_types:
tcpLength
unsigned short

variable_types: struct TCPPsuedoHeader { unsigned int tcp_source_address; unsigned int tcp_destination_address; unsigned char tcp_zeroes; unsigned char tcp_protocol; unsigned short tcp_length; } *

variable_types:
checksum
unsigned short

return_type: unsigned short


tcp

decompiled_code:
int __fastcall tcp(void *src, __int64 a2, __int64 a3)
{
unsigned __int16 v4; // ax
unsigned __int16 v5; // ax
unsigned int v6; // eax
unsigned int v7; // eax
const char *v8; // rsi
const char *v9; // rsi
const char *v10; // rsi
unsigned __int16 v11; // ax
unsigned __int16 v12; // ax

puts("\n\tTCP Header");
printf("\t\tSource Port: ");
v4 = _bswap_16(*(_WORD *)src);
if ( v4 == 25 )
{
printf("SMTP");
}
else
{
if ( v4 <= 0x19u )
{
if ( v4 == 20 )
{
printf("FTP");
goto LABEL_9;
}
if ( v4 == 23 )
{
puts("Telnet");
goto LABEL_9;
}
}
else
{
if ( v4 == 80 )
{
printf("HTTP");
goto LABEL_9;
}
if ( v4 == 110 )
{
printf("POP3");
goto LABEL_9;
}
}
printf("%d", v4);
}
LABEL_9:
putchar(10);
printf("\t\tDest Port: ");
v5 = _bswap_16(*((_WORD *)src + 1));
if ( v5 == 25 )
{
printf("SMTP");
}
else
{
if ( v5 <= 0x19u )
{
if ( v5 == 20 )
{
printf("FTP");
goto LABEL_21;
}
if ( v5 == 23 )
{
puts("Telnet");
goto LABEL_21;
}
}
else
{
if ( v5 == 80 )
{
printf("HTTP");
goto LABEL_21;
}
if ( v5 == 110 )
{
printf("POP3");
goto LABEL_21;
}
}
printf("%d", v5);
}
LABEL_21:
putchar(10);
v6 = _bswap_32(*((_DWORD *)src + 1));
printf("\t\tSequence Number: %u\n", v6);
v7 = _bswap_32(*((_DWORD *)src + 2));
printf("\t\tACK Number: %u\n", v7);
v8 = (const char *)&unk_342;
if ( (*((_BYTE *)src + 13) & 2) == 0 )
v8 = (const char *)&unk_346;
printf("\t\tSYN Flag: %s\n", v8);
v9 = (const char *)&unk_342;
if ( (*((_BYTE *)src + 13) & 4) == 0 )
v9 = (const char *)&unk_346;
printf("\t\tRST Flag: %s\n", v9);
v10 = (const char *)&unk_342;
if ( (*((_BYTE *)src + 13) & 1) == 0 )
v10 = (const char *)&unk_346;
printf("\t\tFIN Flag: %s\n", v10);
v11 = _bswap_16(*((_WORD *)src + 7));
printf("\t\tWindow Size: %u\n", v11);
printf("\t\tChecksum: ");
if ( (unsigned __int16)tcp_checksum(src, a3) )
printf("Incorrect");
else
printf("Correct");
v12 = _bswap_16(*((_WORD *)src + 8));
return printf(" (0x%x)", v12);
}

canonical_decompiled_code:
int __fastcall func3(void *src, __int64 a2, __int64 a3)
{
unsigned __int16 v4; // ax
unsigned __int16 v5; // ax
unsigned int v6; // eax
unsigned int v7; // eax
const char *v8; // rsi
const char *v9; // rsi
const char *v10; // rsi
unsigned __int16 v11; // ax
unsigned __int16 v12; // ax

puts("\n\tTCP Header");
printf("\t\tSource Port: ");
v4 = func0(*(_WORD *)src);
if ( v4 == 25 )
{
printf("SMTP");
}
else
{
if ( v4 <= 0x19u )
{
if ( v4 == 20 )
{
printf("FTP");
goto LABEL_9;
}
if ( v4 == 23 )
{
puts("Telnet");
goto LABEL_9;
}
}
else
{
if ( v4 == 80 )
{
printf("HTTP");
goto LABEL_9;
}
if ( v4 == 110 )
{
printf("POP3");
goto LABEL_9;
}
}
printf("%d", v4);
}
LABEL_9:
putchar(10);
printf("\t\tDest Port: ");
v5 = func0(*((_WORD *)src + 1));
if ( v5 == 25 )
{
printf("SMTP");
}
else
{
if ( v5 <= 0x19u )
{
if ( v5 == 20 )
{
printf("FTP");
goto LABEL_21;
}
if ( v5 == 23 )
{
puts("Telnet");
goto LABEL_21;
}
}
else
{
if ( v5 == 80 )
{
printf("HTTP");
goto LABEL_21;
}
if ( v5 == 110 )
{
printf("POP3");
goto LABEL_21;
}
}
printf("%d", v5);
}
LABEL_21:
putchar(10);
v6 = func1(*((_DWORD *)src + 1));
printf("\t\tSequence Number: %u\n", v6);
v7 = func1(*((_DWORD *)src + 2));
printf("\t\tACK Number: %u\n", v7);
v8 = (const char *)&unk_342;
if ( (*((_BYTE *)src + 13) & 2) == 0 )
v8 = (const char *)&unk_346;
printf("\t\tSYN Flag: %s\n", v8);
v9 = (const char *)&unk_342;
if ( (*((_BYTE *)src + 13) & 4) == 0 )
v9 = (const char *)&unk_346;
printf("\t\tRST Flag: %s\n", v9);
v10 = (const char *)&unk_342;
if ( (*((_BYTE *)src + 13) & 1) == 0 )
v10 = (const char *)&unk_346;
printf("\t\tFIN Flag: %s\n", v10);
v11 = func0(*((_WORD *)src + 7));
printf("\t\tWindow Size: %u\n", v11);
printf("\t\tChecksum: ");
if ( (unsigned __int16)func2(src, a3) )
printf("Incorrect");
else
printf("Correct");
v12 = func0(*((_WORD *)src + 8));
return printf(" (0x%x)", v12);
}

original_code:
void tcp(uint8_t *packetData, int packetLength, struct IPFrameHeader *ipHeader)
{
 struct TCPFrameHeader *header = (struct TCPFrameHeader *)packetData;
 printf("\n\tTCP Header\n");
 printf("\t\tSource Port:  ");
 switch(ntohs(header->tcp_source_port)) {
  case PORT_TELNET:
   printf("Telnet\n");
   break;
  case PORT_FTP:
   printf("FTP");
   break;
  case PORT_HTTP:
   printf("HTTP");
   break;
  case PORT_POP3:
   printf("POP3");
   break;
  case PORT_SMTP:
   printf("SMTP");
   break;
  default:
   printf("%d", ntohs(header->tcp_source_port));
   break;
 }
 printf("\n");
 printf("\t\tDest Port:  ");
 switch(ntohs(header->tcp_destination_port)) {
  case PORT_TELNET:
   printf("Telnet\n");
   break;
  case PORT_FTP:
   printf("FTP");
   break;
  case PORT_HTTP:
   printf("HTTP");
   break;
  case PORT_POP3:
   printf("POP3");
   break;
  case PORT_SMTP:
   printf("SMTP");
   break;
  default:
   printf("%d", ntohs(header->tcp_destination_port));
   break;
 }
 printf("\n");
 printf("\t\tSequence Number: %u\n", ntohl(header->tcp_sequence_number));
 printf("\t\tACK Number: %u\n", ntohl(header->tcp_acknowledgement_number));
 printf("\t\tSYN Flag: %s\n", (header->tcp_flags & TCP_SYN) ? "Yes" : "No");
 printf("\t\tRST Flag: %s\n", (header->tcp_flags & TCP_RST) ? "Yes" : "No");
 printf("\t\tFIN Flag: %s\n", (header->tcp_flags & TCP_FIN) ? "Yes" : "No");
 printf("\t\tWindow Size: %u\n", ntohs(header->tcp_window_size));
 printf("\t\tChecksum: ");
 if(tcp_checksum(header, ipHeader)) {
  printf("Incorrect");
 }
 else {
  printf("Correct");
 }
 printf(" (0x%x)", ntohs(header->tcp_checksum));
}

canonical_original_code:
void tcp(unsigned char *packetData, int packetLength, struct IPFrameHeader *ipHeader)
{
 struct TCPFrameHeader *header = (struct TCPFrameHeader *)packetData;
 printf("\n\tTCP Header\n");
 printf("\t\tSource Port:  ");
 switch(ntohs(header->tcp_source_port)) {
  case PORT_TELNET:
   printf("Telnet\n");
   break;
  case PORT_FTP:
   printf("FTP");
   break;
  case PORT_HTTP:
   printf("HTTP");
   break;
  case PORT_POP3:
   printf("POP3");
   break;
  case PORT_SMTP:
   printf("SMTP");
   break;
  default:
   printf("%d", ntohs(header->tcp_source_port));
   break;
 }
 printf("\n");
 printf("\t\tDest Port:  ");
 switch(ntohs(header->tcp_destination_port)) {
  case PORT_TELNET:
   printf("Telnet\n");
   break;
  case PORT_FTP:
   printf("FTP");
   break;
  case PORT_HTTP:
   printf("HTTP");
   break;
  case PORT_POP3:
   printf("POP3");
   break;
  case PORT_SMTP:
   printf("SMTP");
   break;
  default:
   printf("%d", ntohs(header->tcp_destination_port));
   break;
 }
 printf("\n");
 printf("\t\tSequence Number: %u\n", ntohl(header->tcp_sequence_number));
 printf("\t\tACK Number: %u\n", ntohl(header->tcp_acknowledgement_number));
 printf("\t\tSYN Flag: %s\n", (header->tcp_flags & TCP_SYN) ? "Yes" : "No");
 printf("\t\tRST Flag: %s\n", (header->tcp_flags & TCP_RST) ? "Yes" : "No");
 printf("\t\tFIN Flag: %s\n", (header->tcp_flags & TCP_FIN) ? "Yes" : "No");
 printf("\t\tWindow Size: %u\n", ntohs(header->tcp_window_size));
 printf("\t\tChecksum: ");
 if(tcp_checksum(header, ipHeader)) {
  printf("Incorrect");
 }
 else {
  printf("Correct");
 }
 printf(" (0x%x)", ntohs(header->tcp_checksum));
}

variable_types:
packetData
unsigned char *

variable_types: packetLength int

user_defined_types: struct IPFrameHeader { unsigned char ip_version_and_header_length; unsigned char ip_dscp_and_ecn; unsigned short ip_total_length; unsigned short ip_identifier; unsigned short ip_flags_and_fragment_offset; unsigned char ip_time_to_live; unsigned char ip_protocol; unsigned short ip_header_checksum; unsigned int ip_source_address; unsigned int ip_destination_address; }

user_defined_types: struct TCPFrameHeader { unsigned short tcp_source_port; unsigned short tcp_destination_port; unsigned int tcp_sequence_number; unsigned int tcp_acknowledgement_number; unsigned char tcp_data_offset; unsigned char tcp_flags; unsigned short tcp_window_size; unsigned short tcp_checksum; unsigned short tcp_urgent_pointer; }


decompiled_code:
__int64 __fastcall window_full(__int64 a1, int a2)
{
write(a2, *(const void **)(a1 + 8), *(unsigned int *)(a1 + 24));
return 4LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
write(a2, *(const void **)(a1 + 8), *(unsigned int *)(a1 + 24));
return 4LL;
}

original_code:
STATE window_full(Window *window, int output_fd)
{
 write(output_fd, window->buffer, window->buffer_size);
 return STATE_RECV_DATA;
}

canonical_original_code:
enum STATE window_full(struct Window *window, int output_fd)
{
 write(output_fd, window->buffer, window->buffer_size);
 return STATE_RECV_DATA;
}

variable_types: struct Window { unsigned char *registry; unsigned char *buffer; unsigned int window_size; unsigned int block_size; unsigned int buffer_size; int data_len; unsigned int base_seq_num; unsigned int window_index; } *

return_type: enum STATE { STATE_START=0, STATE_DONE=1, STATE_FILENAME=2, STATE_FILE_OK=3, STATE_RECV_DATA=4, STATE_WINDOW_FULL=5, STATE_EOF=6 }


decompiled_code:
__int64 __fastcall send_ack(unsigned int a1)
{
return send_buf(0LL, 0LL, &server, 3LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
return send_buf(0LL, 0LL, &server, 3LL, a1);
}

original_code:
void send_ack(int seq_num)
{
 send_buf(((void *)0), 0, &server, FLAG_ACK, seq_num);
}


decompiled_code:
__int64 recv_eof()
{
__int64 result; // rax
unsigned int v1; // [rsp+Ch] [rbp-81Ch]
char v2[2072]; // [rsp+10h] [rbp-818h] BYREF

v2[2063] = 0;
v1 = nextOpenSequenceNumber();
send_ack(v1);
if ( (unsigned int)select_call((unsigned int)server, 1LL, 0LL, 1LL) == 1 )
{
recv_buf(
v2,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
(const char *)(unsigned int)server);
result = 6LL;
}
else
{
puts("File Transfer Complete");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
unsigned int v1; // [rsp+Ch] [rbp-81Ch]
char v2[2072]; // [rsp+10h] [rbp-818h] BYREF

v2[2063] = 0;
v1 = nextOpenSequenceNumber();
func3(v1);
if ( (unsigned int)select_call((unsigned int)server, 1LL, 0LL, 1LL) == 1 )
{
recv_buf(
v2,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
(const char *)(unsigned int)server);
result = 6LL;
}
else
{
puts("File Transfer Complete");
result = 1LL;
}
return result;
}

original_code:
STATE recv_eof(Window *window, int output_fd)
{
 uint8_t flag = 0;
 int32_t data_len = 0;
 uint8_t data_buf[2048];
 uint32_t seq_num = nextOpenSequenceNumber(window);
 send_ack(seq_num);
 while (select_call(server.socket_num, 1, 0, NOT_NULL) == 1) {
  data_len = recv_buf(data_buf, 2048, server.socket_num, &server, &flag, &seq_num);
  return STATE_EOF;
 }
 printf("File Transfer Complete\n");
 return STATE_DONE;
}

canonical_original_code:
enum STATE recv_eof(struct Window *window, int output_fd)
{
 unsigned char flag = 0;
 int data_len = 0;
 unsigned char data_buf[2048];
 unsigned int seq_num = nextOpenSequenceNumber(window);
 send_ack(seq_num);
 while (select_call(server.socket_num, 1, 0, NOT_NULL) == 1) {
  data_len = recv_buf(data_buf, 2048, server.socket_num, &server, &flag, &seq_num);
  return STATE_EOF;
 }
 printf("File Transfer Complete\n");
 return STATE_DONE;
}

variable_types:
flag
unsigned char

user_defined_types: enum STATE { STATE_START=0, STATE_DONE=1, STATE_FILENAME=2, STATE_FILE_OK=3, STATE_RECV_DATA=4, STATE_WINDOW_FULL=5, STATE_EOF=6 }


decompiled_code:
__int64 __fastcall send_srej(unsigned int a1)
{
return send_buf(0LL, 0LL, &server, 4LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
return send_buf(0LL, 0LL, &server, 4LL, a1);
}

original_code:
void send_srej(int seq_num)
{
 send_buf(((void *)0), 0, &server, FLAG_SREJ, seq_num);
}


decompiled_code:
__int64 __fastcall recv_data(__int64 a1)
{
int v2; // ebp
__int64 result; // rax
unsigned int v4; // er13
unsigned int v5; // er12
unsigned int v6; // edi
unsigned int v7; // edx
unsigned int v8; // ecx
unsigned int v9; // edx
__int64 v10; // r14
unsigned int v11; // edx
unsigned int v12; // er13
int v13; // er14
_BYTE v14[2059]; // [rsp+0h] [rbp-838h] BYREF
unsigned __int8 v15; // [rsp+80Bh] [rbp-2Dh]
unsigned int v16; // [rsp+80Ch] [rbp-2Ch]

v16 = 0;
v15 = 0;
if ( !(unsigned int)select_call((unsigned int)server, 10LL, 0LL, 1LL) )
{
fwrite("Shutting down: No response from server for 10 seconds.\n", 1uLL, 0x37uLL, stderr);
exit(1);
}
v2 = recv_buf(
v14,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
(const char *)(unsigned int)server);
result = 4LL;
if ( v2 != -1 )
{
if ( v15 <= 2u )
{
if ( v15 )
{
v4 = maxSequenceNumber(a1);
v5 = nextOpenSequenceNumber(a1);
if ( v16 >= v5 )
{
v7 = *(_DWORD *)(a1 + 32);
if ( v16 >= v7 )
{
v8 = *(_DWORD *)(a1 + 16);
if ( v16 < v8 + v7 )
{
v9 = (v16 - 1) % v8;
v10 = v9;
if ( !*(_BYTE *)(*(_QWORD *)a1 + v9) )
{
v12 = *(_DWORD *)(a1 + 20) * v9;
memcpy((void *)(*(_QWORD *)(a1 + 8) + v12), v14, v2);
*(_BYTE *)(*(_QWORD *)a1 + v10) = 1;
v13 = maxSequenceNumber(a1);
v5 = nextOpenSequenceNumber(a1);
if ( v16 == v13 )
*(_DWORD *)(a1 + 24) = v12 + v2;
}
}
}
if ( v16 <= v5 )
{
send_ack(v5 - 1);
}
else
{
v11 = (v16 - 1) % *(_DWORD *)(a1 + 16);
if ( !*(_BYTE *)(*(_QWORD *)a1 + v11 - 1) )
send_srej(v11 + *(_DWORD *)(a1 + 32) - 1);
}
result = 4 - ((unsigned int)((unsigned int)windowIsFull(a1) == 0) - 1);
}
else
{
v6 = *(_DWORD *)(a1 + 32);
if ( v16 >= v6 )
{
if ( (unsigned int)windowIsFull(a1) )
send_ack(v4);
else
send_srej(v5);
result = 4LL;
}
else
{
send_ack(v6 - 1);
result = 4LL;
}
}
}
else
{
result = 4LL;
}
}
else
{
result = 2 * (unsigned int)(v15 == 9) + 4;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v2; // ebp
__int64 result; // rax
unsigned int v4; // er13
unsigned int v5; // er12
unsigned int v6; // edi
unsigned int v7; // edx
unsigned int v8; // ecx
unsigned int v9; // edx
__int64 v10; // r14
unsigned int v11; // edx
unsigned int v12; // er13
int v13; // er14
_BYTE v14[2059]; // [rsp+0h] [rbp-838h] BYREF
unsigned __int8 v15; // [rsp+80Bh] [rbp-2Dh]
unsigned int v16; // [rsp+80Ch] [rbp-2Ch]

v16 = 0;
v15 = 0;
if ( !(unsigned int)select_call((unsigned int)server, 10LL, 0LL, 1LL) )
{
fwrite("Shutting down: No response from server for 10 seconds.\n", 1uLL, 0x37uLL, stderr);
exit(1);
}
v2 = recv_buf(
v14,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
(const char *)(unsigned int)server);
result = 4LL;
if ( v2 != -1 )
{
if ( v15 <= 2u )
{
if ( v15 )
{
v4 = maxSequenceNumber(a1);
v5 = nextOpenSequenceNumber(a1);
if ( v16 >= v5 )
{
v7 = *(_DWORD *)(a1 + 32);
if ( v16 >= v7 )
{
v8 = *(_DWORD *)(a1 + 16);
if ( v16 < v8 + v7 )
{
v9 = (v16 - 1) % v8;
v10 = v9;
if ( !*(_BYTE *)(*(_QWORD *)a1 + v9) )
{
v12 = *(_DWORD *)(a1 + 20) * v9;
memcpy((void *)(*(_QWORD *)(a1 + 8) + v12), v14, v2);
*(_BYTE *)(*(_QWORD *)a1 + v10) = 1;
v13 = maxSequenceNumber(a1);
v5 = nextOpenSequenceNumber(a1);
if ( v16 == v13 )
*(_DWORD *)(a1 + 24) = v12 + v2;
}
}
}
if ( v16 <= v5 )
{
func3(v5 - 1);
}
else
{
v11 = (v16 - 1) % *(_DWORD *)(a1 + 16);
if ( !*(_BYTE *)(*(_QWORD *)a1 + v11 - 1) )
func5(v11 + *(_DWORD *)(a1 + 32) - 1);
}
result = 4 - ((unsigned int)((unsigned int)windowIsFull(a1) == 0) - 1);
}
else
{
v6 = *(_DWORD *)(a1 + 32);
if ( v16 >= v6 )
{
if ( (unsigned int)windowIsFull(a1) )
func3(v4);
else
func5(v5);
result = 4LL;
}
else
{
func3(v6 - 1);
result = 4LL;
}
}
}
else
{
result = 4LL;
}
}
else
{
result = 2 * (unsigned int)(v15 == 9) + 4;
}
}
return result;
}

original_code:
STATE recv_data(Window *window)
{
 uint32_t seq_num = 0;
 uint8_t flag = 0;
 int32_t data_len = 0;
 uint8_t data_buf[2048];
 uint32_t window_index;
 uint32_t buffer_offset;
 uint32_t next_seq_num;
 uint32_t max_seq_num;
 static uint32_t expected_seq_number = 0;
 if (select_call(server.socket_num, 10, 0, NOT_NULL) == 0) {
  fprintf(stderr, "Shutting down: No response from server for 10 seconds.\n");
  exit(1);
 }
 data_len = recv_buf(data_buf, 2048, server.socket_num, &server, &flag, &seq_num);
 if (data_len == CRC_ERROR) {
  return STATE_RECV_DATA;
 }
 switch (flag) {
  case FLAG_DATA:
  case FLAG_DATA_RESENT:
   max_seq_num = maxSequenceNumber(window);
   next_seq_num = nextOpenSequenceNumber(window);
   if (seq_num < next_seq_num) {
    if (seq_num < window->base_seq_num) {
     send_ack(window->base_seq_num-1);
    }
    else {
     if (windowIsFull(window)) {
      send_ack(max_seq_num);
     }
     else {
      send_srej(next_seq_num);
     }
    }
    break;
   }
   if ((seq_num >= window->base_seq_num) && (seq_num < (window->base_seq_num + window->window_size))) {
    window_index = (seq_num-1) % window->window_size;
    if (window->registry[window_index] == 0) {
     buffer_offset = window_index * window->block_size;
     memcpy(&window->buffer[buffer_offset], data_buf, data_len);
     window->registry[window_index] = 1;
     max_seq_num = maxSequenceNumber(window);
     next_seq_num = nextOpenSequenceNumber(window);
     if (seq_num == max_seq_num) {
      window->buffer_size = buffer_offset + data_len;
     }
    }
   }
   if (seq_num > next_seq_num) {
    window_index = ((seq_num-1) % window->window_size);
    if (window->registry[window_index-1] == 0) {
     send_srej(window->base_seq_num+window_index-1);
    }
   }
   else {
    send_ack(next_seq_num-1);
   }
   expected_seq_number = next_seq_num;
   if (windowIsFull(window)) {
    return STATE_WINDOW_FULL;
   }
   break;
  case FLAG_END_OF_FILE:
   return STATE_EOF;
   break;
  default:
   break;
 }
 return STATE_RECV_DATA;
}

canonical_original_code:
enum STATE recv_data(struct Window *window)
{
 unsigned int seq_num = 0;
 unsigned char flag = 0;
 int data_len = 0;
 unsigned char data_buf[2048];
 unsigned int window_index;
 unsigned int buffer_offset;
 unsigned int next_seq_num;
 unsigned int max_seq_num;
 static unsigned int expected_seq_number = 0;
 if (select_call(server.socket_num, 10, 0, NOT_NULL) == 0) {
  fprintf(stderr, "Shutting down: No response from server for 10 seconds.\n");
  exit(1);
 }
 data_len = recv_buf(data_buf, 2048, server.socket_num, &server, &flag, &seq_num);
 if (data_len == CRC_ERROR) {
  return STATE_RECV_DATA;
 }
 switch (flag) {
  case FLAG_DATA:
  case FLAG_DATA_RESENT:
   max_seq_num = maxSequenceNumber(window);
   next_seq_num = nextOpenSequenceNumber(window);
   if (seq_num < next_seq_num) {
    if (seq_num < window->base_seq_num) {
     send_ack(window->base_seq_num-1);
    }
    else {
     if (windowIsFull(window)) {
      send_ack(max_seq_num);
     }
     else {
      send_srej(next_seq_num);
     }
    }
    break;
   }
   if ((seq_num >= window->base_seq_num) && (seq_num < (window->base_seq_num + window->window_size))) {
    window_index = (seq_num-1) % window->window_size;
    if (window->registry[window_index] == 0) {
     buffer_offset = window_index * window->block_size;
     memcpy(&window->buffer[buffer_offset], data_buf, data_len);
     window->registry[window_index] = 1;
     max_seq_num = maxSequenceNumber(window);
     next_seq_num = nextOpenSequenceNumber(window);
     if (seq_num == max_seq_num) {
      window->buffer_size = buffer_offset + data_len;
     }
    }
   }
   if (seq_num > next_seq_num) {
    window_index = ((seq_num-1) % window->window_size);
    if (window->registry[window_index-1] == 0) {
     send_srej(window->base_seq_num+window_index-1);
    }
   }
   else {
    send_ack(next_seq_num-1);
   }
   expected_seq_number = next_seq_num;
   if (windowIsFull(window)) {
    return STATE_WINDOW_FULL;
   }
   break;
  case FLAG_END_OF_FILE:
   return STATE_EOF;
   break;
  default:
   break;
 }
 return STATE_RECV_DATA;
}


decompiled_code:
int __fastcall rcopy_runloop(__int64 a1, unsigned __int16 a2, void *a3, const char *a4, unsigned int a5, unsigned int a6)
{
_DWORD *v8; // rbp
int v9; // ebx
int v10; // er12
int v11; // er13
int result; // eax

v8 = 0LL;
v9 = 2;
v10 = 0;
v11 = -1;
LABEL_12:
while ( v9 != 1 )
{
switch ( v9 )
{
case 2:
if ( (unsigned int)connectToServer(a1, a2, &server) )
{
perror("ERROR: unable to connect to host");
exit(1);
}
v9 = filename(a3, a5, a6);
if ( v9 == 2 )
close(server);
if ( ++v10 > 9 )
{
fwrite("Ten failed filename tries: unable to reach server\n", 1uLL, 0x32uLL, stderr);
exit(1);
}
return result;
case 3:
v11 = open(a4, 577, 384LL);
if ( v11 < 0 )
{
perror("Open local_file");
exit(1);
}
v8 = (_DWORD *)newWindowWithSizeAndBuffer(a6, a5);
v9 = 4;
v10 = 0;
break;
case 4:
v9 = recv_data((__int64)v8);
break;
case 5:
v9 = window_full((__int64)v8, v11);
clearWindow(v8);
v8[8] += v8[4];
v8[6] = 0;
break;
case 6:
if ( v10 )
{
if ( ++v10 <= 9 )
goto LABEL_19;
fwrite("Ten failed EOF acks: file is ok but server doesn't know\n", 1uLL, 0x38uLL, stderr);
v9 = 1;
}
else
{
window_full((__int64)v8, v11);
v10 = 1;
LABEL_19:
v9 = recv_eof(v8, (unsigned int)v11);
}
break;
default:
goto LABEL_12;
}
}
if ( v8 )
destroyWindow(v8);
return close(v11);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, unsigned __int16 a2, void *a3, const char *a4, unsigned int a5, unsigned int a6)
{
_DWORD *v8; // rbp
int v9; // ebx
int v10; // er12
int v11; // er13
int result; // eax

v8 = 0LL;
v9 = 2;
v10 = 0;
v11 = -1;
LABEL_12:
while ( v9 != 1 )
{
switch ( v9 )
{
case 2:
if ( (unsigned int)connectToServer(a1, a2, &server) )
{
perror("ERROR: unable to connect to host");
exit(1);
}
v9 = func1(a3, a5, a6);
if ( v9 == 2 )
close(server);
if ( ++v10 > 9 )
{
fwrite("Ten failed filename tries: unable to reach server\n", 1uLL, 0x32uLL, stderr);
exit(1);
}
return result;
case 3:
v11 = open(a4, 577, 384LL);
if ( v11 < 0 )
{
perror("Open local_file");
exit(1);
}
v8 = (_DWORD *)newWindowWithSizeAndBuffer(a6, a5);
v9 = 4;
v10 = 0;
break;
case 4:
v9 = func6((__int64)v8);
break;
case 5:
v9 = func2((__int64)v8, v11);
clearWindow(v8);
v8[8] += v8[4];
v8[6] = 0;
break;
case 6:
if ( v10 )
{
if ( ++v10 <= 9 )
goto LABEL_19;
fwrite("Ten failed EOF acks: file is ok but server doesn't know\n", 1uLL, 0x38uLL, stderr);
v9 = 1;
}
else
{
func2((__int64)v8, v11);
v10 = 1;
LABEL_19:
v9 = func4(v8, (unsigned int)v11);
}
break;
default:
goto LABEL_12;
}
}
if ( v8 )
destroyWindow(v8);
return close(v11);
}

original_code:
void rcopy_runloop(char *server_host, uint16_t server_port, char *remote_file_name, char *output_file_name, uint32_t buf_size, uint32_t window_size)
{
 int output_fd = -1;
 int select_count = 0;
 int state = STATE_FILENAME;
 Window *window = ((void *)0);
 while (state != STATE_DONE) {
  switch (state) {
   case STATE_FILENAME:
    if (connectToServer(server_host, server_port, &server)) {
     perror("ERROR: unable to connect to host");
     exit(1);
    }
    state = filename(remote_file_name, buf_size, window_size);
    if (state == STATE_FILENAME) {
     close(server.socket_num);
    }
    select_count++;
    if (select_count >= 10) {
     fprintf(stderr, "Ten failed filename tries: unable to reach server\n");
     exit(1);
    }
    break;
   case STATE_FILE_OK:
    select_count = 0;
    if((output_fd = open(output_file_name, 01|0100|01000, 0600)) < 0 ) {
        perror("Open local_file");
        exit(1);
       }
       window = newWindowWithSizeAndBuffer(window_size, buf_size);
       state = STATE_RECV_DATA;
       break;
   case STATE_RECV_DATA:
    state = recv_data(window);
    break;
   case STATE_WINDOW_FULL:
    state = window_full(window, output_fd);
    clearWindow(window);
    window->base_seq_num += window->window_size;
    window->buffer_size = 0;
    break;
   case STATE_EOF:
    if (select_count == 0) {
     window_full(window, output_fd);
    }
    select_count++;
    if (select_count >= 10) {
     fprintf(stderr, "Ten failed EOF acks: file is ok but server doesn't know\n");
     state = STATE_DONE;
    }
    else {
     state = recv_eof(window, output_fd);
    }
    break;
   case STATE_DONE:
   default:
    break;
  }
 }
 if (window != ((void *)0)) {
  destroyWindow(window);
  window = ((void *)0);
 }
 close(output_fd);
}

canonical_original_code:
void rcopy_runloop(char *server_host, unsigned short server_port, char *remote_file_name, char *output_file_name, unsigned int buf_size, unsigned int window_size)
{
 int output_fd = -1;
 int select_count = 0;
 int state = STATE_FILENAME;
 struct Window *window = ((void *)0);
 while (state != STATE_DONE) {
  switch (state) {
   case STATE_FILENAME:
    if (connectToServer(server_host, server_port, &server)) {
     perror("ERROR: unable to connect to host");
     exit(1);
    }
    state = filename(remote_file_name, buf_size, window_size);
    if (state == STATE_FILENAME) {
     close(server.socket_num);
    }
    select_count++;
    if (select_count >= 10) {
     fprintf(stderr, "Ten failed filename tries: unable to reach server\n");
     exit(1);
    }
    break;
   case STATE_FILE_OK:
    select_count = 0;
    if((output_fd = open(output_file_name, 01|0100|01000, 0600)) < 0 ) {
        perror("Open local_file");
        exit(1);
       }
       window = newWindowWithSizeAndBuffer(window_size, buf_size);
       state = STATE_RECV_DATA;
       break;
   case STATE_RECV_DATA:
    state = recv_data(window);
    break;
   case STATE_WINDOW_FULL:
    state = window_full(window, output_fd);
    clearWindow(window);
    window->base_seq_num += window->window_size;
    window->buffer_size = 0;
    break;
   case STATE_EOF:
    if (select_count == 0) {
     window_full(window, output_fd);
    }
    select_count++;
    if (select_count >= 10) {
     fprintf(stderr, "Ten failed EOF acks: file is ok but server doesn't know\n");
     state = STATE_DONE;
    }
    else {
     state = recv_eof(window, output_fd);
    }
    break;
   case STATE_DONE:
   default:
    break;
  }
 }
 if (window != ((void *)0)) {
  destroyWindow(window);
  window = ((void *)0);
 }
 close(output_fd);
}

variable_types: state int


filename

decompiled_code:
__int64 __fastcall filename(void *src, unsigned int a2, unsigned int a3)
{
int v4; // ebx
int v5; // edx
__int64 result; // rax
int v7[2]; // [rsp+10h] [rbp-828h] BYREF
char dest[2080]; // [rsp+18h] [rbp-820h] BYREF

v4 = strlen((const char *)src);
v7[0] = _bswap_32(a2);
v7[1] = _bswap_32(a3);
memcpy(dest, src, v4 + 1);
send_buf(v7, (unsigned int)(v4 + 9), &server, 6LL, 0LL);
v5 = select_call((unsigned int)server, 1LL, 0LL, 1LL);
result = 2LL;
if ( v5 == 1 )
{
recv_buf(
v7,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
(const char *)(unsigned int)server);
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *src, unsigned int a2, unsigned int a3)
{
int v4; // ebx
int v5; // edx
__int64 result; // rax
int v7[2]; // [rsp+10h] [rbp-828h] BYREF
char dest[2080]; // [rsp+18h] [rbp-820h] BYREF

v4 = strlen((const char *)src);
v7[0] = func0(a2);
v7[1] = func0(a3);
memcpy(dest, src, v4 + 1);
send_buf(v7, (unsigned int)(v4 + 9), &server, 6LL, 0LL);
v5 = select_call((unsigned int)server, 1LL, 0LL, 1LL);
result = 2LL;
if ( v5 == 1 )
{
recv_buf(
v7,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
(const char *)(unsigned int)server);
result = 2LL;
}
return result;
}

original_code:
STATE filename(char *fname, int32_t buf_size, int32_t window_size)
{
 uint8_t buf[2048];
 uint8_t flag = 0;
 uint32_t seq_num = 0;
 int32_t fname_len = strlen(fname) + 1;
 int32_t recv_check = 0;
 buf_size = htonl(buf_size);
 memcpy(buf, &buf_size, 4);
 window_size = htonl(window_size);
 memcpy(&buf[4], &window_size, 4);
 memcpy(&buf[8], fname, fname_len);
 send_buf(buf, fname_len + 8, &server, FLAG_FILENAME_REQ, 0);
 if (select_call(server.socket_num, 1, 0, NOT_NULL) == 1) {
  recv_check = recv_buf(buf, 2048, server.socket_num, &server, &flag, &seq_num);
  if (recv_check == CRC_ERROR) {
   return STATE_FILENAME;
  }
  switch (flag) {
   case FLAG_FILENAME_RESP_OK:
    return STATE_FILE_OK;
    break;
   case FLAG_FILENAME_RESP_BAD:
    printf("File (%s) not found on server\n", fname);
    exit(1);
    break;
   default:
    break;
  }
 }
 return STATE_FILENAME;
}

canonical_original_code:
enum STATE filename(char *fname, int buf_size, int window_size)
{
 unsigned char buf[2048];
 unsigned char flag = 0;
 unsigned int seq_num = 0;
 int fname_len = strlen(fname) + 1;
 int recv_check = 0;
 buf_size = htonl(buf_size);
 memcpy(buf, &buf_size, 4);
 window_size = htonl(window_size);
 memcpy(&buf[4], &window_size, 4);
 memcpy(&buf[8], fname, fname_len);
 send_buf(buf, fname_len + 8, &server, FLAG_FILENAME_REQ, 0);
 if (select_call(server.socket_num, 1, 0, NOT_NULL) == 1) {
  recv_check = recv_buf(buf, 2048, server.socket_num, &server, &flag, &seq_num);
  if (recv_check == CRC_ERROR) {
   return STATE_FILENAME;
  }
  switch (flag) {
   case FLAG_FILENAME_RESP_OK:
    return STATE_FILE_OK;
    break;
   case FLAG_FILENAME_RESP_BAD:
    printf("File (%s) not found on server\n", fname);
    exit(1);
    break;
   default:
    break;
  }
 }
 return STATE_FILENAME;
}

variable_types:
fname
char *

variable_types:
buf
unsigned char [2048]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
const char *v4; // r14
int v5; // ebp
double v6; // xmm0_8
int v7; // er12
unsigned __int16 v8; // ax

if ( argc != 8 )
{
fprintf(
stderr,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
*argv);
exit(-2);
}
v3 = (char *)argv[1];
if ( strlen(v3) > 0xFF )
{
fwrite("ERROR: Remote filename exceeds maximum allowed length\n", 1uLL, 0x36uLL, stderr);
exit(1);
}
v4 = argv[2];
if ( strlen(v4) > 0xFF )
{
fwrite("ERROR: Local filename exceeds maximum allowed length\n", 1uLL, 0x35uLL, stderr);
exit(1);
}
v5 = atoi(argv[3]);
v6 = atof(argv[4]);
sendErr_init(1LL, 1LL, 0LL, 0LL, v6);
v7 = atoi(argv[5]);
v8 = atoi(argv[7]);
rcopy_runloop((__int64)argv[6], v8, v3, v4, v5, v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
const char *v4; // r14
int v5; // ebp
double v6; // xmm0_8
int v7; // er12
unsigned __int16 v8; // ax

if ( argc != 8 )
{
fprintf(
stderr,
"usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n",
*argv);
exit(-2);
}
v3 = (char *)argv[1];
if ( strlen(v3) > 0xFF )
{
fwrite("ERROR: Remote filename exceeds maximum allowed length\n", 1uLL, 0x36uLL, stderr);
exit(1);
}
v4 = argv[2];
if ( strlen(v4) > 0xFF )
{
fwrite("ERROR: Local filename exceeds maximum allowed length\n", 1uLL, 0x35uLL, stderr);
exit(1);
}
v5 = atoi(argv[3]);
v6 = atof(argv[4]);
sendErr_init(1LL, 1LL, 0LL, 0LL, v6);
v7 = atoi(argv[5]);
v8 = atoi(argv[7]);
func7((__int64)argv[6], v8, v3, v4, v5, v7);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 char *remote_file_name;
 char *local_file_name;
 uint32_t buf_size;
 uint32_t window_size;
 if (argc != 8) {
       fprintf(stderr, "usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n" , argv[0]);
       exit(-2);
    }
    if (strlen(argv[1]) > 255) {
     fprintf(stderr, "ERROR: Remote filename exceeds maximum allowed length\n");
     exit(1);
    }
    remote_file_name = argv[1];
    if (strlen(argv[2]) > 255) {
     fprintf(stderr, "ERROR: Local filename exceeds maximum allowed length\n");
     exit(1);
    }
    local_file_name = argv[2];
    buf_size = atoi(argv[3]);
    sendErr_init(atof(argv[4]),
  1,
  1,
  0,
  0);
    window_size = atoi(argv[5]);
    rcopy_runloop(argv[6], atoi(argv[7]), remote_file_name, local_file_name, buf_size, window_size);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char *remote_file_name;
 char *local_file_name;
 unsigned int buf_size;
 unsigned int window_size;
 if (argc != 8) {
       fprintf(stderr, "usage: %s <remote_file_name> <local_file_name> <buf_size> <error_rate> <window_size> <remote-machine> <remote-port>\n" , argv[0]);
       exit(-2);
    }
    if (strlen(argv[1]) > 255) {
     fprintf(stderr, "ERROR: Remote filename exceeds maximum allowed length\n");
     exit(1);
    }
    remote_file_name = argv[1];
    if (strlen(argv[2]) > 255) {
     fprintf(stderr, "ERROR: Local filename exceeds maximum allowed length\n");
     exit(1);
    }
    local_file_name = argv[2];
    buf_size = atoi(argv[3]);
    sendErr_init(atof(argv[4]),
  1,
  1,
  0,
  0);
    window_size = atoi(argv[5]);
    rcopy_runloop(argv[6], atoi(argv[7]), remote_file_name, local_file_name, buf_size, window_size);
 return 0;
}

variable_types: argc int


arp

decompiled_code:
int __fastcall arp(__int64 a1)
{
unsigned __int16 v1; // ax
char *v2; // rax
struct in_addr *v3; // rbx
char *v4; // rax
const struct ether_addr *v5; // rbx
char *v6; // rax
char *v7; // rax

puts("\tARP header");
printf("\t\tOpcode: ");
v1 = _bswap_16(*(_WORD *)(a1 + 6));
*(_WORD *)(a1 + 6) = v1;
switch ( v1 )
{
case 1u:
printf("Request");
break;
case 2u:
printf("Reply");
break;
case 3u:
printf("Rev Request");
break;
case 4u:
printf("Rev Reply");
break;
case 8u:
printf("INV Request");
break;
case 9u:
printf("INV Reply");
break;
default:
printf("Unknown (%d)", v1);
break;
}
putchar(10);
v2 = ether_ntoa((const struct ether_addr *)(a1 + 8));
printf("\t\tSender MAC: %s\n", v2);
v3 = (struct in_addr *)(*(unsigned __int8 *)(a1 + 4) + a1 + 8);
v4 = inet_ntoa((struct in_addr)v3->s_addr);
printf("\t\tSender IP: %s\n", v4);
v5 = (const struct ether_addr *)((char *)v3 + *(unsigned __int8 *)(a1 + 5));
v6 = ether_ntoa(v5);
printf("\t\tTarget MAC: %s\n", v6);
v7 = inet_ntoa(*(struct in_addr *)&v5->ether_addr_octet[*(unsigned __int8 *)(a1 + 4)]);
return printf("\t\tTarget IP: %s\n", v7);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
unsigned __int16 v1; // ax
char *v2; // rax
struct in_addr *v3; // rbx
char *v4; // rax
const struct ether_addr *v5; // rbx
char *v6; // rax
char *v7; // rax

puts("\tARP header");
printf("\t\tOpcode: ");
v1 = func0(*(_WORD *)(a1 + 6));
*(_WORD *)(a1 + 6) = v1;
switch ( v1 )
{
case 1u:
printf("Request");
break;
case 2u:
printf("Reply");
break;
case 3u:
printf("Rev Request");
break;
case 4u:
printf("Rev Reply");
break;
case 8u:
printf("INV Request");
break;
case 9u:
printf("INV Reply");
break;
default:
printf("Unknown (%d)", v1);
break;
}
putchar(10);
v2 = ether_ntoa((const struct ether_addr *)(a1 + 8));
printf("\t\tSender MAC: %s\n", v2);
v3 = (struct in_addr *)(*(unsigned __int8 *)(a1 + 4) + a1 + 8);
v4 = inet_ntoa((struct in_addr)v3->s_addr);
printf("\t\tSender IP: %s\n", v4);
v5 = (const struct ether_addr *)((char *)v3 + *(unsigned __int8 *)(a1 + 5));
v6 = ether_ntoa(v5);
printf("\t\tTarget MAC: %s\n", v6);
v7 = inet_ntoa(*(struct in_addr *)&v5->ether_addr_octet[*(unsigned __int8 *)(a1 + 4)]);
return printf("\t\tTarget IP: %s\n", v7);
}

original_code:
void arp(uint8_t *packetData, int packetLength)
{
 struct ARPFrameHeader *header = (struct ARPFrameHeader *) packetData;
 uint8_t *senderMAC, *targetMAC;
 in_addr_t *senderIP, *targetIP;
 struct in_addr netAddr;
 printf("\tARP header\n");
 printf("\t\tOpcode: ");
 header->arp_opcode = ntohs(header->arp_opcode);
 switch (header->arp_opcode) {
  case 1:
   printf("Request");
  break;
  case 2:
   printf("Reply");
  break;
  case 3:
   printf("Rev Request");
  break;
  case 4:
   printf("Rev Reply");
  break;
  case 8:
   printf("INV Request");
  break;
  case 9:
   printf("INV Reply");
  break;
  default:
   printf("Unknown (%d)", header->arp_opcode);
  break;
 }
 printf("\n");
 senderMAC = (uint8_t *)header+sizeof(struct ARPFrameHeader);
 printf("\t\tSender MAC: %s\n", ether_ntoa((struct ether_addr *)senderMAC));
 senderIP = (in_addr_t *)(senderMAC + header->arp_hardware_length);
 netAddr.s_addr = (in_addr_t)*senderIP;
 printf("\t\tSender IP: %s\n", inet_ntoa(netAddr));
 targetMAC = (uint8_t *)senderIP+header->arp_protocol_length;
 printf("\t\tTarget MAC: %s\n", ether_ntoa((struct ether_addr *)targetMAC));
 targetIP = (in_addr_t *)(targetMAC + header->arp_hardware_length);
 netAddr.s_addr = (in_addr_t)*targetIP;
 printf("\t\tTarget IP: %s\n", inet_ntoa(netAddr));
}

canonical_original_code:
void arp(unsigned char *packetData, int packetLength)
{
 struct ARPFrameHeader *header = (struct ARPFrameHeader *) packetData;
 unsigned char *senderMAC; unsigned char *targetMAC;
 unsigned int *senderIP; unsigned int *targetIP;
 struct in_addr netAddr;
 printf("\tARP header\n");
 printf("\t\tOpcode: ");
 header->arp_opcode = ntohs(header->arp_opcode);
 switch (header->arp_opcode) {
  case 1:
   printf("Request");
  break;
  case 2:
   printf("Reply");
  break;
  case 3:
   printf("Rev Request");
  break;
  case 4:
   printf("Rev Reply");
  break;
  case 8:
   printf("INV Request");
  break;
  case 9:
   printf("INV Reply");
  break;
  default:
   printf("Unknown (%d)", header->arp_opcode);
  break;
 }
 printf("\n");
 senderMAC = (unsigned char *)header+sizeof(struct ARPFrameHeader);
 printf("\t\tSender MAC: %s\n", ether_ntoa((struct ether_addr *)senderMAC));
 senderIP = (unsigned int *)(senderMAC + header->arp_hardware_length);
 netAddr.s_addr = (in_addr_t)*senderIP;
 printf("\t\tSender IP: %s\n", inet_ntoa(netAddr));
 targetMAC = (unsigned char *)senderIP+header->arp_protocol_length;
 printf("\t\tTarget MAC: %s\n", ether_ntoa((struct ether_addr *)targetMAC));
 targetIP = (unsigned int *)(targetMAC + header->arp_hardware_length);
 netAddr.s_addr = (in_addr_t)*targetIP;
 printf("\t\tTarget IP: %s\n", inet_ntoa(netAddr));
}

variable_types: struct ARPFrameHeader { unsigned short arp_hardware_format; unsigned short arp_protocol_format; unsigned char arp_hardware_length; unsigned char arp_protocol_length; unsigned short arp_opcode; } *

variable_types:
senderMAC
unsigned char *

variable_types:
targetMAC
unsigned char *

variable_types:
senderIP
unsigned int *

variable_types:
targetIP
unsigned int *

variable_types:
netAddr
struct in_addr { unsigned int s_addr; }

user_defined_types: struct ether_addr { unsigned char ether_addr_octet[6]; }


clientNamed

decompiled_code:
__int64 __fastcall clientNamed(char *s1)
{
__int64 i; // rbx
const char *v2; // rsi

for ( i = *(_QWORD *)clientList; i; i = *(_QWORD *)(i + 16) )
{
v2 = *(const char **)(i + 8);
if ( v2 && !strcmp(s1, v2) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1)
{
__int64 i; // rbx
const char *v2; // rsi

for ( i = *(_QWORD *)clientList; i; i = *(_QWORD *)(i + 16) )
{
v2 = *(const char **)(i + 8);
if ( v2 && !strcmp(s1, v2) )
break;
}
return i;
}

original_code:
struct ClientNode *clientNamed(char *name)
{
 struct ClientNode *node = clientList->firstClient;
 while (node != ((void *)0)) {
  if (node->clientName && (strcmp(name, node->clientName) == 0)) {
   break;
  }
  node = node->nextClient;
 }
 return node;
}

variable_types:
name
char *

variable_types: struct ClientNode { int clientSocket; char *clientName; struct ClientNode *nextClient; struct ChatHeader { unsigned int sequenceNumber; unsigned short checksum; unsigned char flag; } *packetData; long packetLength; unsigned int sequenceNumber; } *

user_defined_types: struct ClientNode { int clientSocket; char *clientName; struct ClientNode *nextClient; struct ChatHeader *packetData; long packetLength; unsigned int sequenceNumber; }