worker

decompiled_code:
__int64 worker()
{
unsigned int v0; // eax
int v1; // ebp
__m128i v2; // xmm1
unsigned __int16 v3; // bx
char *v4; // rax
unsigned __int16 v5; // bx
char *v6; // rax
__int64 result; // rax
unsigned __int16 v8; // bx
char *v9; // rax
char *ptr; // [rsp+8h] [rbp-30h] BYREF
struct in_addr in[4]; // [rsp+10h] [rbp-28h] BYREF

ptr = 0LL;
v0 = getpid();
if ( asprintf(&ptr, "%s/synexec_slave_conf.%d", "/tmp/", v0) < 0 )
{
perror("asprintf");
fprintf(stderr, "%s: Error allocating memory for configuration file name.\n", "worker");
quit = 2;
result = 0LL;
}
else
{
if ( access("/tmp/", 2) )
{
perror("access");
fprintf(stderr, "%s: Unable to write to configuration directory '%s'.\n", "worker", "/tmp/");
quit = 2;
}
else
{
signal(17, (__sighandler_t)sigchld_h);
while ( !quit )
{
v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
perror("socket");
fprintf(stderr, "%s: Error creating worker TCP socket.\n", "worker");
quit = 2;
break;
}
*(_OWORD *)&in[0].s_addr = 0LL;
pthread_mutex_lock(&master_mutex);
while ( !(master_addr.m128i_i64[0] ^ *(_QWORD *)&in[0].s_addr | master_addr.m128i_i64[1] ^ *(_QWORD *)&in[2].s_addr) )
pthread_cond_wait(&master_cond, &master_mutex);
v2 = _mm_loadu_si128(&master_addr);
master_addr = 0LL;
*(__m128i *)&in[0].s_addr = v2;
pthread_mutex_unlock(&master_mutex);
if ( quit )
goto LABEL_31;
if ( verbose > 0 )
{
v3 = _bswap_16(SHIWORD(in[0].s_addr));
v4 = inet_ntoa(in[1]);
printf("%s: Connecting to '%s:%hu' with socket %d.\n", "worker", v4, v3, (unsigned int)v1);
}
if ( connect(v1, (const struct sockaddr *)in, 0x10u) < 0 )
{
perror("connect");
v8 = _bswap_16(SHIWORD(in[0].s_addr));
v9 = inet_ntoa(in[1]);
fprintf(stderr, "%s: Error connecting to master at '%s:%hu'. Looping...\n", "worker", v9, v8);
close(v1);
pthread_mutex_lock(&master_mutex);
master_addr = 0LL;
pthread_mutex_unlock(&master_mutex);
}
else
{
if ( verbose > 0 )
{
v5 = _bswap_16(SHIWORD(in[0].s_addr));
v6 = inet_ntoa(in[1]);
printf("%s: Connected to '%s:%hu'.\n", "worker", v6, v5);
}
if ( (int)comm_send((unsigned int)v1, 0LL, 0LL, 0LL, 0LL) >= 0 )
{
worker_time = 0LL;
tv = 0LL;
xmmword_D00 = 0LL;
if ( (unsigned int)handle_conn(v1, ptr) )
{
quit = 2;
LABEL_31:
close(v1);
break;
}
if ( ptr && !access(ptr, 2) )
unlink(ptr);
}
close(v1);
pthread_mutex_lock(&master_mutex);
master_addr = 0LL;
pthread_mutex_unlock(&master_mutex);
if ( verbose > 0 )
{
printf("%s: Disconnecting from master and looping...\n", "worker");
fflush(stdout);
}
}
}
}
if ( ptr )
{
if ( !access(ptr, 2) )
unlink(ptr);
free(ptr);
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // eax
int v1; // ebp
__m128i v2; // xmm1
unsigned __int16 v3; // bx
char *v4; // rax
unsigned __int16 v5; // bx
char *v6; // rax
__int64 result; // rax
unsigned __int16 v8; // bx
char *v9; // rax
char *ptr; // [rsp+8h] [rbp-30h] BYREF
struct in_addr in[4]; // [rsp+10h] [rbp-28h] BYREF

ptr = 0LL;
v0 = getpid();
if ( asprintf(&ptr, "%s/synexec_slave_conf.%d", "/tmp/", v0) < 0 )
{
perror("asprintf");
fprintf(stderr, "%s: Error allocating memory for configuration file name.\n", "worker");
quit = 2;
result = 0LL;
}
else
{
if ( access("/tmp/", 2) )
{
perror("access");
fprintf(stderr, "%s: Unable to write to configuration directory '%s'.\n", "worker", "/tmp/");
quit = 2;
}
else
{
signal(17, (__sighandler_t)sigchld_h);
while ( !quit )
{
v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
perror("socket");
fprintf(stderr, "%s: Error creating worker TCP socket.\n", "worker");
quit = 2;
break;
}
*(_OWORD *)&in[0].s_addr = 0LL;
pthread_mutex_lock(&master_mutex);
while ( !(master_addr.m128i_i64[0] ^ *(_QWORD *)&in[0].s_addr | master_addr.m128i_i64[1] ^ *(_QWORD *)&in[2].s_addr) )
pthread_cond_wait(&master_cond, &master_mutex);
v2 = _mm_loadu_si128(&master_addr);
master_addr = 0LL;
*(__m128i *)&in[0].s_addr = v2;
pthread_mutex_unlock(&master_mutex);
if ( quit )
goto LABEL_31;
if ( verbose > 0 )
{
v3 = func2(SHIWORD(in[0].s_addr));
v4 = inet_ntoa(in[1]);
printf("%s: Connecting to '%s:%hu' with socket %d.\n", "worker", v4, v3, (unsigned int)v1);
}
if ( connect(v1, (const struct sockaddr *)in, 0x10u) < 0 )
{
perror("connect");
v8 = func2(SHIWORD(in[0].s_addr));
v9 = inet_ntoa(in[1]);
fprintf(stderr, "%s: Error connecting to master at '%s:%hu'. Looping...\n", "worker", v9, v8);
close(v1);
pthread_mutex_lock(&master_mutex);
master_addr = 0LL;
pthread_mutex_unlock(&master_mutex);
}
else
{
if ( verbose > 0 )
{
v5 = func2(SHIWORD(in[0].s_addr));
v6 = inet_ntoa(in[1]);
printf("%s: Connected to '%s:%hu'.\n", "worker", v6, v5);
}
if ( (int)comm_send((unsigned int)v1, 0LL, 0LL, 0LL, 0LL) >= 0 )
{
worker_time = 0LL;
tv = 0LL;
xmmword_D00 = 0LL;
if ( (unsigned int)func4(v1, ptr) )
{
quit = 2;
LABEL_31:
close(v1);
break;
}
if ( ptr && !access(ptr, 2) )
unlink(ptr);
}
close(v1);
pthread_mutex_lock(&master_mutex);
master_addr = 0LL;
pthread_mutex_unlock(&master_mutex);
if ( verbose > 0 )
{
printf("%s: Disconnecting from master and looping...\n", "worker");
fflush(stdout);
}
}
}
}
if ( ptr )
{
if ( !access(ptr, 2) )
unlink(ptr);
free(ptr);
}
result = 0LL;
}
return result;
}

original_code:
void *
worker(){
 int worker_fd = -1;
 struct sockaddr_in worker_addr;
 char *conf_fn = ((void *)0);
 if (asprintf(&conf_fn, "%s/synexec_slave_conf.%d", "/tmp/", getpid()) < 0){
  perror("asprintf");
  fprintf(stderr, "%s: Error allocating memory for configuration file name.\n", __FUNCTION__);
  conf_fn = ((void *)0);
  goto err;
 }
 if (access("/tmp/", 2) != 0){
  perror("access");
  fprintf(stderr, "%s: Unable to write to configuration directory '%s'.\n", __FUNCTION__, "/tmp/");
  goto err;
 }
 signal(17, sigchld_h);
 while (!quit){
  if ((worker_fd = socket(2, SOCK_STREAM, 0)) < 0){
   perror("socket");
   fprintf(stderr, "%s: Error creating worker TCP socket.\n", __FUNCTION__);
   goto err;
  }
  memset(&worker_addr, 0, sizeof(worker_addr));
  pthread_mutex_lock(&master_mutex);
  while(!memcmp(&worker_addr, &master_addr, sizeof(worker_addr)))
   pthread_cond_wait(&master_cond, &master_mutex);
  memcpy(&worker_addr, &master_addr, sizeof(worker_addr));
  memset(&master_addr, 0, sizeof(master_addr));
  pthread_mutex_unlock(&master_mutex);
  if (quit){
   break;
  }
  if (verbose > 0){
   printf("%s: Connecting to '%s:%hu' with socket %d.\n", __FUNCTION__,
    inet_ntoa(worker_addr.sin_addr), ntohs(worker_addr.sin_port), worker_fd);
  }
  if (connect(worker_fd, (struct sockaddr *)&worker_addr, sizeof(worker_addr)) < 0){
   perror("connect");
   fprintf(stderr, "%s: Error connecting to master at '%s:%hu'. Looping...\n", __FUNCTION__,
    inet_ntoa(worker_addr.sin_addr), ntohs(worker_addr.sin_port));
   close(worker_fd);
   worker_fd = -1;
   pthread_mutex_lock(&master_mutex);
   memset(&master_addr, 0, sizeof(master_addr));
   pthread_mutex_unlock(&master_mutex);
   continue;
  }
  if (verbose > 0){
   printf("%s: Connected to '%s:%hu'.\n", __FUNCTION__,
    inet_ntoa(worker_addr.sin_addr), ntohs(worker_addr.sin_port));
  }
  if (comm_send(worker_fd, 0, ((void *)0), ((void *)0), 0) < 0){
   goto conn_fail;
  }
  memset(&worker_time, 0, sizeof(worker_time));
  if (handle_conn(worker_fd, conf_fn) != 0){
   goto err;
  }
  if (conf_fn && (access(conf_fn, 2) == 0)){
   unlink(conf_fn);
  }
conn_fail:
  close(worker_fd);
  worker_fd = -1;
  pthread_mutex_lock(&master_mutex);
  memset(&master_addr, 0, sizeof(master_addr));
  pthread_mutex_unlock(&master_mutex);
  if (verbose > 0){
   printf("%s: Disconnecting from master and looping...\n", __FUNCTION__);
   fflush(stdout);
  }
 }
 goto out;
err:
 quit = 2;
out:
 if (worker_fd >= 0){
  close(worker_fd);
 }
 if (conf_fn){
  if (access(conf_fn, 2) == 0){
   unlink(conf_fn);
  }
  free(conf_fn);
  conf_fn = ((void *)0);
 }
 return ((void *)0);
}

canonical_original_code:
void *worker(){
 int worker_fd = -1;
 struct sockaddr_in worker_addr;
 char *conf_fn = ((void *)0);
 if (asprintf(&conf_fn, "%s/synexec_slave_conf.%d", "/tmp/", getpid()) < 0){
  perror("asprintf");
  fprintf(stderr, "%s: Error allocating memory for configuration file name.\n", __FUNCTION__);
  conf_fn = ((void *)0);
  goto err;
 }
 if (access("/tmp/", 2) != 0){
  perror("access");
  fprintf(stderr, "%s: Unable to write to configuration directory '%s'.\n", __FUNCTION__, "/tmp/");
  goto err;
 }
 signal(17, sigchld_h);
 while (!quit){
  if ((worker_fd = socket(2, SOCK_STREAM, 0)) < 0){
   perror("socket");
   fprintf(stderr, "%s: Error creating worker TCP socket.\n", __FUNCTION__);
   goto err;
  }
  memset(&worker_addr, 0, sizeof(worker_addr));
  pthread_mutex_lock(&master_mutex);
  while(!memcmp(&worker_addr, &master_addr, sizeof(worker_addr)))
   pthread_cond_wait(&master_cond, &master_mutex);
  memcpy(&worker_addr, &master_addr, sizeof(worker_addr));
  memset(&master_addr, 0, sizeof(master_addr));
  pthread_mutex_unlock(&master_mutex);
  if (quit){
   break;
  }
  if (verbose > 0){
   printf("%s: Connecting to '%s:%hu' with socket %d.\n", __FUNCTION__,
    inet_ntoa(worker_addr.sin_addr), ntohs(worker_addr.sin_port), worker_fd);
  }
  if (connect(worker_fd, (struct sockaddr *)&worker_addr, sizeof(worker_addr)) < 0){
   perror("connect");
   fprintf(stderr, "%s: Error connecting to master at '%s:%hu'. Looping...\n", __FUNCTION__,
    inet_ntoa(worker_addr.sin_addr), ntohs(worker_addr.sin_port));
   close(worker_fd);
   worker_fd = -1;
   pthread_mutex_lock(&master_mutex);
   memset(&master_addr, 0, sizeof(master_addr));
   pthread_mutex_unlock(&master_mutex);
   continue;
  }
  if (verbose > 0){
   printf("%s: Connected to '%s:%hu'.\n", __FUNCTION__,
    inet_ntoa(worker_addr.sin_addr), ntohs(worker_addr.sin_port));
  }
  if (comm_send(worker_fd, 0, ((void *)0), ((void *)0), 0) < 0){
   goto conn_fail;
  }
  memset(&worker_time, 0, sizeof(worker_time));
  if (handle_conn(worker_fd, conf_fn) != 0){
   goto err;
  }
  if (conf_fn && (access(conf_fn, 2) == 0)){
   unlink(conf_fn);
  }
conn_fail:
  close(worker_fd);
  worker_fd = -1;
  pthread_mutex_lock(&master_mutex);
  memset(&master_addr, 0, sizeof(master_addr));
  pthread_mutex_unlock(&master_mutex);
  if (verbose > 0){
   printf("%s: Disconnecting from master and looping...\n", __FUNCTION__);
   fflush(stdout);
  }
 }
 goto out;
err:
 quit = 2;
out:
 if (worker_fd >= 0){
  close(worker_fd);
 }
 if (conf_fn){
  if (access(conf_fn, 2) == 0){
   unlink(conf_fn);
  }
  free(conf_fn);
  conf_fn = ((void *)0);
 }
 return ((void *)0);
}

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]; }

