decompiled_code:
int SOCKET_prepare()
{
int *v1; // rax
int *v2; // rax
int *v3; // rax
int *v4; // rax
__int64 v5; // rsi
__int64 v6; // rsi
__int64 v7; // rsi
int optval; // [rsp+4h] [rbp-24h] BYREF
__int64 v9; // [rsp+8h] [rbp-20h] BYREF
__int64 v10[3]; // [rsp+10h] [rbp-18h] BYREF

v9 = 0LL;
optval = 1;
v10[0] = 0LL;
v10[1] = 20000LL;
memset(&master, 0, 0x80uLL);
memset(&read_fds, 0, sizeof(read_fds));
setuid(0);
setgid(0);
if ( setsockopt(socket_server, 1, 2, &optval, 4u) == -1 )
{
v1 = _errno_location();
printf("setsockopt( SO_REUSEADDR ) error: %d.\n", (unsigned int)*v1);
}
if ( bind(socket_server, &server_address, 0x10u) == -1 )
{
v5 = (unsigned int)*_errno_location();
printf("bind error: %d.\n", v5);
goto LABEL_14;
}
if ( setsockopt(socket_server, 1, 20, v10, 0x10u) == -1 )
{
v4 = _errno_location();
printf("setsockopt( SO_RCVTIMEO ) error: %d.\n", (unsigned int)*v4);
}
if ( setsockopt(socket_server, 1, 21, v10, 0x10u) == -1 )
{
v3 = _errno_location();
printf("setsockopt( SO_SNDTIMEO ) error: %d.\n", (unsigned int)*v3);
}
if ( setsockopt(socket_server, 6, 1, &optval, 4u) == -1 )
{
v2 = _errno_location();
printf("setsockopt( TCP_NODELAY ) error: %d.\n", (unsigned int)*v2);
}
if ( fcntl(socket_server, 4, &v9) == -1 )
{
v7 = (unsigned int)*_errno_location();
printf("ioctlsocket error: %d.\n", v7);
LABEL_14:
SOCKET_stop();
exit(1);
}
if ( listen(socket_server, 1024) == -1 )
{
v6 = (unsigned int)*_errno_location();
printf("listen error: %d.\n", v6);
goto LABEL_14;
}
return puts("ok.");
}

canonical_decompiled_code:
int func6()
{
int *v1; // rax
int *v2; // rax
int *v3; // rax
int *v4; // rax
__int64 v5; // rsi
__int64 v6; // rsi
__int64 v7; // rsi
int optval; // [rsp+4h] [rbp-24h] BYREF
__int64 v9; // [rsp+8h] [rbp-20h] BYREF
__int64 v10[3]; // [rsp+10h] [rbp-18h] BYREF

v9 = 0LL;
optval = 1;
v10[0] = 0LL;
v10[1] = 20000LL;
memset(&master, 0, 0x80uLL);
memset(&read_fds, 0, sizeof(read_fds));
setuid(0);
setgid(0);
if ( setsockopt(socket_server, 1, 2, &optval, 4u) == -1 )
{
v1 = _errno_location();
printf("setsockopt( SO_REUSEADDR ) error: %d.\n", (unsigned int)*v1);
}
if ( bind(socket_server, &server_address, 0x10u) == -1 )
{
v5 = (unsigned int)*_errno_location();
printf("bind error: %d.\n", v5);
goto LABEL_14;
}
if ( setsockopt(socket_server, 1, 20, v10, 0x10u) == -1 )
{
v4 = _errno_location();
printf("setsockopt( SO_RCVTIMEO ) error: %d.\n", (unsigned int)*v4);
}
if ( setsockopt(socket_server, 1, 21, v10, 0x10u) == -1 )
{
v3 = _errno_location();
printf("setsockopt( SO_SNDTIMEO ) error: %d.\n", (unsigned int)*v3);
}
if ( setsockopt(socket_server, 6, 1, &optval, 4u) == -1 )
{
v2 = _errno_location();
printf("setsockopt( TCP_NODELAY ) error: %d.\n", (unsigned int)*v2);
}
if ( fcntl(socket_server, 4, &v9) == -1 )
{
v7 = (unsigned int)*_errno_location();
printf("ioctlsocket error: %d.\n", v7);
LABEL_14:
func4();
exit(1);
}
if ( listen(socket_server, 1024) == -1 )
{
v6 = (unsigned int)*_errno_location();
printf("listen error: %d.\n", v6);
goto LABEL_14;
}
return puts("ok.");
}