return_type: void *


repo: franciozzy/synexec

decompiled_code:
__int64 __fastcall get_ifipaddr(const __m128i *a1, _DWORD *a2)
{
int v2; // eax
int v3; // er13
__m128i v4; // xmm1
unsigned int v5; // er12
__int64 result; // rax
__m128i v7; // [rsp+0h] [rbp-58h] BYREF
__int128 v8; // [rsp+10h] [rbp-48h]
__int64 v9; // [rsp+20h] [rbp-38h]

if ( !a1 || !a2 )
{
v5 = -1;
fprintf(stderr, "%s: invalid arguments.\n", "get_ifipaddr");
return v5;
}
if ( !strcmp(a1->m128i_i8, "any") )
{
*a2 = 0;
result = 0LL;
}
else
{
v2 = socket(2, 2, 0);
v3 = v2;
if ( v2 >= 0 )
{
v4 = _mm_loadu_si128(a1);
v8 = 0LL;
v9 = 0LL;
v7 = v4;
if ( ioctl(v2, 0x8915uLL, &v7) < 0 )
{
v5 = -1;
perror("ioctl");
fprintf(stderr, "%s: error fetching interface address.\n", "get_ifipaddr");
}
else
{
v5 = 0;
*a2 = DWORD1(v8);
}
close(v3);
return v5;
}
perror("socket");
fprintf(stderr, "%s: error creating temporary socket.\n", "get_ifipaddr");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const __m128i *a1, _DWORD *a2)
{
int v2; // eax
int v3; // er13
__m128i v4; // xmm1
unsigned int v5; // er12
__int64 result; // rax
__m128i v7; // [rsp+0h] [rbp-58h] BYREF
__int128 v8; // [rsp+10h] [rbp-48h]
__int64 v9; // [rsp+20h] [rbp-38h]

if ( !a1 || !a2 )
{
v5 = -1;
fprintf(stderr, "%s: invalid arguments.\n", "get_ifipaddr");
return v5;
}
if ( !strcmp(a1->m128i_i8, "any") )
{
*a2 = 0;
result = 0LL;
}
else
{
v2 = socket(2, 2, 0);
v3 = v2;
if ( v2 >= 0 )
{
v4 = _mm_loadu_si128(a1);
v8 = 0LL;
v9 = 0LL;
v7 = v4;
if ( ioctl(v2, 0x8915uLL, &v7) < 0 )
{
v5 = -1;
perror("ioctl");
fprintf(stderr, "%s: error fetching interface address.\n", "get_ifipaddr");
}
else
{
v5 = 0;
*a2 = DWORD1(v8);
}
close(v3);
return v5;
}
perror("socket");
fprintf(stderr, "%s: error creating temporary socket.\n", "get_ifipaddr");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
get_ifipaddr(char *if_name, struct in_addr *if_addr){
 int sock = -1;
 struct ifreq ifr;
 int err = 0;
 if (!if_name || !if_addr){
  fprintf(stderr, "%s: invalid arguments.\n", __FUNCTION__);
  goto err;
 }
 if (!strcmp(if_name, "any")){
  memset(&(if_addr->s_addr), 0, sizeof(if_addr->s_addr));
  goto out;
 }
 if ((sock = socket(2, SOCK_DGRAM, 0)) < 0){
  perror("socket");
  fprintf(stderr, "%s: error creating temporary socket.\n", __FUNCTION__);
  goto err;
 }
 memset(&ifr, 0, sizeof(ifr));
 memcpy(&ifr.ifr_ifrn.ifrn_name, if_name, sizeof(ifr.ifr_ifrn.ifrn_name));
 if (ioctl(sock, 0x8915, &ifr) < 0){
  perror("ioctl");
  fprintf(stderr, "%s: error fetching interface address.\n", __FUNCTION__);
  goto err;
 }
 memcpy(if_addr, (ifr.ifr_ifru.ifru_addr.sa_data)+2, sizeof(*if_addr));
out:
 if (sock >= 0){
  close(sock);
 }
 return(err);
err:
 err = -1;
 goto out;
}

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types: sock int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

variable_types: err int

return_type: int

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


decompiled_code:
__int64 __fastcall get_ifbroad(const __m128i *a1, _DWORD *a2)
{
int v2; // eax
int v3; // er13
__m128i v4; // xmm1
unsigned int v5; // er12
__int64 result; // rax
__m128i v7; // [rsp+0h] [rbp-58h] BYREF
__int128 v8; // [rsp+10h] [rbp-48h]
__int64 v9; // [rsp+20h] [rbp-38h]

if ( !a1 || !a2 )
{
v5 = -1;
fprintf(stderr, "%s: invalid arguments.\n", "get_ifbroad");
return v5;
}
if ( !strcmp(a1->m128i_i8, "any") )
{
*a2 = -1;
result = 0LL;
}
else
{
v2 = socket(2, 2, 0);
v3 = v2;
if ( v2 >= 0 )
{
v4 = _mm_loadu_si128(a1);
v8 = 0LL;
v9 = 0LL;
v7 = v4;
if ( ioctl(v2, 0x8919uLL, &v7) < 0 )
{
v5 = -1;
fprintf(stderr, "%s: error fetching interface netmask.\n", "get_ifbroad");
perror("ioctl");
}
else
{
v5 = 0;
*a2 = DWORD1(v8);
}
close(v3);
return v5;
}
fprintf(stderr, "%s: error creating temporary socket.\n", "get_ifbroad");
perror("socket");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const __m128i *a1, _DWORD *a2)
{
int v2; // eax
int v3; // er13
__m128i v4; // xmm1
unsigned int v5; // er12
__int64 result; // rax
__m128i v7; // [rsp+0h] [rbp-58h] BYREF
__int128 v8; // [rsp+10h] [rbp-48h]
__int64 v9; // [rsp+20h] [rbp-38h]

if ( !a1 || !a2 )
{
v5 = -1;
fprintf(stderr, "%s: invalid arguments.\n", "get_ifbroad");
return v5;
}
if ( !strcmp(a1->m128i_i8, "any") )
{
*a2 = -1;
result = 0LL;
}
else
{
v2 = socket(2, 2, 0);
v3 = v2;
if ( v2 >= 0 )
{
v4 = _mm_loadu_si128(a1);
v8 = 0LL;
v9 = 0LL;
v7 = v4;
if ( ioctl(v2, 0x8919uLL, &v7) < 0 )
{
v5 = -1;
fprintf(stderr, "%s: error fetching interface netmask.\n", "get_ifbroad");
perror("ioctl");
}
else
{
v5 = 0;
*a2 = DWORD1(v8);
}
close(v3);
return v5;
}
fprintf(stderr, "%s: error creating temporary socket.\n", "get_ifbroad");
perror("socket");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
get_ifbroad(char *if_name, struct in_addr *if_broad){
 int sock = -1;
 struct ifreq ifr;
 int err = 0;
 if (!if_name || !if_broad){
  fprintf(stderr, "%s: invalid arguments.\n", __FUNCTION__);
  goto err;
 }
 if (!strcmp(if_name, "any")){
  memset(&(if_broad->s_addr), 0xFF, sizeof(if_broad->s_addr));
  goto out;
 }
 if ((sock = socket(2, SOCK_DGRAM, 0)) < 0){
  fprintf(stderr, "%s: error creating temporary socket.\n", __FUNCTION__);
  perror("socket");
  goto err;
 }
 memset(&ifr, 0, sizeof(ifr));
 memcpy(&ifr.ifr_ifrn.ifrn_name, if_name, sizeof(ifr.ifr_ifrn.ifrn_name));
 if (ioctl(sock, 0x8919, &ifr) < 0){
  fprintf(stderr, "%s: error fetching interface netmask.\n", __FUNCTION__);
  perror("ioctl");
  goto err;
 }
 memcpy(if_broad, (ifr.ifr_ifru.ifru_addr.sa_data)+2, sizeof(*if_broad));
out:
 if (sock >= 0){
  close(sock);
 }
 return(err);
err:
 err = -1;
 goto out;
}

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

user_defined_types: struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; }


beacon

decompiled_code:
__int64 beacon()
{
int v0; // eax
int v1; // er12
int v2; // eax
socklen_t v3; // edx
int v4; // eax
ssize_t v5; // r15
char *v7; // rax
struct sockaddr *v8; // r15
__int64 v9; // rsi
socklen_t v10; // [rsp+0h] [rbp-F8h] BYREF
__int64 buf; // [rsp+5h] [rbp-F3h] BYREF
_BYTE v12[3]; // [rsp+Dh] [rbp-EBh]
struct sockaddr v13; // [rsp+10h] [rbp-E8h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-D8h] BYREF
struct timeval timeout; // [rsp+30h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-B8h] BYREF

master_addr = 0LL;
*(_WORD *)v12 = 0;
v10 = 0;
buf = 0LL;
v12[2] = 0;
v13 = 0LL;
addr = 0LL;
v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
perror("socket");
fprintf(stderr, "%s: Error creating beacon UDP socket.\n", "beacon");
quit = 2;
pthread_mutex_lock(&master_mutex);
master_addr = -1LL;
pthread_cond_signal(&master_cond);
pthread_mutex_unlock(&master_mutex);
}
else
{
v1 = v0;
v2 = fcntl(v0, 1);
if ( v2 < 0 || fcntl(v1, 2, v2 | 1u) )
fprintf(stderr, "%s: Could not set socket to be closed on exec\n", "beacon");
v13.sa_family = 2;
*(_DWORD *)&v13.sa_data[2] = net_ifbc;
*(_WORD *)v13.sa_data = _bswap_16(net_port);
if ( bind(v1, &v13, v3) < 0 )
{
perror("bind");
fprintf(stderr, "%s: Error binding beacon UDP socket.\n", "beacon");
LABEL_24:
quit = 2;
pthread_mutex_lock(&master_mutex);
master_addr = -1LL;
pthread_cond_signal(&master_cond);
pthread_mutex_unlock(&master_mutex);
}
else
{
LABEL_6:
while ( !quit )
{
while ( 1 )
{
v12[2] = 0;
*(_WORD *)v12 = 0;
buf = 0LL;
v10 = 16;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v1 >> 6] |= 1LL << v1;
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
v4 = select(v1 + 1, &readfds, 0LL, 0LL, &timeout);
if ( !v4 )
break;
if ( v4 == -1 )
goto LABEL_24;
v5 = recvfrom(v1, &buf, 0xBuLL, 0, &addr, &v10);
net_msg_ntoh(&buf);
if ( verbose > 0 )
{
printf("%s: UDP Received %ld bytes.\n", "beacon", v5);
fflush(stdout);
}
if ( v5 < 0 )
{
perror("recvfrom");
fprintf(stderr, "%s: Error reading from beacon UDP socket.\n", "beacon");
goto LABEL_24;
}
if ( v5 != 11 || (_DWORD)buf != 1 || v12[0] != 1 || HIDWORD(buf) != session )
goto LABEL_6;
if ( verbose > 2 )
{
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("%s: Received probe from '%s'.\n", "beacon", v7);
printf("%s: net_msg.version = %u\n", "beacon", (unsigned int)buf);
printf("%s: net_msg.session = %u\n", "beacon", HIDWORD(buf));
printf("%s: net_msg.command = %hhu\n", "beacon", v12[0]);
printf("%s: net_msg.datalen = %hu\n", "beacon", *(unsigned __int16 *)&v12[1]);
if ( verbose > 3 )
{
printf("%s: Packet had %ld bytes: ", "beacon", 11LL);
v8 = (struct sockaddr *)&buf;
do
{
v9 = (unsigned int)SLOBYTE(v8->sa_family);
v8 = (struct sockaddr *)((char *)v8 + 1);
printf("%02hhX ", v9);
}
while ( &v13 != v8 );
putchar(10);
}
fflush(stdout);
}
pthread_mutex_lock(&master_mutex);
if ( !WORD1(master_addr) )
{
master_addr = (__int128)_mm_loadu_si128((const __m128i *)&addr);
WORD1(master_addr) = _bswap_16(net_port);
pthread_cond_signal(&master_cond);
}
pthread_mutex_unlock(&master_mutex);
if ( quit )
goto LABEL_20;
}
}
}
LABEL_20:
close(v1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // eax
int v1; // er12
int v2; // eax
socklen_t v3; // edx
int v4; // eax
ssize_t v5; // r15
char *v7; // rax
struct sockaddr *v8; // r15
__int64 v9; // rsi
socklen_t v10; // [rsp+0h] [rbp-F8h] BYREF
__int64 buf; // [rsp+5h] [rbp-F3h] BYREF
_BYTE v12[3]; // [rsp+Dh] [rbp-EBh]
struct sockaddr v13; // [rsp+10h] [rbp-E8h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-D8h] BYREF
struct timeval timeout; // [rsp+30h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-B8h] BYREF

master_addr = 0LL;
*(_WORD *)v12 = 0;
v10 = 0;
buf = 0LL;
v12[2] = 0;
v13 = 0LL;
addr = 0LL;
v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
perror("socket");
fprintf(stderr, "%s: Error creating beacon UDP socket.\n", "beacon");
quit = 2;
pthread_mutex_lock(&master_mutex);
master_addr = -1LL;
pthread_cond_signal(&master_cond);
pthread_mutex_unlock(&master_mutex);
}
else
{
v1 = v0;
v2 = fcntl(v0, 1);
if ( v2 < 0 || fcntl(v1, 2, v2 | 1u) )
fprintf(stderr, "%s: Could not set socket to be closed on exec\n", "beacon");
v13.sa_family = 2;
*(_DWORD *)&v13.sa_data[2] = net_ifbc;
*(_WORD *)v13.sa_data = func0(net_port);
if ( bind(v1, &v13, v3) < 0 )
{
perror("bind");
fprintf(stderr, "%s: Error binding beacon UDP socket.\n", "beacon");
LABEL_24:
quit = 2;
pthread_mutex_lock(&master_mutex);
master_addr = -1LL;
pthread_cond_signal(&master_cond);
pthread_mutex_unlock(&master_mutex);
}
else
{
LABEL_6:
while ( !quit )
{
while ( 1 )
{
v12[2] = 0;
*(_WORD *)v12 = 0;
buf = 0LL;
v10 = 16;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v1 >> 6] |= 1LL << v1;
timeout.tv_sec = 1LL;
timeout.tv_usec = 0LL;
v4 = select(v1 + 1, &readfds, 0LL, 0LL, &timeout);
if ( !v4 )
break;
if ( v4 == -1 )
goto LABEL_24;
v5 = recvfrom(v1, &buf, 0xBuLL, 0, &addr, &v10);
net_msg_ntoh(&buf);
if ( verbose > 0 )
{
printf("%s: UDP Received %ld bytes.\n", "beacon", v5);
fflush(stdout);
}
if ( v5 < 0 )
{
perror("recvfrom");
fprintf(stderr, "%s: Error reading from beacon UDP socket.\n", "beacon");
goto LABEL_24;
}
if ( v5 != 11 || (_DWORD)buf != 1 || v12[0] != 1 || HIDWORD(buf) != session )
goto LABEL_6;
if ( verbose > 2 )
{
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
printf("%s: Received probe from '%s'.\n", "beacon", v7);
printf("%s: net_msg.version = %u\n", "beacon", (unsigned int)buf);
printf("%s: net_msg.session = %u\n", "beacon", HIDWORD(buf));
printf("%s: net_msg.command = %hhu\n", "beacon", v12[0]);
printf("%s: net_msg.datalen = %hu\n", "beacon", *(unsigned __int16 *)&v12[1]);
if ( verbose > 3 )
{
printf("%s: Packet had %ld bytes: ", "beacon", 11LL);
v8 = (struct sockaddr *)&buf;
do
{
v9 = (unsigned int)SLOBYTE(v8->sa_family);
v8 = (struct sockaddr *)((char *)v8 + 1);
printf("%02hhX ", v9);
}
while ( &v13 != v8 );
putchar(10);
}
fflush(stdout);
}
pthread_mutex_lock(&master_mutex);
if ( !WORD1(master_addr) )
{
master_addr = (__int128)_mm_loadu_si128((const __m128i *)&addr);
WORD1(master_addr) = func0(net_port);
pthread_cond_signal(&master_cond);
}
pthread_mutex_unlock(&master_mutex);
if ( quit )
goto LABEL_20;
}
}
}
LABEL_20:
close(v1);
}
return 0LL;
}

original_code:
void *
beacon(){
 int beacon_fd = -1;
 struct sockaddr_in beacon_addr;
 socklen_t beacon_slen = 0;
 struct sockaddr_in sender_addr;
 synexec_msg_t net_msg;
 ssize_t net_len = 0;
 fd_set fds;
 struct timeval fds_timeout;
 int i;
 memset(&master_addr, 0, sizeof(master_addr));
 memset(&beacon_addr, 0, sizeof(beacon_addr));
 memset(&sender_addr, 0, sizeof(sender_addr));
 memset(&net_msg, 0, sizeof(net_msg));
 if ((beacon_fd = socket(2, SOCK_DGRAM, 0)) < 0){
  perror("socket");
  fprintf(stderr, "%s: Error creating beacon UDP socket.\n", __FUNCTION__);
  goto err;
 }
 if ((i = fcntl(beacon_fd, 1)) < 0 ||
     fcntl(beacon_fd, 2, i | 1)) {
  fprintf(stderr, "%s: Could not set socket to be closed on exec\n", __FUNCTION__);
 }
 beacon_addr.sin_family = 2;
 memcpy(&beacon_addr.sin_addr, &net_ifbc, sizeof(beacon_addr.sin_addr));
 beacon_addr.sin_port = htons(net_port);
 if (bind(beacon_fd, (struct sockaddr *)&beacon_addr, sizeof(struct sockaddr)) < 0){
  perror("bind");
  fprintf(stderr, "%s: Error binding beacon UDP socket.\n", __FUNCTION__);
  goto err;
 }
 while(!quit){
  memset(&net_msg, 0, sizeof(net_msg));
  beacon_slen = sizeof(struct sockaddr);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((beacon_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((beacon_fd) % (8 * (int) sizeof (__fd_mask)))))));
  fds_timeout.tv_sec = 1;
  fds_timeout.tv_usec = 0;
  i = select(beacon_fd+1, &fds, ((void *)0), ((void *)0), &fds_timeout);
  if (i == 0){
   continue;
  }else
  if (i == -1){
   goto err;
  }
  net_len = recvfrom(beacon_fd, &net_msg, sizeof(net_msg), 0, (struct sockaddr *)&sender_addr, &beacon_slen);
  net_msg_ntoh(&net_msg);
  if (verbose > 0){
   printf("%s: UDP Received %ld bytes.\n", __FUNCTION__, (long)net_len);
   fflush(stdout);
  }
  if (net_len < 0){
   perror("recvfrom");
   fprintf(stderr, "%s: Error reading from beacon UDP socket.\n", __FUNCTION__);
   goto err;
  }else
  if (net_len != sizeof(net_msg)){
   continue;
  }else
  if ((net_msg.version != 1) ||
      (net_msg.command != 1) ||
      (net_msg.session != session)){
   continue;
  }
  if (verbose > 2){
   printf("%s: Received probe from '%s'.\n", __FUNCTION__, inet_ntoa(sender_addr.sin_addr));
   printf("%s:  net_msg.version = %u\n", __FUNCTION__, net_msg.version);
   printf("%s:  net_msg.session = %u\n", __FUNCTION__, net_msg.session);
   printf("%s:  net_msg.command = %hhu\n", __FUNCTION__, net_msg.command);
   printf("%s:  net_msg.datalen = %hu\n", __FUNCTION__, net_msg.datalen);
   if (verbose > 3){
    printf("%s: Packet had %ld bytes: ", __FUNCTION__, (long)net_len);
    for (i=0; i<net_len; i++){
     printf("%02hhX ", *(((char *)&net_msg)+i));
    }
    printf("\n");
   }
   fflush(stdout);
  }
  pthread_mutex_lock(&master_mutex);
  if (master_addr.sin_port == 0){
   memcpy(&master_addr, &sender_addr, sizeof(master_addr));
   master_addr.sin_port = htons(net_port);
   pthread_cond_signal(&master_cond);
  }
  pthread_mutex_unlock(&master_mutex);
 }
out:
 if (beacon_fd >= 0){
  close(beacon_fd);
 }
 return ((void *)0);
err:
 quit = 2;
 pthread_mutex_lock(&master_mutex);
 memset(&master_addr, 0xFF, sizeof(master_addr));
 pthread_cond_signal(&master_cond);
 pthread_mutex_unlock(&master_mutex);
 goto out;
}

canonical_original_code:
void *beacon(){
 int beacon_fd = -1;
 struct sockaddr_in beacon_addr;
 unsigned int beacon_slen = 0;
 struct sockaddr_in sender_addr;
 struct synexec_msg_t net_msg;
 long net_len = 0;
 struct fd_set fds;
 struct timeval fds_timeout;
 int i;
 memset(&master_addr, 0, sizeof(master_addr));
 memset(&beacon_addr, 0, sizeof(beacon_addr));
 memset(&sender_addr, 0, sizeof(sender_addr));
 memset(&net_msg, 0, sizeof(net_msg));
 if ((beacon_fd = socket(2, SOCK_DGRAM, 0)) < 0){
  perror("socket");
  fprintf(stderr, "%s: Error creating beacon UDP socket.\n", __FUNCTION__);
  goto err;
 }
 if ((i = fcntl(beacon_fd, 1)) < 0 ||
     fcntl(beacon_fd, 2, i | 1)) {
  fprintf(stderr, "%s: Could not set socket to be closed on exec\n", __FUNCTION__);
 }
 beacon_addr.sin_family = 2;
 memcpy(&beacon_addr.sin_addr, &net_ifbc, sizeof(beacon_addr.sin_addr));
 beacon_addr.sin_port = htons(net_port);
 if (bind(beacon_fd, (struct sockaddr *)&beacon_addr, sizeof(struct sockaddr)) < 0){
  perror("bind");
  fprintf(stderr, "%s: Error binding beacon UDP socket.\n", __FUNCTION__);
  goto err;
 }
 while(!quit){
  memset(&net_msg, 0, sizeof(net_msg));
  beacon_slen = sizeof(struct sockaddr);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&fds)->__fds_bits)[((beacon_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((beacon_fd) % (8 * (int) sizeof (__fd_mask)))))));
  fds_timeout.tv_sec = 1;
  fds_timeout.tv_usec = 0;
  i = select(beacon_fd+1, &fds, ((void *)0), ((void *)0), &fds_timeout);
  if (i == 0){
   continue;
  }else
  if (i == -1){
   goto err;
  }
  net_len = recvfrom(beacon_fd, &net_msg, sizeof(net_msg), 0, (struct sockaddr *)&sender_addr, &beacon_slen);
  net_msg_ntoh(&net_msg);
  if (verbose > 0){
   printf("%s: UDP Received %ld bytes.\n", __FUNCTION__, (long)net_len);
   fflush(stdout);
  }
  if (net_len < 0){
   perror("recvfrom");
   fprintf(stderr, "%s: Error reading from beacon UDP socket.\n", __FUNCTION__);
   goto err;
  }else
  if (net_len != sizeof(net_msg)){
   continue;
  }else
  if ((net_msg.version != 1) ||
      (net_msg.command != 1) ||
      (net_msg.session != session)){
   continue;
  }
  if (verbose > 2){
   printf("%s: Received probe from '%s'.\n", __FUNCTION__, inet_ntoa(sender_addr.sin_addr));
   printf("%s:  net_msg.version = %u\n", __FUNCTION__, net_msg.version);
   printf("%s:  net_msg.session = %u\n", __FUNCTION__, net_msg.session);
   printf("%s:  net_msg.command = %hhu\n", __FUNCTION__, net_msg.command);
   printf("%s:  net_msg.datalen = %hu\n", __FUNCTION__, net_msg.datalen);
   if (verbose > 3){
    printf("%s: Packet had %ld bytes: ", __FUNCTION__, (long)net_len);
    for (i=0; i<net_len; i++){
     printf("%02hhX ", *(((char *)&net_msg)+i));
    }
    printf("\n");
   }
   fflush(stdout);
  }
  pthread_mutex_lock(&master_mutex);
  if (master_addr.sin_port == 0){
   memcpy(&master_addr, &sender_addr, sizeof(master_addr));
   master_addr.sin_port = htons(net_port);
   pthread_cond_signal(&master_cond);
  }
  pthread_mutex_unlock(&master_mutex);
 }
out:
 if (beacon_fd >= 0){
  close(beacon_fd);
 }
 return ((void *)0);
err:
 quit = 2;
 pthread_mutex_lock(&master_mutex);
 memset(&master_addr, 0xFF, sizeof(master_addr));
 pthread_cond_signal(&master_cond);
 pthread_mutex_unlock(&master_mutex);
 goto out;
}

variable_types: struct synexec_msg_t { unsigned int version; unsigned int session; char command; unsigned short datalen; }

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

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

variable_types: i int

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


repo: franciozzy/udpgen

decompiled_code:
void timer_disable()
{
ignore_sigalrm = 1;
}

canonical_decompiled_code:
void func2()
{
ignore_sigalrm = 1;
}

return_type: void


decompiled_code:
void timer_reset()
{
if ( setitimer(ITIMER_REAL, &itv, 0LL) )
perror("setitimer");
}

canonical_decompiled_code:
void func3()
{
if ( setitimer(ITIMER_REAL, &itv, 0LL) )
perror("setitimer");
}

original_code:
void timer_reset() {
    if (setitimer(ITIMER_REAL, &itv, ((void *)0)))
        perror("setitimer");
}


decompiled_code:
__time_t timer_init()
{
__time_t result; // rax

result = toler_sec;
itv.it_value.tv_usec = 0LL;
itv.it_interval.tv_usec = 0LL;
itv.it_value.tv_sec = result;
itv.it_interval.tv_sec = result;
return result;
}