original_code:
static void SOCKET_prepare( void ) {
 unsigned long b = 0;
 int i = 1;
 int wsa_result = 0;
 struct timeval tv = {0, 0};
 tv.tv_sec = 0;
 tv.tv_usec = 20000;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&master)->__fds_bits)[0]) : "memory"); } while (0);
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
 setuid( 0 );
 setgid( 0 );
 if( setsockopt( socket_server, 1, 2, &i, sizeof( int ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( SO_REUSEADDR ) error: %d.\n", wsa_result );
 }
 if ( bind( socket_server, ( struct sockaddr* )&server_address, sizeof( server_address ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "bind error: %d.\n", wsa_result );
  SOCKET_stop();
  exit( 1 );
 }
 if( setsockopt( socket_server, 1, 20, ( char* )&tv, sizeof( struct timeval ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( SO_RCVTIMEO ) error: %d.\n", wsa_result );
 }
 if( setsockopt( socket_server, 1, 21, ( char* )&tv, sizeof( struct timeval ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( SO_SNDTIMEO ) error: %d.\n", wsa_result );
 }
 if( setsockopt( socket_server, IPPROTO_TCP, 1, ( char * )&i, sizeof( i ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( TCP_NODELAY ) error: %d.\n", wsa_result );
 }
 if( fcntl( socket_server, 4, &b ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "ioctlsocket error: %d.\n", wsa_result );
  SOCKET_stop();
  exit( 1 );
 }
 if( listen( socket_server, 1024 ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "listen error: %d.\n", wsa_result );
  SOCKET_stop();
  exit( 1 );
 }
 printf( "ok.\n" );
}

canonical_original_code:
static void SOCKET_prepare() {
 unsigned long b = 0;
 int i = 1;
 int wsa_result = 0;
 struct timeval tv = {0, 0};
 tv.tv_sec = 0;
 tv.tv_usec = 20000;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&master)->__fds_bits)[0]) : "memory"); } while (0);
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
 setuid( 0 );
 setgid( 0 );
 if( setsockopt( socket_server, 1, 2, &i, sizeof( int ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( SO_REUSEADDR ) error: %d.\n", wsa_result );
 }
 if ( bind( socket_server, ( struct sockaddr * )&server_address, sizeof( server_address ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "bind error: %d.\n", wsa_result );
  SOCKET_stop();
  exit( 1 );
 }
 if( setsockopt( socket_server, 1, 20, ( char * )&tv, sizeof( struct timeval ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( SO_RCVTIMEO ) error: %d.\n", wsa_result );
 }
 if( setsockopt( socket_server, 1, 21, ( char * )&tv, sizeof( struct timeval ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( SO_SNDTIMEO ) error: %d.\n", wsa_result );
 }
 if( setsockopt( socket_server, IPPROTO_TCP, 1, ( char * )&i, sizeof( i ) ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "setsockopt( TCP_NODELAY ) error: %d.\n", wsa_result );
 }
 if( fcntl( socket_server, 4, &b ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "ioctlsocket error: %d.\n", wsa_result );
  SOCKET_stop();
  exit( 1 );
 }
 if( listen( socket_server, 1024 ) == ( -1 ) ) {
  wsa_result = (*__errno_location ());
  printf( "listen error: %d.\n", wsa_result );
  SOCKET_stop();
  exit( 1 );
 }
 printf( "ok.\n" );
}

variable_types:
b
unsigned long

variable_types: i int

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

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


repo: OrionExplorer/c-server-core

decompiled_code:
__int64 __fastcall SOCKET_release(__int64 a1)
{
__int64 result; // rax

*(_QWORD *)(a1 + 152) = -1LL;
result = 0xFFFFFFFFLL;
*(__int16 *)((char *)&word_80098 + a1) = -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 result; // rax

*(_QWORD *)(a1 + 152) = -1LL;
result = 0xFFFFFFFFLL;
*(__int16 *)((char *)&word_80098 + a1) = -1;
return result;
}

original_code:
void SOCKET_release( COMMUNICATION_SESSION *communication_session ) {
 communication_session->socket_descriptor = -1;
 communication_session->data_length = -1;
 communication_session->keep_alive = -1;
}

canonical_original_code:
void SOCKET_release(struct COMMUNICATION_SESSION *communication_session) {
 communication_session->socket_descriptor = -1;
 communication_session->data_length = -1;
 communication_session->keep_alive = -1;
}

variable_types: struct COMMUNICATION_SESSION { 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]; } address; int socket; struct fd_set { long __fds_bits[-1]; } socket_data; unsigned int data_length; int socket_descriptor; char *content[65535]; short keep_alive; } *

return_type: void

user_defined_types: struct COMMUNICATION_SESSION { struct sockaddr_in address; int socket; struct fd_set socket_data; unsigned int data_length; int socket_descriptor; char *content[65535]; short keep_alive; }

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 SOCKET_disconnect_client(__int64 a1)
{
int v1; // er8
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 156);
if ( v1 == -1 )
result = SOCKET_release(a1);
else
result = SOCKET_close(v1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
int v1; // er8
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 156);
if ( v1 == -1 )
result = func7(a1);
else
result = func3(v1);
return result;
}

original_code:
void SOCKET_disconnect_client( COMMUNICATION_SESSION *communication_session ) {
 if( communication_session->socket_descriptor != ( -1 ) ) {
  SOCKET_close( communication_session->socket_descriptor );
 } else {
  SOCKET_release( communication_session );
 }
}

canonical_original_code:
void SOCKET_disconnect_client(struct COMMUNICATION_SESSION *communication_session) {
 if( communication_session->socket_descriptor != ( -1 ) ) {
  SOCKET_close( communication_session->socket_descriptor );
 } else {
  SOCKET_release( communication_session );
 }
}


decompiled_code:
void __fastcall SOCKET_send(__int64 a1, int *a2, const char *a3, int a4)
{
char *v6; // r13
int v7; // eax

v6 = (char *)calloc(0xFFFFuLL, 1uLL);
if ( a4 == -1 )
a4 = strlen(a3);
strncpy(v6, a3, 0xFFFFuLL);
v7 = send(*a2, v6, a4, 0);
*(_DWORD *)(a1 + 152) = v7;
if ( !v7 )
SOCKET_disconnect_client(a1);
free(v6);
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int *a2, const char *a3, int a4)
{
char *v6; // r13
int v7; // eax

v6 = (char *)calloc(0xFFFFuLL, 1uLL);
if ( a4 == -1 )
a4 = strlen(a3);
strncpy(v6, a3, 0xFFFFuLL);
v7 = send(*a2, v6, a4, 0);
*(_DWORD *)(a1 + 152) = v7;
if ( !v7 )
func8(a1);
free(v6);
}

original_code:
void SOCKET_send( COMMUNICATION_SESSION *communication_session, CONNECTED_CLIENT *client, const char *data, unsigned int data_size ) {
 int _data_size = data_size;
 char *data_to_send = ( char * )calloc( 65535, sizeof( char ) );
 if( _data_size == -1 ) {
  _data_size = strlen( data );
 }
 strncpy( data_to_send, data, 65535 );
 if( ( communication_session->data_length = send( client->socket_descriptor, data_to_send, _data_size, 0 ) ) <= 0 ) {
  SOCKET_disconnect_client( communication_session );
 }
 free( data_to_send );
 data_to_send = ((void *)0);
}

canonical_original_code:
void SOCKET_send(struct COMMUNICATION_SESSION *communication_session, struct CONNECTED_CLIENT *client, char *data, unsigned int data_size) {
 int _data_size = data_size;
 char *data_to_send = ( char * )calloc( 65535, sizeof( char ) );
 if( _data_size == -1 ) {
  _data_size = strlen( data );
 }
 strncpy( data_to_send, data, 65535 );
 if( ( communication_session->data_length = send( client->socket_descriptor, data_to_send, _data_size, 0 ) ) <= 0 ) {
  SOCKET_disconnect_client( communication_session );
 }
 free( data_to_send );
 data_to_send = ((void *)0);
}

variable_types: struct CONNECTED_CLIENT { int socket_descriptor; short connected; } *

variable_types:
data
char *


decompiled_code:
char *__fastcall SOCKET_get_remote_ip(const struct sockaddr *a1)
{
*(_OWORD *)ip_addr_0 = 0LL;
getnameinfo(a1, 0x10u, ip_addr_0, 0x10u, 0LL, 0, 1);
return ip_addr_0;
}

canonical_decompiled_code:
char *__fastcall func10(const struct sockaddr *a1)
{
*(_OWORD *)ip_addr_0 = 0LL;
getnameinfo(a1, 0x10u, ip_addr_0, 0x10u, 0LL, 0, 1);
return ip_addr_0;
}

original_code:
char* SOCKET_get_remote_ip( COMMUNICATION_SESSION *communication_session ) {
 static char ip_addr[ 16 ];
 memset( ip_addr, '\0', 16 );
 getnameinfo( ( struct sockaddr * )&communication_session->address, sizeof( communication_session->address ), ip_addr, sizeof( ip_addr ), ((void *)0), 0, 1 );
 return ( ( char* )&ip_addr );
}

canonical_original_code:
char *SOCKET_get_remote_ip(struct COMMUNICATION_SESSION *communication_session) {
 static char ip_addr[16];
 memset( ip_addr, '\0', 16 );
 getnameinfo( ( struct sockaddr * )&communication_session->address, sizeof( communication_session->address ), ip_addr, sizeof( ip_addr ), ((void *)0), 0, 1 );
 return ( ( char * )&ip_addr );
}

return_type: char *


decompiled_code:
int __fastcall SOCKET_register_client(unsigned int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( connected_clients[2 * v1] )
{
if ( ++v1 == 1024 )
return v1;
}
connected_clients[2 * (int)v1] = a1;
LOWORD(connected_clients[2 * (int)v1 + 1]) = 1;
LODWORD(v1) = printf("Client connected with descriptor %d.\n", a1);
return v1;
}

canonical_decompiled_code:
int __fastcall func11(unsigned int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( connected_clients[2 * v1] )
{
if ( ++v1 == 1024 )
return v1;
}
connected_clients[2 * (int)v1] = a1;
LOWORD(connected_clients[2 * (int)v1 + 1]) = 1;
LODWORD(v1) = printf("Client connected with descriptor %d.\n", a1);
return v1;
}

original_code:
void SOCKET_register_client( int socket_descriptor ) {
 int i;
 for( i = 0; i < 1024; i++ ){
  if( connected_clients[ i ].socket_descriptor == 0 ) {
   connected_clients[ i ].socket_descriptor = socket_descriptor;
   connected_clients[ i ].connected = 1;
   printf("Client connected with descriptor %d.\n", socket_descriptor );
   return;
  }
 }
}

canonical_original_code:
void SOCKET_register_client(int socket_descriptor) {
 int i;
 for( i = 0; i < 1024; i++ ){
  if( connected_clients[ i ].socket_descriptor == 0 ) {
   connected_clients[ i ].socket_descriptor = socket_descriptor;
   connected_clients[ i ].connected = 1;
   printf("Client connected with descriptor %d.\n", socket_descriptor );
   return;
  }
 }
}


decompiled_code:
int __fastcall SOCKET_unregister_client(unsigned int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( connected_clients[2 * v1] != a1 )
{
if ( ++v1 == 1024 )
return v1;
}
connected_clients[2 * (int)v1] = 0;
LOWORD(connected_clients[2 * (int)v1 + 1]) = 0;
LODWORD(v1) = printf("Client disconnected: %d.\n", a1);
return v1;
}

canonical_decompiled_code:
int __fastcall func12(unsigned int a1)
{
__int64 v1; // rax

v1 = 0LL;
while ( connected_clients[2 * v1] != a1 )
{
if ( ++v1 == 1024 )
return v1;
}
connected_clients[2 * (int)v1] = 0;
LOWORD(connected_clients[2 * (int)v1 + 1]) = 0;
LODWORD(v1) = printf("Client disconnected: %d.\n", a1);
return v1;
}

original_code:
void SOCKET_unregister_client( int socket_descriptor ) {
 int i;
 for( i = 0; i < 1024; i++ ){
  if( connected_clients[ i ].socket_descriptor == socket_descriptor ) {
   connected_clients[ i ].socket_descriptor = 0;
   connected_clients[ i ].connected = 0;
   printf("Client disconnected: %d.\n", socket_descriptor );
   break;
  }
 }
}

canonical_original_code:
void SOCKET_unregister_client(int socket_descriptor) {
 int i;
 for( i = 0; i < 1024; i++ ){
  if( connected_clients[ i ].socket_descriptor == socket_descriptor ) {
   connected_clients[ i ].socket_descriptor = 0;
   connected_clients[ i ].connected = 0;
   printf("Client disconnected: %d.\n", socket_descriptor );
   break;
  }
 }
}


decompiled_code:
int __fastcall SOCKET_process(int fd)
{
int *v1; // rax
int *v2; // rbx
unsigned int v3; // eax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
*((_DWORD *)&communication_session_ + 39) = fd;
memset((void *)(unsigned int)((_DWORD)&communication_session_ + 160), 0, 0xFFFFuLL);
v3 = recv(fd, (void *)(unsigned int)((_DWORD)&communication_session_ + 160), 0xFFFFuLL, 0);
*((_DWORD *)&communication_session_ + 38) = v3;
if ( *v2 <= 1 && v3 )
return printf("Received packet with size %ld.\n", v3);
SOCKET_unregister_client(fd);
return SOCKET_close(fd);
}

canonical_decompiled_code:
int __fastcall func13(int fd)
{
int *v1; // rax
int *v2; // rbx
unsigned int v3; // eax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
*((_DWORD *)&communication_session_ + 39) = fd;
memset((void *)(unsigned int)((_DWORD)&communication_session_ + 160), 0, 0xFFFFuLL);
v3 = recv(fd, (void *)(unsigned int)((_DWORD)&communication_session_ + 160), 0xFFFFuLL, 0);
*((_DWORD *)&communication_session_ + 38) = v3;
if ( *v2 <= 1 && v3 )
return printf("Received packet with size %ld.\n", v3);
func12(fd);
return func3(fd);
}

original_code:
static void SOCKET_process( int socket_fd ) {
 CONNECTED_CLIENT *client = SOCKET_find_client( socket_fd );
 extern int (*__errno_location ());
 (*__errno_location ()) = 0;
 communication_session_.socket_descriptor = socket_fd;
 memset( communication_session_.content, '\0', 65535 );
 communication_session_.data_length = recv( ( int )socket_fd, communication_session_.content, 65535, 0 );
 if( (*__errno_location ()) > 1) {
  SOCKET_unregister_client( socket_fd );
  SOCKET_close( socket_fd );
 } else {
  if ( communication_session_.data_length <= 0 ) {
   SOCKET_unregister_client( socket_fd );
   SOCKET_close( socket_fd );
  } else {
   printf( "Received packet with size %ld.\n", communication_session_.data_length );
  }
 }
}

canonical_original_code:
static void SOCKET_process(int socket_fd) {
 struct CONNECTED_CLIENT *client = SOCKET_find_client( socket_fd );
 extern int *__errno_location();
 (*__errno_location ()) = 0;
 communication_session_.socket_descriptor = socket_fd;
 memset( communication_session_.content, '\0', 65535 );
 communication_session_.data_length = recv( ( int )socket_fd, communication_session_.content, 65535, 0 );
 if( (*__errno_location ()) > 1) {
  SOCKET_unregister_client( socket_fd );
  SOCKET_close( socket_fd );
 } else {
  if ( communication_session_.data_length <= 0 ) {
   SOCKET_unregister_client( socket_fd );
   SOCKET_close( socket_fd );
  } else {
   printf( "Received packet with size %ld.\n", communication_session_.data_length );
  }
 }
}

user_defined_types: struct CONNECTED_CLIENT { int socket_descriptor; short connected; }


decompiled_code:
void __noreturn SOCKET_run()
{
int v0; // ebx
int v1; // eax
__m128i v2; // xmm2
__m128i v3; // xmm3
__m128i v4; // xmm4
__m128i v5; // xmm5
__m128i v6; // xmm6
__m128i v7; // xmm7
int v8; // edi
socklen_t *v9; // rdx
int v10; // eax
int v11; // edx
struct timeval v12; // [rsp+0h] [rbp-28h] BYREF

v0 = socket_server;
v12.tv_sec = 1LL;
v1 = socket_server + 63;
fdmax = socket_server;
v12.tv_usec = 0LL;
if ( socket_server >= 0 )
v1 = socket_server;
*((_QWORD *)&master + (v1 >> 6)) |= 1LL << (socket_server % 64);
do
{
LABEL_4:
v2 = _mm_load_si128((const __m128i *)&master + 2);
v3 = _mm_load_si128((const __m128i *)&master + 3);
v4 = _mm_load_si128((const __m128i *)&master + 4);
v5 = _mm_load_si128((const __m128i *)&master + 5);
*(__m128i *)read_fds.fds_bits = _mm_load_si128((const __m128i *)&master);
v6 = _mm_load_si128((const __m128i *)&master + 6);
v7 = _mm_load_si128((const __m128i *)&master + 7);
*(__m128i *)&read_fds.fds_bits[2] = _mm_load_si128((const __m128i *)&master + 1);
*(__m128i *)&read_fds.fds_bits[4] = v2;
*(__m128i *)&read_fds.fds_bits[6] = v3;
*(__m128i *)&read_fds.fds_bits[8] = v4;
*(__m128i *)&read_fds.fds_bits[10] = v5;
*(__m128i *)&read_fds.fds_bits[12] = v6;
*(__m128i *)&read_fds.fds_bits[14] = v7;
if ( select(v0 + 1, &read_fds, 0LL, 0LL, &v12) == -1 )
{
SOCKET_stop();
exit(1);
}
v0 = fdmax;
}
while ( !fdmax );
while ( 1 )
{
if ( (read_fds.fds_bits[v0 / 64] & (1LL << (v0 % 64))) == 0 )
goto LABEL_8;
if ( socket_server != v0 )
break;
SOCKET_modify_clients_count(1);
v8 = socket_server;
*((_DWORD *)&communication_session_ + 38) = 16;
v10 = accept(v8, (struct sockaddr *)&communication_session_, v9);
newfd = v10;
*((_DWORD *)&communication_session_ + 39) = v10;
if ( v10 == -1 )
{
puts("Socket closed.");
goto LABEL_8;
}
SOCKET_register_client(v10);
v11 = newfd;
*((_QWORD *)&master + newfd / 64) |= 1LL << (newfd % 64);
if ( v11 <= fdmax )
{
LABEL_8:
if ( !--v0 )
goto LABEL_14;
}
else
{
fdmax = v11;
if ( !--v0 )
{
LABEL_14:
v0 = fdmax;
goto LABEL_4;
}
}
}
SOCKET_process(v0);
goto LABEL_8;
}

canonical_decompiled_code:
void __noreturn func14()
{
int v0; // ebx
int v1; // eax
__m128i v2; // xmm2
__m128i v3; // xmm3
__m128i v4; // xmm4
__m128i v5; // xmm5
__m128i v6; // xmm6
__m128i v7; // xmm7
int v8; // edi
socklen_t *v9; // rdx
int v10; // eax
int v11; // edx
struct timeval v12; // [rsp+0h] [rbp-28h] BYREF

v0 = socket_server;
v12.tv_sec = 1LL;
v1 = socket_server + 63;
fdmax = socket_server;
v12.tv_usec = 0LL;
if ( socket_server >= 0 )
v1 = socket_server;
*((_QWORD *)&master + (v1 >> 6)) |= 1LL << (socket_server % 64);
do
{
LABEL_4:
v2 = _mm_load_si128((const __m128i *)&master + 2);
v3 = _mm_load_si128((const __m128i *)&master + 3);
v4 = _mm_load_si128((const __m128i *)&master + 4);
v5 = _mm_load_si128((const __m128i *)&master + 5);
*(__m128i *)read_fds.fds_bits = _mm_load_si128((const __m128i *)&master);
v6 = _mm_load_si128((const __m128i *)&master + 6);
v7 = _mm_load_si128((const __m128i *)&master + 7);
*(__m128i *)&read_fds.fds_bits[2] = _mm_load_si128((const __m128i *)&master + 1);
*(__m128i *)&read_fds.fds_bits[4] = v2;
*(__m128i *)&read_fds.fds_bits[6] = v3;
*(__m128i *)&read_fds.fds_bits[8] = v4;
*(__m128i *)&read_fds.fds_bits[10] = v5;
*(__m128i *)&read_fds.fds_bits[12] = v6;
*(__m128i *)&read_fds.fds_bits[14] = v7;
if ( select(v0 + 1, &read_fds, 0LL, 0LL, &v12) == -1 )
{
func4();
exit(1);
}
v0 = fdmax;
}
while ( !fdmax );
while ( 1 )
{
if ( (read_fds.fds_bits[v0 / 64] & (1LL << (v0 % 64))) == 0 )
goto LABEL_8;
if ( socket_server != v0 )
break;
func2(1);
v8 = socket_server;
*((_DWORD *)&communication_session_ + 38) = 16;
v10 = accept(v8, (struct sockaddr *)&communication_session_, v9);
newfd = v10;
*((_DWORD *)&communication_session_ + 39) = v10;
if ( v10 == -1 )
{
puts("Socket closed.");
goto LABEL_8;
}
func11(v10);
v11 = newfd;
*((_QWORD *)&master + newfd / 64) |= 1LL << (newfd % 64);
if ( v11 <= fdmax )
{
LABEL_8:
if ( !--v0 )
goto LABEL_14;
}
else
{
fdmax = v11;
if ( !--v0 )
{
LABEL_14:
v0 = fdmax;
goto LABEL_4;
}
}
}
func13(v0);
goto LABEL_8;
}

original_code:
void SOCKET_run( void ) {
 register int i = 0;
 struct timeval tv;
 pthread_t sthread;
 ((void) (((&master)->__fds_bits)[((socket_server) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_server) % (8 * (int) sizeof (__fd_mask)))))));
 tv.tv_sec = 1;
 tv.tv_usec = 0;
 fdmax = socket_server;
 for( ;"elvis presley lives"; ) {
  read_fds = master;
  if( select( fdmax+1, &read_fds, ((void *)0), ((void *)0), &tv ) == -1 ) {
   SOCKET_stop();
   exit( 1 );
  }
  i = fdmax+1;
  while( --i ) {
   if( ((((&read_fds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
    if( i == socket_server ) {
     SOCKET_modify_clients_count( 1 );
     communication_session_.data_length = sizeof( struct sockaddr );
     newfd = accept( socket_server, ( struct sockaddr* )&communication_session_.address, &communication_session_.data_length );
     communication_session_.socket_descriptor = newfd;
     if( newfd == -1 ) {
      printf( "Socket closed.\n" );
     } else {
      SOCKET_register_client( newfd );
      ((void) (((&master)->__fds_bits)[((newfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((newfd) % (8 * (int) sizeof (__fd_mask)))))));
      if( newfd > fdmax ) {
       fdmax = newfd;
      }
     }
    } else {
     SOCKET_process( i );
    }
   }
  }
 }
}

canonical_original_code:
void SOCKET_run() {
 register int i = 0;
 struct timeval tv;
 unsigned long sthread;
 ((void) (((&master)->__fds_bits)[((socket_server) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_server) % (8 * (int) sizeof (__fd_mask)))))));
 tv.tv_sec = 1;
 tv.tv_usec = 0;
 fdmax = socket_server;
 for( ;"elvis presley lives"; ) {
  read_fds = master;
  if( select( fdmax+1, &read_fds, ((void *)0), ((void *)0), &tv ) == -1 ) {
   SOCKET_stop();
   exit( 1 );
  }
  i = fdmax+1;
  while( --i ) {
   if( ((((&read_fds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
    if( i == socket_server ) {
     SOCKET_modify_clients_count( 1 );
     communication_session_.data_length = sizeof( struct sockaddr );
     newfd = accept( socket_server, ( struct sockaddr * )&communication_session_.address, &communication_session_.data_length );
     communication_session_.socket_descriptor = newfd;
     if( newfd == -1 ) {
      printf( "Socket closed.\n" );
     } else {
      SOCKET_register_client( newfd );
      ((void) (((&master)->__fds_bits)[((newfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((newfd) % (8 * (int) sizeof (__fd_mask)))))));
      if( newfd > fdmax ) {
       fdmax = newfd;
      }
     }
    } else {
     SOCKET_process( i );
    }
   }
  }
 }
}

variable_types:
sthread
unsigned long

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
void __noreturn SOCKET_main()
{
SOCKET_initialization();
SOCKET_prepare();
SOCKET_run();
}

canonical_decompiled_code:
void __noreturn func15()
{
func5();
func6();
func14();
}

original_code:
void SOCKET_main( void ) {
 ( void )SOCKET_initialization();
 ( void )SOCKET_prepare();
 ( void )SOCKET_run();
 ( void )SOCKET_stop();
}

canonical_original_code:
void SOCKET_main() {
 ( void )SOCKET_initialization();
 ( void )SOCKET_prepare();
 ( void )SOCKET_run();
 ( void )SOCKET_stop();
}


decompiled_code:
int *__fastcall SOCKET_find_client(int a1)
{
int *result; // rax

result = 0LL;
while ( connected_clients[2 * (_QWORD)result] != a1 )
{
result = (int *)((char *)result + 1);
if ( result == (int *)&loc_400 )
return result;
}
return &connected_clients[2 * (int)result];
}

canonical_decompiled_code:
int *__fastcall func16(int a1)
{
int *result; // rax

result = 0LL;
while ( connected_clients[2 * (_QWORD)result] != a1 )
{
result = (int *)((char *)result + 1);
if ( result == (int *)&loc_400 )
return result;
}
return &connected_clients[2 * (int)result];
}

original_code:
CONNECTED_CLIENT* SOCKET_find_client( int socket_descriptor ) {
 int i;
 for( i = 0; i < 1024; i++ ){
  if( connected_clients[ i ].socket_descriptor == socket_descriptor ) {
   return &connected_clients[ i ];
  }
 }
}

canonical_original_code:
struct CONNECTED_CLIENT *SOCKET_find_client(int socket_descriptor) {
 int i;
 for( i = 0; i < 1024; i++ ){
  if( connected_clients[ i ].socket_descriptor == socket_descriptor ) {
   return &connected_clients[ i ];
  }
 }
}

return_type: struct CONNECTED_CLIENT { int socket_descriptor; short connected; } *


repo: Ornicare/CFilesEchanger

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

do
result = getchar();
while ( result != 10 && result != -1 );
return result;
}

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

do
result = getchar();
while ( result != 10 && result != -1 );
return result;
}

original_code:
void clean_stdin(void)
{
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != (-1));
}

canonical_original_code:
void clean_stdin()
{
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != (-1));
}

variable_types: c int


sendCommandToServer

decompiled_code:
ssize_t __fastcall sendCommandToServer(int fd, const char *a2)
{
char dest[18]; // [rsp+6h] [rbp-12h] BYREF

strcpy(dest, a2);
return write(fd, dest, 0xAuLL);
}

canonical_decompiled_code:
ssize_t __fastcall func2(int fd, const char *a2)
{
char dest[18]; // [rsp+6h] [rbp-12h] BYREF

strcpy(dest, a2);
return write(fd, dest, 0xAuLL);
}

original_code:
int sendCommandToServer(int sock, char* commande)
{
 char commandReader[10];
     sprintf(commandReader, "%s", commande);
 write(sock, commandReader, sizeof(commandReader));
}

canonical_original_code:
int sendCommandToServer(int sock, char *commande)
{
 char commandReader[10];
     sprintf(commandReader, "%s", commande);
 write(sock, commandReader, sizeof(commandReader));
}

variable_types: sock int

variable_types:
commande
char *

variable_types:
commandReader
char [10]

return_type: int


min

decompiled_code:
__int64 __fastcall min(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
if ( (int)a1 <= (int)a2 )
result = a1;
return result;
}

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

result = a2;
if ( (int)a1 <= (int)a2 )
result = a1;
return result;
}

original_code:
int min(int a, int b)
{
 int tmp = a;
 if(a>b) tmp=b;
 return tmp;
}

variable_types: a int

variable_types: b int

variable_types: tmp int


toLowerCase

decompiled_code:
char *__fastcall toLowerCase(char *a1)
{
int v2; // edi
_BYTE *v3; // rbx

v2 = *a1;
if ( (_BYTE)v2 )
{
v3 = a1;
do
{
*v3++ = tolower(v2);
v2 = (char)*v3;
}
while ( *v3 );
}
return a1;
}

canonical_decompiled_code:
char *__fastcall func4(char *a1)
{
int v2; // edi
_BYTE *v3; // rbx

v2 = *a1;
if ( (_BYTE)v2 )
{
v3 = a1;
do
{
*v3++ = tolower(v2);
v2 = (char)*v3;
}
while ( *v3 );
}
return a1;
}

original_code:
char * toLowerCase(char * word)
{
 int i;
 for (i = 0; word[i]; i++)
 word[i] = tolower(word[i]);
 return word;
}

canonical_original_code:
char *toLowerCase(char *word)
{
 int i;
 for (i = 0; word[i]; i++)
 word[i] = tolower(word[i]);
 return word;
}

variable_types:
word
char *


waitingForServerMessage

decompiled_code:
int __fastcall waitingForServerMessage(int a1, const char *a2)
{
int result; // eax
char buf[18]; // [rsp+6h] [rbp-12h] BYREF

read(a1, buf, 0xAuLL);
printf("READ : %s\n", buf);
result = strcmp(buf, a2);
if ( result )
{
puts("Erreur protocolaire");
result = 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1, const char *a2)
{
int result; // eax
char buf[18]; // [rsp+6h] [rbp-12h] BYREF

read(a1, buf, 0xAuLL);
printf("READ : %s\n", buf);
result = strcmp(buf, a2);
if ( result )
{
puts("Erreur protocolaire");
result = 1;
}
return result;
}

original_code:
int waitingForServerMessage(int sock, char* commande)
{
 char commandReader[10];
 read(sock, commandReader, sizeof(commandReader)); if(1) printf("READ : %s\n",commandReader);
 if(strcmp(commandReader,commande)!=0)
 {
  printf("Erreur protocolaire\n");
  return 1;
 }
 return 0;
}

canonical_original_code:
int waitingForServerMessage(int sock, char *commande)
{
 char commandReader[10];
 read(sock, commandReader, sizeof(commandReader)); if(1) printf("READ : %s\n",commandReader);
 if(strcmp(commandReader,commande)!=0)
 {
  printf("Erreur protocolaire\n");
  return 1;
 }
 return 0;
}


getLock

decompiled_code:
_BOOL8 __fastcall getLock(char *file)
{
int v1; // eax
int v2; // ebp
_BOOL8 result; // rax
int v4; // [rsp+0h] [rbp-28h] BYREF
__int64 v5; // [rsp+8h] [rbp-20h]
__int64 v6; // [rsp+10h] [rbp-18h]
__pid_t v7; // [rsp+18h] [rbp-10h]

v4 = 1;
v5 = 0LL;
v6 = 0LL;
v7 = getpid();
v1 = open(file, 2);
if ( v1 == -1 )
{
perror("open");
result = 1LL;
}
else
{
v2 = v1;
if ( fcntl(v1, 5, &v4) == -1 )
{
perror("fcntl");
result = 1LL;
}
else
{
close(v2);
result = (_WORD)v4 != 2;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(char *file)
{
int v1; // eax
int v2; // ebp
_BOOL8 result; // rax
int v4; // [rsp+0h] [rbp-28h] BYREF
__int64 v5; // [rsp+8h] [rbp-20h]
__int64 v6; // [rsp+10h] [rbp-18h]
__pid_t v7; // [rsp+18h] [rbp-10h]

v4 = 1;
v5 = 0LL;
v6 = 0LL;
v7 = getpid();
v1 = open(file, 2);
if ( v1 == -1 )
{
perror("open");
result = 1LL;
}
else
{
v2 = v1;
if ( fcntl(v1, 5, &v4) == -1 )
{
perror("fcntl");
result = 1LL;
}
else
{
close(v2);
result = (_WORD)v4 != 2;
}
}
return result;
}

original_code:
int getLock(char * filePath)
{
 struct flock fl = {1, 0, 0, 0, 0 };
 int fd;
 fl.l_pid =getpid();
 if ((fd = open(filePath, 02)) == -1) {
  perror("open");
  return 1;
 }
 if (fcntl(fd, 5, &fl) == -1) {
  perror("fcntl");
  return 1;
 }
 close(fd);
 if(fl.l_type==2)
 {
  return 0;
 }
 return 1;
}

canonical_original_code:
int getLock(char *filePath)
{
 struct flock fl = {1, 0, 0, 0, 0 };
 int fd;
 fl.l_pid =getpid();
 if ((fd = open(filePath, 02)) == -1) {
  perror("open");
  return 1;
 }
 if (fcntl(fd, 5, &fl) == -1) {
  perror("fcntl");
  return 1;
 }
 close(fd);
 if(fl.l_type==2)
 {
  return 0;
 }
 return 1;
}

variable_types:
filePath
char *

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }

variable_types: fd int


setLock

decompiled_code:
__int64 __fastcall setLock(char *file)
{
unsigned int v1; // er12
int v3; // edi
int v4; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+8h] [rbp-30h]
__int64 v6; // [rsp+10h] [rbp-28h]
__pid_t v7; // [rsp+18h] [rbp-20h]

v1 = 1;
if ( getLock(file) )
return v1;
v1 = 0;
v4 = 1;
v5 = 0LL;
v6 = 0LL;
v7 = getpid();
v3 = open(file, 2);
if ( v3 != -1 )
{
if ( fcntl(v3, 7, &v4) == -1 )
{
v1 = 1;
perror("fcntl");
}
return v1;
}
perror("open");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *file)
{
unsigned int v1; // er12
int v3; // edi
int v4; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+8h] [rbp-30h]
__int64 v6; // [rsp+10h] [rbp-28h]
__pid_t v7; // [rsp+18h] [rbp-20h]

v1 = 1;
if ( func7(file) )
return v1;
v1 = 0;
v4 = 1;
v5 = 0LL;
v6 = 0LL;
v7 = getpid();
v3 = open(file, 2);
if ( v3 != -1 )
{
if ( fcntl(v3, 7, &v4) == -1 )
{
v1 = 1;
perror("fcntl");
}
return v1;
}
perror("open");
return 1LL;
}

original_code:
int setLock(char * filePath)
{
 if(getLock(filePath)) return 1;
 struct flock fl = {1, 0, 0, 0, 0 };
 int fd;
 fl.l_pid = getpid();
 if ((fd = open(filePath, 02)) == -1) {
  perror("open");
  return 1;
 }
 if (fcntl(fd, 7, &fl) == -1) {
  perror("fcntl");
  return 1;
 }
 return 0;
}

canonical_original_code:
int setLock(char *filePath)
{
 if(getLock(filePath)) return 1;
 struct flock fl = {1, 0, 0, 0, 0 };
 int fd;
 fl.l_pid = getpid();
 if ((fd = open(filePath, 02)) == -1) {
  perror("open");
  return 1;
 }
 if (fcntl(fd, 7, &fl) == -1) {
  perror("fcntl");
  return 1;
 }
 return 0;
}

user_defined_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


unLock

decompiled_code:
__int64 __fastcall unLock(char *file)
{
unsigned int v1; // er12
int v3; // ebp
int v4; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+8h] [rbp-30h]
__int64 v6; // [rsp+10h] [rbp-28h]
__pid_t v7; // [rsp+18h] [rbp-20h]

v1 = 1;
if ( !getLock(file) )
return v1;
v4 = 2;
v5 = 0LL;
v6 = 0LL;
v7 = 0;
v3 = open(file, 2);
if ( v3 != -1 )
{
v1 = 0;
v7 = getpid();
if ( fcntl(v3, 6, &v4) == -1 )
{
v1 = 1;
perror("fcntl");
}
return v1;
}
perror("open");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *file)
{
unsigned int v1; // er12
int v3; // ebp
int v4; // [rsp+0h] [rbp-38h] BYREF
__int64 v5; // [rsp+8h] [rbp-30h]
__int64 v6; // [rsp+10h] [rbp-28h]
__pid_t v7; // [rsp+18h] [rbp-20h]

v1 = 1;
if ( !func7(file) )
return v1;
v4 = 2;
v5 = 0LL;
v6 = 0LL;
v7 = 0;
v3 = open(file, 2);
if ( v3 != -1 )
{
v1 = 0;
v7 = getpid();
if ( fcntl(v3, 6, &v4) == -1 )
{
v1 = 1;
perror("fcntl");
}
return v1;
}
perror("open");
return 1LL;
}

original_code:
int unLock(char * filePath)
{
 if(!getLock(filePath)) return 1;
 struct flock fl = {2, 0, 0, 0, 0 };
 int fd;
 if ((fd = open(filePath, 02)) == -1) {
  perror("open");
  return 1;
 }
 fl.l_pid =getpid();
 if (fcntl(fd, 6, &fl) == -1) {
  perror("fcntl");
  return 1;
 }
     return 0;
}

canonical_original_code:
int unLock(char *filePath)
{
 if(!getLock(filePath)) return 1;
 struct flock fl = {2, 0, 0, 0, 0 };
 int fd;
 if ((fd = open(filePath, 02)) == -1) {
  perror("open");
  return 1;
 }
 fl.l_pid =getpid();
 if (fcntl(fd, 6, &fl) == -1) {
  perror("fcntl");
  return 1;
 }
     return 0;
}


decompiled_code:
__int64 __fastcall init_pipe(__int16 a1, int *a2, _QWORD *a3, _QWORD *a4, unsigned __int8 a5)
{
int v8; // eax
int v9; // ebp
int v10; // eax
__int64 v11; // rdi
ssize_t v12; // rax
ssize_t v13; // rax
unsigned int v14; // er15
ssize_t v15; // rax
struct sockaddr v17; // [rsp+0h] [rbp-78h] BYREF
__int128 buf; // [rsp+10h] [rbp-68h] BYREF
__int128 v19; // [rsp+20h] [rbp-58h]
__int64 v20; // [rsp+30h] [rbp-48h]

v8 = socket(2, 2, 0);
if ( v8 < 0 )
{
v14 = -1;
printf("Unable to open an udp socket");
perror("socket");
return v14;
}
v9 = v8;
v17 = 0LL;
buf = 0LL;
*(_WORD *)v17.sa_data = _bswap_16(a1);
v10 = *a2;
v20 = 0LL;
*(_DWORD *)&v17.sa_data[2] = v10;
v19 = 0LL;
if ( (unsigned int)xen_shm_pipe_init(a3, 1LL) )
{
puts("Unable to init xen_shm_pipe");
perror("xen_shm_pipe_init");
goto LABEL_23;
}
if ( (unsigned int)xen_shm_pipe_init(a4, 0LL) )
{
puts("Unable to init xen_shm_pipe");
perror("xen_shm_pipe_init");
goto LABEL_22;
}
v11 = *a4;
LOWORD(buf) = 257;
if ( (unsigned int)xen_shm_pipe_getdomid(v11, (char *)&buf + 8) )
{
puts("Unable to get local domid");
perror("xen_shm_pipe_getdomid");
goto LABEL_21;
}
v12 = sendto(v9, &buf, 0xCuLL, 0, &v17, 0x10u);
if ( v12 < 0 )
{
LABEL_32:
puts("Unable to send message");
perror("sendto");
goto LABEL_20;
}
if ( v12 != 12 )
{
LABEL_25:
puts("Unable to send message (bad size=");
perror("sendto");
goto LABEL_20;
}
v13 = recv(v9, &buf, 0x28uLL, 0);
if ( v13 <= 0 )
{
puts("Unable to receive message");
perror("recv");
goto LABEL_20;
}
if ( BYTE1(buf) == 2 )
{
puts("Server error");
goto LABEL_20;
}
if ( BYTE1(buf) != 3 )
{
puts("Protocol error");
goto LABEL_20;
}
if ( v13 <= 17 )
{
printf("Bad packet: too short");
goto LABEL_20;
}
if ( (_BYTE)v19 == 1 )
{
puts("xen_shm_pipe_conv_writer_offers not supporter");
goto LABEL_20;
}
if ( (_BYTE)v19 != 2 )
{
puts("Protocol error: bad mode");
LABEL_20:
BYTE1(buf) = 4;
sendto(v9, &buf, 8uLL, 0, &v17, 0x10u);
LABEL_21:
xen_shm_pipe_free(*a4);
LABEL_22:
xen_shm_pipe_free(*a3);
LABEL_23:
v14 = -1;
shutdown(v9, 2);
return v14;
}
if ( (unsigned int)xen_shm_pipe_connect(*a4, BYTE1(v19), HIDWORD(buf), DWORD2(buf)) )
{
puts("Unable to connect xen_shm_pipe");
perror("xen_shm_pipe_connect");
goto LABEL_20;
}
v14 = xen_shm_pipe_offers(*a3, a5, HIDWORD(buf), (char *)&buf + 12, (char *)&buf + 8);
if ( v14 )
{
puts("Unable to offer xen_shm_pipe");
perror("xen_shm_pipe_offers");
goto LABEL_20;
}
BYTE1(buf) = 5;
BYTE1(v19) = a5;
v15 = sendto(v9, &buf, 0x12uLL, 0, &v17, 0x10u);
if ( v15 < 0 )
goto LABEL_32;
if ( v15 != 18 )
goto LABEL_25;
shutdown(v9, 2);
return v14;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, int *a2, _QWORD *a3, _QWORD *a4, unsigned __int8 a5)
{
int v8; // eax
int v9; // ebp
int v10; // eax
__int64 v11; // rdi
ssize_t v12; // rax
ssize_t v13; // rax
unsigned int v14; // er15
ssize_t v15; // rax
struct sockaddr v17; // [rsp+0h] [rbp-78h] BYREF
__int128 buf; // [rsp+10h] [rbp-68h] BYREF
__int128 v19; // [rsp+20h] [rbp-58h]
__int64 v20; // [rsp+30h] [rbp-48h]

v8 = socket(2, 2, 0);
if ( v8 < 0 )
{
v14 = -1;
printf("Unable to open an udp socket");
perror("socket");
return v14;
}
v9 = v8;
v17 = 0LL;
buf = 0LL;
*(_WORD *)v17.sa_data = func0(a1);
v10 = *a2;
v20 = 0LL;
*(_DWORD *)&v17.sa_data[2] = v10;
v19 = 0LL;
if ( (unsigned int)xen_shm_pipe_init(a3, 1LL) )
{
puts("Unable to init xen_shm_pipe");
perror("xen_shm_pipe_init");
goto LABEL_23;
}
if ( (unsigned int)xen_shm_pipe_init(a4, 0LL) )
{
puts("Unable to init xen_shm_pipe");
perror("xen_shm_pipe_init");
goto LABEL_22;
}
v11 = *a4;
LOWORD(buf) = 257;
if ( (unsigned int)xen_shm_pipe_getdomid(v11, (char *)&buf + 8) )
{
puts("Unable to get local domid");
perror("xen_shm_pipe_getdomid");
goto LABEL_21;
}
v12 = sendto(v9, &buf, 0xCuLL, 0, &v17, 0x10u);
if ( v12 < 0 )
{
LABEL_32:
puts("Unable to send message");
perror("sendto");
goto LABEL_20;
}
if ( v12 != 12 )
{
LABEL_25:
puts("Unable to send message (bad size=");
perror("sendto");
goto LABEL_20;
}
v13 = recv(v9, &buf, 0x28uLL, 0);
if ( v13 <= 0 )
{
puts("Unable to receive message");
perror("recv");
goto LABEL_20;
}
if ( BYTE1(buf) == 2 )
{
puts("Server error");
goto LABEL_20;
}
if ( BYTE1(buf) != 3 )
{
puts("Protocol error");
goto LABEL_20;
}
if ( v13 <= 17 )
{
printf("Bad packet: too short");
goto LABEL_20;
}
if ( (_BYTE)v19 == 1 )
{
puts("xen_shm_pipe_conv_writer_offers not supporter");
goto LABEL_20;
}
if ( (_BYTE)v19 != 2 )
{
puts("Protocol error: bad mode");
LABEL_20:
BYTE1(buf) = 4;
sendto(v9, &buf, 8uLL, 0, &v17, 0x10u);
LABEL_21:
xen_shm_pipe_free(*a4);
LABEL_22:
xen_shm_pipe_free(*a3);
LABEL_23:
v14 = -1;
shutdown(v9, 2);
return v14;
}
if ( (unsigned int)xen_shm_pipe_connect(*a4, BYTE1(v19), HIDWORD(buf), DWORD2(buf)) )
{
puts("Unable to connect xen_shm_pipe");
perror("xen_shm_pipe_connect");
goto LABEL_20;
}
v14 = xen_shm_pipe_offers(*a3, a5, HIDWORD(buf), (char *)&buf + 12, (char *)&buf + 8);
if ( v14 )
{
puts("Unable to offer xen_shm_pipe");
perror("xen_shm_pipe_offers");
goto LABEL_20;
}
BYTE1(buf) = 5;
BYTE1(v19) = a5;
v15 = sendto(v9, &buf, 0x12uLL, 0, &v17, 0x10u);
if ( v15 < 0 )
goto LABEL_32;
if ( v15 != 18 )
goto LABEL_25;
shutdown(v9, 2);
return v14;
}

original_code:
int
init_pipe(in_port_t distant_port, struct in_addr *distant_addr, xen_shm_pipe_p *receive_fd, xen_shm_pipe_p *send_fd, uint8_t proposed_page_page_count)
{
    struct sockaddr_in addr;
    int ret;
    int client_fd;
    int return_value;
    ssize_t len;
    uint8_t buffer[40];
    struct xen_shm_udp_proto_header *header;
    struct xen_shm_udp_proto_client_hello *client_hello;
    struct xen_shm_udp_proto_grant *grant;
    return_value = -1;
    client_fd = socket(2, SOCK_DGRAM, 0);
    if (client_fd < 0) {
        printf("Unable to open an udp socket");
        perror("socket");
        return -1;
    }
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_port = htons(distant_port);
    memcpy(&addr.sin_addr, distant_addr,sizeof(struct in_addr));
    memset(buffer, 0, 40);
    header = (struct xen_shm_udp_proto_header*) buffer;
    client_hello = (struct xen_shm_udp_proto_client_hello*) buffer;
    grant = (struct xen_shm_udp_proto_grant*) buffer;
    ret = xen_shm_pipe_init(receive_fd, xen_shm_pipe_mod_read, xen_shm_pipe_conv_reader_offers);
    if (ret != 0) {
        printf("Unable to init xen_shm_pipe\n");
        perror("xen_shm_pipe_init");
        goto shutdown_socket;
    }
    ret = xen_shm_pipe_init(send_fd, xen_shm_pipe_mod_write, xen_shm_pipe_conv_reader_offers);
    if (ret != 0) {
        printf("Unable to init xen_shm_pipe\n");
        perror("xen_shm_pipe_init");
        goto clean_receive_fd;
    }
    header->version = 0x01;
    header->message = 0x01;
    ret = xen_shm_pipe_getdomid(*send_fd, &client_hello->domid);
    if (ret != 0) {
        printf("Unable to get local domid\n");
        perror("xen_shm_pipe_getdomid");
        goto clean_send_fd;
    }
    len = sendto(client_fd, buffer, sizeof(struct xen_shm_udp_proto_client_hello), 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
    if (len < 0) {
        printf("Unable to send message\n");
        perror("sendto");
        goto cancel_server;
    }
    if ((size_t)len != sizeof(struct xen_shm_udp_proto_client_hello)) {
        printf("Unable to send message (bad size=\n");
        perror("sendto");
        goto cancel_server;
    }
    len = recv(client_fd, buffer, 40, 0 );
    if (len <= 0) {
        printf("Unable to receive message\n");
        perror("recv");
        goto cancel_server;
    }
    if (header->message == 0x02) {
        printf("Server error\n");
        goto cancel_server;
    }
    if (header->message != 0x03) {
        printf("Protocol error\n");
        goto cancel_server;
    }
    if ((size_t)len < sizeof(struct xen_shm_udp_proto_grant)) {
        printf("Bad packet: too short");
        goto cancel_server;
    }
    switch (grant->mode) {
        case 0x01:
            printf("xen_shm_pipe_conv_writer_offers not supporter\n");
            goto cancel_server;
        case 0x02:
            break;
        default:
            printf("Protocol error: bad mode\n");
            goto cancel_server;
    }
    ;
    ret = xen_shm_pipe_connect(*send_fd, grant->page_count, grant->domid, grant->grant_ref);
    if (ret != 0) {
        printf("Unable to connect xen_shm_pipe\n");
        perror("xen_shm_pipe_connect");
        goto cancel_server;
    }
    ;
    ret = xen_shm_pipe_offers(*receive_fd, proposed_page_page_count, grant->domid, &grant->domid, &grant->grant_ref);
    ;
    if (ret != 0) {
        printf("Unable to offer xen_shm_pipe\n");
        perror("xen_shm_pipe_offers");
        goto cancel_server;
    }
    header->message = 0x05;
    grant->page_count = proposed_page_page_count;
    len = sendto(client_fd, buffer, sizeof(struct xen_shm_udp_proto_grant), 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
    if (len < 0) {
        printf("Unable to send message\n");
        perror("sendto");
        goto cancel_server;
    }
    if ((size_t)len != sizeof(struct xen_shm_udp_proto_grant)) {
        printf("Unable to send message (bad size=\n");
        perror("sendto");
        goto cancel_server;
    }
    shutdown(client_fd, SHUT_RDWR);
    return 0;
cancel_server:
    header->message = 0x04;
    sendto(client_fd, buffer, sizeof(struct xen_shm_udp_proto_header), 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
clean_send_fd:
    xen_shm_pipe_free(*send_fd);
clean_receive_fd:
    xen_shm_pipe_free(*receive_fd);
shutdown_socket:
    shutdown(client_fd, SHUT_RDWR);
    return return_value;
}

canonical_original_code:
int init_pipe(unsigned short distant_port, struct in_addr *distant_addr, void **receive_fd, void **send_fd, unsigned char proposed_page_page_count)
{
    struct sockaddr_in addr;
    int ret;
    int client_fd;
    int return_value;
    long len;
    unsigned char buffer[40];
    struct xen_shm_udp_proto_header *header;
    struct xen_shm_udp_proto_client_hello *client_hello;
    struct xen_shm_udp_proto_grant *grant;
    return_value = -1;
    client_fd = socket(2, SOCK_DGRAM, 0);
    if (client_fd < 0) {
        printf("Unable to open an udp socket");
        perror("socket");
        return -1;
    }
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_port = htons(distant_port);
    memcpy(&addr.sin_addr, distant_addr,sizeof(struct in_addr));
    memset(buffer, 0, 40);
    header = (struct xen_shm_udp_proto_header *) buffer;
    client_hello = (struct xen_shm_udp_proto_client_hello *) buffer;
    grant = (struct xen_shm_udp_proto_grant *) buffer;
    ret = xen_shm_pipe_init(receive_fd, xen_shm_pipe_mod_read, xen_shm_pipe_conv_reader_offers);
    if (ret != 0) {
        printf("Unable to init xen_shm_pipe\n");
        perror("xen_shm_pipe_init");
        goto shutdown_socket;
    }
    ret = xen_shm_pipe_init(send_fd, xen_shm_pipe_mod_write, xen_shm_pipe_conv_reader_offers);
    if (ret != 0) {
        printf("Unable to init xen_shm_pipe\n");
        perror("xen_shm_pipe_init");
        goto clean_receive_fd;
    }
    header->version = 0x01;
    header->message = 0x01;
    ret = xen_shm_pipe_getdomid(*send_fd, &client_hello->domid);
    if (ret != 0) {
        printf("Unable to get local domid\n");
        perror("xen_shm_pipe_getdomid");
        goto clean_send_fd;
    }
    len = sendto(client_fd, buffer, sizeof(struct xen_shm_udp_proto_client_hello), 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
    if (len < 0) {
        printf("Unable to send message\n");
        perror("sendto");
        goto cancel_server;
    }
    if ((unsigned long)len != sizeof(struct xen_shm_udp_proto_client_hello)) {
        printf("Unable to send message (bad size=\n");
        perror("sendto");
        goto cancel_server;
    }
    len = recv(client_fd, buffer, 40, 0 );
    if (len <= 0) {
        printf("Unable to receive message\n");
        perror("recv");
        goto cancel_server;
    }
    if (header->message == 0x02) {
        printf("Server error\n");
        goto cancel_server;
    }
    if (header->message != 0x03) {
        printf("Protocol error\n");
        goto cancel_server;
    }
    if ((unsigned long)len < sizeof(struct xen_shm_udp_proto_grant)) {
        printf("Bad packet: too short");
        goto cancel_server;
    }
    switch (grant->mode) {
        case 0x01:
            printf("xen_shm_pipe_conv_writer_offers not supporter\n");
            goto cancel_server;
        case 0x02:
            break;
        default:
            printf("Protocol error: bad mode\n");
            goto cancel_server;
    }
    ;
    ret = xen_shm_pipe_connect(*send_fd, grant->page_count, grant->domid, grant->grant_ref);
    if (ret != 0) {
        printf("Unable to connect xen_shm_pipe\n");
        perror("xen_shm_pipe_connect");
        goto cancel_server;
    }
    ;
    ret = xen_shm_pipe_offers(*receive_fd, proposed_page_page_count, grant->domid, &grant->domid, &grant->grant_ref);
    ;
    if (ret != 0) {
        printf("Unable to offer xen_shm_pipe\n");
        perror("xen_shm_pipe_offers");
        goto cancel_server;
    }
    header->message = 0x05;
    grant->page_count = proposed_page_page_count;
    len = sendto(client_fd, buffer, sizeof(struct xen_shm_udp_proto_grant), 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
    if (len < 0) {
        printf("Unable to send message\n");
        perror("sendto");
        goto cancel_server;
    }
    if ((unsigned long)len != sizeof(struct xen_shm_udp_proto_grant)) {
        printf("Unable to send message (bad size=\n");
        perror("sendto");
        goto cancel_server;
    }
    shutdown(client_fd, SHUT_RDWR);
    return 0;
cancel_server:
    header->message = 0x04;
    sendto(client_fd, buffer, sizeof(struct xen_shm_udp_proto_header), 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
clean_send_fd:
    xen_shm_pipe_free(*send_fd);
clean_receive_fd:
    xen_shm_pipe_free(*receive_fd);
shutdown_socket:
    shutdown(client_fd, SHUT_RDWR);
    return return_value;
}

variable_types: struct in_addr { unsigned int s_addr; } *

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: ret int

variable_types: len long

variable_types:
buffer
unsigned char [40]

variable_types: struct xen_shm_udp_proto_header { unsigned char version; unsigned char message; unsigned short reserved; unsigned int big_reserved; } *

variable_types: struct xen_shm_udp_proto_client_hello { struct xen_shm_udp_proto_header { unsigned char version; unsigned char message; unsigned short reserved; unsigned int big_reserved; } header; unsigned int domid; } *

variable_types: struct xen_shm_udp_proto_grant { struct xen_shm_udp_proto_header { unsigned char version; unsigned char message; unsigned short reserved; unsigned int big_reserved; } header; unsigned int grant_ref; unsigned int domid; unsigned char mode; unsigned char page_count; } *

user_defined_types: struct xen_shm_udp_proto_client_hello { struct xen_shm_udp_proto_header header; unsigned int domid; }

user_defined_types: struct xen_shm_udp_proto_grant { struct xen_shm_udp_proto_header header; unsigned int grant_ref; unsigned int domid; unsigned char mode; unsigned char page_count; }


decompiled_code:
int __fastcall run_client_thread(__int16 a1, int *a2, unsigned __int8 a3, void *(*a4)(void *), _QWORD *a5, pthread_t *a6)
{
int v9; // eax
int result; // eax
int v11; // [rsp+Ch] [rbp-1Ch]
int v12; // [rsp+Ch] [rbp-1Ch]

*((_DWORD *)a5 + 4) = 0;
v9 = init_pipe(a1, a2, a5, a5 + 1, a3);
if ( v9 )
{
v11 = v9;
perror("init pipe");
result = v11;
}
else
{
result = pthread_create(a6, 0LL, a4, a5);
if ( result )
{
v12 = result;
perror("pthread create");
xen_shm_pipe_free(*a5);
xen_shm_pipe_free(a5[1]);
result = v12;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int16 a1, int *a2, unsigned __int8 a3, void *(*a4)(void *), _QWORD *a5, pthread_t *a6)
{
int v9; // eax
int result; // eax
int v11; // [rsp+Ch] [rbp-1Ch]
int v12; // [rsp+Ch] [rbp-1Ch]

*((_DWORD *)a5 + 4) = 0;
v9 = func1(a1, a2, a5, a5 + 1, a3);
if ( v9 )
{
v11 = v9;
perror("init pipe");
result = v11;
}
else
{
result = pthread_create(a6, 0LL, a4, a5);
if ( result )
{
v12 = result;
perror("pthread create");
xen_shm_pipe_free(*a5);
xen_shm_pipe_free(a5[1]);
result = v12;
}
}
return result;
}

original_code:
int
run_client_thread(in_port_t distant_por, struct in_addr *distant_addr, uint8_t proposed_page_page_count,
        handler_run handler_fct, struct xen_shm_handler_data* hdlr_data, pthread_t* thread_info)
{
    int ret;
    hdlr_data->stop = 0;
    ret = init_pipe(distant_por, distant_addr, &hdlr_data->receive_fd, &hdlr_data->send_fd, proposed_page_page_count);
    if(ret!=0) {
        perror("init pipe");
        return ret;
    }
    ret = pthread_create(thread_info, ((void *)0), (void * (*)(void *))handler_fct, hdlr_data);
    if (ret != 0) {
        perror("pthread create");
        xen_shm_pipe_free(hdlr_data->receive_fd);
        xen_shm_pipe_free(hdlr_data->send_fd);
        return ret;
    }
    return 0;
}

canonical_original_code:
int run_client_thread(unsigned short distant_por, struct in_addr *distant_addr, unsigned char proposed_page_page_count, void *(*handler_fct)(struct xen_shm_handler_data *data), struct xen_shm_handler_data *hdlr_data, unsigned long *thread_info)
{
    int ret;
    hdlr_data->stop = 0;
    ret = init_pipe(distant_por, distant_addr, &hdlr_data->receive_fd, &hdlr_data->send_fd, proposed_page_page_count);
    if(ret!=0) {
        perror("init pipe");
        return ret;
    }
    ret = pthread_create(thread_info, ((void *)0), (void *(*)(void *))handler_fct, hdlr_data);
    if (ret != 0) {
        perror("pthread create");
        xen_shm_pipe_free(hdlr_data->receive_fd);
        xen_shm_pipe_free(hdlr_data->send_fd);
        return ret;
    }
    return 0;
}

variable_types: void *(*)(struct xen_shm_handler_data { void *receive_fd; void *send_fd; int stop; void *private_data; } *data)


decompiled_code:
__int64 __fastcall run_client(__int16 a1, int *a2, unsigned __int8 a3, __int64 (__fastcall *a4)(__int64), __int64 a5, _QWORD *a6)
{
unsigned int v9; // er12

*(_DWORD *)(a5 + 16) = 0;
v9 = init_pipe(a1, a2, (_QWORD *)a5, (_QWORD *)(a5 + 8), a3);
if ( v9 )
perror("init pipe");
else
*a6 = a4(a5);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1, int *a2, unsigned __int8 a3, __int64 (__fastcall *a4)(__int64), __int64 a5, _QWORD *a6)
{
unsigned int v9; // er12

*(_DWORD *)(a5 + 16) = 0;
v9 = func1(a1, a2, (_QWORD *)a5, (_QWORD *)(a5 + 8), a3);
if ( v9 )
perror("init pipe");
else
*a6 = a4(a5);
return v9;
}

original_code:
int
run_client(in_port_t distant_por, struct in_addr *distant_addr, uint8_t proposed_page_page_count,
        handler_run handler_fct, struct xen_shm_handler_data* hdlr_data, void** returned_value)
{
    int ret;
    hdlr_data->stop = 0;
    ret = init_pipe(distant_por, distant_addr, &hdlr_data->receive_fd, &hdlr_data->send_fd, proposed_page_page_count);
    if(ret!=0) {
        perror("init pipe");
        return ret;
    }
    *returned_value = handler_fct(hdlr_data);
    return 0;
}

canonical_original_code:
int run_client(unsigned short distant_por, struct in_addr *distant_addr, unsigned char proposed_page_page_count, void *(*handler_fct)(struct xen_shm_handler_data *data), struct xen_shm_handler_data *hdlr_data, void **returned_value)
{
    int ret;
    hdlr_data->stop = 0;
    ret = init_pipe(distant_por, distant_addr, &hdlr_data->receive_fd, &hdlr_data->send_fd, proposed_page_page_count);
    if(ret!=0) {
        perror("init pipe");
        return ret;
    }
    *returned_value = handler_fct(hdlr_data);
    return 0;
}

user_defined_types: struct xen_shm_handler_data { void *receive_fd; void *send_fd; int stop; void *private_data; }


decompiled_code:
void __noreturn pipe_read()
{
void *v0; // rax
void *v1; // rbx
__int64 v2; // rax

v0 = malloc((unsigned int)buffer_size);
if ( v0 )
{
v1 = v0;
gettimeofday(&start, 0LL);
while ( 1 )
{
v2 = xen_shm_pipe_read_all(xpipe, v1, (unsigned int)buffer_size);
if ( v2 <= 0 )
break;
byte_count += v2;
}
if ( v2 )
perror("Xen pipe read");
else
puts("End of file ");
clean(0);
}
puts("Memory error");
clean(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
void *v0; // rax
void *v1; // rbx
__int64 v2; // rax

v0 = malloc((unsigned int)buffer_size);
if ( v0 )
{
v1 = v0;
gettimeofday(&start, 0LL);
while ( 1 )
{
v2 = xen_shm_pipe_read_all(xpipe, v1, (unsigned int)buffer_size);
if ( v2 <= 0 )
break;
byte_count += v2;
}
if ( v2 )
perror("Xen pipe read");
else
puts("End of file ");
func0(0);
}
puts("Memory error");
func0(0);
}

original_code:
void pipe_read(void) {
    uint8_t* buffer;
    ssize_t retval;
    if((buffer = malloc(sizeof(uint8_t)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    gettimeofday(&start , ((void *)0));
    while((retval = xen_shm_pipe_read_all(xpipe, buffer, buffer_size)) > 0) {
        byte_count += (uint64_t) retval;
    }
    if(retval == 0) {
        printf("End of file \n");
    } else {
        perror("Xen pipe read");
    }
    clean(0);
}

canonical_original_code:
void pipe_read() {
    unsigned char *buffer;
    long retval;
    if((buffer = malloc(sizeof(unsigned char)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    gettimeofday(&start , ((void *)0));
    while((retval = xen_shm_pipe_read_all(xpipe, buffer, buffer_size)) > 0) {
        byte_count += (unsigned long) retval;
    }
    if(retval == 0) {
        printf("End of file \n");
    } else {
        perror("Xen pipe read");
    }
    clean(0);
}

variable_types:
buffer
unsigned char *

variable_types: retval long


decompiled_code:
void __noreturn pipe_write()
{
size_t v0; // r12
void *v1; // rax
void *v2; // rbp
unsigned int v3; // ebx
__int64 v4; // rax

v0 = (unsigned int)buffer_size;
v1 = malloc((unsigned int)buffer_size);
if ( v1 )
{
v2 = v1;
if ( (_DWORD)v0 )
memset(v1, 117, v0);
gettimeofday(&start, 0LL);
if ( iterations )
{
v3 = 0;
do
{
v4 = xen_shm_pipe_write_all(xpipe, v2, (unsigned int)buffer_size);
if ( v4 < 0 )
{
perror("Xen pipe write");
clean(0);
}
if ( !v4 )
{
puts("Write retval = 0 !");
clean(0);
}
byte_count += v4;
++v3;
}
while ( iterations > v3 );
}
clean(0);
}
puts("Memory error");
clean(0);
}

canonical_decompiled_code:
void __noreturn func3()
{
size_t v0; // r12
void *v1; // rax
void *v2; // rbp
unsigned int v3; // ebx
__int64 v4; // rax

v0 = (unsigned int)buffer_size;
v1 = malloc((unsigned int)buffer_size);
if ( v1 )
{
v2 = v1;
if ( (_DWORD)v0 )
memset(v1, 117, v0);
gettimeofday(&start, 0LL);
if ( iterations )
{
v3 = 0;
do
{
v4 = xen_shm_pipe_write_all(xpipe, v2, (unsigned int)buffer_size);
if ( v4 < 0 )
{
perror("Xen pipe write");
func0(0);
}
if ( !v4 )
{
puts("Write retval = 0 !");
func0(0);
}
byte_count += v4;
++v3;
}
while ( iterations > v3 );
}
func0(0);
}
puts("Memory error");
func0(0);
}

original_code:
void pipe_write(void) {
    uint8_t* buffer;
    ssize_t retval;
    uint32_t i;
    if((buffer = malloc(sizeof(uint8_t)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    for(i = 0; i<buffer_size; i++) {
        buffer[i] = 'u';
    }
    gettimeofday(&start , ((void *)0));
    for(i=0; i<iterations; i++) {
        retval = xen_shm_pipe_write_all(xpipe, buffer, buffer_size);
        if(retval < 0) {
            perror("Xen pipe write");
            clean(0);
        } else if(retval == 0) {
            printf("Write retval = 0 !\n");
            clean(0);
        }
        byte_count+=(uint64_t) retval;
    }
    clean(0);
}

canonical_original_code:
void pipe_write() {
    unsigned char *buffer;
    long retval;
    unsigned int i;
    if((buffer = malloc(sizeof(unsigned char)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    for(i = 0; i<buffer_size; i++) {
        buffer[i] = 'u';
    }
    gettimeofday(&start , ((void *)0));
    for(i=0; i<iterations; i++) {
        retval = xen_shm_pipe_write_all(xpipe, buffer, buffer_size);
        if(retval < 0) {
            perror("Xen pipe write");
            clean(0);
        } else if(retval == 0) {
            printf("Write retval = 0 !\n");
            clean(0);
        }
        byte_count+=(unsigned long) retval;
    }
    clean(0);
}

variable_types:
i
unsigned int


decompiled_code:
__sighandler_t init_pipe_reader()
{
unsigned int v1; // [rsp+4h] [rbp-14h] BYREF
unsigned int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

puts("\nInit: Reader - Receiver");
if ( (unsigned int)xen_shm_pipe_init(&xpipe, 1LL, 0LL) )
{
perror("Pipe init");
exit(-1);
}
if ( (unsigned int)xen_shm_pipe_getdomid(xpipe, &v1) )
{
perror("Pipe get domid");
clean(0);
}
printf("RECEIVER domain id: %u\n", v1);
printf("Distant domain id: ");
if ( (unsigned int)_isoc99_scanf("%u", &v2) != 1
|| (printf("Offerer's grant ref: "), (unsigned int)_isoc99_scanf("%u", v3) != 1) )
{
printf("Scanf error");
clean(0);
}
if ( (unsigned int)xen_shm_pipe_connect(xpipe, (unsigned __int8)page_count, v2, v3[0]) )
{
perror("Pipe connect");
clean(0);
}
puts("Connected successfully !");
return signal(2, (__sighandler_t)clean);
}

canonical_decompiled_code:
__sighandler_t func4()
{
unsigned int v1; // [rsp+4h] [rbp-14h] BYREF
unsigned int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

puts("\nInit: Reader - Receiver");
if ( (unsigned int)xen_shm_pipe_init(&xpipe, 1LL, 0LL) )
{
perror("Pipe init");
exit(-1);
}
if ( (unsigned int)xen_shm_pipe_getdomid(xpipe, &v1) )
{
perror("Pipe get domid");
func0(0);
}
printf("RECEIVER domain id: %u\n", v1);
printf("Distant domain id: ");
if ( (unsigned int)_isoc99_scanf("%u", &v2) != 1
|| (printf("Offerer's grant ref: "), (unsigned int)_isoc99_scanf("%u", v3) != 1) )
{
printf("Scanf error");
func0(0);
}
if ( (unsigned int)xen_shm_pipe_connect(xpipe, (unsigned __int8)page_count, v2, v3[0]) )
{
perror("Pipe connect");
func0(0);
}
puts("Connected successfully !");
return signal(2, (__sighandler_t)clean);
}

original_code:
void init_pipe_reader(void) {
    uint32_t local_domid;
    uint32_t dist_domid;
    uint32_t grant_ref;
    printf("\nInit: Reader - Receiver\n");
    if(xen_shm_pipe_init(&xpipe, xen_shm_pipe_mod_read, xen_shm_pipe_conv_writer_offers)) {
        perror("Pipe init");
        exit(-1);
    }
    pipe_used = 1;
    if(xen_shm_pipe_getdomid(xpipe, &local_domid)) {
        perror("Pipe get domid");
        clean(0);
    }
    printf("RECEIVER domain id: %""u""\n", local_domid);
    printf("Distant domain id: ");
    if((scanf("%""u", &dist_domid)!=1)) {
        printf("Scanf error");
        clean(0);
    }
    printf("Offerer's grant ref: ");
    if((scanf("%""u", &grant_ref)!=1)) {
        printf("Scanf error");
        clean(0);
    }
    if(xen_shm_pipe_connect(xpipe,page_count,dist_domid, grant_ref)) {
        perror("Pipe connect");
        clean(0);
    }
    printf("Connected successfully !\n");
    signal(2, clean);
}

canonical_original_code:
void init_pipe_reader() {
    unsigned int local_domid;
    unsigned int dist_domid;
    unsigned int grant_ref;
    printf("\nInit: Reader - Receiver\n");
    if(xen_shm_pipe_init(&xpipe, xen_shm_pipe_mod_read, xen_shm_pipe_conv_writer_offers)) {
        perror("Pipe init");
        exit(-1);
    }
    pipe_used = 1;
    if(xen_shm_pipe_getdomid(xpipe, &local_domid)) {
        perror("Pipe get domid");
        clean(0);
    }
    printf("RECEIVER domain id: %""u""\n", local_domid);
    printf("Distant domain id: ");
    if((scanf("%""u", &dist_domid)!=1)) {
        printf("Scanf error");
        clean(0);
    }
    printf("Offerer's grant ref: ");
    if((scanf("%""u", &grant_ref)!=1)) {
        printf("Scanf error");
        clean(0);
    }
    if(xen_shm_pipe_connect(xpipe,page_count,dist_domid, grant_ref)) {
        perror("Pipe connect");
        clean(0);
    }
    printf("Connected successfully !\n");
    signal(2, clean);
}


decompiled_code:
__sighandler_t init_pipe_writer()
{
unsigned int v1; // [rsp+4h] [rbp-14h] BYREF
unsigned int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

puts("Init: Writer - Offerer");
if ( (unsigned int)xen_shm_pipe_init(&xpipe, 0LL, 0LL) )
{
perror("Pipe init");
exit(-1);
}
printf("Distant domain id: ");
if ( (unsigned int)_isoc99_scanf("%u", &v2) != 1 )
{
printf("Scanf error");
clean(0);
}
if ( (unsigned int)xen_shm_pipe_offers(xpipe, (unsigned __int8)page_count, v2, &v1, v3) )
{
perror("Pipe get domid");
clean(0);
}
printf("Local domain id: %u\n", v1);
printf("Grant reference id: %u\n", v3[0]);
puts("Will now wait for at most 30 seconds");
if ( (unsigned int)xen_shm_pipe_wait(xpipe, 30000LL) )
{
perror("Pipe wait");
clean(0);
}
puts("Connected successfully !");
return signal(2, (__sighandler_t)clean);
}

canonical_decompiled_code:
__sighandler_t func5()
{
unsigned int v1; // [rsp+4h] [rbp-14h] BYREF
unsigned int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

puts("Init: Writer - Offerer");
if ( (unsigned int)xen_shm_pipe_init(&xpipe, 0LL, 0LL) )
{
perror("Pipe init");
exit(-1);
}
printf("Distant domain id: ");
if ( (unsigned int)_isoc99_scanf("%u", &v2) != 1 )
{
printf("Scanf error");
func0(0);
}
if ( (unsigned int)xen_shm_pipe_offers(xpipe, (unsigned __int8)page_count, v2, &v1, v3) )
{
perror("Pipe get domid");
func0(0);
}
printf("Local domain id: %u\n", v1);
printf("Grant reference id: %u\n", v3[0]);
puts("Will now wait for at most 30 seconds");
if ( (unsigned int)xen_shm_pipe_wait(xpipe, 30000LL) )
{
perror("Pipe wait");
func0(0);
}
puts("Connected successfully !");
return signal(2, (__sighandler_t)clean);
}

original_code:
void init_pipe_writer(void) {
    uint32_t local_domid;
    uint32_t dist_domid;
    uint32_t grant_ref;
    printf("Init: Writer - Offerer\n");
    if(xen_shm_pipe_init(&xpipe, xen_shm_pipe_mod_write, xen_shm_pipe_conv_writer_offers)) {
        perror("Pipe init");
        exit(-1);
    }
    pipe_used = 1;
    printf("Distant domain id: ");
    if((scanf("%""u", &dist_domid)!=1)) {
        printf("Scanf error");
        clean(0);
    }
    if(xen_shm_pipe_offers(xpipe, page_count, dist_domid, &local_domid, &grant_ref)) {
        perror("Pipe get domid");
        clean(0);
    }
    printf("Local domain id: %""u""\n", local_domid);
    printf("Grant reference id: %""u""\n", grant_ref);
    printf("Will now wait for at most 30 seconds\n");
    if(xen_shm_pipe_wait(xpipe, 30*1000)) {
        perror("Pipe wait");
        clean(0);
    }
    printf("Connected successfully !\n");
    signal(2, clean);
}

canonical_original_code:
void init_pipe_writer() {
    unsigned int local_domid;
    unsigned int dist_domid;
    unsigned int grant_ref;
    printf("Init: Writer - Offerer\n");
    if(xen_shm_pipe_init(&xpipe, xen_shm_pipe_mod_write, xen_shm_pipe_conv_writer_offers)) {
        perror("Pipe init");
        exit(-1);
    }
    pipe_used = 1;
    printf("Distant domain id: ");
    if((scanf("%""u", &dist_domid)!=1)) {
        printf("Scanf error");
        clean(0);
    }
    if(xen_shm_pipe_offers(xpipe, page_count, dist_domid, &local_domid, &grant_ref)) {
        perror("Pipe get domid");
        clean(0);
    }
    printf("Local domain id: %""u""\n", local_domid);
    printf("Grant reference id: %""u""\n", grant_ref);
    printf("Will now wait for at most 30 seconds\n");
    if(xen_shm_pipe_wait(xpipe, 30*1000)) {
        perror("Pipe wait");
        clean(0);
    }
    printf("Connected successfully !\n");
    signal(2, clean);
}


decompiled_code:
int __fastcall read_pc_and_size(__int64 a1, __int64 a2)
{
__int64 v2; // rdi

v2 = *(_QWORD *)(a2 + 16);
byte_count = 0LL;
if ( !(unsigned int)_isoc99_sscanf(v2, "%hhu", &page_count) )
{
puts("Invalid page count");
usage();
}
printf("Page count: %u\n", (unsigned __int8)page_count);
if ( !(unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 24), "%u", &buffer_size) )
{
puts("Invalid size");
usage();
}
return printf("Size: %u\n", (unsigned int)buffer_size);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v2; // rdi

v2 = *(_QWORD *)(a2 + 16);
byte_count = 0LL;
if ( !(unsigned int)_isoc99_sscanf(v2, "%hhu", &page_count) )
{
puts("Invalid page count");
func1();
}
printf("Page count: %u\n", (unsigned __int8)page_count);
if ( !(unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 24), "%u", &buffer_size) )
{
puts("Invalid size");
func1();
}
return printf("Size: %u\n", (unsigned int)buffer_size);
}

original_code:
void read_pc_and_size(int argc, char **argv) {
    byte_count = 0;
    if(sscanf(argv[2], "%""hhu", &page_count) ) {
        printf("Page count: %""u""\n", page_count);
    } else {
        printf("Invalid page count\n");
        usage();
    }
    if(sscanf(argv[3], "%""u", &buffer_size) ) {
        printf("Size: %""u""\n", buffer_size);
    } else {
        printf("Invalid size\n");
        usage();
    }
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
void __fastcall __noreturn pipe_reader(__int64 a1, __int64 a2)
{
if ( (int)a1 <= 3 )
usage();
read_pc_and_size(a1, a2);
init_pipe_reader();
pipe_read();
}

canonical_decompiled_code:
void __fastcall __noreturn func7(__int64 a1, __int64 a2)
{
if ( (int)a1 <= 3 )
func1();
func6(a1, a2);
func4();
func2();
}

original_code:
void pipe_reader(int argc, char **argv) {
    if(argc < 4) {
        usage();
    }
    read_pc_and_size(argc, argv);
    init_pipe_reader();
    pipe_read();
}


decompiled_code:
void __fastcall __noreturn pipe_writer(__int64 a1, __int64 a2)
{
if ( (int)a1 > 4 )
{
read_pc_and_size(a1, a2);
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 32), "%u", &iterations) )
{
printf("Iterations: %u\n", (unsigned int)iterations);
init_pipe_writer();
pipe_write();
}
puts("Invalid size");
}
usage();
}

canonical_decompiled_code:
void __fastcall __noreturn func8(__int64 a1, __int64 a2)
{
if ( (int)a1 > 4 )
{
func6(a1, a2);
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 32), "%u", &iterations) )
{
printf("Iterations: %u\n", (unsigned int)iterations);
func5();
func3();
}
puts("Invalid size");
}
func1();
}

original_code:
void pipe_writer(int argc, char **argv) {
    if(argc < 5) {
        usage();
    }
    read_pc_and_size(argc, argv);
    if(sscanf(argv[4], "%""u", &iterations) ) {
        printf("Iterations: %""u""\n", iterations);
    } else {
        printf("Invalid size\n");
        usage();
    }
    init_pipe_writer();
    pipe_write();
}


decompiled_code:
void __fastcall __noreturn pipe_ramwriter(int a1, __int64 a2)
{
size_t v2; // r13
unsigned int v3; // ebx
char *v4; // rbp
char *v5; // r12
int v6; // ebx
__int64 v7; // rax
char *v8; // r14
size_t v9; // r13
unsigned int v10; // [rsp+0h] [rbp-58h]
unsigned int v11; // [rsp+4h] [rbp-54h]
unsigned int v12; // [rsp+8h] [rbp-50h]
char *src; // [rsp+10h] [rbp-48h]
__int64 v14; // [rsp+18h] [rbp-40h]

if ( a1 > 3 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 16), "%u", &buffer_size) )
{
printf("Size: %u\n", (unsigned int)buffer_size);
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 24), "%u", &iterations) )
{
printf("Iterations: %u\n", (unsigned int)iterations);
v2 = (unsigned int)buffer_size;
v3 = buffer_size;
v4 = (char *)malloc((unsigned int)buffer_size);
if ( v4 )
{
v5 = (char *)malloc(v2);
if ( v5 )
{
if ( (_DWORD)v2 )
memset(v4, 117, v2);
byte_count = 0LL;
v11 = v3 >> 3;
gettimeofday(&start, 0LL);
v10 = iterations;
if ( iterations )
{
v6 = 0;
v12 = buffer_size;
v14 = (unsigned int)buffer_size;
v7 = 8 * v11;
v8 = &v5[v7];
src = &v4[v7];
v9 = (unsigned int)(buffer_size - 1 - v7) + 1LL;
do
{
if ( v11 )
memcpy(v4, v5, 8LL * (v11 - 1) + 8);
if ( v12 > 8 * v11 )
memcpy(v8, src, v9);
++v6;
}
while ( v10 != v6 );
byte_count = v14 * v10;
}
clean(0);
}
}
puts("Memory error");
clean(0);
}
}
puts("Invalid size");
}
usage();
}

canonical_decompiled_code:
void __fastcall __noreturn func9(int a1, __int64 a2)
{
size_t v2; // r13
unsigned int v3; // ebx
char *v4; // rbp
char *v5; // r12
int v6; // ebx
__int64 v7; // rax
char *v8; // r14
size_t v9; // r13
unsigned int v10; // [rsp+0h] [rbp-58h]
unsigned int v11; // [rsp+4h] [rbp-54h]
unsigned int v12; // [rsp+8h] [rbp-50h]
char *src; // [rsp+10h] [rbp-48h]
__int64 v14; // [rsp+18h] [rbp-40h]

if ( a1 > 3 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 16), "%u", &buffer_size) )
{
printf("Size: %u\n", (unsigned int)buffer_size);
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 24), "%u", &iterations) )
{
printf("Iterations: %u\n", (unsigned int)iterations);
v2 = (unsigned int)buffer_size;
v3 = buffer_size;
v4 = (char *)malloc((unsigned int)buffer_size);
if ( v4 )
{
v5 = (char *)malloc(v2);
if ( v5 )
{
if ( (_DWORD)v2 )
memset(v4, 117, v2);
byte_count = 0LL;
v11 = v3 >> 3;
gettimeofday(&start, 0LL);
v10 = iterations;
if ( iterations )
{
v6 = 0;
v12 = buffer_size;
v14 = (unsigned int)buffer_size;
v7 = 8 * v11;
v8 = &v5[v7];
src = &v4[v7];
v9 = (unsigned int)(buffer_size - 1 - v7) + 1LL;
do
{
if ( v11 )
memcpy(v4, v5, 8LL * (v11 - 1) + 8);
if ( v12 > 8 * v11 )
memcpy(v8, src, v9);
++v6;
}
while ( v10 != v6 );
byte_count = v14 * v10;
}
func0(0);
}
}
puts("Memory error");
func0(0);
}
}
puts("Invalid size");
}
func1();
}

original_code:
void pipe_ramwriter(int argc, char **argv) {
    uint8_t* buffer;
    uint8_t* buffer_2;
    uint32_t i;
    uint32_t j;
    uint32_t aligned_iter;
    if(argc < 4) {
        usage();
    }
    if(sscanf(argv[2], "%""u", &buffer_size) ) {
        printf("Size: %""u""\n", buffer_size);
    } else {
        printf("Invalid size\n");
        usage();
    }
    if(sscanf(argv[3], "%""u", &iterations) ) {
        printf("Iterations: %""u""\n", iterations);
    } else {
        printf("Invalid size\n");
        usage();
    }
    if((buffer = malloc(sizeof(uint8_t)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    if((buffer_2 = malloc(sizeof(uint8_t)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    for(i = 0; i<buffer_size; i++) {
        buffer[i] = 'u';
    }
    aligned_iter = buffer_size/8;
    byte_count = 0;
    gettimeofday(&start , ((void *)0));
    for(i=0; i<iterations; i++) {
        for(j=0; j<aligned_iter ; j++) {
            *((uint64_t*) (&buffer[8*j])) = *((uint64_t*) (&buffer_2[8*j]));
        }
        j = j*8;
        for(; j<buffer_size; j++) {
            buffer_2[j] = buffer[j];
        }
        byte_count+=buffer_size;
    }
    clean(0);
}

canonical_original_code:
void pipe_ramwriter(int argc, char **argv) {
    unsigned char *buffer;
    unsigned char *buffer_2;
    unsigned int i;
    unsigned int j;
    unsigned int aligned_iter;
    if(argc < 4) {
        usage();
    }
    if(sscanf(argv[2], "%""u", &buffer_size) ) {
        printf("Size: %""u""\n", buffer_size);
    } else {
        printf("Invalid size\n");
        usage();
    }
    if(sscanf(argv[3], "%""u", &iterations) ) {
        printf("Iterations: %""u""\n", iterations);
    } else {
        printf("Invalid size\n");
        usage();
    }
    if((buffer = malloc(sizeof(unsigned char)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    if((buffer_2 = malloc(sizeof(unsigned char)*buffer_size))== ((void *)0)) {
        printf("Memory error\n");
        clean(0);
    }
    for(i = 0; i<buffer_size; i++) {
        buffer[i] = 'u';
    }
    aligned_iter = buffer_size/8;
    byte_count = 0;
    gettimeofday(&start , ((void *)0));
    for(i=0; i<iterations; i++) {
        for(j=0; j<aligned_iter ; j++) {
            *((unsigned long *) (&buffer[8*j])) = *((unsigned long *) (&buffer_2[8*j]));
        }
        j = j*8;
        for(; j<buffer_size; j++) {
            buffer_2[j] = buffer[j];
        }
        byte_count+=buffer_size;
    }
    clean(0);
}

variable_types:
j
unsigned int


clean

decompiled_code:
void __fastcall __noreturn clean(int a1)
{
__suseconds_t v1; // rdx
double v2; // xmm0_8
double v3; // xmm2_8
__int64 v4[5]; // [rsp+10h] [rbp-38h] BYREF
unsigned __int8 v5; // [rsp+38h] [rbp-10h]

gettimeofday(&stop, 0LL);
putchar(10);
if ( a1 >= 0 )
printf("Signal received: %i\n", (unsigned int)a1);
printf("Byte count : %lu\n", byte_count);
if ( byte_count )
{
v1 = stop.tv_usec + 1000000 * stop.tv_sec - (start.tv_usec + 1000000 * start.tv_sec);
if ( v1 < 0 )
v2 = (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1)) + (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1));
else
v2 = (double)(int)v1;
if ( byte_count < 0 )
v3 = (double)(int)(byte_count & 1 | ((unsigned __int64)byte_count >> 1))
+ (double)(int)(byte_count & 1 | ((unsigned __int64)byte_count >> 1));
else
v3 = (double)(int)byte_count;
printf("Time lapse : %f seconds\n", v2 / 1000000.0);
printf("Bandwidth : %f MBps = %f Mbps\n", v3 / v2, 8.0 * (v3 / v2));
}
xen_shm_pipe_get_stats(v4, xpipe);
printf("\nWait calls : %lu\n", v4[0]);
printf("Signal calls : %lu\n", v4[2]);
printf("Write calls : %lu\n", v4[4]);
printf("Read calls : %lu\n", v4[3]);
printf("Waiting : %u\n", v5);
printf("Epipe Prone : %lu\n", v4[1]);
puts("Now closing the pipe");
xen_shm_pipe_free(xpipe);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(int a1)
{
__suseconds_t v1; // rdx
double v2; // xmm0_8
double v3; // xmm2_8
__int64 v4[5]; // [rsp+10h] [rbp-38h] BYREF
unsigned __int8 v5; // [rsp+38h] [rbp-10h]

gettimeofday(&stop, 0LL);
putchar(10);
if ( a1 >= 0 )
printf("Signal received: %i\n", (unsigned int)a1);
printf("Byte count : %lu\n", byte_count);
if ( byte_count )
{
v1 = stop.tv_usec + 1000000 * stop.tv_sec - (start.tv_usec + 1000000 * start.tv_sec);
if ( v1 < 0 )
v2 = (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1)) + (double)(int)(v1 & 1 | ((unsigned __int64)v1 >> 1));
else
v2 = (double)(int)v1;
if ( byte_count < 0 )
v3 = (double)(int)(byte_count & 1 | ((unsigned __int64)byte_count >> 1))
+ (double)(int)(byte_count & 1 | ((unsigned __int64)byte_count >> 1));
else
v3 = (double)(int)byte_count;
printf("Time lapse : %f seconds\n", v2 / 1000000.0);
printf("Bandwidth : %f MBps = %f Mbps\n", v3 / v2, 8.0 * (v3 / v2));
}
xen_shm_pipe_get_stats(v4, xpipe);
printf("\nWait calls : %lu\n", v4[0]);
printf("Signal calls : %lu\n", v4[2]);
printf("Write calls : %lu\n", v4[4]);
printf("Read calls : %lu\n", v4[3]);
printf("Waiting : %u\n", v5);
printf("Epipe Prone : %lu\n", v4[1]);
puts("Now closing the pipe");
xen_shm_pipe_free(xpipe);
exit(0);
}

original_code:
static void
clean(int sig)
{
    uint64_t usec_interval;
    double seconds;
    double bandwidth;
    struct xen_shm_pipe_stats stats;
    gettimeofday(&stop , ((void *)0));
    printf("\n");
    if(sig >= 0) {
        printf("Signal received: %i\n", sig);
    }
    printf("Byte count : %""l" "u""\n", byte_count);
    if(byte_count) {
        usec_interval = (uint64_t) ((stop.tv_sec*1000000 + stop.tv_usec) - (start.tv_sec*1000000 + start.tv_usec));
        seconds = ((double) usec_interval)/1000000.0;
        bandwidth = ((double) byte_count)/((double) usec_interval);
        printf("Time lapse : %f seconds\n", seconds);
        printf("Bandwidth  : %f MBps  =   %f Mbps\n", bandwidth, bandwidth*8);
    }
    stats = xen_shm_pipe_get_stats(xpipe);
    printf("\nWait calls   : %""l" "u""\n", stats.ioctl_count_await);
    printf("Signal calls : %""l" "u""\n", stats.ioctl_count_ssig);
    printf("Write calls  : %""l" "u""\n", stats.write_count);
    printf("Read calls   : %""l" "u""\n", stats.read_count);
    printf("Waiting      : %""u""\n", stats.waiting);
    printf("Epipe Prone  : %""l" "u""\n", stats.ioctl_count_epipe_prone);
    printf("Now closing the pipe\n");
    xen_shm_pipe_free(xpipe);
    exit(0);
}

canonical_original_code:
static void clean(int sig)
{
    unsigned long usec_interval;
    double seconds;
    double bandwidth;
    struct xen_shm_pipe_stats stats;
    gettimeofday(&stop , ((void *)0));
    printf("\n");
    if(sig >= 0) {
        printf("Signal received: %i\n", sig);
    }
    printf("Byte count : %""l" "u""\n", byte_count);
    if(byte_count) {
        usec_interval = (unsigned long) ((stop.tv_sec*1000000 + stop.tv_usec) - (start.tv_sec*1000000 + start.tv_usec));
        seconds = ((double) usec_interval)/1000000.0;
        bandwidth = ((double) byte_count)/((double) usec_interval);
        printf("Time lapse : %f seconds\n", seconds);
        printf("Bandwidth  : %f MBps  =   %f Mbps\n", bandwidth, bandwidth*8);
    }
    stats = xen_shm_pipe_get_stats(xpipe);
    printf("\nWait calls   : %""l" "u""\n", stats.ioctl_count_await);
    printf("Signal calls : %""l" "u""\n", stats.ioctl_count_ssig);
    printf("Write calls  : %""l" "u""\n", stats.write_count);
    printf("Read calls   : %""l" "u""\n", stats.read_count);
    printf("Waiting      : %""u""\n", stats.waiting);
    printf("Epipe Prone  : %""l" "u""\n", stats.ioctl_count_epipe_prone);
    printf("Now closing the pipe\n");
    xen_shm_pipe_free(xpipe);
    exit(0);
}

variable_types: sig int

variable_types: seconds double

variable_types: bandwidth double

variable_types: struct xen_shm_pipe_stats { unsigned long ioctl_count_await; unsigned long ioctl_count_epipe_prone; unsigned long ioctl_count_ssig; unsigned long read_count; unsigned long write_count; unsigned char waiting; }


usage

decompiled_code:
void __noreturn usage()
{
puts("Usage: reader <page_count> <buffer_size>");
puts(" OR writer <page_count> <message_size> <iterations>");
puts(" OR ram_writer <message_size> <iterations>");
exit(-1);
}

canonical_decompiled_code:
void __noreturn func1()
{
puts("Usage: reader <page_count> <buffer_size>");
puts(" OR writer <page_count> <message_size> <iterations>");
puts(" OR ram_writer <message_size> <iterations>");
exit(-1);
}

original_code:
void
usage(void)
{
    printf("Usage: reader <page_count> <buffer_size>\n");
    printf("  OR   writer <page_count> <message_size> <iterations>\n");
    printf("  OR   ram_writer <message_size> <iterations>\n");
    exit(-1);
}

canonical_original_code:
void usage()
{
    printf("Usage: reader <page_count> <buffer_size>\n");
    printf("  OR   writer <page_count> <message_size> <iterations>\n");
    printf("  OR   ram_writer <message_size> <iterations>\n");
    exit(-1);
}


main

canonical_name: main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // ebp
const char **v5; // r12

if ( argc > 1 )
{
v3 = argv[1];
v4 = argc;
v5 = argv;
if ( !strcmp(v3, "reader") )
pipe_reader((unsigned int)argc, (__int64)argv);
if ( !strcmp(v3, "writer") )
pipe_writer((unsigned int)argc, (__int64)argv);
argv = (const char **)"ram_writer";
*(_QWORD *)&argc = v3;
if ( !strcmp(v3, "ram_writer") )
pipe_ramwriter(v4, (__int64)v5);
}
usage(*(_QWORD *)&argc, argv, envp);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // ebp
const char **v5; // r12

if ( argc > 1 )
{
v3 = argv[1];
v4 = argc;
v5 = argv;
if ( !strcmp(v3, "reader") )
func7((unsigned int)argc, (__int64)argv);
if ( !strcmp(v3, "writer") )
func8((unsigned int)argc, (__int64)argv);
argv = (const char **)"ram_writer";
*(_QWORD *)&argc = v3;
if ( !strcmp(v3, "ram_writer") )
func9(v4, (__int64)v5);
}
func1(*(_QWORD *)&argc, argv, envp);
}

original_code:
int main(int argc, char **argv) {
    if(argc < 2) {
        usage();
    }
    pipe_used = 0;
    if(strcmp(argv[1], "reader") == 0) {
        pipe_reader(argc, argv);
    } else if(strcmp(argv[1], "writer")==0) {
        pipe_writer(argc, argv);
    } else if(strcmp(argv[1], "ram_writer")==0) {
        pipe_ramwriter(argc, argv);
    }
    usage();
    return -1;
}


server

decompiled_code:
__int64 __fastcall server(int a1, _QWORD *a2)
{
__int64 result; // rax
unsigned __int8 v3; // [rsp+9h] [rbp-1Fh] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned int v5; // [rsp+Ch] [rbp-1Ch] BYREF
__int64 v6; // [rsp+10h] [rbp-18h] BYREF
char v7; // [rsp+18h] [rbp-10h]

if ( a1 > 5 )
{
puts("Too many arguments");
usage();
result = 0xFFFFFFFFLL;
}
else if ( a1 == 5 )
{
if ( (unsigned int)_isoc99_sscanf(a2[2], "%hu", &v4) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(a2[3], "%hhu", &v3) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(a2[4], "%u", &v5) == 1 )
{
v7 = 1;
v6 = v5;
result = run_server(v4, v3, &xen_shm_handler_receiver, &v6);
}
else
{
puts("Bad page count");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad page number");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad port");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Not enough arguments");
usage();
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, _QWORD *a2)
{
__int64 result; // rax
unsigned __int8 v3; // [rsp+9h] [rbp-1Fh] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned int v5; // [rsp+Ch] [rbp-1Ch] BYREF
__int64 v6; // [rsp+10h] [rbp-18h] BYREF
char v7; // [rsp+18h] [rbp-10h]

if ( a1 > 5 )
{
puts("Too many arguments");
func0();
result = 0xFFFFFFFFLL;
}
else if ( a1 == 5 )
{
if ( (unsigned int)_isoc99_sscanf(a2[2], "%hu", &v4) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(a2[3], "%hhu", &v3) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(a2[4], "%u", &v5) == 1 )
{
v7 = 1;
v6 = v5;
result = run_server(v4, v3, &xen_shm_handler_receiver, &v6);
}
else
{
puts("Bad page count");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad page number");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad port");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Not enough arguments");
func0();
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int server(int argc, char **argv) {
    in_port_t port;
    uint8_t page_count;
    uint32_t buffer_size;
    struct xen_shm_handler_transfert tr;
    if (argc > 5) {
        printf("Too many arguments\n");
        usage();
        return -1;
    }
    if (argc < 5) {
        printf("Not enough arguments\n");
        usage();
        return -1;
    }
    if (sscanf(argv[2], "%""hu", &port) != 1) {
        printf("Bad port\n");
        usage();
        return -1;
    }
    if (sscanf(argv[3], "%""hhu", &page_count) != 1) {
        printf("Bad page number\n");
        usage();
        return -1;
    }
    if (sscanf(argv[4], "%""u", &buffer_size) != 1) {
        printf("Bad page count\n");
        usage();
        return -1;
    }
    tr.buffer_len = (size_t) buffer_size;
    tr.print_info = 1;
    return run_server(port, page_count, xen_shm_handler_receiver, &tr);
}

canonical_original_code:
int server(int argc, char **argv) {
    unsigned short port;
    unsigned char page_count;
    unsigned int buffer_size;
    struct xen_shm_handler_transfert tr;
    if (argc > 5) {
        printf("Too many arguments\n");
        usage();
        return -1;
    }
    if (argc < 5) {
        printf("Not enough arguments\n");
        usage();
        return -1;
    }
    if (sscanf(argv[2], "%""hu", &port) != 1) {
        printf("Bad port\n");
        usage();
        return -1;
    }
    if (sscanf(argv[3], "%""hhu", &page_count) != 1) {
        printf("Bad page number\n");
        usage();
        return -1;
    }
    if (sscanf(argv[4], "%""u", &buffer_size) != 1) {
        printf("Bad page count\n");
        usage();
        return -1;
    }
    tr.buffer_len = (unsigned long) buffer_size;
    tr.print_info = 1;
    return run_server(port, page_count, xen_shm_handler_receiver, &tr);
}

variable_types:
port
unsigned short

variable_types: struct xen_shm_handler_transfert { unsigned long buffer_len; unsigned char print_info; }


client

decompiled_code:
__int64 __fastcall client(int a1, __int64 a2)
{
int v2; // er12
__int64 v3; // rdi
_QWORD *v4; // r14
_QWORD *v5; // rbp
char *v6; // rbx
_QWORD *v7; // r12
int v8; // er13
__int64 v9; // rax
__int64 v10; // rdx
__int64 v11; // rdi
unsigned int v12; // eax
__int64 result; // rax
__int64 v14; // rbx
pthread_t v15; // rdi
unsigned int v16; // [rsp+Ch] [rbp-4Ch]
unsigned __int8 v17; // [rsp+1Dh] [rbp-3Bh] BYREF
unsigned __int16 v18; // [rsp+1Eh] [rbp-3Ah] BYREF
char buf[4]; // [rsp+20h] [rbp-38h] BYREF
unsigned int v20; // [rsp+24h] [rbp-34h] BYREF
int v21; // [rsp+28h] [rbp-30h] BYREF
unsigned int seconds; // [rsp+2Ch] [rbp-2Ch] BYREF

if ( a1 > 8 )
{
puts("Too many arguments");
usage();
result = 0xFFFFFFFFLL;
}
else if ( a1 == 8 )
{
if ( inet_pton(2, *(const char **)(a2 + 16), buf) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 24), "%hu", &v18) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 32), "%hhu", &v17) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 40), "%u", &v20) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 48), "%u", &v21) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 56), "%u", &seconds) == 1 )
{
v2 = v21;
v3 = (unsigned int)v21;
v4 = malloc(8LL * (unsigned int)v21);
v5 = malloc(32 * v3);
v6 = (char *)malloc(16 * v3);
if ( v5 == 0LL || v4 == 0LL || !v6 )
{
puts("No memory");
result = 0xFFFFFFFFLL;
}
else
{
if ( v2 > 0 )
{
v7 = v4;
v8 = 0;
do
{
v9 = v20;
v6[8] = 1;
v10 = v17;
v11 = v18;
*(_QWORD *)v6 = v9;
v5[3] = v6;
v12 = run_client_thread(v11, buf, v10, &xen_shm_handler_sender, v5, v7);
if ( v12 )
{
v16 = v12;
perror("run client thread");
return v16;
}
++v8;
v6 += 16;
v5 += 4;
++v7;
sleep(seconds);
}
while ( v21 > v8 );
if ( v21 > 0 )
{
v14 = 0LL;
do
{
v15 = v4[v14++];
pthread_join(v15, 0LL);
}
while ( v21 > (int)v14 );
}
}
result = 0LL;
}
}
else
{
puts("Bad sleep interval");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad client count");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad buffer size");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad page count");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad port");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad address");
usage();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Not enough arguments");
usage();
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
int v2; // er12
__int64 v3; // rdi
_QWORD *v4; // r14
_QWORD *v5; // rbp
char *v6; // rbx
_QWORD *v7; // r12
int v8; // er13
__int64 v9; // rax
__int64 v10; // rdx
__int64 v11; // rdi
unsigned int v12; // eax
__int64 result; // rax
__int64 v14; // rbx
pthread_t v15; // rdi
unsigned int v16; // [rsp+Ch] [rbp-4Ch]
unsigned __int8 v17; // [rsp+1Dh] [rbp-3Bh] BYREF
unsigned __int16 v18; // [rsp+1Eh] [rbp-3Ah] BYREF
char buf[4]; // [rsp+20h] [rbp-38h] BYREF
unsigned int v20; // [rsp+24h] [rbp-34h] BYREF
int v21; // [rsp+28h] [rbp-30h] BYREF
unsigned int seconds; // [rsp+2Ch] [rbp-2Ch] BYREF

if ( a1 > 8 )
{
puts("Too many arguments");
func0();
result = 0xFFFFFFFFLL;
}
else if ( a1 == 8 )
{
if ( inet_pton(2, *(const char **)(a2 + 16), buf) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 24), "%hu", &v18) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 32), "%hhu", &v17) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 40), "%u", &v20) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 48), "%u", &v21) == 1 )
{
if ( (unsigned int)_isoc99_sscanf(*(_QWORD *)(a2 + 56), "%u", &seconds) == 1 )
{
v2 = v21;
v3 = (unsigned int)v21;
v4 = malloc(8LL * (unsigned int)v21);
v5 = malloc(32 * v3);
v6 = (char *)malloc(16 * v3);
if ( v5 == 0LL || v4 == 0LL || !v6 )
{
puts("No memory");
result = 0xFFFFFFFFLL;
}
else
{
if ( v2 > 0 )
{
v7 = v4;
v8 = 0;
do
{
v9 = v20;
v6[8] = 1;
v10 = v17;
v11 = v18;
*(_QWORD *)v6 = v9;
v5[3] = v6;
v12 = run_client_thread(v11, buf, v10, &xen_shm_handler_sender, v5, v7);
if ( v12 )
{
v16 = v12;
perror("run client thread");
return v16;
}
++v8;
v6 += 16;
v5 += 4;
++v7;
sleep(seconds);
}
while ( v21 > v8 );
if ( v21 > 0 )
{
v14 = 0LL;
do
{
v15 = v4[v14++];
pthread_join(v15, 0LL);
}
while ( v21 > (int)v14 );
}
}
result = 0LL;
}
}
else
{
puts("Bad sleep interval");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad client count");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad buffer size");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad page count");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad port");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Bad address");
func0();
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Not enough arguments");
func0();
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int client(int argc, char **argv) {
    int retval;
    int i;
    in_port_t port;
    uint8_t page_count;
    struct in_addr addr;
    uint32_t buffer_size;
    uint32_t client_count;
    uint32_t sleep_interval;
    pthread_t* thread_infos;
    struct xen_shm_handler_data* datas;
    struct xen_shm_handler_transfert* trs;
    if (argc > 8) {
        printf("Too many arguments\n");
        usage();
        return -1;
    }
    if (argc < 8) {
        printf("Not enough arguments\n");
        usage();
        return -1;
    }
    retval = inet_pton(2, argv[2], &addr);
    if (retval != 1) {
        printf("Bad address\n");
        usage();
        return -1;
    }
    if (sscanf(argv[3], "%""hu", &port) != 1) {
        printf("Bad port\n");
        usage();
        return -1;
    }
    if (sscanf(argv[4], "%""hhu", &page_count) != 1) {
        printf("Bad page count\n");
        usage();
        return -1;
    }
    if (sscanf(argv[5], "%""u", &buffer_size) != 1) {
        printf("Bad buffer size\n");
        usage();
        return -1;
    }
    if (sscanf(argv[6], "%""u", &client_count) != 1) {
        printf("Bad client count\n");
        usage();
        return -1;
    }
    if (sscanf(argv[7], "%""u", &sleep_interval) != 1) {
        printf("Bad sleep interval\n");
        usage();
        return -1;
    }
    thread_infos = malloc(sizeof(pthread_t)*client_count);
    datas = malloc(sizeof(struct xen_shm_handler_data)*client_count);
    trs = malloc(sizeof(struct xen_shm_handler_transfert)*client_count);
    if(thread_infos==((void *)0) || datas==((void *)0) || trs==((void *)0)) {
        printf("No memory\n");
        return -1;
    }
    for (i=0; i<(int) client_count; i++) {
        trs[i].buffer_len = (size_t) buffer_size;
        trs[i].print_info = 1;
        datas[i].private_data = &(trs[i]);
        retval = run_client_thread(port, &addr, page_count, xen_shm_handler_sender, &(datas[i]), &(thread_infos[i]));
        if(retval != 0) {
            perror("run client thread");
            return retval;
        }
        sleep(sleep_interval);
    }
    for (i=0; i<(int)client_count; i++) {
        pthread_join(thread_infos[i], ((void *)0));
    }
    return 0;
}

canonical_original_code:
int client(int argc, char **argv) {
    int retval;
    int i;
    unsigned short port;
    unsigned char page_count;
    struct in_addr addr;
    unsigned int buffer_size;
    unsigned int client_count;
    unsigned int sleep_interval;
    unsigned long *thread_infos;
    struct xen_shm_handler_data *datas;
    struct xen_shm_handler_transfert *trs;
    if (argc > 8) {
        printf("Too many arguments\n");
        usage();
        return -1;
    }
    if (argc < 8) {
        printf("Not enough arguments\n");
        usage();
        return -1;
    }
    retval = inet_pton(2, argv[2], &addr);
    if (retval != 1) {
        printf("Bad address\n");
        usage();
        return -1;
    }
    if (sscanf(argv[3], "%""hu", &port) != 1) {
        printf("Bad port\n");
        usage();
        return -1;
    }
    if (sscanf(argv[4], "%""hhu", &page_count) != 1) {
        printf("Bad page count\n");
        usage();
        return -1;
    }
    if (sscanf(argv[5], "%""u", &buffer_size) != 1) {
        printf("Bad buffer size\n");
        usage();
        return -1;
    }
    if (sscanf(argv[6], "%""u", &client_count) != 1) {
        printf("Bad client count\n");
        usage();
        return -1;
    }
    if (sscanf(argv[7], "%""u", &sleep_interval) != 1) {
        printf("Bad sleep interval\n");
        usage();
        return -1;
    }
    thread_infos = malloc(sizeof(pthread_t)*client_count);
    datas = malloc(sizeof(struct xen_shm_handler_data)*client_count);
    trs = malloc(sizeof(struct xen_shm_handler_transfert)*client_count);
    if(thread_infos==((void *)0) || datas==((void *)0) || trs==((void *)0)) {
        printf("No memory\n");
        return -1;
    }
    for (i=0; i<(int) client_count; i++) {
        trs[i].buffer_len = (unsigned long) buffer_size;
        trs[i].print_info = 1;
        datas[i].private_data = &(trs[i]);
        retval = run_client_thread(port, &addr, page_count, xen_shm_handler_sender, &(datas[i]), &(thread_infos[i]));
        if(retval != 0) {
            perror("run client thread");
            return retval;
        }
        sleep(sleep_interval);
    }
    for (i=0; i<(int)client_count; i++) {
        pthread_join(thread_infos[i], ((void *)0));
    }
    return 0;
}

variable_types: retval int

variable_types: i int

variable_types:
addr
struct in_addr { unsigned int s_addr; }

variable_types: struct xen_shm_handler_data { void *receive_fd; void *send_fd; int stop; void *private_data; } *

variable_types: struct xen_shm_handler_transfert { unsigned long buffer_len; unsigned char print_info; } *


usage

decompiled_code:
int usage()
{
puts("server <port> <pages_count> <buffer_size> ");
return puts("client <addr> <port> <pages_count> <buffer_size> <client_count> <sec_between_clients_start>");
}

canonical_decompiled_code:
int func0()
{
puts("server <port> <pages_count> <buffer_size> ");
return puts("client <addr> <port> <pages_count> <buffer_size> <client_count> <sec_between_clients_start>");
}

original_code:
void usage(void) {
    printf("server <port> <pages_count> <buffer_size> \n");
    printf("client <addr> <port> <pages_count> <buffer_size> <client_count> <sec_between_clients_start>\n");
}

canonical_original_code:
void usage() {
    printf("server <port> <pages_count> <buffer_size> \n");
    printf("client <addr> <port> <pages_count> <buffer_size> <client_count> <sec_between_clients_start>\n");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
usage();
if ( !strcmp(argv[1], "server") )
return server(argc, argv);
if ( !strcmp(argv[1], "client") )
return client(argc, (__int64)argv);
usage();
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
func0();
if ( !strcmp(argv[1], "server") )
return func1(argc, argv);
if ( !strcmp(argv[1], "client") )
return func2(argc, (__int64)argv);
func0();
return -1;
}

original_code:
int main(int argc, char **argv) {
    if(argc < 2) {
        usage();
    }
    if(strcmp(argv[1], "server") == 0) {
        return server(argc, argv);
    } else if(strcmp(argv[1], "client")==0) {
        return client(argc, argv);
    }
    usage();
    return -1;
}


decompiled_code:
__int64 xen_shm_handler_get_id()
{
return (unsigned int)unique_handler_id++;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)unique_handler_id++;
}

original_code:
uint32_t
xen_shm_handler_get_id(void) {
    return unique_handler_id++;
}

canonical_original_code:
unsigned int xen_shm_handler_get_id() {
    return unique_handler_id++;
}

return_type: unsigned int