canonical_decompiled_code:
__time_t func4()
{
__time_t result; // rax

result = toler_sec;
itv.it_value.tv_usec = 0LL;
itv.it_interval.tv_usec = 0LL;
itv.it_value.tv_sec = result;
itv.it_interval.tv_sec = result;
return result;
}

original_code:
void timer_init() {
    struct timeval tv;
    tv.tv_sec = toler_sec;
    tv.tv_usec = 0;
    itv.it_value = tv;
    itv.it_interval = tv;
}

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


dump

decompiled_code:
int dump()
{
__int64 v0; // rax
int v1; // er13
__int64 v2; // r12
__int64 v3; // rdx
__int64 v4; // rbx
__int64 v5; // r14
__int64 v6; // rbp
__int64 v7; // rax
double v8; // xmm1_8
double v9; // xmm2_8
float v10; // xmm0_4
float v11; // xmm1_4
double v12; // xmm0_8
double v13; // xmm1_8

v0 = get_hz();
v1 = npkts;
v2 = v0;
timer_disable();
if ( npkts <= 0 )
goto LABEL_16;
v3 = udpdata;
v4 = 0LL;
v5 = -1LL;
do
{
v6 = 20 * v4;
if ( !suppress_dump )
{
printf(
"%5u %lu %lu (%lu) %llu\n",
*(unsigned int *)(v3 + 20 * v4),
*(_QWORD *)(v3 + v6 + 4),
*(_QWORD *)(v3 + v6 + 12),
*(_QWORD *)(v3 + v6 + 12) - *(_QWORD *)(v3 + v6 + 4),
1000000LL * (*(_QWORD *)(v3 + v6 + 12) - *(_QWORD *)(v3 + v6 + 4)) / (unsigned __int64)v2);
v3 = udpdata;
}
if ( *(_QWORD *)(v3 + v6 + 4) )
{
--v1;
v5 = (int)v4;
}
++v4;
}
while ( npkts > (int)v4 );
if ( (_DWORD)v5 == -1 )
{
LABEL_16:
v12 = 0.0;
v13 = 0.0;
goto LABEL_13;
}
v7 = *(_QWORD *)(v3 + 20 * v5 + 12) - *(_QWORD *)(v3 + 4);
if ( v7 < 0 )
{
v8 = (double)(int)(v7 & 1 | ((unsigned __int64)v7 >> 1)) + (double)(int)(v7 & 1 | ((unsigned __int64)v7 >> 1));
if ( v2 >= 0 )
goto LABEL_11;
LABEL_15:
v9 = (double)(int)(v2 & 1 | ((unsigned __int64)v2 >> 1)) + (double)(int)(v2 & 1 | ((unsigned __int64)v2 >> 1));
goto LABEL_12;
}
v8 = (double)(int)v7;
if ( v2 < 0 )
goto LABEL_15;
LABEL_11:
v9 = (double)(int)v2;
LABEL_12:
v10 = (double)(bufsz * (npkts - v1)) / (v8 / v9) / 1000000.0;
v11 = 8.0 * v10;
v12 = v10;
v13 = (float)(v11 / 1000.0);
LABEL_13:
fprintf(stderr, "Average throughput: %.0f MB/s = %.2f Gbit/s\n", v12, v13);
return fprintf(
stderr,
"Dropped packets: %d of %d (%.1f%%)\n",
(unsigned int)v1,
(unsigned int)npkts,
(double)v1 * 100.0 / (double)npkts);
}

original_code:
void dump(void){
    int i;
    unsigned long long hz = get_hz();
    int last_recv_pkt = -1;
    int total_pkts_dropped = npkts;
    float tput_mbs;
    timer_disable();
    for (i=0; i<npkts; i++){
        if (!suppress_dump)
            printf("%5u %" "l" "u" " %" "l" "u" " (%" "l" "u" ") %llu\n",
                   udpdata[i].seq, udpdata[i].tsctx,
                   udpdata[i].tscrx, udpdata[i].tscrx-udpdata[i].tsctx,
                   (udpdata[i].tscrx-udpdata[i].tsctx)*1000000/hz);
        if(udpdata[i].tsctx != 0) {
            last_recv_pkt = i;
            --total_pkts_dropped;
        }
    }
    if (last_recv_pkt >= 0) {
        tput_mbs = (long long)(npkts - total_pkts_dropped) * bufsz /
            ((udpdata[last_recv_pkt].tscrx - udpdata[0].tsctx)*1.0/hz) /
            1000000.0;
    } else {
        tput_mbs = 0.0;
    }
    fprintf(stderr, "Average throughput: %.0f MB/s = %.2f Gbit/s\n",
            tput_mbs, tput_mbs * 8 / 1000);
    fprintf(stderr, "Dropped packets: %d of %d (%.1f%%)\n",
            total_pkts_dropped, npkts, total_pkts_dropped*100.0 / npkts);
}

canonical_original_code:
void dump(){
    int i;
    unsigned long long hz = get_hz();
    int last_recv_pkt = -1;
    int total_pkts_dropped = npkts;
    float tput_mbs;
    timer_disable();
    for (i=0; i<npkts; i++){
        if (!suppress_dump)
            printf("%5u %" "l" "u" " %" "l" "u" " (%" "l" "u" ") %llu\n",
                   udpdata[i].seq, udpdata[i].tsctx,
                   udpdata[i].tscrx, udpdata[i].tscrx-udpdata[i].tsctx,
                   (udpdata[i].tscrx-udpdata[i].tsctx)*1000000/hz);
        if(udpdata[i].tsctx != 0) {
            last_recv_pkt = i;
            --total_pkts_dropped;
        }
    }
    if (last_recv_pkt >= 0) {
        tput_mbs = (long long)(npkts - total_pkts_dropped) * bufsz /
            ((udpdata[last_recv_pkt].tscrx - udpdata[0].tsctx)*1.0/hz) /
            1000000.0;
    } else {
        tput_mbs = 0.0;
    }
    fprintf(stderr, "Average throughput: %.0f MB/s = %.2f Gbit/s\n",
            tput_mbs, tput_mbs * 8 / 1000);
    fprintf(stderr, "Dropped packets: %d of %d (%.1f%%)\n",
            total_pkts_dropped, npkts, total_pkts_dropped*100.0 / npkts);
}

variable_types:
hz
unsigned long long


decompiled_code:
void __noreturn sigterm_h()
{
dump();
exit(0);
}

canonical_decompiled_code:
void __noreturn func6()
{
func5();
exit(0);
}

original_code:
void sigterm_h(int signal){
   dump();
   exit(0);
}

variable_types: signal int


decompiled_code:
void sigalrm_h()
{
if ( !ignore_sigalrm )
{
fprintf(stderr, "Warning: Received no further packets in %u seconds\n", (unsigned int)toler_sec);
dump();
exit(0);
}
}

canonical_decompiled_code:
void func7()
{
if ( !ignore_sigalrm )
{
fprintf(stderr, "Warning: Received no further packets in %u seconds\n", (unsigned int)toler_sec);
func5();
exit(0);
}
}

original_code:
void sigalrm_h(int signal){
    if (!ignore_sigalrm) {
        fprintf(stderr, "Warning: Received no further packets in %u seconds\n", toler_sec);
        dump();
        exit(0);
    }
}


usage

decompiled_code:
size_t usage()
{
return fwrite("RTFSC\n", 1uLL, 6uLL, stderr);
}

canonical_decompiled_code:
size_t func1()
{
return fwrite("RTFSC\n", 1uLL, 6uLL, stderr);
}

original_code:
void usage(char *argv0){
    fprintf(stderr, "RTFSC\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
__m128i *v6; // r14
int v7; // eax
int v8; // ebp
int v9; // edi
socklen_t v10; // edx
__int64 v11; // r13
__int64 v12; // rax
__m128i *v13; // rdx
__int64 v14; // rbx
int v15; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

addr_len = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "n:s:t:q");
if ( v3 == -1 )
break;
if ( v3 == 115 )
{
bufsz = atoi(optarg);
if ( (unsigned int)bufsz <= 0x13 )
{
v4 = 1;
fprintf(stderr, "Payload size must be at least %zd bytes\n", 0x14uLL);
return v4;
}
}
else if ( v3 > 115 )
{
if ( v3 != 116 )
{
LABEL_12:
v4 = 1;
usage();
return v4;
}
toler_sec = atoi(optarg);
}
else if ( v3 == 110 )
{
npkts = atoi(optarg);
}
else
{
if ( v3 != 113 )
goto LABEL_12;
suppress_dump = 1;
}
}
timer_init();
udpdata = (__int64)calloc(npkts, 0x14uLL);
if ( udpdata )
{
v6 = (__m128i *)malloc(bufsz);
if ( v6 )
{
signal(2, (__sighandler_t)sigterm_h);
signal(14, (__sighandler_t)sigalrm_h);
v7 = socket(2, 2, 0);
v8 = v7;
if ( v7 < 0 )
{
v4 = 1;
perror("socket");
fwrite("Error creating temporary socket.\n", 1uLL, 0x21uLL, stderr);
}
else
{
v9 = v7;
addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16_constprop_0((unsigned int)v7, &addr, 16LL);
if ( bind(v9, &addr, v10) < 0 )
{
v4 = 1;
perror("bind");
}
else
{
v11 = 0LL;
v4 = 0;
if ( npkts > 0 )
{
while ( 1 )
{
addr_len = 16;
v15 = recvfrom(v8, v6, bufsz, 0, &addr, &addr_len);
v4 = v15;
if ( v15 < 0 )
{
v4 = 1;
perror("recvfrom");
goto LABEL_25;
}
if ( bufsz != v15 )
break;
v12 = 5 * v11++;
v12 *= 4LL;
v13 = (__m128i *)(v12 + udpdata);
*v13 = _mm_loadu_si128(v6);
v14 = udpdata + v12;
v13[1].m128i_i32[0] = v6[1].m128i_i32[0];
*(_QWORD *)(v14 + 12) = rdtsc();
timer_reset();
if ( npkts <= (int)v11 )
goto LABEL_28;
}
v4 = 1;
fwrite("Received unknown packet.\n", 1uLL, 0x19uLL, stderr);
}
else
{
LABEL_28:
dump();
}
}
LABEL_25:
close(v8);
}
free(v6);
}
else
{
v4 = 1;
perror("malloc");
}
}
else
{
v4 = 1;
perror("calloc");
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
__m128i *v6; // r14
int v7; // eax
int v8; // ebp
int v9; // edi
socklen_t v10; // edx
__int64 v11; // r13
__int64 v12; // rax
__m128i *v13; // rdx
__int64 v14; // rbx
int v15; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

addr_len = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "n:s:t:q");
if ( v3 == -1 )
break;
if ( v3 == 115 )
{
bufsz = atoi(optarg);
if ( (unsigned int)bufsz <= 0x13 )
{
v4 = 1;
fprintf(stderr, "Payload size must be at least %zd bytes\n", 0x14uLL);
return v4;
}
}
else if ( v3 > 115 )
{
if ( v3 != 116 )
{
LABEL_12:
v4 = 1;
func1();
return v4;
}
toler_sec = atoi(optarg);
}
else if ( v3 == 110 )
{
npkts = atoi(optarg);
}
else
{
if ( v3 != 113 )
goto LABEL_12;
suppress_dump = 1;
}
}
func4();
udpdata = (__int64)calloc(npkts, 0x14uLL);
if ( udpdata )
{
v6 = (__m128i *)malloc(bufsz);
if ( v6 )
{
signal(2, (__sighandler_t)sigterm_h);
signal(14, (__sighandler_t)sigalrm_h);
v7 = socket(2, 2, 0);
v8 = v7;
if ( v7 < 0 )
{
v4 = 1;
perror("socket");
fwrite("Error creating temporary socket.\n", 1uLL, 0x21uLL, stderr);
}
else
{
v9 = v7;
addr = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0((unsigned int)v7, &addr, 16LL);
if ( bind(v9, &addr, v10) < 0 )
{
v4 = 1;
perror("bind");
}
else
{
v11 = 0LL;
v4 = 0;
if ( npkts > 0 )
{
while ( 1 )
{
addr_len = 16;
v15 = recvfrom(v8, v6, bufsz, 0, &addr, &addr_len);
v4 = v15;
if ( v15 < 0 )
{
v4 = 1;
perror("recvfrom");
goto LABEL_25;
}
if ( bufsz != v15 )
break;
v12 = 5 * v11++;
v12 *= 4LL;
v13 = (__m128i *)(v12 + udpdata);
*v13 = _mm_loadu_si128(v6);
v14 = udpdata + v12;
v13[1].m128i_i32[0] = v6[1].m128i_i32[0];
*(_QWORD *)(v14 + 12) = rdtsc();
func3();
if ( npkts <= (int)v11 )
goto LABEL_28;
}
v4 = 1;
fwrite("Received unknown packet.\n", 1uLL, 0x19uLL, stderr);
}
else
{
LABEL_28:
func5();
}
}
LABEL_25:
close(v8);
}
free(v6);
}
else
{
v4 = 1;
perror("malloc");
}
}
else
{
v4 = 1;
perror("calloc");
}
return v4;
}

original_code:
int main(int argc, char **argv){
    struct sockaddr_in our_addr;
    void *buf = ((void *)0);
    int sock = -1;
    unsigned int slen = 0;
    int err = 0;
    int i;
    while ((i = getopt(argc, argv, "n:s:t:q")) != -1){
        switch(i){
        case 'n':
            npkts = atoi(optarg);
            break;
        case 's':
            bufsz = atoi(optarg);
            if (bufsz < sizeof(udpdata_t)){
                fprintf(stderr, "Payload size must be at least %zd bytes\n",
                        sizeof(udpdata_t));
                goto err;
            }
            break;
 case 't':
     toler_sec = atoi(optarg);
     break;
 case 'q':
     suppress_dump = 1;
     break;
        default:
            usage(argv[0]);
            goto err;
        }
    }
    timer_init();
    if ((udpdata = (udpdata_t *)calloc(npkts, sizeof(*udpdata))) == ((void *)0))
    {
        perror("calloc");
        goto err;
    }
    if ((buf = malloc(bufsz)) == ((void *)0)){
        perror("malloc");
        goto err;
    }
    signal(2, sigterm_h);
    signal(14, sigalrm_h);
    if ((sock = socket(2, SOCK_DGRAM, 0)) < 0){
        perror("socket");
        fprintf(stderr, "Error creating temporary socket.\n");
        goto err;
    }
    memset(&our_addr, 0, sizeof(our_addr));
    our_addr.sin_family = 2;
    our_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    our_addr.sin_port = htons(41300);
    if (bind(sock, (struct sockaddr *)&our_addr, sizeof(struct sockaddr)) < 0){
        perror("bind");
        goto err;
    }
    for (i=0; i<npkts; i++){
        slen = sizeof(struct sockaddr);
        err = recvfrom(sock, buf, bufsz, 0, (struct sockaddr *)&our_addr, &slen);
        if (err < 0){
            perror("recvfrom");
            goto err;
        } else
        if (err != bufsz){
            fprintf(stderr, "Received unknown packet.\n");
            goto err;
        }
        memcpy(&udpdata[i], buf, sizeof(udpdata_t));
        udpdata[i].tscrx = rdtsc();
        timer_reset();
    }
    dump();
out:
    if (sock >= 0)
        close(sock);
    if (buf)
        free(buf);
    return(err);
err:
    err = 1;
    goto out;
}

canonical_original_code:
int main(int argc, char **argv){
    struct sockaddr_in our_addr;
    void *buf = ((void *)0);
    int sock = -1;
    unsigned int slen = 0;
    int err = 0;
    int i;
    while ((i = getopt(argc, argv, "n:s:t:q")) != -1){
        switch(i){
        case 'n':
            npkts = atoi(optarg);
            break;
        case 's':
            bufsz = atoi(optarg);
            if (bufsz < sizeof(udpdata_t)){
                fprintf(stderr, "Payload size must be at least %zd bytes\n",
                        sizeof(udpdata_t));
                goto err;
            }
            break;
 case 't':
     toler_sec = atoi(optarg);
     break;
 case 'q':
     suppress_dump = 1;
     break;
        default:
            usage(argv[0]);
            goto err;
        }
    }
    timer_init();
    if ((udpdata = (struct udpdata_t *)calloc(npkts, sizeof(*udpdata))) == ((void *)0))
    {
        perror("calloc");
        goto err;
    }
    if ((buf = malloc(bufsz)) == ((void *)0)){
        perror("malloc");
        goto err;
    }
    signal(2, sigterm_h);
    signal(14, sigalrm_h);
    if ((sock = socket(2, SOCK_DGRAM, 0)) < 0){
        perror("socket");
        fprintf(stderr, "Error creating temporary socket.\n");
        goto err;
    }
    memset(&our_addr, 0, sizeof(our_addr));
    our_addr.sin_family = 2;
    our_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
    our_addr.sin_port = htons(41300);
    if (bind(sock, (struct sockaddr *)&our_addr, sizeof(struct sockaddr)) < 0){
        perror("bind");
        goto err;
    }
    for (i=0; i<npkts; i++){
        slen = sizeof(struct sockaddr);
        err = recvfrom(sock, buf, bufsz, 0, (struct sockaddr *)&our_addr, &slen);
        if (err < 0){
            perror("recvfrom");
            goto err;
        } else
        if (err != bufsz){
            fprintf(stderr, "Received unknown packet.\n");
            goto err;
        }
        memcpy(&udpdata[i], buf, sizeof(udpdata_t));
        udpdata[i].tscrx = rdtsc();
        timer_reset();
    }
    dump();
out:
    if (sock >= 0)
        close(sock);
    if (buf)
        free(buf);
    return(err);
err:
    err = 1;
    goto out;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
buf
void *

variable_types:
slen
unsigned int

user_defined_types: struct udpdata_t { unsigned int seq; unsigned long tsctx; unsigned long tscrx; }


repo: frank-liu/CapPkts

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // ax
int v4; // eax
int v5; // ebp
int v6; // eax
const char *v7; // rdi
__int64 v8; // rsi
unsigned __int16 v9; // ax
__int64 v10; // [rsp+0h] [rbp-908h]
__int64 v11; // [rsp+18h] [rbp-8F0h] BYREF
__int16 optval; // [rsp+20h] [rbp-8E8h] BYREF
__int64 *v13; // [rsp+28h] [rbp-8E0h]
__int64 v14[2]; // [rsp+30h] [rbp-8D8h] BYREF
__int16 v15; // [rsp+40h] [rbp-8C8h]
struct sigaction v16; // [rsp+60h] [rbp-8A8h] BYREF
char buf[6]; // [rsp+100h] [rbp-808h] BYREF
unsigned __int8 v18; // [rsp+106h] [rbp-802h]
unsigned __int8 v19; // [rsp+107h] [rbp-801h]
unsigned __int8 v20; // [rsp+108h] [rbp-800h]
unsigned __int8 v21; // [rsp+109h] [rbp-7FFh]
unsigned __int8 v22; // [rsp+10Ah] [rbp-7FEh]
unsigned __int8 v23; // [rsp+10Bh] [rbp-7FDh]
unsigned __int16 v24; // [rsp+10Ch] [rbp-7FCh]
unsigned __int8 v25; // [rsp+10Eh] [rbp-7FAh]
__int16 v26; // [rsp+110h] [rbp-7F8h]
unsigned __int8 v27; // [rsp+117h] [rbp-7F1h]
unsigned __int8 v28; // [rsp+11Ah] [rbp-7EEh]
unsigned __int8 v29; // [rsp+11Bh] [rbp-7EDh]
unsigned __int8 v30; // [rsp+11Ch] [rbp-7ECh]
unsigned __int8 v31; // [rsp+11Dh] [rbp-7EBh]
unsigned __int8 v32; // [rsp+11Eh] [rbp-7EAh]
unsigned __int8 v33; // [rsp+11Fh] [rbp-7E9h]
unsigned __int8 v34; // [rsp+120h] [rbp-7E8h]
unsigned __int8 v35; // [rsp+121h] [rbp-7E7h]
__int16 v36; // [rsp+122h] [rbp-7E6h]
__int16 v37; // [rsp+124h] [rbp-7E4h]

puts("Start to capture packets...");
v11 = 0x6000000006LL;
optval = 1;
v13 = &v11;
v16.sa_flags = 0;
v16.sa_handler = (__sighandler_t)sig_handler;
sigemptyset(&v16.sa_mask);
sigaction(15, &v16, 0LL);
sigaction(2, &v16, 0LL);
sigaction(3, &v16, 0LL);
v3 = _bswap_16(2048LL, 3LL);
v4 = socket(17, 3, v3);
sock = v4;
if ( v4 != -1 )
{
v5 = v4;
v14[0] = 828929125LL;
v14[1] = 0LL;
if ( ioctl(v4, 0x8913uLL, v14) == -1 || (v15 |= 0x100u, ioctl(v5, 0x8914uLL, v14) == -1) )
{
v7 = "ioctl";
}
else
{
if ( setsockopt(v5, 1, 26, &optval, 0x10u) != -1 )
{
while ( 1 )
{
while ( 1 )
{
v6 = recvfrom(sock, buf, 0x800uLL, 0, 0LL, 0LL);
if ( v6 > 41 )
break;
puts("invalid packet");
}
printf("%d bytes recieved\n", (unsigned int)v6);
printf(
"Ethernet: MAC[%02X:%02X:%02X:%02X:%02X:%02X]",
(unsigned __int8)buf[0],
(unsigned __int8)buf[1],
(unsigned __int8)buf[2],
(unsigned __int8)buf[3],
(unsigned __int8)buf[4],
(unsigned __int8)buf[5]);
v8 = v18;
LODWORD(v10) = v23;
printf("->[%02X:%02X:%02X:%02X:%02X:%02X]", v18, v19, v20, v21, v22, v10);
v9 = _bswap_16(v24, v8);
printf(" type[%04x]\n", v9);
printf("IP: Version: %d HeaderLen: %d[%d]", v25 >> 4, v25 & 0xF, 4 * (v25 & 0xFu));
printf(" TotalLen %d", (unsigned __int16)__ROL2__(v26, 8));
printf(" IP [%d.%d.%d.%d]", v28, v29, v30, v31);
printf("->[%d.%d.%d.%d]", v32, v33, v34, v35);
printf(" %d", v27);
switch ( v27 )
{
case 6u:
printf("[TCP]");
break;
case 0x11u:
printf("[UDP]");
break;
case 1u:
printf("[ICMP]");
break;
case 2u:
printf("[IGMP]");
break;
default:
printf("[OTHERS]");
break;
}
printf(" PORT [%d]->[%d]\n", (unsigned __int16)__ROL2__(v36, 8), (unsigned __int16)__ROL2__(v37, 8));
}
}
v7 = "setsockopt";
}
perror(v7);
close(sock);
exit(1);
}
perror("socket");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // ax
int v4; // eax
int v5; // ebp
int v6; // eax
const char *v7; // rdi
__int64 v8; // rsi
unsigned __int16 v9; // ax
__int64 v10; // [rsp+0h] [rbp-908h]
__int64 v11; // [rsp+18h] [rbp-8F0h] BYREF
__int16 optval; // [rsp+20h] [rbp-8E8h] BYREF
__int64 *v13; // [rsp+28h] [rbp-8E0h]
__int64 v14[2]; // [rsp+30h] [rbp-8D8h] BYREF
__int16 v15; // [rsp+40h] [rbp-8C8h]
struct sigaction v16; // [rsp+60h] [rbp-8A8h] BYREF
char buf[6]; // [rsp+100h] [rbp-808h] BYREF
unsigned __int8 v18; // [rsp+106h] [rbp-802h]
unsigned __int8 v19; // [rsp+107h] [rbp-801h]
unsigned __int8 v20; // [rsp+108h] [rbp-800h]
unsigned __int8 v21; // [rsp+109h] [rbp-7FFh]
unsigned __int8 v22; // [rsp+10Ah] [rbp-7FEh]
unsigned __int8 v23; // [rsp+10Bh] [rbp-7FDh]
unsigned __int16 v24; // [rsp+10Ch] [rbp-7FCh]
unsigned __int8 v25; // [rsp+10Eh] [rbp-7FAh]
__int16 v26; // [rsp+110h] [rbp-7F8h]
unsigned __int8 v27; // [rsp+117h] [rbp-7F1h]
unsigned __int8 v28; // [rsp+11Ah] [rbp-7EEh]
unsigned __int8 v29; // [rsp+11Bh] [rbp-7EDh]
unsigned __int8 v30; // [rsp+11Ch] [rbp-7ECh]
unsigned __int8 v31; // [rsp+11Dh] [rbp-7EBh]
unsigned __int8 v32; // [rsp+11Eh] [rbp-7EAh]
unsigned __int8 v33; // [rsp+11Fh] [rbp-7E9h]
unsigned __int8 v34; // [rsp+120h] [rbp-7E8h]
unsigned __int8 v35; // [rsp+121h] [rbp-7E7h]
__int16 v36; // [rsp+122h] [rbp-7E6h]
__int16 v37; // [rsp+124h] [rbp-7E4h]

puts("Start to capture packets...");
v11 = 0x6000000006LL;
optval = 1;
v13 = &v11;
v16.sa_flags = 0;
v16.sa_handler = (__sighandler_t)sig_handler;
sigemptyset(&v16.sa_mask);
sigaction(15, &v16, 0LL);
sigaction(2, &v16, 0LL);
sigaction(3, &v16, 0LL);
v3 = func8(2048LL, 3LL);
v4 = socket(17, 3, v3);
sock = v4;
if ( v4 != -1 )
{
v5 = v4;
v14[0] = 828929125LL;
v14[1] = 0LL;
if ( ioctl(v4, 0x8913uLL, v14) == -1 || (v15 |= 0x100u, ioctl(v5, 0x8914uLL, v14) == -1) )
{
v7 = "ioctl";
}
else
{
if ( setsockopt(v5, 1, 26, &optval, 0x10u) != -1 )
{
while ( 1 )
{
while ( 1 )
{
v6 = recvfrom(sock, buf, 0x800uLL, 0, 0LL, 0LL);
if ( v6 > 41 )
break;
puts("invalid packet");
}
printf("%d bytes recieved\n", (unsigned int)v6);
printf(
"Ethernet: MAC[%02X:%02X:%02X:%02X:%02X:%02X]",
(unsigned __int8)buf[0],
(unsigned __int8)buf[1],
(unsigned __int8)buf[2],
(unsigned __int8)buf[3],
(unsigned __int8)buf[4],
(unsigned __int8)buf[5]);
v8 = v18;
LODWORD(v10) = v23;
printf("->[%02X:%02X:%02X:%02X:%02X:%02X]", v18, v19, v20, v21, v22, v10);
v9 = func8(v24, v8);
printf(" type[%04x]\n", v9);
printf("IP: Version: %d HeaderLen: %d[%d]", v25 >> 4, v25 & 0xF, 4 * (v25 & 0xFu));
printf(" TotalLen %d", (unsigned __int16)__ROL2__(v26, 8));
printf(" IP [%d.%d.%d.%d]", v28, v29, v30, v31);
printf("->[%d.%d.%d.%d]", v32, v33, v34, v35);
printf(" %d", v27);
switch ( v27 )
{
case 6u:
printf("[TCP]");
break;
case 0x11u:
printf("[UDP]");
break;
case 1u:
printf("[ICMP]");
break;
case 2u:
printf("[IGMP]");
break;
default:
printf("[OTHERS]");
break;
}
printf(" PORT [%d]->[%d]\n", (unsigned __int16)__ROL2__(v36, 8), (unsigned __int16)__ROL2__(v37, 8));
}
}
v7 = "setsockopt";
}
perror(v7);
close(sock);
exit(1);
}
perror("socket");
exit(1);
}

original_code:
int main(int argc, char ** argv)
{
 int n;
 char buf[2048];
 unsigned char *ethhead;
 unsigned char *iphead;
 struct ifreq ethreq;
 struct sigaction sighandle;
 printf("Start to capture packets...\n");
 struct sock_filter bpf_code[] =
 {
   { 0x6, 0, 0, 0x00000060 }
 };
 struct sock_fprog filter;
 filter.len = sizeof(bpf_code) / sizeof(bpf_code[0]);
 filter.filter = bpf_code;
 sighandle.sa_flags = 0;
 sighandle.__sigaction_handler.sa_handler = sig_handler;
 sigemptyset(&sighandle.sa_mask);
 sigaction(15, &sighandle, ((void *)0));
 sigaction(2, &sighandle, ((void *)0));
 sigaction(3, &sighandle, ((void *)0));
 if ((sock = socket(17, SOCK_RAW, htons(0x0800))) == -1)
 {
  perror("socket");
  exit(1);
 }
 strncpy(ethreq.ifr_ifrn.ifrn_name, "eth1", 16);
 if (ioctl(sock, 0x8913, &ethreq) == -1)
 {
  perror("ioctl");
  close(sock);
  exit(1);
 }
 ethreq.ifr_ifru.ifru_flags |= IFF_PROMISC;
 if (ioctl(sock, 0x8914, &ethreq) == -1)
 {
  perror("ioctl");
  close(sock);
  exit(1);
 }
 if (setsockopt(sock, 1, 26, &filter, sizeof(filter))
   == -1)
 {
  perror("setsockopt");
  close(sock);
  exit(1);
 }
 while (1)
 {
  n = recvfrom(sock, buf, sizeof(buf), 0, ((void *)0), ((void *)0));
  if (n < (14 + 20 + 8))
  {
   printf("invalid packet\n");
   continue;
  }
  printf("%d bytes recieved\n", n);
  ethhead = buf;
  printf("Ethernet: MAC[%02X:%02X:%02X:%02X:%02X:%02X]", ethhead[0],
    ethhead[1], ethhead[2], ethhead[3], ethhead[4], ethhead[5]);
  printf("->[%02X:%02X:%02X:%02X:%02X:%02X]", ethhead[6], ethhead[7],
    ethhead[8], ethhead[9], ethhead[10], ethhead[11]);
  printf(" type[%04x]\n", (ntohs(ethhead[12] | ethhead[13] << 8)));
  iphead = ethhead + 14;
  printf("IP: Version: %d HeaderLen: %d[%d]", (*iphead >> 4),
    (*iphead & 0x0f), (*iphead & 0x0f) * 4);
  printf(" TotalLen %d", (iphead[2] << 8 | iphead[3]));
  printf(" IP [%d.%d.%d.%d]", iphead[12], iphead[13], iphead[14],
    iphead[15]);
  printf("->[%d.%d.%d.%d]", iphead[16], iphead[17], iphead[18],
    iphead[19]);
  printf(" %d", iphead[9]);
  if (iphead[9] == IPPROTO_TCP)
   printf("[TCP]");
  else if (iphead[9] == IPPROTO_UDP)
   printf("[UDP]");
  else if (iphead[9] == IPPROTO_ICMP)
   printf("[ICMP]");
  else if (iphead[9] == IPPROTO_IGMP)
   printf("[IGMP]");
  else if (iphead[9] == IPPROTO_IGMP)
   printf("[IGMP]");
  else
   printf("[OTHERS]");
  printf(" PORT [%d]->[%d]\n", (iphead[20] << 8 | iphead[21]),
    (iphead[22] << 8 | iphead[23]));
 }
 close(sock);
 printf("Stop capturing packets!\n");
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int n;
 char buf[2048];
 unsigned char *ethhead;
 unsigned char *iphead;
 struct ifreq ethreq;
 struct sigaction sighandle;
 printf("Start to capture packets...\n");
 struct sock_filter bpf_code[] = {
   { 0x6, 0, 0, 0x00000060 }
 };
 struct sock_fprog filter;
 filter.len = sizeof(bpf_code) / sizeof(bpf_code[0]);
 filter.filter = bpf_code;
 sighandle.sa_flags = 0;
 sighandle.__sigaction_handler.sa_handler = sig_handler;
 sigemptyset(&sighandle.sa_mask);
 sigaction(15, &sighandle, ((void *)0));
 sigaction(2, &sighandle, ((void *)0));
 sigaction(3, &sighandle, ((void *)0));
 if ((sock = socket(17, SOCK_RAW, htons(0x0800))) == -1)
 {
  perror("socket");
  exit(1);
 }
 strncpy(ethreq.ifr_ifrn.ifrn_name, "eth1", 16);
 if (ioctl(sock, 0x8913, &ethreq) == -1)
 {
  perror("ioctl");
  close(sock);
  exit(1);
 }
 ethreq.ifr_ifru.ifru_flags |= IFF_PROMISC;
 if (ioctl(sock, 0x8914, &ethreq) == -1)
 {
  perror("ioctl");
  close(sock);
  exit(1);
 }
 if (setsockopt(sock, 1, 26, &filter, sizeof(filter))
   == -1)
 {
  perror("setsockopt");
  close(sock);
  exit(1);
 }
 while (1)
 {
  n = recvfrom(sock, buf, sizeof(buf), 0, ((void *)0), ((void *)0));
  if (n < (14 + 20 + 8))
  {
   printf("invalid packet\n");
   continue;
  }
  printf("%d bytes recieved\n", n);
  ethhead = buf;
  printf("Ethernet: MAC[%02X:%02X:%02X:%02X:%02X:%02X]", ethhead[0],
    ethhead[1], ethhead[2], ethhead[3], ethhead[4], ethhead[5]);
  printf("->[%02X:%02X:%02X:%02X:%02X:%02X]", ethhead[6], ethhead[7],
    ethhead[8], ethhead[9], ethhead[10], ethhead[11]);
  printf(" type[%04x]\n", (ntohs(ethhead[12] | ethhead[13] << 8)));
  iphead = ethhead + 14;
  printf("IP: Version: %d HeaderLen: %d[%d]", (*iphead >> 4),
    (*iphead & 0x0f), (*iphead & 0x0f) * 4);
  printf(" TotalLen %d", (iphead[2] << 8 | iphead[3]));
  printf(" IP [%d.%d.%d.%d]", iphead[12], iphead[13], iphead[14],
    iphead[15]);
  printf("->[%d.%d.%d.%d]", iphead[16], iphead[17], iphead[18],
    iphead[19]);
  printf(" %d", iphead[9]);
  if (iphead[9] == IPPROTO_TCP)
   printf("[TCP]");
  else if (iphead[9] == IPPROTO_UDP)
   printf("[UDP]");
  else if (iphead[9] == IPPROTO_ICMP)
   printf("[ICMP]");
  else if (iphead[9] == IPPROTO_IGMP)
   printf("[IGMP]");
  else if (iphead[9] == IPPROTO_IGMP)
   printf("[IGMP]");
  else
   printf("[OTHERS]");
  printf(" PORT [%d]->[%d]\n", (iphead[20] << 8 | iphead[21]),
    (iphead[22] << 8 | iphead[23]));
 }
 close(sock);
 printf("Stop capturing packets!\n");
 exit(0);
}

variable_types: n int

variable_types:
buf
char [2048]

variable_types:
ethhead
unsigned char *

variable_types:
iphead
unsigned char *

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: struct sock_filter { unsigned short code; unsigned char jt; unsigned char jf; unsigned int k; } []

variable_types: struct sock_fprog { unsigned short len; struct sock_filter { unsigned short code; unsigned char jt; unsigned char jf; unsigned int k; } *filter; }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sock_fprog { unsigned short len; struct sock_filter *filter; }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
void __fastcall __noreturn sig_handler(int a1)
{
int v1; // ebp
__int64 v2[2]; // [rsp+0h] [rbp-38h] BYREF
__int16 v3; // [rsp+10h] [rbp-28h]

switch ( a1 )
{
case 15:
puts("SIGTERM recieved, exiting...");
break;
case 2:
puts("SIGINT recieved, exiting...");
break;
case 3:
puts("SIGQUIT recieved, exiting...");
break;
}
v1 = sock;
v2[0] = 828929125LL;
v2[1] = 0LL;
if ( ioctl(sock, 0x8913uLL, v2) != -1 )
{
v3 &= 0xFEFFu;
ioctl(v1, 0x8914uLL, v2);
}
close(v1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(int a1)
{
int v1; // ebp
__int64 v2[2]; // [rsp+0h] [rbp-38h] BYREF
__int16 v3; // [rsp+10h] [rbp-28h]

switch ( a1 )
{
case 15:
puts("SIGTERM recieved, exiting...");
break;
case 2:
puts("SIGINT recieved, exiting...");
break;
case 3:
puts("SIGQUIT recieved, exiting...");
break;
}
v1 = sock;
v2[0] = 828929125LL;
v2[1] = 0LL;
if ( ioctl(sock, 0x8913uLL, v2) != -1 )
{
v3 &= 0xFEFFu;
ioctl(v1, 0x8914uLL, v2);
}
close(v1);
exit(0);
}

original_code:
void sig_handler(int sig)
{
 struct ifreq ethreq;
 if (sig == 15)
  printf("SIGTERM recieved, exiting...\n");
 else if (sig == 2)
  printf("SIGINT recieved, exiting...\n");
 else if (sig == 3)
  printf("SIGQUIT recieved, exiting...\n");
 strncpy(ethreq.ifr_ifrn.ifrn_name, "eth1", 16);
 if (ioctl(sock, 0x8913, &ethreq) != -1)
 {
  ethreq.ifr_ifru.ifru_flags &= ~IFF_PROMISC;
  ioctl(sock, 0x8914, &ethreq);
 }
 close(sock);
 exit(0);
}

variable_types: sig int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


repo: frank-liu/Hortensia

decompiled_code:
__int64 __fastcall check_nic_up(char *s2)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er13
unsigned __int64 v4; // rdx
unsigned int v5; // eax
const char *v6; // rbx
const char *v7; // r14
_QWORD v9[8]; // [rsp+0h] [rbp-2F8h] BYREF
int v10; // [rsp+40h] [rbp-2B8h] BYREF
_QWORD *v11; // [rsp+48h] [rbp-2B0h]
_QWORD v12[85]; // [rsp+50h] [rbp-2A8h] BYREF

v1 = socket(2, 2, 0);
v2 = v1;
if ( v1 < 0 )
{
v3 = 0;
perror("check_nic_up: socket err\n");
}
else
{
v10 = 640;
v11 = v12;
v3 = ioctl(v1, 0x8912uLL, &v10);
if ( v3 )
{
v3 = 0;
perror("check_nic_up: ioctl err\n");
}
else
{
v4 = v10 / 0x28uLL;
v5 = v4 - 1;
if ( (int)v4 > 0 )
{
v6 = (const char *)&v12[5 * (int)v5];
v7 = (const char *)&v9[5 * (int)v4 + -5 * v5];
while ( 1 )
{
if ( strcmp(v6, s2) || ioctl(v2, 0x8913uLL, v6) )
goto LABEL_6;
if ( (v6[16] & 1) != 0 )
{
v3 = 1;
printf("Interface %s is UP.\n", v6);
LABEL_6:
v6 -= 40;
if ( v6 == v7 )
break;
}
else
{
v6 -= 40;
printf("Interface %s is DOWN. It needs to be set up.\n", s2);
if ( v6 == v7 )
break;
}
}
}
}
}
close(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s2)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er13
unsigned __int64 v4; // rdx
unsigned int v5; // eax
const char *v6; // rbx
const char *v7; // r14
_QWORD v9[8]; // [rsp+0h] [rbp-2F8h] BYREF
int v10; // [rsp+40h] [rbp-2B8h] BYREF
_QWORD *v11; // [rsp+48h] [rbp-2B0h]
_QWORD v12[85]; // [rsp+50h] [rbp-2A8h] BYREF

v1 = socket(2, 2, 0);
v2 = v1;
if ( v1 < 0 )
{
v3 = 0;
perror("check_nic_up: socket err\n");
}
else
{
v10 = 640;
v11 = v12;
v3 = ioctl(v1, 0x8912uLL, &v10);
if ( v3 )
{
v3 = 0;
perror("check_nic_up: ioctl err\n");
}
else
{
v4 = v10 / 0x28uLL;
v5 = v4 - 1;
if ( (int)v4 > 0 )
{
v6 = (const char *)&v12[5 * (int)v5];
v7 = (const char *)&v9[5 * (int)v4 + -5 * v5];
while ( 1 )
{
if ( strcmp(v6, s2) || ioctl(v2, 0x8913uLL, v6) )
goto LABEL_6;
if ( (v6[16] & 1) != 0 )
{
v3 = 1;
printf("Interface %s is UP.\n", v6);
LABEL_6:
v6 -= 40;
if ( v6 == v7 )
break;
}
else
{
v6 -= 40;
printf("Interface %s is DOWN. It needs to be set up.\n", s2);
if ( v6 == v7 )
break;
}
}
}
}
}
close(v2);
return v3;
}

original_code:
int check_nic_up(const char * nic)
{
 int fd, inf_cnt, rtn = 0;
 struct ifreq buf[16];
 struct ifconf ifc;
 if ((fd = socket(2, SOCK_DGRAM, 0)) >= 0)
 {
  ifc.ifc_len = sizeof buf;
  ifc.ifc_ifcu.ifcu_buf = (caddr_t) buf;
  if (!ioctl(fd, 0x8912, (char *) &ifc))
  {
   inf_cnt = ifc.ifc_len / sizeof(struct ifreq);
   while (inf_cnt-- > 0)
   {
    if (0 != strcmp(buf[inf_cnt].ifr_ifrn.ifrn_name, nic))
    {
     continue;
    }
    if (!(ioctl(fd, 0x8913, (char *) &buf[inf_cnt])))
    {
     if (buf[inf_cnt].ifr_ifru.ifru_flags & IFF_UP)
     {
      printf("Interface %s is UP.\n", buf[inf_cnt].ifr_ifrn.ifrn_name);
      rtn = 1;
     }
     else
     {
      printf("Interface %s is DOWN. It needs to be set up.\n",
        nic);
     }
    }
   }
  }
  else
   perror("check_nic_up: ioctl err\n");
 }
 else
  perror("check_nic_up: socket err\n");
 close(fd);
 return rtn;
}

canonical_original_code:
int check_nic_up(char *nic)
{
 int fd; int inf_cnt; int rtn = 0;
 struct ifreq buf[16];
 struct ifconf ifc;
 if ((fd = socket(2, SOCK_DGRAM, 0)) >= 0)
 {
  ifc.ifc_len = sizeof buf;
  ifc.ifc_ifcu.ifcu_buf = (char *) buf;
  if (!ioctl(fd, 0x8912, (char *) &ifc))
  {
   inf_cnt = ifc.ifc_len / sizeof(struct ifreq);
   while (inf_cnt-- > 0)
   {
    if (0 != strcmp(buf[inf_cnt].ifr_ifrn.ifrn_name, nic))
    {
     continue;
    }
    if (!(ioctl(fd, 0x8913, (char *) &buf[inf_cnt])))
    {
     if (buf[inf_cnt].ifr_ifru.ifru_flags & IFF_UP)
     {
      printf("Interface %s is UP.\n", buf[inf_cnt].ifr_ifrn.ifrn_name);
      rtn = 1;
     }
     else
     {
      printf("Interface %s is DOWN. It needs to be set up.\n",
        nic);
     }
    }
   }
  }
  else
   perror("check_nic_up: ioctl err\n");
 }
 else
  perror("check_nic_up: socket err\n");
 close(fd);
 return rtn;
}

variable_types:
nic
char *

variable_types: fd int

variable_types: rtn int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } [16]

variable_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } *ifcu_req; } ifc_ifcu; }

user_defined_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }


repo: frank-liu/aodv-multi-interface

decompiled_code:
__int64 __fastcall ifindex2devindex(int a1)
{
__int64 v1; // rdx
__int64 result; // rax

v1 = this_host[11];
if ( (int)v1 <= 0 )
return 10LL;
result = 0LL;
while ( dev_indices[result] != a1 )
{
if ( ++result == v1 )
return 10LL;
}
return (unsigned int)result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rdx
__int64 result; // rax

v1 = this_host[11];
if ( (int)v1 <= 0 )
return 10LL;
result = 0LL;
while ( dev_indices[result] != a1 )
{
if ( ++result == v1 )
return 10LL;
}
return (unsigned int)result;
}

original_code:
static inline unsigned int ifindex2devindex(unsigned int ifindex)
{
    int i;
    for (i = 0; i < this_host.nif; i++)
 if (dev_indices[i] == ifindex)
     return i;
    return 10;
}

variable_types:
ifindex
unsigned int

variable_types: i int

return_type: unsigned int


original_code:
RREP * rrep_create(u_int8_t flags,
      u_int8_t prefix,
      u_int8_t hcnt,
      struct in_addr dest_addr,
      u_int32_t dest_seqno,
      struct in_addr orig_addr, u_int32_t life)
{
    RREP *rrep;
    rrep = (RREP *) aodv_socket_new_msg();
    rrep->type = 2;
    rrep->res1 = 0;
    rrep->res2 = 0;
    rrep->prefix = prefix;
    rrep->hcnt = hcnt;
    rrep->dest_addr = dest_addr.s_addr;
    rrep->dest_seqno = htonl(dest_seqno);
    rrep->orig_addr = orig_addr.s_addr;
    rrep->lifetime = htonl(life);
    if (flags & 0x2)
 rrep->r = 1;
    if (flags & 0x1)
 rrep->a = 1;
    if (rrep->dest_addr != rrep->orig_addr) {
 alog(7, 0, __FUNCTION__, "Assembled RREP:");
 log_pkt_fields((AODV_msg *) rrep);
    }
    return rrep;
}

canonical_original_code:
struct RREP *rrep_create(unsigned int flags, unsigned int prefix, unsigned int hcnt, struct in_addr dest_addr, unsigned int dest_seqno, struct in_addr orig_addr, unsigned int life)
{
    struct RREP *rrep;
    rrep = (struct RREP *) aodv_socket_new_msg();
    rrep->type = 2;
    rrep->res1 = 0;
    rrep->res2 = 0;
    rrep->prefix = prefix;
    rrep->hcnt = hcnt;
    rrep->dest_addr = dest_addr.s_addr;
    rrep->dest_seqno = htonl(dest_seqno);
    rrep->orig_addr = orig_addr.s_addr;
    rrep->lifetime = htonl(life);
    if (flags & 0x2)
 rrep->r = 1;
    if (flags & 0x1)
 rrep->a = 1;
    if (rrep->dest_addr != rrep->orig_addr) {
 alog(7, 0, __FUNCTION__, "Assembled RREP:");
 log_pkt_fields((struct AODV_msg *) rrep);
    }
    return rrep;
}

variable_types:
flags
unsigned int

variable_types:
prefix
unsigned int

variable_types:
hcnt
unsigned int

variable_types:
life
unsigned int

variable_types: struct RREP { unsigned int type; unsigned int res1; unsigned int a; unsigned int r; unsigned int prefix; unsigned int res2; unsigned int hcnt; unsigned int dest_addr; unsigned int dest_seqno; unsigned int orig_addr; unsigned int lifetime; } *


decompiled_code:
_BYTE *rrep_ack_create()
{
_BYTE *v0; // rax
_BYTE *v1; // r12

v0 = (_BYTE *)aodv_socket_new_msg();
*v0 = 4;
v1 = v0;
alog(7LL, 0LL, "rrep_ack_create", "Assembled RREP_ack");
return v1;
}

canonical_decompiled_code:
_BYTE *func3()
{
_BYTE *v0; // rax
_BYTE *v1; // r12

v0 = (_BYTE *)aodv_socket_new_msg();
*v0 = 4;
v1 = v0;
alog(7LL, 0LL, "rrep_ack_create", "Assembled RREP_ack");
return v1;
}

original_code:
RREP_ack * rrep_ack_create()
{
    RREP_ack *rrep_ack;
    rrep_ack = (RREP_ack *) aodv_socket_new_msg();
    rrep_ack->type = 4;
    alog(7, 0, __FUNCTION__, "Assembled RREP_ack");
    return rrep_ack;
}

canonical_original_code:
struct RREP_ack *rrep_ack_create()
{
    struct RREP_ack *rrep_ack;
    rrep_ack = (struct RREP_ack *) aodv_socket_new_msg();
    rrep_ack->type = 4;
    alog(7, 0, __FUNCTION__, "Assembled RREP_ack");
    return rrep_ack;
}

variable_types: struct RREP_ack { unsigned int type; unsigned int reserved; } *


decompiled_code:
__int64 __fastcall rrep_ack_process(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rax
__int64 v5; // rbx
const char *v6; // rax
__int64 result; // rax
const char *v8; // rax

v4 = rt_table_find(a3);
if ( v4 )
{
v5 = v4;
v6 = (const char *)ip_to_str(a3);
alog(7LL, 0LL, "rrep_ack_process", "Received RREP_ACK from %s", v6);
result = timer_remove(v5 + 96);
}
else
{
v8 = (const char *)ip_to_str(a3);
result = alog(4LL, 0LL, "rrep_ack_process", "No RREP_ACK expected for %s", v8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rax
__int64 v5; // rbx
const char *v6; // rax
__int64 result; // rax
const char *v8; // rax

v4 = rt_table_find(a3);
if ( v4 )
{
v5 = v4;
v6 = (const char *)ip_to_str(a3);
alog(7LL, 0LL, "rrep_ack_process", "Received RREP_ACK from %s", v6);
result = timer_remove(v5 + 96);
}
else
{
v8 = (const char *)ip_to_str(a3);
result = alog(4LL, 0LL, "rrep_ack_process", "No RREP_ACK expected for %s", v8);
}
return result;
}

original_code:
void rrep_ack_process(RREP_ack * rrep_ack, int rrep_acklen,
          struct in_addr ip_src, struct in_addr ip_dst)
{
    rt_table_t *rt;
    rt = rt_table_find(ip_src);
    if (rt == ((void *)0)) {
 alog(4, 0, __FUNCTION__, "No RREP_ACK expected for %s", ip_to_str(ip_src));
 return;
    }
    alog(7, 0, __FUNCTION__, "Received RREP_ACK from %s", ip_to_str(ip_src));
    timer_remove(&rt->ack_timer);
}

canonical_original_code:
void rrep_ack_process(struct RREP_ack *rrep_ack, int rrep_acklen, struct in_addr ip_src, struct in_addr ip_dst)
{
    struct rt_table *rt;
    rt = rt_table_find(ip_src);
    if (rt == ((void *)0)) {
 alog(4, 0, __FUNCTION__, "No RREP_ACK expected for %s", ip_to_str(ip_src));
 return;
    }
    alog(7, 0, __FUNCTION__, "Received RREP_ACK from %s", ip_to_str(ip_src));
    timer_remove(&rt->ack_timer);
}

variable_types: struct rt_table { struct list_t { struct list_t *prev; } l; struct in_addr { unsigned int s_addr; } dest_addr; unsigned int dest_seqno; unsigned int ifindex; struct in_addr next_hop; unsigned int hcnt; unsigned int flags; unsigned int state; struct timer { struct list_t l; int used; struct timeval { long tv_sec; long tv_usec; } timeout; void (*handler)(void *); void *data; } rt_timer; struct timer ack_timer; struct timer hello_timer; struct timeval last_hello_time; unsigned int hello_cnt; unsigned int hash; int nprec; struct list_t precursors; } *

user_defined_types: struct RREP_ack { unsigned int type; unsigned int reserved; }

user_defined_types: struct rt_table { struct list_t l; struct in_addr dest_addr; unsigned int dest_seqno; unsigned int ifindex; struct in_addr next_hop; unsigned int hcnt; unsigned int flags; unsigned int state; struct timer rt_timer; struct timer ack_timer; struct timer hello_timer; struct timeval last_hello_time; unsigned int hello_cnt; unsigned int hash; int nprec; struct list_t precursors; }

user_defined_types: struct timer { struct list_t l; int used; struct timeval timeout; void (*handler)(void *); void *data; }


decompiled_code:
__int64 __fastcall rrep_send(__int64 a1, __int64 a2, __int64 a3, unsigned int a4)
{
unsigned int v7; // er14
const char *v8; // r15
const char *v9; // r14
const char *v10; // rax
int v11; // eax
__int64 v12; // rcx
__int64 result; // rax
__int64 v14; // rsi
__int64 v15; // rax
__int64 v16; // r15
const char *v17; // rax
__int64 v18; // [rsp-40h] [rbp-40h]

if ( !a2 )
return alog(4LL, 0LL, "rrep_send", "Can't send RREP, rev_rt = NULL!");
v7 = *(_DWORD *)(a1 + 4);
if ( *(_BYTE *)(a2 + 36) != 1 || (*(_BYTE *)(a2 + 34) & 1) == 0 )
{
if ( *(_BYTE *)(a2 + 32) != 1 )
goto LABEL_4;
if ( !unidir_hack )
goto LABEL_4;
v15 = rt_table_find(*(unsigned int *)(a2 + 28));
v16 = v15;
if ( !v15 )
goto LABEL_4;
LABEL_11:
if ( *(_BYTE *)(v15 + 36) == 1 && !*(_DWORD *)(v15 + 112) )
{
*(_WORD *)(v15 + 34) |= 1u;
timer_remove(v15 + 152);
neighbor_link_break(v16);
v17 = (const char *)ip_to_str(*(unsigned int *)(v16 + 16));
alog(7LL, 0LL, "rrep_send", "Link to %s is unidirectional!", v17);
timer_set_timeout(v16 + 96, 50LL);
}
goto LABEL_4;
}
v15 = rt_table_find(*(unsigned int *)(a2 + 28));
v16 = v15;
if ( v15 )
goto LABEL_11;
LABEL_4:
v8 = (const char *)ip_to_str(v7);
v9 = (const char *)ip_to_str(*(unsigned int *)(a2 + 16));
v10 = (const char *)ip_to_str(*(unsigned int *)(a2 + 28));
alog(7LL, 0LL, "rrep_send", "Sending RREP to next hop %s about %s->%s", v10, v9, v8);
v11 = ifindex2devindex(*(_DWORD *)(a2 + 24));
result = aodv_socket_send(a1, *(unsigned int *)(a2 + 28), a4, v12, &this_host[11 * v11 + 12]);
v14 = v18;
if ( a3 )
{
precursor_add(a3, *(unsigned int *)(a2 + 28));
v14 = *(unsigned int *)(a3 + 28);
a1 = a2;
result = precursor_add(a2, v14);
}
if ( !llfeedback )
{
result = optimized_hellos;
if ( optimized_hellos )
result = hello_start(a1, v14);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, unsigned int a4)
{
unsigned int v7; // er14
const char *v8; // r15
const char *v9; // r14
const char *v10; // rax
int v11; // eax
__int64 v12; // rcx
__int64 result; // rax
__int64 v14; // rsi
__int64 v15; // rax
__int64 v16; // r15
const char *v17; // rax
__int64 v18; // [rsp-40h] [rbp-40h]

if ( !a2 )
return alog(4LL, 0LL, "rrep_send", "Can't send RREP, rev_rt = NULL!");
v7 = *(_DWORD *)(a1 + 4);
if ( *(_BYTE *)(a2 + 36) != 1 || (*(_BYTE *)(a2 + 34) & 1) == 0 )
{
if ( *(_BYTE *)(a2 + 32) != 1 )
goto LABEL_4;
if ( !unidir_hack )
goto LABEL_4;
v15 = rt_table_find(*(unsigned int *)(a2 + 28));
v16 = v15;
if ( !v15 )
goto LABEL_4;
LABEL_11:
if ( *(_BYTE *)(v15 + 36) == 1 && !*(_DWORD *)(v15 + 112) )
{
*(_WORD *)(v15 + 34) |= 1u;
timer_remove(v15 + 152);
neighbor_link_break(v16);
v17 = (const char *)ip_to_str(*(unsigned int *)(v16 + 16));
alog(7LL, 0LL, "rrep_send", "Link to %s is unidirectional!", v17);
timer_set_timeout(v16 + 96, 50LL);
}
goto LABEL_4;
}
v15 = rt_table_find(*(unsigned int *)(a2 + 28));
v16 = v15;
if ( v15 )
goto LABEL_11;
LABEL_4:
v8 = (const char *)ip_to_str(v7);
v9 = (const char *)ip_to_str(*(unsigned int *)(a2 + 16));
v10 = (const char *)ip_to_str(*(unsigned int *)(a2 + 28));
alog(7LL, 0LL, "rrep_send", "Sending RREP to next hop %s about %s->%s", v10, v9, v8);
v11 = func0(*(_DWORD *)(a2 + 24));
result = aodv_socket_send(a1, *(unsigned int *)(a2 + 28), a4, v12, &this_host[11 * v11 + 12]);
v14 = v18;
if ( a3 )
{
precursor_add(a3, *(unsigned int *)(a2 + 28));
v14 = *(unsigned int *)(a3 + 28);
a1 = a2;
result = precursor_add(a2, v14);
}
if ( !llfeedback )
{
result = optimized_hellos;
if ( optimized_hellos )
result = hello_start(a1, v14);
}
return result;
}

original_code:
void rrep_send(RREP * rrep, rt_table_t * rev_rt,
   rt_table_t * fwd_rt, int size)
{
    u_int8_t rrep_flags = 0;
    struct in_addr dest;
    if (!rev_rt) {
 alog(4, 0, __FUNCTION__, "Can't send RREP, rev_rt = NULL!");
 return;
    }
    dest.s_addr = rrep->dest_addr;
    if ((rev_rt->state == 1 && rev_rt->flags & 0x1) ||
 (rev_rt->hcnt == 1 && unidir_hack)) {
 rt_table_t *neighbor = rt_table_find(rev_rt->next_hop);
 if (neighbor && neighbor->state == 1 && !neighbor->ack_timer.used) {
     rrep_flags |= 0x1;
     neighbor->flags |= 0x1;
     timer_remove(&neighbor->hello_timer);
     neighbor_link_break(neighbor);
     alog(7, 0, __FUNCTION__, "Link to %s is unidirectional!", ip_to_str(neighbor->dest_addr));
     timer_set_timeout(&neighbor->ack_timer, 40 + 10);
 }
    }
    alog(7, 0, __FUNCTION__, "Sending RREP to next hop %s about %s->%s", ip_to_str(rev_rt->next_hop), ip_to_str(rev_rt->dest_addr), ip_to_str(dest));
    aodv_socket_send((AODV_msg *) rrep, rev_rt->next_hop, size, 255,
       &(this_host.devs[ifindex2devindex(rev_rt->ifindex)]));
    if (fwd_rt) {
 precursor_add(fwd_rt, rev_rt->next_hop);
 precursor_add(rev_rt, fwd_rt->next_hop);
    }
    if (!llfeedback && optimized_hellos)
 hello_start();
}

canonical_original_code:
void rrep_send(struct RREP *rrep, struct rt_table *rev_rt, struct rt_table *fwd_rt, int size)
{
    unsigned int rrep_flags = 0;
    struct in_addr dest;
    if (!rev_rt) {
 alog(4, 0, __FUNCTION__, "Can't send RREP, rev_rt = NULL!");
 return;
    }
    dest.s_addr = rrep->dest_addr;
    if ((rev_rt->state == 1 && rev_rt->flags & 0x1) ||
 (rev_rt->hcnt == 1 && unidir_hack)) {
 struct rt_table *neighbor = rt_table_find(rev_rt->next_hop);
 if (neighbor && neighbor->state == 1 && !neighbor->ack_timer.used) {
     rrep_flags |= 0x1;
     neighbor->flags |= 0x1;
     timer_remove(&neighbor->hello_timer);
     neighbor_link_break(neighbor);
     alog(7, 0, __FUNCTION__, "Link to %s is unidirectional!", ip_to_str(neighbor->dest_addr));
     timer_set_timeout(&neighbor->ack_timer, 40 + 10);
 }
    }
    alog(7, 0, __FUNCTION__, "Sending RREP to next hop %s about %s->%s", ip_to_str(rev_rt->next_hop), ip_to_str(rev_rt->dest_addr), ip_to_str(dest));
    aodv_socket_send((struct AODV_msg *) rrep, rev_rt->next_hop, size, 255,
       &(this_host.devs[ifindex2devindex(rev_rt->ifindex)]));
    if (fwd_rt) {
 precursor_add(fwd_rt, rev_rt->next_hop);
 precursor_add(rev_rt, fwd_rt->next_hop);
    }
    if (!llfeedback && optimized_hellos)
 hello_start();
}

variable_types: size int

variable_types:
dest
struct in_addr { unsigned int s_addr; }

user_defined_types: struct RREP { unsigned int type; unsigned int res1; unsigned int a; unsigned int r; unsigned int prefix; unsigned int res2; unsigned int hcnt; unsigned int dest_addr; unsigned int dest_seqno; unsigned int orig_addr; unsigned int lifetime; }


decompiled_code:
__int64 __fastcall rrep_forward(__int64 a1, unsigned int a2, __int64 a3, __int64 a4)
{
const char *v6; // rax
int v7; // eax
__int64 v8; // rcx
__int64 v9; // r9

if ( !a4 || !a3 )
return alog(4LL, 0LL, "rrep_forward", "Could not forward RREP because of NULL route!");
if ( !a1 )
return alog(4LL, 0LL, "rrep_forward", "No RREP to forward!");
v6 = (const char *)ip_to_str(*(unsigned int *)(a3 + 28));
alog(7LL, 0LL, "rrep_forward", "Forwarding RREP to %s", v6);
*(_BYTE *)(aodv_socket_queue_msg(a1, a2) + 3) = *(_BYTE *)(a4 + 32);
v7 = ifindex2devindex(*(_DWORD *)(a3 + 24));
aodv_socket_send(v9, *(unsigned int *)(a3 + 28), a2, v8, &this_host[11 * v7 + 12]);
precursor_add(a4, *(unsigned int *)(a3 + 28));
precursor_add(a3, *(unsigned int *)(a4 + 28));
return rt_table_update_timeout(a3, active_route_timeout);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2, __int64 a3, __int64 a4)
{
const char *v6; // rax
int v7; // eax
__int64 v8; // rcx
__int64 v9; // r9

if ( !a4 || !a3 )
return alog(4LL, 0LL, "rrep_forward", "Could not forward RREP because of NULL route!");
if ( !a1 )
return alog(4LL, 0LL, "rrep_forward", "No RREP to forward!");
v6 = (const char *)ip_to_str(*(unsigned int *)(a3 + 28));
alog(7LL, 0LL, "rrep_forward", "Forwarding RREP to %s", v6);
*(_BYTE *)(aodv_socket_queue_msg(a1, a2) + 3) = *(_BYTE *)(a4 + 32);
v7 = func0(*(_DWORD *)(a3 + 24));
aodv_socket_send(v9, *(unsigned int *)(a3 + 28), a2, v8, &this_host[11 * v7 + 12]);
precursor_add(a4, *(unsigned int *)(a3 + 28));
precursor_add(a3, *(unsigned int *)(a4 + 28));
return rt_table_update_timeout(a3, active_route_timeout);
}

original_code:
void rrep_forward(RREP * rrep, int size, rt_table_t * rev_rt,
      rt_table_t * fwd_rt, int ttl)
{
    if (!fwd_rt || !rev_rt) {
 alog(4, 0, __FUNCTION__, "Could not forward RREP because of NULL route!");
 return;
    }
    if (!rrep) {
 alog(4, 0, __FUNCTION__, "No RREP to forward!");
 return;
    }
    alog(7, 0, __FUNCTION__, "Forwarding RREP to %s", ip_to_str(rev_rt->next_hop));
    if (0) {
 rt_table_t *neighbor;
 if (rev_rt->dest_addr.s_addr != rev_rt->next_hop.s_addr)
     neighbor = rt_table_find(rev_rt->next_hop);
 else
     neighbor = rev_rt;
 if (neighbor && !neighbor->ack_timer.used) {
     rrep->a = 1;
     neighbor->flags |= 0x1;
     timer_set_timeout(&neighbor->ack_timer, 40 + 10);
 }
    }
    rrep = (RREP *) aodv_socket_queue_msg((AODV_msg *) rrep, size);
    rrep->hcnt = fwd_rt->hcnt;
    aodv_socket_send((AODV_msg *) rrep, rev_rt->next_hop, size, ttl,
       &(this_host.devs[ifindex2devindex(rev_rt->ifindex)]));
    precursor_add(fwd_rt, rev_rt->next_hop);
    precursor_add(rev_rt, fwd_rt->next_hop);
    rt_table_update_timeout(rev_rt, active_route_timeout);
}

canonical_original_code:
void rrep_forward(struct RREP *rrep, int size, struct rt_table *rev_rt, struct rt_table *fwd_rt, int ttl)
{
    if (!fwd_rt || !rev_rt) {
 alog(4, 0, __FUNCTION__, "Could not forward RREP because of NULL route!");
 return;
    }
    if (!rrep) {
 alog(4, 0, __FUNCTION__, "No RREP to forward!");
 return;
    }
    alog(7, 0, __FUNCTION__, "Forwarding RREP to %s", ip_to_str(rev_rt->next_hop));
    if (0) {
 struct rt_table *neighbor;
 if (rev_rt->dest_addr.s_addr != rev_rt->next_hop.s_addr)
     neighbor = rt_table_find(rev_rt->next_hop);
 else
     neighbor = rev_rt;
 if (neighbor && !neighbor->ack_timer.used) {
     rrep->a = 1;
     neighbor->flags |= 0x1;
     timer_set_timeout(&neighbor->ack_timer, 40 + 10);
 }
    }
    rrep = (struct RREP *) aodv_socket_queue_msg((struct AODV_msg *) rrep, size);
    rrep->hcnt = fwd_rt->hcnt;
    aodv_socket_send((struct AODV_msg *) rrep, rev_rt->next_hop, size, ttl,
       &(this_host.devs[ifindex2devindex(rev_rt->ifindex)]));
    precursor_add(fwd_rt, rev_rt->next_hop);
    precursor_add(rev_rt, fwd_rt->next_hop);
    rt_table_update_timeout(rev_rt, active_route_timeout);
}

variable_types: ttl int